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