]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-blk.c
cirrus: Properly re-register cirrus_linear_io_addr on vram unmap
[mirror_qemu.git] / hw / virtio-blk.c
CommitLineData
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
869a5c6d
AL
14#include <qemu-common.h>
15#include <sysemu.h>
6e02c38d
AL
16#include "virtio-blk.h"
17#include "block_int.h"
1063b8b1
CH
18#ifdef __linux__
19# include <scsi/sg.h>
20#endif
6e02c38d
AL
21
22typedef struct VirtIOBlock
23{
24 VirtIODevice vdev;
25 BlockDriverState *bs;
26 VirtQueue *vq;
869a5c6d 27 void *rq;
bf011293 28 char serial_str[BLOCK_SERIAL_STRLEN + 1];
213189ab 29 QEMUBH *bh;
711bf3d9 30 size_t config_size;
6e02c38d
AL
31} VirtIOBlock;
32
33static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
34{
35 return (VirtIOBlock *)vdev;
36}
37
bf011293 38/* store identify data in little endian format
39 */
40static inline void put_le16(uint16_t *p, unsigned int v)
41{
42 *p = cpu_to_le16(v);
43}
44
45/* copy to *dst from *src, nul pad dst tail as needed to len bytes
46 */
47static inline void padstr(char *dst, const char *src, int len)
48{
49 while (len--)
50 *dst++ = *src ? *src++ : '\0';
51}
52
53/* setup simulated identify data as appropriate for virtio block device
54 *
55 * ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
56 */
57static inline void virtio_identify_template(struct virtio_blk_config *bc)
58{
59 uint16_t *p = &bc->identify[0];
60 uint64_t lba_sectors = bc->capacity;
61
62 memset(p, 0, sizeof(bc->identify));
63 put_le16(p + 0, 0x0); /* ATA device */
64 padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware revision */
65 padstr((char *)(p + 27), "QEMU VIRT_BLK", 40); /* model# */
66 put_le16(p + 47, 0x80ff); /* max xfer 255 sectors */
67 put_le16(p + 49, 0x0b00); /* support IORDY/LBA/DMA */
68 put_le16(p + 59, 0x1ff); /* cur xfer 255 sectors */
69 put_le16(p + 80, 0x1f0); /* support ATA8/7/6/5/4 */
70 put_le16(p + 81, 0x16);
71 put_le16(p + 82, 0x400);
72 put_le16(p + 83, 0x400);
73 put_le16(p + 100, lba_sectors);
74 put_le16(p + 101, lba_sectors >> 16);
75 put_le16(p + 102, lba_sectors >> 32);
76 put_le16(p + 103, lba_sectors >> 48);
77}
78
6e02c38d
AL
79typedef struct VirtIOBlockReq
80{
81 VirtIOBlock *dev;
82 VirtQueueElement elem;
83 struct virtio_blk_inhdr *in;
84 struct virtio_blk_outhdr *out;
1063b8b1 85 struct virtio_scsi_inhdr *scsi;
d28a1b6e 86 QEMUIOVector qiov;
869a5c6d 87 struct VirtIOBlockReq *next;
6e02c38d
AL
88} VirtIOBlockReq;
89
869a5c6d
AL
90static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
91{
92 VirtIOBlock *s = req->dev;
93
94 req->in->status = status;
d28a1b6e 95 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
869a5c6d
AL
96 virtio_notify(&s->vdev, s->vq);
97
869a5c6d
AL
98 qemu_free(req);
99}
100
f35d68f0
KW
101static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
102 int is_read)
869a5c6d 103{
f35d68f0
KW
104 BlockInterfaceErrorAction action =
105 drive_get_on_error(req->dev->bs, is_read);
869a5c6d
AL
106 VirtIOBlock *s = req->dev;
107
108 if (action == BLOCK_ERR_IGNORE)
109 return 0;
110
111 if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
112 || action == BLOCK_ERR_STOP_ANY) {
113 req->next = s->rq;
114 s->rq = req;
115 vm_stop(0);
116 } else {
117 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
118 }
119
120 return 1;
121}
122
6e02c38d
AL
123static void virtio_blk_rw_complete(void *opaque, int ret)
124{
125 VirtIOBlockReq *req = opaque;
6e02c38d 126
f35d68f0
KW
127 if (ret) {
128 int is_read = !(req->out->type & VIRTIO_BLK_T_OUT);
129 if (virtio_blk_handle_rw_error(req, -ret, is_read))
869a5c6d 130 return;
6e02c38d
AL
131 }
132
f35d68f0 133 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
869a5c6d 134}
6e02c38d 135
aa659be3
CH
136static void virtio_blk_flush_complete(void *opaque, int ret)
137{
138 VirtIOBlockReq *req = opaque;
139
140 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
141}
142
869a5c6d
AL
143static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
144{
145 VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
487414f1 146 req->dev = s;
869a5c6d 147 return req;
6e02c38d
AL
148}
149
150static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
151{
869a5c6d 152 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
6e02c38d 153
869a5c6d
AL
154 if (req != NULL) {
155 if (!virtqueue_pop(s->vq, &req->elem)) {
156 qemu_free(req);
157 return NULL;
158 }
6e02c38d
AL
159 }
160
161 return req;
162}
163
1063b8b1
CH
164#ifdef __linux__
165static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
166{
167 struct sg_io_hdr hdr;
4277906d 168 int ret;
1063b8b1
CH
169 int status;
170 int i;
171
172 /*
173 * We require at least one output segment each for the virtio_blk_outhdr
174 * and the SCSI command block.
175 *
176 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
177 * and the sense buffer pointer in the input segments.
178 */
179 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
180 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
181 return;
182 }
183
184 /*
185 * No support for bidirection commands yet.
186 */
187 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
188 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
189 return;
190 }
191
192 /*
193 * The scsi inhdr is placed in the second-to-last input segment, just
194 * before the regular inhdr.
195 */
196 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
1063b8b1
CH
197
198 memset(&hdr, 0, sizeof(struct sg_io_hdr));
199 hdr.interface_id = 'S';
200 hdr.cmd_len = req->elem.out_sg[1].iov_len;
201 hdr.cmdp = req->elem.out_sg[1].iov_base;
202 hdr.dxfer_len = 0;
203
204 if (req->elem.out_num > 2) {
205 /*
206 * If there are more than the minimally required 2 output segments
207 * there is write payload starting from the third iovec.
208 */
209 hdr.dxfer_direction = SG_DXFER_TO_DEV;
210 hdr.iovec_count = req->elem.out_num - 2;
211
212 for (i = 0; i < hdr.iovec_count; i++)
213 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
214
215 hdr.dxferp = req->elem.out_sg + 2;
216
217 } else if (req->elem.in_num > 3) {
218 /*
219 * If we have more than 3 input segments the guest wants to actually
220 * read data.
221 */
222 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
223 hdr.iovec_count = req->elem.in_num - 3;
224 for (i = 0; i < hdr.iovec_count; i++)
225 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
226
227 hdr.dxferp = req->elem.in_sg;
1063b8b1
CH
228 } else {
229 /*
230 * Some SCSI commands don't actually transfer any data.
231 */
232 hdr.dxfer_direction = SG_DXFER_NONE;
233 }
234
235 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
236 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
1063b8b1
CH
237
238 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
239 if (ret) {
240 status = VIRTIO_BLK_S_UNSUPP;
241 hdr.status = ret;
242 hdr.resid = hdr.dxfer_len;
243 } else if (hdr.status) {
244 status = VIRTIO_BLK_S_IOERR;
245 } else {
246 status = VIRTIO_BLK_S_OK;
247 }
248
249 req->scsi->errors = hdr.status;
250 req->scsi->residual = hdr.resid;
251 req->scsi->sense_len = hdr.sb_len_wr;
252 req->scsi->data_len = hdr.dxfer_len;
253
254 virtio_blk_req_complete(req, status);
255}
256#else
257static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
258{
259 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
260}
261#endif /* __linux__ */
262
91553dcc
KW
263static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
264 int num_writes)
869a5c6d 265{
91553dcc
KW
266 int i, ret;
267 ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
268
269 if (ret != 0) {
270 for (i = 0; i < num_writes; i++) {
271 if (blkreq[i].error) {
272 virtio_blk_req_complete(blkreq[i].opaque, VIRTIO_BLK_S_IOERR);
273 }
274 }
275 }
276}
87b245db 277
aa659be3
CH
278static void virtio_blk_handle_flush(VirtIOBlockReq *req)
279{
280 BlockDriverAIOCB *acb;
281
282 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
283 if (!acb) {
284 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
285 }
286}
287
91553dcc
KW
288static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
289 VirtIOBlockReq *req, BlockDriverState **old_bs)
290{
291 if (req->dev->bs != *old_bs || *num_writes == 32) {
292 if (*old_bs != NULL) {
293 do_multiwrite(*old_bs, blkreq, *num_writes);
294 }
295 *num_writes = 0;
296 *old_bs = req->dev->bs;
87b245db 297 }
91553dcc
KW
298
299 blkreq[*num_writes].sector = req->out->sector;
300 blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
301 blkreq[*num_writes].qiov = &req->qiov;
302 blkreq[*num_writes].cb = virtio_blk_rw_complete;
303 blkreq[*num_writes].opaque = req;
304 blkreq[*num_writes].error = 0;
305
306 (*num_writes)++;
d28a1b6e 307}
869a5c6d 308
d28a1b6e
AL
309static void virtio_blk_handle_read(VirtIOBlockReq *req)
310{
87b245db
CH
311 BlockDriverAIOCB *acb;
312
313 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
314 req->qiov.size / 512, virtio_blk_rw_complete, req);
315 if (!acb) {
316 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
317 }
869a5c6d
AL
318}
319
6e02c38d
AL
320static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
321{
322 VirtIOBlock *s = to_virtio_blk(vdev);
323 VirtIOBlockReq *req;
91553dcc
KW
324 BlockRequest blkreq[32];
325 int num_writes = 0;
326 BlockDriverState *old_bs = NULL;
6e02c38d
AL
327
328 while ((req = virtio_blk_get_request(s))) {
6e02c38d
AL
329 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
330 fprintf(stderr, "virtio-blk missing headers\n");
331 exit(1);
332 }
333
334 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
335 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
336 fprintf(stderr, "virtio-blk header not in correct element\n");
337 exit(1);
338 }
339
340 req->out = (void *)req->elem.out_sg[0].iov_base;
341 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
342
aa659be3
CH
343 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
344 virtio_blk_handle_flush(req);
345 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
1063b8b1 346 virtio_blk_handle_scsi(req);
6e02c38d 347 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
d28a1b6e
AL
348 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
349 req->elem.out_num - 1);
91553dcc 350 virtio_blk_handle_write(blkreq, &num_writes, req, &old_bs);
6e02c38d 351 } else {
d28a1b6e
AL
352 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
353 req->elem.in_num - 1);
354 virtio_blk_handle_read(req);
6e02c38d
AL
355 }
356 }
91553dcc
KW
357
358 if (num_writes > 0) {
359 do_multiwrite(old_bs, blkreq, num_writes);
360 }
361
6e02c38d
AL
362 /*
363 * FIXME: Want to check for completions before returning to guest mode,
364 * so cached reads and writes are reported as quickly as possible. But
365 * that should be done in the generic block layer.
366 */
367}
368
213189ab 369static void virtio_blk_dma_restart_bh(void *opaque)
869a5c6d
AL
370{
371 VirtIOBlock *s = opaque;
372 VirtIOBlockReq *req = s->rq;
373
213189ab
MA
374 qemu_bh_delete(s->bh);
375 s->bh = NULL;
869a5c6d
AL
376
377 s->rq = NULL;
378
379 while (req) {
91553dcc
KW
380 bdrv_aio_writev(req->dev->bs, req->out->sector, &req->qiov,
381 req->qiov.size / 512, virtio_blk_rw_complete, req);
869a5c6d
AL
382 req = req->next;
383 }
384}
385
213189ab
MA
386static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
387{
388 VirtIOBlock *s = opaque;
389
390 if (!running)
391 return;
392
393 if (!s->bh) {
394 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
395 qemu_bh_schedule(s->bh);
396 }
397}
398
6e02c38d
AL
399static void virtio_blk_reset(VirtIODevice *vdev)
400{
401 /*
402 * This should cancel pending requests, but can't do nicely until there
403 * are per-device request lists.
404 */
405 qemu_aio_flush();
406}
407
bf011293 408/* coalesce internal state, copy to pci i/o region 0
409 */
6e02c38d
AL
410static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
411{
412 VirtIOBlock *s = to_virtio_blk(vdev);
413 struct virtio_blk_config blkcfg;
414 uint64_t capacity;
415 int cylinders, heads, secs;
416
417 bdrv_get_geometry(s->bs, &capacity);
418 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
5c5dafdc 419 memset(&blkcfg, 0, sizeof(blkcfg));
6e02c38d
AL
420 stq_raw(&blkcfg.capacity, capacity);
421 stl_raw(&blkcfg.seg_max, 128 - 2);
422 stw_raw(&blkcfg.cylinders, cylinders);
423 blkcfg.heads = heads;
424 blkcfg.sectors = secs;
c7085da7 425 blkcfg.size_max = 0;
bf011293 426 virtio_identify_template(&blkcfg);
427 memcpy(&blkcfg.identify[VIRTIO_BLK_ID_SN], s->serial_str,
428 VIRTIO_BLK_ID_SN_BYTES);
711bf3d9 429 memcpy(config, &blkcfg, s->config_size);
6e02c38d
AL
430}
431
8172539d 432static uint32_t virtio_blk_get_features(VirtIODevice *vdev, uint32_t features)
6e02c38d 433{
bf011293 434 VirtIOBlock *s = to_virtio_blk(vdev);
1063b8b1
CH
435
436 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
437 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
aa659be3
CH
438
439 if (bdrv_enable_write_cache(s->bs))
440 features |= (1 << VIRTIO_BLK_F_WCACHE);
bf011293 441 if (strcmp(s->serial_str, "0"))
442 features |= 1 << VIRTIO_BLK_F_IDENTIFY;
c79662f7
NS
443
444 if (bdrv_is_read_only(s->bs))
445 features |= 1 << VIRTIO_BLK_F_RO;
1063b8b1
CH
446
447 return features;
6e02c38d
AL
448}
449
450static void virtio_blk_save(QEMUFile *f, void *opaque)
451{
452 VirtIOBlock *s = opaque;
869a5c6d
AL
453 VirtIOBlockReq *req = s->rq;
454
6e02c38d 455 virtio_save(&s->vdev, f);
869a5c6d
AL
456
457 while (req) {
458 qemu_put_sbyte(f, 1);
459 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
460 req = req->next;
461 }
462 qemu_put_sbyte(f, 0);
6e02c38d
AL
463}
464
465static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
466{
467 VirtIOBlock *s = opaque;
468
869a5c6d 469 if (version_id != 2)
6e02c38d
AL
470 return -EINVAL;
471
472 virtio_load(&s->vdev, f);
869a5c6d
AL
473 while (qemu_get_sbyte(f)) {
474 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
475 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
476 req->next = s->rq;
477 s->rq = req->next;
478 }
6e02c38d
AL
479
480 return 0;
481}
482
d176c495 483VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo)
6e02c38d
AL
484{
485 VirtIOBlock *s;
486 int cylinders, heads, secs;
487 static int virtio_blk_id;
711bf3d9
MT
488 char *ps = (char *)drive_get_serial(dinfo->bdrv);
489 size_t size = strlen(ps) ? sizeof(struct virtio_blk_config) :
490 offsetof(struct virtio_blk_config, _blk_size);
cf21e106 491
53c25cea 492 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
711bf3d9 493 size,
53c25cea 494 sizeof(VirtIOBlock));
6e02c38d 495
711bf3d9 496 s->config_size = size;
6e02c38d
AL
497 s->vdev.get_config = virtio_blk_update_config;
498 s->vdev.get_features = virtio_blk_get_features;
499 s->vdev.reset = virtio_blk_reset;
d176c495 500 s->bs = dinfo->bdrv;
869a5c6d 501 s->rq = NULL;
711bf3d9 502 if (strlen(ps))
bf011293 503 strncpy(s->serial_str, ps, sizeof(s->serial_str));
504 else
505 snprintf(s->serial_str, sizeof(s->serial_str), "0");
6e02c38d
AL
506 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
507 bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
508
509 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
510
869a5c6d
AL
511 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
512 register_savevm("virtio-blk", virtio_blk_id++, 2,
6e02c38d
AL
513 virtio_blk_save, virtio_blk_load, s);
514
53c25cea 515 return &s->vdev;
6e02c38d 516}