e25127557523fa7a284ad1c44aae169f3ed60a44
[deb_dpdk.git] / lib / librte_eal / common / eal_common_dev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <inttypes.h>
38 #include <sys/queue.h>
39
40 #include <rte_bus.h>
41 #include <rte_dev.h>
42 #include <rte_devargs.h>
43 #include <rte_debug.h>
44 #include <rte_log.h>
45
46 #include "eal_private.h"
47
48 static int cmp_detached_dev_name(const struct rte_device *dev,
49         const void *_name)
50 {
51         const char *name = _name;
52
53         /* skip attached devices */
54         if (dev->driver != NULL)
55                 return 1;
56
57         return strcmp(dev->name, name);
58 }
59
60 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
61 {
62         const char *name = _name;
63
64         return strcmp(dev->name, name);
65 }
66
67 int rte_eal_dev_attach(const char *name, const char *devargs)
68 {
69         struct rte_bus *bus;
70         int ret;
71
72         if (name == NULL || devargs == NULL) {
73                 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
74                 return -EINVAL;
75         }
76
77         bus = rte_bus_find_by_device_name(name);
78         if (bus == NULL) {
79                 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
80                         name);
81                 return -EINVAL;
82         }
83         if (strcmp(bus->name, "pci") == 0)
84                 return rte_eal_hotplug_add("pci", name, devargs);
85         if (strcmp(bus->name, "vdev") != 0) {
86                 RTE_LOG(ERR, EAL, "Device attach is only supported for PCI and vdev devices.\n");
87                 return -ENOTSUP;
88         }
89
90         /*
91          * If we haven't found a bus device the user meant to "hotplug" a
92          * virtual device instead.
93          */
94         ret = rte_vdev_init(name, devargs);
95         if (ret)
96                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
97                         name);
98         return ret;
99 }
100
101 int rte_eal_dev_detach(struct rte_device *dev)
102 {
103         struct rte_bus *bus;
104         int ret;
105
106         if (dev == NULL) {
107                 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
108                 return -EINVAL;
109         }
110
111         bus = rte_bus_find_by_device(dev);
112         if (bus == NULL) {
113                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
114                         dev->name);
115                 return -EINVAL;
116         }
117
118         if (bus->unplug == NULL) {
119                 RTE_LOG(ERR, EAL, "Bus function not supported\n");
120                 return -ENOTSUP;
121         }
122
123         ret = bus->unplug(dev);
124         if (ret)
125                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
126                         dev->name);
127         return ret;
128 }
129
130 static char *
131 full_dev_name(const char *bus, const char *dev, const char *args)
132 {
133         char *name;
134         size_t len;
135
136         len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args) + 1;
137         name = calloc(1, len);
138         if (name == NULL) {
139                 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
140                 return NULL;
141         }
142         snprintf(name, len, "%s:%s,%s", bus, dev, args);
143         return name;
144 }
145
146 int rte_eal_hotplug_add(const char *busname, const char *devname,
147                         const char *devargs)
148 {
149         struct rte_bus *bus;
150         struct rte_device *dev;
151         struct rte_devargs *da;
152         char *name;
153         int ret;
154
155         bus = rte_bus_find_by_name(busname);
156         if (bus == NULL) {
157                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
158                 return -ENOENT;
159         }
160
161         if (bus->plug == NULL) {
162                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
163                         bus->name);
164                 return -ENOTSUP;
165         }
166
167         name = full_dev_name(busname, devname, devargs);
168         if (name == NULL)
169                 return -ENOMEM;
170
171         da = calloc(1, sizeof(*da));
172         if (da == NULL) {
173                 ret = -ENOMEM;
174                 goto err_name;
175         }
176
177         ret = rte_eal_devargs_parse(name, da);
178         if (ret)
179                 goto err_devarg;
180
181         ret = rte_eal_devargs_insert(da);
182         if (ret)
183                 goto err_devarg;
184
185         ret = bus->scan();
186         if (ret)
187                 goto err_devarg;
188
189         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
190         if (dev == NULL) {
191                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
192                         devname);
193                 ret = -ENODEV;
194                 goto err_devarg;
195         }
196
197         ret = bus->plug(dev);
198         if (ret) {
199                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
200                         dev->name);
201                 goto err_devarg;
202         }
203         free(name);
204         return 0;
205
206 err_devarg:
207         if (rte_eal_devargs_remove(busname, devname)) {
208                 free(da->args);
209                 free(da);
210         }
211 err_name:
212         free(name);
213         return ret;
214 }
215
216 int rte_eal_hotplug_remove(const char *busname, const char *devname)
217 {
218         struct rte_bus *bus;
219         struct rte_device *dev;
220         int ret;
221
222         bus = rte_bus_find_by_name(busname);
223         if (bus == NULL) {
224                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
225                 return -ENOENT;
226         }
227
228         if (bus->unplug == NULL) {
229                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
230                         bus->name);
231                 return -ENOTSUP;
232         }
233
234         dev = bus->find_device(NULL, cmp_dev_name, devname);
235         if (dev == NULL) {
236                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
237                 return -EINVAL;
238         }
239
240         ret = bus->unplug(dev);
241         if (ret)
242                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
243                         dev->name);
244         rte_eal_devargs_remove(busname, devname);
245         return ret;
246 }