bus/vdev: add DMA mapping ops
[dpdk.git] / drivers / bus / vdev / rte_bus_vdev.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 RehiveTech. All rights reserved.
3  */
4
5 #ifndef RTE_VDEV_H
6 #define RTE_VDEV_H
7
8 /**
9  * @file
10  * RTE virtual bus API
11  *
12  */
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #include <sys/queue.h>
19 #include <rte_dev.h>
20 #include <rte_devargs.h>
21
22 struct rte_vdev_device {
23         TAILQ_ENTRY(rte_vdev_device) next;      /**< Next attached vdev */
24         struct rte_device device;               /**< Inherit core device */
25 };
26
27 /**
28  * @internal
29  * Helper macro for drivers that need to convert to struct rte_vdev_device.
30  */
31 #define RTE_DEV_TO_VDEV(ptr) \
32         container_of(ptr, struct rte_vdev_device, device)
33
34 #define RTE_DEV_TO_VDEV_CONST(ptr) \
35         container_of(ptr, const struct rte_vdev_device, device)
36
37 static inline const char *
38 rte_vdev_device_name(const struct rte_vdev_device *dev)
39 {
40         if (dev && dev->device.name)
41                 return dev->device.name;
42         return NULL;
43 }
44
45 static inline const char *
46 rte_vdev_device_args(const struct rte_vdev_device *dev)
47 {
48         if (dev && dev->device.devargs)
49                 return dev->device.devargs->args;
50         return "";
51 }
52
53 /** Double linked list of virtual device drivers. */
54 TAILQ_HEAD(vdev_driver_list, rte_vdev_driver);
55
56 /**
57  * Probe function called for each virtual device driver once.
58  */
59 typedef int (rte_vdev_probe_t)(struct rte_vdev_device *dev);
60
61 /**
62  * Remove function called for each virtual device driver once.
63  */
64 typedef int (rte_vdev_remove_t)(struct rte_vdev_device *dev);
65
66 /**
67  * Driver-specific DMA mapping. After a successful call the device
68  * will be able to read/write from/to this segment.
69  *
70  * @param dev
71  *   Pointer to the Virtual device.
72  * @param addr
73  *   Starting virtual address of memory to be mapped.
74  * @param iova
75  *   Starting IOVA address of memory to be mapped.
76  * @param len
77  *   Length of memory segment being mapped.
78  * @return
79  *   - 0 On success.
80  *   - Negative value and rte_errno is set otherwise.
81  */
82 typedef int (rte_vdev_dma_map_t)(struct rte_vdev_device *dev, void *addr,
83                             uint64_t iova, size_t len);
84
85 /**
86  * Driver-specific DMA un-mapping. After a successful call the device
87  * will not be able to read/write from/to this segment.
88  *
89  * @param dev
90  *   Pointer to the Virtual device.
91  * @param addr
92  *   Starting virtual address of memory to be unmapped.
93  * @param iova
94  *   Starting IOVA address of memory to be unmapped.
95  * @param len
96  *   Length of memory segment being unmapped.
97  * @return
98  *   - 0 On success.
99  *   - Negative value and rte_errno is set otherwise.
100  */
101 typedef int (rte_vdev_dma_unmap_t)(struct rte_vdev_device *dev, void *addr,
102                               uint64_t iova, size_t len);
103
104 /**
105  * A virtual device driver abstraction.
106  */
107 struct rte_vdev_driver {
108         TAILQ_ENTRY(rte_vdev_driver) next; /**< Next in list. */
109         struct rte_driver driver;        /**< Inherited general driver. */
110         rte_vdev_probe_t *probe;         /**< Virtual device probe function. */
111         rte_vdev_remove_t *remove;       /**< Virtual device remove function. */
112         rte_vdev_dma_map_t *dma_map;     /**< Virtual device DMA map function. */
113         rte_vdev_dma_unmap_t *dma_unmap; /**< Virtual device DMA unmap function. */
114 };
115
116 /**
117  * Register a virtual device driver.
118  *
119  * @param driver
120  *   A pointer to a rte_vdev_driver structure describing the driver
121  *   to be registered.
122  */
123 void rte_vdev_register(struct rte_vdev_driver *driver);
124
125 /**
126  * Unregister a virtual device driver.
127  *
128  * @param driver
129  *   A pointer to a rte_vdev_driver structure describing the driver
130  *   to be unregistered.
131  */
132 void rte_vdev_unregister(struct rte_vdev_driver *driver);
133
134 #define RTE_PMD_REGISTER_VDEV(nm, vdrv)\
135 static const char *vdrvinit_ ## nm ## _alias;\
136 RTE_INIT(vdrvinitfn_ ##vdrv)\
137 {\
138         (vdrv).driver.name = RTE_STR(nm);\
139         (vdrv).driver.alias = vdrvinit_ ## nm ## _alias;\
140         rte_vdev_register(&vdrv);\
141 } \
142 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
143
144 #define RTE_PMD_REGISTER_ALIAS(nm, alias)\
145 static const char *vdrvinit_ ## nm ## _alias = RTE_STR(alias)
146
147 typedef void (*rte_vdev_scan_callback)(void *user_arg);
148
149 /**
150  * Add a callback to be called on vdev scan
151  * before reading the devargs list.
152  *
153  * This function cannot be called in a scan callback
154  * because of deadlock.
155  *
156  * @param callback
157  *   The function to be called which can update the devargs list.
158  * @param user_arg
159  *   An opaque pointer passed to callback.
160  * @return
161  *   0 on success, negative on error
162  */
163 int
164 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg);
165
166 /**
167  * Remove a registered scan callback.
168  *
169  * This function cannot be called in a scan callback
170  * because of deadlock.
171  *
172  * @param callback
173  *   The registered function to be removed.
174  * @param user_arg
175  *   The associated opaque pointer or (void*)-1 for any.
176  * @return
177  *   0 on success
178  */
179 int
180 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg);
181
182 /**
183  * Initialize a driver specified by name.
184  *
185  * @param name
186  *   The pointer to a driver name to be initialized.
187  * @param args
188  *   The pointer to arguments used by driver initialization.
189  * @return
190  *  0 on success, negative on error
191  */
192 int rte_vdev_init(const char *name, const char *args);
193
194 /**
195  * Uninitalize a driver specified by name.
196  *
197  * @param name
198  *   The pointer to a driver name to be uninitialized.
199  * @return
200  *  0 on success, negative on error
201  */
202 int rte_vdev_uninit(const char *name);
203
204 #ifdef __cplusplus
205 }
206 #endif
207
208 #endif