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)
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 */
65 * A structure describing a PCI device.
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 */
85 * Helper macro for drivers that need to convert to struct rte_pci_device.
87 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
89 #define RTE_DEV_TO_PCI_CONST(ptr) \
90 container_of(ptr, const struct rte_pci_device, device)
92 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
94 /** Any PCI device identifier (vendor, device, ...) */
95 #define PCI_ANY_ID (0xffff)
96 #define RTE_CLASS_ANY_ID (0xffffff)
99 /** C++ macro used to help building up tables of device IDs */
100 #define RTE_PCI_DEVICE(vend, dev) \
107 /** Macro used to help building up tables of device IDs */
108 #define RTE_PCI_DEVICE(vend, dev) \
109 .class_id = RTE_CLASS_ANY_ID, \
110 .vendor_id = (vend), \
111 .device_id = (dev), \
112 .subsystem_vendor_id = PCI_ANY_ID, \
113 .subsystem_device_id = PCI_ANY_ID
117 * Initialisation function for the driver called during PCI probing.
119 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
122 * Uninitialisation function for the driver called during hotplugging.
124 typedef int (pci_remove_t)(struct rte_pci_device *);
127 * Driver-specific DMA mapping. After a successful call the device
128 * will be able to read/write from/to this segment.
131 * Pointer to the PCI device.
133 * Starting virtual address of memory to be mapped.
135 * Starting IOVA address of memory to be mapped.
137 * Length of memory segment being mapped.
140 * - Negative value and rte_errno is set otherwise.
142 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
143 uint64_t iova, size_t len);
146 * Driver-specific DMA un-mapping. After a successful call the device
147 * will not be able to read/write from/to this segment.
150 * Pointer to the PCI device.
152 * Starting virtual address of memory to be unmapped.
154 * Starting IOVA address of memory to be unmapped.
156 * Length of memory segment being unmapped.
159 * - Negative value and rte_errno is set otherwise.
161 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
162 uint64_t iova, size_t len);
165 * A structure describing a PCI driver.
167 struct rte_pci_driver {
168 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
169 struct rte_driver driver; /**< Inherit core driver. */
170 struct rte_pci_bus *bus; /**< PCI bus reference. */
171 pci_probe_t *probe; /**< Device Probe function. */
172 pci_remove_t *remove; /**< Device Remove function. */
173 pci_dma_map_t *dma_map; /**< device dma map function. */
174 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */
175 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
176 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */
180 * Structure describing the PCI bus
183 struct rte_bus bus; /**< Inherit the generic class */
184 struct rte_pci_device_list device_list; /**< List of PCI devices */
185 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
188 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
189 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
190 /** Device needs PCI BAR mapping with enabled write combining (wc) */
191 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
192 /** Device already probed can be probed again to check for new ports. */
193 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
194 /** Device driver supports link state interrupt */
195 #define RTE_PCI_DRV_INTR_LSC 0x0008
196 /** Device driver supports device removal interrupt */
197 #define RTE_PCI_DRV_INTR_RMV 0x0010
198 /** Device driver needs to keep mapped resources if unsupported dev detected */
199 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
200 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */
201 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040
204 * Map the PCI device resources in user space virtual memory address
206 * Note that driver should not call this function when flag
207 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
211 * A pointer to a rte_pci_device structure describing the device
215 * 0 on success, negative on error and positive if no driver
216 * is found for the device.
218 int rte_pci_map_device(struct rte_pci_device *dev);
224 * A pointer to a rte_pci_device structure describing the device
227 void rte_pci_unmap_device(struct rte_pci_device *dev);
230 * Dump the content of the PCI bus.
233 * A pointer to a file for output
235 void rte_pci_dump(FILE *f);
238 * Find device's extended PCI capability.
241 * A pointer to rte_pci_device structure.
244 * Extended capability to be found, which can be any from
245 * RTE_PCI_EXT_CAP_ID_*, defined in librte_pci.
248 * > 0: The offset of the next matching extended capability structure
249 * within the device's PCI configuration space.
250 * < 0: An error in PCI config space read.
251 * = 0: Device does not support it.
254 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap);
257 * Register a PCI driver.
260 * A pointer to a rte_pci_driver structure describing the driver
263 void rte_pci_register(struct rte_pci_driver *driver);
265 /** Helper for PCI device registration from driver (eth, crypto) instance */
266 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
267 RTE_INIT(pciinitfn_ ##nm) \
269 (pci_drv).driver.name = RTE_STR(nm);\
270 rte_pci_register(&pci_drv); \
272 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
275 * Unregister a PCI driver.
278 * A pointer to a rte_pci_driver structure describing the driver
279 * to be unregistered.
281 void rte_pci_unregister(struct rte_pci_driver *driver);
284 * Read PCI config space.
287 * A pointer to a rte_pci_device structure describing the device
290 * A data buffer where the bytes should be read into
292 * The length of the data buffer.
294 * The offset into PCI config space
296 * Number of bytes read on success, negative on error.
298 int rte_pci_read_config(const struct rte_pci_device *device,
299 void *buf, size_t len, off_t offset);
302 * Write PCI config space.
305 * A pointer to a rte_pci_device structure describing the device
308 * A data buffer containing the bytes should be written
310 * The length of the data buffer.
312 * The offset into PCI config space
314 int rte_pci_write_config(const struct rte_pci_device *device,
315 const void *buf, size_t len, off_t offset);
318 * A structure used to access io resources for a pci device.
319 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
322 struct rte_pci_ioport {
323 struct rte_pci_device *dev;
325 uint64_t len; /* only filled for memory mapped ports */
329 * Initialize a rte_pci_ioport object for a pci device io resource.
331 * This object is then used to gain access to those io resources (see below).
334 * A pointer to a rte_pci_device structure describing the device
337 * Index of the io pci resource we want to access.
339 * The rte_pci_ioport object to be initialized.
341 * 0 on success, negative on error.
343 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
344 struct rte_pci_ioport *p);
347 * Release any resources used in a rte_pci_ioport object.
350 * The rte_pci_ioport object to be uninitialized.
352 * 0 on success, negative on error.
354 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
357 * Read from a io pci resource.
360 * The rte_pci_ioport object from which we want to read.
362 * A data buffer where the bytes should be read into
364 * The length of the data buffer.
366 * The offset into the pci io resource.
368 void rte_pci_ioport_read(struct rte_pci_ioport *p,
369 void *data, size_t len, off_t offset);
372 * Write to a io pci resource.
375 * The rte_pci_ioport object to which we want to write.
377 * A data buffer where the bytes should be read into
379 * The length of the data buffer.
381 * The offset into the pci io resource.
383 void rte_pci_ioport_write(struct rte_pci_ioport *p,
384 const void *data, size_t len, off_t offset);
390 #endif /* _RTE_BUS_PCI_H_ */