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"
51 /* Forward declare to access virtual bus name */
52 static struct rte_bus rte_vdev_bus;
54 /** Double linked list of virtual device drivers. */
55 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
57 static struct vdev_device_list vdev_device_list =
58 TAILQ_HEAD_INITIALIZER(vdev_device_list);
59 struct vdev_driver_list vdev_driver_list =
60 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
62 /* register a driver */
64 rte_vdev_register(struct rte_vdev_driver *driver)
66 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
69 /* unregister a driver */
71 rte_vdev_unregister(struct rte_vdev_driver *driver)
73 TAILQ_REMOVE(&vdev_driver_list, driver, next);
77 vdev_parse(const char *name, void *addr)
79 struct rte_vdev_driver **out = addr;
80 struct rte_vdev_driver *driver = NULL;
82 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
83 if (strncmp(driver->driver.name, name,
84 strlen(driver->driver.name)) == 0)
86 if (driver->driver.alias &&
87 strncmp(driver->driver.alias, name,
88 strlen(driver->driver.alias)) == 0)
94 return driver == NULL;
98 vdev_probe_all_drivers(struct rte_vdev_device *dev)
101 struct rte_vdev_driver *driver;
104 name = rte_vdev_device_name(dev);
106 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
107 rte_vdev_device_name(dev));
109 if (vdev_parse(name, &driver))
111 dev->device.driver = &driver->driver;
112 ret = driver->probe(dev);
114 dev->device.driver = NULL;
118 static struct rte_vdev_device *
119 find_vdev(const char *name)
121 struct rte_vdev_device *dev;
126 TAILQ_FOREACH(dev, &vdev_device_list, next) {
127 const char *devname = rte_vdev_device_name(dev);
129 if (!strncmp(devname, name, strlen(name)))
136 static struct rte_devargs *
137 alloc_devargs(const char *name, const char *args)
139 struct rte_devargs *devargs;
142 devargs = calloc(1, sizeof(*devargs));
146 devargs->bus = &rte_vdev_bus;
148 devargs->args = strdup(args);
150 devargs->args = strdup("");
152 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
153 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
163 rte_vdev_init(const char *name, const char *args)
165 struct rte_vdev_device *dev;
166 struct rte_devargs *devargs;
172 dev = find_vdev(name);
176 devargs = alloc_devargs(name, args);
180 dev = calloc(1, sizeof(*dev));
186 dev->device.devargs = devargs;
187 dev->device.numa_node = SOCKET_ID_ANY;
188 dev->device.name = devargs->name;
190 ret = vdev_probe_all_drivers(dev);
193 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
197 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
199 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
210 vdev_remove_driver(struct rte_vdev_device *dev)
212 const char *name = rte_vdev_device_name(dev);
213 const struct rte_vdev_driver *driver;
215 if (!dev->device.driver) {
216 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
220 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
222 return driver->remove(dev);
226 rte_vdev_uninit(const char *name)
228 struct rte_vdev_device *dev;
229 struct rte_devargs *devargs;
235 dev = find_vdev(name);
239 devargs = dev->device.devargs;
241 ret = vdev_remove_driver(dev);
245 TAILQ_REMOVE(&vdev_device_list, dev, next);
247 TAILQ_REMOVE(&devargs_list, devargs, next);
258 struct rte_vdev_device *dev;
259 struct rte_devargs *devargs;
261 /* for virtual devices we scan the devargs_list populated via cmdline */
262 TAILQ_FOREACH(devargs, &devargs_list, next) {
264 if (devargs->bus != &rte_vdev_bus)
267 dev = find_vdev(devargs->name);
271 dev = calloc(1, sizeof(*dev));
275 dev->device.devargs = devargs;
276 dev->device.numa_node = SOCKET_ID_ANY;
277 dev->device.name = devargs->name;
279 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
288 struct rte_vdev_device *dev;
290 /* call the init function for each virtual device */
291 TAILQ_FOREACH(dev, &vdev_device_list, next) {
293 if (dev->device.driver)
296 if (vdev_probe_all_drivers(dev)) {
297 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
298 rte_vdev_device_name(dev));
306 static struct rte_device *
307 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
310 struct rte_vdev_device *dev;
312 TAILQ_FOREACH(dev, &vdev_device_list, next) {
313 if (start && &dev->device == start) {
317 if (cmp(&dev->device, data) == 0)
324 vdev_plug(struct rte_device *dev)
326 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
330 vdev_unplug(struct rte_device *dev)
332 return rte_vdev_uninit(dev->name);
335 static struct rte_bus rte_vdev_bus = {
338 .find_device = vdev_find_device,
340 .unplug = vdev_unplug,
344 RTE_REGISTER_BUS(vdev, rte_vdev_bus);