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);
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",
169 /* Since there is a matching device, it is now its responsibility
170 * to manage the devargs we've just inserted. From this point
171 * those devargs shouldn't be removed manually anymore.
174 ret = dev->bus->plug(dev);
178 if (ret && !rte_dev_is_probed(dev)) { /* if hasn't ever succeeded */
179 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
188 if (rte_devargs_remove(da) != 0) {
196 rte_dev_probe(const char *devargs)
198 struct eal_dev_mp_req req;
199 struct rte_device *dev;
202 memset(&req, 0, sizeof(req));
203 req.t = EAL_DEV_REQ_TYPE_ATTACH;
204 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
206 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
208 * If in secondary process, just send IPC request to
211 ret = eal_dev_hotplug_request_to_primary(&req);
214 "Failed to send hotplug request to primary\n");
219 "Failed to hotplug add device\n");
223 /* attach a shared device from primary start from here: */
225 /* primary attach the new device itself. */
226 ret = local_dev_probe(devargs, &dev);
230 "Failed to attach device on primary process\n");
233 * it is possible that secondary process failed to attached a
234 * device that primary process have during initialization,
235 * so for -EEXIST case, we still need to sync with secondary
242 /* primary send attach sync request to secondary. */
243 ret = eal_dev_hotplug_request_to_secondary(&req);
245 /* if any communication error, we need to rollback. */
248 "Failed to send hotplug add request to secondary\n");
254 * if any secondary failed to attach, we need to consider if rollback
257 if (req.result != 0) {
259 "Failed to attach device on secondary process\n");
262 /* for -EEXIST, we don't need to rollback. */
271 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
273 /* primary send rollback request to secondary. */
274 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
275 RTE_LOG(WARNING, EAL,
276 "Failed to rollback device attach on secondary."
277 "Devices in secondary may not sync with primary\n");
279 /* primary rollback itself. */
280 if (local_dev_remove(dev) != 0)
281 RTE_LOG(WARNING, EAL,
282 "Failed to rollback device attach on primary."
283 "Devices in secondary may not sync with primary\n");
289 rte_eal_hotplug_remove(const char *busname, const char *devname)
291 struct rte_device *dev;
294 bus = rte_bus_find_by_name(busname);
296 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
300 dev = bus->find_device(NULL, cmp_dev_name, devname);
302 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
306 return rte_dev_remove(dev);
309 /* remove device at local process. */
311 local_dev_remove(struct rte_device *dev)
315 if (dev->bus->unplug == NULL) {
316 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
321 ret = dev->bus->unplug(dev);
323 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
325 return (ret < 0) ? ret : -ENOENT;
332 rte_dev_remove(struct rte_device *dev)
334 struct eal_dev_mp_req req;
338 if (!rte_dev_is_probed(dev)) {
339 RTE_LOG(ERR, EAL, "Device is not probed\n");
343 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
347 memset(&req, 0, sizeof(req));
348 req.t = EAL_DEV_REQ_TYPE_DETACH;
349 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
352 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
354 * If in secondary process, just send IPC request to
357 ret = eal_dev_hotplug_request_to_primary(&req);
360 "Failed to send hotplug request to primary\n");
365 "Failed to hotplug remove device\n");
369 /* detach a device from primary start from here: */
371 /* primary send detach sync request to secondary */
372 ret = eal_dev_hotplug_request_to_secondary(&req);
375 * if communication error, we need to rollback, because it is possible
376 * part of the secondary processes still detached it successfully.
380 "Failed to send device detach request to secondary\n");
386 * if any secondary failed to detach, we need to consider if rollback
389 if (req.result != 0) {
391 "Failed to detach device on secondary process\n");
394 * if -ENOENT, we don't need to rollback, since devices is
395 * already detached on secondary process.
401 /* primary detach the device itself. */
402 ret = local_dev_remove(dev);
404 /* if primary failed, still need to consider if rollback is necessary */
407 "Failed to detach device on primary process\n");
408 /* if -ENOENT, we don't need to rollback */
417 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
419 /* primary send rollback request to secondary. */
420 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
421 RTE_LOG(WARNING, EAL,
422 "Failed to rollback device detach on secondary."
423 "Devices in secondary may not sync with primary\n");
429 rte_dev_event_callback_register(const char *device_name,
430 rte_dev_event_cb_fn cb_fn,
433 struct dev_event_callback *event_cb;
439 rte_spinlock_lock(&dev_event_lock);
441 if (TAILQ_EMPTY(&dev_event_cbs))
442 TAILQ_INIT(&dev_event_cbs);
444 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
445 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
446 if (device_name == NULL && event_cb->dev_name == NULL)
448 if (device_name == NULL || event_cb->dev_name == NULL)
450 if (!strcmp(event_cb->dev_name, device_name))
455 /* create a new callback. */
456 if (event_cb == NULL) {
457 event_cb = malloc(sizeof(struct dev_event_callback));
458 if (event_cb != NULL) {
459 event_cb->cb_fn = cb_fn;
460 event_cb->cb_arg = cb_arg;
461 event_cb->active = 0;
463 event_cb->dev_name = NULL;
465 event_cb->dev_name = strdup(device_name);
466 if (event_cb->dev_name == NULL) {
471 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
474 "Failed to allocate memory for device "
481 "The callback is already exist, no need "
482 "to register again.\n");
486 rte_spinlock_unlock(&dev_event_lock);
490 rte_spinlock_unlock(&dev_event_lock);
495 rte_dev_event_callback_unregister(const char *device_name,
496 rte_dev_event_cb_fn cb_fn,
500 struct dev_event_callback *event_cb, *next;
505 rte_spinlock_lock(&dev_event_lock);
506 /*walk through the callbacks and remove all that match. */
507 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
510 next = TAILQ_NEXT(event_cb, next);
512 if (device_name != NULL && event_cb->dev_name != NULL) {
513 if (!strcmp(event_cb->dev_name, device_name)) {
514 if (event_cb->cb_fn != cb_fn ||
515 (cb_arg != (void *)-1 &&
516 event_cb->cb_arg != cb_arg))
519 } else if (device_name != NULL) {
524 * if this callback is not executing right now,
527 if (event_cb->active == 0) {
528 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
535 rte_spinlock_unlock(&dev_event_lock);
540 rte_dev_event_callback_process(const char *device_name,
541 enum rte_dev_event_type event)
543 struct dev_event_callback *cb_lst;
545 if (device_name == NULL)
548 rte_spinlock_lock(&dev_event_lock);
550 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
551 if (cb_lst->dev_name) {
552 if (strcmp(cb_lst->dev_name, device_name))
556 rte_spinlock_unlock(&dev_event_lock);
557 cb_lst->cb_fn(device_name, event,
559 rte_spinlock_lock(&dev_event_lock);
562 rte_spinlock_unlock(&dev_event_lock);
566 rte_dev_iterator_init(struct rte_dev_iterator *it,
569 struct rte_devargs devargs;
570 struct rte_class *cls = NULL;
571 struct rte_bus *bus = NULL;
573 /* Having both bus_str and cls_str NULL is illegal,
574 * marking this iterator as invalid unless
575 * everything goes well.
580 devargs.data = dev_str;
581 if (rte_devargs_layers_parse(&devargs, dev_str))
586 /* The string should have at least
587 * one layer specified.
589 if (bus == NULL && cls == NULL) {
591 "Either bus or class must be specified.\n");
595 if (bus != NULL && bus->dev_iterate == NULL) {
596 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
600 if (cls != NULL && cls->dev_iterate == NULL) {
601 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
605 it->bus_str = devargs.bus_str;
606 it->cls_str = devargs.cls_str;
607 it->dev_str = dev_str;
611 it->class_device = NULL;
617 dev_str_sane_copy(const char *str)
622 end = strcspn(str, ",/");
623 if (str[end] == ',') {
624 copy = strdup(&str[end + 1]);
634 slash = strchr(copy, '/');
642 class_next_dev_cmp(const struct rte_class *cls,
645 struct rte_dev_iterator *it;
646 const char *cls_str = NULL;
649 if (cls->dev_iterate == NULL)
652 cls_str = CLSCTX(ctx);
653 dev = it->class_device;
654 /* it->cls_str != NULL means a class
655 * was specified in the devstr.
657 if (it->cls_str != NULL && cls != it->cls)
659 /* If an error occurred previously,
660 * no need to test further.
664 dev = cls->dev_iterate(dev, cls_str, it);
665 it->class_device = dev;
670 bus_next_dev_cmp(const struct rte_bus *bus,
673 struct rte_device *dev = NULL;
674 struct rte_class *cls = NULL;
675 struct rte_dev_iterator *it;
676 const char *bus_str = NULL;
678 if (bus->dev_iterate == NULL)
681 bus_str = BUSCTX(ctx);
683 /* it->bus_str != NULL means a bus
684 * was specified in the devstr.
686 if (it->bus_str != NULL && bus != it->bus)
688 /* If an error occurred previously,
689 * no need to test further.
693 if (it->cls_str == NULL) {
694 dev = bus->dev_iterate(dev, bus_str, it);
697 /* cls_str != NULL */
700 dev = bus->dev_iterate(dev, bus_str, it);
706 cls = TAILQ_PREV(it->cls, rte_class_list, next);
707 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
712 goto next_dev_on_bus;
718 rte_dev_iterator_next(struct rte_dev_iterator *it)
720 struct rte_bus *bus = NULL;
721 int old_errno = rte_errno;
722 char *bus_str = NULL;
723 char *cls_str = NULL;
726 if (it->bus_str == NULL && it->cls_str == NULL) {
727 /* Invalid iterator. */
732 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
733 if (it->bus_str != NULL) {
734 bus_str = dev_str_sane_copy(it->bus_str);
738 if (it->cls_str != NULL) {
739 cls_str = dev_str_sane_copy(it->cls_str);
743 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
744 CTX(it, bus_str, cls_str)))) {
745 if (it->device != NULL) {
749 if (it->bus_str != NULL ||
754 rte_errno = old_errno;
762 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
765 if (dev->bus->dma_map == NULL || len == 0) {
769 /* Memory must be registered through rte_extmem_* APIs */
770 if (rte_mem_virt2memseg_list(addr) == NULL) {
775 return dev->bus->dma_map(dev, addr, iova, len);
779 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
782 if (dev->bus->dma_unmap == NULL || len == 0) {
786 /* Memory must be registered through rte_extmem_* APIs */
787 if (rte_mem_virt2memseg_list(addr) == NULL) {
792 return dev->bus->dma_unmap(dev, addr, iova, len);