380037e24c333da7d1b9c82035e8c9261c7ea35f
[dpdk.git] / drivers / regex / mlx5 / mlx5_rxp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <rte_log.h>
6 #include <rte_errno.h>
7 #include <rte_malloc.h>
8 #include <rte_regexdev.h>
9 #include <rte_regexdev_core.h>
10 #include <rte_regexdev_driver.h>
11 #include <sys/mman.h>
12
13 #include <mlx5_glue.h>
14 #include <mlx5_devx_cmds.h>
15 #include <mlx5_prm.h>
16 #include <mlx5_common_os.h>
17
18 #include "mlx5_regex.h"
19 #include "mlx5_regex_utils.h"
20 #include "mlx5_rxp_csrs.h"
21 #include "mlx5_rxp.h"
22
23 #define MLX5_REGEX_MAX_MATCHES MLX5_RXP_MAX_MATCHES
24 #define MLX5_REGEX_MAX_PAYLOAD_SIZE MLX5_RXP_MAX_JOB_LENGTH
25 #define MLX5_REGEX_MAX_RULES_PER_GROUP UINT32_MAX
26 #define MLX5_REGEX_MAX_GROUPS MLX5_RXP_MAX_SUBSETS
27
28 #define MLX5_REGEX_RXP_ROF2_LINE_LEN 34
29
30 /* Private Declarations */
31 static int
32 rxp_poll_csr_for_value(struct ibv_context *ctx, uint32_t *value,
33                        uint32_t address, uint32_t expected_value,
34                        uint32_t expected_mask, uint32_t timeout_ms, uint8_t id);
35 static int
36 mlnx_set_database(struct mlx5_regex_priv *priv, uint8_t id, uint8_t db_to_use);
37 static int
38 mlnx_resume_database(struct mlx5_regex_priv *priv, uint8_t id);
39 static int
40 mlnx_update_database(struct mlx5_regex_priv *priv, uint8_t id);
41 static int
42 program_rxp_rules(struct mlx5_regex_priv *priv, const char *buf, uint32_t len,
43                   uint8_t id);
44 static int
45 rxp_init_eng(struct mlx5_regex_priv *priv, uint8_t id);
46 static int
47 rxp_db_setup(struct mlx5_regex_priv *priv);
48 static void
49 rxp_dump_csrs(struct ibv_context *ctx, uint8_t id);
50 static int
51 rxp_start_engine(struct ibv_context *ctx, uint8_t id);
52 static int
53 rxp_stop_engine(struct ibv_context *ctx, uint8_t id);
54
55 static void __rte_unused
56 rxp_dump_csrs(struct ibv_context *ctx __rte_unused, uint8_t id __rte_unused)
57 {
58         uint32_t reg, i;
59
60         /* Main CSRs*/
61         for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
62                 if (mlx5_devx_regex_register_read(ctx, id,
63                                                   (MLX5_RXP_CSR_WIDTH * i) +
64                                                   MLX5_RXP_CSR_BASE_ADDRESS,
65                                                   &reg)) {
66                         DRV_LOG(ERR, "Failed to read Main CSRs Engine %d!", id);
67                         return;
68                 }
69                 DRV_LOG(DEBUG, "RXP Main CSRs (Eng%d) register (%d): %08x",
70                         id, i, reg);
71         }
72         /* RTRU CSRs*/
73         for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
74                 if (mlx5_devx_regex_register_read(ctx, id,
75                                                   (MLX5_RXP_CSR_WIDTH * i) +
76                                                  MLX5_RXP_RTRU_CSR_BASE_ADDRESS,
77                                                   &reg)) {
78                         DRV_LOG(ERR, "Failed to read RTRU CSRs Engine %d!", id);
79                         return;
80                 }
81                 DRV_LOG(DEBUG, "RXP RTRU CSRs (Eng%d) register (%d): %08x",
82                         id, i, reg);
83         }
84         /* STAT CSRs */
85         for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
86                 if (mlx5_devx_regex_register_read(ctx, id,
87                                                   (MLX5_RXP_CSR_WIDTH * i) +
88                                                 MLX5_RXP_STATS_CSR_BASE_ADDRESS,
89                                                   &reg)) {
90                         DRV_LOG(ERR, "Failed to read STAT CSRs Engine %d!", id);
91                         return;
92                 }
93                 DRV_LOG(DEBUG, "RXP STAT CSRs (Eng%d) register (%d): %08x",
94                         id, i, reg);
95         }
96 }
97
98 int
99 mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
100                     struct rte_regexdev_info *info)
101 {
102         info->max_matches = MLX5_REGEX_MAX_MATCHES;
103         info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
104         info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
105         info->max_groups = MLX5_REGEX_MAX_GROUPS;
106         info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
107                               RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
108         info->rule_flags = 0;
109         info->max_queue_pairs = UINT16_MAX;
110         return 0;
111 }
112
113 static int
114 rxp_poll_csr_for_value(struct ibv_context *ctx, uint32_t *value,
115                        uint32_t address, uint32_t expected_value,
116                        uint32_t expected_mask, uint32_t timeout_ms, uint8_t id)
117 {
118         unsigned int i;
119         int ret;
120
121         ret = -EBUSY;
122         for (i = 0; i < timeout_ms; i++) {
123                 if (mlx5_devx_regex_register_read(ctx, id, address, value))
124                         return -1;
125                 if ((*value & expected_mask) == expected_value) {
126                         ret = 0;
127                         break;
128                 }
129                 rte_delay_us(1000);
130         }
131         return ret;
132 }
133
134 static int
135 rxp_start_engine(struct ibv_context *ctx, uint8_t id)
136 {
137         uint32_t ctrl;
138         int ret;
139
140         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
141         if (ret)
142                 return ret;
143         ctrl |= MLX5_RXP_CSR_CTRL_GO;
144         ctrl |= MLX5_RXP_CSR_CTRL_DISABLE_L2C;
145         ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
146         return ret;
147 }
148
149 static int
150 rxp_stop_engine(struct ibv_context *ctx, uint8_t id)
151 {
152         uint32_t ctrl;
153         int ret;
154
155         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
156         if (ret)
157                 return ret;
158         ctrl &= ~MLX5_RXP_CSR_CTRL_GO;
159         ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
160         return ret;
161 }
162
163 static int
164 rxp_init_rtru(struct mlx5_regex_priv *priv, uint8_t id, uint32_t init_bits)
165 {
166         uint32_t ctrl_value;
167         uint32_t poll_value;
168         uint32_t expected_value;
169         uint32_t expected_mask;
170         struct ibv_context *ctx = priv->ctx;
171         int ret = 0;
172
173         /* Read the rtru ctrl CSR. */
174         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
175                                             &ctrl_value);
176         if (ret)
177                 return -1;
178         /* Clear any previous init modes. */
179         ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_MASK);
180         if (ctrl_value & MLX5_RXP_RTRU_CSR_CTRL_INIT) {
181                 ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
182                 mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
183                                                ctrl_value);
184         }
185         /* Set the init_mode bits in the rtru ctrl CSR. */
186         ctrl_value |= init_bits;
187         mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
188                                        ctrl_value);
189         /* Need to sleep for a short period after pulsing the rtru init bit. */
190         rte_delay_us(20000);
191         /* Poll the rtru status CSR until all the init done bits are set. */
192         DRV_LOG(DEBUG, "waiting for RXP rule memory to complete init");
193         /* Set the init bit in the rtru ctrl CSR. */
194         ctrl_value |= MLX5_RXP_RTRU_CSR_CTRL_INIT;
195         mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
196                                        ctrl_value);
197         /* Clear the init bit in the rtru ctrl CSR */
198         ctrl_value &= ~MLX5_RXP_RTRU_CSR_CTRL_INIT;
199         mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
200                                        ctrl_value);
201         /* Check that the following bits are set in the RTRU_CSR. */
202         if (init_bits == MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_L1_L2) {
203                 /* Must be incremental mode */
204                 expected_value = MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE;
205         } else {
206                 expected_value = MLX5_RXP_RTRU_CSR_STATUS_IM_INIT_DONE |
207                         MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE;
208         }
209         if (priv->is_bf2)
210                 expected_value |= MLX5_RXP_RTRU_CSR_STATUS_L2C_INIT_DONE;
211
212
213         expected_mask = expected_value;
214         ret = rxp_poll_csr_for_value(ctx, &poll_value,
215                                      MLX5_RXP_RTRU_CSR_STATUS,
216                                      expected_value, expected_mask,
217                                      MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT, id);
218         if (ret)
219                 return ret;
220         DRV_LOG(DEBUG, "rule memory initialise: 0x%08X", poll_value);
221         /* Clear the init bit in the rtru ctrl CSR */
222         ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
223         mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
224                                        ctrl_value);
225         return 0;
226 }
227
228 static int
229 rxp_parse_line(char *line, uint32_t *type, uint32_t *address, uint64_t *value)
230 {
231         char *cur_pos;
232
233         if (*line == '\0' || *line == '#')
234                 return  1;
235         *type = strtoul(line, &cur_pos, 10);
236         if (*cur_pos != ',' && *cur_pos != '\0')
237                 return -1;
238         *address = strtoul(cur_pos+1, &cur_pos, 16);
239         if (*cur_pos != ',' && *cur_pos != '\0')
240                 return -1;
241         *value = strtoul(cur_pos+1, &cur_pos, 16);
242         if (*cur_pos != ',' && *cur_pos != '\0')
243                 return -1;
244         return 0;
245 }
246
247 static uint32_t
248 rxp_get_reg_address(uint32_t address)
249 {
250         uint32_t block;
251         uint32_t reg;
252
253         block = (address >> 16) & 0xFFFF;
254         if (block == 0)
255                 reg = MLX5_RXP_CSR_BASE_ADDRESS;
256         else if (block == 1)
257                 reg = MLX5_RXP_RTRU_CSR_BASE_ADDRESS;
258         else {
259                 DRV_LOG(ERR, "Invalid ROF register 0x%08X!", address);
260                         return UINT32_MAX;
261         }
262         reg += (address & 0xFFFF) * MLX5_RXP_CSR_WIDTH;
263         return reg;
264 }
265
266 #define MLX5_RXP_NUM_LINES_PER_BLOCK 8
267
268 static int
269 rxp_program_rof(struct mlx5_regex_priv *priv, const char *buf, uint32_t len,
270                 uint8_t id)
271 {
272         static const char del[] = "\n\r";
273         char *line;
274         char *tmp;
275         uint32_t type = 0;
276         uint32_t address;
277         uint64_t val;
278         uint32_t reg_val;
279         int ret;
280         int skip = -1;
281         int last = 0;
282         uint32_t temp;
283         uint32_t tmp_addr;
284         uint32_t rof_rule_addr;
285         uint64_t tmp_write_swap[4];
286         struct mlx5_rxp_rof_entry rules[8];
287         int i;
288         int db_free;
289         int j;
290
291         tmp = rte_malloc("", len, 0);
292         if (!tmp)
293                 return -ENOMEM;
294         memcpy(tmp, buf, len);
295         db_free = mlnx_update_database(priv, id);
296         if (db_free < 0) {
297                 DRV_LOG(ERR, "Failed to setup db memory!");
298                 rte_free(tmp);
299                 return db_free;
300         }
301         for (line = strtok(tmp, del), j = 0; line; line = strtok(NULL, del),
302              j++, last = type) {
303                 ret = rxp_parse_line(line, &type, &address, &val);
304                 if (ret != 0) {
305                         if (ret < 0)
306                                 goto parse_error;
307                         continue;
308                 }
309                 switch (type) {
310                 case MLX5_RXP_ROF_ENTRY_EQ:
311                         if (skip == 0 && address == 0)
312                                 skip = 1;
313                         tmp_addr = rxp_get_reg_address(address);
314                         if (tmp_addr == UINT32_MAX)
315                                 goto parse_error;
316                         ret = mlx5_devx_regex_register_read(priv->ctx, id,
317                                                             tmp_addr, &reg_val);
318                         if (ret)
319                                 goto parse_error;
320                         if (skip == -1 && address == 0) {
321                                 if (val == reg_val) {
322                                         skip = 0;
323                                         continue;
324                                 }
325                         } else if (skip == 0) {
326                                 if (val != reg_val) {
327                                         DRV_LOG(ERR,
328                                                 "got %08X expected == %" PRIx64,
329                                                 reg_val, val);
330                                         goto parse_error;
331                                 }
332                         }
333                         break;
334                 case MLX5_RXP_ROF_ENTRY_GTE:
335                         if (skip == 0 && address == 0)
336                                 skip = 1;
337                         tmp_addr = rxp_get_reg_address(address);
338                         if (tmp_addr == UINT32_MAX)
339                                 goto parse_error;
340                         ret = mlx5_devx_regex_register_read(priv->ctx, id,
341                                                             tmp_addr, &reg_val);
342                         if (ret)
343                                 goto parse_error;
344                         if (skip == -1 && address == 0) {
345                                 if (reg_val >= val) {
346                                         skip = 0;
347                                         continue;
348                                 }
349                         } else if (skip == 0) {
350                                 if (reg_val < val) {
351                                         DRV_LOG(ERR,
352                                                 "got %08X expected >= %" PRIx64,
353                                                 reg_val, val);
354                                         goto parse_error;
355                                 }
356                         }
357                         break;
358                 case MLX5_RXP_ROF_ENTRY_LTE:
359                         tmp_addr = rxp_get_reg_address(address);
360                         if (tmp_addr == UINT32_MAX)
361                                 goto parse_error;
362                         ret = mlx5_devx_regex_register_read(priv->ctx, id,
363                                                             tmp_addr, &reg_val);
364                         if (ret)
365                                 goto parse_error;
366                         if (skip == 0 && address == 0 &&
367                             last != MLX5_RXP_ROF_ENTRY_GTE) {
368                                 skip = 1;
369                         } else if (skip == 0 && address == 0 &&
370                                    last == MLX5_RXP_ROF_ENTRY_GTE) {
371                                 if (reg_val > val)
372                                         skip = -1;
373                                 continue;
374                         }
375                         if (skip == -1 && address == 0) {
376                                 if (reg_val <= val) {
377                                         skip = 0;
378                                         continue;
379                                 }
380                         } else if (skip == 0) {
381                                 if (reg_val > val) {
382                                         DRV_LOG(ERR,
383                                                 "got %08X expected <= %" PRIx64,
384                                                 reg_val, val);
385                                         goto parse_error;
386                                 }
387                         }
388                         break;
389                 case MLX5_RXP_ROF_ENTRY_CHECKSUM:
390                         break;
391                 case MLX5_RXP_ROF_ENTRY_CHECKSUM_EX_EM:
392                         if (skip)
393                                 continue;
394                         tmp_addr = rxp_get_reg_address(address);
395                         if (tmp_addr == UINT32_MAX)
396                                 goto parse_error;
397
398                         ret = mlx5_devx_regex_register_read(priv->ctx, id,
399                                                             tmp_addr, &reg_val);
400                         if (ret) {
401                                 DRV_LOG(ERR, "RXP CSR read failed!");
402                                 return ret;
403                         }
404                         if (reg_val != val) {
405                                 DRV_LOG(ERR, "got %08X expected <= %" PRIx64,
406                                         reg_val, val);
407                                 goto parse_error;
408                         }
409                         break;
410                 case MLX5_RXP_ROF_ENTRY_IM:
411                         if (skip)
412                                 continue;
413                         /*
414                          * NOTE: All rules written to RXP must be carried out in
415                          * triplets of: 2xData + 1xAddr.
416                          * No optimisation is currently allowed in this
417                          * sequence to perform less writes.
418                          */
419                         temp = val;
420                         ret |= mlx5_devx_regex_register_write
421                                         (priv->ctx, id,
422                                          MLX5_RXP_RTRU_CSR_DATA_0, temp);
423                         temp = (uint32_t)(val >> 32);
424                         ret |= mlx5_devx_regex_register_write
425                                         (priv->ctx, id,
426                                          MLX5_RXP_RTRU_CSR_DATA_0 +
427                                          MLX5_RXP_CSR_WIDTH, temp);
428                         temp = address;
429                         ret |= mlx5_devx_regex_register_write
430                                         (priv->ctx, id, MLX5_RXP_RTRU_CSR_ADDR,
431                                          temp);
432                         if (ret) {
433                                 DRV_LOG(ERR,
434                                         "Failed to copy instructions to RXP.");
435                                 goto parse_error;
436                         }
437                         break;
438                 case MLX5_RXP_ROF_ENTRY_EM:
439                         if (skip)
440                                 continue;
441                         for (i = 0; i < MLX5_RXP_NUM_LINES_PER_BLOCK; i++) {
442                                 ret = rxp_parse_line(line, &type,
443                                                      &rules[i].addr,
444                                                      &rules[i].value);
445                                 if (ret != 0)
446                                         goto parse_error;
447                                 if (i < (MLX5_RXP_NUM_LINES_PER_BLOCK - 1)) {
448                                         line = strtok(NULL, del);
449                                         if (!line)
450                                                 goto parse_error;
451                                 }
452                         }
453                         if ((uint8_t *)((uint8_t *)
454                                         priv->db[id].ptr +
455                                         ((rules[7].addr <<
456                                          MLX5_RXP_INST_OFFSET))) >=
457                                         ((uint8_t *)((uint8_t *)
458                                         priv->db[id].ptr + MLX5_MAX_DB_SIZE))) {
459                                 DRV_LOG(ERR, "DB exceeded memory!");
460                                 goto parse_error;
461                         }
462                         /*
463                          * Rule address Offset to align with RXP
464                          * external instruction offset.
465                          */
466                         rof_rule_addr = (rules[0].addr << MLX5_RXP_INST_OFFSET);
467                         /* 32 byte instruction swap (sw work around)! */
468                         tmp_write_swap[0] = le64toh(rules[4].value);
469                         tmp_write_swap[1] = le64toh(rules[5].value);
470                         tmp_write_swap[2] = le64toh(rules[6].value);
471                         tmp_write_swap[3] = le64toh(rules[7].value);
472                         /* Write only 4 of the 8 instructions. */
473                         memcpy((uint8_t *)((uint8_t *)
474                                 priv->db[id].ptr + rof_rule_addr),
475                                 &tmp_write_swap, (sizeof(uint64_t) * 4));
476                         /* Write 1st 4 rules of block after last 4. */
477                         rof_rule_addr = (rules[4].addr << MLX5_RXP_INST_OFFSET);
478                         tmp_write_swap[0] = le64toh(rules[0].value);
479                         tmp_write_swap[1] = le64toh(rules[1].value);
480                         tmp_write_swap[2] = le64toh(rules[2].value);
481                         tmp_write_swap[3] = le64toh(rules[3].value);
482                         memcpy((uint8_t *)((uint8_t *)
483                                 priv->db[id].ptr + rof_rule_addr),
484                                 &tmp_write_swap, (sizeof(uint64_t) * 4));
485                         break;
486                 default:
487                         break;
488                 }
489
490         }
491         ret = mlnx_set_database(priv, id, db_free);
492         if (ret < 0) {
493                 DRV_LOG(ERR, "Failed to register db memory!");
494                 goto parse_error;
495         }
496         rte_free(tmp);
497         return 0;
498 parse_error:
499         rte_free(tmp);
500         return ret;
501 }
502
503 static int
504 mlnx_set_database(struct mlx5_regex_priv *priv, uint8_t id, uint8_t db_to_use)
505 {
506         int ret;
507         uint32_t umem_id;
508
509         ret = mlx5_devx_regex_database_stop(priv->ctx, id);
510         if (ret < 0) {
511                 DRV_LOG(ERR, "stop engine failed!");
512                 return ret;
513         }
514         umem_id = mlx5_os_get_umem_id(priv->db[db_to_use].umem.umem);
515         ret = mlx5_devx_regex_database_program(priv->ctx, id, umem_id, 0);
516         if (ret < 0) {
517                 DRV_LOG(ERR, "program db failed!");
518                 return ret;
519         }
520         return 0;
521 }
522
523 static int
524 mlnx_resume_database(struct mlx5_regex_priv *priv, uint8_t id)
525 {
526         mlx5_devx_regex_database_resume(priv->ctx, id);
527         return 0;
528 }
529
530 /*
531  * Assign db memory for RXP programming.
532  */
533 static int
534 mlnx_update_database(struct mlx5_regex_priv *priv, uint8_t id)
535 {
536         unsigned int i;
537         uint8_t db_free = MLX5_RXP_DB_NOT_ASSIGNED;
538         uint8_t eng_assigned = MLX5_RXP_DB_NOT_ASSIGNED;
539
540         /* Check which database rxp_eng is currently located if any? */
541         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
542              i++) {
543                 if (priv->db[i].db_assigned_to_eng_num == id) {
544                         eng_assigned = i;
545                         break;
546                 }
547         }
548         /*
549          * If private mode then, we can keep the same db ptr as RXP will be
550          * programming EM itself if necessary, however need to see if
551          * programmed yet.
552          */
553         if ((priv->prog_mode == MLX5_RXP_PRIVATE_PROG_MODE) &&
554             (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED))
555                 return eng_assigned;
556         /* Check for inactive db memory to use. */
557         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
558              i++) {
559                 if (priv->db[i].active == true)
560                         continue; /* Already in use, so skip db. */
561                 /* Set this db to active now as free to use. */
562                 priv->db[i].active = true;
563                 /* Now unassign last db index in use by RXP Eng. */
564                 if (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED) {
565                         priv->db[eng_assigned].active = false;
566                         priv->db[eng_assigned].db_assigned_to_eng_num =
567                                 MLX5_RXP_DB_NOT_ASSIGNED;
568
569                         /* Set all DB memory to 0's before setting up DB. */
570                         memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
571                 }
572                 /* Now reassign new db index with RXP Engine. */
573                 priv->db[i].db_assigned_to_eng_num = id;
574                 db_free = i;
575                 break;
576         }
577         if (db_free == MLX5_RXP_DB_NOT_ASSIGNED)
578                 return -1;
579         return db_free;
580 }
581
582 /*
583  * Program RXP instruction db to RXP engine/s.
584  */
585 static int
586 program_rxp_rules(struct mlx5_regex_priv *priv, const char *buf, uint32_t len,
587                   uint8_t id)
588 {
589         int ret;
590         uint32_t val;
591
592         ret = rxp_init_eng(priv, id);
593         if (ret < 0)
594                 return ret;
595         /* Confirm the RXP is initialised. */
596         if (mlx5_devx_regex_register_read(priv->ctx, id,
597                                             MLX5_RXP_CSR_STATUS, &val)) {
598                 DRV_LOG(ERR, "Failed to read from RXP!");
599                 return -ENODEV;
600         }
601         if (!(val & MLX5_RXP_CSR_STATUS_INIT_DONE)) {
602                 DRV_LOG(ERR, "RXP not initialised...");
603                 return -EBUSY;
604         }
605         ret = mlx5_devx_regex_register_read(priv->ctx, id,
606                                             MLX5_RXP_RTRU_CSR_CTRL, &val);
607         if (ret) {
608                 DRV_LOG(ERR, "CSR read failed!");
609                 return -1;
610         }
611         val |= MLX5_RXP_RTRU_CSR_CTRL_GO;
612         ret = mlx5_devx_regex_register_write(priv->ctx, id,
613                                              MLX5_RXP_RTRU_CSR_CTRL, val);
614         if (ret) {
615                 DRV_LOG(ERR, "Can't program rof file!");
616                 return -1;
617         }
618         ret = rxp_program_rof(priv, buf, len, id);
619         if (ret) {
620                 DRV_LOG(ERR, "Can't program rof file!");
621                 return -1;
622         }
623         if (priv->is_bf2) {
624                 ret = rxp_poll_csr_for_value
625                         (priv->ctx, &val, MLX5_RXP_RTRU_CSR_STATUS,
626                          MLX5_RXP_RTRU_CSR_STATUS_UPDATE_DONE,
627                          MLX5_RXP_RTRU_CSR_STATUS_UPDATE_DONE,
628                          MLX5_RXP_POLL_CSR_FOR_VALUE_TIMEOUT, id);
629                 if (ret < 0) {
630                         DRV_LOG(ERR, "Rules update timeout: 0x%08X", val);
631                         return ret;
632                 }
633                 DRV_LOG(DEBUG, "Rules update took %d cycles", ret);
634         }
635         if (mlx5_devx_regex_register_read(priv->ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
636                                           &val)) {
637                 DRV_LOG(ERR, "CSR read failed!");
638                 return -1;
639         }
640         val &= ~(MLX5_RXP_RTRU_CSR_CTRL_GO);
641         if (mlx5_devx_regex_register_write(priv->ctx, id,
642                                            MLX5_RXP_RTRU_CSR_CTRL, val)) {
643                 DRV_LOG(ERR, "CSR write failed!");
644                 return -1;
645         }
646         ret = mlx5_devx_regex_register_read(priv->ctx, id, MLX5_RXP_CSR_CTRL,
647                                             &val);
648         if (ret)
649                 return ret;
650         val &= ~MLX5_RXP_CSR_CTRL_INIT;
651         ret = mlx5_devx_regex_register_write(priv->ctx, id, MLX5_RXP_CSR_CTRL,
652                                              val);
653         if (ret)
654                 return ret;
655         rxp_init_rtru(priv, id, MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_L1_L2);
656         if (priv->is_bf2) {
657                 ret = rxp_poll_csr_for_value(priv->ctx, &val,
658                                              MLX5_RXP_CSR_STATUS,
659                                              MLX5_RXP_CSR_STATUS_INIT_DONE,
660                                              MLX5_RXP_CSR_STATUS_INIT_DONE,
661                                              MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT,
662                                              id);
663                 if (ret) {
664                         DRV_LOG(ERR, "Device init failed!");
665                         return ret;
666                 }
667         }
668         ret = mlnx_resume_database(priv, id);
669         if (ret < 0) {
670                 DRV_LOG(ERR, "Failed to resume engine!");
671                 return ret;
672         }
673
674         return ret;
675
676 }
677
678 static int
679 rxp_init_eng(struct mlx5_regex_priv *priv, uint8_t id)
680 {
681         uint32_t ctrl;
682         uint32_t reg;
683         struct ibv_context *ctx = priv->ctx;
684         int ret;
685
686         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
687         if (ret)
688                 return ret;
689         if (ctrl & MLX5_RXP_CSR_CTRL_INIT) {
690                 ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
691                 ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL,
692                                                      ctrl);
693                 if (ret)
694                         return ret;
695         }
696         ctrl |= MLX5_RXP_CSR_CTRL_INIT;
697         ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
698         if (ret)
699                 return ret;
700         ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
701         ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
702         if (ret)
703                 return ret;
704         rte_delay_us(20000);
705         ret = rxp_poll_csr_for_value(ctx, &ctrl, MLX5_RXP_CSR_STATUS,
706                                      MLX5_RXP_CSR_STATUS_INIT_DONE,
707                                      MLX5_RXP_CSR_STATUS_INIT_DONE,
708                                      MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT, id);
709         if (ret)
710                 return ret;
711         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
712         if (ret)
713                 return ret;
714         ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
715         ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL,
716                                              ctrl);
717         if (ret)
718                 return ret;
719         ret = rxp_init_rtru(priv, id,
720                             MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_IM_L1_L2);
721         if (ret)
722                 return ret;
723         ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CAPABILITY_5,
724                                             &reg);
725         if (ret)
726                 return ret;
727         DRV_LOG(DEBUG, "max matches: %d, DDOS threshold: %d", reg >> 16,
728                 reg & 0xffff);
729         if ((reg >> 16) >= priv->nb_max_matches)
730                 ret = mlx5_devx_regex_register_write(ctx, id,
731                                                      MLX5_RXP_CSR_MAX_MATCH,
732                                                      priv->nb_max_matches);
733         else
734                 ret = mlx5_devx_regex_register_write(ctx, id,
735                                                      MLX5_RXP_CSR_MAX_MATCH,
736                                                      (reg >> 16));
737         ret |= mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_MAX_PREFIX,
738                                          (reg & 0xFFFF));
739         ret |= mlx5_devx_regex_register_write(ctx, id,
740                                               MLX5_RXP_CSR_MAX_LATENCY, 0);
741         ret |= mlx5_devx_regex_register_write(ctx, id,
742                                               MLX5_RXP_CSR_MAX_PRI_THREAD, 0);
743         return ret;
744 }
745
746 static int
747 rxp_db_setup(struct mlx5_regex_priv *priv)
748 {
749         int ret;
750         uint8_t i;
751
752         /* Setup database memories for both RXP engines + reprogram memory. */
753         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
754                 priv->db[i].ptr = rte_malloc("", MLX5_MAX_DB_SIZE, 1 << 21);
755                 if (!priv->db[i].ptr) {
756                         DRV_LOG(ERR, "Failed to alloc db memory!");
757                         ret = ENODEV;
758                         goto tidyup_error;
759                 }
760                 /* Register the memory. */
761                 priv->db[i].umem.umem = mlx5_glue->devx_umem_reg(priv->ctx,
762                                                         priv->db[i].ptr,
763                                                         MLX5_MAX_DB_SIZE, 7);
764                 if (!priv->db[i].umem.umem) {
765                         DRV_LOG(ERR, "Failed to register memory!");
766                         ret = ENODEV;
767                         goto tidyup_error;
768                 }
769                 /* Ensure set all DB memory to 0's before setting up DB. */
770                 memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
771                 /* No data currently in database. */
772                 priv->db[i].len = 0;
773                 priv->db[i].active = false;
774                 priv->db[i].db_assigned_to_eng_num = MLX5_RXP_DB_NOT_ASSIGNED;
775         }
776         return 0;
777 tidyup_error:
778         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
779                 if (priv->db[i].ptr)
780                         rte_free(priv->db[i].ptr);
781                 if (priv->db[i].umem.umem)
782                         mlx5_glue->devx_umem_dereg(priv->db[i].umem.umem);
783         }
784         return -ret;
785 }
786
787 int
788 mlx5_regex_rules_db_import(struct rte_regexdev *dev,
789                      const char *rule_db, uint32_t rule_db_len)
790 {
791         struct mlx5_regex_priv *priv = dev->data->dev_private;
792         struct mlx5_rxp_ctl_rules_pgm *rules = NULL;
793         uint32_t id;
794         int ret;
795         uint32_t ver;
796
797         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
798                 DRV_LOG(ERR, "RXP programming mode not set!");
799                 return -1;
800         }
801         if (rule_db == NULL) {
802                 DRV_LOG(ERR, "Database empty!");
803                 return -ENODEV;
804         }
805         if (rule_db_len == 0)
806                 return -EINVAL;
807         if (mlx5_devx_regex_register_read(priv->ctx, 0,
808                                           MLX5_RXP_CSR_BASE_ADDRESS, &ver)) {
809                 DRV_LOG(ERR, "Failed to read Main CSRs Engine 0!");
810                 return -1;
811         }
812         /* Need to ensure RXP not busy before stop! */
813         for (id = 0; id < priv->nb_engines; id++) {
814                 ret = rxp_stop_engine(priv->ctx, id);
815                 if (ret) {
816                         DRV_LOG(ERR, "Can't stop engine.");
817                         ret = -ENODEV;
818                         goto tidyup_error;
819                 }
820                 ret = program_rxp_rules(priv, rule_db, rule_db_len, id);
821                 if (ret < 0) {
822                         DRV_LOG(ERR, "Failed to program rxp rules.");
823                         ret = -ENODEV;
824                         goto tidyup_error;
825                 }
826                 ret = rxp_start_engine(priv->ctx, id);
827                 if (ret) {
828                         DRV_LOG(ERR, "Can't start engine.");
829                         ret = -ENODEV;
830                         goto tidyup_error;
831                 }
832         }
833         rte_free(rules);
834         return 0;
835 tidyup_error:
836         rte_free(rules);
837         return ret;
838 }
839
840 int
841 mlx5_regex_configure(struct rte_regexdev *dev,
842                      const struct rte_regexdev_config *cfg)
843 {
844         struct mlx5_regex_priv *priv = dev->data->dev_private;
845         int ret;
846
847         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
848                 return -1;
849         priv->nb_queues = cfg->nb_queue_pairs;
850         dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
851         priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
852                                 priv->nb_queues, 0);
853         if (!priv->nb_queues) {
854                 DRV_LOG(ERR, "can't allocate qps memory");
855                 rte_errno = ENOMEM;
856                 return -rte_errno;
857         }
858         priv->nb_max_matches = cfg->nb_max_matches;
859         /* Setup rxp db memories. */
860         if (rxp_db_setup(priv)) {
861                 DRV_LOG(ERR, "Failed to setup RXP db memory");
862                 rte_errno = ENOMEM;
863                 return -rte_errno;
864         }
865         if (cfg->rule_db != NULL) {
866                 ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
867                                                  cfg->rule_db_len);
868                 if (ret < 0) {
869                         DRV_LOG(ERR, "Failed to program rxp rules.");
870                         rte_errno = ENODEV;
871                         goto configure_error;
872                 }
873         } else
874                 DRV_LOG(DEBUG, "Regex config without rules programming!");
875         return 0;
876 configure_error:
877         if (priv->qps)
878                 rte_free(priv->qps);
879         return -rte_errno;
880 }