xref: /linux/include/linux/virtio.h (revision 02e3f7ffe2906033da73b7c7ea8180b131d0cdbc)
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/mod_devicetable.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  * @vqs: the list of virtqueues for this device.
161  * @features: the 64 lower features supported by both driver and device.
162  * @features_array: the full features space supported by both driver and
163  *		    device.
164  * @priv: private pointer for the driver's use.
165  * @debugfs_dir: debugfs directory entry.
166  * @debugfs_filter_features: features to be filtered set by debugfs.
167  */
168 struct virtio_device {
169 	int index;
170 	bool failed;
171 	bool config_core_enabled;
172 	bool config_driver_disabled;
173 	bool config_change_pending;
174 	spinlock_t config_lock;
175 	spinlock_t vqs_list_lock;
176 	struct device dev;
177 	struct virtio_device_id id;
178 	const struct virtio_config_ops *config;
179 	const struct vringh_config_ops *vringh_config;
180 	const struct virtio_map_ops *map;
181 	struct list_head vqs;
182 	VIRTIO_DECLARE_FEATURES(features);
183 	void *priv;
184 	union virtio_map vmap;
185 #ifdef CONFIG_VIRTIO_DEBUG
186 	struct dentry *debugfs_dir;
187 	u64 debugfs_filter_features[VIRTIO_FEATURES_U64S];
188 #endif
189 };
190 
191 #define dev_to_virtio(_dev)	container_of_const(_dev, struct virtio_device, dev)
192 
193 void virtio_add_status(struct virtio_device *dev, unsigned int status);
194 int register_virtio_device(struct virtio_device *dev);
195 void unregister_virtio_device(struct virtio_device *dev);
196 bool is_virtio_device(struct device *dev);
197 
198 void virtio_break_device(struct virtio_device *dev);
199 void __virtio_unbreak_device(struct virtio_device *dev);
200 
201 void __virtqueue_break(struct virtqueue *_vq);
202 void __virtqueue_unbreak(struct virtqueue *_vq);
203 
204 void virtio_config_changed(struct virtio_device *dev);
205 
206 void virtio_config_driver_disable(struct virtio_device *dev);
207 void virtio_config_driver_enable(struct virtio_device *dev);
208 
209 #ifdef CONFIG_PM_SLEEP
210 int virtio_device_freeze(struct virtio_device *dev);
211 int virtio_device_restore(struct virtio_device *dev);
212 #endif
213 void virtio_reset_device(struct virtio_device *dev);
214 int virtio_device_reset_prepare(struct virtio_device *dev);
215 int virtio_device_reset_done(struct virtio_device *dev);
216 
217 size_t virtio_max_dma_size(const struct virtio_device *vdev);
218 
219 #define virtio_device_for_each_vq(vdev, vq) \
220 	list_for_each_entry(vq, &(vdev)->vqs, list)
221 
222 /**
223  * struct virtio_driver - operations for a virtio I/O driver
224  * @driver: underlying device driver (populate name).
225  * @id_table: the ids serviced by this driver.
226  * @feature_table: an array of feature numbers supported by this driver.
227  * @feature_table_size: number of entries in the feature table array.
228  * @feature_table_legacy: same as feature_table but when working in legacy mode.
229  * @feature_table_size_legacy: number of entries in feature table legacy array.
230  * @validate: the function to call to validate features and config space.
231  *            Returns 0 or -errno.
232  * @probe: the function to call when a device is found.  Returns 0 or -errno.
233  * @scan: optional function to call after successful probe; intended
234  *    for virtio-scsi to invoke a scan.
235  * @remove: the function to call when a device is removed.
236  * @config_changed: optional function to call when the device configuration
237  *    changes; may be called in interrupt context.
238  * @freeze: optional function to call during suspend/hibernation.
239  * @restore: optional function to call on resume.
240  * @reset_prepare: optional function to call when a transport specific reset
241  *    occurs.
242  * @reset_done: optional function to call after transport specific reset
243  *    operation has finished.
244  * @shutdown: synchronize with the device on shutdown. If provided, replaces
245  *    the virtio core implementation.
246  */
247 struct virtio_driver {
248 	struct device_driver driver;
249 	const struct virtio_device_id *id_table;
250 	const unsigned int *feature_table;
251 	unsigned int feature_table_size;
252 	const unsigned int *feature_table_legacy;
253 	unsigned int feature_table_size_legacy;
254 	int (*validate)(struct virtio_device *dev);
255 	int (*probe)(struct virtio_device *dev);
256 	void (*scan)(struct virtio_device *dev);
257 	void (*remove)(struct virtio_device *dev);
258 	void (*config_changed)(struct virtio_device *dev);
259 	int (*freeze)(struct virtio_device *dev);
260 	int (*restore)(struct virtio_device *dev);
261 	int (*reset_prepare)(struct virtio_device *dev);
262 	int (*reset_done)(struct virtio_device *dev);
263 	void (*shutdown)(struct virtio_device *dev);
264 };
265 
266 #define drv_to_virtio(__drv)	container_of_const(__drv, struct virtio_driver, driver)
267 
268 /* use a macro to avoid include chaining to get THIS_MODULE */
269 #define register_virtio_driver(drv) \
270 	__register_virtio_driver(drv, THIS_MODULE)
271 int __register_virtio_driver(struct virtio_driver *drv, struct module *owner);
272 void unregister_virtio_driver(struct virtio_driver *drv);
273 
274 /* module_virtio_driver() - Helper macro for drivers that don't do
275  * anything special in module init/exit.  This eliminates a lot of
276  * boilerplate.  Each module may only use this macro once, and
277  * calling it replaces module_init() and module_exit()
278  */
279 #define module_virtio_driver(__virtio_driver) \
280 	module_driver(__virtio_driver, register_virtio_driver, \
281 			unregister_virtio_driver)
282 
283 
284 void *virtqueue_map_alloc_coherent(struct virtio_device *vdev,
285 				   union virtio_map mapping_token,
286 				   size_t size, dma_addr_t *dma_handle,
287 				   gfp_t gfp);
288 
289 void virtqueue_map_free_coherent(struct virtio_device *vdev,
290 				 union virtio_map mapping_token,
291 				 size_t size, void *vaddr,
292 				 dma_addr_t dma_handle);
293 
294 dma_addr_t virtqueue_map_page_attrs(const struct virtqueue *_vq,
295 				    struct page *page,
296 				    unsigned long offset,
297 				    size_t size,
298 				    enum dma_data_direction dir,
299 				    unsigned long attrs);
300 
301 void virtqueue_unmap_page_attrs(const struct virtqueue *_vq,
302 				dma_addr_t dma_handle,
303 				size_t size, enum dma_data_direction dir,
304 				unsigned long attrs);
305 
306 dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *ptr, size_t size,
307 					  enum dma_data_direction dir, unsigned long attrs);
308 void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, dma_addr_t addr,
309 				      size_t size, enum dma_data_direction dir,
310 				      unsigned long attrs);
311 int virtqueue_map_mapping_error(const struct virtqueue *_vq, dma_addr_t addr);
312 
313 bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr);
314 void virtqueue_map_sync_single_range_for_cpu(const struct virtqueue *_vq, dma_addr_t addr,
315 					     unsigned long offset, size_t size,
316 					     enum dma_data_direction dir);
317 void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_vq, dma_addr_t addr,
318 						unsigned long offset, size_t size,
319 						enum dma_data_direction dir);
320 
321 #ifdef CONFIG_VIRTIO_DEBUG
322 void virtio_debug_device_init(struct virtio_device *dev);
323 void virtio_debug_device_exit(struct virtio_device *dev);
324 void virtio_debug_device_filter_features(struct virtio_device *dev);
325 void virtio_debug_init(void);
326 void virtio_debug_exit(void);
327 #else
328 static inline void virtio_debug_device_init(struct virtio_device *dev)
329 {
330 }
331 
332 static inline void virtio_debug_device_exit(struct virtio_device *dev)
333 {
334 }
335 
336 static inline void virtio_debug_device_filter_features(struct virtio_device *dev)
337 {
338 }
339 
340 static inline void virtio_debug_init(void)
341 {
342 }
343 
344 static inline void virtio_debug_exit(void)
345 {
346 }
347 #endif
348 
349 #endif /* _LINUX_VIRTIO_H */
350