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