0de1c5d6286a9af6483a2300aec1a171891a3842
[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_compat.h>
41 #include <rte_bus.h>
42 #include <rte_dev.h>
43 #include <rte_devargs.h>
44 #include <rte_debug.h>
45 #include <rte_log.h>
46
47 #include "eal_private.h"
48
49 static int cmp_detached_dev_name(const struct rte_device *dev,
50         const void *_name)
51 {
52         const char *name = _name;
53
54         /* skip attached devices */
55         if (dev->driver != NULL)
56                 return 1;
57
58         return strcmp(dev->name, name);
59 }
60
61 static int cmp_dev_name(const struct rte_device *dev, const void *_name)
62 {
63         const char *name = _name;
64
65         return strcmp(dev->name, name);
66 }
67
68 int rte_eal_dev_attach(const char *name, const char *devargs)
69 {
70         struct rte_bus *bus;
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 || strcmp(bus->name, "vdev") == 0)
84                 return rte_eal_hotplug_add(bus->name, name, devargs);
85
86         RTE_LOG(ERR, EAL,
87                 "Device attach is only supported for PCI and vdev devices.\n");
88
89         return -ENOTSUP;
90 }
91
92 int rte_eal_dev_detach(struct rte_device *dev)
93 {
94         struct rte_bus *bus;
95         int ret;
96
97         if (dev == NULL) {
98                 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
99                 return -EINVAL;
100         }
101
102         bus = rte_bus_find_by_device(dev);
103         if (bus == NULL) {
104                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
105                         dev->name);
106                 return -EINVAL;
107         }
108
109         if (bus->unplug == NULL) {
110                 RTE_LOG(ERR, EAL, "Bus function not supported\n");
111                 return -ENOTSUP;
112         }
113
114         ret = bus->unplug(dev);
115         if (ret)
116                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
117                         dev->name);
118         return ret;
119 }
120
121 static char *
122 full_dev_name(const char *bus, const char *dev, const char *args)
123 {
124         char *name;
125         size_t len;
126
127         len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args) + 1;
128         name = calloc(1, len);
129         if (name == NULL) {
130                 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
131                 return NULL;
132         }
133         snprintf(name, len, "%s:%s,%s", bus, dev, args);
134         return name;
135 }
136
137 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
138                         const char *devargs)
139 {
140         struct rte_bus *bus;
141         struct rte_device *dev;
142         struct rte_devargs *da;
143         char *name;
144         int ret;
145
146         bus = rte_bus_find_by_name(busname);
147         if (bus == NULL) {
148                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
149                 return -ENOENT;
150         }
151
152         if (bus->plug == NULL) {
153                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
154                         bus->name);
155                 return -ENOTSUP;
156         }
157
158         name = full_dev_name(busname, devname, devargs);
159         if (name == NULL)
160                 return -ENOMEM;
161
162         da = calloc(1, sizeof(*da));
163         if (da == NULL) {
164                 ret = -ENOMEM;
165                 goto err_name;
166         }
167
168         ret = rte_eal_devargs_parse(name, da);
169         if (ret)
170                 goto err_devarg;
171
172         ret = rte_eal_devargs_insert(da);
173         if (ret)
174                 goto err_devarg;
175
176         ret = bus->scan();
177         if (ret)
178                 goto err_devarg;
179
180         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
181         if (dev == NULL) {
182                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
183                         devname);
184                 ret = -ENODEV;
185                 goto err_devarg;
186         }
187
188         ret = bus->plug(dev);
189         if (ret) {
190                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
191                         dev->name);
192                 goto err_devarg;
193         }
194         free(name);
195         return 0;
196
197 err_devarg:
198         if (rte_eal_devargs_remove(busname, devname)) {
199                 free(da->args);
200                 free(da);
201         }
202 err_name:
203         free(name);
204         return ret;
205 }
206
207 int __rte_experimental
208 rte_eal_hotplug_remove(const char *busname, const char *devname)
209 {
210         struct rte_bus *bus;
211         struct rte_device *dev;
212         int ret;
213
214         bus = rte_bus_find_by_name(busname);
215         if (bus == NULL) {
216                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
217                 return -ENOENT;
218         }
219
220         if (bus->unplug == NULL) {
221                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
222                         bus->name);
223                 return -ENOTSUP;
224         }
225
226         dev = bus->find_device(NULL, cmp_dev_name, devname);
227         if (dev == NULL) {
228                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
229                 return -EINVAL;
230         }
231
232         ret = bus->unplug(dev);
233         if (ret)
234                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
235                         dev->name);
236         rte_eal_devargs_remove(busname, devname);
237         return ret;
238 }