1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock.h>
12 #include "ice_txrx_lib.h"
16 * ice_qp_reset_stats - Resets all stats for rings of given index
17 * @vsi: VSI that contains rings of interest
18 * @q_idx: ring index in array
20 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx)
22 memset(&vsi->rx_rings[q_idx]->rx_stats, 0,
23 sizeof(vsi->rx_rings[q_idx]->rx_stats));
24 memset(&vsi->tx_rings[q_idx]->stats, 0,
25 sizeof(vsi->tx_rings[q_idx]->stats));
26 if (ice_is_xdp_ena_vsi(vsi))
27 memset(&vsi->xdp_rings[q_idx]->stats, 0,
28 sizeof(vsi->xdp_rings[q_idx]->stats));
32 * ice_qp_clean_rings - Cleans all the rings of a given index
33 * @vsi: VSI that contains rings of interest
34 * @q_idx: ring index in array
36 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx)
38 ice_clean_tx_ring(vsi->tx_rings[q_idx]);
39 if (ice_is_xdp_ena_vsi(vsi))
40 ice_clean_tx_ring(vsi->xdp_rings[q_idx]);
41 ice_clean_rx_ring(vsi->rx_rings[q_idx]);
45 * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector
46 * @vsi: VSI that has netdev
47 * @q_vector: q_vector that has NAPI context
48 * @enable: true for enable, false for disable
51 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector,
54 if (!vsi->netdev || !q_vector)
58 napi_enable(&q_vector->napi);
60 napi_disable(&q_vector->napi);
64 * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring
65 * @vsi: the VSI that contains queue vector being un-configured
66 * @rx_ring: Rx ring that will have its IRQ disabled
67 * @q_vector: queue vector
70 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_ring *rx_ring,
71 struct ice_q_vector *q_vector)
73 struct ice_pf *pf = vsi->back;
74 struct ice_hw *hw = &pf->hw;
75 int base = vsi->base_vector;
79 /* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle
80 * here only QINT_RQCTL
82 reg = rx_ring->reg_idx;
83 val = rd32(hw, QINT_RQCTL(reg));
84 val &= ~QINT_RQCTL_CAUSE_ENA_M;
85 wr32(hw, QINT_RQCTL(reg), val);
88 u16 v_idx = q_vector->v_idx;
90 wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0);
92 synchronize_irq(pf->msix_entries[v_idx + base].vector);
97 * ice_qvec_cfg_msix - Enable IRQ for given queue vector
98 * @vsi: the VSI that contains queue vector
99 * @q_vector: queue vector
102 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
104 u16 reg_idx = q_vector->reg_idx;
105 struct ice_pf *pf = vsi->back;
106 struct ice_hw *hw = &pf->hw;
107 struct ice_ring *ring;
109 ice_cfg_itr(hw, q_vector);
111 wr32(hw, GLINT_RATE(reg_idx),
112 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
114 ice_for_each_ring(ring, q_vector->tx)
115 ice_cfg_txq_interrupt(vsi, ring->reg_idx, reg_idx,
116 q_vector->tx.itr_idx);
118 ice_for_each_ring(ring, q_vector->rx)
119 ice_cfg_rxq_interrupt(vsi, ring->reg_idx, reg_idx,
120 q_vector->rx.itr_idx);
126 * ice_qvec_ena_irq - Enable IRQ for given queue vector
127 * @vsi: the VSI that contains queue vector
128 * @q_vector: queue vector
130 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
132 struct ice_pf *pf = vsi->back;
133 struct ice_hw *hw = &pf->hw;
135 ice_irq_dynamic_ena(hw, vsi, q_vector);
141 * ice_qp_dis - Disables a queue pair
142 * @vsi: VSI of interest
143 * @q_idx: ring index in array
145 * Returns 0 on success, negative on failure.
147 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx)
149 struct ice_txq_meta txq_meta = { };
150 struct ice_ring *tx_ring, *rx_ring;
151 struct ice_q_vector *q_vector;
155 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
158 tx_ring = vsi->tx_rings[q_idx];
159 rx_ring = vsi->rx_rings[q_idx];
160 q_vector = rx_ring->q_vector;
162 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state)) {
166 usleep_range(1000, 2000);
168 netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
170 ice_qvec_dis_irq(vsi, rx_ring, q_vector);
172 ice_fill_txq_meta(vsi, tx_ring, &txq_meta);
173 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta);
176 if (ice_is_xdp_ena_vsi(vsi)) {
177 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx];
179 memset(&txq_meta, 0, sizeof(txq_meta));
180 ice_fill_txq_meta(vsi, xdp_ring, &txq_meta);
181 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring,
186 err = ice_vsi_ctrl_rx_ring(vsi, false, q_idx);
190 ice_qvec_toggle_napi(vsi, q_vector, false);
191 ice_qp_clean_rings(vsi, q_idx);
192 ice_qp_reset_stats(vsi, q_idx);
198 * ice_qp_ena - Enables a queue pair
199 * @vsi: VSI of interest
200 * @q_idx: ring index in array
202 * Returns 0 on success, negative on failure.
204 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx)
206 struct ice_aqc_add_tx_qgrp *qg_buf;
207 struct ice_ring *tx_ring, *rx_ring;
208 struct ice_q_vector *q_vector;
211 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
214 qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
218 qg_buf->num_txqs = 1;
220 tx_ring = vsi->tx_rings[q_idx];
221 rx_ring = vsi->rx_rings[q_idx];
222 q_vector = rx_ring->q_vector;
224 err = ice_vsi_cfg_txq(vsi, tx_ring, qg_buf);
228 if (ice_is_xdp_ena_vsi(vsi)) {
229 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx];
231 memset(qg_buf, 0, sizeof(*qg_buf));
232 qg_buf->num_txqs = 1;
233 err = ice_vsi_cfg_txq(vsi, xdp_ring, qg_buf);
236 ice_set_ring_xdp(xdp_ring);
237 xdp_ring->xsk_umem = ice_xsk_umem(xdp_ring);
240 err = ice_setup_rx_ctx(rx_ring);
244 ice_qvec_cfg_msix(vsi, q_vector);
246 err = ice_vsi_ctrl_rx_ring(vsi, true, q_idx);
250 clear_bit(__ICE_CFG_BUSY, vsi->state);
251 ice_qvec_toggle_napi(vsi, q_vector, true);
252 ice_qvec_ena_irq(vsi, q_vector);
254 netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
261 * ice_xsk_alloc_umems - allocate a UMEM region for an XDP socket
262 * @vsi: VSI to allocate the UMEM on
264 * Returns 0 on success, negative on error
266 static int ice_xsk_alloc_umems(struct ice_vsi *vsi)
271 vsi->xsk_umems = kcalloc(vsi->num_xsk_umems, sizeof(*vsi->xsk_umems),
274 if (!vsi->xsk_umems) {
275 vsi->num_xsk_umems = 0;
283 * ice_xsk_add_umem - add a UMEM region for XDP sockets
284 * @vsi: VSI to which the UMEM will be added
285 * @umem: pointer to a requested UMEM region
288 * Returns 0 on success, negative on error
290 static int ice_xsk_add_umem(struct ice_vsi *vsi, struct xdp_umem *umem, u16 qid)
294 err = ice_xsk_alloc_umems(vsi);
298 vsi->xsk_umems[qid] = umem;
299 vsi->num_xsk_umems_used++;
305 * ice_xsk_remove_umem - Remove an UMEM for a certain ring/qid
306 * @vsi: VSI from which the VSI will be removed
307 * @qid: Ring/qid associated with the UMEM
309 static void ice_xsk_remove_umem(struct ice_vsi *vsi, u16 qid)
311 vsi->xsk_umems[qid] = NULL;
312 vsi->num_xsk_umems_used--;
314 if (vsi->num_xsk_umems_used == 0) {
315 kfree(vsi->xsk_umems);
316 vsi->xsk_umems = NULL;
317 vsi->num_xsk_umems = 0;
322 * ice_xsk_umem_dma_map - DMA map UMEM region for XDP sockets
323 * @vsi: VSI to map the UMEM region
326 * Returns 0 on success, negative on error
328 static int ice_xsk_umem_dma_map(struct ice_vsi *vsi, struct xdp_umem *umem)
330 struct ice_pf *pf = vsi->back;
334 dev = ice_pf_to_dev(pf);
335 for (i = 0; i < umem->npgs; i++) {
336 dma_addr_t dma = dma_map_page_attrs(dev, umem->pgs[i], 0,
340 if (dma_mapping_error(dev, dma)) {
342 "XSK UMEM DMA mapping error on page num %d", i);
346 umem->pages[i].dma = dma;
353 dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE,
354 DMA_BIDIRECTIONAL, ICE_RX_DMA_ATTR);
355 umem->pages[i].dma = 0;
362 * ice_xsk_umem_dma_unmap - DMA unmap UMEM region for XDP sockets
363 * @vsi: VSI from which the UMEM will be unmapped
364 * @umem: UMEM to unmap
366 static void ice_xsk_umem_dma_unmap(struct ice_vsi *vsi, struct xdp_umem *umem)
368 struct ice_pf *pf = vsi->back;
372 dev = ice_pf_to_dev(pf);
373 for (i = 0; i < umem->npgs; i++) {
374 dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE,
375 DMA_BIDIRECTIONAL, ICE_RX_DMA_ATTR);
377 umem->pages[i].dma = 0;
382 * ice_xsk_umem_disable - disable a UMEM region
386 * Returns 0 on success, negative on failure
388 static int ice_xsk_umem_disable(struct ice_vsi *vsi, u16 qid)
390 if (!vsi->xsk_umems || qid >= vsi->num_xsk_umems ||
391 !vsi->xsk_umems[qid])
394 ice_xsk_umem_dma_unmap(vsi, vsi->xsk_umems[qid]);
395 ice_xsk_remove_umem(vsi, qid);
401 * ice_xsk_umem_enable - enable a UMEM region
403 * @umem: pointer to a requested UMEM region
406 * Returns 0 on success, negative on failure
409 ice_xsk_umem_enable(struct ice_vsi *vsi, struct xdp_umem *umem, u16 qid)
411 struct xdp_umem_fq_reuse *reuseq;
414 if (vsi->type != ICE_VSI_PF)
417 vsi->num_xsk_umems = min_t(u16, vsi->num_rxq, vsi->num_txq);
418 if (qid >= vsi->num_xsk_umems)
421 if (vsi->xsk_umems && vsi->xsk_umems[qid])
424 reuseq = xsk_reuseq_prepare(vsi->rx_rings[0]->count);
428 xsk_reuseq_free(xsk_reuseq_swap(umem, reuseq));
430 err = ice_xsk_umem_dma_map(vsi, umem);
434 err = ice_xsk_add_umem(vsi, umem, qid);
442 * ice_xsk_umem_setup - enable/disable a UMEM region depending on its state
444 * @umem: UMEM to enable/associate to a ring, NULL to disable
447 * Returns 0 on success, negative on failure
449 int ice_xsk_umem_setup(struct ice_vsi *vsi, struct xdp_umem *umem, u16 qid)
451 bool if_running, umem_present = !!umem;
452 int ret = 0, umem_failure = 0;
454 if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi);
457 ret = ice_qp_dis(vsi, qid);
459 netdev_err(vsi->netdev, "ice_qp_dis error = %d", ret);
464 umem_failure = umem_present ? ice_xsk_umem_enable(vsi, umem, qid) :
465 ice_xsk_umem_disable(vsi, qid);
469 ret = ice_qp_ena(vsi, qid);
470 if (!ret && umem_present)
471 napi_schedule(&vsi->xdp_rings[qid]->q_vector->napi);
473 netdev_err(vsi->netdev, "ice_qp_ena error = %d", ret);
477 netdev_err(vsi->netdev, "Could not %sable UMEM, error = %d",
478 umem_present ? "en" : "dis", umem_failure);
486 * ice_zca_free - Callback for MEM_TYPE_ZERO_COPY allocations
487 * @zca: zero-cpoy allocator
488 * @handle: Buffer handle
490 void ice_zca_free(struct zero_copy_allocator *zca, unsigned long handle)
492 struct ice_rx_buf *rx_buf;
493 struct ice_ring *rx_ring;
494 struct xdp_umem *umem;
498 rx_ring = container_of(zca, struct ice_ring, zca);
499 umem = rx_ring->xsk_umem;
500 hr = umem->headroom + XDP_PACKET_HEADROOM;
502 mask = umem->chunk_mask;
504 nta = rx_ring->next_to_alloc;
505 rx_buf = &rx_ring->rx_buf[nta];
508 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
512 rx_buf->dma = xdp_umem_get_dma(umem, handle);
515 rx_buf->addr = xdp_umem_get_data(umem, handle);
518 rx_buf->handle = (u64)handle + umem->headroom;
522 * ice_alloc_buf_fast_zc - Retrieve buffer address from XDP umem
523 * @rx_ring: ring with an xdp_umem bound to it
524 * @rx_buf: buffer to which xsk page address will be assigned
526 * This function allocates an Rx buffer in the hot path.
527 * The buffer can come from fill queue or recycle queue.
529 * Returns true if an assignment was successful, false if not.
531 static __always_inline bool
532 ice_alloc_buf_fast_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf)
534 struct xdp_umem *umem = rx_ring->xsk_umem;
535 void *addr = rx_buf->addr;
539 rx_ring->rx_stats.page_reuse_count++;
543 if (!xsk_umem_peek_addr(umem, &handle)) {
544 rx_ring->rx_stats.alloc_page_failed++;
548 hr = umem->headroom + XDP_PACKET_HEADROOM;
550 rx_buf->dma = xdp_umem_get_dma(umem, handle);
553 rx_buf->addr = xdp_umem_get_data(umem, handle);
556 rx_buf->handle = handle + umem->headroom;
558 xsk_umem_discard_addr(umem);
563 * ice_alloc_buf_slow_zc - Retrieve buffer address from XDP umem
564 * @rx_ring: ring with an xdp_umem bound to it
565 * @rx_buf: buffer to which xsk page address will be assigned
567 * This function allocates an Rx buffer in the slow path.
568 * The buffer can come from fill queue or recycle queue.
570 * Returns true if an assignment was successful, false if not.
572 static __always_inline bool
573 ice_alloc_buf_slow_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf)
575 struct xdp_umem *umem = rx_ring->xsk_umem;
576 u64 handle, headroom;
578 if (!xsk_umem_peek_addr_rq(umem, &handle)) {
579 rx_ring->rx_stats.alloc_page_failed++;
583 handle &= umem->chunk_mask;
584 headroom = umem->headroom + XDP_PACKET_HEADROOM;
586 rx_buf->dma = xdp_umem_get_dma(umem, handle);
587 rx_buf->dma += headroom;
589 rx_buf->addr = xdp_umem_get_data(umem, handle);
590 rx_buf->addr += headroom;
592 rx_buf->handle = handle + umem->headroom;
594 xsk_umem_discard_addr_rq(umem);
599 * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
601 * @count: The number of buffers to allocate
602 * @alloc: the function pointer to call for allocation
604 * This function allocates a number of Rx buffers from the fill ring
605 * or the internal recycle mechanism and places them on the Rx ring.
607 * Returns false if all allocations were successful, true if any fail.
610 ice_alloc_rx_bufs_zc(struct ice_ring *rx_ring, int count,
611 bool alloc(struct ice_ring *, struct ice_rx_buf *))
613 union ice_32b_rx_flex_desc *rx_desc;
614 u16 ntu = rx_ring->next_to_use;
615 struct ice_rx_buf *rx_buf;
621 rx_desc = ICE_RX_DESC(rx_ring, ntu);
622 rx_buf = &rx_ring->rx_buf[ntu];
625 if (!alloc(rx_ring, rx_buf)) {
630 dma_sync_single_range_for_device(rx_ring->dev, rx_buf->dma, 0,
634 rx_desc->read.pkt_addr = cpu_to_le64(rx_buf->dma);
635 rx_desc->wb.status_error0 = 0;
641 if (unlikely(ntu == rx_ring->count)) {
642 rx_desc = ICE_RX_DESC(rx_ring, 0);
643 rx_buf = rx_ring->rx_buf;
648 if (rx_ring->next_to_use != ntu)
649 ice_release_rx_desc(rx_ring, ntu);
655 * ice_alloc_rx_bufs_fast_zc - allocate zero copy bufs in the hot path
657 * @count: number of bufs to allocate
659 * Returns false on success, true on failure.
661 static bool ice_alloc_rx_bufs_fast_zc(struct ice_ring *rx_ring, u16 count)
663 return ice_alloc_rx_bufs_zc(rx_ring, count,
664 ice_alloc_buf_fast_zc);
668 * ice_alloc_rx_bufs_slow_zc - allocate zero copy bufs in the slow path
670 * @count: number of bufs to allocate
672 * Returns false on success, true on failure.
674 bool ice_alloc_rx_bufs_slow_zc(struct ice_ring *rx_ring, u16 count)
676 return ice_alloc_rx_bufs_zc(rx_ring, count,
677 ice_alloc_buf_slow_zc);
681 * ice_bump_ntc - Bump the next_to_clean counter of an Rx ring
684 static void ice_bump_ntc(struct ice_ring *rx_ring)
686 int ntc = rx_ring->next_to_clean + 1;
688 ntc = (ntc < rx_ring->count) ? ntc : 0;
689 rx_ring->next_to_clean = ntc;
690 prefetch(ICE_RX_DESC(rx_ring, ntc));
694 * ice_get_rx_buf_zc - Fetch the current Rx buffer
696 * @size: size of a buffer
698 * This function returns the current, received Rx buffer and does
699 * DMA synchronization.
701 * Returns a pointer to the received Rx buffer.
703 static struct ice_rx_buf *ice_get_rx_buf_zc(struct ice_ring *rx_ring, int size)
705 struct ice_rx_buf *rx_buf;
707 rx_buf = &rx_ring->rx_buf[rx_ring->next_to_clean];
709 dma_sync_single_range_for_cpu(rx_ring->dev, rx_buf->dma, 0,
710 size, DMA_BIDIRECTIONAL);
716 * ice_reuse_rx_buf_zc - reuse an Rx buffer
718 * @old_buf: The buffer to recycle
720 * This function recycles a finished Rx buffer, and places it on the recycle
721 * queue (next_to_alloc).
724 ice_reuse_rx_buf_zc(struct ice_ring *rx_ring, struct ice_rx_buf *old_buf)
726 unsigned long mask = (unsigned long)rx_ring->xsk_umem->chunk_mask;
727 u64 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM;
728 u16 nta = rx_ring->next_to_alloc;
729 struct ice_rx_buf *new_buf;
731 new_buf = &rx_ring->rx_buf[nta++];
732 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
734 new_buf->dma = old_buf->dma & mask;
737 new_buf->addr = (void *)((unsigned long)old_buf->addr & mask);
740 new_buf->handle = old_buf->handle & mask;
741 new_buf->handle += rx_ring->xsk_umem->headroom;
743 old_buf->addr = NULL;
747 * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer
749 * @rx_buf: zero-copy Rx buffer
752 * This function allocates a new skb from a zero-copy Rx buffer.
754 * Returns the skb on success, NULL on failure.
756 static struct sk_buff *
757 ice_construct_skb_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf,
758 struct xdp_buff *xdp)
760 unsigned int metasize = xdp->data - xdp->data_meta;
761 unsigned int datasize = xdp->data_end - xdp->data;
762 unsigned int datasize_hard = xdp->data_end -
763 xdp->data_hard_start;
766 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, datasize_hard,
767 GFP_ATOMIC | __GFP_NOWARN);
771 skb_reserve(skb, xdp->data - xdp->data_hard_start);
772 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
774 skb_metadata_set(skb, metasize);
776 ice_reuse_rx_buf_zc(rx_ring, rx_buf);
782 * ice_run_xdp_zc - Executes an XDP program in zero-copy path
784 * @xdp: xdp_buff used as input to the XDP program
786 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
789 ice_run_xdp_zc(struct ice_ring *rx_ring, struct xdp_buff *xdp)
791 int err, result = ICE_XDP_PASS;
792 struct bpf_prog *xdp_prog;
793 struct ice_ring *xdp_ring;
797 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
803 act = bpf_prog_run_xdp(xdp_prog, xdp);
804 xdp->handle += xdp->data - xdp->data_hard_start;
809 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->q_index];
810 result = ice_xmit_xdp_buff(xdp, xdp_ring);
813 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
814 result = !err ? ICE_XDP_REDIR : ICE_XDP_CONSUMED;
817 bpf_warn_invalid_xdp_action(act);
818 /* fallthrough -- not supported action */
820 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
821 /* fallthrough -- handle aborts by dropping frame */
823 result = ICE_XDP_CONSUMED;
832 * ice_clean_rx_irq_zc - consumes packets from the hardware ring
833 * @rx_ring: AF_XDP Rx ring
834 * @budget: NAPI budget
836 * Returns number of processed packets on success, remaining budget on failure.
838 int ice_clean_rx_irq_zc(struct ice_ring *rx_ring, int budget)
840 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
841 u16 cleaned_count = ICE_DESC_UNUSED(rx_ring);
842 unsigned int xdp_xmit = 0;
846 xdp.rxq = &rx_ring->xdp_rxq;
848 while (likely(total_rx_packets < (unsigned int)budget)) {
849 union ice_32b_rx_flex_desc *rx_desc;
850 unsigned int size, xdp_res = 0;
851 struct ice_rx_buf *rx_buf;
857 if (cleaned_count >= ICE_RX_BUF_WRITE) {
858 failure |= ice_alloc_rx_bufs_fast_zc(rx_ring,
863 rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
865 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
866 if (!ice_test_staterr(rx_desc, stat_err_bits))
869 /* This memory barrier is needed to keep us from reading
870 * any other fields out of the rx_desc until we have
871 * verified the descriptor has been written back.
875 size = le16_to_cpu(rx_desc->wb.pkt_len) &
876 ICE_RX_FLX_DESC_PKT_LEN_M;
880 rx_buf = ice_get_rx_buf_zc(rx_ring, size);
884 xdp.data = rx_buf->addr;
885 xdp.data_meta = xdp.data;
886 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
887 xdp.data_end = xdp.data + size;
888 xdp.handle = rx_buf->handle;
890 xdp_res = ice_run_xdp_zc(rx_ring, &xdp);
892 if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR)) {
896 ice_reuse_rx_buf_zc(rx_ring, rx_buf);
899 total_rx_bytes += size;
903 ice_bump_ntc(rx_ring);
908 skb = ice_construct_skb_zc(rx_ring, rx_buf, &xdp);
910 rx_ring->rx_stats.alloc_buf_failed++;
915 ice_bump_ntc(rx_ring);
917 if (eth_skb_pad(skb)) {
922 total_rx_bytes += skb->len;
925 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S);
926 if (ice_test_staterr(rx_desc, stat_err_bits))
927 vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1);
929 rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
930 ICE_RX_FLEX_DESC_PTYPE_M;
932 ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
933 ice_receive_skb(rx_ring, skb, vlan_tag);
936 ice_finalize_xdp_rx(rx_ring, xdp_xmit);
937 ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes);
939 return failure ? budget : (int)total_rx_packets;
943 * ice_xmit_zc - Completes AF_XDP entries, and cleans XDP entries
944 * @xdp_ring: XDP Tx ring
945 * @budget: max number of frames to xmit
947 * Returns true if cleanup/transmission is done.
949 static bool ice_xmit_zc(struct ice_ring *xdp_ring, int budget)
951 struct ice_tx_desc *tx_desc = NULL;
952 bool work_done = true;
953 struct xdp_desc desc;
956 while (likely(budget-- > 0)) {
957 struct ice_tx_buf *tx_buf;
959 if (unlikely(!ICE_DESC_UNUSED(xdp_ring))) {
960 xdp_ring->tx_stats.tx_busy++;
965 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use];
967 if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &desc))
970 dma = xdp_umem_get_dma(xdp_ring->xsk_umem, desc.addr);
972 dma_sync_single_for_device(xdp_ring->dev, dma, desc.len,
975 tx_buf->bytecount = desc.len;
977 tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use);
978 tx_desc->buf_addr = cpu_to_le64(dma);
979 tx_desc->cmd_type_offset_bsz = build_ctob(ICE_TXD_LAST_DESC_CMD,
982 xdp_ring->next_to_use++;
983 if (xdp_ring->next_to_use == xdp_ring->count)
984 xdp_ring->next_to_use = 0;
988 ice_xdp_ring_update_tail(xdp_ring);
989 xsk_umem_consume_tx_done(xdp_ring->xsk_umem);
992 return budget > 0 && work_done;
996 * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
997 * @xdp_ring: XDP Tx ring
998 * @tx_buf: Tx buffer to clean
1001 ice_clean_xdp_tx_buf(struct ice_ring *xdp_ring, struct ice_tx_buf *tx_buf)
1003 xdp_return_frame((struct xdp_frame *)tx_buf->raw_buf);
1004 dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma),
1005 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
1006 dma_unmap_len_set(tx_buf, len, 0);
1010 * ice_clean_tx_irq_zc - Completes AF_XDP entries, and cleans XDP entries
1011 * @xdp_ring: XDP Tx ring
1012 * @budget: NAPI budget
1014 * Returns true if cleanup/tranmission is done.
1016 bool ice_clean_tx_irq_zc(struct ice_ring *xdp_ring, int budget)
1018 int total_packets = 0, total_bytes = 0;
1019 s16 ntc = xdp_ring->next_to_clean;
1020 struct ice_tx_desc *tx_desc;
1021 struct ice_tx_buf *tx_buf;
1022 bool xmit_done = true;
1025 tx_desc = ICE_TX_DESC(xdp_ring, ntc);
1026 tx_buf = &xdp_ring->tx_buf[ntc];
1027 ntc -= xdp_ring->count;
1030 if (!(tx_desc->cmd_type_offset_bsz &
1031 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
1034 total_bytes += tx_buf->bytecount;
1037 if (tx_buf->raw_buf) {
1038 ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
1039 tx_buf->raw_buf = NULL;
1044 tx_desc->cmd_type_offset_bsz = 0;
1049 if (unlikely(!ntc)) {
1050 ntc -= xdp_ring->count;
1051 tx_buf = xdp_ring->tx_buf;
1052 tx_desc = ICE_TX_DESC(xdp_ring, 0);
1057 } while (likely(--budget));
1059 ntc += xdp_ring->count;
1060 xdp_ring->next_to_clean = ntc;
1063 xsk_umem_complete_tx(xdp_ring->xsk_umem, xsk_frames);
1065 ice_update_tx_ring_stats(xdp_ring, total_packets, total_bytes);
1066 xmit_done = ice_xmit_zc(xdp_ring, ICE_DFLT_IRQ_WORK);
1068 return budget > 0 && xmit_done;
1072 * ice_xsk_wakeup - Implements ndo_xsk_wakeup
1073 * @netdev: net_device
1074 * @queue_id: queue to wake up
1075 * @flags: ignored in our case, since we have Rx and Tx in the same NAPI
1077 * Returns negative on error, zero otherwise.
1080 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id,
1081 u32 __always_unused flags)
1083 struct ice_netdev_priv *np = netdev_priv(netdev);
1084 struct ice_q_vector *q_vector;
1085 struct ice_vsi *vsi = np->vsi;
1086 struct ice_ring *ring;
1088 if (test_bit(__ICE_DOWN, vsi->state))
1091 if (!ice_is_xdp_ena_vsi(vsi))
1094 if (queue_id >= vsi->num_txq)
1097 if (!vsi->xdp_rings[queue_id]->xsk_umem)
1100 ring = vsi->xdp_rings[queue_id];
1102 /* The idea here is that if NAPI is running, mark a miss, so
1103 * it will run again. If not, trigger an interrupt and
1104 * schedule the NAPI from interrupt context. If NAPI would be
1105 * scheduled here, the interrupt affinity would not be
1108 q_vector = ring->q_vector;
1109 if (!napi_if_scheduled_mark_missed(&q_vector->napi))
1110 ice_trigger_sw_intr(&vsi->back->hw, q_vector);
1116 * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP UMEM attached
1117 * @vsi: VSI to be checked
1119 * Returns true if any of the Rx rings has an AF_XDP UMEM attached
1121 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi)
1125 if (!vsi->xsk_umems)
1128 for (i = 0; i < vsi->num_xsk_umems; i++) {
1129 if (vsi->xsk_umems[i])
1137 * ice_xsk_clean_rx_ring - clean UMEM queues connected to a given Rx ring
1138 * @rx_ring: ring to be cleaned
1140 void ice_xsk_clean_rx_ring(struct ice_ring *rx_ring)
1144 for (i = 0; i < rx_ring->count; i++) {
1145 struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
1150 xsk_umem_fq_reuse(rx_ring->xsk_umem, rx_buf->handle);
1151 rx_buf->addr = NULL;
1156 * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its UMEM queues
1157 * @xdp_ring: XDP_Tx ring
1159 void ice_xsk_clean_xdp_ring(struct ice_ring *xdp_ring)
1161 u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use;
1164 while (ntc != ntu) {
1165 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
1167 if (tx_buf->raw_buf)
1168 ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
1172 tx_buf->raw_buf = NULL;
1175 if (ntc >= xdp_ring->count)
1180 xsk_umem_complete_tx(xdp_ring->xsk_umem, xsk_frames);