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