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