]> Git Repo - qemu.git/blame - hw/block/virtio-blk.c
linux-aio: process completions from ioq_submit()
[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 <[email protected]>
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
edaffd9f
SH
32void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
33 VirtIOBlockReq *req)
671ec3f0 34{
671ec3f0 35 req->dev = s;
edaffd9f 36 req->vq = vq;
869d66af 37 req->qiov.size = 0;
2a6cdd6d 38 req->in_len = 0;
869d66af 39 req->next = NULL;
95f7142a 40 req->mr_next = NULL;
671ec3f0
FZ
41}
42
f897bf75 43void virtio_blk_free_request(VirtIOBlockReq *req)
671ec3f0
FZ
44{
45 if (req) {
c84b3192 46 g_free(req);
671ec3f0
FZ
47 }
48}
49
03de2f52 50static void virtio_blk_req_complete(VirtIOBlockReq *req, 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);
edaffd9f 58 virtqueue_push(req->vq, &req->elem, req->in_len);
eb41cf78 59 if (s->dataplane_started && !s->dataplane_disabled) {
edaffd9f 60 virtio_blk_data_plane_notify(s->dataplane, req->vq);
03de2f52 61 } else {
edaffd9f 62 virtio_notify(vdev, req->vq);
03de2f52 63 }
bf4bd461
FZ
64}
65
f35d68f0 66static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
1ceee0d5 67 bool is_read)
869a5c6d 68{
4be74634
MA
69 BlockErrorAction action = blk_get_error_action(req->dev->blk,
70 is_read, error);
869a5c6d
AL
71 VirtIOBlock *s = req->dev;
72
a589569f 73 if (action == BLOCK_ERROR_ACTION_STOP) {
466138dc
FZ
74 /* Break the link as the next request is going to be parsed from the
75 * ring again. Otherwise we may end up doing a double completion! */
76 req->mr_next = NULL;
869a5c6d
AL
77 req->next = s->rq;
78 s->rq = req;
a589569f 79 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
869a5c6d 80 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
01762e03 81 block_acct_failed(blk_get_stats(s->blk), &req->acct);
671ec3f0 82 virtio_blk_free_request(req);
869a5c6d
AL
83 }
84
4be74634 85 blk_error_action(s->blk, action, is_read, error);
a589569f 86 return action != BLOCK_ERROR_ACTION_IGNORE;
869a5c6d
AL
87}
88
6e02c38d
AL
89static void virtio_blk_rw_complete(void *opaque, int ret)
90{
95f7142a
PL
91 VirtIOBlockReq *next = opaque;
92
93 while (next) {
94 VirtIOBlockReq *req = next;
95 next = req->mr_next;
96 trace_virtio_blk_rw_complete(req, ret);
97
98 if (req->qiov.nalloc != -1) {
99 /* If nalloc is != 1 req->qiov is a local copy of the original
100 * external iovec. It was allocated in submit_merged_requests
101 * to be able to merge requests. */
102 qemu_iovec_destroy(&req->qiov);
103 }
6e02c38d 104
95f7142a
PL
105 if (ret) {
106 int p = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type);
107 bool is_read = !(p & VIRTIO_BLK_T_OUT);
2a6cdd6d
PB
108 /* Note that memory may be dirtied on read failure. If the
109 * virtio request is not completed here, as is the case for
110 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
111 * correctly during live migration. While this is ugly,
112 * it is acceptable because the device is free to write to
113 * the memory until the request is completed (which will
114 * happen on the other side of the migration).
115 */
95f7142a
PL
116 if (virtio_blk_handle_rw_error(req, -ret, is_read)) {
117 continue;
118 }
119 }
6d519a5f 120
95f7142a
PL
121 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
122 block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
123 virtio_blk_free_request(req);
6e02c38d 124 }
869a5c6d 125}
6e02c38d 126
aa659be3
CH
127static void virtio_blk_flush_complete(void *opaque, int ret)
128{
129 VirtIOBlockReq *req = opaque;
130
8c269b54
KW
131 if (ret) {
132 if (virtio_blk_handle_rw_error(req, -ret, 0)) {
133 return;
134 }
135 }
136
137 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
4be74634 138 block_acct_done(blk_get_stats(req->dev->blk), &req->acct);
671ec3f0 139 virtio_blk_free_request(req);
6e02c38d
AL
140}
141
1dc936aa
FZ
142#ifdef __linux__
143
144typedef struct {
145 VirtIOBlockReq *req;
146 struct sg_io_hdr hdr;
147} VirtIOBlockIoctlReq;
148
149static void virtio_blk_ioctl_complete(void *opaque, int status)
150{
151 VirtIOBlockIoctlReq *ioctl_req = opaque;
152 VirtIOBlockReq *req = ioctl_req->req;
153 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
154 struct virtio_scsi_inhdr *scsi;
155 struct sg_io_hdr *hdr;
156
157 scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
158
159 if (status) {
160 status = VIRTIO_BLK_S_UNSUPP;
161 virtio_stl_p(vdev, &scsi->errors, 255);
162 goto out;
163 }
164
165 hdr = &ioctl_req->hdr;
166 /*
167 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
168 * clear the masked_status field [hence status gets cleared too, see
169 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
170 * status has occurred. However they do set DRIVER_SENSE in driver_status
171 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
172 */
173 if (hdr->status == 0 && hdr->sb_len_wr > 0) {
174 hdr->status = CHECK_CONDITION;
175 }
176
177 virtio_stl_p(vdev, &scsi->errors,
178 hdr->status | (hdr->msg_status << 8) |
179 (hdr->host_status << 16) | (hdr->driver_status << 24));
180 virtio_stl_p(vdev, &scsi->residual, hdr->resid);
181 virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
182 virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
183
184out:
185 virtio_blk_req_complete(req, status);
186 virtio_blk_free_request(req);
187 g_free(ioctl_req);
188}
189
190#endif
191
edaffd9f 192static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 193{
edaffd9f 194 VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
6e02c38d 195
51b19ebe 196 if (req) {
edaffd9f 197 virtio_blk_init_request(s, vq, req);
6e02c38d 198 }
6e02c38d
AL
199 return req;
200}
201
75344fa4 202static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
1063b8b1 203{
5a05cbee
FZ
204 int status = VIRTIO_BLK_S_OK;
205 struct virtio_scsi_inhdr *scsi = NULL;
75344fa4
FZ
206 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
207 VirtQueueElement *elem = &req->elem;
208 VirtIOBlock *blk = req->dev;
783d1897 209
47ce9ef7 210#ifdef __linux__
1063b8b1 211 int i;
1dc936aa 212 VirtIOBlockIoctlReq *ioctl_req;
a209f461 213 BlockAIOCB *acb;
47ce9ef7 214#endif
1063b8b1
CH
215
216 /*
217 * We require at least one output segment each for the virtio_blk_outhdr
218 * and the SCSI command block.
219 *
220 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
221 * and the sense buffer pointer in the input segments.
222 */
5a05cbee
FZ
223 if (elem->out_num < 2 || elem->in_num < 3) {
224 status = VIRTIO_BLK_S_IOERR;
225 goto fail;
1063b8b1
CH
226 }
227
228 /*
f34e73cd
PB
229 * The scsi inhdr is placed in the second-to-last input segment, just
230 * before the regular inhdr.
1063b8b1 231 */
5a05cbee 232 scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
f34e73cd 233
2a30307f 234 if (!blk->conf.scsi) {
f34e73cd
PB
235 status = VIRTIO_BLK_S_UNSUPP;
236 goto fail;
1063b8b1
CH
237 }
238
239 /*
f34e73cd 240 * No support for bidirection commands yet.
1063b8b1 241 */
5a05cbee 242 if (elem->out_num > 2 && elem->in_num > 3) {
f34e73cd
PB
243 status = VIRTIO_BLK_S_UNSUPP;
244 goto fail;
245 }
1063b8b1 246
f34e73cd 247#ifdef __linux__
1dc936aa
FZ
248 ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
249 ioctl_req->req = req;
250 ioctl_req->hdr.interface_id = 'S';
251 ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
252 ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
253 ioctl_req->hdr.dxfer_len = 0;
1063b8b1 254
5a05cbee 255 if (elem->out_num > 2) {
1063b8b1
CH
256 /*
257 * If there are more than the minimally required 2 output segments
258 * there is write payload starting from the third iovec.
259 */
1dc936aa
FZ
260 ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
261 ioctl_req->hdr.iovec_count = elem->out_num - 2;
1063b8b1 262
1dc936aa
FZ
263 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
264 ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
265 }
1063b8b1 266
1dc936aa 267 ioctl_req->hdr.dxferp = elem->out_sg + 2;
1063b8b1 268
5a05cbee 269 } else if (elem->in_num > 3) {
1063b8b1
CH
270 /*
271 * If we have more than 3 input segments the guest wants to actually
272 * read data.
273 */
1dc936aa
FZ
274 ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
275 ioctl_req->hdr.iovec_count = elem->in_num - 3;
276 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
277 ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
278 }
1063b8b1 279
1dc936aa 280 ioctl_req->hdr.dxferp = elem->in_sg;
1063b8b1
CH
281 } else {
282 /*
283 * Some SCSI commands don't actually transfer any data.
284 */
1dc936aa 285 ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
1063b8b1
CH
286 }
287
1dc936aa
FZ
288 ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
289 ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
1063b8b1 290
a209f461
FZ
291 acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
292 virtio_blk_ioctl_complete, ioctl_req);
293 if (!acb) {
294 g_free(ioctl_req);
295 status = VIRTIO_BLK_S_UNSUPP;
296 goto fail;
297 }
1dc936aa 298 return -EINPROGRESS;
1063b8b1 299#else
f34e73cd
PB
300 abort();
301#endif
302
303fail:
304 /* Just put anything nonzero so that the ioctl fails in the guest. */
5a05cbee 305 if (scsi) {
783d1897 306 virtio_stl_p(vdev, &scsi->errors, 255);
5a05cbee
FZ
307 }
308 return status;
309}
310
311static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
312{
313 int status;
314
75344fa4 315 status = virtio_blk_handle_scsi_req(req);
1dc936aa
FZ
316 if (status != -EINPROGRESS) {
317 virtio_blk_req_complete(req, status);
318 virtio_blk_free_request(req);
319 }
1063b8b1 320}
1063b8b1 321
95f7142a
PL
322static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
323 int start, int num_reqs, int niov)
869a5c6d 324{
95f7142a
PL
325 QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
326 int64_t sector_num = mrb->reqs[start]->sector_num;
95f7142a
PL
327 bool is_write = mrb->is_write;
328
329 if (num_reqs > 1) {
330 int i;
331 struct iovec *tmp_iov = qiov->iov;
332 int tmp_niov = qiov->niov;
333
334 /* mrb->reqs[start]->qiov was initialized from external so we can't
b5772fdd 335 * modify it here. We need to initialize it locally and then add the
95f7142a
PL
336 * external iovecs. */
337 qemu_iovec_init(qiov, niov);
338
339 for (i = 0; i < tmp_niov; i++) {
340 qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
341 }
342
343 for (i = start + 1; i < start + num_reqs; i++) {
344 qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
345 mrb->reqs[i]->qiov.size);
346 mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
95f7142a 347 }
95f7142a 348
b5772fdd
EB
349 trace_virtio_blk_submit_multireq(mrb, start, num_reqs,
350 sector_num << BDRV_SECTOR_BITS,
351 qiov->size, is_write);
95f7142a
PL
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 357 if (is_write) {
b5772fdd
EB
358 blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
359 virtio_blk_rw_complete, mrb->reqs[start]);
95f7142a 360 } else {
b5772fdd
EB
361 blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0,
362 virtio_blk_rw_complete, mrb->reqs[start]);
95f7142a
PL
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;
5def6b80 387 uint32_t max_transfer;
95f7142a
PL
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
5def6b80 396 max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
95f7142a
PL
397
398 qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
399 &multireq_compare);
400
401 for (i = 0; i < mrb->num_reqs; i++) {
402 VirtIOBlockReq *req = mrb->reqs[i];
403 if (num_reqs > 0) {
49cffbc6
GA
404 /*
405 * NOTE: We cannot merge the requests in below situations:
406 * 1. requests are not sequential
407 * 2. merge would exceed maximum number of IOVs
408 * 3. merge would exceed maximum transfer length of backend device
409 */
410 if (sector_num + nb_sectors != req->sector_num ||
222565f6 411 niov > blk_get_max_iov(blk) - req->qiov.niov ||
5def6b80
EB
412 req->qiov.size > max_transfer ||
413 nb_sectors > (max_transfer -
414 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
8a2fad57 581void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 582{
6e02c38d 583 VirtIOBlockReq *req;
95f7142a 584 MultiReqBuffer mrb = {};
6e02c38d 585
fc73548e
SH
586 blk_io_plug(s->blk);
587
edaffd9f 588 while ((req = virtio_blk_get_request(s, vq))) {
bc6694d4 589 virtio_blk_handle_request(req, &mrb);
6e02c38d 590 }
91553dcc 591
95f7142a
PL
592 if (mrb.num_reqs) {
593 virtio_blk_submit_multireq(s->blk, &mrb);
594 }
fc73548e
SH
595
596 blk_io_unplug(s->blk);
6e02c38d
AL
597}
598
8a2fad57
MT
599static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
600{
601 VirtIOBlock *s = (VirtIOBlock *)vdev;
602
603 if (s->dataplane) {
604 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
605 * dataplane here instead of waiting for .set_status().
606 */
607 virtio_blk_data_plane_start(s->dataplane);
608 if (!s->dataplane_disabled) {
609 return;
610 }
611 }
612 virtio_blk_handle_vq(s, vq);
613}
614
213189ab 615static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
616{
617 VirtIOBlock *s = opaque;
618 VirtIOBlockReq *req = s->rq;
95f7142a 619 MultiReqBuffer mrb = {};
869a5c6d 620
213189ab
MA
621 qemu_bh_delete(s->bh);
622 s->bh = NULL;
869a5c6d
AL
623
624 s->rq = NULL;
625
626 while (req) {
1bdb176a 627 VirtIOBlockReq *next = req->next;
f1b52868 628 virtio_blk_handle_request(req, &mrb);
1bdb176a 629 req = next;
869a5c6d 630 }
f1b52868 631
95f7142a
PL
632 if (mrb.num_reqs) {
633 virtio_blk_submit_multireq(s->blk, &mrb);
634 }
869a5c6d
AL
635}
636
1dfb4dd9
LC
637static void virtio_blk_dma_restart_cb(void *opaque, int running,
638 RunState state)
213189ab
MA
639{
640 VirtIOBlock *s = opaque;
641
392808b4 642 if (!running) {
213189ab 643 return;
392808b4 644 }
213189ab
MA
645
646 if (!s->bh) {
4be74634 647 s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
4407c1c5 648 virtio_blk_dma_restart_bh, s);
213189ab
MA
649 qemu_bh_schedule(s->bh);
650 }
651}
652
6e02c38d
AL
653static void virtio_blk_reset(VirtIODevice *vdev)
654{
1cc91b7d 655 VirtIOBlock *s = VIRTIO_BLK(vdev);
6e40b3bf 656 AioContext *ctx;
26307f6a 657 VirtIOBlockReq *req;
392808b4 658
6e40b3bf
AY
659 ctx = blk_get_aio_context(s->blk);
660 aio_context_acquire(ctx);
661 blk_drain(s->blk);
662
26307f6a
FZ
663 /* We drop queued requests after blk_drain() because blk_drain() itself can
664 * produce them. */
665 while (s->rq) {
666 req = s->rq;
667 s->rq = req->next;
668 virtio_blk_free_request(req);
669 }
670
6e40b3bf
AY
671 if (s->dataplane) {
672 virtio_blk_data_plane_stop(s->dataplane);
673 }
674 aio_context_release(ctx);
675
4be74634 676 blk_set_enable_write_cache(s->blk, s->original_wce);
6e02c38d
AL
677}
678
bf011293 679/* coalesce internal state, copy to pci i/o region 0
680 */
6e02c38d
AL
681static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
682{
1cc91b7d 683 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a30307f 684 BlockConf *conf = &s->conf.conf;
6e02c38d
AL
685 struct virtio_blk_config blkcfg;
686 uint64_t capacity;
f7516731 687 int blk_size = conf->logical_block_size;
6e02c38d 688
4be74634 689 blk_get_geometry(s->blk, &capacity);
5c5dafdc 690 memset(&blkcfg, 0, sizeof(blkcfg));
783d1897
RR
691 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
692 virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2);
907eb3e5 693 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
783d1897 694 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
f7516731
MA
695 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
696 virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
907eb3e5 697 blkcfg.geometry.heads = conf->heads;
136be99e
CB
698 /*
699 * We must ensure that the block device capacity is a multiple of
e03ba136 700 * the logical block size. If that is not the case, let's use
136be99e
CB
701 * sector_mask to adopt the geometry to have a correct picture.
702 * For those devices where the capacity is ok for the given geometry
e03ba136 703 * we don't touch the sector value of the geometry, since some devices
136be99e
CB
704 * (like s390 dasd) need a specific value. Here the capacity is already
705 * cyls*heads*secs*blk_size and the sector value is not block size
706 * divided by 512 - instead it is the amount of blk_size blocks
707 * per track (cylinder).
708 */
4be74634 709 if (blk_getlength(s->blk) / conf->heads / conf->secs % blk_size) {
907eb3e5 710 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
136be99e 711 } else {
907eb3e5 712 blkcfg.geometry.sectors = conf->secs;
136be99e 713 }
c7085da7 714 blkcfg.size_max = 0;
f7516731 715 blkcfg.physical_block_exp = get_physical_block_exp(conf);
9752c371 716 blkcfg.alignment_offset = 0;
4be74634 717 blkcfg.wce = blk_enable_write_cache(s->blk);
2f270590 718 virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
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 }
2f270590
SH
762 if (s->conf.num_queues > 1) {
763 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
764 }
1063b8b1
CH
765
766 return features;
6e02c38d
AL
767}
768
9315cbfd
PB
769static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
770{
1cc91b7d 771 VirtIOBlock *s = VIRTIO_BLK(vdev);
9315cbfd 772
cf139388
SH
773 if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER |
774 VIRTIO_CONFIG_S_DRIVER_OK))) {
392808b4
SH
775 virtio_blk_data_plane_stop(s->dataplane);
776 }
392808b4 777
9315cbfd
PB
778 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
779 return;
780 }
781
ef5bc962
PB
782 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
783 * cache flushes. Thus, the "auto writethrough" behavior is never
784 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
785 * Leaving it enabled would break the following sequence:
786 *
787 * Guest started with "-drive cache=writethrough"
788 * Guest sets status to 0
789 * Guest sets DRIVER bit in status field
790 * Guest reads host features (WCE=0, CONFIG_WCE=1)
791 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
792 * Guest writes 1 to the WCE configuration field (writeback mode)
793 * Guest sets DRIVER_OK bit in status field
794 *
4be74634 795 * s->blk would erroneously be placed in writethrough mode.
ef5bc962 796 */
95129d6f 797 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
4be74634
MA
798 aio_context_acquire(blk_get_aio_context(s->blk));
799 blk_set_enable_write_cache(s->blk,
95129d6f
CH
800 virtio_vdev_has_feature(vdev,
801 VIRTIO_BLK_F_WCE));
4be74634 802 aio_context_release(blk_get_aio_context(s->blk));
ef5bc962 803 }
9315cbfd
PB
804}
805
bbded32c 806static void virtio_blk_save(QEMUFile *f, void *opaque, size_t size)
6e02c38d 807{
b2b295a7 808 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
869a5c6d 809
1cc91b7d 810 virtio_save(vdev, f);
b2b295a7 811}
869a5c6d 812
b2b295a7
GK
813static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
814{
815 VirtIOBlock *s = VIRTIO_BLK(vdev);
816 VirtIOBlockReq *req = s->rq;
817
869a5c6d
AL
818 while (req) {
819 qemu_put_sbyte(f, 1);
30d8bf6d
SH
820
821 if (s->conf.num_queues > 1) {
822 qemu_put_be32(f, virtio_get_queue_index(req->vq));
823 }
824
ab281c17 825 qemu_put_virtqueue_element(f, &req->elem);
869a5c6d
AL
826 req = req->next;
827 }
828 qemu_put_sbyte(f, 0);
6e02c38d
AL
829}
830
bbded32c 831static int virtio_blk_load(QEMUFile *f, void *opaque, size_t size)
6e02c38d
AL
832{
833 VirtIOBlock *s = opaque;
1cc91b7d 834 VirtIODevice *vdev = VIRTIO_DEVICE(s);
6e02c38d 835
bbded32c 836 return virtio_load(vdev, f, 2);
b2b295a7
GK
837}
838
839static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
840 int version_id)
841{
842 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a633c46 843
869a5c6d 844 while (qemu_get_sbyte(f)) {
30d8bf6d
SH
845 unsigned nvqs = s->conf.num_queues;
846 unsigned vq_idx = 0;
ab281c17 847 VirtIOBlockReq *req;
30d8bf6d
SH
848
849 if (nvqs > 1) {
850 vq_idx = qemu_get_be32(f);
851
852 if (vq_idx >= nvqs) {
853 error_report("Invalid virtqueue index in request list: %#x",
854 vq_idx);
855 return -EINVAL;
856 }
857 }
858
ab281c17 859 req = qemu_get_virtqueue_element(f, sizeof(VirtIOBlockReq));
30d8bf6d 860 virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
869a5c6d 861 req->next = s->rq;
20a81e4d 862 s->rq = req;
869a5c6d 863 }
6e02c38d
AL
864
865 return 0;
866}
867
145feb17 868static void virtio_blk_resize(void *opaque)
e5051fc7 869{
1cc91b7d 870 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
e5051fc7 871
1cc91b7d 872 virtio_notify_config(vdev);
e5051fc7
CH
873}
874
0e49de52 875static const BlockDevOps virtio_block_ops = {
145feb17 876 .resize_cb = virtio_blk_resize,
0e49de52
MA
877};
878
75884afd 879static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1c028ddf 880{
75884afd 881 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179b417e 882 VirtIOBlock *s = VIRTIO_BLK(dev);
2a30307f 883 VirtIOBlkConf *conf = &s->conf;
3ffeeef7 884 Error *err = NULL;
2f270590 885 unsigned i;
cf21e106 886
4be74634 887 if (!conf->conf.blk) {
75884afd
AF
888 error_setg(errp, "drive property not set");
889 return;
d75d25e3 890 }
4be74634 891 if (!blk_is_inserted(conf->conf.blk)) {
75884afd
AF
892 error_setg(errp, "Device needs media, but drive is empty");
893 return;
98f28ad7 894 }
2f270590
SH
895 if (!conf->num_queues) {
896 error_setg(errp, "num-queues property must be larger than 0");
897 return;
898 }
d75d25e3 899
2a30307f 900 blkconf_serial(&conf->conf, &conf->serial);
f6166a06 901 blkconf_apply_backend_options(&conf->conf);
4be74634 902 s->original_wce = blk_enable_write_cache(conf->conf.blk);
2a30307f 903 blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, &err);
5ff5efb4
FZ
904 if (err) {
905 error_propagate(errp, err);
75884afd 906 return;
b7eb0c9f 907 }
0eb28a42 908 blkconf_blocksizes(&conf->conf);
a8686a9b 909
05ff6865
FK
910 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
911 sizeof(struct virtio_blk_config));
6e02c38d 912
4be74634 913 s->blk = conf->conf.blk;
869a5c6d 914 s->rq = NULL;
2a30307f 915 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 916
2f270590 917 for (i = 0; i < conf->num_queues; i++) {
0ff841f6 918 virtio_add_queue_aio(vdev, 128, virtio_blk_handle_output);
2f270590 919 }
2a30307f 920 virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
3ffeeef7 921 if (err != NULL) {
75884afd 922 error_propagate(errp, err);
6a1a8cc7 923 virtio_cleanup(vdev);
75884afd 924 return;
392808b4 925 }
6e02c38d 926
69b302b2 927 s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
4be74634
MA
928 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
929 blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
6e02c38d 930
4be74634 931 blk_iostatus_enable(s->blk);
1c028ddf
FK
932}
933
306ec6c3 934static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp)
1c028ddf 935{
306ec6c3
AF
936 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
937 VirtIOBlock *s = VIRTIO_BLK(dev);
938
1c028ddf
FK
939 virtio_blk_data_plane_destroy(s->dataplane);
940 s->dataplane = NULL;
1c028ddf 941 qemu_del_vm_change_state_handler(s->change);
4be74634 942 blockdev_mark_auto_del(s->blk);
6a1a8cc7 943 virtio_cleanup(vdev);
1c028ddf
FK
944}
945
467b3f33
SH
946static void virtio_blk_instance_init(Object *obj)
947{
948 VirtIOBlock *s = VIRTIO_BLK(obj);
949
950 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
2a30307f 951 (Object **)&s->conf.iothread,
467b3f33
SH
952 qdev_prop_allow_set_link_before_realize,
953 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
2a30307f 954 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
3342ec32
GA
955 "bootindex", "/disk@0,0",
956 DEVICE(obj), NULL);
467b3f33
SH
957}
958
bbded32c
DDAG
959VMSTATE_VIRTIO_DEVICE(blk, 2, virtio_blk_load, virtio_blk_save);
960
1c028ddf 961static Property virtio_blk_properties[] = {
2a30307f 962 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
8c398252 963 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
2a30307f
MA
964 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
965 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
966 DEFINE_PROP_BIT("config-wce", VirtIOBlock, conf.config_wce, 0, true),
32a877e4 967#ifdef __linux__
ed65fd1a 968 DEFINE_PROP_BIT("scsi", VirtIOBlock, conf.scsi, 0, false),
32a877e4 969#endif
c99495ac
PL
970 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
971 true),
2f270590 972 DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues, 1),
1c028ddf
FK
973 DEFINE_PROP_END_OF_LIST(),
974};
975
976static void virtio_blk_class_init(ObjectClass *klass, void *data)
977{
978 DeviceClass *dc = DEVICE_CLASS(klass);
979 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
75884afd 980
1c028ddf 981 dc->props = virtio_blk_properties;
bbded32c 982 dc->vmsd = &vmstate_virtio_blk;
125ee0ed 983 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
75884afd 984 vdc->realize = virtio_blk_device_realize;
306ec6c3 985 vdc->unrealize = virtio_blk_device_unrealize;
1c028ddf
FK
986 vdc->get_config = virtio_blk_update_config;
987 vdc->set_config = virtio_blk_set_config;
988 vdc->get_features = virtio_blk_get_features;
989 vdc->set_status = virtio_blk_set_status;
990 vdc->reset = virtio_blk_reset;
b2b295a7
GK
991 vdc->save = virtio_blk_save_device;
992 vdc->load = virtio_blk_load_device;
1c028ddf
FK
993}
994
995static const TypeInfo virtio_device_info = {
996 .name = TYPE_VIRTIO_BLK,
997 .parent = TYPE_VIRTIO_DEVICE,
998 .instance_size = sizeof(VirtIOBlock),
467b3f33 999 .instance_init = virtio_blk_instance_init,
1c028ddf
FK
1000 .class_init = virtio_blk_class_init,
1001};
1002
1003static void virtio_register_types(void)
1004{
1005 type_register_static(&virtio_device_info);
1006}
1007
1008type_init(virtio_register_types)
This page took 1.014177 seconds and 4 git commands to generate.