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