1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014 6WIND S.A.
11 * RTE PMD Driver Registration Interface
13 * This file manages the list of device drivers.
21 #include <sys/queue.h>
23 #include <rte_config.h>
24 #include <rte_compat.h>
28 * The device event type.
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 */
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 */
42 typedef void (*rte_dev_event_cb_fn)(const char *device_name,
43 enum rte_dev_event_type event,
46 __attribute__((format(printf, 2, 0)))
48 rte_pmd_debug_trace(const char *func_name, const char *fmt, ...)
55 char buffer[vsnprintf(NULL, 0, fmt, ap) + 1];
60 vsnprintf(buffer, sizeof(buffer), fmt, ap);
63 rte_log(RTE_LOG_ERR, RTE_LOGTYPE_PMD, "%s: %s",
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.
72 #if defined(RTE_LIBRTE_EVENTDEV_DEBUG)
73 #define RTE_PMD_DEBUG_TRACE(...) \
74 rte_pmd_debug_trace(__func__, __VA_ARGS__)
76 #define RTE_PMD_DEBUG_TRACE(...) (void)0
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"); \
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"); \
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"); \
102 #define RTE_FUNC_PTR_OR_RET(func) do { \
103 if ((func) == NULL) { \
104 RTE_PMD_DEBUG_TRACE("Function not supported\n"); \
112 enum rte_kernel_driver {
113 RTE_KDRV_UNKNOWN = 0,
116 RTE_KDRV_UIO_GENERIC,
124 enum rte_dev_policy {
130 * A generic memory resource representation.
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. */
139 * A structure describing a device driver.
142 TAILQ_ENTRY(rte_driver) next; /**< Next in list. */
143 const char *name; /**< Driver name. */
144 const char *alias; /**< Driver alias. */
148 * Internal identifier length
149 * Sufficiently large to allow for UUID or PCI address
151 #define RTE_DEV_NAME_MAX_LEN 64
154 * A structure describing a generic 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 */
167 * @b EXPERIMENTAL: this API may change without prior notice
169 * Query status of a device.
172 * Generic device pointer.
174 * (int)true if already probed successfully, 0 otherwise.
177 int rte_dev_is_probed(const struct rte_device *dev);
180 * Attach a device to a registered driver.
183 * The device name, that refers to a pci device (or some private
184 * way of designating a vdev device). Based on this device name, eal
185 * will identify a driver capable of handling it and pass it to the
186 * driver probing function.
188 * Device arguments to be passed to the driver.
190 * 0 on success, negative on error.
193 int rte_eal_dev_attach(const char *name, const char *devargs);
196 * Detach a device from its driver.
199 * A pointer to a rte_device structure.
201 * 0 on success, negative on error.
204 int rte_eal_dev_detach(struct rte_device *dev);
207 * Hotplug add a given device to a specific bus.
209 * In multi-process, it will request other processes to add the same device.
210 * A failure, in any process, will rollback the action
213 * The bus name the device is added to.
215 * The device name. Based on this device name, eal will identify a driver
216 * capable of handling it and pass it to the driver probing function.
218 * Device arguments to be passed to the driver.
220 * 0 on success, negative on error.
222 int rte_eal_hotplug_add(const char *busname, const char *devname,
223 const char *drvargs);
227 * @b EXPERIMENTAL: this API may change without prior notice
229 * Add matching devices.
231 * In multi-process, it will request other processes to add the same device.
232 * A failure, in any process, will rollback the action
235 * Device arguments including bus, class and driver properties.
237 * 0 on success, negative on error.
239 int __rte_experimental rte_dev_probe(const char *devargs);
242 * Hotplug remove a given device from a specific bus.
244 * In multi-process, it will request other processes to remove the same device.
245 * A failure, in any process, will rollback the action
248 * The bus name the device is removed from.
250 * The device name being removed.
252 * 0 on success, negative on error.
254 int rte_eal_hotplug_remove(const char *busname, const char *devname);
258 * @b EXPERIMENTAL: this API may change without prior notice
262 * In multi-process, it will request other processes to remove the same device.
263 * A failure, in any process, will rollback the action
266 * Data structure of the device to remove.
268 * 0 on success, negative on error.
270 int __rte_experimental rte_dev_remove(struct rte_device *dev);
273 * Device comparison function.
275 * This type of function is used to compare an rte_device with arbitrary
282 * Data to compare against. The type of this parameter is determined by
283 * the kind of comparison performed by the function.
286 * 0 if the device matches the data.
287 * !0 if the device does not match.
288 * <0 if ordering is possible and the device is lower than the data.
289 * >0 if ordering is possible and the device is greater than the data.
291 typedef int (*rte_dev_cmp_t)(const struct rte_device *dev, const void *data);
293 #define RTE_PMD_EXPORT_NAME_ARRAY(n, idx) n##idx[]
295 #define RTE_PMD_EXPORT_NAME(name, idx) \
296 static const char RTE_PMD_EXPORT_NAME_ARRAY(this_pmd_name, idx) \
297 __attribute__((used)) = RTE_STR(name)
299 #define DRV_EXP_TAG(name, tag) __##name##_##tag
301 #define RTE_PMD_REGISTER_PCI_TABLE(name, table) \
302 static const char DRV_EXP_TAG(name, pci_tbl_export)[] __attribute__((used)) = \
305 #define RTE_PMD_REGISTER_PARAM_STRING(name, str) \
306 static const char DRV_EXP_TAG(name, param_string_export)[] \
307 __attribute__((used)) = str
310 * Advertise the list of kernel modules required to run this driver
312 * This string lists the kernel modules required for the devices
313 * associated to a PMD. The format of each line of the string is:
314 * "<device-pattern> <kmod-expression>".
316 * The possible formats for the device pattern are:
317 * "*" all devices supported by this driver
318 * "pci:*" all PCI devices supported by this driver
319 * "pci:v8086:d*:sv*:sd*" all PCI devices supported by this driver
320 * whose vendor id is 0x8086.
322 * The format of the kernel modules list is a parenthesed expression
323 * containing logical-and (&) and logical-or (|).
325 * The device pattern and the kmod expression are separated by a space.
328 * - "* igb_uio | uio_pci_generic | vfio"
330 #define RTE_PMD_REGISTER_KMOD_DEP(name, str) \
331 static const char DRV_EXP_TAG(name, kmod_dep_export)[] \
332 __attribute__((used)) = str
337 * This context carries over the current iteration state.
339 struct rte_dev_iterator {
340 const char *dev_str; /**< device string. */
341 const char *bus_str; /**< bus-related part of device string. */
342 const char *cls_str; /**< class-related part of device string. */
343 struct rte_bus *bus; /**< bus handle. */
344 struct rte_class *cls; /**< class handle. */
345 struct rte_device *device; /**< current position. */
346 void *class_device; /**< additional specialized context. */
350 * Device iteration function.
352 * Find the next device matching properties passed in parameters.
353 * The function takes an additional ``start`` parameter, that is
354 * used as starting context when relevant.
356 * The function returns the current element in the iteration.
357 * This return value will potentially be used as a start parameter
358 * in subsequent calls to the function.
360 * The additional iterator parameter is only there if a specific
361 * implementation needs additional context. It must not be modified by
362 * the iteration function itself.
365 * Starting iteration context.
368 * Device description string.
374 * The address of the current element matching the device description
377 typedef void *(*rte_dev_iterate_t)(const void *start,
379 const struct rte_dev_iterator *it);
382 * Initializes a device iterator.
384 * This iterator allows accessing a list of devices matching a criteria.
385 * The device matching is made among all buses and classes currently registered,
386 * filtered by the device description given as parameter.
388 * This function will not allocate any memory. It is safe to stop the
389 * iteration at any moment and let the iterator go out of context.
392 * Device iterator handle.
395 * Device description string.
398 * 0 on successful initialization.
403 rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str);
406 * Iterates on a device iterator.
408 * Generates a new rte_device handle corresponding to the next element
409 * in the list described in comprehension by the iterator.
411 * The next object is returned, and the iterator is updated.
414 * Device iterator handle.
417 * An rte_device handle if found.
418 * NULL if an error occurred (rte_errno is set).
419 * NULL if no device could be found (rte_errno is not set).
423 rte_dev_iterator_next(struct rte_dev_iterator *it);
425 #define RTE_DEV_FOREACH(dev, devstr, it) \
426 for (rte_dev_iterator_init(it, devstr), \
427 dev = rte_dev_iterator_next(it); \
429 dev = rte_dev_iterator_next(it))
437 * @b EXPERIMENTAL: this API may change without prior notice
439 * It registers the callback for the specific device.
440 * Multiple callbacks cal be registered at the same time.
443 * The device name, that is the param name of the struct rte_device,
444 * null value means for all devices.
448 * address of parameter for callback.
451 * - On success, zero.
452 * - On failure, a negative value.
454 int __rte_experimental
455 rte_dev_event_callback_register(const char *device_name,
456 rte_dev_event_cb_fn cb_fn,
461 * @b EXPERIMENTAL: this API may change without prior notice
463 * It unregisters the callback according to the specified device.
466 * The device name, that is the param name of the struct rte_device,
467 * null value means for all devices and their callbacks.
471 * address of parameter for callback, (void *)-1 means to remove all
472 * registered which has the same callback address.
475 * - On success, return the number of callback entities removed.
476 * - On failure, a negative value.
478 int __rte_experimental
479 rte_dev_event_callback_unregister(const char *device_name,
480 rte_dev_event_cb_fn cb_fn,
485 * @b EXPERIMENTAL: this API may change without prior notice
487 * Executes all the user application registered callbacks for
488 * the specific device.
493 * the device event type.
495 void __rte_experimental
496 rte_dev_event_callback_process(const char *device_name,
497 enum rte_dev_event_type event);
501 * @b EXPERIMENTAL: this API may change without prior notice
503 * Start the device event monitoring.
506 * - On success, zero.
507 * - On failure, a negative value.
509 int __rte_experimental
510 rte_dev_event_monitor_start(void);
514 * @b EXPERIMENTAL: this API may change without prior notice
516 * Stop the device event monitoring.
519 * - On success, zero.
520 * - On failure, a negative value.
522 int __rte_experimental
523 rte_dev_event_monitor_stop(void);
527 * @b EXPERIMENTAL: this API may change without prior notice
529 * Enable hotplug handling for devices.
532 * - On success, zero.
533 * - On failure, a negative value.
535 int __rte_experimental
536 rte_dev_hotplug_handle_enable(void);
540 * @b EXPERIMENTAL: this API may change without prior notice
542 * Disable hotplug handling for devices.
545 * - On success, zero.
546 * - On failure, a negative value.
548 int __rte_experimental
549 rte_dev_hotplug_handle_disable(void);
551 #endif /* _RTE_DEV_H_ */