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>
14 #include <rte_devargs.h>
15 #include <rte_debug.h>
17 #include <rte_spinlock.h>
18 #include <rte_malloc.h>
20 #include "eal_private.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;
45 static int cmp_detached_dev_name(const struct rte_device *dev,
48 const char *name = _name;
50 /* skip attached devices */
51 if (dev->driver != NULL)
54 return strcmp(dev->name, name);
57 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
59 const char *name = _name;
61 return strcmp(dev->name, name);
64 int rte_eal_dev_attach(const char *name, const char *devargs)
68 if (name == NULL || devargs == NULL) {
69 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
73 bus = rte_bus_find_by_device_name(name);
75 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
79 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
80 return rte_eal_hotplug_add(bus->name, name, devargs);
83 "Device attach is only supported for PCI and vdev devices.\n");
88 int rte_eal_dev_detach(struct rte_device *dev)
94 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
98 bus = rte_bus_find_by_device(dev);
100 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
105 if (bus->unplug == NULL) {
106 RTE_LOG(ERR, EAL, "Bus function not supported\n");
110 ret = bus->unplug(dev);
112 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
118 full_dev_name(const char *bus, const char *dev, const char *args)
123 len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args) + 1;
124 name = calloc(1, len);
126 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
129 snprintf(name, len, "%s:%s,%s", bus, dev, args);
133 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
137 struct rte_device *dev;
138 struct rte_devargs *da;
142 bus = rte_bus_find_by_name(busname);
144 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
148 if (bus->plug == NULL) {
149 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
154 name = full_dev_name(busname, devname, devargs);
158 da = calloc(1, sizeof(*da));
164 ret = rte_eal_devargs_parse(name, da);
168 ret = rte_eal_devargs_insert(da);
176 dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
178 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
184 ret = bus->plug(dev);
186 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
194 if (rte_eal_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_eal_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);