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 if (rte_dev_is_probed(dev)) {
223 RTE_LOG(ERR, EAL, "Device is already plugged\n");
227 ret = dev->bus->plug(dev);
229 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
238 if (rte_devargs_remove(da) != 0) {
245 int __rte_experimental
246 rte_dev_probe(const char *devargs)
248 struct eal_dev_mp_req req;
249 struct rte_device *dev;
252 memset(&req, 0, sizeof(req));
253 req.t = EAL_DEV_REQ_TYPE_ATTACH;
254 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
256 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
258 * If in secondary process, just send IPC request to
261 ret = eal_dev_hotplug_request_to_primary(&req);
264 "Failed to send hotplug request to primary\n");
269 "Failed to hotplug add device\n");
273 /* attach a shared device from primary start from here: */
275 /* primary attach the new device itself. */
276 ret = local_dev_probe(devargs, &dev);
280 "Failed to attach device on primary process\n");
283 * it is possible that secondary process failed to attached a
284 * device that primary process have during initialization,
285 * so for -EEXIST case, we still need to sync with secondary
292 /* primary send attach sync request to secondary. */
293 ret = eal_dev_hotplug_request_to_secondary(&req);
295 /* if any communication error, we need to rollback. */
298 "Failed to send hotplug add request to secondary\n");
304 * if any secondary failed to attach, we need to consider if rollback
307 if (req.result != 0) {
309 "Failed to attach device on secondary process\n");
312 /* for -EEXIST, we don't need to rollback. */
321 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
323 /* primary send rollback request to secondary. */
324 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
325 RTE_LOG(WARNING, EAL,
326 "Failed to rollback device attach on secondary."
327 "Devices in secondary may not sync with primary\n");
329 /* primary rollback itself. */
330 if (local_dev_remove(dev) != 0)
331 RTE_LOG(WARNING, EAL,
332 "Failed to rollback device attach on primary."
333 "Devices in secondary may not sync with primary\n");
339 rte_eal_hotplug_remove(const char *busname, const char *devname)
341 struct rte_device *dev;
344 bus = rte_bus_find_by_name(busname);
346 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
350 dev = bus->find_device(NULL, cmp_dev_name, devname);
352 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
356 return rte_dev_remove(dev);
359 /* remove device at local process. */
361 local_dev_remove(struct rte_device *dev)
365 if (dev->bus->unplug == NULL) {
366 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
371 ret = dev->bus->unplug(dev);
373 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
378 rte_devargs_remove(dev->devargs);
383 int __rte_experimental
384 rte_dev_remove(struct rte_device *dev)
386 struct eal_dev_mp_req req;
390 if (!rte_dev_is_probed(dev)) {
391 RTE_LOG(ERR, EAL, "Device is not probed\n");
395 ret = build_devargs(dev->devargs->bus->name, dev->name, "", &devargs);
399 memset(&req, 0, sizeof(req));
400 req.t = EAL_DEV_REQ_TYPE_DETACH;
401 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
404 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
406 * If in secondary process, just send IPC request to
409 ret = eal_dev_hotplug_request_to_primary(&req);
412 "Failed to send hotplug request to primary\n");
417 "Failed to hotplug remove device\n");
421 /* detach a device from primary start from here: */
423 /* primary send detach sync request to secondary */
424 ret = eal_dev_hotplug_request_to_secondary(&req);
427 * if communication error, we need to rollback, because it is possible
428 * part of the secondary processes still detached it successfully.
432 "Failed to send device detach request to secondary\n");
438 * if any secondary failed to detach, we need to consider if rollback
441 if (req.result != 0) {
443 "Failed to detach device on secondary process\n");
446 * if -ENOENT, we don't need to rollback, since devices is
447 * already detached on secondary process.
453 /* primary detach the device itself. */
454 ret = local_dev_remove(dev);
456 /* if primary failed, still need to consider if rollback is necessary */
459 "Failed to detach device on primary process\n");
460 /* if -ENOENT, we don't need to rollback */
469 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
471 /* primary send rollback request to secondary. */
472 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
473 RTE_LOG(WARNING, EAL,
474 "Failed to rollback device detach on secondary."
475 "Devices in secondary may not sync with primary\n");
480 int __rte_experimental
481 rte_dev_event_callback_register(const char *device_name,
482 rte_dev_event_cb_fn cb_fn,
485 struct dev_event_callback *event_cb;
491 rte_spinlock_lock(&dev_event_lock);
493 if (TAILQ_EMPTY(&dev_event_cbs))
494 TAILQ_INIT(&dev_event_cbs);
496 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
497 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
498 if (device_name == NULL && event_cb->dev_name == NULL)
500 if (device_name == NULL || event_cb->dev_name == NULL)
502 if (!strcmp(event_cb->dev_name, device_name))
507 /* create a new callback. */
508 if (event_cb == NULL) {
509 event_cb = malloc(sizeof(struct dev_event_callback));
510 if (event_cb != NULL) {
511 event_cb->cb_fn = cb_fn;
512 event_cb->cb_arg = cb_arg;
513 event_cb->active = 0;
515 event_cb->dev_name = NULL;
517 event_cb->dev_name = strdup(device_name);
518 if (event_cb->dev_name == NULL) {
523 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
526 "Failed to allocate memory for device "
533 "The callback is already exist, no need "
534 "to register again.\n");
538 rte_spinlock_unlock(&dev_event_lock);
542 rte_spinlock_unlock(&dev_event_lock);
546 int __rte_experimental
547 rte_dev_event_callback_unregister(const char *device_name,
548 rte_dev_event_cb_fn cb_fn,
552 struct dev_event_callback *event_cb, *next;
557 rte_spinlock_lock(&dev_event_lock);
558 /*walk through the callbacks and remove all that match. */
559 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
562 next = TAILQ_NEXT(event_cb, next);
564 if (device_name != NULL && event_cb->dev_name != NULL) {
565 if (!strcmp(event_cb->dev_name, device_name)) {
566 if (event_cb->cb_fn != cb_fn ||
567 (cb_arg != (void *)-1 &&
568 event_cb->cb_arg != cb_arg))
571 } else if (device_name != NULL) {
576 * if this callback is not executing right now,
579 if (event_cb->active == 0) {
580 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
587 rte_spinlock_unlock(&dev_event_lock);
591 void __rte_experimental
592 rte_dev_event_callback_process(const char *device_name,
593 enum rte_dev_event_type event)
595 struct dev_event_callback *cb_lst;
597 if (device_name == NULL)
600 rte_spinlock_lock(&dev_event_lock);
602 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
603 if (cb_lst->dev_name) {
604 if (strcmp(cb_lst->dev_name, device_name))
608 rte_spinlock_unlock(&dev_event_lock);
609 cb_lst->cb_fn(device_name, event,
611 rte_spinlock_lock(&dev_event_lock);
614 rte_spinlock_unlock(&dev_event_lock);
619 rte_dev_iterator_init(struct rte_dev_iterator *it,
622 struct rte_devargs devargs;
623 struct rte_class *cls = NULL;
624 struct rte_bus *bus = NULL;
626 /* Having both bus_str and cls_str NULL is illegal,
627 * marking this iterator as invalid unless
628 * everything goes well.
633 devargs.data = dev_str;
634 if (rte_devargs_layers_parse(&devargs, dev_str))
639 /* The string should have at least
640 * one layer specified.
642 if (bus == NULL && cls == NULL) {
644 "Either bus or class must be specified.\n");
648 if (bus != NULL && bus->dev_iterate == NULL) {
649 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
653 if (cls != NULL && cls->dev_iterate == NULL) {
654 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
658 it->bus_str = devargs.bus_str;
659 it->cls_str = devargs.cls_str;
660 it->dev_str = dev_str;
664 it->class_device = NULL;
670 dev_str_sane_copy(const char *str)
675 end = strcspn(str, ",/");
676 if (str[end] == ',') {
677 copy = strdup(&str[end + 1]);
687 slash = strchr(copy, '/');
695 class_next_dev_cmp(const struct rte_class *cls,
698 struct rte_dev_iterator *it;
699 const char *cls_str = NULL;
702 if (cls->dev_iterate == NULL)
705 cls_str = CLSCTX(ctx);
706 dev = it->class_device;
707 /* it->cls_str != NULL means a class
708 * was specified in the devstr.
710 if (it->cls_str != NULL && cls != it->cls)
712 /* If an error occurred previously,
713 * no need to test further.
717 dev = cls->dev_iterate(dev, cls_str, it);
718 it->class_device = dev;
723 bus_next_dev_cmp(const struct rte_bus *bus,
726 struct rte_device *dev = NULL;
727 struct rte_class *cls = NULL;
728 struct rte_dev_iterator *it;
729 const char *bus_str = NULL;
731 if (bus->dev_iterate == NULL)
734 bus_str = BUSCTX(ctx);
736 /* it->bus_str != NULL means a bus
737 * was specified in the devstr.
739 if (it->bus_str != NULL && bus != it->bus)
741 /* If an error occurred previously,
742 * no need to test further.
746 if (it->cls_str == NULL) {
747 dev = bus->dev_iterate(dev, bus_str, it);
750 /* cls_str != NULL */
753 dev = bus->dev_iterate(dev, bus_str, it);
759 cls = TAILQ_PREV(it->cls, rte_class_list, next);
760 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
765 goto next_dev_on_bus;
772 rte_dev_iterator_next(struct rte_dev_iterator *it)
774 struct rte_bus *bus = NULL;
775 int old_errno = rte_errno;
776 char *bus_str = NULL;
777 char *cls_str = NULL;
780 if (it->bus_str == NULL && it->cls_str == NULL) {
781 /* Invalid iterator. */
786 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
787 if (it->bus_str != NULL) {
788 bus_str = dev_str_sane_copy(it->bus_str);
792 if (it->cls_str != NULL) {
793 cls_str = dev_str_sane_copy(it->cls_str);
797 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
798 CTX(it, bus_str, cls_str)))) {
799 if (it->device != NULL) {
803 if (it->bus_str != NULL ||
808 rte_errno = old_errno;