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