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;
48 static int cmp_detached_dev_name(const struct rte_device *dev,
51 const char *name = _name;
53 /* skip attached devices */
54 if (dev->driver != NULL)
57 return strcmp(dev->name, name);
60 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
62 const char *name = _name;
64 return strcmp(dev->name, name);
67 int rte_eal_dev_attach(const char *name, const char *devargs)
71 if (name == NULL || devargs == NULL) {
72 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
76 bus = rte_bus_find_by_device_name(name);
78 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
82 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
83 return rte_eal_hotplug_add(bus->name, name, devargs);
86 "Device attach is only supported for PCI and vdev devices.\n");
91 int rte_eal_dev_detach(struct rte_device *dev)
97 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
101 bus = rte_bus_find_by_device(dev);
103 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
108 if (bus->unplug == NULL) {
109 RTE_LOG(ERR, EAL, "Bus function not supported\n");
113 ret = bus->unplug(dev);
115 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
120 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
124 struct rte_device *dev;
125 struct rte_devargs *da;
128 bus = rte_bus_find_by_name(busname);
130 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
134 if (bus->plug == NULL) {
135 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
140 da = calloc(1, sizeof(*da));
144 ret = rte_devargs_parsef(da, "%s:%s,%s",
145 busname, devname, devargs);
149 ret = rte_devargs_insert(da);
157 dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
159 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
165 ret = bus->plug(dev);
167 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
174 if (rte_devargs_remove(busname, devname)) {
181 int __rte_experimental
182 rte_eal_hotplug_remove(const char *busname, const char *devname)
185 struct rte_device *dev;
188 bus = rte_bus_find_by_name(busname);
190 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
194 if (bus->unplug == NULL) {
195 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
200 dev = bus->find_device(NULL, cmp_dev_name, devname);
202 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
206 ret = bus->unplug(dev);
208 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
210 rte_devargs_remove(busname, devname);
214 int __rte_experimental
215 rte_dev_event_callback_register(const char *device_name,
216 rte_dev_event_cb_fn cb_fn,
219 struct dev_event_callback *event_cb;
225 rte_spinlock_lock(&dev_event_lock);
227 if (TAILQ_EMPTY(&dev_event_cbs))
228 TAILQ_INIT(&dev_event_cbs);
230 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
231 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
232 if (device_name == NULL && event_cb->dev_name == NULL)
234 if (device_name == NULL || event_cb->dev_name == NULL)
236 if (!strcmp(event_cb->dev_name, device_name))
241 /* create a new callback. */
242 if (event_cb == NULL) {
243 event_cb = malloc(sizeof(struct dev_event_callback));
244 if (event_cb != NULL) {
245 event_cb->cb_fn = cb_fn;
246 event_cb->cb_arg = cb_arg;
247 event_cb->active = 0;
249 event_cb->dev_name = NULL;
251 event_cb->dev_name = strdup(device_name);
252 if (event_cb->dev_name == NULL) {
257 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
260 "Failed to allocate memory for device "
267 "The callback is already exist, no need "
268 "to register again.\n");
272 rte_spinlock_unlock(&dev_event_lock);
276 rte_spinlock_unlock(&dev_event_lock);
280 int __rte_experimental
281 rte_dev_event_callback_unregister(const char *device_name,
282 rte_dev_event_cb_fn cb_fn,
286 struct dev_event_callback *event_cb, *next;
291 rte_spinlock_lock(&dev_event_lock);
292 /*walk through the callbacks and remove all that match. */
293 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
296 next = TAILQ_NEXT(event_cb, next);
298 if (device_name != NULL && event_cb->dev_name != NULL) {
299 if (!strcmp(event_cb->dev_name, device_name)) {
300 if (event_cb->cb_fn != cb_fn ||
301 (cb_arg != (void *)-1 &&
302 event_cb->cb_arg != cb_arg))
305 } else if (device_name != NULL) {
310 * if this callback is not executing right now,
313 if (event_cb->active == 0) {
314 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
321 rte_spinlock_unlock(&dev_event_lock);
326 dev_callback_process(char *device_name, enum rte_dev_event_type event)
328 struct dev_event_callback *cb_lst;
330 if (device_name == NULL)
333 rte_spinlock_lock(&dev_event_lock);
335 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
336 if (cb_lst->dev_name) {
337 if (strcmp(cb_lst->dev_name, device_name))
341 rte_spinlock_unlock(&dev_event_lock);
342 cb_lst->cb_fn(device_name, event,
344 rte_spinlock_lock(&dev_event_lock);
347 rte_spinlock_unlock(&dev_event_lock);
352 rte_dev_iterator_init(struct rte_dev_iterator *it,
355 struct rte_devargs devargs;
356 struct rte_class *cls = NULL;
357 struct rte_bus *bus = NULL;
359 /* Having both bus_str and cls_str NULL is illegal,
360 * marking this iterator as invalid unless
361 * everything goes well.
366 devargs.data = dev_str;
367 if (rte_devargs_layers_parse(&devargs, dev_str))
372 /* The string should have at least
373 * one layer specified.
375 if (bus == NULL && cls == NULL) {
377 "Either bus or class must be specified.\n");
381 if (bus != NULL && bus->dev_iterate == NULL) {
382 RTE_LOG(ERR, EAL, "Bus %s not supported\n", bus->name);
386 if (cls != NULL && cls->dev_iterate == NULL) {
387 RTE_LOG(ERR, EAL, "Class %s not supported\n", cls->name);
391 it->bus_str = devargs.bus_str;
392 it->cls_str = devargs.cls_str;
393 it->dev_str = dev_str;
397 it->class_device = NULL;