monitor: do_balloon(): Check for errors
[qemu/kraxel.git] / hw / virtio-blk.c
bloba2f063974ab88f7917634262128fbf6864addf39
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <qemu-common.h>
15 #include <sysemu.h>
16 #include "virtio-blk.h"
17 #include "block_int.h"
18 #ifdef __linux__
19 # include <scsi/sg.h>
20 #endif
22 typedef struct VirtIOBlock
24 VirtIODevice vdev;
25 BlockDriverState *bs;
26 VirtQueue *vq;
27 void *rq;
28 char serial_str[BLOCK_SERIAL_STRLEN + 1];
29 QEMUBH *bh;
30 size_t config_size;
31 } VirtIOBlock;
33 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
35 return (VirtIOBlock *)vdev;
38 /* store identify data in little endian format
40 static inline void put_le16(uint16_t *p, unsigned int v)
42 *p = cpu_to_le16(v);
45 /* copy to *dst from *src, nul pad dst tail as needed to len bytes
47 static inline void padstr(char *dst, const char *src, int len)
49 while (len--)
50 *dst++ = *src ? *src++ : '\0';
53 /* setup simulated identify data as appropriate for virtio block device
55 * ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
57 static inline void virtio_identify_template(struct virtio_blk_config *bc)
59 uint16_t *p = &bc->identify[0];
60 uint64_t lba_sectors = bc->capacity;
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);
79 typedef struct VirtIOBlockReq
81 VirtIOBlock *dev;
82 VirtQueueElement elem;
83 struct virtio_blk_inhdr *in;
84 struct virtio_blk_outhdr *out;
85 struct virtio_scsi_inhdr *scsi;
86 QEMUIOVector qiov;
87 struct VirtIOBlockReq *next;
88 } VirtIOBlockReq;
90 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
92 VirtIOBlock *s = req->dev;
94 req->in->status = status;
95 virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
96 virtio_notify(&s->vdev, s->vq);
98 qemu_free(req);
101 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
102 int is_read)
104 BlockInterfaceErrorAction action =
105 drive_get_on_error(req->dev->bs, is_read);
106 VirtIOBlock *s = req->dev;
108 if (action == BLOCK_ERR_IGNORE)
109 return 0;
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);
120 return 1;
123 static void virtio_blk_rw_complete(void *opaque, int ret)
125 VirtIOBlockReq *req = opaque;
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))
130 return;
133 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
136 static void virtio_blk_flush_complete(void *opaque, int ret)
138 VirtIOBlockReq *req = opaque;
140 virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK);
143 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
145 VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
146 req->dev = s;
147 return req;
150 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
152 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
154 if (req != NULL) {
155 if (!virtqueue_pop(s->vq, &req->elem)) {
156 qemu_free(req);
157 return NULL;
161 return req;
164 #ifdef __linux__
165 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
167 struct sg_io_hdr hdr;
168 int ret, size = 0;
169 int status;
170 int i;
173 * We require at least one output segment each for the virtio_blk_outhdr
174 * and the SCSI command block.
176 * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
177 * and the sense buffer pointer in the input segments.
179 if (req->elem.out_num < 2 || req->elem.in_num < 3) {
180 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
181 return;
185 * No support for bidirection commands yet.
187 if (req->elem.out_num > 2 && req->elem.in_num > 3) {
188 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
189 return;
193 * The scsi inhdr is placed in the second-to-last input segment, just
194 * before the regular inhdr.
196 req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
197 size = sizeof(*req->in) + sizeof(*req->scsi);
199 memset(&hdr, 0, sizeof(struct sg_io_hdr));
200 hdr.interface_id = 'S';
201 hdr.cmd_len = req->elem.out_sg[1].iov_len;
202 hdr.cmdp = req->elem.out_sg[1].iov_base;
203 hdr.dxfer_len = 0;
205 if (req->elem.out_num > 2) {
207 * If there are more than the minimally required 2 output segments
208 * there is write payload starting from the third iovec.
210 hdr.dxfer_direction = SG_DXFER_TO_DEV;
211 hdr.iovec_count = req->elem.out_num - 2;
213 for (i = 0; i < hdr.iovec_count; i++)
214 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
216 hdr.dxferp = req->elem.out_sg + 2;
218 } else if (req->elem.in_num > 3) {
220 * If we have more than 3 input segments the guest wants to actually
221 * read data.
223 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
224 hdr.iovec_count = req->elem.in_num - 3;
225 for (i = 0; i < hdr.iovec_count; i++)
226 hdr.dxfer_len += req->elem.in_sg[i].iov_len;
228 hdr.dxferp = req->elem.in_sg;
229 size += hdr.dxfer_len;
230 } else {
232 * Some SCSI commands don't actually transfer any data.
234 hdr.dxfer_direction = SG_DXFER_NONE;
237 hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
238 hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
239 size += hdr.mx_sb_len;
241 ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
242 if (ret) {
243 status = VIRTIO_BLK_S_UNSUPP;
244 hdr.status = ret;
245 hdr.resid = hdr.dxfer_len;
246 } else if (hdr.status) {
247 status = VIRTIO_BLK_S_IOERR;
248 } else {
249 status = VIRTIO_BLK_S_OK;
252 req->scsi->errors = hdr.status;
253 req->scsi->residual = hdr.resid;
254 req->scsi->sense_len = hdr.sb_len_wr;
255 req->scsi->data_len = hdr.dxfer_len;
257 virtio_blk_req_complete(req, status);
259 #else
260 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
262 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
264 #endif /* __linux__ */
266 static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
267 int num_writes)
269 int i, ret;
270 ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
272 if (ret != 0) {
273 for (i = 0; i < num_writes; i++) {
274 if (blkreq[i].error) {
275 virtio_blk_req_complete(blkreq[i].opaque, VIRTIO_BLK_S_IOERR);
281 static void virtio_blk_handle_flush(VirtIOBlockReq *req)
283 BlockDriverAIOCB *acb;
285 acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
286 if (!acb) {
287 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
291 static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
292 VirtIOBlockReq *req, BlockDriverState **old_bs)
294 if (req->dev->bs != *old_bs || *num_writes == 32) {
295 if (*old_bs != NULL) {
296 do_multiwrite(*old_bs, blkreq, *num_writes);
298 *num_writes = 0;
299 *old_bs = req->dev->bs;
302 blkreq[*num_writes].sector = req->out->sector;
303 blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
304 blkreq[*num_writes].qiov = &req->qiov;
305 blkreq[*num_writes].cb = virtio_blk_rw_complete;
306 blkreq[*num_writes].opaque = req;
307 blkreq[*num_writes].error = 0;
309 (*num_writes)++;
312 static void virtio_blk_handle_read(VirtIOBlockReq *req)
314 BlockDriverAIOCB *acb;
316 acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
317 req->qiov.size / 512, virtio_blk_rw_complete, req);
318 if (!acb) {
319 virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
323 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
325 VirtIOBlock *s = to_virtio_blk(vdev);
326 VirtIOBlockReq *req;
327 BlockRequest blkreq[32];
328 int num_writes = 0;
329 BlockDriverState *old_bs = NULL;
331 while ((req = virtio_blk_get_request(s))) {
332 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
333 fprintf(stderr, "virtio-blk missing headers\n");
334 exit(1);
337 if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
338 req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
339 fprintf(stderr, "virtio-blk header not in correct element\n");
340 exit(1);
343 req->out = (void *)req->elem.out_sg[0].iov_base;
344 req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
346 if (req->out->type & VIRTIO_BLK_T_FLUSH) {
347 virtio_blk_handle_flush(req);
348 } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
349 virtio_blk_handle_scsi(req);
350 } else if (req->out->type & VIRTIO_BLK_T_OUT) {
351 qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
352 req->elem.out_num - 1);
353 virtio_blk_handle_write(blkreq, &num_writes, req, &old_bs);
354 } else {
355 qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
356 req->elem.in_num - 1);
357 virtio_blk_handle_read(req);
361 if (num_writes > 0) {
362 do_multiwrite(old_bs, blkreq, num_writes);
366 * FIXME: Want to check for completions before returning to guest mode,
367 * so cached reads and writes are reported as quickly as possible. But
368 * that should be done in the generic block layer.
372 static void virtio_blk_dma_restart_bh(void *opaque)
374 VirtIOBlock *s = opaque;
375 VirtIOBlockReq *req = s->rq;
377 qemu_bh_delete(s->bh);
378 s->bh = NULL;
380 s->rq = NULL;
382 while (req) {
383 bdrv_aio_writev(req->dev->bs, req->out->sector, &req->qiov,
384 req->qiov.size / 512, virtio_blk_rw_complete, req);
385 req = req->next;
389 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
391 VirtIOBlock *s = opaque;
393 if (!running)
394 return;
396 if (!s->bh) {
397 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
398 qemu_bh_schedule(s->bh);
402 static void virtio_blk_reset(VirtIODevice *vdev)
405 * This should cancel pending requests, but can't do nicely until there
406 * are per-device request lists.
408 qemu_aio_flush();
411 /* coalesce internal state, copy to pci i/o region 0
413 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
415 VirtIOBlock *s = to_virtio_blk(vdev);
416 struct virtio_blk_config blkcfg;
417 uint64_t capacity;
418 int cylinders, heads, secs;
420 bdrv_get_geometry(s->bs, &capacity);
421 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
422 memset(&blkcfg, 0, sizeof(blkcfg));
423 stq_raw(&blkcfg.capacity, capacity);
424 stl_raw(&blkcfg.seg_max, 128 - 2);
425 stw_raw(&blkcfg.cylinders, cylinders);
426 blkcfg.heads = heads;
427 blkcfg.sectors = secs;
428 blkcfg.size_max = 0;
429 virtio_identify_template(&blkcfg);
430 memcpy(&blkcfg.identify[VIRTIO_BLK_ID_SN], s->serial_str,
431 VIRTIO_BLK_ID_SN_BYTES);
432 memcpy(config, &blkcfg, s->config_size);
435 static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
437 VirtIOBlock *s = to_virtio_blk(vdev);
438 uint32_t features = 0;
440 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
441 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
443 if (bdrv_enable_write_cache(s->bs))
444 features |= (1 << VIRTIO_BLK_F_WCACHE);
445 #ifdef __linux__
446 features |= (1 << VIRTIO_BLK_F_SCSI);
447 #endif
448 if (strcmp(s->serial_str, "0"))
449 features |= 1 << VIRTIO_BLK_F_IDENTIFY;
451 if (bdrv_is_read_only(s->bs))
452 features |= 1 << VIRTIO_BLK_F_RO;
454 return features;
457 static void virtio_blk_save(QEMUFile *f, void *opaque)
459 VirtIOBlock *s = opaque;
460 VirtIOBlockReq *req = s->rq;
462 virtio_save(&s->vdev, f);
464 while (req) {
465 qemu_put_sbyte(f, 1);
466 qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
467 req = req->next;
469 qemu_put_sbyte(f, 0);
472 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
474 VirtIOBlock *s = opaque;
476 if (version_id != 2)
477 return -EINVAL;
479 virtio_load(&s->vdev, f);
480 while (qemu_get_sbyte(f)) {
481 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
482 qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
483 req->next = s->rq;
484 s->rq = req->next;
487 return 0;
490 VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo)
492 VirtIOBlock *s;
493 int cylinders, heads, secs;
494 static int virtio_blk_id;
495 char *ps = (char *)drive_get_serial(dinfo->bdrv);
496 size_t size = strlen(ps) ? sizeof(struct virtio_blk_config) :
497 offsetof(struct virtio_blk_config, _blk_size);
499 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
500 size,
501 sizeof(VirtIOBlock));
503 s->config_size = size;
504 s->vdev.get_config = virtio_blk_update_config;
505 s->vdev.get_features = virtio_blk_get_features;
506 s->vdev.reset = virtio_blk_reset;
507 s->bs = dinfo->bdrv;
508 s->rq = NULL;
509 if (strlen(ps))
510 strncpy(s->serial_str, ps, sizeof(s->serial_str));
511 else
512 snprintf(s->serial_str, sizeof(s->serial_str), "0");
513 bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
514 bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
516 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
518 qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
519 register_savevm("virtio-blk", virtio_blk_id++, 2,
520 virtio_blk_save, virtio_blk_load, s);
522 return &s->vdev;