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.h"
21
22 #define MLX5_REGEX_MAX_MATCHES MLX5_RXP_MAX_MATCHES
23 #define MLX5_REGEX_MAX_PAYLOAD_SIZE MLX5_RXP_MAX_JOB_LENGTH
24 #define MLX5_REGEX_MAX_RULES_PER_GROUP UINT32_MAX
25 #define MLX5_REGEX_MAX_GROUPS MLX5_RXP_MAX_SUBSETS
26
27 #define MLX5_REGEX_RXP_ROF2_LINE_LEN 34
28
29 /* Private Declarations */
30 static int
31 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
32 uint32_t access, struct mlx5_regex_mkey *mkey);
33 static inline void
34 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey);
35
36 int
mlx5_regex_info_get(struct rte_regexdev * dev __rte_unused,struct rte_regexdev_info * info)37 mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
38 struct rte_regexdev_info *info)
39 {
40 info->max_matches = MLX5_REGEX_MAX_MATCHES;
41 info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
42 info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
43 info->max_groups = MLX5_REGEX_MAX_GROUPS;
44 info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
45 RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
46 info->rule_flags = 0;
47 info->max_queue_pairs = UINT16_MAX;
48 return 0;
49 }
50
51 static int
rxp_create_mkey(struct mlx5_regex_priv * priv,void * ptr,size_t size,uint32_t access,struct mlx5_regex_mkey * mkey)52 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
53 uint32_t access, struct mlx5_regex_mkey *mkey)
54 {
55 struct mlx5_devx_mkey_attr mkey_attr;
56
57 /* Register the memory. */
58 mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
59 if (!mkey->umem) {
60 DRV_LOG(ERR, "Failed to register memory!");
61 return -ENODEV;
62 }
63 /* Create mkey */
64 mkey_attr = (struct mlx5_devx_mkey_attr) {
65 .addr = (uintptr_t)ptr,
66 .size = (uint32_t)size,
67 .umem_id = mlx5_os_get_umem_id(mkey->umem),
68 .pg_access = 1,
69 .umr_en = 0,
70 };
71 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
72 mkey_attr.pd = priv->cdev->pdn;
73 #endif
74 mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
75 if (!mkey->mkey) {
76 DRV_LOG(ERR, "Failed to create direct mkey!");
77 return -ENODEV;
78 }
79 return 0;
80 }
81
82 static inline void
rxp_destroy_mkey(struct mlx5_regex_mkey * mkey)83 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
84 {
85 if (mkey->mkey)
86 claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
87 if (mkey->umem)
88 claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
89 }
90
91 int
mlx5_regex_rules_db_import(struct rte_regexdev * dev,const char * rule_db,uint32_t rule_db_len)92 mlx5_regex_rules_db_import(struct rte_regexdev *dev,
93 const char *rule_db, uint32_t rule_db_len)
94 {
95 struct mlx5_regex_priv *priv = dev->data->dev_private;
96 struct mlx5_regex_mkey mkey;
97 uint32_t id;
98 int ret;
99 void *ptr;
100
101 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
102 DRV_LOG(ERR, "RXP programming mode not set!");
103 return -1;
104 }
105 if (rule_db == NULL) {
106 DRV_LOG(ERR, "Database empty!");
107 return -ENODEV;
108 }
109 if (rule_db_len == 0)
110 return -EINVAL;
111 /* copy rules - rules have to be 4KB aligned. */
112 ptr = rte_malloc("", rule_db_len, 1 << 12);
113 if (!ptr) {
114 DRV_LOG(ERR, "Failed to allocate rules file memory.");
115 return -ENOMEM;
116 }
117 rte_memcpy(ptr, rule_db, rule_db_len);
118 /* Register umem and create rof mkey. */
119 ret = rxp_create_mkey(priv, ptr, rule_db_len, /*access=*/7, &mkey);
120 if (ret < 0)
121 return ret;
122
123 for (id = 0; id < priv->nb_engines; id++) {
124 ret = mlx5_devx_regex_rules_program(priv->cdev->ctx, id,
125 mkey.mkey->id, rule_db_len, (uintptr_t)ptr);
126 if (ret < 0) {
127 DRV_LOG(ERR, "Failed to program rxp rules.");
128 ret = -ENODEV;
129 break;
130 }
131 ret = 0;
132 }
133 rxp_destroy_mkey(&mkey);
134 rte_free(ptr);
135 return ret;
136 }
137
138 int
mlx5_regex_configure(struct rte_regexdev * dev,const struct rte_regexdev_config * cfg)139 mlx5_regex_configure(struct rte_regexdev *dev,
140 const struct rte_regexdev_config *cfg)
141 {
142 struct mlx5_regex_priv *priv = dev->data->dev_private;
143 int ret;
144
145 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
146 return -1;
147 priv->nb_queues = cfg->nb_queue_pairs;
148 dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
149 priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
150 priv->nb_queues, 0);
151 if (!priv->qps) {
152 DRV_LOG(ERR, "can't allocate qps memory");
153 rte_errno = ENOMEM;
154 return -rte_errno;
155 }
156 priv->nb_max_matches = cfg->nb_max_matches;
157 if (cfg->rule_db != NULL) {
158 ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
159 cfg->rule_db_len);
160 if (ret < 0) {
161 DRV_LOG(ERR, "Failed to program rxp rules.");
162 rte_errno = ENODEV;
163 goto configure_error;
164 }
165 } else
166 DRV_LOG(DEBUG, "Regex config without rules programming!");
167 return 0;
168 configure_error:
169 rte_free(priv->qps);
170 return -rte_errno;
171 }
172