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",
117 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
121 struct rte_device *dev;
122 struct rte_devargs *da;
125 bus = rte_bus_find_by_name(busname);
127 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
131 if (bus->plug == NULL) {
132 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
137 da = calloc(1, sizeof(*da));
141 ret = rte_devargs_parse(da, "%s:%s,%s",
142 busname, devname, devargs);
146 ret = rte_devargs_insert(da);
154 dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
156 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
162 ret = bus->plug(dev);
164 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
171 if (rte_devargs_remove(busname, devname)) {
178 int __rte_experimental
179 rte_eal_hotplug_remove(const char *busname, const char *devname)
182 struct rte_device *dev;
185 bus = rte_bus_find_by_name(busname);
187 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
191 if (bus->unplug == NULL) {
192 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
197 dev = bus->find_device(NULL, cmp_dev_name, devname);
199 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
203 ret = bus->unplug(dev);
205 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
207 rte_devargs_remove(busname, devname);
211 int __rte_experimental
212 rte_dev_event_callback_register(const char *device_name,
213 rte_dev_event_cb_fn cb_fn,
216 struct dev_event_callback *event_cb;
222 rte_spinlock_lock(&dev_event_lock);
224 if (TAILQ_EMPTY(&dev_event_cbs))
225 TAILQ_INIT(&dev_event_cbs);
227 TAILQ_FOREACH(event_cb, &dev_event_cbs, next) {
228 if (event_cb->cb_fn == cb_fn && event_cb->cb_arg == cb_arg) {
229 if (device_name == NULL && event_cb->dev_name == NULL)
231 if (device_name == NULL || event_cb->dev_name == NULL)
233 if (!strcmp(event_cb->dev_name, device_name))
238 /* create a new callback. */
239 if (event_cb == NULL) {
240 event_cb = malloc(sizeof(struct dev_event_callback));
241 if (event_cb != NULL) {
242 event_cb->cb_fn = cb_fn;
243 event_cb->cb_arg = cb_arg;
244 event_cb->active = 0;
246 event_cb->dev_name = NULL;
248 event_cb->dev_name = strdup(device_name);
249 if (event_cb->dev_name == NULL) {
254 TAILQ_INSERT_TAIL(&dev_event_cbs, event_cb, next);
257 "Failed to allocate memory for device "
264 "The callback is already exist, no need "
265 "to register again.\n");
269 rte_spinlock_unlock(&dev_event_lock);
273 rte_spinlock_unlock(&dev_event_lock);
277 int __rte_experimental
278 rte_dev_event_callback_unregister(const char *device_name,
279 rte_dev_event_cb_fn cb_fn,
283 struct dev_event_callback *event_cb, *next;
288 rte_spinlock_lock(&dev_event_lock);
289 /*walk through the callbacks and remove all that match. */
290 for (event_cb = TAILQ_FIRST(&dev_event_cbs); event_cb != NULL;
293 next = TAILQ_NEXT(event_cb, next);
295 if (device_name != NULL && event_cb->dev_name != NULL) {
296 if (!strcmp(event_cb->dev_name, device_name)) {
297 if (event_cb->cb_fn != cb_fn ||
298 (cb_arg != (void *)-1 &&
299 event_cb->cb_arg != cb_arg))
302 } else if (device_name != NULL) {
307 * if this callback is not executing right now,
310 if (event_cb->active == 0) {
311 TAILQ_REMOVE(&dev_event_cbs, event_cb, next);
318 rte_spinlock_unlock(&dev_event_lock);
323 dev_callback_process(char *device_name, enum rte_dev_event_type event)
325 struct dev_event_callback *cb_lst;
327 if (device_name == NULL)
330 rte_spinlock_lock(&dev_event_lock);
332 TAILQ_FOREACH(cb_lst, &dev_event_cbs, next) {
333 if (cb_lst->dev_name) {
334 if (strcmp(cb_lst->dev_name, device_name))
338 rte_spinlock_unlock(&dev_event_lock);
339 cb_lst->cb_fn(device_name, event,
341 rte_spinlock_lock(&dev_event_lock);
344 rte_spinlock_unlock(&dev_event_lock);