Debian Codegraph @

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

raw
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies. */
3
4 #include "setup.h"
5 #include "en/params.h"
6 #include "en/txrx.h"
7 #include "en/health.h"
8 #include <net/xdp_sock_drv.h>
9
10 static int mlx5e_legacy_rq_validate_xsk(struct mlx5_core_dev *mdev,
11 struct mlx5e_params *params,
12 struct mlx5e_rq_opt_param *rqo)
13 {
14 if (!mlx5e_rx_is_linear_skb(mdev, params, rqo)) {
15 mlx5_core_err(mdev, "Legacy RQ linear mode for XSK can't be activated with current params\n");
16 return -EINVAL;
17 }
18
19 return 0;
20 }
21
22 /* The limitation of 2048 can be altered, but shouldn't go beyond the minimal
23 * stride size of striding RQ.
24 */
25 #define MLX5E_MIN_XSK_CHUNK_SIZE max(2048, XDP_UMEM_MIN_CHUNK_SIZE)
26
27 bool mlx5e_validate_xsk_param(struct mlx5e_params *params,
28 struct mlx5e_rq_opt_param *rqo,
29 struct mlx5_core_dev *mdev)
30 {
31 struct mlx5e_xsk_param *xsk = mlx5e_rqo_xsk_param(rqo);
32
33 if (WARN_ON(!xsk))
34 return false;
35
36 /* AF_XDP doesn't support frames larger than PAGE_SIZE,
37 * and xsk->chunk_size is limited to 65535 bytes.
38 */
39 if ((size_t)xsk->chunk_size > PAGE_SIZE || xsk->chunk_size < MLX5E_MIN_XSK_CHUNK_SIZE) {
40 mlx5_core_err(mdev, "XSK chunk size %u out of bounds [%u, %lu]\n", xsk->chunk_size,
41 MLX5E_MIN_XSK_CHUNK_SIZE, PAGE_SIZE);
42 return false;
43 }
44
45 /* frag_sz is different for regular and XSK RQs, so ensure that linear
46 * SKB mode is possible.
47 */
48 switch (params->rq_wq_type) {
49 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
50 return !mlx5e_mpwrq_validate_xsk(mdev, params, rqo);
51 default: /* MLX5_WQ_TYPE_CYCLIC */
52 return !mlx5e_legacy_rq_validate_xsk(mdev, params, rqo);
53 }
54 }
55
56 static int mlx5e_init_xsk_rq(struct mlx5e_channel *c,
57 struct mlx5e_params *params,
58 struct xsk_buff_pool *pool,
59 struct mlx5e_xsk_param *xsk,
60 struct mlx5e_rq *rq)
61 {
62 struct mlx5_core_dev *mdev = c->mdev;
63 int rq_xdp_ix;
64 int err;
65
66 rq->wq_type = params->rq_wq_type;
67 rq->pdev = c->pdev;
68 rq->netdev = c->netdev;
69 rq->priv = c->priv;
70 rq->hwtstamp_config = &c->priv->hwtstamp_config;
71 rq->clock = mdev->clock;
72 rq->icosq = &c->icosq;
73 rq->ix = c->ix;
74 rq->channel = c;
75 rq->mdev = mdev;
76 rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
77 rq->xdpsq = &c->rq_xdpsq;
78 rq->xsk_pool = pool;
79 rq->stats = &c->priv->channel_stats[c->ix]->xskrq;
80 rq->ptp_cyc2time = mlx5_rq_ts_translator(mdev);
81 rq_xdp_ix = c->ix;
82 err = mlx5e_rq_set_handlers(rq, params, xsk);
83 if (err)
84 return err;
85
86 return xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq_xdp_ix, c->napi.napi_id);
87 }
88
89 static int mlx5e_open_xsk_rq(struct mlx5e_channel *c,
90 struct mlx5e_params *params,
91 struct mlx5e_channel_param *cparam,
92 struct xsk_buff_pool *pool)
93 {
94 struct mlx5e_rq_param *rq_param = &cparam->rq;
95 struct mlx5e_rq_opt_param *rqo = &cparam->rq_opt;
96 u16 q_counter = c->priv->q_counter[c->sd_ix];
97 struct mlx5e_rq *xskrq = &c->xskrq;
98 int err;
99
100 err = mlx5e_init_xsk_rq(c, params, pool, rqo->xsk, xskrq);
101 if (err)
102 return err;
103
104 err = mlx5e_open_rq(params, rq_param, rqo, cpu_to_node(c->cpu),
105 q_counter, xskrq);
106 if (err)
107 return err;
108
109 __set_bit(MLX5E_RQ_STATE_XSK, &xskrq->state);
110 return 0;
111 }
112
113 int mlx5e_open_xsk(struct mlx5e_priv *priv, struct mlx5e_params *params,
114 struct mlx5e_channel_param *cparam,
115 struct xsk_buff_pool *pool,
116 struct mlx5e_channel *c)
117 {
118 struct mlx5e_create_cq_param ccp;
119 int err;
120
121 mlx5e_build_create_cq_param(&ccp, c);
122
123 if (!mlx5e_validate_xsk_param(params, &cparam->rq_opt, priv->mdev))
124 return -EINVAL;
125
126 err = mlx5e_open_cq(c->mdev, params->rx_cq_moderation, &cparam->rq.cqp, &ccp,
127 &c->xskrq.cq);
128 if (unlikely(err))
129 return err;
130
131 err = mlx5e_open_xsk_rq(c, params, cparam, pool);
132 if (unlikely(err))
133 goto err_close_rx_cq;
134
135 err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->xdp_sq.cqp, &ccp,
136 &c->xsksq.cq);
137 if (unlikely(err))
138 goto err_close_rq;
139
140 /* Create a separate SQ, so that when the buff pool is disabled, we could
141 * close this SQ safely and stop receiving CQEs. In other case, e.g., if
142 * the XDPSQ was used instead, we might run into trouble when the buff pool
143 * is disabled and then re-enabled, but the SQ continues receiving CQEs
144 * from the old buff pool.
145 */
146 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, pool, &c->xsksq, true);
147 if (unlikely(err))
148 goto err_close_tx_cq;
149
150 set_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
151
152 return 0;
153
154 err_close_tx_cq:
155 mlx5e_close_cq(&c->xsksq.cq);
156
157 err_close_rq:
158 mlx5e_close_rq(&c->xskrq);
159
160 err_close_rx_cq:
161 mlx5e_close_cq(&c->xskrq.cq);
162
163 return err;
164 }
165
166 void mlx5e_close_xsk(struct mlx5e_channel *c)
167 {
168 clear_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
169 synchronize_net(); /* Sync with NAPI. */
170
171 mlx5e_close_rq(&c->xskrq);
172 mlx5e_close_cq(&c->xskrq.cq);
173 mlx5e_close_xdpsq(&c->xsksq);
174 mlx5e_close_cq(&c->xsksq.cq);
175
176 memset(&c->xskrq, 0, sizeof(c->xskrq));
177 memset(&c->xsksq, 0, sizeof(c->xsksq));
178 }
179
180 void mlx5e_activate_xsk(struct mlx5e_channel *c)
181 {
182 /* ICOSQ recovery deactivates RQs. Suspend the recovery to avoid
183 * activating XSKRQ in the middle of recovery.
184 */
185 mlx5e_reporter_icosq_suspend_recovery(c);
186 set_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
187 mlx5e_reporter_icosq_resume_recovery(c);
188
189 /* TX queue is created active. */
190 }
191
192 void mlx5e_deactivate_xsk(struct mlx5e_channel *c)
193 {
194 /* ICOSQ recovery may reactivate XSKRQ if clear_bit is called in the
195 * middle of recovery. Suspend the recovery to avoid it.
196 */
197 mlx5e_reporter_icosq_suspend_recovery(c);
198 clear_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
199 mlx5e_reporter_icosq_resume_recovery(c);
200 synchronize_net(); /* Sync with NAPI to prevent mlx5e_post_rx_wqes. */
201
202 /* TX queue is disabled on close. */
203 }