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