regex/mlx5: remove register read/write
[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 int
32 mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
33                     struct rte_regexdev_info *info)
34 {
35         info->max_matches = MLX5_REGEX_MAX_MATCHES;
36         info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
37         info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
38         info->max_groups = MLX5_REGEX_MAX_GROUPS;
39         info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
40                               RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
41         info->rule_flags = 0;
42         info->max_queue_pairs = UINT16_MAX;
43         return 0;
44 }
45
46 static int
47 rxp_db_setup(struct mlx5_regex_priv *priv)
48 {
49         int ret;
50         uint8_t i;
51
52         /* Setup database memories for both RXP engines + reprogram memory. */
53         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
54                 priv->db[i].ptr = rte_malloc("", MLX5_MAX_DB_SIZE, 1 << 21);
55                 if (!priv->db[i].ptr) {
56                         DRV_LOG(ERR, "Failed to alloc db memory!");
57                         ret = ENODEV;
58                         goto tidyup_error;
59                 }
60                 /* Register the memory. */
61                 priv->db[i].umem.umem = mlx5_glue->devx_umem_reg
62                                                         (priv->cdev->ctx,
63                                                          priv->db[i].ptr,
64                                                          MLX5_MAX_DB_SIZE, 7);
65                 if (!priv->db[i].umem.umem) {
66                         DRV_LOG(ERR, "Failed to register memory!");
67                         ret = ENODEV;
68                         goto tidyup_error;
69                 }
70                 /* Ensure set all DB memory to 0's before setting up DB. */
71                 memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
72                 /* No data currently in database. */
73                 priv->db[i].len = 0;
74                 priv->db[i].active = false;
75                 priv->db[i].db_assigned_to_eng_num = MLX5_RXP_DB_NOT_ASSIGNED;
76         }
77         return 0;
78 tidyup_error:
79         for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
80                 if (priv->db[i].umem.umem)
81                         mlx5_glue->devx_umem_dereg(priv->db[i].umem.umem);
82                 rte_free(priv->db[i].ptr);
83                 priv->db[i].ptr = NULL;
84         }
85         return -ret;
86 }
87
88 int
89 mlx5_regex_rules_db_import(struct rte_regexdev *dev,
90                      const char *rule_db, uint32_t rule_db_len)
91 {
92         struct mlx5_regex_priv *priv = dev->data->dev_private;
93
94         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
95                 DRV_LOG(ERR, "RXP programming mode not set!");
96                 return -1;
97         }
98         if (rule_db == NULL) {
99                 DRV_LOG(ERR, "Database empty!");
100                 return -ENODEV;
101         }
102         if (rule_db_len == 0)
103                 return -EINVAL;
104
105         return 0;
106 }
107
108 int
109 mlx5_regex_configure(struct rte_regexdev *dev,
110                      const struct rte_regexdev_config *cfg)
111 {
112         struct mlx5_regex_priv *priv = dev->data->dev_private;
113         int ret;
114
115         if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
116                 return -1;
117         priv->nb_queues = cfg->nb_queue_pairs;
118         dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
119         priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
120                                 priv->nb_queues, 0);
121         if (!priv->nb_queues) {
122                 DRV_LOG(ERR, "can't allocate qps memory");
123                 rte_errno = ENOMEM;
124                 return -rte_errno;
125         }
126         priv->nb_max_matches = cfg->nb_max_matches;
127         /* Setup rxp db memories. */
128         if (rxp_db_setup(priv)) {
129                 DRV_LOG(ERR, "Failed to setup RXP db memory");
130                 rte_errno = ENOMEM;
131                 return -rte_errno;
132         }
133         if (cfg->rule_db != NULL) {
134                 ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
135                                                  cfg->rule_db_len);
136                 if (ret < 0) {
137                         DRV_LOG(ERR, "Failed to program rxp rules.");
138                         rte_errno = ENODEV;
139                         goto configure_error;
140                 }
141         } else
142                 DRV_LOG(DEBUG, "Regex config without rules programming!");
143         return 0;
144 configure_error:
145         if (priv->qps)
146                 rte_free(priv->qps);
147         return -rte_errno;
148 }