xref: /openbmc/qemu/hw/s390x/virtio-ccw.c (revision 0399a381)
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->orb = NULL;
563     sch->thinint_active = false;
564     /*
565      * Use a device number if provided. Otherwise, fall back to subchannel
566      * number.
567      */
568     if (dev->bus_id) {
569         num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
570         if (num == 3) {
571             if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
572                 ret = -EINVAL;
573                 error_report("Invalid cssid or ssid: cssid %x, ssid %x",
574                              cssid, ssid);
575                 goto out_err;
576             }
577             /* Enforce use of virtual cssid. */
578             if (cssid != VIRTUAL_CSSID) {
579                 ret = -EINVAL;
580                 error_report("cssid %x not valid for virtio devices", cssid);
581                 goto out_err;
582             }
583             if (css_devno_used(cssid, ssid, devno)) {
584                 ret = -EEXIST;
585                 error_report("Device %x.%x.%04x already exists", cssid, ssid,
586                              devno);
587                 goto out_err;
588             }
589             sch->cssid = cssid;
590             sch->ssid = ssid;
591             sch->devno = devno;
592             have_devno = true;
593         } else {
594             ret = -EINVAL;
595             error_report("Malformed devno parameter '%s'", dev->bus_id);
596             goto out_err;
597         }
598     }
599 
600     /* Find the next free id. */
601     if (have_devno) {
602         for (schid = 0; schid <= MAX_SCHID; schid++) {
603             if (!css_find_subch(1, cssid, ssid, schid)) {
604                 sch->schid = schid;
605                 css_subch_assign(cssid, ssid, schid, devno, sch);
606                 found = true;
607                 break;
608             }
609         }
610         if (!found) {
611             ret = -ENODEV;
612             error_report("No free subchannel found for %x.%x.%04x", cssid, ssid,
613                          devno);
614             goto out_err;
615         }
616         trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
617                                     "user-configured");
618     } else {
619         cssid = VIRTUAL_CSSID;
620         for (ssid = 0; ssid <= MAX_SSID; ssid++) {
621             for (schid = 0; schid <= MAX_SCHID; schid++) {
622                 if (!css_find_subch(1, cssid, ssid, schid)) {
623                     sch->cssid = cssid;
624                     sch->ssid = ssid;
625                     sch->schid = schid;
626                     devno = schid;
627                     /*
628                      * If the devno is already taken, look further in this
629                      * subchannel set.
630                      */
631                     while (css_devno_used(cssid, ssid, devno)) {
632                         if (devno == MAX_SCHID) {
633                             devno = 0;
634                         } else if (devno == schid - 1) {
635                             ret = -ENODEV;
636                             error_report("No free devno found");
637                             goto out_err;
638                         } else {
639                             devno++;
640                         }
641                     }
642                     sch->devno = devno;
643                     css_subch_assign(cssid, ssid, schid, devno, sch);
644                     found = true;
645                     break;
646                 }
647             }
648             if (found) {
649                 break;
650             }
651         }
652         if (!found) {
653             ret = -ENODEV;
654             error_report("Virtual channel subsystem is full!");
655             goto out_err;
656         }
657         trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
658                                     "auto-configured");
659     }
660 
661     /* Build initial schib. */
662     css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
663 
664     sch->ccw_cb = virtio_ccw_cb;
665 
666     /* Build senseid data. */
667     memset(&sch->id, 0, sizeof(SenseId));
668     sch->id.reserved = 0xff;
669     sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
670     sch->id.cu_model = vdev->device_id;
671 
672     /* Only the first 32 feature bits are used. */
673     dev->host_features[0] = virtio_bus_get_vdev_features(&dev->bus,
674                                                          dev->host_features[0]);
675 
676     dev->host_features[0] |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
677     dev->host_features[0] |= 0x1 << VIRTIO_F_BAD_FEATURE;
678 
679     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
680                           parent->hotplugged, 1);
681     return 0;
682 
683 out_err:
684     dev->sch = NULL;
685     g_free(sch);
686     return ret;
687 }
688 
689 static int virtio_ccw_exit(VirtioCcwDevice *dev)
690 {
691     SubchDev *sch = dev->sch;
692 
693     if (sch) {
694         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
695         g_free(sch);
696     }
697     dev->indicators = 0;
698     return 0;
699 }
700 
701 static int virtio_ccw_net_init(VirtioCcwDevice *ccw_dev)
702 {
703     DeviceState *qdev = DEVICE(ccw_dev);
704     VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
705     DeviceState *vdev = DEVICE(&dev->vdev);
706 
707     virtio_net_set_config_size(&dev->vdev, ccw_dev->host_features[0]);
708     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
709                                   object_get_typename(OBJECT(qdev)));
710     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
711     if (qdev_init(vdev) < 0) {
712         return -1;
713     }
714 
715     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
716 }
717 
718 static void virtio_ccw_net_instance_init(Object *obj)
719 {
720     VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
721     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET);
722     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
723 }
724 
725 static int virtio_ccw_blk_init(VirtioCcwDevice *ccw_dev)
726 {
727     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
728     DeviceState *vdev = DEVICE(&dev->vdev);
729     virtio_blk_set_conf(vdev, &(dev->blk));
730     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
731     if (qdev_init(vdev) < 0) {
732         return -1;
733     }
734 
735     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
736 }
737 
738 static void virtio_ccw_blk_instance_init(Object *obj)
739 {
740     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
741     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK);
742     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
743 }
744 
745 static int virtio_ccw_serial_init(VirtioCcwDevice *ccw_dev)
746 {
747     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
748     DeviceState *vdev = DEVICE(&dev->vdev);
749     DeviceState *proxy = DEVICE(ccw_dev);
750     char *bus_name;
751 
752     /*
753      * For command line compatibility, this sets the virtio-serial-device bus
754      * name as before.
755      */
756     if (proxy->id) {
757         bus_name = g_strdup_printf("%s.0", proxy->id);
758         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
759         g_free(bus_name);
760     }
761 
762     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
763     if (qdev_init(vdev) < 0) {
764         return -1;
765     }
766 
767     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
768 }
769 
770 
771 static void virtio_ccw_serial_instance_init(Object *obj)
772 {
773     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
774     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL);
775     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
776 }
777 
778 static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev)
779 {
780     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
781     DeviceState *vdev = DEVICE(&dev->vdev);
782 
783     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
784     if (qdev_init(vdev) < 0) {
785         return -1;
786     }
787 
788     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
789 }
790 
791 static void balloon_ccw_stats_get_all(Object *obj, struct Visitor *v,
792                                       void *opaque, const char *name,
793                                       Error **errp)
794 {
795     VirtIOBalloonCcw *dev = opaque;
796     object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
797 }
798 
799 static void balloon_ccw_stats_get_poll_interval(Object *obj, struct Visitor *v,
800                                                 void *opaque, const char *name,
801                                                 Error **errp)
802 {
803     VirtIOBalloonCcw *dev = opaque;
804     object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
805                         errp);
806 }
807 
808 static void balloon_ccw_stats_set_poll_interval(Object *obj, struct Visitor *v,
809                                                 void *opaque, const char *name,
810                                                 Error **errp)
811 {
812     VirtIOBalloonCcw *dev = opaque;
813     object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
814                         errp);
815 }
816 
817 static void virtio_ccw_balloon_instance_init(Object *obj)
818 {
819     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
820     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
821     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
822 
823     object_property_add(obj, "guest-stats", "guest statistics",
824                         balloon_ccw_stats_get_all, NULL, NULL, dev, NULL);
825 
826     object_property_add(obj, "guest-stats-polling-interval", "int",
827                         balloon_ccw_stats_get_poll_interval,
828                         balloon_ccw_stats_set_poll_interval,
829                         NULL, dev, NULL);
830 }
831 
832 static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
833 {
834     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
835     DeviceState *vdev = DEVICE(&dev->vdev);
836     DeviceState *qdev = DEVICE(ccw_dev);
837     char *bus_name;
838 
839     /*
840      * For command line compatibility, this sets the virtio-scsi-device bus
841      * name as before.
842      */
843     if (qdev->id) {
844         bus_name = g_strdup_printf("%s.0", qdev->id);
845         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
846         g_free(bus_name);
847     }
848 
849     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
850     if (qdev_init(vdev) < 0) {
851         return -1;
852     }
853 
854     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
855 }
856 
857 static void virtio_ccw_scsi_instance_init(Object *obj)
858 {
859     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
860     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI);
861     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
862 }
863 
864 #ifdef CONFIG_VHOST_SCSI
865 static int vhost_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
866 {
867     VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
868     DeviceState *vdev = DEVICE(&dev->vdev);
869 
870     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
871     if (qdev_init(vdev) < 0) {
872         return -1;
873     }
874 
875     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
876 }
877 
878 static void vhost_ccw_scsi_instance_init(Object *obj)
879 {
880     VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
881     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI);
882     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
883 }
884 #endif
885 
886 static int virtio_ccw_rng_init(VirtioCcwDevice *ccw_dev)
887 {
888     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
889     DeviceState *vdev = DEVICE(&dev->vdev);
890 
891     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
892     if (qdev_init(vdev) < 0) {
893         return -1;
894     }
895 
896     object_property_set_link(OBJECT(dev),
897                              OBJECT(dev->vdev.conf.rng), "rng",
898                              NULL);
899 
900     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
901 }
902 
903 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
904  * be careful and test performance if you change this.
905  */
906 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
907 {
908     return container_of(d, VirtioCcwDevice, parent_obj);
909 }
910 
911 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
912                                      uint8_t to_be_set)
913 {
914     uint8_t ind_old, ind_new;
915     hwaddr len = 1;
916     uint8_t *ind_addr;
917 
918     ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
919     if (!ind_addr) {
920         error_report("%s(%x.%x.%04x): unable to access indicator",
921                      __func__, sch->cssid, sch->ssid, sch->schid);
922         return -1;
923     }
924     do {
925         ind_old = *ind_addr;
926         ind_new = ind_old | to_be_set;
927     } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
928     cpu_physical_memory_unmap(ind_addr, len, 1, len);
929 
930     return ind_old;
931 }
932 
933 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
934 {
935     VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
936     SubchDev *sch = dev->sch;
937     uint64_t indicators;
938 
939     if (vector >= 128) {
940         return;
941     }
942 
943     if (vector < VIRTIO_PCI_QUEUE_MAX) {
944         if (!dev->indicators) {
945             return;
946         }
947         if (sch->thinint_active) {
948             /*
949              * In the adapter interrupt case, indicators points to a
950              * memory area that may be (way) larger than 64 bit and
951              * ind_bit indicates the start of the indicators in a big
952              * endian notation.
953              */
954             virtio_set_ind_atomic(sch, dev->indicators +
955                                   (dev->ind_bit + vector) / 8,
956                                   0x80 >> ((dev->ind_bit + vector) % 8));
957             if (!virtio_set_ind_atomic(sch, dev->summary_indicator,
958                                        0x01)) {
959                 css_adapter_interrupt(dev->thinint_isc);
960             }
961         } else {
962             indicators = ldq_phys(&address_space_memory, dev->indicators);
963             indicators |= 1ULL << vector;
964             stq_phys(&address_space_memory, dev->indicators, indicators);
965             css_conditional_io_interrupt(sch);
966         }
967     } else {
968         if (!dev->indicators2) {
969             return;
970         }
971         vector = 0;
972         indicators = ldq_phys(&address_space_memory, dev->indicators2);
973         indicators |= 1ULL << vector;
974         stq_phys(&address_space_memory, dev->indicators2, indicators);
975         css_conditional_io_interrupt(sch);
976     }
977 }
978 
979 static unsigned virtio_ccw_get_features(DeviceState *d)
980 {
981     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
982 
983     /* Only the first 32 feature bits are used. */
984     return dev->host_features[0];
985 }
986 
987 static void virtio_ccw_reset(DeviceState *d)
988 {
989     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
990     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
991 
992     virtio_ccw_stop_ioeventfd(dev);
993     virtio_reset(vdev);
994     css_reset_sch(dev->sch);
995     dev->indicators = 0;
996     dev->indicators2 = 0;
997     dev->summary_indicator = 0;
998 }
999 
1000 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1001 {
1002     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1003 
1004     if (running) {
1005         virtio_ccw_start_ioeventfd(dev);
1006     } else {
1007         virtio_ccw_stop_ioeventfd(dev);
1008     }
1009 }
1010 
1011 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1012 {
1013     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1014 
1015     return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1016 }
1017 
1018 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1019 {
1020     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1021 
1022     /* Stop using the generic ioeventfd, we are doing eventfd handling
1023      * ourselves below */
1024     dev->ioeventfd_disabled = assign;
1025     if (assign) {
1026         virtio_ccw_stop_ioeventfd(dev);
1027     }
1028     return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1029 }
1030 
1031 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1032                                          bool assign, bool with_irqfd)
1033 {
1034     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1035     VirtQueue *vq = virtio_get_queue(vdev, n);
1036     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1037     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1038 
1039     if (assign) {
1040         int r = event_notifier_init(notifier, 0);
1041 
1042         if (r < 0) {
1043             return r;
1044         }
1045         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1046         /* We do not support irqfd for classic I/O interrupts, because the
1047          * classic interrupts are intermixed with the subchannel status, that
1048          * is queried with test subchannel. We want to use vhost, though.
1049          * Lets make sure to have vhost running and wire up the irq fd to
1050          * land in qemu (and only the irq fd) in this code.
1051          */
1052         if (k->guest_notifier_mask) {
1053             k->guest_notifier_mask(vdev, n, false);
1054         }
1055         /* get lost events and re-inject */
1056         if (k->guest_notifier_pending &&
1057             k->guest_notifier_pending(vdev, n)) {
1058             event_notifier_set(notifier);
1059         }
1060     } else {
1061         if (k->guest_notifier_mask) {
1062             k->guest_notifier_mask(vdev, n, true);
1063         }
1064         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1065         event_notifier_cleanup(notifier);
1066     }
1067     return 0;
1068 }
1069 
1070 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1071                                           bool assigned)
1072 {
1073     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1074     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1075     int r, n;
1076 
1077     for (n = 0; n < nvqs; n++) {
1078         if (!virtio_queue_get_num(vdev, n)) {
1079             break;
1080         }
1081         /* false -> true, as soon as irqfd works */
1082         r = virtio_ccw_set_guest_notifier(dev, n, assigned, false);
1083         if (r < 0) {
1084             goto assign_error;
1085         }
1086     }
1087     return 0;
1088 
1089 assign_error:
1090     while (--n >= 0) {
1091         virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1092     }
1093     return r;
1094 }
1095 
1096 /**************** Virtio-ccw Bus Device Descriptions *******************/
1097 
1098 static Property virtio_ccw_net_properties[] = {
1099     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1100     DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]),
1101     DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetCcw, vdev.net_conf),
1102     DEFINE_NIC_PROPERTIES(VirtIONetCcw, vdev.nic_conf),
1103     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1104                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1105     DEFINE_PROP_END_OF_LIST(),
1106 };
1107 
1108 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1109 {
1110     DeviceClass *dc = DEVICE_CLASS(klass);
1111     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1112 
1113     k->init = virtio_ccw_net_init;
1114     k->exit = virtio_ccw_exit;
1115     dc->reset = virtio_ccw_reset;
1116     dc->props = virtio_ccw_net_properties;
1117 }
1118 
1119 static const TypeInfo virtio_ccw_net = {
1120     .name          = TYPE_VIRTIO_NET_CCW,
1121     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1122     .instance_size = sizeof(VirtIONetCcw),
1123     .instance_init = virtio_ccw_net_instance_init,
1124     .class_init    = virtio_ccw_net_class_init,
1125 };
1126 
1127 static Property virtio_ccw_blk_properties[] = {
1128     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1129     DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice, host_features[0]),
1130     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkCcw, blk),
1131     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1132                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1133 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1134     DEFINE_PROP_BIT("x-data-plane", VirtIOBlkCcw, blk.data_plane, 0, false),
1135 #endif
1136     DEFINE_PROP_END_OF_LIST(),
1137 };
1138 
1139 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1140 {
1141     DeviceClass *dc = DEVICE_CLASS(klass);
1142     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1143 
1144     k->init = virtio_ccw_blk_init;
1145     k->exit = virtio_ccw_exit;
1146     dc->reset = virtio_ccw_reset;
1147     dc->props = virtio_ccw_blk_properties;
1148 }
1149 
1150 static const TypeInfo virtio_ccw_blk = {
1151     .name          = TYPE_VIRTIO_BLK_CCW,
1152     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1153     .instance_size = sizeof(VirtIOBlkCcw),
1154     .instance_init = virtio_ccw_blk_instance_init,
1155     .class_init    = virtio_ccw_blk_class_init,
1156 };
1157 
1158 static Property virtio_ccw_serial_properties[] = {
1159     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1160     DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtioSerialCcw, vdev.serial),
1161     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1162     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1163                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1164     DEFINE_PROP_END_OF_LIST(),
1165 };
1166 
1167 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1168 {
1169     DeviceClass *dc = DEVICE_CLASS(klass);
1170     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1171 
1172     k->init = virtio_ccw_serial_init;
1173     k->exit = virtio_ccw_exit;
1174     dc->reset = virtio_ccw_reset;
1175     dc->props = virtio_ccw_serial_properties;
1176 }
1177 
1178 static const TypeInfo virtio_ccw_serial = {
1179     .name          = TYPE_VIRTIO_SERIAL_CCW,
1180     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1181     .instance_size = sizeof(VirtioSerialCcw),
1182     .instance_init = virtio_ccw_serial_instance_init,
1183     .class_init    = virtio_ccw_serial_class_init,
1184 };
1185 
1186 static Property virtio_ccw_balloon_properties[] = {
1187     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1188     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1189     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1190                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1191     DEFINE_PROP_END_OF_LIST(),
1192 };
1193 
1194 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1195 {
1196     DeviceClass *dc = DEVICE_CLASS(klass);
1197     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1198 
1199     k->init = virtio_ccw_balloon_init;
1200     k->exit = virtio_ccw_exit;
1201     dc->reset = virtio_ccw_reset;
1202     dc->props = virtio_ccw_balloon_properties;
1203 }
1204 
1205 static const TypeInfo virtio_ccw_balloon = {
1206     .name          = TYPE_VIRTIO_BALLOON_CCW,
1207     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1208     .instance_size = sizeof(VirtIOBalloonCcw),
1209     .instance_init = virtio_ccw_balloon_instance_init,
1210     .class_init    = virtio_ccw_balloon_class_init,
1211 };
1212 
1213 static Property virtio_ccw_scsi_properties[] = {
1214     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1215     DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.parent_obj.conf),
1216     DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]),
1217     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1218                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1219     DEFINE_PROP_END_OF_LIST(),
1220 };
1221 
1222 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1223 {
1224     DeviceClass *dc = DEVICE_CLASS(klass);
1225     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1226 
1227     k->init = virtio_ccw_scsi_init;
1228     k->exit = virtio_ccw_exit;
1229     dc->reset = virtio_ccw_reset;
1230     dc->props = virtio_ccw_scsi_properties;
1231 }
1232 
1233 static const TypeInfo virtio_ccw_scsi = {
1234     .name          = TYPE_VIRTIO_SCSI_CCW,
1235     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1236     .instance_size = sizeof(VirtIOSCSICcw),
1237     .instance_init = virtio_ccw_scsi_instance_init,
1238     .class_init    = virtio_ccw_scsi_class_init,
1239 };
1240 
1241 #ifdef CONFIG_VHOST_SCSI
1242 static Property vhost_ccw_scsi_properties[] = {
1243     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1244     DEFINE_VHOST_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.parent_obj.conf),
1245     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1246     DEFINE_PROP_END_OF_LIST(),
1247 };
1248 
1249 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1250 {
1251     DeviceClass *dc = DEVICE_CLASS(klass);
1252     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1253 
1254     k->init = vhost_ccw_scsi_init;
1255     k->exit = virtio_ccw_exit;
1256     dc->reset = virtio_ccw_reset;
1257     dc->props = vhost_ccw_scsi_properties;
1258 }
1259 
1260 static const TypeInfo vhost_ccw_scsi = {
1261     .name          = TYPE_VHOST_SCSI_CCW,
1262     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1263     .instance_size = sizeof(VirtIOSCSICcw),
1264     .instance_init = vhost_ccw_scsi_instance_init,
1265     .class_init    = vhost_ccw_scsi_class_init,
1266 };
1267 #endif
1268 
1269 static void virtio_ccw_rng_instance_init(Object *obj)
1270 {
1271     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1272     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG);
1273     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1274     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1275                              (Object **)&dev->vdev.conf.rng, NULL);
1276 }
1277 
1278 static Property virtio_ccw_rng_properties[] = {
1279     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1280     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1281     DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNGCcw, vdev.conf),
1282     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1283                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1284     DEFINE_PROP_END_OF_LIST(),
1285 };
1286 
1287 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1288 {
1289     DeviceClass *dc = DEVICE_CLASS(klass);
1290     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1291 
1292     k->init = virtio_ccw_rng_init;
1293     k->exit = virtio_ccw_exit;
1294     dc->reset = virtio_ccw_reset;
1295     dc->props = virtio_ccw_rng_properties;
1296 }
1297 
1298 static const TypeInfo virtio_ccw_rng = {
1299     .name          = TYPE_VIRTIO_RNG_CCW,
1300     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1301     .instance_size = sizeof(VirtIORNGCcw),
1302     .instance_init = virtio_ccw_rng_instance_init,
1303     .class_init    = virtio_ccw_rng_class_init,
1304 };
1305 
1306 static int virtio_ccw_busdev_init(DeviceState *dev)
1307 {
1308     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1309     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1310 
1311     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1312 
1313     return _info->init(_dev);
1314 }
1315 
1316 static int virtio_ccw_busdev_exit(DeviceState *dev)
1317 {
1318     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1319     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1320 
1321     return _info->exit(_dev);
1322 }
1323 
1324 static int virtio_ccw_busdev_unplug(DeviceState *dev)
1325 {
1326     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1327     SubchDev *sch = _dev->sch;
1328 
1329     virtio_ccw_stop_ioeventfd(_dev);
1330 
1331     /*
1332      * We should arrive here only for device_del, since we don't support
1333      * direct hot(un)plug of channels, but only through virtio.
1334      */
1335     assert(sch != NULL);
1336     /* Subchannel is now disabled and no longer valid. */
1337     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1338                                      PMCW_FLAGS_MASK_DNV);
1339 
1340     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1341 
1342     object_unparent(OBJECT(dev));
1343     return 0;
1344 }
1345 
1346 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1347 {
1348     DeviceClass *dc = DEVICE_CLASS(klass);
1349 
1350     dc->init = virtio_ccw_busdev_init;
1351     dc->exit = virtio_ccw_busdev_exit;
1352     dc->unplug = virtio_ccw_busdev_unplug;
1353     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1354 
1355 }
1356 
1357 static const TypeInfo virtio_ccw_device_info = {
1358     .name = TYPE_VIRTIO_CCW_DEVICE,
1359     .parent = TYPE_DEVICE,
1360     .instance_size = sizeof(VirtioCcwDevice),
1361     .class_init = virtio_ccw_device_class_init,
1362     .class_size = sizeof(VirtIOCCWDeviceClass),
1363     .abstract = true,
1364 };
1365 
1366 /***************** Virtual-css Bus Bridge Device ********************/
1367 /* Only required to have the virtio bus as child in the system bus */
1368 
1369 static int virtual_css_bridge_init(SysBusDevice *dev)
1370 {
1371     /* nothing */
1372     return 0;
1373 }
1374 
1375 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1376 {
1377     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1378 
1379     k->init = virtual_css_bridge_init;
1380 }
1381 
1382 static const TypeInfo virtual_css_bridge_info = {
1383     .name          = "virtual-css-bridge",
1384     .parent        = TYPE_SYS_BUS_DEVICE,
1385     .instance_size = sizeof(SysBusDevice),
1386     .class_init    = virtual_css_bridge_class_init,
1387 };
1388 
1389 /* virtio-ccw-bus */
1390 
1391 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1392                                VirtioCcwDevice *dev)
1393 {
1394     DeviceState *qdev = DEVICE(dev);
1395     BusState *qbus;
1396     char virtio_bus_name[] = "virtio-bus";
1397 
1398     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1399                         qdev, virtio_bus_name);
1400     qbus = BUS(bus);
1401     qbus->allow_hotplug = 1;
1402 }
1403 
1404 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1405 {
1406     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1407     BusClass *bus_class = BUS_CLASS(klass);
1408 
1409     bus_class->max_dev = 1;
1410     k->notify = virtio_ccw_notify;
1411     k->get_features = virtio_ccw_get_features;
1412     k->vmstate_change = virtio_ccw_vmstate_change;
1413     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1414     k->set_host_notifier = virtio_ccw_set_host_notifier;
1415     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1416 }
1417 
1418 static const TypeInfo virtio_ccw_bus_info = {
1419     .name = TYPE_VIRTIO_CCW_BUS,
1420     .parent = TYPE_VIRTIO_BUS,
1421     .instance_size = sizeof(VirtioCcwBusState),
1422     .class_init = virtio_ccw_bus_class_init,
1423 };
1424 
1425 static void virtio_ccw_register(void)
1426 {
1427     type_register_static(&virtio_ccw_bus_info);
1428     type_register_static(&virtual_css_bus_info);
1429     type_register_static(&virtio_ccw_device_info);
1430     type_register_static(&virtio_ccw_serial);
1431     type_register_static(&virtio_ccw_blk);
1432     type_register_static(&virtio_ccw_net);
1433     type_register_static(&virtio_ccw_balloon);
1434     type_register_static(&virtio_ccw_scsi);
1435 #ifdef CONFIG_VHOST_SCSI
1436     type_register_static(&vhost_ccw_scsi);
1437 #endif
1438     type_register_static(&virtio_ccw_rng);
1439     type_register_static(&virtual_css_bridge_info);
1440 }
1441 
1442 type_init(virtio_ccw_register)
1443