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