2 * virtio ccw target implementation
4 * Copyright 2012 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
13 #include "block/block.h"
14 #include "sysemu/blockdev.h"
15 #include "sysemu/sysemu.h"
17 #include "monitor/monitor.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/virtio/virtio-serial.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "hw/sysbus.h"
22 #include "qemu/bitops.h"
23 #include "hw/virtio/virtio-bus.h"
27 #include "virtio-ccw.h"
30 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
31 VirtioCcwDevice
*dev
);
33 static void virtual_css_bus_reset(BusState
*qbus
)
35 /* This should actually be modelled via the generic css */
40 static void virtual_css_bus_class_init(ObjectClass
*klass
, void *data
)
42 BusClass
*k
= BUS_CLASS(klass
);
44 k
->reset
= virtual_css_bus_reset
;
47 static const TypeInfo virtual_css_bus_info
= {
48 .name
= TYPE_VIRTUAL_CSS_BUS
,
50 .instance_size
= sizeof(VirtualCssBus
),
51 .class_init
= virtual_css_bus_class_init
,
54 VirtIODevice
*virtio_ccw_get_vdev(SubchDev
*sch
)
56 VirtIODevice
*vdev
= NULL
;
57 VirtioCcwDevice
*dev
= sch
->driver_data
;
60 vdev
= virtio_bus_get_device(&dev
->bus
);
65 static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice
*dev
, int n
,
66 bool assign
, bool set_handler
)
68 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
69 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
70 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
72 SubchDev
*sch
= dev
->sch
;
73 uint32_t sch_id
= (css_build_subchannel_id(sch
) << 16) | sch
->schid
;
76 r
= event_notifier_init(notifier
, 1);
78 error_report("%s: unable to init event notifier: %d", __func__
, r
);
81 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
82 r
= s390_assign_subch_ioeventfd(notifier
, sch_id
, n
, assign
);
84 error_report("%s: unable to assign ioeventfd: %d", __func__
, r
);
85 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
86 event_notifier_cleanup(notifier
);
90 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
91 s390_assign_subch_ioeventfd(notifier
, sch_id
, n
, assign
);
92 event_notifier_cleanup(notifier
);
97 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice
*dev
)
102 if (!(dev
->flags
& VIRTIO_CCW_FLAG_USE_IOEVENTFD
) ||
103 dev
->ioeventfd_disabled
||
104 dev
->ioeventfd_started
) {
107 vdev
= virtio_bus_get_device(&dev
->bus
);
108 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
109 if (!virtio_queue_get_num(vdev
, n
)) {
112 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, true, true);
117 dev
->ioeventfd_started
= true;
122 if (!virtio_queue_get_num(vdev
, n
)) {
125 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, false, false);
128 dev
->ioeventfd_started
= false;
129 /* Disable ioeventfd for this device. */
130 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
131 error_report("%s: failed. Fallback to userspace (slower).", __func__
);
134 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice
*dev
)
139 if (!dev
->ioeventfd_started
) {
142 vdev
= virtio_bus_get_device(&dev
->bus
);
143 for (n
= 0; n
< VIRTIO_PCI_QUEUE_MAX
; n
++) {
144 if (!virtio_queue_get_num(vdev
, n
)) {
147 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, false, false);
150 dev
->ioeventfd_started
= false;
153 VirtualCssBus
*virtual_css_bus_init(void)
159 /* Create bridge device */
160 dev
= qdev_create(NULL
, "virtual-css-bridge");
161 qdev_init_nofail(dev
);
163 /* Create bus on bridge device */
164 bus
= qbus_create(TYPE_VIRTUAL_CSS_BUS
, dev
, "virtual-css");
165 cbus
= VIRTUAL_CSS_BUS(bus
);
167 /* Enable hotplugging */
168 bus
->allow_hotplug
= 1;
173 /* Communication blocks used by several channel commands. */
174 typedef struct VqInfoBlock
{
179 } QEMU_PACKED VqInfoBlock
;
181 typedef struct VqConfigBlock
{
184 } QEMU_PACKED VqConfigBlock
;
186 typedef struct VirtioFeatDesc
{
189 } QEMU_PACKED VirtioFeatDesc
;
191 /* Specify where the virtqueues for the subchannel are in guest memory. */
192 static int virtio_ccw_set_vqs(SubchDev
*sch
, uint64_t addr
, uint32_t align
,
193 uint16_t index
, uint16_t num
)
195 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
197 if (index
> VIRTIO_PCI_QUEUE_MAX
) {
201 /* Current code in virtio.c relies on 4K alignment. */
202 if (addr
&& (align
!= 4096)) {
210 virtio_queue_set_addr(vdev
, index
, addr
);
212 virtio_queue_set_vector(vdev
, index
, 0);
214 /* Fail if we don't have a big enough queue. */
215 /* TODO: Add interface to handle vring.num changing */
216 if (virtio_queue_get_num(vdev
, index
) > num
) {
219 virtio_queue_set_vector(vdev
, index
, index
);
221 /* tell notify handler in case of config change */
222 vdev
->config_vector
= VIRTIO_PCI_QUEUE_MAX
;
226 static int virtio_ccw_cb(SubchDev
*sch
, CCW1 ccw
)
231 VirtioFeatDesc features
;
234 VqConfigBlock vq_config
;
235 VirtioCcwDevice
*dev
= sch
->driver_data
;
236 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
245 trace_virtio_ccw_interpret_ccw(sch
->cssid
, sch
->ssid
, sch
->schid
,
247 check_len
= !((ccw
.flags
& CCW_FLAG_SLI
) && !(ccw
.flags
& CCW_FLAG_DC
));
249 /* Look at the command. */
250 switch (ccw
.cmd_code
) {
253 if (ccw
.count
!= sizeof(info
)) {
257 } else if (ccw
.count
< sizeof(info
)) {
258 /* Can't execute command. */
265 info
.queue
= ldq_phys(&address_space_memory
, ccw
.cda
);
266 info
.align
= ldl_phys(&address_space_memory
,
267 ccw
.cda
+ sizeof(info
.queue
));
268 info
.index
= lduw_phys(ccw
.cda
+ sizeof(info
.queue
)
269 + sizeof(info
.align
));
270 info
.num
= lduw_phys(ccw
.cda
+ sizeof(info
.queue
)
272 + sizeof(info
.index
));
273 ret
= virtio_ccw_set_vqs(sch
, info
.queue
, info
.align
, info
.index
,
275 sch
->curr_status
.scsw
.count
= 0;
278 case CCW_CMD_VDEV_RESET
:
279 virtio_ccw_stop_ioeventfd(dev
);
283 case CCW_CMD_READ_FEAT
:
285 if (ccw
.count
!= sizeof(features
)) {
289 } else if (ccw
.count
< sizeof(features
)) {
290 /* Can't execute command. */
297 features
.index
= ldub_phys(&address_space_memory
,
298 ccw
.cda
+ sizeof(features
.features
));
299 if (features
.index
< ARRAY_SIZE(dev
->host_features
)) {
300 features
.features
= dev
->host_features
[features
.index
];
302 /* Return zeroes if the guest supports more feature bits. */
303 features
.features
= 0;
305 stl_le_phys(ccw
.cda
, features
.features
);
306 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
310 case CCW_CMD_WRITE_FEAT
:
312 if (ccw
.count
!= sizeof(features
)) {
316 } else if (ccw
.count
< sizeof(features
)) {
317 /* Can't execute command. */
324 features
.index
= ldub_phys(&address_space_memory
,
325 ccw
.cda
+ sizeof(features
.features
));
326 features
.features
= ldl_le_phys(&address_space_memory
, ccw
.cda
);
327 if (features
.index
< ARRAY_SIZE(dev
->host_features
)) {
328 virtio_bus_set_vdev_features(&dev
->bus
, features
.features
);
329 vdev
->guest_features
= features
.features
;
332 * If the guest supports more feature bits, assert that it
333 * passes us zeroes for those we don't support.
335 if (features
.features
) {
336 fprintf(stderr
, "Guest bug: features[%i]=%x (expected 0)\n",
337 features
.index
, features
.features
);
338 /* XXX: do a unit check here? */
341 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
345 case CCW_CMD_READ_CONF
:
347 if (ccw
.count
> vdev
->config_len
) {
352 len
= MIN(ccw
.count
, vdev
->config_len
);
356 virtio_bus_get_vdev_config(&dev
->bus
, vdev
->config
);
357 /* XXX config space endianness */
358 cpu_physical_memory_write(ccw
.cda
, vdev
->config
, len
);
359 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
363 case CCW_CMD_WRITE_CONF
:
365 if (ccw
.count
> vdev
->config_len
) {
370 len
= MIN(ccw
.count
, vdev
->config_len
);
375 config
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
380 /* XXX config space endianness */
381 memcpy(vdev
->config
, config
, len
);
382 cpu_physical_memory_unmap(config
, hw_len
, 0, hw_len
);
383 virtio_bus_set_vdev_config(&dev
->bus
, vdev
->config
);
384 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
389 case CCW_CMD_WRITE_STATUS
:
391 if (ccw
.count
!= sizeof(status
)) {
395 } else if (ccw
.count
< sizeof(status
)) {
396 /* Can't execute command. */
403 status
= ldub_phys(&address_space_memory
, ccw
.cda
);
404 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
405 virtio_ccw_stop_ioeventfd(dev
);
407 virtio_set_status(vdev
, status
);
408 if (vdev
->status
== 0) {
411 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
412 virtio_ccw_start_ioeventfd(dev
);
414 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(status
);
418 case CCW_CMD_SET_IND
:
420 if (ccw
.count
!= sizeof(indicators
)) {
424 } else if (ccw
.count
< sizeof(indicators
)) {
425 /* Can't execute command. */
432 indicators
= ldq_phys(&address_space_memory
, ccw
.cda
);
433 dev
->indicators
= indicators
;
434 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
438 case CCW_CMD_SET_CONF_IND
:
440 if (ccw
.count
!= sizeof(indicators
)) {
444 } else if (ccw
.count
< sizeof(indicators
)) {
445 /* Can't execute command. */
452 indicators
= ldq_phys(&address_space_memory
, ccw
.cda
);
453 dev
->indicators2
= indicators
;
454 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
458 case CCW_CMD_READ_VQ_CONF
:
460 if (ccw
.count
!= sizeof(vq_config
)) {
464 } else if (ccw
.count
< sizeof(vq_config
)) {
465 /* Can't execute command. */
472 vq_config
.index
= lduw_phys(ccw
.cda
);
473 vq_config
.num_max
= virtio_queue_get_num(vdev
,
475 stw_phys(ccw
.cda
+ sizeof(vq_config
.index
), vq_config
.num_max
);
476 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(vq_config
);
487 static int virtio_ccw_device_init(VirtioCcwDevice
*dev
, VirtIODevice
*vdev
)
489 unsigned int cssid
= 0;
490 unsigned int ssid
= 0;
493 bool have_devno
= false;
498 DeviceState
*parent
= DEVICE(dev
);
500 sch
= g_malloc0(sizeof(SubchDev
));
502 sch
->driver_data
= dev
;
507 /* Initialize subchannel structure. */
508 sch
->channel_prog
= 0x0;
509 sch
->last_cmd_valid
= false;
512 * Use a device number if provided. Otherwise, fall back to subchannel
516 num
= sscanf(dev
->bus_id
, "%x.%x.%04x", &cssid
, &ssid
, &devno
);
518 if ((cssid
> MAX_CSSID
) || (ssid
> MAX_SSID
)) {
520 error_report("Invalid cssid or ssid: cssid %x, ssid %x",
524 /* Enforce use of virtual cssid. */
525 if (cssid
!= VIRTUAL_CSSID
) {
527 error_report("cssid %x not valid for virtio devices", cssid
);
530 if (css_devno_used(cssid
, ssid
, devno
)) {
532 error_report("Device %x.%x.%04x already exists", cssid
, ssid
,
542 error_report("Malformed devno parameter '%s'", dev
->bus_id
);
547 /* Find the next free id. */
549 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
550 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
552 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
559 error_report("No free subchannel found for %x.%x.%04x", cssid
, ssid
,
563 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
566 cssid
= VIRTUAL_CSSID
;
567 for (ssid
= 0; ssid
<= MAX_SSID
; ssid
++) {
568 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
569 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
575 * If the devno is already taken, look further in this
578 while (css_devno_used(cssid
, ssid
, devno
)) {
579 if (devno
== MAX_SCHID
) {
581 } else if (devno
== schid
- 1) {
583 error_report("No free devno found");
590 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
601 error_report("Virtual channel subsystem is full!");
604 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
608 /* Build initial schib. */
609 css_sch_build_virtual_schib(sch
, 0, VIRTIO_CCW_CHPID_TYPE
);
611 sch
->ccw_cb
= virtio_ccw_cb
;
613 /* Build senseid data. */
614 memset(&sch
->id
, 0, sizeof(SenseId
));
615 sch
->id
.reserved
= 0xff;
616 sch
->id
.cu_type
= VIRTIO_CCW_CU_TYPE
;
617 sch
->id
.cu_model
= vdev
->device_id
;
619 /* Only the first 32 feature bits are used. */
620 dev
->host_features
[0] = virtio_bus_get_vdev_features(&dev
->bus
,
621 dev
->host_features
[0]);
623 dev
->host_features
[0] |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY
;
624 dev
->host_features
[0] |= 0x1 << VIRTIO_F_BAD_FEATURE
;
626 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
627 parent
->hotplugged
, 1);
636 static int virtio_ccw_exit(VirtioCcwDevice
*dev
)
638 SubchDev
*sch
= dev
->sch
;
641 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
648 static int virtio_ccw_net_init(VirtioCcwDevice
*ccw_dev
)
650 DeviceState
*qdev
= DEVICE(ccw_dev
);
651 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(ccw_dev
);
652 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
654 virtio_net_set_config_size(&dev
->vdev
, ccw_dev
->host_features
[0]);
655 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
656 object_get_typename(OBJECT(qdev
)));
657 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
658 if (qdev_init(vdev
) < 0) {
662 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
665 static void virtio_ccw_net_instance_init(Object
*obj
)
667 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(obj
);
668 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_NET
);
669 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
672 static int virtio_ccw_blk_init(VirtioCcwDevice
*ccw_dev
)
674 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(ccw_dev
);
675 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
676 virtio_blk_set_conf(vdev
, &(dev
->blk
));
677 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
678 if (qdev_init(vdev
) < 0) {
682 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
685 static void virtio_ccw_blk_instance_init(Object
*obj
)
687 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(obj
);
688 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BLK
);
689 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
692 static int virtio_ccw_serial_init(VirtioCcwDevice
*ccw_dev
)
694 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(ccw_dev
);
695 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
696 DeviceState
*proxy
= DEVICE(ccw_dev
);
700 * For command line compatibility, this sets the virtio-serial-device bus
704 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
705 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
709 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
710 if (qdev_init(vdev
) < 0) {
714 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
718 static void virtio_ccw_serial_instance_init(Object
*obj
)
720 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(obj
);
721 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SERIAL
);
722 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
725 static int virtio_ccw_balloon_init(VirtioCcwDevice
*ccw_dev
)
727 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(ccw_dev
);
728 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
730 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
731 if (qdev_init(vdev
) < 0) {
735 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
738 static void balloon_ccw_stats_get_all(Object
*obj
, struct Visitor
*v
,
739 void *opaque
, const char *name
,
742 VirtIOBalloonCcw
*dev
= opaque
;
743 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats", errp
);
746 static void balloon_ccw_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
747 void *opaque
, const char *name
,
750 VirtIOBalloonCcw
*dev
= opaque
;
751 object_property_get(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
755 static void balloon_ccw_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
756 void *opaque
, const char *name
,
759 VirtIOBalloonCcw
*dev
= opaque
;
760 object_property_set(OBJECT(&dev
->vdev
), v
, "guest-stats-polling-interval",
764 static void virtio_ccw_balloon_instance_init(Object
*obj
)
766 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(obj
);
767 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_BALLOON
);
768 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
770 object_property_add(obj
, "guest-stats", "guest statistics",
771 balloon_ccw_stats_get_all
, NULL
, NULL
, dev
, NULL
);
773 object_property_add(obj
, "guest-stats-polling-interval", "int",
774 balloon_ccw_stats_get_poll_interval
,
775 balloon_ccw_stats_set_poll_interval
,
779 static int virtio_ccw_scsi_init(VirtioCcwDevice
*ccw_dev
)
781 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(ccw_dev
);
782 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
783 DeviceState
*qdev
= DEVICE(ccw_dev
);
787 * For command line compatibility, this sets the virtio-scsi-device bus
791 bus_name
= g_strdup_printf("%s.0", qdev
->id
);
792 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
796 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
797 if (qdev_init(vdev
) < 0) {
801 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
804 static void virtio_ccw_scsi_instance_init(Object
*obj
)
806 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(obj
);
807 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_SCSI
);
808 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
811 #ifdef CONFIG_VHOST_SCSI
812 static int vhost_ccw_scsi_init(VirtioCcwDevice
*ccw_dev
)
814 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(ccw_dev
);
815 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
817 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
818 if (qdev_init(vdev
) < 0) {
822 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
825 static void vhost_ccw_scsi_instance_init(Object
*obj
)
827 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(obj
);
828 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VHOST_SCSI
);
829 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
833 static int virtio_ccw_rng_init(VirtioCcwDevice
*ccw_dev
)
835 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(ccw_dev
);
836 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
838 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
839 if (qdev_init(vdev
) < 0) {
843 object_property_set_link(OBJECT(dev
),
844 OBJECT(dev
->vdev
.conf
.rng
), "rng",
847 return virtio_ccw_device_init(ccw_dev
, VIRTIO_DEVICE(vdev
));
850 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
851 * be careful and test performance if you change this.
853 static inline VirtioCcwDevice
*to_virtio_ccw_dev_fast(DeviceState
*d
)
855 return container_of(d
, VirtioCcwDevice
, parent_obj
);
858 static void virtio_ccw_notify(DeviceState
*d
, uint16_t vector
)
860 VirtioCcwDevice
*dev
= to_virtio_ccw_dev_fast(d
);
861 SubchDev
*sch
= dev
->sch
;
868 if (vector
< VIRTIO_PCI_QUEUE_MAX
) {
869 if (!dev
->indicators
) {
872 indicators
= ldq_phys(&address_space_memory
, dev
->indicators
);
873 indicators
|= 1ULL << vector
;
874 stq_phys(dev
->indicators
, indicators
);
876 if (!dev
->indicators2
) {
880 indicators
= ldq_phys(&address_space_memory
, dev
->indicators2
);
881 indicators
|= 1ULL << vector
;
882 stq_phys(dev
->indicators2
, indicators
);
885 css_conditional_io_interrupt(sch
);
889 static unsigned virtio_ccw_get_features(DeviceState
*d
)
891 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
893 /* Only the first 32 feature bits are used. */
894 return dev
->host_features
[0];
897 static void virtio_ccw_reset(DeviceState
*d
)
899 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
900 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
902 virtio_ccw_stop_ioeventfd(dev
);
904 css_reset_sch(dev
->sch
);
906 dev
->indicators2
= 0;
909 static void virtio_ccw_vmstate_change(DeviceState
*d
, bool running
)
911 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
914 virtio_ccw_start_ioeventfd(dev
);
916 virtio_ccw_stop_ioeventfd(dev
);
920 static bool virtio_ccw_query_guest_notifiers(DeviceState
*d
)
922 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
924 return !!(dev
->sch
->curr_status
.pmcw
.flags
& PMCW_FLAGS_MASK_ENA
);
927 static int virtio_ccw_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
929 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
931 /* Stop using the generic ioeventfd, we are doing eventfd handling
933 dev
->ioeventfd_disabled
= assign
;
935 virtio_ccw_stop_ioeventfd(dev
);
937 return virtio_ccw_set_guest2host_notifier(dev
, n
, assign
, false);
940 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice
*dev
, int n
,
941 bool assign
, bool with_irqfd
)
943 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
944 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
945 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
946 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
949 int r
= event_notifier_init(notifier
, 0);
954 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
955 /* We do not support irqfd for classic I/O interrupts, because the
956 * classic interrupts are intermixed with the subchannel status, that
957 * is queried with test subchannel. We want to use vhost, though.
958 * Lets make sure to have vhost running and wire up the irq fd to
959 * land in qemu (and only the irq fd) in this code.
961 if (k
->guest_notifier_mask
) {
962 k
->guest_notifier_mask(vdev
, n
, false);
964 /* get lost events and re-inject */
965 if (k
->guest_notifier_pending
&&
966 k
->guest_notifier_pending(vdev
, n
)) {
967 event_notifier_set(notifier
);
970 if (k
->guest_notifier_mask
) {
971 k
->guest_notifier_mask(vdev
, n
, true);
973 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
974 event_notifier_cleanup(notifier
);
979 static int virtio_ccw_set_guest_notifiers(DeviceState
*d
, int nvqs
,
982 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
983 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
986 for (n
= 0; n
< nvqs
; n
++) {
987 if (!virtio_queue_get_num(vdev
, n
)) {
990 /* false -> true, as soon as irqfd works */
991 r
= virtio_ccw_set_guest_notifier(dev
, n
, assigned
, false);
1000 virtio_ccw_set_guest_notifier(dev
, n
, !assigned
, false);
1005 /**************** Virtio-ccw Bus Device Descriptions *******************/
1007 static Property virtio_ccw_net_properties
[] = {
1008 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1009 DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice
, host_features
[0]),
1010 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetCcw
, vdev
.net_conf
),
1011 DEFINE_NIC_PROPERTIES(VirtIONetCcw
, vdev
.nic_conf
),
1012 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1013 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1014 DEFINE_PROP_END_OF_LIST(),
1017 static void virtio_ccw_net_class_init(ObjectClass
*klass
, void *data
)
1019 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1020 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1022 k
->init
= virtio_ccw_net_init
;
1023 k
->exit
= virtio_ccw_exit
;
1024 dc
->reset
= virtio_ccw_reset
;
1025 dc
->props
= virtio_ccw_net_properties
;
1028 static const TypeInfo virtio_ccw_net
= {
1029 .name
= TYPE_VIRTIO_NET_CCW
,
1030 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1031 .instance_size
= sizeof(VirtIONetCcw
),
1032 .instance_init
= virtio_ccw_net_instance_init
,
1033 .class_init
= virtio_ccw_net_class_init
,
1036 static Property virtio_ccw_blk_properties
[] = {
1037 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1038 DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice
, host_features
[0]),
1039 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkCcw
, blk
),
1040 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1041 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1042 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1043 DEFINE_PROP_BIT("x-data-plane", VirtIOBlkCcw
, blk
.data_plane
, 0, false),
1045 DEFINE_PROP_END_OF_LIST(),
1048 static void virtio_ccw_blk_class_init(ObjectClass
*klass
, void *data
)
1050 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1051 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1053 k
->init
= virtio_ccw_blk_init
;
1054 k
->exit
= virtio_ccw_exit
;
1055 dc
->reset
= virtio_ccw_reset
;
1056 dc
->props
= virtio_ccw_blk_properties
;
1059 static const TypeInfo virtio_ccw_blk
= {
1060 .name
= TYPE_VIRTIO_BLK_CCW
,
1061 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1062 .instance_size
= sizeof(VirtIOBlkCcw
),
1063 .instance_init
= virtio_ccw_blk_instance_init
,
1064 .class_init
= virtio_ccw_blk_class_init
,
1067 static Property virtio_ccw_serial_properties
[] = {
1068 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1069 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtioSerialCcw
, vdev
.serial
),
1070 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice
, host_features
[0]),
1071 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1072 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1073 DEFINE_PROP_END_OF_LIST(),
1076 static void virtio_ccw_serial_class_init(ObjectClass
*klass
, void *data
)
1078 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1079 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1081 k
->init
= virtio_ccw_serial_init
;
1082 k
->exit
= virtio_ccw_exit
;
1083 dc
->reset
= virtio_ccw_reset
;
1084 dc
->props
= virtio_ccw_serial_properties
;
1087 static const TypeInfo virtio_ccw_serial
= {
1088 .name
= TYPE_VIRTIO_SERIAL_CCW
,
1089 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1090 .instance_size
= sizeof(VirtioSerialCcw
),
1091 .instance_init
= virtio_ccw_serial_instance_init
,
1092 .class_init
= virtio_ccw_serial_class_init
,
1095 static Property virtio_ccw_balloon_properties
[] = {
1096 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1097 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice
, host_features
[0]),
1098 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1099 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1100 DEFINE_PROP_END_OF_LIST(),
1103 static void virtio_ccw_balloon_class_init(ObjectClass
*klass
, void *data
)
1105 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1106 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1108 k
->init
= virtio_ccw_balloon_init
;
1109 k
->exit
= virtio_ccw_exit
;
1110 dc
->reset
= virtio_ccw_reset
;
1111 dc
->props
= virtio_ccw_balloon_properties
;
1114 static const TypeInfo virtio_ccw_balloon
= {
1115 .name
= TYPE_VIRTIO_BALLOON_CCW
,
1116 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1117 .instance_size
= sizeof(VirtIOBalloonCcw
),
1118 .instance_init
= virtio_ccw_balloon_instance_init
,
1119 .class_init
= virtio_ccw_balloon_class_init
,
1122 static Property virtio_ccw_scsi_properties
[] = {
1123 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1124 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSICcw
, vdev
.parent_obj
.conf
),
1125 DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice
, host_features
[0]),
1126 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1127 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1128 DEFINE_PROP_END_OF_LIST(),
1131 static void virtio_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1133 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1134 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1136 k
->init
= virtio_ccw_scsi_init
;
1137 k
->exit
= virtio_ccw_exit
;
1138 dc
->reset
= virtio_ccw_reset
;
1139 dc
->props
= virtio_ccw_scsi_properties
;
1142 static const TypeInfo virtio_ccw_scsi
= {
1143 .name
= TYPE_VIRTIO_SCSI_CCW
,
1144 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1145 .instance_size
= sizeof(VirtIOSCSICcw
),
1146 .instance_init
= virtio_ccw_scsi_instance_init
,
1147 .class_init
= virtio_ccw_scsi_class_init
,
1150 #ifdef CONFIG_VHOST_SCSI
1151 static Property vhost_ccw_scsi_properties
[] = {
1152 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1153 DEFINE_VHOST_SCSI_PROPERTIES(VirtIOSCSICcw
, vdev
.parent_obj
.conf
),
1154 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice
, host_features
[0]),
1155 DEFINE_PROP_END_OF_LIST(),
1158 static void vhost_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1160 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1161 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1163 k
->init
= vhost_ccw_scsi_init
;
1164 k
->exit
= virtio_ccw_exit
;
1165 dc
->reset
= virtio_ccw_reset
;
1166 dc
->props
= vhost_ccw_scsi_properties
;
1169 static const TypeInfo vhost_ccw_scsi
= {
1170 .name
= TYPE_VHOST_SCSI_CCW
,
1171 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1172 .instance_size
= sizeof(VirtIOSCSICcw
),
1173 .instance_init
= vhost_ccw_scsi_instance_init
,
1174 .class_init
= vhost_ccw_scsi_class_init
,
1178 static void virtio_ccw_rng_instance_init(Object
*obj
)
1180 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(obj
);
1181 object_initialize(&dev
->vdev
, sizeof(dev
->vdev
), TYPE_VIRTIO_RNG
);
1182 object_property_add_child(obj
, "virtio-backend", OBJECT(&dev
->vdev
), NULL
);
1183 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
1184 (Object
**)&dev
->vdev
.conf
.rng
, NULL
);
1187 static Property virtio_ccw_rng_properties
[] = {
1188 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1189 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice
, host_features
[0]),
1190 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNGCcw
, vdev
.conf
),
1191 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1192 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1193 DEFINE_PROP_END_OF_LIST(),
1196 static void virtio_ccw_rng_class_init(ObjectClass
*klass
, void *data
)
1198 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1199 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1201 k
->init
= virtio_ccw_rng_init
;
1202 k
->exit
= virtio_ccw_exit
;
1203 dc
->reset
= virtio_ccw_reset
;
1204 dc
->props
= virtio_ccw_rng_properties
;
1207 static const TypeInfo virtio_ccw_rng
= {
1208 .name
= TYPE_VIRTIO_RNG_CCW
,
1209 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1210 .instance_size
= sizeof(VirtIORNGCcw
),
1211 .instance_init
= virtio_ccw_rng_instance_init
,
1212 .class_init
= virtio_ccw_rng_class_init
,
1215 static int virtio_ccw_busdev_init(DeviceState
*dev
)
1217 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1218 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1220 virtio_ccw_bus_new(&_dev
->bus
, sizeof(_dev
->bus
), _dev
);
1222 return _info
->init(_dev
);
1225 static int virtio_ccw_busdev_exit(DeviceState
*dev
)
1227 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1228 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1230 return _info
->exit(_dev
);
1233 static int virtio_ccw_busdev_unplug(DeviceState
*dev
)
1235 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1236 SubchDev
*sch
= _dev
->sch
;
1238 virtio_ccw_stop_ioeventfd(_dev
);
1241 * We should arrive here only for device_del, since we don't support
1242 * direct hot(un)plug of channels, but only through virtio.
1244 assert(sch
!= NULL
);
1245 /* Subchannel is now disabled and no longer valid. */
1246 sch
->curr_status
.pmcw
.flags
&= ~(PMCW_FLAGS_MASK_ENA
|
1247 PMCW_FLAGS_MASK_DNV
);
1249 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
, 1, 0);
1251 object_unparent(OBJECT(dev
));
1255 static void virtio_ccw_device_class_init(ObjectClass
*klass
, void *data
)
1257 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1259 dc
->init
= virtio_ccw_busdev_init
;
1260 dc
->exit
= virtio_ccw_busdev_exit
;
1261 dc
->unplug
= virtio_ccw_busdev_unplug
;
1262 dc
->bus_type
= TYPE_VIRTUAL_CSS_BUS
;
1266 static const TypeInfo virtio_ccw_device_info
= {
1267 .name
= TYPE_VIRTIO_CCW_DEVICE
,
1268 .parent
= TYPE_DEVICE
,
1269 .instance_size
= sizeof(VirtioCcwDevice
),
1270 .class_init
= virtio_ccw_device_class_init
,
1271 .class_size
= sizeof(VirtIOCCWDeviceClass
),
1275 /***************** Virtual-css Bus Bridge Device ********************/
1276 /* Only required to have the virtio bus as child in the system bus */
1278 static int virtual_css_bridge_init(SysBusDevice
*dev
)
1284 static void virtual_css_bridge_class_init(ObjectClass
*klass
, void *data
)
1286 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1288 k
->init
= virtual_css_bridge_init
;
1291 static const TypeInfo virtual_css_bridge_info
= {
1292 .name
= "virtual-css-bridge",
1293 .parent
= TYPE_SYS_BUS_DEVICE
,
1294 .instance_size
= sizeof(SysBusDevice
),
1295 .class_init
= virtual_css_bridge_class_init
,
1298 /* virtio-ccw-bus */
1300 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1301 VirtioCcwDevice
*dev
)
1303 DeviceState
*qdev
= DEVICE(dev
);
1305 char virtio_bus_name
[] = "virtio-bus";
1307 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_CCW_BUS
,
1308 qdev
, virtio_bus_name
);
1310 qbus
->allow_hotplug
= 1;
1313 static void virtio_ccw_bus_class_init(ObjectClass
*klass
, void *data
)
1315 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1316 BusClass
*bus_class
= BUS_CLASS(klass
);
1318 bus_class
->max_dev
= 1;
1319 k
->notify
= virtio_ccw_notify
;
1320 k
->get_features
= virtio_ccw_get_features
;
1321 k
->vmstate_change
= virtio_ccw_vmstate_change
;
1322 k
->query_guest_notifiers
= virtio_ccw_query_guest_notifiers
;
1323 k
->set_host_notifier
= virtio_ccw_set_host_notifier
;
1324 k
->set_guest_notifiers
= virtio_ccw_set_guest_notifiers
;
1327 static const TypeInfo virtio_ccw_bus_info
= {
1328 .name
= TYPE_VIRTIO_CCW_BUS
,
1329 .parent
= TYPE_VIRTIO_BUS
,
1330 .instance_size
= sizeof(VirtioCcwBusState
),
1331 .class_init
= virtio_ccw_bus_class_init
,
1334 static void virtio_ccw_register(void)
1336 type_register_static(&virtio_ccw_bus_info
);
1337 type_register_static(&virtual_css_bus_info
);
1338 type_register_static(&virtio_ccw_device_info
);
1339 type_register_static(&virtio_ccw_serial
);
1340 type_register_static(&virtio_ccw_blk
);
1341 type_register_static(&virtio_ccw_net
);
1342 type_register_static(&virtio_ccw_balloon
);
1343 type_register_static(&virtio_ccw_scsi
);
1344 #ifdef CONFIG_VHOST_SCSI
1345 type_register_static(&vhost_ccw_scsi
);
1347 type_register_static(&virtio_ccw_rng
);
1348 type_register_static(&virtual_css_bridge_info
);
1351 type_init(virtio_ccw_register
)