bus/vdev: remove probe with driver name option
[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 static int
73 vdev_parse(const char *name, void *addr)
74 {
75         struct rte_vdev_driver **out = addr;
76         struct rte_vdev_driver *driver = NULL;
77
78         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
79                 if (strncmp(driver->driver.name, name,
80                             strlen(driver->driver.name)) == 0)
81                         break;
82                 if (driver->driver.alias &&
83                     strncmp(driver->driver.alias, name,
84                             strlen(driver->driver.alias)) == 0)
85                         break;
86         }
87         if (driver != NULL &&
88             addr != NULL)
89                 *out = driver;
90         return driver == NULL;
91 }
92
93 static int
94 vdev_probe_all_drivers(struct rte_vdev_device *dev)
95 {
96         const char *name;
97         struct rte_vdev_driver *driver;
98         int ret;
99
100         name = rte_vdev_device_name(dev);
101
102         RTE_LOG(DEBUG, EAL, "Search driver %s to probe device %s\n", name,
103                 rte_vdev_device_name(dev));
104
105         if (vdev_parse(name, &driver))
106                 return -1;
107         dev->device.driver = &driver->driver;
108         ret = driver->probe(dev);
109         if (ret)
110                 dev->device.driver = NULL;
111         return ret;
112 }
113
114 static struct rte_vdev_device *
115 find_vdev(const char *name)
116 {
117         struct rte_vdev_device *dev;
118
119         if (!name)
120                 return NULL;
121
122         TAILQ_FOREACH(dev, &vdev_device_list, next) {
123                 const char *devname = rte_vdev_device_name(dev);
124                 if (!strncmp(devname, name, strlen(name)))
125                         return dev;
126         }
127
128         return NULL;
129 }
130
131 static struct rte_devargs *
132 alloc_devargs(const char *name, const char *args)
133 {
134         struct rte_devargs *devargs;
135         int ret;
136
137         devargs = calloc(1, sizeof(*devargs));
138         if (!devargs)
139                 return NULL;
140
141         devargs->bus = rte_bus_find_by_name("vdev");
142         if (args)
143                 devargs->args = strdup(args);
144
145         ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
146         if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
147                 free(devargs->args);
148                 free(devargs);
149                 return NULL;
150         }
151
152         return devargs;
153 }
154
155 int
156 rte_vdev_init(const char *name, const char *args)
157 {
158         struct rte_vdev_device *dev;
159         struct rte_devargs *devargs;
160         int ret;
161
162         if (name == NULL)
163                 return -EINVAL;
164
165         dev = find_vdev(name);
166         if (dev)
167                 return -EEXIST;
168
169         devargs = alloc_devargs(name, args);
170         if (!devargs)
171                 return -ENOMEM;
172
173         dev = calloc(1, sizeof(*dev));
174         if (!dev) {
175                 ret = -ENOMEM;
176                 goto fail;
177         }
178
179         dev->device.devargs = devargs;
180         dev->device.numa_node = SOCKET_ID_ANY;
181         dev->device.name = devargs->name;
182
183         ret = vdev_probe_all_drivers(dev);
184         if (ret) {
185                 if (ret > 0)
186                         RTE_LOG(ERR, EAL, "no driver found for %s\n", name);
187                 goto fail;
188         }
189
190         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
191
192         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
193         return 0;
194
195 fail:
196         free(devargs->args);
197         free(devargs);
198         free(dev);
199         return ret;
200 }
201
202 static int
203 vdev_remove_driver(struct rte_vdev_device *dev)
204 {
205         const char *name = rte_vdev_device_name(dev);
206         const struct rte_vdev_driver *driver;
207
208         if (!dev->device.driver) {
209                 RTE_LOG(DEBUG, EAL, "no driver attach to device %s\n", name);
210                 return 1;
211         }
212
213         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
214                 driver);
215         return driver->remove(dev);
216 }
217
218 int
219 rte_vdev_uninit(const char *name)
220 {
221         struct rte_vdev_device *dev;
222         struct rte_devargs *devargs;
223         int ret;
224
225         if (name == NULL)
226                 return -EINVAL;
227
228         dev = find_vdev(name);
229         if (!dev)
230                 return -ENOENT;
231
232         devargs = dev->device.devargs;
233
234         ret = vdev_remove_driver(dev);
235         if (ret)
236                 return ret;
237
238         TAILQ_REMOVE(&vdev_device_list, dev, next);
239
240         TAILQ_REMOVE(&devargs_list, devargs, next);
241
242         free(devargs->args);
243         free(devargs);
244         free(dev);
245         return 0;
246 }
247
248 static int
249 vdev_scan(void)
250 {
251         struct rte_vdev_device *dev;
252         struct rte_devargs *devargs;
253         struct rte_bus *vbus;
254
255         /* for virtual devices we scan the devargs_list populated via cmdline */
256         vbus = rte_bus_find_by_name("vdev");
257         TAILQ_FOREACH(devargs, &devargs_list, next) {
258
259                 if (devargs->bus != vbus)
260                         continue;
261
262                 dev = find_vdev(devargs->name);
263                 if (dev)
264                         continue;
265
266                 dev = calloc(1, sizeof(*dev));
267                 if (!dev)
268                         return -1;
269
270                 dev->device.devargs = devargs;
271                 dev->device.numa_node = SOCKET_ID_ANY;
272                 dev->device.name = devargs->name;
273
274                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
275         }
276
277         return 0;
278 }
279
280 static int
281 vdev_probe(void)
282 {
283         struct rte_vdev_device *dev;
284
285         /* call the init function for each virtual device */
286         TAILQ_FOREACH(dev, &vdev_device_list, next) {
287
288                 if (dev->device.driver)
289                         continue;
290
291                 if (vdev_probe_all_drivers(dev)) {
292                         RTE_LOG(ERR, EAL, "failed to initialize %s device\n",
293                                 rte_vdev_device_name(dev));
294                         return -1;
295                 }
296         }
297
298         return 0;
299 }
300
301 static struct rte_device *
302 vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
303                  const void *data)
304 {
305         struct rte_vdev_device *dev;
306
307         TAILQ_FOREACH(dev, &vdev_device_list, next) {
308                 if (start && &dev->device == start) {
309                         start = NULL;
310                         continue;
311                 }
312                 if (cmp(&dev->device, data) == 0)
313                         return &dev->device;
314         }
315         return NULL;
316 }
317
318 static int
319 vdev_unplug(struct rte_device *dev)
320 {
321         /*
322          * The virtual bus doesn't support 'unattached' devices so this is
323          * actually equal to hotplugging removal of it.
324          */
325         return rte_vdev_uninit(dev->name);
326 }
327
328 static struct rte_bus rte_vdev_bus = {
329         .scan = vdev_scan,
330         .probe = vdev_probe,
331         .find_device = vdev_find_device,
332         /* .plug = NULL, see comment on vdev_unplug */
333         .unplug = vdev_unplug,
334         .parse = vdev_parse,
335 };
336
337 RTE_REGISTER_BUS(vdev, rte_vdev_bus);