]> git.proxmox.com Git - qemu.git/blame - hw/virtio-blk.c
softmmu: move include files to include/sysemu/
[qemu.git] / hw / virtio-blk.c
CommitLineData
6e02c38d
AL
1/*
2 * Virtio Block Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
5a61cb60 14#include "qemu-common.h"
1de7afc9 15#include "qemu/error-report.h"
6d519a5f 16#include "trace.h"
9db1c0f7 17#include "hw/block-common.h"
9c17d615 18#include "sysemu/blockdev.h"
6e02c38d 19#include "virtio-blk.h"
5bb23927 20#include "scsi-defs.h"
1063b8b1
CH
21#ifdef __linux__
22# include <scsi/sg.h>
23#endif
6e02c38d
AL
24
25typedef struct VirtIOBlock
26{
27 VirtIODevice vdev;
28 BlockDriverState *bs;
29 VirtQueue *vq;
869a5c6d 30 void *rq;
213189ab 31 QEMUBH *bh;
9752c371 32 BlockConf *conf;
12c5674b 33 VirtIOBlkConf *blk;
8cfacf07 34 unsigned short sector_mask;
9d0d3138 35 DeviceState *qdev;
6e02c38d
AL
36} VirtIOBlock;
37
38static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
39{
40 return (VirtIOBlock *)vdev;
41}
42
43typedef struct VirtIOBlockReq
44{
45 VirtIOBlock *dev;
46 VirtQueueElement elem;
47 struct virtio_blk_inhdr *in;
48 struct virtio_blk_outhdr *out;
1063b8b1 49 struct virtio_scsi_inhdr *scsi;
d28a1b6e 50 QEMUIOVector qiov;
869a5c6d 51 struct VirtIOBlockReq *next;
a597e79c 52 BlockAcctCookie acct;
6e02c38d
AL
53} VirtIOBlockReq;
54
869a5c6d
AL
55static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
56{
57 VirtIOBlock *s = req->dev;
58
6d519a5f
SH
59 trace_virtio_blk_req_complete(req, status);
60
92e3c2a3 61 stb_p(&req->in->status, status);
d28a1b6e 62 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
869a5c6d 63 virtio_notify(&s->vdev, s->vq);
869a5c6d
AL
64}
65
f35d68f0 66static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
1ceee0d5 67 bool is_read)
869a5c6d 68{
3e1caa5f 69 BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error);
869a5c6d
AL
70 VirtIOBlock *s = req->dev;
71
3e1caa5f 72 if (action == BDRV_ACTION_STOP) {
869a5c6d
AL
73 req->next = s->rq;
74 s->rq = req;
3e1caa5f 75 } else if (action == BDRV_ACTION_REPORT) {
869a5c6d 76 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
a597e79c
CH
77 bdrv_acct_done(s->bs, &req->acct);
78 g_free(req);
869a5c6d
AL
79 }
80
3e1caa5f
PB
81 bdrv_error_action(s->bs, action, is_read, error);
82 return action != BDRV_ACTION_IGNORE;
869a5c6d
AL
83}
84
6e02c38d
AL
85static void virtio_blk_rw_complete(void *opaque, int ret)
86{
87 VirtIOBlockReq *req = opaque;
6e02c38d 88
6d519a5f
SH
89 trace_virtio_blk_rw_complete(req, ret);
90
f35d68f0 91 if (ret) {
1ceee0d5 92 bool is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT);
f35d68f0 93 if (virtio_blk_handle_rw_error(req, -ret, is_read))
869a5c6d 94 return;
6e02c38d
AL
95 }
96
f35d68f0 97 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
a597e79c
CH
98 bdrv_acct_done(req->dev->bs, &req->acct);
99 g_free(req);
869a5c6d 100}
6e02c38d 101
aa659be3
CH
102static void virtio_blk_flush_complete(void *opaque, int ret)
103{
104 VirtIOBlockReq *req = opaque;
105
8c269b54
KW
106 if (ret) {
107 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
108 return;
109 }
110 }
111
112 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
a597e79c
CH
113 bdrv_acct_done(req->dev->bs, &req->acct);
114 g_free(req);
aa659be3
CH
115}
116
869a5c6d
AL
117static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
118{
7267c094 119 VirtIOBlockReq *req = g_malloc(sizeof(*req));
487414f1 120 req->dev = s;
de6c8042
SH
121 req->qiov.size = 0;
122 req->next = NULL;
869a5c6d 123 return req;
6e02c38d
AL
124}
125
126static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
127{
869a5c6d 128 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
6e02c38d 129
869a5c6d
AL
130 if (req != NULL) {
131 if (!virtqueue_pop(s->vq, &req->elem)) {
7267c094 132 g_free(req);
869a5c6d
AL
133 return NULL;
134 }
6e02c38d
AL
135 }
136
137 return req;
138}
139
1063b8b1
CH
140static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
141{
47ce9ef7 142#ifdef __linux__
4277906d 143 int ret;
1063b8b1 144 int i;
47ce9ef7
SW
145#endif
146 int status = VIRTIO_BLK_S_OK;
1063b8b1
CH
147
148 /*
149 * We require at least one output segment each for the virtio_blk_outhdr
150 * and the SCSI command block.
151 *
152 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
153 * and the sense buffer pointer in the input segments.
154 */
155 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
156 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
a597e79c 157 g_free(req);
1063b8b1
CH
158 return;
159 }
160
161 /*
f34e73cd
PB
162 * The scsi inhdr is placed in the second-to-last input segment, just
163 * before the regular inhdr.
1063b8b1 164 */
f34e73cd
PB
165 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
166
a6c5c84a 167 if (!req->dev->blk->scsi) {
f34e73cd
PB
168 status = VIRTIO_BLK_S_UNSUPP;
169 goto fail;
1063b8b1
CH
170 }
171
172 /*
f34e73cd 173 * No support for bidirection commands yet.
1063b8b1 174 */
f34e73cd
PB
175 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
176 status = VIRTIO_BLK_S_UNSUPP;
177 goto fail;
178 }
1063b8b1 179
f34e73cd
PB
180#ifdef __linux__
181 struct sg_io_hdr hdr;
1063b8b1
CH
182 memset(&hdr, 0, sizeof(struct sg_io_hdr));
183 hdr.interface_id = 'S';
184 hdr.cmd_len = req->elem.out_sg[1].iov_len;
185 hdr.cmdp = req->elem.out_sg[1].iov_base;
186 hdr.dxfer_len = 0;
187
188 if (req->elem.out_num > 2) {
189 /*
190 * If there are more than the minimally required 2 output segments
191 * there is write payload starting from the third iovec.
192 */
193 hdr.dxfer_direction = SG_DXFER_TO_DEV;
194 hdr.iovec_count = req->elem.out_num - 2;
195
196 for (i = 0; i < hdr.iovec_count; i++)
197 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
198
199 hdr.dxferp = req->elem.out_sg + 2;
200
201 } else if (req->elem.in_num > 3) {
202 /*
203 * If we have more than 3 input segments the guest wants to actually
204 * read data.
205 */
206 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
207 hdr.iovec_count = req->elem.in_num - 3;
208 for (i = 0; i < hdr.iovec_count; i++)
209 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
210
211 hdr.dxferp = req->elem.in_sg;
1063b8b1
CH
212 } else {
213 /*
214 * Some SCSI commands don't actually transfer any data.
215 */
216 hdr.dxfer_direction = SG_DXFER_NONE;
217 }
218
219 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
220 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
1063b8b1
CH
221
222 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
223 if (ret) {
224 status = VIRTIO_BLK_S_UNSUPP;
f34e73cd 225 goto fail;
1063b8b1
CH
226 }
227
5bb23927
PB
228 /*
229 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
230 * clear the masked_status field [hence status gets cleared too, see
231 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
232 * status has occurred. However they do set DRIVER_SENSE in driver_status
233 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
234 */
235 if (hdr.status == 0 && hdr.sb_len_wr > 0) {
236 hdr.status = CHECK_CONDITION;
237 }
238
239 stl_p(&req->scsi->errors,
240 hdr.status | (hdr.msg_status << 8) |
241 (hdr.host_status << 16) | (hdr.driver_status << 24));
92e3c2a3
AJ
242 stl_p(&req->scsi->residual, hdr.resid);
243 stl_p(&req->scsi->sense_len, hdr.sb_len_wr);
244 stl_p(&req->scsi->data_len, hdr.dxfer_len);
1063b8b1
CH
245
246 virtio_blk_req_complete(req, status);
a597e79c 247 g_free(req);
730a9c53 248 return;
1063b8b1 249#else
f34e73cd
PB
250 abort();
251#endif
252
253fail:
254 /* Just put anything nonzero so that the ioctl fails in the guest. */
255 stl_p(&req->scsi->errors, 255);
256 virtio_blk_req_complete(req, status);
a597e79c 257 g_free(req);
1063b8b1 258}
1063b8b1 259
c20fd872
CH
260typedef struct MultiReqBuffer {
261 BlockRequest blkreq[32];
262 unsigned int num_writes;
263} MultiReqBuffer;
264
265static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)
869a5c6d 266{
91553dcc 267 int i, ret;
91553dcc 268
c20fd872
CH
269 if (!mrb->num_writes) {
270 return;
271 }
272
273 ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes);
91553dcc 274 if (ret != 0) {
c20fd872
CH
275 for (i = 0; i < mrb->num_writes; i++) {
276 if (mrb->blkreq[i].error) {
277 virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO);
91553dcc
KW
278 }
279 }
280 }
c20fd872
CH
281
282 mrb->num_writes = 0;
91553dcc 283}
87b245db 284
c20fd872 285static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
aa659be3 286{
a597e79c
CH
287 bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH);
288
618fbb84
CH
289 /*
290 * Make sure all outstanding writes are posted to the backing device.
291 */
c20fd872 292 virtio_submit_multiwrite(req->dev->bs, mrb);
ad54ae80 293 bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
aa659be3
CH
294}
295
c20fd872 296static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
91553dcc 297{
c20fd872 298 BlockRequest *blkreq;
92e3c2a3 299 uint64_t sector;
c20fd872 300
92e3c2a3 301 sector = ldq_p(&req->out->sector);
6d519a5f 302
a597e79c
CH
303 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
304
92e3c2a3
AJ
305 trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
306
307 if (sector & req->dev->sector_mask) {
8cfacf07
CH
308 virtio_blk_rw_complete(req, -EIO);
309 return;
310 }
52c05023
CH
311 if (req->qiov.size % req->dev->conf->logical_block_size) {
312 virtio_blk_rw_complete(req, -EIO);
313 return;
314 }
8cfacf07 315
c20fd872
CH
316 if (mrb->num_writes == 32) {
317 virtio_submit_multiwrite(req->dev->bs, mrb);
87b245db 318 }
91553dcc 319
c20fd872 320 blkreq = &mrb->blkreq[mrb->num_writes];
92e3c2a3 321 blkreq->sector = sector;
c20fd872
CH
322 blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE;
323 blkreq->qiov = &req->qiov;
324 blkreq->cb = virtio_blk_rw_complete;
325 blkreq->opaque = req;
326 blkreq->error = 0;
91553dcc 327
c20fd872 328 mrb->num_writes++;
d28a1b6e 329}
869a5c6d 330
d28a1b6e
AL
331static void virtio_blk_handle_read(VirtIOBlockReq *req)
332{
92e3c2a3
AJ
333 uint64_t sector;
334
335 sector = ldq_p(&req->out->sector);
87b245db 336
a597e79c
CH
337 bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
338
81b6b9fa
SH
339 trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
340
92e3c2a3 341 if (sector & req->dev->sector_mask) {
8cfacf07
CH
342 virtio_blk_rw_complete(req, -EIO);
343 return;
344 }
52c05023
CH
345 if (req->qiov.size % req->dev->conf->logical_block_size) {
346 virtio_blk_rw_complete(req, -EIO);
347 return;
348 }
ad54ae80
PB
349 bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
350 req->qiov.size / BDRV_SECTOR_SIZE,
351 virtio_blk_rw_complete, req);
869a5c6d
AL
352}
353
bc6694d4
KW
354static void virtio_blk_handle_request(VirtIOBlockReq *req,
355 MultiReqBuffer *mrb)
356{
92e3c2a3
AJ
357 uint32_t type;
358
bc6694d4 359 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
870cef1d 360 error_report("virtio-blk missing headers");
bc6694d4
KW
361 exit(1);
362 }
363
364 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
365 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
870cef1d 366 error_report("virtio-blk header not in correct element");
bc6694d4
KW
367 exit(1);
368 }
369
370 req->out = (void *)req->elem.out_sg[0].iov_base;
371 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
372
92e3c2a3
AJ
373 type = ldl_p(&req->out->type);
374
375 if (type & VIRTIO_BLK_T_FLUSH) {
c20fd872 376 virtio_blk_handle_flush(req, mrb);
92e3c2a3 377 } else if (type & VIRTIO_BLK_T_SCSI_CMD) {
bc6694d4 378 virtio_blk_handle_scsi(req);
92e3c2a3 379 } else if (type & VIRTIO_BLK_T_GET_ID) {
2930b313 380 VirtIOBlock *s = req->dev;
381
a8686a9b
MA
382 /*
383 * NB: per existing s/n string convention the string is
384 * terminated by '\0' only when shorter than buffer.
385 */
386 strncpy(req->elem.in_sg[0].iov_base,
12c5674b 387 s->blk->serial ? s->blk->serial : "",
a8686a9b 388 MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
2930b313 389 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
a597e79c 390 g_free(req);
92e3c2a3 391 } else if (type & VIRTIO_BLK_T_OUT) {
bc6694d4
KW
392 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
393 req->elem.out_num - 1);
c20fd872 394 virtio_blk_handle_write(req, mrb);
bc6694d4
KW
395 } else {
396 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
397 req->elem.in_num - 1);
398 virtio_blk_handle_read(req);
399 }
400}
401
6e02c38d
AL
402static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
403{
404 VirtIOBlock *s = to_virtio_blk(vdev);
405 VirtIOBlockReq *req;
bc6694d4
KW
406 MultiReqBuffer mrb = {
407 .num_writes = 0,
bc6694d4 408 };
6e02c38d
AL
409
410 while ((req = virtio_blk_get_request(s))) {
bc6694d4 411 virtio_blk_handle_request(req, &mrb);
6e02c38d 412 }
91553dcc 413
c20fd872 414 virtio_submit_multiwrite(s->bs, &mrb);
91553dcc 415
6e02c38d
AL
416 /*
417 * FIXME: Want to check for completions before returning to guest mode,
418 * so cached reads and writes are reported as quickly as possible. But
419 * that should be done in the generic block layer.
420 */
421}
422
213189ab 423static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
424{
425 VirtIOBlock *s = opaque;
426 VirtIOBlockReq *req = s->rq;
f1b52868
KW
427 MultiReqBuffer mrb = {
428 .num_writes = 0,
f1b52868 429 };
869a5c6d 430
213189ab
MA
431 qemu_bh_delete(s->bh);
432 s->bh = NULL;
869a5c6d
AL
433
434 s->rq = NULL;
435
436 while (req) {
f1b52868 437 virtio_blk_handle_request(req, &mrb);
869a5c6d
AL
438 req = req->next;
439 }
f1b52868 440
c20fd872 441 virtio_submit_multiwrite(s->bs, &mrb);
869a5c6d
AL
442}
443
1dfb4dd9
LC
444static void virtio_blk_dma_restart_cb(void *opaque, int running,
445 RunState state)
213189ab
MA
446{
447 VirtIOBlock *s = opaque;
448
449 if (!running)
450 return;
451
452 if (!s->bh) {
453 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
454 qemu_bh_schedule(s->bh);
455 }
456}
457
6e02c38d
AL
458static void virtio_blk_reset(VirtIODevice *vdev)
459{
460 /*
461 * This should cancel pending requests, but can't do nicely until there
462 * are per-device request lists.
463 */
922453bc 464 bdrv_drain_all();
6e02c38d
AL
465}
466
bf011293 467/* coalesce internal state, copy to pci i/o region 0
468 */
6e02c38d
AL
469static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
470{
471 VirtIOBlock *s = to_virtio_blk(vdev);
472 struct virtio_blk_config blkcfg;
473 uint64_t capacity;
3a395142 474 int blk_size = s->conf->logical_block_size;
6e02c38d
AL
475
476 bdrv_get_geometry(s->bs, &capacity);
5c5dafdc 477 memset(&blkcfg, 0, sizeof(blkcfg));
6e02c38d
AL
478 stq_raw(&blkcfg.capacity, capacity);
479 stl_raw(&blkcfg.seg_max, 128 - 2);
e63e7fde 480 stw_raw(&blkcfg.cylinders, s->conf->cyls);
3a395142
PB
481 stl_raw(&blkcfg.blk_size, blk_size);
482 stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size);
483 stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size);
e63e7fde 484 blkcfg.heads = s->conf->heads;
136be99e
CB
485 /*
486 * We must ensure that the block device capacity is a multiple of
487 * the logical block size. If that is not the case, lets use
488 * sector_mask to adopt the geometry to have a correct picture.
489 * For those devices where the capacity is ok for the given geometry
490 * we dont touch the sector value of the geometry, since some devices
491 * (like s390 dasd) need a specific value. Here the capacity is already
492 * cyls*heads*secs*blk_size and the sector value is not block size
493 * divided by 512 - instead it is the amount of blk_size blocks
494 * per track (cylinder).
495 */
e63e7fde
MA
496 if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) {
497 blkcfg.sectors = s->conf->secs & ~s->sector_mask;
136be99e 498 } else {
e63e7fde 499 blkcfg.sectors = s->conf->secs;
136be99e 500 }
c7085da7 501 blkcfg.size_max = 0;
9752c371
CH
502 blkcfg.physical_block_exp = get_physical_block_exp(s->conf);
503 blkcfg.alignment_offset = 0;
13e3dce0 504 blkcfg.wce = bdrv_enable_write_cache(s->bs);
37d5ddd6 505 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
6e02c38d
AL
506}
507
13e3dce0
PB
508static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
509{
510 VirtIOBlock *s = to_virtio_blk(vdev);
511 struct virtio_blk_config blkcfg;
512
513 memcpy(&blkcfg, config, sizeof(blkcfg));
514 bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0);
515}
516
8172539d 517static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
6e02c38d 518{
bf011293 519 VirtIOBlock *s = to_virtio_blk(vdev);
1063b8b1
CH
520
521 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
522 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
9752c371 523 features |= (1 << VIRTIO_BLK_F_TOPOLOGY);
8cfacf07 524 features |= (1 << VIRTIO_BLK_F_BLK_SIZE);
a6c5c84a 525 features |= (1 << VIRTIO_BLK_F_SCSI);
aa659be3
CH
526
527 if (bdrv_enable_write_cache(s->bs))
13e3dce0
PB
528 features |= (1 << VIRTIO_BLK_F_WCE);
529
c79662f7
NS
530 if (bdrv_is_read_only(s->bs))
531 features |= 1 << VIRTIO_BLK_F_RO;
1063b8b1
CH
532
533 return features;
6e02c38d
AL
534}
535
9315cbfd
PB
536static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
537{
538 VirtIOBlock *s = to_virtio_blk(vdev);
539 uint32_t features;
540
541 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
542 return;
543 }
544
545 features = vdev->guest_features;
546 bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE)));
547}
548
6e02c38d
AL
549static void virtio_blk_save(QEMUFile *f, void *opaque)
550{
551 VirtIOBlock *s = opaque;
869a5c6d
AL
552 VirtIOBlockReq *req = s->rq;
553
6e02c38d 554 virtio_save(&s->vdev, f);
869a5c6d
AL
555
556 while (req) {
557 qemu_put_sbyte(f, 1);
558 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
559 req = req->next;
560 }
561 qemu_put_sbyte(f, 0);
6e02c38d
AL
562}
563
564static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
565{
566 VirtIOBlock *s = opaque;
2a633c46 567 int ret;
6e02c38d 568
869a5c6d 569 if (version_id != 2)
6e02c38d
AL
570 return -EINVAL;
571
2a633c46
OW
572 ret = virtio_load(&s->vdev, f);
573 if (ret) {
574 return ret;
575 }
576
869a5c6d
AL
577 while (qemu_get_sbyte(f)) {
578 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
579 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
580 req->next = s->rq;
20a81e4d 581 s->rq = req;
b6a4805b
KW
582
583 virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr,
584 req->elem.in_num, 1);
585 virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr,
586 req->elem.out_num, 0);
869a5c6d 587 }
6e02c38d
AL
588
589 return 0;
590}
591
145feb17 592static void virtio_blk_resize(void *opaque)
e5051fc7
CH
593{
594 VirtIOBlock *s = opaque;
595
145feb17 596 virtio_notify_config(&s->vdev);
e5051fc7
CH
597}
598
0e49de52 599static const BlockDevOps virtio_block_ops = {
145feb17 600 .resize_cb = virtio_blk_resize,
0e49de52
MA
601};
602
12c5674b 603VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk)
6e02c38d
AL
604{
605 VirtIOBlock *s;
6e02c38d 606 static int virtio_blk_id;
cf21e106 607
12c5674b 608 if (!blk->conf.bs) {
6a84cb1f 609 error_report("drive property not set");
d75d25e3
MA
610 return NULL;
611 }
12c5674b 612 if (!bdrv_is_inserted(blk->conf.bs)) {
98f28ad7
MA
613 error_report("Device needs media, but drive is empty");
614 return NULL;
615 }
d75d25e3 616
911525db 617 blkconf_serial(&blk->conf, &blk->serial);
b7eb0c9f
MA
618 if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) {
619 return NULL;
620 }
a8686a9b 621
53c25cea 622 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
37d5ddd6 623 sizeof(struct virtio_blk_config),
53c25cea 624 sizeof(VirtIOBlock));
6e02c38d
AL
625
626 s->vdev.get_config = virtio_blk_update_config;
13e3dce0 627 s->vdev.set_config = virtio_blk_set_config;
6e02c38d 628 s->vdev.get_features = virtio_blk_get_features;
9315cbfd 629 s->vdev.set_status = virtio_blk_set_status;
6e02c38d 630 s->vdev.reset = virtio_blk_reset;
12c5674b
PB
631 s->bs = blk->conf.bs;
632 s->conf = &blk->conf;
633 s->blk = blk;
869a5c6d 634 s->rq = NULL;
1573a35d 635 s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 636
6e02c38d
AL
637 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
638
869a5c6d 639 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
9d0d3138 640 s->qdev = dev;
0be71e32 641 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
6e02c38d 642 virtio_blk_save, virtio_blk_load, s);
0e49de52 643 bdrv_set_dev_ops(s->bs, &virtio_block_ops, s);
12c5674b 644 bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size);
6e02c38d 645
af239a62 646 bdrv_iostatus_enable(s->bs);
12c5674b 647 add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0");
1ca4d09a 648
53c25cea 649 return &s->vdev;
6e02c38d 650}
9d0d3138
AW
651
652void virtio_blk_exit(VirtIODevice *vdev)
653{
654 VirtIOBlock *s = to_virtio_blk(vdev);
655 unregister_savevm(s->qdev, "virtio-blk", s);
0e47931b 656 blockdev_mark_auto_del(s->bs);
d92551f2 657 virtio_cleanup(vdev);
9d0d3138 658}