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_detached_dev_name(const struct rte_device *dev,
73 const char *name = _name;
75 /* skip attached devices */
76 if (dev->driver != NULL)
79 return strcmp(dev->name, name);
82 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
84 const char *name = _name;
86 return strcmp(dev->name, name);
89 int rte_eal_dev_attach(const char *name, const char *devargs)
93 if (name == NULL || devargs == NULL) {
94 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
98 bus = rte_bus_find_by_device_name(name);
100 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
104 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
105 return rte_eal_hotplug_add(bus->name, name, devargs);
108 "Device attach is only supported for PCI and vdev devices.\n");
113 int rte_eal_dev_detach(struct rte_device *dev)
119 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
123 bus = rte_bus_find_by_device(dev);
125 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
130 if (bus->unplug == NULL) {
131 RTE_LOG(ERR, EAL, "Bus function not supported\n");
135 ret = bus->unplug(dev);
137 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
142 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
146 struct rte_device *dev;
147 struct rte_devargs *da;
150 bus = rte_bus_find_by_name(busname);
152 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
156 if (bus->plug == NULL) {
157 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
162 da = calloc(1, sizeof(*da));
166 ret = rte_devargs_parsef(da, "%s:%s,%s",
167 busname, devname, devargs);
171 ret = rte_devargs_insert(da);
179 dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
181 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
187 ret = bus->plug(dev);
189 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
196 if (rte_devargs_remove(busname, devname)) {
203 int __rte_experimental
204 rte_eal_hotplug_remove(const char *busname, const char *devname)
207 struct rte_device *dev;
210 bus = rte_bus_find_by_name(busname);
212 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
216 if (bus->unplug == NULL) {
217 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
222 dev = bus->find_device(NULL, cmp_dev_name, devname);
224 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
228 ret = bus->unplug(dev);
230 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
232 rte_devargs_remove(busname, devname);
236 int __rte_experimental
237 rte_dev_event_callback_register(const char *device_name,
238 rte_dev_event_cb_fn cb_fn,
241 struct dev_event_callback *event_cb;
247 rte_spinlock_lock(&dev_event_lock);
249 if (TAILQ_EMPTY(&dev_event_cbs))
250 TAILQ_INIT(&dev_event_cbs);
252 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
253 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
254 if (device_name == NULL && event_cb->dev_name == NULL)
256 if (device_name == NULL || event_cb->dev_name == NULL)
258 if (!strcmp(event_cb->dev_name, device_name))
263 /* create a new callback. */
264 if (event_cb == NULL) {
265 event_cb = malloc(sizeof(struct dev_event_callback));
266 if (event_cb != NULL) {
267 event_cb->cb_fn = cb_fn;
268 event_cb->cb_arg = cb_arg;
269 event_cb->active = 0;
271 event_cb->dev_name = NULL;
273 event_cb->dev_name = strdup(device_name);
274 if (event_cb->dev_name == NULL) {
279 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
282 "Failed to allocate memory for device "
289 "The callback is already exist, no need "
290 "to register again.\n");
294 rte_spinlock_unlock(&dev_event_lock);
298 rte_spinlock_unlock(&dev_event_lock);
302 int __rte_experimental
303 rte_dev_event_callback_unregister(const char *device_name,
304 rte_dev_event_cb_fn cb_fn,
308 struct dev_event_callback *event_cb, *next;
313 rte_spinlock_lock(&dev_event_lock);
314 /*walk through the callbacks and remove all that match. */
315 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
318 next = TAILQ_NEXT(event_cb, next);
320 if (device_name != NULL && event_cb->dev_name != NULL) {
321 if (!strcmp(event_cb->dev_name, device_name)) {
322 if (event_cb->cb_fn != cb_fn ||
323 (cb_arg != (void *)-1 &&
324 event_cb->cb_arg != cb_arg))
327 } else if (device_name != NULL) {
332 * if this callback is not executing right now,
335 if (event_cb->active == 0) {
336 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
343 rte_spinlock_unlock(&dev_event_lock);
348 dev_callback_process(char *device_name, enum rte_dev_event_type event)
350 struct dev_event_callback *cb_lst;
352 if (device_name == NULL)
355 rte_spinlock_lock(&dev_event_lock);
357 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
358 if (cb_lst->dev_name) {
359 if (strcmp(cb_lst->dev_name, device_name))
363 rte_spinlock_unlock(&dev_event_lock);
364 cb_lst->cb_fn(device_name, event,
366 rte_spinlock_lock(&dev_event_lock);
369 rte_spinlock_unlock(&dev_event_lock);
374 rte_dev_iterator_init(struct rte_dev_iterator *it,
377 struct rte_devargs devargs;
378 struct rte_class *cls = NULL;
379 struct rte_bus *bus = NULL;
381 /* Having both bus_str and cls_str NULL is illegal,
382 * marking this iterator as invalid unless
383 * everything goes well.
388 devargs.data = dev_str;
389 if (rte_devargs_layers_parse(&devargs, dev_str))
394 /* The string should have at least
395 * one layer specified.
397 if (bus == NULL && cls == NULL) {
399 "Either bus or class must be specified.\n");
403 if (bus != NULL && bus->dev_iterate == NULL) {
404 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
408 if (cls != NULL && cls->dev_iterate == NULL) {
409 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
413 it->bus_str = devargs.bus_str;
414 it->cls_str = devargs.cls_str;
415 it->dev_str = dev_str;
419 it->class_device = NULL;
425 dev_str_sane_copy(const char *str)
430 end = strcspn(str, ",/");
431 if (str[end] == ',') {
432 copy = strdup(&str[end + 1]);
442 slash = strchr(copy, '/');
450 class_next_dev_cmp(const struct rte_class *cls,
453 struct rte_dev_iterator *it;
454 const char *cls_str = NULL;
457 if (cls->dev_iterate == NULL)
460 cls_str = CLSCTX(ctx);
461 dev = it->class_device;
462 /* it->cls_str != NULL means a class
463 * was specified in the devstr.
465 if (it->cls_str != NULL && cls != it->cls)
467 /* If an error occurred previously,
468 * no need to test further.
472 dev = cls->dev_iterate(dev, cls_str, it);
473 it->class_device = dev;
478 bus_next_dev_cmp(const struct rte_bus *bus,
481 struct rte_device *dev = NULL;
482 struct rte_class *cls = NULL;
483 struct rte_dev_iterator *it;
484 const char *bus_str = NULL;
486 if (bus->dev_iterate == NULL)
489 bus_str = BUSCTX(ctx);
491 /* it->bus_str != NULL means a bus
492 * was specified in the devstr.
494 if (it->bus_str != NULL && bus != it->bus)
496 /* If an error occurred previously,
497 * no need to test further.
501 if (it->cls_str == NULL) {
502 dev = bus->dev_iterate(dev, bus_str, it);
505 /* cls_str != NULL */
508 dev = bus->dev_iterate(dev, bus_str, it);
514 cls = TAILQ_PREV(it->cls, rte_class_list, next);
515 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
520 goto next_dev_on_bus;
527 rte_dev_iterator_next(struct rte_dev_iterator *it)
529 struct rte_bus *bus = NULL;
530 int old_errno = rte_errno;
531 char *bus_str = NULL;
532 char *cls_str = NULL;
535 if (it->bus_str == NULL && it->cls_str == NULL) {
536 /* Invalid iterator. */
541 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
542 if (it->bus_str != NULL) {
543 bus_str = dev_str_sane_copy(it->bus_str);
547 if (it->cls_str != NULL) {
548 cls_str = dev_str_sane_copy(it->cls_str);
552 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
553 CTX(it, bus_str, cls_str)))) {
554 if (it->device != NULL) {
558 if (it->bus_str != NULL ||
563 rte_errno = old_errno;