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> 53106c580SMagnus Karlsson #include <linux/stringify.h> 6a71506a4SMagnus Karlsson #include <net/xdp_sock_drv.h> 70a714186SBjörn Töpel #include <net/xdp.h> 80a714186SBjörn Töpel 90a714186SBjörn Töpel #include "i40e.h" 100a714186SBjörn Töpel #include "i40e_txrx_common.h" 110a714186SBjörn Töpel #include "i40e_xsk.h" 120a714186SBjörn Töpel 13be1222b5SBjörn Töpel int i40e_alloc_rx_bi_zc(struct i40e_ring *rx_ring) 14e1675f97SBjörn Töpel { 15be1222b5SBjörn Töpel unsigned long sz = sizeof(*rx_ring->rx_bi_zc) * rx_ring->count; 16be1222b5SBjörn Töpel 17be1222b5SBjörn Töpel rx_ring->rx_bi_zc = kzalloc(sz, GFP_KERNEL); 18be1222b5SBjörn Töpel return rx_ring->rx_bi_zc ? 0 : -ENOMEM; 19be1222b5SBjörn Töpel } 20be1222b5SBjörn Töpel 21be1222b5SBjörn Töpel void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring) 22be1222b5SBjörn Töpel { 23be1222b5SBjörn Töpel memset(rx_ring->rx_bi_zc, 0, 24be1222b5SBjörn Töpel sizeof(*rx_ring->rx_bi_zc) * rx_ring->count); 25be1222b5SBjörn Töpel } 26be1222b5SBjörn Töpel 273b4f0b66SBjörn Töpel static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx) 28be1222b5SBjörn Töpel { 29be1222b5SBjörn Töpel return &rx_ring->rx_bi_zc[idx]; 30e1675f97SBjörn Töpel } 31e1675f97SBjörn Töpel 320a714186SBjörn Töpel /** 331742b3d5SMagnus Karlsson * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a 341742b3d5SMagnus Karlsson * certain ring/qid 350a714186SBjörn Töpel * @vsi: Current VSI 361742b3d5SMagnus Karlsson * @pool: buffer pool 371742b3d5SMagnus Karlsson * @qid: Rx ring to associate buffer pool with 380a714186SBjörn Töpel * 390a714186SBjörn Töpel * Returns 0 on success, <0 on failure 400a714186SBjörn Töpel **/ 411742b3d5SMagnus Karlsson static int i40e_xsk_pool_enable(struct i40e_vsi *vsi, 421742b3d5SMagnus Karlsson struct xsk_buff_pool *pool, 430a714186SBjörn Töpel u16 qid) 440a714186SBjörn Töpel { 45f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 460a714186SBjörn Töpel bool if_running; 470a714186SBjörn Töpel int err; 480a714186SBjörn Töpel 490a714186SBjörn Töpel if (vsi->type != I40E_VSI_MAIN) 500a714186SBjörn Töpel return -EINVAL; 510a714186SBjörn Töpel 520a714186SBjörn Töpel if (qid >= vsi->num_queue_pairs) 530a714186SBjörn Töpel return -EINVAL; 540a714186SBjörn Töpel 55f3fef2b6SJan Sokolowski if (qid >= netdev->real_num_rx_queues || 56f3fef2b6SJan Sokolowski qid >= netdev->real_num_tx_queues) 570a714186SBjörn Töpel return -EINVAL; 580a714186SBjörn Töpel 59c4655761SMagnus Karlsson err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR); 600a714186SBjörn Töpel if (err) 610a714186SBjörn Töpel return err; 620a714186SBjörn Töpel 6344ddd4f1SBjörn Töpel set_bit(qid, vsi->af_xdp_zc_qps); 6444ddd4f1SBjörn Töpel 650a714186SBjörn Töpel if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 660a714186SBjörn Töpel 670a714186SBjörn Töpel if (if_running) { 680a714186SBjörn Töpel err = i40e_queue_pair_disable(vsi, qid); 690a714186SBjörn Töpel if (err) 700a714186SBjörn Töpel return err; 710a714186SBjörn Töpel 720a714186SBjörn Töpel err = i40e_queue_pair_enable(vsi, qid); 730a714186SBjörn Töpel if (err) 740a714186SBjörn Töpel return err; 7514ffeb52SMagnus Karlsson 7614ffeb52SMagnus Karlsson /* Kick start the NAPI context so that receiving will start */ 779116e5e2SMagnus Karlsson err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX); 7814ffeb52SMagnus Karlsson if (err) 7914ffeb52SMagnus Karlsson return err; 800a714186SBjörn Töpel } 810a714186SBjörn Töpel 820a714186SBjörn Töpel return 0; 830a714186SBjörn Töpel } 840a714186SBjörn Töpel 850a714186SBjörn Töpel /** 861742b3d5SMagnus Karlsson * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a 871742b3d5SMagnus Karlsson * certain ring/qid 880a714186SBjörn Töpel * @vsi: Current VSI 891742b3d5SMagnus Karlsson * @qid: Rx ring to associate buffer pool with 900a714186SBjörn Töpel * 910a714186SBjörn Töpel * Returns 0 on success, <0 on failure 920a714186SBjörn Töpel **/ 931742b3d5SMagnus Karlsson static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid) 940a714186SBjörn Töpel { 95f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 961742b3d5SMagnus Karlsson struct xsk_buff_pool *pool; 970a714186SBjörn Töpel bool if_running; 980a714186SBjörn Töpel int err; 990a714186SBjörn Töpel 100c4655761SMagnus Karlsson pool = xsk_get_pool_from_qid(netdev, qid); 1011742b3d5SMagnus Karlsson if (!pool) 1020a714186SBjörn Töpel return -EINVAL; 1030a714186SBjörn Töpel 1040a714186SBjörn Töpel if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 1050a714186SBjörn Töpel 1060a714186SBjörn Töpel if (if_running) { 1070a714186SBjörn Töpel err = i40e_queue_pair_disable(vsi, qid); 1080a714186SBjörn Töpel if (err) 1090a714186SBjörn Töpel return err; 1100a714186SBjörn Töpel } 1110a714186SBjörn Töpel 11244ddd4f1SBjörn Töpel clear_bit(qid, vsi->af_xdp_zc_qps); 113c4655761SMagnus Karlsson xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR); 1140a714186SBjörn Töpel 1150a714186SBjörn Töpel if (if_running) { 1160a714186SBjörn Töpel err = i40e_queue_pair_enable(vsi, qid); 1170a714186SBjörn Töpel if (err) 1180a714186SBjörn Töpel return err; 1190a714186SBjörn Töpel } 1200a714186SBjörn Töpel 1210a714186SBjörn Töpel return 0; 1220a714186SBjörn Töpel } 1230a714186SBjörn Töpel 1240a714186SBjörn Töpel /** 1251742b3d5SMagnus Karlsson * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from 1261742b3d5SMagnus Karlsson * a ring/qid 1270a714186SBjörn Töpel * @vsi: Current VSI 1281742b3d5SMagnus Karlsson * @pool: Buffer pool to enable/associate to a ring, or NULL to disable 1291742b3d5SMagnus Karlsson * @qid: Rx ring to (dis)associate buffer pool (from)to 1300a714186SBjörn Töpel * 1311742b3d5SMagnus Karlsson * This function enables or disables a buffer pool to a certain ring. 1320a714186SBjörn Töpel * 1330a714186SBjörn Töpel * Returns 0 on success, <0 on failure 1340a714186SBjörn Töpel **/ 1351742b3d5SMagnus Karlsson int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool, 1360a714186SBjörn Töpel u16 qid) 1370a714186SBjörn Töpel { 1381742b3d5SMagnus Karlsson return pool ? i40e_xsk_pool_enable(vsi, pool, qid) : 1391742b3d5SMagnus Karlsson i40e_xsk_pool_disable(vsi, qid); 1400a714186SBjörn Töpel } 1410a714186SBjörn Töpel 1420a714186SBjörn Töpel /** 1430a714186SBjörn Töpel * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff 1440a714186SBjörn Töpel * @rx_ring: Rx ring 1450a714186SBjörn Töpel * @xdp: xdp_buff used as input to the XDP program 1460a714186SBjörn Töpel * 1470a714186SBjörn Töpel * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR} 1480a714186SBjörn Töpel **/ 1490a714186SBjörn Töpel static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) 1500a714186SBjörn Töpel { 1510a714186SBjörn Töpel int err, result = I40E_XDP_PASS; 1520a714186SBjörn Töpel struct i40e_ring *xdp_ring; 1530a714186SBjörn Töpel struct bpf_prog *xdp_prog; 1540a714186SBjörn Töpel u32 act; 1550a714186SBjörn Töpel 1560a714186SBjörn Töpel rcu_read_lock(); 1570a714186SBjörn Töpel /* NB! xdp_prog will always be !NULL, due to the fact that 1580a714186SBjörn Töpel * this path is enabled by setting an XDP program. 1590a714186SBjörn Töpel */ 1600a714186SBjörn Töpel xdp_prog = READ_ONCE(rx_ring->xdp_prog); 1610a714186SBjörn Töpel act = bpf_prog_run_xdp(xdp_prog, xdp); 1622f86c806SKevin Laatz 1630a714186SBjörn Töpel switch (act) { 1640a714186SBjörn Töpel case XDP_PASS: 1650a714186SBjörn Töpel break; 1660a714186SBjörn Töpel case XDP_TX: 1670a714186SBjörn Töpel xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index]; 1680a714186SBjörn Töpel result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring); 1690a714186SBjörn Töpel break; 1700a714186SBjörn Töpel case XDP_REDIRECT: 1710a714186SBjörn Töpel err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 1720a714186SBjörn Töpel result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED; 1730a714186SBjörn Töpel break; 1740a714186SBjörn Töpel default: 1750a714186SBjörn Töpel bpf_warn_invalid_xdp_action(act); 1765463fce6SJeff Kirsher fallthrough; 1770a714186SBjörn Töpel case XDP_ABORTED: 1780a714186SBjörn Töpel trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 1795463fce6SJeff Kirsher fallthrough; /* handle aborts by dropping packet */ 1800a714186SBjörn Töpel case XDP_DROP: 1810a714186SBjörn Töpel result = I40E_XDP_CONSUMED; 1820a714186SBjörn Töpel break; 1830a714186SBjörn Töpel } 1840a714186SBjörn Töpel rcu_read_unlock(); 1850a714186SBjörn Töpel return result; 1860a714186SBjörn Töpel } 1870a714186SBjörn Töpel 1883b4f0b66SBjörn Töpel bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count) 1890a714186SBjörn Töpel { 1900a714186SBjörn Töpel u16 ntu = rx_ring->next_to_use; 1910a714186SBjörn Töpel union i40e_rx_desc *rx_desc; 1923b4f0b66SBjörn Töpel struct xdp_buff **bi, *xdp; 1933b4f0b66SBjörn Töpel dma_addr_t dma; 1940a714186SBjörn Töpel bool ok = true; 1950a714186SBjörn Töpel 1960a714186SBjörn Töpel rx_desc = I40E_RX_DESC(rx_ring, ntu); 197e1675f97SBjörn Töpel bi = i40e_rx_bi(rx_ring, ntu); 1980a714186SBjörn Töpel do { 199c4655761SMagnus Karlsson xdp = xsk_buff_alloc(rx_ring->xsk_pool); 2003b4f0b66SBjörn Töpel if (!xdp) { 2010a714186SBjörn Töpel ok = false; 2020a714186SBjörn Töpel goto no_buffers; 2030a714186SBjörn Töpel } 2043b4f0b66SBjörn Töpel *bi = xdp; 2053b4f0b66SBjörn Töpel dma = xsk_buff_xdp_get_dma(xdp); 2063b4f0b66SBjörn Töpel rx_desc->read.pkt_addr = cpu_to_le64(dma); 2073b4f0b66SBjörn Töpel rx_desc->read.hdr_addr = 0; 2080a714186SBjörn Töpel 2090a714186SBjörn Töpel rx_desc++; 2100a714186SBjörn Töpel bi++; 2110a714186SBjörn Töpel ntu++; 2120a714186SBjörn Töpel 2130a714186SBjörn Töpel if (unlikely(ntu == rx_ring->count)) { 2140a714186SBjörn Töpel rx_desc = I40E_RX_DESC(rx_ring, 0); 215e1675f97SBjörn Töpel bi = i40e_rx_bi(rx_ring, 0); 2160a714186SBjörn Töpel ntu = 0; 2170a714186SBjörn Töpel } 218f892a9afSBjörn Töpel } while (--count); 2190a714186SBjörn Töpel 2200a714186SBjörn Töpel no_buffers: 22164050b5bSBjörn Töpel if (rx_ring->next_to_use != ntu) { 22264050b5bSBjörn Töpel /* clear the status bits for the next_to_use descriptor */ 22364050b5bSBjörn Töpel rx_desc->wb.qword1.status_error_len = 0; 2240a714186SBjörn Töpel i40e_release_rx_desc(rx_ring, ntu); 22564050b5bSBjörn Töpel } 2260a714186SBjörn Töpel 2270a714186SBjörn Töpel return ok; 2280a714186SBjörn Töpel } 2290a714186SBjörn Töpel 2300a714186SBjörn Töpel /** 231e92c0e02SJesper Dangaard Brouer * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer 2320a714186SBjörn Töpel * @rx_ring: Rx ring 2330a714186SBjörn Töpel * @xdp: xdp_buff 2340a714186SBjörn Töpel * 2350a714186SBjörn Töpel * This functions allocates a new skb from a zero-copy Rx buffer. 2360a714186SBjörn Töpel * 2370a714186SBjörn Töpel * Returns the skb, or NULL on failure. 2380a714186SBjörn Töpel **/ 2390a714186SBjörn Töpel static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring, 2400a714186SBjörn Töpel struct xdp_buff *xdp) 2410a714186SBjörn Töpel { 2420a714186SBjörn Töpel unsigned int metasize = xdp->data - xdp->data_meta; 2430a714186SBjörn Töpel unsigned int datasize = xdp->data_end - xdp->data; 2440a714186SBjörn Töpel struct sk_buff *skb; 2450a714186SBjörn Töpel 2460a714186SBjörn Töpel /* allocate a skb to store the frags */ 2470a714186SBjörn Töpel skb = __napi_alloc_skb(&rx_ring->q_vector->napi, 2480a714186SBjörn Töpel xdp->data_end - xdp->data_hard_start, 2490a714186SBjörn Töpel GFP_ATOMIC | __GFP_NOWARN); 2500a714186SBjörn Töpel if (unlikely(!skb)) 251f020fa1aSCristian Dumitrescu goto out; 2520a714186SBjörn Töpel 2530a714186SBjörn Töpel skb_reserve(skb, xdp->data - xdp->data_hard_start); 2540a714186SBjörn Töpel memcpy(__skb_put(skb, datasize), xdp->data, datasize); 2550a714186SBjörn Töpel if (metasize) 2560a714186SBjörn Töpel skb_metadata_set(skb, metasize); 2570a714186SBjörn Töpel 258f020fa1aSCristian Dumitrescu out: 2593b4f0b66SBjörn Töpel xsk_buff_free(xdp); 2600a714186SBjörn Töpel return skb; 2610a714186SBjörn Töpel } 2620a714186SBjörn Töpel 263f020fa1aSCristian Dumitrescu static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring, 264f020fa1aSCristian Dumitrescu struct xdp_buff *xdp_buff, 265f020fa1aSCristian Dumitrescu union i40e_rx_desc *rx_desc, 266f020fa1aSCristian Dumitrescu unsigned int *rx_packets, 267f020fa1aSCristian Dumitrescu unsigned int *rx_bytes, 268f020fa1aSCristian Dumitrescu unsigned int size, 269f020fa1aSCristian Dumitrescu unsigned int xdp_res) 270f020fa1aSCristian Dumitrescu { 271f020fa1aSCristian Dumitrescu struct sk_buff *skb; 272f020fa1aSCristian Dumitrescu 273f020fa1aSCristian Dumitrescu *rx_packets = 1; 274f020fa1aSCristian Dumitrescu *rx_bytes = size; 275f020fa1aSCristian Dumitrescu 276f020fa1aSCristian Dumitrescu if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX) 277f020fa1aSCristian Dumitrescu return; 278f020fa1aSCristian Dumitrescu 279f020fa1aSCristian Dumitrescu if (xdp_res == I40E_XDP_CONSUMED) { 280f020fa1aSCristian Dumitrescu xsk_buff_free(xdp_buff); 281f020fa1aSCristian Dumitrescu return; 282f020fa1aSCristian Dumitrescu } 283f020fa1aSCristian Dumitrescu 284f020fa1aSCristian Dumitrescu if (xdp_res == I40E_XDP_PASS) { 285f020fa1aSCristian Dumitrescu /* NB! We are not checking for errors using 286f020fa1aSCristian Dumitrescu * i40e_test_staterr with 287f020fa1aSCristian Dumitrescu * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that 288f020fa1aSCristian Dumitrescu * SBP is *not* set in PRT_SBPVSI (default not set). 289f020fa1aSCristian Dumitrescu */ 290f020fa1aSCristian Dumitrescu skb = i40e_construct_skb_zc(rx_ring, xdp_buff); 291f020fa1aSCristian Dumitrescu if (!skb) { 292f020fa1aSCristian Dumitrescu rx_ring->rx_stats.alloc_buff_failed++; 293f020fa1aSCristian Dumitrescu *rx_packets = 0; 294f020fa1aSCristian Dumitrescu *rx_bytes = 0; 295f020fa1aSCristian Dumitrescu return; 296f020fa1aSCristian Dumitrescu } 297f020fa1aSCristian Dumitrescu 298f020fa1aSCristian Dumitrescu if (eth_skb_pad(skb)) { 299f020fa1aSCristian Dumitrescu *rx_packets = 0; 300f020fa1aSCristian Dumitrescu *rx_bytes = 0; 301f020fa1aSCristian Dumitrescu return; 302f020fa1aSCristian Dumitrescu } 303f020fa1aSCristian Dumitrescu 304f020fa1aSCristian Dumitrescu *rx_bytes = skb->len; 305f020fa1aSCristian Dumitrescu i40e_process_skb_fields(rx_ring, rx_desc, skb); 306f020fa1aSCristian Dumitrescu napi_gro_receive(&rx_ring->q_vector->napi, skb); 307f020fa1aSCristian Dumitrescu return; 308f020fa1aSCristian Dumitrescu } 309f020fa1aSCristian Dumitrescu 310f020fa1aSCristian Dumitrescu /* Should never get here, as all valid cases have been handled already. 311f020fa1aSCristian Dumitrescu */ 312f020fa1aSCristian Dumitrescu WARN_ON_ONCE(1); 313f020fa1aSCristian Dumitrescu } 314f020fa1aSCristian Dumitrescu 3150a714186SBjörn Töpel /** 3160a714186SBjörn Töpel * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring 3170a714186SBjörn Töpel * @rx_ring: Rx ring 3180a714186SBjörn Töpel * @budget: NAPI budget 3190a714186SBjörn Töpel * 3200a714186SBjörn Töpel * Returns amount of work completed 3210a714186SBjörn Töpel **/ 3220a714186SBjörn Töpel int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget) 3230a714186SBjörn Töpel { 3240a714186SBjörn Töpel unsigned int total_rx_bytes = 0, total_rx_packets = 0; 3250a714186SBjörn Töpel u16 cleaned_count = I40E_DESC_UNUSED(rx_ring); 326c8a8ca34SCristian Dumitrescu u16 next_to_clean = rx_ring->next_to_clean; 327c8a8ca34SCristian Dumitrescu u16 count_mask = rx_ring->count - 1; 3280a714186SBjörn Töpel unsigned int xdp_res, xdp_xmit = 0; 3291773482fSDan Carpenter bool failure = false; 3300a714186SBjörn Töpel 3310a714186SBjörn Töpel while (likely(total_rx_packets < (unsigned int)budget)) { 3320a714186SBjörn Töpel union i40e_rx_desc *rx_desc; 333f020fa1aSCristian Dumitrescu unsigned int rx_packets; 334f020fa1aSCristian Dumitrescu unsigned int rx_bytes; 335d4178c31SCristian Dumitrescu struct xdp_buff *bi; 3360a714186SBjörn Töpel unsigned int size; 3370a714186SBjörn Töpel u64 qword; 3380a714186SBjörn Töpel 339c8a8ca34SCristian Dumitrescu rx_desc = I40E_RX_DESC(rx_ring, next_to_clean); 3400a714186SBjörn Töpel qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 3410a714186SBjörn Töpel 3420a714186SBjörn Töpel /* This memory barrier is needed to keep us from reading 3430a714186SBjörn Töpel * any other fields out of the rx_desc until we have 3440a714186SBjörn Töpel * verified the descriptor has been written back. 3450a714186SBjörn Töpel */ 3460a714186SBjörn Töpel dma_rmb(); 3470a714186SBjörn Töpel 348be1222b5SBjörn Töpel if (i40e_rx_is_programming_status(qword)) { 349be1222b5SBjörn Töpel i40e_clean_programming_status(rx_ring, 350be1222b5SBjörn Töpel rx_desc->raw.qword[0], 3510a714186SBjörn Töpel qword); 352d4178c31SCristian Dumitrescu bi = *i40e_rx_bi(rx_ring, next_to_clean); 353d4178c31SCristian Dumitrescu xsk_buff_free(bi); 354c8a8ca34SCristian Dumitrescu next_to_clean = (next_to_clean + 1) & count_mask; 3550a714186SBjörn Töpel continue; 3560a714186SBjörn Töpel } 3570a714186SBjörn Töpel 3580a714186SBjörn Töpel size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >> 3590a714186SBjörn Töpel I40E_RXD_QW1_LENGTH_PBUF_SHIFT; 3600a714186SBjörn Töpel if (!size) 3610a714186SBjörn Töpel break; 3620a714186SBjörn Töpel 363d4178c31SCristian Dumitrescu bi = *i40e_rx_bi(rx_ring, next_to_clean); 364d4178c31SCristian Dumitrescu bi->data_end = bi->data + size; 365d4178c31SCristian Dumitrescu xsk_buff_dma_sync_for_cpu(bi, rx_ring->xsk_pool); 3660a714186SBjörn Töpel 367d4178c31SCristian Dumitrescu xdp_res = i40e_run_xdp_zc(rx_ring, bi); 368f020fa1aSCristian Dumitrescu i40e_handle_xdp_result_zc(rx_ring, bi, rx_desc, &rx_packets, 369f020fa1aSCristian Dumitrescu &rx_bytes, size, xdp_res); 370f020fa1aSCristian Dumitrescu total_rx_packets += rx_packets; 371f020fa1aSCristian Dumitrescu total_rx_bytes += rx_bytes; 372f020fa1aSCristian Dumitrescu xdp_xmit |= xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR); 373c8a8ca34SCristian Dumitrescu next_to_clean = (next_to_clean + 1) & count_mask; 3740a714186SBjörn Töpel } 3750a714186SBjörn Töpel 376c8a8ca34SCristian Dumitrescu rx_ring->next_to_clean = next_to_clean; 377f12738b6SCristian Dumitrescu cleaned_count = (next_to_clean - rx_ring->next_to_use - 1) & count_mask; 378c8a8ca34SCristian Dumitrescu 3798cbf7414SBjörn Töpel if (cleaned_count >= I40E_RX_BUFFER_WRITE) 3808cbf7414SBjörn Töpel failure = !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count); 3818cbf7414SBjörn Töpel 3820a714186SBjörn Töpel i40e_finalize_xdp_rx(rx_ring, xdp_xmit); 3830a714186SBjörn Töpel i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets); 3843d0c5f1cSMagnus Karlsson 385c4655761SMagnus Karlsson if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) { 386c8a8ca34SCristian Dumitrescu if (failure || next_to_clean == rx_ring->next_to_use) 387c4655761SMagnus Karlsson xsk_set_rx_need_wakeup(rx_ring->xsk_pool); 3883d0c5f1cSMagnus Karlsson else 389c4655761SMagnus Karlsson xsk_clear_rx_need_wakeup(rx_ring->xsk_pool); 3903d0c5f1cSMagnus Karlsson 3913d0c5f1cSMagnus Karlsson return (int)total_rx_packets; 3923d0c5f1cSMagnus Karlsson } 3930a714186SBjörn Töpel return failure ? budget : (int)total_rx_packets; 3940a714186SBjörn Töpel } 3950a714186SBjörn Töpel 3963106c580SMagnus Karlsson static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc, 3973106c580SMagnus Karlsson unsigned int *total_bytes) 3983106c580SMagnus Karlsson { 3993106c580SMagnus Karlsson struct i40e_tx_desc *tx_desc; 4003106c580SMagnus Karlsson dma_addr_t dma; 4013106c580SMagnus Karlsson 4023106c580SMagnus Karlsson dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr); 4033106c580SMagnus Karlsson xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len); 4043106c580SMagnus Karlsson 4053106c580SMagnus Karlsson tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++); 4063106c580SMagnus Karlsson tx_desc->buffer_addr = cpu_to_le64(dma); 4073106c580SMagnus Karlsson tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC | I40E_TX_DESC_CMD_EOP, 4083106c580SMagnus Karlsson 0, desc->len, 0); 4093106c580SMagnus Karlsson 4103106c580SMagnus Karlsson *total_bytes += desc->len; 4113106c580SMagnus Karlsson } 4123106c580SMagnus Karlsson 4133106c580SMagnus Karlsson static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc, 4143106c580SMagnus Karlsson unsigned int *total_bytes) 4153106c580SMagnus Karlsson { 4163106c580SMagnus Karlsson u16 ntu = xdp_ring->next_to_use; 4173106c580SMagnus Karlsson struct i40e_tx_desc *tx_desc; 4183106c580SMagnus Karlsson dma_addr_t dma; 4193106c580SMagnus Karlsson u32 i; 4203106c580SMagnus Karlsson 4213106c580SMagnus Karlsson loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) { 4223106c580SMagnus Karlsson dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr); 4233106c580SMagnus Karlsson xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len); 4243106c580SMagnus Karlsson 4253106c580SMagnus Karlsson tx_desc = I40E_TX_DESC(xdp_ring, ntu++); 4263106c580SMagnus Karlsson tx_desc->buffer_addr = cpu_to_le64(dma); 4273106c580SMagnus Karlsson tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC | 4283106c580SMagnus Karlsson I40E_TX_DESC_CMD_EOP, 4293106c580SMagnus Karlsson 0, desc[i].len, 0); 4303106c580SMagnus Karlsson 4313106c580SMagnus Karlsson *total_bytes += desc[i].len; 4323106c580SMagnus Karlsson } 4333106c580SMagnus Karlsson 4343106c580SMagnus Karlsson xdp_ring->next_to_use = ntu; 4353106c580SMagnus Karlsson } 4363106c580SMagnus Karlsson 4373106c580SMagnus Karlsson static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts, 4383106c580SMagnus Karlsson unsigned int *total_bytes) 4393106c580SMagnus Karlsson { 4403106c580SMagnus Karlsson u32 batched, leftover, i; 4413106c580SMagnus Karlsson 4423106c580SMagnus Karlsson batched = nb_pkts & ~(PKTS_PER_BATCH - 1); 4433106c580SMagnus Karlsson leftover = nb_pkts & (PKTS_PER_BATCH - 1); 4443106c580SMagnus Karlsson for (i = 0; i < batched; i += PKTS_PER_BATCH) 4453106c580SMagnus Karlsson i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes); 4463106c580SMagnus Karlsson for (i = batched; i < batched + leftover; i++) 4473106c580SMagnus Karlsson i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes); 4483106c580SMagnus Karlsson } 4493106c580SMagnus Karlsson 4503106c580SMagnus Karlsson static void i40e_set_rs_bit(struct i40e_ring *xdp_ring) 4513106c580SMagnus Karlsson { 4523106c580SMagnus Karlsson u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1; 4533106c580SMagnus Karlsson struct i40e_tx_desc *tx_desc; 4543106c580SMagnus Karlsson 4553106c580SMagnus Karlsson tx_desc = I40E_TX_DESC(xdp_ring, ntu); 456b32cddd2SNorbert Ciosek tx_desc->cmd_type_offset_bsz |= cpu_to_le64(I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT); 4573106c580SMagnus Karlsson } 4583106c580SMagnus Karlsson 4591328dcddSMagnus Karlsson /** 4601328dcddSMagnus Karlsson * i40e_xmit_zc - Performs zero-copy Tx AF_XDP 4611328dcddSMagnus Karlsson * @xdp_ring: XDP Tx ring 4621328dcddSMagnus Karlsson * @budget: NAPI budget 4631328dcddSMagnus Karlsson * 4641328dcddSMagnus Karlsson * Returns true if the work is finished. 4651328dcddSMagnus Karlsson **/ 4661328dcddSMagnus Karlsson static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget) 4671328dcddSMagnus Karlsson { 4683106c580SMagnus Karlsson struct xdp_desc *descs = xdp_ring->xsk_descs; 4693106c580SMagnus Karlsson u32 nb_pkts, nb_processed = 0; 4703106c580SMagnus Karlsson unsigned int total_bytes = 0; 4711328dcddSMagnus Karlsson 4723106c580SMagnus Karlsson nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, descs, budget); 4733106c580SMagnus Karlsson if (!nb_pkts) 474*528060efSMagnus Karlsson return true; 4751328dcddSMagnus Karlsson 4763106c580SMagnus Karlsson if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) { 4773106c580SMagnus Karlsson nb_processed = xdp_ring->count - xdp_ring->next_to_use; 4783106c580SMagnus Karlsson i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes); 4791328dcddSMagnus Karlsson xdp_ring->next_to_use = 0; 4801328dcddSMagnus Karlsson } 4811328dcddSMagnus Karlsson 4823106c580SMagnus Karlsson i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed, 4833106c580SMagnus Karlsson &total_bytes); 4843106c580SMagnus Karlsson 4851328dcddSMagnus Karlsson /* Request an interrupt for the last frame and bump tail ptr. */ 4863106c580SMagnus Karlsson i40e_set_rs_bit(xdp_ring); 4871328dcddSMagnus Karlsson i40e_xdp_ring_update_tail(xdp_ring); 4881328dcddSMagnus Karlsson 4893106c580SMagnus Karlsson i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes); 4901328dcddSMagnus Karlsson 491*528060efSMagnus Karlsson return nb_pkts < budget; 4921328dcddSMagnus Karlsson } 4931328dcddSMagnus Karlsson 4941328dcddSMagnus Karlsson /** 4951328dcddSMagnus Karlsson * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry 4961328dcddSMagnus Karlsson * @tx_ring: XDP Tx ring 4971328dcddSMagnus Karlsson * @tx_bi: Tx buffer info to clean 4981328dcddSMagnus Karlsson **/ 4991328dcddSMagnus Karlsson static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring, 5001328dcddSMagnus Karlsson struct i40e_tx_buffer *tx_bi) 5011328dcddSMagnus Karlsson { 5021328dcddSMagnus Karlsson xdp_return_frame(tx_bi->xdpf); 5035574ff7bSMagnus Karlsson tx_ring->xdp_tx_active--; 5041328dcddSMagnus Karlsson dma_unmap_single(tx_ring->dev, 5051328dcddSMagnus Karlsson dma_unmap_addr(tx_bi, dma), 5061328dcddSMagnus Karlsson dma_unmap_len(tx_bi, len), DMA_TO_DEVICE); 5071328dcddSMagnus Karlsson dma_unmap_len_set(tx_bi, len, 0); 5081328dcddSMagnus Karlsson } 5091328dcddSMagnus Karlsson 5101328dcddSMagnus Karlsson /** 5111328dcddSMagnus Karlsson * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries 5121fd972ebSMagnus Karlsson * @vsi: Current VSI 5131328dcddSMagnus Karlsson * @tx_ring: XDP Tx ring 5141328dcddSMagnus Karlsson * 5151328dcddSMagnus Karlsson * Returns true if cleanup/tranmission is done. 5161328dcddSMagnus Karlsson **/ 5175574ff7bSMagnus Karlsson bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring) 5181328dcddSMagnus Karlsson { 5191742b3d5SMagnus Karlsson struct xsk_buff_pool *bp = tx_ring->xsk_pool; 5205574ff7bSMagnus Karlsson u32 i, completed_frames, xsk_frames = 0; 5211328dcddSMagnus Karlsson u32 head_idx = i40e_get_head(tx_ring); 5221328dcddSMagnus Karlsson struct i40e_tx_buffer *tx_bi; 5231fd972ebSMagnus Karlsson unsigned int ntc; 5241328dcddSMagnus Karlsson 5251328dcddSMagnus Karlsson if (head_idx < tx_ring->next_to_clean) 5261328dcddSMagnus Karlsson head_idx += tx_ring->count; 5275574ff7bSMagnus Karlsson completed_frames = head_idx - tx_ring->next_to_clean; 5281328dcddSMagnus Karlsson 5295574ff7bSMagnus Karlsson if (completed_frames == 0) 5301328dcddSMagnus Karlsson goto out_xmit; 5315574ff7bSMagnus Karlsson 5325574ff7bSMagnus Karlsson if (likely(!tx_ring->xdp_tx_active)) { 5335574ff7bSMagnus Karlsson xsk_frames = completed_frames; 5345574ff7bSMagnus Karlsson goto skip; 5351328dcddSMagnus Karlsson } 5361328dcddSMagnus Karlsson 5371328dcddSMagnus Karlsson ntc = tx_ring->next_to_clean; 5381328dcddSMagnus Karlsson 5391328dcddSMagnus Karlsson for (i = 0; i < completed_frames; i++) { 5401328dcddSMagnus Karlsson tx_bi = &tx_ring->tx_bi[ntc]; 5411328dcddSMagnus Karlsson 5425574ff7bSMagnus Karlsson if (tx_bi->xdpf) { 5431328dcddSMagnus Karlsson i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 5441328dcddSMagnus Karlsson tx_bi->xdpf = NULL; 5455574ff7bSMagnus Karlsson } else { 5465574ff7bSMagnus Karlsson xsk_frames++; 5475574ff7bSMagnus Karlsson } 5481328dcddSMagnus Karlsson 5491328dcddSMagnus Karlsson if (++ntc >= tx_ring->count) 5501328dcddSMagnus Karlsson ntc = 0; 5511328dcddSMagnus Karlsson } 5521328dcddSMagnus Karlsson 5535574ff7bSMagnus Karlsson skip: 5541328dcddSMagnus Karlsson tx_ring->next_to_clean += completed_frames; 5551328dcddSMagnus Karlsson if (unlikely(tx_ring->next_to_clean >= tx_ring->count)) 5561328dcddSMagnus Karlsson tx_ring->next_to_clean -= tx_ring->count; 5571328dcddSMagnus Karlsson 5581328dcddSMagnus Karlsson if (xsk_frames) 559c4655761SMagnus Karlsson xsk_tx_completed(bp, xsk_frames); 5601328dcddSMagnus Karlsson 5615574ff7bSMagnus Karlsson i40e_arm_wb(tx_ring, vsi, completed_frames); 5621328dcddSMagnus Karlsson 5631328dcddSMagnus Karlsson out_xmit: 564c4655761SMagnus Karlsson if (xsk_uses_need_wakeup(tx_ring->xsk_pool)) 565c4655761SMagnus Karlsson xsk_set_tx_need_wakeup(tx_ring->xsk_pool); 5663d0c5f1cSMagnus Karlsson 5671fd972ebSMagnus Karlsson return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring)); 5681328dcddSMagnus Karlsson } 5691328dcddSMagnus Karlsson 5701328dcddSMagnus Karlsson /** 5719116e5e2SMagnus Karlsson * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup 5721328dcddSMagnus Karlsson * @dev: the netdevice 5731328dcddSMagnus Karlsson * @queue_id: queue id to wake up 5749116e5e2SMagnus Karlsson * @flags: ignored in our case since we have Rx and Tx in the same NAPI. 5751328dcddSMagnus Karlsson * 5761328dcddSMagnus Karlsson * Returns <0 for errors, 0 otherwise. 5771328dcddSMagnus Karlsson **/ 5789116e5e2SMagnus Karlsson int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags) 5791328dcddSMagnus Karlsson { 5801328dcddSMagnus Karlsson struct i40e_netdev_priv *np = netdev_priv(dev); 5811328dcddSMagnus Karlsson struct i40e_vsi *vsi = np->vsi; 582b3873a5bSMaxim Mikityanskiy struct i40e_pf *pf = vsi->back; 5831328dcddSMagnus Karlsson struct i40e_ring *ring; 5841328dcddSMagnus Karlsson 585b3873a5bSMaxim Mikityanskiy if (test_bit(__I40E_CONFIG_BUSY, pf->state)) 586c77e9f09SMaciej Fijalkowski return -EAGAIN; 587b3873a5bSMaxim Mikityanskiy 5881328dcddSMagnus Karlsson if (test_bit(__I40E_VSI_DOWN, vsi->state)) 5891328dcddSMagnus Karlsson return -ENETDOWN; 5901328dcddSMagnus Karlsson 5911328dcddSMagnus Karlsson if (!i40e_enabled_xdp_vsi(vsi)) 5921328dcddSMagnus Karlsson return -ENXIO; 5931328dcddSMagnus Karlsson 5941328dcddSMagnus Karlsson if (queue_id >= vsi->num_queue_pairs) 5951328dcddSMagnus Karlsson return -ENXIO; 5961328dcddSMagnus Karlsson 5971742b3d5SMagnus Karlsson if (!vsi->xdp_rings[queue_id]->xsk_pool) 5981328dcddSMagnus Karlsson return -ENXIO; 5991328dcddSMagnus Karlsson 6001328dcddSMagnus Karlsson ring = vsi->xdp_rings[queue_id]; 6011328dcddSMagnus Karlsson 6021328dcddSMagnus Karlsson /* The idea here is that if NAPI is running, mark a miss, so 6031328dcddSMagnus Karlsson * it will run again. If not, trigger an interrupt and 6041328dcddSMagnus Karlsson * schedule the NAPI from interrupt context. If NAPI would be 6051328dcddSMagnus Karlsson * scheduled here, the interrupt affinity would not be 6061328dcddSMagnus Karlsson * honored. 6071328dcddSMagnus Karlsson */ 6081328dcddSMagnus Karlsson if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi)) 6091328dcddSMagnus Karlsson i40e_force_wb(vsi, ring->q_vector); 6101328dcddSMagnus Karlsson 6111328dcddSMagnus Karlsson return 0; 6121328dcddSMagnus Karlsson } 6139dbb1370SBjörn Töpel 614411dc16fSBjörn Töpel void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring) 615411dc16fSBjörn Töpel { 616d4178c31SCristian Dumitrescu u16 count_mask = rx_ring->count - 1; 617d4178c31SCristian Dumitrescu u16 ntc = rx_ring->next_to_clean; 618d4178c31SCristian Dumitrescu u16 ntu = rx_ring->next_to_use; 619411dc16fSBjörn Töpel 620d4178c31SCristian Dumitrescu for ( ; ntc != ntu; ntc = (ntc + 1) & count_mask) { 621d4178c31SCristian Dumitrescu struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc); 622411dc16fSBjörn Töpel 6233b4f0b66SBjörn Töpel xsk_buff_free(rx_bi); 624411dc16fSBjörn Töpel } 625411dc16fSBjörn Töpel } 626411dc16fSBjörn Töpel 6279dbb1370SBjörn Töpel /** 6289dbb1370SBjörn Töpel * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown 6291fd972ebSMagnus Karlsson * @tx_ring: XDP Tx ring 6309dbb1370SBjörn Töpel **/ 6319dbb1370SBjörn Töpel void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring) 6329dbb1370SBjörn Töpel { 6339dbb1370SBjörn Töpel u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use; 6341742b3d5SMagnus Karlsson struct xsk_buff_pool *bp = tx_ring->xsk_pool; 6359dbb1370SBjörn Töpel struct i40e_tx_buffer *tx_bi; 6369dbb1370SBjörn Töpel u32 xsk_frames = 0; 6379dbb1370SBjörn Töpel 6389dbb1370SBjörn Töpel while (ntc != ntu) { 6399dbb1370SBjörn Töpel tx_bi = &tx_ring->tx_bi[ntc]; 6409dbb1370SBjörn Töpel 6419dbb1370SBjörn Töpel if (tx_bi->xdpf) 6429dbb1370SBjörn Töpel i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 6439dbb1370SBjörn Töpel else 6449dbb1370SBjörn Töpel xsk_frames++; 6459dbb1370SBjörn Töpel 6469dbb1370SBjörn Töpel tx_bi->xdpf = NULL; 6479dbb1370SBjörn Töpel 6489dbb1370SBjörn Töpel ntc++; 6499dbb1370SBjörn Töpel if (ntc >= tx_ring->count) 6509dbb1370SBjörn Töpel ntc = 0; 6519dbb1370SBjörn Töpel } 6529dbb1370SBjörn Töpel 6539dbb1370SBjörn Töpel if (xsk_frames) 654c4655761SMagnus Karlsson xsk_tx_completed(bp, xsk_frames); 6559dbb1370SBjörn Töpel } 6563ab52af5SBjörn Töpel 6573ab52af5SBjörn Töpel /** 6581742b3d5SMagnus Karlsson * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP 6591742b3d5SMagnus Karlsson * buffer pool attached 6603ab52af5SBjörn Töpel * @vsi: vsi 6613ab52af5SBjörn Töpel * 6621742b3d5SMagnus Karlsson * Returns true if any of the Rx rings has an AF_XDP buffer pool attached 6633ab52af5SBjörn Töpel **/ 6643ab52af5SBjörn Töpel bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi) 6653ab52af5SBjörn Töpel { 666f3fef2b6SJan Sokolowski struct net_device *netdev = vsi->netdev; 6673ab52af5SBjörn Töpel int i; 6683ab52af5SBjörn Töpel 6693ab52af5SBjörn Töpel for (i = 0; i < vsi->num_queue_pairs; i++) { 670c4655761SMagnus Karlsson if (xsk_get_pool_from_qid(netdev, i)) 6713ab52af5SBjörn Töpel return true; 6723ab52af5SBjörn Töpel } 6733ab52af5SBjörn Töpel 6743ab52af5SBjörn Töpel return false; 6753ab52af5SBjörn Töpel } 676