/drivers/net/sfc/rx.c
C | 749 lines | 476 code | 115 blank | 158 comment | 75 complexity | 060e3a5538f9c9e94eae8311245ad9b6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /****************************************************************************
- * Driver for Solarflare Solarstorm network controllers and boards
- * Copyright 2005-2006 Fen Systems Ltd.
- * Copyright 2005-2011 Solarflare Communications Inc.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation, incorporated herein by reference.
- */
- #include <linux/socket.h>
- #include <linux/in.h>
- #include <linux/slab.h>
- #include <linux/ip.h>
- #include <linux/tcp.h>
- #include <linux/udp.h>
- #include <linux/prefetch.h>
- #include <net/ip.h>
- #include <net/checksum.h>
- #include "net_driver.h"
- #include "efx.h"
- #include "nic.h"
- #include "selftest.h"
- #include "workarounds.h"
- /* Number of RX descriptors pushed at once. */
- #define EFX_RX_BATCH 8
- /* Maximum size of a buffer sharing a page */
- #define EFX_RX_HALF_PAGE ((PAGE_SIZE >> 1) - sizeof(struct efx_rx_page_state))
- /* Size of buffer allocated for skb header area. */
- #define EFX_SKB_HEADERS 64u
- /*
- * rx_alloc_method - RX buffer allocation method
- *
- * This driver supports two methods for allocating and using RX buffers:
- * each RX buffer may be backed by an skb or by an order-n page.
- *
- * When GRO is in use then the second method has a lower overhead,
- * since we don't have to allocate then free skbs on reassembled frames.
- *
- * Values:
- * - RX_ALLOC_METHOD_AUTO = 0
- * - RX_ALLOC_METHOD_SKB = 1
- * - RX_ALLOC_METHOD_PAGE = 2
- *
- * The heuristic for %RX_ALLOC_METHOD_AUTO is a simple hysteresis count
- * controlled by the parameters below.
- *
- * - Since pushing and popping descriptors are separated by the rx_queue
- * size, so the watermarks should be ~rxd_size.
- * - The performance win by using page-based allocation for GRO is less
- * than the performance hit of using page-based allocation of non-GRO,
- * so the watermarks should reflect this.
- *
- * Per channel we maintain a single variable, updated by each channel:
- *
- * rx_alloc_level += (gro_performed ? RX_ALLOC_FACTOR_GRO :
- * RX_ALLOC_FACTOR_SKB)
- * Per NAPI poll interval, we constrain rx_alloc_level to 0..MAX (which
- * limits the hysteresis), and update the allocation strategy:
- *
- * rx_alloc_method = (rx_alloc_level > RX_ALLOC_LEVEL_GRO ?
- * RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB)
- */
- static int rx_alloc_method = RX_ALLOC_METHOD_AUTO;
- #define RX_ALLOC_LEVEL_GRO 0x2000
- #define RX_ALLOC_LEVEL_MAX 0x3000
- #define RX_ALLOC_FACTOR_GRO 1
- #define RX_ALLOC_FACTOR_SKB (-2)
- /* This is the percentage fill level below which new RX descriptors
- * will be added to the RX descriptor ring.
- */
- static unsigned int rx_refill_threshold = 90;
- /* This is the percentage fill level to which an RX queue will be refilled
- * when the "RX refill threshold" is reached.
- */
- static unsigned int rx_refill_limit = 95;
- /*
- * RX maximum head room required.
- *
- * This must be at least 1 to prevent overflow and at least 2 to allow
- * pipelined receives.
- */
- #define EFX_RXD_HEAD_ROOM 2
- /* Offset of ethernet header within page */
- static inline unsigned int efx_rx_buf_offset(struct efx_nic *efx,
- struct efx_rx_buffer *buf)
- {
- /* Offset is always within one page, so we don't need to consider
- * the page order.
- */
- return (((__force unsigned long) buf->dma_addr & (PAGE_SIZE - 1)) +
- efx->type->rx_buffer_hash_size);
- }
- static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
- {
- return PAGE_SIZE << efx->rx_buffer_order;
- }
- static u8 *efx_rx_buf_eh(struct efx_nic *efx, struct efx_rx_buffer *buf)
- {
- if (buf->is_page)
- return page_address(buf->u.page) + efx_rx_buf_offset(efx, buf);
- else
- return ((u8 *)buf->u.skb->data +
- efx->type->rx_buffer_hash_size);
- }
- static inline u32 efx_rx_buf_hash(const u8 *eh)
- {
- /* The ethernet header is always directly after any hash. */
- #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || NET_IP_ALIGN % 4 == 0
- return __le32_to_cpup((const __le32 *)(eh - 4));
- #else
- const u8 *data = eh - 4;
- return ((u32)data[0] |
- (u32)data[1] << 8 |
- (u32)data[2] << 16 |
- (u32)data[3] << 24);
- #endif
- }
- /**
- * efx_init_rx_buffers_skb - create EFX_RX_BATCH skb-based RX buffers
- *
- * @rx_queue: Efx RX queue
- *
- * This allocates EFX_RX_BATCH skbs, maps them for DMA, and populates a
- * struct efx_rx_buffer for each one. Return a negative error code or 0
- * on success. May fail having only inserted fewer than EFX_RX_BATCH
- * buffers.
- */
- static int efx_init_rx_buffers_skb(struct efx_rx_queue *rx_queue)
- {
- struct efx_nic *efx = rx_queue->efx;
- struct net_device *net_dev = efx->net_dev;
- struct efx_rx_buffer *rx_buf;
- struct sk_buff *skb;
- int skb_len = efx->rx_buffer_len;
- unsigned index, count;
- for (count = 0; count < EFX_RX_BATCH; ++count) {
- index = rx_queue->added_count & rx_queue->ptr_mask;
- rx_buf = efx_rx_buffer(rx_queue, index);
- rx_buf->u.skb = skb = netdev_alloc_skb(net_dev, skb_len);
- if (unlikely(!skb))
- return -ENOMEM;
- /* Adjust the SKB for padding */
- skb_reserve(skb, NET_IP_ALIGN);
- rx_buf->len = skb_len - NET_IP_ALIGN;
- rx_buf->is_page = false;
- rx_buf->dma_addr = pci_map_single(efx->pci_dev,
- skb->data, rx_buf->len,
- PCI_DMA_FROMDEVICE);
- if (unlikely(pci_dma_mapping_error(efx->pci_dev,
- rx_buf->dma_addr))) {
- dev_kfree_skb_any(skb);
- rx_buf->u.skb = NULL;
- return -EIO;
- }
- ++rx_queue->added_count;
- ++rx_queue->alloc_skb_count;
- }
- return 0;
- }
- /**
- * efx_init_rx_buffers_page - create EFX_RX_BATCH page-based RX buffers
- *
- * @rx_queue: Efx RX queue
- *
- * This allocates memory for EFX_RX_BATCH receive buffers, maps them for DMA,
- * and populates struct efx_rx_buffers for each one. Return a negative error
- * code or 0 on success. If a single page can be split between two buffers,
- * then the page will either be inserted fully, or not at at all.
- */
- static int efx_init_rx_buffers_page(struct efx_rx_queue *rx_queue)
- {
- struct efx_nic *efx = rx_queue->efx;
- struct efx_rx_buffer *rx_buf;
- struct page *page;
- void *page_addr;
- struct efx_rx_page_state *state;
- dma_addr_t dma_addr;
- unsigned index, count;
- /* We can split a page between two buffers */
- BUILD_BUG_ON(EFX_RX_BATCH & 1);
- for (count = 0; count < EFX_RX_BATCH; ++count) {
- page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
- efx->rx_buffer_order);
- if (unlikely(page == NULL))
- return -ENOMEM;
- dma_addr = pci_map_page(efx->pci_dev, page, 0,
- efx_rx_buf_size(efx),
- PCI_DMA_FROMDEVICE);
- if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
- __free_pages(page, efx->rx_buffer_order);
- return -EIO;
- }
- page_addr = page_address(page);
- state = page_addr;
- state->refcnt = 0;
- state->dma_addr = dma_addr;
- page_addr += sizeof(struct efx_rx_page_state);
- dma_addr += sizeof(struct efx_rx_page_state);
- split:
- index = rx_queue->added_count & rx_queue->ptr_mask;
- rx_buf = efx_rx_buffer(rx_queue, index);
- rx_buf->dma_addr = dma_addr + EFX_PAGE_IP_ALIGN;
- rx_buf->u.page = page;
- rx_buf->len = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
- rx_buf->is_page = true;
- ++rx_queue->added_count;
- ++rx_queue->alloc_page_count;
- ++state->refcnt;
- if ((~count & 1) && (efx->rx_buffer_len <= EFX_RX_HALF_PAGE)) {
- /* Use the second half of the page */
- get_page(page);
- dma_addr += (PAGE_SIZE >> 1);
- page_addr += (PAGE_SIZE >> 1);
- ++count;
- goto split;
- }
- }
- return 0;
- }
- static void efx_unmap_rx_buffer(struct efx_nic *efx,
-