1550d49d858be8d72c959aced1087d451b57f4ab
[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 /** IO resource type: memory address space */
108 #define IORESOURCE_MEM        0x00000200
109
110 /**
111  * A structure describing a PCI resource.
112  */
113 struct rte_pci_resource {
114         uint64_t phys_addr;   /**< Physical address, 0 if no resource. */
115         uint64_t len;         /**< Length of the resource. */
116         void *addr;           /**< Virtual address, NULL when not mapped. */
117 };
118
119 /** Maximum number of PCI resources. */
120 #define PCI_MAX_RESOURCE 6
121
122 /**
123  * A structure describing an ID for a PCI driver. Each driver provides a
124  * table of these IDs for each device that it supports.
125  */
126 struct rte_pci_id {
127         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
128         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
129         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
130         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
131 };
132
133 /**
134  * A structure describing the location of a PCI device.
135  */
136 struct rte_pci_addr {
137         uint16_t domain;                /**< Device domain */
138         uint8_t bus;                    /**< Device bus */
139         uint8_t devid;                  /**< Device ID */
140         uint8_t function;               /**< Device function. */
141 };
142
143 struct rte_devargs;
144
145 enum rte_pt_driver {
146         RTE_PT_UNKNOWN          = 0,
147         RTE_PT_IGB_UIO          = 1,
148         RTE_PT_VFIO             = 2,
149         RTE_PT_UIO_GENERIC      = 3,
150 };
151
152 /**
153  * A structure describing a PCI device.
154  */
155 struct rte_pci_device {
156         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
157         struct rte_pci_addr addr;               /**< PCI location. */
158         struct rte_pci_id id;                   /**< PCI ID. */
159         struct rte_pci_resource mem_resource[PCI_MAX_RESOURCE];   /**< PCI Memory Resource */
160         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
161         const struct rte_pci_driver *driver;    /**< Associated driver */
162         uint16_t max_vfs;                       /**< sriov enable if not zero */
163         int numa_node;                          /**< NUMA node connection */
164         struct rte_devargs *devargs;            /**< Device user arguments */
165         enum rte_pt_driver pt_driver;           /**< Driver of passthrough */
166 };
167
168 /** Any PCI device identifier (vendor, device, ...) */
169 #define PCI_ANY_ID (0xffff)
170
171 #ifdef __cplusplus
172 /** C++ macro used to help building up tables of device IDs */
173 #define RTE_PCI_DEVICE(vend, dev) \
174         (vend),                   \
175         (dev),                    \
176         PCI_ANY_ID,               \
177         PCI_ANY_ID
178 #else
179 /** Macro used to help building up tables of device IDs */
180 #define RTE_PCI_DEVICE(vend, dev)          \
181         .vendor_id = (vend),               \
182         .device_id = (dev),                \
183         .subsystem_vendor_id = PCI_ANY_ID, \
184         .subsystem_device_id = PCI_ANY_ID
185 #endif
186
187 struct rte_pci_driver;
188
189 /**
190  * Initialisation function for the driver called during PCI probing.
191  */
192 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
193
194 /**
195  * Uninitialisation function for the driver called during hotplugging.
196  */
197 typedef int (pci_devuninit_t)(struct rte_pci_device *);
198
199 /**
200  * A structure describing a PCI driver.
201  */
202 struct rte_pci_driver {
203         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
204         const char *name;                       /**< Driver name. */
205         pci_devinit_t *devinit;                 /**< Device init. function. */
206         pci_devuninit_t *devuninit;             /**< Device uninit function. */
207         struct rte_pci_id *id_table;            /**< ID table, NULL terminated. */
208         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
209 };
210
211 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
212 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
213 /** Device driver must be registered several times until failure - deprecated */
214 #pragma GCC poison RTE_PCI_DRV_MULTIPLE
215 /** Device needs to be unbound even if no module is provided */
216 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
217 /** Device driver supports link state interrupt */
218 #define RTE_PCI_DRV_INTR_LSC    0x0008
219
220 /**< Internal use only - Macro used by pci addr parsing functions **/
221 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
222 do {                                                               \
223         unsigned long val;                                      \
224         char *end;                                              \
225         errno = 0;                                              \
226         val = strtoul((in), &end, 16);                          \
227         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
228                 return (-EINVAL);                               \
229         (fd) = (typeof (fd))val;                                \
230         (in) = end + 1;                                         \
231 } while(0)
232
233 /**
234  * Utility function to produce a PCI Bus-Device-Function value
235  * given a string representation. Assumes that the BDF is provided without
236  * a domain prefix (i.e. domain returned is always 0)
237  *
238  * @param input
239  *      The input string to be parsed. Should have the format XX:XX.X
240  * @param dev_addr
241  *      The PCI Bus-Device-Function address to be returned. Domain will always be
242  *      returned as 0
243  * @return
244  *  0 on success, negative on error.
245  */
246 static inline int
247 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
248 {
249         dev_addr->domain = 0;
250         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
251         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
252         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
253         return (0);
254 }
255
256 /**
257  * Utility function to produce a PCI Bus-Device-Function value
258  * given a string representation. Assumes that the BDF is provided including
259  * a domain prefix.
260  *
261  * @param input
262  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
263  * @param dev_addr
264  *      The PCI Bus-Device-Function address to be returned
265  * @return
266  *  0 on success, negative on error.
267  */
268 static inline int
269 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
270 {
271         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
272         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
273         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
274         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
275         return (0);
276 }
277 #undef GET_PCIADDR_FIELD
278
279 /* Compare two PCI device addresses. */
280 /**
281  * Utility function to compare two PCI device addresses.
282  *
283  * @param addr
284  *      The PCI Bus-Device-Function address to compare
285  * @param addr2
286  *      The PCI Bus-Device-Function address to compare
287  * @return
288  *      0 on equal PCI address.
289  *      Positive on addr is greater than addr2.
290  *      Negative on addr is less than addr2, or error.
291  */
292 static inline int
293 rte_eal_compare_pci_addr(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
294 {
295         uint64_t dev_addr, dev_addr2;
296
297         if ((addr == NULL) || (addr2 == NULL))
298                 return -1;
299
300         dev_addr = (addr->domain << 24) | (addr->bus << 16) |
301                                 (addr->devid << 8) | addr->function;
302         dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
303                                 (addr2->devid << 8) | addr2->function;
304
305         if (dev_addr > dev_addr2)
306                 return 1;
307         else if (dev_addr < dev_addr2)
308                 return -1;
309         else
310                 return 0;
311 }
312
313 /**
314  * Probe the PCI bus for registered drivers.
315  *
316  * Scan the content of the PCI bus, and call the probe() function for
317  * all registered drivers that have a matching entry in its id_table
318  * for discovered devices.
319  *
320  * @return
321  *   - 0 on success.
322  *   - Negative on error.
323  */
324 int rte_eal_pci_probe(void);
325
326 #ifdef RTE_LIBRTE_EAL_HOTPLUG
327 /**
328  * Probe the single PCI device.
329  *
330  * Scan the content of the PCI bus, and find the pci device specified by pci
331  * address, then call the probe() function for registered driver that has a
332  * matching entry in its id_table for discovered device.
333  *
334  * @param addr
335  *      The PCI Bus-Device-Function address to probe.
336  * @return
337  *   - 0 on success.
338  *   - Negative on error.
339  */
340 int rte_eal_pci_probe_one(struct rte_pci_addr *addr);
341
342 /**
343  * Close the single PCI device.
344  *
345  * Scan the content of the PCI bus, and find the pci device specified by pci
346  * address, then call the close() function for registered driver that has a
347  * matching entry in its id_table for discovered device.
348  *
349  * @param addr
350  *      The PCI Bus-Device-Function address to close.
351  * @return
352  *   - 0 on success.
353  *   - Negative on error.
354  */
355 int rte_eal_pci_close_one(struct rte_pci_addr *addr);
356 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
357
358 /**
359  * Dump the content of the PCI bus.
360  *
361  * @param f
362  *   A pointer to a file for output
363  */
364 void rte_eal_pci_dump(FILE *f);
365
366 /**
367  * Register a PCI driver.
368  *
369  * @param driver
370  *   A pointer to a rte_pci_driver structure describing the driver
371  *   to be registered.
372  */
373 void rte_eal_pci_register(struct rte_pci_driver *driver);
374
375 /**
376  * Unregister a PCI driver.
377  *
378  * @param driver
379  *   A pointer to a rte_pci_driver structure describing the driver
380  *   to be unregistered.
381  */
382 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
383
384 #ifdef __cplusplus
385 }
386 #endif
387
388 #endif /* _RTE_PCI_H_ */