08bec2d931d10fbd001c6197f78e4c4257a44c0e
[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
39 #include "eal_private.h"
40
41 struct rte_bus_list rte_bus_list =
42         TAILQ_HEAD_INITIALIZER(rte_bus_list);
43
44 void
45 rte_bus_register(struct rte_bus *bus)
46 {
47         RTE_VERIFY(bus);
48         RTE_VERIFY(bus->name && strlen(bus->name));
49         /* A bus should mandatorily have the scan implemented */
50         RTE_VERIFY(bus->scan);
51         RTE_VERIFY(bus->probe);
52         RTE_VERIFY(bus->find_device);
53         /* Buses supporting driver plug also require unplug. */
54         RTE_VERIFY(!bus->plug || bus->unplug);
55
56         TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
57         RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
58 }
59
60 void
61 rte_bus_unregister(struct rte_bus *bus)
62 {
63         TAILQ_REMOVE(&rte_bus_list, bus, next);
64         RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
65 }
66
67 /* Scan all the buses for registered devices */
68 int
69 rte_bus_scan(void)
70 {
71         int ret;
72         struct rte_bus *bus = NULL;
73
74         TAILQ_FOREACH(bus, &rte_bus_list, next) {
75                 ret = bus->scan();
76                 if (ret) {
77                         RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
78                                 bus->name);
79                         return ret;
80                 }
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                         return ret;
104                 }
105         }
106
107         if (vbus) {
108                 ret = vbus->probe();
109                 if (ret) {
110                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
111                                 vbus->name);
112                         return ret;
113                 }
114         }
115
116         return 0;
117 }
118
119 /* Dump information of a single bus */
120 static int
121 bus_dump_one(FILE *f, struct rte_bus *bus)
122 {
123         int ret;
124
125         /* For now, dump only the bus name */
126         ret = fprintf(f, " %s\n", bus->name);
127
128         /* Error in case of inability in writing to stream */
129         if (ret < 0)
130                 return ret;
131
132         return 0;
133 }
134
135 void
136 rte_bus_dump(FILE *f)
137 {
138         int ret;
139         struct rte_bus *bus;
140
141         TAILQ_FOREACH(bus, &rte_bus_list, next) {
142                 ret = bus_dump_one(f, bus);
143                 if (ret) {
144                         RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
145                                 ret);
146                         break;
147                 }
148         }
149 }
150
151 struct rte_bus *
152 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
153              const void *data)
154 {
155         struct rte_bus *bus = NULL;
156
157         TAILQ_FOREACH(bus, &rte_bus_list, next) {
158                 if (start && bus == start) {
159                         start = NULL; /* starting point found */
160                         continue;
161                 }
162                 if (cmp(bus, data) == 0)
163                         break;
164         }
165         return bus;
166 }
167
168 static int
169 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
170 {
171         const struct rte_device *dev2 = _dev2;
172
173         return dev1 != dev2;
174 }
175
176 static int
177 bus_find_device(const struct rte_bus *bus, const void *_dev)
178 {
179         struct rte_device *dev;
180
181         dev = bus->find_device(NULL, cmp_rte_device, _dev);
182         return dev == NULL;
183 }
184
185 struct rte_bus *
186 rte_bus_find_by_device(const struct rte_device *dev)
187 {
188         return rte_bus_find(NULL, bus_find_device, (const void *)dev);
189 }
190
191 static int
192 cmp_bus_name(const struct rte_bus *bus, const void *_name)
193 {
194         const char *name = _name;
195
196         return strcmp(bus->name, name);
197 }
198
199 struct rte_bus *
200 rte_bus_find_by_name(const char *busname)
201 {
202         return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
203 }
204
205 static int
206 bus_can_parse(const struct rte_bus *bus, const void *_name)
207 {
208         const char *name = _name;
209
210         return !(bus->parse && bus->parse(name, NULL) == 0);
211 }
212
213 struct rte_bus *
214 rte_bus_find_by_device_name(const char *str)
215 {
216         char name[RTE_DEV_NAME_MAX_LEN];
217         char *c;
218
219         snprintf(name, sizeof(name), "%s", str);
220         c = strchr(name, ',');
221         if (c != NULL)
222                 c[0] = '\0';
223         return rte_bus_find(NULL, bus_can_parse, name);
224 }