4 * Copyright(c) 2010-2015 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_debug.h>
86 #include <rte_interrupts.h>
90 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
91 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
93 extern struct pci_driver_list pci_driver_list; /**< Global list of PCI drivers. */
94 extern struct pci_device_list pci_device_list; /**< Global list of PCI devices. */
96 /** Pathname of PCI devices directory. */
97 const char *pci_get_sysfs_path(void);
99 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
100 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
101 #define PCI_PRI_STR_SIZE sizeof("XXXX:XX:XX.X")
103 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
104 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
106 /** Nb. of values in PCI device identifier format string. */
107 #define PCI_FMT_NVAL 4
109 /** Nb. of values in PCI resource format. */
110 #define PCI_RESOURCE_FMT_NVAL 3
112 /** Maximum number of PCI resources. */
113 #define PCI_MAX_RESOURCE 6
115 /** Name of PCI Bus */
116 #define PCI_BUS_NAME "PCI"
118 /* Forward declarations */
119 struct rte_pci_device;
120 struct rte_pci_driver;
122 /** List of PCI devices */
123 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
124 /** List of PCI drivers */
125 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
127 /* PCI Bus iterators */
128 #define FOREACH_DEVICE_ON_PCIBUS(p) \
129 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
131 #define FOREACH_DRIVER_ON_PCIBUS(p) \
132 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
135 * A structure describing an ID for a PCI driver. Each driver provides a
136 * table of these IDs for each device that it supports.
139 uint32_t class_id; /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
140 uint16_t vendor_id; /**< Vendor ID or PCI_ANY_ID. */
141 uint16_t device_id; /**< Device ID or PCI_ANY_ID. */
142 uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
143 uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
147 * A structure describing the location of a PCI device.
149 struct rte_pci_addr {
150 uint16_t domain; /**< Device domain */
151 uint8_t bus; /**< Device bus */
152 uint8_t devid; /**< Device ID */
153 uint8_t function; /**< Device function. */
158 enum rte_kernel_driver {
159 RTE_KDRV_UNKNOWN = 0,
162 RTE_KDRV_UIO_GENERIC,
168 * A structure describing a PCI device.
170 struct rte_pci_device {
171 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
172 struct rte_device device; /**< Inherit core device */
173 struct rte_pci_addr addr; /**< PCI location. */
174 struct rte_pci_id id; /**< PCI ID. */
175 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
176 /**< PCI Memory Resource */
177 struct rte_intr_handle intr_handle; /**< Interrupt handle */
178 struct rte_pci_driver *driver; /**< Associated driver */
179 uint16_t max_vfs; /**< sriov enable if not zero */
180 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
185 * Helper macro for drivers that need to convert to struct rte_pci_device.
187 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
189 /** Any PCI device identifier (vendor, device, ...) */
190 #define PCI_ANY_ID (0xffff)
191 #define RTE_CLASS_ANY_ID (0xffffff)
194 /** C++ macro used to help building up tables of device IDs */
195 #define RTE_PCI_DEVICE(vend, dev) \
202 /** Macro used to help building up tables of device IDs */
203 #define RTE_PCI_DEVICE(vend, dev) \
204 .class_id = RTE_CLASS_ANY_ID, \
205 .vendor_id = (vend), \
206 .device_id = (dev), \
207 .subsystem_vendor_id = PCI_ANY_ID, \
208 .subsystem_device_id = PCI_ANY_ID
211 struct rte_pci_driver;
214 * Initialisation function for the driver called during PCI probing.
216 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
219 * Uninitialisation function for the driver called during hotplugging.
221 typedef int (pci_remove_t)(struct rte_pci_device *);
224 * A structure describing a PCI driver.
226 struct rte_pci_driver {
227 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
228 struct rte_driver driver; /**< Inherit core driver. */
229 struct rte_pci_bus *bus; /**< PCI bus reference. */
230 pci_probe_t *probe; /**< Device Probe function. */
231 pci_remove_t *remove; /**< Device Remove function. */
232 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
233 uint32_t drv_flags; /**< Flags contolling handling of device. */
237 * Structure describing the PCI bus
240 struct rte_bus bus; /**< Inherit the generic class */
241 struct rte_pci_device_list device_list; /**< List of PCI devices */
242 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
245 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
246 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
247 /** Device driver supports link state interrupt */
248 #define RTE_PCI_DRV_INTR_LSC 0x0008
251 * A structure describing a PCI mapping.
262 * A structure describing a mapped PCI resource.
263 * For multi-process we need to reproduce all PCI mappings in secondary
264 * processes, so save them in a tailq.
266 struct mapped_pci_resource {
267 TAILQ_ENTRY(mapped_pci_resource) next;
269 struct rte_pci_addr pci_addr;
272 struct pci_map maps[PCI_MAX_RESOURCE];
275 /** mapped pci device list */
276 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
278 /**< Internal use only - Macro used by pci addr parsing functions **/
279 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
284 val = strtoul((in), &end, 16); \
285 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
287 (fd) = (typeof (fd))val; \
292 * Utility function to produce a PCI Bus-Device-Function value
293 * given a string representation. Assumes that the BDF is provided without
294 * a domain prefix (i.e. domain returned is always 0)
297 * The input string to be parsed. Should have the format XX:XX.X
299 * The PCI Bus-Device-Function address to be returned. Domain will always be
302 * 0 on success, negative on error.
305 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
307 dev_addr->domain = 0;
308 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
309 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
310 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
315 * Utility function to produce a PCI Bus-Device-Function value
316 * given a string representation. Assumes that the BDF is provided including
320 * The input string to be parsed. Should have the format XXXX:XX:XX.X
322 * The PCI Bus-Device-Function address to be returned
324 * 0 on success, negative on error.
327 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
329 GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
330 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
331 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
332 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
335 #undef GET_PCIADDR_FIELD
338 * Utility function to write a pci device name, this device name can later be
339 * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
343 * The PCI Bus-Device-Function address
345 * The output buffer string
347 * The output buffer size
350 rte_eal_pci_device_name(const struct rte_pci_addr *addr,
351 char *output, size_t size)
353 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
354 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
355 addr->domain, addr->bus,
356 addr->devid, addr->function) >= 0);
359 /* Compare two PCI device addresses. */
361 * Utility function to compare two PCI device addresses.
364 * The PCI Bus-Device-Function address to compare
366 * The PCI Bus-Device-Function address to compare
368 * 0 on equal PCI address.
369 * Positive on addr is greater than addr2.
370 * Negative on addr is less than addr2, or error.
373 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
374 const struct rte_pci_addr *addr2)
376 uint64_t dev_addr, dev_addr2;
378 if ((addr == NULL) || (addr2 == NULL))
381 dev_addr = (addr->domain << 24) | (addr->bus << 16) |
382 (addr->devid << 8) | addr->function;
383 dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
384 (addr2->devid << 8) | addr2->function;
386 if (dev_addr > dev_addr2)
388 else if (dev_addr < dev_addr2)
395 * Scan the content of the PCI bus, and the devices in the devices
399 * 0 on success, negative on error
401 int rte_eal_pci_scan(void);
404 * Probe the PCI bus for registered drivers.
406 * Scan the content of the PCI bus, and call the probe() function for
407 * all registered drivers that have a matching entry in its id_table
408 * for discovered devices.
412 * - Negative on error.
414 int rte_eal_pci_probe(void);
417 * Map the PCI device resources in user space virtual memory address
419 * Note that driver should not call this function when flag
420 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
424 * A pointer to a rte_pci_device structure describing the device
428 * 0 on success, negative on error and positive if no driver
429 * is found for the device.
431 int rte_eal_pci_map_device(struct rte_pci_device *dev);
437 * A pointer to a rte_pci_device structure describing the device
440 void rte_eal_pci_unmap_device(struct rte_pci_device *dev);
444 * Map a particular resource from a file.
446 * @param requested_addr
447 * The starting address for the new mapping range.
449 * The file descriptor.
451 * The offset for the mapping range.
453 * The size for the mapping range.
454 * @param additional_flags
455 * The additional flags for the mapping range.
457 * - On success, the function returns a pointer to the mapped area.
458 * - On error, the value MAP_FAILED is returned.
460 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
461 size_t size, int additional_flags);
465 * Unmap a particular resource.
467 * @param requested_addr
468 * The address for the unmapping range.
470 * The size for the unmapping range.
472 void pci_unmap_resource(void *requested_addr, size_t size);
475 * Probe the single PCI device.
477 * Scan the content of the PCI bus, and find the pci device specified by pci
478 * address, then call the probe() function for registered driver that has a
479 * matching entry in its id_table for discovered device.
482 * The PCI Bus-Device-Function address to probe.
485 * - Negative on error.
487 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
490 * Close the single PCI device.
492 * Scan the content of the PCI bus, and find the pci device specified by pci
493 * address, then call the remove() function for registered driver that has a
494 * matching entry in its id_table for discovered device.
497 * The PCI Bus-Device-Function address to close.
500 * - Negative on error.
502 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
505 * Dump the content of the PCI bus.
508 * A pointer to a file for output
510 void rte_eal_pci_dump(FILE *f);
513 * Register a PCI driver.
516 * A pointer to a rte_pci_driver structure describing the driver
519 void rte_eal_pci_register(struct rte_pci_driver *driver);
521 /** Helper for PCI device registration from driver (eth, crypto) instance */
522 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
523 RTE_INIT(pciinitfn_ ##nm); \
524 static void pciinitfn_ ##nm(void) \
526 (pci_drv).driver.name = RTE_STR(nm);\
527 rte_eal_pci_register(&pci_drv); \
529 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
532 * Unregister a PCI driver.
535 * A pointer to a rte_pci_driver structure describing the driver
536 * to be unregistered.
538 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
541 * Read PCI config space.
544 * A pointer to a rte_pci_device structure describing the device
547 * A data buffer where the bytes should be read into
549 * The length of the data buffer.
551 * The offset into PCI config space
553 int rte_eal_pci_read_config(const struct rte_pci_device *device,
554 void *buf, size_t len, off_t offset);
557 * Write PCI config space.
560 * A pointer to a rte_pci_device structure describing the device
563 * A data buffer containing the bytes should be written
565 * The length of the data buffer.
567 * The offset into PCI config space
569 int rte_eal_pci_write_config(const struct rte_pci_device *device,
570 const void *buf, size_t len, off_t offset);
573 * A structure used to access io resources for a pci device.
574 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
577 struct rte_pci_ioport {
578 struct rte_pci_device *dev;
580 uint64_t len; /* only filled for memory mapped ports */
584 * Initialize a rte_pci_ioport object for a pci device io resource.
586 * This object is then used to gain access to those io resources (see below).
589 * A pointer to a rte_pci_device structure describing the device
592 * Index of the io pci resource we want to access.
594 * The rte_pci_ioport object to be initialized.
596 * 0 on success, negative on error.
598 int rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
599 struct rte_pci_ioport *p);
602 * Release any resources used in a rte_pci_ioport object.
605 * The rte_pci_ioport object to be uninitialized.
607 * 0 on success, negative on error.
609 int rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p);
612 * Read from a io pci resource.
615 * The rte_pci_ioport object from which we want to read.
617 * A data buffer where the bytes should be read into
619 * The length of the data buffer.
621 * The offset into the pci io resource.
623 void rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
624 void *data, size_t len, off_t offset);
627 * Write to a io pci resource.
630 * The rte_pci_ioport object to which we want to write.
632 * A data buffer where the bytes should be read into
634 * The length of the data buffer.
636 * The offset into the pci io resource.
638 void rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
639 const void *data, size_t len, off_t offset);
645 #endif /* _RTE_PCI_H_ */