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