| 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 | /* 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 | /* Feature negotiation complete */ |
| 22 | #define VIRTIO_CONFIG_S_FEATURES_OK 8 |
| 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 | long cookie; |
| 109 | int id; |
| 110 | uint16_t pci_notify; |
| 111 | }; |
| 112 | typedef struct VRing VRing; |
| 113 | |
| 114 | char *virtio_get_ring_area(int ring_num); |
| 115 | |
| 116 | /*********************************************** |
| 117 | * Virtio block * |
| 118 | ***********************************************/ |
| 119 | |
| 120 | /* |
| 121 | * Command types |
| 122 | * |
| 123 | * Usage is a bit tricky as some bits are used as flags and some are not. |
| 124 | * |
| 125 | * Rules: |
| 126 | * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or |
| 127 | * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own |
| 128 | * and may not be combined with any of the other flags. |
| 129 | */ |
| 130 | |
| 131 | /* These two define direction. */ |
| 132 | #define VIRTIO_BLK_T_IN 0 |
| 133 | #define VIRTIO_BLK_T_OUT 1 |
| 134 | |
| 135 | /* This bit says it's a scsi command, not an actual read or write. */ |
| 136 | #define VIRTIO_BLK_T_SCSI_CMD 2 |
| 137 | |
| 138 | /* Cache flush command */ |
| 139 | #define VIRTIO_BLK_T_FLUSH 4 |
| 140 | |
| 141 | /* Barrier before this op. */ |
| 142 | #define VIRTIO_BLK_T_BARRIER 0x80000000 |
| 143 | |
| 144 | /* This is the first element of the read scatter-gather list. */ |
| 145 | struct VirtioBlkOuthdr { |
| 146 | /* VIRTIO_BLK_T* */ |
| 147 | uint32_t type; |
| 148 | /* io priority. */ |
| 149 | uint32_t ioprio; |
| 150 | /* Sector (ie. 512 byte offset) */ |
| 151 | uint64_t sector; |
| 152 | }; |
| 153 | typedef struct VirtioBlkOuthdr VirtioBlkOuthdr; |
| 154 | |
| 155 | struct VirtioBlkConfig { |
| 156 | uint64_t capacity; /* in 512-byte sectors */ |
| 157 | uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */ |
| 158 | uint32_t seg_max; /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */ |
| 159 | |
| 160 | struct VirtioBlkGeometry { |
| 161 | uint16_t cylinders; |
| 162 | uint8_t heads; |
| 163 | uint8_t sectors; |
| 164 | } geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */ |
| 165 | |
| 166 | uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ |
| 167 | |
| 168 | /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */ |
| 169 | uint8_t physical_block_exp; /* exponent for physical blk per logical blk */ |
| 170 | uint8_t alignment_offset; /* alignment offset in logical blocks */ |
| 171 | uint16_t min_io_size; /* min I/O size without performance penalty |
| 172 | in logical blocks */ |
| 173 | uint32_t opt_io_size; /* optimal sustained I/O size in logical blks */ |
| 174 | |
| 175 | uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ |
| 176 | } __attribute__((packed)); |
| 177 | typedef struct VirtioBlkConfig VirtioBlkConfig; |
| 178 | |
| 179 | enum guessed_disk_nature_type { |
| 180 | VIRTIO_GDN_NONE = 0, |
| 181 | VIRTIO_GDN_DASD = 1, |
| 182 | VIRTIO_GDN_CDROM = 2, |
| 183 | VIRTIO_GDN_SCSI = 3, |
| 184 | }; |
| 185 | typedef enum guessed_disk_nature_type VirtioGDN; |
| 186 | |
| 187 | VirtioGDN virtio_guessed_disk_nature(void); |
| 188 | void virtio_assume_eckd(void); |
| 189 | void virtio_assume_iso9660(void); |
| 190 | |
| 191 | bool virtio_ipl_disk_is_valid(void); |
| 192 | int virtio_get_block_size(void); |
| 193 | uint8_t virtio_get_heads(void); |
| 194 | uint8_t virtio_get_sectors(void); |
| 195 | uint64_t virtio_get_blocks(void); |
| 196 | int virtio_read_many(unsigned long sector, void *load_addr, int sec_num); |
| 197 | |
| 198 | #define VIRTIO_SECTOR_SIZE 512 |
| 199 | #define VIRTIO_ISO_BLOCK_SIZE 2048 |
| 200 | #define VIRTIO_SCSI_BLOCK_SIZE 512 |
| 201 | #define VIRTIO_DASD_DEFAULT_BLOCK_SIZE 4096 |
| 202 | |
| 203 | static inline unsigned long virtio_sector_adjust(unsigned long sector) |
| 204 | { |
| 205 | return sector * (virtio_get_block_size() / VIRTIO_SECTOR_SIZE); |
| 206 | } |
| 207 | |
| 208 | struct VirtioScsiConfig { |
| 209 | uint32_t num_queues; |
| 210 | uint32_t seg_max; |
| 211 | uint32_t max_sectors; |
| 212 | uint32_t cmd_per_lun; |
| 213 | uint32_t event_info_size; |
| 214 | uint32_t sense_size; |
| 215 | uint32_t cdb_size; |
| 216 | uint16_t max_channel; |
| 217 | uint16_t max_target; |
| 218 | uint32_t max_lun; |
| 219 | } __attribute__((packed)); |
| 220 | typedef struct VirtioScsiConfig VirtioScsiConfig; |
| 221 | |
| 222 | struct ScsiDevice { |
| 223 | uint16_t channel; /* Always 0 in QEMU */ |
| 224 | uint16_t target; /* will be scanned over */ |
| 225 | uint32_t lun; /* will be reported */ |
| 226 | }; |
| 227 | typedef struct ScsiDevice ScsiDevice; |
| 228 | |
| 229 | struct VirtioNetConfig { |
| 230 | uint8_t mac[6]; |
| 231 | /* uint16_t status; */ /* Only with VIRTIO_NET_F_STATUS */ |
| 232 | /* uint16_t max_virtqueue_pairs; */ /* Only with VIRTIO_NET_F_MQ */ |
| 233 | }; |
| 234 | typedef struct VirtioNetConfig VirtioNetConfig; |
| 235 | |
| 236 | struct VDev { |
| 237 | int nr_vqs; |
| 238 | VRing *vrings; |
| 239 | int cmd_vr_idx; |
| 240 | void *ring_area; |
| 241 | long wait_reply_timeout; |
| 242 | VirtioGDN guessed_disk_nature; |
| 243 | SubChannelId schid; |
| 244 | SenseId senseid; |
| 245 | S390IplType ipl_type; |
| 246 | VirtioDevType dev_type; |
| 247 | union { |
| 248 | VirtioBlkConfig blk; |
| 249 | VirtioScsiConfig scsi; |
| 250 | VirtioNetConfig net; |
| 251 | } config; |
| 252 | ScsiDevice *scsi_device; |
| 253 | bool is_cdrom; |
| 254 | int scsi_block_size; |
| 255 | int blk_factor; |
| 256 | uint64_t scsi_last_block; |
| 257 | uint32_t scsi_dev_cyls; |
| 258 | uint8_t scsi_dev_heads; |
| 259 | bool scsi_device_selected; |
| 260 | ScsiDevice selected_scsi_device; |
| 261 | uint32_t pci_fh; |
| 262 | uint16_t vendor_id; |
| 263 | uint32_t max_transfer; |
| 264 | uint32_t guest_features[2]; |
| 265 | }; |
| 266 | typedef struct VDev VDev; |
| 267 | |
| 268 | VDev *virtio_get_device(void); |
| 269 | VirtioDevType virtio_get_device_type(void); |
| 270 | |
| 271 | struct VirtioCmd { |
| 272 | void *data; |
| 273 | int size; |
| 274 | int flags; |
| 275 | }; |
| 276 | typedef struct VirtioCmd VirtioCmd; |
| 277 | |
| 278 | bool be_ipl(void); |
| 279 | void vring_init(VRing *vr, VqInfo *info); |
| 280 | bool virtio_is_supported(VDev *vdev); |
| 281 | bool vring_notify(VRing *vr); |
| 282 | int drain_irqs(void); |
| 283 | void vring_send_buf(VRing *vr, void *p, int len, int flags); |
| 284 | int vr_poll(VRing *vr); |
| 285 | int vring_wait_reply(void); |
| 286 | int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd); |
| 287 | int virtio_reset(VDev *vdev); |
| 288 | int virtio_setup_ccw(VDev *vdev); |
| 289 | |
| 290 | /* virtio-net.c */ |
| 291 | int virtio_net_init(void *mac_addr); |
| 292 | void virtio_net_deinit(void); |
| 293 | |
| 294 | /* virtio-blkdev.c */ |
| 295 | int virtio_blk_setup_device(VDev *vdev); |
| 296 | int virtio_read(unsigned long sector, void *load_addr); |
| 297 | unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2, |
| 298 | void *load_addr); |
| 299 | |
| 300 | #endif /* VIRTIO_H */ |