1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies. */
8 #include <net/xdp_sock.h>
10 int mlx5e_xsk_wakeup(struct net_device *dev, u32 qid, u32 flags)
12 struct mlx5e_priv *priv = netdev_priv(dev);
13 struct mlx5e_params *params = &priv->channels.params;
14 struct mlx5e_channel *c;
17 if (unlikely(!mlx5e_xdp_is_active(priv)))
20 if (unlikely(!mlx5e_qid_get_ch_if_in_group(params, qid, MLX5E_RQ_GROUP_XSK, &ix)))
23 c = priv->channels.c[ix];
25 if (unlikely(!test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)))
28 if (!napi_if_scheduled_mark_missed(&c->napi)) {
29 /* To avoid WQE overrun, don't post a NOP if XSKICOSQ is not
30 * active and not polled by NAPI. Return 0, because the upcoming
31 * activate will trigger the IRQ for us.
33 if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &c->xskicosq.state)))
36 spin_lock(&c->xskicosq_lock);
37 mlx5e_trigger_irq(&c->xskicosq);
38 spin_unlock(&c->xskicosq_lock);
44 /* When TX fails (because of the size of the packet), we need to get completions
45 * in order, so post a NOP to get a CQE. Since AF_XDP doesn't distinguish
46 * between successful TX and errors, handling in mlx5e_poll_xdpsq_cq is the
49 static void mlx5e_xsk_tx_post_err(struct mlx5e_xdpsq *sq,
50 struct mlx5e_xdp_info *xdpi)
52 u16 pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
53 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[pi];
54 struct mlx5e_tx_wqe *nopwqe;
59 nopwqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
60 mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo, xdpi);
61 sq->doorbell_cseg = &nopwqe->ctrl;
64 bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget)
66 struct xdp_umem *umem = sq->umem;
67 struct mlx5e_xdp_info xdpi;
68 struct mlx5e_xdp_xmit_data xdptxd;
69 bool work_done = true;
72 xdpi.mode = MLX5E_XDP_XMIT_MODE_XSK;
74 for (; budget; budget--) {
75 int check_result = sq->xmit_xdp_frame_check(sq);
78 if (unlikely(check_result < 0)) {
83 if (!xsk_umem_consume_tx(umem, &desc)) {
84 /* TX will get stuck until something wakes it up by
85 * triggering NAPI. Currently it's expected that the
86 * application calls sendto() if there are consumed, but
87 * not completed frames.
92 xdptxd.dma_addr = xdp_umem_get_dma(umem, desc.addr);
93 xdptxd.data = xdp_umem_get_data(umem, desc.addr);
94 xdptxd.len = desc.len;
96 dma_sync_single_for_device(sq->pdev, xdptxd.dma_addr,
97 xdptxd.len, DMA_BIDIRECTIONAL);
99 if (unlikely(!sq->xmit_xdp_frame(sq, &xdptxd, &xdpi, check_result))) {
101 mlx5e_xdp_mpwqe_complete(sq);
103 mlx5e_xsk_tx_post_err(sq, &xdpi);
111 mlx5e_xdp_mpwqe_complete(sq);
112 mlx5e_xmit_xdp_doorbell(sq);
114 xsk_umem_consume_tx_done(umem);
117 return !(budget && work_done);