linux/drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c
<<
>>
Prefs
   1/* Broadcom NetXtreme-C/E network driver.
   2 *
   3 * Copyright (c) 2016-2017 Broadcom Limited
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation.
   8 */
   9#undef CONFIG_BPF_SYSCALL
  10
  11#include <linux/kernel.h>
  12#include <linux/errno.h>
  13#include <linux/pci.h>
  14#include <linux/netdevice.h>
  15#include <linux/etherdevice.h>
  16#include <linux/if_vlan.h>
  17#include <linux/bpf.h>
  18#include <linux/bpf_trace.h>
  19#include <linux/filter.h>
  20#include "bnxt_hsi.h"
  21#include "bnxt.h"
  22#include "bnxt_xdp.h"
  23
  24void bnxt_xmit_xdp(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
  25                   dma_addr_t mapping, u32 len, u16 rx_prod)
  26{
  27        struct bnxt_sw_tx_bd *tx_buf;
  28        struct tx_bd *txbd;
  29        u32 flags;
  30        u16 prod;
  31
  32        prod = txr->tx_prod;
  33        tx_buf = &txr->tx_buf_ring[prod];
  34        tx_buf->rx_prod = rx_prod;
  35
  36        txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  37        flags = (len << TX_BD_LEN_SHIFT) | (1 << TX_BD_FLAGS_BD_CNT_SHIFT) |
  38                TX_BD_FLAGS_PACKET_END | bnxt_lhint_arr[len >> 9];
  39        txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  40        txbd->tx_bd_opaque = prod;
  41        txbd->tx_bd_haddr = cpu_to_le64(mapping);
  42
  43        prod = NEXT_TX(prod);
  44        txr->tx_prod = prod;
  45}
  46
  47void bnxt_tx_int_xdp(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
  48{
  49        struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  50        struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  51        struct bnxt_sw_tx_bd *tx_buf;
  52        u16 tx_cons = txr->tx_cons;
  53        u16 last_tx_cons = tx_cons;
  54        u16 rx_prod;
  55        int i;
  56
  57        for (i = 0; i < nr_pkts; i++) {
  58                last_tx_cons = tx_cons;
  59                tx_cons = NEXT_TX(tx_cons);
  60        }
  61        txr->tx_cons = tx_cons;
  62        if (bnxt_tx_avail(bp, txr) == bp->tx_ring_size) {
  63                rx_prod = rxr->rx_prod;
  64        } else {
  65                tx_buf = &txr->tx_buf_ring[last_tx_cons];
  66                rx_prod = tx_buf->rx_prod;
  67        }
  68        bnxt_db_write(bp, &rxr->rx_db, rx_prod);
  69}
  70
  71/* returns the following:
  72 * true    - packet consumed by XDP and new buffer is allocated.
  73 * false   - packet should be passed to the stack.
  74 */
  75bool bnxt_rx_xdp(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, u16 cons,
  76                 struct page *page, u8 **data_ptr, unsigned int *len, u8 *event)
  77{
  78        struct bpf_prog *xdp_prog = READ_ONCE(rxr->xdp_prog);
  79        struct bnxt_tx_ring_info *txr;
  80        struct bnxt_sw_rx_bd *rx_buf;
  81        struct pci_dev *pdev;
  82        struct xdp_buff xdp;
  83        dma_addr_t mapping;
  84        void *orig_data;
  85        u32 tx_avail;
  86        u32 offset;
  87        u32 act;
  88
  89        if (!xdp_prog)
  90                return false;
  91
  92        pdev = bp->pdev;
  93        txr = rxr->bnapi->tx_ring;
  94        rx_buf = &rxr->rx_buf_ring[cons];
  95        offset = bp->rx_offset;
  96
  97        xdp.data_hard_start = *data_ptr - offset;
  98        xdp.data = *data_ptr;
  99        xdp.data_end = *data_ptr + *len;
 100        xdp.rxq = &rxr->xdp_rxq;
 101        orig_data = xdp.data;
 102        mapping = rx_buf->mapping - bp->rx_dma_offset;
 103
 104        dma_sync_single_for_cpu(&pdev->dev, mapping + offset, *len, bp->rx_dir);
 105
 106        rcu_read_lock();
 107        act = bpf_prog_run_xdp(xdp_prog, &xdp);
 108        rcu_read_unlock();
 109
 110        tx_avail = bnxt_tx_avail(bp, txr);
 111        /* If the tx ring is not full, we must not update the rx producer yet
 112         * because we may still be transmitting on some BDs.
 113         */
 114        if (tx_avail != bp->tx_ring_size)
 115                *event &= ~BNXT_RX_EVENT;
 116
 117        *len = xdp.data_end - xdp.data;
 118        if (orig_data != xdp.data) {
 119                offset = xdp.data - xdp.data_hard_start;
 120                *data_ptr = xdp.data_hard_start + offset;
 121        }
 122        switch (act) {
 123        case XDP_PASS:
 124                return false;
 125
 126        case XDP_TX:
 127                if (tx_avail < 1) {
 128                        trace_xdp_exception(bp->dev, xdp_prog, act);
 129                        bnxt_reuse_rx_data(rxr, cons, page);
 130                        return true;
 131                }
 132
 133                *event = BNXT_TX_EVENT;
 134                dma_sync_single_for_device(&pdev->dev, mapping + offset, *len,
 135                                           bp->rx_dir);
 136                bnxt_xmit_xdp(bp, txr, mapping + offset, *len,
 137                              NEXT_RX(rxr->rx_prod));
 138                bnxt_reuse_rx_data(rxr, cons, page);
 139                return true;
 140        default:
 141                bpf_warn_invalid_xdp_action(act);
 142                /* Fall thru */
 143        case XDP_ABORTED:
 144                trace_xdp_exception(bp->dev, xdp_prog, act);
 145                /* Fall thru */
 146        case XDP_DROP:
 147                bnxt_reuse_rx_data(rxr, cons, page);
 148                break;
 149        }
 150        return true;
 151}
 152
 153/* Under rtnl_lock */
 154static int bnxt_xdp_set(struct bnxt *bp, struct bpf_prog *prog)
 155{
 156        struct net_device *dev = bp->dev;
 157        int tx_xdp = 0, rc, tc;
 158        struct bpf_prog *old;
 159
 160        if (prog && bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
 161                netdev_warn(dev, "MTU %d larger than largest XDP supported MTU %d.\n",
 162                            bp->dev->mtu, BNXT_MAX_PAGE_MODE_MTU);
 163                return -EOPNOTSUPP;
 164        }
 165        if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) {
 166                netdev_warn(dev, "ethtool rx/tx channels must be combined to support XDP.\n");
 167                return -EOPNOTSUPP;
 168        }
 169        if (prog)
 170                tx_xdp = bp->rx_nr_rings;
 171
 172        tc = netdev_get_num_tc(dev);
 173        if (!tc)
 174                tc = 1;
 175        rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
 176                              true, tc, tx_xdp);
 177        if (rc) {
 178                netdev_warn(dev, "Unable to reserve enough TX rings to support XDP.\n");
 179                return rc;
 180        }
 181        if (netif_running(dev))
 182                bnxt_close_nic(bp, true, false);
 183
 184        old = xchg(&bp->xdp_prog, prog);
 185        if (old)
 186                bpf_prog_put(old);
 187
 188        if (prog) {
 189                bnxt_set_rx_skb_mode(bp, true);
 190        } else {
 191                int rx, tx;
 192
 193                bnxt_set_rx_skb_mode(bp, false);
 194                bnxt_get_max_rings(bp, &rx, &tx, true);
 195                if (rx > 1) {
 196                        bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
 197                        bp->dev->hw_features |= NETIF_F_LRO;
 198                }
 199        }
 200        bp->tx_nr_rings_xdp = tx_xdp;
 201        bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc + tx_xdp;
 202        bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
 203        bnxt_set_tpa_flags(bp);
 204        bnxt_set_ring_params(bp);
 205
 206        if (netif_running(dev))
 207                return bnxt_open_nic(bp, true, false);
 208
 209        return 0;
 210}
 211
 212int bnxt_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 213{
 214        struct bnxt *bp = netdev_priv(dev);
 215        int rc;
 216
 217        switch (xdp->command) {
 218        case XDP_SETUP_PROG:
 219                rc = bnxt_xdp_set(bp, xdp->prog);
 220                break;
 221        case XDP_QUERY_PROG:
 222                xdp->prog_id = bp->xdp_prog ? bp->xdp_prog->aux->id : 0;
 223                rc = 0;
 224                break;
 225        default:
 226                rc = -EINVAL;
 227                break;
 228        }
 229        return rc;
 230}
 231