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