xref: /openbmc/qemu/hw/s390x/virtio-ccw.c (revision d341d9f3)
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     /* queue indicators + secondary indicators */
1181     if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) {
1182         return;
1183     }
1184 
1185     if (vector < VIRTIO_CCW_QUEUE_MAX) {
1186         if (!dev->indicators) {
1187             return;
1188         }
1189         if (sch->thinint_active) {
1190             /*
1191              * In the adapter interrupt case, indicators points to a
1192              * memory area that may be (way) larger than 64 bit and
1193              * ind_bit indicates the start of the indicators in a big
1194              * endian notation.
1195              */
1196             uint64_t ind_bit = dev->routes.adapter.ind_offset;
1197 
1198             virtio_set_ind_atomic(sch, dev->indicators->addr +
1199                                   (ind_bit + vector) / 8,
1200                                   0x80 >> ((ind_bit + vector) % 8));
1201             if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1202                                        0x01)) {
1203                 css_adapter_interrupt(dev->thinint_isc);
1204             }
1205         } else {
1206             indicators = address_space_ldq(&address_space_memory,
1207                                            dev->indicators->addr,
1208                                            MEMTXATTRS_UNSPECIFIED,
1209                                            NULL);
1210             indicators |= 1ULL << vector;
1211             address_space_stq(&address_space_memory, dev->indicators->addr,
1212                               indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1213             css_conditional_io_interrupt(sch);
1214         }
1215     } else {
1216         if (!dev->indicators2) {
1217             return;
1218         }
1219         vector = 0;
1220         indicators = address_space_ldq(&address_space_memory,
1221                                        dev->indicators2->addr,
1222                                        MEMTXATTRS_UNSPECIFIED,
1223                                        NULL);
1224         indicators |= 1ULL << vector;
1225         address_space_stq(&address_space_memory, dev->indicators2->addr,
1226                           indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1227         css_conditional_io_interrupt(sch);
1228     }
1229 }
1230 
1231 static void virtio_ccw_reset(DeviceState *d)
1232 {
1233     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1234     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1235 
1236     virtio_ccw_reset_virtio(dev, vdev);
1237     css_reset_sch(dev->sch);
1238 }
1239 
1240 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1241 {
1242     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1243 
1244     if (running) {
1245         virtio_ccw_start_ioeventfd(dev);
1246     } else {
1247         virtio_ccw_stop_ioeventfd(dev);
1248     }
1249 }
1250 
1251 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1252 {
1253     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1254 
1255     return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1256 }
1257 
1258 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1259 {
1260     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1261 
1262     /* Stop using the generic ioeventfd, we are doing eventfd handling
1263      * ourselves below */
1264     dev->ioeventfd_disabled = assign;
1265     if (assign) {
1266         virtio_ccw_stop_ioeventfd(dev);
1267     }
1268     return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1269 }
1270 
1271 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1272 {
1273     int r;
1274 
1275     if (!dev->sch->thinint_active) {
1276         return -EINVAL;
1277     }
1278 
1279     r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1280     if (r) {
1281         return r;
1282     }
1283     r = map_indicator(&dev->routes.adapter, dev->indicators);
1284     if (r) {
1285         return r;
1286     }
1287     dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1288     dev->routes.adapter.ind_addr = dev->indicators->map;
1289 
1290     return 0;
1291 }
1292 
1293 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1294 {
1295     int i;
1296     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1297     int ret;
1298     S390FLICState *fs = s390_get_flic();
1299     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1300 
1301     ret = virtio_ccw_get_mappings(dev);
1302     if (ret) {
1303         return ret;
1304     }
1305     for (i = 0; i < nvqs; i++) {
1306         if (!virtio_queue_get_num(vdev, i)) {
1307             break;
1308         }
1309     }
1310     dev->routes.num_routes = i;
1311     return fsc->add_adapter_routes(fs, &dev->routes);
1312 }
1313 
1314 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1315 {
1316     S390FLICState *fs = s390_get_flic();
1317     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1318 
1319     fsc->release_adapter_routes(fs, &dev->routes);
1320 }
1321 
1322 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1323 {
1324     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1325     VirtQueue *vq = virtio_get_queue(vdev, n);
1326     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1327 
1328     return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1329                                               dev->routes.gsi[n]);
1330 }
1331 
1332 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1333 {
1334     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1335     VirtQueue *vq = virtio_get_queue(vdev, n);
1336     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1337     int ret;
1338 
1339     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1340                                                 dev->routes.gsi[n]);
1341     assert(ret == 0);
1342 }
1343 
1344 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1345                                          bool assign, bool with_irqfd)
1346 {
1347     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1348     VirtQueue *vq = virtio_get_queue(vdev, n);
1349     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1350     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1351 
1352     if (assign) {
1353         int r = event_notifier_init(notifier, 0);
1354 
1355         if (r < 0) {
1356             return r;
1357         }
1358         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1359         if (with_irqfd) {
1360             r = virtio_ccw_add_irqfd(dev, n);
1361             if (r) {
1362                 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1363                                                            with_irqfd);
1364                 return r;
1365             }
1366         }
1367         /*
1368          * We do not support individual masking for channel devices, so we
1369          * need to manually trigger any guest masking callbacks here.
1370          */
1371         if (k->guest_notifier_mask) {
1372             k->guest_notifier_mask(vdev, n, false);
1373         }
1374         /* get lost events and re-inject */
1375         if (k->guest_notifier_pending &&
1376             k->guest_notifier_pending(vdev, n)) {
1377             event_notifier_set(notifier);
1378         }
1379     } else {
1380         if (k->guest_notifier_mask) {
1381             k->guest_notifier_mask(vdev, n, true);
1382         }
1383         if (with_irqfd) {
1384             virtio_ccw_remove_irqfd(dev, n);
1385         }
1386         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1387         event_notifier_cleanup(notifier);
1388     }
1389     return 0;
1390 }
1391 
1392 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1393                                           bool assigned)
1394 {
1395     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1396     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1397     bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1398     int r, n;
1399 
1400     if (with_irqfd && assigned) {
1401         /* irq routes need to be set up before assigning irqfds */
1402         r = virtio_ccw_setup_irqroutes(dev, nvqs);
1403         if (r < 0) {
1404             goto irqroute_error;
1405         }
1406     }
1407     for (n = 0; n < nvqs; n++) {
1408         if (!virtio_queue_get_num(vdev, n)) {
1409             break;
1410         }
1411         r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1412         if (r < 0) {
1413             goto assign_error;
1414         }
1415     }
1416     if (with_irqfd && !assigned) {
1417         /* release irq routes after irqfds have been released */
1418         virtio_ccw_release_irqroutes(dev, nvqs);
1419     }
1420     return 0;
1421 
1422 assign_error:
1423     while (--n >= 0) {
1424         virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1425     }
1426 irqroute_error:
1427     if (with_irqfd && assigned) {
1428         virtio_ccw_release_irqroutes(dev, nvqs);
1429     }
1430     return r;
1431 }
1432 
1433 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1434 {
1435     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1436     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1437 
1438     qemu_put_be16(f, virtio_queue_vector(vdev, n));
1439 }
1440 
1441 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1442 {
1443     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1444     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1445     uint16_t vector;
1446 
1447     qemu_get_be16s(f, &vector);
1448     virtio_queue_set_vector(vdev, n , vector);
1449 
1450     return 0;
1451 }
1452 
1453 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1454 {
1455     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1456     SubchDev *s = dev->sch;
1457     VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1458 
1459     subch_device_save(s, f);
1460     if (dev->indicators != NULL) {
1461         qemu_put_be32(f, dev->indicators->len);
1462         qemu_put_be64(f, dev->indicators->addr);
1463     } else {
1464         qemu_put_be32(f, 0);
1465         qemu_put_be64(f, 0UL);
1466     }
1467     if (dev->indicators2 != NULL) {
1468         qemu_put_be32(f, dev->indicators2->len);
1469         qemu_put_be64(f, dev->indicators2->addr);
1470     } else {
1471         qemu_put_be32(f, 0);
1472         qemu_put_be64(f, 0UL);
1473     }
1474     if (dev->summary_indicator != NULL) {
1475         qemu_put_be32(f, dev->summary_indicator->len);
1476         qemu_put_be64(f, dev->summary_indicator->addr);
1477     } else {
1478         qemu_put_be32(f, 0);
1479         qemu_put_be64(f, 0UL);
1480     }
1481     qemu_put_be16(f, vdev->config_vector);
1482     qemu_put_be64(f, dev->routes.adapter.ind_offset);
1483     qemu_put_byte(f, dev->thinint_isc);
1484     qemu_put_be32(f, dev->revision);
1485 }
1486 
1487 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1488 {
1489     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1490     SubchDev *s = dev->sch;
1491     VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1492     int len;
1493 
1494     s->driver_data = dev;
1495     subch_device_load(s, f);
1496     len = qemu_get_be32(f);
1497     if (len != 0) {
1498         dev->indicators = get_indicator(qemu_get_be64(f), len);
1499     } else {
1500         qemu_get_be64(f);
1501         dev->indicators = NULL;
1502     }
1503     len = qemu_get_be32(f);
1504     if (len != 0) {
1505         dev->indicators2 = get_indicator(qemu_get_be64(f), len);
1506     } else {
1507         qemu_get_be64(f);
1508         dev->indicators2 = NULL;
1509     }
1510     len = qemu_get_be32(f);
1511     if (len != 0) {
1512         dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
1513     } else {
1514         qemu_get_be64(f);
1515         dev->summary_indicator = NULL;
1516     }
1517     qemu_get_be16s(f, &vdev->config_vector);
1518     dev->routes.adapter.ind_offset = qemu_get_be64(f);
1519     dev->thinint_isc = qemu_get_byte(f);
1520     dev->revision = qemu_get_be32(f);
1521     if (s->thinint_active) {
1522         return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
1523                                        dev->thinint_isc, true, false,
1524                                        &dev->routes.adapter.adapter_id);
1525     }
1526 
1527     return 0;
1528 }
1529 
1530 /* This is called by virtio-bus just after the device is plugged. */
1531 static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1532 {
1533     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1534     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1535     SubchDev *sch = dev->sch;
1536     int n = virtio_get_num_queues(vdev);
1537 
1538     if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
1539         error_setg(errp, "The nubmer of virtqueues %d "
1540                    "exceeds ccw limit %d", n,
1541                    VIRTIO_CCW_QUEUE_MAX);
1542         return;
1543     }
1544 
1545     if (!kvm_eventfds_enabled()) {
1546         dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
1547     }
1548 
1549     sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1550 
1551     if (dev->max_rev >= 1) {
1552         virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1553     }
1554 
1555     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1556                           d->hotplugged, 1);
1557 }
1558 
1559 static void virtio_ccw_post_plugged(DeviceState *d, Error **errp)
1560 {
1561    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1562    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1563 
1564    if (!virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1565         /* A backend didn't support modern virtio. */
1566        dev->max_rev = 0;
1567    }
1568 }
1569 
1570 static void virtio_ccw_device_unplugged(DeviceState *d)
1571 {
1572     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1573 
1574     virtio_ccw_stop_ioeventfd(dev);
1575 }
1576 /**************** Virtio-ccw Bus Device Descriptions *******************/
1577 
1578 static Property virtio_ccw_net_properties[] = {
1579     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1580     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1581                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1582     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1583                        VIRTIO_CCW_MAX_REV),
1584     DEFINE_PROP_END_OF_LIST(),
1585 };
1586 
1587 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1588 {
1589     DeviceClass *dc = DEVICE_CLASS(klass);
1590     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1591 
1592     k->realize = virtio_ccw_net_realize;
1593     k->exit = virtio_ccw_exit;
1594     dc->reset = virtio_ccw_reset;
1595     dc->props = virtio_ccw_net_properties;
1596     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1597 }
1598 
1599 static const TypeInfo virtio_ccw_net = {
1600     .name          = TYPE_VIRTIO_NET_CCW,
1601     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1602     .instance_size = sizeof(VirtIONetCcw),
1603     .instance_init = virtio_ccw_net_instance_init,
1604     .class_init    = virtio_ccw_net_class_init,
1605 };
1606 
1607 static Property virtio_ccw_blk_properties[] = {
1608     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1609     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1610                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1611     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1612                        VIRTIO_CCW_MAX_REV),
1613     DEFINE_PROP_END_OF_LIST(),
1614 };
1615 
1616 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1617 {
1618     DeviceClass *dc = DEVICE_CLASS(klass);
1619     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1620 
1621     k->realize = virtio_ccw_blk_realize;
1622     k->exit = virtio_ccw_exit;
1623     dc->reset = virtio_ccw_reset;
1624     dc->props = virtio_ccw_blk_properties;
1625     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1626 }
1627 
1628 static const TypeInfo virtio_ccw_blk = {
1629     .name          = TYPE_VIRTIO_BLK_CCW,
1630     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1631     .instance_size = sizeof(VirtIOBlkCcw),
1632     .instance_init = virtio_ccw_blk_instance_init,
1633     .class_init    = virtio_ccw_blk_class_init,
1634 };
1635 
1636 static Property virtio_ccw_serial_properties[] = {
1637     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1638     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1639                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1640     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1641                        VIRTIO_CCW_MAX_REV),
1642     DEFINE_PROP_END_OF_LIST(),
1643 };
1644 
1645 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1646 {
1647     DeviceClass *dc = DEVICE_CLASS(klass);
1648     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1649 
1650     k->realize = virtio_ccw_serial_realize;
1651     k->exit = virtio_ccw_exit;
1652     dc->reset = virtio_ccw_reset;
1653     dc->props = virtio_ccw_serial_properties;
1654     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1655 }
1656 
1657 static const TypeInfo virtio_ccw_serial = {
1658     .name          = TYPE_VIRTIO_SERIAL_CCW,
1659     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1660     .instance_size = sizeof(VirtioSerialCcw),
1661     .instance_init = virtio_ccw_serial_instance_init,
1662     .class_init    = virtio_ccw_serial_class_init,
1663 };
1664 
1665 static Property virtio_ccw_balloon_properties[] = {
1666     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1667     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1668                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1669     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1670                        VIRTIO_CCW_MAX_REV),
1671     DEFINE_PROP_END_OF_LIST(),
1672 };
1673 
1674 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1675 {
1676     DeviceClass *dc = DEVICE_CLASS(klass);
1677     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1678 
1679     k->realize = virtio_ccw_balloon_realize;
1680     k->exit = virtio_ccw_exit;
1681     dc->reset = virtio_ccw_reset;
1682     dc->props = virtio_ccw_balloon_properties;
1683     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1684 }
1685 
1686 static const TypeInfo virtio_ccw_balloon = {
1687     .name          = TYPE_VIRTIO_BALLOON_CCW,
1688     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1689     .instance_size = sizeof(VirtIOBalloonCcw),
1690     .instance_init = virtio_ccw_balloon_instance_init,
1691     .class_init    = virtio_ccw_balloon_class_init,
1692 };
1693 
1694 static Property virtio_ccw_scsi_properties[] = {
1695     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1696     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1697                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1698     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1699                        VIRTIO_CCW_MAX_REV),
1700     DEFINE_PROP_END_OF_LIST(),
1701 };
1702 
1703 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1704 {
1705     DeviceClass *dc = DEVICE_CLASS(klass);
1706     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1707 
1708     k->realize = virtio_ccw_scsi_realize;
1709     k->exit = virtio_ccw_exit;
1710     dc->reset = virtio_ccw_reset;
1711     dc->props = virtio_ccw_scsi_properties;
1712     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1713 }
1714 
1715 static const TypeInfo virtio_ccw_scsi = {
1716     .name          = TYPE_VIRTIO_SCSI_CCW,
1717     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1718     .instance_size = sizeof(VirtIOSCSICcw),
1719     .instance_init = virtio_ccw_scsi_instance_init,
1720     .class_init    = virtio_ccw_scsi_class_init,
1721 };
1722 
1723 #ifdef CONFIG_VHOST_SCSI
1724 static Property vhost_ccw_scsi_properties[] = {
1725     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1726     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1727                        VIRTIO_CCW_MAX_REV),
1728     DEFINE_PROP_END_OF_LIST(),
1729 };
1730 
1731 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1732 {
1733     DeviceClass *dc = DEVICE_CLASS(klass);
1734     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1735 
1736     k->realize = vhost_ccw_scsi_realize;
1737     k->exit = virtio_ccw_exit;
1738     dc->reset = virtio_ccw_reset;
1739     dc->props = vhost_ccw_scsi_properties;
1740     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1741 }
1742 
1743 static const TypeInfo vhost_ccw_scsi = {
1744     .name          = TYPE_VHOST_SCSI_CCW,
1745     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1746     .instance_size = sizeof(VHostSCSICcw),
1747     .instance_init = vhost_ccw_scsi_instance_init,
1748     .class_init    = vhost_ccw_scsi_class_init,
1749 };
1750 #endif
1751 
1752 static void virtio_ccw_rng_instance_init(Object *obj)
1753 {
1754     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1755 
1756     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1757                                 TYPE_VIRTIO_RNG);
1758     object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1759                               "rng", &error_abort);
1760 }
1761 
1762 static Property virtio_ccw_rng_properties[] = {
1763     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1764     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1765                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1766     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1767                        VIRTIO_CCW_MAX_REV),
1768     DEFINE_PROP_END_OF_LIST(),
1769 };
1770 
1771 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1772 {
1773     DeviceClass *dc = DEVICE_CLASS(klass);
1774     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1775 
1776     k->realize = virtio_ccw_rng_realize;
1777     k->exit = virtio_ccw_exit;
1778     dc->reset = virtio_ccw_reset;
1779     dc->props = virtio_ccw_rng_properties;
1780     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1781 }
1782 
1783 static const TypeInfo virtio_ccw_rng = {
1784     .name          = TYPE_VIRTIO_RNG_CCW,
1785     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1786     .instance_size = sizeof(VirtIORNGCcw),
1787     .instance_init = virtio_ccw_rng_instance_init,
1788     .class_init    = virtio_ccw_rng_class_init,
1789 };
1790 
1791 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1792 {
1793     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1794 
1795     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1796     virtio_ccw_device_realize(_dev, errp);
1797 }
1798 
1799 static int virtio_ccw_busdev_exit(DeviceState *dev)
1800 {
1801     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1802     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1803 
1804     return _info->exit(_dev);
1805 }
1806 
1807 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1808                                      DeviceState *dev, Error **errp)
1809 {
1810     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1811     SubchDev *sch = _dev->sch;
1812 
1813     virtio_ccw_stop_ioeventfd(_dev);
1814 
1815     /*
1816      * We should arrive here only for device_del, since we don't support
1817      * direct hot(un)plug of channels, but only through virtio.
1818      */
1819     assert(sch != NULL);
1820     /* Subchannel is now disabled and no longer valid. */
1821     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1822                                      PMCW_FLAGS_MASK_DNV);
1823 
1824     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1825 
1826     object_unparent(OBJECT(dev));
1827 }
1828 
1829 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1830 {
1831     DeviceClass *dc = DEVICE_CLASS(klass);
1832 
1833     dc->realize = virtio_ccw_busdev_realize;
1834     dc->exit = virtio_ccw_busdev_exit;
1835     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1836 }
1837 
1838 static const TypeInfo virtio_ccw_device_info = {
1839     .name = TYPE_VIRTIO_CCW_DEVICE,
1840     .parent = TYPE_DEVICE,
1841     .instance_size = sizeof(VirtioCcwDevice),
1842     .class_init = virtio_ccw_device_class_init,
1843     .class_size = sizeof(VirtIOCCWDeviceClass),
1844     .abstract = true,
1845 };
1846 
1847 /***************** Virtual-css Bus Bridge Device ********************/
1848 /* Only required to have the virtio bus as child in the system bus */
1849 
1850 static int virtual_css_bridge_init(SysBusDevice *dev)
1851 {
1852     /* nothing */
1853     return 0;
1854 }
1855 
1856 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1857 {
1858     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1859     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1860     DeviceClass *dc = DEVICE_CLASS(klass);
1861 
1862     k->init = virtual_css_bridge_init;
1863     hc->unplug = virtio_ccw_busdev_unplug;
1864     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1865 }
1866 
1867 static const TypeInfo virtual_css_bridge_info = {
1868     .name          = "virtual-css-bridge",
1869     .parent        = TYPE_SYS_BUS_DEVICE,
1870     .instance_size = sizeof(SysBusDevice),
1871     .class_init    = virtual_css_bridge_class_init,
1872     .interfaces = (InterfaceInfo[]) {
1873         { TYPE_HOTPLUG_HANDLER },
1874         { }
1875     }
1876 };
1877 
1878 /* virtio-ccw-bus */
1879 
1880 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1881                                VirtioCcwDevice *dev)
1882 {
1883     DeviceState *qdev = DEVICE(dev);
1884     char virtio_bus_name[] = "virtio-bus";
1885 
1886     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1887                         qdev, virtio_bus_name);
1888 }
1889 
1890 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1891 {
1892     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1893     BusClass *bus_class = BUS_CLASS(klass);
1894 
1895     bus_class->max_dev = 1;
1896     k->notify = virtio_ccw_notify;
1897     k->vmstate_change = virtio_ccw_vmstate_change;
1898     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1899     k->set_host_notifier = virtio_ccw_set_host_notifier;
1900     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1901     k->save_queue = virtio_ccw_save_queue;
1902     k->load_queue = virtio_ccw_load_queue;
1903     k->save_config = virtio_ccw_save_config;
1904     k->load_config = virtio_ccw_load_config;
1905     k->device_plugged = virtio_ccw_device_plugged;
1906     k->post_plugged = virtio_ccw_post_plugged;
1907     k->device_unplugged = virtio_ccw_device_unplugged;
1908 }
1909 
1910 static const TypeInfo virtio_ccw_bus_info = {
1911     .name = TYPE_VIRTIO_CCW_BUS,
1912     .parent = TYPE_VIRTIO_BUS,
1913     .instance_size = sizeof(VirtioCcwBusState),
1914     .class_init = virtio_ccw_bus_class_init,
1915 };
1916 
1917 #ifdef CONFIG_VIRTFS
1918 static Property virtio_ccw_9p_properties[] = {
1919     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1920     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1921             VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1922     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1923                        VIRTIO_CCW_MAX_REV),
1924     DEFINE_PROP_END_OF_LIST(),
1925 };
1926 
1927 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1928 {
1929     V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1930     DeviceState *vdev = DEVICE(&dev->vdev);
1931     Error *err = NULL;
1932 
1933     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1934     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1935     if (err) {
1936         error_propagate(errp, err);
1937     }
1938 }
1939 
1940 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1941 {
1942     DeviceClass *dc = DEVICE_CLASS(klass);
1943     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1944 
1945     k->exit = virtio_ccw_exit;
1946     k->realize = virtio_ccw_9p_realize;
1947     dc->reset = virtio_ccw_reset;
1948     dc->props = virtio_ccw_9p_properties;
1949     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1950 }
1951 
1952 static void virtio_ccw_9p_instance_init(Object *obj)
1953 {
1954     V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1955 
1956     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1957                                 TYPE_VIRTIO_9P);
1958 }
1959 
1960 static const TypeInfo virtio_ccw_9p_info = {
1961     .name          = TYPE_VIRTIO_9P_CCW,
1962     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1963     .instance_size = sizeof(V9fsCCWState),
1964     .instance_init = virtio_ccw_9p_instance_init,
1965     .class_init    = virtio_ccw_9p_class_init,
1966 };
1967 #endif
1968 
1969 static void virtio_ccw_register(void)
1970 {
1971     type_register_static(&virtio_ccw_bus_info);
1972     type_register_static(&virtual_css_bus_info);
1973     type_register_static(&virtio_ccw_device_info);
1974     type_register_static(&virtio_ccw_serial);
1975     type_register_static(&virtio_ccw_blk);
1976     type_register_static(&virtio_ccw_net);
1977     type_register_static(&virtio_ccw_balloon);
1978     type_register_static(&virtio_ccw_scsi);
1979 #ifdef CONFIG_VHOST_SCSI
1980     type_register_static(&vhost_ccw_scsi);
1981 #endif
1982     type_register_static(&virtio_ccw_rng);
1983     type_register_static(&virtual_css_bridge_info);
1984 #ifdef CONFIG_VIRTFS
1985     type_register_static(&virtio_ccw_9p_info);
1986 #endif
1987 }
1988 
1989 type_init(virtio_ccw_register)
1990