bus/vdev: use standard bus registration
[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 #include <rte_memory.h>
46
47 /** Double linked list of virtual device drivers. */
48 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
49
50 static struct vdev_device_list vdev_device_list =
51         TAILQ_HEAD_INITIALIZER(vdev_device_list);
52 struct vdev_driver_list vdev_driver_list =
53         TAILQ_HEAD_INITIALIZER(vdev_driver_list);
54
55 /* register a driver */
56 void
57 rte_vdev_register(struct rte_vdev_driver *driver)
58 {
59         TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
60 }
61
62 /* unregister a driver */
63 void
64 rte_vdev_unregister(struct rte_vdev_driver *driver)
65 {
66         TAILQ_REMOVE(&vdev_driver_list, driver, next);
67 }
68
69 /*
70  * Parse "driver" devargs without adding a dependency on rte_kvargs.h
71  */
72 static char *parse_driver_arg(const char *args)
73 {
74         const char *c;
75         char *str;
76
77         if (!args || args[0] == '\0')
78                 return NULL;
79
80         c = args;
81
82         do {
83                 if (strncmp(c, "driver=", 7) == 0) {
84                         c += 7;
85                         break;
86                 }
87
88                 c = strchr(c, ',');
89                 if (c)
90                         c++;
91         } while (c);
92
93         if (c)
94                 str = strdup(c);
95         else
96                 str = NULL;
97
98         return str;
99 }
100
101 static int
102 vdev_probe_all_drivers(struct rte_vdev_device *dev)
103 {
104         const char *name;
105         char *drv_name;
106         struct rte_vdev_driver *driver;
107         int ret = 1;
108
109         drv_name = parse_driver_arg(rte_vdev_device_args(dev));
110         name = drv_name ? drv_name : rte_vdev_device_name(dev);
111
112         RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
113                 rte_vdev_device_name(dev));
114
115         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
116                 /*
117                  * search a driver prefix in virtual device name.
118                  * For example, if the driver is pcap PMD, driver->name
119                  * will be "net_pcap", but "name" will be "net_pcapN".
120                  * So use strncmp to compare.
121                  */
122                 if (!strncmp(driver->driver.name, name,
123                             strlen(driver->driver.name))) {
124                         dev->device.driver = &driver->driver;
125                         ret = driver->probe(dev);
126                         if (ret)
127                                 dev->device.driver = NULL;
128                         goto out;
129                 }
130         }
131
132         /* Give new names precedence over aliases. */
133         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
134                 if (driver->driver.alias &&
135                     !strncmp(driver->driver.alias, name,
136                             strlen(driver->driver.alias))) {
137                         dev->device.driver = &driver->driver;
138                         ret = driver->probe(dev);
139                         if (ret)
140                                 dev->device.driver = NULL;
141                         break;
142                 }
143         }
144
145 out:
146         free(drv_name);
147         return ret;
148 }
149
150 static struct rte_vdev_device *
151 find_vdev(const char *name)
152 {
153         struct rte_vdev_device *dev;
154
155         if (!name)
156                 return NULL;
157
158         TAILQ_FOREACH(dev, &vdev_device_list, next) {
159                 const char *devname = rte_vdev_device_name(dev);
160                 if (!strncmp(devname, name, strlen(name)))
161                         return dev;
162         }
163
164         return NULL;
165 }
166
167 static struct rte_devargs *
168 alloc_devargs(const char *name, const char *args)
169 {
170         struct rte_devargs *devargs;
171         int ret;
172
173         devargs = calloc(1, sizeof(*devargs));
174         if (!devargs)
175                 return NULL;
176
177         devargs->type = RTE_DEVTYPE_VIRTUAL;
178         if (args)
179                 devargs->args = strdup(args);
180
181         ret = snprintf(devargs->virt.drv_name,
182                                sizeof(devargs->virt.drv_name), "%s", name);
183         if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name)) {
184                 free(devargs->args);
185                 free(devargs);
186                 return NULL;
187         }
188
189         return devargs;
190 }
191
192 int
193 rte_vdev_init(const char *name, const char *args)
194 {
195         struct rte_vdev_device *dev;
196         struct rte_devargs *devargs;
197         int ret;
198
199         if (name == NULL)
200                 return -EINVAL;
201
202         dev = find_vdev(name);
203         if (dev)
204                 return -EEXIST;
205
206         devargs = alloc_devargs(name, args);
207         if (!devargs)
208                 return -ENOMEM;
209
210         dev = calloc(1, sizeof(*dev));
211         if (!dev) {
212                 ret = -ENOMEM;
213                 goto fail;
214         }
215
216         dev->device.devargs = devargs;
217         dev->device.numa_node = SOCKET_ID_ANY;
218         dev->device.name = devargs->virt.drv_name;
219
220         ret = vdev_probe_all_drivers(dev);
221         if (ret) {
222                 if (ret > 0)
223                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
224                 goto fail;
225         }
226
227         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
228
229         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
230         return 0;
231
232 fail:
233         free(devargs->args);
234         free(devargs);
235         free(dev);
236         return ret;
237 }
238
239 static int
240 vdev_remove_driver(struct rte_vdev_device *dev)
241 {
242         const char *name = rte_vdev_device_name(dev);
243         const struct rte_vdev_driver *driver;
244
245         if (!dev->device.driver) {
246                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
247                 return 1;
248         }
249
250         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
251                 driver);
252         return driver->remove(dev);
253 }
254
255 int
256 rte_vdev_uninit(const char *name)
257 {
258         struct rte_vdev_device *dev;
259         struct rte_devargs *devargs;
260         int ret;
261
262         if (name == NULL)
263                 return -EINVAL;
264
265         dev = find_vdev(name);
266         if (!dev)
267                 return -ENOENT;
268
269         devargs = dev->device.devargs;
270
271         ret = vdev_remove_driver(dev);
272         if (ret)
273                 return ret;
274
275         TAILQ_REMOVE(&vdev_device_list, dev, next);
276
277         TAILQ_REMOVE(&devargs_list, devargs, next);
278
279         free(devargs->args);
280         free(devargs);
281         free(dev);
282         return 0;
283 }
284
285 static int
286 vdev_scan(void)
287 {
288         struct rte_vdev_device *dev;
289         struct rte_devargs *devargs;
290
291         /* for virtual devices we scan the devargs_list populated via cmdline */
292
293         TAILQ_FOREACH(devargs, &devargs_list, next) {
294
295                 if (devargs->type != RTE_DEVTYPE_VIRTUAL)
296                         continue;
297
298                 dev = find_vdev(devargs->virt.drv_name);
299                 if (dev)
300                         continue;
301
302                 dev = calloc(1, sizeof(*dev));
303                 if (!dev)
304                         return -1;
305
306                 dev->device.devargs = devargs;
307                 dev->device.numa_node = SOCKET_ID_ANY;
308                 dev->device.name = devargs->virt.drv_name;
309
310                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
311         }
312
313         return 0;
314 }
315
316 static int
317 vdev_probe(void)
318 {
319         struct rte_vdev_device *dev;
320
321         /* call the init function for each virtual device */
322         TAILQ_FOREACH(dev, &vdev_device_list, next) {
323
324                 if (dev->device.driver)
325                         continue;
326
327                 if (vdev_probe_all_drivers(dev)) {
328                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
329                                 rte_vdev_device_name(dev));
330                         return -1;
331                 }
332         }
333
334         return 0;
335 }
336
337 static struct rte_bus rte_vdev_bus = {
338         .scan = vdev_scan,
339         .probe = vdev_probe,
340 };
341
342 RTE_REGISTER_BUS(VIRTUAL_BUS_NAME, rte_vdev_bus);