4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
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
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
27 VIRTIO_ID_CONSOLE
= 3,
28 VIRTIO_ID_BALLOON
= 5,
31 typedef enum VirtioDevType VirtioDevType
;
38 } __attribute__((packed
));
39 typedef struct VqInfo VqInfo
;
44 } __attribute__((packed
));
45 typedef struct VqConfig VqConfig
;
47 #define VIRTIO_RING_SIZE (PAGE_SIZE * 8)
48 #define VIRTIO_MAX_VQS 3
49 #define KVM_S390_VIRTIO_RING_ALIGN 4096
51 #define VRING_USED_F_NO_NOTIFY 1
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
60 /* Internal flag to mark follow-up segments as such */
61 #define VRING_HIDDEN_IS_CHAIN 256
63 /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
65 /* Address (guest-physical). */
69 /* The flags as indicated above. */
71 /* We chain unused descriptors via this, too */
73 } __attribute__((packed
));
74 typedef struct VRingDesc VRingDesc
;
80 } __attribute__((packed
));
81 typedef struct VRingAvail VRingAvail
;
83 /* uint32_t is used here for ids for padding reasons. */
84 struct VRingUsedElem
{
85 /* Index of start of used descriptor chain. */
87 /* Total length of the descriptor chain which was used (written to) */
89 } __attribute__((packed
));
90 typedef struct VRingUsedElem VRingUsedElem
;
96 } __attribute__((packed
));
97 typedef struct VRingUsed VRingUsed
;
110 typedef struct VRing VRing
;
113 /***********************************************
115 ***********************************************/
120 * Usage is a bit tricky as some bits are used as flags and some are not.
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.
128 /* These two define direction. */
129 #define VIRTIO_BLK_T_IN 0
130 #define VIRTIO_BLK_T_OUT 1
132 /* This bit says it's a scsi command, not an actual read or write. */
133 #define VIRTIO_BLK_T_SCSI_CMD 2
135 /* Cache flush command */
136 #define VIRTIO_BLK_T_FLUSH 4
138 /* Barrier before this op. */
139 #define VIRTIO_BLK_T_BARRIER 0x80000000
141 /* This is the first element of the read scatter-gather list. */
142 struct VirtioBlkOuthdr
{
147 /* Sector (ie. 512 byte offset) */
150 typedef struct VirtioBlkOuthdr VirtioBlkOuthdr
;
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) */
157 struct VirtioBlkGeometry
{
161 } geometry
; /* (if VIRTIO_BLK_F_GEOMETRY) */
163 uint32_t blk_size
; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
165 /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
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
170 uint32_t opt_io_size
; /* optimal sustained I/O size in logical blks */
172 uint8_t wce
; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
173 } __attribute__((packed
));
174 typedef struct VirtioBlkConfig VirtioBlkConfig
;
176 enum guessed_disk_nature_type
{
179 VIRTIO_GDN_CDROM
= 2,
182 typedef enum guessed_disk_nature_type VirtioGDN
;
184 VirtioGDN
virtio_guessed_disk_nature(void);
185 void virtio_assume_eckd(void);
186 void virtio_assume_iso9660(void);
188 bool virtio_ipl_disk_is_valid(void);
189 int virtio_get_block_size(void);
190 uint8_t virtio_get_heads(void);
191 uint8_t virtio_get_sectors(void);
192 uint64_t virtio_get_blocks(void);
193 int virtio_read_many(unsigned long sector
, void *load_addr
, int sec_num
);
195 #define VIRTIO_SECTOR_SIZE 512
196 #define VIRTIO_ISO_BLOCK_SIZE 2048
197 #define VIRTIO_SCSI_BLOCK_SIZE 512
198 #define VIRTIO_DASD_DEFAULT_BLOCK_SIZE 4096
200 static inline unsigned long virtio_sector_adjust(unsigned long sector
)
202 return sector
* (virtio_get_block_size() / VIRTIO_SECTOR_SIZE
);
205 struct VirtioScsiConfig
{
208 uint32_t max_sectors
;
209 uint32_t cmd_per_lun
;
210 uint32_t event_info_size
;
213 uint16_t max_channel
;
216 } __attribute__((packed
));
217 typedef struct VirtioScsiConfig VirtioScsiConfig
;
220 uint16_t channel
; /* Always 0 in QEMU */
221 uint16_t target
; /* will be scanned over */
222 uint32_t lun
; /* will be reported */
224 typedef struct ScsiDevice ScsiDevice
;
226 struct VirtioNetConfig
{
228 /* uint16_t status; */ /* Only with VIRTIO_NET_F_STATUS */
229 /* uint16_t max_virtqueue_pairs; */ /* Only with VIRTIO_NET_F_MQ */
231 typedef struct VirtioNetConfig VirtioNetConfig
;
238 long wait_reply_timeout
;
239 VirtioGDN guessed_disk_nature
;
244 VirtioScsiConfig scsi
;
247 ScsiDevice
*scsi_device
;
251 uint64_t scsi_last_block
;
252 uint32_t scsi_dev_cyls
;
253 uint8_t scsi_dev_heads
;
254 bool scsi_device_selected
;
255 ScsiDevice selected_scsi_device
;
256 uint64_t netboot_start_addr
;
257 uint32_t max_transfer
;
258 uint32_t guest_features
[2];
260 typedef struct VDev VDev
;
262 VDev
*virtio_get_device(void);
263 VirtioDevType
virtio_get_device_type(void);
270 typedef struct VirtioCmd VirtioCmd
;
272 bool vring_notify(VRing
*vr
);
273 int drain_irqs(SubChannelId schid
);
274 void vring_send_buf(VRing
*vr
, void *p
, int len
, int flags
);
275 int vr_poll(VRing
*vr
);
276 int vring_wait_reply(void);
277 int virtio_run(VDev
*vdev
, int vqid
, VirtioCmd
*cmd
);
278 void virtio_setup_ccw(VDev
*vdev
);
280 int virtio_net_init(void *mac_addr
);
282 #endif /* VIRTIO_H */