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