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