regex/mlx5: support configuration
[dpdk.git] / drivers / regex / mlx5 / mlx5_regex.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <rte_malloc.h>
6 #include <rte_log.h>
7 #include <rte_errno.h>
8 #include <rte_bus_pci.h>
9 #include <rte_pci.h>
10 #include <rte_regexdev.h>
11 #include <rte_regexdev_core.h>
12 #include <rte_regexdev_driver.h>
13
14 #include <mlx5_glue.h>
15 #include <mlx5_devx_cmds.h>
16 #include <mlx5_prm.h>
17
18 #include "mlx5_regex.h"
19 #include "mlx5_regex_utils.h"
20 #include "mlx5_rxp_csrs.h"
21
22 int mlx5_regex_logtype;
23
24 const struct rte_regexdev_ops mlx5_regexdev_ops = {
25         .dev_info_get = mlx5_regex_info_get,
26         .dev_configure = mlx5_regex_configure,
27 };
28
29 static struct ibv_device *
30 mlx5_regex_get_ib_device_match(struct rte_pci_addr *addr)
31 {
32         int n;
33         struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
34         struct ibv_device *ibv_match = NULL;
35
36         if (!ibv_list) {
37                 rte_errno = ENOSYS;
38                 return NULL;
39         }
40         while (n-- > 0) {
41                 struct rte_pci_addr pci_addr;
42
43                 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
44                 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
45                         continue;
46                 if (rte_pci_addr_cmp(addr, &pci_addr))
47                         continue;
48                 ibv_match = ibv_list[n];
49                 break;
50         }
51         if (!ibv_match)
52                 rte_errno = ENOENT;
53         mlx5_glue->free_device_list(ibv_list);
54         return ibv_match;
55 }
56 static int
57 mlx5_regex_engines_status(struct ibv_context *ctx, int num_engines)
58 {
59         uint32_t fpga_ident = 0;
60         int err;
61         int i;
62
63         for (i = 0; i < num_engines; i++) {
64                 err = mlx5_devx_regex_register_read(ctx, i,
65                                                     MLX5_RXP_CSR_IDENTIFIER,
66                                                     &fpga_ident);
67                 fpga_ident = (fpga_ident & (0x0000FFFF));
68                 if (err || fpga_ident != MLX5_RXP_IDENTIFIER) {
69                         DRV_LOG(ERR, "Failed setup RXP %d err %d database "
70                                 "memory 0x%x", i, err, fpga_ident);
71                         if (!err)
72                                 err = EINVAL;
73                         return err;
74                 }
75         }
76         return 0;
77 }
78
79 static void
80 mlx5_regex_get_name(char *name, struct rte_pci_device *pci_dev __rte_unused)
81 {
82         sprintf(name, "mlx5_regex_%02x:%02x.%02x", pci_dev->addr.bus,
83                 pci_dev->addr.devid, pci_dev->addr.function);
84 }
85
86 static int
87 mlx5_regex_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
88                      struct rte_pci_device *pci_dev)
89 {
90         struct ibv_device *ibv;
91         struct mlx5_regex_priv *priv = NULL;
92         struct ibv_context *ctx = NULL;
93         struct mlx5_hca_attr attr;
94         char name[RTE_REGEXDEV_NAME_MAX_LEN];
95         int ret;
96
97         ibv = mlx5_regex_get_ib_device_match(&pci_dev->addr);
98         if (!ibv) {
99                 DRV_LOG(ERR, "No matching IB device for PCI slot "
100                         PCI_PRI_FMT ".", pci_dev->addr.domain,
101                         pci_dev->addr.bus, pci_dev->addr.devid,
102                         pci_dev->addr.function);
103                 return -rte_errno;
104         }
105         DRV_LOG(INFO, "PCI information matches for device \"%s\".",
106                 ibv->name);
107         ctx = mlx5_glue->dv_open_device(ibv);
108         if (!ctx) {
109                 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
110                 rte_errno = ENODEV;
111                 return -rte_errno;
112         }
113         ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
114         if (ret) {
115                 DRV_LOG(ERR, "Unable to read HCA capabilities.");
116                 rte_errno = ENOTSUP;
117                 goto error;
118         } else if (!attr.regex || attr.regexp_num_of_engines == 0) {
119                 DRV_LOG(ERR, "Not enough capabilities to support RegEx, maybe "
120                         "old FW/OFED version?");
121                 rte_errno = ENOTSUP;
122                 goto error;
123         }
124         if (mlx5_regex_engines_status(ctx, 2)) {
125                 DRV_LOG(ERR, "RegEx engine error.");
126                 rte_errno = ENOMEM;
127                 goto error;
128         }
129         priv = rte_zmalloc("mlx5 regex device private", sizeof(*priv),
130                            RTE_CACHE_LINE_SIZE);
131         if (!priv) {
132                 DRV_LOG(ERR, "Failed to allocate private memory.");
133                 rte_errno = ENOMEM;
134                 goto error;
135         }
136         priv->ctx = ctx;
137         mlx5_regex_get_name(name, pci_dev);
138         priv->regexdev = rte_regexdev_register(name);
139         if (priv->regexdev == NULL) {
140                 DRV_LOG(ERR, "Failed to register RegEx device.");
141                 rte_errno = rte_errno ? rte_errno : EINVAL;
142                 goto error;
143         }
144         priv->regexdev->dev_ops = &mlx5_regexdev_ops;
145         priv->regexdev->device = (struct rte_device *)pci_dev;
146         priv->regexdev->data->dev_private = priv;
147         priv->regexdev->state = RTE_REGEXDEV_READY;
148         return 0;
149
150 error:
151         if (ctx)
152                 mlx5_glue->close_device(ctx);
153         if (priv)
154                 rte_free(priv);
155         return -rte_errno;
156 }
157
158 static int
159 mlx5_regex_pci_remove(struct rte_pci_device *pci_dev)
160 {
161         char name[RTE_REGEXDEV_NAME_MAX_LEN];
162         struct rte_regexdev *dev;
163         struct mlx5_regex_priv *priv = NULL;
164
165         mlx5_regex_get_name(name, pci_dev);
166         dev = rte_regexdev_get_device_by_name(name);
167         if (!dev)
168                 return 0;
169         priv = dev->data->dev_private;
170         if (priv) {
171                 if (priv->ctx)
172                         mlx5_glue->close_device(priv->ctx);
173                 if (priv->regexdev)
174                         rte_regexdev_unregister(priv->regexdev);
175                 rte_free(priv);
176         }
177         return 0;
178 }
179
180 static const struct rte_pci_id mlx5_regex_pci_id_map[] = {
181         {
182                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
183                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
184         },
185         {
186                 .vendor_id = 0
187         }
188 };
189
190 static struct rte_pci_driver mlx5_regex_driver = {
191         .driver = {
192                 .name = "mlx5_regex",
193         },
194         .id_table = mlx5_regex_pci_id_map,
195         .probe = mlx5_regex_pci_probe,
196         .remove = mlx5_regex_pci_remove,
197         .drv_flags = 0,
198 };
199
200 RTE_INIT(rte_mlx5_regex_init)
201 {
202         if (mlx5_glue)
203                 rte_pci_register(&mlx5_regex_driver);
204 }
205
206 RTE_LOG_REGISTER(mlx5_regex_logtype, pmd.regex.mlx5, NOTICE)
207 RTE_PMD_EXPORT_NAME(net_mlx5_regex, __COUNTER__);
208 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_regex, mlx5_regex_pci_id_map);
209 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_regex, "* ib_uverbs & mlx5_core & mlx5_ib");