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