xref: /linux/drivers/net/ethernet/intel/i40e/i40e_xsk.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
3 
4 #include <linux/bpf_trace.h>
5 #include <linux/stringify.h>
6 #include <net/xdp_sock_drv.h>
7 #include <net/xdp.h>
8 
9 #include "i40e.h"
10 #include "i40e_txrx_common.h"
11 #include "i40e_xsk.h"
12 
13 int i40e_alloc_rx_bi_zc(struct i40e_ring *rx_ring)
14 {
15 	unsigned long sz = sizeof(*rx_ring->rx_bi_zc) * rx_ring->count;
16 
17 	rx_ring->rx_bi_zc = kzalloc(sz, GFP_KERNEL);
18 	return rx_ring->rx_bi_zc ? 0 : -ENOMEM;
19 }
20 
21 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
22 {
23 	memset(rx_ring->rx_bi_zc, 0,
24 	       sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
25 }
26 
27 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
28 {
29 	return &rx_ring->rx_bi_zc[idx];
30 }
31 
32 /**
33  * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
34  * certain ring/qid
35  * @vsi: Current VSI
36  * @pool: buffer pool
37  * @qid: Rx ring to associate buffer pool with
38  *
39  * Returns 0 on success, <0 on failure
40  **/
41 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
42 				struct xsk_buff_pool *pool,
43 				u16 qid)
44 {
45 	struct net_device *netdev = vsi->netdev;
46 	bool if_running;
47 	int err;
48 
49 	if (vsi->type != I40E_VSI_MAIN)
50 		return -EINVAL;
51 
52 	if (qid >= vsi->num_queue_pairs)
53 		return -EINVAL;
54 
55 	if (qid >= netdev->real_num_rx_queues ||
56 	    qid >= netdev->real_num_tx_queues)
57 		return -EINVAL;
58 
59 	err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
60 	if (err)
61 		return err;
62 
63 	set_bit(qid, vsi->af_xdp_zc_qps);
64 
65 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
66 
67 	if (if_running) {
68 		err = i40e_queue_pair_disable(vsi, qid);
69 		if (err)
70 			return err;
71 
72 		err = i40e_queue_pair_enable(vsi, qid);
73 		if (err)
74 			return err;
75 
76 		/* Kick start the NAPI context so that receiving will start */
77 		err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
78 		if (err)
79 			return err;
80 	}
81 
82 	return 0;
83 }
84 
85 /**
86  * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
87  * certain ring/qid
88  * @vsi: Current VSI
89  * @qid: Rx ring to associate buffer pool with
90  *
91  * Returns 0 on success, <0 on failure
92  **/
93 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
94 {
95 	struct net_device *netdev = vsi->netdev;
96 	struct xsk_buff_pool *pool;
97 	bool if_running;
98 	int err;
99 
100 	pool = xsk_get_pool_from_qid(netdev, qid);
101 	if (!pool)
102 		return -EINVAL;
103 
104 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
105 
106 	if (if_running) {
107 		err = i40e_queue_pair_disable(vsi, qid);
108 		if (err)
109 			return err;
110 	}
111 
112 	clear_bit(qid, vsi->af_xdp_zc_qps);
113 	xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
114 
115 	if (if_running) {
116 		err = i40e_queue_pair_enable(vsi, qid);
117 		if (err)
118 			return err;
119 	}
120 
121 	return 0;
122 }
123 
124 /**
125  * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
126  * a ring/qid
127  * @vsi: Current VSI
128  * @pool: Buffer pool to enable/associate to a ring, or NULL to disable
129  * @qid: Rx ring to (dis)associate buffer pool (from)to
130  *
131  * This function enables or disables a buffer pool to a certain ring.
132  *
133  * Returns 0 on success, <0 on failure
134  **/
135 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
136 			u16 qid)
137 {
138 	return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
139 		i40e_xsk_pool_disable(vsi, qid);
140 }
141 
142 /**
143  * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
144  * @rx_ring: Rx ring
145  * @xdp: xdp_buff used as input to the XDP program
146  *
147  * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
148  **/
149 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp)
150 {
151 	int err, result = I40E_XDP_PASS;
152 	struct i40e_ring *xdp_ring;
153 	struct bpf_prog *xdp_prog;
154 	u32 act;
155 
156 	rcu_read_lock();
157 	/* NB! xdp_prog will always be !NULL, due to the fact that
158 	 * this path is enabled by setting an XDP program.
159 	 */
160 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
161 	act = bpf_prog_run_xdp(xdp_prog, xdp);
162 
163 	switch (act) {
164 	case XDP_PASS:
165 		break;
166 	case XDP_TX:
167 		xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
168 		result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
169 		break;
170 	case XDP_REDIRECT:
171 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
172 		result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
173 		break;
174 	default:
175 		bpf_warn_invalid_xdp_action(act);
176 		fallthrough;
177 	case XDP_ABORTED:
178 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
179 		fallthrough; /* handle aborts by dropping packet */
180 	case XDP_DROP:
181 		result = I40E_XDP_CONSUMED;
182 		break;
183 	}
184 	rcu_read_unlock();
185 	return result;
186 }
187 
188 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
189 {
190 	u16 ntu = rx_ring->next_to_use;
191 	union i40e_rx_desc *rx_desc;
192 	struct xdp_buff **bi, *xdp;
193 	dma_addr_t dma;
194 	bool ok = true;
195 
196 	rx_desc = I40E_RX_DESC(rx_ring, ntu);
197 	bi = i40e_rx_bi(rx_ring, ntu);
198 	do {
199 		xdp = xsk_buff_alloc(rx_ring->xsk_pool);
200 		if (!xdp) {
201 			ok = false;
202 			goto no_buffers;
203 		}
204 		*bi = xdp;
205 		dma = xsk_buff_xdp_get_dma(xdp);
206 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
207 		rx_desc->read.hdr_addr = 0;
208 
209 		rx_desc++;
210 		bi++;
211 		ntu++;
212 
213 		if (unlikely(ntu == rx_ring->count)) {
214 			rx_desc = I40E_RX_DESC(rx_ring, 0);
215 			bi = i40e_rx_bi(rx_ring, 0);
216 			ntu = 0;
217 		}
218 
219 		count--;
220 	} while (count);
221 
222 no_buffers:
223 	if (rx_ring->next_to_use != ntu) {
224 		/* clear the status bits for the next_to_use descriptor */
225 		rx_desc->wb.qword1.status_error_len = 0;
226 		i40e_release_rx_desc(rx_ring, ntu);
227 	}
228 
229 	return ok;
230 }
231 
232 /**
233  * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
234  * @rx_ring: Rx ring
235  * @xdp: xdp_buff
236  *
237  * This functions allocates a new skb from a zero-copy Rx buffer.
238  *
239  * Returns the skb, or NULL on failure.
240  **/
241 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
242 					     struct xdp_buff *xdp)
243 {
244 	unsigned int metasize = xdp->data - xdp->data_meta;
245 	unsigned int datasize = xdp->data_end - xdp->data;
246 	struct sk_buff *skb;
247 
248 	/* allocate a skb to store the frags */
249 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
250 			       xdp->data_end - xdp->data_hard_start,
251 			       GFP_ATOMIC | __GFP_NOWARN);
252 	if (unlikely(!skb))
253 		return NULL;
254 
255 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
256 	memcpy(__skb_put(skb, datasize), xdp->data, datasize);
257 	if (metasize)
258 		skb_metadata_set(skb, metasize);
259 
260 	xsk_buff_free(xdp);
261 	return skb;
262 }
263 
264 /**
265  * i40e_inc_ntc: Advance the next_to_clean index
266  * @rx_ring: Rx ring
267  **/
268 static void i40e_inc_ntc(struct i40e_ring *rx_ring)
269 {
270 	u32 ntc = rx_ring->next_to_clean + 1;
271 
272 	ntc = (ntc < rx_ring->count) ? ntc : 0;
273 	rx_ring->next_to_clean = ntc;
274 }
275 
276 /**
277  * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
278  * @rx_ring: Rx ring
279  * @budget: NAPI budget
280  *
281  * Returns amount of work completed
282  **/
283 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
284 {
285 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
286 	u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
287 	unsigned int xdp_res, xdp_xmit = 0;
288 	bool failure = false;
289 	struct sk_buff *skb;
290 
291 	while (likely(total_rx_packets < (unsigned int)budget)) {
292 		union i40e_rx_desc *rx_desc;
293 		struct xdp_buff **bi;
294 		unsigned int size;
295 		u64 qword;
296 
297 		rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
298 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
299 
300 		/* This memory barrier is needed to keep us from reading
301 		 * any other fields out of the rx_desc until we have
302 		 * verified the descriptor has been written back.
303 		 */
304 		dma_rmb();
305 
306 		if (i40e_rx_is_programming_status(qword)) {
307 			i40e_clean_programming_status(rx_ring,
308 						      rx_desc->raw.qword[0],
309 						      qword);
310 			bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
311 			xsk_buff_free(*bi);
312 			*bi = NULL;
313 			cleaned_count++;
314 			i40e_inc_ntc(rx_ring);
315 			continue;
316 		}
317 
318 		size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
319 		       I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
320 		if (!size)
321 			break;
322 
323 		bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
324 		(*bi)->data_end = (*bi)->data + size;
325 		xsk_buff_dma_sync_for_cpu(*bi, rx_ring->xsk_pool);
326 
327 		xdp_res = i40e_run_xdp_zc(rx_ring, *bi);
328 		if (xdp_res) {
329 			if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR))
330 				xdp_xmit |= xdp_res;
331 			else
332 				xsk_buff_free(*bi);
333 
334 			*bi = NULL;
335 			total_rx_bytes += size;
336 			total_rx_packets++;
337 
338 			cleaned_count++;
339 			i40e_inc_ntc(rx_ring);
340 			continue;
341 		}
342 
343 		/* XDP_PASS path */
344 
345 		/* NB! We are not checking for errors using
346 		 * i40e_test_staterr with
347 		 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
348 		 * SBP is *not* set in PRT_SBPVSI (default not set).
349 		 */
350 		skb = i40e_construct_skb_zc(rx_ring, *bi);
351 		if (!skb) {
352 			rx_ring->rx_stats.alloc_buff_failed++;
353 			break;
354 		}
355 
356 		*bi = NULL;
357 		cleaned_count++;
358 		i40e_inc_ntc(rx_ring);
359 
360 		if (eth_skb_pad(skb))
361 			continue;
362 
363 		total_rx_bytes += skb->len;
364 		total_rx_packets++;
365 
366 		i40e_process_skb_fields(rx_ring, rx_desc, skb);
367 		napi_gro_receive(&rx_ring->q_vector->napi, skb);
368 	}
369 
370 	if (cleaned_count >= I40E_RX_BUFFER_WRITE)
371 		failure = !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
372 
373 	i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
374 	i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
375 
376 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
377 		if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
378 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
379 		else
380 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
381 
382 		return (int)total_rx_packets;
383 	}
384 	return failure ? budget : (int)total_rx_packets;
385 }
386 
387 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
388 			  unsigned int *total_bytes)
389 {
390 	struct i40e_tx_desc *tx_desc;
391 	dma_addr_t dma;
392 
393 	dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
394 	xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
395 
396 	tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
397 	tx_desc->buffer_addr = cpu_to_le64(dma);
398 	tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC | I40E_TX_DESC_CMD_EOP,
399 						  0, desc->len, 0);
400 
401 	*total_bytes += desc->len;
402 }
403 
404 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
405 				unsigned int *total_bytes)
406 {
407 	u16 ntu = xdp_ring->next_to_use;
408 	struct i40e_tx_desc *tx_desc;
409 	dma_addr_t dma;
410 	u32 i;
411 
412 	loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
413 		dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr);
414 		xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len);
415 
416 		tx_desc = I40E_TX_DESC(xdp_ring, ntu++);
417 		tx_desc->buffer_addr = cpu_to_le64(dma);
418 		tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC |
419 							  I40E_TX_DESC_CMD_EOP,
420 							  0, desc[i].len, 0);
421 
422 		*total_bytes += desc[i].len;
423 	}
424 
425 	xdp_ring->next_to_use = ntu;
426 }
427 
428 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
429 				 unsigned int *total_bytes)
430 {
431 	u32 batched, leftover, i;
432 
433 	batched = nb_pkts & ~(PKTS_PER_BATCH - 1);
434 	leftover = nb_pkts & (PKTS_PER_BATCH - 1);
435 	for (i = 0; i < batched; i += PKTS_PER_BATCH)
436 		i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
437 	for (i = batched; i < batched + leftover; i++)
438 		i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes);
439 }
440 
441 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
442 {
443 	u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1;
444 	struct i40e_tx_desc *tx_desc;
445 
446 	tx_desc = I40E_TX_DESC(xdp_ring, ntu);
447 	tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT);
448 }
449 
450 /**
451  * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
452  * @xdp_ring: XDP Tx ring
453  * @budget: NAPI budget
454  *
455  * Returns true if the work is finished.
456  **/
457 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
458 {
459 	struct xdp_desc *descs = xdp_ring->xsk_descs;
460 	u32 nb_pkts, nb_processed = 0;
461 	unsigned int total_bytes = 0;
462 
463 	nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, descs, budget);
464 	if (!nb_pkts)
465 		return false;
466 
467 	if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
468 		nb_processed = xdp_ring->count - xdp_ring->next_to_use;
469 		i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
470 		xdp_ring->next_to_use = 0;
471 	}
472 
473 	i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
474 			     &total_bytes);
475 
476 	/* Request an interrupt for the last frame and bump tail ptr. */
477 	i40e_set_rs_bit(xdp_ring);
478 	i40e_xdp_ring_update_tail(xdp_ring);
479 
480 	i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
481 
482 	return true;
483 }
484 
485 /**
486  * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
487  * @tx_ring: XDP Tx ring
488  * @tx_bi: Tx buffer info to clean
489  **/
490 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
491 				     struct i40e_tx_buffer *tx_bi)
492 {
493 	xdp_return_frame(tx_bi->xdpf);
494 	tx_ring->xdp_tx_active--;
495 	dma_unmap_single(tx_ring->dev,
496 			 dma_unmap_addr(tx_bi, dma),
497 			 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
498 	dma_unmap_len_set(tx_bi, len, 0);
499 }
500 
501 /**
502  * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
503  * @vsi: Current VSI
504  * @tx_ring: XDP Tx ring
505  *
506  * Returns true if cleanup/tranmission is done.
507  **/
508 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
509 {
510 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
511 	u32 i, completed_frames, xsk_frames = 0;
512 	u32 head_idx = i40e_get_head(tx_ring);
513 	struct i40e_tx_buffer *tx_bi;
514 	unsigned int ntc;
515 
516 	if (head_idx < tx_ring->next_to_clean)
517 		head_idx += tx_ring->count;
518 	completed_frames = head_idx - tx_ring->next_to_clean;
519 
520 	if (completed_frames == 0)
521 		goto out_xmit;
522 
523 	if (likely(!tx_ring->xdp_tx_active)) {
524 		xsk_frames = completed_frames;
525 		goto skip;
526 	}
527 
528 	ntc = tx_ring->next_to_clean;
529 
530 	for (i = 0; i < completed_frames; i++) {
531 		tx_bi = &tx_ring->tx_bi[ntc];
532 
533 		if (tx_bi->xdpf) {
534 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
535 			tx_bi->xdpf = NULL;
536 		} else {
537 			xsk_frames++;
538 		}
539 
540 		if (++ntc >= tx_ring->count)
541 			ntc = 0;
542 	}
543 
544 skip:
545 	tx_ring->next_to_clean += completed_frames;
546 	if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
547 		tx_ring->next_to_clean -= tx_ring->count;
548 
549 	if (xsk_frames)
550 		xsk_tx_completed(bp, xsk_frames);
551 
552 	i40e_arm_wb(tx_ring, vsi, completed_frames);
553 
554 out_xmit:
555 	if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
556 		xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
557 
558 	return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
559 }
560 
561 /**
562  * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
563  * @dev: the netdevice
564  * @queue_id: queue id to wake up
565  * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
566  *
567  * Returns <0 for errors, 0 otherwise.
568  **/
569 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
570 {
571 	struct i40e_netdev_priv *np = netdev_priv(dev);
572 	struct i40e_vsi *vsi = np->vsi;
573 	struct i40e_pf *pf = vsi->back;
574 	struct i40e_ring *ring;
575 
576 	if (test_bit(__I40E_CONFIG_BUSY, pf->state))
577 		return -EAGAIN;
578 
579 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
580 		return -ENETDOWN;
581 
582 	if (!i40e_enabled_xdp_vsi(vsi))
583 		return -ENXIO;
584 
585 	if (queue_id >= vsi->num_queue_pairs)
586 		return -ENXIO;
587 
588 	if (!vsi->xdp_rings[queue_id]->xsk_pool)
589 		return -ENXIO;
590 
591 	ring = vsi->xdp_rings[queue_id];
592 
593 	/* The idea here is that if NAPI is running, mark a miss, so
594 	 * it will run again. If not, trigger an interrupt and
595 	 * schedule the NAPI from interrupt context. If NAPI would be
596 	 * scheduled here, the interrupt affinity would not be
597 	 * honored.
598 	 */
599 	if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
600 		i40e_force_wb(vsi, ring->q_vector);
601 
602 	return 0;
603 }
604 
605 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
606 {
607 	u16 i;
608 
609 	for (i = 0; i < rx_ring->count; i++) {
610 		struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, i);
611 
612 		if (!rx_bi)
613 			continue;
614 
615 		xsk_buff_free(rx_bi);
616 		rx_bi = NULL;
617 	}
618 }
619 
620 /**
621  * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown
622  * @tx_ring: XDP Tx ring
623  **/
624 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
625 {
626 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
627 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
628 	struct i40e_tx_buffer *tx_bi;
629 	u32 xsk_frames = 0;
630 
631 	while (ntc != ntu) {
632 		tx_bi = &tx_ring->tx_bi[ntc];
633 
634 		if (tx_bi->xdpf)
635 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
636 		else
637 			xsk_frames++;
638 
639 		tx_bi->xdpf = NULL;
640 
641 		ntc++;
642 		if (ntc >= tx_ring->count)
643 			ntc = 0;
644 	}
645 
646 	if (xsk_frames)
647 		xsk_tx_completed(bp, xsk_frames);
648 }
649 
650 /**
651  * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
652  * buffer pool attached
653  * @vsi: vsi
654  *
655  * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
656  **/
657 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
658 {
659 	struct net_device *netdev = vsi->netdev;
660 	int i;
661 
662 	for (i = 0; i < vsi->num_queue_pairs; i++) {
663 		if (xsk_get_pool_from_qid(netdev, i))
664 			return true;
665 	}
666 
667 	return false;
668 }
669