87b0c6e6fa036102140a2668d44edd2f0f32a86f
[dpdk.git] / lib / librte_eal / common / eal_common_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 NXP
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of NXP nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/queue.h>
37
38 #include <rte_bus.h>
39
40 #include "eal_private.h"
41
42 struct rte_bus_list rte_bus_list =
43         TAILQ_HEAD_INITIALIZER(rte_bus_list);
44
45 void
46 rte_bus_register(struct rte_bus *bus)
47 {
48         RTE_VERIFY(bus);
49         RTE_VERIFY(bus->name && strlen(bus->name));
50         /* A bus should mandatorily have the scan implemented */
51         RTE_VERIFY(bus->scan);
52         RTE_VERIFY(bus->probe);
53         RTE_VERIFY(bus->find_device);
54         /* Buses supporting driver plug also require unplug. */
55         RTE_VERIFY(!bus->plug || bus->unplug);
56
57         TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
58         RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", bus->name);
59 }
60
61 void
62 rte_bus_unregister(struct rte_bus *bus)
63 {
64         TAILQ_REMOVE(&rte_bus_list, bus, next);
65         RTE_LOG(DEBUG, EAL, "Unregistered [%s] bus.\n", bus->name);
66 }
67
68 /* Scan all the buses for registered devices */
69 int
70 rte_bus_scan(void)
71 {
72         int ret;
73         struct rte_bus *bus = NULL;
74
75         TAILQ_FOREACH(bus, &rte_bus_list, next) {
76                 ret = bus->scan();
77                 if (ret) {
78                         RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
79                                 bus->name);
80                         return ret;
81                 }
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, "virtual")) {
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                         return ret;
105                 }
106         }
107
108         if (vbus) {
109                 ret = vbus->probe();
110                 if (ret) {
111                         RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
112                                 vbus->name);
113                         return ret;
114                 }
115         }
116
117         return 0;
118 }
119
120 /* Dump information of a single bus */
121 static int
122 bus_dump_one(FILE *f, struct rte_bus *bus)
123 {
124         int ret;
125
126         /* For now, dump only the bus name */
127         ret = fprintf(f, " %s\n", bus->name);
128
129         /* Error in case of inability in writing to stream */
130         if (ret < 0)
131                 return ret;
132
133         return 0;
134 }
135
136 void
137 rte_bus_dump(FILE *f)
138 {
139         int ret;
140         struct rte_bus *bus;
141
142         TAILQ_FOREACH(bus, &rte_bus_list, next) {
143                 ret = bus_dump_one(f, bus);
144                 if (ret) {
145                         RTE_LOG(ERR, EAL, "Unable to write to stream (%d)\n",
146                                 ret);
147                         break;
148                 }
149         }
150 }
151
152 struct rte_bus *
153 rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
154              const void *data)
155 {
156         struct rte_bus *bus = NULL;
157         bool start_found = !start;
158
159         TAILQ_FOREACH(bus, &rte_bus_list, next) {
160                 if (!start_found) {
161                         if (bus == start)
162                                 start_found = 1;
163                         continue;
164                 }
165                 if (cmp(bus, data) == 0)
166                         break;
167         }
168         return bus;
169 }
170
171 static int
172 cmp_rte_device(const struct rte_device *dev1, const void *_dev2)
173 {
174         const struct rte_device *dev2 = _dev2;
175
176         return dev1 != dev2;
177 }
178
179 static int
180 bus_find_device(const struct rte_bus *bus, const void *_dev)
181 {
182         struct rte_device *dev;
183
184         dev = bus->find_device(NULL, cmp_rte_device, _dev);
185         return dev == NULL;
186 }
187
188 struct rte_bus *
189 rte_bus_find_by_device(const struct rte_device *dev)
190 {
191         return rte_bus_find(NULL, bus_find_device, (const void *)dev);
192 }
193
194 static int
195 cmp_bus_name(const struct rte_bus *bus, const void *_name)
196 {
197         const char *name = _name;
198
199         return strcmp(bus->name, name);
200 }
201
202 struct rte_bus *
203 rte_bus_find_by_name(const char *busname)
204 {
205         return rte_bus_find(NULL, cmp_bus_name, (const void *)busname);
206 }