62e9ed477aaf0d2288a59a40e43b81097348c0ca
[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 #include <rte_string_fns.h>
23
24 #include "eal_private.h"
25 #include "hotplug_mp.h"
26
27 /**
28  * The device event callback description.
29  *
30  * It contains callback address to be registered by user application,
31  * the pointer to the parameters for callback, and the device name.
32  */
33 struct dev_event_callback {
34         TAILQ_ENTRY(dev_event_callback) next; /**< Callbacks list */
35         rte_dev_event_cb_fn cb_fn;            /**< Callback address */
36         void *cb_arg;                         /**< Callback parameter */
37         char *dev_name;  /**< Callback device name, NULL is for all device */
38         uint32_t active;                      /**< Callback is executing */
39 };
40
41 /** @internal Structure to keep track of registered callbacks */
42 TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
43
44 /* The device event callback list for all registered callbacks. */
45 static struct dev_event_cb_list dev_event_cbs;
46
47 /* spinlock for device callbacks */
48 static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
49
50 struct dev_next_ctx {
51         struct rte_dev_iterator *it;
52         const char *bus_str;
53         const char *cls_str;
54 };
55
56 #define CTX(it, bus_str, cls_str) \
57         (&(const struct dev_next_ctx){ \
58                 .it = it, \
59                 .bus_str = bus_str, \
60                 .cls_str = cls_str, \
61         })
62
63 #define ITCTX(ptr) \
64         (((struct dev_next_ctx *)(intptr_t)ptr)->it)
65
66 #define BUSCTX(ptr) \
67         (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
68
69 #define CLSCTX(ptr) \
70         (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
71
72 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
73 {
74         const char *name = _name;
75
76         return strcmp(dev->name, name);
77 }
78
79 int __rte_experimental
80 rte_dev_is_probed(const struct rte_device *dev)
81 {
82         /* The field driver should be set only when the probe is successful. */
83         return dev->driver != NULL;
84 }
85
86 /* helper function to build devargs, caller should free the memory */
87 static int
88 build_devargs(const char *busname, const char *devname,
89               const char *drvargs, char **devargs)
90 {
91         int length;
92
93         length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
94         if (length < 0)
95                 return -EINVAL;
96
97         *devargs = malloc(length + 1);
98         if (*devargs == NULL)
99                 return -ENOMEM;
100
101         length = snprintf(*devargs, length + 1, "%s:%s,%s",
102                         busname, devname, drvargs);
103         if (length < 0) {
104                 free(*devargs);
105                 return -EINVAL;
106         }
107
108         return 0;
109 }
110
111 int
112 rte_eal_hotplug_add(const char *busname, const char *devname,
113                     const char *drvargs)
114 {
115
116         char *devargs;
117         int ret;
118
119         ret = build_devargs(busname, devname, drvargs, &devargs);
120         if (ret != 0)
121                 return ret;
122
123         ret = rte_dev_probe(devargs);
124         free(devargs);
125
126         return ret;
127 }
128
129 /* probe device at local process. */
130 int
131 local_dev_probe(const char *devargs, struct rte_device **new_dev)
132 {
133         struct rte_device *dev;
134         struct rte_devargs *da;
135         int ret;
136
137         *new_dev = NULL;
138         da = calloc(1, sizeof(*da));
139         if (da == NULL)
140                 return -ENOMEM;
141
142         ret = rte_devargs_parse(da, devargs);
143         if (ret)
144                 goto err_devarg;
145
146         if (da->bus->plug == NULL) {
147                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
148                         da->bus->name);
149                 ret = -ENOTSUP;
150                 goto err_devarg;
151         }
152
153         ret = rte_devargs_insert(da);
154         if (ret)
155                 goto err_devarg;
156
157         ret = da->bus->scan();
158         if (ret)
159                 goto err_devarg;
160
161         dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
162         if (dev == NULL) {
163                 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
164                         da->name);
165                 ret = -ENODEV;
166                 goto err_devarg;
167         }
168
169         ret = dev->bus->plug(dev);
170         if (ret) {
171                 if (rte_dev_is_probed(dev)) /* if already succeeded earlier */
172                         return ret; /* no rollback */
173                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
174                         dev->name);
175                 goto err_devarg;
176         }
177
178         *new_dev = dev;
179         return 0;
180
181 err_devarg:
182         if (rte_devargs_remove(da) != 0) {
183                 free(da->args);
184                 free(da);
185         }
186         return ret;
187 }
188
189 int __rte_experimental
190 rte_dev_probe(const char *devargs)
191 {
192         struct eal_dev_mp_req req;
193         struct rte_device *dev;
194         int ret;
195
196         memset(&req, 0, sizeof(req));
197         req.t = EAL_DEV_REQ_TYPE_ATTACH;
198         strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
199
200         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
201                 /**
202                  * If in secondary process, just send IPC request to
203                  * primary process.
204                  */
205                 ret = eal_dev_hotplug_request_to_primary(&req);
206                 if (ret != 0) {
207                         RTE_LOG(ERR, EAL,
208                                 "Failed to send hotplug request to primary\n");
209                         return -ENOMSG;
210                 }
211                 if (req.result != 0)
212                         RTE_LOG(ERR, EAL,
213                                 "Failed to hotplug add device\n");
214                 return req.result;
215         }
216
217         /* attach a shared device from primary start from here: */
218
219         /* primary attach the new device itself. */
220         ret = local_dev_probe(devargs, &dev);
221
222         if (ret != 0) {
223                 RTE_LOG(ERR, EAL,
224                         "Failed to attach device on primary process\n");
225
226                 /**
227                  * it is possible that secondary process failed to attached a
228                  * device that primary process have during initialization,
229                  * so for -EEXIST case, we still need to sync with secondary
230                  * process.
231                  */
232                 if (ret != -EEXIST)
233                         return ret;
234         }
235
236         /* primary send attach sync request to secondary. */
237         ret = eal_dev_hotplug_request_to_secondary(&req);
238
239         /* if any communication error, we need to rollback. */
240         if (ret != 0) {
241                 RTE_LOG(ERR, EAL,
242                         "Failed to send hotplug add request to secondary\n");
243                 ret = -ENOMSG;
244                 goto rollback;
245         }
246
247         /**
248          * if any secondary failed to attach, we need to consider if rollback
249          * is necessary.
250          */
251         if (req.result != 0) {
252                 RTE_LOG(ERR, EAL,
253                         "Failed to attach device on secondary process\n");
254                 ret = req.result;
255
256                 /* for -EEXIST, we don't need to rollback. */
257                 if (ret == -EEXIST)
258                         return ret;
259                 goto rollback;
260         }
261
262         return 0;
263
264 rollback:
265         req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
266
267         /* primary send rollback request to secondary. */
268         if (eal_dev_hotplug_request_to_secondary(&req) != 0)
269                 RTE_LOG(WARNING, EAL,
270                         "Failed to rollback device attach on secondary."
271                         "Devices in secondary may not sync with primary\n");
272
273         /* primary rollback itself. */
274         if (local_dev_remove(dev) != 0)
275                 RTE_LOG(WARNING, EAL,
276                         "Failed to rollback device attach on primary."
277                         "Devices in secondary may not sync with primary\n");
278
279         return ret;
280 }
281
282 int
283 rte_eal_hotplug_remove(const char *busname, const char *devname)
284 {
285         struct rte_device *dev;
286         struct rte_bus *bus;
287
288         bus = rte_bus_find_by_name(busname);
289         if (bus == NULL) {
290                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
291                 return -ENOENT;
292         }
293
294         dev = bus->find_device(NULL, cmp_dev_name, devname);
295         if (dev == NULL) {
296                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
297                 return -EINVAL;
298         }
299
300         return rte_dev_remove(dev);
301 }
302
303 /* remove device at local process. */
304 int
305 local_dev_remove(struct rte_device *dev)
306 {
307         int ret;
308
309         if (dev->bus->unplug == NULL) {
310                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
311                         dev->bus->name);
312                 return -ENOTSUP;
313         }
314
315         ret = dev->bus->unplug(dev);
316         if (ret) {
317                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
318                         dev->name);
319                 return ret;
320         }
321
322         return 0;
323 }
324
325 int __rte_experimental
326 rte_dev_remove(struct rte_device *dev)
327 {
328         struct eal_dev_mp_req req;
329         char *devargs;
330         int ret;
331
332         if (!rte_dev_is_probed(dev)) {
333                 RTE_LOG(ERR, EAL, "Device is not probed\n");
334                 return -ENOENT;
335         }
336
337         ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
338         if (ret != 0)
339                 return ret;
340
341         memset(&req, 0, sizeof(req));
342         req.t = EAL_DEV_REQ_TYPE_DETACH;
343         strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
344         free(devargs);
345
346         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
347                 /**
348                  * If in secondary process, just send IPC request to
349                  * primary process.
350                  */
351                 ret = eal_dev_hotplug_request_to_primary(&req);
352                 if (ret != 0) {
353                         RTE_LOG(ERR, EAL,
354                                 "Failed to send hotplug request to primary\n");
355                         return -ENOMSG;
356                 }
357                 if (req.result != 0)
358                         RTE_LOG(ERR, EAL,
359                                 "Failed to hotplug remove device\n");
360                 return req.result;
361         }
362
363         /* detach a device from primary start from here: */
364
365         /* primary send detach sync request to secondary */
366         ret = eal_dev_hotplug_request_to_secondary(&req);
367
368         /**
369          * if communication error, we need to rollback, because it is possible
370          * part of the secondary processes still detached it successfully.
371          */
372         if (ret != 0) {
373                 RTE_LOG(ERR, EAL,
374                         "Failed to send device detach request to secondary\n");
375                 ret = -ENOMSG;
376                 goto rollback;
377         }
378
379         /**
380          * if any secondary failed to detach, we need to consider if rollback
381          * is necessary.
382          */
383         if (req.result != 0) {
384                 RTE_LOG(ERR, EAL,
385                         "Failed to detach device on secondary process\n");
386                 ret = req.result;
387                 /**
388                  * if -ENOENT, we don't need to rollback, since devices is
389                  * already detached on secondary process.
390                  */
391                 if (ret != -ENOENT)
392                         goto rollback;
393         }
394
395         /* primary detach the device itself. */
396         ret = local_dev_remove(dev);
397
398         /* if primary failed, still need to consider if rollback is necessary */
399         if (ret != 0) {
400                 RTE_LOG(ERR, EAL,
401                         "Failed to detach device on primary process\n");
402                 /* if -ENOENT, we don't need to rollback */
403                 if (ret == -ENOENT)
404                         return ret;
405                 goto rollback;
406         }
407
408         return 0;
409
410 rollback:
411         req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
412
413         /* primary send rollback request to secondary. */
414         if (eal_dev_hotplug_request_to_secondary(&req) != 0)
415                 RTE_LOG(WARNING, EAL,
416                         "Failed to rollback device detach on secondary."
417                         "Devices in secondary may not sync with primary\n");
418
419         return ret;
420 }
421
422 int __rte_experimental
423 rte_dev_event_callback_register(const char *device_name,
424                                 rte_dev_event_cb_fn cb_fn,
425                                 void *cb_arg)
426 {
427         struct dev_event_callback *event_cb;
428         int ret;
429
430         if (!cb_fn)
431                 return -EINVAL;
432
433         rte_spinlock_lock(&dev_event_lock);
434
435         if (TAILQ_EMPTY(&dev_event_cbs))
436                 TAILQ_INIT(&dev_event_cbs);
437
438         TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
439                 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
440                         if (device_name == NULL && event_cb->dev_name == NULL)
441                                 break;
442                         if (device_name == NULL || event_cb->dev_name == NULL)
443                                 continue;
444                         if (!strcmp(event_cb->dev_name, device_name))
445                                 break;
446                 }
447         }
448
449         /* create a new callback. */
450         if (event_cb == NULL) {
451                 event_cb = malloc(sizeof(struct dev_event_callback));
452                 if (event_cb != NULL) {
453                         event_cb->cb_fn = cb_fn;
454                         event_cb->cb_arg = cb_arg;
455                         event_cb->active = 0;
456                         if (!device_name) {
457                                 event_cb->dev_name = NULL;
458                         } else {
459                                 event_cb->dev_name = strdup(device_name);
460                                 if (event_cb->dev_name == NULL) {
461                                         ret = -ENOMEM;
462                                         goto error;
463                                 }
464                         }
465                         TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
466                 } else {
467                         RTE_LOG(ERR, EAL,
468                                 "Failed to allocate memory for device "
469                                 "event callback.");
470                         ret = -ENOMEM;
471                         goto error;
472                 }
473         } else {
474                 RTE_LOG(ERR, EAL,
475                         "The callback is already exist, no need "
476                         "to register again.\n");
477                 ret = -EEXIST;
478         }
479
480         rte_spinlock_unlock(&dev_event_lock);
481         return 0;
482 error:
483         free(event_cb);
484         rte_spinlock_unlock(&dev_event_lock);
485         return ret;
486 }
487
488 int __rte_experimental
489 rte_dev_event_callback_unregister(const char *device_name,
490                                   rte_dev_event_cb_fn cb_fn,
491                                   void *cb_arg)
492 {
493         int ret = 0;
494         struct dev_event_callback *event_cb, *next;
495
496         if (!cb_fn)
497                 return -EINVAL;
498
499         rte_spinlock_lock(&dev_event_lock);
500         /*walk through the callbacks and remove all that match. */
501         for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
502              event_cb = next) {
503
504                 next = TAILQ_NEXT(event_cb, next);
505
506                 if (device_name != NULL && event_cb->dev_name != NULL) {
507                         if (!strcmp(event_cb->dev_name, device_name)) {
508                                 if (event_cb->cb_fn != cb_fn ||
509                                     (cb_arg != (void *)-1 &&
510                                     event_cb->cb_arg != cb_arg))
511                                         continue;
512                         }
513                 } else if (device_name != NULL) {
514                         continue;
515                 }
516
517                 /*
518                  * if this callback is not executing right now,
519                  * then remove it.
520                  */
521                 if (event_cb->active == 0) {
522                         TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
523                         free(event_cb);
524                         ret++;
525                 } else {
526                         continue;
527                 }
528         }
529         rte_spinlock_unlock(&dev_event_lock);
530         return ret;
531 }
532
533 void __rte_experimental
534 rte_dev_event_callback_process(const char *device_name,
535                                enum rte_dev_event_type event)
536 {
537         struct dev_event_callback *cb_lst;
538
539         if (device_name == NULL)
540                 return;
541
542         rte_spinlock_lock(&dev_event_lock);
543
544         TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
545                 if (cb_lst->dev_name) {
546                         if (strcmp(cb_lst->dev_name, device_name))
547                                 continue;
548                 }
549                 cb_lst->active = 1;
550                 rte_spinlock_unlock(&dev_event_lock);
551                 cb_lst->cb_fn(device_name, event,
552                                 cb_lst->cb_arg);
553                 rte_spinlock_lock(&dev_event_lock);
554                 cb_lst->active = 0;
555         }
556         rte_spinlock_unlock(&dev_event_lock);
557 }
558
559 __rte_experimental
560 int
561 rte_dev_iterator_init(struct rte_dev_iterator *it,
562                       const char *dev_str)
563 {
564         struct rte_devargs devargs;
565         struct rte_class *cls = NULL;
566         struct rte_bus *bus = NULL;
567
568         /* Having both bus_str and cls_str NULL is illegal,
569          * marking this iterator as invalid unless
570          * everything goes well.
571          */
572         it->bus_str = NULL;
573         it->cls_str = NULL;
574
575         devargs.data = dev_str;
576         if (rte_devargs_layers_parse(&devargs, dev_str))
577                 goto get_out;
578
579         bus = devargs.bus;
580         cls = devargs.cls;
581         /* The string should have at least
582          * one layer specified.
583          */
584         if (bus == NULL && cls == NULL) {
585                 RTE_LOG(ERR, EAL,
586                         "Either bus or class must be specified.\n");
587                 rte_errno = EINVAL;
588                 goto get_out;
589         }
590         if (bus != NULL && bus->dev_iterate == NULL) {
591                 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
592                 rte_errno = ENOTSUP;
593                 goto get_out;
594         }
595         if (cls != NULL && cls->dev_iterate == NULL) {
596                 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
597                 rte_errno = ENOTSUP;
598                 goto get_out;
599         }
600         it->bus_str = devargs.bus_str;
601         it->cls_str = devargs.cls_str;
602         it->dev_str = dev_str;
603         it->bus = bus;
604         it->cls = cls;
605         it->device = NULL;
606         it->class_device = NULL;
607 get_out:
608         return -rte_errno;
609 }
610
611 static char *
612 dev_str_sane_copy(const char *str)
613 {
614         size_t end;
615         char *copy;
616
617         end = strcspn(str, ",/");
618         if (str[end] == ',') {
619                 copy = strdup(&str[end + 1]);
620         } else {
621                 /* '/' or '\0' */
622                 copy = strdup("");
623         }
624         if (copy == NULL) {
625                 rte_errno = ENOMEM;
626         } else {
627                 char *slash;
628
629                 slash = strchr(copy, '/');
630                 if (slash != NULL)
631                         slash[0] = '\0';
632         }
633         return copy;
634 }
635
636 static int
637 class_next_dev_cmp(const struct rte_class *cls,
638                    const void *ctx)
639 {
640         struct rte_dev_iterator *it;
641         const char *cls_str = NULL;
642         void *dev;
643
644         if (cls->dev_iterate == NULL)
645                 return 1;
646         it = ITCTX(ctx);
647         cls_str = CLSCTX(ctx);
648         dev = it->class_device;
649         /* it->cls_str != NULL means a class
650          * was specified in the devstr.
651          */
652         if (it->cls_str != NULL && cls != it->cls)
653                 return 1;
654         /* If an error occurred previously,
655          * no need to test further.
656          */
657         if (rte_errno != 0)
658                 return -1;
659         dev = cls->dev_iterate(dev, cls_str, it);
660         it->class_device = dev;
661         return dev == NULL;
662 }
663
664 static int
665 bus_next_dev_cmp(const struct rte_bus *bus,
666                  const void *ctx)
667 {
668         struct rte_device *dev = NULL;
669         struct rte_class *cls = NULL;
670         struct rte_dev_iterator *it;
671         const char *bus_str = NULL;
672
673         if (bus->dev_iterate == NULL)
674                 return 1;
675         it = ITCTX(ctx);
676         bus_str = BUSCTX(ctx);
677         dev = it->device;
678         /* it->bus_str != NULL means a bus
679          * was specified in the devstr.
680          */
681         if (it->bus_str != NULL && bus != it->bus)
682                 return 1;
683         /* If an error occurred previously,
684          * no need to test further.
685          */
686         if (rte_errno != 0)
687                 return -1;
688         if (it->cls_str == NULL) {
689                 dev = bus->dev_iterate(dev, bus_str, it);
690                 goto end;
691         }
692         /* cls_str != NULL */
693         if (dev == NULL) {
694 next_dev_on_bus:
695                 dev = bus->dev_iterate(dev, bus_str, it);
696                 it->device = dev;
697         }
698         if (dev == NULL)
699                 return 1;
700         if (it->cls != NULL)
701                 cls = TAILQ_PREV(it->cls, rte_class_list, next);
702         cls = rte_class_find(cls, class_next_dev_cmp, ctx);
703         if (cls != NULL) {
704                 it->cls = cls;
705                 goto end;
706         }
707         goto next_dev_on_bus;
708 end:
709         it->device = dev;
710         return dev == NULL;
711 }
712 __rte_experimental
713 struct rte_device *
714 rte_dev_iterator_next(struct rte_dev_iterator *it)
715 {
716         struct rte_bus *bus = NULL;
717         int old_errno = rte_errno;
718         char *bus_str = NULL;
719         char *cls_str = NULL;
720
721         rte_errno = 0;
722         if (it->bus_str == NULL && it->cls_str == NULL) {
723                 /* Invalid iterator. */
724                 rte_errno = EINVAL;
725                 return NULL;
726         }
727         if (it->bus != NULL)
728                 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
729         if (it->bus_str != NULL) {
730                 bus_str = dev_str_sane_copy(it->bus_str);
731                 if (bus_str == NULL)
732                         goto out;
733         }
734         if (it->cls_str != NULL) {
735                 cls_str = dev_str_sane_copy(it->cls_str);
736                 if (cls_str == NULL)
737                         goto out;
738         }
739         while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
740                                    CTX(it, bus_str, cls_str)))) {
741                 if (it->device != NULL) {
742                         it->bus = bus;
743                         goto out;
744                 }
745                 if (it->bus_str != NULL ||
746                     rte_errno != 0)
747                         break;
748         }
749         if (rte_errno == 0)
750                 rte_errno = old_errno;
751 out:
752         free(bus_str);
753         free(cls_str);
754         return it->device;
755 }