1 /* 2 * Virtio Block Device 3 * 4 * Copyright IBM, Corp. 2007 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef QEMU_VIRTIO_BLK_H 15 #define QEMU_VIRTIO_BLK_H 16 17 #include "standard-headers/linux/virtio_blk.h" 18 #include "hw/virtio/virtio.h" 19 #include "hw/block/block.h" 20 #include "sysemu/iothread.h" 21 #include "sysemu/block-backend.h" 22 #include "sysemu/block-ram-registrar.h" 23 #include "qom/object.h" 24 #include "qapi/qapi-types-virtio.h" 25 26 #define TYPE_VIRTIO_BLK "virtio-blk-device" 27 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOBlock, VIRTIO_BLK) 28 29 /* This is the last element of the write scatter-gather list */ 30 struct virtio_blk_inhdr 31 { 32 unsigned char status; 33 }; 34 35 #define VIRTIO_BLK_AUTO_NUM_QUEUES UINT16_MAX 36 37 struct VirtIOBlkConf 38 { 39 BlockConf conf; 40 IOThread *iothread; 41 IOThreadVirtQueueMappingList *iothread_vq_mapping_list; 42 char *serial; 43 uint32_t request_merging; 44 uint16_t num_queues; 45 uint16_t queue_size; 46 bool seg_max_adjust; 47 bool report_discard_granularity; 48 uint32_t max_discard_sectors; 49 uint32_t max_write_zeroes_sectors; 50 bool x_enable_wce_if_config_wce; 51 }; 52 53 struct VirtIOBlockReq; 54 struct VirtIOBlock { 55 VirtIODevice parent_obj; 56 BlockBackend *blk; 57 QemuMutex rq_lock; 58 struct VirtIOBlockReq *rq; /* protected by rq_lock */ 59 VirtIOBlkConf conf; 60 unsigned short sector_mask; 61 bool original_wce; 62 VMChangeStateEntry *change; 63 bool ioeventfd_disabled; 64 bool ioeventfd_started; 65 bool ioeventfd_starting; 66 bool ioeventfd_stopping; 67 68 /* 69 * The AioContext for each virtqueue. The BlockDriverState will use the 70 * first element as its AioContext. 71 */ 72 AioContext **vq_aio_context; 73 74 uint64_t host_features; 75 size_t config_size; 76 BlockRAMRegistrar blk_ram_registrar; 77 }; 78 79 typedef struct VirtIOBlockReq { 80 VirtQueueElement elem; 81 int64_t sector_num; 82 VirtIOBlock *dev; 83 VirtQueue *vq; 84 IOVDiscardUndo inhdr_undo; 85 IOVDiscardUndo outhdr_undo; 86 struct virtio_blk_inhdr *in; 87 struct virtio_blk_outhdr out; 88 QEMUIOVector qiov; 89 size_t in_len; 90 struct VirtIOBlockReq *next; 91 struct VirtIOBlockReq *mr_next; 92 BlockAcctCookie acct; 93 } VirtIOBlockReq; 94 95 #define VIRTIO_BLK_MAX_MERGE_REQS 32 96 97 typedef struct MultiReqBuffer { 98 VirtIOBlockReq *reqs[VIRTIO_BLK_MAX_MERGE_REQS]; 99 unsigned int num_reqs; 100 bool is_write; 101 } MultiReqBuffer; 102 103 void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq); 104 105 #endif 106