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