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>
89 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
90 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
92 extern struct pci_driver_list pci_driver_list; /**< Global list of PCI drivers. */
93 extern struct pci_device_list pci_device_list; /**< Global list of PCI devices. */
95 /** Pathname of PCI devices directory. */
96 const char *pci_get_sysfs_path(void);
98 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
99 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
100 #define PCI_PRI_STR_SIZE sizeof("XXXX:XX:XX.X")
102 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
103 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
105 /** Nb. of values in PCI device identifier format string. */
106 #define PCI_FMT_NVAL 4
108 /** Nb. of values in PCI resource format. */
109 #define PCI_RESOURCE_FMT_NVAL 3
111 /** Maximum number of PCI resources. */
112 #define PCI_MAX_RESOURCE 6
115 * A structure describing an ID for a PCI driver. Each driver provides a
116 * table of these IDs for each device that it supports.
119 uint32_t class_id; /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
120 uint16_t vendor_id; /**< Vendor ID or PCI_ANY_ID. */
121 uint16_t device_id; /**< Device ID or PCI_ANY_ID. */
122 uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
123 uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
127 * A structure describing the location of a PCI device.
129 struct rte_pci_addr {
130 uint16_t domain; /**< Device domain */
131 uint8_t bus; /**< Device bus */
132 uint8_t devid; /**< Device ID */
133 uint8_t function; /**< Device function. */
138 enum rte_kernel_driver {
139 RTE_KDRV_UNKNOWN = 0,
142 RTE_KDRV_UIO_GENERIC,
148 * A structure describing a PCI device.
150 struct rte_pci_device {
151 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
152 struct rte_device device; /**< Inherit core device */
153 struct rte_pci_addr addr; /**< PCI location. */
154 struct rte_pci_id id; /**< PCI ID. */
155 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
156 /**< PCI Memory Resource */
157 struct rte_intr_handle intr_handle; /**< Interrupt handle */
158 struct rte_pci_driver *driver; /**< Associated driver */
159 uint16_t max_vfs; /**< sriov enable if not zero */
160 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
163 /** Any PCI device identifier (vendor, device, ...) */
164 #define PCI_ANY_ID (0xffff)
165 #define RTE_CLASS_ANY_ID (0xffffff)
168 /** C++ macro used to help building up tables of device IDs */
169 #define RTE_PCI_DEVICE(vend, dev) \
176 /** Macro used to help building up tables of device IDs */
177 #define RTE_PCI_DEVICE(vend, dev) \
178 .class_id = RTE_CLASS_ANY_ID, \
179 .vendor_id = (vend), \
180 .device_id = (dev), \
181 .subsystem_vendor_id = PCI_ANY_ID, \
182 .subsystem_device_id = PCI_ANY_ID
185 struct rte_pci_driver;
188 * Initialisation function for the driver called during PCI probing.
190 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
193 * Uninitialisation function for the driver called during hotplugging.
195 typedef int (pci_remove_t)(struct rte_pci_device *);
198 * A structure describing a PCI driver.
200 struct rte_pci_driver {
201 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
202 struct rte_driver driver; /**< Inherit core driver. */
203 pci_probe_t *probe; /**< Device Probe function. */
204 pci_remove_t *remove; /**< Device Remove function. */
205 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
206 uint32_t drv_flags; /**< Flags contolling handling of device. */
209 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
210 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
211 /** Device needs to be unbound even if no module is provided */
212 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
213 /** Device driver supports link state interrupt */
214 #define RTE_PCI_DRV_INTR_LSC 0x0008
215 /** Device driver supports detaching capability */
216 #define RTE_PCI_DRV_DETACHABLE 0x0010
219 * A structure describing a PCI mapping.
230 * A structure describing a mapped PCI resource.
231 * For multi-process we need to reproduce all PCI mappings in secondary
232 * processes, so save them in a tailq.
234 struct mapped_pci_resource {
235 TAILQ_ENTRY(mapped_pci_resource) next;
237 struct rte_pci_addr pci_addr;
240 struct pci_map maps[PCI_MAX_RESOURCE];
243 /** mapped pci device list */
244 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
246 /**< Internal use only - Macro used by pci addr parsing functions **/
247 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
252 val = strtoul((in), &end, 16); \
253 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
255 (fd) = (typeof (fd))val; \
260 * Utility function to produce a PCI Bus-Device-Function value
261 * given a string representation. Assumes that the BDF is provided without
262 * a domain prefix (i.e. domain returned is always 0)
265 * The input string to be parsed. Should have the format XX:XX.X
267 * The PCI Bus-Device-Function address to be returned. Domain will always be
270 * 0 on success, negative on error.
273 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
275 dev_addr->domain = 0;
276 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
277 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
278 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
283 * Utility function to produce a PCI Bus-Device-Function value
284 * given a string representation. Assumes that the BDF is provided including
288 * The input string to be parsed. Should have the format XXXX:XX:XX.X
290 * The PCI Bus-Device-Function address to be returned
292 * 0 on success, negative on error.
295 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
297 GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
298 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
299 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
300 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
303 #undef GET_PCIADDR_FIELD
306 * Utility function to write a pci device name, this device name can later be
307 * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
311 * The PCI Bus-Device-Function address
313 * The output buffer string
315 * The output buffer size
318 rte_eal_pci_device_name(const struct rte_pci_addr *addr,
319 char *output, size_t size)
321 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
322 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
323 addr->domain, addr->bus,
324 addr->devid, addr->function) >= 0);
327 /* Compare two PCI device addresses. */
329 * Utility function to compare two PCI device addresses.
332 * The PCI Bus-Device-Function address to compare
334 * The PCI Bus-Device-Function address to compare
336 * 0 on equal PCI address.
337 * Positive on addr is greater than addr2.
338 * Negative on addr is less than addr2, or error.
341 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
342 const struct rte_pci_addr *addr2)
344 uint64_t dev_addr, dev_addr2;
346 if ((addr == NULL) || (addr2 == NULL))
349 dev_addr = (addr->domain << 24) | (addr->bus << 16) |
350 (addr->devid << 8) | addr->function;
351 dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
352 (addr2->devid << 8) | addr2->function;
354 if (dev_addr > dev_addr2)
356 else if (dev_addr < dev_addr2)
363 * Scan the content of the PCI bus, and the devices in the devices
367 * 0 on success, negative on error
369 int rte_eal_pci_scan(void);
372 * Probe the PCI bus for registered drivers.
374 * Scan the content of the PCI bus, and call the probe() function for
375 * all registered drivers that have a matching entry in its id_table
376 * for discovered devices.
380 * - Negative on error.
382 int rte_eal_pci_probe(void);
385 * Map the PCI device resources in user space virtual memory address
387 * Note that driver should not call this function when flag
388 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
392 * A pointer to a rte_pci_device structure describing the device
396 * 0 on success, negative on error and positive if no driver
397 * is found for the device.
399 int rte_eal_pci_map_device(struct rte_pci_device *dev);
405 * A pointer to a rte_pci_device structure describing the device
408 void rte_eal_pci_unmap_device(struct rte_pci_device *dev);
412 * Map a particular resource from a file.
414 * @param requested_addr
415 * The starting address for the new mapping range.
417 * The file descriptor.
419 * The offset for the mapping range.
421 * The size for the mapping range.
422 * @param additional_flags
423 * The additional flags for the mapping range.
425 * - On success, the function returns a pointer to the mapped area.
426 * - On error, the value MAP_FAILED is returned.
428 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
429 size_t size, int additional_flags);
433 * Unmap a particular resource.
435 * @param requested_addr
436 * The address for the unmapping range.
438 * The size for the unmapping range.
440 void pci_unmap_resource(void *requested_addr, size_t size);
443 * Probe the single PCI device.
445 * Scan the content of the PCI bus, and find the pci device specified by pci
446 * address, then call the probe() function for registered driver that has a
447 * matching entry in its id_table for discovered device.
450 * The PCI Bus-Device-Function address to probe.
453 * - Negative on error.
455 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
458 * Close the single PCI device.
460 * Scan the content of the PCI bus, and find the pci device specified by pci
461 * address, then call the remove() function for registered driver that has a
462 * matching entry in its id_table for discovered device.
465 * The PCI Bus-Device-Function address to close.
468 * - Negative on error.
470 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
473 * Dump the content of the PCI bus.
476 * A pointer to a file for output
478 void rte_eal_pci_dump(FILE *f);
481 * Register a PCI driver.
484 * A pointer to a rte_pci_driver structure describing the driver
487 void rte_eal_pci_register(struct rte_pci_driver *driver);
489 /** Helper for PCI device registration from driver (eth, crypto) instance */
490 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
491 RTE_INIT(pciinitfn_ ##nm); \
492 static void pciinitfn_ ##nm(void) \
494 (pci_drv).driver.name = RTE_STR(nm);\
495 rte_eal_pci_register(&pci_drv); \
497 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
500 * Unregister a PCI driver.
503 * A pointer to a rte_pci_driver structure describing the driver
504 * to be unregistered.
506 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
509 * Read PCI config space.
512 * A pointer to a rte_pci_device structure describing the device
515 * A data buffer where the bytes should be read into
517 * The length of the data buffer.
519 * The offset into PCI config space
521 int rte_eal_pci_read_config(const struct rte_pci_device *device,
522 void *buf, size_t len, off_t offset);
525 * Write PCI config space.
528 * A pointer to a rte_pci_device structure describing the device
531 * A data buffer containing the bytes should be written
533 * The length of the data buffer.
535 * The offset into PCI config space
537 int rte_eal_pci_write_config(const struct rte_pci_device *device,
538 const void *buf, size_t len, off_t offset);
541 * A structure used to access io resources for a pci device.
542 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
545 struct rte_pci_ioport {
546 struct rte_pci_device *dev;
548 uint64_t len; /* only filled for memory mapped ports */
552 * Initialize a rte_pci_ioport object for a pci device io resource.
554 * This object is then used to gain access to those io resources (see below).
557 * A pointer to a rte_pci_device structure describing the device
560 * Index of the io pci resource we want to access.
562 * The rte_pci_ioport object to be initialized.
564 * 0 on success, negative on error.
566 int rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
567 struct rte_pci_ioport *p);
570 * Release any resources used in a rte_pci_ioport object.
573 * The rte_pci_ioport object to be uninitialized.
575 * 0 on success, negative on error.
577 int rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p);
580 * Read from a io pci resource.
583 * The rte_pci_ioport object from which we want to read.
585 * A data buffer where the bytes should be read into
587 * The length of the data buffer.
589 * The offset into the pci io resource.
591 void rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
592 void *data, size_t len, off_t offset);
595 * Write to a io pci resource.
598 * The rte_pci_ioport object to which we want to write.
600 * A data buffer where the bytes should be read into
602 * The length of the data buffer.
604 * The offset into the pci io resource.
606 void rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
607 const void *data, size_t len, off_t offset);
613 #endif /* _RTE_PCI_H_ */