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 static struct xdp_buff **ice_xdp_buf(struct ice_rx_ring *rx_ring, u32 idx)
16 {
17 	return &rx_ring->xdp_buf[idx];
18 }
19 
20 /**
21  * ice_qp_reset_stats - Resets all stats for rings of given index
22  * @vsi: VSI that contains rings of interest
23  * @q_idx: ring index in array
24  */
25 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx)
26 {
27 	memset(&vsi->rx_rings[q_idx]->rx_stats, 0,
28 	       sizeof(vsi->rx_rings[q_idx]->rx_stats));
29 	memset(&vsi->tx_rings[q_idx]->stats, 0,
30 	       sizeof(vsi->tx_rings[q_idx]->stats));
31 	if (ice_is_xdp_ena_vsi(vsi))
32 		memset(&vsi->xdp_rings[q_idx]->stats, 0,
33 		       sizeof(vsi->xdp_rings[q_idx]->stats));
34 }
35 
36 /**
37  * ice_qp_clean_rings - Cleans all the rings of a given index
38  * @vsi: VSI that contains rings of interest
39  * @q_idx: ring index in array
40  */
41 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx)
42 {
43 	ice_clean_tx_ring(vsi->tx_rings[q_idx]);
44 	if (ice_is_xdp_ena_vsi(vsi)) {
45 		synchronize_rcu();
46 		ice_clean_tx_ring(vsi->xdp_rings[q_idx]);
47 	}
48 	ice_clean_rx_ring(vsi->rx_rings[q_idx]);
49 }
50 
51 /**
52  * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector
53  * @vsi: VSI that has netdev
54  * @q_vector: q_vector that has NAPI context
55  * @enable: true for enable, false for disable
56  */
57 static void
58 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector,
59 		     bool enable)
60 {
61 	if (!vsi->netdev || !q_vector)
62 		return;
63 
64 	if (enable)
65 		napi_enable(&q_vector->napi);
66 	else
67 		napi_disable(&q_vector->napi);
68 }
69 
70 /**
71  * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring
72  * @vsi: the VSI that contains queue vector being un-configured
73  * @rx_ring: Rx ring that will have its IRQ disabled
74  * @q_vector: queue vector
75  */
76 static void
77 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_rx_ring *rx_ring,
78 		 struct ice_q_vector *q_vector)
79 {
80 	struct ice_pf *pf = vsi->back;
81 	struct ice_hw *hw = &pf->hw;
82 	int base = vsi->base_vector;
83 	u16 reg;
84 	u32 val;
85 
86 	/* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle
87 	 * here only QINT_RQCTL
88 	 */
89 	reg = rx_ring->reg_idx;
90 	val = rd32(hw, QINT_RQCTL(reg));
91 	val &= ~QINT_RQCTL_CAUSE_ENA_M;
92 	wr32(hw, QINT_RQCTL(reg), val);
93 
94 	if (q_vector) {
95 		u16 v_idx = q_vector->v_idx;
96 
97 		wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0);
98 		ice_flush(hw);
99 		synchronize_irq(pf->msix_entries[v_idx + base].vector);
100 	}
101 }
102 
103 /**
104  * ice_qvec_cfg_msix - Enable IRQ for given queue vector
105  * @vsi: the VSI that contains queue vector
106  * @q_vector: queue vector
107  */
108 static void
109 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
110 {
111 	u16 reg_idx = q_vector->reg_idx;
112 	struct ice_pf *pf = vsi->back;
113 	struct ice_hw *hw = &pf->hw;
114 	struct ice_tx_ring *tx_ring;
115 	struct ice_rx_ring *rx_ring;
116 
117 	ice_cfg_itr(hw, q_vector);
118 
119 	ice_for_each_tx_ring(tx_ring, q_vector->tx)
120 		ice_cfg_txq_interrupt(vsi, tx_ring->reg_idx, reg_idx,
121 				      q_vector->tx.itr_idx);
122 
123 	ice_for_each_rx_ring(rx_ring, q_vector->rx)
124 		ice_cfg_rxq_interrupt(vsi, rx_ring->reg_idx, reg_idx,
125 				      q_vector->rx.itr_idx);
126 
127 	ice_flush(hw);
128 }
129 
130 /**
131  * ice_qvec_ena_irq - Enable IRQ for given queue vector
132  * @vsi: the VSI that contains queue vector
133  * @q_vector: queue vector
134  */
135 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
136 {
137 	struct ice_pf *pf = vsi->back;
138 	struct ice_hw *hw = &pf->hw;
139 
140 	ice_irq_dynamic_ena(hw, vsi, q_vector);
141 
142 	ice_flush(hw);
143 }
144 
145 /**
146  * ice_qp_dis - Disables a queue pair
147  * @vsi: VSI of interest
148  * @q_idx: ring index in array
149  *
150  * Returns 0 on success, negative on failure.
151  */
152 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx)
153 {
154 	struct ice_txq_meta txq_meta = { };
155 	struct ice_q_vector *q_vector;
156 	struct ice_tx_ring *tx_ring;
157 	struct ice_rx_ring *rx_ring;
158 	int timeout = 50;
159 	int err;
160 
161 	if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
162 		return -EINVAL;
163 
164 	tx_ring = vsi->tx_rings[q_idx];
165 	rx_ring = vsi->rx_rings[q_idx];
166 	q_vector = rx_ring->q_vector;
167 
168 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state)) {
169 		timeout--;
170 		if (!timeout)
171 			return -EBUSY;
172 		usleep_range(1000, 2000);
173 	}
174 	netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
175 
176 	ice_qvec_dis_irq(vsi, rx_ring, q_vector);
177 
178 	ice_fill_txq_meta(vsi, tx_ring, &txq_meta);
179 	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta);
180 	if (err)
181 		return err;
182 	if (ice_is_xdp_ena_vsi(vsi)) {
183 		struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
184 
185 		memset(&txq_meta, 0, sizeof(txq_meta));
186 		ice_fill_txq_meta(vsi, xdp_ring, &txq_meta);
187 		err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring,
188 					   &txq_meta);
189 		if (err)
190 			return err;
191 	}
192 	err = ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, true);
193 	if (err)
194 		return err;
195 
196 	ice_qvec_toggle_napi(vsi, q_vector, false);
197 	ice_qp_clean_rings(vsi, q_idx);
198 	ice_qp_reset_stats(vsi, q_idx);
199 
200 	return 0;
201 }
202 
203 /**
204  * ice_qp_ena - Enables a queue pair
205  * @vsi: VSI of interest
206  * @q_idx: ring index in array
207  *
208  * Returns 0 on success, negative on failure.
209  */
210 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx)
211 {
212 	struct ice_aqc_add_tx_qgrp *qg_buf;
213 	struct ice_q_vector *q_vector;
214 	struct ice_tx_ring *tx_ring;
215 	struct ice_rx_ring *rx_ring;
216 	u16 size;
217 	int err;
218 
219 	if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
220 		return -EINVAL;
221 
222 	size = struct_size(qg_buf, txqs, 1);
223 	qg_buf = kzalloc(size, GFP_KERNEL);
224 	if (!qg_buf)
225 		return -ENOMEM;
226 
227 	qg_buf->num_txqs = 1;
228 
229 	tx_ring = vsi->tx_rings[q_idx];
230 	rx_ring = vsi->rx_rings[q_idx];
231 	q_vector = rx_ring->q_vector;
232 
233 	err = ice_vsi_cfg_txq(vsi, tx_ring, qg_buf);
234 	if (err)
235 		goto free_buf;
236 
237 	if (ice_is_xdp_ena_vsi(vsi)) {
238 		struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
239 
240 		memset(qg_buf, 0, size);
241 		qg_buf->num_txqs = 1;
242 		err = ice_vsi_cfg_txq(vsi, xdp_ring, qg_buf);
243 		if (err)
244 			goto free_buf;
245 		ice_set_ring_xdp(xdp_ring);
246 		ice_tx_xsk_pool(vsi, q_idx);
247 	}
248 
249 	err = ice_vsi_cfg_rxq(rx_ring);
250 	if (err)
251 		goto free_buf;
252 
253 	ice_qvec_cfg_msix(vsi, q_vector);
254 
255 	err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true);
256 	if (err)
257 		goto free_buf;
258 
259 	clear_bit(ICE_CFG_BUSY, vsi->state);
260 	ice_qvec_toggle_napi(vsi, q_vector, true);
261 	ice_qvec_ena_irq(vsi, q_vector);
262 
263 	netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
264 free_buf:
265 	kfree(qg_buf);
266 	return err;
267 }
268 
269 /**
270  * ice_xsk_pool_disable - disable a buffer pool region
271  * @vsi: Current VSI
272  * @qid: queue ID
273  *
274  * Returns 0 on success, negative on failure
275  */
276 static int ice_xsk_pool_disable(struct ice_vsi *vsi, u16 qid)
277 {
278 	struct xsk_buff_pool *pool = xsk_get_pool_from_qid(vsi->netdev, qid);
279 
280 	if (!pool)
281 		return -EINVAL;
282 
283 	clear_bit(qid, vsi->af_xdp_zc_qps);
284 	xsk_pool_dma_unmap(pool, ICE_RX_DMA_ATTR);
285 
286 	return 0;
287 }
288 
289 /**
290  * ice_xsk_pool_enable - enable a buffer pool region
291  * @vsi: Current VSI
292  * @pool: pointer to a requested buffer pool region
293  * @qid: queue ID
294  *
295  * Returns 0 on success, negative on failure
296  */
297 static int
298 ice_xsk_pool_enable(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
299 {
300 	int err;
301 
302 	if (vsi->type != ICE_VSI_PF)
303 		return -EINVAL;
304 
305 	if (qid >= vsi->netdev->real_num_rx_queues ||
306 	    qid >= vsi->netdev->real_num_tx_queues)
307 		return -EINVAL;
308 
309 	err = xsk_pool_dma_map(pool, ice_pf_to_dev(vsi->back),
310 			       ICE_RX_DMA_ATTR);
311 	if (err)
312 		return err;
313 
314 	set_bit(qid, vsi->af_xdp_zc_qps);
315 
316 	return 0;
317 }
318 
319 /**
320  * ice_xsk_pool_setup - enable/disable a buffer pool region depending on its state
321  * @vsi: Current VSI
322  * @pool: buffer pool to enable/associate to a ring, NULL to disable
323  * @qid: queue ID
324  *
325  * Returns 0 on success, negative on failure
326  */
327 int ice_xsk_pool_setup(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
328 {
329 	bool if_running, pool_present = !!pool;
330 	int ret = 0, pool_failure = 0;
331 
332 	if (qid >= vsi->num_rxq || qid >= vsi->num_txq) {
333 		netdev_err(vsi->netdev, "Please use queue id in scope of combined queues count\n");
334 		pool_failure = -EINVAL;
335 		goto failure;
336 	}
337 
338 	if (!is_power_of_2(vsi->rx_rings[qid]->count) ||
339 	    !is_power_of_2(vsi->tx_rings[qid]->count)) {
340 		netdev_err(vsi->netdev, "Please align ring sizes to power of 2\n");
341 		pool_failure = -EINVAL;
342 		goto failure;
343 	}
344 
345 	if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi);
346 
347 	if (if_running) {
348 		ret = ice_qp_dis(vsi, qid);
349 		if (ret) {
350 			netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret);
351 			goto xsk_pool_if_up;
352 		}
353 	}
354 
355 	pool_failure = pool_present ? ice_xsk_pool_enable(vsi, pool, qid) :
356 				      ice_xsk_pool_disable(vsi, qid);
357 
358 xsk_pool_if_up:
359 	if (if_running) {
360 		ret = ice_qp_ena(vsi, qid);
361 		if (!ret && pool_present)
362 			napi_schedule(&vsi->rx_rings[qid]->xdp_ring->q_vector->napi);
363 		else if (ret)
364 			netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret);
365 	}
366 
367 failure:
368 	if (pool_failure) {
369 		netdev_err(vsi->netdev, "Could not %sable buffer pool, error = %d\n",
370 			   pool_present ? "en" : "dis", pool_failure);
371 		return pool_failure;
372 	}
373 
374 	return ret;
375 }
376 
377 /**
378  * ice_fill_rx_descs - pick buffers from XSK buffer pool and use it
379  * @pool: XSK Buffer pool to pull the buffers from
380  * @xdp: SW ring of xdp_buff that will hold the buffers
381  * @rx_desc: Pointer to Rx descriptors that will be filled
382  * @count: The number of buffers to allocate
383  *
384  * This function allocates a number of Rx buffers from the fill ring
385  * or the internal recycle mechanism and places them on the Rx ring.
386  *
387  * Note that ring wrap should be handled by caller of this function.
388  *
389  * Returns the amount of allocated Rx descriptors
390  */
391 static u16 ice_fill_rx_descs(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
392 			     union ice_32b_rx_flex_desc *rx_desc, u16 count)
393 {
394 	dma_addr_t dma;
395 	u16 buffs;
396 	int i;
397 
398 	buffs = xsk_buff_alloc_batch(pool, xdp, count);
399 	for (i = 0; i < buffs; i++) {
400 		dma = xsk_buff_xdp_get_dma(*xdp);
401 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
402 		rx_desc->wb.status_error0 = 0;
403 
404 		rx_desc++;
405 		xdp++;
406 	}
407 
408 	return buffs;
409 }
410 
411 /**
412  * __ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
413  * @rx_ring: Rx ring
414  * @count: The number of buffers to allocate
415  *
416  * Place the @count of descriptors onto Rx ring. Handle the ring wrap
417  * for case where space from next_to_use up to the end of ring is less
418  * than @count. Finally do a tail bump.
419  *
420  * Returns true if all allocations were successful, false if any fail.
421  */
422 static bool __ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring, u16 count)
423 {
424 	u32 nb_buffs_extra = 0, nb_buffs = 0;
425 	union ice_32b_rx_flex_desc *rx_desc;
426 	u16 ntu = rx_ring->next_to_use;
427 	u16 total_count = count;
428 	struct xdp_buff **xdp;
429 
430 	rx_desc = ICE_RX_DESC(rx_ring, ntu);
431 	xdp = ice_xdp_buf(rx_ring, ntu);
432 
433 	if (ntu + count >= rx_ring->count) {
434 		nb_buffs_extra = ice_fill_rx_descs(rx_ring->xsk_pool, xdp,
435 						   rx_desc,
436 						   rx_ring->count - ntu);
437 		if (nb_buffs_extra != rx_ring->count - ntu) {
438 			ntu += nb_buffs_extra;
439 			goto exit;
440 		}
441 		rx_desc = ICE_RX_DESC(rx_ring, 0);
442 		xdp = ice_xdp_buf(rx_ring, 0);
443 		ntu = 0;
444 		count -= nb_buffs_extra;
445 		ice_release_rx_desc(rx_ring, 0);
446 	}
447 
448 	nb_buffs = ice_fill_rx_descs(rx_ring->xsk_pool, xdp, rx_desc, count);
449 
450 	ntu += nb_buffs;
451 	if (ntu == rx_ring->count)
452 		ntu = 0;
453 
454 exit:
455 	if (rx_ring->next_to_use != ntu)
456 		ice_release_rx_desc(rx_ring, ntu);
457 
458 	return total_count == (nb_buffs_extra + nb_buffs);
459 }
460 
461 /**
462  * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
463  * @rx_ring: Rx ring
464  * @count: The number of buffers to allocate
465  *
466  * Wrapper for internal allocation routine; figure out how many tail
467  * bumps should take place based on the given threshold
468  *
469  * Returns true if all calls to internal alloc routine succeeded
470  */
471 bool ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring, u16 count)
472 {
473 	u16 rx_thresh = ICE_RING_QUARTER(rx_ring);
474 	u16 batched, leftover, i, tail_bumps;
475 
476 	batched = ALIGN_DOWN(count, rx_thresh);
477 	tail_bumps = batched / rx_thresh;
478 	leftover = count & (rx_thresh - 1);
479 
480 	for (i = 0; i < tail_bumps; i++)
481 		if (!__ice_alloc_rx_bufs_zc(rx_ring, rx_thresh))
482 			return false;
483 	return __ice_alloc_rx_bufs_zc(rx_ring, leftover);
484 }
485 
486 /**
487  * ice_bump_ntc - Bump the next_to_clean counter of an Rx ring
488  * @rx_ring: Rx ring
489  */
490 static void ice_bump_ntc(struct ice_rx_ring *rx_ring)
491 {
492 	int ntc = rx_ring->next_to_clean + 1;
493 
494 	ntc = (ntc < rx_ring->count) ? ntc : 0;
495 	rx_ring->next_to_clean = ntc;
496 	prefetch(ICE_RX_DESC(rx_ring, ntc));
497 }
498 
499 /**
500  * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer
501  * @rx_ring: Rx ring
502  * @xdp: Pointer to XDP buffer
503  *
504  * This function allocates a new skb from a zero-copy Rx buffer.
505  *
506  * Returns the skb on success, NULL on failure.
507  */
508 static struct sk_buff *
509 ice_construct_skb_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
510 {
511 	unsigned int totalsize = xdp->data_end - xdp->data_meta;
512 	unsigned int metasize = xdp->data - xdp->data_meta;
513 	struct sk_buff *skb;
514 
515 	net_prefetch(xdp->data_meta);
516 
517 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
518 			       GFP_ATOMIC | __GFP_NOWARN);
519 	if (unlikely(!skb))
520 		return NULL;
521 
522 	memcpy(__skb_put(skb, totalsize), xdp->data_meta,
523 	       ALIGN(totalsize, sizeof(long)));
524 
525 	if (metasize) {
526 		skb_metadata_set(skb, metasize);
527 		__skb_pull(skb, metasize);
528 	}
529 
530 	xsk_buff_free(xdp);
531 	return skb;
532 }
533 
534 /**
535  * ice_run_xdp_zc - Executes an XDP program in zero-copy path
536  * @rx_ring: Rx ring
537  * @xdp: xdp_buff used as input to the XDP program
538  * @xdp_prog: XDP program to run
539  * @xdp_ring: ring to be used for XDP_TX action
540  *
541  * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
542  */
543 static int
544 ice_run_xdp_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
545 	       struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring)
546 {
547 	int err, result = ICE_XDP_PASS;
548 	u32 act;
549 
550 	act = bpf_prog_run_xdp(xdp_prog, xdp);
551 
552 	if (likely(act == XDP_REDIRECT)) {
553 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
554 		if (!err)
555 			return ICE_XDP_REDIR;
556 		if (xsk_uses_need_wakeup(rx_ring->xsk_pool) && err == -ENOBUFS)
557 			result = ICE_XDP_EXIT;
558 		else
559 			result = ICE_XDP_CONSUMED;
560 		goto out_failure;
561 	}
562 
563 	switch (act) {
564 	case XDP_PASS:
565 		break;
566 	case XDP_TX:
567 		result = ice_xmit_xdp_buff(xdp, xdp_ring);
568 		if (result == ICE_XDP_CONSUMED)
569 			goto out_failure;
570 		break;
571 	case XDP_DROP:
572 		result = ICE_XDP_CONSUMED;
573 		break;
574 	default:
575 		bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
576 		fallthrough;
577 	case XDP_ABORTED:
578 		result = ICE_XDP_CONSUMED;
579 out_failure:
580 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
581 		break;
582 	}
583 
584 	return result;
585 }
586 
587 /**
588  * ice_clean_rx_irq_zc - consumes packets from the hardware ring
589  * @rx_ring: AF_XDP Rx ring
590  * @budget: NAPI budget
591  *
592  * Returns number of processed packets on success, remaining budget on failure.
593  */
594 int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring, int budget)
595 {
596 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
597 	struct ice_tx_ring *xdp_ring;
598 	unsigned int xdp_xmit = 0;
599 	struct bpf_prog *xdp_prog;
600 	bool failure = false;
601 	int entries_to_alloc;
602 
603 	/* ZC patch is enabled only when XDP program is set,
604 	 * so here it can not be NULL
605 	 */
606 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
607 	xdp_ring = rx_ring->xdp_ring;
608 
609 	while (likely(total_rx_packets < (unsigned int)budget)) {
610 		union ice_32b_rx_flex_desc *rx_desc;
611 		unsigned int size, xdp_res = 0;
612 		struct xdp_buff *xdp;
613 		struct sk_buff *skb;
614 		u16 stat_err_bits;
615 		u16 vlan_tag = 0;
616 		u16 rx_ptype;
617 
618 		rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
619 
620 		stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
621 		if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits))
622 			break;
623 
624 		/* This memory barrier is needed to keep us from reading
625 		 * any other fields out of the rx_desc until we have
626 		 * verified the descriptor has been written back.
627 		 */
628 		dma_rmb();
629 
630 		if (unlikely(rx_ring->next_to_clean == rx_ring->next_to_use))
631 			break;
632 
633 		xdp = *ice_xdp_buf(rx_ring, rx_ring->next_to_clean);
634 
635 		size = le16_to_cpu(rx_desc->wb.pkt_len) &
636 				   ICE_RX_FLX_DESC_PKT_LEN_M;
637 		if (!size) {
638 			xdp->data = NULL;
639 			xdp->data_end = NULL;
640 			xdp->data_hard_start = NULL;
641 			xdp->data_meta = NULL;
642 			goto construct_skb;
643 		}
644 
645 		xsk_buff_set_size(xdp, size);
646 		xsk_buff_dma_sync_for_cpu(xdp, rx_ring->xsk_pool);
647 
648 		xdp_res = ice_run_xdp_zc(rx_ring, xdp, xdp_prog, xdp_ring);
649 		if (likely(xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))) {
650 			xdp_xmit |= xdp_res;
651 		} else if (xdp_res == ICE_XDP_EXIT) {
652 			failure = true;
653 			break;
654 		} else if (xdp_res == ICE_XDP_CONSUMED) {
655 			xsk_buff_free(xdp);
656 		} else if (xdp_res == ICE_XDP_PASS) {
657 			goto construct_skb;
658 		}
659 
660 		total_rx_bytes += size;
661 		total_rx_packets++;
662 
663 		ice_bump_ntc(rx_ring);
664 		continue;
665 
666 construct_skb:
667 		/* XDP_PASS path */
668 		skb = ice_construct_skb_zc(rx_ring, xdp);
669 		if (!skb) {
670 			rx_ring->rx_stats.alloc_buf_failed++;
671 			break;
672 		}
673 
674 		ice_bump_ntc(rx_ring);
675 
676 		if (eth_skb_pad(skb)) {
677 			skb = NULL;
678 			continue;
679 		}
680 
681 		total_rx_bytes += skb->len;
682 		total_rx_packets++;
683 
684 		vlan_tag = ice_get_vlan_tag_from_rx_desc(rx_desc);
685 
686 		rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
687 				       ICE_RX_FLEX_DESC_PTYPE_M;
688 
689 		ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
690 		ice_receive_skb(rx_ring, skb, vlan_tag);
691 	}
692 
693 	entries_to_alloc = ICE_DESC_UNUSED(rx_ring);
694 	if (entries_to_alloc > ICE_RING_QUARTER(rx_ring))
695 		failure |= !ice_alloc_rx_bufs_zc(rx_ring, entries_to_alloc);
696 
697 	ice_finalize_xdp_rx(xdp_ring, xdp_xmit);
698 	ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes);
699 
700 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
701 		if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
702 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
703 		else
704 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
705 
706 		return (int)total_rx_packets;
707 	}
708 
709 	return failure ? budget : (int)total_rx_packets;
710 }
711 
712 /**
713  * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
714  * @xdp_ring: XDP Tx ring
715  * @tx_buf: Tx buffer to clean
716  */
717 static void
718 ice_clean_xdp_tx_buf(struct ice_tx_ring *xdp_ring, struct ice_tx_buf *tx_buf)
719 {
720 	xdp_return_frame((struct xdp_frame *)tx_buf->raw_buf);
721 	xdp_ring->xdp_tx_active--;
722 	dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma),
723 			 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
724 	dma_unmap_len_set(tx_buf, len, 0);
725 }
726 
727 /**
728  * ice_clean_xdp_irq_zc - Reclaim resources after transmit completes on XDP ring
729  * @xdp_ring: XDP ring to clean
730  * @napi_budget: amount of descriptors that NAPI allows us to clean
731  *
732  * Returns count of cleaned descriptors
733  */
734 static u16 ice_clean_xdp_irq_zc(struct ice_tx_ring *xdp_ring, int napi_budget)
735 {
736 	u16 tx_thresh = ICE_RING_QUARTER(xdp_ring);
737 	int budget = napi_budget / tx_thresh;
738 	u16 next_dd = xdp_ring->next_dd;
739 	u16 ntc, cleared_dds = 0;
740 
741 	do {
742 		struct ice_tx_desc *next_dd_desc;
743 		u16 desc_cnt = xdp_ring->count;
744 		struct ice_tx_buf *tx_buf;
745 		u32 xsk_frames;
746 		u16 i;
747 
748 		next_dd_desc = ICE_TX_DESC(xdp_ring, next_dd);
749 		if (!(next_dd_desc->cmd_type_offset_bsz &
750 		    cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
751 			break;
752 
753 		cleared_dds++;
754 		xsk_frames = 0;
755 		if (likely(!xdp_ring->xdp_tx_active)) {
756 			xsk_frames = tx_thresh;
757 			goto skip;
758 		}
759 
760 		ntc = xdp_ring->next_to_clean;
761 
762 		for (i = 0; i < tx_thresh; i++) {
763 			tx_buf = &xdp_ring->tx_buf[ntc];
764 
765 			if (tx_buf->raw_buf) {
766 				ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
767 				tx_buf->raw_buf = NULL;
768 			} else {
769 				xsk_frames++;
770 			}
771 
772 			ntc++;
773 			if (ntc >= xdp_ring->count)
774 				ntc = 0;
775 		}
776 skip:
777 		xdp_ring->next_to_clean += tx_thresh;
778 		if (xdp_ring->next_to_clean >= desc_cnt)
779 			xdp_ring->next_to_clean -= desc_cnt;
780 		if (xsk_frames)
781 			xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
782 		next_dd_desc->cmd_type_offset_bsz = 0;
783 		next_dd = next_dd + tx_thresh;
784 		if (next_dd >= desc_cnt)
785 			next_dd = tx_thresh - 1;
786 	} while (--budget);
787 
788 	xdp_ring->next_dd = next_dd;
789 
790 	return cleared_dds * tx_thresh;
791 }
792 
793 /**
794  * ice_xmit_pkt - produce a single HW Tx descriptor out of AF_XDP descriptor
795  * @xdp_ring: XDP ring to produce the HW Tx descriptor on
796  * @desc: AF_XDP descriptor to pull the DMA address and length from
797  * @total_bytes: bytes accumulator that will be used for stats update
798  */
799 static void ice_xmit_pkt(struct ice_tx_ring *xdp_ring, struct xdp_desc *desc,
800 			 unsigned int *total_bytes)
801 {
802 	struct ice_tx_desc *tx_desc;
803 	dma_addr_t dma;
804 
805 	dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
806 	xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
807 
808 	tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
809 	tx_desc->buf_addr = cpu_to_le64(dma);
810 	tx_desc->cmd_type_offset_bsz = ice_build_ctob(ICE_TX_DESC_CMD_EOP,
811 						      0, desc->len, 0);
812 
813 	*total_bytes += desc->len;
814 }
815 
816 /**
817  * ice_xmit_pkt_batch - produce a batch of HW Tx descriptors out of AF_XDP descriptors
818  * @xdp_ring: XDP ring to produce the HW Tx descriptors on
819  * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from
820  * @total_bytes: bytes accumulator that will be used for stats update
821  */
822 static void ice_xmit_pkt_batch(struct ice_tx_ring *xdp_ring, struct xdp_desc *descs,
823 			       unsigned int *total_bytes)
824 {
825 	u16 tx_thresh = ICE_RING_QUARTER(xdp_ring);
826 	u16 ntu = xdp_ring->next_to_use;
827 	struct ice_tx_desc *tx_desc;
828 	u32 i;
829 
830 	loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
831 		dma_addr_t dma;
832 
833 		dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, descs[i].addr);
834 		xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, descs[i].len);
835 
836 		tx_desc = ICE_TX_DESC(xdp_ring, ntu++);
837 		tx_desc->buf_addr = cpu_to_le64(dma);
838 		tx_desc->cmd_type_offset_bsz = ice_build_ctob(ICE_TX_DESC_CMD_EOP,
839 							      0, descs[i].len, 0);
840 
841 		*total_bytes += descs[i].len;
842 	}
843 
844 	xdp_ring->next_to_use = ntu;
845 
846 	if (xdp_ring->next_to_use > xdp_ring->next_rs) {
847 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_rs);
848 		tx_desc->cmd_type_offset_bsz |=
849 			cpu_to_le64(ICE_TX_DESC_CMD_RS << ICE_TXD_QW1_CMD_S);
850 		xdp_ring->next_rs += tx_thresh;
851 	}
852 }
853 
854 /**
855  * ice_fill_tx_hw_ring - produce the number of Tx descriptors onto ring
856  * @xdp_ring: XDP ring to produce the HW Tx descriptors on
857  * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from
858  * @nb_pkts: count of packets to be send
859  * @total_bytes: bytes accumulator that will be used for stats update
860  */
861 static void ice_fill_tx_hw_ring(struct ice_tx_ring *xdp_ring, struct xdp_desc *descs,
862 				u32 nb_pkts, unsigned int *total_bytes)
863 {
864 	u16 tx_thresh = ICE_RING_QUARTER(xdp_ring);
865 	u32 batched, leftover, i;
866 
867 	batched = ALIGN_DOWN(nb_pkts, PKTS_PER_BATCH);
868 	leftover = nb_pkts & (PKTS_PER_BATCH - 1);
869 	for (i = 0; i < batched; i += PKTS_PER_BATCH)
870 		ice_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
871 	for (; i < batched + leftover; i++)
872 		ice_xmit_pkt(xdp_ring, &descs[i], total_bytes);
873 
874 	if (xdp_ring->next_to_use > xdp_ring->next_rs) {
875 		struct ice_tx_desc *tx_desc;
876 
877 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_rs);
878 		tx_desc->cmd_type_offset_bsz |=
879 			cpu_to_le64(ICE_TX_DESC_CMD_RS << ICE_TXD_QW1_CMD_S);
880 		xdp_ring->next_rs += tx_thresh;
881 	}
882 }
883 
884 /**
885  * ice_xmit_zc - take entries from XSK Tx ring and place them onto HW Tx ring
886  * @xdp_ring: XDP ring to produce the HW Tx descriptors on
887  * @budget: number of free descriptors on HW Tx ring that can be used
888  * @napi_budget: amount of descriptors that NAPI allows us to clean
889  *
890  * Returns true if there is no more work that needs to be done, false otherwise
891  */
892 bool ice_xmit_zc(struct ice_tx_ring *xdp_ring, u32 budget, int napi_budget)
893 {
894 	struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs;
895 	u16 tx_thresh = ICE_RING_QUARTER(xdp_ring);
896 	u32 nb_pkts, nb_processed = 0;
897 	unsigned int total_bytes = 0;
898 
899 	if (budget < tx_thresh)
900 		budget += ice_clean_xdp_irq_zc(xdp_ring, napi_budget);
901 
902 	nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget);
903 	if (!nb_pkts)
904 		return true;
905 
906 	if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
907 		struct ice_tx_desc *tx_desc;
908 
909 		nb_processed = xdp_ring->count - xdp_ring->next_to_use;
910 		ice_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
911 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_rs);
912 		tx_desc->cmd_type_offset_bsz |=
913 			cpu_to_le64(ICE_TX_DESC_CMD_RS << ICE_TXD_QW1_CMD_S);
914 		xdp_ring->next_rs = tx_thresh - 1;
915 		xdp_ring->next_to_use = 0;
916 	}
917 
918 	ice_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
919 			    &total_bytes);
920 
921 	ice_xdp_ring_update_tail(xdp_ring);
922 	ice_update_tx_ring_stats(xdp_ring, nb_pkts, total_bytes);
923 
924 	if (xsk_uses_need_wakeup(xdp_ring->xsk_pool))
925 		xsk_set_tx_need_wakeup(xdp_ring->xsk_pool);
926 
927 	return nb_pkts < budget;
928 }
929 
930 /**
931  * ice_xsk_wakeup - Implements ndo_xsk_wakeup
932  * @netdev: net_device
933  * @queue_id: queue to wake up
934  * @flags: ignored in our case, since we have Rx and Tx in the same NAPI
935  *
936  * Returns negative on error, zero otherwise.
937  */
938 int
939 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id,
940 	       u32 __always_unused flags)
941 {
942 	struct ice_netdev_priv *np = netdev_priv(netdev);
943 	struct ice_q_vector *q_vector;
944 	struct ice_vsi *vsi = np->vsi;
945 	struct ice_tx_ring *ring;
946 
947 	if (test_bit(ICE_VSI_DOWN, vsi->state))
948 		return -ENETDOWN;
949 
950 	if (!ice_is_xdp_ena_vsi(vsi))
951 		return -EINVAL;
952 
953 	if (queue_id >= vsi->num_txq || queue_id >= vsi->num_rxq)
954 		return -EINVAL;
955 
956 	ring = vsi->rx_rings[queue_id]->xdp_ring;
957 
958 	if (!ring->xsk_pool)
959 		return -EINVAL;
960 
961 	/* The idea here is that if NAPI is running, mark a miss, so
962 	 * it will run again. If not, trigger an interrupt and
963 	 * schedule the NAPI from interrupt context. If NAPI would be
964 	 * scheduled here, the interrupt affinity would not be
965 	 * honored.
966 	 */
967 	q_vector = ring->q_vector;
968 	if (!napi_if_scheduled_mark_missed(&q_vector->napi))
969 		ice_trigger_sw_intr(&vsi->back->hw, q_vector);
970 
971 	return 0;
972 }
973 
974 /**
975  * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP buff pool attached
976  * @vsi: VSI to be checked
977  *
978  * Returns true if any of the Rx rings has an AF_XDP buff pool attached
979  */
980 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi)
981 {
982 	int i;
983 
984 	ice_for_each_rxq(vsi, i) {
985 		if (xsk_get_pool_from_qid(vsi->netdev, i))
986 			return true;
987 	}
988 
989 	return false;
990 }
991 
992 /**
993  * ice_xsk_clean_rx_ring - clean buffer pool queues connected to a given Rx ring
994  * @rx_ring: ring to be cleaned
995  */
996 void ice_xsk_clean_rx_ring(struct ice_rx_ring *rx_ring)
997 {
998 	u16 count_mask = rx_ring->count - 1;
999 	u16 ntc = rx_ring->next_to_clean;
1000 	u16 ntu = rx_ring->next_to_use;
1001 
1002 	for ( ; ntc != ntu; ntc = (ntc + 1) & count_mask) {
1003 		struct xdp_buff *xdp = *ice_xdp_buf(rx_ring, ntc);
1004 
1005 		xsk_buff_free(xdp);
1006 	}
1007 }
1008 
1009 /**
1010  * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its buffer pool queues
1011  * @xdp_ring: XDP_Tx ring
1012  */
1013 void ice_xsk_clean_xdp_ring(struct ice_tx_ring *xdp_ring)
1014 {
1015 	u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use;
1016 	u32 xsk_frames = 0;
1017 
1018 	while (ntc != ntu) {
1019 		struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
1020 
1021 		if (tx_buf->raw_buf)
1022 			ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
1023 		else
1024 			xsk_frames++;
1025 
1026 		tx_buf->raw_buf = NULL;
1027 
1028 		ntc++;
1029 		if (ntc >= xdp_ring->count)
1030 			ntc = 0;
1031 	}
1032 
1033 	if (xsk_frames)
1034 		xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
1035 }
1036