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 /* helper function to build devargs, caller should free the memory */
88 build_devargs(const char *busname, const char *devname,
89 const char *drvargs, char **devargs)
93 length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
97 *devargs = malloc(length + 1);
101 length = snprintf(*devargs, length + 1, "%s:%s,%s",
102 busname, devname, drvargs);
112 rte_eal_hotplug_add(const char *busname, const char *devname,
119 ret = build_devargs(busname, devname, drvargs, &devargs);
123 ret = rte_dev_probe(devargs);
129 /* probe device at local process. */
131 local_dev_probe(const char *devargs, struct rte_device **new_dev)
133 struct rte_device *dev;
134 struct rte_devargs *da;
138 da = calloc(1, sizeof(*da));
142 ret = rte_devargs_parse(da, devargs);
146 if (da->bus->plug == NULL) {
147 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
153 ret = rte_devargs_insert(&da);
157 /* the rte_devargs will be referenced in the matching rte_device */
158 ret = da->bus->scan();
162 dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
164 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
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",
181 if (rte_devargs_remove(da) != 0) {
189 rte_dev_probe(const char *devargs)
191 struct eal_dev_mp_req req;
192 struct rte_device *dev;
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);
199 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
201 * If in secondary process, just send IPC request to
204 ret = eal_dev_hotplug_request_to_primary(&req);
207 "Failed to send hotplug request to primary\n");
212 "Failed to hotplug add device\n");
216 /* attach a shared device from primary start from here: */
218 /* primary attach the new device itself. */
219 ret = local_dev_probe(devargs, &dev);
223 "Failed to attach device on primary process\n");
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
235 /* primary send attach sync request to secondary. */
236 ret = eal_dev_hotplug_request_to_secondary(&req);
238 /* if any communication error, we need to rollback. */
241 "Failed to send hotplug add request to secondary\n");
247 * if any secondary failed to attach, we need to consider if rollback
250 if (req.result != 0) {
252 "Failed to attach device on secondary process\n");
255 /* for -EEXIST, we don't need to rollback. */
264 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
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");
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");
282 rte_eal_hotplug_remove(const char *busname, const char *devname)
284 struct rte_device *dev;
287 bus = rte_bus_find_by_name(busname);
289 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
293 dev = bus->find_device(NULL, cmp_dev_name, devname);
295 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
299 return rte_dev_remove(dev);
302 /* remove device at local process. */
304 local_dev_remove(struct rte_device *dev)
308 if (dev->bus->unplug == NULL) {
309 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
314 ret = dev->bus->unplug(dev);
316 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
325 rte_dev_remove(struct rte_device *dev)
327 struct eal_dev_mp_req req;
331 if (!rte_dev_is_probed(dev)) {
332 RTE_LOG(ERR, EAL, "Device is not probed\n");
336 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
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);
345 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
347 * If in secondary process, just send IPC request to
350 ret = eal_dev_hotplug_request_to_primary(&req);
353 "Failed to send hotplug request to primary\n");
358 "Failed to hotplug remove device\n");
362 /* detach a device from primary start from here: */
364 /* primary send detach sync request to secondary */
365 ret = eal_dev_hotplug_request_to_secondary(&req);
368 * if communication error, we need to rollback, because it is possible
369 * part of the secondary processes still detached it successfully.
373 "Failed to send device detach request to secondary\n");
379 * if any secondary failed to detach, we need to consider if rollback
382 if (req.result != 0) {
384 "Failed to detach device on secondary process\n");
387 * if -ENOENT, we don't need to rollback, since devices is
388 * already detached on secondary process.
394 /* primary detach the device itself. */
395 ret = local_dev_remove(dev);
397 /* if primary failed, still need to consider if rollback is necessary */
400 "Failed to detach device on primary process\n");
401 /* if -ENOENT, we don't need to rollback */
410 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
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");
421 int __rte_experimental
422 rte_dev_event_callback_register(const char *device_name,
423 rte_dev_event_cb_fn cb_fn,
426 struct dev_event_callback *event_cb;
432 rte_spinlock_lock(&dev_event_lock);
434 if (TAILQ_EMPTY(&dev_event_cbs))
435 TAILQ_INIT(&dev_event_cbs);
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)
441 if (device_name == NULL || event_cb->dev_name == NULL)
443 if (!strcmp(event_cb->dev_name, device_name))
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;
456 event_cb->dev_name = NULL;
458 event_cb->dev_name = strdup(device_name);
459 if (event_cb->dev_name == NULL) {
464 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
467 "Failed to allocate memory for device "
474 "The callback is already exist, no need "
475 "to register again.\n");
479 rte_spinlock_unlock(&dev_event_lock);
483 rte_spinlock_unlock(&dev_event_lock);
487 int __rte_experimental
488 rte_dev_event_callback_unregister(const char *device_name,
489 rte_dev_event_cb_fn cb_fn,
493 struct dev_event_callback *event_cb, *next;
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;
503 next = TAILQ_NEXT(event_cb, next);
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))
512 } else if (device_name != NULL) {
517 * if this callback is not executing right now,
520 if (event_cb->active == 0) {
521 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
528 rte_spinlock_unlock(&dev_event_lock);
532 void __rte_experimental
533 rte_dev_event_callback_process(const char *device_name,
534 enum rte_dev_event_type event)
536 struct dev_event_callback *cb_lst;
538 if (device_name == NULL)
541 rte_spinlock_lock(&dev_event_lock);
543 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
544 if (cb_lst->dev_name) {
545 if (strcmp(cb_lst->dev_name, device_name))
549 rte_spinlock_unlock(&dev_event_lock);
550 cb_lst->cb_fn(device_name, event,
552 rte_spinlock_lock(&dev_event_lock);
555 rte_spinlock_unlock(&dev_event_lock);
560 rte_dev_iterator_init(struct rte_dev_iterator *it,
563 struct rte_devargs devargs;
564 struct rte_class *cls = NULL;
565 struct rte_bus *bus = NULL;
567 /* Having both bus_str and cls_str NULL is illegal,
568 * marking this iterator as invalid unless
569 * everything goes well.
574 devargs.data = dev_str;
575 if (rte_devargs_layers_parse(&devargs, dev_str))
580 /* The string should have at least
581 * one layer specified.
583 if (bus == NULL && cls == NULL) {
585 "Either bus or class must be specified.\n");
589 if (bus != NULL && bus->dev_iterate == NULL) {
590 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
594 if (cls != NULL && cls->dev_iterate == NULL) {
595 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
599 it->bus_str = devargs.bus_str;
600 it->cls_str = devargs.cls_str;
601 it->dev_str = dev_str;
605 it->class_device = NULL;
611 dev_str_sane_copy(const char *str)
616 end = strcspn(str, ",/");
617 if (str[end] == ',') {
618 copy = strdup(&str[end + 1]);
628 slash = strchr(copy, '/');
636 class_next_dev_cmp(const struct rte_class *cls,
639 struct rte_dev_iterator *it;
640 const char *cls_str = NULL;
643 if (cls->dev_iterate == NULL)
646 cls_str = CLSCTX(ctx);
647 dev = it->class_device;
648 /* it->cls_str != NULL means a class
649 * was specified in the devstr.
651 if (it->cls_str != NULL && cls != it->cls)
653 /* If an error occurred previously,
654 * no need to test further.
658 dev = cls->dev_iterate(dev, cls_str, it);
659 it->class_device = dev;
664 bus_next_dev_cmp(const struct rte_bus *bus,
667 struct rte_device *dev = NULL;
668 struct rte_class *cls = NULL;
669 struct rte_dev_iterator *it;
670 const char *bus_str = NULL;
672 if (bus->dev_iterate == NULL)
675 bus_str = BUSCTX(ctx);
677 /* it->bus_str != NULL means a bus
678 * was specified in the devstr.
680 if (it->bus_str != NULL && bus != it->bus)
682 /* If an error occurred previously,
683 * no need to test further.
687 if (it->cls_str == NULL) {
688 dev = bus->dev_iterate(dev, bus_str, it);
691 /* cls_str != NULL */
694 dev = bus->dev_iterate(dev, bus_str, it);
700 cls = TAILQ_PREV(it->cls, rte_class_list, next);
701 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
706 goto next_dev_on_bus;
713 rte_dev_iterator_next(struct rte_dev_iterator *it)
715 struct rte_bus *bus = NULL;
716 int old_errno = rte_errno;
717 char *bus_str = NULL;
718 char *cls_str = NULL;
721 if (it->bus_str == NULL && it->cls_str == NULL) {
722 /* Invalid iterator. */
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);
733 if (it->cls_str != NULL) {
734 cls_str = dev_str_sane_copy(it->cls_str);
738 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
739 CTX(it, bus_str, cls_str)))) {
740 if (it->device != NULL) {
744 if (it->bus_str != NULL ||
749 rte_errno = old_errno;