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