]>
Commit | Line | Data |
---|---|---|
6e02c38d AL |
1 | /* |
2 | * Virtio Block Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
80c71a24 | 14 | #include "qemu/osdep.h" |
da34e65c | 15 | #include "qapi/error.h" |
827805a2 | 16 | #include "qemu/iov.h" |
0b8fa32f | 17 | #include "qemu/module.h" |
1de7afc9 | 18 | #include "qemu/error-report.h" |
6d519a5f | 19 | #include "trace.h" |
0d09e41a | 20 | #include "hw/block/block.h" |
a27bd6c7 | 21 | #include "hw/qdev-properties.h" |
9c17d615 | 22 | #include "sysemu/blockdev.h" |
2f780b6a | 23 | #include "sysemu/sysemu.h" |
54d31236 | 24 | #include "sysemu/runstate.h" |
0d09e41a | 25 | #include "hw/virtio/virtio-blk.h" |
52b53c04 | 26 | #include "dataplane/virtio-blk.h" |
08e2c9f1 | 27 | #include "scsi/constants.h" |
1063b8b1 CH |
28 | #ifdef __linux__ |
29 | # include <scsi/sg.h> | |
30 | #endif | |
0d09e41a | 31 | #include "hw/virtio/virtio-bus.h" |
ca77ee28 | 32 | #include "migration/qemu-file-types.h" |
783d1897 | 33 | #include "hw/virtio/virtio-access.h" |
6e02c38d | 34 | |
20764be0 | 35 | /* Config size before the discard support (hide associated config fields) */ |
42824b4d CL |
36 | #define VIRTIO_BLK_CFG_SIZE offsetof(struct virtio_blk_config, \ |
37 | max_discard_sectors) | |
20764be0 SG |
38 | /* |
39 | * Starting from the discard feature, we can use this array to properly | |
40 | * set the config size depending on the features enabled. | |
41 | */ | |
42 | static VirtIOFeature feature_sizes[] = { | |
43 | {.flags = 1ULL << VIRTIO_BLK_F_DISCARD, | |
44 | .end = virtio_endof(struct virtio_blk_config, discard_sector_alignment)}, | |
45 | {.flags = 1ULL << VIRTIO_BLK_F_WRITE_ZEROES, | |
46 | .end = virtio_endof(struct virtio_blk_config, write_zeroes_may_unmap)}, | |
47 | {} | |
48 | }; | |
49 | ||
50 | static void virtio_blk_set_config_size(VirtIOBlock *s, uint64_t host_features) | |
51 | { | |
52 | s->config_size = MAX(VIRTIO_BLK_CFG_SIZE, | |
53 | virtio_feature_get_config_size(feature_sizes, host_features)); | |
54 | ||
55 | assert(s->config_size <= sizeof(struct virtio_blk_config)); | |
56 | } | |
42824b4d | 57 | |
d14dde5e GK |
58 | static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq, |
59 | VirtIOBlockReq *req) | |
671ec3f0 | 60 | { |
671ec3f0 | 61 | req->dev = s; |
edaffd9f | 62 | req->vq = vq; |
869d66af | 63 | req->qiov.size = 0; |
2a6cdd6d | 64 | req->in_len = 0; |
869d66af | 65 | req->next = NULL; |
95f7142a | 66 | req->mr_next = NULL; |
671ec3f0 FZ |
67 | } |
68 | ||
d14dde5e | 69 | static void virtio_blk_free_request(VirtIOBlockReq *req) |
671ec3f0 | 70 | { |
1d29b5b0 | 71 | g_free(req); |
671ec3f0 FZ |
72 | } |
73 | ||
03de2f52 | 74 | static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status) |
869a5c6d AL |
75 | { |
76 | VirtIOBlock *s = req->dev; | |
1cc91b7d | 77 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
869a5c6d | 78 | |
a576ceac | 79 | trace_virtio_blk_req_complete(vdev, req, status); |
6d519a5f | 80 | |
92e3c2a3 | 81 | stb_p(&req->in->status, status); |
edaffd9f | 82 | virtqueue_push(req->vq, &req->elem, req->in_len); |
eb41cf78 | 83 | if (s->dataplane_started && !s->dataplane_disabled) { |
edaffd9f | 84 | virtio_blk_data_plane_notify(s->dataplane, req->vq); |
03de2f52 | 85 | } else { |
edaffd9f | 86 | virtio_notify(vdev, req->vq); |
03de2f52 | 87 | } |
bf4bd461 FZ |
88 | } |
89 | ||
f35d68f0 | 90 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
00f639fb | 91 | bool is_read, bool acct_failed) |
869a5c6d | 92 | { |
869a5c6d | 93 | VirtIOBlock *s = req->dev; |
9a6719d5 | 94 | BlockErrorAction action = blk_get_error_action(s->blk, is_read, error); |
869a5c6d | 95 | |
a589569f | 96 | if (action == BLOCK_ERROR_ACTION_STOP) { |
466138dc FZ |
97 | /* Break the link as the next request is going to be parsed from the |
98 | * ring again. Otherwise we may end up doing a double completion! */ | |
99 | req->mr_next = NULL; | |
869a5c6d AL |
100 | req->next = s->rq; |
101 | s->rq = req; | |
a589569f | 102 | } else if (action == BLOCK_ERROR_ACTION_REPORT) { |
869a5c6d | 103 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
00f639fb SG |
104 | if (acct_failed) { |
105 | block_acct_failed(blk_get_stats(s->blk), &req->acct); | |
106 | } | |
671ec3f0 | 107 | virtio_blk_free_request(req); |
869a5c6d AL |
108 | } |
109 | ||
4be74634 | 110 | blk_error_action(s->blk, action, is_read, error); |
a589569f | 111 | return action != BLOCK_ERROR_ACTION_IGNORE; |
869a5c6d AL |
112 | } |
113 | ||
6e02c38d AL |
114 | static void virtio_blk_rw_complete(void *opaque, int ret) |
115 | { | |
95f7142a | 116 | VirtIOBlockReq *next = opaque; |
b9e413dd | 117 | VirtIOBlock *s = next->dev; |
a576ceac | 118 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
95f7142a | 119 | |
b9e413dd | 120 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
95f7142a PL |
121 | while (next) { |
122 | VirtIOBlockReq *req = next; | |
123 | next = req->mr_next; | |
a576ceac | 124 | trace_virtio_blk_rw_complete(vdev, req, ret); |
95f7142a PL |
125 | |
126 | if (req->qiov.nalloc != -1) { | |
e61809ed | 127 | /* If nalloc is != -1 req->qiov is a local copy of the original |
9bb192a4 YB |
128 | * external iovec. It was allocated in submit_requests to be |
129 | * able to merge requests. */ | |
95f7142a PL |
130 | qemu_iovec_destroy(&req->qiov); |
131 | } | |
6e02c38d | 132 | |
95f7142a | 133 | if (ret) { |
bf4069fb | 134 | int p = virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type); |
95f7142a | 135 | bool is_read = !(p & VIRTIO_BLK_T_OUT); |
2a6cdd6d PB |
136 | /* Note that memory may be dirtied on read failure. If the |
137 | * virtio request is not completed here, as is the case for | |
138 | * BLOCK_ERROR_ACTION_STOP, the memory may not be copied | |
139 | * correctly during live migration. While this is ugly, | |
140 | * it is acceptable because the device is free to write to | |
141 | * the memory until the request is completed (which will | |
142 | * happen on the other side of the migration). | |
143 | */ | |
00f639fb | 144 | if (virtio_blk_handle_rw_error(req, -ret, is_read, true)) { |
95f7142a PL |
145 | continue; |
146 | } | |
147 | } | |
6d519a5f | 148 | |
95f7142a | 149 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
bf4069fb | 150 | block_acct_done(blk_get_stats(s->blk), &req->acct); |
95f7142a | 151 | virtio_blk_free_request(req); |
6e02c38d | 152 | } |
b9e413dd | 153 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
869a5c6d | 154 | } |
6e02c38d | 155 | |
aa659be3 CH |
156 | static void virtio_blk_flush_complete(void *opaque, int ret) |
157 | { | |
158 | VirtIOBlockReq *req = opaque; | |
b9e413dd | 159 | VirtIOBlock *s = req->dev; |
aa659be3 | 160 | |
b9e413dd | 161 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
8c269b54 | 162 | if (ret) { |
00f639fb | 163 | if (virtio_blk_handle_rw_error(req, -ret, 0, true)) { |
b9e413dd | 164 | goto out; |
8c269b54 KW |
165 | } |
166 | } | |
167 | ||
168 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); | |
9a6719d5 | 169 | block_acct_done(blk_get_stats(s->blk), &req->acct); |
671ec3f0 | 170 | virtio_blk_free_request(req); |
b9e413dd PB |
171 | |
172 | out: | |
173 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); | |
6e02c38d AL |
174 | } |
175 | ||
37b06f8d SG |
176 | static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret) |
177 | { | |
178 | VirtIOBlockReq *req = opaque; | |
179 | VirtIOBlock *s = req->dev; | |
180 | bool is_write_zeroes = (virtio_ldl_p(VIRTIO_DEVICE(s), &req->out.type) & | |
181 | ~VIRTIO_BLK_T_BARRIER) == VIRTIO_BLK_T_WRITE_ZEROES; | |
182 | ||
183 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); | |
184 | if (ret) { | |
185 | if (virtio_blk_handle_rw_error(req, -ret, false, is_write_zeroes)) { | |
186 | goto out; | |
187 | } | |
188 | } | |
189 | ||
190 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); | |
191 | if (is_write_zeroes) { | |
192 | block_acct_done(blk_get_stats(s->blk), &req->acct); | |
193 | } | |
194 | virtio_blk_free_request(req); | |
195 | ||
196 | out: | |
197 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); | |
198 | } | |
199 | ||
1dc936aa FZ |
200 | #ifdef __linux__ |
201 | ||
202 | typedef struct { | |
203 | VirtIOBlockReq *req; | |
204 | struct sg_io_hdr hdr; | |
205 | } VirtIOBlockIoctlReq; | |
206 | ||
207 | static void virtio_blk_ioctl_complete(void *opaque, int status) | |
208 | { | |
209 | VirtIOBlockIoctlReq *ioctl_req = opaque; | |
210 | VirtIOBlockReq *req = ioctl_req->req; | |
9d456654 PB |
211 | VirtIOBlock *s = req->dev; |
212 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
1dc936aa FZ |
213 | struct virtio_scsi_inhdr *scsi; |
214 | struct sg_io_hdr *hdr; | |
215 | ||
216 | scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; | |
217 | ||
218 | if (status) { | |
219 | status = VIRTIO_BLK_S_UNSUPP; | |
220 | virtio_stl_p(vdev, &scsi->errors, 255); | |
221 | goto out; | |
222 | } | |
223 | ||
224 | hdr = &ioctl_req->hdr; | |
225 | /* | |
226 | * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi) | |
227 | * clear the masked_status field [hence status gets cleared too, see | |
228 | * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED | |
229 | * status has occurred. However they do set DRIVER_SENSE in driver_status | |
230 | * field. Also a (sb_len_wr > 0) indicates there is a sense buffer. | |
231 | */ | |
232 | if (hdr->status == 0 && hdr->sb_len_wr > 0) { | |
233 | hdr->status = CHECK_CONDITION; | |
234 | } | |
235 | ||
236 | virtio_stl_p(vdev, &scsi->errors, | |
237 | hdr->status | (hdr->msg_status << 8) | | |
238 | (hdr->host_status << 16) | (hdr->driver_status << 24)); | |
239 | virtio_stl_p(vdev, &scsi->residual, hdr->resid); | |
240 | virtio_stl_p(vdev, &scsi->sense_len, hdr->sb_len_wr); | |
241 | virtio_stl_p(vdev, &scsi->data_len, hdr->dxfer_len); | |
242 | ||
243 | out: | |
b9e413dd | 244 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
1dc936aa FZ |
245 | virtio_blk_req_complete(req, status); |
246 | virtio_blk_free_request(req); | |
b9e413dd | 247 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
1dc936aa FZ |
248 | g_free(ioctl_req); |
249 | } | |
250 | ||
251 | #endif | |
252 | ||
edaffd9f | 253 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq) |
6e02c38d | 254 | { |
edaffd9f | 255 | VirtIOBlockReq *req = virtqueue_pop(vq, sizeof(VirtIOBlockReq)); |
6e02c38d | 256 | |
51b19ebe | 257 | if (req) { |
edaffd9f | 258 | virtio_blk_init_request(s, vq, req); |
6e02c38d | 259 | } |
6e02c38d AL |
260 | return req; |
261 | } | |
262 | ||
75344fa4 | 263 | static int virtio_blk_handle_scsi_req(VirtIOBlockReq *req) |
1063b8b1 | 264 | { |
5a05cbee FZ |
265 | int status = VIRTIO_BLK_S_OK; |
266 | struct virtio_scsi_inhdr *scsi = NULL; | |
75344fa4 | 267 | VirtIOBlock *blk = req->dev; |
bf4069fb AR |
268 | VirtIODevice *vdev = VIRTIO_DEVICE(blk); |
269 | VirtQueueElement *elem = &req->elem; | |
783d1897 | 270 | |
47ce9ef7 | 271 | #ifdef __linux__ |
1063b8b1 | 272 | int i; |
1dc936aa | 273 | VirtIOBlockIoctlReq *ioctl_req; |
a209f461 | 274 | BlockAIOCB *acb; |
47ce9ef7 | 275 | #endif |
1063b8b1 CH |
276 | |
277 | /* | |
278 | * We require at least one output segment each for the virtio_blk_outhdr | |
279 | * and the SCSI command block. | |
280 | * | |
281 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr | |
282 | * and the sense buffer pointer in the input segments. | |
283 | */ | |
5a05cbee FZ |
284 | if (elem->out_num < 2 || elem->in_num < 3) { |
285 | status = VIRTIO_BLK_S_IOERR; | |
286 | goto fail; | |
1063b8b1 CH |
287 | } |
288 | ||
289 | /* | |
f34e73cd PB |
290 | * The scsi inhdr is placed in the second-to-last input segment, just |
291 | * before the regular inhdr. | |
1063b8b1 | 292 | */ |
5a05cbee | 293 | scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base; |
f34e73cd | 294 | |
bbe8bd4d | 295 | if (!virtio_has_feature(blk->host_features, VIRTIO_BLK_F_SCSI)) { |
f34e73cd PB |
296 | status = VIRTIO_BLK_S_UNSUPP; |
297 | goto fail; | |
1063b8b1 CH |
298 | } |
299 | ||
300 | /* | |
f34e73cd | 301 | * No support for bidirection commands yet. |
1063b8b1 | 302 | */ |
5a05cbee | 303 | if (elem->out_num > 2 && elem->in_num > 3) { |
f34e73cd PB |
304 | status = VIRTIO_BLK_S_UNSUPP; |
305 | goto fail; | |
306 | } | |
1063b8b1 | 307 | |
f34e73cd | 308 | #ifdef __linux__ |
1dc936aa FZ |
309 | ioctl_req = g_new0(VirtIOBlockIoctlReq, 1); |
310 | ioctl_req->req = req; | |
311 | ioctl_req->hdr.interface_id = 'S'; | |
312 | ioctl_req->hdr.cmd_len = elem->out_sg[1].iov_len; | |
313 | ioctl_req->hdr.cmdp = elem->out_sg[1].iov_base; | |
314 | ioctl_req->hdr.dxfer_len = 0; | |
1063b8b1 | 315 | |
5a05cbee | 316 | if (elem->out_num > 2) { |
1063b8b1 CH |
317 | /* |
318 | * If there are more than the minimally required 2 output segments | |
319 | * there is write payload starting from the third iovec. | |
320 | */ | |
1dc936aa FZ |
321 | ioctl_req->hdr.dxfer_direction = SG_DXFER_TO_DEV; |
322 | ioctl_req->hdr.iovec_count = elem->out_num - 2; | |
1063b8b1 | 323 | |
1dc936aa FZ |
324 | for (i = 0; i < ioctl_req->hdr.iovec_count; i++) { |
325 | ioctl_req->hdr.dxfer_len += elem->out_sg[i + 2].iov_len; | |
326 | } | |
1063b8b1 | 327 | |
1dc936aa | 328 | ioctl_req->hdr.dxferp = elem->out_sg + 2; |
1063b8b1 | 329 | |
5a05cbee | 330 | } else if (elem->in_num > 3) { |
1063b8b1 CH |
331 | /* |
332 | * If we have more than 3 input segments the guest wants to actually | |
333 | * read data. | |
334 | */ | |
1dc936aa FZ |
335 | ioctl_req->hdr.dxfer_direction = SG_DXFER_FROM_DEV; |
336 | ioctl_req->hdr.iovec_count = elem->in_num - 3; | |
337 | for (i = 0; i < ioctl_req->hdr.iovec_count; i++) { | |
338 | ioctl_req->hdr.dxfer_len += elem->in_sg[i].iov_len; | |
339 | } | |
1063b8b1 | 340 | |
1dc936aa | 341 | ioctl_req->hdr.dxferp = elem->in_sg; |
1063b8b1 CH |
342 | } else { |
343 | /* | |
344 | * Some SCSI commands don't actually transfer any data. | |
345 | */ | |
1dc936aa | 346 | ioctl_req->hdr.dxfer_direction = SG_DXFER_NONE; |
1063b8b1 CH |
347 | } |
348 | ||
1dc936aa FZ |
349 | ioctl_req->hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base; |
350 | ioctl_req->hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len; | |
1063b8b1 | 351 | |
a209f461 FZ |
352 | acb = blk_aio_ioctl(blk->blk, SG_IO, &ioctl_req->hdr, |
353 | virtio_blk_ioctl_complete, ioctl_req); | |
354 | if (!acb) { | |
355 | g_free(ioctl_req); | |
356 | status = VIRTIO_BLK_S_UNSUPP; | |
357 | goto fail; | |
358 | } | |
1dc936aa | 359 | return -EINPROGRESS; |
1063b8b1 | 360 | #else |
f34e73cd PB |
361 | abort(); |
362 | #endif | |
363 | ||
364 | fail: | |
365 | /* Just put anything nonzero so that the ioctl fails in the guest. */ | |
5a05cbee | 366 | if (scsi) { |
783d1897 | 367 | virtio_stl_p(vdev, &scsi->errors, 255); |
5a05cbee FZ |
368 | } |
369 | return status; | |
370 | } | |
371 | ||
372 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) | |
373 | { | |
374 | int status; | |
375 | ||
75344fa4 | 376 | status = virtio_blk_handle_scsi_req(req); |
1dc936aa FZ |
377 | if (status != -EINPROGRESS) { |
378 | virtio_blk_req_complete(req, status); | |
379 | virtio_blk_free_request(req); | |
380 | } | |
1063b8b1 | 381 | } |
1063b8b1 | 382 | |
95f7142a PL |
383 | static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb, |
384 | int start, int num_reqs, int niov) | |
869a5c6d | 385 | { |
95f7142a PL |
386 | QEMUIOVector *qiov = &mrb->reqs[start]->qiov; |
387 | int64_t sector_num = mrb->reqs[start]->sector_num; | |
95f7142a PL |
388 | bool is_write = mrb->is_write; |
389 | ||
390 | if (num_reqs > 1) { | |
391 | int i; | |
392 | struct iovec *tmp_iov = qiov->iov; | |
393 | int tmp_niov = qiov->niov; | |
394 | ||
395 | /* mrb->reqs[start]->qiov was initialized from external so we can't | |
b5772fdd | 396 | * modify it here. We need to initialize it locally and then add the |
95f7142a PL |
397 | * external iovecs. */ |
398 | qemu_iovec_init(qiov, niov); | |
399 | ||
400 | for (i = 0; i < tmp_niov; i++) { | |
401 | qemu_iovec_add(qiov, tmp_iov[i].iov_base, tmp_iov[i].iov_len); | |
402 | } | |
403 | ||
404 | for (i = start + 1; i < start + num_reqs; i++) { | |
405 | qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0, | |
406 | mrb->reqs[i]->qiov.size); | |
407 | mrb->reqs[i - 1]->mr_next = mrb->reqs[i]; | |
95f7142a | 408 | } |
95f7142a | 409 | |
a576ceac SH |
410 | trace_virtio_blk_submit_multireq(VIRTIO_DEVICE(mrb->reqs[start]->dev), |
411 | mrb, start, num_reqs, | |
b5772fdd EB |
412 | sector_num << BDRV_SECTOR_BITS, |
413 | qiov->size, is_write); | |
95f7142a PL |
414 | block_acct_merge_done(blk_get_stats(blk), |
415 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ, | |
416 | num_reqs - 1); | |
417 | } | |
91553dcc | 418 | |
95f7142a | 419 | if (is_write) { |
b5772fdd EB |
420 | blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, |
421 | virtio_blk_rw_complete, mrb->reqs[start]); | |
95f7142a | 422 | } else { |
b5772fdd EB |
423 | blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, |
424 | virtio_blk_rw_complete, mrb->reqs[start]); | |
95f7142a PL |
425 | } |
426 | } | |
427 | ||
428 | static int multireq_compare(const void *a, const void *b) | |
429 | { | |
430 | const VirtIOBlockReq *req1 = *(VirtIOBlockReq **)a, | |
431 | *req2 = *(VirtIOBlockReq **)b; | |
432 | ||
433 | /* | |
434 | * Note that we can't simply subtract sector_num1 from sector_num2 | |
435 | * here as that could overflow the return value. | |
436 | */ | |
437 | if (req1->sector_num > req2->sector_num) { | |
438 | return 1; | |
439 | } else if (req1->sector_num < req2->sector_num) { | |
440 | return -1; | |
441 | } else { | |
442 | return 0; | |
443 | } | |
444 | } | |
445 | ||
d14dde5e | 446 | static void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb) |
95f7142a PL |
447 | { |
448 | int i = 0, start = 0, num_reqs = 0, niov = 0, nb_sectors = 0; | |
5def6b80 | 449 | uint32_t max_transfer; |
95f7142a PL |
450 | int64_t sector_num = 0; |
451 | ||
452 | if (mrb->num_reqs == 1) { | |
453 | submit_requests(blk, mrb, 0, 1, -1); | |
454 | mrb->num_reqs = 0; | |
c20fd872 CH |
455 | return; |
456 | } | |
457 | ||
5def6b80 | 458 | max_transfer = blk_get_max_transfer(mrb->reqs[0]->dev->blk); |
95f7142a PL |
459 | |
460 | qsort(mrb->reqs, mrb->num_reqs, sizeof(*mrb->reqs), | |
461 | &multireq_compare); | |
462 | ||
463 | for (i = 0; i < mrb->num_reqs; i++) { | |
464 | VirtIOBlockReq *req = mrb->reqs[i]; | |
465 | if (num_reqs > 0) { | |
49cffbc6 GA |
466 | /* |
467 | * NOTE: We cannot merge the requests in below situations: | |
468 | * 1. requests are not sequential | |
469 | * 2. merge would exceed maximum number of IOVs | |
470 | * 3. merge would exceed maximum transfer length of backend device | |
471 | */ | |
472 | if (sector_num + nb_sectors != req->sector_num || | |
222565f6 | 473 | niov > blk_get_max_iov(blk) - req->qiov.niov || |
5def6b80 EB |
474 | req->qiov.size > max_transfer || |
475 | nb_sectors > (max_transfer - | |
476 | req->qiov.size) / BDRV_SECTOR_SIZE) { | |
95f7142a PL |
477 | submit_requests(blk, mrb, start, num_reqs, niov); |
478 | num_reqs = 0; | |
91553dcc KW |
479 | } |
480 | } | |
95f7142a PL |
481 | |
482 | if (num_reqs == 0) { | |
483 | sector_num = req->sector_num; | |
484 | nb_sectors = niov = 0; | |
485 | start = i; | |
486 | } | |
487 | ||
488 | nb_sectors += req->qiov.size / BDRV_SECTOR_SIZE; | |
489 | niov += req->qiov.niov; | |
490 | num_reqs++; | |
91553dcc | 491 | } |
c20fd872 | 492 | |
95f7142a PL |
493 | submit_requests(blk, mrb, start, num_reqs, niov); |
494 | mrb->num_reqs = 0; | |
91553dcc | 495 | } |
87b245db | 496 | |
c20fd872 | 497 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
aa659be3 | 498 | { |
bf4069fb AR |
499 | VirtIOBlock *s = req->dev; |
500 | ||
501 | block_acct_start(blk_get_stats(s->blk), &req->acct, 0, | |
5366d0c8 | 502 | BLOCK_ACCT_FLUSH); |
a597e79c | 503 | |
618fbb84 CH |
504 | /* |
505 | * Make sure all outstanding writes are posted to the backing device. | |
506 | */ | |
95f7142a | 507 | if (mrb->is_write && mrb->num_reqs > 0) { |
bf4069fb | 508 | virtio_blk_submit_multireq(s->blk, mrb); |
95f7142a | 509 | } |
bf4069fb | 510 | blk_aio_flush(s->blk, virtio_blk_flush_complete, req); |
aa659be3 CH |
511 | } |
512 | ||
d0e14376 MA |
513 | static bool virtio_blk_sect_range_ok(VirtIOBlock *dev, |
514 | uint64_t sector, size_t size) | |
515 | { | |
3c2daac0 MA |
516 | uint64_t nb_sectors = size >> BDRV_SECTOR_BITS; |
517 | uint64_t total_sectors; | |
518 | ||
75af1f34 | 519 | if (nb_sectors > BDRV_REQUEST_MAX_SECTORS) { |
95f7142a PL |
520 | return false; |
521 | } | |
d0e14376 MA |
522 | if (sector & dev->sector_mask) { |
523 | return false; | |
524 | } | |
2a30307f | 525 | if (size % dev->conf.conf.logical_block_size) { |
d0e14376 MA |
526 | return false; |
527 | } | |
4be74634 | 528 | blk_get_geometry(dev->blk, &total_sectors); |
3c2daac0 MA |
529 | if (sector > total_sectors || nb_sectors > total_sectors - sector) { |
530 | return false; | |
531 | } | |
d0e14376 MA |
532 | return true; |
533 | } | |
534 | ||
37b06f8d SG |
535 | static uint8_t virtio_blk_handle_discard_write_zeroes(VirtIOBlockReq *req, |
536 | struct virtio_blk_discard_write_zeroes *dwz_hdr, bool is_write_zeroes) | |
537 | { | |
538 | VirtIOBlock *s = req->dev; | |
539 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
540 | uint64_t sector; | |
541 | uint32_t num_sectors, flags, max_sectors; | |
542 | uint8_t err_status; | |
543 | int bytes; | |
544 | ||
545 | sector = virtio_ldq_p(vdev, &dwz_hdr->sector); | |
546 | num_sectors = virtio_ldl_p(vdev, &dwz_hdr->num_sectors); | |
547 | flags = virtio_ldl_p(vdev, &dwz_hdr->flags); | |
548 | max_sectors = is_write_zeroes ? s->conf.max_write_zeroes_sectors : | |
549 | s->conf.max_discard_sectors; | |
550 | ||
551 | /* | |
552 | * max_sectors is at most BDRV_REQUEST_MAX_SECTORS, this check | |
553 | * make us sure that "num_sectors << BDRV_SECTOR_BITS" can fit in | |
554 | * the integer variable. | |
555 | */ | |
556 | if (unlikely(num_sectors > max_sectors)) { | |
557 | err_status = VIRTIO_BLK_S_IOERR; | |
558 | goto err; | |
559 | } | |
560 | ||
561 | bytes = num_sectors << BDRV_SECTOR_BITS; | |
562 | ||
563 | if (unlikely(!virtio_blk_sect_range_ok(s, sector, bytes))) { | |
564 | err_status = VIRTIO_BLK_S_IOERR; | |
565 | goto err; | |
566 | } | |
567 | ||
568 | /* | |
569 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard | |
570 | * and write zeroes commands if any unknown flag is set. | |
571 | */ | |
572 | if (unlikely(flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { | |
573 | err_status = VIRTIO_BLK_S_UNSUPP; | |
574 | goto err; | |
575 | } | |
576 | ||
577 | if (is_write_zeroes) { /* VIRTIO_BLK_T_WRITE_ZEROES */ | |
578 | int blk_aio_flags = 0; | |
579 | ||
580 | if (flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) { | |
581 | blk_aio_flags |= BDRV_REQ_MAY_UNMAP; | |
582 | } | |
583 | ||
584 | block_acct_start(blk_get_stats(s->blk), &req->acct, bytes, | |
585 | BLOCK_ACCT_WRITE); | |
586 | ||
587 | blk_aio_pwrite_zeroes(s->blk, sector << BDRV_SECTOR_BITS, | |
588 | bytes, blk_aio_flags, | |
589 | virtio_blk_discard_write_zeroes_complete, req); | |
590 | } else { /* VIRTIO_BLK_T_DISCARD */ | |
591 | /* | |
592 | * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for | |
593 | * discard commands if the unmap flag is set. | |
594 | */ | |
595 | if (unlikely(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) { | |
596 | err_status = VIRTIO_BLK_S_UNSUPP; | |
597 | goto err; | |
598 | } | |
599 | ||
600 | blk_aio_pdiscard(s->blk, sector << BDRV_SECTOR_BITS, bytes, | |
601 | virtio_blk_discard_write_zeroes_complete, req); | |
602 | } | |
603 | ||
604 | return VIRTIO_BLK_S_OK; | |
605 | ||
606 | err: | |
607 | if (is_write_zeroes) { | |
608 | block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE); | |
609 | } | |
610 | return err_status; | |
611 | } | |
612 | ||
20ea686a | 613 | static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
bc6694d4 | 614 | { |
92e3c2a3 | 615 | uint32_t type; |
f897bf75 | 616 | struct iovec *in_iov = req->elem.in_sg; |
5636da76 | 617 | struct iovec *out_iov = req->elem.out_sg; |
f897bf75 SH |
618 | unsigned in_num = req->elem.in_num; |
619 | unsigned out_num = req->elem.out_num; | |
20ea686a GK |
620 | VirtIOBlock *s = req->dev; |
621 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
92e3c2a3 | 622 | |
f897bf75 | 623 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
20ea686a GK |
624 | virtio_error(vdev, "virtio-blk missing headers"); |
625 | return -1; | |
bc6694d4 KW |
626 | } |
627 | ||
5636da76 | 628 | if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out, |
827805a2 | 629 | sizeof(req->out)) != sizeof(req->out))) { |
20ea686a GK |
630 | virtio_error(vdev, "virtio-blk request outhdr too short"); |
631 | return -1; | |
827805a2 | 632 | } |
ee17e848 | 633 | |
5636da76 | 634 | iov_discard_front(&out_iov, &out_num, sizeof(req->out)); |
ee17e848 | 635 | |
12048545 | 636 | if (in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { |
20ea686a GK |
637 | virtio_error(vdev, "virtio-blk request inhdr too short"); |
638 | return -1; | |
ee17e848 FZ |
639 | } |
640 | ||
2a6cdd6d PB |
641 | /* We always touch the last byte, so just see how big in_iov is. */ |
642 | req->in_len = iov_size(in_iov, in_num); | |
ee17e848 FZ |
643 | req->in = (void *)in_iov[in_num - 1].iov_base |
644 | + in_iov[in_num - 1].iov_len | |
645 | - sizeof(struct virtio_blk_inhdr); | |
646 | iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); | |
bc6694d4 | 647 | |
9a6719d5 | 648 | type = virtio_ldl_p(vdev, &req->out.type); |
92e3c2a3 | 649 | |
95f7142a | 650 | /* VIRTIO_BLK_T_OUT defines the command direction. VIRTIO_BLK_T_BARRIER |
631b22ea | 651 | * is an optional flag. Although a guest should not send this flag if |
95f7142a PL |
652 | * not negotiated we ignored it in the past. So keep ignoring it. */ |
653 | switch (type & ~(VIRTIO_BLK_T_OUT | VIRTIO_BLK_T_BARRIER)) { | |
654 | case VIRTIO_BLK_T_IN: | |
655 | { | |
656 | bool is_write = type & VIRTIO_BLK_T_OUT; | |
9a6719d5 | 657 | req->sector_num = virtio_ldq_p(vdev, &req->out.sector); |
95f7142a PL |
658 | |
659 | if (is_write) { | |
5636da76 | 660 | qemu_iovec_init_external(&req->qiov, out_iov, out_num); |
a576ceac | 661 | trace_virtio_blk_handle_write(vdev, req, req->sector_num, |
95f7142a PL |
662 | req->qiov.size / BDRV_SECTOR_SIZE); |
663 | } else { | |
664 | qemu_iovec_init_external(&req->qiov, in_iov, in_num); | |
a576ceac | 665 | trace_virtio_blk_handle_read(vdev, req, req->sector_num, |
95f7142a PL |
666 | req->qiov.size / BDRV_SECTOR_SIZE); |
667 | } | |
668 | ||
9a6719d5 | 669 | if (!virtio_blk_sect_range_ok(s, req->sector_num, req->qiov.size)) { |
95f7142a | 670 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
9a6719d5 | 671 | block_acct_invalid(blk_get_stats(s->blk), |
01762e03 | 672 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ); |
95f7142a | 673 | virtio_blk_free_request(req); |
20ea686a | 674 | return 0; |
95f7142a PL |
675 | } |
676 | ||
9a6719d5 | 677 | block_acct_start(blk_get_stats(s->blk), &req->acct, req->qiov.size, |
95f7142a PL |
678 | is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ); |
679 | ||
680 | /* merge would exceed maximum number of requests or IO direction | |
681 | * changes */ | |
682 | if (mrb->num_reqs > 0 && (mrb->num_reqs == VIRTIO_BLK_MAX_MERGE_REQS || | |
c99495ac | 683 | is_write != mrb->is_write || |
9a6719d5 SG |
684 | !s->conf.request_merging)) { |
685 | virtio_blk_submit_multireq(s->blk, mrb); | |
95f7142a PL |
686 | } |
687 | ||
688 | assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS); | |
689 | mrb->reqs[mrb->num_reqs++] = req; | |
690 | mrb->is_write = is_write; | |
691 | break; | |
692 | } | |
693 | case VIRTIO_BLK_T_FLUSH: | |
c20fd872 | 694 | virtio_blk_handle_flush(req, mrb); |
95f7142a PL |
695 | break; |
696 | case VIRTIO_BLK_T_SCSI_CMD: | |
bc6694d4 | 697 | virtio_blk_handle_scsi(req); |
95f7142a PL |
698 | break; |
699 | case VIRTIO_BLK_T_GET_ID: | |
700 | { | |
a8686a9b MA |
701 | /* |
702 | * NB: per existing s/n string convention the string is | |
703 | * terminated by '\0' only when shorter than buffer. | |
704 | */ | |
2a30307f | 705 | const char *serial = s->conf.serial ? s->conf.serial : ""; |
a83ceea8 MM |
706 | size_t size = MIN(strlen(serial) + 1, |
707 | MIN(iov_size(in_iov, in_num), | |
708 | VIRTIO_BLK_ID_BYTES)); | |
709 | iov_from_buf(in_iov, in_num, 0, serial, size); | |
2930b313 | 710 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
671ec3f0 | 711 | virtio_blk_free_request(req); |
95f7142a PL |
712 | break; |
713 | } | |
37b06f8d SG |
714 | /* |
715 | * VIRTIO_BLK_T_DISCARD and VIRTIO_BLK_T_WRITE_ZEROES are defined with | |
716 | * VIRTIO_BLK_T_OUT flag set. We masked this flag in the switch statement, | |
717 | * so we must mask it for these requests, then we will check if it is set. | |
718 | */ | |
719 | case VIRTIO_BLK_T_DISCARD & ~VIRTIO_BLK_T_OUT: | |
720 | case VIRTIO_BLK_T_WRITE_ZEROES & ~VIRTIO_BLK_T_OUT: | |
721 | { | |
722 | struct virtio_blk_discard_write_zeroes dwz_hdr; | |
723 | size_t out_len = iov_size(out_iov, out_num); | |
724 | bool is_write_zeroes = (type & ~VIRTIO_BLK_T_BARRIER) == | |
725 | VIRTIO_BLK_T_WRITE_ZEROES; | |
726 | uint8_t err_status; | |
727 | ||
728 | /* | |
729 | * Unsupported if VIRTIO_BLK_T_OUT is not set or the request contains | |
730 | * more than one segment. | |
731 | */ | |
732 | if (unlikely(!(type & VIRTIO_BLK_T_OUT) || | |
733 | out_len > sizeof(dwz_hdr))) { | |
734 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); | |
735 | virtio_blk_free_request(req); | |
736 | return 0; | |
737 | } | |
738 | ||
739 | if (unlikely(iov_to_buf(out_iov, out_num, 0, &dwz_hdr, | |
740 | sizeof(dwz_hdr)) != sizeof(dwz_hdr))) { | |
741 | virtio_error(vdev, "virtio-blk discard/write_zeroes header" | |
742 | " too short"); | |
743 | return -1; | |
744 | } | |
745 | ||
746 | err_status = virtio_blk_handle_discard_write_zeroes(req, &dwz_hdr, | |
747 | is_write_zeroes); | |
748 | if (err_status != VIRTIO_BLK_S_OK) { | |
749 | virtio_blk_req_complete(req, err_status); | |
750 | virtio_blk_free_request(req); | |
751 | } | |
752 | ||
753 | break; | |
754 | } | |
95f7142a | 755 | default: |
9e72c450 | 756 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
671ec3f0 | 757 | virtio_blk_free_request(req); |
bc6694d4 | 758 | } |
20ea686a | 759 | return 0; |
bc6694d4 KW |
760 | } |
761 | ||
07931698 | 762 | bool virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq) |
6e02c38d | 763 | { |
6e02c38d | 764 | VirtIOBlockReq *req; |
95f7142a | 765 | MultiReqBuffer mrb = {}; |
07931698 | 766 | bool progress = false; |
6e02c38d | 767 | |
9d456654 | 768 | aio_context_acquire(blk_get_aio_context(s->blk)); |
fc73548e SH |
769 | blk_io_plug(s->blk); |
770 | ||
9ef9d402 SH |
771 | do { |
772 | virtio_queue_set_notification(vq, 0); | |
773 | ||
774 | while ((req = virtio_blk_get_request(s, vq))) { | |
07931698 | 775 | progress = true; |
9ef9d402 SH |
776 | if (virtio_blk_handle_request(req, &mrb)) { |
777 | virtqueue_detach_element(req->vq, &req->elem, 0); | |
778 | virtio_blk_free_request(req); | |
779 | break; | |
780 | } | |
20ea686a | 781 | } |
9ef9d402 SH |
782 | |
783 | virtio_queue_set_notification(vq, 1); | |
784 | } while (!virtio_queue_empty(vq)); | |
91553dcc | 785 | |
95f7142a PL |
786 | if (mrb.num_reqs) { |
787 | virtio_blk_submit_multireq(s->blk, &mrb); | |
788 | } | |
fc73548e SH |
789 | |
790 | blk_io_unplug(s->blk); | |
9d456654 | 791 | aio_context_release(blk_get_aio_context(s->blk)); |
07931698 FZ |
792 | return progress; |
793 | } | |
794 | ||
795 | static void virtio_blk_handle_output_do(VirtIOBlock *s, VirtQueue *vq) | |
796 | { | |
797 | virtio_blk_handle_vq(s, vq); | |
6e02c38d AL |
798 | } |
799 | ||
8a2fad57 MT |
800 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
801 | { | |
802 | VirtIOBlock *s = (VirtIOBlock *)vdev; | |
803 | ||
804 | if (s->dataplane) { | |
805 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start | |
806 | * dataplane here instead of waiting for .set_status(). | |
807 | */ | |
9ffe337c | 808 | virtio_device_start_ioeventfd(vdev); |
8a2fad57 MT |
809 | if (!s->dataplane_disabled) { |
810 | return; | |
811 | } | |
812 | } | |
07931698 | 813 | virtio_blk_handle_output_do(s, vq); |
8a2fad57 MT |
814 | } |
815 | ||
213189ab | 816 | static void virtio_blk_dma_restart_bh(void *opaque) |
869a5c6d AL |
817 | { |
818 | VirtIOBlock *s = opaque; | |
819 | VirtIOBlockReq *req = s->rq; | |
95f7142a | 820 | MultiReqBuffer mrb = {}; |
869a5c6d | 821 | |
213189ab MA |
822 | qemu_bh_delete(s->bh); |
823 | s->bh = NULL; | |
869a5c6d AL |
824 | |
825 | s->rq = NULL; | |
826 | ||
1919631e | 827 | aio_context_acquire(blk_get_aio_context(s->conf.conf.blk)); |
869a5c6d | 828 | while (req) { |
1bdb176a | 829 | VirtIOBlockReq *next = req->next; |
20ea686a GK |
830 | if (virtio_blk_handle_request(req, &mrb)) { |
831 | /* Device is now broken and won't do any processing until it gets | |
832 | * reset. Already queued requests will be lost: let's purge them. | |
833 | */ | |
834 | while (req) { | |
835 | next = req->next; | |
836 | virtqueue_detach_element(req->vq, &req->elem, 0); | |
837 | virtio_blk_free_request(req); | |
838 | req = next; | |
839 | } | |
840 | break; | |
841 | } | |
1bdb176a | 842 | req = next; |
869a5c6d | 843 | } |
f1b52868 | 844 | |
95f7142a PL |
845 | if (mrb.num_reqs) { |
846 | virtio_blk_submit_multireq(s->blk, &mrb); | |
847 | } | |
680f2002 | 848 | blk_dec_in_flight(s->conf.conf.blk); |
1919631e | 849 | aio_context_release(blk_get_aio_context(s->conf.conf.blk)); |
869a5c6d AL |
850 | } |
851 | ||
1dfb4dd9 LC |
852 | static void virtio_blk_dma_restart_cb(void *opaque, int running, |
853 | RunState state) | |
213189ab MA |
854 | { |
855 | VirtIOBlock *s = opaque; | |
856 | ||
392808b4 | 857 | if (!running) { |
213189ab | 858 | return; |
392808b4 | 859 | } |
213189ab MA |
860 | |
861 | if (!s->bh) { | |
680f2002 KW |
862 | /* FIXME The data plane is not started yet, so these requests are |
863 | * processed in the main thread. */ | |
4be74634 | 864 | s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk), |
4407c1c5 | 865 | virtio_blk_dma_restart_bh, s); |
680f2002 | 866 | blk_inc_in_flight(s->conf.conf.blk); |
213189ab MA |
867 | qemu_bh_schedule(s->bh); |
868 | } | |
869 | } | |
870 | ||
6e02c38d AL |
871 | static void virtio_blk_reset(VirtIODevice *vdev) |
872 | { | |
1cc91b7d | 873 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
6e40b3bf | 874 | AioContext *ctx; |
26307f6a | 875 | VirtIOBlockReq *req; |
392808b4 | 876 | |
6e40b3bf AY |
877 | ctx = blk_get_aio_context(s->blk); |
878 | aio_context_acquire(ctx); | |
879 | blk_drain(s->blk); | |
880 | ||
26307f6a FZ |
881 | /* We drop queued requests after blk_drain() because blk_drain() itself can |
882 | * produce them. */ | |
883 | while (s->rq) { | |
884 | req = s->rq; | |
885 | s->rq = req->next; | |
97b93c8a | 886 | virtqueue_detach_element(req->vq, &req->elem, 0); |
26307f6a FZ |
887 | virtio_blk_free_request(req); |
888 | } | |
889 | ||
6e40b3bf AY |
890 | aio_context_release(ctx); |
891 | ||
9ffe337c | 892 | assert(!s->dataplane_started); |
4be74634 | 893 | blk_set_enable_write_cache(s->blk, s->original_wce); |
6e02c38d AL |
894 | } |
895 | ||
bf011293 | 896 | /* coalesce internal state, copy to pci i/o region 0 |
897 | */ | |
6e02c38d AL |
898 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
899 | { | |
1cc91b7d | 900 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
2a30307f | 901 | BlockConf *conf = &s->conf.conf; |
6e02c38d AL |
902 | struct virtio_blk_config blkcfg; |
903 | uint64_t capacity; | |
17d0bc01 | 904 | int64_t length; |
f7516731 | 905 | int blk_size = conf->logical_block_size; |
6e02c38d | 906 | |
4be74634 | 907 | blk_get_geometry(s->blk, &capacity); |
5c5dafdc | 908 | memset(&blkcfg, 0, sizeof(blkcfg)); |
783d1897 RR |
909 | virtio_stq_p(vdev, &blkcfg.capacity, capacity); |
910 | virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2); | |
907eb3e5 | 911 | virtio_stw_p(vdev, &blkcfg.geometry.cylinders, conf->cyls); |
783d1897 | 912 | virtio_stl_p(vdev, &blkcfg.blk_size, blk_size); |
f7516731 MA |
913 | virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size); |
914 | virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size); | |
907eb3e5 | 915 | blkcfg.geometry.heads = conf->heads; |
136be99e CB |
916 | /* |
917 | * We must ensure that the block device capacity is a multiple of | |
e03ba136 | 918 | * the logical block size. If that is not the case, let's use |
136be99e CB |
919 | * sector_mask to adopt the geometry to have a correct picture. |
920 | * For those devices where the capacity is ok for the given geometry | |
e03ba136 | 921 | * we don't touch the sector value of the geometry, since some devices |
136be99e CB |
922 | * (like s390 dasd) need a specific value. Here the capacity is already |
923 | * cyls*heads*secs*blk_size and the sector value is not block size | |
924 | * divided by 512 - instead it is the amount of blk_size blocks | |
925 | * per track (cylinder). | |
926 | */ | |
17d0bc01 SH |
927 | length = blk_getlength(s->blk); |
928 | if (length > 0 && length / conf->heads / conf->secs % blk_size) { | |
907eb3e5 | 929 | blkcfg.geometry.sectors = conf->secs & ~s->sector_mask; |
136be99e | 930 | } else { |
907eb3e5 | 931 | blkcfg.geometry.sectors = conf->secs; |
136be99e | 932 | } |
c7085da7 | 933 | blkcfg.size_max = 0; |
f7516731 | 934 | blkcfg.physical_block_exp = get_physical_block_exp(conf); |
9752c371 | 935 | blkcfg.alignment_offset = 0; |
4be74634 | 936 | blkcfg.wce = blk_enable_write_cache(s->blk); |
2f270590 | 937 | virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues); |
37b06f8d SG |
938 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) { |
939 | virtio_stl_p(vdev, &blkcfg.max_discard_sectors, | |
940 | s->conf.max_discard_sectors); | |
941 | virtio_stl_p(vdev, &blkcfg.discard_sector_alignment, | |
942 | blk_size >> BDRV_SECTOR_BITS); | |
943 | /* | |
944 | * We support only one segment per request since multiple segments | |
945 | * are not widely used and there are no userspace APIs that allow | |
946 | * applications to submit multiple segments in a single call. | |
947 | */ | |
948 | virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1); | |
949 | } | |
950 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) { | |
951 | virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors, | |
952 | s->conf.max_write_zeroes_sectors); | |
953 | blkcfg.write_zeroes_may_unmap = 1; | |
954 | virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1); | |
955 | } | |
20764be0 | 956 | memcpy(config, &blkcfg, s->config_size); |
6e02c38d AL |
957 | } |
958 | ||
13e3dce0 PB |
959 | static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
960 | { | |
1cc91b7d | 961 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
13e3dce0 PB |
962 | struct virtio_blk_config blkcfg; |
963 | ||
20764be0 | 964 | memcpy(&blkcfg, config, s->config_size); |
6d7e73d6 | 965 | |
4be74634 MA |
966 | aio_context_acquire(blk_get_aio_context(s->blk)); |
967 | blk_set_enable_write_cache(s->blk, blkcfg.wce != 0); | |
968 | aio_context_release(blk_get_aio_context(s->blk)); | |
13e3dce0 PB |
969 | } |
970 | ||
9d5b731d JW |
971 | static uint64_t virtio_blk_get_features(VirtIODevice *vdev, uint64_t features, |
972 | Error **errp) | |
6e02c38d | 973 | { |
1cc91b7d | 974 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
1063b8b1 | 975 | |
bbe8bd4d SG |
976 | /* Firstly sync all virtio-blk possible supported features */ |
977 | features |= s->host_features; | |
978 | ||
0cd09c3a CH |
979 | virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); |
980 | virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); | |
981 | virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); | |
982 | virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); | |
95129d6f | 983 | if (virtio_has_feature(features, VIRTIO_F_VERSION_1)) { |
bbe8bd4d | 984 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_SCSI)) { |
efb8206c JW |
985 | error_setg(errp, "Please set scsi=off for virtio-blk devices in order to use virtio 1.0"); |
986 | return 0; | |
987 | } | |
efb8206c | 988 | } else { |
c9b11f97 | 989 | virtio_clear_feature(&features, VIRTIO_F_ANY_LAYOUT); |
efb8206c JW |
990 | virtio_add_feature(&features, VIRTIO_BLK_F_SCSI); |
991 | } | |
aa659be3 | 992 | |
4be74634 | 993 | if (blk_enable_write_cache(s->blk)) { |
0cd09c3a | 994 | virtio_add_feature(&features, VIRTIO_BLK_F_WCE); |
4be74634 MA |
995 | } |
996 | if (blk_is_read_only(s->blk)) { | |
0cd09c3a | 997 | virtio_add_feature(&features, VIRTIO_BLK_F_RO); |
4be74634 | 998 | } |
2f270590 SH |
999 | if (s->conf.num_queues > 1) { |
1000 | virtio_add_feature(&features, VIRTIO_BLK_F_MQ); | |
1001 | } | |
1063b8b1 CH |
1002 | |
1003 | return features; | |
6e02c38d AL |
1004 | } |
1005 | ||
9315cbfd PB |
1006 | static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) |
1007 | { | |
1cc91b7d | 1008 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
9315cbfd | 1009 | |
9ffe337c PB |
1010 | if (!(status & (VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK))) { |
1011 | assert(!s->dataplane_started); | |
392808b4 | 1012 | } |
392808b4 | 1013 | |
9315cbfd PB |
1014 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
1015 | return; | |
1016 | } | |
1017 | ||
ef5bc962 PB |
1018 | /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send |
1019 | * cache flushes. Thus, the "auto writethrough" behavior is never | |
1020 | * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature. | |
1021 | * Leaving it enabled would break the following sequence: | |
1022 | * | |
1023 | * Guest started with "-drive cache=writethrough" | |
1024 | * Guest sets status to 0 | |
1025 | * Guest sets DRIVER bit in status field | |
1026 | * Guest reads host features (WCE=0, CONFIG_WCE=1) | |
1027 | * Guest writes guest features (WCE=0, CONFIG_WCE=1) | |
1028 | * Guest writes 1 to the WCE configuration field (writeback mode) | |
1029 | * Guest sets DRIVER_OK bit in status field | |
1030 | * | |
4be74634 | 1031 | * s->blk would erroneously be placed in writethrough mode. |
ef5bc962 | 1032 | */ |
95129d6f | 1033 | if (!virtio_vdev_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) { |
4be74634 MA |
1034 | aio_context_acquire(blk_get_aio_context(s->blk)); |
1035 | blk_set_enable_write_cache(s->blk, | |
95129d6f CH |
1036 | virtio_vdev_has_feature(vdev, |
1037 | VIRTIO_BLK_F_WCE)); | |
4be74634 | 1038 | aio_context_release(blk_get_aio_context(s->blk)); |
ef5bc962 | 1039 | } |
9315cbfd PB |
1040 | } |
1041 | ||
b2b295a7 GK |
1042 | static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f) |
1043 | { | |
1044 | VirtIOBlock *s = VIRTIO_BLK(vdev); | |
1045 | VirtIOBlockReq *req = s->rq; | |
1046 | ||
869a5c6d AL |
1047 | while (req) { |
1048 | qemu_put_sbyte(f, 1); | |
30d8bf6d SH |
1049 | |
1050 | if (s->conf.num_queues > 1) { | |
1051 | qemu_put_be32(f, virtio_get_queue_index(req->vq)); | |
1052 | } | |
1053 | ||
ab281c17 | 1054 | qemu_put_virtqueue_element(f, &req->elem); |
869a5c6d AL |
1055 | req = req->next; |
1056 | } | |
1057 | qemu_put_sbyte(f, 0); | |
6e02c38d AL |
1058 | } |
1059 | ||
b2b295a7 GK |
1060 | static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, |
1061 | int version_id) | |
1062 | { | |
1063 | VirtIOBlock *s = VIRTIO_BLK(vdev); | |
2a633c46 | 1064 | |
869a5c6d | 1065 | while (qemu_get_sbyte(f)) { |
30d8bf6d SH |
1066 | unsigned nvqs = s->conf.num_queues; |
1067 | unsigned vq_idx = 0; | |
ab281c17 | 1068 | VirtIOBlockReq *req; |
30d8bf6d SH |
1069 | |
1070 | if (nvqs > 1) { | |
1071 | vq_idx = qemu_get_be32(f); | |
1072 | ||
1073 | if (vq_idx >= nvqs) { | |
1074 | error_report("Invalid virtqueue index in request list: %#x", | |
1075 | vq_idx); | |
1076 | return -EINVAL; | |
1077 | } | |
1078 | } | |
1079 | ||
8607f5c3 | 1080 | req = qemu_get_virtqueue_element(vdev, f, sizeof(VirtIOBlockReq)); |
30d8bf6d | 1081 | virtio_blk_init_request(s, virtio_get_queue(vdev, vq_idx), req); |
869a5c6d | 1082 | req->next = s->rq; |
20a81e4d | 1083 | s->rq = req; |
869a5c6d | 1084 | } |
6e02c38d AL |
1085 | |
1086 | return 0; | |
1087 | } | |
1088 | ||
145feb17 | 1089 | static void virtio_blk_resize(void *opaque) |
e5051fc7 | 1090 | { |
1cc91b7d | 1091 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
e5051fc7 | 1092 | |
1cc91b7d | 1093 | virtio_notify_config(vdev); |
e5051fc7 CH |
1094 | } |
1095 | ||
0e49de52 | 1096 | static const BlockDevOps virtio_block_ops = { |
145feb17 | 1097 | .resize_cb = virtio_blk_resize, |
0e49de52 MA |
1098 | }; |
1099 | ||
75884afd | 1100 | static void virtio_blk_device_realize(DeviceState *dev, Error **errp) |
1c028ddf | 1101 | { |
75884afd | 1102 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
179b417e | 1103 | VirtIOBlock *s = VIRTIO_BLK(dev); |
2a30307f | 1104 | VirtIOBlkConf *conf = &s->conf; |
3ffeeef7 | 1105 | Error *err = NULL; |
2f270590 | 1106 | unsigned i; |
cf21e106 | 1107 | |
4be74634 | 1108 | if (!conf->conf.blk) { |
75884afd AF |
1109 | error_setg(errp, "drive property not set"); |
1110 | return; | |
d75d25e3 | 1111 | } |
4be74634 | 1112 | if (!blk_is_inserted(conf->conf.blk)) { |
75884afd AF |
1113 | error_setg(errp, "Device needs media, but drive is empty"); |
1114 | return; | |
98f28ad7 | 1115 | } |
2f270590 SH |
1116 | if (!conf->num_queues) { |
1117 | error_setg(errp, "num-queues property must be larger than 0"); | |
1118 | return; | |
1119 | } | |
6040aedd MK |
1120 | if (!is_power_of_2(conf->queue_size) || |
1121 | conf->queue_size > VIRTQUEUE_MAX_SIZE) { | |
1122 | error_setg(errp, "invalid queue-size property (%" PRIu16 "), " | |
1123 | "must be a power of 2 (max %d)", | |
1124 | conf->queue_size, VIRTQUEUE_MAX_SIZE); | |
1125 | return; | |
1126 | } | |
d75d25e3 | 1127 | |
ceff3e1f MZ |
1128 | if (!blkconf_apply_backend_options(&conf->conf, |
1129 | blk_is_read_only(conf->conf.blk), true, | |
1130 | errp)) { | |
a17c17a2 KW |
1131 | return; |
1132 | } | |
4be74634 | 1133 | s->original_wce = blk_enable_write_cache(conf->conf.blk); |
ceff3e1f | 1134 | if (!blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, errp)) { |
75884afd | 1135 | return; |
b7eb0c9f | 1136 | } |
ceff3e1f | 1137 | |
0eb28a42 | 1138 | blkconf_blocksizes(&conf->conf); |
a8686a9b | 1139 | |
0a75b60c MK |
1140 | if (conf->conf.logical_block_size > |
1141 | conf->conf.physical_block_size) { | |
1142 | error_setg(errp, | |
1143 | "logical_block_size > physical_block_size not supported"); | |
1144 | return; | |
1145 | } | |
1146 | ||
37b06f8d SG |
1147 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) && |
1148 | (!conf->max_discard_sectors || | |
1149 | conf->max_discard_sectors > BDRV_REQUEST_MAX_SECTORS)) { | |
1150 | error_setg(errp, "invalid max-discard-sectors property (%" PRIu32 ")" | |
1151 | ", must be between 1 and %d", | |
1152 | conf->max_discard_sectors, (int)BDRV_REQUEST_MAX_SECTORS); | |
1153 | return; | |
1154 | } | |
1155 | ||
1156 | if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) && | |
1157 | (!conf->max_write_zeroes_sectors || | |
1158 | conf->max_write_zeroes_sectors > BDRV_REQUEST_MAX_SECTORS)) { | |
1159 | error_setg(errp, "invalid max-write-zeroes-sectors property (%" PRIu32 | |
1160 | "), must be between 1 and %d", | |
1161 | conf->max_write_zeroes_sectors, | |
1162 | (int)BDRV_REQUEST_MAX_SECTORS); | |
1163 | return; | |
1164 | } | |
1165 | ||
20764be0 SG |
1166 | virtio_blk_set_config_size(s, s->host_features); |
1167 | ||
1168 | virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, s->config_size); | |
6e02c38d | 1169 | |
4be74634 | 1170 | s->blk = conf->conf.blk; |
869a5c6d | 1171 | s->rq = NULL; |
2a30307f | 1172 | s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1; |
e63e7fde | 1173 | |
2f270590 | 1174 | for (i = 0; i < conf->num_queues; i++) { |
6040aedd | 1175 | virtio_add_queue(vdev, conf->queue_size, virtio_blk_handle_output); |
2f270590 | 1176 | } |
2a30307f | 1177 | virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err); |
3ffeeef7 | 1178 | if (err != NULL) { |
75884afd | 1179 | error_propagate(errp, err); |
6a1a8cc7 | 1180 | virtio_cleanup(vdev); |
75884afd | 1181 | return; |
392808b4 | 1182 | } |
6e02c38d | 1183 | |
69b302b2 | 1184 | s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
4be74634 MA |
1185 | blk_set_dev_ops(s->blk, &virtio_block_ops, s); |
1186 | blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size); | |
6e02c38d | 1187 | |
4be74634 | 1188 | blk_iostatus_enable(s->blk); |
1c028ddf FK |
1189 | } |
1190 | ||
306ec6c3 | 1191 | static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp) |
1c028ddf | 1192 | { |
306ec6c3 AF |
1193 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
1194 | VirtIOBlock *s = VIRTIO_BLK(dev); | |
1195 | ||
1c028ddf FK |
1196 | virtio_blk_data_plane_destroy(s->dataplane); |
1197 | s->dataplane = NULL; | |
1c028ddf | 1198 | qemu_del_vm_change_state_handler(s->change); |
4be74634 | 1199 | blockdev_mark_auto_del(s->blk); |
6a1a8cc7 | 1200 | virtio_cleanup(vdev); |
1c028ddf FK |
1201 | } |
1202 | ||
467b3f33 SH |
1203 | static void virtio_blk_instance_init(Object *obj) |
1204 | { | |
1205 | VirtIOBlock *s = VIRTIO_BLK(obj); | |
1206 | ||
2a30307f | 1207 | device_add_bootindex_property(obj, &s->conf.conf.bootindex, |
3342ec32 GA |
1208 | "bootindex", "/disk@0,0", |
1209 | DEVICE(obj), NULL); | |
467b3f33 SH |
1210 | } |
1211 | ||
977a117f HP |
1212 | static const VMStateDescription vmstate_virtio_blk = { |
1213 | .name = "virtio-blk", | |
1214 | .minimum_version_id = 2, | |
1215 | .version_id = 2, | |
1216 | .fields = (VMStateField[]) { | |
1217 | VMSTATE_VIRTIO_DEVICE, | |
1218 | VMSTATE_END_OF_LIST() | |
1219 | }, | |
1220 | }; | |
bbded32c | 1221 | |
1c028ddf | 1222 | static Property virtio_blk_properties[] = { |
2a30307f | 1223 | DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf), |
8c398252 | 1224 | DEFINE_BLOCK_ERROR_PROPERTIES(VirtIOBlock, conf.conf), |
2a30307f MA |
1225 | DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf), |
1226 | DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial), | |
bbe8bd4d SG |
1227 | DEFINE_PROP_BIT64("config-wce", VirtIOBlock, host_features, |
1228 | VIRTIO_BLK_F_CONFIG_WCE, true), | |
32a877e4 | 1229 | #ifdef __linux__ |
bbe8bd4d SG |
1230 | DEFINE_PROP_BIT64("scsi", VirtIOBlock, host_features, |
1231 | VIRTIO_BLK_F_SCSI, false), | |
32a877e4 | 1232 | #endif |
c99495ac PL |
1233 | DEFINE_PROP_BIT("request-merging", VirtIOBlock, conf.request_merging, 0, |
1234 | true), | |
2f270590 | 1235 | DEFINE_PROP_UINT16("num-queues", VirtIOBlock, conf.num_queues, 1), |
6040aedd | 1236 | DEFINE_PROP_UINT16("queue-size", VirtIOBlock, conf.queue_size, 128), |
d679ac09 FZ |
1237 | DEFINE_PROP_LINK("iothread", VirtIOBlock, conf.iothread, TYPE_IOTHREAD, |
1238 | IOThread *), | |
5c81161f SG |
1239 | DEFINE_PROP_BIT64("discard", VirtIOBlock, host_features, |
1240 | VIRTIO_BLK_F_DISCARD, true), | |
1241 | DEFINE_PROP_BIT64("write-zeroes", VirtIOBlock, host_features, | |
1242 | VIRTIO_BLK_F_WRITE_ZEROES, true), | |
37b06f8d SG |
1243 | DEFINE_PROP_UINT32("max-discard-sectors", VirtIOBlock, |
1244 | conf.max_discard_sectors, BDRV_REQUEST_MAX_SECTORS), | |
1245 | DEFINE_PROP_UINT32("max-write-zeroes-sectors", VirtIOBlock, | |
1246 | conf.max_write_zeroes_sectors, BDRV_REQUEST_MAX_SECTORS), | |
1c028ddf FK |
1247 | DEFINE_PROP_END_OF_LIST(), |
1248 | }; | |
1249 | ||
1250 | static void virtio_blk_class_init(ObjectClass *klass, void *data) | |
1251 | { | |
1252 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1253 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
75884afd | 1254 | |
1c028ddf | 1255 | dc->props = virtio_blk_properties; |
bbded32c | 1256 | dc->vmsd = &vmstate_virtio_blk; |
125ee0ed | 1257 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
75884afd | 1258 | vdc->realize = virtio_blk_device_realize; |
306ec6c3 | 1259 | vdc->unrealize = virtio_blk_device_unrealize; |
1c028ddf FK |
1260 | vdc->get_config = virtio_blk_update_config; |
1261 | vdc->set_config = virtio_blk_set_config; | |
1262 | vdc->get_features = virtio_blk_get_features; | |
1263 | vdc->set_status = virtio_blk_set_status; | |
1264 | vdc->reset = virtio_blk_reset; | |
b2b295a7 GK |
1265 | vdc->save = virtio_blk_save_device; |
1266 | vdc->load = virtio_blk_load_device; | |
9ffe337c PB |
1267 | vdc->start_ioeventfd = virtio_blk_data_plane_start; |
1268 | vdc->stop_ioeventfd = virtio_blk_data_plane_stop; | |
1c028ddf FK |
1269 | } |
1270 | ||
b5c7ceaf | 1271 | static const TypeInfo virtio_blk_info = { |
1c028ddf FK |
1272 | .name = TYPE_VIRTIO_BLK, |
1273 | .parent = TYPE_VIRTIO_DEVICE, | |
1274 | .instance_size = sizeof(VirtIOBlock), | |
467b3f33 | 1275 | .instance_init = virtio_blk_instance_init, |
1c028ddf FK |
1276 | .class_init = virtio_blk_class_init, |
1277 | }; | |
1278 | ||
1279 | static void virtio_register_types(void) | |
1280 | { | |
b5c7ceaf | 1281 | type_register_static(&virtio_blk_info); |
1c028ddf FK |
1282 | } |
1283 | ||
1284 | type_init(virtio_register_types) |