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