1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock.h>
9 #include "i40e_txrx_common.h"
13 * i40e_xsk_umem_dma_map - DMA maps all UMEM memory for the netdev
15 * @umem: UMEM to DMA map
17 * Returns 0 on success, <0 on failure
19 static int i40e_xsk_umem_dma_map(struct i40e_vsi *vsi, struct xdp_umem *umem)
21 struct i40e_pf *pf = vsi->back;
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))
33 umem->pages[i].dma = dma;
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;
49 * i40e_xsk_umem_dma_unmap - DMA unmaps all UMEM memory for the netdev
51 * @umem: UMEM to DMA map
53 static void i40e_xsk_umem_dma_unmap(struct i40e_vsi *vsi, struct xdp_umem *umem)
55 struct i40e_pf *pf = vsi->back;
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);
65 umem->pages[i].dma = 0;
70 * i40e_xsk_umem_enable - Enable/associate a UMEM to a certain ring/qid
73 * @qid: Rx ring to associate UMEM to
75 * Returns 0 on success, <0 on failure
77 static int i40e_xsk_umem_enable(struct i40e_vsi *vsi, struct xdp_umem *umem,
80 struct net_device *netdev = vsi->netdev;
81 struct xdp_umem_fq_reuse *reuseq;
85 if (vsi->type != I40E_VSI_MAIN)
88 if (qid >= vsi->num_queue_pairs)
91 if (qid >= netdev->real_num_rx_queues ||
92 qid >= netdev->real_num_tx_queues)
95 reuseq = xsk_reuseq_prepare(vsi->rx_rings[0]->count);
99 xsk_reuseq_free(xsk_reuseq_swap(umem, reuseq));
101 err = i40e_xsk_umem_dma_map(vsi, umem);
105 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
108 err = i40e_queue_pair_disable(vsi, qid);
112 err = i40e_queue_pair_enable(vsi, qid);
116 /* Kick start the NAPI context so that receiving will start */
117 err = i40e_xsk_async_xmit(vsi->netdev, qid);
126 * i40e_xsk_umem_disable - Disassociate a UMEM from a certain ring/qid
128 * @qid: Rx ring to associate UMEM to
130 * Returns 0 on success, <0 on failure
132 static int i40e_xsk_umem_disable(struct i40e_vsi *vsi, u16 qid)
134 struct net_device *netdev = vsi->netdev;
135 struct xdp_umem *umem;
139 umem = xdp_get_umem_from_qid(netdev, qid);
143 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
146 err = i40e_queue_pair_disable(vsi, qid);
151 i40e_xsk_umem_dma_unmap(vsi, umem);
154 err = i40e_queue_pair_enable(vsi, qid);
163 * i40e_xsk_umem_setup - Enable/disassociate a UMEM to/from a ring/qid
165 * @umem: UMEM to enable/associate to a ring, or NULL to disable
166 * @qid: Rx ring to (dis)associate UMEM (from)to
168 * This function enables or disables a UMEM to a certain ring.
170 * Returns 0 on success, <0 on failure
172 int i40e_xsk_umem_setup(struct i40e_vsi *vsi, struct xdp_umem *umem,
175 return umem ? i40e_xsk_umem_enable(vsi, umem, qid) :
176 i40e_xsk_umem_disable(vsi, qid);
180 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
182 * @xdp: xdp_buff used as input to the XDP program
184 * This function enables or disables a UMEM to a certain ring.
186 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
188 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp)
190 int err, result = I40E_XDP_PASS;
191 struct i40e_ring *xdp_ring;
192 struct bpf_prog *xdp_prog;
196 /* NB! xdp_prog will always be !NULL, due to the fact that
197 * this path is enabled by setting an XDP program.
199 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
200 act = bpf_prog_run_xdp(xdp_prog, xdp);
201 xdp->handle += xdp->data - xdp->data_hard_start;
206 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
207 result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
210 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
211 result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED;
214 bpf_warn_invalid_xdp_action(act);
216 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
217 /* fallthrough -- handle aborts by dropping packet */
219 result = I40E_XDP_CONSUMED;
227 * i40e_alloc_buffer_zc - Allocates an i40e_rx_buffer
229 * @bi: Rx buffer to populate
231 * This function allocates an Rx buffer. The buffer can come from fill
232 * queue, or via the recycle queue (next_to_alloc).
234 * Returns true for a successful allocation, false otherwise
236 static bool i40e_alloc_buffer_zc(struct i40e_ring *rx_ring,
237 struct i40e_rx_buffer *bi)
239 struct xdp_umem *umem = rx_ring->xsk_umem;
240 void *addr = bi->addr;
244 rx_ring->rx_stats.page_reuse_count++;
248 if (!xsk_umem_peek_addr(umem, &handle)) {
249 rx_ring->rx_stats.alloc_page_failed++;
253 hr = umem->headroom + XDP_PACKET_HEADROOM;
255 bi->dma = xdp_umem_get_dma(umem, handle);
258 bi->addr = xdp_umem_get_data(umem, handle);
261 bi->handle = handle + umem->headroom;
263 xsk_umem_discard_addr(umem);
268 * i40e_alloc_buffer_slow_zc - Allocates an i40e_rx_buffer
270 * @bi: Rx buffer to populate
272 * This function allocates an Rx buffer. The buffer can come from fill
273 * queue, or via the reuse queue.
275 * Returns true for a successful allocation, false otherwise
277 static bool i40e_alloc_buffer_slow_zc(struct i40e_ring *rx_ring,
278 struct i40e_rx_buffer *bi)
280 struct xdp_umem *umem = rx_ring->xsk_umem;
283 if (!xsk_umem_peek_addr_rq(umem, &handle)) {
284 rx_ring->rx_stats.alloc_page_failed++;
288 handle &= rx_ring->xsk_umem->chunk_mask;
290 hr = umem->headroom + XDP_PACKET_HEADROOM;
292 bi->dma = xdp_umem_get_dma(umem, handle);
295 bi->addr = xdp_umem_get_data(umem, handle);
298 bi->handle = handle + umem->headroom;
300 xsk_umem_discard_addr_rq(umem);
304 static __always_inline bool
305 __i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count,
306 bool alloc(struct i40e_ring *rx_ring,
307 struct i40e_rx_buffer *bi))
309 u16 ntu = rx_ring->next_to_use;
310 union i40e_rx_desc *rx_desc;
311 struct i40e_rx_buffer *bi;
314 rx_desc = I40E_RX_DESC(rx_ring, ntu);
315 bi = &rx_ring->rx_bi[ntu];
317 if (!alloc(rx_ring, bi)) {
322 dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 0,
326 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
332 if (unlikely(ntu == rx_ring->count)) {
333 rx_desc = I40E_RX_DESC(rx_ring, 0);
338 rx_desc->wb.qword1.status_error_len = 0;
343 if (rx_ring->next_to_use != ntu)
344 i40e_release_rx_desc(rx_ring, ntu);
350 * i40e_alloc_rx_buffers_zc - Allocates a number of Rx buffers
352 * @count: The number of buffers to allocate
354 * This function allocates a number of Rx buffers from the reuse queue
355 * or fill ring and places them on the Rx ring.
357 * Returns true for a successful allocation, false otherwise
359 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
361 return __i40e_alloc_rx_buffers_zc(rx_ring, count,
362 i40e_alloc_buffer_slow_zc);
366 * i40e_alloc_rx_buffers_fast_zc - Allocates a number of Rx buffers
368 * @count: The number of buffers to allocate
370 * This function allocates a number of Rx buffers from the fill ring
371 * or the internal recycle mechanism and places them on the Rx ring.
373 * Returns true for a successful allocation, false otherwise
375 static bool i40e_alloc_rx_buffers_fast_zc(struct i40e_ring *rx_ring, u16 count)
377 return __i40e_alloc_rx_buffers_zc(rx_ring, count,
378 i40e_alloc_buffer_zc);
382 * i40e_get_rx_buffer_zc - Return the current Rx buffer
384 * @size: The size of the rx buffer (read from descriptor)
386 * This function returns the current, received Rx buffer, and also
387 * does DMA synchronization. the Rx ring.
389 * Returns the received Rx buffer
391 static struct i40e_rx_buffer *i40e_get_rx_buffer_zc(struct i40e_ring *rx_ring,
392 const unsigned int size)
394 struct i40e_rx_buffer *bi;
396 bi = &rx_ring->rx_bi[rx_ring->next_to_clean];
398 /* we are reusing so sync this buffer for CPU use */
399 dma_sync_single_range_for_cpu(rx_ring->dev,
408 * i40e_reuse_rx_buffer_zc - Recycle an Rx buffer
410 * @old_bi: The Rx buffer to recycle
412 * This function recycles a finished Rx buffer, and places it on the
413 * recycle queue (next_to_alloc).
415 static void i40e_reuse_rx_buffer_zc(struct i40e_ring *rx_ring,
416 struct i40e_rx_buffer *old_bi)
418 struct i40e_rx_buffer *new_bi = &rx_ring->rx_bi[rx_ring->next_to_alloc];
419 unsigned long mask = (unsigned long)rx_ring->xsk_umem->chunk_mask;
420 u64 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM;
421 u16 nta = rx_ring->next_to_alloc;
423 /* update, and store next to alloc */
425 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
427 /* transfer page from old buffer to new buffer */
428 new_bi->dma = old_bi->dma & mask;
431 new_bi->addr = (void *)((unsigned long)old_bi->addr & mask);
434 new_bi->handle = old_bi->handle & mask;
435 new_bi->handle += rx_ring->xsk_umem->headroom;
441 * i40e_zca_free - Free callback for MEM_TYPE_ZERO_COPY allocations
442 * @alloc: Zero-copy allocator
443 * @handle: Buffer handle
445 void i40e_zca_free(struct zero_copy_allocator *alloc, unsigned long handle)
447 struct i40e_rx_buffer *bi;
448 struct i40e_ring *rx_ring;
452 rx_ring = container_of(alloc, struct i40e_ring, zca);
453 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM;
454 mask = rx_ring->xsk_umem->chunk_mask;
456 nta = rx_ring->next_to_alloc;
457 bi = &rx_ring->rx_bi[nta];
460 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
464 bi->dma = xdp_umem_get_dma(rx_ring->xsk_umem, handle);
467 bi->addr = xdp_umem_get_data(rx_ring->xsk_umem, handle);
470 bi->handle = (u64)handle + rx_ring->xsk_umem->headroom;
474 * i40e_construct_skb_zc - Create skbufff from zero-copy Rx buffer
479 * This functions allocates a new skb from a zero-copy Rx buffer.
481 * Returns the skb, or NULL on failure.
483 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
484 struct i40e_rx_buffer *bi,
485 struct xdp_buff *xdp)
487 unsigned int metasize = xdp->data - xdp->data_meta;
488 unsigned int datasize = xdp->data_end - xdp->data;
491 /* allocate a skb to store the frags */
492 skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
493 xdp->data_end - xdp->data_hard_start,
494 GFP_ATOMIC | __GFP_NOWARN);
498 skb_reserve(skb, xdp->data - xdp->data_hard_start);
499 memcpy(__skb_put(skb, datasize), xdp->data, datasize);
501 skb_metadata_set(skb, metasize);
503 i40e_reuse_rx_buffer_zc(rx_ring, bi);
508 * i40e_inc_ntc: Advance the next_to_clean index
511 static void i40e_inc_ntc(struct i40e_ring *rx_ring)
513 u32 ntc = rx_ring->next_to_clean + 1;
515 ntc = (ntc < rx_ring->count) ? ntc : 0;
516 rx_ring->next_to_clean = ntc;
517 prefetch(I40E_RX_DESC(rx_ring, ntc));
521 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
523 * @budget: NAPI budget
525 * Returns amount of work completed
527 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
529 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
530 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
531 unsigned int xdp_res, xdp_xmit = 0;
532 bool failure = false;
536 xdp.rxq = &rx_ring->xdp_rxq;
538 while (likely(total_rx_packets < (unsigned int)budget)) {
539 struct i40e_rx_buffer *bi;
540 union i40e_rx_desc *rx_desc;
544 if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
546 !i40e_alloc_rx_buffers_fast_zc(rx_ring,
551 rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
552 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
554 /* This memory barrier is needed to keep us from reading
555 * any other fields out of the rx_desc until we have
556 * verified the descriptor has been written back.
560 bi = i40e_clean_programming_status(rx_ring, rx_desc,
563 i40e_reuse_rx_buffer_zc(rx_ring, bi);
568 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
569 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
573 bi = i40e_get_rx_buffer_zc(rx_ring, size);
575 xdp.data_meta = xdp.data;
576 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
577 xdp.data_end = xdp.data + size;
578 xdp.handle = bi->handle;
580 xdp_res = i40e_run_xdp_zc(rx_ring, &xdp);
582 if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) {
586 i40e_reuse_rx_buffer_zc(rx_ring, bi);
589 total_rx_bytes += size;
593 i40e_inc_ntc(rx_ring);
599 /* NB! We are not checking for errors using
600 * i40e_test_staterr with
601 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
602 * SBP is *not* set in PRT_SBPVSI (default not set).
604 skb = i40e_construct_skb_zc(rx_ring, bi, &xdp);
606 rx_ring->rx_stats.alloc_buff_failed++;
611 i40e_inc_ntc(rx_ring);
613 if (eth_skb_pad(skb))
616 total_rx_bytes += skb->len;
619 i40e_process_skb_fields(rx_ring, rx_desc, skb);
620 napi_gro_receive(&rx_ring->q_vector->napi, skb);
623 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
624 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
625 return failure ? budget : (int)total_rx_packets;
629 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
630 * @xdp_ring: XDP Tx ring
631 * @budget: NAPI budget
633 * Returns true if the work is finished.
635 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
637 struct i40e_tx_desc *tx_desc = NULL;
638 struct i40e_tx_buffer *tx_bi;
639 bool work_done = true;
643 while (budget-- > 0) {
644 if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
645 xdp_ring->tx_stats.tx_busy++;
650 if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &dma, &len))
653 dma_sync_single_for_device(xdp_ring->dev, dma, len,
656 tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use];
657 tx_bi->bytecount = len;
659 tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use);
660 tx_desc->buffer_addr = cpu_to_le64(dma);
661 tx_desc->cmd_type_offset_bsz =
662 build_ctob(I40E_TX_DESC_CMD_ICRC
663 | I40E_TX_DESC_CMD_EOP,
666 xdp_ring->next_to_use++;
667 if (xdp_ring->next_to_use == xdp_ring->count)
668 xdp_ring->next_to_use = 0;
672 /* Request an interrupt for the last frame and bump tail ptr. */
673 tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS <<
674 I40E_TXD_QW1_CMD_SHIFT);
675 i40e_xdp_ring_update_tail(xdp_ring);
677 xsk_umem_consume_tx_done(xdp_ring->xsk_umem);
680 return !!budget && work_done;
684 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
685 * @tx_ring: XDP Tx ring
686 * @tx_bi: Tx buffer info to clean
688 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
689 struct i40e_tx_buffer *tx_bi)
691 xdp_return_frame(tx_bi->xdpf);
692 dma_unmap_single(tx_ring->dev,
693 dma_unmap_addr(tx_bi, dma),
694 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
695 dma_unmap_len_set(tx_bi, len, 0);
699 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
700 * @tx_ring: XDP Tx ring
701 * @tx_bi: Tx buffer info to clean
703 * Returns true if cleanup/tranmission is done.
705 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi,
706 struct i40e_ring *tx_ring, int napi_budget)
708 unsigned int ntc, total_bytes = 0, budget = vsi->work_limit;
709 u32 i, completed_frames, frames_ready, xsk_frames = 0;
710 struct xdp_umem *umem = tx_ring->xsk_umem;
711 u32 head_idx = i40e_get_head(tx_ring);
712 bool work_done = true, xmit_done;
713 struct i40e_tx_buffer *tx_bi;
715 if (head_idx < tx_ring->next_to_clean)
716 head_idx += tx_ring->count;
717 frames_ready = head_idx - tx_ring->next_to_clean;
719 if (frames_ready == 0) {
721 } else if (frames_ready > budget) {
722 completed_frames = budget;
725 completed_frames = frames_ready;
728 ntc = tx_ring->next_to_clean;
730 for (i = 0; i < completed_frames; i++) {
731 tx_bi = &tx_ring->tx_bi[ntc];
734 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
739 total_bytes += tx_bi->bytecount;
741 if (++ntc >= tx_ring->count)
745 tx_ring->next_to_clean += completed_frames;
746 if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
747 tx_ring->next_to_clean -= tx_ring->count;
750 xsk_umem_complete_tx(umem, xsk_frames);
752 i40e_arm_wb(tx_ring, vsi, budget);
753 i40e_update_tx_stats(tx_ring, completed_frames, total_bytes);
756 xmit_done = i40e_xmit_zc(tx_ring, budget);
758 return work_done && xmit_done;
762 * i40e_xsk_async_xmit - Implements the ndo_xsk_async_xmit
763 * @dev: the netdevice
764 * @queue_id: queue id to wake up
766 * Returns <0 for errors, 0 otherwise.
768 int i40e_xsk_async_xmit(struct net_device *dev, u32 queue_id)
770 struct i40e_netdev_priv *np = netdev_priv(dev);
771 struct i40e_vsi *vsi = np->vsi;
772 struct i40e_ring *ring;
774 if (test_bit(__I40E_VSI_DOWN, vsi->state))
777 if (!i40e_enabled_xdp_vsi(vsi))
780 if (queue_id >= vsi->num_queue_pairs)
783 if (!vsi->xdp_rings[queue_id]->xsk_umem)
786 ring = vsi->xdp_rings[queue_id];
788 /* The idea here is that if NAPI is running, mark a miss, so
789 * it will run again. If not, trigger an interrupt and
790 * schedule the NAPI from interrupt context. If NAPI would be
791 * scheduled here, the interrupt affinity would not be
794 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
795 i40e_force_wb(vsi, ring->q_vector);
800 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
804 for (i = 0; i < rx_ring->count; i++) {
805 struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
810 xsk_umem_fq_reuse(rx_ring->xsk_umem, rx_bi->handle);
816 * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown
817 * @xdp_ring: XDP Tx ring
819 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
821 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
822 struct xdp_umem *umem = tx_ring->xsk_umem;
823 struct i40e_tx_buffer *tx_bi;
827 tx_bi = &tx_ring->tx_bi[ntc];
830 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
837 if (ntc >= tx_ring->count)
842 xsk_umem_complete_tx(umem, xsk_frames);
846 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have AF_XDP UMEM attached
849 * Returns true if any of the Rx rings has an AF_XDP UMEM attached
851 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
853 struct net_device *netdev = vsi->netdev;
856 for (i = 0; i < vsi->num_queue_pairs; i++) {
857 if (xdp_get_umem_from_qid(netdev, i))