1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_H
3 #define _LINUX_VIRTIO_H
4 /* Everything a virtio driver needs to work with any particular virtio
5 * implementation. */
6 #include <linux/types.h>
7 #include <linux/scatterlist.h>
8 #include <linux/spinlock.h>
9 #include <linux/device.h>
10 #include <linux/device-id/virtio.h>
11 #include <linux/gfp.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/completion.h>
14 #include <linux/virtio_features.h>
15
16 struct module;
17
18 /**
19 * struct virtqueue - a queue to register buffers for sending or receiving.
20 * @list: the chain of virtqueues for this device
21 * @callback: the function to call when buffers are consumed (can be NULL).
22 * @name: the name of this virtqueue (mainly for debugging)
23 * @vdev: the virtio device this queue was created for.
24 * @priv: a pointer for the virtqueue implementation to use.
25 * @index: the zero-based ordinal number for this queue.
26 * @num_free: number of elements we expect to be able to fit.
27 * @num_max: the maximum number of elements supported by the device.
28 * @reset: vq is in reset state or not.
29 *
30 * A note on @num_free: with indirect buffers, each buffer needs one
31 * element in the queue, otherwise a buffer will need one element per
32 * sg element.
33 */
34 struct virtqueue {
35 struct list_head list;
36 void (*callback)(struct virtqueue *vq);
37 const char *name;
38 struct virtio_device *vdev;
39 unsigned int index;
40 unsigned int num_free;
41 unsigned int num_max;
42 bool reset;
43 void *priv;
44 };
45
46 struct vduse_vq_group;
47
48 union virtio_map {
49 /* Device that performs DMA */
50 struct device *dma_dev;
51 /* VDUSE specific virtqueue group for doing map */
52 struct vduse_vq_group *group;
53 };
54
55 int virtqueue_add_outbuf(struct virtqueue *vq,
56 struct scatterlist sg[], unsigned int num,
57 void *data,
58 gfp_t gfp);
59
60 int virtqueue_add_inbuf(struct virtqueue *vq,
61 struct scatterlist sg[], unsigned int num,
62 void *data,
63 gfp_t gfp);
64
65 int virtqueue_add_inbuf_cache_clean(struct virtqueue *vq,
66 struct scatterlist sg[], unsigned int num,
67 void *data,
68 gfp_t gfp);
69
70 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
71 struct scatterlist sg[], unsigned int num,
72 void *data,
73 void *ctx,
74 gfp_t gfp);
75
76 int virtqueue_add_inbuf_premapped(struct virtqueue *vq,
77 struct scatterlist *sg, unsigned int num,
78 void *data,
79 void *ctx,
80 gfp_t gfp);
81
82 int virtqueue_add_outbuf_premapped(struct virtqueue *vq,
83 struct scatterlist *sg, unsigned int num,
84 void *data,
85 gfp_t gfp);
86
87 int virtqueue_add_sgs(struct virtqueue *vq,
88 struct scatterlist *sgs[],
89 unsigned int out_sgs,
90 unsigned int in_sgs,
91 void *data,
92 gfp_t gfp);
93
94 struct device *virtqueue_dma_dev(struct virtqueue *vq);
95
96 bool virtqueue_kick(struct virtqueue *vq);
97
98 bool virtqueue_kick_prepare(struct virtqueue *vq);
99
100 bool virtqueue_notify(struct virtqueue *vq);
101
102 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
103
104 void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
105 void **ctx);
106
107 void virtqueue_disable_cb(struct virtqueue *vq);
108
109 bool virtqueue_enable_cb(struct virtqueue *vq);
110
111 unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
112
113 bool virtqueue_poll(struct virtqueue *vq, unsigned);
114
115 bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
116
117 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
118
119 unsigned int virtqueue_get_vring_size(const struct virtqueue *vq);
120
121 bool virtqueue_is_broken(const struct virtqueue *vq);
122
123 const struct vring *virtqueue_get_vring(const struct virtqueue *vq);
124 dma_addr_t virtqueue_get_desc_addr(const struct virtqueue *vq);
125 dma_addr_t virtqueue_get_avail_addr(const struct virtqueue *vq);
126 dma_addr_t virtqueue_get_used_addr(const struct virtqueue *vq);
127
128 int virtqueue_resize(struct virtqueue *vq, u32 num,
129 void (*recycle)(struct virtqueue *vq, void *buf),
130 void (*recycle_done)(struct virtqueue *vq));
131 int virtqueue_reset(struct virtqueue *vq,
132 void (*recycle)(struct virtqueue *vq, void *buf),
133 void (*recycle_done)(struct virtqueue *vq));
134
135 struct virtio_admin_cmd {
136 __le16 opcode;
137 __le16 group_type;
138 __le64 group_member_id;
139 struct scatterlist *data_sg;
140 struct scatterlist *result_sg;
141 struct completion completion;
142 u32 result_sg_size;
143 int ret;
144 };
145
146 /**
147 * struct virtio_device - representation of a device using virtio
148 * @index: unique position on the virtio bus
149 * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
150 * @config_core_enabled: configuration change reporting enabled by core
151 * @config_driver_disabled: configuration change reporting disabled by
152 * a driver
153 * @config_change_pending: configuration change reported while disabled
154 * @config_lock: protects configuration change reporting
155 * @vqs_list_lock: protects @vqs.
156 * @dev: underlying device.
157 * @id: the device type identification (used to match it with a driver).
158 * @config: the configuration ops for this device.
159 * @vringh_config: configuration ops for host vrings.
160 * @map: the map operations for mapping virtio device memory.
161 * @vqs: the list of virtqueues for this device.
162 * @features: the 64 lower features supported by both driver and device.
163 * @features_array: the full features space supported by both driver and
164 * device.
165 * @priv: private pointer for the driver's use.
166 * @vmap: the map container with transport- or device-specific metadata.
167 * @debugfs_dir: debugfs directory entry.
168 * @debugfs_filter_features: features to be filtered set by debugfs.
169 */
170 struct virtio_device {
171 int index;
172 bool failed;
173 bool config_core_enabled;
174 bool config_driver_disabled;
175 bool config_change_pending;
176 spinlock_t config_lock;
177 spinlock_t vqs_list_lock;
178 struct device dev;
179 struct virtio_device_id id;
180 const struct virtio_config_ops *config;
181 const struct vringh_config_ops *vringh_config;
182 const struct virtio_map_ops *map;
183 struct list_head vqs;
184 VIRTIO_DECLARE_FEATURES(features);
185 void *priv;
186 union virtio_map vmap;
187 #ifdef CONFIG_VIRTIO_DEBUG
188 struct dentry *debugfs_dir;
189 u64 debugfs_filter_features[VIRTIO_FEATURES_U64S];
190 #endif
191 };
192
193 #define dev_to_virtio(_dev) container_of_const(_dev, struct virtio_device, dev)
194
195 void virtio_add_status(struct virtio_device *dev, unsigned int status);
196 int register_virtio_device(struct virtio_device *dev);
197 void unregister_virtio_device(struct virtio_device *dev);
198 bool is_virtio_device(struct device *dev);
199
200 void virtio_break_device(struct virtio_device *dev);
201 void __virtio_unbreak_device(struct virtio_device *dev);
202
203 void __virtqueue_break(struct virtqueue *_vq);
204 void __virtqueue_unbreak(struct virtqueue *_vq);
205
206 void virtio_config_changed(struct virtio_device *dev);
207
208 void virtio_config_driver_disable(struct virtio_device *dev);
209 void virtio_config_driver_enable(struct virtio_device *dev);
210
211 #ifdef CONFIG_PM_SLEEP
212 int virtio_device_freeze(struct virtio_device *dev);
213 int virtio_device_restore(struct virtio_device *dev);
214 #endif
215 void virtio_reset_device(struct virtio_device *dev);
216 void virtio_device_shutdown(struct virtio_device *dev);
217 int virtio_device_reset_prepare(struct virtio_device *dev);
218 int virtio_device_reset_done(struct virtio_device *dev);
219
220 size_t virtio_max_dma_size(const struct virtio_device *vdev);
221
222 #define virtio_device_for_each_vq(vdev, vq) \
223 list_for_each_entry(vq, &(vdev)->vqs, list)
224
225 /**
226 * struct virtio_driver - operations for a virtio I/O driver
227 * @driver: underlying device driver (populate name).
228 * @id_table: the ids serviced by this driver.
229 * @feature_table: an array of feature numbers supported by this driver.
230 * @feature_table_size: number of entries in the feature table array.
231 * @feature_table_legacy: same as feature_table but when working in legacy mode.
232 * @feature_table_size_legacy: number of entries in feature table legacy array.
233 * @validate: the function to call to validate features and config space.
234 * Returns 0 or -errno.
235 * @probe: the function to call when a device is found. Returns 0 or -errno.
236 * @scan: optional function to call after successful probe; intended
237 * for virtio-scsi to invoke a scan.
238 * @remove: the function to call when a device is removed.
239 * @config_changed: optional function to call when the device configuration
240 * changes; may be called in interrupt context.
241 * @freeze: optional function to call during suspend/hibernation.
242 * @restore: optional function to call on resume.
243 * @reset_prepare: optional function to call when a transport specific reset
244 * occurs.
245 * @reset_done: optional function to call after transport specific reset
246 * operation has finished.
247 * @shutdown: synchronize with the device on shutdown. If provided, replaces
248 * the virtio core implementation.
249 */
250 struct virtio_driver {
251 struct device_driver driver;
252 const struct virtio_device_id *id_table;
253 const unsigned int *feature_table;
254 unsigned int feature_table_size;
255 const unsigned int *feature_table_legacy;
256 unsigned int feature_table_size_legacy;
257 int (*validate)(struct virtio_device *dev);
258 int (*probe)(struct virtio_device *dev);
259 void (*scan)(struct virtio_device *dev);
260 void (*remove)(struct virtio_device *dev);
261 void (*config_changed)(struct virtio_device *dev);
262 int (*freeze)(struct virtio_device *dev);
263 int (*restore)(struct virtio_device *dev);
264 int (*reset_prepare)(struct virtio_device *dev);
265 int (*reset_done)(struct virtio_device *dev);
266 void (*shutdown)(struct virtio_device *dev);
267 };
268
269 #define drv_to_virtio(__drv) container_of_const(__drv, struct virtio_driver, driver)
270
271 /* use a macro to avoid include chaining to get THIS_MODULE */
272 #define register_virtio_driver(drv) \
273 __register_virtio_driver(drv, THIS_MODULE)
274 int __register_virtio_driver(struct virtio_driver *drv, struct module *owner);
275 void unregister_virtio_driver(struct virtio_driver *drv);
276
277 /* module_virtio_driver() - Helper macro for drivers that don't do
278 * anything special in module init/exit. This eliminates a lot of
279 * boilerplate. Each module may only use this macro once, and
280 * calling it replaces module_init() and module_exit()
281 */
282 #define module_virtio_driver(__virtio_driver) \
283 module_driver(__virtio_driver, register_virtio_driver, \
284 unregister_virtio_driver)
285
286
287 void *virtqueue_map_alloc_coherent(struct virtio_device *vdev,
288 union virtio_map mapping_token,
289 size_t size, dma_addr_t *dma_handle,
290 gfp_t gfp);
291
292 void virtqueue_map_free_coherent(struct virtio_device *vdev,
293 union virtio_map mapping_token,
294 size_t size, void *vaddr,
295 dma_addr_t dma_handle);
296
297 dma_addr_t virtqueue_map_page_attrs(const struct virtqueue *_vq,
298 struct page *page,
299 unsigned long offset,
300 size_t size,
301 enum dma_data_direction dir,
302 unsigned long attrs);
303
304 void virtqueue_unmap_page_attrs(const struct virtqueue *_vq,
305 dma_addr_t dma_handle,
306 size_t size, enum dma_data_direction dir,
307 unsigned long attrs);
308
309 dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *ptr, size_t size,
310 enum dma_data_direction dir, unsigned long attrs);
311 void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, dma_addr_t addr,
312 size_t size, enum dma_data_direction dir,
313 unsigned long attrs);
314 int virtqueue_map_mapping_error(const struct virtqueue *_vq, dma_addr_t addr);
315
316 bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr);
317 void virtqueue_map_sync_single_range_for_cpu(const struct virtqueue *_vq, dma_addr_t addr,
318 unsigned long offset, size_t size,
319 enum dma_data_direction dir);
320 void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_vq, dma_addr_t addr,
321 unsigned long offset, size_t size,
322 enum dma_data_direction dir);
323
324 #ifdef CONFIG_VIRTIO_DEBUG
325 void virtio_debug_device_init(struct virtio_device *dev);
326 void virtio_debug_device_exit(struct virtio_device *dev);
327 void virtio_debug_device_filter_features(struct virtio_device *dev);
328 void virtio_debug_init(void);
329 void virtio_debug_exit(void);
330 #else
virtio_debug_device_init(struct virtio_device * dev)331 static inline void virtio_debug_device_init(struct virtio_device *dev)
332 {
333 }
334
virtio_debug_device_exit(struct virtio_device * dev)335 static inline void virtio_debug_device_exit(struct virtio_device *dev)
336 {
337 }
338
virtio_debug_device_filter_features(struct virtio_device * dev)339 static inline void virtio_debug_device_filter_features(struct virtio_device *dev)
340 {
341 }
342
virtio_debug_init(void)343 static inline void virtio_debug_init(void)
344 {
345 }
346
virtio_debug_exit(void)347 static inline void virtio_debug_exit(void)
348 {
349 }
350 #endif
351
352 #endif /* _LINUX_VIRTIO_H */
353