1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
8 #include <rte_malloc.h>
10 #include <rte_errno.h>
11 #include <rte_bus_pci.h>
13 #include <rte_regexdev_driver.h>
16 #include <infiniband/mlx5dv.h>
17 #include <mlx5_glue.h>
18 #include <mlx5_common.h>
22 #include "mlx5_regex_utils.h"
24 #include "mlx5_regex.h"
26 #define MLX5_REGEX_MAX_WQE_INDEX 0xffff
27 #define MLX5_REGEX_METADATA_SIZE 64
28 #define MLX5_REGEX_MAX_INPUT (1 << 14)
29 #define MLX5_REGEX_MAX_OUTPUT (1 << 11)
30 #define MLX5_REGEX_WQE_CTRL_OFFSET 12
31 #define MLX5_REGEX_WQE_METADATA_OFFSET 16
32 #define MLX5_REGEX_WQE_GATHER_OFFSET 32
33 #define MLX5_REGEX_WQE_SCATTER_OFFSET 48
35 static inline uint32_t
36 sq_size_get(struct mlx5_regex_sq *sq)
38 return (1U << sq->log_nb_desc);
41 struct mlx5_regex_job {
44 volatile uint8_t *output;
45 volatile uint8_t *metadata;
46 } __rte_cached_aligned;
49 set_data_seg(struct mlx5_wqe_data_seg *seg,
50 uint32_t length, uint32_t lkey,
53 seg->byte_count = rte_cpu_to_be_32(length);
54 seg->lkey = rte_cpu_to_be_32(lkey);
55 seg->addr = rte_cpu_to_be_64(address);
59 set_metadata_seg(struct mlx5_wqe_metadata_seg *seg,
60 uint32_t mmo_control_31_0, uint32_t lkey,
63 seg->mmo_control_31_0 = htobe32(mmo_control_31_0);
64 seg->lkey = rte_cpu_to_be_32(lkey);
65 seg->addr = rte_cpu_to_be_64(address);
69 set_regex_ctrl_seg(void *seg, uint8_t le, uint16_t subset_id0,
70 uint16_t subset_id1, uint16_t subset_id2,
71 uint16_t subset_id3, uint8_t ctrl)
73 MLX5_SET(regexp_mmo_control, seg, le, le);
74 MLX5_SET(regexp_mmo_control, seg, ctrl, ctrl);
75 MLX5_SET(regexp_mmo_control, seg, subset_id_0, subset_id0);
76 MLX5_SET(regexp_mmo_control, seg, subset_id_1, subset_id1);
77 MLX5_SET(regexp_mmo_control, seg, subset_id_2, subset_id2);
78 MLX5_SET(regexp_mmo_control, seg, subset_id_3, subset_id3);
82 set_wqe_ctrl_seg(struct mlx5_wqe_ctrl_seg *seg, uint16_t pi, uint8_t opcode,
83 uint8_t opmod, uint32_t qp_num, uint8_t fm_ce_se, uint8_t ds,
84 uint8_t signature, uint32_t imm)
86 seg->opmod_idx_opcode = rte_cpu_to_be_32(((uint32_t)opmod << 24) |
89 seg->qpn_ds = rte_cpu_to_be_32((qp_num << 8) | ds);
90 seg->fm_ce_se = fm_ce_se;
91 seg->signature = signature;
96 prep_one(struct mlx5_regex_sq *sq, struct rte_regex_ops *op,
97 struct mlx5_regex_job *job)
99 size_t wqe_offset = (sq->pi & (sq_size_get(sq) - 1)) * MLX5_SEND_WQE_BB;
100 uint8_t *wqe = (uint8_t *)sq->wqe + wqe_offset;
101 int ds = 4; /* ctrl + meta + input + output */
104 rte_pktmbuf_mtod(op->mbuf, void *),
105 rte_pktmbuf_data_len(op->mbuf));
106 set_wqe_ctrl_seg((struct mlx5_wqe_ctrl_seg *)wqe, sq->pi,
107 MLX5_OPCODE_MMO, MLX5_OPC_MOD_MMO_REGEX, sq->obj->id,
109 set_regex_ctrl_seg(wqe + 12, 0, op->group_id0, op->group_id1,
112 struct mlx5_wqe_data_seg *input_seg =
113 (struct mlx5_wqe_data_seg *)(wqe +
114 MLX5_REGEX_WQE_GATHER_OFFSET);
115 input_seg->byte_count =
116 rte_cpu_to_be_32(rte_pktmbuf_data_len(op->mbuf));
117 job->user_id = op->user_id;
119 sq->pi = (sq->pi + 1) & MLX5_REGEX_MAX_WQE_INDEX;
123 send_doorbell(struct mlx5dv_devx_uar *uar, struct mlx5_regex_sq *sq)
125 size_t wqe_offset = (sq->db_pi & (sq_size_get(sq) - 1)) *
127 uint8_t *wqe = (uint8_t *)sq->wqe + wqe_offset;
128 ((struct mlx5_wqe_ctrl_seg *)wqe)->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
129 uint64_t *doorbell_addr =
130 (uint64_t *)((uint8_t *)uar->base_addr + 0x800);
132 sq->dbr[MLX5_SND_DBR] = rte_cpu_to_be_32((sq->db_pi + 1) &
133 MLX5_REGEX_MAX_WQE_INDEX);
135 *doorbell_addr = *(volatile uint64_t *)wqe;
140 can_send(struct mlx5_regex_sq *sq) {
141 return unlikely(sq->ci > sq->pi) ?
142 MLX5_REGEX_MAX_WQE_INDEX + sq->pi - sq->ci <
144 sq->pi - sq->ci < sq_size_get(sq);
147 static inline uint32_t
148 job_id_get(uint32_t qid, size_t sq_size, size_t index) {
149 return qid * sq_size + index % sq_size;
153 mlx5_regexdev_enqueue(struct rte_regexdev *dev, uint16_t qp_id,
154 struct rte_regex_ops **ops, uint16_t nb_ops)
156 struct mlx5_regex_priv *priv = dev->data->dev_private;
157 struct mlx5_regex_qp *queue = &priv->qps[qp_id];
158 struct mlx5_regex_sq *sq;
159 size_t sqid, job_id, i = 0;
161 while ((sqid = ffs(queue->free_sqs))) {
162 sqid--; /* ffs returns 1 for bit 0 */
163 sq = &queue->sqs[sqid];
164 while (can_send(sq)) {
165 job_id = job_id_get(sqid, sq_size_get(sq), sq->pi);
166 prep_one(sq, ops[i], &queue->jobs[job_id]);
168 if (unlikely(i == nb_ops)) {
169 send_doorbell(priv->uar, sq);
173 queue->free_sqs &= ~(1 << sqid);
174 send_doorbell(priv->uar, sq);
183 setup_sqs(struct mlx5_regex_qp *queue)
187 for (sqid = 0; sqid < queue->nb_obj; sqid++) {
188 struct mlx5_regex_sq *sq = &queue->sqs[sqid];
189 uint8_t *wqe = (uint8_t *)sq->wqe;
190 for (entry = 0 ; entry < sq_size_get(sq); entry++) {
191 job_id = sqid * sq_size_get(sq) + entry;
192 struct mlx5_regex_job *job = &queue->jobs[job_id];
194 set_metadata_seg((struct mlx5_wqe_metadata_seg *)
195 (wqe + MLX5_REGEX_WQE_METADATA_OFFSET),
196 0, queue->metadata->lkey,
197 (uintptr_t)job->metadata);
198 set_data_seg((struct mlx5_wqe_data_seg *)
199 (wqe + MLX5_REGEX_WQE_GATHER_OFFSET),
200 0, queue->inputs->lkey,
201 (uintptr_t)job->input);
202 set_data_seg((struct mlx5_wqe_data_seg *)
203 (wqe + MLX5_REGEX_WQE_SCATTER_OFFSET),
204 MLX5_REGEX_MAX_OUTPUT,
205 queue->outputs->lkey,
206 (uintptr_t)job->output);
209 queue->free_sqs |= 1 << sqid;
214 setup_buffers(struct mlx5_regex_qp *qp, struct ibv_pd *pd)
219 void *ptr = rte_calloc(__func__, qp->nb_desc,
220 MLX5_REGEX_METADATA_SIZE,
221 MLX5_REGEX_METADATA_SIZE);
225 qp->metadata = mlx5_glue->reg_mr(pd, ptr,
226 MLX5_REGEX_METADATA_SIZE*qp->nb_desc,
227 IBV_ACCESS_LOCAL_WRITE);
232 ptr = rte_calloc(__func__, qp->nb_desc,
233 MLX5_REGEX_MAX_INPUT,
234 MLX5_REGEX_MAX_INPUT);
240 qp->inputs = mlx5_glue->reg_mr(pd, ptr,
241 MLX5_REGEX_MAX_INPUT*qp->nb_desc,
242 IBV_ACCESS_LOCAL_WRITE);
249 ptr = rte_calloc(__func__, qp->nb_desc,
250 MLX5_REGEX_MAX_OUTPUT,
251 MLX5_REGEX_MAX_OUTPUT);
256 qp->outputs = mlx5_glue->reg_mr(pd, ptr,
257 MLX5_REGEX_MAX_OUTPUT * qp->nb_desc,
258 IBV_ACCESS_LOCAL_WRITE);
265 /* distribute buffers to jobs */
266 for (i = 0; i < qp->nb_desc; i++) {
268 (uint8_t *)qp->inputs->addr +
269 (i % qp->nb_desc) * MLX5_REGEX_MAX_INPUT;
271 (uint8_t *)qp->outputs->addr +
272 (i % qp->nb_desc) * MLX5_REGEX_MAX_OUTPUT;
273 qp->jobs[i].metadata =
274 (uint8_t *)qp->metadata->addr +
275 (i % qp->nb_desc) * MLX5_REGEX_METADATA_SIZE;
280 ptr = qp->inputs->addr;
282 mlx5_glue->dereg_mr(qp->inputs);
284 ptr = qp->metadata->addr;
286 mlx5_glue->dereg_mr(qp->metadata);
291 mlx5_regexdev_setup_fastpath(struct mlx5_regex_priv *priv, uint32_t qp_id)
293 struct mlx5_regex_qp *qp = &priv->qps[qp_id];
296 qp->jobs = rte_calloc(__func__, qp->nb_desc, sizeof(*qp->jobs),
300 err = setup_buffers(qp, priv->pd);