1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
4 #include <linux/bpf_trace.h>
5 #include <linux/stringify.h>
6 #include <net/xdp_sock_drv.h>
10 #include "i40e_txrx_common.h"
13 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
15 memset(rx_ring->rx_bi_zc, 0,
16 sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
19 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
21 return &rx_ring->rx_bi_zc[idx];
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
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
33 static int i40e_realloc_rx_xdp_bi(struct i40e_ring *rx_ring, bool pool_present)
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);
43 kfree(rx_ring->rx_bi);
44 rx_ring->rx_bi = NULL;
45 rx_ring->rx_bi_zc = sw_ring;
47 kfree(rx_ring->rx_bi_zc);
48 rx_ring->rx_bi_zc = NULL;
49 rx_ring->rx_bi = sw_ring;
55 * i40e_realloc_rx_bi_zc - reallocate rx SW rings
57 * @zc: is zero copy set
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
63 int i40e_realloc_rx_bi_zc(struct i40e_vsi *vsi, bool zc)
65 struct i40e_ring *rx_ring;
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))
77 * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
81 * @qid: Rx ring to associate buffer pool with
83 * Returns 0 on success, <0 on failure
85 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
86 struct xsk_buff_pool *pool,
89 struct net_device *netdev = vsi->netdev;
93 if (vsi->type != I40E_VSI_MAIN)
96 if (qid >= vsi->num_queue_pairs)
99 if (qid >= netdev->real_num_rx_queues ||
100 qid >= netdev->real_num_tx_queues)
103 err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
107 set_bit(qid, vsi->af_xdp_zc_qps);
109 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
112 err = i40e_queue_pair_disable(vsi, qid);
116 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], true);
120 err = i40e_queue_pair_enable(vsi, qid);
124 /* Kick start the NAPI context so that receiving will start */
125 err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
134 * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
137 * @qid: Rx ring to associate buffer pool with
139 * Returns 0 on success, <0 on failure
141 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
143 struct net_device *netdev = vsi->netdev;
144 struct xsk_buff_pool *pool;
148 pool = xsk_get_pool_from_qid(netdev, qid);
152 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
155 err = i40e_queue_pair_disable(vsi, qid);
160 clear_bit(qid, vsi->af_xdp_zc_qps);
161 xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
164 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], false);
167 err = i40e_queue_pair_enable(vsi, qid);
176 * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
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
182 * This function enables or disables a buffer pool to a certain ring.
184 * Returns 0 on success, <0 on failure
186 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
189 return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
190 i40e_xsk_pool_disable(vsi, qid);
194 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
196 * @xdp: xdp_buff used as input to the XDP program
197 * @xdp_prog: XDP program to run
199 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
201 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp,
202 struct bpf_prog *xdp_prog)
204 int err, result = I40E_XDP_PASS;
205 struct i40e_ring *xdp_ring;
208 act = bpf_prog_run_xdp(xdp_prog, xdp);
210 if (likely(act == XDP_REDIRECT)) {
211 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
213 return I40E_XDP_REDIR;
214 if (xsk_uses_need_wakeup(rx_ring->xsk_pool) && err == -ENOBUFS)
215 result = I40E_XDP_EXIT;
217 result = I40E_XDP_CONSUMED;
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)
231 result = I40E_XDP_CONSUMED;
234 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
237 result = I40E_XDP_CONSUMED;
239 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
244 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
246 u16 ntu = rx_ring->next_to_use;
247 union i40e_rx_desc *rx_desc;
248 struct xdp_buff **xdp;
252 rx_desc = I40E_RX_DESC(rx_ring, ntu);
253 xdp = i40e_rx_bi(rx_ring, ntu);
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);
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;
271 if (ntu == rx_ring->count) {
272 rx_desc = I40E_RX_DESC(rx_ring, 0);
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);
280 return count == nb_buffs;
284 * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
288 * This functions allocates a new skb from a zero-copy Rx buffer.
290 * Returns the skb, or NULL on failure.
292 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
293 struct xdp_buff *xdp)
295 unsigned int totalsize = xdp->data_end - xdp->data_meta;
296 unsigned int metasize = xdp->data - xdp->data_meta;
299 net_prefetch(xdp->data_meta);
301 /* allocate a skb to store the frags */
302 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
303 GFP_ATOMIC | __GFP_NOWARN);
307 memcpy(__skb_put(skb, totalsize), xdp->data_meta,
308 ALIGN(totalsize, sizeof(long)));
311 skb_metadata_set(skb, metasize);
312 __skb_pull(skb, metasize);
320 static 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,
326 unsigned int xdp_res,
334 if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX)
337 if (xdp_res == I40E_XDP_EXIT) {
342 if (xdp_res == I40E_XDP_CONSUMED) {
343 xsk_buff_free(xdp_buff);
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).
352 skb = i40e_construct_skb_zc(rx_ring, xdp_buff);
354 rx_ring->rx_stats.alloc_buff_failed++;
360 if (eth_skb_pad(skb)) {
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);
372 /* Should never get here, as all valid cases have been handled already.
378 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
380 * @budget: NAPI budget
382 * Returns amount of work completed
384 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
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;
394 /* NB! xdp_prog will always be !NULL, due to the fact that
395 * this path is enabled by setting an XDP program.
397 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
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;
407 rx_desc = I40E_RX_DESC(rx_ring, next_to_clean);
408 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
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.
416 if (i40e_rx_is_programming_status(qword)) {
417 i40e_clean_programming_status(rx_ring,
418 rx_desc->raw.qword[0],
420 bi = *i40e_rx_bi(rx_ring, next_to_clean);
422 next_to_clean = (next_to_clean + 1) & count_mask;
426 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
427 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
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);
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);
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;
446 rx_ring->next_to_clean = next_to_clean;
447 cleaned_count = (next_to_clean - rx_ring->next_to_use - 1) & count_mask;
449 if (cleaned_count >= I40E_RX_BUFFER_WRITE)
450 failure |= !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
452 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
453 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
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);
459 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
461 return (int)total_rx_packets;
463 return failure ? budget : (int)total_rx_packets;
466 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
467 unsigned int *total_bytes)
469 struct i40e_tx_desc *tx_desc;
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);
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,
480 *total_bytes += desc->len;
483 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
484 unsigned int *total_bytes)
486 u16 ntu = xdp_ring->next_to_use;
487 struct i40e_tx_desc *tx_desc;
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);
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,
501 *total_bytes += desc[i].len;
504 xdp_ring->next_to_use = ntu;
507 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
508 unsigned int *total_bytes)
510 u32 batched, leftover, i;
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);
520 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
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;
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);
530 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
531 * @xdp_ring: XDP Tx ring
532 * @budget: NAPI budget
534 * Returns true if the work is finished.
536 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
538 struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs;
539 u32 nb_pkts, nb_processed = 0;
540 unsigned int total_bytes = 0;
542 nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget);
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;
552 i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
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);
559 i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
561 return nb_pkts < budget;
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
569 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
570 struct i40e_tx_buffer *tx_bi)
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);
581 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
583 * @tx_ring: XDP Tx ring
585 * Returns true if cleanup/transmission is done.
587 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
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;
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;
599 if (completed_frames == 0)
602 if (likely(!tx_ring->xdp_tx_active)) {
603 xsk_frames = completed_frames;
607 ntc = tx_ring->next_to_clean;
609 for (i = 0; i < completed_frames; i++) {
610 tx_bi = &tx_ring->tx_bi[ntc];
613 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
619 if (++ntc >= tx_ring->count)
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;
629 xsk_tx_completed(bp, xsk_frames);
631 i40e_arm_wb(tx_ring, vsi, completed_frames);
634 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
635 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
637 return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
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.
646 * Returns <0 for errors, 0 otherwise.
648 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
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;
655 if (test_bit(__I40E_CONFIG_BUSY, pf->state))
658 if (test_bit(__I40E_VSI_DOWN, vsi->state))
661 if (!i40e_enabled_xdp_vsi(vsi))
664 if (queue_id >= vsi->num_queue_pairs)
667 if (!vsi->xdp_rings[queue_id]->xsk_pool)
670 ring = vsi->xdp_rings[queue_id];
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
678 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
679 i40e_force_wb(vsi, ring->q_vector);
684 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
686 u16 count_mask = rx_ring->count - 1;
687 u16 ntc = rx_ring->next_to_clean;
688 u16 ntu = rx_ring->next_to_use;
690 for ( ; ntc != ntu; ntc = (ntc + 1) & count_mask) {
691 struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc);
693 xsk_buff_free(rx_bi);
698 * i40e_xsk_clean_tx_ring - Clean the XDP Tx ring on shutdown
699 * @tx_ring: XDP Tx ring
701 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
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;
709 tx_bi = &tx_ring->tx_bi[ntc];
712 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
719 if (ntc >= tx_ring->count)
724 xsk_tx_completed(bp, xsk_frames);
728 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
729 * buffer pool attached
732 * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
734 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
736 struct net_device *netdev = vsi->netdev;
739 for (i = 0; i < vsi->num_queue_pairs; i++) {
740 if (xsk_get_pool_from_qid(netdev, i))