New upstream version 18.08
[deb_dpdk.git] / 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
41 #include "eal_private.h"
42
43 struct rte_bus_list rte_bus_list =
44         TAILQ_HEAD_INITIALIZER(rte_bus_list);
45
46 void
47 rte_bus_register(struct rte_bus *bus)
48 {
49         RTE_VERIFY(bus);
50         RTE_VERIFY(bus->name && strlen(bus->name));
51         /* A bus should mandatorily have the scan implemented */
52         RTE_VERIFY(bus->scan);
53         RTE_VERIFY(bus->probe);
54         RTE_VERIFY(bus->find_device);
55         /* Buses supporting driver plug also require unplug. */
56         RTE_VERIFY(!bus->plug || bus->unplug);
57
58         TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
59         RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
60 }
61
62 void
63 rte_bus_unregister(struct rte_bus *bus)
64 {
65         TAILQ_REMOVE(&rte_bus_list, bus, next);
66         RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
67 }
68
69 /* Scan all the buses for registered devices */
70 int
71 rte_bus_scan(void)
72 {
73         int ret;
74         struct rte_bus *bus = NULL;
75
76         TAILQ_FOREACH(bus, &rte_bus_list, next) {
77                 ret = bus->scan();
78                 if (ret)
79                         RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
80                                 bus->name);
81         }
82
83         return 0;
84 }
85
86 /* Probe all devices of all buses */
87 int
88 rte_bus_probe(void)
89 {
90         int ret;
91         struct rte_bus *bus, *vbus = NULL;
92
93         TAILQ_FOREACH(bus, &rte_bus_list, next) {
94                 if (!strcmp(bus->name, "vdev")) {
95                         vbus = bus;
96                         continue;
97                 }
98
99                 ret = bus->probe();
100                 if (ret)
101                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
102                                 bus->name);
103         }
104
105         if (vbus) {
106                 ret = vbus->probe();
107                 if (ret)
108                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
109                                 vbus->name);
110         }
111
112         return 0;
113 }
114
115 /* Dump information of a single bus */
116 static int
117 bus_dump_one(FILE *f, struct rte_bus *bus)
118 {
119         int ret;
120
121         /* For now, dump only the bus name */
122         ret = fprintf(f, " %s\n", bus->name);
123
124         /* Error in case of inability in writing to stream */
125         if (ret < 0)
126                 return ret;
127
128         return 0;
129 }
130
131 void
132 rte_bus_dump(FILE *f)
133 {
134         int ret;
135         struct rte_bus *bus;
136
137         TAILQ_FOREACH(bus, &rte_bus_list, next) {
138                 ret = bus_dump_one(f, bus);
139                 if (ret) {
140                         RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
141                                 ret);
142                         break;
143                 }
144         }
145 }
146
147 struct rte_bus *
148 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
149              const void *data)
150 {
151         struct rte_bus *bus;
152
153         if (start != NULL)
154                 bus = TAILQ_NEXT(start, next);
155         else
156                 bus = TAILQ_FIRST(&rte_bus_list);
157         while (bus != NULL) {
158                 if (cmp(bus, data) == 0)
159                         break;
160                 bus = TAILQ_NEXT(bus, next);
161         }
162         return bus;
163 }
164
165 static int
166 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
167 {
168         const struct rte_device *dev2 = _dev2;
169
170         return dev1 != dev2;
171 }
172
173 static int
174 bus_find_device(const struct rte_bus *bus, const void *_dev)
175 {
176         struct rte_device *dev;
177
178         dev = bus->find_device(NULL, cmp_rte_device, _dev);
179         return dev == NULL;
180 }
181
182 struct rte_bus *
183 rte_bus_find_by_device(const struct rte_device *dev)
184 {
185         return rte_bus_find(NULL, bus_find_device, (const void *)dev);
186 }
187
188 static int
189 cmp_bus_name(const struct rte_bus *bus, const void *_name)
190 {
191         const char *name = _name;
192
193         return strcmp(bus->name, name);
194 }
195
196 struct rte_bus *
197 rte_bus_find_by_name(const char *busname)
198 {
199         return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
200 }
201
202 static int
203 bus_can_parse(const struct rte_bus *bus, const void *_name)
204 {
205         const char *name = _name;
206
207         return !(bus->parse && bus->parse(name, NULL) == 0);
208 }
209
210 struct rte_bus *
211 rte_bus_find_by_device_name(const char *str)
212 {
213         char name[RTE_DEV_NAME_MAX_LEN];
214         char *c;
215
216         strlcpy(name, str, sizeof(name));
217         c = strchr(name, ',');
218         if (c != NULL)
219                 c[0] = '\0';
220         return rte_bus_find(NULL, bus_can_parse, name);
221 }
222
223
224 /*
225  * Get iommu class of devices on the bus.
226  */
227 enum rte_iova_mode
228 rte_bus_get_iommu_class(void)
229 {
230         int mode = RTE_IOVA_DC;
231         struct rte_bus *bus;
232
233         TAILQ_FOREACH(bus, &rte_bus_list, next) {
234
235                 if (bus->get_iommu_class)
236                         mode |= bus->get_iommu_class();
237         }
238
239         if (mode != RTE_IOVA_VA) {
240                 /* Use default IOVA mode */
241                 mode = RTE_IOVA_PA;
242         }
243         return mode;
244 }