aebf57efdd469e93e456fcbd4306cfea380d9d7f
[dpdk.git] / lib / librte_eal / common / include / rte_bus.h
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 #ifndef _RTE_BUS_H_
35 #define _RTE_BUS_H_
36
37 /**
38  * @file
39  *
40  * DPDK device bus interface
41  *
42  * This file exposes API and interfaces for bus abstraction
43  * over the devices and drivers in EAL.
44  */
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #include <stdio.h>
51 #include <sys/queue.h>
52
53 #include <rte_log.h>
54 #include <rte_dev.h>
55
56 /** Double linked list of buses */
57 TAILQ_HEAD(rte_bus_list, rte_bus);
58
59 /**
60  * Bus specific scan for devices attached on the bus.
61  * For each bus object, the scan would be responsible for finding devices and
62  * adding them to its private device list.
63  *
64  * A bus should mandatorily implement this method.
65  *
66  * @return
67  *      0 for successful scan
68  *      <0 for unsuccessful scan with error value
69  */
70 typedef int (*rte_bus_scan_t)(void);
71
72 /**
73  * Implementation specific probe function which is responsible for linking
74  * devices on that bus with applicable drivers.
75  *
76  * This is called while iterating over each registered bus.
77  *
78  * @return
79  *      0 for successful probe
80  *      !0 for any error while probing
81  */
82 typedef int (*rte_bus_probe_t)(void);
83
84 /**
85  * Device iterator to find a device on a bus.
86  *
87  * This function returns an rte_device if one of those held by the bus
88  * matches the data passed as parameter.
89  *
90  * If the comparison function returns zero this function should stop iterating
91  * over any more devices. To continue a search the device of a previous search
92  * can be passed via the start parameter.
93  *
94  * @param cmp
95  *      Comparison function.
96  *
97  * @param data
98  *      Data to compare each device against.
99  *
100  * @param start
101  *      starting point for the iteration
102  *
103  * @return
104  *      The first device matching the data, NULL if none exists.
105  */
106 typedef struct rte_device *
107 (*rte_bus_find_device_t)(const struct rte_device *start, rte_dev_cmp_t cmp,
108                          const void *data);
109
110 /**
111  * Implementation specific probe function which is responsible for linking
112  * devices on that bus with applicable drivers.
113  *
114  * @param dev
115  *      Device pointer that was returned by a previous call to find_device.
116  *
117  * @param devargs
118  *      Device declaration.
119  *
120  * @return
121  *      0 on success.
122  *      !0 on error.
123  */
124 typedef int (*rte_bus_plug_t)(struct rte_device *dev,
125                               const char *devargs);
126
127 /**
128  * Implementation specific remove function which is responsible for unlinking
129  * devices on that bus from assigned driver.
130  *
131  * @param dev
132  *      Device pointer that was returned by a previous call to find_device.
133  *
134  * @return
135  *      0 on success.
136  *      !0 on error.
137  */
138 typedef int (*rte_bus_unplug_t)(struct rte_device *dev);
139
140 /**
141  * Bus specific parsing function.
142  * Validates the syntax used in the textual representation of a device,
143  * If the syntax is valid and ``addr`` is not NULL, writes the bus-specific
144  * device representation to ``addr``.
145  *
146  * @param[in] name
147  *      device textual description
148  *
149  * @param[out] addr
150  *      device information location address, into which parsed info
151  *      should be written. If NULL, nothing should be written, which
152  *      is not an error.
153  *
154  * @return
155  *      0 if parsing was successful.
156  *      !0 for any error.
157  */
158 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
159
160 /**
161  * A structure describing a generic bus.
162  */
163 struct rte_bus {
164         TAILQ_ENTRY(rte_bus) next;   /**< Next bus object in linked list */
165         const char *name;            /**< Name of the bus */
166         rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
167         rte_bus_probe_t probe;       /**< Probe devices on bus */
168         rte_bus_find_device_t find_device; /**< Find a device on the bus */
169         rte_bus_plug_t plug;         /**< Probe single device for drivers */
170         rte_bus_unplug_t unplug;     /**< Remove single device from driver */
171         rte_bus_parse_t parse;       /**< Parse a device name */
172 };
173
174 /**
175  * Register a Bus handler.
176  *
177  * @param bus
178  *   A pointer to a rte_bus structure describing the bus
179  *   to be registered.
180  */
181 void rte_bus_register(struct rte_bus *bus);
182
183 /**
184  * Unregister a Bus handler.
185  *
186  * @param bus
187  *   A pointer to a rte_bus structure describing the bus
188  *   to be unregistered.
189  */
190 void rte_bus_unregister(struct rte_bus *bus);
191
192 /**
193  * Scan all the buses.
194  *
195  * @return
196  *   0 in case of success in scanning all buses
197  *  !0 in case of failure to scan
198  */
199 int rte_bus_scan(void);
200
201 /**
202  * For each device on the buses, perform a driver 'match' and call the
203  * driver-specific probe for device initialization.
204  *
205  * @return
206  *       0 for successful match/probe
207  *      !0 otherwise
208  */
209 int rte_bus_probe(void);
210
211 /**
212  * Dump information of all the buses registered with EAL.
213  *
214  * @param f
215  *       A valid and open output stream handle
216  *
217  * @return
218  *       0 in case of success
219  *      !0 in case there is error in opening the output stream
220  */
221 void rte_bus_dump(FILE *f);
222
223 /**
224  * Bus comparison function.
225  *
226  * @param bus
227  *      Bus under test.
228  *
229  * @param data
230  *      Data to compare against.
231  *
232  * @return
233  *      0 if the bus matches the data.
234  *      !0 if the bus does not match.
235  *      <0 if ordering is possible and the bus is lower than the data.
236  *      >0 if ordering is possible and the bus is greater than the data.
237  */
238 typedef int (*rte_bus_cmp_t)(const struct rte_bus *bus, const void *data);
239
240 /**
241  * Bus iterator to find a particular bus.
242  *
243  * This function compares each registered bus to find one that matches
244  * the data passed as parameter.
245  *
246  * If the comparison function returns zero this function will stop iterating
247  * over any more buses. To continue a search the bus of a previous search can
248  * be passed via the start parameter.
249  *
250  * @param start
251  *      Starting point for the iteration.
252  *
253  * @param cmp
254  *      Comparison function.
255  *
256  * @param data
257  *       Data to pass to comparison function.
258  *
259  * @return
260  *       A pointer to a rte_bus structure or NULL in case no bus matches
261  */
262 struct rte_bus *rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
263                              const void *data);
264
265 /**
266  * Find the registered bus for a particular device.
267  */
268 struct rte_bus *rte_bus_find_by_device(const struct rte_device *dev);
269
270 /**
271  * Find the registered bus for a given name.
272  */
273 struct rte_bus *rte_bus_find_by_name(const char *busname);
274
275 /**
276  * Helper for Bus registration.
277  * The constructor has higher priority than PMD constructors.
278  */
279 #define RTE_REGISTER_BUS(nm, bus) \
280 RTE_INIT_PRIO(businitfn_ ##nm, 101); \
281 static void businitfn_ ##nm(void) \
282 {\
283         (bus).name = RTE_STR(nm);\
284         rte_bus_register(&bus); \
285 }
286
287 #ifdef __cplusplus
288 }
289 #endif
290
291 #endif /* _RTE_BUS_H */