]> git.proxmox.com Git - mirror_qemu.git/blame - hw/block/virtio-blk.c
Merge tag 'pull-target-arm-20240126' of https://git.linaro.org/people/pmaydell/qemu...
[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"
433fcea4 15#include "qemu/defer-call.h"
da34e65c 16#include "qapi/error.h"
827805a2 17#include "qemu/iov.h"
0b8fa32f 18#include "qemu/module.h"
1de7afc9 19#include "qemu/error-report.h"
9b92fbcf 20#include "qemu/main-loop.h"
4f736650 21#include "block/block_int.h"
6d519a5f 22#include "trace.h"
0d09e41a 23#include "hw/block/block.h"
a27bd6c7 24#include "hw/qdev-properties.h"
9c17d615 25#include "sysemu/blockdev.h"
baf42268 26#include "sysemu/block-ram-registrar.h"
2f780b6a 27#include "sysemu/sysemu.h"
54d31236 28#include "sysemu/runstate.h"
0d09e41a 29#include "hw/virtio/virtio-blk.h"
08e2c9f1 30#include "scsi/constants.h"
1063b8b1
CH
31#ifdef __linux__
32# include <scsi/sg.h>
33#endif
0d09e41a 34#include "hw/virtio/virtio-bus.h"
ca77ee28 35#include "migration/qemu-file-types.h"
783d1897 36#include "hw/virtio/virtio-access.h"
d9cf55a8 37#include "hw/virtio/virtio-blk-common.h"
4c41c69e 38#include "qemu/coroutine.h"
6e02c38d 39
d14dde5e
GK
40static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
41 VirtIOBlockReq *req)
671ec3f0 42{
671ec3f0 43 req->dev = s;
edaffd9f 44 req->vq = vq;
869d66af 45 req->qiov.size = 0;
2a6cdd6d 46 req->in_len = 0;
869d66af 47 req->next = NULL;
95f7142a 48 req->mr_next = NULL;
671ec3f0
FZ
49}
50
d14dde5e 51static void virtio_blk_free_request(VirtIOBlockReq *req)
671ec3f0 52{
1d29b5b0 53 g_free(req);
671ec3f0
FZ
54}
55
03de2f52 56static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
869a5c6d
AL
57{
58 VirtIOBlock *s = req->dev;
1cc91b7d 59 VirtIODevice *vdev = VIRTIO_DEVICE(s);
869a5c6d 60
a576ceac 61 trace_virtio_blk_req_complete(vdev, req, status);
6d519a5f 62
92e3c2a3 63 stb_p(&req->in->status, status);
7bd04a04
SH
64 iov_discard_undo(&req->inhdr_undo);
65 iov_discard_undo(&req->outhdr_undo);
edaffd9f 66 virtqueue_push(req->vq, &req->elem, req->in_len);
3cdaf3dd 67 if (s->ioeventfd_started && !s->ioeventfd_disabled) {
3bcc17f0 68 virtio_notify_irqfd(vdev, req->vq);
03de2f52 69 } else {
edaffd9f 70 virtio_notify(vdev, req->vq);
03de2f52 71 }
bf4bd461
FZ
72}
73
f35d68f0 74static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
00f639fb 75 bool is_read, bool acct_failed)
869a5c6d 76{
869a5c6d 77 VirtIOBlock *s = req->dev;
9a6719d5 78 BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
869a5c6d 79
a589569f 80 if (action == BLOCK_ERROR_ACTION_STOP) {
466138dc
FZ
81 /* Break the link as the next request is going to be parsed from the
82 * ring again. Otherwise we may end up doing a double completion! */
83 req->mr_next = NULL;
9c67f33f
SH
84
85 WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
86 req->next = s->rq;
87 s->rq = req;
88 }
a589569f 89 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
869a5c6d 90 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
00f639fb
SG
91 if (acct_failed) {
92 block_acct_failed(blk_get_stats(s->blk), &req->acct);
93 }
671ec3f0 94 virtio_blk_free_request(req);
869a5c6d
AL
95 }
96
4be74634 97 blk_error_action(s->blk, action, is_read, error);
a589569f 98 return action != BLOCK_ERROR_ACTION_IGNORE;
869a5c6d
AL
99}
100
6e02c38d
AL
101static void virtio_blk_rw_complete(void *opaque, int ret)
102{
95f7142a 103 VirtIOBlockReq *next = opaque;
b9e413dd 104 VirtIOBlock *s = next->dev;
a576ceac 105 VirtIODevice *vdev = VIRTIO_DEVICE(s);
95f7142a
PL
106
107 while (next) {
108 VirtIOBlockReq *req = next;
109 next = req->mr_next;
a576ceac 110 trace_virtio_blk_rw_complete(vdev, req, ret);
95f7142a
PL
111
112 if (req->qiov.nalloc != -1) {
e61809ed 113 /* If nalloc is != -1 req->qiov is a local copy of the original
9bb192a4
YB
114 * external iovec. It was allocated in submit_requests to be
115 * able to merge requests. */
95f7142a
PL
116 qemu_iovec_destroy(&req->qiov);
117 }
6e02c38d 118
95f7142a 119 if (ret) {
bf4069fb 120 int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type);
95f7142a 121 bool is_read = !(p & VIRTIO_BLK_T_OUT);
2a6cdd6d
PB
122 /* Note that memory may be dirtied on read failure. If the
123 * virtio request is not completed here, as is the case for
124 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
125 * correctly during live migration. While this is ugly,
126 * it is acceptable because the device is free to write to
127 * the memory until the request is completed (which will
128 * happen on the other side of the migration).
129 */
00f639fb 130 if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) {
95f7142a
PL
131 continue;
132 }
133 }
6d519a5f 134
95f7142a 135 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
bf4069fb 136 block_acct_done(blk_get_stats(s->blk), &req->acct);
95f7142a 137 virtio_blk_free_request(req);
6e02c38d 138 }
869a5c6d 139}
6e02c38d 140
aa659be3
CH
141static void virtio_blk_flush_complete(void *opaque, int ret)
142{
143 VirtIOBlockReq *req = opaque;
b9e413dd 144 VirtIOBlock *s = req->dev;
aa659be3 145
c1135913
SH
146 if (ret && virtio_blk_handle_rw_error(req, -ret, 0, true)) {
147 return;
8c269b54
KW
148 }
149
150 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
9a6719d5 151 block_acct_done(blk_get_stats(s->blk), &req->acct);
671ec3f0 152 virtio_blk_free_request(req);
6e02c38d
AL
153}
154
37b06f8d
SG
155static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
156{
157 VirtIOBlockReq *req = opaque;
158 VirtIOBlock *s = req->dev;
159 bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) &
160 ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES;
161
c1135913
SH
162 if (ret && virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) {
163 return;
37b06f8d
SG
164 }
165
166 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
167 if (is_write_zeroes) {
168 block_acct_done(blk_get_stats(s->blk), &req->acct);
169 }
170 virtio_blk_free_request(req);
37b06f8d
SG
171}
172
1dc936aa
FZ
173#ifdef __linux__
174
175typedef struct {
176 VirtIOBlockReq *req;
177 struct sg_io_hdr hdr;
178} VirtIOBlockIoctlReq;
179
180static void virtio_blk_ioctl_complete(void *opaque, int status)
181{
182 VirtIOBlockIoctlReq *ioctl_req = opaque;
183 VirtIOBlockReq *req = ioctl_req->req;
9d456654
PB
184 VirtIOBlock *s = req->dev;
185 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1dc936aa
FZ
186 struct virtio_scsi_inhdr *scsi;
187 struct sg_io_hdr *hdr;
188
189 scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
190
191 if (status) {
192 status = VIRTIO_BLK_S_UNSUPP;
193 virtio_stl_p(vdev, &scsi->errors, 255);
194 goto out;
195 }
196
197 hdr = &ioctl_req->hdr;
198 /*
199 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
200 * clear the masked_status field [hence status gets cleared too, see
201 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
202 * status has occurred. However they do set DRIVER_SENSE in driver_status
203 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
204 */
205 if (hdr->status == 0 && hdr->sb_len_wr > 0) {
206 hdr->status = CHECK_CONDITION;
207 }
208
209 virtio_stl_p(vdev, &scsi->errors,
210 hdr->status | (hdr->msg_status << 8) |
211 (hdr->host_status << 16) | (hdr->driver_status << 24));
212 virtio_stl_p(vdev, &scsi->residual, hdr->resid);
213 virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
214 virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
215
216out:
217 virtio_blk_req_complete(req, status);
218 virtio_blk_free_request(req);
219 g_free(ioctl_req);
220}
221
222#endif
223
edaffd9f 224static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 225{
edaffd9f 226 VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
6e02c38d 227
51b19ebe 228 if (req) {
edaffd9f 229 virtio_blk_init_request(s, vq, req);
6e02c38d 230 }
6e02c38d
AL
231 return req;
232}
233
75344fa4 234static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
1063b8b1 235{
5a05cbee
FZ
236 int status = VIRTIO_BLK_S_OK;
237 struct virtio_scsi_inhdr *scsi = NULL;
75344fa4 238 VirtIOBlock *blk = req->dev;
bf4069fb
AR
239 VirtIODevice *vdev = VIRTIO_DEVICE(blk);
240 VirtQueueElement *elem = &req->elem;
783d1897 241
47ce9ef7 242#ifdef __linux__
1063b8b1 243 int i;
1dc936aa 244 VirtIOBlockIoctlReq *ioctl_req;
a209f461 245 BlockAIOCB *acb;
47ce9ef7 246#endif
1063b8b1
CH
247
248 /*
249 * We require at least one output segment each for the virtio_blk_outhdr
250 * and the SCSI command block.
251 *
252 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
253 * and the sense buffer pointer in the input segments.
254 */
5a05cbee
FZ
255 if (elem->out_num < 2 || elem->in_num < 3) {
256 status = VIRTIO_BLK_S_IOERR;
257 goto fail;
1063b8b1
CH
258 }
259
260 /*
f34e73cd
PB
261 * The scsi inhdr is placed in the second-to-last input segment, just
262 * before the regular inhdr.
1063b8b1 263 */
5a05cbee 264 scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
f34e73cd 265
bbe8bd4d 266 if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) {
f34e73cd
PB
267 status = VIRTIO_BLK_S_UNSUPP;
268 goto fail;
1063b8b1
CH
269 }
270
271 /*
f34e73cd 272 * No support for bidirection commands yet.
1063b8b1 273 */
5a05cbee 274 if (elem->out_num > 2 && elem->in_num > 3) {
f34e73cd
PB
275 status = VIRTIO_BLK_S_UNSUPP;
276 goto fail;
277 }
1063b8b1 278
f34e73cd 279#ifdef __linux__
1dc936aa
FZ
280 ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
281 ioctl_req->req = req;
282 ioctl_req->hdr.interface_id = 'S';
283 ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
284 ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
285 ioctl_req->hdr.dxfer_len = 0;
1063b8b1 286
5a05cbee 287 if (elem->out_num > 2) {
1063b8b1
CH
288 /*
289 * If there are more than the minimally required 2 output segments
290 * there is write payload starting from the third iovec.
291 */
1dc936aa
FZ
292 ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
293 ioctl_req->hdr.iovec_count = elem->out_num - 2;
1063b8b1 294
1dc936aa
FZ
295 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
296 ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
297 }
1063b8b1 298
1dc936aa 299 ioctl_req->hdr.dxferp = elem->out_sg + 2;
1063b8b1 300
5a05cbee 301 } else if (elem->in_num > 3) {
1063b8b1
CH
302 /*
303 * If we have more than 3 input segments the guest wants to actually
304 * read data.
305 */
1dc936aa
FZ
306 ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
307 ioctl_req->hdr.iovec_count = elem->in_num - 3;
308 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
309 ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
310 }
1063b8b1 311
1dc936aa 312 ioctl_req->hdr.dxferp = elem->in_sg;
1063b8b1
CH
313 } else {
314 /*
315 * Some SCSI commands don't actually transfer any data.
316 */
1dc936aa 317 ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
1063b8b1
CH
318 }
319
1dc936aa
FZ
320 ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
321 ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
1063b8b1 322
a209f461
FZ
323 acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
324 virtio_blk_ioctl_complete, ioctl_req);
325 if (!acb) {
326 g_free(ioctl_req);
327 status = VIRTIO_BLK_S_UNSUPP;
328 goto fail;
329 }
1dc936aa 330 return -EINPROGRESS;
1063b8b1 331#else
f34e73cd
PB
332 abort();
333#endif
334
335fail:
336 /* Just put anything nonzero so that the ioctl fails in the guest. */
5a05cbee 337 if (scsi) {
783d1897 338 virtio_stl_p(vdev, &scsi->errors, 255);
5a05cbee
FZ
339 }
340 return status;
341}
342
343static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
344{
345 int status;
346
75344fa4 347 status = virtio_blk_handle_scsi_req(req);
1dc936aa
FZ
348 if (status != -EINPROGRESS) {
349 virtio_blk_req_complete(req, status);
350 virtio_blk_free_request(req);
351 }
1063b8b1 352}
1063b8b1 353
baf42268 354static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
95f7142a 355 int start, int num_reqs, int niov)
869a5c6d 356{
baf42268 357 BlockBackend *blk = s->blk;
95f7142a
PL
358 QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
359 int64_t sector_num = mrb->reqs[start]->sector_num;
95f7142a 360 bool is_write = mrb->is_write;
baf42268 361 BdrvRequestFlags flags = 0;
95f7142a
PL
362
363 if (num_reqs > 1) {
364 int i;
365 struct iovec *tmp_iov = qiov->iov;
366 int tmp_niov = qiov->niov;
367
368 /* mrb->reqs[start]->qiov was initialized from external so we can't
b5772fdd 369 * modify it here. We need to initialize it locally and then add the
95f7142a
PL
370 * external iovecs. */
371 qemu_iovec_init(qiov, niov);
372
373 for (i = 0; i < tmp_niov; i++) {
374 qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
375 }
376
377 for (i = start + 1; i < start + num_reqs; i++) {
378 qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
379 mrb->reqs[i]->qiov.size);
380 mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
95f7142a 381 }
95f7142a 382
a576ceac
SH
383 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev),
384 mrb, start, num_reqs,
b5772fdd
EB
385 sector_num << BDRV_SECTOR_BITS,
386 qiov->size, is_write);
95f7142a
PL
387 block_acct_merge_done(blk_get_stats(blk),
388 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
389 num_reqs - 1);
390 }
91553dcc 391
baf42268
SH
392 if (blk_ram_registrar_ok(&s->blk_ram_registrar)) {
393 flags |= BDRV_REQ_REGISTERED_BUF;
394 }
395
95f7142a 396 if (is_write) {
baf42268
SH
397 blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov,
398 flags, virtio_blk_rw_complete,
399 mrb->reqs[start]);
95f7142a 400 } else {
baf42268
SH
401 blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov,
402 flags, virtio_blk_rw_complete,
403 mrb->reqs[start]);
95f7142a
PL
404 }
405}
406
407static int multireq_compare(const void *a, const void *b)
408{
409 const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
410 *req2 = *(VirtIOBlockReq **)b;
411
412 /*
413 * Note that we can't simply subtract sector_num1 from sector_num2
414 * here as that could overflow the return value.
415 */
416 if (req1->sector_num > req2->sector_num) {
417 return 1;
418 } else if (req1->sector_num < req2->sector_num) {
419 return -1;
420 } else {
421 return 0;
422 }
423}
424
baf42268 425static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb)
95f7142a
PL
426{
427 int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
5def6b80 428 uint32_t max_transfer;
95f7142a
PL
429 int64_t sector_num = 0;
430
431 if (mrb->num_reqs == 1) {
baf42268 432 submit_requests(s, mrb, 0, 1, -1);
95f7142a 433 mrb->num_reqs = 0;
c20fd872
CH
434 return;
435 }
436
5def6b80 437 max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
95f7142a
PL
438
439 qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
440 &multireq_compare);
441
442 for (i = 0; i < mrb->num_reqs; i++) {
443 VirtIOBlockReq *req = mrb->reqs[i];
444 if (num_reqs > 0) {
49cffbc6
GA
445 /*
446 * NOTE: We cannot merge the requests in below situations:
447 * 1. requests are not sequential
448 * 2. merge would exceed maximum number of IOVs
449 * 3. merge would exceed maximum transfer length of backend device
450 */
451 if (sector_num + nb_sectors != req->sector_num ||
baf42268 452 niov > blk_get_max_iov(s->blk) - req->qiov.niov ||
5def6b80
EB
453 req->qiov.size > max_transfer ||
454 nb_sectors > (max_transfer -
455 req->qiov.size) / BDRV_SECTOR_SIZE) {
baf42268 456 submit_requests(s, mrb, start, num_reqs, niov);
95f7142a 457 num_reqs = 0;
91553dcc
KW
458 }
459 }
95f7142a
PL
460
461 if (num_reqs == 0) {
462 sector_num = req->sector_num;
463 nb_sectors = niov = 0;
464 start = i;
465 }
466
467 nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
468 niov += req->qiov.niov;
469 num_reqs++;
91553dcc 470 }
c20fd872 471
baf42268 472 submit_requests(s, mrb, start, num_reqs, niov);
95f7142a 473 mrb->num_reqs = 0;
91553dcc 474}
87b245db 475
c20fd872 476static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
aa659be3 477{
bf4069fb
AR
478 VirtIOBlock *s = req->dev;
479
480 block_acct_start(blk_get_stats(s->blk), &req->acct, 0,
5366d0c8 481 BLOCK_ACCT_FLUSH);
a597e79c 482
618fbb84
CH
483 /*
484 * Make sure all outstanding writes are posted to the backing device.
485 */
95f7142a 486 if (mrb->is_write && mrb->num_reqs > 0) {
baf42268 487 virtio_blk_submit_multireq(s, mrb);
95f7142a 488 }
bf4069fb 489 blk_aio_flush(s->blk, virtio_blk_flush_complete, req);
aa659be3
CH
490}
491
d0e14376
MA
492static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
493 uint64_t sector, size_t size)
494{
3c2daac0
MA
495 uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
496 uint64_t total_sectors;
497
75af1f34 498 if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
95f7142a
PL
499 return false;
500 }
d0e14376
MA
501 if (sector & dev->sector_mask) {
502 return false;
503 }
2a30307f 504 if (size % dev->conf.conf.logical_block_size) {
d0e14376
MA
505 return false;
506 }
4be74634 507 blk_get_geometry(dev->blk, &total_sectors);
3c2daac0
MA
508 if (sector > total_sectors || nb_sectors > total_sectors - sector) {
509 return false;
510 }
d0e14376
MA
511 return true;
512}
513
37b06f8d
SG
514static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req,
515 struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes)
516{
517 VirtIOBlock *s = req->dev;
518 VirtIODevice *vdev = VIRTIO_DEVICE(s);
519 uint64_t sector;
520 uint32_t num_sectors, flags, max_sectors;
521 uint8_t err_status;
522 int bytes;
523
524 sector = virtio_ldq_p(vdev, &dwz_hdr->sector);
525 num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors);
526 flags = virtio_ldl_p(vdev, &dwz_hdr->flags);
527 max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors :
528 s->conf.max_discard_sectors;
529
530 /*
531 * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
532 * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
533 * the integer variable.
534 */
535 if (unlikely(num_sectors > max_sectors)) {
536 err_status = VIRTIO_BLK_S_IOERR;
537 goto err;
538 }
539
540 bytes = num_sectors << BDRV_SECTOR_BITS;
541
542 if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) {
543 err_status = VIRTIO_BLK_S_IOERR;
544 goto err;
545 }
546
547 /*
548 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
549 * and write zeroes commands if any unknown flag is set.
550 */
551 if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
552 err_status = VIRTIO_BLK_S_UNSUPP;
553 goto err;
554 }
555
556 if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */
557 int blk_aio_flags = 0;
558
559 if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
560 blk_aio_flags |= BDRV_REQ_MAY_UNMAP;
561 }
562
563 block_acct_start(blk_get_stats(s->blk), &req->acct, bytes,
564 BLOCK_ACCT_WRITE);
565
566 blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS,
567 bytes, blk_aio_flags,
568 virtio_blk_discard_write_zeroes_complete, req);
569 } else { /* VIRTIO_BLK_T_DISCARD */
570 /*
571 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
572 * discard commands if the unmap flag is set.
573 */
574 if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
575 err_status = VIRTIO_BLK_S_UNSUPP;
576 goto err;
577 }
578
579 blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes,
580 virtio_blk_discard_write_zeroes_complete, req);
581 }
582
583 return VIRTIO_BLK_S_OK;
584
585err:
586 if (is_write_zeroes) {
587 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
588 }
589 return err_status;
590}
591
4f736650
SL
592typedef struct ZoneCmdData {
593 VirtIOBlockReq *req;
594 struct iovec *in_iov;
595 unsigned in_num;
596 union {
597 struct {
598 unsigned int nr_zones;
599 BlockZoneDescriptor *zones;
600 } zone_report_data;
601 struct {
602 int64_t offset;
603 } zone_append_data;
604 };
605} ZoneCmdData;
606
607/*
608 * check zoned_request: error checking before issuing requests. If all checks
609 * passed, return true.
610 * append: true if only zone append requests issued.
611 */
612static bool check_zoned_request(VirtIOBlock *s, int64_t offset, int64_t len,
613 bool append, uint8_t *status) {
614 BlockDriverState *bs = blk_bs(s->blk);
615 int index;
616
617 if (!virtio_has_feature(s->host_features, VIRTIO_BLK_F_ZONED)) {
618 *status = VIRTIO_BLK_S_UNSUPP;
619 return false;
620 }
621
622 if (offset < 0 || len < 0 || len > (bs->total_sectors << BDRV_SECTOR_BITS)
623 || offset > (bs->total_sectors << BDRV_SECTOR_BITS) - len) {
624 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
625 return false;
626 }
627
628 if (append) {
629 if (bs->bl.write_granularity) {
630 if ((offset % bs->bl.write_granularity) != 0) {
631 *status = VIRTIO_BLK_S_ZONE_UNALIGNED_WP;
632 return false;
633 }
634 }
635
636 index = offset / bs->bl.zone_size;
637 if (BDRV_ZT_IS_CONV(bs->wps->wp[index])) {
638 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
639 return false;
640 }
641
642 if (len / 512 > bs->bl.max_append_sectors) {
643 if (bs->bl.max_append_sectors == 0) {
644 *status = VIRTIO_BLK_S_UNSUPP;
645 } else {
646 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
647 }
648 return false;
649 }
650 }
651 return true;
652}
653
654static void virtio_blk_zone_report_complete(void *opaque, int ret)
655{
656 ZoneCmdData *data = opaque;
657 VirtIOBlockReq *req = data->req;
4f736650
SL
658 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
659 struct iovec *in_iov = data->in_iov;
660 unsigned in_num = data->in_num;
661 int64_t zrp_size, n, j = 0;
662 int64_t nz = data->zone_report_data.nr_zones;
663 int8_t err_status = VIRTIO_BLK_S_OK;
664
4e92acf7 665 trace_virtio_blk_zone_report_complete(vdev, req, nz, ret);
4f736650
SL
666 if (ret) {
667 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
668 goto out;
669 }
670
671 struct virtio_blk_zone_report zrp_hdr = (struct virtio_blk_zone_report) {
672 .nr_zones = cpu_to_le64(nz),
673 };
674 zrp_size = sizeof(struct virtio_blk_zone_report)
675 + sizeof(struct virtio_blk_zone_descriptor) * nz;
676 n = iov_from_buf(in_iov, in_num, 0, &zrp_hdr, sizeof(zrp_hdr));
677 if (n != sizeof(zrp_hdr)) {
678 virtio_error(vdev, "Driver provided input buffer that is too small!");
679 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
680 goto out;
681 }
682
683 for (size_t i = sizeof(zrp_hdr); i < zrp_size;
684 i += sizeof(struct virtio_blk_zone_descriptor), ++j) {
685 struct virtio_blk_zone_descriptor desc =
686 (struct virtio_blk_zone_descriptor) {
687 .z_start = cpu_to_le64(data->zone_report_data.zones[j].start
688 >> BDRV_SECTOR_BITS),
689 .z_cap = cpu_to_le64(data->zone_report_data.zones[j].cap
690 >> BDRV_SECTOR_BITS),
691 .z_wp = cpu_to_le64(data->zone_report_data.zones[j].wp
692 >> BDRV_SECTOR_BITS),
693 };
694
695 switch (data->zone_report_data.zones[j].type) {
696 case BLK_ZT_CONV:
697 desc.z_type = VIRTIO_BLK_ZT_CONV;
698 break;
699 case BLK_ZT_SWR:
700 desc.z_type = VIRTIO_BLK_ZT_SWR;
701 break;
702 case BLK_ZT_SWP:
703 desc.z_type = VIRTIO_BLK_ZT_SWP;
704 break;
705 default:
706 g_assert_not_reached();
707 }
708
709 switch (data->zone_report_data.zones[j].state) {
710 case BLK_ZS_RDONLY:
711 desc.z_state = VIRTIO_BLK_ZS_RDONLY;
712 break;
713 case BLK_ZS_OFFLINE:
714 desc.z_state = VIRTIO_BLK_ZS_OFFLINE;
715 break;
716 case BLK_ZS_EMPTY:
717 desc.z_state = VIRTIO_BLK_ZS_EMPTY;
718 break;
719 case BLK_ZS_CLOSED:
720 desc.z_state = VIRTIO_BLK_ZS_CLOSED;
721 break;
722 case BLK_ZS_FULL:
723 desc.z_state = VIRTIO_BLK_ZS_FULL;
724 break;
725 case BLK_ZS_EOPEN:
726 desc.z_state = VIRTIO_BLK_ZS_EOPEN;
727 break;
728 case BLK_ZS_IOPEN:
729 desc.z_state = VIRTIO_BLK_ZS_IOPEN;
730 break;
731 case BLK_ZS_NOT_WP:
732 desc.z_state = VIRTIO_BLK_ZS_NOT_WP;
733 break;
734 default:
735 g_assert_not_reached();
736 }
737
738 /* TODO: it takes O(n^2) time complexity. Optimizations required. */
739 n = iov_from_buf(in_iov, in_num, i, &desc, sizeof(desc));
740 if (n != sizeof(desc)) {
741 virtio_error(vdev, "Driver provided input buffer "
742 "for descriptors that is too small!");
743 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
744 }
745 }
746
747out:
4f736650
SL
748 virtio_blk_req_complete(req, err_status);
749 virtio_blk_free_request(req);
4f736650
SL
750 g_free(data->zone_report_data.zones);
751 g_free(data);
752}
753
754static void virtio_blk_handle_zone_report(VirtIOBlockReq *req,
755 struct iovec *in_iov,
756 unsigned in_num)
757{
758 VirtIOBlock *s = req->dev;
759 VirtIODevice *vdev = VIRTIO_DEVICE(s);
760 unsigned int nr_zones;
761 ZoneCmdData *data;
762 int64_t zone_size, offset;
763 uint8_t err_status;
764
765 if (req->in_len < sizeof(struct virtio_blk_inhdr) +
766 sizeof(struct virtio_blk_zone_report) +
767 sizeof(struct virtio_blk_zone_descriptor)) {
768 virtio_error(vdev, "in buffer too small for zone report");
769 return;
770 }
771
772 /* start byte offset of the zone report */
773 offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
774 if (!check_zoned_request(s, offset, 0, false, &err_status)) {
775 goto out;
776 }
777 nr_zones = (req->in_len - sizeof(struct virtio_blk_inhdr) -
778 sizeof(struct virtio_blk_zone_report)) /
779 sizeof(struct virtio_blk_zone_descriptor);
4e92acf7
SL
780 trace_virtio_blk_handle_zone_report(vdev, req,
781 offset >> BDRV_SECTOR_BITS, nr_zones);
4f736650
SL
782
783 zone_size = sizeof(BlockZoneDescriptor) * nr_zones;
784 data = g_malloc(sizeof(ZoneCmdData));
785 data->req = req;
786 data->in_iov = in_iov;
787 data->in_num = in_num;
788 data->zone_report_data.nr_zones = nr_zones;
789 data->zone_report_data.zones = g_malloc(zone_size),
790
791 blk_aio_zone_report(s->blk, offset, &data->zone_report_data.nr_zones,
792 data->zone_report_data.zones,
793 virtio_blk_zone_report_complete, data);
794 return;
795out:
796 virtio_blk_req_complete(req, err_status);
797 virtio_blk_free_request(req);
798}
799
800static void virtio_blk_zone_mgmt_complete(void *opaque, int ret)
801{
802 VirtIOBlockReq *req = opaque;
803 VirtIOBlock *s = req->dev;
4e92acf7 804 VirtIODevice *vdev = VIRTIO_DEVICE(s);
4f736650 805 int8_t err_status = VIRTIO_BLK_S_OK;
4e92acf7 806 trace_virtio_blk_zone_mgmt_complete(vdev, req,ret);
4f736650
SL
807
808 if (ret) {
809 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
810 }
811
4f736650
SL
812 virtio_blk_req_complete(req, err_status);
813 virtio_blk_free_request(req);
4f736650
SL
814}
815
816static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq *req, BlockZoneOp op)
817{
818 VirtIOBlock *s = req->dev;
819 VirtIODevice *vdev = VIRTIO_DEVICE(s);
820 BlockDriverState *bs = blk_bs(s->blk);
821 int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
822 uint64_t len;
823 uint64_t capacity = bs->total_sectors << BDRV_SECTOR_BITS;
824 uint8_t err_status = VIRTIO_BLK_S_OK;
825
826 uint32_t type = virtio_ldl_p(vdev, &req->out.type);
827 if (type == VIRTIO_BLK_T_ZONE_RESET_ALL) {
828 /* Entire drive capacity */
829 offset = 0;
830 len = capacity;
4e92acf7
SL
831 trace_virtio_blk_handle_zone_reset_all(vdev, req, 0,
832 bs->total_sectors);
4f736650
SL
833 } else {
834 if (bs->bl.zone_size > capacity - offset) {
835 /* The zoned device allows the last smaller zone. */
836 len = capacity - bs->bl.zone_size * (bs->bl.nr_zones - 1);
837 } else {
838 len = bs->bl.zone_size;
839 }
4e92acf7
SL
840 trace_virtio_blk_handle_zone_mgmt(vdev, req, op,
841 offset >> BDRV_SECTOR_BITS,
842 len >> BDRV_SECTOR_BITS);
4f736650
SL
843 }
844
845 if (!check_zoned_request(s, offset, len, false, &err_status)) {
846 goto out;
847 }
848
849 blk_aio_zone_mgmt(s->blk, op, offset, len,
850 virtio_blk_zone_mgmt_complete, req);
851
852 return 0;
853out:
854 virtio_blk_req_complete(req, err_status);
855 virtio_blk_free_request(req);
856 return err_status;
857}
858
859static void virtio_blk_zone_append_complete(void *opaque, int ret)
860{
861 ZoneCmdData *data = opaque;
862 VirtIOBlockReq *req = data->req;
4f736650
SL
863 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
864 int64_t append_sector, n;
865 uint8_t err_status = VIRTIO_BLK_S_OK;
866
867 if (ret) {
868 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
869 goto out;
870 }
871
872 virtio_stq_p(vdev, &append_sector,
873 data->zone_append_data.offset >> BDRV_SECTOR_BITS);
874 n = iov_from_buf(data->in_iov, data->in_num, 0, &append_sector,
875 sizeof(append_sector));
876 if (n != sizeof(append_sector)) {
877 virtio_error(vdev, "Driver provided input buffer less than size of "
878 "append_sector");
879 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
880 goto out;
881 }
4e92acf7 882 trace_virtio_blk_zone_append_complete(vdev, req, append_sector, ret);
4f736650
SL
883
884out:
4f736650
SL
885 virtio_blk_req_complete(req, err_status);
886 virtio_blk_free_request(req);
4f736650
SL
887 g_free(data);
888}
889
890static int virtio_blk_handle_zone_append(VirtIOBlockReq *req,
891 struct iovec *out_iov,
892 struct iovec *in_iov,
893 uint64_t out_num,
894 unsigned in_num) {
895 VirtIOBlock *s = req->dev;
896 VirtIODevice *vdev = VIRTIO_DEVICE(s);
897 uint8_t err_status = VIRTIO_BLK_S_OK;
898
899 int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
900 int64_t len = iov_size(out_iov, out_num);
901
4e92acf7 902 trace_virtio_blk_handle_zone_append(vdev, req, offset >> BDRV_SECTOR_BITS);
4f736650
SL
903 if (!check_zoned_request(s, offset, len, true, &err_status)) {
904 goto out;
905 }
906
907 ZoneCmdData *data = g_malloc(sizeof(ZoneCmdData));
908 data->req = req;
909 data->in_iov = in_iov;
910 data->in_num = in_num;
911 data->zone_append_data.offset = offset;
912 qemu_iovec_init_external(&req->qiov, out_iov, out_num);
52eb76f4
SL
913
914 block_acct_start(blk_get_stats(s->blk), &req->acct, len,
915 BLOCK_ACCT_ZONE_APPEND);
916
4f736650
SL
917 blk_aio_zone_append(s->blk, &data->zone_append_data.offset, &req->qiov, 0,
918 virtio_blk_zone_append_complete, data);
919 return 0;
920
921out:
4f736650
SL
922 virtio_blk_req_complete(req, err_status);
923 virtio_blk_free_request(req);
4f736650
SL
924 return err_status;
925}
926
20ea686a 927static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
bc6694d4 928{
92e3c2a3 929 uint32_t type;
f897bf75 930 struct iovec *in_iov = req->elem.in_sg;
5636da76 931 struct iovec *out_iov = req->elem.out_sg;
f897bf75
SH
932 unsigned in_num = req->elem.in_num;
933 unsigned out_num = req->elem.out_num;
20ea686a
GK
934 VirtIOBlock *s = req->dev;
935 VirtIODevice *vdev = VIRTIO_DEVICE(s);
92e3c2a3 936
f897bf75 937 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
20ea686a
GK
938 virtio_error(vdev, "virtio-blk missing headers");
939 return -1;
bc6694d4
KW
940 }
941
5636da76 942 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
827805a2 943 sizeof(req->out)) != sizeof(req->out))) {
20ea686a
GK
944 virtio_error(vdev, "virtio-blk request outhdr too short");
945 return -1;
827805a2 946 }
ee17e848 947
7bd04a04
SH
948 iov_discard_front_undoable(&out_iov, &out_num, sizeof(req->out),
949 &req->outhdr_undo);
ee17e848 950
12048545 951 if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
20ea686a 952 virtio_error(vdev, "virtio-blk request inhdr too short");
7bd04a04 953 iov_discard_undo(&req->outhdr_undo);
20ea686a 954 return -1;
ee17e848
FZ
955 }
956
2a6cdd6d
PB
957 /* We always touch the last byte, so just see how big in_iov is. */
958 req->in_len = iov_size(in_iov, in_num);
ee17e848
FZ
959 req->in = (void *)in_iov[in_num - 1].iov_base
960 + in_iov[in_num - 1].iov_len
961 - sizeof(struct virtio_blk_inhdr);
7bd04a04
SH
962 iov_discard_back_undoable(in_iov, &in_num, sizeof(struct virtio_blk_inhdr),
963 &req->inhdr_undo);
bc6694d4 964
9a6719d5 965 type = virtio_ldl_p(vdev, &req->out.type);
92e3c2a3 966
95f7142a 967 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
631b22ea 968 * is an optional flag. Although a guest should not send this flag if
95f7142a
PL
969 * not negotiated we ignored it in the past. So keep ignoring it. */
970 switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
971 case VIRTIO_BLK_T_IN:
972 {
973 bool is_write = type & VIRTIO_BLK_T_OUT;
9a6719d5 974 req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
95f7142a
PL
975
976 if (is_write) {
5636da76 977 qemu_iovec_init_external(&req->qiov, out_iov, out_num);
a576ceac 978 trace_virtio_blk_handle_write(vdev, req, req->sector_num,
95f7142a
PL
979 req->qiov.size / BDRV_SECTOR_SIZE);
980 } else {
981 qemu_iovec_init_external(&req->qiov, in_iov, in_num);
a576ceac 982 trace_virtio_blk_handle_read(vdev, req, req->sector_num,
95f7142a
PL
983 req->qiov.size / BDRV_SECTOR_SIZE);
984 }
985
9a6719d5 986 if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) {
95f7142a 987 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
9a6719d5 988 block_acct_invalid(blk_get_stats(s->blk),
01762e03 989 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
95f7142a 990 virtio_blk_free_request(req);
20ea686a 991 return 0;
95f7142a
PL
992 }
993
9a6719d5 994 block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size,
95f7142a
PL
995 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
996
997 /* merge would exceed maximum number of requests or IO direction
998 * changes */
999 if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
c99495ac 1000 is_write != mrb->is_write ||
9a6719d5 1001 !s->conf.request_merging)) {
baf42268 1002 virtio_blk_submit_multireq(s, mrb);
95f7142a
PL
1003 }
1004
1005 assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
1006 mrb->reqs[mrb->num_reqs++] = req;
1007 mrb->is_write = is_write;
1008 break;
1009 }
1010 case VIRTIO_BLK_T_FLUSH:
c20fd872 1011 virtio_blk_handle_flush(req, mrb);
95f7142a 1012 break;
4f736650
SL
1013 case VIRTIO_BLK_T_ZONE_REPORT:
1014 virtio_blk_handle_zone_report(req, in_iov, in_num);
1015 break;
1016 case VIRTIO_BLK_T_ZONE_OPEN:
1017 virtio_blk_handle_zone_mgmt(req, BLK_ZO_OPEN);
1018 break;
1019 case VIRTIO_BLK_T_ZONE_CLOSE:
1020 virtio_blk_handle_zone_mgmt(req, BLK_ZO_CLOSE);
1021 break;
1022 case VIRTIO_BLK_T_ZONE_FINISH:
1023 virtio_blk_handle_zone_mgmt(req, BLK_ZO_FINISH);
1024 break;
1025 case VIRTIO_BLK_T_ZONE_RESET:
1026 virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1027 break;
1028 case VIRTIO_BLK_T_ZONE_RESET_ALL:
1029 virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1030 break;
95f7142a 1031 case VIRTIO_BLK_T_SCSI_CMD:
bc6694d4 1032 virtio_blk_handle_scsi(req);
95f7142a
PL
1033 break;
1034 case VIRTIO_BLK_T_GET_ID:
1035 {
a8686a9b
MA
1036 /*
1037 * NB: per existing s/n string convention the string is
1038 * terminated by '\0' only when shorter than buffer.
1039 */
2a30307f 1040 const char *serial = s->conf.serial ? s->conf.serial : "";
a83ceea8
MM
1041 size_t size = MIN(strlen(serial) + 1,
1042 MIN(iov_size(in_iov, in_num),
1043 VIRTIO_BLK_ID_BYTES));
1044 iov_from_buf(in_iov, in_num, 0, serial, size);
2930b313 1045 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
671ec3f0 1046 virtio_blk_free_request(req);
95f7142a
PL
1047 break;
1048 }
4f736650
SL
1049 case VIRTIO_BLK_T_ZONE_APPEND & ~VIRTIO_BLK_T_OUT:
1050 /*
1051 * Passing out_iov/out_num and in_iov/in_num is not safe
1052 * to access req->elem.out_sg directly because it may be
1053 * modified by virtio_blk_handle_request().
1054 */
1055 virtio_blk_handle_zone_append(req, out_iov, in_iov, out_num, in_num);
1056 break;
37b06f8d
SG
1057 /*
1058 * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
1059 * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
1060 * so we must mask it for these requests, then we will check if it is set.
1061 */
1062 case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT:
1063 case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT:
1064 {
1065 struct virtio_blk_discard_write_zeroes dwz_hdr;
1066 size_t out_len = iov_size(out_iov, out_num);
1067 bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) ==
1068 VIRTIO_BLK_T_WRITE_ZEROES;
1069 uint8_t err_status;
1070
1071 /*
1072 * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
1073 * more than one segment.
1074 */
1075 if (unlikely(!(type & VIRTIO_BLK_T_OUT) ||
1076 out_len > sizeof(dwz_hdr))) {
1077 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
1078 virtio_blk_free_request(req);
1079 return 0;
1080 }
1081
1082 if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr,
1083 sizeof(dwz_hdr)) != sizeof(dwz_hdr))) {
7bd04a04
SH
1084 iov_discard_undo(&req->inhdr_undo);
1085 iov_discard_undo(&req->outhdr_undo);
37b06f8d
SG
1086 virtio_error(vdev, "virtio-blk discard/write_zeroes header"
1087 " too short");
1088 return -1;
1089 }
1090
1091 err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr,
1092 is_write_zeroes);
1093 if (err_status != VIRTIO_BLK_S_OK) {
1094 virtio_blk_req_complete(req, err_status);
1095 virtio_blk_free_request(req);
1096 }
1097
1098 break;
1099 }
95f7142a 1100 default:
9e72c450 1101 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
671ec3f0 1102 virtio_blk_free_request(req);
bc6694d4 1103 }
20ea686a 1104 return 0;
bc6694d4
KW
1105}
1106
186b9691 1107void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
6e02c38d 1108{
6e02c38d 1109 VirtIOBlockReq *req;
95f7142a 1110 MultiReqBuffer mrb = {};
d0435bc5 1111 bool suppress_notifications = virtio_queue_get_notification(vq);
6e02c38d 1112
ccee48aa 1113 defer_call_begin();
fc73548e 1114
9ef9d402 1115 do {
d0435bc5
SH
1116 if (suppress_notifications) {
1117 virtio_queue_set_notification(vq, 0);
1118 }
9ef9d402
SH
1119
1120 while ((req = virtio_blk_get_request(s, vq))) {
1121 if (virtio_blk_handle_request(req, &mrb)) {
1122 virtqueue_detach_element(req->vq, &req->elem, 0);
1123 virtio_blk_free_request(req);
1124 break;
1125 }
20ea686a 1126 }
9ef9d402 1127
d0435bc5
SH
1128 if (suppress_notifications) {
1129 virtio_queue_set_notification(vq, 1);
1130 }
9ef9d402 1131 } while (!virtio_queue_empty(vq));
91553dcc 1132
95f7142a 1133 if (mrb.num_reqs) {
baf42268 1134 virtio_blk_submit_multireq(s, &mrb);
95f7142a 1135 }
fc73548e 1136
ccee48aa 1137 defer_call_end();
6e02c38d
AL
1138}
1139
8a2fad57
MT
1140static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
1141{
1142 VirtIOBlock *s = (VirtIOBlock *)vdev;
1143
3cdaf3dd 1144 if (!s->ioeventfd_disabled && !s->ioeventfd_started) {
8a2fad57 1145 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
3cdaf3dd 1146 * ioeventfd here instead of waiting for .set_status().
8a2fad57 1147 */
9ffe337c 1148 virtio_device_start_ioeventfd(vdev);
3cdaf3dd 1149 if (!s->ioeventfd_disabled) {
8a2fad57
MT
1150 return;
1151 }
1152 }
b6948ab0 1153
186b9691 1154 virtio_blk_handle_vq(s, vq);
8a2fad57
MT
1155}
1156
a937f8e8 1157static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d 1158{
71ee0cdd
SH
1159 VirtIOBlockReq *req = opaque;
1160 VirtIOBlock *s = req->dev; /* we're called with at least one request */
a937f8e8 1161
95f7142a 1162 MultiReqBuffer mrb = {};
869a5c6d 1163
869a5c6d 1164 while (req) {
1bdb176a 1165 VirtIOBlockReq *next = req->next;
20ea686a
GK
1166 if (virtio_blk_handle_request(req, &mrb)) {
1167 /* Device is now broken and won't do any processing until it gets
1168 * reset. Already queued requests will be lost: let's purge them.
1169 */
1170 while (req) {
1171 next = req->next;
1172 virtqueue_detach_element(req->vq, &req->elem, 0);
1173 virtio_blk_free_request(req);
1174 req = next;
1175 }
1176 break;
1177 }
1bdb176a 1178 req = next;
869a5c6d 1179 }
f1b52868 1180
95f7142a 1181 if (mrb.num_reqs) {
baf42268 1182 virtio_blk_submit_multireq(s, &mrb);
95f7142a 1183 }
7aa1c247 1184
a937f8e8
SH
1185 /* Paired with inc in virtio_blk_dma_restart_cb() */
1186 blk_dec_in_flight(s->conf.conf.blk);
7aa1c247
SL
1187}
1188
538f0497 1189static void virtio_blk_dma_restart_cb(void *opaque, bool running,
1dfb4dd9 1190 RunState state)
213189ab
MA
1191{
1192 VirtIOBlock *s = opaque;
71ee0cdd 1193 uint16_t num_queues = s->conf.num_queues;
213189ab 1194
392808b4 1195 if (!running) {
213189ab 1196 return;
392808b4 1197 }
213189ab 1198
71ee0cdd
SH
1199 /* Split the device-wide s->rq request list into per-vq request lists */
1200 g_autofree VirtIOBlockReq **vq_rq = g_new0(VirtIOBlockReq *, num_queues);
1201 VirtIOBlockReq *rq;
1202
1203 WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
1204 rq = s->rq;
1205 s->rq = NULL;
1206 }
1207
1208 while (rq) {
1209 VirtIOBlockReq *next = rq->next;
1210 uint16_t idx = virtio_get_queue_index(rq->vq);
1211
1212 rq->next = vq_rq[idx];
1213 vq_rq[idx] = rq;
1214 rq = next;
1215 }
a937f8e8 1216
71ee0cdd
SH
1217 /* Schedule a BH to submit the requests in each vq's AioContext */
1218 for (uint16_t i = 0; i < num_queues; i++) {
1219 if (!vq_rq[i]) {
1220 continue;
1221 }
1222
1223 /* Paired with dec in virtio_blk_dma_restart_bh() */
1224 blk_inc_in_flight(s->conf.conf.blk);
1225
1226 aio_bh_schedule_oneshot(s->vq_aio_context[i],
1227 virtio_blk_dma_restart_bh,
1228 vq_rq[i]);
1229 }
213189ab
MA
1230}
1231
6e02c38d
AL
1232static void virtio_blk_reset(VirtIODevice *vdev)
1233{
1cc91b7d 1234 VirtIOBlock *s = VIRTIO_BLK(vdev);
26307f6a 1235 VirtIOBlockReq *req;
392808b4 1236
9c67f33f 1237 /* Dataplane has stopped... */
3cdaf3dd 1238 assert(!s->ioeventfd_started);
9c67f33f
SH
1239
1240 /* ...but requests may still be in flight. */
6e40b3bf
AY
1241 blk_drain(s->blk);
1242
26307f6a
FZ
1243 /* We drop queued requests after blk_drain() because blk_drain() itself can
1244 * produce them. */
9c67f33f
SH
1245 WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
1246 while (s->rq) {
1247 req = s->rq;
1248 s->rq = req->next;
26307f6a 1249
9c67f33f
SH
1250 /* No other threads can access req->vq here */
1251 virtqueue_detach_element(req->vq, &req->elem, 0);
1252
1253 virtio_blk_free_request(req);
1254 }
1255 }
6e40b3bf 1256
4be74634 1257 blk_set_enable_write_cache(s->blk, s->original_wce);
6e02c38d
AL
1258}
1259
bf011293 1260/* coalesce internal state, copy to pci i/o region 0
1261 */
6e02c38d
AL
1262static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
1263{
1cc91b7d 1264 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a30307f 1265 BlockConf *conf = &s->conf.conf;
4f736650 1266 BlockDriverState *bs = blk_bs(s->blk);
6e02c38d
AL
1267 struct virtio_blk_config blkcfg;
1268 uint64_t capacity;
17d0bc01 1269 int64_t length;
f7516731 1270 int blk_size = conf->logical_block_size;
6e02c38d 1271
4be74634 1272 blk_get_geometry(s->blk, &capacity);
5c5dafdc 1273 memset(&blkcfg, 0, sizeof(blkcfg));
783d1897 1274 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
1bf8a989
DP
1275 virtio_stl_p(vdev, &blkcfg.seg_max,
1276 s->conf.seg_max_adjust ? s->conf.queue_size - 2 : 128 - 2);
907eb3e5 1277 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
783d1897 1278 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
f7516731 1279 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
6abee260 1280 virtio_stl_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
907eb3e5 1281 blkcfg.geometry.heads = conf->heads;
136be99e
CB
1282 /*
1283 * We must ensure that the block device capacity is a multiple of
e03ba136 1284 * the logical block size. If that is not the case, let's use
136be99e
CB
1285 * sector_mask to adopt the geometry to have a correct picture.
1286 * For those devices where the capacity is ok for the given geometry
e03ba136 1287 * we don't touch the sector value of the geometry, since some devices
136be99e
CB
1288 * (like s390 dasd) need a specific value. Here the capacity is already
1289 * cyls*heads*secs*blk_size and the sector value is not block size
1290 * divided by 512 - instead it is the amount of blk_size blocks
1291 * per track (cylinder).
1292 */
17d0bc01
SH
1293 length = blk_getlength(s->blk);
1294 if (length > 0 && length / conf->heads / conf->secs % blk_size) {
907eb3e5 1295 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
136be99e 1296 } else {
907eb3e5 1297 blkcfg.geometry.sectors = conf->secs;
136be99e 1298 }
c7085da7 1299 blkcfg.size_max = 0;
f7516731 1300 blkcfg.physical_block_exp = get_physical_block_exp(conf);
9752c371 1301 blkcfg.alignment_offset = 0;
4be74634 1302 blkcfg.wce = blk_enable_write_cache(s->blk);
2f270590 1303 virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
37b06f8d 1304 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
fb0b154c
AO
1305 uint32_t discard_granularity = conf->discard_granularity;
1306 if (discard_granularity == -1 || !s->conf.report_discard_granularity) {
1307 discard_granularity = blk_size;
1308 }
37b06f8d
SG
1309 virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
1310 s->conf.max_discard_sectors);
1311 virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
fb0b154c 1312 discard_granularity >> BDRV_SECTOR_BITS);
37b06f8d
SG
1313 /*
1314 * We support only one segment per request since multiple segments
1315 * are not widely used and there are no userspace APIs that allow
1316 * applications to submit multiple segments in a single call.
1317 */
1318 virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
1319 }
1320 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
1321 virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
1322 s->conf.max_write_zeroes_sectors);
1323 blkcfg.write_zeroes_may_unmap = 1;
1324 virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
1325 }
4f736650
SL
1326 if (bs->bl.zoned != BLK_Z_NONE) {
1327 switch (bs->bl.zoned) {
1328 case BLK_Z_HM:
1329 blkcfg.zoned.model = VIRTIO_BLK_Z_HM;
1330 break;
1331 case BLK_Z_HA:
1332 blkcfg.zoned.model = VIRTIO_BLK_Z_HA;
1333 break;
1334 default:
1335 g_assert_not_reached();
1336 }
1337
1338 virtio_stl_p(vdev, &blkcfg.zoned.zone_sectors,
1339 bs->bl.zone_size / 512);
1340 virtio_stl_p(vdev, &blkcfg.zoned.max_active_zones,
1341 bs->bl.max_active_zones);
1342 virtio_stl_p(vdev, &blkcfg.zoned.max_open_zones,
1343 bs->bl.max_open_zones);
1344 virtio_stl_p(vdev, &blkcfg.zoned.write_granularity, blk_size);
1345 virtio_stl_p(vdev, &blkcfg.zoned.max_append_sectors,
1346 bs->bl.max_append_sectors);
1347 } else {
1348 blkcfg.zoned.model = VIRTIO_BLK_Z_NONE;
1349 }
20764be0 1350 memcpy(config, &blkcfg, s->config_size);
6e02c38d
AL
1351}
1352
13e3dce0
PB
1353static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
1354{
1cc91b7d 1355 VirtIOBlock *s = VIRTIO_BLK(vdev);
13e3dce0
PB
1356 struct virtio_blk_config blkcfg;
1357
20764be0 1358 memcpy(&blkcfg, config, s->config_size);
6d7e73d6 1359
4be74634 1360 blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
13e3dce0
PB
1361}
1362
9d5b731d
JW
1363static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
1364 Error **errp)
6e02c38d 1365{
1cc91b7d 1366 VirtIOBlock *s = VIRTIO_BLK(vdev);
1063b8b1 1367
bbe8bd4d
SG
1368 /* Firstly sync all virtio-blk possible supported features */
1369 features |= s->host_features;
1370
0cd09c3a
CH
1371 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
1372 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
1373 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
1374 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
95129d6f 1375 if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
bbe8bd4d 1376 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) {
efb8206c
JW
1377 error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
1378 return 0;
1379 }
efb8206c 1380 } else {
c9b11f97 1381 virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
efb8206c
JW
1382 virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
1383 }
aa659be3 1384
5f258577
EY
1385 if (blk_enable_write_cache(s->blk) ||
1386 (s->conf.x_enable_wce_if_config_wce &&
1387 virtio_has_feature(features, VIRTIO_BLK_F_CONFIG_WCE))) {
0cd09c3a 1388 virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
4be74634 1389 }
86b1cf32 1390 if (!blk_is_writable(s->blk)) {
0cd09c3a 1391 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
4be74634 1392 }
2f270590
SH
1393 if (s->conf.num_queues > 1) {
1394 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
1395 }
1063b8b1
CH
1396
1397 return features;
6e02c38d
AL
1398}
1399
9315cbfd
PB
1400static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
1401{
1cc91b7d 1402 VirtIOBlock *s = VIRTIO_BLK(vdev);
9315cbfd 1403
9ffe337c 1404 if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) {
3cdaf3dd 1405 assert(!s->ioeventfd_started);
392808b4 1406 }
392808b4 1407
9315cbfd
PB
1408 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1409 return;
1410 }
1411
ef5bc962
PB
1412 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
1413 * cache flushes. Thus, the "auto writethrough" behavior is never
1414 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
1415 * Leaving it enabled would break the following sequence:
1416 *
1417 * Guest started with "-drive cache=writethrough"
1418 * Guest sets status to 0
1419 * Guest sets DRIVER bit in status field
1420 * Guest reads host features (WCE=0, CONFIG_WCE=1)
1421 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
1422 * Guest writes 1 to the WCE configuration field (writeback mode)
1423 * Guest sets DRIVER_OK bit in status field
1424 *
4be74634 1425 * s->blk would erroneously be placed in writethrough mode.
ef5bc962 1426 */
95129d6f 1427 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
4be74634 1428 blk_set_enable_write_cache(s->blk,
95129d6f
CH
1429 virtio_vdev_has_feature(vdev,
1430 VIRTIO_BLK_F_WCE));
ef5bc962 1431 }
9315cbfd
PB
1432}
1433
b2b295a7
GK
1434static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
1435{
1436 VirtIOBlock *s = VIRTIO_BLK(vdev);
b2b295a7 1437
9c67f33f
SH
1438 WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
1439 VirtIOBlockReq *req = s->rq;
30d8bf6d 1440
9c67f33f
SH
1441 while (req) {
1442 qemu_put_sbyte(f, 1);
30d8bf6d 1443
9c67f33f
SH
1444 if (s->conf.num_queues > 1) {
1445 qemu_put_be32(f, virtio_get_queue_index(req->vq));
1446 }
1447
1448 qemu_put_virtqueue_element(vdev, f, &req->elem);
1449 req = req->next;
1450 }
869a5c6d 1451 }
9c67f33f 1452
869a5c6d 1453 qemu_put_sbyte(f, 0);
6e02c38d
AL
1454}
1455
b2b295a7
GK
1456static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
1457 int version_id)
1458{
1459 VirtIOBlock *s = VIRTIO_BLK(vdev);
2a633c46 1460
869a5c6d 1461 while (qemu_get_sbyte(f)) {
30d8bf6d
SH
1462 unsigned nvqs = s->conf.num_queues;
1463 unsigned vq_idx = 0;
ab281c17 1464 VirtIOBlockReq *req;
30d8bf6d
SH
1465
1466 if (nvqs > 1) {
1467 vq_idx = qemu_get_be32(f);
1468
1469 if (vq_idx >= nvqs) {
1470 error_report("Invalid virtqueue index in request list: %#x",
1471 vq_idx);
1472 return -EINVAL;
1473 }
1474 }
1475
8607f5c3 1476 req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
30d8bf6d 1477 virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
9c67f33f
SH
1478
1479 WITH_QEMU_LOCK_GUARD(&s->rq_lock) {
1480 req->next = s->rq;
1481 s->rq = req;
1482 }
869a5c6d 1483 }
6e02c38d
AL
1484
1485 return 0;
1486}
1487
b6948ab0
SH
1488static bool
1489validate_iothread_vq_mapping_list(IOThreadVirtQueueMappingList *list,
1490 uint16_t num_queues, Error **errp)
1491{
1492 g_autofree unsigned long *vqs = bitmap_new(num_queues);
1493 g_autoptr(GHashTable) iothreads =
1494 g_hash_table_new(g_str_hash, g_str_equal);
1495
1496 for (IOThreadVirtQueueMappingList *node = list; node; node = node->next) {
1497 const char *name = node->value->iothread;
1498 uint16List *vq;
1499
1500 if (!iothread_by_id(name)) {
1501 error_setg(errp, "IOThread \"%s\" object does not exist", name);
1502 return false;
1503 }
1504
1505 if (!g_hash_table_add(iothreads, (gpointer)name)) {
1506 error_setg(errp,
1507 "duplicate IOThread name \"%s\" in iothread-vq-mapping",
1508 name);
1509 return false;
1510 }
1511
1512 if (node != list) {
1513 if (!!node->value->vqs != !!list->value->vqs) {
1514 error_setg(errp, "either all items in iothread-vq-mapping "
1515 "must have vqs or none of them must have it");
1516 return false;
1517 }
1518 }
1519
1520 for (vq = node->value->vqs; vq; vq = vq->next) {
1521 if (vq->value >= num_queues) {
1522 error_setg(errp, "vq index %u for IOThread \"%s\" must be "
1523 "less than num_queues %u in iothread-vq-mapping",
1524 vq->value, name, num_queues);
1525 return false;
1526 }
1527
1528 if (test_and_set_bit(vq->value, vqs)) {
1529 error_setg(errp, "cannot assign vq %u to IOThread \"%s\" "
1530 "because it is already assigned", vq->value, name);
1531 return false;
1532 }
1533 }
1534 }
1535
1536 if (list->value->vqs) {
1537 for (uint16_t i = 0; i < num_queues; i++) {
1538 if (!test_bit(i, vqs)) {
1539 error_setg(errp,
1540 "missing vq %u IOThread assignment in iothread-vq-mapping",
1541 i);
1542 return false;
1543 }
1544 }
1545 }
1546
1547 return true;
1548}
1549
9b92fbcf
SL
1550static void virtio_resize_cb(void *opaque)
1551{
1552 VirtIODevice *vdev = opaque;
1553
1554 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1555 virtio_notify_config(vdev);
1556}
1557
145feb17 1558static void virtio_blk_resize(void *opaque)
e5051fc7 1559{
1cc91b7d 1560 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
e5051fc7 1561
9b92fbcf 1562 /*
0b2675c4 1563 * virtio_notify_config() needs to acquire the BQL,
9b92fbcf
SL
1564 * so it can't be called from an iothread. Instead, schedule
1565 * it to be run in the main context BH.
1566 */
1567 aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb, vdev);
e5051fc7
CH
1568}
1569
3cdaf3dd 1570static void virtio_blk_ioeventfd_detach(VirtIOBlock *s)
3bcc17f0
SH
1571{
1572 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1573
1574 for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1575 VirtQueue *vq = virtio_get_queue(vdev, i);
1576 virtio_queue_aio_detach_host_notifier(vq, s->vq_aio_context[i]);
1577 }
1578}
1579
3cdaf3dd 1580static void virtio_blk_ioeventfd_attach(VirtIOBlock *s)
3bcc17f0
SH
1581{
1582 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1583
1584 for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1585 VirtQueue *vq = virtio_get_queue(vdev, i);
1586 virtio_queue_aio_attach_host_notifier(vq, s->vq_aio_context[i]);
1587 }
1588}
1589
1665d932
SH
1590/* Suspend virtqueue ioeventfd processing during drain */
1591static void virtio_blk_drained_begin(void *opaque)
1592{
1593 VirtIOBlock *s = opaque;
1665d932 1594
3cdaf3dd
SH
1595 if (s->ioeventfd_started) {
1596 virtio_blk_ioeventfd_detach(s);
1665d932 1597 }
1665d932
SH
1598}
1599
1600/* Resume virtqueue ioeventfd processing after drain */
1601static void virtio_blk_drained_end(void *opaque)
1602{
1603 VirtIOBlock *s = opaque;
1665d932 1604
3cdaf3dd
SH
1605 if (s->ioeventfd_started) {
1606 virtio_blk_ioeventfd_attach(s);
1665d932 1607 }
1665d932
SH
1608}
1609
0e49de52 1610static const BlockDevOps virtio_block_ops = {
1665d932
SH
1611 .resize_cb = virtio_blk_resize,
1612 .drained_begin = virtio_blk_drained_begin,
1613 .drained_end = virtio_blk_drained_end,
0e49de52
MA
1614};
1615
3bcc17f0
SH
1616/* Generate vq:AioContext mappings from a validated iothread-vq-mapping list */
1617static void
1618apply_vq_mapping(IOThreadVirtQueueMappingList *iothread_vq_mapping_list,
1619 AioContext **vq_aio_context, uint16_t num_queues)
1620{
1621 IOThreadVirtQueueMappingList *node;
1622 size_t num_iothreads = 0;
1623 size_t cur_iothread = 0;
1624
1625 for (node = iothread_vq_mapping_list; node; node = node->next) {
1626 num_iothreads++;
1627 }
1628
1629 for (node = iothread_vq_mapping_list; node; node = node->next) {
1630 IOThread *iothread = iothread_by_id(node->value->iothread);
1631 AioContext *ctx = iothread_get_aio_context(iothread);
1632
57bc2658 1633 /* Released in virtio_blk_vq_aio_context_cleanup() */
3bcc17f0
SH
1634 object_ref(OBJECT(iothread));
1635
1636 if (node->value->vqs) {
1637 uint16List *vq;
1638
1639 /* Explicit vq:IOThread assignment */
1640 for (vq = node->value->vqs; vq; vq = vq->next) {
1641 vq_aio_context[vq->value] = ctx;
1642 }
1643 } else {
1644 /* Round-robin vq:IOThread assignment */
1645 for (unsigned i = cur_iothread; i < num_queues;
1646 i += num_iothreads) {
1647 vq_aio_context[i] = ctx;
1648 }
1649 }
1650
1651 cur_iothread++;
1652 }
1653}
1654
1655/* Context: BQL held */
57bc2658 1656static bool virtio_blk_vq_aio_context_init(VirtIOBlock *s, Error **errp)
3bcc17f0
SH
1657{
1658 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1659 VirtIOBlkConf *conf = &s->conf;
1660 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1661 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1662
1663 if (conf->iothread || conf->iothread_vq_mapping_list) {
1664 if (!k->set_guest_notifiers || !k->ioeventfd_assign) {
1665 error_setg(errp,
1666 "device is incompatible with iothread "
1667 "(transport does not support notifiers)");
1668 return false;
1669 }
1670 if (!virtio_device_ioeventfd_enabled(vdev)) {
1671 error_setg(errp, "ioeventfd is required for iothread");
1672 return false;
1673 }
1674
1675 /*
3cdaf3dd 1676 * If ioeventfd is (re-)enabled while the guest is running there could
3bcc17f0
SH
1677 * be block jobs that can conflict.
1678 */
1679 if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
3cdaf3dd 1680 error_prepend(errp, "cannot start virtio-blk ioeventfd: ");
3bcc17f0
SH
1681 return false;
1682 }
1683 }
3bcc17f0
SH
1684
1685 s->vq_aio_context = g_new(AioContext *, conf->num_queues);
1686
1687 if (conf->iothread_vq_mapping_list) {
1688 apply_vq_mapping(conf->iothread_vq_mapping_list, s->vq_aio_context,
1689 conf->num_queues);
1690 } else if (conf->iothread) {
1691 AioContext *ctx = iothread_get_aio_context(conf->iothread);
1692 for (unsigned i = 0; i < conf->num_queues; i++) {
1693 s->vq_aio_context[i] = ctx;
1694 }
1695
57bc2658 1696 /* Released in virtio_blk_vq_aio_context_cleanup() */
3bcc17f0
SH
1697 object_ref(OBJECT(conf->iothread));
1698 } else {
1699 AioContext *ctx = qemu_get_aio_context();
1700 for (unsigned i = 0; i < conf->num_queues; i++) {
1701 s->vq_aio_context[i] = ctx;
1702 }
1703 }
1704
1705 return true;
1706}
1707
1708/* Context: BQL held */
57bc2658 1709static void virtio_blk_vq_aio_context_cleanup(VirtIOBlock *s)
3bcc17f0
SH
1710{
1711 VirtIOBlkConf *conf = &s->conf;
1712
3cdaf3dd 1713 assert(!s->ioeventfd_started);
3bcc17f0
SH
1714
1715 if (conf->iothread_vq_mapping_list) {
1716 IOThreadVirtQueueMappingList *node;
1717
1718 for (node = conf->iothread_vq_mapping_list; node; node = node->next) {
1719 IOThread *iothread = iothread_by_id(node->value->iothread);
1720 object_unref(OBJECT(iothread));
1721 }
1722 }
1723
1724 if (conf->iothread) {
1725 object_unref(OBJECT(conf->iothread));
1726 }
1727
1728 g_free(s->vq_aio_context);
1729 s->vq_aio_context = NULL;
1730}
1731
1732/* Context: BQL held */
3cdaf3dd 1733static int virtio_blk_start_ioeventfd(VirtIODevice *vdev)
3bcc17f0
SH
1734{
1735 VirtIOBlock *s = VIRTIO_BLK(vdev);
1736 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
1737 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1738 unsigned i;
1739 unsigned nvqs = s->conf.num_queues;
1740 Error *local_err = NULL;
1741 int r;
1742
3cdaf3dd 1743 if (s->ioeventfd_started || s->ioeventfd_starting) {
3bcc17f0
SH
1744 return 0;
1745 }
1746
3cdaf3dd 1747 s->ioeventfd_starting = true;
3bcc17f0
SH
1748
1749 /* Set up guest notifier (irq) */
1750 r = k->set_guest_notifiers(qbus->parent, nvqs, true);
1751 if (r != 0) {
1752 error_report("virtio-blk failed to set guest notifier (%d), "
1753 "ensure -accel kvm is set.", r);
1754 goto fail_guest_notifiers;
1755 }
1756
1757 /*
1758 * Batch all the host notifiers in a single transaction to avoid
1759 * quadratic time complexity in address_space_update_ioeventfds().
1760 */
1761 memory_region_transaction_begin();
1762
1763 /* Set up virtqueue notify */
1764 for (i = 0; i < nvqs; i++) {
1765 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, true);
1766 if (r != 0) {
1767 int j = i;
1768
1769 fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
1770 while (i--) {
1771 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
1772 }
1773
1774 /*
1775 * The transaction expects the ioeventfds to be open when it
1776 * commits. Do it now, before the cleanup loop.
1777 */
1778 memory_region_transaction_commit();
1779
1780 while (j--) {
1781 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), j);
1782 }
1783 goto fail_host_notifiers;
1784 }
1785 }
1786
1787 memory_region_transaction_commit();
1788
ea0736d7
SH
1789 /*
1790 * Try to change the AioContext so that block jobs and other operations can
1791 * co-locate their activity in the same AioContext. If it fails, nevermind.
1792 */
3bcc17f0
SH
1793 r = blk_set_aio_context(s->conf.conf.blk, s->vq_aio_context[0],
1794 &local_err);
1795 if (r < 0) {
ea0736d7 1796 warn_report_err(local_err);
3bcc17f0
SH
1797 }
1798
1799 /*
1800 * These fields must be visible to the IOThread when it processes the
3cdaf3dd 1801 * virtqueue, otherwise it will think ioeventfd has not started yet.
3bcc17f0 1802 *
3cdaf3dd 1803 * Make sure ->ioeventfd_started is false when blk_set_aio_context() is
3bcc17f0
SH
1804 * called above so that draining does not cause the host notifier to be
1805 * detached/attached prematurely.
1806 */
3cdaf3dd
SH
1807 s->ioeventfd_starting = false;
1808 s->ioeventfd_started = true;
3bcc17f0
SH
1809 smp_wmb(); /* paired with aio_notify_accept() on the read side */
1810
1811 /* Get this show started by hooking up our callbacks */
d3f6f294
SH
1812 for (i = 0; i < nvqs; i++) {
1813 VirtQueue *vq = virtio_get_queue(vdev, i);
1814 AioContext *ctx = s->vq_aio_context[i];
3bcc17f0 1815
d3f6f294
SH
1816 /* Kick right away to begin processing requests already in vring */
1817 event_notifier_set(virtio_queue_get_host_notifier(vq));
3bcc17f0 1818
d3f6f294 1819 if (!blk_in_drain(s->conf.conf.blk)) {
3bcc17f0
SH
1820 virtio_queue_aio_attach_host_notifier(vq, ctx);
1821 }
1822 }
1823 return 0;
1824
3bcc17f0
SH
1825 fail_host_notifiers:
1826 k->set_guest_notifiers(qbus->parent, nvqs, false);
1827 fail_guest_notifiers:
3cdaf3dd
SH
1828 s->ioeventfd_disabled = true;
1829 s->ioeventfd_starting = false;
3bcc17f0
SH
1830 return -ENOSYS;
1831}
1832
1833/* Stop notifications for new requests from guest.
1834 *
1835 * Context: BH in IOThread
1836 */
3cdaf3dd 1837static void virtio_blk_ioeventfd_stop_vq_bh(void *opaque)
3bcc17f0
SH
1838{
1839 VirtQueue *vq = opaque;
1840 EventNotifier *host_notifier = virtio_queue_get_host_notifier(vq);
1841
1842 virtio_queue_aio_detach_host_notifier(vq, qemu_get_current_aio_context());
1843
1844 /*
1845 * Test and clear notifier after disabling event, in case poll callback
1846 * didn't have time to run.
1847 */
1848 virtio_queue_host_notifier_read(host_notifier);
1849}
1850
1851/* Context: BQL held */
3cdaf3dd 1852static void virtio_blk_stop_ioeventfd(VirtIODevice *vdev)
3bcc17f0
SH
1853{
1854 VirtIOBlock *s = VIRTIO_BLK(vdev);
1855 BusState *qbus = qdev_get_parent_bus(DEVICE(s));
1856 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1857 unsigned i;
1858 unsigned nvqs = s->conf.num_queues;
1859
3cdaf3dd 1860 if (!s->ioeventfd_started || s->ioeventfd_stopping) {
3bcc17f0
SH
1861 return;
1862 }
1863
1864 /* Better luck next time. */
3cdaf3dd
SH
1865 if (s->ioeventfd_disabled) {
1866 s->ioeventfd_disabled = false;
1867 s->ioeventfd_started = false;
3bcc17f0
SH
1868 return;
1869 }
3cdaf3dd 1870 s->ioeventfd_stopping = true;
3bcc17f0
SH
1871
1872 if (!blk_in_drain(s->conf.conf.blk)) {
1873 for (i = 0; i < nvqs; i++) {
1874 VirtQueue *vq = virtio_get_queue(vdev, i);
1875 AioContext *ctx = s->vq_aio_context[i];
1876
3cdaf3dd 1877 aio_wait_bh_oneshot(ctx, virtio_blk_ioeventfd_stop_vq_bh, vq);
3bcc17f0
SH
1878 }
1879 }
1880
1881 /*
1882 * Batch all the host notifiers in a single transaction to avoid
1883 * quadratic time complexity in address_space_update_ioeventfds().
1884 */
1885 memory_region_transaction_begin();
1886
1887 for (i = 0; i < nvqs; i++) {
1888 virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), i, false);
1889 }
1890
1891 /*
1892 * The transaction expects the ioeventfds to be open when it
1893 * commits. Do it now, before the cleanup loop.
1894 */
1895 memory_region_transaction_commit();
1896
1897 for (i = 0; i < nvqs; i++) {
1898 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), i);
1899 }
1900
1901 /*
3cdaf3dd 1902 * Set ->ioeventfd_started to false before draining so that host notifiers
3bcc17f0
SH
1903 * are not detached/attached anymore.
1904 */
3cdaf3dd 1905 s->ioeventfd_started = false;
3bcc17f0
SH
1906
1907 /* Wait for virtio_blk_dma_restart_bh() and in flight I/O to complete */
1908 blk_drain(s->conf.conf.blk);
1909
1910 /*
1911 * Try to switch bs back to the QEMU main loop. If other users keep the
1912 * BlockBackend in the iothread, that's ok
1913 */
1914 blk_set_aio_context(s->conf.conf.blk, qemu_get_aio_context(), NULL);
1915
1916 /* Clean up guest notifier (irq) */
1917 k->set_guest_notifiers(qbus->parent, nvqs, false);
1918
3cdaf3dd 1919 s->ioeventfd_stopping = false;
3bcc17f0
SH
1920}
1921
75884afd 1922static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
1c028ddf 1923{
75884afd 1924 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179b417e 1925 VirtIOBlock *s = VIRTIO_BLK(dev);
2a30307f 1926 VirtIOBlkConf *conf = &s->conf;
3ffeeef7 1927 Error *err = NULL;
2f270590 1928 unsigned i;
cf21e106 1929
4be74634 1930 if (!conf->conf.blk) {
75884afd
AF
1931 error_setg(errp, "drive property not set");
1932 return;
d75d25e3 1933 }
4be74634 1934 if (!blk_is_inserted(conf->conf.blk)) {
75884afd
AF
1935 error_setg(errp, "Device needs media, but drive is empty");
1936 return;
98f28ad7 1937 }
9445e1e1
SH
1938 if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
1939 conf->num_queues = 1;
1940 }
2f270590
SH
1941 if (!conf->num_queues) {
1942 error_setg(errp, "num-queues property must be larger than 0");
1943 return;
1944 }
1bf8a989
DP
1945 if (conf->queue_size <= 2) {
1946 error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1947 "must be > 2", conf->queue_size);
1948 return;
1949 }
6040aedd
MK
1950 if (!is_power_of_2(conf->queue_size) ||
1951 conf->queue_size > VIRTQUEUE_MAX_SIZE) {
1952 error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1953 "must be a power of 2 (max %d)",
1954 conf->queue_size, VIRTQUEUE_MAX_SIZE);
1955 return;
1956 }
d75d25e3 1957
ceff3e1f 1958 if (!blkconf_apply_backend_options(&conf->conf,
86b1cf32
KW
1959 !blk_supports_write_perm(conf->conf.blk),
1960 true, errp)) {
a17c17a2
KW
1961 return;
1962 }
4be74634 1963 s->original_wce = blk_enable_write_cache(conf->conf.blk);
ceff3e1f 1964 if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) {
75884afd 1965 return;
b7eb0c9f 1966 }
ceff3e1f 1967
c56ee92f 1968 if (!blkconf_blocksizes(&conf->conf, errp)) {
0a75b60c
MK
1969 return;
1970 }
1971
4f736650
SL
1972 BlockDriverState *bs = blk_bs(conf->conf.blk);
1973 if (bs->bl.zoned != BLK_Z_NONE) {
1974 virtio_add_feature(&s->host_features, VIRTIO_BLK_F_ZONED);
1975 if (bs->bl.zoned == BLK_Z_HM) {
1976 virtio_clear_feature(&s->host_features, VIRTIO_BLK_F_DISCARD);
1977 }
1978 }
1979
37b06f8d
SG
1980 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) &&
1981 (!conf->max_discard_sectors ||
1982 conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1983 error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")"
1984 ", must be between 1 and %d",
1985 conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS);
1986 return;
1987 }
1988
1989 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) &&
1990 (!conf->max_write_zeroes_sectors ||
1991 conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1992 error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32
1993 "), must be between 1 and %d",
1994 conf->max_write_zeroes_sectors,
1995 (int)BDRV_REQUEST_MAX_SECTORS);
1996 return;
1997 }
1998
b6948ab0
SH
1999 if (conf->iothread_vq_mapping_list) {
2000 if (conf->iothread) {
2001 error_setg(errp, "iothread and iothread-vq-mapping properties "
2002 "cannot be set at the same time");
2003 return;
2004 }
2005
2006 if (!validate_iothread_vq_mapping_list(conf->iothread_vq_mapping_list,
2007 conf->num_queues, errp)) {
2008 return;
2009 }
2010 }
2011
d9cf55a8 2012 s->config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
d74c30c8 2013 s->host_features);
3857cd5c 2014 virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
6e02c38d 2015
9c67f33f
SH
2016 qemu_mutex_init(&s->rq_lock);
2017
4be74634 2018 s->blk = conf->conf.blk;
869a5c6d 2019 s->rq = NULL;
2a30307f 2020 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
e63e7fde 2021
2f270590 2022 for (i = 0; i < conf->num_queues; i++) {
6040aedd 2023 virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
2f270590 2024 }
98e3ab35 2025 qemu_coroutine_inc_pool_size(conf->num_queues * conf->queue_size / 2);
57bc2658 2026
3cdaf3dd 2027 /* Don't start ioeventfd if transport does not support notifiers. */
57bc2658 2028 if (!virtio_device_ioeventfd_enabled(vdev)) {
3cdaf3dd 2029 s->ioeventfd_disabled = true;
57bc2658
SH
2030 }
2031
2032 virtio_blk_vq_aio_context_init(s, &err);
3ffeeef7 2033 if (err != NULL) {
75884afd 2034 error_propagate(errp, err);
cfaf757e
PN
2035 for (i = 0; i < conf->num_queues; i++) {
2036 virtio_del_queue(vdev, i);
2037 }
6a1a8cc7 2038 virtio_cleanup(vdev);
75884afd 2039 return;
392808b4 2040 }
6e02c38d 2041
a937f8e8
SH
2042 /*
2043 * This must be after virtio_init() so virtio_blk_dma_restart_cb() gets
2044 * called after ->start_ioeventfd() has already set blk's AioContext.
2045 */
2046 s->change =
2047 qdev_add_vm_change_state_handler(dev, virtio_blk_dma_restart_cb, s);
2048
baf42268 2049 blk_ram_registrar_init(&s->blk_ram_registrar, s->blk);
4be74634 2050 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
6e02c38d 2051
4be74634 2052 blk_iostatus_enable(s->blk);
71f571a2
SE
2053
2054 add_boot_device_lchs(dev, "/disk@0,0",
2055 conf->conf.lcyls,
2056 conf->conf.lheads,
2057 conf->conf.lsecs);
1c028ddf
FK
2058}
2059
b69c3c21 2060static void virtio_blk_device_unrealize(DeviceState *dev)
1c028ddf 2061{
306ec6c3
AF
2062 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2063 VirtIOBlock *s = VIRTIO_BLK(dev);
4a0117cf
EP
2064 VirtIOBlkConf *conf = &s->conf;
2065 unsigned i;
306ec6c3 2066
7bfde688 2067 blk_drain(s->blk);
71f571a2 2068 del_boot_device_lchs(dev, "/disk@0,0");
57bc2658 2069 virtio_blk_vq_aio_context_cleanup(s);
4a0117cf
EP
2070 for (i = 0; i < conf->num_queues; i++) {
2071 virtio_del_queue(vdev, i);
2072 }
98e3ab35 2073 qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
9c67f33f 2074 qemu_mutex_destroy(&s->rq_lock);
baf42268 2075 blk_ram_registrar_destroy(&s->blk_ram_registrar);
1c028ddf 2076 qemu_del_vm_change_state_handler(s->change);
4be74634 2077 blockdev_mark_auto_del(s->blk);
6a1a8cc7 2078 virtio_cleanup(vdev);
1c028ddf
FK
2079}
2080
467b3f33
SH
2081static void virtio_blk_instance_init(Object *obj)
2082{
2083 VirtIOBlock *s = VIRTIO_BLK(obj);
2084
2a30307f 2085 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
3342ec32 2086 "bootindex", "/disk@0,0",
40c2281c 2087 DEVICE(obj));
467b3f33
SH
2088}
2089
977a117f
HP
2090static const VMStateDescription vmstate_virtio_blk = {
2091 .name = "virtio-blk",
2092 .minimum_version_id = 2,
2093 .version_id = 2,
7d5dc0a3 2094 .fields = (const VMStateField[]) {
977a117f
HP
2095 VMSTATE_VIRTIO_DEVICE,
2096 VMSTATE_END_OF_LIST()
2097 },
2098};
bbded32c 2099
1c028ddf 2100static Property virtio_blk_properties[] = {
2a30307f 2101 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
8c398252 2102 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
2a30307f
MA
2103 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
2104 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
bbe8bd4d
SG
2105 DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features,
2106 VIRTIO_BLK_F_CONFIG_WCE, true),
32a877e4 2107#ifdef __linux__
bbe8bd4d
SG
2108 DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features,
2109 VIRTIO_BLK_F_SCSI, false),
32a877e4 2110#endif
c99495ac
PL
2111 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
2112 true),
9445e1e1
SH
2113 DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues,
2114 VIRTIO_BLK_AUTO_NUM_QUEUES),
c9b7d9ec 2115 DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 256),
1bf8a989 2116 DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock, conf.seg_max_adjust, true),
d679ac09
FZ
2117 DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD,
2118 IOThread *),
b6948ab0
SH
2119 DEFINE_PROP_IOTHREAD_VQ_MAPPING_LIST("iothread-vq-mapping", VirtIOBlock,
2120 conf.iothread_vq_mapping_list),
5c81161f
SG
2121 DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features,
2122 VIRTIO_BLK_F_DISCARD, true),
fb0b154c
AO
2123 DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock,
2124 conf.report_discard_granularity, true),
5c81161f
SG
2125 DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features,
2126 VIRTIO_BLK_F_WRITE_ZEROES, true),
37b06f8d
SG
2127 DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock,
2128 conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
2129 DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
2130 conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
5f258577
EY
2131 DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
2132 conf.x_enable_wce_if_config_wce, true),
1c028ddf
FK
2133 DEFINE_PROP_END_OF_LIST(),
2134};
2135
2136static void virtio_blk_class_init(ObjectClass *klass, void *data)
2137{
2138 DeviceClass *dc = DEVICE_CLASS(klass);
2139 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
75884afd 2140
4f67d30b 2141 device_class_set_props(dc, virtio_blk_properties);
bbded32c 2142 dc->vmsd = &vmstate_virtio_blk;
125ee0ed 2143 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
75884afd 2144 vdc->realize = virtio_blk_device_realize;
306ec6c3 2145 vdc->unrealize = virtio_blk_device_unrealize;
1c028ddf
FK
2146 vdc->get_config = virtio_blk_update_config;
2147 vdc->set_config = virtio_blk_set_config;
2148 vdc->get_features = virtio_blk_get_features;
2149 vdc->set_status = virtio_blk_set_status;
2150 vdc->reset = virtio_blk_reset;
b2b295a7
GK
2151 vdc->save = virtio_blk_save_device;
2152 vdc->load = virtio_blk_load_device;
3cdaf3dd
SH
2153 vdc->start_ioeventfd = virtio_blk_start_ioeventfd;
2154 vdc->stop_ioeventfd = virtio_blk_stop_ioeventfd;
1c028ddf
FK
2155}
2156
b5c7ceaf 2157static const TypeInfo virtio_blk_info = {
1c028ddf
FK
2158 .name = TYPE_VIRTIO_BLK,
2159 .parent = TYPE_VIRTIO_DEVICE,
2160 .instance_size = sizeof(VirtIOBlock),
467b3f33 2161 .instance_init = virtio_blk_instance_init,
1c028ddf
FK
2162 .class_init = virtio_blk_class_init,
2163};
2164
2165static void virtio_register_types(void)
2166{
b5c7ceaf 2167 type_register_static(&virtio_blk_info);
1c028ddf
FK
2168}
2169
2170type_init(virtio_register_types)