eal: allow probing a device again
[dpdk.git] / drivers / bus / pci / rte_bus_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_BUS_PCI_H_
7 #define _RTE_BUS_PCI_H_
8
9 /**
10  * @file
11  *
12  * RTE PCI Bus Interface
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <errno.h>
23 #include <sys/queue.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26
27 #include <rte_debug.h>
28 #include <rte_interrupts.h>
29 #include <rte_dev.h>
30 #include <rte_bus.h>
31 #include <rte_pci.h>
32
33 /** Pathname of PCI devices directory. */
34 const char *rte_pci_get_sysfs_path(void);
35
36 /* Forward declarations */
37 struct rte_pci_device;
38 struct rte_pci_driver;
39
40 /** List of PCI devices */
41 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
42 /** List of PCI drivers */
43 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
44
45 /* PCI Bus iterators */
46 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
47                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
48
49 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
50                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
51
52 struct rte_devargs;
53
54 /**
55  * A structure describing a PCI device.
56  */
57 struct rte_pci_device {
58         TAILQ_ENTRY(rte_pci_device) next;   /**< Next probed PCI device. */
59         struct rte_device device;           /**< Inherit core device */
60         struct rte_pci_addr addr;           /**< PCI location. */
61         struct rte_pci_id id;               /**< PCI ID. */
62         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
63                                             /**< PCI Memory Resource */
64         struct rte_intr_handle intr_handle; /**< Interrupt handle */
65         struct rte_pci_driver *driver;      /**< PCI driver used in probing */
66         uint16_t max_vfs;                   /**< sriov enable if not zero */
67         enum rte_kernel_driver kdrv;        /**< Kernel driver passthrough */
68         char name[PCI_PRI_STR_SIZE+1];      /**< PCI location (ASCII) */
69         struct rte_intr_handle vfio_req_intr_handle;
70                                 /**< Handler of VFIO request interrupt */
71 };
72
73 /**
74  * @internal
75  * Helper macro for drivers that need to convert to struct rte_pci_device.
76  */
77 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
78
79 #define RTE_DEV_TO_PCI_CONST(ptr) \
80         container_of(ptr, const struct rte_pci_device, device)
81
82 #define RTE_ETH_DEV_TO_PCI(eth_dev)     RTE_DEV_TO_PCI((eth_dev)->device)
83
84 /** Any PCI device identifier (vendor, device, ...) */
85 #define PCI_ANY_ID (0xffff)
86 #define RTE_CLASS_ANY_ID (0xffffff)
87
88 #ifdef __cplusplus
89 /** C++ macro used to help building up tables of device IDs */
90 #define RTE_PCI_DEVICE(vend, dev) \
91         RTE_CLASS_ANY_ID,         \
92         (vend),                   \
93         (dev),                    \
94         PCI_ANY_ID,               \
95         PCI_ANY_ID
96 #else
97 /** Macro used to help building up tables of device IDs */
98 #define RTE_PCI_DEVICE(vend, dev)          \
99         .class_id = RTE_CLASS_ANY_ID,      \
100         .vendor_id = (vend),               \
101         .device_id = (dev),                \
102         .subsystem_vendor_id = PCI_ANY_ID, \
103         .subsystem_device_id = PCI_ANY_ID
104 #endif
105
106 /**
107  * Initialisation function for the driver called during PCI probing.
108  */
109 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
110
111 /**
112  * Uninitialisation function for the driver called during hotplugging.
113  */
114 typedef int (pci_remove_t)(struct rte_pci_device *);
115
116 /**
117  * A structure describing a PCI driver.
118  */
119 struct rte_pci_driver {
120         TAILQ_ENTRY(rte_pci_driver) next;  /**< Next in list. */
121         struct rte_driver driver;          /**< Inherit core driver. */
122         struct rte_pci_bus *bus;           /**< PCI bus reference. */
123         pci_probe_t *probe;                /**< Device Probe function. */
124         pci_remove_t *remove;              /**< Device Remove function. */
125         const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
126         uint32_t drv_flags;                /**< Flags RTE_PCI_DRV_*. */
127 };
128
129 /**
130  * Structure describing the PCI bus
131  */
132 struct rte_pci_bus {
133         struct rte_bus bus;               /**< Inherit the generic class */
134         struct rte_pci_device_list device_list;  /**< List of PCI devices */
135         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
136 };
137
138 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
139 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
140 /** Device needs PCI BAR mapping with enabled write combining (wc) */
141 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
142 /** Device already probed can be probed again to check for new ports. */
143 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
144 /** Device driver supports link state interrupt */
145 #define RTE_PCI_DRV_INTR_LSC    0x0008
146 /** Device driver supports device removal interrupt */
147 #define RTE_PCI_DRV_INTR_RMV 0x0010
148 /** Device driver needs to keep mapped resources if unsupported dev detected */
149 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
150 /** Device driver supports IOVA as VA */
151 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
152
153 /**
154  * Map the PCI device resources in user space virtual memory address
155  *
156  * Note that driver should not call this function when flag
157  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
158  * you when it's on.
159  *
160  * @param dev
161  *   A pointer to a rte_pci_device structure describing the device
162  *   to use
163  *
164  * @return
165  *   0 on success, negative on error and positive if no driver
166  *   is found for the device.
167  */
168 int rte_pci_map_device(struct rte_pci_device *dev);
169
170 /**
171  * Unmap this device
172  *
173  * @param dev
174  *   A pointer to a rte_pci_device structure describing the device
175  *   to use
176  */
177 void rte_pci_unmap_device(struct rte_pci_device *dev);
178
179 /**
180  * Dump the content of the PCI bus.
181  *
182  * @param f
183  *   A pointer to a file for output
184  */
185 void rte_pci_dump(FILE *f);
186
187 /**
188  * Register a PCI driver.
189  *
190  * @param driver
191  *   A pointer to a rte_pci_driver structure describing the driver
192  *   to be registered.
193  */
194 void rte_pci_register(struct rte_pci_driver *driver);
195
196 /** Helper for PCI device registration from driver (eth, crypto) instance */
197 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
198 RTE_INIT(pciinitfn_ ##nm) \
199 {\
200         (pci_drv).driver.name = RTE_STR(nm);\
201         rte_pci_register(&pci_drv); \
202 } \
203 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
204
205 /**
206  * Unregister a PCI driver.
207  *
208  * @param driver
209  *   A pointer to a rte_pci_driver structure describing the driver
210  *   to be unregistered.
211  */
212 void rte_pci_unregister(struct rte_pci_driver *driver);
213
214 /**
215  * Read PCI config space.
216  *
217  * @param device
218  *   A pointer to a rte_pci_device structure describing the device
219  *   to use
220  * @param buf
221  *   A data buffer where the bytes should be read into
222  * @param len
223  *   The length of the data buffer.
224  * @param offset
225  *   The offset into PCI config space
226  */
227 int rte_pci_read_config(const struct rte_pci_device *device,
228                 void *buf, size_t len, off_t offset);
229
230 /**
231  * Write PCI config space.
232  *
233  * @param device
234  *   A pointer to a rte_pci_device structure describing the device
235  *   to use
236  * @param buf
237  *   A data buffer containing the bytes should be written
238  * @param len
239  *   The length of the data buffer.
240  * @param offset
241  *   The offset into PCI config space
242  */
243 int rte_pci_write_config(const struct rte_pci_device *device,
244                 const void *buf, size_t len, off_t offset);
245
246 /**
247  * A structure used to access io resources for a pci device.
248  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
249  * of pci ioport api.
250  */
251 struct rte_pci_ioport {
252         struct rte_pci_device *dev;
253         uint64_t base;
254         uint64_t len; /* only filled for memory mapped ports */
255 };
256
257 /**
258  * Initialize a rte_pci_ioport object for a pci device io resource.
259  *
260  * This object is then used to gain access to those io resources (see below).
261  *
262  * @param dev
263  *   A pointer to a rte_pci_device structure describing the device
264  *   to use.
265  * @param bar
266  *   Index of the io pci resource we want to access.
267  * @param p
268  *   The rte_pci_ioport object to be initialized.
269  * @return
270  *  0 on success, negative on error.
271  */
272 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
273                 struct rte_pci_ioport *p);
274
275 /**
276  * Release any resources used in a rte_pci_ioport object.
277  *
278  * @param p
279  *   The rte_pci_ioport object to be uninitialized.
280  * @return
281  *  0 on success, negative on error.
282  */
283 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
284
285 /**
286  * Read from a io pci resource.
287  *
288  * @param p
289  *   The rte_pci_ioport object from which we want to read.
290  * @param data
291  *   A data buffer where the bytes should be read into
292  * @param len
293  *   The length of the data buffer.
294  * @param offset
295  *   The offset into the pci io resource.
296  */
297 void rte_pci_ioport_read(struct rte_pci_ioport *p,
298                 void *data, size_t len, off_t offset);
299
300 /**
301  * Write to a io pci resource.
302  *
303  * @param p
304  *   The rte_pci_ioport object to which we want to write.
305  * @param data
306  *   A data buffer where the bytes should be read into
307  * @param len
308  *   The length of the data buffer.
309  * @param offset
310  *   The offset into the pci io resource.
311  */
312 void rte_pci_ioport_write(struct rte_pci_ioport *p,
313                 const void *data, size_t len, off_t offset);
314
315 #ifdef __cplusplus
316 }
317 #endif
318
319 #endif /* _RTE_BUS_PCI_H_ */