devargs: make device representation generic
[dpdk.git] / lib / librte_eal / common / eal_common_vdev.c
index 9bb7427..7b857a6 100644 (file)
 #include <sys/queue.h>
 
 #include <rte_eal.h>
+#include <rte_dev.h>
 #include <rte_bus.h>
 #include <rte_vdev.h>
 #include <rte_common.h>
 #include <rte_devargs.h>
 #include <rte_memory.h>
+#include <rte_errno.h>
 
 /** Double linked list of virtual device drivers. */
 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
@@ -99,6 +101,27 @@ static char *parse_driver_arg(const char *args)
        return str;
 }
 
+static int
+vdev_parse(const char *name, void *addr)
+{
+       struct rte_vdev_driver **out = addr;
+       struct rte_vdev_driver *driver = NULL;
+
+       TAILQ_FOREACH(driver, &vdev_driver_list, next) {
+               if (strncmp(driver->driver.name, name,
+                           strlen(driver->driver.name)) == 0)
+                       break;
+               if (driver->driver.alias &&
+                   strncmp(driver->driver.alias, name,
+                           strlen(driver->driver.alias)) == 0)
+                       break;
+       }
+       if (driver != NULL &&
+           addr != NULL)
+               *out = driver;
+       return driver == NULL;
+}
+
 static int
 vdev_probe_all_drivers(struct rte_vdev_device *dev)
 {
@@ -113,36 +136,14 @@ vdev_probe_all_drivers(struct rte_vdev_device *dev)
        RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
                rte_vdev_device_name(dev));
 
-       TAILQ_FOREACH(driver, &vdev_driver_list, next) {
-               /*
-                * search a driver prefix in virtual device name.
-                * For example, if the driver is pcap PMD, driver->name
-                * will be "net_pcap", but "name" will be "net_pcapN".
-                * So use strncmp to compare.
-                */
-               if (!strncmp(driver->driver.name, name,
-                           strlen(driver->driver.name))) {
-                       dev->device.driver = &driver->driver;
-                       ret = driver->probe(dev);
-                       if (ret)
-                               dev->device.driver = NULL;
-                       goto out;
-               }
+       if (vdev_parse(name, &driver)) {
+               ret = -1;
+               goto out;
        }
-
-       /* Give new names precedence over aliases. */
-       TAILQ_FOREACH(driver, &vdev_driver_list, next) {
-               if (driver->driver.alias &&
-                   !strncmp(driver->driver.alias, name,
-                           strlen(driver->driver.alias))) {
-                       dev->device.driver = &driver->driver;
-                       ret = driver->probe(dev);
-                       if (ret)
-                               dev->device.driver = NULL;
-                       break;
-               }
-       }
-
+       dev->device.driver = &driver->driver;
+       ret = driver->probe(dev);
+       if (ret)
+               dev->device.driver = NULL;
 out:
        free(drv_name);
        return ret;
@@ -176,12 +177,12 @@ alloc_devargs(const char *name, const char *args)
                return NULL;
 
        devargs->type = RTE_DEVTYPE_VIRTUAL;
+       devargs->bus = rte_bus_find_by_name("vdev");
        if (args)
                devargs->args = strdup(args);
 
-       ret = snprintf(devargs->virt.drv_name,
-                              sizeof(devargs->virt.drv_name), "%s", name);
-       if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
+       ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
+       if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
                free(devargs->args);
                free(devargs);
                return NULL;
@@ -216,7 +217,7 @@ rte_vdev_init(const char *name, const char *args)
 
        dev->device.devargs = devargs;
        dev->device.numa_node = SOCKET_ID_ANY;
-       dev->device.name = devargs->virt.drv_name;
+       dev->device.name = devargs->name;
 
        ret = vdev_probe_all_drivers(dev);
        if (ret) {
@@ -288,15 +289,16 @@ vdev_scan(void)
 {
        struct rte_vdev_device *dev;
        struct rte_devargs *devargs;
+       struct rte_bus *vbus;
 
        /* for virtual devices we scan the devargs_list populated via cmdline */
-
+       vbus = rte_bus_find_by_name("vdev");
        TAILQ_FOREACH(devargs, &devargs_list, next) {
 
-               if (devargs->type != RTE_DEVTYPE_VIRTUAL)
+               if (devargs->bus != vbus)
                        continue;
 
-               dev = find_vdev(devargs->virt.drv_name);
+               dev = find_vdev(devargs->name);
                if (dev)
                        continue;
 
@@ -306,7 +308,7 @@ vdev_scan(void)
 
                dev->device.devargs = devargs;
                dev->device.numa_node = SOCKET_ID_ANY;
-               dev->device.name = devargs->virt.drv_name;
+               dev->device.name = devargs->name;
 
                TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
        }
@@ -340,12 +342,10 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
                 const void *data)
 {
        struct rte_vdev_device *dev;
-       bool start_found = !start;
 
        TAILQ_FOREACH(dev, &vdev_device_list, next) {
-               if (start_found == 0) {
-                       if (&dev->device == start)
-                               start_found = 1;
+               if (start && &dev->device == start) {
+                       start = NULL;
                        continue;
                }
                if (cmp(&dev->device, data) == 0)
@@ -354,10 +354,23 @@ vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
        return NULL;
 }
 
+static int
+vdev_unplug(struct rte_device *dev)
+{
+       /*
+        * The virtual bus doesn't support 'unattached' devices so this is
+        * actually equal to hotplugging removal of it.
+        */
+       return rte_vdev_uninit(dev->name);
+}
+
 static struct rte_bus rte_vdev_bus = {
        .scan = vdev_scan,
        .probe = vdev_probe,
        .find_device = vdev_find_device,
+       /* .plug = NULL, see comment on vdev_unplug */
+       .unplug = vdev_unplug,
+       .parse = vdev_parse,
 };
 
-RTE_REGISTER_BUS(VIRTUAL_BUS_NAME, rte_vdev_bus);
+RTE_REGISTER_BUS(vdev, rte_vdev_bus);