2013-04-22 23:01:00 +04:00
|
|
|
/*
|
|
|
|
* Virtio driver bits
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Alexander Graf <agraf@suse.de>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or (at
|
|
|
|
* your option) any later version. See the COPYING file in the top-level
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VIRTIO_H
|
|
|
|
#define VIRTIO_H
|
|
|
|
|
|
|
|
/* Status byte for guest to report progress, and synchronize features. */
|
|
|
|
/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
|
|
|
|
#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
|
|
|
|
/* We have found a driver for the device. */
|
|
|
|
#define VIRTIO_CONFIG_S_DRIVER 2
|
|
|
|
/* Driver has used its parts of the config, and is happy */
|
|
|
|
#define VIRTIO_CONFIG_S_DRIVER_OK 4
|
|
|
|
/* We've given up on this device. */
|
|
|
|
#define VIRTIO_CONFIG_S_FAILED 0x80
|
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
enum VirtioDevType {
|
2013-04-22 23:01:00 +04:00
|
|
|
VIRTIO_ID_NET = 1,
|
|
|
|
VIRTIO_ID_BLOCK = 2,
|
|
|
|
VIRTIO_ID_CONSOLE = 3,
|
|
|
|
VIRTIO_ID_BALLOON = 5,
|
2015-11-10 17:37:22 +03:00
|
|
|
VIRTIO_ID_SCSI = 8,
|
2013-04-22 23:01:00 +04:00
|
|
|
};
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef enum VirtioDevType VirtioDevType;
|
|
|
|
|
|
|
|
struct VqInfo {
|
|
|
|
uint64_t queue;
|
|
|
|
uint32_t align;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t num;
|
2013-04-22 23:01:00 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VqInfo VqInfo;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VqConfig {
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t num;
|
2013-04-26 06:12:55 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VqConfig VqConfig;
|
2013-04-26 06:12:55 +04:00
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
#define VIRTIO_RING_SIZE (PAGE_SIZE * 8)
|
|
|
|
#define VIRTIO_MAX_VQS 3
|
2014-05-19 22:05:40 +04:00
|
|
|
#define KVM_S390_VIRTIO_RING_ALIGN 4096
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
#define VRING_USED_F_NO_NOTIFY 1
|
|
|
|
|
|
|
|
/* This marks a buffer as continuing via the next field. */
|
|
|
|
#define VRING_DESC_F_NEXT 1
|
|
|
|
/* This marks a buffer as write-only (otherwise read-only). */
|
|
|
|
#define VRING_DESC_F_WRITE 2
|
|
|
|
/* This means the buffer contains a list of buffer descriptors. */
|
|
|
|
#define VRING_DESC_F_INDIRECT 4
|
|
|
|
|
|
|
|
/* Internal flag to mark follow-up segments as such */
|
|
|
|
#define VRING_HIDDEN_IS_CHAIN 256
|
|
|
|
|
|
|
|
/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VRingDesc {
|
2013-04-22 23:01:00 +04:00
|
|
|
/* Address (guest-physical). */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint64_t addr;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* Length. */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t len;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* The flags as indicated above. */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint16_t flags;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* We chain unused descriptors via this, too */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint16_t next;
|
2013-04-22 23:01:00 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VRingDesc VRingDesc;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VRingAvail {
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t idx;
|
|
|
|
uint16_t ring[];
|
2013-04-22 23:01:00 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VRingAvail VRingAvail;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
/* uint32_t is used here for ids for padding reasons. */
|
|
|
|
struct VRingUsedElem {
|
2013-04-22 23:01:00 +04:00
|
|
|
/* Index of start of used descriptor chain. */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t id;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* Total length of the descriptor chain which was used (written to) */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t len;
|
2013-04-22 23:01:00 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VRingUsedElem VRingUsedElem;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VRingUsed {
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t idx;
|
|
|
|
VRingUsedElem ring[];
|
2013-04-22 23:01:00 +04:00
|
|
|
} __attribute__((packed));
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VRingUsed VRingUsed;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VRing {
|
2013-04-22 23:01:00 +04:00
|
|
|
unsigned int num;
|
|
|
|
int next_idx;
|
2013-08-29 19:52:43 +04:00
|
|
|
int used_idx;
|
2015-11-10 16:10:20 +03:00
|
|
|
VRingDesc *desc;
|
|
|
|
VRingAvail *avail;
|
|
|
|
VRingUsed *used;
|
|
|
|
SubChannelId schid;
|
2015-11-10 17:13:36 +03:00
|
|
|
long cookie;
|
|
|
|
int id;
|
2013-04-22 23:01:00 +04:00
|
|
|
};
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VRing VRing;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Virtio block *
|
|
|
|
***********************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command types
|
|
|
|
*
|
|
|
|
* Usage is a bit tricky as some bits are used as flags and some are not.
|
|
|
|
*
|
|
|
|
* Rules:
|
|
|
|
* VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
|
|
|
|
* VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
|
|
|
|
* and may not be combined with any of the other flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* These two define direction. */
|
|
|
|
#define VIRTIO_BLK_T_IN 0
|
|
|
|
#define VIRTIO_BLK_T_OUT 1
|
|
|
|
|
|
|
|
/* This bit says it's a scsi command, not an actual read or write. */
|
|
|
|
#define VIRTIO_BLK_T_SCSI_CMD 2
|
|
|
|
|
|
|
|
/* Cache flush command */
|
|
|
|
#define VIRTIO_BLK_T_FLUSH 4
|
|
|
|
|
|
|
|
/* Barrier before this op. */
|
|
|
|
#define VIRTIO_BLK_T_BARRIER 0x80000000
|
|
|
|
|
|
|
|
/* This is the first element of the read scatter-gather list. */
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VirtioBlkOuthdr {
|
2013-04-22 23:01:00 +04:00
|
|
|
/* VIRTIO_BLK_T* */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t type;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* io priority. */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t ioprio;
|
2013-04-22 23:01:00 +04:00
|
|
|
/* Sector (ie. 512 byte offset) */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint64_t sector;
|
2013-04-22 23:01:00 +04:00
|
|
|
};
|
2015-11-10 16:10:20 +03:00
|
|
|
typedef struct VirtioBlkOuthdr VirtioBlkOuthdr;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VirtioBlkConfig {
|
|
|
|
uint64_t capacity; /* in 512-byte sectors */
|
|
|
|
uint32_t size_max; /* max segment size (if VIRTIO_BLK_F_SIZE_MAX) */
|
|
|
|
uint32_t seg_max; /* max number of segments (if VIRTIO_BLK_F_SEG_MAX) */
|
2014-05-19 22:10:27 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
struct VirtioBlkGeometry {
|
|
|
|
uint16_t cylinders;
|
|
|
|
uint8_t heads;
|
|
|
|
uint8_t sectors;
|
2014-05-19 22:10:27 +04:00
|
|
|
} geometry; /* (if VIRTIO_BLK_F_GEOMETRY) */
|
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t blk_size; /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
|
2014-05-19 22:10:27 +04:00
|
|
|
|
|
|
|
/* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint8_t physical_block_exp; /* exponent for physical blk per logical blk */
|
|
|
|
uint8_t alignment_offset; /* alignment offset in logical blocks */
|
|
|
|
uint16_t min_io_size; /* min I/O size without performance penalty
|
2014-05-19 22:10:27 +04:00
|
|
|
in logical blocks */
|
2015-11-10 16:10:20 +03:00
|
|
|
uint32_t opt_io_size; /* optimal sustained I/O size in logical blks */
|
2014-05-19 22:10:27 +04:00
|
|
|
|
2015-11-10 16:10:20 +03:00
|
|
|
uint8_t wce; /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
|
|
|
|
} __attribute__((packed));
|
|
|
|
typedef struct VirtioBlkConfig VirtioBlkConfig;
|
2014-05-19 22:10:27 +04:00
|
|
|
|
2015-10-26 18:55:16 +03:00
|
|
|
enum guessed_disk_nature_type {
|
|
|
|
VIRTIO_GDN_NONE = 0,
|
|
|
|
VIRTIO_GDN_DASD = 1,
|
|
|
|
VIRTIO_GDN_CDROM = 2,
|
|
|
|
VIRTIO_GDN_SCSI = 3,
|
|
|
|
};
|
|
|
|
typedef enum guessed_disk_nature_type VirtioGDN;
|
|
|
|
|
|
|
|
VirtioGDN virtio_guessed_disk_nature(void);
|
2014-05-19 22:10:27 +04:00
|
|
|
void virtio_assume_scsi(void);
|
|
|
|
void virtio_assume_eckd(void);
|
2015-10-12 18:50:20 +03:00
|
|
|
void virtio_assume_iso9660(void);
|
2014-05-19 22:10:27 +04:00
|
|
|
|
|
|
|
extern bool virtio_disk_is_scsi(void);
|
|
|
|
extern bool virtio_disk_is_eckd(void);
|
|
|
|
extern bool virtio_ipl_disk_is_valid(void);
|
|
|
|
extern int virtio_get_block_size(void);
|
|
|
|
extern uint8_t virtio_get_heads(void);
|
|
|
|
extern uint8_t virtio_get_sectors(void);
|
2014-08-29 13:01:39 +04:00
|
|
|
extern uint64_t virtio_get_blocks(void);
|
2014-05-19 22:10:27 +04:00
|
|
|
extern int virtio_read_many(ulong sector, void *load_addr, int sec_num);
|
|
|
|
|
|
|
|
#define VIRTIO_SECTOR_SIZE 512
|
2015-11-10 17:37:22 +03:00
|
|
|
#define VIRTIO_ISO_BLOCK_SIZE 2048
|
|
|
|
#define VIRTIO_SCSI_BLOCK_SIZE 512
|
2014-05-19 22:10:27 +04:00
|
|
|
|
|
|
|
static inline ulong virtio_sector_adjust(ulong sector)
|
|
|
|
{
|
2015-10-12 18:50:20 +03:00
|
|
|
return sector * (virtio_get_block_size() / VIRTIO_SECTOR_SIZE);
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
2015-11-10 17:37:22 +03:00
|
|
|
struct VirtioScsiConfig {
|
|
|
|
uint32_t num_queues;
|
|
|
|
uint32_t seg_max;
|
|
|
|
uint32_t max_sectors;
|
|
|
|
uint32_t cmd_per_lun;
|
|
|
|
uint32_t event_info_size;
|
|
|
|
uint32_t sense_size;
|
|
|
|
uint32_t cdb_size;
|
|
|
|
uint16_t max_channel;
|
|
|
|
uint16_t max_target;
|
|
|
|
uint32_t max_lun;
|
|
|
|
} __attribute__((packed));
|
|
|
|
typedef struct VirtioScsiConfig VirtioScsiConfig;
|
|
|
|
|
|
|
|
struct ScsiDevice {
|
|
|
|
uint16_t channel; /* Always 0 in QEMU */
|
|
|
|
uint16_t target; /* will be scanned over */
|
|
|
|
uint32_t lun; /* will be reported */
|
|
|
|
};
|
|
|
|
typedef struct ScsiDevice ScsiDevice;
|
|
|
|
|
2017-07-12 15:49:52 +03:00
|
|
|
struct VirtioNetConfig {
|
|
|
|
uint8_t mac[6];
|
|
|
|
/* uint16_t status; */ /* Only with VIRTIO_NET_F_STATUS */
|
|
|
|
/* uint16_t max_virtqueue_pairs; */ /* Only with VIRTIO_NET_F_MQ */
|
|
|
|
};
|
|
|
|
typedef struct VirtioNetConfig VirtioNetConfig;
|
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
struct VDev {
|
|
|
|
int nr_vqs;
|
|
|
|
VRing *vrings;
|
|
|
|
int cmd_vr_idx;
|
|
|
|
void *ring_area;
|
|
|
|
long wait_reply_timeout;
|
2015-10-26 18:55:16 +03:00
|
|
|
VirtioGDN guessed_disk_nature;
|
2015-10-26 17:47:24 +03:00
|
|
|
SubChannelId schid;
|
|
|
|
SenseId senseid;
|
|
|
|
union {
|
|
|
|
VirtioBlkConfig blk;
|
2015-11-10 17:37:22 +03:00
|
|
|
VirtioScsiConfig scsi;
|
2017-07-12 15:49:52 +03:00
|
|
|
VirtioNetConfig net;
|
2015-10-26 17:47:24 +03:00
|
|
|
} config;
|
2015-11-10 17:37:22 +03:00
|
|
|
ScsiDevice *scsi_device;
|
|
|
|
bool is_cdrom;
|
|
|
|
int scsi_block_size;
|
|
|
|
int blk_factor;
|
|
|
|
uint64_t scsi_last_block;
|
|
|
|
uint32_t scsi_dev_cyls;
|
|
|
|
uint8_t scsi_dev_heads;
|
2016-06-01 16:25:51 +03:00
|
|
|
bool scsi_device_selected;
|
|
|
|
ScsiDevice selected_scsi_device;
|
2016-11-02 00:34:00 +03:00
|
|
|
uint64_t netboot_start_addr;
|
pc-bios/s390-ccw: Get Block Limits VPD device data
The "Block Limits" Inquiry VPD page is optional for any SCSI device,
but if it's supported it provides a hint of the maximum I/O transfer
length for this particular device. If this page is supported by the
disk, let's issue that Inquiry and use the minimum of it and the
SCSI controller limit. That will cover this scenario:
qemu-system-s390x ...
-device virtio-scsi-ccw,id=scsi0,max_sectors=32768 ...
-drive file=/dev/sda,if=none,id=drive0,format=raw ...
-device scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,
drive=drive0,id=disk0,max_io_size=1048576
controller: 32768 sectors x 512 bytes/sector = 16777216 bytes
disk: 1048576 bytes
Now that we have a limit for a virtio-scsi disk, compare that with the
limit for the virtio-scsi controller when we actually build the I/O.
The minimum of these two limits should be the one we use.
Signed-off-by: Eric Farman <farman@linux.vnet.ibm.com>
Message-Id: <20170510155359.32727-7-farman@linux.vnet.ibm.com>
Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
2017-05-10 18:53:57 +03:00
|
|
|
uint32_t max_transfer;
|
2017-07-12 15:49:49 +03:00
|
|
|
uint32_t guest_features[2];
|
2015-10-26 17:47:24 +03:00
|
|
|
};
|
|
|
|
typedef struct VDev VDev;
|
|
|
|
|
|
|
|
VDev *virtio_get_device(void);
|
|
|
|
VirtioDevType virtio_get_device_type(void);
|
|
|
|
|
2015-10-28 13:12:13 +03:00
|
|
|
struct VirtioCmd {
|
|
|
|
void *data;
|
|
|
|
int size;
|
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
typedef struct VirtioCmd VirtioCmd;
|
|
|
|
|
2017-07-12 15:49:45 +03:00
|
|
|
bool vring_notify(VRing *vr);
|
|
|
|
int drain_irqs(SubChannelId schid);
|
|
|
|
void vring_send_buf(VRing *vr, void *p, int len, int flags);
|
|
|
|
int vr_poll(VRing *vr);
|
|
|
|
int vring_wait_reply(void);
|
2015-10-28 13:12:13 +03:00
|
|
|
int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
|
2017-07-12 15:49:45 +03:00
|
|
|
void virtio_setup_ccw(VDev *vdev);
|
2015-10-28 13:12:13 +03:00
|
|
|
|
2017-07-12 15:49:52 +03:00
|
|
|
int virtio_net_init(void *mac_addr);
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
#endif /* VIRTIO_H */
|