Branch data Line data Source code
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 : : const uint64_t combined_rof_tag = 0xff52544424a52475;
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
37 : 0 : mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
38 : : struct rte_regexdev_info *info)
39 : : {
40 : 0 : info->max_matches = MLX5_REGEX_MAX_MATCHES;
41 : 0 : info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
42 : 0 : info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
43 : 0 : info->max_groups = MLX5_REGEX_MAX_GROUPS;
44 : 0 : info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
45 : : RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
46 : 0 : info->rule_flags = 0;
47 : 0 : info->max_queue_pairs = UINT16_MAX;
48 : 0 : info->max_segs = mlx5_regexdev_max_segs_get();
49 : 0 : return 0;
50 : : }
51 : :
52 : : static int
53 : 0 : rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
54 : : uint32_t access, struct mlx5_regex_mkey *mkey)
55 : : {
56 : : struct mlx5_devx_mkey_attr mkey_attr;
57 : 0 : struct mlx5_hca_attr *hca_attr = &priv->cdev->config.hca_attr;
58 : :
59 : : /* Register the memory. */
60 : 0 : mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
61 [ # # ]: 0 : if (!mkey->umem) {
62 : 0 : DRV_LOG(ERR, "Failed to register memory!");
63 : 0 : return -ENODEV;
64 : : }
65 : : /* Create mkey */
66 : 0 : mkey_attr = (struct mlx5_devx_mkey_attr) {
67 [ # # ]: 0 : .addr = (uintptr_t)ptr,
68 : : .size = (uint32_t)size,
69 : : .umem_id = mlx5_os_get_umem_id(mkey->umem),
70 : : .pg_access = 1,
71 : : .umr_en = 0,
72 : : };
73 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
74 : 0 : mkey_attr.pd = priv->cdev->pdn;
75 : : #endif
76 : : /* If only relaxed order is allowed. */
77 [ # # ]: 0 : if (hca_attr->mkc_order_write_after_write_ro_only)
78 : 0 : mlx5_devx_mkey_attr_set_ordering(&mkey_attr, hca_attr);
79 : 0 : mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
80 [ # # ]: 0 : if (!mkey->mkey) {
81 : 0 : DRV_LOG(ERR, "Failed to create direct mkey!");
82 : 0 : return -ENODEV;
83 : : }
84 : : return 0;
85 : : }
86 : :
87 : : static inline void
88 : 0 : rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
89 : : {
90 [ # # ]: 0 : if (mkey->mkey)
91 : 0 : claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
92 [ # # ]: 0 : if (mkey->umem)
93 : 0 : claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
94 : 0 : }
95 : :
96 : : int
97 : 0 : mlx5_regex_get_rxp_vers(uint32_t regexp_version, uint32_t *target_rxp_vers)
98 : : {
99 : : int ret = 0;
100 [ # # # ]: 0 : switch (regexp_version) {
101 : 0 : case MLX5_RXP_BF2_IDENTIFIER:
102 : 0 : *target_rxp_vers = MLX5_RXP_BF2_ROF_VERSION_STRING;
103 : 0 : break;
104 : 0 : case MLX5_RXP_BF3_IDENTIFIER:
105 : 0 : *target_rxp_vers = MLX5_RXP_BF3_ROF_VERSION_STRING;
106 : 0 : break;
107 : 0 : default:
108 : 0 : DRV_LOG(ERR, "Unsupported rxp version: %u", regexp_version);
109 : : ret = -EINVAL;
110 : 0 : break;
111 : : }
112 : 0 : return ret;
113 : : }
114 : :
115 : : int
116 : 0 : mlx5_regex_check_rof_version(uint32_t combined_rof_vers)
117 : : {
118 : : int ret = 0;
119 : : /* Check if combined rof version is supported */
120 [ # # ]: 0 : switch (combined_rof_vers) {
121 : : case 1:
122 : : break;
123 : 0 : default:
124 : 0 : DRV_LOG(ERR, "Unsupported combined rof version: %u",
125 : : combined_rof_vers);
126 : : ret = -EINVAL;
127 : 0 : break;
128 : : }
129 : 0 : return ret;
130 : : }
131 : :
132 : : int
133 : 0 : mlx5_regex_parse_rules_db(struct mlx5_regex_priv *priv,
134 : : const char **rules_db, uint32_t *rules_db_len)
135 : : {
136 : : int i = 0;
137 : : uint32_t j = 0;
138 : : int ret = 0;
139 : : bool combined_rof = true;
140 : 0 : const char *rof_ptr = *rules_db;
141 : : uint32_t combined_rof_vers = 0;
142 : : uint32_t num_rof_blocks = 0;
143 : : uint32_t rxpc_vers = 0;
144 : 0 : uint32_t target_rxp_vers = 0;
145 : : uint32_t byte_count = 0;
146 : : uint32_t rof_bytes_read = 0;
147 : : bool rof_binary_found = false;
148 : 0 : struct mlx5_hca_attr *attr = &priv->cdev->config.hca_attr;
149 : :
150 : : /* Need minimum of 8 bytes to process single or combined rof */
151 [ # # ]: 0 : if (*rules_db_len < 8)
152 : : return -EINVAL;
153 : :
154 [ # # ]: 0 : for (i = 0; i < 8; i++) {
155 : 0 : if ((char) *rof_ptr !=
156 [ # # ]: 0 : (char)((combined_rof_tag >> (i * 8)) & 0xFF)) {
157 : : combined_rof = false;
158 : : break;
159 : : }
160 : 0 : rof_ptr++;
161 : : }
162 : : rof_bytes_read += 8;
163 : :
164 [ # # ]: 0 : if (combined_rof == true) {
165 : : /* Need at least 24 bytes of header info: 16 byte combined */
166 : : /* rof header and 8 byte binary rof blob header. */
167 [ # # ]: 0 : if (*rules_db_len < 24)
168 : : return -EINVAL;
169 : :
170 : : /* Read the combined rof version and number of rof blocks */
171 [ # # ]: 0 : for (i = 0; i < 4; i++) {
172 : 0 : combined_rof_vers |= *rof_ptr << (i * 8);
173 : 0 : rof_ptr++;
174 : : }
175 : :
176 : : rof_bytes_read += 4;
177 : 0 : ret = mlx5_regex_check_rof_version(combined_rof_vers);
178 [ # # ]: 0 : if (ret < 0)
179 : : return ret;
180 : :
181 [ # # ]: 0 : for (i = 0; i < 4; i++) {
182 : 0 : num_rof_blocks |= *rof_ptr << (i * 8);
183 : 0 : rof_ptr++;
184 : : }
185 : : rof_bytes_read += 4;
186 : :
187 [ # # ]: 0 : if (num_rof_blocks == 0)
188 : : return -EINVAL;
189 : :
190 : : /* Get the version of rxp we need the rof for */
191 : 0 : ret = mlx5_regex_get_rxp_vers(attr->regexp_version, &target_rxp_vers);
192 [ # # ]: 0 : if (ret < 0)
193 : : return ret;
194 : :
195 : : /* Try to find the rof binary blob for this version of rxp */
196 [ # # ]: 0 : for (j = 0; j < num_rof_blocks; j++) {
197 : : rxpc_vers = 0;
198 : : byte_count = 0;
199 [ # # ]: 0 : for (i = 0; i < 4; i++) {
200 : 0 : rxpc_vers |= (*rof_ptr & 0xFF) << (i * 8);
201 : 0 : rof_ptr++;
202 : : }
203 [ # # ]: 0 : for (i = 0; i < 4; i++) {
204 : 0 : byte_count |= (*rof_ptr & 0xFF) << (i * 8);
205 : 0 : rof_ptr++;
206 : : }
207 : 0 : rof_bytes_read += 8;
208 : :
209 [ # # ]: 0 : if (rxpc_vers == target_rxp_vers) {
210 : : /* Found corresponding binary rof entry */
211 [ # # ]: 0 : if (rof_bytes_read + byte_count <= (*rules_db_len))
212 : : rof_binary_found = true;
213 : : else
214 : 0 : DRV_LOG(ERR, "Compatible rof file found - invalid length!");
215 : : break;
216 : : }
217 : : /* Move on to next rof blob */
218 [ # # ]: 0 : if (rof_bytes_read + byte_count + 8 < (*rules_db_len)) {
219 : 0 : rof_ptr += byte_count;
220 : : rof_bytes_read += byte_count;
221 : : } else {
222 : : /* Cannot parse any more of combined rof file */
223 : : break;
224 : : }
225 : : }
226 [ # # ]: 0 : if (rof_binary_found == true) {
227 : 0 : *rules_db = rof_ptr;
228 : 0 : *rules_db_len = byte_count;
229 : : } else {
230 : 0 : DRV_LOG(ERR, "Compatible rof file not found!");
231 : 0 : return -EINVAL;
232 : : }
233 : : }
234 : : return 0;
235 : : }
236 : :
237 : : int
238 : 0 : mlx5_regex_rules_db_import(struct rte_regexdev *dev,
239 : : const char *rule_db, uint32_t rule_db_len)
240 : : {
241 : 0 : struct mlx5_regex_priv *priv = dev->data->dev_private;
242 : : struct mlx5_regex_mkey mkey;
243 : : uint32_t id;
244 : : int ret;
245 : : void *ptr;
246 : :
247 [ # # ]: 0 : if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
248 : 0 : DRV_LOG(ERR, "RXP programming mode not set!");
249 : 0 : return -1;
250 : : }
251 [ # # ]: 0 : if (rule_db == NULL) {
252 : 0 : DRV_LOG(ERR, "Database empty!");
253 : 0 : return -ENODEV;
254 : : }
255 [ # # ]: 0 : if (rule_db_len == 0)
256 : : return -EINVAL;
257 : :
258 : 0 : ret = mlx5_regex_parse_rules_db(priv, &rule_db, &rule_db_len);
259 [ # # ]: 0 : if (ret < 0)
260 : : return ret;
261 : :
262 : : /* copy rules - rules have to be 4KB aligned. */
263 : 0 : ptr = rte_malloc("", rule_db_len, 1 << 12);
264 [ # # ]: 0 : if (!ptr) {
265 : 0 : DRV_LOG(ERR, "Failed to allocate rules file memory.");
266 : 0 : return -ENOMEM;
267 : : }
268 [ # # ]: 0 : rte_memcpy(ptr, rule_db, rule_db_len);
269 : : /* Register umem and create rof mkey. */
270 : 0 : ret = rxp_create_mkey(priv, ptr, rule_db_len, /*access=*/7, &mkey);
271 [ # # ]: 0 : if (ret < 0)
272 : : return ret;
273 : :
274 [ # # ]: 0 : for (id = 0; id < priv->nb_engines; id++) {
275 : 0 : ret = mlx5_devx_regex_rules_program(priv->cdev->ctx, id,
276 : 0 : mkey.mkey->id, rule_db_len, (uintptr_t)ptr);
277 [ # # ]: 0 : if (ret < 0) {
278 : 0 : DRV_LOG(ERR, "Failed to program rxp rules.");
279 : : ret = -ENODEV;
280 : 0 : break;
281 : : }
282 : : ret = 0;
283 : : }
284 : 0 : rxp_destroy_mkey(&mkey);
285 : 0 : rte_free(ptr);
286 : 0 : return ret;
287 : : }
288 : :
289 : : int
290 : 0 : mlx5_regex_configure(struct rte_regexdev *dev,
291 : : const struct rte_regexdev_config *cfg)
292 : : {
293 : 0 : struct mlx5_regex_priv *priv = dev->data->dev_private;
294 : : int ret;
295 : :
296 [ # # ]: 0 : if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
297 : : return -1;
298 [ # # ]: 0 : if (cfg->nb_max_matches != MLX5_REGEX_MAX_MATCHES) {
299 : 0 : DRV_LOG(ERR, "nb_max_matches is not configurable.");
300 : 0 : rte_errno = EINVAL;
301 : 0 : return -rte_errno;
302 : : }
303 : 0 : priv->nb_queues = cfg->nb_queue_pairs;
304 : 0 : dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
305 : 0 : priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
306 : 0 : priv->nb_queues, 0);
307 [ # # ]: 0 : if (!priv->qps) {
308 : 0 : DRV_LOG(ERR, "can't allocate qps memory");
309 : 0 : rte_errno = ENOMEM;
310 : 0 : return -rte_errno;
311 : : }
312 : 0 : priv->nb_max_matches = cfg->nb_max_matches;
313 [ # # ]: 0 : if (cfg->rule_db != NULL) {
314 : 0 : ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
315 : 0 : cfg->rule_db_len);
316 [ # # ]: 0 : if (ret < 0) {
317 : 0 : DRV_LOG(ERR, "Failed to program rxp rules.");
318 : 0 : rte_errno = ENODEV;
319 : 0 : goto configure_error;
320 : : }
321 : : } else
322 : 0 : DRV_LOG(DEBUG, "Regex config without rules programming!");
323 : : return 0;
324 : : configure_error:
325 : 0 : rte_free(priv->qps);
326 : 0 : return -rte_errno;
327 : : }
|