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