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