Revert "fix return check for KVM_GET_DIRTY_LOG ioctl"
[qemu.git] / hw / block / virtio-blk.c
blob8a568e5edb7c9d5bfa9f009de62de93a0b79cc56
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
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 #include "qemu-common.h"
15 #include "qemu/error-report.h"
16 #include "trace.h"
17 #include "hw/block/block.h"
18 #include "sysemu/blockdev.h"
19 #include "hw/virtio/virtio-blk.h"
20 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
21 # include "dataplane/virtio-blk.h"
22 # include "migration/migration.h"
23 #endif
24 #include "block/scsi.h"
25 #ifdef __linux__
26 # include <scsi/sg.h>
27 #endif
28 #include "hw/virtio/virtio-bus.h"
30 typedef struct VirtIOBlockReq
32 VirtIOBlock *dev;
33 VirtQueueElement elem;
34 struct virtio_blk_inhdr *in;
35 struct virtio_blk_outhdr *out;
36 struct virtio_scsi_inhdr *scsi;
37 QEMUIOVector qiov;
38 struct VirtIOBlockReq *next;
39 BlockAcctCookie acct;
40 } VirtIOBlockReq;
42 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
44 VirtIOBlock *s = req->dev;
45 VirtIODevice *vdev = VIRTIO_DEVICE(s);
47 trace_virtio_blk_req_complete(req, status);
49 stb_p(&req->in->status, status);
50 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
51 virtio_notify(vdev, s->vq);
54 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
55 bool is_read)
57 BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
58 VirtIOBlock *s = req->dev;
60 if (action == BDRV_ACTION_STOP) {
61 req->next = s->rq;
62 s->rq = req;
63 } else if (action == BDRV_ACTION_REPORT) {
64 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
65 bdrv_acct_done(s->bs, &req->acct);
66 g_free(req);
69 bdrv_error_action(s->bs, action, is_read, error);
70 return action != BDRV_ACTION_IGNORE;
73 static void virtio_blk_rw_complete(void *opaque, int ret)
75 VirtIOBlockReq *req = opaque;
77 trace_virtio_blk_rw_complete(req, ret);
79 if (ret) {
80 bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
81 if (virtio_blk_handle_rw_error(req, -ret, is_read))
82 return;
85 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
86 bdrv_acct_done(req->dev->bs, &req->acct);
87 g_free(req);
90 static void virtio_blk_flush_complete(void *opaque, int ret)
92 VirtIOBlockReq *req = opaque;
94 if (ret) {
95 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
96 return;
100 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
101 bdrv_acct_done(req->dev->bs, &req->acct);
102 g_free(req);
105 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
107 VirtIOBlockReq *req = g_malloc(sizeof(*req));
108 req->dev = s;
109 req->qiov.size = 0;
110 req->next = NULL;
111 return req;
114 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
116 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
118 if (req != NULL) {
119 if (!virtqueue_pop(s->vq, &req->elem)) {
120 g_free(req);
121 return NULL;
125 return req;
128 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
130 #ifdef __linux__
131 int ret;
132 int i;
133 #endif
134 int status = VIRTIO_BLK_S_OK;
137 * We require at least one output segment each for the virtio_blk_outhdr
138 * and the SCSI command block.
140 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
141 * and the sense buffer pointer in the input segments.
143 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
144 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
145 g_free(req);
146 return;
150 * The scsi inhdr is placed in the second-to-last input segment, just
151 * before the regular inhdr.
153 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
155 if (!req->dev->blk.scsi) {
156 status = VIRTIO_BLK_S_UNSUPP;
157 goto fail;
161 * No support for bidirection commands yet.
163 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
164 status = VIRTIO_BLK_S_UNSUPP;
165 goto fail;
168 #ifdef __linux__
169 struct sg_io_hdr hdr;
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;
176 if (req->elem.out_num > 2) {
178 * If there are more than the minimally required 2 output segments
179 * there is write payload starting from the third iovec.
181 hdr.dxfer_direction = SG_DXFER_TO_DEV;
182 hdr.iovec_count = req->elem.out_num - 2;
184 for (i = 0; i < hdr.iovec_count; i++)
185 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
187 hdr.dxferp = req->elem.out_sg + 2;
189 } else if (req->elem.in_num > 3) {
191 * If we have more than 3 input segments the guest wants to actually
192 * read data.
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;
199 hdr.dxferp = req->elem.in_sg;
200 } else {
202 * Some SCSI commands don't actually transfer any data.
204 hdr.dxfer_direction = SG_DXFER_NONE;
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;
210 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
211 if (ret) {
212 status = VIRTIO_BLK_S_UNSUPP;
213 goto fail;
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.
223 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
224 hdr.status = CHECK_CONDITION;
227 stl_p(&req->scsi->errors,
228 hdr.status | (hdr.msg_status << 8) |
229 (hdr.host_status << 16) | (hdr.driver_status << 24));
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);
234 virtio_blk_req_complete(req, status);
235 g_free(req);
236 return;
237 #else
238 abort();
239 #endif
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);
245 g_free(req);
248 typedef struct MultiReqBuffer {
249 BlockRequest blkreq[32];
250 unsigned int num_writes;
251 } MultiReqBuffer;
253 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
255 int i, ret;
257 if (!mrb->num_writes) {
258 return;
261 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
262 if (ret != 0) {
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);
270 mrb->num_writes = 0;
273 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
275 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
278 * Make sure all outstanding writes are posted to the backing device.
280 virtio_submit_multiwrite(req->dev->bs, mrb);
281 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
284 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
286 BlockRequest *blkreq;
287 uint64_t sector;
289 sector = ldq_p(&req->out->sector);
291 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
293 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
295 if (sector & req->dev->sector_mask) {
296 virtio_blk_rw_complete(req, -EIO);
297 return;
299 if (req->qiov.size % req->dev->conf->logical_block_size) {
300 virtio_blk_rw_complete(req, -EIO);
301 return;
304 if (mrb->num_writes == 32) {
305 virtio_submit_multiwrite(req->dev->bs, mrb);
308 blkreq = &mrb->blkreq[mrb->num_writes];
309 blkreq->sector = sector;
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;
316 mrb->num_writes++;
319 static void virtio_blk_handle_read(VirtIOBlockReq *req)
321 uint64_t sector;
323 sector = ldq_p(&req->out->sector);
325 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
327 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
329 if (sector & req->dev->sector_mask) {
330 virtio_blk_rw_complete(req, -EIO);
331 return;
333 if (req->qiov.size % req->dev->conf->logical_block_size) {
334 virtio_blk_rw_complete(req, -EIO);
335 return;
337 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
338 req->qiov.size / BDRV_SECTOR_SIZE,
339 virtio_blk_rw_complete, req);
342 static void virtio_blk_handle_request(VirtIOBlockReq *req,
343 MultiReqBuffer *mrb)
345 uint32_t type;
347 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
348 error_report("virtio-blk missing headers");
349 exit(1);
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)) {
354 error_report("virtio-blk header not in correct element");
355 exit(1);
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;
361 type = ldl_p(&req->out->type);
363 if (type & VIRTIO_BLK_T_FLUSH) {
364 virtio_blk_handle_flush(req, mrb);
365 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
366 virtio_blk_handle_scsi(req);
367 } else if (type & VIRTIO_BLK_T_GET_ID) {
368 VirtIOBlock *s = req->dev;
371 * NB: per existing s/n string convention the string is
372 * terminated by '\0' only when shorter than buffer.
374 strncpy(req->elem.in_sg[0].iov_base,
375 s->blk.serial ? s->blk.serial : "",
376 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
377 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
378 g_free(req);
379 } else if (type & VIRTIO_BLK_T_OUT) {
380 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
381 req->elem.out_num - 1);
382 virtio_blk_handle_write(req, mrb);
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. */
385 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
386 req->elem.in_num - 1);
387 virtio_blk_handle_read(req);
388 } else {
389 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
390 g_free(req);
394 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
396 VirtIOBlock *s = VIRTIO_BLK(vdev);
397 VirtIOBlockReq *req;
398 MultiReqBuffer mrb = {
399 .num_writes = 0,
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().
406 if (s->dataplane) {
407 virtio_blk_data_plane_start(s->dataplane);
408 return;
410 #endif
412 while ((req = virtio_blk_get_request(s))) {
413 virtio_blk_handle_request(req, &mrb);
416 virtio_submit_multiwrite(s->bs, &mrb);
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.
425 static void virtio_blk_dma_restart_bh(void *opaque)
427 VirtIOBlock *s = opaque;
428 VirtIOBlockReq *req = s->rq;
429 MultiReqBuffer mrb = {
430 .num_writes = 0,
433 qemu_bh_delete(s->bh);
434 s->bh = NULL;
436 s->rq = NULL;
438 while (req) {
439 virtio_blk_handle_request(req, &mrb);
440 req = req->next;
443 virtio_submit_multiwrite(s->bs, &mrb);
446 static void virtio_blk_dma_restart_cb(void *opaque, int running,
447 RunState state)
449 VirtIOBlock *s = opaque;
451 if (!running) {
452 return;
455 if (!s->bh) {
456 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
457 qemu_bh_schedule(s->bh);
461 static void virtio_blk_reset(VirtIODevice *vdev)
463 VirtIOBlock *s = VIRTIO_BLK(vdev);
465 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
466 if (s->dataplane) {
467 virtio_blk_data_plane_stop(s->dataplane);
469 #endif
472 * This should cancel pending requests, but can't do nicely until there
473 * are per-device request lists.
475 bdrv_drain_all();
476 bdrv_set_enable_write_cache(s->bs, s->original_wce);
479 /* coalesce internal state, copy to pci i/o region 0
481 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
483 VirtIOBlock *s = VIRTIO_BLK(vdev);
484 struct virtio_blk_config blkcfg;
485 uint64_t capacity;
486 int blk_size = s->conf->logical_block_size;
488 bdrv_get_geometry(s->bs, &capacity);
489 memset(&blkcfg, 0, sizeof(blkcfg));
490 stq_raw(&blkcfg.capacity, capacity);
491 stl_raw(&blkcfg.seg_max, 128 - 2);
492 stw_raw(&blkcfg.cylinders, s->conf->cyls);
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);
496 blkcfg.heads = s->conf->heads;
498 * We must ensure that the block device capacity is a multiple of
499 * the logical block size. If that is not the case, let's use
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
502 * we don't touch the sector value of the geometry, since some devices
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).
508 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
509 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
510 } else {
511 blkcfg.sectors = s->conf->secs;
513 blkcfg.size_max = 0;
514 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
515 blkcfg.alignment_offset = 0;
516 blkcfg.wce = bdrv_enable_write_cache(s->bs);
517 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
520 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
522 VirtIOBlock *s = VIRTIO_BLK(vdev);
523 struct virtio_blk_config blkcfg;
525 memcpy(&blkcfg, config, sizeof(blkcfg));
526 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
529 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
531 VirtIOBlock *s = VIRTIO_BLK(vdev);
533 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
534 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
535 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
536 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
537 features |= (1 << VIRTIO_BLK_F_SCSI);
539 if (s->blk.config_wce) {
540 features |= (1 << VIRTIO_BLK_F_CONFIG_WCE);
542 if (bdrv_enable_write_cache(s->bs))
543 features |= (1 << VIRTIO_BLK_F_WCE);
545 if (bdrv_is_read_only(s->bs))
546 features |= 1 << VIRTIO_BLK_F_RO;
548 return features;
551 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
553 VirtIOBlock *s = VIRTIO_BLK(vdev);
554 uint32_t features;
556 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
557 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
558 VIRTIO_CONFIG_S_DRIVER_OK))) {
559 virtio_blk_data_plane_stop(s->dataplane);
561 #endif
563 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
564 return;
567 features = vdev->guest_features;
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:
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
582 * s->bs would erroneously be placed in writethrough mode.
584 if (!(features & (1 << VIRTIO_BLK_F_CONFIG_WCE))) {
585 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
589 static void virtio_blk_save(QEMUFile *f, void *opaque)
591 VirtIOBlock *s = opaque;
592 VirtIODevice *vdev = VIRTIO_DEVICE(s);
593 VirtIOBlockReq *req = s->rq;
595 virtio_save(vdev, f);
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;
602 qemu_put_sbyte(f, 0);
605 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
607 VirtIOBlock *s = opaque;
608 VirtIODevice *vdev = VIRTIO_DEVICE(s);
609 int ret;
611 if (version_id != 2)
612 return -EINVAL;
614 ret = virtio_load(vdev, f);
615 if (ret) {
616 return ret;
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;
623 s->rq = req;
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);
631 return 0;
634 static void virtio_blk_resize(void *opaque)
636 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
638 virtio_notify_config(vdev);
641 static const BlockDevOps virtio_block_ops = {
642 .resize_cb = virtio_blk_resize,
645 void virtio_blk_set_conf(DeviceState *dev, VirtIOBlkConf *blk)
647 VirtIOBlock *s = VIRTIO_BLK(dev);
648 memcpy(&(s->blk), blk, sizeof(struct VirtIOBlkConf));
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.
655 static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
657 VirtIOBlock *s = container_of(notifier, VirtIOBlock,
658 migration_state_notifier);
659 MigrationState *mig = data;
660 Error *err = NULL;
662 if (migration_in_setup(mig)) {
663 if (!s->dataplane) {
664 return;
666 virtio_blk_data_plane_destroy(s->dataplane);
667 s->dataplane = NULL;
668 } else if (migration_has_finished(mig) ||
669 migration_has_failed(mig)) {
670 if (s->dataplane) {
671 return;
673 bdrv_drain_all(); /* complete in-flight non-dataplane requests */
674 virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->blk,
675 &s->dataplane, &err);
676 if (err != NULL) {
677 error_report("%s", error_get_pretty(err));
678 error_free(err);
682 #endif /* CONFIG_VIRTIO_BLK_DATA_PLANE */
684 static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
686 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
687 VirtIOBlock *s = VIRTIO_BLK(dev);
688 VirtIOBlkConf *blk = &(s->blk);
689 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
690 Error *err = NULL;
691 #endif
692 static int virtio_blk_id;
694 if (!blk->conf.bs) {
695 error_setg(errp, "drive property not set");
696 return;
698 if (!bdrv_is_inserted(blk->conf.bs)) {
699 error_setg(errp, "Device needs media, but drive is empty");
700 return;
703 blkconf_serial(&blk->conf, &blk->serial);
704 s->original_wce = bdrv_enable_write_cache(blk->conf.bs);
705 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
706 error_setg(errp, "Error setting geometry");
707 return;
710 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
711 sizeof(struct virtio_blk_config));
713 s->bs = blk->conf.bs;
714 s->conf = &blk->conf;
715 s->rq = NULL;
716 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
718 s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
719 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
720 virtio_blk_data_plane_create(vdev, blk, &s->dataplane, &err);
721 if (err != NULL) {
722 error_propagate(errp, err);
723 virtio_cleanup(vdev);
724 return;
726 s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
727 add_migration_state_change_notifier(&s->migration_state_notifier);
728 #endif
730 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
731 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
732 virtio_blk_save, virtio_blk_load, s);
733 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
734 bdrv_set_guest_block_size(s->bs, s->conf->logical_block_size);
736 bdrv_iostatus_enable(s->bs);
738 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
741 static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
743 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
744 VirtIOBlock *s = VIRTIO_BLK(dev);
746 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
747 remove_migration_state_change_notifier(&s->migration_state_notifier);
748 virtio_blk_data_plane_destroy(s->dataplane);
749 s->dataplane = NULL;
750 #endif
751 qemu_del_vm_change_state_handler(s->change);
752 unregister_savevm(dev, "virtio-blk", s);
753 blockdev_mark_auto_del(s->bs);
754 virtio_cleanup(vdev);
757 static Property virtio_blk_properties[] = {
758 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlock, blk),
759 DEFINE_PROP_END_OF_LIST(),
762 static void virtio_blk_class_init(ObjectClass *klass, void *data)
764 DeviceClass *dc = DEVICE_CLASS(klass);
765 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
767 dc->props = virtio_blk_properties;
768 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
769 vdc->realize = virtio_blk_device_realize;
770 vdc->unrealize = virtio_blk_device_unrealize;
771 vdc->get_config = virtio_blk_update_config;
772 vdc->set_config = virtio_blk_set_config;
773 vdc->get_features = virtio_blk_get_features;
774 vdc->set_status = virtio_blk_set_status;
775 vdc->reset = virtio_blk_reset;
778 static const TypeInfo virtio_device_info = {
779 .name = TYPE_VIRTIO_BLK,
780 .parent = TYPE_VIRTIO_DEVICE,
781 .instance_size = sizeof(VirtIOBlock),
782 .class_init = virtio_blk_class_init,
785 static void virtio_register_types(void)
787 type_register_static(&virtio_device_info);
790 type_init(virtio_register_types)