1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
6 #ifndef _RTE_BUS_PCI_H_
7 #define _RTE_BUS_PCI_H_
12 * RTE PCI Bus Interface
23 #include <sys/queue.h>
27 #include <rte_debug.h>
28 #include <rte_interrupts.h>
33 /** Pathname of PCI devices directory. */
34 const char *rte_pci_get_sysfs_path(void);
36 /* Forward declarations */
37 struct rte_pci_device;
38 struct rte_pci_driver;
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);
45 /* PCI Bus iterators */
46 #define FOREACH_DEVICE_ON_PCIBUS(p) \
47 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
49 #define FOREACH_DRIVER_ON_PCIBUS(p) \
50 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
55 * A structure describing a PCI device.
57 struct rte_pci_device {
58 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
59 struct rte_device device; /**< Inherit core device */
60 struct rte_pci_addr addr; /**< PCI location. */
61 struct rte_pci_id id; /**< PCI ID. */
62 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
63 /**< PCI Memory Resource */
64 struct rte_intr_handle intr_handle; /**< Interrupt handle */
65 struct rte_pci_driver *driver; /**< PCI driver used in probing */
66 uint16_t max_vfs; /**< sriov enable if not zero */
67 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
68 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */
69 struct rte_intr_handle vfio_req_intr_handle;
70 /**< Handler of VFIO request interrupt */
75 * Helper macro for drivers that need to convert to struct rte_pci_device.
77 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
79 #define RTE_DEV_TO_PCI_CONST(ptr) \
80 container_of(ptr, const struct rte_pci_device, device)
82 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
84 /** Any PCI device identifier (vendor, device, ...) */
85 #define PCI_ANY_ID (0xffff)
86 #define RTE_CLASS_ANY_ID (0xffffff)
89 /** C++ macro used to help building up tables of device IDs */
90 #define RTE_PCI_DEVICE(vend, dev) \
97 /** Macro used to help building up tables of device IDs */
98 #define RTE_PCI_DEVICE(vend, dev) \
99 .class_id = RTE_CLASS_ANY_ID, \
100 .vendor_id = (vend), \
101 .device_id = (dev), \
102 .subsystem_vendor_id = PCI_ANY_ID, \
103 .subsystem_device_id = PCI_ANY_ID
107 * Initialisation function for the driver called during PCI probing.
109 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
112 * Uninitialisation function for the driver called during hotplugging.
114 typedef int (pci_remove_t)(struct rte_pci_device *);
117 * Driver-specific DMA mapping. After a successful call the device
118 * will be able to read/write from/to this segment.
121 * Pointer to the PCI device.
123 * Starting virtual address of memory to be mapped.
125 * Starting IOVA address of memory to be mapped.
127 * Length of memory segment being mapped.
130 * - Negative value and rte_errno is set otherwise.
132 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
133 uint64_t iova, size_t len);
136 * Driver-specific DMA un-mapping. After a successful call the device
137 * will not be able to read/write from/to this segment.
140 * Pointer to the PCI device.
142 * Starting virtual address of memory to be unmapped.
144 * Starting IOVA address of memory to be unmapped.
146 * Length of memory segment being unmapped.
149 * - Negative value and rte_errno is set otherwise.
151 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
152 uint64_t iova, size_t len);
155 * A structure describing a PCI driver.
157 struct rte_pci_driver {
158 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
159 struct rte_driver driver; /**< Inherit core driver. */
160 struct rte_pci_bus *bus; /**< PCI bus reference. */
161 pci_probe_t *probe; /**< Device Probe function. */
162 pci_remove_t *remove; /**< Device Remove function. */
163 pci_dma_map_t *dma_map; /**< device dma map function. */
164 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */
165 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
166 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */
170 * Structure describing the PCI bus
173 struct rte_bus bus; /**< Inherit the generic class */
174 struct rte_pci_device_list device_list; /**< List of PCI devices */
175 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
178 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
179 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
180 /** Device needs PCI BAR mapping with enabled write combining (wc) */
181 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
182 /** Device already probed can be probed again to check for new ports. */
183 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
184 /** Device driver supports link state interrupt */
185 #define RTE_PCI_DRV_INTR_LSC 0x0008
186 /** Device driver supports device removal interrupt */
187 #define RTE_PCI_DRV_INTR_RMV 0x0010
188 /** Device driver needs to keep mapped resources if unsupported dev detected */
189 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
190 /** Device driver supports IOVA as VA */
191 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
194 * Map the PCI device resources in user space virtual memory address
196 * Note that driver should not call this function when flag
197 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
201 * A pointer to a rte_pci_device structure describing the device
205 * 0 on success, negative on error and positive if no driver
206 * is found for the device.
208 int rte_pci_map_device(struct rte_pci_device *dev);
214 * A pointer to a rte_pci_device structure describing the device
217 void rte_pci_unmap_device(struct rte_pci_device *dev);
220 * Dump the content of the PCI bus.
223 * A pointer to a file for output
225 void rte_pci_dump(FILE *f);
228 * Register a PCI driver.
231 * A pointer to a rte_pci_driver structure describing the driver
234 void rte_pci_register(struct rte_pci_driver *driver);
236 /** Helper for PCI device registration from driver (eth, crypto) instance */
237 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
238 RTE_INIT(pciinitfn_ ##nm) \
240 (pci_drv).driver.name = RTE_STR(nm);\
241 rte_pci_register(&pci_drv); \
243 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
246 * Unregister a PCI driver.
249 * A pointer to a rte_pci_driver structure describing the driver
250 * to be unregistered.
252 void rte_pci_unregister(struct rte_pci_driver *driver);
255 * Read PCI config space.
258 * A pointer to a rte_pci_device structure describing the device
261 * A data buffer where the bytes should be read into
263 * The length of the data buffer.
265 * The offset into PCI config space
267 * Number of bytes read on success, negative on error.
269 int rte_pci_read_config(const struct rte_pci_device *device,
270 void *buf, size_t len, off_t offset);
273 * Write PCI config space.
276 * A pointer to a rte_pci_device structure describing the device
279 * A data buffer containing the bytes should be written
281 * The length of the data buffer.
283 * The offset into PCI config space
285 int rte_pci_write_config(const struct rte_pci_device *device,
286 const void *buf, size_t len, off_t offset);
289 * A structure used to access io resources for a pci device.
290 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
293 struct rte_pci_ioport {
294 struct rte_pci_device *dev;
296 uint64_t len; /* only filled for memory mapped ports */
300 * Initialize a rte_pci_ioport object for a pci device io resource.
302 * This object is then used to gain access to those io resources (see below).
305 * A pointer to a rte_pci_device structure describing the device
308 * Index of the io pci resource we want to access.
310 * The rte_pci_ioport object to be initialized.
312 * 0 on success, negative on error.
314 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
315 struct rte_pci_ioport *p);
318 * Release any resources used in a rte_pci_ioport object.
321 * The rte_pci_ioport object to be uninitialized.
323 * 0 on success, negative on error.
325 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
328 * Read from a io pci resource.
331 * The rte_pci_ioport object from which we want to read.
333 * A data buffer where the bytes should be read into
335 * The length of the data buffer.
337 * The offset into the pci io resource.
339 void rte_pci_ioport_read(struct rte_pci_ioport *p,
340 void *data, size_t len, off_t offset);
343 * Write to a io pci resource.
346 * The rte_pci_ioport object to which we want to write.
348 * A data buffer where the bytes should be read into
350 * The length of the data buffer.
352 * The offset into the pci io resource.
354 void rte_pci_ioport_write(struct rte_pci_ioport *p,
355 const void *data, size_t len, off_t offset);
361 #endif /* _RTE_BUS_PCI_H_ */