1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock_drv.h>
6 #include <net/xdp.h>
7 #include "ice.h"
8 #include "ice_base.h"
9 #include "ice_type.h"
10 #include "ice_xsk.h"
11 #include "ice_txrx.h"
12 #include "ice_txrx_lib.h"
13 #include "ice_lib.h"
14 
15 /**
16  * ice_qp_reset_stats - Resets all stats for rings of given index
17  * @vsi: VSI that contains rings of interest
18  * @q_idx: ring index in array
19  */
20 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx)
21 {
22 	memset(&vsi->rx_rings[q_idx]->rx_stats, 0,
23 	       sizeof(vsi->rx_rings[q_idx]->rx_stats));
24 	memset(&vsi->tx_rings[q_idx]->stats, 0,
25 	       sizeof(vsi->tx_rings[q_idx]->stats));
26 	if (ice_is_xdp_ena_vsi(vsi))
27 		memset(&vsi->xdp_rings[q_idx]->stats, 0,
28 		       sizeof(vsi->xdp_rings[q_idx]->stats));
29 }
30 
31 /**
32  * ice_qp_clean_rings - Cleans all the rings of a given index
33  * @vsi: VSI that contains rings of interest
34  * @q_idx: ring index in array
35  */
36 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx)
37 {
38 	ice_clean_tx_ring(vsi->tx_rings[q_idx]);
39 	if (ice_is_xdp_ena_vsi(vsi))
40 		ice_clean_tx_ring(vsi->xdp_rings[q_idx]);
41 	ice_clean_rx_ring(vsi->rx_rings[q_idx]);
42 }
43 
44 /**
45  * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector
46  * @vsi: VSI that has netdev
47  * @q_vector: q_vector that has NAPI context
48  * @enable: true for enable, false for disable
49  */
50 static void
51 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector,
52 		     bool enable)
53 {
54 	if (!vsi->netdev || !q_vector)
55 		return;
56 
57 	if (enable)
58 		napi_enable(&q_vector->napi);
59 	else
60 		napi_disable(&q_vector->napi);
61 }
62 
63 /**
64  * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring
65  * @vsi: the VSI that contains queue vector being un-configured
66  * @rx_ring: Rx ring that will have its IRQ disabled
67  * @q_vector: queue vector
68  */
69 static void
70 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_rx_ring *rx_ring,
71 		 struct ice_q_vector *q_vector)
72 {
73 	struct ice_pf *pf = vsi->back;
74 	struct ice_hw *hw = &pf->hw;
75 	int base = vsi->base_vector;
76 	u16 reg;
77 	u32 val;
78 
79 	/* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle
80 	 * here only QINT_RQCTL
81 	 */
82 	reg = rx_ring->reg_idx;
83 	val = rd32(hw, QINT_RQCTL(reg));
84 	val &= ~QINT_RQCTL_CAUSE_ENA_M;
85 	wr32(hw, QINT_RQCTL(reg), val);
86 
87 	if (q_vector) {
88 		u16 v_idx = q_vector->v_idx;
89 
90 		wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0);
91 		ice_flush(hw);
92 		synchronize_irq(pf->msix_entries[v_idx + base].vector);
93 	}
94 }
95 
96 /**
97  * ice_qvec_cfg_msix - Enable IRQ for given queue vector
98  * @vsi: the VSI that contains queue vector
99  * @q_vector: queue vector
100  */
101 static void
102 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
103 {
104 	u16 reg_idx = q_vector->reg_idx;
105 	struct ice_pf *pf = vsi->back;
106 	struct ice_hw *hw = &pf->hw;
107 	struct ice_tx_ring *tx_ring;
108 	struct ice_rx_ring *rx_ring;
109 
110 	ice_cfg_itr(hw, q_vector);
111 
112 	ice_for_each_tx_ring(tx_ring, q_vector->tx)
113 		ice_cfg_txq_interrupt(vsi, tx_ring->reg_idx, reg_idx,
114 				      q_vector->tx.itr_idx);
115 
116 	ice_for_each_rx_ring(rx_ring, q_vector->rx)
117 		ice_cfg_rxq_interrupt(vsi, rx_ring->reg_idx, reg_idx,
118 				      q_vector->rx.itr_idx);
119 
120 	ice_flush(hw);
121 }
122 
123 /**
124  * ice_qvec_ena_irq - Enable IRQ for given queue vector
125  * @vsi: the VSI that contains queue vector
126  * @q_vector: queue vector
127  */
128 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
129 {
130 	struct ice_pf *pf = vsi->back;
131 	struct ice_hw *hw = &pf->hw;
132 
133 	ice_irq_dynamic_ena(hw, vsi, q_vector);
134 
135 	ice_flush(hw);
136 }
137 
138 /**
139  * ice_qp_dis - Disables a queue pair
140  * @vsi: VSI of interest
141  * @q_idx: ring index in array
142  *
143  * Returns 0 on success, negative on failure.
144  */
145 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx)
146 {
147 	struct ice_txq_meta txq_meta = { };
148 	struct ice_q_vector *q_vector;
149 	struct ice_tx_ring *tx_ring;
150 	struct ice_rx_ring *rx_ring;
151 	int timeout = 50;
152 	int err;
153 
154 	if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
155 		return -EINVAL;
156 
157 	tx_ring = vsi->tx_rings[q_idx];
158 	rx_ring = vsi->rx_rings[q_idx];
159 	q_vector = rx_ring->q_vector;
160 
161 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state)) {
162 		timeout--;
163 		if (!timeout)
164 			return -EBUSY;
165 		usleep_range(1000, 2000);
166 	}
167 	netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
168 
169 	ice_qvec_dis_irq(vsi, rx_ring, q_vector);
170 
171 	ice_fill_txq_meta(vsi, tx_ring, &txq_meta);
172 	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta);
173 	if (err)
174 		return err;
175 	if (ice_is_xdp_ena_vsi(vsi)) {
176 		struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
177 
178 		memset(&txq_meta, 0, sizeof(txq_meta));
179 		ice_fill_txq_meta(vsi, xdp_ring, &txq_meta);
180 		err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring,
181 					   &txq_meta);
182 		if (err)
183 			return err;
184 	}
185 	err = ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, true);
186 	if (err)
187 		return err;
188 
189 	ice_qvec_toggle_napi(vsi, q_vector, false);
190 	ice_qp_clean_rings(vsi, q_idx);
191 	ice_qp_reset_stats(vsi, q_idx);
192 
193 	return 0;
194 }
195 
196 /**
197  * ice_qp_ena - Enables a queue pair
198  * @vsi: VSI of interest
199  * @q_idx: ring index in array
200  *
201  * Returns 0 on success, negative on failure.
202  */
203 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx)
204 {
205 	struct ice_aqc_add_tx_qgrp *qg_buf;
206 	struct ice_q_vector *q_vector;
207 	struct ice_tx_ring *tx_ring;
208 	struct ice_rx_ring *rx_ring;
209 	u16 size;
210 	int err;
211 
212 	if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
213 		return -EINVAL;
214 
215 	size = struct_size(qg_buf, txqs, 1);
216 	qg_buf = kzalloc(size, GFP_KERNEL);
217 	if (!qg_buf)
218 		return -ENOMEM;
219 
220 	qg_buf->num_txqs = 1;
221 
222 	tx_ring = vsi->tx_rings[q_idx];
223 	rx_ring = vsi->rx_rings[q_idx];
224 	q_vector = rx_ring->q_vector;
225 
226 	err = ice_vsi_cfg_txq(vsi, tx_ring, qg_buf);
227 	if (err)
228 		goto free_buf;
229 
230 	if (ice_is_xdp_ena_vsi(vsi)) {
231 		struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
232 
233 		memset(qg_buf, 0, size);
234 		qg_buf->num_txqs = 1;
235 		err = ice_vsi_cfg_txq(vsi, xdp_ring, qg_buf);
236 		if (err)
237 			goto free_buf;
238 		ice_set_ring_xdp(xdp_ring);
239 		xdp_ring->xsk_pool = ice_tx_xsk_pool(xdp_ring);
240 	}
241 
242 	err = ice_vsi_cfg_rxq(rx_ring);
243 	if (err)
244 		goto free_buf;
245 
246 	ice_qvec_cfg_msix(vsi, q_vector);
247 
248 	err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true);
249 	if (err)
250 		goto free_buf;
251 
252 	clear_bit(ICE_CFG_BUSY, vsi->state);
253 	ice_qvec_toggle_napi(vsi, q_vector, true);
254 	ice_qvec_ena_irq(vsi, q_vector);
255 
256 	netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
257 free_buf:
258 	kfree(qg_buf);
259 	return err;
260 }
261 
262 /**
263  * ice_xsk_pool_disable - disable a buffer pool region
264  * @vsi: Current VSI
265  * @qid: queue ID
266  *
267  * Returns 0 on success, negative on failure
268  */
269 static int ice_xsk_pool_disable(struct ice_vsi *vsi, u16 qid)
270 {
271 	struct xsk_buff_pool *pool = xsk_get_pool_from_qid(vsi->netdev, qid);
272 
273 	if (!pool)
274 		return -EINVAL;
275 
276 	clear_bit(qid, vsi->af_xdp_zc_qps);
277 	xsk_pool_dma_unmap(pool, ICE_RX_DMA_ATTR);
278 
279 	return 0;
280 }
281 
282 /**
283  * ice_xsk_pool_enable - enable a buffer pool region
284  * @vsi: Current VSI
285  * @pool: pointer to a requested buffer pool region
286  * @qid: queue ID
287  *
288  * Returns 0 on success, negative on failure
289  */
290 static int
291 ice_xsk_pool_enable(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
292 {
293 	int err;
294 
295 	if (vsi->type != ICE_VSI_PF)
296 		return -EINVAL;
297 
298 	if (qid >= vsi->netdev->real_num_rx_queues ||
299 	    qid >= vsi->netdev->real_num_tx_queues)
300 		return -EINVAL;
301 
302 	err = xsk_pool_dma_map(pool, ice_pf_to_dev(vsi->back),
303 			       ICE_RX_DMA_ATTR);
304 	if (err)
305 		return err;
306 
307 	set_bit(qid, vsi->af_xdp_zc_qps);
308 
309 	return 0;
310 }
311 
312 /**
313  * ice_xsk_pool_setup - enable/disable a buffer pool region depending on its state
314  * @vsi: Current VSI
315  * @pool: buffer pool to enable/associate to a ring, NULL to disable
316  * @qid: queue ID
317  *
318  * Returns 0 on success, negative on failure
319  */
320 int ice_xsk_pool_setup(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
321 {
322 	bool if_running, pool_present = !!pool;
323 	int ret = 0, pool_failure = 0;
324 
325 	if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi);
326 
327 	if (if_running) {
328 		ret = ice_qp_dis(vsi, qid);
329 		if (ret) {
330 			netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret);
331 			goto xsk_pool_if_up;
332 		}
333 	}
334 
335 	pool_failure = pool_present ? ice_xsk_pool_enable(vsi, pool, qid) :
336 				      ice_xsk_pool_disable(vsi, qid);
337 
338 xsk_pool_if_up:
339 	if (if_running) {
340 		ret = ice_qp_ena(vsi, qid);
341 		if (!ret && pool_present)
342 			napi_schedule(&vsi->xdp_rings[qid]->q_vector->napi);
343 		else if (ret)
344 			netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret);
345 	}
346 
347 	if (pool_failure) {
348 		netdev_err(vsi->netdev, "Could not %sable buffer pool, error = %d\n",
349 			   pool_present ? "en" : "dis", pool_failure);
350 		return pool_failure;
351 	}
352 
353 	return ret;
354 }
355 
356 /**
357  * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
358  * @rx_ring: Rx ring
359  * @count: The number of buffers to allocate
360  *
361  * This function allocates a number of Rx buffers from the fill ring
362  * or the internal recycle mechanism and places them on the Rx ring.
363  *
364  * Returns true if all allocations were successful, false if any fail.
365  */
366 bool ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring, u16 count)
367 {
368 	union ice_32b_rx_flex_desc *rx_desc;
369 	u16 ntu = rx_ring->next_to_use;
370 	struct xdp_buff **xdp;
371 	u32 nb_buffs, i;
372 	dma_addr_t dma;
373 
374 	rx_desc = ICE_RX_DESC(rx_ring, ntu);
375 	xdp = &rx_ring->xdp_buf[ntu];
376 
377 	nb_buffs = min_t(u16, count, rx_ring->count - ntu);
378 	nb_buffs = xsk_buff_alloc_batch(rx_ring->xsk_pool, xdp, nb_buffs);
379 	if (!nb_buffs)
380 		return false;
381 
382 	i = nb_buffs;
383 	while (i--) {
384 		dma = xsk_buff_xdp_get_dma(*xdp);
385 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
386 		rx_desc->wb.status_error0 = 0;
387 
388 		rx_desc++;
389 		xdp++;
390 	}
391 
392 	ntu += nb_buffs;
393 	if (ntu == rx_ring->count) {
394 		rx_desc = ICE_RX_DESC(rx_ring, 0);
395 		xdp = rx_ring->xdp_buf;
396 		ntu = 0;
397 	}
398 
399 	/* clear the status bits for the next_to_use descriptor */
400 	rx_desc->wb.status_error0 = 0;
401 	ice_release_rx_desc(rx_ring, ntu);
402 
403 	return count == nb_buffs;
404 }
405 
406 /**
407  * ice_bump_ntc - Bump the next_to_clean counter of an Rx ring
408  * @rx_ring: Rx ring
409  */
410 static void ice_bump_ntc(struct ice_rx_ring *rx_ring)
411 {
412 	int ntc = rx_ring->next_to_clean + 1;
413 
414 	ntc = (ntc < rx_ring->count) ? ntc : 0;
415 	rx_ring->next_to_clean = ntc;
416 	prefetch(ICE_RX_DESC(rx_ring, ntc));
417 }
418 
419 /**
420  * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer
421  * @rx_ring: Rx ring
422  * @xdp_arr: Pointer to the SW ring of xdp_buff pointers
423  *
424  * This function allocates a new skb from a zero-copy Rx buffer.
425  *
426  * Returns the skb on success, NULL on failure.
427  */
428 static struct sk_buff *
429 ice_construct_skb_zc(struct ice_rx_ring *rx_ring, struct xdp_buff **xdp_arr)
430 {
431 	struct xdp_buff *xdp = *xdp_arr;
432 	unsigned int metasize = xdp->data - xdp->data_meta;
433 	unsigned int datasize = xdp->data_end - xdp->data;
434 	unsigned int datasize_hard = xdp->data_end - xdp->data_hard_start;
435 	struct sk_buff *skb;
436 
437 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi, datasize_hard,
438 			       GFP_ATOMIC | __GFP_NOWARN);
439 	if (unlikely(!skb))
440 		return NULL;
441 
442 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
443 	memcpy(__skb_put(skb, datasize), xdp->data, datasize);
444 	if (metasize)
445 		skb_metadata_set(skb, metasize);
446 
447 	xsk_buff_free(xdp);
448 	*xdp_arr = NULL;
449 	return skb;
450 }
451 
452 /**
453  * ice_run_xdp_zc - Executes an XDP program in zero-copy path
454  * @rx_ring: Rx ring
455  * @xdp: xdp_buff used as input to the XDP program
456  * @xdp_prog: XDP program to run
457  * @xdp_ring: ring to be used for XDP_TX action
458  *
459  * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
460  */
461 static int
462 ice_run_xdp_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
463 	       struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring)
464 {
465 	int err, result = ICE_XDP_PASS;
466 	u32 act;
467 
468 	act = bpf_prog_run_xdp(xdp_prog, xdp);
469 
470 	if (likely(act == XDP_REDIRECT)) {
471 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
472 		if (err)
473 			goto out_failure;
474 		return ICE_XDP_REDIR;
475 	}
476 
477 	switch (act) {
478 	case XDP_PASS:
479 		break;
480 	case XDP_TX:
481 		result = ice_xmit_xdp_buff(xdp, xdp_ring);
482 		if (result == ICE_XDP_CONSUMED)
483 			goto out_failure;
484 		break;
485 	default:
486 		bpf_warn_invalid_xdp_action(act);
487 		fallthrough;
488 	case XDP_ABORTED:
489 out_failure:
490 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
491 		fallthrough;
492 	case XDP_DROP:
493 		result = ICE_XDP_CONSUMED;
494 		break;
495 	}
496 
497 	return result;
498 }
499 
500 /**
501  * ice_clean_rx_irq_zc - consumes packets from the hardware ring
502  * @rx_ring: AF_XDP Rx ring
503  * @budget: NAPI budget
504  *
505  * Returns number of processed packets on success, remaining budget on failure.
506  */
507 int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring, int budget)
508 {
509 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
510 	u16 cleaned_count = ICE_DESC_UNUSED(rx_ring);
511 	struct ice_tx_ring *xdp_ring;
512 	unsigned int xdp_xmit = 0;
513 	struct bpf_prog *xdp_prog;
514 	bool failure = false;
515 
516 	/* ZC patch is enabled only when XDP program is set,
517 	 * so here it can not be NULL
518 	 */
519 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
520 	xdp_ring = rx_ring->xdp_ring;
521 
522 	while (likely(total_rx_packets < (unsigned int)budget)) {
523 		union ice_32b_rx_flex_desc *rx_desc;
524 		unsigned int size, xdp_res = 0;
525 		struct xdp_buff **xdp;
526 		struct sk_buff *skb;
527 		u16 stat_err_bits;
528 		u16 vlan_tag = 0;
529 		u16 rx_ptype;
530 
531 		rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
532 
533 		stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
534 		if (!ice_test_staterr(rx_desc, stat_err_bits))
535 			break;
536 
537 		/* This memory barrier is needed to keep us from reading
538 		 * any other fields out of the rx_desc until we have
539 		 * verified the descriptor has been written back.
540 		 */
541 		dma_rmb();
542 
543 		size = le16_to_cpu(rx_desc->wb.pkt_len) &
544 				   ICE_RX_FLX_DESC_PKT_LEN_M;
545 		if (!size)
546 			break;
547 
548 		xdp = &rx_ring->xdp_buf[rx_ring->next_to_clean];
549 		xsk_buff_set_size(*xdp, size);
550 		xsk_buff_dma_sync_for_cpu(*xdp, rx_ring->xsk_pool);
551 
552 		xdp_res = ice_run_xdp_zc(rx_ring, *xdp, xdp_prog, xdp_ring);
553 		if (xdp_res) {
554 			if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))
555 				xdp_xmit |= xdp_res;
556 			else
557 				xsk_buff_free(*xdp);
558 
559 			*xdp = NULL;
560 			total_rx_bytes += size;
561 			total_rx_packets++;
562 			cleaned_count++;
563 
564 			ice_bump_ntc(rx_ring);
565 			continue;
566 		}
567 
568 		/* XDP_PASS path */
569 		skb = ice_construct_skb_zc(rx_ring, xdp);
570 		if (!skb) {
571 			rx_ring->rx_stats.alloc_buf_failed++;
572 			break;
573 		}
574 
575 		cleaned_count++;
576 		ice_bump_ntc(rx_ring);
577 
578 		if (eth_skb_pad(skb)) {
579 			skb = NULL;
580 			continue;
581 		}
582 
583 		total_rx_bytes += skb->len;
584 		total_rx_packets++;
585 
586 		stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S);
587 		if (ice_test_staterr(rx_desc, stat_err_bits))
588 			vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1);
589 
590 		rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
591 				       ICE_RX_FLEX_DESC_PTYPE_M;
592 
593 		ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
594 		ice_receive_skb(rx_ring, skb, vlan_tag);
595 	}
596 
597 	if (cleaned_count >= ICE_RX_BUF_WRITE)
598 		failure = !ice_alloc_rx_bufs_zc(rx_ring, cleaned_count);
599 
600 	ice_finalize_xdp_rx(xdp_ring, xdp_xmit);
601 	ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes);
602 
603 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
604 		if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
605 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
606 		else
607 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
608 
609 		return (int)total_rx_packets;
610 	}
611 
612 	return failure ? budget : (int)total_rx_packets;
613 }
614 
615 /**
616  * ice_xmit_zc - Completes AF_XDP entries, and cleans XDP entries
617  * @xdp_ring: XDP Tx ring
618  * @budget: max number of frames to xmit
619  *
620  * Returns true if cleanup/transmission is done.
621  */
622 static bool ice_xmit_zc(struct ice_tx_ring *xdp_ring, int budget)
623 {
624 	struct ice_tx_desc *tx_desc = NULL;
625 	bool work_done = true;
626 	struct xdp_desc desc;
627 	dma_addr_t dma;
628 
629 	while (likely(budget-- > 0)) {
630 		struct ice_tx_buf *tx_buf;
631 
632 		if (unlikely(!ICE_DESC_UNUSED(xdp_ring))) {
633 			xdp_ring->tx_stats.tx_busy++;
634 			work_done = false;
635 			break;
636 		}
637 
638 		tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use];
639 
640 		if (!xsk_tx_peek_desc(xdp_ring->xsk_pool, &desc))
641 			break;
642 
643 		dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc.addr);
644 		xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma,
645 						 desc.len);
646 
647 		tx_buf->bytecount = desc.len;
648 
649 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use);
650 		tx_desc->buf_addr = cpu_to_le64(dma);
651 		tx_desc->cmd_type_offset_bsz =
652 			ice_build_ctob(ICE_TXD_LAST_DESC_CMD, 0, desc.len, 0);
653 
654 		xdp_ring->next_to_use++;
655 		if (xdp_ring->next_to_use == xdp_ring->count)
656 			xdp_ring->next_to_use = 0;
657 	}
658 
659 	if (tx_desc) {
660 		ice_xdp_ring_update_tail(xdp_ring);
661 		xsk_tx_release(xdp_ring->xsk_pool);
662 	}
663 
664 	return budget > 0 && work_done;
665 }
666 
667 /**
668  * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
669  * @xdp_ring: XDP Tx ring
670  * @tx_buf: Tx buffer to clean
671  */
672 static void
673 ice_clean_xdp_tx_buf(struct ice_tx_ring *xdp_ring, struct ice_tx_buf *tx_buf)
674 {
675 	xdp_return_frame((struct xdp_frame *)tx_buf->raw_buf);
676 	dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma),
677 			 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
678 	dma_unmap_len_set(tx_buf, len, 0);
679 }
680 
681 /**
682  * ice_clean_tx_irq_zc - Completes AF_XDP entries, and cleans XDP entries
683  * @xdp_ring: XDP Tx ring
684  * @budget: NAPI budget
685  *
686  * Returns true if cleanup/tranmission is done.
687  */
688 bool ice_clean_tx_irq_zc(struct ice_tx_ring *xdp_ring, int budget)
689 {
690 	int total_packets = 0, total_bytes = 0;
691 	s16 ntc = xdp_ring->next_to_clean;
692 	struct ice_tx_desc *tx_desc;
693 	struct ice_tx_buf *tx_buf;
694 	u32 xsk_frames = 0;
695 	bool xmit_done;
696 
697 	tx_desc = ICE_TX_DESC(xdp_ring, ntc);
698 	tx_buf = &xdp_ring->tx_buf[ntc];
699 	ntc -= xdp_ring->count;
700 
701 	do {
702 		if (!(tx_desc->cmd_type_offset_bsz &
703 		      cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
704 			break;
705 
706 		total_bytes += tx_buf->bytecount;
707 		total_packets++;
708 
709 		if (tx_buf->raw_buf) {
710 			ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
711 			tx_buf->raw_buf = NULL;
712 		} else {
713 			xsk_frames++;
714 		}
715 
716 		tx_desc->cmd_type_offset_bsz = 0;
717 		tx_buf++;
718 		tx_desc++;
719 		ntc++;
720 
721 		if (unlikely(!ntc)) {
722 			ntc -= xdp_ring->count;
723 			tx_buf = xdp_ring->tx_buf;
724 			tx_desc = ICE_TX_DESC(xdp_ring, 0);
725 		}
726 
727 		prefetch(tx_desc);
728 
729 	} while (likely(--budget));
730 
731 	ntc += xdp_ring->count;
732 	xdp_ring->next_to_clean = ntc;
733 
734 	if (xsk_frames)
735 		xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
736 
737 	if (xsk_uses_need_wakeup(xdp_ring->xsk_pool))
738 		xsk_set_tx_need_wakeup(xdp_ring->xsk_pool);
739 
740 	ice_update_tx_ring_stats(xdp_ring, total_packets, total_bytes);
741 	xmit_done = ice_xmit_zc(xdp_ring, ICE_DFLT_IRQ_WORK);
742 
743 	return budget > 0 && xmit_done;
744 }
745 
746 /**
747  * ice_xsk_wakeup - Implements ndo_xsk_wakeup
748  * @netdev: net_device
749  * @queue_id: queue to wake up
750  * @flags: ignored in our case, since we have Rx and Tx in the same NAPI
751  *
752  * Returns negative on error, zero otherwise.
753  */
754 int
755 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id,
756 	       u32 __always_unused flags)
757 {
758 	struct ice_netdev_priv *np = netdev_priv(netdev);
759 	struct ice_q_vector *q_vector;
760 	struct ice_vsi *vsi = np->vsi;
761 	struct ice_tx_ring *ring;
762 
763 	if (test_bit(ICE_DOWN, vsi->state))
764 		return -ENETDOWN;
765 
766 	if (!ice_is_xdp_ena_vsi(vsi))
767 		return -ENXIO;
768 
769 	if (queue_id >= vsi->num_txq)
770 		return -ENXIO;
771 
772 	if (!vsi->xdp_rings[queue_id]->xsk_pool)
773 		return -ENXIO;
774 
775 	ring = vsi->xdp_rings[queue_id];
776 
777 	/* The idea here is that if NAPI is running, mark a miss, so
778 	 * it will run again. If not, trigger an interrupt and
779 	 * schedule the NAPI from interrupt context. If NAPI would be
780 	 * scheduled here, the interrupt affinity would not be
781 	 * honored.
782 	 */
783 	q_vector = ring->q_vector;
784 	if (!napi_if_scheduled_mark_missed(&q_vector->napi))
785 		ice_trigger_sw_intr(&vsi->back->hw, q_vector);
786 
787 	return 0;
788 }
789 
790 /**
791  * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP buff pool attached
792  * @vsi: VSI to be checked
793  *
794  * Returns true if any of the Rx rings has an AF_XDP buff pool attached
795  */
796 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi)
797 {
798 	int i;
799 
800 	ice_for_each_rxq(vsi, i) {
801 		if (xsk_get_pool_from_qid(vsi->netdev, i))
802 			return true;
803 	}
804 
805 	return false;
806 }
807 
808 /**
809  * ice_xsk_clean_rx_ring - clean buffer pool queues connected to a given Rx ring
810  * @rx_ring: ring to be cleaned
811  */
812 void ice_xsk_clean_rx_ring(struct ice_rx_ring *rx_ring)
813 {
814 	u16 i;
815 
816 	for (i = 0; i < rx_ring->count; i++) {
817 		struct xdp_buff **xdp = &rx_ring->xdp_buf[i];
818 
819 		if (!xdp)
820 			continue;
821 
822 		*xdp = NULL;
823 	}
824 }
825 
826 /**
827  * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its buffer pool queues
828  * @xdp_ring: XDP_Tx ring
829  */
830 void ice_xsk_clean_xdp_ring(struct ice_tx_ring *xdp_ring)
831 {
832 	u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use;
833 	u32 xsk_frames = 0;
834 
835 	while (ntc != ntu) {
836 		struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
837 
838 		if (tx_buf->raw_buf)
839 			ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
840 		else
841 			xsk_frames++;
842 
843 		tx_buf->raw_buf = NULL;
844 
845 		ntc++;
846 		if (ntc >= xdp_ring->count)
847 			ntc = 0;
848 	}
849 
850 	if (xsk_frames)
851 		xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
852 }
853