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 /** Pathname of PCI devices directory. */
91 const char *pci_get_sysfs_path(void);
93 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
94 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
95 #define PCI_PRI_STR_SIZE sizeof("XXXX:XX:XX.X")
97 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
98 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
100 /** Nb. of values in PCI device identifier format string. */
101 #define PCI_FMT_NVAL 4
103 /** Nb. of values in PCI resource format. */
104 #define PCI_RESOURCE_FMT_NVAL 3
106 /** Maximum number of PCI resources. */
107 #define PCI_MAX_RESOURCE 6
109 /** Name of PCI Bus */
110 #define PCI_BUS_NAME "PCI"
112 /* Forward declarations */
113 struct rte_pci_device;
114 struct rte_pci_driver;
116 /** List of PCI devices */
117 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
118 /** List of PCI drivers */
119 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
121 /* PCI Bus iterators */
122 #define FOREACH_DEVICE_ON_PCIBUS(p) \
123 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
125 #define FOREACH_DRIVER_ON_PCIBUS(p) \
126 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
129 * A structure describing an ID for a PCI driver. Each driver provides a
130 * table of these IDs for each device that it supports.
133 uint32_t class_id; /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
134 uint16_t vendor_id; /**< Vendor ID or PCI_ANY_ID. */
135 uint16_t device_id; /**< Device ID or PCI_ANY_ID. */
136 uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
137 uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
141 * A structure describing the location of a PCI device.
143 struct rte_pci_addr {
144 uint16_t domain; /**< Device domain */
145 uint8_t bus; /**< Device bus */
146 uint8_t devid; /**< Device ID */
147 uint8_t function; /**< Device function. */
152 enum rte_kernel_driver {
153 RTE_KDRV_UNKNOWN = 0,
156 RTE_KDRV_UIO_GENERIC,
162 * A structure describing a PCI device.
164 struct rte_pci_device {
165 TAILQ_ENTRY(rte_pci_device) next; /**< Next probed PCI device. */
166 struct rte_device device; /**< Inherit core device */
167 struct rte_pci_addr addr; /**< PCI location. */
168 struct rte_pci_id id; /**< PCI ID. */
169 struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
170 /**< PCI Memory Resource */
171 struct rte_intr_handle intr_handle; /**< Interrupt handle */
172 struct rte_pci_driver *driver; /**< Associated driver */
173 uint16_t max_vfs; /**< sriov enable if not zero */
174 enum rte_kernel_driver kdrv; /**< Kernel driver passthrough */
175 char name[PCI_PRI_STR_SIZE+1]; /**< PCI location (ASCII) */
180 * Helper macro for drivers that need to convert to struct rte_pci_device.
182 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
184 /** Any PCI device identifier (vendor, device, ...) */
185 #define PCI_ANY_ID (0xffff)
186 #define RTE_CLASS_ANY_ID (0xffffff)
189 /** C++ macro used to help building up tables of device IDs */
190 #define RTE_PCI_DEVICE(vend, dev) \
197 /** Macro used to help building up tables of device IDs */
198 #define RTE_PCI_DEVICE(vend, dev) \
199 .class_id = RTE_CLASS_ANY_ID, \
200 .vendor_id = (vend), \
201 .device_id = (dev), \
202 .subsystem_vendor_id = PCI_ANY_ID, \
203 .subsystem_device_id = PCI_ANY_ID
207 * Initialisation function for the driver called during PCI probing.
209 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
212 * Uninitialisation function for the driver called during hotplugging.
214 typedef int (pci_remove_t)(struct rte_pci_device *);
217 * A structure describing a PCI driver.
219 struct rte_pci_driver {
220 TAILQ_ENTRY(rte_pci_driver) next; /**< Next in list. */
221 struct rte_driver driver; /**< Inherit core driver. */
222 struct rte_pci_bus *bus; /**< PCI bus reference. */
223 pci_probe_t *probe; /**< Device Probe function. */
224 pci_remove_t *remove; /**< Device Remove function. */
225 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
226 uint32_t drv_flags; /**< Flags contolling handling of device. */
230 * Structure describing the PCI bus
233 struct rte_bus bus; /**< Inherit the generic class */
234 struct rte_pci_device_list device_list; /**< List of PCI devices */
235 struct rte_pci_driver_list driver_list; /**< List of PCI drivers */
238 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
239 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
240 /** Device driver supports link state interrupt */
241 #define RTE_PCI_DRV_INTR_LSC 0x0008
242 /** Device driver supports device removal interrupt */
243 #define RTE_PCI_DRV_INTR_RMV 0x0010
246 * A structure describing a PCI mapping.
257 * A structure describing a mapped PCI resource.
258 * For multi-process we need to reproduce all PCI mappings in secondary
259 * processes, so save them in a tailq.
261 struct mapped_pci_resource {
262 TAILQ_ENTRY(mapped_pci_resource) next;
264 struct rte_pci_addr pci_addr;
267 struct pci_map maps[PCI_MAX_RESOURCE];
270 /** mapped pci device list */
271 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
273 /**< Internal use only - Macro used by pci addr parsing functions **/
274 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
279 val = strtoul((in), &end, 16); \
280 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
282 (fd) = (typeof (fd))val; \
287 * Utility function to produce a PCI Bus-Device-Function value
288 * given a string representation. Assumes that the BDF is provided without
289 * a domain prefix (i.e. domain returned is always 0)
292 * The input string to be parsed. Should have the format XX:XX.X
294 * The PCI Bus-Device-Function address to be returned. Domain will always be
297 * 0 on success, negative on error.
300 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
302 dev_addr->domain = 0;
303 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
304 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
305 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
310 * Utility function to produce a PCI Bus-Device-Function value
311 * given a string representation. Assumes that the BDF is provided including
315 * The input string to be parsed. Should have the format XXXX:XX:XX.X
317 * The PCI Bus-Device-Function address to be returned
319 * 0 on success, negative on error.
322 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
324 GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
325 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
326 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
327 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
330 #undef GET_PCIADDR_FIELD
333 * Utility function to write a pci device name, this device name can later be
334 * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
338 * The PCI Bus-Device-Function address
340 * The output buffer string
342 * The output buffer size
345 rte_pci_device_name(const struct rte_pci_addr *addr,
346 char *output, size_t size)
348 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
349 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
350 addr->domain, addr->bus,
351 addr->devid, addr->function) >= 0);
354 /* Compare two PCI device addresses. */
356 * Utility function to compare two PCI device addresses.
359 * The PCI Bus-Device-Function address to compare
361 * The PCI Bus-Device-Function address to compare
363 * 0 on equal PCI address.
364 * Positive on addr is greater than addr2.
365 * Negative on addr is less than addr2, or error.
368 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
369 const struct rte_pci_addr *addr2)
371 uint64_t dev_addr, dev_addr2;
373 if ((addr == NULL) || (addr2 == NULL))
376 dev_addr = (addr->domain << 24) | (addr->bus << 16) |
377 (addr->devid << 8) | addr->function;
378 dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
379 (addr2->devid << 8) | addr2->function;
381 if (dev_addr > dev_addr2)
383 else if (dev_addr < dev_addr2)
390 * Scan the content of the PCI bus, and the devices in the devices
394 * 0 on success, negative on error
396 int rte_pci_scan(void);
409 * Map the PCI device resources in user space virtual memory address
411 * Note that driver should not call this function when flag
412 * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
416 * A pointer to a rte_pci_device structure describing the device
420 * 0 on success, negative on error and positive if no driver
421 * is found for the device.
423 int rte_pci_map_device(struct rte_pci_device *dev);
429 * A pointer to a rte_pci_device structure describing the device
432 void rte_pci_unmap_device(struct rte_pci_device *dev);
436 * Map a particular resource from a file.
438 * @param requested_addr
439 * The starting address for the new mapping range.
441 * The file descriptor.
443 * The offset for the mapping range.
445 * The size for the mapping range.
446 * @param additional_flags
447 * The additional flags for the mapping range.
449 * - On success, the function returns a pointer to the mapped area.
450 * - On error, the value MAP_FAILED is returned.
452 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
453 size_t size, int additional_flags);
457 * Unmap a particular resource.
459 * @param requested_addr
460 * The address for the unmapping range.
462 * The size for the unmapping range.
464 void pci_unmap_resource(void *requested_addr, size_t size);
467 * Probe the single PCI device.
469 * Scan the content of the PCI bus, and find the pci device specified by pci
470 * address, then call the probe() function for registered driver that has a
471 * matching entry in its id_table for discovered device.
474 * The PCI Bus-Device-Function address to probe.
477 * - Negative on error.
479 int rte_pci_probe_one(const struct rte_pci_addr *addr);
482 * Close the single PCI device.
484 * Scan the content of the PCI bus, and find the pci device specified by pci
485 * address, then call the remove() function for registered driver that has a
486 * matching entry in its id_table for discovered device.
489 * The PCI Bus-Device-Function address to close.
492 * - Negative on error.
494 int rte_pci_detach(const struct rte_pci_addr *addr);
497 * Dump the content of the PCI bus.
500 * A pointer to a file for output
502 void rte_pci_dump(FILE *f);
505 * Register a PCI driver.
508 * A pointer to a rte_pci_driver structure describing the driver
511 void rte_pci_register(struct rte_pci_driver *driver);
513 /** Helper for PCI device registration from driver (eth, crypto) instance */
514 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
515 RTE_INIT(pciinitfn_ ##nm); \
516 static void pciinitfn_ ##nm(void) \
518 (pci_drv).driver.name = RTE_STR(nm);\
519 rte_pci_register(&pci_drv); \
521 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
524 * Unregister a PCI driver.
527 * A pointer to a rte_pci_driver structure describing the driver
528 * to be unregistered.
530 void rte_pci_unregister(struct rte_pci_driver *driver);
533 * Read PCI config space.
536 * A pointer to a rte_pci_device structure describing the device
539 * A data buffer where the bytes should be read into
541 * The length of the data buffer.
543 * The offset into PCI config space
545 int rte_pci_read_config(const struct rte_pci_device *device,
546 void *buf, size_t len, off_t offset);
549 * Write PCI config space.
552 * A pointer to a rte_pci_device structure describing the device
555 * A data buffer containing the bytes should be written
557 * The length of the data buffer.
559 * The offset into PCI config space
561 int rte_pci_write_config(const struct rte_pci_device *device,
562 const void *buf, size_t len, off_t offset);
565 * A structure used to access io resources for a pci device.
566 * rte_pci_ioport is arch, os, driver specific, and should not be used outside
569 struct rte_pci_ioport {
570 struct rte_pci_device *dev;
572 uint64_t len; /* only filled for memory mapped ports */
576 * Initialize a rte_pci_ioport object for a pci device io resource.
578 * This object is then used to gain access to those io resources (see below).
581 * A pointer to a rte_pci_device structure describing the device
584 * Index of the io pci resource we want to access.
586 * The rte_pci_ioport object to be initialized.
588 * 0 on success, negative on error.
590 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
591 struct rte_pci_ioport *p);
594 * Release any resources used in a rte_pci_ioport object.
597 * The rte_pci_ioport object to be uninitialized.
599 * 0 on success, negative on error.
601 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
604 * Read from a io pci resource.
607 * The rte_pci_ioport object from which we want to read.
609 * A data buffer where the bytes should be read into
611 * The length of the data buffer.
613 * The offset into the pci io resource.
615 void rte_pci_ioport_read(struct rte_pci_ioport *p,
616 void *data, size_t len, off_t offset);
619 * Write to a io pci resource.
622 * The rte_pci_ioport object to which we want to write.
624 * A data buffer where the bytes should be read into
626 * The length of the data buffer.
628 * The offset into the pci io resource.
630 void rte_pci_ioport_write(struct rte_pci_ioport *p,
631 const void *data, size_t len, off_t offset);
637 #endif /* _RTE_PCI_H_ */