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