1
2
3
4
5
6
7
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 <net/page_pool.h>
19#include "bnxt_hsi.h"
20#include "bnxt.h"
21#include "bnxt_xdp.h"
22
23struct bnxt_sw_tx_bd *bnxt_xmit_bd(struct bnxt *bp,
24 struct bnxt_tx_ring_info *txr,
25 dma_addr_t mapping, u32 len)
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
35 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
36 flags = (len << TX_BD_LEN_SHIFT) | (1 << TX_BD_FLAGS_BD_CNT_SHIFT) |
37 TX_BD_FLAGS_PACKET_END | bnxt_lhint_arr[len >> 9];
38 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
39 txbd->tx_bd_opaque = prod;
40 txbd->tx_bd_haddr = cpu_to_le64(mapping);
41
42 prod = NEXT_TX(prod);
43 txr->tx_prod = prod;
44 return tx_buf;
45}
46
47static void __bnxt_xmit_xdp(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
48 dma_addr_t mapping, u32 len, u16 rx_prod)
49{
50 struct bnxt_sw_tx_bd *tx_buf;
51
52 tx_buf = bnxt_xmit_bd(bp, txr, mapping, len);
53 tx_buf->rx_prod = rx_prod;
54 tx_buf->action = XDP_TX;
55}
56
57static void __bnxt_xmit_xdp_redirect(struct bnxt *bp,
58 struct bnxt_tx_ring_info *txr,
59 dma_addr_t mapping, u32 len,
60 struct xdp_frame *xdpf)
61{
62 struct bnxt_sw_tx_bd *tx_buf;
63
64 tx_buf = bnxt_xmit_bd(bp, txr, mapping, len);
65 tx_buf->action = XDP_REDIRECT;
66 tx_buf->xdpf = xdpf;
67 dma_unmap_addr_set(tx_buf, mapping, mapping);
68 dma_unmap_len_set(tx_buf, len, 0);
69}
70
71void bnxt_tx_int_xdp(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
72{
73 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
74 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
75 bool rx_doorbell_needed = false;
76 struct bnxt_sw_tx_bd *tx_buf;
77 u16 tx_cons = txr->tx_cons;
78 u16 last_tx_cons = tx_cons;
79 int i;
80
81 for (i = 0; i < nr_pkts; i++) {
82 tx_buf = &txr->tx_buf_ring[tx_cons];
83
84 if (tx_buf->action == XDP_REDIRECT) {
85 struct pci_dev *pdev = bp->pdev;
86
87 dma_unmap_single(&pdev->dev,
88 dma_unmap_addr(tx_buf, mapping),
89 dma_unmap_len(tx_buf, len),
90 PCI_DMA_TODEVICE);
91 xdp_return_frame(tx_buf->xdpf);
92 tx_buf->action = 0;
93 tx_buf->xdpf = NULL;
94 } else if (tx_buf->action == XDP_TX) {
95 rx_doorbell_needed = true;
96 last_tx_cons = tx_cons;
97 }
98 tx_cons = NEXT_TX(tx_cons);
99 }
100 txr->tx_cons = tx_cons;
101 if (rx_doorbell_needed) {
102 tx_buf = &txr->tx_buf_ring[last_tx_cons];
103 bnxt_db_write(bp, &rxr->rx_db, tx_buf->rx_prod);
104 }
105}
106
107
108
109
110
111bool bnxt_rx_xdp(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, u16 cons,
112 struct page *page, u8 **data_ptr, unsigned int *len, u8 *event)
113{
114 struct bpf_prog *xdp_prog = READ_ONCE(rxr->xdp_prog);
115 struct bnxt_tx_ring_info *txr;
116 struct bnxt_sw_rx_bd *rx_buf;
117 struct pci_dev *pdev;
118 struct xdp_buff xdp;
119 dma_addr_t mapping;
120 void *orig_data;
121 u32 tx_avail;
122 u32 offset;
123 u32 act;
124
125 if (!xdp_prog)
126 return false;
127
128 pdev = bp->pdev;
129 rx_buf = &rxr->rx_buf_ring[cons];
130 offset = bp->rx_offset;
131
132 mapping = rx_buf->mapping - bp->rx_dma_offset;
133 dma_sync_single_for_cpu(&pdev->dev, mapping + offset, *len, bp->rx_dir);
134
135 txr = rxr->bnapi->tx_ring;
136 xdp.data_hard_start = *data_ptr - offset;
137 xdp.data = *data_ptr;
138 xdp_set_data_meta_invalid(&xdp);
139 xdp.data_end = *data_ptr + *len;
140 xdp.rxq = &rxr->xdp_rxq;
141 orig_data = xdp.data;
142
143 rcu_read_lock();
144 act = bpf_prog_run_xdp(xdp_prog, &xdp);
145 rcu_read_unlock();
146
147 tx_avail = bnxt_tx_avail(bp, txr);
148
149
150
151 if (tx_avail != bp->tx_ring_size)
152 *event &= ~BNXT_RX_EVENT;
153
154 *len = xdp.data_end - xdp.data;
155 if (orig_data != xdp.data) {
156 offset = xdp.data - xdp.data_hard_start;
157 *data_ptr = xdp.data_hard_start + offset;
158 }
159 switch (act) {
160 case XDP_PASS:
161 return false;
162
163 case XDP_TX:
164 if (tx_avail < 1) {
165 trace_xdp_exception(bp->dev, xdp_prog, act);
166 bnxt_reuse_rx_data(rxr, cons, page);
167 return true;
168 }
169
170 *event = BNXT_TX_EVENT;
171 dma_sync_single_for_device(&pdev->dev, mapping + offset, *len,
172 bp->rx_dir);
173 __bnxt_xmit_xdp(bp, txr, mapping + offset, *len,
174 NEXT_RX(rxr->rx_prod));
175 bnxt_reuse_rx_data(rxr, cons, page);
176 return true;
177 case XDP_REDIRECT:
178
179
180
181
182 dma_unmap_page_attrs(&pdev->dev, mapping,
183 PAGE_SIZE, bp->rx_dir,
184 DMA_ATTR_WEAK_ORDERING);
185
186
187 if (bnxt_alloc_rx_data(bp, rxr, rxr->rx_prod, GFP_ATOMIC)) {
188 trace_xdp_exception(bp->dev, xdp_prog, act);
189 bnxt_reuse_rx_data(rxr, cons, page);
190 return true;
191 }
192
193 if (xdp_do_redirect(bp->dev, &xdp, xdp_prog)) {
194 trace_xdp_exception(bp->dev, xdp_prog, act);
195 page_pool_recycle_direct(rxr->page_pool, page);
196 return true;
197 }
198
199 *event |= BNXT_REDIRECT_EVENT;
200 break;
201 default:
202 bpf_warn_invalid_xdp_action(act);
203
204 case XDP_ABORTED:
205 trace_xdp_exception(bp->dev, xdp_prog, act);
206
207 case XDP_DROP:
208 bnxt_reuse_rx_data(rxr, cons, page);
209 break;
210 }
211 return true;
212}
213
214int bnxt_xdp_xmit(struct net_device *dev, int num_frames,
215 struct xdp_frame **frames, u32 flags)
216{
217 struct bnxt *bp = netdev_priv(dev);
218 struct bpf_prog *xdp_prog = READ_ONCE(bp->xdp_prog);
219 struct pci_dev *pdev = bp->pdev;
220 struct bnxt_tx_ring_info *txr;
221 dma_addr_t mapping;
222 int drops = 0;
223 int ring;
224 int i;
225
226 if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
227 !bp->tx_nr_rings_xdp ||
228 !xdp_prog)
229 return -EINVAL;
230
231 ring = smp_processor_id() % bp->tx_nr_rings_xdp;
232 txr = &bp->tx_ring[ring];
233
234 for (i = 0; i < num_frames; i++) {
235 struct xdp_frame *xdp = frames[i];
236
237 if (!txr || !bnxt_tx_avail(bp, txr) ||
238 !(bp->bnapi[ring]->flags & BNXT_NAPI_FLAG_XDP)) {
239 xdp_return_frame_rx_napi(xdp);
240 drops++;
241 continue;
242 }
243
244 mapping = dma_map_single(&pdev->dev, xdp->data, xdp->len,
245 DMA_TO_DEVICE);
246
247 if (dma_mapping_error(&pdev->dev, mapping)) {
248 xdp_return_frame_rx_napi(xdp);
249 drops++;
250 continue;
251 }
252 __bnxt_xmit_xdp_redirect(bp, txr, mapping, xdp->len, xdp);
253 }
254
255 if (flags & XDP_XMIT_FLUSH) {
256
257 wmb();
258 bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
259 }
260
261 return num_frames - drops;
262}
263
264
265static int bnxt_xdp_set(struct bnxt *bp, struct bpf_prog *prog)
266{
267 struct net_device *dev = bp->dev;
268 int tx_xdp = 0, rc, tc;
269 struct bpf_prog *old;
270
271 if (prog && bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
272 netdev_warn(dev, "MTU %d larger than largest XDP supported MTU %d.\n",
273 bp->dev->mtu, BNXT_MAX_PAGE_MODE_MTU);
274 return -EOPNOTSUPP;
275 }
276 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) {
277 netdev_warn(dev, "ethtool rx/tx channels must be combined to support XDP.\n");
278 return -EOPNOTSUPP;
279 }
280 if (prog)
281 tx_xdp = bp->rx_nr_rings;
282
283 tc = netdev_get_num_tc(dev);
284 if (!tc)
285 tc = 1;
286 rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
287 true, tc, tx_xdp);
288 if (rc) {
289 netdev_warn(dev, "Unable to reserve enough TX rings to support XDP.\n");
290 return rc;
291 }
292 if (netif_running(dev))
293 bnxt_close_nic(bp, true, false);
294
295 old = xchg(&bp->xdp_prog, prog);
296 if (old)
297 bpf_prog_put(old);
298
299 if (prog) {
300 bnxt_set_rx_skb_mode(bp, true);
301 } else {
302 int rx, tx;
303
304 bnxt_set_rx_skb_mode(bp, false);
305 bnxt_get_max_rings(bp, &rx, &tx, true);
306 if (rx > 1) {
307 bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
308 bp->dev->hw_features |= NETIF_F_LRO;
309 }
310 }
311 bp->tx_nr_rings_xdp = tx_xdp;
312 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc + tx_xdp;
313 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
314 bnxt_set_tpa_flags(bp);
315 bnxt_set_ring_params(bp);
316
317 if (netif_running(dev))
318 return bnxt_open_nic(bp, true, false);
319
320 return 0;
321}
322
323int bnxt_xdp(struct net_device *dev, struct netdev_bpf *xdp)
324{
325 struct bnxt *bp = netdev_priv(dev);
326 int rc;
327
328 switch (xdp->command) {
329 case XDP_SETUP_PROG:
330 rc = bnxt_xdp_set(bp, xdp->prog);
331 break;
332 case XDP_QUERY_PROG:
333 xdp->prog_id = bp->xdp_prog ? bp->xdp_prog->aux->id : 0;
334 rc = 0;
335 break;
336 default:
337 rc = -EINVAL;
338 break;
339 }
340 return rc;
341}
342