pci: make specialized parsing functions private
[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  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _RTE_PCI_H_
36 #define _RTE_PCI_H_
37
38 /**
39  * @file
40  *
41  * RTE PCI Interface
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <errno.h>
52 #include <sys/queue.h>
53 #include <stdint.h>
54 #include <inttypes.h>
55
56 #include <rte_debug.h>
57 #include <rte_interrupts.h>
58 #include <rte_dev.h>
59 #include <rte_bus.h>
60
61 /** Pathname of PCI devices directory. */
62 const char *pci_get_sysfs_path(void);
63
64 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
65 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
66 #define PCI_PRI_STR_SIZE sizeof("XXXXXXXX:XX:XX.X")
67
68 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
69 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
70
71 /** Nb. of values in PCI device identifier format string. */
72 #define PCI_FMT_NVAL 4
73
74 /** Nb. of values in PCI resource format. */
75 #define PCI_RESOURCE_FMT_NVAL 3
76
77 /** Maximum number of PCI resources. */
78 #define PCI_MAX_RESOURCE 6
79
80 /* Forward declarations */
81 struct rte_pci_device;
82 struct rte_pci_driver;
83
84 /** List of PCI devices */
85 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
86 /** List of PCI drivers */
87 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
88
89 /* PCI Bus iterators */
90 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
91                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
92
93 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
94                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
95
96 /**
97  * A structure describing an ID for a PCI driver. Each driver provides a
98  * table of these IDs for each device that it supports.
99  */
100 struct rte_pci_id {
101         uint32_t class_id;            /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
102         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
103         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
104         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
105         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
106 };
107
108 /**
109  * A structure describing the location of a PCI device.
110  */
111 struct rte_pci_addr {
112         uint32_t domain;                /**< Device domain */
113         uint8_t bus;                    /**< Device bus */
114         uint8_t devid;                  /**< Device ID */
115         uint8_t function;               /**< Device function. */
116 };
117
118 struct rte_devargs;
119
120 /**
121  * A structure describing a PCI device.
122  */
123 struct rte_pci_device {
124         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
125         struct rte_device device;               /**< Inherit core device */
126         struct rte_pci_addr addr;               /**< PCI location. */
127         struct rte_pci_id id;                   /**< PCI ID. */
128         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
129                                                 /**< PCI Memory Resource */
130         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
131         struct rte_pci_driver *driver;          /**< Associated driver */
132         uint16_t max_vfs;                       /**< sriov enable if not zero */
133         enum rte_kernel_driver kdrv;            /**< Kernel driver passthrough */
134         char name[PCI_PRI_STR_SIZE+1];          /**< PCI location (ASCII) */
135 };
136
137 /**
138  * @internal
139  * Helper macro for drivers that need to convert to struct rte_pci_device.
140  */
141 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
142
143 /** Any PCI device identifier (vendor, device, ...) */
144 #define PCI_ANY_ID (0xffff)
145 #define RTE_CLASS_ANY_ID (0xffffff)
146
147 #ifdef __cplusplus
148 /** C++ macro used to help building up tables of device IDs */
149 #define RTE_PCI_DEVICE(vend, dev) \
150         RTE_CLASS_ANY_ID,         \
151         (vend),                   \
152         (dev),                    \
153         PCI_ANY_ID,               \
154         PCI_ANY_ID
155 #else
156 /** Macro used to help building up tables of device IDs */
157 #define RTE_PCI_DEVICE(vend, dev)          \
158         .class_id = RTE_CLASS_ANY_ID,      \
159         .vendor_id = (vend),               \
160         .device_id = (dev),                \
161         .subsystem_vendor_id = PCI_ANY_ID, \
162         .subsystem_device_id = PCI_ANY_ID
163 #endif
164
165 /**
166  * Initialisation function for the driver called during PCI probing.
167  */
168 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
169
170 /**
171  * Uninitialisation function for the driver called during hotplugging.
172  */
173 typedef int (pci_remove_t)(struct rte_pci_device *);
174
175 /**
176  * A structure describing a PCI driver.
177  */
178 struct rte_pci_driver {
179         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
180         struct rte_driver driver;               /**< Inherit core driver. */
181         struct rte_pci_bus *bus;                /**< PCI bus reference. */
182         pci_probe_t *probe;                     /**< Device Probe function. */
183         pci_remove_t *remove;                   /**< Device Remove function. */
184         const struct rte_pci_id *id_table;      /**< ID table, NULL terminated. */
185         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
186 };
187
188 /**
189  * Structure describing the PCI bus
190  */
191 struct rte_pci_bus {
192         struct rte_bus bus;               /**< Inherit the generic class */
193         struct rte_pci_device_list device_list;  /**< List of PCI devices */
194         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
195 };
196
197 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
198 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
199 /** Device driver supports link state interrupt */
200 #define RTE_PCI_DRV_INTR_LSC    0x0008
201 /** Device driver supports device removal interrupt */
202 #define RTE_PCI_DRV_INTR_RMV 0x0010
203 /** Device driver needs to keep mapped resources if unsupported dev detected */
204 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
205 /** Device driver supports IOVA as VA */
206 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
207
208 /**
209  * A structure describing a PCI mapping.
210  */
211 struct pci_map {
212         void *addr;
213         char *path;
214         uint64_t offset;
215         uint64_t size;
216         uint64_t phaddr;
217 };
218
219 struct pci_msix_table {
220         int bar_index;
221         uint32_t offset;
222         uint32_t size;
223 };
224
225 /**
226  * A structure describing a mapped PCI resource.
227  * For multi-process we need to reproduce all PCI mappings in secondary
228  * processes, so save them in a tailq.
229  */
230 struct mapped_pci_resource {
231         TAILQ_ENTRY(mapped_pci_resource) next;
232
233         struct rte_pci_addr pci_addr;
234         char path[PATH_MAX];
235         int nb_maps;
236         struct pci_map maps[PCI_MAX_RESOURCE];
237         struct pci_msix_table msix_table;
238 };
239
240 /** mapped pci device list */
241 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
242
243 /**
244  * @deprecated
245  * Utility function to produce a PCI Bus-Device-Function value
246  * given a string representation. Assumes that the BDF is provided without
247  * a domain prefix (i.e. domain returned is always 0)
248  *
249  * @param input
250  *      The input string to be parsed. Should have the format XX:XX.X
251  * @param dev_addr
252  *      The PCI Bus-Device-Function address to be returned. Domain will always be
253  *      returned as 0
254  * @return
255  *  0 on success, negative on error.
256  */
257 int eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr);
258
259 /**
260  * @deprecated
261  * Utility function to produce a PCI Bus-Device-Function value
262  * given a string representation. Assumes that the BDF is provided including
263  * a domain prefix.
264  *
265  * @param input
266  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
267  * @param dev_addr
268  *      The PCI Bus-Device-Function address to be returned
269  * @return
270  *  0 on success, negative on error.
271  */
272 int eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr);
273
274 /**
275  * Utility function to write a pci device name, this device name can later be
276  * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
277  * BDF helpers.
278  *
279  * @param addr
280  *      The PCI Bus-Device-Function address
281  * @param output
282  *      The output buffer string
283  * @param size
284  *      The output buffer size
285  */
286 void rte_pci_device_name(const struct rte_pci_addr *addr, char *output,
287                          size_t size);
288
289 /**
290  * Utility function to compare two PCI device addresses.
291  *
292  * @param addr
293  *      The PCI Bus-Device-Function address to compare
294  * @param addr2
295  *      The PCI Bus-Device-Function address to compare
296  * @return
297  *      0 on equal PCI address.
298  *      Positive on addr is greater than addr2.
299  *      Negative on addr is less than addr2, or error.
300  */
301 int rte_pci_addr_cmp(const struct rte_pci_addr *addr,
302                      const struct rte_pci_addr *addr2);
303
304 /**
305  * @deprecated
306  * Utility function to compare two PCI device addresses.
307  *
308  * @param addr
309  *      The PCI Bus-Device-Function address to compare
310  * @param addr2
311  *      The PCI Bus-Device-Function address to compare
312  * @return
313  *      0 on equal PCI address.
314  *      Positive on addr is greater than addr2.
315  *      Negative on addr is less than addr2, or error.
316  */
317 int rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
318                              const struct rte_pci_addr *addr2);
319
320 /**
321  * Utility function to parse a string into a PCI location.
322  *
323  * @param str
324  *     The string to parse
325  * @param addr
326  *     The reference to the structure where the location
327  *     is stored.
328  * @return
329  *     0 on success
330  *     <0 otherwise
331  */
332 int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
333
334 /**
335  * Scan the content of the PCI bus, and the devices in the devices
336  * list
337  *
338  * @return
339  *  0 on success, negative on error
340  */
341 int rte_pci_scan(void);
342
343 /**
344  * Probe the PCI bus
345  *
346  * @return
347  *   - 0 on success.
348  *   - !0 on error.
349  */
350 int
351 rte_pci_probe(void);
352
353 /*
354  * Match the PCI Driver and Device using the ID Table
355  *
356  * @param pci_drv
357  *      PCI driver from which ID table would be extracted
358  * @param pci_dev
359  *      PCI device to match against the driver
360  * @return
361  *      1 for successful match
362  *      0 for unsuccessful match
363  */
364 int
365 rte_pci_match(const struct rte_pci_driver *pci_drv,
366               const struct rte_pci_device *pci_dev);
367
368
369 /**
370  * Get iommu class of PCI devices on the bus.
371  * And return their preferred iova mapping mode.
372  *
373  * @return
374  *   - enum rte_iova_mode.
375  */
376 enum rte_iova_mode
377 rte_pci_get_iommu_class(void);
378
379 /**
380  * Map the PCI device resources in user space virtual memory address
381  *
382  * Note that driver should not call this function when flag
383  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
384  * you when it's on.
385  *
386  * @param dev
387  *   A pointer to a rte_pci_device structure describing the device
388  *   to use
389  *
390  * @return
391  *   0 on success, negative on error and positive if no driver
392  *   is found for the device.
393  */
394 int rte_pci_map_device(struct rte_pci_device *dev);
395
396 /**
397  * Unmap this device
398  *
399  * @param dev
400  *   A pointer to a rte_pci_device structure describing the device
401  *   to use
402  */
403 void rte_pci_unmap_device(struct rte_pci_device *dev);
404
405 /**
406  * @internal
407  * Map a particular resource from a file.
408  *
409  * @param requested_addr
410  *      The starting address for the new mapping range.
411  * @param fd
412  *      The file descriptor.
413  * @param offset
414  *      The offset for the mapping range.
415  * @param size
416  *      The size for the mapping range.
417  * @param additional_flags
418  *      The additional flags for the mapping range.
419  * @return
420  *   - On success, the function returns a pointer to the mapped area.
421  *   - On error, the value MAP_FAILED is returned.
422  */
423 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
424                 size_t size, int additional_flags);
425
426 /**
427  * @internal
428  * Unmap a particular resource.
429  *
430  * @param requested_addr
431  *      The address for the unmapping range.
432  * @param size
433  *      The size for the unmapping range.
434  */
435 void pci_unmap_resource(void *requested_addr, size_t size);
436
437 /**
438  * Probe the single PCI device.
439  *
440  * Scan the content of the PCI bus, and find the pci device specified by pci
441  * address, then call the probe() function for registered driver that has a
442  * matching entry in its id_table for discovered device.
443  *
444  * @param addr
445  *      The PCI Bus-Device-Function address to probe.
446  * @return
447  *   - 0 on success.
448  *   - Negative on error.
449  */
450 int rte_pci_probe_one(const struct rte_pci_addr *addr);
451
452 /**
453  * Close the single PCI device.
454  *
455  * Scan the content of the PCI bus, and find the pci device specified by pci
456  * address, then call the remove() function for registered driver that has a
457  * matching entry in its id_table for discovered device.
458  *
459  * @param addr
460  *      The PCI Bus-Device-Function address to close.
461  * @return
462  *   - 0 on success.
463  *   - Negative on error.
464  */
465 int rte_pci_detach(const struct rte_pci_addr *addr);
466
467 /**
468  * Dump the content of the PCI bus.
469  *
470  * @param f
471  *   A pointer to a file for output
472  */
473 void rte_pci_dump(FILE *f);
474
475 /**
476  * Register a PCI driver.
477  *
478  * @param driver
479  *   A pointer to a rte_pci_driver structure describing the driver
480  *   to be registered.
481  */
482 void rte_pci_register(struct rte_pci_driver *driver);
483
484 /** Helper for PCI device registration from driver (eth, crypto) instance */
485 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
486 RTE_INIT(pciinitfn_ ##nm); \
487 static void pciinitfn_ ##nm(void) \
488 {\
489         (pci_drv).driver.name = RTE_STR(nm);\
490         rte_pci_register(&pci_drv); \
491 } \
492 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
493
494 /**
495  * Unregister a PCI driver.
496  *
497  * @param driver
498  *   A pointer to a rte_pci_driver structure describing the driver
499  *   to be unregistered.
500  */
501 void rte_pci_unregister(struct rte_pci_driver *driver);
502
503 /**
504  * Read PCI config space.
505  *
506  * @param device
507  *   A pointer to a rte_pci_device structure describing the device
508  *   to use
509  * @param buf
510  *   A data buffer where the bytes should be read into
511  * @param len
512  *   The length of the data buffer.
513  * @param offset
514  *   The offset into PCI config space
515  */
516 int rte_pci_read_config(const struct rte_pci_device *device,
517                 void *buf, size_t len, off_t offset);
518
519 /**
520  * Write PCI config space.
521  *
522  * @param device
523  *   A pointer to a rte_pci_device structure describing the device
524  *   to use
525  * @param buf
526  *   A data buffer containing the bytes should be written
527  * @param len
528  *   The length of the data buffer.
529  * @param offset
530  *   The offset into PCI config space
531  */
532 int rte_pci_write_config(const struct rte_pci_device *device,
533                 const void *buf, size_t len, off_t offset);
534
535 /**
536  * A structure used to access io resources for a pci device.
537  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
538  * of pci ioport api.
539  */
540 struct rte_pci_ioport {
541         struct rte_pci_device *dev;
542         uint64_t base;
543         uint64_t len; /* only filled for memory mapped ports */
544 };
545
546 /**
547  * Initialize a rte_pci_ioport object for a pci device io resource.
548  *
549  * This object is then used to gain access to those io resources (see below).
550  *
551  * @param dev
552  *   A pointer to a rte_pci_device structure describing the device
553  *   to use.
554  * @param bar
555  *   Index of the io pci resource we want to access.
556  * @param p
557  *   The rte_pci_ioport object to be initialized.
558  * @return
559  *  0 on success, negative on error.
560  */
561 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
562                 struct rte_pci_ioport *p);
563
564 /**
565  * Release any resources used in a rte_pci_ioport object.
566  *
567  * @param p
568  *   The rte_pci_ioport object to be uninitialized.
569  * @return
570  *  0 on success, negative on error.
571  */
572 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
573
574 /**
575  * Read from a io pci resource.
576  *
577  * @param p
578  *   The rte_pci_ioport object from which we want to read.
579  * @param data
580  *   A data buffer where the bytes should be read into
581  * @param len
582  *   The length of the data buffer.
583  * @param offset
584  *   The offset into the pci io resource.
585  */
586 void rte_pci_ioport_read(struct rte_pci_ioport *p,
587                 void *data, size_t len, off_t offset);
588
589 /**
590  * Write to a io pci resource.
591  *
592  * @param p
593  *   The rte_pci_ioport object to which we want to write.
594  * @param data
595  *   A data buffer where the bytes should be read into
596  * @param len
597  *   The length of the data buffer.
598  * @param offset
599  *   The offset into the pci io resource.
600  */
601 void rte_pci_ioport_write(struct rte_pci_ioport *p,
602                 const void *data, size_t len, off_t offset);
603
604 #ifdef __cplusplus
605 }
606 #endif
607
608 #endif /* _RTE_PCI_H_ */