net/mlx5e: Refactor build channel params
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / xsk / setup.c
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
7 /* It matches XDP_UMEM_MIN_CHUNK_SIZE, but as this constant is private and may
8  * change unexpectedly, and mlx5e has a minimum valid stride size for striding
9  * RQ, keep this check in the driver.
10  */
11 #define MLX5E_MIN_XSK_CHUNK_SIZE 2048
12
13 bool mlx5e_validate_xsk_param(struct mlx5e_params *params,
14                               struct mlx5e_xsk_param *xsk,
15                               struct mlx5_core_dev *mdev)
16 {
17         /* AF_XDP doesn't support frames larger than PAGE_SIZE. */
18         if (xsk->chunk_size > PAGE_SIZE ||
19                         xsk->chunk_size < MLX5E_MIN_XSK_CHUNK_SIZE)
20                 return false;
21
22         /* Current MTU and XSK headroom don't allow packets to fit the frames. */
23         if (mlx5e_rx_get_min_frag_sz(params, xsk) > xsk->chunk_size)
24                 return false;
25
26         /* frag_sz is different for regular and XSK RQs, so ensure that linear
27          * SKB mode is possible.
28          */
29         switch (params->rq_wq_type) {
30         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
31                 return mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk);
32         default: /* MLX5_WQ_TYPE_CYCLIC */
33                 return mlx5e_rx_is_linear_skb(params, xsk);
34         }
35 }
36
37 static void mlx5e_build_xsk_cparam(struct mlx5e_priv *priv,
38                                    struct mlx5e_params *params,
39                                    struct mlx5e_xsk_param *xsk,
40                                    struct mlx5e_channel_param *cparam)
41 {
42         mlx5e_build_rq_param(priv, params, xsk, &cparam->rq);
43         mlx5e_build_xdpsq_param(priv, params, &cparam->xdp_sq);
44 }
45
46 int mlx5e_open_xsk(struct mlx5e_priv *priv, struct mlx5e_params *params,
47                    struct mlx5e_xsk_param *xsk, struct xdp_umem *umem,
48                    struct mlx5e_channel *c)
49 {
50         struct mlx5e_channel_param *cparam;
51         int err;
52
53         if (!mlx5e_validate_xsk_param(params, xsk, priv->mdev))
54                 return -EINVAL;
55
56         cparam = kvzalloc(sizeof(*cparam), GFP_KERNEL);
57         if (!cparam)
58                 return -ENOMEM;
59
60         mlx5e_build_xsk_cparam(priv, params, xsk, cparam);
61
62         err = mlx5e_open_cq(c, params->rx_cq_moderation, &cparam->rq.cqp, &c->xskrq.cq);
63         if (unlikely(err))
64                 goto err_free_cparam;
65
66         err = mlx5e_open_rq(c, params, &cparam->rq, xsk, umem, &c->xskrq);
67         if (unlikely(err))
68                 goto err_close_rx_cq;
69
70         err = mlx5e_open_cq(c, params->tx_cq_moderation, &cparam->xdp_sq.cqp, &c->xsksq.cq);
71         if (unlikely(err))
72                 goto err_close_rq;
73
74         /* Create a separate SQ, so that when the UMEM is disabled, we could
75          * close this SQ safely and stop receiving CQEs. In other case, e.g., if
76          * the XDPSQ was used instead, we might run into trouble when the UMEM
77          * is disabled and then reenabled, but the SQ continues receiving CQEs
78          * from the old UMEM.
79          */
80         err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, umem, &c->xsksq, true);
81         if (unlikely(err))
82                 goto err_close_tx_cq;
83
84         kvfree(cparam);
85
86         set_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
87
88         return 0;
89
90 err_close_tx_cq:
91         mlx5e_close_cq(&c->xsksq.cq);
92
93 err_close_rq:
94         mlx5e_close_rq(&c->xskrq);
95
96 err_close_rx_cq:
97         mlx5e_close_cq(&c->xskrq.cq);
98
99 err_free_cparam:
100         kvfree(cparam);
101
102         return err;
103 }
104
105 void mlx5e_close_xsk(struct mlx5e_channel *c)
106 {
107         clear_bit(MLX5E_CHANNEL_STATE_XSK, c->state);
108         napi_synchronize(&c->napi);
109         synchronize_rcu(); /* Sync with the XSK wakeup. */
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
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(&c->async_icosq_lock);
126         mlx5e_trigger_irq(&c->async_icosq);
127         spin_unlock(&c->async_icosq_lock);
128 }
129
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
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
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
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
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
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 }