]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/sfc/rx.c
sfc: Move assertions and buffer cleanup earlier in efx_rx_packet_lro()
[mirror_ubuntu-bionic-kernel.git] / drivers / net / sfc / rx.c
CommitLineData
8ceee660
BH
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"
3273c2e8 22#include "selftest.h"
8ceee660
BH
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 */
c3c63365 64static int rx_alloc_method = RX_ALLOC_METHOD_AUTO;
8ceee660
BH
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 */
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
55668611
BH
89static 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 */
184be0c2 94 return (__force unsigned long) buf->data & (PAGE_SIZE - 1);
55668611
BH
95}
96static inline unsigned int efx_rx_buf_size(struct efx_nic *efx)
97{
98 return PAGE_SIZE << efx->rx_buffer_order;
99}
8ceee660
BH
100
101
8ceee660
BH
102/**
103 * efx_init_rx_buffer_skb - create new RX buffer using skb-based allocation
104 *
105 * @rx_queue: Efx RX queue
106 * @rx_buf: RX buffer structure to populate
107 *
108 * This allocates memory for a new receive buffer, maps it for DMA,
109 * and populates a struct efx_rx_buffer with the relevant
110 * information. Return a negative error code or 0 on success.
111 */
4d566063
BH
112static int efx_init_rx_buffer_skb(struct efx_rx_queue *rx_queue,
113 struct efx_rx_buffer *rx_buf)
8ceee660
BH
114{
115 struct efx_nic *efx = rx_queue->efx;
116 struct net_device *net_dev = efx->net_dev;
117 int skb_len = efx->rx_buffer_len;
118
119 rx_buf->skb = netdev_alloc_skb(net_dev, skb_len);
120 if (unlikely(!rx_buf->skb))
121 return -ENOMEM;
122
123 /* Adjust the SKB for padding and checksum */
124 skb_reserve(rx_buf->skb, NET_IP_ALIGN);
125 rx_buf->len = skb_len - NET_IP_ALIGN;
126 rx_buf->data = (char *)rx_buf->skb->data;
127 rx_buf->skb->ip_summed = CHECKSUM_UNNECESSARY;
128
129 rx_buf->dma_addr = pci_map_single(efx->pci_dev,
130 rx_buf->data, rx_buf->len,
131 PCI_DMA_FROMDEVICE);
132
8d8bb39b 133 if (unlikely(pci_dma_mapping_error(efx->pci_dev, rx_buf->dma_addr))) {
8ceee660
BH
134 dev_kfree_skb_any(rx_buf->skb);
135 rx_buf->skb = NULL;
136 return -EIO;
137 }
138
139 return 0;
140}
141
142/**
143 * efx_init_rx_buffer_page - create new RX buffer using page-based allocation
144 *
145 * @rx_queue: Efx RX queue
146 * @rx_buf: RX buffer structure to populate
147 *
148 * This allocates memory for a new receive buffer, maps it for DMA,
149 * and populates a struct efx_rx_buffer with the relevant
150 * information. Return a negative error code or 0 on success.
151 */
4d566063
BH
152static int efx_init_rx_buffer_page(struct efx_rx_queue *rx_queue,
153 struct efx_rx_buffer *rx_buf)
8ceee660
BH
154{
155 struct efx_nic *efx = rx_queue->efx;
156 int bytes, space, offset;
157
158 bytes = efx->rx_buffer_len - EFX_PAGE_IP_ALIGN;
159
160 /* If there is space left in the previously allocated page,
161 * then use it. Otherwise allocate a new one */
162 rx_buf->page = rx_queue->buf_page;
163 if (rx_buf->page == NULL) {
164 dma_addr_t dma_addr;
165
166 rx_buf->page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC,
167 efx->rx_buffer_order);
168 if (unlikely(rx_buf->page == NULL))
169 return -ENOMEM;
170
171 dma_addr = pci_map_page(efx->pci_dev, rx_buf->page,
55668611 172 0, efx_rx_buf_size(efx),
8ceee660
BH
173 PCI_DMA_FROMDEVICE);
174
8d8bb39b 175 if (unlikely(pci_dma_mapping_error(efx->pci_dev, dma_addr))) {
8ceee660
BH
176 __free_pages(rx_buf->page, efx->rx_buffer_order);
177 rx_buf->page = NULL;
178 return -EIO;
179 }
180
181 rx_queue->buf_page = rx_buf->page;
182 rx_queue->buf_dma_addr = dma_addr;
d3208b5e 183 rx_queue->buf_data = (page_address(rx_buf->page) +
8ceee660
BH
184 EFX_PAGE_IP_ALIGN);
185 }
186
8ceee660 187 rx_buf->len = bytes;
8ceee660 188 rx_buf->data = rx_queue->buf_data;
184be0c2
BH
189 offset = efx_rx_buf_offset(rx_buf);
190 rx_buf->dma_addr = rx_queue->buf_dma_addr + offset;
8ceee660
BH
191
192 /* Try to pack multiple buffers per page */
193 if (efx->rx_buffer_order == 0) {
194 /* The next buffer starts on the next 512 byte boundary */
195 rx_queue->buf_data += ((bytes + 0x1ff) & ~0x1ff);
196 offset += ((bytes + 0x1ff) & ~0x1ff);
197
55668611 198 space = efx_rx_buf_size(efx) - offset;
8ceee660
BH
199 if (space >= bytes) {
200 /* Refs dropped on kernel releasing each skb */
201 get_page(rx_queue->buf_page);
202 goto out;
203 }
204 }
205
206 /* This is the final RX buffer for this page, so mark it for
207 * unmapping */
208 rx_queue->buf_page = NULL;
209 rx_buf->unmap_addr = rx_queue->buf_dma_addr;
210
211 out:
212 return 0;
213}
214
215/* This allocates memory for a new receive buffer, maps it for DMA,
216 * and populates a struct efx_rx_buffer with the relevant
217 * information.
218 */
4d566063
BH
219static int efx_init_rx_buffer(struct efx_rx_queue *rx_queue,
220 struct efx_rx_buffer *new_rx_buf)
8ceee660
BH
221{
222 int rc = 0;
223
224 if (rx_queue->channel->rx_alloc_push_pages) {
225 new_rx_buf->skb = NULL;
226 rc = efx_init_rx_buffer_page(rx_queue, new_rx_buf);
227 rx_queue->alloc_page_count++;
228 } else {
229 new_rx_buf->page = NULL;
230 rc = efx_init_rx_buffer_skb(rx_queue, new_rx_buf);
231 rx_queue->alloc_skb_count++;
232 }
233
234 if (unlikely(rc < 0))
235 EFX_LOG_RL(rx_queue->efx, "%s RXQ[%d] =%d\n", __func__,
236 rx_queue->queue, rc);
237 return rc;
238}
239
4d566063
BH
240static void efx_unmap_rx_buffer(struct efx_nic *efx,
241 struct efx_rx_buffer *rx_buf)
8ceee660
BH
242{
243 if (rx_buf->page) {
244 EFX_BUG_ON_PARANOID(rx_buf->skb);
245 if (rx_buf->unmap_addr) {
246 pci_unmap_page(efx->pci_dev, rx_buf->unmap_addr,
55668611
BH
247 efx_rx_buf_size(efx),
248 PCI_DMA_FROMDEVICE);
8ceee660
BH
249 rx_buf->unmap_addr = 0;
250 }
251 } else if (likely(rx_buf->skb)) {
252 pci_unmap_single(efx->pci_dev, rx_buf->dma_addr,
253 rx_buf->len, PCI_DMA_FROMDEVICE);
254 }
255}
256
4d566063
BH
257static void efx_free_rx_buffer(struct efx_nic *efx,
258 struct efx_rx_buffer *rx_buf)
8ceee660
BH
259{
260 if (rx_buf->page) {
261 __free_pages(rx_buf->page, efx->rx_buffer_order);
262 rx_buf->page = NULL;
263 } else if (likely(rx_buf->skb)) {
264 dev_kfree_skb_any(rx_buf->skb);
265 rx_buf->skb = NULL;
266 }
267}
268
4d566063
BH
269static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
270 struct efx_rx_buffer *rx_buf)
8ceee660
BH
271{
272 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
273 efx_free_rx_buffer(rx_queue->efx, rx_buf);
274}
275
276/**
277 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
278 * @rx_queue: RX descriptor queue
279 * @retry: Recheck the fill level
280 * This will aim to fill the RX descriptor queue up to
281 * @rx_queue->@fast_fill_limit. If there is insufficient atomic
282 * memory to do so, the caller should retry.
283 */
284static int __efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
285 int retry)
286{
287 struct efx_rx_buffer *rx_buf;
288 unsigned fill_level, index;
289 int i, space, rc = 0;
290
291 /* Calculate current fill level. Do this outside the lock,
292 * because most of the time we'll end up not wanting to do the
293 * fill anyway.
294 */
295 fill_level = (rx_queue->added_count - rx_queue->removed_count);
3ffeabdd 296 EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
8ceee660
BH
297
298 /* Don't fill if we don't need to */
299 if (fill_level >= rx_queue->fast_fill_trigger)
300 return 0;
301
302 /* Record minimum fill level */
b3475645 303 if (unlikely(fill_level < rx_queue->min_fill)) {
8ceee660
BH
304 if (fill_level)
305 rx_queue->min_fill = fill_level;
b3475645 306 }
8ceee660
BH
307
308 /* Acquire RX add lock. If this lock is contended, then a fast
309 * fill must already be in progress (e.g. in the refill
310 * tasklet), so we don't need to do anything
311 */
312 if (!spin_trylock_bh(&rx_queue->add_lock))
313 return -1;
314
315 retry:
316 /* Recalculate current fill level now that we have the lock */
317 fill_level = (rx_queue->added_count - rx_queue->removed_count);
3ffeabdd 318 EFX_BUG_ON_PARANOID(fill_level > EFX_RXQ_SIZE);
8ceee660
BH
319 space = rx_queue->fast_fill_limit - fill_level;
320 if (space < EFX_RX_BATCH)
321 goto out_unlock;
322
323 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filling descriptor ring from"
324 " level %d to level %d using %s allocation\n",
325 rx_queue->queue, fill_level, rx_queue->fast_fill_limit,
326 rx_queue->channel->rx_alloc_push_pages ? "page" : "skb");
327
328 do {
329 for (i = 0; i < EFX_RX_BATCH; ++i) {
3ffeabdd 330 index = rx_queue->added_count & EFX_RXQ_MASK;
8ceee660
BH
331 rx_buf = efx_rx_buffer(rx_queue, index);
332 rc = efx_init_rx_buffer(rx_queue, rx_buf);
333 if (unlikely(rc))
334 goto out;
335 ++rx_queue->added_count;
336 }
337 } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH);
338
339 EFX_TRACE(rx_queue->efx, "RX queue %d fast-filled descriptor ring "
340 "to level %d\n", rx_queue->queue,
341 rx_queue->added_count - rx_queue->removed_count);
342
343 out:
344 /* Send write pointer to card. */
345 falcon_notify_rx_desc(rx_queue);
346
347 /* If the fast fill is running inside from the refill tasklet, then
348 * for SMP systems it may be running on a different CPU to
349 * RX event processing, which means that the fill level may now be
350 * out of date. */
351 if (unlikely(retry && (rc == 0)))
352 goto retry;
353
354 out_unlock:
355 spin_unlock_bh(&rx_queue->add_lock);
356
357 return rc;
358}
359
360/**
361 * efx_fast_push_rx_descriptors - push new RX descriptors quickly
362 * @rx_queue: RX descriptor queue
363 *
364 * This will aim to fill the RX descriptor queue up to
365 * @rx_queue->@fast_fill_limit. If there is insufficient memory to do so,
366 * it will schedule a work item to immediately continue the fast fill
367 */
368void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue)
369{
370 int rc;
371
372 rc = __efx_fast_push_rx_descriptors(rx_queue, 0);
373 if (unlikely(rc)) {
374 /* Schedule the work item to run immediately. The hope is
375 * that work is immediately pending to free some memory
376 * (e.g. an RX event or TX completion)
377 */
378 efx_schedule_slow_fill(rx_queue, 0);
379 }
380}
381
382void efx_rx_work(struct work_struct *data)
383{
384 struct efx_rx_queue *rx_queue;
385 int rc;
386
387 rx_queue = container_of(data, struct efx_rx_queue, work.work);
388
389 if (unlikely(!rx_queue->channel->enabled))
390 return;
391
392 EFX_TRACE(rx_queue->efx, "RX queue %d worker thread executing on CPU "
393 "%d\n", rx_queue->queue, raw_smp_processor_id());
394
395 ++rx_queue->slow_fill_count;
396 /* Push new RX descriptors, allowing at least 1 jiffy for
397 * the kernel to free some more memory. */
398 rc = __efx_fast_push_rx_descriptors(rx_queue, 1);
399 if (rc)
400 efx_schedule_slow_fill(rx_queue, 1);
401}
402
4d566063
BH
403static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
404 struct efx_rx_buffer *rx_buf,
405 int len, bool *discard,
406 bool *leak_packet)
8ceee660
BH
407{
408 struct efx_nic *efx = rx_queue->efx;
409 unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
410
411 if (likely(len <= max_len))
412 return;
413
414 /* The packet must be discarded, but this is only a fatal error
415 * if the caller indicated it was
416 */
dc8cfa55 417 *discard = true;
8ceee660
BH
418
419 if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) {
420 EFX_ERR_RL(efx, " RX queue %d seriously overlength "
421 "RX event (0x%x > 0x%x+0x%x). Leaking\n",
422 rx_queue->queue, len, max_len,
423 efx->type->rx_buffer_padding);
424 /* If this buffer was skb-allocated, then the meta
425 * data at the end of the skb will be trashed. So
426 * we have no choice but to leak the fragment.
427 */
428 *leak_packet = (rx_buf->skb != NULL);
429 efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY);
430 } else {
431 EFX_ERR_RL(efx, " RX queue %d overlength RX event "
432 "(0x%x > 0x%x)\n", rx_queue->queue, len, max_len);
433 }
434
435 rx_queue->channel->n_rx_overlength++;
436}
437
438/* Pass a received packet up through the generic LRO stack
439 *
440 * Handles driverlink veto, and passes the fragment up via
441 * the appropriate LRO method
442 */
4d566063 443static void efx_rx_packet_lro(struct efx_channel *channel,
345056af
BH
444 struct efx_rx_buffer *rx_buf,
445 bool checksummed)
8ceee660 446{
da3bc071 447 struct napi_struct *napi = &channel->napi_str;
18e1d2be 448 gro_result_t gro_result;
8ceee660
BH
449
450 /* Pass the skb/page into the LRO engine */
451 if (rx_buf->page) {
1241e951
BH
452 struct page *page = rx_buf->page;
453 struct sk_buff *skb;
8ceee660 454
1241e951
BH
455 EFX_BUG_ON_PARANOID(rx_buf->skb);
456 rx_buf->page = NULL;
457
458 skb = napi_get_frags(napi);
76620aaf 459 if (!skb) {
1241e951
BH
460 put_page(page);
461 return;
76620aaf
HX
462 }
463
1241e951 464 skb_shinfo(skb)->frags[0].page = page;
76620aaf
HX
465 skb_shinfo(skb)->frags[0].page_offset =
466 efx_rx_buf_offset(rx_buf);
467 skb_shinfo(skb)->frags[0].size = rx_buf->len;
468 skb_shinfo(skb)->nr_frags = 1;
469
470 skb->len = rx_buf->len;
471 skb->data_len = rx_buf->len;
472 skb->truesize += rx_buf->len;
345056af
BH
473 skb->ip_summed =
474 checksummed ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
8ceee660 475
18e1d2be 476 gro_result = napi_gro_frags(napi);
8ceee660 477 } else {
1241e951 478 struct sk_buff *skb = rx_buf->skb;
8ceee660 479
1241e951
BH
480 EFX_BUG_ON_PARANOID(!skb);
481 EFX_BUG_ON_PARANOID(!checksummed);
8ceee660 482 rx_buf->skb = NULL;
1241e951
BH
483
484 gro_result = napi_gro_receive(napi, skb);
8ceee660 485 }
18e1d2be
BH
486
487 if (gro_result == GRO_NORMAL) {
488 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
489 } else if (gro_result != GRO_DROP) {
490 channel->rx_alloc_level += RX_ALLOC_FACTOR_LRO;
491 channel->irq_mod_score += 2;
492 }
8ceee660
BH
493}
494
8ceee660 495void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
dc8cfa55 496 unsigned int len, bool checksummed, bool discard)
8ceee660
BH
497{
498 struct efx_nic *efx = rx_queue->efx;
499 struct efx_rx_buffer *rx_buf;
dc8cfa55 500 bool leak_packet = false;
8ceee660
BH
501
502 rx_buf = efx_rx_buffer(rx_queue, index);
503 EFX_BUG_ON_PARANOID(!rx_buf->data);
504 EFX_BUG_ON_PARANOID(rx_buf->skb && rx_buf->page);
505 EFX_BUG_ON_PARANOID(!(rx_buf->skb || rx_buf->page));
506
507 /* This allows the refill path to post another buffer.
508 * EFX_RXD_HEAD_ROOM ensures that the slot we are using
509 * isn't overwritten yet.
510 */
511 rx_queue->removed_count++;
512
513 /* Validate the length encoded in the event vs the descriptor pushed */
514 efx_rx_packet__check_len(rx_queue, rx_buf, len,
515 &discard, &leak_packet);
516
517 EFX_TRACE(efx, "RX queue %d received id %x at %llx+%x %s%s\n",
518 rx_queue->queue, index,
519 (unsigned long long)rx_buf->dma_addr, len,
520 (checksummed ? " [SUMMED]" : ""),
521 (discard ? " [DISCARD]" : ""));
522
523 /* Discard packet, if instructed to do so */
524 if (unlikely(discard)) {
525 if (unlikely(leak_packet))
526 rx_queue->channel->n_skbuff_leaks++;
527 else
528 /* We haven't called efx_unmap_rx_buffer yet,
529 * so fini the entire rx_buffer here */
530 efx_fini_rx_buffer(rx_queue, rx_buf);
531 return;
532 }
533
534 /* Release card resources - assumes all RX buffers consumed in-order
535 * per RX queue
536 */
537 efx_unmap_rx_buffer(efx, rx_buf);
538
539 /* Prefetch nice and early so data will (hopefully) be in cache by
540 * the time we look at it.
541 */
542 prefetch(rx_buf->data);
543
544 /* Pipeline receives so that we give time for packet headers to be
545 * prefetched into cache.
546 */
547 rx_buf->len = len;
548 if (rx_queue->channel->rx_pkt)
549 __efx_rx_packet(rx_queue->channel,
550 rx_queue->channel->rx_pkt,
551 rx_queue->channel->rx_pkt_csummed);
552 rx_queue->channel->rx_pkt = rx_buf;
553 rx_queue->channel->rx_pkt_csummed = checksummed;
554}
555
556/* Handle a received packet. Second half: Touches packet payload. */
557void __efx_rx_packet(struct efx_channel *channel,
dc8cfa55 558 struct efx_rx_buffer *rx_buf, bool checksummed)
8ceee660
BH
559{
560 struct efx_nic *efx = channel->efx;
561 struct sk_buff *skb;
8ceee660 562
3273c2e8
BH
563 /* If we're in loopback test, then pass the packet directly to the
564 * loopback layer, and free the rx_buf here
565 */
566 if (unlikely(efx->loopback_selftest)) {
567 efx_loopback_rx_packet(efx, rx_buf->data, rx_buf->len);
568 efx_free_rx_buffer(efx, rx_buf);
d96d7dc9 569 return;
3273c2e8
BH
570 }
571
8ceee660
BH
572 if (rx_buf->skb) {
573 prefetch(skb_shinfo(rx_buf->skb));
574
575 skb_put(rx_buf->skb, rx_buf->len);
576
577 /* Move past the ethernet header. rx_buf->data still points
578 * at the ethernet header */
579 rx_buf->skb->protocol = eth_type_trans(rx_buf->skb,
580 efx->net_dev);
581 }
582
da3bc071 583 if (likely(checksummed || rx_buf->page)) {
345056af 584 efx_rx_packet_lro(channel, rx_buf, checksummed);
d96d7dc9 585 return;
8ceee660
BH
586 }
587
da3bc071
HX
588 /* We now own the SKB */
589 skb = rx_buf->skb;
590 rx_buf->skb = NULL;
8ceee660
BH
591 EFX_BUG_ON_PARANOID(!skb);
592
593 /* Set the SKB flags */
da3bc071 594 skb->ip_summed = CHECKSUM_NONE;
8ceee660 595
0c8dfc83
DM
596 skb_record_rx_queue(skb, channel->channel);
597
8ceee660
BH
598 /* Pass the packet up */
599 netif_receive_skb(skb);
600
601 /* Update allocation strategy method */
602 channel->rx_alloc_level += RX_ALLOC_FACTOR_SKB;
8ceee660
BH
603}
604
605void efx_rx_strategy(struct efx_channel *channel)
606{
607 enum efx_rx_alloc_method method = rx_alloc_method;
608
609 /* Only makes sense to use page based allocation if LRO is enabled */
da3bc071 610 if (!(channel->efx->net_dev->features & NETIF_F_GRO)) {
8ceee660
BH
611 method = RX_ALLOC_METHOD_SKB;
612 } else if (method == RX_ALLOC_METHOD_AUTO) {
613 /* Constrain the rx_alloc_level */
614 if (channel->rx_alloc_level < 0)
615 channel->rx_alloc_level = 0;
616 else if (channel->rx_alloc_level > RX_ALLOC_LEVEL_MAX)
617 channel->rx_alloc_level = RX_ALLOC_LEVEL_MAX;
618
619 /* Decide on the allocation method */
620 method = ((channel->rx_alloc_level > RX_ALLOC_LEVEL_LRO) ?
621 RX_ALLOC_METHOD_PAGE : RX_ALLOC_METHOD_SKB);
622 }
623
624 /* Push the option */
625 channel->rx_alloc_push_pages = (method == RX_ALLOC_METHOD_PAGE);
626}
627
628int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
629{
630 struct efx_nic *efx = rx_queue->efx;
631 unsigned int rxq_size;
632 int rc;
633
634 EFX_LOG(efx, "creating RX queue %d\n", rx_queue->queue);
635
636 /* Allocate RX buffers */
3ffeabdd 637 rxq_size = EFX_RXQ_SIZE * sizeof(*rx_queue->buffer);
8ceee660 638 rx_queue->buffer = kzalloc(rxq_size, GFP_KERNEL);
8831da7b
BH
639 if (!rx_queue->buffer)
640 return -ENOMEM;
8ceee660
BH
641
642 rc = falcon_probe_rx(rx_queue);
8831da7b
BH
643 if (rc) {
644 kfree(rx_queue->buffer);
645 rx_queue->buffer = NULL;
646 }
8ceee660
BH
647 return rc;
648}
649
bc3c90a2 650void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
8ceee660 651{
8ceee660
BH
652 unsigned int max_fill, trigger, limit;
653
654 EFX_LOG(rx_queue->efx, "initialising RX queue %d\n", rx_queue->queue);
655
656 /* Initialise ptr fields */
657 rx_queue->added_count = 0;
658 rx_queue->notified_count = 0;
659 rx_queue->removed_count = 0;
660 rx_queue->min_fill = -1U;
661 rx_queue->min_overfill = -1U;
662
663 /* Initialise limit fields */
3ffeabdd 664 max_fill = EFX_RXQ_SIZE - EFX_RXD_HEAD_ROOM;
8ceee660
BH
665 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
666 limit = max_fill * min(rx_refill_limit, 100U) / 100U;
667
668 rx_queue->max_fill = max_fill;
669 rx_queue->fast_fill_trigger = trigger;
670 rx_queue->fast_fill_limit = limit;
671
672 /* Set up RX descriptor ring */
bc3c90a2 673 falcon_init_rx(rx_queue);
8ceee660
BH
674}
675
676void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
677{
678 int i;
679 struct efx_rx_buffer *rx_buf;
680
681 EFX_LOG(rx_queue->efx, "shutting down RX queue %d\n", rx_queue->queue);
682
683 falcon_fini_rx(rx_queue);
684
685 /* Release RX buffers NB start at index 0 not current HW ptr */
686 if (rx_queue->buffer) {
3ffeabdd 687 for (i = 0; i <= EFX_RXQ_MASK; i++) {
8ceee660
BH
688 rx_buf = efx_rx_buffer(rx_queue, i);
689 efx_fini_rx_buffer(rx_queue, rx_buf);
690 }
691 }
692
693 /* For a page that is part-way through splitting into RX buffers */
694 if (rx_queue->buf_page != NULL) {
695 pci_unmap_page(rx_queue->efx->pci_dev, rx_queue->buf_dma_addr,
55668611
BH
696 efx_rx_buf_size(rx_queue->efx),
697 PCI_DMA_FROMDEVICE);
8ceee660
BH
698 __free_pages(rx_queue->buf_page,
699 rx_queue->efx->rx_buffer_order);
700 rx_queue->buf_page = NULL;
701 }
702}
703
704void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
705{
706 EFX_LOG(rx_queue->efx, "destroying RX queue %d\n", rx_queue->queue);
707
708 falcon_remove_rx(rx_queue);
709
710 kfree(rx_queue->buffer);
711 rx_queue->buffer = NULL;
8ceee660
BH
712}
713
8ceee660
BH
714
715module_param(rx_alloc_method, int, 0644);
716MODULE_PARM_DESC(rx_alloc_method, "Allocation method used for RX buffers");
717
718module_param(rx_refill_threshold, uint, 0444);
719MODULE_PARM_DESC(rx_refill_threshold,
720 "RX descriptor ring fast/slow fill threshold (%)");
721