xref: /linux/drivers/net/ethernet/intel/i40e/i40e_xsk.c (revision 2b64b2ed277ff23e785fbdb65098ee7e1252d64f)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
3 
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock.h>
6 #include <net/xdp.h>
7 
8 #include "i40e.h"
9 #include "i40e_txrx_common.h"
10 #include "i40e_xsk.h"
11 
12 /**
13  * i40e_xsk_umem_dma_map - DMA maps all UMEM memory for the netdev
14  * @vsi: Current VSI
15  * @umem: UMEM to DMA map
16  *
17  * Returns 0 on success, <0 on failure
18  **/
19 static int i40e_xsk_umem_dma_map(struct i40e_vsi *vsi, struct xdp_umem *umem)
20 {
21 	struct i40e_pf *pf = vsi->back;
22 	struct device *dev;
23 	unsigned int i, j;
24 	dma_addr_t dma;
25 
26 	dev = &pf->pdev->dev;
27 	for (i = 0; i < umem->npgs; i++) {
28 		dma = dma_map_page_attrs(dev, umem->pgs[i], 0, PAGE_SIZE,
29 					 DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR);
30 		if (dma_mapping_error(dev, dma))
31 			goto out_unmap;
32 
33 		umem->pages[i].dma = dma;
34 	}
35 
36 	return 0;
37 
38 out_unmap:
39 	for (j = 0; j < i; j++) {
40 		dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE,
41 				     DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR);
42 		umem->pages[i].dma = 0;
43 	}
44 
45 	return -1;
46 }
47 
48 /**
49  * i40e_xsk_umem_dma_unmap - DMA unmaps all UMEM memory for the netdev
50  * @vsi: Current VSI
51  * @umem: UMEM to DMA map
52  **/
53 static void i40e_xsk_umem_dma_unmap(struct i40e_vsi *vsi, struct xdp_umem *umem)
54 {
55 	struct i40e_pf *pf = vsi->back;
56 	struct device *dev;
57 	unsigned int i;
58 
59 	dev = &pf->pdev->dev;
60 
61 	for (i = 0; i < umem->npgs; i++) {
62 		dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE,
63 				     DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR);
64 
65 		umem->pages[i].dma = 0;
66 	}
67 }
68 
69 /**
70  * i40e_xsk_umem_enable - Enable/associate a UMEM to a certain ring/qid
71  * @vsi: Current VSI
72  * @umem: UMEM
73  * @qid: Rx ring to associate UMEM to
74  *
75  * Returns 0 on success, <0 on failure
76  **/
77 static int i40e_xsk_umem_enable(struct i40e_vsi *vsi, struct xdp_umem *umem,
78 				u16 qid)
79 {
80 	struct net_device *netdev = vsi->netdev;
81 	struct xdp_umem_fq_reuse *reuseq;
82 	bool if_running;
83 	int err;
84 
85 	if (vsi->type != I40E_VSI_MAIN)
86 		return -EINVAL;
87 
88 	if (qid >= vsi->num_queue_pairs)
89 		return -EINVAL;
90 
91 	if (qid >= netdev->real_num_rx_queues ||
92 	    qid >= netdev->real_num_tx_queues)
93 		return -EINVAL;
94 
95 	reuseq = xsk_reuseq_prepare(vsi->rx_rings[0]->count);
96 	if (!reuseq)
97 		return -ENOMEM;
98 
99 	xsk_reuseq_free(xsk_reuseq_swap(umem, reuseq));
100 
101 	err = i40e_xsk_umem_dma_map(vsi, umem);
102 	if (err)
103 		return err;
104 
105 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
106 
107 	if (if_running) {
108 		err = i40e_queue_pair_disable(vsi, qid);
109 		if (err)
110 			return err;
111 
112 		err = i40e_queue_pair_enable(vsi, qid);
113 		if (err)
114 			return err;
115 
116 		/* Kick start the NAPI context so that receiving will start */
117 		err = i40e_xsk_async_xmit(vsi->netdev, qid);
118 		if (err)
119 			return err;
120 	}
121 
122 	return 0;
123 }
124 
125 /**
126  * i40e_xsk_umem_disable - Disassociate a UMEM from a certain ring/qid
127  * @vsi: Current VSI
128  * @qid: Rx ring to associate UMEM to
129  *
130  * Returns 0 on success, <0 on failure
131  **/
132 static int i40e_xsk_umem_disable(struct i40e_vsi *vsi, u16 qid)
133 {
134 	struct net_device *netdev = vsi->netdev;
135 	struct xdp_umem *umem;
136 	bool if_running;
137 	int err;
138 
139 	umem = xdp_get_umem_from_qid(netdev, qid);
140 	if (!umem)
141 		return -EINVAL;
142 
143 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
144 
145 	if (if_running) {
146 		err = i40e_queue_pair_disable(vsi, qid);
147 		if (err)
148 			return err;
149 	}
150 
151 	i40e_xsk_umem_dma_unmap(vsi, umem);
152 
153 	if (if_running) {
154 		err = i40e_queue_pair_enable(vsi, qid);
155 		if (err)
156 			return err;
157 	}
158 
159 	return 0;
160 }
161 
162 /**
163  * i40e_xsk_umem_setup - Enable/disassociate a UMEM to/from a ring/qid
164  * @vsi: Current VSI
165  * @umem: UMEM to enable/associate to a ring, or NULL to disable
166  * @qid: Rx ring to (dis)associate UMEM (from)to
167  *
168  * This function enables or disables a UMEM to a certain ring.
169  *
170  * Returns 0 on success, <0 on failure
171  **/
172 int i40e_xsk_umem_setup(struct i40e_vsi *vsi, struct xdp_umem *umem,
173 			u16 qid)
174 {
175 	return umem ? i40e_xsk_umem_enable(vsi, umem, qid) :
176 		i40e_xsk_umem_disable(vsi, qid);
177 }
178 
179 /**
180  * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
181  * @rx_ring: Rx ring
182  * @xdp: xdp_buff used as input to the XDP program
183  *
184  * This function enables or disables a UMEM to a certain ring.
185  *
186  * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
187  **/
188 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp)
189 {
190 	int err, result = I40E_XDP_PASS;
191 	struct i40e_ring *xdp_ring;
192 	struct bpf_prog *xdp_prog;
193 	u32 act;
194 
195 	rcu_read_lock();
196 	/* NB! xdp_prog will always be !NULL, due to the fact that
197 	 * this path is enabled by setting an XDP program.
198 	 */
199 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
200 	act = bpf_prog_run_xdp(xdp_prog, xdp);
201 	xdp->handle += xdp->data - xdp->data_hard_start;
202 	switch (act) {
203 	case XDP_PASS:
204 		break;
205 	case XDP_TX:
206 		xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
207 		result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
208 		break;
209 	case XDP_REDIRECT:
210 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
211 		result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
212 		break;
213 	default:
214 		bpf_warn_invalid_xdp_action(act);
215 	case XDP_ABORTED:
216 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
217 		/* fallthrough -- handle aborts by dropping packet */
218 	case XDP_DROP:
219 		result = I40E_XDP_CONSUMED;
220 		break;
221 	}
222 	rcu_read_unlock();
223 	return result;
224 }
225 
226 /**
227  * i40e_alloc_buffer_zc - Allocates an i40e_rx_buffer
228  * @rx_ring: Rx ring
229  * @bi: Rx buffer to populate
230  *
231  * This function allocates an Rx buffer. The buffer can come from fill
232  * queue, or via the recycle queue (next_to_alloc).
233  *
234  * Returns true for a successful allocation, false otherwise
235  **/
236 static bool i40e_alloc_buffer_zc(struct i40e_ring *rx_ring,
237 				 struct i40e_rx_buffer *bi)
238 {
239 	struct xdp_umem *umem = rx_ring->xsk_umem;
240 	void *addr = bi->addr;
241 	u64 handle, hr;
242 
243 	if (addr) {
244 		rx_ring->rx_stats.page_reuse_count++;
245 		return true;
246 	}
247 
248 	if (!xsk_umem_peek_addr(umem, &handle)) {
249 		rx_ring->rx_stats.alloc_page_failed++;
250 		return false;
251 	}
252 
253 	hr = umem->headroom + XDP_PACKET_HEADROOM;
254 
255 	bi->dma = xdp_umem_get_dma(umem, handle);
256 	bi->dma += hr;
257 
258 	bi->addr = xdp_umem_get_data(umem, handle);
259 	bi->addr += hr;
260 
261 	bi->handle = handle + umem->headroom;
262 
263 	xsk_umem_discard_addr(umem);
264 	return true;
265 }
266 
267 /**
268  * i40e_alloc_buffer_slow_zc - Allocates an i40e_rx_buffer
269  * @rx_ring: Rx ring
270  * @bi: Rx buffer to populate
271  *
272  * This function allocates an Rx buffer. The buffer can come from fill
273  * queue, or via the reuse queue.
274  *
275  * Returns true for a successful allocation, false otherwise
276  **/
277 static bool i40e_alloc_buffer_slow_zc(struct i40e_ring *rx_ring,
278 				      struct i40e_rx_buffer *bi)
279 {
280 	struct xdp_umem *umem = rx_ring->xsk_umem;
281 	u64 handle, hr;
282 
283 	if (!xsk_umem_peek_addr_rq(umem, &handle)) {
284 		rx_ring->rx_stats.alloc_page_failed++;
285 		return false;
286 	}
287 
288 	handle &= rx_ring->xsk_umem->chunk_mask;
289 
290 	hr = umem->headroom + XDP_PACKET_HEADROOM;
291 
292 	bi->dma = xdp_umem_get_dma(umem, handle);
293 	bi->dma += hr;
294 
295 	bi->addr = xdp_umem_get_data(umem, handle);
296 	bi->addr += hr;
297 
298 	bi->handle = handle + umem->headroom;
299 
300 	xsk_umem_discard_addr_rq(umem);
301 	return true;
302 }
303 
304 static __always_inline bool
305 __i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count,
306 			   bool alloc(struct i40e_ring *rx_ring,
307 				      struct i40e_rx_buffer *bi))
308 {
309 	u16 ntu = rx_ring->next_to_use;
310 	union i40e_rx_desc *rx_desc;
311 	struct i40e_rx_buffer *bi;
312 	bool ok = true;
313 
314 	rx_desc = I40E_RX_DESC(rx_ring, ntu);
315 	bi = &rx_ring->rx_bi[ntu];
316 	do {
317 		if (!alloc(rx_ring, bi)) {
318 			ok = false;
319 			goto no_buffers;
320 		}
321 
322 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 0,
323 						 rx_ring->rx_buf_len,
324 						 DMA_BIDIRECTIONAL);
325 
326 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
327 
328 		rx_desc++;
329 		bi++;
330 		ntu++;
331 
332 		if (unlikely(ntu == rx_ring->count)) {
333 			rx_desc = I40E_RX_DESC(rx_ring, 0);
334 			bi = rx_ring->rx_bi;
335 			ntu = 0;
336 		}
337 
338 		rx_desc->wb.qword1.status_error_len = 0;
339 		count--;
340 	} while (count);
341 
342 no_buffers:
343 	if (rx_ring->next_to_use != ntu)
344 		i40e_release_rx_desc(rx_ring, ntu);
345 
346 	return ok;
347 }
348 
349 /**
350  * i40e_alloc_rx_buffers_zc - Allocates a number of Rx buffers
351  * @rx_ring: Rx ring
352  * @count: The number of buffers to allocate
353  *
354  * This function allocates a number of Rx buffers from the reuse queue
355  * or fill ring and places them on the Rx ring.
356  *
357  * Returns true for a successful allocation, false otherwise
358  **/
359 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
360 {
361 	return __i40e_alloc_rx_buffers_zc(rx_ring, count,
362 					  i40e_alloc_buffer_slow_zc);
363 }
364 
365 /**
366  * i40e_alloc_rx_buffers_fast_zc - Allocates a number of Rx buffers
367  * @rx_ring: Rx ring
368  * @count: The number of buffers to allocate
369  *
370  * This function allocates a number of Rx buffers from the fill ring
371  * or the internal recycle mechanism and places them on the Rx ring.
372  *
373  * Returns true for a successful allocation, false otherwise
374  **/
375 static bool i40e_alloc_rx_buffers_fast_zc(struct i40e_ring *rx_ring, u16 count)
376 {
377 	return __i40e_alloc_rx_buffers_zc(rx_ring, count,
378 					  i40e_alloc_buffer_zc);
379 }
380 
381 /**
382  * i40e_get_rx_buffer_zc - Return the current Rx buffer
383  * @rx_ring: Rx ring
384  * @size: The size of the rx buffer (read from descriptor)
385  *
386  * This function returns the current, received Rx buffer, and also
387  * does DMA synchronization.  the Rx ring.
388  *
389  * Returns the received Rx buffer
390  **/
391 static struct i40e_rx_buffer *i40e_get_rx_buffer_zc(struct i40e_ring *rx_ring,
392 						    const unsigned int size)
393 {
394 	struct i40e_rx_buffer *bi;
395 
396 	bi = &rx_ring->rx_bi[rx_ring->next_to_clean];
397 
398 	/* we are reusing so sync this buffer for CPU use */
399 	dma_sync_single_range_for_cpu(rx_ring->dev,
400 				      bi->dma, 0,
401 				      size,
402 				      DMA_BIDIRECTIONAL);
403 
404 	return bi;
405 }
406 
407 /**
408  * i40e_reuse_rx_buffer_zc - Recycle an Rx buffer
409  * @rx_ring: Rx ring
410  * @old_bi: The Rx buffer to recycle
411  *
412  * This function recycles a finished Rx buffer, and places it on the
413  * recycle queue (next_to_alloc).
414  **/
415 static void i40e_reuse_rx_buffer_zc(struct i40e_ring *rx_ring,
416 				    struct i40e_rx_buffer *old_bi)
417 {
418 	struct i40e_rx_buffer *new_bi = &rx_ring->rx_bi[rx_ring->next_to_alloc];
419 	unsigned long mask = (unsigned long)rx_ring->xsk_umem->chunk_mask;
420 	u64 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM;
421 	u16 nta = rx_ring->next_to_alloc;
422 
423 	/* update, and store next to alloc */
424 	nta++;
425 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
426 
427 	/* transfer page from old buffer to new buffer */
428 	new_bi->dma = old_bi->dma & mask;
429 	new_bi->dma += hr;
430 
431 	new_bi->addr = (void *)((unsigned long)old_bi->addr & mask);
432 	new_bi->addr += hr;
433 
434 	new_bi->handle = old_bi->handle & mask;
435 	new_bi->handle += rx_ring->xsk_umem->headroom;
436 
437 	old_bi->addr = NULL;
438 }
439 
440 /**
441  * i40e_zca_free - Free callback for MEM_TYPE_ZERO_COPY allocations
442  * @alloc: Zero-copy allocator
443  * @handle: Buffer handle
444  **/
445 void i40e_zca_free(struct zero_copy_allocator *alloc, unsigned long handle)
446 {
447 	struct i40e_rx_buffer *bi;
448 	struct i40e_ring *rx_ring;
449 	u64 hr, mask;
450 	u16 nta;
451 
452 	rx_ring = container_of(alloc, struct i40e_ring, zca);
453 	hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM;
454 	mask = rx_ring->xsk_umem->chunk_mask;
455 
456 	nta = rx_ring->next_to_alloc;
457 	bi = &rx_ring->rx_bi[nta];
458 
459 	nta++;
460 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
461 
462 	handle &= mask;
463 
464 	bi->dma = xdp_umem_get_dma(rx_ring->xsk_umem, handle);
465 	bi->dma += hr;
466 
467 	bi->addr = xdp_umem_get_data(rx_ring->xsk_umem, handle);
468 	bi->addr += hr;
469 
470 	bi->handle = (u64)handle + rx_ring->xsk_umem->headroom;
471 }
472 
473 /**
474  * i40e_construct_skb_zc - Create skbufff from zero-copy Rx buffer
475  * @rx_ring: Rx ring
476  * @bi: Rx buffer
477  * @xdp: xdp_buff
478  *
479  * This functions allocates a new skb from a zero-copy Rx buffer.
480  *
481  * Returns the skb, or NULL on failure.
482  **/
483 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
484 					     struct i40e_rx_buffer *bi,
485 					     struct xdp_buff *xdp)
486 {
487 	unsigned int metasize = xdp->data - xdp->data_meta;
488 	unsigned int datasize = xdp->data_end - xdp->data;
489 	struct sk_buff *skb;
490 
491 	/* allocate a skb to store the frags */
492 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
493 			       xdp->data_end - xdp->data_hard_start,
494 			       GFP_ATOMIC | __GFP_NOWARN);
495 	if (unlikely(!skb))
496 		return NULL;
497 
498 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
499 	memcpy(__skb_put(skb, datasize), xdp->data, datasize);
500 	if (metasize)
501 		skb_metadata_set(skb, metasize);
502 
503 	i40e_reuse_rx_buffer_zc(rx_ring, bi);
504 	return skb;
505 }
506 
507 /**
508  * i40e_inc_ntc: Advance the next_to_clean index
509  * @rx_ring: Rx ring
510  **/
511 static void i40e_inc_ntc(struct i40e_ring *rx_ring)
512 {
513 	u32 ntc = rx_ring->next_to_clean + 1;
514 
515 	ntc = (ntc < rx_ring->count) ? ntc : 0;
516 	rx_ring->next_to_clean = ntc;
517 	prefetch(I40E_RX_DESC(rx_ring, ntc));
518 }
519 
520 /**
521  * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
522  * @rx_ring: Rx ring
523  * @budget: NAPI budget
524  *
525  * Returns amount of work completed
526  **/
527 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
528 {
529 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
530 	u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
531 	unsigned int xdp_res, xdp_xmit = 0;
532 	bool failure = false;
533 	struct sk_buff *skb;
534 	struct xdp_buff xdp;
535 
536 	xdp.rxq = &rx_ring->xdp_rxq;
537 
538 	while (likely(total_rx_packets < (unsigned int)budget)) {
539 		struct i40e_rx_buffer *bi;
540 		union i40e_rx_desc *rx_desc;
541 		unsigned int size;
542 		u64 qword;
543 
544 		if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
545 			failure = failure ||
546 				  !i40e_alloc_rx_buffers_fast_zc(rx_ring,
547 								 cleaned_count);
548 			cleaned_count = 0;
549 		}
550 
551 		rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
552 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
553 
554 		/* This memory barrier is needed to keep us from reading
555 		 * any other fields out of the rx_desc until we have
556 		 * verified the descriptor has been written back.
557 		 */
558 		dma_rmb();
559 
560 		bi = i40e_clean_programming_status(rx_ring, rx_desc,
561 						   qword);
562 		if (unlikely(bi)) {
563 			i40e_reuse_rx_buffer_zc(rx_ring, bi);
564 			cleaned_count++;
565 			continue;
566 		}
567 
568 		size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
569 		       I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
570 		if (!size)
571 			break;
572 
573 		bi = i40e_get_rx_buffer_zc(rx_ring, size);
574 		xdp.data = bi->addr;
575 		xdp.data_meta = xdp.data;
576 		xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
577 		xdp.data_end = xdp.data + size;
578 		xdp.handle = bi->handle;
579 
580 		xdp_res = i40e_run_xdp_zc(rx_ring, &xdp);
581 		if (xdp_res) {
582 			if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) {
583 				xdp_xmit |= xdp_res;
584 				bi->addr = NULL;
585 			} else {
586 				i40e_reuse_rx_buffer_zc(rx_ring, bi);
587 			}
588 
589 			total_rx_bytes += size;
590 			total_rx_packets++;
591 
592 			cleaned_count++;
593 			i40e_inc_ntc(rx_ring);
594 			continue;
595 		}
596 
597 		/* XDP_PASS path */
598 
599 		/* NB! We are not checking for errors using
600 		 * i40e_test_staterr with
601 		 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
602 		 * SBP is *not* set in PRT_SBPVSI (default not set).
603 		 */
604 		skb = i40e_construct_skb_zc(rx_ring, bi, &xdp);
605 		if (!skb) {
606 			rx_ring->rx_stats.alloc_buff_failed++;
607 			break;
608 		}
609 
610 		cleaned_count++;
611 		i40e_inc_ntc(rx_ring);
612 
613 		if (eth_skb_pad(skb))
614 			continue;
615 
616 		total_rx_bytes += skb->len;
617 		total_rx_packets++;
618 
619 		i40e_process_skb_fields(rx_ring, rx_desc, skb);
620 		napi_gro_receive(&rx_ring->q_vector->napi, skb);
621 	}
622 
623 	i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
624 	i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
625 	return failure ? budget : (int)total_rx_packets;
626 }
627 
628 /**
629  * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
630  * @xdp_ring: XDP Tx ring
631  * @budget: NAPI budget
632  *
633  * Returns true if the work is finished.
634  **/
635 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
636 {
637 	struct i40e_tx_desc *tx_desc = NULL;
638 	struct i40e_tx_buffer *tx_bi;
639 	bool work_done = true;
640 	dma_addr_t dma;
641 	u32 len;
642 
643 	while (budget-- > 0) {
644 		if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
645 			xdp_ring->tx_stats.tx_busy++;
646 			work_done = false;
647 			break;
648 		}
649 
650 		if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &dma, &len))
651 			break;
652 
653 		dma_sync_single_for_device(xdp_ring->dev, dma, len,
654 					   DMA_BIDIRECTIONAL);
655 
656 		tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use];
657 		tx_bi->bytecount = len;
658 
659 		tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use);
660 		tx_desc->buffer_addr = cpu_to_le64(dma);
661 		tx_desc->cmd_type_offset_bsz =
662 			build_ctob(I40E_TX_DESC_CMD_ICRC
663 				   | I40E_TX_DESC_CMD_EOP,
664 				   0, len, 0);
665 
666 		xdp_ring->next_to_use++;
667 		if (xdp_ring->next_to_use == xdp_ring->count)
668 			xdp_ring->next_to_use = 0;
669 	}
670 
671 	if (tx_desc) {
672 		/* Request an interrupt for the last frame and bump tail ptr. */
673 		tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS <<
674 						 I40E_TXD_QW1_CMD_SHIFT);
675 		i40e_xdp_ring_update_tail(xdp_ring);
676 
677 		xsk_umem_consume_tx_done(xdp_ring->xsk_umem);
678 	}
679 
680 	return !!budget && work_done;
681 }
682 
683 /**
684  * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
685  * @tx_ring: XDP Tx ring
686  * @tx_bi: Tx buffer info to clean
687  **/
688 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
689 				     struct i40e_tx_buffer *tx_bi)
690 {
691 	xdp_return_frame(tx_bi->xdpf);
692 	dma_unmap_single(tx_ring->dev,
693 			 dma_unmap_addr(tx_bi, dma),
694 			 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
695 	dma_unmap_len_set(tx_bi, len, 0);
696 }
697 
698 /**
699  * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
700  * @tx_ring: XDP Tx ring
701  * @tx_bi: Tx buffer info to clean
702  *
703  * Returns true if cleanup/tranmission is done.
704  **/
705 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi,
706 			   struct i40e_ring *tx_ring, int napi_budget)
707 {
708 	unsigned int ntc, total_bytes = 0, budget = vsi->work_limit;
709 	u32 i, completed_frames, frames_ready, xsk_frames = 0;
710 	struct xdp_umem *umem = tx_ring->xsk_umem;
711 	u32 head_idx = i40e_get_head(tx_ring);
712 	bool work_done = true, xmit_done;
713 	struct i40e_tx_buffer *tx_bi;
714 
715 	if (head_idx < tx_ring->next_to_clean)
716 		head_idx += tx_ring->count;
717 	frames_ready = head_idx - tx_ring->next_to_clean;
718 
719 	if (frames_ready == 0) {
720 		goto out_xmit;
721 	} else if (frames_ready > budget) {
722 		completed_frames = budget;
723 		work_done = false;
724 	} else {
725 		completed_frames = frames_ready;
726 	}
727 
728 	ntc = tx_ring->next_to_clean;
729 
730 	for (i = 0; i < completed_frames; i++) {
731 		tx_bi = &tx_ring->tx_bi[ntc];
732 
733 		if (tx_bi->xdpf)
734 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
735 		else
736 			xsk_frames++;
737 
738 		tx_bi->xdpf = NULL;
739 		total_bytes += tx_bi->bytecount;
740 
741 		if (++ntc >= tx_ring->count)
742 			ntc = 0;
743 	}
744 
745 	tx_ring->next_to_clean += completed_frames;
746 	if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
747 		tx_ring->next_to_clean -= tx_ring->count;
748 
749 	if (xsk_frames)
750 		xsk_umem_complete_tx(umem, xsk_frames);
751 
752 	i40e_arm_wb(tx_ring, vsi, budget);
753 	i40e_update_tx_stats(tx_ring, completed_frames, total_bytes);
754 
755 out_xmit:
756 	xmit_done = i40e_xmit_zc(tx_ring, budget);
757 
758 	return work_done && xmit_done;
759 }
760 
761 /**
762  * i40e_xsk_async_xmit - Implements the ndo_xsk_async_xmit
763  * @dev: the netdevice
764  * @queue_id: queue id to wake up
765  *
766  * Returns <0 for errors, 0 otherwise.
767  **/
768 int i40e_xsk_async_xmit(struct net_device *dev, u32 queue_id)
769 {
770 	struct i40e_netdev_priv *np = netdev_priv(dev);
771 	struct i40e_vsi *vsi = np->vsi;
772 	struct i40e_ring *ring;
773 
774 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
775 		return -ENETDOWN;
776 
777 	if (!i40e_enabled_xdp_vsi(vsi))
778 		return -ENXIO;
779 
780 	if (queue_id >= vsi->num_queue_pairs)
781 		return -ENXIO;
782 
783 	if (!vsi->xdp_rings[queue_id]->xsk_umem)
784 		return -ENXIO;
785 
786 	ring = vsi->xdp_rings[queue_id];
787 
788 	/* The idea here is that if NAPI is running, mark a miss, so
789 	 * it will run again. If not, trigger an interrupt and
790 	 * schedule the NAPI from interrupt context. If NAPI would be
791 	 * scheduled here, the interrupt affinity would not be
792 	 * honored.
793 	 */
794 	if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
795 		i40e_force_wb(vsi, ring->q_vector);
796 
797 	return 0;
798 }
799 
800 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
801 {
802 	u16 i;
803 
804 	for (i = 0; i < rx_ring->count; i++) {
805 		struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
806 
807 		if (!rx_bi->addr)
808 			continue;
809 
810 		xsk_umem_fq_reuse(rx_ring->xsk_umem, rx_bi->handle);
811 		rx_bi->addr = NULL;
812 	}
813 }
814 
815 /**
816  * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown
817  * @xdp_ring: XDP Tx ring
818  **/
819 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
820 {
821 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
822 	struct xdp_umem *umem = tx_ring->xsk_umem;
823 	struct i40e_tx_buffer *tx_bi;
824 	u32 xsk_frames = 0;
825 
826 	while (ntc != ntu) {
827 		tx_bi = &tx_ring->tx_bi[ntc];
828 
829 		if (tx_bi->xdpf)
830 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
831 		else
832 			xsk_frames++;
833 
834 		tx_bi->xdpf = NULL;
835 
836 		ntc++;
837 		if (ntc >= tx_ring->count)
838 			ntc = 0;
839 	}
840 
841 	if (xsk_frames)
842 		xsk_umem_complete_tx(umem, xsk_frames);
843 }
844 
845 /**
846  * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have AF_XDP UMEM attached
847  * @vsi: vsi
848  *
849  * Returns true if any of the Rx rings has an AF_XDP UMEM attached
850  **/
851 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
852 {
853 	struct net_device *netdev = vsi->netdev;
854 	int i;
855 
856 	for (i = 0; i < vsi->num_queue_pairs; i++) {
857 		if (xdp_get_umem_from_qid(netdev, i))
858 			return true;
859 	}
860 
861 	return false;
862 }
863