]> Git Repo - qemu.git/blob - hw/s390x/virtio-ccw.c
error: Remove NULL checks on error_propagate() calls
[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 schid;
741     bool found = false;
742     SubchDev *sch;
743     Error *err = NULL;
744     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
745
746     sch = g_malloc0(sizeof(SubchDev));
747
748     sch->driver_data = dev;
749     dev->sch = sch;
750
751     dev->indicators = NULL;
752
753     /* Initialize subchannel structure. */
754     sch->channel_prog = 0x0;
755     sch->last_cmd_valid = false;
756     sch->thinint_active = false;
757     /*
758      * Use a device number if provided. Otherwise, fall back to subchannel
759      * number.
760      */
761     if (dev->bus_id.valid) {
762         /* Enforce use of virtual cssid. */
763         if (dev->bus_id.cssid != VIRTUAL_CSSID) {
764             error_setg(errp, "cssid %x not valid for virtio devices",
765                        dev->bus_id.cssid);
766             goto out_err;
767         }
768         if (css_devno_used(dev->bus_id.cssid, dev->bus_id.ssid,
769                            dev->bus_id.devid)) {
770                 error_setg(errp, "Device %x.%x.%04x already exists",
771                            dev->bus_id.cssid, dev->bus_id.ssid,
772                            dev->bus_id.devid);
773                 goto out_err;
774         }
775         sch->cssid = dev->bus_id.cssid;
776         sch->ssid = dev->bus_id.ssid;
777         sch->devno = dev->bus_id.devid;
778
779         /* Find the next free id. */
780         for (schid = 0; schid <= MAX_SCHID; schid++) {
781             if (!css_find_subch(1, sch->cssid, sch->ssid, schid)) {
782                 sch->schid = schid;
783                 css_subch_assign(sch->cssid, sch->ssid, sch->schid,
784                                  sch->devno, sch);
785                 found = true;
786                 break;
787             }
788         }
789         if (!found) {
790             error_setg(errp, "No free subchannel found for %x.%x.%04x",
791                        sch->cssid, sch->ssid, sch->devno);
792             goto out_err;
793         }
794         trace_virtio_ccw_new_device(sch->cssid, sch->ssid, sch->schid,
795                                     sch->devno, "user-configured");
796     } else {
797         unsigned int cssid = VIRTUAL_CSSID, ssid, devno;
798
799         for (ssid = 0; ssid <= MAX_SSID; ssid++) {
800             for (schid = 0; schid <= MAX_SCHID; schid++) {
801                 if (!css_find_subch(1, cssid, ssid, schid)) {
802                     sch->cssid = cssid;
803                     sch->ssid = ssid;
804                     sch->schid = schid;
805                     devno = schid;
806                     /*
807                      * If the devno is already taken, look further in this
808                      * subchannel set.
809                      */
810                     while (css_devno_used(cssid, ssid, devno)) {
811                         if (devno == MAX_SCHID) {
812                             devno = 0;
813                         } else if (devno == schid - 1) {
814                             error_setg(errp, "No free devno found");
815                             goto out_err;
816                         } else {
817                             devno++;
818                         }
819                     }
820                     sch->devno = devno;
821                     css_subch_assign(cssid, ssid, schid, devno, sch);
822                     found = true;
823                     break;
824                 }
825             }
826             if (found) {
827                 break;
828             }
829         }
830         if (!found) {
831             error_setg(errp, "Virtual channel subsystem is full!");
832             goto out_err;
833         }
834         trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
835                                     "auto-configured");
836     }
837
838     /* Build initial schib. */
839     css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
840
841     sch->ccw_cb = virtio_ccw_cb;
842     sch->disable_cb = virtio_sch_disable_cb;
843
844     /* Build senseid data. */
845     memset(&sch->id, 0, sizeof(SenseId));
846     sch->id.reserved = 0xff;
847     sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
848
849     dev->revision = -1;
850
851     if (k->realize) {
852         k->realize(dev, &err);
853     }
854     if (err) {
855         error_propagate(errp, err);
856         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
857         goto out_err;
858     }
859
860     return;
861
862 out_err:
863     dev->sch = NULL;
864     g_free(sch);
865 }
866
867 static int virtio_ccw_exit(VirtioCcwDevice *dev)
868 {
869     SubchDev *sch = dev->sch;
870
871     if (sch) {
872         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
873         g_free(sch);
874     }
875     if (dev->indicators) {
876         release_indicator(&dev->routes.adapter, dev->indicators);
877         dev->indicators = NULL;
878     }
879     return 0;
880 }
881
882 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp)
883 {
884     DeviceState *qdev = DEVICE(ccw_dev);
885     VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
886     DeviceState *vdev = DEVICE(&dev->vdev);
887     Error *err = NULL;
888
889     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
890                                   object_get_typename(OBJECT(qdev)));
891     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
892     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
893     error_propagate(errp, err);
894 }
895
896 static void virtio_ccw_net_instance_init(Object *obj)
897 {
898     VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
899
900     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
901                                 TYPE_VIRTIO_NET);
902     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
903                               "bootindex", &error_abort);
904 }
905
906 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp)
907 {
908     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
909     DeviceState *vdev = DEVICE(&dev->vdev);
910     Error *err = NULL;
911
912     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
913     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
914     error_propagate(errp, err);
915 }
916
917 static void virtio_ccw_blk_instance_init(Object *obj)
918 {
919     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
920
921     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
922                                 TYPE_VIRTIO_BLK);
923     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
924                               &error_abort);
925     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
926                               "bootindex", &error_abort);
927 }
928
929 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp)
930 {
931     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
932     DeviceState *vdev = DEVICE(&dev->vdev);
933     DeviceState *proxy = DEVICE(ccw_dev);
934     Error *err = NULL;
935     char *bus_name;
936
937     /*
938      * For command line compatibility, this sets the virtio-serial-device bus
939      * name as before.
940      */
941     if (proxy->id) {
942         bus_name = g_strdup_printf("%s.0", proxy->id);
943         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
944         g_free(bus_name);
945     }
946
947     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
948     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
949     error_propagate(errp, err);
950 }
951
952
953 static void virtio_ccw_serial_instance_init(Object *obj)
954 {
955     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
956
957     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
958                                 TYPE_VIRTIO_SERIAL);
959 }
960
961 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp)
962 {
963     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
964     DeviceState *vdev = DEVICE(&dev->vdev);
965     Error *err = NULL;
966
967     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
968     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
969     error_propagate(errp, err);
970 }
971
972 static void virtio_ccw_balloon_instance_init(Object *obj)
973 {
974     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
975
976     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
977                                 TYPE_VIRTIO_BALLOON);
978     object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
979                               "guest-stats", &error_abort);
980     object_property_add_alias(obj, "guest-stats-polling-interval",
981                               OBJECT(&dev->vdev),
982                               "guest-stats-polling-interval", &error_abort);
983 }
984
985 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
986 {
987     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
988     DeviceState *vdev = DEVICE(&dev->vdev);
989     DeviceState *qdev = DEVICE(ccw_dev);
990     Error *err = NULL;
991     char *bus_name;
992
993     /*
994      * For command line compatibility, this sets the virtio-scsi-device bus
995      * name as before.
996      */
997     if (qdev->id) {
998         bus_name = g_strdup_printf("%s.0", qdev->id);
999         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1000         g_free(bus_name);
1001     }
1002
1003     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1004     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1005     error_propagate(errp, err);
1006 }
1007
1008 static void virtio_ccw_scsi_instance_init(Object *obj)
1009 {
1010     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
1011
1012     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1013                                 TYPE_VIRTIO_SCSI);
1014     object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
1015                               &error_abort);
1016 }
1017
1018 #ifdef CONFIG_VHOST_SCSI
1019 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1020 {
1021     VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
1022     DeviceState *vdev = DEVICE(&dev->vdev);
1023     Error *err = NULL;
1024
1025     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1026     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1027     error_propagate(errp, err);
1028 }
1029
1030 static void vhost_ccw_scsi_instance_init(Object *obj)
1031 {
1032     VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
1033
1034     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1035                                 TYPE_VHOST_SCSI);
1036 }
1037 #endif
1038
1039 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1040 {
1041     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
1042     DeviceState *vdev = DEVICE(&dev->vdev);
1043     Error *err = NULL;
1044
1045     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1046     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1047     if (err) {
1048         error_propagate(errp, err);
1049         return;
1050     }
1051
1052     object_property_set_link(OBJECT(dev),
1053                              OBJECT(dev->vdev.conf.rng), "rng",
1054                              NULL);
1055 }
1056
1057 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
1058  * be careful and test performance if you change this.
1059  */
1060 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
1061 {
1062     return container_of(d, VirtioCcwDevice, parent_obj);
1063 }
1064
1065 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
1066                                      uint8_t to_be_set)
1067 {
1068     uint8_t ind_old, ind_new;
1069     hwaddr len = 1;
1070     uint8_t *ind_addr;
1071
1072     ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
1073     if (!ind_addr) {
1074         error_report("%s(%x.%x.%04x): unable to access indicator",
1075                      __func__, sch->cssid, sch->ssid, sch->schid);
1076         return -1;
1077     }
1078     do {
1079         ind_old = *ind_addr;
1080         ind_new = ind_old | to_be_set;
1081     } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
1082     trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new);
1083     cpu_physical_memory_unmap(ind_addr, len, 1, len);
1084
1085     return ind_old;
1086 }
1087
1088 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
1089 {
1090     VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1091     SubchDev *sch = dev->sch;
1092     uint64_t indicators;
1093
1094     /* queue indicators + secondary indicators */
1095     if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) {
1096         return;
1097     }
1098
1099     if (vector < VIRTIO_CCW_QUEUE_MAX) {
1100         if (!dev->indicators) {
1101             return;
1102         }
1103         if (sch->thinint_active) {
1104             /*
1105              * In the adapter interrupt case, indicators points to a
1106              * memory area that may be (way) larger than 64 bit and
1107              * ind_bit indicates the start of the indicators in a big
1108              * endian notation.
1109              */
1110             uint64_t ind_bit = dev->routes.adapter.ind_offset;
1111
1112             virtio_set_ind_atomic(sch, dev->indicators->addr +
1113                                   (ind_bit + vector) / 8,
1114                                   0x80 >> ((ind_bit + vector) % 8));
1115             if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1116                                        0x01)) {
1117                 css_adapter_interrupt(dev->thinint_isc);
1118             }
1119         } else {
1120             indicators = address_space_ldq(&address_space_memory,
1121                                            dev->indicators->addr,
1122                                            MEMTXATTRS_UNSPECIFIED,
1123                                            NULL);
1124             indicators |= 1ULL << vector;
1125             address_space_stq(&address_space_memory, dev->indicators->addr,
1126                               indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1127             css_conditional_io_interrupt(sch);
1128         }
1129     } else {
1130         if (!dev->indicators2) {
1131             return;
1132         }
1133         vector = 0;
1134         indicators = address_space_ldq(&address_space_memory,
1135                                        dev->indicators2->addr,
1136                                        MEMTXATTRS_UNSPECIFIED,
1137                                        NULL);
1138         indicators |= 1ULL << vector;
1139         address_space_stq(&address_space_memory, dev->indicators2->addr,
1140                           indicators, MEMTXATTRS_UNSPECIFIED, NULL);
1141         css_conditional_io_interrupt(sch);
1142     }
1143 }
1144
1145 static void virtio_ccw_reset(DeviceState *d)
1146 {
1147     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1148     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1149
1150     virtio_ccw_reset_virtio(dev, vdev);
1151     css_reset_sch(dev->sch);
1152 }
1153
1154 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1155 {
1156     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1157
1158     if (running) {
1159         virtio_ccw_start_ioeventfd(dev);
1160     } else {
1161         virtio_ccw_stop_ioeventfd(dev);
1162     }
1163 }
1164
1165 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1166 {
1167     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1168
1169     return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1170 }
1171
1172 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1173 {
1174     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1175
1176     /* Stop using the generic ioeventfd, we are doing eventfd handling
1177      * ourselves below */
1178     dev->ioeventfd_disabled = assign;
1179     if (assign) {
1180         virtio_ccw_stop_ioeventfd(dev);
1181     }
1182     return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1183 }
1184
1185 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1186 {
1187     int r;
1188
1189     if (!dev->sch->thinint_active) {
1190         return -EINVAL;
1191     }
1192
1193     r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1194     if (r) {
1195         return r;
1196     }
1197     r = map_indicator(&dev->routes.adapter, dev->indicators);
1198     if (r) {
1199         return r;
1200     }
1201     dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1202     dev->routes.adapter.ind_addr = dev->indicators->map;
1203
1204     return 0;
1205 }
1206
1207 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1208 {
1209     int i;
1210     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1211     int ret;
1212     S390FLICState *fs = s390_get_flic();
1213     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1214
1215     ret = virtio_ccw_get_mappings(dev);
1216     if (ret) {
1217         return ret;
1218     }
1219     for (i = 0; i < nvqs; i++) {
1220         if (!virtio_queue_get_num(vdev, i)) {
1221             break;
1222         }
1223     }
1224     dev->routes.num_routes = i;
1225     return fsc->add_adapter_routes(fs, &dev->routes);
1226 }
1227
1228 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1229 {
1230     S390FLICState *fs = s390_get_flic();
1231     S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1232
1233     fsc->release_adapter_routes(fs, &dev->routes);
1234 }
1235
1236 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1237 {
1238     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1239     VirtQueue *vq = virtio_get_queue(vdev, n);
1240     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1241
1242     return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL,
1243                                               dev->routes.gsi[n]);
1244 }
1245
1246 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1247 {
1248     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1249     VirtQueue *vq = virtio_get_queue(vdev, n);
1250     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1251     int ret;
1252
1253     ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier,
1254                                                 dev->routes.gsi[n]);
1255     assert(ret == 0);
1256 }
1257
1258 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1259                                          bool assign, bool with_irqfd)
1260 {
1261     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1262     VirtQueue *vq = virtio_get_queue(vdev, n);
1263     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1264     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1265
1266     if (assign) {
1267         int r = event_notifier_init(notifier, 0);
1268
1269         if (r < 0) {
1270             return r;
1271         }
1272         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1273         if (with_irqfd) {
1274             r = virtio_ccw_add_irqfd(dev, n);
1275             if (r) {
1276                 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1277                                                            with_irqfd);
1278                 return r;
1279             }
1280         }
1281         /*
1282          * We do not support individual masking for channel devices, so we
1283          * need to manually trigger any guest masking callbacks here.
1284          */
1285         if (k->guest_notifier_mask) {
1286             k->guest_notifier_mask(vdev, n, false);
1287         }
1288         /* get lost events and re-inject */
1289         if (k->guest_notifier_pending &&
1290             k->guest_notifier_pending(vdev, n)) {
1291             event_notifier_set(notifier);
1292         }
1293     } else {
1294         if (k->guest_notifier_mask) {
1295             k->guest_notifier_mask(vdev, n, true);
1296         }
1297         if (with_irqfd) {
1298             virtio_ccw_remove_irqfd(dev, n);
1299         }
1300         virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1301         event_notifier_cleanup(notifier);
1302     }
1303     return 0;
1304 }
1305
1306 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1307                                           bool assigned)
1308 {
1309     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1310     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1311     bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1312     int r, n;
1313
1314     if (with_irqfd && assigned) {
1315         /* irq routes need to be set up before assigning irqfds */
1316         r = virtio_ccw_setup_irqroutes(dev, nvqs);
1317         if (r < 0) {
1318             goto irqroute_error;
1319         }
1320     }
1321     for (n = 0; n < nvqs; n++) {
1322         if (!virtio_queue_get_num(vdev, n)) {
1323             break;
1324         }
1325         r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1326         if (r < 0) {
1327             goto assign_error;
1328         }
1329     }
1330     if (with_irqfd && !assigned) {
1331         /* release irq routes after irqfds have been released */
1332         virtio_ccw_release_irqroutes(dev, nvqs);
1333     }
1334     return 0;
1335
1336 assign_error:
1337     while (--n >= 0) {
1338         virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1339     }
1340 irqroute_error:
1341     if (with_irqfd && assigned) {
1342         virtio_ccw_release_irqroutes(dev, nvqs);
1343     }
1344     return r;
1345 }
1346
1347 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1348 {
1349     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1350     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1351
1352     qemu_put_be16(f, virtio_queue_vector(vdev, n));
1353 }
1354
1355 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1356 {
1357     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1358     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1359     uint16_t vector;
1360
1361     qemu_get_be16s(f, &vector);
1362     virtio_queue_set_vector(vdev, n , vector);
1363
1364     return 0;
1365 }
1366
1367 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1368 {
1369     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1370     SubchDev *s = dev->sch;
1371     VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1372
1373     subch_device_save(s, f);
1374     if (dev->indicators != NULL) {
1375         qemu_put_be32(f, dev->indicators->len);
1376         qemu_put_be64(f, dev->indicators->addr);
1377     } else {
1378         qemu_put_be32(f, 0);
1379         qemu_put_be64(f, 0UL);
1380     }
1381     if (dev->indicators2 != NULL) {
1382         qemu_put_be32(f, dev->indicators2->len);
1383         qemu_put_be64(f, dev->indicators2->addr);
1384     } else {
1385         qemu_put_be32(f, 0);
1386         qemu_put_be64(f, 0UL);
1387     }
1388     if (dev->summary_indicator != NULL) {
1389         qemu_put_be32(f, dev->summary_indicator->len);
1390         qemu_put_be64(f, dev->summary_indicator->addr);
1391     } else {
1392         qemu_put_be32(f, 0);
1393         qemu_put_be64(f, 0UL);
1394     }
1395     qemu_put_be16(f, vdev->config_vector);
1396     qemu_put_be64(f, dev->routes.adapter.ind_offset);
1397     qemu_put_byte(f, dev->thinint_isc);
1398     qemu_put_be32(f, dev->revision);
1399 }
1400
1401 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1402 {
1403     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1404     SubchDev *s = dev->sch;
1405     VirtIODevice *vdev = virtio_ccw_get_vdev(s);
1406     int len;
1407
1408     s->driver_data = dev;
1409     subch_device_load(s, f);
1410     len = qemu_get_be32(f);
1411     if (len != 0) {
1412         dev->indicators = get_indicator(qemu_get_be64(f), len);
1413     } else {
1414         qemu_get_be64(f);
1415         dev->indicators = NULL;
1416     }
1417     len = qemu_get_be32(f);
1418     if (len != 0) {
1419         dev->indicators2 = get_indicator(qemu_get_be64(f), len);
1420     } else {
1421         qemu_get_be64(f);
1422         dev->indicators2 = NULL;
1423     }
1424     len = qemu_get_be32(f);
1425     if (len != 0) {
1426         dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
1427     } else {
1428         qemu_get_be64(f);
1429         dev->summary_indicator = NULL;
1430     }
1431     qemu_get_be16s(f, &vdev->config_vector);
1432     dev->routes.adapter.ind_offset = qemu_get_be64(f);
1433     dev->thinint_isc = qemu_get_byte(f);
1434     dev->revision = qemu_get_be32(f);
1435     if (s->thinint_active) {
1436         return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
1437                                        dev->thinint_isc, true, false,
1438                                        &dev->routes.adapter.adapter_id);
1439     }
1440
1441     return 0;
1442 }
1443
1444 /* This is called by virtio-bus just after the device is plugged. */
1445 static void virtio_ccw_device_plugged(DeviceState *d, Error **errp)
1446 {
1447     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1448     VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1449     SubchDev *sch = dev->sch;
1450     int n = virtio_get_num_queues(vdev);
1451
1452     if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) {
1453         error_setg(errp, "The number of virtqueues %d "
1454                    "exceeds ccw limit %d", n,
1455                    VIRTIO_CCW_QUEUE_MAX);
1456         return;
1457     }
1458
1459     if (!kvm_eventfds_enabled()) {
1460         dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
1461     }
1462
1463     sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus);
1464
1465     if (dev->max_rev >= 1) {
1466         virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1467     }
1468
1469     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
1470                           d->hotplugged, 1);
1471 }
1472
1473 static void virtio_ccw_post_plugged(DeviceState *d, Error **errp)
1474 {
1475    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1476    VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1477
1478    if (!virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1479         /* A backend didn't support modern virtio. */
1480        dev->max_rev = 0;
1481    }
1482 }
1483
1484 static void virtio_ccw_device_unplugged(DeviceState *d)
1485 {
1486     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1487
1488     virtio_ccw_stop_ioeventfd(dev);
1489 }
1490 /**************** Virtio-ccw Bus Device Descriptions *******************/
1491
1492 static Property virtio_ccw_net_properties[] = {
1493     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1494     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1495                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1496     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1497                        VIRTIO_CCW_MAX_REV),
1498     DEFINE_PROP_END_OF_LIST(),
1499 };
1500
1501 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1502 {
1503     DeviceClass *dc = DEVICE_CLASS(klass);
1504     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1505
1506     k->realize = virtio_ccw_net_realize;
1507     k->exit = virtio_ccw_exit;
1508     dc->reset = virtio_ccw_reset;
1509     dc->props = virtio_ccw_net_properties;
1510     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1511 }
1512
1513 static const TypeInfo virtio_ccw_net = {
1514     .name          = TYPE_VIRTIO_NET_CCW,
1515     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1516     .instance_size = sizeof(VirtIONetCcw),
1517     .instance_init = virtio_ccw_net_instance_init,
1518     .class_init    = virtio_ccw_net_class_init,
1519 };
1520
1521 static Property virtio_ccw_blk_properties[] = {
1522     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1523     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1524                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1525     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1526                        VIRTIO_CCW_MAX_REV),
1527     DEFINE_PROP_END_OF_LIST(),
1528 };
1529
1530 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1531 {
1532     DeviceClass *dc = DEVICE_CLASS(klass);
1533     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1534
1535     k->realize = virtio_ccw_blk_realize;
1536     k->exit = virtio_ccw_exit;
1537     dc->reset = virtio_ccw_reset;
1538     dc->props = virtio_ccw_blk_properties;
1539     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1540 }
1541
1542 static const TypeInfo virtio_ccw_blk = {
1543     .name          = TYPE_VIRTIO_BLK_CCW,
1544     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1545     .instance_size = sizeof(VirtIOBlkCcw),
1546     .instance_init = virtio_ccw_blk_instance_init,
1547     .class_init    = virtio_ccw_blk_class_init,
1548 };
1549
1550 static Property virtio_ccw_serial_properties[] = {
1551     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1552     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1553                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1554     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1555                        VIRTIO_CCW_MAX_REV),
1556     DEFINE_PROP_END_OF_LIST(),
1557 };
1558
1559 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1560 {
1561     DeviceClass *dc = DEVICE_CLASS(klass);
1562     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1563
1564     k->realize = virtio_ccw_serial_realize;
1565     k->exit = virtio_ccw_exit;
1566     dc->reset = virtio_ccw_reset;
1567     dc->props = virtio_ccw_serial_properties;
1568     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1569 }
1570
1571 static const TypeInfo virtio_ccw_serial = {
1572     .name          = TYPE_VIRTIO_SERIAL_CCW,
1573     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1574     .instance_size = sizeof(VirtioSerialCcw),
1575     .instance_init = virtio_ccw_serial_instance_init,
1576     .class_init    = virtio_ccw_serial_class_init,
1577 };
1578
1579 static Property virtio_ccw_balloon_properties[] = {
1580     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1581     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1582                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1583     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1584                        VIRTIO_CCW_MAX_REV),
1585     DEFINE_PROP_END_OF_LIST(),
1586 };
1587
1588 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1589 {
1590     DeviceClass *dc = DEVICE_CLASS(klass);
1591     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1592
1593     k->realize = virtio_ccw_balloon_realize;
1594     k->exit = virtio_ccw_exit;
1595     dc->reset = virtio_ccw_reset;
1596     dc->props = virtio_ccw_balloon_properties;
1597     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1598 }
1599
1600 static const TypeInfo virtio_ccw_balloon = {
1601     .name          = TYPE_VIRTIO_BALLOON_CCW,
1602     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1603     .instance_size = sizeof(VirtIOBalloonCcw),
1604     .instance_init = virtio_ccw_balloon_instance_init,
1605     .class_init    = virtio_ccw_balloon_class_init,
1606 };
1607
1608 static Property virtio_ccw_scsi_properties[] = {
1609     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1610     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1611                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1612     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1613                        VIRTIO_CCW_MAX_REV),
1614     DEFINE_PROP_END_OF_LIST(),
1615 };
1616
1617 static void virtio_ccw_scsi_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_scsi_realize;
1623     k->exit = virtio_ccw_exit;
1624     dc->reset = virtio_ccw_reset;
1625     dc->props = virtio_ccw_scsi_properties;
1626     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1627 }
1628
1629 static const TypeInfo virtio_ccw_scsi = {
1630     .name          = TYPE_VIRTIO_SCSI_CCW,
1631     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1632     .instance_size = sizeof(VirtIOSCSICcw),
1633     .instance_init = virtio_ccw_scsi_instance_init,
1634     .class_init    = virtio_ccw_scsi_class_init,
1635 };
1636
1637 #ifdef CONFIG_VHOST_SCSI
1638 static Property vhost_ccw_scsi_properties[] = {
1639     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1640     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1641                        VIRTIO_CCW_MAX_REV),
1642     DEFINE_PROP_END_OF_LIST(),
1643 };
1644
1645 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1646 {
1647     DeviceClass *dc = DEVICE_CLASS(klass);
1648     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1649
1650     k->realize = vhost_ccw_scsi_realize;
1651     k->exit = virtio_ccw_exit;
1652     dc->reset = virtio_ccw_reset;
1653     dc->props = vhost_ccw_scsi_properties;
1654     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1655 }
1656
1657 static const TypeInfo vhost_ccw_scsi = {
1658     .name          = TYPE_VHOST_SCSI_CCW,
1659     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1660     .instance_size = sizeof(VHostSCSICcw),
1661     .instance_init = vhost_ccw_scsi_instance_init,
1662     .class_init    = vhost_ccw_scsi_class_init,
1663 };
1664 #endif
1665
1666 static void virtio_ccw_rng_instance_init(Object *obj)
1667 {
1668     VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1669
1670     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1671                                 TYPE_VIRTIO_RNG);
1672     object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1673                               "rng", &error_abort);
1674 }
1675
1676 static Property virtio_ccw_rng_properties[] = {
1677     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1678     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1679                     VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1680     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1681                        VIRTIO_CCW_MAX_REV),
1682     DEFINE_PROP_END_OF_LIST(),
1683 };
1684
1685 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1686 {
1687     DeviceClass *dc = DEVICE_CLASS(klass);
1688     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1689
1690     k->realize = virtio_ccw_rng_realize;
1691     k->exit = virtio_ccw_exit;
1692     dc->reset = virtio_ccw_reset;
1693     dc->props = virtio_ccw_rng_properties;
1694     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1695 }
1696
1697 static const TypeInfo virtio_ccw_rng = {
1698     .name          = TYPE_VIRTIO_RNG_CCW,
1699     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1700     .instance_size = sizeof(VirtIORNGCcw),
1701     .instance_init = virtio_ccw_rng_instance_init,
1702     .class_init    = virtio_ccw_rng_class_init,
1703 };
1704
1705 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp)
1706 {
1707     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1708
1709     virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1710     virtio_ccw_device_realize(_dev, errp);
1711 }
1712
1713 static int virtio_ccw_busdev_exit(DeviceState *dev)
1714 {
1715     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1716     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1717
1718     return _info->exit(_dev);
1719 }
1720
1721 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1722                                      DeviceState *dev, Error **errp)
1723 {
1724     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1725     SubchDev *sch = _dev->sch;
1726
1727     virtio_ccw_stop_ioeventfd(_dev);
1728
1729     /*
1730      * We should arrive here only for device_del, since we don't support
1731      * direct hot(un)plug of channels, but only through virtio.
1732      */
1733     assert(sch != NULL);
1734     /* Subchannel is now disabled and no longer valid. */
1735     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1736                                      PMCW_FLAGS_MASK_DNV);
1737
1738     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1739
1740     object_unparent(OBJECT(dev));
1741 }
1742
1743 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1744 {
1745     DeviceClass *dc = DEVICE_CLASS(klass);
1746
1747     dc->realize = virtio_ccw_busdev_realize;
1748     dc->exit = virtio_ccw_busdev_exit;
1749     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1750 }
1751
1752 static const TypeInfo virtio_ccw_device_info = {
1753     .name = TYPE_VIRTIO_CCW_DEVICE,
1754     .parent = TYPE_DEVICE,
1755     .instance_size = sizeof(VirtioCcwDevice),
1756     .class_init = virtio_ccw_device_class_init,
1757     .class_size = sizeof(VirtIOCCWDeviceClass),
1758     .abstract = true,
1759 };
1760
1761 /***************** Virtual-css Bus Bridge Device ********************/
1762 /* Only required to have the virtio bus as child in the system bus */
1763
1764 static int virtual_css_bridge_init(SysBusDevice *dev)
1765 {
1766     /* nothing */
1767     return 0;
1768 }
1769
1770 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1771 {
1772     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1773     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1774     DeviceClass *dc = DEVICE_CLASS(klass);
1775
1776     k->init = virtual_css_bridge_init;
1777     hc->unplug = virtio_ccw_busdev_unplug;
1778     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1779 }
1780
1781 static const TypeInfo virtual_css_bridge_info = {
1782     .name          = "virtual-css-bridge",
1783     .parent        = TYPE_SYS_BUS_DEVICE,
1784     .instance_size = sizeof(SysBusDevice),
1785     .class_init    = virtual_css_bridge_class_init,
1786     .interfaces = (InterfaceInfo[]) {
1787         { TYPE_HOTPLUG_HANDLER },
1788         { }
1789     }
1790 };
1791
1792 /* virtio-ccw-bus */
1793
1794 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1795                                VirtioCcwDevice *dev)
1796 {
1797     DeviceState *qdev = DEVICE(dev);
1798     char virtio_bus_name[] = "virtio-bus";
1799
1800     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1801                         qdev, virtio_bus_name);
1802 }
1803
1804 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1805 {
1806     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1807     BusClass *bus_class = BUS_CLASS(klass);
1808
1809     bus_class->max_dev = 1;
1810     k->notify = virtio_ccw_notify;
1811     k->vmstate_change = virtio_ccw_vmstate_change;
1812     k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1813     k->set_host_notifier = virtio_ccw_set_host_notifier;
1814     k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1815     k->save_queue = virtio_ccw_save_queue;
1816     k->load_queue = virtio_ccw_load_queue;
1817     k->save_config = virtio_ccw_save_config;
1818     k->load_config = virtio_ccw_load_config;
1819     k->device_plugged = virtio_ccw_device_plugged;
1820     k->post_plugged = virtio_ccw_post_plugged;
1821     k->device_unplugged = virtio_ccw_device_unplugged;
1822 }
1823
1824 static const TypeInfo virtio_ccw_bus_info = {
1825     .name = TYPE_VIRTIO_CCW_BUS,
1826     .parent = TYPE_VIRTIO_BUS,
1827     .instance_size = sizeof(VirtioCcwBusState),
1828     .class_init = virtio_ccw_bus_class_init,
1829 };
1830
1831 #ifdef CONFIG_VIRTFS
1832 static Property virtio_ccw_9p_properties[] = {
1833     DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, bus_id),
1834     DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1835             VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1836     DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev,
1837                        VIRTIO_CCW_MAX_REV),
1838     DEFINE_PROP_END_OF_LIST(),
1839 };
1840
1841 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp)
1842 {
1843     V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev);
1844     DeviceState *vdev = DEVICE(&dev->vdev);
1845     Error *err = NULL;
1846
1847     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
1848     object_property_set_bool(OBJECT(vdev), true, "realized", &err);
1849     error_propagate(errp, err);
1850 }
1851
1852 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data)
1853 {
1854     DeviceClass *dc = DEVICE_CLASS(klass);
1855     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1856
1857     k->exit = virtio_ccw_exit;
1858     k->realize = virtio_ccw_9p_realize;
1859     dc->reset = virtio_ccw_reset;
1860     dc->props = virtio_ccw_9p_properties;
1861     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1862 }
1863
1864 static void virtio_ccw_9p_instance_init(Object *obj)
1865 {
1866     V9fsCCWState *dev = VIRTIO_9P_CCW(obj);
1867
1868     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1869                                 TYPE_VIRTIO_9P);
1870 }
1871
1872 static const TypeInfo virtio_ccw_9p_info = {
1873     .name          = TYPE_VIRTIO_9P_CCW,
1874     .parent        = TYPE_VIRTIO_CCW_DEVICE,
1875     .instance_size = sizeof(V9fsCCWState),
1876     .instance_init = virtio_ccw_9p_instance_init,
1877     .class_init    = virtio_ccw_9p_class_init,
1878 };
1879 #endif
1880
1881 static void virtio_ccw_register(void)
1882 {
1883     type_register_static(&virtio_ccw_bus_info);
1884     type_register_static(&virtual_css_bus_info);
1885     type_register_static(&virtio_ccw_device_info);
1886     type_register_static(&virtio_ccw_serial);
1887     type_register_static(&virtio_ccw_blk);
1888     type_register_static(&virtio_ccw_net);
1889     type_register_static(&virtio_ccw_balloon);
1890     type_register_static(&virtio_ccw_scsi);
1891 #ifdef CONFIG_VHOST_SCSI
1892     type_register_static(&vhost_ccw_scsi);
1893 #endif
1894     type_register_static(&virtio_ccw_rng);
1895     type_register_static(&virtual_css_bridge_info);
1896 #ifdef CONFIG_VIRTFS
1897     type_register_static(&virtio_ccw_9p_info);
1898 #endif
1899 }
1900
1901 type_init(virtio_ccw_register)
This page took 0.138502 seconds and 4 git commands to generate.