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)
189 rte_devargs_reset(da);
194 rte_dev_probe(const char *devargs)
196 struct eal_dev_mp_req req;
197 struct rte_device *dev;
200 memset(&req, 0, sizeof(req));
201 req.t = EAL_DEV_REQ_TYPE_ATTACH;
202 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
204 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
206 * If in secondary process, just send IPC request to
209 ret = eal_dev_hotplug_request_to_primary(&req);
212 "Failed to send hotplug request to primary\n");
217 "Failed to hotplug add device\n");
221 /* attach a shared device from primary start from here: */
223 /* primary attach the new device itself. */
224 ret = local_dev_probe(devargs, &dev);
228 "Failed to attach device on primary process\n");
231 * it is possible that secondary process failed to attached a
232 * device that primary process have during initialization,
233 * so for -EEXIST case, we still need to sync with secondary
240 /* primary send attach sync request to secondary. */
241 ret = eal_dev_hotplug_request_to_secondary(&req);
243 /* if any communication error, we need to rollback. */
246 "Failed to send hotplug add request to secondary\n");
252 * if any secondary failed to attach, we need to consider if rollback
255 if (req.result != 0) {
257 "Failed to attach device on secondary process\n");
260 /* for -EEXIST, we don't need to rollback. */
269 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
271 /* primary send rollback request to secondary. */
272 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
273 RTE_LOG(WARNING, EAL,
274 "Failed to rollback device attach on secondary."
275 "Devices in secondary may not sync with primary\n");
277 /* primary rollback itself. */
278 if (local_dev_remove(dev) != 0)
279 RTE_LOG(WARNING, EAL,
280 "Failed to rollback device attach on primary."
281 "Devices in secondary may not sync with primary\n");
287 rte_eal_hotplug_remove(const char *busname, const char *devname)
289 struct rte_device *dev;
292 bus = rte_bus_find_by_name(busname);
294 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
298 dev = bus->find_device(NULL, cmp_dev_name, devname);
300 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
304 return rte_dev_remove(dev);
307 /* remove device at local process. */
309 local_dev_remove(struct rte_device *dev)
313 if (dev->bus->unplug == NULL) {
314 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
319 ret = dev->bus->unplug(dev);
321 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
323 return (ret < 0) ? ret : -ENOENT;
330 rte_dev_remove(struct rte_device *dev)
332 struct eal_dev_mp_req req;
336 if (!rte_dev_is_probed(dev)) {
337 RTE_LOG(ERR, EAL, "Device is not probed\n");
341 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
345 memset(&req, 0, sizeof(req));
346 req.t = EAL_DEV_REQ_TYPE_DETACH;
347 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
350 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
352 * If in secondary process, just send IPC request to
355 ret = eal_dev_hotplug_request_to_primary(&req);
358 "Failed to send hotplug request to primary\n");
363 "Failed to hotplug remove device\n");
367 /* detach a device from primary start from here: */
369 /* primary send detach sync request to secondary */
370 ret = eal_dev_hotplug_request_to_secondary(&req);
373 * if communication error, we need to rollback, because it is possible
374 * part of the secondary processes still detached it successfully.
378 "Failed to send device detach request to secondary\n");
384 * if any secondary failed to detach, we need to consider if rollback
387 if (req.result != 0) {
389 "Failed to detach device on secondary process\n");
392 * if -ENOENT, we don't need to rollback, since devices is
393 * already detached on secondary process.
399 /* primary detach the device itself. */
400 ret = local_dev_remove(dev);
402 /* if primary failed, still need to consider if rollback is necessary */
405 "Failed to detach device on primary process\n");
406 /* if -ENOENT, we don't need to rollback */
415 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
417 /* primary send rollback request to secondary. */
418 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
419 RTE_LOG(WARNING, EAL,
420 "Failed to rollback device detach on secondary."
421 "Devices in secondary may not sync with primary\n");
427 rte_dev_event_callback_register(const char *device_name,
428 rte_dev_event_cb_fn cb_fn,
431 struct dev_event_callback *event_cb;
437 rte_spinlock_lock(&dev_event_lock);
439 if (TAILQ_EMPTY(&dev_event_cbs))
440 TAILQ_INIT(&dev_event_cbs);
442 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
443 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
444 if (device_name == NULL && event_cb->dev_name == NULL)
446 if (device_name == NULL || event_cb->dev_name == NULL)
448 if (!strcmp(event_cb->dev_name, device_name))
453 /* create a new callback. */
454 if (event_cb == NULL) {
455 event_cb = malloc(sizeof(struct dev_event_callback));
456 if (event_cb != NULL) {
457 event_cb->cb_fn = cb_fn;
458 event_cb->cb_arg = cb_arg;
459 event_cb->active = 0;
461 event_cb->dev_name = NULL;
463 event_cb->dev_name = strdup(device_name);
464 if (event_cb->dev_name == NULL) {
469 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
472 "Failed to allocate memory for device "
479 "The callback is already exist, no need "
480 "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);
529 free(event_cb->dev_name);
538 /* this callback is not be registered */
542 rte_spinlock_unlock(&dev_event_lock);
547 rte_dev_event_callback_process(const char *device_name,
548 enum rte_dev_event_type event)
550 struct dev_event_callback *cb_lst;
552 if (device_name == NULL)
555 rte_spinlock_lock(&dev_event_lock);
557 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
558 if (cb_lst->dev_name) {
559 if (strcmp(cb_lst->dev_name, device_name))
563 rte_spinlock_unlock(&dev_event_lock);
564 cb_lst->cb_fn(device_name, event,
566 rte_spinlock_lock(&dev_event_lock);
569 rte_spinlock_unlock(&dev_event_lock);
573 rte_dev_iterator_init(struct rte_dev_iterator *it,
576 struct rte_devargs devargs = { .bus = NULL };
577 struct rte_class *cls = NULL;
578 struct rte_bus *bus = NULL;
580 /* Having both bus_str and cls_str NULL is illegal,
581 * marking this iterator as invalid unless
582 * everything goes well.
587 /* Setting data field implies no malloc in parsing. */
588 devargs.data = (void *)(intptr_t)dev_str;
589 if (rte_devargs_layers_parse(&devargs, dev_str))
594 /* The string should have at least
595 * one layer specified.
597 if (bus == NULL && cls == NULL) {
599 "Either bus or class must be specified.\n");
603 if (bus != NULL && bus->dev_iterate == NULL) {
604 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
608 if (cls != NULL && cls->dev_iterate == NULL) {
609 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
613 it->bus_str = devargs.bus_str;
614 it->cls_str = devargs.cls_str;
615 it->dev_str = dev_str;
619 it->class_device = NULL;
625 dev_str_sane_copy(const char *str)
630 end = strcspn(str, ",/");
631 if (str[end] == ',') {
632 copy = strdup(&str[end + 1]);
642 slash = strchr(copy, '/');
650 class_next_dev_cmp(const struct rte_class *cls,
653 struct rte_dev_iterator *it;
654 const char *cls_str = NULL;
657 if (cls->dev_iterate == NULL)
660 cls_str = CLSCTX(ctx);
661 dev = it->class_device;
662 /* it->cls_str != NULL means a class
663 * was specified in the devstr.
665 if (it->cls_str != NULL && cls != it->cls)
667 /* If an error occurred previously,
668 * no need to test further.
672 dev = cls->dev_iterate(dev, cls_str, it);
673 it->class_device = dev;
678 bus_next_dev_cmp(const struct rte_bus *bus,
681 struct rte_device *dev = NULL;
682 struct rte_class *cls = NULL;
683 struct rte_dev_iterator *it;
684 const char *bus_str = NULL;
686 if (bus->dev_iterate == NULL)
689 bus_str = BUSCTX(ctx);
691 /* it->bus_str != NULL means a bus
692 * was specified in the devstr.
694 if (it->bus_str != NULL && bus != it->bus)
696 /* If an error occurred previously,
697 * no need to test further.
701 if (it->cls_str == NULL) {
702 dev = bus->dev_iterate(dev, bus_str, it);
705 /* cls_str != NULL */
708 dev = bus->dev_iterate(dev, bus_str, it);
714 cls = TAILQ_PREV(it->cls, rte_class_list, next);
715 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
720 goto next_dev_on_bus;
726 rte_dev_iterator_next(struct rte_dev_iterator *it)
728 struct rte_bus *bus = NULL;
729 int old_errno = rte_errno;
730 char *bus_str = NULL;
731 char *cls_str = NULL;
734 if (it->bus_str == NULL && it->cls_str == NULL) {
735 /* Invalid iterator. */
740 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
741 if (it->bus_str != NULL) {
742 bus_str = dev_str_sane_copy(it->bus_str);
746 if (it->cls_str != NULL) {
747 cls_str = dev_str_sane_copy(it->cls_str);
751 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
752 CTX(it, bus_str, cls_str)))) {
753 if (it->device != NULL) {
757 if (it->bus_str != NULL ||
762 rte_errno = old_errno;
770 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
773 if (dev->bus->dma_map == NULL || len == 0) {
777 /* Memory must be registered through rte_extmem_* APIs */
778 if (rte_mem_virt2memseg_list(addr) == NULL) {
783 return dev->bus->dma_map(dev, addr, iova, len);
787 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
790 if (dev->bus->dma_unmap == NULL || len == 0) {
794 /* Memory must be registered through rte_extmem_* APIs */
795 if (rte_mem_virt2memseg_list(addr) == NULL) {
800 return dev->bus->dma_unmap(dev, addr, iova, len);