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",
130 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
134 struct rte_device *dev;
135 struct rte_devargs *da;
138 bus = rte_bus_find_by_name(busname);
140 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
144 if (bus->plug == NULL) {
145 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
150 da = calloc(1, sizeof(*da));
154 ret = rte_devargs_parsef(da, "%s:%s,%s",
155 busname, devname, devargs);
159 ret = rte_devargs_insert(da);
167 dev = bus->find_device(NULL, cmp_dev_name, devname);
169 RTE_LOG(ERR, EAL, "Cannot find device (%s)\n",
175 if (dev->driver != NULL) {
176 RTE_LOG(ERR, EAL, "Device is already plugged\n");
180 ret = bus->plug(dev);
182 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
189 if (rte_devargs_remove(busname, devname)) {
196 int __rte_experimental
197 rte_eal_hotplug_remove(const char *busname, const char *devname)
200 struct rte_device *dev;
203 bus = rte_bus_find_by_name(busname);
205 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
209 if (bus->unplug == NULL) {
210 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
215 dev = bus->find_device(NULL, cmp_dev_name, devname);
217 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
221 if (dev->driver == NULL) {
222 RTE_LOG(ERR, EAL, "Device is already unplugged\n");
226 ret = bus->unplug(dev);
228 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
230 rte_devargs_remove(busname, devname);
234 int __rte_experimental
235 rte_dev_event_callback_register(const char *device_name,
236 rte_dev_event_cb_fn cb_fn,
239 struct dev_event_callback *event_cb;
245 rte_spinlock_lock(&dev_event_lock);
247 if (TAILQ_EMPTY(&dev_event_cbs))
248 TAILQ_INIT(&dev_event_cbs);
250 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
251 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
252 if (device_name == NULL && event_cb->dev_name == NULL)
254 if (device_name == NULL || event_cb->dev_name == NULL)
256 if (!strcmp(event_cb->dev_name, device_name))
261 /* create a new callback. */
262 if (event_cb == NULL) {
263 event_cb = malloc(sizeof(struct dev_event_callback));
264 if (event_cb != NULL) {
265 event_cb->cb_fn = cb_fn;
266 event_cb->cb_arg = cb_arg;
267 event_cb->active = 0;
269 event_cb->dev_name = NULL;
271 event_cb->dev_name = strdup(device_name);
272 if (event_cb->dev_name == NULL) {
277 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
280 "Failed to allocate memory for device "
287 "The callback is already exist, no need "
288 "to register again.\n");
292 rte_spinlock_unlock(&dev_event_lock);
296 rte_spinlock_unlock(&dev_event_lock);
300 int __rte_experimental
301 rte_dev_event_callback_unregister(const char *device_name,
302 rte_dev_event_cb_fn cb_fn,
306 struct dev_event_callback *event_cb, *next;
311 rte_spinlock_lock(&dev_event_lock);
312 /*walk through the callbacks and remove all that match. */
313 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
316 next = TAILQ_NEXT(event_cb, next);
318 if (device_name != NULL && event_cb->dev_name != NULL) {
319 if (!strcmp(event_cb->dev_name, device_name)) {
320 if (event_cb->cb_fn != cb_fn ||
321 (cb_arg != (void *)-1 &&
322 event_cb->cb_arg != cb_arg))
325 } else if (device_name != NULL) {
330 * if this callback is not executing right now,
333 if (event_cb->active == 0) {
334 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
341 rte_spinlock_unlock(&dev_event_lock);
346 dev_callback_process(char *device_name, enum rte_dev_event_type event)
348 struct dev_event_callback *cb_lst;
350 if (device_name == NULL)
353 rte_spinlock_lock(&dev_event_lock);
355 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
356 if (cb_lst->dev_name) {
357 if (strcmp(cb_lst->dev_name, device_name))
361 rte_spinlock_unlock(&dev_event_lock);
362 cb_lst->cb_fn(device_name, event,
364 rte_spinlock_lock(&dev_event_lock);
367 rte_spinlock_unlock(&dev_event_lock);
372 rte_dev_iterator_init(struct rte_dev_iterator *it,
375 struct rte_devargs devargs;
376 struct rte_class *cls = NULL;
377 struct rte_bus *bus = NULL;
379 /* Having both bus_str and cls_str NULL is illegal,
380 * marking this iterator as invalid unless
381 * everything goes well.
386 devargs.data = dev_str;
387 if (rte_devargs_layers_parse(&devargs, dev_str))
392 /* The string should have at least
393 * one layer specified.
395 if (bus == NULL && cls == NULL) {
397 "Either bus or class must be specified.\n");
401 if (bus != NULL && bus->dev_iterate == NULL) {
402 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
406 if (cls != NULL && cls->dev_iterate == NULL) {
407 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
411 it->bus_str = devargs.bus_str;
412 it->cls_str = devargs.cls_str;
413 it->dev_str = dev_str;
417 it->class_device = NULL;
423 dev_str_sane_copy(const char *str)
428 end = strcspn(str, ",/");
429 if (str[end] == ',') {
430 copy = strdup(&str[end + 1]);
440 slash = strchr(copy, '/');
448 class_next_dev_cmp(const struct rte_class *cls,
451 struct rte_dev_iterator *it;
452 const char *cls_str = NULL;
455 if (cls->dev_iterate == NULL)
458 cls_str = CLSCTX(ctx);
459 dev = it->class_device;
460 /* it->cls_str != NULL means a class
461 * was specified in the devstr.
463 if (it->cls_str != NULL && cls != it->cls)
465 /* If an error occurred previously,
466 * no need to test further.
470 dev = cls->dev_iterate(dev, cls_str, it);
471 it->class_device = dev;
476 bus_next_dev_cmp(const struct rte_bus *bus,
479 struct rte_device *dev = NULL;
480 struct rte_class *cls = NULL;
481 struct rte_dev_iterator *it;
482 const char *bus_str = NULL;
484 if (bus->dev_iterate == NULL)
487 bus_str = BUSCTX(ctx);
489 /* it->bus_str != NULL means a bus
490 * was specified in the devstr.
492 if (it->bus_str != NULL && bus != it->bus)
494 /* If an error occurred previously,
495 * no need to test further.
499 if (it->cls_str == NULL) {
500 dev = bus->dev_iterate(dev, bus_str, it);
503 /* cls_str != NULL */
506 dev = bus->dev_iterate(dev, bus_str, it);
512 cls = TAILQ_PREV(it->cls, rte_class_list, next);
513 cls = rte_class_find(cls, class_next_dev_cmp, ctx);
518 goto next_dev_on_bus;
525 rte_dev_iterator_next(struct rte_dev_iterator *it)
527 struct rte_bus *bus = NULL;
528 int old_errno = rte_errno;
529 char *bus_str = NULL;
530 char *cls_str = NULL;
533 if (it->bus_str == NULL && it->cls_str == NULL) {
534 /* Invalid iterator. */
539 bus = TAILQ_PREV(it->bus, rte_bus_list, next);
540 if (it->bus_str != NULL) {
541 bus_str = dev_str_sane_copy(it->bus_str);
545 if (it->cls_str != NULL) {
546 cls_str = dev_str_sane_copy(it->cls_str);
550 while ((bus = rte_bus_find(bus, bus_next_dev_cmp,
551 CTX(it, bus_str, cls_str)))) {
552 if (it->device != NULL) {
556 if (it->bus_str != NULL ||
561 rte_errno = old_errno;