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)
95 /** C++ macro used to help building up tables of device IDs */
96 #define RTE_PCI_DEVICE(vend, dev) \
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
113 * Initialisation function for the driver called during PCI probing.
115 typedef int (rte_pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
118 * Uninitialisation function for the driver called during hotplugging.
120 typedef int (rte_pci_remove_t)(struct rte_pci_device *);
123 * Driver-specific DMA mapping. After a successful call the device
124 * will be able to read/write from/to this segment.
127 * Pointer to the PCI device.
129 * Starting virtual address of memory to be mapped.
131 * Starting IOVA address of memory to be mapped.
133 * Length of memory segment being mapped.
136 * - Negative value and rte_errno is set otherwise.
138 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
139 uint64_t iova, size_t len);
142 * Driver-specific DMA un-mapping. After a successful call the device
143 * will not be able to read/write from/to this segment.
146 * Pointer to the PCI device.
148 * Starting virtual address of memory to be unmapped.
150 * Starting IOVA address of memory to be unmapped.
152 * Length of memory segment being unmapped.
155 * - Negative value and rte_errno is set otherwise.
157 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
158 uint64_t iova, size_t len);
161 * A structure describing a PCI driver.
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_*. */
176 * Structure describing the 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 */
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
200 * Map the PCI device resources in user space virtual memory address
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
207 * A pointer to a rte_pci_device structure describing the device
211 * 0 on success, negative on error and positive if no driver
212 * is found for the device.
214 int rte_pci_map_device(struct rte_pci_device *dev);
220 * A pointer to a rte_pci_device structure describing the device
223 void rte_pci_unmap_device(struct rte_pci_device *dev);
226 * Dump the content of the PCI bus.
229 * A pointer to a file for output
231 void rte_pci_dump(FILE *f);
234 * Find device's extended PCI capability.
237 * A pointer to rte_pci_device structure.
240 * Extended capability to be found, which can be any from
241 * RTE_PCI_EXT_CAP_ID_*, defined in librte_pci.
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.
250 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap);
253 * Enables/Disables Bus Master for device's PCI command register.
256 * A pointer to rte_pci_device structure.
258 * Enable or disable Bus Master.
261 * 0 on success, -1 on error in PCI config space read/write.
264 int rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable);
267 * Register a PCI driver.
270 * A pointer to a rte_pci_driver structure describing the driver
273 void rte_pci_register(struct rte_pci_driver *driver);
275 /** Helper for PCI device registration from driver (eth, crypto) instance */
276 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
277 RTE_INIT(pciinitfn_ ##nm) \
279 (pci_drv).driver.name = RTE_STR(nm);\
280 rte_pci_register(&pci_drv); \
282 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
285 * Unregister a PCI driver.
288 * A pointer to a rte_pci_driver structure describing the driver
289 * to be unregistered.
291 void rte_pci_unregister(struct rte_pci_driver *driver);
294 * Read PCI config space.
297 * A pointer to a rte_pci_device structure describing the device
300 * A data buffer where the bytes should be read into
302 * The length of the data buffer.
304 * The offset into PCI config space
306 * Number of bytes read on success, negative on error.
308 int rte_pci_read_config(const struct rte_pci_device *device,
309 void *buf, size_t len, off_t offset);
312 * Write PCI config space.
315 * A pointer to a rte_pci_device structure describing the device
318 * A data buffer containing the bytes should be written
320 * The length of the data buffer.
322 * The offset into PCI config space
324 int rte_pci_write_config(const struct rte_pci_device *device,
325 const void *buf, size_t len, off_t offset);
328 * A structure used to access io resources for a pci device.
329 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
332 struct rte_pci_ioport {
333 struct rte_pci_device *dev;
335 uint64_t len; /* only filled for memory mapped ports */
339 * Initialize a rte_pci_ioport object for a pci device io resource.
341 * This object is then used to gain access to those io resources (see below).
344 * A pointer to a rte_pci_device structure describing the device
347 * Index of the io pci resource we want to access.
349 * The rte_pci_ioport object to be initialized.
351 * 0 on success, negative on error.
353 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
354 struct rte_pci_ioport *p);
357 * Release any resources used in a rte_pci_ioport object.
360 * The rte_pci_ioport object to be uninitialized.
362 * 0 on success, negative on error.
364 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
367 * Read from a io pci resource.
370 * The rte_pci_ioport object from which we want to read.
372 * A data buffer where the bytes should be read into
374 * The length of the data buffer.
376 * The offset into the pci io resource.
378 void rte_pci_ioport_read(struct rte_pci_ioport *p,
379 void *data, size_t len, off_t offset);
382 * Write to a io pci resource.
385 * The rte_pci_ioport object to which we want to write.
387 * A data buffer where the bytes should be read into
389 * The length of the data buffer.
391 * The offset into the pci io resource.
393 void rte_pci_ioport_write(struct rte_pci_ioport *p,
394 const void *data, size_t len, off_t offset);
400 #endif /* _RTE_BUS_PCI_H_ */