4 * Copyright(c) 2016 RehiveTech. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of RehiveTech nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/queue.h>
44 #include <rte_common.h>
45 #include <rte_devargs.h>
46 #include <rte_memory.h>
47 #include <rte_tailq.h>
48 #include <rte_spinlock.h>
49 #include <rte_errno.h>
51 #include "rte_bus_vdev.h"
52 #include "vdev_logs.h"
56 /* Forward declare to access virtual bus name */
57 static struct rte_bus rte_vdev_bus;
59 /** Double linked list of virtual device drivers. */
60 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
62 static struct vdev_device_list vdev_device_list =
63 TAILQ_HEAD_INITIALIZER(vdev_device_list);
64 struct vdev_driver_list vdev_driver_list =
65 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
67 struct vdev_custom_scan {
68 TAILQ_ENTRY(vdev_custom_scan) next;
69 rte_vdev_scan_callback callback;
72 TAILQ_HEAD(vdev_custom_scans, vdev_custom_scan);
73 static struct vdev_custom_scans vdev_custom_scans =
74 TAILQ_HEAD_INITIALIZER(vdev_custom_scans);
75 static rte_spinlock_t vdev_custom_scan_lock = RTE_SPINLOCK_INITIALIZER;
77 /* register a driver */
79 rte_vdev_register(struct rte_vdev_driver *driver)
81 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
84 /* unregister a driver */
86 rte_vdev_unregister(struct rte_vdev_driver *driver)
88 TAILQ_REMOVE(&vdev_driver_list, driver, next);
92 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
94 struct vdev_custom_scan *custom_scan;
96 rte_spinlock_lock(&vdev_custom_scan_lock);
98 /* check if already registered */
99 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
100 if (custom_scan->callback == callback &&
101 custom_scan->user_arg == user_arg)
105 if (custom_scan == NULL) {
106 custom_scan = malloc(sizeof(struct vdev_custom_scan));
107 if (custom_scan != NULL) {
108 custom_scan->callback = callback;
109 custom_scan->user_arg = user_arg;
110 TAILQ_INSERT_TAIL(&vdev_custom_scans, custom_scan, next);
114 rte_spinlock_unlock(&vdev_custom_scan_lock);
116 return (custom_scan == NULL) ? -1 : 0;
120 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
122 struct vdev_custom_scan *custom_scan, *tmp_scan;
124 rte_spinlock_lock(&vdev_custom_scan_lock);
125 TAILQ_FOREACH_SAFE(custom_scan, &vdev_custom_scans, next, tmp_scan) {
126 if (custom_scan->callback != callback ||
127 (custom_scan->user_arg != (void *)-1 &&
128 custom_scan->user_arg != user_arg))
130 TAILQ_REMOVE(&vdev_custom_scans, custom_scan, next);
133 rte_spinlock_unlock(&vdev_custom_scan_lock);
139 vdev_parse(const char *name, void *addr)
141 struct rte_vdev_driver **out = addr;
142 struct rte_vdev_driver *driver = NULL;
144 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
145 if (strncmp(driver->driver.name, name,
146 strlen(driver->driver.name)) == 0)
148 if (driver->driver.alias &&
149 strncmp(driver->driver.alias, name,
150 strlen(driver->driver.alias)) == 0)
153 if (driver != NULL &&
156 return driver == NULL;
160 vdev_probe_all_drivers(struct rte_vdev_device *dev)
163 struct rte_vdev_driver *driver;
166 name = rte_vdev_device_name(dev);
168 VDEV_LOG(DEBUG, "Search driver %s to probe device %s\n", name,
169 rte_vdev_device_name(dev));
171 if (vdev_parse(name, &driver))
173 dev->device.driver = &driver->driver;
174 ret = driver->probe(dev);
176 dev->device.driver = NULL;
180 static struct rte_vdev_device *
181 find_vdev(const char *name)
183 struct rte_vdev_device *dev;
188 TAILQ_FOREACH(dev, &vdev_device_list, next) {
189 const char *devname = rte_vdev_device_name(dev);
191 if (!strncmp(devname, name, strlen(name)))
198 static struct rte_devargs *
199 alloc_devargs(const char *name, const char *args)
201 struct rte_devargs *devargs;
204 devargs = calloc(1, sizeof(*devargs));
208 devargs->bus = &rte_vdev_bus;
210 devargs->args = strdup(args);
212 devargs->args = strdup("");
214 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
215 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
225 rte_vdev_init(const char *name, const char *args)
227 struct rte_vdev_device *dev;
228 struct rte_devargs *devargs;
234 dev = find_vdev(name);
238 devargs = alloc_devargs(name, args);
242 dev = calloc(1, sizeof(*dev));
248 dev->device.devargs = devargs;
249 dev->device.numa_node = SOCKET_ID_ANY;
250 dev->device.name = devargs->name;
252 ret = vdev_probe_all_drivers(dev);
255 VDEV_LOG(ERR, "no driver found for %s\n", name);
259 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
261 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
272 vdev_remove_driver(struct rte_vdev_device *dev)
274 const char *name = rte_vdev_device_name(dev);
275 const struct rte_vdev_driver *driver;
277 if (!dev->device.driver) {
278 VDEV_LOG(DEBUG, "no driver attach to device %s\n", name);
282 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
284 return driver->remove(dev);
288 rte_vdev_uninit(const char *name)
290 struct rte_vdev_device *dev;
291 struct rte_devargs *devargs;
297 dev = find_vdev(name);
301 devargs = dev->device.devargs;
303 ret = vdev_remove_driver(dev);
307 TAILQ_REMOVE(&vdev_device_list, dev, next);
309 TAILQ_REMOVE(&devargs_list, devargs, next);
320 struct rte_vdev_device *dev;
321 struct rte_devargs *devargs;
322 struct vdev_custom_scan *custom_scan;
324 /* call custom scan callbacks if any */
325 rte_spinlock_lock(&vdev_custom_scan_lock);
326 TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
327 if (custom_scan->callback != NULL)
329 * the callback should update devargs list
330 * by calling rte_eal_devargs_insert() with
331 * devargs.bus = rte_bus_find_by_name("vdev");
332 * devargs.type = RTE_DEVTYPE_VIRTUAL;
333 * devargs.policy = RTE_DEV_WHITELISTED;
335 custom_scan->callback(custom_scan->user_arg);
337 rte_spinlock_unlock(&vdev_custom_scan_lock);
339 /* for virtual devices we scan the devargs_list populated via cmdline */
340 TAILQ_FOREACH(devargs, &devargs_list, next) {
342 if (devargs->bus != &rte_vdev_bus)
345 dev = find_vdev(devargs->name);
349 dev = calloc(1, sizeof(*dev));
353 dev->device.devargs = devargs;
354 dev->device.numa_node = SOCKET_ID_ANY;
355 dev->device.name = devargs->name;
357 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
366 struct rte_vdev_device *dev;
368 /* call the init function for each virtual device */
369 TAILQ_FOREACH(dev, &vdev_device_list, next) {
371 if (dev->device.driver)
374 if (vdev_probe_all_drivers(dev)) {
375 VDEV_LOG(ERR, "failed to initialize %s device\n",
376 rte_vdev_device_name(dev));
384 static struct rte_device *
385 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
388 struct rte_vdev_device *dev;
390 TAILQ_FOREACH(dev, &vdev_device_list, next) {
391 if (start && &dev->device == start) {
395 if (cmp(&dev->device, data) == 0)
402 vdev_plug(struct rte_device *dev)
404 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
408 vdev_unplug(struct rte_device *dev)
410 return rte_vdev_uninit(dev->name);
413 static struct rte_bus rte_vdev_bus = {
416 .find_device = vdev_find_device,
418 .unplug = vdev_unplug,
422 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
424 RTE_INIT(vdev_init_log)
426 vdev_logtype_bus = rte_log_register("bus.vdev");
427 if (vdev_logtype_bus >= 0)
428 rte_log_set_level(vdev_logtype_bus, RTE_LOG_NOTICE);