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