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 BUS_PCI_DRIVER_H 7 #define BUS_PCI_DRIVER_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #include "rte_bus_pci.h" 14 #include "dev_driver.h" 15 #include <rte_compat.h> 16 17 /** Pathname of PCI devices directory. */ 18 __rte_internal 19 const char *rte_pci_get_sysfs_path(void); 20 21 enum rte_pci_kernel_driver { 22 RTE_PCI_KDRV_UNKNOWN = 0, /* may be misc UIO or bifurcated driver */ 23 RTE_PCI_KDRV_IGB_UIO, /* igb_uio for Linux */ 24 RTE_PCI_KDRV_VFIO, /* VFIO for Linux */ 25 RTE_PCI_KDRV_UIO_GENERIC, /* uio_pci_generic for Linux */ 26 RTE_PCI_KDRV_NIC_UIO, /* nic_uio for FreeBSD */ 27 RTE_PCI_KDRV_NONE, /* no attached driver */ 28 RTE_PCI_KDRV_NET_UIO, /* NetUIO for Windows */ 29 }; 30 31 /** 32 * A structure describing a PCI device. 33 */ 34 struct rte_pci_device { 35 RTE_TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */ 36 struct rte_device device; /**< Inherit core device */ 37 struct rte_pci_addr addr; /**< PCI location. */ 38 struct rte_pci_id id; /**< PCI ID. */ 39 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE]; 40 /**< PCI Memory Resource */ 41 struct rte_intr_handle *intr_handle; /**< Interrupt handle */ 42 struct rte_pci_driver *driver; /**< PCI driver used in probing */ 43 uint16_t max_vfs; /**< sriov enable if not zero */ 44 enum rte_pci_kernel_driver kdrv; /**< Kernel driver passthrough */ 45 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */ 46 char *bus_info; /**< PCI bus specific info */ 47 struct rte_intr_handle *vfio_req_intr_handle; 48 /**< Handler of VFIO request interrupt */ 49 }; 50 51 /** 52 * @internal 53 * Helper macro for drivers that need to convert to struct rte_pci_device. 54 */ 55 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device) 56 57 #define RTE_DEV_TO_PCI_CONST(ptr) \ 58 container_of(ptr, const struct rte_pci_device, device) 59 60 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device) 61 62 #ifdef __cplusplus 63 /** C++ macro used to help building up tables of device IDs */ 64 #define RTE_PCI_DEVICE(vend, dev) \ 65 RTE_CLASS_ANY_ID, \ 66 (vend), \ 67 (dev), \ 68 RTE_PCI_ANY_ID, \ 69 RTE_PCI_ANY_ID 70 #else 71 /** Macro used to help building up tables of device IDs */ 72 #define RTE_PCI_DEVICE(vend, dev) \ 73 .class_id = RTE_CLASS_ANY_ID, \ 74 .vendor_id = (vend), \ 75 .device_id = (dev), \ 76 .subsystem_vendor_id = RTE_PCI_ANY_ID, \ 77 .subsystem_device_id = RTE_PCI_ANY_ID 78 #endif 79 80 /** 81 * Initialisation function for the driver called during PCI probing. 82 */ 83 typedef int (rte_pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *); 84 85 /** 86 * Uninitialisation function for the driver called during hotplugging. 87 */ 88 typedef int (rte_pci_remove_t)(struct rte_pci_device *); 89 90 /** 91 * Driver-specific DMA mapping. After a successful call the device 92 * will be able to read/write from/to this segment. 93 * 94 * @param dev 95 * Pointer to the PCI device. 96 * @param addr 97 * Starting virtual address of memory to be mapped. 98 * @param iova 99 * Starting IOVA address of memory to be mapped. 100 * @param len 101 * Length of memory segment being mapped. 102 * @return 103 * - 0 On success. 104 * - Negative value and rte_errno is set otherwise. 105 */ 106 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr, 107 uint64_t iova, size_t len); 108 109 /** 110 * Driver-specific DMA un-mapping. After a successful call the device 111 * will not be able to read/write from/to this segment. 112 * 113 * @param dev 114 * Pointer to the PCI device. 115 * @param addr 116 * Starting virtual address of memory to be unmapped. 117 * @param iova 118 * Starting IOVA address of memory to be unmapped. 119 * @param len 120 * Length of memory segment being unmapped. 121 * @return 122 * - 0 On success. 123 * - Negative value and rte_errno is set otherwise. 124 */ 125 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr, 126 uint64_t iova, size_t len); 127 128 /** 129 * A structure describing a PCI driver. 130 */ 131 struct rte_pci_driver { 132 RTE_TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */ 133 struct rte_driver driver; /**< Inherit core driver. */ 134 rte_pci_probe_t *probe; /**< Device probe function. */ 135 rte_pci_remove_t *remove; /**< Device remove function. */ 136 pci_dma_map_t *dma_map; /**< device dma map function. */ 137 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */ 138 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */ 139 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */ 140 }; 141 142 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */ 143 #define RTE_PCI_DRV_NEED_MAPPING 0x0001 144 /** Device needs PCI BAR mapping with enabled write combining (wc) */ 145 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002 146 /** Device already probed can be probed again to check for new ports. */ 147 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004 148 /** Device driver supports link state interrupt */ 149 #define RTE_PCI_DRV_INTR_LSC 0x0008 150 /** Device driver supports device removal interrupt */ 151 #define RTE_PCI_DRV_INTR_RMV 0x0010 152 /** Device driver needs to keep mapped resources if unsupported dev detected */ 153 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020 154 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */ 155 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040 156 157 /** 158 * Register a PCI driver. 159 * 160 * @param driver 161 * A pointer to a rte_pci_driver structure describing the driver 162 * to be registered. 163 */ 164 __rte_internal 165 void rte_pci_register(struct rte_pci_driver *driver); 166 167 /** Helper for PCI device registration from driver (eth, crypto) instance */ 168 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \ 169 RTE_INIT(pciinitfn_ ##nm) \ 170 {\ 171 (pci_drv).driver.name = RTE_STR(nm);\ 172 rte_pci_register(&pci_drv); \ 173 } \ 174 RTE_PMD_EXPORT_NAME(nm, __COUNTER__) 175 176 /** 177 * Unregister a PCI driver. 178 * 179 * @param driver 180 * A pointer to a rte_pci_driver structure describing the driver 181 * to be unregistered. 182 */ 183 __rte_internal 184 void rte_pci_unregister(struct rte_pci_driver *driver); 185 186 /* 187 * A structure used to access io resources for a pci device. 188 * rte_pci_ioport is arch, os, driver specific, and should not be used outside 189 * of pci ioport api. 190 */ 191 struct rte_pci_ioport { 192 struct rte_pci_device *dev; 193 uint64_t base; 194 uint64_t len; /* only filled for memory mapped ports */ 195 }; 196 197 #ifdef __cplusplus 198 } 199 #endif 200 201 #endif /* BUS_PCI_DRIVER_H */ 202