sync with tizen_2.2
[sdk/emulator/qemu.git] / hw / 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-error.h"
16 #include "trace.h"
17 #include "hw/block-common.h"
18 #include "blockdev.h"
19 #include "virtio-transport.h"
20 #include "virtio-pci.h"
21 #include "virtio-blk.h"
22 #include "scsi-defs.h"
23 #ifdef __linux__
24 # include <scsi/sg.h>
25 #endif
26
27 typedef struct VirtIOBlock
28 {
29     VirtIODevice vdev;
30     BlockDriverState *bs;
31     VirtQueue *vq;
32     void *rq;
33     QEMUBH *bh;
34     BlockConf *conf;
35     VirtIOBlkConf *blk;
36     unsigned short sector_mask;
37     DeviceState *qdev;
38 } VirtIOBlock;
39
40 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
41 {
42     return (VirtIOBlock *)vdev;
43 }
44
45 typedef struct VirtIOBlockReq
46 {
47     VirtIOBlock *dev;
48     VirtQueueElement elem;
49     struct virtio_blk_inhdr *in;
50     struct virtio_blk_outhdr *out;
51     struct virtio_scsi_inhdr *scsi;
52     QEMUIOVector qiov;
53     struct VirtIOBlockReq *next;
54     BlockAcctCookie acct;
55 } VirtIOBlockReq;
56
57 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
58 {
59     VirtIOBlock *s = req->dev;
60
61     trace_virtio_blk_req_complete(req, status);
62
63     stb_p(&req->in->status, status);
64     virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
65     virtio_notify(&s->vdev, s->vq);
66 }
67
68 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
69     int is_read)
70 {
71     BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read);
72     VirtIOBlock *s = req->dev;
73
74     if (action == BLOCK_ERR_IGNORE) {
75         bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_IGNORE, is_read);
76         return 0;
77     }
78
79     if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
80             || action == BLOCK_ERR_STOP_ANY) {
81         req->next = s->rq;
82         s->rq = req;
83         bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_STOP, is_read);
84         vm_stop(RUN_STATE_IO_ERROR);
85         bdrv_iostatus_set_err(s->bs, error);
86     } else {
87         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
88         bdrv_acct_done(s->bs, &req->acct);
89         g_free(req);
90         bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_REPORT, is_read);
91     }
92
93     return 1;
94 }
95
96 static void virtio_blk_rw_complete(void *opaque, int ret)
97 {
98     VirtIOBlockReq *req = opaque;
99
100     trace_virtio_blk_rw_complete(req, ret);
101
102     if (ret) {
103         int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
104         if (virtio_blk_handle_rw_error(req, -ret, is_read))
105             return;
106     }
107
108     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
109     bdrv_acct_done(req->dev->bs, &req->acct);
110     g_free(req);
111 }
112
113 static void virtio_blk_flush_complete(void *opaque, int ret)
114 {
115     VirtIOBlockReq *req = opaque;
116
117     if (ret) {
118         if (virtio_blk_handle_rw_error(req, -ret, 0)) {
119             return;
120         }
121     }
122
123     virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
124     bdrv_acct_done(req->dev->bs, &req->acct);
125     g_free(req);
126 }
127
128 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
129 {
130     VirtIOBlockReq *req = g_malloc(sizeof(*req));
131     req->dev = s;
132     req->qiov.size = 0;
133     req->next = NULL;
134     return req;
135 }
136
137 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
138 {
139     VirtIOBlockReq *req = virtio_blk_alloc_request(s);
140
141     if (req != NULL) {
142         if (!virtqueue_pop(s->vq, &req->elem)) {
143             g_free(req);
144             return NULL;
145         }
146     }
147
148     return req;
149 }
150
151 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
152 {
153 #ifdef __linux__
154     int ret;
155     int i;
156 #endif
157     int status = VIRTIO_BLK_S_OK;
158
159     /*
160      * We require at least one output segment each for the virtio_blk_outhdr
161      * and the SCSI command block.
162      *
163      * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
164      * and the sense buffer pointer in the input segments.
165      */
166     if (req->elem.out_num < 2 || req->elem.in_num < 3) {
167         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
168         g_free(req);
169         return;
170     }
171
172     /*
173      * The scsi inhdr is placed in the second-to-last input segment, just
174      * before the regular inhdr.
175      */
176     req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
177
178     if (!req->dev->blk->scsi) {
179         status = VIRTIO_BLK_S_UNSUPP;
180         goto fail;
181     }
182
183     /*
184      * No support for bidirection commands yet.
185      */
186     if (req->elem.out_num > 2 && req->elem.in_num > 3) {
187         status = VIRTIO_BLK_S_UNSUPP;
188         goto fail;
189     }
190
191 #ifdef __linux__
192     struct sg_io_hdr hdr;
193     memset(&hdr, 0, sizeof(struct sg_io_hdr));
194     hdr.interface_id = 'S';
195     hdr.cmd_len = req->elem.out_sg[1].iov_len;
196     hdr.cmdp = req->elem.out_sg[1].iov_base;
197     hdr.dxfer_len = 0;
198
199     if (req->elem.out_num > 2) {
200         /*
201          * If there are more than the minimally required 2 output segments
202          * there is write payload starting from the third iovec.
203          */
204         hdr.dxfer_direction = SG_DXFER_TO_DEV;
205         hdr.iovec_count = req->elem.out_num - 2;
206
207         for (i = 0; i < hdr.iovec_count; i++)
208             hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
209
210         hdr.dxferp = req->elem.out_sg + 2;
211
212     } else if (req->elem.in_num > 3) {
213         /*
214          * If we have more than 3 input segments the guest wants to actually
215          * read data.
216          */
217         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
218         hdr.iovec_count = req->elem.in_num - 3;
219         for (i = 0; i < hdr.iovec_count; i++)
220             hdr.dxfer_len += req->elem.in_sg[i].iov_len;
221
222         hdr.dxferp = req->elem.in_sg;
223     } else {
224         /*
225          * Some SCSI commands don't actually transfer any data.
226          */
227         hdr.dxfer_direction = SG_DXFER_NONE;
228     }
229
230     hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
231     hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
232
233     ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
234     if (ret) {
235         status = VIRTIO_BLK_S_UNSUPP;
236         goto fail;
237     }
238
239     /*
240      * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
241      * clear the masked_status field [hence status gets cleared too, see
242      * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
243      * status has occurred.  However they do set DRIVER_SENSE in driver_status
244      * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
245      */
246     if (hdr.status == 0 && hdr.sb_len_wr > 0) {
247         hdr.status = CHECK_CONDITION;
248     }
249
250     stl_p(&req->scsi->errors,
251           hdr.status | (hdr.msg_status << 8) |
252           (hdr.host_status << 16) | (hdr.driver_status << 24));
253     stl_p(&req->scsi->residual, hdr.resid);
254     stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
255     stl_p(&req->scsi->data_len, hdr.dxfer_len);
256
257     virtio_blk_req_complete(req, status);
258     g_free(req);
259     return;
260 #else
261     abort();
262 #endif
263
264 fail:
265     /* Just put anything nonzero so that the ioctl fails in the guest.  */
266     stl_p(&req->scsi->errors, 255);
267     virtio_blk_req_complete(req, status);
268     g_free(req);
269 }
270
271 typedef struct MultiReqBuffer {
272     BlockRequest        blkreq[32];
273     unsigned int        num_writes;
274 } MultiReqBuffer;
275
276 static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
277 {
278     int i, ret;
279
280     if (!mrb->num_writes) {
281         return;
282     }
283
284     ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
285     if (ret != 0) {
286         for (i = 0; i < mrb->num_writes; i++) {
287             if (mrb->blkreq[i].error) {
288                 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
289             }
290         }
291     }
292
293     mrb->num_writes = 0;
294 }
295
296 static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
297 {
298     bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
299
300     /*
301      * Make sure all outstanding writes are posted to the backing device.
302      */
303     virtio_submit_multiwrite(req->dev->bs, mrb);
304     bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
305 }
306
307 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
308 {
309     BlockRequest *blkreq;
310     uint64_t sector;
311
312     sector = ldq_p(&req->out->sector);
313
314     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
315
316     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
317
318     if (sector & req->dev->sector_mask) {
319         virtio_blk_rw_complete(req, -EIO);
320         return;
321     }
322     if (req->qiov.size % req->dev->conf->logical_block_size) {
323         virtio_blk_rw_complete(req, -EIO);
324         return;
325     }
326
327     if (mrb->num_writes == 32) {
328         virtio_submit_multiwrite(req->dev->bs, mrb);
329     }
330
331     blkreq = &mrb->blkreq[mrb->num_writes];
332     blkreq->sector = sector;
333     blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
334     blkreq->qiov = &req->qiov;
335     blkreq->cb = virtio_blk_rw_complete;
336     blkreq->opaque = req;
337     blkreq->error = 0;
338
339     mrb->num_writes++;
340 }
341
342 static void virtio_blk_handle_read(VirtIOBlockReq *req)
343 {
344     uint64_t sector;
345
346     sector = ldq_p(&req->out->sector);
347
348     bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
349
350     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
351
352     if (sector & req->dev->sector_mask) {
353         virtio_blk_rw_complete(req, -EIO);
354         return;
355     }
356     if (req->qiov.size % req->dev->conf->logical_block_size) {
357         virtio_blk_rw_complete(req, -EIO);
358         return;
359     }
360     bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
361                    req->qiov.size / BDRV_SECTOR_SIZE,
362                    virtio_blk_rw_complete, req);
363 }
364
365 static void virtio_blk_handle_request(VirtIOBlockReq *req,
366     MultiReqBuffer *mrb)
367 {
368     uint32_t type;
369
370     if (req->elem.out_num < 1 || req->elem.in_num < 1) {
371         error_report("virtio-blk missing headers");
372         exit(1);
373     }
374
375     if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
376         req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
377         error_report("virtio-blk header not in correct element");
378         exit(1);
379     }
380
381     req->out = (void *)req->elem.out_sg[0].iov_base;
382     req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
383
384     type = ldl_p(&req->out->type);
385
386     if (type & VIRTIO_BLK_T_FLUSH) {
387         virtio_blk_handle_flush(req, mrb);
388     } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
389         virtio_blk_handle_scsi(req);
390     } else if (type & VIRTIO_BLK_T_GET_ID) {
391         VirtIOBlock *s = req->dev;
392
393         /*
394          * NB: per existing s/n string convention the string is
395          * terminated by '\0' only when shorter than buffer.
396          */
397         strncpy(req->elem.in_sg[0].iov_base,
398                 s->blk->serial ? s->blk->serial : "",
399                 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
400         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
401         g_free(req);
402     } else if (type & VIRTIO_BLK_T_OUT) {
403         qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
404                                  req->elem.out_num - 1);
405         virtio_blk_handle_write(req, mrb);
406     } else {
407         qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
408                                  req->elem.in_num - 1);
409         virtio_blk_handle_read(req);
410     }
411 }
412
413 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
414 {
415     VirtIOBlock *s = to_virtio_blk(vdev);
416     VirtIOBlockReq *req;
417     MultiReqBuffer mrb = {
418         .num_writes = 0,
419     };
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     if (!s->bh) {
464         s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
465         qemu_bh_schedule(s->bh);
466     }
467 }
468
469 static void virtio_blk_reset(VirtIODevice *vdev)
470 {
471     /*
472      * This should cancel pending requests, but can't do nicely until there
473      * are per-device request lists.
474      */
475     bdrv_drain_all();
476 }
477
478 /* coalesce internal state, copy to pci i/o region 0
479  */
480 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
481 {
482     VirtIOBlock *s = to_virtio_blk(vdev);
483     struct virtio_blk_config blkcfg;
484     uint64_t capacity;
485     int blk_size = s->conf->logical_block_size;
486
487     bdrv_get_geometry(s->bs, &capacity);
488     memset(&blkcfg, 0, sizeof(blkcfg));
489     stq_raw(&blkcfg.capacity, capacity);
490     stl_raw(&blkcfg.seg_max, 128 - 2);
491     stw_raw(&blkcfg.cylinders, s->conf->cyls);
492     stl_raw(&blkcfg.blk_size, blk_size);
493     stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
494     stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
495     blkcfg.heads = s->conf->heads;
496     /*
497      * We must ensure that the block device capacity is a multiple of
498      * the logical block size. If that is not the case, lets use
499      * sector_mask to adopt the geometry to have a correct picture.
500      * For those devices where the capacity is ok for the given geometry
501      * we dont touch the sector value of the geometry, since some devices
502      * (like s390 dasd) need a specific value. Here the capacity is already
503      * cyls*heads*secs*blk_size and the sector value is not block size
504      * divided by 512 - instead it is the amount of blk_size blocks
505      * per track (cylinder).
506      */
507     if (bdrv_getlength(s->bs) /  s->conf->heads / s->conf->secs % blk_size) {
508         blkcfg.sectors = s->conf->secs & ~s->sector_mask;
509     } else {
510         blkcfg.sectors = s->conf->secs;
511     }
512     blkcfg.size_max = 0;
513     blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
514     blkcfg.alignment_offset = 0;
515     blkcfg.wce = bdrv_enable_write_cache(s->bs);
516     memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
517 }
518
519 static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
520 {
521     VirtIOBlock *s = to_virtio_blk(vdev);
522     struct virtio_blk_config blkcfg;
523
524     memcpy(&blkcfg, config, sizeof(blkcfg));
525     bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
526 }
527
528 static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
529 {
530     VirtIOBlock *s = to_virtio_blk(vdev);
531
532     features |= (1 << VIRTIO_BLK_F_SEG_MAX);
533     features |= (1 << VIRTIO_BLK_F_GEOMETRY);
534     features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
535     features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
536     features |= (1 << VIRTIO_BLK_F_SCSI);
537
538     if (bdrv_enable_write_cache(s->bs))
539         features |= (1 << VIRTIO_BLK_F_WCE);
540
541     if (bdrv_is_read_only(s->bs))
542         features |= 1 << VIRTIO_BLK_F_RO;
543
544     return features;
545 }
546
547 static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
548 {
549     VirtIOBlock *s = to_virtio_blk(vdev);
550     uint32_t features;
551
552     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
553         return;
554     }
555
556     features = vdev->guest_features;
557     bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
558 }
559
560 static void virtio_blk_save(QEMUFile *f, void *opaque)
561 {
562     VirtIOBlock *s = opaque;
563     VirtIOBlockReq *req = s->rq;
564
565     virtio_save(&s->vdev, f);
566     
567     while (req) {
568         qemu_put_sbyte(f, 1);
569         qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
570         req = req->next;
571     }
572     qemu_put_sbyte(f, 0);
573 }
574
575 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
576 {
577     VirtIOBlock *s = opaque;
578     int ret;
579
580     if (version_id != 2)
581         return -EINVAL;
582
583     ret = virtio_load(&s->vdev, f);
584     if (ret) {
585         return ret;
586     }
587
588     while (qemu_get_sbyte(f)) {
589         VirtIOBlockReq *req = virtio_blk_alloc_request(s);
590         qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
591         req->next = s->rq;
592         s->rq = req;
593
594         virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
595             req->elem.in_num, 1);
596         virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
597             req->elem.out_num, 0);
598     }
599
600     return 0;
601 }
602
603 static void virtio_blk_resize(void *opaque)
604 {
605     VirtIOBlock *s = opaque;
606
607     virtio_notify_config(&s->vdev);
608 }
609
610 static const BlockDevOps virtio_block_ops = {
611     .resize_cb = virtio_blk_resize,
612 };
613
614 VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
615 {
616     VirtIOBlock *s;
617     static int virtio_blk_id;
618
619     if (!blk->conf.bs) {
620         error_report("drive property not set");
621         return NULL;
622     }
623     if (!bdrv_is_inserted(blk->conf.bs)) {
624         error_report("Device needs media, but drive is empty");
625         return NULL;
626     }
627
628     blkconf_serial(&blk->conf, &blk->serial);
629     if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
630         return NULL;
631     }
632
633     s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
634                                           sizeof(struct virtio_blk_config),
635                                           sizeof(VirtIOBlock));
636
637     s->vdev.get_config = virtio_blk_update_config;
638     s->vdev.set_config = virtio_blk_set_config;
639     s->vdev.get_features = virtio_blk_get_features;
640     s->vdev.set_status = virtio_blk_set_status;
641     s->vdev.reset = virtio_blk_reset;
642     s->bs = blk->conf.bs;
643     s->conf = &blk->conf;
644     s->blk = blk;
645     s->rq = NULL;
646     s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
647
648     s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
649
650     qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
651     s->qdev = dev;
652     register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
653                     virtio_blk_save, virtio_blk_load, s);
654     bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
655     bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
656
657     bdrv_iostatus_enable(s->bs);
658     add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
659
660     return &s->vdev;
661 }
662
663 void virtio_blk_exit(VirtIODevice *vdev)
664 {
665     VirtIOBlock *s = to_virtio_blk(vdev);
666     unregister_savevm(s->qdev, "virtio-blk", s);
667     blockdev_mark_auto_del(s->bs);
668     virtio_cleanup(vdev);
669 }
670
671 /******************** VirtIOBlk Device **********************/
672
673 static int virtio_blkdev_init(DeviceState *dev)
674 {
675     VirtIODevice *vdev;
676     VirtIOBlockState *s = VIRTIO_BLK_FROM_QDEV(dev);
677
678     assert(s->trl != NULL);
679
680     vdev = virtio_blk_init(dev, &s->blk);
681     if (!vdev) {
682         return -1;
683     }
684
685     /* Pass default host_features to transport */
686     s->trl->host_features = s->host_features;
687
688     if (virtio_call_backend_init_cb(dev, s->trl, vdev) != 0) {
689         return -1;
690     }
691
692     /* Binding should be ready here, let's get final features */
693     if (vdev->binding->get_features) {
694        s->host_features = vdev->binding->get_features(vdev->binding_opaque);
695     }
696     return 0;
697 }
698
699 static Property virtio_blkdev_properties[] = {
700     DEFINE_BLOCK_PROPERTIES(VirtIOBlockState, blk.conf),
701     DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlockState, blk.conf),
702     DEFINE_PROP_STRING("serial", VirtIOBlockState, blk.serial),
703 #ifdef __linux__
704     DEFINE_PROP_BIT("scsi", VirtIOBlockState, blk.scsi, 0, true),
705 #endif
706     DEFINE_PROP_BIT("config-wce", VirtIOBlockState, blk.config_wce, 0, true),
707     DEFINE_VIRTIO_BLK_FEATURES(VirtIOBlockState, host_features),
708
709     DEFINE_PROP_TRANSPORT("transport", VirtIOBlockState, trl),
710     DEFINE_PROP_END_OF_LIST(),
711 };
712
713 static void virtio_blkdev_class_init(ObjectClass *klass, void *data)
714 {
715     DeviceClass *dc = DEVICE_CLASS(klass);
716     dc->init = virtio_blkdev_init;
717     dc->props = virtio_blkdev_properties;
718 }
719
720 static TypeInfo virtio_blkdev_info = {
721     .name = "virtio-blk",
722     .parent = TYPE_DEVICE,
723     .instance_size = sizeof(VirtIOBlockState),
724     .class_init = virtio_blkdev_class_init,
725 };
726
727 static void virtio_blk_register_types(void)
728 {
729     type_register_static(&virtio_blkdev_info);
730 }
731
732 type_init(virtio_blk_register_types)