Debian Codegraph @

drivers/net/ethernet/mellanox/mlx5/core/en/xsk/tx.c

raw
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies. */
3
4 #include "tx.h"
5 #include "pool.h"
6 #include "en/xdp.h"
7 #include "en/params.h"
8 #include <net/xdp_sock_drv.h>
9
10 int mlx5e_xsk_wakeup(struct net_device *dev, u32 qid, u32 flags)
11 {
12 struct mlx5e_priv *priv = netdev_priv(dev);
13 struct mlx5e_params *params = &priv->channels.params;
14 struct mlx5e_channel *c;
15
16 if (unlikely(!mlx5e_xdp_is_active(priv)))
17 return -ENETDOWN;
18
19 if (unlikely(qid >= params->num_channels))
20 return -EINVAL;
21
22 c = priv->channels.c[qid];
23
24 if (!napi_if_scheduled_mark_missed(&c->napi)) {
25 /* To avoid WQE overrun, don't post a NOP if async_icosq is not
26 * active and not polled by NAPI. Return 0, because the upcoming
27 * activate will trigger the IRQ for us.
28 */
29 if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED,
30 &c->async_icosq->state)))
31 return 0;
32
33 if (test_and_set_bit(MLX5E_SQ_STATE_PENDING_XSK_TX,
34 &c->async_icosq->state))
35 return 0;
36
37 mlx5e_trigger_napi_async_icosq(c);
38 }
39
40 return 0;
41 }
42
43 /* When TX fails (because of the size of the packet), we need to get completions
44 * in order, so post a NOP to get a CQE. Since AF_XDP doesn't distinguish
45 * between successful TX and errors, handling in mlx5e_poll_xdpsq_cq is the
46 * same.
47 */
48 static void mlx5e_xsk_tx_post_err(struct mlx5e_xdpsq *sq,
49 union mlx5e_xdp_info *xdpi)
50 {
51 u16 pi = mlx5_wq_cyc_ctr2ix(&sq->wq, sq->pc);
52 struct mlx5e_xdp_wqe_info *wi = &sq->db.wqe_info[pi];
53 struct mlx5e_tx_wqe *nopwqe;
54
55 wi->num_wqebbs = 1;
56 wi->num_pkts = 1;
57
58 nopwqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
59 mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo, *xdpi);
60 if (xp_tx_metadata_enabled(sq->xsk_pool))
61 mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo,
62 (union mlx5e_xdp_info) { .xsk_meta = {} });
63 sq->doorbell_cseg = &nopwqe->ctrl;
64 }
65
66 bool mlx5e_xsk_tx(struct mlx5e_xdpsq *sq, unsigned int budget)
67 {
68 struct xsk_buff_pool *pool = sq->xsk_pool;
69 struct xsk_tx_metadata *meta = NULL;
70 union mlx5e_xdp_info xdpi;
71 bool work_done = true;
72 bool flush = false;
73
74 xdpi.mode = MLX5E_XDP_XMIT_MODE_XSK;
75
76 for (; budget; budget--) {
77 int check_result = INDIRECT_CALL_2(sq->xmit_xdp_frame_check,
78 mlx5e_xmit_xdp_frame_check_mpwqe,
79 mlx5e_xmit_xdp_frame_check,
80 sq);
81 struct mlx5e_xmit_data xdptxd = {};
82 struct xdp_desc desc;
83 bool ret;
84
85 if (unlikely(check_result < 0)) {
86 work_done = false;
87 break;
88 }
89
90 if (!xsk_tx_peek_desc(pool, &desc)) {
91 /* TX will get stuck until something wakes it up by
92 * triggering NAPI. Currently it's expected that the
93 * application calls sendto() if there are consumed, but
94 * not completed frames.
95 */
96 break;
97 }
98
99 xdptxd.dma_addr = xsk_buff_raw_get_dma(pool, desc.addr);
100 xdptxd.data = xsk_buff_raw_get_data(pool, desc.addr);
101 xdptxd.len = desc.len;
102 meta = xsk_buff_get_metadata(pool, desc.addr);
103
104 xsk_buff_raw_dma_sync_for_device(pool, xdptxd.dma_addr, xdptxd.len);
105
106 ret = INDIRECT_CALL_2(sq->xmit_xdp_frame, mlx5e_xmit_xdp_frame_mpwqe,
107 mlx5e_xmit_xdp_frame, sq, &xdptxd,
108 check_result, meta);
109 if (unlikely(!ret)) {
110 if (sq->mpwqe.wqe)
111 mlx5e_xdp_mpwqe_complete(sq);
112
113 mlx5e_xsk_tx_post_err(sq, &xdpi);
114 } else {
115 mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo, xdpi);
116 if (xp_tx_metadata_enabled(sq->xsk_pool)) {
117 struct xsk_tx_metadata_compl compl;
118
119 xsk_tx_metadata_to_compl(meta, &compl);
120 XSK_TX_COMPL_FITS(void *);
121
122 mlx5e_xdpi_fifo_push(&sq->db.xdpi_fifo,
123 (union mlx5e_xdp_info)
124 { .xsk_meta = compl });
125 }
126 }
127
128 flush = true;
129 }
130
131 if (flush) {
132 if (sq->mpwqe.wqe)
133 mlx5e_xdp_mpwqe_complete(sq);
134 mlx5e_xmit_xdp_doorbell(sq);
135
136 xsk_tx_release(pool);
137 }
138
139 return !(budget && work_done);
140 }