File Index
Files and directories
Sign in
FILE

ice_xsk.c

File preview with syntax highlighting.

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