cns3xxx: fix ethernet jumbo-frame support, use single-page rx buffers and skb fragmen...
[openwrt/svn-archive/archive.git] / target / linux / cns3xxx / patches-3.3 / 410-ethernet_fix_jumbo_frame.patch
1 --- a/drivers/net/ethernet/cavium/cns3xxx_eth.c
2 +++ b/drivers/net/ethernet/cavium/cns3xxx_eth.c
3 @@ -26,15 +26,18 @@
4
5 #define DRV_NAME "cns3xxx_eth"
6
7 -#define RX_DESCS 512
8 -#define TX_DESCS 512
9 -#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
10 +#define RX_DESCS 128
11 +#define TX_DESCS 128
12
13 #define RX_POOL_ALLOC_SIZE (sizeof(struct rx_desc) * RX_DESCS)
14 #define TX_POOL_ALLOC_SIZE (sizeof(struct tx_desc) * TX_DESCS)
15 #define REGS_SIZE 336
16 -#define MAX_MRU (1536 + SKB_DMA_REALIGN)
17 -#define CNS3XXX_MAX_MTU (1536)
18 +
19 +#define SKB_HEAD_ALIGN (((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES) + NET_SKB_PAD + NET_IP_ALIGN)
20 +#define RX_SEGMENT_ALLOC_SIZE 4096
21 +#define RX_SEGMENT_BUFSIZE (SKB_WITH_OVERHEAD(RX_SEGMENT_ALLOC_SIZE))
22 +#define RX_SEGMENT_MRU ((RX_SEGMENT_BUFSIZE - SKB_HEAD_ALIGN))
23 +#define MAX_MTU 9500
24
25 #define NAPI_WEIGHT 64
26
27 @@ -266,7 +269,7 @@ struct _rx_ring {
28 struct rx_desc *desc;
29 dma_addr_t phys_addr;
30 struct rx_desc *cur_addr;
31 - struct sk_buff *buff_tab[RX_DESCS];
32 + void *buff_tab[RX_DESCS];
33 unsigned int phys_tab[RX_DESCS];
34 u32 cur_index;
35 u32 alloc_index;
36 @@ -280,6 +283,8 @@ struct sw {
37 struct cns3xxx_plat_info *plat;
38 struct _tx_ring *tx_ring;
39 struct _rx_ring *rx_ring;
40 + struct sk_buff *frag_first;
41 + struct sk_buff *frag_last;
42 };
43
44 struct port {
45 @@ -500,37 +505,35 @@ static void cns3xxx_alloc_rx_buf(struct
46 struct _rx_ring *rx_ring = sw->rx_ring;
47 unsigned int i = rx_ring->alloc_index;
48 struct rx_desc *desc = &(rx_ring)->desc[i];
49 - struct sk_buff *skb;
50 + void *buf;
51 unsigned int phys;
52
53 for (received += rx_ring->alloc_count; received > 0; received--) {
54 - if ((skb = dev_alloc_skb(MAX_MRU))) {
55 - if (SKB_DMA_REALIGN)
56 - skb_reserve(skb, SKB_DMA_REALIGN);
57 - skb_reserve(skb, NET_IP_ALIGN);
58 - phys = dma_map_single(NULL, skb->data,
59 - CNS3XXX_MAX_MTU, DMA_FROM_DEVICE);
60 - if (dma_mapping_error(NULL, phys)) {
61 - dev_kfree_skb(skb);
62 - /* Failed to map, better luck next time */
63 - goto out;;
64 - }
65 - desc->sdp = phys;
66 - } else {
67 - /* Failed to allocate skb, try again next time */
68 + buf = kzalloc(RX_SEGMENT_ALLOC_SIZE, GFP_ATOMIC);
69 + if (!buf)
70 + goto out;
71 +
72 + phys = dma_map_single(NULL, buf + SKB_HEAD_ALIGN,
73 + RX_SEGMENT_MRU, DMA_FROM_DEVICE);
74 + if (dma_mapping_error(NULL, phys)) {
75 + kfree(buf);
76 goto out;
77 }
78
79 + desc->sdl = RX_SEGMENT_MRU;
80 + desc->sdp = phys;
81 +
82 /* put the new buffer on RX-free queue */
83 - rx_ring->buff_tab[i] = skb;
84 + rx_ring->buff_tab[i] = buf;
85 rx_ring->phys_tab[i] = phys;
86 if (i == RX_DESCS - 1) {
87 i = 0;
88 desc->config0 = END_OF_RING | FIRST_SEGMENT |
89 - LAST_SEGMENT | CNS3XXX_MAX_MTU;
90 + LAST_SEGMENT | RX_SEGMENT_MRU;
91 desc = &(rx_ring)->desc[i];
92 } else {
93 - desc->config0 = FIRST_SEGMENT | LAST_SEGMENT | CNS3XXX_MAX_MTU;
94 + desc->config0 = FIRST_SEGMENT | LAST_SEGMENT |
95 + RX_SEGMENT_MRU;
96 i++;
97 desc++;
98 }
99 @@ -588,49 +591,78 @@ static int eth_poll(struct napi_struct *
100
101 while (desc->cown) {
102 struct sk_buff *skb;
103 + int reserve = SKB_HEAD_ALIGN;
104
105 if (received >= budget)
106 break;
107
108 - skb = rx_ring->buff_tab[i];
109 + /* process received frame */
110 + dma_unmap_single(&dev->dev, rx_ring->phys_tab[i],
111 + RX_SEGMENT_MRU, DMA_FROM_DEVICE);
112 +
113 + skb = build_skb(rx_ring->buff_tab[i]);
114 + if (!skb)
115 + break;
116
117 dev = switch_port_tab[desc->sp]->netdev;
118
119 length = desc->sdl;
120 - /* process received frame */
121 - dma_unmap_single(&dev->dev, rx_ring->phys_tab[i],
122 - length, DMA_FROM_DEVICE);
123 + if (desc->fsd && !desc->lsd)
124 + length = RX_SEGMENT_MRU;
125
126 + if (!desc->fsd) {
127 + reserve -= NET_IP_ALIGN;
128 + if (!desc->lsd)
129 + length += NET_IP_ALIGN;
130 + }
131 +
132 + skb_reserve(skb, reserve);
133 skb_put(skb, length);
134
135 - skb->dev = dev;
136 - skb->protocol = eth_type_trans(skb, dev);
137 + if (!sw->frag_first)
138 + sw->frag_first = skb;
139 + else {
140 + if (sw->frag_first == sw->frag_last)
141 + skb_frag_add_head(sw->frag_first, skb);
142 + sw->frag_last->next = skb;
143 + sw->frag_first->len += skb->len;
144 + sw->frag_first->data_len += skb->len;
145 + sw->frag_first->truesize += skb->truesize;
146 + }
147 + sw->frag_last = skb;
148
149 - dev->stats.rx_packets++;
150 - dev->stats.rx_bytes += length;
151 + if (desc->lsd) {
152 + skb = sw->frag_first;
153 + skb->protocol = eth_type_trans(skb, dev);
154 +
155 + dev->stats.rx_packets++;
156 + dev->stats.rx_bytes += skb->len;
157 +
158 + /* RX Hardware checksum offload */
159 + skb->ip_summed = CHECKSUM_NONE;
160 + switch (desc->prot) {
161 + case 1:
162 + case 2:
163 + case 5:
164 + case 6:
165 + case 13:
166 + case 14:
167 + if (desc->l4f)
168 + break;
169
170 - /* RX Hardware checksum offload */
171 - switch (desc->prot) {
172 - case 1:
173 - case 2:
174 - case 5:
175 - case 6:
176 - case 13:
177 - case 14:
178 - if (desc->l4f)
179 - skb->ip_summed = CHECKSUM_NONE;
180 - else
181 skb->ip_summed = CHECKSUM_UNNECESSARY;
182 - break;
183 - default:
184 - skb->ip_summed = CHECKSUM_NONE;
185 - break;
186 - }
187 + break;
188 + default:
189 + break;
190 + }
191
192 - napi_gro_receive(napi, skb);
193 + napi_gro_receive(napi, skb);
194
195 - received++;
196 + sw->frag_first = NULL;
197 + sw->frag_last = NULL;
198 + }
199
200 + received++;
201 if (++i == RX_DESCS) {
202 i = 0;
203 desc = &(rx_ring)->desc[i];
204 @@ -671,12 +703,6 @@ static int eth_xmit(struct sk_buff *skb,
205 if (pmap == 8)
206 pmap = (1 << 4);
207
208 - if (skb->len > CNS3XXX_MAX_MTU) {
209 - dev_kfree_skb(skb);
210 - dev->stats.tx_errors++;
211 - return NETDEV_TX_OK;
212 - }
213 -
214 spin_lock(&tx_lock);
215
216 if ((tx_ring->num_used + nr_frags) >= TX_DESCS) {
217 @@ -701,8 +727,7 @@ static int eth_xmit(struct sk_buff *skb,
218
219 len = skb->len;
220
221 - phys = dma_map_single(NULL, skb->data, len,
222 - DMA_TO_DEVICE);
223 + phys = dma_map_single(NULL, skb->data, len, DMA_TO_DEVICE);
224
225 tx_desc->sdp = phys;
226 tx_desc->pmap = pmap;
227 @@ -849,24 +874,24 @@ static int init_rings(struct sw *sw)
228 /* Setup RX buffers */
229 for (i = 0; i < RX_DESCS; i++) {
230 struct rx_desc *desc = &(rx_ring)->desc[i];
231 - struct sk_buff *skb;
232 - if (!(skb = dev_alloc_skb(MAX_MRU)))
233 + void *buf;
234 +
235 + buf = kzalloc(RX_SEGMENT_ALLOC_SIZE, GFP_KERNEL);
236 + if (!buf)
237 return -ENOMEM;
238 - if (SKB_DMA_REALIGN)
239 - skb_reserve(skb, SKB_DMA_REALIGN);
240 - skb_reserve(skb, NET_IP_ALIGN);
241 - desc->sdl = CNS3XXX_MAX_MTU;
242 +
243 + desc->sdl = RX_SEGMENT_MRU;
244 if (i == (RX_DESCS - 1))
245 desc->eor = 1;
246 desc->fsd = 1;
247 desc->lsd = 1;
248
249 - desc->sdp = dma_map_single(NULL, skb->data,
250 - CNS3XXX_MAX_MTU, DMA_FROM_DEVICE);
251 - if (dma_mapping_error(NULL, desc->sdp)) {
252 + desc->sdp = dma_map_single(NULL, buf + SKB_HEAD_ALIGN,
253 + RX_SEGMENT_MRU, DMA_FROM_DEVICE);
254 + if (dma_mapping_error(NULL, desc->sdp))
255 return -EIO;
256 - }
257 - rx_ring->buff_tab[i] = skb;
258 +
259 + rx_ring->buff_tab[i] = buf;
260 rx_ring->phys_tab[i] = desc->sdp;
261 desc->cown = 0;
262 }
263 @@ -905,12 +930,13 @@ static void destroy_rings(struct sw *sw)
264 struct _rx_ring *rx_ring = sw->rx_ring;
265 struct rx_desc *desc = &(rx_ring)->desc[i];
266 struct sk_buff *skb = sw->rx_ring->buff_tab[i];
267 - if (skb) {
268 - dma_unmap_single(NULL,
269 - desc->sdp,
270 - CNS3XXX_MAX_MTU, DMA_FROM_DEVICE);
271 - dev_kfree_skb(skb);
272 - }
273 +
274 + if (!skb)
275 + continue;
276 +
277 + dma_unmap_single(NULL, desc->sdp, RX_SEGMENT_MRU,
278 + DMA_FROM_DEVICE);
279 + dev_kfree_skb(skb);
280 }
281 dma_pool_free(rx_dma_pool, sw->rx_ring->desc, sw->rx_ring->phys_addr);
282 dma_pool_destroy(rx_dma_pool);
283 @@ -1085,13 +1111,22 @@ static int eth_set_mac(struct net_device
284 return 0;
285 }
286
287 +static int cns3xxx_change_mtu(struct net_device *dev, int new_mtu)
288 +{
289 + if (new_mtu > MAX_MTU)
290 + return -EINVAL;
291 +
292 + dev->mtu = new_mtu;
293 + return 0;
294 +}
295 +
296 static const struct net_device_ops cns3xxx_netdev_ops = {
297 .ndo_open = eth_open,
298 .ndo_stop = eth_close,
299 .ndo_start_xmit = eth_xmit,
300 .ndo_set_rx_mode = eth_rx_mode,
301 .ndo_do_ioctl = eth_ioctl,
302 - .ndo_change_mtu = eth_change_mtu,
303 + .ndo_change_mtu = cns3xxx_change_mtu,
304 .ndo_set_mac_address = eth_set_mac,
305 .ndo_validate_addr = eth_validate_addr,
306 };
307 @@ -1124,6 +1159,10 @@ static int __devinit eth_init_one(struct
308 goto err_free;
309 }
310
311 + temp = __raw_readl(&sw->regs->phy_auto_addr);
312 + temp |= (3 << 30); /* maximum frame length: 9600 bytes */
313 + __raw_writel(temp, &sw->regs->phy_auto_addr);
314 +
315 for (i = 0; i < 4; i++) {
316 temp = __raw_readl(&sw->regs->mac_cfg[i]);
317 temp |= (PORT_DISABLE);