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