4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5 * Copyright 2013-2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _RTE_BUS_PCI_H_
36 #define _RTE_BUS_PCI_H_
41 * RTE PCI Bus Interface
52 #include <sys/queue.h>
56 #include <rte_debug.h>
57 #include <rte_interrupts.h>
62 /** Pathname of PCI devices directory. */
63 const char *rte_pci_get_sysfs_path(void);
65 /* Forward declarations */
66 struct rte_pci_device;
67 struct rte_pci_driver;
69 /** List of PCI devices */
70 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
71 /** List of PCI drivers */
72 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
74 /* PCI Bus iterators */
75 #define FOREACH_DEVICE_ON_PCIBUS(p) \
76 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
78 #define FOREACH_DRIVER_ON_PCIBUS(p) \
79 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
84 * A structure describing a PCI device.
86 struct rte_pci_device {
87 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
88 struct rte_device device; /**< Inherit core device */
89 struct rte_pci_addr addr; /**< PCI location. */
90 struct rte_pci_id id; /**< PCI ID. */
91 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
92 /**< PCI Memory Resource */
93 struct rte_intr_handle intr_handle; /**< Interrupt handle */
94 struct rte_pci_driver *driver; /**< Associated driver */
95 uint16_t max_vfs; /**< sriov enable if not zero */
96 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
97 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */
102 * Helper macro for drivers that need to convert to struct rte_pci_device.
104 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
106 #define RTE_DEV_TO_PCI_CONST(ptr) \
107 container_of(ptr, const struct rte_pci_device, device)
109 #define RTE_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
111 /** Any PCI device identifier (vendor, device, ...) */
112 #define PCI_ANY_ID (0xffff)
113 #define RTE_CLASS_ANY_ID (0xffffff)
116 /** C++ macro used to help building up tables of device IDs */
117 #define RTE_PCI_DEVICE(vend, dev) \
124 /** Macro used to help building up tables of device IDs */
125 #define RTE_PCI_DEVICE(vend, dev) \
126 .class_id = RTE_CLASS_ANY_ID, \
127 .vendor_id = (vend), \
128 .device_id = (dev), \
129 .subsystem_vendor_id = PCI_ANY_ID, \
130 .subsystem_device_id = PCI_ANY_ID
134 * Initialisation function for the driver called during PCI probing.
136 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
139 * Uninitialisation function for the driver called during hotplugging.
141 typedef int (pci_remove_t)(struct rte_pci_device *);
144 * A structure describing a PCI driver.
146 struct rte_pci_driver {
147 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
148 struct rte_driver driver; /**< Inherit core driver. */
149 struct rte_pci_bus *bus; /**< PCI bus reference. */
150 pci_probe_t *probe; /**< Device Probe function. */
151 pci_remove_t *remove; /**< Device Remove function. */
152 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
153 uint32_t drv_flags; /**< Flags contolling handling of device. */
157 * Structure describing the PCI bus
160 struct rte_bus bus; /**< Inherit the generic class */
161 struct rte_pci_device_list device_list; /**< List of PCI devices */
162 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
165 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
166 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
167 /** Device driver supports link state interrupt */
168 #define RTE_PCI_DRV_INTR_LSC 0x0008
169 /** Device driver supports device removal interrupt */
170 #define RTE_PCI_DRV_INTR_RMV 0x0010
171 /** Device driver needs to keep mapped resources if unsupported dev detected */
172 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
173 /** Device driver supports IOVA as VA */
174 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
177 * Map the PCI device resources in user space virtual memory address
179 * Note that driver should not call this function when flag
180 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
184 * A pointer to a rte_pci_device structure describing the device
188 * 0 on success, negative on error and positive if no driver
189 * is found for the device.
191 int rte_pci_map_device(struct rte_pci_device *dev);
197 * A pointer to a rte_pci_device structure describing the device
200 void rte_pci_unmap_device(struct rte_pci_device *dev);
203 * Dump the content of the PCI bus.
206 * A pointer to a file for output
208 void rte_pci_dump(FILE *f);
211 * Register a PCI driver.
214 * A pointer to a rte_pci_driver structure describing the driver
217 void rte_pci_register(struct rte_pci_driver *driver);
219 /** Helper for PCI device registration from driver (eth, crypto) instance */
220 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
221 RTE_INIT(pciinitfn_ ##nm); \
222 static void pciinitfn_ ##nm(void) \
224 (pci_drv).driver.name = RTE_STR(nm);\
225 rte_pci_register(&pci_drv); \
227 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
230 * Unregister a PCI driver.
233 * A pointer to a rte_pci_driver structure describing the driver
234 * to be unregistered.
236 void rte_pci_unregister(struct rte_pci_driver *driver);
239 * Read PCI config space.
242 * A pointer to a rte_pci_device structure describing the device
245 * A data buffer where the bytes should be read into
247 * The length of the data buffer.
249 * The offset into PCI config space
251 int rte_pci_read_config(const struct rte_pci_device *device,
252 void *buf, size_t len, off_t offset);
255 * Write PCI config space.
258 * A pointer to a rte_pci_device structure describing the device
261 * A data buffer containing the bytes should be written
263 * The length of the data buffer.
265 * The offset into PCI config space
267 int rte_pci_write_config(const struct rte_pci_device *device,
268 const void *buf, size_t len, off_t offset);
271 * A structure used to access io resources for a pci device.
272 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
275 struct rte_pci_ioport {
276 struct rte_pci_device *dev;
278 uint64_t len; /* only filled for memory mapped ports */
282 * Initialize a rte_pci_ioport object for a pci device io resource.
284 * This object is then used to gain access to those io resources (see below).
287 * A pointer to a rte_pci_device structure describing the device
290 * Index of the io pci resource we want to access.
292 * The rte_pci_ioport object to be initialized.
294 * 0 on success, negative on error.
296 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
297 struct rte_pci_ioport *p);
300 * Release any resources used in a rte_pci_ioport object.
303 * The rte_pci_ioport object to be uninitialized.
305 * 0 on success, negative on error.
307 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
310 * Read from a io pci resource.
313 * The rte_pci_ioport object from which we want to read.
315 * A data buffer where the bytes should be read into
317 * The length of the data buffer.
319 * The offset into the pci io resource.
321 void rte_pci_ioport_read(struct rte_pci_ioport *p,
322 void *data, size_t len, off_t offset);
325 * Write to a io pci resource.
328 * The rte_pci_ioport object to which we want to write.
330 * A data buffer where the bytes should be read into
332 * The length of the data buffer.
334 * The offset into the pci io resource.
336 void rte_pci_ioport_write(struct rte_pci_ioport *p,
337 const void *data, size_t len, off_t offset);
343 #endif /* _RTE_BUS_PCI_H_ */