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