blob: a1f8e155227591fb428f065d11658f133e348a6b [file] [log] [blame]
aliguori6e02c382008-12-04 19:52:44 +00001/*
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
Peter Maydell80c71a22016-01-18 18:01:42 +000014#include "qemu/osdep.h"
Stefan Hajnoczi433fcea2023-09-13 16:00:43 -040015#include "qemu/defer-call.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Fam Zheng827805a2014-06-11 12:11:48 +080017#include "qemu/iov.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020018#include "qemu/module.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/error-report.h"
Sergio Lopez9b92fbc2019-09-16 13:24:12 +020020#include "qemu/main-loop.h"
Sam Li4f736652023-05-08 13:19:13 +080021#include "block/block_int.h"
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010022#include "trace.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010023#include "hw/block/block.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020024#include "hw/qdev-properties.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/blockdev.h"
Stefan Hajnoczibaf42262022-10-13 14:59:08 -040026#include "sysemu/block-ram-registrar.h"
Markus Armbruster2f780b62019-08-12 07:23:58 +020027#include "sysemu/sysemu.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020028#include "sysemu/runstate.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/virtio/virtio-blk.h"
Fam Zheng52b53c02014-09-10 14:17:51 +080030#include "dataplane/virtio-blk.h"
Paolo Bonzini08e2c9f2017-08-22 09:23:55 +020031#include "scsi/constants.h"
Christoph Hellwig1063b8b2009-04-27 10:29:14 +020032#ifdef __linux__
33# include <scsi/sg.h>
34#endif
Paolo Bonzini0d09e412013-02-05 17:06:20 +010035#include "hw/virtio/virtio-bus.h"
Markus Armbrusterca77ee22019-08-12 07:23:39 +020036#include "migration/qemu-file-types.h"
Rusty Russell783d1892014-06-24 19:43:44 +020037#include "hw/virtio/virtio-access.h"
Daniil Tatianind9cf55a2022-09-06 10:31:08 +030038#include "hw/virtio/virtio-blk-common.h"
Hiroki Narukawa4c41c692022-02-14 20:53:02 +090039#include "qemu/coroutine.h"
aliguori6e02c382008-12-04 19:52:44 +000040
Greg Kurzd14dde52016-09-30 17:12:50 +020041static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
42 VirtIOBlockReq *req)
Fam Zheng671ec3f2014-06-11 12:11:43 +080043{
Fam Zheng671ec3f2014-06-11 12:11:43 +080044 req->dev = s;
Stefan Hajnocziedaffd92016-06-21 13:13:13 +010045 req->vq = vq;
Stefan Hajnoczi869d66a2014-07-09 10:05:48 +020046 req->qiov.size = 0;
Paolo Bonzini2a6cdd62015-04-02 19:50:44 +020047 req->in_len = 0;
Stefan Hajnoczi869d66a2014-07-09 10:05:48 +020048 req->next = NULL;
Peter Lieven95f71422015-02-02 14:52:21 +010049 req->mr_next = NULL;
Fam Zheng671ec3f2014-06-11 12:11:43 +080050}
51
Greg Kurzd14dde52016-09-30 17:12:50 +020052static void virtio_blk_free_request(VirtIOBlockReq *req)
Fam Zheng671ec3f2014-06-11 12:11:43 +080053{
Fam Zheng1d29b5b2017-02-07 21:27:22 +080054 g_free(req);
Fam Zheng671ec3f2014-06-11 12:11:43 +080055}
56
Paolo Bonzini03de2f52016-02-14 18:17:09 +010057static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
aliguori869a5c62009-01-22 19:52:25 +000058{
59 VirtIOBlock *s = req->dev;
KONRAD Frederic1cc91b72013-03-18 17:37:27 +010060 VirtIODevice *vdev = VIRTIO_DEVICE(s);
aliguori869a5c62009-01-22 19:52:25 +000061
Stefan Hajnoczia576cea2017-06-14 10:29:30 +010062 trace_virtio_blk_req_complete(vdev, req, status);
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +010063
Aurelien Jarno92e3c2a2011-01-25 11:55:14 +010064 stb_p(&req->in->status, status);
Stefan Hajnoczi7bd04a02020-09-17 10:44:54 +010065 iov_discard_undo(&req->inhdr_undo);
66 iov_discard_undo(&req->outhdr_undo);
Stefan Hajnocziedaffd92016-06-21 13:13:13 +010067 virtqueue_push(req->vq, &req->elem, req->in_len);
Paolo Bonzinieb41cf72016-04-06 12:16:23 +020068 if (s->dataplane_started && !s->dataplane_disabled) {
Stefan Hajnocziedaffd92016-06-21 13:13:13 +010069 virtio_blk_data_plane_notify(s->dataplane, req->vq);
Paolo Bonzini03de2f52016-02-14 18:17:09 +010070 } else {
Stefan Hajnocziedaffd92016-06-21 13:13:13 +010071 virtio_notify(vdev, req->vq);
Paolo Bonzini03de2f52016-02-14 18:17:09 +010072 }
Fam Zhengbf4bd462014-06-17 14:32:06 +080073}
74
Kevin Wolff35d68f2009-11-27 13:25:39 +010075static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
Stefano Garzarella00f639f2019-02-21 11:33:05 +010076 bool is_read, bool acct_failed)
aliguori869a5c62009-01-22 19:52:25 +000077{
aliguori869a5c62009-01-22 19:52:25 +000078 VirtIOBlock *s = req->dev;
Stefano Garzarella9a6719d2019-02-08 15:23:47 +010079 BlockErrorAction action = blk_get_error_action(s->blk, is_read, error);
aliguori869a5c62009-01-22 19:52:25 +000080
Wenchao Xiaa5895692014-06-18 08:43:30 +020081 if (action == BLOCK_ERROR_ACTION_STOP) {
Fam Zheng466138d2015-11-23 08:41:20 +080082 /* Break the link as the next request is going to be parsed from the
83 * ring again. Otherwise we may end up doing a double completion! */
84 req->mr_next = NULL;
aliguori869a5c62009-01-22 19:52:25 +000085 req->next = s->rq;
86 s->rq = req;
Wenchao Xiaa5895692014-06-18 08:43:30 +020087 } else if (action == BLOCK_ERROR_ACTION_REPORT) {
aliguori869a5c62009-01-22 19:52:25 +000088 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
Stefano Garzarella00f639f2019-02-21 11:33:05 +010089 if (acct_failed) {
90 block_acct_failed(blk_get_stats(s->blk), &req->acct);
91 }
Fam Zheng671ec3f2014-06-11 12:11:43 +080092 virtio_blk_free_request(req);
aliguori869a5c62009-01-22 19:52:25 +000093 }
94
Markus Armbruster4be74632014-10-07 13:59:18 +020095 blk_error_action(s->blk, action, is_read, error);
Wenchao Xiaa5895692014-06-18 08:43:30 +020096 return action != BLOCK_ERROR_ACTION_IGNORE;
aliguori869a5c62009-01-22 19:52:25 +000097}
98
aliguori6e02c382008-12-04 19:52:44 +000099static void virtio_blk_rw_complete(void *opaque, int ret)
100{
Peter Lieven95f71422015-02-02 14:52:21 +0100101 VirtIOBlockReq *next = opaque;
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100102 VirtIOBlock *s = next->dev;
Stefan Hajnoczia576cea2017-06-14 10:29:30 +0100103 VirtIODevice *vdev = VIRTIO_DEVICE(s);
aliguori6e02c382008-12-04 19:52:44 +0000104
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100105 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
Peter Lieven95f71422015-02-02 14:52:21 +0100106 while (next) {
107 VirtIOBlockReq *req = next;
108 next = req->mr_next;
Stefan Hajnoczia576cea2017-06-14 10:29:30 +0100109 trace_virtio_blk_rw_complete(vdev, req, ret);
Stefan Hajnoczi6d519a52010-05-22 18:15:08 +0100110
Peter Lieven95f71422015-02-02 14:52:21 +0100111 if (req->qiov.nalloc != -1) {
Dongli Zhange61809e2018-11-06 12:52:32 +0800112 /* If nalloc is != -1 req->qiov is a local copy of the original
Yaowei Bai9bb192a2018-07-28 13:18:44 +0800113 * external iovec. It was allocated in submit_requests to be
114 * able to merge requests. */
Peter Lieven95f71422015-02-02 14:52:21 +0100115 qemu_iovec_destroy(&req->qiov);
116 }
117
118 if (ret) {
Anastasiia Rusakovabf4069f2019-03-07 18:19:25 +0200119 int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type);
Peter Lieven95f71422015-02-02 14:52:21 +0100120 bool is_read = !(p & VIRTIO_BLK_T_OUT);
Paolo Bonzini2a6cdd62015-04-02 19:50:44 +0200121 /* Note that memory may be dirtied on read failure. If the
122 * virtio request is not completed here, as is the case for
123 * BLOCK_ERROR_ACTION_STOP, the memory may not be copied
124 * correctly during live migration. While this is ugly,
125 * it is acceptable because the device is free to write to
126 * the memory until the request is completed (which will
127 * happen on the other side of the migration).
128 */
Stefano Garzarella00f639f2019-02-21 11:33:05 +0100129 if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) {
Peter Lieven95f71422015-02-02 14:52:21 +0100130 continue;
131 }
132 }
133
134 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
Anastasiia Rusakovabf4069f2019-03-07 18:19:25 +0200135 block_acct_done(blk_get_stats(s->blk), &req->acct);
Peter Lieven95f71422015-02-02 14:52:21 +0100136 virtio_blk_free_request(req);
aliguori6e02c382008-12-04 19:52:44 +0000137 }
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100138 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
aliguori869a5c62009-01-22 19:52:25 +0000139}
aliguori6e02c382008-12-04 19:52:44 +0000140
Christoph Hellwigaa659be2009-09-04 19:02:23 +0200141static void virtio_blk_flush_complete(void *opaque, int ret)
142{
143 VirtIOBlockReq *req = opaque;
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100144 VirtIOBlock *s = req->dev;
Christoph Hellwigaa659be2009-09-04 19:02:23 +0200145
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100146 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
Kevin Wolf8c269b52010-10-20 13:17:30 +0200147 if (ret) {
Stefano Garzarella00f639f2019-02-21 11:33:05 +0100148 if (virtio_blk_handle_rw_error(req, -ret, 0, true)) {
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100149 goto out;
Kevin Wolf8c269b52010-10-20 13:17:30 +0200150 }
151 }
152
153 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
Stefano Garzarella9a6719d2019-02-08 15:23:47 +0100154 block_acct_done(blk_get_stats(s->blk), &req->acct);
Fam Zheng671ec3f2014-06-11 12:11:43 +0800155 virtio_blk_free_request(req);
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100156
157out:
158 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
aliguori6e02c382008-12-04 19:52:44 +0000159}
160
Stefano Garzarella37b06f82019-02-21 11:33:10 +0100161static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
162{
163 VirtIOBlockReq *req = opaque;
164 VirtIOBlock *s = req->dev;
165 bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) &
166 ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES;
167
168 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
169 if (ret) {
170 if (virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) {
171 goto out;
172 }
173 }
174
175 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
176 if (is_write_zeroes) {
177 block_acct_done(blk_get_stats(s->blk), &req->acct);
178 }
179 virtio_blk_free_request(req);
180
181out:
182 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
183}
184
Fam Zheng1dc936a2015-01-20 11:28:47 +0800185#ifdef __linux__
186
187typedef struct {
188 VirtIOBlockReq *req;
189 struct sg_io_hdr hdr;
190} VirtIOBlockIoctlReq;
191
192static void virtio_blk_ioctl_complete(void *opaque, int status)
193{
194 VirtIOBlockIoctlReq *ioctl_req = opaque;
195 VirtIOBlockReq *req = ioctl_req->req;
Paolo Bonzini9d456652017-02-13 14:52:30 +0100196 VirtIOBlock *s = req->dev;
197 VirtIODevice *vdev = VIRTIO_DEVICE(s);
Fam Zheng1dc936a2015-01-20 11:28:47 +0800198 struct virtio_scsi_inhdr *scsi;
199 struct sg_io_hdr *hdr;
200
201 scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
202
203 if (status) {
204 status = VIRTIO_BLK_S_UNSUPP;
205 virtio_stl_p(vdev, &scsi->errors, 255);
206 goto out;
207 }
208
209 hdr = &ioctl_req->hdr;
210 /*
211 * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi)
212 * clear the masked_status field [hence status gets cleared too, see
213 * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED
214 * status has occurred. However they do set DRIVER_SENSE in driver_status
215 * field. Also a (sb_len_wr > 0) indicates there is a sense buffer.
216 */
217 if (hdr->status == 0 && hdr->sb_len_wr > 0) {
218 hdr->status = CHECK_CONDITION;
219 }
220
221 virtio_stl_p(vdev, &scsi->errors,
222 hdr->status | (hdr->msg_status << 8) |
223 (hdr->host_status << 16) | (hdr->driver_status << 24));
224 virtio_stl_p(vdev, &scsi->residual, hdr->resid);
225 virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr);
226 virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len);
227
228out:
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100229 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
Fam Zheng1dc936a2015-01-20 11:28:47 +0800230 virtio_blk_req_complete(req, status);
231 virtio_blk_free_request(req);
Paolo Bonzinib9e413d2017-02-13 14:52:32 +0100232 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
Fam Zheng1dc936a2015-01-20 11:28:47 +0800233 g_free(ioctl_req);
234}
235
236#endif
237
Stefan Hajnocziedaffd92016-06-21 13:13:13 +0100238static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
aliguori6e02c382008-12-04 19:52:44 +0000239{
Stefan Hajnocziedaffd92016-06-21 13:13:13 +0100240 VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq));
aliguori6e02c382008-12-04 19:52:44 +0000241
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200242 if (req) {
Stefan Hajnocziedaffd92016-06-21 13:13:13 +0100243 virtio_blk_init_request(s, vq, req);
aliguori6e02c382008-12-04 19:52:44 +0000244 }
aliguori6e02c382008-12-04 19:52:44 +0000245 return req;
246}
247
Fam Zheng75344fa2015-01-20 11:28:46 +0800248static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req)
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200249{
Stefan Weil47ce9ef2012-05-22 23:23:32 +0200250 int status = VIRTIO_BLK_S_OK;
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800251 struct virtio_scsi_inhdr *scsi = NULL;
Fam Zheng75344fa2015-01-20 11:28:46 +0800252 VirtIOBlock *blk = req->dev;
Anastasiia Rusakovabf4069f2019-03-07 18:19:25 +0200253 VirtIODevice *vdev = VIRTIO_DEVICE(blk);
254 VirtQueueElement *elem = &req->elem;
Rusty Russell783d1892014-06-24 19:43:44 +0200255
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800256#ifdef __linux__
257 int i;
Fam Zheng1dc936a2015-01-20 11:28:47 +0800258 VirtIOBlockIoctlReq *ioctl_req;
Fam Zhenga209f462015-02-17 17:55:53 +0800259 BlockAIOCB *acb;
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800260#endif
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200261
262 /*
263 * We require at least one output segment each for the virtio_blk_outhdr
264 * and the SCSI command block.
265 *
266 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
267 * and the sense buffer pointer in the input segments.
268 */
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800269 if (elem->out_num < 2 || elem->in_num < 3) {
270 status = VIRTIO_BLK_S_IOERR;
271 goto fail;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200272 }
273
274 /*
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200275 * The scsi inhdr is placed in the second-to-last input segment, just
276 * before the regular inhdr.
277 */
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800278 scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200279
Stefano Garzarellabbe8bd42019-02-21 11:33:06 +0100280 if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) {
Paolo Bonzinif34e73c2012-05-16 12:54:03 +0200281 status = VIRTIO_BLK_S_UNSUPP;
282 goto fail;
283 }
284
285 /*
286 * No support for bidirection commands yet.
287 */
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800288 if (elem->out_num > 2 && elem->in_num > 3) {
Paolo Bonzinif34e73c2012-05-16 12:54:03 +0200289 status = VIRTIO_BLK_S_UNSUPP;
290 goto fail;
291 }
292
293#ifdef __linux__
Fam Zheng1dc936a2015-01-20 11:28:47 +0800294 ioctl_req = g_new0(VirtIOBlockIoctlReq, 1);
295 ioctl_req->req = req;
296 ioctl_req->hdr.interface_id = 'S';
297 ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len;
298 ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base;
299 ioctl_req->hdr.dxfer_len = 0;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200300
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800301 if (elem->out_num > 2) {
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200302 /*
303 * If there are more than the minimally required 2 output segments
304 * there is write payload starting from the third iovec.
305 */
Fam Zheng1dc936a2015-01-20 11:28:47 +0800306 ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV;
307 ioctl_req->hdr.iovec_count = elem->out_num - 2;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200308
Fam Zheng1dc936a2015-01-20 11:28:47 +0800309 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
310 ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len;
311 }
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200312
Fam Zheng1dc936a2015-01-20 11:28:47 +0800313 ioctl_req->hdr.dxferp = elem->out_sg + 2;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200314
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800315 } else if (elem->in_num > 3) {
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200316 /*
317 * If we have more than 3 input segments the guest wants to actually
318 * read data.
319 */
Fam Zheng1dc936a2015-01-20 11:28:47 +0800320 ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV;
321 ioctl_req->hdr.iovec_count = elem->in_num - 3;
322 for (i = 0; i < ioctl_req->hdr.iovec_count; i++) {
323 ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len;
324 }
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200325
Fam Zheng1dc936a2015-01-20 11:28:47 +0800326 ioctl_req->hdr.dxferp = elem->in_sg;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200327 } else {
328 /*
329 * Some SCSI commands don't actually transfer any data.
330 */
Fam Zheng1dc936a2015-01-20 11:28:47 +0800331 ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200332 }
333
Fam Zheng1dc936a2015-01-20 11:28:47 +0800334 ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base;
335 ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200336
Fam Zhenga209f462015-02-17 17:55:53 +0800337 acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr,
338 virtio_blk_ioctl_complete, ioctl_req);
339 if (!acb) {
340 g_free(ioctl_req);
341 status = VIRTIO_BLK_S_UNSUPP;
342 goto fail;
343 }
Fam Zheng1dc936a2015-01-20 11:28:47 +0800344 return -EINPROGRESS;
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200345#else
Paolo Bonzinif34e73c2012-05-16 12:54:03 +0200346 abort();
347#endif
348
349fail:
350 /* Just put anything nonzero so that the ioctl fails in the guest. */
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800351 if (scsi) {
Rusty Russell783d1892014-06-24 19:43:44 +0200352 virtio_stl_p(vdev, &scsi->errors, 255);
Fam Zheng5a05cbe2014-05-22 16:22:42 +0800353 }
354 return status;
355}
356
357static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
358{
359 int status;
360
Fam Zheng75344fa2015-01-20 11:28:46 +0800361 status = virtio_blk_handle_scsi_req(req);
Fam Zheng1dc936a2015-01-20 11:28:47 +0800362 if (status != -EINPROGRESS) {
363 virtio_blk_req_complete(req, status);
364 virtio_blk_free_request(req);
365 }
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200366}
Christoph Hellwig1063b8b2009-04-27 10:29:14 +0200367
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400368static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
Peter Lieven95f71422015-02-02 14:52:21 +0100369 int start, int num_reqs, int niov)
aliguori869a5c62009-01-22 19:52:25 +0000370{
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400371 BlockBackend *blk = s->blk;
Peter Lieven95f71422015-02-02 14:52:21 +0100372 QEMUIOVector *qiov = &mrb->reqs[start]->qiov;
373 int64_t sector_num = mrb->reqs[start]->sector_num;
Peter Lieven95f71422015-02-02 14:52:21 +0100374 bool is_write = mrb->is_write;
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400375 BdrvRequestFlags flags = 0;
Christoph Hellwig87b245d2009-08-13 16:49:56 +0200376
Peter Lieven95f71422015-02-02 14:52:21 +0100377 if (num_reqs > 1) {
378 int i;
379 struct iovec *tmp_iov = qiov->iov;
380 int tmp_niov = qiov->niov;
381
382 /* mrb->reqs[start]->qiov was initialized from external so we can't
Eric Blakeb5772fd2016-05-06 10:26:33 -0600383 * modify it here. We need to initialize it locally and then add the
Peter Lieven95f71422015-02-02 14:52:21 +0100384 * external iovecs. */
385 qemu_iovec_init(qiov, niov);
386
387 for (i = 0; i < tmp_niov; i++) {
388 qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len);
389 }
390
391 for (i = start + 1; i < start + num_reqs; i++) {
392 qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
393 mrb->reqs[i]->qiov.size);
394 mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
Peter Lieven95f71422015-02-02 14:52:21 +0100395 }
Peter Lieven95f71422015-02-02 14:52:21 +0100396
Stefan Hajnoczia576cea2017-06-14 10:29:30 +0100397 trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev),
398 mrb, start, num_reqs,
Eric Blakeb5772fd2016-05-06 10:26:33 -0600399 sector_num << BDRV_SECTOR_BITS,
400 qiov->size, is_write);
Peter Lieven95f71422015-02-02 14:52:21 +0100401 block_acct_merge_done(blk_get_stats(blk),
402 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ,
403 num_reqs - 1);
404 }
405
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400406 if (blk_ram_registrar_ok(&s->blk_ram_registrar)) {
407 flags |= BDRV_REQ_REGISTERED_BUF;
408 }
409
Peter Lieven95f71422015-02-02 14:52:21 +0100410 if (is_write) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400411 blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov,
412 flags, virtio_blk_rw_complete,
413 mrb->reqs[start]);
Peter Lieven95f71422015-02-02 14:52:21 +0100414 } else {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400415 blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov,
416 flags, virtio_blk_rw_complete,
417 mrb->reqs[start]);
Peter Lieven95f71422015-02-02 14:52:21 +0100418 }
419}
420
421static int multireq_compare(const void *a, const void *b)
422{
423 const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a,
424 *req2 = *(VirtIOBlockReq **)b;
425
426 /*
427 * Note that we can't simply subtract sector_num1 from sector_num2
428 * here as that could overflow the return value.
429 */
430 if (req1->sector_num > req2->sector_num) {
431 return 1;
432 } else if (req1->sector_num < req2->sector_num) {
433 return -1;
434 } else {
435 return 0;
436 }
437}
438
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400439static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb)
Peter Lieven95f71422015-02-02 14:52:21 +0100440{
441 int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0;
Eric Blake5def6b82016-06-23 16:37:19 -0600442 uint32_t max_transfer;
Peter Lieven95f71422015-02-02 14:52:21 +0100443 int64_t sector_num = 0;
444
445 if (mrb->num_reqs == 1) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400446 submit_requests(s, mrb, 0, 1, -1);
Peter Lieven95f71422015-02-02 14:52:21 +0100447 mrb->num_reqs = 0;
Christoph Hellwigc20fd872010-06-08 18:26:07 +0200448 return;
449 }
450
Eric Blake5def6b82016-06-23 16:37:19 -0600451 max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk);
Peter Lieven95f71422015-02-02 14:52:21 +0100452
453 qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs),
454 &multireq_compare);
455
456 for (i = 0; i < mrb->num_reqs; i++) {
457 VirtIOBlockReq *req = mrb->reqs[i];
458 if (num_reqs > 0) {
Gonglei49cffbc2015-11-11 09:59:26 +0800459 /*
460 * NOTE: We cannot merge the requests in below situations:
461 * 1. requests are not sequential
462 * 2. merge would exceed maximum number of IOVs
463 * 3. merge would exceed maximum transfer length of backend device
464 */
465 if (sector_num + nb_sectors != req->sector_num ||
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400466 niov > blk_get_max_iov(s->blk) - req->qiov.niov ||
Eric Blake5def6b82016-06-23 16:37:19 -0600467 req->qiov.size > max_transfer ||
468 nb_sectors > (max_transfer -
469 req->qiov.size) / BDRV_SECTOR_SIZE) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400470 submit_requests(s, mrb, start, num_reqs, niov);
Peter Lieven95f71422015-02-02 14:52:21 +0100471 num_reqs = 0;
Kevin Wolf91553dc2009-09-09 17:53:38 +0200472 }
473 }
Peter Lieven95f71422015-02-02 14:52:21 +0100474
475 if (num_reqs == 0) {
476 sector_num = req->sector_num;
477 nb_sectors = niov = 0;
478 start = i;
479 }
480
481 nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE;
482 niov += req->qiov.niov;
483 num_reqs++;
Christoph Hellwig87b245d2009-08-13 16:49:56 +0200484 }
Christoph Hellwigc20fd872010-06-08 18:26:07 +0200485
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400486 submit_requests(s, mrb, start, num_reqs, niov);
Peter Lieven95f71422015-02-02 14:52:21 +0100487 mrb->num_reqs = 0;
aliguorid28a1b62009-03-28 17:46:14 +0000488}
aliguori869a5c62009-01-22 19:52:25 +0000489
Christoph Hellwigc20fd872010-06-08 18:26:07 +0200490static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
Christoph Hellwigaa659be2009-09-04 19:02:23 +0200491{
Anastasiia Rusakovabf4069f2019-03-07 18:19:25 +0200492 VirtIOBlock *s = req->dev;
493
494 block_acct_start(blk_get_stats(s->blk), &req->acct, 0,
Benoît Canet5366d0c2014-09-05 15:46:18 +0200495 BLOCK_ACCT_FLUSH);
Christoph Hellwiga597e792011-08-25 08:26:01 +0200496
Christoph Hellwig618fbb82010-05-19 12:40:09 +0200497 /*
498 * Make sure all outstanding writes are posted to the backing device.
499 */
Peter Lieven95f71422015-02-02 14:52:21 +0100500 if (mrb->is_write && mrb->num_reqs > 0) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -0400501 virtio_blk_submit_multireq(s, mrb);
Peter Lieven95f71422015-02-02 14:52:21 +0100502 }
Anastasiia Rusakovabf4069f2019-03-07 18:19:25 +0200503 blk_aio_flush(s->blk, virtio_blk_flush_complete, req);
Christoph Hellwigaa659be2009-09-04 19:02:23 +0200504}
505
Markus Armbrusterd0e14372014-07-09 19:07:29 +0200506static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
507 uint64_t sector, size_t size)
508{
Markus Armbruster3c2daac2014-07-09 19:07:31 +0200509 uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
510 uint64_t total_sectors;
511
Peter Lieven75af1f32015-02-06 11:54:11 +0100512 if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) {
Peter Lieven95f71422015-02-02 14:52:21 +0100513 return false;
514 }
Markus Armbrusterd0e14372014-07-09 19:07:29 +0200515 if (sector & dev->sector_mask) {
516 return false;
517 }
Markus Armbruster2a303072014-10-07 13:59:17 +0200518 if (size % dev->conf.conf.logical_block_size) {
Markus Armbrusterd0e14372014-07-09 19:07:29 +0200519 return false;
520 }
Markus Armbruster4be74632014-10-07 13:59:18 +0200521 blk_get_geometry(dev->blk, &total_sectors);
Markus Armbruster3c2daac2014-07-09 19:07:31 +0200522 if (sector > total_sectors || nb_sectors > total_sectors - sector) {
523 return false;
524 }
Markus Armbrusterd0e14372014-07-09 19:07:29 +0200525 return true;
526}
527
Stefano Garzarella37b06f82019-02-21 11:33:10 +0100528static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req,
529 struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes)
530{
531 VirtIOBlock *s = req->dev;
532 VirtIODevice *vdev = VIRTIO_DEVICE(s);
533 uint64_t sector;
534 uint32_t num_sectors, flags, max_sectors;
535 uint8_t err_status;
536 int bytes;
537
538 sector = virtio_ldq_p(vdev, &dwz_hdr->sector);
539 num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors);
540 flags = virtio_ldl_p(vdev, &dwz_hdr->flags);
541 max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors :
542 s->conf.max_discard_sectors;
543
544 /*
545 * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check
546 * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in
547 * the integer variable.
548 */
549 if (unlikely(num_sectors > max_sectors)) {
550 err_status = VIRTIO_BLK_S_IOERR;
551 goto err;
552 }
553
554 bytes = num_sectors << BDRV_SECTOR_BITS;
555
556 if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) {
557 err_status = VIRTIO_BLK_S_IOERR;
558 goto err;
559 }
560
561 /*
562 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard
563 * and write zeroes commands if any unknown flag is set.
564 */
565 if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
566 err_status = VIRTIO_BLK_S_UNSUPP;
567 goto err;
568 }
569
570 if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */
571 int blk_aio_flags = 0;
572
573 if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) {
574 blk_aio_flags |= BDRV_REQ_MAY_UNMAP;
575 }
576
577 block_acct_start(blk_get_stats(s->blk), &req->acct, bytes,
578 BLOCK_ACCT_WRITE);
579
580 blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS,
581 bytes, blk_aio_flags,
582 virtio_blk_discard_write_zeroes_complete, req);
583 } else { /* VIRTIO_BLK_T_DISCARD */
584 /*
585 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for
586 * discard commands if the unmap flag is set.
587 */
588 if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) {
589 err_status = VIRTIO_BLK_S_UNSUPP;
590 goto err;
591 }
592
593 blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes,
594 virtio_blk_discard_write_zeroes_complete, req);
595 }
596
597 return VIRTIO_BLK_S_OK;
598
599err:
600 if (is_write_zeroes) {
601 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
602 }
603 return err_status;
604}
605
Sam Li4f736652023-05-08 13:19:13 +0800606typedef struct ZoneCmdData {
607 VirtIOBlockReq *req;
608 struct iovec *in_iov;
609 unsigned in_num;
610 union {
611 struct {
612 unsigned int nr_zones;
613 BlockZoneDescriptor *zones;
614 } zone_report_data;
615 struct {
616 int64_t offset;
617 } zone_append_data;
618 };
619} ZoneCmdData;
620
621/*
622 * check zoned_request: error checking before issuing requests. If all checks
623 * passed, return true.
624 * append: true if only zone append requests issued.
625 */
626static bool check_zoned_request(VirtIOBlock *s, int64_t offset, int64_t len,
627 bool append, uint8_t *status) {
628 BlockDriverState *bs = blk_bs(s->blk);
629 int index;
630
631 if (!virtio_has_feature(s->host_features, VIRTIO_BLK_F_ZONED)) {
632 *status = VIRTIO_BLK_S_UNSUPP;
633 return false;
634 }
635
636 if (offset < 0 || len < 0 || len > (bs->total_sectors << BDRV_SECTOR_BITS)
637 || offset > (bs->total_sectors << BDRV_SECTOR_BITS) - len) {
638 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
639 return false;
640 }
641
642 if (append) {
643 if (bs->bl.write_granularity) {
644 if ((offset % bs->bl.write_granularity) != 0) {
645 *status = VIRTIO_BLK_S_ZONE_UNALIGNED_WP;
646 return false;
647 }
648 }
649
650 index = offset / bs->bl.zone_size;
651 if (BDRV_ZT_IS_CONV(bs->wps->wp[index])) {
652 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
653 return false;
654 }
655
656 if (len / 512 > bs->bl.max_append_sectors) {
657 if (bs->bl.max_append_sectors == 0) {
658 *status = VIRTIO_BLK_S_UNSUPP;
659 } else {
660 *status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
661 }
662 return false;
663 }
664 }
665 return true;
666}
667
668static void virtio_blk_zone_report_complete(void *opaque, int ret)
669{
670 ZoneCmdData *data = opaque;
671 VirtIOBlockReq *req = data->req;
672 VirtIOBlock *s = req->dev;
673 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
674 struct iovec *in_iov = data->in_iov;
675 unsigned in_num = data->in_num;
676 int64_t zrp_size, n, j = 0;
677 int64_t nz = data->zone_report_data.nr_zones;
678 int8_t err_status = VIRTIO_BLK_S_OK;
679
Sam Li4e92acf2023-05-08 13:19:15 +0800680 trace_virtio_blk_zone_report_complete(vdev, req, nz, ret);
Sam Li4f736652023-05-08 13:19:13 +0800681 if (ret) {
682 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
683 goto out;
684 }
685
686 struct virtio_blk_zone_report zrp_hdr = (struct virtio_blk_zone_report) {
687 .nr_zones = cpu_to_le64(nz),
688 };
689 zrp_size = sizeof(struct virtio_blk_zone_report)
690 + sizeof(struct virtio_blk_zone_descriptor) * nz;
691 n = iov_from_buf(in_iov, in_num, 0, &zrp_hdr, sizeof(zrp_hdr));
692 if (n != sizeof(zrp_hdr)) {
693 virtio_error(vdev, "Driver provided input buffer that is too small!");
694 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
695 goto out;
696 }
697
698 for (size_t i = sizeof(zrp_hdr); i < zrp_size;
699 i += sizeof(struct virtio_blk_zone_descriptor), ++j) {
700 struct virtio_blk_zone_descriptor desc =
701 (struct virtio_blk_zone_descriptor) {
702 .z_start = cpu_to_le64(data->zone_report_data.zones[j].start
703 >> BDRV_SECTOR_BITS),
704 .z_cap = cpu_to_le64(data->zone_report_data.zones[j].cap
705 >> BDRV_SECTOR_BITS),
706 .z_wp = cpu_to_le64(data->zone_report_data.zones[j].wp
707 >> BDRV_SECTOR_BITS),
708 };
709
710 switch (data->zone_report_data.zones[j].type) {
711 case BLK_ZT_CONV:
712 desc.z_type = VIRTIO_BLK_ZT_CONV;
713 break;
714 case BLK_ZT_SWR:
715 desc.z_type = VIRTIO_BLK_ZT_SWR;
716 break;
717 case BLK_ZT_SWP:
718 desc.z_type = VIRTIO_BLK_ZT_SWP;
719 break;
720 default:
721 g_assert_not_reached();
722 }
723
724 switch (data->zone_report_data.zones[j].state) {
725 case BLK_ZS_RDONLY:
726 desc.z_state = VIRTIO_BLK_ZS_RDONLY;
727 break;
728 case BLK_ZS_OFFLINE:
729 desc.z_state = VIRTIO_BLK_ZS_OFFLINE;
730 break;
731 case BLK_ZS_EMPTY:
732 desc.z_state = VIRTIO_BLK_ZS_EMPTY;
733 break;
734 case BLK_ZS_CLOSED:
735 desc.z_state = VIRTIO_BLK_ZS_CLOSED;
736 break;
737 case BLK_ZS_FULL:
738 desc.z_state = VIRTIO_BLK_ZS_FULL;
739 break;
740 case BLK_ZS_EOPEN:
741 desc.z_state = VIRTIO_BLK_ZS_EOPEN;
742 break;
743 case BLK_ZS_IOPEN:
744 desc.z_state = VIRTIO_BLK_ZS_IOPEN;
745 break;
746 case BLK_ZS_NOT_WP:
747 desc.z_state = VIRTIO_BLK_ZS_NOT_WP;
748 break;
749 default:
750 g_assert_not_reached();
751 }
752
753 /* TODO: it takes O(n^2) time complexity. Optimizations required. */
754 n = iov_from_buf(in_iov, in_num, i, &desc, sizeof(desc));
755 if (n != sizeof(desc)) {
756 virtio_error(vdev, "Driver provided input buffer "
757 "for descriptors that is too small!");
758 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
759 }
760 }
761
762out:
763 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
764 virtio_blk_req_complete(req, err_status);
765 virtio_blk_free_request(req);
766 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
767 g_free(data->zone_report_data.zones);
768 g_free(data);
769}
770
771static void virtio_blk_handle_zone_report(VirtIOBlockReq *req,
772 struct iovec *in_iov,
773 unsigned in_num)
774{
775 VirtIOBlock *s = req->dev;
776 VirtIODevice *vdev = VIRTIO_DEVICE(s);
777 unsigned int nr_zones;
778 ZoneCmdData *data;
779 int64_t zone_size, offset;
780 uint8_t err_status;
781
782 if (req->in_len < sizeof(struct virtio_blk_inhdr) +
783 sizeof(struct virtio_blk_zone_report) +
784 sizeof(struct virtio_blk_zone_descriptor)) {
785 virtio_error(vdev, "in buffer too small for zone report");
786 return;
787 }
788
789 /* start byte offset of the zone report */
790 offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
791 if (!check_zoned_request(s, offset, 0, false, &err_status)) {
792 goto out;
793 }
794 nr_zones = (req->in_len - sizeof(struct virtio_blk_inhdr) -
795 sizeof(struct virtio_blk_zone_report)) /
796 sizeof(struct virtio_blk_zone_descriptor);
Sam Li4e92acf2023-05-08 13:19:15 +0800797 trace_virtio_blk_handle_zone_report(vdev, req,
798 offset >> BDRV_SECTOR_BITS, nr_zones);
Sam Li4f736652023-05-08 13:19:13 +0800799
800 zone_size = sizeof(BlockZoneDescriptor) * nr_zones;
801 data = g_malloc(sizeof(ZoneCmdData));
802 data->req = req;
803 data->in_iov = in_iov;
804 data->in_num = in_num;
805 data->zone_report_data.nr_zones = nr_zones;
806 data->zone_report_data.zones = g_malloc(zone_size),
807
808 blk_aio_zone_report(s->blk, offset, &data->zone_report_data.nr_zones,
809 data->zone_report_data.zones,
810 virtio_blk_zone_report_complete, data);
811 return;
812out:
813 virtio_blk_req_complete(req, err_status);
814 virtio_blk_free_request(req);
815}
816
817static void virtio_blk_zone_mgmt_complete(void *opaque, int ret)
818{
819 VirtIOBlockReq *req = opaque;
820 VirtIOBlock *s = req->dev;
Sam Li4e92acf2023-05-08 13:19:15 +0800821 VirtIODevice *vdev = VIRTIO_DEVICE(s);
Sam Li4f736652023-05-08 13:19:13 +0800822 int8_t err_status = VIRTIO_BLK_S_OK;
Sam Li4e92acf2023-05-08 13:19:15 +0800823 trace_virtio_blk_zone_mgmt_complete(vdev, req,ret);
Sam Li4f736652023-05-08 13:19:13 +0800824
825 if (ret) {
826 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
827 }
828
829 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
830 virtio_blk_req_complete(req, err_status);
831 virtio_blk_free_request(req);
832 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
833}
834
835static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq *req, BlockZoneOp op)
836{
837 VirtIOBlock *s = req->dev;
838 VirtIODevice *vdev = VIRTIO_DEVICE(s);
839 BlockDriverState *bs = blk_bs(s->blk);
840 int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
841 uint64_t len;
842 uint64_t capacity = bs->total_sectors << BDRV_SECTOR_BITS;
843 uint8_t err_status = VIRTIO_BLK_S_OK;
844
845 uint32_t type = virtio_ldl_p(vdev, &req->out.type);
846 if (type == VIRTIO_BLK_T_ZONE_RESET_ALL) {
847 /* Entire drive capacity */
848 offset = 0;
849 len = capacity;
Sam Li4e92acf2023-05-08 13:19:15 +0800850 trace_virtio_blk_handle_zone_reset_all(vdev, req, 0,
851 bs->total_sectors);
Sam Li4f736652023-05-08 13:19:13 +0800852 } else {
853 if (bs->bl.zone_size > capacity - offset) {
854 /* The zoned device allows the last smaller zone. */
855 len = capacity - bs->bl.zone_size * (bs->bl.nr_zones - 1);
856 } else {
857 len = bs->bl.zone_size;
858 }
Sam Li4e92acf2023-05-08 13:19:15 +0800859 trace_virtio_blk_handle_zone_mgmt(vdev, req, op,
860 offset >> BDRV_SECTOR_BITS,
861 len >> BDRV_SECTOR_BITS);
Sam Li4f736652023-05-08 13:19:13 +0800862 }
863
864 if (!check_zoned_request(s, offset, len, false, &err_status)) {
865 goto out;
866 }
867
868 blk_aio_zone_mgmt(s->blk, op, offset, len,
869 virtio_blk_zone_mgmt_complete, req);
870
871 return 0;
872out:
873 virtio_blk_req_complete(req, err_status);
874 virtio_blk_free_request(req);
875 return err_status;
876}
877
878static void virtio_blk_zone_append_complete(void *opaque, int ret)
879{
880 ZoneCmdData *data = opaque;
881 VirtIOBlockReq *req = data->req;
882 VirtIOBlock *s = req->dev;
883 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
884 int64_t append_sector, n;
885 uint8_t err_status = VIRTIO_BLK_S_OK;
886
887 if (ret) {
888 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
889 goto out;
890 }
891
892 virtio_stq_p(vdev, &append_sector,
893 data->zone_append_data.offset >> BDRV_SECTOR_BITS);
894 n = iov_from_buf(data->in_iov, data->in_num, 0, &append_sector,
895 sizeof(append_sector));
896 if (n != sizeof(append_sector)) {
897 virtio_error(vdev, "Driver provided input buffer less than size of "
898 "append_sector");
899 err_status = VIRTIO_BLK_S_ZONE_INVALID_CMD;
900 goto out;
901 }
Sam Li4e92acf2023-05-08 13:19:15 +0800902 trace_virtio_blk_zone_append_complete(vdev, req, append_sector, ret);
Sam Li4f736652023-05-08 13:19:13 +0800903
904out:
905 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
906 virtio_blk_req_complete(req, err_status);
907 virtio_blk_free_request(req);
908 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
909 g_free(data);
910}
911
912static int virtio_blk_handle_zone_append(VirtIOBlockReq *req,
913 struct iovec *out_iov,
914 struct iovec *in_iov,
915 uint64_t out_num,
916 unsigned in_num) {
917 VirtIOBlock *s = req->dev;
918 VirtIODevice *vdev = VIRTIO_DEVICE(s);
919 uint8_t err_status = VIRTIO_BLK_S_OK;
920
921 int64_t offset = virtio_ldq_p(vdev, &req->out.sector) << BDRV_SECTOR_BITS;
922 int64_t len = iov_size(out_iov, out_num);
923
Sam Li4e92acf2023-05-08 13:19:15 +0800924 trace_virtio_blk_handle_zone_append(vdev, req, offset >> BDRV_SECTOR_BITS);
Sam Li4f736652023-05-08 13:19:13 +0800925 if (!check_zoned_request(s, offset, len, true, &err_status)) {
926 goto out;
927 }
928
929 ZoneCmdData *data = g_malloc(sizeof(ZoneCmdData));
930 data->req = req;
931 data->in_iov = in_iov;
932 data->in_num = in_num;
933 data->zone_append_data.offset = offset;
934 qemu_iovec_init_external(&req->qiov, out_iov, out_num);
Sam Li52eb76f2023-05-08 13:19:14 +0800935
936 block_acct_start(blk_get_stats(s->blk), &req->acct, len,
937 BLOCK_ACCT_ZONE_APPEND);
938
Sam Li4f736652023-05-08 13:19:13 +0800939 blk_aio_zone_append(s->blk, &data->zone_append_data.offset, &req->qiov, 0,
940 virtio_blk_zone_append_complete, data);
941 return 0;
942
943out:
944 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
945 virtio_blk_req_complete(req, err_status);
946 virtio_blk_free_request(req);
947 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
948 return err_status;
949}
950
Greg Kurz20ea6862016-09-30 17:13:07 +0200951static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
Kevin Wolfbc6694d2010-01-27 13:12:34 +0100952{
Aurelien Jarno92e3c2a2011-01-25 11:55:14 +0100953 uint32_t type;
Stefan Hajnoczif897bf72014-07-09 10:05:49 +0200954 struct iovec *in_iov = req->elem.in_sg;
Dongli Zhang5636da72018-11-07 00:09:16 +0800955 struct iovec *out_iov = req->elem.out_sg;
Stefan Hajnoczif897bf72014-07-09 10:05:49 +0200956 unsigned in_num = req->elem.in_num;
957 unsigned out_num = req->elem.out_num;
Greg Kurz20ea6862016-09-30 17:13:07 +0200958 VirtIOBlock *s = req->dev;
959 VirtIODevice *vdev = VIRTIO_DEVICE(s);
Aurelien Jarno92e3c2a2011-01-25 11:55:14 +0100960
Stefan Hajnoczif897bf72014-07-09 10:05:49 +0200961 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
Greg Kurz20ea6862016-09-30 17:13:07 +0200962 virtio_error(vdev, "virtio-blk missing headers");
963 return -1;
Kevin Wolfbc6694d2010-01-27 13:12:34 +0100964 }
965
Dongli Zhang5636da72018-11-07 00:09:16 +0800966 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out,
Fam Zheng827805a2014-06-11 12:11:48 +0800967 sizeof(req->out)) != sizeof(req->out))) {
Greg Kurz20ea6862016-09-30 17:13:07 +0200968 virtio_error(vdev, "virtio-blk request outhdr too short");
969 return -1;
Fam Zheng827805a2014-06-11 12:11:48 +0800970 }
Fam Zhengee17e842014-06-11 12:11:50 +0800971
Stefan Hajnoczi7bd04a02020-09-17 10:44:54 +0100972 iov_discard_front_undoable(&out_iov, &out_num, sizeof(req->out),
973 &req->outhdr_undo);
Fam Zhengee17e842014-06-11 12:11:50 +0800974
Gonglei12048542015-06-24 17:29:24 +0800975 if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) {
Greg Kurz20ea6862016-09-30 17:13:07 +0200976 virtio_error(vdev, "virtio-blk request inhdr too short");
Stefan Hajnoczi7bd04a02020-09-17 10:44:54 +0100977 iov_discard_undo(&req->outhdr_undo);
Greg Kurz20ea6862016-09-30 17:13:07 +0200978 return -1;
Fam Zhengee17e842014-06-11 12:11:50 +0800979 }
980
Paolo Bonzini2a6cdd62015-04-02 19:50:44 +0200981 /* We always touch the last byte, so just see how big in_iov is. */
982 req->in_len = iov_size(in_iov, in_num);
Fam Zhengee17e842014-06-11 12:11:50 +0800983 req->in = (void *)in_iov[in_num - 1].iov_base
984 + in_iov[in_num - 1].iov_len
985 - sizeof(struct virtio_blk_inhdr);
Stefan Hajnoczi7bd04a02020-09-17 10:44:54 +0100986 iov_discard_back_undoable(in_iov, &in_num, sizeof(struct virtio_blk_inhdr),
987 &req->inhdr_undo);
Kevin Wolfbc6694d2010-01-27 13:12:34 +0100988
Stefano Garzarella9a6719d2019-02-08 15:23:47 +0100989 type = virtio_ldl_p(vdev, &req->out.type);
Aurelien Jarno92e3c2a2011-01-25 11:55:14 +0100990
Peter Lieven95f71422015-02-02 14:52:21 +0100991 /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER
Stefan Weil631b22e2015-04-09 20:32:39 +0200992 * is an optional flag. Although a guest should not send this flag if
Peter Lieven95f71422015-02-02 14:52:21 +0100993 * not negotiated we ignored it in the past. So keep ignoring it. */
994 switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) {
995 case VIRTIO_BLK_T_IN:
996 {
997 bool is_write = type & VIRTIO_BLK_T_OUT;
Stefano Garzarella9a6719d2019-02-08 15:23:47 +0100998 req->sector_num = virtio_ldq_p(vdev, &req->out.sector);
Peter Lieven95f71422015-02-02 14:52:21 +0100999
1000 if (is_write) {
Dongli Zhang5636da72018-11-07 00:09:16 +08001001 qemu_iovec_init_external(&req->qiov, out_iov, out_num);
Stefan Hajnoczia576cea2017-06-14 10:29:30 +01001002 trace_virtio_blk_handle_write(vdev, req, req->sector_num,
Peter Lieven95f71422015-02-02 14:52:21 +01001003 req->qiov.size / BDRV_SECTOR_SIZE);
1004 } else {
1005 qemu_iovec_init_external(&req->qiov, in_iov, in_num);
Stefan Hajnoczia576cea2017-06-14 10:29:30 +01001006 trace_virtio_blk_handle_read(vdev, req, req->sector_num,
Peter Lieven95f71422015-02-02 14:52:21 +01001007 req->qiov.size / BDRV_SECTOR_SIZE);
1008 }
1009
Stefano Garzarella9a6719d2019-02-08 15:23:47 +01001010 if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) {
Peter Lieven95f71422015-02-02 14:52:21 +01001011 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
Stefano Garzarella9a6719d2019-02-08 15:23:47 +01001012 block_acct_invalid(blk_get_stats(s->blk),
Alberto Garcia01762e02015-10-28 17:33:12 +02001013 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
Peter Lieven95f71422015-02-02 14:52:21 +01001014 virtio_blk_free_request(req);
Greg Kurz20ea6862016-09-30 17:13:07 +02001015 return 0;
Peter Lieven95f71422015-02-02 14:52:21 +01001016 }
1017
Stefano Garzarella9a6719d2019-02-08 15:23:47 +01001018 block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size,
Peter Lieven95f71422015-02-02 14:52:21 +01001019 is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
1020
1021 /* merge would exceed maximum number of requests or IO direction
1022 * changes */
1023 if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS ||
Peter Lievenc99495a2015-02-02 14:52:22 +01001024 is_write != mrb->is_write ||
Stefano Garzarella9a6719d2019-02-08 15:23:47 +01001025 !s->conf.request_merging)) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -04001026 virtio_blk_submit_multireq(s, mrb);
Peter Lieven95f71422015-02-02 14:52:21 +01001027 }
1028
1029 assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS);
1030 mrb->reqs[mrb->num_reqs++] = req;
1031 mrb->is_write = is_write;
1032 break;
1033 }
1034 case VIRTIO_BLK_T_FLUSH:
Christoph Hellwigc20fd872010-06-08 18:26:07 +02001035 virtio_blk_handle_flush(req, mrb);
Peter Lieven95f71422015-02-02 14:52:21 +01001036 break;
Sam Li4f736652023-05-08 13:19:13 +08001037 case VIRTIO_BLK_T_ZONE_REPORT:
1038 virtio_blk_handle_zone_report(req, in_iov, in_num);
1039 break;
1040 case VIRTIO_BLK_T_ZONE_OPEN:
1041 virtio_blk_handle_zone_mgmt(req, BLK_ZO_OPEN);
1042 break;
1043 case VIRTIO_BLK_T_ZONE_CLOSE:
1044 virtio_blk_handle_zone_mgmt(req, BLK_ZO_CLOSE);
1045 break;
1046 case VIRTIO_BLK_T_ZONE_FINISH:
1047 virtio_blk_handle_zone_mgmt(req, BLK_ZO_FINISH);
1048 break;
1049 case VIRTIO_BLK_T_ZONE_RESET:
1050 virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1051 break;
1052 case VIRTIO_BLK_T_ZONE_RESET_ALL:
1053 virtio_blk_handle_zone_mgmt(req, BLK_ZO_RESET);
1054 break;
Peter Lieven95f71422015-02-02 14:52:21 +01001055 case VIRTIO_BLK_T_SCSI_CMD:
Kevin Wolfbc6694d2010-01-27 13:12:34 +01001056 virtio_blk_handle_scsi(req);
Peter Lieven95f71422015-02-02 14:52:21 +01001057 break;
1058 case VIRTIO_BLK_T_GET_ID:
1059 {
Markus Armbrustera8686a92011-06-20 11:35:18 +02001060 /*
1061 * NB: per existing s/n string convention the string is
1062 * terminated by '\0' only when shorter than buffer.
1063 */
Markus Armbruster2a303072014-10-07 13:59:17 +02001064 const char *serial = s->conf.serial ? s->conf.serial : "";
Marc María83ceea2014-08-12 13:41:51 +02001065 size_t size = MIN(strlen(serial) + 1,
1066 MIN(iov_size(in_iov, in_num),
1067 VIRTIO_BLK_ID_BYTES));
1068 iov_from_buf(in_iov, in_num, 0, serial, size);
john cooper2930b312010-07-02 13:44:25 -04001069 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
Fam Zheng671ec3f2014-06-11 12:11:43 +08001070 virtio_blk_free_request(req);
Peter Lieven95f71422015-02-02 14:52:21 +01001071 break;
1072 }
Sam Li4f736652023-05-08 13:19:13 +08001073 case VIRTIO_BLK_T_ZONE_APPEND & ~VIRTIO_BLK_T_OUT:
1074 /*
1075 * Passing out_iov/out_num and in_iov/in_num is not safe
1076 * to access req->elem.out_sg directly because it may be
1077 * modified by virtio_blk_handle_request().
1078 */
1079 virtio_blk_handle_zone_append(req, out_iov, in_iov, out_num, in_num);
1080 break;
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001081 /*
1082 * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with
1083 * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement,
1084 * so we must mask it for these requests, then we will check if it is set.
1085 */
1086 case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT:
1087 case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT:
1088 {
1089 struct virtio_blk_discard_write_zeroes dwz_hdr;
1090 size_t out_len = iov_size(out_iov, out_num);
1091 bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) ==
1092 VIRTIO_BLK_T_WRITE_ZEROES;
1093 uint8_t err_status;
1094
1095 /*
1096 * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains
1097 * more than one segment.
1098 */
1099 if (unlikely(!(type & VIRTIO_BLK_T_OUT) ||
1100 out_len > sizeof(dwz_hdr))) {
1101 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
1102 virtio_blk_free_request(req);
1103 return 0;
1104 }
1105
1106 if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr,
1107 sizeof(dwz_hdr)) != sizeof(dwz_hdr))) {
Stefan Hajnoczi7bd04a02020-09-17 10:44:54 +01001108 iov_discard_undo(&req->inhdr_undo);
1109 iov_discard_undo(&req->outhdr_undo);
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001110 virtio_error(vdev, "virtio-blk discard/write_zeroes header"
1111 " too short");
1112 return -1;
1113 }
1114
1115 err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr,
1116 is_write_zeroes);
1117 if (err_status != VIRTIO_BLK_S_OK) {
1118 virtio_blk_req_complete(req, err_status);
1119 virtio_blk_free_request(req);
1120 }
1121
1122 break;
1123 }
Peter Lieven95f71422015-02-02 14:52:21 +01001124 default:
Alexey Zaytsev9e72c452012-12-13 09:03:43 +02001125 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
Fam Zheng671ec3f2014-06-11 12:11:43 +08001126 virtio_blk_free_request(req);
Kevin Wolfbc6694d2010-01-27 13:12:34 +01001127 }
Greg Kurz20ea6862016-09-30 17:13:07 +02001128 return 0;
Kevin Wolfbc6694d2010-01-27 13:12:34 +01001129}
1130
Stefan Hajnoczi186b9692021-12-07 13:23:33 +00001131void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
aliguori6e02c382008-12-04 19:52:44 +00001132{
aliguori6e02c382008-12-04 19:52:44 +00001133 VirtIOBlockReq *req;
Peter Lieven95f71422015-02-02 14:52:21 +01001134 MultiReqBuffer mrb = {};
Stefan Hajnoczid0435bc2019-12-09 21:09:57 +00001135 bool suppress_notifications = virtio_queue_get_notification(vq);
aliguori6e02c382008-12-04 19:52:44 +00001136
Paolo Bonzini9d456652017-02-13 14:52:30 +01001137 aio_context_acquire(blk_get_aio_context(s->blk));
Stefan Hajnocziccee48a2023-09-13 16:00:42 -04001138 defer_call_begin();
Stefan Hajnoczifc735482015-07-20 16:54:16 +01001139
Stefan Hajnoczi9ef9d402016-12-01 19:26:46 +00001140 do {
Stefan Hajnoczid0435bc2019-12-09 21:09:57 +00001141 if (suppress_notifications) {
1142 virtio_queue_set_notification(vq, 0);
1143 }
Stefan Hajnoczi9ef9d402016-12-01 19:26:46 +00001144
1145 while ((req = virtio_blk_get_request(s, vq))) {
1146 if (virtio_blk_handle_request(req, &mrb)) {
1147 virtqueue_detach_element(req->vq, &req->elem, 0);
1148 virtio_blk_free_request(req);
1149 break;
1150 }
Greg Kurz20ea6862016-09-30 17:13:07 +02001151 }
Stefan Hajnoczi9ef9d402016-12-01 19:26:46 +00001152
Stefan Hajnoczid0435bc2019-12-09 21:09:57 +00001153 if (suppress_notifications) {
1154 virtio_queue_set_notification(vq, 1);
1155 }
Stefan Hajnoczi9ef9d402016-12-01 19:26:46 +00001156 } while (!virtio_queue_empty(vq));
Kevin Wolf91553dc2009-09-09 17:53:38 +02001157
Peter Lieven95f71422015-02-02 14:52:21 +01001158 if (mrb.num_reqs) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -04001159 virtio_blk_submit_multireq(s, &mrb);
Peter Lieven95f71422015-02-02 14:52:21 +01001160 }
Stefan Hajnoczifc735482015-07-20 16:54:16 +01001161
Stefan Hajnocziccee48a2023-09-13 16:00:42 -04001162 defer_call_end();
Paolo Bonzini9d456652017-02-13 14:52:30 +01001163 aio_context_release(blk_get_aio_context(s->blk));
aliguori6e02c382008-12-04 19:52:44 +00001164}
1165
Michael S. Tsirkin8a2fad52016-04-06 12:16:26 +02001166static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
1167{
1168 VirtIOBlock *s = (VirtIOBlock *)vdev;
1169
Stefan Hajnoczi186b9692021-12-07 13:23:33 +00001170 if (s->dataplane && !s->dataplane_started) {
Michael S. Tsirkin8a2fad52016-04-06 12:16:26 +02001171 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
1172 * dataplane here instead of waiting for .set_status().
1173 */
Paolo Bonzini9ffe3372016-10-21 22:48:09 +02001174 virtio_device_start_ioeventfd(vdev);
Michael S. Tsirkin8a2fad52016-04-06 12:16:26 +02001175 if (!s->dataplane_disabled) {
1176 return;
1177 }
1178 }
Stefan Hajnoczi186b9692021-12-07 13:23:33 +00001179 virtio_blk_handle_vq(s, vq);
Michael S. Tsirkin8a2fad52016-04-06 12:16:26 +02001180}
1181
Stefan Hajnoczia937f8e2022-11-02 14:23:37 -04001182static void virtio_blk_dma_restart_bh(void *opaque)
aliguori869a5c62009-01-22 19:52:25 +00001183{
Stefan Hajnoczia937f8e2022-11-02 14:23:37 -04001184 VirtIOBlock *s = opaque;
1185
aliguori869a5c62009-01-22 19:52:25 +00001186 VirtIOBlockReq *req = s->rq;
Peter Lieven95f71422015-02-02 14:52:21 +01001187 MultiReqBuffer mrb = {};
aliguori869a5c62009-01-22 19:52:25 +00001188
aliguori869a5c62009-01-22 19:52:25 +00001189 s->rq = NULL;
1190
Paolo Bonzini19196312017-02-13 14:52:31 +01001191 aio_context_acquire(blk_get_aio_context(s->conf.conf.blk));
aliguori869a5c62009-01-22 19:52:25 +00001192 while (req) {
zhanghailiang1bdb1762014-08-18 15:42:50 +08001193 VirtIOBlockReq *next = req->next;
Greg Kurz20ea6862016-09-30 17:13:07 +02001194 if (virtio_blk_handle_request(req, &mrb)) {
1195 /* Device is now broken and won't do any processing until it gets
1196 * reset. Already queued requests will be lost: let's purge them.
1197 */
1198 while (req) {
1199 next = req->next;
1200 virtqueue_detach_element(req->vq, &req->elem, 0);
1201 virtio_blk_free_request(req);
1202 req = next;
1203 }
1204 break;
1205 }
zhanghailiang1bdb1762014-08-18 15:42:50 +08001206 req = next;
aliguori869a5c62009-01-22 19:52:25 +00001207 }
Kevin Wolff1b52862010-01-27 13:12:35 +01001208
Peter Lieven95f71422015-02-02 14:52:21 +01001209 if (mrb.num_reqs) {
Stefan Hajnoczibaf42262022-10-13 14:59:08 -04001210 virtio_blk_submit_multireq(s, &mrb);
Peter Lieven95f71422015-02-02 14:52:21 +01001211 }
Stefan Hajnoczia937f8e2022-11-02 14:23:37 -04001212
1213 /* Paired with inc in virtio_blk_dma_restart_cb() */
1214 blk_dec_in_flight(s->conf.conf.blk);
1215
Paolo Bonzini19196312017-02-13 14:52:31 +01001216 aio_context_release(blk_get_aio_context(s->conf.conf.blk));
aliguori869a5c62009-01-22 19:52:25 +00001217}
1218
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +01001219static void virtio_blk_dma_restart_cb(void *opaque, bool running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001220 RunState state)
Markus Armbruster213189a2009-07-28 14:33:41 -04001221{
1222 VirtIOBlock *s = opaque;
1223
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001224 if (!running) {
Markus Armbruster213189a2009-07-28 14:33:41 -04001225 return;
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001226 }
Markus Armbruster213189a2009-07-28 14:33:41 -04001227
Stefan Hajnoczia937f8e2022-11-02 14:23:37 -04001228 /* Paired with dec in virtio_blk_dma_restart_bh() */
1229 blk_inc_in_flight(s->conf.conf.blk);
1230
1231 aio_bh_schedule_oneshot(blk_get_aio_context(s->conf.conf.blk),
1232 virtio_blk_dma_restart_bh, s);
Markus Armbruster213189a2009-07-28 14:33:41 -04001233}
1234
aliguori6e02c382008-12-04 19:52:44 +00001235static void virtio_blk_reset(VirtIODevice *vdev)
1236{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001237 VirtIOBlock *s = VIRTIO_BLK(vdev);
Alexander Yarygin6e40b3b2015-06-17 13:37:20 +03001238 AioContext *ctx;
Fam Zheng26307f62016-08-04 10:44:13 +08001239 VirtIOBlockReq *req;
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001240
Alexander Yarygin6e40b3b2015-06-17 13:37:20 +03001241 ctx = blk_get_aio_context(s->blk);
1242 aio_context_acquire(ctx);
1243 blk_drain(s->blk);
1244
Fam Zheng26307f62016-08-04 10:44:13 +08001245 /* We drop queued requests after blk_drain() because blk_drain() itself can
1246 * produce them. */
1247 while (s->rq) {
1248 req = s->rq;
1249 s->rq = req->next;
Stefan Hajnoczi97b93c82016-09-19 14:28:04 +01001250 virtqueue_detach_element(req->vq, &req->elem, 0);
Fam Zheng26307f62016-08-04 10:44:13 +08001251 virtio_blk_free_request(req);
1252 }
1253
Alexander Yarygin6e40b3b2015-06-17 13:37:20 +03001254 aio_context_release(ctx);
1255
Paolo Bonzini9ffe3372016-10-21 22:48:09 +02001256 assert(!s->dataplane_started);
Markus Armbruster4be74632014-10-07 13:59:18 +02001257 blk_set_enable_write_cache(s->blk, s->original_wce);
aliguori6e02c382008-12-04 19:52:44 +00001258}
1259
john cooperbf011292009-06-22 14:26:51 -04001260/* coalesce internal state, copy to pci i/o region 0
1261 */
aliguori6e02c382008-12-04 19:52:44 +00001262static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
1263{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001264 VirtIOBlock *s = VIRTIO_BLK(vdev);
Markus Armbruster2a303072014-10-07 13:59:17 +02001265 BlockConf *conf = &s->conf.conf;
Sam Li4f736652023-05-08 13:19:13 +08001266 BlockDriverState *bs = blk_bs(s->blk);
aliguori6e02c382008-12-04 19:52:44 +00001267 struct virtio_blk_config blkcfg;
1268 uint64_t capacity;
Stefan Hajnoczi17d0bc02017-08-08 13:22:51 +01001269 int64_t length;
Markus Armbrusterf7516732014-10-07 13:59:16 +02001270 int blk_size = conf->logical_block_size;
Emanuele Giuseppe Esposito1f433e82023-02-08 06:11:48 -05001271 AioContext *ctx;
1272
1273 ctx = blk_get_aio_context(s->blk);
1274 aio_context_acquire(ctx);
aliguori6e02c382008-12-04 19:52:44 +00001275
Markus Armbruster4be74632014-10-07 13:59:18 +02001276 blk_get_geometry(s->blk, &capacity);
Gerd Hoffmann5c5dafd2009-06-12 09:50:18 +02001277 memset(&blkcfg, 0, sizeof(blkcfg));
Rusty Russell783d1892014-06-24 19:43:44 +02001278 virtio_stq_p(vdev, &blkcfg.capacity, capacity);
Denis Plotnikov1bf8a982019-12-20 17:09:04 +03001279 virtio_stl_p(vdev, &blkcfg.seg_max,
1280 s->conf.seg_max_adjust ? s->conf.queue_size - 2 : 128 - 2);
Michael S. Tsirkin907eb3e2015-02-16 22:36:03 +01001281 virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls);
Rusty Russell783d1892014-06-24 19:43:44 +02001282 virtio_stl_p(vdev, &blkcfg.blk_size, blk_size);
Markus Armbrusterf7516732014-10-07 13:59:16 +02001283 virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size);
Roman Kagan6abee262020-05-29 01:55:09 +03001284 virtio_stl_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size);
Michael S. Tsirkin907eb3e2015-02-16 22:36:03 +01001285 blkcfg.geometry.heads = conf->heads;
Christian Borntraeger136be992012-05-24 13:22:55 +02001286 /*
1287 * We must ensure that the block device capacity is a multiple of
Peter Maydelle03ba132013-04-09 12:48:19 +01001288 * the logical block size. If that is not the case, let's use
Christian Borntraeger136be992012-05-24 13:22:55 +02001289 * sector_mask to adopt the geometry to have a correct picture.
1290 * For those devices where the capacity is ok for the given geometry
Peter Maydelle03ba132013-04-09 12:48:19 +01001291 * we don't touch the sector value of the geometry, since some devices
Christian Borntraeger136be992012-05-24 13:22:55 +02001292 * (like s390 dasd) need a specific value. Here the capacity is already
1293 * cyls*heads*secs*blk_size and the sector value is not block size
1294 * divided by 512 - instead it is the amount of blk_size blocks
1295 * per track (cylinder).
1296 */
Stefan Hajnoczi17d0bc02017-08-08 13:22:51 +01001297 length = blk_getlength(s->blk);
Emanuele Giuseppe Esposito1f433e82023-02-08 06:11:48 -05001298 aio_context_release(ctx);
Stefan Hajnoczi17d0bc02017-08-08 13:22:51 +01001299 if (length > 0 && length / conf->heads / conf->secs % blk_size) {
Michael S. Tsirkin907eb3e2015-02-16 22:36:03 +01001300 blkcfg.geometry.sectors = conf->secs & ~s->sector_mask;
Christian Borntraeger136be992012-05-24 13:22:55 +02001301 } else {
Michael S. Tsirkin907eb3e2015-02-16 22:36:03 +01001302 blkcfg.geometry.sectors = conf->secs;
Christian Borntraeger136be992012-05-24 13:22:55 +02001303 }
Blue Swirlc7085da2009-06-13 13:20:25 +00001304 blkcfg.size_max = 0;
Markus Armbrusterf7516732014-10-07 13:59:16 +02001305 blkcfg.physical_block_exp = get_physical_block_exp(conf);
Christoph Hellwig9752c372010-02-10 23:37:25 +01001306 blkcfg.alignment_offset = 0;
Markus Armbruster4be74632014-10-07 13:59:18 +02001307 blkcfg.wce = blk_enable_write_cache(s->blk);
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001308 virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001309 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
Akihiko Odakifb0b1542021-02-25 09:12:39 +09001310 uint32_t discard_granularity = conf->discard_granularity;
1311 if (discard_granularity == -1 || !s->conf.report_discard_granularity) {
1312 discard_granularity = blk_size;
1313 }
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001314 virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
1315 s->conf.max_discard_sectors);
1316 virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
Akihiko Odakifb0b1542021-02-25 09:12:39 +09001317 discard_granularity >> BDRV_SECTOR_BITS);
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001318 /*
1319 * We support only one segment per request since multiple segments
1320 * are not widely used and there are no userspace APIs that allow
1321 * applications to submit multiple segments in a single call.
1322 */
1323 virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
1324 }
1325 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
1326 virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
1327 s->conf.max_write_zeroes_sectors);
1328 blkcfg.write_zeroes_may_unmap = 1;
1329 virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
1330 }
Sam Li4f736652023-05-08 13:19:13 +08001331 if (bs->bl.zoned != BLK_Z_NONE) {
1332 switch (bs->bl.zoned) {
1333 case BLK_Z_HM:
1334 blkcfg.zoned.model = VIRTIO_BLK_Z_HM;
1335 break;
1336 case BLK_Z_HA:
1337 blkcfg.zoned.model = VIRTIO_BLK_Z_HA;
1338 break;
1339 default:
1340 g_assert_not_reached();
1341 }
1342
1343 virtio_stl_p(vdev, &blkcfg.zoned.zone_sectors,
1344 bs->bl.zone_size / 512);
1345 virtio_stl_p(vdev, &blkcfg.zoned.max_active_zones,
1346 bs->bl.max_active_zones);
1347 virtio_stl_p(vdev, &blkcfg.zoned.max_open_zones,
1348 bs->bl.max_open_zones);
1349 virtio_stl_p(vdev, &blkcfg.zoned.write_granularity, blk_size);
1350 virtio_stl_p(vdev, &blkcfg.zoned.max_append_sectors,
1351 bs->bl.max_append_sectors);
1352 } else {
1353 blkcfg.zoned.model = VIRTIO_BLK_Z_NONE;
1354 }
Stefano Garzarella20764be2019-02-21 11:33:09 +01001355 memcpy(config, &blkcfg, s->config_size);
aliguori6e02c382008-12-04 19:52:44 +00001356}
1357
Paolo Bonzini13e3dce2012-08-09 16:07:19 +02001358static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
1359{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001360 VirtIOBlock *s = VIRTIO_BLK(vdev);
Paolo Bonzini13e3dce2012-08-09 16:07:19 +02001361 struct virtio_blk_config blkcfg;
1362
Stefano Garzarella20764be2019-02-21 11:33:09 +01001363 memcpy(&blkcfg, config, s->config_size);
Fam Zheng6d7e73d2014-05-15 19:22:06 +08001364
Markus Armbruster4be74632014-10-07 13:59:18 +02001365 aio_context_acquire(blk_get_aio_context(s->blk));
1366 blk_set_enable_write_cache(s->blk, blkcfg.wce != 0);
1367 aio_context_release(blk_get_aio_context(s->blk));
Paolo Bonzini13e3dce2012-08-09 16:07:19 +02001368}
1369
Jason Wang9d5b7312015-07-27 17:49:19 +08001370static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features,
1371 Error **errp)
aliguori6e02c382008-12-04 19:52:44 +00001372{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001373 VirtIOBlock *s = VIRTIO_BLK(vdev);
Christoph Hellwig1063b8b2009-04-27 10:29:14 +02001374
Stefano Garzarellabbe8bd42019-02-21 11:33:06 +01001375 /* Firstly sync all virtio-blk possible supported features */
1376 features |= s->host_features;
1377
Cornelia Huck0cd09c32014-12-11 14:25:05 +01001378 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
1379 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
1380 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
1381 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
Cornelia Huck95129d62015-08-17 11:48:29 +02001382 if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) {
Stefano Garzarellabbe8bd42019-02-21 11:33:06 +01001383 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) {
Jason Wangefb82062015-07-27 17:49:20 +08001384 error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0");
1385 return 0;
1386 }
Jason Wangefb82062015-07-27 17:49:20 +08001387 } else {
Jason Wangc9b11f92015-07-27 17:49:21 +08001388 virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT);
Jason Wangefb82062015-07-27 17:49:20 +08001389 virtio_add_feature(&features, VIRTIO_BLK_F_SCSI);
1390 }
Christoph Hellwigaa659be2009-09-04 19:02:23 +02001391
Evgeny Yakovlev5f258572019-11-05 21:22:17 +03001392 if (blk_enable_write_cache(s->blk) ||
1393 (s->conf.x_enable_wce_if_config_wce &&
1394 virtio_has_feature(features, VIRTIO_BLK_F_CONFIG_WCE))) {
Cornelia Huck0cd09c32014-12-11 14:25:05 +01001395 virtio_add_feature(&features, VIRTIO_BLK_F_WCE);
Markus Armbruster4be74632014-10-07 13:59:18 +02001396 }
Kevin Wolf86b1cf32021-01-18 13:34:47 +01001397 if (!blk_is_writable(s->blk)) {
Cornelia Huck0cd09c32014-12-11 14:25:05 +01001398 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
Markus Armbruster4be74632014-10-07 13:59:18 +02001399 }
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001400 if (s->conf.num_queues > 1) {
1401 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
1402 }
Christoph Hellwig1063b8b2009-04-27 10:29:14 +02001403
1404 return features;
aliguori6e02c382008-12-04 19:52:44 +00001405}
1406
Paolo Bonzini9315cbf2012-08-09 16:07:20 +02001407static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status)
1408{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001409 VirtIOBlock *s = VIRTIO_BLK(vdev);
Paolo Bonzini9315cbf2012-08-09 16:07:20 +02001410
Paolo Bonzini9ffe3372016-10-21 22:48:09 +02001411 if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) {
1412 assert(!s->dataplane_started);
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001413 }
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001414
Paolo Bonzini9315cbf2012-08-09 16:07:20 +02001415 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1416 return;
1417 }
1418
Paolo Bonzinief5bc962013-09-20 17:31:55 +02001419 /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send
1420 * cache flushes. Thus, the "auto writethrough" behavior is never
1421 * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature.
1422 * Leaving it enabled would break the following sequence:
1423 *
1424 * Guest started with "-drive cache=writethrough"
1425 * Guest sets status to 0
1426 * Guest sets DRIVER bit in status field
1427 * Guest reads host features (WCE=0, CONFIG_WCE=1)
1428 * Guest writes guest features (WCE=0, CONFIG_WCE=1)
1429 * Guest writes 1 to the WCE configuration field (writeback mode)
1430 * Guest sets DRIVER_OK bit in status field
1431 *
Markus Armbruster4be74632014-10-07 13:59:18 +02001432 * s->blk would erroneously be placed in writethrough mode.
Paolo Bonzinief5bc962013-09-20 17:31:55 +02001433 */
Cornelia Huck95129d62015-08-17 11:48:29 +02001434 if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) {
Markus Armbruster4be74632014-10-07 13:59:18 +02001435 aio_context_acquire(blk_get_aio_context(s->blk));
1436 blk_set_enable_write_cache(s->blk,
Cornelia Huck95129d62015-08-17 11:48:29 +02001437 virtio_vdev_has_feature(vdev,
1438 VIRTIO_BLK_F_WCE));
Markus Armbruster4be74632014-10-07 13:59:18 +02001439 aio_context_release(blk_get_aio_context(s->blk));
Paolo Bonzinief5bc962013-09-20 17:31:55 +02001440 }
Paolo Bonzini9315cbf2012-08-09 16:07:20 +02001441}
1442
Greg Kurzb2b295a2014-06-24 19:19:23 +02001443static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f)
1444{
1445 VirtIOBlock *s = VIRTIO_BLK(vdev);
1446 VirtIOBlockReq *req = s->rq;
1447
aliguori869a5c62009-01-22 19:52:25 +00001448 while (req) {
1449 qemu_put_sbyte(f, 1);
Stefan Hajnoczi30d8bf62016-06-21 13:13:14 +01001450
1451 if (s->conf.num_queues > 1) {
1452 qemu_put_be32(f, virtio_get_queue_index(req->vq));
1453 }
1454
Jason Wang86044b22019-10-25 10:35:24 +02001455 qemu_put_virtqueue_element(vdev, f, &req->elem);
aliguori869a5c62009-01-22 19:52:25 +00001456 req = req->next;
1457 }
1458 qemu_put_sbyte(f, 0);
aliguori6e02c382008-12-04 19:52:44 +00001459}
1460
Greg Kurzb2b295a2014-06-24 19:19:23 +02001461static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f,
1462 int version_id)
1463{
1464 VirtIOBlock *s = VIRTIO_BLK(vdev);
Orit Wassermann2a633c42012-05-16 12:21:35 +02001465
aliguori869a5c62009-01-22 19:52:25 +00001466 while (qemu_get_sbyte(f)) {
Stefan Hajnoczi30d8bf62016-06-21 13:13:14 +01001467 unsigned nvqs = s->conf.num_queues;
1468 unsigned vq_idx = 0;
Paolo Bonziniab281c12016-01-31 11:28:59 +01001469 VirtIOBlockReq *req;
Stefan Hajnoczi30d8bf62016-06-21 13:13:14 +01001470
1471 if (nvqs > 1) {
1472 vq_idx = qemu_get_be32(f);
1473
1474 if (vq_idx >= nvqs) {
1475 error_report("Invalid virtqueue index in request list: %#x",
1476 vq_idx);
1477 return -EINVAL;
1478 }
1479 }
1480
Jason Wang8607f5c2016-12-30 18:09:10 +08001481 req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq));
Stefan Hajnoczi30d8bf62016-06-21 13:13:14 +01001482 virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req);
aliguori869a5c62009-01-22 19:52:25 +00001483 req->next = s->rq;
Yoshiaki Tamura20a81e42010-06-21 17:50:01 +09001484 s->rq = req;
aliguori869a5c62009-01-22 19:52:25 +00001485 }
aliguori6e02c382008-12-04 19:52:44 +00001486
1487 return 0;
1488}
1489
Sergio Lopez9b92fbc2019-09-16 13:24:12 +02001490static void virtio_resize_cb(void *opaque)
1491{
1492 VirtIODevice *vdev = opaque;
1493
1494 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1495 virtio_notify_config(vdev);
1496}
1497
Markus Armbruster145feb12011-08-03 15:07:42 +02001498static void virtio_blk_resize(void *opaque)
Christoph Hellwige5051fc2011-01-24 13:32:51 +01001499{
KONRAD Frederic1cc91b72013-03-18 17:37:27 +01001500 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
Christoph Hellwige5051fc2011-01-24 13:32:51 +01001501
Sergio Lopez9b92fbc2019-09-16 13:24:12 +02001502 /*
1503 * virtio_notify_config() needs to acquire the global mutex,
1504 * so it can't be called from an iothread. Instead, schedule
1505 * it to be run in the main context BH.
1506 */
1507 aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb, vdev);
Christoph Hellwige5051fc2011-01-24 13:32:51 +01001508}
1509
Stefan Hajnoczi1665d932023-05-16 15:02:35 -04001510/* Suspend virtqueue ioeventfd processing during drain */
1511static void virtio_blk_drained_begin(void *opaque)
1512{
1513 VirtIOBlock *s = opaque;
1514 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1515 AioContext *ctx = blk_get_aio_context(s->conf.conf.blk);
1516
1517 if (!s->dataplane || !s->dataplane_started) {
1518 return;
1519 }
1520
1521 for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1522 VirtQueue *vq = virtio_get_queue(vdev, i);
1523 virtio_queue_aio_detach_host_notifier(vq, ctx);
1524 }
1525}
1526
1527/* Resume virtqueue ioeventfd processing after drain */
1528static void virtio_blk_drained_end(void *opaque)
1529{
1530 VirtIOBlock *s = opaque;
1531 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1532 AioContext *ctx = blk_get_aio_context(s->conf.conf.blk);
1533
1534 if (!s->dataplane || !s->dataplane_started) {
1535 return;
1536 }
1537
1538 for (uint16_t i = 0; i < s->conf.num_queues; i++) {
1539 VirtQueue *vq = virtio_get_queue(vdev, i);
1540 virtio_queue_aio_attach_host_notifier(vq, ctx);
1541 }
1542}
1543
Markus Armbruster0e49de52011-08-03 15:07:41 +02001544static const BlockDevOps virtio_block_ops = {
Stefan Hajnoczi1665d932023-05-16 15:02:35 -04001545 .resize_cb = virtio_blk_resize,
1546 .drained_begin = virtio_blk_drained_begin,
1547 .drained_end = virtio_blk_drained_end,
Markus Armbruster0e49de52011-08-03 15:07:41 +02001548};
1549
Andreas Färber75884af2013-07-30 01:35:08 +02001550static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001551{
Andreas Färber75884af2013-07-30 01:35:08 +02001552 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
Andreas Färber179b4172013-07-30 04:48:15 +02001553 VirtIOBlock *s = VIRTIO_BLK(dev);
Markus Armbruster2a303072014-10-07 13:59:17 +02001554 VirtIOBlkConf *conf = &s->conf;
Andreas Färber3ffeeef2013-06-07 16:18:50 +02001555 Error *err = NULL;
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001556 unsigned i;
aliguori6e02c382008-12-04 19:52:44 +00001557
Markus Armbruster4be74632014-10-07 13:59:18 +02001558 if (!conf->conf.blk) {
Andreas Färber75884af2013-07-30 01:35:08 +02001559 error_setg(errp, "drive property not set");
1560 return;
Markus Armbrusterd75d25e2010-07-06 14:37:43 +02001561 }
Markus Armbruster4be74632014-10-07 13:59:18 +02001562 if (!blk_is_inserted(conf->conf.blk)) {
Andreas Färber75884af2013-07-30 01:35:08 +02001563 error_setg(errp, "Device needs media, but drive is empty");
1564 return;
Markus Armbruster98f28ad2010-07-06 14:37:44 +02001565 }
Stefan Hajnoczi9445e1e2020-08-18 15:33:47 +01001566 if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
1567 conf->num_queues = 1;
1568 }
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001569 if (!conf->num_queues) {
1570 error_setg(errp, "num-queues property must be larger than 0");
1571 return;
1572 }
Denis Plotnikov1bf8a982019-12-20 17:09:04 +03001573 if (conf->queue_size <= 2) {
1574 error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1575 "must be > 2", conf->queue_size);
1576 return;
1577 }
Mark Kanda6040aed2017-12-11 09:16:24 -06001578 if (!is_power_of_2(conf->queue_size) ||
1579 conf->queue_size > VIRTQUEUE_MAX_SIZE) {
1580 error_setg(errp, "invalid queue-size property (%" PRIu16 "), "
1581 "must be a power of 2 (max %d)",
1582 conf->queue_size, VIRTQUEUE_MAX_SIZE);
1583 return;
1584 }
Markus Armbrusterd75d25e2010-07-06 14:37:43 +02001585
Mao Zhongyiceff3e12017-11-22 11:08:45 +08001586 if (!blkconf_apply_backend_options(&conf->conf,
Kevin Wolf86b1cf32021-01-18 13:34:47 +01001587 !blk_supports_write_perm(conf->conf.blk),
1588 true, errp)) {
Kevin Wolfa17c17a2017-01-24 13:43:31 +01001589 return;
1590 }
Markus Armbruster4be74632014-10-07 13:59:18 +02001591 s->original_wce = blk_enable_write_cache(conf->conf.blk);
Mao Zhongyiceff3e12017-11-22 11:08:45 +08001592 if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) {
Andreas Färber75884af2013-07-30 01:35:08 +02001593 return;
Markus Armbrusterb7eb0c92012-07-11 15:08:39 +02001594 }
Mao Zhongyiceff3e12017-11-22 11:08:45 +08001595
Roman Kaganc56ee922020-05-29 01:55:10 +03001596 if (!blkconf_blocksizes(&conf->conf, errp)) {
Mark Kanda0a75b602017-12-11 09:16:25 -06001597 return;
1598 }
1599
Sam Li4f736652023-05-08 13:19:13 +08001600 BlockDriverState *bs = blk_bs(conf->conf.blk);
1601 if (bs->bl.zoned != BLK_Z_NONE) {
1602 virtio_add_feature(&s->host_features, VIRTIO_BLK_F_ZONED);
1603 if (bs->bl.zoned == BLK_Z_HM) {
1604 virtio_clear_feature(&s->host_features, VIRTIO_BLK_F_DISCARD);
1605 }
1606 }
1607
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001608 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) &&
1609 (!conf->max_discard_sectors ||
1610 conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1611 error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")"
1612 ", must be between 1 and %d",
1613 conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS);
1614 return;
1615 }
1616
1617 if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) &&
1618 (!conf->max_write_zeroes_sectors ||
1619 conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) {
1620 error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32
1621 "), must be between 1 and %d",
1622 conf->max_write_zeroes_sectors,
1623 (int)BDRV_REQUEST_MAX_SECTORS);
1624 return;
1625 }
1626
Daniil Tatianind9cf55a2022-09-06 10:31:08 +03001627 s->config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
Daniil Tatianind74c30c2022-09-06 10:31:07 +03001628 s->host_features);
Jonah Palmer3857cd52022-04-01 09:23:18 -04001629 virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size);
aliguori6e02c382008-12-04 19:52:44 +00001630
Markus Armbruster4be74632014-10-07 13:59:18 +02001631 s->blk = conf->conf.blk;
aliguori869a5c62009-01-22 19:52:25 +00001632 s->rq = NULL;
Markus Armbruster2a303072014-10-07 13:59:17 +02001633 s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1;
Markus Armbrustere63e7fd2012-07-10 11:12:43 +02001634
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001635 for (i = 0; i < conf->num_queues; i++) {
Mark Kanda6040aed2017-12-11 09:16:24 -06001636 virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output);
Stefan Hajnoczi2f270592016-06-21 13:13:16 +01001637 }
Kevin Wolf98e3ab32022-05-10 17:10:19 +02001638 qemu_coroutine_inc_pool_size(conf->num_queues * conf->queue_size / 2);
Markus Armbruster2a303072014-10-07 13:59:17 +02001639 virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err);
Andreas Färber3ffeeef2013-06-07 16:18:50 +02001640 if (err != NULL) {
Andreas Färber75884af2013-07-30 01:35:08 +02001641 error_propagate(errp, err);
Pan Nengyuancfaf7572020-03-28 08:57:04 +08001642 for (i = 0; i < conf->num_queues; i++) {
1643 virtio_del_queue(vdev, i);
1644 }
KONRAD Frederic6a1a8cc2013-04-24 10:21:22 +02001645 virtio_cleanup(vdev);
Andreas Färber75884af2013-07-30 01:35:08 +02001646 return;
Stefan Hajnoczi392808b2012-11-14 15:45:38 +01001647 }
aliguori6e02c382008-12-04 19:52:44 +00001648
Stefan Hajnoczia937f8e2022-11-02 14:23:37 -04001649 /*
1650 * This must be after virtio_init() so virtio_blk_dma_restart_cb() gets
1651 * called after ->start_ioeventfd() has already set blk's AioContext.
1652 */
1653 s->change =
1654 qdev_add_vm_change_state_handler(dev, virtio_blk_dma_restart_cb, s);
1655
Stefan Hajnoczibaf42262022-10-13 14:59:08 -04001656 blk_ram_registrar_init(&s->blk_ram_registrar, s->blk);
Markus Armbruster4be74632014-10-07 13:59:18 +02001657 blk_set_dev_ops(s->blk, &virtio_block_ops, s);
Paul Brook07e3af92009-05-14 22:35:08 +01001658
Markus Armbruster4be74632014-10-07 13:59:18 +02001659 blk_iostatus_enable(s->blk);
Sam Eiderman71f571a2019-10-16 19:41:42 +03001660
1661 add_boot_device_lchs(dev, "/disk@0,0",
1662 conf->conf.lcyls,
1663 conf->conf.lheads,
1664 conf->conf.lsecs);
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001665}
1666
Markus Armbrusterb69c3c22020-05-05 17:29:24 +02001667static void virtio_blk_device_unrealize(DeviceState *dev)
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001668{
Andreas Färber306ec6c2013-07-30 03:50:44 +02001669 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1670 VirtIOBlock *s = VIRTIO_BLK(dev);
Eugenio Pérez4a0117c2019-10-25 10:35:22 +02001671 VirtIOBlkConf *conf = &s->conf;
1672 unsigned i;
Andreas Färber306ec6c2013-07-30 03:50:44 +02001673
Julia Suvorova7bfde682019-10-18 16:28:56 +02001674 blk_drain(s->blk);
Sam Eiderman71f571a2019-10-16 19:41:42 +03001675 del_boot_device_lchs(dev, "/disk@0,0");
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001676 virtio_blk_data_plane_destroy(s->dataplane);
1677 s->dataplane = NULL;
Eugenio Pérez4a0117c2019-10-25 10:35:22 +02001678 for (i = 0; i < conf->num_queues; i++) {
1679 virtio_del_queue(vdev, i);
1680 }
Kevin Wolf98e3ab32022-05-10 17:10:19 +02001681 qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2);
Stefan Hajnoczibaf42262022-10-13 14:59:08 -04001682 blk_ram_registrar_destroy(&s->blk_ram_registrar);
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001683 qemu_del_vm_change_state_handler(s->change);
Markus Armbruster4be74632014-10-07 13:59:18 +02001684 blockdev_mark_auto_del(s->blk);
KONRAD Frederic6a1a8cc2013-04-24 10:21:22 +02001685 virtio_cleanup(vdev);
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001686}
1687
Stefan Hajnoczi467b3f32014-06-10 09:03:20 +02001688static void virtio_blk_instance_init(Object *obj)
1689{
1690 VirtIOBlock *s = VIRTIO_BLK(obj);
1691
Markus Armbruster2a303072014-10-07 13:59:17 +02001692 device_add_bootindex_property(obj, &s->conf.conf.bootindex,
Gonglei3342ec32014-10-07 16:00:30 +08001693 "bootindex", "/disk@0,0",
Markus Armbruster40c22812020-05-05 17:29:23 +02001694 DEVICE(obj));
Stefan Hajnoczi467b3f32014-06-10 09:03:20 +02001695}
1696
Halil Pasic977a1172016-10-06 14:55:40 +02001697static const VMStateDescription vmstate_virtio_blk = {
1698 .name = "virtio-blk",
1699 .minimum_version_id = 2,
1700 .version_id = 2,
1701 .fields = (VMStateField[]) {
1702 VMSTATE_VIRTIO_DEVICE,
1703 VMSTATE_END_OF_LIST()
1704 },
1705};
Dr. David Alan Gilbertbbded322016-07-14 18:22:47 +01001706
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001707static Property virtio_blk_properties[] = {
Markus Armbruster2a303072014-10-07 13:59:17 +02001708 DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf),
Kevin Wolf8c398252016-06-29 17:41:35 +02001709 DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf),
Markus Armbruster2a303072014-10-07 13:59:17 +02001710 DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf),
1711 DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial),
Stefano Garzarellabbe8bd42019-02-21 11:33:06 +01001712 DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features,
1713 VIRTIO_BLK_F_CONFIG_WCE, true),
Stefan Hajnoczi32a877e2014-06-18 17:58:36 +08001714#ifdef __linux__
Stefano Garzarellabbe8bd42019-02-21 11:33:06 +01001715 DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features,
1716 VIRTIO_BLK_F_SCSI, false),
Stefan Hajnoczi32a877e2014-06-18 17:58:36 +08001717#endif
Peter Lievenc99495a2015-02-02 14:52:22 +01001718 DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0,
1719 true),
Stefan Hajnoczi9445e1e2020-08-18 15:33:47 +01001720 DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues,
1721 VIRTIO_BLK_AUTO_NUM_QUEUES),
Denis Plotnikovc9b7d9e2020-02-14 10:46:48 +03001722 DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 256),
Denis Plotnikov1bf8a982019-12-20 17:09:04 +03001723 DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock, conf.seg_max_adjust, true),
Fam Zhengd679ac02017-07-14 10:14:55 +08001724 DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD,
1725 IOThread *),
Stefano Garzarella5c811612019-02-21 11:33:07 +01001726 DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features,
1727 VIRTIO_BLK_F_DISCARD, true),
Akihiko Odakifb0b1542021-02-25 09:12:39 +09001728 DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock,
1729 conf.report_discard_granularity, true),
Stefano Garzarella5c811612019-02-21 11:33:07 +01001730 DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features,
1731 VIRTIO_BLK_F_WRITE_ZEROES, true),
Stefano Garzarella37b06f82019-02-21 11:33:10 +01001732 DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock,
1733 conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS),
1734 DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock,
1735 conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS),
Evgeny Yakovlev5f258572019-11-05 21:22:17 +03001736 DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock,
1737 conf.x_enable_wce_if_config_wce, true),
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001738 DEFINE_PROP_END_OF_LIST(),
1739};
1740
1741static void virtio_blk_class_init(ObjectClass *klass, void *data)
1742{
1743 DeviceClass *dc = DEVICE_CLASS(klass);
1744 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
Andreas Färber75884af2013-07-30 01:35:08 +02001745
Marc-André Lureau4f67d302020-01-10 19:30:32 +04001746 device_class_set_props(dc, virtio_blk_properties);
Dr. David Alan Gilbertbbded322016-07-14 18:22:47 +01001747 dc->vmsd = &vmstate_virtio_blk;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +03001748 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
Andreas Färber75884af2013-07-30 01:35:08 +02001749 vdc->realize = virtio_blk_device_realize;
Andreas Färber306ec6c2013-07-30 03:50:44 +02001750 vdc->unrealize = virtio_blk_device_unrealize;
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001751 vdc->get_config = virtio_blk_update_config;
1752 vdc->set_config = virtio_blk_set_config;
1753 vdc->get_features = virtio_blk_get_features;
1754 vdc->set_status = virtio_blk_set_status;
1755 vdc->reset = virtio_blk_reset;
Greg Kurzb2b295a2014-06-24 19:19:23 +02001756 vdc->save = virtio_blk_save_device;
1757 vdc->load = virtio_blk_load_device;
Paolo Bonzini9ffe3372016-10-21 22:48:09 +02001758 vdc->start_ioeventfd = virtio_blk_data_plane_start;
1759 vdc->stop_ioeventfd = virtio_blk_data_plane_stop;
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001760}
1761
Changlong Xieb5c7cea2016-08-03 16:49:07 +08001762static const TypeInfo virtio_blk_info = {
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001763 .name = TYPE_VIRTIO_BLK,
1764 .parent = TYPE_VIRTIO_DEVICE,
1765 .instance_size = sizeof(VirtIOBlock),
Stefan Hajnoczi467b3f32014-06-10 09:03:20 +02001766 .instance_init = virtio_blk_instance_init,
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001767 .class_init = virtio_blk_class_init,
1768};
1769
1770static void virtio_register_types(void)
1771{
Changlong Xieb5c7cea2016-08-03 16:49:07 +08001772 type_register_static(&virtio_blk_info);
KONRAD Frederic1c028dd2013-03-18 17:37:22 +01001773}
1774
1775type_init(virtio_register_types)