New upstream version 18.11-rc1
[deb_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)(const 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; /**< Driver assigned after probing */
160         const struct rte_bus *bus;    /**< Bus handle assigned on scan */
161         int numa_node;                /**< NUMA node connection */
162         struct rte_devargs *devargs;  /**< Arguments for latest probing */
163 };
164
165 /**
166  * @warning
167  * @b EXPERIMENTAL: this API may change without prior notice
168  *
169  * Query status of a device.
170  *
171  * @param dev
172  *   Generic device pointer.
173  * @return
174  *   (int)true if already probed successfully, 0 otherwise.
175  */
176 __rte_experimental
177 int rte_dev_is_probed(const struct rte_device *dev);
178
179 /**
180  * Hotplug add a given device to a specific bus.
181  *
182  * In multi-process, it will request other processes to add the same device.
183  * A failure, in any process, will rollback the action
184  *
185  * @param busname
186  *   The bus name the device is added to.
187  * @param devname
188  *   The device name. Based on this device name, eal will identify a driver
189  *   capable of handling it and pass it to the driver probing function.
190  * @param drvargs
191  *   Device arguments to be passed to the driver.
192  * @return
193  *   0 on success, negative on error.
194  */
195 int rte_eal_hotplug_add(const char *busname, const char *devname,
196                         const char *drvargs);
197
198 /**
199  * @warning
200  * @b EXPERIMENTAL: this API may change without prior notice
201  *
202  * Add matching devices.
203  *
204  * In multi-process, it will request other processes to add the same device.
205  * A failure, in any process, will rollback the action
206  *
207  * @param devargs
208  *   Device arguments including bus, class and driver properties.
209  * @return
210  *   0 on success, negative on error.
211  */
212 int __rte_experimental rte_dev_probe(const char *devargs);
213
214 /**
215  * Hotplug remove a given device from a specific bus.
216  *
217  * In multi-process, it will request other processes to remove the same device.
218  * A failure, in any process, will rollback the action
219  *
220  * @param busname
221  *   The bus name the device is removed from.
222  * @param devname
223  *   The device name being removed.
224  * @return
225  *   0 on success, negative on error.
226  */
227 int rte_eal_hotplug_remove(const char *busname, const char *devname);
228
229 /**
230  * @warning
231  * @b EXPERIMENTAL: this API may change without prior notice
232  *
233  * Remove one device.
234  *
235  * In multi-process, it will request other processes to remove the same device.
236  * A failure, in any process, will rollback the action
237  *
238  * @param dev
239  *   Data structure of the device to remove.
240  * @return
241  *   0 on success, negative on error.
242  */
243 int __rte_experimental rte_dev_remove(struct rte_device *dev);
244
245 /**
246  * Device comparison function.
247  *
248  * This type of function is used to compare an rte_device with arbitrary
249  * data.
250  *
251  * @param dev
252  *   Device handle.
253  *
254  * @param data
255  *   Data to compare against. The type of this parameter is determined by
256  *   the kind of comparison performed by the function.
257  *
258  * @return
259  *   0 if the device matches the data.
260  *   !0 if the device does not match.
261  *   <0 if ordering is possible and the device is lower than the data.
262  *   >0 if ordering is possible and the device is greater than the data.
263  */
264 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
265
266 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
267
268 #define RTE_PMD_EXPORT_NAME(name, idx) \
269 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
270 __attribute__((used)) = RTE_STR(name)
271
272 #define DRV_EXP_TAG(name, tag) __##name##_##tag
273
274 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
275 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
276 RTE_STR(table)
277
278 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
279 static const char DRV_EXP_TAG(name, param_string_export)[] \
280 __attribute__((used)) = str
281
282 /**
283  * Advertise the list of kernel modules required to run this driver
284  *
285  * This string lists the kernel modules required for the devices
286  * associated to a PMD. The format of each line of the string is:
287  * "<device-pattern> <kmod-expression>".
288  *
289  * The possible formats for the device pattern are:
290  *   "*"                     all devices supported by this driver
291  *   "pci:*"                 all PCI devices supported by this driver
292  *   "pci:v8086:d*:sv*:sd*"  all PCI devices supported by this driver
293  *                           whose vendor id is 0x8086.
294  *
295  * The format of the kernel modules list is a parenthesed expression
296  * containing logical-and (&) and logical-or (|).
297  *
298  * The device pattern and the kmod expression are separated by a space.
299  *
300  * Example:
301  * - "* igb_uio | uio_pci_generic | vfio"
302  */
303 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
304 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
305 __attribute__((used)) = str
306
307 /**
308  * Iteration context.
309  *
310  * This context carries over the current iteration state.
311  */
312 struct rte_dev_iterator {
313         const char *dev_str; /**< device string. */
314         const char *bus_str; /**< bus-related part of device string. */
315         const char *cls_str; /**< class-related part of device string. */
316         struct rte_bus *bus; /**< bus handle. */
317         struct rte_class *cls; /**< class handle. */
318         struct rte_device *device; /**< current position. */
319         void *class_device; /**< additional specialized context. */
320 };
321
322 /**
323  * Device iteration function.
324  *
325  * Find the next device matching properties passed in parameters.
326  * The function takes an additional ``start`` parameter, that is
327  * used as starting context when relevant.
328  *
329  * The function returns the current element in the iteration.
330  * This return value will potentially be used as a start parameter
331  * in subsequent calls to the function.
332  *
333  * The additional iterator parameter is only there if a specific
334  * implementation needs additional context. It must not be modified by
335  * the iteration function itself.
336  *
337  * @param start
338  *   Starting iteration context.
339  *
340  * @param devstr
341  *   Device description string.
342  *
343  * @param it
344  *   Device iterator.
345  *
346  * @return
347  *   The address of the current element matching the device description
348  *   string.
349  */
350 typedef void *(*rte_dev_iterate_t)(const void *start,
351                                    const char *devstr,
352                                    const struct rte_dev_iterator *it);
353
354 /**
355  * Initializes a device iterator.
356  *
357  * This iterator allows accessing a list of devices matching a criteria.
358  * The device matching is made among all buses and classes currently registered,
359  * filtered by the device description given as parameter.
360  *
361  * This function will not allocate any memory. It is safe to stop the
362  * iteration at any moment and let the iterator go out of context.
363  *
364  * @param it
365  *   Device iterator handle.
366  *
367  * @param str
368  *   Device description string.
369  *
370  * @return
371  *   0 on successful initialization.
372  *   <0 on error.
373  */
374 __rte_experimental
375 int
376 rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str);
377
378 /**
379  * Iterates on a device iterator.
380  *
381  * Generates a new rte_device handle corresponding to the next element
382  * in the list described in comprehension by the iterator.
383  *
384  * The next object is returned, and the iterator is updated.
385  *
386  * @param it
387  *   Device iterator handle.
388  *
389  * @return
390  *   An rte_device handle if found.
391  *   NULL if an error occurred (rte_errno is set).
392  *   NULL if no device could be found (rte_errno is not set).
393  */
394 __rte_experimental
395 struct rte_device *
396 rte_dev_iterator_next(struct rte_dev_iterator *it);
397
398 #define RTE_DEV_FOREACH(dev, devstr, it) \
399         for (rte_dev_iterator_init(it, devstr), \
400              dev = rte_dev_iterator_next(it); \
401              dev != NULL; \
402              dev = rte_dev_iterator_next(it))
403
404 #ifdef __cplusplus
405 }
406 #endif
407
408 /**
409  * @warning
410  * @b EXPERIMENTAL: this API may change without prior notice
411  *
412  * It registers the callback for the specific device.
413  * Multiple callbacks cal be registered at the same time.
414  *
415  * @param device_name
416  *  The device name, that is the param name of the struct rte_device,
417  *  null value means for all devices.
418  * @param cb_fn
419  *  callback address.
420  * @param cb_arg
421  *  address of parameter for callback.
422  *
423  * @return
424  *  - On success, zero.
425  *  - On failure, a negative value.
426  */
427 int __rte_experimental
428 rte_dev_event_callback_register(const char *device_name,
429                                 rte_dev_event_cb_fn cb_fn,
430                                 void *cb_arg);
431
432 /**
433  * @warning
434  * @b EXPERIMENTAL: this API may change without prior notice
435  *
436  * It unregisters the callback according to the specified device.
437  *
438  * @param device_name
439  *  The device name, that is the param name of the struct rte_device,
440  *  null value means for all devices and their callbacks.
441  * @param cb_fn
442  *  callback address.
443  * @param cb_arg
444  *  address of parameter for callback, (void *)-1 means to remove all
445  *  registered which has the same callback address.
446  *
447  * @return
448  *  - On success, return the number of callback entities removed.
449  *  - On failure, a negative value.
450  */
451 int __rte_experimental
452 rte_dev_event_callback_unregister(const char *device_name,
453                                   rte_dev_event_cb_fn cb_fn,
454                                   void *cb_arg);
455
456 /**
457  * @warning
458  * @b EXPERIMENTAL: this API may change without prior notice
459  *
460  * Executes all the user application registered callbacks for
461  * the specific device.
462  *
463  * @param device_name
464  *  The device name.
465  * @param event
466  *  the device event type.
467  */
468 void  __rte_experimental
469 rte_dev_event_callback_process(const char *device_name,
470                                enum rte_dev_event_type event);
471
472 /**
473  * @warning
474  * @b EXPERIMENTAL: this API may change without prior notice
475  *
476  * Start the device event monitoring.
477  *
478  * @return
479  *   - On success, zero.
480  *   - On failure, a negative value.
481  */
482 int __rte_experimental
483 rte_dev_event_monitor_start(void);
484
485 /**
486  * @warning
487  * @b EXPERIMENTAL: this API may change without prior notice
488  *
489  * Stop the device event monitoring.
490  *
491  * @return
492  *   - On success, zero.
493  *   - On failure, a negative value.
494  */
495 int __rte_experimental
496 rte_dev_event_monitor_stop(void);
497
498 /**
499  * @warning
500  * @b EXPERIMENTAL: this API may change without prior notice
501  *
502  * Enable hotplug handling for devices.
503  *
504  * @return
505  *   - On success, zero.
506  *   - On failure, a negative value.
507  */
508 int __rte_experimental
509 rte_dev_hotplug_handle_enable(void);
510
511 /**
512  * @warning
513  * @b EXPERIMENTAL: this API may change without prior notice
514  *
515  * Disable hotplug handling for devices.
516  *
517  * @return
518  *   - On success, zero.
519  *   - On failure, a negative value.
520  */
521 int __rte_experimental
522 rte_dev_hotplug_handle_disable(void);
523
524 #endif /* _RTE_DEV_H_ */