1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 Intel Corporation
15 #include <rte_string_fns.h>
16 #include "ioat_private.h"
18 /* default value for DSA paths, but allow override in environment for testing */
19 #define DSA_DEV_PATH "/dev/dsa"
20 #define DSA_SYSFS_PATH "/sys/bus/dsa/devices"
22 static unsigned int devcount;
24 /** unique identifier for a DSA device/WQ instance */
30 /** a DSA device instance */
31 struct rte_dsa_device {
32 struct rte_device device; /**< Inherit core device */
33 TAILQ_ENTRY(rte_dsa_device) next; /**< next dev in list */
35 char wq_name[32]; /**< the workqueue name/number e.g. wq0.1 */
36 struct dsa_wq_addr addr; /**< Identifies the specific WQ */
39 /* forward prototypes */
41 static int dsa_scan(void);
42 static int dsa_probe(void);
43 static struct rte_device *dsa_find_device(const struct rte_device *start,
44 rte_dev_cmp_t cmp, const void *data);
45 static enum rte_iova_mode dsa_get_iommu_class(void);
46 static int dsa_addr_parse(const char *name, void *addr);
48 /** List of devices */
49 TAILQ_HEAD(dsa_device_list, rte_dsa_device);
52 * Structure describing the DSA bus
55 struct rte_bus bus; /**< Inherit the generic class */
56 struct rte_driver driver; /**< Driver struct for devices to point to */
57 struct dsa_device_list device_list; /**< List of PCI devices */
60 struct dsa_bus dsa_bus = {
64 .find_device = dsa_find_device,
65 .get_iommu_class = dsa_get_iommu_class,
66 .parse = dsa_addr_parse,
71 .device_list = TAILQ_HEAD_INITIALIZER(dsa_bus.device_list),
74 static inline const char *
75 dsa_get_dev_path(void)
77 const char *path = getenv("DSA_DEV_PATH");
78 return path ? path : DSA_DEV_PATH;
81 static inline const char *
82 dsa_get_sysfs_path(void)
84 const char *path = getenv("DSA_SYSFS_PATH");
85 return path ? path : DSA_SYSFS_PATH;
88 static const struct rte_rawdev_ops idxd_vdev_ops = {
89 .dev_close = idxd_rawdev_close,
90 .dev_selftest = ioat_rawdev_test,
91 .dump = idxd_dev_dump,
92 .dev_configure = idxd_dev_configure,
93 .dev_info_get = idxd_dev_info_get,
94 .xstats_get = ioat_xstats_get,
95 .xstats_get_names = ioat_xstats_get_names,
96 .xstats_reset = ioat_xstats_reset,
100 idxd_vdev_mmap_wq(struct rte_dsa_device *dev)
106 snprintf(path, sizeof(path), "%s/%s", dsa_get_dev_path(), dev->wq_name);
107 fd = open(path, O_RDWR);
109 IOAT_PMD_ERR("Failed to open device path: %s", path);
113 addr = mmap(NULL, 0x1000, PROT_WRITE, MAP_SHARED, fd, 0);
115 if (addr == MAP_FAILED) {
116 IOAT_PMD_ERR("Failed to mmap device %s", path);
124 read_wq_string(struct rte_dsa_device *dev, const char *filename,
125 char *value, size_t valuelen)
127 char sysfs_node[PATH_MAX];
131 snprintf(sysfs_node, sizeof(sysfs_node), "%s/%s/%s",
132 dsa_get_sysfs_path(), dev->wq_name, filename);
133 fd = open(sysfs_node, O_RDONLY);
135 IOAT_PMD_ERR("%s(): opening file '%s' failed: %s",
136 __func__, sysfs_node, strerror(errno));
140 len = read(fd, value, valuelen - 1);
143 IOAT_PMD_ERR("%s(): error reading file '%s': %s",
144 __func__, sysfs_node, strerror(errno));
152 read_wq_int(struct rte_dsa_device *dev, const char *filename,
155 char sysfs_node[PATH_MAX];
159 snprintf(sysfs_node, sizeof(sysfs_node), "%s/%s/%s",
160 dsa_get_sysfs_path(), dev->wq_name, filename);
161 f = fopen(sysfs_node, "r");
163 IOAT_PMD_ERR("%s(): opening file '%s' failed: %s",
164 __func__, sysfs_node, strerror(errno));
168 if (fscanf(f, "%d", value) != 1) {
169 IOAT_PMD_ERR("%s(): error reading file '%s': %s",
170 __func__, sysfs_node, strerror(errno));
179 read_device_int(struct rte_dsa_device *dev, const char *filename,
182 char sysfs_node[PATH_MAX];
186 snprintf(sysfs_node, sizeof(sysfs_node), "%s/dsa%d/%s",
187 dsa_get_sysfs_path(), dev->addr.device_id, filename);
188 f = fopen(sysfs_node, "r");
190 IOAT_PMD_ERR("%s(): opening file '%s' failed: %s",
191 __func__, sysfs_node, strerror(errno));
195 if (fscanf(f, "%d", value) != 1) {
196 IOAT_PMD_ERR("%s(): error reading file '%s': %s",
197 __func__, sysfs_node, strerror(errno));
206 idxd_rawdev_probe_dsa(struct rte_dsa_device *dev)
208 struct idxd_rawdev idxd = {{0}}; /* double {} to avoid error on BSD12 */
211 IOAT_PMD_INFO("Probing device %s on numa node %d",
212 dev->wq_name, dev->device.numa_node);
213 if (read_wq_int(dev, "size", &ret) < 0)
215 idxd.max_batches = ret;
216 idxd.qid = dev->addr.wq_id;
217 idxd.u.vdev.dsa_id = dev->addr.device_id;
219 idxd.public.portal = idxd_vdev_mmap_wq(dev);
220 if (idxd.public.portal == NULL) {
221 IOAT_PMD_ERR("WQ mmap failed");
225 ret = idxd_rawdev_create(dev->wq_name, &dev->device, &idxd, &idxd_vdev_ops);
227 IOAT_PMD_ERR("Failed to create rawdev %s", dev->wq_name);
235 is_for_this_process_use(const char *name)
237 char *runtime_dir = strdup(rte_eal_get_runtime_dir());
238 char *prefix = basename(runtime_dir);
239 int prefixlen = strlen(prefix);
242 if (strncmp(name, "dpdk_", 5) == 0)
244 if (strncmp(name, prefix, prefixlen) == 0 && name[prefixlen] == '_')
254 struct rte_dsa_device *dev;
256 TAILQ_FOREACH(dev, &dsa_bus.device_list, next) {
257 char type[64], name[64];
259 if (read_wq_string(dev, "type", type, sizeof(type)) < 0 ||
260 read_wq_string(dev, "name", name, sizeof(name)) < 0)
263 if (strncmp(type, "user", 4) == 0 && is_for_this_process_use(name)) {
264 dev->device.driver = &dsa_bus.driver;
265 idxd_rawdev_probe_dsa(dev);
268 IOAT_PMD_DEBUG("WQ '%s', not allocated to DPDK", dev->wq_name);
277 const char *path = dsa_get_dev_path();
281 dev_dir = opendir(path);
282 if (dev_dir == NULL) {
284 return 0; /* no bus, return without error */
285 IOAT_PMD_ERR("%s(): opendir '%s' failed: %s",
286 __func__, path, strerror(errno));
290 while ((wq = readdir(dev_dir)) != NULL) {
291 struct rte_dsa_device *dev;
294 if (strncmp(wq->d_name, "wq", 2) != 0)
296 if (strnlen(wq->d_name, sizeof(dev->wq_name)) == sizeof(dev->wq_name)) {
297 IOAT_PMD_ERR("%s(): wq name too long: '%s', skipping",
298 __func__, wq->d_name);
301 IOAT_PMD_DEBUG("%s(): found %s/%s", __func__, path, wq->d_name);
303 dev = malloc(sizeof(*dev));
304 if (dsa_addr_parse(wq->d_name, &dev->addr) < 0) {
305 IOAT_PMD_ERR("Error parsing WQ name: %s", wq->d_name);
309 dev->device.bus = &dsa_bus.bus;
310 strlcpy(dev->wq_name, wq->d_name, sizeof(dev->wq_name));
311 TAILQ_INSERT_TAIL(&dsa_bus.device_list, dev, next);
314 read_device_int(dev, "numa_node", &numa_node);
315 dev->device.numa_node = numa_node;
316 dev->device.name = dev->wq_name;
323 static struct rte_device *
324 dsa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
327 struct rte_dsa_device *dev = TAILQ_FIRST(&dsa_bus.device_list);
329 /* the rte_device struct must be at start of dsa structure */
330 RTE_BUILD_BUG_ON(offsetof(struct rte_dsa_device, device) != 0);
332 if (start != NULL) /* jump to start point if given */
333 dev = TAILQ_NEXT((const struct rte_dsa_device *)start, next);
334 while (dev != NULL) {
335 if (cmp(&dev->device, data) == 0)
337 dev = TAILQ_NEXT(dev, next);
342 static enum rte_iova_mode
343 dsa_get_iommu_class(void)
345 /* if there are no devices, report don't care, otherwise VA mode */
346 return devcount > 0 ? RTE_IOVA_VA : RTE_IOVA_DC;
350 dsa_addr_parse(const char *name, void *addr)
352 struct dsa_wq_addr *wq = addr;
353 unsigned int device_id, wq_id;
355 if (sscanf(name, "wq%u.%u", &device_id, &wq_id) != 2) {
356 IOAT_PMD_DEBUG("Parsing WQ name failed: %s", name);
360 wq->device_id = device_id;
365 RTE_REGISTER_BUS(dsa, dsa_bus.bus);