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