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