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