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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "s390-ccw.h"
|
|
|
|
#include "virtio.h"
|
|
|
|
|
|
|
|
struct vring block;
|
|
|
|
|
2013-06-17 16:29:42 +04:00
|
|
|
static char chsc_page[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
static long kvm_hypercall(unsigned long nr, unsigned long param1,
|
|
|
|
unsigned long param2)
|
|
|
|
{
|
2014-05-19 22:05:40 +04:00
|
|
|
register ulong r_nr asm("1") = nr;
|
|
|
|
register ulong r_param1 asm("2") = param1;
|
|
|
|
register ulong r_param2 asm("3") = param2;
|
|
|
|
register long retval asm("2");
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2014-05-19 22:05:40 +04:00
|
|
|
asm volatile ("diag 2,4,0x500"
|
|
|
|
: "=d" (retval)
|
|
|
|
: "d" (r_nr), "0" (r_param1), "r"(r_param2)
|
|
|
|
: "memory", "cc");
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2014-05-19 22:05:40 +04:00
|
|
|
return retval;
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_notify(struct subchannel_id schid)
|
|
|
|
{
|
2014-05-19 22:05:40 +04:00
|
|
|
kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid, 0);
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Virtio functions *
|
|
|
|
***********************************************/
|
|
|
|
|
2013-04-26 06:12:53 +04:00
|
|
|
static int drain_irqs(struct subchannel_id schid)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
|
|
|
struct irb irb = {};
|
2013-04-26 06:12:53 +04:00
|
|
|
int r = 0;
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
while (1) {
|
2013-04-26 06:12:53 +04:00
|
|
|
/* FIXME: make use of TPI, for that enable subchannel and isc */
|
2013-04-22 23:01:00 +04:00
|
|
|
if (tsch(schid, &irb)) {
|
2013-04-26 06:12:53 +04:00
|
|
|
/* Might want to differentiate error codes later on. */
|
|
|
|
if (irb.scsw.cstat) {
|
|
|
|
r = -EIO;
|
|
|
|
} else if (irb.scsw.dstat != 0xc) {
|
|
|
|
r = -EIO;
|
|
|
|
}
|
|
|
|
return r;
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_ccw(struct subchannel_id schid, int cmd, void *ptr, int len)
|
|
|
|
{
|
|
|
|
struct ccw1 ccw = {};
|
|
|
|
struct cmd_orb orb = {};
|
|
|
|
struct schib schib;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
/* start command processing */
|
|
|
|
stsch_err(schid, &schib);
|
|
|
|
schib.scsw.ctrl = SCSW_FCTL_START_FUNC;
|
|
|
|
msch(schid, &schib);
|
|
|
|
|
|
|
|
/* start subchannel command */
|
|
|
|
orb.fmt = 1;
|
|
|
|
orb.cpa = (u32)(long)&ccw;
|
|
|
|
orb.lpm = 0x80;
|
|
|
|
|
|
|
|
ccw.cmd_code = cmd;
|
|
|
|
ccw.cda = (long)ptr;
|
|
|
|
ccw.count = len;
|
|
|
|
|
|
|
|
r = ssch(schid, &orb);
|
|
|
|
/*
|
|
|
|
* XXX Wait until device is done processing the CCW. For now we can
|
|
|
|
* assume that a simple tsch will have finished the CCW processing,
|
|
|
|
* but the architecture allows for asynchronous operation
|
|
|
|
*/
|
2013-04-26 06:12:54 +04:00
|
|
|
if (!r) {
|
|
|
|
r = drain_irqs(schid);
|
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_set_status(struct subchannel_id schid,
|
|
|
|
unsigned long dev_addr)
|
|
|
|
{
|
|
|
|
unsigned char status = dev_addr;
|
2013-04-26 06:12:54 +04:00
|
|
|
if (run_ccw(schid, CCW_CMD_WRITE_STATUS, &status, sizeof(status))) {
|
|
|
|
virtio_panic("Could not write status to host!\n");
|
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_reset(struct subchannel_id schid)
|
|
|
|
{
|
|
|
|
run_ccw(schid, CCW_CMD_VDEV_RESET, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vring_init(struct vring *vr, unsigned int num, void *p,
|
|
|
|
unsigned long align)
|
|
|
|
{
|
|
|
|
debug_print_addr("init p", p);
|
|
|
|
vr->num = num;
|
|
|
|
vr->desc = p;
|
|
|
|
vr->avail = p + num*sizeof(struct vring_desc);
|
|
|
|
vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
|
|
|
|
& ~(align - 1));
|
|
|
|
|
2013-05-23 15:51:41 +04:00
|
|
|
/* Zero out all relevant field */
|
|
|
|
vr->avail->flags = 0;
|
|
|
|
vr->avail->idx = 0;
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
/* We're running with interrupts off anyways, so don't bother */
|
|
|
|
vr->used->flags = VRING_USED_F_NO_NOTIFY;
|
2013-05-23 15:51:41 +04:00
|
|
|
vr->used->idx = 0;
|
2013-08-29 19:52:43 +04:00
|
|
|
vr->used_idx = 0;
|
2014-02-12 19:17:35 +04:00
|
|
|
vr->next_idx = 0;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
debug_print_addr("init vr", vr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vring_notify(struct subchannel_id schid)
|
|
|
|
{
|
|
|
|
virtio_notify(schid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vring_send_buf(struct vring *vr, void *p, int len, int flags)
|
|
|
|
{
|
|
|
|
/* For follow-up chains we need to keep the first entry point */
|
|
|
|
if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
|
|
|
|
vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
vr->desc[vr->next_idx].addr = (ulong)p;
|
|
|
|
vr->desc[vr->next_idx].len = len;
|
|
|
|
vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
|
2013-04-23 05:23:04 +04:00
|
|
|
vr->desc[vr->next_idx].next = vr->next_idx;
|
|
|
|
vr->desc[vr->next_idx].next++;
|
|
|
|
vr->next_idx++;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
/* Chains only have a single ID */
|
|
|
|
if (!(flags & VRING_DESC_F_NEXT)) {
|
|
|
|
vr->avail->idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 get_clock(void)
|
|
|
|
{
|
|
|
|
u64 r;
|
|
|
|
|
|
|
|
asm volatile("stck %0" : "=Q" (r) : : "cc");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ulong get_second(void)
|
|
|
|
{
|
|
|
|
return (get_clock() >> 12) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for the host to reply.
|
|
|
|
*
|
|
|
|
* timeout is in seconds if > 0.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, 1 on timeout.
|
|
|
|
*/
|
|
|
|
static int vring_wait_reply(struct vring *vr, int timeout)
|
|
|
|
{
|
|
|
|
ulong target_second = get_second() + timeout;
|
|
|
|
struct subchannel_id schid = vr->schid;
|
|
|
|
int r = 0;
|
|
|
|
|
2013-08-29 19:52:43 +04:00
|
|
|
/* Wait until the used index has moved. */
|
|
|
|
while (vr->used->idx == vr->used_idx) {
|
2013-04-22 23:01:00 +04:00
|
|
|
vring_notify(schid);
|
|
|
|
if (timeout && (get_second() >= target_second)) {
|
|
|
|
r = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2013-08-29 19:52:43 +04:00
|
|
|
vr->used_idx = vr->used->idx;
|
2013-04-22 23:01:00 +04:00
|
|
|
vr->next_idx = 0;
|
|
|
|
vr->desc[0].len = 0;
|
|
|
|
vr->desc[0].flags = 0;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Virtio block *
|
|
|
|
***********************************************/
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
int virtio_read_many(ulong sector, void *load_addr, int sec_num)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
|
|
|
struct virtio_blk_outhdr out_hdr;
|
|
|
|
u8 status;
|
2013-04-26 06:12:54 +04:00
|
|
|
int r;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
/* Tell the host we want to read */
|
|
|
|
out_hdr.type = VIRTIO_BLK_T_IN;
|
|
|
|
out_hdr.ioprio = 99;
|
2014-05-19 22:10:27 +04:00
|
|
|
out_hdr.sector = virtio_sector_adjust(sector);
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
vring_send_buf(&block, &out_hdr, sizeof(out_hdr), VRING_DESC_F_NEXT);
|
|
|
|
|
|
|
|
/* This is where we want to receive data */
|
2014-05-19 22:10:27 +04:00
|
|
|
vring_send_buf(&block, load_addr, virtio_get_block_size() * sec_num,
|
2013-04-22 23:01:00 +04:00
|
|
|
VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN |
|
|
|
|
VRING_DESC_F_NEXT);
|
|
|
|
|
|
|
|
/* status field */
|
|
|
|
vring_send_buf(&block, &status, sizeof(u8), VRING_DESC_F_WRITE |
|
|
|
|
VRING_HIDDEN_IS_CHAIN);
|
|
|
|
|
|
|
|
/* Now we can tell the host to read */
|
|
|
|
vring_wait_reply(&block, 0);
|
|
|
|
|
2013-04-26 06:12:54 +04:00
|
|
|
r = drain_irqs(block.schid);
|
|
|
|
if (r) {
|
|
|
|
/* Well, whatever status is supposed to contain... */
|
|
|
|
status = 1;
|
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
|
2014-05-19 22:05:40 +04:00
|
|
|
ulong subchan_id, void *load_addr)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
|
|
|
u8 status;
|
|
|
|
int sec = rec_list1;
|
2014-06-18 16:16:44 +04:00
|
|
|
int sec_num = ((rec_list2 >> 32) & 0xffff) + 1;
|
2013-04-22 23:01:00 +04:00
|
|
|
int sec_len = rec_list2 >> 48;
|
|
|
|
ulong addr = (ulong)load_addr;
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
if (sec_len != virtio_get_block_size()) {
|
2013-04-22 23:01:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sclp_print(".");
|
2014-05-19 22:05:40 +04:00
|
|
|
status = virtio_read_many(sec, (void *)addr, sec_num);
|
2013-04-22 23:01:00 +04:00
|
|
|
if (status) {
|
|
|
|
virtio_panic("I/O Error");
|
|
|
|
}
|
2014-05-19 22:10:27 +04:00
|
|
|
addr += sec_num * virtio_get_block_size();
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int virtio_read(ulong sector, void *load_addr)
|
|
|
|
{
|
|
|
|
return virtio_read_many(sector, load_addr, 1);
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
static VirtioBlkConfig blk_cfg = {};
|
|
|
|
static bool guessed_disk_nature;
|
|
|
|
|
|
|
|
bool virtio_guessed_disk_nature(void)
|
|
|
|
{
|
|
|
|
return guessed_disk_nature;
|
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_assume_scsi(void)
|
|
|
|
{
|
|
|
|
guessed_disk_nature = true;
|
|
|
|
blk_cfg.blk_size = 512;
|
2014-08-29 13:01:36 +04:00
|
|
|
blk_cfg.physical_block_exp = 0;
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtio_assume_eckd(void)
|
|
|
|
{
|
|
|
|
guessed_disk_nature = true;
|
|
|
|
blk_cfg.blk_size = 4096;
|
2014-08-29 13:01:36 +04:00
|
|
|
blk_cfg.physical_block_exp = 0;
|
2014-05-19 22:10:27 +04:00
|
|
|
|
|
|
|
/* this must be here to calculate code segment position */
|
|
|
|
blk_cfg.geometry.heads = 15;
|
|
|
|
blk_cfg.geometry.sectors = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool virtio_disk_is_scsi(void)
|
|
|
|
{
|
|
|
|
if (guessed_disk_nature) {
|
2014-08-29 13:01:36 +04:00
|
|
|
return (virtio_get_block_size() == 512);
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
return (blk_cfg.geometry.heads == 255)
|
|
|
|
&& (blk_cfg.geometry.sectors == 63)
|
2014-08-29 13:01:36 +04:00
|
|
|
&& (virtio_get_block_size() == 512);
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:37 +04:00
|
|
|
/*
|
|
|
|
* Other supported value pairs, if any, would need to be added here.
|
|
|
|
* Note: head count is always 15.
|
|
|
|
*/
|
|
|
|
static inline u8 virtio_eckd_sectors_for_block_size(int size)
|
|
|
|
{
|
|
|
|
switch (size) {
|
|
|
|
case 512:
|
|
|
|
return 49;
|
|
|
|
case 1024:
|
|
|
|
return 33;
|
|
|
|
case 2048:
|
|
|
|
return 21;
|
|
|
|
case 4096:
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
bool virtio_disk_is_eckd(void)
|
|
|
|
{
|
2014-08-29 13:01:37 +04:00
|
|
|
const int block_size = virtio_get_block_size();
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
if (guessed_disk_nature) {
|
2014-08-29 13:01:37 +04:00
|
|
|
return (block_size == 4096);
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
return (blk_cfg.geometry.heads == 15)
|
2014-08-29 13:01:37 +04:00
|
|
|
&& (blk_cfg.geometry.sectors ==
|
|
|
|
virtio_eckd_sectors_for_block_size(block_size));
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool virtio_ipl_disk_is_valid(void)
|
|
|
|
{
|
2014-08-29 13:01:36 +04:00
|
|
|
return virtio_disk_is_scsi() || virtio_disk_is_eckd();
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int virtio_get_block_size(void)
|
|
|
|
{
|
2014-08-29 13:01:36 +04:00
|
|
|
return blk_cfg.blk_size << blk_cfg.physical_block_exp;
|
2014-05-19 22:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t virtio_get_heads(void)
|
|
|
|
{
|
|
|
|
return blk_cfg.geometry.heads;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t virtio_get_sectors(void)
|
|
|
|
{
|
|
|
|
return blk_cfg.geometry.sectors;
|
|
|
|
}
|
|
|
|
|
2014-08-29 13:01:39 +04:00
|
|
|
uint64_t virtio_get_blocks(void)
|
|
|
|
{
|
|
|
|
return blk_cfg.capacity /
|
|
|
|
(virtio_get_block_size() / VIRTIO_SECTOR_SIZE);
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
void virtio_setup_block(struct subchannel_id schid)
|
|
|
|
{
|
|
|
|
struct vq_info_block info;
|
2013-04-26 06:12:55 +04:00
|
|
|
struct vq_config_block config = {};
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
blk_cfg.blk_size = 0; /* mark "illegal" - setup started... */
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
virtio_reset(schid);
|
|
|
|
|
2014-05-19 22:10:27 +04:00
|
|
|
/*
|
|
|
|
* Skipping CCW_CMD_READ_FEAT. We're not doing anything fancy, and
|
|
|
|
* we'll just stop dead anyway if anything does not work like we
|
|
|
|
* expect it.
|
|
|
|
*/
|
|
|
|
|
2013-04-26 06:12:55 +04:00
|
|
|
config.index = 0;
|
|
|
|
if (run_ccw(schid, CCW_CMD_READ_VQ_CONF, &config, sizeof(config))) {
|
2014-05-19 22:10:27 +04:00
|
|
|
virtio_panic("Could not get block device VQ configuration\n");
|
|
|
|
}
|
|
|
|
if (run_ccw(schid, CCW_CMD_READ_CONF, &blk_cfg, sizeof(blk_cfg))) {
|
2013-04-26 06:12:55 +04:00
|
|
|
virtio_panic("Could not get block device configuration\n");
|
|
|
|
}
|
2014-05-19 22:05:40 +04:00
|
|
|
vring_init(&block, config.num, (void *)(100 * 1024 * 1024),
|
2013-04-22 23:01:00 +04:00
|
|
|
KVM_S390_VIRTIO_RING_ALIGN);
|
|
|
|
|
|
|
|
info.queue = (100ULL * 1024ULL* 1024ULL);
|
|
|
|
info.align = KVM_S390_VIRTIO_RING_ALIGN;
|
|
|
|
info.index = 0;
|
2013-04-26 06:12:55 +04:00
|
|
|
info.num = config.num;
|
2013-04-22 23:01:00 +04:00
|
|
|
block.schid = schid;
|
|
|
|
|
2013-04-26 06:12:54 +04:00
|
|
|
if (!run_ccw(schid, CCW_CMD_SET_VQ, &info, sizeof(info))) {
|
|
|
|
virtio_set_status(schid, VIRTIO_CONFIG_S_DRIVER_OK);
|
|
|
|
}
|
2014-05-19 22:10:27 +04:00
|
|
|
|
|
|
|
if (!virtio_ipl_disk_is_valid()) {
|
|
|
|
/* make sure all getters but blocksize return 0 for invalid IPL disk */
|
|
|
|
memset(&blk_cfg, 0, sizeof(blk_cfg));
|
|
|
|
virtio_assume_scsi();
|
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool virtio_is_blk(struct subchannel_id schid)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct senseid senseid = {};
|
|
|
|
|
|
|
|
/* run sense id command */
|
|
|
|
r = run_ccw(schid, CCW_CMD_SENSE_ID, &senseid, sizeof(senseid));
|
|
|
|
if (r) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((senseid.cu_type != 0x3832) || (senseid.cu_model != VIRTIO_ID_BLOCK)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-17 16:29:42 +04:00
|
|
|
int enable_mss_facility(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct chsc_area_sda *sda_area = (struct chsc_area_sda *) chsc_page;
|
|
|
|
|
|
|
|
memset(sda_area, 0, PAGE_SIZE);
|
|
|
|
sda_area->request.length = 0x0400;
|
|
|
|
sda_area->request.code = 0x0031;
|
|
|
|
sda_area->operation_code = 0x2;
|
|
|
|
|
|
|
|
ret = chsc(sda_area);
|
|
|
|
if ((ret == 0) && (sda_area->response.code == 0x0001)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|