10a714186SBjörn Töpel // SPDX-License-Identifier: GPL-2.0 20a714186SBjörn Töpel /* Copyright(c) 2018 Intel Corporation. */ 30a714186SBjörn Töpel 40a714186SBjörn Töpel #include <linux/bpf_trace.h> 5a71506a4SMagnus Karlsson #include <net/xdp_sock_drv.h> 60a714186SBjörn Töpel #include <net/xdp.h> 70a714186SBjörn Töpel 80a714186SBjörn Töpel #include "i40e.h" 90a714186SBjörn Töpel #include "i40e_txrx_common.h" 100a714186SBjörn Töpel #include "i40e_xsk.h" 110a714186SBjörn Töpel 12be1222b5SBjörn Töpel int i40e_alloc_rx_bi_zc(struct i40e_ring *rx_ring) 13e1675f97SBjörn Töpel { 14be1222b5SBjörn Töpel unsigned long sz = sizeof(*rx_ring->rx_bi_zc) * rx_ring->count; 15be1222b5SBjörn Töpel 16be1222b5SBjörn Töpel rx_ring->rx_bi_zc = kzalloc(sz, GFP_KERNEL); 17be1222b5SBjörn Töpel return rx_ring->rx_bi_zc ? 0 : -ENOMEM; 18be1222b5SBjörn Töpel } 19be1222b5SBjörn Töpel 20be1222b5SBjörn Töpel void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring) 21be1222b5SBjörn Töpel { 22be1222b5SBjörn Töpel memset(rx_ring->rx_bi_zc, 0, 23be1222b5SBjörn Töpel sizeof(*rx_ring->rx_bi_zc) * rx_ring->count); 24be1222b5SBjörn Töpel } 25be1222b5SBjörn Töpel 263b4f0b66SBjörn Töpel static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx) 27be1222b5SBjörn Töpel { 28be1222b5SBjörn Töpel return &rx_ring->rx_bi_zc[idx]; 29e1675f97SBjörn Töpel } 30e1675f97SBjörn Töpel 310a714186SBjörn Töpel /** 32529eb362SJan Sokolowski * i40e_xsk_umem_enable - Enable/associate a UMEM to a certain ring/qid 330a714186SBjörn Töpel * @vsi: Current VSI 340a714186SBjörn Töpel * @umem: UMEM 350a714186SBjörn Töpel * @qid: Rx ring to associate UMEM to 360a714186SBjörn Töpel * 370a714186SBjörn Töpel * Returns 0 on success, <0 on failure 380a714186SBjörn Töpel **/ 390a714186SBjörn Töpel static int i40e_xsk_umem_enable(struct i40e_vsi *vsi, struct xdp_umem *umem, 400a714186SBjörn Töpel u16 qid) 410a714186SBjörn Töpel { 42f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 430a714186SBjörn Töpel bool if_running; 440a714186SBjörn Töpel int err; 450a714186SBjörn Töpel 460a714186SBjörn Töpel if (vsi->type != I40E_VSI_MAIN) 470a714186SBjörn Töpel return -EINVAL; 480a714186SBjörn Töpel 490a714186SBjörn Töpel if (qid >= vsi->num_queue_pairs) 500a714186SBjörn Töpel return -EINVAL; 510a714186SBjörn Töpel 52f3fef2b6SJan Sokolowski if (qid >= netdev->real_num_rx_queues || 53f3fef2b6SJan Sokolowski qid >= netdev->real_num_tx_queues) 540a714186SBjörn Töpel return -EINVAL; 550a714186SBjörn Töpel 563b4f0b66SBjörn Töpel err = xsk_buff_dma_map(umem, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR); 570a714186SBjörn Töpel if (err) 580a714186SBjörn Töpel return err; 590a714186SBjörn Töpel 6044ddd4f1SBjörn Töpel set_bit(qid, vsi->af_xdp_zc_qps); 6144ddd4f1SBjörn Töpel 620a714186SBjörn Töpel if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 630a714186SBjörn Töpel 640a714186SBjörn Töpel if (if_running) { 650a714186SBjörn Töpel err = i40e_queue_pair_disable(vsi, qid); 660a714186SBjörn Töpel if (err) 670a714186SBjörn Töpel return err; 680a714186SBjörn Töpel 690a714186SBjörn Töpel err = i40e_queue_pair_enable(vsi, qid); 700a714186SBjörn Töpel if (err) 710a714186SBjörn Töpel return err; 7214ffeb52SMagnus Karlsson 7314ffeb52SMagnus Karlsson /* Kick start the NAPI context so that receiving will start */ 749116e5e2SMagnus Karlsson err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX); 7514ffeb52SMagnus Karlsson if (err) 7614ffeb52SMagnus Karlsson return err; 770a714186SBjörn Töpel } 780a714186SBjörn Töpel 790a714186SBjörn Töpel return 0; 800a714186SBjörn Töpel } 810a714186SBjörn Töpel 820a714186SBjörn Töpel /** 83529eb362SJan Sokolowski * i40e_xsk_umem_disable - Disassociate a UMEM from a certain ring/qid 840a714186SBjörn Töpel * @vsi: Current VSI 850a714186SBjörn Töpel * @qid: Rx ring to associate UMEM to 860a714186SBjörn Töpel * 870a714186SBjörn Töpel * Returns 0 on success, <0 on failure 880a714186SBjörn Töpel **/ 890a714186SBjörn Töpel static int i40e_xsk_umem_disable(struct i40e_vsi *vsi, u16 qid) 900a714186SBjörn Töpel { 91f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 92f3fef2b6SJan Sokolowski struct xdp_umem *umem; 930a714186SBjörn Töpel bool if_running; 940a714186SBjörn Töpel int err; 950a714186SBjörn Töpel 96f3fef2b6SJan Sokolowski umem = xdp_get_umem_from_qid(netdev, qid); 97f3fef2b6SJan Sokolowski if (!umem) 980a714186SBjörn Töpel return -EINVAL; 990a714186SBjörn Töpel 1000a714186SBjörn Töpel if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 1010a714186SBjörn Töpel 1020a714186SBjörn Töpel if (if_running) { 1030a714186SBjörn Töpel err = i40e_queue_pair_disable(vsi, qid); 1040a714186SBjörn Töpel if (err) 1050a714186SBjörn Töpel return err; 1060a714186SBjörn Töpel } 1070a714186SBjörn Töpel 10844ddd4f1SBjörn Töpel clear_bit(qid, vsi->af_xdp_zc_qps); 1093b4f0b66SBjörn Töpel xsk_buff_dma_unmap(umem, I40E_RX_DMA_ATTR); 1100a714186SBjörn Töpel 1110a714186SBjörn Töpel if (if_running) { 1120a714186SBjörn Töpel err = i40e_queue_pair_enable(vsi, qid); 1130a714186SBjörn Töpel if (err) 1140a714186SBjörn Töpel return err; 1150a714186SBjörn Töpel } 1160a714186SBjörn Töpel 1170a714186SBjörn Töpel return 0; 1180a714186SBjörn Töpel } 1190a714186SBjörn Töpel 1200a714186SBjörn Töpel /** 121529eb362SJan Sokolowski * i40e_xsk_umem_setup - Enable/disassociate a UMEM to/from a ring/qid 1220a714186SBjörn Töpel * @vsi: Current VSI 1230a714186SBjörn Töpel * @umem: UMEM to enable/associate to a ring, or NULL to disable 1240a714186SBjörn Töpel * @qid: Rx ring to (dis)associate UMEM (from)to 1250a714186SBjörn Töpel * 126529eb362SJan Sokolowski * This function enables or disables a UMEM to a certain ring. 1270a714186SBjörn Töpel * 1280a714186SBjörn Töpel * Returns 0 on success, <0 on failure 1290a714186SBjörn Töpel **/ 1300a714186SBjörn Töpel int i40e_xsk_umem_setup(struct i40e_vsi *vsi, struct xdp_umem *umem, 1310a714186SBjörn Töpel u16 qid) 1320a714186SBjörn Töpel { 1330a714186SBjörn Töpel return umem ? i40e_xsk_umem_enable(vsi, umem, qid) : 1340a714186SBjörn Töpel i40e_xsk_umem_disable(vsi, qid); 1350a714186SBjörn Töpel } 1360a714186SBjörn Töpel 1370a714186SBjörn Töpel /** 1380a714186SBjörn Töpel * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff 1390a714186SBjörn Töpel * @rx_ring: Rx ring 1400a714186SBjörn Töpel * @xdp: xdp_buff used as input to the XDP program 1410a714186SBjörn Töpel * 1420a714186SBjörn Töpel * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR} 1430a714186SBjörn Töpel **/ 1440a714186SBjörn Töpel static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) 1450a714186SBjörn Töpel { 1460a714186SBjörn Töpel int err, result = I40E_XDP_PASS; 1470a714186SBjörn Töpel struct i40e_ring *xdp_ring; 1480a714186SBjörn Töpel struct bpf_prog *xdp_prog; 1490a714186SBjörn Töpel u32 act; 1500a714186SBjörn Töpel 1510a714186SBjörn Töpel rcu_read_lock(); 1520a714186SBjörn Töpel /* NB! xdp_prog will always be !NULL, due to the fact that 1530a714186SBjörn Töpel * this path is enabled by setting an XDP program. 1540a714186SBjörn Töpel */ 1550a714186SBjörn Töpel xdp_prog = READ_ONCE(rx_ring->xdp_prog); 1560a714186SBjörn Töpel act = bpf_prog_run_xdp(xdp_prog, xdp); 1572f86c806SKevin Laatz 1580a714186SBjörn Töpel switch (act) { 1590a714186SBjörn Töpel case XDP_PASS: 1600a714186SBjörn Töpel break; 1610a714186SBjörn Töpel case XDP_TX: 1620a714186SBjörn Töpel xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index]; 1630a714186SBjörn Töpel result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring); 1640a714186SBjörn Töpel break; 1650a714186SBjörn Töpel case XDP_REDIRECT: 1660a714186SBjörn Töpel err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 1670a714186SBjörn Töpel result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED; 1680a714186SBjörn Töpel break; 1690a714186SBjörn Töpel default: 1700a714186SBjörn Töpel bpf_warn_invalid_xdp_action(act); 1715463fce6SJeff Kirsher fallthrough; 1720a714186SBjörn Töpel case XDP_ABORTED: 1730a714186SBjörn Töpel trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 1745463fce6SJeff Kirsher fallthrough; /* handle aborts by dropping packet */ 1750a714186SBjörn Töpel case XDP_DROP: 1760a714186SBjörn Töpel result = I40E_XDP_CONSUMED; 1770a714186SBjörn Töpel break; 1780a714186SBjörn Töpel } 1790a714186SBjörn Töpel rcu_read_unlock(); 1800a714186SBjörn Töpel return result; 1810a714186SBjörn Töpel } 1820a714186SBjörn Töpel 1833b4f0b66SBjörn Töpel bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count) 1840a714186SBjörn Töpel { 1850a714186SBjörn Töpel u16 ntu = rx_ring->next_to_use; 1860a714186SBjörn Töpel union i40e_rx_desc *rx_desc; 1873b4f0b66SBjörn Töpel struct xdp_buff **bi, *xdp; 1883b4f0b66SBjörn Töpel dma_addr_t dma; 1890a714186SBjörn Töpel bool ok = true; 1900a714186SBjörn Töpel 1910a714186SBjörn Töpel rx_desc = I40E_RX_DESC(rx_ring, ntu); 192e1675f97SBjörn Töpel bi = i40e_rx_bi(rx_ring, ntu); 1930a714186SBjörn Töpel do { 1943b4f0b66SBjörn Töpel xdp = xsk_buff_alloc(rx_ring->xsk_umem); 1953b4f0b66SBjörn Töpel if (!xdp) { 1960a714186SBjörn Töpel ok = false; 1970a714186SBjörn Töpel goto no_buffers; 1980a714186SBjörn Töpel } 1993b4f0b66SBjörn Töpel *bi = xdp; 2003b4f0b66SBjörn Töpel dma = xsk_buff_xdp_get_dma(xdp); 2013b4f0b66SBjörn Töpel rx_desc->read.pkt_addr = cpu_to_le64(dma); 2023b4f0b66SBjörn Töpel rx_desc->read.hdr_addr = 0; 2030a714186SBjörn Töpel 2040a714186SBjörn Töpel rx_desc++; 2050a714186SBjörn Töpel bi++; 2060a714186SBjörn Töpel ntu++; 2070a714186SBjörn Töpel 2080a714186SBjörn Töpel if (unlikely(ntu == rx_ring->count)) { 2090a714186SBjörn Töpel rx_desc = I40E_RX_DESC(rx_ring, 0); 210e1675f97SBjörn Töpel bi = i40e_rx_bi(rx_ring, 0); 2110a714186SBjörn Töpel ntu = 0; 2120a714186SBjörn Töpel } 2130a714186SBjörn Töpel 2140a714186SBjörn Töpel count--; 2150a714186SBjörn Töpel } while (count); 2160a714186SBjörn Töpel 2170a714186SBjörn Töpel no_buffers: 2180a714186SBjörn Töpel if (rx_ring->next_to_use != ntu) 2190a714186SBjörn Töpel i40e_release_rx_desc(rx_ring, ntu); 2200a714186SBjörn Töpel 2210a714186SBjörn Töpel return ok; 2220a714186SBjörn Töpel } 2230a714186SBjörn Töpel 2240a714186SBjörn Töpel /** 225e92c0e02SJesper Dangaard Brouer * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer 2260a714186SBjörn Töpel * @rx_ring: Rx ring 2270a714186SBjörn Töpel * @xdp: xdp_buff 2280a714186SBjörn Töpel * 2290a714186SBjörn Töpel * This functions allocates a new skb from a zero-copy Rx buffer. 2300a714186SBjörn Töpel * 2310a714186SBjörn Töpel * Returns the skb, or NULL on failure. 2320a714186SBjörn Töpel **/ 2330a714186SBjörn Töpel static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring, 2340a714186SBjörn Töpel struct xdp_buff *xdp) 2350a714186SBjörn Töpel { 2360a714186SBjörn Töpel unsigned int metasize = xdp->data - xdp->data_meta; 2370a714186SBjörn Töpel unsigned int datasize = xdp->data_end - xdp->data; 2380a714186SBjörn Töpel struct sk_buff *skb; 2390a714186SBjörn Töpel 2400a714186SBjörn Töpel /* allocate a skb to store the frags */ 2410a714186SBjörn Töpel skb = __napi_alloc_skb(&rx_ring->q_vector->napi, 2420a714186SBjörn Töpel xdp->data_end - xdp->data_hard_start, 2430a714186SBjörn Töpel GFP_ATOMIC | __GFP_NOWARN); 2440a714186SBjörn Töpel if (unlikely(!skb)) 2450a714186SBjörn Töpel return NULL; 2460a714186SBjörn Töpel 2470a714186SBjörn Töpel skb_reserve(skb, xdp->data - xdp->data_hard_start); 2480a714186SBjörn Töpel memcpy(__skb_put(skb, datasize), xdp->data, datasize); 2490a714186SBjörn Töpel if (metasize) 2500a714186SBjörn Töpel skb_metadata_set(skb, metasize); 2510a714186SBjörn Töpel 2523b4f0b66SBjörn Töpel xsk_buff_free(xdp); 2530a714186SBjörn Töpel return skb; 2540a714186SBjörn Töpel } 2550a714186SBjörn Töpel 2560a714186SBjörn Töpel /** 2570a714186SBjörn Töpel * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring 2580a714186SBjörn Töpel * @rx_ring: Rx ring 2590a714186SBjörn Töpel * @budget: NAPI budget 2600a714186SBjörn Töpel * 2610a714186SBjörn Töpel * Returns amount of work completed 2620a714186SBjörn Töpel **/ 2630a714186SBjörn Töpel int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget) 2640a714186SBjörn Töpel { 2650a714186SBjörn Töpel unsigned int total_rx_bytes = 0, total_rx_packets = 0; 2660a714186SBjörn Töpel u16 cleaned_count = I40E_DESC_UNUSED(rx_ring); 2670a714186SBjörn Töpel unsigned int xdp_res, xdp_xmit = 0; 2680a714186SBjörn Töpel bool failure = false; 2690a714186SBjörn Töpel struct sk_buff *skb; 2700a714186SBjörn Töpel 2710a714186SBjörn Töpel while (likely(total_rx_packets < (unsigned int)budget)) { 2720a714186SBjörn Töpel union i40e_rx_desc *rx_desc; 2733b4f0b66SBjörn Töpel struct xdp_buff **bi; 2740a714186SBjörn Töpel unsigned int size; 2750a714186SBjörn Töpel u64 qword; 2760a714186SBjörn Töpel 2770a714186SBjörn Töpel if (cleaned_count >= I40E_RX_BUFFER_WRITE) { 2780a714186SBjörn Töpel failure = failure || 2793b4f0b66SBjörn Töpel !i40e_alloc_rx_buffers_zc(rx_ring, 2800a714186SBjörn Töpel cleaned_count); 2810a714186SBjörn Töpel cleaned_count = 0; 2820a714186SBjörn Töpel } 2830a714186SBjörn Töpel 2840a714186SBjörn Töpel rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean); 2850a714186SBjörn Töpel qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 2860a714186SBjörn Töpel 2870a714186SBjörn Töpel /* This memory barrier is needed to keep us from reading 2880a714186SBjörn Töpel * any other fields out of the rx_desc until we have 2890a714186SBjörn Töpel * verified the descriptor has been written back. 2900a714186SBjörn Töpel */ 2910a714186SBjörn Töpel dma_rmb(); 2920a714186SBjörn Töpel 293be1222b5SBjörn Töpel if (i40e_rx_is_programming_status(qword)) { 294be1222b5SBjörn Töpel i40e_clean_programming_status(rx_ring, 295be1222b5SBjörn Töpel rx_desc->raw.qword[0], 2960a714186SBjörn Töpel qword); 297be1222b5SBjörn Töpel bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean); 2983b4f0b66SBjörn Töpel xsk_buff_free(*bi); 2993b4f0b66SBjörn Töpel *bi = NULL; 3000a714186SBjörn Töpel cleaned_count++; 3013b4f0b66SBjörn Töpel i40e_inc_ntc(rx_ring); 3020a714186SBjörn Töpel continue; 3030a714186SBjörn Töpel } 3040a714186SBjörn Töpel 305be1222b5SBjörn Töpel bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean); 3060a714186SBjörn Töpel size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >> 3070a714186SBjörn Töpel I40E_RXD_QW1_LENGTH_PBUF_SHIFT; 3080a714186SBjörn Töpel if (!size) 3090a714186SBjörn Töpel break; 3100a714186SBjörn Töpel 3113b4f0b66SBjörn Töpel bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean); 3123b4f0b66SBjörn Töpel (*bi)->data_end = (*bi)->data + size; 3133b4f0b66SBjörn Töpel xsk_buff_dma_sync_for_cpu(*bi); 3140a714186SBjörn Töpel 3153b4f0b66SBjörn Töpel xdp_res = i40e_run_xdp_zc(rx_ring, *bi); 3160a714186SBjörn Töpel if (xdp_res) { 3173b4f0b66SBjörn Töpel if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) 3180a714186SBjörn Töpel xdp_xmit |= xdp_res; 3193b4f0b66SBjörn Töpel else 3203b4f0b66SBjörn Töpel xsk_buff_free(*bi); 3210a714186SBjörn Töpel 3223b4f0b66SBjörn Töpel *bi = NULL; 3230a714186SBjörn Töpel total_rx_bytes += size; 3240a714186SBjörn Töpel total_rx_packets++; 3250a714186SBjörn Töpel 3260a714186SBjörn Töpel cleaned_count++; 3270a714186SBjörn Töpel i40e_inc_ntc(rx_ring); 3280a714186SBjörn Töpel continue; 3290a714186SBjörn Töpel } 3300a714186SBjörn Töpel 3310a714186SBjörn Töpel /* XDP_PASS path */ 3320a714186SBjörn Töpel 3330a714186SBjörn Töpel /* NB! We are not checking for errors using 3340a714186SBjörn Töpel * i40e_test_staterr with 3350a714186SBjörn Töpel * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that 3360a714186SBjörn Töpel * SBP is *not* set in PRT_SBPVSI (default not set). 3370a714186SBjörn Töpel */ 3383b4f0b66SBjörn Töpel skb = i40e_construct_skb_zc(rx_ring, *bi); 3393b4f0b66SBjörn Töpel *bi = NULL; 3400a714186SBjörn Töpel if (!skb) { 3410a714186SBjörn Töpel rx_ring->rx_stats.alloc_buff_failed++; 3420a714186SBjörn Töpel break; 3430a714186SBjörn Töpel } 3440a714186SBjörn Töpel 3450a714186SBjörn Töpel cleaned_count++; 3460a714186SBjörn Töpel i40e_inc_ntc(rx_ring); 3470a714186SBjörn Töpel 3480a714186SBjörn Töpel if (eth_skb_pad(skb)) 3490a714186SBjörn Töpel continue; 3500a714186SBjörn Töpel 3510a714186SBjörn Töpel total_rx_bytes += skb->len; 3520a714186SBjörn Töpel total_rx_packets++; 3530a714186SBjörn Töpel 354800b8f63SMichał Mirosław i40e_process_skb_fields(rx_ring, rx_desc, skb); 3552a508c64SMichał Mirosław napi_gro_receive(&rx_ring->q_vector->napi, skb); 3560a714186SBjörn Töpel } 3570a714186SBjörn Töpel 3580a714186SBjörn Töpel i40e_finalize_xdp_rx(rx_ring, xdp_xmit); 3590a714186SBjörn Töpel i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets); 3603d0c5f1cSMagnus Karlsson 3613d0c5f1cSMagnus Karlsson if (xsk_umem_uses_need_wakeup(rx_ring->xsk_umem)) { 3623d0c5f1cSMagnus Karlsson if (failure || rx_ring->next_to_clean == rx_ring->next_to_use) 3633d0c5f1cSMagnus Karlsson xsk_set_rx_need_wakeup(rx_ring->xsk_umem); 3643d0c5f1cSMagnus Karlsson else 3653d0c5f1cSMagnus Karlsson xsk_clear_rx_need_wakeup(rx_ring->xsk_umem); 3663d0c5f1cSMagnus Karlsson 3673d0c5f1cSMagnus Karlsson return (int)total_rx_packets; 3683d0c5f1cSMagnus Karlsson } 3690a714186SBjörn Töpel return failure ? budget : (int)total_rx_packets; 3700a714186SBjörn Töpel } 3710a714186SBjörn Töpel 3721328dcddSMagnus Karlsson /** 3731328dcddSMagnus Karlsson * i40e_xmit_zc - Performs zero-copy Tx AF_XDP 3741328dcddSMagnus Karlsson * @xdp_ring: XDP Tx ring 3751328dcddSMagnus Karlsson * @budget: NAPI budget 3761328dcddSMagnus Karlsson * 3771328dcddSMagnus Karlsson * Returns true if the work is finished. 3781328dcddSMagnus Karlsson **/ 3791328dcddSMagnus Karlsson static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget) 3801328dcddSMagnus Karlsson { 3815574ff7bSMagnus Karlsson unsigned int sent_frames = 0, total_bytes = 0; 382cf484f9fSMagnus Karlsson struct i40e_tx_desc *tx_desc = NULL; 3831328dcddSMagnus Karlsson struct i40e_tx_buffer *tx_bi; 3844bce4e5cSMaxim Mikityanskiy struct xdp_desc desc; 3851328dcddSMagnus Karlsson dma_addr_t dma; 3861328dcddSMagnus Karlsson 3871328dcddSMagnus Karlsson while (budget-- > 0) { 3884bce4e5cSMaxim Mikityanskiy if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &desc)) 3891328dcddSMagnus Karlsson break; 3901328dcddSMagnus Karlsson 3913b4f0b66SBjörn Töpel dma = xsk_buff_raw_get_dma(xdp_ring->xsk_umem, desc.addr); 3923b4f0b66SBjörn Töpel xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_umem, dma, 3933b4f0b66SBjörn Töpel desc.len); 3941328dcddSMagnus Karlsson 3951328dcddSMagnus Karlsson tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use]; 3964bce4e5cSMaxim Mikityanskiy tx_bi->bytecount = desc.len; 3971328dcddSMagnus Karlsson 3981328dcddSMagnus Karlsson tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use); 3991328dcddSMagnus Karlsson tx_desc->buffer_addr = cpu_to_le64(dma); 4001328dcddSMagnus Karlsson tx_desc->cmd_type_offset_bsz = 4011328dcddSMagnus Karlsson build_ctob(I40E_TX_DESC_CMD_ICRC 4021328dcddSMagnus Karlsson | I40E_TX_DESC_CMD_EOP, 4034bce4e5cSMaxim Mikityanskiy 0, desc.len, 0); 4041328dcddSMagnus Karlsson 4055574ff7bSMagnus Karlsson sent_frames++; 4065574ff7bSMagnus Karlsson total_bytes += tx_bi->bytecount; 4075574ff7bSMagnus Karlsson 4081328dcddSMagnus Karlsson xdp_ring->next_to_use++; 4091328dcddSMagnus Karlsson if (xdp_ring->next_to_use == xdp_ring->count) 4101328dcddSMagnus Karlsson xdp_ring->next_to_use = 0; 4111328dcddSMagnus Karlsson } 4121328dcddSMagnus Karlsson 413cf484f9fSMagnus Karlsson if (tx_desc) { 4141328dcddSMagnus Karlsson /* Request an interrupt for the last frame and bump tail ptr. */ 4151328dcddSMagnus Karlsson tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS << 4161328dcddSMagnus Karlsson I40E_TXD_QW1_CMD_SHIFT); 4171328dcddSMagnus Karlsson i40e_xdp_ring_update_tail(xdp_ring); 4181328dcddSMagnus Karlsson 4191328dcddSMagnus Karlsson xsk_umem_consume_tx_done(xdp_ring->xsk_umem); 4205574ff7bSMagnus Karlsson i40e_update_tx_stats(xdp_ring, sent_frames, total_bytes); 4211328dcddSMagnus Karlsson } 4221328dcddSMagnus Karlsson 423*1fd972ebSMagnus Karlsson return !!budget; 4241328dcddSMagnus Karlsson } 4251328dcddSMagnus Karlsson 4261328dcddSMagnus Karlsson /** 4271328dcddSMagnus Karlsson * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry 4281328dcddSMagnus Karlsson * @tx_ring: XDP Tx ring 4291328dcddSMagnus Karlsson * @tx_bi: Tx buffer info to clean 4301328dcddSMagnus Karlsson **/ 4311328dcddSMagnus Karlsson static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring, 4321328dcddSMagnus Karlsson struct i40e_tx_buffer *tx_bi) 4331328dcddSMagnus Karlsson { 4341328dcddSMagnus Karlsson xdp_return_frame(tx_bi->xdpf); 4355574ff7bSMagnus Karlsson tx_ring->xdp_tx_active--; 4361328dcddSMagnus Karlsson dma_unmap_single(tx_ring->dev, 4371328dcddSMagnus Karlsson dma_unmap_addr(tx_bi, dma), 4381328dcddSMagnus Karlsson dma_unmap_len(tx_bi, len), DMA_TO_DEVICE); 4391328dcddSMagnus Karlsson dma_unmap_len_set(tx_bi, len, 0); 4401328dcddSMagnus Karlsson } 4411328dcddSMagnus Karlsson 4421328dcddSMagnus Karlsson /** 4431328dcddSMagnus Karlsson * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries 444*1fd972ebSMagnus Karlsson * @vsi: Current VSI 4451328dcddSMagnus Karlsson * @tx_ring: XDP Tx ring 4461328dcddSMagnus Karlsson * 4471328dcddSMagnus Karlsson * Returns true if cleanup/tranmission is done. 4481328dcddSMagnus Karlsson **/ 4495574ff7bSMagnus Karlsson bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring) 4501328dcddSMagnus Karlsson { 4511328dcddSMagnus Karlsson struct xdp_umem *umem = tx_ring->xsk_umem; 4525574ff7bSMagnus Karlsson u32 i, completed_frames, xsk_frames = 0; 4531328dcddSMagnus Karlsson u32 head_idx = i40e_get_head(tx_ring); 4541328dcddSMagnus Karlsson struct i40e_tx_buffer *tx_bi; 455*1fd972ebSMagnus Karlsson unsigned int ntc; 4561328dcddSMagnus Karlsson 4571328dcddSMagnus Karlsson if (head_idx < tx_ring->next_to_clean) 4581328dcddSMagnus Karlsson head_idx += tx_ring->count; 4595574ff7bSMagnus Karlsson completed_frames = head_idx - tx_ring->next_to_clean; 4601328dcddSMagnus Karlsson 4615574ff7bSMagnus Karlsson if (completed_frames == 0) 4621328dcddSMagnus Karlsson goto out_xmit; 4635574ff7bSMagnus Karlsson 4645574ff7bSMagnus Karlsson if (likely(!tx_ring->xdp_tx_active)) { 4655574ff7bSMagnus Karlsson xsk_frames = completed_frames; 4665574ff7bSMagnus Karlsson goto skip; 4671328dcddSMagnus Karlsson } 4681328dcddSMagnus Karlsson 4691328dcddSMagnus Karlsson ntc = tx_ring->next_to_clean; 4701328dcddSMagnus Karlsson 4711328dcddSMagnus Karlsson for (i = 0; i < completed_frames; i++) { 4721328dcddSMagnus Karlsson tx_bi = &tx_ring->tx_bi[ntc]; 4731328dcddSMagnus Karlsson 4745574ff7bSMagnus Karlsson if (tx_bi->xdpf) { 4751328dcddSMagnus Karlsson i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 4761328dcddSMagnus Karlsson tx_bi->xdpf = NULL; 4775574ff7bSMagnus Karlsson } else { 4785574ff7bSMagnus Karlsson xsk_frames++; 4795574ff7bSMagnus Karlsson } 4801328dcddSMagnus Karlsson 4811328dcddSMagnus Karlsson if (++ntc >= tx_ring->count) 4821328dcddSMagnus Karlsson ntc = 0; 4831328dcddSMagnus Karlsson } 4841328dcddSMagnus Karlsson 4855574ff7bSMagnus Karlsson skip: 4861328dcddSMagnus Karlsson tx_ring->next_to_clean += completed_frames; 4871328dcddSMagnus Karlsson if (unlikely(tx_ring->next_to_clean >= tx_ring->count)) 4881328dcddSMagnus Karlsson tx_ring->next_to_clean -= tx_ring->count; 4891328dcddSMagnus Karlsson 4901328dcddSMagnus Karlsson if (xsk_frames) 4911328dcddSMagnus Karlsson xsk_umem_complete_tx(umem, xsk_frames); 4921328dcddSMagnus Karlsson 4935574ff7bSMagnus Karlsson i40e_arm_wb(tx_ring, vsi, completed_frames); 4941328dcddSMagnus Karlsson 4951328dcddSMagnus Karlsson out_xmit: 49670563957SMagnus Karlsson if (xsk_umem_uses_need_wakeup(tx_ring->xsk_umem)) 4973d0c5f1cSMagnus Karlsson xsk_set_tx_need_wakeup(tx_ring->xsk_umem); 4983d0c5f1cSMagnus Karlsson 499*1fd972ebSMagnus Karlsson return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring)); 5001328dcddSMagnus Karlsson } 5011328dcddSMagnus Karlsson 5021328dcddSMagnus Karlsson /** 5039116e5e2SMagnus Karlsson * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup 5041328dcddSMagnus Karlsson * @dev: the netdevice 5051328dcddSMagnus Karlsson * @queue_id: queue id to wake up 5069116e5e2SMagnus Karlsson * @flags: ignored in our case since we have Rx and Tx in the same NAPI. 5071328dcddSMagnus Karlsson * 5081328dcddSMagnus Karlsson * Returns <0 for errors, 0 otherwise. 5091328dcddSMagnus Karlsson **/ 5109116e5e2SMagnus Karlsson int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags) 5111328dcddSMagnus Karlsson { 5121328dcddSMagnus Karlsson struct i40e_netdev_priv *np = netdev_priv(dev); 5131328dcddSMagnus Karlsson struct i40e_vsi *vsi = np->vsi; 514b3873a5bSMaxim Mikityanskiy struct i40e_pf *pf = vsi->back; 5151328dcddSMagnus Karlsson struct i40e_ring *ring; 5161328dcddSMagnus Karlsson 517b3873a5bSMaxim Mikityanskiy if (test_bit(__I40E_CONFIG_BUSY, pf->state)) 518c77e9f09SMaciej Fijalkowski return -EAGAIN; 519b3873a5bSMaxim Mikityanskiy 5201328dcddSMagnus Karlsson if (test_bit(__I40E_VSI_DOWN, vsi->state)) 5211328dcddSMagnus Karlsson return -ENETDOWN; 5221328dcddSMagnus Karlsson 5231328dcddSMagnus Karlsson if (!i40e_enabled_xdp_vsi(vsi)) 5241328dcddSMagnus Karlsson return -ENXIO; 5251328dcddSMagnus Karlsson 5261328dcddSMagnus Karlsson if (queue_id >= vsi->num_queue_pairs) 5271328dcddSMagnus Karlsson return -ENXIO; 5281328dcddSMagnus Karlsson 5291328dcddSMagnus Karlsson if (!vsi->xdp_rings[queue_id]->xsk_umem) 5301328dcddSMagnus Karlsson return -ENXIO; 5311328dcddSMagnus Karlsson 5321328dcddSMagnus Karlsson ring = vsi->xdp_rings[queue_id]; 5331328dcddSMagnus Karlsson 5341328dcddSMagnus Karlsson /* The idea here is that if NAPI is running, mark a miss, so 5351328dcddSMagnus Karlsson * it will run again. If not, trigger an interrupt and 5361328dcddSMagnus Karlsson * schedule the NAPI from interrupt context. If NAPI would be 5371328dcddSMagnus Karlsson * scheduled here, the interrupt affinity would not be 5381328dcddSMagnus Karlsson * honored. 5391328dcddSMagnus Karlsson */ 5401328dcddSMagnus Karlsson if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi)) 5411328dcddSMagnus Karlsson i40e_force_wb(vsi, ring->q_vector); 5421328dcddSMagnus Karlsson 5431328dcddSMagnus Karlsson return 0; 5441328dcddSMagnus Karlsson } 5459dbb1370SBjörn Töpel 546411dc16fSBjörn Töpel void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring) 547411dc16fSBjörn Töpel { 548411dc16fSBjörn Töpel u16 i; 549411dc16fSBjörn Töpel 550411dc16fSBjörn Töpel for (i = 0; i < rx_ring->count; i++) { 5513b4f0b66SBjörn Töpel struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, i); 552411dc16fSBjörn Töpel 5533b4f0b66SBjörn Töpel if (!rx_bi) 554411dc16fSBjörn Töpel continue; 555411dc16fSBjörn Töpel 5563b4f0b66SBjörn Töpel xsk_buff_free(rx_bi); 5573b4f0b66SBjörn Töpel rx_bi = NULL; 558411dc16fSBjörn Töpel } 559411dc16fSBjörn Töpel } 560411dc16fSBjörn Töpel 5619dbb1370SBjörn Töpel /** 5629dbb1370SBjörn Töpel * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown 563*1fd972ebSMagnus Karlsson * @tx_ring: XDP Tx ring 5649dbb1370SBjörn Töpel **/ 5659dbb1370SBjörn Töpel void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring) 5669dbb1370SBjörn Töpel { 5679dbb1370SBjörn Töpel u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use; 5689dbb1370SBjörn Töpel struct xdp_umem *umem = tx_ring->xsk_umem; 5699dbb1370SBjörn Töpel struct i40e_tx_buffer *tx_bi; 5709dbb1370SBjörn Töpel u32 xsk_frames = 0; 5719dbb1370SBjörn Töpel 5729dbb1370SBjörn Töpel while (ntc != ntu) { 5739dbb1370SBjörn Töpel tx_bi = &tx_ring->tx_bi[ntc]; 5749dbb1370SBjörn Töpel 5759dbb1370SBjörn Töpel if (tx_bi->xdpf) 5769dbb1370SBjörn Töpel i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 5779dbb1370SBjörn Töpel else 5789dbb1370SBjörn Töpel xsk_frames++; 5799dbb1370SBjörn Töpel 5809dbb1370SBjörn Töpel tx_bi->xdpf = NULL; 5819dbb1370SBjörn Töpel 5829dbb1370SBjörn Töpel ntc++; 5839dbb1370SBjörn Töpel if (ntc >= tx_ring->count) 5849dbb1370SBjörn Töpel ntc = 0; 5859dbb1370SBjörn Töpel } 5869dbb1370SBjörn Töpel 5879dbb1370SBjörn Töpel if (xsk_frames) 5889dbb1370SBjörn Töpel xsk_umem_complete_tx(umem, xsk_frames); 5899dbb1370SBjörn Töpel } 5903ab52af5SBjörn Töpel 5913ab52af5SBjörn Töpel /** 5923ab52af5SBjörn Töpel * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have AF_XDP UMEM attached 5933ab52af5SBjörn Töpel * @vsi: vsi 5943ab52af5SBjörn Töpel * 5953ab52af5SBjörn Töpel * Returns true if any of the Rx rings has an AF_XDP UMEM attached 5963ab52af5SBjörn Töpel **/ 5973ab52af5SBjörn Töpel bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi) 5983ab52af5SBjörn Töpel { 599f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 6003ab52af5SBjörn Töpel int i; 6013ab52af5SBjörn Töpel 6023ab52af5SBjörn Töpel for (i = 0; i < vsi->num_queue_pairs; i++) { 603f3fef2b6SJan Sokolowski if (xdp_get_umem_from_qid(netdev, i)) 6043ab52af5SBjörn Töpel return true; 6053ab52af5SBjörn Töpel } 6063ab52af5SBjörn Töpel 6073ab52af5SBjörn Töpel return false; 6083ab52af5SBjörn Töpel } 609