xref: /qemu/hw/s390x/virtio-ccw.c (revision 9277d81f)
1 /*
2  * virtio ccw target implementation
3  *
4  * Copyright 2012,2015 IBM Corp.
5  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6  *            Pierre Morel <pmorel@linux.vnet.ibm.com>
7  *
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
10  * directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "hw/hw.h"
16 #include "sysemu/sysemu.h"
17 #include "sysemu/kvm.h"
18 #include "net/net.h"
19 #include "hw/virtio/virtio.h"
20 #include "hw/virtio/virtio-serial.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "hw/sysbus.h"
23 #include "qemu/bitops.h"
24 #include "qemu/error-report.h"
25 #include "hw/virtio/virtio-access.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "hw/s390x/adapter.h"
28 #include "hw/s390x/s390_flic.h"
29 
30 #include "hw/s390x/ioinst.h"
31 #include "hw/s390x/css.h"
32 #include "virtio-ccw.h"
33 #include "trace.h"
34 #include "hw/s390x/css-bridge.h"
35 #include "hw/s390x/s390-virtio-ccw.h"
36 
37 #define NR_CLASSIC_INDICATOR_BITS 64
38 
39 static int virtio_ccw_dev_post_load(void *opaque, int version_id)
40 {
41     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(opaque);
42     CcwDevice *ccw_dev = CCW_DEVICE(dev);
43     CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
44 
45     ccw_dev->sch->driver_data = dev;
46     if (ccw_dev->sch->thinint_active) {
47         dev->routes.adapter.adapter_id = css_get_adapter_id(
48                                          CSS_IO_ADAPTER_VIRTIO,
49                                          dev->thinint_isc);
50     }
51     /* Re-fill subch_id after loading the subchannel states.*/
52     if (ck->refill_ids) {
53         ck->refill_ids(ccw_dev);
54     }
55     return 0;
56 }
57 
58 typedef struct VirtioCcwDeviceTmp {
59     VirtioCcwDevice *parent;
60     uint16_t config_vector;
61 } VirtioCcwDeviceTmp;
62 
63 static int virtio_ccw_dev_tmp_pre_save(void *opaque)
64 {
65     VirtioCcwDeviceTmp *tmp = opaque;
66     VirtioCcwDevice *dev = tmp->parent;
67     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
68 
69     tmp->config_vector = vdev->config_vector;
70 
71     return 0;
72 }
73 
74 static int virtio_ccw_dev_tmp_post_load(void *opaque, int version_id)
75 {
76     VirtioCcwDeviceTmp *tmp = opaque;
77     VirtioCcwDevice *dev = tmp->parent;
78     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
79 
80     vdev->config_vector = tmp->config_vector;
81     return 0;
82 }
83 
84 const VMStateDescription vmstate_virtio_ccw_dev_tmp = {
85     .name = "s390_virtio_ccw_dev_tmp",
86     .pre_save = virtio_ccw_dev_tmp_pre_save,
87     .post_load = virtio_ccw_dev_tmp_post_load,
88     .fields = (VMStateField[]) {
89         VMSTATE_UINT16(config_vector, VirtioCcwDeviceTmp),
90         VMSTATE_END_OF_LIST()
91     }
92 };
93 
94 const VMStateDescription vmstate_virtio_ccw_dev = {
95     .name = "s390_virtio_ccw_dev",
96     .version_id = 1,
97     .minimum_version_id = 1,
98     .post_load = virtio_ccw_dev_post_load,
99     .fields = (VMStateField[]) {
100         VMSTATE_CCW_DEVICE(parent_obj, VirtioCcwDevice),
101         VMSTATE_PTR_TO_IND_ADDR(indicators, VirtioCcwDevice),
102         VMSTATE_PTR_TO_IND_ADDR(indicators2, VirtioCcwDevice),
103         VMSTATE_PTR_TO_IND_ADDR(summary_indicator, VirtioCcwDevice),
104         /*
105          * Ugly hack because VirtIODevice does not migrate itself.
106          * This also makes legacy via vmstate_save_state possible.
107          */
108         VMSTATE_WITH_TMP(VirtioCcwDevice, VirtioCcwDeviceTmp,
109                          vmstate_virtio_ccw_dev_tmp),
110         VMSTATE_STRUCT(routes, VirtioCcwDevice, 1, vmstate_adapter_routes,
111                        AdapterRoutes),
112         VMSTATE_UINT8(thinint_isc, VirtioCcwDevice),
113         VMSTATE_INT32(revision, VirtioCcwDevice),
114         VMSTATE_END_OF_LIST()
115     }
116 };
117 
118 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
119                                VirtioCcwDevice *dev);
120 
121 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
122 {
123     VirtIODevice *vdev = NULL;
124     VirtioCcwDevice *dev = sch->driver_data;
125 
126     if (dev) {
127         vdev = virtio_bus_get_device(&dev->bus);
128     }
129     return vdev;
130 }
131 
132 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
133 {
134     virtio_bus_start_ioeventfd(&dev->bus);
135 }
136 
137 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
138 {
139     virtio_bus_stop_ioeventfd(&dev->bus);
140 }
141 
142 static bool virtio_ccw_ioeventfd_enabled(DeviceState *d)
143 {
144     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
145 
146     return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0;
147 }
148 
149 static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
150                                        int n, bool assign)
151 {
152     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
153     CcwDevice *ccw_dev = CCW_DEVICE(dev);
154     SubchDev *sch = ccw_dev->sch;
155     uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;
156 
157     return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
158 }
159 
160 /* Communication blocks used by several channel commands. */
161 typedef struct VqInfoBlockLegacy {
162     uint64_t queue;
163     uint32_t align;
164     uint16_t index;
165     uint16_t num;
166 } QEMU_PACKED VqInfoBlockLegacy;
167 
168 typedef struct VqInfoBlock {
169     uint64_t desc;
170     uint32_t res0;
171     uint16_t index;
172     uint16_t num;
173     uint64_t avail;
174     uint64_t used;
175 } QEMU_PACKED VqInfoBlock;
176 
177 typedef struct VqConfigBlock {
178     uint16_t index;
179     uint16_t num_max;
180 } QEMU_PACKED VqConfigBlock;
181 
182 typedef struct VirtioFeatDesc {
183     uint32_t features;
184     uint8_t index;
185 } QEMU_PACKED VirtioFeatDesc;
186 
187 typedef struct VirtioThinintInfo {
188     hwaddr summary_indicator;
189     hwaddr device_indicator;
190     uint64_t ind_bit;
191     uint8_t isc;
192 } QEMU_PACKED VirtioThinintInfo;
193 
194 typedef struct VirtioRevInfo {
195     uint16_t revision;
196     uint16_t length;
197     uint8_t data[0];
198 } QEMU_PACKED VirtioRevInfo;
199 
200 /* Specify where the virtqueues for the subchannel are in guest memory. */
201 static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info,
202                               VqInfoBlockLegacy *linfo)
203 {
204     VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
205     uint16_t index = info ? info->index : linfo->index;
206     uint16_t num = info ? info->num : linfo->num;
207     uint64_t desc = info ? info->desc : linfo->queue;
208 
209     if (index >= VIRTIO_QUEUE_MAX) {
210         return -EINVAL;
211     }
212 
213     /* Current code in virtio.c relies on 4K alignment. */
214     if (linfo && desc && (linfo->align != 4096)) {
215         return -EINVAL;
216     }
217 
218     if (!vdev) {
219         return -EINVAL;
220     }
221 
222     if (info) {
223         virtio_queue_set_rings(vdev, index, desc, info->avail, info->used);
224     } else {
225         virtio_queue_set_addr(vdev, index, desc);
226     }
227     if (!desc) {
228         virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR);
229     } else {
230         if (info) {
231             /* virtio-1 allows changing the ring size. */
232             if (virtio_queue_get_max_num(vdev, index) < num) {
233                 /* Fail if we exceed the maximum number. */
234                 return -EINVAL;
235             }
236             virtio_queue_set_num(vdev, index, num);
237         } else if (virtio_queue_get_num(vdev, index) > num) {
238             /* Fail if we don't have a big enough queue. */
239             return -EINVAL;
240         }
241         /* We ignore possible increased num for legacy for compatibility. */
242         virtio_queue_set_vector(vdev, index, index);
243     }
244     /* tell notify handler in case of config change */
245     vdev->config_vector = VIRTIO_QUEUE_MAX;
246     return 0;
247 }
248 
249 static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
250 {
251     CcwDevice *ccw_dev = CCW_DEVICE(dev);
252 
253     virtio_ccw_stop_ioeventfd(dev);
254     virtio_reset(vdev);
255     if (dev->indicators) {
256         release_indicator(&dev->routes.adapter, dev->indicators);
257         dev->indicators = NULL;
258     }
259     if (dev->indicators2) {
260         release_indicator(&dev->routes.adapter, dev->indicators2);
261         dev->indicators2 = NULL;
262     }
263     if (dev->summary_indicator) {
264         release_indicator(&dev->routes.adapter, dev->summary_indicator);
265         dev->summary_indicator = NULL;
266     }
267     ccw_dev->sch->thinint_active = false;
268 }
269 
270 static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
271                                     bool is_legacy)
272 {
273     int ret;
274     VqInfoBlock info;
275     VqInfoBlockLegacy linfo;
276     size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info);
277 
278     if (check_len) {
279         if (ccw.count != info_len) {
280             return -EINVAL;
281         }
282     } else if (ccw.count < info_len) {
283         /* Can't execute command. */
284         return -EINVAL;
285     }
286     if (!ccw.cda) {
287         return -EFAULT;
288     }
289     if (is_legacy) {
290         ccw_dstream_read(&sch->cds, linfo);
291         be64_to_cpus(&linfo.queue);
292         be32_to_cpus(&linfo.align);
293         be16_to_cpus(&linfo.index);
294         be16_to_cpus(&linfo.num);
295         ret = virtio_ccw_set_vqs(sch, NULL, &linfo);
296     } else {
297         ccw_dstream_read(&sch->cds, info);
298         be64_to_cpus(&info.desc);
299         be16_to_cpus(&info.index);
300         be16_to_cpus(&info.num);
301         be64_to_cpus(&info.avail);
302         be64_to_cpus(&info.used);
303         ret = virtio_ccw_set_vqs(sch, &info, NULL);
304     }
305     sch->curr_status.scsw.count = 0;
306     return ret;
307 }
308 
309 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
310 {
311     int ret;
312     VirtioRevInfo revinfo;
313     uint8_t status;
314     VirtioFeatDesc features;
315     hwaddr indicators;
316     VqConfigBlock vq_config;
317     VirtioCcwDevice *dev = sch->driver_data;
318     VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
319     bool check_len;
320     int len;
321     VirtioThinintInfo thinint;
322 
323     if (!dev) {
324         return -EINVAL;
325     }
326 
327     trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
328                                    ccw.cmd_code);
329     check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
330 
331     if (dev->force_revision_1 && dev->revision < 0 &&
332         ccw.cmd_code != CCW_CMD_SET_VIRTIO_REV) {
333         /*
334          * virtio-1 drivers must start with negotiating to a revision >= 1,
335          * so post a command reject for all other commands
336          */
337         return -ENOSYS;
338     }
339 
340     /* Look at the command. */
341     switch (ccw.cmd_code) {
342     case CCW_CMD_SET_VQ:
343         ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1);
344         break;
345     case CCW_CMD_VDEV_RESET:
346         virtio_ccw_reset_virtio(dev, vdev);
347         ret = 0;
348         break;
349     case CCW_CMD_READ_FEAT:
350         if (check_len) {
351             if (ccw.count != sizeof(features)) {
352                 ret = -EINVAL;
353                 break;
354             }
355         } else if (ccw.count < sizeof(features)) {
356             /* Can't execute command. */
357             ret = -EINVAL;
358             break;
359         }
360         if (!ccw.cda) {
361             ret = -EFAULT;
362         } else {
363             VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
364 
365             ccw_dstream_advance(&sch->cds, sizeof(features.features));
366             ccw_dstream_read(&sch->cds, features.index);
367             if (features.index == 0) {
368                 if (dev->revision >= 1) {
369                     /* Don't offer legacy features for modern devices. */
370                     features.features = (uint32_t)
371                         (vdev->host_features & ~vdc->legacy_features);
372                 } else {
373                     features.features = (uint32_t)vdev->host_features;
374                 }
375             } else if ((features.index == 1) && (dev->revision >= 1)) {
376                 /*
377                  * Only offer feature bits beyond 31 if the guest has
378                  * negotiated at least revision 1.
379                  */
380                 features.features = (uint32_t)(vdev->host_features >> 32);
381             } else {
382                 /* Return zeroes if the guest supports more feature bits. */
383                 features.features = 0;
384             }
385             ccw_dstream_rewind(&sch->cds);
386             cpu_to_le32s(&features.features);
387             ccw_dstream_write(&sch->cds, features.features);
388             sch->curr_status.scsw.count = ccw.count - sizeof(features);
389             ret = 0;
390         }
391         break;
392     case CCW_CMD_WRITE_FEAT:
393         if (check_len) {
394             if (ccw.count != sizeof(features)) {
395                 ret = -EINVAL;
396                 break;
397             }
398         } else if (ccw.count < sizeof(features)) {
399             /* Can't execute command. */
400             ret = -EINVAL;
401             break;
402         }
403         if (!ccw.cda) {
404             ret = -EFAULT;
405         } else {
406             ccw_dstream_read(&sch->cds, features);
407             le32_to_cpus(&features.features);
408             if (features.index == 0) {
409                 virtio_set_features(vdev,
410                                     (vdev->guest_features & 0xffffffff00000000ULL) |
411                                     features.features);
412             } else if ((features.index == 1) && (dev->revision >= 1)) {
413                 /*
414                  * If the guest did not negotiate at least revision 1,
415                  * we did not offer it any feature bits beyond 31. Such a
416                  * guest passing us any bit here is therefore buggy.
417                  */
418                 virtio_set_features(vdev,
419                                     (vdev->guest_features & 0x00000000ffffffffULL) |
420                                     ((uint64_t)features.features << 32));
421             } else {
422                 /*
423                  * If the guest supports more feature bits, assert that it
424                  * passes us zeroes for those we don't support.
425                  */
426                 if (features.features) {
427                     qemu_log_mask(LOG_GUEST_ERROR,
428                                   "Guest bug: features[%i]=%x (expected 0)",
429                                   features.index, features.features);
430                     /* XXX: do a unit check here? */
431                 }
432             }
433             sch->curr_status.scsw.count = ccw.count - sizeof(features);
434             ret = 0;
435         }
436         break;
437     case CCW_CMD_READ_CONF:
438         if (check_len) {
439             if (ccw.count > vdev->config_len) {
440                 ret = -EINVAL;
441                 break;
442             }
443         }
444         len = MIN(ccw.count, vdev->config_len);
445         if (!ccw.cda) {
446             ret = -EFAULT;
447         } else {
448             virtio_bus_get_vdev_config(&dev->bus, vdev->config);
449             ccw_dstream_write_buf(&sch->cds, vdev->config, len);
450             sch->curr_status.scsw.count = ccw.count - len;
451             ret = 0;
452         }
453         break;
454     case CCW_CMD_WRITE_CONF:
455         if (check_len) {
456             if (ccw.count > vdev->config_len) {
457                 ret = -EINVAL;
458                 break;
459             }
460         }
461         len = MIN(ccw.count, vdev->config_len);
462         if (!ccw.cda) {
463             ret = -EFAULT;
464         } else {
465             ret = ccw_dstream_read_buf(&sch->cds, vdev->config, len);
466             if (!ret) {
467                 virtio_bus_set_vdev_config(&dev->bus, vdev->config);
468                 sch->curr_status.scsw.count = ccw.count - len;
469             }
470         }
471         break;
472     case CCW_CMD_READ_STATUS:
473         if (check_len) {
474             if (ccw.count != sizeof(status)) {
475                 ret = -EINVAL;
476                 break;
477             }
478         } else if (ccw.count < sizeof(status)) {
479             /* Can't execute command. */
480             ret = -EINVAL;
481             break;
482         }
483         if (!ccw.cda) {
484             ret = -EFAULT;
485         } else {
486             address_space_stb(&address_space_memory, ccw.cda, vdev->status,
487                                         MEMTXATTRS_UNSPECIFIED, NULL);
488             sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status);
489             ret = 0;
490         }
491         break;
492     case CCW_CMD_WRITE_STATUS:
493         if (check_len) {
494             if (ccw.count != sizeof(status)) {
495                 ret = -EINVAL;
496                 break;
497             }
498         } else if (ccw.count < sizeof(status)) {
499             /* Can't execute command. */
500             ret = -EINVAL;
501             break;
502         }
503         if (!ccw.cda) {
504             ret = -EFAULT;
505         } else {
506             ccw_dstream_read(&sch->cds, status);
507             if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
508                 virtio_ccw_stop_ioeventfd(dev);
509             }
510             if (virtio_set_status(vdev, status) == 0) {
511                 if (vdev->status == 0) {
512                     virtio_ccw_reset_virtio(dev, vdev);
513                 }
514                 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
515                     virtio_ccw_start_ioeventfd(dev);
516                 }
517                 sch->curr_status.scsw.count = ccw.count - sizeof(status);
518                 ret = 0;
519             } else {
520                 /* Trigger a command reject. */
521                 ret = -ENOSYS;
522             }
523         }
524         break;
525     case CCW_CMD_SET_IND:
526         if (check_len) {
527             if (ccw.count != sizeof(indicators)) {
528                 ret = -EINVAL;
529                 break;
530             }
531         } else if (ccw.count < sizeof(indicators)) {
532             /* Can't execute command. */
533             ret = -EINVAL;
534             break;
535         }
536         if (sch->thinint_active) {
537             /* Trigger a command reject. */
538             ret = -ENOSYS;
539             break;
540         }
541         if (virtio_get_num_queues(vdev) > NR_CLASSIC_INDICATOR_BITS) {
542             /* More queues than indicator bits --> trigger a reject */
543             ret = -ENOSYS;
544             break;
545         }
546         if (!ccw.cda) {
547             ret = -EFAULT;
548         } else {
549             ccw_dstream_read(&sch->cds, indicators);
550             be64_to_cpus(&indicators);
551             dev->indicators = get_indicator(indicators, sizeof(uint64_t));
552             sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
553             ret = 0;
554         }
555         break;
556     case CCW_CMD_SET_CONF_IND:
557         if (check_len) {
558             if (ccw.count != sizeof(indicators)) {
559                 ret = -EINVAL;
560                 break;
561             }
562         } else if (ccw.count < sizeof(indicators)) {
563             /* Can't execute command. */
564             ret = -EINVAL;
565             break;
566         }
567         if (!ccw.cda) {
568             ret = -EFAULT;
569         } else {
570             ccw_dstream_read(&sch->cds, indicators);
571             be64_to_cpus(&indicators);
572             dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
573             sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
574             ret = 0;
575         }
576         break;
577     case CCW_CMD_READ_VQ_CONF:
578         if (check_len) {
579             if (ccw.count != sizeof(vq_config)) {
580                 ret = -EINVAL;
581                 break;
582             }
583         } else if (ccw.count < sizeof(vq_config)) {
584             /* Can't execute command. */
585             ret = -EINVAL;
586             break;
587         }
588         if (!ccw.cda) {
589             ret = -EFAULT;
590         } else {
591             ccw_dstream_read(&sch->cds, vq_config.index);
592             be16_to_cpus(&vq_config.index);
593             if (vq_config.index >= VIRTIO_QUEUE_MAX) {
594                 ret = -EINVAL;
595                 break;
596             }
597             vq_config.num_max = virtio_queue_get_num(vdev,
598                                                      vq_config.index);
599             cpu_to_be16s(&vq_config.num_max);
600             ccw_dstream_write(&sch->cds, vq_config.num_max);
601             sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
602             ret = 0;
603         }
604         break;
605     case CCW_CMD_SET_IND_ADAPTER:
606         if (check_len) {
607             if (ccw.count != sizeof(thinint)) {
608                 ret = -EINVAL;
609                 break;
610             }
611         } else if (ccw.count < sizeof(thinint)) {
612             /* Can't execute command. */
613             ret = -EINVAL;
614             break;
615         }
616         if (!ccw.cda) {
617             ret = -EFAULT;
618         } else if (dev->indicators && !sch->thinint_active) {
619             /* Trigger a command reject. */
620             ret = -ENOSYS;
621         } else {
622             if (ccw_dstream_read(&sch->cds, thinint)) {
623                 ret = -EFAULT;
624             } else {
625                 be64_to_cpus(&thinint.ind_bit);
626                 be64_to_cpus(&thinint.summary_indicator);
627                 be64_to_cpus(&thinint.device_indicator);
628 
629                 dev->summary_indicator =
630                     get_indicator(thinint.summary_indicator, sizeof(uint8_t));
631                 dev->indicators =
632                     get_indicator(thinint.device_indicator,
633                                   thinint.ind_bit / 8 + 1);
634                 dev->thinint_isc = thinint.isc;
635                 dev->routes.adapter.ind_offset = thinint.ind_bit;
636                 dev->routes.adapter.summary_offset = 7;
637                 dev->routes.adapter.adapter_id = css_get_adapter_id(
638                                                  CSS_IO_ADAPTER_VIRTIO,
639                                                  dev->thinint_isc);
640                 sch->thinint_active = ((dev->indicators != NULL) &&
641                                        (dev->summary_indicator != NULL));
642                 sch->curr_status.scsw.count = ccw.count - sizeof(thinint);
643                 ret = 0;
644             }
645         }
646         break;
647     case CCW_CMD_SET_VIRTIO_REV:
648         len = sizeof(revinfo);
649         if (ccw.count < len) {
650             ret = -EINVAL;
651             break;
652         }
653         if (!ccw.cda) {
654             ret = -EFAULT;
655             break;
656         }
657         ccw_dstream_read_buf(&sch->cds, &revinfo, 4);
658         be16_to_cpus(&revinfo.revision);
659         be16_to_cpus(&revinfo.length);
660         if (ccw.count < len + revinfo.length ||
661             (check_len && ccw.count > len + revinfo.length)) {
662             ret = -EINVAL;
663             break;
664         }
665         /*
666          * Once we start to support revisions with additional data, we'll
667          * need to fetch it here. Nothing to do for now, though.
668          */
669         if (dev->revision >= 0 ||
670             revinfo.revision > virtio_ccw_rev_max(dev) ||
671             (dev->force_revision_1 && !revinfo.revision)) {
672             ret = -ENOSYS;
673             break;
674         }
675         ret = 0;
676         dev->revision = revinfo.revision;
677         break;
678     default:
679         ret = -ENOSYS;
680         break;
681     }
682     return ret;
683 }
684 
685 static void virtio_sch_disable_cb(SubchDev *sch)
686 {
687     VirtioCcwDevice *dev = sch->driver_data;
688 
689     dev->revision = -1;
690 }
691 
692 static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp)
693 {
694     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
695     CcwDevice *ccw_dev = CCW_DEVICE(dev);
696     CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
697     DeviceState *parent = DEVICE(ccw_dev);
698     BusState *qbus = qdev_get_parent_bus(parent);
699     VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
700     SubchDev *sch;
701     Error *err = NULL;
702 
703     sch = css_create_sch(ccw_dev->devno, cbus->squash_mcss, errp);
704     if (!sch) {
705         return;
706     }
707     if (!virtio_ccw_rev_max(dev) && dev->force_revision_1) {
708         error_setg(&err, "Invalid value of property max_rev "
709                    "(is %d expected >= 1)", virtio_ccw_rev_max(dev));
710         goto out_err;
711     }
712 
713     sch->driver_data = dev;
714     sch->ccw_cb = virtio_ccw_cb;
715     sch->disable_cb = virtio_sch_disable_cb;
716     sch->id.reserved = 0xff;
717     sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
718     sch->do_subchannel_work = do_subchannel_work_virtual;
719     ccw_dev->sch = sch;
720     dev->indicators = NULL;
721     dev->revision = -1;
722     css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
723 
724     trace_virtio_ccw_new_device(
725         sch->cssid, sch->ssid, sch->schid, sch->devno,
726         ccw_dev->devno.valid ? "user-configured" : "auto-configured");
727 
728     if (kvm_enabled() && !kvm_eventfds_enabled()) {
729         dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
730     }
731 
732     if (k->realize) {
733         k->realize(dev, &err);
734         if (err) {
735             goto out_err;
736         }
737     }
738 
739     ck->realize(ccw_dev, &err);
740     if (err) {
741         goto out_err;
742     }
743 
744     return;
745 
746 out_err:
747     error_propagate(errp, err);
748     css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
749     ccw_dev->sch = NULL;
750     g_free(sch);
751 }
752 
753 static void virtio_ccw_unrealize(VirtioCcwDevice *dev, Error **errp)
754 {
755     CcwDevice *ccw_dev = CCW_DEVICE(dev);
756     SubchDev *sch = ccw_dev->sch;
757 
758     if (sch) {
759         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
760         g_free(sch);
761         ccw_dev->sch = NULL;
762     }
763     if (dev->indicators) {
764         release_indicator(&dev->routes.adapter, dev->indicators);
765         dev->indicators = NULL;
766     }
767 }
768 
769 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
770 {
771     DeviceState *qdev = DEVICE(ccw_dev);
772     VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
773     DeviceState *vdev = DEVICE(&dev->vdev);
774 
775     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
776                                   object_get_typename(OBJECT(qdev)));
777     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
778     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
779 }
780 
781 static void virtio_ccw_net_instance_init(Object *obj)
782 {
783     VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
784 
785     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
786                                 TYPE_VIRTIO_NET);
787     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
788                               "bootindex", &error_abort);
789 }
790 
791 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
792 {
793     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
794     DeviceState *vdev = DEVICE(&dev->vdev);
795 
796     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
797     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
798 }
799 
800 static void virtio_ccw_blk_instance_init(Object *obj)
801 {
802     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
803 
804     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
805                                 TYPE_VIRTIO_BLK);
806     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
807                               "bootindex", &error_abort);
808 }
809 
810 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
811 {
812     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
813     DeviceState *vdev = DEVICE(&dev->vdev);
814     DeviceState *proxy = DEVICE(ccw_dev);
815     char *bus_name;
816 
817     /*
818      * For command line compatibility, this sets the virtio-serial-device bus
819      * name as before.
820      */
821     if (proxy->id) {
822         bus_name = g_strdup_printf("%s.0", proxy->id);
823         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
824         g_free(bus_name);
825     }
826 
827     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
828     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
829 }
830 
831 
832 static void virtio_ccw_serial_instance_init(Object *obj)
833 {
834     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
835 
836     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
837                                 TYPE_VIRTIO_SERIAL);
838 }
839 
840 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
841 {
842     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
843     DeviceState *vdev = DEVICE(&dev->vdev);
844 
845     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
846     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
847 }
848 
849 static void virtio_ccw_balloon_instance_init(Object *obj)
850 {
851     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
852 
853     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
854                                 TYPE_VIRTIO_BALLOON);
855     object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
856                               "guest-stats", &error_abort);
857     object_property_add_alias(obj, "guest-stats-polling-interval",
858                               OBJECT(&dev->vdev),
859                               "guest-stats-polling-interval", &error_abort);
860 }
861 
862 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
863 {
864     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
865     DeviceState *vdev = DEVICE(&dev->vdev);
866     DeviceState *qdev = DEVICE(ccw_dev);
867     char *bus_name;
868 
869     /*
870      * For command line compatibility, this sets the virtio-scsi-device bus
871      * name as before.
872      */
873     if (qdev->id) {
874         bus_name = g_strdup_printf("%s.0", qdev->id);
875         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
876         g_free(bus_name);
877     }
878 
879     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
880     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
881 }
882 
883 static void virtio_ccw_scsi_instance_init(Object *obj)
884 {
885     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
886 
887     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
888                                 TYPE_VIRTIO_SCSI);
889 }
890 
891 #ifdef CONFIG_VHOST_SCSI
892 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
893 {
894     VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
895     DeviceState *vdev = DEVICE(&dev->vdev);
896 
897     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
898     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
899 }
900 
901 static void vhost_ccw_scsi_instance_init(Object *obj)
902 {
903     VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
904 
905     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
906                                 TYPE_VHOST_SCSI);
907 }
908 #endif
909 
910 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
911 {
912     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
913     DeviceState *vdev = DEVICE(&dev->vdev);
914     Error *err = NULL;
915 
916     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
917     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
918     if (err) {
919         error_propagate(errp, err);
920         return;
921     }
922 
923     object_property_set_link(OBJECT(dev),
924                              OBJECT(dev->vdev.conf.rng), "rng",
925                              NULL);
926 }
927 
928 static void virtio_ccw_crypto_realize(VirtioCcwDevice *ccw_dev, Error **errp)
929 {
930     VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(ccw_dev);
931     DeviceState *vdev = DEVICE(&dev->vdev);
932     Error *err = NULL;
933 
934     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
935     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
936     if (err) {
937         error_propagate(errp, err);
938         return;
939     }
940 
941     object_property_set_link(OBJECT(vdev),
942                              OBJECT(dev->vdev.conf.cryptodev), "cryptodev",
943                              NULL);
944 }
945 
946 static void virtio_ccw_gpu_realize(VirtioCcwDevice *ccw_dev, Error **errp)
947 {
948     VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(ccw_dev);
949     DeviceState *vdev = DEVICE(&dev->vdev);
950 
951     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
952     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
953 }
954 
955 static void virtio_ccw_input_realize(VirtioCcwDevice *ccw_dev, Error **errp)
956 {
957     VirtIOInputCcw *dev = VIRTIO_INPUT_CCW(ccw_dev);
958     DeviceState *vdev = DEVICE(&dev->vdev);
959 
960     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
961     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
962 }
963 
964 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
965  * be careful and test performance if you change this.
966  */
967 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
968 {
969     CcwDevice *ccw_dev = to_ccw_dev_fast(d);
970 
971     return container_of(ccw_dev, VirtioCcwDevice, parent_obj);
972 }
973 
974 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
975                                      uint8_t to_be_set)
976 {
977     uint8_t ind_old, ind_new;
978     hwaddr len = 1;
979     uint8_t *ind_addr;
980 
981     ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
982     if (!ind_addr) {
983         error_report("%s(%x.%x.%04x): unable to access indicator",
984                      __func__, sch->cssid, sch->ssid, sch->schid);
985         return -1;
986     }
987     do {
988         ind_old = *ind_addr;
989         ind_new = ind_old | to_be_set;
990     } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
991     trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
992     cpu_physical_memory_unmap(ind_addr, len, 1, len);
993 
994     return ind_old;
995 }
996 
997 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
998 {
999     VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1000     CcwDevice *ccw_dev = to_ccw_dev_fast(d);
1001     SubchDev *sch = ccw_dev->sch;
1002     uint64_t indicators;
1003 
1004     if (vector == VIRTIO_NO_VECTOR) {
1005         return;
1006     }
1007     /*
1008      * vector < VIRTIO_QUEUE_MAX: notification for a virtqueue
1009      * vector == VIRTIO_QUEUE_MAX: configuration change notification
1010      * bits beyond that are unused and should never be notified for
1011      */
1012     assert(vector <= VIRTIO_QUEUE_MAX);
1013 
1014     if (vector < VIRTIO_QUEUE_MAX) {
1015         if (!dev->indicators) {
1016             return;
1017         }
1018         if (sch->thinint_active) {
1019             /*
1020              * In the adapter interrupt case, indicators points to a
1021              * memory area that may be (way) larger than 64 bit and
1022              * ind_bit indicates the start of the indicators in a big
1023              * endian notation.
1024              */
1025             uint64_t ind_bit = dev->routes.adapter.ind_offset;
1026 
1027             virtio_set_ind_atomic(sch, dev->indicators->addr +
1028                                   (ind_bit + vector) / 8,
1029                                   0x80 >> ((ind_bit + vector) % 8));
1030             if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1031                                        0x01)) {
1032                 css_adapter_interrupt(CSS_IO_ADAPTER_VIRTIO, dev->thinint_isc);
1033             }
1034         } else {
1035             assert(vector < NR_CLASSIC_INDICATOR_BITS);
1036             indicators = address_space_ldq(&address_space_memory,
1037                                            dev->indicators->addr,
1038                                            MEMTXATTRS_UNSPECIFIED,
1039                                            NULL);
1040             indicators |= 1ULL << vector;
1041             address_space_stq(&address_space_memory, dev->indicators->addr,
1042                               indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1043             css_conditional_io_interrupt(sch);
1044         }
1045     } else {
1046         if (!dev->indicators2) {
1047             return;
1048         }
1049         indicators = address_space_ldq(&address_space_memory,
1050                                        dev->indicators2->addr,
1051                                        MEMTXATTRS_UNSPECIFIED,
1052                                        NULL);
1053         indicators |= 1ULL;
1054         address_space_stq(&address_space_memory, dev->indicators2->addr,
1055                           indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1056         css_conditional_io_interrupt(sch);
1057     }
1058 }
1059 
1060 static void virtio_ccw_reset(DeviceState *d)
1061 {
1062     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1063     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1064     VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1065 
1066     virtio_ccw_reset_virtio(dev, vdev);
1067     if (vdc->parent_reset) {
1068         vdc->parent_reset(d);
1069     }
1070 }
1071 
1072 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1073 {
1074     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1075 
1076     if (running) {
1077         virtio_ccw_start_ioeventfd(dev);
1078     } else {
1079         virtio_ccw_stop_ioeventfd(dev);
1080     }
1081 }
1082 
1083 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1084 {
1085     CcwDevice *dev = CCW_DEVICE(d);
1086 
1087     return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1088 }
1089 
1090 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1091 {
1092     int r;
1093     CcwDevice *ccw_dev = CCW_DEVICE(dev);
1094 
1095     if (!ccw_dev->sch->thinint_active) {
1096         return -EINVAL;
1097     }
1098 
1099     r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1100     if (r) {
1101         return r;
1102     }
1103     r = map_indicator(&dev->routes.adapter, dev->indicators);
1104     if (r) {
1105         return r;
1106     }
1107     dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1108     dev->routes.adapter.ind_addr = dev->indicators->map;
1109 
1110     return 0;
1111 }
1112 
1113 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1114 {
1115     int i;
1116     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1117     int ret;
1118     S390FLICState *fs = s390_get_flic();
1119     S390FLICStateClass *fsc = s390_get_flic_class(fs);
1120 
1121     ret = virtio_ccw_get_mappings(dev);
1122     if (ret) {
1123         return ret;
1124     }
1125     for (i = 0; i < nvqs; i++) {
1126         if (!virtio_queue_get_num(vdev, i)) {
1127             break;
1128         }
1129     }
1130     dev->routes.num_routes = i;
1131     return fsc->add_adapter_routes(fs, &dev->routes);
1132 }
1133 
1134 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1135 {
1136     S390FLICState *fs = s390_get_flic();
1137     S390FLICStateClass *fsc = s390_get_flic_class(fs);
1138 
1139     fsc->release_adapter_routes(fs, &dev->routes);
1140 }
1141 
1142 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1143 {
1144     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1145     VirtQueue *vq = virtio_get_queue(vdev, n);
1146     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1147 
1148     return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1149                                               dev->routes.gsi[n]);
1150 }
1151 
1152 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1153 {
1154     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1155     VirtQueue *vq = virtio_get_queue(vdev, n);
1156     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1157     int ret;
1158 
1159     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1160                                                 dev->routes.gsi[n]);
1161     assert(ret == 0);
1162 }
1163 
1164 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1165                                          bool assign, bool with_irqfd)
1166 {
1167     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1168     VirtQueue *vq = virtio_get_queue(vdev, n);
1169     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1170     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1171 
1172     if (assign) {
1173         int r = event_notifier_init(notifier, 0);
1174 
1175         if (r < 0) {
1176             return r;
1177         }
1178         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1179         if (with_irqfd) {
1180             r = virtio_ccw_add_irqfd(dev, n);
1181             if (r) {
1182                 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1183                                                            with_irqfd);
1184                 return r;
1185             }
1186         }
1187         /*
1188          * We do not support individual masking for channel devices, so we
1189          * need to manually trigger any guest masking callbacks here.
1190          */
1191         if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1192             k->guest_notifier_mask(vdev, n, false);
1193         }
1194         /* get lost events and re-inject */
1195         if (k->guest_notifier_pending &&
1196             k->guest_notifier_pending(vdev, n)) {
1197             event_notifier_set(notifier);
1198         }
1199     } else {
1200         if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1201             k->guest_notifier_mask(vdev, n, true);
1202         }
1203         if (with_irqfd) {
1204             virtio_ccw_remove_irqfd(dev, n);
1205         }
1206         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1207         event_notifier_cleanup(notifier);
1208     }
1209     return 0;
1210 }
1211 
1212 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1213                                           bool assigned)
1214 {
1215     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1216     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1217     CcwDevice *ccw_dev = CCW_DEVICE(d);
1218     bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled();
1219     int r, n;
1220 
1221     if (with_irqfd && assigned) {
1222         /* irq routes need to be set up before assigning irqfds */
1223         r = virtio_ccw_setup_irqroutes(dev, nvqs);
1224         if (r < 0) {
1225             goto irqroute_error;
1226         }
1227     }
1228     for (n = 0; n < nvqs; n++) {
1229         if (!virtio_queue_get_num(vdev, n)) {
1230             break;
1231         }
1232         r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1233         if (r < 0) {
1234             goto assign_error;
1235         }
1236     }
1237     if (with_irqfd && !assigned) {
1238         /* release irq routes after irqfds have been released */
1239         virtio_ccw_release_irqroutes(dev, nvqs);
1240     }
1241     return 0;
1242 
1243 assign_error:
1244     while (--n >= 0) {
1245         virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1246     }
1247 irqroute_error:
1248     if (with_irqfd && assigned) {
1249         virtio_ccw_release_irqroutes(dev, nvqs);
1250     }
1251     return r;
1252 }
1253 
1254 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1255 {
1256     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1257     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1258 
1259     qemu_put_be16(f, virtio_queue_vector(vdev, n));
1260 }
1261 
1262 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1263 {
1264     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1265     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1266     uint16_t vector;
1267 
1268     qemu_get_be16s(f, &vector);
1269     virtio_queue_set_vector(vdev, n , vector);
1270 
1271     return 0;
1272 }
1273 
1274 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1275 {
1276     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1277     vmstate_save_state(f, &vmstate_virtio_ccw_dev, dev, NULL);
1278 }
1279 
1280 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1281 {
1282     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1283     return vmstate_load_state(f, &vmstate_virtio_ccw_dev, dev, 1);
1284 }
1285 
1286 static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp)
1287 {
1288    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1289    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1290 
1291     if (dev->max_rev >= 1) {
1292         virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1293     }
1294 }
1295 
1296 /* This is called by virtio-bus just after the device is plugged. */
1297 static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1298 {
1299     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1300     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1301     CcwDevice *ccw_dev = CCW_DEVICE(d);
1302     SubchDev *sch = ccw_dev->sch;
1303     int n = virtio_get_num_queues(vdev);
1304     S390FLICState *flic = s390_get_flic();
1305 
1306     if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1307         dev->max_rev = 0;
1308     }
1309 
1310     if (virtio_get_num_queues(vdev) > VIRTIO_QUEUE_MAX) {
1311         error_setg(errp, "The number of virtqueues %d "
1312                    "exceeds virtio limit %d", n,
1313                    VIRTIO_QUEUE_MAX);
1314         return;
1315     }
1316     if (virtio_get_num_queues(vdev) > flic->adapter_routes_max_batch) {
1317         error_setg(errp, "The number of virtqueues %d "
1318                    "exceeds flic adapter route limit %d", n,
1319                    flic->adapter_routes_max_batch);
1320         return;
1321     }
1322 
1323     sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1324 
1325 
1326     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1327                           d->hotplugged, 1);
1328 }
1329 
1330 static void virtio_ccw_device_unplugged(DeviceState *d)
1331 {
1332     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1333 
1334     virtio_ccw_stop_ioeventfd(dev);
1335 }
1336 /**************** Virtio-ccw Bus Device Descriptions *******************/
1337 
1338 static Property virtio_ccw_net_properties[] = {
1339     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1340                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1341     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1342                        VIRTIO_CCW_MAX_REV),
1343     DEFINE_PROP_END_OF_LIST(),
1344 };
1345 
1346 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1347 {
1348     DeviceClass *dc = DEVICE_CLASS(klass);
1349     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1350 
1351     k->realize = virtio_ccw_net_realize;
1352     k->unrealize = virtio_ccw_unrealize;
1353     dc->props = virtio_ccw_net_properties;
1354     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1355 }
1356 
1357 static const TypeInfo virtio_ccw_net = {
1358     .name          = TYPE_VIRTIO_NET_CCW,
1359     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1360     .instance_size = sizeof(VirtIONetCcw),
1361     .instance_init = virtio_ccw_net_instance_init,
1362     .class_init    = virtio_ccw_net_class_init,
1363 };
1364 
1365 static Property virtio_ccw_blk_properties[] = {
1366     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1367                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1368     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1369                        VIRTIO_CCW_MAX_REV),
1370     DEFINE_PROP_END_OF_LIST(),
1371 };
1372 
1373 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1374 {
1375     DeviceClass *dc = DEVICE_CLASS(klass);
1376     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1377 
1378     k->realize = virtio_ccw_blk_realize;
1379     k->unrealize = virtio_ccw_unrealize;
1380     dc->props = virtio_ccw_blk_properties;
1381     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1382 }
1383 
1384 static const TypeInfo virtio_ccw_blk = {
1385     .name          = TYPE_VIRTIO_BLK_CCW,
1386     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1387     .instance_size = sizeof(VirtIOBlkCcw),
1388     .instance_init = virtio_ccw_blk_instance_init,
1389     .class_init    = virtio_ccw_blk_class_init,
1390 };
1391 
1392 static Property virtio_ccw_serial_properties[] = {
1393     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1394                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1395     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1396                        VIRTIO_CCW_MAX_REV),
1397     DEFINE_PROP_END_OF_LIST(),
1398 };
1399 
1400 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1401 {
1402     DeviceClass *dc = DEVICE_CLASS(klass);
1403     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1404 
1405     k->realize = virtio_ccw_serial_realize;
1406     k->unrealize = virtio_ccw_unrealize;
1407     dc->props = virtio_ccw_serial_properties;
1408     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1409 }
1410 
1411 static const TypeInfo virtio_ccw_serial = {
1412     .name          = TYPE_VIRTIO_SERIAL_CCW,
1413     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1414     .instance_size = sizeof(VirtioSerialCcw),
1415     .instance_init = virtio_ccw_serial_instance_init,
1416     .class_init    = virtio_ccw_serial_class_init,
1417 };
1418 
1419 static Property virtio_ccw_balloon_properties[] = {
1420     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1421                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1422     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1423                        VIRTIO_CCW_MAX_REV),
1424     DEFINE_PROP_END_OF_LIST(),
1425 };
1426 
1427 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1428 {
1429     DeviceClass *dc = DEVICE_CLASS(klass);
1430     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1431 
1432     k->realize = virtio_ccw_balloon_realize;
1433     k->unrealize = virtio_ccw_unrealize;
1434     dc->props = virtio_ccw_balloon_properties;
1435     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1436 }
1437 
1438 static const TypeInfo virtio_ccw_balloon = {
1439     .name          = TYPE_VIRTIO_BALLOON_CCW,
1440     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1441     .instance_size = sizeof(VirtIOBalloonCcw),
1442     .instance_init = virtio_ccw_balloon_instance_init,
1443     .class_init    = virtio_ccw_balloon_class_init,
1444 };
1445 
1446 static Property virtio_ccw_scsi_properties[] = {
1447     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1448                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1449     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1450                        VIRTIO_CCW_MAX_REV),
1451     DEFINE_PROP_END_OF_LIST(),
1452 };
1453 
1454 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1455 {
1456     DeviceClass *dc = DEVICE_CLASS(klass);
1457     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1458 
1459     k->realize = virtio_ccw_scsi_realize;
1460     k->unrealize = virtio_ccw_unrealize;
1461     dc->props = virtio_ccw_scsi_properties;
1462     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1463 }
1464 
1465 static const TypeInfo virtio_ccw_scsi = {
1466     .name          = TYPE_VIRTIO_SCSI_CCW,
1467     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1468     .instance_size = sizeof(VirtIOSCSICcw),
1469     .instance_init = virtio_ccw_scsi_instance_init,
1470     .class_init    = virtio_ccw_scsi_class_init,
1471 };
1472 
1473 #ifdef CONFIG_VHOST_SCSI
1474 static Property vhost_ccw_scsi_properties[] = {
1475     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1476                        VIRTIO_CCW_MAX_REV),
1477     DEFINE_PROP_END_OF_LIST(),
1478 };
1479 
1480 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1481 {
1482     DeviceClass *dc = DEVICE_CLASS(klass);
1483     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1484 
1485     k->realize = vhost_ccw_scsi_realize;
1486     k->unrealize = virtio_ccw_unrealize;
1487     dc->props = vhost_ccw_scsi_properties;
1488     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1489 }
1490 
1491 static const TypeInfo vhost_ccw_scsi = {
1492     .name          = TYPE_VHOST_SCSI_CCW,
1493     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1494     .instance_size = sizeof(VHostSCSICcw),
1495     .instance_init = vhost_ccw_scsi_instance_init,
1496     .class_init    = vhost_ccw_scsi_class_init,
1497 };
1498 #endif
1499 
1500 static void virtio_ccw_rng_instance_init(Object *obj)
1501 {
1502     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1503 
1504     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1505                                 TYPE_VIRTIO_RNG);
1506 }
1507 
1508 static Property virtio_ccw_rng_properties[] = {
1509     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1510                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1511     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1512                        VIRTIO_CCW_MAX_REV),
1513     DEFINE_PROP_END_OF_LIST(),
1514 };
1515 
1516 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1517 {
1518     DeviceClass *dc = DEVICE_CLASS(klass);
1519     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1520 
1521     k->realize = virtio_ccw_rng_realize;
1522     k->unrealize = virtio_ccw_unrealize;
1523     dc->props = virtio_ccw_rng_properties;
1524     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1525 }
1526 
1527 static const TypeInfo virtio_ccw_rng = {
1528     .name          = TYPE_VIRTIO_RNG_CCW,
1529     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1530     .instance_size = sizeof(VirtIORNGCcw),
1531     .instance_init = virtio_ccw_rng_instance_init,
1532     .class_init    = virtio_ccw_rng_class_init,
1533 };
1534 
1535 static Property virtio_ccw_crypto_properties[] = {
1536     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1537                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1538     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1539                        VIRTIO_CCW_MAX_REV),
1540     DEFINE_PROP_END_OF_LIST(),
1541 };
1542 
1543 static void virtio_ccw_crypto_instance_init(Object *obj)
1544 {
1545     VirtIOCryptoCcw *dev = VIRTIO_CRYPTO_CCW(obj);
1546     VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1547 
1548     ccw_dev->force_revision_1 = true;
1549     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1550                                 TYPE_VIRTIO_CRYPTO);
1551 }
1552 
1553 static void virtio_ccw_crypto_class_init(ObjectClass *klass, void *data)
1554 {
1555     DeviceClass *dc = DEVICE_CLASS(klass);
1556     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1557 
1558     k->realize = virtio_ccw_crypto_realize;
1559     k->unrealize = virtio_ccw_unrealize;
1560     dc->props = virtio_ccw_crypto_properties;
1561     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1562 }
1563 
1564 static const TypeInfo virtio_ccw_crypto = {
1565     .name          = TYPE_VIRTIO_CRYPTO_CCW,
1566     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1567     .instance_size = sizeof(VirtIOCryptoCcw),
1568     .instance_init = virtio_ccw_crypto_instance_init,
1569     .class_init    = virtio_ccw_crypto_class_init,
1570 };
1571 
1572 static Property virtio_ccw_gpu_properties[] = {
1573     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1574                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1575     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1576                        VIRTIO_CCW_MAX_REV),
1577     DEFINE_PROP_END_OF_LIST(),
1578 };
1579 
1580 static void virtio_ccw_gpu_instance_init(Object *obj)
1581 {
1582     VirtIOGPUCcw *dev = VIRTIO_GPU_CCW(obj);
1583     VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1584 
1585     ccw_dev->force_revision_1 = true;
1586     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1587                                 TYPE_VIRTIO_GPU);
1588 }
1589 
1590 static void virtio_ccw_gpu_class_init(ObjectClass *klass, void *data)
1591 {
1592     DeviceClass *dc = DEVICE_CLASS(klass);
1593     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1594 
1595     k->realize = virtio_ccw_gpu_realize;
1596     k->unrealize = virtio_ccw_unrealize;
1597     dc->props = virtio_ccw_gpu_properties;
1598     dc->hotpluggable = false;
1599     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
1600 }
1601 
1602 static const TypeInfo virtio_ccw_gpu = {
1603     .name          = TYPE_VIRTIO_GPU_CCW,
1604     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1605     .instance_size = sizeof(VirtIOGPUCcw),
1606     .instance_init = virtio_ccw_gpu_instance_init,
1607     .class_init    = virtio_ccw_gpu_class_init,
1608 };
1609 
1610 static Property virtio_ccw_input_properties[] = {
1611     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1612                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1613     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1614                        VIRTIO_CCW_MAX_REV),
1615     DEFINE_PROP_END_OF_LIST(),
1616 };
1617 
1618 static void virtio_ccw_input_class_init(ObjectClass *klass, void *data)
1619 {
1620     DeviceClass *dc = DEVICE_CLASS(klass);
1621     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1622 
1623     k->realize = virtio_ccw_input_realize;
1624     k->unrealize = virtio_ccw_unrealize;
1625     dc->props = virtio_ccw_input_properties;
1626     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1627 }
1628 
1629 static void virtio_ccw_keyboard_instance_init(Object *obj)
1630 {
1631     VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1632     VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1633 
1634     ccw_dev->force_revision_1 = true;
1635     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1636                                 TYPE_VIRTIO_KEYBOARD);
1637 }
1638 
1639 static void virtio_ccw_mouse_instance_init(Object *obj)
1640 {
1641     VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1642     VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1643 
1644     ccw_dev->force_revision_1 = true;
1645     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1646                                 TYPE_VIRTIO_MOUSE);
1647 }
1648 
1649 static void virtio_ccw_tablet_instance_init(Object *obj)
1650 {
1651     VirtIOInputHIDCcw *dev = VIRTIO_INPUT_HID_CCW(obj);
1652     VirtioCcwDevice *ccw_dev = VIRTIO_CCW_DEVICE(obj);
1653 
1654     ccw_dev->force_revision_1 = true;
1655     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1656                                 TYPE_VIRTIO_TABLET);
1657 }
1658 
1659 static const TypeInfo virtio_ccw_input = {
1660     .name          = TYPE_VIRTIO_INPUT_CCW,
1661     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1662     .instance_size = sizeof(VirtIOInputCcw),
1663     .class_init    = virtio_ccw_input_class_init,
1664     .abstract = true,
1665 };
1666 
1667 static const TypeInfo virtio_ccw_input_hid = {
1668     .name          = TYPE_VIRTIO_INPUT_HID_CCW,
1669     .parent        = TYPE_VIRTIO_INPUT_CCW,
1670     .instance_size = sizeof(VirtIOInputHIDCcw),
1671     .abstract = true,
1672 };
1673 
1674 static const TypeInfo virtio_ccw_keyboard = {
1675     .name          = TYPE_VIRTIO_KEYBOARD_CCW,
1676     .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1677     .instance_size = sizeof(VirtIOInputHIDCcw),
1678     .instance_init = virtio_ccw_keyboard_instance_init,
1679 };
1680 
1681 static const TypeInfo virtio_ccw_mouse = {
1682     .name          = TYPE_VIRTIO_MOUSE_CCW,
1683     .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1684     .instance_size = sizeof(VirtIOInputHIDCcw),
1685     .instance_init = virtio_ccw_mouse_instance_init,
1686 };
1687 
1688 static const TypeInfo virtio_ccw_tablet = {
1689     .name          = TYPE_VIRTIO_TABLET_CCW,
1690     .parent        = TYPE_VIRTIO_INPUT_HID_CCW,
1691     .instance_size = sizeof(VirtIOInputHIDCcw),
1692     .instance_init = virtio_ccw_tablet_instance_init,
1693 };
1694 
1695 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1696 {
1697     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1698 
1699     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1700     virtio_ccw_device_realize(_dev, errp);
1701 }
1702 
1703 static void virtio_ccw_busdev_unrealize(DeviceState *dev, Error **errp)
1704 {
1705     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1706     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1707 
1708     _info->unrealize(_dev, errp);
1709 }
1710 
1711 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1712                                      DeviceState *dev, Error **errp)
1713 {
1714     VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev);
1715 
1716     virtio_ccw_stop_ioeventfd(_dev);
1717 }
1718 
1719 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1720 {
1721     DeviceClass *dc = DEVICE_CLASS(klass);
1722     CCWDeviceClass *k = CCW_DEVICE_CLASS(dc);
1723     VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_CLASS(klass);
1724 
1725     k->unplug = virtio_ccw_busdev_unplug;
1726     dc->realize = virtio_ccw_busdev_realize;
1727     dc->unrealize = virtio_ccw_busdev_unrealize;
1728     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1729     device_class_set_parent_reset(dc, virtio_ccw_reset, &vdc->parent_reset);
1730 }
1731 
1732 static const TypeInfo virtio_ccw_device_info = {
1733     .name = TYPE_VIRTIO_CCW_DEVICE,
1734     .parent = TYPE_CCW_DEVICE,
1735     .instance_size = sizeof(VirtioCcwDevice),
1736     .class_init = virtio_ccw_device_class_init,
1737     .class_size = sizeof(VirtIOCCWDeviceClass),
1738     .abstract = true,
1739 };
1740 
1741 /* virtio-ccw-bus */
1742 
1743 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1744                                VirtioCcwDevice *dev)
1745 {
1746     DeviceState *qdev = DEVICE(dev);
1747     char virtio_bus_name[] = "virtio-bus";
1748 
1749     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1750                         qdev, virtio_bus_name);
1751 }
1752 
1753 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1754 {
1755     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1756     BusClass *bus_class = BUS_CLASS(klass);
1757 
1758     bus_class->max_dev = 1;
1759     k->notify = virtio_ccw_notify;
1760     k->vmstate_change = virtio_ccw_vmstate_change;
1761     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1762     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1763     k->save_queue = virtio_ccw_save_queue;
1764     k->load_queue = virtio_ccw_load_queue;
1765     k->save_config = virtio_ccw_save_config;
1766     k->load_config = virtio_ccw_load_config;
1767     k->pre_plugged = virtio_ccw_pre_plugged;
1768     k->device_plugged = virtio_ccw_device_plugged;
1769     k->device_unplugged = virtio_ccw_device_unplugged;
1770     k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled;
1771     k->ioeventfd_assign = virtio_ccw_ioeventfd_assign;
1772 }
1773 
1774 static const TypeInfo virtio_ccw_bus_info = {
1775     .name = TYPE_VIRTIO_CCW_BUS,
1776     .parent = TYPE_VIRTIO_BUS,
1777     .instance_size = sizeof(VirtioCcwBusState),
1778     .class_init = virtio_ccw_bus_class_init,
1779 };
1780 
1781 #ifdef CONFIG_VIRTFS
1782 static Property virtio_ccw_9p_properties[] = {
1783     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1784             VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1785     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1786                        VIRTIO_CCW_MAX_REV),
1787     DEFINE_PROP_END_OF_LIST(),
1788 };
1789 
1790 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1791 {
1792     V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1793     DeviceState *vdev = DEVICE(&dev->vdev);
1794 
1795     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1796     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1797 }
1798 
1799 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1800 {
1801     DeviceClass *dc = DEVICE_CLASS(klass);
1802     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1803 
1804     k->unrealize = virtio_ccw_unrealize;
1805     k->realize = virtio_ccw_9p_realize;
1806     dc->props = virtio_ccw_9p_properties;
1807     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1808 }
1809 
1810 static void virtio_ccw_9p_instance_init(Object *obj)
1811 {
1812     V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1813 
1814     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1815                                 TYPE_VIRTIO_9P);
1816 }
1817 
1818 static const TypeInfo virtio_ccw_9p_info = {
1819     .name          = TYPE_VIRTIO_9P_CCW,
1820     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1821     .instance_size = sizeof(V9fsCCWState),
1822     .instance_init = virtio_ccw_9p_instance_init,
1823     .class_init    = virtio_ccw_9p_class_init,
1824 };
1825 #endif
1826 
1827 #ifdef CONFIG_VHOST_VSOCK
1828 
1829 static Property vhost_vsock_ccw_properties[] = {
1830     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1831                        VIRTIO_CCW_MAX_REV),
1832     DEFINE_PROP_END_OF_LIST(),
1833 };
1834 
1835 static void vhost_vsock_ccw_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1836 {
1837     VHostVSockCCWState *dev = VHOST_VSOCK_CCW(ccw_dev);
1838     DeviceState *vdev = DEVICE(&dev->vdev);
1839 
1840     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1841     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1842 }
1843 
1844 static void vhost_vsock_ccw_class_init(ObjectClass *klass, void *data)
1845 {
1846     DeviceClass *dc = DEVICE_CLASS(klass);
1847     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1848 
1849     k->realize = vhost_vsock_ccw_realize;
1850     k->unrealize = virtio_ccw_unrealize;
1851     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1852     dc->props = vhost_vsock_ccw_properties;
1853 }
1854 
1855 static void vhost_vsock_ccw_instance_init(Object *obj)
1856 {
1857     VHostVSockCCWState *dev = VHOST_VSOCK_CCW(obj);
1858 
1859     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1860                                 TYPE_VHOST_VSOCK);
1861 }
1862 
1863 static const TypeInfo vhost_vsock_ccw_info = {
1864     .name          = TYPE_VHOST_VSOCK_CCW,
1865     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1866     .instance_size = sizeof(VHostVSockCCWState),
1867     .instance_init = vhost_vsock_ccw_instance_init,
1868     .class_init    = vhost_vsock_ccw_class_init,
1869 };
1870 #endif
1871 
1872 static void virtio_ccw_register(void)
1873 {
1874     type_register_static(&virtio_ccw_bus_info);
1875     type_register_static(&virtio_ccw_device_info);
1876     type_register_static(&virtio_ccw_serial);
1877     type_register_static(&virtio_ccw_blk);
1878     type_register_static(&virtio_ccw_net);
1879     type_register_static(&virtio_ccw_balloon);
1880     type_register_static(&virtio_ccw_scsi);
1881 #ifdef CONFIG_VHOST_SCSI
1882     type_register_static(&vhost_ccw_scsi);
1883 #endif
1884     type_register_static(&virtio_ccw_rng);
1885 #ifdef CONFIG_VIRTFS
1886     type_register_static(&virtio_ccw_9p_info);
1887 #endif
1888 #ifdef CONFIG_VHOST_VSOCK
1889     type_register_static(&vhost_vsock_ccw_info);
1890 #endif
1891     type_register_static(&virtio_ccw_crypto);
1892     type_register_static(&virtio_ccw_gpu);
1893     type_register_static(&virtio_ccw_input);
1894     type_register_static(&virtio_ccw_input_hid);
1895     type_register_static(&virtio_ccw_keyboard);
1896     type_register_static(&virtio_ccw_mouse);
1897     type_register_static(&virtio_ccw_tablet);
1898 }
1899 
1900 type_init(virtio_ccw_register)
1901