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 int i40e_alloc_rx_bi_zc(struct i40e_ring *rx_ring)
15 unsigned long sz = sizeof(*rx_ring->rx_bi_zc) * rx_ring->count;
17 rx_ring->rx_bi_zc = kzalloc(sz, GFP_KERNEL);
18 return rx_ring->rx_bi_zc ? 0 : -ENOMEM;
21 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring)
23 memset(rx_ring->rx_bi_zc, 0,
24 sizeof(*rx_ring->rx_bi_zc) * rx_ring->count);
27 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx)
29 return &rx_ring->rx_bi_zc[idx];
33 * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a
37 * @qid: Rx ring to associate buffer pool with
39 * Returns 0 on success, <0 on failure
41 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi,
42 struct xsk_buff_pool *pool,
45 struct net_device *netdev = vsi->netdev;
49 if (vsi->type != I40E_VSI_MAIN)
52 if (qid >= vsi->num_queue_pairs)
55 if (qid >= netdev->real_num_rx_queues ||
56 qid >= netdev->real_num_tx_queues)
59 err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR);
63 set_bit(qid, vsi->af_xdp_zc_qps);
65 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
68 err = i40e_queue_pair_disable(vsi, qid);
72 err = i40e_queue_pair_enable(vsi, qid);
76 /* Kick start the NAPI context so that receiving will start */
77 err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX);
86 * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a
89 * @qid: Rx ring to associate buffer pool with
91 * Returns 0 on success, <0 on failure
93 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid)
95 struct net_device *netdev = vsi->netdev;
96 struct xsk_buff_pool *pool;
100 pool = xsk_get_pool_from_qid(netdev, qid);
104 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi);
107 err = i40e_queue_pair_disable(vsi, qid);
112 clear_bit(qid, vsi->af_xdp_zc_qps);
113 xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR);
116 err = i40e_queue_pair_enable(vsi, qid);
125 * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from
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
131 * This function enables or disables a buffer pool to a certain ring.
133 * Returns 0 on success, <0 on failure
135 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool,
138 return pool ? i40e_xsk_pool_enable(vsi, pool, qid) :
139 i40e_xsk_pool_disable(vsi, qid);
143 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff
145 * @xdp: xdp_buff used as input to the XDP program
146 * @xdp_prog: XDP program to run
148 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR}
150 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp,
151 struct bpf_prog *xdp_prog)
153 int err, result = I40E_XDP_PASS;
154 struct i40e_ring *xdp_ring;
157 act = bpf_prog_run_xdp(xdp_prog, xdp);
159 if (likely(act == XDP_REDIRECT)) {
160 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
162 return I40E_XDP_REDIR;
163 if (xsk_uses_need_wakeup(rx_ring->xsk_pool) && err == -ENOBUFS)
164 result = I40E_XDP_EXIT;
166 result = I40E_XDP_CONSUMED;
174 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
175 result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
176 if (result == I40E_XDP_CONSUMED)
180 result = I40E_XDP_CONSUMED;
183 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
186 result = I40E_XDP_CONSUMED;
188 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
193 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count)
195 u16 ntu = rx_ring->next_to_use;
196 union i40e_rx_desc *rx_desc;
197 struct xdp_buff **xdp;
201 rx_desc = I40E_RX_DESC(rx_ring, ntu);
202 xdp = i40e_rx_bi(rx_ring, ntu);
204 nb_buffs = min_t(u16, count, rx_ring->count - ntu);
205 nb_buffs = xsk_buff_alloc_batch(rx_ring->xsk_pool, xdp, nb_buffs);
211 dma = xsk_buff_xdp_get_dma(*xdp);
212 rx_desc->read.pkt_addr = cpu_to_le64(dma);
213 rx_desc->read.hdr_addr = 0;
220 if (ntu == rx_ring->count) {
221 rx_desc = I40E_RX_DESC(rx_ring, 0);
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);
229 return count == nb_buffs;
233 * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer
237 * This functions allocates a new skb from a zero-copy Rx buffer.
239 * Returns the skb, or NULL on failure.
241 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,
242 struct xdp_buff *xdp)
244 unsigned int totalsize = xdp->data_end - xdp->data_meta;
245 unsigned int metasize = xdp->data - xdp->data_meta;
248 net_prefetch(xdp->data_meta);
250 /* allocate a skb to store the frags */
251 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, totalsize,
252 GFP_ATOMIC | __GFP_NOWARN);
256 memcpy(__skb_put(skb, totalsize), xdp->data_meta,
257 ALIGN(totalsize, sizeof(long)));
260 skb_metadata_set(skb, metasize);
261 __skb_pull(skb, metasize);
269 static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring,
270 struct xdp_buff *xdp_buff,
271 union i40e_rx_desc *rx_desc,
272 unsigned int *rx_packets,
273 unsigned int *rx_bytes,
275 unsigned int xdp_res,
283 if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX)
286 if (xdp_res == I40E_XDP_EXIT) {
291 if (xdp_res == I40E_XDP_CONSUMED) {
292 xsk_buff_free(xdp_buff);
295 if (xdp_res == I40E_XDP_PASS) {
296 /* NB! We are not checking for errors using
297 * i40e_test_staterr with
298 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that
299 * SBP is *not* set in PRT_SBPVSI (default not set).
301 skb = i40e_construct_skb_zc(rx_ring, xdp_buff);
303 rx_ring->rx_stats.alloc_buff_failed++;
309 if (eth_skb_pad(skb)) {
315 *rx_bytes = skb->len;
316 i40e_process_skb_fields(rx_ring, rx_desc, skb);
317 napi_gro_receive(&rx_ring->q_vector->napi, skb);
321 /* Should never get here, as all valid cases have been handled already.
327 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring
329 * @budget: NAPI budget
331 * Returns amount of work completed
333 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)
335 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
336 u16 next_to_clean = rx_ring->next_to_clean;
337 u16 count_mask = rx_ring->count - 1;
338 unsigned int xdp_res, xdp_xmit = 0;
339 struct bpf_prog *xdp_prog;
340 bool failure = false;
343 /* NB! xdp_prog will always be !NULL, due to the fact that
344 * this path is enabled by setting an XDP program.
346 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
348 while (likely(total_rx_packets < (unsigned int)budget)) {
349 union i40e_rx_desc *rx_desc;
350 unsigned int rx_packets;
351 unsigned int rx_bytes;
356 rx_desc = I40E_RX_DESC(rx_ring, next_to_clean);
357 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
359 /* This memory barrier is needed to keep us from reading
360 * any other fields out of the rx_desc until we have
361 * verified the descriptor has been written back.
365 if (i40e_rx_is_programming_status(qword)) {
366 i40e_clean_programming_status(rx_ring,
367 rx_desc->raw.qword[0],
369 bi = *i40e_rx_bi(rx_ring, next_to_clean);
371 next_to_clean = (next_to_clean + 1) & count_mask;
375 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
376 I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
380 bi = *i40e_rx_bi(rx_ring, next_to_clean);
381 xsk_buff_set_size(bi, size);
382 xsk_buff_dma_sync_for_cpu(bi, rx_ring->xsk_pool);
384 xdp_res = i40e_run_xdp_zc(rx_ring, bi, xdp_prog);
385 i40e_handle_xdp_result_zc(rx_ring, bi, rx_desc, &rx_packets,
386 &rx_bytes, size, xdp_res, &failure);
389 total_rx_packets += rx_packets;
390 total_rx_bytes += rx_bytes;
391 xdp_xmit |= xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR);
392 next_to_clean = (next_to_clean + 1) & count_mask;
395 rx_ring->next_to_clean = next_to_clean;
396 cleaned_count = (next_to_clean - rx_ring->next_to_use - 1) & count_mask;
398 if (cleaned_count >= I40E_RX_BUFFER_WRITE)
399 failure |= !i40e_alloc_rx_buffers_zc(rx_ring, cleaned_count);
401 i40e_finalize_xdp_rx(rx_ring, xdp_xmit);
402 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets);
404 if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
405 if (failure || next_to_clean == rx_ring->next_to_use)
406 xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
408 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
410 return (int)total_rx_packets;
412 return failure ? budget : (int)total_rx_packets;
415 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
416 unsigned int *total_bytes)
418 struct i40e_tx_desc *tx_desc;
421 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
422 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
424 tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
425 tx_desc->buffer_addr = cpu_to_le64(dma);
426 tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC | I40E_TX_DESC_CMD_EOP,
429 *total_bytes += desc->len;
432 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
433 unsigned int *total_bytes)
435 u16 ntu = xdp_ring->next_to_use;
436 struct i40e_tx_desc *tx_desc;
440 loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
441 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr);
442 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len);
444 tx_desc = I40E_TX_DESC(xdp_ring, ntu++);
445 tx_desc->buffer_addr = cpu_to_le64(dma);
446 tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC |
447 I40E_TX_DESC_CMD_EOP,
450 *total_bytes += desc[i].len;
453 xdp_ring->next_to_use = ntu;
456 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
457 unsigned int *total_bytes)
459 u32 batched, leftover, i;
461 batched = nb_pkts & ~(PKTS_PER_BATCH - 1);
462 leftover = nb_pkts & (PKTS_PER_BATCH - 1);
463 for (i = 0; i < batched; i += PKTS_PER_BATCH)
464 i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
465 for (i = batched; i < batched + leftover; i++)
466 i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes);
469 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
471 u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1;
472 struct i40e_tx_desc *tx_desc;
474 tx_desc = I40E_TX_DESC(xdp_ring, ntu);
475 tx_desc->cmd_type_offset_bsz |= cpu_to_le64(I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT);
479 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
480 * @xdp_ring: XDP Tx ring
481 * @budget: NAPI budget
483 * Returns true if the work is finished.
485 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
487 struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs;
488 u32 nb_pkts, nb_processed = 0;
489 unsigned int total_bytes = 0;
491 nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget);
495 if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
496 nb_processed = xdp_ring->count - xdp_ring->next_to_use;
497 i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
498 xdp_ring->next_to_use = 0;
501 i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
504 /* Request an interrupt for the last frame and bump tail ptr. */
505 i40e_set_rs_bit(xdp_ring);
506 i40e_xdp_ring_update_tail(xdp_ring);
508 i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
510 return nb_pkts < budget;
514 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry
515 * @tx_ring: XDP Tx ring
516 * @tx_bi: Tx buffer info to clean
518 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring,
519 struct i40e_tx_buffer *tx_bi)
521 xdp_return_frame(tx_bi->xdpf);
522 tx_ring->xdp_tx_active--;
523 dma_unmap_single(tx_ring->dev,
524 dma_unmap_addr(tx_bi, dma),
525 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE);
526 dma_unmap_len_set(tx_bi, len, 0);
530 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries
532 * @tx_ring: XDP Tx ring
534 * Returns true if cleanup/tranmission is done.
536 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring)
538 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
539 u32 i, completed_frames, xsk_frames = 0;
540 u32 head_idx = i40e_get_head(tx_ring);
541 struct i40e_tx_buffer *tx_bi;
544 if (head_idx < tx_ring->next_to_clean)
545 head_idx += tx_ring->count;
546 completed_frames = head_idx - tx_ring->next_to_clean;
548 if (completed_frames == 0)
551 if (likely(!tx_ring->xdp_tx_active)) {
552 xsk_frames = completed_frames;
556 ntc = tx_ring->next_to_clean;
558 for (i = 0; i < completed_frames; i++) {
559 tx_bi = &tx_ring->tx_bi[ntc];
562 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
568 if (++ntc >= tx_ring->count)
573 tx_ring->next_to_clean += completed_frames;
574 if (unlikely(tx_ring->next_to_clean >= tx_ring->count))
575 tx_ring->next_to_clean -= tx_ring->count;
578 xsk_tx_completed(bp, xsk_frames);
580 i40e_arm_wb(tx_ring, vsi, completed_frames);
583 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
584 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
586 return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring));
590 * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup
591 * @dev: the netdevice
592 * @queue_id: queue id to wake up
593 * @flags: ignored in our case since we have Rx and Tx in the same NAPI.
595 * Returns <0 for errors, 0 otherwise.
597 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
599 struct i40e_netdev_priv *np = netdev_priv(dev);
600 struct i40e_vsi *vsi = np->vsi;
601 struct i40e_pf *pf = vsi->back;
602 struct i40e_ring *ring;
604 if (test_bit(__I40E_CONFIG_BUSY, pf->state))
607 if (test_bit(__I40E_VSI_DOWN, vsi->state))
610 if (!i40e_enabled_xdp_vsi(vsi))
613 if (queue_id >= vsi->num_queue_pairs)
616 if (!vsi->xdp_rings[queue_id]->xsk_pool)
619 ring = vsi->xdp_rings[queue_id];
621 /* The idea here is that if NAPI is running, mark a miss, so
622 * it will run again. If not, trigger an interrupt and
623 * schedule the NAPI from interrupt context. If NAPI would be
624 * scheduled here, the interrupt affinity would not be
627 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi))
628 i40e_force_wb(vsi, ring->q_vector);
633 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring)
635 u16 count_mask = rx_ring->count - 1;
636 u16 ntc = rx_ring->next_to_clean;
637 u16 ntu = rx_ring->next_to_use;
639 for ( ; ntc != ntu; ntc = (ntc + 1) & count_mask) {
640 struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc);
642 xsk_buff_free(rx_bi);
647 * i40e_xsk_clean_tx_ring - Clean the XDP Tx ring on shutdown
648 * @tx_ring: XDP Tx ring
650 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring)
652 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use;
653 struct xsk_buff_pool *bp = tx_ring->xsk_pool;
654 struct i40e_tx_buffer *tx_bi;
658 tx_bi = &tx_ring->tx_bi[ntc];
661 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi);
668 if (ntc >= tx_ring->count)
673 xsk_tx_completed(bp, xsk_frames);
677 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP
678 * buffer pool attached
681 * Returns true if any of the Rx rings has an AF_XDP buffer pool attached
683 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi)
685 struct net_device *netdev = vsi->netdev;
688 for (i = 0; i < vsi->num_queue_pairs; i++) {
689 if (xsk_get_pool_from_qid(netdev, i))