]>
Commit | Line | Data |
---|---|---|
6e02c38d AL |
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 | ||
0d09e41a PB |
17 | #include "hw/virtio/virtio.h" |
18 | #include "hw/block/block.h" | |
48ff2692 | 19 | #include "sysemu/iothread.h" |
09f64587 | 20 | #include "block/block.h" |
6e02c38d | 21 | |
f574fa8b | 22 | #define TYPE_VIRTIO_BLK "virtio-blk-device" |
1c028ddf FK |
23 | #define VIRTIO_BLK(obj) \ |
24 | OBJECT_CHECK(VirtIOBlock, (obj), TYPE_VIRTIO_BLK) | |
25 | ||
6e02c38d AL |
26 | /* from Linux's linux/virtio_blk.h */ |
27 | ||
28 | /* The ID for virtio_block */ | |
29 | #define VIRTIO_ID_BLOCK 2 | |
30 | ||
31 | /* Feature bits */ | |
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 */ | |
1063b8b1 CH |
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 */ | |
37d5ddd6 | 39 | /* #define VIRTIO_BLK_F_IDENTIFY 8 ATA IDENTIFY supported, DEPRECATED */ |
13e3dce0 | 40 | #define VIRTIO_BLK_F_WCE 9 /* write cache enabled */ |
9752c371 | 41 | #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ |
13e3dce0 | 42 | #define VIRTIO_BLK_F_CONFIG_WCE 11 /* write cache configurable */ |
bf011293 | 43 | |
a8686a9b MA |
44 | #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ |
45 | ||
6e02c38d AL |
46 | struct virtio_blk_config |
47 | { | |
48 | uint64_t capacity; | |
49 | uint32_t size_max; | |
50 | uint32_t seg_max; | |
51 | uint16_t cylinders; | |
52 | uint8_t heads; | |
53 | uint8_t sectors; | |
8cfacf07 | 54 | uint32_t blk_size; |
9752c371 CH |
55 | uint8_t physical_block_exp; |
56 | uint8_t alignment_offset; | |
57 | uint16_t min_io_size; | |
58 | uint32_t opt_io_size; | |
13e3dce0 | 59 | uint8_t wce; |
541dc0d4 | 60 | } QEMU_PACKED; |
6e02c38d AL |
61 | |
62 | /* These two define direction. */ | |
63 | #define VIRTIO_BLK_T_IN 0 | |
64 | #define VIRTIO_BLK_T_OUT 1 | |
65 | ||
66 | /* This bit says it's a scsi command, not an actual read or write. */ | |
67 | #define VIRTIO_BLK_T_SCSI_CMD 2 | |
68 | ||
aa659be3 CH |
69 | /* Flush the volatile write cache */ |
70 | #define VIRTIO_BLK_T_FLUSH 4 | |
71 | ||
2930b313 | 72 | /* return the device ID string */ |
73 | #define VIRTIO_BLK_T_GET_ID 8 | |
74 | ||
6e02c38d AL |
75 | /* Barrier before this op. */ |
76 | #define VIRTIO_BLK_T_BARRIER 0x80000000 | |
77 | ||
78 | /* This is the first element of the read scatter-gather list. */ | |
79 | struct virtio_blk_outhdr | |
80 | { | |
81 | /* VIRTIO_BLK_T* */ | |
82 | uint32_t type; | |
83 | /* io priority. */ | |
84 | uint32_t ioprio; | |
85 | /* Sector (ie. 512 byte offset) */ | |
86 | uint64_t sector; | |
87 | }; | |
88 | ||
89 | #define VIRTIO_BLK_S_OK 0 | |
90 | #define VIRTIO_BLK_S_IOERR 1 | |
91 | #define VIRTIO_BLK_S_UNSUPP 2 | |
92 | ||
8b91408b | 93 | /* This is the last element of the write scatter-gather list */ |
6e02c38d AL |
94 | struct virtio_blk_inhdr |
95 | { | |
96 | unsigned char status; | |
97 | }; | |
98 | ||
1063b8b1 CH |
99 | /* SCSI pass-through header */ |
100 | struct virtio_scsi_inhdr | |
101 | { | |
102 | uint32_t errors; | |
103 | uint32_t data_len; | |
104 | uint32_t sense_len; | |
105 | uint32_t residual; | |
106 | }; | |
107 | ||
12c5674b PB |
108 | struct VirtIOBlkConf |
109 | { | |
110 | BlockConf conf; | |
48ff2692 | 111 | IOThread *iothread; |
12c5674b | 112 | char *serial; |
a6c5c84a | 113 | uint32_t scsi; |
8a873ba7 | 114 | uint32_t config_wce; |
e72f66a0 | 115 | uint32_t data_plane; |
12c5674b PB |
116 | }; |
117 | ||
0d09e41a PB |
118 | struct VirtIOBlockDataPlane; |
119 | ||
bf4bd461 | 120 | struct VirtIOBlockReq; |
f1b24e84 | 121 | typedef struct VirtIOBlock { |
1cc91b7d | 122 | VirtIODevice parent_obj; |
f1b24e84 FK |
123 | BlockDriverState *bs; |
124 | VirtQueue *vq; | |
125 | void *rq; | |
126 | QEMUBH *bh; | |
127 | BlockConf *conf; | |
da3dcefa | 128 | VirtIOBlkConf blk; |
f1b24e84 | 129 | unsigned short sector_mask; |
ef5bc962 | 130 | bool original_wce; |
f1b24e84 | 131 | VMChangeStateEntry *change; |
bf4bd461 FZ |
132 | /* Function to push to vq and notify guest */ |
133 | void (*complete_request)(struct VirtIOBlockReq *req, unsigned char status); | |
f1b24e84 | 134 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
84db52d0 | 135 | Notifier migration_state_notifier; |
0d09e41a | 136 | struct VirtIOBlockDataPlane *dataplane; |
f1b24e84 FK |
137 | #endif |
138 | } VirtIOBlock; | |
139 | ||
09f64587 FZ |
140 | typedef struct VirtIOBlockReq { |
141 | VirtIOBlock *dev; | |
671ec3f0 | 142 | VirtQueueElement *elem; |
09f64587 | 143 | struct virtio_blk_inhdr *in; |
827805a2 | 144 | struct virtio_blk_outhdr out; |
09f64587 FZ |
145 | QEMUIOVector qiov; |
146 | struct VirtIOBlockReq *next; | |
147 | BlockAcctCookie acct; | |
148 | } VirtIOBlockReq; | |
149 | ||
8172539d | 150 | #define DEFINE_VIRTIO_BLK_FEATURES(_state, _field) \ |
8a873ba7 | 151 | DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) |
12c5674b | 152 | |
1c028ddf FK |
153 | #ifdef __linux__ |
154 | #define DEFINE_VIRTIO_BLK_PROPERTIES(_state, _field) \ | |
155 | DEFINE_BLOCK_PROPERTIES(_state, _field.conf), \ | |
156 | DEFINE_BLOCK_CHS_PROPERTIES(_state, _field.conf), \ | |
157 | DEFINE_PROP_STRING("serial", _state, _field.serial), \ | |
158 | DEFINE_PROP_BIT("config-wce", _state, _field.config_wce, 0, true), \ | |
48ff2692 SH |
159 | DEFINE_PROP_BIT("scsi", _state, _field.scsi, 0, true), \ |
160 | DEFINE_PROP_IOTHREAD("x-iothread", _state, _field.iothread) | |
1c028ddf FK |
161 | #else |
162 | #define DEFINE_VIRTIO_BLK_PROPERTIES(_state, _field) \ | |
163 | DEFINE_BLOCK_PROPERTIES(_state, _field.conf), \ | |
164 | DEFINE_BLOCK_CHS_PROPERTIES(_state, _field.conf), \ | |
165 | DEFINE_PROP_STRING("serial", _state, _field.serial), \ | |
48ff2692 SH |
166 | DEFINE_PROP_BIT("config-wce", _state, _field.config_wce, 0, true), \ |
167 | DEFINE_PROP_IOTHREAD("x-iothread", _state, _field.iothread) | |
1c028ddf FK |
168 | #endif /* __linux__ */ |
169 | ||
170 | void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk); | |
171 | ||
5a05cbee FZ |
172 | int virtio_blk_handle_scsi_req(VirtIOBlock *blk, |
173 | VirtQueueElement *elem); | |
174 | ||
6e02c38d | 175 | #endif |