af758777d9432334e67b9ef46ee84cac0fff5dbb
[dpdk.git] / lib / librte_eal / common / include / rte_pci.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 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 /*   BSD LICENSE
34  *
35  *   Copyright(c) 2013 6WIND.
36  *
37  *   Redistribution and use in source and binary forms, with or without
38  *   modification, are permitted provided that the following conditions
39  *   are met:
40  *
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
46  *       distribution.
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.
50  *
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.
62  */
63
64 #ifndef _RTE_PCI_H_
65 #define _RTE_PCI_H_
66
67 /**
68  * @file
69  *
70  * RTE PCI Interface
71  */
72
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76
77 #include <stdlib.h>
78 #include <limits.h>
79 #include <errno.h>
80 #include <sys/queue.h>
81 #include <stdint.h>
82 #include <inttypes.h>
83 #include <rte_interrupts.h>
84
85 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
86 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
87
88 extern struct pci_driver_list driver_list; /**< Global list of PCI drivers. */
89 extern struct pci_device_list device_list; /**< Global list of PCI devices. */
90
91 /** Pathname of PCI devices directory. */
92 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
93
94 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
95 #define PCI_PRI_FMT "%.4"PRIx16":%.2"PRIx8":%.2"PRIx8".%"PRIx8
96
97 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
98 #define PCI_SHORT_PRI_FMT "%.2"PRIx8":%.2"PRIx8".%"PRIx8
99
100 /** Nb. of values in PCI device identifier format string. */
101 #define PCI_FMT_NVAL 4
102
103 /** Nb. of values in PCI resource format. */
104 #define PCI_RESOURCE_FMT_NVAL 3
105
106 /**
107  * A structure describing a PCI resource.
108  */
109 struct rte_pci_resource {
110         uint64_t phys_addr;   /**< Physical address, 0 if no resource. */
111         uint64_t len;         /**< Length of the resource. */
112         void *addr;           /**< Virtual address, NULL when not mapped. */
113 };
114
115 /** Maximum number of PCI resources. */
116 #define PCI_MAX_RESOURCE 7
117
118 /**
119  * A structure describing an ID for a PCI driver. Each driver provides a
120  * table of these IDs for each device that it supports.
121  */
122 struct rte_pci_id {
123         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
124         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
125         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
126         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
127 };
128
129 /**
130  * A structure describing the location of a PCI device.
131  */
132 struct rte_pci_addr {
133         uint16_t domain;                /**< Device domain */
134         uint8_t bus;                    /**< Device bus */
135         uint8_t devid;                  /**< Device ID */
136         uint8_t function;               /**< Device function. */
137 };
138
139 /**
140  * A structure describing a PCI device.
141  */
142 struct rte_pci_device {
143         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
144         struct rte_pci_addr addr;               /**< PCI location. */
145         struct rte_pci_id id;                   /**< PCI ID. */
146         struct rte_pci_resource mem_resource[PCI_MAX_RESOURCE];   /**< PCI Memory Resource */
147         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
148         const struct rte_pci_driver *driver;    /**< Associated driver */
149         uint16_t max_vfs;                       /**< sriov enable if not zero */
150         int numa_node;                          /**< NUMA node connection */
151         unsigned int blacklisted:1;             /**< Device is blacklisted */
152 };
153
154 /** Any PCI device identifier (vendor, device, ...) */
155 #define PCI_ANY_ID (0xffff)
156
157 #ifdef __cplusplus
158 /** C++ macro used to help building up tables of device IDs */
159 #define RTE_PCI_DEVICE(vend, dev) \
160         (vend),                   \
161         (dev),                    \
162         PCI_ANY_ID,               \
163         PCI_ANY_ID
164 #else
165 /** Macro used to help building up tables of device IDs */
166 #define RTE_PCI_DEVICE(vend, dev)          \
167         .vendor_id = (vend),               \
168         .device_id = (dev),                \
169         .subsystem_vendor_id = PCI_ANY_ID, \
170         .subsystem_device_id = PCI_ANY_ID
171 #endif
172
173 struct rte_pci_driver;
174
175 /**
176  * Initialisation function for the driver called during PCI probing.
177  */
178 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
179
180 /**
181  * A structure describing a PCI driver.
182  */
183 struct rte_pci_driver {
184         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
185         const char *name;                       /**< Driver name. */
186         pci_devinit_t *devinit;                 /**< Device init. function. */
187         struct rte_pci_id *id_table;            /**< ID table, NULL terminated. */
188         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
189 };
190
191 /** Device needs igb_uio kernel module */
192 #define RTE_PCI_DRV_NEED_IGB_UIO 0x0001
193 /** Device driver must be registered several times until failure */
194 #define RTE_PCI_DRV_MULTIPLE 0x0002
195
196 /**< Internal use only - Macro used by pci addr parsing functions **/
197 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
198 do {                                                               \
199         unsigned long val;                                      \
200         char *end;                                              \
201         errno = 0;                                              \
202         val = strtoul((in), &end, 16);                          \
203         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
204                 return (-EINVAL);                               \
205         (fd) = (typeof (fd))val;                                \
206         (in) = end + 1;                                         \
207 } while(0)
208
209 /**
210  * Utility function to produce a PCI Bus-Device-Function value
211  * given a string representation. Assumes that the BDF is provided without
212  * a domain prefix (i.e. domain returned is always 0)
213  *
214  * @param input
215  *      The input string to be parsed. Should have the format XX:XX.X
216  * @param dev_addr
217  *      The PCI Bus-Device-Function address to be returned. Domain will always be
218  *      returned as 0
219  * @return
220  *  0 on success, negative on error.
221  */
222 static inline int
223 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
224 {
225         dev_addr->domain = 0;
226         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
227         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
228         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
229         return (0);
230 }
231
232 /**
233  * Utility function to produce a PCI Bus-Device-Function value
234  * given a string representation. Assumes that the BDF is provided including
235  * a domain prefix.
236  *
237  * @param input
238  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
239  * @param dev_addr
240  *      The PCI Bus-Device-Function address to be returned
241  * @return
242  *  0 on success, negative on error.
243  */
244 static inline int
245 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
246 {
247         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
248         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
249         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
250         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
251         return (0);
252 }
253 #undef GET_PCIADDR_FIELD
254
255 /**
256  * Probe the PCI bus for registered drivers.
257  *
258  * Scan the content of the PCI bus, and call the probe() function for
259  * all registered drivers that have a matching entry in its id_table
260  * for discovered devices.
261  *
262  * @return
263  *   - 0 on success.
264  *   - Negative on error.
265  */
266 int rte_eal_pci_probe(void);
267
268 /**
269  * Dump the content of the PCI bus.
270  */
271 void rte_eal_pci_dump(void);
272
273 /**
274  * Register a PCI driver.
275  *
276  * @param driver
277  *   A pointer to a rte_pci_driver structure describing the driver
278  *   to be registered.
279  */
280 void rte_eal_pci_register(struct rte_pci_driver *driver);
281
282 /**
283  * Unregister a PCI driver.
284  *
285  * @param driver
286  *   A pointer to a rte_pci_driver structure describing the driver
287  *   to be unregistered.
288  */
289 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
290
291 /**
292  * Register a list of PCI locations that will be blacklisted (not used by DPDK).
293  *
294  * @param blacklist
295  *   List of PCI device addresses that will not be used by DPDK.
296  * @param size
297  *   Number of items in the list.
298  */
299 void rte_eal_pci_set_blacklist(struct rte_pci_addr *blacklist, unsigned size);
300
301 #ifdef __cplusplus
302 }
303 #endif
304
305 #endif /* _RTE_PCI_H_ */