1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
3 
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock_drv.h>
6 #include <net/xdp.h>
7 
8 #include "ixgbe.h"
9 #include "ixgbe_txrx_common.h"
10 
11 struct xsk_buff_pool *ixgbe_xsk_pool(struct ixgbe_adapter *adapter,
12 				     struct ixgbe_ring *ring)
13 {
14 	bool xdp_on = READ_ONCE(adapter->xdp_prog);
15 	int qid = ring->ring_idx;
16 
17 	if (!xdp_on || !test_bit(qid, adapter->af_xdp_zc_qps))
18 		return NULL;
19 
20 	return xsk_get_pool_from_qid(adapter->netdev, qid);
21 }
22 
23 static int ixgbe_xsk_pool_enable(struct ixgbe_adapter *adapter,
24 				 struct xsk_buff_pool *pool,
25 				 u16 qid)
26 {
27 	struct net_device *netdev = adapter->netdev;
28 	bool if_running;
29 	int err;
30 
31 	if (qid >= adapter->num_rx_queues)
32 		return -EINVAL;
33 
34 	if (qid >= netdev->real_num_rx_queues ||
35 	    qid >= netdev->real_num_tx_queues)
36 		return -EINVAL;
37 
38 	err = xsk_pool_dma_map(pool, &adapter->pdev->dev, IXGBE_RX_DMA_ATTR);
39 	if (err)
40 		return err;
41 
42 	if_running = netif_running(adapter->netdev) &&
43 		     ixgbe_enabled_xdp_adapter(adapter);
44 
45 	if (if_running)
46 		ixgbe_txrx_ring_disable(adapter, qid);
47 
48 	set_bit(qid, adapter->af_xdp_zc_qps);
49 
50 	if (if_running) {
51 		ixgbe_txrx_ring_enable(adapter, qid);
52 
53 		/* Kick start the NAPI context so that receiving will start */
54 		err = ixgbe_xsk_wakeup(adapter->netdev, qid, XDP_WAKEUP_RX);
55 		if (err) {
56 			clear_bit(qid, adapter->af_xdp_zc_qps);
57 			xsk_pool_dma_unmap(pool, IXGBE_RX_DMA_ATTR);
58 			return err;
59 		}
60 	}
61 
62 	return 0;
63 }
64 
65 static int ixgbe_xsk_pool_disable(struct ixgbe_adapter *adapter, u16 qid)
66 {
67 	struct xsk_buff_pool *pool;
68 	bool if_running;
69 
70 	pool = xsk_get_pool_from_qid(adapter->netdev, qid);
71 	if (!pool)
72 		return -EINVAL;
73 
74 	if_running = netif_running(adapter->netdev) &&
75 		     ixgbe_enabled_xdp_adapter(adapter);
76 
77 	if (if_running)
78 		ixgbe_txrx_ring_disable(adapter, qid);
79 
80 	clear_bit(qid, adapter->af_xdp_zc_qps);
81 	xsk_pool_dma_unmap(pool, IXGBE_RX_DMA_ATTR);
82 
83 	if (if_running)
84 		ixgbe_txrx_ring_enable(adapter, qid);
85 
86 	return 0;
87 }
88 
89 int ixgbe_xsk_pool_setup(struct ixgbe_adapter *adapter,
90 			 struct xsk_buff_pool *pool,
91 			 u16 qid)
92 {
93 	return pool ? ixgbe_xsk_pool_enable(adapter, pool, qid) :
94 		ixgbe_xsk_pool_disable(adapter, qid);
95 }
96 
97 static int ixgbe_run_xdp_zc(struct ixgbe_adapter *adapter,
98 			    struct ixgbe_ring *rx_ring,
99 			    struct xdp_buff *xdp)
100 {
101 	int err, result = IXGBE_XDP_PASS;
102 	struct bpf_prog *xdp_prog;
103 	struct ixgbe_ring *ring;
104 	struct xdp_frame *xdpf;
105 	u32 act;
106 
107 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
108 	act = bpf_prog_run_xdp(xdp_prog, xdp);
109 
110 	if (likely(act == XDP_REDIRECT)) {
111 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
112 		if (err)
113 			goto out_failure;
114 		return IXGBE_XDP_REDIR;
115 	}
116 
117 	switch (act) {
118 	case XDP_PASS:
119 		break;
120 	case XDP_TX:
121 		xdpf = xdp_convert_buff_to_frame(xdp);
122 		if (unlikely(!xdpf))
123 			goto out_failure;
124 		ring = ixgbe_determine_xdp_ring(adapter);
125 		if (static_branch_unlikely(&ixgbe_xdp_locking_key))
126 			spin_lock(&ring->tx_lock);
127 		result = ixgbe_xmit_xdp_ring(ring, xdpf);
128 		if (static_branch_unlikely(&ixgbe_xdp_locking_key))
129 			spin_unlock(&ring->tx_lock);
130 		if (result == IXGBE_XDP_CONSUMED)
131 			goto out_failure;
132 		break;
133 	default:
134 		bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
135 		fallthrough;
136 	case XDP_ABORTED:
137 out_failure:
138 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
139 		fallthrough; /* handle aborts by dropping packet */
140 	case XDP_DROP:
141 		result = IXGBE_XDP_CONSUMED;
142 		break;
143 	}
144 	return result;
145 }
146 
147 bool ixgbe_alloc_rx_buffers_zc(struct ixgbe_ring *rx_ring, u16 count)
148 {
149 	union ixgbe_adv_rx_desc *rx_desc;
150 	struct ixgbe_rx_buffer *bi;
151 	u16 i = rx_ring->next_to_use;
152 	dma_addr_t dma;
153 	bool ok = true;
154 
155 	/* nothing to do */
156 	if (!count)
157 		return true;
158 
159 	rx_desc = IXGBE_RX_DESC(rx_ring, i);
160 	bi = &rx_ring->rx_buffer_info[i];
161 	i -= rx_ring->count;
162 
163 	do {
164 		bi->xdp = xsk_buff_alloc(rx_ring->xsk_pool);
165 		if (!bi->xdp) {
166 			ok = false;
167 			break;
168 		}
169 
170 		dma = xsk_buff_xdp_get_dma(bi->xdp);
171 
172 		/* Refresh the desc even if buffer_addrs didn't change
173 		 * because each write-back erases this info.
174 		 */
175 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
176 
177 		rx_desc++;
178 		bi++;
179 		i++;
180 		if (unlikely(!i)) {
181 			rx_desc = IXGBE_RX_DESC(rx_ring, 0);
182 			bi = rx_ring->rx_buffer_info;
183 			i -= rx_ring->count;
184 		}
185 
186 		/* clear the length for the next_to_use descriptor */
187 		rx_desc->wb.upper.length = 0;
188 
189 		count--;
190 	} while (count);
191 
192 	i += rx_ring->count;
193 
194 	if (rx_ring->next_to_use != i) {
195 		rx_ring->next_to_use = i;
196 
197 		/* Force memory writes to complete before letting h/w
198 		 * know there are new descriptors to fetch.  (Only
199 		 * applicable for weak-ordered memory model archs,
200 		 * such as IA-64).
201 		 */
202 		wmb();
203 		writel(i, rx_ring->tail);
204 	}
205 
206 	return ok;
207 }
208 
209 static struct sk_buff *ixgbe_construct_skb_zc(struct ixgbe_ring *rx_ring,
210 					      const struct xdp_buff *xdp)
211 {
212 	unsigned int totalsize = xdp->data_end - xdp->data_meta;
213 	unsigned int metasize = xdp->data - xdp->data_meta;
214 	struct sk_buff *skb;
215 
216 	net_prefetch(xdp->data_meta);
217 
218 	/* allocate a skb to store the frags */
219 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
220 			       GFP_ATOMIC | __GFP_NOWARN);
221 	if (unlikely(!skb))
222 		return NULL;
223 
224 	memcpy(__skb_put(skb, totalsize), xdp->data_meta,
225 	       ALIGN(totalsize, sizeof(long)));
226 
227 	if (metasize) {
228 		skb_metadata_set(skb, metasize);
229 		__skb_pull(skb, metasize);
230 	}
231 
232 	return skb;
233 }
234 
235 static void ixgbe_inc_ntc(struct ixgbe_ring *rx_ring)
236 {
237 	u32 ntc = rx_ring->next_to_clean + 1;
238 
239 	ntc = (ntc < rx_ring->count) ? ntc : 0;
240 	rx_ring->next_to_clean = ntc;
241 	prefetch(IXGBE_RX_DESC(rx_ring, ntc));
242 }
243 
244 int ixgbe_clean_rx_irq_zc(struct ixgbe_q_vector *q_vector,
245 			  struct ixgbe_ring *rx_ring,
246 			  const int budget)
247 {
248 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
249 	struct ixgbe_adapter *adapter = q_vector->adapter;
250 	u16 cleaned_count = ixgbe_desc_unused(rx_ring);
251 	unsigned int xdp_res, xdp_xmit = 0;
252 	bool failure = false;
253 	struct sk_buff *skb;
254 
255 	while (likely(total_rx_packets < budget)) {
256 		union ixgbe_adv_rx_desc *rx_desc;
257 		struct ixgbe_rx_buffer *bi;
258 		unsigned int size;
259 
260 		/* return some buffers to hardware, one at a time is too slow */
261 		if (cleaned_count >= IXGBE_RX_BUFFER_WRITE) {
262 			failure = failure ||
263 				  !ixgbe_alloc_rx_buffers_zc(rx_ring,
264 							     cleaned_count);
265 			cleaned_count = 0;
266 		}
267 
268 		rx_desc = IXGBE_RX_DESC(rx_ring, rx_ring->next_to_clean);
269 		size = le16_to_cpu(rx_desc->wb.upper.length);
270 		if (!size)
271 			break;
272 
273 		/* This memory barrier is needed to keep us from reading
274 		 * any other fields out of the rx_desc until we know the
275 		 * descriptor has been written back
276 		 */
277 		dma_rmb();
278 
279 		bi = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
280 
281 		if (unlikely(!ixgbe_test_staterr(rx_desc,
282 						 IXGBE_RXD_STAT_EOP))) {
283 			struct ixgbe_rx_buffer *next_bi;
284 
285 			xsk_buff_free(bi->xdp);
286 			bi->xdp = NULL;
287 			ixgbe_inc_ntc(rx_ring);
288 			next_bi =
289 			       &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
290 			next_bi->discard = true;
291 			continue;
292 		}
293 
294 		if (unlikely(bi->discard)) {
295 			xsk_buff_free(bi->xdp);
296 			bi->xdp = NULL;
297 			bi->discard = false;
298 			ixgbe_inc_ntc(rx_ring);
299 			continue;
300 		}
301 
302 		bi->xdp->data_end = bi->xdp->data + size;
303 		xsk_buff_dma_sync_for_cpu(bi->xdp, rx_ring->xsk_pool);
304 		xdp_res = ixgbe_run_xdp_zc(adapter, rx_ring, bi->xdp);
305 
306 		if (xdp_res) {
307 			if (xdp_res & (IXGBE_XDP_TX | IXGBE_XDP_REDIR))
308 				xdp_xmit |= xdp_res;
309 			else
310 				xsk_buff_free(bi->xdp);
311 
312 			bi->xdp = NULL;
313 			total_rx_packets++;
314 			total_rx_bytes += size;
315 
316 			cleaned_count++;
317 			ixgbe_inc_ntc(rx_ring);
318 			continue;
319 		}
320 
321 		/* XDP_PASS path */
322 		skb = ixgbe_construct_skb_zc(rx_ring, bi->xdp);
323 		if (!skb) {
324 			rx_ring->rx_stats.alloc_rx_buff_failed++;
325 			break;
326 		}
327 
328 		xsk_buff_free(bi->xdp);
329 		bi->xdp = NULL;
330 
331 		cleaned_count++;
332 		ixgbe_inc_ntc(rx_ring);
333 
334 		if (eth_skb_pad(skb))
335 			continue;
336 
337 		total_rx_bytes += skb->len;
338 		total_rx_packets++;
339 
340 		ixgbe_process_skb_fields(rx_ring, rx_desc, skb);
341 		ixgbe_rx_skb(q_vector, skb);
342 	}
343 
344 	if (xdp_xmit & IXGBE_XDP_REDIR)
345 		xdp_do_flush_map();
346 
347 	if (xdp_xmit & IXGBE_XDP_TX) {
348 		struct ixgbe_ring *ring = ixgbe_determine_xdp_ring(adapter);
349 
350 		ixgbe_xdp_ring_update_tail_locked(ring);
351 	}
352 
353 	u64_stats_update_begin(&rx_ring->syncp);
354 	rx_ring->stats.packets += total_rx_packets;
355 	rx_ring->stats.bytes += total_rx_bytes;
356 	u64_stats_update_end(&rx_ring->syncp);
357 	q_vector->rx.total_packets += total_rx_packets;
358 	q_vector->rx.total_bytes += total_rx_bytes;
359 
360 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
361 		if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
362 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
363 		else
364 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
365 
366 		return (int)total_rx_packets;
367 	}
368 	return failure ? budget : (int)total_rx_packets;
369 }
370 
371 void ixgbe_xsk_clean_rx_ring(struct ixgbe_ring *rx_ring)
372 {
373 	struct ixgbe_rx_buffer *bi;
374 	u16 i;
375 
376 	for (i = 0; i < rx_ring->count; i++) {
377 		bi = &rx_ring->rx_buffer_info[i];
378 
379 		if (!bi->xdp)
380 			continue;
381 
382 		xsk_buff_free(bi->xdp);
383 		bi->xdp = NULL;
384 	}
385 }
386 
387 static bool ixgbe_xmit_zc(struct ixgbe_ring *xdp_ring, unsigned int budget)
388 {
389 	struct xsk_buff_pool *pool = xdp_ring->xsk_pool;
390 	union ixgbe_adv_tx_desc *tx_desc = NULL;
391 	struct ixgbe_tx_buffer *tx_bi;
392 	bool work_done = true;
393 	struct xdp_desc desc;
394 	dma_addr_t dma;
395 	u32 cmd_type;
396 
397 	while (budget-- > 0) {
398 		if (unlikely(!ixgbe_desc_unused(xdp_ring))) {
399 			work_done = false;
400 			break;
401 		}
402 
403 		if (!netif_carrier_ok(xdp_ring->netdev))
404 			break;
405 
406 		if (!xsk_tx_peek_desc(pool, &desc))
407 			break;
408 
409 		dma = xsk_buff_raw_get_dma(pool, desc.addr);
410 		xsk_buff_raw_dma_sync_for_device(pool, dma, desc.len);
411 
412 		tx_bi = &xdp_ring->tx_buffer_info[xdp_ring->next_to_use];
413 		tx_bi->bytecount = desc.len;
414 		tx_bi->xdpf = NULL;
415 		tx_bi->gso_segs = 1;
416 
417 		tx_desc = IXGBE_TX_DESC(xdp_ring, xdp_ring->next_to_use);
418 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
419 
420 		/* put descriptor type bits */
421 		cmd_type = IXGBE_ADVTXD_DTYP_DATA |
422 			   IXGBE_ADVTXD_DCMD_DEXT |
423 			   IXGBE_ADVTXD_DCMD_IFCS;
424 		cmd_type |= desc.len | IXGBE_TXD_CMD;
425 		tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
426 		tx_desc->read.olinfo_status =
427 			cpu_to_le32(desc.len << IXGBE_ADVTXD_PAYLEN_SHIFT);
428 
429 		xdp_ring->next_to_use++;
430 		if (xdp_ring->next_to_use == xdp_ring->count)
431 			xdp_ring->next_to_use = 0;
432 	}
433 
434 	if (tx_desc) {
435 		ixgbe_xdp_ring_update_tail(xdp_ring);
436 		xsk_tx_release(pool);
437 	}
438 
439 	return !!budget && work_done;
440 }
441 
442 static void ixgbe_clean_xdp_tx_buffer(struct ixgbe_ring *tx_ring,
443 				      struct ixgbe_tx_buffer *tx_bi)
444 {
445 	xdp_return_frame(tx_bi->xdpf);
446 	dma_unmap_single(tx_ring->dev,
447 			 dma_unmap_addr(tx_bi, dma),
448 			 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
449 	dma_unmap_len_set(tx_bi, len, 0);
450 }
451 
452 bool ixgbe_clean_xdp_tx_irq(struct ixgbe_q_vector *q_vector,
453 			    struct ixgbe_ring *tx_ring, int napi_budget)
454 {
455 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
456 	unsigned int total_packets = 0, total_bytes = 0;
457 	struct xsk_buff_pool *pool = tx_ring->xsk_pool;
458 	union ixgbe_adv_tx_desc *tx_desc;
459 	struct ixgbe_tx_buffer *tx_bi;
460 	u32 xsk_frames = 0;
461 
462 	tx_bi = &tx_ring->tx_buffer_info[ntc];
463 	tx_desc = IXGBE_TX_DESC(tx_ring, ntc);
464 
465 	while (ntc != ntu) {
466 		if (!(tx_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
467 			break;
468 
469 		total_bytes += tx_bi->bytecount;
470 		total_packets += tx_bi->gso_segs;
471 
472 		if (tx_bi->xdpf)
473 			ixgbe_clean_xdp_tx_buffer(tx_ring, tx_bi);
474 		else
475 			xsk_frames++;
476 
477 		tx_bi->xdpf = NULL;
478 
479 		tx_bi++;
480 		tx_desc++;
481 		ntc++;
482 		if (unlikely(ntc == tx_ring->count)) {
483 			ntc = 0;
484 			tx_bi = tx_ring->tx_buffer_info;
485 			tx_desc = IXGBE_TX_DESC(tx_ring, 0);
486 		}
487 
488 		/* issue prefetch for next Tx descriptor */
489 		prefetch(tx_desc);
490 	}
491 
492 	tx_ring->next_to_clean = ntc;
493 
494 	u64_stats_update_begin(&tx_ring->syncp);
495 	tx_ring->stats.bytes += total_bytes;
496 	tx_ring->stats.packets += total_packets;
497 	u64_stats_update_end(&tx_ring->syncp);
498 	q_vector->tx.total_bytes += total_bytes;
499 	q_vector->tx.total_packets += total_packets;
500 
501 	if (xsk_frames)
502 		xsk_tx_completed(pool, xsk_frames);
503 
504 	if (xsk_uses_need_wakeup(pool))
505 		xsk_set_tx_need_wakeup(pool);
506 
507 	return ixgbe_xmit_zc(tx_ring, q_vector->tx.work_limit);
508 }
509 
510 int ixgbe_xsk_wakeup(struct net_device *dev, u32 qid, u32 flags)
511 {
512 	struct ixgbe_adapter *adapter = netdev_priv(dev);
513 	struct ixgbe_ring *ring;
514 
515 	if (test_bit(__IXGBE_DOWN, &adapter->state))
516 		return -ENETDOWN;
517 
518 	if (!READ_ONCE(adapter->xdp_prog))
519 		return -ENXIO;
520 
521 	if (qid >= adapter->num_xdp_queues)
522 		return -ENXIO;
523 
524 	ring = adapter->xdp_ring[qid];
525 
526 	if (test_bit(__IXGBE_TX_DISABLED, &ring->state))
527 		return -ENETDOWN;
528 
529 	if (!ring->xsk_pool)
530 		return -ENXIO;
531 
532 	if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi)) {
533 		u64 eics = BIT_ULL(ring->q_vector->v_idx);
534 
535 		ixgbe_irq_rearm_queues(adapter, eics);
536 	}
537 
538 	return 0;
539 }
540 
541 void ixgbe_xsk_clean_tx_ring(struct ixgbe_ring *tx_ring)
542 {
543 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
544 	struct xsk_buff_pool *pool = tx_ring->xsk_pool;
545 	struct ixgbe_tx_buffer *tx_bi;
546 	u32 xsk_frames = 0;
547 
548 	while (ntc != ntu) {
549 		tx_bi = &tx_ring->tx_buffer_info[ntc];
550 
551 		if (tx_bi->xdpf)
552 			ixgbe_clean_xdp_tx_buffer(tx_ring, tx_bi);
553 		else
554 			xsk_frames++;
555 
556 		tx_bi->xdpf = NULL;
557 
558 		ntc++;
559 		if (ntc == tx_ring->count)
560 			ntc = 0;
561 	}
562 
563 	if (xsk_frames)
564 		xsk_tx_completed(pool, xsk_frames);
565 }
566