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