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>
23 #include "eal_private.h"
26 * The device event callback description.
28 * It contains callback address to be registered by user application,
29 * the pointer to the parameters for callback, and the device name.
31 struct dev_event_callback {
32 TAILQ_ENTRY(dev_event_callback) next; /**< Callbacks list */
33 rte_dev_event_cb_fn cb_fn; /**< Callback address */
34 void *cb_arg; /**< Callback parameter */
35 char *dev_name; /**< Callback device name, NULL is for all device */
36 uint32_t active; /**< Callback is executing */
39 /** @internal Structure to keep track of registered callbacks */
40 TAILQ_HEAD(dev_event_cb_list, dev_event_callback);
42 /* The device event callback list for all registered callbacks. */
43 static struct dev_event_cb_list dev_event_cbs;
45 /* spinlock for device callbacks */
46 static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER;
49 struct rte_dev_iterator *it;
54 #define CTX(it, bus_str, cls_str) \
55 (&(const struct dev_next_ctx){ \
62 (((struct dev_next_ctx *)(intptr_t)ptr)->it)
65 (((struct dev_next_ctx *)(intptr_t)ptr)->bus_str)
68 (((struct dev_next_ctx *)(intptr_t)ptr)->cls_str)
70 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
72 const char *name = _name;
74 return strcmp(dev->name, name);
77 int rte_eal_dev_attach(const char *name, const char *devargs)
81 if (name == NULL || devargs == NULL) {
82 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
86 bus = rte_bus_find_by_device_name(name);
88 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
92 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
93 return rte_eal_hotplug_add(bus->name, name, devargs);
96 "Device attach is only supported for PCI and vdev devices.\n");
101 int rte_eal_dev_detach(struct rte_device *dev)
107 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
111 bus = rte_bus_find_by_device(dev);
113 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
118 if (bus->unplug == NULL) {
119 RTE_LOG(ERR, EAL, "Bus function not supported\n");
123 ret = bus->unplug(dev);
125 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
131 rte_eal_hotplug_add(const char *busname, const char *devname,
135 char *devargs = NULL;
138 length = snprintf(NULL, 0, "%s:%s,%s", busname, devname, drvargs);
141 devargs = malloc(length + 1);
144 ret = snprintf(devargs, length + 1, "%s:%s,%s", busname, devname, drvargs);
148 ret = rte_dev_probe(devargs);
154 int __rte_experimental
155 rte_dev_probe(const char *devargs)
157 struct rte_device *dev;
158 struct rte_devargs *da;
161 da = calloc(1, sizeof(*da));
165 ret = rte_devargs_parse(da, devargs);
169 if (da->bus->plug == NULL) {
170 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
176 ret = rte_devargs_insert(da);
180 ret = da->bus->scan();
184 dev = da->bus->find_device(NULL, cmp_dev_name, da->name);
186 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
192 if (dev->driver != NULL) {
193 RTE_LOG(ERR, EAL, "Device is already plugged\n");
197 ret = dev->bus->plug(dev);
199 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
206 if (rte_devargs_remove(da) != 0) {
214 rte_eal_hotplug_remove(const char *busname, const char *devname)
216 struct rte_device *dev;
219 bus = rte_bus_find_by_name(busname);
221 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
225 dev = bus->find_device(NULL, cmp_dev_name, devname);
227 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
231 return rte_dev_remove(dev);
234 int __rte_experimental
235 rte_dev_remove(struct rte_device *dev)
239 if (dev->driver == NULL) {
240 RTE_LOG(ERR, EAL, "Device is already unplugged\n");
244 if (dev->bus->unplug == NULL) {
245 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
250 ret = dev->bus->unplug(dev);
252 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
254 rte_devargs_remove(dev->devargs);
258 int __rte_experimental
259 rte_dev_event_callback_register(const char *device_name,
260 rte_dev_event_cb_fn cb_fn,
263 struct dev_event_callback *event_cb;
269 rte_spinlock_lock(&dev_event_lock);
271 if (TAILQ_EMPTY(&dev_event_cbs))
272 TAILQ_INIT(&dev_event_cbs);
274 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
275 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
276 if (device_name == NULL && event_cb->dev_name == NULL)
278 if (device_name == NULL || event_cb->dev_name == NULL)
280 if (!strcmp(event_cb->dev_name, device_name))
285 /* create a new callback. */
286 if (event_cb == NULL) {
287 event_cb = malloc(sizeof(struct dev_event_callback));
288 if (event_cb != NULL) {
289 event_cb->cb_fn = cb_fn;
290 event_cb->cb_arg = cb_arg;
291 event_cb->active = 0;
293 event_cb->dev_name = NULL;
295 event_cb->dev_name = strdup(device_name);
296 if (event_cb->dev_name == NULL) {
301 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
304 "Failed to allocate memory for device "
311 "The callback is already exist, no need "
312 "to register again.\n");
316 rte_spinlock_unlock(&dev_event_lock);
320 rte_spinlock_unlock(&dev_event_lock);
324 int __rte_experimental
325 rte_dev_event_callback_unregister(const char *device_name,
326 rte_dev_event_cb_fn cb_fn,
330 struct dev_event_callback *event_cb, *next;
335 rte_spinlock_lock(&dev_event_lock);
336 /*walk through the callbacks and remove all that match. */
337 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
340 next = TAILQ_NEXT(event_cb, next);
342 if (device_name != NULL && event_cb->dev_name != NULL) {
343 if (!strcmp(event_cb->dev_name, device_name)) {
344 if (event_cb->cb_fn != cb_fn ||
345 (cb_arg != (void *)-1 &&
346 event_cb->cb_arg != cb_arg))
349 } else if (device_name != NULL) {
354 * if this callback is not executing right now,
357 if (event_cb->active == 0) {
358 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
365 rte_spinlock_unlock(&dev_event_lock);
370 dev_callback_process(char *device_name, enum rte_dev_event_type event)
372 struct dev_event_callback *cb_lst;
374 if (device_name == NULL)
377 rte_spinlock_lock(&dev_event_lock);
379 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
380 if (cb_lst->dev_name) {
381 if (strcmp(cb_lst->dev_name, device_name))
385 rte_spinlock_unlock(&dev_event_lock);
386 cb_lst->cb_fn(device_name, event,
388 rte_spinlock_lock(&dev_event_lock);
391 rte_spinlock_unlock(&dev_event_lock);
396 rte_dev_iterator_init(struct rte_dev_iterator *it,
399 struct rte_devargs devargs;
400 struct rte_class *cls = NULL;
401 struct rte_bus *bus = NULL;
403 /* Having both bus_str and cls_str NULL is illegal,
404 * marking this iterator as invalid unless
405 * everything goes well.
410 devargs.data = dev_str;
411 if (rte_devargs_layers_parse(&devargs, dev_str))
416 /* The string should have at least
417 * one layer specified.
419 if (bus == NULL && cls == NULL) {
421 "Either bus or class must be specified.\n");
425 if (bus != NULL && bus->dev_iterate == NULL) {
426 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
430 if (cls != NULL && cls->dev_iterate == NULL) {
431 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
435 it->bus_str = devargs.bus_str;
436 it->cls_str = devargs.cls_str;
437 it->dev_str = dev_str;
441 it->class_device = NULL;
447 dev_str_sane_copy(const char *str)
452 end = strcspn(str, ",/");
453 if (str[end] == ',') {
454 copy = strdup(&str[end + 1]);
464 slash = strchr(copy, '/');
472 class_next_dev_cmp(const struct rte_class *cls,
475 struct rte_dev_iterator *it;
476 const char *cls_str = NULL;
479 if (cls->dev_iterate == NULL)
482 cls_str = CLSCTX(ctx);
483 dev = it->class_device;
484 /* it->cls_str != NULL means a class
485 * was specified in the devstr.
487 if (it->cls_str != NULL && cls != it->cls)
489 /* If an error occurred previously,
490 * no need to test further.
494 dev = cls->dev_iterate(dev, cls_str, it);
495 it->class_device = dev;
500 bus_next_dev_cmp(const struct rte_bus *bus,
503 struct rte_device *dev = NULL;
504 struct rte_class *cls = NULL;
505 struct rte_dev_iterator *it;
506 const char *bus_str = NULL;
508 if (bus->dev_iterate == NULL)
511 bus_str = BUSCTX(ctx);
513 /* it->bus_str != NULL means a bus
514 * was specified in the devstr.
516 if (it->bus_str != NULL && bus != it->bus)
518 /* If an error occurred previously,
519 * no need to test further.
523 if (it->cls_str == NULL) {
524 dev = bus->dev_iterate(dev, bus_str, it);
527 /* cls_str != NULL */
530 dev = bus->dev_iterate(dev, bus_str, it);
536 cls = TAILQ_PREV(it->cls, rte_class_list, next);
537 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
542 goto next_dev_on_bus;
549 rte_dev_iterator_next(struct rte_dev_iterator *it)
551 struct rte_bus *bus = NULL;
552 int old_errno = rte_errno;
553 char *bus_str = NULL;
554 char *cls_str = NULL;
557 if (it->bus_str == NULL && it->cls_str == NULL) {
558 /* Invalid iterator. */
563 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
564 if (it->bus_str != NULL) {
565 bus_str = dev_str_sane_copy(it->bus_str);
569 if (it->cls_str != NULL) {
570 cls_str = dev_str_sane_copy(it->cls_str);
574 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
575 CTX(it, bus_str, cls_str)))) {
576 if (it->device != NULL) {
580 if (it->bus_str != NULL ||
585 rte_errno = old_errno;