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);
191 rte_dev_probe(const char *devargs)
193 struct eal_dev_mp_req req;
194 struct rte_device *dev;
197 memset(&req, 0, sizeof(req));
198 req.t = EAL_DEV_REQ_TYPE_ATTACH;
199 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
201 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
203 * If in secondary process, just send IPC request to
206 ret = eal_dev_hotplug_request_to_primary(&req);
209 "Failed to send hotplug request to primary\n");
214 "Failed to hotplug add device\n");
218 /* attach a shared device from primary start from here: */
220 /* primary attach the new device itself. */
221 ret = local_dev_probe(devargs, &dev);
225 "Failed to attach device on primary process\n");
228 * it is possible that secondary process failed to attached a
229 * device that primary process have during initialization,
230 * so for -EEXIST case, we still need to sync with secondary
237 /* primary send attach sync request to secondary. */
238 ret = eal_dev_hotplug_request_to_secondary(&req);
240 /* if any communication error, we need to rollback. */
243 "Failed to send hotplug add request to secondary\n");
249 * if any secondary failed to attach, we need to consider if rollback
252 if (req.result != 0) {
254 "Failed to attach device on secondary process\n");
257 /* for -EEXIST, we don't need to rollback. */
266 req.t = EAL_DEV_REQ_TYPE_ATTACH_ROLLBACK;
268 /* primary send rollback request to secondary. */
269 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
270 RTE_LOG(WARNING, EAL,
271 "Failed to rollback device attach on secondary."
272 "Devices in secondary may not sync with primary\n");
274 /* primary rollback itself. */
275 if (local_dev_remove(dev) != 0)
276 RTE_LOG(WARNING, EAL,
277 "Failed to rollback device attach on primary."
278 "Devices in secondary may not sync with primary\n");
284 rte_eal_hotplug_remove(const char *busname, const char *devname)
286 struct rte_device *dev;
289 bus = rte_bus_find_by_name(busname);
291 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
295 dev = bus->find_device(NULL, cmp_dev_name, devname);
297 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
301 return rte_dev_remove(dev);
304 /* remove device at local process. */
306 local_dev_remove(struct rte_device *dev)
310 if (dev->bus->unplug == NULL) {
311 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
316 ret = dev->bus->unplug(dev);
318 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
320 return (ret < 0) ? ret : -ENOENT;
327 rte_dev_remove(struct rte_device *dev)
329 struct eal_dev_mp_req req;
333 if (!rte_dev_is_probed(dev)) {
334 RTE_LOG(ERR, EAL, "Device is not probed\n");
338 ret = build_devargs(dev->bus->name, dev->name, "", &devargs);
342 memset(&req, 0, sizeof(req));
343 req.t = EAL_DEV_REQ_TYPE_DETACH;
344 strlcpy(req.devargs, devargs, EAL_DEV_MP_DEV_ARGS_MAX_LEN);
347 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
349 * If in secondary process, just send IPC request to
352 ret = eal_dev_hotplug_request_to_primary(&req);
355 "Failed to send hotplug request to primary\n");
360 "Failed to hotplug remove device\n");
364 /* detach a device from primary start from here: */
366 /* primary send detach sync request to secondary */
367 ret = eal_dev_hotplug_request_to_secondary(&req);
370 * if communication error, we need to rollback, because it is possible
371 * part of the secondary processes still detached it successfully.
375 "Failed to send device detach request to secondary\n");
381 * if any secondary failed to detach, we need to consider if rollback
384 if (req.result != 0) {
386 "Failed to detach device on secondary process\n");
389 * if -ENOENT, we don't need to rollback, since devices is
390 * already detached on secondary process.
396 /* primary detach the device itself. */
397 ret = local_dev_remove(dev);
399 /* if primary failed, still need to consider if rollback is necessary */
402 "Failed to detach device on primary process\n");
403 /* if -ENOENT, we don't need to rollback */
412 req.t = EAL_DEV_REQ_TYPE_DETACH_ROLLBACK;
414 /* primary send rollback request to secondary. */
415 if (eal_dev_hotplug_request_to_secondary(&req) != 0)
416 RTE_LOG(WARNING, EAL,
417 "Failed to rollback device detach on secondary."
418 "Devices in secondary may not sync with primary\n");
424 rte_dev_event_callback_register(const char *device_name,
425 rte_dev_event_cb_fn cb_fn,
428 struct dev_event_callback *event_cb;
434 rte_spinlock_lock(&dev_event_lock);
436 if (TAILQ_EMPTY(&dev_event_cbs))
437 TAILQ_INIT(&dev_event_cbs);
439 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
440 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
441 if (device_name == NULL && event_cb->dev_name == NULL)
443 if (device_name == NULL || event_cb->dev_name == NULL)
445 if (!strcmp(event_cb->dev_name, device_name))
450 /* create a new callback. */
451 if (event_cb == NULL) {
452 event_cb = malloc(sizeof(struct dev_event_callback));
453 if (event_cb != NULL) {
454 event_cb->cb_fn = cb_fn;
455 event_cb->cb_arg = cb_arg;
456 event_cb->active = 0;
458 event_cb->dev_name = NULL;
460 event_cb->dev_name = strdup(device_name);
461 if (event_cb->dev_name == NULL) {
466 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
469 "Failed to allocate memory for device "
476 "The callback is already exist, no need "
477 "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);
526 free(event_cb->dev_name);
535 /* this callback is not be registered */
539 rte_spinlock_unlock(&dev_event_lock);
544 rte_dev_event_callback_process(const char *device_name,
545 enum rte_dev_event_type event)
547 struct dev_event_callback *cb_lst;
549 if (device_name == NULL)
552 rte_spinlock_lock(&dev_event_lock);
554 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
555 if (cb_lst->dev_name) {
556 if (strcmp(cb_lst->dev_name, device_name))
560 rte_spinlock_unlock(&dev_event_lock);
561 cb_lst->cb_fn(device_name, event,
563 rte_spinlock_lock(&dev_event_lock);
566 rte_spinlock_unlock(&dev_event_lock);
570 rte_dev_iterator_init(struct rte_dev_iterator *it,
573 struct rte_devargs devargs = { .bus = NULL };
574 struct rte_class *cls = NULL;
575 struct rte_bus *bus = NULL;
577 /* Having both bus_str and cls_str NULL is illegal,
578 * marking this iterator as invalid unless
579 * everything goes well.
584 /* Setting data field implies no malloc in parsing. */
585 devargs.data = (void *)(intptr_t)dev_str;
586 if (rte_devargs_layers_parse(&devargs, dev_str))
591 /* The string should have at least
592 * one layer specified.
594 if (bus == NULL && cls == NULL) {
596 "Either bus or class must be specified.\n");
600 if (bus != NULL && bus->dev_iterate == NULL) {
601 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
605 if (cls != NULL && cls->dev_iterate == NULL) {
606 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
610 it->bus_str = devargs.bus_str;
611 it->cls_str = devargs.cls_str;
612 it->dev_str = dev_str;
616 it->class_device = NULL;
622 dev_str_sane_copy(const char *str)
627 end = strcspn(str, ",/");
628 if (str[end] == ',') {
629 copy = strdup(&str[end + 1]);
639 slash = strchr(copy, '/');
647 class_next_dev_cmp(const struct rte_class *cls,
650 struct rte_dev_iterator *it;
651 const char *cls_str = NULL;
654 if (cls->dev_iterate == NULL)
657 cls_str = CLSCTX(ctx);
658 dev = it->class_device;
659 /* it->cls_str != NULL means a class
660 * was specified in the devstr.
662 if (it->cls_str != NULL && cls != it->cls)
664 /* If an error occurred previously,
665 * no need to test further.
669 dev = cls->dev_iterate(dev, cls_str, it);
670 it->class_device = dev;
675 bus_next_dev_cmp(const struct rte_bus *bus,
678 struct rte_device *dev = NULL;
679 struct rte_class *cls = NULL;
680 struct rte_dev_iterator *it;
681 const char *bus_str = NULL;
683 if (bus->dev_iterate == NULL)
686 bus_str = BUSCTX(ctx);
688 /* it->bus_str != NULL means a bus
689 * was specified in the devstr.
691 if (it->bus_str != NULL && bus != it->bus)
693 /* If an error occurred previously,
694 * no need to test further.
698 if (it->cls_str == NULL) {
699 dev = bus->dev_iterate(dev, bus_str, it);
702 /* cls_str != NULL */
705 dev = bus->dev_iterate(dev, bus_str, it);
711 cls = TAILQ_PREV(it->cls, rte_class_list, next);
712 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
717 goto next_dev_on_bus;
723 rte_dev_iterator_next(struct rte_dev_iterator *it)
725 struct rte_bus *bus = NULL;
726 int old_errno = rte_errno;
727 char *bus_str = NULL;
728 char *cls_str = NULL;
731 if (it->bus_str == NULL && it->cls_str == NULL) {
732 /* Invalid iterator. */
737 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
738 if (it->bus_str != NULL) {
739 bus_str = dev_str_sane_copy(it->bus_str);
743 if (it->cls_str != NULL) {
744 cls_str = dev_str_sane_copy(it->cls_str);
748 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
749 CTX(it, bus_str, cls_str)))) {
750 if (it->device != NULL) {
754 if (it->bus_str != NULL ||
759 rte_errno = old_errno;
767 rte_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
770 if (dev->bus->dma_map == NULL || len == 0) {
774 /* Memory must be registered through rte_extmem_* APIs */
775 if (rte_mem_virt2memseg_list(addr) == NULL) {
780 return dev->bus->dma_map(dev, addr, iova, len);
784 rte_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
787 if (dev->bus->dma_unmap == NULL || len == 0) {
791 /* Memory must be registered through rte_extmem_* APIs */
792 if (rte_mem_virt2memseg_list(addr) == NULL) {
797 return dev->bus->dma_unmap(dev, addr, iova, len);