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