]> git.proxmox.com Git - mirror_qemu.git/blame - hw/block/virtio-blk.c
block-backend: add blk_get_max_iov()
[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) {
49cffbc6
GA
410 /*
411 * NOTE: We cannot merge the requests in below situations:
412 * 1. requests are not sequential
413 * 2. merge would exceed maximum number of IOVs
414 * 3. merge would exceed maximum transfer length of backend device
415 */
416 if (sector_num + nb_sectors != req->sector_num ||
417 niov > IOV_MAX - req->qiov.niov ||
418 req->qiov.size / BDRV_SECTOR_SIZE > max_xfer_len ||
419 nb_sectors > max_xfer_len - req->qiov.size / BDRV_SECTOR_SIZE) {
95f7142a
PL
420 submit_requests(blk, mrb, start, num_reqs, niov);
421 num_reqs = 0;
91553dcc
KW
422 }
423 }
95f7142a
PL
424
425 if (num_reqs == 0) {
426 sector_num = req->sector_num;
427 nb_sectors = niov = 0;
428 start = i;
429 }
430
431 nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
432 niov += req->qiov.niov;
433 num_reqs++;
91553dcc 434 }
c20fd872 435
95f7142a
PL
436 submit_requests(blk, mrb, start, num_reqs, niov);
437 mrb->num_reqs = 0;
91553dcc 438}
87b245db 439
c20fd872 440static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
aa659be3 441{
4be74634 442 block_acct_start(blk_get_stats(req->dev->blk), &req->acct, 0,
5366d0c8 443 BLOCK_ACCT_FLUSH);
a597e79c 444
618fbb84
CH
445 /*
446 * Make sure all outstanding writes are posted to the backing device.
447 */
95f7142a
PL
448 if (mrb->is_write && mrb->num_reqs > 0) {
449 virtio_blk_submit_multireq(req->dev->blk, mrb);
450 }
4be74634 451 blk_aio_flush(req->dev->blk, virtio_blk_flush_complete, req);
aa659be3
CH
452}
453
d0e14376
MA
454static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
455 uint64_t sector, size_t size)
456{
3c2daac0
MA
457 uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
458 uint64_t total_sectors;
459
75af1f34 460 if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
95f7142a
PL
461 return false;
462 }
d0e14376
MA
463 if (sector & dev->sector_mask) {
464 return false;
465 }
2a30307f 466 if (size % dev->conf.conf.logical_block_size) {
d0e14376
MA
467 return false;
468 }
4be74634 469 blk_get_geometry(dev->blk, &total_sectors);
3c2daac0
MA
470 if (sector > total_sectors || nb_sectors > total_sectors - sector) {
471 return false;
472 }
d0e14376
MA
473 return true;
474}
475
fee65db7 476void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
bc6694d4 477{
92e3c2a3 478 uint32_t type;
f897bf75
SH
479 struct iovec *in_iov = req->elem.in_sg;
480 struct iovec *iov = req->elem.out_sg;
481 unsigned in_num = req->elem.in_num;
482 unsigned out_num = req->elem.out_num;
92e3c2a3 483
f897bf75 484 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
870cef1d 485 error_report("virtio-blk missing headers");
bc6694d4
KW
486 exit(1);
487 }
488
827805a2
FZ
489 if (unlikely(iov_to_buf(iov, out_num, 0, &req->out,
490 sizeof(req->out)) != sizeof(req->out))) {
491 error_report("virtio-blk request outhdr too short");
492 exit(1);
493 }
ee17e848 494
827805a2 495 iov_discard_front(&iov, &out_num, sizeof(req->out));
ee17e848 496
12048545 497 if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
ee17e848
FZ
498 error_report("virtio-blk request inhdr too short");
499 exit(1);
500 }
501
2a6cdd6d
PB
502 /* We always touch the last byte, so just see how big in_iov is. */
503 req->in_len = iov_size(in_iov, in_num);
ee17e848
FZ
504 req->in = (void *)in_iov[in_num - 1].iov_base
505 + in_iov[in_num - 1].iov_len
506 - sizeof(struct virtio_blk_inhdr);
507 iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
bc6694d4 508
783d1897 509 type = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type);
92e3c2a3 510
95f7142a 511 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
631b22ea 512 * is an optional flag. Although a guest should not send this flag if
95f7142a
PL
513 * not negotiated we ignored it in the past. So keep ignoring it. */
514 switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
515 case VIRTIO_BLK_T_IN:
516 {
517 bool is_write = type & VIRTIO_BLK_T_OUT;
518 req->sector_num = virtio_ldq_p(VIRTIO_DEVICE(req->dev),
519 &req->out.sector);
520
521 if (is_write) {
522 qemu_iovec_init_external(&req->qiov, iov, out_num);
523 trace_virtio_blk_handle_write(req, req->sector_num,
524 req->qiov.size / BDRV_SECTOR_SIZE);
525 } else {
526 qemu_iovec_init_external(&req->qiov, in_iov, in_num);
527 trace_virtio_blk_handle_read(req, req->sector_num,
528 req->qiov.size / BDRV_SECTOR_SIZE);
529 }
530
531 if (!virtio_blk_sect_range_ok(req->dev, req->sector_num,
532 req->qiov.size)) {
533 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
01762e03
AG
534 block_acct_invalid(blk_get_stats(req->dev->blk),
535 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
95f7142a
PL
536 virtio_blk_free_request(req);
537 return;
538 }
539
540 block_acct_start(blk_get_stats(req->dev->blk),
541 &req->acct, req->qiov.size,
542 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
543
544 /* merge would exceed maximum number of requests or IO direction
545 * changes */
546 if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
c99495ac
PL
547 is_write != mrb->is_write ||
548 !req->dev->conf.request_merging)) {
95f7142a
PL
549 virtio_blk_submit_multireq(req->dev->blk, mrb);
550 }
551
552 assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
553 mrb->reqs[mrb->num_reqs++] = req;
554 mrb->is_write = is_write;
555 break;
556 }
557 case VIRTIO_BLK_T_FLUSH:
c20fd872 558 virtio_blk_handle_flush(req, mrb);
95f7142a
PL
559 break;
560 case VIRTIO_BLK_T_SCSI_CMD:
bc6694d4 561 virtio_blk_handle_scsi(req);
95f7142a
PL
562 break;
563 case VIRTIO_BLK_T_GET_ID:
564 {
2930b313 565 VirtIOBlock *s = req->dev;
566
a8686a9b
MA
567 /*
568 * NB: per existing s/n string convention the string is
569 * terminated by '\0' only when shorter than buffer.
570 */
2a30307f 571 const char *serial = s->conf.serial ? s->conf.serial : "";
a83ceea8
MM
572 size_t size = MIN(strlen(serial) + 1,
573 MIN(iov_size(in_iov, in_num),
574 VIRTIO_BLK_ID_BYTES));
575 iov_from_buf(in_iov, in_num, 0, serial, size);
2930b313 576 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
671ec3f0 577 virtio_blk_free_request(req);
95f7142a
PL
578 break;
579 }
580 default:
9e72c450 581 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
671ec3f0 582 virtio_blk_free_request(req);
bc6694d4
KW
583 }
584}
585
6e02c38d
AL
586static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
587{
1cc91b7d 588 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e02c38d 589 VirtIOBlockReq *req;
95f7142a 590 MultiReqBuffer mrb = {};
6e02c38d 591
392808b4
SH
592 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
593 * dataplane here instead of waiting for .set_status().
594 */
595 if (s->dataplane) {
596 virtio_blk_data_plane_start(s->dataplane);
597 return;
598 }
392808b4 599
fc73548e
SH
600 blk_io_plug(s->blk);
601
6e02c38d 602 while ((req = virtio_blk_get_request(s))) {
bc6694d4 603 virtio_blk_handle_request(req, &mrb);
6e02c38d 604 }
91553dcc 605
95f7142a
PL
606 if (mrb.num_reqs) {
607 virtio_blk_submit_multireq(s->blk, &mrb);
608 }
fc73548e
SH
609
610 blk_io_unplug(s->blk);
6e02c38d
AL
611}
612
213189ab 613static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
614{
615 VirtIOBlock *s = opaque;
616 VirtIOBlockReq *req = s->rq;
95f7142a 617 MultiReqBuffer mrb = {};
869a5c6d 618
213189ab
MA
619 qemu_bh_delete(s->bh);
620 s->bh = NULL;
869a5c6d
AL
621
622 s->rq = NULL;
623
624 while (req) {
1bdb176a 625 VirtIOBlockReq *next = req->next;
f1b52868 626 virtio_blk_handle_request(req, &mrb);
1bdb176a 627 req = next;
869a5c6d 628 }
f1b52868 629
95f7142a
PL
630 if (mrb.num_reqs) {
631 virtio_blk_submit_multireq(s->blk, &mrb);
632 }
869a5c6d
AL
633}
634
1dfb4dd9
LC
635static void virtio_blk_dma_restart_cb(void *opaque, int running,
636 RunState state)
213189ab
MA
637{
638 VirtIOBlock *s = opaque;
639
392808b4 640 if (!running) {
213189ab 641 return;
392808b4 642 }
213189ab
MA
643
644 if (!s->bh) {
4be74634 645 s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
4407c1c5 646 virtio_blk_dma_restart_bh, s);
213189ab
MA
647 qemu_bh_schedule(s->bh);
648 }
649}
650
6e02c38d
AL
651static void virtio_blk_reset(VirtIODevice *vdev)
652{
1cc91b7d 653 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e40b3bf 654 AioContext *ctx;
392808b4 655
6e02c38d
AL
656 /*
657 * This should cancel pending requests, but can't do nicely until there
658 * are per-device request lists.
659 */
6e40b3bf
AY
660 ctx = blk_get_aio_context(s->blk);
661 aio_context_acquire(ctx);
662 blk_drain(s->blk);
663
664 if (s->dataplane) {
665 virtio_blk_data_plane_stop(s->dataplane);
666 }
667 aio_context_release(ctx);
668
4be74634 669 blk_set_enable_write_cache(s->blk, s->original_wce);
6e02c38d
AL
670}
671
bf011293 672/* coalesce internal state, copy to pci i/o region 0
673 */
6e02c38d
AL
674static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
675{
1cc91b7d 676 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a30307f 677 BlockConf *conf = &s->conf.conf;
6e02c38d
AL
678 struct virtio_blk_config blkcfg;
679 uint64_t capacity;
f7516731 680 int blk_size = conf->logical_block_size;
6e02c38d 681
4be74634 682 blk_get_geometry(s->blk, &capacity);
5c5dafdc 683 memset(&blkcfg, 0, sizeof(blkcfg));
783d1897
RR
684 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
685 virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2);
907eb3e5 686 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
783d1897 687 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
f7516731
MA
688 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
689 virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
907eb3e5 690 blkcfg.geometry.heads = conf->heads;
136be99e
CB
691 /*
692 * We must ensure that the block device capacity is a multiple of
e03ba136 693 * the logical block size. If that is not the case, let's use
136be99e
CB
694 * sector_mask to adopt the geometry to have a correct picture.
695 * For those devices where the capacity is ok for the given geometry
e03ba136 696 * we don't touch the sector value of the geometry, since some devices
136be99e
CB
697 * (like s390 dasd) need a specific value. Here the capacity is already
698 * cyls*heads*secs*blk_size and the sector value is not block size
699 * divided by 512 - instead it is the amount of blk_size blocks
700 * per track (cylinder).
701 */
4be74634 702 if (blk_getlength(s->blk) / conf->heads / conf->secs % blk_size) {
907eb3e5 703 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
136be99e 704 } else {
907eb3e5 705 blkcfg.geometry.sectors = conf->secs;
136be99e 706 }
c7085da7 707 blkcfg.size_max = 0;
f7516731 708 blkcfg.physical_block_exp = get_physical_block_exp(conf);
9752c371 709 blkcfg.alignment_offset = 0;
4be74634 710 blkcfg.wce = blk_enable_write_cache(s->blk);
37d5ddd6 711 memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
6e02c38d
AL
712}
713
13e3dce0
PB
714static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
715{
1cc91b7d 716 VirtIOBlock *s = VIRTIO_BLK(vdev);
13e3dce0
PB
717 struct virtio_blk_config blkcfg;
718
719 memcpy(&blkcfg, config, sizeof(blkcfg));
6d7e73d6 720
4be74634
MA
721 aio_context_acquire(blk_get_aio_context(s->blk));
722 blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
723 aio_context_release(blk_get_aio_context(s->blk));
13e3dce0
PB
724}
725
9d5b731d
JW
726static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
727 Error **errp)
6e02c38d 728{
1cc91b7d 729 VirtIOBlock *s = VIRTIO_BLK(vdev);
1063b8b1 730
0cd09c3a
CH
731 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
732 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
733 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
734 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
95129d6f 735 if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
efb8206c
JW
736 if (s->conf.scsi) {
737 error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
738 return 0;
739 }
efb8206c 740 } else {
c9b11f97 741 virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
efb8206c
JW
742 virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
743 }
aa659be3 744
2a30307f 745 if (s->conf.config_wce) {
0cd09c3a 746 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
8a873ba7 747 }
4be74634 748 if (blk_enable_write_cache(s->blk)) {
0cd09c3a 749 virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
4be74634
MA
750 }
751 if (blk_is_read_only(s->blk)) {
0cd09c3a 752 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
4be74634 753 }
1063b8b1
CH
754
755 return features;
6e02c38d
AL
756}
757
9315cbfd
PB
758static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
759{
1cc91b7d 760 VirtIOBlock *s = VIRTIO_BLK(vdev);
9315cbfd 761
cf139388
SH
762 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
763 VIRTIO_CONFIG_S_DRIVER_OK))) {
392808b4
SH
764 virtio_blk_data_plane_stop(s->dataplane);
765 }
392808b4 766
9315cbfd
PB
767 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
768 return;
769 }
770
ef5bc962
PB
771 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
772 * cache flushes. Thus, the "auto writethrough" behavior is never
773 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
774 * Leaving it enabled would break the following sequence:
775 *
776 * Guest started with "-drive cache=writethrough"
777 * Guest sets status to 0
778 * Guest sets DRIVER bit in status field
779 * Guest reads host features (WCE=0, CONFIG_WCE=1)
780 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
781 * Guest writes 1 to the WCE configuration field (writeback mode)
782 * Guest sets DRIVER_OK bit in status field
783 *
4be74634 784 * s->blk would erroneously be placed in writethrough mode.
ef5bc962 785 */
95129d6f 786 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
4be74634
MA
787 aio_context_acquire(blk_get_aio_context(s->blk));
788 blk_set_enable_write_cache(s->blk,
95129d6f
CH
789 virtio_vdev_has_feature(vdev,
790 VIRTIO_BLK_F_WCE));
4be74634 791 aio_context_release(blk_get_aio_context(s->blk));
ef5bc962 792 }
9315cbfd
PB
793}
794
6e02c38d
AL
795static void virtio_blk_save(QEMUFile *f, void *opaque)
796{
b2b295a7 797 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
10a06fd6
PB
798 VirtIOBlock *s = VIRTIO_BLK(vdev);
799
800 if (s->dataplane) {
801 virtio_blk_data_plane_stop(s->dataplane);
802 }
869a5c6d 803
1cc91b7d 804 virtio_save(vdev, f);
b2b295a7 805}
869a5c6d 806
b2b295a7
GK
807static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
808{
809 VirtIOBlock *s = VIRTIO_BLK(vdev);
810 VirtIOBlockReq *req = s->rq;
811
869a5c6d
AL
812 while (req) {
813 qemu_put_sbyte(f, 1);
f897bf75 814 qemu_put_buffer(f, (unsigned char *)&req->elem,
671ec3f0 815 sizeof(VirtQueueElement));
869a5c6d
AL
816 req = req->next;
817 }
818 qemu_put_sbyte(f, 0);
6e02c38d
AL
819}
820
821static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
822{
823 VirtIOBlock *s = opaque;
1cc91b7d 824 VirtIODevice *vdev = VIRTIO_DEVICE(s);
6e02c38d 825
869a5c6d 826 if (version_id != 2)
6e02c38d
AL
827 return -EINVAL;
828
b2b295a7
GK
829 return virtio_load(vdev, f, version_id);
830}
831
832static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
833 int version_id)
834{
835 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a633c46 836
869a5c6d
AL
837 while (qemu_get_sbyte(f)) {
838 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
f897bf75 839 qemu_get_buffer(f, (unsigned char *)&req->elem,
671ec3f0 840 sizeof(VirtQueueElement));
869a5c6d 841 req->next = s->rq;
20a81e4d 842 s->rq = req;
b6a4805b 843
3d8db153 844 virtqueue_map(&req->elem);
869a5c6d 845 }
6e02c38d
AL
846
847 return 0;
848}
849
145feb17 850static void virtio_blk_resize(void *opaque)
e5051fc7 851{
1cc91b7d 852 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
e5051fc7 853
1cc91b7d 854 virtio_notify_config(vdev);
e5051fc7
CH
855}
856
0e49de52 857static const BlockDevOps virtio_block_ops = {
145feb17 858 .resize_cb = virtio_blk_resize,
0e49de52
MA
859};
860
84db52d0
SH
861/* Disable dataplane thread during live migration since it does not
862 * update the dirty memory bitmap yet.
863 */
864static void virtio_blk_migration_state_changed(Notifier *notifier, void *data)
865{
866 VirtIOBlock *s = container_of(notifier, VirtIOBlock,
867 migration_state_notifier);
868 MigrationState *mig = data;
3ffeeef7 869 Error *err = NULL;
84db52d0
SH
870
871 if (migration_in_setup(mig)) {
872 if (!s->dataplane) {
873 return;
874 }
875 virtio_blk_data_plane_destroy(s->dataplane);
876 s->dataplane = NULL;
877 } else if (migration_has_finished(mig) ||
878 migration_has_failed(mig)) {
879 if (s->dataplane) {
880 return;
881 }
4be74634 882 blk_drain_all(); /* complete in-flight non-dataplane requests */
2a30307f 883 virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->conf,
3ffeeef7
AF
884 &s->dataplane, &err);
885 if (err != NULL) {
565f65d2 886 error_report_err(err);
3ffeeef7 887 }
84db52d0
SH
888 }
889}
84db52d0 890
75884afd 891static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1c028ddf 892{
75884afd 893 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179b417e 894 VirtIOBlock *s = VIRTIO_BLK(dev);
2a30307f 895 VirtIOBlkConf *conf = &s->conf;
3ffeeef7 896 Error *err = NULL;
6e02c38d 897 static int virtio_blk_id;
cf21e106 898
4be74634 899 if (!conf->conf.blk) {
75884afd
AF
900 error_setg(errp, "drive property not set");
901 return;
d75d25e3 902 }
4be74634 903 if (!blk_is_inserted(conf->conf.blk)) {
75884afd
AF
904 error_setg(errp, "Device needs media, but drive is empty");
905 return;
98f28ad7 906 }
d75d25e3 907
2a30307f 908 blkconf_serial(&conf->conf, &conf->serial);
4be74634 909 s->original_wce = blk_enable_write_cache(conf->conf.blk);
2a30307f 910 blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, &err);
5ff5efb4
FZ
911 if (err) {
912 error_propagate(errp, err);
75884afd 913 return;
b7eb0c9f 914 }
0eb28a42 915 blkconf_blocksizes(&conf->conf);
a8686a9b 916
05ff6865
FK
917 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
918 sizeof(struct virtio_blk_config));
6e02c38d 919
4be74634 920 s->blk = conf->conf.blk;
869a5c6d 921 s->rq = NULL;
2a30307f 922 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 923
05ff6865 924 s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output);
bf4bd461 925 s->complete_request = virtio_blk_complete_request;
2a30307f 926 virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
3ffeeef7 927 if (err != NULL) {
75884afd 928 error_propagate(errp, err);
6a1a8cc7 929 virtio_cleanup(vdev);
75884afd 930 return;
392808b4 931 }
84db52d0
SH
932 s->migration_state_notifier.notify = virtio_blk_migration_state_changed;
933 add_migration_state_change_notifier(&s->migration_state_notifier);
6e02c38d 934
69b302b2 935 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
179b417e 936 register_savevm(dev, "virtio-blk", virtio_blk_id++, 2,
6e02c38d 937 virtio_blk_save, virtio_blk_load, s);
4be74634
MA
938 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
939 blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
6e02c38d 940
4be74634 941 blk_iostatus_enable(s->blk);
1c028ddf
FK
942}
943
306ec6c3 944static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
1c028ddf 945{
306ec6c3
AF
946 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
947 VirtIOBlock *s = VIRTIO_BLK(dev);
948
84db52d0 949 remove_migration_state_change_notifier(&s->migration_state_notifier);
1c028ddf
FK
950 virtio_blk_data_plane_destroy(s->dataplane);
951 s->dataplane = NULL;
1c028ddf 952 qemu_del_vm_change_state_handler(s->change);
306ec6c3 953 unregister_savevm(dev, "virtio-blk", s);
4be74634 954 blockdev_mark_auto_del(s->blk);
6a1a8cc7 955 virtio_cleanup(vdev);
1c028ddf
FK
956}
957
467b3f33
SH
958static void virtio_blk_instance_init(Object *obj)
959{
960 VirtIOBlock *s = VIRTIO_BLK(obj);
961
962 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
2a30307f 963 (Object **)&s->conf.iothread,
467b3f33
SH
964 qdev_prop_allow_set_link_before_realize,
965 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
2a30307f 966 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
3342ec32
GA
967 "bootindex", "/disk@0,0",
968 DEVICE(obj), NULL);
467b3f33
SH
969}
970
1c028ddf 971static Property virtio_blk_properties[] = {
2a30307f
MA
972 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
973 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
974 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
975 DEFINE_PROP_BIT("config-wce", VirtIOBlock, conf.config_wce, 0, true),
32a877e4 976#ifdef __linux__
ed65fd1a 977 DEFINE_PROP_BIT("scsi", VirtIOBlock, conf.scsi, 0, false),
32a877e4 978#endif
c99495ac
PL
979 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
980 true),
1c028ddf
FK
981 DEFINE_PROP_END_OF_LIST(),
982};
983
984static void virtio_blk_class_init(ObjectClass *klass, void *data)
985{
986 DeviceClass *dc = DEVICE_CLASS(klass);
987 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
75884afd 988
1c028ddf 989 dc->props = virtio_blk_properties;
125ee0ed 990 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
75884afd 991 vdc->realize = virtio_blk_device_realize;
306ec6c3 992 vdc->unrealize = virtio_blk_device_unrealize;
1c028ddf
FK
993 vdc->get_config = virtio_blk_update_config;
994 vdc->set_config = virtio_blk_set_config;
995 vdc->get_features = virtio_blk_get_features;
996 vdc->set_status = virtio_blk_set_status;
997 vdc->reset = virtio_blk_reset;
b2b295a7
GK
998 vdc->save = virtio_blk_save_device;
999 vdc->load = virtio_blk_load_device;
1c028ddf
FK
1000}
1001
1002static const TypeInfo virtio_device_info = {
1003 .name = TYPE_VIRTIO_BLK,
1004 .parent = TYPE_VIRTIO_DEVICE,
1005 .instance_size = sizeof(VirtIOBlock),
467b3f33 1006 .instance_init = virtio_blk_instance_init,
1c028ddf
FK
1007 .class_init = virtio_blk_class_init,
1008};
1009
1010static void virtio_register_types(void)
1011{
1012 type_register_static(&virtio_device_info);
1013}
1014
1015type_init(virtio_register_types)