1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
11 #include <rte_class.h>
13 #include <rte_devargs.h>
14 #include <rte_errno.h>
16 #include <rte_spinlock.h>
17 #include <rte_string_fns.h>
19 #include "eal_private.h"
20 #include "hotplug_mp.h"
23 * The device event callback description.
25 * It contains callback address to be registered by user application,
26 * the pointer to the parameters for callback, and the device name.
28 struct dev_event_callback {
29 TAILQ_ENTRY(dev_event_callback) next; /**< Callbacks list */
30 rte_dev_event_cb_fn cb_fn; /**< Callback address */
31 void *cb_arg; /**< Callback parameter */
32 char *dev_name; /**< Callback device name, NULL is for all device */
33 uint32_t active; /**< Callback is executing */
36 /** @internal Structure to keep track of registered callbacks */
37 TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
39 /* The device event callback list for all registered callbacks. */
40 static struct dev_event_cb_list dev_event_cbs;
42 /* spinlock for device callbacks */
43 static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
46 struct rte_dev_iterator *it;
51 #define CTX(it, bus_str, cls_str) \
52 (&(const struct dev_next_ctx){ \
59 (((struct dev_next_ctx *)(intptr_t)ptr)->it)
62 (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
65 (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
67 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
69 const char *name = _name;
71 return strcmp(dev->name, name);
75 rte_dev_is_probed(const struct rte_device *dev)
77 /* The field driver should be set only when the probe is successful. */
78 return dev->driver != NULL;
81 /* helper function to build devargs, caller should free the memory */
83 build_devargs(const char *busname, const char *devname,
84 const char *drvargs, char **devargs)
88 length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
92 *devargs = malloc(length + 1);
96 length = snprintf(*devargs, length + 1, "%s:%s,%s",
97 busname, devname, drvargs);
107 rte_eal_hotplug_add(const char *busname, const char *devname,
114 ret = build_devargs(busname, devname, drvargs, &devargs);
118 ret = rte_dev_probe(devargs);
124 /* probe device at local process. */
126 local_dev_probe(const char *devargs, struct rte_device **new_dev)
128 struct rte_device *dev;
129 struct rte_devargs *da;
133 da = calloc(1, sizeof(*da));
137 ret = rte_devargs_parse(da, devargs);
141 if (da->bus->plug == NULL) {
142 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
148 ret = rte_devargs_insert(&da);
152 /* the rte_devargs will be referenced in the matching rte_device */
153 ret = da->bus->scan();
157 dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
159 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
164 /* Since there is a matching device, it is now its responsibility
165 * to manage the devargs we've just inserted. From this point
166 * those devargs shouldn't be removed manually anymore.
169 ret = dev->bus->plug(dev);
173 if (ret && !rte_dev_is_probed(dev)) { /* if hasn't ever succeeded */
174 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
183 if (rte_devargs_remove(da) != 0)
184 rte_devargs_reset(da);
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",
318 return (ret < 0) ? ret : -ENOENT;
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");
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");
481 rte_spinlock_unlock(&dev_event_lock);
485 rte_spinlock_unlock(&dev_event_lock);
490 rte_dev_event_callback_unregister(const char *device_name,
491 rte_dev_event_cb_fn cb_fn,
495 struct dev_event_callback *event_cb, *next;
500 rte_spinlock_lock(&dev_event_lock);
501 /*walk through the callbacks and remove all that match. */
502 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
505 next = TAILQ_NEXT(event_cb, next);
507 if (device_name != NULL && event_cb->dev_name != NULL) {
508 if (!strcmp(event_cb->dev_name, device_name)) {
509 if (event_cb->cb_fn != cb_fn ||
510 (cb_arg != (void *)-1 &&
511 event_cb->cb_arg != cb_arg))
514 } else if (device_name != NULL) {
519 * if this callback is not executing right now,
522 if (event_cb->active == 0) {
523 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
524 free(event_cb->dev_name);
533 /* this callback is not be registered */
537 rte_spinlock_unlock(&dev_event_lock);
542 rte_dev_event_callback_process(const char *device_name,
543 enum rte_dev_event_type event)
545 struct dev_event_callback *cb_lst;
547 if (device_name == NULL)
550 rte_spinlock_lock(&dev_event_lock);
552 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
553 if (cb_lst->dev_name) {
554 if (strcmp(cb_lst->dev_name, device_name))
558 rte_spinlock_unlock(&dev_event_lock);
559 cb_lst->cb_fn(device_name, event,
561 rte_spinlock_lock(&dev_event_lock);
564 rte_spinlock_unlock(&dev_event_lock);
568 rte_dev_iterator_init(struct rte_dev_iterator *it,
571 struct rte_devargs devargs = { .bus = NULL };
572 struct rte_class *cls = NULL;
573 struct rte_bus *bus = NULL;
575 /* Having both bus_str and cls_str NULL is illegal,
576 * marking this iterator as invalid unless
577 * everything goes well.
582 /* Setting data field implies no malloc in parsing. */
583 devargs.data = (void *)(intptr_t)dev_str;
584 if (rte_devargs_layers_parse(&devargs, dev_str))
589 /* The string should have at least
590 * one layer specified.
592 if (bus == NULL && cls == NULL) {
594 "Either bus or class must be specified.\n");
598 if (bus != NULL && bus->dev_iterate == NULL) {
599 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
603 if (cls != NULL && cls->dev_iterate == NULL) {
604 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
608 it->bus_str = devargs.bus_str;
609 it->cls_str = devargs.cls_str;
610 it->dev_str = dev_str;
614 it->class_device = NULL;
620 dev_str_sane_copy(const char *str)
625 end = strcspn(str, ",/");
626 if (str[end] == ',') {
627 copy = strdup(&str[end + 1]);
637 slash = strchr(copy, '/');
645 class_next_dev_cmp(const struct rte_class *cls,
648 struct rte_dev_iterator *it;
649 const char *cls_str = NULL;
652 if (cls->dev_iterate == NULL)
655 cls_str = CLSCTX(ctx);
656 dev = it->class_device;
657 /* it->cls_str != NULL means a class
658 * was specified in the devstr.
660 if (it->cls_str != NULL && cls != it->cls)
662 /* If an error occurred previously,
663 * no need to test further.
667 dev = cls->dev_iterate(dev, cls_str, it);
668 it->class_device = dev;
673 bus_next_dev_cmp(const struct rte_bus *bus,
676 struct rte_device *dev = NULL;
677 struct rte_class *cls = NULL;
678 struct rte_dev_iterator *it;
679 const char *bus_str = NULL;
681 if (bus->dev_iterate == NULL)
684 bus_str = BUSCTX(ctx);
686 /* it->bus_str != NULL means a bus
687 * was specified in the devstr.
689 if (it->bus_str != NULL && bus != it->bus)
691 /* If an error occurred previously,
692 * no need to test further.
696 if (it->cls_str == NULL) {
697 dev = bus->dev_iterate(dev, bus_str, it);
700 /* cls_str != NULL */
703 dev = bus->dev_iterate(dev, bus_str, it);
709 cls = TAILQ_PREV(it->cls, rte_class_list, next);
710 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
715 goto next_dev_on_bus;
721 rte_dev_iterator_next(struct rte_dev_iterator *it)
723 struct rte_bus *bus = NULL;
724 int old_errno = rte_errno;
725 char *bus_str = NULL;
726 char *cls_str = NULL;
729 if (it->bus_str == NULL && it->cls_str == NULL) {
730 /* Invalid iterator. */
735 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
736 if (it->bus_str != NULL) {
737 bus_str = dev_str_sane_copy(it->bus_str);
741 if (it->cls_str != NULL) {
742 cls_str = dev_str_sane_copy(it->cls_str);
746 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
747 CTX(it, bus_str, cls_str)))) {
748 if (it->device != NULL) {
752 if (it->bus_str != NULL ||
757 rte_errno = old_errno;
765 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
768 if (dev->bus->dma_map == NULL || len == 0) {
772 /* Memory must be registered through rte_extmem_* APIs */
773 if (rte_mem_virt2memseg_list(addr) == NULL) {
778 return dev->bus->dma_map(dev, addr, iova, len);
782 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
785 if (dev->bus->dma_unmap == NULL || len == 0) {
789 /* Memory must be registered through rte_extmem_* APIs */
790 if (rte_mem_virt2memseg_list(addr) == NULL) {
795 return dev->bus->dma_unmap(dev, addr, iova, len);