eal: implement device iteration
[dpdk.git] / lib / librte_eal / common / eal_common_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <inttypes.h>
9 #include <sys/queue.h>
10
11 #include <rte_compat.h>
12 #include <rte_bus.h>
13 #include <rte_class.h>
14 #include <rte_dev.h>
15 #include <rte_devargs.h>
16 #include <rte_debug.h>
17 #include <rte_errno.h>
18 #include <rte_kvargs.h>
19 #include <rte_log.h>
20 #include <rte_spinlock.h>
21 #include <rte_malloc.h>
22
23 #include "eal_private.h"
24
25 /**
26  * The device event callback description.
27  *
28  * It contains callback address to be registered by user application,
29  * the pointer to the parameters for callback, and the device name.
30  */
31 struct dev_event_callback {
32         TAILQ_ENTRY(dev_event_callback) next; /**< Callbacks list */
33         rte_dev_event_cb_fn cb_fn;            /**< Callback address */
34         void *cb_arg;                         /**< Callback parameter */
35         char *dev_name;  /**< Callback device name, NULL is for all device */
36         uint32_t active;                      /**< Callback is executing */
37 };
38
39 /** @internal Structure to keep track of registered callbacks */
40 TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
41
42 /* The device event callback list for all registered callbacks. */
43 static struct dev_event_cb_list dev_event_cbs;
44
45 /* spinlock for device callbacks */
46 static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
47
48 struct dev_next_ctx {
49         struct rte_dev_iterator *it;
50         const char *bus_str;
51         const char *cls_str;
52 };
53
54 #define CTX(it, bus_str, cls_str) \
55         (&(const struct dev_next_ctx){ \
56                 .it = it, \
57                 .bus_str = bus_str, \
58                 .cls_str = cls_str, \
59         })
60
61 #define ITCTX(ptr) \
62         (((struct dev_next_ctx *)(intptr_t)ptr)->it)
63
64 #define BUSCTX(ptr) \
65         (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
66
67 #define CLSCTX(ptr) \
68         (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
69
70 static int cmp_detached_dev_name(const struct rte_device *dev,
71         const void *_name)
72 {
73         const char *name = _name;
74
75         /* skip attached devices */
76         if (dev->driver != NULL)
77                 return 1;
78
79         return strcmp(dev->name, name);
80 }
81
82 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
83 {
84         const char *name = _name;
85
86         return strcmp(dev->name, name);
87 }
88
89 int rte_eal_dev_attach(const char *name, const char *devargs)
90 {
91         struct rte_bus *bus;
92
93         if (name == NULL || devargs == NULL) {
94                 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
95                 return -EINVAL;
96         }
97
98         bus = rte_bus_find_by_device_name(name);
99         if (bus == NULL) {
100                 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
101                         name);
102                 return -EINVAL;
103         }
104         if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
105                 return rte_eal_hotplug_add(bus->name, name, devargs);
106
107         RTE_LOG(ERR, EAL,
108                 "Device attach is only supported for PCI and vdev devices.\n");
109
110         return -ENOTSUP;
111 }
112
113 int rte_eal_dev_detach(struct rte_device *dev)
114 {
115         struct rte_bus *bus;
116         int ret;
117
118         if (dev == NULL) {
119                 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
120                 return -EINVAL;
121         }
122
123         bus = rte_bus_find_by_device(dev);
124         if (bus == NULL) {
125                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
126                         dev->name);
127                 return -EINVAL;
128         }
129
130         if (bus->unplug == NULL) {
131                 RTE_LOG(ERR, EAL, "Bus function not supported\n");
132                 return -ENOTSUP;
133         }
134
135         ret = bus->unplug(dev);
136         if (ret)
137                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
138                         dev->name);
139         return ret;
140 }
141
142 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
143                         const char *devargs)
144 {
145         struct rte_bus *bus;
146         struct rte_device *dev;
147         struct rte_devargs *da;
148         int ret;
149
150         bus = rte_bus_find_by_name(busname);
151         if (bus == NULL) {
152                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
153                 return -ENOENT;
154         }
155
156         if (bus->plug == NULL) {
157                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
158                         bus->name);
159                 return -ENOTSUP;
160         }
161
162         da = calloc(1, sizeof(*da));
163         if (da == NULL)
164                 return -ENOMEM;
165
166         ret = rte_devargs_parsef(da, "%s:%s,%s",
167                                  busname, devname, devargs);
168         if (ret)
169                 goto err_devarg;
170
171         ret = rte_devargs_insert(da);
172         if (ret)
173                 goto err_devarg;
174
175         ret = bus->scan();
176         if (ret)
177                 goto err_devarg;
178
179         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
180         if (dev == NULL) {
181                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
182                         devname);
183                 ret = -ENODEV;
184                 goto err_devarg;
185         }
186
187         ret = bus->plug(dev);
188         if (ret) {
189                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
190                         dev->name);
191                 goto err_devarg;
192         }
193         return 0;
194
195 err_devarg:
196         if (rte_devargs_remove(busname, devname)) {
197                 free(da->args);
198                 free(da);
199         }
200         return ret;
201 }
202
203 int __rte_experimental
204 rte_eal_hotplug_remove(const char *busname, const char *devname)
205 {
206         struct rte_bus *bus;
207         struct rte_device *dev;
208         int ret;
209
210         bus = rte_bus_find_by_name(busname);
211         if (bus == NULL) {
212                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
213                 return -ENOENT;
214         }
215
216         if (bus->unplug == NULL) {
217                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
218                         bus->name);
219                 return -ENOTSUP;
220         }
221
222         dev = bus->find_device(NULL, cmp_dev_name, devname);
223         if (dev == NULL) {
224                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
225                 return -EINVAL;
226         }
227
228         ret = bus->unplug(dev);
229         if (ret)
230                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
231                         dev->name);
232         rte_devargs_remove(busname, devname);
233         return ret;
234 }
235
236 int __rte_experimental
237 rte_dev_event_callback_register(const char *device_name,
238                                 rte_dev_event_cb_fn cb_fn,
239                                 void *cb_arg)
240 {
241         struct dev_event_callback *event_cb;
242         int ret;
243
244         if (!cb_fn)
245                 return -EINVAL;
246
247         rte_spinlock_lock(&dev_event_lock);
248
249         if (TAILQ_EMPTY(&dev_event_cbs))
250                 TAILQ_INIT(&dev_event_cbs);
251
252         TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
253                 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
254                         if (device_name == NULL && event_cb->dev_name == NULL)
255                                 break;
256                         if (device_name == NULL || event_cb->dev_name == NULL)
257                                 continue;
258                         if (!strcmp(event_cb->dev_name, device_name))
259                                 break;
260                 }
261         }
262
263         /* create a new callback. */
264         if (event_cb == NULL) {
265                 event_cb = malloc(sizeof(struct dev_event_callback));
266                 if (event_cb != NULL) {
267                         event_cb->cb_fn = cb_fn;
268                         event_cb->cb_arg = cb_arg;
269                         event_cb->active = 0;
270                         if (!device_name) {
271                                 event_cb->dev_name = NULL;
272                         } else {
273                                 event_cb->dev_name = strdup(device_name);
274                                 if (event_cb->dev_name == NULL) {
275                                         ret = -ENOMEM;
276                                         goto error;
277                                 }
278                         }
279                         TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
280                 } else {
281                         RTE_LOG(ERR, EAL,
282                                 "Failed to allocate memory for device "
283                                 "event callback.");
284                         ret = -ENOMEM;
285                         goto error;
286                 }
287         } else {
288                 RTE_LOG(ERR, EAL,
289                         "The callback is already exist, no need "
290                         "to register again.\n");
291                 ret = -EEXIST;
292         }
293
294         rte_spinlock_unlock(&dev_event_lock);
295         return 0;
296 error:
297         free(event_cb);
298         rte_spinlock_unlock(&dev_event_lock);
299         return ret;
300 }
301
302 int __rte_experimental
303 rte_dev_event_callback_unregister(const char *device_name,
304                                   rte_dev_event_cb_fn cb_fn,
305                                   void *cb_arg)
306 {
307         int ret = 0;
308         struct dev_event_callback *event_cb, *next;
309
310         if (!cb_fn)
311                 return -EINVAL;
312
313         rte_spinlock_lock(&dev_event_lock);
314         /*walk through the callbacks and remove all that match. */
315         for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
316              event_cb = next) {
317
318                 next = TAILQ_NEXT(event_cb, next);
319
320                 if (device_name != NULL && event_cb->dev_name != NULL) {
321                         if (!strcmp(event_cb->dev_name, device_name)) {
322                                 if (event_cb->cb_fn != cb_fn ||
323                                     (cb_arg != (void *)-1 &&
324                                     event_cb->cb_arg != cb_arg))
325                                         continue;
326                         }
327                 } else if (device_name != NULL) {
328                         continue;
329                 }
330
331                 /*
332                  * if this callback is not executing right now,
333                  * then remove it.
334                  */
335                 if (event_cb->active == 0) {
336                         TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
337                         free(event_cb);
338                         ret++;
339                 } else {
340                         continue;
341                 }
342         }
343         rte_spinlock_unlock(&dev_event_lock);
344         return ret;
345 }
346
347 void
348 dev_callback_process(char *device_name, enum rte_dev_event_type event)
349 {
350         struct dev_event_callback *cb_lst;
351
352         if (device_name == NULL)
353                 return;
354
355         rte_spinlock_lock(&dev_event_lock);
356
357         TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
358                 if (cb_lst->dev_name) {
359                         if (strcmp(cb_lst->dev_name, device_name))
360                                 continue;
361                 }
362                 cb_lst->active = 1;
363                 rte_spinlock_unlock(&dev_event_lock);
364                 cb_lst->cb_fn(device_name, event,
365                                 cb_lst->cb_arg);
366                 rte_spinlock_lock(&dev_event_lock);
367                 cb_lst->active = 0;
368         }
369         rte_spinlock_unlock(&dev_event_lock);
370 }
371
372 __rte_experimental
373 int
374 rte_dev_iterator_init(struct rte_dev_iterator *it,
375                       const char *dev_str)
376 {
377         struct rte_devargs devargs;
378         struct rte_class *cls = NULL;
379         struct rte_bus *bus = NULL;
380
381         /* Having both bus_str and cls_str NULL is illegal,
382          * marking this iterator as invalid unless
383          * everything goes well.
384          */
385         it->bus_str = NULL;
386         it->cls_str = NULL;
387
388         devargs.data = dev_str;
389         if (rte_devargs_layers_parse(&devargs, dev_str))
390                 goto get_out;
391
392         bus = devargs.bus;
393         cls = devargs.cls;
394         /* The string should have at least
395          * one layer specified.
396          */
397         if (bus == NULL && cls == NULL) {
398                 RTE_LOG(ERR, EAL,
399                         "Either bus or class must be specified.\n");
400                 rte_errno = EINVAL;
401                 goto get_out;
402         }
403         if (bus != NULL && bus->dev_iterate == NULL) {
404                 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
405                 rte_errno = ENOTSUP;
406                 goto get_out;
407         }
408         if (cls != NULL && cls->dev_iterate == NULL) {
409                 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
410                 rte_errno = ENOTSUP;
411                 goto get_out;
412         }
413         it->bus_str = devargs.bus_str;
414         it->cls_str = devargs.cls_str;
415         it->dev_str = dev_str;
416         it->bus = bus;
417         it->cls = cls;
418         it->device = NULL;
419         it->class_device = NULL;
420 get_out:
421         return -rte_errno;
422 }
423
424 static char *
425 dev_str_sane_copy(const char *str)
426 {
427         size_t end;
428         char *copy;
429
430         end = strcspn(str, ",/");
431         if (str[end] == ',') {
432                 copy = strdup(&str[end + 1]);
433         } else {
434                 /* '/' or '\0' */
435                 copy = strdup("");
436         }
437         if (copy == NULL) {
438                 rte_errno = ENOMEM;
439         } else {
440                 char *slash;
441
442                 slash = strchr(copy, '/');
443                 if (slash != NULL)
444                         slash[0] = '\0';
445         }
446         return copy;
447 }
448
449 static int
450 class_next_dev_cmp(const struct rte_class *cls,
451                    const void *ctx)
452 {
453         struct rte_dev_iterator *it;
454         const char *cls_str = NULL;
455         void *dev;
456
457         if (cls->dev_iterate == NULL)
458                 return 1;
459         it = ITCTX(ctx);
460         cls_str = CLSCTX(ctx);
461         dev = it->class_device;
462         /* it->cls_str != NULL means a class
463          * was specified in the devstr.
464          */
465         if (it->cls_str != NULL && cls != it->cls)
466                 return 1;
467         /* If an error occurred previously,
468          * no need to test further.
469          */
470         if (rte_errno != 0)
471                 return -1;
472         dev = cls->dev_iterate(dev, cls_str, it);
473         it->class_device = dev;
474         return dev == NULL;
475 }
476
477 static int
478 bus_next_dev_cmp(const struct rte_bus *bus,
479                  const void *ctx)
480 {
481         struct rte_device *dev = NULL;
482         struct rte_class *cls = NULL;
483         struct rte_dev_iterator *it;
484         const char *bus_str = NULL;
485
486         if (bus->dev_iterate == NULL)
487                 return 1;
488         it = ITCTX(ctx);
489         bus_str = BUSCTX(ctx);
490         dev = it->device;
491         /* it->bus_str != NULL means a bus
492          * was specified in the devstr.
493          */
494         if (it->bus_str != NULL && bus != it->bus)
495                 return 1;
496         /* If an error occurred previously,
497          * no need to test further.
498          */
499         if (rte_errno != 0)
500                 return -1;
501         if (it->cls_str == NULL) {
502                 dev = bus->dev_iterate(dev, bus_str, it);
503                 goto end;
504         }
505         /* cls_str != NULL */
506         if (dev == NULL) {
507 next_dev_on_bus:
508                 dev = bus->dev_iterate(dev, bus_str, it);
509                 it->device = dev;
510         }
511         if (dev == NULL)
512                 return 1;
513         if (it->cls != NULL)
514                 cls = TAILQ_PREV(it->cls, rte_class_list, next);
515         cls = rte_class_find(cls, class_next_dev_cmp, ctx);
516         if (cls != NULL) {
517                 it->cls = cls;
518                 goto end;
519         }
520         goto next_dev_on_bus;
521 end:
522         it->device = dev;
523         return dev == NULL;
524 }
525 __rte_experimental
526 struct rte_device *
527 rte_dev_iterator_next(struct rte_dev_iterator *it)
528 {
529         struct rte_bus *bus = NULL;
530         int old_errno = rte_errno;
531         char *bus_str = NULL;
532         char *cls_str = NULL;
533
534         rte_errno = 0;
535         if (it->bus_str == NULL && it->cls_str == NULL) {
536                 /* Invalid iterator. */
537                 rte_errno = EINVAL;
538                 return NULL;
539         }
540         if (it->bus != NULL)
541                 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
542         if (it->bus_str != NULL) {
543                 bus_str = dev_str_sane_copy(it->bus_str);
544                 if (bus_str == NULL)
545                         goto out;
546         }
547         if (it->cls_str != NULL) {
548                 cls_str = dev_str_sane_copy(it->cls_str);
549                 if (cls_str == NULL)
550                         goto out;
551         }
552         while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
553                                    CTX(it, bus_str, cls_str)))) {
554                 if (it->device != NULL) {
555                         it->bus = bus;
556                         goto out;
557                 }
558                 if (it->bus_str != NULL ||
559                     rte_errno != 0)
560                         break;
561         }
562         if (rte_errno == 0)
563                 rte_errno = old_errno;
564 out:
565         free(bus_str);
566         free(cls_str);
567         return it->device;
568 }