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"
29 * GUID associated with network devices
30 * {f8615163-df3e-46c5-913f-f2d2f965ed0e}
32 static const rte_uuid_t vmbus_nic_uuid
= {
33 0xf8, 0x61, 0x51, 0x63,
37 0xf2, 0xd2, 0xf9, 0x65, 0xed, 0xe
40 extern struct rte_vmbus_bus rte_vmbus_bus
;
42 /* Read sysfs file to get UUID */
44 parse_sysfs_uuid(const char *filename
, rte_uuid_t uu
)
50 f
= fopen(filename
, "r");
52 VMBUS_LOG(ERR
, "cannot open sysfs value %s: %s",
53 filename
, strerror(errno
));
57 if (fgets(buf
, sizeof(buf
), f
) == NULL
) {
58 VMBUS_LOG(ERR
, "cannot read sysfs value %s",
65 cp
= strchr(buf
, '\n');
69 /* strip { } notation */
77 if (rte_uuid_parse(in
, uu
) < 0) {
78 VMBUS_LOG(ERR
, "%s %s not a valid UUID",
87 get_sysfs_string(const char *filename
, char *buf
, size_t buflen
)
92 f
= fopen(filename
, "r");
94 VMBUS_LOG(ERR
, "cannot open sysfs value %s:%s",
95 filename
, strerror(errno
));
99 if (fgets(buf
, buflen
, f
) == NULL
) {
100 VMBUS_LOG(ERR
, "cannot read sysfs value %s",
107 /* remove trailing newline */
108 cp
= memchr(buf
, '\n', buflen
);
116 vmbus_get_uio_dev(const struct rte_vmbus_device
*dev
,
117 char *dstbuf
, size_t buflen
)
119 char dirname
[PATH_MAX
];
120 unsigned int uio_num
;
124 /* Assume recent kernel where uio is in uio/uioX */
125 snprintf(dirname
, sizeof(dirname
),
126 SYSFS_VMBUS_DEVICES
"/%s/uio", dev
->device
.name
);
128 dir
= opendir(dirname
);
130 return -1; /* Not a UIO device */
132 /* take the first file starting with "uio" */
133 while ((e
= readdir(dir
)) != NULL
) {
134 const int prefix_len
= 3;
137 if (strncmp(e
->d_name
, "uio", prefix_len
) != 0)
142 uio_num
= strtoull(e
->d_name
+ prefix_len
, &endptr
, 10);
143 if (errno
== 0 && endptr
!= (e
->d_name
+ prefix_len
)) {
144 snprintf(dstbuf
, buflen
, "%s/uio%u", dirname
, uio_num
);
156 /* Check map names with kernel names */
157 static const char *map_names
[VMBUS_MAX_RESOURCE
] = {
158 [HV_TXRX_RING_MAP
] = "txrx_rings",
159 [HV_INT_PAGE_MAP
] = "int_page",
160 [HV_MON_PAGE_MAP
] = "monitor_page",
161 [HV_RECV_BUF_MAP
] = "recv:",
162 [HV_SEND_BUF_MAP
] = "send:",
166 /* map the resources of a vmbus device in virtual memory */
168 rte_vmbus_map_device(struct rte_vmbus_device
*dev
)
170 char uioname
[PATH_MAX
], filename
[PATH_MAX
];
171 char dirname
[PATH_MAX
], mapname
[64];
174 dev
->uio_num
= vmbus_get_uio_dev(dev
, uioname
, sizeof(uioname
));
175 if (dev
->uio_num
< 0) {
176 VMBUS_LOG(DEBUG
, "Not managed by UIO driver, skipped");
180 /* Extract resource value */
181 for (i
= 0; i
< VMBUS_MAX_RESOURCE
; i
++) {
182 struct rte_mem_resource
*res
= &dev
->resource
[i
];
183 unsigned long len
, gpad
= 0;
186 snprintf(dirname
, sizeof(dirname
),
187 "%s/maps/map%d", uioname
, i
);
189 snprintf(filename
, sizeof(filename
),
192 if (get_sysfs_string(filename
, mapname
, sizeof(mapname
)) < 0) {
193 VMBUS_LOG(ERR
, "could not read %s", filename
);
197 if (strncmp(map_names
[i
], mapname
, strlen(map_names
[i
])) != 0) {
199 "unexpected resource %s (expected %s)",
200 mapname
, map_names
[i
]);
204 snprintf(filename
, sizeof(filename
),
206 if (eal_parse_sysfs_value(filename
, &len
) < 0) {
208 "could not read %s", filename
);
213 /* both send and receive buffers have gpad in name */
214 cp
= memchr(mapname
, ':', sizeof(mapname
));
216 gpad
= strtoul(cp
+1, NULL
, 0);
218 /* put the GPAD value in physical address */
219 res
->phys_addr
= gpad
;
222 return vmbus_uio_map_resource(dev
);
226 rte_vmbus_unmap_device(struct rte_vmbus_device
*dev
)
228 vmbus_uio_unmap_resource(dev
);
231 /* Scan one vmbus sysfs entry, and fill the devices list from it. */
233 vmbus_scan_one(const char *name
)
235 struct rte_vmbus_device
*dev
, *dev2
;
236 char filename
[PATH_MAX
];
237 char dirname
[PATH_MAX
];
240 dev
= calloc(1, sizeof(*dev
));
244 dev
->device
.bus
= &rte_vmbus_bus
.bus
;
245 dev
->device
.name
= strdup(name
);
246 if (!dev
->device
.name
)
249 /* sysfs base directory
250 * /sys/bus/vmbus/devices/7a08391f-f5a0-4ac0-9802-d13fd964f8df
252 * /sys/bus/vmbus/devices/vmbus_1
254 snprintf(dirname
, sizeof(dirname
), "%s/%s",
255 SYSFS_VMBUS_DEVICES
, name
);
257 /* get device class */
258 snprintf(filename
, sizeof(filename
), "%s/class_id", dirname
);
259 if (parse_sysfs_uuid(filename
, dev
->class_id
) < 0)
262 /* skip non-network devices */
263 if (rte_uuid_compare(dev
->class_id
, vmbus_nic_uuid
) != 0) {
269 snprintf(filename
, sizeof(filename
), "%s/device_id", dirname
);
270 if (parse_sysfs_uuid(filename
, dev
->device_id
) < 0)
274 snprintf(filename
, sizeof(filename
), "%s/id", dirname
);
275 if (eal_parse_sysfs_value(filename
, &tmp
) < 0)
280 snprintf(filename
, sizeof(filename
), "%s/monitor_id", dirname
);
281 if (eal_parse_sysfs_value(filename
, &tmp
) < 0)
283 dev
->monitor_id
= tmp
;
285 /* get numa node (if present) */
286 snprintf(filename
, sizeof(filename
), "%s/numa_node",
289 if (access(filename
, R_OK
) == 0) {
290 if (eal_parse_sysfs_value(filename
, &tmp
) < 0)
292 dev
->device
.numa_node
= tmp
;
294 /* if no NUMA support, set default to 0 */
295 dev
->device
.numa_node
= SOCKET_ID_ANY
;
298 dev
->device
.devargs
= vmbus_devargs_lookup(dev
);
300 /* device is valid, add in list (sorted) */
301 VMBUS_LOG(DEBUG
, "Adding vmbus device %s", name
);
303 TAILQ_FOREACH(dev2
, &rte_vmbus_bus
.device_list
, next
) {
306 ret
= rte_uuid_compare(dev
->device_id
, dev2
->device_id
);
311 vmbus_insert_device(dev2
, dev
);
312 } else { /* already registered */
314 "%s already registered", name
);
320 vmbus_add_device(dev
);
323 VMBUS_LOG(DEBUG
, "failed");
330 * Scan the content of the vmbus, and the devices in the devices list
338 dir
= opendir(SYSFS_VMBUS_DEVICES
);
343 VMBUS_LOG(ERR
, "opendir %s failed: %s",
344 SYSFS_VMBUS_DEVICES
, strerror(errno
));
348 while ((e
= readdir(dir
)) != NULL
) {
349 if (e
->d_name
[0] == '.')
352 if (vmbus_scan_one(e
->d_name
) < 0)
363 void rte_vmbus_irq_mask(struct rte_vmbus_device
*device
)
365 vmbus_uio_irq_control(device
, 1);
368 void rte_vmbus_irq_unmask(struct rte_vmbus_device
*device
)
370 vmbus_uio_irq_control(device
, 0);
373 int rte_vmbus_irq_read(struct rte_vmbus_device
*device
)
375 return vmbus_uio_irq_read(device
);