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