lib: remove duplicate includes
[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 && 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 int rte_eal_hotplug_add(const char *busname, const char *devname,
121                         const char *devargs)
122 {
123         struct rte_bus *bus;
124         struct rte_device *dev;
125         int ret;
126
127         bus = rte_bus_find_by_name(busname);
128         if (bus == NULL) {
129                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
130                 return -ENOENT;
131         }
132
133         if (bus->plug == NULL) {
134                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
135                         bus->name);
136                 return -ENOTSUP;
137         }
138
139         ret = bus->scan();
140         if (ret)
141                 return ret;
142
143         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
144         if (dev == NULL) {
145                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
146                         devname);
147                 return -EINVAL;
148         }
149
150         ret = bus->plug(dev, devargs);
151         if (ret)
152                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
153                         dev->name);
154         return ret;
155 }
156
157 int rte_eal_hotplug_remove(const char *busname, const char *devname)
158 {
159         struct rte_bus *bus;
160         struct rte_device *dev;
161         int ret;
162
163         bus = rte_bus_find_by_name(busname);
164         if (bus == NULL) {
165                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
166                 return -ENOENT;
167         }
168
169         if (bus->unplug == NULL) {
170                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
171                         bus->name);
172                 return -ENOTSUP;
173         }
174
175         dev = bus->find_device(NULL, cmp_dev_name, devname);
176         if (dev == NULL) {
177                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
178                 return -EINVAL;
179         }
180
181         ret = bus->unplug(dev);
182         if (ret)
183                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
184                         dev->name);
185         return ret;
186 }