xref: /qemu/include/hw/virtio/virtio-blk.h (revision 226419d6)
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 #ifndef _QEMU_VIRTIO_BLK_H
15 #define _QEMU_VIRTIO_BLK_H
16 
17 #include "standard-headers/linux/virtio_blk.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/block/block.h"
20 #include "sysemu/iothread.h"
21 #include "sysemu/block-backend.h"
22 
23 #define TYPE_VIRTIO_BLK "virtio-blk-device"
24 #define VIRTIO_BLK(obj) \
25         OBJECT_CHECK(VirtIOBlock, (obj), TYPE_VIRTIO_BLK)
26 
27 /* This is the last element of the write scatter-gather list */
28 struct virtio_blk_inhdr
29 {
30     unsigned char status;
31 };
32 
33 struct VirtIOBlkConf
34 {
35     BlockConf conf;
36     IOThread *iothread;
37     char *serial;
38     uint32_t scsi;
39     uint32_t config_wce;
40     uint32_t request_merging;
41 };
42 
43 struct VirtIOBlockDataPlane;
44 
45 struct VirtIOBlockReq;
46 typedef struct VirtIOBlock {
47     VirtIODevice parent_obj;
48     BlockBackend *blk;
49     VirtQueue *vq;
50     void *rq;
51     QEMUBH *bh;
52     VirtIOBlkConf conf;
53     unsigned short sector_mask;
54     bool original_wce;
55     VMChangeStateEntry *change;
56     bool dataplane_started;
57     struct VirtIOBlockDataPlane *dataplane;
58 } VirtIOBlock;
59 
60 typedef struct VirtIOBlockReq {
61     VirtQueueElement elem;
62     int64_t sector_num;
63     VirtIOBlock *dev;
64     struct virtio_blk_inhdr *in;
65     struct virtio_blk_outhdr out;
66     QEMUIOVector qiov;
67     size_t in_len;
68     struct VirtIOBlockReq *next;
69     struct VirtIOBlockReq *mr_next;
70     BlockAcctCookie acct;
71 } VirtIOBlockReq;
72 
73 #define VIRTIO_BLK_MAX_MERGE_REQS 32
74 
75 typedef struct MultiReqBuffer {
76     VirtIOBlockReq *reqs[VIRTIO_BLK_MAX_MERGE_REQS];
77     unsigned int num_reqs;
78     bool is_write;
79 } MultiReqBuffer;
80 
81 void virtio_blk_init_request(VirtIOBlock *s, VirtIOBlockReq *req);
82 void virtio_blk_free_request(VirtIOBlockReq *req);
83 
84 void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb);
85 
86 void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *mrb);
87 
88 #endif
89