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