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