bus/pci: use SPDX tags in 6WIND copyrighted files
[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;      /**< Associated driver */
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 };
70
71 /**
72  * @internal
73  * Helper macro for drivers that need to convert to struct rte_pci_device.
74  */
75 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
76
77 #define RTE_ETH_DEV_TO_PCI(eth_dev)     RTE_DEV_TO_PCI((eth_dev)->device)
78
79 /** Any PCI device identifier (vendor, device, ...) */
80 #define PCI_ANY_ID (0xffff)
81 #define RTE_CLASS_ANY_ID (0xffffff)
82
83 #ifdef __cplusplus
84 /** C++ macro used to help building up tables of device IDs */
85 #define RTE_PCI_DEVICE(vend, dev) \
86         RTE_CLASS_ANY_ID,         \
87         (vend),                   \
88         (dev),                    \
89         PCI_ANY_ID,               \
90         PCI_ANY_ID
91 #else
92 /** Macro used to help building up tables of device IDs */
93 #define RTE_PCI_DEVICE(vend, dev)          \
94         .class_id = RTE_CLASS_ANY_ID,      \
95         .vendor_id = (vend),               \
96         .device_id = (dev),                \
97         .subsystem_vendor_id = PCI_ANY_ID, \
98         .subsystem_device_id = PCI_ANY_ID
99 #endif
100
101 /**
102  * Initialisation function for the driver called during PCI probing.
103  */
104 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
105
106 /**
107  * Uninitialisation function for the driver called during hotplugging.
108  */
109 typedef int (pci_remove_t)(struct rte_pci_device *);
110
111 /**
112  * A structure describing a PCI driver.
113  */
114 struct rte_pci_driver {
115         TAILQ_ENTRY(rte_pci_driver) next;  /**< Next in list. */
116         struct rte_driver driver;          /**< Inherit core driver. */
117         struct rte_pci_bus *bus;           /**< PCI bus reference. */
118         pci_probe_t *probe;                /**< Device Probe function. */
119         pci_remove_t *remove;              /**< Device Remove function. */
120         const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
121         uint32_t drv_flags;                /**< Flags contolling handling of device. */
122 };
123
124 /**
125  * Structure describing the PCI bus
126  */
127 struct rte_pci_bus {
128         struct rte_bus bus;               /**< Inherit the generic class */
129         struct rte_pci_device_list device_list;  /**< List of PCI devices */
130         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
131 };
132
133 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
134 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
135 /** Device driver supports link state interrupt */
136 #define RTE_PCI_DRV_INTR_LSC    0x0008
137 /** Device driver supports device removal interrupt */
138 #define RTE_PCI_DRV_INTR_RMV 0x0010
139 /** Device driver needs to keep mapped resources if unsupported dev detected */
140 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
141 /** Device driver supports IOVA as VA */
142 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
143
144 /**
145  * Map the PCI device resources in user space virtual memory address
146  *
147  * Note that driver should not call this function when flag
148  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
149  * you when it's on.
150  *
151  * @param dev
152  *   A pointer to a rte_pci_device structure describing the device
153  *   to use
154  *
155  * @return
156  *   0 on success, negative on error and positive if no driver
157  *   is found for the device.
158  */
159 int rte_pci_map_device(struct rte_pci_device *dev);
160
161 /**
162  * Unmap this device
163  *
164  * @param dev
165  *   A pointer to a rte_pci_device structure describing the device
166  *   to use
167  */
168 void rte_pci_unmap_device(struct rte_pci_device *dev);
169
170 /**
171  * Dump the content of the PCI bus.
172  *
173  * @param f
174  *   A pointer to a file for output
175  */
176 void rte_pci_dump(FILE *f);
177
178 /**
179  * Register a PCI driver.
180  *
181  * @param driver
182  *   A pointer to a rte_pci_driver structure describing the driver
183  *   to be registered.
184  */
185 void rte_pci_register(struct rte_pci_driver *driver);
186
187 /** Helper for PCI device registration from driver (eth, crypto) instance */
188 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
189 RTE_INIT(pciinitfn_ ##nm); \
190 static void pciinitfn_ ##nm(void) \
191 {\
192         (pci_drv).driver.name = RTE_STR(nm);\
193         rte_pci_register(&pci_drv); \
194 } \
195 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
196
197 /**
198  * Unregister a PCI driver.
199  *
200  * @param driver
201  *   A pointer to a rte_pci_driver structure describing the driver
202  *   to be unregistered.
203  */
204 void rte_pci_unregister(struct rte_pci_driver *driver);
205
206 /**
207  * Read PCI config space.
208  *
209  * @param device
210  *   A pointer to a rte_pci_device structure describing the device
211  *   to use
212  * @param buf
213  *   A data buffer where the bytes should be read into
214  * @param len
215  *   The length of the data buffer.
216  * @param offset
217  *   The offset into PCI config space
218  */
219 int rte_pci_read_config(const struct rte_pci_device *device,
220                 void *buf, size_t len, off_t offset);
221
222 /**
223  * Write PCI config space.
224  *
225  * @param device
226  *   A pointer to a rte_pci_device structure describing the device
227  *   to use
228  * @param buf
229  *   A data buffer containing the bytes should be written
230  * @param len
231  *   The length of the data buffer.
232  * @param offset
233  *   The offset into PCI config space
234  */
235 int rte_pci_write_config(const struct rte_pci_device *device,
236                 const void *buf, size_t len, off_t offset);
237
238 /**
239  * A structure used to access io resources for a pci device.
240  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
241  * of pci ioport api.
242  */
243 struct rte_pci_ioport {
244         struct rte_pci_device *dev;
245         uint64_t base;
246         uint64_t len; /* only filled for memory mapped ports */
247 };
248
249 /**
250  * Initialize a rte_pci_ioport object for a pci device io resource.
251  *
252  * This object is then used to gain access to those io resources (see below).
253  *
254  * @param dev
255  *   A pointer to a rte_pci_device structure describing the device
256  *   to use.
257  * @param bar
258  *   Index of the io pci resource we want to access.
259  * @param p
260  *   The rte_pci_ioport object to be initialized.
261  * @return
262  *  0 on success, negative on error.
263  */
264 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
265                 struct rte_pci_ioport *p);
266
267 /**
268  * Release any resources used in a rte_pci_ioport object.
269  *
270  * @param p
271  *   The rte_pci_ioport object to be uninitialized.
272  * @return
273  *  0 on success, negative on error.
274  */
275 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
276
277 /**
278  * Read from a io pci resource.
279  *
280  * @param p
281  *   The rte_pci_ioport object from which we want to read.
282  * @param data
283  *   A data buffer where the bytes should be read into
284  * @param len
285  *   The length of the data buffer.
286  * @param offset
287  *   The offset into the pci io resource.
288  */
289 void rte_pci_ioport_read(struct rte_pci_ioport *p,
290                 void *data, size_t len, off_t offset);
291
292 /**
293  * Write to a io pci resource.
294  *
295  * @param p
296  *   The rte_pci_ioport object to which we want to write.
297  * @param data
298  *   A data buffer where the bytes should be read into
299  * @param len
300  *   The length of the data buffer.
301  * @param offset
302  *   The offset into the pci io resource.
303  */
304 void rte_pci_ioport_write(struct rte_pci_ioport *p,
305                 const void *data, size_t len, off_t offset);
306
307 #ifdef __cplusplus
308 }
309 #endif
310
311 #endif /* _RTE_BUS_PCI_H_ */