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 21 int mlx5_regex_logtype; 22 23 const struct rte_regexdev_ops mlx5_regexdev_ops = { 24 .dev_info_get = mlx5_regex_info_get, 25 }; 26 27 static struct ibv_device * 28 mlx5_regex_get_ib_device_match(struct rte_pci_addr *addr) 29 { 30 int n; 31 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n); 32 struct ibv_device *ibv_match = NULL; 33 34 if (!ibv_list) { 35 rte_errno = ENOSYS; 36 return NULL; 37 } 38 while (n-- > 0) { 39 struct rte_pci_addr pci_addr; 40 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)) 43 continue; 44 if (rte_pci_addr_cmp(addr, &pci_addr)) 45 continue; 46 ibv_match = ibv_list[n]; 47 break; 48 } 49 if (!ibv_match) 50 rte_errno = ENOENT; 51 mlx5_glue->free_device_list(ibv_list); 52 return ibv_match; 53 } 54 55 static void 56 mlx5_regex_get_name(char *name, struct rte_pci_device *pci_dev __rte_unused) 57 { 58 sprintf(name, "mlx5_regex_%02x:%02x.%02x", pci_dev->addr.bus, 59 pci_dev->addr.devid, pci_dev->addr.function); 60 } 61 62 static int 63 mlx5_regex_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 64 struct rte_pci_device *pci_dev) 65 { 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]; 71 int ret; 72 73 ibv = mlx5_regex_get_ib_device_match(&pci_dev->addr); 74 if (!ibv) { 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); 79 return -rte_errno; 80 } 81 DRV_LOG(INFO, "PCI information matches for device \"%s\".", 82 ibv->name); 83 ctx = mlx5_glue->dv_open_device(ibv); 84 if (!ctx) { 85 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name); 86 rte_errno = ENODEV; 87 return -rte_errno; 88 } 89 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr); 90 if (ret) { 91 DRV_LOG(ERR, "Unable to read HCA capabilities."); 92 rte_errno = ENOTSUP; 93 goto error; 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?"); 97 rte_errno = ENOTSUP; 98 goto error; 99 } 100 priv = rte_zmalloc("mlx5 regex device private", sizeof(*priv), 101 RTE_CACHE_LINE_SIZE); 102 if (!priv) { 103 DRV_LOG(ERR, "Failed to allocate private memory."); 104 rte_errno = ENOMEM; 105 goto error; 106 } 107 priv->ctx = ctx; 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; 113 goto error; 114 } 115 priv->regexdev->dev_ops = &mlx5_regexdev_ops; 116 priv->regexdev->device = (struct rte_device *)pci_dev; 117 priv->regexdev->data->dev_private = priv; 118 return 0; 119 120 error: 121 if (ctx) 122 mlx5_glue->close_device(ctx); 123 if (priv) 124 rte_free(priv); 125 return -rte_errno; 126 } 127 128 static int 129 mlx5_regex_pci_remove(struct rte_pci_device *pci_dev) 130 { 131 char name[RTE_REGEXDEV_NAME_MAX_LEN]; 132 struct rte_regexdev *dev; 133 struct mlx5_regex_priv *priv = NULL; 134 135 mlx5_regex_get_name(name, pci_dev); 136 dev = rte_regexdev_get_device_by_name(name); 137 if (!dev) 138 return 0; 139 priv = dev->data->dev_private; 140 if (priv) { 141 if (priv->ctx) 142 mlx5_glue->close_device(priv->ctx); 143 if (priv->regexdev) 144 rte_regexdev_unregister(priv->regexdev); 145 rte_free(priv); 146 } 147 return 0; 148 } 149 150 static const struct rte_pci_id mlx5_regex_pci_id_map[] = { 151 { 152 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, 153 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF) 154 }, 155 { 156 .vendor_id = 0 157 } 158 }; 159 160 static struct rte_pci_driver mlx5_regex_driver = { 161 .driver = { 162 .name = "mlx5_regex", 163 }, 164 .id_table = mlx5_regex_pci_id_map, 165 .probe = mlx5_regex_pci_probe, 166 .remove = mlx5_regex_pci_remove, 167 .drv_flags = 0, 168 }; 169 170 RTE_INIT(rte_mlx5_regex_init) 171 { 172 if (mlx5_glue) 173 rte_pci_register(&mlx5_regex_driver); 174 } 175 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"); 180