1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
9 #include <rte_memory.h>
10 #include <rte_malloc.h>
11 #include <rte_regexdev.h>
12 #include <rte_regexdev_core.h>
13 #include <rte_regexdev_driver.h>
16 #include <mlx5_common.h>
17 #include <mlx5_glue.h>
18 #include <mlx5_devx_cmds.h>
20 #include <mlx5_common_os.h>
21 #include <mlx5_common_devx.h>
23 #include "mlx5_regex.h"
24 #include "mlx5_regex_utils.h"
25 #include "mlx5_rxp_csrs.h"
28 #define MLX5_REGEX_NUM_WQE_PER_PAGE (4096/64)
31 * Returns the number of qp obj to be created.
34 * The number of descriptors for the queue.
37 * The number of obj to be created.
40 regex_ctrl_get_nb_obj(uint16_t nb_desc)
42 return ((nb_desc / MLX5_REGEX_NUM_WQE_PER_PAGE) +
43 !!(nb_desc % MLX5_REGEX_NUM_WQE_PER_PAGE));
50 * Pointer to the CQ to be destroyed.
53 * 0 on success, a negative errno value otherwise and rte_errno is set.
56 regex_ctrl_destroy_cq(struct mlx5_regex_cq *cq)
58 mlx5_devx_cq_destroy(&cq->cq_obj);
59 memset(cq, 0, sizeof(*cq));
64 * create the CQ object.
67 * Pointer to the priv object.
69 * Pointer to the CQ to be created.
72 * 0 on success, a negative errno value otherwise and rte_errno is set.
75 regex_ctrl_create_cq(struct mlx5_regex_priv *priv, struct mlx5_regex_cq *cq)
77 struct mlx5_devx_cq_attr attr = {
78 .uar_page_id = priv->uar->page_id,
83 ret = mlx5_devx_cq_create(priv->ctx, &cq->cq_obj, cq->log_nb_desc,
84 &attr, SOCKET_ID_ANY);
86 DRV_LOG(ERR, "Can't create CQ object.");
87 memset(cq, 0, sizeof(*cq));
94 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
96 regex_get_pdn(void *pd, uint32_t *pdn)
98 struct mlx5dv_obj obj;
99 struct mlx5dv_pd pd_info;
103 obj.pd.out = &pd_info;
104 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
106 DRV_LOG(DEBUG, "Fail to get PD object info");
115 * create the SQ object.
118 * Pointer to the priv object.
120 * Pointer to the QP element
122 * The index of the queue.
124 * Log 2 of the number of descriptors to be used.
127 * 0 on success, a negative errno value otherwise and rte_errno is set.
130 regex_ctrl_create_sq(struct mlx5_regex_priv *priv, struct mlx5_regex_qp *qp,
131 uint16_t q_ind, uint16_t log_nb_desc)
133 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
134 struct mlx5_devx_create_sq_attr attr = { 0 };
135 struct mlx5_devx_modify_sq_attr modify_attr = { 0 };
136 struct mlx5_devx_wq_attr *wq_attr = &attr.wq_attr;
137 struct mlx5_devx_dbr_page *dbr_page = NULL;
138 struct mlx5_regex_sq *sq = &qp->sqs[q_ind];
144 sq->log_nb_desc = log_nb_desc;
145 sq_size = 1 << sq->log_nb_desc;
146 sq->dbr_offset = mlx5_get_dbr(priv->ctx, &priv->dbrpgs, &dbr_page);
147 if (sq->dbr_offset < 0) {
148 DRV_LOG(ERR, "Can't allocate sq door bell record.");
152 sq->dbr_umem = mlx5_os_get_umem_id(dbr_page->umem);
153 sq->dbr = (uint32_t *)((uintptr_t)dbr_page->dbrs +
154 (uintptr_t)sq->dbr_offset);
156 buf = rte_calloc(NULL, 1, 64 * sq_size, 4096);
158 DRV_LOG(ERR, "Can't allocate wqe buffer.");
163 sq->wqe_umem = mlx5_glue->devx_umem_reg(priv->ctx, buf, 64 * sq_size,
168 DRV_LOG(ERR, "Can't register wqe mem.");
172 attr.state = MLX5_SQC_STATE_RST;
175 attr.user_index = q_ind;
176 attr.cqn = qp->cq.cq_obj.cq->id;
177 wq_attr->uar_page = priv->uar->page_id;
178 regex_get_pdn(priv->pd, &pd_num);
179 wq_attr->pd = pd_num;
180 wq_attr->wq_type = MLX5_WQ_TYPE_CYCLIC;
181 wq_attr->dbr_umem_id = sq->dbr_umem;
182 wq_attr->dbr_addr = sq->dbr_offset;
183 wq_attr->dbr_umem_valid = 1;
184 wq_attr->wq_umem_id = mlx5_os_get_umem_id(sq->wqe_umem);
185 wq_attr->wq_umem_offset = 0;
186 wq_attr->wq_umem_valid = 1;
187 wq_attr->log_wq_stride = 6;
188 wq_attr->log_wq_sz = sq->log_nb_desc;
189 sq->obj = mlx5_devx_cmd_create_sq(priv->ctx, &attr);
191 DRV_LOG(ERR, "Can't create sq object.");
195 modify_attr.state = MLX5_SQC_STATE_RDY;
196 ret = mlx5_devx_cmd_modify_sq(sq->obj, &modify_attr);
198 DRV_LOG(ERR, "Can't change sq state to ready.");
206 mlx5_glue->devx_umem_dereg(sq->wqe_umem);
210 mlx5_release_dbr(&priv->dbrpgs, sq->dbr_umem, sq->dbr_offset);
217 DRV_LOG(ERR, "Cannot get pdn - no DV support.");
223 * Destroy the SQ object.
226 * Pointer to the priv object.
228 * Pointer to the QP element
230 * The index of the queue.
233 * 0 on success, a negative errno value otherwise and rte_errno is set.
236 regex_ctrl_destroy_sq(struct mlx5_regex_priv *priv, struct mlx5_regex_qp *qp,
239 struct mlx5_regex_sq *sq = &qp->sqs[q_ind];
242 mlx5_glue->devx_umem_dereg(sq->wqe_umem);
246 rte_free((void *)(uintptr_t)sq->wqe);
249 if (sq->dbr_offset) {
250 mlx5_release_dbr(&priv->dbrpgs, sq->dbr_umem, sq->dbr_offset);
254 mlx5_devx_cmd_destroy(sq->obj);
264 * Pointer to RegEx dev structure.
266 * The queue index to setup.
268 * The queue requested configuration.
271 * 0 on success, a negative errno value otherwise and rte_errno is set.
274 mlx5_regex_qp_setup(struct rte_regexdev *dev, uint16_t qp_ind,
275 const struct rte_regexdev_qp_conf *cfg)
277 struct mlx5_regex_priv *priv = dev->data->dev_private;
278 struct mlx5_regex_qp *qp;
280 int nb_sq_config = 0;
284 qp = &priv->qps[qp_ind];
285 qp->flags = cfg->qp_conf_flags;
286 qp->cq.log_nb_desc = rte_log2_u32(cfg->nb_desc);
287 qp->nb_desc = 1 << qp->cq.log_nb_desc;
288 if (qp->flags & RTE_REGEX_QUEUE_PAIR_CFG_OOS_F)
289 qp->nb_obj = regex_ctrl_get_nb_obj(qp->nb_desc);
292 qp->sqs = rte_malloc(NULL,
293 qp->nb_obj * sizeof(struct mlx5_regex_sq), 64);
295 DRV_LOG(ERR, "Can't allocate sq array memory.");
299 log_desc = rte_log2_u32(qp->nb_desc / qp->nb_obj);
300 ret = regex_ctrl_create_cq(priv, &qp->cq);
302 DRV_LOG(ERR, "Can't create cq.");
305 for (i = 0; i < qp->nb_obj; i++) {
306 ret = regex_ctrl_create_sq(priv, qp, i, log_desc);
308 DRV_LOG(ERR, "Can't create sq.");
314 ret = mlx5_mr_btree_init(&qp->mr_ctrl.cache_bh, MLX5_MR_BTREE_CACHE_N,
317 DRV_LOG(ERR, "Error setting up mr btree");
321 ret = mlx5_regexdev_setup_fastpath(priv, qp_ind);
323 DRV_LOG(ERR, "Error setting up fastpath");
329 mlx5_mr_btree_free(&qp->mr_ctrl.cache_bh);
331 for (i = 0; i < nb_sq_config; i++)
332 regex_ctrl_destroy_sq(priv, qp, i);
333 regex_ctrl_destroy_cq(&qp->cq);