virtio-ccw.c 61.2 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
 *
 * 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.
 */

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

32 33
#include "hw/s390x/ioinst.h"
#include "hw/s390x/css.h"
34 35 36
#include "virtio-ccw.h"
#include "trace.h"

37 38
static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtioCcwDevice *dev);
39

P
Paolo Bonzini 已提交
40
static void virtual_css_bus_reset(BusState *qbus)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
    /* 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 已提交
64
    VirtioCcwDevice *dev = sch->driver_data;
65

P
Paolo Bonzini 已提交
66 67
    if (dev) {
        vdev = virtio_bus_get_device(&dev->bus);
68 69 70 71
    }
    return vdev;
}

C
Cornelia Huck 已提交
72 73 74
static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice *dev, int n,
                                              bool assign, bool set_handler)
{
P
Paolo Bonzini 已提交
75 76
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
C
Cornelia Huck 已提交
77 78 79 80 81 82 83 84 85 86 87 88
    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);
89
        r = s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
C
Cornelia Huck 已提交
90 91 92 93 94 95 96 97
        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);
98
        s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
C
Cornelia Huck 已提交
99 100 101 102 103 104 105
        event_notifier_cleanup(notifier);
    }
    return r;
}

static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
{
P
Paolo Bonzini 已提交
106
    VirtIODevice *vdev;
C
Cornelia Huck 已提交
107 108 109
    int n, r;

    if (!(dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) ||
110
        dev->ioeventfd_disabled ||
C
Cornelia Huck 已提交
111 112 113
        dev->ioeventfd_started) {
        return;
    }
P
Paolo Bonzini 已提交
114
    vdev = virtio_bus_get_device(&dev->bus);
115
    for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
116
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
117 118 119 120 121 122 123 124 125 126 127 128
            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 已提交
129
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142
            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 已提交
143
    VirtIODevice *vdev;
C
Cornelia Huck 已提交
144 145 146 147 148
    int n, r;

    if (!dev->ioeventfd_started) {
        return;
    }
P
Paolo Bonzini 已提交
149
    vdev = virtio_bus_get_device(&dev->bus);
150
    for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
151
        if (!virtio_queue_get_num(vdev, n)) {
C
Cornelia Huck 已提交
152 153 154 155 156 157 158 159
            continue;
        }
        r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
        assert(r >= 0);
    }
    dev->ioeventfd_started = false;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
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 */
175
    qbus_set_hotplug_handler(bus, dev, &error_abort);
176 177 178 179 180

    return cbus;
}

/* Communication blocks used by several channel commands. */
181
typedef struct VqInfoBlockLegacy {
182 183 184 185
    uint64_t queue;
    uint32_t align;
    uint16_t index;
    uint16_t num;
186 187 188 189 190 191 192 193 194
} QEMU_PACKED VqInfoBlockLegacy;

typedef struct VqInfoBlock {
    uint64_t desc;
    uint32_t res0;
    uint16_t index;
    uint16_t num;
    uint64_t avail;
    uint64_t used;
195 196 197 198 199 200 201 202 203 204 205 206
} 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;

207 208 209 210 211 212 213
typedef struct VirtioThinintInfo {
    hwaddr summary_indicator;
    hwaddr device_indicator;
    uint64_t ind_bit;
    uint8_t isc;
} QEMU_PACKED VirtioThinintInfo;

214 215 216 217 218 219
typedef struct VirtioRevInfo {
    uint16_t revision;
    uint16_t length;
    uint8_t data[0];
} QEMU_PACKED VirtioRevInfo;

220
/* Specify where the virtqueues for the subchannel are in guest memory. */
221 222
static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
                              VqInfoBlockLegacy *linfo)
223
{
P
Paolo Bonzini 已提交
224
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
225 226 227
    uint16_t index = info ? info->index : linfo->index;
    uint16_t num = info ? info->num : linfo->num;
    uint64_t desc = info ? info->desc : linfo->queue;
228

229
    if (index >= VIRTIO_CCW_QUEUE_MAX) {
230 231 232 233
        return -EINVAL;
    }

    /* Current code in virtio.c relies on 4K alignment. */
234
    if (linfo && desc && (linfo->align != 4096)) {
235 236 237
        return -EINVAL;
    }

P
Paolo Bonzini 已提交
238
    if (!vdev) {
239 240 241
        return -EINVAL;
    }

242 243 244 245 246 247
    if (info) {
        virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
    } else {
        virtio_queue_set_addr(vdev, index, desc);
    }
    if (!desc) {
248
        virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
249
    } else {
250 251 252 253 254 255 256 257 258
        if (info) {
            /* virtio-1 allows changing the ring size. */
            if (virtio_queue_get_num(vdev, index) < num) {
                /* Fail if we exceed the maximum number. */
                return -EINVAL;
            }
            virtio_queue_set_num(vdev, index, num);
        } else if (virtio_queue_get_num(vdev, index) > num) {
            /* Fail if we don't have a big enough queue. */
259 260
            return -EINVAL;
        }
261
        /* We ignore possible increased num for legacy for compatibility. */
P
Paolo Bonzini 已提交
262
        virtio_queue_set_vector(vdev, index, index);
263 264
    }
    /* tell notify handler in case of config change */
265
    vdev->config_vector = VIRTIO_CCW_QUEUE_MAX;
266 267 268
    return 0;
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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;
}

288 289
static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
                                    bool is_legacy)
290 291 292
{
    int ret;
    VqInfoBlock info;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    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;
360
    VirtioRevInfo revinfo;
361 362 363 364 365 366
    uint8_t status;
    VirtioFeatDesc features;
    void *config;
    hwaddr indicators;
    VqConfigBlock vq_config;
    VirtioCcwDevice *dev = sch->driver_data;
P
Paolo Bonzini 已提交
367
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
368 369 370
    bool check_len;
    int len;
    hwaddr hw_len;
371
    VirtioThinintInfo *thinint;
372 373 374 375 376 377 378 379 380 381 382 383

    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:
384
        ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
385 386
        break;
    case CCW_CMD_VDEV_RESET:
387
        virtio_ccw_reset_virtio(dev, vdev);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        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 {
404 405 406 407 408
            features.index = address_space_ldub(&address_space_memory,
                                                ccw.cda
                                                + sizeof(features.features),
                                                MEMTXATTRS_UNSPECIFIED,
                                                NULL);
C
Cornelia Huck 已提交
409
            if (features.index == 0) {
C
Cornelia Huck 已提交
410 411 412 413 414 415 416
                if (dev->revision >= 1) {
                    /* Don't offer legacy features for modern devices. */
                    features.features = (uint32_t)
                        (vdev->host_features & ~VIRTIO_LEGACY_FEATURES);
                } else {
                    features.features = (uint32_t)vdev->host_features;
                }
417
            } else if ((features.index == 1) && (dev->revision >= 1)) {
418
                /*
419 420
                 * Only offer feature bits beyond 31 if the guest has
                 * negotiated at least revision 1.
421
                 */
422
                features.features = (uint32_t)(vdev->host_features >> 32);
423 424 425 426
            } else {
                /* Return zeroes if the guest supports more feature bits. */
                features.features = 0;
            }
427 428 429
            address_space_stl_le(&address_space_memory, ccw.cda,
                                 features.features, MEMTXATTRS_UNSPECIFIED,
                                 NULL);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
            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 {
448 449 450 451 452 453 454 455 456
            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 已提交
457
            if (features.index == 0) {
458 459 460
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0xffffffff00000000ULL) |
                                    features.features);
461
            } else if ((features.index == 1) && (dev->revision >= 1)) {
462
                /*
463 464 465
                 * If the guest did not negotiate at least revision 1,
                 * we did not offer it any feature bits beyond 31. Such a
                 * guest passing us any bit here is therefore buggy.
466 467 468 469
                 */
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0x00000000ffffffffULL) |
                                    ((uint64_t)features.features << 32));
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
            } 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 已提交
487
            if (ccw.count > vdev->config_len) {
488 489 490 491
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
492
        len = MIN(ccw.count, vdev->config_len);
493 494 495
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
P
Paolo Bonzini 已提交
496
            virtio_bus_get_vdev_config(&dev->bus, vdev->config);
497
            /* XXX config space endianness */
P
Paolo Bonzini 已提交
498
            cpu_physical_memory_write(ccw.cda, vdev->config, len);
499 500 501 502 503 504
            sch->curr_status.scsw.count = ccw.count - len;
            ret = 0;
        }
        break;
    case CCW_CMD_WRITE_CONF:
        if (check_len) {
P
Paolo Bonzini 已提交
505
            if (ccw.count > vdev->config_len) {
506 507 508 509
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
510
        len = MIN(ccw.count, vdev->config_len);
511 512 513 514 515 516 517 518 519 520
        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 已提交
521
                memcpy(vdev->config, config, len);
522
                cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
P
Paolo Bonzini 已提交
523
                virtio_bus_set_vdev_config(&dev->bus, vdev->config);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
                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 {
543 544
            status = address_space_ldub(&address_space_memory, ccw.cda,
                                        MEMTXATTRS_UNSPECIFIED, NULL);
C
Cornelia Huck 已提交
545 546 547
            if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
                virtio_ccw_stop_ioeventfd(dev);
            }
548 549
            if (virtio_set_status(vdev, status) == 0) {
                if (vdev->status == 0) {
550
                    virtio_ccw_reset_virtio(dev, vdev);
551 552 553 554 555 556 557 558 559
                }
                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 已提交
560
            }
561 562 563 564 565 566 567 568 569 570 571 572 573
        }
        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;
        }
574 575 576 577 578
        if (sch->thinint_active) {
            /* Trigger a command reject. */
            ret = -ENOSYS;
            break;
        }
579
        if (!ccw.cda) {
580 581
            ret = -EFAULT;
        } else {
582 583
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
584
            dev->indicators = get_indicator(indicators, sizeof(uint64_t));
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
            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;
        }
600
        if (!ccw.cda) {
601 602
            ret = -EFAULT;
        } else {
603 604
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
605
            dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
            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 {
624 625 626 627
            vq_config.index = address_space_lduw_be(&address_space_memory,
                                                    ccw.cda,
                                                    MEMTXATTRS_UNSPECIFIED,
                                                    NULL);
628
            if (vq_config.index >= VIRTIO_CCW_QUEUE_MAX) {
629 630 631
                ret = -EINVAL;
                break;
            }
P
Paolo Bonzini 已提交
632
            vq_config.num_max = virtio_queue_get_num(vdev,
633
                                                     vq_config.index);
634 635 636 637 638
            address_space_stw_be(&address_space_memory,
                                 ccw.cda + sizeof(vq_config.index),
                                 vq_config.num_max,
                                 MEMTXATTRS_UNSPECIFIED,
                                 NULL);
639 640 641 642
            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
            ret = 0;
        }
        break;
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    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 已提交
666 667
                uint64_t ind_bit = ldq_be_p(&thinint->ind_bit);

668
                len = hw_len;
669
                dev->summary_indicator =
C
Cornelia Huck 已提交
670 671 672 673 674
                    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);
675
                dev->thinint_isc = thinint->isc;
C
Cornelia Huck 已提交
676
                dev->routes.adapter.ind_offset = ind_bit;
677
                dev->routes.adapter.summary_offset = 7;
678
                cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len);
679 680
                ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
                                              dev->thinint_isc, true, false,
681
                                              &dev->routes.adapter.adapter_id);
682
                assert(ret == 0);
683 684
                sch->thinint_active = ((dev->indicators != NULL) &&
                                       (dev->summary_indicator != NULL));
685 686 687 688 689
                sch->curr_status.scsw.count = ccw.count - len;
                ret = 0;
            }
        }
        break;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    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 ||
C
Cornelia Huck 已提交
717
            revinfo.revision > virtio_ccw_rev_max(dev)) {
718 719 720 721 722 723
            ret = -ENOSYS;
            break;
        }
        ret = 0;
        dev->revision = revinfo.revision;
        break;
724
    default:
C
Cornelia Huck 已提交
725
        ret = -ENOSYS;
726 727 728 729 730
        break;
    }
    return ret;
}

731 732 733 734 735 736 737
static void virtio_sch_disable_cb(SubchDev *sch)
{
    VirtioCcwDevice *dev = sch->driver_data;

    dev->revision = -1;
}

738
static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
739 740 741 742
{
    unsigned int schid;
    bool found = false;
    SubchDev *sch;
743 744
    Error *err = NULL;
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
745 746 747 748 749 750

    sch = g_malloc0(sizeof(SubchDev));

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

751
    dev->indicators = NULL;
752 753 754 755

    /* Initialize subchannel structure. */
    sch->channel_prog = 0x0;
    sch->last_cmd_valid = false;
756
    sch->thinint_active = false;
757 758 759 760
    /*
     * Use a device number if provided. Otherwise, fall back to subchannel
     * number.
     */
761 762 763 764 765 766 767 768 769
    if (dev->bus_id.valid) {
        /* Enforce use of virtual cssid. */
        if (dev->bus_id.cssid != VIRTUAL_CSSID) {
            error_setg(errp, "cssid %x not valid for virtio devices",
                       dev->bus_id.cssid);
            goto out_err;
        }
        if (css_devno_used(dev->bus_id.cssid, dev->bus_id.ssid,
                           dev->bus_id.devid)) {
770
                error_setg(errp, "Device %x.%x.%04x already exists",
771 772
                           dev->bus_id.cssid, dev->bus_id.ssid,
                           dev->bus_id.devid);
773 774
                goto out_err;
        }
775 776 777
        sch->cssid = dev->bus_id.cssid;
        sch->ssid = dev->bus_id.ssid;
        sch->devno = dev->bus_id.devid;
778

779
        /* Find the next free id. */
780
        for (schid = 0; schid <= MAX_SCHID; schid++) {
781
            if (!css_find_subch(1, sch->cssid, sch->ssid, schid)) {
782
                sch->schid = schid;
783 784
                css_subch_assign(sch->cssid, sch->ssid, sch->schid,
                                 sch->devno, sch);
785 786 787 788 789
                found = true;
                break;
            }
        }
        if (!found) {
790
            error_setg(errp, "No free subchannel found for %x.%x.%04x",
791
                       sch->cssid, sch->ssid, sch->devno);
792 793
            goto out_err;
        }
794 795
        trace_virtio_ccw_new_device(sch->cssid, sch->ssid, sch->schid,
                                    sch->devno, "user-configured");
796
    } else {
797 798
        unsigned int cssid = VIRTUAL_CSSID, ssid, devno;

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
        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) {
814
                            error_setg(errp, "No free devno found");
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
                            goto out_err;
                        } else {
                            devno++;
                        }
                    }
                    sch->devno = devno;
                    css_subch_assign(cssid, ssid, schid, devno, sch);
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        if (!found) {
831
            error_setg(errp, "Virtual channel subsystem is full!");
832 833 834 835 836 837 838 839 840 841
            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;
842
    sch->disable_cb = virtio_sch_disable_cb;
843 844 845 846 847

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

849 850
    dev->revision = -1;

851 852 853 854 855
    if (k->realize) {
        k->realize(dev, &err);
    }
    if (err) {
        error_propagate(errp, err);
856
        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
857 858 859
        goto out_err;
    }

860
    return;
861 862 863 864 865 866 867 868 869 870 871 872 873 874

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);
    }
875
    if (dev->indicators) {
876
        release_indicator(&dev->routes.adapter, dev->indicators);
877 878
        dev->indicators = NULL;
    }
879 880 881
    return 0;
}

882
static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
883
{
884
    DeviceState *qdev = DEVICE(ccw_dev);
885 886
    VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
887

888 889
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
890
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
891
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
892 893
}

894
static void virtio_ccw_net_instance_init(Object *obj)
895
{
896
    VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
897 898 899

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
900 901
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
902 903
}

904
static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
905
{
906 907
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
908

909
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
910
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
911 912
}

913
static void virtio_ccw_blk_instance_init(Object *obj)
914
{
915
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
916 917 918

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
919 920
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
                              &error_abort);
921 922
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
923 924
}

925
static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
926
{
927 928
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
929 930 931 932 933 934 935 936 937 938 939 940
    DeviceState *proxy = DEVICE(ccw_dev);
    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);
    }
941

942
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
943
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
944 945
}

946 947

static void virtio_ccw_serial_instance_init(Object *obj)
948
{
949
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
950 951 952

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
953 954
}

955
static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
956
{
957 958
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
959

960
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
961
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
962 963
}

964
static void virtio_ccw_balloon_instance_init(Object *obj)
965
{
966
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
967

968 969
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
970 971 972 973 974
    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);
975 976
}

977
static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
978
{
979 980
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
981 982 983 984 985 986 987 988 989 990 991 992
    DeviceState *qdev = DEVICE(ccw_dev);
    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);
    }
993

994
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
995
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
996 997
}

998
static void virtio_ccw_scsi_instance_init(Object *obj)
999
{
1000
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
1001 1002 1003

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
1004 1005
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
                              &error_abort);
1006 1007
}

1008
#ifdef CONFIG_VHOST_SCSI
1009
static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1010 1011 1012 1013 1014
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1015
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1016 1017 1018 1019 1020
}

static void vhost_ccw_scsi_instance_init(Object *obj)
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
1021 1022 1023

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
1024 1025 1026
}
#endif

1027
static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
C
Cornelia Huck 已提交
1028
{
1029 1030
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1031
    Error *err = NULL;
C
Cornelia Huck 已提交
1032

1033
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1034 1035 1036 1037
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
C
Cornelia Huck 已提交
1038 1039
    }

1040
    object_property_set_link(OBJECT(dev),
1041
                             OBJECT(dev->vdev.conf.rng), "rng",
1042
                             NULL);
C
Cornelia Huck 已提交
1043 1044
}

1045 1046 1047 1048 1049 1050 1051 1052
/* 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);
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
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);
1070
    trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
1071 1072 1073 1074 1075
    cpu_physical_memory_unmap(ind_addr, len, 1, len);

    return ind_old;
}

1076 1077 1078 1079 1080 1081
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;

1082 1083
    /* queue indicators + secondary indicators */
    if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) {
1084 1085 1086
        return;
    }

1087
    if (vector < VIRTIO_CCW_QUEUE_MAX) {
1088 1089 1090
        if (!dev->indicators) {
            return;
        }
1091 1092 1093 1094 1095 1096 1097
        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.
             */
1098 1099
            uint64_t ind_bit = dev->routes.adapter.ind_offset;

1100
            virtio_set_ind_atomic(sch, dev->indicators->addr +
1101 1102
                                  (ind_bit + vector) / 8,
                                  0x80 >> ((ind_bit + vector) % 8));
1103
            if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1104 1105 1106 1107
                                       0x01)) {
                css_adapter_interrupt(dev->thinint_isc);
            }
        } else {
1108 1109 1110 1111
            indicators = address_space_ldq(&address_space_memory,
                                           dev->indicators->addr,
                                           MEMTXATTRS_UNSPECIFIED,
                                           NULL);
1112
            indicators |= 1ULL << vector;
1113 1114
            address_space_stq(&address_space_memory, dev->indicators->addr,
                              indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1115 1116
            css_conditional_io_interrupt(sch);
        }
1117
    } else {
1118 1119 1120
        if (!dev->indicators2) {
            return;
        }
1121
        vector = 0;
1122 1123 1124 1125
        indicators = address_space_ldq(&address_space_memory,
                                       dev->indicators2->addr,
                                       MEMTXATTRS_UNSPECIFIED,
                                       NULL);
1126
        indicators |= 1ULL << vector;
1127 1128
        address_space_stq(&address_space_memory, dev->indicators2->addr,
                          indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1129
        css_conditional_io_interrupt(sch);
1130 1131 1132 1133 1134 1135
    }
}

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

1138
    virtio_ccw_reset_virtio(dev, vdev);
1139 1140 1141
    css_reset_sch(dev->sch);
}

C
Cornelia Huck 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
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);
    }
}

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
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);
}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
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);

1230 1231
    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
                                              dev->routes.gsi[n]);
1232 1233 1234 1235 1236 1237 1238 1239 1240
}

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;

1241 1242
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
                                                dev->routes.gsi[n]);
1243 1244 1245
    assert(ret == 0);
}

1246 1247 1248
static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
                                         bool assign, bool with_irqfd)
{
P
Paolo Bonzini 已提交
1249 1250
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
1251
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
P
Paolo Bonzini 已提交
1252
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1253 1254 1255 1256 1257 1258 1259 1260

    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);
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        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.
1272 1273
         */
        if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
1274
            k->guest_notifier_mask(vdev, n, false);
1275 1276 1277
        }
        /* get lost events and re-inject */
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
1278
            k->guest_notifier_pending(vdev, n)) {
1279 1280 1281 1282
            event_notifier_set(notifier);
        }
    } else {
        if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
1283
            k->guest_notifier_mask(vdev, n, true);
1284
        }
1285 1286 1287
        if (with_irqfd) {
            virtio_ccw_remove_irqfd(dev, n);
        }
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
        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 已提交
1298
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1299
    bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1300 1301
    int r, n;

1302 1303 1304 1305 1306 1307 1308
    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;
        }
    }
1309 1310 1311 1312
    for (n = 0; n < nvqs; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }
1313
        r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1314 1315 1316 1317
        if (r < 0) {
            goto assign_error;
        }
    }
1318 1319 1320 1321
    if (with_irqfd && !assigned) {
        /* release irq routes after irqfds have been released */
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1322 1323 1324 1325 1326 1327
    return 0;

assign_error:
    while (--n >= 0) {
        virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
    }
1328 1329 1330 1331
irqroute_error:
    if (with_irqfd && assigned) {
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1332 1333 1334
    return r;
}

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
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;
1359
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

    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);
    }
1383
    qemu_put_be16(f, vdev->config_vector);
1384 1385
    qemu_put_be64(f, dev->routes.adapter.ind_offset);
    qemu_put_byte(f, dev->thinint_isc);
C
Cornelia Huck 已提交
1386
    qemu_put_be32(f, dev->revision);
1387 1388 1389 1390 1391 1392
}

static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
    SubchDev *s = dev->sch;
1393
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    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;
    }
1419
    qemu_get_be16s(f, &vdev->config_vector);
1420 1421
    dev->routes.adapter.ind_offset = qemu_get_be64(f);
    dev->thinint_isc = qemu_get_byte(f);
1422
    dev->revision = qemu_get_be32(f);
1423 1424 1425 1426 1427 1428 1429 1430 1431
    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;
}

1432
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1433
static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1434 1435
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1436
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1437
    SubchDev *sch = dev->sch;
1438 1439 1440
    int n = virtio_get_num_queues(vdev);

    if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
M
Michael Tokarev 已提交
1441
        error_setg(errp, "The number of virtqueues %d "
1442 1443 1444 1445
                   "exceeds ccw limit %d", n,
                   VIRTIO_CCW_QUEUE_MAX);
        return;
    }
1446

1447 1448 1449 1450
    if (!kvm_eventfds_enabled()) {
        dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
    }

1451 1452
    sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);

C
Cornelia Huck 已提交
1453 1454 1455 1456
    if (dev->max_rev >= 1) {
        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
    }

1457 1458 1459 1460
    css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
                          d->hotplugged, 1);
}

1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
static void virtio_ccw_post_plugged(DeviceState *d, Error **errp)
{
   VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
   VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);

   if (!virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1)) {
        /* A backend didn't support modern virtio. */
       dev->max_rev = 0;
   }
}

1472 1473 1474 1475 1476 1477
static void virtio_ccw_device_unplugged(DeviceState *d)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    virtio_ccw_stop_ioeventfd(dev);
}
1478 1479 1480
/**************** Virtio-ccw Bus Device Descriptions *******************/

static Property virtio_ccw_net_properties[] = {
1481
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1482 1483
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1484 1485
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1486 1487 1488 1489 1490 1491 1492 1493
    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);

1494
    k->realize = virtio_ccw_net_realize;
1495
    k->exit = virtio_ccw_exit;
1496 1497
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_net_properties;
C
Cornelia Huck 已提交
1498
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1499 1500 1501
}

static const TypeInfo virtio_ccw_net = {
1502
    .name          = TYPE_VIRTIO_NET_CCW,
1503
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1504 1505
    .instance_size = sizeof(VirtIONetCcw),
    .instance_init = virtio_ccw_net_instance_init,
1506 1507 1508 1509
    .class_init    = virtio_ccw_net_class_init,
};

static Property virtio_ccw_blk_properties[] = {
1510
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1511 1512
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1513 1514
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1515 1516 1517 1518 1519 1520 1521 1522
    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);

1523
    k->realize = virtio_ccw_blk_realize;
1524
    k->exit = virtio_ccw_exit;
1525 1526
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_blk_properties;
C
Cornelia Huck 已提交
1527
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1528 1529 1530
}

static const TypeInfo virtio_ccw_blk = {
1531
    .name          = TYPE_VIRTIO_BLK_CCW,
1532
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1533 1534
    .instance_size = sizeof(VirtIOBlkCcw),
    .instance_init = virtio_ccw_blk_instance_init,
1535 1536 1537 1538
    .class_init    = virtio_ccw_blk_class_init,
};

static Property virtio_ccw_serial_properties[] = {
1539
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1540 1541
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1542 1543
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1544 1545 1546 1547 1548 1549 1550 1551
    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);

1552
    k->realize = virtio_ccw_serial_realize;
1553
    k->exit = virtio_ccw_exit;
1554 1555
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_serial_properties;
C
Cornelia Huck 已提交
1556
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1557 1558 1559
}

static const TypeInfo virtio_ccw_serial = {
1560
    .name          = TYPE_VIRTIO_SERIAL_CCW,
1561
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1562 1563
    .instance_size = sizeof(VirtioSerialCcw),
    .instance_init = virtio_ccw_serial_instance_init,
1564 1565 1566 1567
    .class_init    = virtio_ccw_serial_class_init,
};

static Property virtio_ccw_balloon_properties[] = {
1568
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1569 1570
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1571 1572
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1573 1574 1575 1576 1577 1578 1579 1580
    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);

1581
    k->realize = virtio_ccw_balloon_realize;
1582
    k->exit = virtio_ccw_exit;
1583 1584
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_balloon_properties;
C
Cornelia Huck 已提交
1585
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1586 1587 1588
}

static const TypeInfo virtio_ccw_balloon = {
1589
    .name          = TYPE_VIRTIO_BALLOON_CCW,
1590
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1591 1592
    .instance_size = sizeof(VirtIOBalloonCcw),
    .instance_init = virtio_ccw_balloon_instance_init,
1593 1594 1595 1596
    .class_init    = virtio_ccw_balloon_class_init,
};

static Property virtio_ccw_scsi_properties[] = {
1597
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1598 1599
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1600 1601
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1602 1603 1604 1605 1606 1607 1608 1609
    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);

1610
    k->realize = virtio_ccw_scsi_realize;
1611
    k->exit = virtio_ccw_exit;
1612 1613
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_scsi_properties;
C
Cornelia Huck 已提交
1614
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1615 1616 1617
}

static const TypeInfo virtio_ccw_scsi = {
1618
    .name          = TYPE_VIRTIO_SCSI_CCW,
1619
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1620 1621
    .instance_size = sizeof(VirtIOSCSICcw),
    .instance_init = virtio_ccw_scsi_instance_init,
1622 1623 1624
    .class_init    = virtio_ccw_scsi_class_init,
};

1625 1626
#ifdef CONFIG_VHOST_SCSI
static Property vhost_ccw_scsi_properties[] = {
1627
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1628 1629
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1630 1631 1632 1633 1634 1635 1636 1637
    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);

1638
    k->realize = vhost_ccw_scsi_realize;
1639 1640 1641
    k->exit = virtio_ccw_exit;
    dc->reset = virtio_ccw_reset;
    dc->props = vhost_ccw_scsi_properties;
C
Cornelia Huck 已提交
1642
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1643 1644 1645 1646 1647
}

static const TypeInfo vhost_ccw_scsi = {
    .name          = TYPE_VHOST_SCSI_CCW,
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1648
    .instance_size = sizeof(VHostSCSICcw),
1649 1650 1651 1652 1653
    .instance_init = vhost_ccw_scsi_instance_init,
    .class_init    = vhost_ccw_scsi_class_init,
};
#endif

1654
static void virtio_ccw_rng_instance_init(Object *obj)
C
Cornelia Huck 已提交
1655
{
1656
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1657 1658 1659

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
1660 1661
    object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
                              "rng", &error_abort);
C
Cornelia Huck 已提交
1662 1663 1664
}

static Property virtio_ccw_rng_properties[] = {
1665
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
C
Cornelia Huck 已提交
1666 1667
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1668 1669
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
C
Cornelia Huck 已提交
1670 1671 1672 1673 1674 1675 1676 1677
    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);

1678
    k->realize = virtio_ccw_rng_realize;
1679
    k->exit = virtio_ccw_exit;
C
Cornelia Huck 已提交
1680 1681
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_rng_properties;
C
Cornelia Huck 已提交
1682
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
C
Cornelia Huck 已提交
1683 1684 1685
}

static const TypeInfo virtio_ccw_rng = {
1686
    .name          = TYPE_VIRTIO_RNG_CCW,
C
Cornelia Huck 已提交
1687
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1688 1689
    .instance_size = sizeof(VirtIORNGCcw),
    .instance_init = virtio_ccw_rng_instance_init,
C
Cornelia Huck 已提交
1690 1691 1692
    .class_init    = virtio_ccw_rng_class_init,
};

1693
static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1694 1695 1696
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;

1697
    virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1698
    virtio_ccw_device_realize(_dev, errp);
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
}

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

    return _info->exit(_dev);
}

1709 1710
static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
                                     DeviceState *dev, Error **errp)
1711 1712 1713 1714
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
    SubchDev *sch = _dev->sch;

1715 1716
    virtio_ccw_stop_ioeventfd(_dev);

1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    /*
     * 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);

1728
    object_unparent(OBJECT(dev));
1729 1730 1731 1732 1733 1734
}

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

1735
    dc->realize = virtio_ccw_busdev_realize;
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
    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);
1761
    HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
C
Cornelia Huck 已提交
1762
    DeviceClass *dc = DEVICE_CLASS(klass);
1763 1764

    k->init = virtual_css_bridge_init;
1765
    hc->unplug = virtio_ccw_busdev_unplug;
C
Cornelia Huck 已提交
1766
    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1767 1768 1769 1770 1771 1772 1773
}

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,
1774 1775 1776 1777
    .interfaces = (InterfaceInfo[]) {
        { TYPE_HOTPLUG_HANDLER },
        { }
    }
1778 1779 1780 1781
};

/* virtio-ccw-bus */

1782 1783
static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtioCcwDevice *dev)
1784 1785
{
    DeviceState *qdev = DEVICE(dev);
1786
    char virtio_bus_name[] = "virtio-bus";
1787

1788 1789
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
                        qdev, virtio_bus_name);
1790 1791 1792 1793 1794 1795 1796 1797 1798
}

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 已提交
1799
    k->vmstate_change = virtio_ccw_vmstate_change;
1800 1801 1802
    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;
1803 1804 1805 1806
    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;
1807
    k->device_plugged = virtio_ccw_device_plugged;
1808
    k->post_plugged = virtio_ccw_post_plugged;
1809
    k->device_unplugged = virtio_ccw_device_unplugged;
1810 1811 1812 1813 1814 1815 1816 1817 1818
}

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 已提交
1819 1820
#ifdef CONFIG_VIRTFS
static Property virtio_ccw_9p_properties[] = {
1821
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
P
Pierre Morel 已提交
1822 1823
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
            VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1824 1825
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
P
Pierre Morel 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834
    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);

    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1835
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
P
Pierre Morel 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
}

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

1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
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);
1877
#ifdef CONFIG_VHOST_SCSI
1878
    type_register_static(&vhost_ccw_scsi);
1879
#endif
C
Cornelia Huck 已提交
1880
    type_register_static(&virtio_ccw_rng);
1881
    type_register_static(&virtual_css_bridge_info);
P
Pierre Morel 已提交
1882 1883 1884
#ifdef CONFIG_VIRTFS
    type_register_static(&virtio_ccw_9p_info);
#endif
1885 1886 1887
}

type_init(virtio_ccw_register)