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