xref: /linux/drivers/net/ethernet/sfc/rx_common.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2018 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 "net_driver.h"
12 #include <linux/module.h>
13 #include <linux/iommu.h>
14 #include <net/rps.h>
15 #include "efx.h"
16 #include "nic.h"
17 #include "rx_common.h"
18 
19 /* This is the percentage fill level below which new RX descriptors
20  * will be added to the RX descriptor ring.
21  */
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 (%)");
26 
27 /* RX maximum head room required.
28  *
29  * This must be at least 1 to prevent overflow, plus one packet-worth
30  * to allow pipelined receives.
31  */
32 #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
33 
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)
36 {
37 	struct efx_nic *efx = rx_queue->efx;
38 	struct efx_rx_page_state *state;
39 	unsigned int index;
40 	struct page *page;
41 
42 	if (unlikely(!rx_queue->page_ring))
43 		return NULL;
44 	index = rx_queue->page_remove & rx_queue->page_ptr_mask;
45 	page = rx_queue->page_ring[index];
46 	if (page == NULL)
47 		return NULL;
48 
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;
53 
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;
57 		return page;
58 	} else {
59 		state = page_address(page);
60 		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
61 			       PAGE_SIZE << efx->rx_buffer_order,
62 			       DMA_FROM_DEVICE);
63 		put_page(page);
64 		++rx_queue->page_recycle_failed;
65 	}
66 
67 	return NULL;
68 }
69 
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.
73  */
74 static void efx_recycle_rx_page(struct efx_channel *channel,
75 				struct efx_rx_buffer *rx_buf)
76 {
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;
80 	unsigned int index;
81 
82 	/* Only recycle the page after processing the final buffer. */
83 	if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
84 		return;
85 
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;
90 
91 		/* The next slot in the recycle ring is available, but
92 		 * increment page_remove if the read pointer currently
93 		 * points here.
94 		 */
95 		if (read_index == index)
96 			++rx_queue->page_remove;
97 		rx_queue->page_ring[index] = page;
98 		++rx_queue->page_add;
99 		return;
100 	}
101 	++rx_queue->page_recycle_full;
102 	efx_unmap_rx_buffer(efx, rx_buf);
103 	put_page(rx_buf->page);
104 }
105 
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)
110 {
111 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
112 
113 	if (unlikely(!rx_queue->page_ring))
114 		return;
115 
116 	do {
117 		efx_recycle_rx_page(channel, rx_buf);
118 		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
119 	} while (--n_frags);
120 }
121 
122 void efx_discard_rx_packet(struct efx_channel *channel,
123 			   struct efx_rx_buffer *rx_buf,
124 			   unsigned int n_frags)
125 {
126 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
127 
128 	efx_recycle_rx_pages(channel, rx_buf, n_frags);
129 
130 	efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
131 }
132 
133 static void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
134 {
135 	unsigned int bufs_in_recycle_ring, page_ring_size;
136 	struct efx_nic *efx = rx_queue->efx;
137 
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 = kzalloc_objs(*rx_queue->page_ring, page_ring_size,
142 				           GFP_KERNEL);
143 	if (!rx_queue->page_ring)
144 		rx_queue->page_ptr_mask = 0;
145 	else
146 		rx_queue->page_ptr_mask = page_ring_size - 1;
147 }
148 
149 static void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
150 {
151 	struct efx_nic *efx = rx_queue->efx;
152 	int i;
153 
154 	if (unlikely(!rx_queue->page_ring))
155 		return;
156 
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;
161 
162 		if (page == NULL)
163 			continue;
164 
165 		state = page_address(page);
166 		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
167 			       PAGE_SIZE << efx->rx_buffer_order,
168 			       DMA_FROM_DEVICE);
169 		put_page(page);
170 	}
171 	kfree(rx_queue->page_ring);
172 	rx_queue->page_ring = NULL;
173 }
174 
175 static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
176 			       struct efx_rx_buffer *rx_buf)
177 {
178 	/* Release the page reference we hold for the buffer. */
179 	if (rx_buf->page)
180 		put_page(rx_buf->page);
181 
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);
186 	}
187 	rx_buf->page = NULL;
188 }
189 
190 int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
191 {
192 	struct efx_nic *efx = rx_queue->efx;
193 	unsigned int entries;
194 	int rc;
195 
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;
200 
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,
204 		  rx_queue->ptr_mask);
205 
206 	/* Allocate RX buffers */
207 	rx_queue->buffer = kzalloc_objs(*rx_queue->buffer, entries);
208 	if (!rx_queue->buffer)
209 		return -ENOMEM;
210 
211 	rc = efx_nic_probe_rx(rx_queue);
212 	if (rc) {
213 		kfree(rx_queue->buffer);
214 		rx_queue->buffer = NULL;
215 	}
216 
217 	return rc;
218 }
219 
220 void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
221 {
222 	unsigned int max_fill, trigger, max_trigger;
223 	struct efx_nic *efx = rx_queue->efx;
224 	int rc = 0;
225 
226 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
227 		  "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
228 
229 	/* Initialise ptr fields */
230 	rx_queue->added_count = 0;
231 	rx_queue->notified_count = 0;
232 	rx_queue->granted_count = 0;
233 	rx_queue->removed_count = 0;
234 	rx_queue->min_fill = -1U;
235 	efx_init_rx_recycle_ring(rx_queue);
236 
237 	rx_queue->page_remove = 0;
238 	rx_queue->page_add = rx_queue->page_ptr_mask + 1;
239 	rx_queue->page_recycle_count = 0;
240 	rx_queue->page_recycle_failed = 0;
241 	rx_queue->page_recycle_full = 0;
242 
243 	rx_queue->old_rx_packets = rx_queue->rx_packets;
244 	rx_queue->old_rx_bytes = rx_queue->rx_bytes;
245 
246 	/* Initialise limit fields */
247 	max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
248 	max_trigger =
249 		max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
250 	if (rx_refill_threshold != 0) {
251 		trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
252 		if (trigger > max_trigger)
253 			trigger = max_trigger;
254 	} else {
255 		trigger = max_trigger;
256 	}
257 
258 	rx_queue->max_fill = max_fill;
259 	rx_queue->fast_fill_trigger = trigger;
260 	rx_queue->refill_enabled = true;
261 
262 	/* Initialise XDP queue information */
263 	rc = xdp_rxq_info_reg(&rx_queue->xdp_rxq_info, efx->net_dev,
264 			      rx_queue->core_index, 0);
265 
266 	if (rc) {
267 		netif_err(efx, rx_err, efx->net_dev,
268 			  "Failure to initialise XDP queue information rc=%d\n",
269 			  rc);
270 		efx->xdp_rxq_info_failed = true;
271 	}
272 
273 	/* Set up RX descriptor ring */
274 	efx_nic_init_rx(rx_queue);
275 }
276 
277 void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
278 {
279 	struct efx_rx_buffer *rx_buf;
280 	int i;
281 
282 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
283 		  "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
284 
285 	timer_delete_sync(&rx_queue->slow_fill);
286 	if (rx_queue->grant_credits)
287 		flush_work(&rx_queue->grant_work);
288 
289 	/* Release RX buffers from the current read ptr to the write ptr */
290 	if (rx_queue->buffer) {
291 		for (i = rx_queue->removed_count; i < rx_queue->added_count;
292 		     i++) {
293 			unsigned int index = i & rx_queue->ptr_mask;
294 
295 			rx_buf = efx_rx_buffer(rx_queue, index);
296 			efx_fini_rx_buffer(rx_queue, rx_buf);
297 		}
298 	}
299 
300 	efx_fini_rx_recycle_ring(rx_queue);
301 
302 	if (xdp_rxq_info_is_reg(&rx_queue->xdp_rxq_info))
303 		xdp_rxq_info_unreg(&rx_queue->xdp_rxq_info);
304 }
305 
306 void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
307 {
308 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
309 		  "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
310 
311 	efx_nic_remove_rx(rx_queue);
312 
313 	kfree(rx_queue->buffer);
314 	rx_queue->buffer = NULL;
315 }
316 
317 /* Unmap a DMA-mapped page.  This function is only called for the final RX
318  * buffer in a page.
319  */
320 void efx_unmap_rx_buffer(struct efx_nic *efx,
321 			 struct efx_rx_buffer *rx_buf)
322 {
323 	struct page *page = rx_buf->page;
324 
325 	if (page) {
326 		struct efx_rx_page_state *state = page_address(page);
327 
328 		dma_unmap_page(&efx->pci_dev->dev,
329 			       state->dma_addr,
330 			       PAGE_SIZE << efx->rx_buffer_order,
331 			       DMA_FROM_DEVICE);
332 	}
333 }
334 
335 void efx_free_rx_buffers(struct efx_rx_queue *rx_queue,
336 			 struct efx_rx_buffer *rx_buf,
337 			 unsigned int num_bufs)
338 {
339 	do {
340 		if (rx_buf->page) {
341 			put_page(rx_buf->page);
342 			rx_buf->page = NULL;
343 		}
344 		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
345 	} while (--num_bufs);
346 }
347 
348 void efx_rx_slow_fill(struct timer_list *t)
349 {
350 	struct efx_rx_queue *rx_queue = timer_container_of(rx_queue, t,
351 							   slow_fill);
352 
353 	/* Post an event to cause NAPI to run and refill the queue */
354 	efx_nic_generate_fill_event(rx_queue);
355 	++rx_queue->slow_fill_count;
356 }
357 
358 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
359 {
360 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
361 }
362 
363 /* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
364  *
365  * @rx_queue:		Efx RX queue
366  *
367  * This allocates a batch of pages, maps them for DMA, and populates
368  * struct efx_rx_buffers for each one. Return a negative error code or
369  * 0 on success. If a single page can be used for multiple buffers,
370  * then the page will either be inserted fully, or not at all.
371  */
372 static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
373 {
374 	unsigned int page_offset, index, count;
375 	struct efx_nic *efx = rx_queue->efx;
376 	struct efx_rx_page_state *state;
377 	struct efx_rx_buffer *rx_buf;
378 	dma_addr_t dma_addr;
379 	struct page *page;
380 
381 	count = 0;
382 	do {
383 		page = efx_reuse_page(rx_queue);
384 		if (page == NULL) {
385 			page = alloc_pages(__GFP_COMP |
386 					   (atomic ? GFP_ATOMIC : GFP_KERNEL),
387 					   efx->rx_buffer_order);
388 			if (unlikely(page == NULL))
389 				return -ENOMEM;
390 			dma_addr =
391 				dma_map_page(&efx->pci_dev->dev, page, 0,
392 					     PAGE_SIZE << efx->rx_buffer_order,
393 					     DMA_FROM_DEVICE);
394 			if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
395 						       dma_addr))) {
396 				__free_pages(page, efx->rx_buffer_order);
397 				return -EIO;
398 			}
399 			state = page_address(page);
400 			state->dma_addr = dma_addr;
401 		} else {
402 			state = page_address(page);
403 			dma_addr = state->dma_addr;
404 		}
405 
406 		dma_addr += sizeof(struct efx_rx_page_state);
407 		page_offset = sizeof(struct efx_rx_page_state);
408 
409 		do {
410 			index = rx_queue->added_count & rx_queue->ptr_mask;
411 			rx_buf = efx_rx_buffer(rx_queue, index);
412 			rx_buf->dma_addr = dma_addr + efx->rx_ip_align +
413 					   EFX_XDP_HEADROOM;
414 			rx_buf->page = page;
415 			rx_buf->page_offset = page_offset + efx->rx_ip_align +
416 					      EFX_XDP_HEADROOM;
417 			rx_buf->len = efx->rx_dma_len;
418 			rx_buf->flags = 0;
419 			++rx_queue->added_count;
420 			get_page(page);
421 			dma_addr += efx->rx_page_buf_step;
422 			page_offset += efx->rx_page_buf_step;
423 		} while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
424 
425 		rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
426 	} while (++count < efx->rx_pages_per_batch);
427 
428 	return 0;
429 }
430 
431 void efx_rx_config_page_split(struct efx_nic *efx)
432 {
433 	efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align +
434 				      EFX_XDP_HEADROOM + EFX_XDP_TAILROOM,
435 				      EFX_RX_BUF_ALIGNMENT);
436 	efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
437 		((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
438 		efx->rx_page_buf_step);
439 	efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
440 		efx->rx_bufs_per_page;
441 	efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
442 					       efx->rx_bufs_per_page);
443 }
444 
445 /* efx_fast_push_rx_descriptors - push new RX descriptors quickly
446  * @rx_queue:		RX descriptor queue
447  *
448  * This will aim to fill the RX descriptor queue up to
449  * @rx_queue->@max_fill. If there is insufficient atomic
450  * memory to do so, a slow fill will be scheduled.
451  *
452  * The caller must provide serialisation (none is used here). In practise,
453  * this means this function must run from the NAPI handler, or be called
454  * when NAPI is disabled.
455  */
456 void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
457 {
458 	struct efx_nic *efx = rx_queue->efx;
459 	unsigned int fill_level, batch_size;
460 	int space, rc = 0;
461 
462 	if (!rx_queue->refill_enabled)
463 		return;
464 
465 	/* Calculate current fill level, and exit if we don't need to fill */
466 	fill_level = (rx_queue->added_count - rx_queue->removed_count);
467 	EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
468 	if (fill_level >= rx_queue->fast_fill_trigger)
469 		goto out;
470 
471 	/* Record minimum fill level */
472 	if (unlikely(fill_level < rx_queue->min_fill)) {
473 		if (fill_level)
474 			rx_queue->min_fill = fill_level;
475 	}
476 
477 	batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
478 	space = rx_queue->max_fill - fill_level;
479 	EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
480 
481 	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
482 		   "RX queue %d fast-filling descriptor ring from"
483 		   " level %d to level %d\n",
484 		   efx_rx_queue_index(rx_queue), fill_level,
485 		   rx_queue->max_fill);
486 
487 	do {
488 		rc = efx_init_rx_buffers(rx_queue, atomic);
489 		if (unlikely(rc)) {
490 			/* Ensure that we don't leave the rx queue empty */
491 			efx_schedule_slow_fill(rx_queue);
492 			goto out;
493 		}
494 	} while ((space -= batch_size) >= batch_size);
495 
496 	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
497 		   "RX queue %d fast-filled descriptor ring "
498 		   "to level %d\n", efx_rx_queue_index(rx_queue),
499 		   rx_queue->added_count - rx_queue->removed_count);
500 
501  out:
502 	if (rx_queue->notified_count != rx_queue->added_count)
503 		efx_nic_notify_rx_desc(rx_queue);
504 }
505 
506 /* Pass a received packet up through GRO.  GRO can handle pages
507  * regardless of checksum state and skbs with a good checksum.
508  */
509 void
510 efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
511 		  unsigned int n_frags, u8 *eh, __wsum csum)
512 {
513 	struct napi_struct *napi = &channel->napi_str;
514 	struct efx_nic *efx = channel->efx;
515 	struct sk_buff *skb;
516 
517 	skb = napi_get_frags(napi);
518 	if (unlikely(!skb)) {
519 		struct efx_rx_queue *rx_queue;
520 
521 		rx_queue = efx_channel_get_rx_queue(channel);
522 		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
523 		return;
524 	}
525 
526 	if (efx->net_dev->features & NETIF_F_RXHASH &&
527 	    efx_rx_buf_hash_valid(efx, eh))
528 		skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
529 			     PKT_HASH_TYPE_L3);
530 	if (csum) {
531 		skb->csum = csum;
532 		skb->ip_summed = CHECKSUM_COMPLETE;
533 	} else {
534 		skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
535 				  CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
536 	}
537 	skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
538 
539 	for (;;) {
540 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
541 				   rx_buf->page, rx_buf->page_offset,
542 				   rx_buf->len);
543 		rx_buf->page = NULL;
544 		skb->len += rx_buf->len;
545 		if (skb_shinfo(skb)->nr_frags == n_frags)
546 			break;
547 
548 		rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
549 	}
550 
551 	skb->data_len = skb->len;
552 	skb->truesize += n_frags * efx->rx_buffer_truesize;
553 
554 	skb_record_rx_queue(skb, channel->rx_queue.core_index);
555 
556 	napi_gro_frags(napi);
557 }
558 
559 struct efx_rss_context_priv *efx_find_rss_context_entry(struct efx_nic *efx,
560 							u32 id)
561 {
562 	struct ethtool_rxfh_context *ctx;
563 
564 	WARN_ON(!mutex_is_locked(&efx->net_dev->ethtool->rss_lock));
565 
566 	ctx = xa_load(&efx->net_dev->ethtool->rss_ctx, id);
567 	if (!ctx)
568 		return NULL;
569 	return ethtool_rxfh_context_priv(ctx);
570 }
571 
572 void efx_set_default_rx_indir_table(struct efx_nic *efx, u32 *indir)
573 {
574 	size_t i;
575 
576 	for (i = 0; i < ARRAY_SIZE(efx->rss_context.rx_indir_table); i++)
577 		indir[i] = ethtool_rxfh_indir_default(i, efx->rss_spread);
578 }
579 
580 /**
581  * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
582  * @spec: Specification to test
583  *
584  * Return: %true if the specification is a non-drop RX filter that
585  * matches a local MAC address I/G bit value of 1 or matches a local
586  * IPv4 or IPv6 address value in the respective multicast address
587  * range.  Otherwise %false.
588  */
589 bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
590 {
591 	if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
592 	    spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
593 		return false;
594 
595 	if (spec->match_flags &
596 	    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
597 	    is_multicast_ether_addr(spec->loc_mac))
598 		return true;
599 
600 	if ((spec->match_flags &
601 	     (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
602 	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
603 		if (spec->ether_type == htons(ETH_P_IP) &&
604 		    ipv4_is_multicast(spec->loc_host[0]))
605 			return true;
606 		if (spec->ether_type == htons(ETH_P_IPV6) &&
607 		    ((const u8 *)spec->loc_host)[0] == 0xff)
608 			return true;
609 	}
610 
611 	return false;
612 }
613 
614 bool efx_filter_spec_equal(const struct efx_filter_spec *left,
615 			   const struct efx_filter_spec *right)
616 {
617 	if ((left->match_flags ^ right->match_flags) |
618 	    ((left->flags ^ right->flags) &
619 	     (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
620 		return false;
621 
622 	return memcmp(&left->vport_id, &right->vport_id,
623 		      sizeof(struct efx_filter_spec) -
624 		      offsetof(struct efx_filter_spec, vport_id)) == 0;
625 }
626 
627 u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
628 {
629 	BUILD_BUG_ON(offsetof(struct efx_filter_spec, vport_id) & 3);
630 	return jhash2((const u32 *)&spec->vport_id,
631 		      (sizeof(struct efx_filter_spec) -
632 		       offsetof(struct efx_filter_spec, vport_id)) / 4,
633 		      0);
634 }
635 
636 #ifdef CONFIG_RFS_ACCEL
637 bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
638 			bool *force)
639 {
640 	if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
641 		/* ARFS is currently updating this entry, leave it */
642 		return false;
643 	}
644 	if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
645 		/* ARFS tried and failed to update this, so it's probably out
646 		 * of date.  Remove the filter and the ARFS rule entry.
647 		 */
648 		rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
649 		*force = true;
650 		return true;
651 	} else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
652 		/* ARFS has moved on, so old filter is not needed.  Since we did
653 		 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
654 		 * not be removed by efx_rps_hash_del() subsequently.
655 		 */
656 		*force = true;
657 		return true;
658 	}
659 	/* Remove it iff ARFS wants to. */
660 	return true;
661 }
662 
663 static
664 struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
665 				       const struct efx_filter_spec *spec)
666 {
667 	u32 hash = efx_filter_spec_hash(spec);
668 
669 	lockdep_assert_held(&efx->rps_hash_lock);
670 	if (!efx->rps_hash_table)
671 		return NULL;
672 	return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
673 }
674 
675 struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
676 					const struct efx_filter_spec *spec)
677 {
678 	struct efx_arfs_rule *rule;
679 	struct hlist_head *head;
680 	struct hlist_node *node;
681 
682 	head = efx_rps_hash_bucket(efx, spec);
683 	if (!head)
684 		return NULL;
685 	hlist_for_each(node, head) {
686 		rule = container_of(node, struct efx_arfs_rule, node);
687 		if (efx_filter_spec_equal(spec, &rule->spec))
688 			return rule;
689 	}
690 	return NULL;
691 }
692 
693 struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
694 				       const struct efx_filter_spec *spec,
695 				       bool *new)
696 {
697 	struct efx_arfs_rule *rule;
698 	struct hlist_head *head;
699 	struct hlist_node *node;
700 
701 	head = efx_rps_hash_bucket(efx, spec);
702 	if (!head)
703 		return NULL;
704 	hlist_for_each(node, head) {
705 		rule = container_of(node, struct efx_arfs_rule, node);
706 		if (efx_filter_spec_equal(spec, &rule->spec)) {
707 			*new = false;
708 			return rule;
709 		}
710 	}
711 	rule = kmalloc_obj(*rule, GFP_ATOMIC);
712 	*new = true;
713 	if (rule) {
714 		memcpy(&rule->spec, spec, sizeof(rule->spec));
715 		hlist_add_head(&rule->node, head);
716 	}
717 	return rule;
718 }
719 
720 void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
721 {
722 	struct efx_arfs_rule *rule;
723 	struct hlist_head *head;
724 	struct hlist_node *node;
725 
726 	head = efx_rps_hash_bucket(efx, spec);
727 	if (WARN_ON(!head))
728 		return;
729 	hlist_for_each(node, head) {
730 		rule = container_of(node, struct efx_arfs_rule, node);
731 		if (efx_filter_spec_equal(spec, &rule->spec)) {
732 			/* Someone already reused the entry.  We know that if
733 			 * this check doesn't fire (i.e. filter_id == REMOVING)
734 			 * then the REMOVING mark was put there by our caller,
735 			 * because caller is holding a lock on filter table and
736 			 * only holders of that lock set REMOVING.
737 			 */
738 			if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
739 				return;
740 			hlist_del(node);
741 			kfree(rule);
742 			return;
743 		}
744 	}
745 	/* We didn't find it. */
746 	WARN_ON(1);
747 }
748 #endif
749 
750 int efx_probe_filters(struct efx_nic *efx)
751 {
752 	int rc;
753 
754 	mutex_lock(&efx->mac_lock);
755 	rc = efx->type->filter_table_probe(efx);
756 	if (rc)
757 		goto out_unlock;
758 
759 #ifdef CONFIG_RFS_ACCEL
760 	if (efx->type->offload_features & NETIF_F_NTUPLE) {
761 		struct efx_channel *channel;
762 		int i, success = 1;
763 
764 		efx_for_each_channel(channel, efx) {
765 			channel->rps_flow_id =
766 				kcalloc(efx->type->max_rx_ip_filters,
767 					sizeof(*channel->rps_flow_id),
768 					GFP_KERNEL);
769 			if (!channel->rps_flow_id)
770 				success = 0;
771 			else
772 				for (i = 0;
773 				     i < efx->type->max_rx_ip_filters;
774 				     ++i)
775 					channel->rps_flow_id[i] =
776 						RPS_FLOW_ID_INVALID;
777 			channel->rfs_expire_index = 0;
778 			channel->rfs_filter_count = 0;
779 		}
780 
781 		if (!success) {
782 			efx_for_each_channel(channel, efx) {
783 				kfree(channel->rps_flow_id);
784 				channel->rps_flow_id = NULL;
785 			}
786 			efx->type->filter_table_remove(efx);
787 			rc = -ENOMEM;
788 			goto out_unlock;
789 		}
790 	}
791 #endif
792 out_unlock:
793 	mutex_unlock(&efx->mac_lock);
794 	return rc;
795 }
796 
797 void efx_remove_filters(struct efx_nic *efx)
798 {
799 #ifdef CONFIG_RFS_ACCEL
800 	struct efx_channel *channel;
801 
802 	efx_for_each_channel(channel, efx) {
803 		cancel_delayed_work_sync(&channel->filter_work);
804 		kfree(channel->rps_flow_id);
805 		channel->rps_flow_id = NULL;
806 	}
807 #endif
808 	efx->type->filter_table_remove(efx);
809 }
810 
811 #ifdef CONFIG_RFS_ACCEL
812 
813 static void efx_filter_rfs_work(struct work_struct *data)
814 {
815 	struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
816 							      work);
817 	struct efx_nic *efx = efx_netdev_priv(req->net_dev);
818 	struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
819 	int slot_idx = req - efx->rps_slot;
820 	struct efx_arfs_rule *rule;
821 	u16 arfs_id = 0;
822 	int rc;
823 
824 	rc = efx->type->filter_insert(efx, &req->spec, true);
825 	if (rc >= 0)
826 		/* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
827 		rc %= efx->type->max_rx_ip_filters;
828 	if (efx->rps_hash_table) {
829 		spin_lock_bh(&efx->rps_hash_lock);
830 		rule = efx_rps_hash_find(efx, &req->spec);
831 		/* The rule might have already gone, if someone else's request
832 		 * for the same spec was already worked and then expired before
833 		 * we got around to our work.  In that case we have nothing
834 		 * tying us to an arfs_id, meaning that as soon as the filter
835 		 * is considered for expiry it will be removed.
836 		 */
837 		if (rule) {
838 			if (rc < 0)
839 				rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
840 			else
841 				rule->filter_id = rc;
842 			arfs_id = rule->arfs_id;
843 		}
844 		spin_unlock_bh(&efx->rps_hash_lock);
845 	}
846 	if (rc >= 0) {
847 		/* Remember this so we can check whether to expire the filter
848 		 * later.
849 		 */
850 		mutex_lock(&efx->rps_mutex);
851 		if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
852 			channel->rfs_filter_count++;
853 		channel->rps_flow_id[rc] = req->flow_id;
854 		mutex_unlock(&efx->rps_mutex);
855 
856 		if (req->spec.ether_type == htons(ETH_P_IP))
857 			netif_info(efx, rx_status, efx->net_dev,
858 				   "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
859 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
860 				   req->spec.rem_host, ntohs(req->spec.rem_port),
861 				   req->spec.loc_host, ntohs(req->spec.loc_port),
862 				   req->rxq_index, req->flow_id, rc, arfs_id);
863 		else
864 			netif_info(efx, rx_status, efx->net_dev,
865 				   "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
866 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
867 				   req->spec.rem_host, ntohs(req->spec.rem_port),
868 				   req->spec.loc_host, ntohs(req->spec.loc_port),
869 				   req->rxq_index, req->flow_id, rc, arfs_id);
870 		channel->n_rfs_succeeded++;
871 	} else {
872 		if (req->spec.ether_type == htons(ETH_P_IP))
873 			netif_dbg(efx, rx_status, efx->net_dev,
874 				  "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
875 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
876 				  req->spec.rem_host, ntohs(req->spec.rem_port),
877 				  req->spec.loc_host, ntohs(req->spec.loc_port),
878 				  req->rxq_index, req->flow_id, rc, arfs_id);
879 		else
880 			netif_dbg(efx, rx_status, efx->net_dev,
881 				  "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
882 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
883 				  req->spec.rem_host, ntohs(req->spec.rem_port),
884 				  req->spec.loc_host, ntohs(req->spec.loc_port),
885 				  req->rxq_index, req->flow_id, rc, arfs_id);
886 		channel->n_rfs_failed++;
887 		/* We're overloading the NIC's filter tables, so let's do a
888 		 * chunk of extra expiry work.
889 		 */
890 		__efx_filter_rfs_expire(channel, min(channel->rfs_filter_count,
891 						     100u));
892 	}
893 
894 	/* Release references */
895 	clear_bit(slot_idx, &efx->rps_slot_map);
896 	netdev_put(req->net_dev, &req->net_dev_tracker);
897 }
898 
899 int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
900 		   u16 rxq_index, u32 flow_id)
901 {
902 	struct efx_nic *efx = efx_netdev_priv(net_dev);
903 	struct efx_async_filter_insertion *req;
904 	struct efx_arfs_rule *rule;
905 	struct flow_keys fk;
906 	int slot_idx;
907 	bool new;
908 	int rc;
909 
910 	/* find a free slot */
911 	for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
912 		if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
913 			break;
914 	if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
915 		return -EBUSY;
916 
917 	if (flow_id == RPS_FLOW_ID_INVALID) {
918 		rc = -EINVAL;
919 		goto out_clear;
920 	}
921 
922 	if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
923 		rc = -EPROTONOSUPPORT;
924 		goto out_clear;
925 	}
926 
927 	if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
928 		rc = -EPROTONOSUPPORT;
929 		goto out_clear;
930 	}
931 	if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
932 		rc = -EPROTONOSUPPORT;
933 		goto out_clear;
934 	}
935 
936 	req = efx->rps_slot + slot_idx;
937 	efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
938 			   efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
939 			   rxq_index);
940 	req->spec.match_flags =
941 		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
942 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
943 		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
944 	req->spec.ether_type = fk.basic.n_proto;
945 	req->spec.ip_proto = fk.basic.ip_proto;
946 
947 	if (fk.basic.n_proto == htons(ETH_P_IP)) {
948 		req->spec.rem_host[0] = fk.addrs.v4addrs.src;
949 		req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
950 	} else {
951 		memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
952 		       sizeof(struct in6_addr));
953 		memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
954 		       sizeof(struct in6_addr));
955 	}
956 
957 	req->spec.rem_port = fk.ports.src;
958 	req->spec.loc_port = fk.ports.dst;
959 
960 	if (efx->rps_hash_table) {
961 		/* Add it to ARFS hash table */
962 		spin_lock(&efx->rps_hash_lock);
963 		rule = efx_rps_hash_add(efx, &req->spec, &new);
964 		if (!rule) {
965 			rc = -ENOMEM;
966 			goto out_unlock;
967 		}
968 		if (new)
969 			rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
970 		rc = rule->arfs_id;
971 		/* Skip if existing or pending filter already does the right thing */
972 		if (!new && rule->rxq_index == rxq_index &&
973 		    rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
974 			goto out_unlock;
975 		rule->rxq_index = rxq_index;
976 		rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
977 		spin_unlock(&efx->rps_hash_lock);
978 	} else {
979 		/* Without an ARFS hash table, we just use arfs_id 0 for all
980 		 * filters.  This means if multiple flows hash to the same
981 		 * flow_id, all but the most recently touched will be eligible
982 		 * for expiry.
983 		 */
984 		rc = 0;
985 	}
986 
987 	/* Queue the request */
988 	req->net_dev = net_dev;
989 	netdev_hold(req->net_dev, &req->net_dev_tracker, GFP_ATOMIC);
990 	INIT_WORK(&req->work, efx_filter_rfs_work);
991 	req->rxq_index = rxq_index;
992 	req->flow_id = flow_id;
993 	schedule_work(&req->work);
994 	return rc;
995 out_unlock:
996 	spin_unlock(&efx->rps_hash_lock);
997 out_clear:
998 	clear_bit(slot_idx, &efx->rps_slot_map);
999 	return rc;
1000 }
1001 
1002 bool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota)
1003 {
1004 	bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
1005 	struct efx_nic *efx = channel->efx;
1006 	unsigned int index, size, start;
1007 	u32 flow_id;
1008 
1009 	if (!mutex_trylock(&efx->rps_mutex))
1010 		return false;
1011 	expire_one = efx->type->filter_rfs_expire_one;
1012 	index = channel->rfs_expire_index;
1013 	start = index;
1014 	size = efx->type->max_rx_ip_filters;
1015 	while (quota) {
1016 		flow_id = channel->rps_flow_id[index];
1017 
1018 		if (flow_id != RPS_FLOW_ID_INVALID) {
1019 			quota--;
1020 			if (expire_one(efx, flow_id, index)) {
1021 				netif_info(efx, rx_status, efx->net_dev,
1022 					   "expired filter %d [channel %u flow %u]\n",
1023 					   index, channel->channel, flow_id);
1024 				channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
1025 				channel->rfs_filter_count--;
1026 			}
1027 		}
1028 		if (++index == size)
1029 			index = 0;
1030 		/* If we were called with a quota that exceeds the total number
1031 		 * of filters in the table (which shouldn't happen, but could
1032 		 * if two callers race), ensure that we don't loop forever -
1033 		 * stop when we've examined every row of the table.
1034 		 */
1035 		if (index == start)
1036 			break;
1037 	}
1038 
1039 	channel->rfs_expire_index = index;
1040 	mutex_unlock(&efx->rps_mutex);
1041 	return true;
1042 }
1043 
1044 #endif /* CONFIG_RFS_ACCEL */
1045