1314d20f20966f79c3aad0a8120c5ff3faab7a4e
[dpdk.git] / lib / librte_eal / common / include / rte_pci.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34 /*   BSD LICENSE
35  *
36  *   Copyright(c) 2013 6WIND.
37  *
38  *   Redistribution and use in source and binary forms, with or without
39  *   modification, are permitted provided that the following conditions
40  *   are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in
46  *       the documentation and/or other materials provided with the
47  *       distribution.
48  *     * Neither the name of 6WIND S.A. nor the names of its
49  *       contributors may be used to endorse or promote products derived
50  *       from this software without specific prior written permission.
51  *
52  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  */
64
65 #ifndef _RTE_PCI_H_
66 #define _RTE_PCI_H_
67
68 /**
69  * @file
70  *
71  * RTE PCI Interface
72  */
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 #include <limits.h>
79 #include <sys/queue.h>
80 #include <stdint.h>
81 #include <inttypes.h>
82 #include <rte_interrupts.h>
83
84 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
85 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
86
87 extern struct pci_driver_list driver_list; /**< Global list of PCI drivers. */
88 extern struct pci_device_list device_list; /**< Global list of PCI devices. */
89
90 /** Pathname of PCI devices directory. */
91 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
92
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
96 /** Nb. of values in PCI device identifier format string. */
97 #define PCI_FMT_NVAL 4
98
99 /** Nb. of values in PCI resource format. */
100 #define PCI_RESOURCE_FMT_NVAL 3
101
102 /**
103  * A structure describing a PCI resource.
104  */
105 struct rte_pci_resource {
106         uint64_t phys_addr;   /**< Physical address, 0 if no resource. */
107         uint64_t len;         /**< Length of the resource. */
108         void *addr;           /**< Virtual address, NULL when not mapped. */
109 };
110
111 /** Maximum number of PCI resources. */
112 #define PCI_MAX_RESOURCE 7
113
114 /**
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.
117  */
118 struct rte_pci_id {
119         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
120         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
121         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
122         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
123 };
124
125 /**
126  * A structure describing the location of a PCI device.
127  */
128 struct rte_pci_addr {
129         uint16_t domain;                /**< Device domain */
130         uint8_t bus;                    /**< Device bus */
131         uint8_t devid;                  /**< Device ID */
132         uint8_t function;               /**< Device function. */
133 };
134
135 /**
136  * A structure describing a PCI device.
137  */
138 struct rte_pci_device {
139         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
140         struct rte_pci_addr addr;               /**< PCI location. */
141         struct rte_pci_id id;                   /**< PCI ID. */
142         struct rte_pci_resource mem_resource[PCI_MAX_RESOURCE];   /**< PCI Memory Resource */
143         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
144         const struct rte_pci_driver *driver;    /**< Associated driver */
145         uint16_t max_vfs;                       /**< sriov enable if not zero */
146         int numa_node;                          /**< NUMA node connection */
147         unsigned int blacklisted:1;             /**< Device is blacklisted */
148 };
149
150 /** Any PCI device identifier (vendor, device, ...) */
151 #define PCI_ANY_ID (0xffff)
152
153 #ifdef __cplusplus
154 /** C++ macro used to help building up tables of device IDs */
155 #define RTE_PCI_DEVICE(vend, dev) \
156         (vend),                   \
157         (dev),                    \
158         PCI_ANY_ID,               \
159         PCI_ANY_ID
160 #else
161 /** Macro used to help building up tables of device IDs */
162 #define RTE_PCI_DEVICE(vend, dev)          \
163         .vendor_id = (vend),               \
164         .device_id = (dev),                \
165         .subsystem_vendor_id = PCI_ANY_ID, \
166         .subsystem_device_id = PCI_ANY_ID
167 #endif
168
169 struct rte_pci_driver;
170
171 /**
172  * Initialisation function for the driver called during PCI probing.
173  */
174 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
175
176 /**
177  * A structure describing a PCI driver.
178  */
179 struct rte_pci_driver {
180         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
181         const char *name;                       /**< Driver name. */
182         pci_devinit_t *devinit;                 /**< Device init. function. */
183         struct rte_pci_id *id_table;            /**< ID table, NULL terminated. */
184         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
185 };
186
187 #ifdef RTE_EAL_UNBIND_PORTS
188 /** Device needs igb_uio kernel module */
189 #define RTE_PCI_DRV_NEED_IGB_UIO 0x0001
190 #endif
191 /** Device driver must be registered several times until failure */
192 #define RTE_PCI_DRV_MULTIPLE 0x0002
193
194 /**
195  * Probe the PCI bus for registered drivers.
196  *
197  * Scan the content of the PCI bus, and call the probe() function for
198  * all registered drivers that have a matching entry in its id_table
199  * for discovered devices.
200  *
201  * @return
202  *   - 0 on success.
203  *   - Negative on error.
204  */
205 int rte_eal_pci_probe(void);
206
207 /**
208  * Dump the content of the PCI bus.
209  */
210 void rte_eal_pci_dump(void);
211
212 /**
213  * Register a PCI driver.
214  *
215  * @param driver
216  *   A pointer to a rte_pci_driver structure describing the driver
217  *   to be registered.
218  */
219 void rte_eal_pci_register(struct rte_pci_driver *driver);
220
221 /**
222  * Unregister a PCI driver.
223  *
224  * @param driver
225  *   A pointer to a rte_pci_driver structure describing the driver
226  *   to be unregistered.
227  */
228 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
229
230 /**
231  * Register a list of PCI locations that will be blacklisted (not used by DPDK).
232  *
233  * @param blacklist
234  *   List of PCI device addresses that will not be used by DPDK.
235  * @param size
236  *   Number of items in the list.
237  */
238 void rte_eal_pci_set_blacklist(struct rte_pci_addr *blacklist, unsigned size);
239
240 #ifdef __cplusplus
241 }
242 #endif
243
244 #endif /* _RTE_PCI_H_ */