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