4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #ifndef _QEMU_VIRTIO_BLK_H
15 #define _QEMU_VIRTIO_BLK_H
17 #include "hw/virtio/virtio.h"
18 #include "hw/block/block.h"
19 #include "sysemu/iothread.h"
20 #include "block/block.h"
22 #define TYPE_VIRTIO_BLK "virtio-blk-device"
23 #define VIRTIO_BLK(obj) \
24 OBJECT_CHECK(VirtIOBlock, (obj), TYPE_VIRTIO_BLK)
26 /* from Linux's linux/virtio_blk.h */
28 /* The ID for virtio_block */
29 #define VIRTIO_ID_BLOCK 2
32 #define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */
33 #define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */
34 #define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */
35 #define VIRTIO_BLK_F_GEOMETRY 4 /* Indicates support of legacy geometry */
36 #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */
37 #define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available*/
38 #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */
39 /* #define VIRTIO_BLK_F_IDENTIFY 8 ATA IDENTIFY supported, DEPRECATED */
40 #define VIRTIO_BLK_F_WCE 9 /* write cache enabled */
41 #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */
42 #define VIRTIO_BLK_F_CONFIG_WCE 11 /* write cache configurable */
44 #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */
46 struct virtio_blk_config
55 uint8_t physical_block_exp
;
56 uint8_t alignment_offset
;
62 /* These two define direction. */
63 #define VIRTIO_BLK_T_IN 0
64 #define VIRTIO_BLK_T_OUT 1
66 /* This bit says it's a scsi command, not an actual read or write. */
67 #define VIRTIO_BLK_T_SCSI_CMD 2
69 /* Flush the volatile write cache */
70 #define VIRTIO_BLK_T_FLUSH 4
72 /* return the device ID string */
73 #define VIRTIO_BLK_T_GET_ID 8
75 /* Barrier before this op. */
76 #define VIRTIO_BLK_T_BARRIER 0x80000000
78 /* This is the first element of the read scatter-gather list. */
79 struct virtio_blk_outhdr
85 /* Sector (ie. 512 byte offset) */
89 #define VIRTIO_BLK_S_OK 0
90 #define VIRTIO_BLK_S_IOERR 1
91 #define VIRTIO_BLK_S_UNSUPP 2
93 /* This is the last element of the write scatter-gather list */
94 struct virtio_blk_inhdr
99 /* SCSI pass-through header */
100 struct virtio_scsi_inhdr
118 struct VirtIOBlockDataPlane
;
120 struct VirtIOBlockReq
;
121 typedef struct VirtIOBlock
{
122 VirtIODevice parent_obj
;
123 BlockDriverState
*bs
;
129 unsigned short sector_mask
;
131 VMChangeStateEntry
*change
;
132 /* Function to push to vq and notify guest */
133 void (*complete_request
)(struct VirtIOBlockReq
*req
, unsigned char status
);
134 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
135 Notifier migration_state_notifier
;
136 struct VirtIOBlockDataPlane
*dataplane
;
140 typedef struct MultiReqBuffer
{
141 BlockRequest blkreq
[32];
142 unsigned int num_writes
;
145 typedef struct VirtIOBlockReq
{
147 VirtQueueElement
*elem
;
148 struct virtio_blk_inhdr
*in
;
149 struct virtio_blk_outhdr out
;
151 struct VirtIOBlockReq
*next
;
152 BlockAcctCookie acct
;
155 #define DEFINE_VIRTIO_BLK_FEATURES(_state, _field) \
156 DEFINE_VIRTIO_COMMON_FEATURES(_state, _field)
159 #define DEFINE_VIRTIO_BLK_PROPERTIES_LINUX(_state, _field) \
160 DEFINE_PROP_BIT("scsi", _state, _field.scsi, 0, true),
162 #define DEFINE_VIRTIO_BLK_PROPERTIES_LINUX(_state, _field)
165 #define DEFINE_VIRTIO_BLK_PROPERTIES(_state, _field) \
166 DEFINE_VIRTIO_BLK_PROPERTIES_LINUX(_state, _field) \
167 DEFINE_BLOCK_PROPERTIES(_state, _field.conf), \
168 DEFINE_BLOCK_CHS_PROPERTIES(_state, _field.conf), \
169 DEFINE_PROP_STRING("serial", _state, _field.serial), \
170 DEFINE_PROP_BIT("config-wce", _state, _field.config_wce, 0, true), \
171 DEFINE_PROP_IOTHREAD("x-iothread", _state, _field.iothread)
173 void virtio_blk_set_conf(DeviceState
*dev
, VirtIOBlkConf
*blk
);
175 int virtio_blk_handle_scsi_req(VirtIOBlock
*blk
,
176 VirtQueueElement
*elem
);
178 void virtio_blk_handle_request(VirtIOBlockReq
*req
, MultiReqBuffer
*mrb
);
180 void virtio_submit_multiwrite(BlockDriverState
*bs
, MultiReqBuffer
*mrb
);