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