qemu/hw/s390x/virtio-ccw.c
<<
>>
Prefs
   1/*
   2 * virtio ccw target implementation
   3 *
   4 * Copyright 2012,2015 IBM Corp.
   5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
   6 *            Pierre Morel <pmorel@linux.vnet.ibm.com>
   7 *
   8 * This work is licensed under the terms of the GNU GPL, version 2 or (at
   9 * your option) any later version. See the COPYING file in the top-level
  10 * directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "qapi/error.h"
  15#include "hw/hw.h"
  16#include "sysemu/block-backend.h"
  17#include "sysemu/blockdev.h"
  18#include "sysemu/sysemu.h"
  19#include "sysemu/kvm.h"
  20#include "net/net.h"
  21#include "hw/virtio/virtio.h"
  22#include "hw/virtio/virtio-serial.h"
  23#include "hw/virtio/virtio-net.h"
  24#include "hw/sysbus.h"
  25#include "qemu/bitops.h"
  26#include "qemu/error-report.h"
  27#include "hw/virtio/virtio-access.h"
  28#include "hw/virtio/virtio-bus.h"
  29#include "hw/s390x/adapter.h"
  30#include "hw/s390x/s390_flic.h"
  31
  32#include "hw/s390x/ioinst.h"
  33#include "hw/s390x/css.h"
  34#include "virtio-ccw.h"
  35#include "trace.h"
  36#include "hw/s390x/css-bridge.h"
  37#include "hw/s390x/s390-virtio-ccw.h"
  38
  39#define NR_CLASSIC_INDICATOR_BITS 64
  40
  41static int virtio_ccw_dev_post_load(void *opaque, int version_id)
  42{
  43    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(opaque);
  44    CcwDevice *ccw_dev = CCW_DEVICE(dev);
  45    CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
  46
  47    ccw_dev->sch->driver_data = dev;
  48    if (ccw_dev->sch->thinint_active) {
  49        dev->routes.adapter.adapter_id = css_get_adapter_id(
  50                                         CSS_IO_ADAPTER_VIRTIO,
  51                                         dev->thinint_isc);
  52    }
  53    /* Re-fill subch_id after loading the subchannel states.*/
  54    if (ck->refill_ids) {
  55        ck->refill_ids(ccw_dev);
  56    }
  57    return 0;
  58}
  59
  60typedef struct VirtioCcwDeviceTmp {
  61    VirtioCcwDevice *parent;
  62    uint16_t config_vector;
  63} VirtioCcwDeviceTmp;
  64
  65static int virtio_ccw_dev_tmp_pre_save(void *opaque)
  66{
  67    VirtioCcwDeviceTmp *tmp = opaque;
  68    VirtioCcwDevice *dev = tmp->parent;
  69    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
  70
  71    tmp->config_vector = vdev->config_vector;
  72
  73    return 0;
  74}
  75
  76static int virtio_ccw_dev_tmp_post_load(void *opaque, int version_id)
  77{
  78    VirtioCcwDeviceTmp *tmp = opaque;
  79    VirtioCcwDevice *dev = tmp->parent;
  80    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
  81
  82    vdev->config_vector = tmp->config_vector;
  83    return 0;
  84}
  85
  86const VMStateDescription vmstate_virtio_ccw_dev_tmp = {
  87    .name = "s390_virtio_ccw_dev_tmp",
  88    .pre_save = virtio_ccw_dev_tmp_pre_save,
  89    .post_load = virtio_ccw_dev_tmp_post_load,
  90    .fields = (VMStateField[]) {
  91        VMSTATE_UINT16(config_vector, VirtioCcwDeviceTmp),
  92        VMSTATE_END_OF_LIST()
  93    }
  94};
  95
  96const VMStateDescription vmstate_virtio_ccw_dev = {
  97    .name = "s390_virtio_ccw_dev",
  98    .version_id = 1,
  99    .minimum_version_id = 1,
 100    .post_load = virtio_ccw_dev_post_load,
 101    .fields = (VMStateField[]) {
 102        VMSTATE_CCW_DEVICE(parent_obj, VirtioCcwDevice),
 103        VMSTATE_PTR_TO_IND_ADDR(indicators, VirtioCcwDevice),
 104        VMSTATE_PTR_TO_IND_ADDR(indicators2, VirtioCcwDevice),
 105        VMSTATE_PTR_TO_IND_ADDR(summary_indicator, VirtioCcwDevice),
 106        /*
 107         * Ugly hack because VirtIODevice does not migrate itself.
 108         * This also makes legacy via vmstate_save_state possible.
 109         */
 110        VMSTATE_WITH_TMP(VirtioCcwDevice, VirtioCcwDeviceTmp,
 111                         vmstate_virtio_ccw_dev_tmp),
 112        VMSTATE_STRUCT(routes, VirtioCcwDevice, 1, vmstate_adapter_routes,
 113                       AdapterRoutes),
 114        VMSTATE_UINT8(thinint_isc, VirtioCcwDevice),
 115        VMSTATE_INT32(revision, VirtioCcwDevice),
 116        VMSTATE_END_OF_LIST()
 117    }
 118};
 119
 120static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
 121                               VirtioCcwDevice *dev);
 122
 123VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
 124{
 125    VirtIODevice *vdev = NULL;
 126    VirtioCcwDevice *dev = sch->driver_data;
 127
 128    if (dev) {
 129        vdev = virtio_bus_get_device(&dev->bus);
 130    }
 131    return vdev;
 132}
 133
 134static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
 135{
 136    virtio_bus_start_ioeventfd(&dev->bus);
 137}
 138
 139static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
 140{
 141    virtio_bus_stop_ioeventfd(&dev->bus);
 142}
 143
 144static bool virtio_ccw_ioeventfd_enabled(DeviceState *d)
 145{
 146    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
 147
 148    return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0;
 149}
 150
 151static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
 152                                       int n, bool assign)
 153{
 154    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
 155    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 156    SubchDev *sch = ccw_dev->sch;
 157    uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;
 158
 159    return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
 160}
 161
 162/* Communication blocks used by several channel commands. */
 163typedef struct VqInfoBlockLegacy {
 164    uint64_t queue;
 165    uint32_t align;
 166    uint16_t index;
 167    uint16_t num;
 168} QEMU_PACKED VqInfoBlockLegacy;
 169
 170typedef struct VqInfoBlock {
 171    uint64_t desc;
 172    uint32_t res0;
 173    uint16_t index;
 174    uint16_t num;
 175    uint64_t avail;
 176    uint64_t used;
 177} QEMU_PACKED VqInfoBlock;
 178
 179typedef struct VqConfigBlock {
 180    uint16_t index;
 181    uint16_t num_max;
 182} QEMU_PACKED VqConfigBlock;
 183
 184typedef struct VirtioFeatDesc {
 185    uint32_t features;
 186    uint8_t index;
 187} QEMU_PACKED VirtioFeatDesc;
 188
 189typedef struct VirtioThinintInfo {
 190    hwaddr summary_indicator;
 191    hwaddr device_indicator;
 192    uint64_t ind_bit;
 193    uint8_t isc;
 194} QEMU_PACKED VirtioThinintInfo;
 195
 196typedef struct VirtioRevInfo {
 197    uint16_t revision;
 198    uint16_t length;
 199    uint8_t data[0];
 200} QEMU_PACKED VirtioRevInfo;
 201
 202/* Specify where the virtqueues for the subchannel are in guest memory. */
 203static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
 204                              VqInfoBlockLegacy *linfo)
 205{
 206    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
 207    uint16_t index = info ? info->index : linfo->index;
 208    uint16_t num = info ? info->num : linfo->num;
 209    uint64_t desc = info ? info->desc : linfo->queue;
 210
 211    if (index >= VIRTIO_QUEUE_MAX) {
 212        return -EINVAL;
 213    }
 214
 215    /* Current code in virtio.c relies on 4K alignment. */
 216    if (linfo && desc && (linfo->align != 4096)) {
 217        return -EINVAL;
 218    }
 219
 220    if (!vdev) {
 221        return -EINVAL;
 222    }
 223
 224    if (info) {
 225        virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
 226    } else {
 227        virtio_queue_set_addr(vdev, index, desc);
 228    }
 229    if (!desc) {
 230        virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
 231    } else {
 232        if (info) {
 233            /* virtio-1 allows changing the ring size. */
 234            if (virtio_queue_get_max_num(vdev, index) < num) {
 235                /* Fail if we exceed the maximum number. */
 236                return -EINVAL;
 237            }
 238            virtio_queue_set_num(vdev, index, num);
 239        } else if (virtio_queue_get_num(vdev, index) > num) {
 240            /* Fail if we don't have a big enough queue. */
 241            return -EINVAL;
 242        }
 243        /* We ignore possible increased num for legacy for compatibility. */
 244        virtio_queue_set_vector(vdev, index, index);
 245    }
 246    /* tell notify handler in case of config change */
 247    vdev->config_vector = VIRTIO_QUEUE_MAX;
 248    return 0;
 249}
 250
 251static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
 252{
 253    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 254
 255    virtio_ccw_stop_ioeventfd(dev);
 256    virtio_reset(vdev);
 257    if (dev->indicators) {
 258        release_indicator(&dev->routes.adapter, dev->indicators);
 259        dev->indicators = NULL;
 260    }
 261    if (dev->indicators2) {
 262        release_indicator(&dev->routes.adapter, dev->indicators2);
 263        dev->indicators2 = NULL;
 264    }
 265    if (dev->summary_indicator) {
 266        release_indicator(&dev->routes.adapter, dev->summary_indicator);
 267        dev->summary_indicator = NULL;
 268    }
 269    ccw_dev->sch->thinint_active = false;
 270}
 271
 272static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
 273                                    bool is_legacy)
 274{
 275    int ret;
 276    VqInfoBlock info;
 277    VqInfoBlockLegacy linfo;
 278    size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info);
 279
 280    if (check_len) {
 281        if (ccw.count != info_len) {
 282            return -EINVAL;
 283        }
 284    } else if (ccw.count < info_len) {
 285        /* Can't execute command. */
 286        return -EINVAL;
 287    }
 288    if (!ccw.cda) {
 289        return -EFAULT;
 290    }
 291    if (is_legacy) {
 292        ccw_dstream_read(&sch->cds, linfo);
 293        be64_to_cpus(&linfo.queue);
 294        be32_to_cpus(&linfo.align);
 295        be16_to_cpus(&linfo.index);
 296        be16_to_cpus(&linfo.num);
 297        ret = virtio_ccw_set_vqs(sch, NULL, &linfo);
 298    } else {
 299        ccw_dstream_read(&sch->cds, info);
 300        be64_to_cpus(&info.desc);
 301        be16_to_cpus(&info.index);
 302        be16_to_cpus(&info.num);
 303        be64_to_cpus(&info.avail);
 304        be64_to_cpus(&info.used);
 305        ret = virtio_ccw_set_vqs(sch, &info, NULL);
 306    }
 307    sch->curr_status.scsw.count = 0;
 308    return ret;
 309}
 310
 311static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
 312{
 313    int ret;
 314    VirtioRevInfo revinfo;
 315    uint8_t status;
 316    VirtioFeatDesc features;
 317    hwaddr indicators;
 318    VqConfigBlock vq_config;
 319    VirtioCcwDevice *dev = sch->driver_data;
 320    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
 321    bool check_len;
 322    int len;
 323    VirtioThinintInfo thinint;
 324
 325    if (!dev) {
 326        return -EINVAL;
 327    }
 328
 329    trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
 330                                   ccw.cmd_code);
 331    check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
 332
 333    if (dev->force_revision_1 && dev->revision < 0 &&
 334        ccw.cmd_code != CCW_CMD_SET_VIRTIO_REV) {
 335        /*
 336         * virtio-1 drivers must start with negotiating to a revision >= 1,
 337         * so post a command reject for all other commands
 338         */
 339        return -ENOSYS;
 340    }
 341
 342    /* Look at the command. */
 343    switch (ccw.cmd_code) {
 344    case CCW_CMD_SET_VQ:
 345        ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
 346        break;
 347    case CCW_CMD_VDEV_RESET:
 348        virtio_ccw_reset_virtio(dev, vdev);
 349        ret = 0;
 350        break;
 351    case CCW_CMD_READ_FEAT:
 352        if (check_len) {
 353            if (ccw.count != sizeof(features)) {
 354                ret = -EINVAL;
 355                break;
 356            }
 357        } else if (ccw.count < sizeof(features)) {
 358            /* Can't execute command. */
 359            ret = -EINVAL;
 360            break;
 361        }
 362        if (!ccw.cda) {
 363            ret = -EFAULT;
 364        } else {
 365            VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
 366
 367            ccw_dstream_advance(&sch->cds, sizeof(features.features));
 368            ccw_dstream_read(&sch->cds, features.index);
 369            if (features.index == 0) {
 370                if (dev->revision >= 1) {
 371                    /* Don't offer legacy features for modern devices. */
 372                    features.features = (uint32_t)
 373                        (vdev->host_features & ~vdc->legacy_features);
 374                } else {
 375                    features.features = (uint32_t)vdev->host_features;
 376                }
 377            } else if ((features.index == 1) && (dev->revision >= 1)) {
 378                /*
 379                 * Only offer feature bits beyond 31 if the guest has
 380                 * negotiated at least revision 1.
 381                 */
 382                features.features = (uint32_t)(vdev->host_features >> 32);
 383            } else {
 384                /* Return zeroes if the guest supports more feature bits. */
 385                features.features = 0;
 386            }
 387            ccw_dstream_rewind(&sch->cds);
 388            cpu_to_le32s(&features.features);
 389            ccw_dstream_write(&sch->cds, features.features);
 390            sch->curr_status.scsw.count = ccw.count - sizeof(features);
 391            ret = 0;
 392        }
 393        break;
 394    case CCW_CMD_WRITE_FEAT:
 395        if (check_len) {
 396            if (ccw.count != sizeof(features)) {
 397                ret = -EINVAL;
 398                break;
 399            }
 400        } else if (ccw.count < sizeof(features)) {
 401            /* Can't execute command. */
 402            ret = -EINVAL;
 403            break;
 404        }
 405        if (!ccw.cda) {
 406            ret = -EFAULT;
 407        } else {
 408            ccw_dstream_read(&sch->cds, features);
 409            le32_to_cpus(&features.features);
 410            if (features.index == 0) {
 411                virtio_set_features(vdev,
 412                                    (vdev->guest_features & 0xffffffff00000000ULL) |
 413                                    features.features);
 414            } else if ((features.index == 1) && (dev->revision >= 1)) {
 415                /*
 416                 * If the guest did not negotiate at least revision 1,
 417                 * we did not offer it any feature bits beyond 31. Such a
 418                 * guest passing us any bit here is therefore buggy.
 419                 */
 420                virtio_set_features(vdev,
 421                                    (vdev->guest_features & 0x00000000ffffffffULL) |
 422                                    ((uint64_t)features.features << 32));
 423            } else {
 424                /*
 425                 * If the guest supports more feature bits, assert that it
 426                 * passes us zeroes for those we don't support.
 427                 */
 428                if (features.features) {
 429                    qemu_log_mask(LOG_GUEST_ERROR,
 430                                  "Guest bug: features[%i]=%x (expected 0)",
 431                                  features.index, features.features);
 432                    /* XXX: do a unit check here? */
 433                }
 434            }
 435            sch->curr_status.scsw.count = ccw.count - sizeof(features);
 436            ret = 0;
 437        }
 438        break;
 439    case CCW_CMD_READ_CONF:
 440        if (check_len) {
 441            if (ccw.count > vdev->config_len) {
 442                ret = -EINVAL;
 443                break;
 444            }
 445        }
 446        len = MIN(ccw.count, vdev->config_len);
 447        if (!ccw.cda) {
 448            ret = -EFAULT;
 449        } else {
 450            virtio_bus_get_vdev_config(&dev->bus, vdev->config);
 451            ccw_dstream_write_buf(&sch->cds, vdev->config, len);
 452            sch->curr_status.scsw.count = ccw.count - len;
 453            ret = 0;
 454        }
 455        break;
 456    case CCW_CMD_WRITE_CONF:
 457        if (check_len) {
 458            if (ccw.count > vdev->config_len) {
 459                ret = -EINVAL;
 460                break;
 461            }
 462        }
 463        len = MIN(ccw.count, vdev->config_len);
 464        if (!ccw.cda) {
 465            ret = -EFAULT;
 466        } else {
 467            ret = ccw_dstream_read_buf(&sch->cds, vdev->config, len);
 468            if (!ret) {
 469                virtio_bus_set_vdev_config(&dev->bus, vdev->config);
 470                sch->curr_status.scsw.count = ccw.count - len;
 471            }
 472        }
 473        break;
 474    case CCW_CMD_READ_STATUS:
 475        if (check_len) {
 476            if (ccw.count != sizeof(status)) {
 477                ret = -EINVAL;
 478                break;
 479            }
 480        } else if (ccw.count < sizeof(status)) {
 481            /* Can't execute command. */
 482            ret = -EINVAL;
 483            break;
 484        }
 485        if (!ccw.cda) {
 486            ret = -EFAULT;
 487        } else {
 488            address_space_stb(&address_space_memory, ccw.cda, vdev->status,
 489                                        MEMTXATTRS_UNSPECIFIED, NULL);
 490            sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status);
 491            ret = 0;
 492        }
 493        break;
 494    case CCW_CMD_WRITE_STATUS:
 495        if (check_len) {
 496            if (ccw.count != sizeof(status)) {
 497                ret = -EINVAL;
 498                break;
 499            }
 500        } else if (ccw.count < sizeof(status)) {
 501            /* Can't execute command. */
 502            ret = -EINVAL;
 503            break;
 504        }
 505        if (!ccw.cda) {
 506            ret = -EFAULT;
 507        } else {
 508            ccw_dstream_read(&sch->cds, status);
 509            if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
 510                virtio_ccw_stop_ioeventfd(dev);
 511            }
 512            if (virtio_set_status(vdev, status) == 0) {
 513                if (vdev->status == 0) {
 514                    virtio_ccw_reset_virtio(dev, vdev);
 515                }
 516                if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
 517                    virtio_ccw_start_ioeventfd(dev);
 518                }
 519                sch->curr_status.scsw.count = ccw.count - sizeof(status);
 520                ret = 0;
 521            } else {
 522                /* Trigger a command reject. */
 523                ret = -ENOSYS;
 524            }
 525        }
 526        break;
 527    case CCW_CMD_SET_IND:
 528        if (check_len) {
 529            if (ccw.count != sizeof(indicators)) {
 530                ret = -EINVAL;
 531                break;
 532            }
 533        } else if (ccw.count < sizeof(indicators)) {
 534            /* Can't execute command. */
 535            ret = -EINVAL;
 536            break;
 537        }
 538        if (sch->thinint_active) {
 539            /* Trigger a command reject. */
 540            ret = -ENOSYS;
 541            break;
 542        }
 543        if (virtio_get_num_queues(vdev) > NR_CLASSIC_INDICATOR_BITS) {
 544            /* More queues than indicator bits --> trigger a reject */
 545            ret = -ENOSYS;
 546            break;
 547        }
 548        if (!ccw.cda) {
 549            ret = -EFAULT;
 550        } else {
 551            ccw_dstream_read(&sch->cds, indicators);
 552            be64_to_cpus(&indicators);
 553            dev->indicators = get_indicator(indicators, sizeof(uint64_t));
 554            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
 555            ret = 0;
 556        }
 557        break;
 558    case CCW_CMD_SET_CONF_IND:
 559        if (check_len) {
 560            if (ccw.count != sizeof(indicators)) {
 561                ret = -EINVAL;
 562                break;
 563            }
 564        } else if (ccw.count < sizeof(indicators)) {
 565            /* Can't execute command. */
 566            ret = -EINVAL;
 567            break;
 568        }
 569        if (!ccw.cda) {
 570            ret = -EFAULT;
 571        } else {
 572            ccw_dstream_read(&sch->cds, indicators);
 573            be64_to_cpus(&indicators);
 574            dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
 575            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
 576            ret = 0;
 577        }
 578        break;
 579    case CCW_CMD_READ_VQ_CONF:
 580        if (check_len) {
 581            if (ccw.count != sizeof(vq_config)) {
 582                ret = -EINVAL;
 583                break;
 584            }
 585        } else if (ccw.count < sizeof(vq_config)) {
 586            /* Can't execute command. */
 587            ret = -EINVAL;
 588            break;
 589        }
 590        if (!ccw.cda) {
 591            ret = -EFAULT;
 592        } else {
 593            ccw_dstream_read(&sch->cds, vq_config.index);
 594            be16_to_cpus(&vq_config.index);
 595            if (vq_config.index >= VIRTIO_QUEUE_MAX) {
 596                ret = -EINVAL;
 597                break;
 598            }
 599            vq_config.num_max = virtio_queue_get_num(vdev,
 600                                                     vq_config.index);
 601            cpu_to_be16s(&vq_config.num_max);
 602            ccw_dstream_write(&sch->cds, vq_config.num_max);
 603            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
 604            ret = 0;
 605        }
 606        break;
 607    case CCW_CMD_SET_IND_ADAPTER:
 608        if (check_len) {
 609            if (ccw.count != sizeof(thinint)) {
 610                ret = -EINVAL;
 611                break;
 612            }
 613        } else if (ccw.count < sizeof(thinint)) {
 614            /* Can't execute command. */
 615            ret = -EINVAL;
 616            break;
 617        }
 618        if (!ccw.cda) {
 619            ret = -EFAULT;
 620        } else if (dev->indicators && !sch->thinint_active) {
 621            /* Trigger a command reject. */
 622            ret = -ENOSYS;
 623        } else {
 624            if (ccw_dstream_read(&sch->cds, thinint)) {
 625                ret = -EFAULT;
 626            } else {
 627                be64_to_cpus(&thinint.ind_bit);
 628                be64_to_cpus(&thinint.summary_indicator);
 629                be64_to_cpus(&thinint.device_indicator);
 630
 631                dev->summary_indicator =
 632                    get_indicator(thinint.summary_indicator, sizeof(uint8_t));
 633                dev->indicators =
 634                    get_indicator(thinint.device_indicator,
 635                                  thinint.ind_bit / 8 + 1);
 636                dev->thinint_isc = thinint.isc;
 637                dev->routes.adapter.ind_offset = thinint.ind_bit;
 638                dev->routes.adapter.summary_offset = 7;
 639                dev->routes.adapter.adapter_id = css_get_adapter_id(
 640                                                 CSS_IO_ADAPTER_VIRTIO,
 641                                                 dev->thinint_isc);
 642                sch->thinint_active = ((dev->indicators != NULL) &&
 643                                       (dev->summary_indicator != NULL));
 644                sch->curr_status.scsw.count = ccw.count - sizeof(thinint);
 645                ret = 0;
 646            }
 647        }
 648        break;
 649    case CCW_CMD_SET_VIRTIO_REV:
 650        len = sizeof(revinfo);
 651        if (ccw.count < len) {
 652            ret = -EINVAL;
 653            break;
 654        }
 655        if (!ccw.cda) {
 656            ret = -EFAULT;
 657            break;
 658        }
 659        ccw_dstream_read_buf(&sch->cds, &revinfo, 4);
 660        be16_to_cpus(&revinfo.revision);
 661        be16_to_cpus(&revinfo.length);
 662        if (ccw.count < len + revinfo.length ||
 663            (check_len && ccw.count > len + revinfo.length)) {
 664            ret = -EINVAL;
 665            break;
 666        }
 667        /*
 668         * Once we start to support revisions with additional data, we'll
 669         * need to fetch it here. Nothing to do for now, though.
 670         */
 671        if (dev->revision >= 0 ||
 672            revinfo.revision > virtio_ccw_rev_max(dev) ||
 673            (dev->force_revision_1 && !revinfo.revision)) {
 674            ret = -ENOSYS;
 675            break;
 676        }
 677        ret = 0;
 678        dev->revision = revinfo.revision;
 679        break;
 680    default:
 681        ret = -ENOSYS;
 682        break;
 683    }
 684    return ret;
 685}
 686
 687static void virtio_sch_disable_cb(SubchDev *sch)
 688{
 689    VirtioCcwDevice *dev = sch->driver_data;
 690
 691    dev->revision = -1;
 692}
 693
 694static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
 695{
 696    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
 697    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 698    CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
 699    DeviceState *parent = DEVICE(ccw_dev);
 700    BusState *qbus = qdev_get_parent_bus(parent);
 701    VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
 702    SubchDev *sch;
 703    Error *err = NULL;
 704
 705    sch = css_create_sch(ccw_dev->devno, cbus->squash_mcss, errp);
 706    if (!sch) {
 707        return;
 708    }
 709    if (!virtio_ccw_rev_max(dev) && dev->force_revision_1) {
 710        error_setg(&err, "Invalid value of property max_rev "
 711                   "(is %d expected >= 1)", virtio_ccw_rev_max(dev));
 712        goto out_err;
 713    }
 714
 715    sch->driver_data = dev;
 716    sch->ccw_cb = virtio_ccw_cb;
 717    sch->disable_cb = virtio_sch_disable_cb;
 718    sch->id.reserved = 0xff;
 719    sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
 720    sch->do_subchannel_work = do_subchannel_work_virtual;
 721    ccw_dev->sch = sch;
 722    dev->indicators = NULL;
 723    dev->revision = -1;
 724    css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
 725
 726    trace_virtio_ccw_new_device(
 727        sch->cssid, sch->ssid, sch->schid, sch->devno,
 728        ccw_dev->devno.valid ? "user-configured" : "auto-configured");
 729
 730    if (kvm_enabled() && !kvm_eventfds_enabled()) {
 731        dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
 732    }
 733
 734    if (k->realize) {
 735        k->realize(dev, &err);
 736        if (err) {
 737            goto out_err;
 738        }
 739    }
 740
 741    ck->realize(ccw_dev, &err);
 742    if (err) {
 743        goto out_err;
 744    }
 745
 746    return;
 747
 748out_err:
 749    error_propagate(errp, err);
 750    css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
 751    ccw_dev->sch = NULL;
 752    g_free(sch);
 753}
 754
 755static void virtio_ccw_unrealize(VirtioCcwDevice *dev, Error **errp)
 756{
 757    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 758    SubchDev *sch = ccw_dev->sch;
 759
 760    if (sch) {
 761        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
 762        g_free(sch);
 763        ccw_dev->sch = NULL;
 764    }
 765    if (dev->indicators) {
 766        release_indicator(&dev->routes.adapter, dev->indicators);
 767        dev->indicators = NULL;
 768    }
 769}
 770
 771static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 772{
 773    DeviceState *qdev = DEVICE(ccw_dev);
 774    VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
 775    DeviceState *vdev = DEVICE(&dev->vdev);
 776
 777    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
 778                                  object_get_typename(OBJECT(qdev)));
 779    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 780    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 781}
 782
 783static void virtio_ccw_net_instance_init(Object *obj)
 784{
 785    VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
 786
 787    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 788                                TYPE_VIRTIO_NET);
 789    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
 790                              "bootindex", &error_abort);
 791}
 792
 793static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 794{
 795    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
 796    DeviceState *vdev = DEVICE(&dev->vdev);
 797
 798    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 799    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 800}
 801
 802static void virtio_ccw_blk_instance_init(Object *obj)
 803{
 804    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
 805
 806    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 807                                TYPE_VIRTIO_BLK);
 808    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
 809                              "bootindex", &error_abort);
 810}
 811
 812static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 813{
 814    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
 815    DeviceState *vdev = DEVICE(&dev->vdev);
 816    DeviceState *proxy = DEVICE(ccw_dev);
 817    char *bus_name;
 818
 819    /*
 820     * For command line compatibility, this sets the virtio-serial-device bus
 821     * name as before.
 822     */
 823    if (proxy->id) {
 824        bus_name = g_strdup_printf("%s.0", proxy->id);
 825        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 826        g_free(bus_name);
 827    }
 828
 829    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 830    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 831}
 832
 833
 834static void virtio_ccw_serial_instance_init(Object *obj)
 835{
 836    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
 837
 838    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 839                                TYPE_VIRTIO_SERIAL);
 840}
 841
 842static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 843{
 844    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
 845    DeviceState *vdev = DEVICE(&dev->vdev);
 846
 847    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 848    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 849}
 850
 851static void virtio_ccw_balloon_instance_init(Object *obj)
 852{
 853    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
 854
 855    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 856                                TYPE_VIRTIO_BALLOON);
 857    object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
 858                              "guest-stats", &error_abort);
 859    object_property_add_alias(obj, "guest-stats-polling-interval",
 860                              OBJECT(&dev->vdev),
 861                              "guest-stats-polling-interval", &error_abort);
 862}
 863
 864static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 865{
 866    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
 867    DeviceState *vdev = DEVICE(&dev->vdev);
 868    DeviceState *qdev = DEVICE(ccw_dev);
 869    char *bus_name;
 870
 871    /*
 872     * For command line compatibility, this sets the virtio-scsi-device bus
 873     * name as before.
 874     */
 875    if (qdev->id) {
 876        bus_name = g_strdup_printf("%s.0", qdev->id);
 877        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 878        g_free(bus_name);
 879    }
 880
 881    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 882    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 883}
 884
 885static void virtio_ccw_scsi_instance_init(Object *obj)
 886{
 887    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
 888
 889    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 890                                TYPE_VIRTIO_SCSI);
 891}
 892
 893#ifdef CONFIG_VHOST_SCSI
 894static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 895{
 896    VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
 897    DeviceState *vdev = DEVICE(&dev->vdev);
 898
 899    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 900    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 901}
 902
 903static void vhost_ccw_scsi_instance_init(Object *obj)
 904{
 905    VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
 906
 907    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 908                                TYPE_VHOST_SCSI);
 909}
 910#endif
 911
 912static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 913{
 914    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
 915    DeviceState *vdev = DEVICE(&dev->vdev);
 916    Error *err = NULL;
 917
 918    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 919    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
 920    if (err) {
 921        error_propagate(errp, err);
 922        return;
 923    }
 924
 925    object_property_set_link(OBJECT(dev),
 926                             OBJECT(dev->vdev.conf.rng), "rng",
 927                             NULL);
 928}
 929
 930static void virtio_ccw_crypto_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 931{
 932    VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(ccw_dev);
 933    DeviceState *vdev = DEVICE(&dev->vdev);
 934    Error *err = NULL;
 935
 936    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 937    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
 938    if (err) {
 939        error_propagate(errp, err);
 940        return;
 941    }
 942
 943    object_property_set_link(OBJECT(vdev),
 944                             OBJECT(dev->vdev.conf.cryptodev), "cryptodev",
 945                             NULL);
 946}
 947
 948static void virtio_ccw_gpu_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 949{
 950    VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(ccw_dev);
 951    DeviceState *vdev = DEVICE(&dev->vdev);
 952
 953    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 954    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 955}
 956
 957static void virtio_ccw_input_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 958{
 959    VirtIOInputCcw *dev = VIRTIO_INPUT_CCW(ccw_dev);
 960    DeviceState *vdev = DEVICE(&dev->vdev);
 961
 962    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 963    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 964}
 965
 966/* DeviceState to VirtioCcwDevice. Note: used on datapath,
 967 * be careful and test performance if you change this.
 968 */
 969static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
 970{
 971    CcwDevice *ccw_dev = to_ccw_dev_fast(d);
 972
 973    return container_of(ccw_dev, VirtioCcwDevice, parent_obj);
 974}
 975
 976static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
 977                                     uint8_t to_be_set)
 978{
 979    uint8_t ind_old, ind_new;
 980    hwaddr len = 1;
 981    uint8_t *ind_addr;
 982
 983    ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
 984    if (!ind_addr) {
 985        error_report("%s(%x.%x.%04x): unable to access indicator",
 986                     __func__, sch->cssid, sch->ssid, sch->schid);
 987        return -1;
 988    }
 989    do {
 990        ind_old = *ind_addr;
 991        ind_new = ind_old | to_be_set;
 992    } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
 993    trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
 994    cpu_physical_memory_unmap(ind_addr, len, 1, len);
 995
 996    return ind_old;
 997}
 998
 999static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
1000{
1001    VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1002    CcwDevice *ccw_dev = to_ccw_dev_fast(d);
1003    SubchDev *sch = ccw_dev->sch;
1004    uint64_t indicators;
1005
1006    /* queue indicators + secondary indicators */
1007    if (vector >= VIRTIO_QUEUE_MAX + 64) {
1008        return;
1009    }
1010
1011    if (vector < VIRTIO_QUEUE_MAX) {
1012        if (!dev->indicators) {
1013            return;
1014        }
1015        if (sch->thinint_active) {
1016            /*
1017             * In the adapter interrupt case, indicators points to a
1018             * memory area that may be (way) larger than 64 bit and
1019             * ind_bit indicates the start of the indicators in a big
1020             * endian notation.
1021             */
1022            uint64_t ind_bit = dev->routes.adapter.ind_offset;
1023
1024            virtio_set_ind_atomic(sch, dev->indicators->addr +
1025                                  (ind_bit + vector) / 8,
1026                                  0x80 >> ((ind_bit + vector) % 8));
1027            if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1028                                       0x01)) {
1029                css_adapter_interrupt(CSS_IO_ADAPTER_VIRTIO, dev->thinint_isc);
1030            }
1031        } else {
1032            indicators = address_space_ldq(&address_space_memory,
1033                                           dev->indicators->addr,
1034                                           MEMTXATTRS_UNSPECIFIED,
1035                                           NULL);
1036            indicators |= 1ULL << vector;
1037            address_space_stq(&address_space_memory, dev->indicators->addr,
1038                              indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1039            css_conditional_io_interrupt(sch);
1040        }
1041    } else {
1042        if (!dev->indicators2) {
1043            return;
1044        }
1045        vector = 0;
1046        indicators = address_space_ldq(&address_space_memory,
1047                                       dev->indicators2->addr,
1048                                       MEMTXATTRS_UNSPECIFIED,
1049                                       NULL);
1050        indicators |= 1ULL << vector;
1051        address_space_stq(&address_space_memory, dev->indicators2->addr,
1052                          indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1053        css_conditional_io_interrupt(sch);
1054    }
1055}
1056
1057static void virtio_ccw_reset(DeviceState *d)
1058{
1059    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1060    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1061    CcwDevice *ccw_dev = CCW_DEVICE(d);
1062
1063    virtio_ccw_reset_virtio(dev, vdev);
1064    css_reset_sch(ccw_dev->sch);
1065}
1066
1067static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1068{
1069    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1070
1071    if (running) {
1072        virtio_ccw_start_ioeventfd(dev);
1073    } else {
1074        virtio_ccw_stop_ioeventfd(dev);
1075    }
1076}
1077
1078static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1079{
1080    CcwDevice *dev = CCW_DEVICE(d);
1081
1082    return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1083}
1084
1085static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1086{
1087    int r;
1088    CcwDevice *ccw_dev = CCW_DEVICE(dev);
1089
1090    if (!ccw_dev->sch->thinint_active) {
1091        return -EINVAL;
1092    }
1093
1094    r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1095    if (r) {
1096        return r;
1097    }
1098    r = map_indicator(&dev->routes.adapter, dev->indicators);
1099    if (r) {
1100        return r;
1101    }
1102    dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1103    dev->routes.adapter.ind_addr = dev->indicators->map;
1104
1105    return 0;
1106}
1107
1108static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1109{
1110    int i;
1111    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1112    int ret;
1113    S390FLICState *fs = s390_get_flic();
1114    S390FLICStateClass *fsc = s390_get_flic_class(fs);
1115
1116    ret = virtio_ccw_get_mappings(dev);
1117    if (ret) {
1118        return ret;
1119    }
1120    for (i = 0; i < nvqs; i++) {
1121        if (!virtio_queue_get_num(vdev, i)) {
1122            break;
1123        }
1124    }
1125    dev->routes.num_routes = i;
1126    return fsc->add_adapter_routes(fs, &dev->routes);
1127}
1128
1129static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1130{
1131    S390FLICState *fs = s390_get_flic();
1132    S390FLICStateClass *fsc = s390_get_flic_class(fs);
1133
1134    fsc->release_adapter_routes(fs, &dev->routes);
1135}
1136
1137static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1138{
1139    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1140    VirtQueue *vq = virtio_get_queue(vdev, n);
1141    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1142
1143    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1144                                              dev->routes.gsi[n]);
1145}
1146
1147static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1148{
1149    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1150    VirtQueue *vq = virtio_get_queue(vdev, n);
1151    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1152    int ret;
1153
1154    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1155                                                dev->routes.gsi[n]);
1156    assert(ret == 0);
1157}
1158
1159static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1160                                         bool assign, bool with_irqfd)
1161{
1162    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1163    VirtQueue *vq = virtio_get_queue(vdev, n);
1164    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1165    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1166
1167    if (assign) {
1168        int r = event_notifier_init(notifier, 0);
1169
1170        if (r < 0) {
1171            return r;
1172        }
1173        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1174        if (with_irqfd) {
1175            r = virtio_ccw_add_irqfd(dev, n);
1176            if (r) {
1177                virtio_queue_set_guest_notifier_fd_handler(vq, false,
1178                                                           with_irqfd);
1179                return r;
1180            }
1181        }
1182        /*
1183         * We do not support individual masking for channel devices, so we
1184         * need to manually trigger any guest masking callbacks here.
1185         */
1186        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1187            k->guest_notifier_mask(vdev, n, false);
1188        }
1189        /* get lost events and re-inject */
1190        if (k->guest_notifier_pending &&
1191            k->guest_notifier_pending(vdev, n)) {
1192            event_notifier_set(notifier);
1193        }
1194    } else {
1195        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1196            k->guest_notifier_mask(vdev, n, true);
1197        }
1198        if (with_irqfd) {
1199            virtio_ccw_remove_irqfd(dev, n);
1200        }
1201        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1202        event_notifier_cleanup(notifier);
1203    }
1204    return 0;
1205}
1206
1207static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1208                                          bool assigned)
1209{
1210    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1211    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1212    CcwDevice *ccw_dev = CCW_DEVICE(d);
1213    bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled();
1214    int r, n;
1215
1216    if (with_irqfd && assigned) {
1217        /* irq routes need to be set up before assigning irqfds */
1218        r = virtio_ccw_setup_irqroutes(dev, nvqs);
1219        if (r < 0) {
1220            goto irqroute_error;
1221        }
1222    }
1223    for (n = 0; n < nvqs; n++) {
1224        if (!virtio_queue_get_num(vdev, n)) {
1225            break;
1226        }
1227        r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1228        if (r < 0) {
1229            goto assign_error;
1230        }
1231    }
1232    if (with_irqfd && !assigned) {
1233        /* release irq routes after irqfds have been released */
1234        virtio_ccw_release_irqroutes(dev, nvqs);
1235    }
1236    return 0;
1237
1238assign_error:
1239    while (--n >= 0) {
1240        virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1241    }
1242irqroute_error:
1243    if (with_irqfd && assigned) {
1244        virtio_ccw_release_irqroutes(dev, nvqs);
1245    }
1246    return r;
1247}
1248
1249static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1250{
1251    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1252    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1253
1254    qemu_put_be16(f, virtio_queue_vector(vdev, n));
1255}
1256
1257static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1258{
1259    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1260    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1261    uint16_t vector;
1262
1263    qemu_get_be16s(f, &vector);
1264    virtio_queue_set_vector(vdev, n , vector);
1265
1266    return 0;
1267}
1268
1269static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1270{
1271    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1272    vmstate_save_state(f, &vmstate_virtio_ccw_dev, dev, NULL);
1273}
1274
1275static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1276{
1277    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1278    return vmstate_load_state(f, &vmstate_virtio_ccw_dev, dev, 1);
1279}
1280
1281static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp)
1282{
1283   VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1284   VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1285
1286    if (dev->max_rev >= 1) {
1287        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1288    }
1289}
1290
1291/* This is called by virtio-bus just after the device is plugged. */
1292static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1293{
1294    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1295    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1296    CcwDevice *ccw_dev = CCW_DEVICE(d);
1297    SubchDev *sch = ccw_dev->sch;
1298    int n = virtio_get_num_queues(vdev);
1299    S390FLICState *flic = s390_get_flic();
1300
1301    if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1302        dev->max_rev = 0;
1303    }
1304
1305    if (virtio_get_num_queues(vdev) > VIRTIO_QUEUE_MAX) {
1306        error_setg(errp, "The number of virtqueues %d "
1307                   "exceeds virtio limit %d", n,
1308                   VIRTIO_QUEUE_MAX);
1309        return;
1310    }
1311    if (virtio_get_num_queues(vdev) > flic->adapter_routes_max_batch) {
1312        error_setg(errp, "The number of virtqueues %d "
1313                   "exceeds flic adapter route limit %d", n,
1314                   flic->adapter_routes_max_batch);
1315        return;
1316    }
1317
1318    sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1319
1320
1321    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1322                          d->hotplugged, 1);
1323}
1324
1325static void virtio_ccw_device_unplugged(DeviceState *d)
1326{
1327    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1328
1329    virtio_ccw_stop_ioeventfd(dev);
1330}
1331/**************** Virtio-ccw Bus Device Descriptions *******************/
1332
1333static Property virtio_ccw_net_properties[] = {
1334    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1335                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1336    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1337                       VIRTIO_CCW_MAX_REV),
1338    DEFINE_PROP_END_OF_LIST(),
1339};
1340
1341static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1342{
1343    DeviceClass *dc = DEVICE_CLASS(klass);
1344    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1345
1346    k->realize = virtio_ccw_net_realize;
1347    k->unrealize = virtio_ccw_unrealize;
1348    dc->reset = virtio_ccw_reset;
1349    dc->props = virtio_ccw_net_properties;
1350    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1351}
1352
1353static const TypeInfo virtio_ccw_net = {
1354    .name          = TYPE_VIRTIO_NET_CCW,
1355    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1356    .instance_size = sizeof(VirtIONetCcw),
1357    .instance_init = virtio_ccw_net_instance_init,
1358    .class_init    = virtio_ccw_net_class_init,
1359};
1360
1361static Property virtio_ccw_blk_properties[] = {
1362    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1363                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1364    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1365                       VIRTIO_CCW_MAX_REV),
1366    DEFINE_PROP_END_OF_LIST(),
1367};
1368
1369static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1370{
1371    DeviceClass *dc = DEVICE_CLASS(klass);
1372    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1373
1374    k->realize = virtio_ccw_blk_realize;
1375    k->unrealize = virtio_ccw_unrealize;
1376    dc->reset = virtio_ccw_reset;
1377    dc->props = virtio_ccw_blk_properties;
1378    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1379}
1380
1381static const TypeInfo virtio_ccw_blk = {
1382    .name          = TYPE_VIRTIO_BLK_CCW,
1383    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1384    .instance_size = sizeof(VirtIOBlkCcw),
1385    .instance_init = virtio_ccw_blk_instance_init,
1386    .class_init    = virtio_ccw_blk_class_init,
1387};
1388
1389static Property virtio_ccw_serial_properties[] = {
1390    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1391                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1392    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1393                       VIRTIO_CCW_MAX_REV),
1394    DEFINE_PROP_END_OF_LIST(),
1395};
1396
1397static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1398{
1399    DeviceClass *dc = DEVICE_CLASS(klass);
1400    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1401
1402    k->realize = virtio_ccw_serial_realize;
1403    k->unrealize = virtio_ccw_unrealize;
1404    dc->reset = virtio_ccw_reset;
1405    dc->props = virtio_ccw_serial_properties;
1406    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1407}
1408
1409static const TypeInfo virtio_ccw_serial = {
1410    .name          = TYPE_VIRTIO_SERIAL_CCW,
1411    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1412    .instance_size = sizeof(VirtioSerialCcw),
1413    .instance_init = virtio_ccw_serial_instance_init,
1414    .class_init    = virtio_ccw_serial_class_init,
1415};
1416
1417static Property virtio_ccw_balloon_properties[] = {
1418    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1419                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1420    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1421                       VIRTIO_CCW_MAX_REV),
1422    DEFINE_PROP_END_OF_LIST(),
1423};
1424
1425static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1426{
1427    DeviceClass *dc = DEVICE_CLASS(klass);
1428    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1429
1430    k->realize = virtio_ccw_balloon_realize;
1431    k->unrealize = virtio_ccw_unrealize;
1432    dc->reset = virtio_ccw_reset;
1433    dc->props = virtio_ccw_balloon_properties;
1434    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1435}
1436
1437static const TypeInfo virtio_ccw_balloon = {
1438    .name          = TYPE_VIRTIO_BALLOON_CCW,
1439    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1440    .instance_size = sizeof(VirtIOBalloonCcw),
1441    .instance_init = virtio_ccw_balloon_instance_init,
1442    .class_init    = virtio_ccw_balloon_class_init,
1443};
1444
1445static Property virtio_ccw_scsi_properties[] = {
1446    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1447                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1448    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1449                       VIRTIO_CCW_MAX_REV),
1450    DEFINE_PROP_END_OF_LIST(),
1451};
1452
1453static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1454{
1455    DeviceClass *dc = DEVICE_CLASS(klass);
1456    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1457
1458    k->realize = virtio_ccw_scsi_realize;
1459    k->unrealize = virtio_ccw_unrealize;
1460    dc->reset = virtio_ccw_reset;
1461    dc->props = virtio_ccw_scsi_properties;
1462    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1463}
1464
1465static const TypeInfo virtio_ccw_scsi = {
1466    .name          = TYPE_VIRTIO_SCSI_CCW,
1467    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1468    .instance_size = sizeof(VirtIOSCSICcw),
1469    .instance_init = virtio_ccw_scsi_instance_init,
1470    .class_init    = virtio_ccw_scsi_class_init,
1471};
1472
1473#ifdef CONFIG_VHOST_SCSI
1474static Property vhost_ccw_scsi_properties[] = {
1475    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1476                       VIRTIO_CCW_MAX_REV),
1477    DEFINE_PROP_END_OF_LIST(),
1478};
1479
1480static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1481{
1482    DeviceClass *dc = DEVICE_CLASS(klass);
1483    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1484
1485    k->realize = vhost_ccw_scsi_realize;
1486    k->unrealize = virtio_ccw_unrealize;
1487    dc->reset = virtio_ccw_reset;
1488    dc->props = vhost_ccw_scsi_properties;
1489    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1490}
1491
1492static const TypeInfo vhost_ccw_scsi = {
1493    .name          = TYPE_VHOST_SCSI_CCW,
1494    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1495    .instance_size = sizeof(VHostSCSICcw),
1496    .instance_init = vhost_ccw_scsi_instance_init,
1497    .class_init    = vhost_ccw_scsi_class_init,
1498};
1499#endif
1500
1501static void virtio_ccw_rng_instance_init(Object *obj)
1502{
1503    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1504
1505    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1506                                TYPE_VIRTIO_RNG);
1507}
1508
1509static Property virtio_ccw_rng_properties[] = {
1510    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1511                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1512    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1513                       VIRTIO_CCW_MAX_REV),
1514    DEFINE_PROP_END_OF_LIST(),
1515};
1516
1517static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1518{
1519    DeviceClass *dc = DEVICE_CLASS(klass);
1520    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1521
1522    k->realize = virtio_ccw_rng_realize;
1523    k->unrealize = virtio_ccw_unrealize;
1524    dc->reset = virtio_ccw_reset;
1525    dc->props = virtio_ccw_rng_properties;
1526    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1527}
1528
1529static const TypeInfo virtio_ccw_rng = {
1530    .name          = TYPE_VIRTIO_RNG_CCW,
1531    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1532    .instance_size = sizeof(VirtIORNGCcw),
1533    .instance_init = virtio_ccw_rng_instance_init,
1534    .class_init    = virtio_ccw_rng_class_init,
1535};
1536
1537static Property virtio_ccw_crypto_properties[] = {
1538    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1539                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1540    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1541                       VIRTIO_CCW_MAX_REV),
1542    DEFINE_PROP_END_OF_LIST(),
1543};
1544
1545static void virtio_ccw_crypto_instance_init(Object *obj)
1546{
1547    VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(obj);
1548    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1549
1550    ccw_dev->force_revision_1 = true;
1551    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1552                                TYPE_VIRTIO_CRYPTO);
1553}
1554
1555static void virtio_ccw_crypto_class_init(ObjectClass *klass, void *data)
1556{
1557    DeviceClass *dc = DEVICE_CLASS(klass);
1558    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1559
1560    k->realize = virtio_ccw_crypto_realize;
1561    k->unrealize = virtio_ccw_unrealize;
1562    dc->reset = virtio_ccw_reset;
1563    dc->props = virtio_ccw_crypto_properties;
1564    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1565}
1566
1567static const TypeInfo virtio_ccw_crypto = {
1568    .name          = TYPE_VIRTIO_CRYPTO_CCW,
1569    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1570    .instance_size = sizeof(VirtIOCryptoCcw),
1571    .instance_init = virtio_ccw_crypto_instance_init,
1572    .class_init    = virtio_ccw_crypto_class_init,
1573};
1574
1575static Property virtio_ccw_gpu_properties[] = {
1576    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1577                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1578    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1579                       VIRTIO_CCW_MAX_REV),
1580    DEFINE_PROP_END_OF_LIST(),
1581};
1582
1583static void virtio_ccw_gpu_instance_init(Object *obj)
1584{
1585    VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(obj);
1586    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1587
1588    ccw_dev->force_revision_1 = true;
1589    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1590                                TYPE_VIRTIO_GPU);
1591}
1592
1593static void virtio_ccw_gpu_class_init(ObjectClass *klass, void *data)
1594{
1595    DeviceClass *dc = DEVICE_CLASS(klass);
1596    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1597
1598    k->realize = virtio_ccw_gpu_realize;
1599    k->unrealize = virtio_ccw_unrealize;
1600    dc->reset = virtio_ccw_reset;
1601    dc->props = virtio_ccw_gpu_properties;
1602    dc->hotpluggable = false;
1603    set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1604}
1605
1606static const TypeInfo virtio_ccw_gpu = {
1607    .name          = TYPE_VIRTIO_GPU_CCW,
1608    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1609    .instance_size = sizeof(VirtIOGPUCcw),
1610    .instance_init = virtio_ccw_gpu_instance_init,
1611    .class_init    = virtio_ccw_gpu_class_init,
1612};
1613
1614static Property virtio_ccw_input_properties[] = {
1615    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1616                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1617    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1618                       VIRTIO_CCW_MAX_REV),
1619    DEFINE_PROP_END_OF_LIST(),
1620};
1621
1622static void virtio_ccw_input_class_init(ObjectClass *klass, void *data)
1623{
1624    DeviceClass *dc = DEVICE_CLASS(klass);
1625    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1626
1627    k->realize = virtio_ccw_input_realize;
1628    k->unrealize = virtio_ccw_unrealize;
1629    dc->reset = virtio_ccw_reset;
1630    dc->props = virtio_ccw_input_properties;
1631    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1632}
1633
1634static void virtio_ccw_keyboard_instance_init(Object *obj)
1635{
1636    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1637    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1638
1639    ccw_dev->force_revision_1 = true;
1640    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1641                                TYPE_VIRTIO_KEYBOARD);
1642}
1643
1644static void virtio_ccw_mouse_instance_init(Object *obj)
1645{
1646    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1647    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1648
1649    ccw_dev->force_revision_1 = true;
1650    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1651                                TYPE_VIRTIO_MOUSE);
1652}
1653
1654static void virtio_ccw_tablet_instance_init(Object *obj)
1655{
1656    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1657    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1658
1659    ccw_dev->force_revision_1 = true;
1660    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1661                                TYPE_VIRTIO_TABLET);
1662}
1663
1664static const TypeInfo virtio_ccw_input = {
1665    .name          = TYPE_VIRTIO_INPUT_CCW,
1666    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1667    .instance_size = sizeof(VirtIOInputCcw),
1668    .class_init    = virtio_ccw_input_class_init,
1669    .abstract = true,
1670};
1671
1672static const TypeInfo virtio_ccw_input_hid = {
1673    .name          = TYPE_VIRTIO_INPUT_HID_CCW,
1674    .parent        = TYPE_VIRTIO_INPUT_CCW,
1675    .instance_size = sizeof(VirtIOInputHIDCcw),
1676    .abstract = true,
1677};
1678
1679static const TypeInfo virtio_ccw_keyboard = {
1680    .name          = TYPE_VIRTIO_KEYBOARD_CCW,
1681    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1682    .instance_size = sizeof(VirtIOInputHIDCcw),
1683    .instance_init = virtio_ccw_keyboard_instance_init,
1684};
1685
1686static const TypeInfo virtio_ccw_mouse = {
1687    .name          = TYPE_VIRTIO_MOUSE_CCW,
1688    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1689    .instance_size = sizeof(VirtIOInputHIDCcw),
1690    .instance_init = virtio_ccw_mouse_instance_init,
1691};
1692
1693static const TypeInfo virtio_ccw_tablet = {
1694    .name          = TYPE_VIRTIO_TABLET_CCW,
1695    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1696    .instance_size = sizeof(VirtIOInputHIDCcw),
1697    .instance_init = virtio_ccw_tablet_instance_init,
1698};
1699
1700static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1701{
1702    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1703
1704    virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1705    virtio_ccw_device_realize(_dev, errp);
1706}
1707
1708static void virtio_ccw_busdev_unrealize(DeviceState *dev, Error **errp)
1709{
1710    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1711    VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1712
1713    _info->unrealize(_dev, errp);
1714}
1715
1716static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1717                                     DeviceState *dev, Error **errp)
1718{
1719    VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev);
1720
1721    virtio_ccw_stop_ioeventfd(_dev);
1722}
1723
1724static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1725{
1726    DeviceClass *dc = DEVICE_CLASS(klass);
1727    CCWDeviceClass *k = CCW_DEVICE_CLASS(dc);
1728
1729    k->unplug = virtio_ccw_busdev_unplug;
1730    dc->realize = virtio_ccw_busdev_realize;
1731    dc->unrealize = virtio_ccw_busdev_unrealize;
1732    dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1733}
1734
1735static const TypeInfo virtio_ccw_device_info = {
1736    .name = TYPE_VIRTIO_CCW_DEVICE,
1737    .parent = TYPE_CCW_DEVICE,
1738    .instance_size = sizeof(VirtioCcwDevice),
1739    .class_init = virtio_ccw_device_class_init,
1740    .class_size = sizeof(VirtIOCCWDeviceClass),
1741    .abstract = true,
1742};
1743
1744/* virtio-ccw-bus */
1745
1746static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1747                               VirtioCcwDevice *dev)
1748{
1749    DeviceState *qdev = DEVICE(dev);
1750    char virtio_bus_name[] = "virtio-bus";
1751
1752    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1753                        qdev, virtio_bus_name);
1754}
1755
1756static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1757{
1758    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1759    BusClass *bus_class = BUS_CLASS(klass);
1760
1761    bus_class->max_dev = 1;
1762    k->notify = virtio_ccw_notify;
1763    k->vmstate_change = virtio_ccw_vmstate_change;
1764    k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1765    k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1766    k->save_queue = virtio_ccw_save_queue;
1767    k->load_queue = virtio_ccw_load_queue;
1768    k->save_config = virtio_ccw_save_config;
1769    k->load_config = virtio_ccw_load_config;
1770    k->pre_plugged = virtio_ccw_pre_plugged;
1771    k->device_plugged = virtio_ccw_device_plugged;
1772    k->device_unplugged = virtio_ccw_device_unplugged;
1773    k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled;
1774    k->ioeventfd_assign = virtio_ccw_ioeventfd_assign;
1775}
1776
1777static const TypeInfo virtio_ccw_bus_info = {
1778    .name = TYPE_VIRTIO_CCW_BUS,
1779    .parent = TYPE_VIRTIO_BUS,
1780    .instance_size = sizeof(VirtioCcwBusState),
1781    .class_init = virtio_ccw_bus_class_init,
1782};
1783
1784#ifdef CONFIG_VIRTFS
1785static Property virtio_ccw_9p_properties[] = {
1786    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1787            VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1788    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1789                       VIRTIO_CCW_MAX_REV),
1790    DEFINE_PROP_END_OF_LIST(),
1791};
1792
1793static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1794{
1795    V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1796    DeviceState *vdev = DEVICE(&dev->vdev);
1797
1798    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1799    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1800}
1801
1802static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1803{
1804    DeviceClass *dc = DEVICE_CLASS(klass);
1805    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1806
1807    k->unrealize = virtio_ccw_unrealize;
1808    k->realize = virtio_ccw_9p_realize;
1809    dc->reset = virtio_ccw_reset;
1810    dc->props = virtio_ccw_9p_properties;
1811    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1812}
1813
1814static void virtio_ccw_9p_instance_init(Object *obj)
1815{
1816    V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1817
1818    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1819                                TYPE_VIRTIO_9P);
1820}
1821
1822static const TypeInfo virtio_ccw_9p_info = {
1823    .name          = TYPE_VIRTIO_9P_CCW,
1824    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1825    .instance_size = sizeof(V9fsCCWState),
1826    .instance_init = virtio_ccw_9p_instance_init,
1827    .class_init    = virtio_ccw_9p_class_init,
1828};
1829#endif
1830
1831#ifdef CONFIG_VHOST_VSOCK
1832
1833static Property vhost_vsock_ccw_properties[] = {
1834    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1835                       VIRTIO_CCW_MAX_REV),
1836    DEFINE_PROP_END_OF_LIST(),
1837};
1838
1839static void vhost_vsock_ccw_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1840{
1841    VHostVSockCCWState *dev = VHOST_VSOCK_CCW(ccw_dev);
1842    DeviceState *vdev = DEVICE(&dev->vdev);
1843    Error *err = NULL;
1844
1845    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1846    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1847    error_propagate(errp, err);
1848}
1849
1850static void vhost_vsock_ccw_class_init(ObjectClass *klass, void *data)
1851{
1852    DeviceClass *dc = DEVICE_CLASS(klass);
1853    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1854
1855    k->realize = vhost_vsock_ccw_realize;
1856    k->unrealize = virtio_ccw_unrealize;
1857    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1858    dc->props = vhost_vsock_ccw_properties;
1859    dc->reset = virtio_ccw_reset;
1860}
1861
1862static void vhost_vsock_ccw_instance_init(Object *obj)
1863{
1864    VHostVSockCCWState *dev = VHOST_VSOCK_CCW(obj);
1865
1866    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1867                                TYPE_VHOST_VSOCK);
1868}
1869
1870static const TypeInfo vhost_vsock_ccw_info = {
1871    .name          = TYPE_VHOST_VSOCK_CCW,
1872    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1873    .instance_size = sizeof(VHostVSockCCWState),
1874    .instance_init = vhost_vsock_ccw_instance_init,
1875    .class_init    = vhost_vsock_ccw_class_init,
1876};
1877#endif
1878
1879static void virtio_ccw_register(void)
1880{
1881    type_register_static(&virtio_ccw_bus_info);
1882    type_register_static(&virtio_ccw_device_info);
1883    type_register_static(&virtio_ccw_serial);
1884    type_register_static(&virtio_ccw_blk);
1885    type_register_static(&virtio_ccw_net);
1886    type_register_static(&virtio_ccw_balloon);
1887    type_register_static(&virtio_ccw_scsi);
1888#ifdef CONFIG_VHOST_SCSI
1889    type_register_static(&vhost_ccw_scsi);
1890#endif
1891    type_register_static(&virtio_ccw_rng);
1892#ifdef CONFIG_VIRTFS
1893    type_register_static(&virtio_ccw_9p_info);
1894#endif
1895#ifdef CONFIG_VHOST_VSOCK
1896    type_register_static(&vhost_vsock_ccw_info);
1897#endif
1898    type_register_static(&virtio_ccw_crypto);
1899    type_register_static(&virtio_ccw_gpu);
1900    type_register_static(&virtio_ccw_input);
1901    type_register_static(&virtio_ccw_input_hid);
1902    type_register_static(&virtio_ccw_keyboard);
1903    type_register_static(&virtio_ccw_mouse);
1904    type_register_static(&virtio_ccw_tablet);
1905}
1906
1907type_init(virtio_ccw_register)
1908