699cc03ffe346ee02413b9fdeb4d35bae15fea7e
[dpdk.git] / drivers / regex / mlx5 / mlx5_regex_fastpath.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <unistd.h>
6 #include <sys/mman.h>
7
8 #include <rte_malloc.h>
9 #include <rte_log.h>
10 #include <rte_errno.h>
11 #include <rte_bus_pci.h>
12 #include <rte_pci.h>
13 #include <rte_regexdev_driver.h>
14 #include <rte_mbuf.h>
15
16 #include <infiniband/mlx5dv.h>
17 #include <mlx5_glue.h>
18 #include <mlx5_common.h>
19 #include <mlx5_prm.h>
20 #include <strings.h>
21
22 #include "mlx5_regex_utils.h"
23 #include "mlx5_rxp.h"
24 #include "mlx5_regex.h"
25
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
34
35 static inline uint32_t
36 sq_size_get(struct mlx5_regex_sq *sq)
37 {
38         return (1U << sq->log_nb_desc);
39 }
40
41 struct mlx5_regex_job {
42         uint64_t user_id;
43         uint8_t *input;
44         volatile uint8_t *output;
45         volatile uint8_t *metadata;
46 } __rte_cached_aligned;
47
48 static inline void
49 set_data_seg(struct mlx5_wqe_data_seg *seg,
50              uint32_t length, uint32_t lkey,
51              uintptr_t address)
52 {
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);
56 }
57
58 static inline void
59 set_metadata_seg(struct mlx5_wqe_metadata_seg *seg,
60                  uint32_t mmo_control_31_0, uint32_t lkey,
61                  uintptr_t address)
62 {
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);
66 }
67
68 static inline void
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)
72 {
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);
79 }
80
81 static inline void
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)
85 {
86         seg->opmod_idx_opcode = rte_cpu_to_be_32(((uint32_t)opmod << 24) |
87                                                  ((uint32_t)pi << 8) |
88                                                  opcode);
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;
92         seg->imm = imm;
93 }
94
95 static inline void
96 prep_one(struct mlx5_regex_sq *sq, struct rte_regex_ops *op,
97          struct mlx5_regex_job *job)
98 {
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 */
102
103         memcpy(job->input,
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,
108                          0, ds, 0, 0);
109         set_regex_ctrl_seg(wqe + 12, 0, op->group_id0, op->group_id1,
110                            op->group_id2,
111                            op->group_id3, 0);
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;
118         sq->db_pi = sq->pi;
119         sq->pi = (sq->pi + 1) & MLX5_REGEX_MAX_WQE_INDEX;
120 }
121
122 static inline void
123 send_doorbell(struct mlx5dv_devx_uar *uar, struct mlx5_regex_sq *sq)
124 {
125         size_t wqe_offset = (sq->db_pi & (sq_size_get(sq) - 1)) *
126                 MLX5_SEND_WQE_BB;
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);
131         rte_cio_wmb();
132         sq->dbr[MLX5_SND_DBR] = rte_cpu_to_be_32((sq->db_pi + 1) &
133                                                  MLX5_REGEX_MAX_WQE_INDEX);
134         rte_wmb();
135         *doorbell_addr = *(volatile uint64_t *)wqe;
136         rte_wmb();
137 }
138
139 static inline int
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 <
143                         sq_size_get(sq) :
144                         sq->pi - sq->ci < sq_size_get(sq);
145 }
146
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;
150 }
151
152 uint16_t
153 mlx5_regexdev_enqueue(struct rte_regexdev *dev, uint16_t qp_id,
154                       struct rte_regex_ops **ops, uint16_t nb_ops)
155 {
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;
160
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]);
167                         i++;
168                         if (unlikely(i == nb_ops)) {
169                                 send_doorbell(priv->uar, sq);
170                                 goto out;
171                         }
172                 }
173                 queue->free_sqs &= ~(1 << sqid);
174                 send_doorbell(priv->uar, sq);
175         }
176
177 out:
178         queue->pi += i;
179         return i;
180 }
181
182 static void
183 setup_sqs(struct mlx5_regex_qp *queue)
184 {
185         size_t sqid, entry;
186         uint32_t job_id;
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];
193
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);
207                         wqe += 64;
208                 }
209                 queue->free_sqs |= 1 << sqid;
210         }
211 }
212
213 static int
214 setup_buffers(struct mlx5_regex_qp *qp, struct ibv_pd *pd)
215 {
216         uint32_t i;
217         int err;
218
219         void *ptr = rte_calloc(__func__, qp->nb_desc,
220                                MLX5_REGEX_METADATA_SIZE,
221                                MLX5_REGEX_METADATA_SIZE);
222         if (!ptr)
223                 return -ENOMEM;
224
225         qp->metadata = mlx5_glue->reg_mr(pd, ptr,
226                                          MLX5_REGEX_METADATA_SIZE*qp->nb_desc,
227                                          IBV_ACCESS_LOCAL_WRITE);
228         if (!qp->metadata) {
229                 rte_free(ptr);
230                 return -EINVAL;
231         }
232         ptr = rte_calloc(__func__, qp->nb_desc,
233                          MLX5_REGEX_MAX_INPUT,
234                          MLX5_REGEX_MAX_INPUT);
235
236         if (!ptr) {
237                 err = -ENOMEM;
238                 goto err_input;
239         }
240         qp->inputs = mlx5_glue->reg_mr(pd, ptr,
241                                        MLX5_REGEX_MAX_INPUT*qp->nb_desc,
242                                        IBV_ACCESS_LOCAL_WRITE);
243         if (!qp->inputs) {
244                 rte_free(ptr);
245                 err = -EINVAL;
246                 goto err_input;
247         }
248
249         ptr = rte_calloc(__func__, qp->nb_desc,
250                          MLX5_REGEX_MAX_OUTPUT,
251                          MLX5_REGEX_MAX_OUTPUT);
252         if (!ptr) {
253                 err = -ENOMEM;
254                 goto err_output;
255         }
256         qp->outputs = mlx5_glue->reg_mr(pd, ptr,
257                                         MLX5_REGEX_MAX_OUTPUT * qp->nb_desc,
258                                         IBV_ACCESS_LOCAL_WRITE);
259         if (!qp->outputs) {
260                 rte_free(ptr);
261                 err = -EINVAL;
262                 goto err_output;
263         }
264
265         /* distribute buffers to jobs */
266         for (i = 0; i < qp->nb_desc; i++) {
267                 qp->jobs[i].input =
268                         (uint8_t *)qp->inputs->addr +
269                         (i % qp->nb_desc) * MLX5_REGEX_MAX_INPUT;
270                 qp->jobs[i].output =
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;
276         }
277         return 0;
278
279 err_output:
280         ptr = qp->inputs->addr;
281         rte_free(ptr);
282         mlx5_glue->dereg_mr(qp->inputs);
283 err_input:
284         ptr = qp->metadata->addr;
285         rte_free(ptr);
286         mlx5_glue->dereg_mr(qp->metadata);
287         return err;
288 }
289
290 int
291 mlx5_regexdev_setup_fastpath(struct mlx5_regex_priv *priv, uint32_t qp_id)
292 {
293         struct mlx5_regex_qp *qp = &priv->qps[qp_id];
294         int err;
295
296         qp->jobs = rte_calloc(__func__, qp->nb_desc, sizeof(*qp->jobs),
297                               sizeof(*qp->jobs));
298         if (!qp->jobs)
299                 return -ENOMEM;
300         err = setup_buffers(qp, priv->pd);
301         if (err)
302                 return err;
303         setup_sqs(qp);
304         return 0;
305 }