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"
19 #include "sysemu/kvm.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-serial.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "hw/sysbus.h"
25 #include "qemu/bitops.h"
26 #include "qemu/error-report.h"
27 #include "hw/virtio/virtio-access.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/s390x/adapter.h"
30 #include "hw/s390x/s390_flic.h"
32 #include "hw/s390x/ioinst.h"
33 #include "hw/s390x/css.h"
34 #include "virtio-ccw.h"
37 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
38 VirtioCcwDevice
*dev
);
40 static void virtual_css_bus_reset(BusState
*qbus
)
42 /* This should actually be modelled via the generic css */
47 static void virtual_css_bus_class_init(ObjectClass
*klass
, void *data
)
49 BusClass
*k
= BUS_CLASS(klass
);
51 k
->reset
= virtual_css_bus_reset
;
54 static const TypeInfo virtual_css_bus_info
= {
55 .name
= TYPE_VIRTUAL_CSS_BUS
,
57 .instance_size
= sizeof(VirtualCssBus
),
58 .class_init
= virtual_css_bus_class_init
,
61 VirtIODevice
*virtio_ccw_get_vdev(SubchDev
*sch
)
63 VirtIODevice
*vdev
= NULL
;
64 VirtioCcwDevice
*dev
= sch
->driver_data
;
67 vdev
= virtio_bus_get_device(&dev
->bus
);
72 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice
*dev
)
74 virtio_bus_start_ioeventfd(&dev
->bus
);
77 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice
*dev
)
79 virtio_bus_stop_ioeventfd(&dev
->bus
);
82 static bool virtio_ccw_ioeventfd_started(DeviceState
*d
)
84 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
86 return dev
->ioeventfd_started
;
89 static void virtio_ccw_ioeventfd_set_started(DeviceState
*d
, bool started
,
92 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
94 dev
->ioeventfd_started
= started
;
96 /* Disable ioeventfd for this device. */
97 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
101 static bool virtio_ccw_ioeventfd_disabled(DeviceState
*d
)
103 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
105 return dev
->ioeventfd_disabled
||
106 !(dev
->flags
& VIRTIO_CCW_FLAG_USE_IOEVENTFD
);
109 static void virtio_ccw_ioeventfd_set_disabled(DeviceState
*d
, bool disabled
)
111 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
113 dev
->ioeventfd_disabled
= disabled
;
116 static int virtio_ccw_ioeventfd_assign(DeviceState
*d
, EventNotifier
*notifier
,
119 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
120 SubchDev
*sch
= dev
->sch
;
121 uint32_t sch_id
= (css_build_subchannel_id(sch
) << 16) | sch
->schid
;
123 return s390_assign_subch_ioeventfd(notifier
, sch_id
, n
, assign
);
126 VirtualCssBus
*virtual_css_bus_init(void)
132 /* Create bridge device */
133 dev
= qdev_create(NULL
, "virtual-css-bridge");
134 qdev_init_nofail(dev
);
136 /* Create bus on bridge device */
137 bus
= qbus_create(TYPE_VIRTUAL_CSS_BUS
, dev
, "virtual-css");
138 cbus
= VIRTUAL_CSS_BUS(bus
);
140 /* Enable hotplugging */
141 qbus_set_hotplug_handler(bus
, dev
, &error_abort
);
146 /* Communication blocks used by several channel commands. */
147 typedef struct VqInfoBlockLegacy
{
152 } QEMU_PACKED VqInfoBlockLegacy
;
154 typedef struct VqInfoBlock
{
161 } QEMU_PACKED VqInfoBlock
;
163 typedef struct VqConfigBlock
{
166 } QEMU_PACKED VqConfigBlock
;
168 typedef struct VirtioFeatDesc
{
171 } QEMU_PACKED VirtioFeatDesc
;
173 typedef struct VirtioThinintInfo
{
174 hwaddr summary_indicator
;
175 hwaddr device_indicator
;
178 } QEMU_PACKED VirtioThinintInfo
;
180 typedef struct VirtioRevInfo
{
184 } QEMU_PACKED VirtioRevInfo
;
186 /* Specify where the virtqueues for the subchannel are in guest memory. */
187 static int virtio_ccw_set_vqs(SubchDev
*sch
, VqInfoBlock
*info
,
188 VqInfoBlockLegacy
*linfo
)
190 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
191 uint16_t index
= info
? info
->index
: linfo
->index
;
192 uint16_t num
= info
? info
->num
: linfo
->num
;
193 uint64_t desc
= info
? info
->desc
: linfo
->queue
;
195 if (index
>= VIRTIO_CCW_QUEUE_MAX
) {
199 /* Current code in virtio.c relies on 4K alignment. */
200 if (linfo
&& desc
&& (linfo
->align
!= 4096)) {
209 virtio_queue_set_rings(vdev
, index
, desc
, info
->avail
, info
->used
);
211 virtio_queue_set_addr(vdev
, index
, desc
);
214 virtio_queue_set_vector(vdev
, index
, VIRTIO_NO_VECTOR
);
217 /* virtio-1 allows changing the ring size. */
218 if (virtio_queue_get_num(vdev
, index
) < num
) {
219 /* Fail if we exceed the maximum number. */
222 virtio_queue_set_num(vdev
, index
, num
);
223 } else if (virtio_queue_get_num(vdev
, index
) > num
) {
224 /* Fail if we don't have a big enough queue. */
227 /* We ignore possible increased num for legacy for compatibility. */
228 virtio_queue_set_vector(vdev
, index
, index
);
230 /* tell notify handler in case of config change */
231 vdev
->config_vector
= VIRTIO_CCW_QUEUE_MAX
;
235 static void virtio_ccw_reset_virtio(VirtioCcwDevice
*dev
, VirtIODevice
*vdev
)
237 virtio_ccw_stop_ioeventfd(dev
);
239 if (dev
->indicators
) {
240 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
241 dev
->indicators
= NULL
;
243 if (dev
->indicators2
) {
244 release_indicator(&dev
->routes
.adapter
, dev
->indicators2
);
245 dev
->indicators2
= NULL
;
247 if (dev
->summary_indicator
) {
248 release_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
249 dev
->summary_indicator
= NULL
;
251 dev
->sch
->thinint_active
= false;
254 static int virtio_ccw_handle_set_vq(SubchDev
*sch
, CCW1 ccw
, bool check_len
,
259 VqInfoBlockLegacy linfo
;
260 size_t info_len
= is_legacy
? sizeof(linfo
) : sizeof(info
);
263 if (ccw
.count
!= info_len
) {
266 } else if (ccw
.count
< info_len
) {
267 /* Can't execute command. */
274 linfo
.queue
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
275 MEMTXATTRS_UNSPECIFIED
, NULL
);
276 linfo
.align
= address_space_ldl_be(&address_space_memory
,
277 ccw
.cda
+ sizeof(linfo
.queue
),
278 MEMTXATTRS_UNSPECIFIED
,
280 linfo
.index
= address_space_lduw_be(&address_space_memory
,
281 ccw
.cda
+ sizeof(linfo
.queue
)
282 + sizeof(linfo
.align
),
283 MEMTXATTRS_UNSPECIFIED
,
285 linfo
.num
= address_space_lduw_be(&address_space_memory
,
286 ccw
.cda
+ sizeof(linfo
.queue
)
287 + sizeof(linfo
.align
)
288 + sizeof(linfo
.index
),
289 MEMTXATTRS_UNSPECIFIED
,
291 ret
= virtio_ccw_set_vqs(sch
, NULL
, &linfo
);
293 info
.desc
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
294 MEMTXATTRS_UNSPECIFIED
, NULL
);
295 info
.index
= address_space_lduw_be(&address_space_memory
,
296 ccw
.cda
+ sizeof(info
.desc
)
298 MEMTXATTRS_UNSPECIFIED
, NULL
);
299 info
.num
= address_space_lduw_be(&address_space_memory
,
300 ccw
.cda
+ sizeof(info
.desc
)
302 + sizeof(info
.index
),
303 MEMTXATTRS_UNSPECIFIED
, NULL
);
304 info
.avail
= address_space_ldq_be(&address_space_memory
,
305 ccw
.cda
+ sizeof(info
.desc
)
309 MEMTXATTRS_UNSPECIFIED
, NULL
);
310 info
.used
= address_space_ldq_be(&address_space_memory
,
311 ccw
.cda
+ sizeof(info
.desc
)
315 + sizeof(info
.avail
),
316 MEMTXATTRS_UNSPECIFIED
, NULL
);
317 ret
= virtio_ccw_set_vqs(sch
, &info
, NULL
);
319 sch
->curr_status
.scsw
.count
= 0;
323 static int virtio_ccw_cb(SubchDev
*sch
, CCW1 ccw
)
326 VirtioRevInfo revinfo
;
328 VirtioFeatDesc features
;
331 VqConfigBlock vq_config
;
332 VirtioCcwDevice
*dev
= sch
->driver_data
;
333 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
337 VirtioThinintInfo
*thinint
;
343 trace_virtio_ccw_interpret_ccw(sch
->cssid
, sch
->ssid
, sch
->schid
,
345 check_len
= !((ccw
.flags
& CCW_FLAG_SLI
) && !(ccw
.flags
& CCW_FLAG_DC
));
347 /* Look at the command. */
348 switch (ccw
.cmd_code
) {
350 ret
= virtio_ccw_handle_set_vq(sch
, ccw
, check_len
, dev
->revision
< 1);
352 case CCW_CMD_VDEV_RESET
:
353 virtio_ccw_reset_virtio(dev
, vdev
);
356 case CCW_CMD_READ_FEAT
:
358 if (ccw
.count
!= sizeof(features
)) {
362 } else if (ccw
.count
< sizeof(features
)) {
363 /* Can't execute command. */
370 features
.index
= address_space_ldub(&address_space_memory
,
372 + sizeof(features
.features
),
373 MEMTXATTRS_UNSPECIFIED
,
375 if (features
.index
== 0) {
376 if (dev
->revision
>= 1) {
377 /* Don't offer legacy features for modern devices. */
378 features
.features
= (uint32_t)
379 (vdev
->host_features
& ~VIRTIO_LEGACY_FEATURES
);
381 features
.features
= (uint32_t)vdev
->host_features
;
383 } else if ((features
.index
== 1) && (dev
->revision
>= 1)) {
385 * Only offer feature bits beyond 31 if the guest has
386 * negotiated at least revision 1.
388 features
.features
= (uint32_t)(vdev
->host_features
>> 32);
390 /* Return zeroes if the guest supports more feature bits. */
391 features
.features
= 0;
393 address_space_stl_le(&address_space_memory
, ccw
.cda
,
394 features
.features
, MEMTXATTRS_UNSPECIFIED
,
396 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
400 case CCW_CMD_WRITE_FEAT
:
402 if (ccw
.count
!= sizeof(features
)) {
406 } else if (ccw
.count
< sizeof(features
)) {
407 /* Can't execute command. */
414 features
.index
= address_space_ldub(&address_space_memory
,
416 + sizeof(features
.features
),
417 MEMTXATTRS_UNSPECIFIED
,
419 features
.features
= address_space_ldl_le(&address_space_memory
,
421 MEMTXATTRS_UNSPECIFIED
,
423 if (features
.index
== 0) {
424 virtio_set_features(vdev
,
425 (vdev
->guest_features
& 0xffffffff00000000ULL
) |
427 } else if ((features
.index
== 1) && (dev
->revision
>= 1)) {
429 * If the guest did not negotiate at least revision 1,
430 * we did not offer it any feature bits beyond 31. Such a
431 * guest passing us any bit here is therefore buggy.
433 virtio_set_features(vdev
,
434 (vdev
->guest_features
& 0x00000000ffffffffULL
) |
435 ((uint64_t)features
.features
<< 32));
438 * If the guest supports more feature bits, assert that it
439 * passes us zeroes for those we don't support.
441 if (features
.features
) {
442 fprintf(stderr
, "Guest bug: features[%i]=%x (expected 0)\n",
443 features
.index
, features
.features
);
444 /* XXX: do a unit check here? */
447 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
451 case CCW_CMD_READ_CONF
:
453 if (ccw
.count
> vdev
->config_len
) {
458 len
= MIN(ccw
.count
, vdev
->config_len
);
462 virtio_bus_get_vdev_config(&dev
->bus
, vdev
->config
);
463 /* XXX config space endianness */
464 cpu_physical_memory_write(ccw
.cda
, vdev
->config
, len
);
465 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
469 case CCW_CMD_WRITE_CONF
:
471 if (ccw
.count
> vdev
->config_len
) {
476 len
= MIN(ccw
.count
, vdev
->config_len
);
481 config
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
486 /* XXX config space endianness */
487 memcpy(vdev
->config
, config
, len
);
488 cpu_physical_memory_unmap(config
, hw_len
, 0, hw_len
);
489 virtio_bus_set_vdev_config(&dev
->bus
, vdev
->config
);
490 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
495 case CCW_CMD_WRITE_STATUS
:
497 if (ccw
.count
!= sizeof(status
)) {
501 } else if (ccw
.count
< sizeof(status
)) {
502 /* Can't execute command. */
509 status
= address_space_ldub(&address_space_memory
, ccw
.cda
,
510 MEMTXATTRS_UNSPECIFIED
, NULL
);
511 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
512 virtio_ccw_stop_ioeventfd(dev
);
514 if (virtio_set_status(vdev
, status
) == 0) {
515 if (vdev
->status
== 0) {
516 virtio_ccw_reset_virtio(dev
, vdev
);
518 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
519 virtio_ccw_start_ioeventfd(dev
);
521 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(status
);
524 /* Trigger a command reject. */
529 case CCW_CMD_SET_IND
:
531 if (ccw
.count
!= sizeof(indicators
)) {
535 } else if (ccw
.count
< sizeof(indicators
)) {
536 /* Can't execute command. */
540 if (sch
->thinint_active
) {
541 /* Trigger a command reject. */
548 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
549 MEMTXATTRS_UNSPECIFIED
, NULL
);
550 dev
->indicators
= get_indicator(indicators
, sizeof(uint64_t));
551 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
555 case CCW_CMD_SET_CONF_IND
:
557 if (ccw
.count
!= sizeof(indicators
)) {
561 } else if (ccw
.count
< sizeof(indicators
)) {
562 /* Can't execute command. */
569 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
570 MEMTXATTRS_UNSPECIFIED
, NULL
);
571 dev
->indicators2
= get_indicator(indicators
, sizeof(uint64_t));
572 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
576 case CCW_CMD_READ_VQ_CONF
:
578 if (ccw
.count
!= sizeof(vq_config
)) {
582 } else if (ccw
.count
< sizeof(vq_config
)) {
583 /* Can't execute command. */
590 vq_config
.index
= address_space_lduw_be(&address_space_memory
,
592 MEMTXATTRS_UNSPECIFIED
,
594 if (vq_config
.index
>= VIRTIO_CCW_QUEUE_MAX
) {
598 vq_config
.num_max
= virtio_queue_get_num(vdev
,
600 address_space_stw_be(&address_space_memory
,
601 ccw
.cda
+ sizeof(vq_config
.index
),
603 MEMTXATTRS_UNSPECIFIED
,
605 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(vq_config
);
609 case CCW_CMD_SET_IND_ADAPTER
:
611 if (ccw
.count
!= sizeof(*thinint
)) {
615 } else if (ccw
.count
< sizeof(*thinint
)) {
616 /* Can't execute command. */
620 len
= sizeof(*thinint
);
624 } else if (dev
->indicators
&& !sch
->thinint_active
) {
625 /* Trigger a command reject. */
628 thinint
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
632 uint64_t ind_bit
= ldq_be_p(&thinint
->ind_bit
);
635 dev
->summary_indicator
=
636 get_indicator(ldq_be_p(&thinint
->summary_indicator
),
639 get_indicator(ldq_be_p(&thinint
->device_indicator
),
641 dev
->thinint_isc
= thinint
->isc
;
642 dev
->routes
.adapter
.ind_offset
= ind_bit
;
643 dev
->routes
.adapter
.summary_offset
= 7;
644 cpu_physical_memory_unmap(thinint
, hw_len
, 0, hw_len
);
645 ret
= css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
646 dev
->thinint_isc
, true, false,
647 &dev
->routes
.adapter
.adapter_id
);
649 sch
->thinint_active
= ((dev
->indicators
!= NULL
) &&
650 (dev
->summary_indicator
!= NULL
));
651 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
656 case CCW_CMD_SET_VIRTIO_REV
:
657 len
= sizeof(revinfo
);
658 if (ccw
.count
< len
) {
667 address_space_lduw_be(&address_space_memory
, ccw
.cda
,
668 MEMTXATTRS_UNSPECIFIED
, NULL
);
670 address_space_lduw_be(&address_space_memory
,
671 ccw
.cda
+ sizeof(revinfo
.revision
),
672 MEMTXATTRS_UNSPECIFIED
, NULL
);
673 if (ccw
.count
< len
+ revinfo
.length
||
674 (check_len
&& ccw
.count
> len
+ revinfo
.length
)) {
679 * Once we start to support revisions with additional data, we'll
680 * need to fetch it here. Nothing to do for now, though.
682 if (dev
->revision
>= 0 ||
683 revinfo
.revision
> virtio_ccw_rev_max(dev
)) {
688 dev
->revision
= revinfo
.revision
;
697 static void virtio_sch_disable_cb(SubchDev
*sch
)
699 VirtioCcwDevice
*dev
= sch
->driver_data
;
704 static void virtio_ccw_device_realize(VirtioCcwDevice
*dev
, Error
**errp
)
710 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
712 sch
= g_malloc0(sizeof(SubchDev
));
714 sch
->driver_data
= dev
;
717 dev
->indicators
= NULL
;
719 /* Initialize subchannel structure. */
720 sch
->channel_prog
= 0x0;
721 sch
->last_cmd_valid
= false;
722 sch
->thinint_active
= false;
724 * Use a device number if provided. Otherwise, fall back to subchannel
727 if (dev
->bus_id
.valid
) {
728 /* Enforce use of virtual cssid. */
729 if (dev
->bus_id
.cssid
!= VIRTUAL_CSSID
) {
730 error_setg(errp
, "cssid %x not valid for virtio devices",
734 if (css_devno_used(dev
->bus_id
.cssid
, dev
->bus_id
.ssid
,
735 dev
->bus_id
.devid
)) {
736 error_setg(errp
, "Device %x.%x.%04x already exists",
737 dev
->bus_id
.cssid
, dev
->bus_id
.ssid
,
741 sch
->cssid
= dev
->bus_id
.cssid
;
742 sch
->ssid
= dev
->bus_id
.ssid
;
743 sch
->devno
= dev
->bus_id
.devid
;
745 /* Find the next free id. */
746 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
747 if (!css_find_subch(1, sch
->cssid
, sch
->ssid
, schid
)) {
749 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
,
756 error_setg(errp
, "No free subchannel found for %x.%x.%04x",
757 sch
->cssid
, sch
->ssid
, sch
->devno
);
760 trace_virtio_ccw_new_device(sch
->cssid
, sch
->ssid
, sch
->schid
,
761 sch
->devno
, "user-configured");
763 unsigned int cssid
= VIRTUAL_CSSID
, ssid
, devno
;
765 for (ssid
= 0; ssid
<= MAX_SSID
; ssid
++) {
766 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
767 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
773 * If the devno is already taken, look further in this
776 while (css_devno_used(cssid
, ssid
, devno
)) {
777 if (devno
== MAX_SCHID
) {
779 } else if (devno
== schid
- 1) {
780 error_setg(errp
, "No free devno found");
787 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
797 error_setg(errp
, "Virtual channel subsystem is full!");
800 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
804 /* Build initial schib. */
805 css_sch_build_virtual_schib(sch
, 0, VIRTIO_CCW_CHPID_TYPE
);
807 sch
->ccw_cb
= virtio_ccw_cb
;
808 sch
->disable_cb
= virtio_sch_disable_cb
;
810 /* Build senseid data. */
811 memset(&sch
->id
, 0, sizeof(SenseId
));
812 sch
->id
.reserved
= 0xff;
813 sch
->id
.cu_type
= VIRTIO_CCW_CU_TYPE
;
818 k
->realize(dev
, &err
);
821 error_propagate(errp
, err
);
822 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
833 static int virtio_ccw_exit(VirtioCcwDevice
*dev
)
835 SubchDev
*sch
= dev
->sch
;
838 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
841 if (dev
->indicators
) {
842 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
843 dev
->indicators
= NULL
;
848 static void virtio_ccw_net_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
850 DeviceState
*qdev
= DEVICE(ccw_dev
);
851 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(ccw_dev
);
852 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
854 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
855 object_get_typename(OBJECT(qdev
)));
856 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
857 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
860 static void virtio_ccw_net_instance_init(Object
*obj
)
862 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(obj
);
864 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
866 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
867 "bootindex", &error_abort
);
870 static void virtio_ccw_blk_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
872 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(ccw_dev
);
873 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
875 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
876 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
879 static void virtio_ccw_blk_instance_init(Object
*obj
)
881 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(obj
);
883 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
885 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
887 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
888 "bootindex", &error_abort
);
891 static void virtio_ccw_serial_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
893 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(ccw_dev
);
894 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
895 DeviceState
*proxy
= DEVICE(ccw_dev
);
899 * For command line compatibility, this sets the virtio-serial-device bus
903 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
904 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
908 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
909 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
913 static void virtio_ccw_serial_instance_init(Object
*obj
)
915 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(obj
);
917 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
921 static void virtio_ccw_balloon_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
923 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(ccw_dev
);
924 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
926 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
927 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
930 static void virtio_ccw_balloon_instance_init(Object
*obj
)
932 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(obj
);
934 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
935 TYPE_VIRTIO_BALLOON
);
936 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
937 "guest-stats", &error_abort
);
938 object_property_add_alias(obj
, "guest-stats-polling-interval",
940 "guest-stats-polling-interval", &error_abort
);
943 static void virtio_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
945 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(ccw_dev
);
946 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
947 DeviceState
*qdev
= DEVICE(ccw_dev
);
951 * For command line compatibility, this sets the virtio-scsi-device bus
955 bus_name
= g_strdup_printf("%s.0", qdev
->id
);
956 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
960 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
961 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
964 static void virtio_ccw_scsi_instance_init(Object
*obj
)
966 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(obj
);
968 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
970 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
974 #ifdef CONFIG_VHOST_SCSI
975 static void vhost_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
977 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(ccw_dev
);
978 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
980 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
981 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
984 static void vhost_ccw_scsi_instance_init(Object
*obj
)
986 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(obj
);
988 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
993 static void virtio_ccw_rng_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
995 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(ccw_dev
);
996 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
999 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1000 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1002 error_propagate(errp
, err
);
1006 object_property_set_link(OBJECT(dev
),
1007 OBJECT(dev
->vdev
.conf
.rng
), "rng",
1011 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1012 * be careful and test performance if you change this.
1014 static inline VirtioCcwDevice
*to_virtio_ccw_dev_fast(DeviceState
*d
)
1016 return container_of(d
, VirtioCcwDevice
, parent_obj
);
1019 static uint8_t virtio_set_ind_atomic(SubchDev
*sch
, uint64_t ind_loc
,
1022 uint8_t ind_old
, ind_new
;
1026 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
1028 error_report("%s(%x.%x.%04x): unable to access indicator",
1029 __func__
, sch
->cssid
, sch
->ssid
, sch
->schid
);
1033 ind_old
= *ind_addr
;
1034 ind_new
= ind_old
| to_be_set
;
1035 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
1036 trace_virtio_ccw_set_ind(ind_loc
, ind_old
, ind_new
);
1037 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
1042 static void virtio_ccw_notify(DeviceState
*d
, uint16_t vector
)
1044 VirtioCcwDevice
*dev
= to_virtio_ccw_dev_fast(d
);
1045 SubchDev
*sch
= dev
->sch
;
1046 uint64_t indicators
;
1048 /* queue indicators + secondary indicators */
1049 if (vector
>= VIRTIO_CCW_QUEUE_MAX
+ 64) {
1053 if (vector
< VIRTIO_CCW_QUEUE_MAX
) {
1054 if (!dev
->indicators
) {
1057 if (sch
->thinint_active
) {
1059 * In the adapter interrupt case, indicators points to a
1060 * memory area that may be (way) larger than 64 bit and
1061 * ind_bit indicates the start of the indicators in a big
1064 uint64_t ind_bit
= dev
->routes
.adapter
.ind_offset
;
1066 virtio_set_ind_atomic(sch
, dev
->indicators
->addr
+
1067 (ind_bit
+ vector
) / 8,
1068 0x80 >> ((ind_bit
+ vector
) % 8));
1069 if (!virtio_set_ind_atomic(sch
, dev
->summary_indicator
->addr
,
1071 css_adapter_interrupt(dev
->thinint_isc
);
1074 indicators
= address_space_ldq(&address_space_memory
,
1075 dev
->indicators
->addr
,
1076 MEMTXATTRS_UNSPECIFIED
,
1078 indicators
|= 1ULL << vector
;
1079 address_space_stq(&address_space_memory
, dev
->indicators
->addr
,
1080 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1081 css_conditional_io_interrupt(sch
);
1084 if (!dev
->indicators2
) {
1088 indicators
= address_space_ldq(&address_space_memory
,
1089 dev
->indicators2
->addr
,
1090 MEMTXATTRS_UNSPECIFIED
,
1092 indicators
|= 1ULL << vector
;
1093 address_space_stq(&address_space_memory
, dev
->indicators2
->addr
,
1094 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1095 css_conditional_io_interrupt(sch
);
1099 static void virtio_ccw_reset(DeviceState
*d
)
1101 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1102 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1104 virtio_ccw_reset_virtio(dev
, vdev
);
1105 css_reset_sch(dev
->sch
);
1108 static void virtio_ccw_vmstate_change(DeviceState
*d
, bool running
)
1110 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1113 virtio_ccw_start_ioeventfd(dev
);
1115 virtio_ccw_stop_ioeventfd(dev
);
1119 static bool virtio_ccw_query_guest_notifiers(DeviceState
*d
)
1121 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1123 return !!(dev
->sch
->curr_status
.pmcw
.flags
& PMCW_FLAGS_MASK_ENA
);
1126 static int virtio_ccw_get_mappings(VirtioCcwDevice
*dev
)
1130 if (!dev
->sch
->thinint_active
) {
1134 r
= map_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
1138 r
= map_indicator(&dev
->routes
.adapter
, dev
->indicators
);
1142 dev
->routes
.adapter
.summary_addr
= dev
->summary_indicator
->map
;
1143 dev
->routes
.adapter
.ind_addr
= dev
->indicators
->map
;
1148 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1151 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1153 S390FLICState
*fs
= s390_get_flic();
1154 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1156 ret
= virtio_ccw_get_mappings(dev
);
1160 for (i
= 0; i
< nvqs
; i
++) {
1161 if (!virtio_queue_get_num(vdev
, i
)) {
1165 dev
->routes
.num_routes
= i
;
1166 return fsc
->add_adapter_routes(fs
, &dev
->routes
);
1169 static void virtio_ccw_release_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1171 S390FLICState
*fs
= s390_get_flic();
1172 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1174 fsc
->release_adapter_routes(fs
, &dev
->routes
);
1177 static int virtio_ccw_add_irqfd(VirtioCcwDevice
*dev
, int n
)
1179 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1180 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1181 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1183 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, notifier
, NULL
,
1184 dev
->routes
.gsi
[n
]);
1187 static void virtio_ccw_remove_irqfd(VirtioCcwDevice
*dev
, int n
)
1189 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1190 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1191 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1194 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, notifier
,
1195 dev
->routes
.gsi
[n
]);
1199 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice
*dev
, int n
,
1200 bool assign
, bool with_irqfd
)
1202 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1203 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1204 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1205 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1208 int r
= event_notifier_init(notifier
, 0);
1213 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
1215 r
= virtio_ccw_add_irqfd(dev
, n
);
1217 virtio_queue_set_guest_notifier_fd_handler(vq
, false,
1223 * We do not support individual masking for channel devices, so we
1224 * need to manually trigger any guest masking callbacks here.
1226 if (k
->guest_notifier_mask
) {
1227 k
->guest_notifier_mask(vdev
, n
, false);
1229 /* get lost events and re-inject */
1230 if (k
->guest_notifier_pending
&&
1231 k
->guest_notifier_pending(vdev
, n
)) {
1232 event_notifier_set(notifier
);
1235 if (k
->guest_notifier_mask
) {
1236 k
->guest_notifier_mask(vdev
, n
, true);
1239 virtio_ccw_remove_irqfd(dev
, n
);
1241 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
1242 event_notifier_cleanup(notifier
);
1247 static int virtio_ccw_set_guest_notifiers(DeviceState
*d
, int nvqs
,
1250 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1251 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1252 bool with_irqfd
= dev
->sch
->thinint_active
&& kvm_irqfds_enabled();
1255 if (with_irqfd
&& assigned
) {
1256 /* irq routes need to be set up before assigning irqfds */
1257 r
= virtio_ccw_setup_irqroutes(dev
, nvqs
);
1259 goto irqroute_error
;
1262 for (n
= 0; n
< nvqs
; n
++) {
1263 if (!virtio_queue_get_num(vdev
, n
)) {
1266 r
= virtio_ccw_set_guest_notifier(dev
, n
, assigned
, with_irqfd
);
1271 if (with_irqfd
&& !assigned
) {
1272 /* release irq routes after irqfds have been released */
1273 virtio_ccw_release_irqroutes(dev
, nvqs
);
1279 virtio_ccw_set_guest_notifier(dev
, n
, !assigned
, false);
1282 if (with_irqfd
&& assigned
) {
1283 virtio_ccw_release_irqroutes(dev
, nvqs
);
1288 static void virtio_ccw_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1290 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1291 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1293 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
1296 static int virtio_ccw_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1298 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1299 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1302 qemu_get_be16s(f
, &vector
);
1303 virtio_queue_set_vector(vdev
, n
, vector
);
1308 static void virtio_ccw_save_config(DeviceState
*d
, QEMUFile
*f
)
1310 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1311 SubchDev
*s
= dev
->sch
;
1312 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1314 subch_device_save(s
, f
);
1315 if (dev
->indicators
!= NULL
) {
1316 qemu_put_be32(f
, dev
->indicators
->len
);
1317 qemu_put_be64(f
, dev
->indicators
->addr
);
1319 qemu_put_be32(f
, 0);
1320 qemu_put_be64(f
, 0UL);
1322 if (dev
->indicators2
!= NULL
) {
1323 qemu_put_be32(f
, dev
->indicators2
->len
);
1324 qemu_put_be64(f
, dev
->indicators2
->addr
);
1326 qemu_put_be32(f
, 0);
1327 qemu_put_be64(f
, 0UL);
1329 if (dev
->summary_indicator
!= NULL
) {
1330 qemu_put_be32(f
, dev
->summary_indicator
->len
);
1331 qemu_put_be64(f
, dev
->summary_indicator
->addr
);
1333 qemu_put_be32(f
, 0);
1334 qemu_put_be64(f
, 0UL);
1336 qemu_put_be16(f
, vdev
->config_vector
);
1337 qemu_put_be64(f
, dev
->routes
.adapter
.ind_offset
);
1338 qemu_put_byte(f
, dev
->thinint_isc
);
1339 qemu_put_be32(f
, dev
->revision
);
1342 static int virtio_ccw_load_config(DeviceState
*d
, QEMUFile
*f
)
1344 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1345 SubchDev
*s
= dev
->sch
;
1346 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1349 s
->driver_data
= dev
;
1350 subch_device_load(s
, f
);
1351 len
= qemu_get_be32(f
);
1353 dev
->indicators
= get_indicator(qemu_get_be64(f
), len
);
1356 dev
->indicators
= NULL
;
1358 len
= qemu_get_be32(f
);
1360 dev
->indicators2
= get_indicator(qemu_get_be64(f
), len
);
1363 dev
->indicators2
= NULL
;
1365 len
= qemu_get_be32(f
);
1367 dev
->summary_indicator
= get_indicator(qemu_get_be64(f
), len
);
1370 dev
->summary_indicator
= NULL
;
1372 qemu_get_be16s(f
, &vdev
->config_vector
);
1373 dev
->routes
.adapter
.ind_offset
= qemu_get_be64(f
);
1374 dev
->thinint_isc
= qemu_get_byte(f
);
1375 dev
->revision
= qemu_get_be32(f
);
1376 if (s
->thinint_active
) {
1377 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
1378 dev
->thinint_isc
, true, false,
1379 &dev
->routes
.adapter
.adapter_id
);
1385 /* This is called by virtio-bus just after the device is plugged. */
1386 static void virtio_ccw_device_plugged(DeviceState
*d
, Error
**errp
)
1388 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1389 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1390 SubchDev
*sch
= dev
->sch
;
1391 int n
= virtio_get_num_queues(vdev
);
1393 if (virtio_get_num_queues(vdev
) > VIRTIO_CCW_QUEUE_MAX
) {
1394 error_setg(errp
, "The number of virtqueues %d "
1395 "exceeds ccw limit %d", n
,
1396 VIRTIO_CCW_QUEUE_MAX
);
1400 if (!kvm_eventfds_enabled()) {
1401 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
1404 sch
->id
.cu_model
= virtio_bus_get_vdev_id(&dev
->bus
);
1406 if (dev
->max_rev
>= 1) {
1407 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1410 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
1414 static void virtio_ccw_post_plugged(DeviceState
*d
, Error
**errp
)
1416 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1417 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1419 if (!virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1420 /* A backend didn't support modern virtio. */
1425 static void virtio_ccw_device_unplugged(DeviceState
*d
)
1427 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1429 virtio_ccw_stop_ioeventfd(dev
);
1431 /**************** Virtio-ccw Bus Device Descriptions *******************/
1433 static Property virtio_ccw_net_properties
[] = {
1434 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1435 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1436 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1437 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1438 VIRTIO_CCW_MAX_REV
),
1439 DEFINE_PROP_END_OF_LIST(),
1442 static void virtio_ccw_net_class_init(ObjectClass
*klass
, void *data
)
1444 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1445 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1447 k
->realize
= virtio_ccw_net_realize
;
1448 k
->exit
= virtio_ccw_exit
;
1449 dc
->reset
= virtio_ccw_reset
;
1450 dc
->props
= virtio_ccw_net_properties
;
1451 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1454 static const TypeInfo virtio_ccw_net
= {
1455 .name
= TYPE_VIRTIO_NET_CCW
,
1456 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1457 .instance_size
= sizeof(VirtIONetCcw
),
1458 .instance_init
= virtio_ccw_net_instance_init
,
1459 .class_init
= virtio_ccw_net_class_init
,
1462 static Property virtio_ccw_blk_properties
[] = {
1463 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1464 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1465 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1466 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1467 VIRTIO_CCW_MAX_REV
),
1468 DEFINE_PROP_END_OF_LIST(),
1471 static void virtio_ccw_blk_class_init(ObjectClass
*klass
, void *data
)
1473 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1474 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1476 k
->realize
= virtio_ccw_blk_realize
;
1477 k
->exit
= virtio_ccw_exit
;
1478 dc
->reset
= virtio_ccw_reset
;
1479 dc
->props
= virtio_ccw_blk_properties
;
1480 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1483 static const TypeInfo virtio_ccw_blk
= {
1484 .name
= TYPE_VIRTIO_BLK_CCW
,
1485 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1486 .instance_size
= sizeof(VirtIOBlkCcw
),
1487 .instance_init
= virtio_ccw_blk_instance_init
,
1488 .class_init
= virtio_ccw_blk_class_init
,
1491 static Property virtio_ccw_serial_properties
[] = {
1492 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1493 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1494 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1495 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1496 VIRTIO_CCW_MAX_REV
),
1497 DEFINE_PROP_END_OF_LIST(),
1500 static void virtio_ccw_serial_class_init(ObjectClass
*klass
, void *data
)
1502 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1503 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1505 k
->realize
= virtio_ccw_serial_realize
;
1506 k
->exit
= virtio_ccw_exit
;
1507 dc
->reset
= virtio_ccw_reset
;
1508 dc
->props
= virtio_ccw_serial_properties
;
1509 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1512 static const TypeInfo virtio_ccw_serial
= {
1513 .name
= TYPE_VIRTIO_SERIAL_CCW
,
1514 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1515 .instance_size
= sizeof(VirtioSerialCcw
),
1516 .instance_init
= virtio_ccw_serial_instance_init
,
1517 .class_init
= virtio_ccw_serial_class_init
,
1520 static Property virtio_ccw_balloon_properties
[] = {
1521 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1522 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1523 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1524 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1525 VIRTIO_CCW_MAX_REV
),
1526 DEFINE_PROP_END_OF_LIST(),
1529 static void virtio_ccw_balloon_class_init(ObjectClass
*klass
, void *data
)
1531 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1532 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1534 k
->realize
= virtio_ccw_balloon_realize
;
1535 k
->exit
= virtio_ccw_exit
;
1536 dc
->reset
= virtio_ccw_reset
;
1537 dc
->props
= virtio_ccw_balloon_properties
;
1538 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1541 static const TypeInfo virtio_ccw_balloon
= {
1542 .name
= TYPE_VIRTIO_BALLOON_CCW
,
1543 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1544 .instance_size
= sizeof(VirtIOBalloonCcw
),
1545 .instance_init
= virtio_ccw_balloon_instance_init
,
1546 .class_init
= virtio_ccw_balloon_class_init
,
1549 static Property virtio_ccw_scsi_properties
[] = {
1550 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1551 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1552 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1553 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1554 VIRTIO_CCW_MAX_REV
),
1555 DEFINE_PROP_END_OF_LIST(),
1558 static void virtio_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1560 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1561 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1563 k
->realize
= virtio_ccw_scsi_realize
;
1564 k
->exit
= virtio_ccw_exit
;
1565 dc
->reset
= virtio_ccw_reset
;
1566 dc
->props
= virtio_ccw_scsi_properties
;
1567 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1570 static const TypeInfo virtio_ccw_scsi
= {
1571 .name
= TYPE_VIRTIO_SCSI_CCW
,
1572 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1573 .instance_size
= sizeof(VirtIOSCSICcw
),
1574 .instance_init
= virtio_ccw_scsi_instance_init
,
1575 .class_init
= virtio_ccw_scsi_class_init
,
1578 #ifdef CONFIG_VHOST_SCSI
1579 static Property vhost_ccw_scsi_properties
[] = {
1580 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1581 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1582 VIRTIO_CCW_MAX_REV
),
1583 DEFINE_PROP_END_OF_LIST(),
1586 static void vhost_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1588 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1589 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1591 k
->realize
= vhost_ccw_scsi_realize
;
1592 k
->exit
= virtio_ccw_exit
;
1593 dc
->reset
= virtio_ccw_reset
;
1594 dc
->props
= vhost_ccw_scsi_properties
;
1595 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1598 static const TypeInfo vhost_ccw_scsi
= {
1599 .name
= TYPE_VHOST_SCSI_CCW
,
1600 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1601 .instance_size
= sizeof(VHostSCSICcw
),
1602 .instance_init
= vhost_ccw_scsi_instance_init
,
1603 .class_init
= vhost_ccw_scsi_class_init
,
1607 static void virtio_ccw_rng_instance_init(Object
*obj
)
1609 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(obj
);
1611 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1613 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
),
1614 "rng", &error_abort
);
1617 static Property virtio_ccw_rng_properties
[] = {
1618 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1619 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1620 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1621 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1622 VIRTIO_CCW_MAX_REV
),
1623 DEFINE_PROP_END_OF_LIST(),
1626 static void virtio_ccw_rng_class_init(ObjectClass
*klass
, void *data
)
1628 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1629 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1631 k
->realize
= virtio_ccw_rng_realize
;
1632 k
->exit
= virtio_ccw_exit
;
1633 dc
->reset
= virtio_ccw_reset
;
1634 dc
->props
= virtio_ccw_rng_properties
;
1635 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1638 static const TypeInfo virtio_ccw_rng
= {
1639 .name
= TYPE_VIRTIO_RNG_CCW
,
1640 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1641 .instance_size
= sizeof(VirtIORNGCcw
),
1642 .instance_init
= virtio_ccw_rng_instance_init
,
1643 .class_init
= virtio_ccw_rng_class_init
,
1646 static void virtio_ccw_busdev_realize(DeviceState
*dev
, Error
**errp
)
1648 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1650 virtio_ccw_bus_new(&_dev
->bus
, sizeof(_dev
->bus
), _dev
);
1651 virtio_ccw_device_realize(_dev
, errp
);
1654 static int virtio_ccw_busdev_exit(DeviceState
*dev
)
1656 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1657 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1659 return _info
->exit(_dev
);
1662 static void virtio_ccw_busdev_unplug(HotplugHandler
*hotplug_dev
,
1663 DeviceState
*dev
, Error
**errp
)
1665 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1666 SubchDev
*sch
= _dev
->sch
;
1668 virtio_ccw_stop_ioeventfd(_dev
);
1671 * We should arrive here only for device_del, since we don't support
1672 * direct hot(un)plug of channels, but only through virtio.
1674 assert(sch
!= NULL
);
1675 /* Subchannel is now disabled and no longer valid. */
1676 sch
->curr_status
.pmcw
.flags
&= ~(PMCW_FLAGS_MASK_ENA
|
1677 PMCW_FLAGS_MASK_DNV
);
1679 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
, 1, 0);
1681 object_unparent(OBJECT(dev
));
1684 static void virtio_ccw_device_class_init(ObjectClass
*klass
, void *data
)
1686 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1688 dc
->realize
= virtio_ccw_busdev_realize
;
1689 dc
->exit
= virtio_ccw_busdev_exit
;
1690 dc
->bus_type
= TYPE_VIRTUAL_CSS_BUS
;
1693 static const TypeInfo virtio_ccw_device_info
= {
1694 .name
= TYPE_VIRTIO_CCW_DEVICE
,
1695 .parent
= TYPE_DEVICE
,
1696 .instance_size
= sizeof(VirtioCcwDevice
),
1697 .class_init
= virtio_ccw_device_class_init
,
1698 .class_size
= sizeof(VirtIOCCWDeviceClass
),
1702 /***************** Virtual-css Bus Bridge Device ********************/
1703 /* Only required to have the virtio bus as child in the system bus */
1705 static int virtual_css_bridge_init(SysBusDevice
*dev
)
1711 static void virtual_css_bridge_class_init(ObjectClass
*klass
, void *data
)
1713 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1714 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1715 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1717 k
->init
= virtual_css_bridge_init
;
1718 hc
->unplug
= virtio_ccw_busdev_unplug
;
1719 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1722 static const TypeInfo virtual_css_bridge_info
= {
1723 .name
= "virtual-css-bridge",
1724 .parent
= TYPE_SYS_BUS_DEVICE
,
1725 .instance_size
= sizeof(SysBusDevice
),
1726 .class_init
= virtual_css_bridge_class_init
,
1727 .interfaces
= (InterfaceInfo
[]) {
1728 { TYPE_HOTPLUG_HANDLER
},
1733 /* virtio-ccw-bus */
1735 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1736 VirtioCcwDevice
*dev
)
1738 DeviceState
*qdev
= DEVICE(dev
);
1739 char virtio_bus_name
[] = "virtio-bus";
1741 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_CCW_BUS
,
1742 qdev
, virtio_bus_name
);
1745 static void virtio_ccw_bus_class_init(ObjectClass
*klass
, void *data
)
1747 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1748 BusClass
*bus_class
= BUS_CLASS(klass
);
1750 bus_class
->max_dev
= 1;
1751 k
->notify
= virtio_ccw_notify
;
1752 k
->vmstate_change
= virtio_ccw_vmstate_change
;
1753 k
->query_guest_notifiers
= virtio_ccw_query_guest_notifiers
;
1754 k
->set_guest_notifiers
= virtio_ccw_set_guest_notifiers
;
1755 k
->save_queue
= virtio_ccw_save_queue
;
1756 k
->load_queue
= virtio_ccw_load_queue
;
1757 k
->save_config
= virtio_ccw_save_config
;
1758 k
->load_config
= virtio_ccw_load_config
;
1759 k
->device_plugged
= virtio_ccw_device_plugged
;
1760 k
->post_plugged
= virtio_ccw_post_plugged
;
1761 k
->device_unplugged
= virtio_ccw_device_unplugged
;
1762 k
->ioeventfd_started
= virtio_ccw_ioeventfd_started
;
1763 k
->ioeventfd_set_started
= virtio_ccw_ioeventfd_set_started
;
1764 k
->ioeventfd_disabled
= virtio_ccw_ioeventfd_disabled
;
1765 k
->ioeventfd_set_disabled
= virtio_ccw_ioeventfd_set_disabled
;
1766 k
->ioeventfd_assign
= virtio_ccw_ioeventfd_assign
;
1769 static const TypeInfo virtio_ccw_bus_info
= {
1770 .name
= TYPE_VIRTIO_CCW_BUS
,
1771 .parent
= TYPE_VIRTIO_BUS
,
1772 .instance_size
= sizeof(VirtioCcwBusState
),
1773 .class_init
= virtio_ccw_bus_class_init
,
1776 #ifdef CONFIG_VIRTFS
1777 static Property virtio_ccw_9p_properties
[] = {
1778 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice
, bus_id
),
1779 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1780 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1781 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1782 VIRTIO_CCW_MAX_REV
),
1783 DEFINE_PROP_END_OF_LIST(),
1786 static void virtio_ccw_9p_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1788 V9fsCCWState
*dev
= VIRTIO_9P_CCW(ccw_dev
);
1789 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1791 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1792 object_property_set_bool(OBJECT(vdev
), true, "realized", errp
);
1795 static void virtio_ccw_9p_class_init(ObjectClass
*klass
, void *data
)
1797 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1798 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1800 k
->exit
= virtio_ccw_exit
;
1801 k
->realize
= virtio_ccw_9p_realize
;
1802 dc
->reset
= virtio_ccw_reset
;
1803 dc
->props
= virtio_ccw_9p_properties
;
1804 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1807 static void virtio_ccw_9p_instance_init(Object
*obj
)
1809 V9fsCCWState
*dev
= VIRTIO_9P_CCW(obj
);
1811 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1815 static const TypeInfo virtio_ccw_9p_info
= {
1816 .name
= TYPE_VIRTIO_9P_CCW
,
1817 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1818 .instance_size
= sizeof(V9fsCCWState
),
1819 .instance_init
= virtio_ccw_9p_instance_init
,
1820 .class_init
= virtio_ccw_9p_class_init
,
1824 static void virtio_ccw_register(void)
1826 type_register_static(&virtio_ccw_bus_info
);
1827 type_register_static(&virtual_css_bus_info
);
1828 type_register_static(&virtio_ccw_device_info
);
1829 type_register_static(&virtio_ccw_serial
);
1830 type_register_static(&virtio_ccw_blk
);
1831 type_register_static(&virtio_ccw_net
);
1832 type_register_static(&virtio_ccw_balloon
);
1833 type_register_static(&virtio_ccw_scsi
);
1834 #ifdef CONFIG_VHOST_SCSI
1835 type_register_static(&vhost_ccw_scsi
);
1837 type_register_static(&virtio_ccw_rng
);
1838 type_register_static(&virtual_css_bridge_info
);
1839 #ifdef CONFIG_VIRTFS
1840 type_register_static(&virtio_ccw_9p_info
);
1844 type_init(virtio_ccw_register
)