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 ret = da->bus->scan();
161 dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
163 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
169 ret = dev->bus->plug(dev);
171 if (rte_dev_is_probed(dev)) /* if already succeeded earlier */
172 return ret; /* no rollback */
173 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
182 if (rte_devargs_remove(da) != 0) {
189 int __rte_experimental
190 rte_dev_probe(const char *devargs)
192 struct eal_dev_mp_req req;
193 struct rte_device *dev;
196 memset(&req, 0, sizeof(req));
197 req.t = EAL_DEV_REQ_TYPE_ATTACH;
198 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
200 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
202 * If in secondary process, just send IPC request to
205 ret = eal_dev_hotplug_request_to_primary(&req);
208 "Failed to send hotplug request to primary\n");
213 "Failed to hotplug add device\n");
217 /* attach a shared device from primary start from here: */
219 /* primary attach the new device itself. */
220 ret = local_dev_probe(devargs, &dev);
224 "Failed to attach device on primary process\n");
227 * it is possible that secondary process failed to attached a
228 * device that primary process have during initialization,
229 * so for -EEXIST case, we still need to sync with secondary
236 /* primary send attach sync request to secondary. */
237 ret = eal_dev_hotplug_request_to_secondary(&req);
239 /* if any communication error, we need to rollback. */
242 "Failed to send hotplug add request to secondary\n");
248 * if any secondary failed to attach, we need to consider if rollback
251 if (req.result != 0) {
253 "Failed to attach device on secondary process\n");
256 /* for -EEXIST, we don't need to rollback. */
265 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
267 /* primary send rollback request to secondary. */
268 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
269 RTE_LOG(WARNING, EAL,
270 "Failed to rollback device attach on secondary."
271 "Devices in secondary may not sync with primary\n");
273 /* primary rollback itself. */
274 if (local_dev_remove(dev) != 0)
275 RTE_LOG(WARNING, EAL,
276 "Failed to rollback device attach on primary."
277 "Devices in secondary may not sync with primary\n");
283 rte_eal_hotplug_remove(const char *busname, const char *devname)
285 struct rte_device *dev;
288 bus = rte_bus_find_by_name(busname);
290 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
294 dev = bus->find_device(NULL, cmp_dev_name, devname);
296 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
300 return rte_dev_remove(dev);
303 /* remove device at local process. */
305 local_dev_remove(struct rte_device *dev)
309 if (dev->bus->unplug == NULL) {
310 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
315 ret = dev->bus->unplug(dev);
317 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
325 int __rte_experimental
326 rte_dev_remove(struct rte_device *dev)
328 struct eal_dev_mp_req req;
332 if (!rte_dev_is_probed(dev)) {
333 RTE_LOG(ERR, EAL, "Device is not probed\n");
337 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
341 memset(&req, 0, sizeof(req));
342 req.t = EAL_DEV_REQ_TYPE_DETACH;
343 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
346 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
348 * If in secondary process, just send IPC request to
351 ret = eal_dev_hotplug_request_to_primary(&req);
354 "Failed to send hotplug request to primary\n");
359 "Failed to hotplug remove device\n");
363 /* detach a device from primary start from here: */
365 /* primary send detach sync request to secondary */
366 ret = eal_dev_hotplug_request_to_secondary(&req);
369 * if communication error, we need to rollback, because it is possible
370 * part of the secondary processes still detached it successfully.
374 "Failed to send device detach request to secondary\n");
380 * if any secondary failed to detach, we need to consider if rollback
383 if (req.result != 0) {
385 "Failed to detach device on secondary process\n");
388 * if -ENOENT, we don't need to rollback, since devices is
389 * already detached on secondary process.
395 /* primary detach the device itself. */
396 ret = local_dev_remove(dev);
398 /* if primary failed, still need to consider if rollback is necessary */
401 "Failed to detach device on primary process\n");
402 /* if -ENOENT, we don't need to rollback */
411 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
413 /* primary send rollback request to secondary. */
414 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
415 RTE_LOG(WARNING, EAL,
416 "Failed to rollback device detach on secondary."
417 "Devices in secondary may not sync with primary\n");
422 int __rte_experimental
423 rte_dev_event_callback_register(const char *device_name,
424 rte_dev_event_cb_fn cb_fn,
427 struct dev_event_callback *event_cb;
433 rte_spinlock_lock(&dev_event_lock);
435 if (TAILQ_EMPTY(&dev_event_cbs))
436 TAILQ_INIT(&dev_event_cbs);
438 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
439 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
440 if (device_name == NULL && event_cb->dev_name == NULL)
442 if (device_name == NULL || event_cb->dev_name == NULL)
444 if (!strcmp(event_cb->dev_name, device_name))
449 /* create a new callback. */
450 if (event_cb == NULL) {
451 event_cb = malloc(sizeof(struct dev_event_callback));
452 if (event_cb != NULL) {
453 event_cb->cb_fn = cb_fn;
454 event_cb->cb_arg = cb_arg;
455 event_cb->active = 0;
457 event_cb->dev_name = NULL;
459 event_cb->dev_name = strdup(device_name);
460 if (event_cb->dev_name == NULL) {
465 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
468 "Failed to allocate memory for device "
475 "The callback is already exist, no need "
476 "to register again.\n");
480 rte_spinlock_unlock(&dev_event_lock);
484 rte_spinlock_unlock(&dev_event_lock);
488 int __rte_experimental
489 rte_dev_event_callback_unregister(const char *device_name,
490 rte_dev_event_cb_fn cb_fn,
494 struct dev_event_callback *event_cb, *next;
499 rte_spinlock_lock(&dev_event_lock);
500 /*walk through the callbacks and remove all that match. */
501 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
504 next = TAILQ_NEXT(event_cb, next);
506 if (device_name != NULL && event_cb->dev_name != NULL) {
507 if (!strcmp(event_cb->dev_name, device_name)) {
508 if (event_cb->cb_fn != cb_fn ||
509 (cb_arg != (void *)-1 &&
510 event_cb->cb_arg != cb_arg))
513 } else if (device_name != NULL) {
518 * if this callback is not executing right now,
521 if (event_cb->active == 0) {
522 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
529 rte_spinlock_unlock(&dev_event_lock);
533 void __rte_experimental
534 rte_dev_event_callback_process(const char *device_name,
535 enum rte_dev_event_type event)
537 struct dev_event_callback *cb_lst;
539 if (device_name == NULL)
542 rte_spinlock_lock(&dev_event_lock);
544 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
545 if (cb_lst->dev_name) {
546 if (strcmp(cb_lst->dev_name, device_name))
550 rte_spinlock_unlock(&dev_event_lock);
551 cb_lst->cb_fn(device_name, event,
553 rte_spinlock_lock(&dev_event_lock);
556 rte_spinlock_unlock(&dev_event_lock);
561 rte_dev_iterator_init(struct rte_dev_iterator *it,
564 struct rte_devargs devargs;
565 struct rte_class *cls = NULL;
566 struct rte_bus *bus = NULL;
568 /* Having both bus_str and cls_str NULL is illegal,
569 * marking this iterator as invalid unless
570 * everything goes well.
575 devargs.data = dev_str;
576 if (rte_devargs_layers_parse(&devargs, dev_str))
581 /* The string should have at least
582 * one layer specified.
584 if (bus == NULL && cls == NULL) {
586 "Either bus or class must be specified.\n");
590 if (bus != NULL && bus->dev_iterate == NULL) {
591 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
595 if (cls != NULL && cls->dev_iterate == NULL) {
596 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
600 it->bus_str = devargs.bus_str;
601 it->cls_str = devargs.cls_str;
602 it->dev_str = dev_str;
606 it->class_device = NULL;
612 dev_str_sane_copy(const char *str)
617 end = strcspn(str, ",/");
618 if (str[end] == ',') {
619 copy = strdup(&str[end + 1]);
629 slash = strchr(copy, '/');
637 class_next_dev_cmp(const struct rte_class *cls,
640 struct rte_dev_iterator *it;
641 const char *cls_str = NULL;
644 if (cls->dev_iterate == NULL)
647 cls_str = CLSCTX(ctx);
648 dev = it->class_device;
649 /* it->cls_str != NULL means a class
650 * was specified in the devstr.
652 if (it->cls_str != NULL && cls != it->cls)
654 /* If an error occurred previously,
655 * no need to test further.
659 dev = cls->dev_iterate(dev, cls_str, it);
660 it->class_device = dev;
665 bus_next_dev_cmp(const struct rte_bus *bus,
668 struct rte_device *dev = NULL;
669 struct rte_class *cls = NULL;
670 struct rte_dev_iterator *it;
671 const char *bus_str = NULL;
673 if (bus->dev_iterate == NULL)
676 bus_str = BUSCTX(ctx);
678 /* it->bus_str != NULL means a bus
679 * was specified in the devstr.
681 if (it->bus_str != NULL && bus != it->bus)
683 /* If an error occurred previously,
684 * no need to test further.
688 if (it->cls_str == NULL) {
689 dev = bus->dev_iterate(dev, bus_str, it);
692 /* cls_str != NULL */
695 dev = bus->dev_iterate(dev, bus_str, it);
701 cls = TAILQ_PREV(it->cls, rte_class_list, next);
702 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
707 goto next_dev_on_bus;
714 rte_dev_iterator_next(struct rte_dev_iterator *it)
716 struct rte_bus *bus = NULL;
717 int old_errno = rte_errno;
718 char *bus_str = NULL;
719 char *cls_str = NULL;
722 if (it->bus_str == NULL && it->cls_str == NULL) {
723 /* Invalid iterator. */
728 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
729 if (it->bus_str != NULL) {
730 bus_str = dev_str_sane_copy(it->bus_str);
734 if (it->cls_str != NULL) {
735 cls_str = dev_str_sane_copy(it->cls_str);
739 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
740 CTX(it, bus_str, cls_str)))) {
741 if (it->device != NULL) {
745 if (it->bus_str != NULL ||
750 rte_errno = old_errno;