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 * @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 int virtio_device_reset_prepare(struct virtio_device *dev); 217 int virtio_device_reset_done(struct virtio_device *dev); 218 219 size_t virtio_max_dma_size(const struct virtio_device *vdev); 220 221 #define virtio_device_for_each_vq(vdev, vq) \ 222 list_for_each_entry(vq, &(vdev)->vqs, list) 223 224 /** 225 * struct virtio_driver - operations for a virtio I/O driver 226 * @driver: underlying device driver (populate name). 227 * @id_table: the ids serviced by this driver. 228 * @feature_table: an array of feature numbers supported by this driver. 229 * @feature_table_size: number of entries in the feature table array. 230 * @feature_table_legacy: same as feature_table but when working in legacy mode. 231 * @feature_table_size_legacy: number of entries in feature table legacy array. 232 * @validate: the function to call to validate features and config space. 233 * Returns 0 or -errno. 234 * @probe: the function to call when a device is found. Returns 0 or -errno. 235 * @scan: optional function to call after successful probe; intended 236 * for virtio-scsi to invoke a scan. 237 * @remove: the function to call when a device is removed. 238 * @config_changed: optional function to call when the device configuration 239 * changes; may be called in interrupt context. 240 * @freeze: optional function to call during suspend/hibernation. 241 * @restore: optional function to call on resume. 242 * @reset_prepare: optional function to call when a transport specific reset 243 * occurs. 244 * @reset_done: optional function to call after transport specific reset 245 * operation has finished. 246 * @shutdown: synchronize with the device on shutdown. If provided, replaces 247 * the virtio core implementation. 248 */ 249 struct virtio_driver { 250 struct device_driver driver; 251 const struct virtio_device_id *id_table; 252 const unsigned int *feature_table; 253 unsigned int feature_table_size; 254 const unsigned int *feature_table_legacy; 255 unsigned int feature_table_size_legacy; 256 int (*validate)(struct virtio_device *dev); 257 int (*probe)(struct virtio_device *dev); 258 void (*scan)(struct virtio_device *dev); 259 void (*remove)(struct virtio_device *dev); 260 void (*config_changed)(struct virtio_device *dev); 261 int (*freeze)(struct virtio_device *dev); 262 int (*restore)(struct virtio_device *dev); 263 int (*reset_prepare)(struct virtio_device *dev); 264 int (*reset_done)(struct virtio_device *dev); 265 void (*shutdown)(struct virtio_device *dev); 266 }; 267 268 #define drv_to_virtio(__drv) container_of_const(__drv, struct virtio_driver, driver) 269 270 /* use a macro to avoid include chaining to get THIS_MODULE */ 271 #define register_virtio_driver(drv) \ 272 __register_virtio_driver(drv, THIS_MODULE) 273 int __register_virtio_driver(struct virtio_driver *drv, struct module *owner); 274 void unregister_virtio_driver(struct virtio_driver *drv); 275 276 /* module_virtio_driver() - Helper macro for drivers that don't do 277 * anything special in module init/exit. This eliminates a lot of 278 * boilerplate. Each module may only use this macro once, and 279 * calling it replaces module_init() and module_exit() 280 */ 281 #define module_virtio_driver(__virtio_driver) \ 282 module_driver(__virtio_driver, register_virtio_driver, \ 283 unregister_virtio_driver) 284 285 286 void *virtqueue_map_alloc_coherent(struct virtio_device *vdev, 287 union virtio_map mapping_token, 288 size_t size, dma_addr_t *dma_handle, 289 gfp_t gfp); 290 291 void virtqueue_map_free_coherent(struct virtio_device *vdev, 292 union virtio_map mapping_token, 293 size_t size, void *vaddr, 294 dma_addr_t dma_handle); 295 296 dma_addr_t virtqueue_map_page_attrs(const struct virtqueue *_vq, 297 struct page *page, 298 unsigned long offset, 299 size_t size, 300 enum dma_data_direction dir, 301 unsigned long attrs); 302 303 void virtqueue_unmap_page_attrs(const struct virtqueue *_vq, 304 dma_addr_t dma_handle, 305 size_t size, enum dma_data_direction dir, 306 unsigned long attrs); 307 308 dma_addr_t virtqueue_map_single_attrs(const struct virtqueue *_vq, void *ptr, size_t size, 309 enum dma_data_direction dir, unsigned long attrs); 310 void virtqueue_unmap_single_attrs(const struct virtqueue *_vq, dma_addr_t addr, 311 size_t size, enum dma_data_direction dir, 312 unsigned long attrs); 313 int virtqueue_map_mapping_error(const struct virtqueue *_vq, dma_addr_t addr); 314 315 bool virtqueue_map_need_sync(const struct virtqueue *_vq, dma_addr_t addr); 316 void virtqueue_map_sync_single_range_for_cpu(const struct virtqueue *_vq, dma_addr_t addr, 317 unsigned long offset, size_t size, 318 enum dma_data_direction dir); 319 void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_vq, dma_addr_t addr, 320 unsigned long offset, size_t size, 321 enum dma_data_direction dir); 322 323 #ifdef CONFIG_VIRTIO_DEBUG 324 void virtio_debug_device_init(struct virtio_device *dev); 325 void virtio_debug_device_exit(struct virtio_device *dev); 326 void virtio_debug_device_filter_features(struct virtio_device *dev); 327 void virtio_debug_init(void); 328 void virtio_debug_exit(void); 329 #else 330 static inline void virtio_debug_device_init(struct virtio_device *dev) 331 { 332 } 333 334 static inline void virtio_debug_device_exit(struct virtio_device *dev) 335 { 336 } 337 338 static inline void virtio_debug_device_filter_features(struct virtio_device *dev) 339 { 340 } 341 342 static inline void virtio_debug_init(void) 343 { 344 } 345 346 static inline void virtio_debug_exit(void) 347 { 348 } 349 #endif 350 351 #endif /* _LINUX_VIRTIO_H */ 352