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