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
14 #include "sysemu/block-backend.h"
15 #include "sysemu/blockdev.h"
16 #include "sysemu/sysemu.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 "qemu/error-report.h"
24 #include "hw/virtio/virtio-access.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/s390x/adapter.h"
27 #include "hw/s390x/s390_flic.h"
31 #include "virtio-ccw.h"
34 static QTAILQ_HEAD(, IndAddr
) indicator_addresses
=
35 QTAILQ_HEAD_INITIALIZER(indicator_addresses
);
37 static IndAddr
*get_indicator(hwaddr ind_addr
, int len
)
41 QTAILQ_FOREACH(indicator
, &indicator_addresses
, sibling
) {
42 if (indicator
->addr
== ind_addr
) {
47 indicator
= g_new0(IndAddr
, 1);
48 indicator
->addr
= ind_addr
;
50 indicator
->refcnt
= 1;
51 QTAILQ_INSERT_TAIL(&indicator_addresses
, indicator
, sibling
);
55 static int s390_io_adapter_map(AdapterInfo
*adapter
, uint64_t map_addr
,
58 S390FLICState
*fs
= s390_get_flic();
59 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
61 return fsc
->io_adapter_map(fs
, adapter
->adapter_id
, map_addr
, do_map
);
64 static void release_indicator(AdapterInfo
*adapter
, IndAddr
*indicator
)
66 assert(indicator
->refcnt
> 0);
68 if (indicator
->refcnt
> 0) {
71 QTAILQ_REMOVE(&indicator_addresses
, indicator
, sibling
);
73 s390_io_adapter_map(adapter
, indicator
->map
, false);
78 static int map_indicator(AdapterInfo
*adapter
, IndAddr
*indicator
)
83 return 0; /* already mapped is not an error */
85 indicator
->map
= indicator
->addr
;
86 ret
= s390_io_adapter_map(adapter
, indicator
->map
, true);
87 if ((ret
!= 0) && (ret
!= -ENOSYS
)) {
97 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
98 VirtioCcwDevice
*dev
);
100 static void virtual_css_bus_reset(BusState
*qbus
)
102 /* This should actually be modelled via the generic css */
107 static void virtual_css_bus_class_init(ObjectClass
*klass
, void *data
)
109 BusClass
*k
= BUS_CLASS(klass
);
111 k
->reset
= virtual_css_bus_reset
;
114 static const TypeInfo virtual_css_bus_info
= {
115 .name
= TYPE_VIRTUAL_CSS_BUS
,
117 .instance_size
= sizeof(VirtualCssBus
),
118 .class_init
= virtual_css_bus_class_init
,
121 VirtIODevice
*virtio_ccw_get_vdev(SubchDev
*sch
)
123 VirtIODevice
*vdev
= NULL
;
124 VirtioCcwDevice
*dev
= sch
->driver_data
;
127 vdev
= virtio_bus_get_device(&dev
->bus
);
132 static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice
*dev
, int n
,
133 bool assign
, bool set_handler
)
135 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
136 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
137 EventNotifier
*notifier
= virtio_queue_get_host_notifier(vq
);
139 SubchDev
*sch
= dev
->sch
;
140 uint32_t sch_id
= (css_build_subchannel_id(sch
) << 16) | sch
->schid
;
143 r
= event_notifier_init(notifier
, 1);
145 error_report("%s: unable to init event notifier: %d", __func__
, r
);
148 virtio_queue_set_host_notifier_fd_handler(vq
, true, set_handler
);
149 r
= s390_assign_subch_ioeventfd(notifier
, sch_id
, n
, assign
);
151 error_report("%s: unable to assign ioeventfd: %d", __func__
, r
);
152 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
153 event_notifier_cleanup(notifier
);
157 virtio_queue_set_host_notifier_fd_handler(vq
, false, false);
158 s390_assign_subch_ioeventfd(notifier
, sch_id
, n
, assign
);
159 event_notifier_cleanup(notifier
);
164 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice
*dev
)
169 if (!(dev
->flags
& VIRTIO_CCW_FLAG_USE_IOEVENTFD
) ||
170 dev
->ioeventfd_disabled
||
171 dev
->ioeventfd_started
) {
174 vdev
= virtio_bus_get_device(&dev
->bus
);
175 for (n
= 0; n
< VIRTIO_CCW_QUEUE_MAX
; n
++) {
176 if (!virtio_queue_get_num(vdev
, n
)) {
179 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, true, true);
184 dev
->ioeventfd_started
= true;
189 if (!virtio_queue_get_num(vdev
, n
)) {
192 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, false, false);
195 dev
->ioeventfd_started
= false;
196 /* Disable ioeventfd for this device. */
197 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
198 error_report("%s: failed. Fallback to userspace (slower).", __func__
);
201 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice
*dev
)
206 if (!dev
->ioeventfd_started
) {
209 vdev
= virtio_bus_get_device(&dev
->bus
);
210 for (n
= 0; n
< VIRTIO_CCW_QUEUE_MAX
; n
++) {
211 if (!virtio_queue_get_num(vdev
, n
)) {
214 r
= virtio_ccw_set_guest2host_notifier(dev
, n
, false, false);
217 dev
->ioeventfd_started
= false;
220 VirtualCssBus
*virtual_css_bus_init(void)
226 /* Create bridge device */
227 dev
= qdev_create(NULL
, "virtual-css-bridge");
228 qdev_init_nofail(dev
);
230 /* Create bus on bridge device */
231 bus
= qbus_create(TYPE_VIRTUAL_CSS_BUS
, dev
, "virtual-css");
232 cbus
= VIRTUAL_CSS_BUS(bus
);
234 /* Enable hotplugging */
235 qbus_set_hotplug_handler(bus
, dev
, &error_abort
);
240 /* Communication blocks used by several channel commands. */
241 typedef struct VqInfoBlockLegacy
{
246 } QEMU_PACKED VqInfoBlockLegacy
;
248 typedef struct VqInfoBlock
{
255 } QEMU_PACKED VqInfoBlock
;
257 typedef struct VqConfigBlock
{
260 } QEMU_PACKED VqConfigBlock
;
262 typedef struct VirtioFeatDesc
{
265 } QEMU_PACKED VirtioFeatDesc
;
267 typedef struct VirtioThinintInfo
{
268 hwaddr summary_indicator
;
269 hwaddr device_indicator
;
272 } QEMU_PACKED VirtioThinintInfo
;
274 typedef struct VirtioRevInfo
{
278 } QEMU_PACKED VirtioRevInfo
;
280 /* Specify where the virtqueues for the subchannel are in guest memory. */
281 static int virtio_ccw_set_vqs(SubchDev
*sch
, VqInfoBlock
*info
,
282 VqInfoBlockLegacy
*linfo
)
284 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
285 uint16_t index
= info
? info
->index
: linfo
->index
;
286 uint16_t num
= info
? info
->num
: linfo
->num
;
287 uint64_t desc
= info
? info
->desc
: linfo
->queue
;
289 if (index
>= VIRTIO_CCW_QUEUE_MAX
) {
293 /* Current code in virtio.c relies on 4K alignment. */
294 if (linfo
&& desc
&& (linfo
->align
!= 4096)) {
303 virtio_queue_set_rings(vdev
, index
, desc
, info
->avail
, info
->used
);
305 virtio_queue_set_addr(vdev
, index
, desc
);
308 virtio_queue_set_vector(vdev
, index
, VIRTIO_NO_VECTOR
);
310 /* Fail if we don't have a big enough queue. */
311 /* TODO: Add interface to handle vring.num changing */
312 if (virtio_queue_get_num(vdev
, index
) > num
) {
315 virtio_queue_set_vector(vdev
, index
, index
);
317 /* tell notify handler in case of config change */
318 vdev
->config_vector
= VIRTIO_CCW_QUEUE_MAX
;
322 static void virtio_ccw_reset_virtio(VirtioCcwDevice
*dev
, VirtIODevice
*vdev
)
324 virtio_ccw_stop_ioeventfd(dev
);
326 if (dev
->indicators
) {
327 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
328 dev
->indicators
= NULL
;
330 if (dev
->indicators2
) {
331 release_indicator(&dev
->routes
.adapter
, dev
->indicators2
);
332 dev
->indicators2
= NULL
;
334 if (dev
->summary_indicator
) {
335 release_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
336 dev
->summary_indicator
= NULL
;
338 dev
->sch
->thinint_active
= false;
341 static int virtio_ccw_handle_set_vq(SubchDev
*sch
, CCW1 ccw
, bool check_len
,
346 VqInfoBlockLegacy linfo
;
347 size_t info_len
= is_legacy
? sizeof(linfo
) : sizeof(info
);
350 if (ccw
.count
!= info_len
) {
353 } else if (ccw
.count
< info_len
) {
354 /* Can't execute command. */
361 linfo
.queue
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
362 MEMTXATTRS_UNSPECIFIED
, NULL
);
363 linfo
.align
= address_space_ldl_be(&address_space_memory
,
364 ccw
.cda
+ sizeof(linfo
.queue
),
365 MEMTXATTRS_UNSPECIFIED
,
367 linfo
.index
= address_space_lduw_be(&address_space_memory
,
368 ccw
.cda
+ sizeof(linfo
.queue
)
369 + sizeof(linfo
.align
),
370 MEMTXATTRS_UNSPECIFIED
,
372 linfo
.num
= address_space_lduw_be(&address_space_memory
,
373 ccw
.cda
+ sizeof(linfo
.queue
)
374 + sizeof(linfo
.align
)
375 + sizeof(linfo
.index
),
376 MEMTXATTRS_UNSPECIFIED
,
378 ret
= virtio_ccw_set_vqs(sch
, NULL
, &linfo
);
380 info
.desc
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
381 MEMTXATTRS_UNSPECIFIED
, NULL
);
382 info
.index
= address_space_lduw_be(&address_space_memory
,
383 ccw
.cda
+ sizeof(info
.desc
)
385 MEMTXATTRS_UNSPECIFIED
, NULL
);
386 info
.num
= address_space_lduw_be(&address_space_memory
,
387 ccw
.cda
+ sizeof(info
.desc
)
389 + sizeof(info
.index
),
390 MEMTXATTRS_UNSPECIFIED
, NULL
);
391 info
.avail
= address_space_ldq_be(&address_space_memory
,
392 ccw
.cda
+ sizeof(info
.desc
)
396 MEMTXATTRS_UNSPECIFIED
, NULL
);
397 info
.used
= address_space_ldq_be(&address_space_memory
,
398 ccw
.cda
+ sizeof(info
.desc
)
402 + sizeof(info
.avail
),
403 MEMTXATTRS_UNSPECIFIED
, NULL
);
404 ret
= virtio_ccw_set_vqs(sch
, &info
, NULL
);
406 sch
->curr_status
.scsw
.count
= 0;
410 static int virtio_ccw_cb(SubchDev
*sch
, CCW1 ccw
)
413 VirtioRevInfo revinfo
;
415 VirtioFeatDesc features
;
418 VqConfigBlock vq_config
;
419 VirtioCcwDevice
*dev
= sch
->driver_data
;
420 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
424 VirtioThinintInfo
*thinint
;
430 trace_virtio_ccw_interpret_ccw(sch
->cssid
, sch
->ssid
, sch
->schid
,
432 check_len
= !((ccw
.flags
& CCW_FLAG_SLI
) && !(ccw
.flags
& CCW_FLAG_DC
));
434 /* Look at the command. */
435 switch (ccw
.cmd_code
) {
437 ret
= virtio_ccw_handle_set_vq(sch
, ccw
, check_len
, dev
->revision
< 1);
439 case CCW_CMD_VDEV_RESET
:
440 virtio_ccw_reset_virtio(dev
, vdev
);
443 case CCW_CMD_READ_FEAT
:
445 if (ccw
.count
!= sizeof(features
)) {
449 } else if (ccw
.count
< sizeof(features
)) {
450 /* Can't execute command. */
457 features
.index
= address_space_ldub(&address_space_memory
,
459 + sizeof(features
.features
),
460 MEMTXATTRS_UNSPECIFIED
,
462 if (features
.index
== 0) {
463 features
.features
= (uint32_t)vdev
->host_features
;
464 } else if (features
.index
== 1) {
465 features
.features
= (uint32_t)(vdev
->host_features
>> 32);
467 * Don't offer version 1 to the guest if it did not
468 * negotiate at least revision 1.
470 if (dev
->revision
<= 0) {
471 features
.features
&= ~(1 << (VIRTIO_F_VERSION_1
- 32));
474 /* Return zeroes if the guest supports more feature bits. */
475 features
.features
= 0;
477 address_space_stl_le(&address_space_memory
, ccw
.cda
,
478 features
.features
, MEMTXATTRS_UNSPECIFIED
,
480 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
484 case CCW_CMD_WRITE_FEAT
:
486 if (ccw
.count
!= sizeof(features
)) {
490 } else if (ccw
.count
< sizeof(features
)) {
491 /* Can't execute command. */
498 features
.index
= address_space_ldub(&address_space_memory
,
500 + sizeof(features
.features
),
501 MEMTXATTRS_UNSPECIFIED
,
503 features
.features
= address_space_ldl_le(&address_space_memory
,
505 MEMTXATTRS_UNSPECIFIED
,
507 if (features
.index
== 0) {
508 virtio_set_features(vdev
,
509 (vdev
->guest_features
& 0xffffffff00000000ULL
) |
511 } else if (features
.index
== 1) {
513 * The guest should not set version 1 if it didn't
514 * negotiate a revision >= 1.
516 if (dev
->revision
<= 0) {
517 features
.features
&= ~(1 << (VIRTIO_F_VERSION_1
- 32));
519 virtio_set_features(vdev
,
520 (vdev
->guest_features
& 0x00000000ffffffffULL
) |
521 ((uint64_t)features
.features
<< 32));
524 * If the guest supports more feature bits, assert that it
525 * passes us zeroes for those we don't support.
527 if (features
.features
) {
528 fprintf(stderr
, "Guest bug: features[%i]=%x (expected 0)\n",
529 features
.index
, features
.features
);
530 /* XXX: do a unit check here? */
533 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
537 case CCW_CMD_READ_CONF
:
539 if (ccw
.count
> vdev
->config_len
) {
544 len
= MIN(ccw
.count
, vdev
->config_len
);
548 virtio_bus_get_vdev_config(&dev
->bus
, vdev
->config
);
549 /* XXX config space endianness */
550 cpu_physical_memory_write(ccw
.cda
, vdev
->config
, len
);
551 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
555 case CCW_CMD_WRITE_CONF
:
557 if (ccw
.count
> vdev
->config_len
) {
562 len
= MIN(ccw
.count
, vdev
->config_len
);
567 config
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
572 /* XXX config space endianness */
573 memcpy(vdev
->config
, config
, len
);
574 cpu_physical_memory_unmap(config
, hw_len
, 0, hw_len
);
575 virtio_bus_set_vdev_config(&dev
->bus
, vdev
->config
);
576 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
581 case CCW_CMD_WRITE_STATUS
:
583 if (ccw
.count
!= sizeof(status
)) {
587 } else if (ccw
.count
< sizeof(status
)) {
588 /* Can't execute command. */
595 status
= address_space_ldub(&address_space_memory
, ccw
.cda
,
596 MEMTXATTRS_UNSPECIFIED
, NULL
);
597 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
598 virtio_ccw_stop_ioeventfd(dev
);
600 if (virtio_set_status(vdev
, status
) == 0) {
601 if (vdev
->status
== 0) {
602 virtio_ccw_reset_virtio(dev
, vdev
);
604 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
605 virtio_ccw_start_ioeventfd(dev
);
607 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(status
);
610 /* Trigger a command reject. */
615 case CCW_CMD_SET_IND
:
617 if (ccw
.count
!= sizeof(indicators
)) {
621 } else if (ccw
.count
< sizeof(indicators
)) {
622 /* Can't execute command. */
626 if (sch
->thinint_active
) {
627 /* Trigger a command reject. */
634 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
635 MEMTXATTRS_UNSPECIFIED
, NULL
);
636 dev
->indicators
= get_indicator(indicators
, sizeof(uint64_t));
637 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
641 case CCW_CMD_SET_CONF_IND
:
643 if (ccw
.count
!= sizeof(indicators
)) {
647 } else if (ccw
.count
< sizeof(indicators
)) {
648 /* Can't execute command. */
655 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
656 MEMTXATTRS_UNSPECIFIED
, NULL
);
657 dev
->indicators2
= get_indicator(indicators
, sizeof(uint64_t));
658 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
662 case CCW_CMD_READ_VQ_CONF
:
664 if (ccw
.count
!= sizeof(vq_config
)) {
668 } else if (ccw
.count
< sizeof(vq_config
)) {
669 /* Can't execute command. */
676 vq_config
.index
= address_space_lduw_be(&address_space_memory
,
678 MEMTXATTRS_UNSPECIFIED
,
680 if (vq_config
.index
>= VIRTIO_CCW_QUEUE_MAX
) {
684 vq_config
.num_max
= virtio_queue_get_num(vdev
,
686 address_space_stw_be(&address_space_memory
,
687 ccw
.cda
+ sizeof(vq_config
.index
),
689 MEMTXATTRS_UNSPECIFIED
,
691 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(vq_config
);
695 case CCW_CMD_SET_IND_ADAPTER
:
697 if (ccw
.count
!= sizeof(*thinint
)) {
701 } else if (ccw
.count
< sizeof(*thinint
)) {
702 /* Can't execute command. */
706 len
= sizeof(*thinint
);
710 } else if (dev
->indicators
&& !sch
->thinint_active
) {
711 /* Trigger a command reject. */
714 thinint
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
718 uint64_t ind_bit
= ldq_be_p(&thinint
->ind_bit
);
721 dev
->summary_indicator
=
722 get_indicator(ldq_be_p(&thinint
->summary_indicator
),
725 get_indicator(ldq_be_p(&thinint
->device_indicator
),
727 dev
->thinint_isc
= thinint
->isc
;
728 dev
->routes
.adapter
.ind_offset
= ind_bit
;
729 dev
->routes
.adapter
.summary_offset
= 7;
730 cpu_physical_memory_unmap(thinint
, hw_len
, 0, hw_len
);
731 ret
= css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
732 dev
->thinint_isc
, true, false,
733 &dev
->routes
.adapter
.adapter_id
);
735 sch
->thinint_active
= ((dev
->indicators
!= NULL
) &&
736 (dev
->summary_indicator
!= NULL
));
737 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
742 case CCW_CMD_SET_VIRTIO_REV
:
743 len
= sizeof(revinfo
);
744 if (ccw
.count
< len
) {
753 address_space_lduw_be(&address_space_memory
, ccw
.cda
,
754 MEMTXATTRS_UNSPECIFIED
, NULL
);
756 address_space_lduw_be(&address_space_memory
,
757 ccw
.cda
+ sizeof(revinfo
.revision
),
758 MEMTXATTRS_UNSPECIFIED
, NULL
);
759 if (ccw
.count
< len
+ revinfo
.length
||
760 (check_len
&& ccw
.count
> len
+ revinfo
.length
)) {
765 * Once we start to support revisions with additional data, we'll
766 * need to fetch it here. Nothing to do for now, though.
768 if (dev
->revision
>= 0 ||
769 revinfo
.revision
> virtio_ccw_rev_max(vdev
)) {
774 dev
->revision
= revinfo
.revision
;
783 static void virtio_sch_disable_cb(SubchDev
*sch
)
785 VirtioCcwDevice
*dev
= sch
->driver_data
;
790 static void virtio_ccw_device_realize(VirtioCcwDevice
*dev
, Error
**errp
)
792 unsigned int cssid
= 0;
793 unsigned int ssid
= 0;
796 bool have_devno
= false;
801 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
803 sch
= g_malloc0(sizeof(SubchDev
));
805 sch
->driver_data
= dev
;
808 dev
->indicators
= NULL
;
810 /* Initialize subchannel structure. */
811 sch
->channel_prog
= 0x0;
812 sch
->last_cmd_valid
= false;
813 sch
->thinint_active
= false;
815 * Use a device number if provided. Otherwise, fall back to subchannel
819 num
= sscanf(dev
->bus_id
, "%x.%x.%04x", &cssid
, &ssid
, &devno
);
821 if ((cssid
> MAX_CSSID
) || (ssid
> MAX_SSID
)) {
822 error_setg(errp
, "Invalid cssid or ssid: cssid %x, ssid %x",
826 /* Enforce use of virtual cssid. */
827 if (cssid
!= VIRTUAL_CSSID
) {
828 error_setg(errp
, "cssid %x not valid for virtio devices",
832 if (css_devno_used(cssid
, ssid
, devno
)) {
833 error_setg(errp
, "Device %x.%x.%04x already exists",
842 error_setg(errp
, "Malformed devno parameter '%s'", dev
->bus_id
);
847 /* Find the next free id. */
849 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
850 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
852 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
858 error_setg(errp
, "No free subchannel found for %x.%x.%04x",
862 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
865 cssid
= VIRTUAL_CSSID
;
866 for (ssid
= 0; ssid
<= MAX_SSID
; ssid
++) {
867 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
868 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
874 * If the devno is already taken, look further in this
877 while (css_devno_used(cssid
, ssid
, devno
)) {
878 if (devno
== MAX_SCHID
) {
880 } else if (devno
== schid
- 1) {
881 error_setg(errp
, "No free devno found");
888 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
898 error_setg(errp
, "Virtual channel subsystem is full!");
901 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
905 /* Build initial schib. */
906 css_sch_build_virtual_schib(sch
, 0, VIRTIO_CCW_CHPID_TYPE
);
908 sch
->ccw_cb
= virtio_ccw_cb
;
909 sch
->disable_cb
= virtio_sch_disable_cb
;
911 /* Build senseid data. */
912 memset(&sch
->id
, 0, sizeof(SenseId
));
913 sch
->id
.reserved
= 0xff;
914 sch
->id
.cu_type
= VIRTIO_CCW_CU_TYPE
;
919 k
->realize(dev
, &err
);
922 error_propagate(errp
, err
);
923 css_subch_assign(cssid
, ssid
, schid
, devno
, NULL
);
934 static int virtio_ccw_exit(VirtioCcwDevice
*dev
)
936 SubchDev
*sch
= dev
->sch
;
939 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
942 if (dev
->indicators
) {
943 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
944 dev
->indicators
= NULL
;
949 static void virtio_ccw_net_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
951 DeviceState
*qdev
= DEVICE(ccw_dev
);
952 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(ccw_dev
);
953 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
956 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
957 object_get_typename(OBJECT(qdev
)));
958 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
959 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
961 error_propagate(errp
, err
);
965 static void virtio_ccw_net_instance_init(Object
*obj
)
967 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(obj
);
969 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
971 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
972 "bootindex", &error_abort
);
975 static void virtio_ccw_blk_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
977 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(ccw_dev
);
978 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
981 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
982 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
984 error_propagate(errp
, err
);
988 static void virtio_ccw_blk_instance_init(Object
*obj
)
990 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(obj
);
992 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
994 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
996 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
997 "bootindex", &error_abort
);
1000 static void virtio_ccw_serial_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1002 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(ccw_dev
);
1003 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1004 DeviceState
*proxy
= DEVICE(ccw_dev
);
1009 * For command line compatibility, this sets the virtio-serial-device bus
1013 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1014 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1018 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1019 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1021 error_propagate(errp
, err
);
1026 static void virtio_ccw_serial_instance_init(Object
*obj
)
1028 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(obj
);
1030 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1031 TYPE_VIRTIO_SERIAL
);
1034 static void virtio_ccw_balloon_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1036 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(ccw_dev
);
1037 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1040 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1041 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1043 error_propagate(errp
, err
);
1047 static void virtio_ccw_balloon_instance_init(Object
*obj
)
1049 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(obj
);
1051 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1052 TYPE_VIRTIO_BALLOON
);
1053 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1054 "guest-stats", &error_abort
);
1055 object_property_add_alias(obj
, "guest-stats-polling-interval",
1057 "guest-stats-polling-interval", &error_abort
);
1060 static void virtio_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1062 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(ccw_dev
);
1063 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1064 DeviceState
*qdev
= DEVICE(ccw_dev
);
1069 * For command line compatibility, this sets the virtio-scsi-device bus
1073 bus_name
= g_strdup_printf("%s.0", qdev
->id
);
1074 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1078 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1079 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1081 error_propagate(errp
, err
);
1085 static void virtio_ccw_scsi_instance_init(Object
*obj
)
1087 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(obj
);
1089 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1091 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1095 #ifdef CONFIG_VHOST_SCSI
1096 static void vhost_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1098 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(ccw_dev
);
1099 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1102 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1103 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1105 error_propagate(errp
, err
);
1109 static void vhost_ccw_scsi_instance_init(Object
*obj
)
1111 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(obj
);
1113 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1118 static void virtio_ccw_rng_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1120 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(ccw_dev
);
1121 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1124 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1125 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1127 error_propagate(errp
, err
);
1131 object_property_set_link(OBJECT(dev
),
1132 OBJECT(dev
->vdev
.conf
.rng
), "rng",
1136 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1137 * be careful and test performance if you change this.
1139 static inline VirtioCcwDevice
*to_virtio_ccw_dev_fast(DeviceState
*d
)
1141 return container_of(d
, VirtioCcwDevice
, parent_obj
);
1144 static uint8_t virtio_set_ind_atomic(SubchDev
*sch
, uint64_t ind_loc
,
1147 uint8_t ind_old
, ind_new
;
1151 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
1153 error_report("%s(%x.%x.%04x): unable to access indicator",
1154 __func__
, sch
->cssid
, sch
->ssid
, sch
->schid
);
1158 ind_old
= *ind_addr
;
1159 ind_new
= ind_old
| to_be_set
;
1160 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
1161 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
1166 static void virtio_ccw_notify(DeviceState
*d
, uint16_t vector
)
1168 VirtioCcwDevice
*dev
= to_virtio_ccw_dev_fast(d
);
1169 SubchDev
*sch
= dev
->sch
;
1170 uint64_t indicators
;
1172 if (vector
>= 128) {
1176 if (vector
< VIRTIO_CCW_QUEUE_MAX
) {
1177 if (!dev
->indicators
) {
1180 if (sch
->thinint_active
) {
1182 * In the adapter interrupt case, indicators points to a
1183 * memory area that may be (way) larger than 64 bit and
1184 * ind_bit indicates the start of the indicators in a big
1187 uint64_t ind_bit
= dev
->routes
.adapter
.ind_offset
;
1189 virtio_set_ind_atomic(sch
, dev
->indicators
->addr
+
1190 (ind_bit
+ vector
) / 8,
1191 0x80 >> ((ind_bit
+ vector
) % 8));
1192 if (!virtio_set_ind_atomic(sch
, dev
->summary_indicator
->addr
,
1194 css_adapter_interrupt(dev
->thinint_isc
);
1197 indicators
= address_space_ldq(&address_space_memory
,
1198 dev
->indicators
->addr
,
1199 MEMTXATTRS_UNSPECIFIED
,
1201 indicators
|= 1ULL << vector
;
1202 address_space_stq(&address_space_memory
, dev
->indicators
->addr
,
1203 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1204 css_conditional_io_interrupt(sch
);
1207 if (!dev
->indicators2
) {
1211 indicators
= address_space_ldq(&address_space_memory
,
1212 dev
->indicators2
->addr
,
1213 MEMTXATTRS_UNSPECIFIED
,
1215 indicators
|= 1ULL << vector
;
1216 address_space_stq(&address_space_memory
, dev
->indicators2
->addr
,
1217 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1218 css_conditional_io_interrupt(sch
);
1222 static void virtio_ccw_reset(DeviceState
*d
)
1224 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1225 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1227 virtio_ccw_reset_virtio(dev
, vdev
);
1228 css_reset_sch(dev
->sch
);
1231 static void virtio_ccw_vmstate_change(DeviceState
*d
, bool running
)
1233 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1236 virtio_ccw_start_ioeventfd(dev
);
1238 virtio_ccw_stop_ioeventfd(dev
);
1242 static bool virtio_ccw_query_guest_notifiers(DeviceState
*d
)
1244 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1246 return !!(dev
->sch
->curr_status
.pmcw
.flags
& PMCW_FLAGS_MASK_ENA
);
1249 static int virtio_ccw_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
1251 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1253 /* Stop using the generic ioeventfd, we are doing eventfd handling
1254 * ourselves below */
1255 dev
->ioeventfd_disabled
= assign
;
1257 virtio_ccw_stop_ioeventfd(dev
);
1259 return virtio_ccw_set_guest2host_notifier(dev
, n
, assign
, false);
1262 static int virtio_ccw_get_mappings(VirtioCcwDevice
*dev
)
1266 if (!dev
->sch
->thinint_active
) {
1270 r
= map_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
1274 r
= map_indicator(&dev
->routes
.adapter
, dev
->indicators
);
1278 dev
->routes
.adapter
.summary_addr
= dev
->summary_indicator
->map
;
1279 dev
->routes
.adapter
.ind_addr
= dev
->indicators
->map
;
1284 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1287 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1289 S390FLICState
*fs
= s390_get_flic();
1290 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1292 ret
= virtio_ccw_get_mappings(dev
);
1296 for (i
= 0; i
< nvqs
; i
++) {
1297 if (!virtio_queue_get_num(vdev
, i
)) {
1301 dev
->routes
.num_routes
= i
;
1302 return fsc
->add_adapter_routes(fs
, &dev
->routes
);
1305 static void virtio_ccw_release_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1307 S390FLICState
*fs
= s390_get_flic();
1308 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1310 fsc
->release_adapter_routes(fs
, &dev
->routes
);
1313 static int virtio_ccw_add_irqfd(VirtioCcwDevice
*dev
, int n
)
1315 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1316 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1317 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1319 return kvm_irqchip_add_irqfd_notifier(kvm_state
, notifier
, NULL
,
1320 dev
->routes
.gsi
[n
]);
1323 static void virtio_ccw_remove_irqfd(VirtioCcwDevice
*dev
, int n
)
1325 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1326 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1327 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1330 ret
= kvm_irqchip_remove_irqfd_notifier(kvm_state
, notifier
,
1331 dev
->routes
.gsi
[n
]);
1335 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice
*dev
, int n
,
1336 bool assign
, bool with_irqfd
)
1338 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1339 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1340 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1341 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1344 int r
= event_notifier_init(notifier
, 0);
1349 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
1351 r
= virtio_ccw_add_irqfd(dev
, n
);
1353 virtio_queue_set_guest_notifier_fd_handler(vq
, false,
1359 * We do not support individual masking for channel devices, so we
1360 * need to manually trigger any guest masking callbacks here.
1362 if (k
->guest_notifier_mask
) {
1363 k
->guest_notifier_mask(vdev
, n
, false);
1365 /* get lost events and re-inject */
1366 if (k
->guest_notifier_pending
&&
1367 k
->guest_notifier_pending(vdev
, n
)) {
1368 event_notifier_set(notifier
);
1371 if (k
->guest_notifier_mask
) {
1372 k
->guest_notifier_mask(vdev
, n
, true);
1375 virtio_ccw_remove_irqfd(dev
, n
);
1377 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
1378 event_notifier_cleanup(notifier
);
1383 static int virtio_ccw_set_guest_notifiers(DeviceState
*d
, int nvqs
,
1386 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1387 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1388 bool with_irqfd
= dev
->sch
->thinint_active
&& kvm_irqfds_enabled();
1391 if (with_irqfd
&& assigned
) {
1392 /* irq routes need to be set up before assigning irqfds */
1393 r
= virtio_ccw_setup_irqroutes(dev
, nvqs
);
1395 goto irqroute_error
;
1398 for (n
= 0; n
< nvqs
; n
++) {
1399 if (!virtio_queue_get_num(vdev
, n
)) {
1402 r
= virtio_ccw_set_guest_notifier(dev
, n
, assigned
, with_irqfd
);
1407 if (with_irqfd
&& !assigned
) {
1408 /* release irq routes after irqfds have been released */
1409 virtio_ccw_release_irqroutes(dev
, nvqs
);
1415 virtio_ccw_set_guest_notifier(dev
, n
, !assigned
, false);
1418 if (with_irqfd
&& assigned
) {
1419 virtio_ccw_release_irqroutes(dev
, nvqs
);
1424 static void virtio_ccw_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1426 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1427 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1429 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
1432 static int virtio_ccw_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1434 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1435 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1438 qemu_get_be16s(f
, &vector
);
1439 virtio_queue_set_vector(vdev
, n
, vector
);
1444 static void virtio_ccw_save_config(DeviceState
*d
, QEMUFile
*f
)
1446 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1447 SubchDev
*s
= dev
->sch
;
1448 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1450 subch_device_save(s
, f
);
1451 if (dev
->indicators
!= NULL
) {
1452 qemu_put_be32(f
, dev
->indicators
->len
);
1453 qemu_put_be64(f
, dev
->indicators
->addr
);
1455 qemu_put_be32(f
, 0);
1456 qemu_put_be64(f
, 0UL);
1458 if (dev
->indicators2
!= NULL
) {
1459 qemu_put_be32(f
, dev
->indicators2
->len
);
1460 qemu_put_be64(f
, dev
->indicators2
->addr
);
1462 qemu_put_be32(f
, 0);
1463 qemu_put_be64(f
, 0UL);
1465 if (dev
->summary_indicator
!= NULL
) {
1466 qemu_put_be32(f
, dev
->summary_indicator
->len
);
1467 qemu_put_be64(f
, dev
->summary_indicator
->addr
);
1469 qemu_put_be32(f
, 0);
1470 qemu_put_be64(f
, 0UL);
1472 qemu_put_be16(f
, vdev
->config_vector
);
1473 qemu_put_be64(f
, dev
->routes
.adapter
.ind_offset
);
1474 qemu_put_byte(f
, dev
->thinint_isc
);
1475 qemu_put_be32(f
, dev
->revision
);
1478 static int virtio_ccw_load_config(DeviceState
*d
, QEMUFile
*f
)
1480 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1481 SubchDev
*s
= dev
->sch
;
1482 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1485 s
->driver_data
= dev
;
1486 subch_device_load(s
, f
);
1487 len
= qemu_get_be32(f
);
1489 dev
->indicators
= get_indicator(qemu_get_be64(f
), len
);
1492 dev
->indicators
= NULL
;
1494 len
= qemu_get_be32(f
);
1496 dev
->indicators2
= get_indicator(qemu_get_be64(f
), len
);
1499 dev
->indicators2
= NULL
;
1501 len
= qemu_get_be32(f
);
1503 dev
->summary_indicator
= get_indicator(qemu_get_be64(f
), len
);
1506 dev
->summary_indicator
= NULL
;
1508 qemu_get_be16s(f
, &vdev
->config_vector
);
1509 dev
->routes
.adapter
.ind_offset
= qemu_get_be64(f
);
1510 dev
->thinint_isc
= qemu_get_byte(f
);
1511 if (s
->thinint_active
) {
1512 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
1513 dev
->thinint_isc
, true, false,
1514 &dev
->routes
.adapter
.adapter_id
);
1516 dev
->revision
= qemu_get_be32(f
);
1521 /* This is called by virtio-bus just after the device is plugged. */
1522 static void virtio_ccw_device_plugged(DeviceState
*d
, Error
**errp
)
1524 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1525 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1526 SubchDev
*sch
= dev
->sch
;
1527 int n
= virtio_get_num_queues(vdev
);
1529 if (virtio_get_num_queues(vdev
) > VIRTIO_CCW_QUEUE_MAX
) {
1530 error_setg(errp
, "The nubmer of virtqueues %d "
1531 "exceeds ccw limit %d", n
,
1532 VIRTIO_CCW_QUEUE_MAX
);
1536 if (!kvm_eventfds_enabled()) {
1537 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
1540 sch
->id
.cu_model
= virtio_bus_get_vdev_id(&dev
->bus
);
1542 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
1546 static void virtio_ccw_device_unplugged(DeviceState
*d
)
1548 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1550 virtio_ccw_stop_ioeventfd(dev
);
1552 /**************** Virtio-ccw Bus Device Descriptions *******************/
1554 static Property virtio_ccw_net_properties
[] = {
1555 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1556 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1557 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1558 DEFINE_PROP_END_OF_LIST(),
1561 static void virtio_ccw_net_class_init(ObjectClass
*klass
, void *data
)
1563 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1564 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1566 k
->realize
= virtio_ccw_net_realize
;
1567 k
->exit
= virtio_ccw_exit
;
1568 dc
->reset
= virtio_ccw_reset
;
1569 dc
->props
= virtio_ccw_net_properties
;
1570 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1573 static const TypeInfo virtio_ccw_net
= {
1574 .name
= TYPE_VIRTIO_NET_CCW
,
1575 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1576 .instance_size
= sizeof(VirtIONetCcw
),
1577 .instance_init
= virtio_ccw_net_instance_init
,
1578 .class_init
= virtio_ccw_net_class_init
,
1581 static Property virtio_ccw_blk_properties
[] = {
1582 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1583 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1584 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1585 DEFINE_PROP_END_OF_LIST(),
1588 static void virtio_ccw_blk_class_init(ObjectClass
*klass
, void *data
)
1590 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1591 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1593 k
->realize
= virtio_ccw_blk_realize
;
1594 k
->exit
= virtio_ccw_exit
;
1595 dc
->reset
= virtio_ccw_reset
;
1596 dc
->props
= virtio_ccw_blk_properties
;
1597 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1600 static const TypeInfo virtio_ccw_blk
= {
1601 .name
= TYPE_VIRTIO_BLK_CCW
,
1602 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1603 .instance_size
= sizeof(VirtIOBlkCcw
),
1604 .instance_init
= virtio_ccw_blk_instance_init
,
1605 .class_init
= virtio_ccw_blk_class_init
,
1608 static Property virtio_ccw_serial_properties
[] = {
1609 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1610 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1611 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1612 DEFINE_PROP_END_OF_LIST(),
1615 static void virtio_ccw_serial_class_init(ObjectClass
*klass
, void *data
)
1617 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1618 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1620 k
->realize
= virtio_ccw_serial_realize
;
1621 k
->exit
= virtio_ccw_exit
;
1622 dc
->reset
= virtio_ccw_reset
;
1623 dc
->props
= virtio_ccw_serial_properties
;
1624 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1627 static const TypeInfo virtio_ccw_serial
= {
1628 .name
= TYPE_VIRTIO_SERIAL_CCW
,
1629 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1630 .instance_size
= sizeof(VirtioSerialCcw
),
1631 .instance_init
= virtio_ccw_serial_instance_init
,
1632 .class_init
= virtio_ccw_serial_class_init
,
1635 static Property virtio_ccw_balloon_properties
[] = {
1636 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1637 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1638 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1639 DEFINE_PROP_END_OF_LIST(),
1642 static void virtio_ccw_balloon_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_balloon_realize
;
1648 k
->exit
= virtio_ccw_exit
;
1649 dc
->reset
= virtio_ccw_reset
;
1650 dc
->props
= virtio_ccw_balloon_properties
;
1651 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1654 static const TypeInfo virtio_ccw_balloon
= {
1655 .name
= TYPE_VIRTIO_BALLOON_CCW
,
1656 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1657 .instance_size
= sizeof(VirtIOBalloonCcw
),
1658 .instance_init
= virtio_ccw_balloon_instance_init
,
1659 .class_init
= virtio_ccw_balloon_class_init
,
1662 static Property virtio_ccw_scsi_properties
[] = {
1663 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1664 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1665 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1666 DEFINE_PROP_END_OF_LIST(),
1669 static void virtio_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1671 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1672 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1674 k
->realize
= virtio_ccw_scsi_realize
;
1675 k
->exit
= virtio_ccw_exit
;
1676 dc
->reset
= virtio_ccw_reset
;
1677 dc
->props
= virtio_ccw_scsi_properties
;
1678 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1681 static const TypeInfo virtio_ccw_scsi
= {
1682 .name
= TYPE_VIRTIO_SCSI_CCW
,
1683 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1684 .instance_size
= sizeof(VirtIOSCSICcw
),
1685 .instance_init
= virtio_ccw_scsi_instance_init
,
1686 .class_init
= virtio_ccw_scsi_class_init
,
1689 #ifdef CONFIG_VHOST_SCSI
1690 static Property vhost_ccw_scsi_properties
[] = {
1691 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1692 DEFINE_PROP_END_OF_LIST(),
1695 static void vhost_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1697 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1698 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1700 k
->realize
= vhost_ccw_scsi_realize
;
1701 k
->exit
= virtio_ccw_exit
;
1702 dc
->reset
= virtio_ccw_reset
;
1703 dc
->props
= vhost_ccw_scsi_properties
;
1704 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1707 static const TypeInfo vhost_ccw_scsi
= {
1708 .name
= TYPE_VHOST_SCSI_CCW
,
1709 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1710 .instance_size
= sizeof(VHostSCSICcw
),
1711 .instance_init
= vhost_ccw_scsi_instance_init
,
1712 .class_init
= vhost_ccw_scsi_class_init
,
1716 static void virtio_ccw_rng_instance_init(Object
*obj
)
1718 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(obj
);
1720 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1722 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
),
1723 "rng", &error_abort
);
1726 static Property virtio_ccw_rng_properties
[] = {
1727 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1728 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1729 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1730 DEFINE_PROP_END_OF_LIST(),
1733 static void virtio_ccw_rng_class_init(ObjectClass
*klass
, void *data
)
1735 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1736 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1738 k
->realize
= virtio_ccw_rng_realize
;
1739 k
->exit
= virtio_ccw_exit
;
1740 dc
->reset
= virtio_ccw_reset
;
1741 dc
->props
= virtio_ccw_rng_properties
;
1742 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1745 static const TypeInfo virtio_ccw_rng
= {
1746 .name
= TYPE_VIRTIO_RNG_CCW
,
1747 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1748 .instance_size
= sizeof(VirtIORNGCcw
),
1749 .instance_init
= virtio_ccw_rng_instance_init
,
1750 .class_init
= virtio_ccw_rng_class_init
,
1753 static void virtio_ccw_busdev_realize(DeviceState
*dev
, Error
**errp
)
1755 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1757 virtio_ccw_bus_new(&_dev
->bus
, sizeof(_dev
->bus
), _dev
);
1758 virtio_ccw_device_realize(_dev
, errp
);
1761 static int virtio_ccw_busdev_exit(DeviceState
*dev
)
1763 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1764 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1766 return _info
->exit(_dev
);
1769 static void virtio_ccw_busdev_unplug(HotplugHandler
*hotplug_dev
,
1770 DeviceState
*dev
, Error
**errp
)
1772 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1773 SubchDev
*sch
= _dev
->sch
;
1775 virtio_ccw_stop_ioeventfd(_dev
);
1778 * We should arrive here only for device_del, since we don't support
1779 * direct hot(un)plug of channels, but only through virtio.
1781 assert(sch
!= NULL
);
1782 /* Subchannel is now disabled and no longer valid. */
1783 sch
->curr_status
.pmcw
.flags
&= ~(PMCW_FLAGS_MASK_ENA
|
1784 PMCW_FLAGS_MASK_DNV
);
1786 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
, 1, 0);
1788 object_unparent(OBJECT(dev
));
1791 static void virtio_ccw_device_class_init(ObjectClass
*klass
, void *data
)
1793 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1795 dc
->realize
= virtio_ccw_busdev_realize
;
1796 dc
->exit
= virtio_ccw_busdev_exit
;
1797 dc
->bus_type
= TYPE_VIRTUAL_CSS_BUS
;
1800 static const TypeInfo virtio_ccw_device_info
= {
1801 .name
= TYPE_VIRTIO_CCW_DEVICE
,
1802 .parent
= TYPE_DEVICE
,
1803 .instance_size
= sizeof(VirtioCcwDevice
),
1804 .class_init
= virtio_ccw_device_class_init
,
1805 .class_size
= sizeof(VirtIOCCWDeviceClass
),
1809 /***************** Virtual-css Bus Bridge Device ********************/
1810 /* Only required to have the virtio bus as child in the system bus */
1812 static int virtual_css_bridge_init(SysBusDevice
*dev
)
1818 static void virtual_css_bridge_class_init(ObjectClass
*klass
, void *data
)
1820 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1821 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1822 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1824 k
->init
= virtual_css_bridge_init
;
1825 hc
->unplug
= virtio_ccw_busdev_unplug
;
1826 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1829 static const TypeInfo virtual_css_bridge_info
= {
1830 .name
= "virtual-css-bridge",
1831 .parent
= TYPE_SYS_BUS_DEVICE
,
1832 .instance_size
= sizeof(SysBusDevice
),
1833 .class_init
= virtual_css_bridge_class_init
,
1834 .interfaces
= (InterfaceInfo
[]) {
1835 { TYPE_HOTPLUG_HANDLER
},
1840 /* virtio-ccw-bus */
1842 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1843 VirtioCcwDevice
*dev
)
1845 DeviceState
*qdev
= DEVICE(dev
);
1846 char virtio_bus_name
[] = "virtio-bus";
1848 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_CCW_BUS
,
1849 qdev
, virtio_bus_name
);
1852 static void virtio_ccw_bus_class_init(ObjectClass
*klass
, void *data
)
1854 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1855 BusClass
*bus_class
= BUS_CLASS(klass
);
1857 bus_class
->max_dev
= 1;
1858 k
->notify
= virtio_ccw_notify
;
1859 k
->vmstate_change
= virtio_ccw_vmstate_change
;
1860 k
->query_guest_notifiers
= virtio_ccw_query_guest_notifiers
;
1861 k
->set_host_notifier
= virtio_ccw_set_host_notifier
;
1862 k
->set_guest_notifiers
= virtio_ccw_set_guest_notifiers
;
1863 k
->save_queue
= virtio_ccw_save_queue
;
1864 k
->load_queue
= virtio_ccw_load_queue
;
1865 k
->save_config
= virtio_ccw_save_config
;
1866 k
->load_config
= virtio_ccw_load_config
;
1867 k
->device_plugged
= virtio_ccw_device_plugged
;
1868 k
->device_unplugged
= virtio_ccw_device_unplugged
;
1871 static const TypeInfo virtio_ccw_bus_info
= {
1872 .name
= TYPE_VIRTIO_CCW_BUS
,
1873 .parent
= TYPE_VIRTIO_BUS
,
1874 .instance_size
= sizeof(VirtioCcwBusState
),
1875 .class_init
= virtio_ccw_bus_class_init
,
1878 #ifdef CONFIG_VIRTFS
1879 static Property virtio_ccw_9p_properties
[] = {
1880 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1881 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1882 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1883 DEFINE_PROP_END_OF_LIST(),
1886 static void virtio_ccw_9p_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1888 V9fsCCWState
*dev
= VIRTIO_9P_CCW(ccw_dev
);
1889 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1892 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1893 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1895 error_propagate(errp
, err
);
1899 static void virtio_ccw_9p_class_init(ObjectClass
*klass
, void *data
)
1901 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1902 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1904 k
->exit
= virtio_ccw_exit
;
1905 k
->realize
= virtio_ccw_9p_realize
;
1906 dc
->reset
= virtio_ccw_reset
;
1907 dc
->props
= virtio_ccw_9p_properties
;
1908 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1911 static void virtio_ccw_9p_instance_init(Object
*obj
)
1913 V9fsCCWState
*dev
= VIRTIO_9P_CCW(obj
);
1915 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1919 static const TypeInfo virtio_ccw_9p_info
= {
1920 .name
= TYPE_VIRTIO_9P_CCW
,
1921 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1922 .instance_size
= sizeof(V9fsCCWState
),
1923 .instance_init
= virtio_ccw_9p_instance_init
,
1924 .class_init
= virtio_ccw_9p_class_init
,
1928 static void virtio_ccw_register(void)
1930 type_register_static(&virtio_ccw_bus_info
);
1931 type_register_static(&virtual_css_bus_info
);
1932 type_register_static(&virtio_ccw_device_info
);
1933 type_register_static(&virtio_ccw_serial
);
1934 type_register_static(&virtio_ccw_blk
);
1935 type_register_static(&virtio_ccw_net
);
1936 type_register_static(&virtio_ccw_balloon
);
1937 type_register_static(&virtio_ccw_scsi
);
1938 #ifdef CONFIG_VHOST_SCSI
1939 type_register_static(&vhost_ccw_scsi
);
1941 type_register_static(&virtio_ccw_rng
);
1942 type_register_static(&virtual_css_bridge_info
);
1943 #ifdef CONFIG_VIRTFS
1944 type_register_static(&virtio_ccw_9p_info
);
1948 type_init(virtio_ccw_register
)