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