virtio-ccw.c 56.5 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
#include "virtio-ccw.h"
#include "trace.h"
36
#include "hw/s390x/css-bridge.h"
37

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

41 42 43
VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
{
    VirtIODevice *vdev = NULL;
P
Paolo Bonzini 已提交
44
    VirtioCcwDevice *dev = sch->driver_data;
45

P
Paolo Bonzini 已提交
46 47
    if (dev) {
        vdev = virtio_bus_get_device(&dev->bus);
48 49 50 51
    }
    return vdev;
}

52
static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
C
Cornelia Huck 已提交
53
{
54 55
    virtio_bus_start_ioeventfd(&dev->bus);
}
C
Cornelia Huck 已提交
56

57 58 59
static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
{
    virtio_bus_stop_ioeventfd(&dev->bus);
C
Cornelia Huck 已提交
60 61
}

62
static bool virtio_ccw_ioeventfd_enabled(DeviceState *d)
C
Cornelia Huck 已提交
63
{
64
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
C
Cornelia Huck 已提交
65

66
    return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0;
67 68 69 70 71 72
}

static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
                                       int n, bool assign)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
73 74
    CcwDevice *ccw_dev = CCW_DEVICE(dev);
    SubchDev *sch = ccw_dev->sch;
75 76 77
    uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;

    return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
C
Cornelia Huck 已提交
78 79
}

80
/* Communication blocks used by several channel commands. */
81
typedef struct VqInfoBlockLegacy {
82 83 84 85
    uint64_t queue;
    uint32_t align;
    uint16_t index;
    uint16_t num;
86 87 88 89 90 91 92 93 94
} QEMU_PACKED VqInfoBlockLegacy;

typedef struct VqInfoBlock {
    uint64_t desc;
    uint32_t res0;
    uint16_t index;
    uint16_t num;
    uint64_t avail;
    uint64_t used;
95 96 97 98 99 100 101 102 103 104 105 106
} 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;

107 108 109 110 111 112 113
typedef struct VirtioThinintInfo {
    hwaddr summary_indicator;
    hwaddr device_indicator;
    uint64_t ind_bit;
    uint8_t isc;
} QEMU_PACKED VirtioThinintInfo;

114 115 116 117 118 119
typedef struct VirtioRevInfo {
    uint16_t revision;
    uint16_t length;
    uint8_t data[0];
} QEMU_PACKED VirtioRevInfo;

120
/* Specify where the virtqueues for the subchannel are in guest memory. */
121 122
static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
                              VqInfoBlockLegacy *linfo)
123
{
P
Paolo Bonzini 已提交
124
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
125 126 127
    uint16_t index = info ? info->index : linfo->index;
    uint16_t num = info ? info->num : linfo->num;
    uint64_t desc = info ? info->desc : linfo->queue;
128

129
    if (index >= VIRTIO_CCW_QUEUE_MAX) {
130 131 132 133
        return -EINVAL;
    }

    /* Current code in virtio.c relies on 4K alignment. */
134
    if (linfo && desc && (linfo->align != 4096)) {
135 136 137
        return -EINVAL;
    }

P
Paolo Bonzini 已提交
138
    if (!vdev) {
139 140 141
        return -EINVAL;
    }

142 143 144 145 146 147
    if (info) {
        virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
    } else {
        virtio_queue_set_addr(vdev, index, desc);
    }
    if (!desc) {
148
        virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
149
    } else {
150 151
        if (info) {
            /* virtio-1 allows changing the ring size. */
M
Michael S. Tsirkin 已提交
152
            if (virtio_queue_get_max_num(vdev, index) < num) {
153 154 155 156 157 158
                /* 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. */
159 160
            return -EINVAL;
        }
161
        /* We ignore possible increased num for legacy for compatibility. */
P
Paolo Bonzini 已提交
162
        virtio_queue_set_vector(vdev, index, index);
163 164
    }
    /* tell notify handler in case of config change */
165
    vdev->config_vector = VIRTIO_CCW_QUEUE_MAX;
166 167 168
    return 0;
}

169 170
static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
{
171 172
    CcwDevice *ccw_dev = CCW_DEVICE(dev);

173 174 175 176 177 178 179 180 181 182 183 184 185 186
    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;
    }
187
    ccw_dev->sch->thinint_active = false;
188 189
}

190 191
static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
                                    bool is_legacy)
192 193 194
{
    int ret;
    VqInfoBlock info;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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;
262
    VirtioRevInfo revinfo;
263 264 265 266 267 268
    uint8_t status;
    VirtioFeatDesc features;
    void *config;
    hwaddr indicators;
    VqConfigBlock vq_config;
    VirtioCcwDevice *dev = sch->driver_data;
P
Paolo Bonzini 已提交
269
    VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
270 271 272
    bool check_len;
    int len;
    hwaddr hw_len;
273
    VirtioThinintInfo *thinint;
274 275 276 277 278 279 280 281 282 283 284 285

    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:
286
        ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
287 288
        break;
    case CCW_CMD_VDEV_RESET:
289
        virtio_ccw_reset_virtio(dev, vdev);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        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 {
306 307
            VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

308 309 310 311 312
            features.index = address_space_ldub(&address_space_memory,
                                                ccw.cda
                                                + sizeof(features.features),
                                                MEMTXATTRS_UNSPECIFIED,
                                                NULL);
C
Cornelia Huck 已提交
313
            if (features.index == 0) {
C
Cornelia Huck 已提交
314 315 316
                if (dev->revision >= 1) {
                    /* Don't offer legacy features for modern devices. */
                    features.features = (uint32_t)
317
                        (vdev->host_features & ~vdc->legacy_features);
C
Cornelia Huck 已提交
318 319 320
                } else {
                    features.features = (uint32_t)vdev->host_features;
                }
321
            } else if ((features.index == 1) && (dev->revision >= 1)) {
322
                /*
323 324
                 * Only offer feature bits beyond 31 if the guest has
                 * negotiated at least revision 1.
325
                 */
326
                features.features = (uint32_t)(vdev->host_features >> 32);
327 328 329 330
            } else {
                /* Return zeroes if the guest supports more feature bits. */
                features.features = 0;
            }
331 332 333
            address_space_stl_le(&address_space_memory, ccw.cda,
                                 features.features, MEMTXATTRS_UNSPECIFIED,
                                 NULL);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
            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 {
352 353 354 355 356 357 358 359 360
            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 已提交
361
            if (features.index == 0) {
362 363 364
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0xffffffff00000000ULL) |
                                    features.features);
365
            } else if ((features.index == 1) && (dev->revision >= 1)) {
366
                /*
367 368 369
                 * 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.
370 371 372 373
                 */
                virtio_set_features(vdev,
                                    (vdev->guest_features & 0x00000000ffffffffULL) |
                                    ((uint64_t)features.features << 32));
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
            } 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 已提交
391
            if (ccw.count > vdev->config_len) {
392 393 394 395
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
396
        len = MIN(ccw.count, vdev->config_len);
397 398 399
        if (!ccw.cda) {
            ret = -EFAULT;
        } else {
P
Paolo Bonzini 已提交
400
            virtio_bus_get_vdev_config(&dev->bus, vdev->config);
401
            /* XXX config space endianness */
P
Paolo Bonzini 已提交
402
            cpu_physical_memory_write(ccw.cda, vdev->config, len);
403 404 405 406 407 408
            sch->curr_status.scsw.count = ccw.count - len;
            ret = 0;
        }
        break;
    case CCW_CMD_WRITE_CONF:
        if (check_len) {
P
Paolo Bonzini 已提交
409
            if (ccw.count > vdev->config_len) {
410 411 412 413
                ret = -EINVAL;
                break;
            }
        }
P
Paolo Bonzini 已提交
414
        len = MIN(ccw.count, vdev->config_len);
415 416 417 418 419 420 421 422 423 424
        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 已提交
425
                memcpy(vdev->config, config, len);
426
                cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
P
Paolo Bonzini 已提交
427
                virtio_bus_set_vdev_config(&dev->bus, vdev->config);
428 429 430 431 432
                sch->curr_status.scsw.count = ccw.count - len;
                ret = 0;
            }
        }
        break;
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    case CCW_CMD_READ_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 {
            address_space_stb(&address_space_memory, ccw.cda, vdev->status,
                                        MEMTXATTRS_UNSPECIFIED, NULL);
            sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status);;
            ret = 0;
        }
        break;
453 454 455 456 457 458 459 460 461 462 463 464 465 466
    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 {
467 468
            status = address_space_ldub(&address_space_memory, ccw.cda,
                                        MEMTXATTRS_UNSPECIFIED, NULL);
C
Cornelia Huck 已提交
469 470 471
            if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
                virtio_ccw_stop_ioeventfd(dev);
            }
472 473
            if (virtio_set_status(vdev, status) == 0) {
                if (vdev->status == 0) {
474
                    virtio_ccw_reset_virtio(dev, vdev);
475 476 477 478 479 480 481 482 483
                }
                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 已提交
484
            }
485 486 487 488 489 490 491 492 493 494 495 496 497
        }
        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;
        }
498 499 500 501 502
        if (sch->thinint_active) {
            /* Trigger a command reject. */
            ret = -ENOSYS;
            break;
        }
503
        if (!ccw.cda) {
504 505
            ret = -EFAULT;
        } else {
506 507
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
508
            dev->indicators = get_indicator(indicators, sizeof(uint64_t));
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
            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;
        }
524
        if (!ccw.cda) {
525 526
            ret = -EFAULT;
        } else {
527 528
            indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
                                              MEMTXATTRS_UNSPECIFIED, NULL);
529
            dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
            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 {
548 549 550 551
            vq_config.index = address_space_lduw_be(&address_space_memory,
                                                    ccw.cda,
                                                    MEMTXATTRS_UNSPECIFIED,
                                                    NULL);
552
            if (vq_config.index >= VIRTIO_CCW_QUEUE_MAX) {
553 554 555
                ret = -EINVAL;
                break;
            }
P
Paolo Bonzini 已提交
556
            vq_config.num_max = virtio_queue_get_num(vdev,
557
                                                     vq_config.index);
558 559 560 561 562
            address_space_stw_be(&address_space_memory,
                                 ccw.cda + sizeof(vq_config.index),
                                 vq_config.num_max,
                                 MEMTXATTRS_UNSPECIFIED,
                                 NULL);
563 564 565 566
            sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
            ret = 0;
        }
        break;
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    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 已提交
590 591
                uint64_t ind_bit = ldq_be_p(&thinint->ind_bit);

592
                len = hw_len;
593
                dev->summary_indicator =
C
Cornelia Huck 已提交
594 595 596 597 598
                    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);
599
                dev->thinint_isc = thinint->isc;
C
Cornelia Huck 已提交
600
                dev->routes.adapter.ind_offset = ind_bit;
601
                dev->routes.adapter.summary_offset = 7;
602
                cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len);
603 604
                ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
                                              dev->thinint_isc, true, false,
605
                                              &dev->routes.adapter.adapter_id);
606
                assert(ret == 0);
607 608
                sch->thinint_active = ((dev->indicators != NULL) &&
                                       (dev->summary_indicator != NULL));
609 610 611 612 613
                sch->curr_status.scsw.count = ccw.count - len;
                ret = 0;
            }
        }
        break;
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    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 已提交
641
            revinfo.revision > virtio_ccw_rev_max(dev)) {
642 643 644 645 646 647
            ret = -ENOSYS;
            break;
        }
        ret = 0;
        dev->revision = revinfo.revision;
        break;
648
    default:
C
Cornelia Huck 已提交
649
        ret = -ENOSYS;
650 651 652 653 654
        break;
    }
    return ret;
}

655 656 657 658 659 660 661
static void virtio_sch_disable_cb(SubchDev *sch)
{
    VirtioCcwDevice *dev = sch->driver_data;

    dev->revision = -1;
}

662
static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
663
{
664
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
665 666
    CcwDevice *ccw_dev = CCW_DEVICE(dev);
    SubchDev *sch = css_create_virtual_sch(ccw_dev->bus_id, errp);
667
    Error *err = NULL;
668

669 670
    if (!sch) {
        return;
671 672
    }

673
    sch->driver_data = dev;
674
    sch->ccw_cb = virtio_ccw_cb;
675
    sch->disable_cb = virtio_sch_disable_cb;
676 677
    sch->id.reserved = 0xff;
    sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
678
    ccw_dev->sch = sch;
679
    dev->indicators = NULL;
680
    dev->revision = -1;
681 682 683 684
    css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);

    trace_virtio_ccw_new_device(
        sch->cssid, sch->ssid, sch->schid, sch->devno,
685
        ccw_dev->bus_id.valid ? "user-configured" : "auto-configured");
686

687 688 689 690
    if (!kvm_eventfds_enabled()) {
        dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
    }

691 692 693 694 695
    if (k->realize) {
        k->realize(dev, &err);
    }
    if (err) {
        error_propagate(errp, err);
696
        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
697
        ccw_dev->sch = NULL;
698
        g_free(sch);
699
    }
700 701 702 703
}

static int virtio_ccw_exit(VirtioCcwDevice *dev)
{
704 705
    CcwDevice *ccw_dev = CCW_DEVICE(dev);
    SubchDev *sch = ccw_dev->sch;
706 707 708 709 710

    if (sch) {
        css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
        g_free(sch);
    }
711
    if (dev->indicators) {
712
        release_indicator(&dev->routes.adapter, dev->indicators);
713 714
        dev->indicators = NULL;
    }
715 716 717
    return 0;
}

718
static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
719
{
720
    DeviceState *qdev = DEVICE(ccw_dev);
721 722
    VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
723

724 725
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
726
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
727
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
728 729
}

730
static void virtio_ccw_net_instance_init(Object *obj)
731
{
732
    VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
733 734 735

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
736 737
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
738 739
}

740
static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
741
{
742 743
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
744

745
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
746
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
747 748
}

749
static void virtio_ccw_blk_instance_init(Object *obj)
750
{
751
    VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
752 753 754

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
755 756
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
                              &error_abort);
757 758
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
759 760
}

761
static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
762
{
763 764
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
765 766 767 768 769 770 771 772 773 774 775 776
    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);
    }
777

778
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
779
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
780 781
}

782 783

static void virtio_ccw_serial_instance_init(Object *obj)
784
{
785
    VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
786 787 788

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
789 790
}

791
static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
792
{
793 794
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
795

796
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
797
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
798 799
}

800
static void virtio_ccw_balloon_instance_init(Object *obj)
801
{
802
    VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
803

804 805
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
806 807 808 809 810
    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);
811 812
}

813
static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
814
{
815 816
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
817 818 819 820 821 822 823 824 825 826 827 828
    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);
    }
829

830
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
831
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
832 833
}

834
static void virtio_ccw_scsi_instance_init(Object *obj)
835
{
836
    VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
837 838 839

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
840 841
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
                              &error_abort);
842 843
}

844
#ifdef CONFIG_VHOST_SCSI
845
static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
846 847 848 849 850
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
851
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
852 853 854 855 856
}

static void vhost_ccw_scsi_instance_init(Object *obj)
{
    VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
857 858 859

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
860 861 862
}
#endif

863
static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
C
Cornelia Huck 已提交
864
{
865 866
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
867
    Error *err = NULL;
C
Cornelia Huck 已提交
868

869
    qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
870 871 872 873
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
C
Cornelia Huck 已提交
874 875
    }

876
    object_property_set_link(OBJECT(dev),
877
                             OBJECT(dev->vdev.conf.rng), "rng",
878
                             NULL);
C
Cornelia Huck 已提交
879 880
}

881 882 883 884 885
/* 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)
{
886 887 888
    CcwDevice *ccw_dev = to_ccw_dev_fast(d);

    return container_of(ccw_dev, VirtioCcwDevice, parent_obj);
889 890
}

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
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);
908
    trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
909 910 911 912 913
    cpu_physical_memory_unmap(ind_addr, len, 1, len);

    return ind_old;
}

914 915 916
static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
{
    VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
917 918
    CcwDevice *ccw_dev = to_ccw_dev_fast(d);
    SubchDev *sch = ccw_dev->sch;
919 920
    uint64_t indicators;

921 922
    /* queue indicators + secondary indicators */
    if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) {
923 924 925
        return;
    }

926
    if (vector < VIRTIO_CCW_QUEUE_MAX) {
927 928 929
        if (!dev->indicators) {
            return;
        }
930 931 932 933 934 935 936
        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.
             */
937 938
            uint64_t ind_bit = dev->routes.adapter.ind_offset;

939
            virtio_set_ind_atomic(sch, dev->indicators->addr +
940 941
                                  (ind_bit + vector) / 8,
                                  0x80 >> ((ind_bit + vector) % 8));
942
            if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
943 944 945 946
                                       0x01)) {
                css_adapter_interrupt(dev->thinint_isc);
            }
        } else {
947 948 949 950
            indicators = address_space_ldq(&address_space_memory,
                                           dev->indicators->addr,
                                           MEMTXATTRS_UNSPECIFIED,
                                           NULL);
951
            indicators |= 1ULL << vector;
952 953
            address_space_stq(&address_space_memory, dev->indicators->addr,
                              indicators, MEMTXATTRS_UNSPECIFIED, NULL);
954 955
            css_conditional_io_interrupt(sch);
        }
956
    } else {
957 958 959
        if (!dev->indicators2) {
            return;
        }
960
        vector = 0;
961 962 963 964
        indicators = address_space_ldq(&address_space_memory,
                                       dev->indicators2->addr,
                                       MEMTXATTRS_UNSPECIFIED,
                                       NULL);
965
        indicators |= 1ULL << vector;
966 967
        address_space_stq(&address_space_memory, dev->indicators2->addr,
                          indicators, MEMTXATTRS_UNSPECIFIED, NULL);
968
        css_conditional_io_interrupt(sch);
969 970 971 972 973 974
    }
}

static void virtio_ccw_reset(DeviceState *d)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
P
Paolo Bonzini 已提交
975
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
976
    CcwDevice *ccw_dev = CCW_DEVICE(d);
977

978
    virtio_ccw_reset_virtio(dev, vdev);
979
    css_reset_sch(ccw_dev->sch);
980 981
}

C
Cornelia Huck 已提交
982 983 984 985 986 987 988 989 990 991 992
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);
    }
}

993 994
static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
{
995
    CcwDevice *dev = CCW_DEVICE(d);
996 997 998 999

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

1000 1001 1002
static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
{
    int r;
1003
    CcwDevice *ccw_dev = CCW_DEVICE(dev);
1004

1005
    if (!ccw_dev->sch->thinint_active) {
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        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);

1058 1059
    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
                                              dev->routes.gsi[n]);
1060 1061 1062 1063 1064 1065 1066 1067 1068
}

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;

1069 1070
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
                                                dev->routes.gsi[n]);
1071 1072 1073
    assert(ret == 0);
}

1074 1075 1076
static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
                                         bool assign, bool with_irqfd)
{
P
Paolo Bonzini 已提交
1077 1078
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
1079
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
P
Paolo Bonzini 已提交
1080
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1081 1082 1083 1084 1085 1086 1087 1088

    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);
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
        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.
1100
         */
1101
        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
P
Paolo Bonzini 已提交
1102
            k->guest_notifier_mask(vdev, n, false);
1103 1104 1105
        }
        /* get lost events and re-inject */
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
1106
            k->guest_notifier_pending(vdev, n)) {
1107 1108 1109
            event_notifier_set(notifier);
        }
    } else {
1110
        if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
P
Paolo Bonzini 已提交
1111
            k->guest_notifier_mask(vdev, n, true);
1112
        }
1113 1114 1115
        if (with_irqfd) {
            virtio_ccw_remove_irqfd(dev, n);
        }
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
        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 已提交
1126
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1127 1128
    CcwDevice *ccw_dev = CCW_DEVICE(d);
    bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled();
1129 1130
    int r, n;

1131 1132 1133 1134 1135 1136 1137
    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;
        }
    }
1138 1139 1140 1141
    for (n = 0; n < nvqs; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }
1142
        r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1143 1144 1145 1146
        if (r < 0) {
            goto assign_error;
        }
    }
1147 1148 1149 1150
    if (with_irqfd && !assigned) {
        /* release irq routes after irqfds have been released */
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1151 1152 1153 1154 1155 1156
    return 0;

assign_error:
    while (--n >= 0) {
        virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
    }
1157 1158 1159 1160
irqroute_error:
    if (with_irqfd && assigned) {
        virtio_ccw_release_irqroutes(dev, nvqs);
    }
1161 1162 1163
    return r;
}

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
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);
1187 1188
    CcwDevice *ccw_dev = CCW_DEVICE(d);
    SubchDev *s = ccw_dev->sch;
1189
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

    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);
    }
1213
    qemu_put_be16(f, vdev->config_vector);
1214 1215
    qemu_put_be64(f, dev->routes.adapter.ind_offset);
    qemu_put_byte(f, dev->thinint_isc);
C
Cornelia Huck 已提交
1216
    qemu_put_be32(f, dev->revision);
1217 1218 1219 1220 1221
}

static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1222 1223
    CcwDevice *ccw_dev = CCW_DEVICE(d);
    SubchDev *s = ccw_dev->sch;
1224
    VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    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;
    }
1250
    qemu_get_be16s(f, &vdev->config_vector);
1251 1252
    dev->routes.adapter.ind_offset = qemu_get_be64(f);
    dev->thinint_isc = qemu_get_byte(f);
1253
    dev->revision = qemu_get_be32(f);
1254 1255 1256 1257 1258 1259 1260 1261 1262
    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;
}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp)
{
   VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
   VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);

    if (dev->max_rev >= 1) {
        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
    }
}

1273
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1274
static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1275 1276
{
    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1277
    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1278 1279
    CcwDevice *ccw_dev = CCW_DEVICE(d);
    SubchDev *sch = ccw_dev->sch;
1280 1281
    int n = virtio_get_num_queues(vdev);

1282 1283 1284 1285
    if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
        dev->max_rev = 0;
    }

1286
    if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
M
Michael Tokarev 已提交
1287
        error_setg(errp, "The number of virtqueues %d "
1288 1289 1290 1291
                   "exceeds ccw limit %d", n,
                   VIRTIO_CCW_QUEUE_MAX);
        return;
    }
1292 1293 1294

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

C
Cornelia Huck 已提交
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    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);
}
1306 1307 1308
/**************** Virtio-ccw Bus Device Descriptions *******************/

static Property virtio_ccw_net_properties[] = {
1309
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1310 1311
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1312 1313
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1314 1315 1316 1317 1318 1319 1320 1321
    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);

1322
    k->realize = virtio_ccw_net_realize;
1323
    k->exit = virtio_ccw_exit;
1324 1325
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_net_properties;
C
Cornelia Huck 已提交
1326
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1327 1328 1329
}

static const TypeInfo virtio_ccw_net = {
1330
    .name          = TYPE_VIRTIO_NET_CCW,
1331
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1332 1333
    .instance_size = sizeof(VirtIONetCcw),
    .instance_init = virtio_ccw_net_instance_init,
1334 1335 1336 1337
    .class_init    = virtio_ccw_net_class_init,
};

static Property virtio_ccw_blk_properties[] = {
1338
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1339 1340
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1341 1342
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1343 1344 1345 1346 1347 1348 1349 1350
    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);

1351
    k->realize = virtio_ccw_blk_realize;
1352
    k->exit = virtio_ccw_exit;
1353 1354
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_blk_properties;
C
Cornelia Huck 已提交
1355
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1356 1357 1358
}

static const TypeInfo virtio_ccw_blk = {
1359
    .name          = TYPE_VIRTIO_BLK_CCW,
1360
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1361 1362
    .instance_size = sizeof(VirtIOBlkCcw),
    .instance_init = virtio_ccw_blk_instance_init,
1363 1364 1365 1366
    .class_init    = virtio_ccw_blk_class_init,
};

static Property virtio_ccw_serial_properties[] = {
1367
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1368 1369
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1370 1371
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1372 1373 1374 1375 1376 1377 1378 1379
    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);

1380
    k->realize = virtio_ccw_serial_realize;
1381
    k->exit = virtio_ccw_exit;
1382 1383
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_serial_properties;
C
Cornelia Huck 已提交
1384
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1385 1386 1387
}

static const TypeInfo virtio_ccw_serial = {
1388
    .name          = TYPE_VIRTIO_SERIAL_CCW,
1389
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1390 1391
    .instance_size = sizeof(VirtioSerialCcw),
    .instance_init = virtio_ccw_serial_instance_init,
1392 1393 1394 1395
    .class_init    = virtio_ccw_serial_class_init,
};

static Property virtio_ccw_balloon_properties[] = {
1396
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1397 1398
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1399 1400
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1401 1402 1403 1404 1405 1406 1407 1408
    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);

1409
    k->realize = virtio_ccw_balloon_realize;
1410
    k->exit = virtio_ccw_exit;
1411 1412
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_balloon_properties;
C
Cornelia Huck 已提交
1413
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1414 1415 1416
}

static const TypeInfo virtio_ccw_balloon = {
1417
    .name          = TYPE_VIRTIO_BALLOON_CCW,
1418
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1419 1420
    .instance_size = sizeof(VirtIOBalloonCcw),
    .instance_init = virtio_ccw_balloon_instance_init,
1421 1422 1423 1424
    .class_init    = virtio_ccw_balloon_class_init,
};

static Property virtio_ccw_scsi_properties[] = {
1425
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1426 1427
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1428 1429
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1430 1431 1432 1433 1434 1435 1436 1437
    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);

1438
    k->realize = virtio_ccw_scsi_realize;
1439
    k->exit = virtio_ccw_exit;
1440 1441
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_scsi_properties;
C
Cornelia Huck 已提交
1442
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1443 1444 1445
}

static const TypeInfo virtio_ccw_scsi = {
1446
    .name          = TYPE_VIRTIO_SCSI_CCW,
1447
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1448 1449
    .instance_size = sizeof(VirtIOSCSICcw),
    .instance_init = virtio_ccw_scsi_instance_init,
1450 1451 1452
    .class_init    = virtio_ccw_scsi_class_init,
};

1453 1454
#ifdef CONFIG_VHOST_SCSI
static Property vhost_ccw_scsi_properties[] = {
1455
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1456 1457
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
1458 1459 1460 1461 1462 1463 1464 1465
    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);

1466
    k->realize = vhost_ccw_scsi_realize;
1467 1468 1469
    k->exit = virtio_ccw_exit;
    dc->reset = virtio_ccw_reset;
    dc->props = vhost_ccw_scsi_properties;
C
Cornelia Huck 已提交
1470
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1471 1472 1473 1474 1475
}

static const TypeInfo vhost_ccw_scsi = {
    .name          = TYPE_VHOST_SCSI_CCW,
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1476
    .instance_size = sizeof(VHostSCSICcw),
1477 1478 1479 1480 1481
    .instance_init = vhost_ccw_scsi_instance_init,
    .class_init    = vhost_ccw_scsi_class_init,
};
#endif

1482
static void virtio_ccw_rng_instance_init(Object *obj)
C
Cornelia Huck 已提交
1483
{
1484
    VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1485 1486 1487

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
1488 1489
    object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
                              "rng", &error_abort);
C
Cornelia Huck 已提交
1490 1491 1492
}

static Property virtio_ccw_rng_properties[] = {
1493
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
C
Cornelia Huck 已提交
1494 1495
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
                    VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1496 1497
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
C
Cornelia Huck 已提交
1498 1499 1500 1501 1502 1503 1504 1505
    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);

1506
    k->realize = virtio_ccw_rng_realize;
1507
    k->exit = virtio_ccw_exit;
C
Cornelia Huck 已提交
1508 1509
    dc->reset = virtio_ccw_reset;
    dc->props = virtio_ccw_rng_properties;
C
Cornelia Huck 已提交
1510
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
C
Cornelia Huck 已提交
1511 1512 1513
}

static const TypeInfo virtio_ccw_rng = {
1514
    .name          = TYPE_VIRTIO_RNG_CCW,
C
Cornelia Huck 已提交
1515
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
1516 1517
    .instance_size = sizeof(VirtIORNGCcw),
    .instance_init = virtio_ccw_rng_instance_init,
C
Cornelia Huck 已提交
1518 1519 1520
    .class_init    = virtio_ccw_rng_class_init,
};

1521
static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1522 1523 1524
{
    VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;

1525
    virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1526
    virtio_ccw_device_realize(_dev, errp);
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}

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

    return _info->exit(_dev);
}

1537 1538
static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
                                     DeviceState *dev, Error **errp)
1539
{
1540
    VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev);
1541

1542
    virtio_ccw_stop_ioeventfd(_dev);
1543 1544 1545 1546 1547
}

static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
1548
    CCWDeviceClass *k = CCW_DEVICE_CLASS(dc);
1549

1550
    k->unplug = virtio_ccw_busdev_unplug;
1551
    dc->realize = virtio_ccw_busdev_realize;
1552 1553 1554 1555 1556 1557
    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,
1558
    .parent = TYPE_CCW_DEVICE,
1559 1560 1561 1562 1563 1564 1565 1566
    .instance_size = sizeof(VirtioCcwDevice),
    .class_init = virtio_ccw_device_class_init,
    .class_size = sizeof(VirtIOCCWDeviceClass),
    .abstract = true,
};

/* virtio-ccw-bus */

1567 1568
static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtioCcwDevice *dev)
1569 1570
{
    DeviceState *qdev = DEVICE(dev);
1571
    char virtio_bus_name[] = "virtio-bus";
1572

1573 1574
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
                        qdev, virtio_bus_name);
1575 1576 1577 1578 1579 1580 1581 1582 1583
}

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 已提交
1584
    k->vmstate_change = virtio_ccw_vmstate_change;
1585 1586
    k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
    k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1587 1588 1589 1590
    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;
1591
    k->pre_plugged = virtio_ccw_pre_plugged;
1592 1593
    k->device_plugged = virtio_ccw_device_plugged;
    k->device_unplugged = virtio_ccw_device_unplugged;
1594
    k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled;
1595
    k->ioeventfd_assign = virtio_ccw_ioeventfd_assign;
1596 1597 1598 1599 1600 1601 1602 1603 1604
}

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 已提交
1605 1606
#ifdef CONFIG_VIRTFS
static Property virtio_ccw_9p_properties[] = {
1607
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
P
Pierre Morel 已提交
1608 1609
    DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
            VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
C
Cornelia Huck 已提交
1610 1611
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
P
Pierre Morel 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620
    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));
1621
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
P
Pierre Morel 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
}

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

1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
#ifdef CONFIG_VHOST_VSOCK

static Property vhost_vsock_ccw_properties[] = {
    DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id),
    DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
                       VIRTIO_CCW_MAX_REV),
    DEFINE_PROP_END_OF_LIST(),
};

static void vhost_vsock_ccw_realize(VirtioCcwDevice *ccw_dev, Error **errp)
{
    VHostVSockCCWState *dev = VHOST_VSOCK_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 vhost_vsock_ccw_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);

    k->realize = vhost_vsock_ccw_realize;
    k->exit = virtio_ccw_exit;
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
    dc->props = vhost_vsock_ccw_properties;
    dc->reset = virtio_ccw_reset;
}

static void vhost_vsock_ccw_instance_init(Object *obj)
{
    VHostVSockCCWState *dev = VHOST_VSOCK_CCW(obj);

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

static const TypeInfo vhost_vsock_ccw_info = {
    .name          = TYPE_VHOST_VSOCK_CCW,
    .parent        = TYPE_VIRTIO_CCW_DEVICE,
    .instance_size = sizeof(VHostVSockCCWState),
    .instance_init = vhost_vsock_ccw_instance_init,
    .class_init    = vhost_vsock_ccw_class_init,
};
#endif

1704 1705 1706 1707 1708 1709 1710 1711 1712
static void virtio_ccw_register(void)
{
    type_register_static(&virtio_ccw_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);
1713
#ifdef CONFIG_VHOST_SCSI
1714
    type_register_static(&vhost_ccw_scsi);
1715
#endif
C
Cornelia Huck 已提交
1716
    type_register_static(&virtio_ccw_rng);
P
Pierre Morel 已提交
1717 1718 1719
#ifdef CONFIG_VIRTFS
    type_register_static(&virtio_ccw_9p_info);
#endif
1720 1721 1722
#ifdef CONFIG_VHOST_VSOCK
    type_register_static(&vhost_vsock_ccw_info);
#endif
1723 1724 1725
}

type_init(virtio_ccw_register)