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