1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 RehiveTech. All rights reserved. 3 */ 4 5 #ifndef RTE_VDEV_H 6 #define RTE_VDEV_H 7 8 /** 9 * @file 10 * RTE virtual bus API 11 * 12 */ 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #include <sys/queue.h> 19 #include <rte_dev.h> 20 #include <rte_devargs.h> 21 22 struct rte_vdev_device { 23 TAILQ_ENTRY(rte_vdev_device) next; /**< Next attached vdev */ 24 struct rte_device device; /**< Inherit core device */ 25 }; 26 27 /** 28 * @internal 29 * Helper macro for drivers that need to convert to struct rte_vdev_device. 30 */ 31 #define RTE_DEV_TO_VDEV(ptr) \ 32 container_of(ptr, struct rte_vdev_device, device) 33 34 #define RTE_DEV_TO_VDEV_CONST(ptr) \ 35 container_of(ptr, const struct rte_vdev_device, device) 36 37 static inline const char * 38 rte_vdev_device_name(const struct rte_vdev_device *dev) 39 { 40 if (dev && dev->device.name) 41 return dev->device.name; 42 return NULL; 43 } 44 45 static inline const char * 46 rte_vdev_device_args(const struct rte_vdev_device *dev) 47 { 48 if (dev && dev->device.devargs) 49 return dev->device.devargs->args; 50 return ""; 51 } 52 53 /** Double linked list of virtual device drivers. */ 54 TAILQ_HEAD(vdev_driver_list, rte_vdev_driver); 55 56 /** 57 * Probe function called for each virtual device driver once. 58 */ 59 typedef int (rte_vdev_probe_t)(struct rte_vdev_device *dev); 60 61 /** 62 * Remove function called for each virtual device driver once. 63 */ 64 typedef int (rte_vdev_remove_t)(struct rte_vdev_device *dev); 65 66 /** 67 * A virtual device driver abstraction. 68 */ 69 struct rte_vdev_driver { 70 TAILQ_ENTRY(rte_vdev_driver) next; /**< Next in list. */ 71 struct rte_driver driver; /**< Inherited general driver. */ 72 rte_vdev_probe_t *probe; /**< Virtual device probe function. */ 73 rte_vdev_remove_t *remove; /**< Virtual device remove function. */ 74 }; 75 76 /** 77 * Register a virtual device driver. 78 * 79 * @param driver 80 * A pointer to a rte_vdev_driver structure describing the driver 81 * to be registered. 82 */ 83 void rte_vdev_register(struct rte_vdev_driver *driver); 84 85 /** 86 * Unregister a virtual device driver. 87 * 88 * @param driver 89 * A pointer to a rte_vdev_driver structure describing the driver 90 * to be unregistered. 91 */ 92 void rte_vdev_unregister(struct rte_vdev_driver *driver); 93 94 #define RTE_PMD_REGISTER_VDEV(nm, vdrv)\ 95 static const char *vdrvinit_ ## nm ## _alias;\ 96 RTE_INIT(vdrvinitfn_ ##vdrv)\ 97 {\ 98 (vdrv).driver.name = RTE_STR(nm);\ 99 (vdrv).driver.alias = vdrvinit_ ## nm ## _alias;\ 100 rte_vdev_register(&vdrv);\ 101 } \ 102 RTE_PMD_EXPORT_NAME(nm, __COUNTER__) 103 104 #define RTE_PMD_REGISTER_ALIAS(nm, alias)\ 105 static const char *vdrvinit_ ## nm ## _alias = RTE_STR(alias) 106 107 typedef void (*rte_vdev_scan_callback)(void *user_arg); 108 109 /** 110 * Add a callback to be called on vdev scan 111 * before reading the devargs list. 112 * 113 * This function cannot be called in a scan callback 114 * because of deadlock. 115 * 116 * @param callback 117 * The function to be called which can update the devargs list. 118 * @param user_arg 119 * An opaque pointer passed to callback. 120 * @return 121 * 0 on success, negative on error 122 */ 123 int 124 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg); 125 126 /** 127 * Remove a registered scan callback. 128 * 129 * This function cannot be called in a scan callback 130 * because of deadlock. 131 * 132 * @param callback 133 * The registered function to be removed. 134 * @param user_arg 135 * The associated opaque pointer or (void*)-1 for any. 136 * @return 137 * 0 on success 138 */ 139 int 140 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg); 141 142 /** 143 * Initialize a driver specified by name. 144 * 145 * @param name 146 * The pointer to a driver name to be initialized. 147 * @param args 148 * The pointer to arguments used by driver initialization. 149 * @return 150 * 0 on success, negative on error 151 */ 152 int rte_vdev_init(const char *name, const char *args); 153 154 /** 155 * Uninitalize a driver specified by name. 156 * 157 * @param name 158 * The pointer to a driver name to be initialized. 159 * @return 160 * 0 on success, negative on error 161 */ 162 int rte_vdev_uninit(const char *name); 163 164 #ifdef __cplusplus 165 } 166 #endif 167 168 #endif 169