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