1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
9 #include <rte_regexdev.h>
10 #include <rte_regexdev_core.h>
11 #include <rte_regexdev_driver.h>
13 #include <mlx5_common_pci.h>
14 #include <mlx5_glue.h>
15 #include <mlx5_devx_cmds.h>
18 #include "mlx5_regex.h"
19 #include "mlx5_regex_utils.h"
20 #include "mlx5_rxp_csrs.h"
22 #define MLX5_REGEX_DRIVER_NAME regex_mlx5
23 #define MLX5_REGEX_LOG_NAME pmd.regex.mlx5
25 int mlx5_regex_logtype;
27 const struct rte_regexdev_ops mlx5_regexdev_ops = {
28 .dev_info_get = mlx5_regex_info_get,
29 .dev_configure = mlx5_regex_configure,
30 .dev_db_import = mlx5_regex_rules_db_import,
31 .dev_qp_setup = mlx5_regex_qp_setup,
32 .dev_start = mlx5_regex_start,
33 .dev_stop = mlx5_regex_stop,
34 .dev_close = mlx5_regex_close,
38 mlx5_regex_start(struct rte_regexdev *dev __rte_unused)
44 mlx5_regex_stop(struct rte_regexdev *dev __rte_unused)
50 mlx5_regex_close(struct rte_regexdev *dev __rte_unused)
55 static struct ibv_device *
56 mlx5_regex_get_ib_device_match(struct rte_pci_addr *addr)
59 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
60 struct ibv_device *ibv_match = NULL;
67 struct rte_pci_addr pci_addr;
69 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
70 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
72 if (rte_pci_addr_cmp(addr, &pci_addr))
74 ibv_match = ibv_list[n];
79 mlx5_glue->free_device_list(ibv_list);
83 mlx5_regex_engines_status(struct ibv_context *ctx, int num_engines)
85 uint32_t fpga_ident = 0;
89 for (i = 0; i < num_engines; i++) {
90 err = mlx5_devx_regex_register_read(ctx, i,
91 MLX5_RXP_CSR_IDENTIFIER,
93 fpga_ident = (fpga_ident & (0x0000FFFF));
94 if (err || fpga_ident != MLX5_RXP_IDENTIFIER) {
95 DRV_LOG(ERR, "Failed setup RXP %d err %d database "
96 "memory 0x%x", i, err, fpga_ident);
106 mlx5_regex_get_name(char *name, struct rte_pci_device *pci_dev __rte_unused)
108 sprintf(name, "mlx5_regex_%02x:%02x.%02x", pci_dev->addr.bus,
109 pci_dev->addr.devid, pci_dev->addr.function);
113 mlx5_regex_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
114 struct rte_pci_device *pci_dev)
116 struct ibv_device *ibv;
117 struct mlx5_regex_priv *priv = NULL;
118 struct ibv_context *ctx = NULL;
119 struct mlx5_hca_attr attr;
120 char name[RTE_REGEXDEV_NAME_MAX_LEN];
124 ibv = mlx5_regex_get_ib_device_match(&pci_dev->addr);
126 DRV_LOG(ERR, "No matching IB device for PCI slot "
127 PCI_PRI_FMT ".", pci_dev->addr.domain,
128 pci_dev->addr.bus, pci_dev->addr.devid,
129 pci_dev->addr.function);
132 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
134 ctx = mlx5_glue->dv_open_device(ibv);
136 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
140 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
142 DRV_LOG(ERR, "Unable to read HCA capabilities.");
145 } else if (!attr.regex || attr.regexp_num_of_engines == 0) {
146 DRV_LOG(ERR, "Not enough capabilities to support RegEx, maybe "
147 "old FW/OFED version?");
151 if (mlx5_regex_engines_status(ctx, 2)) {
152 DRV_LOG(ERR, "RegEx engine error.");
156 priv = rte_zmalloc("mlx5 regex device private", sizeof(*priv),
157 RTE_CACHE_LINE_SIZE);
159 DRV_LOG(ERR, "Failed to allocate private memory.");
163 priv->sq_ts_format = attr.sq_ts_format;
165 priv->nb_engines = 2; /* attr.regexp_num_of_engines */
166 ret = mlx5_devx_regex_register_read(priv->ctx, 0,
167 MLX5_RXP_CSR_IDENTIFIER, &val);
169 DRV_LOG(ERR, "CSR read failed!");
172 if (val == MLX5_RXP_BF2_IDENTIFIER)
174 /* Default RXP programming mode to Shared. */
175 priv->prog_mode = MLX5_RXP_SHARED_PROG_MODE;
176 mlx5_regex_get_name(name, pci_dev);
177 priv->regexdev = rte_regexdev_register(name);
178 if (priv->regexdev == NULL) {
179 DRV_LOG(ERR, "Failed to register RegEx device.");
180 rte_errno = rte_errno ? rte_errno : EINVAL;
184 * This PMD always claims the write memory barrier on UAR
185 * registers writings, it is safe to allocate UAR with any
186 * memory mapping type.
188 priv->uar = mlx5_devx_alloc_uar(ctx, -1);
190 DRV_LOG(ERR, "can't allocate uar.");
194 priv->pd = mlx5_glue->alloc_pd(ctx);
196 DRV_LOG(ERR, "can't allocate pd.");
200 priv->regexdev->dev_ops = &mlx5_regexdev_ops;
201 priv->regexdev->enqueue = mlx5_regexdev_enqueue;
202 priv->regexdev->dequeue = mlx5_regexdev_dequeue;
203 priv->regexdev->device = (struct rte_device *)pci_dev;
204 priv->regexdev->data->dev_private = priv;
205 priv->regexdev->state = RTE_REGEXDEV_READY;
206 priv->mr_scache.reg_mr_cb = mlx5_common_verbs_reg_mr;
207 priv->mr_scache.dereg_mr_cb = mlx5_common_verbs_dereg_mr;
208 ret = mlx5_mr_btree_init(&priv->mr_scache.cache,
209 MLX5_MR_BTREE_CACHE_N * 2,
212 DRV_LOG(ERR, "MR init tree failed.");
220 mlx5_glue->dealloc_pd(priv->pd);
222 mlx5_glue->devx_free_uar(priv->uar);
224 rte_regexdev_unregister(priv->regexdev);
227 mlx5_glue->close_device(ctx);
234 mlx5_regex_pci_remove(struct rte_pci_device *pci_dev)
236 char name[RTE_REGEXDEV_NAME_MAX_LEN];
237 struct rte_regexdev *dev;
238 struct mlx5_regex_priv *priv = NULL;
240 mlx5_regex_get_name(name, pci_dev);
241 dev = rte_regexdev_get_device_by_name(name);
244 priv = dev->data->dev_private;
247 mlx5_glue->dealloc_pd(priv->pd);
249 mlx5_glue->devx_free_uar(priv->uar);
251 rte_regexdev_unregister(priv->regexdev);
253 mlx5_glue->close_device(priv->ctx);
255 rte_regexdev_unregister(priv->regexdev);
261 static const struct rte_pci_id mlx5_regex_pci_id_map[] = {
263 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
264 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
267 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
268 PCI_DEVICE_ID_MELLANOX_CONNECTX7BF)
275 static struct mlx5_pci_driver mlx5_regex_driver = {
276 .driver_class = MLX5_CLASS_REGEX,
279 .name = RTE_STR(MLX5_REGEX_DRIVER_NAME),
281 .id_table = mlx5_regex_pci_id_map,
282 .probe = mlx5_regex_pci_probe,
283 .remove = mlx5_regex_pci_remove,
288 RTE_INIT(rte_mlx5_regex_init)
292 mlx5_pci_driver_register(&mlx5_regex_driver);
295 RTE_LOG_REGISTER(mlx5_regex_logtype, MLX5_REGEX_LOG_NAME, NOTICE)
296 RTE_PMD_EXPORT_NAME(MLX5_REGEX_DRIVER_NAME, __COUNTER__);
297 RTE_PMD_REGISTER_PCI_TABLE(MLX5_REGEX_DRIVER_NAME, mlx5_regex_pci_id_map);
298 RTE_PMD_REGISTER_KMOD_DEP(MLX5_REGEX_DRIVER_NAME, "* ib_uverbs & mlx5_core & mlx5_ib");