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_errno.h>
49 #include "rte_bus_vdev.h"
50 #include "vdev_logs.h"
54 /* Forward declare to access virtual bus name */
55 static struct rte_bus rte_vdev_bus;
57 /** Double linked list of virtual device drivers. */
58 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
60 static struct vdev_device_list vdev_device_list =
61 TAILQ_HEAD_INITIALIZER(vdev_device_list);
62 struct vdev_driver_list vdev_driver_list =
63 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
65 /* register a driver */
67 rte_vdev_register(struct rte_vdev_driver *driver)
69 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
72 /* unregister a driver */
74 rte_vdev_unregister(struct rte_vdev_driver *driver)
76 TAILQ_REMOVE(&vdev_driver_list, driver, next);
80 vdev_parse(const char *name, void *addr)
82 struct rte_vdev_driver **out = addr;
83 struct rte_vdev_driver *driver = NULL;
85 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
86 if (strncmp(driver->driver.name, name,
87 strlen(driver->driver.name)) == 0)
89 if (driver->driver.alias &&
90 strncmp(driver->driver.alias, name,
91 strlen(driver->driver.alias)) == 0)
97 return driver == NULL;
101 vdev_probe_all_drivers(struct rte_vdev_device *dev)
104 struct rte_vdev_driver *driver;
107 name = rte_vdev_device_name(dev);
109 VDEV_LOG(DEBUG, "Search driver %s to probe device %s\n", name,
110 rte_vdev_device_name(dev));
112 if (vdev_parse(name, &driver))
114 dev->device.driver = &driver->driver;
115 ret = driver->probe(dev);
117 dev->device.driver = NULL;
121 static struct rte_vdev_device *
122 find_vdev(const char *name)
124 struct rte_vdev_device *dev;
129 TAILQ_FOREACH(dev, &vdev_device_list, next) {
130 const char *devname = rte_vdev_device_name(dev);
132 if (!strncmp(devname, name, strlen(name)))
139 static struct rte_devargs *
140 alloc_devargs(const char *name, const char *args)
142 struct rte_devargs *devargs;
145 devargs = calloc(1, sizeof(*devargs));
149 devargs->bus = &rte_vdev_bus;
151 devargs->args = strdup(args);
153 devargs->args = strdup("");
155 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
156 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
166 rte_vdev_init(const char *name, const char *args)
168 struct rte_vdev_device *dev;
169 struct rte_devargs *devargs;
175 dev = find_vdev(name);
179 devargs = alloc_devargs(name, args);
183 dev = calloc(1, sizeof(*dev));
189 dev->device.devargs = devargs;
190 dev->device.numa_node = SOCKET_ID_ANY;
191 dev->device.name = devargs->name;
193 ret = vdev_probe_all_drivers(dev);
196 VDEV_LOG(ERR, "no driver found for %s\n", name);
200 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
202 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
213 vdev_remove_driver(struct rte_vdev_device *dev)
215 const char *name = rte_vdev_device_name(dev);
216 const struct rte_vdev_driver *driver;
218 if (!dev->device.driver) {
219 VDEV_LOG(DEBUG, "no driver attach to device %s\n", name);
223 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
225 return driver->remove(dev);
229 rte_vdev_uninit(const char *name)
231 struct rte_vdev_device *dev;
232 struct rte_devargs *devargs;
238 dev = find_vdev(name);
242 devargs = dev->device.devargs;
244 ret = vdev_remove_driver(dev);
248 TAILQ_REMOVE(&vdev_device_list, dev, next);
250 TAILQ_REMOVE(&devargs_list, devargs, next);
261 struct rte_vdev_device *dev;
262 struct rte_devargs *devargs;
264 /* for virtual devices we scan the devargs_list populated via cmdline */
265 TAILQ_FOREACH(devargs, &devargs_list, next) {
267 if (devargs->bus != &rte_vdev_bus)
270 dev = find_vdev(devargs->name);
274 dev = calloc(1, sizeof(*dev));
278 dev->device.devargs = devargs;
279 dev->device.numa_node = SOCKET_ID_ANY;
280 dev->device.name = devargs->name;
282 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
291 struct rte_vdev_device *dev;
293 /* call the init function for each virtual device */
294 TAILQ_FOREACH(dev, &vdev_device_list, next) {
296 if (dev->device.driver)
299 if (vdev_probe_all_drivers(dev)) {
300 VDEV_LOG(ERR, "failed to initialize %s device\n",
301 rte_vdev_device_name(dev));
309 static struct rte_device *
310 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
313 struct rte_vdev_device *dev;
315 TAILQ_FOREACH(dev, &vdev_device_list, next) {
316 if (start && &dev->device == start) {
320 if (cmp(&dev->device, data) == 0)
327 vdev_plug(struct rte_device *dev)
329 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
333 vdev_unplug(struct rte_device *dev)
335 return rte_vdev_uninit(dev->name);
338 static struct rte_bus rte_vdev_bus = {
341 .find_device = vdev_find_device,
343 .unplug = vdev_unplug,
347 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
349 RTE_INIT(vdev_init_log)
351 vdev_logtype_bus = rte_log_register("bus.vdev");
352 if (vdev_logtype_bus >= 0)
353 rte_log_set_level(vdev_logtype_bus, RTE_LOG_NOTICE);