New upstream version 17.11-rc3
[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
71         if (name == NULL || devargs == NULL) {
72                 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
73                 return -EINVAL;
74         }
75
76         bus = rte_bus_find_by_device_name(name);
77         if (bus == NULL) {
78                 RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
79                         name);
80                 return -EINVAL;
81         }
82         if (strcmp(bus->name, "pci") == 0 || strcmp(bus->name, "vdev") == 0)
83                 return rte_eal_hotplug_add(bus->name, name, devargs);
84
85         RTE_LOG(ERR, EAL,
86                 "Device attach is only supported for PCI and vdev devices.\n");
87
88         return -ENOTSUP;
89 }
90
91 int rte_eal_dev_detach(struct rte_device *dev)
92 {
93         struct rte_bus *bus;
94         int ret;
95
96         if (dev == NULL) {
97                 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
98                 return -EINVAL;
99         }
100
101         bus = rte_bus_find_by_device(dev);
102         if (bus == NULL) {
103                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
104                         dev->name);
105                 return -EINVAL;
106         }
107
108         if (bus->unplug == NULL) {
109                 RTE_LOG(ERR, EAL, "Bus function not supported\n");
110                 return -ENOTSUP;
111         }
112
113         ret = bus->unplug(dev);
114         if (ret)
115                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
116                         dev->name);
117         return ret;
118 }
119
120 static char *
121 full_dev_name(const char *bus, const char *dev, const char *args)
122 {
123         char *name;
124         size_t len;
125
126         len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args) + 1;
127         name = calloc(1, len);
128         if (name == NULL) {
129                 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
130                 return NULL;
131         }
132         snprintf(name, len, "%s:%s,%s", bus, dev, args);
133         return name;
134 }
135
136 int rte_eal_hotplug_add(const char *busname, const char *devname,
137                         const char *devargs)
138 {
139         struct rte_bus *bus;
140         struct rte_device *dev;
141         struct rte_devargs *da;
142         char *name;
143         int ret;
144
145         bus = rte_bus_find_by_name(busname);
146         if (bus == NULL) {
147                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
148                 return -ENOENT;
149         }
150
151         if (bus->plug == NULL) {
152                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
153                         bus->name);
154                 return -ENOTSUP;
155         }
156
157         name = full_dev_name(busname, devname, devargs);
158         if (name == NULL)
159                 return -ENOMEM;
160
161         da = calloc(1, sizeof(*da));
162         if (da == NULL) {
163                 ret = -ENOMEM;
164                 goto err_name;
165         }
166
167         ret = rte_eal_devargs_parse(name, da);
168         if (ret)
169                 goto err_devarg;
170
171         ret = rte_eal_devargs_insert(da);
172         if (ret)
173                 goto err_devarg;
174
175         ret = bus->scan();
176         if (ret)
177                 goto err_devarg;
178
179         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
180         if (dev == NULL) {
181                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
182                         devname);
183                 ret = -ENODEV;
184                 goto err_devarg;
185         }
186
187         ret = bus->plug(dev);
188         if (ret) {
189                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
190                         dev->name);
191                 goto err_devarg;
192         }
193         free(name);
194         return 0;
195
196 err_devarg:
197         if (rte_eal_devargs_remove(busname, devname)) {
198                 free(da->args);
199                 free(da);
200         }
201 err_name:
202         free(name);
203         return ret;
204 }
205
206 int rte_eal_hotplug_remove(const char *busname, const char *devname)
207 {
208         struct rte_bus *bus;
209         struct rte_device *dev;
210         int ret;
211
212         bus = rte_bus_find_by_name(busname);
213         if (bus == NULL) {
214                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
215                 return -ENOENT;
216         }
217
218         if (bus->unplug == NULL) {
219                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
220                         bus->name);
221                 return -ENOTSUP;
222         }
223
224         dev = bus->find_device(NULL, cmp_dev_name, devname);
225         if (dev == NULL) {
226                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
227                 return -EINVAL;
228         }
229
230         ret = bus->unplug(dev);
231         if (ret)
232                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
233                         dev->name);
234         rte_eal_devargs_remove(busname, devname);
235         return ret;
236 }