vdev: move virtual device probing into a bus
[dpdk.git] / lib / librte_eal / common / eal_common_vdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 RehiveTech. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32
33 #include <string.h>
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <sys/queue.h>
39
40 #include <rte_eal.h>
41 #include <rte_bus.h>
42 #include <rte_vdev.h>
43 #include <rte_common.h>
44 #include <rte_devargs.h>
45
46 struct vdev_driver_list vdev_driver_list =
47         TAILQ_HEAD_INITIALIZER(vdev_driver_list);
48
49 static void rte_vdev_bus_register(void);
50
51 /* register a driver */
52 void
53 rte_eal_vdrv_register(struct rte_vdev_driver *driver)
54 {
55         rte_vdev_bus_register();
56
57         TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
58         rte_eal_driver_register(&driver->driver);
59 }
60
61 /* unregister a driver */
62 void
63 rte_eal_vdrv_unregister(struct rte_vdev_driver *driver)
64 {
65         rte_eal_driver_unregister(&driver->driver);
66         TAILQ_REMOVE(&vdev_driver_list, driver, next);
67 }
68
69 int
70 rte_eal_vdev_init(const char *name, const char *args)
71 {
72         struct rte_vdev_driver *driver;
73
74         if (name == NULL)
75                 return -EINVAL;
76
77         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
78                 /*
79                  * search a driver prefix in virtual device name.
80                  * For example, if the driver is pcap PMD, driver->name
81                  * will be "net_pcap", but "name" will be "net_pcapN".
82                  * So use strncmp to compare.
83                  */
84                 if (!strncmp(driver->driver.name, name,
85                             strlen(driver->driver.name)))
86                         return driver->probe(name, args);
87         }
88
89         /* Give new names precedence over aliases. */
90         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
91                 if (driver->driver.alias &&
92                     !strncmp(driver->driver.alias, name,
93                             strlen(driver->driver.alias)))
94                         return driver->probe(name, args);
95         }
96
97         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
98         return -EINVAL;
99 }
100
101 int
102 rte_eal_vdev_uninit(const char *name)
103 {
104         struct rte_vdev_driver *driver;
105
106         if (name == NULL)
107                 return -EINVAL;
108
109         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
110                 /*
111                  * search a driver prefix in virtual device name.
112                  * For example, if the driver is pcap PMD, driver->name
113                  * will be "net_pcap", but "name" will be "net_pcapN".
114                  * So use strncmp to compare.
115                  */
116                 if (!strncmp(driver->driver.name, name,
117                              strlen(driver->driver.name)))
118                         return driver->remove(name);
119         }
120
121         /* Give new names precedence over aliases. */
122         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
123                 if (driver->driver.alias &&
124                     !strncmp(driver->driver.alias, name,
125                             strlen(driver->driver.alias)))
126                         return driver->remove(name);
127         }
128
129         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
130         return -EINVAL;
131 }
132
133 static int
134 vdev_scan(void)
135 {
136         /* for virtual devices we don't need to scan anything */
137         return 0;
138 }
139
140 static int
141 vdev_probe(void)
142 {
143         struct rte_devargs *devargs;
144
145         /*
146          * Note that the dev_driver_list is populated here
147          * from calls made to rte_eal_driver_register from constructor functions
148          * embedded into PMD modules via the RTE_PMD_REGISTER_VDEV macro
149          */
150
151         /* call the init function for each virtual device */
152         TAILQ_FOREACH(devargs, &devargs_list, next) {
153
154                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
155                         continue;
156
157                 if (rte_eal_vdev_init(devargs->virt.drv_name,
158                                       devargs->args)) {
159                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
160                                 devargs->virt.drv_name);
161                         return -1;
162                 }
163         }
164
165         return 0;
166 }
167
168 static struct rte_bus rte_vdev_bus = {
169         .scan = vdev_scan,
170         .probe = vdev_probe,
171 };
172
173 RTE_INIT(rte_vdev_bus_register);
174
175 static void rte_vdev_bus_register(void)
176 {
177         static int registered;
178
179         if (registered)
180                 return;
181
182         registered = 1;
183         rte_vdev_bus.name = RTE_STR(virtual);
184         rte_bus_register(&rte_vdev_bus);
185 }