xref: /linux/drivers/net/ethernet/intel/i40e/i40e_xsk.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
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 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
14 {
15 	memset(rx_ring->rx_bi_zc, 0,
16 	       sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
17 }
18 
19 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
20 {
21 	return &rx_ring->rx_bi_zc[idx];
22 }
23 
24 /**
25  * i40e_realloc_rx_xdp_bi - reallocate SW ring for either XSK or normal buffer
26  * @rx_ring: Current rx ring
27  * @pool_present: is pool for XSK present
28  *
29  * Try allocating memory and return ENOMEM, if failed to allocate.
30  * If allocation was successful, substitute buffer with allocated one.
31  * Returns 0 on success, negative on failure
32  */
33 static int i40e_realloc_rx_xdp_bi(struct i40e_ring *rx_ring, bool pool_present)
34 {
35 	size_t elem_size = pool_present ? sizeof(*rx_ring->rx_bi_zc) :
36 					  sizeof(*rx_ring->rx_bi);
37 	void *sw_ring = kcalloc(rx_ring->count, elem_size, GFP_KERNEL);
38 
39 	if (!sw_ring)
40 		return -ENOMEM;
41 
42 	if (pool_present) {
43 		kfree(rx_ring->rx_bi);
44 		rx_ring->rx_bi = NULL;
45 		rx_ring->rx_bi_zc = sw_ring;
46 	} else {
47 		kfree(rx_ring->rx_bi_zc);
48 		rx_ring->rx_bi_zc = NULL;
49 		rx_ring->rx_bi = sw_ring;
50 	}
51 	return 0;
52 }
53 
54 /**
55  * i40e_realloc_rx_bi_zc - reallocate rx SW rings
56  * @vsi: Current VSI
57  * @zc: is zero copy set
58  *
59  * Reallocate buffer for rx_rings that might be used by XSK.
60  * XDP requires more memory, than rx_buf provides.
61  * Returns 0 on success, negative on failure
62  */
63 int i40e_realloc_rx_bi_zc(struct i40e_vsi *vsi, bool zc)
64 {
65 	struct i40e_ring *rx_ring;
66 	unsigned long q;
67 
68 	for_each_set_bit(q, vsi->af_xdp_zc_qps, vsi->alloc_queue_pairs) {
69 		rx_ring = vsi->rx_rings[q];
70 		if (i40e_realloc_rx_xdp_bi(rx_ring, zc))
71 			return -ENOMEM;
72 	}
73 	return 0;
74 }
75 
76 /**
77  * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
78  * certain ring/qid
79  * @vsi: Current VSI
80  * @pool: buffer pool
81  * @qid: Rx ring to associate buffer pool with
82  *
83  * Returns 0 on success, <0 on failure
84  **/
85 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
86 				struct xsk_buff_pool *pool,
87 				u16 qid)
88 {
89 	struct net_device *netdev = vsi->netdev;
90 	bool if_running;
91 	int err;
92 
93 	if (vsi->type != I40E_VSI_MAIN)
94 		return -EINVAL;
95 
96 	if (qid >= vsi->num_queue_pairs)
97 		return -EINVAL;
98 
99 	if (qid >= netdev->real_num_rx_queues ||
100 	    qid >= netdev->real_num_tx_queues)
101 		return -EINVAL;
102 
103 	err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
104 	if (err)
105 		return err;
106 
107 	set_bit(qid, vsi->af_xdp_zc_qps);
108 
109 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
110 
111 	if (if_running) {
112 		err = i40e_queue_pair_disable(vsi, qid);
113 		if (err)
114 			return err;
115 
116 		err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], true);
117 		if (err)
118 			return err;
119 
120 		err = i40e_queue_pair_enable(vsi, qid);
121 		if (err)
122 			return err;
123 
124 		/* Kick start the NAPI context so that receiving will start */
125 		err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
126 		if (err)
127 			return err;
128 	}
129 
130 	return 0;
131 }
132 
133 /**
134  * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
135  * certain ring/qid
136  * @vsi: Current VSI
137  * @qid: Rx ring to associate buffer pool with
138  *
139  * Returns 0 on success, <0 on failure
140  **/
141 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
142 {
143 	struct net_device *netdev = vsi->netdev;
144 	struct xsk_buff_pool *pool;
145 	bool if_running;
146 	int err;
147 
148 	pool = xsk_get_pool_from_qid(netdev, qid);
149 	if (!pool)
150 		return -EINVAL;
151 
152 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
153 
154 	if (if_running) {
155 		err = i40e_queue_pair_disable(vsi, qid);
156 		if (err)
157 			return err;
158 	}
159 
160 	clear_bit(qid, vsi->af_xdp_zc_qps);
161 	xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
162 
163 	if (if_running) {
164 		err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], false);
165 		if (err)
166 			return err;
167 		err = i40e_queue_pair_enable(vsi, qid);
168 		if (err)
169 			return err;
170 	}
171 
172 	return 0;
173 }
174 
175 /**
176  * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
177  * a ring/qid
178  * @vsi: Current VSI
179  * @pool: Buffer pool to enable/associate to a ring, or NULL to disable
180  * @qid: Rx ring to (dis)associate buffer pool (from)to
181  *
182  * This function enables or disables a buffer pool to a certain ring.
183  *
184  * Returns 0 on success, <0 on failure
185  **/
186 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
187 			u16 qid)
188 {
189 	return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
190 		i40e_xsk_pool_disable(vsi, qid);
191 }
192 
193 /**
194  * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
195  * @rx_ring: Rx ring
196  * @xdp: xdp_buff used as input to the XDP program
197  * @xdp_prog: XDP program to run
198  *
199  * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
200  **/
201 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp,
202 			   struct bpf_prog *xdp_prog)
203 {
204 	int err, result = I40E_XDP_PASS;
205 	struct i40e_ring *xdp_ring;
206 	u32 act;
207 
208 	act = bpf_prog_run_xdp(xdp_prog, xdp);
209 
210 	if (likely(act == XDP_REDIRECT)) {
211 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
212 		if (!err)
213 			return I40E_XDP_REDIR;
214 		if (xsk_uses_need_wakeup(rx_ring->xsk_pool) && err == -ENOBUFS)
215 			result = I40E_XDP_EXIT;
216 		else
217 			result = I40E_XDP_CONSUMED;
218 		goto out_failure;
219 	}
220 
221 	switch (act) {
222 	case XDP_PASS:
223 		break;
224 	case XDP_TX:
225 		xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
226 		result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
227 		if (result == I40E_XDP_CONSUMED)
228 			goto out_failure;
229 		break;
230 	case XDP_DROP:
231 		result = I40E_XDP_CONSUMED;
232 		break;
233 	default:
234 		bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
235 		fallthrough;
236 	case XDP_ABORTED:
237 		result = I40E_XDP_CONSUMED;
238 out_failure:
239 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
240 	}
241 	return result;
242 }
243 
244 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
245 {
246 	u16 ntu = rx_ring->next_to_use;
247 	union i40e_rx_desc *rx_desc;
248 	struct xdp_buff **xdp;
249 	u32 nb_buffs, i;
250 	dma_addr_t dma;
251 
252 	rx_desc = I40E_RX_DESC(rx_ring, ntu);
253 	xdp = i40e_rx_bi(rx_ring, ntu);
254 
255 	nb_buffs = min_t(u16, count, rx_ring->count - ntu);
256 	nb_buffs = xsk_buff_alloc_batch(rx_ring->xsk_pool, xdp, nb_buffs);
257 	if (!nb_buffs)
258 		return false;
259 
260 	i = nb_buffs;
261 	while (i--) {
262 		dma = xsk_buff_xdp_get_dma(*xdp);
263 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
264 		rx_desc->read.hdr_addr = 0;
265 
266 		rx_desc++;
267 		xdp++;
268 	}
269 
270 	ntu += nb_buffs;
271 	if (ntu == rx_ring->count) {
272 		rx_desc = I40E_RX_DESC(rx_ring, 0);
273 		ntu = 0;
274 	}
275 
276 	/* clear the status bits for the next_to_use descriptor */
277 	rx_desc->wb.qword1.status_error_len = 0;
278 	i40e_release_rx_desc(rx_ring, ntu);
279 
280 	return count == nb_buffs;
281 }
282 
283 /**
284  * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
285  * @rx_ring: Rx ring
286  * @xdp: xdp_buff
287  *
288  * This functions allocates a new skb from a zero-copy Rx buffer.
289  *
290  * Returns the skb, or NULL on failure.
291  **/
292 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
293 					     struct xdp_buff *xdp)
294 {
295 	unsigned int totalsize = xdp->data_end - xdp->data_meta;
296 	unsigned int metasize = xdp->data - xdp->data_meta;
297 	struct skb_shared_info *sinfo = NULL;
298 	struct sk_buff *skb;
299 	u32 nr_frags = 0;
300 
301 	if (unlikely(xdp_buff_has_frags(xdp))) {
302 		sinfo = xdp_get_shared_info_from_buff(xdp);
303 		nr_frags = sinfo->nr_frags;
304 	}
305 	net_prefetch(xdp->data_meta);
306 
307 	/* allocate a skb to store the frags */
308 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
309 			       GFP_ATOMIC | __GFP_NOWARN);
310 	if (unlikely(!skb))
311 		goto out;
312 
313 	memcpy(__skb_put(skb, totalsize), xdp->data_meta,
314 	       ALIGN(totalsize, sizeof(long)));
315 
316 	if (metasize) {
317 		skb_metadata_set(skb, metasize);
318 		__skb_pull(skb, metasize);
319 	}
320 
321 	if (likely(!xdp_buff_has_frags(xdp)))
322 		goto out;
323 
324 	for (int i = 0; i < nr_frags; i++) {
325 		struct skb_shared_info *skinfo = skb_shinfo(skb);
326 		skb_frag_t *frag = &sinfo->frags[i];
327 		struct page *page;
328 		void *addr;
329 
330 		page = dev_alloc_page();
331 		if (!page) {
332 			dev_kfree_skb(skb);
333 			return NULL;
334 		}
335 		addr = page_to_virt(page);
336 
337 		memcpy(addr, skb_frag_page(frag), skb_frag_size(frag));
338 
339 		__skb_fill_page_desc_noacc(skinfo, skinfo->nr_frags++,
340 					   addr, 0, skb_frag_size(frag));
341 	}
342 
343 out:
344 	xsk_buff_free(xdp);
345 	return skb;
346 }
347 
348 static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring,
349 				      struct xdp_buff *xdp_buff,
350 				      union i40e_rx_desc *rx_desc,
351 				      unsigned int *rx_packets,
352 				      unsigned int *rx_bytes,
353 				      unsigned int xdp_res,
354 				      bool *failure)
355 {
356 	struct sk_buff *skb;
357 
358 	*rx_packets = 1;
359 	*rx_bytes = xdp_get_buff_len(xdp_buff);
360 
361 	if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX)
362 		return;
363 
364 	if (xdp_res == I40E_XDP_EXIT) {
365 		*failure = true;
366 		return;
367 	}
368 
369 	if (xdp_res == I40E_XDP_CONSUMED) {
370 		xsk_buff_free(xdp_buff);
371 		return;
372 	}
373 	if (xdp_res == I40E_XDP_PASS) {
374 		/* NB! We are not checking for errors using
375 		 * i40e_test_staterr with
376 		 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
377 		 * SBP is *not* set in PRT_SBPVSI (default not set).
378 		 */
379 		skb = i40e_construct_skb_zc(rx_ring, xdp_buff);
380 		if (!skb) {
381 			rx_ring->rx_stats.alloc_buff_failed++;
382 			*rx_packets = 0;
383 			*rx_bytes = 0;
384 			return;
385 		}
386 
387 		if (eth_skb_pad(skb)) {
388 			*rx_packets = 0;
389 			*rx_bytes = 0;
390 			return;
391 		}
392 
393 		i40e_process_skb_fields(rx_ring, rx_desc, skb);
394 		napi_gro_receive(&rx_ring->q_vector->napi, skb);
395 		return;
396 	}
397 
398 	/* Should never get here, as all valid cases have been handled already.
399 	 */
400 	WARN_ON_ONCE(1);
401 }
402 
403 static int
404 i40e_add_xsk_frag(struct i40e_ring *rx_ring, struct xdp_buff *first,
405 		  struct xdp_buff *xdp, const unsigned int size)
406 {
407 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(first);
408 
409 	if (!xdp_buff_has_frags(first)) {
410 		sinfo->nr_frags = 0;
411 		sinfo->xdp_frags_size = 0;
412 		xdp_buff_set_frags_flag(first);
413 	}
414 
415 	if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS)) {
416 		xsk_buff_free(first);
417 		return -ENOMEM;
418 	}
419 
420 	__skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++,
421 				   virt_to_page(xdp->data_hard_start), 0, size);
422 	sinfo->xdp_frags_size += size;
423 	xsk_buff_add_frag(xdp);
424 
425 	return 0;
426 }
427 
428 /**
429  * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
430  * @rx_ring: Rx ring
431  * @budget: NAPI budget
432  *
433  * Returns amount of work completed
434  **/
435 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
436 {
437 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
438 	u16 next_to_process = rx_ring->next_to_process;
439 	u16 next_to_clean = rx_ring->next_to_clean;
440 	u16 count_mask = rx_ring->count - 1;
441 	unsigned int xdp_res, xdp_xmit = 0;
442 	struct xdp_buff *first = NULL;
443 	struct bpf_prog *xdp_prog;
444 	bool failure = false;
445 	u16 cleaned_count;
446 
447 	if (next_to_process != next_to_clean)
448 		first = *i40e_rx_bi(rx_ring, next_to_clean);
449 
450 	/* NB! xdp_prog will always be !NULL, due to the fact that
451 	 * this path is enabled by setting an XDP program.
452 	 */
453 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
454 
455 	while (likely(total_rx_packets < (unsigned int)budget)) {
456 		union i40e_rx_desc *rx_desc;
457 		unsigned int rx_packets;
458 		unsigned int rx_bytes;
459 		struct xdp_buff *bi;
460 		unsigned int size;
461 		u64 qword;
462 
463 		rx_desc = I40E_RX_DESC(rx_ring, next_to_process);
464 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
465 
466 		/* This memory barrier is needed to keep us from reading
467 		 * any other fields out of the rx_desc until we have
468 		 * verified the descriptor has been written back.
469 		 */
470 		dma_rmb();
471 
472 		if (i40e_rx_is_programming_status(qword)) {
473 			i40e_clean_programming_status(rx_ring,
474 						      rx_desc->raw.qword[0],
475 						      qword);
476 			bi = *i40e_rx_bi(rx_ring, next_to_process);
477 			xsk_buff_free(bi);
478 			next_to_process = (next_to_process + 1) & count_mask;
479 			continue;
480 		}
481 
482 		size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
483 		       I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
484 		if (!size)
485 			break;
486 
487 		bi = *i40e_rx_bi(rx_ring, next_to_process);
488 		xsk_buff_set_size(bi, size);
489 		xsk_buff_dma_sync_for_cpu(bi, rx_ring->xsk_pool);
490 
491 		if (!first)
492 			first = bi;
493 		else if (i40e_add_xsk_frag(rx_ring, first, bi, size))
494 			break;
495 
496 		next_to_process = (next_to_process + 1) & count_mask;
497 
498 		if (i40e_is_non_eop(rx_ring, rx_desc))
499 			continue;
500 
501 		xdp_res = i40e_run_xdp_zc(rx_ring, first, xdp_prog);
502 		i40e_handle_xdp_result_zc(rx_ring, first, rx_desc, &rx_packets,
503 					  &rx_bytes, xdp_res, &failure);
504 		first->flags = 0;
505 		next_to_clean = next_to_process;
506 		if (failure)
507 			break;
508 		total_rx_packets += rx_packets;
509 		total_rx_bytes += rx_bytes;
510 		xdp_xmit |= xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR);
511 		first = NULL;
512 	}
513 
514 	rx_ring->next_to_clean = next_to_clean;
515 	rx_ring->next_to_process = next_to_process;
516 	cleaned_count = (next_to_clean - rx_ring->next_to_use - 1) & count_mask;
517 
518 	if (cleaned_count >= I40E_RX_BUFFER_WRITE)
519 		failure |= !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
520 
521 	i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
522 	i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
523 
524 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
525 		if (failure || next_to_clean == rx_ring->next_to_use)
526 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
527 		else
528 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
529 
530 		return (int)total_rx_packets;
531 	}
532 	return failure ? budget : (int)total_rx_packets;
533 }
534 
535 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
536 			  unsigned int *total_bytes)
537 {
538 	u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(desc);
539 	struct i40e_tx_desc *tx_desc;
540 	dma_addr_t dma;
541 
542 	dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
543 	xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
544 
545 	tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
546 	tx_desc->buffer_addr = cpu_to_le64(dma);
547 	tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc->len, 0);
548 
549 	*total_bytes += desc->len;
550 }
551 
552 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
553 				unsigned int *total_bytes)
554 {
555 	u16 ntu = xdp_ring->next_to_use;
556 	struct i40e_tx_desc *tx_desc;
557 	dma_addr_t dma;
558 	u32 i;
559 
560 	loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
561 		u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(&desc[i]);
562 
563 		dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr);
564 		xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len);
565 
566 		tx_desc = I40E_TX_DESC(xdp_ring, ntu++);
567 		tx_desc->buffer_addr = cpu_to_le64(dma);
568 		tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc[i].len, 0);
569 
570 		*total_bytes += desc[i].len;
571 	}
572 
573 	xdp_ring->next_to_use = ntu;
574 }
575 
576 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
577 				 unsigned int *total_bytes)
578 {
579 	u32 batched, leftover, i;
580 
581 	batched = nb_pkts & ~(PKTS_PER_BATCH - 1);
582 	leftover = nb_pkts & (PKTS_PER_BATCH - 1);
583 	for (i = 0; i < batched; i += PKTS_PER_BATCH)
584 		i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
585 	for (i = batched; i < batched + leftover; i++)
586 		i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes);
587 }
588 
589 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
590 {
591 	u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1;
592 	struct i40e_tx_desc *tx_desc;
593 
594 	tx_desc = I40E_TX_DESC(xdp_ring, ntu);
595 	tx_desc->cmd_type_offset_bsz |= cpu_to_le64(I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT);
596 }
597 
598 /**
599  * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
600  * @xdp_ring: XDP Tx ring
601  * @budget: NAPI budget
602  *
603  * Returns true if the work is finished.
604  **/
605 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
606 {
607 	struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs;
608 	u32 nb_pkts, nb_processed = 0;
609 	unsigned int total_bytes = 0;
610 
611 	nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget);
612 	if (!nb_pkts)
613 		return true;
614 
615 	if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
616 		nb_processed = xdp_ring->count - xdp_ring->next_to_use;
617 		i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
618 		xdp_ring->next_to_use = 0;
619 	}
620 
621 	i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
622 			     &total_bytes);
623 
624 	/* Request an interrupt for the last frame and bump tail ptr. */
625 	i40e_set_rs_bit(xdp_ring);
626 	i40e_xdp_ring_update_tail(xdp_ring);
627 
628 	i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
629 
630 	return nb_pkts < budget;
631 }
632 
633 /**
634  * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
635  * @tx_ring: XDP Tx ring
636  * @tx_bi: Tx buffer info to clean
637  **/
638 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
639 				     struct i40e_tx_buffer *tx_bi)
640 {
641 	xdp_return_frame(tx_bi->xdpf);
642 	tx_ring->xdp_tx_active--;
643 	dma_unmap_single(tx_ring->dev,
644 			 dma_unmap_addr(tx_bi, dma),
645 			 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
646 	dma_unmap_len_set(tx_bi, len, 0);
647 }
648 
649 /**
650  * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
651  * @vsi: Current VSI
652  * @tx_ring: XDP Tx ring
653  *
654  * Returns true if cleanup/transmission is done.
655  **/
656 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
657 {
658 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
659 	u32 i, completed_frames, xsk_frames = 0;
660 	u32 head_idx = i40e_get_head(tx_ring);
661 	struct i40e_tx_buffer *tx_bi;
662 	unsigned int ntc;
663 
664 	if (head_idx < tx_ring->next_to_clean)
665 		head_idx += tx_ring->count;
666 	completed_frames = head_idx - tx_ring->next_to_clean;
667 
668 	if (completed_frames == 0)
669 		goto out_xmit;
670 
671 	if (likely(!tx_ring->xdp_tx_active)) {
672 		xsk_frames = completed_frames;
673 		goto skip;
674 	}
675 
676 	ntc = tx_ring->next_to_clean;
677 
678 	for (i = 0; i < completed_frames; i++) {
679 		tx_bi = &tx_ring->tx_bi[ntc];
680 
681 		if (tx_bi->xdpf) {
682 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
683 			tx_bi->xdpf = NULL;
684 		} else {
685 			xsk_frames++;
686 		}
687 
688 		if (++ntc >= tx_ring->count)
689 			ntc = 0;
690 	}
691 
692 skip:
693 	tx_ring->next_to_clean += completed_frames;
694 	if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
695 		tx_ring->next_to_clean -= tx_ring->count;
696 
697 	if (xsk_frames)
698 		xsk_tx_completed(bp, xsk_frames);
699 
700 	i40e_arm_wb(tx_ring, vsi, completed_frames);
701 
702 out_xmit:
703 	if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
704 		xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
705 
706 	return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
707 }
708 
709 /**
710  * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
711  * @dev: the netdevice
712  * @queue_id: queue id to wake up
713  * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
714  *
715  * Returns <0 for errors, 0 otherwise.
716  **/
717 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
718 {
719 	struct i40e_netdev_priv *np = netdev_priv(dev);
720 	struct i40e_vsi *vsi = np->vsi;
721 	struct i40e_pf *pf = vsi->back;
722 	struct i40e_ring *ring;
723 
724 	if (test_bit(__I40E_CONFIG_BUSY, pf->state))
725 		return -EAGAIN;
726 
727 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
728 		return -ENETDOWN;
729 
730 	if (!i40e_enabled_xdp_vsi(vsi))
731 		return -EINVAL;
732 
733 	if (queue_id >= vsi->num_queue_pairs)
734 		return -EINVAL;
735 
736 	if (!vsi->xdp_rings[queue_id]->xsk_pool)
737 		return -EINVAL;
738 
739 	ring = vsi->xdp_rings[queue_id];
740 
741 	/* The idea here is that if NAPI is running, mark a miss, so
742 	 * it will run again. If not, trigger an interrupt and
743 	 * schedule the NAPI from interrupt context. If NAPI would be
744 	 * scheduled here, the interrupt affinity would not be
745 	 * honored.
746 	 */
747 	if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
748 		i40e_force_wb(vsi, ring->q_vector);
749 
750 	return 0;
751 }
752 
753 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
754 {
755 	u16 count_mask = rx_ring->count - 1;
756 	u16 ntc = rx_ring->next_to_clean;
757 	u16 ntu = rx_ring->next_to_use;
758 
759 	for ( ; ntc != ntu; ntc = (ntc + 1)  & count_mask) {
760 		struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc);
761 
762 		xsk_buff_free(rx_bi);
763 	}
764 }
765 
766 /**
767  * i40e_xsk_clean_tx_ring - Clean the XDP Tx ring on shutdown
768  * @tx_ring: XDP Tx ring
769  **/
770 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
771 {
772 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
773 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
774 	struct i40e_tx_buffer *tx_bi;
775 	u32 xsk_frames = 0;
776 
777 	while (ntc != ntu) {
778 		tx_bi = &tx_ring->tx_bi[ntc];
779 
780 		if (tx_bi->xdpf)
781 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
782 		else
783 			xsk_frames++;
784 
785 		tx_bi->xdpf = NULL;
786 
787 		ntc++;
788 		if (ntc >= tx_ring->count)
789 			ntc = 0;
790 	}
791 
792 	if (xsk_frames)
793 		xsk_tx_completed(bp, xsk_frames);
794 }
795 
796 /**
797  * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
798  * buffer pool attached
799  * @vsi: vsi
800  *
801  * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
802  **/
803 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
804 {
805 	struct net_device *netdev = vsi->netdev;
806 	int i;
807 
808 	for (i = 0; i < vsi->num_queue_pairs; i++) {
809 		if (xsk_get_pool_from_qid(netdev, i))
810 			return true;
811 	}
812 
813 	return false;
814 }
815