1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2009 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.
11 #include <linux/socket.h>
13 #include <linux/slab.h>
15 #include <linux/tcp.h>
16 #include <linux/udp.h>
18 #include <net/checksum.h>
19 #include "net_driver.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.
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_AUTO;
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
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
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 * efx_init_rx_buffers_skb - create EFX_RX_BATCH skb-based RX buffers
104 * @rx_queue: Efx RX queue
106 * This allocates EFX_RX_BATCH skbs, maps them for DMA, and populates a
107 * struct efx_rx_buffer for each one. Return a negative error code or 0
108 * on success. May fail having only inserted fewer than EFX_RX_BATCH
111 static int efx_init_rx_buffers_skb(struct efx_rx_queue *rx_queue)
113 struct efx_nic *efx = rx_queue->efx;
114 struct net_device *net_dev = efx->net_dev;
115 struct efx_rx_buffer *rx_buf;
116 int skb_len = efx->rx_buffer_len;
117 unsigned index, count;
119 for (count = 0; count < EFX_RX_BATCH; ++count) {
120 index = rx_queue->added_count & EFX_RXQ_MASK;
121 rx_buf = efx_rx_buffer(rx_queue, index);
123 rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
124 if (unlikely(!rx_buf->skb))
128 /* Adjust the SKB for padding and checksum */
129 skb_reserve(rx_buf->skb, NET_IP_ALIGN);
130 rx_buf->len = skb_len - NET_IP_ALIGN;
131 rx_buf->data = (char *)rx_buf->skb->data;
132 rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
134 rx_buf->dma_addr = pci_map_single(efx->pci_dev,
135 rx_buf->data, rx_buf->len,
137 if (unlikely(pci_dma_mapping_error(efx->pci_dev,
138 rx_buf->dma_addr))) {
139 dev_kfree_skb_any(rx_buf->skb);
144 ++rx_queue->added_count;
145 ++rx_queue->alloc_skb_count;
152 * efx_init_rx_buffers_page - create EFX_RX_BATCH page-based RX buffers
154 * @rx_queue: Efx RX queue
156 * This allocates memory for EFX_RX_BATCH receive buffers, maps them for DMA,
157 * and populates struct efx_rx_buffers for each one. Return a negative error
158 * code or 0 on success. If a single page can be split between two buffers,
159 * then the page will either be inserted fully, or not at at all.
161 static int efx_init_rx_buffers_page(struct efx_rx_queue *rx_queue)
163 struct efx_nic *efx = rx_queue->efx;
164 struct efx_rx_buffer *rx_buf;
168 unsigned index, count;
170 /* We can split a page between two buffers */
171 BUILD_BUG_ON(EFX_RX_BATCH & 1);
173 for (count = 0; count < EFX_RX_BATCH; ++count) {
174 page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
175 efx->rx_buffer_order);
176 if (unlikely(page == NULL))
178 dma_addr = pci_map_page(efx->pci_dev, page, 0,
179 efx_rx_buf_size(efx),
181 if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
182 __free_pages(page, efx->rx_buffer_order);
185 EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1));
186 page_addr = page_address(page) + EFX_PAGE_IP_ALIGN;
187 dma_addr += EFX_PAGE_IP_ALIGN;
190 index = rx_queue->added_count & EFX_RXQ_MASK;
191 rx_buf = efx_rx_buffer(rx_queue, index);
192 rx_buf->dma_addr = dma_addr;
195 rx_buf->data = page_addr;
196 rx_buf->len = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
197 ++rx_queue->added_count;
198 ++rx_queue->alloc_page_count;
200 if ((~count & 1) && (efx->rx_buffer_len < (PAGE_SIZE >> 1))) {
201 /* Use the second half of the page */
203 dma_addr += (PAGE_SIZE >> 1);
204 page_addr += (PAGE_SIZE >> 1);
213 static void efx_unmap_rx_buffer(struct efx_nic *efx,
214 struct efx_rx_buffer *rx_buf)
217 EFX_BUG_ON_PARANOID(rx_buf->skb);
219 /* Unmap the buffer if there's only one buffer per page(s),
220 * or this is the second half of a two buffer page. */
221 if (efx->rx_buffer_order != 0 ||
222 (efx_rx_buf_offset(rx_buf) & (PAGE_SIZE >> 1)) != 0) {
223 pci_unmap_page(efx->pci_dev,
224 rx_buf->dma_addr & ~(PAGE_SIZE - 1),
225 efx_rx_buf_size(efx),
228 } else if (likely(rx_buf->skb)) {
229 pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
230 rx_buf->len, PCI_DMA_FROMDEVICE);
234 static void efx_free_rx_buffer(struct efx_nic *efx,
235 struct efx_rx_buffer *rx_buf)
238 __free_pages(rx_buf->page, efx->rx_buffer_order);
240 } else if (likely(rx_buf->skb)) {
241 dev_kfree_skb_any(rx_buf->skb);
246 static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
247 struct efx_rx_buffer *rx_buf)
249 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
250 efx_free_rx_buffer(rx_queue->efx, rx_buf);
254 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
255 * @rx_queue: RX descriptor queue
256 * This will aim to fill the RX descriptor queue up to
257 * @rx_queue->@fast_fill_limit. If there is insufficient atomic
258 * memory to do so, a slow fill will be scheduled.
260 * The caller must provide serialisation (none is used here). In practise,
261 * this means this function must run from the NAPI handler, or be called
262 * when NAPI is disabled.
264 void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
266 struct efx_channel *channel = rx_queue->channel;
270 /* Calculate current fill level, and exit if we don't need to fill */
271 fill_level = (rx_queue->added_count - rx_queue->removed_count);
272 EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
273 if (fill_level >= rx_queue->fast_fill_trigger)
276 /* Record minimum fill level */
277 if (unlikely(fill_level < rx_queue->min_fill)) {
279 rx_queue->min_fill = fill_level;
282 space = rx_queue->fast_fill_limit - fill_level;
283 if (space < EFX_RX_BATCH)
286 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
287 " level %d to level %d using %s allocation\n",
288 rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
289 channel->rx_alloc_push_pages ? "page" : "skb");
292 if (channel->rx_alloc_push_pages)
293 rc = efx_init_rx_buffers_page(rx_queue);
295 rc = efx_init_rx_buffers_skb(rx_queue);
297 /* Ensure that we don't leave the rx queue empty */
298 if (rx_queue->added_count == rx_queue->removed_count)
299 efx_schedule_slow_fill(rx_queue);
302 } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
304 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
305 "to level %d\n", rx_queue->queue,
306 rx_queue->added_count - rx_queue->removed_count);
309 /* Send write pointer to card. */
310 efx_nic_notify_rx_desc(rx_queue);
313 void efx_rx_slow_fill(unsigned long context)
315 struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context;
316 struct efx_channel *channel = rx_queue->channel;
318 /* Post an event to cause NAPI to run and refill the queue */
319 efx_nic_generate_fill_event(channel);
320 ++rx_queue->slow_fill_count;
323 static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
324 struct efx_rx_buffer *rx_buf,
325 int len, bool *discard,
328 struct efx_nic *efx = rx_queue->efx;
329 unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
331 if (likely(len <= max_len))
334 /* The packet must be discarded, but this is only a fatal error
335 * if the caller indicated it was
339 if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
340 EFX_ERR_RL(efx, " RX queue %d seriously overlength "
341 "RX event (0x%x > 0x%x+0x%x). Leaking\n",
342 rx_queue->queue, len, max_len,
343 efx->type->rx_buffer_padding);
344 /* If this buffer was skb-allocated, then the meta
345 * data at the end of the skb will be trashed. So
346 * we have no choice but to leak the fragment.
348 *leak_packet = (rx_buf->skb != NULL);
349 efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
351 EFX_ERR_RL(efx, " RX queue %d overlength RX event "
352 "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
355 rx_queue->channel->n_rx_overlength++;
358 /* Pass a received packet up through the generic LRO stack
360 * Handles driverlink veto, and passes the fragment up via
361 * the appropriate LRO method
363 static void efx_rx_packet_lro(struct efx_channel *channel,
364 struct efx_rx_buffer *rx_buf,
367 struct napi_struct *napi = &channel->napi_str;
368 gro_result_t gro_result;
370 /* Pass the skb/page into the LRO engine */
372 struct page *page = rx_buf->page;
375 EFX_BUG_ON_PARANOID(rx_buf->skb);
378 skb = napi_get_frags(napi);
384 skb_shinfo(skb)->frags[0].page = page;
385 skb_shinfo(skb)->frags[0].page_offset =
386 efx_rx_buf_offset(rx_buf);
387 skb_shinfo(skb)->frags[0].size = rx_buf->len;
388 skb_shinfo(skb)->nr_frags = 1;
390 skb->len = rx_buf->len;
391 skb->data_len = rx_buf->len;
392 skb->truesize += rx_buf->len;
394 checksummed ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
396 skb_record_rx_queue(skb, channel->channel);
398 gro_result = napi_gro_frags(napi);
400 struct sk_buff *skb = rx_buf->skb;
402 EFX_BUG_ON_PARANOID(!skb);
403 EFX_BUG_ON_PARANOID(!checksummed);
406 gro_result = napi_gro_receive(napi, skb);
409 if (gro_result == GRO_NORMAL) {
410 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
411 } else if (gro_result != GRO_DROP) {
412 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
413 channel->irq_mod_score += 2;
417 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
418 unsigned int len, bool checksummed, bool discard)
420 struct efx_nic *efx = rx_queue->efx;
421 struct efx_rx_buffer *rx_buf;
422 bool leak_packet = false;
424 rx_buf = efx_rx_buffer(rx_queue, index);
425 EFX_BUG_ON_PARANOID(!rx_buf->data);
426 EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
427 EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
429 /* This allows the refill path to post another buffer.
430 * EFX_RXD_HEAD_ROOM ensures that the slot we are using
431 * isn't overwritten yet.
433 rx_queue->removed_count++;
435 /* Validate the length encoded in the event vs the descriptor pushed */
436 efx_rx_packet__check_len(rx_queue, rx_buf, len,
437 &discard, &leak_packet);
439 EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
440 rx_queue->queue, index,
441 (unsigned long long)rx_buf->dma_addr, len,
442 (checksummed ? " [SUMMED]" : ""),
443 (discard ? " [DISCARD]" : ""));
445 /* Discard packet, if instructed to do so */
446 if (unlikely(discard)) {
447 if (unlikely(leak_packet))
448 rx_queue->channel->n_skbuff_leaks++;
450 /* We haven't called efx_unmap_rx_buffer yet,
451 * so fini the entire rx_buffer here */
452 efx_fini_rx_buffer(rx_queue, rx_buf);
456 /* Release card resources - assumes all RX buffers consumed in-order
459 efx_unmap_rx_buffer(efx, rx_buf);
461 /* Prefetch nice and early so data will (hopefully) be in cache by
462 * the time we look at it.
464 prefetch(rx_buf->data);
466 /* Pipeline receives so that we give time for packet headers to be
467 * prefetched into cache.
470 if (rx_queue->channel->rx_pkt)
471 __efx_rx_packet(rx_queue->channel,
472 rx_queue->channel->rx_pkt,
473 rx_queue->channel->rx_pkt_csummed);
474 rx_queue->channel->rx_pkt = rx_buf;
475 rx_queue->channel->rx_pkt_csummed = checksummed;
478 /* Handle a received packet. Second half: Touches packet payload. */
479 void __efx_rx_packet(struct efx_channel *channel,
480 struct efx_rx_buffer *rx_buf, bool checksummed)
482 struct efx_nic *efx = channel->efx;
485 /* If we're in loopback test, then pass the packet directly to the
486 * loopback layer, and free the rx_buf here
488 if (unlikely(efx->loopback_selftest)) {
489 efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
490 efx_free_rx_buffer(efx, rx_buf);
495 prefetch(skb_shinfo(rx_buf->skb));
497 skb_put(rx_buf->skb, rx_buf->len);
499 /* Move past the ethernet header. rx_buf->data still points
500 * at the ethernet header */
501 rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
504 skb_record_rx_queue(rx_buf->skb, channel->channel);
507 if (likely(checksummed || rx_buf->page)) {
508 efx_rx_packet_lro(channel, rx_buf, checksummed);
512 /* We now own the SKB */
515 EFX_BUG_ON_PARANOID(!skb);
517 /* Set the SKB flags */
518 skb->ip_summed = CHECKSUM_NONE;
520 /* Pass the packet up */
521 netif_receive_skb(skb);
523 /* Update allocation strategy method */
524 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
527 void efx_rx_strategy(struct efx_channel *channel)
529 enum efx_rx_alloc_method method = rx_alloc_method;
531 /* Only makes sense to use page based allocation if LRO is enabled */
532 if (!(channel->efx->net_dev->features & NETIF_F_GRO)) {
533 method = RX_ALLOC_METHOD_SKB;
534 } else if (method == RX_ALLOC_METHOD_AUTO) {
535 /* Constrain the rx_alloc_level */
536 if (channel->rx_alloc_level < 0)
537 channel->rx_alloc_level = 0;
538 else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
539 channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
541 /* Decide on the allocation method */
542 method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
543 RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
546 /* Push the option */
547 channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
550 int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
552 struct efx_nic *efx = rx_queue->efx;
553 unsigned int rxq_size;
556 EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
558 /* Allocate RX buffers */
559 rxq_size = EFX_RXQ_SIZE * sizeof(*rx_queue->buffer);
560 rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
561 if (!rx_queue->buffer)
564 rc = efx_nic_probe_rx(rx_queue);
566 kfree(rx_queue->buffer);
567 rx_queue->buffer = NULL;
572 void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
574 unsigned int max_fill, trigger, limit;
576 EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
578 /* Initialise ptr fields */
579 rx_queue->added_count = 0;
580 rx_queue->notified_count = 0;
581 rx_queue->removed_count = 0;
582 rx_queue->min_fill = -1U;
583 rx_queue->min_overfill = -1U;
585 /* Initialise limit fields */
586 max_fill = EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM;
587 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
588 limit = max_fill * min(rx_refill_limit, 100U) / 100U;
590 rx_queue->max_fill = max_fill;
591 rx_queue->fast_fill_trigger = trigger;
592 rx_queue->fast_fill_limit = limit;
594 /* Set up RX descriptor ring */
595 efx_nic_init_rx(rx_queue);
598 void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
601 struct efx_rx_buffer *rx_buf;
603 EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
605 del_timer_sync(&rx_queue->slow_fill);
606 efx_nic_fini_rx(rx_queue);
608 /* Release RX buffers NB start at index 0 not current HW ptr */
609 if (rx_queue->buffer) {
610 for (i = 0; i <= EFX_RXQ_MASK; i++) {
611 rx_buf = efx_rx_buffer(rx_queue, i);
612 efx_fini_rx_buffer(rx_queue, rx_buf);
617 void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
619 EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
621 efx_nic_remove_rx(rx_queue);
623 kfree(rx_queue->buffer);
624 rx_queue->buffer = NULL;
628 module_param(rx_alloc_method, int, 0644);
629 MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
631 module_param(rx_refill_threshold, uint, 0444);
632 MODULE_PARM_DESC(rx_refill_threshold,
633 "RX descriptor ring fast/slow fill threshold (%)");