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