xref: /openbmc/qemu/pc-bios/s390-ccw/virtio.h (revision 2993683b)
1 /*
2  * Virtio driver bits
3  *
4  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or (at
7  * your option) any later version. See the COPYING file in the top-level
8  * directory.
9  */
10 
11 #ifndef VIRTIO_H
12 #define VIRTIO_H
13 
14 #include "s390-ccw.h"
15 
16 /* Status byte for guest to report progress, and synchronize features. */
17 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
18 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
19 /* We have found a driver for the device. */
20 #define VIRTIO_CONFIG_S_DRIVER          2
21 /* Driver has used its parts of the config, and is happy */
22 #define VIRTIO_CONFIG_S_DRIVER_OK       4
23 /* We've given up on this device. */
24 #define VIRTIO_CONFIG_S_FAILED          0x80
25 
26 enum virtio_dev_type {
27     VIRTIO_ID_NET = 1,
28     VIRTIO_ID_BLOCK = 2,
29     VIRTIO_ID_CONSOLE = 3,
30     VIRTIO_ID_BALLOON = 5,
31 };
32 
33 struct virtio_dev_header {
34     enum virtio_dev_type type : 8;
35     u8  num_vq;
36     u8  feature_len;
37     u8  config_len;
38     u8  status;
39     u8  vqconfig[];
40 } __attribute__((packed));
41 
42 struct virtio_vqconfig {
43     u64 token;
44     u64 address;
45     u16 num;
46     u8  pad[6];
47 } __attribute__((packed));
48 
49 struct vq_info_block {
50     u64 queue;
51     u32 align;
52     u16 index;
53     u16 num;
54 } __attribute__((packed));
55 
56 struct vq_config_block {
57     u16 index;
58     u16 num;
59 } __attribute__((packed));
60 
61 struct virtio_dev {
62     struct virtio_dev_header *header;
63     struct virtio_vqconfig *vqconfig;
64     char *host_features;
65     char *guest_features;
66     char *config;
67 };
68 
69 #define KVM_S390_VIRTIO_RING_ALIGN	4096
70 
71 #define VRING_USED_F_NO_NOTIFY  1
72 
73 /* This marks a buffer as continuing via the next field. */
74 #define VRING_DESC_F_NEXT       1
75 /* This marks a buffer as write-only (otherwise read-only). */
76 #define VRING_DESC_F_WRITE      2
77 /* This means the buffer contains a list of buffer descriptors. */
78 #define VRING_DESC_F_INDIRECT   4
79 
80 /* Internal flag to mark follow-up segments as such */
81 #define VRING_HIDDEN_IS_CHAIN   256
82 
83 /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
84 struct vring_desc {
85     /* Address (guest-physical). */
86     u64 addr;
87     /* Length. */
88     u32 len;
89     /* The flags as indicated above. */
90     u16 flags;
91     /* We chain unused descriptors via this, too */
92     u16 next;
93 } __attribute__((packed));
94 
95 struct vring_avail {
96     u16 flags;
97     u16 idx;
98     u16 ring[];
99 } __attribute__((packed));
100 
101 /* u32 is used here for ids for padding reasons. */
102 struct vring_used_elem {
103     /* Index of start of used descriptor chain. */
104     u32 id;
105     /* Total length of the descriptor chain which was used (written to) */
106     u32 len;
107 } __attribute__((packed));
108 
109 struct vring_used {
110     u16 flags;
111     u16 idx;
112     struct vring_used_elem ring[];
113 } __attribute__((packed));
114 
115 struct vring {
116     unsigned int num;
117     int next_idx;
118     struct vring_desc *desc;
119     struct vring_avail *avail;
120     struct vring_used *used;
121     struct subchannel_id schid;
122 };
123 
124 
125 /***********************************************
126  *               Virtio block                  *
127  ***********************************************/
128 
129 /*
130  * Command types
131  *
132  * Usage is a bit tricky as some bits are used as flags and some are not.
133  *
134  * Rules:
135  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
136  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
137  *   and may not be combined with any of the other flags.
138  */
139 
140 /* These two define direction. */
141 #define VIRTIO_BLK_T_IN         0
142 #define VIRTIO_BLK_T_OUT        1
143 
144 /* This bit says it's a scsi command, not an actual read or write. */
145 #define VIRTIO_BLK_T_SCSI_CMD   2
146 
147 /* Cache flush command */
148 #define VIRTIO_BLK_T_FLUSH      4
149 
150 /* Barrier before this op. */
151 #define VIRTIO_BLK_T_BARRIER    0x80000000
152 
153 /* This is the first element of the read scatter-gather list. */
154 struct virtio_blk_outhdr {
155         /* VIRTIO_BLK_T* */
156         u32 type;
157         /* io priority. */
158         u32 ioprio;
159         /* Sector (ie. 512 byte offset) */
160         u64 sector;
161 };
162 
163 #endif /* VIRTIO_H */
164