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