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