1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
15 #include <rte_tailq.h>
17 #include <rte_devargs.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_bus_vmbus.h>
22 #include "eal_filesystem.h"
25 /** Pathname of VMBUS devices directory. */
26 #define SYSFS_VMBUS_DEVICES "/sys/bus/vmbus/devices"
28 extern struct rte_vmbus_bus rte_vmbus_bus;
30 /* Read sysfs file to get UUID */
32 parse_sysfs_uuid(const char *filename, rte_uuid_t uu)
38 f = fopen(filename, "r");
40 VMBUS_LOG(ERR, "cannot open sysfs value %s: %s",
41 filename, strerror(errno));
45 if (fgets(buf, sizeof(buf), f) == NULL) {
46 VMBUS_LOG(ERR, "cannot read sysfs value %s",
53 cp = strchr(buf, '\n');
57 /* strip { } notation */
65 if (rte_uuid_parse(in, uu) < 0) {
66 VMBUS_LOG(ERR, "%s %s not a valid UUID",
75 get_sysfs_string(const char *filename, char *buf, size_t buflen)
80 f = fopen(filename, "r");
82 VMBUS_LOG(ERR, "cannot open sysfs value %s:%s",
83 filename, strerror(errno));
87 if (fgets(buf, buflen, f) == NULL) {
88 VMBUS_LOG(ERR, "cannot read sysfs value %s",
95 /* remove trailing newline */
96 cp = memchr(buf, '\n', buflen);
104 vmbus_get_uio_dev(const struct rte_vmbus_device *dev,
105 char *dstbuf, size_t buflen)
107 char dirname[PATH_MAX];
108 unsigned int uio_num;
112 /* Assume recent kernel where uio is in uio/uioX */
113 snprintf(dirname, sizeof(dirname),
114 SYSFS_VMBUS_DEVICES "/%s/uio", dev->device.name);
116 dir = opendir(dirname);
118 return -1; /* Not a UIO device */
120 /* take the first file starting with "uio" */
121 while ((e = readdir(dir)) != NULL) {
122 const int prefix_len = 3;
125 if (strncmp(e->d_name, "uio", prefix_len) != 0)
130 uio_num = strtoull(e->d_name + prefix_len, &endptr, 10);
131 if (errno == 0 && endptr != (e->d_name + prefix_len)) {
132 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
144 /* Check map names with kernel names */
145 static const char *map_names[VMBUS_MAX_RESOURCE] = {
146 [HV_TXRX_RING_MAP] = "txrx_rings",
147 [HV_INT_PAGE_MAP] = "int_page",
148 [HV_MON_PAGE_MAP] = "monitor_page",
149 [HV_RECV_BUF_MAP] = "recv:",
150 [HV_SEND_BUF_MAP] = "send:",
154 /* map the resources of a vmbus device in virtual memory */
156 rte_vmbus_map_device(struct rte_vmbus_device *dev)
158 char uioname[PATH_MAX], filename[PATH_MAX];
159 char dirname[PATH_MAX], mapname[64];
162 dev->uio_num = vmbus_get_uio_dev(dev, uioname, sizeof(uioname));
163 if (dev->uio_num < 0) {
164 VMBUS_LOG(DEBUG, "Not managed by UIO driver, skipped");
168 /* Extract resource value */
169 for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
170 struct rte_mem_resource *res = &dev->resource[i];
171 unsigned long len, gpad = 0;
174 snprintf(dirname, sizeof(dirname),
175 "%s/maps/map%d", uioname, i);
177 snprintf(filename, sizeof(filename),
180 if (get_sysfs_string(filename, mapname, sizeof(mapname)) < 0) {
181 VMBUS_LOG(ERR, "could not read %s", filename);
185 if (strncmp(map_names[i], mapname, strlen(map_names[i])) != 0) {
187 "unexpected resource %s (expected %s)",
188 mapname, map_names[i]);
192 snprintf(filename, sizeof(filename),
194 if (eal_parse_sysfs_value(filename, &len) < 0) {
196 "could not read %s", filename);
201 /* both send and receive buffers have gpad in name */
202 cp = memchr(mapname, ':', sizeof(mapname));
204 gpad = strtoul(cp+1, NULL, 0);
206 /* put the GPAD value in physical address */
207 res->phys_addr = gpad;
210 return vmbus_uio_map_resource(dev);
214 rte_vmbus_unmap_device(struct rte_vmbus_device *dev)
216 vmbus_uio_unmap_resource(dev);
219 /* Scan one vmbus sysfs entry, and fill the devices list from it. */
221 vmbus_scan_one(const char *name)
223 struct rte_vmbus_device *dev, *dev2;
224 char filename[PATH_MAX];
225 char dirname[PATH_MAX];
228 dev = calloc(1, sizeof(*dev));
232 dev->device.bus = &rte_vmbus_bus.bus;
233 dev->device.name = strdup(name);
234 if (!dev->device.name)
237 /* sysfs base directory
238 * /sys/bus/vmbus/devices/7a08391f-f5a0-4ac0-9802-d13fd964f8df
240 * /sys/bus/vmbus/devices/vmbus_1
242 snprintf(dirname, sizeof(dirname), "%s/%s",
243 SYSFS_VMBUS_DEVICES, name);
246 snprintf(filename, sizeof(filename), "%s/device_id", dirname);
247 if (parse_sysfs_uuid(filename, dev->device_id) < 0)
250 /* get device class */
251 snprintf(filename, sizeof(filename), "%s/class_id", dirname);
252 if (parse_sysfs_uuid(filename, dev->class_id) < 0)
256 snprintf(filename, sizeof(filename), "%s/id", dirname);
257 if (eal_parse_sysfs_value(filename, &tmp) < 0)
262 snprintf(filename, sizeof(filename), "%s/monitor_id", dirname);
263 if (eal_parse_sysfs_value(filename, &tmp) < 0)
265 dev->monitor_id = tmp;
267 /* get numa node (if present) */
268 snprintf(filename, sizeof(filename), "%s/numa_node",
271 if (access(filename, R_OK) == 0) {
272 if (eal_parse_sysfs_value(filename, &tmp) < 0)
274 dev->device.numa_node = tmp;
276 /* if no NUMA support, set default to 0 */
277 dev->device.numa_node = SOCKET_ID_ANY;
280 dev->device.devargs = vmbus_devargs_lookup(dev);
282 /* device is valid, add in list (sorted) */
283 VMBUS_LOG(DEBUG, "Adding vmbus device %s", name);
285 TAILQ_FOREACH(dev2, &rte_vmbus_bus.device_list, next) {
288 ret = rte_uuid_compare(dev->device_id, dev2->device_id);
293 vmbus_insert_device(dev2, dev);
294 } else { /* already registered */
296 "%s already registered", name);
302 vmbus_add_device(dev);
305 VMBUS_LOG(DEBUG, "failed");
312 * Scan the content of the vmbus, and the devices in the devices list
320 dir = opendir(SYSFS_VMBUS_DEVICES);
325 VMBUS_LOG(ERR, "opendir %s failed: %s",
326 SYSFS_VMBUS_DEVICES, strerror(errno));
330 while ((e = readdir(dir)) != NULL) {
331 if (e->d_name[0] == '.')
334 if (vmbus_scan_one(e->d_name) < 0)
345 void rte_vmbus_irq_mask(struct rte_vmbus_device *device)
347 vmbus_uio_irq_control(device, 1);
350 void rte_vmbus_irq_unmask(struct rte_vmbus_device *device)
352 vmbus_uio_irq_control(device, 0);
355 int rte_vmbus_irq_read(struct rte_vmbus_device *device)
357 return vmbus_uio_irq_read(device);