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_
11 * PCI device & driver interface
25 #include <rte_debug.h>
26 #include <rte_interrupts.h>
31 /** Pathname of PCI devices directory. */
32 const char *rte_pci_get_sysfs_path(void);
34 /* Forward declarations */
35 struct rte_pci_device;
36 struct rte_pci_driver;
38 /** List of PCI devices */
39 RTE_TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
40 /** List of PCI drivers */
41 RTE_TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
43 /* PCI Bus iterators */
44 #define FOREACH_DEVICE_ON_PCIBUS(p) \
45 RTE_TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
47 #define FOREACH_DRIVER_ON_PCIBUS(p) \
48 RTE_TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
52 enum rte_pci_kernel_driver {
53 RTE_PCI_KDRV_UNKNOWN = 0, /* may be misc UIO or bifurcated driver */
54 RTE_PCI_KDRV_IGB_UIO, /* igb_uio for Linux */
55 RTE_PCI_KDRV_VFIO, /* VFIO for Linux */
56 RTE_PCI_KDRV_UIO_GENERIC, /* uio_pci_generic for Linux */
57 RTE_PCI_KDRV_NIC_UIO, /* nic_uio for FreeBSD */
58 RTE_PCI_KDRV_NONE, /* no attached driver */
59 RTE_PCI_KDRV_NET_UIO, /* NetUIO for Windows */
63 * A structure describing a PCI device.
65 struct rte_pci_device {
66 RTE_TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
67 struct rte_device device; /**< Inherit core device */
68 struct rte_pci_addr addr; /**< PCI location. */
69 struct rte_pci_id id; /**< PCI ID. */
70 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
71 /**< PCI Memory Resource */
72 struct rte_intr_handle *intr_handle; /**< Interrupt handle */
73 struct rte_pci_driver *driver; /**< PCI driver used in probing */
74 uint16_t max_vfs; /**< sriov enable if not zero */
75 enum rte_pci_kernel_driver kdrv; /**< Kernel driver passthrough */
76 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */
77 struct rte_intr_handle *vfio_req_intr_handle;
78 /**< Handler of VFIO request interrupt */
83 * Helper macro for drivers that need to convert to struct rte_pci_device.
85 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
87 #define RTE_DEV_TO_PCI_CONST(ptr) \
88 container_of(ptr, const struct rte_pci_device, device)
90 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
93 /** C++ macro used to help building up tables of device IDs */
94 #define RTE_PCI_DEVICE(vend, dev) \
101 /** Macro used to help building up tables of device IDs */
102 #define RTE_PCI_DEVICE(vend, dev) \
103 .class_id = RTE_CLASS_ANY_ID, \
104 .vendor_id = (vend), \
105 .device_id = (dev), \
106 .subsystem_vendor_id = RTE_PCI_ANY_ID, \
107 .subsystem_device_id = RTE_PCI_ANY_ID
111 * Initialisation function for the driver called during PCI probing.
113 typedef int (rte_pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
116 * Uninitialisation function for the driver called during hotplugging.
118 typedef int (rte_pci_remove_t)(struct rte_pci_device *);
121 * Driver-specific DMA mapping. After a successful call the device
122 * will be able to read/write from/to this segment.
125 * Pointer to the PCI device.
127 * Starting virtual address of memory to be mapped.
129 * Starting IOVA address of memory to be mapped.
131 * Length of memory segment being mapped.
134 * - Negative value and rte_errno is set otherwise.
136 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
137 uint64_t iova, size_t len);
140 * Driver-specific DMA un-mapping. After a successful call the device
141 * will not be able to read/write from/to this segment.
144 * Pointer to the PCI device.
146 * Starting virtual address of memory to be unmapped.
148 * Starting IOVA address of memory to be unmapped.
150 * Length of memory segment being unmapped.
153 * - Negative value and rte_errno is set otherwise.
155 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
156 uint64_t iova, size_t len);
159 * A structure describing a PCI driver.
161 struct rte_pci_driver {
162 RTE_TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
163 struct rte_driver driver; /**< Inherit core driver. */
164 struct rte_pci_bus *bus; /**< PCI bus reference. */
165 rte_pci_probe_t *probe; /**< Device probe function. */
166 rte_pci_remove_t *remove; /**< Device remove function. */
167 pci_dma_map_t *dma_map; /**< device dma map function. */
168 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */
169 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
170 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */
174 * Structure describing the PCI bus
177 struct rte_bus bus; /**< Inherit the generic class */
178 struct rte_pci_device_list device_list; /**< List of PCI devices */
179 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
182 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
183 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
184 /** Device needs PCI BAR mapping with enabled write combining (wc) */
185 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
186 /** Device already probed can be probed again to check for new ports. */
187 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
188 /** Device driver supports link state interrupt */
189 #define RTE_PCI_DRV_INTR_LSC 0x0008
190 /** Device driver supports device removal interrupt */
191 #define RTE_PCI_DRV_INTR_RMV 0x0010
192 /** Device driver needs to keep mapped resources if unsupported dev detected */
193 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
194 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */
195 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040
198 * Map the PCI device resources in user space virtual memory address
200 * Note that driver should not call this function when flag
201 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
205 * A pointer to a rte_pci_device structure describing the device
209 * 0 on success, negative on error and positive if no driver
210 * is found for the device.
212 int rte_pci_map_device(struct rte_pci_device *dev);
218 * A pointer to a rte_pci_device structure describing the device
221 void rte_pci_unmap_device(struct rte_pci_device *dev);
224 * Dump the content of the PCI bus.
227 * A pointer to a file for output
229 void rte_pci_dump(FILE *f);
232 * Find device's extended PCI capability.
235 * A pointer to rte_pci_device structure.
238 * Extended capability to be found, which can be any from
239 * RTE_PCI_EXT_CAP_ID_*, defined in librte_pci.
242 * > 0: The offset of the next matching extended capability structure
243 * within the device's PCI configuration space.
244 * < 0: An error in PCI config space read.
245 * = 0: Device does not support it.
248 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap);
251 * Enables/Disables Bus Master for device's PCI command register.
254 * A pointer to rte_pci_device structure.
256 * Enable or disable Bus Master.
259 * 0 on success, -1 on error in PCI config space read/write.
262 int rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable);
265 * Register a PCI driver.
268 * A pointer to a rte_pci_driver structure describing the driver
271 void rte_pci_register(struct rte_pci_driver *driver);
273 /** Helper for PCI device registration from driver (eth, crypto) instance */
274 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
275 RTE_INIT(pciinitfn_ ##nm) \
277 (pci_drv).driver.name = RTE_STR(nm);\
278 rte_pci_register(&pci_drv); \
280 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
283 * Unregister a PCI driver.
286 * A pointer to a rte_pci_driver structure describing the driver
287 * to be unregistered.
289 void rte_pci_unregister(struct rte_pci_driver *driver);
292 * Read PCI config space.
295 * A pointer to a rte_pci_device structure describing the device
298 * A data buffer where the bytes should be read into
300 * The length of the data buffer.
302 * The offset into PCI config space
304 * Number of bytes read on success, negative on error.
306 int rte_pci_read_config(const struct rte_pci_device *device,
307 void *buf, size_t len, off_t offset);
310 * Write PCI config space.
313 * A pointer to a rte_pci_device structure describing the device
316 * A data buffer containing the bytes should be written
318 * The length of the data buffer.
320 * The offset into PCI config space
322 int rte_pci_write_config(const struct rte_pci_device *device,
323 const void *buf, size_t len, off_t offset);
326 * A structure used to access io resources for a pci device.
327 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
330 struct rte_pci_ioport {
331 struct rte_pci_device *dev;
333 uint64_t len; /* only filled for memory mapped ports */
337 * Initialize a rte_pci_ioport object for a pci device io resource.
339 * This object is then used to gain access to those io resources (see below).
342 * A pointer to a rte_pci_device structure describing the device
345 * Index of the io pci resource we want to access.
347 * The rte_pci_ioport object to be initialized.
349 * 0 on success, negative on error.
351 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
352 struct rte_pci_ioport *p);
355 * Release any resources used in a rte_pci_ioport object.
358 * The rte_pci_ioport object to be uninitialized.
360 * 0 on success, negative on error.
362 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
365 * Read from a io pci resource.
368 * The rte_pci_ioport object from which we want to read.
370 * A data buffer where the bytes should be read into
372 * The length of the data buffer.
374 * The offset into the pci io resource.
376 void rte_pci_ioport_read(struct rte_pci_ioport *p,
377 void *data, size_t len, off_t offset);
380 * Write to a io pci resource.
383 * The rte_pci_ioport object to which we want to write.
385 * A data buffer where the bytes should be read into
387 * The length of the data buffer.
389 * The offset into the pci io resource.
391 void rte_pci_ioport_write(struct rte_pci_ioport *p,
392 const void *data, size_t len, off_t offset);
398 #endif /* _RTE_BUS_PCI_H_ */