]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_eal/common/eal_common_bus.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / dpdk / lib / librte_eal / common / eal_common_bus.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright 2016 NXP.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of NXP nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/queue.h>
36
37 #include <rte_bus.h>
38 #include <rte_debug.h>
39 #include <rte_string_fns.h>
40 #include <rte_errno.h>
41
42 #include "eal_private.h"
43
44 static struct rte_bus_list rte_bus_list =
45 TAILQ_HEAD_INITIALIZER(rte_bus_list);
46
47 void
48 rte_bus_register(struct rte_bus *bus)
49 {
50 RTE_VERIFY(bus);
51 RTE_VERIFY(bus->name && strlen(bus->name));
52 /* A bus should mandatorily have the scan implemented */
53 RTE_VERIFY(bus->scan);
54 RTE_VERIFY(bus->probe);
55 RTE_VERIFY(bus->find_device);
56 /* Buses supporting driver plug also require unplug. */
57 RTE_VERIFY(!bus->plug || bus->unplug);
58
59 TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
60 RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
61 }
62
63 void
64 rte_bus_unregister(struct rte_bus *bus)
65 {
66 TAILQ_REMOVE(&rte_bus_list, bus, next);
67 RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
68 }
69
70 /* Scan all the buses for registered devices */
71 int
72 rte_bus_scan(void)
73 {
74 int ret;
75 struct rte_bus *bus = NULL;
76
77 TAILQ_FOREACH(bus, &rte_bus_list, next) {
78 ret = bus->scan();
79 if (ret)
80 RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
81 bus->name);
82 }
83
84 return 0;
85 }
86
87 /* Probe all devices of all buses */
88 int
89 rte_bus_probe(void)
90 {
91 int ret;
92 struct rte_bus *bus, *vbus = NULL;
93
94 TAILQ_FOREACH(bus, &rte_bus_list, next) {
95 if (!strcmp(bus->name, "vdev")) {
96 vbus = bus;
97 continue;
98 }
99
100 ret = bus->probe();
101 if (ret)
102 RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
103 bus->name);
104 }
105
106 if (vbus) {
107 ret = vbus->probe();
108 if (ret)
109 RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
110 vbus->name);
111 }
112
113 return 0;
114 }
115
116 /* Dump information of a single bus */
117 static int
118 bus_dump_one(FILE *f, struct rte_bus *bus)
119 {
120 int ret;
121
122 /* For now, dump only the bus name */
123 ret = fprintf(f, " %s\n", bus->name);
124
125 /* Error in case of inability in writing to stream */
126 if (ret < 0)
127 return ret;
128
129 return 0;
130 }
131
132 void
133 rte_bus_dump(FILE *f)
134 {
135 int ret;
136 struct rte_bus *bus;
137
138 TAILQ_FOREACH(bus, &rte_bus_list, next) {
139 ret = bus_dump_one(f, bus);
140 if (ret) {
141 RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
142 ret);
143 break;
144 }
145 }
146 }
147
148 struct rte_bus *
149 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
150 const void *data)
151 {
152 struct rte_bus *bus;
153
154 if (start != NULL)
155 bus = TAILQ_NEXT(start, next);
156 else
157 bus = TAILQ_FIRST(&rte_bus_list);
158 while (bus != NULL) {
159 if (cmp(bus, data) == 0)
160 break;
161 bus = TAILQ_NEXT(bus, next);
162 }
163 return bus;
164 }
165
166 static int
167 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
168 {
169 const struct rte_device *dev2 = _dev2;
170
171 return dev1 != dev2;
172 }
173
174 static int
175 bus_find_device(const struct rte_bus *bus, const void *_dev)
176 {
177 struct rte_device *dev;
178
179 dev = bus->find_device(NULL, cmp_rte_device, _dev);
180 return dev == NULL;
181 }
182
183 struct rte_bus *
184 rte_bus_find_by_device(const struct rte_device *dev)
185 {
186 return rte_bus_find(NULL, bus_find_device, (const void *)dev);
187 }
188
189 static int
190 cmp_bus_name(const struct rte_bus *bus, const void *_name)
191 {
192 const char *name = _name;
193
194 return strcmp(bus->name, name);
195 }
196
197 struct rte_bus *
198 rte_bus_find_by_name(const char *busname)
199 {
200 return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
201 }
202
203 static int
204 bus_can_parse(const struct rte_bus *bus, const void *_name)
205 {
206 const char *name = _name;
207
208 return !(bus->parse && bus->parse(name, NULL) == 0);
209 }
210
211 struct rte_bus *
212 rte_bus_find_by_device_name(const char *str)
213 {
214 char name[RTE_DEV_NAME_MAX_LEN];
215 char *c;
216
217 strlcpy(name, str, sizeof(name));
218 c = strchr(name, ',');
219 if (c != NULL)
220 c[0] = '\0';
221 return rte_bus_find(NULL, bus_can_parse, name);
222 }
223
224
225 /*
226 * Get iommu class of devices on the bus.
227 */
228 enum rte_iova_mode
229 rte_bus_get_iommu_class(void)
230 {
231 int mode = RTE_IOVA_DC;
232 struct rte_bus *bus;
233
234 TAILQ_FOREACH(bus, &rte_bus_list, next) {
235
236 if (bus->get_iommu_class)
237 mode |= bus->get_iommu_class();
238 }
239
240 if (mode != RTE_IOVA_VA) {
241 /* Use default IOVA mode */
242 mode = RTE_IOVA_PA;
243 }
244 return mode;
245 }
246
247 static int
248 bus_handle_sigbus(const struct rte_bus *bus,
249 const void *failure_addr)
250 {
251 int ret;
252
253 if (!bus->sigbus_handler)
254 return -1;
255
256 ret = bus->sigbus_handler(failure_addr);
257
258 /* find bus but handle failed, keep the errno be set. */
259 if (ret < 0 && rte_errno == 0)
260 rte_errno = ENOTSUP;
261
262 return ret > 0;
263 }
264
265 int
266 rte_bus_sigbus_handler(const void *failure_addr)
267 {
268 struct rte_bus *bus;
269
270 int ret = 0;
271 int old_errno = rte_errno;
272
273 rte_errno = 0;
274
275 bus = rte_bus_find(NULL, bus_handle_sigbus, failure_addr);
276 /* can not find bus. */
277 if (!bus)
278 return 1;
279 /* find bus but handle failed, pass on the new errno. */
280 else if (rte_errno != 0)
281 return -1;
282
283 /* restore the old errno. */
284 rte_errno = old_errno;
285
286 return ret;
287 }