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