xref: /openbmc/qemu/hw/s390x/virtio-ccw.c (revision 76f4afb4)
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     sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1405 
1406     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1407                           d->hotplugged, 1);
1408 }
1409 
1410 static void virtio_ccw_device_unplugged(DeviceState *d)
1411 {
1412     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1413 
1414     virtio_ccw_stop_ioeventfd(dev);
1415 }
1416 /**************** Virtio-ccw Bus Device Descriptions *******************/
1417 
1418 static Property virtio_ccw_net_properties[] = {
1419     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1420     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1421                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1422     DEFINE_PROP_END_OF_LIST(),
1423 };
1424 
1425 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1426 {
1427     DeviceClass *dc = DEVICE_CLASS(klass);
1428     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1429 
1430     k->realize = virtio_ccw_net_realize;
1431     k->exit = virtio_ccw_exit;
1432     dc->reset = virtio_ccw_reset;
1433     dc->props = virtio_ccw_net_properties;
1434     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1435 }
1436 
1437 static const TypeInfo virtio_ccw_net = {
1438     .name          = TYPE_VIRTIO_NET_CCW,
1439     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1440     .instance_size = sizeof(VirtIONetCcw),
1441     .instance_init = virtio_ccw_net_instance_init,
1442     .class_init    = virtio_ccw_net_class_init,
1443 };
1444 
1445 static Property virtio_ccw_blk_properties[] = {
1446     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1447     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1448                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1449     DEFINE_PROP_END_OF_LIST(),
1450 };
1451 
1452 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1453 {
1454     DeviceClass *dc = DEVICE_CLASS(klass);
1455     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1456 
1457     k->realize = virtio_ccw_blk_realize;
1458     k->exit = virtio_ccw_exit;
1459     dc->reset = virtio_ccw_reset;
1460     dc->props = virtio_ccw_blk_properties;
1461     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1462 }
1463 
1464 static const TypeInfo virtio_ccw_blk = {
1465     .name          = TYPE_VIRTIO_BLK_CCW,
1466     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1467     .instance_size = sizeof(VirtIOBlkCcw),
1468     .instance_init = virtio_ccw_blk_instance_init,
1469     .class_init    = virtio_ccw_blk_class_init,
1470 };
1471 
1472 static Property virtio_ccw_serial_properties[] = {
1473     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1474     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1475                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1476     DEFINE_PROP_END_OF_LIST(),
1477 };
1478 
1479 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1480 {
1481     DeviceClass *dc = DEVICE_CLASS(klass);
1482     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1483 
1484     k->realize = virtio_ccw_serial_realize;
1485     k->exit = virtio_ccw_exit;
1486     dc->reset = virtio_ccw_reset;
1487     dc->props = virtio_ccw_serial_properties;
1488     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1489 }
1490 
1491 static const TypeInfo virtio_ccw_serial = {
1492     .name          = TYPE_VIRTIO_SERIAL_CCW,
1493     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1494     .instance_size = sizeof(VirtioSerialCcw),
1495     .instance_init = virtio_ccw_serial_instance_init,
1496     .class_init    = virtio_ccw_serial_class_init,
1497 };
1498 
1499 static Property virtio_ccw_balloon_properties[] = {
1500     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1501     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1502                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1503     DEFINE_PROP_END_OF_LIST(),
1504 };
1505 
1506 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1507 {
1508     DeviceClass *dc = DEVICE_CLASS(klass);
1509     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1510 
1511     k->realize = virtio_ccw_balloon_realize;
1512     k->exit = virtio_ccw_exit;
1513     dc->reset = virtio_ccw_reset;
1514     dc->props = virtio_ccw_balloon_properties;
1515     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1516 }
1517 
1518 static const TypeInfo virtio_ccw_balloon = {
1519     .name          = TYPE_VIRTIO_BALLOON_CCW,
1520     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1521     .instance_size = sizeof(VirtIOBalloonCcw),
1522     .instance_init = virtio_ccw_balloon_instance_init,
1523     .class_init    = virtio_ccw_balloon_class_init,
1524 };
1525 
1526 static Property virtio_ccw_scsi_properties[] = {
1527     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1528     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1529                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1530     DEFINE_PROP_END_OF_LIST(),
1531 };
1532 
1533 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1534 {
1535     DeviceClass *dc = DEVICE_CLASS(klass);
1536     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1537 
1538     k->realize = virtio_ccw_scsi_realize;
1539     k->exit = virtio_ccw_exit;
1540     dc->reset = virtio_ccw_reset;
1541     dc->props = virtio_ccw_scsi_properties;
1542     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1543 }
1544 
1545 static const TypeInfo virtio_ccw_scsi = {
1546     .name          = TYPE_VIRTIO_SCSI_CCW,
1547     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1548     .instance_size = sizeof(VirtIOSCSICcw),
1549     .instance_init = virtio_ccw_scsi_instance_init,
1550     .class_init    = virtio_ccw_scsi_class_init,
1551 };
1552 
1553 #ifdef CONFIG_VHOST_SCSI
1554 static Property vhost_ccw_scsi_properties[] = {
1555     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1556     DEFINE_PROP_END_OF_LIST(),
1557 };
1558 
1559 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1560 {
1561     DeviceClass *dc = DEVICE_CLASS(klass);
1562     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1563 
1564     k->realize = vhost_ccw_scsi_realize;
1565     k->exit = virtio_ccw_exit;
1566     dc->reset = virtio_ccw_reset;
1567     dc->props = vhost_ccw_scsi_properties;
1568     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1569 }
1570 
1571 static const TypeInfo vhost_ccw_scsi = {
1572     .name          = TYPE_VHOST_SCSI_CCW,
1573     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1574     .instance_size = sizeof(VHostSCSICcw),
1575     .instance_init = vhost_ccw_scsi_instance_init,
1576     .class_init    = vhost_ccw_scsi_class_init,
1577 };
1578 #endif
1579 
1580 static void virtio_ccw_rng_instance_init(Object *obj)
1581 {
1582     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1583 
1584     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1585                                 TYPE_VIRTIO_RNG);
1586     object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1587                               "rng", &error_abort);
1588 }
1589 
1590 static Property virtio_ccw_rng_properties[] = {
1591     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1592     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1593                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1594     DEFINE_PROP_END_OF_LIST(),
1595 };
1596 
1597 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1598 {
1599     DeviceClass *dc = DEVICE_CLASS(klass);
1600     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1601 
1602     k->realize = virtio_ccw_rng_realize;
1603     k->exit = virtio_ccw_exit;
1604     dc->reset = virtio_ccw_reset;
1605     dc->props = virtio_ccw_rng_properties;
1606     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1607 }
1608 
1609 static const TypeInfo virtio_ccw_rng = {
1610     .name          = TYPE_VIRTIO_RNG_CCW,
1611     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1612     .instance_size = sizeof(VirtIORNGCcw),
1613     .instance_init = virtio_ccw_rng_instance_init,
1614     .class_init    = virtio_ccw_rng_class_init,
1615 };
1616 
1617 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1618 {
1619     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1620 
1621     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1622     virtio_ccw_device_realize(_dev, errp);
1623 }
1624 
1625 static int virtio_ccw_busdev_exit(DeviceState *dev)
1626 {
1627     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1628     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1629 
1630     return _info->exit(_dev);
1631 }
1632 
1633 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1634                                      DeviceState *dev, Error **errp)
1635 {
1636     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1637     SubchDev *sch = _dev->sch;
1638 
1639     virtio_ccw_stop_ioeventfd(_dev);
1640 
1641     /*
1642      * We should arrive here only for device_del, since we don't support
1643      * direct hot(un)plug of channels, but only through virtio.
1644      */
1645     assert(sch != NULL);
1646     /* Subchannel is now disabled and no longer valid. */
1647     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1648                                      PMCW_FLAGS_MASK_DNV);
1649 
1650     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1651 
1652     object_unparent(OBJECT(dev));
1653 }
1654 
1655 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1656 {
1657     DeviceClass *dc = DEVICE_CLASS(klass);
1658 
1659     dc->realize = virtio_ccw_busdev_realize;
1660     dc->exit = virtio_ccw_busdev_exit;
1661     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1662 }
1663 
1664 static const TypeInfo virtio_ccw_device_info = {
1665     .name = TYPE_VIRTIO_CCW_DEVICE,
1666     .parent = TYPE_DEVICE,
1667     .instance_size = sizeof(VirtioCcwDevice),
1668     .class_init = virtio_ccw_device_class_init,
1669     .class_size = sizeof(VirtIOCCWDeviceClass),
1670     .abstract = true,
1671 };
1672 
1673 /***************** Virtual-css Bus Bridge Device ********************/
1674 /* Only required to have the virtio bus as child in the system bus */
1675 
1676 static int virtual_css_bridge_init(SysBusDevice *dev)
1677 {
1678     /* nothing */
1679     return 0;
1680 }
1681 
1682 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1683 {
1684     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1685     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1686     DeviceClass *dc = DEVICE_CLASS(klass);
1687 
1688     k->init = virtual_css_bridge_init;
1689     hc->unplug = virtio_ccw_busdev_unplug;
1690     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1691 }
1692 
1693 static const TypeInfo virtual_css_bridge_info = {
1694     .name          = "virtual-css-bridge",
1695     .parent        = TYPE_SYS_BUS_DEVICE,
1696     .instance_size = sizeof(SysBusDevice),
1697     .class_init    = virtual_css_bridge_class_init,
1698     .interfaces = (InterfaceInfo[]) {
1699         { TYPE_HOTPLUG_HANDLER },
1700         { }
1701     }
1702 };
1703 
1704 /* virtio-ccw-bus */
1705 
1706 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1707                                VirtioCcwDevice *dev)
1708 {
1709     DeviceState *qdev = DEVICE(dev);
1710     char virtio_bus_name[] = "virtio-bus";
1711 
1712     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1713                         qdev, virtio_bus_name);
1714 }
1715 
1716 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1717 {
1718     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1719     BusClass *bus_class = BUS_CLASS(klass);
1720 
1721     bus_class->max_dev = 1;
1722     k->notify = virtio_ccw_notify;
1723     k->vmstate_change = virtio_ccw_vmstate_change;
1724     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1725     k->set_host_notifier = virtio_ccw_set_host_notifier;
1726     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1727     k->save_queue = virtio_ccw_save_queue;
1728     k->load_queue = virtio_ccw_load_queue;
1729     k->save_config = virtio_ccw_save_config;
1730     k->load_config = virtio_ccw_load_config;
1731     k->device_plugged = virtio_ccw_device_plugged;
1732     k->device_unplugged = virtio_ccw_device_unplugged;
1733 }
1734 
1735 static const TypeInfo virtio_ccw_bus_info = {
1736     .name = TYPE_VIRTIO_CCW_BUS,
1737     .parent = TYPE_VIRTIO_BUS,
1738     .instance_size = sizeof(VirtioCcwBusState),
1739     .class_init = virtio_ccw_bus_class_init,
1740 };
1741 
1742 #ifdef CONFIG_VIRTFS
1743 static Property virtio_ccw_9p_properties[] = {
1744     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1745     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1746             VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1747     DEFINE_PROP_END_OF_LIST(),
1748 };
1749 
1750 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1751 {
1752     V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1753     DeviceState *vdev = DEVICE(&dev->vdev);
1754     Error *err = NULL;
1755 
1756     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1757     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1758     if (err) {
1759         error_propagate(errp, err);
1760     }
1761 }
1762 
1763 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1764 {
1765     DeviceClass *dc = DEVICE_CLASS(klass);
1766     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1767 
1768     k->exit = virtio_ccw_exit;
1769     k->realize = virtio_ccw_9p_realize;
1770     dc->reset = virtio_ccw_reset;
1771     dc->props = virtio_ccw_9p_properties;
1772     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1773 }
1774 
1775 static void virtio_ccw_9p_instance_init(Object *obj)
1776 {
1777     V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1778 
1779     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1780                                 TYPE_VIRTIO_9P);
1781 }
1782 
1783 static const TypeInfo virtio_ccw_9p_info = {
1784     .name          = TYPE_VIRTIO_9P_CCW,
1785     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1786     .instance_size = sizeof(V9fsCCWState),
1787     .instance_init = virtio_ccw_9p_instance_init,
1788     .class_init    = virtio_ccw_9p_class_init,
1789 };
1790 #endif
1791 
1792 static void virtio_ccw_register(void)
1793 {
1794     type_register_static(&virtio_ccw_bus_info);
1795     type_register_static(&virtual_css_bus_info);
1796     type_register_static(&virtio_ccw_device_info);
1797     type_register_static(&virtio_ccw_serial);
1798     type_register_static(&virtio_ccw_blk);
1799     type_register_static(&virtio_ccw_net);
1800     type_register_static(&virtio_ccw_balloon);
1801     type_register_static(&virtio_ccw_scsi);
1802 #ifdef CONFIG_VHOST_SCSI
1803     type_register_static(&vhost_ccw_scsi);
1804 #endif
1805     type_register_static(&virtio_ccw_rng);
1806     type_register_static(&virtual_css_bridge_info);
1807 #ifdef CONFIG_VIRTFS
1808     type_register_static(&virtio_ccw_9p_info);
1809 #endif
1810 }
1811 
1812 type_init(virtio_ccw_register)
1813