1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
11 #include <rte_compat.h>
13 #include <rte_class.h>
15 #include <rte_devargs.h>
16 #include <rte_debug.h>
17 #include <rte_errno.h>
18 #include <rte_kvargs.h>
20 #include <rte_spinlock.h>
21 #include <rte_malloc.h>
22 #include <rte_string_fns.h>
24 #include "eal_private.h"
25 #include "hotplug_mp.h"
28 * The device event callback description.
30 * It contains callback address to be registered by user application,
31 * the pointer to the parameters for callback, and the device name.
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 */
41 /** @internal Structure to keep track of registered callbacks */
42 TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
44 /* The device event callback list for all registered callbacks. */
45 static struct dev_event_cb_list dev_event_cbs;
47 /* spinlock for device callbacks */
48 static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
51 struct rte_dev_iterator *it;
56 #define CTX(it, bus_str, cls_str) \
57 (&(const struct dev_next_ctx){ \
64 (((struct dev_next_ctx *)(intptr_t)ptr)->it)
67 (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
70 (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
72 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
74 const char *name = _name;
76 return strcmp(dev->name, name);
79 int __rte_experimental
80 rte_dev_is_probed(const struct rte_device *dev)
82 /* The field driver should be set only when the probe is successful. */
83 return dev->driver != NULL;
86 int rte_eal_dev_attach(const char *name, const char *devargs)
90 if (name == NULL || devargs == NULL) {
91 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
95 bus = rte_bus_find_by_device_name(name);
97 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
101 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
102 return rte_eal_hotplug_add(bus->name, name, devargs);
105 "Device attach is only supported for PCI and vdev devices.\n");
110 int rte_eal_dev_detach(struct rte_device *dev)
116 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
120 bus = rte_bus_find_by_device(dev);
122 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
127 if (bus->unplug == NULL) {
128 RTE_LOG(ERR, EAL, "Bus function not supported\n");
132 ret = bus->unplug(dev);
134 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
139 /* helper function to build devargs, caller should free the memory */
141 build_devargs(const char *busname, const char *devname,
142 const char *drvargs, char **devargs)
146 length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
150 *devargs = malloc(length + 1);
151 if (*devargs == NULL)
154 length = snprintf(*devargs, length + 1, "%s:%s,%s",
155 busname, devname, drvargs);
165 rte_eal_hotplug_add(const char *busname, const char *devname,
172 ret = build_devargs(busname, devname, drvargs, &devargs);
176 ret = rte_dev_probe(devargs);
182 /* probe device at local process. */
184 local_dev_probe(const char *devargs, struct rte_device **new_dev)
186 struct rte_device *dev;
187 struct rte_devargs *da;
191 da = calloc(1, sizeof(*da));
195 ret = rte_devargs_parse(da, devargs);
199 if (da->bus->plug == NULL) {
200 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
206 ret = rte_devargs_insert(da);
210 ret = da->bus->scan();
214 dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
216 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
222 ret = dev->bus->plug(dev);
224 if (rte_dev_is_probed(dev)) /* if already succeeded earlier */
225 return ret; /* no rollback */
226 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
235 if (rte_devargs_remove(da) != 0) {
242 int __rte_experimental
243 rte_dev_probe(const char *devargs)
245 struct eal_dev_mp_req req;
246 struct rte_device *dev;
249 memset(&req, 0, sizeof(req));
250 req.t = EAL_DEV_REQ_TYPE_ATTACH;
251 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
253 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
255 * If in secondary process, just send IPC request to
258 ret = eal_dev_hotplug_request_to_primary(&req);
261 "Failed to send hotplug request to primary\n");
266 "Failed to hotplug add device\n");
270 /* attach a shared device from primary start from here: */
272 /* primary attach the new device itself. */
273 ret = local_dev_probe(devargs, &dev);
277 "Failed to attach device on primary process\n");
280 * it is possible that secondary process failed to attached a
281 * device that primary process have during initialization,
282 * so for -EEXIST case, we still need to sync with secondary
289 /* primary send attach sync request to secondary. */
290 ret = eal_dev_hotplug_request_to_secondary(&req);
292 /* if any communication error, we need to rollback. */
295 "Failed to send hotplug add request to secondary\n");
301 * if any secondary failed to attach, we need to consider if rollback
304 if (req.result != 0) {
306 "Failed to attach device on secondary process\n");
309 /* for -EEXIST, we don't need to rollback. */
318 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
320 /* primary send rollback request to secondary. */
321 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
322 RTE_LOG(WARNING, EAL,
323 "Failed to rollback device attach on secondary."
324 "Devices in secondary may not sync with primary\n");
326 /* primary rollback itself. */
327 if (local_dev_remove(dev) != 0)
328 RTE_LOG(WARNING, EAL,
329 "Failed to rollback device attach on primary."
330 "Devices in secondary may not sync with primary\n");
336 rte_eal_hotplug_remove(const char *busname, const char *devname)
338 struct rte_device *dev;
341 bus = rte_bus_find_by_name(busname);
343 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
347 dev = bus->find_device(NULL, cmp_dev_name, devname);
349 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
353 return rte_dev_remove(dev);
356 /* remove device at local process. */
358 local_dev_remove(struct rte_device *dev)
362 if (dev->bus->unplug == NULL) {
363 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
368 ret = dev->bus->unplug(dev);
370 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
378 int __rte_experimental
379 rte_dev_remove(struct rte_device *dev)
381 struct eal_dev_mp_req req;
385 if (!rte_dev_is_probed(dev)) {
386 RTE_LOG(ERR, EAL, "Device is not probed\n");
390 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
394 memset(&req, 0, sizeof(req));
395 req.t = EAL_DEV_REQ_TYPE_DETACH;
396 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
399 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
401 * If in secondary process, just send IPC request to
404 ret = eal_dev_hotplug_request_to_primary(&req);
407 "Failed to send hotplug request to primary\n");
412 "Failed to hotplug remove device\n");
416 /* detach a device from primary start from here: */
418 /* primary send detach sync request to secondary */
419 ret = eal_dev_hotplug_request_to_secondary(&req);
422 * if communication error, we need to rollback, because it is possible
423 * part of the secondary processes still detached it successfully.
427 "Failed to send device detach request to secondary\n");
433 * if any secondary failed to detach, we need to consider if rollback
436 if (req.result != 0) {
438 "Failed to detach device on secondary process\n");
441 * if -ENOENT, we don't need to rollback, since devices is
442 * already detached on secondary process.
448 /* primary detach the device itself. */
449 ret = local_dev_remove(dev);
451 /* if primary failed, still need to consider if rollback is necessary */
454 "Failed to detach device on primary process\n");
455 /* if -ENOENT, we don't need to rollback */
464 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
466 /* primary send rollback request to secondary. */
467 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
468 RTE_LOG(WARNING, EAL,
469 "Failed to rollback device detach on secondary."
470 "Devices in secondary may not sync with primary\n");
475 int __rte_experimental
476 rte_dev_event_callback_register(const char *device_name,
477 rte_dev_event_cb_fn cb_fn,
480 struct dev_event_callback *event_cb;
486 rte_spinlock_lock(&dev_event_lock);
488 if (TAILQ_EMPTY(&dev_event_cbs))
489 TAILQ_INIT(&dev_event_cbs);
491 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
492 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
493 if (device_name == NULL && event_cb->dev_name == NULL)
495 if (device_name == NULL || event_cb->dev_name == NULL)
497 if (!strcmp(event_cb->dev_name, device_name))
502 /* create a new callback. */
503 if (event_cb == NULL) {
504 event_cb = malloc(sizeof(struct dev_event_callback));
505 if (event_cb != NULL) {
506 event_cb->cb_fn = cb_fn;
507 event_cb->cb_arg = cb_arg;
508 event_cb->active = 0;
510 event_cb->dev_name = NULL;
512 event_cb->dev_name = strdup(device_name);
513 if (event_cb->dev_name == NULL) {
518 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
521 "Failed to allocate memory for device "
528 "The callback is already exist, no need "
529 "to register again.\n");
533 rte_spinlock_unlock(&dev_event_lock);
537 rte_spinlock_unlock(&dev_event_lock);
541 int __rte_experimental
542 rte_dev_event_callback_unregister(const char *device_name,
543 rte_dev_event_cb_fn cb_fn,
547 struct dev_event_callback *event_cb, *next;
552 rte_spinlock_lock(&dev_event_lock);
553 /*walk through the callbacks and remove all that match. */
554 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
557 next = TAILQ_NEXT(event_cb, next);
559 if (device_name != NULL && event_cb->dev_name != NULL) {
560 if (!strcmp(event_cb->dev_name, device_name)) {
561 if (event_cb->cb_fn != cb_fn ||
562 (cb_arg != (void *)-1 &&
563 event_cb->cb_arg != cb_arg))
566 } else if (device_name != NULL) {
571 * if this callback is not executing right now,
574 if (event_cb->active == 0) {
575 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
582 rte_spinlock_unlock(&dev_event_lock);
586 void __rte_experimental
587 rte_dev_event_callback_process(const char *device_name,
588 enum rte_dev_event_type event)
590 struct dev_event_callback *cb_lst;
592 if (device_name == NULL)
595 rte_spinlock_lock(&dev_event_lock);
597 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
598 if (cb_lst->dev_name) {
599 if (strcmp(cb_lst->dev_name, device_name))
603 rte_spinlock_unlock(&dev_event_lock);
604 cb_lst->cb_fn(device_name, event,
606 rte_spinlock_lock(&dev_event_lock);
609 rte_spinlock_unlock(&dev_event_lock);
614 rte_dev_iterator_init(struct rte_dev_iterator *it,
617 struct rte_devargs devargs;
618 struct rte_class *cls = NULL;
619 struct rte_bus *bus = NULL;
621 /* Having both bus_str and cls_str NULL is illegal,
622 * marking this iterator as invalid unless
623 * everything goes well.
628 devargs.data = dev_str;
629 if (rte_devargs_layers_parse(&devargs, dev_str))
634 /* The string should have at least
635 * one layer specified.
637 if (bus == NULL && cls == NULL) {
639 "Either bus or class must be specified.\n");
643 if (bus != NULL && bus->dev_iterate == NULL) {
644 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
648 if (cls != NULL && cls->dev_iterate == NULL) {
649 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
653 it->bus_str = devargs.bus_str;
654 it->cls_str = devargs.cls_str;
655 it->dev_str = dev_str;
659 it->class_device = NULL;
665 dev_str_sane_copy(const char *str)
670 end = strcspn(str, ",/");
671 if (str[end] == ',') {
672 copy = strdup(&str[end + 1]);
682 slash = strchr(copy, '/');
690 class_next_dev_cmp(const struct rte_class *cls,
693 struct rte_dev_iterator *it;
694 const char *cls_str = NULL;
697 if (cls->dev_iterate == NULL)
700 cls_str = CLSCTX(ctx);
701 dev = it->class_device;
702 /* it->cls_str != NULL means a class
703 * was specified in the devstr.
705 if (it->cls_str != NULL && cls != it->cls)
707 /* If an error occurred previously,
708 * no need to test further.
712 dev = cls->dev_iterate(dev, cls_str, it);
713 it->class_device = dev;
718 bus_next_dev_cmp(const struct rte_bus *bus,
721 struct rte_device *dev = NULL;
722 struct rte_class *cls = NULL;
723 struct rte_dev_iterator *it;
724 const char *bus_str = NULL;
726 if (bus->dev_iterate == NULL)
729 bus_str = BUSCTX(ctx);
731 /* it->bus_str != NULL means a bus
732 * was specified in the devstr.
734 if (it->bus_str != NULL && bus != it->bus)
736 /* If an error occurred previously,
737 * no need to test further.
741 if (it->cls_str == NULL) {
742 dev = bus->dev_iterate(dev, bus_str, it);
745 /* cls_str != NULL */
748 dev = bus->dev_iterate(dev, bus_str, it);
754 cls = TAILQ_PREV(it->cls, rte_class_list, next);
755 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
760 goto next_dev_on_bus;
767 rte_dev_iterator_next(struct rte_dev_iterator *it)
769 struct rte_bus *bus = NULL;
770 int old_errno = rte_errno;
771 char *bus_str = NULL;
772 char *cls_str = NULL;
775 if (it->bus_str == NULL && it->cls_str == NULL) {
776 /* Invalid iterator. */
781 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
782 if (it->bus_str != NULL) {
783 bus_str = dev_str_sane_copy(it->bus_str);
787 if (it->cls_str != NULL) {
788 cls_str = dev_str_sane_copy(it->cls_str);
792 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
793 CTX(it, bus_str, cls_str)))) {
794 if (it->device != NULL) {
798 if (it->bus_str != NULL ||
803 rte_errno = old_errno;