]> git.proxmox.com Git - mirror_qemu.git/blame - hw/block/virtio-blk.c
Merge remote-tracking branch 'remotes/berrange/tags/pull-qcrypto-secrets-base-2015...
[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
5a61cb60 14#include "qemu-common.h"
827805a2 15#include "qemu/iov.h"
1de7afc9 16#include "qemu/error-report.h"
6d519a5f 17#include "trace.h"
0d09e41a 18#include "hw/block/block.h"
4be74634 19#include "sysemu/block-backend.h"
9c17d615 20#include "sysemu/blockdev.h"
0d09e41a 21#include "hw/virtio/virtio-blk.h"
52b53c04
FZ
22#include "dataplane/virtio-blk.h"
23#include "migration/migration.h"
0d09e41a 24#include "block/scsi.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
f897bf75 31VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
671ec3f0 32{
c84b3192 33 VirtIOBlockReq *req = g_new(VirtIOBlockReq, 1);
671ec3f0 34 req->dev = s;
869d66af 35 req->qiov.size = 0;
2a6cdd6d 36 req->in_len = 0;
869d66af 37 req->next = NULL;
95f7142a 38 req->mr_next = NULL;
671ec3f0
FZ
39 return req;
40}
41
f897bf75 42void virtio_blk_free_request(VirtIOBlockReq *req)
671ec3f0
FZ
43{
44 if (req) {
c84b3192 45 g_free(req);
671ec3f0
FZ
46 }
47}
48
bf4bd461
FZ
49static void virtio_blk_complete_request(VirtIOBlockReq *req,
50 unsigned char status)
869a5c6d
AL
51{
52 VirtIOBlock *s = req->dev;
1cc91b7d 53 VirtIODevice *vdev = VIRTIO_DEVICE(s);
869a5c6d 54
6d519a5f
SH
55 trace_virtio_blk_req_complete(req, status);
56
92e3c2a3 57 stb_p(&req->in->status, status);
2a6cdd6d 58 virtqueue_push(s->vq, &req->elem, req->in_len);
1cc91b7d 59 virtio_notify(vdev, s->vq);
869a5c6d
AL
60}
61
bf4bd461
FZ
62static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
63{
64 req->dev->complete_request(req, status);
65}
66
f35d68f0 67static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
1ceee0d5 68 bool is_read)
869a5c6d 69{
4be74634
MA
70 BlockErrorAction action = blk_get_error_action(req->dev->blk,
71 is_read, error);
869a5c6d
AL
72 VirtIOBlock *s = req->dev;
73
a589569f 74 if (action == BLOCK_ERROR_ACTION_STOP) {
466138dc
FZ
75 /* Break the link as the next request is going to be parsed from the
76 * ring again. Otherwise we may end up doing a double completion! */
77 req->mr_next = NULL;
869a5c6d
AL
78 req->next = s->rq;
79 s->rq = req;
a589569f 80 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
869a5c6d 81 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
01762e03 82 block_acct_failed(blk_get_stats(s->blk), &req->acct);
671ec3f0 83 virtio_blk_free_request(req);
869a5c6d
AL
84 }
85
4be74634 86 blk_error_action(s->blk, action, is_read, error);
a589569f 87 return action != BLOCK_ERROR_ACTION_IGNORE;
869a5c6d
AL
88}
89
6e02c38d
AL
90static void virtio_blk_rw_complete(void *opaque, int ret)
91{
95f7142a
PL
92 VirtIOBlockReq *next = opaque;
93
94 while (next) {
95 VirtIOBlockReq *req = next;
96 next = req->mr_next;
97 trace_virtio_blk_rw_complete(req, ret);
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 }
869a5c6d 126}
6e02c38d 127
aa659be3
CH
128static void virtio_blk_flush_complete(void *opaque, int ret)
129{
130 VirtIOBlockReq *req = opaque;
131
8c269b54
KW
132 if (ret) {
133 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
134 return;
135 }
136 }
137
138 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
4be74634 139 block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
671ec3f0 140 virtio_blk_free_request(req);
6e02c38d
AL
141}
142
1dc936aa
FZ
143#ifdef __linux__
144
145typedef struct {
146 VirtIOBlockReq *req;
147 struct sg_io_hdr hdr;
148} VirtIOBlockIoctlReq;
149
150static void virtio_blk_ioctl_complete(void *opaque, int status)
151{
152 VirtIOBlockIoctlReq *ioctl_req = opaque;
153 VirtIOBlockReq *req = ioctl_req->req;
154 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
155 struct virtio_scsi_inhdr *scsi;
156 struct sg_io_hdr *hdr;
157
158 scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
159
160 if (status) {
161 status = VIRTIO_BLK_S_UNSUPP;
162 virtio_stl_p(vdev, &scsi->errors, 255);
163 goto out;
164 }
165
166 hdr = &ioctl_req->hdr;
167 /*
168 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
169 * clear the masked_status field [hence status gets cleared too, see
170 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
171 * status has occurred. However they do set DRIVER_SENSE in driver_status
172 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
173 */
174 if (hdr->status == 0 && hdr->sb_len_wr > 0) {
175 hdr->status = CHECK_CONDITION;
176 }
177
178 virtio_stl_p(vdev, &scsi->errors,
179 hdr->status | (hdr->msg_status << 8) |
180 (hdr->host_status << 16) | (hdr->driver_status << 24));
181 virtio_stl_p(vdev, &scsi->residual, hdr->resid);
182 virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
183 virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
184
185out:
186 virtio_blk_req_complete(req, status);
187 virtio_blk_free_request(req);
188 g_free(ioctl_req);
189}
190
191#endif
192
6e02c38d
AL
193static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
194{
869a5c6d 195 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
6e02c38d 196
f897bf75 197 if (!virtqueue_pop(s->vq, &req->elem)) {
671ec3f0
FZ
198 virtio_blk_free_request(req);
199 return NULL;
6e02c38d
AL
200 }
201
202 return req;
203}
204
75344fa4 205static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
1063b8b1 206{
5a05cbee
FZ
207 int status = VIRTIO_BLK_S_OK;
208 struct virtio_scsi_inhdr *scsi = NULL;
75344fa4
FZ
209 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
210 VirtQueueElement *elem = &req->elem;
211 VirtIOBlock *blk = req->dev;
783d1897 212
47ce9ef7 213#ifdef __linux__
1063b8b1 214 int i;
1dc936aa 215 VirtIOBlockIoctlReq *ioctl_req;
a209f461 216 BlockAIOCB *acb;
47ce9ef7 217#endif
1063b8b1
CH
218
219 /*
220 * We require at least one output segment each for the virtio_blk_outhdr
221 * and the SCSI command block.
222 *
223 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
224 * and the sense buffer pointer in the input segments.
225 */
5a05cbee
FZ
226 if (elem->out_num < 2 || elem->in_num < 3) {
227 status = VIRTIO_BLK_S_IOERR;
228 goto fail;
1063b8b1
CH
229 }
230
231 /*
f34e73cd
PB
232 * The scsi inhdr is placed in the second-to-last input segment, just
233 * before the regular inhdr.
1063b8b1 234 */
5a05cbee 235 scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
f34e73cd 236
2a30307f 237 if (!blk->conf.scsi) {
f34e73cd
PB
238 status = VIRTIO_BLK_S_UNSUPP;
239 goto fail;
1063b8b1
CH
240 }
241
242 /*
f34e73cd 243 * No support for bidirection commands yet.
1063b8b1 244 */
5a05cbee 245 if (elem->out_num > 2 && elem->in_num > 3) {
f34e73cd
PB
246 status = VIRTIO_BLK_S_UNSUPP;
247 goto fail;
248 }
1063b8b1 249
f34e73cd 250#ifdef __linux__
1dc936aa
FZ
251 ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
252 ioctl_req->req = req;
253 ioctl_req->hdr.interface_id = 'S';
254 ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
255 ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
256 ioctl_req->hdr.dxfer_len = 0;
1063b8b1 257
5a05cbee 258 if (elem->out_num > 2) {
1063b8b1
CH
259 /*
260 * If there are more than the minimally required 2 output segments
261 * there is write payload starting from the third iovec.
262 */
1dc936aa
FZ
263 ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
264 ioctl_req->hdr.iovec_count = elem->out_num - 2;
1063b8b1 265
1dc936aa
FZ
266 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
267 ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
268 }
1063b8b1 269
1dc936aa 270 ioctl_req->hdr.dxferp = elem->out_sg + 2;
1063b8b1 271
5a05cbee 272 } else if (elem->in_num > 3) {
1063b8b1
CH
273 /*
274 * If we have more than 3 input segments the guest wants to actually
275 * read data.
276 */
1dc936aa
FZ
277 ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
278 ioctl_req->hdr.iovec_count = elem->in_num - 3;
279 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
280 ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
281 }
1063b8b1 282
1dc936aa 283 ioctl_req->hdr.dxferp = elem->in_sg;
1063b8b1
CH
284 } else {
285 /*
286 * Some SCSI commands don't actually transfer any data.
287 */
1dc936aa 288 ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
1063b8b1
CH
289 }
290
1dc936aa
FZ
291 ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
292 ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
1063b8b1 293
a209f461
FZ
294 acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
295 virtio_blk_ioctl_complete, ioctl_req);
296 if (!acb) {
297 g_free(ioctl_req);
298 status = VIRTIO_BLK_S_UNSUPP;
299 goto fail;
300 }
1dc936aa 301 return -EINPROGRESS;
1063b8b1 302#else
f34e73cd
PB
303 abort();
304#endif
305
306fail:
307 /* Just put anything nonzero so that the ioctl fails in the guest. */
5a05cbee 308 if (scsi) {
783d1897 309 virtio_stl_p(vdev, &scsi->errors, 255);
5a05cbee
FZ
310 }
311 return status;
312}
313
314static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
315{
316 int status;
317
75344fa4 318 status = virtio_blk_handle_scsi_req(req);
1dc936aa
FZ
319 if (status != -EINPROGRESS) {
320 virtio_blk_req_complete(req, status);
321 virtio_blk_free_request(req);
322 }
1063b8b1 323}
1063b8b1 324
95f7142a
PL
325static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
326 int start, int num_reqs, int niov)
869a5c6d 327{
95f7142a
PL
328 QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
329 int64_t sector_num = mrb->reqs[start]->sector_num;
330 int nb_sectors = mrb->reqs[start]->qiov.size / BDRV_SECTOR_SIZE;
331 bool is_write = mrb->is_write;
332
333 if (num_reqs > 1) {
334 int i;
335 struct iovec *tmp_iov = qiov->iov;
336 int tmp_niov = qiov->niov;
337
338 /* mrb->reqs[start]->qiov was initialized from external so we can't
339 * modifiy it here. We need to initialize it locally and then add the
340 * external iovecs. */
341 qemu_iovec_init(qiov, niov);
342
343 for (i = 0; i < tmp_niov; i++) {
344 qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
345 }
346
347 for (i = start + 1; i < start + num_reqs; i++) {
348 qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
349 mrb->reqs[i]->qiov.size);
350 mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
351 nb_sectors += mrb->reqs[i]->qiov.size / BDRV_SECTOR_SIZE;
352 }
353 assert(nb_sectors == qiov->size / BDRV_SECTOR_SIZE);
354
355 trace_virtio_blk_submit_multireq(mrb, start, num_reqs, sector_num,
356 nb_sectors, is_write);
357 block_acct_merge_done(blk_get_stats(blk),
358 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
359 num_reqs - 1);
360 }
91553dcc 361
95f7142a
PL
362 if (is_write) {
363 blk_aio_writev(blk, sector_num, qiov, nb_sectors,
364 virtio_blk_rw_complete, mrb->reqs[start]);
365 } else {
366 blk_aio_readv(blk, sector_num, qiov, nb_sectors,
367 virtio_blk_rw_complete, mrb->reqs[start]);
368 }
369}
370
371static int multireq_compare(const void *a, const void *b)
372{
373 const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
374 *req2 = *(VirtIOBlockReq **)b;
375
376 /*
377 * Note that we can't simply subtract sector_num1 from sector_num2
378 * here as that could overflow the return value.
379 */
380 if (req1->sector_num > req2->sector_num) {
381 return 1;
382 } else if (req1->sector_num < req2->sector_num) {
383 return -1;
384 } else {
385 return 0;
386 }
387}
388
389void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb)
390{
391 int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
392 int max_xfer_len = 0;
393 int64_t sector_num = 0;
394
395 if (mrb->num_reqs == 1) {
396 submit_requests(blk, mrb, 0, 1, -1);
397 mrb->num_reqs = 0;
c20fd872
CH
398 return;
399 }
400
95f7142a 401 max_xfer_len = blk_get_max_transfer_length(mrb->reqs[0]->dev->blk);
75af1f34 402 max_xfer_len = MIN_NON_ZERO(max_xfer_len, BDRV_REQUEST_MAX_SECTORS);
95f7142a
PL
403
404 qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
405 &multireq_compare);
406
407 for (i = 0; i < mrb->num_reqs; i++) {
408 VirtIOBlockReq *req = mrb->reqs[i];
409 if (num_reqs > 0) {
410 bool merge = true;
411
412 /* merge would exceed maximum number of IOVs */
413 if (niov + req->qiov.niov > IOV_MAX) {
414 merge = false;
415 }
416
417 /* merge would exceed maximum transfer length of backend device */
418 if (req->qiov.size / BDRV_SECTOR_SIZE + nb_sectors > max_xfer_len) {
419 merge = false;
420 }
421
422 /* requests are not sequential */
423 if (sector_num + nb_sectors != req->sector_num) {
424 merge = false;
425 }
426
427 if (!merge) {
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
fee65db7 484void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
bc6694d4 485{
92e3c2a3 486 uint32_t type;
f897bf75
SH
487 struct iovec *in_iov = req->elem.in_sg;
488 struct iovec *iov = req->elem.out_sg;
489 unsigned in_num = req->elem.in_num;
490 unsigned out_num = req->elem.out_num;
92e3c2a3 491
f897bf75 492 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
870cef1d 493 error_report("virtio-blk missing headers");
bc6694d4
KW
494 exit(1);
495 }
496
827805a2
FZ
497 if (unlikely(iov_to_buf(iov, out_num, 0, &req->out,
498 sizeof(req->out)) != sizeof(req->out))) {
499 error_report("virtio-blk request outhdr too short");
500 exit(1);
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)) {
ee17e848
FZ
506 error_report("virtio-blk request inhdr too short");
507 exit(1);
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);
531 trace_virtio_blk_handle_write(req, req->sector_num,
532 req->qiov.size / BDRV_SECTOR_SIZE);
533 } else {
534 qemu_iovec_init_external(&req->qiov, in_iov, in_num);
535 trace_virtio_blk_handle_read(req, req->sector_num,
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
PL
544 virtio_blk_free_request(req);
545 return;
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
KW
591 }
592}
593
6e02c38d
AL
594static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
595{
1cc91b7d 596 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e02c38d 597 VirtIOBlockReq *req;
95f7142a 598 MultiReqBuffer mrb = {};
6e02c38d 599
392808b4
SH
600 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
601 * dataplane here instead of waiting for .set_status().
602 */
603 if (s->dataplane) {
604 virtio_blk_data_plane_start(s->dataplane);
605 return;
606 }
392808b4 607
fc73548e
SH
608 blk_io_plug(s->blk);
609
6e02c38d 610 while ((req = virtio_blk_get_request(s))) {
bc6694d4 611 virtio_blk_handle_request(req, &mrb);
6e02c38d 612 }
91553dcc 613
95f7142a
PL
614 if (mrb.num_reqs) {
615 virtio_blk_submit_multireq(s->blk, &mrb);
616 }
fc73548e
SH
617
618 blk_io_unplug(s->blk);
6e02c38d
AL
619}
620
213189ab 621static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
622{
623 VirtIOBlock *s = opaque;
624 VirtIOBlockReq *req = s->rq;
95f7142a 625 MultiReqBuffer mrb = {};
869a5c6d 626
213189ab
MA
627 qemu_bh_delete(s->bh);
628 s->bh = NULL;
869a5c6d
AL
629
630 s->rq = NULL;
631
632 while (req) {
1bdb176a 633 VirtIOBlockReq *next = req->next;
f1b52868 634 virtio_blk_handle_request(req, &mrb);
1bdb176a 635 req = next;
869a5c6d 636 }
f1b52868 637
95f7142a
PL
638 if (mrb.num_reqs) {
639 virtio_blk_submit_multireq(s->blk, &mrb);
640 }
869a5c6d
AL
641}
642
1dfb4dd9
LC
643static void virtio_blk_dma_restart_cb(void *opaque, int running,
644 RunState state)
213189ab
MA
645{
646 VirtIOBlock *s = opaque;
647
392808b4 648 if (!running) {
213189ab 649 return;
392808b4 650 }
213189ab
MA
651
652 if (!s->bh) {
4be74634 653 s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
4407c1c5 654 virtio_blk_dma_restart_bh, s);
213189ab
MA
655 qemu_bh_schedule(s->bh);
656 }
657}
658
6e02c38d
AL
659static void virtio_blk_reset(VirtIODevice *vdev)
660{
1cc91b7d 661 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e40b3bf 662 AioContext *ctx;
392808b4 663
6e02c38d
AL
664 /*
665 * This should cancel pending requests, but can't do nicely until there
666 * are per-device request lists.
667 */
6e40b3bf
AY
668 ctx = blk_get_aio_context(s->blk);
669 aio_context_acquire(ctx);
670 blk_drain(s->blk);
671
672 if (s->dataplane) {
673 virtio_blk_data_plane_stop(s->dataplane);
674 }
675 aio_context_release(ctx);
676
4be74634 677 blk_set_enable_write_cache(s->blk, s->original_wce);
6e02c38d
AL
678}
679
bf011293 680/* coalesce internal state, copy to pci i/o region 0
681 */
6e02c38d
AL
682static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
683{
1cc91b7d 684 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a30307f 685 BlockConf *conf = &s->conf.conf;
6e02c38d
AL
686 struct virtio_blk_config blkcfg;
687 uint64_t capacity;
f7516731 688 int blk_size = conf->logical_block_size;
6e02c38d 689
4be74634 690 blk_get_geometry(s->blk, &capacity);
5c5dafdc 691 memset(&blkcfg, 0, sizeof(blkcfg));
783d1897
RR
692 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
693 virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2);
907eb3e5 694 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
783d1897 695 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
f7516731
MA
696 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
697 virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
907eb3e5 698 blkcfg.geometry.heads = conf->heads;
136be99e
CB
699 /*
700 * We must ensure that the block device capacity is a multiple of
e03ba136 701 * the logical block size. If that is not the case, let's use
136be99e
CB
702 * sector_mask to adopt the geometry to have a correct picture.
703 * For those devices where the capacity is ok for the given geometry
e03ba136 704 * we don't touch the sector value of the geometry, since some devices
136be99e
CB
705 * (like s390 dasd) need a specific value. Here the capacity is already
706 * cyls*heads*secs*blk_size and the sector value is not block size
707 * divided by 512 - instead it is the amount of blk_size blocks
708 * per track (cylinder).
709 */
4be74634 710 if (blk_getlength(s->blk) / conf->heads / conf->secs % blk_size) {
907eb3e5 711 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
136be99e 712 } else {
907eb3e5 713 blkcfg.geometry.sectors = conf->secs;
136be99e 714 }
c7085da7 715 blkcfg.size_max = 0;
f7516731 716 blkcfg.physical_block_exp = get_physical_block_exp(conf);
9752c371 717 blkcfg.alignment_offset = 0;
4be74634 718 blkcfg.wce = blk_enable_write_cache(s->blk);
37d5ddd6 719 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
6e02c38d
AL
720}
721
13e3dce0
PB
722static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
723{
1cc91b7d 724 VirtIOBlock *s = VIRTIO_BLK(vdev);
13e3dce0
PB
725 struct virtio_blk_config blkcfg;
726
727 memcpy(&blkcfg, config, sizeof(blkcfg));
6d7e73d6 728
4be74634
MA
729 aio_context_acquire(blk_get_aio_context(s->blk));
730 blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
731 aio_context_release(blk_get_aio_context(s->blk));
13e3dce0
PB
732}
733
9d5b731d
JW
734static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
735 Error **errp)
6e02c38d 736{
1cc91b7d 737 VirtIOBlock *s = VIRTIO_BLK(vdev);
1063b8b1 738
0cd09c3a
CH
739 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
740 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
741 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
742 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
95129d6f 743 if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
efb8206c
JW
744 if (s->conf.scsi) {
745 error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
746 return 0;
747 }
efb8206c 748 } else {
c9b11f97 749 virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
efb8206c
JW
750 virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
751 }
aa659be3 752
2a30307f 753 if (s->conf.config_wce) {
0cd09c3a 754 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
8a873ba7 755 }
4be74634 756 if (blk_enable_write_cache(s->blk)) {
0cd09c3a 757 virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
4be74634
MA
758 }
759 if (blk_is_read_only(s->blk)) {
0cd09c3a 760 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
4be74634 761 }
1063b8b1
CH
762
763 return features;
6e02c38d
AL
764}
765
9315cbfd
PB
766static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
767{
1cc91b7d 768 VirtIOBlock *s = VIRTIO_BLK(vdev);
9315cbfd 769
cf139388
SH
770 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
771 VIRTIO_CONFIG_S_DRIVER_OK))) {
392808b4
SH
772 virtio_blk_data_plane_stop(s->dataplane);
773 }
392808b4 774
9315cbfd
PB
775 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
776 return;
777 }
778
ef5bc962
PB
779 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
780 * cache flushes. Thus, the "auto writethrough" behavior is never
781 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
782 * Leaving it enabled would break the following sequence:
783 *
784 * Guest started with "-drive cache=writethrough"
785 * Guest sets status to 0
786 * Guest sets DRIVER bit in status field
787 * Guest reads host features (WCE=0, CONFIG_WCE=1)
788 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
789 * Guest writes 1 to the WCE configuration field (writeback mode)
790 * Guest sets DRIVER_OK bit in status field
791 *
4be74634 792 * s->blk would erroneously be placed in writethrough mode.
ef5bc962 793 */
95129d6f 794 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
4be74634
MA
795 aio_context_acquire(blk_get_aio_context(s->blk));
796 blk_set_enable_write_cache(s->blk,
95129d6f
CH
797 virtio_vdev_has_feature(vdev,
798 VIRTIO_BLK_F_WCE));
4be74634 799 aio_context_release(blk_get_aio_context(s->blk));
ef5bc962 800 }
9315cbfd
PB
801}
802
6e02c38d
AL
803static void virtio_blk_save(QEMUFile *f, void *opaque)
804{
b2b295a7 805 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
10a06fd6
PB
806 VirtIOBlock *s = VIRTIO_BLK(vdev);
807
808 if (s->dataplane) {
809 virtio_blk_data_plane_stop(s->dataplane);
810 }
869a5c6d 811
1cc91b7d 812 virtio_save(vdev, f);
b2b295a7 813}
869a5c6d 814
b2b295a7
GK
815static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
816{
817 VirtIOBlock *s = VIRTIO_BLK(vdev);
818 VirtIOBlockReq *req = s->rq;
819
869a5c6d
AL
820 while (req) {
821 qemu_put_sbyte(f, 1);
f897bf75 822 qemu_put_buffer(f, (unsigned char *)&req->elem,
671ec3f0 823 sizeof(VirtQueueElement));
869a5c6d
AL
824 req = req->next;
825 }
826 qemu_put_sbyte(f, 0);
6e02c38d
AL
827}
828
829static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
830{
831 VirtIOBlock *s = opaque;
1cc91b7d 832 VirtIODevice *vdev = VIRTIO_DEVICE(s);
6e02c38d 833
869a5c6d 834 if (version_id != 2)
6e02c38d
AL
835 return -EINVAL;
836
b2b295a7
GK
837 return virtio_load(vdev, f, version_id);
838}
839
840static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
841 int version_id)
842{
843 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a633c46 844
869a5c6d
AL
845 while (qemu_get_sbyte(f)) {
846 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
f897bf75 847 qemu_get_buffer(f, (unsigned char *)&req->elem,
671ec3f0 848 sizeof(VirtQueueElement));
869a5c6d 849 req->next = s->rq;
20a81e4d 850 s->rq = req;
b6a4805b 851
3d8db153 852 virtqueue_map(&req->elem);
869a5c6d 853 }
6e02c38d
AL
854
855 return 0;
856}
857
145feb17 858static void virtio_blk_resize(void *opaque)
e5051fc7 859{
1cc91b7d 860 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
e5051fc7 861
1cc91b7d 862 virtio_notify_config(vdev);
e5051fc7
CH
863}
864
0e49de52 865static const BlockDevOps virtio_block_ops = {
145feb17 866 .resize_cb = virtio_blk_resize,
0e49de52
MA
867};
868
84db52d0
SH
869/* Disable dataplane thread during live migration since it does not
870 * update the dirty memory bitmap yet.
871 */
872static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
873{
874 VirtIOBlock *s = container_of(notifier, VirtIOBlock,
875 migration_state_notifier);
876 MigrationState *mig = data;
3ffeeef7 877 Error *err = NULL;
84db52d0
SH
878
879 if (migration_in_setup(mig)) {
880 if (!s->dataplane) {
881 return;
882 }
883 virtio_blk_data_plane_destroy(s->dataplane);
884 s->dataplane = NULL;
885 } else if (migration_has_finished(mig) ||
886 migration_has_failed(mig)) {
887 if (s->dataplane) {
888 return;
889 }
4be74634 890 blk_drain_all(); /* complete in-flight non-dataplane requests */
2a30307f 891 virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->conf,
3ffeeef7
AF
892 &s->dataplane, &err);
893 if (err != NULL) {
565f65d2 894 error_report_err(err);
3ffeeef7 895 }
84db52d0
SH
896 }
897}
84db52d0 898
75884afd 899static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1c028ddf 900{
75884afd 901 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179b417e 902 VirtIOBlock *s = VIRTIO_BLK(dev);
2a30307f 903 VirtIOBlkConf *conf = &s->conf;
3ffeeef7 904 Error *err = NULL;
6e02c38d 905 static int virtio_blk_id;
cf21e106 906
4be74634 907 if (!conf->conf.blk) {
75884afd
AF
908 error_setg(errp, "drive property not set");
909 return;
d75d25e3 910 }
4be74634 911 if (!blk_is_inserted(conf->conf.blk)) {
75884afd
AF
912 error_setg(errp, "Device needs media, but drive is empty");
913 return;
98f28ad7 914 }
d75d25e3 915
2a30307f 916 blkconf_serial(&conf->conf, &conf->serial);
4be74634 917 s->original_wce = blk_enable_write_cache(conf->conf.blk);
2a30307f 918 blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, &err);
5ff5efb4
FZ
919 if (err) {
920 error_propagate(errp, err);
75884afd 921 return;
b7eb0c9f 922 }
0eb28a42 923 blkconf_blocksizes(&conf->conf);
a8686a9b 924
05ff6865
FK
925 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
926 sizeof(struct virtio_blk_config));
6e02c38d 927
4be74634 928 s->blk = conf->conf.blk;
869a5c6d 929 s->rq = NULL;
2a30307f 930 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 931
05ff6865 932 s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
bf4bd461 933 s->complete_request = virtio_blk_complete_request;
2a30307f 934 virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
3ffeeef7 935 if (err != NULL) {
75884afd 936 error_propagate(errp, err);
6a1a8cc7 937 virtio_cleanup(vdev);
75884afd 938 return;
392808b4 939 }
84db52d0
SH
940 s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
941 add_migration_state_change_notifier(&s->migration_state_notifier);
6e02c38d 942
69b302b2 943 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
179b417e 944 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
6e02c38d 945 virtio_blk_save, virtio_blk_load, s);
4be74634
MA
946 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
947 blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
6e02c38d 948
4be74634 949 blk_iostatus_enable(s->blk);
1c028ddf
FK
950}
951
306ec6c3 952static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
1c028ddf 953{
306ec6c3
AF
954 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
955 VirtIOBlock *s = VIRTIO_BLK(dev);
956
84db52d0 957 remove_migration_state_change_notifier(&s->migration_state_notifier);
1c028ddf
FK
958 virtio_blk_data_plane_destroy(s->dataplane);
959 s->dataplane = NULL;
1c028ddf 960 qemu_del_vm_change_state_handler(s->change);
306ec6c3 961 unregister_savevm(dev, "virtio-blk", s);
4be74634 962 blockdev_mark_auto_del(s->blk);
6a1a8cc7 963 virtio_cleanup(vdev);
1c028ddf
FK
964}
965
467b3f33
SH
966static void virtio_blk_instance_init(Object *obj)
967{
968 VirtIOBlock *s = VIRTIO_BLK(obj);
969
970 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
2a30307f 971 (Object **)&s->conf.iothread,
467b3f33
SH
972 qdev_prop_allow_set_link_before_realize,
973 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
2a30307f 974 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
3342ec32
GA
975 "bootindex", "/disk@0,0",
976 DEVICE(obj), NULL);
467b3f33
SH
977}
978
1c028ddf 979static Property virtio_blk_properties[] = {
2a30307f
MA
980 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
981 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
982 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
983 DEFINE_PROP_BIT("config-wce", VirtIOBlock, conf.config_wce, 0, true),
32a877e4 984#ifdef __linux__
ed65fd1a 985 DEFINE_PROP_BIT("scsi", VirtIOBlock, conf.scsi, 0, false),
32a877e4 986#endif
c99495ac
PL
987 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
988 true),
1c028ddf
FK
989 DEFINE_PROP_END_OF_LIST(),
990};
991
992static void virtio_blk_class_init(ObjectClass *klass, void *data)
993{
994 DeviceClass *dc = DEVICE_CLASS(klass);
995 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
75884afd 996
1c028ddf 997 dc->props = virtio_blk_properties;
125ee0ed 998 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
75884afd 999 vdc->realize = virtio_blk_device_realize;
306ec6c3 1000 vdc->unrealize = virtio_blk_device_unrealize;
1c028ddf
FK
1001 vdc->get_config = virtio_blk_update_config;
1002 vdc->set_config = virtio_blk_set_config;
1003 vdc->get_features = virtio_blk_get_features;
1004 vdc->set_status = virtio_blk_set_status;
1005 vdc->reset = virtio_blk_reset;
b2b295a7
GK
1006 vdc->save = virtio_blk_save_device;
1007 vdc->load = virtio_blk_load_device;
1c028ddf
FK
1008}
1009
1010static const TypeInfo virtio_device_info = {
1011 .name = TYPE_VIRTIO_BLK,
1012 .parent = TYPE_VIRTIO_DEVICE,
1013 .instance_size = sizeof(VirtIOBlock),
467b3f33 1014 .instance_init = virtio_blk_instance_init,
1c028ddf
FK
1015 .class_init = virtio_blk_class_init,
1016};
1017
1018static void virtio_register_types(void)
1019{
1020 type_register_static(&virtio_device_info);
1021}
1022
1023type_init(virtio_register_types)