aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 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 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 14 | #include <qemu-common.h> |
Markus Armbruster | d75d25e | 2010-07-06 14:37:43 +0200 | [diff] [blame] | 15 | #include "qemu-error.h" |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 16 | #include "trace.h" |
Blue Swirl | 2446333 | 2010-08-24 15:22:24 +0000 | [diff] [blame] | 17 | #include "blockdev.h" |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 18 | #include "virtio-blk.h" |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 19 | #ifdef __linux__ |
| 20 | # include <scsi/sg.h> |
| 21 | #endif |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 22 | |
| 23 | typedef struct VirtIOBlock |
| 24 | { |
| 25 | VirtIODevice vdev; |
| 26 | BlockDriverState *bs; |
| 27 | VirtQueue *vq; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 28 | void *rq; |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 29 | QEMUBH *bh; |
Christoph Hellwig | 9752c37 | 2010-02-10 23:37:25 +0100 | [diff] [blame] | 30 | BlockConf *conf; |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 31 | char *serial; |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 32 | unsigned short sector_mask; |
Alex Williamson | 9d0d313 | 2010-07-20 11:14:22 -0600 | [diff] [blame] | 33 | DeviceState *qdev; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 34 | } VirtIOBlock; |
| 35 | |
| 36 | static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev) |
| 37 | { |
| 38 | return (VirtIOBlock *)vdev; |
| 39 | } |
| 40 | |
| 41 | typedef struct VirtIOBlockReq |
| 42 | { |
| 43 | VirtIOBlock *dev; |
| 44 | VirtQueueElement elem; |
| 45 | struct virtio_blk_inhdr *in; |
| 46 | struct virtio_blk_outhdr *out; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 47 | struct virtio_scsi_inhdr *scsi; |
aliguori | d28a1b6 | 2009-03-28 17:46:14 +0000 | [diff] [blame] | 48 | QEMUIOVector qiov; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 49 | struct VirtIOBlockReq *next; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 50 | } VirtIOBlockReq; |
| 51 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 52 | static void virtio_blk_req_complete(VirtIOBlockReq *req, int status) |
| 53 | { |
| 54 | VirtIOBlock *s = req->dev; |
| 55 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 56 | trace_virtio_blk_req_complete(req, status); |
| 57 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 58 | stb_p(&req->in->status, status); |
aliguori | d28a1b6 | 2009-03-28 17:46:14 +0000 | [diff] [blame] | 59 | virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in)); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 60 | virtio_notify(&s->vdev, s->vq); |
| 61 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 62 | g_free(req); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 65 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
| 66 | int is_read) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 67 | { |
Markus Armbruster | abd7f68 | 2010-06-02 18:55:17 +0200 | [diff] [blame] | 68 | BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 69 | VirtIOBlock *s = req->dev; |
| 70 | |
Luiz Capitulino | eaa6c85 | 2010-02-03 12:41:04 -0200 | [diff] [blame] | 71 | if (action == BLOCK_ERR_IGNORE) { |
Kevin Wolf | 908bb94 | 2010-03-31 17:46:59 +0200 | [diff] [blame] | 72 | bdrv_mon_event(s->bs, BDRV_ACTION_IGNORE, is_read); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 73 | return 0; |
Luiz Capitulino | eaa6c85 | 2010-02-03 12:41:04 -0200 | [diff] [blame] | 74 | } |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 75 | |
| 76 | if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC) |
| 77 | || action == BLOCK_ERR_STOP_ANY) { |
| 78 | req->next = s->rq; |
| 79 | s->rq = req; |
Kevin Wolf | 908bb94 | 2010-03-31 17:46:59 +0200 | [diff] [blame] | 80 | bdrv_mon_event(s->bs, BDRV_ACTION_STOP, is_read); |
Jan Kiszka | e07bbac | 2011-02-09 16:29:40 +0100 | [diff] [blame] | 81 | vm_stop(VMSTOP_DISKFULL); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 82 | } else { |
| 83 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
Kevin Wolf | 908bb94 | 2010-03-31 17:46:59 +0200 | [diff] [blame] | 84 | bdrv_mon_event(s->bs, BDRV_ACTION_REPORT, is_read); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 90 | static void virtio_blk_rw_complete(void *opaque, int ret) |
| 91 | { |
| 92 | VirtIOBlockReq *req = opaque; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 93 | |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 94 | trace_virtio_blk_rw_complete(req, ret); |
| 95 | |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 96 | if (ret) { |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 97 | int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT); |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 98 | if (virtio_blk_handle_rw_error(req, -ret, is_read)) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 99 | return; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 102 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 103 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 104 | |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 105 | static void virtio_blk_flush_complete(void *opaque, int ret) |
| 106 | { |
| 107 | VirtIOBlockReq *req = opaque; |
| 108 | |
Kevin Wolf | 8c269b5 | 2010-10-20 13:17:30 +0200 | [diff] [blame] | 109 | if (ret) { |
| 110 | if (virtio_blk_handle_rw_error(req, -ret, 0)) { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 116 | } |
| 117 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 118 | static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s) |
| 119 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 120 | VirtIOBlockReq *req = g_malloc(sizeof(*req)); |
aliguori | 487414f | 2009-02-05 22:06:05 +0000 | [diff] [blame] | 121 | req->dev = s; |
Stefan Hajnoczi | de6c804 | 2010-05-14 22:52:30 +0100 | [diff] [blame] | 122 | req->qiov.size = 0; |
| 123 | req->next = NULL; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 124 | return req; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s) |
| 128 | { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 129 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 130 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 131 | if (req != NULL) { |
| 132 | if (!virtqueue_pop(s->vq, &req->elem)) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 133 | g_free(req); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return req; |
| 139 | } |
| 140 | |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 141 | #ifdef __linux__ |
| 142 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
| 143 | { |
| 144 | struct sg_io_hdr hdr; |
Christoph Hellwig | 4277906 | 2010-01-13 13:30:32 +0100 | [diff] [blame] | 145 | int ret; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 146 | int status; |
| 147 | int i; |
| 148 | |
| 149 | /* |
| 150 | * We require at least one output segment each for the virtio_blk_outhdr |
| 151 | * and the SCSI command block. |
| 152 | * |
| 153 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr |
| 154 | * and the sense buffer pointer in the input segments. |
| 155 | */ |
| 156 | if (req->elem.out_num < 2 || req->elem.in_num < 3) { |
| 157 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * No support for bidirection commands yet. |
| 163 | */ |
| 164 | if (req->elem.out_num > 2 && req->elem.in_num > 3) { |
| 165 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * The scsi inhdr is placed in the second-to-last input segment, just |
| 171 | * before the regular inhdr. |
| 172 | */ |
| 173 | req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 174 | |
| 175 | memset(&hdr, 0, sizeof(struct sg_io_hdr)); |
| 176 | hdr.interface_id = 'S'; |
| 177 | hdr.cmd_len = req->elem.out_sg[1].iov_len; |
| 178 | hdr.cmdp = req->elem.out_sg[1].iov_base; |
| 179 | hdr.dxfer_len = 0; |
| 180 | |
| 181 | if (req->elem.out_num > 2) { |
| 182 | /* |
| 183 | * If there are more than the minimally required 2 output segments |
| 184 | * there is write payload starting from the third iovec. |
| 185 | */ |
| 186 | hdr.dxfer_direction = SG_DXFER_TO_DEV; |
| 187 | hdr.iovec_count = req->elem.out_num - 2; |
| 188 | |
| 189 | for (i = 0; i < hdr.iovec_count; i++) |
| 190 | hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len; |
| 191 | |
| 192 | hdr.dxferp = req->elem.out_sg + 2; |
| 193 | |
| 194 | } else if (req->elem.in_num > 3) { |
| 195 | /* |
| 196 | * If we have more than 3 input segments the guest wants to actually |
| 197 | * read data. |
| 198 | */ |
| 199 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; |
| 200 | hdr.iovec_count = req->elem.in_num - 3; |
| 201 | for (i = 0; i < hdr.iovec_count; i++) |
| 202 | hdr.dxfer_len += req->elem.in_sg[i].iov_len; |
| 203 | |
| 204 | hdr.dxferp = req->elem.in_sg; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 205 | } else { |
| 206 | /* |
| 207 | * Some SCSI commands don't actually transfer any data. |
| 208 | */ |
| 209 | hdr.dxfer_direction = SG_DXFER_NONE; |
| 210 | } |
| 211 | |
| 212 | hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base; |
| 213 | hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 214 | |
| 215 | ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr); |
| 216 | if (ret) { |
| 217 | status = VIRTIO_BLK_S_UNSUPP; |
| 218 | hdr.status = ret; |
| 219 | hdr.resid = hdr.dxfer_len; |
| 220 | } else if (hdr.status) { |
| 221 | status = VIRTIO_BLK_S_IOERR; |
| 222 | } else { |
| 223 | status = VIRTIO_BLK_S_OK; |
| 224 | } |
| 225 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 226 | stl_p(&req->scsi->errors, hdr.status); |
| 227 | stl_p(&req->scsi->residual, hdr.resid); |
| 228 | stl_p(&req->scsi->sense_len, hdr.sb_len_wr); |
| 229 | stl_p(&req->scsi->data_len, hdr.dxfer_len); |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 230 | |
| 231 | virtio_blk_req_complete(req, status); |
| 232 | } |
| 233 | #else |
| 234 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
| 235 | { |
| 236 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
| 237 | } |
| 238 | #endif /* __linux__ */ |
| 239 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 240 | typedef struct MultiReqBuffer { |
| 241 | BlockRequest blkreq[32]; |
| 242 | unsigned int num_writes; |
| 243 | } MultiReqBuffer; |
| 244 | |
| 245 | static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 246 | { |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 247 | int i, ret; |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 248 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 249 | if (!mrb->num_writes) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes); |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 254 | if (ret != 0) { |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 255 | for (i = 0; i < mrb->num_writes; i++) { |
| 256 | if (mrb->blkreq[i].error) { |
| 257 | virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO); |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 260 | } |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 261 | |
| 262 | mrb->num_writes = 0; |
aliguori | d28a1b6 | 2009-03-28 17:46:14 +0000 | [diff] [blame] | 263 | } |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 264 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 265 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 266 | { |
| 267 | BlockDriverAIOCB *acb; |
| 268 | |
Christoph Hellwig | 618fbb8 | 2010-05-19 12:40:09 +0200 | [diff] [blame] | 269 | /* |
| 270 | * Make sure all outstanding writes are posted to the backing device. |
| 271 | */ |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 272 | virtio_submit_multiwrite(req->dev->bs, mrb); |
Christoph Hellwig | 618fbb8 | 2010-05-19 12:40:09 +0200 | [diff] [blame] | 273 | |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 274 | acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req); |
| 275 | if (!acb) { |
Kevin Wolf | 18a8d42 | 2010-10-27 13:10:15 +0200 | [diff] [blame] | 276 | virtio_blk_flush_complete(req, -EIO); |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 280 | static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 281 | { |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 282 | BlockRequest *blkreq; |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 283 | uint64_t sector; |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 284 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 285 | sector = ldq_p(&req->out->sector); |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 286 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 287 | trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512); |
| 288 | |
| 289 | if (sector & req->dev->sector_mask) { |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 290 | virtio_blk_rw_complete(req, -EIO); |
| 291 | return; |
| 292 | } |
Christoph Hellwig | 52c0502 | 2011-04-06 20:28:34 +0200 | [diff] [blame] | 293 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
| 294 | virtio_blk_rw_complete(req, -EIO); |
| 295 | return; |
| 296 | } |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 297 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 298 | if (mrb->num_writes == 32) { |
| 299 | virtio_submit_multiwrite(req->dev->bs, mrb); |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 302 | blkreq = &mrb->blkreq[mrb->num_writes]; |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 303 | blkreq->sector = sector; |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 304 | blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE; |
| 305 | blkreq->qiov = &req->qiov; |
| 306 | blkreq->cb = virtio_blk_rw_complete; |
| 307 | blkreq->opaque = req; |
| 308 | blkreq->error = 0; |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 309 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 310 | mrb->num_writes++; |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 311 | } |
| 312 | |
aliguori | d28a1b6 | 2009-03-28 17:46:14 +0000 | [diff] [blame] | 313 | static void virtio_blk_handle_read(VirtIOBlockReq *req) |
| 314 | { |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 315 | BlockDriverAIOCB *acb; |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 316 | uint64_t sector; |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 317 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 318 | sector = ldq_p(&req->out->sector); |
| 319 | |
| 320 | if (sector & req->dev->sector_mask) { |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 321 | virtio_blk_rw_complete(req, -EIO); |
| 322 | return; |
| 323 | } |
Christoph Hellwig | 52c0502 | 2011-04-06 20:28:34 +0200 | [diff] [blame] | 324 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
| 325 | virtio_blk_rw_complete(req, -EIO); |
| 326 | return; |
| 327 | } |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 328 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 329 | acb = bdrv_aio_readv(req->dev->bs, sector, &req->qiov, |
Jes Sorensen | 1573a35 | 2010-05-27 16:20:33 +0200 | [diff] [blame] | 330 | req->qiov.size / BDRV_SECTOR_SIZE, |
| 331 | virtio_blk_rw_complete, req); |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 332 | if (!acb) { |
Kevin Wolf | 6c510fb | 2010-01-27 13:12:36 +0100 | [diff] [blame] | 333 | virtio_blk_rw_complete(req, -EIO); |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 334 | } |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 337 | static void virtio_blk_handle_request(VirtIOBlockReq *req, |
| 338 | MultiReqBuffer *mrb) |
| 339 | { |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 340 | uint32_t type; |
| 341 | |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 342 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
Stefan Hajnoczi | 870cef1 | 2010-11-15 20:44:35 +0000 | [diff] [blame] | 343 | error_report("virtio-blk missing headers"); |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 344 | exit(1); |
| 345 | } |
| 346 | |
| 347 | if (req->elem.out_sg[0].iov_len < sizeof(*req->out) || |
| 348 | req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) { |
Stefan Hajnoczi | 870cef1 | 2010-11-15 20:44:35 +0000 | [diff] [blame] | 349 | error_report("virtio-blk header not in correct element"); |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 350 | exit(1); |
| 351 | } |
| 352 | |
| 353 | req->out = (void *)req->elem.out_sg[0].iov_base; |
| 354 | req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base; |
| 355 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 356 | type = ldl_p(&req->out->type); |
| 357 | |
| 358 | if (type & VIRTIO_BLK_T_FLUSH) { |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 359 | virtio_blk_handle_flush(req, mrb); |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 360 | } else if (type & VIRTIO_BLK_T_SCSI_CMD) { |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 361 | virtio_blk_handle_scsi(req); |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 362 | } else if (type & VIRTIO_BLK_T_GET_ID) { |
john cooper | 2930b31 | 2010-07-02 13:44:25 -0400 | [diff] [blame] | 363 | VirtIOBlock *s = req->dev; |
| 364 | |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 365 | /* |
| 366 | * NB: per existing s/n string convention the string is |
| 367 | * terminated by '\0' only when shorter than buffer. |
| 368 | */ |
| 369 | strncpy(req->elem.in_sg[0].iov_base, |
| 370 | s->serial ? s->serial : "", |
| 371 | MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES)); |
john cooper | 2930b31 | 2010-07-02 13:44:25 -0400 | [diff] [blame] | 372 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 373 | } else if (type & VIRTIO_BLK_T_OUT) { |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 374 | qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1], |
| 375 | req->elem.out_num - 1); |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 376 | virtio_blk_handle_write(req, mrb); |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 377 | } else { |
| 378 | qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0], |
| 379 | req->elem.in_num - 1); |
| 380 | virtio_blk_handle_read(req); |
| 381 | } |
| 382 | } |
| 383 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 384 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 385 | { |
| 386 | VirtIOBlock *s = to_virtio_blk(vdev); |
| 387 | VirtIOBlockReq *req; |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 388 | MultiReqBuffer mrb = { |
| 389 | .num_writes = 0, |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 390 | }; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 391 | |
| 392 | while ((req = virtio_blk_get_request(s))) { |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 393 | virtio_blk_handle_request(req, &mrb); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 394 | } |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 395 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 396 | virtio_submit_multiwrite(s->bs, &mrb); |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 397 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 398 | /* |
| 399 | * FIXME: Want to check for completions before returning to guest mode, |
| 400 | * so cached reads and writes are reported as quickly as possible. But |
| 401 | * that should be done in the generic block layer. |
| 402 | */ |
| 403 | } |
| 404 | |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 405 | static void virtio_blk_dma_restart_bh(void *opaque) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 406 | { |
| 407 | VirtIOBlock *s = opaque; |
| 408 | VirtIOBlockReq *req = s->rq; |
Kevin Wolf | f1b5286 | 2010-01-27 13:12:35 +0100 | [diff] [blame] | 409 | MultiReqBuffer mrb = { |
| 410 | .num_writes = 0, |
Kevin Wolf | f1b5286 | 2010-01-27 13:12:35 +0100 | [diff] [blame] | 411 | }; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 412 | |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 413 | qemu_bh_delete(s->bh); |
| 414 | s->bh = NULL; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 415 | |
| 416 | s->rq = NULL; |
| 417 | |
| 418 | while (req) { |
Kevin Wolf | f1b5286 | 2010-01-27 13:12:35 +0100 | [diff] [blame] | 419 | virtio_blk_handle_request(req, &mrb); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 420 | req = req->next; |
| 421 | } |
Kevin Wolf | f1b5286 | 2010-01-27 13:12:35 +0100 | [diff] [blame] | 422 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 423 | virtio_submit_multiwrite(s->bs, &mrb); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 426 | static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason) |
| 427 | { |
| 428 | VirtIOBlock *s = opaque; |
| 429 | |
| 430 | if (!running) |
| 431 | return; |
| 432 | |
| 433 | if (!s->bh) { |
| 434 | s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s); |
| 435 | qemu_bh_schedule(s->bh); |
| 436 | } |
| 437 | } |
| 438 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 439 | static void virtio_blk_reset(VirtIODevice *vdev) |
| 440 | { |
| 441 | /* |
| 442 | * This should cancel pending requests, but can't do nicely until there |
| 443 | * are per-device request lists. |
| 444 | */ |
| 445 | qemu_aio_flush(); |
| 446 | } |
| 447 | |
john cooper | bf01129 | 2009-06-22 14:26:51 -0400 | [diff] [blame] | 448 | /* coalesce internal state, copy to pci i/o region 0 |
| 449 | */ |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 450 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
| 451 | { |
| 452 | VirtIOBlock *s = to_virtio_blk(vdev); |
| 453 | struct virtio_blk_config blkcfg; |
| 454 | uint64_t capacity; |
| 455 | int cylinders, heads, secs; |
| 456 | |
| 457 | bdrv_get_geometry(s->bs, &capacity); |
| 458 | bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs); |
Gerd Hoffmann | 5c5dafd | 2009-06-12 09:50:18 +0200 | [diff] [blame] | 459 | memset(&blkcfg, 0, sizeof(blkcfg)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 460 | stq_raw(&blkcfg.capacity, capacity); |
| 461 | stl_raw(&blkcfg.seg_max, 128 - 2); |
| 462 | stw_raw(&blkcfg.cylinders, cylinders); |
| 463 | blkcfg.heads = heads; |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 464 | blkcfg.sectors = secs & ~s->sector_mask; |
| 465 | blkcfg.blk_size = s->conf->logical_block_size; |
Blue Swirl | c7085da | 2009-06-13 13:20:25 +0000 | [diff] [blame] | 466 | blkcfg.size_max = 0; |
Christoph Hellwig | 9752c37 | 2010-02-10 23:37:25 +0100 | [diff] [blame] | 467 | blkcfg.physical_block_exp = get_physical_block_exp(s->conf); |
| 468 | blkcfg.alignment_offset = 0; |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 469 | blkcfg.min_io_size = s->conf->min_io_size / blkcfg.blk_size; |
| 470 | blkcfg.opt_io_size = s->conf->opt_io_size / blkcfg.blk_size; |
hch@lst.de | 37d5ddd | 2010-02-10 23:36:49 +0100 | [diff] [blame] | 471 | memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 474 | static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 475 | { |
john cooper | bf01129 | 2009-06-22 14:26:51 -0400 | [diff] [blame] | 476 | VirtIOBlock *s = to_virtio_blk(vdev); |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 477 | |
| 478 | features |= (1 << VIRTIO_BLK_F_SEG_MAX); |
| 479 | features |= (1 << VIRTIO_BLK_F_GEOMETRY); |
Christoph Hellwig | 9752c37 | 2010-02-10 23:37:25 +0100 | [diff] [blame] | 480 | features |= (1 << VIRTIO_BLK_F_TOPOLOGY); |
Christoph Hellwig | 8cfacf0 | 2010-03-04 14:20:17 +0100 | [diff] [blame] | 481 | features |= (1 << VIRTIO_BLK_F_BLK_SIZE); |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 482 | |
| 483 | if (bdrv_enable_write_cache(s->bs)) |
| 484 | features |= (1 << VIRTIO_BLK_F_WCACHE); |
Naphtali Sprei | c79662f | 2009-10-29 11:42:11 +0200 | [diff] [blame] | 485 | |
| 486 | if (bdrv_is_read_only(s->bs)) |
| 487 | features |= 1 << VIRTIO_BLK_F_RO; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 488 | |
| 489 | return features; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static void virtio_blk_save(QEMUFile *f, void *opaque) |
| 493 | { |
| 494 | VirtIOBlock *s = opaque; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 495 | VirtIOBlockReq *req = s->rq; |
| 496 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 497 | virtio_save(&s->vdev, f); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 498 | |
| 499 | while (req) { |
| 500 | qemu_put_sbyte(f, 1); |
| 501 | qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); |
| 502 | req = req->next; |
| 503 | } |
| 504 | qemu_put_sbyte(f, 0); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) |
| 508 | { |
| 509 | VirtIOBlock *s = opaque; |
| 510 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 511 | if (version_id != 2) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | |
| 514 | virtio_load(&s->vdev, f); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 515 | while (qemu_get_sbyte(f)) { |
| 516 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
| 517 | qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); |
| 518 | req->next = s->rq; |
Yoshiaki Tamura | 20a81e4 | 2010-06-21 17:50:01 +0900 | [diff] [blame] | 519 | s->rq = req; |
Kevin Wolf | b6a4805 | 2010-08-03 16:57:02 +0200 | [diff] [blame] | 520 | |
| 521 | virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr, |
| 522 | req->elem.in_num, 1); |
| 523 | virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr, |
| 524 | req->elem.out_num, 0); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 525 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Christoph Hellwig | e5051fc | 2011-01-24 13:32:51 +0100 | [diff] [blame] | 530 | static void virtio_blk_change_cb(void *opaque, int reason) |
| 531 | { |
| 532 | VirtIOBlock *s = opaque; |
| 533 | |
| 534 | if (reason & CHANGE_SIZE) { |
| 535 | virtio_notify_config(&s->vdev); |
| 536 | } |
| 537 | } |
| 538 | |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 539 | VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf, |
| 540 | char **serial) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 541 | { |
| 542 | VirtIOBlock *s; |
| 543 | int cylinders, heads, secs; |
| 544 | static int virtio_blk_id; |
john cooper | 2930b31 | 2010-07-02 13:44:25 -0400 | [diff] [blame] | 545 | DriveInfo *dinfo; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 546 | |
Markus Armbruster | d75d25e | 2010-07-06 14:37:43 +0200 | [diff] [blame] | 547 | if (!conf->bs) { |
| 548 | error_report("virtio-blk-pci: drive property not set"); |
| 549 | return NULL; |
| 550 | } |
Markus Armbruster | 98f28ad | 2010-07-06 14:37:44 +0200 | [diff] [blame] | 551 | if (!bdrv_is_inserted(conf->bs)) { |
| 552 | error_report("Device needs media, but drive is empty"); |
| 553 | return NULL; |
| 554 | } |
Markus Armbruster | d75d25e | 2010-07-06 14:37:43 +0200 | [diff] [blame] | 555 | |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 556 | if (!*serial) { |
| 557 | /* try to fall back to value set with legacy -drive serial=... */ |
| 558 | dinfo = drive_get_by_blockdev(conf->bs); |
| 559 | if (*dinfo->serial) { |
| 560 | *serial = strdup(dinfo->serial); |
| 561 | } |
| 562 | } |
| 563 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 564 | s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK, |
hch@lst.de | 37d5ddd | 2010-02-10 23:36:49 +0100 | [diff] [blame] | 565 | sizeof(struct virtio_blk_config), |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 566 | sizeof(VirtIOBlock)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 567 | |
| 568 | s->vdev.get_config = virtio_blk_update_config; |
| 569 | s->vdev.get_features = virtio_blk_get_features; |
| 570 | s->vdev.reset = virtio_blk_reset; |
Markus Armbruster | f8b6cc0 | 2010-05-05 16:36:52 +0200 | [diff] [blame] | 571 | s->bs = conf->bs; |
Christoph Hellwig | 9752c37 | 2010-02-10 23:37:25 +0100 | [diff] [blame] | 572 | s->conf = conf; |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 573 | s->serial = *serial; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 574 | s->rq = NULL; |
Jes Sorensen | 1573a35 | 2010-05-27 16:20:33 +0200 | [diff] [blame] | 575 | s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 576 | bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 577 | |
| 578 | s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output); |
| 579 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 580 | qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
Alex Williamson | 9d0d313 | 2010-07-20 11:14:22 -0600 | [diff] [blame] | 581 | s->qdev = dev; |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 582 | register_savevm(dev, "virtio-blk", virtio_blk_id++, 2, |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 583 | virtio_blk_save, virtio_blk_load, s); |
Markus Armbruster | 7d0d695 | 2010-06-25 13:42:14 +0200 | [diff] [blame] | 584 | bdrv_set_removable(s->bs, 0); |
Christoph Hellwig | e5051fc | 2011-01-24 13:32:51 +0100 | [diff] [blame] | 585 | bdrv_set_change_cb(s->bs, virtio_blk_change_cb, s); |
Christoph Hellwig | 316a7af | 2010-09-12 23:43:39 +0200 | [diff] [blame] | 586 | s->bs->buffer_alignment = conf->logical_block_size; |
Paul Brook | 07e3af9 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 587 | |
Gleb Natapov | 1ca4d09 | 2010-12-08 13:35:05 +0200 | [diff] [blame] | 588 | add_boot_device_path(conf->bootindex, dev, "/disk@0,0"); |
| 589 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 590 | return &s->vdev; |
Paul Brook | 07e3af9 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 591 | } |
Alex Williamson | 9d0d313 | 2010-07-20 11:14:22 -0600 | [diff] [blame] | 592 | |
| 593 | void virtio_blk_exit(VirtIODevice *vdev) |
| 594 | { |
| 595 | VirtIOBlock *s = to_virtio_blk(vdev); |
| 596 | unregister_savevm(s->qdev, "virtio-blk", s); |
Amit Shah | d92551f | 2011-07-27 14:00:30 +0530 | [diff] [blame] | 597 | virtio_cleanup(vdev); |
Alex Williamson | 9d0d313 | 2010-07-20 11:14:22 -0600 | [diff] [blame] | 598 | } |