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
8 /* It matches XDP_UMEM_MIN_CHUNK_SIZE, but as this constant is private and may
9 * change unexpectedly, and mlx5e has a minimum valid stride size for striding
10 * RQ, keep this check in the driver.
11 */
12 #define MLX5E_MIN_XSK_CHUNK_SIZE 2048
13
mlx5e_validate_xsk_param(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5_core_dev * mdev)14 bool mlx5e_validate_xsk_param(struct mlx5e_params *params,
15 struct mlx5e_xsk_param *xsk,
16 struct mlx5_core_dev *mdev)
17 {
18 /* AF_XDP doesn't support frames larger than PAGE_SIZE. */
19 if (xsk->chunk_size > PAGE_SIZE ||
20 xsk->chunk_size < MLX5E_MIN_XSK_CHUNK_SIZE)
21 return false;
22
23 /* Current MTU and XSK headroom don't allow packets to fit the frames. */
24 if (mlx5e_rx_get_min_frag_sz(params, xsk) > xsk->chunk_size)
25 return false;
26
27 /* frag_sz is different for regular and XSK RQs, so ensure that linear
28 * SKB mode is possible.
29 */
30 switch (params->rq_wq_type) {
31 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
32 return mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk);
33 default: /* MLX5_WQ_TYPE_CYCLIC */
34 return mlx5e_rx_is_linear_skb(params, xsk);
35 }
36 }
37
mlx5e_build_xsk_cparam(struct mlx5e_priv * priv,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_channel_param * cparam)38 static void mlx5e_build_xsk_cparam(struct mlx5e_priv *priv,
39 struct mlx5e_params *params,
40 struct mlx5e_xsk_param *xsk,
41 struct mlx5e_channel_param *cparam)
42 {
43 mlx5e_build_rq_param(priv, params, xsk, &cparam->rq);
44 mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
45 }
46
mlx5e_open_xsk(struct mlx5e_priv * priv,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct xsk_buff_pool * pool,struct mlx5e_channel * c)47 int mlx5e_open_xsk(struct mlx5e_priv *priv, struct mlx5e_params *params,
48 struct mlx5e_xsk_param *xsk, struct xsk_buff_pool *pool,
49 struct mlx5e_channel *c)
50 {
51 struct mlx5e_channel_param *cparam;
52 int err;
53
54 if (!mlx5e_validate_xsk_param(params, xsk, priv->mdev))
55 return -EINVAL;
56
57 cparam = kvzalloc(sizeof(*cparam), GFP_KERNEL);
58 if (!cparam)
59 return -ENOMEM;
60
61 mlx5e_build_xsk_cparam(priv, params, xsk, cparam);
62
63 err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rq.cqp, &c->xskrq.cq);
64 if (unlikely(err))
65 goto err_free_cparam;
66
67 err = mlx5e_open_rq(c, params, &cparam->rq, xsk, pool, &c->xskrq);
68 if (unlikely(err))
69 goto err_close_rx_cq;
70
71 err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->xdp_sq.cqp, &c->xsksq.cq);
72 if (unlikely(err))
73 goto err_close_rq;
74
75 /* Create a separate SQ, so that when the buff pool is disabled, we could
76 * close this SQ safely and stop receiving CQEs. In other case, e.g., if
77 * the XDPSQ was used instead, we might run into trouble when the buff pool
78 * is disabled and then reenabled, but the SQ continues receiving CQEs
79 * from the old buff pool.
80 */
81 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, pool, &c->xsksq, true);
82 if (unlikely(err))
83 goto err_close_tx_cq;
84
85 kvfree(cparam);
86
87 set_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
88
89 return 0;
90
91 err_close_tx_cq:
92 mlx5e_close_cq(&c->xsksq.cq);
93
94 err_close_rq:
95 mlx5e_close_rq(&c->xskrq);
96
97 err_close_rx_cq:
98 mlx5e_close_cq(&c->xskrq.cq);
99
100 err_free_cparam:
101 kvfree(cparam);
102
103 return err;
104 }
105
mlx5e_close_xsk(struct mlx5e_channel * c)106 void mlx5e_close_xsk(struct mlx5e_channel *c)
107 {
108 clear_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
109 synchronize_net(); /* Sync with the XSK wakeup and with NAPI. */
110
111 mlx5e_close_rq(&c->xskrq);
112 mlx5e_close_cq(&c->xskrq.cq);
113 mlx5e_close_xdpsq(&c->xsksq);
114 mlx5e_close_cq(&c->xsksq.cq);
115
116 memset(&c->xskrq, 0, sizeof(c->xskrq));
117 memset(&c->xsksq, 0, sizeof(c->xsksq));
118 }
119
mlx5e_activate_xsk(struct mlx5e_channel * c)120 void mlx5e_activate_xsk(struct mlx5e_channel *c)
121 {
122 set_bit(MLX5E_RQ_STATE_ENABLED, &c->xskrq.state);
123 /* TX queue is created active. */
124
125 spin_lock_bh(&c->async_icosq_lock);
126 mlx5e_trigger_irq(&c->async_icosq);
127 spin_unlock_bh(&c->async_icosq_lock);
128 }
129
mlx5e_deactivate_xsk(struct mlx5e_channel * c)130 void mlx5e_deactivate_xsk(struct mlx5e_channel *c)
131 {
132 mlx5e_deactivate_rq(&c->xskrq);
133 /* TX queue is disabled on close. */
134 }
135
mlx5e_redirect_xsk_rqt(struct mlx5e_priv * priv,u16 ix,u32 rqn)136 static int mlx5e_redirect_xsk_rqt(struct mlx5e_priv *priv, u16 ix, u32 rqn)
137 {
138 struct mlx5e_redirect_rqt_param direct_rrp = {
139 .is_rss = false,
140 {
141 .rqn = rqn,
142 },
143 };
144
145 u32 rqtn = priv->xsk_tir[ix].rqt.rqtn;
146
147 return mlx5e_redirect_rqt(priv, rqtn, 1, direct_rrp);
148 }
149
mlx5e_xsk_redirect_rqt_to_channel(struct mlx5e_priv * priv,struct mlx5e_channel * c)150 int mlx5e_xsk_redirect_rqt_to_channel(struct mlx5e_priv *priv, struct mlx5e_channel *c)
151 {
152 return mlx5e_redirect_xsk_rqt(priv, c->ix, c->xskrq.rqn);
153 }
154
mlx5e_xsk_redirect_rqt_to_drop(struct mlx5e_priv * priv,u16 ix)155 int mlx5e_xsk_redirect_rqt_to_drop(struct mlx5e_priv *priv, u16 ix)
156 {
157 return mlx5e_redirect_xsk_rqt(priv, ix, priv->drop_rq.rqn);
158 }
159
mlx5e_xsk_redirect_rqts_to_channels(struct mlx5e_priv * priv,struct mlx5e_channels * chs)160 int mlx5e_xsk_redirect_rqts_to_channels(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
161 {
162 int err, i;
163
164 if (!priv->xsk.refcnt)
165 return 0;
166
167 for (i = 0; i < chs->num; i++) {
168 struct mlx5e_channel *c = chs->c[i];
169
170 if (!test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
171 continue;
172
173 err = mlx5e_xsk_redirect_rqt_to_channel(priv, c);
174 if (unlikely(err))
175 goto err_stop;
176 }
177
178 return 0;
179
180 err_stop:
181 for (i--; i >= 0; i--) {
182 if (!test_bit(MLX5E_CHANNEL_STATE_XSK, chs->c[i]->state))
183 continue;
184
185 mlx5e_xsk_redirect_rqt_to_drop(priv, i);
186 }
187
188 return err;
189 }
190
mlx5e_xsk_redirect_rqts_to_drop(struct mlx5e_priv * priv,struct mlx5e_channels * chs)191 void mlx5e_xsk_redirect_rqts_to_drop(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
192 {
193 int i;
194
195 if (!priv->xsk.refcnt)
196 return;
197
198 for (i = 0; i < chs->num; i++) {
199 if (!test_bit(MLX5E_CHANNEL_STATE_XSK, chs->c[i]->state))
200 continue;
201
202 mlx5e_xsk_redirect_rqt_to_drop(priv, i);
203 }
204 }
205