eal: use SPDX tags in 6WIND copyrighted files
[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 __attribute__((format(printf, 2, 0)))
28 static inline void
29 rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
30 {
31         va_list ap;
32
33         va_start(ap, fmt);
34
35         char buffer[vsnprintf(NULL, 0, fmt, ap) + 1];
36
37         va_end(ap);
38
39         va_start(ap, fmt);
40         vsnprintf(buffer, sizeof(buffer), fmt, ap);
41         va_end(ap);
42
43         rte_log(RTE_LOG_ERR, RTE_LOGTYPE_PMD, "%s: %s", func_name, buffer);
44 }
45
46 /*
47  * Enable RTE_PMD_DEBUG_TRACE() when at least one component relying on the
48  * RTE_*_RET() macros defined below is compiled in debug mode.
49  */
50 #if defined(RTE_LIBRTE_ETHDEV_DEBUG) || \
51         defined(RTE_LIBRTE_CRYPTODEV_DEBUG) || \
52         defined(RTE_LIBRTE_EVENTDEV_DEBUG)
53 #define RTE_PMD_DEBUG_TRACE(...) \
54         rte_pmd_debug_trace(__func__, __VA_ARGS__)
55 #else
56 #define RTE_PMD_DEBUG_TRACE(...) (void)0
57 #endif
58
59 /* Macros for checking for restricting functions to primary instance only */
60 #define RTE_PROC_PRIMARY_OR_ERR_RET(retval) do { \
61         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
62                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
63                 return retval; \
64         } \
65 } while (0)
66
67 #define RTE_PROC_PRIMARY_OR_RET() do { \
68         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
69                 RTE_PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
70                 return; \
71         } \
72 } while (0)
73
74 /* Macros to check for invalid function pointers */
75 #define RTE_FUNC_PTR_OR_ERR_RET(func, retval) do { \
76         if ((func) == NULL) { \
77                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
78                 return retval; \
79         } \
80 } while (0)
81
82 #define RTE_FUNC_PTR_OR_RET(func) do { \
83         if ((func) == NULL) { \
84                 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
85                 return; \
86         } \
87 } while (0)
88
89 /**
90  * Device driver.
91  */
92 enum rte_kernel_driver {
93         RTE_KDRV_UNKNOWN = 0,
94         RTE_KDRV_IGB_UIO,
95         RTE_KDRV_VFIO,
96         RTE_KDRV_UIO_GENERIC,
97         RTE_KDRV_NIC_UIO,
98         RTE_KDRV_NONE,
99 };
100
101 /**
102  * Device policies.
103  */
104 enum rte_dev_policy {
105         RTE_DEV_WHITELISTED,
106         RTE_DEV_BLACKLISTED,
107 };
108
109 /**
110  * A generic memory resource representation.
111  */
112 struct rte_mem_resource {
113         uint64_t phys_addr; /**< Physical address, 0 if not resource. */
114         uint64_t len;       /**< Length of the resource. */
115         void *addr;         /**< Virtual address, NULL when not mapped. */
116 };
117
118 /**
119  * A structure describing a device driver.
120  */
121 struct rte_driver {
122         TAILQ_ENTRY(rte_driver) next;  /**< Next in list. */
123         const char *name;                   /**< Driver name. */
124         const char *alias;              /**< Driver alias. */
125 };
126
127 /*
128  * Internal identifier length
129  * Sufficiently large to allow for UUID or PCI address
130  */
131 #define RTE_DEV_NAME_MAX_LEN 64
132
133 /**
134  * A structure describing a generic device.
135  */
136 struct rte_device {
137         TAILQ_ENTRY(rte_device) next; /**< Next device */
138         const char *name;             /**< Device name */
139         const struct rte_driver *driver;/**< Associated driver */
140         int numa_node;                /**< NUMA node connection */
141         struct rte_devargs *devargs;  /**< Device user arguments */
142 };
143
144 /**
145  * Attach a device to a registered driver.
146  *
147  * @param name
148  *   The device name, that refers to a pci device (or some private
149  *   way of designating a vdev device). Based on this device name, eal
150  *   will identify a driver capable of handling it and pass it to the
151  *   driver probing function.
152  * @param devargs
153  *   Device arguments to be passed to the driver.
154  * @return
155  *   0 on success, negative on error.
156  */
157 int rte_eal_dev_attach(const char *name, const char *devargs);
158
159 /**
160  * Detach a device from its driver.
161  *
162  * @param dev
163  *   A pointer to a rte_device structure.
164  * @return
165  *   0 on success, negative on error.
166  */
167 int rte_eal_dev_detach(struct rte_device *dev);
168
169 /**
170  * @warning
171  * @b EXPERIMENTAL: this API may change without prior notice
172  *
173  * Hotplug add a given device to a specific bus.
174  *
175  * @param busname
176  *   The bus name the device is added to.
177  * @param devname
178  *   The device name. Based on this device name, eal will identify a driver
179  *   capable of handling it and pass it to the driver probing function.
180  * @param devargs
181  *   Device arguments to be passed to the driver.
182  * @return
183  *   0 on success, negative on error.
184  */
185 int __rte_experimental rte_eal_hotplug_add(const char *busname, const char *devname,
186                         const char *devargs);
187
188 /**
189  * @warning
190  * @b EXPERIMENTAL: this API may change without prior notice
191  *
192  * Hotplug remove a given device from a specific bus.
193  *
194  * @param busname
195  *   The bus name the device is removed from.
196  * @param devname
197  *   The device name being removed.
198  * @return
199  *   0 on success, negative on error.
200  */
201 int __rte_experimental rte_eal_hotplug_remove(const char *busname,
202                                           const char *devname);
203
204 /**
205  * Device comparison function.
206  *
207  * This type of function is used to compare an rte_device with arbitrary
208  * data.
209  *
210  * @param dev
211  *   Device handle.
212  *
213  * @param data
214  *   Data to compare against. The type of this parameter is determined by
215  *   the kind of comparison performed by the function.
216  *
217  * @return
218  *   0 if the device matches the data.
219  *   !0 if the device does not match.
220  *   <0 if ordering is possible and the device is lower than the data.
221  *   >0 if ordering is possible and the device is greater than the data.
222  */
223 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
224
225 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
226
227 #define RTE_PMD_EXPORT_NAME(name, idx) \
228 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
229 __attribute__((used)) = RTE_STR(name)
230
231 #define DRV_EXP_TAG(name, tag) __##name##_##tag
232
233 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
234 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
235 RTE_STR(table)
236
237 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
238 static const char DRV_EXP_TAG(name, param_string_export)[] \
239 __attribute__((used)) = str
240
241 /**
242  * Advertise the list of kernel modules required to run this driver
243  *
244  * This string lists the kernel modules required for the devices
245  * associated to a PMD. The format of each line of the string is:
246  * "<device-pattern> <kmod-expression>".
247  *
248  * The possible formats for the device pattern are:
249  *   "*"                     all devices supported by this driver
250  *   "pci:*"                 all PCI devices supported by this driver
251  *   "pci:v8086:d*:sv*:sd*"  all PCI devices supported by this driver
252  *                           whose vendor id is 0x8086.
253  *
254  * The format of the kernel modules list is a parenthesed expression
255  * containing logical-and (&) and logical-or (|).
256  *
257  * The device pattern and the kmod expression are separated by a space.
258  *
259  * Example:
260  * - "* igb_uio | uio_pci_generic | vfio"
261  */
262 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
263 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
264 __attribute__((used)) = str
265
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif /* _RTE_DEV_H_ */