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>
45 #include <rte_common.h>
46 #include <rte_devargs.h>
47 #include <rte_memory.h>
48 #include <rte_errno.h>
50 /** Double linked list of virtual device drivers. */
51 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
53 static struct vdev_device_list vdev_device_list =
54 TAILQ_HEAD_INITIALIZER(vdev_device_list);
55 struct vdev_driver_list vdev_driver_list =
56 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
58 /* register a driver */
60 rte_vdev_register(struct rte_vdev_driver *driver)
62 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
65 /* unregister a driver */
67 rte_vdev_unregister(struct rte_vdev_driver *driver)
69 TAILQ_REMOVE(&vdev_driver_list, driver, next);
73 vdev_parse(const char *name, void *addr)
75 struct rte_vdev_driver **out = addr;
76 struct rte_vdev_driver *driver = NULL;
78 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
79 if (strncmp(driver->driver.name, name,
80 strlen(driver->driver.name)) == 0)
82 if (driver->driver.alias &&
83 strncmp(driver->driver.alias, name,
84 strlen(driver->driver.alias)) == 0)
90 return driver == NULL;
94 vdev_probe_all_drivers(struct rte_vdev_device *dev)
97 struct rte_vdev_driver *driver;
100 name = rte_vdev_device_name(dev);
102 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
103 rte_vdev_device_name(dev));
105 if (vdev_parse(name, &driver))
107 dev->device.driver = &driver->driver;
108 ret = driver->probe(dev);
110 dev->device.driver = NULL;
114 static struct rte_vdev_device *
115 find_vdev(const char *name)
117 struct rte_vdev_device *dev;
122 TAILQ_FOREACH(dev, &vdev_device_list, next) {
123 const char *devname = rte_vdev_device_name(dev);
124 if (!strncmp(devname, name, strlen(name)))
131 static struct rte_devargs *
132 alloc_devargs(const char *name, const char *args)
134 struct rte_devargs *devargs;
137 devargs = calloc(1, sizeof(*devargs));
141 devargs->bus = rte_bus_find_by_name("vdev");
143 devargs->args = strdup(args);
145 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
146 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
156 rte_vdev_init(const char *name, const char *args)
158 struct rte_vdev_device *dev;
159 struct rte_devargs *devargs;
165 dev = find_vdev(name);
169 devargs = alloc_devargs(name, args);
173 dev = calloc(1, sizeof(*dev));
179 dev->device.devargs = devargs;
180 dev->device.numa_node = SOCKET_ID_ANY;
181 dev->device.name = devargs->name;
183 ret = vdev_probe_all_drivers(dev);
186 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
190 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
192 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
203 vdev_remove_driver(struct rte_vdev_device *dev)
205 const char *name = rte_vdev_device_name(dev);
206 const struct rte_vdev_driver *driver;
208 if (!dev->device.driver) {
209 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
213 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
215 return driver->remove(dev);
219 rte_vdev_uninit(const char *name)
221 struct rte_vdev_device *dev;
222 struct rte_devargs *devargs;
228 dev = find_vdev(name);
232 devargs = dev->device.devargs;
234 ret = vdev_remove_driver(dev);
238 TAILQ_REMOVE(&vdev_device_list, dev, next);
240 TAILQ_REMOVE(&devargs_list, devargs, next);
251 struct rte_vdev_device *dev;
252 struct rte_devargs *devargs;
253 struct rte_bus *vbus;
255 /* for virtual devices we scan the devargs_list populated via cmdline */
256 vbus = rte_bus_find_by_name("vdev");
257 TAILQ_FOREACH(devargs, &devargs_list, next) {
259 if (devargs->bus != vbus)
262 dev = find_vdev(devargs->name);
266 dev = calloc(1, sizeof(*dev));
270 dev->device.devargs = devargs;
271 dev->device.numa_node = SOCKET_ID_ANY;
272 dev->device.name = devargs->name;
274 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
283 struct rte_vdev_device *dev;
285 /* call the init function for each virtual device */
286 TAILQ_FOREACH(dev, &vdev_device_list, next) {
288 if (dev->device.driver)
291 if (vdev_probe_all_drivers(dev)) {
292 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
293 rte_vdev_device_name(dev));
301 static struct rte_device *
302 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
305 struct rte_vdev_device *dev;
307 TAILQ_FOREACH(dev, &vdev_device_list, next) {
308 if (start && &dev->device == start) {
312 if (cmp(&dev->device, data) == 0)
319 vdev_unplug(struct rte_device *dev)
322 * The virtual bus doesn't support 'unattached' devices so this is
323 * actually equal to hotplugging removal of it.
325 return rte_vdev_uninit(dev->name);
328 static struct rte_bus rte_vdev_bus = {
331 .find_device = vdev_find_device,
332 /* .plug = NULL, see comment on vdev_unplug */
333 .unplug = vdev_unplug,
337 RTE_REGISTER_BUS(vdev, rte_vdev_bus);