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