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);
175 if (ret && !rte_dev_is_probed(dev)) { /* if hasn't ever succeeded */
176 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
185 if (rte_devargs_remove(da) != 0) {
193 rte_dev_probe(const char *devargs)
195 struct eal_dev_mp_req req;
196 struct rte_device *dev;
199 memset(&req, 0, sizeof(req));
200 req.t = EAL_DEV_REQ_TYPE_ATTACH;
201 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
203 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
205 * If in secondary process, just send IPC request to
208 ret = eal_dev_hotplug_request_to_primary(&req);
211 "Failed to send hotplug request to primary\n");
216 "Failed to hotplug add device\n");
220 /* attach a shared device from primary start from here: */
222 /* primary attach the new device itself. */
223 ret = local_dev_probe(devargs, &dev);
227 "Failed to attach device on primary process\n");
230 * it is possible that secondary process failed to attached a
231 * device that primary process have during initialization,
232 * so for -EEXIST case, we still need to sync with secondary
239 /* primary send attach sync request to secondary. */
240 ret = eal_dev_hotplug_request_to_secondary(&req);
242 /* if any communication error, we need to rollback. */
245 "Failed to send hotplug add request to secondary\n");
251 * if any secondary failed to attach, we need to consider if rollback
254 if (req.result != 0) {
256 "Failed to attach device on secondary process\n");
259 /* for -EEXIST, we don't need to rollback. */
268 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
270 /* primary send rollback request to secondary. */
271 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
272 RTE_LOG(WARNING, EAL,
273 "Failed to rollback device attach on secondary."
274 "Devices in secondary may not sync with primary\n");
276 /* primary rollback itself. */
277 if (local_dev_remove(dev) != 0)
278 RTE_LOG(WARNING, EAL,
279 "Failed to rollback device attach on primary."
280 "Devices in secondary may not sync with primary\n");
286 rte_eal_hotplug_remove(const char *busname, const char *devname)
288 struct rte_device *dev;
291 bus = rte_bus_find_by_name(busname);
293 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
297 dev = bus->find_device(NULL, cmp_dev_name, devname);
299 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
303 return rte_dev_remove(dev);
306 /* remove device at local process. */
308 local_dev_remove(struct rte_device *dev)
312 if (dev->bus->unplug == NULL) {
313 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
318 ret = dev->bus->unplug(dev);
320 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
329 rte_dev_remove(struct rte_device *dev)
331 struct eal_dev_mp_req req;
335 if (!rte_dev_is_probed(dev)) {
336 RTE_LOG(ERR, EAL, "Device is not probed\n");
340 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
344 memset(&req, 0, sizeof(req));
345 req.t = EAL_DEV_REQ_TYPE_DETACH;
346 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
349 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
351 * If in secondary process, just send IPC request to
354 ret = eal_dev_hotplug_request_to_primary(&req);
357 "Failed to send hotplug request to primary\n");
362 "Failed to hotplug remove device\n");
366 /* detach a device from primary start from here: */
368 /* primary send detach sync request to secondary */
369 ret = eal_dev_hotplug_request_to_secondary(&req);
372 * if communication error, we need to rollback, because it is possible
373 * part of the secondary processes still detached it successfully.
377 "Failed to send device detach request to secondary\n");
383 * if any secondary failed to detach, we need to consider if rollback
386 if (req.result != 0) {
388 "Failed to detach device on secondary process\n");
391 * if -ENOENT, we don't need to rollback, since devices is
392 * already detached on secondary process.
398 /* primary detach the device itself. */
399 ret = local_dev_remove(dev);
401 /* if primary failed, still need to consider if rollback is necessary */
404 "Failed to detach device on primary process\n");
405 /* if -ENOENT, we don't need to rollback */
414 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
416 /* primary send rollback request to secondary. */
417 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
418 RTE_LOG(WARNING, EAL,
419 "Failed to rollback device detach on secondary."
420 "Devices in secondary may not sync with primary\n");
426 rte_dev_event_callback_register(const char *device_name,
427 rte_dev_event_cb_fn cb_fn,
430 struct dev_event_callback *event_cb;
436 rte_spinlock_lock(&dev_event_lock);
438 if (TAILQ_EMPTY(&dev_event_cbs))
439 TAILQ_INIT(&dev_event_cbs);
441 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
442 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
443 if (device_name == NULL && event_cb->dev_name == NULL)
445 if (device_name == NULL || event_cb->dev_name == NULL)
447 if (!strcmp(event_cb->dev_name, device_name))
452 /* create a new callback. */
453 if (event_cb == NULL) {
454 event_cb = malloc(sizeof(struct dev_event_callback));
455 if (event_cb != NULL) {
456 event_cb->cb_fn = cb_fn;
457 event_cb->cb_arg = cb_arg;
458 event_cb->active = 0;
460 event_cb->dev_name = NULL;
462 event_cb->dev_name = strdup(device_name);
463 if (event_cb->dev_name == NULL) {
468 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
471 "Failed to allocate memory for device "
478 "The callback is already exist, no need "
479 "to register again.\n");
483 rte_spinlock_unlock(&dev_event_lock);
487 rte_spinlock_unlock(&dev_event_lock);
492 rte_dev_event_callback_unregister(const char *device_name,
493 rte_dev_event_cb_fn cb_fn,
497 struct dev_event_callback *event_cb, *next;
502 rte_spinlock_lock(&dev_event_lock);
503 /*walk through the callbacks and remove all that match. */
504 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
507 next = TAILQ_NEXT(event_cb, next);
509 if (device_name != NULL && event_cb->dev_name != NULL) {
510 if (!strcmp(event_cb->dev_name, device_name)) {
511 if (event_cb->cb_fn != cb_fn ||
512 (cb_arg != (void *)-1 &&
513 event_cb->cb_arg != cb_arg))
516 } else if (device_name != NULL) {
521 * if this callback is not executing right now,
524 if (event_cb->active == 0) {
525 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
532 rte_spinlock_unlock(&dev_event_lock);
537 rte_dev_event_callback_process(const char *device_name,
538 enum rte_dev_event_type event)
540 struct dev_event_callback *cb_lst;
542 if (device_name == NULL)
545 rte_spinlock_lock(&dev_event_lock);
547 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
548 if (cb_lst->dev_name) {
549 if (strcmp(cb_lst->dev_name, device_name))
553 rte_spinlock_unlock(&dev_event_lock);
554 cb_lst->cb_fn(device_name, event,
556 rte_spinlock_lock(&dev_event_lock);
559 rte_spinlock_unlock(&dev_event_lock);
563 rte_dev_iterator_init(struct rte_dev_iterator *it,
566 struct rte_devargs devargs;
567 struct rte_class *cls = NULL;
568 struct rte_bus *bus = NULL;
570 /* Having both bus_str and cls_str NULL is illegal,
571 * marking this iterator as invalid unless
572 * everything goes well.
577 devargs.data = dev_str;
578 if (rte_devargs_layers_parse(&devargs, dev_str))
583 /* The string should have at least
584 * one layer specified.
586 if (bus == NULL && cls == NULL) {
588 "Either bus or class must be specified.\n");
592 if (bus != NULL && bus->dev_iterate == NULL) {
593 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
597 if (cls != NULL && cls->dev_iterate == NULL) {
598 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
602 it->bus_str = devargs.bus_str;
603 it->cls_str = devargs.cls_str;
604 it->dev_str = dev_str;
608 it->class_device = NULL;
614 dev_str_sane_copy(const char *str)
619 end = strcspn(str, ",/");
620 if (str[end] == ',') {
621 copy = strdup(&str[end + 1]);
631 slash = strchr(copy, '/');
639 class_next_dev_cmp(const struct rte_class *cls,
642 struct rte_dev_iterator *it;
643 const char *cls_str = NULL;
646 if (cls->dev_iterate == NULL)
649 cls_str = CLSCTX(ctx);
650 dev = it->class_device;
651 /* it->cls_str != NULL means a class
652 * was specified in the devstr.
654 if (it->cls_str != NULL && cls != it->cls)
656 /* If an error occurred previously,
657 * no need to test further.
661 dev = cls->dev_iterate(dev, cls_str, it);
662 it->class_device = dev;
667 bus_next_dev_cmp(const struct rte_bus *bus,
670 struct rte_device *dev = NULL;
671 struct rte_class *cls = NULL;
672 struct rte_dev_iterator *it;
673 const char *bus_str = NULL;
675 if (bus->dev_iterate == NULL)
678 bus_str = BUSCTX(ctx);
680 /* it->bus_str != NULL means a bus
681 * was specified in the devstr.
683 if (it->bus_str != NULL && bus != it->bus)
685 /* If an error occurred previously,
686 * no need to test further.
690 if (it->cls_str == NULL) {
691 dev = bus->dev_iterate(dev, bus_str, it);
694 /* cls_str != NULL */
697 dev = bus->dev_iterate(dev, bus_str, it);
703 cls = TAILQ_PREV(it->cls, rte_class_list, next);
704 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
709 goto next_dev_on_bus;
715 rte_dev_iterator_next(struct rte_dev_iterator *it)
717 struct rte_bus *bus = NULL;
718 int old_errno = rte_errno;
719 char *bus_str = NULL;
720 char *cls_str = NULL;
723 if (it->bus_str == NULL && it->cls_str == NULL) {
724 /* Invalid iterator. */
729 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
730 if (it->bus_str != NULL) {
731 bus_str = dev_str_sane_copy(it->bus_str);
735 if (it->cls_str != NULL) {
736 cls_str = dev_str_sane_copy(it->cls_str);
740 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
741 CTX(it, bus_str, cls_str)))) {
742 if (it->device != NULL) {
746 if (it->bus_str != NULL ||
751 rte_errno = old_errno;
759 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
762 if (dev->bus->dma_map == NULL || len == 0) {
766 /* Memory must be registered through rte_extmem_* APIs */
767 if (rte_mem_virt2memseg_list(addr) == NULL) {
772 return dev->bus->dma_map(dev, addr, iova, len);
776 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
779 if (dev->bus->dma_unmap == NULL || len == 0) {
783 /* Memory must be registered through rte_extmem_* APIs */
784 if (rte_mem_virt2memseg_list(addr) == NULL) {
789 return dev->bus->dma_unmap(dev, addr, iova, len);