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