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