1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
8 #include <rte_bus_pci.h>
10 #include <rte_regexdev.h>
11 #include <rte_regexdev_core.h>
12 #include <rte_regexdev_driver.h>
14 #include <mlx5_glue.h>
15 #include <mlx5_devx_cmds.h>
18 #include "mlx5_regex.h"
19 #include "mlx5_regex_utils.h"
21 int mlx5_regex_logtype;
23 const struct rte_regexdev_ops mlx5_regexdev_ops = {
24 .dev_info_get = mlx5_regex_info_get,
27 static struct ibv_device *
28 mlx5_regex_get_ib_device_match(struct rte_pci_addr *addr)
31 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
32 struct ibv_device *ibv_match = NULL;
39 struct rte_pci_addr pci_addr;
41 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
42 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
44 if (rte_pci_addr_cmp(addr, &pci_addr))
46 ibv_match = ibv_list[n];
51 mlx5_glue->free_device_list(ibv_list);
56 mlx5_regex_get_name(char *name, struct rte_pci_device *pci_dev __rte_unused)
58 sprintf(name, "mlx5_regex_%02x:%02x.%02x", pci_dev->addr.bus,
59 pci_dev->addr.devid, pci_dev->addr.function);
63 mlx5_regex_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
64 struct rte_pci_device *pci_dev)
66 struct ibv_device *ibv;
67 struct mlx5_regex_priv *priv = NULL;
68 struct ibv_context *ctx = NULL;
69 struct mlx5_hca_attr attr;
70 char name[RTE_REGEXDEV_NAME_MAX_LEN];
73 ibv = mlx5_regex_get_ib_device_match(&pci_dev->addr);
75 DRV_LOG(ERR, "No matching IB device for PCI slot "
76 PCI_PRI_FMT ".", pci_dev->addr.domain,
77 pci_dev->addr.bus, pci_dev->addr.devid,
78 pci_dev->addr.function);
81 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
83 ctx = mlx5_glue->dv_open_device(ibv);
85 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
89 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
91 DRV_LOG(ERR, "Unable to read HCA capabilities.");
94 } else if (!attr.regex || attr.regexp_num_of_engines == 0) {
95 DRV_LOG(ERR, "Not enough capabilities to support RegEx, maybe "
96 "old FW/OFED version?");
100 priv = rte_zmalloc("mlx5 regex device private", sizeof(*priv),
101 RTE_CACHE_LINE_SIZE);
103 DRV_LOG(ERR, "Failed to allocate private memory.");
108 mlx5_regex_get_name(name, pci_dev);
109 priv->regexdev = rte_regexdev_register(name);
110 if (priv->regexdev == NULL) {
111 DRV_LOG(ERR, "Failed to register RegEx device.");
112 rte_errno = rte_errno ? rte_errno : EINVAL;
115 priv->regexdev->dev_ops = &mlx5_regexdev_ops;
116 priv->regexdev->device = (struct rte_device *)pci_dev;
117 priv->regexdev->data->dev_private = priv;
122 mlx5_glue->close_device(ctx);
129 mlx5_regex_pci_remove(struct rte_pci_device *pci_dev)
131 char name[RTE_REGEXDEV_NAME_MAX_LEN];
132 struct rte_regexdev *dev;
133 struct mlx5_regex_priv *priv = NULL;
135 mlx5_regex_get_name(name, pci_dev);
136 dev = rte_regexdev_get_device_by_name(name);
139 priv = dev->data->dev_private;
142 mlx5_glue->close_device(priv->ctx);
144 rte_regexdev_unregister(priv->regexdev);
150 static const struct rte_pci_id mlx5_regex_pci_id_map[] = {
152 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
153 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
160 static struct rte_pci_driver mlx5_regex_driver = {
162 .name = "mlx5_regex",
164 .id_table = mlx5_regex_pci_id_map,
165 .probe = mlx5_regex_pci_probe,
166 .remove = mlx5_regex_pci_remove,
170 RTE_INIT(rte_mlx5_regex_init)
173 rte_pci_register(&mlx5_regex_driver);
176 RTE_LOG_REGISTER(mlx5_regex_logtype, pmd.regex.mlx5, NOTICE)
177 RTE_PMD_EXPORT_NAME(net_mlx5_regex, __COUNTER__);
178 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_regex, mlx5_regex_pci_id_map);
179 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_regex, "* ib_uverbs & mlx5_core & mlx5_ib");