1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2018, Microsoft Corporation.
10 #include <sys/queue.h>
16 #include <rte_tailq.h>
17 #include <rte_devargs.h>
18 #include <rte_malloc.h>
19 #include <rte_errno.h>
20 #include <rte_memory.h>
21 #include <rte_bus_vmbus.h>
25 int vmbus_logtype_bus;
26 extern struct rte_vmbus_bus rte_vmbus_bus;
28 /* map a particular resource from a file */
30 vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
35 /* Map the memory resource of device */
36 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
37 MAP_SHARED | flags, fd, offset);
38 if (mapaddr == MAP_FAILED) {
40 "mmap(%d, %p, %zu, %ld) failed: %s",
41 fd, requested_addr, size, (long)offset,
47 /* unmap a particular resource */
49 vmbus_unmap_resource(void *requested_addr, size_t size)
51 if (requested_addr == NULL)
54 /* Unmap the VMBUS memory resource of device */
55 if (munmap(requested_addr, size)) {
56 VMBUS_LOG(ERR, "munmap(%p, 0x%lx) failed: %s",
57 requested_addr, (unsigned long)size,
60 VMBUS_LOG(DEBUG, " VMBUS memory unmapped at %p",
65 * Match the VMBUS driver and device using UUID table
68 * VMBUS driver from which ID table would be extracted
70 * VMBUS device to match against the driver
72 * true for successful match
73 * false for unsuccessful match
76 vmbus_match(const struct rte_vmbus_driver *dr,
77 const struct rte_vmbus_device *dev)
79 const rte_uuid_t *id_table;
81 for (id_table = dr->id_table; !rte_uuid_is_null(*id_table); ++id_table) {
82 if (rte_uuid_compare(*id_table, dev->class_id) == 0)
89 * If device ID match, call the devinit() function of the driver.
92 vmbus_probe_one_driver(struct rte_vmbus_driver *dr,
93 struct rte_vmbus_device *dev)
95 char guid[RTE_UUID_STRLEN];
98 if (!vmbus_match(dr, dev))
99 return 1; /* not supported */
101 rte_uuid_unparse(dev->device_id, guid, sizeof(guid));
102 VMBUS_LOG(INFO, "VMBUS device %s on NUMA socket %i",
103 guid, dev->device.numa_node);
105 /* TODO add blacklisted */
107 /* map resources for device */
108 ret = rte_vmbus_map_device(dev);
112 /* reference driver structure */
114 dev->device.driver = &dr->driver;
116 if (dev->device.numa_node < 0) {
117 VMBUS_LOG(WARNING, " Invalid NUMA socket, default to 0");
118 dev->device.numa_node = 0;
121 /* call the driver probe() function */
122 VMBUS_LOG(INFO, " probe driver: %s", dr->driver.name);
123 ret = dr->probe(dr, dev);
126 rte_vmbus_unmap_device(dev);
133 * IF device class GUID mathces, call the probe function of
134 * registere drivers for the vmbus device.
135 * Return -1 if initialization failed,
136 * and 1 if no driver found for this device.
139 vmbus_probe_all_drivers(struct rte_vmbus_device *dev)
141 struct rte_vmbus_driver *dr;
144 /* Check if a driver is already loaded */
145 if (dev->driver != NULL) {
146 VMBUS_LOG(DEBUG, "VMBUS driver already loaded");
150 FOREACH_DRIVER_ON_VMBUS(dr) {
151 rc = vmbus_probe_one_driver(dr, dev);
152 if (rc < 0) /* negative is an error */
155 if (rc > 0) /* positive driver doesn't support it */
164 * Scan the vmbus, and call the devinit() function for
165 * all registered drivers that have a matching entry in its id_table
166 * for discovered devices.
169 rte_vmbus_probe(void)
171 struct rte_vmbus_device *dev;
172 size_t probed = 0, failed = 0;
173 char ubuf[RTE_UUID_STRLEN];
175 FOREACH_DEVICE_ON_VMBUS(dev) {
178 rte_uuid_unparse(dev->device_id, ubuf, sizeof(ubuf));
180 /* TODO: add whitelist/blacklist */
182 if (vmbus_probe_all_drivers(dev) < 0) {
184 "Requested device %s cannot be used", ubuf);
190 return (probed && probed == failed) ? -1 : 0;
194 vmbus_parse(const char *name, void *addr)
199 ret = rte_uuid_parse(name, guid);
200 if (ret == 0 && addr)
201 memcpy(addr, &guid, sizeof(guid));
207 * scan for matching device args on command line
209 * -w 'vmbus:635a7ae3-091e-4410-ad59-667c4f8c04c3,latency=20'
212 vmbus_devargs_lookup(struct rte_vmbus_device *dev)
214 struct rte_devargs *devargs;
217 RTE_EAL_DEVARGS_FOREACH("vmbus", devargs) {
218 vmbus_parse(devargs->name, &addr);
220 if (rte_uuid_compare(dev->device_id, addr) == 0)
227 /* register vmbus driver */
229 rte_vmbus_register(struct rte_vmbus_driver *driver)
232 "Registered driver %s", driver->driver.name);
234 TAILQ_INSERT_TAIL(&rte_vmbus_bus.driver_list, driver, next);
235 driver->bus = &rte_vmbus_bus;
238 /* unregister vmbus driver */
240 rte_vmbus_unregister(struct rte_vmbus_driver *driver)
242 TAILQ_REMOVE(&rte_vmbus_bus.driver_list, driver, next);
246 /* Add a device to VMBUS bus */
248 vmbus_add_device(struct rte_vmbus_device *vmbus_dev)
250 TAILQ_INSERT_TAIL(&rte_vmbus_bus.device_list, vmbus_dev, next);
253 /* Insert a device into a predefined position in VMBUS bus */
255 vmbus_insert_device(struct rte_vmbus_device *exist_vmbus_dev,
256 struct rte_vmbus_device *new_vmbus_dev)
258 TAILQ_INSERT_BEFORE(exist_vmbus_dev, new_vmbus_dev, next);
261 /* Remove a device from VMBUS bus */
263 vmbus_remove_device(struct rte_vmbus_device *vmbus_dev)
265 TAILQ_REMOVE(&rte_vmbus_bus.device_list, vmbus_dev, next);
268 /* VMBUS doesn't support hotplug */
269 static struct rte_device *
270 vmbus_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
273 struct rte_vmbus_device *dev;
275 FOREACH_DEVICE_ON_VMBUS(dev) {
276 if (start && &dev->device == start) {
280 if (cmp(&dev->device, data) == 0)
288 struct rte_vmbus_bus rte_vmbus_bus = {
290 .scan = rte_vmbus_scan,
291 .probe = rte_vmbus_probe,
292 .find_device = vmbus_find_device,
293 .parse = vmbus_parse,
295 .device_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.device_list),
296 .driver_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.driver_list),
299 RTE_REGISTER_BUS(vmbus, rte_vmbus_bus.bus);
301 RTE_INIT(vmbus_init_log)
303 vmbus_logtype_bus = rte_log_register("bus.vmbus");
304 if (vmbus_logtype_bus >= 0)
305 rte_log_set_level(vmbus_logtype_bus, RTE_LOG_NOTICE);