Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_eal / common / include / rte_pci.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 const char *pci_get_sysfs_path(void);
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 /**
109  * A structure describing a PCI resource.
110  */
111 struct rte_pci_resource {
112         uint64_t phys_addr;   /**< Physical address, 0 if no resource. */
113         uint64_t len;         /**< Length of the resource. */
114         void *addr;           /**< Virtual address, NULL when not mapped. */
115 };
116
117 /** Maximum number of PCI resources. */
118 #define PCI_MAX_RESOURCE 6
119
120 /**
121  * A structure describing an ID for a PCI driver. Each driver provides a
122  * table of these IDs for each device that it supports.
123  */
124 struct rte_pci_id {
125         uint32_t class_id;            /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
126         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
127         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
128         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
129         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
130 };
131
132 /**
133  * A structure describing the location of a PCI device.
134  */
135 struct rte_pci_addr {
136         uint16_t domain;                /**< Device domain */
137         uint8_t bus;                    /**< Device bus */
138         uint8_t devid;                  /**< Device ID */
139         uint8_t function;               /**< Device function. */
140 };
141
142 struct rte_devargs;
143
144 enum rte_kernel_driver {
145         RTE_KDRV_UNKNOWN = 0,
146         RTE_KDRV_IGB_UIO,
147         RTE_KDRV_VFIO,
148         RTE_KDRV_UIO_GENERIC,
149         RTE_KDRV_NIC_UIO,
150         RTE_KDRV_NONE,
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 #define RTE_CLASS_ANY_ID (0xffffff)
172
173 #ifdef __cplusplus
174 /** C++ macro used to help building up tables of device IDs */
175 #define RTE_PCI_DEVICE(vend, dev) \
176         RTE_CLASS_ANY_ID,         \
177         (vend),                   \
178         (dev),                    \
179         PCI_ANY_ID,               \
180         PCI_ANY_ID
181 #else
182 /** Macro used to help building up tables of device IDs */
183 #define RTE_PCI_DEVICE(vend, dev)          \
184         .class_id = RTE_CLASS_ANY_ID,      \
185         .vendor_id = (vend),               \
186         .device_id = (dev),                \
187         .subsystem_vendor_id = PCI_ANY_ID, \
188         .subsystem_device_id = PCI_ANY_ID
189 #endif
190
191 struct rte_pci_driver;
192
193 /**
194  * Initialisation function for the driver called during PCI probing.
195  */
196 typedef int (pci_devinit_t)(struct rte_pci_driver *, struct rte_pci_device *);
197
198 /**
199  * Uninitialisation function for the driver called during hotplugging.
200  */
201 typedef int (pci_devuninit_t)(struct rte_pci_device *);
202
203 /**
204  * A structure describing a PCI driver.
205  */
206 struct rte_pci_driver {
207         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
208         const char *name;                       /**< Driver name. */
209         pci_devinit_t *devinit;                 /**< Device init. function. */
210         pci_devuninit_t *devuninit;             /**< Device uninit function. */
211         const struct rte_pci_id *id_table;      /**< ID table, NULL terminated. */
212         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
213 };
214
215 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
216 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
217 /** Device needs to be unbound even if no module is provided */
218 #define RTE_PCI_DRV_FORCE_UNBIND 0x0004
219 /** Device driver supports link state interrupt */
220 #define RTE_PCI_DRV_INTR_LSC    0x0008
221 /** Device driver supports detaching capability */
222 #define RTE_PCI_DRV_DETACHABLE  0x0010
223
224 /**
225  * A structure describing a PCI mapping.
226  */
227 struct pci_map {
228         void *addr;
229         char *path;
230         uint64_t offset;
231         uint64_t size;
232         uint64_t phaddr;
233 };
234
235 /**
236  * A structure describing a mapped PCI resource.
237  * For multi-process we need to reproduce all PCI mappings in secondary
238  * processes, so save them in a tailq.
239  */
240 struct mapped_pci_resource {
241         TAILQ_ENTRY(mapped_pci_resource) next;
242
243         struct rte_pci_addr pci_addr;
244         char path[PATH_MAX];
245         int nb_maps;
246         struct pci_map maps[PCI_MAX_RESOURCE];
247 };
248
249 /** mapped pci device list */
250 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
251
252 /**< Internal use only - Macro used by pci addr parsing functions **/
253 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
254 do {                                                               \
255         unsigned long val;                                      \
256         char *end;                                              \
257         errno = 0;                                              \
258         val = strtoul((in), &end, 16);                          \
259         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
260                 return -EINVAL;                                 \
261         (fd) = (typeof (fd))val;                                \
262         (in) = end + 1;                                         \
263 } while(0)
264
265 /**
266  * Utility function to produce a PCI Bus-Device-Function value
267  * given a string representation. Assumes that the BDF is provided without
268  * a domain prefix (i.e. domain returned is always 0)
269  *
270  * @param input
271  *      The input string to be parsed. Should have the format XX:XX.X
272  * @param dev_addr
273  *      The PCI Bus-Device-Function address to be returned. Domain will always be
274  *      returned as 0
275  * @return
276  *  0 on success, negative on error.
277  */
278 static inline int
279 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
280 {
281         dev_addr->domain = 0;
282         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
283         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
284         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
285         return 0;
286 }
287
288 /**
289  * Utility function to produce a PCI Bus-Device-Function value
290  * given a string representation. Assumes that the BDF is provided including
291  * a domain prefix.
292  *
293  * @param input
294  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
295  * @param dev_addr
296  *      The PCI Bus-Device-Function address to be returned
297  * @return
298  *  0 on success, negative on error.
299  */
300 static inline int
301 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
302 {
303         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
304         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
305         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
306         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
307         return 0;
308 }
309 #undef GET_PCIADDR_FIELD
310
311 /* Compare two PCI device addresses. */
312 /**
313  * Utility function to compare two PCI device addresses.
314  *
315  * @param addr
316  *      The PCI Bus-Device-Function address to compare
317  * @param addr2
318  *      The PCI Bus-Device-Function address to compare
319  * @return
320  *      0 on equal PCI address.
321  *      Positive on addr is greater than addr2.
322  *      Negative on addr is less than addr2, or error.
323  */
324 static inline int
325 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
326                          const struct rte_pci_addr *addr2)
327 {
328         uint64_t dev_addr, dev_addr2;
329
330         if ((addr == NULL) || (addr2 == NULL))
331                 return -1;
332
333         dev_addr = (addr->domain << 24) | (addr->bus << 16) |
334                                 (addr->devid << 8) | addr->function;
335         dev_addr2 = (addr2->domain << 24) | (addr2->bus << 16) |
336                                 (addr2->devid << 8) | addr2->function;
337
338         if (dev_addr > dev_addr2)
339                 return 1;
340         else if (dev_addr < dev_addr2)
341                 return -1;
342         else
343                 return 0;
344 }
345
346 /**
347  * Scan the content of the PCI bus, and the devices in the devices
348  * list
349  *
350  * @return
351  *  0 on success, negative on error
352  */
353 int rte_eal_pci_scan(void);
354
355 /**
356  * Probe the PCI bus for registered drivers.
357  *
358  * Scan the content of the PCI bus, and call the probe() function for
359  * all registered drivers that have a matching entry in its id_table
360  * for discovered devices.
361  *
362  * @return
363  *   - 0 on success.
364  *   - Negative on error.
365  */
366 int rte_eal_pci_probe(void);
367
368 /**
369  * Map the PCI device resources in user space virtual memory address
370  *
371  * Note that driver should not call this function when flag
372  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
373  * you when it's on.
374  *
375  * @param dev
376  *   A pointer to a rte_pci_device structure describing the device
377  *   to use
378  *
379  * @return
380  *   0 on success, negative on error and positive if no driver
381  *   is found for the device.
382  */
383 int rte_eal_pci_map_device(struct rte_pci_device *dev);
384
385 /**
386  * Unmap this device
387  *
388  * @param dev
389  *   A pointer to a rte_pci_device structure describing the device
390  *   to use
391  */
392 void rte_eal_pci_unmap_device(struct rte_pci_device *dev);
393
394 /**
395  * @internal
396  * Map a particular resource from a file.
397  *
398  * @param requested_addr
399  *      The starting address for the new mapping range.
400  * @param fd
401  *      The file descriptor.
402  * @param offset
403  *      The offset for the mapping range.
404  * @param size
405  *      The size for the mapping range.
406  * @param additional_flags
407  *      The additional flags for the mapping range.
408  * @return
409  *   - On success, the function returns a pointer to the mapped area.
410  *   - On error, the value MAP_FAILED is returned.
411  */
412 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
413                 size_t size, int additional_flags);
414
415 /**
416  * @internal
417  * Unmap a particular resource.
418  *
419  * @param requested_addr
420  *      The address for the unmapping range.
421  * @param size
422  *      The size for the unmapping range.
423  */
424 void pci_unmap_resource(void *requested_addr, size_t size);
425
426 /**
427  * Probe the single PCI device.
428  *
429  * Scan the content of the PCI bus, and find the pci device specified by pci
430  * address, then call the probe() function for registered driver that has a
431  * matching entry in its id_table for discovered device.
432  *
433  * @param addr
434  *      The PCI Bus-Device-Function address to probe.
435  * @return
436  *   - 0 on success.
437  *   - Negative on error.
438  */
439 int rte_eal_pci_probe_one(const struct rte_pci_addr *addr);
440
441 /**
442  * Close the single PCI device.
443  *
444  * Scan the content of the PCI bus, and find the pci device specified by pci
445  * address, then call the devuninit() function for registered driver that has a
446  * matching entry in its id_table for discovered device.
447  *
448  * @param addr
449  *      The PCI Bus-Device-Function address to close.
450  * @return
451  *   - 0 on success.
452  *   - Negative on error.
453  */
454 int rte_eal_pci_detach(const struct rte_pci_addr *addr);
455
456 /**
457  * Dump the content of the PCI bus.
458  *
459  * @param f
460  *   A pointer to a file for output
461  */
462 void rte_eal_pci_dump(FILE *f);
463
464 /**
465  * Register a PCI driver.
466  *
467  * @param driver
468  *   A pointer to a rte_pci_driver structure describing the driver
469  *   to be registered.
470  */
471 void rte_eal_pci_register(struct rte_pci_driver *driver);
472
473 /**
474  * Unregister a PCI driver.
475  *
476  * @param driver
477  *   A pointer to a rte_pci_driver structure describing the driver
478  *   to be unregistered.
479  */
480 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
481
482 /**
483  * Read PCI config space.
484  *
485  * @param device
486  *   A pointer to a rte_pci_device structure describing the device
487  *   to use
488  * @param buf
489  *   A data buffer where the bytes should be read into
490  * @param len
491  *   The length of the data buffer.
492  * @param offset
493  *   The offset into PCI config space
494  */
495 int rte_eal_pci_read_config(const struct rte_pci_device *device,
496                             void *buf, size_t len, off_t offset);
497
498 /**
499  * Write PCI config space.
500  *
501  * @param device
502  *   A pointer to a rte_pci_device structure describing the device
503  *   to use
504  * @param buf
505  *   A data buffer containing the bytes should be written
506  * @param len
507  *   The length of the data buffer.
508  * @param offset
509  *   The offset into PCI config space
510  */
511 int rte_eal_pci_write_config(const struct rte_pci_device *device,
512                              const void *buf, size_t len, off_t offset);
513
514 /**
515  * A structure used to access io resources for a pci device.
516  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
517  * of pci ioport api.
518  */
519 struct rte_pci_ioport {
520         struct rte_pci_device *dev;
521         uint64_t base;
522         uint64_t len; /* only filled for memory mapped ports */
523 };
524
525 /**
526  * Initialize a rte_pci_ioport object for a pci device io resource.
527  *
528  * This object is then used to gain access to those io resources (see below).
529  *
530  * @param dev
531  *   A pointer to a rte_pci_device structure describing the device
532  *   to use.
533  * @param bar
534  *   Index of the io pci resource we want to access.
535  * @param p
536  *   The rte_pci_ioport object to be initialized.
537  * @return
538  *  0 on success, negative on error.
539  */
540 int rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
541                            struct rte_pci_ioport *p);
542
543 /**
544  * Release any resources used in a rte_pci_ioport object.
545  *
546  * @param p
547  *   The rte_pci_ioport object to be uninitialized.
548  * @return
549  *  0 on success, negative on error.
550  */
551 int rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p);
552
553 /**
554  * Read from a io pci resource.
555  *
556  * @param p
557  *   The rte_pci_ioport object from which we want to read.
558  * @param data
559  *   A data buffer where the bytes should be read into
560  * @param len
561  *   The length of the data buffer.
562  * @param offset
563  *   The offset into the pci io resource.
564  */
565 void rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
566                              void *data, size_t len, off_t offset);
567
568 /**
569  * Write to a io pci resource.
570  *
571  * @param p
572  *   The rte_pci_ioport object to which we want to write.
573  * @param data
574  *   A data buffer where the bytes should be read into
575  * @param len
576  *   The length of the data buffer.
577  * @param offset
578  *   The offset into the pci io resource.
579  */
580 void rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
581                               const void *data, size_t len, off_t offset);
582
583 #ifdef __cplusplus
584 }
585 #endif
586
587 #endif /* _RTE_PCI_H_ */