]>
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 | ||
5a61cb60 | 14 | #include "qemu-common.h" |
827805a2 | 15 | #include "qemu/iov.h" |
1de7afc9 | 16 | #include "qemu/error-report.h" |
6d519a5f | 17 | #include "trace.h" |
0d09e41a | 18 | #include "hw/block/block.h" |
9c17d615 | 19 | #include "sysemu/blockdev.h" |
0d09e41a | 20 | #include "hw/virtio/virtio-blk.h" |
52b53c04 FZ |
21 | #include "dataplane/virtio-blk.h" |
22 | #include "migration/migration.h" | |
0d09e41a | 23 | #include "block/scsi.h" |
1063b8b1 CH |
24 | #ifdef __linux__ |
25 | # include <scsi/sg.h> | |
26 | #endif | |
0d09e41a | 27 | #include "hw/virtio/virtio-bus.h" |
783d1897 | 28 | #include "hw/virtio/virtio-access.h" |
6e02c38d | 29 | |
f897bf75 | 30 | VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s) |
671ec3f0 | 31 | { |
869d66af | 32 | VirtIOBlockReq *req = g_slice_new(VirtIOBlockReq); |
671ec3f0 | 33 | req->dev = s; |
869d66af SH |
34 | req->qiov.size = 0; |
35 | req->next = NULL; | |
671ec3f0 FZ |
36 | return req; |
37 | } | |
38 | ||
f897bf75 | 39 | void virtio_blk_free_request(VirtIOBlockReq *req) |
671ec3f0 FZ |
40 | { |
41 | if (req) { | |
671ec3f0 FZ |
42 | g_slice_free(VirtIOBlockReq, req); |
43 | } | |
44 | } | |
45 | ||
bf4bd461 FZ |
46 | static void virtio_blk_complete_request(VirtIOBlockReq *req, |
47 | unsigned char status) | |
869a5c6d AL |
48 | { |
49 | VirtIOBlock *s = req->dev; | |
1cc91b7d | 50 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
869a5c6d | 51 | |
6d519a5f SH |
52 | trace_virtio_blk_req_complete(req, status); |
53 | ||
92e3c2a3 | 54 | stb_p(&req->in->status, status); |
f897bf75 | 55 | virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in)); |
1cc91b7d | 56 | virtio_notify(vdev, s->vq); |
869a5c6d AL |
57 | } |
58 | ||
bf4bd461 FZ |
59 | static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status) |
60 | { | |
61 | req->dev->complete_request(req, status); | |
62 | } | |
63 | ||
f35d68f0 | 64 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
1ceee0d5 | 65 | bool is_read) |
869a5c6d | 66 | { |
3e1caa5f | 67 | BlockErrorAction action = bdrv_get_error_action(req->dev->bs, is_read, error); |
869a5c6d AL |
68 | VirtIOBlock *s = req->dev; |
69 | ||
a589569f | 70 | if (action == BLOCK_ERROR_ACTION_STOP) { |
869a5c6d AL |
71 | req->next = s->rq; |
72 | s->rq = req; | |
a589569f | 73 | } else if (action == BLOCK_ERROR_ACTION_REPORT) { |
869a5c6d | 74 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
5366d0c8 | 75 | block_acct_done(bdrv_get_stats(s->bs), &req->acct); |
671ec3f0 | 76 | virtio_blk_free_request(req); |
869a5c6d AL |
77 | } |
78 | ||
3e1caa5f | 79 | bdrv_error_action(s->bs, action, is_read, error); |
a589569f | 80 | return action != BLOCK_ERROR_ACTION_IGNORE; |
869a5c6d AL |
81 | } |
82 | ||
6e02c38d AL |
83 | static void virtio_blk_rw_complete(void *opaque, int ret) |
84 | { | |
85 | VirtIOBlockReq *req = opaque; | |
6e02c38d | 86 | |
6d519a5f SH |
87 | trace_virtio_blk_rw_complete(req, ret); |
88 | ||
f35d68f0 | 89 | if (ret) { |
783d1897 RR |
90 | int p = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type); |
91 | bool is_read = !(p & VIRTIO_BLK_T_OUT); | |
f35d68f0 | 92 | if (virtio_blk_handle_rw_error(req, -ret, is_read)) |
869a5c6d | 93 | return; |
6e02c38d AL |
94 | } |
95 | ||
f35d68f0 | 96 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
5366d0c8 | 97 | block_acct_done(bdrv_get_stats(req->dev->bs), &req->acct); |
671ec3f0 | 98 | virtio_blk_free_request(req); |
869a5c6d | 99 | } |
6e02c38d | 100 | |
aa659be3 CH |
101 | static void virtio_blk_flush_complete(void *opaque, int ret) |
102 | { | |
103 | VirtIOBlockReq *req = opaque; | |
104 | ||
8c269b54 KW |
105 | if (ret) { |
106 | if (virtio_blk_handle_rw_error(req, -ret, 0)) { | |
107 | return; | |
108 | } | |
109 | } | |
110 | ||
111 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); | |
5366d0c8 | 112 | block_acct_done(bdrv_get_stats(req->dev->bs), &req->acct); |
671ec3f0 | 113 | virtio_blk_free_request(req); |
6e02c38d AL |
114 | } |
115 | ||
116 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s) | |
117 | { | |
869a5c6d | 118 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
6e02c38d | 119 | |
f897bf75 | 120 | if (!virtqueue_pop(s->vq, &req->elem)) { |
671ec3f0 FZ |
121 | virtio_blk_free_request(req); |
122 | return NULL; | |
6e02c38d AL |
123 | } |
124 | ||
125 | return req; | |
126 | } | |
127 | ||
5a05cbee FZ |
128 | int virtio_blk_handle_scsi_req(VirtIOBlock *blk, |
129 | VirtQueueElement *elem) | |
1063b8b1 | 130 | { |
5a05cbee FZ |
131 | int status = VIRTIO_BLK_S_OK; |
132 | struct virtio_scsi_inhdr *scsi = NULL; | |
783d1897 RR |
133 | VirtIODevice *vdev = VIRTIO_DEVICE(blk); |
134 | ||
47ce9ef7 | 135 | #ifdef __linux__ |
1063b8b1 | 136 | int i; |
5a05cbee | 137 | struct sg_io_hdr hdr; |
47ce9ef7 | 138 | #endif |
1063b8b1 CH |
139 | |
140 | /* | |
141 | * We require at least one output segment each for the virtio_blk_outhdr | |
142 | * and the SCSI command block. | |
143 | * | |
144 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr | |
145 | * and the sense buffer pointer in the input segments. | |
146 | */ | |
5a05cbee FZ |
147 | if (elem->out_num < 2 || elem->in_num < 3) { |
148 | status = VIRTIO_BLK_S_IOERR; | |
149 | goto fail; | |
1063b8b1 CH |
150 | } |
151 | ||
152 | /* | |
f34e73cd PB |
153 | * The scsi inhdr is placed in the second-to-last input segment, just |
154 | * before the regular inhdr. | |
1063b8b1 | 155 | */ |
5a05cbee | 156 | scsi = (void *)elem->in_sg[elem->in_num - 2].iov_base; |
f34e73cd | 157 | |
2a30307f | 158 | if (!blk->conf.scsi) { |
f34e73cd PB |
159 | status = VIRTIO_BLK_S_UNSUPP; |
160 | goto fail; | |
1063b8b1 CH |
161 | } |
162 | ||
163 | /* | |
f34e73cd | 164 | * No support for bidirection commands yet. |
1063b8b1 | 165 | */ |
5a05cbee | 166 | if (elem->out_num > 2 && elem->in_num > 3) { |
f34e73cd PB |
167 | status = VIRTIO_BLK_S_UNSUPP; |
168 | goto fail; | |
169 | } | |
1063b8b1 | 170 | |
f34e73cd | 171 | #ifdef __linux__ |
1063b8b1 CH |
172 | memset(&hdr, 0, sizeof(struct sg_io_hdr)); |
173 | hdr.interface_id = 'S'; | |
5a05cbee FZ |
174 | hdr.cmd_len = elem->out_sg[1].iov_len; |
175 | hdr.cmdp = elem->out_sg[1].iov_base; | |
1063b8b1 CH |
176 | hdr.dxfer_len = 0; |
177 | ||
5a05cbee | 178 | if (elem->out_num > 2) { |
1063b8b1 CH |
179 | /* |
180 | * If there are more than the minimally required 2 output segments | |
181 | * there is write payload starting from the third iovec. | |
182 | */ | |
183 | hdr.dxfer_direction = SG_DXFER_TO_DEV; | |
5a05cbee | 184 | hdr.iovec_count = elem->out_num - 2; |
1063b8b1 CH |
185 | |
186 | for (i = 0; i < hdr.iovec_count; i++) | |
5a05cbee | 187 | hdr.dxfer_len += elem->out_sg[i + 2].iov_len; |
1063b8b1 | 188 | |
5a05cbee | 189 | hdr.dxferp = elem->out_sg + 2; |
1063b8b1 | 190 | |
5a05cbee | 191 | } else if (elem->in_num > 3) { |
1063b8b1 CH |
192 | /* |
193 | * If we have more than 3 input segments the guest wants to actually | |
194 | * read data. | |
195 | */ | |
196 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
5a05cbee | 197 | hdr.iovec_count = elem->in_num - 3; |
1063b8b1 | 198 | for (i = 0; i < hdr.iovec_count; i++) |
5a05cbee | 199 | hdr.dxfer_len += elem->in_sg[i].iov_len; |
1063b8b1 | 200 | |
5a05cbee | 201 | hdr.dxferp = elem->in_sg; |
1063b8b1 CH |
202 | } else { |
203 | /* | |
204 | * Some SCSI commands don't actually transfer any data. | |
205 | */ | |
206 | hdr.dxfer_direction = SG_DXFER_NONE; | |
207 | } | |
208 | ||
5a05cbee FZ |
209 | hdr.sbp = elem->in_sg[elem->in_num - 3].iov_base; |
210 | hdr.mx_sb_len = elem->in_sg[elem->in_num - 3].iov_len; | |
1063b8b1 | 211 | |
5a05cbee FZ |
212 | status = bdrv_ioctl(blk->bs, SG_IO, &hdr); |
213 | if (status) { | |
1063b8b1 | 214 | status = VIRTIO_BLK_S_UNSUPP; |
f34e73cd | 215 | goto fail; |
1063b8b1 CH |
216 | } |
217 | ||
5bb23927 PB |
218 | /* |
219 | * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi) | |
220 | * clear the masked_status field [hence status gets cleared too, see | |
221 | * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED | |
222 | * status has occurred. However they do set DRIVER_SENSE in driver_status | |
223 | * field. Also a (sb_len_wr > 0) indicates there is a sense buffer. | |
224 | */ | |
225 | if (hdr.status == 0 && hdr.sb_len_wr > 0) { | |
226 | hdr.status = CHECK_CONDITION; | |
227 | } | |
228 | ||
783d1897 RR |
229 | virtio_stl_p(vdev, &scsi->errors, |
230 | hdr.status | (hdr.msg_status << 8) | | |
231 | (hdr.host_status << 16) | (hdr.driver_status << 24)); | |
232 | virtio_stl_p(vdev, &scsi->residual, hdr.resid); | |
233 | virtio_stl_p(vdev, &scsi->sense_len, hdr.sb_len_wr); | |
234 | virtio_stl_p(vdev, &scsi->data_len, hdr.dxfer_len); | |
1063b8b1 | 235 | |
5a05cbee | 236 | return status; |
1063b8b1 | 237 | #else |
f34e73cd PB |
238 | abort(); |
239 | #endif | |
240 | ||
241 | fail: | |
242 | /* Just put anything nonzero so that the ioctl fails in the guest. */ | |
5a05cbee | 243 | if (scsi) { |
783d1897 | 244 | virtio_stl_p(vdev, &scsi->errors, 255); |
5a05cbee FZ |
245 | } |
246 | return status; | |
247 | } | |
248 | ||
249 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) | |
250 | { | |
251 | int status; | |
252 | ||
f897bf75 | 253 | status = virtio_blk_handle_scsi_req(req->dev, &req->elem); |
f34e73cd | 254 | virtio_blk_req_complete(req, status); |
671ec3f0 | 255 | virtio_blk_free_request(req); |
1063b8b1 | 256 | } |
1063b8b1 | 257 | |
fee65db7 | 258 | void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb) |
869a5c6d | 259 | { |
91553dcc | 260 | int i, ret; |
91553dcc | 261 | |
c20fd872 CH |
262 | if (!mrb->num_writes) { |
263 | return; | |
264 | } | |
265 | ||
266 | ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes); | |
91553dcc | 267 | if (ret != 0) { |
c20fd872 CH |
268 | for (i = 0; i < mrb->num_writes; i++) { |
269 | if (mrb->blkreq[i].error) { | |
270 | virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO); | |
91553dcc KW |
271 | } |
272 | } | |
273 | } | |
c20fd872 CH |
274 | |
275 | mrb->num_writes = 0; | |
91553dcc | 276 | } |
87b245db | 277 | |
c20fd872 | 278 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
aa659be3 | 279 | { |
5366d0c8 BC |
280 | block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, 0, |
281 | BLOCK_ACCT_FLUSH); | |
a597e79c | 282 | |
618fbb84 CH |
283 | /* |
284 | * Make sure all outstanding writes are posted to the backing device. | |
285 | */ | |
c20fd872 | 286 | virtio_submit_multiwrite(req->dev->bs, mrb); |
ad54ae80 | 287 | bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req); |
aa659be3 CH |
288 | } |
289 | ||
d0e14376 MA |
290 | static bool virtio_blk_sect_range_ok(VirtIOBlock *dev, |
291 | uint64_t sector, size_t size) | |
292 | { | |
3c2daac0 MA |
293 | uint64_t nb_sectors = size >> BDRV_SECTOR_BITS; |
294 | uint64_t total_sectors; | |
295 | ||
d0e14376 MA |
296 | if (sector & dev->sector_mask) { |
297 | return false; | |
298 | } | |
2a30307f | 299 | if (size % dev->conf.conf.logical_block_size) { |
d0e14376 MA |
300 | return false; |
301 | } | |
3c2daac0 MA |
302 | bdrv_get_geometry(dev->bs, &total_sectors); |
303 | if (sector > total_sectors || nb_sectors > total_sectors - sector) { | |
304 | return false; | |
305 | } | |
d0e14376 MA |
306 | return true; |
307 | } | |
308 | ||
c20fd872 | 309 | static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
91553dcc | 310 | { |
c20fd872 | 311 | BlockRequest *blkreq; |
92e3c2a3 | 312 | uint64_t sector; |
c20fd872 | 313 | |
783d1897 | 314 | sector = virtio_ldq_p(VIRTIO_DEVICE(req->dev), &req->out.sector); |
6d519a5f | 315 | |
92e3c2a3 AJ |
316 | trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512); |
317 | ||
d0e14376 | 318 | if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) { |
42e38c1f MA |
319 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
320 | virtio_blk_free_request(req); | |
52c05023 CH |
321 | return; |
322 | } | |
8cfacf07 | 323 | |
5366d0c8 BC |
324 | block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, req->qiov.size, |
325 | BLOCK_ACCT_WRITE); | |
42e38c1f | 326 | |
c20fd872 CH |
327 | if (mrb->num_writes == 32) { |
328 | virtio_submit_multiwrite(req->dev->bs, mrb); | |
87b245db | 329 | } |
91553dcc | 330 | |
c20fd872 | 331 | blkreq = &mrb->blkreq[mrb->num_writes]; |
92e3c2a3 | 332 | blkreq->sector = sector; |
c20fd872 CH |
333 | blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE; |
334 | blkreq->qiov = &req->qiov; | |
335 | blkreq->cb = virtio_blk_rw_complete; | |
336 | blkreq->opaque = req; | |
337 | blkreq->error = 0; | |
91553dcc | 338 | |
c20fd872 | 339 | mrb->num_writes++; |
d28a1b6e | 340 | } |
869a5c6d | 341 | |
d28a1b6e AL |
342 | static void virtio_blk_handle_read(VirtIOBlockReq *req) |
343 | { | |
92e3c2a3 AJ |
344 | uint64_t sector; |
345 | ||
783d1897 | 346 | sector = virtio_ldq_p(VIRTIO_DEVICE(req->dev), &req->out.sector); |
87b245db | 347 | |
81b6b9fa SH |
348 | trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512); |
349 | ||
d0e14376 | 350 | if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) { |
42e38c1f MA |
351 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
352 | virtio_blk_free_request(req); | |
52c05023 CH |
353 | return; |
354 | } | |
42e38c1f | 355 | |
5366d0c8 BC |
356 | block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, req->qiov.size, |
357 | BLOCK_ACCT_READ); | |
ad54ae80 PB |
358 | bdrv_aio_readv(req->dev->bs, sector, &req->qiov, |
359 | req->qiov.size / BDRV_SECTOR_SIZE, | |
360 | virtio_blk_rw_complete, req); | |
869a5c6d AL |
361 | } |
362 | ||
fee65db7 | 363 | void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
bc6694d4 | 364 | { |
92e3c2a3 | 365 | uint32_t type; |
f897bf75 SH |
366 | struct iovec *in_iov = req->elem.in_sg; |
367 | struct iovec *iov = req->elem.out_sg; | |
368 | unsigned in_num = req->elem.in_num; | |
369 | unsigned out_num = req->elem.out_num; | |
92e3c2a3 | 370 | |
f897bf75 | 371 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
870cef1d | 372 | error_report("virtio-blk missing headers"); |
bc6694d4 KW |
373 | exit(1); |
374 | } | |
375 | ||
827805a2 FZ |
376 | if (unlikely(iov_to_buf(iov, out_num, 0, &req->out, |
377 | sizeof(req->out)) != sizeof(req->out))) { | |
378 | error_report("virtio-blk request outhdr too short"); | |
379 | exit(1); | |
380 | } | |
ee17e848 | 381 | |
827805a2 | 382 | iov_discard_front(&iov, &out_num, sizeof(req->out)); |
ee17e848 FZ |
383 | |
384 | if (in_num < 1 || | |
385 | in_iov[in_num - 1].iov_len < sizeof(struct virtio_blk_inhdr)) { | |
386 | error_report("virtio-blk request inhdr too short"); | |
387 | exit(1); | |
388 | } | |
389 | ||
390 | req->in = (void *)in_iov[in_num - 1].iov_base | |
391 | + in_iov[in_num - 1].iov_len | |
392 | - sizeof(struct virtio_blk_inhdr); | |
393 | iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr)); | |
bc6694d4 | 394 | |
783d1897 | 395 | type = virtio_ldl_p(VIRTIO_DEVICE(req->dev), &req->out.type); |
92e3c2a3 AJ |
396 | |
397 | if (type & VIRTIO_BLK_T_FLUSH) { | |
c20fd872 | 398 | virtio_blk_handle_flush(req, mrb); |
92e3c2a3 | 399 | } else if (type & VIRTIO_BLK_T_SCSI_CMD) { |
bc6694d4 | 400 | virtio_blk_handle_scsi(req); |
92e3c2a3 | 401 | } else if (type & VIRTIO_BLK_T_GET_ID) { |
2930b313 | 402 | VirtIOBlock *s = req->dev; |
403 | ||
a8686a9b MA |
404 | /* |
405 | * NB: per existing s/n string convention the string is | |
406 | * terminated by '\0' only when shorter than buffer. | |
407 | */ | |
2a30307f | 408 | const char *serial = s->conf.serial ? s->conf.serial : ""; |
a83ceea8 MM |
409 | size_t size = MIN(strlen(serial) + 1, |
410 | MIN(iov_size(in_iov, in_num), | |
411 | VIRTIO_BLK_ID_BYTES)); | |
412 | iov_from_buf(in_iov, in_num, 0, serial, size); | |
2930b313 | 413 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
671ec3f0 | 414 | virtio_blk_free_request(req); |
92e3c2a3 | 415 | } else if (type & VIRTIO_BLK_T_OUT) { |
a83ceea8 | 416 | qemu_iovec_init_external(&req->qiov, iov, out_num); |
c20fd872 | 417 | virtio_blk_handle_write(req, mrb); |
9e72c450 AZ |
418 | } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) { |
419 | /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */ | |
a83ceea8 | 420 | qemu_iovec_init_external(&req->qiov, in_iov, in_num); |
bc6694d4 | 421 | virtio_blk_handle_read(req); |
9e72c450 AZ |
422 | } else { |
423 | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); | |
671ec3f0 | 424 | virtio_blk_free_request(req); |
bc6694d4 KW |
425 | } |
426 | } | |
427 | ||
6e02c38d AL |
428 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
429 | { | |
1cc91b7d | 430 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
6e02c38d | 431 | VirtIOBlockReq *req; |
bc6694d4 KW |
432 | MultiReqBuffer mrb = { |
433 | .num_writes = 0, | |
bc6694d4 | 434 | }; |
6e02c38d | 435 | |
392808b4 SH |
436 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start |
437 | * dataplane here instead of waiting for .set_status(). | |
438 | */ | |
439 | if (s->dataplane) { | |
440 | virtio_blk_data_plane_start(s->dataplane); | |
441 | return; | |
442 | } | |
392808b4 | 443 | |
6e02c38d | 444 | while ((req = virtio_blk_get_request(s))) { |
bc6694d4 | 445 | virtio_blk_handle_request(req, &mrb); |
6e02c38d | 446 | } |
91553dcc | 447 | |
c20fd872 | 448 | virtio_submit_multiwrite(s->bs, &mrb); |
91553dcc | 449 | |
6e02c38d AL |
450 | /* |
451 | * FIXME: Want to check for completions before returning to guest mode, | |
452 | * so cached reads and writes are reported as quickly as possible. But | |
453 | * that should be done in the generic block layer. | |
454 | */ | |
455 | } | |
456 | ||
213189ab | 457 | static void virtio_blk_dma_restart_bh(void *opaque) |
869a5c6d AL |
458 | { |
459 | VirtIOBlock *s = opaque; | |
460 | VirtIOBlockReq *req = s->rq; | |
f1b52868 KW |
461 | MultiReqBuffer mrb = { |
462 | .num_writes = 0, | |
f1b52868 | 463 | }; |
869a5c6d | 464 | |
213189ab MA |
465 | qemu_bh_delete(s->bh); |
466 | s->bh = NULL; | |
869a5c6d AL |
467 | |
468 | s->rq = NULL; | |
469 | ||
470 | while (req) { | |
1bdb176a | 471 | VirtIOBlockReq *next = req->next; |
f1b52868 | 472 | virtio_blk_handle_request(req, &mrb); |
1bdb176a | 473 | req = next; |
869a5c6d | 474 | } |
f1b52868 | 475 | |
c20fd872 | 476 | virtio_submit_multiwrite(s->bs, &mrb); |
869a5c6d AL |
477 | } |
478 | ||
1dfb4dd9 LC |
479 | static void virtio_blk_dma_restart_cb(void *opaque, int running, |
480 | RunState state) | |
213189ab MA |
481 | { |
482 | VirtIOBlock *s = opaque; | |
483 | ||
392808b4 | 484 | if (!running) { |
213189ab | 485 | return; |
392808b4 | 486 | } |
213189ab MA |
487 | |
488 | if (!s->bh) { | |
2a30307f | 489 | s->bh = aio_bh_new(bdrv_get_aio_context(s->conf.conf.bs), |
4407c1c5 | 490 | virtio_blk_dma_restart_bh, s); |
213189ab MA |
491 | qemu_bh_schedule(s->bh); |
492 | } | |
493 | } | |
494 | ||
6e02c38d AL |
495 | static void virtio_blk_reset(VirtIODevice *vdev) |
496 | { | |
1cc91b7d | 497 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
392808b4 SH |
498 | |
499 | if (s->dataplane) { | |
500 | virtio_blk_data_plane_stop(s->dataplane); | |
501 | } | |
392808b4 | 502 | |
6e02c38d AL |
503 | /* |
504 | * This should cancel pending requests, but can't do nicely until there | |
505 | * are per-device request lists. | |
506 | */ | |
922453bc | 507 | bdrv_drain_all(); |
ef5bc962 | 508 | bdrv_set_enable_write_cache(s->bs, s->original_wce); |
6e02c38d AL |
509 | } |
510 | ||
bf011293 | 511 | /* coalesce internal state, copy to pci i/o region 0 |
512 | */ | |
6e02c38d AL |
513 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
514 | { | |
1cc91b7d | 515 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
2a30307f | 516 | BlockConf *conf = &s->conf.conf; |
6e02c38d AL |
517 | struct virtio_blk_config blkcfg; |
518 | uint64_t capacity; | |
f7516731 | 519 | int blk_size = conf->logical_block_size; |
6e02c38d AL |
520 | |
521 | bdrv_get_geometry(s->bs, &capacity); | |
5c5dafdc | 522 | memset(&blkcfg, 0, sizeof(blkcfg)); |
783d1897 RR |
523 | virtio_stq_p(vdev, &blkcfg.capacity, capacity); |
524 | virtio_stl_p(vdev, &blkcfg.seg_max, 128 - 2); | |
f7516731 | 525 | virtio_stw_p(vdev, &blkcfg.cylinders, conf->cyls); |
783d1897 | 526 | virtio_stl_p(vdev, &blkcfg.blk_size, blk_size); |
f7516731 MA |
527 | virtio_stw_p(vdev, &blkcfg.min_io_size, conf->min_io_size / blk_size); |
528 | virtio_stw_p(vdev, &blkcfg.opt_io_size, conf->opt_io_size / blk_size); | |
529 | blkcfg.heads = conf->heads; | |
136be99e CB |
530 | /* |
531 | * We must ensure that the block device capacity is a multiple of | |
e03ba136 | 532 | * the logical block size. If that is not the case, let's use |
136be99e CB |
533 | * sector_mask to adopt the geometry to have a correct picture. |
534 | * For those devices where the capacity is ok for the given geometry | |
e03ba136 | 535 | * we don't touch the sector value of the geometry, since some devices |
136be99e CB |
536 | * (like s390 dasd) need a specific value. Here the capacity is already |
537 | * cyls*heads*secs*blk_size and the sector value is not block size | |
538 | * divided by 512 - instead it is the amount of blk_size blocks | |
539 | * per track (cylinder). | |
540 | */ | |
f7516731 MA |
541 | if (bdrv_getlength(s->bs) / conf->heads / conf->secs % blk_size) { |
542 | blkcfg.sectors = conf->secs & ~s->sector_mask; | |
136be99e | 543 | } else { |
f7516731 | 544 | blkcfg.sectors = conf->secs; |
136be99e | 545 | } |
c7085da7 | 546 | blkcfg.size_max = 0; |
f7516731 | 547 | blkcfg.physical_block_exp = get_physical_block_exp(conf); |
9752c371 | 548 | blkcfg.alignment_offset = 0; |
13e3dce0 | 549 | blkcfg.wce = bdrv_enable_write_cache(s->bs); |
37d5ddd6 | 550 | memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); |
6e02c38d AL |
551 | } |
552 | ||
13e3dce0 PB |
553 | static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
554 | { | |
1cc91b7d | 555 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
13e3dce0 PB |
556 | struct virtio_blk_config blkcfg; |
557 | ||
558 | memcpy(&blkcfg, config, sizeof(blkcfg)); | |
6d7e73d6 FZ |
559 | |
560 | aio_context_acquire(bdrv_get_aio_context(s->bs)); | |
13e3dce0 | 561 | bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0); |
6d7e73d6 | 562 | aio_context_release(bdrv_get_aio_context(s->bs)); |
13e3dce0 PB |
563 | } |
564 | ||
8172539d | 565 | static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features) |
6e02c38d | 566 | { |
1cc91b7d | 567 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
1063b8b1 CH |
568 | |
569 | features |= (1 << VIRTIO_BLK_F_SEG_MAX); | |
570 | features |= (1 << VIRTIO_BLK_F_GEOMETRY); | |
9752c371 | 571 | features |= (1 << VIRTIO_BLK_F_TOPOLOGY); |
8cfacf07 | 572 | features |= (1 << VIRTIO_BLK_F_BLK_SIZE); |
a6c5c84a | 573 | features |= (1 << VIRTIO_BLK_F_SCSI); |
aa659be3 | 574 | |
2a30307f | 575 | if (s->conf.config_wce) { |
8a873ba7 SH |
576 | features |= (1 << VIRTIO_BLK_F_CONFIG_WCE); |
577 | } | |
aa659be3 | 578 | if (bdrv_enable_write_cache(s->bs)) |
13e3dce0 PB |
579 | features |= (1 << VIRTIO_BLK_F_WCE); |
580 | ||
c79662f7 NS |
581 | if (bdrv_is_read_only(s->bs)) |
582 | features |= 1 << VIRTIO_BLK_F_RO; | |
1063b8b1 CH |
583 | |
584 | return features; | |
6e02c38d AL |
585 | } |
586 | ||
9315cbfd PB |
587 | static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) |
588 | { | |
1cc91b7d | 589 | VirtIOBlock *s = VIRTIO_BLK(vdev); |
9315cbfd PB |
590 | uint32_t features; |
591 | ||
cf139388 SH |
592 | if (s->dataplane && !(status & (VIRTIO_CONFIG_S_DRIVER | |
593 | VIRTIO_CONFIG_S_DRIVER_OK))) { | |
392808b4 SH |
594 | virtio_blk_data_plane_stop(s->dataplane); |
595 | } | |
392808b4 | 596 | |
9315cbfd PB |
597 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
598 | return; | |
599 | } | |
600 | ||
601 | features = vdev->guest_features; | |
ef5bc962 PB |
602 | |
603 | /* A guest that supports VIRTIO_BLK_F_CONFIG_WCE must be able to send | |
604 | * cache flushes. Thus, the "auto writethrough" behavior is never | |
605 | * necessary for guests that support the VIRTIO_BLK_F_CONFIG_WCE feature. | |
606 | * Leaving it enabled would break the following sequence: | |
607 | * | |
608 | * Guest started with "-drive cache=writethrough" | |
609 | * Guest sets status to 0 | |
610 | * Guest sets DRIVER bit in status field | |
611 | * Guest reads host features (WCE=0, CONFIG_WCE=1) | |
612 | * Guest writes guest features (WCE=0, CONFIG_WCE=1) | |
613 | * Guest writes 1 to the WCE configuration field (writeback mode) | |
614 | * Guest sets DRIVER_OK bit in status field | |
615 | * | |
616 | * s->bs would erroneously be placed in writethrough mode. | |
617 | */ | |
618 | if (!(features & (1 << VIRTIO_BLK_F_CONFIG_WCE))) { | |
6d7e73d6 FZ |
619 | aio_context_acquire(bdrv_get_aio_context(s->bs)); |
620 | bdrv_set_enable_write_cache(s->bs, | |
621 | !!(features & (1 << VIRTIO_BLK_F_WCE))); | |
622 | aio_context_release(bdrv_get_aio_context(s->bs)); | |
ef5bc962 | 623 | } |
9315cbfd PB |
624 | } |
625 | ||
6e02c38d AL |
626 | static void virtio_blk_save(QEMUFile *f, void *opaque) |
627 | { | |
b2b295a7 | 628 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
869a5c6d | 629 | |
1cc91b7d | 630 | virtio_save(vdev, f); |
b2b295a7 | 631 | } |
869a5c6d | 632 | |
b2b295a7 GK |
633 | static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f) |
634 | { | |
635 | VirtIOBlock *s = VIRTIO_BLK(vdev); | |
636 | VirtIOBlockReq *req = s->rq; | |
637 | ||
869a5c6d AL |
638 | while (req) { |
639 | qemu_put_sbyte(f, 1); | |
f897bf75 | 640 | qemu_put_buffer(f, (unsigned char *)&req->elem, |
671ec3f0 | 641 | sizeof(VirtQueueElement)); |
869a5c6d AL |
642 | req = req->next; |
643 | } | |
644 | qemu_put_sbyte(f, 0); | |
6e02c38d AL |
645 | } |
646 | ||
647 | static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) | |
648 | { | |
649 | VirtIOBlock *s = opaque; | |
1cc91b7d | 650 | VirtIODevice *vdev = VIRTIO_DEVICE(s); |
6e02c38d | 651 | |
869a5c6d | 652 | if (version_id != 2) |
6e02c38d AL |
653 | return -EINVAL; |
654 | ||
b2b295a7 GK |
655 | return virtio_load(vdev, f, version_id); |
656 | } | |
657 | ||
658 | static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, | |
659 | int version_id) | |
660 | { | |
661 | VirtIOBlock *s = VIRTIO_BLK(vdev); | |
2a633c46 | 662 | |
869a5c6d AL |
663 | while (qemu_get_sbyte(f)) { |
664 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); | |
f897bf75 | 665 | qemu_get_buffer(f, (unsigned char *)&req->elem, |
671ec3f0 | 666 | sizeof(VirtQueueElement)); |
869a5c6d | 667 | req->next = s->rq; |
20a81e4d | 668 | s->rq = req; |
b6a4805b | 669 | |
f897bf75 SH |
670 | virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr, |
671 | req->elem.in_num, 1); | |
672 | virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr, | |
673 | req->elem.out_num, 0); | |
869a5c6d | 674 | } |
6e02c38d AL |
675 | |
676 | return 0; | |
677 | } | |
678 | ||
145feb17 | 679 | static void virtio_blk_resize(void *opaque) |
e5051fc7 | 680 | { |
1cc91b7d | 681 | VirtIODevice *vdev = VIRTIO_DEVICE(opaque); |
e5051fc7 | 682 | |
1cc91b7d | 683 | virtio_notify_config(vdev); |
e5051fc7 CH |
684 | } |
685 | ||
0e49de52 | 686 | static const BlockDevOps virtio_block_ops = { |
145feb17 | 687 | .resize_cb = virtio_blk_resize, |
0e49de52 MA |
688 | }; |
689 | ||
84db52d0 SH |
690 | /* Disable dataplane thread during live migration since it does not |
691 | * update the dirty memory bitmap yet. | |
692 | */ | |
693 | static void virtio_blk_migration_state_changed(Notifier *notifier, void *data) | |
694 | { | |
695 | VirtIOBlock *s = container_of(notifier, VirtIOBlock, | |
696 | migration_state_notifier); | |
697 | MigrationState *mig = data; | |
3ffeeef7 | 698 | Error *err = NULL; |
84db52d0 SH |
699 | |
700 | if (migration_in_setup(mig)) { | |
701 | if (!s->dataplane) { | |
702 | return; | |
703 | } | |
704 | virtio_blk_data_plane_destroy(s->dataplane); | |
705 | s->dataplane = NULL; | |
706 | } else if (migration_has_finished(mig) || | |
707 | migration_has_failed(mig)) { | |
708 | if (s->dataplane) { | |
709 | return; | |
710 | } | |
711 | bdrv_drain_all(); /* complete in-flight non-dataplane requests */ | |
2a30307f | 712 | virtio_blk_data_plane_create(VIRTIO_DEVICE(s), &s->conf, |
3ffeeef7 AF |
713 | &s->dataplane, &err); |
714 | if (err != NULL) { | |
715 | error_report("%s", error_get_pretty(err)); | |
716 | error_free(err); | |
717 | } | |
84db52d0 SH |
718 | } |
719 | } | |
84db52d0 | 720 | |
75884afd | 721 | static void virtio_blk_device_realize(DeviceState *dev, Error **errp) |
1c028ddf | 722 | { |
75884afd | 723 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
179b417e | 724 | VirtIOBlock *s = VIRTIO_BLK(dev); |
2a30307f | 725 | VirtIOBlkConf *conf = &s->conf; |
3ffeeef7 | 726 | Error *err = NULL; |
6e02c38d | 727 | static int virtio_blk_id; |
cf21e106 | 728 | |
2a30307f | 729 | if (!conf->conf.bs) { |
75884afd AF |
730 | error_setg(errp, "drive property not set"); |
731 | return; | |
d75d25e3 | 732 | } |
2a30307f | 733 | if (!bdrv_is_inserted(conf->conf.bs)) { |
75884afd AF |
734 | error_setg(errp, "Device needs media, but drive is empty"); |
735 | return; | |
98f28ad7 | 736 | } |
d75d25e3 | 737 | |
2a30307f MA |
738 | blkconf_serial(&conf->conf, &conf->serial); |
739 | s->original_wce = bdrv_enable_write_cache(conf->conf.bs); | |
740 | blkconf_geometry(&conf->conf, NULL, 65535, 255, 255, &err); | |
5ff5efb4 FZ |
741 | if (err) { |
742 | error_propagate(errp, err); | |
75884afd | 743 | return; |
b7eb0c9f | 744 | } |
a8686a9b | 745 | |
05ff6865 FK |
746 | virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, |
747 | sizeof(struct virtio_blk_config)); | |
6e02c38d | 748 | |
2a30307f | 749 | s->bs = conf->conf.bs; |
869a5c6d | 750 | s->rq = NULL; |
2a30307f | 751 | s->sector_mask = (s->conf.conf.logical_block_size / BDRV_SECTOR_SIZE) - 1; |
e63e7fde | 752 | |
05ff6865 | 753 | s->vq = virtio_add_queue(vdev, 128, virtio_blk_handle_output); |
bf4bd461 | 754 | s->complete_request = virtio_blk_complete_request; |
2a30307f | 755 | virtio_blk_data_plane_create(vdev, conf, &s->dataplane, &err); |
3ffeeef7 | 756 | if (err != NULL) { |
75884afd | 757 | error_propagate(errp, err); |
6a1a8cc7 | 758 | virtio_cleanup(vdev); |
75884afd | 759 | return; |
392808b4 | 760 | } |
84db52d0 SH |
761 | s->migration_state_notifier.notify = virtio_blk_migration_state_changed; |
762 | add_migration_state_change_notifier(&s->migration_state_notifier); | |
6e02c38d | 763 | |
69b302b2 | 764 | s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
179b417e | 765 | register_savevm(dev, "virtio-blk", virtio_blk_id++, 2, |
6e02c38d | 766 | virtio_blk_save, virtio_blk_load, s); |
0e49de52 | 767 | bdrv_set_dev_ops(s->bs, &virtio_block_ops, s); |
2a30307f | 768 | bdrv_set_guest_block_size(s->bs, s->conf.conf.logical_block_size); |
6e02c38d | 769 | |
af239a62 | 770 | bdrv_iostatus_enable(s->bs); |
1c028ddf FK |
771 | } |
772 | ||
306ec6c3 | 773 | static void virtio_blk_device_unrealize(DeviceState *dev, Error **errp) |
1c028ddf | 774 | { |
306ec6c3 AF |
775 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
776 | VirtIOBlock *s = VIRTIO_BLK(dev); | |
777 | ||
84db52d0 | 778 | remove_migration_state_change_notifier(&s->migration_state_notifier); |
1c028ddf FK |
779 | virtio_blk_data_plane_destroy(s->dataplane); |
780 | s->dataplane = NULL; | |
1c028ddf | 781 | qemu_del_vm_change_state_handler(s->change); |
306ec6c3 | 782 | unregister_savevm(dev, "virtio-blk", s); |
1c028ddf | 783 | blockdev_mark_auto_del(s->bs); |
6a1a8cc7 | 784 | virtio_cleanup(vdev); |
1c028ddf FK |
785 | } |
786 | ||
467b3f33 SH |
787 | static void virtio_blk_instance_init(Object *obj) |
788 | { | |
789 | VirtIOBlock *s = VIRTIO_BLK(obj); | |
790 | ||
791 | object_property_add_link(obj, "iothread", TYPE_IOTHREAD, | |
2a30307f | 792 | (Object **)&s->conf.iothread, |
467b3f33 SH |
793 | qdev_prop_allow_set_link_before_realize, |
794 | OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL); | |
2a30307f | 795 | device_add_bootindex_property(obj, &s->conf.conf.bootindex, |
3342ec32 GA |
796 | "bootindex", "/disk@0,0", |
797 | DEVICE(obj), NULL); | |
467b3f33 SH |
798 | } |
799 | ||
1c028ddf | 800 | static Property virtio_blk_properties[] = { |
2a30307f MA |
801 | DEFINE_BLOCK_PROPERTIES(VirtIOBlock, conf.conf), |
802 | DEFINE_BLOCK_CHS_PROPERTIES(VirtIOBlock, conf.conf), | |
803 | DEFINE_PROP_STRING("serial", VirtIOBlock, conf.serial), | |
804 | DEFINE_PROP_BIT("config-wce", VirtIOBlock, conf.config_wce, 0, true), | |
32a877e4 | 805 | #ifdef __linux__ |
2a30307f | 806 | DEFINE_PROP_BIT("scsi", VirtIOBlock, conf.scsi, 0, true), |
32a877e4 | 807 | #endif |
2a30307f | 808 | DEFINE_PROP_BIT("x-data-plane", VirtIOBlock, conf.data_plane, 0, false), |
1c028ddf FK |
809 | DEFINE_PROP_END_OF_LIST(), |
810 | }; | |
811 | ||
812 | static void virtio_blk_class_init(ObjectClass *klass, void *data) | |
813 | { | |
814 | DeviceClass *dc = DEVICE_CLASS(klass); | |
815 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
75884afd | 816 | |
1c028ddf | 817 | dc->props = virtio_blk_properties; |
125ee0ed | 818 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
75884afd | 819 | vdc->realize = virtio_blk_device_realize; |
306ec6c3 | 820 | vdc->unrealize = virtio_blk_device_unrealize; |
1c028ddf FK |
821 | vdc->get_config = virtio_blk_update_config; |
822 | vdc->set_config = virtio_blk_set_config; | |
823 | vdc->get_features = virtio_blk_get_features; | |
824 | vdc->set_status = virtio_blk_set_status; | |
825 | vdc->reset = virtio_blk_reset; | |
b2b295a7 GK |
826 | vdc->save = virtio_blk_save_device; |
827 | vdc->load = virtio_blk_load_device; | |
1c028ddf FK |
828 | } |
829 | ||
830 | static const TypeInfo virtio_device_info = { | |
831 | .name = TYPE_VIRTIO_BLK, | |
832 | .parent = TYPE_VIRTIO_DEVICE, | |
833 | .instance_size = sizeof(VirtIOBlock), | |
467b3f33 | 834 | .instance_init = virtio_blk_instance_init, |
1c028ddf FK |
835 | .class_init = virtio_blk_class_init, |
836 | }; | |
837 | ||
838 | static void virtio_register_types(void) | |
839 | { | |
840 | type_register_static(&virtio_device_info); | |
841 | } | |
842 | ||
843 | type_init(virtio_register_types) |