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