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