2 * virtio ccw target implementation
4 * Copyright 2012,2015 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6 * Pierre Morel <pmorel@linux.vnet.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or (at
9 * your option) any later version. See the COPYING file in the top-level
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
16 #include "sysemu/block-backend.h"
17 #include "sysemu/blockdev.h"
18 #include "sysemu/sysemu.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/virtio/virtio-serial.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "hw/sysbus.h"
24 #include "qemu/bitops.h"
25 #include "qemu/error-report.h"
26 #include "hw/virtio/virtio-access.h"
27 #include "hw/virtio/virtio-bus.h"
28 #include "hw/s390x/adapter.h"
29 #include "hw/s390x/s390_flic.h"
33 #include "virtio-ccw.h"
36 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
37 VirtioCcwDevice *dev);
39 static void virtual_css_bus_reset(BusState *qbus)
41 /* This should actually be modelled via the generic css */
46 static void virtual_css_bus_class_init(ObjectClass *klass, void *data)
48 BusClass *k = BUS_CLASS(klass);
50 k->reset = virtual_css_bus_reset;
53 static const TypeInfo virtual_css_bus_info = {
54 .name = TYPE_VIRTUAL_CSS_BUS,
56 .instance_size = sizeof(VirtualCssBus),
57 .class_init = virtual_css_bus_class_init,
60 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
62 VirtIODevice *vdev = NULL;
63 VirtioCcwDevice *dev = sch->driver_data;
66 vdev = virtio_bus_get_device(&dev->bus);
71 static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice *dev, int n,
72 bool assign, bool set_handler)
74 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
75 VirtQueue *vq = virtio_get_queue(vdev, n);
76 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
78 SubchDev *sch = dev->sch;
79 uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;
82 r = event_notifier_init(notifier, 1);
84 error_report("%s: unable to init event notifier: %d", __func__, r);
87 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
88 r = s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
90 error_report("%s: unable to assign ioeventfd: %d", __func__, r);
91 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
92 event_notifier_cleanup(notifier);
96 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
97 s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
98 event_notifier_cleanup(notifier);
103 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
108 if (!(dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) ||
109 dev->ioeventfd_disabled ||
110 dev->ioeventfd_started) {
113 vdev = virtio_bus_get_device(&dev->bus);
114 for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
115 if (!virtio_queue_get_num(vdev, n)) {
118 r = virtio_ccw_set_guest2host_notifier(dev, n, true, true);
123 dev->ioeventfd_started = true;
128 if (!virtio_queue_get_num(vdev, n)) {
131 r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
134 dev->ioeventfd_started = false;
135 /* Disable ioeventfd for this device. */
136 dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
137 error_report("%s: failed. Fallback to userspace (slower).", __func__);
140 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
145 if (!dev->ioeventfd_started) {
148 vdev = virtio_bus_get_device(&dev->bus);
149 for (n = 0; n < VIRTIO_CCW_QUEUE_MAX; n++) {
150 if (!virtio_queue_get_num(vdev, n)) {
153 r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
156 dev->ioeventfd_started = false;
159 VirtualCssBus *virtual_css_bus_init(void)
165 /* Create bridge device */
166 dev = qdev_create(NULL, "virtual-css-bridge");
167 qdev_init_nofail(dev);
169 /* Create bus on bridge device */
170 bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
171 cbus = VIRTUAL_CSS_BUS(bus);
173 /* Enable hotplugging */
174 qbus_set_hotplug_handler(bus, dev, &error_abort);
179 /* Communication blocks used by several channel commands. */
180 typedef struct VqInfoBlockLegacy {
185 } QEMU_PACKED VqInfoBlockLegacy;
187 typedef struct VqInfoBlock {
194 } QEMU_PACKED VqInfoBlock;
196 typedef struct VqConfigBlock {
199 } QEMU_PACKED VqConfigBlock;
201 typedef struct VirtioFeatDesc {
204 } QEMU_PACKED VirtioFeatDesc;
206 typedef struct VirtioThinintInfo {
207 hwaddr summary_indicator;
208 hwaddr device_indicator;
211 } QEMU_PACKED VirtioThinintInfo;
213 typedef struct VirtioRevInfo {
217 } QEMU_PACKED VirtioRevInfo;
219 /* Specify where the virtqueues for the subchannel are in guest memory. */
220 static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
221 VqInfoBlockLegacy *linfo)
223 VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
224 uint16_t index = info ? info->index : linfo->index;
225 uint16_t num = info ? info->num : linfo->num;
226 uint64_t desc = info ? info->desc : linfo->queue;
228 if (index >= VIRTIO_CCW_QUEUE_MAX) {
232 /* Current code in virtio.c relies on 4K alignment. */
233 if (linfo && desc && (linfo->align != 4096)) {
242 virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
244 virtio_queue_set_addr(vdev, index, desc);
247 virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
250 /* virtio-1 allows changing the ring size. */
251 if (virtio_queue_get_num(vdev, index) < num) {
252 /* Fail if we exceed the maximum number. */
255 virtio_queue_set_num(vdev, index, num);
256 } else if (virtio_queue_get_num(vdev, index) > num) {
257 /* Fail if we don't have a big enough queue. */
260 /* We ignore possible increased num for legacy for compatibility. */
261 virtio_queue_set_vector(vdev, index, index);
263 /* tell notify handler in case of config change */
264 vdev->config_vector = VIRTIO_CCW_QUEUE_MAX;
268 static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
270 virtio_ccw_stop_ioeventfd(dev);
272 if (dev->indicators) {
273 release_indicator(&dev->routes.adapter, dev->indicators);
274 dev->indicators = NULL;
276 if (dev->indicators2) {
277 release_indicator(&dev->routes.adapter, dev->indicators2);
278 dev->indicators2 = NULL;
280 if (dev->summary_indicator) {
281 release_indicator(&dev->routes.adapter, dev->summary_indicator);
282 dev->summary_indicator = NULL;
284 dev->sch->thinint_active = false;
287 static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
292 VqInfoBlockLegacy linfo;
293 size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info);
296 if (ccw.count != info_len) {
299 } else if (ccw.count < info_len) {
300 /* Can't execute command. */
307 linfo.queue = address_space_ldq_be(&address_space_memory, ccw.cda,
308 MEMTXATTRS_UNSPECIFIED, NULL);
309 linfo.align = address_space_ldl_be(&address_space_memory,
310 ccw.cda + sizeof(linfo.queue),
311 MEMTXATTRS_UNSPECIFIED,
313 linfo.index = address_space_lduw_be(&address_space_memory,
314 ccw.cda + sizeof(linfo.queue)
315 + sizeof(linfo.align),
316 MEMTXATTRS_UNSPECIFIED,
318 linfo.num = address_space_lduw_be(&address_space_memory,
319 ccw.cda + sizeof(linfo.queue)
320 + sizeof(linfo.align)
321 + sizeof(linfo.index),
322 MEMTXATTRS_UNSPECIFIED,
324 ret = virtio_ccw_set_vqs(sch, NULL, &linfo);
326 info.desc = address_space_ldq_be(&address_space_memory, ccw.cda,
327 MEMTXATTRS_UNSPECIFIED, NULL);
328 info.index = address_space_lduw_be(&address_space_memory,
329 ccw.cda + sizeof(info.desc)
331 MEMTXATTRS_UNSPECIFIED, NULL);
332 info.num = address_space_lduw_be(&address_space_memory,
333 ccw.cda + sizeof(info.desc)
335 + sizeof(info.index),
336 MEMTXATTRS_UNSPECIFIED, NULL);
337 info.avail = address_space_ldq_be(&address_space_memory,
338 ccw.cda + sizeof(info.desc)
342 MEMTXATTRS_UNSPECIFIED, NULL);
343 info.used = address_space_ldq_be(&address_space_memory,
344 ccw.cda + sizeof(info.desc)
348 + sizeof(info.avail),
349 MEMTXATTRS_UNSPECIFIED, NULL);
350 ret = virtio_ccw_set_vqs(sch, &info, NULL);
352 sch->curr_status.scsw.count = 0;
356 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
359 VirtioRevInfo revinfo;
361 VirtioFeatDesc features;
364 VqConfigBlock vq_config;
365 VirtioCcwDevice *dev = sch->driver_data;
366 VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
370 VirtioThinintInfo *thinint;
376 trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
378 check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
380 /* Look at the command. */
381 switch (ccw.cmd_code) {
383 ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
385 case CCW_CMD_VDEV_RESET:
386 virtio_ccw_reset_virtio(dev, vdev);
389 case CCW_CMD_READ_FEAT:
391 if (ccw.count != sizeof(features)) {
395 } else if (ccw.count < sizeof(features)) {
396 /* Can't execute command. */
403 features.index = address_space_ldub(&address_space_memory,
405 + sizeof(features.features),
406 MEMTXATTRS_UNSPECIFIED,
408 if (features.index == 0) {
409 if (dev->revision >= 1) {
410 /* Don't offer legacy features for modern devices. */
411 features.features = (uint32_t)
412 (vdev->host_features & ~VIRTIO_LEGACY_FEATURES);
414 features.features = (uint32_t)vdev->host_features;
416 } else if ((features.index == 1) && (dev->revision >= 1)) {
418 * Only offer feature bits beyond 31 if the guest has
419 * negotiated at least revision 1.
421 features.features = (uint32_t)(vdev->host_features >> 32);
423 /* Return zeroes if the guest supports more feature bits. */
424 features.features = 0;
426 address_space_stl_le(&address_space_memory, ccw.cda,
427 features.features, MEMTXATTRS_UNSPECIFIED,
429 sch->curr_status.scsw.count = ccw.count - sizeof(features);
433 case CCW_CMD_WRITE_FEAT:
435 if (ccw.count != sizeof(features)) {
439 } else if (ccw.count < sizeof(features)) {
440 /* Can't execute command. */
447 features.index = address_space_ldub(&address_space_memory,
449 + sizeof(features.features),
450 MEMTXATTRS_UNSPECIFIED,
452 features.features = address_space_ldl_le(&address_space_memory,
454 MEMTXATTRS_UNSPECIFIED,
456 if (features.index == 0) {
457 virtio_set_features(vdev,
458 (vdev->guest_features & 0xffffffff00000000ULL) |
460 } else if ((features.index == 1) && (dev->revision >= 1)) {
462 * If the guest did not negotiate at least revision 1,
463 * we did not offer it any feature bits beyond 31. Such a
464 * guest passing us any bit here is therefore buggy.
466 virtio_set_features(vdev,
467 (vdev->guest_features & 0x00000000ffffffffULL) |
468 ((uint64_t)features.features << 32));
471 * If the guest supports more feature bits, assert that it
472 * passes us zeroes for those we don't support.
474 if (features.features) {
475 fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
476 features.index, features.features);
477 /* XXX: do a unit check here? */
480 sch->curr_status.scsw.count = ccw.count - sizeof(features);
484 case CCW_CMD_READ_CONF:
486 if (ccw.count > vdev->config_len) {
491 len = MIN(ccw.count, vdev->config_len);
495 virtio_bus_get_vdev_config(&dev->bus, vdev->config);
496 /* XXX config space endianness */
497 cpu_physical_memory_write(ccw.cda, vdev->config, len);
498 sch->curr_status.scsw.count = ccw.count - len;
502 case CCW_CMD_WRITE_CONF:
504 if (ccw.count > vdev->config_len) {
509 len = MIN(ccw.count, vdev->config_len);
514 config = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
519 /* XXX config space endianness */
520 memcpy(vdev->config, config, len);
521 cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
522 virtio_bus_set_vdev_config(&dev->bus, vdev->config);
523 sch->curr_status.scsw.count = ccw.count - len;
528 case CCW_CMD_WRITE_STATUS:
530 if (ccw.count != sizeof(status)) {
534 } else if (ccw.count < sizeof(status)) {
535 /* Can't execute command. */
542 status = address_space_ldub(&address_space_memory, ccw.cda,
543 MEMTXATTRS_UNSPECIFIED, NULL);
544 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
545 virtio_ccw_stop_ioeventfd(dev);
547 if (virtio_set_status(vdev, status) == 0) {
548 if (vdev->status == 0) {
549 virtio_ccw_reset_virtio(dev, vdev);
551 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
552 virtio_ccw_start_ioeventfd(dev);
554 sch->curr_status.scsw.count = ccw.count - sizeof(status);
557 /* Trigger a command reject. */
562 case CCW_CMD_SET_IND:
564 if (ccw.count != sizeof(indicators)) {
568 } else if (ccw.count < sizeof(indicators)) {
569 /* Can't execute command. */
573 if (sch->thinint_active) {
574 /* Trigger a command reject. */
581 indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
582 MEMTXATTRS_UNSPECIFIED, NULL);
583 dev->indicators = get_indicator(indicators, sizeof(uint64_t));
584 sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
588 case CCW_CMD_SET_CONF_IND:
590 if (ccw.count != sizeof(indicators)) {
594 } else if (ccw.count < sizeof(indicators)) {
595 /* Can't execute command. */
602 indicators = address_space_ldq_be(&address_space_memory, ccw.cda,
603 MEMTXATTRS_UNSPECIFIED, NULL);
604 dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
605 sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
609 case CCW_CMD_READ_VQ_CONF:
611 if (ccw.count != sizeof(vq_config)) {
615 } else if (ccw.count < sizeof(vq_config)) {
616 /* Can't execute command. */
623 vq_config.index = address_space_lduw_be(&address_space_memory,
625 MEMTXATTRS_UNSPECIFIED,
627 if (vq_config.index >= VIRTIO_CCW_QUEUE_MAX) {
631 vq_config.num_max = virtio_queue_get_num(vdev,
633 address_space_stw_be(&address_space_memory,
634 ccw.cda + sizeof(vq_config.index),
636 MEMTXATTRS_UNSPECIFIED,
638 sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
642 case CCW_CMD_SET_IND_ADAPTER:
644 if (ccw.count != sizeof(*thinint)) {
648 } else if (ccw.count < sizeof(*thinint)) {
649 /* Can't execute command. */
653 len = sizeof(*thinint);
657 } else if (dev->indicators && !sch->thinint_active) {
658 /* Trigger a command reject. */
661 thinint = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
665 uint64_t ind_bit = ldq_be_p(&thinint->ind_bit);
668 dev->summary_indicator =
669 get_indicator(ldq_be_p(&thinint->summary_indicator),
672 get_indicator(ldq_be_p(&thinint->device_indicator),
674 dev->thinint_isc = thinint->isc;
675 dev->routes.adapter.ind_offset = ind_bit;
676 dev->routes.adapter.summary_offset = 7;
677 cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len);
678 ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
679 dev->thinint_isc, true, false,
680 &dev->routes.adapter.adapter_id);
682 sch->thinint_active = ((dev->indicators != NULL) &&
683 (dev->summary_indicator != NULL));
684 sch->curr_status.scsw.count = ccw.count - len;
689 case CCW_CMD_SET_VIRTIO_REV:
690 len = sizeof(revinfo);
691 if (ccw.count < len) {
700 address_space_lduw_be(&address_space_memory, ccw.cda,
701 MEMTXATTRS_UNSPECIFIED, NULL);
703 address_space_lduw_be(&address_space_memory,
704 ccw.cda + sizeof(revinfo.revision),
705 MEMTXATTRS_UNSPECIFIED, NULL);
706 if (ccw.count < len + revinfo.length ||
707 (check_len && ccw.count > len + revinfo.length)) {
712 * Once we start to support revisions with additional data, we'll
713 * need to fetch it here. Nothing to do for now, though.
715 if (dev->revision >= 0 ||
716 revinfo.revision > virtio_ccw_rev_max(dev)) {
721 dev->revision = revinfo.revision;
730 static void virtio_sch_disable_cb(SubchDev *sch)
732 VirtioCcwDevice *dev = sch->driver_data;
737 static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
739 unsigned int cssid = 0;
740 unsigned int ssid = 0;
743 bool have_devno = false;
748 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
750 sch = g_malloc0(sizeof(SubchDev));
752 sch->driver_data = dev;
755 dev->indicators = NULL;
757 /* Initialize subchannel structure. */
758 sch->channel_prog = 0x0;
759 sch->last_cmd_valid = false;
760 sch->thinint_active = false;
762 * Use a device number if provided. Otherwise, fall back to subchannel
766 num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
768 if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
769 error_setg(errp, "Invalid cssid or ssid: cssid %x, ssid %x",
773 /* Enforce use of virtual cssid. */
774 if (cssid != VIRTUAL_CSSID) {
775 error_setg(errp, "cssid %x not valid for virtio devices",
779 if (css_devno_used(cssid, ssid, devno)) {
780 error_setg(errp, "Device %x.%x.%04x already exists",
789 error_setg(errp, "Malformed devno parameter '%s'", dev->bus_id);
794 /* Find the next free id. */
796 for (schid = 0; schid <= MAX_SCHID; schid++) {
797 if (!css_find_subch(1, cssid, ssid, schid)) {
799 css_subch_assign(cssid, ssid, schid, devno, sch);
805 error_setg(errp, "No free subchannel found for %x.%x.%04x",
809 trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
812 cssid = VIRTUAL_CSSID;
813 for (ssid = 0; ssid <= MAX_SSID; ssid++) {
814 for (schid = 0; schid <= MAX_SCHID; schid++) {
815 if (!css_find_subch(1, cssid, ssid, schid)) {
821 * If the devno is already taken, look further in this
824 while (css_devno_used(cssid, ssid, devno)) {
825 if (devno == MAX_SCHID) {
827 } else if (devno == schid - 1) {
828 error_setg(errp, "No free devno found");
835 css_subch_assign(cssid, ssid, schid, devno, sch);
845 error_setg(errp, "Virtual channel subsystem is full!");
848 trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
852 /* Build initial schib. */
853 css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
855 sch->ccw_cb = virtio_ccw_cb;
856 sch->disable_cb = virtio_sch_disable_cb;
858 /* Build senseid data. */
859 memset(&sch->id, 0, sizeof(SenseId));
860 sch->id.reserved = 0xff;
861 sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
866 k->realize(dev, &err);
869 error_propagate(errp, err);
870 css_subch_assign(cssid, ssid, schid, devno, NULL);
881 static int virtio_ccw_exit(VirtioCcwDevice *dev)
883 SubchDev *sch = dev->sch;
886 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
889 if (dev->indicators) {
890 release_indicator(&dev->routes.adapter, dev->indicators);
891 dev->indicators = NULL;
896 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
898 DeviceState *qdev = DEVICE(ccw_dev);
899 VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
900 DeviceState *vdev = DEVICE(&dev->vdev);
903 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
904 object_get_typename(OBJECT(qdev)));
905 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
906 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
908 error_propagate(errp, err);
912 static void virtio_ccw_net_instance_init(Object *obj)
914 VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
916 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
918 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
919 "bootindex", &error_abort);
922 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
924 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
925 DeviceState *vdev = DEVICE(&dev->vdev);
928 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
929 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
931 error_propagate(errp, err);
935 static void virtio_ccw_blk_instance_init(Object *obj)
937 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
939 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
941 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
943 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
944 "bootindex", &error_abort);
947 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
949 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
950 DeviceState *vdev = DEVICE(&dev->vdev);
951 DeviceState *proxy = DEVICE(ccw_dev);
956 * For command line compatibility, this sets the virtio-serial-device bus
960 bus_name = g_strdup_printf("%s.0", proxy->id);
961 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
965 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
966 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
968 error_propagate(errp, err);
973 static void virtio_ccw_serial_instance_init(Object *obj)
975 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
977 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
981 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
983 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
984 DeviceState *vdev = DEVICE(&dev->vdev);
987 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
988 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
990 error_propagate(errp, err);
994 static void virtio_ccw_balloon_instance_init(Object *obj)
996 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
998 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
999 TYPE_VIRTIO_BALLOON);
1000 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
1001 "guest-stats", &error_abort);
1002 object_property_add_alias(obj, "guest-stats-polling-interval",
1004 "guest-stats-polling-interval", &error_abort);
1007 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1009 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
1010 DeviceState *vdev = DEVICE(&dev->vdev);
1011 DeviceState *qdev = DEVICE(ccw_dev);
1016 * For command line compatibility, this sets the virtio-scsi-device bus
1020 bus_name = g_strdup_printf("%s.0", qdev->id);
1021 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1025 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1026 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1028 error_propagate(errp, err);
1032 static void virtio_ccw_scsi_instance_init(Object *obj)
1034 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
1036 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1038 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1042 #ifdef CONFIG_VHOST_SCSI
1043 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1045 VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
1046 DeviceState *vdev = DEVICE(&dev->vdev);
1049 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1050 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1052 error_propagate(errp, err);
1056 static void vhost_ccw_scsi_instance_init(Object *obj)
1058 VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
1060 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1065 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1067 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
1068 DeviceState *vdev = DEVICE(&dev->vdev);
1071 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1072 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1074 error_propagate(errp, err);
1078 object_property_set_link(OBJECT(dev),
1079 OBJECT(dev->vdev.conf.rng), "rng",
1083 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1084 * be careful and test performance if you change this.
1086 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
1088 return container_of(d, VirtioCcwDevice, parent_obj);
1091 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
1094 uint8_t ind_old, ind_new;
1098 ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
1100 error_report("%s(%x.%x.%04x): unable to access indicator",
1101 __func__, sch->cssid, sch->ssid, sch->schid);
1105 ind_old = *ind_addr;
1106 ind_new = ind_old | to_be_set;
1107 } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
1108 cpu_physical_memory_unmap(ind_addr, len, 1, len);
1113 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
1115 VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1116 SubchDev *sch = dev->sch;
1117 uint64_t indicators;
1119 /* queue indicators + secondary indicators */
1120 if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) {
1124 if (vector < VIRTIO_CCW_QUEUE_MAX) {
1125 if (!dev->indicators) {
1128 if (sch->thinint_active) {
1130 * In the adapter interrupt case, indicators points to a
1131 * memory area that may be (way) larger than 64 bit and
1132 * ind_bit indicates the start of the indicators in a big
1135 uint64_t ind_bit = dev->routes.adapter.ind_offset;
1137 virtio_set_ind_atomic(sch, dev->indicators->addr +
1138 (ind_bit + vector) / 8,
1139 0x80 >> ((ind_bit + vector) % 8));
1140 if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1142 css_adapter_interrupt(dev->thinint_isc);
1145 indicators = address_space_ldq(&address_space_memory,
1146 dev->indicators->addr,
1147 MEMTXATTRS_UNSPECIFIED,
1149 indicators |= 1ULL << vector;
1150 address_space_stq(&address_space_memory, dev->indicators->addr,
1151 indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1152 css_conditional_io_interrupt(sch);
1155 if (!dev->indicators2) {
1159 indicators = address_space_ldq(&address_space_memory,
1160 dev->indicators2->addr,
1161 MEMTXATTRS_UNSPECIFIED,
1163 indicators |= 1ULL << vector;
1164 address_space_stq(&address_space_memory, dev->indicators2->addr,
1165 indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1166 css_conditional_io_interrupt(sch);
1170 static void virtio_ccw_reset(DeviceState *d)
1172 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1173 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1175 virtio_ccw_reset_virtio(dev, vdev);
1176 css_reset_sch(dev->sch);
1179 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1181 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1184 virtio_ccw_start_ioeventfd(dev);
1186 virtio_ccw_stop_ioeventfd(dev);
1190 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1192 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1194 return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1197 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1199 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1201 /* Stop using the generic ioeventfd, we are doing eventfd handling
1202 * ourselves below */
1203 dev->ioeventfd_disabled = assign;
1205 virtio_ccw_stop_ioeventfd(dev);
1207 return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1210 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1214 if (!dev->sch->thinint_active) {
1218 r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1222 r = map_indicator(&dev->routes.adapter, dev->indicators);
1226 dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1227 dev->routes.adapter.ind_addr = dev->indicators->map;
1232 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1235 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1237 S390FLICState *fs = s390_get_flic();
1238 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1240 ret = virtio_ccw_get_mappings(dev);
1244 for (i = 0; i < nvqs; i++) {
1245 if (!virtio_queue_get_num(vdev, i)) {
1249 dev->routes.num_routes = i;
1250 return fsc->add_adapter_routes(fs, &dev->routes);
1253 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1255 S390FLICState *fs = s390_get_flic();
1256 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1258 fsc->release_adapter_routes(fs, &dev->routes);
1261 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1263 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1264 VirtQueue *vq = virtio_get_queue(vdev, n);
1265 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1267 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1268 dev->routes.gsi[n]);
1271 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1273 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1274 VirtQueue *vq = virtio_get_queue(vdev, n);
1275 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1278 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1279 dev->routes.gsi[n]);
1283 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1284 bool assign, bool with_irqfd)
1286 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1287 VirtQueue *vq = virtio_get_queue(vdev, n);
1288 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1289 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1292 int r = event_notifier_init(notifier, 0);
1297 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1299 r = virtio_ccw_add_irqfd(dev, n);
1301 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1307 * We do not support individual masking for channel devices, so we
1308 * need to manually trigger any guest masking callbacks here.
1310 if (k->guest_notifier_mask) {
1311 k->guest_notifier_mask(vdev, n, false);
1313 /* get lost events and re-inject */
1314 if (k->guest_notifier_pending &&
1315 k->guest_notifier_pending(vdev, n)) {
1316 event_notifier_set(notifier);
1319 if (k->guest_notifier_mask) {
1320 k->guest_notifier_mask(vdev, n, true);
1323 virtio_ccw_remove_irqfd(dev, n);
1325 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1326 event_notifier_cleanup(notifier);
1331 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1334 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1335 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1336 bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1339 if (with_irqfd && assigned) {
1340 /* irq routes need to be set up before assigning irqfds */
1341 r = virtio_ccw_setup_irqroutes(dev, nvqs);
1343 goto irqroute_error;
1346 for (n = 0; n < nvqs; n++) {
1347 if (!virtio_queue_get_num(vdev, n)) {
1350 r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1355 if (with_irqfd && !assigned) {
1356 /* release irq routes after irqfds have been released */
1357 virtio_ccw_release_irqroutes(dev, nvqs);
1363 virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1366 if (with_irqfd && assigned) {
1367 virtio_ccw_release_irqroutes(dev, nvqs);
1372 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1374 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1375 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1377 qemu_put_be16(f, virtio_queue_vector(vdev, n));
1380 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1382 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1383 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1386 qemu_get_be16s(f, &vector);
1387 virtio_queue_set_vector(vdev, n , vector);
1392 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1394 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1395 SubchDev *s = dev->sch;
1396 VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1398 subch_device_save(s, f);
1399 if (dev->indicators != NULL) {
1400 qemu_put_be32(f, dev->indicators->len);
1401 qemu_put_be64(f, dev->indicators->addr);
1403 qemu_put_be32(f, 0);
1404 qemu_put_be64(f, 0UL);
1406 if (dev->indicators2 != NULL) {
1407 qemu_put_be32(f, dev->indicators2->len);
1408 qemu_put_be64(f, dev->indicators2->addr);
1410 qemu_put_be32(f, 0);
1411 qemu_put_be64(f, 0UL);
1413 if (dev->summary_indicator != NULL) {
1414 qemu_put_be32(f, dev->summary_indicator->len);
1415 qemu_put_be64(f, dev->summary_indicator->addr);
1417 qemu_put_be32(f, 0);
1418 qemu_put_be64(f, 0UL);
1420 qemu_put_be16(f, vdev->config_vector);
1421 qemu_put_be64(f, dev->routes.adapter.ind_offset);
1422 qemu_put_byte(f, dev->thinint_isc);
1423 qemu_put_be32(f, dev->revision);
1426 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1428 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1429 SubchDev *s = dev->sch;
1430 VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1433 s->driver_data = dev;
1434 subch_device_load(s, f);
1435 len = qemu_get_be32(f);
1437 dev->indicators = get_indicator(qemu_get_be64(f), len);
1440 dev->indicators = NULL;
1442 len = qemu_get_be32(f);
1444 dev->indicators2 = get_indicator(qemu_get_be64(f), len);
1447 dev->indicators2 = NULL;
1449 len = qemu_get_be32(f);
1451 dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
1454 dev->summary_indicator = NULL;
1456 qemu_get_be16s(f, &vdev->config_vector);
1457 dev->routes.adapter.ind_offset = qemu_get_be64(f);
1458 dev->thinint_isc = qemu_get_byte(f);
1459 dev->revision = qemu_get_be32(f);
1460 if (s->thinint_active) {
1461 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
1462 dev->thinint_isc, true, false,
1463 &dev->routes.adapter.adapter_id);
1469 /* This is called by virtio-bus just after the device is plugged. */
1470 static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1472 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1473 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1474 SubchDev *sch = dev->sch;
1475 int n = virtio_get_num_queues(vdev);
1477 if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
1478 error_setg(errp, "The nubmer of virtqueues %d "
1479 "exceeds ccw limit %d", n,
1480 VIRTIO_CCW_QUEUE_MAX);
1484 if (!kvm_eventfds_enabled()) {
1485 dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
1488 sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1490 if (dev->max_rev >= 1) {
1491 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1494 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1498 static void virtio_ccw_post_plugged(DeviceState *d, Error **errp)
1500 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1501 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1503 if (!virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1504 /* A backend didn't support modern virtio. */
1509 static void virtio_ccw_device_unplugged(DeviceState *d)
1511 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1513 virtio_ccw_stop_ioeventfd(dev);
1515 /**************** Virtio-ccw Bus Device Descriptions *******************/
1517 static Property virtio_ccw_net_properties[] = {
1518 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1519 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1520 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1521 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1522 VIRTIO_CCW_MAX_REV),
1523 DEFINE_PROP_END_OF_LIST(),
1526 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1528 DeviceClass *dc = DEVICE_CLASS(klass);
1529 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1531 k->realize = virtio_ccw_net_realize;
1532 k->exit = virtio_ccw_exit;
1533 dc->reset = virtio_ccw_reset;
1534 dc->props = virtio_ccw_net_properties;
1535 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1538 static const TypeInfo virtio_ccw_net = {
1539 .name = TYPE_VIRTIO_NET_CCW,
1540 .parent = TYPE_VIRTIO_CCW_DEVICE,
1541 .instance_size = sizeof(VirtIONetCcw),
1542 .instance_init = virtio_ccw_net_instance_init,
1543 .class_init = virtio_ccw_net_class_init,
1546 static Property virtio_ccw_blk_properties[] = {
1547 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1548 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1549 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1550 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1551 VIRTIO_CCW_MAX_REV),
1552 DEFINE_PROP_END_OF_LIST(),
1555 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1557 DeviceClass *dc = DEVICE_CLASS(klass);
1558 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1560 k->realize = virtio_ccw_blk_realize;
1561 k->exit = virtio_ccw_exit;
1562 dc->reset = virtio_ccw_reset;
1563 dc->props = virtio_ccw_blk_properties;
1564 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1567 static const TypeInfo virtio_ccw_blk = {
1568 .name = TYPE_VIRTIO_BLK_CCW,
1569 .parent = TYPE_VIRTIO_CCW_DEVICE,
1570 .instance_size = sizeof(VirtIOBlkCcw),
1571 .instance_init = virtio_ccw_blk_instance_init,
1572 .class_init = virtio_ccw_blk_class_init,
1575 static Property virtio_ccw_serial_properties[] = {
1576 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1577 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1578 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1579 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1580 VIRTIO_CCW_MAX_REV),
1581 DEFINE_PROP_END_OF_LIST(),
1584 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1586 DeviceClass *dc = DEVICE_CLASS(klass);
1587 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1589 k->realize = virtio_ccw_serial_realize;
1590 k->exit = virtio_ccw_exit;
1591 dc->reset = virtio_ccw_reset;
1592 dc->props = virtio_ccw_serial_properties;
1593 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1596 static const TypeInfo virtio_ccw_serial = {
1597 .name = TYPE_VIRTIO_SERIAL_CCW,
1598 .parent = TYPE_VIRTIO_CCW_DEVICE,
1599 .instance_size = sizeof(VirtioSerialCcw),
1600 .instance_init = virtio_ccw_serial_instance_init,
1601 .class_init = virtio_ccw_serial_class_init,
1604 static Property virtio_ccw_balloon_properties[] = {
1605 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1606 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1607 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1608 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1609 VIRTIO_CCW_MAX_REV),
1610 DEFINE_PROP_END_OF_LIST(),
1613 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1615 DeviceClass *dc = DEVICE_CLASS(klass);
1616 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1618 k->realize = virtio_ccw_balloon_realize;
1619 k->exit = virtio_ccw_exit;
1620 dc->reset = virtio_ccw_reset;
1621 dc->props = virtio_ccw_balloon_properties;
1622 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1625 static const TypeInfo virtio_ccw_balloon = {
1626 .name = TYPE_VIRTIO_BALLOON_CCW,
1627 .parent = TYPE_VIRTIO_CCW_DEVICE,
1628 .instance_size = sizeof(VirtIOBalloonCcw),
1629 .instance_init = virtio_ccw_balloon_instance_init,
1630 .class_init = virtio_ccw_balloon_class_init,
1633 static Property virtio_ccw_scsi_properties[] = {
1634 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1635 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1636 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1637 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1638 VIRTIO_CCW_MAX_REV),
1639 DEFINE_PROP_END_OF_LIST(),
1642 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1644 DeviceClass *dc = DEVICE_CLASS(klass);
1645 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1647 k->realize = virtio_ccw_scsi_realize;
1648 k->exit = virtio_ccw_exit;
1649 dc->reset = virtio_ccw_reset;
1650 dc->props = virtio_ccw_scsi_properties;
1651 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1654 static const TypeInfo virtio_ccw_scsi = {
1655 .name = TYPE_VIRTIO_SCSI_CCW,
1656 .parent = TYPE_VIRTIO_CCW_DEVICE,
1657 .instance_size = sizeof(VirtIOSCSICcw),
1658 .instance_init = virtio_ccw_scsi_instance_init,
1659 .class_init = virtio_ccw_scsi_class_init,
1662 #ifdef CONFIG_VHOST_SCSI
1663 static Property vhost_ccw_scsi_properties[] = {
1664 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1665 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1666 VIRTIO_CCW_MAX_REV),
1667 DEFINE_PROP_END_OF_LIST(),
1670 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1672 DeviceClass *dc = DEVICE_CLASS(klass);
1673 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1675 k->realize = vhost_ccw_scsi_realize;
1676 k->exit = virtio_ccw_exit;
1677 dc->reset = virtio_ccw_reset;
1678 dc->props = vhost_ccw_scsi_properties;
1679 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1682 static const TypeInfo vhost_ccw_scsi = {
1683 .name = TYPE_VHOST_SCSI_CCW,
1684 .parent = TYPE_VIRTIO_CCW_DEVICE,
1685 .instance_size = sizeof(VHostSCSICcw),
1686 .instance_init = vhost_ccw_scsi_instance_init,
1687 .class_init = vhost_ccw_scsi_class_init,
1691 static void virtio_ccw_rng_instance_init(Object *obj)
1693 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1695 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1697 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1698 "rng", &error_abort);
1701 static Property virtio_ccw_rng_properties[] = {
1702 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1703 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1704 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1705 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1706 VIRTIO_CCW_MAX_REV),
1707 DEFINE_PROP_END_OF_LIST(),
1710 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1712 DeviceClass *dc = DEVICE_CLASS(klass);
1713 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1715 k->realize = virtio_ccw_rng_realize;
1716 k->exit = virtio_ccw_exit;
1717 dc->reset = virtio_ccw_reset;
1718 dc->props = virtio_ccw_rng_properties;
1719 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1722 static const TypeInfo virtio_ccw_rng = {
1723 .name = TYPE_VIRTIO_RNG_CCW,
1724 .parent = TYPE_VIRTIO_CCW_DEVICE,
1725 .instance_size = sizeof(VirtIORNGCcw),
1726 .instance_init = virtio_ccw_rng_instance_init,
1727 .class_init = virtio_ccw_rng_class_init,
1730 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1732 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1734 virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1735 virtio_ccw_device_realize(_dev, errp);
1738 static int virtio_ccw_busdev_exit(DeviceState *dev)
1740 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1741 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1743 return _info->exit(_dev);
1746 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1747 DeviceState *dev, Error **errp)
1749 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1750 SubchDev *sch = _dev->sch;
1752 virtio_ccw_stop_ioeventfd(_dev);
1755 * We should arrive here only for device_del, since we don't support
1756 * direct hot(un)plug of channels, but only through virtio.
1758 assert(sch != NULL);
1759 /* Subchannel is now disabled and no longer valid. */
1760 sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1761 PMCW_FLAGS_MASK_DNV);
1763 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1765 object_unparent(OBJECT(dev));
1768 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1770 DeviceClass *dc = DEVICE_CLASS(klass);
1772 dc->realize = virtio_ccw_busdev_realize;
1773 dc->exit = virtio_ccw_busdev_exit;
1774 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1777 static const TypeInfo virtio_ccw_device_info = {
1778 .name = TYPE_VIRTIO_CCW_DEVICE,
1779 .parent = TYPE_DEVICE,
1780 .instance_size = sizeof(VirtioCcwDevice),
1781 .class_init = virtio_ccw_device_class_init,
1782 .class_size = sizeof(VirtIOCCWDeviceClass),
1786 /***************** Virtual-css Bus Bridge Device ********************/
1787 /* Only required to have the virtio bus as child in the system bus */
1789 static int virtual_css_bridge_init(SysBusDevice *dev)
1795 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1797 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1798 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1799 DeviceClass *dc = DEVICE_CLASS(klass);
1801 k->init = virtual_css_bridge_init;
1802 hc->unplug = virtio_ccw_busdev_unplug;
1803 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1806 static const TypeInfo virtual_css_bridge_info = {
1807 .name = "virtual-css-bridge",
1808 .parent = TYPE_SYS_BUS_DEVICE,
1809 .instance_size = sizeof(SysBusDevice),
1810 .class_init = virtual_css_bridge_class_init,
1811 .interfaces = (InterfaceInfo[]) {
1812 { TYPE_HOTPLUG_HANDLER },
1817 /* virtio-ccw-bus */
1819 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1820 VirtioCcwDevice *dev)
1822 DeviceState *qdev = DEVICE(dev);
1823 char virtio_bus_name[] = "virtio-bus";
1825 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1826 qdev, virtio_bus_name);
1829 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1831 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1832 BusClass *bus_class = BUS_CLASS(klass);
1834 bus_class->max_dev = 1;
1835 k->notify = virtio_ccw_notify;
1836 k->vmstate_change = virtio_ccw_vmstate_change;
1837 k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1838 k->set_host_notifier = virtio_ccw_set_host_notifier;
1839 k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1840 k->save_queue = virtio_ccw_save_queue;
1841 k->load_queue = virtio_ccw_load_queue;
1842 k->save_config = virtio_ccw_save_config;
1843 k->load_config = virtio_ccw_load_config;
1844 k->device_plugged = virtio_ccw_device_plugged;
1845 k->post_plugged = virtio_ccw_post_plugged;
1846 k->device_unplugged = virtio_ccw_device_unplugged;
1849 static const TypeInfo virtio_ccw_bus_info = {
1850 .name = TYPE_VIRTIO_CCW_BUS,
1851 .parent = TYPE_VIRTIO_BUS,
1852 .instance_size = sizeof(VirtioCcwBusState),
1853 .class_init = virtio_ccw_bus_class_init,
1856 #ifdef CONFIG_VIRTFS
1857 static Property virtio_ccw_9p_properties[] = {
1858 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1859 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1860 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1861 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1862 VIRTIO_CCW_MAX_REV),
1863 DEFINE_PROP_END_OF_LIST(),
1866 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1868 V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1869 DeviceState *vdev = DEVICE(&dev->vdev);
1872 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1873 object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1875 error_propagate(errp, err);
1879 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1881 DeviceClass *dc = DEVICE_CLASS(klass);
1882 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1884 k->exit = virtio_ccw_exit;
1885 k->realize = virtio_ccw_9p_realize;
1886 dc->reset = virtio_ccw_reset;
1887 dc->props = virtio_ccw_9p_properties;
1888 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1891 static void virtio_ccw_9p_instance_init(Object *obj)
1893 V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1895 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1899 static const TypeInfo virtio_ccw_9p_info = {
1900 .name = TYPE_VIRTIO_9P_CCW,
1901 .parent = TYPE_VIRTIO_CCW_DEVICE,
1902 .instance_size = sizeof(V9fsCCWState),
1903 .instance_init = virtio_ccw_9p_instance_init,
1904 .class_init = virtio_ccw_9p_class_init,
1908 static void virtio_ccw_register(void)
1910 type_register_static(&virtio_ccw_bus_info);
1911 type_register_static(&virtual_css_bus_info);
1912 type_register_static(&virtio_ccw_device_info);
1913 type_register_static(&virtio_ccw_serial);
1914 type_register_static(&virtio_ccw_blk);
1915 type_register_static(&virtio_ccw_net);
1916 type_register_static(&virtio_ccw_balloon);
1917 type_register_static(&virtio_ccw_scsi);
1918 #ifdef CONFIG_VHOST_SCSI
1919 type_register_static(&vhost_ccw_scsi);
1921 type_register_static(&virtio_ccw_rng);
1922 type_register_static(&virtual_css_bridge_info);
1923 #ifdef CONFIG_VIRTFS
1924 type_register_static(&virtio_ccw_9p_info);
1928 type_init(virtio_ccw_register)