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