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 if (dev
->revision
>= 1) {
471 /* Don't offer legacy features for modern devices. */
472 features
.features
= (uint32_t)
473 (vdev
->host_features
& ~VIRTIO_LEGACY_FEATURES
);
475 features
.features
= (uint32_t)vdev
->host_features
;
477 } else if ((features
.index
== 1) && (dev
->revision
>= 1)) {
479 * Only offer feature bits beyond 31 if the guest has
480 * negotiated at least revision 1.
482 features
.features
= (uint32_t)(vdev
->host_features
>> 32);
484 /* Return zeroes if the guest supports more feature bits. */
485 features
.features
= 0;
487 address_space_stl_le(&address_space_memory
, ccw
.cda
,
488 features
.features
, MEMTXATTRS_UNSPECIFIED
,
490 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
494 case CCW_CMD_WRITE_FEAT
:
496 if (ccw
.count
!= sizeof(features
)) {
500 } else if (ccw
.count
< sizeof(features
)) {
501 /* Can't execute command. */
508 features
.index
= address_space_ldub(&address_space_memory
,
510 + sizeof(features
.features
),
511 MEMTXATTRS_UNSPECIFIED
,
513 features
.features
= address_space_ldl_le(&address_space_memory
,
515 MEMTXATTRS_UNSPECIFIED
,
517 if (features
.index
== 0) {
518 virtio_set_features(vdev
,
519 (vdev
->guest_features
& 0xffffffff00000000ULL
) |
521 } else if ((features
.index
== 1) && (dev
->revision
>= 1)) {
523 * If the guest did not negotiate at least revision 1,
524 * we did not offer it any feature bits beyond 31. Such a
525 * guest passing us any bit here is therefore buggy.
527 virtio_set_features(vdev
,
528 (vdev
->guest_features
& 0x00000000ffffffffULL
) |
529 ((uint64_t)features
.features
<< 32));
532 * If the guest supports more feature bits, assert that it
533 * passes us zeroes for those we don't support.
535 if (features
.features
) {
536 fprintf(stderr
, "Guest bug: features[%i]=%x (expected 0)\n",
537 features
.index
, features
.features
);
538 /* XXX: do a unit check here? */
541 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(features
);
545 case CCW_CMD_READ_CONF
:
547 if (ccw
.count
> vdev
->config_len
) {
552 len
= MIN(ccw
.count
, vdev
->config_len
);
556 virtio_bus_get_vdev_config(&dev
->bus
, vdev
->config
);
557 /* XXX config space endianness */
558 cpu_physical_memory_write(ccw
.cda
, vdev
->config
, len
);
559 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
563 case CCW_CMD_WRITE_CONF
:
565 if (ccw
.count
> vdev
->config_len
) {
570 len
= MIN(ccw
.count
, vdev
->config_len
);
575 config
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
580 /* XXX config space endianness */
581 memcpy(vdev
->config
, config
, len
);
582 cpu_physical_memory_unmap(config
, hw_len
, 0, hw_len
);
583 virtio_bus_set_vdev_config(&dev
->bus
, vdev
->config
);
584 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
589 case CCW_CMD_WRITE_STATUS
:
591 if (ccw
.count
!= sizeof(status
)) {
595 } else if (ccw
.count
< sizeof(status
)) {
596 /* Can't execute command. */
603 status
= address_space_ldub(&address_space_memory
, ccw
.cda
,
604 MEMTXATTRS_UNSPECIFIED
, NULL
);
605 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
606 virtio_ccw_stop_ioeventfd(dev
);
608 if (virtio_set_status(vdev
, status
) == 0) {
609 if (vdev
->status
== 0) {
610 virtio_ccw_reset_virtio(dev
, vdev
);
612 if (status
& VIRTIO_CONFIG_S_DRIVER_OK
) {
613 virtio_ccw_start_ioeventfd(dev
);
615 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(status
);
618 /* Trigger a command reject. */
623 case CCW_CMD_SET_IND
:
625 if (ccw
.count
!= sizeof(indicators
)) {
629 } else if (ccw
.count
< sizeof(indicators
)) {
630 /* Can't execute command. */
634 if (sch
->thinint_active
) {
635 /* Trigger a command reject. */
642 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
643 MEMTXATTRS_UNSPECIFIED
, NULL
);
644 dev
->indicators
= get_indicator(indicators
, sizeof(uint64_t));
645 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
649 case CCW_CMD_SET_CONF_IND
:
651 if (ccw
.count
!= sizeof(indicators
)) {
655 } else if (ccw
.count
< sizeof(indicators
)) {
656 /* Can't execute command. */
663 indicators
= address_space_ldq_be(&address_space_memory
, ccw
.cda
,
664 MEMTXATTRS_UNSPECIFIED
, NULL
);
665 dev
->indicators2
= get_indicator(indicators
, sizeof(uint64_t));
666 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(indicators
);
670 case CCW_CMD_READ_VQ_CONF
:
672 if (ccw
.count
!= sizeof(vq_config
)) {
676 } else if (ccw
.count
< sizeof(vq_config
)) {
677 /* Can't execute command. */
684 vq_config
.index
= address_space_lduw_be(&address_space_memory
,
686 MEMTXATTRS_UNSPECIFIED
,
688 if (vq_config
.index
>= VIRTIO_CCW_QUEUE_MAX
) {
692 vq_config
.num_max
= virtio_queue_get_num(vdev
,
694 address_space_stw_be(&address_space_memory
,
695 ccw
.cda
+ sizeof(vq_config
.index
),
697 MEMTXATTRS_UNSPECIFIED
,
699 sch
->curr_status
.scsw
.count
= ccw
.count
- sizeof(vq_config
);
703 case CCW_CMD_SET_IND_ADAPTER
:
705 if (ccw
.count
!= sizeof(*thinint
)) {
709 } else if (ccw
.count
< sizeof(*thinint
)) {
710 /* Can't execute command. */
714 len
= sizeof(*thinint
);
718 } else if (dev
->indicators
&& !sch
->thinint_active
) {
719 /* Trigger a command reject. */
722 thinint
= cpu_physical_memory_map(ccw
.cda
, &hw_len
, 0);
726 uint64_t ind_bit
= ldq_be_p(&thinint
->ind_bit
);
729 dev
->summary_indicator
=
730 get_indicator(ldq_be_p(&thinint
->summary_indicator
),
733 get_indicator(ldq_be_p(&thinint
->device_indicator
),
735 dev
->thinint_isc
= thinint
->isc
;
736 dev
->routes
.adapter
.ind_offset
= ind_bit
;
737 dev
->routes
.adapter
.summary_offset
= 7;
738 cpu_physical_memory_unmap(thinint
, hw_len
, 0, hw_len
);
739 ret
= css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
740 dev
->thinint_isc
, true, false,
741 &dev
->routes
.adapter
.adapter_id
);
743 sch
->thinint_active
= ((dev
->indicators
!= NULL
) &&
744 (dev
->summary_indicator
!= NULL
));
745 sch
->curr_status
.scsw
.count
= ccw
.count
- len
;
750 case CCW_CMD_SET_VIRTIO_REV
:
751 len
= sizeof(revinfo
);
752 if (ccw
.count
< len
) {
761 address_space_lduw_be(&address_space_memory
, ccw
.cda
,
762 MEMTXATTRS_UNSPECIFIED
, NULL
);
764 address_space_lduw_be(&address_space_memory
,
765 ccw
.cda
+ sizeof(revinfo
.revision
),
766 MEMTXATTRS_UNSPECIFIED
, NULL
);
767 if (ccw
.count
< len
+ revinfo
.length
||
768 (check_len
&& ccw
.count
> len
+ revinfo
.length
)) {
773 * Once we start to support revisions with additional data, we'll
774 * need to fetch it here. Nothing to do for now, though.
776 if (dev
->revision
>= 0 ||
777 revinfo
.revision
> virtio_ccw_rev_max(dev
)) {
782 dev
->revision
= revinfo
.revision
;
791 static void virtio_sch_disable_cb(SubchDev
*sch
)
793 VirtioCcwDevice
*dev
= sch
->driver_data
;
798 static void virtio_ccw_device_realize(VirtioCcwDevice
*dev
, Error
**errp
)
800 unsigned int cssid
= 0;
801 unsigned int ssid
= 0;
804 bool have_devno
= false;
809 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
811 sch
= g_malloc0(sizeof(SubchDev
));
813 sch
->driver_data
= dev
;
816 dev
->indicators
= NULL
;
818 /* Initialize subchannel structure. */
819 sch
->channel_prog
= 0x0;
820 sch
->last_cmd_valid
= false;
821 sch
->thinint_active
= false;
823 * Use a device number if provided. Otherwise, fall back to subchannel
827 num
= sscanf(dev
->bus_id
, "%x.%x.%04x", &cssid
, &ssid
, &devno
);
829 if ((cssid
> MAX_CSSID
) || (ssid
> MAX_SSID
)) {
830 error_setg(errp
, "Invalid cssid or ssid: cssid %x, ssid %x",
834 /* Enforce use of virtual cssid. */
835 if (cssid
!= VIRTUAL_CSSID
) {
836 error_setg(errp
, "cssid %x not valid for virtio devices",
840 if (css_devno_used(cssid
, ssid
, devno
)) {
841 error_setg(errp
, "Device %x.%x.%04x already exists",
850 error_setg(errp
, "Malformed devno parameter '%s'", dev
->bus_id
);
855 /* Find the next free id. */
857 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
858 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
860 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
866 error_setg(errp
, "No free subchannel found for %x.%x.%04x",
870 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
873 cssid
= VIRTUAL_CSSID
;
874 for (ssid
= 0; ssid
<= MAX_SSID
; ssid
++) {
875 for (schid
= 0; schid
<= MAX_SCHID
; schid
++) {
876 if (!css_find_subch(1, cssid
, ssid
, schid
)) {
882 * If the devno is already taken, look further in this
885 while (css_devno_used(cssid
, ssid
, devno
)) {
886 if (devno
== MAX_SCHID
) {
888 } else if (devno
== schid
- 1) {
889 error_setg(errp
, "No free devno found");
896 css_subch_assign(cssid
, ssid
, schid
, devno
, sch
);
906 error_setg(errp
, "Virtual channel subsystem is full!");
909 trace_virtio_ccw_new_device(cssid
, ssid
, schid
, devno
,
913 /* Build initial schib. */
914 css_sch_build_virtual_schib(sch
, 0, VIRTIO_CCW_CHPID_TYPE
);
916 sch
->ccw_cb
= virtio_ccw_cb
;
917 sch
->disable_cb
= virtio_sch_disable_cb
;
919 /* Build senseid data. */
920 memset(&sch
->id
, 0, sizeof(SenseId
));
921 sch
->id
.reserved
= 0xff;
922 sch
->id
.cu_type
= VIRTIO_CCW_CU_TYPE
;
927 k
->realize(dev
, &err
);
930 error_propagate(errp
, err
);
931 css_subch_assign(cssid
, ssid
, schid
, devno
, NULL
);
942 static int virtio_ccw_exit(VirtioCcwDevice
*dev
)
944 SubchDev
*sch
= dev
->sch
;
947 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
950 if (dev
->indicators
) {
951 release_indicator(&dev
->routes
.adapter
, dev
->indicators
);
952 dev
->indicators
= NULL
;
957 static void virtio_ccw_net_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
959 DeviceState
*qdev
= DEVICE(ccw_dev
);
960 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(ccw_dev
);
961 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
964 virtio_net_set_netclient_name(&dev
->vdev
, qdev
->id
,
965 object_get_typename(OBJECT(qdev
)));
966 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
967 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
969 error_propagate(errp
, err
);
973 static void virtio_ccw_net_instance_init(Object
*obj
)
975 VirtIONetCcw
*dev
= VIRTIO_NET_CCW(obj
);
977 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
979 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
980 "bootindex", &error_abort
);
983 static void virtio_ccw_blk_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
985 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(ccw_dev
);
986 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
989 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
990 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
992 error_propagate(errp
, err
);
996 static void virtio_ccw_blk_instance_init(Object
*obj
)
998 VirtIOBlkCcw
*dev
= VIRTIO_BLK_CCW(obj
);
1000 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1002 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
),"iothread",
1004 object_property_add_alias(obj
, "bootindex", OBJECT(&dev
->vdev
),
1005 "bootindex", &error_abort
);
1008 static void virtio_ccw_serial_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1010 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(ccw_dev
);
1011 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1012 DeviceState
*proxy
= DEVICE(ccw_dev
);
1017 * For command line compatibility, this sets the virtio-serial-device bus
1021 bus_name
= g_strdup_printf("%s.0", proxy
->id
);
1022 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1026 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1027 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1029 error_propagate(errp
, err
);
1034 static void virtio_ccw_serial_instance_init(Object
*obj
)
1036 VirtioSerialCcw
*dev
= VIRTIO_SERIAL_CCW(obj
);
1038 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1039 TYPE_VIRTIO_SERIAL
);
1042 static void virtio_ccw_balloon_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1044 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(ccw_dev
);
1045 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1048 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1049 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1051 error_propagate(errp
, err
);
1055 static void virtio_ccw_balloon_instance_init(Object
*obj
)
1057 VirtIOBalloonCcw
*dev
= VIRTIO_BALLOON_CCW(obj
);
1059 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1060 TYPE_VIRTIO_BALLOON
);
1061 object_property_add_alias(obj
, "guest-stats", OBJECT(&dev
->vdev
),
1062 "guest-stats", &error_abort
);
1063 object_property_add_alias(obj
, "guest-stats-polling-interval",
1065 "guest-stats-polling-interval", &error_abort
);
1068 static void virtio_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1070 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(ccw_dev
);
1071 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1072 DeviceState
*qdev
= DEVICE(ccw_dev
);
1077 * For command line compatibility, this sets the virtio-scsi-device bus
1081 bus_name
= g_strdup_printf("%s.0", qdev
->id
);
1082 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev
), bus_name
);
1086 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1087 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1089 error_propagate(errp
, err
);
1093 static void virtio_ccw_scsi_instance_init(Object
*obj
)
1095 VirtIOSCSICcw
*dev
= VIRTIO_SCSI_CCW(obj
);
1097 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1099 object_property_add_alias(obj
, "iothread", OBJECT(&dev
->vdev
), "iothread",
1103 #ifdef CONFIG_VHOST_SCSI
1104 static void vhost_ccw_scsi_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1106 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(ccw_dev
);
1107 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1110 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1111 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1113 error_propagate(errp
, err
);
1117 static void vhost_ccw_scsi_instance_init(Object
*obj
)
1119 VHostSCSICcw
*dev
= VHOST_SCSI_CCW(obj
);
1121 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1126 static void virtio_ccw_rng_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1128 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(ccw_dev
);
1129 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1132 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1133 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1135 error_propagate(errp
, err
);
1139 object_property_set_link(OBJECT(dev
),
1140 OBJECT(dev
->vdev
.conf
.rng
), "rng",
1144 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1145 * be careful and test performance if you change this.
1147 static inline VirtioCcwDevice
*to_virtio_ccw_dev_fast(DeviceState
*d
)
1149 return container_of(d
, VirtioCcwDevice
, parent_obj
);
1152 static uint8_t virtio_set_ind_atomic(SubchDev
*sch
, uint64_t ind_loc
,
1155 uint8_t ind_old
, ind_new
;
1159 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
1161 error_report("%s(%x.%x.%04x): unable to access indicator",
1162 __func__
, sch
->cssid
, sch
->ssid
, sch
->schid
);
1166 ind_old
= *ind_addr
;
1167 ind_new
= ind_old
| to_be_set
;
1168 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
1169 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
1174 static void virtio_ccw_notify(DeviceState
*d
, uint16_t vector
)
1176 VirtioCcwDevice
*dev
= to_virtio_ccw_dev_fast(d
);
1177 SubchDev
*sch
= dev
->sch
;
1178 uint64_t indicators
;
1180 /* queue indicators + secondary indicators */
1181 if (vector
>= VIRTIO_CCW_QUEUE_MAX
+ 64) {
1185 if (vector
< VIRTIO_CCW_QUEUE_MAX
) {
1186 if (!dev
->indicators
) {
1189 if (sch
->thinint_active
) {
1191 * In the adapter interrupt case, indicators points to a
1192 * memory area that may be (way) larger than 64 bit and
1193 * ind_bit indicates the start of the indicators in a big
1196 uint64_t ind_bit
= dev
->routes
.adapter
.ind_offset
;
1198 virtio_set_ind_atomic(sch
, dev
->indicators
->addr
+
1199 (ind_bit
+ vector
) / 8,
1200 0x80 >> ((ind_bit
+ vector
) % 8));
1201 if (!virtio_set_ind_atomic(sch
, dev
->summary_indicator
->addr
,
1203 css_adapter_interrupt(dev
->thinint_isc
);
1206 indicators
= address_space_ldq(&address_space_memory
,
1207 dev
->indicators
->addr
,
1208 MEMTXATTRS_UNSPECIFIED
,
1210 indicators
|= 1ULL << vector
;
1211 address_space_stq(&address_space_memory
, dev
->indicators
->addr
,
1212 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1213 css_conditional_io_interrupt(sch
);
1216 if (!dev
->indicators2
) {
1220 indicators
= address_space_ldq(&address_space_memory
,
1221 dev
->indicators2
->addr
,
1222 MEMTXATTRS_UNSPECIFIED
,
1224 indicators
|= 1ULL << vector
;
1225 address_space_stq(&address_space_memory
, dev
->indicators2
->addr
,
1226 indicators
, MEMTXATTRS_UNSPECIFIED
, NULL
);
1227 css_conditional_io_interrupt(sch
);
1231 static void virtio_ccw_reset(DeviceState
*d
)
1233 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1234 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1236 virtio_ccw_reset_virtio(dev
, vdev
);
1237 css_reset_sch(dev
->sch
);
1240 static void virtio_ccw_vmstate_change(DeviceState
*d
, bool running
)
1242 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1245 virtio_ccw_start_ioeventfd(dev
);
1247 virtio_ccw_stop_ioeventfd(dev
);
1251 static bool virtio_ccw_query_guest_notifiers(DeviceState
*d
)
1253 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1255 return !!(dev
->sch
->curr_status
.pmcw
.flags
& PMCW_FLAGS_MASK_ENA
);
1258 static int virtio_ccw_set_host_notifier(DeviceState
*d
, int n
, bool assign
)
1260 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1262 /* Stop using the generic ioeventfd, we are doing eventfd handling
1263 * ourselves below */
1264 dev
->ioeventfd_disabled
= assign
;
1266 virtio_ccw_stop_ioeventfd(dev
);
1268 return virtio_ccw_set_guest2host_notifier(dev
, n
, assign
, false);
1271 static int virtio_ccw_get_mappings(VirtioCcwDevice
*dev
)
1275 if (!dev
->sch
->thinint_active
) {
1279 r
= map_indicator(&dev
->routes
.adapter
, dev
->summary_indicator
);
1283 r
= map_indicator(&dev
->routes
.adapter
, dev
->indicators
);
1287 dev
->routes
.adapter
.summary_addr
= dev
->summary_indicator
->map
;
1288 dev
->routes
.adapter
.ind_addr
= dev
->indicators
->map
;
1293 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1296 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1298 S390FLICState
*fs
= s390_get_flic();
1299 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1301 ret
= virtio_ccw_get_mappings(dev
);
1305 for (i
= 0; i
< nvqs
; i
++) {
1306 if (!virtio_queue_get_num(vdev
, i
)) {
1310 dev
->routes
.num_routes
= i
;
1311 return fsc
->add_adapter_routes(fs
, &dev
->routes
);
1314 static void virtio_ccw_release_irqroutes(VirtioCcwDevice
*dev
, int nvqs
)
1316 S390FLICState
*fs
= s390_get_flic();
1317 S390FLICStateClass
*fsc
= S390_FLIC_COMMON_GET_CLASS(fs
);
1319 fsc
->release_adapter_routes(fs
, &dev
->routes
);
1322 static int virtio_ccw_add_irqfd(VirtioCcwDevice
*dev
, int n
)
1324 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1325 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1326 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1328 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
, notifier
, NULL
,
1329 dev
->routes
.gsi
[n
]);
1332 static void virtio_ccw_remove_irqfd(VirtioCcwDevice
*dev
, int n
)
1334 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1335 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1336 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1339 ret
= kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
, notifier
,
1340 dev
->routes
.gsi
[n
]);
1344 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice
*dev
, int n
,
1345 bool assign
, bool with_irqfd
)
1347 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1348 VirtQueue
*vq
= virtio_get_queue(vdev
, n
);
1349 EventNotifier
*notifier
= virtio_queue_get_guest_notifier(vq
);
1350 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1353 int r
= event_notifier_init(notifier
, 0);
1358 virtio_queue_set_guest_notifier_fd_handler(vq
, true, with_irqfd
);
1360 r
= virtio_ccw_add_irqfd(dev
, n
);
1362 virtio_queue_set_guest_notifier_fd_handler(vq
, false,
1368 * We do not support individual masking for channel devices, so we
1369 * need to manually trigger any guest masking callbacks here.
1371 if (k
->guest_notifier_mask
) {
1372 k
->guest_notifier_mask(vdev
, n
, false);
1374 /* get lost events and re-inject */
1375 if (k
->guest_notifier_pending
&&
1376 k
->guest_notifier_pending(vdev
, n
)) {
1377 event_notifier_set(notifier
);
1380 if (k
->guest_notifier_mask
) {
1381 k
->guest_notifier_mask(vdev
, n
, true);
1384 virtio_ccw_remove_irqfd(dev
, n
);
1386 virtio_queue_set_guest_notifier_fd_handler(vq
, false, with_irqfd
);
1387 event_notifier_cleanup(notifier
);
1392 static int virtio_ccw_set_guest_notifiers(DeviceState
*d
, int nvqs
,
1395 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1396 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1397 bool with_irqfd
= dev
->sch
->thinint_active
&& kvm_irqfds_enabled();
1400 if (with_irqfd
&& assigned
) {
1401 /* irq routes need to be set up before assigning irqfds */
1402 r
= virtio_ccw_setup_irqroutes(dev
, nvqs
);
1404 goto irqroute_error
;
1407 for (n
= 0; n
< nvqs
; n
++) {
1408 if (!virtio_queue_get_num(vdev
, n
)) {
1411 r
= virtio_ccw_set_guest_notifier(dev
, n
, assigned
, with_irqfd
);
1416 if (with_irqfd
&& !assigned
) {
1417 /* release irq routes after irqfds have been released */
1418 virtio_ccw_release_irqroutes(dev
, nvqs
);
1424 virtio_ccw_set_guest_notifier(dev
, n
, !assigned
, false);
1427 if (with_irqfd
&& assigned
) {
1428 virtio_ccw_release_irqroutes(dev
, nvqs
);
1433 static void virtio_ccw_save_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1435 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1436 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1438 qemu_put_be16(f
, virtio_queue_vector(vdev
, n
));
1441 static int virtio_ccw_load_queue(DeviceState
*d
, int n
, QEMUFile
*f
)
1443 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1444 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1447 qemu_get_be16s(f
, &vector
);
1448 virtio_queue_set_vector(vdev
, n
, vector
);
1453 static void virtio_ccw_save_config(DeviceState
*d
, QEMUFile
*f
)
1455 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1456 SubchDev
*s
= dev
->sch
;
1457 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1459 subch_device_save(s
, f
);
1460 if (dev
->indicators
!= NULL
) {
1461 qemu_put_be32(f
, dev
->indicators
->len
);
1462 qemu_put_be64(f
, dev
->indicators
->addr
);
1464 qemu_put_be32(f
, 0);
1465 qemu_put_be64(f
, 0UL);
1467 if (dev
->indicators2
!= NULL
) {
1468 qemu_put_be32(f
, dev
->indicators2
->len
);
1469 qemu_put_be64(f
, dev
->indicators2
->addr
);
1471 qemu_put_be32(f
, 0);
1472 qemu_put_be64(f
, 0UL);
1474 if (dev
->summary_indicator
!= NULL
) {
1475 qemu_put_be32(f
, dev
->summary_indicator
->len
);
1476 qemu_put_be64(f
, dev
->summary_indicator
->addr
);
1478 qemu_put_be32(f
, 0);
1479 qemu_put_be64(f
, 0UL);
1481 qemu_put_be16(f
, vdev
->config_vector
);
1482 qemu_put_be64(f
, dev
->routes
.adapter
.ind_offset
);
1483 qemu_put_byte(f
, dev
->thinint_isc
);
1484 qemu_put_be32(f
, dev
->revision
);
1487 static int virtio_ccw_load_config(DeviceState
*d
, QEMUFile
*f
)
1489 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1490 SubchDev
*s
= dev
->sch
;
1491 VirtIODevice
*vdev
= virtio_ccw_get_vdev(s
);
1494 s
->driver_data
= dev
;
1495 subch_device_load(s
, f
);
1496 len
= qemu_get_be32(f
);
1498 dev
->indicators
= get_indicator(qemu_get_be64(f
), len
);
1501 dev
->indicators
= NULL
;
1503 len
= qemu_get_be32(f
);
1505 dev
->indicators2
= get_indicator(qemu_get_be64(f
), len
);
1508 dev
->indicators2
= NULL
;
1510 len
= qemu_get_be32(f
);
1512 dev
->summary_indicator
= get_indicator(qemu_get_be64(f
), len
);
1515 dev
->summary_indicator
= NULL
;
1517 qemu_get_be16s(f
, &vdev
->config_vector
);
1518 dev
->routes
.adapter
.ind_offset
= qemu_get_be64(f
);
1519 dev
->thinint_isc
= qemu_get_byte(f
);
1520 dev
->revision
= qemu_get_be32(f
);
1521 if (s
->thinint_active
) {
1522 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO
,
1523 dev
->thinint_isc
, true, false,
1524 &dev
->routes
.adapter
.adapter_id
);
1530 /* This is called by virtio-bus just after the device is plugged. */
1531 static void virtio_ccw_device_plugged(DeviceState
*d
, Error
**errp
)
1533 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1534 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1535 SubchDev
*sch
= dev
->sch
;
1536 int n
= virtio_get_num_queues(vdev
);
1538 if (virtio_get_num_queues(vdev
) > VIRTIO_CCW_QUEUE_MAX
) {
1539 error_setg(errp
, "The nubmer of virtqueues %d "
1540 "exceeds ccw limit %d", n
,
1541 VIRTIO_CCW_QUEUE_MAX
);
1545 if (!kvm_eventfds_enabled()) {
1546 dev
->flags
&= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD
;
1549 sch
->id
.cu_model
= virtio_bus_get_vdev_id(&dev
->bus
);
1551 if (dev
->max_rev
>= 1) {
1552 virtio_add_feature(&vdev
->host_features
, VIRTIO_F_VERSION_1
);
1555 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
1559 static void virtio_ccw_post_plugged(DeviceState
*d
, Error
**errp
)
1561 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1562 VirtIODevice
*vdev
= virtio_bus_get_device(&dev
->bus
);
1564 if (!virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1565 /* A backend didn't support modern virtio. */
1570 static void virtio_ccw_device_unplugged(DeviceState
*d
)
1572 VirtioCcwDevice
*dev
= VIRTIO_CCW_DEVICE(d
);
1574 virtio_ccw_stop_ioeventfd(dev
);
1576 /**************** Virtio-ccw Bus Device Descriptions *******************/
1578 static Property virtio_ccw_net_properties
[] = {
1579 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1580 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1581 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1582 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1583 VIRTIO_CCW_MAX_REV
),
1584 DEFINE_PROP_END_OF_LIST(),
1587 static void virtio_ccw_net_class_init(ObjectClass
*klass
, void *data
)
1589 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1590 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1592 k
->realize
= virtio_ccw_net_realize
;
1593 k
->exit
= virtio_ccw_exit
;
1594 dc
->reset
= virtio_ccw_reset
;
1595 dc
->props
= virtio_ccw_net_properties
;
1596 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1599 static const TypeInfo virtio_ccw_net
= {
1600 .name
= TYPE_VIRTIO_NET_CCW
,
1601 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1602 .instance_size
= sizeof(VirtIONetCcw
),
1603 .instance_init
= virtio_ccw_net_instance_init
,
1604 .class_init
= virtio_ccw_net_class_init
,
1607 static Property virtio_ccw_blk_properties
[] = {
1608 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1609 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1610 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1611 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1612 VIRTIO_CCW_MAX_REV
),
1613 DEFINE_PROP_END_OF_LIST(),
1616 static void virtio_ccw_blk_class_init(ObjectClass
*klass
, void *data
)
1618 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1619 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1621 k
->realize
= virtio_ccw_blk_realize
;
1622 k
->exit
= virtio_ccw_exit
;
1623 dc
->reset
= virtio_ccw_reset
;
1624 dc
->props
= virtio_ccw_blk_properties
;
1625 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1628 static const TypeInfo virtio_ccw_blk
= {
1629 .name
= TYPE_VIRTIO_BLK_CCW
,
1630 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1631 .instance_size
= sizeof(VirtIOBlkCcw
),
1632 .instance_init
= virtio_ccw_blk_instance_init
,
1633 .class_init
= virtio_ccw_blk_class_init
,
1636 static Property virtio_ccw_serial_properties
[] = {
1637 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1638 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1639 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1640 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1641 VIRTIO_CCW_MAX_REV
),
1642 DEFINE_PROP_END_OF_LIST(),
1645 static void virtio_ccw_serial_class_init(ObjectClass
*klass
, void *data
)
1647 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1648 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1650 k
->realize
= virtio_ccw_serial_realize
;
1651 k
->exit
= virtio_ccw_exit
;
1652 dc
->reset
= virtio_ccw_reset
;
1653 dc
->props
= virtio_ccw_serial_properties
;
1654 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1657 static const TypeInfo virtio_ccw_serial
= {
1658 .name
= TYPE_VIRTIO_SERIAL_CCW
,
1659 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1660 .instance_size
= sizeof(VirtioSerialCcw
),
1661 .instance_init
= virtio_ccw_serial_instance_init
,
1662 .class_init
= virtio_ccw_serial_class_init
,
1665 static Property virtio_ccw_balloon_properties
[] = {
1666 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1667 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1668 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1669 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1670 VIRTIO_CCW_MAX_REV
),
1671 DEFINE_PROP_END_OF_LIST(),
1674 static void virtio_ccw_balloon_class_init(ObjectClass
*klass
, void *data
)
1676 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1677 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1679 k
->realize
= virtio_ccw_balloon_realize
;
1680 k
->exit
= virtio_ccw_exit
;
1681 dc
->reset
= virtio_ccw_reset
;
1682 dc
->props
= virtio_ccw_balloon_properties
;
1683 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1686 static const TypeInfo virtio_ccw_balloon
= {
1687 .name
= TYPE_VIRTIO_BALLOON_CCW
,
1688 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1689 .instance_size
= sizeof(VirtIOBalloonCcw
),
1690 .instance_init
= virtio_ccw_balloon_instance_init
,
1691 .class_init
= virtio_ccw_balloon_class_init
,
1694 static Property virtio_ccw_scsi_properties
[] = {
1695 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1696 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1697 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1698 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1699 VIRTIO_CCW_MAX_REV
),
1700 DEFINE_PROP_END_OF_LIST(),
1703 static void virtio_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1705 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1706 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1708 k
->realize
= virtio_ccw_scsi_realize
;
1709 k
->exit
= virtio_ccw_exit
;
1710 dc
->reset
= virtio_ccw_reset
;
1711 dc
->props
= virtio_ccw_scsi_properties
;
1712 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1715 static const TypeInfo virtio_ccw_scsi
= {
1716 .name
= TYPE_VIRTIO_SCSI_CCW
,
1717 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1718 .instance_size
= sizeof(VirtIOSCSICcw
),
1719 .instance_init
= virtio_ccw_scsi_instance_init
,
1720 .class_init
= virtio_ccw_scsi_class_init
,
1723 #ifdef CONFIG_VHOST_SCSI
1724 static Property vhost_ccw_scsi_properties
[] = {
1725 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1726 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1727 VIRTIO_CCW_MAX_REV
),
1728 DEFINE_PROP_END_OF_LIST(),
1731 static void vhost_ccw_scsi_class_init(ObjectClass
*klass
, void *data
)
1733 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1734 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1736 k
->realize
= vhost_ccw_scsi_realize
;
1737 k
->exit
= virtio_ccw_exit
;
1738 dc
->reset
= virtio_ccw_reset
;
1739 dc
->props
= vhost_ccw_scsi_properties
;
1740 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1743 static const TypeInfo vhost_ccw_scsi
= {
1744 .name
= TYPE_VHOST_SCSI_CCW
,
1745 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1746 .instance_size
= sizeof(VHostSCSICcw
),
1747 .instance_init
= vhost_ccw_scsi_instance_init
,
1748 .class_init
= vhost_ccw_scsi_class_init
,
1752 static void virtio_ccw_rng_instance_init(Object
*obj
)
1754 VirtIORNGCcw
*dev
= VIRTIO_RNG_CCW(obj
);
1756 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1758 object_property_add_alias(obj
, "rng", OBJECT(&dev
->vdev
),
1759 "rng", &error_abort
);
1762 static Property virtio_ccw_rng_properties
[] = {
1763 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1764 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1765 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1766 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1767 VIRTIO_CCW_MAX_REV
),
1768 DEFINE_PROP_END_OF_LIST(),
1771 static void virtio_ccw_rng_class_init(ObjectClass
*klass
, void *data
)
1773 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1774 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1776 k
->realize
= virtio_ccw_rng_realize
;
1777 k
->exit
= virtio_ccw_exit
;
1778 dc
->reset
= virtio_ccw_reset
;
1779 dc
->props
= virtio_ccw_rng_properties
;
1780 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1783 static const TypeInfo virtio_ccw_rng
= {
1784 .name
= TYPE_VIRTIO_RNG_CCW
,
1785 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1786 .instance_size
= sizeof(VirtIORNGCcw
),
1787 .instance_init
= virtio_ccw_rng_instance_init
,
1788 .class_init
= virtio_ccw_rng_class_init
,
1791 static void virtio_ccw_busdev_realize(DeviceState
*dev
, Error
**errp
)
1793 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1795 virtio_ccw_bus_new(&_dev
->bus
, sizeof(_dev
->bus
), _dev
);
1796 virtio_ccw_device_realize(_dev
, errp
);
1799 static int virtio_ccw_busdev_exit(DeviceState
*dev
)
1801 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1802 VirtIOCCWDeviceClass
*_info
= VIRTIO_CCW_DEVICE_GET_CLASS(dev
);
1804 return _info
->exit(_dev
);
1807 static void virtio_ccw_busdev_unplug(HotplugHandler
*hotplug_dev
,
1808 DeviceState
*dev
, Error
**errp
)
1810 VirtioCcwDevice
*_dev
= (VirtioCcwDevice
*)dev
;
1811 SubchDev
*sch
= _dev
->sch
;
1813 virtio_ccw_stop_ioeventfd(_dev
);
1816 * We should arrive here only for device_del, since we don't support
1817 * direct hot(un)plug of channels, but only through virtio.
1819 assert(sch
!= NULL
);
1820 /* Subchannel is now disabled and no longer valid. */
1821 sch
->curr_status
.pmcw
.flags
&= ~(PMCW_FLAGS_MASK_ENA
|
1822 PMCW_FLAGS_MASK_DNV
);
1824 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
, 1, 0);
1826 object_unparent(OBJECT(dev
));
1829 static void virtio_ccw_device_class_init(ObjectClass
*klass
, void *data
)
1831 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1833 dc
->realize
= virtio_ccw_busdev_realize
;
1834 dc
->exit
= virtio_ccw_busdev_exit
;
1835 dc
->bus_type
= TYPE_VIRTUAL_CSS_BUS
;
1838 static const TypeInfo virtio_ccw_device_info
= {
1839 .name
= TYPE_VIRTIO_CCW_DEVICE
,
1840 .parent
= TYPE_DEVICE
,
1841 .instance_size
= sizeof(VirtioCcwDevice
),
1842 .class_init
= virtio_ccw_device_class_init
,
1843 .class_size
= sizeof(VirtIOCCWDeviceClass
),
1847 /***************** Virtual-css Bus Bridge Device ********************/
1848 /* Only required to have the virtio bus as child in the system bus */
1850 static int virtual_css_bridge_init(SysBusDevice
*dev
)
1856 static void virtual_css_bridge_class_init(ObjectClass
*klass
, void *data
)
1858 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1859 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1860 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1862 k
->init
= virtual_css_bridge_init
;
1863 hc
->unplug
= virtio_ccw_busdev_unplug
;
1864 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1867 static const TypeInfo virtual_css_bridge_info
= {
1868 .name
= "virtual-css-bridge",
1869 .parent
= TYPE_SYS_BUS_DEVICE
,
1870 .instance_size
= sizeof(SysBusDevice
),
1871 .class_init
= virtual_css_bridge_class_init
,
1872 .interfaces
= (InterfaceInfo
[]) {
1873 { TYPE_HOTPLUG_HANDLER
},
1878 /* virtio-ccw-bus */
1880 static void virtio_ccw_bus_new(VirtioBusState
*bus
, size_t bus_size
,
1881 VirtioCcwDevice
*dev
)
1883 DeviceState
*qdev
= DEVICE(dev
);
1884 char virtio_bus_name
[] = "virtio-bus";
1886 qbus_create_inplace(bus
, bus_size
, TYPE_VIRTIO_CCW_BUS
,
1887 qdev
, virtio_bus_name
);
1890 static void virtio_ccw_bus_class_init(ObjectClass
*klass
, void *data
)
1892 VirtioBusClass
*k
= VIRTIO_BUS_CLASS(klass
);
1893 BusClass
*bus_class
= BUS_CLASS(klass
);
1895 bus_class
->max_dev
= 1;
1896 k
->notify
= virtio_ccw_notify
;
1897 k
->vmstate_change
= virtio_ccw_vmstate_change
;
1898 k
->query_guest_notifiers
= virtio_ccw_query_guest_notifiers
;
1899 k
->set_host_notifier
= virtio_ccw_set_host_notifier
;
1900 k
->set_guest_notifiers
= virtio_ccw_set_guest_notifiers
;
1901 k
->save_queue
= virtio_ccw_save_queue
;
1902 k
->load_queue
= virtio_ccw_load_queue
;
1903 k
->save_config
= virtio_ccw_save_config
;
1904 k
->load_config
= virtio_ccw_load_config
;
1905 k
->device_plugged
= virtio_ccw_device_plugged
;
1906 k
->post_plugged
= virtio_ccw_post_plugged
;
1907 k
->device_unplugged
= virtio_ccw_device_unplugged
;
1910 static const TypeInfo virtio_ccw_bus_info
= {
1911 .name
= TYPE_VIRTIO_CCW_BUS
,
1912 .parent
= TYPE_VIRTIO_BUS
,
1913 .instance_size
= sizeof(VirtioCcwBusState
),
1914 .class_init
= virtio_ccw_bus_class_init
,
1917 #ifdef CONFIG_VIRTFS
1918 static Property virtio_ccw_9p_properties
[] = {
1919 DEFINE_PROP_STRING("devno", VirtioCcwDevice
, bus_id
),
1920 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice
, flags
,
1921 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT
, true),
1922 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice
, max_rev
,
1923 VIRTIO_CCW_MAX_REV
),
1924 DEFINE_PROP_END_OF_LIST(),
1927 static void virtio_ccw_9p_realize(VirtioCcwDevice
*ccw_dev
, Error
**errp
)
1929 V9fsCCWState
*dev
= VIRTIO_9P_CCW(ccw_dev
);
1930 DeviceState
*vdev
= DEVICE(&dev
->vdev
);
1933 qdev_set_parent_bus(vdev
, BUS(&ccw_dev
->bus
));
1934 object_property_set_bool(OBJECT(vdev
), true, "realized", &err
);
1936 error_propagate(errp
, err
);
1940 static void virtio_ccw_9p_class_init(ObjectClass
*klass
, void *data
)
1942 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1943 VirtIOCCWDeviceClass
*k
= VIRTIO_CCW_DEVICE_CLASS(klass
);
1945 k
->exit
= virtio_ccw_exit
;
1946 k
->realize
= virtio_ccw_9p_realize
;
1947 dc
->reset
= virtio_ccw_reset
;
1948 dc
->props
= virtio_ccw_9p_properties
;
1949 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
1952 static void virtio_ccw_9p_instance_init(Object
*obj
)
1954 V9fsCCWState
*dev
= VIRTIO_9P_CCW(obj
);
1956 virtio_instance_init_common(obj
, &dev
->vdev
, sizeof(dev
->vdev
),
1960 static const TypeInfo virtio_ccw_9p_info
= {
1961 .name
= TYPE_VIRTIO_9P_CCW
,
1962 .parent
= TYPE_VIRTIO_CCW_DEVICE
,
1963 .instance_size
= sizeof(V9fsCCWState
),
1964 .instance_init
= virtio_ccw_9p_instance_init
,
1965 .class_init
= virtio_ccw_9p_class_init
,
1969 static void virtio_ccw_register(void)
1971 type_register_static(&virtio_ccw_bus_info
);
1972 type_register_static(&virtual_css_bus_info
);
1973 type_register_static(&virtio_ccw_device_info
);
1974 type_register_static(&virtio_ccw_serial
);
1975 type_register_static(&virtio_ccw_blk
);
1976 type_register_static(&virtio_ccw_net
);
1977 type_register_static(&virtio_ccw_balloon
);
1978 type_register_static(&virtio_ccw_scsi
);
1979 #ifdef CONFIG_VHOST_SCSI
1980 type_register_static(&vhost_ccw_scsi
);
1982 type_register_static(&virtio_ccw_rng
);
1983 type_register_static(&virtual_css_bridge_info
);
1984 #ifdef CONFIG_VIRTFS
1985 type_register_static(&virtio_ccw_9p_info
);
1989 type_init(virtio_ccw_register
)