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