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