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 devargs->args = strdup("");
150 ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
151 if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
161 rte_vdev_init(const char *name, const char *args)
163 struct rte_vdev_device *dev;
164 struct rte_devargs *devargs;
170 dev = find_vdev(name);
174 devargs = alloc_devargs(name, args);
178 dev = calloc(1, sizeof(*dev));
184 dev->device.devargs = devargs;
185 dev->device.numa_node = SOCKET_ID_ANY;
186 dev->device.name = devargs->name;
188 ret = vdev_probe_all_drivers(dev);
191 RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
195 TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
197 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
208 vdev_remove_driver(struct rte_vdev_device *dev)
210 const char *name = rte_vdev_device_name(dev);
211 const struct rte_vdev_driver *driver;
213 if (!dev->device.driver) {
214 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
218 driver = container_of(dev->device.driver, const struct rte_vdev_driver,
220 return driver->remove(dev);
224 rte_vdev_uninit(const char *name)
226 struct rte_vdev_device *dev;
227 struct rte_devargs *devargs;
233 dev = find_vdev(name);
237 devargs = dev->device.devargs;
239 ret = vdev_remove_driver(dev);
243 TAILQ_REMOVE(&vdev_device_list, dev, next);
245 TAILQ_REMOVE(&devargs_list, devargs, next);
256 struct rte_vdev_device *dev;
257 struct rte_devargs *devargs;
259 /* for virtual devices we scan the devargs_list populated via cmdline */
260 TAILQ_FOREACH(devargs, &devargs_list, next) {
262 if (devargs->bus != &rte_vdev_bus)
265 dev = find_vdev(devargs->name);
269 dev = calloc(1, sizeof(*dev));
273 dev->device.devargs = devargs;
274 dev->device.numa_node = SOCKET_ID_ANY;
275 dev->device.name = devargs->name;
277 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
286 struct rte_vdev_device *dev;
288 /* call the init function for each virtual device */
289 TAILQ_FOREACH(dev, &vdev_device_list, next) {
291 if (dev->device.driver)
294 if (vdev_probe_all_drivers(dev)) {
295 RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
296 rte_vdev_device_name(dev));
304 static struct rte_device *
305 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
308 struct rte_vdev_device *dev;
310 TAILQ_FOREACH(dev, &vdev_device_list, next) {
311 if (start && &dev->device == start) {
315 if (cmp(&dev->device, data) == 0)
322 vdev_plug(struct rte_device *dev)
324 return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
328 vdev_unplug(struct rte_device *dev)
330 return rte_vdev_uninit(dev->name);
333 static struct rte_bus rte_vdev_bus = {
336 .find_device = vdev_find_device,
338 .unplug = vdev_unplug,
342 RTE_REGISTER_BUS(vdev, rte_vdev_bus);