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