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 "i40e.h"
9 #include "i40e_txrx_common.h"
10 #include "i40e_xsk.h"
11 
12 int i40e_alloc_rx_bi_zc(struct i40e_ring *rx_ring)
13 {
14 	unsigned long sz = sizeof(*rx_ring->rx_bi_zc) * rx_ring->count;
15 
16 	rx_ring->rx_bi_zc = kzalloc(sz, GFP_KERNEL);
17 	return rx_ring->rx_bi_zc ? 0 : -ENOMEM;
18 }
19 
20 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
21 {
22 	memset(rx_ring->rx_bi_zc, 0,
23 	       sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
24 }
25 
26 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
27 {
28 	return &rx_ring->rx_bi_zc[idx];
29 }
30 
31 /**
32  * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
33  * certain ring/qid
34  * @vsi: Current VSI
35  * @pool: buffer pool
36  * @qid: Rx ring to associate buffer pool with
37  *
38  * Returns 0 on success, <0 on failure
39  **/
40 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
41 				struct xsk_buff_pool *pool,
42 				u16 qid)
43 {
44 	struct net_device *netdev = vsi->netdev;
45 	bool if_running;
46 	int err;
47 
48 	if (vsi->type != I40E_VSI_MAIN)
49 		return -EINVAL;
50 
51 	if (qid >= vsi->num_queue_pairs)
52 		return -EINVAL;
53 
54 	if (qid >= netdev->real_num_rx_queues ||
55 	    qid >= netdev->real_num_tx_queues)
56 		return -EINVAL;
57 
58 	err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
59 	if (err)
60 		return err;
61 
62 	set_bit(qid, vsi->af_xdp_zc_qps);
63 
64 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
65 
66 	if (if_running) {
67 		err = i40e_queue_pair_disable(vsi, qid);
68 		if (err)
69 			return err;
70 
71 		err = i40e_queue_pair_enable(vsi, qid);
72 		if (err)
73 			return err;
74 
75 		/* Kick start the NAPI context so that receiving will start */
76 		err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
77 		if (err)
78 			return err;
79 	}
80 
81 	return 0;
82 }
83 
84 /**
85  * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
86  * certain ring/qid
87  * @vsi: Current VSI
88  * @qid: Rx ring to associate buffer pool with
89  *
90  * Returns 0 on success, <0 on failure
91  **/
92 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
93 {
94 	struct net_device *netdev = vsi->netdev;
95 	struct xsk_buff_pool *pool;
96 	bool if_running;
97 	int err;
98 
99 	pool = xsk_get_pool_from_qid(netdev, qid);
100 	if (!pool)
101 		return -EINVAL;
102 
103 	if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
104 
105 	if (if_running) {
106 		err = i40e_queue_pair_disable(vsi, qid);
107 		if (err)
108 			return err;
109 	}
110 
111 	clear_bit(qid, vsi->af_xdp_zc_qps);
112 	xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
113 
114 	if (if_running) {
115 		err = i40e_queue_pair_enable(vsi, qid);
116 		if (err)
117 			return err;
118 	}
119 
120 	return 0;
121 }
122 
123 /**
124  * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
125  * a ring/qid
126  * @vsi: Current VSI
127  * @pool: Buffer pool to enable/associate to a ring, or NULL to disable
128  * @qid: Rx ring to (dis)associate buffer pool (from)to
129  *
130  * This function enables or disables a buffer pool to a certain ring.
131  *
132  * Returns 0 on success, <0 on failure
133  **/
134 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
135 			u16 qid)
136 {
137 	return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
138 		i40e_xsk_pool_disable(vsi, qid);
139 }
140 
141 /**
142  * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
143  * @rx_ring: Rx ring
144  * @xdp: xdp_buff used as input to the XDP program
145  *
146  * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
147  **/
148 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp)
149 {
150 	int err, result = I40E_XDP_PASS;
151 	struct i40e_ring *xdp_ring;
152 	struct bpf_prog *xdp_prog;
153 	u32 act;
154 
155 	rcu_read_lock();
156 	/* NB! xdp_prog will always be !NULL, due to the fact that
157 	 * this path is enabled by setting an XDP program.
158 	 */
159 	xdp_prog = READ_ONCE(rx_ring->xdp_prog);
160 	act = bpf_prog_run_xdp(xdp_prog, xdp);
161 
162 	switch (act) {
163 	case XDP_PASS:
164 		break;
165 	case XDP_TX:
166 		xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
167 		result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
168 		break;
169 	case XDP_REDIRECT:
170 		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
171 		result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
172 		break;
173 	default:
174 		bpf_warn_invalid_xdp_action(act);
175 		fallthrough;
176 	case XDP_ABORTED:
177 		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
178 		fallthrough; /* handle aborts by dropping packet */
179 	case XDP_DROP:
180 		result = I40E_XDP_CONSUMED;
181 		break;
182 	}
183 	rcu_read_unlock();
184 	return result;
185 }
186 
187 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
188 {
189 	u16 ntu = rx_ring->next_to_use;
190 	union i40e_rx_desc *rx_desc;
191 	struct xdp_buff **bi, *xdp;
192 	dma_addr_t dma;
193 	bool ok = true;
194 
195 	rx_desc = I40E_RX_DESC(rx_ring, ntu);
196 	bi = i40e_rx_bi(rx_ring, ntu);
197 	do {
198 		xdp = xsk_buff_alloc(rx_ring->xsk_pool);
199 		if (!xdp) {
200 			ok = false;
201 			goto no_buffers;
202 		}
203 		*bi = xdp;
204 		dma = xsk_buff_xdp_get_dma(xdp);
205 		rx_desc->read.pkt_addr = cpu_to_le64(dma);
206 		rx_desc->read.hdr_addr = 0;
207 
208 		rx_desc++;
209 		bi++;
210 		ntu++;
211 
212 		if (unlikely(ntu == rx_ring->count)) {
213 			rx_desc = I40E_RX_DESC(rx_ring, 0);
214 			bi = i40e_rx_bi(rx_ring, 0);
215 			ntu = 0;
216 		}
217 
218 		count--;
219 	} while (count);
220 
221 no_buffers:
222 	if (rx_ring->next_to_use != ntu)
223 		i40e_release_rx_desc(rx_ring, ntu);
224 
225 	return ok;
226 }
227 
228 /**
229  * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
230  * @rx_ring: Rx ring
231  * @xdp: xdp_buff
232  *
233  * This functions allocates a new skb from a zero-copy Rx buffer.
234  *
235  * Returns the skb, or NULL on failure.
236  **/
237 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
238 					     struct xdp_buff *xdp)
239 {
240 	unsigned int metasize = xdp->data - xdp->data_meta;
241 	unsigned int datasize = xdp->data_end - xdp->data;
242 	struct sk_buff *skb;
243 
244 	/* allocate a skb to store the frags */
245 	skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
246 			       xdp->data_end - xdp->data_hard_start,
247 			       GFP_ATOMIC | __GFP_NOWARN);
248 	if (unlikely(!skb))
249 		return NULL;
250 
251 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
252 	memcpy(__skb_put(skb, datasize), xdp->data, datasize);
253 	if (metasize)
254 		skb_metadata_set(skb, metasize);
255 
256 	xsk_buff_free(xdp);
257 	return skb;
258 }
259 
260 /**
261  * i40e_inc_ntc: Advance the next_to_clean index
262  * @rx_ring: Rx ring
263  **/
264 static void i40e_inc_ntc(struct i40e_ring *rx_ring)
265 {
266 	u32 ntc = rx_ring->next_to_clean + 1;
267 
268 	ntc = (ntc < rx_ring->count) ? ntc : 0;
269 	rx_ring->next_to_clean = ntc;
270 }
271 
272 /**
273  * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
274  * @rx_ring: Rx ring
275  * @budget: NAPI budget
276  *
277  * Returns amount of work completed
278  **/
279 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
280 {
281 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
282 	u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
283 	unsigned int xdp_res, xdp_xmit = 0;
284 	struct sk_buff *skb;
285 	bool failure;
286 
287 	while (likely(total_rx_packets < (unsigned int)budget)) {
288 		union i40e_rx_desc *rx_desc;
289 		struct xdp_buff **bi;
290 		unsigned int size;
291 		u64 qword;
292 
293 		rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
294 		qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
295 
296 		/* This memory barrier is needed to keep us from reading
297 		 * any other fields out of the rx_desc until we have
298 		 * verified the descriptor has been written back.
299 		 */
300 		dma_rmb();
301 
302 		if (i40e_rx_is_programming_status(qword)) {
303 			i40e_clean_programming_status(rx_ring,
304 						      rx_desc->raw.qword[0],
305 						      qword);
306 			bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
307 			xsk_buff_free(*bi);
308 			*bi = NULL;
309 			cleaned_count++;
310 			i40e_inc_ntc(rx_ring);
311 			continue;
312 		}
313 
314 		bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
315 		size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
316 		       I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
317 		if (!size)
318 			break;
319 
320 		bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
321 		(*bi)->data_end = (*bi)->data + size;
322 		xsk_buff_dma_sync_for_cpu(*bi, rx_ring->xsk_pool);
323 
324 		xdp_res = i40e_run_xdp_zc(rx_ring, *bi);
325 		if (xdp_res) {
326 			if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR))
327 				xdp_xmit |= xdp_res;
328 			else
329 				xsk_buff_free(*bi);
330 
331 			*bi = NULL;
332 			total_rx_bytes += size;
333 			total_rx_packets++;
334 
335 			cleaned_count++;
336 			i40e_inc_ntc(rx_ring);
337 			continue;
338 		}
339 
340 		/* XDP_PASS path */
341 
342 		/* NB! We are not checking for errors using
343 		 * i40e_test_staterr with
344 		 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
345 		 * SBP is *not* set in PRT_SBPVSI (default not set).
346 		 */
347 		skb = i40e_construct_skb_zc(rx_ring, *bi);
348 		*bi = NULL;
349 		if (!skb) {
350 			rx_ring->rx_stats.alloc_buff_failed++;
351 			break;
352 		}
353 
354 		cleaned_count++;
355 		i40e_inc_ntc(rx_ring);
356 
357 		if (eth_skb_pad(skb))
358 			continue;
359 
360 		total_rx_bytes += skb->len;
361 		total_rx_packets++;
362 
363 		i40e_process_skb_fields(rx_ring, rx_desc, skb);
364 		napi_gro_receive(&rx_ring->q_vector->napi, skb);
365 	}
366 
367 	if (cleaned_count >= I40E_RX_BUFFER_WRITE)
368 		failure = !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
369 
370 	i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
371 	i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
372 
373 	if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
374 		if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
375 			xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
376 		else
377 			xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
378 
379 		return (int)total_rx_packets;
380 	}
381 	return failure ? budget : (int)total_rx_packets;
382 }
383 
384 /**
385  * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
386  * @xdp_ring: XDP Tx ring
387  * @budget: NAPI budget
388  *
389  * Returns true if the work is finished.
390  **/
391 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
392 {
393 	unsigned int sent_frames = 0, total_bytes = 0;
394 	struct i40e_tx_desc *tx_desc = NULL;
395 	struct i40e_tx_buffer *tx_bi;
396 	struct xdp_desc desc;
397 	dma_addr_t dma;
398 
399 	while (budget-- > 0) {
400 		if (!xsk_tx_peek_desc(xdp_ring->xsk_pool, &desc))
401 			break;
402 
403 		dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc.addr);
404 		xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma,
405 						 desc.len);
406 
407 		tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use];
408 		tx_bi->bytecount = desc.len;
409 
410 		tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use);
411 		tx_desc->buffer_addr = cpu_to_le64(dma);
412 		tx_desc->cmd_type_offset_bsz =
413 			build_ctob(I40E_TX_DESC_CMD_ICRC
414 				   | I40E_TX_DESC_CMD_EOP,
415 				   0, desc.len, 0);
416 
417 		sent_frames++;
418 		total_bytes += tx_bi->bytecount;
419 
420 		xdp_ring->next_to_use++;
421 		if (xdp_ring->next_to_use == xdp_ring->count)
422 			xdp_ring->next_to_use = 0;
423 	}
424 
425 	if (tx_desc) {
426 		/* Request an interrupt for the last frame and bump tail ptr. */
427 		tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS <<
428 						 I40E_TXD_QW1_CMD_SHIFT);
429 		i40e_xdp_ring_update_tail(xdp_ring);
430 
431 		xsk_tx_release(xdp_ring->xsk_pool);
432 		i40e_update_tx_stats(xdp_ring, sent_frames, total_bytes);
433 	}
434 
435 	return !!budget;
436 }
437 
438 /**
439  * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
440  * @tx_ring: XDP Tx ring
441  * @tx_bi: Tx buffer info to clean
442  **/
443 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
444 				     struct i40e_tx_buffer *tx_bi)
445 {
446 	xdp_return_frame(tx_bi->xdpf);
447 	tx_ring->xdp_tx_active--;
448 	dma_unmap_single(tx_ring->dev,
449 			 dma_unmap_addr(tx_bi, dma),
450 			 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
451 	dma_unmap_len_set(tx_bi, len, 0);
452 }
453 
454 /**
455  * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
456  * @vsi: Current VSI
457  * @tx_ring: XDP Tx ring
458  *
459  * Returns true if cleanup/tranmission is done.
460  **/
461 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
462 {
463 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
464 	u32 i, completed_frames, xsk_frames = 0;
465 	u32 head_idx = i40e_get_head(tx_ring);
466 	struct i40e_tx_buffer *tx_bi;
467 	unsigned int ntc;
468 
469 	if (head_idx < tx_ring->next_to_clean)
470 		head_idx += tx_ring->count;
471 	completed_frames = head_idx - tx_ring->next_to_clean;
472 
473 	if (completed_frames == 0)
474 		goto out_xmit;
475 
476 	if (likely(!tx_ring->xdp_tx_active)) {
477 		xsk_frames = completed_frames;
478 		goto skip;
479 	}
480 
481 	ntc = tx_ring->next_to_clean;
482 
483 	for (i = 0; i < completed_frames; i++) {
484 		tx_bi = &tx_ring->tx_bi[ntc];
485 
486 		if (tx_bi->xdpf) {
487 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
488 			tx_bi->xdpf = NULL;
489 		} else {
490 			xsk_frames++;
491 		}
492 
493 		if (++ntc >= tx_ring->count)
494 			ntc = 0;
495 	}
496 
497 skip:
498 	tx_ring->next_to_clean += completed_frames;
499 	if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
500 		tx_ring->next_to_clean -= tx_ring->count;
501 
502 	if (xsk_frames)
503 		xsk_tx_completed(bp, xsk_frames);
504 
505 	i40e_arm_wb(tx_ring, vsi, completed_frames);
506 
507 out_xmit:
508 	if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
509 		xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
510 
511 	return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
512 }
513 
514 /**
515  * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
516  * @dev: the netdevice
517  * @queue_id: queue id to wake up
518  * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
519  *
520  * Returns <0 for errors, 0 otherwise.
521  **/
522 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
523 {
524 	struct i40e_netdev_priv *np = netdev_priv(dev);
525 	struct i40e_vsi *vsi = np->vsi;
526 	struct i40e_pf *pf = vsi->back;
527 	struct i40e_ring *ring;
528 
529 	if (test_bit(__I40E_CONFIG_BUSY, pf->state))
530 		return -EAGAIN;
531 
532 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
533 		return -ENETDOWN;
534 
535 	if (!i40e_enabled_xdp_vsi(vsi))
536 		return -ENXIO;
537 
538 	if (queue_id >= vsi->num_queue_pairs)
539 		return -ENXIO;
540 
541 	if (!vsi->xdp_rings[queue_id]->xsk_pool)
542 		return -ENXIO;
543 
544 	ring = vsi->xdp_rings[queue_id];
545 
546 	/* The idea here is that if NAPI is running, mark a miss, so
547 	 * it will run again. If not, trigger an interrupt and
548 	 * schedule the NAPI from interrupt context. If NAPI would be
549 	 * scheduled here, the interrupt affinity would not be
550 	 * honored.
551 	 */
552 	if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
553 		i40e_force_wb(vsi, ring->q_vector);
554 
555 	return 0;
556 }
557 
558 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
559 {
560 	u16 i;
561 
562 	for (i = 0; i < rx_ring->count; i++) {
563 		struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, i);
564 
565 		if (!rx_bi)
566 			continue;
567 
568 		xsk_buff_free(rx_bi);
569 		rx_bi = NULL;
570 	}
571 }
572 
573 /**
574  * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown
575  * @tx_ring: XDP Tx ring
576  **/
577 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
578 {
579 	u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
580 	struct xsk_buff_pool *bp = tx_ring->xsk_pool;
581 	struct i40e_tx_buffer *tx_bi;
582 	u32 xsk_frames = 0;
583 
584 	while (ntc != ntu) {
585 		tx_bi = &tx_ring->tx_bi[ntc];
586 
587 		if (tx_bi->xdpf)
588 			i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
589 		else
590 			xsk_frames++;
591 
592 		tx_bi->xdpf = NULL;
593 
594 		ntc++;
595 		if (ntc >= tx_ring->count)
596 			ntc = 0;
597 	}
598 
599 	if (xsk_frames)
600 		xsk_tx_completed(bp, xsk_frames);
601 }
602 
603 /**
604  * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
605  * buffer pool attached
606  * @vsi: vsi
607  *
608  * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
609  **/
610 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
611 {
612 	struct net_device *netdev = vsi->netdev;
613 	int i;
614 
615 	for (i = 0; i < vsi->num_queue_pairs; i++) {
616 		if (xsk_get_pool_from_qid(netdev, i))
617 			return true;
618 	}
619 
620 	return false;
621 }
622