876abddefbcd930fd3740aedb873316d5f4c56e9
[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 enum rte_pci_kernel_driver {
55         RTE_PCI_KDRV_UNKNOWN = 0,  /* may be misc UIO or bifurcated driver */
56         RTE_PCI_KDRV_IGB_UIO,      /* igb_uio for Linux */
57         RTE_PCI_KDRV_VFIO,         /* VFIO for Linux */
58         RTE_PCI_KDRV_UIO_GENERIC,  /* uio_pci_generic for Linux */
59         RTE_PCI_KDRV_NIC_UIO,      /* nic_uio for FreeBSD */
60         RTE_PCI_KDRV_NONE,         /* no attached driver */
61         RTE_PCI_KDRV_NET_UIO,      /* NetUIO for Windows */
62 };
63
64 /**
65  * A structure describing a PCI device.
66  */
67 struct rte_pci_device {
68         TAILQ_ENTRY(rte_pci_device) next;   /**< Next probed PCI device. */
69         struct rte_device device;           /**< Inherit core device */
70         struct rte_pci_addr addr;           /**< PCI location. */
71         struct rte_pci_id id;               /**< PCI ID. */
72         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
73                                             /**< PCI Memory Resource */
74         struct rte_intr_handle intr_handle; /**< Interrupt handle */
75         struct rte_pci_driver *driver;      /**< PCI driver used in probing */
76         uint16_t max_vfs;                   /**< sriov enable if not zero */
77         enum rte_pci_kernel_driver kdrv;    /**< Kernel driver passthrough */
78         char name[PCI_PRI_STR_SIZE+1];      /**< PCI location (ASCII) */
79         struct rte_intr_handle vfio_req_intr_handle;
80                                 /**< Handler of VFIO request interrupt */
81 };
82
83 /**
84  * @internal
85  * Helper macro for drivers that need to convert to struct rte_pci_device.
86  */
87 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
88
89 #define RTE_DEV_TO_PCI_CONST(ptr) \
90         container_of(ptr, const struct rte_pci_device, device)
91
92 #define RTE_ETH_DEV_TO_PCI(eth_dev)     RTE_DEV_TO_PCI((eth_dev)->device)
93
94 /** Any PCI device identifier (vendor, device, ...) */
95 #define PCI_ANY_ID (0xffff)
96 #define RTE_CLASS_ANY_ID (0xffffff)
97
98 #ifdef __cplusplus
99 /** C++ macro used to help building up tables of device IDs */
100 #define RTE_PCI_DEVICE(vend, dev) \
101         RTE_CLASS_ANY_ID,         \
102         (vend),                   \
103         (dev),                    \
104         PCI_ANY_ID,               \
105         PCI_ANY_ID
106 #else
107 /** Macro used to help building up tables of device IDs */
108 #define RTE_PCI_DEVICE(vend, dev)          \
109         .class_id = RTE_CLASS_ANY_ID,      \
110         .vendor_id = (vend),               \
111         .device_id = (dev),                \
112         .subsystem_vendor_id = PCI_ANY_ID, \
113         .subsystem_device_id = PCI_ANY_ID
114 #endif
115
116 /**
117  * Initialisation function for the driver called during PCI probing.
118  */
119 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
120
121 /**
122  * Uninitialisation function for the driver called during hotplugging.
123  */
124 typedef int (pci_remove_t)(struct rte_pci_device *);
125
126 /**
127  * Driver-specific DMA mapping. After a successful call the device
128  * will be able to read/write from/to this segment.
129  *
130  * @param dev
131  *   Pointer to the PCI device.
132  * @param addr
133  *   Starting virtual address of memory to be mapped.
134  * @param iova
135  *   Starting IOVA address of memory to be mapped.
136  * @param len
137  *   Length of memory segment being mapped.
138  * @return
139  *   - 0 On success.
140  *   - Negative value and rte_errno is set otherwise.
141  */
142 typedef int (pci_dma_map_t)(struct rte_pci_device *dev, void *addr,
143                             uint64_t iova, size_t len);
144
145 /**
146  * Driver-specific DMA un-mapping. After a successful call the device
147  * will not be able to read/write from/to this segment.
148  *
149  * @param dev
150  *   Pointer to the PCI device.
151  * @param addr
152  *   Starting virtual address of memory to be unmapped.
153  * @param iova
154  *   Starting IOVA address of memory to be unmapped.
155  * @param len
156  *   Length of memory segment being unmapped.
157  * @return
158  *   - 0 On success.
159  *   - Negative value and rte_errno is set otherwise.
160  */
161 typedef int (pci_dma_unmap_t)(struct rte_pci_device *dev, void *addr,
162                               uint64_t iova, size_t len);
163
164 /**
165  * A structure describing a PCI driver.
166  */
167 struct rte_pci_driver {
168         TAILQ_ENTRY(rte_pci_driver) next;  /**< Next in list. */
169         struct rte_driver driver;          /**< Inherit core driver. */
170         struct rte_pci_bus *bus;           /**< PCI bus reference. */
171         pci_probe_t *probe;                /**< Device Probe function. */
172         pci_remove_t *remove;              /**< Device Remove function. */
173         pci_dma_map_t *dma_map;            /**< device dma map function. */
174         pci_dma_unmap_t *dma_unmap;        /**< device dma unmap function. */
175         const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
176         uint32_t drv_flags;                /**< Flags RTE_PCI_DRV_*. */
177 };
178
179 /**
180  * Structure describing the PCI bus
181  */
182 struct rte_pci_bus {
183         struct rte_bus bus;               /**< Inherit the generic class */
184         struct rte_pci_device_list device_list;  /**< List of PCI devices */
185         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
186 };
187
188 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
189 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
190 /** Device needs PCI BAR mapping with enabled write combining (wc) */
191 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
192 /** Device already probed can be probed again to check for new ports. */
193 #define RTE_PCI_DRV_PROBE_AGAIN 0x0004
194 /** Device driver supports link state interrupt */
195 #define RTE_PCI_DRV_INTR_LSC    0x0008
196 /** Device driver supports device removal interrupt */
197 #define RTE_PCI_DRV_INTR_RMV 0x0010
198 /** Device driver needs to keep mapped resources if unsupported dev detected */
199 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
200 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */
201 #define RTE_PCI_DRV_NEED_IOVA_AS_VA 0x0040
202
203 /**
204  * Map the PCI device resources in user space virtual memory address
205  *
206  * Note that driver should not call this function when flag
207  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
208  * you when it's on.
209  *
210  * @param dev
211  *   A pointer to a rte_pci_device structure describing the device
212  *   to use
213  *
214  * @return
215  *   0 on success, negative on error and positive if no driver
216  *   is found for the device.
217  */
218 int rte_pci_map_device(struct rte_pci_device *dev);
219
220 /**
221  * Unmap this device
222  *
223  * @param dev
224  *   A pointer to a rte_pci_device structure describing the device
225  *   to use
226  */
227 void rte_pci_unmap_device(struct rte_pci_device *dev);
228
229 /**
230  * Dump the content of the PCI bus.
231  *
232  * @param f
233  *   A pointer to a file for output
234  */
235 void rte_pci_dump(FILE *f);
236
237 /**
238  * Find device's extended PCI capability.
239  *
240  *  @param dev
241  *    A pointer to rte_pci_device structure.
242  *
243  *  @param cap
244  *    Extended capability to be found, which can be any from
245  *    RTE_PCI_EXT_CAP_ID_*, defined in librte_pci.
246  *
247  *  @return
248  *  > 0: The offset of the next matching extended capability structure
249  *       within the device's PCI configuration space.
250  *  < 0: An error in PCI config space read.
251  *  = 0: Device does not support it.
252  */
253 __rte_experimental
254 off_t rte_pci_find_ext_capability(struct rte_pci_device *dev, uint32_t cap);
255
256 /**
257  * Register a PCI driver.
258  *
259  * @param driver
260  *   A pointer to a rte_pci_driver structure describing the driver
261  *   to be registered.
262  */
263 void rte_pci_register(struct rte_pci_driver *driver);
264
265 /** Helper for PCI device registration from driver (eth, crypto) instance */
266 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
267 RTE_INIT(pciinitfn_ ##nm) \
268 {\
269         (pci_drv).driver.name = RTE_STR(nm);\
270         rte_pci_register(&pci_drv); \
271 } \
272 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
273
274 /**
275  * Unregister a PCI driver.
276  *
277  * @param driver
278  *   A pointer to a rte_pci_driver structure describing the driver
279  *   to be unregistered.
280  */
281 void rte_pci_unregister(struct rte_pci_driver *driver);
282
283 /**
284  * Read PCI config space.
285  *
286  * @param device
287  *   A pointer to a rte_pci_device structure describing the device
288  *   to use
289  * @param buf
290  *   A data buffer where the bytes should be read into
291  * @param len
292  *   The length of the data buffer.
293  * @param offset
294  *   The offset into PCI config space
295  * @return
296  *  Number of bytes read on success, negative on error.
297  */
298 int rte_pci_read_config(const struct rte_pci_device *device,
299                 void *buf, size_t len, off_t offset);
300
301 /**
302  * Write PCI config space.
303  *
304  * @param device
305  *   A pointer to a rte_pci_device structure describing the device
306  *   to use
307  * @param buf
308  *   A data buffer containing the bytes should be written
309  * @param len
310  *   The length of the data buffer.
311  * @param offset
312  *   The offset into PCI config space
313  */
314 int rte_pci_write_config(const struct rte_pci_device *device,
315                 const void *buf, size_t len, off_t offset);
316
317 /**
318  * A structure used to access io resources for a pci device.
319  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
320  * of pci ioport api.
321  */
322 struct rte_pci_ioport {
323         struct rte_pci_device *dev;
324         uint64_t base;
325         uint64_t len; /* only filled for memory mapped ports */
326 };
327
328 /**
329  * Initialize a rte_pci_ioport object for a pci device io resource.
330  *
331  * This object is then used to gain access to those io resources (see below).
332  *
333  * @param dev
334  *   A pointer to a rte_pci_device structure describing the device
335  *   to use.
336  * @param bar
337  *   Index of the io pci resource we want to access.
338  * @param p
339  *   The rte_pci_ioport object to be initialized.
340  * @return
341  *  0 on success, negative on error.
342  */
343 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
344                 struct rte_pci_ioport *p);
345
346 /**
347  * Release any resources used in a rte_pci_ioport object.
348  *
349  * @param p
350  *   The rte_pci_ioport object to be uninitialized.
351  * @return
352  *  0 on success, negative on error.
353  */
354 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
355
356 /**
357  * Read from a io pci resource.
358  *
359  * @param p
360  *   The rte_pci_ioport object from which we want to read.
361  * @param data
362  *   A data buffer where the bytes should be read into
363  * @param len
364  *   The length of the data buffer.
365  * @param offset
366  *   The offset into the pci io resource.
367  */
368 void rte_pci_ioport_read(struct rte_pci_ioport *p,
369                 void *data, size_t len, off_t offset);
370
371 /**
372  * Write to a io pci resource.
373  *
374  * @param p
375  *   The rte_pci_ioport object to which we want to write.
376  * @param data
377  *   A data buffer where the bytes should be read into
378  * @param len
379  *   The length of the data buffer.
380  * @param offset
381  *   The offset into the pci io resource.
382  */
383 void rte_pci_ioport_write(struct rte_pci_ioport *p,
384                 const void *data, size_t len, off_t offset);
385
386 #ifdef __cplusplus
387 }
388 #endif
389
390 #endif /* _RTE_BUS_PCI_H_ */