devargs: make device representation 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->type = RTE_DEVTYPE_VIRTUAL;
180         devargs->bus = rte_bus_find_by_name("vdev");
181         if (args)
182                 devargs->args = strdup(args);
183
184         ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
185         if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
186                 free(devargs->args);
187                 free(devargs);
188                 return NULL;
189         }
190
191         return devargs;
192 }
193
194 int
195 rte_vdev_init(const char *name, const char *args)
196 {
197         struct rte_vdev_device *dev;
198         struct rte_devargs *devargs;
199         int ret;
200
201         if (name == NULL)
202                 return -EINVAL;
203
204         dev = find_vdev(name);
205         if (dev)
206                 return -EEXIST;
207
208         devargs = alloc_devargs(name, args);
209         if (!devargs)
210                 return -ENOMEM;
211
212         dev = calloc(1, sizeof(*dev));
213         if (!dev) {
214                 ret = -ENOMEM;
215                 goto fail;
216         }
217
218         dev->device.devargs = devargs;
219         dev->device.numa_node = SOCKET_ID_ANY;
220         dev->device.name = devargs->name;
221
222         ret = vdev_probe_all_drivers(dev);
223         if (ret) {
224                 if (ret > 0)
225                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
226                 goto fail;
227         }
228
229         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
230
231         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
232         return 0;
233
234 fail:
235         free(devargs->args);
236         free(devargs);
237         free(dev);
238         return ret;
239 }
240
241 static int
242 vdev_remove_driver(struct rte_vdev_device *dev)
243 {
244         const char *name = rte_vdev_device_name(dev);
245         const struct rte_vdev_driver *driver;
246
247         if (!dev->device.driver) {
248                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
249                 return 1;
250         }
251
252         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
253                 driver);
254         return driver->remove(dev);
255 }
256
257 int
258 rte_vdev_uninit(const char *name)
259 {
260         struct rte_vdev_device *dev;
261         struct rte_devargs *devargs;
262         int ret;
263
264         if (name == NULL)
265                 return -EINVAL;
266
267         dev = find_vdev(name);
268         if (!dev)
269                 return -ENOENT;
270
271         devargs = dev->device.devargs;
272
273         ret = vdev_remove_driver(dev);
274         if (ret)
275                 return ret;
276
277         TAILQ_REMOVE(&vdev_device_list, dev, next);
278
279         TAILQ_REMOVE(&devargs_list, devargs, next);
280
281         free(devargs->args);
282         free(devargs);
283         free(dev);
284         return 0;
285 }
286
287 static int
288 vdev_scan(void)
289 {
290         struct rte_vdev_device *dev;
291         struct rte_devargs *devargs;
292         struct rte_bus *vbus;
293
294         /* for virtual devices we scan the devargs_list populated via cmdline */
295         vbus = rte_bus_find_by_name("vdev");
296         TAILQ_FOREACH(devargs, &devargs_list, next) {
297
298                 if (devargs->bus != vbus)
299                         continue;
300
301                 dev = find_vdev(devargs->name);
302                 if (dev)
303                         continue;
304
305                 dev = calloc(1, sizeof(*dev));
306                 if (!dev)
307                         return -1;
308
309                 dev->device.devargs = devargs;
310                 dev->device.numa_node = SOCKET_ID_ANY;
311                 dev->device.name = devargs->name;
312
313                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
314         }
315
316         return 0;
317 }
318
319 static int
320 vdev_probe(void)
321 {
322         struct rte_vdev_device *dev;
323
324         /* call the init function for each virtual device */
325         TAILQ_FOREACH(dev, &vdev_device_list, next) {
326
327                 if (dev->device.driver)
328                         continue;
329
330                 if (vdev_probe_all_drivers(dev)) {
331                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
332                                 rte_vdev_device_name(dev));
333                         return -1;
334                 }
335         }
336
337         return 0;
338 }
339
340 static struct rte_device *
341 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
342                  const void *data)
343 {
344         struct rte_vdev_device *dev;
345
346         TAILQ_FOREACH(dev, &vdev_device_list, next) {
347                 if (start && &dev->device == start) {
348                         start = NULL;
349                         continue;
350                 }
351                 if (cmp(&dev->device, data) == 0)
352                         return &dev->device;
353         }
354         return NULL;
355 }
356
357 static int
358 vdev_unplug(struct rte_device *dev)
359 {
360         /*
361          * The virtual bus doesn't support 'unattached' devices so this is
362          * actually equal to hotplugging removal of it.
363          */
364         return rte_vdev_uninit(dev->name);
365 }
366
367 static struct rte_bus rte_vdev_bus = {
368         .scan = vdev_scan,
369         .probe = vdev_probe,
370         .find_device = vdev_find_device,
371         /* .plug = NULL, see comment on vdev_unplug */
372         .unplug = vdev_unplug,
373         .parse = vdev_parse,
374 };
375
376 RTE_REGISTER_BUS(vdev, rte_vdev_bus);