83d390abcb984e8bf47b4e389555c5a13a224bc4
[dpdk.git] / drivers / regex / mlx5 / mlx5_regex_control.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <errno.h>
6
7 #include <rte_log.h>
8 #include <rte_errno.h>
9 #include <rte_malloc.h>
10 #include <rte_regexdev.h>
11 #include <rte_regexdev_core.h>
12 #include <rte_regexdev_driver.h>
13
14 #include <mlx5_common.h>
15 #include <mlx5_glue.h>
16 #include <mlx5_devx_cmds.h>
17 #include <mlx5_prm.h>
18 #include <mlx5_common_os.h>
19
20 #include "mlx5_regex.h"
21 #include "mlx5_regex_utils.h"
22 #include "mlx5_rxp_csrs.h"
23 #include "mlx5_rxp.h"
24
25 #define MLX5_REGEX_NUM_WQE_PER_PAGE (4096/64)
26
27 /**
28  * Returns the number of qp obj to be created.
29  *
30  * @param nb_desc
31  *   The number of descriptors for the queue.
32  *
33  * @return
34  *   The number of obj to be created.
35  */
36 static uint16_t
37 regex_ctrl_get_nb_obj(uint16_t nb_desc)
38 {
39         return ((nb_desc / MLX5_REGEX_NUM_WQE_PER_PAGE) +
40                 !!(nb_desc % MLX5_REGEX_NUM_WQE_PER_PAGE));
41 }
42
43 /**
44  * destroy CQ.
45  *
46  * @param priv
47  *   Pointer to the priv object.
48  * @param cp
49  *   Pointer to the CQ to be destroyed.
50  *
51  * @return
52  *   0 on success, a negative errno value otherwise and rte_errno is set.
53  */
54 static int
55 regex_ctrl_destroy_cq(struct mlx5_regex_priv *priv, struct mlx5_regex_cq *cq)
56 {
57         if (cq->cqe_umem) {
58                 mlx5_glue->devx_umem_dereg(cq->cqe_umem);
59                 cq->cqe_umem = NULL;
60         }
61         if (cq->cqe) {
62                 rte_free((void *)(uintptr_t)cq->cqe);
63                 cq->cqe = NULL;
64         }
65         if (cq->dbr_offset) {
66                 mlx5_release_dbr(&priv->dbrpgs, cq->dbr_umem, cq->dbr_offset);
67                 cq->dbr_offset = -1;
68         }
69         if (cq->obj) {
70                 mlx5_devx_cmd_destroy(cq->obj);
71                 cq->obj = NULL;
72         }
73         return 0;
74 }
75
76 /**
77  * create the CQ object.
78  *
79  * @param priv
80  *   Pointer to the priv object.
81  * @param cp
82  *   Pointer to the CQ to be created.
83  *
84  * @return
85  *   0 on success, a negative errno value otherwise and rte_errno is set.
86  */
87 static int
88 regex_ctrl_create_cq(struct mlx5_regex_priv *priv, struct mlx5_regex_cq *cq)
89 {
90         struct mlx5_devx_cq_attr attr = {
91                 .q_umem_valid = 1,
92                 .db_umem_valid = 1,
93                 .eqn = priv->eqn,
94         };
95         struct mlx5_devx_dbr_page *dbr_page = NULL;
96         void *buf = NULL;
97         size_t pgsize = sysconf(_SC_PAGESIZE);
98         uint32_t cq_size = 1 << cq->log_nb_desc;
99         uint32_t i;
100
101         cq->dbr_offset = mlx5_get_dbr(priv->ctx, &priv->dbrpgs, &dbr_page);
102         if (cq->dbr_offset < 0) {
103                 DRV_LOG(ERR, "Can't allocate cq door bell record.");
104                 rte_errno  = ENOMEM;
105                 goto error;
106         }
107         cq->dbr_umem = mlx5_os_get_umem_id(dbr_page->umem);
108         cq->dbr = (uint32_t *)((uintptr_t)dbr_page->dbrs +
109                                (uintptr_t)cq->dbr_offset);
110
111         buf = rte_calloc(NULL, 1, sizeof(struct mlx5_cqe) * cq_size, 4096);
112         if (!buf) {
113                 DRV_LOG(ERR, "Can't allocate cqe buffer.");
114                 rte_errno  = ENOMEM;
115                 goto error;
116         }
117         cq->cqe = buf;
118         for (i = 0; i < cq_size; i++)
119                 cq->cqe[i].op_own = 0xff;
120         cq->cqe_umem = mlx5_glue->devx_umem_reg(priv->ctx, buf,
121                                                 sizeof(struct mlx5_cqe) *
122                                                 cq_size, 7);
123         if (!cq->cqe_umem) {
124                 DRV_LOG(ERR, "Can't register cqe mem.");
125                 rte_errno  = ENOMEM;
126                 goto error;
127         }
128         attr.db_umem_offset = cq->dbr_offset;
129         attr.db_umem_id = cq->dbr_umem;
130         attr.q_umem_id = mlx5_os_get_umem_id(cq->cqe_umem);
131         attr.log_cq_size = cq->log_nb_desc;
132         attr.uar_page_id = priv->uar->page_id;
133         attr.log_page_size = rte_log2_u32(pgsize);
134         cq->obj = mlx5_devx_cmd_create_cq(priv->ctx, &attr);
135         if (!cq->obj) {
136                 DRV_LOG(ERR, "Can't create cq object.");
137                 rte_errno  = ENOMEM;
138                 goto error;
139         }
140         return 0;
141 error:
142         if (cq->cqe_umem)
143                 mlx5_glue->devx_umem_dereg(cq->cqe_umem);
144         if (buf)
145                 rte_free(buf);
146         if (cq->dbr_offset)
147                 mlx5_release_dbr(&priv->dbrpgs, cq->dbr_umem, cq->dbr_offset);
148         return -rte_errno;
149 }
150
151 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
152 static int
153 regex_get_pdn(void *pd, uint32_t *pdn)
154 {
155         struct mlx5dv_obj obj;
156         struct mlx5dv_pd pd_info;
157         int ret = 0;
158
159         obj.pd.in = pd;
160         obj.pd.out = &pd_info;
161         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
162         if (ret) {
163                 DRV_LOG(DEBUG, "Fail to get PD object info");
164                 return ret;
165         }
166         *pdn = pd_info.pdn;
167         return 0;
168 }
169 #endif
170
171 /**
172  * create the SQ object.
173  *
174  * @param priv
175  *   Pointer to the priv object.
176  * @param qp
177  *   Pointer to the QP element
178  * @param q_ind
179  *   The index of the queue.
180  * @param log_nb_desc
181  *   Log 2 of the number of descriptors to be used.
182  *
183  * @return
184  *   0 on success, a negative errno value otherwise and rte_errno is set.
185  */
186 static int
187 regex_ctrl_create_sq(struct mlx5_regex_priv *priv, struct mlx5_regex_qp *qp,
188                      uint16_t q_ind, uint16_t log_nb_desc)
189 {
190 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
191         struct mlx5_devx_create_sq_attr attr = { 0 };
192         struct mlx5_devx_modify_sq_attr modify_attr = { 0 };
193         struct mlx5_devx_wq_attr *wq_attr = &attr.wq_attr;
194         struct mlx5_devx_dbr_page *dbr_page = NULL;
195         struct mlx5_regex_sq *sq = &qp->sqs[q_ind];
196         void *buf = NULL;
197         uint32_t sq_size;
198         uint32_t pd_num = 0;
199         int ret;
200
201         sq->log_nb_desc = log_nb_desc;
202         sq_size = 1 << sq->log_nb_desc;
203         sq->dbr_offset = mlx5_get_dbr(priv->ctx, &priv->dbrpgs, &dbr_page);
204         if (sq->dbr_offset < 0) {
205                 DRV_LOG(ERR, "Can't allocate sq door bell record.");
206                 rte_errno  = ENOMEM;
207                 goto error;
208         }
209         sq->dbr_umem = mlx5_os_get_umem_id(dbr_page->umem);
210         sq->dbr = (uint32_t *)((uintptr_t)dbr_page->dbrs +
211                                (uintptr_t)sq->dbr_offset);
212
213         buf = rte_calloc(NULL, 1, 64 * sq_size, 4096);
214         if (!buf) {
215                 DRV_LOG(ERR, "Can't allocate wqe buffer.");
216                 rte_errno  = ENOMEM;
217                 goto error;
218         }
219         sq->wqe = buf;
220         sq->wqe_umem = mlx5_glue->devx_umem_reg(priv->ctx, buf, 64 * sq_size,
221                                                 7);
222         if (!sq->wqe_umem) {
223                 DRV_LOG(ERR, "Can't register wqe mem.");
224                 rte_errno  = ENOMEM;
225                 goto error;
226         }
227         attr.state = MLX5_SQC_STATE_RST;
228         attr.tis_lst_sz = 0;
229         attr.tis_num = 0;
230         attr.user_index = q_ind;
231         attr.cqn = qp->cq.obj->id;
232         wq_attr->uar_page = priv->uar->page_id;
233         regex_get_pdn(priv->pd, &pd_num);
234         wq_attr->pd = pd_num;
235         wq_attr->wq_type = MLX5_WQ_TYPE_CYCLIC;
236         wq_attr->dbr_umem_id = sq->dbr_umem;
237         wq_attr->dbr_addr = sq->dbr_offset;
238         wq_attr->dbr_umem_valid = 1;
239         wq_attr->wq_umem_id = mlx5_os_get_umem_id(sq->wqe_umem);
240         wq_attr->wq_umem_offset = 0;
241         wq_attr->wq_umem_valid = 1;
242         wq_attr->log_wq_stride = 6;
243         wq_attr->log_wq_sz = sq->log_nb_desc;
244         sq->obj = mlx5_devx_cmd_create_sq(priv->ctx, &attr);
245         if (!sq->obj) {
246                 DRV_LOG(ERR, "Can't create sq object.");
247                 rte_errno  = ENOMEM;
248                 goto error;
249         }
250         modify_attr.state = MLX5_SQC_STATE_RDY;
251         ret = mlx5_devx_cmd_modify_sq(sq->obj, &modify_attr);
252         if (ret) {
253                 DRV_LOG(ERR, "Can't change sq state to ready.");
254                 rte_errno  = ENOMEM;
255                 goto error;
256         }
257
258         return 0;
259 error:
260         if (sq->wqe_umem)
261                 mlx5_glue->devx_umem_dereg(sq->wqe_umem);
262         if (buf)
263                 rte_free(buf);
264         if (sq->dbr_offset)
265                 mlx5_release_dbr(&priv->dbrpgs, sq->dbr_umem, sq->dbr_offset);
266         return -rte_errno;
267 #else
268         (void)priv;
269         (void)qp;
270         (void)q_ind;
271         (void)log_nb_desc;
272         DRV_LOG(ERR, "Cannot get pdn - no DV support.");
273         return -ENOTSUP;
274 #endif
275 }
276
277 /**
278  * Destroy the SQ object.
279  *
280  * @param priv
281  *   Pointer to the priv object.
282  * @param qp
283  *   Pointer to the QP element
284  * @param q_ind
285  *   The index of the queue.
286  *
287  * @return
288  *   0 on success, a negative errno value otherwise and rte_errno is set.
289  */
290 static int
291 regex_ctrl_destroy_sq(struct mlx5_regex_priv *priv, struct mlx5_regex_qp *qp,
292                       uint16_t q_ind)
293 {
294         struct mlx5_regex_sq *sq = &qp->sqs[q_ind];
295
296         if (sq->wqe_umem) {
297                 mlx5_glue->devx_umem_dereg(sq->wqe_umem);
298                 sq->wqe_umem = NULL;
299         }
300         if (sq->wqe) {
301                 rte_free((void *)(uintptr_t)sq->wqe);
302                 sq->wqe = NULL;
303         }
304         if (sq->dbr_offset) {
305                 mlx5_release_dbr(&priv->dbrpgs, sq->dbr_umem, sq->dbr_offset);
306                 sq->dbr_offset = -1;
307         }
308         if (sq->obj) {
309                 mlx5_devx_cmd_destroy(sq->obj);
310                 sq->obj = NULL;
311         }
312         return 0;
313 }
314
315 /**
316  * Setup the qp.
317  *
318  * @param dev
319  *   Pointer to RegEx dev structure.
320  * @param qp_ind
321  *   The queue index to setup.
322  * @param cfg
323  *   The queue requested configuration.
324  *
325  * @return
326  *   0 on success, a negative errno value otherwise and rte_errno is set.
327  */
328 int
329 mlx5_regex_qp_setup(struct rte_regexdev *dev, uint16_t qp_ind,
330                     const struct rte_regexdev_qp_conf *cfg)
331 {
332         struct mlx5_regex_priv *priv = dev->data->dev_private;
333         struct mlx5_regex_qp *qp;
334         int i;
335         int ret;
336         uint16_t log_desc;
337
338         qp = &priv->qps[qp_ind];
339         qp->flags = cfg->qp_conf_flags;
340         qp->cq.log_nb_desc = rte_log2_u32(cfg->nb_desc);
341         qp->nb_desc = 1 << qp->cq.log_nb_desc;
342         if (qp->flags & RTE_REGEX_QUEUE_PAIR_CFG_OOS_F)
343                 qp->nb_obj = regex_ctrl_get_nb_obj(qp->nb_desc);
344         else
345                 qp->nb_obj = 1;
346         qp->sqs = rte_malloc(NULL,
347                              qp->nb_obj * sizeof(struct mlx5_regex_sq), 64);
348         if (!qp->sqs) {
349                 DRV_LOG(ERR, "Can't allocate sq array memory.");
350                 rte_errno  = ENOMEM;
351                 return -rte_errno;
352         }
353         log_desc = rte_log2_u32(qp->nb_desc / qp->nb_obj);
354         ret = regex_ctrl_create_cq(priv, &qp->cq);
355         if (ret) {
356                 DRV_LOG(ERR, "Can't create cq.");
357                 goto error;
358         }
359         for (i = 0; i < qp->nb_obj; i++) {
360                 ret = regex_ctrl_create_sq(priv, qp, i, log_desc);
361                 if (ret) {
362                         DRV_LOG(ERR, "Can't create sq.");
363                         goto error;
364                 }
365         }
366         return 0;
367
368 error:
369         regex_ctrl_destroy_cq(priv, &qp->cq);
370         for (i = 0; i < qp->nb_obj; i++)
371                 ret = regex_ctrl_destroy_sq(priv, qp, i);
372         return -rte_errno;
373
374 }