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