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_db, 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_set_data_meta_invalid(&xdp); 98 xdp.data_end = *data_ptr + *len; 99 xdp.rxq = &rxr->xdp_rxq; 100 orig_data = xdp.data; 101 mapping = rx_buf->mapping - bp->rx_dma_offset; 102 103 dma_sync_single_for_cpu(&pdev->dev, mapping + offset, *len, bp->rx_dir); 104 105 rcu_read_lock(); 106 act = bpf_prog_run_xdp(xdp_prog, &xdp); 107 rcu_read_unlock(); 108 109 tx_avail = bnxt_tx_avail(bp, txr); 110 /* If the tx ring is not full, we must not update the rx producer yet 111 * because we may still be transmitting on some BDs. 112 */ 113 if (tx_avail != bp->tx_ring_size) 114 *event &= ~BNXT_RX_EVENT; 115 116 *len = xdp.data_end - xdp.data; 117 if (orig_data != xdp.data) { 118 offset = xdp.data - xdp.data_hard_start; 119 *data_ptr = xdp.data_hard_start + offset; 120 } 121 switch (act) { 122 case XDP_PASS: 123 return false; 124 125 case XDP_TX: 126 if (tx_avail < 1) { 127 trace_xdp_exception(bp->dev, xdp_prog, act); 128 bnxt_reuse_rx_data(rxr, cons, page); 129 return true; 130 } 131 132 *event = BNXT_TX_EVENT; 133 dma_sync_single_for_device(&pdev->dev, mapping + offset, *len, 134 bp->rx_dir); 135 bnxt_xmit_xdp(bp, txr, mapping + offset, *len, 136 NEXT_RX(rxr->rx_prod)); 137 bnxt_reuse_rx_data(rxr, cons, page); 138 return true; 139 default: 140 bpf_warn_invalid_xdp_action(act); 141 /* Fall thru */ 142 case XDP_ABORTED: 143 trace_xdp_exception(bp->dev, xdp_prog, act); 144 /* Fall thru */ 145 case XDP_DROP: 146 bnxt_reuse_rx_data(rxr, cons, page); 147 break; 148 } 149 return true; 150 } 151 152 /* Under rtnl_lock */ 153 static int bnxt_xdp_set(struct bnxt *bp, struct bpf_prog *prog) 154 { 155 struct net_device *dev = bp->dev; 156 int tx_xdp = 0, rc, tc; 157 struct bpf_prog *old; 158 159 if (prog && bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) { 160 netdev_warn(dev, "MTU %d larger than largest XDP supported MTU %d.\n", 161 bp->dev->mtu, BNXT_MAX_PAGE_MODE_MTU); 162 return -EOPNOTSUPP; 163 } 164 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) { 165 netdev_warn(dev, "ethtool rx/tx channels must be combined to support XDP.\n"); 166 return -EOPNOTSUPP; 167 } 168 if (prog) 169 tx_xdp = bp->rx_nr_rings; 170 171 tc = netdev_get_num_tc(dev); 172 if (!tc) 173 tc = 1; 174 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings, 175 true, tc, tx_xdp); 176 if (rc) { 177 netdev_warn(dev, "Unable to reserve enough TX rings to support XDP.\n"); 178 return rc; 179 } 180 if (netif_running(dev)) 181 bnxt_close_nic(bp, true, false); 182 183 old = xchg(&bp->xdp_prog, prog); 184 if (old) 185 bpf_prog_put(old); 186 187 if (prog) { 188 bnxt_set_rx_skb_mode(bp, true); 189 } else { 190 int rx, tx; 191 192 bnxt_set_rx_skb_mode(bp, false); 193 bnxt_get_max_rings(bp, &rx, &tx, true); 194 if (rx > 1) { 195 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS; 196 bp->dev->hw_features |= NETIF_F_LRO; 197 } 198 } 199 bp->tx_nr_rings_xdp = tx_xdp; 200 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc + tx_xdp; 201 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings); 202 bnxt_set_tpa_flags(bp); 203 bnxt_set_ring_params(bp); 204 205 if (netif_running(dev)) 206 return bnxt_open_nic(bp, true, false); 207 208 return 0; 209 } 210 211 int bnxt_xdp(struct net_device *dev, struct netdev_bpf *xdp) 212 { 213 struct bnxt *bp = netdev_priv(dev); 214 int rc; 215 216 switch (xdp->command) { 217 case XDP_SETUP_PROG: 218 rc = bnxt_xdp_set(bp, xdp->prog); 219 break; 220 case XDP_QUERY_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