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