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
);
311 /* virtio-1 allows changing the ring size. */
312 if (virtio_queue_get_num(vdev
, index
) < num
) {
313 /* Fail if we exceed the maximum number. */
316 virtio_queue_set_num(vdev
, index
, num
);
317 } else if (virtio_queue_get_num(vdev
, index
) > num
) {
318 /* Fail if we don't have a big enough queue. */
321 /* We ignore possible increased num for legacy for compatibility. */
322 virtio_queue_set_vector(vdev
, index
, index
);
324 /* tell notify handler in case of config change */
325 vdev
->config_vector
= VIRTIO_CCW_QUEUE_MAX
;
329 static void virtio_ccw_reset_virtio(VirtioCcwDevice
*dev
, VirtIODevice
*vdev
)
331 virtio_ccw_stop_ioeventfd(dev
);
333 if (dev
->indicators
) {
334 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
335 dev
->indicators
= NULL
;
337 if (dev
->indicators2
) {
338 release_indicator(&dev
->routes
.adapter
, dev
->indicators2
);
339 dev
->indicators2
= NULL
;
341 if (dev
->summary_indicator
) {
342 release_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
343 dev
->summary_indicator
= NULL
;
345 dev
->sch
->thinint_active
= false;
348 static int virtio_ccw_handle_set_vq(SubchDev
*sch
, CCW1 ccw
, bool check_len
,
353 VqInfoBlockLegacy linfo
;
354 size_t info_len
= is_legacy
? sizeof(linfo
) : sizeof(info
);
357 if (ccw
.count
!= info_len
) {
360 } else if (ccw
.count
< info_len
) {
361 /* Can't execute command. */
368 linfo
.queue
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
369 MEMTXATTRS_UNSPECIFIED
, NULL
);
370 linfo
.align
= address_space_ldl_be(&address_space_memory
,
371 ccw
.cda
+ sizeof(linfo
.queue
),
372 MEMTXATTRS_UNSPECIFIED
,
374 linfo
.index
= address_space_lduw_be(&address_space_memory
,
375 ccw
.cda
+ sizeof(linfo
.queue
)
376 + sizeof(linfo
.align
),
377 MEMTXATTRS_UNSPECIFIED
,
379 linfo
.num
= address_space_lduw_be(&address_space_memory
,
380 ccw
.cda
+ sizeof(linfo
.queue
)
381 + sizeof(linfo
.align
)
382 + sizeof(linfo
.index
),
383 MEMTXATTRS_UNSPECIFIED
,
385 ret
= virtio_ccw_set_vqs(sch
, NULL
, &linfo
);
387 info
.desc
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
388 MEMTXATTRS_UNSPECIFIED
, NULL
);
389 info
.index
= address_space_lduw_be(&address_space_memory
,
390 ccw
.cda
+ sizeof(info
.desc
)
392 MEMTXATTRS_UNSPECIFIED
, NULL
);
393 info
.num
= address_space_lduw_be(&address_space_memory
,
394 ccw
.cda
+ sizeof(info
.desc
)
396 + sizeof(info
.index
),
397 MEMTXATTRS_UNSPECIFIED
, NULL
);
398 info
.avail
= address_space_ldq_be(&address_space_memory
,
399 ccw
.cda
+ sizeof(info
.desc
)
403 MEMTXATTRS_UNSPECIFIED
, NULL
);
404 info
.used
= address_space_ldq_be(&address_space_memory
,
405 ccw
.cda
+ sizeof(info
.desc
)
409 + sizeof(info
.avail
),
410 MEMTXATTRS_UNSPECIFIED
, NULL
);
411 ret
= virtio_ccw_set_vqs(sch
, &info
, NULL
);
413 sch
->curr_status
.scsw
.count
= 0;
417 static int virtio_ccw_cb(SubchDev
*sch
, CCW1 ccw
)
420 VirtioRevInfo revinfo
;
422 VirtioFeatDesc features
;
425 VqConfigBlock vq_config
;
426 VirtioCcwDevice
*dev
= sch
->driver_data
;
427 VirtIODevice
*vdev
= virtio_ccw_get_vdev(sch
);
431 VirtioThinintInfo
*thinint
;
437 trace_virtio_ccw_interpret_ccw(sch
->cssid
, sch
->ssid
, sch
->schid
,
439 check_len
= !((ccw
.flags
& CCW_FLAG_SLI
) && !(ccw
.flags
& CCW_FLAG_DC
));
441 /* Look at the command. */
442 switch (ccw
.cmd_code
) {
444 ret
= virtio_ccw_handle_set_vq(sch
, ccw
, check_len
, dev
->revision
< 1);
446 case CCW_CMD_VDEV_RESET
:
447 virtio_ccw_reset_virtio(dev
, vdev
);
450 case CCW_CMD_READ_FEAT
:
452 if (ccw
.count
!= sizeof(features
)) {
456 } else if (ccw
.count
< sizeof(features
)) {
457 /* Can't execute command. */
464 features
.index
= address_space_ldub(&address_space_memory
,
466 + sizeof(features
.features
),
467 MEMTXATTRS_UNSPECIFIED
,
469 if (features
.index
== 0) {
470 features
.features
= (uint32_t)vdev
->host_features
;
471 } else if (features
.index
== 1) {
472 features
.features
= (uint32_t)(vdev
->host_features
>> 32);
474 * Don't offer version 1 to the guest if it did not
475 * negotiate at least revision 1.
477 if (dev
->revision
<= 0) {
478 features
.features
&= ~(1 << (VIRTIO_F_VERSION_1
- 32));
481 /* Return zeroes if the guest supports more feature bits. */
482 features
.features
= 0;
484 address_space_stl_le(&address_space_memory
, ccw
.cda
,
485 features
.features
, MEMTXATTRS_UNSPECIFIED
,
487 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
491 case CCW_CMD_WRITE_FEAT
:
493 if (ccw
.count
!= sizeof(features
)) {
497 } else if (ccw
.count
< sizeof(features
)) {
498 /* Can't execute command. */
505 features
.index
= address_space_ldub(&address_space_memory
,
507 + sizeof(features
.features
),
508 MEMTXATTRS_UNSPECIFIED
,
510 features
.features
= address_space_ldl_le(&address_space_memory
,
512 MEMTXATTRS_UNSPECIFIED
,
514 if (features
.index
== 0) {
515 virtio_set_features(vdev
,
516 (vdev
->guest_features
& 0xffffffff00000000ULL
) |
518 } else if (features
.index
== 1) {
520 * The guest should not set version 1 if it didn't
521 * negotiate a revision >= 1.
523 if (dev
->revision
<= 0) {
524 features
.features
&= ~(1 << (VIRTIO_F_VERSION_1
- 32));
526 virtio_set_features(vdev
,
527 (vdev
->guest_features
& 0x00000000ffffffffULL
) |
528 ((uint64_t)features
.features
<< 32));
531 * If the guest supports more feature bits, assert that it
532 * passes us zeroes for those we don't support.
534 if (features
.features
) {
535 fprintf(stderr
, "Guest bug: features[%i]=%x (expected 0)\n",
536 features
.index
, features
.features
);
537 /* XXX: do a unit check here? */
540 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
544 case CCW_CMD_READ_CONF
:
546 if (ccw
.count
> vdev
->config_len
) {
551 len
= MIN(ccw
.count
, vdev
->config_len
);
555 virtio_bus_get_vdev_config(&dev
->bus
, vdev
->config
);
556 /* XXX config space endianness */
557 cpu_physical_memory_write(ccw
.cda
, vdev
->config
, len
);
558 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
562 case CCW_CMD_WRITE_CONF
:
564 if (ccw
.count
> vdev
->config_len
) {
569 len
= MIN(ccw
.count
, vdev
->config_len
);
574 config
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
579 /* XXX config space endianness */
580 memcpy(vdev
->config
, config
, len
);
581 cpu_physical_memory_unmap(config
, hw_len
, 0, hw_len
);
582 virtio_bus_set_vdev_config(&dev
->bus
, vdev
->config
);
583 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
588 case CCW_CMD_WRITE_STATUS
:
590 if (ccw
.count
!= sizeof(status
)) {
594 } else if (ccw
.count
< sizeof(status
)) {
595 /* Can't execute command. */
602 status
= address_space_ldub(&address_space_memory
, ccw
.cda
,
603 MEMTXATTRS_UNSPECIFIED
, NULL
);
604 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
605 virtio_ccw_stop_ioeventfd(dev
);
607 if (virtio_set_status(vdev
, status
) == 0) {
608 if (vdev
->status
== 0) {
609 virtio_ccw_reset_virtio(dev
, vdev
);
611 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
612 virtio_ccw_start_ioeventfd(dev
);
614 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(status
);
617 /* Trigger a command reject. */
622 case CCW_CMD_SET_IND
:
624 if (ccw
.count
!= sizeof(indicators
)) {
628 } else if (ccw
.count
< sizeof(indicators
)) {
629 /* Can't execute command. */
633 if (sch
->thinint_active
) {
634 /* Trigger a command reject. */
641 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
642 MEMTXATTRS_UNSPECIFIED
, NULL
);
643 dev
->indicators
= get_indicator(indicators
, sizeof(uint64_t));
644 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
648 case CCW_CMD_SET_CONF_IND
:
650 if (ccw
.count
!= sizeof(indicators
)) {
654 } else if (ccw
.count
< sizeof(indicators
)) {
655 /* Can't execute command. */
662 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
663 MEMTXATTRS_UNSPECIFIED
, NULL
);
664 dev
->indicators2
= get_indicator(indicators
, sizeof(uint64_t));
665 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
669 case CCW_CMD_READ_VQ_CONF
:
671 if (ccw
.count
!= sizeof(vq_config
)) {
675 } else if (ccw
.count
< sizeof(vq_config
)) {
676 /* Can't execute command. */
683 vq_config
.index
= address_space_lduw_be(&address_space_memory
,
685 MEMTXATTRS_UNSPECIFIED
,
687 if (vq_config
.index
>= VIRTIO_CCW_QUEUE_MAX
) {
691 vq_config
.num_max
= virtio_queue_get_num(vdev
,
693 address_space_stw_be(&address_space_memory
,
694 ccw
.cda
+ sizeof(vq_config
.index
),
696 MEMTXATTRS_UNSPECIFIED
,
698 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(vq_config
);
702 case CCW_CMD_SET_IND_ADAPTER
:
704 if (ccw
.count
!= sizeof(*thinint
)) {
708 } else if (ccw
.count
< sizeof(*thinint
)) {
709 /* Can't execute command. */
713 len
= sizeof(*thinint
);
717 } else if (dev
->indicators
&& !sch
->thinint_active
) {
718 /* Trigger a command reject. */
721 thinint
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
725 uint64_t ind_bit
= ldq_be_p(&thinint
->ind_bit
);
728 dev
->summary_indicator
=
729 get_indicator(ldq_be_p(&thinint
->summary_indicator
),
732 get_indicator(ldq_be_p(&thinint
->device_indicator
),
734 dev
->thinint_isc
= thinint
->isc
;
735 dev
->routes
.adapter
.ind_offset
= ind_bit
;
736 dev
->routes
.adapter
.summary_offset
= 7;
737 cpu_physical_memory_unmap(thinint
, hw_len
, 0, hw_len
);
738 ret
= css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
739 dev
->thinint_isc
, true, false,
740 &dev
->routes
.adapter
.adapter_id
);
742 sch
->thinint_active
= ((dev
->indicators
!= NULL
) &&
743 (dev
->summary_indicator
!= NULL
));
744 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
749 case CCW_CMD_SET_VIRTIO_REV
:
750 len
= sizeof(revinfo
);
751 if (ccw
.count
< len
) {
760 address_space_lduw_be(&address_space_memory
, ccw
.cda
,
761 MEMTXATTRS_UNSPECIFIED
, NULL
);
763 address_space_lduw_be(&address_space_memory
,
764 ccw
.cda
+ sizeof(revinfo
.revision
),
765 MEMTXATTRS_UNSPECIFIED
, NULL
);
766 if (ccw
.count
< len
+ revinfo
.length
||
767 (check_len
&& ccw
.count
> len
+ revinfo
.length
)) {
772 * Once we start to support revisions with additional data, we'll
773 * need to fetch it here. Nothing to do for now, though.
775 if (dev
->revision
>= 0 ||
776 revinfo
.revision
> virtio_ccw_rev_max(vdev
)) {
781 dev
->revision
= revinfo
.revision
;
790 static void virtio_sch_disable_cb(SubchDev
*sch
)
792 VirtioCcwDevice
*dev
= sch
->driver_data
;
797 static void virtio_ccw_device_realize(VirtioCcwDevice
*dev
, Error
**errp
)
799 unsigned int cssid
= 0;
800 unsigned int ssid
= 0;
803 bool have_devno
= false;
808 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
810 sch
= g_malloc0(sizeof(SubchDev
));
812 sch
->driver_data
= dev
;
815 dev
->indicators
= NULL
;
817 /* Initialize subchannel structure. */
818 sch
->channel_prog
= 0x0;
819 sch
->last_cmd_valid
= false;
820 sch
->thinint_active
= false;
822 * Use a device number if provided. Otherwise, fall back to subchannel
826 num
= sscanf(dev
->bus_id
, "%x.%x.%04x", &cssid
, &ssid
, &devno
);
828 if ((cssid
> MAX_CSSID
) || (ssid
> MAX_SSID
)) {
829 error_setg(errp
, "Invalid cssid or ssid: cssid %x, ssid %x",
833 /* Enforce use of virtual cssid. */
834 if (cssid
!= VIRTUAL_CSSID
) {
835 error_setg(errp
, "cssid %x not valid for virtio devices",
839 if (css_devno_used(cssid
, ssid
, devno
)) {
840 error_setg(errp
, "Device %x.%x.%04x already exists",
849 error_setg(errp
, "Malformed devno parameter '%s'", dev
->bus_id
);
854 /* Find the next free id. */
856 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
857 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
859 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
865 error_setg(errp
, "No free subchannel found for %x.%x.%04x",
869 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
872 cssid
= VIRTUAL_CSSID
;
873 for (ssid
= 0; ssid
<= MAX_SSID
; ssid
++) {
874 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
875 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
881 * If the devno is already taken, look further in this
884 while (css_devno_used(cssid
, ssid
, devno
)) {
885 if (devno
== MAX_SCHID
) {
887 } else if (devno
== schid
- 1) {
888 error_setg(errp
, "No free devno found");
895 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
905 error_setg(errp
, "Virtual channel subsystem is full!");
908 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
912 /* Build initial schib. */
913 css_sch_build_virtual_schib(sch
, 0, VIRTIO_CCW_CHPID_TYPE
);
915 sch
->ccw_cb
= virtio_ccw_cb
;
916 sch
->disable_cb
= virtio_sch_disable_cb
;
918 /* Build senseid data. */
919 memset(&sch
->id
, 0, sizeof(SenseId
));
920 sch
->id
.reserved
= 0xff;
921 sch
->id
.cu_type
= VIRTIO_CCW_CU_TYPE
;
926 k
->realize(dev
, &err
);
929 error_propagate(errp
, err
);
930 css_subch_assign(cssid
, ssid
, schid
, devno
, NULL
);
941 static int virtio_ccw_exit(VirtioCcwDevice
*dev
)
943 SubchDev
*sch
= dev
->sch
;
946 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
949 if (dev
->indicators
) {
950 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
951 dev
->indicators
= NULL
;
956 static void virtio_ccw_net_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
958 DeviceState
*qdev
= DEVICE(ccw_dev
);
959 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(ccw_dev
);
960 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
963 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
964 object_get_typename(OBJECT(qdev
)));
965 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
966 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
968 error_propagate(errp
, err
);
972 static void virtio_ccw_net_instance_init(Object
*obj
)
974 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(obj
);
976 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
978 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
979 "bootindex", &error_abort
);
982 static void virtio_ccw_blk_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
984 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(ccw_dev
);
985 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
988 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
989 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
991 error_propagate(errp
, err
);
995 static void virtio_ccw_blk_instance_init(Object
*obj
)
997 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(obj
);
999 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1001 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1003 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1004 "bootindex", &error_abort
);
1007 static void virtio_ccw_serial_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1009 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(ccw_dev
);
1010 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1011 DeviceState
*proxy
= DEVICE(ccw_dev
);
1016 * For command line compatibility, this sets the virtio-serial-device bus
1020 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1021 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1025 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1026 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1028 error_propagate(errp
, err
);
1033 static void virtio_ccw_serial_instance_init(Object
*obj
)
1035 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(obj
);
1037 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1038 TYPE_VIRTIO_SERIAL
);
1041 static void virtio_ccw_balloon_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1043 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(ccw_dev
);
1044 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1047 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1048 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1050 error_propagate(errp
, err
);
1054 static void virtio_ccw_balloon_instance_init(Object
*obj
)
1056 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(obj
);
1058 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1059 TYPE_VIRTIO_BALLOON
);
1060 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1061 "guest-stats", &error_abort
);
1062 object_property_add_alias(obj
, "guest-stats-polling-interval",
1064 "guest-stats-polling-interval", &error_abort
);
1067 static void virtio_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1069 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(ccw_dev
);
1070 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1071 DeviceState
*qdev
= DEVICE(ccw_dev
);
1076 * For command line compatibility, this sets the virtio-scsi-device bus
1080 bus_name
= g_strdup_printf("%s.0", qdev
->id
);
1081 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1085 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1086 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1088 error_propagate(errp
, err
);
1092 static void virtio_ccw_scsi_instance_init(Object
*obj
)
1094 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(obj
);
1096 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1098 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1102 #ifdef CONFIG_VHOST_SCSI
1103 static void vhost_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1105 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(ccw_dev
);
1106 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1109 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1110 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1112 error_propagate(errp
, err
);
1116 static void vhost_ccw_scsi_instance_init(Object
*obj
)
1118 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(obj
);
1120 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1125 static void virtio_ccw_rng_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1127 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(ccw_dev
);
1128 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1131 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1132 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1134 error_propagate(errp
, err
);
1138 object_property_set_link(OBJECT(dev
),
1139 OBJECT(dev
->vdev
.conf
.rng
), "rng",
1143 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1144 * be careful and test performance if you change this.
1146 static inline VirtioCcwDevice
*to_virtio_ccw_dev_fast(DeviceState
*d
)
1148 return container_of(d
, VirtioCcwDevice
, parent_obj
);
1151 static uint8_t virtio_set_ind_atomic(SubchDev
*sch
, uint64_t ind_loc
,
1154 uint8_t ind_old
, ind_new
;
1158 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
1160 error_report("%s(%x.%x.%04x): unable to access indicator",
1161 __func__
, sch
->cssid
, sch
->ssid
, sch
->schid
);
1165 ind_old
= *ind_addr
;
1166 ind_new
= ind_old
| to_be_set
;
1167 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
1168 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
1173 static void virtio_ccw_notify(DeviceState
*d
, uint16_t vector
)
1175 VirtioCcwDevice
*dev
= to_virtio_ccw_dev_fast(d
);
1176 SubchDev
*sch
= dev
->sch
;
1177 uint64_t indicators
;
1179 if (vector
>= 128) {
1183 if (vector
< VIRTIO_CCW_QUEUE_MAX
) {
1184 if (!dev
->indicators
) {
1187 if (sch
->thinint_active
) {
1189 * In the adapter interrupt case, indicators points to a
1190 * memory area that may be (way) larger than 64 bit and
1191 * ind_bit indicates the start of the indicators in a big
1194 uint64_t ind_bit
= dev
->routes
.adapter
.ind_offset
;
1196 virtio_set_ind_atomic(sch
, dev
->indicators
->addr
+
1197 (ind_bit
+ vector
) / 8,
1198 0x80 >> ((ind_bit
+ vector
) % 8));
1199 if (!virtio_set_ind_atomic(sch
, dev
->summary_indicator
->addr
,
1201 css_adapter_interrupt(dev
->thinint_isc
);
1204 indicators
= address_space_ldq(&address_space_memory
,
1205 dev
->indicators
->addr
,
1206 MEMTXATTRS_UNSPECIFIED
,
1208 indicators
|= 1ULL << vector
;
1209 address_space_stq(&address_space_memory
, dev
->indicators
->addr
,
1210 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1211 css_conditional_io_interrupt(sch
);
1214 if (!dev
->indicators2
) {
1218 indicators
= address_space_ldq(&address_space_memory
,
1219 dev
->indicators2
->addr
,
1220 MEMTXATTRS_UNSPECIFIED
,
1222 indicators
|= 1ULL << vector
;
1223 address_space_stq(&address_space_memory
, dev
->indicators2
->addr
,
1224 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1225 css_conditional_io_interrupt(sch
);
1229 static void virtio_ccw_reset(DeviceState
*d
)
1231 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1232 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1234 virtio_ccw_reset_virtio(dev
, vdev
);
1235 css_reset_sch(dev
->sch
);
1238 static void virtio_ccw_vmstate_change(DeviceState
*d
, bool running
)
1240 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1243 virtio_ccw_start_ioeventfd(dev
);
1245 virtio_ccw_stop_ioeventfd(dev
);
1249 static bool virtio_ccw_query_guest_notifiers(DeviceState
*d
)
1251 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1253 return !!(dev
->sch
->curr_status
.pmcw
.flags
& PMCW_FLAGS_MASK_ENA
);
1256 static int virtio_ccw_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
1258 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1260 /* Stop using the generic ioeventfd, we are doing eventfd handling
1261 * ourselves below */
1262 dev
->ioeventfd_disabled
= assign
;
1264 virtio_ccw_stop_ioeventfd(dev
);
1266 return virtio_ccw_set_guest2host_notifier(dev
, n
, assign
, false);
1269 static int virtio_ccw_get_mappings(VirtioCcwDevice
*dev
)
1273 if (!dev
->sch
->thinint_active
) {
1277 r
= map_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
1281 r
= map_indicator(&dev
->routes
.adapter
, dev
->indicators
);
1285 dev
->routes
.adapter
.summary_addr
= dev
->summary_indicator
->map
;
1286 dev
->routes
.adapter
.ind_addr
= dev
->indicators
->map
;
1291 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1294 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1296 S390FLICState
*fs
= s390_get_flic();
1297 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1299 ret
= virtio_ccw_get_mappings(dev
);
1303 for (i
= 0; i
< nvqs
; i
++) {
1304 if (!virtio_queue_get_num(vdev
, i
)) {
1308 dev
->routes
.num_routes
= i
;
1309 return fsc
->add_adapter_routes(fs
, &dev
->routes
);
1312 static void virtio_ccw_release_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1314 S390FLICState
*fs
= s390_get_flic();
1315 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1317 fsc
->release_adapter_routes(fs
, &dev
->routes
);
1320 static int virtio_ccw_add_irqfd(VirtioCcwDevice
*dev
, int n
)
1322 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1323 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1324 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1326 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, notifier
, NULL
,
1327 dev
->routes
.gsi
[n
]);
1330 static void virtio_ccw_remove_irqfd(VirtioCcwDevice
*dev
, int n
)
1332 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1333 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1334 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1337 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, notifier
,
1338 dev
->routes
.gsi
[n
]);
1342 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice
*dev
, int n
,
1343 bool assign
, bool with_irqfd
)
1345 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1346 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1347 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1348 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1351 int r
= event_notifier_init(notifier
, 0);
1356 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
1358 r
= virtio_ccw_add_irqfd(dev
, n
);
1360 virtio_queue_set_guest_notifier_fd_handler(vq
, false,
1366 * We do not support individual masking for channel devices, so we
1367 * need to manually trigger any guest masking callbacks here.
1369 if (k
->guest_notifier_mask
) {
1370 k
->guest_notifier_mask(vdev
, n
, false);
1372 /* get lost events and re-inject */
1373 if (k
->guest_notifier_pending
&&
1374 k
->guest_notifier_pending(vdev
, n
)) {
1375 event_notifier_set(notifier
);
1378 if (k
->guest_notifier_mask
) {
1379 k
->guest_notifier_mask(vdev
, n
, true);
1382 virtio_ccw_remove_irqfd(dev
, n
);
1384 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
1385 event_notifier_cleanup(notifier
);
1390 static int virtio_ccw_set_guest_notifiers(DeviceState
*d
, int nvqs
,
1393 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1394 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1395 bool with_irqfd
= dev
->sch
->thinint_active
&& kvm_irqfds_enabled();
1398 if (with_irqfd
&& assigned
) {
1399 /* irq routes need to be set up before assigning irqfds */
1400 r
= virtio_ccw_setup_irqroutes(dev
, nvqs
);
1402 goto irqroute_error
;
1405 for (n
= 0; n
< nvqs
; n
++) {
1406 if (!virtio_queue_get_num(vdev
, n
)) {
1409 r
= virtio_ccw_set_guest_notifier(dev
, n
, assigned
, with_irqfd
);
1414 if (with_irqfd
&& !assigned
) {
1415 /* release irq routes after irqfds have been released */
1416 virtio_ccw_release_irqroutes(dev
, nvqs
);
1422 virtio_ccw_set_guest_notifier(dev
, n
, !assigned
, false);
1425 if (with_irqfd
&& assigned
) {
1426 virtio_ccw_release_irqroutes(dev
, nvqs
);
1431 static void virtio_ccw_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1433 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1434 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1436 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
1439 static int virtio_ccw_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1441 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1442 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1445 qemu_get_be16s(f
, &vector
);
1446 virtio_queue_set_vector(vdev
, n
, vector
);
1451 static void virtio_ccw_save_config(DeviceState
*d
, QEMUFile
*f
)
1453 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1454 SubchDev
*s
= dev
->sch
;
1455 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1457 subch_device_save(s
, f
);
1458 if (dev
->indicators
!= NULL
) {
1459 qemu_put_be32(f
, dev
->indicators
->len
);
1460 qemu_put_be64(f
, dev
->indicators
->addr
);
1462 qemu_put_be32(f
, 0);
1463 qemu_put_be64(f
, 0UL);
1465 if (dev
->indicators2
!= NULL
) {
1466 qemu_put_be32(f
, dev
->indicators2
->len
);
1467 qemu_put_be64(f
, dev
->indicators2
->addr
);
1469 qemu_put_be32(f
, 0);
1470 qemu_put_be64(f
, 0UL);
1472 if (dev
->summary_indicator
!= NULL
) {
1473 qemu_put_be32(f
, dev
->summary_indicator
->len
);
1474 qemu_put_be64(f
, dev
->summary_indicator
->addr
);
1476 qemu_put_be32(f
, 0);
1477 qemu_put_be64(f
, 0UL);
1479 qemu_put_be16(f
, vdev
->config_vector
);
1480 qemu_put_be64(f
, dev
->routes
.adapter
.ind_offset
);
1481 qemu_put_byte(f
, dev
->thinint_isc
);
1482 qemu_put_be32(f
, dev
->revision
);
1485 static int virtio_ccw_load_config(DeviceState
*d
, QEMUFile
*f
)
1487 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1488 SubchDev
*s
= dev
->sch
;
1489 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1492 s
->driver_data
= dev
;
1493 subch_device_load(s
, f
);
1494 len
= qemu_get_be32(f
);
1496 dev
->indicators
= get_indicator(qemu_get_be64(f
), len
);
1499 dev
->indicators
= NULL
;
1501 len
= qemu_get_be32(f
);
1503 dev
->indicators2
= get_indicator(qemu_get_be64(f
), len
);
1506 dev
->indicators2
= NULL
;
1508 len
= qemu_get_be32(f
);
1510 dev
->summary_indicator
= get_indicator(qemu_get_be64(f
), len
);
1513 dev
->summary_indicator
= NULL
;
1515 qemu_get_be16s(f
, &vdev
->config_vector
);
1516 dev
->routes
.adapter
.ind_offset
= qemu_get_be64(f
);
1517 dev
->thinint_isc
= qemu_get_byte(f
);
1518 dev
->revision
= qemu_get_be32(f
);
1519 if (s
->thinint_active
) {
1520 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
1521 dev
->thinint_isc
, true, false,
1522 &dev
->routes
.adapter
.adapter_id
);
1528 /* This is called by virtio-bus just after the device is plugged. */
1529 static void virtio_ccw_device_plugged(DeviceState
*d
, Error
**errp
)
1531 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1532 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1533 SubchDev
*sch
= dev
->sch
;
1534 int n
= virtio_get_num_queues(vdev
);
1536 if (virtio_get_num_queues(vdev
) > VIRTIO_CCW_QUEUE_MAX
) {
1537 error_setg(errp
, "The nubmer of virtqueues %d "
1538 "exceeds ccw limit %d", n
,
1539 VIRTIO_CCW_QUEUE_MAX
);
1543 if (!kvm_eventfds_enabled()) {
1544 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
1547 sch
->id
.cu_model
= virtio_bus_get_vdev_id(&dev
->bus
);
1549 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
1553 static void virtio_ccw_device_unplugged(DeviceState
*d
)
1555 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1557 virtio_ccw_stop_ioeventfd(dev
);
1559 /**************** Virtio-ccw Bus Device Descriptions *******************/
1561 static Property virtio_ccw_net_properties
[] = {
1562 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1563 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1564 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1565 DEFINE_PROP_END_OF_LIST(),
1568 static void virtio_ccw_net_class_init(ObjectClass
*klass
, void *data
)
1570 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1571 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1573 k
->realize
= virtio_ccw_net_realize
;
1574 k
->exit
= virtio_ccw_exit
;
1575 dc
->reset
= virtio_ccw_reset
;
1576 dc
->props
= virtio_ccw_net_properties
;
1577 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1580 static const TypeInfo virtio_ccw_net
= {
1581 .name
= TYPE_VIRTIO_NET_CCW
,
1582 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1583 .instance_size
= sizeof(VirtIONetCcw
),
1584 .instance_init
= virtio_ccw_net_instance_init
,
1585 .class_init
= virtio_ccw_net_class_init
,
1588 static Property virtio_ccw_blk_properties
[] = {
1589 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1590 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1591 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1592 DEFINE_PROP_END_OF_LIST(),
1595 static void virtio_ccw_blk_class_init(ObjectClass
*klass
, void *data
)
1597 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1598 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1600 k
->realize
= virtio_ccw_blk_realize
;
1601 k
->exit
= virtio_ccw_exit
;
1602 dc
->reset
= virtio_ccw_reset
;
1603 dc
->props
= virtio_ccw_blk_properties
;
1604 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1607 static const TypeInfo virtio_ccw_blk
= {
1608 .name
= TYPE_VIRTIO_BLK_CCW
,
1609 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1610 .instance_size
= sizeof(VirtIOBlkCcw
),
1611 .instance_init
= virtio_ccw_blk_instance_init
,
1612 .class_init
= virtio_ccw_blk_class_init
,
1615 static Property virtio_ccw_serial_properties
[] = {
1616 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1617 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1618 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1619 DEFINE_PROP_END_OF_LIST(),
1622 static void virtio_ccw_serial_class_init(ObjectClass
*klass
, void *data
)
1624 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1625 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1627 k
->realize
= virtio_ccw_serial_realize
;
1628 k
->exit
= virtio_ccw_exit
;
1629 dc
->reset
= virtio_ccw_reset
;
1630 dc
->props
= virtio_ccw_serial_properties
;
1631 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1634 static const TypeInfo virtio_ccw_serial
= {
1635 .name
= TYPE_VIRTIO_SERIAL_CCW
,
1636 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1637 .instance_size
= sizeof(VirtioSerialCcw
),
1638 .instance_init
= virtio_ccw_serial_instance_init
,
1639 .class_init
= virtio_ccw_serial_class_init
,
1642 static Property virtio_ccw_balloon_properties
[] = {
1643 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1644 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1645 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1646 DEFINE_PROP_END_OF_LIST(),
1649 static void virtio_ccw_balloon_class_init(ObjectClass
*klass
, void *data
)
1651 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1652 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1654 k
->realize
= virtio_ccw_balloon_realize
;
1655 k
->exit
= virtio_ccw_exit
;
1656 dc
->reset
= virtio_ccw_reset
;
1657 dc
->props
= virtio_ccw_balloon_properties
;
1658 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1661 static const TypeInfo virtio_ccw_balloon
= {
1662 .name
= TYPE_VIRTIO_BALLOON_CCW
,
1663 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1664 .instance_size
= sizeof(VirtIOBalloonCcw
),
1665 .instance_init
= virtio_ccw_balloon_instance_init
,
1666 .class_init
= virtio_ccw_balloon_class_init
,
1669 static Property virtio_ccw_scsi_properties
[] = {
1670 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1671 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1672 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1673 DEFINE_PROP_END_OF_LIST(),
1676 static void virtio_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1678 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1679 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1681 k
->realize
= virtio_ccw_scsi_realize
;
1682 k
->exit
= virtio_ccw_exit
;
1683 dc
->reset
= virtio_ccw_reset
;
1684 dc
->props
= virtio_ccw_scsi_properties
;
1685 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1688 static const TypeInfo virtio_ccw_scsi
= {
1689 .name
= TYPE_VIRTIO_SCSI_CCW
,
1690 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1691 .instance_size
= sizeof(VirtIOSCSICcw
),
1692 .instance_init
= virtio_ccw_scsi_instance_init
,
1693 .class_init
= virtio_ccw_scsi_class_init
,
1696 #ifdef CONFIG_VHOST_SCSI
1697 static Property vhost_ccw_scsi_properties
[] = {
1698 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1699 DEFINE_PROP_END_OF_LIST(),
1702 static void vhost_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1704 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1705 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1707 k
->realize
= vhost_ccw_scsi_realize
;
1708 k
->exit
= virtio_ccw_exit
;
1709 dc
->reset
= virtio_ccw_reset
;
1710 dc
->props
= vhost_ccw_scsi_properties
;
1711 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1714 static const TypeInfo vhost_ccw_scsi
= {
1715 .name
= TYPE_VHOST_SCSI_CCW
,
1716 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1717 .instance_size
= sizeof(VHostSCSICcw
),
1718 .instance_init
= vhost_ccw_scsi_instance_init
,
1719 .class_init
= vhost_ccw_scsi_class_init
,
1723 static void virtio_ccw_rng_instance_init(Object
*obj
)
1725 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(obj
);
1727 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1729 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
),
1730 "rng", &error_abort
);
1733 static Property virtio_ccw_rng_properties
[] = {
1734 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1735 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1736 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1737 DEFINE_PROP_END_OF_LIST(),
1740 static void virtio_ccw_rng_class_init(ObjectClass
*klass
, void *data
)
1742 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1743 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1745 k
->realize
= virtio_ccw_rng_realize
;
1746 k
->exit
= virtio_ccw_exit
;
1747 dc
->reset
= virtio_ccw_reset
;
1748 dc
->props
= virtio_ccw_rng_properties
;
1749 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1752 static const TypeInfo virtio_ccw_rng
= {
1753 .name
= TYPE_VIRTIO_RNG_CCW
,
1754 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1755 .instance_size
= sizeof(VirtIORNGCcw
),
1756 .instance_init
= virtio_ccw_rng_instance_init
,
1757 .class_init
= virtio_ccw_rng_class_init
,
1760 static void virtio_ccw_busdev_realize(DeviceState
*dev
, Error
**errp
)
1762 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1764 virtio_ccw_bus_new(&_dev
->bus
, sizeof(_dev
->bus
), _dev
);
1765 virtio_ccw_device_realize(_dev
, errp
);
1768 static int virtio_ccw_busdev_exit(DeviceState
*dev
)
1770 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1771 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1773 return _info
->exit(_dev
);
1776 static void virtio_ccw_busdev_unplug(HotplugHandler
*hotplug_dev
,
1777 DeviceState
*dev
, Error
**errp
)
1779 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1780 SubchDev
*sch
= _dev
->sch
;
1782 virtio_ccw_stop_ioeventfd(_dev
);
1785 * We should arrive here only for device_del, since we don't support
1786 * direct hot(un)plug of channels, but only through virtio.
1788 assert(sch
!= NULL
);
1789 /* Subchannel is now disabled and no longer valid. */
1790 sch
->curr_status
.pmcw
.flags
&= ~(PMCW_FLAGS_MASK_ENA
|
1791 PMCW_FLAGS_MASK_DNV
);
1793 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
, 1, 0);
1795 object_unparent(OBJECT(dev
));
1798 static void virtio_ccw_device_class_init(ObjectClass
*klass
, void *data
)
1800 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1802 dc
->realize
= virtio_ccw_busdev_realize
;
1803 dc
->exit
= virtio_ccw_busdev_exit
;
1804 dc
->bus_type
= TYPE_VIRTUAL_CSS_BUS
;
1807 static const TypeInfo virtio_ccw_device_info
= {
1808 .name
= TYPE_VIRTIO_CCW_DEVICE
,
1809 .parent
= TYPE_DEVICE
,
1810 .instance_size
= sizeof(VirtioCcwDevice
),
1811 .class_init
= virtio_ccw_device_class_init
,
1812 .class_size
= sizeof(VirtIOCCWDeviceClass
),
1816 /***************** Virtual-css Bus Bridge Device ********************/
1817 /* Only required to have the virtio bus as child in the system bus */
1819 static int virtual_css_bridge_init(SysBusDevice
*dev
)
1825 static void virtual_css_bridge_class_init(ObjectClass
*klass
, void *data
)
1827 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1828 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1829 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1831 k
->init
= virtual_css_bridge_init
;
1832 hc
->unplug
= virtio_ccw_busdev_unplug
;
1833 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1836 static const TypeInfo virtual_css_bridge_info
= {
1837 .name
= "virtual-css-bridge",
1838 .parent
= TYPE_SYS_BUS_DEVICE
,
1839 .instance_size
= sizeof(SysBusDevice
),
1840 .class_init
= virtual_css_bridge_class_init
,
1841 .interfaces
= (InterfaceInfo
[]) {
1842 { TYPE_HOTPLUG_HANDLER
},
1847 /* virtio-ccw-bus */
1849 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1850 VirtioCcwDevice
*dev
)
1852 DeviceState
*qdev
= DEVICE(dev
);
1853 char virtio_bus_name
[] = "virtio-bus";
1855 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_CCW_BUS
,
1856 qdev
, virtio_bus_name
);
1859 static void virtio_ccw_bus_class_init(ObjectClass
*klass
, void *data
)
1861 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1862 BusClass
*bus_class
= BUS_CLASS(klass
);
1864 bus_class
->max_dev
= 1;
1865 k
->notify
= virtio_ccw_notify
;
1866 k
->vmstate_change
= virtio_ccw_vmstate_change
;
1867 k
->query_guest_notifiers
= virtio_ccw_query_guest_notifiers
;
1868 k
->set_host_notifier
= virtio_ccw_set_host_notifier
;
1869 k
->set_guest_notifiers
= virtio_ccw_set_guest_notifiers
;
1870 k
->save_queue
= virtio_ccw_save_queue
;
1871 k
->load_queue
= virtio_ccw_load_queue
;
1872 k
->save_config
= virtio_ccw_save_config
;
1873 k
->load_config
= virtio_ccw_load_config
;
1874 k
->device_plugged
= virtio_ccw_device_plugged
;
1875 k
->device_unplugged
= virtio_ccw_device_unplugged
;
1878 static const TypeInfo virtio_ccw_bus_info
= {
1879 .name
= TYPE_VIRTIO_CCW_BUS
,
1880 .parent
= TYPE_VIRTIO_BUS
,
1881 .instance_size
= sizeof(VirtioCcwBusState
),
1882 .class_init
= virtio_ccw_bus_class_init
,
1885 #ifdef CONFIG_VIRTFS
1886 static Property virtio_ccw_9p_properties
[] = {
1887 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1888 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1889 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1890 DEFINE_PROP_END_OF_LIST(),
1893 static void virtio_ccw_9p_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1895 V9fsCCWState
*dev
= VIRTIO_9P_CCW(ccw_dev
);
1896 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1899 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1900 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1902 error_propagate(errp
, err
);
1906 static void virtio_ccw_9p_class_init(ObjectClass
*klass
, void *data
)
1908 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1909 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1911 k
->exit
= virtio_ccw_exit
;
1912 k
->realize
= virtio_ccw_9p_realize
;
1913 dc
->reset
= virtio_ccw_reset
;
1914 dc
->props
= virtio_ccw_9p_properties
;
1915 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1918 static void virtio_ccw_9p_instance_init(Object
*obj
)
1920 V9fsCCWState
*dev
= VIRTIO_9P_CCW(obj
);
1922 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1926 static const TypeInfo virtio_ccw_9p_info
= {
1927 .name
= TYPE_VIRTIO_9P_CCW
,
1928 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1929 .instance_size
= sizeof(V9fsCCWState
),
1930 .instance_init
= virtio_ccw_9p_instance_init
,
1931 .class_init
= virtio_ccw_9p_class_init
,
1935 static void virtio_ccw_register(void)
1937 type_register_static(&virtio_ccw_bus_info
);
1938 type_register_static(&virtual_css_bus_info
);
1939 type_register_static(&virtio_ccw_device_info
);
1940 type_register_static(&virtio_ccw_serial
);
1941 type_register_static(&virtio_ccw_blk
);
1942 type_register_static(&virtio_ccw_net
);
1943 type_register_static(&virtio_ccw_balloon
);
1944 type_register_static(&virtio_ccw_scsi
);
1945 #ifdef CONFIG_VHOST_SCSI
1946 type_register_static(&vhost_ccw_scsi
);
1948 type_register_static(&virtio_ccw_rng
);
1949 type_register_static(&virtual_css_bridge_info
);
1950 #ifdef CONFIG_VIRTFS
1951 type_register_static(&virtio_ccw_9p_info
);
1955 type_init(virtio_ccw_register
)