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