test mbuf attach
[dpdk.git] / lib / librte_pci / rte_pci.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #ifndef _RTE_PCI_H_
7 #define _RTE_PCI_H_
8
9 /**
10  * @file
11  *
12  * RTE PCI Library
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdio.h>
20 #include <limits.h>
21 #include <sys/queue.h>
22 #include <inttypes.h>
23 #include <sys/types.h>
24
25 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
26 #define PCI_PRI_FMT "%.4" PRIx32 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
27 #define PCI_PRI_STR_SIZE sizeof("XXXXXXXX:XX:XX.X")
28
29 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
30 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
31
32 /** Nb. of values in PCI device identifier format string. */
33 #define PCI_FMT_NVAL 4
34
35 /** Nb. of values in PCI resource format. */
36 #define PCI_RESOURCE_FMT_NVAL 3
37
38 /** Maximum number of PCI resources. */
39 #define PCI_MAX_RESOURCE 6
40
41 /**
42  * A structure describing an ID for a PCI driver. Each driver provides a
43  * table of these IDs for each device that it supports.
44  */
45 struct rte_pci_id {
46         uint32_t class_id;            /**< Class ID or RTE_CLASS_ANY_ID. */
47         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
48         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
49         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
50         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
51 };
52
53 /**
54  * A structure describing the location of a PCI device.
55  */
56 struct rte_pci_addr {
57         uint32_t domain;                /**< Device domain */
58         uint8_t bus;                    /**< Device bus */
59         uint8_t devid;                  /**< Device ID */
60         uint8_t function;               /**< Device function. */
61 };
62
63 /** Any PCI device identifier (vendor, device, ...) */
64 #define PCI_ANY_ID (0xffff)
65 #define RTE_CLASS_ANY_ID (0xffffff)
66
67 /**
68  * A structure describing a PCI mapping.
69  */
70 struct pci_map {
71         void *addr;
72         char *path;
73         uint64_t offset;
74         uint64_t size;
75         uint64_t phaddr;
76 };
77
78 struct pci_msix_table {
79         int bar_index;
80         uint32_t offset;
81         uint32_t size;
82 };
83
84 /**
85  * A structure describing a mapped PCI resource.
86  * For multi-process we need to reproduce all PCI mappings in secondary
87  * processes, so save them in a tailq.
88  */
89 struct mapped_pci_resource {
90         TAILQ_ENTRY(mapped_pci_resource) next;
91
92         struct rte_pci_addr pci_addr;
93         char path[PATH_MAX];
94         int nb_maps;
95         struct pci_map maps[PCI_MAX_RESOURCE];
96         struct pci_msix_table msix_table;
97 };
98
99
100 /** mapped pci device list */
101 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
102
103 /**
104  * Utility function to write a pci device name, this device name can later be
105  * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
106  * BDF helpers.
107  *
108  * @param addr
109  *      The PCI Bus-Device-Function address
110  * @param output
111  *      The output buffer string
112  * @param size
113  *      The output buffer size
114  */
115 void rte_pci_device_name(const struct rte_pci_addr *addr,
116                      char *output, size_t size);
117
118 /**
119  * Utility function to compare two PCI device addresses.
120  *
121  * @param addr
122  *      The PCI Bus-Device-Function address to compare
123  * @param addr2
124  *      The PCI Bus-Device-Function address to compare
125  * @return
126  *      0 on equal PCI address.
127  *      Positive on addr is greater than addr2.
128  *      Negative on addr is less than addr2, or error.
129  */
130 int rte_pci_addr_cmp(const struct rte_pci_addr *addr,
131                      const struct rte_pci_addr *addr2);
132
133
134 /**
135  * Utility function to parse a string into a PCI location.
136  *
137  * @param str
138  *      The string to parse
139  * @param addr
140  *      The reference to the structure where the location
141  *      is stored.
142  * @return
143  *      0 on success
144  *      <0 otherwise
145  */
146 int rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr);
147
148 /**
149  * Map a particular resource from a file.
150  *
151  * @param requested_addr
152  *      The starting address for the new mapping range.
153  * @param fd
154  *      The file descriptor.
155  * @param offset
156  *      The offset for the mapping range.
157  * @param size
158  *      The size for the mapping range.
159  * @param additional_flags
160  *      The additional flags for the mapping range.
161  * @return
162  *   - On success, the function returns a pointer to the mapped area.
163  *   - On error, MAP_FAILED is returned.
164  */
165 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
166                 size_t size, int additional_flags);
167
168 /**
169  * Unmap a particular resource.
170  *
171  * @param requested_addr
172  *      The address for the unmapping range.
173  * @param size
174  *      The size for the unmapping range.
175  */
176 void pci_unmap_resource(void *requested_addr, size_t size);
177
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif /* _RTE_PCI_H_ */