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