net: OpenFirmware GPIO based MDIO bitbang driver
[zen-stable.git] / drivers / net / sfc / rx.c
blob601b001437c01b8bf13966448641e197e4eb749d
1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2008 Solarflare Communications Inc.
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 */
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"
25 /* Number of RX descriptors pushed at once. */
26 #define EFX_RX_BATCH 8
28 /* Size of buffer allocated for skb header area. */
29 #define EFX_SKB_HEADERS 64u
32 * rx_alloc_method - RX buffer allocation method
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.
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.
40 * Values:
41 * - RX_ALLOC_METHOD_AUTO = 0
42 * - RX_ALLOC_METHOD_SKB = 1
43 * - RX_ALLOC_METHOD_PAGE = 2
45 * The heuristic for %RX_ALLOC_METHOD_AUTO is a simple hysteresis count
46 * controlled by the parameters below.
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.
54 * Per channel we maintain a single variable, updated by each channel:
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:
61 * rx_alloc_method = (rx_alloc_level > RX_ALLOC_LEVEL_LRO ?
62 * RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB)
64 static int rx_alloc_method = RX_ALLOC_METHOD_PAGE;
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)
71 /* This is the percentage fill level below which new RX descriptors
72 * will be added to the RX descriptor ring.
74 static unsigned int rx_refill_threshold = 90;
76 /* This is the percentage fill level to which an RX queue will be refilled
77 * when the "RX refill threshold" is reached.
79 static unsigned int rx_refill_limit = 95;
82 * RX maximum head room required.
84 * This must be at least 1 to prevent overflow and at least 2 to allow
85 * pipelined receives.
87 #define EFX_RXD_HEAD_ROOM 2
89 static inline unsigned int efx_rx_buf_offset(struct efx_rx_buffer *buf)
91 /* Offset is always within one page, so we don't need to consider
92 * the page order.
94 return (__force unsigned long) buf->data & (PAGE_SIZE - 1);
96 static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
98 return PAGE_SIZE << efx->rx_buffer_order;
102 /**************************************************************************
104 * Linux generic LRO handling
106 **************************************************************************
109 static int efx_lro_get_skb_hdr(struct sk_buff *skb, void **ip_hdr,
110 void **tcpudp_hdr, u64 *hdr_flags, void *priv)
112 struct efx_channel *channel = priv;
113 struct iphdr *iph;
114 struct tcphdr *th;
116 iph = (struct iphdr *)skb->data;
117 if (skb->protocol != htons(ETH_P_IP) || iph->protocol != IPPROTO_TCP)
118 goto fail;
120 th = (struct tcphdr *)(skb->data + iph->ihl * 4);
122 *tcpudp_hdr = th;
123 *ip_hdr = iph;
124 *hdr_flags = LRO_IPV4 | LRO_TCP;
126 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
127 return 0;
128 fail:
129 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
130 return -1;
133 static int efx_get_frag_hdr(struct skb_frag_struct *frag, void **mac_hdr,
134 void **ip_hdr, void **tcpudp_hdr, u64 *hdr_flags,
135 void *priv)
137 struct efx_channel *channel = priv;
138 struct ethhdr *eh;
139 struct iphdr *iph;
141 /* We support EtherII and VLAN encapsulated IPv4 */
142 eh = page_address(frag->page) + frag->page_offset;
143 *mac_hdr = eh;
145 if (eh->h_proto == htons(ETH_P_IP)) {
146 iph = (struct iphdr *)(eh + 1);
147 } else {
148 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)eh;
149 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
150 goto fail;
152 iph = (struct iphdr *)(veh + 1);
154 *ip_hdr = iph;
156 /* We can only do LRO over TCP */
157 if (iph->protocol != IPPROTO_TCP)
158 goto fail;
160 *hdr_flags = LRO_IPV4 | LRO_TCP;
161 *tcpudp_hdr = (struct tcphdr *)((u8 *) iph + iph->ihl * 4);
163 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
164 return 0;
165 fail:
166 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
167 return -1;
170 int efx_lro_init(struct net_lro_mgr *lro_mgr, struct efx_nic *efx)
172 size_t s = sizeof(struct net_lro_desc) * EFX_MAX_LRO_DESCRIPTORS;
173 struct net_lro_desc *lro_arr;
175 /* Allocate the LRO descriptors structure */
176 lro_arr = kzalloc(s, GFP_KERNEL);
177 if (lro_arr == NULL)
178 return -ENOMEM;
180 lro_mgr->lro_arr = lro_arr;
181 lro_mgr->max_desc = EFX_MAX_LRO_DESCRIPTORS;
182 lro_mgr->max_aggr = EFX_MAX_LRO_AGGR;
183 lro_mgr->frag_align_pad = EFX_PAGE_SKB_ALIGN;
185 lro_mgr->get_skb_header = efx_lro_get_skb_hdr;
186 lro_mgr->get_frag_header = efx_get_frag_hdr;
187 lro_mgr->dev = efx->net_dev;
189 lro_mgr->features = LRO_F_NAPI;
191 /* We can pass packets up with the checksum intact */
192 lro_mgr->ip_summed = CHECKSUM_UNNECESSARY;
194 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
196 return 0;
199 void efx_lro_fini(struct net_lro_mgr *lro_mgr)
201 kfree(lro_mgr->lro_arr);
202 lro_mgr->lro_arr = NULL;
206 * efx_init_rx_buffer_skb - create new RX buffer using skb-based allocation
208 * @rx_queue: Efx RX queue
209 * @rx_buf: RX buffer structure to populate
211 * This allocates memory for a new receive buffer, maps it for DMA,
212 * and populates a struct efx_rx_buffer with the relevant
213 * information. Return a negative error code or 0 on success.
215 static inline int efx_init_rx_buffer_skb(struct efx_rx_queue *rx_queue,
216 struct efx_rx_buffer *rx_buf)
218 struct efx_nic *efx = rx_queue->efx;
219 struct net_device *net_dev = efx->net_dev;
220 int skb_len = efx->rx_buffer_len;
222 rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
223 if (unlikely(!rx_buf->skb))
224 return -ENOMEM;
226 /* Adjust the SKB for padding and checksum */
227 skb_reserve(rx_buf->skb, NET_IP_ALIGN);
228 rx_buf->len = skb_len - NET_IP_ALIGN;
229 rx_buf->data = (char *)rx_buf->skb->data;
230 rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
232 rx_buf->dma_addr = pci_map_single(efx->pci_dev,
233 rx_buf->data, rx_buf->len,
234 PCI_DMA_FROMDEVICE);
236 if (unlikely(pci_dma_mapping_error(rx_buf->dma_addr))) {
237 dev_kfree_skb_any(rx_buf->skb);
238 rx_buf->skb = NULL;
239 return -EIO;
242 return 0;
246 * efx_init_rx_buffer_page - create new RX buffer using page-based allocation
248 * @rx_queue: Efx RX queue
249 * @rx_buf: RX buffer structure to populate
251 * This allocates memory for a new receive buffer, maps it for DMA,
252 * and populates a struct efx_rx_buffer with the relevant
253 * information. Return a negative error code or 0 on success.
255 static inline int efx_init_rx_buffer_page(struct efx_rx_queue *rx_queue,
256 struct efx_rx_buffer *rx_buf)
258 struct efx_nic *efx = rx_queue->efx;
259 int bytes, space, offset;
261 bytes = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
263 /* If there is space left in the previously allocated page,
264 * then use it. Otherwise allocate a new one */
265 rx_buf->page = rx_queue->buf_page;
266 if (rx_buf->page == NULL) {
267 dma_addr_t dma_addr;
269 rx_buf->page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
270 efx->rx_buffer_order);
271 if (unlikely(rx_buf->page == NULL))
272 return -ENOMEM;
274 dma_addr = pci_map_page(efx->pci_dev, rx_buf->page,
275 0, efx_rx_buf_size(efx),
276 PCI_DMA_FROMDEVICE);
278 if (unlikely(pci_dma_mapping_error(dma_addr))) {
279 __free_pages(rx_buf->page, efx->rx_buffer_order);
280 rx_buf->page = NULL;
281 return -EIO;
284 rx_queue->buf_page = rx_buf->page;
285 rx_queue->buf_dma_addr = dma_addr;
286 rx_queue->buf_data = (page_address(rx_buf->page) +
287 EFX_PAGE_IP_ALIGN);
290 rx_buf->len = bytes;
291 rx_buf->data = rx_queue->buf_data;
292 offset = efx_rx_buf_offset(rx_buf);
293 rx_buf->dma_addr = rx_queue->buf_dma_addr + offset;
295 /* Try to pack multiple buffers per page */
296 if (efx->rx_buffer_order == 0) {
297 /* The next buffer starts on the next 512 byte boundary */
298 rx_queue->buf_data += ((bytes + 0x1ff) & ~0x1ff);
299 offset += ((bytes + 0x1ff) & ~0x1ff);
301 space = efx_rx_buf_size(efx) - offset;
302 if (space >= bytes) {
303 /* Refs dropped on kernel releasing each skb */
304 get_page(rx_queue->buf_page);
305 goto out;
309 /* This is the final RX buffer for this page, so mark it for
310 * unmapping */
311 rx_queue->buf_page = NULL;
312 rx_buf->unmap_addr = rx_queue->buf_dma_addr;
314 out:
315 return 0;
318 /* This allocates memory for a new receive buffer, maps it for DMA,
319 * and populates a struct efx_rx_buffer with the relevant
320 * information.
322 static inline int efx_init_rx_buffer(struct efx_rx_queue *rx_queue,
323 struct efx_rx_buffer *new_rx_buf)
325 int rc = 0;
327 if (rx_queue->channel->rx_alloc_push_pages) {
328 new_rx_buf->skb = NULL;
329 rc = efx_init_rx_buffer_page(rx_queue, new_rx_buf);
330 rx_queue->alloc_page_count++;
331 } else {
332 new_rx_buf->page = NULL;
333 rc = efx_init_rx_buffer_skb(rx_queue, new_rx_buf);
334 rx_queue->alloc_skb_count++;
337 if (unlikely(rc < 0))
338 EFX_LOG_RL(rx_queue->efx, "%s RXQ[%d] =%d\n", __func__,
339 rx_queue->queue, rc);
340 return rc;
343 static inline void efx_unmap_rx_buffer(struct efx_nic *efx,
344 struct efx_rx_buffer *rx_buf)
346 if (rx_buf->page) {
347 EFX_BUG_ON_PARANOID(rx_buf->skb);
348 if (rx_buf->unmap_addr) {
349 pci_unmap_page(efx->pci_dev, rx_buf->unmap_addr,
350 efx_rx_buf_size(efx),
351 PCI_DMA_FROMDEVICE);
352 rx_buf->unmap_addr = 0;
354 } else if (likely(rx_buf->skb)) {
355 pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
356 rx_buf->len, PCI_DMA_FROMDEVICE);
360 static inline void efx_free_rx_buffer(struct efx_nic *efx,
361 struct efx_rx_buffer *rx_buf)
363 if (rx_buf->page) {
364 __free_pages(rx_buf->page, efx->rx_buffer_order);
365 rx_buf->page = NULL;
366 } else if (likely(rx_buf->skb)) {
367 dev_kfree_skb_any(rx_buf->skb);
368 rx_buf->skb = NULL;
372 static inline void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
373 struct efx_rx_buffer *rx_buf)
375 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
376 efx_free_rx_buffer(rx_queue->efx, rx_buf);
380 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
381 * @rx_queue: RX descriptor queue
382 * @retry: Recheck the fill level
383 * This will aim to fill the RX descriptor queue up to
384 * @rx_queue->@fast_fill_limit. If there is insufficient atomic
385 * memory to do so, the caller should retry.
387 static int __efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
388 int retry)
390 struct efx_rx_buffer *rx_buf;
391 unsigned fill_level, index;
392 int i, space, rc = 0;
394 /* Calculate current fill level. Do this outside the lock,
395 * because most of the time we'll end up not wanting to do the
396 * fill anyway.
398 fill_level = (rx_queue->added_count - rx_queue->removed_count);
399 EFX_BUG_ON_PARANOID(fill_level >
400 rx_queue->efx->type->rxd_ring_mask + 1);
402 /* Don't fill if we don't need to */
403 if (fill_level >= rx_queue->fast_fill_trigger)
404 return 0;
406 /* Record minimum fill level */
407 if (unlikely(fill_level < rx_queue->min_fill)) {
408 if (fill_level)
409 rx_queue->min_fill = fill_level;
412 /* Acquire RX add lock. If this lock is contended, then a fast
413 * fill must already be in progress (e.g. in the refill
414 * tasklet), so we don't need to do anything
416 if (!spin_trylock_bh(&rx_queue->add_lock))
417 return -1;
419 retry:
420 /* Recalculate current fill level now that we have the lock */
421 fill_level = (rx_queue->added_count - rx_queue->removed_count);
422 EFX_BUG_ON_PARANOID(fill_level >
423 rx_queue->efx->type->rxd_ring_mask + 1);
424 space = rx_queue->fast_fill_limit - fill_level;
425 if (space < EFX_RX_BATCH)
426 goto out_unlock;
428 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
429 " level %d to level %d using %s allocation\n",
430 rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
431 rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
433 do {
434 for (i = 0; i < EFX_RX_BATCH; ++i) {
435 index = (rx_queue->added_count &
436 rx_queue->efx->type->rxd_ring_mask);
437 rx_buf = efx_rx_buffer(rx_queue, index);
438 rc = efx_init_rx_buffer(rx_queue, rx_buf);
439 if (unlikely(rc))
440 goto out;
441 ++rx_queue->added_count;
443 } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
445 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
446 "to level %d\n", rx_queue->queue,
447 rx_queue->added_count - rx_queue->removed_count);
449 out:
450 /* Send write pointer to card. */
451 falcon_notify_rx_desc(rx_queue);
453 /* If the fast fill is running inside from the refill tasklet, then
454 * for SMP systems it may be running on a different CPU to
455 * RX event processing, which means that the fill level may now be
456 * out of date. */
457 if (unlikely(retry && (rc == 0)))
458 goto retry;
460 out_unlock:
461 spin_unlock_bh(&rx_queue->add_lock);
463 return rc;
467 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
468 * @rx_queue: RX descriptor queue
470 * This will aim to fill the RX descriptor queue up to
471 * @rx_queue->@fast_fill_limit. If there is insufficient memory to do so,
472 * it will schedule a work item to immediately continue the fast fill
474 void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
476 int rc;
478 rc = __efx_fast_push_rx_descriptors(rx_queue, 0);
479 if (unlikely(rc)) {
480 /* Schedule the work item to run immediately. The hope is
481 * that work is immediately pending to free some memory
482 * (e.g. an RX event or TX completion)
484 efx_schedule_slow_fill(rx_queue, 0);
488 void efx_rx_work(struct work_struct *data)
490 struct efx_rx_queue *rx_queue;
491 int rc;
493 rx_queue = container_of(data, struct efx_rx_queue, work.work);
495 if (unlikely(!rx_queue->channel->enabled))
496 return;
498 EFX_TRACE(rx_queue->efx, "RX queue %d worker thread executing on CPU "
499 "%d\n", rx_queue->queue, raw_smp_processor_id());
501 ++rx_queue->slow_fill_count;
502 /* Push new RX descriptors, allowing at least 1 jiffy for
503 * the kernel to free some more memory. */
504 rc = __efx_fast_push_rx_descriptors(rx_queue, 1);
505 if (rc)
506 efx_schedule_slow_fill(rx_queue, 1);
509 static inline void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
510 struct efx_rx_buffer *rx_buf,
511 int len, int *discard,
512 int *leak_packet)
514 struct efx_nic *efx = rx_queue->efx;
515 unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
517 if (likely(len <= max_len))
518 return;
520 /* The packet must be discarded, but this is only a fatal error
521 * if the caller indicated it was
523 *discard = 1;
525 if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
526 EFX_ERR_RL(efx, " RX queue %d seriously overlength "
527 "RX event (0x%x > 0x%x+0x%x). Leaking\n",
528 rx_queue->queue, len, max_len,
529 efx->type->rx_buffer_padding);
530 /* If this buffer was skb-allocated, then the meta
531 * data at the end of the skb will be trashed. So
532 * we have no choice but to leak the fragment.
534 *leak_packet = (rx_buf->skb != NULL);
535 efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
536 } else {
537 EFX_ERR_RL(efx, " RX queue %d overlength RX event "
538 "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
541 rx_queue->channel->n_rx_overlength++;
544 /* Pass a received packet up through the generic LRO stack
546 * Handles driverlink veto, and passes the fragment up via
547 * the appropriate LRO method
549 static inline void efx_rx_packet_lro(struct efx_channel *channel,
550 struct efx_rx_buffer *rx_buf)
552 struct net_lro_mgr *lro_mgr = &channel->lro_mgr;
553 void *priv = channel;
555 /* Pass the skb/page into the LRO engine */
556 if (rx_buf->page) {
557 struct skb_frag_struct frags;
559 frags.page = rx_buf->page;
560 frags.page_offset = efx_rx_buf_offset(rx_buf);
561 frags.size = rx_buf->len;
563 lro_receive_frags(lro_mgr, &frags, rx_buf->len,
564 rx_buf->len, priv, 0);
566 EFX_BUG_ON_PARANOID(rx_buf->skb);
567 rx_buf->page = NULL;
568 } else {
569 EFX_BUG_ON_PARANOID(!rx_buf->skb);
571 lro_receive_skb(lro_mgr, rx_buf->skb, priv);
572 rx_buf->skb = NULL;
576 /* Allocate and construct an SKB around a struct page.*/
577 static inline struct sk_buff *efx_rx_mk_skb(struct efx_rx_buffer *rx_buf,
578 struct efx_nic *efx,
579 int hdr_len)
581 struct sk_buff *skb;
583 /* Allocate an SKB to store the headers */
584 skb = netdev_alloc_skb(efx->net_dev, hdr_len + EFX_PAGE_SKB_ALIGN);
585 if (unlikely(skb == NULL)) {
586 EFX_ERR_RL(efx, "RX out of memory for skb\n");
587 return NULL;
590 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags);
591 EFX_BUG_ON_PARANOID(rx_buf->len < hdr_len);
593 skb->ip_summed = CHECKSUM_UNNECESSARY;
594 skb_reserve(skb, EFX_PAGE_SKB_ALIGN);
596 skb->len = rx_buf->len;
597 skb->truesize = rx_buf->len + sizeof(struct sk_buff);
598 memcpy(skb->data, rx_buf->data, hdr_len);
599 skb->tail += hdr_len;
601 /* Append the remaining page onto the frag list */
602 if (unlikely(rx_buf->len > hdr_len)) {
603 struct skb_frag_struct *frag = skb_shinfo(skb)->frags;
604 frag->page = rx_buf->page;
605 frag->page_offset = efx_rx_buf_offset(rx_buf) + hdr_len;
606 frag->size = skb->len - hdr_len;
607 skb_shinfo(skb)->nr_frags = 1;
608 skb->data_len = frag->size;
609 } else {
610 __free_pages(rx_buf->page, efx->rx_buffer_order);
611 skb->data_len = 0;
614 /* Ownership has transferred from the rx_buf to skb */
615 rx_buf->page = NULL;
617 /* Move past the ethernet header */
618 skb->protocol = eth_type_trans(skb, efx->net_dev);
620 return skb;
623 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
624 unsigned int len, int checksummed, int discard)
626 struct efx_nic *efx = rx_queue->efx;
627 struct efx_rx_buffer *rx_buf;
628 int leak_packet = 0;
630 rx_buf = efx_rx_buffer(rx_queue, index);
631 EFX_BUG_ON_PARANOID(!rx_buf->data);
632 EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
633 EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
635 /* This allows the refill path to post another buffer.
636 * EFX_RXD_HEAD_ROOM ensures that the slot we are using
637 * isn't overwritten yet.
639 rx_queue->removed_count++;
641 /* Validate the length encoded in the event vs the descriptor pushed */
642 efx_rx_packet__check_len(rx_queue, rx_buf, len,
643 &discard, &leak_packet);
645 EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
646 rx_queue->queue, index,
647 (unsigned long long)rx_buf->dma_addr, len,
648 (checksummed ? " [SUMMED]" : ""),
649 (discard ? " [DISCARD]" : ""));
651 /* Discard packet, if instructed to do so */
652 if (unlikely(discard)) {
653 if (unlikely(leak_packet))
654 rx_queue->channel->n_skbuff_leaks++;
655 else
656 /* We haven't called efx_unmap_rx_buffer yet,
657 * so fini the entire rx_buffer here */
658 efx_fini_rx_buffer(rx_queue, rx_buf);
659 return;
662 /* Release card resources - assumes all RX buffers consumed in-order
663 * per RX queue
665 efx_unmap_rx_buffer(efx, rx_buf);
667 /* Prefetch nice and early so data will (hopefully) be in cache by
668 * the time we look at it.
670 prefetch(rx_buf->data);
672 /* Pipeline receives so that we give time for packet headers to be
673 * prefetched into cache.
675 rx_buf->len = len;
676 if (rx_queue->channel->rx_pkt)
677 __efx_rx_packet(rx_queue->channel,
678 rx_queue->channel->rx_pkt,
679 rx_queue->channel->rx_pkt_csummed);
680 rx_queue->channel->rx_pkt = rx_buf;
681 rx_queue->channel->rx_pkt_csummed = checksummed;
684 /* Handle a received packet. Second half: Touches packet payload. */
685 void __efx_rx_packet(struct efx_channel *channel,
686 struct efx_rx_buffer *rx_buf, int checksummed)
688 struct efx_nic *efx = channel->efx;
689 struct sk_buff *skb;
690 int lro = efx->net_dev->features & NETIF_F_LRO;
692 /* If we're in loopback test, then pass the packet directly to the
693 * loopback layer, and free the rx_buf here
695 if (unlikely(efx->loopback_selftest)) {
696 efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
697 efx_free_rx_buffer(efx, rx_buf);
698 goto done;
701 if (rx_buf->skb) {
702 prefetch(skb_shinfo(rx_buf->skb));
704 skb_put(rx_buf->skb, rx_buf->len);
706 /* Move past the ethernet header. rx_buf->data still points
707 * at the ethernet header */
708 rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
709 efx->net_dev);
712 /* Both our generic-LRO and SFC-SSR support skb and page based
713 * allocation, but neither support switching from one to the
714 * other on the fly. If we spot that the allocation mode has
715 * changed, then flush the LRO state.
717 if (unlikely(channel->rx_alloc_pop_pages != (rx_buf->page != NULL))) {
718 efx_flush_lro(channel);
719 channel->rx_alloc_pop_pages = (rx_buf->page != NULL);
721 if (likely(checksummed && lro)) {
722 efx_rx_packet_lro(channel, rx_buf);
723 goto done;
726 /* Form an skb if required */
727 if (rx_buf->page) {
728 int hdr_len = min(rx_buf->len, EFX_SKB_HEADERS);
729 skb = efx_rx_mk_skb(rx_buf, efx, hdr_len);
730 if (unlikely(skb == NULL)) {
731 efx_free_rx_buffer(efx, rx_buf);
732 goto done;
734 } else {
735 /* We now own the SKB */
736 skb = rx_buf->skb;
737 rx_buf->skb = NULL;
740 EFX_BUG_ON_PARANOID(rx_buf->page);
741 EFX_BUG_ON_PARANOID(rx_buf->skb);
742 EFX_BUG_ON_PARANOID(!skb);
744 /* Set the SKB flags */
745 if (unlikely(!checksummed || !efx->rx_checksum_enabled))
746 skb->ip_summed = CHECKSUM_NONE;
748 /* Pass the packet up */
749 netif_receive_skb(skb);
751 /* Update allocation strategy method */
752 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
754 done:
755 efx->net_dev->last_rx = jiffies;
758 void efx_rx_strategy(struct efx_channel *channel)
760 enum efx_rx_alloc_method method = rx_alloc_method;
762 /* Only makes sense to use page based allocation if LRO is enabled */
763 if (!(channel->efx->net_dev->features & NETIF_F_LRO)) {
764 method = RX_ALLOC_METHOD_SKB;
765 } else if (method == RX_ALLOC_METHOD_AUTO) {
766 /* Constrain the rx_alloc_level */
767 if (channel->rx_alloc_level < 0)
768 channel->rx_alloc_level = 0;
769 else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
770 channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
772 /* Decide on the allocation method */
773 method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
774 RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
777 /* Push the option */
778 channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
781 int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
783 struct efx_nic *efx = rx_queue->efx;
784 unsigned int rxq_size;
785 int rc;
787 EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
789 /* Allocate RX buffers */
790 rxq_size = (efx->type->rxd_ring_mask + 1) * sizeof(*rx_queue->buffer);
791 rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
792 if (!rx_queue->buffer) {
793 rc = -ENOMEM;
794 goto fail1;
797 rc = falcon_probe_rx(rx_queue);
798 if (rc)
799 goto fail2;
801 return 0;
803 fail2:
804 kfree(rx_queue->buffer);
805 rx_queue->buffer = NULL;
806 fail1:
807 rx_queue->used = 0;
809 return rc;
812 int efx_init_rx_queue(struct efx_rx_queue *rx_queue)
814 struct efx_nic *efx = rx_queue->efx;
815 unsigned int max_fill, trigger, limit;
817 EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
819 /* Initialise ptr fields */
820 rx_queue->added_count = 0;
821 rx_queue->notified_count = 0;
822 rx_queue->removed_count = 0;
823 rx_queue->min_fill = -1U;
824 rx_queue->min_overfill = -1U;
826 /* Initialise limit fields */
827 max_fill = efx->type->rxd_ring_mask + 1 - EFX_RXD_HEAD_ROOM;
828 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
829 limit = max_fill * min(rx_refill_limit, 100U) / 100U;
831 rx_queue->max_fill = max_fill;
832 rx_queue->fast_fill_trigger = trigger;
833 rx_queue->fast_fill_limit = limit;
835 /* Set up RX descriptor ring */
836 return falcon_init_rx(rx_queue);
839 void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
841 int i;
842 struct efx_rx_buffer *rx_buf;
844 EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
846 falcon_fini_rx(rx_queue);
848 /* Release RX buffers NB start at index 0 not current HW ptr */
849 if (rx_queue->buffer) {
850 for (i = 0; i <= rx_queue->efx->type->rxd_ring_mask; i++) {
851 rx_buf = efx_rx_buffer(rx_queue, i);
852 efx_fini_rx_buffer(rx_queue, rx_buf);
856 /* For a page that is part-way through splitting into RX buffers */
857 if (rx_queue->buf_page != NULL) {
858 pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
859 efx_rx_buf_size(rx_queue->efx),
860 PCI_DMA_FROMDEVICE);
861 __free_pages(rx_queue->buf_page,
862 rx_queue->efx->rx_buffer_order);
863 rx_queue->buf_page = NULL;
867 void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
869 EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
871 falcon_remove_rx(rx_queue);
873 kfree(rx_queue->buffer);
874 rx_queue->buffer = NULL;
875 rx_queue->used = 0;
878 void efx_flush_lro(struct efx_channel *channel)
880 lro_flush_all(&channel->lro_mgr);
884 module_param(rx_alloc_method, int, 0644);
885 MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
887 module_param(rx_refill_threshold, uint, 0444);
888 MODULE_PARM_DESC(rx_refill_threshold,
889 "RX descriptor ring fast/slow fill threshold (%)");