blob: 615a1fcd66441f5e9b5396229cf238ebbbd37b88 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings906bb262009-11-29 15:16:19 +00004 * Copyright 2005-2009 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010014#include <linux/ip.h>
15#include <linux/tcp.h>
16#include <linux/udp.h>
17#include <net/ip.h>
18#include <net/checksum.h>
19#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000021#include "nic.h"
Ben Hutchings3273c2e2008-05-07 13:36:19 +010022#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010023#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 */
Ben Hutchingsc3c63362009-10-29 07:21:33 +000064static int rx_alloc_method = RX_ALLOC_METHOD_AUTO;
Ben Hutchings8ceee662008-04-27 12:55:59 +010065
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 */
74static 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 */
79static 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
Ben Hutchings55668612008-05-16 21:16:10 +010089static inline unsigned int efx_rx_buf_offset(struct efx_rx_buffer *buf)
90{
91 /* Offset is always within one page, so we don't need to consider
92 * the page order.
93 */
Ben Hutchings184be0c2008-05-16 21:16:31 +010094 return (__force unsigned long) buf->data & (PAGE_SIZE - 1);
Ben Hutchings55668612008-05-16 21:16:10 +010095}
96static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
97{
98 return PAGE_SIZE << efx->rx_buffer_order;
99}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100100
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101/**
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000102 * efx_init_rx_buffers_skb - create EFX_RX_BATCH skb-based RX buffers
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103 *
104 * @rx_queue: Efx RX queue
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000106 * 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
109 * buffers.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100110 */
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000111static int efx_init_rx_buffers_skb(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112{
113 struct efx_nic *efx = rx_queue->efx;
114 struct net_device *net_dev = efx->net_dev;
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000115 struct efx_rx_buffer *rx_buf;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100116 int skb_len = efx->rx_buffer_len;
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000117 unsigned index, count;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100118
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000119 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100122
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000123 rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
124 if (unlikely(!rx_buf->skb))
125 return -ENOMEM;
126 rx_buf->page = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000128 /* 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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100133
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000134 rx_buf->dma_addr = pci_map_single(efx->pci_dev,
135 rx_buf->data, rx_buf->len,
136 PCI_DMA_FROMDEVICE);
137 if (unlikely(pci_dma_mapping_error(efx->pci_dev,
138 rx_buf->dma_addr))) {
139 dev_kfree_skb_any(rx_buf->skb);
140 rx_buf->skb = NULL;
141 return -EIO;
142 }
143
144 ++rx_queue->added_count;
145 ++rx_queue->alloc_skb_count;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100146 }
147
148 return 0;
149}
150
151/**
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000152 * efx_init_rx_buffers_page - create EFX_RX_BATCH page-based RX buffers
Ben Hutchings8ceee662008-04-27 12:55:59 +0100153 *
154 * @rx_queue: Efx RX queue
Ben Hutchings8ceee662008-04-27 12:55:59 +0100155 *
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000156 * 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.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160 */
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000161static int efx_init_rx_buffers_page(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162{
163 struct efx_nic *efx = rx_queue->efx;
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000164 struct efx_rx_buffer *rx_buf;
165 struct page *page;
166 char *page_addr;
167 dma_addr_t dma_addr;
168 unsigned index, count;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000170 /* We can split a page between two buffers */
171 BUILD_BUG_ON(EFX_RX_BATCH & 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100172
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000173 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))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100177 return -ENOMEM;
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000178 dma_addr = pci_map_page(efx->pci_dev, page, 0,
179 efx_rx_buf_size(efx),
Ben Hutchings8ceee662008-04-27 12:55:59 +0100180 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700181 if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000182 __free_pages(page, efx->rx_buffer_order);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100183 return -EIO;
184 }
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000185 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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000189 split:
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;
193 rx_buf->skb = NULL;
194 rx_buf->page = page;
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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000200 if ((~count & 1) && (efx->rx_buffer_len < (PAGE_SIZE >> 1))) {
201 /* Use the second half of the page */
202 get_page(page);
203 dma_addr += (PAGE_SIZE >> 1);
204 page_addr += (PAGE_SIZE >> 1);
205 ++count;
206 goto split;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207 }
208 }
209
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 return 0;
211}
212
Ben Hutchings4d566062008-09-01 12:47:12 +0100213static void efx_unmap_rx_buffer(struct efx_nic *efx,
214 struct efx_rx_buffer *rx_buf)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100215{
216 if (rx_buf->page) {
217 EFX_BUG_ON_PARANOID(rx_buf->skb);
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000218
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),
Ben Hutchings55668612008-05-16 21:16:10 +0100225 efx_rx_buf_size(efx),
226 PCI_DMA_FROMDEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100227 }
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);
231 }
232}
233
Ben Hutchings4d566062008-09-01 12:47:12 +0100234static void efx_free_rx_buffer(struct efx_nic *efx,
235 struct efx_rx_buffer *rx_buf)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100236{
237 if (rx_buf->page) {
238 __free_pages(rx_buf->page, efx->rx_buffer_order);
239 rx_buf->page = NULL;
240 } else if (likely(rx_buf->skb)) {
241 dev_kfree_skb_any(rx_buf->skb);
242 rx_buf->skb = NULL;
243 }
244}
245
Ben Hutchings4d566062008-09-01 12:47:12 +0100246static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
247 struct efx_rx_buffer *rx_buf)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100248{
249 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
250 efx_free_rx_buffer(rx_queue->efx, rx_buf);
251}
252
253/**
254 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
255 * @rx_queue: RX descriptor queue
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256 * This will aim to fill the RX descriptor queue up to
257 * @rx_queue->@fast_fill_limit. If there is insufficient atomic
Steve Hodgson90d683a2010-06-01 11:19:39 +0000258 * memory to do so, a slow fill will be scheduled.
259 *
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.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100263 */
Steve Hodgson90d683a2010-06-01 11:19:39 +0000264void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100265{
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000266 struct efx_channel *channel = rx_queue->channel;
267 unsigned fill_level;
268 int space, rc = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100269
Steve Hodgson90d683a2010-06-01 11:19:39 +0000270 /* Calculate current fill level, and exit if we don't need to fill */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100271 fill_level = (rx_queue->added_count - rx_queue->removed_count);
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000272 EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100273 if (fill_level >= rx_queue->fast_fill_trigger)
Steve Hodgson90d683a2010-06-01 11:19:39 +0000274 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
276 /* Record minimum fill level */
Ben Hutchingsb3475642008-05-16 21:15:49 +0100277 if (unlikely(fill_level < rx_queue->min_fill)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100278 if (fill_level)
279 rx_queue->min_fill = fill_level;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100280 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100281
Ben Hutchings8ceee662008-04-27 12:55:59 +0100282 space = rx_queue->fast_fill_limit - fill_level;
283 if (space < EFX_RX_BATCH)
Steve Hodgson90d683a2010-06-01 11:19:39 +0000284 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100285
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,
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000289 channel->rx_alloc_push_pages ? "page" : "skb");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100290
291 do {
Steve Hodgsonf7d6f372010-06-01 11:33:17 +0000292 if (channel->rx_alloc_push_pages)
293 rc = efx_init_rx_buffers_page(rx_queue);
294 else
295 rc = efx_init_rx_buffers_skb(rx_queue);
296 if (unlikely(rc)) {
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);
300 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100301 }
302 } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
303
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);
307
308 out:
309 /* Send write pointer to card. */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000310 efx_nic_notify_rx_desc(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311}
312
Steve Hodgson90d683a2010-06-01 11:19:39 +0000313void efx_rx_slow_fill(unsigned long context)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100314{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000315 struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context;
316 struct efx_channel *channel = rx_queue->channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317
Steve Hodgson90d683a2010-06-01 11:19:39 +0000318 /* Post an event to cause NAPI to run and refill the queue */
319 efx_nic_generate_fill_event(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320 ++rx_queue->slow_fill_count;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321}
322
Ben Hutchings4d566062008-09-01 12:47:12 +0100323static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
324 struct efx_rx_buffer *rx_buf,
325 int len, bool *discard,
326 bool *leak_packet)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100327{
328 struct efx_nic *efx = rx_queue->efx;
329 unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
330
331 if (likely(len <= max_len))
332 return;
333
334 /* The packet must be discarded, but this is only a fatal error
335 * if the caller indicated it was
336 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100337 *discard = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338
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.
347 */
348 *leak_packet = (rx_buf->skb != NULL);
349 efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
350 } else {
351 EFX_ERR_RL(efx, " RX queue %d overlength RX event "
352 "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
353 }
354
355 rx_queue->channel->n_rx_overlength++;
356}
357
358/* Pass a received packet up through the generic LRO stack
359 *
360 * Handles driverlink veto, and passes the fragment up via
361 * the appropriate LRO method
362 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100363static void efx_rx_packet_lro(struct efx_channel *channel,
Ben Hutchings345056a2009-10-28 03:43:49 -0700364 struct efx_rx_buffer *rx_buf,
365 bool checksummed)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100366{
Herbert Xuda3bc072009-01-18 21:50:16 -0800367 struct napi_struct *napi = &channel->napi_str;
Ben Hutchings18e1d2b2009-10-29 07:21:24 +0000368 gro_result_t gro_result;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100369
370 /* Pass the skb/page into the LRO engine */
371 if (rx_buf->page) {
Ben Hutchings1241e952009-11-23 16:02:25 +0000372 struct page *page = rx_buf->page;
373 struct sk_buff *skb;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374
Ben Hutchings1241e952009-11-23 16:02:25 +0000375 EFX_BUG_ON_PARANOID(rx_buf->skb);
376 rx_buf->page = NULL;
377
378 skb = napi_get_frags(napi);
Herbert Xu76620aa2009-04-16 02:02:07 -0700379 if (!skb) {
Ben Hutchings1241e952009-11-23 16:02:25 +0000380 put_page(page);
381 return;
Herbert Xu76620aa2009-04-16 02:02:07 -0700382 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383
Ben Hutchings1241e952009-11-23 16:02:25 +0000384 skb_shinfo(skb)->frags[0].page = page;
Herbert Xu76620aa2009-04-16 02:02:07 -0700385 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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100389
Herbert Xu76620aa2009-04-16 02:02:07 -0700390 skb->len = rx_buf->len;
391 skb->data_len = rx_buf->len;
392 skb->truesize += rx_buf->len;
Ben Hutchings345056a2009-10-28 03:43:49 -0700393 skb->ip_summed =
394 checksummed ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
Herbert Xu76620aa2009-04-16 02:02:07 -0700395
Ben Hutchings3eadb7b2009-11-23 16:02:40 +0000396 skb_record_rx_queue(skb, channel->channel);
397
Ben Hutchings18e1d2b2009-10-29 07:21:24 +0000398 gro_result = napi_gro_frags(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399 } else {
Ben Hutchings1241e952009-11-23 16:02:25 +0000400 struct sk_buff *skb = rx_buf->skb;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100401
Ben Hutchings1241e952009-11-23 16:02:25 +0000402 EFX_BUG_ON_PARANOID(!skb);
403 EFX_BUG_ON_PARANOID(!checksummed);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100404 rx_buf->skb = NULL;
Ben Hutchings1241e952009-11-23 16:02:25 +0000405
406 gro_result = napi_gro_receive(napi, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100407 }
Ben Hutchings18e1d2b2009-10-29 07:21:24 +0000408
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;
414 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100415}
416
Ben Hutchings8ceee662008-04-27 12:55:59 +0100417void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100418 unsigned int len, bool checksummed, bool discard)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100419{
420 struct efx_nic *efx = rx_queue->efx;
421 struct efx_rx_buffer *rx_buf;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100422 bool leak_packet = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423
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));
428
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.
432 */
433 rx_queue->removed_count++;
434
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);
438
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]" : ""));
444
445 /* Discard packet, if instructed to do so */
446 if (unlikely(discard)) {
447 if (unlikely(leak_packet))
448 rx_queue->channel->n_skbuff_leaks++;
449 else
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);
453 return;
454 }
455
456 /* Release card resources - assumes all RX buffers consumed in-order
457 * per RX queue
458 */
459 efx_unmap_rx_buffer(efx, rx_buf);
460
461 /* Prefetch nice and early so data will (hopefully) be in cache by
462 * the time we look at it.
463 */
464 prefetch(rx_buf->data);
465
466 /* Pipeline receives so that we give time for packet headers to be
467 * prefetched into cache.
468 */
469 rx_buf->len = len;
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;
476}
477
478/* Handle a received packet. Second half: Touches packet payload. */
479void __efx_rx_packet(struct efx_channel *channel,
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100480 struct efx_rx_buffer *rx_buf, bool checksummed)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481{
482 struct efx_nic *efx = channel->efx;
483 struct sk_buff *skb;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100484
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100485 /* If we're in loopback test, then pass the packet directly to the
486 * loopback layer, and free the rx_buf here
487 */
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);
Ben Hutchingsd96d7dc2009-11-23 16:01:44 +0000491 return;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100492 }
493
Ben Hutchings8ceee662008-04-27 12:55:59 +0100494 if (rx_buf->skb) {
495 prefetch(skb_shinfo(rx_buf->skb));
496
497 skb_put(rx_buf->skb, rx_buf->len);
498
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,
502 efx->net_dev);
Ben Hutchings3eadb7b2009-11-23 16:02:40 +0000503
504 skb_record_rx_queue(rx_buf->skb, channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505 }
506
Herbert Xuda3bc072009-01-18 21:50:16 -0800507 if (likely(checksummed || rx_buf->page)) {
Ben Hutchings345056a2009-10-28 03:43:49 -0700508 efx_rx_packet_lro(channel, rx_buf, checksummed);
Ben Hutchingsd96d7dc2009-11-23 16:01:44 +0000509 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510 }
511
Herbert Xuda3bc072009-01-18 21:50:16 -0800512 /* We now own the SKB */
513 skb = rx_buf->skb;
514 rx_buf->skb = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 EFX_BUG_ON_PARANOID(!skb);
516
517 /* Set the SKB flags */
Herbert Xuda3bc072009-01-18 21:50:16 -0800518 skb->ip_summed = CHECKSUM_NONE;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100519
520 /* Pass the packet up */
521 netif_receive_skb(skb);
522
523 /* Update allocation strategy method */
524 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100525}
526
527void efx_rx_strategy(struct efx_channel *channel)
528{
529 enum efx_rx_alloc_method method = rx_alloc_method;
530
531 /* Only makes sense to use page based allocation if LRO is enabled */
Herbert Xuda3bc072009-01-18 21:50:16 -0800532 if (!(channel->efx->net_dev->features & NETIF_F_GRO)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100533 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;
540
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);
544 }
545
546 /* Push the option */
547 channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
548}
549
550int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
551{
552 struct efx_nic *efx = rx_queue->efx;
553 unsigned int rxq_size;
554 int rc;
555
556 EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
557
558 /* Allocate RX buffers */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000559 rxq_size = EFX_RXQ_SIZE * sizeof(*rx_queue->buffer);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100560 rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
Ben Hutchings8831da72008-09-01 12:47:48 +0100561 if (!rx_queue->buffer)
562 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100563
Ben Hutchings152b6a62009-11-29 03:43:56 +0000564 rc = efx_nic_probe_rx(rx_queue);
Ben Hutchings8831da72008-09-01 12:47:48 +0100565 if (rc) {
566 kfree(rx_queue->buffer);
567 rx_queue->buffer = NULL;
568 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569 return rc;
570}
571
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100572void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100574 unsigned int max_fill, trigger, limit;
575
576 EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
577
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;
584
585 /* Initialise limit fields */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000586 max_fill = EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100587 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
588 limit = max_fill * min(rx_refill_limit, 100U) / 100U;
589
590 rx_queue->max_fill = max_fill;
591 rx_queue->fast_fill_trigger = trigger;
592 rx_queue->fast_fill_limit = limit;
593
594 /* Set up RX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000595 efx_nic_init_rx(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100596}
597
598void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
599{
600 int i;
601 struct efx_rx_buffer *rx_buf;
602
603 EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
604
Steve Hodgson90d683a2010-06-01 11:19:39 +0000605 del_timer_sync(&rx_queue->slow_fill);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000606 efx_nic_fini_rx(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100607
608 /* Release RX buffers NB start at index 0 not current HW ptr */
609 if (rx_queue->buffer) {
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000610 for (i = 0; i <= EFX_RXQ_MASK; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611 rx_buf = efx_rx_buffer(rx_queue, i);
612 efx_fini_rx_buffer(rx_queue, rx_buf);
613 }
614 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615}
616
617void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
618{
619 EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
620
Ben Hutchings152b6a62009-11-29 03:43:56 +0000621 efx_nic_remove_rx(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622
623 kfree(rx_queue->buffer);
624 rx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625}
626
Ben Hutchings8ceee662008-04-27 12:55:59 +0100627
628module_param(rx_alloc_method, int, 0644);
629MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
630
631module_param(rx_refill_threshold, uint, 0444);
632MODULE_PARM_DESC(rx_refill_threshold,
633 "RX descriptor ring fast/slow fill threshold (%)");
634