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