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