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 #define RTE_ETH_DEV_TO_VDEV(eth_dev) RTE_DEV_TO_VDEV((eth_dev)->device) 38 39 static inline const char * 40 rte_vdev_device_name(const struct rte_vdev_device *dev) 41 { 42 if (dev && dev->device.name) 43 return dev->device.name; 44 return NULL; 45 } 46 47 static inline const char * 48 rte_vdev_device_args(const struct rte_vdev_device *dev) 49 { 50 if (dev && dev->device.devargs) 51 return dev->device.devargs->args; 52 return ""; 53 } 54 55 /** Double linked list of virtual device drivers. */ 56 TAILQ_HEAD(vdev_driver_list, rte_vdev_driver); 57 58 /** 59 * Probe function called for each virtual device driver once. 60 */ 61 typedef int (rte_vdev_probe_t)(struct rte_vdev_device *dev); 62 63 /** 64 * Remove function called for each virtual device driver once. 65 */ 66 typedef int (rte_vdev_remove_t)(struct rte_vdev_device *dev); 67 68 /** 69 * Driver-specific DMA mapping. After a successful call the device 70 * will be able to read/write from/to this segment. 71 * 72 * @param dev 73 * Pointer to the Virtual device. 74 * @param addr 75 * Starting virtual address of memory to be mapped. 76 * @param iova 77 * Starting IOVA address of memory to be mapped. 78 * @param len 79 * Length of memory segment being mapped. 80 * @return 81 * - 0 On success. 82 * - Negative value and rte_errno is set otherwise. 83 */ 84 typedef int (rte_vdev_dma_map_t)(struct rte_vdev_device *dev, void *addr, 85 uint64_t iova, size_t len); 86 87 /** 88 * Driver-specific DMA un-mapping. After a successful call the device 89 * will not be able to read/write from/to this segment. 90 * 91 * @param dev 92 * Pointer to the Virtual device. 93 * @param addr 94 * Starting virtual address of memory to be unmapped. 95 * @param iova 96 * Starting IOVA address of memory to be unmapped. 97 * @param len 98 * Length of memory segment being unmapped. 99 * @return 100 * - 0 On success. 101 * - Negative value and rte_errno is set otherwise. 102 */ 103 typedef int (rte_vdev_dma_unmap_t)(struct rte_vdev_device *dev, void *addr, 104 uint64_t iova, size_t len); 105 106 /** 107 * A virtual device driver abstraction. 108 */ 109 struct rte_vdev_driver { 110 TAILQ_ENTRY(rte_vdev_driver) next; /**< Next in list. */ 111 struct rte_driver driver; /**< Inherited general driver. */ 112 rte_vdev_probe_t *probe; /**< Virtual device probe function. */ 113 rte_vdev_remove_t *remove; /**< Virtual device remove function. */ 114 rte_vdev_dma_map_t *dma_map; /**< Virtual device DMA map function. */ 115 rte_vdev_dma_unmap_t *dma_unmap; /**< Virtual device DMA unmap function. */ 116 uint32_t drv_flags; /**< Flags RTE_VDEV_DRV_*. */ 117 }; 118 119 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */ 120 #define RTE_VDEV_DRV_NEED_IOVA_AS_VA 0x0001 121 122 /** 123 * Register a virtual device driver. 124 * 125 * @param driver 126 * A pointer to a rte_vdev_driver structure describing the driver 127 * to be registered. 128 */ 129 void rte_vdev_register(struct rte_vdev_driver *driver); 130 131 /** 132 * Unregister a virtual device driver. 133 * 134 * @param driver 135 * A pointer to a rte_vdev_driver structure describing the driver 136 * to be unregistered. 137 */ 138 void rte_vdev_unregister(struct rte_vdev_driver *driver); 139 140 #define RTE_PMD_REGISTER_VDEV(nm, vdrv)\ 141 static const char *vdrvinit_ ## nm ## _alias;\ 142 RTE_INIT(vdrvinitfn_ ##vdrv)\ 143 {\ 144 (vdrv).driver.name = RTE_STR(nm);\ 145 (vdrv).driver.alias = vdrvinit_ ## nm ## _alias;\ 146 rte_vdev_register(&vdrv);\ 147 } \ 148 RTE_PMD_EXPORT_NAME(nm, __COUNTER__) 149 150 #define RTE_PMD_REGISTER_ALIAS(nm, alias)\ 151 static const char *vdrvinit_ ## nm ## _alias = RTE_STR(alias) 152 153 typedef void (*rte_vdev_scan_callback)(void *user_arg); 154 155 /** 156 * Add a callback to be called on vdev scan 157 * before reading the devargs list. 158 * 159 * This function cannot be called in a scan callback 160 * because of deadlock. 161 * 162 * @param callback 163 * The function to be called which can update the devargs list. 164 * @param user_arg 165 * An opaque pointer passed to callback. 166 * @return 167 * 0 on success, negative on error 168 */ 169 int 170 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg); 171 172 /** 173 * Remove a registered scan callback. 174 * 175 * This function cannot be called in a scan callback 176 * because of deadlock. 177 * 178 * @param callback 179 * The registered function to be removed. 180 * @param user_arg 181 * The associated opaque pointer or (void*)-1 for any. 182 * @return 183 * 0 on success 184 */ 185 int 186 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg); 187 188 /** 189 * Initialize a driver specified by name. 190 * 191 * @param name 192 * The pointer to a driver name to be initialized. 193 * @param args 194 * The pointer to arguments used by driver initialization. 195 * @return 196 * 0 on success, negative on error 197 */ 198 int rte_vdev_init(const char *name, const char *args); 199 200 /** 201 * Uninitalize a driver specified by name. 202 * 203 * @param name 204 * The pointer to a driver name to be uninitialized. 205 * @return 206 * 0 on success, negative on error 207 */ 208 int rte_vdev_uninit(const char *name); 209 210 #ifdef __cplusplus 211 } 212 #endif 213 214 #endif 215