1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2018 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 "net_driver.h"
12 #include <linux/module.h>
13 #include <linux/iommu.h>
17 #include "rx_common.h"
19 /* This is the percentage fill level below which new RX descriptors
20 * will be added to the RX descriptor ring.
22 static unsigned int rx_refill_threshold
;
23 module_param(rx_refill_threshold
, uint
, 0444);
24 MODULE_PARM_DESC(rx_refill_threshold
,
25 "RX descriptor ring refill threshold (%)");
27 /* RX maximum head room required.
29 * This must be at least 1 to prevent overflow, plus one packet-worth
30 * to allow pipelined receives.
32 #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
34 /* Check the RX page recycle ring for a page that can be reused. */
35 static struct page
*efx_reuse_page(struct efx_rx_queue
*rx_queue
)
37 struct efx_nic
*efx
= rx_queue
->efx
;
38 struct efx_rx_page_state
*state
;
42 if (unlikely(!rx_queue
->page_ring
))
44 index
= rx_queue
->page_remove
& rx_queue
->page_ptr_mask
;
45 page
= rx_queue
->page_ring
[index
];
49 rx_queue
->page_ring
[index
] = NULL
;
50 /* page_remove cannot exceed page_add. */
51 if (rx_queue
->page_remove
!= rx_queue
->page_add
)
52 ++rx_queue
->page_remove
;
54 /* If page_count is 1 then we hold the only reference to this page. */
55 if (page_count(page
) == 1) {
56 ++rx_queue
->page_recycle_count
;
59 state
= page_address(page
);
60 dma_unmap_page(&efx
->pci_dev
->dev
, state
->dma_addr
,
61 PAGE_SIZE
<< efx
->rx_buffer_order
,
64 ++rx_queue
->page_recycle_failed
;
70 /* Attempt to recycle the page if there is an RX recycle ring; the page can
71 * only be added if this is the final RX buffer, to prevent pages being used in
72 * the descriptor ring and appearing in the recycle ring simultaneously.
74 static void efx_recycle_rx_page(struct efx_channel
*channel
,
75 struct efx_rx_buffer
*rx_buf
)
77 struct efx_rx_queue
*rx_queue
= efx_channel_get_rx_queue(channel
);
78 struct efx_nic
*efx
= rx_queue
->efx
;
79 struct page
*page
= rx_buf
->page
;
82 /* Only recycle the page after processing the final buffer. */
83 if (!(rx_buf
->flags
& EFX_RX_BUF_LAST_IN_PAGE
))
86 index
= rx_queue
->page_add
& rx_queue
->page_ptr_mask
;
87 if (rx_queue
->page_ring
[index
] == NULL
) {
88 unsigned int read_index
= rx_queue
->page_remove
&
89 rx_queue
->page_ptr_mask
;
91 /* The next slot in the recycle ring is available, but
92 * increment page_remove if the read pointer currently
95 if (read_index
== index
)
96 ++rx_queue
->page_remove
;
97 rx_queue
->page_ring
[index
] = page
;
101 ++rx_queue
->page_recycle_full
;
102 efx_unmap_rx_buffer(efx
, rx_buf
);
103 put_page(rx_buf
->page
);
106 /* Recycle the pages that are used by buffers that have just been received. */
107 void efx_recycle_rx_pages(struct efx_channel
*channel
,
108 struct efx_rx_buffer
*rx_buf
,
109 unsigned int n_frags
)
111 struct efx_rx_queue
*rx_queue
= efx_channel_get_rx_queue(channel
);
113 if (unlikely(!rx_queue
->page_ring
))
117 efx_recycle_rx_page(channel
, rx_buf
);
118 rx_buf
= efx_rx_buf_next(rx_queue
, rx_buf
);
122 void efx_discard_rx_packet(struct efx_channel
*channel
,
123 struct efx_rx_buffer
*rx_buf
,
124 unsigned int n_frags
)
126 struct efx_rx_queue
*rx_queue
= efx_channel_get_rx_queue(channel
);
128 efx_recycle_rx_pages(channel
, rx_buf
, n_frags
);
130 efx_free_rx_buffers(rx_queue
, rx_buf
, n_frags
);
133 static void efx_init_rx_recycle_ring(struct efx_rx_queue
*rx_queue
)
135 unsigned int bufs_in_recycle_ring
, page_ring_size
;
136 struct efx_nic
*efx
= rx_queue
->efx
;
138 bufs_in_recycle_ring
= efx_rx_recycle_ring_size(efx
);
139 page_ring_size
= roundup_pow_of_two(bufs_in_recycle_ring
/
140 efx
->rx_bufs_per_page
);
141 rx_queue
->page_ring
= kcalloc(page_ring_size
,
142 sizeof(*rx_queue
->page_ring
), GFP_KERNEL
);
143 if (!rx_queue
->page_ring
)
144 rx_queue
->page_ptr_mask
= 0;
146 rx_queue
->page_ptr_mask
= page_ring_size
- 1;
149 static void efx_fini_rx_recycle_ring(struct efx_rx_queue
*rx_queue
)
151 struct efx_nic
*efx
= rx_queue
->efx
;
154 if (unlikely(!rx_queue
->page_ring
))
157 /* Unmap and release the pages in the recycle ring. Remove the ring. */
158 for (i
= 0; i
<= rx_queue
->page_ptr_mask
; i
++) {
159 struct page
*page
= rx_queue
->page_ring
[i
];
160 struct efx_rx_page_state
*state
;
165 state
= page_address(page
);
166 dma_unmap_page(&efx
->pci_dev
->dev
, state
->dma_addr
,
167 PAGE_SIZE
<< efx
->rx_buffer_order
,
171 kfree(rx_queue
->page_ring
);
172 rx_queue
->page_ring
= NULL
;
175 static void efx_fini_rx_buffer(struct efx_rx_queue
*rx_queue
,
176 struct efx_rx_buffer
*rx_buf
)
178 /* Release the page reference we hold for the buffer. */
180 put_page(rx_buf
->page
);
182 /* If this is the last buffer in a page, unmap and free it. */
183 if (rx_buf
->flags
& EFX_RX_BUF_LAST_IN_PAGE
) {
184 efx_unmap_rx_buffer(rx_queue
->efx
, rx_buf
);
185 efx_free_rx_buffers(rx_queue
, rx_buf
, 1);
190 int efx_probe_rx_queue(struct efx_rx_queue
*rx_queue
)
192 struct efx_nic
*efx
= rx_queue
->efx
;
193 unsigned int entries
;
196 /* Create the smallest power-of-two aligned ring */
197 entries
= max(roundup_pow_of_two(efx
->rxq_entries
), EFX_MIN_DMAQ_SIZE
);
198 EFX_WARN_ON_PARANOID(entries
> EFX_MAX_DMAQ_SIZE
);
199 rx_queue
->ptr_mask
= entries
- 1;
201 netif_dbg(efx
, probe
, efx
->net_dev
,
202 "creating RX queue %d size %#x mask %#x\n",
203 efx_rx_queue_index(rx_queue
), efx
->rxq_entries
,
206 /* Allocate RX buffers */
207 rx_queue
->buffer
= kcalloc(entries
, sizeof(*rx_queue
->buffer
),
209 if (!rx_queue
->buffer
)
212 rc
= efx_nic_probe_rx(rx_queue
);
214 kfree(rx_queue
->buffer
);
215 rx_queue
->buffer
= NULL
;
221 void efx_init_rx_queue(struct efx_rx_queue
*rx_queue
)
223 unsigned int max_fill
, trigger
, max_trigger
;
224 struct efx_nic
*efx
= rx_queue
->efx
;
227 netif_dbg(rx_queue
->efx
, drv
, rx_queue
->efx
->net_dev
,
228 "initialising RX queue %d\n", efx_rx_queue_index(rx_queue
));
230 /* Initialise ptr fields */
231 rx_queue
->added_count
= 0;
232 rx_queue
->notified_count
= 0;
233 rx_queue
->granted_count
= 0;
234 rx_queue
->removed_count
= 0;
235 rx_queue
->min_fill
= -1U;
236 efx_init_rx_recycle_ring(rx_queue
);
238 rx_queue
->page_remove
= 0;
239 rx_queue
->page_add
= rx_queue
->page_ptr_mask
+ 1;
240 rx_queue
->page_recycle_count
= 0;
241 rx_queue
->page_recycle_failed
= 0;
242 rx_queue
->page_recycle_full
= 0;
244 rx_queue
->old_rx_packets
= rx_queue
->rx_packets
;
245 rx_queue
->old_rx_bytes
= rx_queue
->rx_bytes
;
247 /* Initialise limit fields */
248 max_fill
= efx
->rxq_entries
- EFX_RXD_HEAD_ROOM
;
250 max_fill
- efx
->rx_pages_per_batch
* efx
->rx_bufs_per_page
;
251 if (rx_refill_threshold
!= 0) {
252 trigger
= max_fill
* min(rx_refill_threshold
, 100U) / 100U;
253 if (trigger
> max_trigger
)
254 trigger
= max_trigger
;
256 trigger
= max_trigger
;
259 rx_queue
->max_fill
= max_fill
;
260 rx_queue
->fast_fill_trigger
= trigger
;
261 rx_queue
->refill_enabled
= true;
263 /* Initialise XDP queue information */
264 rc
= xdp_rxq_info_reg(&rx_queue
->xdp_rxq_info
, efx
->net_dev
,
265 rx_queue
->core_index
, 0);
268 netif_err(efx
, rx_err
, efx
->net_dev
,
269 "Failure to initialise XDP queue information rc=%d\n",
271 efx
->xdp_rxq_info_failed
= true;
273 rx_queue
->xdp_rxq_info_valid
= true;
276 /* Set up RX descriptor ring */
277 efx_nic_init_rx(rx_queue
);
280 void efx_fini_rx_queue(struct efx_rx_queue
*rx_queue
)
282 struct efx_rx_buffer
*rx_buf
;
285 netif_dbg(rx_queue
->efx
, drv
, rx_queue
->efx
->net_dev
,
286 "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue
));
288 del_timer_sync(&rx_queue
->slow_fill
);
289 if (rx_queue
->grant_credits
)
290 flush_work(&rx_queue
->grant_work
);
292 /* Release RX buffers from the current read ptr to the write ptr */
293 if (rx_queue
->buffer
) {
294 for (i
= rx_queue
->removed_count
; i
< rx_queue
->added_count
;
296 unsigned int index
= i
& rx_queue
->ptr_mask
;
298 rx_buf
= efx_rx_buffer(rx_queue
, index
);
299 efx_fini_rx_buffer(rx_queue
, rx_buf
);
303 efx_fini_rx_recycle_ring(rx_queue
);
305 if (rx_queue
->xdp_rxq_info_valid
)
306 xdp_rxq_info_unreg(&rx_queue
->xdp_rxq_info
);
308 rx_queue
->xdp_rxq_info_valid
= false;
311 void efx_remove_rx_queue(struct efx_rx_queue
*rx_queue
)
313 netif_dbg(rx_queue
->efx
, drv
, rx_queue
->efx
->net_dev
,
314 "destroying RX queue %d\n", efx_rx_queue_index(rx_queue
));
316 efx_nic_remove_rx(rx_queue
);
318 kfree(rx_queue
->buffer
);
319 rx_queue
->buffer
= NULL
;
322 /* Unmap a DMA-mapped page. This function is only called for the final RX
325 void efx_unmap_rx_buffer(struct efx_nic
*efx
,
326 struct efx_rx_buffer
*rx_buf
)
328 struct page
*page
= rx_buf
->page
;
331 struct efx_rx_page_state
*state
= page_address(page
);
333 dma_unmap_page(&efx
->pci_dev
->dev
,
335 PAGE_SIZE
<< efx
->rx_buffer_order
,
340 void efx_free_rx_buffers(struct efx_rx_queue
*rx_queue
,
341 struct efx_rx_buffer
*rx_buf
,
342 unsigned int num_bufs
)
346 put_page(rx_buf
->page
);
349 rx_buf
= efx_rx_buf_next(rx_queue
, rx_buf
);
350 } while (--num_bufs
);
353 void efx_rx_slow_fill(struct timer_list
*t
)
355 struct efx_rx_queue
*rx_queue
= from_timer(rx_queue
, t
, slow_fill
);
357 /* Post an event to cause NAPI to run and refill the queue */
358 efx_nic_generate_fill_event(rx_queue
);
359 ++rx_queue
->slow_fill_count
;
362 void efx_schedule_slow_fill(struct efx_rx_queue
*rx_queue
)
364 mod_timer(&rx_queue
->slow_fill
, jiffies
+ msecs_to_jiffies(10));
367 /* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
369 * @rx_queue: Efx RX queue
371 * This allocates a batch of pages, maps them for DMA, and populates
372 * struct efx_rx_buffers for each one. Return a negative error code or
373 * 0 on success. If a single page can be used for multiple buffers,
374 * then the page will either be inserted fully, or not at all.
376 static int efx_init_rx_buffers(struct efx_rx_queue
*rx_queue
, bool atomic
)
378 unsigned int page_offset
, index
, count
;
379 struct efx_nic
*efx
= rx_queue
->efx
;
380 struct efx_rx_page_state
*state
;
381 struct efx_rx_buffer
*rx_buf
;
387 page
= efx_reuse_page(rx_queue
);
389 page
= alloc_pages(__GFP_COMP
|
390 (atomic
? GFP_ATOMIC
: GFP_KERNEL
),
391 efx
->rx_buffer_order
);
392 if (unlikely(page
== NULL
))
395 dma_map_page(&efx
->pci_dev
->dev
, page
, 0,
396 PAGE_SIZE
<< efx
->rx_buffer_order
,
398 if (unlikely(dma_mapping_error(&efx
->pci_dev
->dev
,
400 __free_pages(page
, efx
->rx_buffer_order
);
403 state
= page_address(page
);
404 state
->dma_addr
= dma_addr
;
406 state
= page_address(page
);
407 dma_addr
= state
->dma_addr
;
410 dma_addr
+= sizeof(struct efx_rx_page_state
);
411 page_offset
= sizeof(struct efx_rx_page_state
);
414 index
= rx_queue
->added_count
& rx_queue
->ptr_mask
;
415 rx_buf
= efx_rx_buffer(rx_queue
, index
);
416 rx_buf
->dma_addr
= dma_addr
+ efx
->rx_ip_align
+
419 rx_buf
->page_offset
= page_offset
+ efx
->rx_ip_align
+
421 rx_buf
->len
= efx
->rx_dma_len
;
423 ++rx_queue
->added_count
;
425 dma_addr
+= efx
->rx_page_buf_step
;
426 page_offset
+= efx
->rx_page_buf_step
;
427 } while (page_offset
+ efx
->rx_page_buf_step
<= PAGE_SIZE
);
429 rx_buf
->flags
= EFX_RX_BUF_LAST_IN_PAGE
;
430 } while (++count
< efx
->rx_pages_per_batch
);
435 void efx_rx_config_page_split(struct efx_nic
*efx
)
437 efx
->rx_page_buf_step
= ALIGN(efx
->rx_dma_len
+ efx
->rx_ip_align
+
438 EFX_XDP_HEADROOM
+ EFX_XDP_TAILROOM
,
439 EFX_RX_BUF_ALIGNMENT
);
440 efx
->rx_bufs_per_page
= efx
->rx_buffer_order
? 1 :
441 ((PAGE_SIZE
- sizeof(struct efx_rx_page_state
)) /
442 efx
->rx_page_buf_step
);
443 efx
->rx_buffer_truesize
= (PAGE_SIZE
<< efx
->rx_buffer_order
) /
444 efx
->rx_bufs_per_page
;
445 efx
->rx_pages_per_batch
= DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH
,
446 efx
->rx_bufs_per_page
);
449 /* efx_fast_push_rx_descriptors - push new RX descriptors quickly
450 * @rx_queue: RX descriptor queue
452 * This will aim to fill the RX descriptor queue up to
453 * @rx_queue->@max_fill. If there is insufficient atomic
454 * memory to do so, a slow fill will be scheduled.
456 * The caller must provide serialisation (none is used here). In practise,
457 * this means this function must run from the NAPI handler, or be called
458 * when NAPI is disabled.
460 void efx_fast_push_rx_descriptors(struct efx_rx_queue
*rx_queue
, bool atomic
)
462 struct efx_nic
*efx
= rx_queue
->efx
;
463 unsigned int fill_level
, batch_size
;
466 if (!rx_queue
->refill_enabled
)
469 /* Calculate current fill level, and exit if we don't need to fill */
470 fill_level
= (rx_queue
->added_count
- rx_queue
->removed_count
);
471 EFX_WARN_ON_ONCE_PARANOID(fill_level
> rx_queue
->efx
->rxq_entries
);
472 if (fill_level
>= rx_queue
->fast_fill_trigger
)
475 /* Record minimum fill level */
476 if (unlikely(fill_level
< rx_queue
->min_fill
)) {
478 rx_queue
->min_fill
= fill_level
;
481 batch_size
= efx
->rx_pages_per_batch
* efx
->rx_bufs_per_page
;
482 space
= rx_queue
->max_fill
- fill_level
;
483 EFX_WARN_ON_ONCE_PARANOID(space
< batch_size
);
485 netif_vdbg(rx_queue
->efx
, rx_status
, rx_queue
->efx
->net_dev
,
486 "RX queue %d fast-filling descriptor ring from"
487 " level %d to level %d\n",
488 efx_rx_queue_index(rx_queue
), fill_level
,
492 rc
= efx_init_rx_buffers(rx_queue
, atomic
);
494 /* Ensure that we don't leave the rx queue empty */
495 efx_schedule_slow_fill(rx_queue
);
498 } while ((space
-= batch_size
) >= batch_size
);
500 netif_vdbg(rx_queue
->efx
, rx_status
, rx_queue
->efx
->net_dev
,
501 "RX queue %d fast-filled descriptor ring "
502 "to level %d\n", efx_rx_queue_index(rx_queue
),
503 rx_queue
->added_count
- rx_queue
->removed_count
);
506 if (rx_queue
->notified_count
!= rx_queue
->added_count
)
507 efx_nic_notify_rx_desc(rx_queue
);
510 /* Pass a received packet up through GRO. GRO can handle pages
511 * regardless of checksum state and skbs with a good checksum.
514 efx_rx_packet_gro(struct efx_channel
*channel
, struct efx_rx_buffer
*rx_buf
,
515 unsigned int n_frags
, u8
*eh
, __wsum csum
)
517 struct napi_struct
*napi
= &channel
->napi_str
;
518 struct efx_nic
*efx
= channel
->efx
;
521 skb
= napi_get_frags(napi
);
522 if (unlikely(!skb
)) {
523 struct efx_rx_queue
*rx_queue
;
525 rx_queue
= efx_channel_get_rx_queue(channel
);
526 efx_free_rx_buffers(rx_queue
, rx_buf
, n_frags
);
530 if (efx
->net_dev
->features
& NETIF_F_RXHASH
&&
531 efx_rx_buf_hash_valid(efx
, eh
))
532 skb_set_hash(skb
, efx_rx_buf_hash(efx
, eh
),
536 skb
->ip_summed
= CHECKSUM_COMPLETE
;
538 skb
->ip_summed
= ((rx_buf
->flags
& EFX_RX_PKT_CSUMMED
) ?
539 CHECKSUM_UNNECESSARY
: CHECKSUM_NONE
);
541 skb
->csum_level
= !!(rx_buf
->flags
& EFX_RX_PKT_CSUM_LEVEL
);
544 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
,
545 rx_buf
->page
, rx_buf
->page_offset
,
548 skb
->len
+= rx_buf
->len
;
549 if (skb_shinfo(skb
)->nr_frags
== n_frags
)
552 rx_buf
= efx_rx_buf_next(&channel
->rx_queue
, rx_buf
);
555 skb
->data_len
= skb
->len
;
556 skb
->truesize
+= n_frags
* efx
->rx_buffer_truesize
;
558 skb_record_rx_queue(skb
, channel
->rx_queue
.core_index
);
560 napi_gro_frags(napi
);
563 struct efx_rss_context_priv
*efx_find_rss_context_entry(struct efx_nic
*efx
,
566 struct ethtool_rxfh_context
*ctx
;
568 WARN_ON(!mutex_is_locked(&efx
->net_dev
->ethtool
->rss_lock
));
570 ctx
= xa_load(&efx
->net_dev
->ethtool
->rss_ctx
, id
);
573 return ethtool_rxfh_context_priv(ctx
);
576 void efx_set_default_rx_indir_table(struct efx_nic
*efx
, u32
*indir
)
580 for (i
= 0; i
< ARRAY_SIZE(efx
->rss_context
.rx_indir_table
); i
++)
581 indir
[i
] = ethtool_rxfh_indir_default(i
, efx
->rss_spread
);
585 * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
586 * @spec: Specification to test
588 * Return: %true if the specification is a non-drop RX filter that
589 * matches a local MAC address I/G bit value of 1 or matches a local
590 * IPv4 or IPv6 address value in the respective multicast address
591 * range. Otherwise %false.
593 bool efx_filter_is_mc_recipient(const struct efx_filter_spec
*spec
)
595 if (!(spec
->flags
& EFX_FILTER_FLAG_RX
) ||
596 spec
->dmaq_id
== EFX_FILTER_RX_DMAQ_ID_DROP
)
599 if (spec
->match_flags
&
600 (EFX_FILTER_MATCH_LOC_MAC
| EFX_FILTER_MATCH_LOC_MAC_IG
) &&
601 is_multicast_ether_addr(spec
->loc_mac
))
604 if ((spec
->match_flags
&
605 (EFX_FILTER_MATCH_ETHER_TYPE
| EFX_FILTER_MATCH_LOC_HOST
)) ==
606 (EFX_FILTER_MATCH_ETHER_TYPE
| EFX_FILTER_MATCH_LOC_HOST
)) {
607 if (spec
->ether_type
== htons(ETH_P_IP
) &&
608 ipv4_is_multicast(spec
->loc_host
[0]))
610 if (spec
->ether_type
== htons(ETH_P_IPV6
) &&
611 ((const u8
*)spec
->loc_host
)[0] == 0xff)
618 bool efx_filter_spec_equal(const struct efx_filter_spec
*left
,
619 const struct efx_filter_spec
*right
)
621 if ((left
->match_flags
^ right
->match_flags
) |
622 ((left
->flags
^ right
->flags
) &
623 (EFX_FILTER_FLAG_RX
| EFX_FILTER_FLAG_TX
)))
626 return memcmp(&left
->vport_id
, &right
->vport_id
,
627 sizeof(struct efx_filter_spec
) -
628 offsetof(struct efx_filter_spec
, vport_id
)) == 0;
631 u32
efx_filter_spec_hash(const struct efx_filter_spec
*spec
)
633 BUILD_BUG_ON(offsetof(struct efx_filter_spec
, vport_id
) & 3);
634 return jhash2((const u32
*)&spec
->vport_id
,
635 (sizeof(struct efx_filter_spec
) -
636 offsetof(struct efx_filter_spec
, vport_id
)) / 4,
640 #ifdef CONFIG_RFS_ACCEL
641 bool efx_rps_check_rule(struct efx_arfs_rule
*rule
, unsigned int filter_idx
,
644 if (rule
->filter_id
== EFX_ARFS_FILTER_ID_PENDING
) {
645 /* ARFS is currently updating this entry, leave it */
648 if (rule
->filter_id
== EFX_ARFS_FILTER_ID_ERROR
) {
649 /* ARFS tried and failed to update this, so it's probably out
650 * of date. Remove the filter and the ARFS rule entry.
652 rule
->filter_id
= EFX_ARFS_FILTER_ID_REMOVING
;
655 } else if (WARN_ON(rule
->filter_id
!= filter_idx
)) { /* can't happen */
656 /* ARFS has moved on, so old filter is not needed. Since we did
657 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
658 * not be removed by efx_rps_hash_del() subsequently.
663 /* Remove it iff ARFS wants to. */
668 struct hlist_head
*efx_rps_hash_bucket(struct efx_nic
*efx
,
669 const struct efx_filter_spec
*spec
)
671 u32 hash
= efx_filter_spec_hash(spec
);
673 lockdep_assert_held(&efx
->rps_hash_lock
);
674 if (!efx
->rps_hash_table
)
676 return &efx
->rps_hash_table
[hash
% EFX_ARFS_HASH_TABLE_SIZE
];
679 struct efx_arfs_rule
*efx_rps_hash_find(struct efx_nic
*efx
,
680 const struct efx_filter_spec
*spec
)
682 struct efx_arfs_rule
*rule
;
683 struct hlist_head
*head
;
684 struct hlist_node
*node
;
686 head
= efx_rps_hash_bucket(efx
, spec
);
689 hlist_for_each(node
, head
) {
690 rule
= container_of(node
, struct efx_arfs_rule
, node
);
691 if (efx_filter_spec_equal(spec
, &rule
->spec
))
697 struct efx_arfs_rule
*efx_rps_hash_add(struct efx_nic
*efx
,
698 const struct efx_filter_spec
*spec
,
701 struct efx_arfs_rule
*rule
;
702 struct hlist_head
*head
;
703 struct hlist_node
*node
;
705 head
= efx_rps_hash_bucket(efx
, spec
);
708 hlist_for_each(node
, head
) {
709 rule
= container_of(node
, struct efx_arfs_rule
, node
);
710 if (efx_filter_spec_equal(spec
, &rule
->spec
)) {
715 rule
= kmalloc(sizeof(*rule
), GFP_ATOMIC
);
718 memcpy(&rule
->spec
, spec
, sizeof(rule
->spec
));
719 hlist_add_head(&rule
->node
, head
);
724 void efx_rps_hash_del(struct efx_nic
*efx
, const struct efx_filter_spec
*spec
)
726 struct efx_arfs_rule
*rule
;
727 struct hlist_head
*head
;
728 struct hlist_node
*node
;
730 head
= efx_rps_hash_bucket(efx
, spec
);
733 hlist_for_each(node
, head
) {
734 rule
= container_of(node
, struct efx_arfs_rule
, node
);
735 if (efx_filter_spec_equal(spec
, &rule
->spec
)) {
736 /* Someone already reused the entry. We know that if
737 * this check doesn't fire (i.e. filter_id == REMOVING)
738 * then the REMOVING mark was put there by our caller,
739 * because caller is holding a lock on filter table and
740 * only holders of that lock set REMOVING.
742 if (rule
->filter_id
!= EFX_ARFS_FILTER_ID_REMOVING
)
749 /* We didn't find it. */
754 int efx_probe_filters(struct efx_nic
*efx
)
758 mutex_lock(&efx
->mac_lock
);
759 rc
= efx
->type
->filter_table_probe(efx
);
763 #ifdef CONFIG_RFS_ACCEL
764 if (efx
->type
->offload_features
& NETIF_F_NTUPLE
) {
765 struct efx_channel
*channel
;
768 efx_for_each_channel(channel
, efx
) {
769 channel
->rps_flow_id
=
770 kcalloc(efx
->type
->max_rx_ip_filters
,
771 sizeof(*channel
->rps_flow_id
),
773 if (!channel
->rps_flow_id
)
777 i
< efx
->type
->max_rx_ip_filters
;
779 channel
->rps_flow_id
[i
] =
781 channel
->rfs_expire_index
= 0;
782 channel
->rfs_filter_count
= 0;
786 efx_for_each_channel(channel
, efx
) {
787 kfree(channel
->rps_flow_id
);
788 channel
->rps_flow_id
= NULL
;
790 efx
->type
->filter_table_remove(efx
);
797 mutex_unlock(&efx
->mac_lock
);
801 void efx_remove_filters(struct efx_nic
*efx
)
803 #ifdef CONFIG_RFS_ACCEL
804 struct efx_channel
*channel
;
806 efx_for_each_channel(channel
, efx
) {
807 cancel_delayed_work_sync(&channel
->filter_work
);
808 kfree(channel
->rps_flow_id
);
809 channel
->rps_flow_id
= NULL
;
812 efx
->type
->filter_table_remove(efx
);
815 #ifdef CONFIG_RFS_ACCEL
817 static void efx_filter_rfs_work(struct work_struct
*data
)
819 struct efx_async_filter_insertion
*req
= container_of(data
, struct efx_async_filter_insertion
,
821 struct efx_nic
*efx
= efx_netdev_priv(req
->net_dev
);
822 struct efx_channel
*channel
= efx_get_channel(efx
, req
->rxq_index
);
823 int slot_idx
= req
- efx
->rps_slot
;
824 struct efx_arfs_rule
*rule
;
828 rc
= efx
->type
->filter_insert(efx
, &req
->spec
, true);
830 /* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
831 rc
%= efx
->type
->max_rx_ip_filters
;
832 if (efx
->rps_hash_table
) {
833 spin_lock_bh(&efx
->rps_hash_lock
);
834 rule
= efx_rps_hash_find(efx
, &req
->spec
);
835 /* The rule might have already gone, if someone else's request
836 * for the same spec was already worked and then expired before
837 * we got around to our work. In that case we have nothing
838 * tying us to an arfs_id, meaning that as soon as the filter
839 * is considered for expiry it will be removed.
843 rule
->filter_id
= EFX_ARFS_FILTER_ID_ERROR
;
845 rule
->filter_id
= rc
;
846 arfs_id
= rule
->arfs_id
;
848 spin_unlock_bh(&efx
->rps_hash_lock
);
851 /* Remember this so we can check whether to expire the filter
854 mutex_lock(&efx
->rps_mutex
);
855 if (channel
->rps_flow_id
[rc
] == RPS_FLOW_ID_INVALID
)
856 channel
->rfs_filter_count
++;
857 channel
->rps_flow_id
[rc
] = req
->flow_id
;
858 mutex_unlock(&efx
->rps_mutex
);
860 if (req
->spec
.ether_type
== htons(ETH_P_IP
))
861 netif_info(efx
, rx_status
, efx
->net_dev
,
862 "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
863 (req
->spec
.ip_proto
== IPPROTO_TCP
) ? "TCP" : "UDP",
864 req
->spec
.rem_host
, ntohs(req
->spec
.rem_port
),
865 req
->spec
.loc_host
, ntohs(req
->spec
.loc_port
),
866 req
->rxq_index
, req
->flow_id
, rc
, arfs_id
);
868 netif_info(efx
, rx_status
, efx
->net_dev
,
869 "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
870 (req
->spec
.ip_proto
== IPPROTO_TCP
) ? "TCP" : "UDP",
871 req
->spec
.rem_host
, ntohs(req
->spec
.rem_port
),
872 req
->spec
.loc_host
, ntohs(req
->spec
.loc_port
),
873 req
->rxq_index
, req
->flow_id
, rc
, arfs_id
);
874 channel
->n_rfs_succeeded
++;
876 if (req
->spec
.ether_type
== htons(ETH_P_IP
))
877 netif_dbg(efx
, rx_status
, efx
->net_dev
,
878 "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
879 (req
->spec
.ip_proto
== IPPROTO_TCP
) ? "TCP" : "UDP",
880 req
->spec
.rem_host
, ntohs(req
->spec
.rem_port
),
881 req
->spec
.loc_host
, ntohs(req
->spec
.loc_port
),
882 req
->rxq_index
, req
->flow_id
, rc
, arfs_id
);
884 netif_dbg(efx
, rx_status
, efx
->net_dev
,
885 "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
886 (req
->spec
.ip_proto
== IPPROTO_TCP
) ? "TCP" : "UDP",
887 req
->spec
.rem_host
, ntohs(req
->spec
.rem_port
),
888 req
->spec
.loc_host
, ntohs(req
->spec
.loc_port
),
889 req
->rxq_index
, req
->flow_id
, rc
, arfs_id
);
890 channel
->n_rfs_failed
++;
891 /* We're overloading the NIC's filter tables, so let's do a
892 * chunk of extra expiry work.
894 __efx_filter_rfs_expire(channel
, min(channel
->rfs_filter_count
,
898 /* Release references */
899 clear_bit(slot_idx
, &efx
->rps_slot_map
);
900 dev_put(req
->net_dev
);
903 int efx_filter_rfs(struct net_device
*net_dev
, const struct sk_buff
*skb
,
904 u16 rxq_index
, u32 flow_id
)
906 struct efx_nic
*efx
= efx_netdev_priv(net_dev
);
907 struct efx_async_filter_insertion
*req
;
908 struct efx_arfs_rule
*rule
;
914 /* find a free slot */
915 for (slot_idx
= 0; slot_idx
< EFX_RPS_MAX_IN_FLIGHT
; slot_idx
++)
916 if (!test_and_set_bit(slot_idx
, &efx
->rps_slot_map
))
918 if (slot_idx
>= EFX_RPS_MAX_IN_FLIGHT
)
921 if (flow_id
== RPS_FLOW_ID_INVALID
) {
926 if (!skb_flow_dissect_flow_keys(skb
, &fk
, 0)) {
927 rc
= -EPROTONOSUPPORT
;
931 if (fk
.basic
.n_proto
!= htons(ETH_P_IP
) && fk
.basic
.n_proto
!= htons(ETH_P_IPV6
)) {
932 rc
= -EPROTONOSUPPORT
;
935 if (fk
.control
.flags
& FLOW_DIS_IS_FRAGMENT
) {
936 rc
= -EPROTONOSUPPORT
;
940 req
= efx
->rps_slot
+ slot_idx
;
941 efx_filter_init_rx(&req
->spec
, EFX_FILTER_PRI_HINT
,
942 efx
->rx_scatter
? EFX_FILTER_FLAG_RX_SCATTER
: 0,
944 req
->spec
.match_flags
=
945 EFX_FILTER_MATCH_ETHER_TYPE
| EFX_FILTER_MATCH_IP_PROTO
|
946 EFX_FILTER_MATCH_LOC_HOST
| EFX_FILTER_MATCH_LOC_PORT
|
947 EFX_FILTER_MATCH_REM_HOST
| EFX_FILTER_MATCH_REM_PORT
;
948 req
->spec
.ether_type
= fk
.basic
.n_proto
;
949 req
->spec
.ip_proto
= fk
.basic
.ip_proto
;
951 if (fk
.basic
.n_proto
== htons(ETH_P_IP
)) {
952 req
->spec
.rem_host
[0] = fk
.addrs
.v4addrs
.src
;
953 req
->spec
.loc_host
[0] = fk
.addrs
.v4addrs
.dst
;
955 memcpy(req
->spec
.rem_host
, &fk
.addrs
.v6addrs
.src
,
956 sizeof(struct in6_addr
));
957 memcpy(req
->spec
.loc_host
, &fk
.addrs
.v6addrs
.dst
,
958 sizeof(struct in6_addr
));
961 req
->spec
.rem_port
= fk
.ports
.src
;
962 req
->spec
.loc_port
= fk
.ports
.dst
;
964 if (efx
->rps_hash_table
) {
965 /* Add it to ARFS hash table */
966 spin_lock(&efx
->rps_hash_lock
);
967 rule
= efx_rps_hash_add(efx
, &req
->spec
, &new);
973 rule
->arfs_id
= efx
->rps_next_id
++ % RPS_NO_FILTER
;
975 /* Skip if existing or pending filter already does the right thing */
976 if (!new && rule
->rxq_index
== rxq_index
&&
977 rule
->filter_id
>= EFX_ARFS_FILTER_ID_PENDING
)
979 rule
->rxq_index
= rxq_index
;
980 rule
->filter_id
= EFX_ARFS_FILTER_ID_PENDING
;
981 spin_unlock(&efx
->rps_hash_lock
);
983 /* Without an ARFS hash table, we just use arfs_id 0 for all
984 * filters. This means if multiple flows hash to the same
985 * flow_id, all but the most recently touched will be eligible
991 /* Queue the request */
992 dev_hold(req
->net_dev
= net_dev
);
993 INIT_WORK(&req
->work
, efx_filter_rfs_work
);
994 req
->rxq_index
= rxq_index
;
995 req
->flow_id
= flow_id
;
996 schedule_work(&req
->work
);
999 spin_unlock(&efx
->rps_hash_lock
);
1001 clear_bit(slot_idx
, &efx
->rps_slot_map
);
1005 bool __efx_filter_rfs_expire(struct efx_channel
*channel
, unsigned int quota
)
1007 bool (*expire_one
)(struct efx_nic
*efx
, u32 flow_id
, unsigned int index
);
1008 struct efx_nic
*efx
= channel
->efx
;
1009 unsigned int index
, size
, start
;
1012 if (!mutex_trylock(&efx
->rps_mutex
))
1014 expire_one
= efx
->type
->filter_rfs_expire_one
;
1015 index
= channel
->rfs_expire_index
;
1017 size
= efx
->type
->max_rx_ip_filters
;
1019 flow_id
= channel
->rps_flow_id
[index
];
1021 if (flow_id
!= RPS_FLOW_ID_INVALID
) {
1023 if (expire_one(efx
, flow_id
, index
)) {
1024 netif_info(efx
, rx_status
, efx
->net_dev
,
1025 "expired filter %d [channel %u flow %u]\n",
1026 index
, channel
->channel
, flow_id
);
1027 channel
->rps_flow_id
[index
] = RPS_FLOW_ID_INVALID
;
1028 channel
->rfs_filter_count
--;
1031 if (++index
== size
)
1033 /* If we were called with a quota that exceeds the total number
1034 * of filters in the table (which shouldn't happen, but could
1035 * if two callers race), ensure that we don't loop forever -
1036 * stop when we've examined every row of the table.
1042 channel
->rfs_expire_index
= index
;
1043 mutex_unlock(&efx
->rps_mutex
);
1047 #endif /* CONFIG_RFS_ACCEL */