xref: /openbmc/linux/drivers/net/ethernet/sfc/rx.c (revision 82df5b73)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2006 Fen Systems Ltd.
5  * Copyright 2005-2013 Solarflare Communications Inc.
6  */
7 
8 #include <linux/socket.h>
9 #include <linux/in.h>
10 #include <linux/slab.h>
11 #include <linux/ip.h>
12 #include <linux/ipv6.h>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/prefetch.h>
16 #include <linux/moduleparam.h>
17 #include <linux/iommu.h>
18 #include <net/ip.h>
19 #include <net/checksum.h>
20 #include <net/xdp.h>
21 #include <linux/bpf_trace.h>
22 #include "net_driver.h"
23 #include "efx.h"
24 #include "rx_common.h"
25 #include "filter.h"
26 #include "nic.h"
27 #include "selftest.h"
28 #include "workarounds.h"
29 
30 /* Preferred number of descriptors to fill at once */
31 #define EFX_RX_PREFERRED_BATCH 8U
32 
33 /* Maximum rx prefix used by any architecture. */
34 #define EFX_MAX_RX_PREFIX_SIZE 16
35 
36 /* Size of buffer allocated for skb header area. */
37 #define EFX_SKB_HEADERS  128u
38 
39 /* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
40 #define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
41 				      EFX_RX_USR_BUF_SIZE)
42 
43 static inline void efx_sync_rx_buffer(struct efx_nic *efx,
44 				      struct efx_rx_buffer *rx_buf,
45 				      unsigned int len)
46 {
47 	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr, len,
48 				DMA_FROM_DEVICE);
49 }
50 
51 static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
52 				     struct efx_rx_buffer *rx_buf,
53 				     int len)
54 {
55 	struct efx_nic *efx = rx_queue->efx;
56 	unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
57 
58 	if (likely(len <= max_len))
59 		return;
60 
61 	/* The packet must be discarded, but this is only a fatal error
62 	 * if the caller indicated it was
63 	 */
64 	rx_buf->flags |= EFX_RX_PKT_DISCARD;
65 
66 	if (net_ratelimit())
67 		netif_err(efx, rx_err, efx->net_dev,
68 			  "RX queue %d overlength RX event (%#x > %#x)\n",
69 			  efx_rx_queue_index(rx_queue), len, max_len);
70 
71 	efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
72 }
73 
74 /* Allocate and construct an SKB around page fragments */
75 static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
76 				     struct efx_rx_buffer *rx_buf,
77 				     unsigned int n_frags,
78 				     u8 *eh, int hdr_len)
79 {
80 	struct efx_nic *efx = channel->efx;
81 	struct sk_buff *skb;
82 
83 	/* Allocate an SKB to store the headers */
84 	skb = netdev_alloc_skb(efx->net_dev,
85 			       efx->rx_ip_align + efx->rx_prefix_size +
86 			       hdr_len);
87 	if (unlikely(skb == NULL)) {
88 		atomic_inc(&efx->n_rx_noskb_drops);
89 		return NULL;
90 	}
91 
92 	EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
93 
94 	memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
95 	       efx->rx_prefix_size + hdr_len);
96 	skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
97 	__skb_put(skb, hdr_len);
98 
99 	/* Append the remaining page(s) onto the frag list */
100 	if (rx_buf->len > hdr_len) {
101 		rx_buf->page_offset += hdr_len;
102 		rx_buf->len -= hdr_len;
103 
104 		for (;;) {
105 			skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
106 					   rx_buf->page, rx_buf->page_offset,
107 					   rx_buf->len);
108 			rx_buf->page = NULL;
109 			skb->len += rx_buf->len;
110 			skb->data_len += rx_buf->len;
111 			if (skb_shinfo(skb)->nr_frags == n_frags)
112 				break;
113 
114 			rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
115 		}
116 	} else {
117 		__free_pages(rx_buf->page, efx->rx_buffer_order);
118 		rx_buf->page = NULL;
119 		n_frags = 0;
120 	}
121 
122 	skb->truesize += n_frags * efx->rx_buffer_truesize;
123 
124 	/* Move past the ethernet header */
125 	skb->protocol = eth_type_trans(skb, efx->net_dev);
126 
127 	skb_mark_napi_id(skb, &channel->napi_str);
128 
129 	return skb;
130 }
131 
132 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
133 		   unsigned int n_frags, unsigned int len, u16 flags)
134 {
135 	struct efx_nic *efx = rx_queue->efx;
136 	struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
137 	struct efx_rx_buffer *rx_buf;
138 
139 	rx_queue->rx_packets++;
140 
141 	rx_buf = efx_rx_buffer(rx_queue, index);
142 	rx_buf->flags |= flags;
143 
144 	/* Validate the number of fragments and completed length */
145 	if (n_frags == 1) {
146 		if (!(flags & EFX_RX_PKT_PREFIX_LEN))
147 			efx_rx_packet__check_len(rx_queue, rx_buf, len);
148 	} else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
149 		   unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
150 		   unlikely(len > n_frags * efx->rx_dma_len) ||
151 		   unlikely(!efx->rx_scatter)) {
152 		/* If this isn't an explicit discard request, either
153 		 * the hardware or the driver is broken.
154 		 */
155 		WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
156 		rx_buf->flags |= EFX_RX_PKT_DISCARD;
157 	}
158 
159 	netif_vdbg(efx, rx_status, efx->net_dev,
160 		   "RX queue %d received ids %x-%x len %d %s%s\n",
161 		   efx_rx_queue_index(rx_queue), index,
162 		   (index + n_frags - 1) & rx_queue->ptr_mask, len,
163 		   (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
164 		   (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
165 
166 	/* Discard packet, if instructed to do so.  Process the
167 	 * previous receive first.
168 	 */
169 	if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
170 		efx_rx_flush_packet(channel);
171 		efx_discard_rx_packet(channel, rx_buf, n_frags);
172 		return;
173 	}
174 
175 	if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
176 		rx_buf->len = len;
177 
178 	/* Release and/or sync the DMA mapping - assumes all RX buffers
179 	 * consumed in-order per RX queue.
180 	 */
181 	efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
182 
183 	/* Prefetch nice and early so data will (hopefully) be in cache by
184 	 * the time we look at it.
185 	 */
186 	prefetch(efx_rx_buf_va(rx_buf));
187 
188 	rx_buf->page_offset += efx->rx_prefix_size;
189 	rx_buf->len -= efx->rx_prefix_size;
190 
191 	if (n_frags > 1) {
192 		/* Release/sync DMA mapping for additional fragments.
193 		 * Fix length for last fragment.
194 		 */
195 		unsigned int tail_frags = n_frags - 1;
196 
197 		for (;;) {
198 			rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
199 			if (--tail_frags == 0)
200 				break;
201 			efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
202 		}
203 		rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
204 		efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
205 	}
206 
207 	/* All fragments have been DMA-synced, so recycle pages. */
208 	rx_buf = efx_rx_buffer(rx_queue, index);
209 	efx_recycle_rx_pages(channel, rx_buf, n_frags);
210 
211 	/* Pipeline receives so that we give time for packet headers to be
212 	 * prefetched into cache.
213 	 */
214 	efx_rx_flush_packet(channel);
215 	channel->rx_pkt_n_frags = n_frags;
216 	channel->rx_pkt_index = index;
217 }
218 
219 static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
220 			   struct efx_rx_buffer *rx_buf,
221 			   unsigned int n_frags)
222 {
223 	struct sk_buff *skb;
224 	u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
225 
226 	skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
227 	if (unlikely(skb == NULL)) {
228 		struct efx_rx_queue *rx_queue;
229 
230 		rx_queue = efx_channel_get_rx_queue(channel);
231 		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
232 		return;
233 	}
234 	skb_record_rx_queue(skb, channel->rx_queue.core_index);
235 
236 	/* Set the SKB flags */
237 	skb_checksum_none_assert(skb);
238 	if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
239 		skb->ip_summed = CHECKSUM_UNNECESSARY;
240 		skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
241 	}
242 
243 	efx_rx_skb_attach_timestamp(channel, skb);
244 
245 	if (channel->type->receive_skb)
246 		if (channel->type->receive_skb(channel, skb))
247 			return;
248 
249 	/* Pass the packet up */
250 	if (channel->rx_list != NULL)
251 		/* Add to list, will pass up later */
252 		list_add_tail(&skb->list, channel->rx_list);
253 	else
254 		/* No list, so pass it up now */
255 		netif_receive_skb(skb);
256 }
257 
258 /** efx_do_xdp: perform XDP processing on a received packet
259  *
260  * Returns true if packet should still be delivered.
261  */
262 static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
263 		       struct efx_rx_buffer *rx_buf, u8 **ehp)
264 {
265 	u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE];
266 	struct efx_rx_queue *rx_queue;
267 	struct bpf_prog *xdp_prog;
268 	struct xdp_frame *xdpf;
269 	struct xdp_buff xdp;
270 	u32 xdp_act;
271 	s16 offset;
272 	int err;
273 
274 	rcu_read_lock();
275 	xdp_prog = rcu_dereference(efx->xdp_prog);
276 	if (!xdp_prog) {
277 		rcu_read_unlock();
278 		return true;
279 	}
280 
281 	rx_queue = efx_channel_get_rx_queue(channel);
282 
283 	if (unlikely(channel->rx_pkt_n_frags > 1)) {
284 		/* We can't do XDP on fragmented packets - drop. */
285 		rcu_read_unlock();
286 		efx_free_rx_buffers(rx_queue, rx_buf,
287 				    channel->rx_pkt_n_frags);
288 		if (net_ratelimit())
289 			netif_err(efx, rx_err, efx->net_dev,
290 				  "XDP is not possible with multiple receive fragments (%d)\n",
291 				  channel->rx_pkt_n_frags);
292 		channel->n_rx_xdp_bad_drops++;
293 		return false;
294 	}
295 
296 	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr,
297 				rx_buf->len, DMA_FROM_DEVICE);
298 
299 	/* Save the rx prefix. */
300 	EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE);
301 	memcpy(rx_prefix, *ehp - efx->rx_prefix_size,
302 	       efx->rx_prefix_size);
303 
304 	xdp.data = *ehp;
305 	xdp.data_hard_start = xdp.data - EFX_XDP_HEADROOM;
306 
307 	/* No support yet for XDP metadata */
308 	xdp_set_data_meta_invalid(&xdp);
309 	xdp.data_end = xdp.data + rx_buf->len;
310 	xdp.rxq = &rx_queue->xdp_rxq_info;
311 	xdp.frame_sz = efx->rx_page_buf_step;
312 
313 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
314 	rcu_read_unlock();
315 
316 	offset = (u8 *)xdp.data - *ehp;
317 
318 	switch (xdp_act) {
319 	case XDP_PASS:
320 		/* Fix up rx prefix. */
321 		if (offset) {
322 			*ehp += offset;
323 			rx_buf->page_offset += offset;
324 			rx_buf->len -= offset;
325 			memcpy(*ehp - efx->rx_prefix_size, rx_prefix,
326 			       efx->rx_prefix_size);
327 		}
328 		break;
329 
330 	case XDP_TX:
331 		/* Buffer ownership passes to tx on success. */
332 		xdpf = xdp_convert_buff_to_frame(&xdp);
333 		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
334 		if (unlikely(err != 1)) {
335 			efx_free_rx_buffers(rx_queue, rx_buf, 1);
336 			if (net_ratelimit())
337 				netif_err(efx, rx_err, efx->net_dev,
338 					  "XDP TX failed (%d)\n", err);
339 			channel->n_rx_xdp_bad_drops++;
340 			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
341 		} else {
342 			channel->n_rx_xdp_tx++;
343 		}
344 		break;
345 
346 	case XDP_REDIRECT:
347 		err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog);
348 		if (unlikely(err)) {
349 			efx_free_rx_buffers(rx_queue, rx_buf, 1);
350 			if (net_ratelimit())
351 				netif_err(efx, rx_err, efx->net_dev,
352 					  "XDP redirect failed (%d)\n", err);
353 			channel->n_rx_xdp_bad_drops++;
354 			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
355 		} else {
356 			channel->n_rx_xdp_redirect++;
357 		}
358 		break;
359 
360 	default:
361 		bpf_warn_invalid_xdp_action(xdp_act);
362 		efx_free_rx_buffers(rx_queue, rx_buf, 1);
363 		channel->n_rx_xdp_bad_drops++;
364 		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
365 		break;
366 
367 	case XDP_ABORTED:
368 		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
369 		/* Fall through */
370 	case XDP_DROP:
371 		efx_free_rx_buffers(rx_queue, rx_buf, 1);
372 		channel->n_rx_xdp_drops++;
373 		break;
374 	}
375 
376 	return xdp_act == XDP_PASS;
377 }
378 
379 /* Handle a received packet.  Second half: Touches packet payload. */
380 void __efx_rx_packet(struct efx_channel *channel)
381 {
382 	struct efx_nic *efx = channel->efx;
383 	struct efx_rx_buffer *rx_buf =
384 		efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
385 	u8 *eh = efx_rx_buf_va(rx_buf);
386 
387 	/* Read length from the prefix if necessary.  This already
388 	 * excludes the length of the prefix itself.
389 	 */
390 	if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
391 		rx_buf->len = le16_to_cpup((__le16 *)
392 					   (eh + efx->rx_packet_len_offset));
393 
394 	/* If we're in loopback test, then pass the packet directly to the
395 	 * loopback layer, and free the rx_buf here
396 	 */
397 	if (unlikely(efx->loopback_selftest)) {
398 		struct efx_rx_queue *rx_queue;
399 
400 		efx_loopback_rx_packet(efx, eh, rx_buf->len);
401 		rx_queue = efx_channel_get_rx_queue(channel);
402 		efx_free_rx_buffers(rx_queue, rx_buf,
403 				    channel->rx_pkt_n_frags);
404 		goto out;
405 	}
406 
407 	if (!efx_do_xdp(efx, channel, rx_buf, &eh))
408 		goto out;
409 
410 	if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
411 		rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
412 
413 	if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
414 		efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh);
415 	else
416 		efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
417 out:
418 	channel->rx_pkt_n_frags = 0;
419 }
420 
421 #ifdef CONFIG_RFS_ACCEL
422 
423 static void efx_filter_rfs_work(struct work_struct *data)
424 {
425 	struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
426 							      work);
427 	struct efx_nic *efx = netdev_priv(req->net_dev);
428 	struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
429 	int slot_idx = req - efx->rps_slot;
430 	struct efx_arfs_rule *rule;
431 	u16 arfs_id = 0;
432 	int rc;
433 
434 	rc = efx->type->filter_insert(efx, &req->spec, true);
435 	if (rc >= 0)
436 		/* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
437 		rc %= efx->type->max_rx_ip_filters;
438 	if (efx->rps_hash_table) {
439 		spin_lock_bh(&efx->rps_hash_lock);
440 		rule = efx_rps_hash_find(efx, &req->spec);
441 		/* The rule might have already gone, if someone else's request
442 		 * for the same spec was already worked and then expired before
443 		 * we got around to our work.  In that case we have nothing
444 		 * tying us to an arfs_id, meaning that as soon as the filter
445 		 * is considered for expiry it will be removed.
446 		 */
447 		if (rule) {
448 			if (rc < 0)
449 				rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
450 			else
451 				rule->filter_id = rc;
452 			arfs_id = rule->arfs_id;
453 		}
454 		spin_unlock_bh(&efx->rps_hash_lock);
455 	}
456 	if (rc >= 0) {
457 		/* Remember this so we can check whether to expire the filter
458 		 * later.
459 		 */
460 		mutex_lock(&efx->rps_mutex);
461 		if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
462 			channel->rfs_filter_count++;
463 		channel->rps_flow_id[rc] = req->flow_id;
464 		mutex_unlock(&efx->rps_mutex);
465 
466 		if (req->spec.ether_type == htons(ETH_P_IP))
467 			netif_info(efx, rx_status, efx->net_dev,
468 				   "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
469 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
470 				   req->spec.rem_host, ntohs(req->spec.rem_port),
471 				   req->spec.loc_host, ntohs(req->spec.loc_port),
472 				   req->rxq_index, req->flow_id, rc, arfs_id);
473 		else
474 			netif_info(efx, rx_status, efx->net_dev,
475 				   "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
476 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
477 				   req->spec.rem_host, ntohs(req->spec.rem_port),
478 				   req->spec.loc_host, ntohs(req->spec.loc_port),
479 				   req->rxq_index, req->flow_id, rc, arfs_id);
480 		channel->n_rfs_succeeded++;
481 	} else {
482 		if (req->spec.ether_type == htons(ETH_P_IP))
483 			netif_dbg(efx, rx_status, efx->net_dev,
484 				  "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
485 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
486 				  req->spec.rem_host, ntohs(req->spec.rem_port),
487 				  req->spec.loc_host, ntohs(req->spec.loc_port),
488 				  req->rxq_index, req->flow_id, rc, arfs_id);
489 		else
490 			netif_dbg(efx, rx_status, efx->net_dev,
491 				  "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
492 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
493 				  req->spec.rem_host, ntohs(req->spec.rem_port),
494 				  req->spec.loc_host, ntohs(req->spec.loc_port),
495 				  req->rxq_index, req->flow_id, rc, arfs_id);
496 		channel->n_rfs_failed++;
497 		/* We're overloading the NIC's filter tables, so let's do a
498 		 * chunk of extra expiry work.
499 		 */
500 		__efx_filter_rfs_expire(channel, min(channel->rfs_filter_count,
501 						     100u));
502 	}
503 
504 	/* Release references */
505 	clear_bit(slot_idx, &efx->rps_slot_map);
506 	dev_put(req->net_dev);
507 }
508 
509 int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
510 		   u16 rxq_index, u32 flow_id)
511 {
512 	struct efx_nic *efx = netdev_priv(net_dev);
513 	struct efx_async_filter_insertion *req;
514 	struct efx_arfs_rule *rule;
515 	struct flow_keys fk;
516 	int slot_idx;
517 	bool new;
518 	int rc;
519 
520 	/* find a free slot */
521 	for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
522 		if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
523 			break;
524 	if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
525 		return -EBUSY;
526 
527 	if (flow_id == RPS_FLOW_ID_INVALID) {
528 		rc = -EINVAL;
529 		goto out_clear;
530 	}
531 
532 	if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
533 		rc = -EPROTONOSUPPORT;
534 		goto out_clear;
535 	}
536 
537 	if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
538 		rc = -EPROTONOSUPPORT;
539 		goto out_clear;
540 	}
541 	if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
542 		rc = -EPROTONOSUPPORT;
543 		goto out_clear;
544 	}
545 
546 	req = efx->rps_slot + slot_idx;
547 	efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
548 			   efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
549 			   rxq_index);
550 	req->spec.match_flags =
551 		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
552 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
553 		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
554 	req->spec.ether_type = fk.basic.n_proto;
555 	req->spec.ip_proto = fk.basic.ip_proto;
556 
557 	if (fk.basic.n_proto == htons(ETH_P_IP)) {
558 		req->spec.rem_host[0] = fk.addrs.v4addrs.src;
559 		req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
560 	} else {
561 		memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
562 		       sizeof(struct in6_addr));
563 		memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
564 		       sizeof(struct in6_addr));
565 	}
566 
567 	req->spec.rem_port = fk.ports.src;
568 	req->spec.loc_port = fk.ports.dst;
569 
570 	if (efx->rps_hash_table) {
571 		/* Add it to ARFS hash table */
572 		spin_lock(&efx->rps_hash_lock);
573 		rule = efx_rps_hash_add(efx, &req->spec, &new);
574 		if (!rule) {
575 			rc = -ENOMEM;
576 			goto out_unlock;
577 		}
578 		if (new)
579 			rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
580 		rc = rule->arfs_id;
581 		/* Skip if existing or pending filter already does the right thing */
582 		if (!new && rule->rxq_index == rxq_index &&
583 		    rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
584 			goto out_unlock;
585 		rule->rxq_index = rxq_index;
586 		rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
587 		spin_unlock(&efx->rps_hash_lock);
588 	} else {
589 		/* Without an ARFS hash table, we just use arfs_id 0 for all
590 		 * filters.  This means if multiple flows hash to the same
591 		 * flow_id, all but the most recently touched will be eligible
592 		 * for expiry.
593 		 */
594 		rc = 0;
595 	}
596 
597 	/* Queue the request */
598 	dev_hold(req->net_dev = net_dev);
599 	INIT_WORK(&req->work, efx_filter_rfs_work);
600 	req->rxq_index = rxq_index;
601 	req->flow_id = flow_id;
602 	schedule_work(&req->work);
603 	return rc;
604 out_unlock:
605 	spin_unlock(&efx->rps_hash_lock);
606 out_clear:
607 	clear_bit(slot_idx, &efx->rps_slot_map);
608 	return rc;
609 }
610 
611 bool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota)
612 {
613 	bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
614 	struct efx_nic *efx = channel->efx;
615 	unsigned int index, size, start;
616 	u32 flow_id;
617 
618 	if (!mutex_trylock(&efx->rps_mutex))
619 		return false;
620 	expire_one = efx->type->filter_rfs_expire_one;
621 	index = channel->rfs_expire_index;
622 	start = index;
623 	size = efx->type->max_rx_ip_filters;
624 	while (quota) {
625 		flow_id = channel->rps_flow_id[index];
626 
627 		if (flow_id != RPS_FLOW_ID_INVALID) {
628 			quota--;
629 			if (expire_one(efx, flow_id, index)) {
630 				netif_info(efx, rx_status, efx->net_dev,
631 					   "expired filter %d [channel %u flow %u]\n",
632 					   index, channel->channel, flow_id);
633 				channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
634 				channel->rfs_filter_count--;
635 			}
636 		}
637 		if (++index == size)
638 			index = 0;
639 		/* If we were called with a quota that exceeds the total number
640 		 * of filters in the table (which shouldn't happen, but could
641 		 * if two callers race), ensure that we don't loop forever -
642 		 * stop when we've examined every row of the table.
643 		 */
644 		if (index == start)
645 			break;
646 	}
647 
648 	channel->rfs_expire_index = index;
649 	mutex_unlock(&efx->rps_mutex);
650 	return true;
651 }
652 
653 #endif /* CONFIG_RFS_ACCEL */
654