mvebu: backport upstream ethernet driver improvements and enable buffer manager support
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.4 / 044-net-add-a-hardware-buffer-management-helper-API.patch
1 From: Gregory CLEMENT <gregory.clement@free-electrons.com>
2 Date: Mon, 14 Mar 2016 09:39:04 +0100
3 Subject: [PATCH] net: add a hardware buffer management helper API
4
5 This basic implementation allows to share code between driver using
6 hardware buffer management. As the code is hardware agnostic, there is
7 few helpers, most of the optimization brought by the an HW BM has to be
8 done at driver level.
9
10 Tested-by: Sebastian Careba <nitroshift@yahoo.com>
11 Signed-off-by: Gregory CLEMENT <gregory.clement@free-electrons.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
13 ---
14 create mode 100644 include/net/hwbm.h
15 create mode 100644 net/core/hwbm.c
16
17 --- /dev/null
18 +++ b/include/net/hwbm.h
19 @@ -0,0 +1,28 @@
20 +#ifndef _HWBM_H
21 +#define _HWBM_H
22 +
23 +struct hwbm_pool {
24 + /* Capacity of the pool */
25 + int size;
26 + /* Size of the buffers managed */
27 + int frag_size;
28 + /* Number of buffers currently used by this pool */
29 + int buf_num;
30 + /* constructor called during alocation */
31 + int (*construct)(struct hwbm_pool *bm_pool, void *buf);
32 + /* protect acces to the buffer counter*/
33 + spinlock_t lock;
34 + /* private data */
35 + void *priv;
36 +};
37 +#ifdef CONFIG_HWBM
38 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf);
39 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp);
40 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp);
41 +#else
42 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf) {}
43 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp) { return 0; }
44 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
45 +{ return 0; }
46 +#endif /* CONFIG_HWBM */
47 +#endif /* _HWBM_H */
48 --- a/net/Kconfig
49 +++ b/net/Kconfig
50 @@ -259,6 +259,9 @@ config XPS
51 depends on SMP
52 default y
53
54 +config HWBM
55 + bool
56 +
57 config CGROUP_NET_PRIO
58 bool "Network priority cgroup"
59 depends on CGROUPS
60 --- a/net/core/Makefile
61 +++ b/net/core/Makefile
62 @@ -14,6 +14,7 @@ obj-y += dev.o ethtool.o dev_addr_
63 obj-$(CONFIG_SOCK_DIAG) += sock_diag.o
64 obj-$(CONFIG_XFRM) += flow.o
65 obj-y += net-sysfs.o
66 +obj-$(CONFIG_HWBM) += hwbm.o
67 obj-$(CONFIG_PROC_FS) += net-procfs.o
68 obj-$(CONFIG_NET_PKTGEN) += pktgen.o
69 obj-$(CONFIG_NETPOLL) += netpoll.o
70 --- /dev/null
71 +++ b/net/core/hwbm.c
72 @@ -0,0 +1,87 @@
73 +/* Support for hardware buffer manager.
74 + *
75 + * Copyright (C) 2016 Marvell
76 + *
77 + * Gregory CLEMENT <gregory.clement@free-electrons.com>
78 + *
79 + * This program is free software; you can redistribute it and/or modify
80 + * it under the terms of the GNU General Public License as published by
81 + * the Free Software Foundation; either version 2 of the License, or
82 + * (at your option) any later version.
83 + */
84 +#include <linux/kernel.h>
85 +#include <linux/printk.h>
86 +#include <linux/skbuff.h>
87 +#include <net/hwbm.h>
88 +
89 +void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf)
90 +{
91 + if (likely(bm_pool->frag_size <= PAGE_SIZE))
92 + skb_free_frag(buf);
93 + else
94 + kfree(buf);
95 +}
96 +EXPORT_SYMBOL_GPL(hwbm_buf_free);
97 +
98 +/* Refill processing for HW buffer management */
99 +int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp)
100 +{
101 + int frag_size = bm_pool->frag_size;
102 + void *buf;
103 +
104 + if (likely(frag_size <= PAGE_SIZE))
105 + buf = netdev_alloc_frag(frag_size);
106 + else
107 + buf = kmalloc(frag_size, gfp);
108 +
109 + if (!buf)
110 + return -ENOMEM;
111 +
112 + if (bm_pool->construct)
113 + if (bm_pool->construct(bm_pool, buf)) {
114 + hwbm_buf_free(bm_pool, buf);
115 + return -ENOMEM;
116 + }
117 +
118 + return 0;
119 +}
120 +EXPORT_SYMBOL_GPL(hwbm_pool_refill);
121 +
122 +int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
123 +{
124 + int err, i;
125 + unsigned long flags;
126 +
127 + spin_lock_irqsave(&bm_pool->lock, flags);
128 + if (bm_pool->buf_num == bm_pool->size) {
129 + pr_warn("pool already filled\n");
130 + return bm_pool->buf_num;
131 + }
132 +
133 + if (buf_num + bm_pool->buf_num > bm_pool->size) {
134 + pr_warn("cannot allocate %d buffers for pool\n",
135 + buf_num);
136 + return 0;
137 + }
138 +
139 + if ((buf_num + bm_pool->buf_num) < bm_pool->buf_num) {
140 + pr_warn("Adding %d buffers to the %d current buffers will overflow\n",
141 + buf_num, bm_pool->buf_num);
142 + return 0;
143 + }
144 +
145 + for (i = 0; i < buf_num; i++) {
146 + err = hwbm_pool_refill(bm_pool, gfp);
147 + if (err < 0)
148 + break;
149 + }
150 +
151 + /* Update BM driver with number of buffers added to pool */
152 + bm_pool->buf_num += i;
153 +
154 + pr_debug("hwpm pool: %d of %d buffers added\n", i, buf_num);
155 + spin_unlock_irqrestore(&bm_pool->lock, flags);
156 +
157 + return i;
158 +}
159 +EXPORT_SYMBOL_GPL(hwbm_pool_add);