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.
|
|
|
|
*/
|
|
|
|
|
2024-10-20 04:29:36 +03:00
|
|
|
#include <string.h>
|
2013-04-22 23:01:00 +04:00
|
|
|
#include "s390-ccw.h"
|
2019-04-04 17:34:24 +03:00
|
|
|
#include "cio.h"
|
2013-04-22 23:01:00 +04:00
|
|
|
#include "virtio.h"
|
2015-11-10 17:37:22 +03:00
|
|
|
#include "virtio-scsi.h"
|
2017-07-12 15:49:49 +03:00
|
|
|
#include "bswap.h"
|
2019-04-04 17:34:31 +03:00
|
|
|
#include "helper.h"
|
2020-06-24 10:52:16 +03:00
|
|
|
#include "s390-time.h"
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2018-03-29 12:37:31 +03:00
|
|
|
#define VRING_WAIT_REPLY_TIMEOUT 30
|
2015-10-26 17:47:24 +03:00
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
static VRing block[VIRTIO_MAX_VQS];
|
|
|
|
static char ring_area[VIRTIO_RING_SIZE * VIRTIO_MAX_VQS]
|
|
|
|
__attribute__((__aligned__(PAGE_SIZE)));
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
static VDev vdev = {
|
|
|
|
.nr_vqs = 1,
|
|
|
|
.vrings = block,
|
|
|
|
.cmd_vr_idx = 0,
|
|
|
|
.ring_area = ring_area,
|
|
|
|
.wait_reply_timeout = VRING_WAIT_REPLY_TIMEOUT,
|
|
|
|
.schid = { .one = 1 },
|
2015-11-10 17:37:22 +03:00
|
|
|
.scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE,
|
|
|
|
.blk_factor = 1,
|
2015-10-26 17:47:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
VDev *virtio_get_device(void)
|
|
|
|
{
|
|
|
|
return &vdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtioDevType virtio_get_device_type(void)
|
|
|
|
{
|
|
|
|
return vdev.senseid.cu_model;
|
|
|
|
}
|
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
/* virtio spec v1.0 para 4.3.3.2 */
|
2013-04-22 23:01:00 +04:00
|
|
|
static long kvm_hypercall(unsigned long nr, unsigned long param1,
|
2015-11-10 17:13:36 +03:00
|
|
|
unsigned long param2, unsigned long param3)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2023-05-10 17:39:25 +03:00
|
|
|
register unsigned long r_nr asm("1") = nr;
|
|
|
|
register unsigned long r_param1 asm("2") = param1;
|
|
|
|
register unsigned long r_param2 asm("3") = param2;
|
|
|
|
register unsigned long r_param3 asm("4") = param3;
|
2014-05-19 22:05:40 +04:00
|
|
|
register long retval asm("2");
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2021-05-12 20:15:48 +03:00
|
|
|
asm volatile ("diag %%r2,%%r4,0x500"
|
2014-05-19 22:05:40 +04:00
|
|
|
: "=d" (retval)
|
2015-11-10 17:13:36 +03:00
|
|
|
: "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
|
2014-05-19 22:05:40 +04:00
|
|
|
: "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
|
|
|
}
|
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-11-10 17:13:36 +03:00
|
|
|
return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
|
|
|
|
vq_idx, cookie);
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* Virtio functions *
|
|
|
|
***********************************************/
|
|
|
|
|
2017-07-12 15:49:45 +03:00
|
|
|
int drain_irqs(SubChannelId schid)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-11-10 16:10:20 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:34:31 +03:00
|
|
|
static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-11-10 16:10:20 +03:00
|
|
|
Ccw1 ccw = {};
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
ccw.cmd_code = cmd;
|
|
|
|
ccw.cda = (long)ptr;
|
|
|
|
ccw.count = len;
|
|
|
|
|
2019-04-04 17:34:31 +03:00
|
|
|
if (sli) {
|
|
|
|
ccw.flags |= CCW_FLAG_SLI;
|
2013-04-26 06:12:54 +04:00
|
|
|
}
|
2019-04-04 17:34:31 +03:00
|
|
|
|
|
|
|
return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
static void vring_init(VRing *vr, VqInfo *info)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-11-10 17:13:36 +03:00
|
|
|
void *p = (void *) info->queue;
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
debug_print_addr("init p", p);
|
2015-11-10 17:13:36 +03:00
|
|
|
vr->id = info->index;
|
|
|
|
vr->num = info->num;
|
2013-04-22 23:01:00 +04:00
|
|
|
vr->desc = p;
|
2015-11-10 17:13:36 +03:00
|
|
|
vr->avail = p + info->num * sizeof(VRingDesc);
|
|
|
|
vr->used = (void *)(((unsigned long)&vr->avail->ring[info->num]
|
|
|
|
+ info->align - 1) & ~(info->align - 1));
|
2013-04-22 23:01:00 +04:00
|
|
|
|
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;
|
2015-11-10 17:13:36 +03:00
|
|
|
vr->cookie = 0;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
|
|
|
debug_print_addr("init vr", vr);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:49:45 +03:00
|
|
|
bool vring_notify(VRing *vr)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-11-10 17:13:36 +03:00
|
|
|
vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
|
|
|
|
return vr->cookie >= 0;
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:49:45 +03:00
|
|
|
void vring_send_buf(VRing *vr, void *p, int len, int flags)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:39:25 +03:00
|
|
|
vr->desc[vr->next_idx].addr = (unsigned long)p;
|
2013-04-22 23:01:00 +04:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:49:45 +03:00
|
|
|
int vr_poll(VRing *vr)
|
2015-11-10 17:13:36 +03:00
|
|
|
{
|
|
|
|
if (vr->used->idx == vr->used_idx) {
|
|
|
|
vring_notify(vr);
|
|
|
|
yield();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
vr->used_idx = vr->used->idx;
|
|
|
|
vr->next_idx = 0;
|
|
|
|
vr->desc[0].len = 0;
|
|
|
|
vr->desc[0].flags = 0;
|
|
|
|
return 1; /* vr has been updated */
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:01:00 +04:00
|
|
|
/*
|
|
|
|
* Wait for the host to reply.
|
|
|
|
*
|
|
|
|
* timeout is in seconds if > 0.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, 1 on timeout.
|
|
|
|
*/
|
2017-07-12 15:49:45 +03:00
|
|
|
int vring_wait_reply(void)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2023-05-10 17:39:25 +03:00
|
|
|
unsigned long target_second = get_time_seconds() + vdev.wait_reply_timeout;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
/* Wait for any queue to be updated by the host */
|
|
|
|
do {
|
|
|
|
int i, r = 0;
|
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
for (i = 0; i < vdev.nr_vqs; i++) {
|
|
|
|
r += vr_poll(&vdev.vrings[i]);
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
yield();
|
2015-11-10 17:13:36 +03:00
|
|
|
if (r) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-06-24 10:52:16 +03:00
|
|
|
} while (!vdev.wait_reply_timeout || (get_time_seconds() < target_second));
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-11-10 17:13:36 +03:00
|
|
|
return 1;
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
|
|
|
|
2015-10-28 13:12:13 +03:00
|
|
|
int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
|
|
|
|
{
|
|
|
|
VRing *vr = &vdev->vrings[vqid];
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
vring_send_buf(vr, cmd[i].data, cmd[i].size,
|
|
|
|
cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
|
|
|
|
} while (cmd[i++].flags & VRING_DESC_F_NEXT);
|
|
|
|
|
|
|
|
vring_wait_reply();
|
|
|
|
if (drain_irqs(vr->schid)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
int virtio_setup_ccw(VDev *vdev)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2024-10-20 04:29:46 +03:00
|
|
|
int i, cfg_size = 0;
|
2022-07-04 14:18:57 +03:00
|
|
|
uint8_t status;
|
2017-07-12 15:49:49 +03:00
|
|
|
struct VirtioFeatureDesc {
|
|
|
|
uint32_t features;
|
|
|
|
uint8_t index;
|
|
|
|
} __attribute__((packed)) feats;
|
2015-10-26 17:47:24 +03:00
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
if (!virtio_is_supported(vdev->schid)) {
|
|
|
|
puts("Virtio unsupported for this device ID");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2015-11-10 17:37:22 +03:00
|
|
|
/* device ID has been established now */
|
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
|
2015-10-26 18:55:16 +03:00
|
|
|
vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2019-04-04 17:34:31 +03:00
|
|
|
run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
|
2014-05-19 22:10:27 +04:00
|
|
|
|
2022-07-04 14:18:57 +03:00
|
|
|
status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
|
|
|
|
puts("Could not write ACKNOWLEDGE status to host");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2022-07-04 14:18:57 +03:00
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
switch (vdev->senseid.cu_model) {
|
2017-07-12 15:49:52 +03:00
|
|
|
case VIRTIO_ID_NET:
|
|
|
|
vdev->nr_vqs = 2;
|
|
|
|
vdev->cmd_vr_idx = 0;
|
|
|
|
cfg_size = sizeof(vdev->config.net);
|
|
|
|
break;
|
2015-10-26 17:47:24 +03:00
|
|
|
case VIRTIO_ID_BLOCK:
|
|
|
|
vdev->nr_vqs = 1;
|
|
|
|
vdev->cmd_vr_idx = 0;
|
|
|
|
cfg_size = sizeof(vdev->config.blk);
|
|
|
|
break;
|
2015-11-10 17:37:22 +03:00
|
|
|
case VIRTIO_ID_SCSI:
|
|
|
|
vdev->nr_vqs = 3;
|
|
|
|
vdev->cmd_vr_idx = VR_REQUEST;
|
|
|
|
cfg_size = sizeof(vdev->config.scsi);
|
|
|
|
break;
|
2015-10-26 17:47:24 +03:00
|
|
|
default:
|
2024-10-20 04:29:46 +03:00
|
|
|
puts("Unsupported virtio device");
|
|
|
|
return -ENODEV;
|
2015-10-26 17:47:24 +03:00
|
|
|
}
|
2022-07-04 14:18:57 +03:00
|
|
|
|
|
|
|
status |= VIRTIO_CONFIG_S_DRIVER;
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
|
|
|
|
puts("Could not write DRIVER status to host");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2022-07-04 14:18:57 +03:00
|
|
|
|
2017-07-12 15:49:49 +03:00
|
|
|
/* Feature negotiation */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
|
|
|
|
feats.features = 0;
|
|
|
|
feats.index = i;
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
|
|
|
|
puts("Could not get features bits");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:49:49 +03:00
|
|
|
vdev->guest_features[i] &= bswap32(feats.features);
|
|
|
|
feats.features = bswap32(vdev->guest_features[i]);
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
|
|
|
|
puts("Could not set features bits");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-07-12 15:49:49 +03:00
|
|
|
}
|
2014-05-19 22:10:27 +04:00
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
|
|
|
|
puts("Could not get virtio device configuration");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2022-07-04 14:18:58 +03:00
|
|
|
|
2015-10-26 17:47:24 +03:00
|
|
|
for (i = 0; i < vdev->nr_vqs; i++) {
|
2015-11-10 17:13:36 +03:00
|
|
|
VqInfo info = {
|
|
|
|
.queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
|
|
|
|
.align = KVM_S390_VIRTIO_RING_ALIGN,
|
|
|
|
.index = i,
|
|
|
|
.num = 0,
|
|
|
|
};
|
|
|
|
VqConfig config = {
|
|
|
|
.index = i,
|
|
|
|
.num = 0,
|
|
|
|
};
|
|
|
|
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
|
|
|
|
false)) {
|
|
|
|
puts("Could not get virtio device VQ config");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-11-10 17:13:36 +03:00
|
|
|
info.num = config.num;
|
2015-10-26 17:47:24 +03:00
|
|
|
vring_init(&vdev->vrings[i], &info);
|
|
|
|
vdev->vrings[i].schid = vdev->schid;
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
|
|
|
|
puts("Cannot set VQ info");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2013-04-26 06:12:55 +04:00
|
|
|
}
|
2022-07-04 14:18:57 +03:00
|
|
|
|
|
|
|
status |= VIRTIO_CONFIG_S_DRIVER_OK;
|
2024-10-20 04:29:46 +03:00
|
|
|
if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
|
|
|
|
puts("Could not write DRIVER_OK status to host");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-11-10 17:13:36 +03:00
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
|
2015-10-26 18:55:16 +03:00
|
|
|
bool virtio_is_supported(SubChannelId schid)
|
2013-04-22 23:01:00 +04:00
|
|
|
{
|
2015-10-26 17:47:24 +03:00
|
|
|
vdev.schid = schid;
|
|
|
|
memset(&vdev.senseid, 0, sizeof(vdev.senseid));
|
2019-04-04 17:34:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Run sense id command.
|
|
|
|
* The size of the senseid data differs between devices (notably,
|
|
|
|
* between virtio devices and dasds), so specify the largest possible
|
|
|
|
* size and suppress the incorrect length indication for smaller sizes.
|
|
|
|
*/
|
|
|
|
if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
|
|
|
|
true)) {
|
2013-04-22 23:01:00 +04:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-26 17:47:24 +03:00
|
|
|
if (vdev.senseid.cu_type == 0x3832) {
|
|
|
|
switch (vdev.senseid.cu_model) {
|
|
|
|
case VIRTIO_ID_BLOCK:
|
2015-11-10 17:37:22 +03:00
|
|
|
case VIRTIO_ID_SCSI:
|
2016-11-02 00:34:00 +03:00
|
|
|
case VIRTIO_ID_NET:
|
2015-10-26 17:47:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|
2015-10-26 17:47:24 +03:00
|
|
|
return false;
|
2013-04-22 23:01:00 +04:00
|
|
|
}
|