1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2015 Intel Corporation. 3 * Copyright 2013-2014 6WIND S.A. 4 */ 5 6 #ifndef _RTE_BUS_PCI_H_ 7 #define _RTE_BUS_PCI_H_ 8 9 /** 10 * @file 11 * 12 * RTE PCI Bus Interface 13 */ 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <limits.h> 22 #include <errno.h> 23 #include <sys/queue.h> 24 #include <stdint.h> 25 #include <inttypes.h> 26 27 #include <rte_debug.h> 28 #include <rte_interrupts.h> 29 #include <rte_dev.h> 30 #include <rte_bus.h> 31 #include <rte_pci.h> 32 33 /** Pathname of PCI devices directory. */ 34 const char *rte_pci_get_sysfs_path(void); 35 36 /* Forward declarations */ 37 struct rte_pci_device; 38 struct rte_pci_driver; 39 40 /** List of PCI devices */ 41 TAILQ_HEAD(rte_pci_device_list, rte_pci_device); 42 /** List of PCI drivers */ 43 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver); 44 45 /* PCI Bus iterators */ 46 #define FOREACH_DEVICE_ON_PCIBUS(p) \ 47 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next) 48 49 #define FOREACH_DRIVER_ON_PCIBUS(p) \ 50 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next) 51 52 struct rte_devargs; 53 54 enum rte_pci_kernel_driver { 55 RTE_PCI_KDRV_UNKNOWN = 0, 56 RTE_PCI_KDRV_IGB_UIO, 57 RTE_PCI_KDRV_VFIO, 58 RTE_PCI_KDRV_UIO_GENERIC, 59 RTE_PCI_KDRV_NIC_UIO, 60 RTE_PCI_KDRV_NONE, 61 }; 62 63 /** 64 * A structure describing a PCI device. 65 */ 66 struct rte_pci_device { 67 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */ 68 struct rte_device device; /**< Inherit core device */ 69 struct rte_pci_addr addr; /**< PCI location. */ 70 struct rte_pci_id id; /**< PCI ID. */ 71 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE]; 72 /**< PCI Memory Resource */ 73 struct rte_intr_handle intr_handle; /**< Interrupt handle */ 74 struct rte_pci_driver *driver; /**< PCI driver used in probing */ 75 uint16_t max_vfs; /**< sriov enable if not zero */ 76 enum rte_pci_kernel_driver kdrv; /**< Kernel driver passthrough */ 77 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */ 78 struct rte_intr_handle vfio_req_intr_handle; 79 /**< Handler of VFIO request interrupt */ 80 }; 81 82 /** 83 * @internal 84 * Helper macro for drivers that need to convert to struct rte_pci_device. 85 */ 86 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device) 87 88 #define RTE_DEV_TO_PCI_CONST(ptr) \ 89 container_of(ptr, const struct rte_pci_device, device) 90 91 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device) 92 93 /** Any PCI device identifier (vendor, device, ...) */ 94 #define PCI_ANY_ID (0xffff) 95 #define RTE_CLASS_ANY_ID (0xffffff) 96 97 #ifdef __cplusplus 98 /** C++ macro used to help building up tables of device IDs */ 99 #define RTE_PCI_DEVICE(vend, dev) \ 100 RTE_CLASS_ANY_ID, \ 101 (vend), \ 102 (dev), \ 103 PCI_ANY_ID, \ 104 PCI_ANY_ID 105 #else 106 /** Macro used to help building up tables of device IDs */ 107 #define RTE_PCI_DEVICE(vend, dev) \ 108 .class_id = RTE_CLASS_ANY_ID, \ 109 .vendor_id = (vend), \ 110 .device_id = (dev), \ 111 .subsystem_vendor_id = PCI_ANY_ID, \ 112 .subsystem_device_id = PCI_ANY_ID 113 #endif 114 115 /** 116 * Initialisation function for the driver called during PCI probing. 117 */ 118 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *); 119 120 /** 121 * Uninitialisation function for the driver called during hotplugging. 122 */ 123 typedef int (pci_remove_t)(struct rte_pci_device *); 124 125 /** 126 * Driver-specific DMA mapping. After a successful call the device 127 * will be able to read/write from/to this segment. 128 * 129 * @param dev 130 * Pointer to the PCI device. 131 * @param addr 132 * Starting virtual address of memory to be mapped. 133 * @param iova 134 * Starting IOVA address of memory to be mapped. 135 * @param len 136 * Length of memory segment being mapped. 137 * @return 138 * - 0 On success. 139 * - Negative value and rte_errno is set otherwise. 140 */ 141 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr, 142 uint64_t iova, size_t len); 143 144 /** 145 * Driver-specific DMA un-mapping. After a successful call the device 146 * will not be able to read/write from/to this segment. 147 * 148 * @param dev 149 * Pointer to the PCI device. 150 * @param addr 151 * Starting virtual address of memory to be unmapped. 152 * @param iova 153 * Starting IOVA address of memory to be unmapped. 154 * @param len 155 * Length of memory segment being unmapped. 156 * @return 157 * - 0 On success. 158 * - Negative value and rte_errno is set otherwise. 159 */ 160 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr, 161 uint64_t iova, size_t len); 162 163 /** 164 * A structure describing a PCI driver. 165 */ 166 struct rte_pci_driver { 167 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */ 168 struct rte_driver driver; /**< Inherit core driver. */ 169 struct rte_pci_bus *bus; /**< PCI bus reference. */ 170 pci_probe_t *probe; /**< Device Probe function. */ 171 pci_remove_t *remove; /**< Device Remove function. */ 172 pci_dma_map_t *dma_map; /**< device dma map function. */ 173 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */ 174 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */ 175 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */ 176 }; 177 178 /** 179 * Structure describing the PCI bus 180 */ 181 struct rte_pci_bus { 182 struct rte_bus bus; /**< Inherit the generic class */ 183 struct rte_pci_device_list device_list; /**< List of PCI devices */ 184 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */ 185 }; 186 187 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */ 188 #define RTE_PCI_DRV_NEED_MAPPING 0x0001 189 /** Device needs PCI BAR mapping with enabled write combining (wc) */ 190 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002 191 /** Device already probed can be probed again to check for new ports. */ 192 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004 193 /** Device driver supports link state interrupt */ 194 #define RTE_PCI_DRV_INTR_LSC 0x0008 195 /** Device driver supports device removal interrupt */ 196 #define RTE_PCI_DRV_INTR_RMV 0x0010 197 /** Device driver needs to keep mapped resources if unsupported dev detected */ 198 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020 199 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */ 200 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040 201 202 /** 203 * Map the PCI device resources in user space virtual memory address 204 * 205 * Note that driver should not call this function when flag 206 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for 207 * you when it's on. 208 * 209 * @param dev 210 * A pointer to a rte_pci_device structure describing the device 211 * to use 212 * 213 * @return 214 * 0 on success, negative on error and positive if no driver 215 * is found for the device. 216 */ 217 int rte_pci_map_device(struct rte_pci_device *dev); 218 219 /** 220 * Unmap this device 221 * 222 * @param dev 223 * A pointer to a rte_pci_device structure describing the device 224 * to use 225 */ 226 void rte_pci_unmap_device(struct rte_pci_device *dev); 227 228 /** 229 * Dump the content of the PCI bus. 230 * 231 * @param f 232 * A pointer to a file for output 233 */ 234 void rte_pci_dump(FILE *f); 235 236 /** 237 * Find device's extended PCI capability. 238 * 239 * @param dev 240 * A pointer to rte_pci_device structure. 241 * 242 * @param cap 243 * Extended capability to be found, which can be any from 244 * RTE_PCI_EXT_CAP_ID_*, defined in librte_pci. 245 * 246 * @return 247 * > 0: The offset of the next matching extended capability structure 248 * within the device's PCI configuration space. 249 * < 0: An error in PCI config space read. 250 * = 0: Device does not support it. 251 */ 252 __rte_experimental 253 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap); 254 255 /** 256 * Register a PCI driver. 257 * 258 * @param driver 259 * A pointer to a rte_pci_driver structure describing the driver 260 * to be registered. 261 */ 262 void rte_pci_register(struct rte_pci_driver *driver); 263 264 /** Helper for PCI device registration from driver (eth, crypto) instance */ 265 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \ 266 RTE_INIT(pciinitfn_ ##nm) \ 267 {\ 268 (pci_drv).driver.name = RTE_STR(nm);\ 269 rte_pci_register(&pci_drv); \ 270 } \ 271 RTE_PMD_EXPORT_NAME(nm, __COUNTER__) 272 273 /** 274 * Unregister a PCI driver. 275 * 276 * @param driver 277 * A pointer to a rte_pci_driver structure describing the driver 278 * to be unregistered. 279 */ 280 void rte_pci_unregister(struct rte_pci_driver *driver); 281 282 /** 283 * Read PCI config space. 284 * 285 * @param device 286 * A pointer to a rte_pci_device structure describing the device 287 * to use 288 * @param buf 289 * A data buffer where the bytes should be read into 290 * @param len 291 * The length of the data buffer. 292 * @param offset 293 * The offset into PCI config space 294 * @return 295 * Number of bytes read on success, negative on error. 296 */ 297 int rte_pci_read_config(const struct rte_pci_device *device, 298 void *buf, size_t len, off_t offset); 299 300 /** 301 * Write PCI config space. 302 * 303 * @param device 304 * A pointer to a rte_pci_device structure describing the device 305 * to use 306 * @param buf 307 * A data buffer containing the bytes should be written 308 * @param len 309 * The length of the data buffer. 310 * @param offset 311 * The offset into PCI config space 312 */ 313 int rte_pci_write_config(const struct rte_pci_device *device, 314 const void *buf, size_t len, off_t offset); 315 316 /** 317 * A structure used to access io resources for a pci device. 318 * rte_pci_ioport is arch, os, driver specific, and should not be used outside 319 * of pci ioport api. 320 */ 321 struct rte_pci_ioport { 322 struct rte_pci_device *dev; 323 uint64_t base; 324 uint64_t len; /* only filled for memory mapped ports */ 325 }; 326 327 /** 328 * Initialize a rte_pci_ioport object for a pci device io resource. 329 * 330 * This object is then used to gain access to those io resources (see below). 331 * 332 * @param dev 333 * A pointer to a rte_pci_device structure describing the device 334 * to use. 335 * @param bar 336 * Index of the io pci resource we want to access. 337 * @param p 338 * The rte_pci_ioport object to be initialized. 339 * @return 340 * 0 on success, negative on error. 341 */ 342 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar, 343 struct rte_pci_ioport *p); 344 345 /** 346 * Release any resources used in a rte_pci_ioport object. 347 * 348 * @param p 349 * The rte_pci_ioport object to be uninitialized. 350 * @return 351 * 0 on success, negative on error. 352 */ 353 int rte_pci_ioport_unmap(struct rte_pci_ioport *p); 354 355 /** 356 * Read from a io pci resource. 357 * 358 * @param p 359 * The rte_pci_ioport object from which we want to read. 360 * @param data 361 * A data buffer where the bytes should be read into 362 * @param len 363 * The length of the data buffer. 364 * @param offset 365 * The offset into the pci io resource. 366 */ 367 void rte_pci_ioport_read(struct rte_pci_ioport *p, 368 void *data, size_t len, off_t offset); 369 370 /** 371 * Write to a io pci resource. 372 * 373 * @param p 374 * The rte_pci_ioport object to which we want to write. 375 * @param data 376 * A data buffer where the bytes should be read into 377 * @param len 378 * The length of the data buffer. 379 * @param offset 380 * The offset into the pci io resource. 381 */ 382 void rte_pci_ioport_write(struct rte_pci_ioport *p, 383 const void *data, size_t len, off_t offset); 384 385 #ifdef __cplusplus 386 } 387 #endif 388 389 #endif /* _RTE_BUS_PCI_H_ */ 390