]> git.proxmox.com Git - mirror_qemu.git/blame - hw/block/virtio-blk.c
virtio-blk: add "discard" and "write-zeroes" properties
[mirror_qemu.git] / hw / block / 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
80c71a24 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
5a61cb60 16#include "qemu-common.h"
827805a2 17#include "qemu/iov.h"
1de7afc9 18#include "qemu/error-report.h"
6d519a5f 19#include "trace.h"
0d09e41a 20#include "hw/block/block.h"
9c17d615 21#include "sysemu/blockdev.h"
0d09e41a 22#include "hw/virtio/virtio-blk.h"
52b53c04 23#include "dataplane/virtio-blk.h"
08e2c9f1 24#include "scsi/constants.h"
1063b8b1
CH
25#ifdef __linux__
26# include <scsi/sg.h>
27#endif
0d09e41a 28#include "hw/virtio/virtio-bus.h"
783d1897 29#include "hw/virtio/virtio-access.h"
6e02c38d 30
42824b4d
CL
31/* We don't support discard yet, hide associated config fields. */
32#define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \
33 max_discard_sectors)
34
d14dde5e
GK
35static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
36 VirtIOBlockReq *req)
671ec3f0 37{
671ec3f0 38 req->dev = s;
edaffd9f 39 req->vq = vq;
869d66af 40 req->qiov.size = 0;
2a6cdd6d 41 req->in_len = 0;
869d66af 42 req->next = NULL;
95f7142a 43 req->mr_next = NULL;
671ec3f0
FZ
44}
45
d14dde5e 46static void virtio_blk_free_request(VirtIOBlockReq *req)
671ec3f0 47{
1d29b5b0 48 g_free(req);
671ec3f0
FZ
49}
50
03de2f52 51static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
869a5c6d
AL
52{
53 VirtIOBlock *s = req->dev;
1cc91b7d 54 VirtIODevice *vdev = VIRTIO_DEVICE(s);
869a5c6d 55
a576ceac 56 trace_virtio_blk_req_complete(vdev, req, status);
6d519a5f 57
92e3c2a3 58 stb_p(&req->in->status, status);
edaffd9f 59 virtqueue_push(req->vq, &req->elem, req->in_len);
eb41cf78 60 if (s->dataplane_started && !s->dataplane_disabled) {
edaffd9f 61 virtio_blk_data_plane_notify(s->dataplane, req->vq);
03de2f52 62 } else {
edaffd9f 63 virtio_notify(vdev, req->vq);
03de2f52 64 }
bf4bd461
FZ
65}
66
f35d68f0 67static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
00f639fb 68 bool is_read, bool acct_failed)
869a5c6d 69{
869a5c6d 70 VirtIOBlock *s = req->dev;
9a6719d5 71 BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
869a5c6d 72
a589569f 73 if (action == BLOCK_ERROR_ACTION_STOP) {
466138dc
FZ
74 /* Break the link as the next request is going to be parsed from the
75 * ring again. Otherwise we may end up doing a double completion! */
76 req->mr_next = NULL;
869a5c6d
AL
77 req->next = s->rq;
78 s->rq = req;
a589569f 79 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
869a5c6d 80 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
00f639fb
SG
81 if (acct_failed) {
82 block_acct_failed(blk_get_stats(s->blk), &req->acct);
83 }
671ec3f0 84 virtio_blk_free_request(req);
869a5c6d
AL
85 }
86
4be74634 87 blk_error_action(s->blk, action, is_read, error);
a589569f 88 return action != BLOCK_ERROR_ACTION_IGNORE;
869a5c6d
AL
89}
90
6e02c38d
AL
91static void virtio_blk_rw_complete(void *opaque, int ret)
92{
95f7142a 93 VirtIOBlockReq *next = opaque;
b9e413dd 94 VirtIOBlock *s = next->dev;
a576ceac 95 VirtIODevice *vdev = VIRTIO_DEVICE(s);
95f7142a 96
b9e413dd 97 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
95f7142a
PL
98 while (next) {
99 VirtIOBlockReq *req = next;
100 next = req->mr_next;
a576ceac 101 trace_virtio_blk_rw_complete(vdev, req, ret);
95f7142a
PL
102
103 if (req->qiov.nalloc != -1) {
e61809ed 104 /* If nalloc is != -1 req->qiov is a local copy of the original
9bb192a4
YB
105 * external iovec. It was allocated in submit_requests to be
106 * able to merge requests. */
95f7142a
PL
107 qemu_iovec_destroy(&req->qiov);
108 }
6e02c38d 109
95f7142a
PL
110 if (ret) {
111 int p = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type);
112 bool is_read = !(p & VIRTIO_BLK_T_OUT);
2a6cdd6d
PB
113 /* Note that memory may be dirtied on read failure. If the
114 * virtio request is not completed here, as is the case for
115 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
116 * correctly during live migration. While this is ugly,
117 * it is acceptable because the device is free to write to
118 * the memory until the request is completed (which will
119 * happen on the other side of the migration).
120 */
00f639fb 121 if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) {
95f7142a
PL
122 continue;
123 }
124 }
6d519a5f 125
95f7142a
PL
126 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
127 block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
128 virtio_blk_free_request(req);
6e02c38d 129 }
b9e413dd 130 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
869a5c6d 131}
6e02c38d 132
aa659be3
CH
133static void virtio_blk_flush_complete(void *opaque, int ret)
134{
135 VirtIOBlockReq *req = opaque;
b9e413dd 136 VirtIOBlock *s = req->dev;
aa659be3 137
b9e413dd 138 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
8c269b54 139 if (ret) {
00f639fb 140 if (virtio_blk_handle_rw_error(req, -ret, 0, true)) {
b9e413dd 141 goto out;
8c269b54
KW
142 }
143 }
144
145 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
9a6719d5 146 block_acct_done(blk_get_stats(s->blk), &req->acct);
671ec3f0 147 virtio_blk_free_request(req);
b9e413dd
PB
148
149out:
150 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
6e02c38d
AL
151}
152
1dc936aa
FZ
153#ifdef __linux__
154
155typedef struct {
156 VirtIOBlockReq *req;
157 struct sg_io_hdr hdr;
158} VirtIOBlockIoctlReq;
159
160static void virtio_blk_ioctl_complete(void *opaque, int status)
161{
162 VirtIOBlockIoctlReq *ioctl_req = opaque;
163 VirtIOBlockReq *req = ioctl_req->req;
9d456654
PB
164 VirtIOBlock *s = req->dev;
165 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1dc936aa
FZ
166 struct virtio_scsi_inhdr *scsi;
167 struct sg_io_hdr *hdr;
168
169 scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
170
171 if (status) {
172 status = VIRTIO_BLK_S_UNSUPP;
173 virtio_stl_p(vdev, &scsi->errors, 255);
174 goto out;
175 }
176
177 hdr = &ioctl_req->hdr;
178 /*
179 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
180 * clear the masked_status field [hence status gets cleared too, see
181 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
182 * status has occurred. However they do set DRIVER_SENSE in driver_status
183 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
184 */
185 if (hdr->status == 0 && hdr->sb_len_wr > 0) {
186 hdr->status = CHECK_CONDITION;
187 }
188
189 virtio_stl_p(vdev, &scsi->errors,
190 hdr->status | (hdr->msg_status << 8) |
191 (hdr->host_status << 16) | (hdr->driver_status << 24));
192 virtio_stl_p(vdev, &scsi->residual, hdr->resid);
193 virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
194 virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
195
196out:
b9e413dd 197 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
1dc936aa
FZ
198 virtio_blk_req_complete(req, status);
199 virtio_blk_free_request(req);
b9e413dd 200 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
1dc936aa
FZ
201 g_free(ioctl_req);
202}
203
204#endif
205
edaffd9f 206static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 207{
edaffd9f 208 VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
6e02c38d 209
51b19ebe 210 if (req) {
edaffd9f 211 virtio_blk_init_request(s, vq, req);
6e02c38d 212 }
6e02c38d
AL
213 return req;
214}
215
75344fa4 216static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
1063b8b1 217{
5a05cbee
FZ
218 int status = VIRTIO_BLK_S_OK;
219 struct virtio_scsi_inhdr *scsi = NULL;
75344fa4
FZ
220 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
221 VirtQueueElement *elem = &req->elem;
222 VirtIOBlock *blk = req->dev;
783d1897 223
47ce9ef7 224#ifdef __linux__
1063b8b1 225 int i;
1dc936aa 226 VirtIOBlockIoctlReq *ioctl_req;
a209f461 227 BlockAIOCB *acb;
47ce9ef7 228#endif
1063b8b1
CH
229
230 /*
231 * We require at least one output segment each for the virtio_blk_outhdr
232 * and the SCSI command block.
233 *
234 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
235 * and the sense buffer pointer in the input segments.
236 */
5a05cbee
FZ
237 if (elem->out_num < 2 || elem->in_num < 3) {
238 status = VIRTIO_BLK_S_IOERR;
239 goto fail;
1063b8b1
CH
240 }
241
242 /*
f34e73cd
PB
243 * The scsi inhdr is placed in the second-to-last input segment, just
244 * before the regular inhdr.
1063b8b1 245 */
5a05cbee 246 scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
f34e73cd 247
bbe8bd4d 248 if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) {
f34e73cd
PB
249 status = VIRTIO_BLK_S_UNSUPP;
250 goto fail;
1063b8b1
CH
251 }
252
253 /*
f34e73cd 254 * No support for bidirection commands yet.
1063b8b1 255 */
5a05cbee 256 if (elem->out_num > 2 && elem->in_num > 3) {
f34e73cd
PB
257 status = VIRTIO_BLK_S_UNSUPP;
258 goto fail;
259 }
1063b8b1 260
f34e73cd 261#ifdef __linux__
1dc936aa
FZ
262 ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
263 ioctl_req->req = req;
264 ioctl_req->hdr.interface_id = 'S';
265 ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
266 ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
267 ioctl_req->hdr.dxfer_len = 0;
1063b8b1 268
5a05cbee 269 if (elem->out_num > 2) {
1063b8b1
CH
270 /*
271 * If there are more than the minimally required 2 output segments
272 * there is write payload starting from the third iovec.
273 */
1dc936aa
FZ
274 ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
275 ioctl_req->hdr.iovec_count = elem->out_num - 2;
1063b8b1 276
1dc936aa
FZ
277 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
278 ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
279 }
1063b8b1 280
1dc936aa 281 ioctl_req->hdr.dxferp = elem->out_sg + 2;
1063b8b1 282
5a05cbee 283 } else if (elem->in_num > 3) {
1063b8b1
CH
284 /*
285 * If we have more than 3 input segments the guest wants to actually
286 * read data.
287 */
1dc936aa
FZ
288 ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
289 ioctl_req->hdr.iovec_count = elem->in_num - 3;
290 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
291 ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
292 }
1063b8b1 293
1dc936aa 294 ioctl_req->hdr.dxferp = elem->in_sg;
1063b8b1
CH
295 } else {
296 /*
297 * Some SCSI commands don't actually transfer any data.
298 */
1dc936aa 299 ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
1063b8b1
CH
300 }
301
1dc936aa
FZ
302 ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
303 ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
1063b8b1 304
a209f461
FZ
305 acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
306 virtio_blk_ioctl_complete, ioctl_req);
307 if (!acb) {
308 g_free(ioctl_req);
309 status = VIRTIO_BLK_S_UNSUPP;
310 goto fail;
311 }
1dc936aa 312 return -EINPROGRESS;
1063b8b1 313#else
f34e73cd
PB
314 abort();
315#endif
316
317fail:
318 /* Just put anything nonzero so that the ioctl fails in the guest. */
5a05cbee 319 if (scsi) {
783d1897 320 virtio_stl_p(vdev, &scsi->errors, 255);
5a05cbee
FZ
321 }
322 return status;
323}
324
325static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
326{
327 int status;
328
75344fa4 329 status = virtio_blk_handle_scsi_req(req);
1dc936aa
FZ
330 if (status != -EINPROGRESS) {
331 virtio_blk_req_complete(req, status);
332 virtio_blk_free_request(req);
333 }
1063b8b1 334}
1063b8b1 335
95f7142a
PL
336static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
337 int start, int num_reqs, int niov)
869a5c6d 338{
95f7142a
PL
339 QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
340 int64_t sector_num = mrb->reqs[start]->sector_num;
95f7142a
PL
341 bool is_write = mrb->is_write;
342
343 if (num_reqs > 1) {
344 int i;
345 struct iovec *tmp_iov = qiov->iov;
346 int tmp_niov = qiov->niov;
347
348 /* mrb->reqs[start]->qiov was initialized from external so we can't
b5772fdd 349 * modify it here. We need to initialize it locally and then add the
95f7142a
PL
350 * external iovecs. */
351 qemu_iovec_init(qiov, niov);
352
353 for (i = 0; i < tmp_niov; i++) {
354 qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
355 }
356
357 for (i = start + 1; i < start + num_reqs; i++) {
358 qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
359 mrb->reqs[i]->qiov.size);
360 mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
95f7142a 361 }
95f7142a 362
a576ceac
SH
363 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev),
364 mrb, start, num_reqs,
b5772fdd
EB
365 sector_num << BDRV_SECTOR_BITS,
366 qiov->size, is_write);
95f7142a
PL
367 block_acct_merge_done(blk_get_stats(blk),
368 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
369 num_reqs - 1);
370 }
91553dcc 371
95f7142a 372 if (is_write) {
b5772fdd
EB
373 blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
374 virtio_blk_rw_complete, mrb->reqs[start]);
95f7142a 375 } else {
b5772fdd
EB
376 blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
377 virtio_blk_rw_complete, mrb->reqs[start]);
95f7142a
PL
378 }
379}
380
381static int multireq_compare(const void *a, const void *b)
382{
383 const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
384 *req2 = *(VirtIOBlockReq **)b;
385
386 /*
387 * Note that we can't simply subtract sector_num1 from sector_num2
388 * here as that could overflow the return value.
389 */
390 if (req1->sector_num > req2->sector_num) {
391 return 1;
392 } else if (req1->sector_num < req2->sector_num) {
393 return -1;
394 } else {
395 return 0;
396 }
397}
398
d14dde5e 399static void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb)
95f7142a
PL
400{
401 int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
5def6b80 402 uint32_t max_transfer;
95f7142a
PL
403 int64_t sector_num = 0;
404
405 if (mrb->num_reqs == 1) {
406 submit_requests(blk, mrb, 0, 1, -1);
407 mrb->num_reqs = 0;
c20fd872
CH
408 return;
409 }
410
5def6b80 411 max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
95f7142a
PL
412
413 qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
414 &multireq_compare);
415
416 for (i = 0; i < mrb->num_reqs; i++) {
417 VirtIOBlockReq *req = mrb->reqs[i];
418 if (num_reqs > 0) {
49cffbc6
GA
419 /*
420 * NOTE: We cannot merge the requests in below situations:
421 * 1. requests are not sequential
422 * 2. merge would exceed maximum number of IOVs
423 * 3. merge would exceed maximum transfer length of backend device
424 */
425 if (sector_num + nb_sectors != req->sector_num ||
222565f6 426 niov > blk_get_max_iov(blk) - req->qiov.niov ||
5def6b80
EB
427 req->qiov.size > max_transfer ||
428 nb_sectors > (max_transfer -
429 req->qiov.size) / BDRV_SECTOR_SIZE) {
95f7142a
PL
430 submit_requests(blk, mrb, start, num_reqs, niov);
431 num_reqs = 0;
91553dcc
KW
432 }
433 }
95f7142a
PL
434
435 if (num_reqs == 0) {
436 sector_num = req->sector_num;
437 nb_sectors = niov = 0;
438 start = i;
439 }
440
441 nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
442 niov += req->qiov.niov;
443 num_reqs++;
91553dcc 444 }
c20fd872 445
95f7142a
PL
446 submit_requests(blk, mrb, start, num_reqs, niov);
447 mrb->num_reqs = 0;
91553dcc 448}
87b245db 449
c20fd872 450static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
aa659be3 451{
4be74634 452 block_acct_start(blk_get_stats(req->dev->blk), &req->acct, 0,
5366d0c8 453 BLOCK_ACCT_FLUSH);
a597e79c 454
618fbb84
CH
455 /*
456 * Make sure all outstanding writes are posted to the backing device.
457 */
95f7142a
PL
458 if (mrb->is_write && mrb->num_reqs > 0) {
459 virtio_blk_submit_multireq(req->dev->blk, mrb);
460 }
4be74634 461 blk_aio_flush(req->dev->blk, virtio_blk_flush_complete, req);
aa659be3
CH
462}
463
d0e14376
MA
464static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
465 uint64_t sector, size_t size)
466{
3c2daac0
MA
467 uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
468 uint64_t total_sectors;
469
75af1f34 470 if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
95f7142a
PL
471 return false;
472 }
d0e14376
MA
473 if (sector & dev->sector_mask) {
474 return false;
475 }
2a30307f 476 if (size % dev->conf.conf.logical_block_size) {
d0e14376
MA
477 return false;
478 }
4be74634 479 blk_get_geometry(dev->blk, &total_sectors);
3c2daac0
MA
480 if (sector > total_sectors || nb_sectors > total_sectors - sector) {
481 return false;
482 }
d0e14376
MA
483 return true;
484}
485
20ea686a 486static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
bc6694d4 487{
92e3c2a3 488 uint32_t type;
f897bf75 489 struct iovec *in_iov = req->elem.in_sg;
5636da76 490 struct iovec *out_iov = req->elem.out_sg;
f897bf75
SH
491 unsigned in_num = req->elem.in_num;
492 unsigned out_num = req->elem.out_num;
20ea686a
GK
493 VirtIOBlock *s = req->dev;
494 VirtIODevice *vdev = VIRTIO_DEVICE(s);
92e3c2a3 495
f897bf75 496 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
20ea686a
GK
497 virtio_error(vdev, "virtio-blk missing headers");
498 return -1;
bc6694d4
KW
499 }
500
5636da76 501 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
827805a2 502 sizeof(req->out)) != sizeof(req->out))) {
20ea686a
GK
503 virtio_error(vdev, "virtio-blk request outhdr too short");
504 return -1;
827805a2 505 }
ee17e848 506
5636da76 507 iov_discard_front(&out_iov, &out_num, sizeof(req->out));
ee17e848 508
12048545 509 if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
20ea686a
GK
510 virtio_error(vdev, "virtio-blk request inhdr too short");
511 return -1;
ee17e848
FZ
512 }
513
2a6cdd6d
PB
514 /* We always touch the last byte, so just see how big in_iov is. */
515 req->in_len = iov_size(in_iov, in_num);
ee17e848
FZ
516 req->in = (void *)in_iov[in_num - 1].iov_base
517 + in_iov[in_num - 1].iov_len
518 - sizeof(struct virtio_blk_inhdr);
519 iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
bc6694d4 520
9a6719d5 521 type = virtio_ldl_p(vdev, &req->out.type);
92e3c2a3 522
95f7142a 523 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
631b22ea 524 * is an optional flag. Although a guest should not send this flag if
95f7142a
PL
525 * not negotiated we ignored it in the past. So keep ignoring it. */
526 switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
527 case VIRTIO_BLK_T_IN:
528 {
529 bool is_write = type & VIRTIO_BLK_T_OUT;
9a6719d5 530 req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
95f7142a
PL
531
532 if (is_write) {
5636da76 533 qemu_iovec_init_external(&req->qiov, out_iov, out_num);
a576ceac 534 trace_virtio_blk_handle_write(vdev, req, req->sector_num,
95f7142a
PL
535 req->qiov.size / BDRV_SECTOR_SIZE);
536 } else {
537 qemu_iovec_init_external(&req->qiov, in_iov, in_num);
a576ceac 538 trace_virtio_blk_handle_read(vdev, req, req->sector_num,
95f7142a
PL
539 req->qiov.size / BDRV_SECTOR_SIZE);
540 }
541
9a6719d5 542 if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) {
95f7142a 543 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
9a6719d5 544 block_acct_invalid(blk_get_stats(s->blk),
01762e03 545 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
95f7142a 546 virtio_blk_free_request(req);
20ea686a 547 return 0;
95f7142a
PL
548 }
549
9a6719d5 550 block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size,
95f7142a
PL
551 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
552
553 /* merge would exceed maximum number of requests or IO direction
554 * changes */
555 if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
c99495ac 556 is_write != mrb->is_write ||
9a6719d5
SG
557 !s->conf.request_merging)) {
558 virtio_blk_submit_multireq(s->blk, mrb);
95f7142a
PL
559 }
560
561 assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
562 mrb->reqs[mrb->num_reqs++] = req;
563 mrb->is_write = is_write;
564 break;
565 }
566 case VIRTIO_BLK_T_FLUSH:
c20fd872 567 virtio_blk_handle_flush(req, mrb);
95f7142a
PL
568 break;
569 case VIRTIO_BLK_T_SCSI_CMD:
bc6694d4 570 virtio_blk_handle_scsi(req);
95f7142a
PL
571 break;
572 case VIRTIO_BLK_T_GET_ID:
573 {
a8686a9b
MA
574 /*
575 * NB: per existing s/n string convention the string is
576 * terminated by '\0' only when shorter than buffer.
577 */
2a30307f 578 const char *serial = s->conf.serial ? s->conf.serial : "";
a83ceea8
MM
579 size_t size = MIN(strlen(serial) + 1,
580 MIN(iov_size(in_iov, in_num),
581 VIRTIO_BLK_ID_BYTES));
582 iov_from_buf(in_iov, in_num, 0, serial, size);
2930b313 583 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
671ec3f0 584 virtio_blk_free_request(req);
95f7142a
PL
585 break;
586 }
587 default:
9e72c450 588 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
671ec3f0 589 virtio_blk_free_request(req);
bc6694d4 590 }
20ea686a 591 return 0;
bc6694d4
KW
592}
593
07931698 594bool virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 595{
6e02c38d 596 VirtIOBlockReq *req;
95f7142a 597 MultiReqBuffer mrb = {};
07931698 598 bool progress = false;
6e02c38d 599
9d456654 600 aio_context_acquire(blk_get_aio_context(s->blk));
fc73548e
SH
601 blk_io_plug(s->blk);
602
9ef9d402
SH
603 do {
604 virtio_queue_set_notification(vq, 0);
605
606 while ((req = virtio_blk_get_request(s, vq))) {
07931698 607 progress = true;
9ef9d402
SH
608 if (virtio_blk_handle_request(req, &mrb)) {
609 virtqueue_detach_element(req->vq, &req->elem, 0);
610 virtio_blk_free_request(req);
611 break;
612 }
20ea686a 613 }
9ef9d402
SH
614
615 virtio_queue_set_notification(vq, 1);
616 } while (!virtio_queue_empty(vq));
91553dcc 617
95f7142a
PL
618 if (mrb.num_reqs) {
619 virtio_blk_submit_multireq(s->blk, &mrb);
620 }
fc73548e
SH
621
622 blk_io_unplug(s->blk);
9d456654 623 aio_context_release(blk_get_aio_context(s->blk));
07931698
FZ
624 return progress;
625}
626
627static void virtio_blk_handle_output_do(VirtIOBlock *s, VirtQueue *vq)
628{
629 virtio_blk_handle_vq(s, vq);
6e02c38d
AL
630}
631
8a2fad57
MT
632static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
633{
634 VirtIOBlock *s = (VirtIOBlock *)vdev;
635
636 if (s->dataplane) {
637 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
638 * dataplane here instead of waiting for .set_status().
639 */
9ffe337c 640 virtio_device_start_ioeventfd(vdev);
8a2fad57
MT
641 if (!s->dataplane_disabled) {
642 return;
643 }
644 }
07931698 645 virtio_blk_handle_output_do(s, vq);
8a2fad57
MT
646}
647
213189ab 648static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
649{
650 VirtIOBlock *s = opaque;
651 VirtIOBlockReq *req = s->rq;
95f7142a 652 MultiReqBuffer mrb = {};
869a5c6d 653
213189ab
MA
654 qemu_bh_delete(s->bh);
655 s->bh = NULL;
869a5c6d
AL
656
657 s->rq = NULL;
658
1919631e 659 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
869a5c6d 660 while (req) {
1bdb176a 661 VirtIOBlockReq *next = req->next;
20ea686a
GK
662 if (virtio_blk_handle_request(req, &mrb)) {
663 /* Device is now broken and won't do any processing until it gets
664 * reset. Already queued requests will be lost: let's purge them.
665 */
666 while (req) {
667 next = req->next;
668 virtqueue_detach_element(req->vq, &req->elem, 0);
669 virtio_blk_free_request(req);
670 req = next;
671 }
672 break;
673 }
1bdb176a 674 req = next;
869a5c6d 675 }
f1b52868 676
95f7142a
PL
677 if (mrb.num_reqs) {
678 virtio_blk_submit_multireq(s->blk, &mrb);
679 }
1919631e 680 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
869a5c6d
AL
681}
682
1dfb4dd9
LC
683static void virtio_blk_dma_restart_cb(void *opaque, int running,
684 RunState state)
213189ab
MA
685{
686 VirtIOBlock *s = opaque;
687
392808b4 688 if (!running) {
213189ab 689 return;
392808b4 690 }
213189ab
MA
691
692 if (!s->bh) {
4be74634 693 s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
4407c1c5 694 virtio_blk_dma_restart_bh, s);
213189ab
MA
695 qemu_bh_schedule(s->bh);
696 }
697}
698
6e02c38d
AL
699static void virtio_blk_reset(VirtIODevice *vdev)
700{
1cc91b7d 701 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e40b3bf 702 AioContext *ctx;
26307f6a 703 VirtIOBlockReq *req;
392808b4 704
6e40b3bf
AY
705 ctx = blk_get_aio_context(s->blk);
706 aio_context_acquire(ctx);
707 blk_drain(s->blk);
708
26307f6a
FZ
709 /* We drop queued requests after blk_drain() because blk_drain() itself can
710 * produce them. */
711 while (s->rq) {
712 req = s->rq;
713 s->rq = req->next;
97b93c8a 714 virtqueue_detach_element(req->vq, &req->elem, 0);
26307f6a
FZ
715 virtio_blk_free_request(req);
716 }
717
6e40b3bf
AY
718 aio_context_release(ctx);
719
9ffe337c 720 assert(!s->dataplane_started);
4be74634 721 blk_set_enable_write_cache(s->blk, s->original_wce);
6e02c38d
AL
722}
723
bf011293 724/* coalesce internal state, copy to pci i/o region 0
725 */
6e02c38d
AL
726static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
727{
1cc91b7d 728 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a30307f 729 BlockConf *conf = &s->conf.conf;
6e02c38d
AL
730 struct virtio_blk_config blkcfg;
731 uint64_t capacity;
17d0bc01 732 int64_t length;
f7516731 733 int blk_size = conf->logical_block_size;
6e02c38d 734
4be74634 735 blk_get_geometry(s->blk, &capacity);
5c5dafdc 736 memset(&blkcfg, 0, sizeof(blkcfg));
783d1897
RR
737 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
738 virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2);
907eb3e5 739 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
783d1897 740 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
f7516731
MA
741 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
742 virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
907eb3e5 743 blkcfg.geometry.heads = conf->heads;
136be99e
CB
744 /*
745 * We must ensure that the block device capacity is a multiple of
e03ba136 746 * the logical block size. If that is not the case, let's use
136be99e
CB
747 * sector_mask to adopt the geometry to have a correct picture.
748 * For those devices where the capacity is ok for the given geometry
e03ba136 749 * we don't touch the sector value of the geometry, since some devices
136be99e
CB
750 * (like s390 dasd) need a specific value. Here the capacity is already
751 * cyls*heads*secs*blk_size and the sector value is not block size
752 * divided by 512 - instead it is the amount of blk_size blocks
753 * per track (cylinder).
754 */
17d0bc01
SH
755 length = blk_getlength(s->blk);
756 if (length > 0 && length / conf->heads / conf->secs % blk_size) {
907eb3e5 757 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
136be99e 758 } else {
907eb3e5 759 blkcfg.geometry.sectors = conf->secs;
136be99e 760 }
c7085da7 761 blkcfg.size_max = 0;
f7516731 762 blkcfg.physical_block_exp = get_physical_block_exp(conf);
9752c371 763 blkcfg.alignment_offset = 0;
4be74634 764 blkcfg.wce = blk_enable_write_cache(s->blk);
2f270590 765 virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
42824b4d
CL
766 memcpy(config, &blkcfg, VIRTIO_BLK_CFG_SIZE);
767 QEMU_BUILD_BUG_ON(VIRTIO_BLK_CFG_SIZE > sizeof(blkcfg));
6e02c38d
AL
768}
769
13e3dce0
PB
770static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
771{
1cc91b7d 772 VirtIOBlock *s = VIRTIO_BLK(vdev);
13e3dce0
PB
773 struct virtio_blk_config blkcfg;
774
42824b4d
CL
775 memcpy(&blkcfg, config, VIRTIO_BLK_CFG_SIZE);
776 QEMU_BUILD_BUG_ON(VIRTIO_BLK_CFG_SIZE > sizeof(blkcfg));
6d7e73d6 777
4be74634
MA
778 aio_context_acquire(blk_get_aio_context(s->blk));
779 blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
780 aio_context_release(blk_get_aio_context(s->blk));
13e3dce0
PB
781}
782
9d5b731d
JW
783static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
784 Error **errp)
6e02c38d 785{
1cc91b7d 786 VirtIOBlock *s = VIRTIO_BLK(vdev);
1063b8b1 787
bbe8bd4d
SG
788 /* Firstly sync all virtio-blk possible supported features */
789 features |= s->host_features;
790
0cd09c3a
CH
791 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
792 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
793 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
794 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
95129d6f 795 if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
bbe8bd4d 796 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) {
efb8206c
JW
797 error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
798 return 0;
799 }
efb8206c 800 } else {
c9b11f97 801 virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
efb8206c
JW
802 virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
803 }
aa659be3 804
4be74634 805 if (blk_enable_write_cache(s->blk)) {
0cd09c3a 806 virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
4be74634
MA
807 }
808 if (blk_is_read_only(s->blk)) {
0cd09c3a 809 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
4be74634 810 }
2f270590
SH
811 if (s->conf.num_queues > 1) {
812 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
813 }
1063b8b1
CH
814
815 return features;
6e02c38d
AL
816}
817
9315cbfd
PB
818static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
819{
1cc91b7d 820 VirtIOBlock *s = VIRTIO_BLK(vdev);
9315cbfd 821
9ffe337c
PB
822 if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) {
823 assert(!s->dataplane_started);
392808b4 824 }
392808b4 825
9315cbfd
PB
826 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
827 return;
828 }
829
ef5bc962
PB
830 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
831 * cache flushes. Thus, the "auto writethrough" behavior is never
832 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
833 * Leaving it enabled would break the following sequence:
834 *
835 * Guest started with "-drive cache=writethrough"
836 * Guest sets status to 0
837 * Guest sets DRIVER bit in status field
838 * Guest reads host features (WCE=0, CONFIG_WCE=1)
839 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
840 * Guest writes 1 to the WCE configuration field (writeback mode)
841 * Guest sets DRIVER_OK bit in status field
842 *
4be74634 843 * s->blk would erroneously be placed in writethrough mode.
ef5bc962 844 */
95129d6f 845 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
4be74634
MA
846 aio_context_acquire(blk_get_aio_context(s->blk));
847 blk_set_enable_write_cache(s->blk,
95129d6f
CH
848 virtio_vdev_has_feature(vdev,
849 VIRTIO_BLK_F_WCE));
4be74634 850 aio_context_release(blk_get_aio_context(s->blk));
ef5bc962 851 }
9315cbfd
PB
852}
853
b2b295a7
GK
854static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
855{
856 VirtIOBlock *s = VIRTIO_BLK(vdev);
857 VirtIOBlockReq *req = s->rq;
858
869a5c6d
AL
859 while (req) {
860 qemu_put_sbyte(f, 1);
30d8bf6d
SH
861
862 if (s->conf.num_queues > 1) {
863 qemu_put_be32(f, virtio_get_queue_index(req->vq));
864 }
865
ab281c17 866 qemu_put_virtqueue_element(f, &req->elem);
869a5c6d
AL
867 req = req->next;
868 }
869 qemu_put_sbyte(f, 0);
6e02c38d
AL
870}
871
b2b295a7
GK
872static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
873 int version_id)
874{
875 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a633c46 876
869a5c6d 877 while (qemu_get_sbyte(f)) {
30d8bf6d
SH
878 unsigned nvqs = s->conf.num_queues;
879 unsigned vq_idx = 0;
ab281c17 880 VirtIOBlockReq *req;
30d8bf6d
SH
881
882 if (nvqs > 1) {
883 vq_idx = qemu_get_be32(f);
884
885 if (vq_idx >= nvqs) {
886 error_report("Invalid virtqueue index in request list: %#x",
887 vq_idx);
888 return -EINVAL;
889 }
890 }
891
8607f5c3 892 req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
30d8bf6d 893 virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
869a5c6d 894 req->next = s->rq;
20a81e4d 895 s->rq = req;
869a5c6d 896 }
6e02c38d
AL
897
898 return 0;
899}
900
145feb17 901static void virtio_blk_resize(void *opaque)
e5051fc7 902{
1cc91b7d 903 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
e5051fc7 904
1cc91b7d 905 virtio_notify_config(vdev);
e5051fc7
CH
906}
907
0e49de52 908static const BlockDevOps virtio_block_ops = {
145feb17 909 .resize_cb = virtio_blk_resize,
0e49de52
MA
910};
911
75884afd 912static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1c028ddf 913{
75884afd 914 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179b417e 915 VirtIOBlock *s = VIRTIO_BLK(dev);
2a30307f 916 VirtIOBlkConf *conf = &s->conf;
3ffeeef7 917 Error *err = NULL;
2f270590 918 unsigned i;
cf21e106 919
4be74634 920 if (!conf->conf.blk) {
75884afd
AF
921 error_setg(errp, "drive property not set");
922 return;
d75d25e3 923 }
4be74634 924 if (!blk_is_inserted(conf->conf.blk)) {
75884afd
AF
925 error_setg(errp, "Device needs media, but drive is empty");
926 return;
98f28ad7 927 }
2f270590
SH
928 if (!conf->num_queues) {
929 error_setg(errp, "num-queues property must be larger than 0");
930 return;
931 }
6040aedd
MK
932 if (!is_power_of_2(conf->queue_size) ||
933 conf->queue_size > VIRTQUEUE_MAX_SIZE) {
934 error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
935 "must be a power of 2 (max %d)",
936 conf->queue_size, VIRTQUEUE_MAX_SIZE);
937 return;
938 }
d75d25e3 939
ceff3e1f
MZ
940 if (!blkconf_apply_backend_options(&conf->conf,
941 blk_is_read_only(conf->conf.blk), true,
942 errp)) {
a17c17a2
KW
943 return;
944 }
4be74634 945 s->original_wce = blk_enable_write_cache(conf->conf.blk);
ceff3e1f 946 if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) {
75884afd 947 return;
b7eb0c9f 948 }
ceff3e1f 949
0eb28a42 950 blkconf_blocksizes(&conf->conf);
a8686a9b 951
0a75b60c
MK
952 if (conf->conf.logical_block_size >
953 conf->conf.physical_block_size) {
954 error_setg(errp,
955 "logical_block_size > physical_block_size not supported");
956 return;
957 }
958
42824b4d 959 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, VIRTIO_BLK_CFG_SIZE);
6e02c38d 960
4be74634 961 s->blk = conf->conf.blk;
869a5c6d 962 s->rq = NULL;
2a30307f 963 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 964
2f270590 965 for (i = 0; i < conf->num_queues; i++) {
6040aedd 966 virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
2f270590 967 }
2a30307f 968 virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
3ffeeef7 969 if (err != NULL) {
75884afd 970 error_propagate(errp, err);
6a1a8cc7 971 virtio_cleanup(vdev);
75884afd 972 return;
392808b4 973 }
6e02c38d 974
69b302b2 975 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
4be74634
MA
976 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
977 blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
6e02c38d 978
4be74634 979 blk_iostatus_enable(s->blk);
1c028ddf
FK
980}
981
306ec6c3 982static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
1c028ddf 983{
306ec6c3
AF
984 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
985 VirtIOBlock *s = VIRTIO_BLK(dev);
986
1c028ddf
FK
987 virtio_blk_data_plane_destroy(s->dataplane);
988 s->dataplane = NULL;
1c028ddf 989 qemu_del_vm_change_state_handler(s->change);
4be74634 990 blockdev_mark_auto_del(s->blk);
6a1a8cc7 991 virtio_cleanup(vdev);
1c028ddf
FK
992}
993
467b3f33
SH
994static void virtio_blk_instance_init(Object *obj)
995{
996 VirtIOBlock *s = VIRTIO_BLK(obj);
997
2a30307f 998 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
3342ec32
GA
999 "bootindex", "/disk@0,0",
1000 DEVICE(obj), NULL);
467b3f33
SH
1001}
1002
977a117f
HP
1003static const VMStateDescription vmstate_virtio_blk = {
1004 .name = "virtio-blk",
1005 .minimum_version_id = 2,
1006 .version_id = 2,
1007 .fields = (VMStateField[]) {
1008 VMSTATE_VIRTIO_DEVICE,
1009 VMSTATE_END_OF_LIST()
1010 },
1011};
bbded32c 1012
1c028ddf 1013static Property virtio_blk_properties[] = {
2a30307f 1014 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
8c398252 1015 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
2a30307f
MA
1016 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
1017 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
bbe8bd4d
SG
1018 DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features,
1019 VIRTIO_BLK_F_CONFIG_WCE, true),
32a877e4 1020#ifdef __linux__
bbe8bd4d
SG
1021 DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features,
1022 VIRTIO_BLK_F_SCSI, false),
32a877e4 1023#endif
c99495ac
PL
1024 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
1025 true),
2f270590 1026 DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues, 1),
6040aedd 1027 DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 128),
d679ac09
FZ
1028 DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD,
1029 IOThread *),
5c81161f
SG
1030 DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features,
1031 VIRTIO_BLK_F_DISCARD, true),
1032 DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features,
1033 VIRTIO_BLK_F_WRITE_ZEROES, true),
1c028ddf
FK
1034 DEFINE_PROP_END_OF_LIST(),
1035};
1036
1037static void virtio_blk_class_init(ObjectClass *klass, void *data)
1038{
1039 DeviceClass *dc = DEVICE_CLASS(klass);
1040 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
75884afd 1041
1c028ddf 1042 dc->props = virtio_blk_properties;
bbded32c 1043 dc->vmsd = &vmstate_virtio_blk;
125ee0ed 1044 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
75884afd 1045 vdc->realize = virtio_blk_device_realize;
306ec6c3 1046 vdc->unrealize = virtio_blk_device_unrealize;
1c028ddf
FK
1047 vdc->get_config = virtio_blk_update_config;
1048 vdc->set_config = virtio_blk_set_config;
1049 vdc->get_features = virtio_blk_get_features;
1050 vdc->set_status = virtio_blk_set_status;
1051 vdc->reset = virtio_blk_reset;
b2b295a7
GK
1052 vdc->save = virtio_blk_save_device;
1053 vdc->load = virtio_blk_load_device;
9ffe337c
PB
1054 vdc->start_ioeventfd = virtio_blk_data_plane_start;
1055 vdc->stop_ioeventfd = virtio_blk_data_plane_stop;
1c028ddf
FK
1056}
1057
b5c7ceaf 1058static const TypeInfo virtio_blk_info = {
1c028ddf
FK
1059 .name = TYPE_VIRTIO_BLK,
1060 .parent = TYPE_VIRTIO_DEVICE,
1061 .instance_size = sizeof(VirtIOBlock),
467b3f33 1062 .instance_init = virtio_blk_instance_init,
1c028ddf
FK
1063 .class_init = virtio_blk_class_init,
1064};
1065
1066static void virtio_register_types(void)
1067{
b5c7ceaf 1068 type_register_static(&virtio_blk_info);
1c028ddf
FK
1069}
1070
1071type_init(virtio_register_types)