1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock_drv.h>
9 #include "i40e_txrx_common.h"
12 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
14 memset(rx_ring->rx_bi_zc, 0,
15 sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
18 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
20 return &rx_ring->rx_bi_zc[idx];
24 * i40e_realloc_rx_xdp_bi - reallocate SW ring for either XSK or normal buffer
25 * @rx_ring: Current rx ring
26 * @pool_present: is pool for XSK present
28 * Try allocating memory and return ENOMEM, if failed to allocate.
29 * If allocation was successful, substitute buffer with allocated one.
30 * Returns 0 on success, negative on failure
32 static int i40e_realloc_rx_xdp_bi(struct i40e_ring *rx_ring, bool pool_present)
34 size_t elem_size = pool_present ? sizeof(*rx_ring->rx_bi_zc) :
35 sizeof(*rx_ring->rx_bi);
36 void *sw_ring = kcalloc(rx_ring->count, elem_size, GFP_KERNEL);
42 kfree(rx_ring->rx_bi);
43 rx_ring->rx_bi = NULL;
44 rx_ring->rx_bi_zc = sw_ring;
46 kfree(rx_ring->rx_bi_zc);
47 rx_ring->rx_bi_zc = NULL;
48 rx_ring->rx_bi = sw_ring;
54 * i40e_realloc_rx_bi_zc - reallocate rx SW rings
56 * @zc: is zero copy set
58 * Reallocate buffer for rx_rings that might be used by XSK.
59 * XDP requires more memory, than rx_buf provides.
60 * Returns 0 on success, negative on failure
62 int i40e_realloc_rx_bi_zc(struct i40e_vsi *vsi, bool zc)
64 struct i40e_ring *rx_ring;
67 for_each_set_bit(q, vsi->af_xdp_zc_qps, vsi->alloc_queue_pairs) {
68 rx_ring = vsi->rx_rings[q];
69 if (i40e_realloc_rx_xdp_bi(rx_ring, zc))
76 * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
80 * @qid: Rx ring to associate buffer pool with
82 * Returns 0 on success, <0 on failure
84 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
85 struct xsk_buff_pool *pool,
88 struct net_device *netdev = vsi->netdev;
92 if (vsi->type != I40E_VSI_MAIN)
95 if (qid >= vsi->num_queue_pairs)
98 if (qid >= netdev->real_num_rx_queues ||
99 qid >= netdev->real_num_tx_queues)
102 err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
106 set_bit(qid, vsi->af_xdp_zc_qps);
108 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
111 err = i40e_queue_pair_disable(vsi, qid);
115 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], true);
119 err = i40e_queue_pair_enable(vsi, qid);
123 /* Kick start the NAPI context so that receiving will start */
124 err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
133 * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
136 * @qid: Rx ring to associate buffer pool with
138 * Returns 0 on success, <0 on failure
140 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
142 struct net_device *netdev = vsi->netdev;
143 struct xsk_buff_pool *pool;
147 pool = xsk_get_pool_from_qid(netdev, qid);
151 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
154 err = i40e_queue_pair_disable(vsi, qid);
159 clear_bit(qid, vsi->af_xdp_zc_qps);
160 xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
163 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], false);
166 err = i40e_queue_pair_enable(vsi, qid);
175 * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
178 * @pool: Buffer pool to enable/associate to a ring, or NULL to disable
179 * @qid: Rx ring to (dis)associate buffer pool (from)to
181 * This function enables or disables a buffer pool to a certain ring.
183 * Returns 0 on success, <0 on failure
185 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
188 return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
189 i40e_xsk_pool_disable(vsi, qid);
193 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
195 * @xdp: xdp_buff used as input to the XDP program
197 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
199 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp)
201 int err, result = I40E_XDP_PASS;
202 struct i40e_ring *xdp_ring;
203 struct bpf_prog *xdp_prog;
207 /* NB! xdp_prog will always be !NULL, due to the fact that
208 * this path is enabled by setting an XDP program.
210 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
211 act = bpf_prog_run_xdp(xdp_prog, xdp);
213 if (likely(act == XDP_REDIRECT)) {
214 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
218 return I40E_XDP_REDIR;
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 bpf_warn_invalid_xdp_action(act);
235 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
236 fallthrough; /* handle aborts by dropping packet */
238 result = I40E_XDP_CONSUMED;
245 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
247 u16 ntu = rx_ring->next_to_use;
248 union i40e_rx_desc *rx_desc;
249 struct xdp_buff **bi, *xdp;
253 rx_desc = I40E_RX_DESC(rx_ring, ntu);
254 bi = i40e_rx_bi(rx_ring, ntu);
256 xdp = xsk_buff_alloc(rx_ring->xsk_pool);
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;
270 if (unlikely(ntu == rx_ring->count)) {
271 rx_desc = I40E_RX_DESC(rx_ring, 0);
272 bi = i40e_rx_bi(rx_ring, 0);
280 if (rx_ring->next_to_use != ntu) {
281 /* clear the status bits for the next_to_use descriptor */
282 rx_desc->wb.qword1.status_error_len = 0;
283 i40e_release_rx_desc(rx_ring, ntu);
290 * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
294 * This functions allocates a new skb from a zero-copy Rx buffer.
296 * Returns the skb, or NULL on failure.
298 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
299 struct xdp_buff *xdp)
301 unsigned int totalsize = xdp->data_end - xdp->data_meta;
302 unsigned int metasize = xdp->data - xdp->data_meta;
305 net_prefetch(xdp->data_meta);
307 /* allocate a skb to store the frags */
308 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
309 GFP_ATOMIC | __GFP_NOWARN);
313 memcpy(__skb_put(skb, totalsize), xdp->data_meta,
314 ALIGN(totalsize, sizeof(long)));
317 skb_metadata_set(skb, metasize);
318 __skb_pull(skb, metasize);
326 * i40e_inc_ntc: Advance the next_to_clean index
329 static void i40e_inc_ntc(struct i40e_ring *rx_ring)
331 u32 ntc = rx_ring->next_to_clean + 1;
333 ntc = (ntc < rx_ring->count) ? ntc : 0;
334 rx_ring->next_to_clean = ntc;
338 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
340 * @budget: NAPI budget
342 * Returns amount of work completed
344 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
346 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
347 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
348 unsigned int xdp_res, xdp_xmit = 0;
349 bool failure = false;
352 while (likely(total_rx_packets < (unsigned int)budget)) {
353 union i40e_rx_desc *rx_desc;
354 struct xdp_buff **bi;
358 rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
359 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
361 /* This memory barrier is needed to keep us from reading
362 * any other fields out of the rx_desc until we have
363 * verified the descriptor has been written back.
367 if (i40e_rx_is_programming_status(qword)) {
368 i40e_clean_programming_status(rx_ring,
369 rx_desc->raw.qword[0],
371 bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
375 i40e_inc_ntc(rx_ring);
379 bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
380 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
381 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
385 bi = i40e_rx_bi(rx_ring, rx_ring->next_to_clean);
386 (*bi)->data_end = (*bi)->data + size;
387 xsk_buff_dma_sync_for_cpu(*bi, rx_ring->xsk_pool);
389 xdp_res = i40e_run_xdp_zc(rx_ring, *bi);
391 if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR))
397 total_rx_bytes += size;
401 i40e_inc_ntc(rx_ring);
407 /* NB! We are not checking for errors using
408 * i40e_test_staterr with
409 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
410 * SBP is *not* set in PRT_SBPVSI (default not set).
412 skb = i40e_construct_skb_zc(rx_ring, *bi);
414 rx_ring->rx_stats.alloc_buff_failed++;
420 i40e_inc_ntc(rx_ring);
422 if (eth_skb_pad(skb))
425 total_rx_bytes += skb->len;
428 i40e_process_skb_fields(rx_ring, rx_desc, skb);
429 napi_gro_receive(&rx_ring->q_vector->napi, skb);
432 if (cleaned_count >= I40E_RX_BUFFER_WRITE)
433 failure = !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
435 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
436 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
438 if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
439 if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
440 xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
442 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
444 return (int)total_rx_packets;
446 return failure ? budget : (int)total_rx_packets;
450 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
451 * @xdp_ring: XDP Tx ring
452 * @budget: NAPI budget
454 * Returns true if the work is finished.
456 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
458 unsigned int sent_frames = 0, total_bytes = 0;
459 struct i40e_tx_desc *tx_desc = NULL;
460 struct i40e_tx_buffer *tx_bi;
461 struct xdp_desc desc;
464 while (budget-- > 0) {
465 if (!xsk_tx_peek_desc(xdp_ring->xsk_pool, &desc))
468 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc.addr);
469 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma,
472 tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use];
473 tx_bi->bytecount = 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 =
478 build_ctob(I40E_TX_DESC_CMD_ICRC
479 | I40E_TX_DESC_CMD_EOP,
483 total_bytes += tx_bi->bytecount;
485 xdp_ring->next_to_use++;
486 if (xdp_ring->next_to_use == xdp_ring->count)
487 xdp_ring->next_to_use = 0;
491 /* Request an interrupt for the last frame and bump tail ptr. */
492 tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS <<
493 I40E_TXD_QW1_CMD_SHIFT);
494 i40e_xdp_ring_update_tail(xdp_ring);
496 xsk_tx_release(xdp_ring->xsk_pool);
497 i40e_update_tx_stats(xdp_ring, sent_frames, total_bytes);
504 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
505 * @tx_ring: XDP Tx ring
506 * @tx_bi: Tx buffer info to clean
508 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
509 struct i40e_tx_buffer *tx_bi)
511 xdp_return_frame(tx_bi->xdpf);
512 tx_ring->xdp_tx_active--;
513 dma_unmap_single(tx_ring->dev,
514 dma_unmap_addr(tx_bi, dma),
515 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
516 dma_unmap_len_set(tx_bi, len, 0);
520 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
522 * @tx_ring: XDP Tx ring
524 * Returns true if cleanup/tranmission is done.
526 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
528 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
529 u32 i, completed_frames, xsk_frames = 0;
530 u32 head_idx = i40e_get_head(tx_ring);
531 struct i40e_tx_buffer *tx_bi;
534 if (head_idx < tx_ring->next_to_clean)
535 head_idx += tx_ring->count;
536 completed_frames = head_idx - tx_ring->next_to_clean;
538 if (completed_frames == 0)
541 if (likely(!tx_ring->xdp_tx_active)) {
542 xsk_frames = completed_frames;
546 ntc = tx_ring->next_to_clean;
548 for (i = 0; i < completed_frames; i++) {
549 tx_bi = &tx_ring->tx_bi[ntc];
552 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
558 if (++ntc >= tx_ring->count)
563 tx_ring->next_to_clean += completed_frames;
564 if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
565 tx_ring->next_to_clean -= tx_ring->count;
568 xsk_tx_completed(bp, xsk_frames);
570 i40e_arm_wb(tx_ring, vsi, completed_frames);
573 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
574 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
576 return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
580 * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
581 * @dev: the netdevice
582 * @queue_id: queue id to wake up
583 * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
585 * Returns <0 for errors, 0 otherwise.
587 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
589 struct i40e_netdev_priv *np = netdev_priv(dev);
590 struct i40e_vsi *vsi = np->vsi;
591 struct i40e_pf *pf = vsi->back;
592 struct i40e_ring *ring;
594 if (test_bit(__I40E_CONFIG_BUSY, pf->state))
597 if (test_bit(__I40E_VSI_DOWN, vsi->state))
600 if (!i40e_enabled_xdp_vsi(vsi))
603 if (queue_id >= vsi->num_queue_pairs)
606 if (!vsi->xdp_rings[queue_id]->xsk_pool)
609 ring = vsi->xdp_rings[queue_id];
611 /* The idea here is that if NAPI is running, mark a miss, so
612 * it will run again. If not, trigger an interrupt and
613 * schedule the NAPI from interrupt context. If NAPI would be
614 * scheduled here, the interrupt affinity would not be
617 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
618 i40e_force_wb(vsi, ring->q_vector);
623 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
627 for (i = 0; i < rx_ring->count; i++) {
628 struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, i);
633 xsk_buff_free(rx_bi);
639 * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown
640 * @tx_ring: XDP Tx ring
642 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
644 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
645 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
646 struct i40e_tx_buffer *tx_bi;
650 tx_bi = &tx_ring->tx_bi[ntc];
653 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
660 if (ntc >= tx_ring->count)
665 xsk_tx_completed(bp, xsk_frames);
669 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
670 * buffer pool attached
673 * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
675 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
677 struct net_device *netdev = vsi->netdev;
680 for (i = 0; i < vsi->num_queue_pairs; i++) {
681 if (xsk_get_pool_from_qid(netdev, i))