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 <net/page_pool/helpers.h>
19 #include "bnxt_hsi.h"
20 #include "bnxt.h"
21 #include "bnxt_xdp.h"
22 
23 DEFINE_STATIC_KEY_FALSE(bnxt_xdp_locking_key);
24 
bnxt_xmit_bd(struct bnxt * bp,struct bnxt_tx_ring_info * txr,dma_addr_t mapping,u32 len,struct xdp_buff * xdp)25 struct bnxt_sw_tx_bd *bnxt_xmit_bd(struct bnxt *bp,
26 				   struct bnxt_tx_ring_info *txr,
27 				   dma_addr_t mapping, u32 len,
28 				   struct xdp_buff *xdp)
29 {
30 	struct skb_shared_info *sinfo;
31 	struct bnxt_sw_tx_bd *tx_buf;
32 	struct tx_bd *txbd;
33 	int num_frags = 0;
34 	u32 flags;
35 	u16 prod;
36 	int i;
37 
38 	if (xdp && xdp_buff_has_frags(xdp)) {
39 		sinfo = xdp_get_shared_info_from_buff(xdp);
40 		num_frags = sinfo->nr_frags;
41 	}
42 
43 	/* fill up the first buffer */
44 	prod = txr->tx_prod;
45 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
46 	tx_buf->nr_frags = num_frags;
47 	if (xdp)
48 		tx_buf->page = virt_to_head_page(xdp->data);
49 
50 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
51 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_CNT(num_frags + 1) |
52 		bnxt_lhint_arr[len >> 9];
53 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
54 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 1 + num_frags);
55 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
56 
57 	/* now let us fill up the frags into the next buffers */
58 	for (i = 0; i < num_frags ; i++) {
59 		skb_frag_t *frag = &sinfo->frags[i];
60 		struct bnxt_sw_tx_bd *frag_tx_buf;
61 		dma_addr_t frag_mapping;
62 		int frag_len;
63 
64 		prod = NEXT_TX(prod);
65 		WRITE_ONCE(txr->tx_prod, prod);
66 
67 		/* first fill up the first buffer */
68 		frag_tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
69 		frag_tx_buf->page = skb_frag_page(frag);
70 
71 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
72 
73 		frag_len = skb_frag_size(frag);
74 		flags = frag_len << TX_BD_LEN_SHIFT;
75 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
76 		frag_mapping = page_pool_get_dma_addr(skb_frag_page(frag)) +
77 			       skb_frag_off(frag);
78 		txbd->tx_bd_haddr = cpu_to_le64(frag_mapping);
79 
80 		len = frag_len;
81 	}
82 
83 	flags &= ~TX_BD_LEN;
84 	txbd->tx_bd_len_flags_type = cpu_to_le32(((len) << TX_BD_LEN_SHIFT) | flags |
85 			TX_BD_FLAGS_PACKET_END);
86 	/* Sync TX BD */
87 	wmb();
88 	prod = NEXT_TX(prod);
89 	WRITE_ONCE(txr->tx_prod, prod);
90 
91 	return tx_buf;
92 }
93 
__bnxt_xmit_xdp(struct bnxt * bp,struct bnxt_tx_ring_info * txr,dma_addr_t mapping,u32 len,u16 rx_prod,struct xdp_buff * xdp)94 static void __bnxt_xmit_xdp(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
95 			    dma_addr_t mapping, u32 len, u16 rx_prod,
96 			    struct xdp_buff *xdp)
97 {
98 	struct bnxt_sw_tx_bd *tx_buf;
99 
100 	tx_buf = bnxt_xmit_bd(bp, txr, mapping, len, xdp);
101 	tx_buf->rx_prod = rx_prod;
102 	tx_buf->action = XDP_TX;
103 
104 }
105 
__bnxt_xmit_xdp_redirect(struct bnxt * bp,struct bnxt_tx_ring_info * txr,dma_addr_t mapping,u32 len,struct xdp_frame * xdpf)106 static void __bnxt_xmit_xdp_redirect(struct bnxt *bp,
107 				     struct bnxt_tx_ring_info *txr,
108 				     dma_addr_t mapping, u32 len,
109 				     struct xdp_frame *xdpf)
110 {
111 	struct bnxt_sw_tx_bd *tx_buf;
112 
113 	tx_buf = bnxt_xmit_bd(bp, txr, mapping, len, NULL);
114 	tx_buf->action = XDP_REDIRECT;
115 	tx_buf->xdpf = xdpf;
116 	dma_unmap_addr_set(tx_buf, mapping, mapping);
117 	dma_unmap_len_set(tx_buf, len, 0);
118 }
119 
bnxt_tx_int_xdp(struct bnxt * bp,struct bnxt_napi * bnapi,int budget)120 void bnxt_tx_int_xdp(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
121 {
122 	struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
123 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
124 	u16 tx_hw_cons = txr->tx_hw_cons;
125 	bool rx_doorbell_needed = false;
126 	struct bnxt_sw_tx_bd *tx_buf;
127 	u16 tx_cons = txr->tx_cons;
128 	u16 last_tx_cons = tx_cons;
129 	int j, frags;
130 
131 	if (!budget)
132 		return;
133 
134 	while (RING_TX(bp, tx_cons) != tx_hw_cons) {
135 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, tx_cons)];
136 
137 		if (tx_buf->action == XDP_REDIRECT) {
138 			struct pci_dev *pdev = bp->pdev;
139 
140 			dma_unmap_single(&pdev->dev,
141 					 dma_unmap_addr(tx_buf, mapping),
142 					 dma_unmap_len(tx_buf, len),
143 					 DMA_TO_DEVICE);
144 			xdp_return_frame(tx_buf->xdpf);
145 			tx_buf->action = 0;
146 			tx_buf->xdpf = NULL;
147 		} else if (tx_buf->action == XDP_TX) {
148 			tx_buf->action = 0;
149 			rx_doorbell_needed = true;
150 			last_tx_cons = tx_cons;
151 
152 			frags = tx_buf->nr_frags;
153 			for (j = 0; j < frags; j++) {
154 				tx_cons = NEXT_TX(tx_cons);
155 				tx_buf = &txr->tx_buf_ring[RING_TX(bp, tx_cons)];
156 				page_pool_recycle_direct(rxr->page_pool, tx_buf->page);
157 			}
158 		} else {
159 			bnxt_sched_reset_txr(bp, txr, tx_cons);
160 			return;
161 		}
162 		tx_cons = NEXT_TX(tx_cons);
163 	}
164 
165 	bnapi->events &= ~BNXT_TX_CMP_EVENT;
166 	WRITE_ONCE(txr->tx_cons, tx_cons);
167 	if (rx_doorbell_needed) {
168 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, last_tx_cons)];
169 		bnxt_db_write(bp, &rxr->rx_db, tx_buf->rx_prod);
170 
171 	}
172 }
173 
bnxt_xdp_attached(struct bnxt * bp,struct bnxt_rx_ring_info * rxr)174 bool bnxt_xdp_attached(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
175 {
176 	struct bpf_prog *xdp_prog = READ_ONCE(rxr->xdp_prog);
177 
178 	return !!xdp_prog;
179 }
180 
bnxt_xdp_buff_init(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,u8 * data_ptr,unsigned int len,struct xdp_buff * xdp)181 void bnxt_xdp_buff_init(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
182 			u16 cons, u8 *data_ptr, unsigned int len,
183 			struct xdp_buff *xdp)
184 {
185 	u32 buflen = BNXT_RX_PAGE_SIZE;
186 	struct bnxt_sw_rx_bd *rx_buf;
187 	struct pci_dev *pdev;
188 	dma_addr_t mapping;
189 	u32 offset;
190 
191 	pdev = bp->pdev;
192 	rx_buf = &rxr->rx_buf_ring[cons];
193 	offset = bp->rx_offset;
194 
195 	mapping = rx_buf->mapping - bp->rx_dma_offset;
196 	dma_sync_single_for_cpu(&pdev->dev, mapping + offset, len, bp->rx_dir);
197 
198 	xdp_init_buff(xdp, buflen, &rxr->xdp_rxq);
199 	xdp_prepare_buff(xdp, data_ptr - offset, offset, len, true);
200 }
201 
bnxt_xdp_buff_frags_free(struct bnxt_rx_ring_info * rxr,struct xdp_buff * xdp)202 void bnxt_xdp_buff_frags_free(struct bnxt_rx_ring_info *rxr,
203 			      struct xdp_buff *xdp)
204 {
205 	struct skb_shared_info *shinfo;
206 	int i;
207 
208 	if (!xdp || !xdp_buff_has_frags(xdp))
209 		return;
210 	shinfo = xdp_get_shared_info_from_buff(xdp);
211 	for (i = 0; i < shinfo->nr_frags; i++) {
212 		struct page *page = skb_frag_page(&shinfo->frags[i]);
213 
214 		page_pool_recycle_direct(rxr->page_pool, page);
215 	}
216 	shinfo->nr_frags = 0;
217 }
218 
219 /* returns the following:
220  * true    - packet consumed by XDP and new buffer is allocated.
221  * false   - packet should be passed to the stack.
222  */
bnxt_rx_xdp(struct bnxt * bp,struct bnxt_rx_ring_info * rxr,u16 cons,struct xdp_buff * xdp,struct page * page,u8 ** data_ptr,unsigned int * len,u8 * event)223 bool bnxt_rx_xdp(struct bnxt *bp, struct bnxt_rx_ring_info *rxr, u16 cons,
224 		 struct xdp_buff *xdp, struct page *page, u8 **data_ptr,
225 		 unsigned int *len, u8 *event)
226 {
227 	struct bpf_prog *xdp_prog = READ_ONCE(rxr->xdp_prog);
228 	struct bnxt_tx_ring_info *txr;
229 	struct bnxt_sw_rx_bd *rx_buf;
230 	struct pci_dev *pdev;
231 	dma_addr_t mapping;
232 	u32 tx_needed = 1;
233 	void *orig_data;
234 	u32 tx_avail;
235 	u32 offset;
236 	u32 act;
237 
238 	if (!xdp_prog)
239 		return false;
240 
241 	pdev = bp->pdev;
242 	offset = bp->rx_offset;
243 
244 	txr = rxr->bnapi->tx_ring[0];
245 	/* BNXT_RX_PAGE_MODE(bp) when XDP enabled */
246 	orig_data = xdp->data;
247 
248 	act = bpf_prog_run_xdp(xdp_prog, xdp);
249 
250 	tx_avail = bnxt_tx_avail(bp, txr);
251 	/* If the tx ring is not full, we must not update the rx producer yet
252 	 * because we may still be transmitting on some BDs.
253 	 */
254 	if (tx_avail != bp->tx_ring_size)
255 		*event &= ~BNXT_RX_EVENT;
256 
257 	*len = xdp->data_end - xdp->data;
258 	if (orig_data != xdp->data) {
259 		offset = xdp->data - xdp->data_hard_start;
260 		*data_ptr = xdp->data_hard_start + offset;
261 	}
262 
263 	switch (act) {
264 	case XDP_PASS:
265 		return false;
266 
267 	case XDP_TX:
268 		rx_buf = &rxr->rx_buf_ring[cons];
269 		mapping = rx_buf->mapping - bp->rx_dma_offset;
270 		*event &= BNXT_TX_CMP_EVENT;
271 
272 		if (unlikely(xdp_buff_has_frags(xdp))) {
273 			struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
274 
275 			tx_needed += sinfo->nr_frags;
276 			*event = BNXT_AGG_EVENT;
277 		}
278 
279 		if (tx_avail < tx_needed) {
280 			trace_xdp_exception(bp->dev, xdp_prog, act);
281 			bnxt_xdp_buff_frags_free(rxr, xdp);
282 			bnxt_reuse_rx_data(rxr, cons, page);
283 			return true;
284 		}
285 
286 		dma_sync_single_for_device(&pdev->dev, mapping + offset, *len,
287 					   bp->rx_dir);
288 
289 		*event |= BNXT_TX_EVENT;
290 		__bnxt_xmit_xdp(bp, txr, mapping + offset, *len,
291 				NEXT_RX(rxr->rx_prod), xdp);
292 		bnxt_reuse_rx_data(rxr, cons, page);
293 		return true;
294 	case XDP_REDIRECT:
295 		/* if we are calling this here then we know that the
296 		 * redirect is coming from a frame received by the
297 		 * bnxt_en driver.
298 		 */
299 
300 		/* if we are unable to allocate a new buffer, abort and reuse */
301 		if (bnxt_alloc_rx_data(bp, rxr, rxr->rx_prod, GFP_ATOMIC)) {
302 			trace_xdp_exception(bp->dev, xdp_prog, act);
303 			bnxt_xdp_buff_frags_free(rxr, xdp);
304 			bnxt_reuse_rx_data(rxr, cons, page);
305 			return true;
306 		}
307 
308 		if (xdp_do_redirect(bp->dev, xdp, xdp_prog)) {
309 			trace_xdp_exception(bp->dev, xdp_prog, act);
310 			page_pool_recycle_direct(rxr->page_pool, page);
311 			return true;
312 		}
313 
314 		*event |= BNXT_REDIRECT_EVENT;
315 		break;
316 	default:
317 		bpf_warn_invalid_xdp_action(bp->dev, xdp_prog, act);
318 		fallthrough;
319 	case XDP_ABORTED:
320 		trace_xdp_exception(bp->dev, xdp_prog, act);
321 		fallthrough;
322 	case XDP_DROP:
323 		bnxt_xdp_buff_frags_free(rxr, xdp);
324 		bnxt_reuse_rx_data(rxr, cons, page);
325 		break;
326 	}
327 	return true;
328 }
329 
bnxt_xdp_xmit(struct net_device * dev,int num_frames,struct xdp_frame ** frames,u32 flags)330 int bnxt_xdp_xmit(struct net_device *dev, int num_frames,
331 		  struct xdp_frame **frames, u32 flags)
332 {
333 	struct bnxt *bp = netdev_priv(dev);
334 	struct bpf_prog *xdp_prog = READ_ONCE(bp->xdp_prog);
335 	struct pci_dev *pdev = bp->pdev;
336 	struct bnxt_tx_ring_info *txr;
337 	dma_addr_t mapping;
338 	int nxmit = 0;
339 	int ring;
340 	int i;
341 
342 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
343 	    !bp->tx_nr_rings_xdp ||
344 	    !xdp_prog)
345 		return -EINVAL;
346 
347 	ring = smp_processor_id() % bp->tx_nr_rings_xdp;
348 	txr = &bp->tx_ring[ring];
349 
350 	if (READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING)
351 		return -EINVAL;
352 
353 	if (static_branch_unlikely(&bnxt_xdp_locking_key))
354 		spin_lock(&txr->xdp_tx_lock);
355 
356 	for (i = 0; i < num_frames; i++) {
357 		struct xdp_frame *xdp = frames[i];
358 
359 		if (!bnxt_tx_avail(bp, txr))
360 			break;
361 
362 		mapping = dma_map_single(&pdev->dev, xdp->data, xdp->len,
363 					 DMA_TO_DEVICE);
364 
365 		if (dma_mapping_error(&pdev->dev, mapping))
366 			break;
367 
368 		__bnxt_xmit_xdp_redirect(bp, txr, mapping, xdp->len, xdp);
369 		nxmit++;
370 	}
371 
372 	if (flags & XDP_XMIT_FLUSH) {
373 		/* Sync BD data before updating doorbell */
374 		wmb();
375 		bnxt_db_write(bp, &txr->tx_db, txr->tx_prod);
376 	}
377 
378 	if (static_branch_unlikely(&bnxt_xdp_locking_key))
379 		spin_unlock(&txr->xdp_tx_lock);
380 
381 	return nxmit;
382 }
383 
384 /* Under rtnl_lock */
bnxt_xdp_set(struct bnxt * bp,struct bpf_prog * prog)385 static int bnxt_xdp_set(struct bnxt *bp, struct bpf_prog *prog)
386 {
387 	struct net_device *dev = bp->dev;
388 	int tx_xdp = 0, tx_cp, rc, tc;
389 	struct bpf_prog *old;
390 
391 	if (prog && !prog->aux->xdp_has_frags &&
392 	    bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
393 		netdev_warn(dev, "MTU %d larger than %d without XDP frag support.\n",
394 			    bp->dev->mtu, BNXT_MAX_PAGE_MODE_MTU);
395 		return -EOPNOTSUPP;
396 	}
397 	if (prog && bp->flags & BNXT_FLAG_HDS) {
398 		netdev_warn(dev, "XDP is disallowed when HDS is enabled.\n");
399 		return -EOPNOTSUPP;
400 	}
401 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS)) {
402 		netdev_warn(dev, "ethtool rx/tx channels must be combined to support XDP.\n");
403 		return -EOPNOTSUPP;
404 	}
405 	if (prog)
406 		tx_xdp = bp->rx_nr_rings;
407 
408 	tc = bp->num_tc;
409 	if (!tc)
410 		tc = 1;
411 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
412 			      true, tc, tx_xdp);
413 	if (rc) {
414 		netdev_warn(dev, "Unable to reserve enough TX rings to support XDP.\n");
415 		return rc;
416 	}
417 	if (netif_running(dev))
418 		bnxt_close_nic(bp, true, false);
419 
420 	old = xchg(&bp->xdp_prog, prog);
421 	if (old)
422 		bpf_prog_put(old);
423 
424 	if (prog) {
425 		bnxt_set_rx_skb_mode(bp, true);
426 		xdp_features_set_redirect_target(dev, true);
427 	} else {
428 		xdp_features_clear_redirect_target(dev);
429 		bnxt_set_rx_skb_mode(bp, false);
430 	}
431 	bp->tx_nr_rings_xdp = tx_xdp;
432 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc + tx_xdp;
433 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
434 	bp->cp_nr_rings = max_t(int, tx_cp, bp->rx_nr_rings);
435 	bnxt_set_tpa_flags(bp);
436 	bnxt_set_ring_params(bp);
437 
438 	if (netif_running(dev))
439 		return bnxt_open_nic(bp, true, false);
440 
441 	return 0;
442 }
443 
bnxt_xdp(struct net_device * dev,struct netdev_bpf * xdp)444 int bnxt_xdp(struct net_device *dev, struct netdev_bpf *xdp)
445 {
446 	struct bnxt *bp = netdev_priv(dev);
447 	int rc;
448 
449 	switch (xdp->command) {
450 	case XDP_SETUP_PROG:
451 		rc = bnxt_xdp_set(bp, xdp->prog);
452 		break;
453 	default:
454 		rc = -EINVAL;
455 		break;
456 	}
457 	return rc;
458 }
459 
460 struct sk_buff *
bnxt_xdp_build_skb(struct bnxt * bp,struct sk_buff * skb,u8 num_frags,struct page_pool * pool,struct xdp_buff * xdp)461 bnxt_xdp_build_skb(struct bnxt *bp, struct sk_buff *skb, u8 num_frags,
462 		   struct page_pool *pool, struct xdp_buff *xdp)
463 {
464 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
465 
466 	if (!skb)
467 		return NULL;
468 
469 	xdp_update_skb_shared_info(skb, num_frags,
470 				   sinfo->xdp_frags_size,
471 				   BNXT_RX_PAGE_SIZE * num_frags,
472 				   xdp_buff_is_frag_pfmemalloc(xdp));
473 	return skb;
474 }
475