d4a2996419146534cba74647147718bf53f2e5d0
[dpdk.git] / drivers / bus / pci / rte_bus_pci.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _RTE_BUS_PCI_H_
36 #define _RTE_BUS_PCI_H_
37
38 /**
39  * @file
40  *
41  * RTE PCI Bus Interface
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <errno.h>
52 #include <sys/queue.h>
53 #include <stdint.h>
54 #include <inttypes.h>
55
56 #include <rte_debug.h>
57 #include <rte_interrupts.h>
58 #include <rte_dev.h>
59 #include <rte_bus.h>
60 #include <rte_pci.h>
61
62 /** Pathname of PCI devices directory. */
63 const char *rte_pci_get_sysfs_path(void);
64
65 /* Forward declarations */
66 struct rte_pci_device;
67 struct rte_pci_driver;
68
69 /** List of PCI devices */
70 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
71 /** List of PCI drivers */
72 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
73
74 /* PCI Bus iterators */
75 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
76                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
77
78 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
79                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
80
81 struct rte_devargs;
82
83 /**
84  * A structure describing a PCI device.
85  */
86 struct rte_pci_device {
87         TAILQ_ENTRY(rte_pci_device) next;   /**< Next probed PCI device. */
88         struct rte_device device;           /**< Inherit core device */
89         struct rte_pci_addr addr;           /**< PCI location. */
90         struct rte_pci_id id;               /**< PCI ID. */
91         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
92                                             /**< PCI Memory Resource */
93         struct rte_intr_handle intr_handle; /**< Interrupt handle */
94         struct rte_pci_driver *driver;      /**< Associated driver */
95         uint16_t max_vfs;                   /**< sriov enable if not zero */
96         enum rte_kernel_driver kdrv;        /**< Kernel driver passthrough */
97         char name[PCI_PRI_STR_SIZE+1];      /**< PCI location (ASCII) */
98 };
99
100 /**
101  * @internal
102  * Helper macro for drivers that need to convert to struct rte_pci_device.
103  */
104 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
105
106 #define RTE_ETH_DEV_TO_PCI(eth_dev)     RTE_DEV_TO_PCI((eth_dev)->device)
107
108 /** Any PCI device identifier (vendor, device, ...) */
109 #define PCI_ANY_ID (0xffff)
110 #define RTE_CLASS_ANY_ID (0xffffff)
111
112 #ifdef __cplusplus
113 /** C++ macro used to help building up tables of device IDs */
114 #define RTE_PCI_DEVICE(vend, dev) \
115         RTE_CLASS_ANY_ID,         \
116         (vend),                   \
117         (dev),                    \
118         PCI_ANY_ID,               \
119         PCI_ANY_ID
120 #else
121 /** Macro used to help building up tables of device IDs */
122 #define RTE_PCI_DEVICE(vend, dev)          \
123         .class_id = RTE_CLASS_ANY_ID,      \
124         .vendor_id = (vend),               \
125         .device_id = (dev),                \
126         .subsystem_vendor_id = PCI_ANY_ID, \
127         .subsystem_device_id = PCI_ANY_ID
128 #endif
129
130 /**
131  * Initialisation function for the driver called during PCI probing.
132  */
133 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
134
135 /**
136  * Uninitialisation function for the driver called during hotplugging.
137  */
138 typedef int (pci_remove_t)(struct rte_pci_device *);
139
140 /**
141  * A structure describing a PCI driver.
142  */
143 struct rte_pci_driver {
144         TAILQ_ENTRY(rte_pci_driver) next;  /**< Next in list. */
145         struct rte_driver driver;          /**< Inherit core driver. */
146         struct rte_pci_bus *bus;           /**< PCI bus reference. */
147         pci_probe_t *probe;                /**< Device Probe function. */
148         pci_remove_t *remove;              /**< Device Remove function. */
149         const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
150         uint32_t drv_flags;                /**< Flags contolling handling of device. */
151 };
152
153 /**
154  * Structure describing the PCI bus
155  */
156 struct rte_pci_bus {
157         struct rte_bus bus;               /**< Inherit the generic class */
158         struct rte_pci_device_list device_list;  /**< List of PCI devices */
159         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
160 };
161
162 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
163 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
164 /** Device driver supports link state interrupt */
165 #define RTE_PCI_DRV_INTR_LSC    0x0008
166 /** Device driver supports device removal interrupt */
167 #define RTE_PCI_DRV_INTR_RMV 0x0010
168 /** Device driver needs to keep mapped resources if unsupported dev detected */
169 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
170 /** Device driver supports IOVA as VA */
171 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
172
173 /**
174  * Map the PCI device resources in user space virtual memory address
175  *
176  * Note that driver should not call this function when flag
177  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
178  * you when it's on.
179  *
180  * @param dev
181  *   A pointer to a rte_pci_device structure describing the device
182  *   to use
183  *
184  * @return
185  *   0 on success, negative on error and positive if no driver
186  *   is found for the device.
187  */
188 int rte_pci_map_device(struct rte_pci_device *dev);
189
190 /**
191  * Unmap this device
192  *
193  * @param dev
194  *   A pointer to a rte_pci_device structure describing the device
195  *   to use
196  */
197 void rte_pci_unmap_device(struct rte_pci_device *dev);
198
199 /**
200  * Dump the content of the PCI bus.
201  *
202  * @param f
203  *   A pointer to a file for output
204  */
205 void rte_pci_dump(FILE *f);
206
207 /**
208  * Register a PCI driver.
209  *
210  * @param driver
211  *   A pointer to a rte_pci_driver structure describing the driver
212  *   to be registered.
213  */
214 void rte_pci_register(struct rte_pci_driver *driver);
215
216 /** Helper for PCI device registration from driver (eth, crypto) instance */
217 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
218 RTE_INIT(pciinitfn_ ##nm); \
219 static void pciinitfn_ ##nm(void) \
220 {\
221         (pci_drv).driver.name = RTE_STR(nm);\
222         rte_pci_register(&pci_drv); \
223 } \
224 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
225
226 /**
227  * Unregister a PCI driver.
228  *
229  * @param driver
230  *   A pointer to a rte_pci_driver structure describing the driver
231  *   to be unregistered.
232  */
233 void rte_pci_unregister(struct rte_pci_driver *driver);
234
235 /**
236  * Read PCI config space.
237  *
238  * @param device
239  *   A pointer to a rte_pci_device structure describing the device
240  *   to use
241  * @param buf
242  *   A data buffer where the bytes should be read into
243  * @param len
244  *   The length of the data buffer.
245  * @param offset
246  *   The offset into PCI config space
247  */
248 int rte_pci_read_config(const struct rte_pci_device *device,
249                 void *buf, size_t len, off_t offset);
250
251 /**
252  * Write PCI config space.
253  *
254  * @param device
255  *   A pointer to a rte_pci_device structure describing the device
256  *   to use
257  * @param buf
258  *   A data buffer containing the bytes should be written
259  * @param len
260  *   The length of the data buffer.
261  * @param offset
262  *   The offset into PCI config space
263  */
264 int rte_pci_write_config(const struct rte_pci_device *device,
265                 const void *buf, size_t len, off_t offset);
266
267 /**
268  * A structure used to access io resources for a pci device.
269  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
270  * of pci ioport api.
271  */
272 struct rte_pci_ioport {
273         struct rte_pci_device *dev;
274         uint64_t base;
275         uint64_t len; /* only filled for memory mapped ports */
276 };
277
278 /**
279  * Initialize a rte_pci_ioport object for a pci device io resource.
280  *
281  * This object is then used to gain access to those io resources (see below).
282  *
283  * @param dev
284  *   A pointer to a rte_pci_device structure describing the device
285  *   to use.
286  * @param bar
287  *   Index of the io pci resource we want to access.
288  * @param p
289  *   The rte_pci_ioport object to be initialized.
290  * @return
291  *  0 on success, negative on error.
292  */
293 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
294                 struct rte_pci_ioport *p);
295
296 /**
297  * Release any resources used in a rte_pci_ioport object.
298  *
299  * @param p
300  *   The rte_pci_ioport object to be uninitialized.
301  * @return
302  *  0 on success, negative on error.
303  */
304 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
305
306 /**
307  * Read from a io pci resource.
308  *
309  * @param p
310  *   The rte_pci_ioport object from which we want to read.
311  * @param data
312  *   A data buffer where the bytes should be read into
313  * @param len
314  *   The length of the data buffer.
315  * @param offset
316  *   The offset into the pci io resource.
317  */
318 void rte_pci_ioport_read(struct rte_pci_ioport *p,
319                 void *data, size_t len, off_t offset);
320
321 /**
322  * Write to a io pci resource.
323  *
324  * @param p
325  *   The rte_pci_ioport object to which we want to write.
326  * @param data
327  *   A data buffer where the bytes should be read into
328  * @param len
329  *   The length of the data buffer.
330  * @param offset
331  *   The offset into the pci io resource.
332  */
333 void rte_pci_ioport_write(struct rte_pci_ioport *p,
334                 const void *data, size_t len, off_t offset);
335
336 #ifdef __cplusplus
337 }
338 #endif
339
340 #endif /* _RTE_BUS_PCI_H_ */