1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
7 #include <rte_malloc.h>
8 #include <rte_regexdev.h>
9 #include <rte_regexdev_core.h>
10 #include <rte_regexdev_driver.h>
13 #include <mlx5_glue.h>
14 #include <mlx5_devx_cmds.h>
16 #include <mlx5_common_os.h>
18 #include "mlx5_regex.h"
19 #include "mlx5_regex_utils.h"
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
27 #define MLX5_REGEX_RXP_ROF2_LINE_LEN 34
29 /* Private Declarations */
31 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
32 uint32_t access, struct mlx5_regex_mkey *mkey);
34 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey);
37 mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
38 struct rte_regexdev_info *info)
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;
47 info->max_queue_pairs = UINT16_MAX;
52 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
53 uint32_t access, struct mlx5_regex_mkey *mkey)
55 struct mlx5_devx_mkey_attr mkey_attr;
57 /* Register the memory. */
58 mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
60 DRV_LOG(ERR, "Failed to register memory!");
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),
71 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
72 mkey_attr.pd = priv->cdev->pdn;
74 mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
76 DRV_LOG(ERR, "Failed to create direct mkey!");
83 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
86 claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
88 claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
92 mlx5_regex_rules_db_import(struct rte_regexdev *dev,
93 const char *rule_db, uint32_t rule_db_len)
95 struct mlx5_regex_priv *priv = dev->data->dev_private;
96 struct mlx5_regex_mkey mkey;
101 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
102 DRV_LOG(ERR, "RXP programming mode not set!");
105 if (rule_db == NULL) {
106 DRV_LOG(ERR, "Database empty!");
109 if (rule_db_len == 0)
111 /* copy rules - rules have to be 4KB aligned. */
112 ptr = rte_malloc("", rule_db_len, 1 << 12);
114 DRV_LOG(ERR, "Failed to allocate rules file memory.");
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);
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);
127 DRV_LOG(ERR, "Failed to program rxp rules.");
133 rxp_destroy_mkey(&mkey);
139 mlx5_regex_configure(struct rte_regexdev *dev,
140 const struct rte_regexdev_config *cfg)
142 struct mlx5_regex_priv *priv = dev->data->dev_private;
145 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
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) *
151 if (!priv->nb_queues) {
152 DRV_LOG(ERR, "can't allocate qps memory");
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,
161 DRV_LOG(ERR, "Failed to program rxp rules.");
163 goto configure_error;
166 DRV_LOG(DEBUG, "Regex config without rules programming!");