xref: /openbmc/qemu/pc-bios/s390-ccw/virtio.h (revision 8692aa29)
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 VirtioDevType {
27     VIRTIO_ID_NET = 1,
28     VIRTIO_ID_BLOCK = 2,
29     VIRTIO_ID_CONSOLE = 3,
30     VIRTIO_ID_BALLOON = 5,
31     VIRTIO_ID_SCSI = 8,
32 };
33 typedef enum VirtioDevType VirtioDevType;
34 
35 struct VirtioDevHeader {
36     VirtioDevType type:8;
37     uint8_t num_vq;
38     uint8_t feature_len;
39     uint8_t config_len;
40     uint8_t status;
41     uint8_t vqconfig[];
42 } __attribute__((packed));
43 typedef struct VirtioDevHeader VirtioDevHeader;
44 
45 struct VirtioVqConfig {
46     uint64_t token;
47     uint64_t address;
48     uint16_t num;
49     uint8_t pad[6];
50 } __attribute__((packed));
51 typedef struct VirtioVqConfig VirtioVqConfig;
52 
53 struct VqInfo {
54     uint64_t queue;
55     uint32_t align;
56     uint16_t index;
57     uint16_t num;
58 } __attribute__((packed));
59 typedef struct VqInfo VqInfo;
60 
61 struct VqConfig {
62     uint16_t index;
63     uint16_t num;
64 } __attribute__((packed));
65 typedef struct VqConfig VqConfig;
66 
67 struct VirtioDev {
68     VirtioDevHeader *header;
69     VirtioVqConfig *vqconfig;
70     char *host_features;
71     char *guest_features;
72     char *config;
73 };
74 typedef struct VirtioDev VirtioDev;
75 
76 #define VIRTIO_RING_SIZE            (PAGE_SIZE * 8)
77 #define VIRTIO_MAX_VQS              3
78 #define KVM_S390_VIRTIO_RING_ALIGN  4096
79 
80 #define VRING_USED_F_NO_NOTIFY  1
81 
82 /* This marks a buffer as continuing via the next field. */
83 #define VRING_DESC_F_NEXT       1
84 /* This marks a buffer as write-only (otherwise read-only). */
85 #define VRING_DESC_F_WRITE      2
86 /* This means the buffer contains a list of buffer descriptors. */
87 #define VRING_DESC_F_INDIRECT   4
88 
89 /* Internal flag to mark follow-up segments as such */
90 #define VRING_HIDDEN_IS_CHAIN   256
91 
92 /* Virtio ring descriptors: 16 bytes.  These can chain together via "next". */
93 struct VRingDesc {
94     /* Address (guest-physical). */
95     uint64_t addr;
96     /* Length. */
97     uint32_t len;
98     /* The flags as indicated above. */
99     uint16_t flags;
100     /* We chain unused descriptors via this, too */
101     uint16_t next;
102 } __attribute__((packed));
103 typedef struct VRingDesc VRingDesc;
104 
105 struct VRingAvail {
106     uint16_t flags;
107     uint16_t idx;
108     uint16_t ring[];
109 } __attribute__((packed));
110 typedef struct VRingAvail VRingAvail;
111 
112 /* uint32_t is used here for ids for padding reasons. */
113 struct VRingUsedElem {
114     /* Index of start of used descriptor chain. */
115     uint32_t id;
116     /* Total length of the descriptor chain which was used (written to) */
117     uint32_t len;
118 } __attribute__((packed));
119 typedef struct VRingUsedElem VRingUsedElem;
120 
121 struct VRingUsed {
122     uint16_t flags;
123     uint16_t idx;
124     VRingUsedElem ring[];
125 } __attribute__((packed));
126 typedef struct VRingUsed VRingUsed;
127 
128 struct VRing {
129     unsigned int num;
130     int next_idx;
131     int used_idx;
132     VRingDesc *desc;
133     VRingAvail *avail;
134     VRingUsed *used;
135     SubChannelId schid;
136     long cookie;
137     int id;
138 };
139 typedef struct VRing VRing;
140 
141 
142 /***********************************************
143  *               Virtio block                  *
144  ***********************************************/
145 
146 /*
147  * Command types
148  *
149  * Usage is a bit tricky as some bits are used as flags and some are not.
150  *
151  * Rules:
152  *   VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
153  *   VIRTIO_BLK_T_BARRIER.  VIRTIO_BLK_T_FLUSH is a command of its own
154  *   and may not be combined with any of the other flags.
155  */
156 
157 /* These two define direction. */
158 #define VIRTIO_BLK_T_IN         0
159 #define VIRTIO_BLK_T_OUT        1
160 
161 /* This bit says it's a scsi command, not an actual read or write. */
162 #define VIRTIO_BLK_T_SCSI_CMD   2
163 
164 /* Cache flush command */
165 #define VIRTIO_BLK_T_FLUSH      4
166 
167 /* Barrier before this op. */
168 #define VIRTIO_BLK_T_BARRIER    0x80000000
169 
170 /* This is the first element of the read scatter-gather list. */
171 struct VirtioBlkOuthdr {
172         /* VIRTIO_BLK_T* */
173         uint32_t type;
174         /* io priority. */
175         uint32_t ioprio;
176         /* Sector (ie. 512 byte offset) */
177         uint64_t sector;
178 };
179 typedef struct VirtioBlkOuthdr VirtioBlkOuthdr;
180 
181 struct VirtioBlkConfig {
182     uint64_t capacity; /* in 512-byte sectors */
183     uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */
184     uint32_t seg_max;  /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */
185 
186     struct VirtioBlkGeometry {
187         uint16_t cylinders;
188         uint8_t heads;
189         uint8_t sectors;
190     } geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */
191 
192     uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
193 
194     /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY  */
195     uint8_t physical_block_exp; /* exponent for physical blk per logical blk */
196     uint8_t alignment_offset;   /* alignment offset in logical blocks */
197     uint16_t min_io_size;       /* min I/O size without performance penalty
198                               in logical blocks */
199     uint32_t opt_io_size;       /* optimal sustained I/O size in logical blks */
200 
201     uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
202 } __attribute__((packed));
203 typedef struct VirtioBlkConfig VirtioBlkConfig;
204 
205 enum guessed_disk_nature_type {
206     VIRTIO_GDN_NONE     = 0,
207     VIRTIO_GDN_DASD     = 1,
208     VIRTIO_GDN_CDROM    = 2,
209     VIRTIO_GDN_SCSI     = 3,
210 };
211 typedef enum guessed_disk_nature_type VirtioGDN;
212 
213 VirtioGDN virtio_guessed_disk_nature(void);
214 void virtio_assume_scsi(void);
215 void virtio_assume_eckd(void);
216 void virtio_assume_iso9660(void);
217 
218 extern bool virtio_disk_is_scsi(void);
219 extern bool virtio_disk_is_eckd(void);
220 extern bool virtio_ipl_disk_is_valid(void);
221 extern int virtio_get_block_size(void);
222 extern uint8_t virtio_get_heads(void);
223 extern uint8_t virtio_get_sectors(void);
224 extern uint64_t virtio_get_blocks(void);
225 extern int virtio_read_many(ulong sector, void *load_addr, int sec_num);
226 
227 #define VIRTIO_SECTOR_SIZE 512
228 #define VIRTIO_ISO_BLOCK_SIZE 2048
229 #define VIRTIO_SCSI_BLOCK_SIZE 512
230 
231 static inline ulong virtio_sector_adjust(ulong sector)
232 {
233     return sector * (virtio_get_block_size() / VIRTIO_SECTOR_SIZE);
234 }
235 
236 struct VirtioScsiConfig {
237     uint32_t num_queues;
238     uint32_t seg_max;
239     uint32_t max_sectors;
240     uint32_t cmd_per_lun;
241     uint32_t event_info_size;
242     uint32_t sense_size;
243     uint32_t cdb_size;
244     uint16_t max_channel;
245     uint16_t max_target;
246     uint32_t max_lun;
247 } __attribute__((packed));
248 typedef struct VirtioScsiConfig VirtioScsiConfig;
249 
250 struct ScsiDevice {
251     uint16_t channel;   /* Always 0 in QEMU     */
252     uint16_t target;    /* will be scanned over */
253     uint32_t lun;       /* will be reported     */
254 };
255 typedef struct ScsiDevice ScsiDevice;
256 
257 struct VDev {
258     int nr_vqs;
259     VRing *vrings;
260     int cmd_vr_idx;
261     void *ring_area;
262     long wait_reply_timeout;
263     VirtioGDN guessed_disk_nature;
264     SubChannelId schid;
265     SenseId senseid;
266     union {
267         VirtioBlkConfig blk;
268         VirtioScsiConfig scsi;
269     } config;
270     ScsiDevice *scsi_device;
271     bool is_cdrom;
272     int scsi_block_size;
273     int blk_factor;
274     uint64_t scsi_last_block;
275     uint32_t scsi_dev_cyls;
276     uint8_t scsi_dev_heads;
277 };
278 typedef struct VDev VDev;
279 
280 VDev *virtio_get_device(void);
281 VirtioDevType virtio_get_device_type(void);
282 
283 struct VirtioCmd {
284     void *data;
285     int size;
286     int flags;
287 };
288 typedef struct VirtioCmd VirtioCmd;
289 
290 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
291 
292 #endif /* VIRTIO_H */
293