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