0f16f2cd7a5852d7d34fee07d5626089280b8c51
[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 /**
224  * A structure describing a PCI mapping.
225  */
226 struct pci_map {
227         void *addr;
228         char *path;
229         uint64_t offset;
230         uint64_t size;
231         uint64_t phaddr;
232 };
233
234 /**
235  * A structure describing a mapped PCI resource.
236  * For multi-process we need to reproduce all PCI mappings in secondary
237  * processes, so save them in a tailq.
238  */
239 struct mapped_pci_resource {
240         TAILQ_ENTRY(mapped_pci_resource) next;
241
242         struct rte_pci_addr pci_addr;
243         char path[PATH_MAX];
244         int nb_maps;
245         struct pci_map maps[PCI_MAX_RESOURCE];
246 };
247
248 /** mapped pci device list */
249 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
250
251 /**< Internal use only - Macro used by pci addr parsing functions **/
252 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
253 do {                                                               \
254         unsigned long val;                                      \
255         char *end;                                              \
256         errno = 0;                                              \
257         val = strtoul((in), &end, 16);                          \
258         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
259                 return -EINVAL;                                 \
260         (fd) = (typeof (fd))val;                                \
261         (in) = end + 1;                                         \
262 } while(0)
263
264 /**
265  * Utility function to produce a PCI Bus-Device-Function value
266  * given a string representation. Assumes that the BDF is provided without
267  * a domain prefix (i.e. domain returned is always 0)
268  *
269  * @param input
270  *      The input string to be parsed. Should have the format XX:XX.X
271  * @param dev_addr
272  *      The PCI Bus-Device-Function address to be returned. Domain will always be
273  *      returned as 0
274  * @return
275  *  0 on success, negative on error.
276  */
277 static inline int
278 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
279 {
280         dev_addr->domain = 0;
281         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
282         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
283         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
284         return 0;
285 }
286
287 /**
288  * Utility function to produce a PCI Bus-Device-Function value
289  * given a string representation. Assumes that the BDF is provided including
290  * a domain prefix.
291  *
292  * @param input
293  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
294  * @param dev_addr
295  *      The PCI Bus-Device-Function address to be returned
296  * @return
297  *  0 on success, negative on error.
298  */
299 static inline int
300 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
301 {
302         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
303         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
304         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
305         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
306         return 0;
307 }
308 #undef GET_PCIADDR_FIELD
309
310 /* Compare two PCI device addresses. */
311 /**
312  * Utility function to compare two PCI device addresses.
313  *
314  * @param addr
315  *      The PCI Bus-Device-Function address to compare
316  * @param addr2
317  *      The PCI Bus-Device-Function address to compare
318  * @return
319  *      0 on equal PCI address.
320  *      Positive on addr is greater than addr2.
321  *      Negative on addr is less than addr2, or error.
322  */
323 static inline int
324 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
325                          const struct rte_pci_addr *addr2)
326 {
327         uint64_t dev_addr, dev_addr2;
328
329         if ((addr == NULL) || (addr2 == NULL))
330                 return -1;
331
332         dev_addr = (addr->domain << 24) | (addr->bus << 16) |
333                                 (addr->devid << 8) | addr->function;
334         dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
335                                 (addr2->devid << 8) | addr2->function;
336
337         if (dev_addr > dev_addr2)
338                 return 1;
339         else if (dev_addr < dev_addr2)
340                 return -1;
341         else
342                 return 0;
343 }
344
345 /**
346  * Scan the content of the PCI bus, and the devices in the devices
347  * list
348  *
349  * @return
350  *  0 on success, negative on error
351  */
352 int rte_eal_pci_scan(void);
353
354 /**
355  * Probe the PCI bus for registered drivers.
356  *
357  * Scan the content of the PCI bus, and call the probe() function for
358  * all registered drivers that have a matching entry in its id_table
359  * for discovered devices.
360  *
361  * @return
362  *   - 0 on success.
363  *   - Negative on error.
364  */
365 int rte_eal_pci_probe(void);
366
367 /**
368  * @internal
369  * Map a particular resource from a file.
370  *
371  * @param requested_addr
372  *      The starting address for the new mapping range.
373  * @param fd
374  *      The file descriptor.
375  * @param offset
376  *      The offset for the mapping range.
377  * @param size
378  *      The size for the mapping range.
379  * @param additional_flags
380  *      The additional flags for the mapping range.
381  * @return
382  *   - On success, the function returns a pointer to the mapped area.
383  *   - On error, the value MAP_FAILED is returned.
384  */
385 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
386                 size_t size, int additional_flags);
387
388 /**
389  * @internal
390  * Unmap a particular resource.
391  *
392  * @param requested_addr
393  *      The address for the unmapping range.
394  * @param size
395  *      The size for the unmapping range.
396  */
397 void pci_unmap_resource(void *requested_addr, size_t size);
398
399 #ifdef RTE_LIBRTE_EAL_HOTPLUG
400 /**
401  * Probe the single PCI device.
402  *
403  * Scan the content of the PCI bus, and find the pci device specified by pci
404  * address, then call the probe() function for registered driver that has a
405  * matching entry in its id_table for discovered device.
406  *
407  * @param addr
408  *      The PCI Bus-Device-Function address to probe.
409  * @return
410  *   - 0 on success.
411  *   - Negative on error.
412  */
413 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
414
415 /**
416  * Close the single PCI device.
417  *
418  * Scan the content of the PCI bus, and find the pci device specified by pci
419  * address, then call the close() function for registered driver that has a
420  * matching entry in its id_table for discovered device.
421  *
422  * @param addr
423  *      The PCI Bus-Device-Function address to close.
424  * @return
425  *   - 0 on success.
426  *   - Negative on error.
427  */
428 int rte_eal_pci_close_one(const struct rte_pci_addr *addr);
429 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
430
431 /**
432  * Dump the content of the PCI bus.
433  *
434  * @param f
435  *   A pointer to a file for output
436  */
437 void rte_eal_pci_dump(FILE *f);
438
439 /**
440  * Register a PCI driver.
441  *
442  * @param driver
443  *   A pointer to a rte_pci_driver structure describing the driver
444  *   to be registered.
445  */
446 void rte_eal_pci_register(struct rte_pci_driver *driver);
447
448 /**
449  * Unregister a PCI driver.
450  *
451  * @param driver
452  *   A pointer to a rte_pci_driver structure describing the driver
453  *   to be unregistered.
454  */
455 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
456
457 #ifdef __cplusplus
458 }
459 #endif
460
461 #endif /* _RTE_PCI_H_ */