eal: declare trace buffer at top of own block
[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_ETHDEV_DEBUG) || \
73         defined(RTE_LIBRTE_CRYPTODEV_DEBUG) || \
74         defined(RTE_LIBRTE_EVENTDEV_DEBUG)
75 #define RTE_PMD_DEBUG_TRACE(...) \
76         rte_pmd_debug_trace(__func__, __VA_ARGS__)
77 #else
78 #define RTE_PMD_DEBUG_TRACE(...) (void)0
79 #endif
80
81 /* Macros for checking for restricting functions to primary instance only */
82 #define RTE_PROC_PRIMARY_OR_ERR_RET(retval) do { \
83         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
84                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
85                 return retval; \
86         } \
87 } while (0)
88
89 #define RTE_PROC_PRIMARY_OR_RET() do { \
90         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
91                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
92                 return; \
93         } \
94 } while (0)
95
96 /* Macros to check for invalid function pointers */
97 #define RTE_FUNC_PTR_OR_ERR_RET(func, retval) do { \
98         if ((func) == NULL) { \
99                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
100                 return retval; \
101         } \
102 } while (0)
103
104 #define RTE_FUNC_PTR_OR_RET(func) do { \
105         if ((func) == NULL) { \
106                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
107                 return; \
108         } \
109 } while (0)
110
111 /**
112  * Device driver.
113  */
114 enum rte_kernel_driver {
115         RTE_KDRV_UNKNOWN = 0,
116         RTE_KDRV_IGB_UIO,
117         RTE_KDRV_VFIO,
118         RTE_KDRV_UIO_GENERIC,
119         RTE_KDRV_NIC_UIO,
120         RTE_KDRV_NONE,
121 };
122
123 /**
124  * Device policies.
125  */
126 enum rte_dev_policy {
127         RTE_DEV_WHITELISTED,
128         RTE_DEV_BLACKLISTED,
129 };
130
131 /**
132  * A generic memory resource representation.
133  */
134 struct rte_mem_resource {
135         uint64_t phys_addr; /**< Physical address, 0 if not resource. */
136         uint64_t len;       /**< Length of the resource. */
137         void *addr;         /**< Virtual address, NULL when not mapped. */
138 };
139
140 /**
141  * A structure describing a device driver.
142  */
143 struct rte_driver {
144         TAILQ_ENTRY(rte_driver) next;  /**< Next in list. */
145         const char *name;                   /**< Driver name. */
146         const char *alias;              /**< Driver alias. */
147 };
148
149 /*
150  * Internal identifier length
151  * Sufficiently large to allow for UUID or PCI address
152  */
153 #define RTE_DEV_NAME_MAX_LEN 64
154
155 /**
156  * A structure describing a generic device.
157  */
158 struct rte_device {
159         TAILQ_ENTRY(rte_device) next; /**< Next device */
160         const char *name;             /**< Device name */
161         const struct rte_driver *driver;/**< Associated driver */
162         int numa_node;                /**< NUMA node connection */
163         struct rte_devargs *devargs;  /**< Device user arguments */
164 };
165
166 /**
167  * Attach a device to a registered driver.
168  *
169  * @param name
170  *   The device name, that refers to a pci device (or some private
171  *   way of designating a vdev device). Based on this device name, eal
172  *   will identify a driver capable of handling it and pass it to the
173  *   driver probing function.
174  * @param devargs
175  *   Device arguments to be passed to the driver.
176  * @return
177  *   0 on success, negative on error.
178  */
179 int rte_eal_dev_attach(const char *name, const char *devargs);
180
181 /**
182  * Detach a device from its driver.
183  *
184  * @param dev
185  *   A pointer to a rte_device structure.
186  * @return
187  *   0 on success, negative on error.
188  */
189 int rte_eal_dev_detach(struct rte_device *dev);
190
191 /**
192  * @warning
193  * @b EXPERIMENTAL: this API may change without prior notice
194  *
195  * Hotplug add a given device to a specific bus.
196  *
197  * @param busname
198  *   The bus name the device is added to.
199  * @param devname
200  *   The device name. Based on this device name, eal will identify a driver
201  *   capable of handling it and pass it to the driver probing function.
202  * @param devargs
203  *   Device arguments to be passed to the driver.
204  * @return
205  *   0 on success, negative on error.
206  */
207 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
208                         const char *devargs);
209
210 /**
211  * @warning
212  * @b EXPERIMENTAL: this API may change without prior notice
213  *
214  * Hotplug remove a given device from a specific bus.
215  *
216  * @param busname
217  *   The bus name the device is removed from.
218  * @param devname
219  *   The device name being removed.
220  * @return
221  *   0 on success, negative on error.
222  */
223 int __rte_experimental rte_eal_hotplug_remove(const char *busname,
224                                           const char *devname);
225
226 /**
227  * Device comparison function.
228  *
229  * This type of function is used to compare an rte_device with arbitrary
230  * data.
231  *
232  * @param dev
233  *   Device handle.
234  *
235  * @param data
236  *   Data to compare against. The type of this parameter is determined by
237  *   the kind of comparison performed by the function.
238  *
239  * @return
240  *   0 if the device matches the data.
241  *   !0 if the device does not match.
242  *   <0 if ordering is possible and the device is lower than the data.
243  *   >0 if ordering is possible and the device is greater than the data.
244  */
245 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
246
247 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
248
249 #define RTE_PMD_EXPORT_NAME(name, idx) \
250 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
251 __attribute__((used)) = RTE_STR(name)
252
253 #define DRV_EXP_TAG(name, tag) __##name##_##tag
254
255 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
256 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
257 RTE_STR(table)
258
259 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
260 static const char DRV_EXP_TAG(name, param_string_export)[] \
261 __attribute__((used)) = str
262
263 /**
264  * Advertise the list of kernel modules required to run this driver
265  *
266  * This string lists the kernel modules required for the devices
267  * associated to a PMD. The format of each line of the string is:
268  * "<device-pattern> <kmod-expression>".
269  *
270  * The possible formats for the device pattern are:
271  *   "*"                     all devices supported by this driver
272  *   "pci:*"                 all PCI devices supported by this driver
273  *   "pci:v8086:d*:sv*:sd*"  all PCI devices supported by this driver
274  *                           whose vendor id is 0x8086.
275  *
276  * The format of the kernel modules list is a parenthesed expression
277  * containing logical-and (&) and logical-or (|).
278  *
279  * The device pattern and the kmod expression are separated by a space.
280  *
281  * Example:
282  * - "* igb_uio | uio_pci_generic | vfio"
283  */
284 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
285 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
286 __attribute__((used)) = str
287
288 #ifdef __cplusplus
289 }
290 #endif
291
292 /**
293  * @warning
294  * @b EXPERIMENTAL: this API may change without prior notice
295  *
296  * It registers the callback for the specific device.
297  * Multiple callbacks cal be registered at the same time.
298  *
299  * @param device_name
300  *  The device name, that is the param name of the struct rte_device,
301  *  null value means for all devices.
302  * @param cb_fn
303  *  callback address.
304  * @param cb_arg
305  *  address of parameter for callback.
306  *
307  * @return
308  *  - On success, zero.
309  *  - On failure, a negative value.
310  */
311 int __rte_experimental
312 rte_dev_event_callback_register(const char *device_name,
313                                 rte_dev_event_cb_fn cb_fn,
314                                 void *cb_arg);
315
316 /**
317  * @warning
318  * @b EXPERIMENTAL: this API may change without prior notice
319  *
320  * It unregisters the callback according to the specified device.
321  *
322  * @param device_name
323  *  The device name, that is the param name of the struct rte_device,
324  *  null value means for all devices and their callbacks.
325  * @param cb_fn
326  *  callback address.
327  * @param cb_arg
328  *  address of parameter for callback, (void *)-1 means to remove all
329  *  registered which has the same callback address.
330  *
331  * @return
332  *  - On success, return the number of callback entities removed.
333  *  - On failure, a negative value.
334  */
335 int __rte_experimental
336 rte_dev_event_callback_unregister(const char *device_name,
337                                   rte_dev_event_cb_fn cb_fn,
338                                   void *cb_arg);
339
340 /**
341  * @warning
342  * @b EXPERIMENTAL: this API may change without prior notice
343  *
344  * Start the device event monitoring.
345  *
346  * @return
347  *   - On success, zero.
348  *   - On failure, a negative value.
349  */
350 int __rte_experimental
351 rte_dev_event_monitor_start(void);
352
353 /**
354  * @warning
355  * @b EXPERIMENTAL: this API may change without prior notice
356  *
357  * Stop the device event monitoring.
358  *
359  * @return
360  *   - On success, zero.
361  *   - On failure, a negative value.
362  */
363 int __rte_experimental
364 rte_dev_event_monitor_stop(void);
365
366 #endif /* _RTE_DEV_H_ */