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