1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 3 */ 4 5 #ifndef BUS_CDX_DRIVER_H 6 #define BUS_CDX_DRIVER_H 7 8 /** 9 * @file 10 * AMD CDX bus interface 11 */ 12 13 #include <stdlib.h> 14 #include <inttypes.h> 15 #include <linux/types.h> 16 17 #include <bus_driver.h> 18 #include <dev_driver.h> 19 #include <rte_interrupts.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /* Forward declarations */ 26 struct rte_cdx_device; 27 struct rte_cdx_driver; 28 struct rte_cdx_bus; 29 30 #define RTE_CDX_BUS_DEVICES_PATH "/sys/bus/cdx/devices" 31 32 #define RTE_CDX_MAX_RESOURCE 4 33 34 /** Any CDX device identifier (vendor, device). */ 35 #define RTE_CDX_ANY_ID (0xffff) 36 37 #define RTE_PMD_REGISTER_CDX_TABLE(name, table) \ 38 static const char DRV_EXP_TAG(name, cdx_tbl_export)[] __rte_used = \ 39 RTE_STR(table) 40 41 /** Device needs resource mapping */ 42 #define RTE_CDX_DRV_NEED_MAPPING 0x0001 43 44 /** 45 * A structure describing an ID for a CDX driver. Each driver provides a 46 * table of these IDs for each device that it supports. 47 */ 48 struct rte_cdx_id { 49 uint16_t vendor_id; /**< Vendor ID. */ 50 uint16_t device_id; /**< Device ID. */ 51 }; 52 53 /** 54 * A structure describing a CDX device. 55 */ 56 struct rte_cdx_device { 57 RTE_TAILQ_ENTRY(rte_cdx_device) next; /**< Next probed CDX device. */ 58 struct rte_device device; /**< Inherit core device */ 59 struct rte_cdx_driver *driver; /**< CDX driver used in probing */ 60 char name[RTE_DEV_NAME_MAX_LEN]; /**< Device name */ 61 struct rte_cdx_id id; /**< CDX ID. */ 62 struct rte_mem_resource mem_resource[RTE_CDX_MAX_RESOURCE]; 63 /**< CDX Memory Resource */ 64 struct rte_intr_handle *intr_handle; /**< Interrupt handle */ 65 }; 66 67 /** 68 * @internal 69 * Helper macro for drivers that need to convert to struct rte_cdx_device. 70 */ 71 #define RTE_DEV_TO_CDX_DEV(ptr) \ 72 container_of(ptr, struct rte_cdx_device, device) 73 74 #define RTE_DEV_TO_CDX_DEV_CONST(ptr) \ 75 container_of(ptr, const struct rte_cdx_device, device) 76 77 #define RTE_ETH_DEV_TO_CDX_DEV(eth_dev) RTE_DEV_TO_CDX_DEV((eth_dev)->device) 78 79 #ifdef __cplusplus 80 /** C++ macro used to help building up tables of device IDs. */ 81 #define RTE_CDX_DEVICE(vend, dev) \ 82 (vend), \ 83 (dev) 84 #else 85 /** Macro used to help building up tables of device IDs. */ 86 #define RTE_CDX_DEVICE(vend, dev) \ 87 .vendor_id = (vend), \ 88 .device_id = (dev) 89 #endif 90 91 /** 92 * Initialisation function for the driver called during CDX probing. 93 */ 94 typedef int (rte_cdx_probe_t)(struct rte_cdx_driver *, struct rte_cdx_device *); 95 96 /** 97 * Uninitialisation function for the driver called during hotplugging. 98 */ 99 typedef int (rte_cdx_remove_t)(struct rte_cdx_device *); 100 101 /** 102 * A structure describing a CDX driver. 103 */ 104 struct rte_cdx_driver { 105 RTE_TAILQ_ENTRY(rte_cdx_driver) next; /**< Next in list. */ 106 struct rte_driver driver; /**< Inherit core driver. */ 107 struct rte_cdx_bus *bus; /**< CDX bus reference. */ 108 rte_cdx_probe_t *probe; /**< Device probe function. */ 109 rte_cdx_remove_t *remove; /**< Device remove function. */ 110 const struct rte_cdx_id *id_table; /**< ID table, NULL terminated. */ 111 uint32_t drv_flags; /**< Flags RTE_CDX_DRV_*. */ 112 }; 113 114 /** 115 * Map the CDX device resources in user space virtual memory address. 116 * 117 * @param dev 118 * A pointer to a rte_cdx_device structure describing the device 119 * to use. 120 * 121 * @return 122 * 0 on success, <0 on error. 123 */ 124 __rte_internal 125 int rte_cdx_map_device(struct rte_cdx_device *dev); 126 127 /** 128 * Unmap this device. 129 * 130 * @param dev 131 * A pointer to a rte_cdx_device structure describing the device 132 * to use. 133 */ 134 __rte_internal 135 void rte_cdx_unmap_device(struct rte_cdx_device *dev); 136 137 /** 138 * Register a CDX driver. 139 * 140 * @param driver 141 * A pointer to a rte_cdx_driver structure describing the driver 142 * to be registered. 143 */ 144 __rte_internal 145 void rte_cdx_register(struct rte_cdx_driver *driver); 146 147 /** 148 * Helper for CDX device registration from driver (eth, crypto, raw) instance. 149 */ 150 #define RTE_PMD_REGISTER_CDX(nm, cdx_drv) \ 151 RTE_INIT(cdxinitfn_ ##nm) \ 152 {\ 153 (cdx_drv).driver.name = RTE_STR(nm);\ 154 rte_cdx_register(&cdx_drv); \ 155 } \ 156 RTE_PMD_EXPORT_NAME(nm, __COUNTER__) 157 158 /** 159 * Enables VFIO Interrupts for CDX bus devices. 160 * 161 * @param intr_handle 162 * Pointer to the interrupt handle. 163 * 164 * @return 165 * 0 on success, -1 on error. 166 */ 167 __rte_internal 168 int rte_cdx_vfio_intr_enable(const struct rte_intr_handle *intr_handle); 169 170 /** 171 * Disable VFIO Interrupts for CDX bus devices. 172 * 173 * @param intr_handle 174 * Pointer to the interrupt handle. 175 * 176 * @return 177 * 0 on success, -1 on error. 178 */ 179 __rte_internal 180 int rte_cdx_vfio_intr_disable(const struct rte_intr_handle *intr_handle); 181 182 /** 183 * Enable Bus Mastering for CDX bus devices. 184 * 185 * @param dev 186 * Pointer to the cdx device. 187 * 188 * @return 189 * 0 on success, -1 on error. 190 */ 191 __rte_internal 192 int rte_cdx_vfio_bm_enable(struct rte_cdx_device *dev); 193 194 /** 195 * Disable Bus Mastering for CDX bus devices. 196 * 197 * @param dev 198 * Pointer to the cdx device. 199 * 200 * @return 201 * 0 on success, -1 on error. 202 */ 203 __rte_internal 204 int rte_cdx_vfio_bm_disable(struct rte_cdx_device *dev); 205 206 /** 207 * Unregister a CDX driver. 208 * 209 * @param driver 210 * A pointer to a rte_cdx_driver structure describing the driver 211 * to be unregistered. 212 */ 213 __rte_internal 214 void rte_cdx_unregister(struct rte_cdx_driver *driver); 215 216 #ifdef __cplusplus 217 } 218 #endif 219 220 #endif /* BUS_CDX_DRIVER_H */ 221