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                    fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
 430                            features.index, features.features);
 431                    /* XXX: do a unit check here? */
 432                }
 433            }
 434            sch->curr_status.scsw.count = ccw.count - sizeof(features);
 435            ret = 0;
 436        }
 437        break;
 438    case CCW_CMD_READ_CONF:
 439        if (check_len) {
 440            if (ccw.count > vdev->config_len) {
 441                ret = -EINVAL;
 442                break;
 443            }
 444        }
 445        len = MIN(ccw.count, vdev->config_len);
 446        if (!ccw.cda) {
 447            ret = -EFAULT;
 448        } else {
 449            virtio_bus_get_vdev_config(&dev->bus, vdev->config);
 450            ccw_dstream_write_buf(&sch->cds, vdev->config, len);
 451            sch->curr_status.scsw.count = ccw.count - len;
 452            ret = 0;
 453        }
 454        break;
 455    case CCW_CMD_WRITE_CONF:
 456        if (check_len) {
 457            if (ccw.count > vdev->config_len) {
 458                ret = -EINVAL;
 459                break;
 460            }
 461        }
 462        len = MIN(ccw.count, vdev->config_len);
 463        if (!ccw.cda) {
 464            ret = -EFAULT;
 465        } else {
 466            ret = ccw_dstream_read_buf(&sch->cds, vdev->config, len);
 467            if (!ret) {
 468                virtio_bus_set_vdev_config(&dev->bus, vdev->config);
 469                sch->curr_status.scsw.count = ccw.count - len;
 470            }
 471        }
 472        break;
 473    case CCW_CMD_READ_STATUS:
 474        if (check_len) {
 475            if (ccw.count != sizeof(status)) {
 476                ret = -EINVAL;
 477                break;
 478            }
 479        } else if (ccw.count < sizeof(status)) {
 480            /* Can't execute command. */
 481            ret = -EINVAL;
 482            break;
 483        }
 484        if (!ccw.cda) {
 485            ret = -EFAULT;
 486        } else {
 487            address_space_stb(&address_space_memory, ccw.cda, vdev->status,
 488                                        MEMTXATTRS_UNSPECIFIED, NULL);
 489            sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status);;
 490            ret = 0;
 491        }
 492        break;
 493    case CCW_CMD_WRITE_STATUS:
 494        if (check_len) {
 495            if (ccw.count != sizeof(status)) {
 496                ret = -EINVAL;
 497                break;
 498            }
 499        } else if (ccw.count < sizeof(status)) {
 500            /* Can't execute command. */
 501            ret = -EINVAL;
 502            break;
 503        }
 504        if (!ccw.cda) {
 505            ret = -EFAULT;
 506        } else {
 507            ccw_dstream_read(&sch->cds, status);
 508            if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
 509                virtio_ccw_stop_ioeventfd(dev);
 510            }
 511            if (virtio_set_status(vdev, status) == 0) {
 512                if (vdev->status == 0) {
 513                    virtio_ccw_reset_virtio(dev, vdev);
 514                }
 515                if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
 516                    virtio_ccw_start_ioeventfd(dev);
 517                }
 518                sch->curr_status.scsw.count = ccw.count - sizeof(status);
 519                ret = 0;
 520            } else {
 521                /* Trigger a command reject. */
 522                ret = -ENOSYS;
 523            }
 524        }
 525        break;
 526    case CCW_CMD_SET_IND:
 527        if (check_len) {
 528            if (ccw.count != sizeof(indicators)) {
 529                ret = -EINVAL;
 530                break;
 531            }
 532        } else if (ccw.count < sizeof(indicators)) {
 533            /* Can't execute command. */
 534            ret = -EINVAL;
 535            break;
 536        }
 537        if (sch->thinint_active) {
 538            /* Trigger a command reject. */
 539            ret = -ENOSYS;
 540            break;
 541        }
 542        if (virtio_get_num_queues(vdev) > NR_CLASSIC_INDICATOR_BITS) {
 543            /* More queues than indicator bits --> trigger a reject */
 544            ret = -ENOSYS;
 545            break;
 546        }
 547        if (!ccw.cda) {
 548            ret = -EFAULT;
 549        } else {
 550            ccw_dstream_read(&sch->cds, indicators);
 551            be64_to_cpus(&indicators);
 552            dev->indicators = get_indicator(indicators, sizeof(uint64_t));
 553            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
 554            ret = 0;
 555        }
 556        break;
 557    case CCW_CMD_SET_CONF_IND:
 558        if (check_len) {
 559            if (ccw.count != sizeof(indicators)) {
 560                ret = -EINVAL;
 561                break;
 562            }
 563        } else if (ccw.count < sizeof(indicators)) {
 564            /* Can't execute command. */
 565            ret = -EINVAL;
 566            break;
 567        }
 568        if (!ccw.cda) {
 569            ret = -EFAULT;
 570        } else {
 571            ccw_dstream_read(&sch->cds, indicators);
 572            be64_to_cpus(&indicators);
 573            dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
 574            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
 575            ret = 0;
 576        }
 577        break;
 578    case CCW_CMD_READ_VQ_CONF:
 579        if (check_len) {
 580            if (ccw.count != sizeof(vq_config)) {
 581                ret = -EINVAL;
 582                break;
 583            }
 584        } else if (ccw.count < sizeof(vq_config)) {
 585            /* Can't execute command. */
 586            ret = -EINVAL;
 587            break;
 588        }
 589        if (!ccw.cda) {
 590            ret = -EFAULT;
 591        } else {
 592            ccw_dstream_read(&sch->cds, vq_config.index);
 593            be16_to_cpus(&vq_config.index);
 594            if (vq_config.index >= VIRTIO_QUEUE_MAX) {
 595                ret = -EINVAL;
 596                break;
 597            }
 598            vq_config.num_max = virtio_queue_get_num(vdev,
 599                                                     vq_config.index);
 600            cpu_to_be16s(&vq_config.num_max);
 601            ccw_dstream_write(&sch->cds, vq_config.num_max);
 602            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
 603            ret = 0;
 604        }
 605        break;
 606    case CCW_CMD_SET_IND_ADAPTER:
 607        if (check_len) {
 608            if (ccw.count != sizeof(thinint)) {
 609                ret = -EINVAL;
 610                break;
 611            }
 612        } else if (ccw.count < sizeof(thinint)) {
 613            /* Can't execute command. */
 614            ret = -EINVAL;
 615            break;
 616        }
 617        if (!ccw.cda) {
 618            ret = -EFAULT;
 619        } else if (dev->indicators && !sch->thinint_active) {
 620            /* Trigger a command reject. */
 621            ret = -ENOSYS;
 622        } else {
 623            if (ccw_dstream_read(&sch->cds, thinint)) {
 624                ret = -EFAULT;
 625            } else {
 626                be64_to_cpus(&thinint.ind_bit);
 627                be64_to_cpus(&thinint.summary_indicator);
 628                be64_to_cpus(&thinint.device_indicator);
 629
 630                dev->summary_indicator =
 631                    get_indicator(thinint.summary_indicator, sizeof(uint8_t));
 632                dev->indicators =
 633                    get_indicator(thinint.device_indicator,
 634                                  thinint.ind_bit / 8 + 1);
 635                dev->thinint_isc = thinint.isc;
 636                dev->routes.adapter.ind_offset = thinint.ind_bit;
 637                dev->routes.adapter.summary_offset = 7;
 638                dev->routes.adapter.adapter_id = css_get_adapter_id(
 639                                                 CSS_IO_ADAPTER_VIRTIO,
 640                                                 dev->thinint_isc);
 641                sch->thinint_active = ((dev->indicators != NULL) &&
 642                                       (dev->summary_indicator != NULL));
 643                sch->curr_status.scsw.count = ccw.count - sizeof(thinint);
 644                ret = 0;
 645            }
 646        }
 647        break;
 648    case CCW_CMD_SET_VIRTIO_REV:
 649        len = sizeof(revinfo);
 650        if (ccw.count < len) {
 651            ret = -EINVAL;
 652            break;
 653        }
 654        if (!ccw.cda) {
 655            ret = -EFAULT;
 656            break;
 657        }
 658        ccw_dstream_read_buf(&sch->cds, &revinfo, 4);
 659        be16_to_cpus(&revinfo.revision);
 660        be16_to_cpus(&revinfo.length);
 661        if (ccw.count < len + revinfo.length ||
 662            (check_len && ccw.count > len + revinfo.length)) {
 663            ret = -EINVAL;
 664            break;
 665        }
 666        /*
 667         * Once we start to support revisions with additional data, we'll
 668         * need to fetch it here. Nothing to do for now, though.
 669         */
 670        if (dev->revision >= 0 ||
 671            revinfo.revision > virtio_ccw_rev_max(dev) ||
 672            (dev->force_revision_1 && !revinfo.revision)) {
 673            ret = -ENOSYS;
 674            break;
 675        }
 676        ret = 0;
 677        dev->revision = revinfo.revision;
 678        break;
 679    default:
 680        ret = -ENOSYS;
 681        break;
 682    }
 683    return ret;
 684}
 685
 686static void virtio_sch_disable_cb(SubchDev *sch)
 687{
 688    VirtioCcwDevice *dev = sch->driver_data;
 689
 690    dev->revision = -1;
 691}
 692
 693static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
 694{
 695    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
 696    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 697    CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
 698    DeviceState *parent = DEVICE(ccw_dev);
 699    BusState *qbus = qdev_get_parent_bus(parent);
 700    VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
 701    SubchDev *sch;
 702    Error *err = NULL;
 703
 704    sch = css_create_sch(ccw_dev->devno, true, cbus->squash_mcss, errp);
 705    if (!sch) {
 706        return;
 707    }
 708    if (!virtio_ccw_rev_max(dev) && dev->force_revision_1) {
 709        error_setg(&err, "Invalid value of property max_rev "
 710                   "(is %d expected >= 1)", virtio_ccw_rev_max(dev));
 711        goto out_err;
 712    }
 713
 714    sch->driver_data = dev;
 715    sch->ccw_cb = virtio_ccw_cb;
 716    sch->disable_cb = virtio_sch_disable_cb;
 717    sch->id.reserved = 0xff;
 718    sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
 719    sch->do_subchannel_work = do_subchannel_work_virtual;
 720    ccw_dev->sch = sch;
 721    dev->indicators = NULL;
 722    dev->revision = -1;
 723    css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
 724
 725    trace_virtio_ccw_new_device(
 726        sch->cssid, sch->ssid, sch->schid, sch->devno,
 727        ccw_dev->devno.valid ? "user-configured" : "auto-configured");
 728
 729    if (kvm_enabled() && !kvm_eventfds_enabled()) {
 730        dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
 731    }
 732
 733    if (k->realize) {
 734        k->realize(dev, &err);
 735        if (err) {
 736            goto out_err;
 737        }
 738    }
 739
 740    ck->realize(ccw_dev, &err);
 741    if (err) {
 742        goto out_err;
 743    }
 744
 745    return;
 746
 747out_err:
 748    error_propagate(errp, err);
 749    css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
 750    ccw_dev->sch = NULL;
 751    g_free(sch);
 752}
 753
 754static int virtio_ccw_exit(VirtioCcwDevice *dev)
 755{
 756    CcwDevice *ccw_dev = CCW_DEVICE(dev);
 757    SubchDev *sch = ccw_dev->sch;
 758
 759    if (sch) {
 760        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
 761        g_free(sch);
 762    }
 763    if (dev->indicators) {
 764        release_indicator(&dev->routes.adapter, dev->indicators);
 765        dev->indicators = NULL;
 766    }
 767    return 0;
 768}
 769
 770static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 771{
 772    DeviceState *qdev = DEVICE(ccw_dev);
 773    VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
 774    DeviceState *vdev = DEVICE(&dev->vdev);
 775
 776    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
 777                                  object_get_typename(OBJECT(qdev)));
 778    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 779    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 780}
 781
 782static void virtio_ccw_net_instance_init(Object *obj)
 783{
 784    VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
 785
 786    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 787                                TYPE_VIRTIO_NET);
 788    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
 789                              "bootindex", &error_abort);
 790}
 791
 792static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 793{
 794    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
 795    DeviceState *vdev = DEVICE(&dev->vdev);
 796
 797    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 798    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 799}
 800
 801static void virtio_ccw_blk_instance_init(Object *obj)
 802{
 803    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
 804
 805    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 806                                TYPE_VIRTIO_BLK);
 807    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
 808                              "bootindex", &error_abort);
 809}
 810
 811static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 812{
 813    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
 814    DeviceState *vdev = DEVICE(&dev->vdev);
 815    DeviceState *proxy = DEVICE(ccw_dev);
 816    char *bus_name;
 817
 818    /*
 819     * For command line compatibility, this sets the virtio-serial-device bus
 820     * name as before.
 821     */
 822    if (proxy->id) {
 823        bus_name = g_strdup_printf("%s.0", proxy->id);
 824        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 825        g_free(bus_name);
 826    }
 827
 828    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 829    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 830}
 831
 832
 833static void virtio_ccw_serial_instance_init(Object *obj)
 834{
 835    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
 836
 837    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 838                                TYPE_VIRTIO_SERIAL);
 839}
 840
 841static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 842{
 843    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
 844    DeviceState *vdev = DEVICE(&dev->vdev);
 845
 846    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 847    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 848}
 849
 850static void virtio_ccw_balloon_instance_init(Object *obj)
 851{
 852    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
 853
 854    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 855                                TYPE_VIRTIO_BALLOON);
 856    object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
 857                              "guest-stats", &error_abort);
 858    object_property_add_alias(obj, "guest-stats-polling-interval",
 859                              OBJECT(&dev->vdev),
 860                              "guest-stats-polling-interval", &error_abort);
 861}
 862
 863static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 864{
 865    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
 866    DeviceState *vdev = DEVICE(&dev->vdev);
 867    DeviceState *qdev = DEVICE(ccw_dev);
 868    char *bus_name;
 869
 870    /*
 871     * For command line compatibility, this sets the virtio-scsi-device bus
 872     * name as before.
 873     */
 874    if (qdev->id) {
 875        bus_name = g_strdup_printf("%s.0", qdev->id);
 876        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 877        g_free(bus_name);
 878    }
 879
 880    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 881    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 882}
 883
 884static void virtio_ccw_scsi_instance_init(Object *obj)
 885{
 886    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
 887
 888    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 889                                TYPE_VIRTIO_SCSI);
 890}
 891
 892#ifdef CONFIG_VHOST_SCSI
 893static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 894{
 895    VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
 896    DeviceState *vdev = DEVICE(&dev->vdev);
 897
 898    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 899    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 900}
 901
 902static void vhost_ccw_scsi_instance_init(Object *obj)
 903{
 904    VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
 905
 906    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
 907                                TYPE_VHOST_SCSI);
 908}
 909#endif
 910
 911static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 912{
 913    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
 914    DeviceState *vdev = DEVICE(&dev->vdev);
 915    Error *err = NULL;
 916
 917    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 918    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
 919    if (err) {
 920        error_propagate(errp, err);
 921        return;
 922    }
 923
 924    object_property_set_link(OBJECT(dev),
 925                             OBJECT(dev->vdev.conf.rng), "rng",
 926                             NULL);
 927}
 928
 929static void virtio_ccw_crypto_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 930{
 931    VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(ccw_dev);
 932    DeviceState *vdev = DEVICE(&dev->vdev);
 933    Error *err = NULL;
 934
 935    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 936    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
 937    if (err) {
 938        error_propagate(errp, err);
 939        return;
 940    }
 941
 942    object_property_set_link(OBJECT(vdev),
 943                             OBJECT(dev->vdev.conf.cryptodev), "cryptodev",
 944                             NULL);
 945}
 946
 947static void virtio_ccw_gpu_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 948{
 949    VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(ccw_dev);
 950    DeviceState *vdev = DEVICE(&dev->vdev);
 951
 952    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 953    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 954}
 955
 956static void virtio_ccw_input_realize(VirtioCcwDevice *ccw_dev, Error **errp)
 957{
 958    VirtIOInputCcw *dev = VIRTIO_INPUT_CCW(ccw_dev);
 959    DeviceState *vdev = DEVICE(&dev->vdev);
 960
 961    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
 962    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
 963}
 964
 965/* DeviceState to VirtioCcwDevice. Note: used on datapath,
 966 * be careful and test performance if you change this.
 967 */
 968static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
 969{
 970    CcwDevice *ccw_dev = to_ccw_dev_fast(d);
 971
 972    return container_of(ccw_dev, VirtioCcwDevice, parent_obj);
 973}
 974
 975static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
 976                                     uint8_t to_be_set)
 977{
 978    uint8_t ind_old, ind_new;
 979    hwaddr len = 1;
 980    uint8_t *ind_addr;
 981
 982    ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
 983    if (!ind_addr) {
 984        error_report("%s(%x.%x.%04x): unable to access indicator",
 985                     __func__, sch->cssid, sch->ssid, sch->schid);
 986        return -1;
 987    }
 988    do {
 989        ind_old = *ind_addr;
 990        ind_new = ind_old | to_be_set;
 991    } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
 992    trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
 993    cpu_physical_memory_unmap(ind_addr, len, 1, len);
 994
 995    return ind_old;
 996}
 997
 998static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
 999{
1000    VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1001    CcwDevice *ccw_dev = to_ccw_dev_fast(d);
1002    SubchDev *sch = ccw_dev->sch;
1003    uint64_t indicators;
1004
1005    /* queue indicators + secondary indicators */
1006    if (vector >= VIRTIO_QUEUE_MAX + 64) {
1007        return;
1008    }
1009
1010    if (vector < VIRTIO_QUEUE_MAX) {
1011        if (!dev->indicators) {
1012            return;
1013        }
1014        if (sch->thinint_active) {
1015            /*
1016             * In the adapter interrupt case, indicators points to a
1017             * memory area that may be (way) larger than 64 bit and
1018             * ind_bit indicates the start of the indicators in a big
1019             * endian notation.
1020             */
1021            uint64_t ind_bit = dev->routes.adapter.ind_offset;
1022
1023            virtio_set_ind_atomic(sch, dev->indicators->addr +
1024                                  (ind_bit + vector) / 8,
1025                                  0x80 >> ((ind_bit + vector) % 8));
1026            if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1027                                       0x01)) {
1028                css_adapter_interrupt(CSS_IO_ADAPTER_VIRTIO, dev->thinint_isc);
1029            }
1030        } else {
1031            indicators = address_space_ldq(&address_space_memory,
1032                                           dev->indicators->addr,
1033                                           MEMTXATTRS_UNSPECIFIED,
1034                                           NULL);
1035            indicators |= 1ULL << vector;
1036            address_space_stq(&address_space_memory, dev->indicators->addr,
1037                              indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1038            css_conditional_io_interrupt(sch);
1039        }
1040    } else {
1041        if (!dev->indicators2) {
1042            return;
1043        }
1044        vector = 0;
1045        indicators = address_space_ldq(&address_space_memory,
1046                                       dev->indicators2->addr,
1047                                       MEMTXATTRS_UNSPECIFIED,
1048                                       NULL);
1049        indicators |= 1ULL << vector;
1050        address_space_stq(&address_space_memory, dev->indicators2->addr,
1051                          indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1052        css_conditional_io_interrupt(sch);
1053    }
1054}
1055
1056static void virtio_ccw_reset(DeviceState *d)
1057{
1058    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1059    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1060    CcwDevice *ccw_dev = CCW_DEVICE(d);
1061
1062    virtio_ccw_reset_virtio(dev, vdev);
1063    css_reset_sch(ccw_dev->sch);
1064}
1065
1066static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1067{
1068    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1069
1070    if (running) {
1071        virtio_ccw_start_ioeventfd(dev);
1072    } else {
1073        virtio_ccw_stop_ioeventfd(dev);
1074    }
1075}
1076
1077static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1078{
1079    CcwDevice *dev = CCW_DEVICE(d);
1080
1081    return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1082}
1083
1084static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1085{
1086    int r;
1087    CcwDevice *ccw_dev = CCW_DEVICE(dev);
1088
1089    if (!ccw_dev->sch->thinint_active) {
1090        return -EINVAL;
1091    }
1092
1093    r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1094    if (r) {
1095        return r;
1096    }
1097    r = map_indicator(&dev->routes.adapter, dev->indicators);
1098    if (r) {
1099        return r;
1100    }
1101    dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1102    dev->routes.adapter.ind_addr = dev->indicators->map;
1103
1104    return 0;
1105}
1106
1107static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1108{
1109    int i;
1110    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1111    int ret;
1112    S390FLICState *fs = s390_get_flic();
1113    S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1114
1115    ret = virtio_ccw_get_mappings(dev);
1116    if (ret) {
1117        return ret;
1118    }
1119    for (i = 0; i < nvqs; i++) {
1120        if (!virtio_queue_get_num(vdev, i)) {
1121            break;
1122        }
1123    }
1124    dev->routes.num_routes = i;
1125    return fsc->add_adapter_routes(fs, &dev->routes);
1126}
1127
1128static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1129{
1130    S390FLICState *fs = s390_get_flic();
1131    S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1132
1133    fsc->release_adapter_routes(fs, &dev->routes);
1134}
1135
1136static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1137{
1138    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1139    VirtQueue *vq = virtio_get_queue(vdev, n);
1140    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1141
1142    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1143                                              dev->routes.gsi[n]);
1144}
1145
1146static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1147{
1148    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1149    VirtQueue *vq = virtio_get_queue(vdev, n);
1150    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1151    int ret;
1152
1153    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1154                                                dev->routes.gsi[n]);
1155    assert(ret == 0);
1156}
1157
1158static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1159                                         bool assign, bool with_irqfd)
1160{
1161    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1162    VirtQueue *vq = virtio_get_queue(vdev, n);
1163    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1164    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1165
1166    if (assign) {
1167        int r = event_notifier_init(notifier, 0);
1168
1169        if (r < 0) {
1170            return r;
1171        }
1172        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1173        if (with_irqfd) {
1174            r = virtio_ccw_add_irqfd(dev, n);
1175            if (r) {
1176                virtio_queue_set_guest_notifier_fd_handler(vq, false,
1177                                                           with_irqfd);
1178                return r;
1179            }
1180        }
1181        /*
1182         * We do not support individual masking for channel devices, so we
1183         * need to manually trigger any guest masking callbacks here.
1184         */
1185        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1186            k->guest_notifier_mask(vdev, n, false);
1187        }
1188        /* get lost events and re-inject */
1189        if (k->guest_notifier_pending &&
1190            k->guest_notifier_pending(vdev, n)) {
1191            event_notifier_set(notifier);
1192        }
1193    } else {
1194        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1195            k->guest_notifier_mask(vdev, n, true);
1196        }
1197        if (with_irqfd) {
1198            virtio_ccw_remove_irqfd(dev, n);
1199        }
1200        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1201        event_notifier_cleanup(notifier);
1202    }
1203    return 0;
1204}
1205
1206static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1207                                          bool assigned)
1208{
1209    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1210    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1211    CcwDevice *ccw_dev = CCW_DEVICE(d);
1212    bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled();
1213    int r, n;
1214
1215    if (with_irqfd && assigned) {
1216        /* irq routes need to be set up before assigning irqfds */
1217        r = virtio_ccw_setup_irqroutes(dev, nvqs);
1218        if (r < 0) {
1219            goto irqroute_error;
1220        }
1221    }
1222    for (n = 0; n < nvqs; n++) {
1223        if (!virtio_queue_get_num(vdev, n)) {
1224            break;
1225        }
1226        r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1227        if (r < 0) {
1228            goto assign_error;
1229        }
1230    }
1231    if (with_irqfd && !assigned) {
1232        /* release irq routes after irqfds have been released */
1233        virtio_ccw_release_irqroutes(dev, nvqs);
1234    }
1235    return 0;
1236
1237assign_error:
1238    while (--n >= 0) {
1239        virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1240    }
1241irqroute_error:
1242    if (with_irqfd && assigned) {
1243        virtio_ccw_release_irqroutes(dev, nvqs);
1244    }
1245    return r;
1246}
1247
1248static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1249{
1250    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1251    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1252
1253    qemu_put_be16(f, virtio_queue_vector(vdev, n));
1254}
1255
1256static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1257{
1258    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1259    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1260    uint16_t vector;
1261
1262    qemu_get_be16s(f, &vector);
1263    virtio_queue_set_vector(vdev, n , vector);
1264
1265    return 0;
1266}
1267
1268static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1269{
1270    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1271    vmstate_save_state(f, &vmstate_virtio_ccw_dev, dev, NULL);
1272}
1273
1274static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1275{
1276    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1277    return vmstate_load_state(f, &vmstate_virtio_ccw_dev, dev, 1);
1278}
1279
1280static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp)
1281{
1282   VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1283   VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1284
1285    if (dev->max_rev >= 1) {
1286        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1287    }
1288}
1289
1290/* This is called by virtio-bus just after the device is plugged. */
1291static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1292{
1293    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1294    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1295    CcwDevice *ccw_dev = CCW_DEVICE(d);
1296    SubchDev *sch = ccw_dev->sch;
1297    int n = virtio_get_num_queues(vdev);
1298    S390FLICState *flic = s390_get_flic();
1299
1300    if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1301        dev->max_rev = 0;
1302    }
1303
1304    if (virtio_get_num_queues(vdev) > VIRTIO_QUEUE_MAX) {
1305        error_setg(errp, "The number of virtqueues %d "
1306                   "exceeds virtio limit %d", n,
1307                   VIRTIO_QUEUE_MAX);
1308        return;
1309    }
1310    if (virtio_get_num_queues(vdev) > flic->adapter_routes_max_batch) {
1311        error_setg(errp, "The number of virtqueues %d "
1312                   "exceeds flic adapter route limit %d", n,
1313                   flic->adapter_routes_max_batch);
1314        return;
1315    }
1316
1317    sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1318
1319
1320    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1321                          d->hotplugged, 1);
1322}
1323
1324static void virtio_ccw_device_unplugged(DeviceState *d)
1325{
1326    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1327
1328    virtio_ccw_stop_ioeventfd(dev);
1329}
1330/**************** Virtio-ccw Bus Device Descriptions *******************/
1331
1332static Property virtio_ccw_net_properties[] = {
1333    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1334                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1335    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1336                       VIRTIO_CCW_MAX_REV),
1337    DEFINE_PROP_END_OF_LIST(),
1338};
1339
1340static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1341{
1342    DeviceClass *dc = DEVICE_CLASS(klass);
1343    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1344
1345    k->realize = virtio_ccw_net_realize;
1346    k->exit = virtio_ccw_exit;
1347    dc->reset = virtio_ccw_reset;
1348    dc->props = virtio_ccw_net_properties;
1349    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1350}
1351
1352static const TypeInfo virtio_ccw_net = {
1353    .name          = TYPE_VIRTIO_NET_CCW,
1354    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1355    .instance_size = sizeof(VirtIONetCcw),
1356    .instance_init = virtio_ccw_net_instance_init,
1357    .class_init    = virtio_ccw_net_class_init,
1358};
1359
1360static Property virtio_ccw_blk_properties[] = {
1361    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1362                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1363    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1364                       VIRTIO_CCW_MAX_REV),
1365    DEFINE_PROP_END_OF_LIST(),
1366};
1367
1368static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1369{
1370    DeviceClass *dc = DEVICE_CLASS(klass);
1371    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1372
1373    k->realize = virtio_ccw_blk_realize;
1374    k->exit = virtio_ccw_exit;
1375    dc->reset = virtio_ccw_reset;
1376    dc->props = virtio_ccw_blk_properties;
1377    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1378}
1379
1380static const TypeInfo virtio_ccw_blk = {
1381    .name          = TYPE_VIRTIO_BLK_CCW,
1382    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1383    .instance_size = sizeof(VirtIOBlkCcw),
1384    .instance_init = virtio_ccw_blk_instance_init,
1385    .class_init    = virtio_ccw_blk_class_init,
1386};
1387
1388static Property virtio_ccw_serial_properties[] = {
1389    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1390                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1391    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1392                       VIRTIO_CCW_MAX_REV),
1393    DEFINE_PROP_END_OF_LIST(),
1394};
1395
1396static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1397{
1398    DeviceClass *dc = DEVICE_CLASS(klass);
1399    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1400
1401    k->realize = virtio_ccw_serial_realize;
1402    k->exit = virtio_ccw_exit;
1403    dc->reset = virtio_ccw_reset;
1404    dc->props = virtio_ccw_serial_properties;
1405    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1406}
1407
1408static const TypeInfo virtio_ccw_serial = {
1409    .name          = TYPE_VIRTIO_SERIAL_CCW,
1410    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1411    .instance_size = sizeof(VirtioSerialCcw),
1412    .instance_init = virtio_ccw_serial_instance_init,
1413    .class_init    = virtio_ccw_serial_class_init,
1414};
1415
1416static Property virtio_ccw_balloon_properties[] = {
1417    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1418                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1419    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1420                       VIRTIO_CCW_MAX_REV),
1421    DEFINE_PROP_END_OF_LIST(),
1422};
1423
1424static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1425{
1426    DeviceClass *dc = DEVICE_CLASS(klass);
1427    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1428
1429    k->realize = virtio_ccw_balloon_realize;
1430    k->exit = virtio_ccw_exit;
1431    dc->reset = virtio_ccw_reset;
1432    dc->props = virtio_ccw_balloon_properties;
1433    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1434}
1435
1436static const TypeInfo virtio_ccw_balloon = {
1437    .name          = TYPE_VIRTIO_BALLOON_CCW,
1438    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1439    .instance_size = sizeof(VirtIOBalloonCcw),
1440    .instance_init = virtio_ccw_balloon_instance_init,
1441    .class_init    = virtio_ccw_balloon_class_init,
1442};
1443
1444static Property virtio_ccw_scsi_properties[] = {
1445    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1446                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1447    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1448                       VIRTIO_CCW_MAX_REV),
1449    DEFINE_PROP_END_OF_LIST(),
1450};
1451
1452static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1453{
1454    DeviceClass *dc = DEVICE_CLASS(klass);
1455    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1456
1457    k->realize = virtio_ccw_scsi_realize;
1458    k->exit = virtio_ccw_exit;
1459    dc->reset = virtio_ccw_reset;
1460    dc->props = virtio_ccw_scsi_properties;
1461    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1462}
1463
1464static const TypeInfo virtio_ccw_scsi = {
1465    .name          = TYPE_VIRTIO_SCSI_CCW,
1466    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1467    .instance_size = sizeof(VirtIOSCSICcw),
1468    .instance_init = virtio_ccw_scsi_instance_init,
1469    .class_init    = virtio_ccw_scsi_class_init,
1470};
1471
1472#ifdef CONFIG_VHOST_SCSI
1473static Property vhost_ccw_scsi_properties[] = {
1474    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1475                       VIRTIO_CCW_MAX_REV),
1476    DEFINE_PROP_END_OF_LIST(),
1477};
1478
1479static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1480{
1481    DeviceClass *dc = DEVICE_CLASS(klass);
1482    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1483
1484    k->realize = vhost_ccw_scsi_realize;
1485    k->exit = virtio_ccw_exit;
1486    dc->reset = virtio_ccw_reset;
1487    dc->props = vhost_ccw_scsi_properties;
1488    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1489}
1490
1491static const TypeInfo vhost_ccw_scsi = {
1492    .name          = TYPE_VHOST_SCSI_CCW,
1493    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1494    .instance_size = sizeof(VHostSCSICcw),
1495    .instance_init = vhost_ccw_scsi_instance_init,
1496    .class_init    = vhost_ccw_scsi_class_init,
1497};
1498#endif
1499
1500static void virtio_ccw_rng_instance_init(Object *obj)
1501{
1502    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1503
1504    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1505                                TYPE_VIRTIO_RNG);
1506}
1507
1508static Property virtio_ccw_rng_properties[] = {
1509    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1510                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1511    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1512                       VIRTIO_CCW_MAX_REV),
1513    DEFINE_PROP_END_OF_LIST(),
1514};
1515
1516static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1517{
1518    DeviceClass *dc = DEVICE_CLASS(klass);
1519    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1520
1521    k->realize = virtio_ccw_rng_realize;
1522    k->exit = virtio_ccw_exit;
1523    dc->reset = virtio_ccw_reset;
1524    dc->props = virtio_ccw_rng_properties;
1525    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1526}
1527
1528static const TypeInfo virtio_ccw_rng = {
1529    .name          = TYPE_VIRTIO_RNG_CCW,
1530    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1531    .instance_size = sizeof(VirtIORNGCcw),
1532    .instance_init = virtio_ccw_rng_instance_init,
1533    .class_init    = virtio_ccw_rng_class_init,
1534};
1535
1536static Property virtio_ccw_crypto_properties[] = {
1537    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1538                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1539    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1540                       VIRTIO_CCW_MAX_REV),
1541    DEFINE_PROP_END_OF_LIST(),
1542};
1543
1544static void virtio_ccw_crypto_instance_init(Object *obj)
1545{
1546    VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(obj);
1547    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1548
1549    ccw_dev->force_revision_1 = true;
1550    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1551                                TYPE_VIRTIO_CRYPTO);
1552}
1553
1554static void virtio_ccw_crypto_class_init(ObjectClass *klass, void *data)
1555{
1556    DeviceClass *dc = DEVICE_CLASS(klass);
1557    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1558
1559    k->realize = virtio_ccw_crypto_realize;
1560    k->exit = virtio_ccw_exit;
1561    dc->reset = virtio_ccw_reset;
1562    dc->props = virtio_ccw_crypto_properties;
1563    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1564}
1565
1566static const TypeInfo virtio_ccw_crypto = {
1567    .name          = TYPE_VIRTIO_CRYPTO_CCW,
1568    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1569    .instance_size = sizeof(VirtIOCryptoCcw),
1570    .instance_init = virtio_ccw_crypto_instance_init,
1571    .class_init    = virtio_ccw_crypto_class_init,
1572};
1573
1574static Property virtio_ccw_gpu_properties[] = {
1575    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1576                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1577    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1578                       VIRTIO_CCW_MAX_REV),
1579    DEFINE_PROP_END_OF_LIST(),
1580};
1581
1582static void virtio_ccw_gpu_instance_init(Object *obj)
1583{
1584    VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(obj);
1585    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1586
1587    ccw_dev->force_revision_1 = true;
1588    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1589                                TYPE_VIRTIO_GPU);
1590}
1591
1592static void virtio_ccw_gpu_class_init(ObjectClass *klass, void *data)
1593{
1594    DeviceClass *dc = DEVICE_CLASS(klass);
1595    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1596
1597    k->realize = virtio_ccw_gpu_realize;
1598    k->exit = virtio_ccw_exit;
1599    dc->reset = virtio_ccw_reset;
1600    dc->props = virtio_ccw_gpu_properties;
1601    dc->hotpluggable = false;
1602    set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1603}
1604
1605static const TypeInfo virtio_ccw_gpu = {
1606    .name          = TYPE_VIRTIO_GPU_CCW,
1607    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1608    .instance_size = sizeof(VirtIOGPUCcw),
1609    .instance_init = virtio_ccw_gpu_instance_init,
1610    .class_init    = virtio_ccw_gpu_class_init,
1611};
1612
1613static Property virtio_ccw_input_properties[] = {
1614    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1615                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1616    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1617                       VIRTIO_CCW_MAX_REV),
1618    DEFINE_PROP_END_OF_LIST(),
1619};
1620
1621static void virtio_ccw_input_class_init(ObjectClass *klass, void *data)
1622{
1623    DeviceClass *dc = DEVICE_CLASS(klass);
1624    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1625
1626    k->realize = virtio_ccw_input_realize;
1627    k->exit = virtio_ccw_exit;
1628    dc->reset = virtio_ccw_reset;
1629    dc->props = virtio_ccw_input_properties;
1630    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1631}
1632
1633static void virtio_ccw_keyboard_instance_init(Object *obj)
1634{
1635    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1636    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1637
1638    ccw_dev->force_revision_1 = true;
1639    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1640                                TYPE_VIRTIO_KEYBOARD);
1641}
1642
1643static void virtio_ccw_mouse_instance_init(Object *obj)
1644{
1645    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1646    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1647
1648    ccw_dev->force_revision_1 = true;
1649    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1650                                TYPE_VIRTIO_MOUSE);
1651}
1652
1653static void virtio_ccw_tablet_instance_init(Object *obj)
1654{
1655    VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1656    VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1657
1658    ccw_dev->force_revision_1 = true;
1659    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1660                                TYPE_VIRTIO_TABLET);
1661}
1662
1663static const TypeInfo virtio_ccw_input = {
1664    .name          = TYPE_VIRTIO_INPUT_CCW,
1665    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1666    .instance_size = sizeof(VirtIOInputCcw),
1667    .class_init    = virtio_ccw_input_class_init,
1668    .abstract = true,
1669};
1670
1671static const TypeInfo virtio_ccw_input_hid = {
1672    .name          = TYPE_VIRTIO_INPUT_HID_CCW,
1673    .parent        = TYPE_VIRTIO_INPUT_CCW,
1674    .instance_size = sizeof(VirtIOInputHIDCcw),
1675    .abstract = true,
1676};
1677
1678static const TypeInfo virtio_ccw_keyboard = {
1679    .name          = TYPE_VIRTIO_KEYBOARD_CCW,
1680    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1681    .instance_size = sizeof(VirtIOInputHIDCcw),
1682    .instance_init = virtio_ccw_keyboard_instance_init,
1683};
1684
1685static const TypeInfo virtio_ccw_mouse = {
1686    .name          = TYPE_VIRTIO_MOUSE_CCW,
1687    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1688    .instance_size = sizeof(VirtIOInputHIDCcw),
1689    .instance_init = virtio_ccw_mouse_instance_init,
1690};
1691
1692static const TypeInfo virtio_ccw_tablet = {
1693    .name          = TYPE_VIRTIO_TABLET_CCW,
1694    .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1695    .instance_size = sizeof(VirtIOInputHIDCcw),
1696    .instance_init = virtio_ccw_tablet_instance_init,
1697};
1698
1699static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1700{
1701    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1702
1703    virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1704    virtio_ccw_device_realize(_dev, errp);
1705}
1706
1707static int virtio_ccw_busdev_exit(DeviceState *dev)
1708{
1709    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1710    VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1711
1712    return _info->exit(_dev);
1713}
1714
1715static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1716                                     DeviceState *dev, Error **errp)
1717{
1718    VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev);
1719
1720    virtio_ccw_stop_ioeventfd(_dev);
1721}
1722
1723static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1724{
1725    DeviceClass *dc = DEVICE_CLASS(klass);
1726    CCWDeviceClass *k = CCW_DEVICE_CLASS(dc);
1727
1728    k->unplug = virtio_ccw_busdev_unplug;
1729    dc->realize = virtio_ccw_busdev_realize;
1730    dc->exit = virtio_ccw_busdev_exit;
1731    dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1732}
1733
1734static const TypeInfo virtio_ccw_device_info = {
1735    .name = TYPE_VIRTIO_CCW_DEVICE,
1736    .parent = TYPE_CCW_DEVICE,
1737    .instance_size = sizeof(VirtioCcwDevice),
1738    .class_init = virtio_ccw_device_class_init,
1739    .class_size = sizeof(VirtIOCCWDeviceClass),
1740    .abstract = true,
1741};
1742
1743/* virtio-ccw-bus */
1744
1745static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1746                               VirtioCcwDevice *dev)
1747{
1748    DeviceState *qdev = DEVICE(dev);
1749    char virtio_bus_name[] = "virtio-bus";
1750
1751    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1752                        qdev, virtio_bus_name);
1753}
1754
1755static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1756{
1757    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1758    BusClass *bus_class = BUS_CLASS(klass);
1759
1760    bus_class->max_dev = 1;
1761    k->notify = virtio_ccw_notify;
1762    k->vmstate_change = virtio_ccw_vmstate_change;
1763    k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1764    k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1765    k->save_queue = virtio_ccw_save_queue;
1766    k->load_queue = virtio_ccw_load_queue;
1767    k->save_config = virtio_ccw_save_config;
1768    k->load_config = virtio_ccw_load_config;
1769    k->pre_plugged = virtio_ccw_pre_plugged;
1770    k->device_plugged = virtio_ccw_device_plugged;
1771    k->device_unplugged = virtio_ccw_device_unplugged;
1772    k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled;
1773    k->ioeventfd_assign = virtio_ccw_ioeventfd_assign;
1774}
1775
1776static const TypeInfo virtio_ccw_bus_info = {
1777    .name = TYPE_VIRTIO_CCW_BUS,
1778    .parent = TYPE_VIRTIO_BUS,
1779    .instance_size = sizeof(VirtioCcwBusState),
1780    .class_init = virtio_ccw_bus_class_init,
1781};
1782
1783#ifdef CONFIG_VIRTFS
1784static Property virtio_ccw_9p_properties[] = {
1785    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1786            VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1787    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1788                       VIRTIO_CCW_MAX_REV),
1789    DEFINE_PROP_END_OF_LIST(),
1790};
1791
1792static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1793{
1794    V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1795    DeviceState *vdev = DEVICE(&dev->vdev);
1796
1797    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1798    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1799}
1800
1801static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1802{
1803    DeviceClass *dc = DEVICE_CLASS(klass);
1804    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1805
1806    k->exit = virtio_ccw_exit;
1807    k->realize = virtio_ccw_9p_realize;
1808    dc->reset = virtio_ccw_reset;
1809    dc->props = virtio_ccw_9p_properties;
1810    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1811}
1812
1813static void virtio_ccw_9p_instance_init(Object *obj)
1814{
1815    V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1816
1817    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1818                                TYPE_VIRTIO_9P);
1819}
1820
1821static const TypeInfo virtio_ccw_9p_info = {
1822    .name          = TYPE_VIRTIO_9P_CCW,
1823    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1824    .instance_size = sizeof(V9fsCCWState),
1825    .instance_init = virtio_ccw_9p_instance_init,
1826    .class_init    = virtio_ccw_9p_class_init,
1827};
1828#endif
1829
1830#ifdef CONFIG_VHOST_VSOCK
1831
1832static Property vhost_vsock_ccw_properties[] = {
1833    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1834                       VIRTIO_CCW_MAX_REV),
1835    DEFINE_PROP_END_OF_LIST(),
1836};
1837
1838static void vhost_vsock_ccw_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1839{
1840    VHostVSockCCWState *dev = VHOST_VSOCK_CCW(ccw_dev);
1841    DeviceState *vdev = DEVICE(&dev->vdev);
1842    Error *err = NULL;
1843
1844    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1845    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1846    error_propagate(errp, err);
1847}
1848
1849static void vhost_vsock_ccw_class_init(ObjectClass *klass, void *data)
1850{
1851    DeviceClass *dc = DEVICE_CLASS(klass);
1852    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1853
1854    k->realize = vhost_vsock_ccw_realize;
1855    k->exit = virtio_ccw_exit;
1856    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1857    dc->props = vhost_vsock_ccw_properties;
1858    dc->reset = virtio_ccw_reset;
1859}
1860
1861static void vhost_vsock_ccw_instance_init(Object *obj)
1862{
1863    VHostVSockCCWState *dev = VHOST_VSOCK_CCW(obj);
1864
1865    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1866                                TYPE_VHOST_VSOCK);
1867}
1868
1869static const TypeInfo vhost_vsock_ccw_info = {
1870    .name          = TYPE_VHOST_VSOCK_CCW,
1871    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1872    .instance_size = sizeof(VHostVSockCCWState),
1873    .instance_init = vhost_vsock_ccw_instance_init,
1874    .class_init    = vhost_vsock_ccw_class_init,
1875};
1876#endif
1877
1878static void virtio_ccw_register(void)
1879{
1880    type_register_static(&virtio_ccw_bus_info);
1881    type_register_static(&virtio_ccw_device_info);
1882    type_register_static(&virtio_ccw_serial);
1883    type_register_static(&virtio_ccw_blk);
1884    type_register_static(&virtio_ccw_net);
1885    type_register_static(&virtio_ccw_balloon);
1886    type_register_static(&virtio_ccw_scsi);
1887#ifdef CONFIG_VHOST_SCSI
1888    type_register_static(&vhost_ccw_scsi);
1889#endif
1890    type_register_static(&virtio_ccw_rng);
1891#ifdef CONFIG_VIRTFS
1892    type_register_static(&virtio_ccw_9p_info);
1893#endif
1894#ifdef CONFIG_VHOST_VSOCK
1895    type_register_static(&vhost_vsock_ccw_info);
1896#endif
1897    type_register_static(&virtio_ccw_crypto);
1898    type_register_static(&virtio_ccw_gpu);
1899    type_register_static(&virtio_ccw_input);
1900    type_register_static(&virtio_ccw_input_hid);
1901    type_register_static(&virtio_ccw_keyboard);
1902    type_register_static(&virtio_ccw_mouse);
1903    type_register_static(&virtio_ccw_tablet);
1904}
1905
1906type_init(virtio_ccw_register)
1907