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