16f997c7017a8d41ae214db653bf4517168d27c6
[dpdk.git] / drivers / bus / pci / private.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 6WIND S.A.
3  */
4
5 #ifndef _PCI_PRIVATE_H_
6 #define _PCI_PRIVATE_H_
7
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <rte_pci.h>
11 #include <rte_bus_pci.h>
12
13 extern struct rte_pci_bus rte_pci_bus;
14
15 struct rte_pci_driver;
16 struct rte_pci_device;
17
18 /**
19  * Scan the content of the PCI bus, and the devices in the devices
20  * list
21  *
22  * @return
23  *  0 on success, negative on error
24  */
25 int rte_pci_scan(void);
26
27 /**
28  * Find the name of a PCI device.
29  */
30 void
31 pci_name_set(struct rte_pci_device *dev);
32
33 /**
34  * Validate whether a device with given PCI address should be ignored or not.
35  *
36  * @param pci_addr
37  *      PCI address of device to be validated
38  * @return
39  *      true: if device is to be ignored,
40  *      false: if device is to be scanned,
41  */
42 bool rte_pci_ignore_device(const struct rte_pci_addr *pci_addr);
43
44 /**
45  * Add a PCI device to the PCI Bus (append to PCI Device list). This function
46  * also updates the bus references of the PCI Device (and the generic device
47  * object embedded within.
48  *
49  * @param pci_dev
50  *      PCI device to add
51  * @return void
52  */
53 void rte_pci_add_device(struct rte_pci_device *pci_dev);
54
55 /**
56  * Insert a PCI device in the PCI Bus at a particular location in the device
57  * list. It also updates the PCI Bus reference of the new devices to be
58  * inserted.
59  *
60  * @param exist_pci_dev
61  *      Existing PCI device in PCI Bus
62  * @param new_pci_dev
63  *      PCI device to be added before exist_pci_dev
64  * @return void
65  */
66 void rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
67                 struct rte_pci_device *new_pci_dev);
68
69 /**
70  * Update a pci device object by asking the kernel for the latest information.
71  *
72  * This function is private to EAL.
73  *
74  * @param addr
75  *      The PCI Bus-Device-Function address to look for
76  * @return
77  *   - 0 on success.
78  *   - negative on error.
79  */
80 int pci_update_device(const struct rte_pci_addr *addr);
81
82 /**
83  * A structure describing a PCI mapping.
84  */
85 struct pci_map {
86         void *addr;
87         char *path;
88         uint64_t offset;
89         uint64_t size;
90         uint64_t phaddr;
91 };
92
93 struct pci_msix_table {
94         int bar_index;
95         uint32_t offset;
96         uint32_t size;
97 };
98
99 /**
100  * A structure describing a mapped PCI resource.
101  * For multi-process we need to reproduce all PCI mappings in secondary
102  * processes, so save them in a tailq.
103  */
104 struct mapped_pci_resource {
105         TAILQ_ENTRY(mapped_pci_resource) next;
106
107         struct rte_pci_addr pci_addr;
108         char path[PATH_MAX];
109         int nb_maps;
110         struct pci_map maps[PCI_MAX_RESOURCE];
111         struct pci_msix_table msix_table;
112 };
113
114 /** mapped pci device list */
115 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
116
117 /**
118  * Map a particular resource from a file.
119  *
120  * @param requested_addr
121  *      The starting address for the new mapping range.
122  * @param fd
123  *      The file descriptor.
124  * @param offset
125  *      The offset for the mapping range.
126  * @param size
127  *      The size for the mapping range.
128  * @param additional_flags
129  *      The additional rte_mem_map() flags for the mapping range.
130  * @return
131  *   - On success, the function returns a pointer to the mapped area.
132  *   - On error, NULL is returned.
133  */
134 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
135                 size_t size, int additional_flags);
136
137 /**
138  * Unmap a particular resource.
139  *
140  * @param requested_addr
141  *      The address for the unmapping range.
142  * @param size
143  *      The size for the unmapping range.
144  */
145 void pci_unmap_resource(void *requested_addr, size_t size);
146
147 /**
148  * Map the PCI resource of a PCI device in virtual memory
149  *
150  * This function is private to EAL.
151  *
152  * @return
153  *   0 on success, negative on error
154  */
155 int pci_uio_map_resource(struct rte_pci_device *dev);
156
157 /**
158  * Unmap the PCI resource of a PCI device
159  *
160  * This function is private to EAL.
161  */
162 void pci_uio_unmap_resource(struct rte_pci_device *dev);
163
164 /**
165  * Allocate uio resource for PCI device
166  *
167  * This function is private to EAL.
168  *
169  * @param dev
170  *   PCI device to allocate uio resource
171  * @param uio_res
172  *   Pointer to uio resource.
173  *   If the function returns 0, the pointer will be filled.
174  * @return
175  *   0 on success, negative on error
176  */
177 int pci_uio_alloc_resource(struct rte_pci_device *dev,
178                 struct mapped_pci_resource **uio_res);
179
180 /**
181  * Free uio resource for PCI device
182  *
183  * This function is private to EAL.
184  *
185  * @param dev
186  *   PCI device to free uio resource
187  * @param uio_res
188  *   Pointer to uio resource.
189  */
190 void pci_uio_free_resource(struct rte_pci_device *dev,
191                 struct mapped_pci_resource *uio_res);
192
193 /**
194  * Remap the PCI resource of a PCI device in anonymous virtual memory.
195  *
196  * @param dev
197  *   Point to the struct rte pci device.
198  * @return
199  *   - On success, zero.
200  *   - On failure, a negative value.
201  */
202 int
203 pci_uio_remap_resource(struct rte_pci_device *dev);
204
205 /**
206  * Map device memory to uio resource
207  *
208  * This function is private to EAL.
209  *
210  * @param dev
211  *   PCI device that has memory information.
212  * @param res_idx
213  *   Memory resource index of the PCI device.
214  * @param uio_res
215  *  uio resource that will keep mapping information.
216  * @param map_idx
217  *   Mapping information index of the uio resource.
218  * @return
219  *   0 on success, negative on error
220  */
221 int pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
222                 struct mapped_pci_resource *uio_res, int map_idx);
223
224 /*
225  * Match the PCI Driver and Device using the ID Table
226  *
227  * @param pci_drv
228  *      PCI driver from which ID table would be extracted
229  * @param pci_dev
230  *      PCI device to match against the driver
231  * @return
232  *      1 for successful match
233  *      0 for unsuccessful match
234  */
235 int
236 rte_pci_match(const struct rte_pci_driver *pci_drv,
237               const struct rte_pci_device *pci_dev);
238
239 /**
240  * OS specific callbacks for rte_pci_get_iommu_class
241  *
242  */
243 bool
244 pci_device_iommu_support_va(const struct rte_pci_device *dev);
245
246 enum rte_iova_mode
247 pci_device_iova_mode(const struct rte_pci_driver *pci_drv,
248                      const struct rte_pci_device *pci_dev);
249
250 /**
251  * Get iommu class of PCI devices on the bus.
252  * And return their preferred iova mapping mode.
253  *
254  * @return
255  *   - enum rte_iova_mode.
256  */
257 enum rte_iova_mode
258 rte_pci_get_iommu_class(void);
259
260 /*
261  * Iterate over internal devices,
262  * matching any device against the provided
263  * string.
264  *
265  * @param start
266  *   Iteration starting point.
267  *
268  * @param str
269  *   Device string to match against.
270  *
271  * @param it
272  *   (unused) iterator structure.
273  *
274  * @return
275  *   A pointer to the next matching device if any.
276  *   NULL otherwise.
277  */
278 void *
279 rte_pci_dev_iterate(const void *start,
280                     const char *str,
281                     const struct rte_dev_iterator *it);
282
283 #endif /* _PCI_PRIVATE_H_ */