xref: /openbmc/qemu/include/hw/virtio/virtio-blk.h (revision ffe98631)
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 #include "sysemu/block-ram-registrar.h"
23 #include "qom/object.h"
24 
25 #define TYPE_VIRTIO_BLK "virtio-blk-device"
26 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOBlock, VIRTIO_BLK)
27 
28 /* This is the last element of the write scatter-gather list */
29 struct virtio_blk_inhdr
30 {
31     unsigned char status;
32 };
33 
34 #define VIRTIO_BLK_AUTO_NUM_QUEUES UINT16_MAX
35 
36 struct VirtIOBlkConf
37 {
38     BlockConf conf;
39     IOThread *iothread;
40     char *serial;
41     uint32_t request_merging;
42     uint16_t num_queues;
43     uint16_t queue_size;
44     bool seg_max_adjust;
45     bool report_discard_granularity;
46     uint32_t max_discard_sectors;
47     uint32_t max_write_zeroes_sectors;
48     bool x_enable_wce_if_config_wce;
49 };
50 
51 struct VirtIOBlockDataPlane;
52 
53 struct VirtIOBlockReq;
54 struct VirtIOBlock {
55     VirtIODevice parent_obj;
56     BlockBackend *blk;
57     void *rq;
58     QEMUBH *bh;
59     VirtIOBlkConf conf;
60     unsigned short sector_mask;
61     bool original_wce;
62     VMChangeStateEntry *change;
63     bool dataplane_disabled;
64     bool dataplane_started;
65     struct VirtIOBlockDataPlane *dataplane;
66     uint64_t host_features;
67     size_t config_size;
68     BlockRAMRegistrar blk_ram_registrar;
69 };
70 
71 typedef struct VirtIOBlockReq {
72     VirtQueueElement elem;
73     int64_t sector_num;
74     VirtIOBlock *dev;
75     VirtQueue *vq;
76     IOVDiscardUndo inhdr_undo;
77     IOVDiscardUndo outhdr_undo;
78     struct virtio_blk_inhdr *in;
79     struct virtio_blk_outhdr out;
80     QEMUIOVector qiov;
81     size_t in_len;
82     struct VirtIOBlockReq *next;
83     struct VirtIOBlockReq *mr_next;
84     BlockAcctCookie acct;
85 } VirtIOBlockReq;
86 
87 #define VIRTIO_BLK_MAX_MERGE_REQS 32
88 
89 typedef struct MultiReqBuffer {
90     VirtIOBlockReq *reqs[VIRTIO_BLK_MAX_MERGE_REQS];
91     unsigned int num_reqs;
92     bool is_write;
93 } MultiReqBuffer;
94 
95 void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
96 void virtio_blk_process_queued_requests(VirtIOBlock *s, bool is_bh);
97 
98 #endif
99