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 /* Forward declare to access virtual bus name */
51 static struct rte_bus rte_vdev_bus;
53 /** Double linked list of virtual device drivers. */
54 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
56 static struct vdev_device_list vdev_device_list =
57 TAILQ_HEAD_INITIALIZER(vdev_device_list);
58 struct vdev_driver_list vdev_driver_list =
59 TAILQ_HEAD_INITIALIZER(vdev_driver_list);
61 /* register a driver */
63 rte_vdev_register(struct rte_vdev_driver *driver)
65 TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
68 /* unregister a driver */
70 rte_vdev_unregister(struct rte_vdev_driver *driver)
72 TAILQ_REMOVE(&vdev_driver_list, driver, next);
76 vdev_parse(const char *name, void *addr)
78 struct rte_vdev_driver **out = addr;
79 struct rte_vdev_driver *driver = NULL;
81 TAILQ_FOREACH(driver, &vdev_driver_list, next) {
82 if (strncmp(driver->driver.name, name,
83 strlen(driver->driver.name)) == 0)
85 if (driver->driver.alias &&
86 strncmp(driver->driver.alias, name,
87 strlen(driver->driver.alias)) == 0)
93 return driver == NULL;
97 vdev_probe_all_drivers(struct rte_vdev_device *dev)
100 struct rte_vdev_driver *driver;
103 name = rte_vdev_device_name(dev);
105 RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
106 rte_vdev_device_name(dev));
108 if (vdev_parse(name, &driver))
110 dev->device.driver = &driver->driver;
111 ret = driver->probe(dev);
113 dev->device.driver = NULL;
117 static struct rte_vdev_device *
118 find_vdev(const char *name)
120 struct rte_vdev_device *dev;
125 TAILQ_FOREACH(dev, &vdev_device_list, next) {
126 const char *devname = rte_vdev_device_name(dev);
127 if (!strncmp(devname, name, strlen(name)))
134 static struct rte_devargs *
135 alloc_devargs(const char *name, const char *args)
137 struct rte_devargs *devargs;
140 devargs = calloc(1, sizeof(*devargs));
144 devargs->bus = &rte_vdev_bus;
146 devargs->args = strdup(args);
148 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
149 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
159 rte_vdev_init(const char *name, const char *args)
161 struct rte_vdev_device *dev;
162 struct rte_devargs *devargs;
168 dev = find_vdev(name);
172 devargs = alloc_devargs(name, args);
176 dev = calloc(1, sizeof(*dev));
182 dev->device.devargs = devargs;
183 dev->device.numa_node = SOCKET_ID_ANY;
184 dev->device.name = devargs->name;
186 ret = vdev_probe_all_drivers(dev);
189 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
193 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
195 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
206 vdev_remove_driver(struct rte_vdev_device *dev)
208 const char *name = rte_vdev_device_name(dev);
209 const struct rte_vdev_driver *driver;
211 if (!dev->device.driver) {
212 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
216 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
218 return driver->remove(dev);
222 rte_vdev_uninit(const char *name)
224 struct rte_vdev_device *dev;
225 struct rte_devargs *devargs;
231 dev = find_vdev(name);
235 devargs = dev->device.devargs;
237 ret = vdev_remove_driver(dev);
241 TAILQ_REMOVE(&vdev_device_list, dev, next);
243 TAILQ_REMOVE(&devargs_list, devargs, next);
254 struct rte_vdev_device *dev;
255 struct rte_devargs *devargs;
257 /* for virtual devices we scan the devargs_list populated via cmdline */
258 TAILQ_FOREACH(devargs, &devargs_list, next) {
260 if (devargs->bus != &rte_vdev_bus)
263 dev = find_vdev(devargs->name);
267 dev = calloc(1, sizeof(*dev));
271 dev->device.devargs = devargs;
272 dev->device.numa_node = SOCKET_ID_ANY;
273 dev->device.name = devargs->name;
275 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
284 struct rte_vdev_device *dev;
286 /* call the init function for each virtual device */
287 TAILQ_FOREACH(dev, &vdev_device_list, next) {
289 if (dev->device.driver)
292 if (vdev_probe_all_drivers(dev)) {
293 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
294 rte_vdev_device_name(dev));
302 static struct rte_device *
303 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
306 struct rte_vdev_device *dev;
308 TAILQ_FOREACH(dev, &vdev_device_list, next) {
309 if (start && &dev->device == start) {
313 if (cmp(&dev->device, data) == 0)
320 vdev_unplug(struct rte_device *dev)
323 * The virtual bus doesn't support 'unattached' devices so this is
324 * actually equal to hotplugging removal of it.
326 return rte_vdev_uninit(dev->name);
329 static struct rte_bus rte_vdev_bus = {
332 .find_device = vdev_find_device,
333 /* .plug = NULL, see comment on vdev_unplug */
334 .unplug = vdev_unplug,
338 RTE_REGISTER_BUS(vdev, rte_vdev_bus);