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_ETH_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
108 /** Any PCI device identifier (vendor, device, ...) */
109 #define PCI_ANY_ID (0xffff)
110 #define RTE_CLASS_ANY_ID (0xffffff)
113 /** C++ macro used to help building up tables of device IDs */
114 #define RTE_PCI_DEVICE(vend, dev) \
121 /** Macro used to help building up tables of device IDs */
122 #define RTE_PCI_DEVICE(vend, dev) \
123 .class_id = RTE_CLASS_ANY_ID, \
124 .vendor_id = (vend), \
125 .device_id = (dev), \
126 .subsystem_vendor_id = PCI_ANY_ID, \
127 .subsystem_device_id = PCI_ANY_ID
131 * Initialisation function for the driver called during PCI probing.
133 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
136 * Uninitialisation function for the driver called during hotplugging.
138 typedef int (pci_remove_t)(struct rte_pci_device *);
141 * A structure describing a PCI driver.
143 struct rte_pci_driver {
144 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
145 struct rte_driver driver; /**< Inherit core driver. */
146 struct rte_pci_bus *bus; /**< PCI bus reference. */
147 pci_probe_t *probe; /**< Device Probe function. */
148 pci_remove_t *remove; /**< Device Remove function. */
149 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
150 uint32_t drv_flags; /**< Flags contolling handling of device. */
154 * Structure describing the PCI bus
157 struct rte_bus bus; /**< Inherit the generic class */
158 struct rte_pci_device_list device_list; /**< List of PCI devices */
159 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
162 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
163 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
164 /** Device driver supports link state interrupt */
165 #define RTE_PCI_DRV_INTR_LSC 0x0008
166 /** Device driver supports device removal interrupt */
167 #define RTE_PCI_DRV_INTR_RMV 0x0010
168 /** Device driver needs to keep mapped resources if unsupported dev detected */
169 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
170 /** Device driver supports IOVA as VA */
171 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
174 * Map the PCI device resources in user space virtual memory address
176 * Note that driver should not call this function when flag
177 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
181 * A pointer to a rte_pci_device structure describing the device
185 * 0 on success, negative on error and positive if no driver
186 * is found for the device.
188 int rte_pci_map_device(struct rte_pci_device *dev);
194 * A pointer to a rte_pci_device structure describing the device
197 void rte_pci_unmap_device(struct rte_pci_device *dev);
200 * Dump the content of the PCI bus.
203 * A pointer to a file for output
205 void rte_pci_dump(FILE *f);
208 * Register a PCI driver.
211 * A pointer to a rte_pci_driver structure describing the driver
214 void rte_pci_register(struct rte_pci_driver *driver);
216 /** Helper for PCI device registration from driver (eth, crypto) instance */
217 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
218 RTE_INIT(pciinitfn_ ##nm); \
219 static void pciinitfn_ ##nm(void) \
221 (pci_drv).driver.name = RTE_STR(nm);\
222 rte_pci_register(&pci_drv); \
224 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
227 * Unregister a PCI driver.
230 * A pointer to a rte_pci_driver structure describing the driver
231 * to be unregistered.
233 void rte_pci_unregister(struct rte_pci_driver *driver);
236 * Read PCI config space.
239 * A pointer to a rte_pci_device structure describing the device
242 * A data buffer where the bytes should be read into
244 * The length of the data buffer.
246 * The offset into PCI config space
248 int rte_pci_read_config(const struct rte_pci_device *device,
249 void *buf, size_t len, off_t offset);
252 * Write PCI config space.
255 * A pointer to a rte_pci_device structure describing the device
258 * A data buffer containing the bytes should be written
260 * The length of the data buffer.
262 * The offset into PCI config space
264 int rte_pci_write_config(const struct rte_pci_device *device,
265 const void *buf, size_t len, off_t offset);
268 * A structure used to access io resources for a pci device.
269 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
272 struct rte_pci_ioport {
273 struct rte_pci_device *dev;
275 uint64_t len; /* only filled for memory mapped ports */
279 * Initialize a rte_pci_ioport object for a pci device io resource.
281 * This object is then used to gain access to those io resources (see below).
284 * A pointer to a rte_pci_device structure describing the device
287 * Index of the io pci resource we want to access.
289 * The rte_pci_ioport object to be initialized.
291 * 0 on success, negative on error.
293 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
294 struct rte_pci_ioport *p);
297 * Release any resources used in a rte_pci_ioport object.
300 * The rte_pci_ioport object to be uninitialized.
302 * 0 on success, negative on error.
304 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
307 * Read from a io pci resource.
310 * The rte_pci_ioport object from which we want to read.
312 * A data buffer where the bytes should be read into
314 * The length of the data buffer.
316 * The offset into the pci io resource.
318 void rte_pci_ioport_read(struct rte_pci_ioport *p,
319 void *data, size_t len, off_t offset);
322 * Write to a io pci resource.
325 * The rte_pci_ioport object to which we want to write.
327 * A data buffer where the bytes should be read into
329 * The length of the data buffer.
331 * The offset into the pci io resource.
333 void rte_pci_ioport_write(struct rte_pci_ioport *p,
334 const void *data, size_t len, off_t offset);
340 #endif /* _RTE_BUS_PCI_H_ */