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