eal: add device iterator interface
[dpdk.git] / lib / librte_eal / common / include / rte_dev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014 6WIND S.A.
3  */
4
5 #ifndef _RTE_DEV_H_
6 #define _RTE_DEV_H_
7
8 /**
9  * @file
10  *
11  * RTE PMD Driver Registration Interface
12  *
13  * This file manages the list of device drivers.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdio.h>
21 #include <sys/queue.h>
22
23 #include <rte_config.h>
24 #include <rte_compat.h>
25 #include <rte_log.h>
26
27 /**
28  * The device event type.
29  */
30 enum rte_dev_event_type {
31         RTE_DEV_EVENT_ADD,      /**< device being added */
32         RTE_DEV_EVENT_REMOVE,   /**< device being removed */
33         RTE_DEV_EVENT_MAX       /**< max value of this enum */
34 };
35
36 struct rte_dev_event {
37         enum rte_dev_event_type type;   /**< device event type */
38         int subsystem;                  /**< subsystem id */
39         char *devname;                  /**< device name */
40 };
41
42 typedef void (*rte_dev_event_cb_fn)(char *device_name,
43                                         enum rte_dev_event_type event,
44                                         void *cb_arg);
45
46 __attribute__((format(printf, 2, 0)))
47 static inline void
48 rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
49 {
50         va_list ap;
51
52         va_start(ap, fmt);
53
54         {
55                 char buffer[vsnprintf(NULL, 0, fmt, ap) + 1];
56
57                 va_end(ap);
58
59                 va_start(ap, fmt);
60                 vsnprintf(buffer, sizeof(buffer), fmt, ap);
61                 va_end(ap);
62
63                 rte_log(RTE_LOG_ERR, RTE_LOGTYPE_PMD, "%s: %s",
64                         func_name, buffer);
65         }
66 }
67
68 /*
69  * Enable RTE_PMD_DEBUG_TRACE() when at least one component relying on the
70  * RTE_*_RET() macros defined below is compiled in debug mode.
71  */
72 #if defined(RTE_LIBRTE_EVENTDEV_DEBUG)
73 #define RTE_PMD_DEBUG_TRACE(...) \
74         rte_pmd_debug_trace(__func__, __VA_ARGS__)
75 #else
76 #define RTE_PMD_DEBUG_TRACE(...) (void)0
77 #endif
78
79 /* Macros for checking for restricting functions to primary instance only */
80 #define RTE_PROC_PRIMARY_OR_ERR_RET(retval) do { \
81         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
82                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
83                 return retval; \
84         } \
85 } while (0)
86
87 #define RTE_PROC_PRIMARY_OR_RET() do { \
88         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
89                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
90                 return; \
91         } \
92 } while (0)
93
94 /* Macros to check for invalid function pointers */
95 #define RTE_FUNC_PTR_OR_ERR_RET(func, retval) do { \
96         if ((func) == NULL) { \
97                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
98                 return retval; \
99         } \
100 } while (0)
101
102 #define RTE_FUNC_PTR_OR_RET(func) do { \
103         if ((func) == NULL) { \
104                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
105                 return; \
106         } \
107 } while (0)
108
109 /**
110  * Device driver.
111  */
112 enum rte_kernel_driver {
113         RTE_KDRV_UNKNOWN = 0,
114         RTE_KDRV_IGB_UIO,
115         RTE_KDRV_VFIO,
116         RTE_KDRV_UIO_GENERIC,
117         RTE_KDRV_NIC_UIO,
118         RTE_KDRV_NONE,
119 };
120
121 /**
122  * Device policies.
123  */
124 enum rte_dev_policy {
125         RTE_DEV_WHITELISTED,
126         RTE_DEV_BLACKLISTED,
127 };
128
129 /**
130  * A generic memory resource representation.
131  */
132 struct rte_mem_resource {
133         uint64_t phys_addr; /**< Physical address, 0 if not resource. */
134         uint64_t len;       /**< Length of the resource. */
135         void *addr;         /**< Virtual address, NULL when not mapped. */
136 };
137
138 /**
139  * A structure describing a device driver.
140  */
141 struct rte_driver {
142         TAILQ_ENTRY(rte_driver) next;  /**< Next in list. */
143         const char *name;                   /**< Driver name. */
144         const char *alias;              /**< Driver alias. */
145 };
146
147 /*
148  * Internal identifier length
149  * Sufficiently large to allow for UUID or PCI address
150  */
151 #define RTE_DEV_NAME_MAX_LEN 64
152
153 /**
154  * A structure describing a generic device.
155  */
156 struct rte_device {
157         TAILQ_ENTRY(rte_device) next; /**< Next device */
158         const char *name;             /**< Device name */
159         const struct rte_driver *driver;/**< Associated driver */
160         int numa_node;                /**< NUMA node connection */
161         struct rte_devargs *devargs;  /**< Device user arguments */
162 };
163
164 /**
165  * Attach a device to a registered driver.
166  *
167  * @param name
168  *   The device name, that refers to a pci device (or some private
169  *   way of designating a vdev device). Based on this device name, eal
170  *   will identify a driver capable of handling it and pass it to the
171  *   driver probing function.
172  * @param devargs
173  *   Device arguments to be passed to the driver.
174  * @return
175  *   0 on success, negative on error.
176  */
177 int rte_eal_dev_attach(const char *name, const char *devargs);
178
179 /**
180  * Detach a device from its driver.
181  *
182  * @param dev
183  *   A pointer to a rte_device structure.
184  * @return
185  *   0 on success, negative on error.
186  */
187 int rte_eal_dev_detach(struct rte_device *dev);
188
189 /**
190  * @warning
191  * @b EXPERIMENTAL: this API may change without prior notice
192  *
193  * Hotplug add a given device to a specific bus.
194  *
195  * @param busname
196  *   The bus name the device is added to.
197  * @param devname
198  *   The device name. Based on this device name, eal will identify a driver
199  *   capable of handling it and pass it to the driver probing function.
200  * @param devargs
201  *   Device arguments to be passed to the driver.
202  * @return
203  *   0 on success, negative on error.
204  */
205 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
206                         const char *devargs);
207
208 /**
209  * @warning
210  * @b EXPERIMENTAL: this API may change without prior notice
211  *
212  * Hotplug remove a given device from a specific bus.
213  *
214  * @param busname
215  *   The bus name the device is removed from.
216  * @param devname
217  *   The device name being removed.
218  * @return
219  *   0 on success, negative on error.
220  */
221 int __rte_experimental rte_eal_hotplug_remove(const char *busname,
222                                           const char *devname);
223
224 /**
225  * Device comparison function.
226  *
227  * This type of function is used to compare an rte_device with arbitrary
228  * data.
229  *
230  * @param dev
231  *   Device handle.
232  *
233  * @param data
234  *   Data to compare against. The type of this parameter is determined by
235  *   the kind of comparison performed by the function.
236  *
237  * @return
238  *   0 if the device matches the data.
239  *   !0 if the device does not match.
240  *   <0 if ordering is possible and the device is lower than the data.
241  *   >0 if ordering is possible and the device is greater than the data.
242  */
243 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
244
245 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
246
247 #define RTE_PMD_EXPORT_NAME(name, idx) \
248 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
249 __attribute__((used)) = RTE_STR(name)
250
251 #define DRV_EXP_TAG(name, tag) __##name##_##tag
252
253 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
254 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
255 RTE_STR(table)
256
257 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
258 static const char DRV_EXP_TAG(name, param_string_export)[] \
259 __attribute__((used)) = str
260
261 /**
262  * Advertise the list of kernel modules required to run this driver
263  *
264  * This string lists the kernel modules required for the devices
265  * associated to a PMD. The format of each line of the string is:
266  * "<device-pattern> <kmod-expression>".
267  *
268  * The possible formats for the device pattern are:
269  *   "*"                     all devices supported by this driver
270  *   "pci:*"                 all PCI devices supported by this driver
271  *   "pci:v8086:d*:sv*:sd*"  all PCI devices supported by this driver
272  *                           whose vendor id is 0x8086.
273  *
274  * The format of the kernel modules list is a parenthesed expression
275  * containing logical-and (&) and logical-or (|).
276  *
277  * The device pattern and the kmod expression are separated by a space.
278  *
279  * Example:
280  * - "* igb_uio | uio_pci_generic | vfio"
281  */
282 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
283 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
284 __attribute__((used)) = str
285
286 /**
287  * Iteration context.
288  *
289  * This context carries over the current iteration state.
290  */
291 struct rte_dev_iterator {
292         const char *dev_str; /**< device string. */
293         const char *bus_str; /**< bus-related part of device string. */
294         const char *cls_str; /**< class-related part of device string. */
295         struct rte_bus *bus; /**< bus handle. */
296         struct rte_class *cls; /**< class handle. */
297         struct rte_device *device; /**< current position. */
298         void *class_device; /**< additional specialized context. */
299 };
300
301 /**
302  * Device iteration function.
303  *
304  * Find the next device matching properties passed in parameters.
305  * The function takes an additional ``start`` parameter, that is
306  * used as starting context when relevant.
307  *
308  * The function returns the current element in the iteration.
309  * This return value will potentially be used as a start parameter
310  * in subsequent calls to the function.
311  *
312  * The additional iterator parameter is only there if a specific
313  * implementation needs additional context. It must not be modified by
314  * the iteration function itself.
315  *
316  * @param start
317  *   Starting iteration context.
318  *
319  * @param devstr
320  *   Device description string.
321  *
322  * @param it
323  *   Device iterator.
324  *
325  * @return
326  *   The address of the current element matching the device description
327  *   string.
328  */
329 typedef void *(*rte_dev_iterate_t)(const void *start,
330                                    const char *devstr,
331                                    const struct rte_dev_iterator *it);
332
333 #ifdef __cplusplus
334 }
335 #endif
336
337 /**
338  * @warning
339  * @b EXPERIMENTAL: this API may change without prior notice
340  *
341  * It registers the callback for the specific device.
342  * Multiple callbacks cal be registered at the same time.
343  *
344  * @param device_name
345  *  The device name, that is the param name of the struct rte_device,
346  *  null value means for all devices.
347  * @param cb_fn
348  *  callback address.
349  * @param cb_arg
350  *  address of parameter for callback.
351  *
352  * @return
353  *  - On success, zero.
354  *  - On failure, a negative value.
355  */
356 int __rte_experimental
357 rte_dev_event_callback_register(const char *device_name,
358                                 rte_dev_event_cb_fn cb_fn,
359                                 void *cb_arg);
360
361 /**
362  * @warning
363  * @b EXPERIMENTAL: this API may change without prior notice
364  *
365  * It unregisters the callback according to the specified device.
366  *
367  * @param device_name
368  *  The device name, that is the param name of the struct rte_device,
369  *  null value means for all devices and their callbacks.
370  * @param cb_fn
371  *  callback address.
372  * @param cb_arg
373  *  address of parameter for callback, (void *)-1 means to remove all
374  *  registered which has the same callback address.
375  *
376  * @return
377  *  - On success, return the number of callback entities removed.
378  *  - On failure, a negative value.
379  */
380 int __rte_experimental
381 rte_dev_event_callback_unregister(const char *device_name,
382                                   rte_dev_event_cb_fn cb_fn,
383                                   void *cb_arg);
384
385 /**
386  * @warning
387  * @b EXPERIMENTAL: this API may change without prior notice
388  *
389  * Start the device event monitoring.
390  *
391  * @return
392  *   - On success, zero.
393  *   - On failure, a negative value.
394  */
395 int __rte_experimental
396 rte_dev_event_monitor_start(void);
397
398 /**
399  * @warning
400  * @b EXPERIMENTAL: this API may change without prior notice
401  *
402  * Stop the device event monitoring.
403  *
404  * @return
405  *   - On success, zero.
406  *   - On failure, a negative value.
407  */
408 int __rte_experimental
409 rte_dev_event_monitor_stop(void);
410
411 #endif /* _RTE_DEV_H_ */