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