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