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