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