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>
18 #include "eal_private.h"
20 static int cmp_detached_dev_name(const struct rte_device *dev,
23 const char *name = _name;
25 /* skip attached devices */
26 if (dev->driver != NULL)
29 return strcmp(dev->name, name);
32 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
34 const char *name = _name;
36 return strcmp(dev->name, name);
39 int rte_eal_dev_attach(const char *name, const char *devargs)
43 if (name == NULL || devargs == NULL) {
44 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
48 bus = rte_bus_find_by_device_name(name);
50 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
54 if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
55 return rte_eal_hotplug_add(bus->name, name, devargs);
58 "Device attach is only supported for PCI and vdev devices.\n");
63 int rte_eal_dev_detach(struct rte_device *dev)
69 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
73 bus = rte_bus_find_by_device(dev);
75 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
80 if (bus->unplug == NULL) {
81 RTE_LOG(ERR, EAL, "Bus function not supported\n");
85 ret = bus->unplug(dev);
87 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
93 full_dev_name(const char *bus, const char *dev, const char *args)
98 len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args) + 1;
99 name = calloc(1, len);
101 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
104 snprintf(name, len, "%s:%s,%s", bus, dev, args);
108 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
112 struct rte_device *dev;
113 struct rte_devargs *da;
117 bus = rte_bus_find_by_name(busname);
119 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
123 if (bus->plug == NULL) {
124 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
129 name = full_dev_name(busname, devname, devargs);
133 da = calloc(1, sizeof(*da));
139 ret = rte_eal_devargs_parse(name, da);
143 ret = rte_eal_devargs_insert(da);
151 dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
153 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
159 ret = bus->plug(dev);
161 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
169 if (rte_eal_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_eal_devargs_remove(busname, devname);