virtio-ccw.c 61.6 KB
Newer Older
1 2 3
/*
 * virtio ccw target implementation
 *
P
Pierre Morel 已提交
4
 * Copyright 2012,2015 IBM Corp.
5
 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
P
Pierre Morel 已提交
6
 *            Pierre Morel <pmorel@linux.vnet.ibm.com>
7 8 9 10 11 12 13
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or (at
 * your option) any later version. See the COPYING file in the top-level
 * directory.
 */

#include "hw/hw.h"
14
#include "sysemu/block-backend.h"
15 16 17
#include "sysemu/blockdev.h"
#include "sysemu/sysemu.h"
#include "net/net.h"
P
Paolo Bonzini 已提交
18 19 20
#include "hw/virtio/virtio.h"
#include "hw/virtio/virtio-serial.h"
#include "hw/virtio/virtio-net.h"
21 22
#include "hw/sysbus.h"
#include "qemu/bitops.h"
23
#include "qemu/error-report.h"
24
#include "hw/virtio/virtio-access.h"
P
Paolo Bonzini 已提交
25
#include "hw/virtio/virtio-bus.h"
26 27
#include "hw/s390x/adapter.h"
#include "hw/s390x/s390_flic.h"
28 29 30 31 32 33

#include "ioinst.h"
#include "css.h"
#include "virtio-ccw.h"
#include "trace.h"

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static QTAILQ_HEAD(, IndAddr) indicator_addresses =
    QTAILQ_HEAD_INITIALIZER(indicator_addresses);

static IndAddr *get_indicator(hwaddr ind_addr, int len)
{
    IndAddr *indicator;

    QTAILQ_FOREACH(indicator, &indicator_addresses, sibling) {
        if (indicator->addr == ind_addr) {
            indicator->refcnt++;
            return indicator;
        }
    }
    indicator = g_new0(IndAddr, 1);
    indicator->addr = ind_addr;
    indicator->len = len;
    indicator->refcnt = 1;
    QTAILQ_INSERT_TAIL(&indicator_addresses, indicator, sibling);
    return indicator;
}

55 56 57 58 59 60 61 62 63 64
static int s390_io_adapter_map(AdapterInfo *adapter, uint64_t map_addr,
                               bool do_map)
{
    S390FLICState *fs = s390_get_flic();
    S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);

    return fsc->io_adapter_map(fs, adapter->adapter_id, map_addr, do_map);
}

static void release_indicator(AdapterInfo *adapter, IndAddr *indicator)
65 66 67 68 69 70 71
{
    assert(indicator->refcnt > 0);
    indicator->refcnt--;
    if (indicator->refcnt > 0) {
        return;
    }
    QTAILQ_REMOVE(&indicator_addresses, indicator, sibling);
72 73 74
    if (indicator->map) {
        s390_io_adapter_map(adapter, indicator->map, false);
    }
75 76 77
    g_free(indicator);
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static int map_indicator(AdapterInfo *adapter, IndAddr *indicator)
{
    int ret;

    if (indicator->map) {
        return 0; /* already mapped is not an error */
    }
    indicator->map = indicator->addr;
    ret = s390_io_adapter_map(adapter, indicator->map, true);
    if ((ret != 0) && (ret != -ENOSYS)) {
        goto out_err;
    }
    return 0;

out_err:
    indicator->map = 0;
    return ret;
}

97 98
static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtioCcwDevice *dev);
99

P
Paolo Bonzini 已提交
100
static void virtual_css_bus_reset(BusState *qbus)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
    /* This should actually be modelled via the generic css */
    css_reset();
}


static void virtual_css_bus_class_init(ObjectClass *klass, void *data)
{
    BusClass *k = BUS_CLASS(klass);

    k->reset = virtual_css_bus_reset;
}

static const TypeInfo virtual_css_bus_info = {
    .name = TYPE_VIRTUAL_CSS_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(VirtualCssBus),
    .class_init = virtual_css_bus_class_init,
};

VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
{
    VirtIODevice *vdev = NULL;
P
Paolo Bonzini 已提交
124
    VirtioCcwDevice *dev = sch->driver_data;
125

P
Paolo Bonzini 已提交
126 127
    if (dev) {
        vdev = virtio_bus_get_device(&dev->bus);
128 129 130 131
    }
    return vdev;
}

C
Cornelia Huck 已提交
132 133 134
static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice *dev, int n,
                                              bool assign, bool set_handler)
{
P
Paolo Bonzini 已提交
135 136
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
C
Cornelia Huck 已提交
137 138 139 140 141 142 143 144 145 146 147 148
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
    int r = 0;
    SubchDev *sch = dev->sch;
    uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;

    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
            error_report("%s: unable to init event notifier: %d", __func__, r);
            return r;
        }
        virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
149
        r = s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
C
Cornelia Huck 已提交
150 151 152 153 154 155 156 157
        if (r < 0) {
            error_report("%s: unable to assign ioeventfd: %d", __func__, r);
            virtio_queue_set_host_notifier_fd_handler(vq, false, false);
            event_notifier_cleanup(notifier);
            return r;
        }
    } else {
        virtio_queue_set_host_notifier_fd_handler(vq, false, false);
158
        s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
C
Cornelia Huck 已提交
159 160 161 162 163 164 165
        event_notifier_cleanup(notifier);
    }
    return r;
}

static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
{
P
Paolo Bonzini 已提交
166
    VirtIODevice *vdev;
C
Cornelia Huck 已提交
167 168 169
    int n, r;

    if (!(dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) ||
170
        dev->ioeventfd_disabled ||
C
Cornelia Huck 已提交
171 172 173
        dev->ioeventfd_started) {
        return;
    }
P
Paolo Bonzini 已提交
174
    vdev = virtio_bus_get_device(&dev->bus);
175
    for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
176
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
177 178 179 180 181 182 183 184 185 186 187 188
            continue;
        }
        r = virtio_ccw_set_guest2host_notifier(dev, n, true, true);
        if (r < 0) {
            goto assign_error;
        }
    }
    dev->ioeventfd_started = true;
    return;

  assign_error:
    while (--n >= 0) {
P
Paolo Bonzini 已提交
189
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202
            continue;
        }
        r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
        assert(r >= 0);
    }
    dev->ioeventfd_started = false;
    /* Disable ioeventfd for this device. */
    dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
    error_report("%s: failed. Fallback to userspace (slower).", __func__);
}

static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
{
P
Paolo Bonzini 已提交
203
    VirtIODevice *vdev;
C
Cornelia Huck 已提交
204 205 206 207 208
    int n, r;

    if (!dev->ioeventfd_started) {
        return;
    }
P
Paolo Bonzini 已提交
209
    vdev = virtio_bus_get_device(&dev->bus);
210
    for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
211
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
212 213 214 215 216 217 218 219
            continue;
        }
        r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
        assert(r >= 0);
    }
    dev->ioeventfd_started = false;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
VirtualCssBus *virtual_css_bus_init(void)
{
    VirtualCssBus *cbus;
    BusState *bus;
    DeviceState *dev;

    /* Create bridge device */
    dev = qdev_create(NULL, "virtual-css-bridge");
    qdev_init_nofail(dev);

    /* Create bus on bridge device */
    bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
    cbus = VIRTUAL_CSS_BUS(bus);

    /* Enable hotplugging */
235
    qbus_set_hotplug_handler(bus, dev, &error_abort);
236 237 238 239 240

    return cbus;
}

/* Communication blocks used by several channel commands. */
241
typedef struct VqInfoBlockLegacy {
242 243 244 245
    uint64_t queue;
    uint32_t align;
    uint16_t index;
    uint16_t num;
246 247 248 249 250 251 252 253 254
} QEMU_PACKED VqInfoBlockLegacy;

typedef struct VqInfoBlock {
    uint64_t desc;
    uint32_t res0;
    uint16_t index;
    uint16_t num;
    uint64_t avail;
    uint64_t used;
255 256 257 258 259 260 261 262 263 264 265 266
} QEMU_PACKED VqInfoBlock;

typedef struct VqConfigBlock {
    uint16_t index;
    uint16_t num_max;
} QEMU_PACKED VqConfigBlock;

typedef struct VirtioFeatDesc {
    uint32_t features;
    uint8_t index;
} QEMU_PACKED VirtioFeatDesc;

267 268 269 270 271 272 273
typedef struct VirtioThinintInfo {
    hwaddr summary_indicator;
    hwaddr device_indicator;
    uint64_t ind_bit;
    uint8_t isc;
} QEMU_PACKED VirtioThinintInfo;

274 275 276 277 278 279
typedef struct VirtioRevInfo {
    uint16_t revision;
    uint16_t length;
    uint8_t data[0];
} QEMU_PACKED VirtioRevInfo;

280
/* Specify where the virtqueues for the subchannel are in guest memory. */
281 282
static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
                              VqInfoBlockLegacy *linfo)
283
{
P
Paolo Bonzini 已提交
284
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
285 286 287
    uint16_t index = info ? info->index : linfo->index;
    uint16_t num = info ? info->num : linfo->num;
    uint64_t desc = info ? info->desc : linfo->queue;
288

289
    if (index >= VIRTIO_CCW_QUEUE_MAX) {
290 291 292 293
        return -EINVAL;
    }

    /* Current code in virtio.c relies on 4K alignment. */
294
    if (linfo && desc && (linfo->align != 4096)) {
295 296 297
        return -EINVAL;
    }

P
Paolo Bonzini 已提交
298
    if (!vdev) {
299 300 301
        return -EINVAL;
    }

302 303 304 305 306 307
    if (info) {
        virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
    } else {
        virtio_queue_set_addr(vdev, index, desc);
    }
    if (!desc) {
308
        virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
309 310 311
    } else {
        /* Fail if we don't have a big enough queue. */
        /* TODO: Add interface to handle vring.num changing */
P
Paolo Bonzini 已提交
312
        if (virtio_queue_get_num(vdev, index) > num) {
313 314
            return -EINVAL;
        }
P
Paolo Bonzini 已提交
315
        virtio_queue_set_vector(vdev, index, index);
316 317
    }
    /* tell notify handler in case of config change */
318
    vdev->config_vector = VIRTIO_CCW_QUEUE_MAX;
319 320 321
    return 0;
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
{
    virtio_ccw_stop_ioeventfd(dev);
    virtio_reset(vdev);
    if (dev->indicators) {
        release_indicator(&dev->routes.adapter, dev->indicators);
        dev->indicators = NULL;
    }
    if (dev->indicators2) {
        release_indicator(&dev->routes.adapter, dev->indicators2);
        dev->indicators2 = NULL;
    }
    if (dev->summary_indicator) {
        release_indicator(&dev->routes.adapter, dev->summary_indicator);
        dev->summary_indicator = NULL;
    }
    dev->sch->thinint_active = false;
}

341 342
static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
                                    bool is_legacy)
343 344 345
{
    int ret;
    VqInfoBlock info;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    VqInfoBlockLegacy linfo;
    size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info);

    if (check_len) {
        if (ccw.count != info_len) {
            return -EINVAL;
        }
    } else if (ccw.count < info_len) {
        /* Can't execute command. */
        return -EINVAL;
    }
    if (!ccw.cda) {
        return -EFAULT;
    }
    if (is_legacy) {
        linfo.queue = address_space_ldq_be(&address_space_memory, ccw.cda,
                                           MEMTXATTRS_UNSPECIFIED, NULL);
        linfo.align = address_space_ldl_be(&address_space_memory,
                                           ccw.cda + sizeof(linfo.queue),
                                           MEMTXATTRS_UNSPECIFIED,
                                           NULL);
        linfo.index = address_space_lduw_be(&address_space_memory,
                                            ccw.cda + sizeof(linfo.queue)
                                            + sizeof(linfo.align),
                                            MEMTXATTRS_UNSPECIFIED,
                                            NULL);
        linfo.num = address_space_lduw_be(&address_space_memory,
                                          ccw.cda + sizeof(linfo.queue)
                                          + sizeof(linfo.align)
                                          + sizeof(linfo.index),
                                          MEMTXATTRS_UNSPECIFIED,
                                          NULL);
        ret = virtio_ccw_set_vqs(sch, NULL, &linfo);
    } else {
        info.desc = address_space_ldq_be(&address_space_memory, ccw.cda,
                                           MEMTXATTRS_UNSPECIFIED, NULL);
        info.index = address_space_lduw_be(&address_space_memory,
                                           ccw.cda + sizeof(info.desc)
                                           + sizeof(info.res0),
                                           MEMTXATTRS_UNSPECIFIED, NULL);
        info.num = address_space_lduw_be(&address_space_memory,
                                         ccw.cda + sizeof(info.desc)
                                         + sizeof(info.res0)
                                         + sizeof(info.index),
                                         MEMTXATTRS_UNSPECIFIED, NULL);
        info.avail = address_space_ldq_be(&address_space_memory,
                                          ccw.cda + sizeof(info.desc)
                                          + sizeof(info.res0)
                                          + sizeof(info.index)
                                          + sizeof(info.num),
                                          MEMTXATTRS_UNSPECIFIED, NULL);
        info.used = address_space_ldq_be(&address_space_memory,
                                         ccw.cda + sizeof(info.desc)
                                         + sizeof(info.res0)
                                         + sizeof(info.index)
                                         + sizeof(info.num)
                                         + sizeof(info.avail),
                                         MEMTXATTRS_UNSPECIFIED, NULL);
        ret = virtio_ccw_set_vqs(sch, &info, NULL);
    }
    sch->curr_status.scsw.count = 0;
    return ret;
}

static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
{
    int ret;
413
    VirtioRevInfo revinfo;
414 415 416 417 418 419
    uint8_t status;
    VirtioFeatDesc features;
    void *config;
    hwaddr indicators;
    VqConfigBlock vq_config;
    VirtioCcwDevice *dev = sch->driver_data;
P
Paolo Bonzini 已提交
420
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
421 422 423
    bool check_len;
    int len;
    hwaddr hw_len;
424
    VirtioThinintInfo *thinint;
425 426 427 428 429 430 431 432 433 434 435 436

    if (!dev) {
        return -EINVAL;
    }

    trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
                                   ccw.cmd_code);
    check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));

    /* Look at the command. */
    switch (ccw.cmd_code) {
    case CCW_CMD_SET_VQ:
437
        ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
438 439
        break;
    case CCW_CMD_VDEV_RESET:
440
        virtio_ccw_reset_virtio(dev, vdev);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        ret = 0;
        break;
    case CCW_CMD_READ_FEAT:
        if (check_len) {
            if (ccw.count != sizeof(features)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(features)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
457 458 459 460 461
            features.index = address_space_ldub(&address_space_memory,
                                                ccw.cda
                                                + sizeof(features.features),
                                                MEMTXATTRS_UNSPECIFIED,
                                                NULL);
C
Cornelia Huck 已提交
462
            if (features.index == 0) {
463 464 465 466 467 468 469 470 471 472
                features.features = (uint32_t)vdev->host_features;
            } else if (features.index == 1) {
                features.features = (uint32_t)(vdev->host_features >> 32);
                /*
                 * Don't offer version 1 to the guest if it did not
                 * negotiate at least revision 1.
                 */
                if (dev->revision <= 0) {
                    features.features &= ~(1 << (VIRTIO_F_VERSION_1 - 32));
                }
473 474 475 476
            } else {
                /* Return zeroes if the guest supports more feature bits. */
                features.features = 0;
            }
477 478 479
            address_space_stl_le(&address_space_memory, ccw.cda,
                                 features.features, MEMTXATTRS_UNSPECIFIED,
                                 NULL);
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
            sch->curr_status.scsw.count = ccw.count - sizeof(features);
            ret = 0;
        }
        break;
    case CCW_CMD_WRITE_FEAT:
        if (check_len) {
            if (ccw.count != sizeof(features)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(features)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
498 499 500 501 502 503 504 505 506
            features.index = address_space_ldub(&address_space_memory,
                                                ccw.cda
                                                + sizeof(features.features),
                                                MEMTXATTRS_UNSPECIFIED,
                                                NULL);
            features.features = address_space_ldl_le(&address_space_memory,
                                                     ccw.cda,
                                                     MEMTXATTRS_UNSPECIFIED,
                                                     NULL);
C
Cornelia Huck 已提交
507
            if (features.index == 0) {
508 509 510 511 512 513 514 515 516 517 518 519 520 521
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0xffffffff00000000ULL) |
                                    features.features);
            } else if (features.index == 1) {
                /*
                 * The guest should not set version 1 if it didn't
                 * negotiate a revision >= 1.
                 */
                if (dev->revision <= 0) {
                    features.features &= ~(1 << (VIRTIO_F_VERSION_1 - 32));
                }
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0x00000000ffffffffULL) |
                                    ((uint64_t)features.features << 32));
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            } else {
                /*
                 * If the guest supports more feature bits, assert that it
                 * passes us zeroes for those we don't support.
                 */
                if (features.features) {
                    fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
                            features.index, features.features);
                    /* XXX: do a unit check here? */
                }
            }
            sch->curr_status.scsw.count = ccw.count - sizeof(features);
            ret = 0;
        }
        break;
    case CCW_CMD_READ_CONF:
        if (check_len) {
P
Paolo Bonzini 已提交
539
            if (ccw.count > vdev->config_len) {
540 541 542 543
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
544
        len = MIN(ccw.count, vdev->config_len);
545 546 547
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
P
Paolo Bonzini 已提交
548
            virtio_bus_get_vdev_config(&dev->bus, vdev->config);
549
            /* XXX config space endianness */
P
Paolo Bonzini 已提交
550
            cpu_physical_memory_write(ccw.cda, vdev->config, len);
551 552 553 554 555 556
            sch->curr_status.scsw.count = ccw.count - len;
            ret = 0;
        }
        break;
    case CCW_CMD_WRITE_CONF:
        if (check_len) {
P
Paolo Bonzini 已提交
557
            if (ccw.count > vdev->config_len) {
558 559 560 561
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
562
        len = MIN(ccw.count, vdev->config_len);
563 564 565 566 567 568 569 570 571 572
        hw_len = len;
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
            config = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
            if (!config) {
                ret = -EFAULT;
            } else {
                len = hw_len;
                /* XXX config space endianness */
P
Paolo Bonzini 已提交
573
                memcpy(vdev->config, config, len);
574
                cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
P
Paolo Bonzini 已提交
575
                virtio_bus_set_vdev_config(&dev->bus, vdev->config);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                sch->curr_status.scsw.count = ccw.count - len;
                ret = 0;
            }
        }
        break;
    case CCW_CMD_WRITE_STATUS:
        if (check_len) {
            if (ccw.count != sizeof(status)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(status)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
595 596
            status = address_space_ldub(&address_space_memory, ccw.cda,
                                        MEMTXATTRS_UNSPECIFIED, NULL);
C
Cornelia Huck 已提交
597 598 599
            if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
                virtio_ccw_stop_ioeventfd(dev);
            }
600 601
            if (virtio_set_status(vdev, status) == 0) {
                if (vdev->status == 0) {
602
                    virtio_ccw_reset_virtio(dev, vdev);
603 604 605 606 607 608 609 610 611
                }
                if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
                    virtio_ccw_start_ioeventfd(dev);
                }
                sch->curr_status.scsw.count = ccw.count - sizeof(status);
                ret = 0;
            } else {
                /* Trigger a command reject. */
                ret = -ENOSYS;
C
Cornelia Huck 已提交
612
            }
613 614 615 616 617 618 619 620 621 622 623 624 625
        }
        break;
    case CCW_CMD_SET_IND:
        if (check_len) {
            if (ccw.count != sizeof(indicators)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(indicators)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
626 627 628 629 630
        if (sch->thinint_active) {
            /* Trigger a command reject. */
            ret = -ENOSYS;
            break;
        }
631
        if (!ccw.cda) {
632 633
            ret = -EFAULT;
        } else {
634 635
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
636
            dev->indicators = get_indicator(indicators, sizeof(uint64_t));
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
            ret = 0;
        }
        break;
    case CCW_CMD_SET_CONF_IND:
        if (check_len) {
            if (ccw.count != sizeof(indicators)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(indicators)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
652
        if (!ccw.cda) {
653 654
            ret = -EFAULT;
        } else {
655 656
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
657
            dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
            sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
            ret = 0;
        }
        break;
    case CCW_CMD_READ_VQ_CONF:
        if (check_len) {
            if (ccw.count != sizeof(vq_config)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(vq_config)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
676 677 678 679
            vq_config.index = address_space_lduw_be(&address_space_memory,
                                                    ccw.cda,
                                                    MEMTXATTRS_UNSPECIFIED,
                                                    NULL);
680
            if (vq_config.index >= VIRTIO_CCW_QUEUE_MAX) {
681 682 683
                ret = -EINVAL;
                break;
            }
P
Paolo Bonzini 已提交
684
            vq_config.num_max = virtio_queue_get_num(vdev,
685
                                                     vq_config.index);
686 687 688 689 690
            address_space_stw_be(&address_space_memory,
                                 ccw.cda + sizeof(vq_config.index),
                                 vq_config.num_max,
                                 MEMTXATTRS_UNSPECIFIED,
                                 NULL);
691 692 693 694
            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
            ret = 0;
        }
        break;
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    case CCW_CMD_SET_IND_ADAPTER:
        if (check_len) {
            if (ccw.count != sizeof(*thinint)) {
                ret = -EINVAL;
                break;
            }
        } else if (ccw.count < sizeof(*thinint)) {
            /* Can't execute command. */
            ret = -EINVAL;
            break;
        }
        len = sizeof(*thinint);
        hw_len = len;
        if (!ccw.cda) {
            ret = -EFAULT;
        } else if (dev->indicators && !sch->thinint_active) {
            /* Trigger a command reject. */
            ret = -ENOSYS;
        } else {
            thinint = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
            if (!thinint) {
                ret = -EFAULT;
            } else {
C
Cornelia Huck 已提交
718 719
                uint64_t ind_bit = ldq_be_p(&thinint->ind_bit);

720
                len = hw_len;
721
                dev->summary_indicator =
C
Cornelia Huck 已提交
722 723 724 725 726
                    get_indicator(ldq_be_p(&thinint->summary_indicator),
                                  sizeof(uint8_t));
                dev->indicators =
                    get_indicator(ldq_be_p(&thinint->device_indicator),
                                  ind_bit / 8 + 1);
727
                dev->thinint_isc = thinint->isc;
C
Cornelia Huck 已提交
728
                dev->routes.adapter.ind_offset = ind_bit;
729
                dev->routes.adapter.summary_offset = 7;
730
                cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len);
731 732
                ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
                                              dev->thinint_isc, true, false,
733
                                              &dev->routes.adapter.adapter_id);
734
                assert(ret == 0);
735 736
                sch->thinint_active = ((dev->indicators != NULL) &&
                                       (dev->summary_indicator != NULL));
737 738 739 740 741
                sch->curr_status.scsw.count = ccw.count - len;
                ret = 0;
            }
        }
        break;
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
    case CCW_CMD_SET_VIRTIO_REV:
        len = sizeof(revinfo);
        if (ccw.count < len) {
            ret = -EINVAL;
            break;
        }
        if (!ccw.cda) {
            ret = -EFAULT;
            break;
        }
        revinfo.revision =
            address_space_lduw_be(&address_space_memory, ccw.cda,
                                  MEMTXATTRS_UNSPECIFIED, NULL);
        revinfo.length =
            address_space_lduw_be(&address_space_memory,
                                  ccw.cda + sizeof(revinfo.revision),
                                  MEMTXATTRS_UNSPECIFIED, NULL);
        if (ccw.count < len + revinfo.length ||
            (check_len && ccw.count > len + revinfo.length)) {
            ret = -EINVAL;
            break;
        }
        /*
         * Once we start to support revisions with additional data, we'll
         * need to fetch it here. Nothing to do for now, though.
         */
        if (dev->revision >= 0 ||
            revinfo.revision > virtio_ccw_rev_max(vdev)) {
            ret = -ENOSYS;
            break;
        }
        ret = 0;
        dev->revision = revinfo.revision;
        break;
776
    default:
C
Cornelia Huck 已提交
777
        ret = -ENOSYS;
778 779 780 781 782
        break;
    }
    return ret;
}

783 784 785 786 787 788 789
static void virtio_sch_disable_cb(SubchDev *sch)
{
    VirtioCcwDevice *dev = sch->driver_data;

    dev->revision = -1;
}

790
static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
791 792 793 794 795 796 797 798 799
{
    unsigned int cssid = 0;
    unsigned int ssid = 0;
    unsigned int schid;
    unsigned int devno;
    bool have_devno = false;
    bool found = false;
    SubchDev *sch;
    int num;
800 801
    Error *err = NULL;
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
802 803 804 805 806 807

    sch = g_malloc0(sizeof(SubchDev));

    sch->driver_data = dev;
    dev->sch = sch;

808
    dev->indicators = NULL;
809 810 811 812

    /* Initialize subchannel structure. */
    sch->channel_prog = 0x0;
    sch->last_cmd_valid = false;
813
    sch->thinint_active = false;
814 815 816 817 818 819 820 821
    /*
     * Use a device number if provided. Otherwise, fall back to subchannel
     * number.
     */
    if (dev->bus_id) {
        num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
        if (num == 3) {
            if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
822 823
                error_setg(errp, "Invalid cssid or ssid: cssid %x, ssid %x",
                           cssid, ssid);
824 825 826 827
                goto out_err;
            }
            /* Enforce use of virtual cssid. */
            if (cssid != VIRTUAL_CSSID) {
828 829
                error_setg(errp, "cssid %x not valid for virtio devices",
                           cssid);
830 831 832
                goto out_err;
            }
            if (css_devno_used(cssid, ssid, devno)) {
833 834
                error_setg(errp, "Device %x.%x.%04x already exists",
                           cssid, ssid, devno);
835 836 837 838 839 840 841
                goto out_err;
            }
            sch->cssid = cssid;
            sch->ssid = ssid;
            sch->devno = devno;
            have_devno = true;
        } else {
842
            error_setg(errp, "Malformed devno parameter '%s'", dev->bus_id);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
            goto out_err;
        }
    }

    /* Find the next free id. */
    if (have_devno) {
        for (schid = 0; schid <= MAX_SCHID; schid++) {
            if (!css_find_subch(1, cssid, ssid, schid)) {
                sch->schid = schid;
                css_subch_assign(cssid, ssid, schid, devno, sch);
                found = true;
                break;
            }
        }
        if (!found) {
858 859
            error_setg(errp, "No free subchannel found for %x.%x.%04x",
                       cssid, ssid, devno);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
            goto out_err;
        }
        trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
                                    "user-configured");
    } else {
        cssid = VIRTUAL_CSSID;
        for (ssid = 0; ssid <= MAX_SSID; ssid++) {
            for (schid = 0; schid <= MAX_SCHID; schid++) {
                if (!css_find_subch(1, cssid, ssid, schid)) {
                    sch->cssid = cssid;
                    sch->ssid = ssid;
                    sch->schid = schid;
                    devno = schid;
                    /*
                     * If the devno is already taken, look further in this
                     * subchannel set.
                     */
                    while (css_devno_used(cssid, ssid, devno)) {
                        if (devno == MAX_SCHID) {
                            devno = 0;
                        } else if (devno == schid - 1) {
881
                            error_setg(errp, "No free devno found");
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
                            goto out_err;
                        } else {
                            devno++;
                        }
                    }
                    sch->devno = devno;
                    css_subch_assign(cssid, ssid, schid, devno, sch);
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        if (!found) {
898
            error_setg(errp, "Virtual channel subsystem is full!");
899 900 901 902 903 904 905 906 907 908
            goto out_err;
        }
        trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
                                    "auto-configured");
    }

    /* Build initial schib. */
    css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);

    sch->ccw_cb = virtio_ccw_cb;
909
    sch->disable_cb = virtio_sch_disable_cb;
910 911 912 913 914

    /* Build senseid data. */
    memset(&sch->id, 0, sizeof(SenseId));
    sch->id.reserved = 0xff;
    sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
915

916 917
    dev->revision = -1;

918 919 920 921 922 923 924 925 926
    if (k->realize) {
        k->realize(dev, &err);
    }
    if (err) {
        error_propagate(errp, err);
        css_subch_assign(cssid, ssid, schid, devno, NULL);
        goto out_err;
    }

927
    return;
928 929 930 931 932 933 934 935 936 937 938 939 940 941

out_err:
    dev->sch = NULL;
    g_free(sch);
}

static int virtio_ccw_exit(VirtioCcwDevice *dev)
{
    SubchDev *sch = dev->sch;

    if (sch) {
        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
        g_free(sch);
    }
942
    if (dev->indicators) {
943
        release_indicator(&dev->routes.adapter, dev->indicators);
944 945
        dev->indicators = NULL;
    }
946 947 948
    return 0;
}

949
static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
950
{
951
    DeviceState *qdev = DEVICE(ccw_dev);
952 953
    VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
954
    Error *err = NULL;
955

956 957
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
958
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
959 960 961
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
962 963 964
    }
}

965
static void virtio_ccw_net_instance_init(Object *obj)
966
{
967
    VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
968 969 970

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
971 972
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
973 974
}

975
static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
976
{
977 978
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
979 980
    Error *err = NULL;

981
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
982 983 984
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
985 986 987
    }
}

988
static void virtio_ccw_blk_instance_init(Object *obj)
989
{
990
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
991 992 993

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
994 995
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
                              &error_abort);
996 997
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
998 999
}

1000
static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1001
{
1002 1003
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1004
    DeviceState *proxy = DEVICE(ccw_dev);
1005
    Error *err = NULL;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    char *bus_name;

    /*
     * For command line compatibility, this sets the virtio-serial-device bus
     * name as before.
     */
    if (proxy->id) {
        bus_name = g_strdup_printf("%s.0", proxy->id);
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
        g_free(bus_name);
    }
1017

1018
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1019 1020 1021
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
1022 1023 1024
    }
}

1025 1026

static void virtio_ccw_serial_instance_init(Object *obj)
1027
{
1028
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
1029 1030 1031

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
1032 1033
}

1034
static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1035
{
1036 1037
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1038
    Error *err = NULL;
1039

1040
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1041 1042 1043
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
1044 1045 1046
    }
}

1047
static void virtio_ccw_balloon_instance_init(Object *obj)
1048
{
1049
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
1050

1051 1052
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
1053 1054 1055 1056 1057
    object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
                              "guest-stats", &error_abort);
    object_property_add_alias(obj, "guest-stats-polling-interval",
                              OBJECT(&dev->vdev),
                              "guest-stats-polling-interval", &error_abort);
1058 1059
}

1060
static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1061
{
1062 1063
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1064
    DeviceState *qdev = DEVICE(ccw_dev);
1065
    Error *err = NULL;
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    char *bus_name;

    /*
     * For command line compatibility, this sets the virtio-scsi-device bus
     * name as before.
     */
    if (qdev->id) {
        bus_name = g_strdup_printf("%s.0", qdev->id);
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
        g_free(bus_name);
    }
1077

1078
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1079 1080 1081
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
1082 1083 1084
    }
}

1085
static void virtio_ccw_scsi_instance_init(Object *obj)
1086
{
1087
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
1088 1089 1090

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
1091 1092
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
                              &error_abort);
1093 1094
}

1095
#ifdef CONFIG_VHOST_SCSI
1096
static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1097 1098 1099
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1100
    Error *err = NULL;
1101 1102

    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1103 1104 1105
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
1106 1107 1108 1109 1110 1111
    }
}

static void vhost_ccw_scsi_instance_init(Object *obj)
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
1112 1113 1114

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
1115 1116 1117
}
#endif

1118
static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
C
Cornelia Huck 已提交
1119
{
1120 1121
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1122
    Error *err = NULL;
C
Cornelia Huck 已提交
1123

1124
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1125 1126 1127 1128
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
C
Cornelia Huck 已提交
1129 1130
    }

1131
    object_property_set_link(OBJECT(dev),
1132
                             OBJECT(dev->vdev.conf.rng), "rng",
1133
                             NULL);
C
Cornelia Huck 已提交
1134 1135
}

1136 1137 1138 1139 1140 1141 1142 1143
/* DeviceState to VirtioCcwDevice. Note: used on datapath,
 * be careful and test performance if you change this.
 */
static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
{
    return container_of(d, VirtioCcwDevice, parent_obj);
}

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
                                     uint8_t to_be_set)
{
    uint8_t ind_old, ind_new;
    hwaddr len = 1;
    uint8_t *ind_addr;

    ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
    if (!ind_addr) {
        error_report("%s(%x.%x.%04x): unable to access indicator",
                     __func__, sch->cssid, sch->ssid, sch->schid);
        return -1;
    }
    do {
        ind_old = *ind_addr;
        ind_new = ind_old | to_be_set;
    } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
    cpu_physical_memory_unmap(ind_addr, len, 1, len);

    return ind_old;
}

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
{
    VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
    SubchDev *sch = dev->sch;
    uint64_t indicators;

    if (vector >= 128) {
        return;
    }

1176
    if (vector < VIRTIO_CCW_QUEUE_MAX) {
1177 1178 1179
        if (!dev->indicators) {
            return;
        }
1180 1181 1182 1183 1184 1185 1186
        if (sch->thinint_active) {
            /*
             * In the adapter interrupt case, indicators points to a
             * memory area that may be (way) larger than 64 bit and
             * ind_bit indicates the start of the indicators in a big
             * endian notation.
             */
1187 1188
            uint64_t ind_bit = dev->routes.adapter.ind_offset;

1189
            virtio_set_ind_atomic(sch, dev->indicators->addr +
1190 1191
                                  (ind_bit + vector) / 8,
                                  0x80 >> ((ind_bit + vector) % 8));
1192
            if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1193 1194 1195 1196
                                       0x01)) {
                css_adapter_interrupt(dev->thinint_isc);
            }
        } else {
1197 1198 1199 1200
            indicators = address_space_ldq(&address_space_memory,
                                           dev->indicators->addr,
                                           MEMTXATTRS_UNSPECIFIED,
                                           NULL);
1201
            indicators |= 1ULL << vector;
1202 1203
            address_space_stq(&address_space_memory, dev->indicators->addr,
                              indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1204 1205
            css_conditional_io_interrupt(sch);
        }
1206
    } else {
1207 1208 1209
        if (!dev->indicators2) {
            return;
        }
1210
        vector = 0;
1211 1212 1213 1214
        indicators = address_space_ldq(&address_space_memory,
                                       dev->indicators2->addr,
                                       MEMTXATTRS_UNSPECIFIED,
                                       NULL);
1215
        indicators |= 1ULL << vector;
1216 1217
        address_space_stq(&address_space_memory, dev->indicators2->addr,
                          indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1218
        css_conditional_io_interrupt(sch);
1219 1220 1221 1222 1223 1224
    }
}

static void virtio_ccw_reset(DeviceState *d)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
P
Paolo Bonzini 已提交
1225
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1226

1227
    virtio_ccw_reset_virtio(dev, vdev);
1228 1229 1230
    css_reset_sch(dev->sch);
}

C
Cornelia Huck 已提交
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    if (running) {
        virtio_ccw_start_ioeventfd(dev);
    } else {
        virtio_ccw_stop_ioeventfd(dev);
    }
}

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
}

static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    /* Stop using the generic ioeventfd, we are doing eventfd handling
     * ourselves below */
    dev->ioeventfd_disabled = assign;
    if (assign) {
        virtio_ccw_stop_ioeventfd(dev);
    }
    return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
}

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
{
    int r;

    if (!dev->sch->thinint_active) {
        return -EINVAL;
    }

    r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
    if (r) {
        return r;
    }
    r = map_indicator(&dev->routes.adapter, dev->indicators);
    if (r) {
        return r;
    }
    dev->routes.adapter.summary_addr = dev->summary_indicator->map;
    dev->routes.adapter.ind_addr = dev->indicators->map;

    return 0;
}

static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
{
    int i;
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    int ret;
    S390FLICState *fs = s390_get_flic();
    S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);

    ret = virtio_ccw_get_mappings(dev);
    if (ret) {
        return ret;
    }
    for (i = 0; i < nvqs; i++) {
        if (!virtio_queue_get_num(vdev, i)) {
            break;
        }
    }
    dev->routes.num_routes = i;
    return fsc->add_adapter_routes(fs, &dev->routes);
}

static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
{
    S390FLICState *fs = s390_get_flic();
    S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);

    fsc->release_adapter_routes(fs, &dev->routes);
}

static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
{
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

1319 1320
    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
                                              dev->routes.gsi[n]);
1321 1322 1323 1324 1325 1326 1327 1328 1329
}

static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
{
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
    int ret;

1330 1331
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
                                                dev->routes.gsi[n]);
1332 1333 1334
    assert(ret == 0);
}

1335 1336 1337
static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
                                         bool assign, bool with_irqfd)
{
P
Paolo Bonzini 已提交
1338 1339
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
1340
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
P
Paolo Bonzini 已提交
1341
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1342 1343 1344 1345 1346 1347 1348 1349

    if (assign) {
        int r = event_notifier_init(notifier, 0);

        if (r < 0) {
            return r;
        }
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        if (with_irqfd) {
            r = virtio_ccw_add_irqfd(dev, n);
            if (r) {
                virtio_queue_set_guest_notifier_fd_handler(vq, false,
                                                           with_irqfd);
                return r;
            }
        }
        /*
         * We do not support individual masking for channel devices, so we
         * need to manually trigger any guest masking callbacks here.
1361 1362
         */
        if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
1363
            k->guest_notifier_mask(vdev, n, false);
1364 1365 1366
        }
        /* get lost events and re-inject */
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
1367
            k->guest_notifier_pending(vdev, n)) {
1368 1369 1370 1371
            event_notifier_set(notifier);
        }
    } else {
        if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
1372
            k->guest_notifier_mask(vdev, n, true);
1373
        }
1374 1375 1376
        if (with_irqfd) {
            virtio_ccw_remove_irqfd(dev, n);
        }
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
        event_notifier_cleanup(notifier);
    }
    return 0;
}

static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
                                          bool assigned)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
P
Paolo Bonzini 已提交
1387
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1388
    bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1389 1390
    int r, n;

1391 1392 1393 1394 1395 1396 1397
    if (with_irqfd && assigned) {
        /* irq routes need to be set up before assigning irqfds */
        r = virtio_ccw_setup_irqroutes(dev, nvqs);
        if (r < 0) {
            goto irqroute_error;
        }
    }
1398 1399 1400 1401
    for (n = 0; n < nvqs; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }
1402
        r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1403 1404 1405 1406
        if (r < 0) {
            goto assign_error;
        }
    }
1407 1408 1409 1410
    if (with_irqfd && !assigned) {
        /* release irq routes after irqfds have been released */
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1411 1412 1413 1414 1415 1416
    return 0;

assign_error:
    while (--n >= 0) {
        virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
    }
1417 1418 1419 1420
irqroute_error:
    if (with_irqfd && assigned) {
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1421 1422 1423
    return r;
}

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);

    qemu_put_be16(f, virtio_queue_vector(vdev, n));
}

static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    uint16_t vector;

    qemu_get_be16s(f, &vector);
    virtio_queue_set_vector(vdev, n , vector);

    return 0;
}

static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
    SubchDev *s = dev->sch;
1448
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

    subch_device_save(s, f);
    if (dev->indicators != NULL) {
        qemu_put_be32(f, dev->indicators->len);
        qemu_put_be64(f, dev->indicators->addr);
    } else {
        qemu_put_be32(f, 0);
        qemu_put_be64(f, 0UL);
    }
    if (dev->indicators2 != NULL) {
        qemu_put_be32(f, dev->indicators2->len);
        qemu_put_be64(f, dev->indicators2->addr);
    } else {
        qemu_put_be32(f, 0);
        qemu_put_be64(f, 0UL);
    }
    if (dev->summary_indicator != NULL) {
        qemu_put_be32(f, dev->summary_indicator->len);
        qemu_put_be64(f, dev->summary_indicator->addr);
    } else {
        qemu_put_be32(f, 0);
        qemu_put_be64(f, 0UL);
    }
1472
    qemu_put_be16(f, vdev->config_vector);
1473 1474
    qemu_put_be64(f, dev->routes.adapter.ind_offset);
    qemu_put_byte(f, dev->thinint_isc);
C
Cornelia Huck 已提交
1475
    qemu_put_be32(f, dev->revision);
1476 1477 1478 1479 1480 1481
}

static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
    SubchDev *s = dev->sch;
1482
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
    int len;

    s->driver_data = dev;
    subch_device_load(s, f);
    len = qemu_get_be32(f);
    if (len != 0) {
        dev->indicators = get_indicator(qemu_get_be64(f), len);
    } else {
        qemu_get_be64(f);
        dev->indicators = NULL;
    }
    len = qemu_get_be32(f);
    if (len != 0) {
        dev->indicators2 = get_indicator(qemu_get_be64(f), len);
    } else {
        qemu_get_be64(f);
        dev->indicators2 = NULL;
    }
    len = qemu_get_be32(f);
    if (len != 0) {
        dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
    } else {
        qemu_get_be64(f);
        dev->summary_indicator = NULL;
    }
1508
    qemu_get_be16s(f, &vdev->config_vector);
1509 1510
    dev->routes.adapter.ind_offset = qemu_get_be64(f);
    dev->thinint_isc = qemu_get_byte(f);
1511
    dev->revision = qemu_get_be32(f);
1512 1513 1514 1515 1516 1517 1518 1519 1520
    if (s->thinint_active) {
        return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
                                       dev->thinint_isc, true, false,
                                       &dev->routes.adapter.adapter_id);
    }

    return 0;
}

1521
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1522
static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1523 1524
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1525
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1526
    SubchDev *sch = dev->sch;
1527 1528 1529 1530 1531 1532 1533 1534
    int n = virtio_get_num_queues(vdev);

    if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
        error_setg(errp, "The nubmer of virtqueues %d "
                   "exceeds ccw limit %d", n,
                   VIRTIO_CCW_QUEUE_MAX);
        return;
    }
1535

1536 1537 1538 1539
    if (!kvm_eventfds_enabled()) {
        dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
    }

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
    sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);

    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
                          d->hotplugged, 1);
}

static void virtio_ccw_device_unplugged(DeviceState *d)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    virtio_ccw_stop_ioeventfd(dev);
}
1552 1553 1554 1555
/**************** Virtio-ccw Bus Device Descriptions *******************/

static Property virtio_ccw_net_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1556 1557
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1558 1559 1560 1561 1562 1563 1564 1565
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1566
    k->realize = virtio_ccw_net_realize;
1567
    k->exit = virtio_ccw_exit;
1568 1569
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_net_properties;
C
Cornelia Huck 已提交
1570
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1571 1572 1573
}

static const TypeInfo virtio_ccw_net = {
1574
    .name          = TYPE_VIRTIO_NET_CCW,
1575
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1576 1577
    .instance_size = sizeof(VirtIONetCcw),
    .instance_init = virtio_ccw_net_instance_init,
1578 1579 1580 1581 1582
    .class_init    = virtio_ccw_net_class_init,
};

static Property virtio_ccw_blk_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1583 1584
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1585 1586 1587 1588 1589 1590 1591 1592
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1593
    k->realize = virtio_ccw_blk_realize;
1594
    k->exit = virtio_ccw_exit;
1595 1596
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_blk_properties;
C
Cornelia Huck 已提交
1597
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1598 1599 1600
}

static const TypeInfo virtio_ccw_blk = {
1601
    .name          = TYPE_VIRTIO_BLK_CCW,
1602
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1603 1604
    .instance_size = sizeof(VirtIOBlkCcw),
    .instance_init = virtio_ccw_blk_instance_init,
1605 1606 1607 1608 1609
    .class_init    = virtio_ccw_blk_class_init,
};

static Property virtio_ccw_serial_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1610 1611
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1612 1613 1614 1615 1616 1617 1618 1619
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1620
    k->realize = virtio_ccw_serial_realize;
1621
    k->exit = virtio_ccw_exit;
1622 1623
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_serial_properties;
C
Cornelia Huck 已提交
1624
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1625 1626 1627
}

static const TypeInfo virtio_ccw_serial = {
1628
    .name          = TYPE_VIRTIO_SERIAL_CCW,
1629
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1630 1631
    .instance_size = sizeof(VirtioSerialCcw),
    .instance_init = virtio_ccw_serial_instance_init,
1632 1633 1634 1635 1636
    .class_init    = virtio_ccw_serial_class_init,
};

static Property virtio_ccw_balloon_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1637 1638
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1639 1640 1641 1642 1643 1644 1645 1646
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1647
    k->realize = virtio_ccw_balloon_realize;
1648
    k->exit = virtio_ccw_exit;
1649 1650
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_balloon_properties;
C
Cornelia Huck 已提交
1651
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1652 1653 1654
}

static const TypeInfo virtio_ccw_balloon = {
1655
    .name          = TYPE_VIRTIO_BALLOON_CCW,
1656
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1657 1658
    .instance_size = sizeof(VirtIOBalloonCcw),
    .instance_init = virtio_ccw_balloon_instance_init,
1659 1660 1661 1662 1663
    .class_init    = virtio_ccw_balloon_class_init,
};

static Property virtio_ccw_scsi_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1664 1665
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1666 1667 1668 1669 1670 1671 1672 1673
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1674
    k->realize = virtio_ccw_scsi_realize;
1675
    k->exit = virtio_ccw_exit;
1676 1677
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_scsi_properties;
C
Cornelia Huck 已提交
1678
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1679 1680 1681
}

static const TypeInfo virtio_ccw_scsi = {
1682
    .name          = TYPE_VIRTIO_SCSI_CCW,
1683
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1684 1685
    .instance_size = sizeof(VirtIOSCSICcw),
    .instance_init = virtio_ccw_scsi_instance_init,
1686 1687 1688
    .class_init    = virtio_ccw_scsi_class_init,
};

1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
#ifdef CONFIG_VHOST_SCSI
static Property vhost_ccw_scsi_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
    DEFINE_PROP_END_OF_LIST(),
};

static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1700
    k->realize = vhost_ccw_scsi_realize;
1701 1702 1703
    k->exit = virtio_ccw_exit;
    dc->reset = virtio_ccw_reset;
    dc->props = vhost_ccw_scsi_properties;
C
Cornelia Huck 已提交
1704
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1705 1706 1707 1708 1709
}

static const TypeInfo vhost_ccw_scsi = {
    .name          = TYPE_VHOST_SCSI_CCW,
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1710
    .instance_size = sizeof(VHostSCSICcw),
1711 1712 1713 1714 1715
    .instance_init = vhost_ccw_scsi_instance_init,
    .class_init    = vhost_ccw_scsi_class_init,
};
#endif

1716
static void virtio_ccw_rng_instance_init(Object *obj)
C
Cornelia Huck 已提交
1717
{
1718
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1719 1720 1721

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
1722 1723
    object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
                              "rng", &error_abort);
C
Cornelia Huck 已提交
1724 1725 1726 1727
}

static Property virtio_ccw_rng_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1728 1729
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1730 1731 1732 1733 1734 1735 1736 1737
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

1738
    k->realize = virtio_ccw_rng_realize;
1739
    k->exit = virtio_ccw_exit;
C
Cornelia Huck 已提交
1740 1741
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_rng_properties;
C
Cornelia Huck 已提交
1742
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
C
Cornelia Huck 已提交
1743 1744 1745
}

static const TypeInfo virtio_ccw_rng = {
1746
    .name          = TYPE_VIRTIO_RNG_CCW,
C
Cornelia Huck 已提交
1747
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1748 1749
    .instance_size = sizeof(VirtIORNGCcw),
    .instance_init = virtio_ccw_rng_instance_init,
C
Cornelia Huck 已提交
1750 1751 1752
    .class_init    = virtio_ccw_rng_class_init,
};

1753
static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1754 1755 1756
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;

1757
    virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1758
    virtio_ccw_device_realize(_dev, errp);
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
}

static int virtio_ccw_busdev_exit(DeviceState *dev)
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
    VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);

    return _info->exit(_dev);
}

1769 1770
static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
                                     DeviceState *dev, Error **errp)
1771 1772 1773 1774
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
    SubchDev *sch = _dev->sch;

1775 1776
    virtio_ccw_stop_ioeventfd(_dev);

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
    /*
     * We should arrive here only for device_del, since we don't support
     * direct hot(un)plug of channels, but only through virtio.
     */
    assert(sch != NULL);
    /* Subchannel is now disabled and no longer valid. */
    sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
                                     PMCW_FLAGS_MASK_DNV);

    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);

1788
    object_unparent(OBJECT(dev));
1789 1790 1791 1792 1793 1794
}

static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

1795
    dc->realize = virtio_ccw_busdev_realize;
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
    dc->exit = virtio_ccw_busdev_exit;
    dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
}

static const TypeInfo virtio_ccw_device_info = {
    .name = TYPE_VIRTIO_CCW_DEVICE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(VirtioCcwDevice),
    .class_init = virtio_ccw_device_class_init,
    .class_size = sizeof(VirtIOCCWDeviceClass),
    .abstract = true,
};

/***************** Virtual-css Bus Bridge Device ********************/
/* Only required to have the virtio bus as child in the system bus */

static int virtual_css_bridge_init(SysBusDevice *dev)
{
    /* nothing */
    return 0;
}

static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1821
    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
C
Cornelia Huck 已提交
1822
    DeviceClass *dc = DEVICE_CLASS(klass);
1823 1824

    k->init = virtual_css_bridge_init;
1825
    hc->unplug = virtio_ccw_busdev_unplug;
C
Cornelia Huck 已提交
1826
    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1827 1828 1829 1830 1831 1832 1833
}

static const TypeInfo virtual_css_bridge_info = {
    .name          = "virtual-css-bridge",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(SysBusDevice),
    .class_init    = virtual_css_bridge_class_init,
1834 1835 1836 1837
    .interfaces = (InterfaceInfo[]) {
        { TYPE_HOTPLUG_HANDLER },
        { }
    }
1838 1839 1840 1841
};

/* virtio-ccw-bus */

1842 1843
static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtioCcwDevice *dev)
1844 1845
{
    DeviceState *qdev = DEVICE(dev);
1846
    char virtio_bus_name[] = "virtio-bus";
1847

1848 1849
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
                        qdev, virtio_bus_name);
1850 1851 1852 1853 1854 1855 1856 1857 1858
}

static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
{
    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
    BusClass *bus_class = BUS_CLASS(klass);

    bus_class->max_dev = 1;
    k->notify = virtio_ccw_notify;
C
Cornelia Huck 已提交
1859
    k->vmstate_change = virtio_ccw_vmstate_change;
1860 1861 1862
    k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
    k->set_host_notifier = virtio_ccw_set_host_notifier;
    k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1863 1864 1865 1866
    k->save_queue = virtio_ccw_save_queue;
    k->load_queue = virtio_ccw_load_queue;
    k->save_config = virtio_ccw_save_config;
    k->load_config = virtio_ccw_load_config;
1867 1868
    k->device_plugged = virtio_ccw_device_plugged;
    k->device_unplugged = virtio_ccw_device_unplugged;
1869 1870 1871 1872 1873 1874 1875 1876 1877
}

static const TypeInfo virtio_ccw_bus_info = {
    .name = TYPE_VIRTIO_CCW_BUS,
    .parent = TYPE_VIRTIO_BUS,
    .instance_size = sizeof(VirtioCcwBusState),
    .class_init = virtio_ccw_bus_class_init,
};

P
Pierre Morel 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
#ifdef CONFIG_VIRTFS
static Property virtio_ccw_9p_properties[] = {
    DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
            VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
{
    V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
    Error *err = NULL;

    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
    }
}

static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

    k->exit = virtio_ccw_exit;
    k->realize = virtio_ccw_9p_realize;
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_9p_properties;
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
}

static void virtio_ccw_9p_instance_init(Object *obj)
{
    V9fsCCWState *dev = VIRTIO_9P_CCW(obj);

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_9P);
}

static const TypeInfo virtio_ccw_9p_info = {
    .name          = TYPE_VIRTIO_9P_CCW,
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
    .instance_size = sizeof(V9fsCCWState),
    .instance_init = virtio_ccw_9p_instance_init,
    .class_init    = virtio_ccw_9p_class_init,
};
#endif

1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
static void virtio_ccw_register(void)
{
    type_register_static(&virtio_ccw_bus_info);
    type_register_static(&virtual_css_bus_info);
    type_register_static(&virtio_ccw_device_info);
    type_register_static(&virtio_ccw_serial);
    type_register_static(&virtio_ccw_blk);
    type_register_static(&virtio_ccw_net);
    type_register_static(&virtio_ccw_balloon);
    type_register_static(&virtio_ccw_scsi);
1938
#ifdef CONFIG_VHOST_SCSI
1939
    type_register_static(&vhost_ccw_scsi);
1940
#endif
C
Cornelia Huck 已提交
1941
    type_register_static(&virtio_ccw_rng);
1942
    type_register_static(&virtual_css_bridge_info);
P
Pierre Morel 已提交
1943 1944 1945
#ifdef CONFIG_VIRTFS
    type_register_static(&virtio_ccw_9p_info);
#endif
1946 1947 1948
}

type_init(virtio_ccw_register)