1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies Ltd
9 #include <rte_malloc.h>
10 #include <rte_errno.h>
11 #include <rte_bus_auxiliary.h>
12 #include <rte_common.h>
13 #include "eal_filesystem.h"
15 #include "mlx5_common_utils.h"
16 #include "mlx5_common_private.h"
18 #define AUXILIARY_SYSFS_PATH "/sys/bus/auxiliary/devices"
19 #define MLX5_AUXILIARY_PREFIX "mlx5_core.sf."
22 mlx5_auxiliary_get_child_name(const char *dev, const char *node,
23 char *child, size_t size)
27 MKSTR(path, "%s/%s%s", AUXILIARY_SYSFS_PATH, dev, node);
34 /* Get the first file name. */
35 while ((dent = readdir(dir)) != NULL) {
36 if (dent->d_name[0] != '.')
44 if (rte_strscpy(child, dent->d_name, size) < 0)
50 mlx5_auxiliary_get_pci_path(const struct rte_auxiliary_device *dev,
51 char *sysfs_pci, size_t size)
53 char sysfs_real[PATH_MAX] = { 0 };
54 MKSTR(sysfs_aux, "%s/%s", AUXILIARY_SYSFS_PATH, dev->name);
57 if (realpath(sysfs_aux, sysfs_real) == NULL) {
61 dir = dirname(sysfs_real);
66 if (rte_strscpy(sysfs_pci, dir, size) < 0)
72 mlx5_auxiliary_get_pci_str(const struct rte_auxiliary_device *dev,
73 char *addr, size_t size)
75 char sysfs_pci[PATH_MAX];
78 if (mlx5_auxiliary_get_pci_path(dev, sysfs_pci, sizeof(sysfs_pci)) != 0)
80 base = basename(sysfs_pci);
83 if (rte_strscpy(addr, base, size) < 0)
89 mlx5_auxiliary_get_numa(const struct rte_auxiliary_device *dev)
92 char numa_path[PATH_MAX];
94 if (mlx5_auxiliary_get_pci_path(dev, numa_path, sizeof(numa_path)) != 0)
96 if (strcat(numa_path, "/numa_node") == NULL) {
97 rte_errno = ENAMETOOLONG;
100 if (eal_parse_sysfs_value(numa_path, &numa) != 0) {
102 return SOCKET_ID_ANY;
108 mlx5_get_aux_ibv_device(const struct rte_auxiliary_device *dev)
111 char ib_name[64] = { 0 };
112 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
113 struct ibv_device *ibv_match = NULL;
119 if (mlx5_auxiliary_get_child_name(dev->name, "/infiniband",
120 ib_name, sizeof(ib_name)) != 0)
123 if (strcmp(ibv_list[n]->name, ib_name) != 0)
125 ibv_match = ibv_list[n];
128 if (ibv_match == NULL)
131 mlx5_glue->free_device_list(ibv_list);
136 mlx5_common_auxiliary_match(const char *name)
138 return strncmp(name, MLX5_AUXILIARY_PREFIX,
139 strlen(MLX5_AUXILIARY_PREFIX)) == 0;
143 mlx5_common_auxiliary_probe(struct rte_auxiliary_driver *drv __rte_unused,
144 struct rte_auxiliary_device *dev)
146 dev->device.numa_node = mlx5_auxiliary_get_numa(dev);
147 return mlx5_common_dev_probe(&dev->device);
151 mlx5_common_auxiliary_remove(struct rte_auxiliary_device *auxiliary_dev)
153 return mlx5_common_dev_remove(&auxiliary_dev->device);
157 mlx5_common_auxiliary_dma_map(struct rte_auxiliary_device *auxiliary_dev,
158 void *addr, uint64_t iova, size_t len)
160 return mlx5_common_dev_dma_map(&auxiliary_dev->device, addr, iova, len);
164 mlx5_common_auxiliary_dma_unmap(struct rte_auxiliary_device *auxiliary_dev,
165 void *addr, uint64_t iova, size_t len)
167 return mlx5_common_dev_dma_unmap(&auxiliary_dev->device, addr, iova,
171 static struct rte_auxiliary_driver mlx5_auxiliary_driver = {
173 .name = MLX5_AUXILIARY_DRIVER_NAME,
175 .match = mlx5_common_auxiliary_match,
176 .probe = mlx5_common_auxiliary_probe,
177 .remove = mlx5_common_auxiliary_remove,
178 .dma_map = mlx5_common_auxiliary_dma_map,
179 .dma_unmap = mlx5_common_auxiliary_dma_unmap,
182 void mlx5_common_auxiliary_init(void)
184 if (mlx5_auxiliary_driver.bus == NULL)
185 rte_auxiliary_register(&mlx5_auxiliary_driver);
188 RTE_FINI(mlx5_common_auxiliary_driver_finish)
190 if (mlx5_auxiliary_driver.bus != NULL)
191 rte_auxiliary_unregister(&mlx5_auxiliary_driver);