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