]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/sfc/rx.c
Clocklib: Fix SA1111 clock name mess.
[mirror_ubuntu-artful-kernel.git] / drivers / net / sfc / rx.c
1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2008 Solarflare Communications Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11 #include <linux/socket.h>
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <net/ip.h>
17 #include <net/checksum.h>
18 #include "net_driver.h"
19 #include "rx.h"
20 #include "efx.h"
21 #include "falcon.h"
22 #include "selftest.h"
23 #include "workarounds.h"
24
25 /* Number of RX descriptors pushed at once. */
26 #define EFX_RX_BATCH 8
27
28 /* Size of buffer allocated for skb header area. */
29 #define EFX_SKB_HEADERS 64u
30
31 /*
32 * rx_alloc_method - RX buffer allocation method
33 *
34 * This driver supports two methods for allocating and using RX buffers:
35 * each RX buffer may be backed by an skb or by an order-n page.
36 *
37 * When LRO is in use then the second method has a lower overhead,
38 * since we don't have to allocate then free skbs on reassembled frames.
39 *
40 * Values:
41 * - RX_ALLOC_METHOD_AUTO = 0
42 * - RX_ALLOC_METHOD_SKB = 1
43 * - RX_ALLOC_METHOD_PAGE = 2
44 *
45 * The heuristic for %RX_ALLOC_METHOD_AUTO is a simple hysteresis count
46 * controlled by the parameters below.
47 *
48 * - Since pushing and popping descriptors are separated by the rx_queue
49 * size, so the watermarks should be ~rxd_size.
50 * - The performance win by using page-based allocation for LRO is less
51 * than the performance hit of using page-based allocation of non-LRO,
52 * so the watermarks should reflect this.
53 *
54 * Per channel we maintain a single variable, updated by each channel:
55 *
56 * rx_alloc_level += (lro_performed ? RX_ALLOC_FACTOR_LRO :
57 * RX_ALLOC_FACTOR_SKB)
58 * Per NAPI poll interval, we constrain rx_alloc_level to 0..MAX (which
59 * limits the hysteresis), and update the allocation strategy:
60 *
61 * rx_alloc_method = (rx_alloc_level > RX_ALLOC_LEVEL_LRO ?
62 * RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB)
63 */
64 static int rx_alloc_method = RX_ALLOC_METHOD_PAGE;
65
66 #define RX_ALLOC_LEVEL_LRO 0x2000
67 #define RX_ALLOC_LEVEL_MAX 0x3000
68 #define RX_ALLOC_FACTOR_LRO 1
69 #define RX_ALLOC_FACTOR_SKB (-2)
70
71 /* This is the percentage fill level below which new RX descriptors
72 * will be added to the RX descriptor ring.
73 */
74 static unsigned int rx_refill_threshold = 90;
75
76 /* This is the percentage fill level to which an RX queue will be refilled
77 * when the "RX refill threshold" is reached.
78 */
79 static unsigned int rx_refill_limit = 95;
80
81 /*
82 * RX maximum head room required.
83 *
84 * This must be at least 1 to prevent overflow and at least 2 to allow
85 * pipelined receives.
86 */
87 #define EFX_RXD_HEAD_ROOM 2
88
89 /* Macros for zero-order pages (potentially) containing multiple RX buffers */
90 #define RX_DATA_OFFSET(_data) \
91 (((unsigned long) (_data)) & (PAGE_SIZE-1))
92 #define RX_BUF_OFFSET(_rx_buf) \
93 RX_DATA_OFFSET((_rx_buf)->data)
94
95 #define RX_PAGE_SIZE(_efx) \
96 (PAGE_SIZE * (1u << (_efx)->rx_buffer_order))
97
98
99 /**************************************************************************
100 *
101 * Linux generic LRO handling
102 *
103 **************************************************************************
104 */
105
106 static int efx_lro_get_skb_hdr(struct sk_buff *skb, void **ip_hdr,
107 void **tcpudp_hdr, u64 *hdr_flags, void *priv)
108 {
109 struct efx_channel *channel = (struct efx_channel *)priv;
110 struct iphdr *iph;
111 struct tcphdr *th;
112
113 iph = (struct iphdr *)skb->data;
114 if (skb->protocol != htons(ETH_P_IP) || iph->protocol != IPPROTO_TCP)
115 goto fail;
116
117 th = (struct tcphdr *)(skb->data + iph->ihl * 4);
118
119 *tcpudp_hdr = th;
120 *ip_hdr = iph;
121 *hdr_flags = LRO_IPV4 | LRO_TCP;
122
123 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
124 return 0;
125 fail:
126 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
127 return -1;
128 }
129
130 static int efx_get_frag_hdr(struct skb_frag_struct *frag, void **mac_hdr,
131 void **ip_hdr, void **tcpudp_hdr, u64 *hdr_flags,
132 void *priv)
133 {
134 struct efx_channel *channel = (struct efx_channel *)priv;
135 struct ethhdr *eh;
136 struct iphdr *iph;
137
138 /* We support EtherII and VLAN encapsulated IPv4 */
139 eh = (struct ethhdr *)(page_address(frag->page) + frag->page_offset);
140 *mac_hdr = eh;
141
142 if (eh->h_proto == htons(ETH_P_IP)) {
143 iph = (struct iphdr *)(eh + 1);
144 } else {
145 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)eh;
146 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
147 goto fail;
148
149 iph = (struct iphdr *)(veh + 1);
150 }
151 *ip_hdr = iph;
152
153 /* We can only do LRO over TCP */
154 if (iph->protocol != IPPROTO_TCP)
155 goto fail;
156
157 *hdr_flags = LRO_IPV4 | LRO_TCP;
158 *tcpudp_hdr = (struct tcphdr *)((u8 *) iph + iph->ihl * 4);
159
160 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
161 return 0;
162 fail:
163 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
164 return -1;
165 }
166
167 int efx_lro_init(struct net_lro_mgr *lro_mgr, struct efx_nic *efx)
168 {
169 size_t s = sizeof(struct net_lro_desc) * EFX_MAX_LRO_DESCRIPTORS;
170 struct net_lro_desc *lro_arr;
171
172 /* Allocate the LRO descriptors structure */
173 lro_arr = kzalloc(s, GFP_KERNEL);
174 if (lro_arr == NULL)
175 return -ENOMEM;
176
177 lro_mgr->lro_arr = lro_arr;
178 lro_mgr->max_desc = EFX_MAX_LRO_DESCRIPTORS;
179 lro_mgr->max_aggr = EFX_MAX_LRO_AGGR;
180 lro_mgr->frag_align_pad = EFX_PAGE_SKB_ALIGN;
181
182 lro_mgr->get_skb_header = efx_lro_get_skb_hdr;
183 lro_mgr->get_frag_header = efx_get_frag_hdr;
184 lro_mgr->dev = efx->net_dev;
185
186 lro_mgr->features = LRO_F_NAPI;
187
188 /* We can pass packets up with the checksum intact */
189 lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
190
191 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
192
193 return 0;
194 }
195
196 void efx_lro_fini(struct net_lro_mgr *lro_mgr)
197 {
198 kfree(lro_mgr->lro_arr);
199 lro_mgr->lro_arr = NULL;
200 }
201
202 /**
203 * efx_init_rx_buffer_skb - create new RX buffer using skb-based allocation
204 *
205 * @rx_queue: Efx RX queue
206 * @rx_buf: RX buffer structure to populate
207 *
208 * This allocates memory for a new receive buffer, maps it for DMA,
209 * and populates a struct efx_rx_buffer with the relevant
210 * information. Return a negative error code or 0 on success.
211 */
212 static inline int efx_init_rx_buffer_skb(struct efx_rx_queue *rx_queue,
213 struct efx_rx_buffer *rx_buf)
214 {
215 struct efx_nic *efx = rx_queue->efx;
216 struct net_device *net_dev = efx->net_dev;
217 int skb_len = efx->rx_buffer_len;
218
219 rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
220 if (unlikely(!rx_buf->skb))
221 return -ENOMEM;
222
223 /* Adjust the SKB for padding and checksum */
224 skb_reserve(rx_buf->skb, NET_IP_ALIGN);
225 rx_buf->len = skb_len - NET_IP_ALIGN;
226 rx_buf->data = (char *)rx_buf->skb->data;
227 rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
228
229 rx_buf->dma_addr = pci_map_single(efx->pci_dev,
230 rx_buf->data, rx_buf->len,
231 PCI_DMA_FROMDEVICE);
232
233 if (unlikely(pci_dma_mapping_error(rx_buf->dma_addr))) {
234 dev_kfree_skb_any(rx_buf->skb);
235 rx_buf->skb = NULL;
236 return -EIO;
237 }
238
239 return 0;
240 }
241
242 /**
243 * efx_init_rx_buffer_page - create new RX buffer using page-based allocation
244 *
245 * @rx_queue: Efx RX queue
246 * @rx_buf: RX buffer structure to populate
247 *
248 * This allocates memory for a new receive buffer, maps it for DMA,
249 * and populates a struct efx_rx_buffer with the relevant
250 * information. Return a negative error code or 0 on success.
251 */
252 static inline int efx_init_rx_buffer_page(struct efx_rx_queue *rx_queue,
253 struct efx_rx_buffer *rx_buf)
254 {
255 struct efx_nic *efx = rx_queue->efx;
256 int bytes, space, offset;
257
258 bytes = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
259
260 /* If there is space left in the previously allocated page,
261 * then use it. Otherwise allocate a new one */
262 rx_buf->page = rx_queue->buf_page;
263 if (rx_buf->page == NULL) {
264 dma_addr_t dma_addr;
265
266 rx_buf->page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
267 efx->rx_buffer_order);
268 if (unlikely(rx_buf->page == NULL))
269 return -ENOMEM;
270
271 dma_addr = pci_map_page(efx->pci_dev, rx_buf->page,
272 0, RX_PAGE_SIZE(efx),
273 PCI_DMA_FROMDEVICE);
274
275 if (unlikely(pci_dma_mapping_error(dma_addr))) {
276 __free_pages(rx_buf->page, efx->rx_buffer_order);
277 rx_buf->page = NULL;
278 return -EIO;
279 }
280
281 rx_queue->buf_page = rx_buf->page;
282 rx_queue->buf_dma_addr = dma_addr;
283 rx_queue->buf_data = ((char *) page_address(rx_buf->page) +
284 EFX_PAGE_IP_ALIGN);
285 }
286
287 offset = RX_DATA_OFFSET(rx_queue->buf_data);
288 rx_buf->len = bytes;
289 rx_buf->dma_addr = rx_queue->buf_dma_addr + offset;
290 rx_buf->data = rx_queue->buf_data;
291
292 /* Try to pack multiple buffers per page */
293 if (efx->rx_buffer_order == 0) {
294 /* The next buffer starts on the next 512 byte boundary */
295 rx_queue->buf_data += ((bytes + 0x1ff) & ~0x1ff);
296 offset += ((bytes + 0x1ff) & ~0x1ff);
297
298 space = RX_PAGE_SIZE(efx) - offset;
299 if (space >= bytes) {
300 /* Refs dropped on kernel releasing each skb */
301 get_page(rx_queue->buf_page);
302 goto out;
303 }
304 }
305
306 /* This is the final RX buffer for this page, so mark it for
307 * unmapping */
308 rx_queue->buf_page = NULL;
309 rx_buf->unmap_addr = rx_queue->buf_dma_addr;
310
311 out:
312 return 0;
313 }
314
315 /* This allocates memory for a new receive buffer, maps it for DMA,
316 * and populates a struct efx_rx_buffer with the relevant
317 * information.
318 */
319 static inline int efx_init_rx_buffer(struct efx_rx_queue *rx_queue,
320 struct efx_rx_buffer *new_rx_buf)
321 {
322 int rc = 0;
323
324 if (rx_queue->channel->rx_alloc_push_pages) {
325 new_rx_buf->skb = NULL;
326 rc = efx_init_rx_buffer_page(rx_queue, new_rx_buf);
327 rx_queue->alloc_page_count++;
328 } else {
329 new_rx_buf->page = NULL;
330 rc = efx_init_rx_buffer_skb(rx_queue, new_rx_buf);
331 rx_queue->alloc_skb_count++;
332 }
333
334 if (unlikely(rc < 0))
335 EFX_LOG_RL(rx_queue->efx, "%s RXQ[%d] =%d\n", __func__,
336 rx_queue->queue, rc);
337 return rc;
338 }
339
340 static inline void efx_unmap_rx_buffer(struct efx_nic *efx,
341 struct efx_rx_buffer *rx_buf)
342 {
343 if (rx_buf->page) {
344 EFX_BUG_ON_PARANOID(rx_buf->skb);
345 if (rx_buf->unmap_addr) {
346 pci_unmap_page(efx->pci_dev, rx_buf->unmap_addr,
347 RX_PAGE_SIZE(efx), PCI_DMA_FROMDEVICE);
348 rx_buf->unmap_addr = 0;
349 }
350 } else if (likely(rx_buf->skb)) {
351 pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
352 rx_buf->len, PCI_DMA_FROMDEVICE);
353 }
354 }
355
356 static inline void efx_free_rx_buffer(struct efx_nic *efx,
357 struct efx_rx_buffer *rx_buf)
358 {
359 if (rx_buf->page) {
360 __free_pages(rx_buf->page, efx->rx_buffer_order);
361 rx_buf->page = NULL;
362 } else if (likely(rx_buf->skb)) {
363 dev_kfree_skb_any(rx_buf->skb);
364 rx_buf->skb = NULL;
365 }
366 }
367
368 static inline void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
369 struct efx_rx_buffer *rx_buf)
370 {
371 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
372 efx_free_rx_buffer(rx_queue->efx, rx_buf);
373 }
374
375 /**
376 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
377 * @rx_queue: RX descriptor queue
378 * @retry: Recheck the fill level
379 * This will aim to fill the RX descriptor queue up to
380 * @rx_queue->@fast_fill_limit. If there is insufficient atomic
381 * memory to do so, the caller should retry.
382 */
383 static int __efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
384 int retry)
385 {
386 struct efx_rx_buffer *rx_buf;
387 unsigned fill_level, index;
388 int i, space, rc = 0;
389
390 /* Calculate current fill level. Do this outside the lock,
391 * because most of the time we'll end up not wanting to do the
392 * fill anyway.
393 */
394 fill_level = (rx_queue->added_count - rx_queue->removed_count);
395 EFX_BUG_ON_PARANOID(fill_level >
396 rx_queue->efx->type->rxd_ring_mask + 1);
397
398 /* Don't fill if we don't need to */
399 if (fill_level >= rx_queue->fast_fill_trigger)
400 return 0;
401
402 /* Record minimum fill level */
403 if (unlikely(fill_level < rx_queue->min_fill))
404 if (fill_level)
405 rx_queue->min_fill = fill_level;
406
407 /* Acquire RX add lock. If this lock is contended, then a fast
408 * fill must already be in progress (e.g. in the refill
409 * tasklet), so we don't need to do anything
410 */
411 if (!spin_trylock_bh(&rx_queue->add_lock))
412 return -1;
413
414 retry:
415 /* Recalculate current fill level now that we have the lock */
416 fill_level = (rx_queue->added_count - rx_queue->removed_count);
417 EFX_BUG_ON_PARANOID(fill_level >
418 rx_queue->efx->type->rxd_ring_mask + 1);
419 space = rx_queue->fast_fill_limit - fill_level;
420 if (space < EFX_RX_BATCH)
421 goto out_unlock;
422
423 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
424 " level %d to level %d using %s allocation\n",
425 rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
426 rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
427
428 do {
429 for (i = 0; i < EFX_RX_BATCH; ++i) {
430 index = (rx_queue->added_count &
431 rx_queue->efx->type->rxd_ring_mask);
432 rx_buf = efx_rx_buffer(rx_queue, index);
433 rc = efx_init_rx_buffer(rx_queue, rx_buf);
434 if (unlikely(rc))
435 goto out;
436 ++rx_queue->added_count;
437 }
438 } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
439
440 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
441 "to level %d\n", rx_queue->queue,
442 rx_queue->added_count - rx_queue->removed_count);
443
444 out:
445 /* Send write pointer to card. */
446 falcon_notify_rx_desc(rx_queue);
447
448 /* If the fast fill is running inside from the refill tasklet, then
449 * for SMP systems it may be running on a different CPU to
450 * RX event processing, which means that the fill level may now be
451 * out of date. */
452 if (unlikely(retry && (rc == 0)))
453 goto retry;
454
455 out_unlock:
456 spin_unlock_bh(&rx_queue->add_lock);
457
458 return rc;
459 }
460
461 /**
462 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
463 * @rx_queue: RX descriptor queue
464 *
465 * This will aim to fill the RX descriptor queue up to
466 * @rx_queue->@fast_fill_limit. If there is insufficient memory to do so,
467 * it will schedule a work item to immediately continue the fast fill
468 */
469 void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
470 {
471 int rc;
472
473 rc = __efx_fast_push_rx_descriptors(rx_queue, 0);
474 if (unlikely(rc)) {
475 /* Schedule the work item to run immediately. The hope is
476 * that work is immediately pending to free some memory
477 * (e.g. an RX event or TX completion)
478 */
479 efx_schedule_slow_fill(rx_queue, 0);
480 }
481 }
482
483 void efx_rx_work(struct work_struct *data)
484 {
485 struct efx_rx_queue *rx_queue;
486 int rc;
487
488 rx_queue = container_of(data, struct efx_rx_queue, work.work);
489
490 if (unlikely(!rx_queue->channel->enabled))
491 return;
492
493 EFX_TRACE(rx_queue->efx, "RX queue %d worker thread executing on CPU "
494 "%d\n", rx_queue->queue, raw_smp_processor_id());
495
496 ++rx_queue->slow_fill_count;
497 /* Push new RX descriptors, allowing at least 1 jiffy for
498 * the kernel to free some more memory. */
499 rc = __efx_fast_push_rx_descriptors(rx_queue, 1);
500 if (rc)
501 efx_schedule_slow_fill(rx_queue, 1);
502 }
503
504 static inline void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
505 struct efx_rx_buffer *rx_buf,
506 int len, int *discard,
507 int *leak_packet)
508 {
509 struct efx_nic *efx = rx_queue->efx;
510 unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
511
512 if (likely(len <= max_len))
513 return;
514
515 /* The packet must be discarded, but this is only a fatal error
516 * if the caller indicated it was
517 */
518 *discard = 1;
519
520 if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
521 EFX_ERR_RL(efx, " RX queue %d seriously overlength "
522 "RX event (0x%x > 0x%x+0x%x). Leaking\n",
523 rx_queue->queue, len, max_len,
524 efx->type->rx_buffer_padding);
525 /* If this buffer was skb-allocated, then the meta
526 * data at the end of the skb will be trashed. So
527 * we have no choice but to leak the fragment.
528 */
529 *leak_packet = (rx_buf->skb != NULL);
530 efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
531 } else {
532 EFX_ERR_RL(efx, " RX queue %d overlength RX event "
533 "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
534 }
535
536 rx_queue->channel->n_rx_overlength++;
537 }
538
539 /* Pass a received packet up through the generic LRO stack
540 *
541 * Handles driverlink veto, and passes the fragment up via
542 * the appropriate LRO method
543 */
544 static inline void efx_rx_packet_lro(struct efx_channel *channel,
545 struct efx_rx_buffer *rx_buf)
546 {
547 struct net_lro_mgr *lro_mgr = &channel->lro_mgr;
548 void *priv = channel;
549
550 /* Pass the skb/page into the LRO engine */
551 if (rx_buf->page) {
552 struct skb_frag_struct frags;
553
554 frags.page = rx_buf->page;
555 frags.page_offset = RX_BUF_OFFSET(rx_buf);
556 frags.size = rx_buf->len;
557
558 lro_receive_frags(lro_mgr, &frags, rx_buf->len,
559 rx_buf->len, priv, 0);
560
561 EFX_BUG_ON_PARANOID(rx_buf->skb);
562 rx_buf->page = NULL;
563 } else {
564 EFX_BUG_ON_PARANOID(!rx_buf->skb);
565
566 lro_receive_skb(lro_mgr, rx_buf->skb, priv);
567 rx_buf->skb = NULL;
568 }
569 }
570
571 /* Allocate and construct an SKB around a struct page.*/
572 static inline struct sk_buff *efx_rx_mk_skb(struct efx_rx_buffer *rx_buf,
573 struct efx_nic *efx,
574 int hdr_len)
575 {
576 struct sk_buff *skb;
577
578 /* Allocate an SKB to store the headers */
579 skb = netdev_alloc_skb(efx->net_dev, hdr_len + EFX_PAGE_SKB_ALIGN);
580 if (unlikely(skb == NULL)) {
581 EFX_ERR_RL(efx, "RX out of memory for skb\n");
582 return NULL;
583 }
584
585 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags);
586 EFX_BUG_ON_PARANOID(rx_buf->len < hdr_len);
587
588 skb->ip_summed = CHECKSUM_UNNECESSARY;
589 skb_reserve(skb, EFX_PAGE_SKB_ALIGN);
590
591 skb->len = rx_buf->len;
592 skb->truesize = rx_buf->len + sizeof(struct sk_buff);
593 memcpy(skb->data, rx_buf->data, hdr_len);
594 skb->tail += hdr_len;
595
596 /* Append the remaining page onto the frag list */
597 if (unlikely(rx_buf->len > hdr_len)) {
598 struct skb_frag_struct *frag = skb_shinfo(skb)->frags;
599 frag->page = rx_buf->page;
600 frag->page_offset = RX_BUF_OFFSET(rx_buf) + hdr_len;
601 frag->size = skb->len - hdr_len;
602 skb_shinfo(skb)->nr_frags = 1;
603 skb->data_len = frag->size;
604 } else {
605 __free_pages(rx_buf->page, efx->rx_buffer_order);
606 skb->data_len = 0;
607 }
608
609 /* Ownership has transferred from the rx_buf to skb */
610 rx_buf->page = NULL;
611
612 /* Move past the ethernet header */
613 skb->protocol = eth_type_trans(skb, efx->net_dev);
614
615 return skb;
616 }
617
618 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
619 unsigned int len, int checksummed, int discard)
620 {
621 struct efx_nic *efx = rx_queue->efx;
622 struct efx_rx_buffer *rx_buf;
623 int leak_packet = 0;
624
625 rx_buf = efx_rx_buffer(rx_queue, index);
626 EFX_BUG_ON_PARANOID(!rx_buf->data);
627 EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
628 EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
629
630 /* This allows the refill path to post another buffer.
631 * EFX_RXD_HEAD_ROOM ensures that the slot we are using
632 * isn't overwritten yet.
633 */
634 rx_queue->removed_count++;
635
636 /* Validate the length encoded in the event vs the descriptor pushed */
637 efx_rx_packet__check_len(rx_queue, rx_buf, len,
638 &discard, &leak_packet);
639
640 EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
641 rx_queue->queue, index,
642 (unsigned long long)rx_buf->dma_addr, len,
643 (checksummed ? " [SUMMED]" : ""),
644 (discard ? " [DISCARD]" : ""));
645
646 /* Discard packet, if instructed to do so */
647 if (unlikely(discard)) {
648 if (unlikely(leak_packet))
649 rx_queue->channel->n_skbuff_leaks++;
650 else
651 /* We haven't called efx_unmap_rx_buffer yet,
652 * so fini the entire rx_buffer here */
653 efx_fini_rx_buffer(rx_queue, rx_buf);
654 return;
655 }
656
657 /* Release card resources - assumes all RX buffers consumed in-order
658 * per RX queue
659 */
660 efx_unmap_rx_buffer(efx, rx_buf);
661
662 /* Prefetch nice and early so data will (hopefully) be in cache by
663 * the time we look at it.
664 */
665 prefetch(rx_buf->data);
666
667 /* Pipeline receives so that we give time for packet headers to be
668 * prefetched into cache.
669 */
670 rx_buf->len = len;
671 if (rx_queue->channel->rx_pkt)
672 __efx_rx_packet(rx_queue->channel,
673 rx_queue->channel->rx_pkt,
674 rx_queue->channel->rx_pkt_csummed);
675 rx_queue->channel->rx_pkt = rx_buf;
676 rx_queue->channel->rx_pkt_csummed = checksummed;
677 }
678
679 /* Handle a received packet. Second half: Touches packet payload. */
680 void __efx_rx_packet(struct efx_channel *channel,
681 struct efx_rx_buffer *rx_buf, int checksummed)
682 {
683 struct efx_nic *efx = channel->efx;
684 struct sk_buff *skb;
685 int lro = efx->net_dev->features & NETIF_F_LRO;
686
687 /* If we're in loopback test, then pass the packet directly to the
688 * loopback layer, and free the rx_buf here
689 */
690 if (unlikely(efx->loopback_selftest)) {
691 efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
692 efx_free_rx_buffer(efx, rx_buf);
693 goto done;
694 }
695
696 if (rx_buf->skb) {
697 prefetch(skb_shinfo(rx_buf->skb));
698
699 skb_put(rx_buf->skb, rx_buf->len);
700
701 /* Move past the ethernet header. rx_buf->data still points
702 * at the ethernet header */
703 rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
704 efx->net_dev);
705 }
706
707 /* Both our generic-LRO and SFC-SSR support skb and page based
708 * allocation, but neither support switching from one to the
709 * other on the fly. If we spot that the allocation mode has
710 * changed, then flush the LRO state.
711 */
712 if (unlikely(channel->rx_alloc_pop_pages != (rx_buf->page != NULL))) {
713 efx_flush_lro(channel);
714 channel->rx_alloc_pop_pages = (rx_buf->page != NULL);
715 }
716 if (likely(checksummed && lro)) {
717 efx_rx_packet_lro(channel, rx_buf);
718 goto done;
719 }
720
721 /* Form an skb if required */
722 if (rx_buf->page) {
723 int hdr_len = min(rx_buf->len, EFX_SKB_HEADERS);
724 skb = efx_rx_mk_skb(rx_buf, efx, hdr_len);
725 if (unlikely(skb == NULL)) {
726 efx_free_rx_buffer(efx, rx_buf);
727 goto done;
728 }
729 } else {
730 /* We now own the SKB */
731 skb = rx_buf->skb;
732 rx_buf->skb = NULL;
733 }
734
735 EFX_BUG_ON_PARANOID(rx_buf->page);
736 EFX_BUG_ON_PARANOID(rx_buf->skb);
737 EFX_BUG_ON_PARANOID(!skb);
738
739 /* Set the SKB flags */
740 if (unlikely(!checksummed || !efx->rx_checksum_enabled))
741 skb->ip_summed = CHECKSUM_NONE;
742
743 /* Pass the packet up */
744 netif_receive_skb(skb);
745
746 /* Update allocation strategy method */
747 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
748
749 done:
750 efx->net_dev->last_rx = jiffies;
751 }
752
753 void efx_rx_strategy(struct efx_channel *channel)
754 {
755 enum efx_rx_alloc_method method = rx_alloc_method;
756
757 /* Only makes sense to use page based allocation if LRO is enabled */
758 if (!(channel->efx->net_dev->features & NETIF_F_LRO)) {
759 method = RX_ALLOC_METHOD_SKB;
760 } else if (method == RX_ALLOC_METHOD_AUTO) {
761 /* Constrain the rx_alloc_level */
762 if (channel->rx_alloc_level < 0)
763 channel->rx_alloc_level = 0;
764 else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
765 channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
766
767 /* Decide on the allocation method */
768 method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
769 RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
770 }
771
772 /* Push the option */
773 channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
774 }
775
776 int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
777 {
778 struct efx_nic *efx = rx_queue->efx;
779 unsigned int rxq_size;
780 int rc;
781
782 EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
783
784 /* Allocate RX buffers */
785 rxq_size = (efx->type->rxd_ring_mask + 1) * sizeof(*rx_queue->buffer);
786 rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
787 if (!rx_queue->buffer) {
788 rc = -ENOMEM;
789 goto fail1;
790 }
791
792 rc = falcon_probe_rx(rx_queue);
793 if (rc)
794 goto fail2;
795
796 return 0;
797
798 fail2:
799 kfree(rx_queue->buffer);
800 rx_queue->buffer = NULL;
801 fail1:
802 rx_queue->used = 0;
803
804 return rc;
805 }
806
807 int efx_init_rx_queue(struct efx_rx_queue *rx_queue)
808 {
809 struct efx_nic *efx = rx_queue->efx;
810 unsigned int max_fill, trigger, limit;
811
812 EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
813
814 /* Initialise ptr fields */
815 rx_queue->added_count = 0;
816 rx_queue->notified_count = 0;
817 rx_queue->removed_count = 0;
818 rx_queue->min_fill = -1U;
819 rx_queue->min_overfill = -1U;
820
821 /* Initialise limit fields */
822 max_fill = efx->type->rxd_ring_mask + 1 - EFX_RXD_HEAD_ROOM;
823 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
824 limit = max_fill * min(rx_refill_limit, 100U) / 100U;
825
826 rx_queue->max_fill = max_fill;
827 rx_queue->fast_fill_trigger = trigger;
828 rx_queue->fast_fill_limit = limit;
829
830 /* Set up RX descriptor ring */
831 return falcon_init_rx(rx_queue);
832 }
833
834 void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
835 {
836 int i;
837 struct efx_rx_buffer *rx_buf;
838
839 EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
840
841 falcon_fini_rx(rx_queue);
842
843 /* Release RX buffers NB start at index 0 not current HW ptr */
844 if (rx_queue->buffer) {
845 for (i = 0; i <= rx_queue->efx->type->rxd_ring_mask; i++) {
846 rx_buf = efx_rx_buffer(rx_queue, i);
847 efx_fini_rx_buffer(rx_queue, rx_buf);
848 }
849 }
850
851 /* For a page that is part-way through splitting into RX buffers */
852 if (rx_queue->buf_page != NULL) {
853 pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
854 RX_PAGE_SIZE(rx_queue->efx), PCI_DMA_FROMDEVICE);
855 __free_pages(rx_queue->buf_page,
856 rx_queue->efx->rx_buffer_order);
857 rx_queue->buf_page = NULL;
858 }
859 }
860
861 void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
862 {
863 EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
864
865 falcon_remove_rx(rx_queue);
866
867 kfree(rx_queue->buffer);
868 rx_queue->buffer = NULL;
869 rx_queue->used = 0;
870 }
871
872 void efx_flush_lro(struct efx_channel *channel)
873 {
874 lro_flush_all(&channel->lro_mgr);
875 }
876
877
878 module_param(rx_alloc_method, int, 0644);
879 MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
880
881 module_param(rx_refill_threshold, uint, 0444);
882 MODULE_PARM_DESC(rx_refill_threshold,
883 "RX descriptor ring fast/slow fill threshold (%)");
884