]> git.proxmox.com Git - qemu.git/blob - hw/virtio-blk.c
tcg-sparc: Use TCG_TARGET_REG_BITS in conditional compilation.
[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 <sysemu.h>
16 #include "virtio-blk.h"
17 #include "block_int.h"
18 #ifdef __linux__
19 # include <scsi/sg.h>
20 #endif
21
22 typedef struct VirtIOBlock
23 {
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;
32
33 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
34 {
35 return (VirtIOBlock *)vdev;
36 }
37
38 /* store identify data in little endian format
39 */
40 static 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 */
47 static 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 */
57 static 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
79 typedef struct VirtIOBlockReq
80 {
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;
89
90 static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
91 {
92 VirtIOBlock *s = req->dev;
93
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);
97
98 qemu_free(req);
99 }
100
101 static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
102 int is_read)
103 {
104 BlockInterfaceErrorAction action =
105 drive_get_on_error(req->dev->bs, is_read);
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
123 static void virtio_blk_rw_complete(void *opaque, int ret)
124 {
125 VirtIOBlockReq *req = opaque;
126
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;
131 }
132
133 virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
134 }
135
136 static 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
143 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
144 {
145 VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
146 req->dev = s;
147 return req;
148 }
149
150 static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
151 {
152 VirtIOBlockReq *req = virtio_blk_alloc_request(s);
153
154 if (req != NULL) {
155 if (!virtqueue_pop(s->vq, &req->elem)) {
156 qemu_free(req);
157 return NULL;
158 }
159 }
160
161 return req;
162 }
163
164 #ifdef __linux__
165 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
166 {
167 struct sg_io_hdr hdr;
168 int ret, size = 0;
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;
197 size = sizeof(*req->in) + sizeof(*req->scsi);
198
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;
204
205 if (req->elem.out_num > 2) {
206 /*
207 * If there are more than the minimally required 2 output segments
208 * there is write payload starting from the third iovec.
209 */
210 hdr.dxfer_direction = SG_DXFER_TO_DEV;
211 hdr.iovec_count = req->elem.out_num - 2;
212
213 for (i = 0; i < hdr.iovec_count; i++)
214 hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
215
216 hdr.dxferp = req->elem.out_sg + 2;
217
218 } else if (req->elem.in_num > 3) {
219 /*
220 * If we have more than 3 input segments the guest wants to actually
221 * read data.
222 */
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;
227
228 hdr.dxferp = req->elem.in_sg;
229 size += hdr.dxfer_len;
230 } else {
231 /*
232 * Some SCSI commands don't actually transfer any data.
233 */
234 hdr.dxfer_direction = SG_DXFER_NONE;
235 }
236
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;
240
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;
250 }
251
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;
256
257 virtio_blk_req_complete(req, status);
258 }
259 #else
260 static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
261 {
262 virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
263 }
264 #endif /* __linux__ */
265
266 static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq,
267 int num_writes)
268 {
269 int i, ret;
270 ret = bdrv_aio_multiwrite(bs, blkreq, num_writes);
271
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);
276 }
277 }
278 }
279 }
280
281 static void virtio_blk_handle_flush(VirtIOBlockReq *req)
282 {
283 BlockDriverAIOCB *acb;
284
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);
288 }
289 }
290
291 static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes,
292 VirtIOBlockReq *req, BlockDriverState **old_bs)
293 {
294 if (req->dev->bs != *old_bs || *num_writes == 32) {
295 if (*old_bs != NULL) {
296 do_multiwrite(*old_bs, blkreq, *num_writes);
297 }
298 *num_writes = 0;
299 *old_bs = req->dev->bs;
300 }
301
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;
308
309 (*num_writes)++;
310 }
311
312 static void virtio_blk_handle_read(VirtIOBlockReq *req)
313 {
314 BlockDriverAIOCB *acb;
315
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);
320 }
321 }
322
323 static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
324 {
325 VirtIOBlock *s = to_virtio_blk(vdev);
326 VirtIOBlockReq *req;
327 BlockRequest blkreq[32];
328 int num_writes = 0;
329 BlockDriverState *old_bs = NULL;
330
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);
335 }
336
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);
341 }
342
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;
345
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);
358 }
359 }
360
361 if (num_writes > 0) {
362 do_multiwrite(old_bs, blkreq, num_writes);
363 }
364
365 /*
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.
369 */
370 }
371
372 static void virtio_blk_dma_restart_bh(void *opaque)
373 {
374 VirtIOBlock *s = opaque;
375 VirtIOBlockReq *req = s->rq;
376
377 qemu_bh_delete(s->bh);
378 s->bh = NULL;
379
380 s->rq = NULL;
381
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;
386 }
387 }
388
389 static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
390 {
391 VirtIOBlock *s = opaque;
392
393 if (!running)
394 return;
395
396 if (!s->bh) {
397 s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s);
398 qemu_bh_schedule(s->bh);
399 }
400 }
401
402 static void virtio_blk_reset(VirtIODevice *vdev)
403 {
404 /*
405 * This should cancel pending requests, but can't do nicely until there
406 * are per-device request lists.
407 */
408 qemu_aio_flush();
409 }
410
411 /* coalesce internal state, copy to pci i/o region 0
412 */
413 static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
414 {
415 VirtIOBlock *s = to_virtio_blk(vdev);
416 struct virtio_blk_config blkcfg;
417 uint64_t capacity;
418 int cylinders, heads, secs;
419
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);
433 }
434
435 static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
436 {
437 VirtIOBlock *s = to_virtio_blk(vdev);
438 uint32_t features = 0;
439
440 features |= (1 << VIRTIO_BLK_F_SEG_MAX);
441 features |= (1 << VIRTIO_BLK_F_GEOMETRY);
442
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;
450
451 if (bdrv_is_read_only(s->bs))
452 features |= 1 << VIRTIO_BLK_F_RO;
453
454 return features;
455 }
456
457 static void virtio_blk_save(QEMUFile *f, void *opaque)
458 {
459 VirtIOBlock *s = opaque;
460 VirtIOBlockReq *req = s->rq;
461
462 virtio_save(&s->vdev, f);
463
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;
468 }
469 qemu_put_sbyte(f, 0);
470 }
471
472 static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
473 {
474 VirtIOBlock *s = opaque;
475
476 if (version_id != 2)
477 return -EINVAL;
478
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;
485 }
486
487 return 0;
488 }
489
490 VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo)
491 {
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);
498
499 s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
500 size,
501 sizeof(VirtIOBlock));
502
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);
515
516 s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
517
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);
521
522 return &s->vdev;
523 }