]>
Commit | Line | Data |
---|---|---|
6e02c38d AL |
1 | /* |
2 | * Virtio Block Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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" |
d75d25e3 | 15 | #include "qemu-error.h" |
6d519a5f | 16 | #include "trace.h" |
9db1c0f7 | 17 | #include "hw/block-common.h" |
2446333c | 18 | #include "blockdev.h" |
6e02c38d | 19 | #include "virtio-blk.h" |
5bb23927 | 20 | #include "scsi-defs.h" |
1063b8b1 CH |
21 | #ifdef __linux__ |
22 | # include <scsi/sg.h> | |
23 | #endif | |
6e02c38d AL |
24 | |
25 | typedef struct VirtIOBlock | |
26 | { | |
27 | VirtIODevice vdev; | |
28 | BlockDriverState *bs; | |
29 | VirtQueue *vq; | |
869a5c6d | 30 | void *rq; |
213189ab | 31 | QEMUBH *bh; |
9752c371 | 32 | BlockConf *conf; |
12c5674b | 33 | VirtIOBlkConf *blk; |
8cfacf07 | 34 | unsigned short sector_mask; |
9d0d3138 | 35 | DeviceState *qdev; |
6e02c38d AL |
36 | } VirtIOBlock; |
37 | ||
38 | static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev) | |
39 | { | |
40 | return (VirtIOBlock *)vdev; | |
41 | } | |
42 | ||
43 | typedef struct VirtIOBlockReq | |
44 | { | |
45 | VirtIOBlock *dev; | |
46 | VirtQueueElement elem; | |
47 | struct virtio_blk_inhdr *in; | |
48 | struct virtio_blk_outhdr *out; | |
1063b8b1 | 49 | struct virtio_scsi_inhdr *scsi; |
d28a1b6e | 50 | QEMUIOVector qiov; |
869a5c6d | 51 | struct VirtIOBlockReq *next; |
a597e79c | 52 | BlockAcctCookie acct; |
6e02c38d AL |
53 | } VirtIOBlockReq; |
54 | ||
869a5c6d AL |
55 | static void virtio_blk_req_complete(VirtIOBlockReq *req, int status) |
56 | { | |
57 | VirtIOBlock *s = req->dev; | |
58 | ||
6d519a5f SH |
59 | trace_virtio_blk_req_complete(req, status); |
60 | ||
92e3c2a3 | 61 | stb_p(&req->in->status, status); |
d28a1b6e | 62 | virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in)); |
869a5c6d | 63 | virtio_notify(&s->vdev, s->vq); |
869a5c6d AL |
64 | } |
65 | ||
f35d68f0 KW |
66 | static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, |
67 | int is_read) | |
869a5c6d | 68 | { |
abd7f68d | 69 | BlockErrorAction action = bdrv_get_on_error(req->dev->bs, is_read); |
869a5c6d AL |
70 | VirtIOBlock *s = req->dev; |
71 | ||
eaa6c85f | 72 | if (action == BLOCK_ERR_IGNORE) { |
329c0a48 | 73 | bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_IGNORE, is_read); |
869a5c6d | 74 | return 0; |
eaa6c85f | 75 | } |
869a5c6d AL |
76 | |
77 | if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC) | |
78 | || action == BLOCK_ERR_STOP_ANY) { | |
79 | req->next = s->rq; | |
80 | s->rq = req; | |
329c0a48 | 81 | bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_STOP, is_read); |
0461d5a6 | 82 | vm_stop(RUN_STATE_IO_ERROR); |
af239a62 | 83 | bdrv_iostatus_set_err(s->bs, error); |
869a5c6d AL |
84 | } else { |
85 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); | |
a597e79c CH |
86 | bdrv_acct_done(s->bs, &req->acct); |
87 | g_free(req); | |
329c0a48 | 88 | bdrv_emit_qmp_error_event(s->bs, BDRV_ACTION_REPORT, is_read); |
869a5c6d AL |
89 | } |
90 | ||
91 | return 1; | |
92 | } | |
93 | ||
6e02c38d AL |
94 | static void virtio_blk_rw_complete(void *opaque, int ret) |
95 | { | |
96 | VirtIOBlockReq *req = opaque; | |
6e02c38d | 97 | |
6d519a5f SH |
98 | trace_virtio_blk_rw_complete(req, ret); |
99 | ||
f35d68f0 | 100 | if (ret) { |
92e3c2a3 | 101 | int is_read = !(ldl_p(&req->out->type) & VIRTIO_BLK_T_OUT); |
f35d68f0 | 102 | if (virtio_blk_handle_rw_error(req, -ret, is_read)) |
869a5c6d | 103 | return; |
6e02c38d AL |
104 | } |
105 | ||
f35d68f0 | 106 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
a597e79c CH |
107 | bdrv_acct_done(req->dev->bs, &req->acct); |
108 | g_free(req); | |
869a5c6d | 109 | } |
6e02c38d | 110 | |
aa659be3 CH |
111 | static void virtio_blk_flush_complete(void *opaque, int ret) |
112 | { | |
113 | VirtIOBlockReq *req = opaque; | |
114 | ||
8c269b54 KW |
115 | if (ret) { |
116 | if (virtio_blk_handle_rw_error(req, -ret, 0)) { | |
117 | return; | |
118 | } | |
119 | } | |
120 | ||
121 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); | |
a597e79c CH |
122 | bdrv_acct_done(req->dev->bs, &req->acct); |
123 | g_free(req); | |
aa659be3 CH |
124 | } |
125 | ||
869a5c6d AL |
126 | static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s) |
127 | { | |
7267c094 | 128 | VirtIOBlockReq *req = g_malloc(sizeof(*req)); |
487414f1 | 129 | req->dev = s; |
de6c8042 SH |
130 | req->qiov.size = 0; |
131 | req->next = NULL; | |
869a5c6d | 132 | return req; |
6e02c38d AL |
133 | } |
134 | ||
135 | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s) | |
136 | { | |
869a5c6d | 137 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
6e02c38d | 138 | |
869a5c6d AL |
139 | if (req != NULL) { |
140 | if (!virtqueue_pop(s->vq, &req->elem)) { | |
7267c094 | 141 | g_free(req); |
869a5c6d AL |
142 | return NULL; |
143 | } | |
6e02c38d AL |
144 | } |
145 | ||
146 | return req; | |
147 | } | |
148 | ||
1063b8b1 CH |
149 | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
150 | { | |
47ce9ef7 | 151 | #ifdef __linux__ |
4277906d | 152 | int ret; |
1063b8b1 | 153 | int i; |
47ce9ef7 SW |
154 | #endif |
155 | int status = VIRTIO_BLK_S_OK; | |
1063b8b1 CH |
156 | |
157 | /* | |
158 | * We require at least one output segment each for the virtio_blk_outhdr | |
159 | * and the SCSI command block. | |
160 | * | |
161 | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr | |
162 | * and the sense buffer pointer in the input segments. | |
163 | */ | |
164 | if (req->elem.out_num < 2 || req->elem.in_num < 3) { | |
165 | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); | |
a597e79c | 166 | g_free(req); |
1063b8b1 CH |
167 | return; |
168 | } | |
169 | ||
170 | /* | |
f34e73cd PB |
171 | * The scsi inhdr is placed in the second-to-last input segment, just |
172 | * before the regular inhdr. | |
1063b8b1 | 173 | */ |
f34e73cd PB |
174 | req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; |
175 | ||
a6c5c84a | 176 | if (!req->dev->blk->scsi) { |
f34e73cd PB |
177 | status = VIRTIO_BLK_S_UNSUPP; |
178 | goto fail; | |
1063b8b1 CH |
179 | } |
180 | ||
181 | /* | |
f34e73cd | 182 | * No support for bidirection commands yet. |
1063b8b1 | 183 | */ |
f34e73cd PB |
184 | if (req->elem.out_num > 2 && req->elem.in_num > 3) { |
185 | status = VIRTIO_BLK_S_UNSUPP; | |
186 | goto fail; | |
187 | } | |
1063b8b1 | 188 | |
f34e73cd PB |
189 | #ifdef __linux__ |
190 | struct sg_io_hdr hdr; | |
1063b8b1 CH |
191 | memset(&hdr, 0, sizeof(struct sg_io_hdr)); |
192 | hdr.interface_id = 'S'; | |
193 | hdr.cmd_len = req->elem.out_sg[1].iov_len; | |
194 | hdr.cmdp = req->elem.out_sg[1].iov_base; | |
195 | hdr.dxfer_len = 0; | |
196 | ||
197 | if (req->elem.out_num > 2) { | |
198 | /* | |
199 | * If there are more than the minimally required 2 output segments | |
200 | * there is write payload starting from the third iovec. | |
201 | */ | |
202 | hdr.dxfer_direction = SG_DXFER_TO_DEV; | |
203 | hdr.iovec_count = req->elem.out_num - 2; | |
204 | ||
205 | for (i = 0; i < hdr.iovec_count; i++) | |
206 | hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len; | |
207 | ||
208 | hdr.dxferp = req->elem.out_sg + 2; | |
209 | ||
210 | } else if (req->elem.in_num > 3) { | |
211 | /* | |
212 | * If we have more than 3 input segments the guest wants to actually | |
213 | * read data. | |
214 | */ | |
215 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
216 | hdr.iovec_count = req->elem.in_num - 3; | |
217 | for (i = 0; i < hdr.iovec_count; i++) | |
218 | hdr.dxfer_len += req->elem.in_sg[i].iov_len; | |
219 | ||
220 | hdr.dxferp = req->elem.in_sg; | |
1063b8b1 CH |
221 | } else { |
222 | /* | |
223 | * Some SCSI commands don't actually transfer any data. | |
224 | */ | |
225 | hdr.dxfer_direction = SG_DXFER_NONE; | |
226 | } | |
227 | ||
228 | hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base; | |
229 | hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len; | |
1063b8b1 CH |
230 | |
231 | ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr); | |
232 | if (ret) { | |
233 | status = VIRTIO_BLK_S_UNSUPP; | |
f34e73cd | 234 | goto fail; |
1063b8b1 CH |
235 | } |
236 | ||
5bb23927 PB |
237 | /* |
238 | * From SCSI-Generic-HOWTO: "Some lower level drivers (e.g. ide-scsi) | |
239 | * clear the masked_status field [hence status gets cleared too, see | |
240 | * block/scsi_ioctl.c] even when a CHECK_CONDITION or COMMAND_TERMINATED | |
241 | * status has occurred. However they do set DRIVER_SENSE in driver_status | |
242 | * field. Also a (sb_len_wr > 0) indicates there is a sense buffer. | |
243 | */ | |
244 | if (hdr.status == 0 && hdr.sb_len_wr > 0) { | |
245 | hdr.status = CHECK_CONDITION; | |
246 | } | |
247 | ||
248 | stl_p(&req->scsi->errors, | |
249 | hdr.status | (hdr.msg_status << 8) | | |
250 | (hdr.host_status << 16) | (hdr.driver_status << 24)); | |
92e3c2a3 AJ |
251 | stl_p(&req->scsi->residual, hdr.resid); |
252 | stl_p(&req->scsi->sense_len, hdr.sb_len_wr); | |
253 | stl_p(&req->scsi->data_len, hdr.dxfer_len); | |
1063b8b1 CH |
254 | |
255 | virtio_blk_req_complete(req, status); | |
a597e79c | 256 | g_free(req); |
730a9c53 | 257 | return; |
1063b8b1 | 258 | #else |
f34e73cd PB |
259 | abort(); |
260 | #endif | |
261 | ||
262 | fail: | |
263 | /* Just put anything nonzero so that the ioctl fails in the guest. */ | |
264 | stl_p(&req->scsi->errors, 255); | |
265 | virtio_blk_req_complete(req, status); | |
a597e79c | 266 | g_free(req); |
1063b8b1 | 267 | } |
1063b8b1 | 268 | |
c20fd872 CH |
269 | typedef struct MultiReqBuffer { |
270 | BlockRequest blkreq[32]; | |
271 | unsigned int num_writes; | |
272 | } MultiReqBuffer; | |
273 | ||
274 | static void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb) | |
869a5c6d | 275 | { |
91553dcc | 276 | int i, ret; |
91553dcc | 277 | |
c20fd872 CH |
278 | if (!mrb->num_writes) { |
279 | return; | |
280 | } | |
281 | ||
282 | ret = bdrv_aio_multiwrite(bs, mrb->blkreq, mrb->num_writes); | |
91553dcc | 283 | if (ret != 0) { |
c20fd872 CH |
284 | for (i = 0; i < mrb->num_writes; i++) { |
285 | if (mrb->blkreq[i].error) { | |
286 | virtio_blk_rw_complete(mrb->blkreq[i].opaque, -EIO); | |
91553dcc KW |
287 | } |
288 | } | |
289 | } | |
c20fd872 CH |
290 | |
291 | mrb->num_writes = 0; | |
91553dcc | 292 | } |
87b245db | 293 | |
c20fd872 | 294 | static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
aa659be3 | 295 | { |
a597e79c CH |
296 | bdrv_acct_start(req->dev->bs, &req->acct, 0, BDRV_ACCT_FLUSH); |
297 | ||
618fbb84 CH |
298 | /* |
299 | * Make sure all outstanding writes are posted to the backing device. | |
300 | */ | |
c20fd872 | 301 | virtio_submit_multiwrite(req->dev->bs, mrb); |
ad54ae80 | 302 | bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req); |
aa659be3 CH |
303 | } |
304 | ||
c20fd872 | 305 | static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb) |
91553dcc | 306 | { |
c20fd872 | 307 | BlockRequest *blkreq; |
92e3c2a3 | 308 | uint64_t sector; |
c20fd872 | 309 | |
92e3c2a3 | 310 | sector = ldq_p(&req->out->sector); |
6d519a5f | 311 | |
a597e79c CH |
312 | bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE); |
313 | ||
92e3c2a3 AJ |
314 | trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512); |
315 | ||
316 | if (sector & req->dev->sector_mask) { | |
8cfacf07 CH |
317 | virtio_blk_rw_complete(req, -EIO); |
318 | return; | |
319 | } | |
52c05023 CH |
320 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
321 | virtio_blk_rw_complete(req, -EIO); | |
322 | return; | |
323 | } | |
8cfacf07 | 324 | |
c20fd872 CH |
325 | if (mrb->num_writes == 32) { |
326 | virtio_submit_multiwrite(req->dev->bs, mrb); | |
87b245db | 327 | } |
91553dcc | 328 | |
c20fd872 | 329 | blkreq = &mrb->blkreq[mrb->num_writes]; |
92e3c2a3 | 330 | blkreq->sector = sector; |
c20fd872 CH |
331 | blkreq->nb_sectors = req->qiov.size / BDRV_SECTOR_SIZE; |
332 | blkreq->qiov = &req->qiov; | |
333 | blkreq->cb = virtio_blk_rw_complete; | |
334 | blkreq->opaque = req; | |
335 | blkreq->error = 0; | |
91553dcc | 336 | |
c20fd872 | 337 | mrb->num_writes++; |
d28a1b6e | 338 | } |
869a5c6d | 339 | |
d28a1b6e AL |
340 | static void virtio_blk_handle_read(VirtIOBlockReq *req) |
341 | { | |
92e3c2a3 AJ |
342 | uint64_t sector; |
343 | ||
344 | sector = ldq_p(&req->out->sector); | |
87b245db | 345 | |
a597e79c CH |
346 | bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ); |
347 | ||
81b6b9fa SH |
348 | trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512); |
349 | ||
92e3c2a3 | 350 | if (sector & req->dev->sector_mask) { |
8cfacf07 CH |
351 | virtio_blk_rw_complete(req, -EIO); |
352 | return; | |
353 | } | |
52c05023 CH |
354 | if (req->qiov.size % req->dev->conf->logical_block_size) { |
355 | virtio_blk_rw_complete(req, -EIO); | |
356 | return; | |
357 | } | |
ad54ae80 PB |
358 | bdrv_aio_readv(req->dev->bs, sector, &req->qiov, |
359 | req->qiov.size / BDRV_SECTOR_SIZE, | |
360 | virtio_blk_rw_complete, req); | |
869a5c6d AL |
361 | } |
362 | ||
bc6694d4 KW |
363 | static void virtio_blk_handle_request(VirtIOBlockReq *req, |
364 | MultiReqBuffer *mrb) | |
365 | { | |
92e3c2a3 AJ |
366 | uint32_t type; |
367 | ||
bc6694d4 | 368 | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
870cef1d | 369 | error_report("virtio-blk missing headers"); |
bc6694d4 KW |
370 | exit(1); |
371 | } | |
372 | ||
373 | if (req->elem.out_sg[0].iov_len < sizeof(*req->out) || | |
374 | req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) { | |
870cef1d | 375 | error_report("virtio-blk header not in correct element"); |
bc6694d4 KW |
376 | exit(1); |
377 | } | |
378 | ||
379 | req->out = (void *)req->elem.out_sg[0].iov_base; | |
380 | req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base; | |
381 | ||
92e3c2a3 AJ |
382 | type = ldl_p(&req->out->type); |
383 | ||
384 | if (type & VIRTIO_BLK_T_FLUSH) { | |
c20fd872 | 385 | virtio_blk_handle_flush(req, mrb); |
92e3c2a3 | 386 | } else if (type & VIRTIO_BLK_T_SCSI_CMD) { |
bc6694d4 | 387 | virtio_blk_handle_scsi(req); |
92e3c2a3 | 388 | } else if (type & VIRTIO_BLK_T_GET_ID) { |
2930b313 | 389 | VirtIOBlock *s = req->dev; |
390 | ||
a8686a9b MA |
391 | /* |
392 | * NB: per existing s/n string convention the string is | |
393 | * terminated by '\0' only when shorter than buffer. | |
394 | */ | |
395 | strncpy(req->elem.in_sg[0].iov_base, | |
12c5674b | 396 | s->blk->serial ? s->blk->serial : "", |
a8686a9b | 397 | MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES)); |
2930b313 | 398 | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
a597e79c | 399 | g_free(req); |
92e3c2a3 | 400 | } else if (type & VIRTIO_BLK_T_OUT) { |
bc6694d4 KW |
401 | qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1], |
402 | req->elem.out_num - 1); | |
c20fd872 | 403 | virtio_blk_handle_write(req, mrb); |
bc6694d4 KW |
404 | } else { |
405 | qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0], | |
406 | req->elem.in_num - 1); | |
407 | virtio_blk_handle_read(req); | |
408 | } | |
409 | } | |
410 | ||
6e02c38d AL |
411 | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
412 | { | |
413 | VirtIOBlock *s = to_virtio_blk(vdev); | |
414 | VirtIOBlockReq *req; | |
bc6694d4 KW |
415 | MultiReqBuffer mrb = { |
416 | .num_writes = 0, | |
bc6694d4 | 417 | }; |
6e02c38d AL |
418 | |
419 | while ((req = virtio_blk_get_request(s))) { | |
bc6694d4 | 420 | virtio_blk_handle_request(req, &mrb); |
6e02c38d | 421 | } |
91553dcc | 422 | |
c20fd872 | 423 | virtio_submit_multiwrite(s->bs, &mrb); |
91553dcc | 424 | |
6e02c38d AL |
425 | /* |
426 | * FIXME: Want to check for completions before returning to guest mode, | |
427 | * so cached reads and writes are reported as quickly as possible. But | |
428 | * that should be done in the generic block layer. | |
429 | */ | |
430 | } | |
431 | ||
213189ab | 432 | static void virtio_blk_dma_restart_bh(void *opaque) |
869a5c6d AL |
433 | { |
434 | VirtIOBlock *s = opaque; | |
435 | VirtIOBlockReq *req = s->rq; | |
f1b52868 KW |
436 | MultiReqBuffer mrb = { |
437 | .num_writes = 0, | |
f1b52868 | 438 | }; |
869a5c6d | 439 | |
213189ab MA |
440 | qemu_bh_delete(s->bh); |
441 | s->bh = NULL; | |
869a5c6d AL |
442 | |
443 | s->rq = NULL; | |
444 | ||
445 | while (req) { | |
f1b52868 | 446 | virtio_blk_handle_request(req, &mrb); |
869a5c6d AL |
447 | req = req->next; |
448 | } | |
f1b52868 | 449 | |
c20fd872 | 450 | virtio_submit_multiwrite(s->bs, &mrb); |
869a5c6d AL |
451 | } |
452 | ||
1dfb4dd9 LC |
453 | static void virtio_blk_dma_restart_cb(void *opaque, int running, |
454 | RunState state) | |
213189ab MA |
455 | { |
456 | VirtIOBlock *s = opaque; | |
457 | ||
458 | if (!running) | |
459 | return; | |
460 | ||
461 | if (!s->bh) { | |
462 | s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s); | |
463 | qemu_bh_schedule(s->bh); | |
464 | } | |
465 | } | |
466 | ||
6e02c38d AL |
467 | static void virtio_blk_reset(VirtIODevice *vdev) |
468 | { | |
469 | /* | |
470 | * This should cancel pending requests, but can't do nicely until there | |
471 | * are per-device request lists. | |
472 | */ | |
922453bc | 473 | bdrv_drain_all(); |
6e02c38d AL |
474 | } |
475 | ||
bf011293 | 476 | /* coalesce internal state, copy to pci i/o region 0 |
477 | */ | |
6e02c38d AL |
478 | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
479 | { | |
480 | VirtIOBlock *s = to_virtio_blk(vdev); | |
481 | struct virtio_blk_config blkcfg; | |
482 | uint64_t capacity; | |
3a395142 | 483 | int blk_size = s->conf->logical_block_size; |
6e02c38d AL |
484 | |
485 | bdrv_get_geometry(s->bs, &capacity); | |
5c5dafdc | 486 | memset(&blkcfg, 0, sizeof(blkcfg)); |
6e02c38d AL |
487 | stq_raw(&blkcfg.capacity, capacity); |
488 | stl_raw(&blkcfg.seg_max, 128 - 2); | |
e63e7fde | 489 | stw_raw(&blkcfg.cylinders, s->conf->cyls); |
3a395142 PB |
490 | stl_raw(&blkcfg.blk_size, blk_size); |
491 | stw_raw(&blkcfg.min_io_size, s->conf->min_io_size / blk_size); | |
492 | stw_raw(&blkcfg.opt_io_size, s->conf->opt_io_size / blk_size); | |
e63e7fde | 493 | blkcfg.heads = s->conf->heads; |
136be99e CB |
494 | /* |
495 | * We must ensure that the block device capacity is a multiple of | |
496 | * the logical block size. If that is not the case, lets use | |
497 | * sector_mask to adopt the geometry to have a correct picture. | |
498 | * For those devices where the capacity is ok for the given geometry | |
499 | * we dont touch the sector value of the geometry, since some devices | |
500 | * (like s390 dasd) need a specific value. Here the capacity is already | |
501 | * cyls*heads*secs*blk_size and the sector value is not block size | |
502 | * divided by 512 - instead it is the amount of blk_size blocks | |
503 | * per track (cylinder). | |
504 | */ | |
e63e7fde MA |
505 | if (bdrv_getlength(s->bs) / s->conf->heads / s->conf->secs % blk_size) { |
506 | blkcfg.sectors = s->conf->secs & ~s->sector_mask; | |
136be99e | 507 | } else { |
e63e7fde | 508 | blkcfg.sectors = s->conf->secs; |
136be99e | 509 | } |
c7085da7 | 510 | blkcfg.size_max = 0; |
9752c371 CH |
511 | blkcfg.physical_block_exp = get_physical_block_exp(s->conf); |
512 | blkcfg.alignment_offset = 0; | |
13e3dce0 | 513 | blkcfg.wce = bdrv_enable_write_cache(s->bs); |
37d5ddd6 | 514 | memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); |
6e02c38d AL |
515 | } |
516 | ||
13e3dce0 PB |
517 | static void virtio_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
518 | { | |
519 | VirtIOBlock *s = to_virtio_blk(vdev); | |
520 | struct virtio_blk_config blkcfg; | |
521 | ||
522 | memcpy(&blkcfg, config, sizeof(blkcfg)); | |
523 | bdrv_set_enable_write_cache(s->bs, blkcfg.wce != 0); | |
524 | } | |
525 | ||
8172539d | 526 | static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features) |
6e02c38d | 527 | { |
bf011293 | 528 | VirtIOBlock *s = to_virtio_blk(vdev); |
1063b8b1 CH |
529 | |
530 | features |= (1 << VIRTIO_BLK_F_SEG_MAX); | |
531 | features |= (1 << VIRTIO_BLK_F_GEOMETRY); | |
9752c371 | 532 | features |= (1 << VIRTIO_BLK_F_TOPOLOGY); |
8cfacf07 | 533 | features |= (1 << VIRTIO_BLK_F_BLK_SIZE); |
a6c5c84a | 534 | features |= (1 << VIRTIO_BLK_F_SCSI); |
aa659be3 | 535 | |
13e3dce0 | 536 | features |= (1 << VIRTIO_BLK_F_CONFIG_WCE); |
aa659be3 | 537 | if (bdrv_enable_write_cache(s->bs)) |
13e3dce0 PB |
538 | features |= (1 << VIRTIO_BLK_F_WCE); |
539 | ||
c79662f7 NS |
540 | if (bdrv_is_read_only(s->bs)) |
541 | features |= 1 << VIRTIO_BLK_F_RO; | |
1063b8b1 CH |
542 | |
543 | return features; | |
6e02c38d AL |
544 | } |
545 | ||
9315cbfd PB |
546 | static void virtio_blk_set_status(VirtIODevice *vdev, uint8_t status) |
547 | { | |
548 | VirtIOBlock *s = to_virtio_blk(vdev); | |
549 | uint32_t features; | |
550 | ||
551 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { | |
552 | return; | |
553 | } | |
554 | ||
555 | features = vdev->guest_features; | |
556 | bdrv_set_enable_write_cache(s->bs, !!(features & (1 << VIRTIO_BLK_F_WCE))); | |
557 | } | |
558 | ||
6e02c38d AL |
559 | static void virtio_blk_save(QEMUFile *f, void *opaque) |
560 | { | |
561 | VirtIOBlock *s = opaque; | |
869a5c6d AL |
562 | VirtIOBlockReq *req = s->rq; |
563 | ||
6e02c38d | 564 | virtio_save(&s->vdev, f); |
869a5c6d AL |
565 | |
566 | while (req) { | |
567 | qemu_put_sbyte(f, 1); | |
568 | qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); | |
569 | req = req->next; | |
570 | } | |
571 | qemu_put_sbyte(f, 0); | |
6e02c38d AL |
572 | } |
573 | ||
574 | static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) | |
575 | { | |
576 | VirtIOBlock *s = opaque; | |
2a633c46 | 577 | int ret; |
6e02c38d | 578 | |
869a5c6d | 579 | if (version_id != 2) |
6e02c38d AL |
580 | return -EINVAL; |
581 | ||
2a633c46 OW |
582 | ret = virtio_load(&s->vdev, f); |
583 | if (ret) { | |
584 | return ret; | |
585 | } | |
586 | ||
869a5c6d AL |
587 | while (qemu_get_sbyte(f)) { |
588 | VirtIOBlockReq *req = virtio_blk_alloc_request(s); | |
589 | qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); | |
590 | req->next = s->rq; | |
20a81e4d | 591 | s->rq = req; |
b6a4805b KW |
592 | |
593 | virtqueue_map_sg(req->elem.in_sg, req->elem.in_addr, | |
594 | req->elem.in_num, 1); | |
595 | virtqueue_map_sg(req->elem.out_sg, req->elem.out_addr, | |
596 | req->elem.out_num, 0); | |
869a5c6d | 597 | } |
6e02c38d AL |
598 | |
599 | return 0; | |
600 | } | |
601 | ||
145feb17 | 602 | static void virtio_blk_resize(void *opaque) |
e5051fc7 CH |
603 | { |
604 | VirtIOBlock *s = opaque; | |
605 | ||
145feb17 | 606 | virtio_notify_config(&s->vdev); |
e5051fc7 CH |
607 | } |
608 | ||
0e49de52 | 609 | static const BlockDevOps virtio_block_ops = { |
145feb17 | 610 | .resize_cb = virtio_blk_resize, |
0e49de52 MA |
611 | }; |
612 | ||
12c5674b | 613 | VirtIODevice *virtio_blk_init(DeviceState *dev, VirtIOBlkConf *blk) |
6e02c38d AL |
614 | { |
615 | VirtIOBlock *s; | |
6e02c38d | 616 | static int virtio_blk_id; |
cf21e106 | 617 | |
12c5674b | 618 | if (!blk->conf.bs) { |
6a84cb1f | 619 | error_report("drive property not set"); |
d75d25e3 MA |
620 | return NULL; |
621 | } | |
12c5674b | 622 | if (!bdrv_is_inserted(blk->conf.bs)) { |
98f28ad7 MA |
623 | error_report("Device needs media, but drive is empty"); |
624 | return NULL; | |
625 | } | |
d75d25e3 | 626 | |
911525db | 627 | blkconf_serial(&blk->conf, &blk->serial); |
b7eb0c9f MA |
628 | if (blkconf_geometry(&blk->conf, NULL, 65535, 255, 255) < 0) { |
629 | return NULL; | |
630 | } | |
a8686a9b | 631 | |
53c25cea | 632 | s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK, |
37d5ddd6 | 633 | sizeof(struct virtio_blk_config), |
53c25cea | 634 | sizeof(VirtIOBlock)); |
6e02c38d AL |
635 | |
636 | s->vdev.get_config = virtio_blk_update_config; | |
13e3dce0 | 637 | s->vdev.set_config = virtio_blk_set_config; |
6e02c38d | 638 | s->vdev.get_features = virtio_blk_get_features; |
9315cbfd | 639 | s->vdev.set_status = virtio_blk_set_status; |
6e02c38d | 640 | s->vdev.reset = virtio_blk_reset; |
12c5674b PB |
641 | s->bs = blk->conf.bs; |
642 | s->conf = &blk->conf; | |
643 | s->blk = blk; | |
869a5c6d | 644 | s->rq = NULL; |
1573a35d | 645 | s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1; |
e63e7fde | 646 | |
6e02c38d AL |
647 | s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output); |
648 | ||
869a5c6d | 649 | qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
9d0d3138 | 650 | s->qdev = dev; |
0be71e32 | 651 | register_savevm(dev, "virtio-blk", virtio_blk_id++, 2, |
6e02c38d | 652 | virtio_blk_save, virtio_blk_load, s); |
0e49de52 | 653 | bdrv_set_dev_ops(s->bs, &virtio_block_ops, s); |
12c5674b | 654 | bdrv_set_buffer_alignment(s->bs, s->conf->logical_block_size); |
6e02c38d | 655 | |
af239a62 | 656 | bdrv_iostatus_enable(s->bs); |
12c5674b | 657 | add_boot_device_path(s->conf->bootindex, dev, "/disk@0,0"); |
1ca4d09a | 658 | |
53c25cea | 659 | return &s->vdev; |
6e02c38d | 660 | } |
9d0d3138 AW |
661 | |
662 | void virtio_blk_exit(VirtIODevice *vdev) | |
663 | { | |
664 | VirtIOBlock *s = to_virtio_blk(vdev); | |
665 | unregister_savevm(s->qdev, "virtio-blk", s); | |
0e47931b | 666 | blockdev_mark_auto_del(s->bs); |
d92551f2 | 667 | virtio_cleanup(vdev); |
9d0d3138 | 668 | } |