]>
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 | ||
5a61cb60 | 14 | #include "qemu-common.h" |
1de7afc9 | 15 | #include "qemu/error-report.h" |
6d519a5f | 16 | #include "trace.h" |
0d09e41a | 17 | #include "hw/block/block.h" |
9c17d615 | 18 | #include "sysemu/blockdev.h" |
0d09e41a PB |
19 | #include "hw/virtio/virtio-blk.h" |
20 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE | |
21 | # include "dataplane/virtio-blk.h" | |
84db52d0 | 22 | # include "migration/migration.h" |
0d09e41a PB |
23 | #endif |
24 | #include "block/scsi.h" | |
1063b8b1 CH |
25 | #ifdef __linux__ |
26 | # include <scsi/sg.h> | |
27 | #endif | |
0d09e41a | 28 | #include "hw/virtio/virtio-bus.h" |
6e02c38d | 29 | |
6e02c38d AL |
30 | typedef struct VirtIOBlockReq |
31 | { | |
32 | VirtIOBlock *dev; | |
33 | VirtQueueElement elem; | |
34 | struct virtio_blk_inhdr *in; | |
35 | struct virtio_blk_outhdr *out; | |
1063b8b1 | 36 | struct virtio_scsi_inhdr *scsi; |
d28a1b6e | 37 | QEMUIOVector qiov; |
869a5c6d | 38 | struct VirtIOBlockReq *next; |
a597e79c | 39 | BlockAcctCookie acct; |
6e02c38d AL |
40 | } VirtIOBlockReq; |
41 | ||
869a5c6d AL |
42 | static void virtio_blk_req_complete(VirtIOBlockReq *req, int status) |
43 | { | |
44 | VirtIOBlock *s = req->dev; | |
1cc91b7d | 45 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
869a5c6d | 46 | |
6d519a5f SH |
47 | trace_virtio_blk_req_complete(req, status); |
48 | ||
92e3c2a3 | 49 | stb_p(&req->in->status, status); |
d28a1b6e | 50 | virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in)); |
1cc91b7d | 51 | virtio_notify(vdev, s->vq); |
869a5c6d AL |
52 | } |
53 | ||
f35d68f0 | 54 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
1ceee0d5 | 55 | bool is_read) |
869a5c6d | 56 | { |
3e1caa5f | 57 | BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error); |
869a5c6d AL |
58 | VirtIOBlock *s = req->dev; |
59 | ||
3e1caa5f | 60 | if (action == BDRV_ACTION_STOP) { |
869a5c6d AL |
61 | req->next = s->rq; |
62 | s->rq = req; | |
3e1caa5f | 63 | } else if (action == BDRV_ACTION_REPORT) { |
869a5c6d | 64 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
a597e79c CH |
65 | bdrv_acct_done(s->bs, &req->acct); |
66 | g_free(req); | |
869a5c6d AL |
67 | } |
68 | ||
3e1caa5f PB |
69 | bdrv_error_action(s->bs, action, is_read, error); |
70 | return action != BDRV_ACTION_IGNORE; | |
869a5c6d AL |
71 | } |
72 | ||
6e02c38d AL |
73 | static void virtio_blk_rw_complete(void *opaque, int ret) |
74 | { | |
75 | VirtIOBlockReq *req = opaque; | |
6e02c38d | 76 | |
6d519a5f SH |
77 | trace_virtio_blk_rw_complete(req, ret); |
78 | ||
f35d68f0 | 79 | if (ret) { |
1ceee0d5 | 80 | bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT); |
f35d68f0 | 81 | if (virtio_blk_handle_rw_error(req, -ret, is_read)) |
869a5c6d | 82 | return; |
6e02c38d AL |
83 | } |
84 | ||
f35d68f0 | 85 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
a597e79c CH |
86 | bdrv_acct_done(req->dev->bs, &req->acct); |
87 | g_free(req); | |
869a5c6d | 88 | } |
6e02c38d | 89 | |
aa659be3 CH |
90 | static void virtio_blk_flush_complete(void *opaque, int ret) |
91 | { | |
92 | VirtIOBlockReq *req = opaque; | |
93 | ||
8c269b54 KW |
94 | if (ret) { |
95 | if (virtio_blk_handle_rw_error(req, -ret, 0)) { | |
96 | return; | |
97 | } | |
98 | } | |
99 | ||
100 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); | |
a597e79c CH |
101 | bdrv_acct_done(req->dev->bs, &req->acct); |
102 | g_free(req); | |
aa659be3 CH |
103 | } |
104 | ||
869a5c6d AL |
105 | static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s) |
106 | { | |
7267c094 | 107 | VirtIOBlockReq *req = g_malloc(sizeof(*req)); |
487414f1 | 108 | req->dev = s; |
de6c8042 SH |
109 | req->qiov.size = 0; |
110 | req->next = NULL; | |
869a5c6d | 111 | return req; |
6e02c38d AL |
112 | } |
113 | ||
114 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s) | |
115 | { | |
869a5c6d | 116 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
6e02c38d | 117 | |
869a5c6d AL |
118 | if (req != NULL) { |
119 | if (!virtqueue_pop(s->vq, &req->elem)) { | |
7267c094 | 120 | g_free(req); |
869a5c6d AL |
121 | return NULL; |
122 | } | |
6e02c38d AL |
123 | } |
124 | ||
125 | return req; | |
126 | } | |
127 | ||
1063b8b1 CH |
128 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
129 | { | |
47ce9ef7 | 130 | #ifdef __linux__ |
4277906d | 131 | int ret; |
1063b8b1 | 132 | int i; |
47ce9ef7 SW |
133 | #endif |
134 | int status = VIRTIO_BLK_S_OK; | |
1063b8b1 CH |
135 | |
136 | /* | |
137 | * We require at least one output segment each for the virtio_blk_outhdr | |
138 | * and the SCSI command block. | |
139 | * | |
140 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr | |
141 | * and the sense buffer pointer in the input segments. | |
142 | */ | |
143 | if (req->elem.out_num < 2 || req->elem.in_num < 3) { | |
144 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); | |
a597e79c | 145 | g_free(req); |
1063b8b1 CH |
146 | return; |
147 | } | |
148 | ||
149 | /* | |
f34e73cd PB |
150 | * The scsi inhdr is placed in the second-to-last input segment, just |
151 | * before the regular inhdr. | |
1063b8b1 | 152 | */ |
f34e73cd PB |
153 | req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; |
154 | ||
da3dcefa | 155 | if (!req->dev->blk.scsi) { |
f34e73cd PB |
156 | status = VIRTIO_BLK_S_UNSUPP; |
157 | goto fail; | |
1063b8b1 CH |
158 | } |
159 | ||
160 | /* | |
f34e73cd | 161 | * No support for bidirection commands yet. |
1063b8b1 | 162 | */ |
f34e73cd PB |
163 | if (req->elem.out_num > 2 && req->elem.in_num > 3) { |
164 | status = VIRTIO_BLK_S_UNSUPP; | |
165 | goto fail; | |
166 | } | |
1063b8b1 | 167 | |
f34e73cd PB |
168 | #ifdef __linux__ |
169 | struct sg_io_hdr hdr; | |
1063b8b1 CH |
170 | memset(&hdr, 0, sizeof(struct sg_io_hdr)); |
171 | hdr.interface_id = 'S'; | |
172 | hdr.cmd_len = req->elem.out_sg[1].iov_len; | |
173 | hdr.cmdp = req->elem.out_sg[1].iov_base; | |
174 | hdr.dxfer_len = 0; | |
175 | ||
176 | if (req->elem.out_num > 2) { | |
177 | /* | |
178 | * If there are more than the minimally required 2 output segments | |
179 | * there is write payload starting from the third iovec. | |
180 | */ | |
181 | hdr.dxfer_direction = SG_DXFER_TO_DEV; | |
182 | hdr.iovec_count = req->elem.out_num - 2; | |
183 | ||
184 | for (i = 0; i < hdr.iovec_count; i++) | |
185 | hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len; | |
186 | ||
187 | hdr.dxferp = req->elem.out_sg + 2; | |
188 | ||
189 | } else if (req->elem.in_num > 3) { | |
190 | /* | |
191 | * If we have more than 3 input segments the guest wants to actually | |
192 | * read data. | |
193 | */ | |
194 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
195 | hdr.iovec_count = req->elem.in_num - 3; | |
196 | for (i = 0; i < hdr.iovec_count; i++) | |
197 | hdr.dxfer_len += req->elem.in_sg[i].iov_len; | |
198 | ||
199 | hdr.dxferp = req->elem.in_sg; | |
1063b8b1 CH |
200 | } else { |
201 | /* | |
202 | * Some SCSI commands don't actually transfer any data. | |
203 | */ | |
204 | hdr.dxfer_direction = SG_DXFER_NONE; | |
205 | } | |
206 | ||
207 | hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base; | |
208 | hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len; | |
1063b8b1 CH |
209 | |
210 | ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr); | |
211 | if (ret) { | |
212 | status = VIRTIO_BLK_S_UNSUPP; | |
f34e73cd | 213 | goto fail; |
1063b8b1 CH |
214 | } |
215 | ||
5bb23927 PB |
216 | /* |
217 | * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi) | |
218 | * clear the masked_status field [hence status gets cleared too, see | |
219 | * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED | |
220 | * status has occurred. However they do set DRIVER_SENSE in driver_status | |
221 | * field. Also a (sb_len_wr > 0) indicates there is a sense buffer. | |
222 | */ | |
223 | if (hdr.status == 0 && hdr.sb_len_wr > 0) { | |
224 | hdr.status = CHECK_CONDITION; | |
225 | } | |
226 | ||
227 | stl_p(&req->scsi->errors, | |
228 | hdr.status | (hdr.msg_status << 8) | | |
229 | (hdr.host_status << 16) | (hdr.driver_status << 24)); | |
92e3c2a3 AJ |
230 | stl_p(&req->scsi->residual, hdr.resid); |
231 | stl_p(&req->scsi->sense_len, hdr.sb_len_wr); | |
232 | stl_p(&req->scsi->data_len, hdr.dxfer_len); | |
1063b8b1 CH |
233 | |
234 | virtio_blk_req_complete(req, status); | |
a597e79c | 235 | g_free(req); |
730a9c53 | 236 | return; |
1063b8b1 | 237 | #else |
f34e73cd PB |
238 | abort(); |
239 | #endif | |
240 | ||
241 | fail: | |
242 | /* Just put anything nonzero so that the ioctl fails in the guest. */ | |
243 | stl_p(&req->scsi->errors, 255); | |
244 | virtio_blk_req_complete(req, status); | |
a597e79c | 245 | g_free(req); |
1063b8b1 | 246 | } |
1063b8b1 | 247 | |
c20fd872 CH |
248 | typedef struct MultiReqBuffer { |
249 | BlockRequest blkreq[32]; | |
250 | unsigned int num_writes; | |
251 | } MultiReqBuffer; | |
252 | ||
253 | static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb) | |
869a5c6d | 254 | { |
91553dcc | 255 | int i, ret; |
91553dcc | 256 | |
c20fd872 CH |
257 | if (!mrb->num_writes) { |
258 | return; | |
259 | } | |
260 | ||
261 | ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes); | |
91553dcc | 262 | if (ret != 0) { |
c20fd872 CH |
263 | for (i = 0; i < mrb->num_writes; i++) { |
264 | if (mrb->blkreq[i].error) { | |
265 | virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO); | |
91553dcc KW |
266 | } |
267 | } | |
268 | } | |
c20fd872 CH |
269 | |
270 | mrb->num_writes = 0; | |
91553dcc | 271 | } |
87b245db | 272 | |
c20fd872 | 273 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
aa659be3 | 274 | { |
a597e79c CH |
275 | bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH); |
276 | ||
618fbb84 CH |
277 | /* |
278 | * Make sure all outstanding writes are posted to the backing device. | |
279 | */ | |
c20fd872 | 280 | virtio_submit_multiwrite(req->dev->bs, mrb); |
ad54ae80 | 281 | bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req); |
aa659be3 CH |
282 | } |
283 | ||
c20fd872 | 284 | static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
91553dcc | 285 | { |
c20fd872 | 286 | BlockRequest *blkreq; |
92e3c2a3 | 287 | uint64_t sector; |
c20fd872 | 288 | |
92e3c2a3 | 289 | sector = ldq_p(&req->out->sector); |
6d519a5f | 290 | |
a597e79c CH |
291 | bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE); |
292 | ||
92e3c2a3 AJ |
293 | trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512); |
294 | ||
295 | if (sector & req->dev->sector_mask) { | |
8cfacf07 CH |
296 | virtio_blk_rw_complete(req, -EIO); |
297 | return; | |
298 | } | |
52c05023 CH |
299 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
300 | virtio_blk_rw_complete(req, -EIO); | |
301 | return; | |
302 | } | |
8cfacf07 | 303 | |
c20fd872 CH |
304 | if (mrb->num_writes == 32) { |
305 | virtio_submit_multiwrite(req->dev->bs, mrb); | |
87b245db | 306 | } |
91553dcc | 307 | |
c20fd872 | 308 | blkreq = &mrb->blkreq[mrb->num_writes]; |
92e3c2a3 | 309 | blkreq->sector = sector; |
c20fd872 CH |
310 | blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE; |
311 | blkreq->qiov = &req->qiov; | |
312 | blkreq->cb = virtio_blk_rw_complete; | |
313 | blkreq->opaque = req; | |
314 | blkreq->error = 0; | |
91553dcc | 315 | |
c20fd872 | 316 | mrb->num_writes++; |
d28a1b6e | 317 | } |
869a5c6d | 318 | |
d28a1b6e AL |
319 | static void virtio_blk_handle_read(VirtIOBlockReq *req) |
320 | { | |
92e3c2a3 AJ |
321 | uint64_t sector; |
322 | ||
323 | sector = ldq_p(&req->out->sector); | |
87b245db | 324 | |
a597e79c CH |
325 | bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ); |
326 | ||
81b6b9fa SH |
327 | trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512); |
328 | ||
92e3c2a3 | 329 | if (sector & req->dev->sector_mask) { |
8cfacf07 CH |
330 | virtio_blk_rw_complete(req, -EIO); |
331 | return; | |
332 | } | |
52c05023 CH |
333 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
334 | virtio_blk_rw_complete(req, -EIO); | |
335 | return; | |
336 | } | |
ad54ae80 PB |
337 | bdrv_aio_readv(req->dev->bs, sector, &req->qiov, |
338 | req->qiov.size / BDRV_SECTOR_SIZE, | |
339 | virtio_blk_rw_complete, req); | |
869a5c6d AL |
340 | } |
341 | ||
bc6694d4 KW |
342 | static void virtio_blk_handle_request(VirtIOBlockReq *req, |
343 | MultiReqBuffer *mrb) | |
344 | { | |
92e3c2a3 AJ |
345 | uint32_t type; |
346 | ||
bc6694d4 | 347 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
870cef1d | 348 | error_report("virtio-blk missing headers"); |
bc6694d4 KW |
349 | exit(1); |
350 | } | |
351 | ||
352 | if (req->elem.out_sg[0].iov_len < sizeof(*req->out) || | |
353 | req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) { | |
870cef1d | 354 | error_report("virtio-blk header not in correct element"); |
bc6694d4 KW |
355 | exit(1); |
356 | } | |
357 | ||
358 | req->out = (void *)req->elem.out_sg[0].iov_base; | |
359 | req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base; | |
360 | ||
92e3c2a3 AJ |
361 | type = ldl_p(&req->out->type); |
362 | ||
363 | if (type & VIRTIO_BLK_T_FLUSH) { | |
c20fd872 | 364 | virtio_blk_handle_flush(req, mrb); |
92e3c2a3 | 365 | } else if (type & VIRTIO_BLK_T_SCSI_CMD) { |
bc6694d4 | 366 | virtio_blk_handle_scsi(req); |
92e3c2a3 | 367 | } else if (type & VIRTIO_BLK_T_GET_ID) { |
2930b313 | 368 | VirtIOBlock *s = req->dev; |
369 | ||
a8686a9b MA |
370 | /* |
371 | * NB: per existing s/n string convention the string is | |
372 | * terminated by '\0' only when shorter than buffer. | |
373 | */ | |
374 | strncpy(req->elem.in_sg[0].iov_base, | |
da3dcefa | 375 | s->blk.serial ? s->blk.serial : "", |
a8686a9b | 376 | MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES)); |
2930b313 | 377 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
a597e79c | 378 | g_free(req); |
92e3c2a3 | 379 | } else if (type & VIRTIO_BLK_T_OUT) { |
bc6694d4 KW |
380 | qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1], |
381 | req->elem.out_num - 1); | |
c20fd872 | 382 | virtio_blk_handle_write(req, mrb); |
9e72c450 AZ |
383 | } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) { |
384 | /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */ | |
bc6694d4 KW |
385 | qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0], |
386 | req->elem.in_num - 1); | |
387 | virtio_blk_handle_read(req); | |
9e72c450 AZ |
388 | } else { |
389 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); | |
390 | g_free(req); | |
bc6694d4 KW |
391 | } |
392 | } | |
393 | ||
6e02c38d AL |
394 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
395 | { | |
1cc91b7d | 396 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
6e02c38d | 397 | VirtIOBlockReq *req; |
bc6694d4 KW |
398 | MultiReqBuffer mrb = { |
399 | .num_writes = 0, | |
bc6694d4 | 400 | }; |
6e02c38d | 401 | |
392808b4 SH |
402 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
403 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start | |
404 | * dataplane here instead of waiting for .set_status(). | |
405 | */ | |
406 | if (s->dataplane) { | |
407 | virtio_blk_data_plane_start(s->dataplane); | |
408 | return; | |
409 | } | |
410 | #endif | |
411 | ||
6e02c38d | 412 | while ((req = virtio_blk_get_request(s))) { |
bc6694d4 | 413 | virtio_blk_handle_request(req, &mrb); |
6e02c38d | 414 | } |
91553dcc | 415 | |
c20fd872 | 416 | virtio_submit_multiwrite(s->bs, &mrb); |
91553dcc | 417 | |
6e02c38d AL |
418 | /* |
419 | * FIXME: Want to check for completions before returning to guest mode, | |
420 | * so cached reads and writes are reported as quickly as possible. But | |
421 | * that should be done in the generic block layer. | |
422 | */ | |
423 | } | |
424 | ||
213189ab | 425 | static void virtio_blk_dma_restart_bh(void *opaque) |
869a5c6d AL |
426 | { |
427 | VirtIOBlock *s = opaque; | |
428 | VirtIOBlockReq *req = s->rq; | |
f1b52868 KW |
429 | MultiReqBuffer mrb = { |
430 | .num_writes = 0, | |
f1b52868 | 431 | }; |
869a5c6d | 432 | |
213189ab MA |
433 | qemu_bh_delete(s->bh); |
434 | s->bh = NULL; | |
869a5c6d AL |
435 | |
436 | s->rq = NULL; | |
437 | ||
438 | while (req) { | |
f1b52868 | 439 | virtio_blk_handle_request(req, &mrb); |
869a5c6d AL |
440 | req = req->next; |
441 | } | |
f1b52868 | 442 | |
c20fd872 | 443 | virtio_submit_multiwrite(s->bs, &mrb); |
869a5c6d AL |
444 | } |
445 | ||
1dfb4dd9 LC |
446 | static void virtio_blk_dma_restart_cb(void *opaque, int running, |
447 | RunState state) | |
213189ab MA |
448 | { |
449 | VirtIOBlock *s = opaque; | |
450 | ||
392808b4 | 451 | if (!running) { |
213189ab | 452 | return; |
392808b4 | 453 | } |
213189ab MA |
454 | |
455 | if (!s->bh) { | |
456 | s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s); | |
457 | qemu_bh_schedule(s->bh); | |
458 | } | |
459 | } | |
460 | ||
6e02c38d AL |
461 | static void virtio_blk_reset(VirtIODevice *vdev) |
462 | { | |
1cc91b7d | 463 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
392808b4 | 464 | |
ef5bc962 | 465 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
392808b4 SH |
466 | if (s->dataplane) { |
467 | virtio_blk_data_plane_stop(s->dataplane); | |
468 | } | |
469 | #endif | |
470 | ||
6e02c38d AL |
471 | /* |
472 | * This should cancel pending requests, but can't do nicely until there | |
473 | * are per-device request lists. | |
474 | */ | |
922453bc | 475 | bdrv_drain_all(); |
ef5bc962 | 476 | bdrv_set_enable_write_cache(s->bs, s->original_wce); |
6e02c38d AL |
477 | } |
478 | ||
bf011293 | 479 | /* coalesce internal state, copy to pci i/o region 0 |
480 | */ | |
6e02c38d AL |
481 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
482 | { | |
1cc91b7d | 483 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
6e02c38d AL |
484 | struct virtio_blk_config blkcfg; |
485 | uint64_t capacity; | |
3a395142 | 486 | int blk_size = s->conf->logical_block_size; |
6e02c38d AL |
487 | |
488 | bdrv_get_geometry(s->bs, &capacity); | |
5c5dafdc | 489 | memset(&blkcfg, 0, sizeof(blkcfg)); |
6e02c38d AL |
490 | stq_raw(&blkcfg.capacity, capacity); |
491 | stl_raw(&blkcfg.seg_max, 128 - 2); | |
e63e7fde | 492 | stw_raw(&blkcfg.cylinders, s->conf->cyls); |
3a395142 PB |
493 | stl_raw(&blkcfg.blk_size, blk_size); |
494 | stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size); | |
495 | stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size); | |
e63e7fde | 496 | blkcfg.heads = s->conf->heads; |
136be99e CB |
497 | /* |
498 | * We must ensure that the block device capacity is a multiple of | |
e03ba136 | 499 | * the logical block size. If that is not the case, let's use |
136be99e CB |
500 | * sector_mask to adopt the geometry to have a correct picture. |
501 | * For those devices where the capacity is ok for the given geometry | |
e03ba136 | 502 | * we don't touch the sector value of the geometry, since some devices |
136be99e CB |
503 | * (like s390 dasd) need a specific value. Here the capacity is already |
504 | * cyls*heads*secs*blk_size and the sector value is not block size | |
505 | * divided by 512 - instead it is the amount of blk_size blocks | |
506 | * per track (cylinder). | |
507 | */ | |
e63e7fde MA |
508 | if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) { |
509 | blkcfg.sectors = s->conf->secs & ~s->sector_mask; | |
136be99e | 510 | } else { |
e63e7fde | 511 | blkcfg.sectors = s->conf->secs; |
136be99e | 512 | } |
c7085da7 | 513 | blkcfg.size_max = 0; |
9752c371 CH |
514 | blkcfg.physical_block_exp = get_physical_block_exp(s->conf); |
515 | blkcfg.alignment_offset = 0; | |
13e3dce0 | 516 | blkcfg.wce = bdrv_enable_write_cache(s->bs); |
37d5ddd6 | 517 | memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); |
6e02c38d AL |
518 | } |
519 | ||
13e3dce0 PB |
520 | static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
521 | { | |
1cc91b7d | 522 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
13e3dce0 PB |
523 | struct virtio_blk_config blkcfg; |
524 | ||
525 | memcpy(&blkcfg, config, sizeof(blkcfg)); | |
526 | bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0); | |
527 | } | |
528 | ||
8172539d | 529 | static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features) |
6e02c38d | 530 | { |
1cc91b7d | 531 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
1063b8b1 CH |
532 | |
533 | features |= (1 << VIRTIO_BLK_F_SEG_MAX); | |
534 | features |= (1 << VIRTIO_BLK_F_GEOMETRY); | |
9752c371 | 535 | features |= (1 << VIRTIO_BLK_F_TOPOLOGY); |
8cfacf07 | 536 | features |= (1 << VIRTIO_BLK_F_BLK_SIZE); |
a6c5c84a | 537 | features |= (1 << VIRTIO_BLK_F_SCSI); |
aa659be3 | 538 | |
da3dcefa | 539 | if (s->blk.config_wce) { |
8a873ba7 SH |
540 | features |= (1 << VIRTIO_BLK_F_CONFIG_WCE); |
541 | } | |
aa659be3 | 542 | if (bdrv_enable_write_cache(s->bs)) |
13e3dce0 PB |
543 | features |= (1 << VIRTIO_BLK_F_WCE); |
544 | ||
c79662f7 NS |
545 | if (bdrv_is_read_only(s->bs)) |
546 | features |= 1 << VIRTIO_BLK_F_RO; | |
1063b8b1 CH |
547 | |
548 | return features; | |
6e02c38d AL |
549 | } |
550 | ||
9315cbfd PB |
551 | static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) |
552 | { | |
1cc91b7d | 553 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
9315cbfd PB |
554 | uint32_t features; |
555 | ||
392808b4 | 556 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
cf139388 SH |
557 | if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER | |
558 | VIRTIO_CONFIG_S_DRIVER_OK))) { | |
392808b4 SH |
559 | virtio_blk_data_plane_stop(s->dataplane); |
560 | } | |
561 | #endif | |
562 | ||
9315cbfd PB |
563 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
564 | return; | |
565 | } | |
566 | ||
567 | features = vdev->guest_features; | |
ef5bc962 PB |
568 | |
569 | /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send | |
570 | * cache flushes. Thus, the "auto writethrough" behavior is never | |
571 | * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature. | |
572 | * Leaving it enabled would break the following sequence: | |
573 | * | |
574 | * Guest started with "-drive cache=writethrough" | |
575 | * Guest sets status to 0 | |
576 | * Guest sets DRIVER bit in status field | |
577 | * Guest reads host features (WCE=0, CONFIG_WCE=1) | |
578 | * Guest writes guest features (WCE=0, CONFIG_WCE=1) | |
579 | * Guest writes 1 to the WCE configuration field (writeback mode) | |
580 | * Guest sets DRIVER_OK bit in status field | |
581 | * | |
582 | * s->bs would erroneously be placed in writethrough mode. | |
583 | */ | |
584 | if (!(features & (1 << VIRTIO_BLK_F_CONFIG_WCE))) { | |
585 | bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE))); | |
586 | } | |
9315cbfd PB |
587 | } |
588 | ||
6e02c38d AL |
589 | static void virtio_blk_save(QEMUFile *f, void *opaque) |
590 | { | |
591 | VirtIOBlock *s = opaque; | |
1cc91b7d | 592 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
869a5c6d AL |
593 | VirtIOBlockReq *req = s->rq; |
594 | ||
1cc91b7d | 595 | virtio_save(vdev, f); |
869a5c6d AL |
596 | |
597 | while (req) { | |
598 | qemu_put_sbyte(f, 1); | |
599 | qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); | |
600 | req = req->next; | |
601 | } | |
602 | qemu_put_sbyte(f, 0); | |
6e02c38d AL |
603 | } |
604 | ||
605 | static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) | |
606 | { | |
607 | VirtIOBlock *s = opaque; | |
1cc91b7d | 608 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
2a633c46 | 609 | int ret; |
6e02c38d | 610 | |
869a5c6d | 611 | if (version_id != 2) |
6e02c38d AL |
612 | return -EINVAL; |
613 | ||
1cc91b7d | 614 | ret = virtio_load(vdev, f); |
2a633c46 OW |
615 | if (ret) { |
616 | return ret; | |
617 | } | |
618 | ||
869a5c6d AL |
619 | while (qemu_get_sbyte(f)) { |
620 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); | |
621 | qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); | |
622 | req->next = s->rq; | |
20a81e4d | 623 | s->rq = req; |
b6a4805b KW |
624 | |
625 | virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr, | |
626 | req->elem.in_num, 1); | |
627 | virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr, | |
628 | req->elem.out_num, 0); | |
869a5c6d | 629 | } |
6e02c38d AL |
630 | |
631 | return 0; | |
632 | } | |
633 | ||
145feb17 | 634 | static void virtio_blk_resize(void *opaque) |
e5051fc7 | 635 | { |
1cc91b7d | 636 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
e5051fc7 | 637 | |
1cc91b7d | 638 | virtio_notify_config(vdev); |
e5051fc7 CH |
639 | } |
640 | ||
0e49de52 | 641 | static const BlockDevOps virtio_block_ops = { |
145feb17 | 642 | .resize_cb = virtio_blk_resize, |
0e49de52 MA |
643 | }; |
644 | ||
1c028ddf | 645 | void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk) |
6e02c38d | 646 | { |
1c028ddf KF |
647 | VirtIOBlock *s = VIRTIO_BLK(dev); |
648 | memcpy(&(s->blk), blk, sizeof(struct VirtIOBlkConf)); | |
649 | } | |
650 | ||
84db52d0 SH |
651 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
652 | /* Disable dataplane thread during live migration since it does not | |
653 | * update the dirty memory bitmap yet. | |
654 | */ | |
655 | static void virtio_blk_migration_state_changed(Notifier *notifier, void *data) | |
656 | { | |
657 | VirtIOBlock *s = container_of(notifier, VirtIOBlock, | |
658 | migration_state_notifier); | |
659 | MigrationState *mig = data; | |
660 | ||
661 | if (migration_in_setup(mig)) { | |
662 | if (!s->dataplane) { | |
663 | return; | |
664 | } | |
665 | virtio_blk_data_plane_destroy(s->dataplane); | |
666 | s->dataplane = NULL; | |
667 | } else if (migration_has_finished(mig) || | |
668 | migration_has_failed(mig)) { | |
669 | if (s->dataplane) { | |
670 | return; | |
671 | } | |
672 | bdrv_drain_all(); /* complete in-flight non-dataplane requests */ | |
673 | virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->blk, | |
674 | &s->dataplane); | |
675 | } | |
676 | } | |
677 | #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */ | |
678 | ||
05ff6865 | 679 | static int virtio_blk_device_init(VirtIODevice *vdev) |
1c028ddf | 680 | { |
05ff6865 KF |
681 | DeviceState *qdev = DEVICE(vdev); |
682 | VirtIOBlock *s = VIRTIO_BLK(vdev); | |
683 | VirtIOBlkConf *blk = &(s->blk); | |
6e02c38d | 684 | static int virtio_blk_id; |
cf21e106 | 685 | |
12c5674b | 686 | if (!blk->conf.bs) { |
6a84cb1f | 687 | error_report("drive property not set"); |
05ff6865 | 688 | return -1; |
d75d25e3 | 689 | } |
12c5674b | 690 | if (!bdrv_is_inserted(blk->conf.bs)) { |
98f28ad7 | 691 | error_report("Device needs media, but drive is empty"); |
05ff6865 | 692 | return -1; |
98f28ad7 | 693 | } |
d75d25e3 | 694 | |
911525db | 695 | blkconf_serial(&blk->conf, &blk->serial); |
ef5bc962 | 696 | s->original_wce = bdrv_enable_write_cache(blk->conf.bs); |
b7eb0c9f | 697 | if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) { |
05ff6865 | 698 | return -1; |
b7eb0c9f | 699 | } |
a8686a9b | 700 | |
05ff6865 KF |
701 | virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, |
702 | sizeof(struct virtio_blk_config)); | |
6e02c38d | 703 | |
12c5674b PB |
704 | s->bs = blk->conf.bs; |
705 | s->conf = &blk->conf; | |
869a5c6d | 706 | s->rq = NULL; |
1573a35d | 707 | s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1; |
e63e7fde | 708 | |
05ff6865 | 709 | s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output); |
392808b4 | 710 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE |
05ff6865 | 711 | if (!virtio_blk_data_plane_create(vdev, blk, &s->dataplane)) { |
6a1a8cc7 | 712 | virtio_cleanup(vdev); |
05ff6865 | 713 | return -1; |
392808b4 | 714 | } |
84db52d0 SH |
715 | s->migration_state_notifier.notify = virtio_blk_migration_state_changed; |
716 | add_migration_state_change_notifier(&s->migration_state_notifier); | |
392808b4 | 717 | #endif |
6e02c38d | 718 | |
69b302b2 | 719 | s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
05ff6865 | 720 | register_savevm(qdev, "virtio-blk", virtio_blk_id++, 2, |
6e02c38d | 721 | virtio_blk_save, virtio_blk_load, s); |
0e49de52 | 722 | bdrv_set_dev_ops(s->bs, &virtio_block_ops, s); |
12c5674b | 723 | bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size); |
6e02c38d | 724 | |
af239a62 | 725 | bdrv_iostatus_enable(s->bs); |
1ca4d09a | 726 | |
05ff6865 | 727 | add_boot_device_path(s->conf->bootindex, qdev, "/disk@0,0"); |
1c028ddf KF |
728 | return 0; |
729 | } | |
730 | ||
731 | static int virtio_blk_device_exit(DeviceState *dev) | |
732 | { | |
733 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
734 | VirtIOBlock *s = VIRTIO_BLK(dev); | |
735 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE | |
84db52d0 | 736 | remove_migration_state_change_notifier(&s->migration_state_notifier); |
1c028ddf KF |
737 | virtio_blk_data_plane_destroy(s->dataplane); |
738 | s->dataplane = NULL; | |
739 | #endif | |
740 | qemu_del_vm_change_state_handler(s->change); | |
2d62a957 | 741 | unregister_savevm(dev, "virtio-blk", s); |
1c028ddf | 742 | blockdev_mark_auto_del(s->bs); |
6a1a8cc7 | 743 | virtio_cleanup(vdev); |
1c028ddf KF |
744 | return 0; |
745 | } | |
746 | ||
747 | static Property virtio_blk_properties[] = { | |
748 | DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock, blk), | |
749 | DEFINE_PROP_END_OF_LIST(), | |
750 | }; | |
751 | ||
752 | static void virtio_blk_class_init(ObjectClass *klass, void *data) | |
753 | { | |
754 | DeviceClass *dc = DEVICE_CLASS(klass); | |
755 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
756 | dc->exit = virtio_blk_device_exit; | |
757 | dc->props = virtio_blk_properties; | |
125ee0ed | 758 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
1c028ddf KF |
759 | vdc->init = virtio_blk_device_init; |
760 | vdc->get_config = virtio_blk_update_config; | |
761 | vdc->set_config = virtio_blk_set_config; | |
762 | vdc->get_features = virtio_blk_get_features; | |
763 | vdc->set_status = virtio_blk_set_status; | |
764 | vdc->reset = virtio_blk_reset; | |
765 | } | |
766 | ||
767 | static const TypeInfo virtio_device_info = { | |
768 | .name = TYPE_VIRTIO_BLK, | |
769 | .parent = TYPE_VIRTIO_DEVICE, | |
770 | .instance_size = sizeof(VirtIOBlock), | |
771 | .class_init = virtio_blk_class_init, | |
772 | }; | |
773 | ||
774 | static void virtio_register_types(void) | |
775 | { | |
776 | type_register_static(&virtio_device_info); | |
777 | } | |
778 | ||
779 | type_init(virtio_register_types) |