regex/mlx5: add completion queue creation
[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         buf = rte_calloc(NULL, 1, sizeof(struct mlx5_cqe) * cq_size, 4096);
109         if (!buf) {
110                 DRV_LOG(ERR, "Can't allocate cqe buffer.");
111                 rte_errno  = ENOMEM;
112                 goto error;
113         }
114         cq->cqe = buf;
115         for (i = 0; i < cq_size; i++)
116                 cq->cqe[i].op_own = 0xff;
117         cq->cqe_umem = mlx5_glue->devx_umem_reg(priv->ctx, buf,
118                                                 sizeof(struct mlx5_cqe) *
119                                                 cq_size, 7);
120         if (!cq->cqe_umem) {
121                 DRV_LOG(ERR, "Can't register cqe mem.");
122                 rte_errno  = ENOMEM;
123                 goto error;
124         }
125         attr.db_umem_offset = cq->dbr_offset;
126         attr.db_umem_id = cq->dbr_umem;
127         attr.q_umem_id = mlx5_os_get_umem_id(cq->cqe_umem);
128         attr.log_cq_size = cq->log_nb_desc;
129         attr.uar_page_id = priv->uar->page_id;
130         attr.log_page_size = rte_log2_u32(pgsize);
131         cq->obj = mlx5_devx_cmd_create_cq(priv->ctx, &attr);
132         if (!cq->obj) {
133                 DRV_LOG(ERR, "Can't create cq object.");
134                 rte_errno  = ENOMEM;
135                 goto error;
136         }
137         return 0;
138 error:
139         if (cq->cqe_umem)
140                 mlx5_glue->devx_umem_dereg(cq->cqe_umem);
141         if (buf)
142                 rte_free(buf);
143         if (cq->dbr_offset)
144                 mlx5_release_dbr(&priv->dbrpgs, cq->dbr_umem, cq->dbr_offset);
145         return -rte_errno;
146 }
147
148 /**
149  * Setup the qp.
150  *
151  * @param dev
152  *   Pointer to RegEx dev structure.
153  * @param qp_ind
154  *   The queue index to setup.
155  * @param cfg
156  *   The queue requested configuration.
157  *
158  * @return
159  *   0 on success, a negative errno value otherwise and rte_errno is set.
160  */
161 int
162 mlx5_regex_qp_setup(struct rte_regexdev *dev, uint16_t qp_ind,
163                     const struct rte_regexdev_qp_conf *cfg)
164 {
165         struct mlx5_regex_priv *priv = dev->data->dev_private;
166         struct mlx5_regex_qp *qp;
167         int ret;
168
169         qp = &priv->qps[qp_ind];
170         qp->flags = cfg->qp_conf_flags;
171         qp->cq.log_nb_desc = rte_log2_u32(cfg->nb_desc);
172         qp->nb_desc = 1 << qp->cq.log_nb_desc;
173         if (qp->flags & RTE_REGEX_QUEUE_PAIR_CFG_OOS_F)
174                 qp->nb_obj = regex_ctrl_get_nb_obj(qp->nb_desc);
175         else
176                 qp->nb_obj = 1;
177         qp->sqs = rte_malloc(NULL,
178                              qp->nb_obj * sizeof(struct mlx5_regex_sq), 64);
179         if (!qp->sqs) {
180                 DRV_LOG(ERR, "Can't allocate sq array memory.");
181                 rte_errno  = ENOMEM;
182                 return -rte_errno;
183         }
184         ret = regex_ctrl_create_cq(priv, &qp->cq);
185         if (ret) {
186                 DRV_LOG(ERR, "Can't create cq.");
187                 goto error;
188         }
189         return 0;
190
191 error:
192         regex_ctrl_destroy_cq(priv, &qp->cq);
193         return -rte_errno;
194
195 }