4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright 2013-2014 6WIND S.A.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name of 6WIND S.A. nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 #include <sys/queue.h>
85 #include <rte_interrupts.h>
87 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
88 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
90 extern struct pci_driver_list pci_driver_list; /**< Global list of PCI drivers. */
91 extern struct pci_device_list pci_device_list; /**< Global list of PCI devices. */
93 /** Pathname of PCI devices directory. */
94 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
96 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
97 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
99 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
100 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
102 /** Nb. of values in PCI device identifier format string. */
103 #define PCI_FMT_NVAL 4
105 /** Nb. of values in PCI resource format. */
106 #define PCI_RESOURCE_FMT_NVAL 3
108 /** IO resource type: memory address space */
109 #define IORESOURCE_MEM 0x00000200
112 * A structure describing a PCI resource.
114 struct rte_pci_resource {
115 uint64_t phys_addr; /**< Physical address, 0 if no resource. */
116 uint64_t len; /**< Length of the resource. */
117 void *addr; /**< Virtual address, NULL when not mapped. */
120 /** Maximum number of PCI resources. */
121 #define PCI_MAX_RESOURCE 6
124 * A structure describing an ID for a PCI driver. Each driver provides a
125 * table of these IDs for each device that it supports.
128 uint16_t vendor_id; /**< Vendor ID or PCI_ANY_ID. */
129 uint16_t device_id; /**< Device ID or PCI_ANY_ID. */
130 uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
131 uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
135 * A structure describing the location of a PCI device.
137 struct rte_pci_addr {
138 uint16_t domain; /**< Device domain */
139 uint8_t bus; /**< Device bus */
140 uint8_t devid; /**< Device ID */
141 uint8_t function; /**< Device function. */
146 enum rte_kernel_driver {
147 RTE_KDRV_UNKNOWN = 0,
150 RTE_KDRV_UIO_GENERIC,
155 * A structure describing a PCI device.
157 struct rte_pci_device {
158 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
159 struct rte_pci_addr addr; /**< PCI location. */
160 struct rte_pci_id id; /**< PCI ID. */
161 struct rte_pci_resource mem_resource[PCI_MAX_RESOURCE]; /**< PCI Memory Resource */
162 struct rte_intr_handle intr_handle; /**< Interrupt handle */
163 struct rte_pci_driver *driver; /**< Associated driver */
164 uint16_t max_vfs; /**< sriov enable if not zero */
165 int numa_node; /**< NUMA node connection */
166 struct rte_devargs *devargs; /**< Device user arguments */
167 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
170 /** Any PCI device identifier (vendor, device, ...) */
171 #define PCI_ANY_ID (0xffff)
174 /** C++ macro used to help building up tables of device IDs */
175 #define RTE_PCI_DEVICE(vend, dev) \
181 /** Macro used to help building up tables of device IDs */
182 #define RTE_PCI_DEVICE(vend, dev) \
183 .vendor_id = (vend), \
184 .device_id = (dev), \
185 .subsystem_vendor_id = PCI_ANY_ID, \
186 .subsystem_device_id = PCI_ANY_ID
189 struct rte_pci_driver;
192 * Initialisation function for the driver called during PCI probing.
194 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
197 * Uninitialisation function for the driver called during hotplugging.
199 typedef int (pci_devuninit_t)(struct rte_pci_device *);
202 * A structure describing a PCI driver.
204 struct rte_pci_driver {
205 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
206 const char *name; /**< Driver name. */
207 pci_devinit_t *devinit; /**< Device init. function. */
208 pci_devuninit_t *devuninit; /**< Device uninit function. */
209 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
210 uint32_t drv_flags; /**< Flags contolling handling of device. */
213 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
214 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
215 /** Device driver must be registered several times until failure - deprecated */
216 #pragma GCC poison RTE_PCI_DRV_MULTIPLE
217 /** Device needs to be unbound even if no module is provided */
218 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
219 /** Device driver supports link state interrupt */
220 #define RTE_PCI_DRV_INTR_LSC 0x0008
221 /** Device driver supports detaching capability */
222 #define RTE_PCI_DRV_DETACHABLE 0x0010
225 * A structure describing a PCI mapping.
236 * A structure describing a mapped PCI resource.
237 * For multi-process we need to reproduce all PCI mappings in secondary
238 * processes, so save them in a tailq.
240 struct mapped_pci_resource {
241 TAILQ_ENTRY(mapped_pci_resource) next;
243 struct rte_pci_addr pci_addr;
246 struct pci_map maps[PCI_MAX_RESOURCE];
249 /** mapped pci device list */
250 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
252 /**< Internal use only - Macro used by pci addr parsing functions **/
253 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
258 val = strtoul((in), &end, 16); \
259 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
261 (fd) = (typeof (fd))val; \
266 * Utility function to produce a PCI Bus-Device-Function value
267 * given a string representation. Assumes that the BDF is provided without
268 * a domain prefix (i.e. domain returned is always 0)
271 * The input string to be parsed. Should have the format XX:XX.X
273 * The PCI Bus-Device-Function address to be returned. Domain will always be
276 * 0 on success, negative on error.
279 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
281 dev_addr->domain = 0;
282 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
283 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
284 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
289 * Utility function to produce a PCI Bus-Device-Function value
290 * given a string representation. Assumes that the BDF is provided including
294 * The input string to be parsed. Should have the format XXXX:XX:XX.X
296 * The PCI Bus-Device-Function address to be returned
298 * 0 on success, negative on error.
301 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
303 GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
304 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
305 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
306 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
309 #undef GET_PCIADDR_FIELD
311 /* Compare two PCI device addresses. */
313 * Utility function to compare two PCI device addresses.
316 * The PCI Bus-Device-Function address to compare
318 * The PCI Bus-Device-Function address to compare
320 * 0 on equal PCI address.
321 * Positive on addr is greater than addr2.
322 * Negative on addr is less than addr2, or error.
325 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
326 const struct rte_pci_addr *addr2)
328 uint64_t dev_addr, dev_addr2;
330 if ((addr == NULL) || (addr2 == NULL))
333 dev_addr = (addr->domain << 24) | (addr->bus << 16) |
334 (addr->devid << 8) | addr->function;
335 dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
336 (addr2->devid << 8) | addr2->function;
338 if (dev_addr > dev_addr2)
340 else if (dev_addr < dev_addr2)
347 * Scan the content of the PCI bus, and the devices in the devices
351 * 0 on success, negative on error
353 int rte_eal_pci_scan(void);
356 * Probe the PCI bus for registered drivers.
358 * Scan the content of the PCI bus, and call the probe() function for
359 * all registered drivers that have a matching entry in its id_table
360 * for discovered devices.
364 * - Negative on error.
366 int rte_eal_pci_probe(void);
370 * Map a particular resource from a file.
372 * @param requested_addr
373 * The starting address for the new mapping range.
375 * The file descriptor.
377 * The offset for the mapping range.
379 * The size for the mapping range.
380 * @param additional_flags
381 * The additional flags for the mapping range.
383 * - On success, the function returns a pointer to the mapped area.
384 * - On error, the value MAP_FAILED is returned.
386 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
387 size_t size, int additional_flags);
391 * Unmap a particular resource.
393 * @param requested_addr
394 * The address for the unmapping range.
396 * The size for the unmapping range.
398 void pci_unmap_resource(void *requested_addr, size_t size);
401 * Probe the single PCI device.
403 * Scan the content of the PCI bus, and find the pci device specified by pci
404 * address, then call the probe() function for registered driver that has a
405 * matching entry in its id_table for discovered device.
408 * The PCI Bus-Device-Function address to probe.
411 * - Negative on error.
413 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
416 * Close the single PCI device.
418 * Scan the content of the PCI bus, and find the pci device specified by pci
419 * address, then call the devuninit() function for registered driver that has a
420 * matching entry in its id_table for discovered device.
423 * The PCI Bus-Device-Function address to close.
426 * - Negative on error.
428 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
429 int __attribute__ ((deprecated))
430 rte_eal_pci_close_one(const struct rte_pci_addr *addr);
433 * Dump the content of the PCI bus.
436 * A pointer to a file for output
438 void rte_eal_pci_dump(FILE *f);
441 * Register a PCI driver.
444 * A pointer to a rte_pci_driver structure describing the driver
447 void rte_eal_pci_register(struct rte_pci_driver *driver);
450 * Unregister a PCI driver.
453 * A pointer to a rte_pci_driver structure describing the driver
454 * to be unregistered.
456 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
462 #endif /* _RTE_PCI_H_ */