aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Fam Zheng | 827805a | 2014-06-11 12:11:48 +0800 | [diff] [blame] | 16 | #include "qemu/iov.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 17 | #include "qemu/module.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/error-report.h" |
Sergio Lopez | 9b92fbc | 2019-09-16 13:24:12 +0200 | [diff] [blame] | 19 | #include "qemu/main-loop.h" |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 20 | #include "trace.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 21 | #include "hw/block/block.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 22 | #include "hw/qdev-properties.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 23 | #include "sysemu/blockdev.h" |
Markus Armbruster | 2f780b6 | 2019-08-12 07:23:58 +0200 | [diff] [blame] | 24 | #include "sysemu/sysemu.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 25 | #include "sysemu/runstate.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/virtio/virtio-blk.h" |
Fam Zheng | 52b53c0 | 2014-09-10 14:17:51 +0800 | [diff] [blame] | 27 | #include "dataplane/virtio-blk.h" |
Paolo Bonzini | 08e2c9f | 2017-08-22 09:23:55 +0200 | [diff] [blame] | 28 | #include "scsi/constants.h" |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 29 | #ifdef __linux__ |
| 30 | # include <scsi/sg.h> |
| 31 | #endif |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 32 | #include "hw/virtio/virtio-bus.h" |
Markus Armbruster | ca77ee2 | 2019-08-12 07:23:39 +0200 | [diff] [blame] | 33 | #include "migration/qemu-file-types.h" |
Rusty Russell | 783d189 | 2014-06-24 19:43:44 +0200 | [diff] [blame] | 34 | #include "hw/virtio/virtio-access.h" |
Hiroki Narukawa | 4c41c69 | 2022-02-14 20:53:02 +0900 | [diff] [blame] | 35 | #include "qemu/coroutine.h" |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 36 | |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 37 | /* Config size before the discard support (hide associated config fields) */ |
Changpeng Liu | 42824b4 | 2019-02-13 09:48:57 +0800 | [diff] [blame] | 38 | #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \ |
| 39 | max_discard_sectors) |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Starting from the discard feature, we can use this array to properly |
| 42 | * set the config size depending on the features enabled. |
| 43 | */ |
Philippe Mathieu-Daudé | 01ce772 | 2021-05-11 12:41:56 +0200 | [diff] [blame] | 44 | static const VirtIOFeature feature_sizes[] = { |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 45 | {.flags = 1ULL << VIRTIO_BLK_F_DISCARD, |
Max Reitz | 5d5b33c | 2019-10-11 17:27:59 +0200 | [diff] [blame] | 46 | .end = endof(struct virtio_blk_config, discard_sector_alignment)}, |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 47 | {.flags = 1ULL << VIRTIO_BLK_F_WRITE_ZEROES, |
Max Reitz | 5d5b33c | 2019-10-11 17:27:59 +0200 | [diff] [blame] | 48 | .end = endof(struct virtio_blk_config, write_zeroes_may_unmap)}, |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 49 | {} |
| 50 | }; |
| 51 | |
| 52 | static void virtio_blk_set_config_size(VirtIOBlock *s, uint64_t host_features) |
| 53 | { |
| 54 | s->config_size = MAX(VIRTIO_BLK_CFG_SIZE, |
| 55 | virtio_feature_get_config_size(feature_sizes, host_features)); |
| 56 | |
| 57 | assert(s->config_size <= sizeof(struct virtio_blk_config)); |
| 58 | } |
Changpeng Liu | 42824b4 | 2019-02-13 09:48:57 +0800 | [diff] [blame] | 59 | |
Greg Kurz | d14dde5 | 2016-09-30 17:12:50 +0200 | [diff] [blame] | 60 | static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq, |
| 61 | VirtIOBlockReq *req) |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 62 | { |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 63 | req->dev = s; |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 64 | req->vq = vq; |
Stefan Hajnoczi | 869d66a | 2014-07-09 10:05:48 +0200 | [diff] [blame] | 65 | req->qiov.size = 0; |
Paolo Bonzini | 2a6cdd6 | 2015-04-02 19:50:44 +0200 | [diff] [blame] | 66 | req->in_len = 0; |
Stefan Hajnoczi | 869d66a | 2014-07-09 10:05:48 +0200 | [diff] [blame] | 67 | req->next = NULL; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 68 | req->mr_next = NULL; |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Greg Kurz | d14dde5 | 2016-09-30 17:12:50 +0200 | [diff] [blame] | 71 | static void virtio_blk_free_request(VirtIOBlockReq *req) |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 72 | { |
Fam Zheng | 1d29b5b | 2017-02-07 21:27:22 +0800 | [diff] [blame] | 73 | g_free(req); |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Paolo Bonzini | 03de2f5 | 2016-02-14 18:17:09 +0100 | [diff] [blame] | 76 | static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 77 | { |
| 78 | VirtIOBlock *s = req->dev; |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 79 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 80 | |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 81 | trace_virtio_blk_req_complete(vdev, req, status); |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 82 | |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 83 | stb_p(&req->in->status, status); |
Stefan Hajnoczi | 7bd04a0 | 2020-09-17 10:44:54 +0100 | [diff] [blame] | 84 | iov_discard_undo(&req->inhdr_undo); |
| 85 | iov_discard_undo(&req->outhdr_undo); |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 86 | virtqueue_push(req->vq, &req->elem, req->in_len); |
Paolo Bonzini | eb41cf7 | 2016-04-06 12:16:23 +0200 | [diff] [blame] | 87 | if (s->dataplane_started && !s->dataplane_disabled) { |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 88 | virtio_blk_data_plane_notify(s->dataplane, req->vq); |
Paolo Bonzini | 03de2f5 | 2016-02-14 18:17:09 +0100 | [diff] [blame] | 89 | } else { |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 90 | virtio_notify(vdev, req->vq); |
Paolo Bonzini | 03de2f5 | 2016-02-14 18:17:09 +0100 | [diff] [blame] | 91 | } |
Fam Zheng | bf4bd46 | 2014-06-17 14:32:06 +0800 | [diff] [blame] | 92 | } |
| 93 | |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 94 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
Stefano Garzarella | 00f639f | 2019-02-21 11:33:05 +0100 | [diff] [blame] | 95 | bool is_read, bool acct_failed) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 96 | { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 97 | VirtIOBlock *s = req->dev; |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 98 | BlockErrorAction action = blk_get_error_action(s->blk, is_read, error); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 99 | |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 100 | if (action == BLOCK_ERROR_ACTION_STOP) { |
Fam Zheng | 466138d | 2015-11-23 08:41:20 +0800 | [diff] [blame] | 101 | /* Break the link as the next request is going to be parsed from the |
| 102 | * ring again. Otherwise we may end up doing a double completion! */ |
| 103 | req->mr_next = NULL; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 104 | req->next = s->rq; |
| 105 | s->rq = req; |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 106 | } else if (action == BLOCK_ERROR_ACTION_REPORT) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 107 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
Stefano Garzarella | 00f639f | 2019-02-21 11:33:05 +0100 | [diff] [blame] | 108 | if (acct_failed) { |
| 109 | block_acct_failed(blk_get_stats(s->blk), &req->acct); |
| 110 | } |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 111 | virtio_blk_free_request(req); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 114 | blk_error_action(s->blk, action, is_read, error); |
Wenchao Xia | a589569 | 2014-06-18 08:43:30 +0200 | [diff] [blame] | 115 | return action != BLOCK_ERROR_ACTION_IGNORE; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 118 | static void virtio_blk_rw_complete(void *opaque, int ret) |
| 119 | { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 120 | VirtIOBlockReq *next = opaque; |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 121 | VirtIOBlock *s = next->dev; |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 122 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 123 | |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 124 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 125 | while (next) { |
| 126 | VirtIOBlockReq *req = next; |
| 127 | next = req->mr_next; |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 128 | trace_virtio_blk_rw_complete(vdev, req, ret); |
Stefan Hajnoczi | 6d519a5 | 2010-05-22 18:15:08 +0100 | [diff] [blame] | 129 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 130 | if (req->qiov.nalloc != -1) { |
Dongli Zhang | e61809e | 2018-11-06 12:52:32 +0800 | [diff] [blame] | 131 | /* If nalloc is != -1 req->qiov is a local copy of the original |
Yaowei Bai | 9bb192a | 2018-07-28 13:18:44 +0800 | [diff] [blame] | 132 | * external iovec. It was allocated in submit_requests to be |
| 133 | * able to merge requests. */ |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 134 | qemu_iovec_destroy(&req->qiov); |
| 135 | } |
| 136 | |
| 137 | if (ret) { |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 138 | int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 139 | bool is_read = !(p & VIRTIO_BLK_T_OUT); |
Paolo Bonzini | 2a6cdd6 | 2015-04-02 19:50:44 +0200 | [diff] [blame] | 140 | /* Note that memory may be dirtied on read failure. If the |
| 141 | * virtio request is not completed here, as is the case for |
| 142 | * BLOCK_ERROR_ACTION_STOP, the memory may not be copied |
| 143 | * correctly during live migration. While this is ugly, |
| 144 | * it is acceptable because the device is free to write to |
| 145 | * the memory until the request is completed (which will |
| 146 | * happen on the other side of the migration). |
| 147 | */ |
Stefano Garzarella | 00f639f | 2019-02-21 11:33:05 +0100 | [diff] [blame] | 148 | if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 154 | block_acct_done(blk_get_stats(s->blk), &req->acct); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 155 | virtio_blk_free_request(req); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 156 | } |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 157 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 158 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 159 | |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 160 | static void virtio_blk_flush_complete(void *opaque, int ret) |
| 161 | { |
| 162 | VirtIOBlockReq *req = opaque; |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 163 | VirtIOBlock *s = req->dev; |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 164 | |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 165 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
Kevin Wolf | 8c269b5 | 2010-10-20 13:17:30 +0200 | [diff] [blame] | 166 | if (ret) { |
Stefano Garzarella | 00f639f | 2019-02-21 11:33:05 +0100 | [diff] [blame] | 167 | if (virtio_blk_handle_rw_error(req, -ret, 0, true)) { |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 168 | goto out; |
Kevin Wolf | 8c269b5 | 2010-10-20 13:17:30 +0200 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 173 | block_acct_done(blk_get_stats(s->blk), &req->acct); |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 174 | virtio_blk_free_request(req); |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 175 | |
| 176 | out: |
| 177 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 180 | static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret) |
| 181 | { |
| 182 | VirtIOBlockReq *req = opaque; |
| 183 | VirtIOBlock *s = req->dev; |
| 184 | bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) & |
| 185 | ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES; |
| 186 | |
| 187 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
| 188 | if (ret) { |
| 189 | if (virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) { |
| 190 | goto out; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
| 195 | if (is_write_zeroes) { |
| 196 | block_acct_done(blk_get_stats(s->blk), &req->acct); |
| 197 | } |
| 198 | virtio_blk_free_request(req); |
| 199 | |
| 200 | out: |
| 201 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
| 202 | } |
| 203 | |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 204 | #ifdef __linux__ |
| 205 | |
| 206 | typedef struct { |
| 207 | VirtIOBlockReq *req; |
| 208 | struct sg_io_hdr hdr; |
| 209 | } VirtIOBlockIoctlReq; |
| 210 | |
| 211 | static void virtio_blk_ioctl_complete(void *opaque, int status) |
| 212 | { |
| 213 | VirtIOBlockIoctlReq *ioctl_req = opaque; |
| 214 | VirtIOBlockReq *req = ioctl_req->req; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 215 | VirtIOBlock *s = req->dev; |
| 216 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 217 | struct virtio_scsi_inhdr *scsi; |
| 218 | struct sg_io_hdr *hdr; |
| 219 | |
| 220 | scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; |
| 221 | |
| 222 | if (status) { |
| 223 | status = VIRTIO_BLK_S_UNSUPP; |
| 224 | virtio_stl_p(vdev, &scsi->errors, 255); |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | hdr = &ioctl_req->hdr; |
| 229 | /* |
| 230 | * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi) |
| 231 | * clear the masked_status field [hence status gets cleared too, see |
| 232 | * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED |
| 233 | * status has occurred. However they do set DRIVER_SENSE in driver_status |
| 234 | * field. Also a (sb_len_wr > 0) indicates there is a sense buffer. |
| 235 | */ |
| 236 | if (hdr->status == 0 && hdr->sb_len_wr > 0) { |
| 237 | hdr->status = CHECK_CONDITION; |
| 238 | } |
| 239 | |
| 240 | virtio_stl_p(vdev, &scsi->errors, |
| 241 | hdr->status | (hdr->msg_status << 8) | |
| 242 | (hdr->host_status << 16) | (hdr->driver_status << 24)); |
| 243 | virtio_stl_p(vdev, &scsi->residual, hdr->resid); |
| 244 | virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr); |
| 245 | virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len); |
| 246 | |
| 247 | out: |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 248 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 249 | virtio_blk_req_complete(req, status); |
| 250 | virtio_blk_free_request(req); |
Paolo Bonzini | b9e413d | 2017-02-13 14:52:32 +0100 | [diff] [blame] | 251 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 252 | g_free(ioctl_req); |
| 253 | } |
| 254 | |
| 255 | #endif |
| 256 | |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 257 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 258 | { |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 259 | VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 260 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 261 | if (req) { |
Stefan Hajnoczi | edaffd9 | 2016-06-21 13:13:13 +0100 | [diff] [blame] | 262 | virtio_blk_init_request(s, vq, req); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 263 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 264 | return req; |
| 265 | } |
| 266 | |
Fam Zheng | 75344fa | 2015-01-20 11:28:46 +0800 | [diff] [blame] | 267 | static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req) |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 268 | { |
Stefan Weil | 47ce9ef | 2012-05-22 23:23:32 +0200 | [diff] [blame] | 269 | int status = VIRTIO_BLK_S_OK; |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 270 | struct virtio_scsi_inhdr *scsi = NULL; |
Fam Zheng | 75344fa | 2015-01-20 11:28:46 +0800 | [diff] [blame] | 271 | VirtIOBlock *blk = req->dev; |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 272 | VirtIODevice *vdev = VIRTIO_DEVICE(blk); |
| 273 | VirtQueueElement *elem = &req->elem; |
Rusty Russell | 783d189 | 2014-06-24 19:43:44 +0200 | [diff] [blame] | 274 | |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 275 | #ifdef __linux__ |
| 276 | int i; |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 277 | VirtIOBlockIoctlReq *ioctl_req; |
Fam Zheng | a209f46 | 2015-02-17 17:55:53 +0800 | [diff] [blame] | 278 | BlockAIOCB *acb; |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 279 | #endif |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | * We require at least one output segment each for the virtio_blk_outhdr |
| 283 | * and the SCSI command block. |
| 284 | * |
| 285 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr |
| 286 | * and the sense buffer pointer in the input segments. |
| 287 | */ |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 288 | if (elem->out_num < 2 || elem->in_num < 3) { |
| 289 | status = VIRTIO_BLK_S_IOERR; |
| 290 | goto fail; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 294 | * The scsi inhdr is placed in the second-to-last input segment, just |
| 295 | * before the regular inhdr. |
| 296 | */ |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 297 | scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 298 | |
Stefano Garzarella | bbe8bd4 | 2019-02-21 11:33:06 +0100 | [diff] [blame] | 299 | if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) { |
Paolo Bonzini | f34e73c | 2012-05-16 12:54:03 +0200 | [diff] [blame] | 300 | status = VIRTIO_BLK_S_UNSUPP; |
| 301 | goto fail; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * No support for bidirection commands yet. |
| 306 | */ |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 307 | if (elem->out_num > 2 && elem->in_num > 3) { |
Paolo Bonzini | f34e73c | 2012-05-16 12:54:03 +0200 | [diff] [blame] | 308 | status = VIRTIO_BLK_S_UNSUPP; |
| 309 | goto fail; |
| 310 | } |
| 311 | |
| 312 | #ifdef __linux__ |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 313 | ioctl_req = g_new0(VirtIOBlockIoctlReq, 1); |
| 314 | ioctl_req->req = req; |
| 315 | ioctl_req->hdr.interface_id = 'S'; |
| 316 | ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len; |
| 317 | ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base; |
| 318 | ioctl_req->hdr.dxfer_len = 0; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 319 | |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 320 | if (elem->out_num > 2) { |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 321 | /* |
| 322 | * If there are more than the minimally required 2 output segments |
| 323 | * there is write payload starting from the third iovec. |
| 324 | */ |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 325 | ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV; |
| 326 | ioctl_req->hdr.iovec_count = elem->out_num - 2; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 327 | |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 328 | for (i = 0; i < ioctl_req->hdr.iovec_count; i++) { |
| 329 | ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len; |
| 330 | } |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 331 | |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 332 | ioctl_req->hdr.dxferp = elem->out_sg + 2; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 333 | |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 334 | } else if (elem->in_num > 3) { |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 335 | /* |
| 336 | * If we have more than 3 input segments the guest wants to actually |
| 337 | * read data. |
| 338 | */ |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 339 | ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV; |
| 340 | ioctl_req->hdr.iovec_count = elem->in_num - 3; |
| 341 | for (i = 0; i < ioctl_req->hdr.iovec_count; i++) { |
| 342 | ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len; |
| 343 | } |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 344 | |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 345 | ioctl_req->hdr.dxferp = elem->in_sg; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 346 | } else { |
| 347 | /* |
| 348 | * Some SCSI commands don't actually transfer any data. |
| 349 | */ |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 350 | ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 353 | ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base; |
| 354 | ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 355 | |
Fam Zheng | a209f46 | 2015-02-17 17:55:53 +0800 | [diff] [blame] | 356 | acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr, |
| 357 | virtio_blk_ioctl_complete, ioctl_req); |
| 358 | if (!acb) { |
| 359 | g_free(ioctl_req); |
| 360 | status = VIRTIO_BLK_S_UNSUPP; |
| 361 | goto fail; |
| 362 | } |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 363 | return -EINPROGRESS; |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 364 | #else |
Paolo Bonzini | f34e73c | 2012-05-16 12:54:03 +0200 | [diff] [blame] | 365 | abort(); |
| 366 | #endif |
| 367 | |
| 368 | fail: |
| 369 | /* Just put anything nonzero so that the ioctl fails in the guest. */ |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 370 | if (scsi) { |
Rusty Russell | 783d189 | 2014-06-24 19:43:44 +0200 | [diff] [blame] | 371 | virtio_stl_p(vdev, &scsi->errors, 255); |
Fam Zheng | 5a05cbe | 2014-05-22 16:22:42 +0800 | [diff] [blame] | 372 | } |
| 373 | return status; |
| 374 | } |
| 375 | |
| 376 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
| 377 | { |
| 378 | int status; |
| 379 | |
Fam Zheng | 75344fa | 2015-01-20 11:28:46 +0800 | [diff] [blame] | 380 | status = virtio_blk_handle_scsi_req(req); |
Fam Zheng | 1dc936a | 2015-01-20 11:28:47 +0800 | [diff] [blame] | 381 | if (status != -EINPROGRESS) { |
| 382 | virtio_blk_req_complete(req, status); |
| 383 | virtio_blk_free_request(req); |
| 384 | } |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 385 | } |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 386 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 387 | static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb, |
| 388 | int start, int num_reqs, int niov) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 389 | { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 390 | QEMUIOVector *qiov = &mrb->reqs[start]->qiov; |
| 391 | int64_t sector_num = mrb->reqs[start]->sector_num; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 392 | bool is_write = mrb->is_write; |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 393 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 394 | if (num_reqs > 1) { |
| 395 | int i; |
| 396 | struct iovec *tmp_iov = qiov->iov; |
| 397 | int tmp_niov = qiov->niov; |
| 398 | |
| 399 | /* mrb->reqs[start]->qiov was initialized from external so we can't |
Eric Blake | b5772fd | 2016-05-06 10:26:33 -0600 | [diff] [blame] | 400 | * modify it here. We need to initialize it locally and then add the |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 401 | * external iovecs. */ |
| 402 | qemu_iovec_init(qiov, niov); |
| 403 | |
| 404 | for (i = 0; i < tmp_niov; i++) { |
| 405 | qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len); |
| 406 | } |
| 407 | |
| 408 | for (i = start + 1; i < start + num_reqs; i++) { |
| 409 | qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0, |
| 410 | mrb->reqs[i]->qiov.size); |
| 411 | mrb->reqs[i - 1]->mr_next = mrb->reqs[i]; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 412 | } |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 413 | |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 414 | trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev), |
| 415 | mrb, start, num_reqs, |
Eric Blake | b5772fd | 2016-05-06 10:26:33 -0600 | [diff] [blame] | 416 | sector_num << BDRV_SECTOR_BITS, |
| 417 | qiov->size, is_write); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 418 | block_acct_merge_done(blk_get_stats(blk), |
| 419 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ, |
| 420 | num_reqs - 1); |
| 421 | } |
| 422 | |
| 423 | if (is_write) { |
Eric Blake | b5772fd | 2016-05-06 10:26:33 -0600 | [diff] [blame] | 424 | blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, |
| 425 | virtio_blk_rw_complete, mrb->reqs[start]); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 426 | } else { |
Eric Blake | b5772fd | 2016-05-06 10:26:33 -0600 | [diff] [blame] | 427 | blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, |
| 428 | virtio_blk_rw_complete, mrb->reqs[start]); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | static int multireq_compare(const void *a, const void *b) |
| 433 | { |
| 434 | const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a, |
| 435 | *req2 = *(VirtIOBlockReq **)b; |
| 436 | |
| 437 | /* |
| 438 | * Note that we can't simply subtract sector_num1 from sector_num2 |
| 439 | * here as that could overflow the return value. |
| 440 | */ |
| 441 | if (req1->sector_num > req2->sector_num) { |
| 442 | return 1; |
| 443 | } else if (req1->sector_num < req2->sector_num) { |
| 444 | return -1; |
| 445 | } else { |
| 446 | return 0; |
| 447 | } |
| 448 | } |
| 449 | |
Greg Kurz | d14dde5 | 2016-09-30 17:12:50 +0200 | [diff] [blame] | 450 | static void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb) |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 451 | { |
| 452 | int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0; |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 453 | uint32_t max_transfer; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 454 | int64_t sector_num = 0; |
| 455 | |
| 456 | if (mrb->num_reqs == 1) { |
| 457 | submit_requests(blk, mrb, 0, 1, -1); |
| 458 | mrb->num_reqs = 0; |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 462 | max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 463 | |
| 464 | qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs), |
| 465 | &multireq_compare); |
| 466 | |
| 467 | for (i = 0; i < mrb->num_reqs; i++) { |
| 468 | VirtIOBlockReq *req = mrb->reqs[i]; |
| 469 | if (num_reqs > 0) { |
Gonglei | 49cffbc | 2015-11-11 09:59:26 +0800 | [diff] [blame] | 470 | /* |
| 471 | * NOTE: We cannot merge the requests in below situations: |
| 472 | * 1. requests are not sequential |
| 473 | * 2. merge would exceed maximum number of IOVs |
| 474 | * 3. merge would exceed maximum transfer length of backend device |
| 475 | */ |
| 476 | if (sector_num + nb_sectors != req->sector_num || |
Stefan Hajnoczi | 222565f | 2015-07-09 10:56:46 +0100 | [diff] [blame] | 477 | niov > blk_get_max_iov(blk) - req->qiov.niov || |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 478 | req->qiov.size > max_transfer || |
| 479 | nb_sectors > (max_transfer - |
| 480 | req->qiov.size) / BDRV_SECTOR_SIZE) { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 481 | submit_requests(blk, mrb, start, num_reqs, niov); |
| 482 | num_reqs = 0; |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 485 | |
| 486 | if (num_reqs == 0) { |
| 487 | sector_num = req->sector_num; |
| 488 | nb_sectors = niov = 0; |
| 489 | start = i; |
| 490 | } |
| 491 | |
| 492 | nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE; |
| 493 | niov += req->qiov.niov; |
| 494 | num_reqs++; |
Christoph Hellwig | 87b245d | 2009-08-13 16:49:56 +0200 | [diff] [blame] | 495 | } |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 496 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 497 | submit_requests(blk, mrb, start, num_reqs, niov); |
| 498 | mrb->num_reqs = 0; |
aliguori | d28a1b6 | 2009-03-28 17:46:14 +0000 | [diff] [blame] | 499 | } |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 500 | |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 501 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 502 | { |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 503 | VirtIOBlock *s = req->dev; |
| 504 | |
| 505 | block_acct_start(blk_get_stats(s->blk), &req->acct, 0, |
Benoît Canet | 5366d0c | 2014-09-05 15:46:18 +0200 | [diff] [blame] | 506 | BLOCK_ACCT_FLUSH); |
Christoph Hellwig | a597e79 | 2011-08-25 08:26:01 +0200 | [diff] [blame] | 507 | |
Christoph Hellwig | 618fbb8 | 2010-05-19 12:40:09 +0200 | [diff] [blame] | 508 | /* |
| 509 | * Make sure all outstanding writes are posted to the backing device. |
| 510 | */ |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 511 | if (mrb->is_write && mrb->num_reqs > 0) { |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 512 | virtio_blk_submit_multireq(s->blk, mrb); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 513 | } |
Anastasiia Rusakova | bf4069f | 2019-03-07 18:19:25 +0200 | [diff] [blame] | 514 | blk_aio_flush(s->blk, virtio_blk_flush_complete, req); |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Markus Armbruster | d0e1437 | 2014-07-09 19:07:29 +0200 | [diff] [blame] | 517 | static bool virtio_blk_sect_range_ok(VirtIOBlock *dev, |
| 518 | uint64_t sector, size_t size) |
| 519 | { |
Markus Armbruster | 3c2daac | 2014-07-09 19:07:31 +0200 | [diff] [blame] | 520 | uint64_t nb_sectors = size >> BDRV_SECTOR_BITS; |
| 521 | uint64_t total_sectors; |
| 522 | |
Peter Lieven | 75af1f3 | 2015-02-06 11:54:11 +0100 | [diff] [blame] | 523 | if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 524 | return false; |
| 525 | } |
Markus Armbruster | d0e1437 | 2014-07-09 19:07:29 +0200 | [diff] [blame] | 526 | if (sector & dev->sector_mask) { |
| 527 | return false; |
| 528 | } |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 529 | if (size % dev->conf.conf.logical_block_size) { |
Markus Armbruster | d0e1437 | 2014-07-09 19:07:29 +0200 | [diff] [blame] | 530 | return false; |
| 531 | } |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 532 | blk_get_geometry(dev->blk, &total_sectors); |
Markus Armbruster | 3c2daac | 2014-07-09 19:07:31 +0200 | [diff] [blame] | 533 | if (sector > total_sectors || nb_sectors > total_sectors - sector) { |
| 534 | return false; |
| 535 | } |
Markus Armbruster | d0e1437 | 2014-07-09 19:07:29 +0200 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 539 | static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req, |
| 540 | struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes) |
| 541 | { |
| 542 | VirtIOBlock *s = req->dev; |
| 543 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
| 544 | uint64_t sector; |
| 545 | uint32_t num_sectors, flags, max_sectors; |
| 546 | uint8_t err_status; |
| 547 | int bytes; |
| 548 | |
| 549 | sector = virtio_ldq_p(vdev, &dwz_hdr->sector); |
| 550 | num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors); |
| 551 | flags = virtio_ldl_p(vdev, &dwz_hdr->flags); |
| 552 | max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors : |
| 553 | s->conf.max_discard_sectors; |
| 554 | |
| 555 | /* |
| 556 | * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check |
| 557 | * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in |
| 558 | * the integer variable. |
| 559 | */ |
| 560 | if (unlikely(num_sectors > max_sectors)) { |
| 561 | err_status = VIRTIO_BLK_S_IOERR; |
| 562 | goto err; |
| 563 | } |
| 564 | |
| 565 | bytes = num_sectors << BDRV_SECTOR_BITS; |
| 566 | |
| 567 | if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) { |
| 568 | err_status = VIRTIO_BLK_S_IOERR; |
| 569 | goto err; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard |
| 574 | * and write zeroes commands if any unknown flag is set. |
| 575 | */ |
| 576 | if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { |
| 577 | err_status = VIRTIO_BLK_S_UNSUPP; |
| 578 | goto err; |
| 579 | } |
| 580 | |
| 581 | if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */ |
| 582 | int blk_aio_flags = 0; |
| 583 | |
| 584 | if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { |
| 585 | blk_aio_flags |= BDRV_REQ_MAY_UNMAP; |
| 586 | } |
| 587 | |
| 588 | block_acct_start(blk_get_stats(s->blk), &req->acct, bytes, |
| 589 | BLOCK_ACCT_WRITE); |
| 590 | |
| 591 | blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS, |
| 592 | bytes, blk_aio_flags, |
| 593 | virtio_blk_discard_write_zeroes_complete, req); |
| 594 | } else { /* VIRTIO_BLK_T_DISCARD */ |
| 595 | /* |
| 596 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for |
| 597 | * discard commands if the unmap flag is set. |
| 598 | */ |
| 599 | if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { |
| 600 | err_status = VIRTIO_BLK_S_UNSUPP; |
| 601 | goto err; |
| 602 | } |
| 603 | |
| 604 | blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes, |
| 605 | virtio_blk_discard_write_zeroes_complete, req); |
| 606 | } |
| 607 | |
| 608 | return VIRTIO_BLK_S_OK; |
| 609 | |
| 610 | err: |
| 611 | if (is_write_zeroes) { |
| 612 | block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE); |
| 613 | } |
| 614 | return err_status; |
| 615 | } |
| 616 | |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 617 | static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 618 | { |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 619 | uint32_t type; |
Stefan Hajnoczi | f897bf7 | 2014-07-09 10:05:49 +0200 | [diff] [blame] | 620 | struct iovec *in_iov = req->elem.in_sg; |
Dongli Zhang | 5636da7 | 2018-11-07 00:09:16 +0800 | [diff] [blame] | 621 | struct iovec *out_iov = req->elem.out_sg; |
Stefan Hajnoczi | f897bf7 | 2014-07-09 10:05:49 +0200 | [diff] [blame] | 622 | unsigned in_num = req->elem.in_num; |
| 623 | unsigned out_num = req->elem.out_num; |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 624 | VirtIOBlock *s = req->dev; |
| 625 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 626 | |
Stefan Hajnoczi | f897bf7 | 2014-07-09 10:05:49 +0200 | [diff] [blame] | 627 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 628 | virtio_error(vdev, "virtio-blk missing headers"); |
| 629 | return -1; |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Dongli Zhang | 5636da7 | 2018-11-07 00:09:16 +0800 | [diff] [blame] | 632 | if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out, |
Fam Zheng | 827805a | 2014-06-11 12:11:48 +0800 | [diff] [blame] | 633 | sizeof(req->out)) != sizeof(req->out))) { |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 634 | virtio_error(vdev, "virtio-blk request outhdr too short"); |
| 635 | return -1; |
Fam Zheng | 827805a | 2014-06-11 12:11:48 +0800 | [diff] [blame] | 636 | } |
Fam Zheng | ee17e84 | 2014-06-11 12:11:50 +0800 | [diff] [blame] | 637 | |
Stefan Hajnoczi | 7bd04a0 | 2020-09-17 10:44:54 +0100 | [diff] [blame] | 638 | iov_discard_front_undoable(&out_iov, &out_num, sizeof(req->out), |
| 639 | &req->outhdr_undo); |
Fam Zheng | ee17e84 | 2014-06-11 12:11:50 +0800 | [diff] [blame] | 640 | |
Gonglei | 1204854 | 2015-06-24 17:29:24 +0800 | [diff] [blame] | 641 | if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 642 | virtio_error(vdev, "virtio-blk request inhdr too short"); |
Stefan Hajnoczi | 7bd04a0 | 2020-09-17 10:44:54 +0100 | [diff] [blame] | 643 | iov_discard_undo(&req->outhdr_undo); |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 644 | return -1; |
Fam Zheng | ee17e84 | 2014-06-11 12:11:50 +0800 | [diff] [blame] | 645 | } |
| 646 | |
Paolo Bonzini | 2a6cdd6 | 2015-04-02 19:50:44 +0200 | [diff] [blame] | 647 | /* We always touch the last byte, so just see how big in_iov is. */ |
| 648 | req->in_len = iov_size(in_iov, in_num); |
Fam Zheng | ee17e84 | 2014-06-11 12:11:50 +0800 | [diff] [blame] | 649 | req->in = (void *)in_iov[in_num - 1].iov_base |
| 650 | + in_iov[in_num - 1].iov_len |
| 651 | - sizeof(struct virtio_blk_inhdr); |
Stefan Hajnoczi | 7bd04a0 | 2020-09-17 10:44:54 +0100 | [diff] [blame] | 652 | iov_discard_back_undoable(in_iov, &in_num, sizeof(struct virtio_blk_inhdr), |
| 653 | &req->inhdr_undo); |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 654 | |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 655 | type = virtio_ldl_p(vdev, &req->out.type); |
Aurelien Jarno | 92e3c2a | 2011-01-25 11:55:14 +0100 | [diff] [blame] | 656 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 657 | /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER |
Stefan Weil | 631b22e | 2015-04-09 20:32:39 +0200 | [diff] [blame] | 658 | * is an optional flag. Although a guest should not send this flag if |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 659 | * not negotiated we ignored it in the past. So keep ignoring it. */ |
| 660 | switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) { |
| 661 | case VIRTIO_BLK_T_IN: |
| 662 | { |
| 663 | bool is_write = type & VIRTIO_BLK_T_OUT; |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 664 | req->sector_num = virtio_ldq_p(vdev, &req->out.sector); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 665 | |
| 666 | if (is_write) { |
Dongli Zhang | 5636da7 | 2018-11-07 00:09:16 +0800 | [diff] [blame] | 667 | qemu_iovec_init_external(&req->qiov, out_iov, out_num); |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 668 | trace_virtio_blk_handle_write(vdev, req, req->sector_num, |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 669 | req->qiov.size / BDRV_SECTOR_SIZE); |
| 670 | } else { |
| 671 | qemu_iovec_init_external(&req->qiov, in_iov, in_num); |
Stefan Hajnoczi | a576cea | 2017-06-14 10:29:30 +0100 | [diff] [blame] | 672 | trace_virtio_blk_handle_read(vdev, req, req->sector_num, |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 673 | req->qiov.size / BDRV_SECTOR_SIZE); |
| 674 | } |
| 675 | |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 676 | if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) { |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 677 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 678 | block_acct_invalid(blk_get_stats(s->blk), |
Alberto Garcia | 01762e0 | 2015-10-28 17:33:12 +0200 | [diff] [blame] | 679 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 680 | virtio_blk_free_request(req); |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 681 | return 0; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 684 | block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size, |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 685 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ); |
| 686 | |
| 687 | /* merge would exceed maximum number of requests or IO direction |
| 688 | * changes */ |
| 689 | if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS || |
Peter Lieven | c99495a | 2015-02-02 14:52:22 +0100 | [diff] [blame] | 690 | is_write != mrb->is_write || |
Stefano Garzarella | 9a6719d | 2019-02-08 15:23:47 +0100 | [diff] [blame] | 691 | !s->conf.request_merging)) { |
| 692 | virtio_blk_submit_multireq(s->blk, mrb); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS); |
| 696 | mrb->reqs[mrb->num_reqs++] = req; |
| 697 | mrb->is_write = is_write; |
| 698 | break; |
| 699 | } |
| 700 | case VIRTIO_BLK_T_FLUSH: |
Christoph Hellwig | c20fd87 | 2010-06-08 18:26:07 +0200 | [diff] [blame] | 701 | virtio_blk_handle_flush(req, mrb); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 702 | break; |
| 703 | case VIRTIO_BLK_T_SCSI_CMD: |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 704 | virtio_blk_handle_scsi(req); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 705 | break; |
| 706 | case VIRTIO_BLK_T_GET_ID: |
| 707 | { |
Markus Armbruster | a8686a9 | 2011-06-20 11:35:18 +0200 | [diff] [blame] | 708 | /* |
| 709 | * NB: per existing s/n string convention the string is |
| 710 | * terminated by '\0' only when shorter than buffer. |
| 711 | */ |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 712 | const char *serial = s->conf.serial ? s->conf.serial : ""; |
Marc Marí | a83ceea | 2014-08-12 13:41:51 +0200 | [diff] [blame] | 713 | size_t size = MIN(strlen(serial) + 1, |
| 714 | MIN(iov_size(in_iov, in_num), |
| 715 | VIRTIO_BLK_ID_BYTES)); |
| 716 | iov_from_buf(in_iov, in_num, 0, serial, size); |
john cooper | 2930b31 | 2010-07-02 13:44:25 -0400 | [diff] [blame] | 717 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 718 | virtio_blk_free_request(req); |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 719 | break; |
| 720 | } |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 721 | /* |
| 722 | * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with |
| 723 | * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement, |
| 724 | * so we must mask it for these requests, then we will check if it is set. |
| 725 | */ |
| 726 | case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT: |
| 727 | case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT: |
| 728 | { |
| 729 | struct virtio_blk_discard_write_zeroes dwz_hdr; |
| 730 | size_t out_len = iov_size(out_iov, out_num); |
| 731 | bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) == |
| 732 | VIRTIO_BLK_T_WRITE_ZEROES; |
| 733 | uint8_t err_status; |
| 734 | |
| 735 | /* |
| 736 | * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains |
| 737 | * more than one segment. |
| 738 | */ |
| 739 | if (unlikely(!(type & VIRTIO_BLK_T_OUT) || |
| 740 | out_len > sizeof(dwz_hdr))) { |
| 741 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
| 742 | virtio_blk_free_request(req); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr, |
| 747 | sizeof(dwz_hdr)) != sizeof(dwz_hdr))) { |
Stefan Hajnoczi | 7bd04a0 | 2020-09-17 10:44:54 +0100 | [diff] [blame] | 748 | iov_discard_undo(&req->inhdr_undo); |
| 749 | iov_discard_undo(&req->outhdr_undo); |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 750 | virtio_error(vdev, "virtio-blk discard/write_zeroes header" |
| 751 | " too short"); |
| 752 | return -1; |
| 753 | } |
| 754 | |
| 755 | err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr, |
| 756 | is_write_zeroes); |
| 757 | if (err_status != VIRTIO_BLK_S_OK) { |
| 758 | virtio_blk_req_complete(req, err_status); |
| 759 | virtio_blk_free_request(req); |
| 760 | } |
| 761 | |
| 762 | break; |
| 763 | } |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 764 | default: |
Alexey Zaytsev | 9e72c45 | 2012-12-13 09:03:43 +0200 | [diff] [blame] | 765 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
Fam Zheng | 671ec3f | 2014-06-11 12:11:43 +0800 | [diff] [blame] | 766 | virtio_blk_free_request(req); |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 767 | } |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 768 | return 0; |
Kevin Wolf | bc6694d | 2010-01-27 13:12:34 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Stefan Hajnoczi | 186b969 | 2021-12-07 13:23:33 +0000 | [diff] [blame] | 771 | void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 772 | { |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 773 | VirtIOBlockReq *req; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 774 | MultiReqBuffer mrb = {}; |
Stefan Hajnoczi | d0435bc | 2019-12-09 21:09:57 +0000 | [diff] [blame] | 775 | bool suppress_notifications = virtio_queue_get_notification(vq); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 776 | |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 777 | aio_context_acquire(blk_get_aio_context(s->blk)); |
Stefan Hajnoczi | fc73548 | 2015-07-20 16:54:16 +0100 | [diff] [blame] | 778 | blk_io_plug(s->blk); |
| 779 | |
Stefan Hajnoczi | 9ef9d40 | 2016-12-01 19:26:46 +0000 | [diff] [blame] | 780 | do { |
Stefan Hajnoczi | d0435bc | 2019-12-09 21:09:57 +0000 | [diff] [blame] | 781 | if (suppress_notifications) { |
| 782 | virtio_queue_set_notification(vq, 0); |
| 783 | } |
Stefan Hajnoczi | 9ef9d40 | 2016-12-01 19:26:46 +0000 | [diff] [blame] | 784 | |
| 785 | while ((req = virtio_blk_get_request(s, vq))) { |
| 786 | if (virtio_blk_handle_request(req, &mrb)) { |
| 787 | virtqueue_detach_element(req->vq, &req->elem, 0); |
| 788 | virtio_blk_free_request(req); |
| 789 | break; |
| 790 | } |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 791 | } |
Stefan Hajnoczi | 9ef9d40 | 2016-12-01 19:26:46 +0000 | [diff] [blame] | 792 | |
Stefan Hajnoczi | d0435bc | 2019-12-09 21:09:57 +0000 | [diff] [blame] | 793 | if (suppress_notifications) { |
| 794 | virtio_queue_set_notification(vq, 1); |
| 795 | } |
Stefan Hajnoczi | 9ef9d40 | 2016-12-01 19:26:46 +0000 | [diff] [blame] | 796 | } while (!virtio_queue_empty(vq)); |
Kevin Wolf | 91553dc | 2009-09-09 17:53:38 +0200 | [diff] [blame] | 797 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 798 | if (mrb.num_reqs) { |
| 799 | virtio_blk_submit_multireq(s->blk, &mrb); |
| 800 | } |
Stefan Hajnoczi | fc73548 | 2015-07-20 16:54:16 +0100 | [diff] [blame] | 801 | |
| 802 | blk_io_unplug(s->blk); |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 803 | aio_context_release(blk_get_aio_context(s->blk)); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Michael S. Tsirkin | 8a2fad5 | 2016-04-06 12:16:26 +0200 | [diff] [blame] | 806 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 807 | { |
| 808 | VirtIOBlock *s = (VirtIOBlock *)vdev; |
| 809 | |
Stefan Hajnoczi | 186b969 | 2021-12-07 13:23:33 +0000 | [diff] [blame] | 810 | if (s->dataplane && !s->dataplane_started) { |
Michael S. Tsirkin | 8a2fad5 | 2016-04-06 12:16:26 +0200 | [diff] [blame] | 811 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start |
| 812 | * dataplane here instead of waiting for .set_status(). |
| 813 | */ |
Paolo Bonzini | 9ffe337 | 2016-10-21 22:48:09 +0200 | [diff] [blame] | 814 | virtio_device_start_ioeventfd(vdev); |
Michael S. Tsirkin | 8a2fad5 | 2016-04-06 12:16:26 +0200 | [diff] [blame] | 815 | if (!s->dataplane_disabled) { |
| 816 | return; |
| 817 | } |
| 818 | } |
Stefan Hajnoczi | 186b969 | 2021-12-07 13:23:33 +0000 | [diff] [blame] | 819 | virtio_blk_handle_vq(s, vq); |
Michael S. Tsirkin | 8a2fad5 | 2016-04-06 12:16:26 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Sergio Lopez | 49b4454 | 2020-06-03 11:32:40 +0200 | [diff] [blame] | 822 | void virtio_blk_process_queued_requests(VirtIOBlock *s, bool is_bh) |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 823 | { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 824 | VirtIOBlockReq *req = s->rq; |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 825 | MultiReqBuffer mrb = {}; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 826 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 827 | s->rq = NULL; |
| 828 | |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 829 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 830 | while (req) { |
zhanghailiang | 1bdb176 | 2014-08-18 15:42:50 +0800 | [diff] [blame] | 831 | VirtIOBlockReq *next = req->next; |
Greg Kurz | 20ea686 | 2016-09-30 17:13:07 +0200 | [diff] [blame] | 832 | if (virtio_blk_handle_request(req, &mrb)) { |
| 833 | /* Device is now broken and won't do any processing until it gets |
| 834 | * reset. Already queued requests will be lost: let's purge them. |
| 835 | */ |
| 836 | while (req) { |
| 837 | next = req->next; |
| 838 | virtqueue_detach_element(req->vq, &req->elem, 0); |
| 839 | virtio_blk_free_request(req); |
| 840 | req = next; |
| 841 | } |
| 842 | break; |
| 843 | } |
zhanghailiang | 1bdb176 | 2014-08-18 15:42:50 +0800 | [diff] [blame] | 844 | req = next; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 845 | } |
Kevin Wolf | f1b5286 | 2010-01-27 13:12:35 +0100 | [diff] [blame] | 846 | |
Peter Lieven | 95f7142 | 2015-02-02 14:52:21 +0100 | [diff] [blame] | 847 | if (mrb.num_reqs) { |
| 848 | virtio_blk_submit_multireq(s->blk, &mrb); |
| 849 | } |
Sergio Lopez | 49b4454 | 2020-06-03 11:32:40 +0200 | [diff] [blame] | 850 | if (is_bh) { |
| 851 | blk_dec_in_flight(s->conf.conf.blk); |
| 852 | } |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 853 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Sergio Lopez | 7aa1c24 | 2020-06-03 11:32:39 +0200 | [diff] [blame] | 856 | static void virtio_blk_dma_restart_bh(void *opaque) |
| 857 | { |
| 858 | VirtIOBlock *s = opaque; |
| 859 | |
| 860 | qemu_bh_delete(s->bh); |
| 861 | s->bh = NULL; |
| 862 | |
Sergio Lopez | 49b4454 | 2020-06-03 11:32:40 +0200 | [diff] [blame] | 863 | virtio_blk_process_queued_requests(s, true); |
Sergio Lopez | 7aa1c24 | 2020-06-03 11:32:39 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Philippe Mathieu-Daudé | 538f049 | 2021-01-11 16:20:20 +0100 | [diff] [blame] | 866 | static void virtio_blk_dma_restart_cb(void *opaque, bool running, |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 867 | RunState state) |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 868 | { |
| 869 | VirtIOBlock *s = opaque; |
Sergio Lopez | 49b4454 | 2020-06-03 11:32:40 +0200 | [diff] [blame] | 870 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s))); |
| 871 | VirtioBusState *bus = VIRTIO_BUS(qbus); |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 872 | |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 873 | if (!running) { |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 874 | return; |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 875 | } |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 876 | |
Sergio Lopez | 49b4454 | 2020-06-03 11:32:40 +0200 | [diff] [blame] | 877 | /* |
| 878 | * If ioeventfd is enabled, don't schedule the BH here as queued |
| 879 | * requests will be processed while starting the data plane. |
| 880 | */ |
| 881 | if (!s->bh && !virtio_bus_ioeventfd_enabled(bus)) { |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 882 | s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk), |
Fam Zheng | 4407c1c | 2014-06-17 14:32:08 +0800 | [diff] [blame] | 883 | virtio_blk_dma_restart_bh, s); |
Kevin Wolf | 680f200 | 2019-02-14 18:51:03 +0100 | [diff] [blame] | 884 | blk_inc_in_flight(s->conf.conf.blk); |
Markus Armbruster | 213189a | 2009-07-28 14:33:41 -0400 | [diff] [blame] | 885 | qemu_bh_schedule(s->bh); |
| 886 | } |
| 887 | } |
| 888 | |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 889 | static void virtio_blk_reset(VirtIODevice *vdev) |
| 890 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 891 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Alexander Yarygin | 6e40b3b | 2015-06-17 13:37:20 +0300 | [diff] [blame] | 892 | AioContext *ctx; |
Fam Zheng | 26307f6 | 2016-08-04 10:44:13 +0800 | [diff] [blame] | 893 | VirtIOBlockReq *req; |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 894 | |
Alexander Yarygin | 6e40b3b | 2015-06-17 13:37:20 +0300 | [diff] [blame] | 895 | ctx = blk_get_aio_context(s->blk); |
| 896 | aio_context_acquire(ctx); |
| 897 | blk_drain(s->blk); |
| 898 | |
Fam Zheng | 26307f6 | 2016-08-04 10:44:13 +0800 | [diff] [blame] | 899 | /* We drop queued requests after blk_drain() because blk_drain() itself can |
| 900 | * produce them. */ |
| 901 | while (s->rq) { |
| 902 | req = s->rq; |
| 903 | s->rq = req->next; |
Stefan Hajnoczi | 97b93c8 | 2016-09-19 14:28:04 +0100 | [diff] [blame] | 904 | virtqueue_detach_element(req->vq, &req->elem, 0); |
Fam Zheng | 26307f6 | 2016-08-04 10:44:13 +0800 | [diff] [blame] | 905 | virtio_blk_free_request(req); |
| 906 | } |
| 907 | |
Alexander Yarygin | 6e40b3b | 2015-06-17 13:37:20 +0300 | [diff] [blame] | 908 | aio_context_release(ctx); |
| 909 | |
Paolo Bonzini | 9ffe337 | 2016-10-21 22:48:09 +0200 | [diff] [blame] | 910 | assert(!s->dataplane_started); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 911 | blk_set_enable_write_cache(s->blk, s->original_wce); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 912 | } |
| 913 | |
john cooper | bf01129 | 2009-06-22 14:26:51 -0400 | [diff] [blame] | 914 | /* coalesce internal state, copy to pci i/o region 0 |
| 915 | */ |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 916 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
| 917 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 918 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 919 | BlockConf *conf = &s->conf.conf; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 920 | struct virtio_blk_config blkcfg; |
| 921 | uint64_t capacity; |
Stefan Hajnoczi | 17d0bc0 | 2017-08-08 13:22:51 +0100 | [diff] [blame] | 922 | int64_t length; |
Markus Armbruster | f751673 | 2014-10-07 13:59:16 +0200 | [diff] [blame] | 923 | int blk_size = conf->logical_block_size; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 924 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 925 | blk_get_geometry(s->blk, &capacity); |
Gerd Hoffmann | 5c5dafd | 2009-06-12 09:50:18 +0200 | [diff] [blame] | 926 | memset(&blkcfg, 0, sizeof(blkcfg)); |
Rusty Russell | 783d189 | 2014-06-24 19:43:44 +0200 | [diff] [blame] | 927 | virtio_stq_p(vdev, &blkcfg.capacity, capacity); |
Denis Plotnikov | 1bf8a98 | 2019-12-20 17:09:04 +0300 | [diff] [blame] | 928 | virtio_stl_p(vdev, &blkcfg.seg_max, |
| 929 | s->conf.seg_max_adjust ? s->conf.queue_size - 2 : 128 - 2); |
Michael S. Tsirkin | 907eb3e | 2015-02-16 22:36:03 +0100 | [diff] [blame] | 930 | virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls); |
Rusty Russell | 783d189 | 2014-06-24 19:43:44 +0200 | [diff] [blame] | 931 | virtio_stl_p(vdev, &blkcfg.blk_size, blk_size); |
Markus Armbruster | f751673 | 2014-10-07 13:59:16 +0200 | [diff] [blame] | 932 | virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size); |
Roman Kagan | 6abee26 | 2020-05-29 01:55:09 +0300 | [diff] [blame] | 933 | virtio_stl_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size); |
Michael S. Tsirkin | 907eb3e | 2015-02-16 22:36:03 +0100 | [diff] [blame] | 934 | blkcfg.geometry.heads = conf->heads; |
Christian Borntraeger | 136be99 | 2012-05-24 13:22:55 +0200 | [diff] [blame] | 935 | /* |
| 936 | * We must ensure that the block device capacity is a multiple of |
Peter Maydell | e03ba13 | 2013-04-09 12:48:19 +0100 | [diff] [blame] | 937 | * the logical block size. If that is not the case, let's use |
Christian Borntraeger | 136be99 | 2012-05-24 13:22:55 +0200 | [diff] [blame] | 938 | * sector_mask to adopt the geometry to have a correct picture. |
| 939 | * For those devices where the capacity is ok for the given geometry |
Peter Maydell | e03ba13 | 2013-04-09 12:48:19 +0100 | [diff] [blame] | 940 | * we don't touch the sector value of the geometry, since some devices |
Christian Borntraeger | 136be99 | 2012-05-24 13:22:55 +0200 | [diff] [blame] | 941 | * (like s390 dasd) need a specific value. Here the capacity is already |
| 942 | * cyls*heads*secs*blk_size and the sector value is not block size |
| 943 | * divided by 512 - instead it is the amount of blk_size blocks |
| 944 | * per track (cylinder). |
| 945 | */ |
Stefan Hajnoczi | 17d0bc0 | 2017-08-08 13:22:51 +0100 | [diff] [blame] | 946 | length = blk_getlength(s->blk); |
| 947 | if (length > 0 && length / conf->heads / conf->secs % blk_size) { |
Michael S. Tsirkin | 907eb3e | 2015-02-16 22:36:03 +0100 | [diff] [blame] | 948 | blkcfg.geometry.sectors = conf->secs & ~s->sector_mask; |
Christian Borntraeger | 136be99 | 2012-05-24 13:22:55 +0200 | [diff] [blame] | 949 | } else { |
Michael S. Tsirkin | 907eb3e | 2015-02-16 22:36:03 +0100 | [diff] [blame] | 950 | blkcfg.geometry.sectors = conf->secs; |
Christian Borntraeger | 136be99 | 2012-05-24 13:22:55 +0200 | [diff] [blame] | 951 | } |
Blue Swirl | c7085da | 2009-06-13 13:20:25 +0000 | [diff] [blame] | 952 | blkcfg.size_max = 0; |
Markus Armbruster | f751673 | 2014-10-07 13:59:16 +0200 | [diff] [blame] | 953 | blkcfg.physical_block_exp = get_physical_block_exp(conf); |
Christoph Hellwig | 9752c37 | 2010-02-10 23:37:25 +0100 | [diff] [blame] | 954 | blkcfg.alignment_offset = 0; |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 955 | blkcfg.wce = blk_enable_write_cache(s->blk); |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 956 | virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues); |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 957 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) { |
Akihiko Odaki | fb0b154 | 2021-02-25 09:12:39 +0900 | [diff] [blame] | 958 | uint32_t discard_granularity = conf->discard_granularity; |
| 959 | if (discard_granularity == -1 || !s->conf.report_discard_granularity) { |
| 960 | discard_granularity = blk_size; |
| 961 | } |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 962 | virtio_stl_p(vdev, &blkcfg.max_discard_sectors, |
| 963 | s->conf.max_discard_sectors); |
| 964 | virtio_stl_p(vdev, &blkcfg.discard_sector_alignment, |
Akihiko Odaki | fb0b154 | 2021-02-25 09:12:39 +0900 | [diff] [blame] | 965 | discard_granularity >> BDRV_SECTOR_BITS); |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 966 | /* |
| 967 | * We support only one segment per request since multiple segments |
| 968 | * are not widely used and there are no userspace APIs that allow |
| 969 | * applications to submit multiple segments in a single call. |
| 970 | */ |
| 971 | virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1); |
| 972 | } |
| 973 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) { |
| 974 | virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors, |
| 975 | s->conf.max_write_zeroes_sectors); |
| 976 | blkcfg.write_zeroes_may_unmap = 1; |
| 977 | virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1); |
| 978 | } |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 979 | memcpy(config, &blkcfg, s->config_size); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Paolo Bonzini | 13e3dce | 2012-08-09 16:07:19 +0200 | [diff] [blame] | 982 | static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 983 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 984 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Paolo Bonzini | 13e3dce | 2012-08-09 16:07:19 +0200 | [diff] [blame] | 985 | struct virtio_blk_config blkcfg; |
| 986 | |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 987 | memcpy(&blkcfg, config, s->config_size); |
Fam Zheng | 6d7e73d | 2014-05-15 19:22:06 +0800 | [diff] [blame] | 988 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 989 | aio_context_acquire(blk_get_aio_context(s->blk)); |
| 990 | blk_set_enable_write_cache(s->blk, blkcfg.wce != 0); |
| 991 | aio_context_release(blk_get_aio_context(s->blk)); |
Paolo Bonzini | 13e3dce | 2012-08-09 16:07:19 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Jason Wang | 9d5b731 | 2015-07-27 17:49:19 +0800 | [diff] [blame] | 994 | static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features, |
| 995 | Error **errp) |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 996 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 997 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 998 | |
Stefano Garzarella | bbe8bd4 | 2019-02-21 11:33:06 +0100 | [diff] [blame] | 999 | /* Firstly sync all virtio-blk possible supported features */ |
| 1000 | features |= s->host_features; |
| 1001 | |
Cornelia Huck | 0cd09c3 | 2014-12-11 14:25:05 +0100 | [diff] [blame] | 1002 | virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); |
| 1003 | virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); |
| 1004 | virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); |
| 1005 | virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); |
Cornelia Huck | 95129d6 | 2015-08-17 11:48:29 +0200 | [diff] [blame] | 1006 | if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) { |
Stefano Garzarella | bbe8bd4 | 2019-02-21 11:33:06 +0100 | [diff] [blame] | 1007 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) { |
Jason Wang | efb8206 | 2015-07-27 17:49:20 +0800 | [diff] [blame] | 1008 | error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0"); |
| 1009 | return 0; |
| 1010 | } |
Jason Wang | efb8206 | 2015-07-27 17:49:20 +0800 | [diff] [blame] | 1011 | } else { |
Jason Wang | c9b11f9 | 2015-07-27 17:49:21 +0800 | [diff] [blame] | 1012 | virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT); |
Jason Wang | efb8206 | 2015-07-27 17:49:20 +0800 | [diff] [blame] | 1013 | virtio_add_feature(&features, VIRTIO_BLK_F_SCSI); |
| 1014 | } |
Christoph Hellwig | aa659be | 2009-09-04 19:02:23 +0200 | [diff] [blame] | 1015 | |
Evgeny Yakovlev | 5f25857 | 2019-11-05 21:22:17 +0300 | [diff] [blame] | 1016 | if (blk_enable_write_cache(s->blk) || |
| 1017 | (s->conf.x_enable_wce_if_config_wce && |
| 1018 | virtio_has_feature(features, VIRTIO_BLK_F_CONFIG_WCE))) { |
Cornelia Huck | 0cd09c3 | 2014-12-11 14:25:05 +0100 | [diff] [blame] | 1019 | virtio_add_feature(&features, VIRTIO_BLK_F_WCE); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1020 | } |
Kevin Wolf | 86b1cf3 | 2021-01-18 13:34:47 +0100 | [diff] [blame] | 1021 | if (!blk_is_writable(s->blk)) { |
Cornelia Huck | 0cd09c3 | 2014-12-11 14:25:05 +0100 | [diff] [blame] | 1022 | virtio_add_feature(&features, VIRTIO_BLK_F_RO); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1023 | } |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 1024 | if (s->conf.num_queues > 1) { |
| 1025 | virtio_add_feature(&features, VIRTIO_BLK_F_MQ); |
| 1026 | } |
Christoph Hellwig | 1063b8b | 2009-04-27 10:29:14 +0200 | [diff] [blame] | 1027 | |
| 1028 | return features; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Paolo Bonzini | 9315cbf | 2012-08-09 16:07:20 +0200 | [diff] [blame] | 1031 | static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) |
| 1032 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 1033 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Paolo Bonzini | 9315cbf | 2012-08-09 16:07:20 +0200 | [diff] [blame] | 1034 | |
Paolo Bonzini | 9ffe337 | 2016-10-21 22:48:09 +0200 | [diff] [blame] | 1035 | if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) { |
| 1036 | assert(!s->dataplane_started); |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 1037 | } |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 1038 | |
Paolo Bonzini | 9315cbf | 2012-08-09 16:07:20 +0200 | [diff] [blame] | 1039 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
| 1040 | return; |
| 1041 | } |
| 1042 | |
Paolo Bonzini | ef5bc96 | 2013-09-20 17:31:55 +0200 | [diff] [blame] | 1043 | /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send |
| 1044 | * cache flushes. Thus, the "auto writethrough" behavior is never |
| 1045 | * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature. |
| 1046 | * Leaving it enabled would break the following sequence: |
| 1047 | * |
| 1048 | * Guest started with "-drive cache=writethrough" |
| 1049 | * Guest sets status to 0 |
| 1050 | * Guest sets DRIVER bit in status field |
| 1051 | * Guest reads host features (WCE=0, CONFIG_WCE=1) |
| 1052 | * Guest writes guest features (WCE=0, CONFIG_WCE=1) |
| 1053 | * Guest writes 1 to the WCE configuration field (writeback mode) |
| 1054 | * Guest sets DRIVER_OK bit in status field |
| 1055 | * |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1056 | * s->blk would erroneously be placed in writethrough mode. |
Paolo Bonzini | ef5bc96 | 2013-09-20 17:31:55 +0200 | [diff] [blame] | 1057 | */ |
Cornelia Huck | 95129d6 | 2015-08-17 11:48:29 +0200 | [diff] [blame] | 1058 | if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) { |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1059 | aio_context_acquire(blk_get_aio_context(s->blk)); |
| 1060 | blk_set_enable_write_cache(s->blk, |
Cornelia Huck | 95129d6 | 2015-08-17 11:48:29 +0200 | [diff] [blame] | 1061 | virtio_vdev_has_feature(vdev, |
| 1062 | VIRTIO_BLK_F_WCE)); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1063 | aio_context_release(blk_get_aio_context(s->blk)); |
Paolo Bonzini | ef5bc96 | 2013-09-20 17:31:55 +0200 | [diff] [blame] | 1064 | } |
Paolo Bonzini | 9315cbf | 2012-08-09 16:07:20 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Greg Kurz | b2b295a | 2014-06-24 19:19:23 +0200 | [diff] [blame] | 1067 | static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f) |
| 1068 | { |
| 1069 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
| 1070 | VirtIOBlockReq *req = s->rq; |
| 1071 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1072 | while (req) { |
| 1073 | qemu_put_sbyte(f, 1); |
Stefan Hajnoczi | 30d8bf6 | 2016-06-21 13:13:14 +0100 | [diff] [blame] | 1074 | |
| 1075 | if (s->conf.num_queues > 1) { |
| 1076 | qemu_put_be32(f, virtio_get_queue_index(req->vq)); |
| 1077 | } |
| 1078 | |
Jason Wang | 86044b2 | 2019-10-25 10:35:24 +0200 | [diff] [blame] | 1079 | qemu_put_virtqueue_element(vdev, f, &req->elem); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1080 | req = req->next; |
| 1081 | } |
| 1082 | qemu_put_sbyte(f, 0); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Greg Kurz | b2b295a | 2014-06-24 19:19:23 +0200 | [diff] [blame] | 1085 | static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, |
| 1086 | int version_id) |
| 1087 | { |
| 1088 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
Orit Wassermann | 2a633c4 | 2012-05-16 12:21:35 +0200 | [diff] [blame] | 1089 | |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1090 | while (qemu_get_sbyte(f)) { |
Stefan Hajnoczi | 30d8bf6 | 2016-06-21 13:13:14 +0100 | [diff] [blame] | 1091 | unsigned nvqs = s->conf.num_queues; |
| 1092 | unsigned vq_idx = 0; |
Paolo Bonzini | ab281c1 | 2016-01-31 11:28:59 +0100 | [diff] [blame] | 1093 | VirtIOBlockReq *req; |
Stefan Hajnoczi | 30d8bf6 | 2016-06-21 13:13:14 +0100 | [diff] [blame] | 1094 | |
| 1095 | if (nvqs > 1) { |
| 1096 | vq_idx = qemu_get_be32(f); |
| 1097 | |
| 1098 | if (vq_idx >= nvqs) { |
| 1099 | error_report("Invalid virtqueue index in request list: %#x", |
| 1100 | vq_idx); |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Jason Wang | 8607f5c | 2016-12-30 18:09:10 +0800 | [diff] [blame] | 1105 | req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq)); |
Stefan Hajnoczi | 30d8bf6 | 2016-06-21 13:13:14 +0100 | [diff] [blame] | 1106 | virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req); |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1107 | req->next = s->rq; |
Yoshiaki Tamura | 20a81e4 | 2010-06-21 17:50:01 +0900 | [diff] [blame] | 1108 | s->rq = req; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1109 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Sergio Lopez | 9b92fbc | 2019-09-16 13:24:12 +0200 | [diff] [blame] | 1114 | static void virtio_resize_cb(void *opaque) |
| 1115 | { |
| 1116 | VirtIODevice *vdev = opaque; |
| 1117 | |
| 1118 | assert(qemu_get_current_aio_context() == qemu_get_aio_context()); |
| 1119 | virtio_notify_config(vdev); |
| 1120 | } |
| 1121 | |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 1122 | static void virtio_blk_resize(void *opaque) |
Christoph Hellwig | e5051fc | 2011-01-24 13:32:51 +0100 | [diff] [blame] | 1123 | { |
KONRAD Frederic | 1cc91b7 | 2013-03-18 17:37:27 +0100 | [diff] [blame] | 1124 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
Christoph Hellwig | e5051fc | 2011-01-24 13:32:51 +0100 | [diff] [blame] | 1125 | |
Sergio Lopez | 9b92fbc | 2019-09-16 13:24:12 +0200 | [diff] [blame] | 1126 | /* |
| 1127 | * virtio_notify_config() needs to acquire the global mutex, |
| 1128 | * so it can't be called from an iothread. Instead, schedule |
| 1129 | * it to be run in the main context BH. |
| 1130 | */ |
| 1131 | aio_bh_schedule_oneshot(qemu_get_aio_context(), virtio_resize_cb, vdev); |
Christoph Hellwig | e5051fc | 2011-01-24 13:32:51 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1134 | static const BlockDevOps virtio_block_ops = { |
Markus Armbruster | 145feb1 | 2011-08-03 15:07:42 +0200 | [diff] [blame] | 1135 | .resize_cb = virtio_blk_resize, |
Markus Armbruster | 0e49de5 | 2011-08-03 15:07:41 +0200 | [diff] [blame] | 1136 | }; |
| 1137 | |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1138 | static void virtio_blk_device_realize(DeviceState *dev, Error **errp) |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1139 | { |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1140 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
Andreas Färber | 179b417 | 2013-07-30 04:48:15 +0200 | [diff] [blame] | 1141 | VirtIOBlock *s = VIRTIO_BLK(dev); |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1142 | VirtIOBlkConf *conf = &s->conf; |
Andreas Färber | 3ffeeef | 2013-06-07 16:18:50 +0200 | [diff] [blame] | 1143 | Error *err = NULL; |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 1144 | unsigned i; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1145 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1146 | if (!conf->conf.blk) { |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1147 | error_setg(errp, "drive property not set"); |
| 1148 | return; |
Markus Armbruster | d75d25e | 2010-07-06 14:37:43 +0200 | [diff] [blame] | 1149 | } |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1150 | if (!blk_is_inserted(conf->conf.blk)) { |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1151 | error_setg(errp, "Device needs media, but drive is empty"); |
| 1152 | return; |
Markus Armbruster | 98f28ad | 2010-07-06 14:37:44 +0200 | [diff] [blame] | 1153 | } |
Stefan Hajnoczi | 9445e1e | 2020-08-18 15:33:47 +0100 | [diff] [blame] | 1154 | if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) { |
| 1155 | conf->num_queues = 1; |
| 1156 | } |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 1157 | if (!conf->num_queues) { |
| 1158 | error_setg(errp, "num-queues property must be larger than 0"); |
| 1159 | return; |
| 1160 | } |
Denis Plotnikov | 1bf8a98 | 2019-12-20 17:09:04 +0300 | [diff] [blame] | 1161 | if (conf->queue_size <= 2) { |
| 1162 | error_setg(errp, "invalid queue-size property (%" PRIu16 "), " |
| 1163 | "must be > 2", conf->queue_size); |
| 1164 | return; |
| 1165 | } |
Mark Kanda | 6040aed | 2017-12-11 09:16:24 -0600 | [diff] [blame] | 1166 | if (!is_power_of_2(conf->queue_size) || |
| 1167 | conf->queue_size > VIRTQUEUE_MAX_SIZE) { |
| 1168 | error_setg(errp, "invalid queue-size property (%" PRIu16 "), " |
| 1169 | "must be a power of 2 (max %d)", |
| 1170 | conf->queue_size, VIRTQUEUE_MAX_SIZE); |
| 1171 | return; |
| 1172 | } |
Markus Armbruster | d75d25e | 2010-07-06 14:37:43 +0200 | [diff] [blame] | 1173 | |
Mao Zhongyi | ceff3e1 | 2017-11-22 11:08:45 +0800 | [diff] [blame] | 1174 | if (!blkconf_apply_backend_options(&conf->conf, |
Kevin Wolf | 86b1cf3 | 2021-01-18 13:34:47 +0100 | [diff] [blame] | 1175 | !blk_supports_write_perm(conf->conf.blk), |
| 1176 | true, errp)) { |
Kevin Wolf | a17c17a | 2017-01-24 13:43:31 +0100 | [diff] [blame] | 1177 | return; |
| 1178 | } |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1179 | s->original_wce = blk_enable_write_cache(conf->conf.blk); |
Mao Zhongyi | ceff3e1 | 2017-11-22 11:08:45 +0800 | [diff] [blame] | 1180 | if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) { |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1181 | return; |
Markus Armbruster | b7eb0c9 | 2012-07-11 15:08:39 +0200 | [diff] [blame] | 1182 | } |
Mao Zhongyi | ceff3e1 | 2017-11-22 11:08:45 +0800 | [diff] [blame] | 1183 | |
Roman Kagan | c56ee92 | 2020-05-29 01:55:10 +0300 | [diff] [blame] | 1184 | if (!blkconf_blocksizes(&conf->conf, errp)) { |
Mark Kanda | 0a75b60 | 2017-12-11 09:16:25 -0600 | [diff] [blame] | 1185 | return; |
| 1186 | } |
| 1187 | |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 1188 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) && |
| 1189 | (!conf->max_discard_sectors || |
| 1190 | conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) { |
| 1191 | error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")" |
| 1192 | ", must be between 1 and %d", |
| 1193 | conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS); |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) && |
| 1198 | (!conf->max_write_zeroes_sectors || |
| 1199 | conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) { |
| 1200 | error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32 |
| 1201 | "), must be between 1 and %d", |
| 1202 | conf->max_write_zeroes_sectors, |
| 1203 | (int)BDRV_REQUEST_MAX_SECTORS); |
| 1204 | return; |
| 1205 | } |
| 1206 | |
Stefano Garzarella | 20764be | 2019-02-21 11:33:09 +0100 | [diff] [blame] | 1207 | virtio_blk_set_config_size(s, s->host_features); |
| 1208 | |
Jonah Palmer | 3857cd5 | 2022-04-01 09:23:18 -0400 | [diff] [blame] | 1209 | virtio_init(vdev, VIRTIO_ID_BLOCK, s->config_size); |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1210 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1211 | s->blk = conf->conf.blk; |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 1212 | s->rq = NULL; |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1213 | s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1; |
Markus Armbruster | e63e7fd | 2012-07-10 11:12:43 +0200 | [diff] [blame] | 1214 | |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 1215 | for (i = 0; i < conf->num_queues; i++) { |
Mark Kanda | 6040aed | 2017-12-11 09:16:24 -0600 | [diff] [blame] | 1216 | virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output); |
Stefan Hajnoczi | 2f27059 | 2016-06-21 13:13:16 +0100 | [diff] [blame] | 1217 | } |
Kevin Wolf | 98e3ab3 | 2022-05-10 17:10:19 +0200 | [diff] [blame] | 1218 | qemu_coroutine_inc_pool_size(conf->num_queues * conf->queue_size / 2); |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1219 | virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err); |
Andreas Färber | 3ffeeef | 2013-06-07 16:18:50 +0200 | [diff] [blame] | 1220 | if (err != NULL) { |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1221 | error_propagate(errp, err); |
Pan Nengyuan | cfaf757 | 2020-03-28 08:57:04 +0800 | [diff] [blame] | 1222 | for (i = 0; i < conf->num_queues; i++) { |
| 1223 | virtio_del_queue(vdev, i); |
| 1224 | } |
KONRAD Frederic | 6a1a8cc | 2013-04-24 10:21:22 +0200 | [diff] [blame] | 1225 | virtio_cleanup(vdev); |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1226 | return; |
Stefan Hajnoczi | 392808b | 2012-11-14 15:45:38 +0100 | [diff] [blame] | 1227 | } |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1228 | |
Christian Borntraeger | 69b302b | 2013-02-22 14:37:10 +0100 | [diff] [blame] | 1229 | s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1230 | blk_set_dev_ops(s->blk, &virtio_block_ops, s); |
Paul Brook | 07e3af9 | 2009-05-14 22:35:08 +0100 | [diff] [blame] | 1231 | |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1232 | blk_iostatus_enable(s->blk); |
Sam Eiderman | 71f571a | 2019-10-16 19:41:42 +0300 | [diff] [blame] | 1233 | |
| 1234 | add_boot_device_lchs(dev, "/disk@0,0", |
| 1235 | conf->conf.lcyls, |
| 1236 | conf->conf.lheads, |
| 1237 | conf->conf.lsecs); |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 1240 | static void virtio_blk_device_unrealize(DeviceState *dev) |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1241 | { |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 1242 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 1243 | VirtIOBlock *s = VIRTIO_BLK(dev); |
Eugenio Pérez | 4a0117c | 2019-10-25 10:35:22 +0200 | [diff] [blame] | 1244 | VirtIOBlkConf *conf = &s->conf; |
| 1245 | unsigned i; |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 1246 | |
Julia Suvorova | 7bfde68 | 2019-10-18 16:28:56 +0200 | [diff] [blame] | 1247 | blk_drain(s->blk); |
Sam Eiderman | 71f571a | 2019-10-16 19:41:42 +0300 | [diff] [blame] | 1248 | del_boot_device_lchs(dev, "/disk@0,0"); |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1249 | virtio_blk_data_plane_destroy(s->dataplane); |
| 1250 | s->dataplane = NULL; |
Eugenio Pérez | 4a0117c | 2019-10-25 10:35:22 +0200 | [diff] [blame] | 1251 | for (i = 0; i < conf->num_queues; i++) { |
| 1252 | virtio_del_queue(vdev, i); |
| 1253 | } |
Kevin Wolf | 98e3ab3 | 2022-05-10 17:10:19 +0200 | [diff] [blame] | 1254 | qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2); |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1255 | qemu_del_vm_change_state_handler(s->change); |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 1256 | blockdev_mark_auto_del(s->blk); |
KONRAD Frederic | 6a1a8cc | 2013-04-24 10:21:22 +0200 | [diff] [blame] | 1257 | virtio_cleanup(vdev); |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
Stefan Hajnoczi | 467b3f3 | 2014-06-10 09:03:20 +0200 | [diff] [blame] | 1260 | static void virtio_blk_instance_init(Object *obj) |
| 1261 | { |
| 1262 | VirtIOBlock *s = VIRTIO_BLK(obj); |
| 1263 | |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1264 | device_add_bootindex_property(obj, &s->conf.conf.bootindex, |
Gonglei | 3342ec3 | 2014-10-07 16:00:30 +0800 | [diff] [blame] | 1265 | "bootindex", "/disk@0,0", |
Markus Armbruster | 40c2281 | 2020-05-05 17:29:23 +0200 | [diff] [blame] | 1266 | DEVICE(obj)); |
Stefan Hajnoczi | 467b3f3 | 2014-06-10 09:03:20 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
Halil Pasic | 977a117 | 2016-10-06 14:55:40 +0200 | [diff] [blame] | 1269 | static const VMStateDescription vmstate_virtio_blk = { |
| 1270 | .name = "virtio-blk", |
| 1271 | .minimum_version_id = 2, |
| 1272 | .version_id = 2, |
| 1273 | .fields = (VMStateField[]) { |
| 1274 | VMSTATE_VIRTIO_DEVICE, |
| 1275 | VMSTATE_END_OF_LIST() |
| 1276 | }, |
| 1277 | }; |
Dr. David Alan Gilbert | bbded32 | 2016-07-14 18:22:47 +0100 | [diff] [blame] | 1278 | |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1279 | static Property virtio_blk_properties[] = { |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1280 | DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf), |
Kevin Wolf | 8c39825 | 2016-06-29 17:41:35 +0200 | [diff] [blame] | 1281 | DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf), |
Markus Armbruster | 2a30307 | 2014-10-07 13:59:17 +0200 | [diff] [blame] | 1282 | DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf), |
| 1283 | DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial), |
Stefano Garzarella | bbe8bd4 | 2019-02-21 11:33:06 +0100 | [diff] [blame] | 1284 | DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features, |
| 1285 | VIRTIO_BLK_F_CONFIG_WCE, true), |
Stefan Hajnoczi | 32a877e | 2014-06-18 17:58:36 +0800 | [diff] [blame] | 1286 | #ifdef __linux__ |
Stefano Garzarella | bbe8bd4 | 2019-02-21 11:33:06 +0100 | [diff] [blame] | 1287 | DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features, |
| 1288 | VIRTIO_BLK_F_SCSI, false), |
Stefan Hajnoczi | 32a877e | 2014-06-18 17:58:36 +0800 | [diff] [blame] | 1289 | #endif |
Peter Lieven | c99495a | 2015-02-02 14:52:22 +0100 | [diff] [blame] | 1290 | DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0, |
| 1291 | true), |
Stefan Hajnoczi | 9445e1e | 2020-08-18 15:33:47 +0100 | [diff] [blame] | 1292 | DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues, |
| 1293 | VIRTIO_BLK_AUTO_NUM_QUEUES), |
Denis Plotnikov | c9b7d9e | 2020-02-14 10:46:48 +0300 | [diff] [blame] | 1294 | DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 256), |
Denis Plotnikov | 1bf8a98 | 2019-12-20 17:09:04 +0300 | [diff] [blame] | 1295 | DEFINE_PROP_BOOL("seg-max-adjust", VirtIOBlock, conf.seg_max_adjust, true), |
Fam Zheng | d679ac0 | 2017-07-14 10:14:55 +0800 | [diff] [blame] | 1296 | DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD, |
| 1297 | IOThread *), |
Stefano Garzarella | 5c81161 | 2019-02-21 11:33:07 +0100 | [diff] [blame] | 1298 | DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features, |
| 1299 | VIRTIO_BLK_F_DISCARD, true), |
Akihiko Odaki | fb0b154 | 2021-02-25 09:12:39 +0900 | [diff] [blame] | 1300 | DEFINE_PROP_BOOL("report-discard-granularity", VirtIOBlock, |
| 1301 | conf.report_discard_granularity, true), |
Stefano Garzarella | 5c81161 | 2019-02-21 11:33:07 +0100 | [diff] [blame] | 1302 | DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features, |
| 1303 | VIRTIO_BLK_F_WRITE_ZEROES, true), |
Stefano Garzarella | 37b06f8 | 2019-02-21 11:33:10 +0100 | [diff] [blame] | 1304 | DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock, |
| 1305 | conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS), |
| 1306 | DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock, |
| 1307 | conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS), |
Evgeny Yakovlev | 5f25857 | 2019-11-05 21:22:17 +0300 | [diff] [blame] | 1308 | DEFINE_PROP_BOOL("x-enable-wce-if-config-wce", VirtIOBlock, |
| 1309 | conf.x_enable_wce_if_config_wce, true), |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1310 | DEFINE_PROP_END_OF_LIST(), |
| 1311 | }; |
| 1312 | |
| 1313 | static void virtio_blk_class_init(ObjectClass *klass, void *data) |
| 1314 | { |
| 1315 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 1316 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1317 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 1318 | device_class_set_props(dc, virtio_blk_properties); |
Dr. David Alan Gilbert | bbded32 | 2016-07-14 18:22:47 +0100 | [diff] [blame] | 1319 | dc->vmsd = &vmstate_virtio_blk; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 1320 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
Andreas Färber | 75884af | 2013-07-30 01:35:08 +0200 | [diff] [blame] | 1321 | vdc->realize = virtio_blk_device_realize; |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 1322 | vdc->unrealize = virtio_blk_device_unrealize; |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1323 | vdc->get_config = virtio_blk_update_config; |
| 1324 | vdc->set_config = virtio_blk_set_config; |
| 1325 | vdc->get_features = virtio_blk_get_features; |
| 1326 | vdc->set_status = virtio_blk_set_status; |
| 1327 | vdc->reset = virtio_blk_reset; |
Greg Kurz | b2b295a | 2014-06-24 19:19:23 +0200 | [diff] [blame] | 1328 | vdc->save = virtio_blk_save_device; |
| 1329 | vdc->load = virtio_blk_load_device; |
Paolo Bonzini | 9ffe337 | 2016-10-21 22:48:09 +0200 | [diff] [blame] | 1330 | vdc->start_ioeventfd = virtio_blk_data_plane_start; |
| 1331 | vdc->stop_ioeventfd = virtio_blk_data_plane_stop; |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Changlong Xie | b5c7cea | 2016-08-03 16:49:07 +0800 | [diff] [blame] | 1334 | static const TypeInfo virtio_blk_info = { |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1335 | .name = TYPE_VIRTIO_BLK, |
| 1336 | .parent = TYPE_VIRTIO_DEVICE, |
| 1337 | .instance_size = sizeof(VirtIOBlock), |
Stefan Hajnoczi | 467b3f3 | 2014-06-10 09:03:20 +0200 | [diff] [blame] | 1338 | .instance_init = virtio_blk_instance_init, |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1339 | .class_init = virtio_blk_class_init, |
| 1340 | }; |
| 1341 | |
| 1342 | static void virtio_register_types(void) |
| 1343 | { |
Changlong Xie | b5c7cea | 2016-08-03 16:49:07 +0800 | [diff] [blame] | 1344 | type_register_static(&virtio_blk_info); |
KONRAD Frederic | 1c028dd | 2013-03-18 17:37:22 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | type_init(virtio_register_types) |