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