dev: fix attach proceeding with vdev on PCI success
[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         int ret;
70
71         if (name == NULL || devargs == NULL) {
72                 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
73                 return -EINVAL;
74         }
75
76         ret = rte_eal_hotplug_add("pci", name, devargs);
77         if (ret != -EINVAL)
78                 return ret;
79
80         /*
81          * If we haven't found a bus device the user meant to "hotplug" a
82          * virtual device instead.
83          */
84         ret = rte_vdev_init(name, devargs);
85         if (ret)
86                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
87                         name);
88         return ret;
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 = strlen(bus) + 1 +
127               strlen(dev) + 1 +
128               strlen(args) + 1;
129         name = calloc(1, len);
130         if (name == NULL) {
131                 RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
132                 return NULL;
133         }
134         snprintf(name, len, "%s:%s,%s", bus, dev,
135                  args ? args : "");
136         return name;
137 }
138
139 int rte_eal_hotplug_add(const char *busname, const char *devname,
140                         const char *devargs)
141 {
142         struct rte_bus *bus;
143         struct rte_device *dev;
144         struct rte_devargs *da;
145         char *name;
146         int ret;
147
148         bus = rte_bus_find_by_name(busname);
149         if (bus == NULL) {
150                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
151                 return -ENOENT;
152         }
153
154         if (bus->plug == NULL) {
155                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
156                         bus->name);
157                 return -ENOTSUP;
158         }
159
160         name = full_dev_name(busname, devname, devargs);
161         if (name == NULL)
162                 return -ENOMEM;
163
164         da = calloc(1, sizeof(*da));
165         if (da == NULL) {
166                 ret = -ENOMEM;
167                 goto err_name;
168         }
169
170         ret = rte_eal_devargs_parse(name, da);
171         if (ret)
172                 goto err_devarg;
173
174         ret = rte_eal_devargs_insert(da);
175         if (ret)
176                 goto err_devarg;
177
178         ret = bus->scan();
179         if (ret)
180                 goto err_devarg;
181
182         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
183         if (dev == NULL) {
184                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
185                         devname);
186                 ret = -ENODEV;
187                 goto err_devarg;
188         }
189
190         ret = bus->plug(dev);
191         if (ret) {
192                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
193                         dev->name);
194                 goto err_devarg;
195         }
196         free(name);
197         return 0;
198
199 err_devarg:
200         rte_eal_devargs_remove(busname, devname);
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 }