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
i40e_clear_rx_bi_zc(struct i40e_ring * rx_ring)13 void 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
i40e_rx_bi(struct i40e_ring * rx_ring,u32 idx)19 static 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 */
i40e_realloc_rx_xdp_bi(struct i40e_ring * rx_ring,bool pool_present)33 static 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 */
i40e_realloc_rx_bi_zc(struct i40e_vsi * vsi,bool zc)63 int 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 **/
i40e_xsk_pool_enable(struct i40e_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)85 static 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 **/
i40e_xsk_pool_disable(struct i40e_vsi * vsi,u16 qid)141 static 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 **/
i40e_xsk_pool_setup(struct i40e_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)186 int 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 **/
i40e_run_xdp_zc(struct i40e_ring * rx_ring,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)201 static 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;
238 out_failure:
239 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
240 }
241 return result;
242 }
243
i40e_alloc_rx_buffers_zc(struct i40e_ring * rx_ring,u16 count)244 bool 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 **/
i40e_construct_skb_zc(struct i40e_ring * rx_ring,struct xdp_buff * xdp)292 static 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 skb_shared_info *sinfo = NULL;
298 struct sk_buff *skb;
299 u32 nr_frags = 0;
300
301 if (unlikely(xdp_buff_has_frags(xdp))) {
302 sinfo = xdp_get_shared_info_from_buff(xdp);
303 nr_frags = sinfo->nr_frags;
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 goto out;
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 if (likely(!xdp_buff_has_frags(xdp)))
322 goto out;
323
324 for (int i = 0; i < nr_frags; i++) {
325 struct skb_shared_info *skinfo = skb_shinfo(skb);
326 skb_frag_t *frag = &sinfo->frags[i];
327 struct page *page;
328 void *addr;
329
330 page = dev_alloc_page();
331 if (!page) {
332 dev_kfree_skb(skb);
333 return NULL;
334 }
335 addr = page_to_virt(page);
336
337 memcpy(addr, skb_frag_page(frag), skb_frag_size(frag));
338
339 __skb_fill_page_desc_noacc(skinfo, skinfo->nr_frags++,
340 addr, 0, skb_frag_size(frag));
341 }
342
343 out:
344 xsk_buff_free(xdp);
345 return skb;
346 }
347
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)348 static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring,
349 struct xdp_buff *xdp_buff,
350 union i40e_rx_desc *rx_desc,
351 unsigned int *rx_packets,
352 unsigned int *rx_bytes,
353 unsigned int xdp_res,
354 bool *failure)
355 {
356 struct sk_buff *skb;
357
358 *rx_packets = 1;
359 *rx_bytes = xdp_get_buff_len(xdp_buff);
360
361 if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX)
362 return;
363
364 if (xdp_res == I40E_XDP_EXIT) {
365 *failure = true;
366 return;
367 }
368
369 if (xdp_res == I40E_XDP_CONSUMED) {
370 xsk_buff_free(xdp_buff);
371 return;
372 }
373 if (xdp_res == I40E_XDP_PASS) {
374 /* NB! We are not checking for errors using
375 * i40e_test_staterr with
376 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
377 * SBP is *not* set in PRT_SBPVSI (default not set).
378 */
379 skb = i40e_construct_skb_zc(rx_ring, xdp_buff);
380 if (!skb) {
381 rx_ring->rx_stats.alloc_buff_failed++;
382 *rx_packets = 0;
383 *rx_bytes = 0;
384 return;
385 }
386
387 if (eth_skb_pad(skb)) {
388 *rx_packets = 0;
389 *rx_bytes = 0;
390 return;
391 }
392
393 i40e_process_skb_fields(rx_ring, rx_desc, skb);
394 napi_gro_receive(&rx_ring->q_vector->napi, skb);
395 return;
396 }
397
398 /* Should never get here, as all valid cases have been handled already.
399 */
400 WARN_ON_ONCE(1);
401 }
402
403 static int
i40e_add_xsk_frag(struct i40e_ring * rx_ring,struct xdp_buff * first,struct xdp_buff * xdp,const unsigned int size)404 i40e_add_xsk_frag(struct i40e_ring *rx_ring, struct xdp_buff *first,
405 struct xdp_buff *xdp, const unsigned int size)
406 {
407 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(first);
408
409 if (!xdp_buff_has_frags(first)) {
410 sinfo->nr_frags = 0;
411 sinfo->xdp_frags_size = 0;
412 xdp_buff_set_frags_flag(first);
413 }
414
415 if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS)) {
416 xsk_buff_free(first);
417 return -ENOMEM;
418 }
419
420 __skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++,
421 virt_to_page(xdp->data_hard_start),
422 XDP_PACKET_HEADROOM, size);
423 sinfo->xdp_frags_size += size;
424 xsk_buff_add_frag(xdp);
425
426 return 0;
427 }
428
429 /**
430 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
431 * @rx_ring: Rx ring
432 * @budget: NAPI budget
433 *
434 * Returns amount of work completed
435 **/
i40e_clean_rx_irq_zc(struct i40e_ring * rx_ring,int budget)436 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
437 {
438 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
439 u16 next_to_process = rx_ring->next_to_process;
440 u16 next_to_clean = rx_ring->next_to_clean;
441 unsigned int xdp_res, xdp_xmit = 0;
442 struct xdp_buff *first = NULL;
443 u32 count = rx_ring->count;
444 struct bpf_prog *xdp_prog;
445 u32 entries_to_alloc;
446 bool failure = false;
447
448 if (next_to_process != next_to_clean)
449 first = *i40e_rx_bi(rx_ring, next_to_clean);
450
451 /* NB! xdp_prog will always be !NULL, due to the fact that
452 * this path is enabled by setting an XDP program.
453 */
454 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
455
456 while (likely(total_rx_packets < (unsigned int)budget)) {
457 union i40e_rx_desc *rx_desc;
458 unsigned int rx_packets;
459 unsigned int rx_bytes;
460 struct xdp_buff *bi;
461 unsigned int size;
462 u64 qword;
463
464 rx_desc = I40E_RX_DESC(rx_ring, next_to_process);
465 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
466
467 /* This memory barrier is needed to keep us from reading
468 * any other fields out of the rx_desc until we have
469 * verified the descriptor has been written back.
470 */
471 dma_rmb();
472
473 if (i40e_rx_is_programming_status(qword)) {
474 i40e_clean_programming_status(rx_ring,
475 rx_desc->raw.qword[0],
476 qword);
477 bi = *i40e_rx_bi(rx_ring, next_to_process);
478 xsk_buff_free(bi);
479 if (++next_to_process == count)
480 next_to_process = 0;
481 continue;
482 }
483
484 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
485 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
486 if (!size)
487 break;
488
489 bi = *i40e_rx_bi(rx_ring, next_to_process);
490 xsk_buff_set_size(bi, size);
491 xsk_buff_dma_sync_for_cpu(bi, rx_ring->xsk_pool);
492
493 if (!first)
494 first = bi;
495 else if (i40e_add_xsk_frag(rx_ring, first, bi, size))
496 break;
497
498 if (++next_to_process == count)
499 next_to_process = 0;
500
501 if (i40e_is_non_eop(rx_ring, rx_desc))
502 continue;
503
504 xdp_res = i40e_run_xdp_zc(rx_ring, first, xdp_prog);
505 i40e_handle_xdp_result_zc(rx_ring, first, rx_desc, &rx_packets,
506 &rx_bytes, xdp_res, &failure);
507 next_to_clean = next_to_process;
508 if (failure)
509 break;
510 total_rx_packets += rx_packets;
511 total_rx_bytes += rx_bytes;
512 xdp_xmit |= xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR);
513 first = NULL;
514 }
515
516 rx_ring->next_to_clean = next_to_clean;
517 rx_ring->next_to_process = next_to_process;
518
519 entries_to_alloc = I40E_DESC_UNUSED(rx_ring);
520 if (entries_to_alloc >= I40E_RX_BUFFER_WRITE)
521 failure |= !i40e_alloc_rx_buffers_zc(rx_ring, entries_to_alloc);
522
523 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
524 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
525
526 if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
527 if (failure || next_to_clean == rx_ring->next_to_use)
528 xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
529 else
530 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
531
532 return (int)total_rx_packets;
533 }
534 return failure ? budget : (int)total_rx_packets;
535 }
536
i40e_xmit_pkt(struct i40e_ring * xdp_ring,struct xdp_desc * desc,unsigned int * total_bytes)537 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
538 unsigned int *total_bytes)
539 {
540 u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(desc);
541 struct i40e_tx_desc *tx_desc;
542 dma_addr_t dma;
543
544 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
545 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
546
547 tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
548 tx_desc->buffer_addr = cpu_to_le64(dma);
549 tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc->len, 0);
550
551 *total_bytes += desc->len;
552 }
553
i40e_xmit_pkt_batch(struct i40e_ring * xdp_ring,struct xdp_desc * desc,unsigned int * total_bytes)554 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
555 unsigned int *total_bytes)
556 {
557 u16 ntu = xdp_ring->next_to_use;
558 struct i40e_tx_desc *tx_desc;
559 dma_addr_t dma;
560 u32 i;
561
562 loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
563 u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(&desc[i]);
564
565 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr);
566 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len);
567
568 tx_desc = I40E_TX_DESC(xdp_ring, ntu++);
569 tx_desc->buffer_addr = cpu_to_le64(dma);
570 tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc[i].len, 0);
571
572 *total_bytes += desc[i].len;
573 }
574
575 xdp_ring->next_to_use = ntu;
576 }
577
i40e_fill_tx_hw_ring(struct i40e_ring * xdp_ring,struct xdp_desc * descs,u32 nb_pkts,unsigned int * total_bytes)578 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
579 unsigned int *total_bytes)
580 {
581 u32 batched, leftover, i;
582
583 batched = nb_pkts & ~(PKTS_PER_BATCH - 1);
584 leftover = nb_pkts & (PKTS_PER_BATCH - 1);
585 for (i = 0; i < batched; i += PKTS_PER_BATCH)
586 i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
587 for (i = batched; i < batched + leftover; i++)
588 i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes);
589 }
590
i40e_set_rs_bit(struct i40e_ring * xdp_ring)591 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
592 {
593 u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1;
594 struct i40e_tx_desc *tx_desc;
595
596 tx_desc = I40E_TX_DESC(xdp_ring, ntu);
597 tx_desc->cmd_type_offset_bsz |= cpu_to_le64(I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT);
598 }
599
600 /**
601 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
602 * @xdp_ring: XDP Tx ring
603 * @budget: NAPI budget
604 *
605 * Returns true if the work is finished.
606 **/
i40e_xmit_zc(struct i40e_ring * xdp_ring,unsigned int budget)607 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
608 {
609 struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs;
610 u32 nb_pkts, nb_processed = 0;
611 unsigned int total_bytes = 0;
612
613 nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget);
614 if (!nb_pkts)
615 return true;
616
617 if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
618 nb_processed = xdp_ring->count - xdp_ring->next_to_use;
619 i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
620 xdp_ring->next_to_use = 0;
621 }
622
623 i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
624 &total_bytes);
625
626 /* Request an interrupt for the last frame and bump tail ptr. */
627 i40e_set_rs_bit(xdp_ring);
628 i40e_xdp_ring_update_tail(xdp_ring);
629
630 i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
631
632 return nb_pkts < budget;
633 }
634
635 /**
636 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
637 * @tx_ring: XDP Tx ring
638 * @tx_bi: Tx buffer info to clean
639 **/
i40e_clean_xdp_tx_buffer(struct i40e_ring * tx_ring,struct i40e_tx_buffer * tx_bi)640 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
641 struct i40e_tx_buffer *tx_bi)
642 {
643 xdp_return_frame(tx_bi->xdpf);
644 tx_ring->xdp_tx_active--;
645 dma_unmap_single(tx_ring->dev,
646 dma_unmap_addr(tx_bi, dma),
647 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
648 dma_unmap_len_set(tx_bi, len, 0);
649 }
650
651 /**
652 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
653 * @vsi: Current VSI
654 * @tx_ring: XDP Tx ring
655 *
656 * Returns true if cleanup/transmission is done.
657 **/
i40e_clean_xdp_tx_irq(struct i40e_vsi * vsi,struct i40e_ring * tx_ring)658 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
659 {
660 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
661 u32 i, completed_frames, xsk_frames = 0;
662 u32 head_idx = i40e_get_head(tx_ring);
663 struct i40e_tx_buffer *tx_bi;
664 unsigned int ntc;
665
666 if (head_idx < tx_ring->next_to_clean)
667 head_idx += tx_ring->count;
668 completed_frames = head_idx - tx_ring->next_to_clean;
669
670 if (completed_frames == 0)
671 goto out_xmit;
672
673 if (likely(!tx_ring->xdp_tx_active)) {
674 xsk_frames = completed_frames;
675 goto skip;
676 }
677
678 ntc = tx_ring->next_to_clean;
679
680 for (i = 0; i < completed_frames; i++) {
681 tx_bi = &tx_ring->tx_bi[ntc];
682
683 if (tx_bi->xdpf) {
684 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
685 tx_bi->xdpf = NULL;
686 } else {
687 xsk_frames++;
688 }
689
690 if (++ntc >= tx_ring->count)
691 ntc = 0;
692 }
693
694 skip:
695 tx_ring->next_to_clean += completed_frames;
696 if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
697 tx_ring->next_to_clean -= tx_ring->count;
698
699 if (xsk_frames)
700 xsk_tx_completed(bp, xsk_frames);
701
702 i40e_arm_wb(tx_ring, vsi, completed_frames);
703
704 out_xmit:
705 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
706 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
707
708 return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
709 }
710
711 /**
712 * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
713 * @dev: the netdevice
714 * @queue_id: queue id to wake up
715 * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
716 *
717 * Returns <0 for errors, 0 otherwise.
718 **/
i40e_xsk_wakeup(struct net_device * dev,u32 queue_id,u32 flags)719 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
720 {
721 struct i40e_netdev_priv *np = netdev_priv(dev);
722 struct i40e_vsi *vsi = np->vsi;
723 struct i40e_pf *pf = vsi->back;
724 struct i40e_ring *ring;
725
726 if (test_bit(__I40E_CONFIG_BUSY, pf->state))
727 return -EAGAIN;
728
729 if (test_bit(__I40E_VSI_DOWN, vsi->state))
730 return -ENETDOWN;
731
732 if (!i40e_enabled_xdp_vsi(vsi))
733 return -EINVAL;
734
735 if (queue_id >= vsi->num_queue_pairs)
736 return -EINVAL;
737
738 if (!vsi->xdp_rings[queue_id]->xsk_pool)
739 return -EINVAL;
740
741 ring = vsi->xdp_rings[queue_id];
742
743 /* The idea here is that if NAPI is running, mark a miss, so
744 * it will run again. If not, trigger an interrupt and
745 * schedule the NAPI from interrupt context. If NAPI would be
746 * scheduled here, the interrupt affinity would not be
747 * honored.
748 */
749 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
750 i40e_force_wb(vsi, ring->q_vector);
751
752 return 0;
753 }
754
i40e_xsk_clean_rx_ring(struct i40e_ring * rx_ring)755 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
756 {
757 u16 ntc = rx_ring->next_to_clean;
758 u16 ntu = rx_ring->next_to_use;
759
760 while (ntc != ntu) {
761 struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc);
762
763 xsk_buff_free(rx_bi);
764 ntc++;
765 if (ntc >= rx_ring->count)
766 ntc = 0;
767 }
768 }
769
770 /**
771 * i40e_xsk_clean_tx_ring - Clean the XDP Tx ring on shutdown
772 * @tx_ring: XDP Tx ring
773 **/
i40e_xsk_clean_tx_ring(struct i40e_ring * tx_ring)774 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
775 {
776 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
777 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
778 struct i40e_tx_buffer *tx_bi;
779 u32 xsk_frames = 0;
780
781 while (ntc != ntu) {
782 tx_bi = &tx_ring->tx_bi[ntc];
783
784 if (tx_bi->xdpf)
785 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
786 else
787 xsk_frames++;
788
789 tx_bi->xdpf = NULL;
790
791 ntc++;
792 if (ntc >= tx_ring->count)
793 ntc = 0;
794 }
795
796 if (xsk_frames)
797 xsk_tx_completed(bp, xsk_frames);
798 }
799
800 /**
801 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
802 * buffer pool attached
803 * @vsi: vsi
804 *
805 * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
806 **/
i40e_xsk_any_rx_ring_enabled(struct i40e_vsi * vsi)807 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
808 {
809 struct net_device *netdev = vsi->netdev;
810 int i;
811
812 for (i = 0; i < vsi->num_queue_pairs; i++) {
813 if (xsk_get_pool_from_qid(netdev, i))
814 return true;
815 }
816
817 return false;
818 }
819