Statistics
| Branch: | Revision:

root / hw / virtio-blk.c @ bf011293

History | View | Annotate | Download (12.1 kB)

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
} VirtIOBlock;
30

    
31
static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
32
{
33
    return (VirtIOBlock *)vdev;
34
}
35

    
36
/* store identify data in little endian format
37
 */
38
static inline void put_le16(uint16_t *p, unsigned int v)
39
{
40
    *p = cpu_to_le16(v);
41
}
42

    
43
/* copy to *dst from *src, nul pad dst tail as needed to len bytes
44
 */
45
static inline void padstr(char *dst, const char *src, int len)
46
{
47
    while (len--)
48
        *dst++ = *src ? *src++ : '\0';
49
}
50

    
51
/* setup simulated identify data as appropriate for virtio block device
52
 *
53
 * ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
54
 */
55
static inline void virtio_identify_template(struct virtio_blk_config *bc)
56
{
57
    uint16_t *p = &bc->identify[0];
58
    uint64_t lba_sectors = bc->capacity;
59

    
60
    memset(p, 0, sizeof(bc->identify));
61
    put_le16(p + 0, 0x0);                            /* ATA device */
62
    padstr((char *)(p + 23), QEMU_VERSION, 8);       /* firmware revision */
63
    padstr((char *)(p + 27), "QEMU VIRT_BLK", 40);   /* model# */
64
    put_le16(p + 47, 0x80ff);                        /* max xfer 255 sectors */
65
    put_le16(p + 49, 0x0b00);                        /* support IORDY/LBA/DMA */
66
    put_le16(p + 59, 0x1ff);                         /* cur xfer 255 sectors */
67
    put_le16(p + 80, 0x1f0);                         /* support ATA8/7/6/5/4 */
68
    put_le16(p + 81, 0x16);
69
    put_le16(p + 82, 0x400);
70
    put_le16(p + 83, 0x400);
71
    put_le16(p + 100, lba_sectors);
72
    put_le16(p + 101, lba_sectors >> 16);
73
    put_le16(p + 102, lba_sectors >> 32);
74
    put_le16(p + 103, lba_sectors >> 48);
75
}
76

    
77
typedef struct VirtIOBlockReq
78
{
79
    VirtIOBlock *dev;
80
    VirtQueueElement elem;
81
    struct virtio_blk_inhdr *in;
82
    struct virtio_blk_outhdr *out;
83
    struct virtio_scsi_inhdr *scsi;
84
    QEMUIOVector qiov;
85
    struct VirtIOBlockReq *next;
86
} VirtIOBlockReq;
87

    
88
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
89
{
90
    VirtIOBlock *s = req->dev;
91

    
92
    req->in->status = status;
93
    virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
94
    virtio_notify(&s->vdev, s->vq);
95

    
96
    qemu_free(req);
97
}
98

    
99
static int virtio_blk_handle_write_error(VirtIOBlockReq *req, int error)
100
{
101
    BlockInterfaceErrorAction action = drive_get_onerror(req->dev->bs);
102
    VirtIOBlock *s = req->dev;
103

    
104
    if (action == BLOCK_ERR_IGNORE)
105
        return 0;
106

    
107
    if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
108
            || action == BLOCK_ERR_STOP_ANY) {
109
        req->next = s->rq;
110
        s->rq = req;
111
        vm_stop(0);
112
    } else {
113
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
114
    }
115

    
116
    return 1;
117
}
118

    
119
static void virtio_blk_rw_complete(void *opaque, int ret)
120
{
121
    VirtIOBlockReq *req = opaque;
122

    
123
    if (ret && (req->out->type & VIRTIO_BLK_T_OUT)) {
124
        if (virtio_blk_handle_write_error(req, -ret))
125
            return;
126
    }
127

    
128
    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
129
}
130

    
131
static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
132
{
133
    VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
134
    req->dev = s;
135
    return req;
136
}
137

    
138
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
139
{
140
    VirtIOBlockReq *req = virtio_blk_alloc_request(s);
141

    
142
    if (req != NULL) {
143
        if (!virtqueue_pop(s->vq, &req->elem)) {
144
            qemu_free(req);
145
            return NULL;
146
        }
147
    }
148

    
149
    return req;
150
}
151

    
152
#ifdef __linux__
153
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
154
{
155
    struct sg_io_hdr hdr;
156
    int ret, size = 0;
157
    int status;
158
    int i;
159

    
160
    /*
161
     * We require at least one output segment each for the virtio_blk_outhdr
162
     * and the SCSI command block.
163
     *
164
     * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
165
     * and the sense buffer pointer in the input segments.
166
     */
167
    if (req->elem.out_num < 2 || req->elem.in_num < 3) {
168
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
169
        return;
170
    }
171

    
172
    /*
173
     * No support for bidirection commands yet.
174
     */
175
    if (req->elem.out_num > 2 && req->elem.in_num > 3) {
176
        virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
177
        return;
178
    }
179

    
180
    /*
181
     * The scsi inhdr is placed in the second-to-last input segment, just
182
     * before the regular inhdr.
183
     */
184
    req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base;
185
    size = sizeof(*req->in) + sizeof(*req->scsi);
186

    
187
    memset(&hdr, 0, sizeof(struct sg_io_hdr));
188
    hdr.interface_id = 'S';
189
    hdr.cmd_len = req->elem.out_sg[1].iov_len;
190
    hdr.cmdp = req->elem.out_sg[1].iov_base;
191
    hdr.dxfer_len = 0;
192

    
193
    if (req->elem.out_num > 2) {
194
        /*
195
         * If there are more than the minimally required 2 output segments
196
         * there is write payload starting from the third iovec.
197
         */
198
        hdr.dxfer_direction = SG_DXFER_TO_DEV;
199
        hdr.iovec_count = req->elem.out_num - 2;
200

    
201
        for (i = 0; i < hdr.iovec_count; i++)
202
            hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
203

    
204
        hdr.dxferp = req->elem.out_sg + 2;
205

    
206
    } else if (req->elem.in_num > 3) {
207
        /*
208
         * If we have more than 3 input segments the guest wants to actually
209
         * read data.
210
         */
211
        hdr.dxfer_direction = SG_DXFER_FROM_DEV;
212
        hdr.iovec_count = req->elem.in_num - 3;
213
        for (i = 0; i < hdr.iovec_count; i++)
214
            hdr.dxfer_len += req->elem.in_sg[i].iov_len;
215

    
216
        hdr.dxferp = req->elem.in_sg;
217
        size += hdr.dxfer_len;
218
    } else {
219
        /*
220
         * Some SCSI commands don't actually transfer any data.
221
         */
222
        hdr.dxfer_direction = SG_DXFER_NONE;
223
    }
224

    
225
    hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
226
    hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
227
    size += hdr.mx_sb_len;
228

    
229
    ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr);
230
    if (ret) {
231
        status = VIRTIO_BLK_S_UNSUPP;
232
        hdr.status = ret;
233
        hdr.resid = hdr.dxfer_len;
234
    } else if (hdr.status) {
235
        status = VIRTIO_BLK_S_IOERR;
236
    } else {
237
        status = VIRTIO_BLK_S_OK;
238
    }
239

    
240
    req->scsi->errors = hdr.status;
241
    req->scsi->residual = hdr.resid;
242
    req->scsi->sense_len = hdr.sb_len_wr;
243
    req->scsi->data_len = hdr.dxfer_len;
244

    
245
    virtio_blk_req_complete(req, status);
246
}
247
#else
248
static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
249
{
250
    virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
251
}
252
#endif /* __linux__ */
253

    
254
static void virtio_blk_handle_write(VirtIOBlockReq *req)
255
{
256
    bdrv_aio_writev(req->dev->bs, req->out->sector, &req->qiov,
257
                    req->qiov.size / 512, virtio_blk_rw_complete, req);
258
}
259

    
260
static void virtio_blk_handle_read(VirtIOBlockReq *req)
261
{
262
    bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov,
263
                   req->qiov.size / 512, virtio_blk_rw_complete, req);
264
}
265

    
266
static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
267
{
268
    VirtIOBlock *s = to_virtio_blk(vdev);
269
    VirtIOBlockReq *req;
270

    
271
    while ((req = virtio_blk_get_request(s))) {
272
        if (req->elem.out_num < 1 || req->elem.in_num < 1) {
273
            fprintf(stderr, "virtio-blk missing headers\n");
274
            exit(1);
275
        }
276

    
277
        if (req->elem.out_sg[0].iov_len < sizeof(*req->out) ||
278
            req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) {
279
            fprintf(stderr, "virtio-blk header not in correct element\n");
280
            exit(1);
281
        }
282

    
283
        req->out = (void *)req->elem.out_sg[0].iov_base;
284
        req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;
285

    
286
        if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
287
            virtio_blk_handle_scsi(req);
288
        } else if (req->out->type & VIRTIO_BLK_T_OUT) {
289
            qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
290
                                     req->elem.out_num - 1);
291
            virtio_blk_handle_write(req);
292
        } else {
293
            qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
294
                                     req->elem.in_num - 1);
295
            virtio_blk_handle_read(req);
296
        }
297
    }
298
    /*
299
     * FIXME: Want to check for completions before returning to guest mode,
300
     * so cached reads and writes are reported as quickly as possible. But
301
     * that should be done in the generic block layer.
302
     */
303
}
304

    
305
static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason)
306
{
307
    VirtIOBlock *s = opaque;
308
    VirtIOBlockReq *req = s->rq;
309

    
310
    if (!running)
311
        return;
312

    
313
    s->rq = NULL;
314

    
315
    while (req) {
316
        virtio_blk_handle_write(req);
317
        req = req->next;
318
    }
319
}
320

    
321
static void virtio_blk_reset(VirtIODevice *vdev)
322
{
323
    /*
324
     * This should cancel pending requests, but can't do nicely until there
325
     * are per-device request lists.
326
     */
327
    qemu_aio_flush();
328
}
329

    
330
/* coalesce internal state, copy to pci i/o region 0
331
 */
332
static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config)
333
{
334
    VirtIOBlock *s = to_virtio_blk(vdev);
335
    struct virtio_blk_config blkcfg;
336
    uint64_t capacity;
337
    int cylinders, heads, secs;
338

    
339
    bdrv_get_geometry(s->bs, &capacity);
340
    bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
341
    memset(&blkcfg, 0, sizeof(blkcfg));
342
    stq_raw(&blkcfg.capacity, capacity);
343
    stl_raw(&blkcfg.seg_max, 128 - 2);
344
    stw_raw(&blkcfg.cylinders, cylinders);
345
    blkcfg.heads = heads;
346
    blkcfg.sectors = secs;
347
    blkcfg.size_max = 0;
348
    virtio_identify_template(&blkcfg);
349
    memcpy(&blkcfg.identify[VIRTIO_BLK_ID_SN], s->serial_str,
350
        VIRTIO_BLK_ID_SN_BYTES);
351
    memcpy(config, &blkcfg, sizeof(blkcfg));
352
}
353

    
354
static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
355
{
356
    VirtIOBlock *s = to_virtio_blk(vdev);
357
    uint32_t features = 0;
358

    
359
    features |= (1 << VIRTIO_BLK_F_SEG_MAX);
360
    features |= (1 << VIRTIO_BLK_F_GEOMETRY);
361
#ifdef __linux__
362
    features |= (1 << VIRTIO_BLK_F_SCSI);
363
#endif
364
    if (strcmp(s->serial_str, "0"))
365
        features |= 1 << VIRTIO_BLK_F_IDENTIFY;
366

    
367
    return features;
368
}
369

    
370
static void virtio_blk_save(QEMUFile *f, void *opaque)
371
{
372
    VirtIOBlock *s = opaque;
373
    VirtIOBlockReq *req = s->rq;
374

    
375
    virtio_save(&s->vdev, f);
376
    
377
    while (req) {
378
        qemu_put_sbyte(f, 1);
379
        qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
380
        req = req->next;
381
    }
382
    qemu_put_sbyte(f, 0);
383
}
384

    
385
static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
386
{
387
    VirtIOBlock *s = opaque;
388

    
389
    if (version_id != 2)
390
        return -EINVAL;
391

    
392
    virtio_load(&s->vdev, f);
393
    while (qemu_get_sbyte(f)) {
394
        VirtIOBlockReq *req = virtio_blk_alloc_request(s);
395
        qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem));
396
        req->next = s->rq;
397
        s->rq = req->next;
398
    }
399

    
400
    return 0;
401
}
402

    
403
VirtIODevice *virtio_blk_init(DeviceState *dev)
404
{
405
    VirtIOBlock *s;
406
    int cylinders, heads, secs;
407
    static int virtio_blk_id;
408
    BlockDriverState *bs;
409
    char *ps;
410

    
411
    s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
412
                                          sizeof(struct virtio_blk_config),
413
                                          sizeof(VirtIOBlock));
414

    
415
    bs = qdev_init_bdrv(dev, IF_VIRTIO);
416
    s->vdev.get_config = virtio_blk_update_config;
417
    s->vdev.get_features = virtio_blk_get_features;
418
    s->vdev.reset = virtio_blk_reset;
419
    s->bs = bs;
420
    s->rq = NULL;
421
    if (strlen(ps = (char *)drive_get_serial(bs)))
422
        strncpy(s->serial_str, ps, sizeof(s->serial_str));
423
    else
424
        snprintf(s->serial_str, sizeof(s->serial_str), "0");
425
    bs->private = dev;
426
    bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
427
    bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
428

    
429
    s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
430

    
431
    qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
432
    register_savevm("virtio-blk", virtio_blk_id++, 2,
433
                    virtio_blk_save, virtio_blk_load, s);
434

    
435
    return &s->vdev;
436
}