ethdev: use device handle to detach
[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_devargs.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_pci_addr addr;
71
72         if (name == NULL || devargs == NULL) {
73                 RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
74                 return -EINVAL;
75         }
76
77         if (eal_parse_pci_DomBDF(name, &addr) == 0) {
78                 if (rte_pci_probe_one(&addr) < 0)
79                         goto err;
80
81         } else {
82                 if (rte_vdev_init(name, devargs))
83                         goto err;
84         }
85
86         return 0;
87
88 err:
89         RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n", name);
90         return -EINVAL;
91 }
92
93 int rte_eal_dev_detach(struct rte_device *dev)
94 {
95         struct rte_bus *bus;
96         int ret;
97
98         if (dev == NULL) {
99                 RTE_LOG(ERR, EAL, "Invalid device provided.\n");
100                 return -EINVAL;
101         }
102
103         bus = rte_bus_find_by_device(dev);
104         if (bus == NULL) {
105                 RTE_LOG(ERR, EAL, "Cannot find bus for device (%s)\n",
106                         dev->name);
107                 return -EINVAL;
108         }
109
110         if (bus->unplug == NULL) {
111                 RTE_LOG(ERR, EAL, "Bus function not supported\n");
112                 return -ENOTSUP;
113         }
114
115         ret = bus->unplug(dev);
116         if (ret)
117                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
118                         dev->name);
119         return ret;
120 }
121
122 int rte_eal_hotplug_add(const char *busname, const char *devname,
123                         const char *devargs)
124 {
125         struct rte_bus *bus;
126         struct rte_device *dev;
127         int ret;
128
129         bus = rte_bus_find_by_name(busname);
130         if (bus == NULL) {
131                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
132                 return -ENOENT;
133         }
134
135         if (bus->plug == NULL) {
136                 RTE_LOG(ERR, EAL, "Function plug not supported by bus (%s)\n",
137                         bus->name);
138                 return -ENOTSUP;
139         }
140
141         ret = bus->scan();
142         if (ret)
143                 return ret;
144
145         dev = bus->find_device(NULL, cmp_detached_dev_name, devname);
146         if (dev == NULL) {
147                 RTE_LOG(ERR, EAL, "Cannot find unplugged device (%s)\n",
148                         devname);
149                 return -EINVAL;
150         }
151
152         ret = bus->plug(dev, devargs);
153         if (ret)
154                 RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n",
155                         dev->name);
156         return ret;
157 }
158
159 int rte_eal_hotplug_remove(const char *busname, const char *devname)
160 {
161         struct rte_bus *bus;
162         struct rte_device *dev;
163         int ret;
164
165         bus = rte_bus_find_by_name(busname);
166         if (bus == NULL) {
167                 RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname);
168                 return -ENOENT;
169         }
170
171         if (bus->unplug == NULL) {
172                 RTE_LOG(ERR, EAL, "Function unplug not supported by bus (%s)\n",
173                         bus->name);
174                 return -ENOTSUP;
175         }
176
177         dev = bus->find_device(NULL, cmp_dev_name, devname);
178         if (dev == NULL) {
179                 RTE_LOG(ERR, EAL, "Cannot find plugged device (%s)\n", devname);
180                 return -EINVAL;
181         }
182
183         ret = bus->unplug(dev);
184         if (ret)
185                 RTE_LOG(ERR, EAL, "Driver cannot detach the device (%s)\n",
186                         dev->name);
187         return ret;
188 }