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
22 #include <sys/queue.h>
26 #include <rte_debug.h>
27 #include <rte_interrupts.h>
32 /** Pathname of PCI devices directory. */
33 const char *rte_pci_get_sysfs_path(void);
35 /* Forward declarations */
36 struct rte_pci_device;
37 struct rte_pci_driver;
39 /** List of PCI devices */
40 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
41 /** List of PCI drivers */
42 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
44 /* PCI Bus iterators */
45 #define FOREACH_DEVICE_ON_PCIBUS(p) \
46 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
48 #define FOREACH_DRIVER_ON_PCIBUS(p) \
49 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
53 enum rte_pci_kernel_driver {
54 RTE_PCI_KDRV_UNKNOWN = 0, /* may be misc UIO or bifurcated driver */
55 RTE_PCI_KDRV_IGB_UIO, /* igb_uio for Linux */
56 RTE_PCI_KDRV_VFIO, /* VFIO for Linux */
57 RTE_PCI_KDRV_UIO_GENERIC, /* uio_pci_generic for Linux */
58 RTE_PCI_KDRV_NIC_UIO, /* nic_uio for FreeBSD */
59 RTE_PCI_KDRV_NONE, /* no attached driver */
60 RTE_PCI_KDRV_NET_UIO, /* NetUIO for Windows */
64 * A structure describing a PCI device.
66 struct rte_pci_device {
67 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
68 struct rte_device device; /**< Inherit core device */
69 struct rte_pci_addr addr; /**< PCI location. */
70 struct rte_pci_id id; /**< PCI ID. */
71 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
72 /**< PCI Memory Resource */
73 struct rte_intr_handle intr_handle; /**< Interrupt handle */
74 struct rte_pci_driver *driver; /**< PCI driver used in probing */
75 uint16_t max_vfs; /**< sriov enable if not zero */
76 enum rte_pci_kernel_driver kdrv; /**< Kernel driver passthrough */
77 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */
78 struct rte_intr_handle vfio_req_intr_handle;
79 /**< Handler of VFIO request interrupt */
84 * Helper macro for drivers that need to convert to struct rte_pci_device.
86 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
88 #define RTE_DEV_TO_PCI_CONST(ptr) \
89 container_of(ptr, const struct rte_pci_device, device)
91 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
94 /** C++ macro used to help building up tables of device IDs */
95 #define RTE_PCI_DEVICE(vend, dev) \
102 /** Macro used to help building up tables of device IDs */
103 #define RTE_PCI_DEVICE(vend, dev) \
104 .class_id = RTE_CLASS_ANY_ID, \
105 .vendor_id = (vend), \
106 .device_id = (dev), \
107 .subsystem_vendor_id = RTE_PCI_ANY_ID, \
108 .subsystem_device_id = RTE_PCI_ANY_ID
112 * Initialisation function for the driver called during PCI probing.
114 typedef int (rte_pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
117 * Uninitialisation function for the driver called during hotplugging.
119 typedef int (rte_pci_remove_t)(struct rte_pci_device *);
122 * Driver-specific DMA mapping. After a successful call the device
123 * will be able to read/write from/to this segment.
126 * Pointer to the PCI device.
128 * Starting virtual address of memory to be mapped.
130 * Starting IOVA address of memory to be mapped.
132 * Length of memory segment being mapped.
135 * - Negative value and rte_errno is set otherwise.
137 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
138 uint64_t iova, size_t len);
141 * Driver-specific DMA un-mapping. After a successful call the device
142 * will not be able to read/write from/to this segment.
145 * Pointer to the PCI device.
147 * Starting virtual address of memory to be unmapped.
149 * Starting IOVA address of memory to be unmapped.
151 * Length of memory segment being unmapped.
154 * - Negative value and rte_errno is set otherwise.
156 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
157 uint64_t iova, size_t len);
160 * A structure describing a PCI driver.
162 struct rte_pci_driver {
163 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
164 struct rte_driver driver; /**< Inherit core driver. */
165 struct rte_pci_bus *bus; /**< PCI bus reference. */
166 rte_pci_probe_t *probe; /**< Device probe function. */
167 rte_pci_remove_t *remove; /**< Device remove function. */
168 pci_dma_map_t *dma_map; /**< device dma map function. */
169 pci_dma_unmap_t *dma_unmap; /**< device dma unmap function. */
170 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
171 uint32_t drv_flags; /**< Flags RTE_PCI_DRV_*. */
175 * Structure describing the PCI bus
178 struct rte_bus bus; /**< Inherit the generic class */
179 struct rte_pci_device_list device_list; /**< List of PCI devices */
180 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
183 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
184 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
185 /** Device needs PCI BAR mapping with enabled write combining (wc) */
186 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
187 /** Device already probed can be probed again to check for new ports. */
188 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
189 /** Device driver supports link state interrupt */
190 #define RTE_PCI_DRV_INTR_LSC 0x0008
191 /** Device driver supports device removal interrupt */
192 #define RTE_PCI_DRV_INTR_RMV 0x0010
193 /** Device driver needs to keep mapped resources if unsupported dev detected */
194 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
195 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */
196 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040
199 * Map the PCI device resources in user space virtual memory address
201 * Note that driver should not call this function when flag
202 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
206 * A pointer to a rte_pci_device structure describing the device
210 * 0 on success, negative on error and positive if no driver
211 * is found for the device.
213 int rte_pci_map_device(struct rte_pci_device *dev);
219 * A pointer to a rte_pci_device structure describing the device
222 void rte_pci_unmap_device(struct rte_pci_device *dev);
225 * Dump the content of the PCI bus.
228 * A pointer to a file for output
230 void rte_pci_dump(FILE *f);
233 * Find device's extended PCI capability.
236 * A pointer to rte_pci_device structure.
239 * Extended capability to be found, which can be any from
240 * RTE_PCI_EXT_CAP_ID_*, defined in librte_pci.
243 * > 0: The offset of the next matching extended capability structure
244 * within the device's PCI configuration space.
245 * < 0: An error in PCI config space read.
246 * = 0: Device does not support it.
249 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap);
252 * Enables/Disables Bus Master for device's PCI command register.
255 * A pointer to rte_pci_device structure.
257 * Enable or disable Bus Master.
260 * 0 on success, -1 on error in PCI config space read/write.
263 int rte_pci_set_bus_master(struct rte_pci_device *dev, bool enable);
266 * Register a PCI driver.
269 * A pointer to a rte_pci_driver structure describing the driver
272 void rte_pci_register(struct rte_pci_driver *driver);
274 /** Helper for PCI device registration from driver (eth, crypto) instance */
275 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
276 RTE_INIT(pciinitfn_ ##nm) \
278 (pci_drv).driver.name = RTE_STR(nm);\
279 rte_pci_register(&pci_drv); \
281 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
284 * Unregister a PCI driver.
287 * A pointer to a rte_pci_driver structure describing the driver
288 * to be unregistered.
290 void rte_pci_unregister(struct rte_pci_driver *driver);
293 * Read PCI config space.
296 * A pointer to a rte_pci_device structure describing the device
299 * A data buffer where the bytes should be read into
301 * The length of the data buffer.
303 * The offset into PCI config space
305 * Number of bytes read on success, negative on error.
307 int rte_pci_read_config(const struct rte_pci_device *device,
308 void *buf, size_t len, off_t offset);
311 * Write PCI config space.
314 * A pointer to a rte_pci_device structure describing the device
317 * A data buffer containing the bytes should be written
319 * The length of the data buffer.
321 * The offset into PCI config space
323 int rte_pci_write_config(const struct rte_pci_device *device,
324 const void *buf, size_t len, off_t offset);
327 * A structure used to access io resources for a pci device.
328 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
331 struct rte_pci_ioport {
332 struct rte_pci_device *dev;
334 uint64_t len; /* only filled for memory mapped ports */
338 * Initialize a rte_pci_ioport object for a pci device io resource.
340 * This object is then used to gain access to those io resources (see below).
343 * A pointer to a rte_pci_device structure describing the device
346 * Index of the io pci resource we want to access.
348 * The rte_pci_ioport object to be initialized.
350 * 0 on success, negative on error.
352 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
353 struct rte_pci_ioport *p);
356 * Release any resources used in a rte_pci_ioport object.
359 * The rte_pci_ioport object to be uninitialized.
361 * 0 on success, negative on error.
363 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
366 * Read from a io pci resource.
369 * The rte_pci_ioport object from which we want to read.
371 * A data buffer where the bytes should be read into
373 * The length of the data buffer.
375 * The offset into the pci io resource.
377 void rte_pci_ioport_read(struct rte_pci_ioport *p,
378 void *data, size_t len, off_t offset);
381 * Write to a io pci resource.
384 * The rte_pci_ioport object to which we want to write.
386 * A data buffer where the bytes should be read into
388 * The length of the data buffer.
390 * The offset into the pci io resource.
392 void rte_pci_ioport_write(struct rte_pci_ioport *p,
393 const void *data, size_t len, off_t offset);
399 #endif /* _RTE_BUS_PCI_H_ */