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