xref: /openbmc/qemu/hw/s390x/virtio-ccw.c (revision 44f552964714a41ccd41b5e8ac4cbd2478249db1)
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, VIRTIO_NO_VECTOR);
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, Error **errp)
646 {
647     unsigned int cssid = 0;
648     unsigned int ssid = 0;
649     unsigned int schid;
650     unsigned int devno;
651     bool have_devno = false;
652     bool found = false;
653     SubchDev *sch;
654     int num;
655     Error *err = NULL;
656     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(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 
770     if (k->realize) {
771         k->realize(dev, &err);
772     }
773     if (err) {
774         error_propagate(errp, err);
775         css_subch_assign(cssid, ssid, schid, devno, NULL);
776         goto out_err;
777     }
778 
779     return;
780 
781 out_err:
782     dev->sch = NULL;
783     g_free(sch);
784 }
785 
786 static int virtio_ccw_exit(VirtioCcwDevice *dev)
787 {
788     SubchDev *sch = dev->sch;
789 
790     if (sch) {
791         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
792         g_free(sch);
793     }
794     if (dev->indicators) {
795         release_indicator(&dev->routes.adapter, dev->indicators);
796         dev->indicators = NULL;
797     }
798     return 0;
799 }
800 
801 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
802 {
803     DeviceState *qdev = DEVICE(ccw_dev);
804     VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
805     DeviceState *vdev = DEVICE(&dev->vdev);
806     Error *err = NULL;
807 
808     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
809                                   object_get_typename(OBJECT(qdev)));
810     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
811     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
812     if (err) {
813         error_propagate(errp, err);
814     }
815 }
816 
817 static void virtio_ccw_net_instance_init(Object *obj)
818 {
819     VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
820 
821     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
822                                 TYPE_VIRTIO_NET);
823     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
824                               "bootindex", &error_abort);
825 }
826 
827 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
828 {
829     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
830     DeviceState *vdev = DEVICE(&dev->vdev);
831     Error *err = NULL;
832 
833     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
834     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
835     if (err) {
836         error_propagate(errp, err);
837     }
838 }
839 
840 static void virtio_ccw_blk_instance_init(Object *obj)
841 {
842     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
843 
844     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
845                                 TYPE_VIRTIO_BLK);
846     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
847                               &error_abort);
848     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
849                               "bootindex", &error_abort);
850 }
851 
852 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
853 {
854     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
855     DeviceState *vdev = DEVICE(&dev->vdev);
856     DeviceState *proxy = DEVICE(ccw_dev);
857     Error *err = NULL;
858     char *bus_name;
859 
860     /*
861      * For command line compatibility, this sets the virtio-serial-device bus
862      * name as before.
863      */
864     if (proxy->id) {
865         bus_name = g_strdup_printf("%s.0", proxy->id);
866         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
867         g_free(bus_name);
868     }
869 
870     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
871     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
872     if (err) {
873         error_propagate(errp, err);
874     }
875 }
876 
877 
878 static void virtio_ccw_serial_instance_init(Object *obj)
879 {
880     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
881 
882     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
883                                 TYPE_VIRTIO_SERIAL);
884 }
885 
886 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
887 {
888     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
889     DeviceState *vdev = DEVICE(&dev->vdev);
890     Error *err = NULL;
891 
892     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
893     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
894     if (err) {
895         error_propagate(errp, err);
896     }
897 }
898 
899 static void balloon_ccw_stats_get_all(Object *obj, struct Visitor *v,
900                                       void *opaque, const char *name,
901                                       Error **errp)
902 {
903     VirtIOBalloonCcw *dev = opaque;
904     object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
905 }
906 
907 static void balloon_ccw_stats_get_poll_interval(Object *obj, struct Visitor *v,
908                                                 void *opaque, const char *name,
909                                                 Error **errp)
910 {
911     VirtIOBalloonCcw *dev = opaque;
912     object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
913                         errp);
914 }
915 
916 static void balloon_ccw_stats_set_poll_interval(Object *obj, struct Visitor *v,
917                                                 void *opaque, const char *name,
918                                                 Error **errp)
919 {
920     VirtIOBalloonCcw *dev = opaque;
921     object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
922                         errp);
923 }
924 
925 static void virtio_ccw_balloon_instance_init(Object *obj)
926 {
927     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
928     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
929                                 TYPE_VIRTIO_BALLOON);
930     object_property_add(obj, "guest-stats", "guest statistics",
931                         balloon_ccw_stats_get_all, NULL, NULL, dev, NULL);
932 
933     object_property_add(obj, "guest-stats-polling-interval", "int",
934                         balloon_ccw_stats_get_poll_interval,
935                         balloon_ccw_stats_set_poll_interval,
936                         NULL, dev, NULL);
937 }
938 
939 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
940 {
941     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
942     DeviceState *vdev = DEVICE(&dev->vdev);
943     DeviceState *qdev = DEVICE(ccw_dev);
944     Error *err = NULL;
945     char *bus_name;
946 
947     /*
948      * For command line compatibility, this sets the virtio-scsi-device bus
949      * name as before.
950      */
951     if (qdev->id) {
952         bus_name = g_strdup_printf("%s.0", qdev->id);
953         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
954         g_free(bus_name);
955     }
956 
957     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
958     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
959     if (err) {
960         error_propagate(errp, err);
961     }
962 }
963 
964 static void virtio_ccw_scsi_instance_init(Object *obj)
965 {
966     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
967 
968     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
969                                 TYPE_VIRTIO_SCSI);
970     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
971                               &error_abort);
972 }
973 
974 #ifdef CONFIG_VHOST_SCSI
975 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
976 {
977     VHostSCSICcw *dev = VHOST_SCSI_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     }
986 }
987 
988 static void vhost_ccw_scsi_instance_init(Object *obj)
989 {
990     VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
991 
992     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
993                                 TYPE_VHOST_SCSI);
994 }
995 #endif
996 
997 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
998 {
999     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
1000     DeviceState *vdev = DEVICE(&dev->vdev);
1001     Error *err = NULL;
1002 
1003     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1004     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1005     if (err) {
1006         error_propagate(errp, err);
1007         return;
1008     }
1009 
1010     object_property_set_link(OBJECT(dev),
1011                              OBJECT(dev->vdev.conf.rng), "rng",
1012                              NULL);
1013 }
1014 
1015 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1016  * be careful and test performance if you change this.
1017  */
1018 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
1019 {
1020     return container_of(d, VirtioCcwDevice, parent_obj);
1021 }
1022 
1023 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
1024                                      uint8_t to_be_set)
1025 {
1026     uint8_t ind_old, ind_new;
1027     hwaddr len = 1;
1028     uint8_t *ind_addr;
1029 
1030     ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
1031     if (!ind_addr) {
1032         error_report("%s(%x.%x.%04x): unable to access indicator",
1033                      __func__, sch->cssid, sch->ssid, sch->schid);
1034         return -1;
1035     }
1036     do {
1037         ind_old = *ind_addr;
1038         ind_new = ind_old | to_be_set;
1039     } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
1040     cpu_physical_memory_unmap(ind_addr, len, 1, len);
1041 
1042     return ind_old;
1043 }
1044 
1045 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
1046 {
1047     VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1048     SubchDev *sch = dev->sch;
1049     uint64_t indicators;
1050 
1051     if (vector >= 128) {
1052         return;
1053     }
1054 
1055     if (vector < VIRTIO_PCI_QUEUE_MAX) {
1056         if (!dev->indicators) {
1057             return;
1058         }
1059         if (sch->thinint_active) {
1060             /*
1061              * In the adapter interrupt case, indicators points to a
1062              * memory area that may be (way) larger than 64 bit and
1063              * ind_bit indicates the start of the indicators in a big
1064              * endian notation.
1065              */
1066             uint64_t ind_bit = dev->routes.adapter.ind_offset;
1067 
1068             virtio_set_ind_atomic(sch, dev->indicators->addr +
1069                                   (ind_bit + vector) / 8,
1070                                   0x80 >> ((ind_bit + vector) % 8));
1071             if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1072                                        0x01)) {
1073                 css_adapter_interrupt(dev->thinint_isc);
1074             }
1075         } else {
1076             indicators = address_space_ldq(&address_space_memory,
1077                                            dev->indicators->addr,
1078                                            MEMTXATTRS_UNSPECIFIED,
1079                                            NULL);
1080             indicators |= 1ULL << vector;
1081             address_space_stq(&address_space_memory, dev->indicators->addr,
1082                               indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1083             css_conditional_io_interrupt(sch);
1084         }
1085     } else {
1086         if (!dev->indicators2) {
1087             return;
1088         }
1089         vector = 0;
1090         indicators = address_space_ldq(&address_space_memory,
1091                                        dev->indicators2->addr,
1092                                        MEMTXATTRS_UNSPECIFIED,
1093                                        NULL);
1094         indicators |= 1ULL << vector;
1095         address_space_stq(&address_space_memory, dev->indicators2->addr,
1096                           indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1097         css_conditional_io_interrupt(sch);
1098     }
1099 }
1100 
1101 static unsigned virtio_ccw_get_features(DeviceState *d)
1102 {
1103     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1104 
1105     /* Only the first 32 feature bits are used. */
1106     return dev->host_features[0];
1107 }
1108 
1109 static void virtio_ccw_reset(DeviceState *d)
1110 {
1111     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1112     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1113 
1114     virtio_ccw_stop_ioeventfd(dev);
1115     virtio_reset(vdev);
1116     css_reset_sch(dev->sch);
1117     if (dev->indicators) {
1118         release_indicator(&dev->routes.adapter, dev->indicators);
1119         dev->indicators = NULL;
1120     }
1121     if (dev->indicators2) {
1122         release_indicator(&dev->routes.adapter, dev->indicators2);
1123         dev->indicators2 = NULL;
1124     }
1125     if (dev->summary_indicator) {
1126         release_indicator(&dev->routes.adapter, dev->summary_indicator);
1127         dev->summary_indicator = NULL;
1128     }
1129 }
1130 
1131 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1132 {
1133     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1134 
1135     if (running) {
1136         virtio_ccw_start_ioeventfd(dev);
1137     } else {
1138         virtio_ccw_stop_ioeventfd(dev);
1139     }
1140 }
1141 
1142 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1143 {
1144     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1145 
1146     return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1147 }
1148 
1149 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1150 {
1151     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1152 
1153     /* Stop using the generic ioeventfd, we are doing eventfd handling
1154      * ourselves below */
1155     dev->ioeventfd_disabled = assign;
1156     if (assign) {
1157         virtio_ccw_stop_ioeventfd(dev);
1158     }
1159     return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1160 }
1161 
1162 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1163 {
1164     int r;
1165 
1166     if (!dev->sch->thinint_active) {
1167         return -EINVAL;
1168     }
1169 
1170     r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1171     if (r) {
1172         return r;
1173     }
1174     r = map_indicator(&dev->routes.adapter, dev->indicators);
1175     if (r) {
1176         return r;
1177     }
1178     dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1179     dev->routes.adapter.ind_addr = dev->indicators->map;
1180 
1181     return 0;
1182 }
1183 
1184 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1185 {
1186     int i;
1187     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1188     int ret;
1189     S390FLICState *fs = s390_get_flic();
1190     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1191 
1192     ret = virtio_ccw_get_mappings(dev);
1193     if (ret) {
1194         return ret;
1195     }
1196     for (i = 0; i < nvqs; i++) {
1197         if (!virtio_queue_get_num(vdev, i)) {
1198             break;
1199         }
1200     }
1201     dev->routes.num_routes = i;
1202     return fsc->add_adapter_routes(fs, &dev->routes);
1203 }
1204 
1205 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1206 {
1207     S390FLICState *fs = s390_get_flic();
1208     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1209 
1210     fsc->release_adapter_routes(fs, &dev->routes);
1211 }
1212 
1213 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1214 {
1215     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1216     VirtQueue *vq = virtio_get_queue(vdev, n);
1217     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1218 
1219     return kvm_irqchip_add_irqfd_notifier(kvm_state, notifier, NULL,
1220                                           dev->routes.gsi[n]);
1221 }
1222 
1223 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1224 {
1225     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1226     VirtQueue *vq = virtio_get_queue(vdev, n);
1227     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1228     int ret;
1229 
1230     ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, notifier,
1231                                             dev->routes.gsi[n]);
1232     assert(ret == 0);
1233 }
1234 
1235 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1236                                          bool assign, bool with_irqfd)
1237 {
1238     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1239     VirtQueue *vq = virtio_get_queue(vdev, n);
1240     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1241     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1242 
1243     if (assign) {
1244         int r = event_notifier_init(notifier, 0);
1245 
1246         if (r < 0) {
1247             return r;
1248         }
1249         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1250         if (with_irqfd) {
1251             r = virtio_ccw_add_irqfd(dev, n);
1252             if (r) {
1253                 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1254                                                            with_irqfd);
1255                 return r;
1256             }
1257         }
1258         /*
1259          * We do not support individual masking for channel devices, so we
1260          * need to manually trigger any guest masking callbacks here.
1261          */
1262         if (k->guest_notifier_mask) {
1263             k->guest_notifier_mask(vdev, n, false);
1264         }
1265         /* get lost events and re-inject */
1266         if (k->guest_notifier_pending &&
1267             k->guest_notifier_pending(vdev, n)) {
1268             event_notifier_set(notifier);
1269         }
1270     } else {
1271         if (k->guest_notifier_mask) {
1272             k->guest_notifier_mask(vdev, n, true);
1273         }
1274         if (with_irqfd) {
1275             virtio_ccw_remove_irqfd(dev, n);
1276         }
1277         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1278         event_notifier_cleanup(notifier);
1279     }
1280     return 0;
1281 }
1282 
1283 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1284                                           bool assigned)
1285 {
1286     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1287     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1288     bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1289     int r, n;
1290 
1291     if (with_irqfd && assigned) {
1292         /* irq routes need to be set up before assigning irqfds */
1293         r = virtio_ccw_setup_irqroutes(dev, nvqs);
1294         if (r < 0) {
1295             goto irqroute_error;
1296         }
1297     }
1298     for (n = 0; n < nvqs; n++) {
1299         if (!virtio_queue_get_num(vdev, n)) {
1300             break;
1301         }
1302         r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1303         if (r < 0) {
1304             goto assign_error;
1305         }
1306     }
1307     if (with_irqfd && !assigned) {
1308         /* release irq routes after irqfds have been released */
1309         virtio_ccw_release_irqroutes(dev, nvqs);
1310     }
1311     return 0;
1312 
1313 assign_error:
1314     while (--n >= 0) {
1315         virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1316     }
1317 irqroute_error:
1318     if (with_irqfd && assigned) {
1319         virtio_ccw_release_irqroutes(dev, nvqs);
1320     }
1321     return r;
1322 }
1323 
1324 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1325 {
1326     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1327     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1328 
1329     qemu_put_be16(f, virtio_queue_vector(vdev, n));
1330 }
1331 
1332 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1333 {
1334     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1335     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1336     uint16_t vector;
1337 
1338     qemu_get_be16s(f, &vector);
1339     virtio_queue_set_vector(vdev, n , vector);
1340 
1341     return 0;
1342 }
1343 
1344 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1345 {
1346     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1347     SubchDev *s = dev->sch;
1348 
1349     subch_device_save(s, f);
1350     if (dev->indicators != NULL) {
1351         qemu_put_be32(f, dev->indicators->len);
1352         qemu_put_be64(f, dev->indicators->addr);
1353     } else {
1354         qemu_put_be32(f, 0);
1355         qemu_put_be64(f, 0UL);
1356     }
1357     if (dev->indicators2 != NULL) {
1358         qemu_put_be32(f, dev->indicators2->len);
1359         qemu_put_be64(f, dev->indicators2->addr);
1360     } else {
1361         qemu_put_be32(f, 0);
1362         qemu_put_be64(f, 0UL);
1363     }
1364     if (dev->summary_indicator != NULL) {
1365         qemu_put_be32(f, dev->summary_indicator->len);
1366         qemu_put_be64(f, dev->summary_indicator->addr);
1367     } else {
1368         qemu_put_be32(f, 0);
1369         qemu_put_be64(f, 0UL);
1370     }
1371     qemu_put_be64(f, dev->routes.adapter.ind_offset);
1372     qemu_put_byte(f, dev->thinint_isc);
1373 }
1374 
1375 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1376 {
1377     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1378     SubchDev *s = dev->sch;
1379     int len;
1380 
1381     s->driver_data = dev;
1382     subch_device_load(s, f);
1383     len = qemu_get_be32(f);
1384     if (len != 0) {
1385         dev->indicators = get_indicator(qemu_get_be64(f), len);
1386     } else {
1387         qemu_get_be64(f);
1388         dev->indicators = NULL;
1389     }
1390     len = qemu_get_be32(f);
1391     if (len != 0) {
1392         dev->indicators2 = get_indicator(qemu_get_be64(f), len);
1393     } else {
1394         qemu_get_be64(f);
1395         dev->indicators2 = NULL;
1396     }
1397     len = qemu_get_be32(f);
1398     if (len != 0) {
1399         dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
1400     } else {
1401         qemu_get_be64(f);
1402         dev->summary_indicator = NULL;
1403     }
1404     dev->routes.adapter.ind_offset = qemu_get_be64(f);
1405     dev->thinint_isc = qemu_get_byte(f);
1406     if (s->thinint_active) {
1407         return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
1408                                        dev->thinint_isc, true, false,
1409                                        &dev->routes.adapter.adapter_id);
1410     }
1411 
1412     return 0;
1413 }
1414 
1415 /* This is called by virtio-bus just after the device is plugged. */
1416 static void virtio_ccw_device_plugged(DeviceState *d)
1417 {
1418     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1419     SubchDev *sch = dev->sch;
1420 
1421     sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1422 
1423     /* Only the first 32 feature bits are used. */
1424     virtio_add_feature(&dev->host_features[0], VIRTIO_F_NOTIFY_ON_EMPTY);
1425     virtio_add_feature(&dev->host_features[0], VIRTIO_F_BAD_FEATURE);
1426     dev->host_features[0] = virtio_bus_get_vdev_features(&dev->bus,
1427                                                          dev->host_features[0]);
1428 
1429     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1430                           d->hotplugged, 1);
1431 }
1432 
1433 static void virtio_ccw_device_unplugged(DeviceState *d)
1434 {
1435     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1436 
1437     virtio_ccw_stop_ioeventfd(dev);
1438 }
1439 /**************** Virtio-ccw Bus Device Descriptions *******************/
1440 
1441 static Property virtio_ccw_net_properties[] = {
1442     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1443     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1444                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1445     DEFINE_PROP_END_OF_LIST(),
1446 };
1447 
1448 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1449 {
1450     DeviceClass *dc = DEVICE_CLASS(klass);
1451     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1452 
1453     k->realize = virtio_ccw_net_realize;
1454     k->exit = virtio_ccw_exit;
1455     dc->reset = virtio_ccw_reset;
1456     dc->props = virtio_ccw_net_properties;
1457     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1458 }
1459 
1460 static const TypeInfo virtio_ccw_net = {
1461     .name          = TYPE_VIRTIO_NET_CCW,
1462     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1463     .instance_size = sizeof(VirtIONetCcw),
1464     .instance_init = virtio_ccw_net_instance_init,
1465     .class_init    = virtio_ccw_net_class_init,
1466 };
1467 
1468 static Property virtio_ccw_blk_properties[] = {
1469     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1470     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1471                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1472     DEFINE_PROP_END_OF_LIST(),
1473 };
1474 
1475 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1476 {
1477     DeviceClass *dc = DEVICE_CLASS(klass);
1478     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1479 
1480     k->realize = virtio_ccw_blk_realize;
1481     k->exit = virtio_ccw_exit;
1482     dc->reset = virtio_ccw_reset;
1483     dc->props = virtio_ccw_blk_properties;
1484     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1485 }
1486 
1487 static const TypeInfo virtio_ccw_blk = {
1488     .name          = TYPE_VIRTIO_BLK_CCW,
1489     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1490     .instance_size = sizeof(VirtIOBlkCcw),
1491     .instance_init = virtio_ccw_blk_instance_init,
1492     .class_init    = virtio_ccw_blk_class_init,
1493 };
1494 
1495 static Property virtio_ccw_serial_properties[] = {
1496     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1497     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1498                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1499     DEFINE_PROP_END_OF_LIST(),
1500 };
1501 
1502 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1503 {
1504     DeviceClass *dc = DEVICE_CLASS(klass);
1505     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1506 
1507     k->realize = virtio_ccw_serial_realize;
1508     k->exit = virtio_ccw_exit;
1509     dc->reset = virtio_ccw_reset;
1510     dc->props = virtio_ccw_serial_properties;
1511     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1512 }
1513 
1514 static const TypeInfo virtio_ccw_serial = {
1515     .name          = TYPE_VIRTIO_SERIAL_CCW,
1516     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1517     .instance_size = sizeof(VirtioSerialCcw),
1518     .instance_init = virtio_ccw_serial_instance_init,
1519     .class_init    = virtio_ccw_serial_class_init,
1520 };
1521 
1522 static Property virtio_ccw_balloon_properties[] = {
1523     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1524     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1525                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1526     DEFINE_PROP_END_OF_LIST(),
1527 };
1528 
1529 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1530 {
1531     DeviceClass *dc = DEVICE_CLASS(klass);
1532     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1533 
1534     k->realize = virtio_ccw_balloon_realize;
1535     k->exit = virtio_ccw_exit;
1536     dc->reset = virtio_ccw_reset;
1537     dc->props = virtio_ccw_balloon_properties;
1538     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1539 }
1540 
1541 static const TypeInfo virtio_ccw_balloon = {
1542     .name          = TYPE_VIRTIO_BALLOON_CCW,
1543     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1544     .instance_size = sizeof(VirtIOBalloonCcw),
1545     .instance_init = virtio_ccw_balloon_instance_init,
1546     .class_init    = virtio_ccw_balloon_class_init,
1547 };
1548 
1549 static Property virtio_ccw_scsi_properties[] = {
1550     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1551     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1552                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1553     DEFINE_PROP_END_OF_LIST(),
1554 };
1555 
1556 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1557 {
1558     DeviceClass *dc = DEVICE_CLASS(klass);
1559     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1560 
1561     k->realize = virtio_ccw_scsi_realize;
1562     k->exit = virtio_ccw_exit;
1563     dc->reset = virtio_ccw_reset;
1564     dc->props = virtio_ccw_scsi_properties;
1565     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1566 }
1567 
1568 static const TypeInfo virtio_ccw_scsi = {
1569     .name          = TYPE_VIRTIO_SCSI_CCW,
1570     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1571     .instance_size = sizeof(VirtIOSCSICcw),
1572     .instance_init = virtio_ccw_scsi_instance_init,
1573     .class_init    = virtio_ccw_scsi_class_init,
1574 };
1575 
1576 #ifdef CONFIG_VHOST_SCSI
1577 static Property vhost_ccw_scsi_properties[] = {
1578     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1579     DEFINE_PROP_END_OF_LIST(),
1580 };
1581 
1582 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1583 {
1584     DeviceClass *dc = DEVICE_CLASS(klass);
1585     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1586 
1587     k->realize = vhost_ccw_scsi_realize;
1588     k->exit = virtio_ccw_exit;
1589     dc->reset = virtio_ccw_reset;
1590     dc->props = vhost_ccw_scsi_properties;
1591     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1592 }
1593 
1594 static const TypeInfo vhost_ccw_scsi = {
1595     .name          = TYPE_VHOST_SCSI_CCW,
1596     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1597     .instance_size = sizeof(VHostSCSICcw),
1598     .instance_init = vhost_ccw_scsi_instance_init,
1599     .class_init    = vhost_ccw_scsi_class_init,
1600 };
1601 #endif
1602 
1603 static void virtio_ccw_rng_instance_init(Object *obj)
1604 {
1605     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1606 
1607     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1608                                 TYPE_VIRTIO_RNG);
1609     object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1610                               "rng", &error_abort);
1611 }
1612 
1613 static Property virtio_ccw_rng_properties[] = {
1614     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1615     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1616                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1617     DEFINE_PROP_END_OF_LIST(),
1618 };
1619 
1620 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1621 {
1622     DeviceClass *dc = DEVICE_CLASS(klass);
1623     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1624 
1625     k->realize = virtio_ccw_rng_realize;
1626     k->exit = virtio_ccw_exit;
1627     dc->reset = virtio_ccw_reset;
1628     dc->props = virtio_ccw_rng_properties;
1629     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1630 }
1631 
1632 static const TypeInfo virtio_ccw_rng = {
1633     .name          = TYPE_VIRTIO_RNG_CCW,
1634     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1635     .instance_size = sizeof(VirtIORNGCcw),
1636     .instance_init = virtio_ccw_rng_instance_init,
1637     .class_init    = virtio_ccw_rng_class_init,
1638 };
1639 
1640 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1641 {
1642     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1643 
1644     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1645     virtio_ccw_device_realize(_dev, errp);
1646 }
1647 
1648 static int virtio_ccw_busdev_exit(DeviceState *dev)
1649 {
1650     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1651     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1652 
1653     return _info->exit(_dev);
1654 }
1655 
1656 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1657                                      DeviceState *dev, Error **errp)
1658 {
1659     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1660     SubchDev *sch = _dev->sch;
1661 
1662     virtio_ccw_stop_ioeventfd(_dev);
1663 
1664     /*
1665      * We should arrive here only for device_del, since we don't support
1666      * direct hot(un)plug of channels, but only through virtio.
1667      */
1668     assert(sch != NULL);
1669     /* Subchannel is now disabled and no longer valid. */
1670     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1671                                      PMCW_FLAGS_MASK_DNV);
1672 
1673     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1674 
1675     object_unparent(OBJECT(dev));
1676 }
1677 
1678 static Property virtio_ccw_properties[] = {
1679     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1680     DEFINE_PROP_END_OF_LIST(),
1681 };
1682 
1683 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1684 {
1685     DeviceClass *dc = DEVICE_CLASS(klass);
1686 
1687     dc->props = virtio_ccw_properties;
1688     dc->realize = virtio_ccw_busdev_realize;
1689     dc->exit = virtio_ccw_busdev_exit;
1690     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1691 }
1692 
1693 static const TypeInfo virtio_ccw_device_info = {
1694     .name = TYPE_VIRTIO_CCW_DEVICE,
1695     .parent = TYPE_DEVICE,
1696     .instance_size = sizeof(VirtioCcwDevice),
1697     .class_init = virtio_ccw_device_class_init,
1698     .class_size = sizeof(VirtIOCCWDeviceClass),
1699     .abstract = true,
1700 };
1701 
1702 /***************** Virtual-css Bus Bridge Device ********************/
1703 /* Only required to have the virtio bus as child in the system bus */
1704 
1705 static int virtual_css_bridge_init(SysBusDevice *dev)
1706 {
1707     /* nothing */
1708     return 0;
1709 }
1710 
1711 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1712 {
1713     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1714     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1715     DeviceClass *dc = DEVICE_CLASS(klass);
1716 
1717     k->init = virtual_css_bridge_init;
1718     hc->unplug = virtio_ccw_busdev_unplug;
1719     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1720 }
1721 
1722 static const TypeInfo virtual_css_bridge_info = {
1723     .name          = "virtual-css-bridge",
1724     .parent        = TYPE_SYS_BUS_DEVICE,
1725     .instance_size = sizeof(SysBusDevice),
1726     .class_init    = virtual_css_bridge_class_init,
1727     .interfaces = (InterfaceInfo[]) {
1728         { TYPE_HOTPLUG_HANDLER },
1729         { }
1730     }
1731 };
1732 
1733 /* virtio-ccw-bus */
1734 
1735 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1736                                VirtioCcwDevice *dev)
1737 {
1738     DeviceState *qdev = DEVICE(dev);
1739     char virtio_bus_name[] = "virtio-bus";
1740 
1741     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1742                         qdev, virtio_bus_name);
1743 }
1744 
1745 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1746 {
1747     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1748     BusClass *bus_class = BUS_CLASS(klass);
1749 
1750     bus_class->max_dev = 1;
1751     k->notify = virtio_ccw_notify;
1752     k->get_features = virtio_ccw_get_features;
1753     k->vmstate_change = virtio_ccw_vmstate_change;
1754     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1755     k->set_host_notifier = virtio_ccw_set_host_notifier;
1756     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1757     k->save_queue = virtio_ccw_save_queue;
1758     k->load_queue = virtio_ccw_load_queue;
1759     k->save_config = virtio_ccw_save_config;
1760     k->load_config = virtio_ccw_load_config;
1761     k->device_plugged = virtio_ccw_device_plugged;
1762     k->device_unplugged = virtio_ccw_device_unplugged;
1763 }
1764 
1765 static const TypeInfo virtio_ccw_bus_info = {
1766     .name = TYPE_VIRTIO_CCW_BUS,
1767     .parent = TYPE_VIRTIO_BUS,
1768     .instance_size = sizeof(VirtioCcwBusState),
1769     .class_init = virtio_ccw_bus_class_init,
1770 };
1771 
1772 static void virtio_ccw_register(void)
1773 {
1774     type_register_static(&virtio_ccw_bus_info);
1775     type_register_static(&virtual_css_bus_info);
1776     type_register_static(&virtio_ccw_device_info);
1777     type_register_static(&virtio_ccw_serial);
1778     type_register_static(&virtio_ccw_blk);
1779     type_register_static(&virtio_ccw_net);
1780     type_register_static(&virtio_ccw_balloon);
1781     type_register_static(&virtio_ccw_scsi);
1782 #ifdef CONFIG_VHOST_SCSI
1783     type_register_static(&vhost_ccw_scsi);
1784 #endif
1785     type_register_static(&virtio_ccw_rng);
1786     type_register_static(&virtual_css_bridge_info);
1787 }
1788 
1789 type_init(virtio_ccw_register)
1790