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