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"
20 #include "mlx5_rxp_csrs.h"
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
28 #define MLX5_REGEX_RXP_ROF2_LINE_LEN 34
30 /* Private Declarations */
32 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
33 uint32_t access, struct mlx5_regex_mkey *mkey);
35 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey);
38 mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
39 struct rte_regexdev_info *info)
41 info->max_matches = MLX5_REGEX_MAX_MATCHES;
42 info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
43 info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
44 info->max_groups = MLX5_REGEX_MAX_GROUPS;
45 info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
46 RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
48 info->max_queue_pairs = UINT16_MAX;
53 rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
54 uint32_t access, struct mlx5_regex_mkey *mkey)
56 struct mlx5_devx_mkey_attr mkey_attr;
58 /* Register the memory. */
59 mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
61 DRV_LOG(ERR, "Failed to register memory!");
65 mkey_attr = (struct mlx5_devx_mkey_attr) {
66 .addr = (uintptr_t)ptr,
67 .size = (uint32_t)size,
68 .umem_id = mlx5_os_get_umem_id(mkey->umem),
72 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
73 mkey_attr.pd = priv->cdev->pdn;
75 mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
77 DRV_LOG(ERR, "Failed to create direct mkey!");
84 rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
87 claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
89 claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
93 mlx5_regex_rules_db_import(struct rte_regexdev *dev,
94 const char *rule_db, uint32_t rule_db_len)
96 struct mlx5_regex_priv *priv = dev->data->dev_private;
97 struct mlx5_regex_mkey mkey;
102 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
103 DRV_LOG(ERR, "RXP programming mode not set!");
106 if (rule_db == NULL) {
107 DRV_LOG(ERR, "Database empty!");
110 if (rule_db_len == 0)
112 /* copy rules - rules have to be 4KB aligned. */
113 ptr = rte_malloc("", rule_db_len, 1 << 12);
115 DRV_LOG(ERR, "Failed to allocate rules file memory.");
118 rte_memcpy(ptr, rule_db, rule_db_len);
119 /* Register umem and create rof mkey. */
120 ret = rxp_create_mkey(priv, ptr, rule_db_len, /*access=*/7, &mkey);
124 for (id = 0; id < priv->nb_engines; id++) {
125 ret = mlx5_devx_regex_rules_program(priv->cdev->ctx, id,
126 mkey.mkey->id, rule_db_len, (uintptr_t)ptr);
128 DRV_LOG(ERR, "Failed to program rxp rules.");
134 rxp_destroy_mkey(&mkey);
140 mlx5_regex_configure(struct rte_regexdev *dev,
141 const struct rte_regexdev_config *cfg)
143 struct mlx5_regex_priv *priv = dev->data->dev_private;
146 if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
148 priv->nb_queues = cfg->nb_queue_pairs;
149 dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
150 priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
152 if (!priv->nb_queues) {
153 DRV_LOG(ERR, "can't allocate qps memory");
157 priv->nb_max_matches = cfg->nb_max_matches;
158 if (cfg->rule_db != NULL) {
159 ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
162 DRV_LOG(ERR, "Failed to program rxp rules.");
164 goto configure_error;
167 DRV_LOG(DEBUG, "Regex config without rules programming!");