d39b7e32c6d447208fd104aa818fd765990d9962
[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         .dev_db_import = mlx5_regex_rules_db_import,
28 };
29
30 static struct ibv_device *
31 mlx5_regex_get_ib_device_match(struct rte_pci_addr *addr)
32 {
33         int n;
34         struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
35         struct ibv_device *ibv_match = NULL;
36
37         if (!ibv_list) {
38                 rte_errno = ENOSYS;
39                 return NULL;
40         }
41         while (n-- > 0) {
42                 struct rte_pci_addr pci_addr;
43
44                 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
45                 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
46                         continue;
47                 if (rte_pci_addr_cmp(addr, &pci_addr))
48                         continue;
49                 ibv_match = ibv_list[n];
50                 break;
51         }
52         if (!ibv_match)
53                 rte_errno = ENOENT;
54         mlx5_glue->free_device_list(ibv_list);
55         return ibv_match;
56 }
57 static int
58 mlx5_regex_engines_status(struct ibv_context *ctx, int num_engines)
59 {
60         uint32_t fpga_ident = 0;
61         int err;
62         int i;
63
64         for (i = 0; i < num_engines; i++) {
65                 err = mlx5_devx_regex_register_read(ctx, i,
66                                                     MLX5_RXP_CSR_IDENTIFIER,
67                                                     &fpga_ident);
68                 fpga_ident = (fpga_ident & (0x0000FFFF));
69                 if (err || fpga_ident != MLX5_RXP_IDENTIFIER) {
70                         DRV_LOG(ERR, "Failed setup RXP %d err %d database "
71                                 "memory 0x%x", i, err, fpga_ident);
72                         if (!err)
73                                 err = EINVAL;
74                         return err;
75                 }
76         }
77         return 0;
78 }
79
80 static void
81 mlx5_regex_get_name(char *name, struct rte_pci_device *pci_dev __rte_unused)
82 {
83         sprintf(name, "mlx5_regex_%02x:%02x.%02x", pci_dev->addr.bus,
84                 pci_dev->addr.devid, pci_dev->addr.function);
85 }
86
87 static int
88 mlx5_regex_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
89                      struct rte_pci_device *pci_dev)
90 {
91         struct ibv_device *ibv;
92         struct mlx5_regex_priv *priv = NULL;
93         struct ibv_context *ctx = NULL;
94         struct mlx5_hca_attr attr;
95         char name[RTE_REGEXDEV_NAME_MAX_LEN];
96         int ret;
97
98         ibv = mlx5_regex_get_ib_device_match(&pci_dev->addr);
99         if (!ibv) {
100                 DRV_LOG(ERR, "No matching IB device for PCI slot "
101                         PCI_PRI_FMT ".", pci_dev->addr.domain,
102                         pci_dev->addr.bus, pci_dev->addr.devid,
103                         pci_dev->addr.function);
104                 return -rte_errno;
105         }
106         DRV_LOG(INFO, "PCI information matches for device \"%s\".",
107                 ibv->name);
108         ctx = mlx5_glue->dv_open_device(ibv);
109         if (!ctx) {
110                 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
111                 rte_errno = ENODEV;
112                 return -rte_errno;
113         }
114         ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
115         if (ret) {
116                 DRV_LOG(ERR, "Unable to read HCA capabilities.");
117                 rte_errno = ENOTSUP;
118                 goto error;
119         } else if (!attr.regex || attr.regexp_num_of_engines == 0) {
120                 DRV_LOG(ERR, "Not enough capabilities to support RegEx, maybe "
121                         "old FW/OFED version?");
122                 rte_errno = ENOTSUP;
123                 goto error;
124         }
125         if (mlx5_regex_engines_status(ctx, 2)) {
126                 DRV_LOG(ERR, "RegEx engine error.");
127                 rte_errno = ENOMEM;
128                 goto error;
129         }
130         priv = rte_zmalloc("mlx5 regex device private", sizeof(*priv),
131                            RTE_CACHE_LINE_SIZE);
132         if (!priv) {
133                 DRV_LOG(ERR, "Failed to allocate private memory.");
134                 rte_errno = ENOMEM;
135                 goto error;
136         }
137         priv->ctx = ctx;
138         priv->nb_engines = 2; /* attr.regexp_num_of_engines */
139         /* Default RXP programming mode to Shared. */
140         priv->prog_mode = MLX5_RXP_SHARED_PROG_MODE;
141         mlx5_regex_get_name(name, pci_dev);
142         priv->regexdev = rte_regexdev_register(name);
143         if (priv->regexdev == NULL) {
144                 DRV_LOG(ERR, "Failed to register RegEx device.");
145                 rte_errno = rte_errno ? rte_errno : EINVAL;
146                 goto error;
147         }
148         ret = mlx5_glue->devx_query_eqn(ctx, 0, &priv->eqn);
149         if (ret) {
150                 DRV_LOG(ERR, "can't query event queue number.");
151                 rte_errno = ENOMEM;
152                 goto error;
153         }
154         priv->uar = mlx5_glue->devx_alloc_uar(ctx, 0);
155         if (!priv->uar) {
156                 DRV_LOG(ERR, "can't allocate uar.");
157                 rte_errno = ENOMEM;
158                 goto error;
159         }
160         priv->pd = mlx5_glue->alloc_pd(ctx);
161         if (!priv->pd) {
162                 DRV_LOG(ERR, "can't allocate pd.");
163                 rte_errno = ENOMEM;
164                 goto error;
165         }
166         priv->regexdev->dev_ops = &mlx5_regexdev_ops;
167         priv->regexdev->device = (struct rte_device *)pci_dev;
168         priv->regexdev->data->dev_private = priv;
169         priv->regexdev->state = RTE_REGEXDEV_READY;
170         return 0;
171
172 error:
173         if (priv->pd)
174                 mlx5_glue->dealloc_pd(priv->pd);
175         if (priv->uar)
176                 mlx5_glue->devx_free_uar(priv->uar);
177         if (priv->regexdev)
178                 rte_regexdev_unregister(priv->regexdev);
179         if (ctx)
180                 mlx5_glue->close_device(ctx);
181         if (priv)
182                 rte_free(priv);
183         return -rte_errno;
184 }
185
186 static int
187 mlx5_regex_pci_remove(struct rte_pci_device *pci_dev)
188 {
189         char name[RTE_REGEXDEV_NAME_MAX_LEN];
190         struct rte_regexdev *dev;
191         struct mlx5_regex_priv *priv = NULL;
192
193         mlx5_regex_get_name(name, pci_dev);
194         dev = rte_regexdev_get_device_by_name(name);
195         if (!dev)
196                 return 0;
197         priv = dev->data->dev_private;
198         if (priv) {
199                 if (priv->pd)
200                         mlx5_glue->dealloc_pd(priv->pd);
201                 if (priv->uar)
202                         mlx5_glue->devx_free_uar(priv->uar);
203                 if (priv->regexdev)
204                         rte_regexdev_unregister(priv->regexdev);
205                 if (priv->ctx)
206                         mlx5_glue->close_device(priv->ctx);
207                 if (priv->regexdev)
208                         rte_regexdev_unregister(priv->regexdev);
209                 rte_free(priv);
210         }
211         return 0;
212 }
213
214 static const struct rte_pci_id mlx5_regex_pci_id_map[] = {
215         {
216                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
217                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
218         },
219         {
220                 .vendor_id = 0
221         }
222 };
223
224 static struct rte_pci_driver mlx5_regex_driver = {
225         .driver = {
226                 .name = "mlx5_regex",
227         },
228         .id_table = mlx5_regex_pci_id_map,
229         .probe = mlx5_regex_pci_probe,
230         .remove = mlx5_regex_pci_remove,
231         .drv_flags = 0,
232 };
233
234 RTE_INIT(rte_mlx5_regex_init)
235 {
236         if (mlx5_glue)
237                 rte_pci_register(&mlx5_regex_driver);
238 }
239
240 RTE_LOG_REGISTER(mlx5_regex_logtype, pmd.regex.mlx5, NOTICE)
241 RTE_PMD_EXPORT_NAME(net_mlx5_regex, __COUNTER__);
242 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_regex, mlx5_regex_pci_id_map);
243 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_regex, "* ib_uverbs & mlx5_core & mlx5_ib");