xref: /openbmc/qemu/hw/s390x/virtio-ccw.c (revision 34b95b2c)
1 /*
2  * virtio ccw target implementation
3  *
4  * Copyright 2012 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 "block/block.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 
25 #include "ioinst.h"
26 #include "css.h"
27 #include "virtio-ccw.h"
28 #include "trace.h"
29 
30 static int virtual_css_bus_reset(BusState *qbus)
31 {
32     /* This should actually be modelled via the generic css */
33     css_reset();
34 
35     /* we dont traverse ourself, return 0 */
36     return 0;
37 }
38 
39 
40 static void virtual_css_bus_class_init(ObjectClass *klass, void *data)
41 {
42     BusClass *k = BUS_CLASS(klass);
43 
44     k->reset = virtual_css_bus_reset;
45 }
46 
47 static const TypeInfo virtual_css_bus_info = {
48     .name = TYPE_VIRTUAL_CSS_BUS,
49     .parent = TYPE_BUS,
50     .instance_size = sizeof(VirtualCssBus),
51     .class_init = virtual_css_bus_class_init,
52 };
53 
54 static const VirtIOBindings virtio_ccw_bindings;
55 
56 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
57 {
58     VirtIODevice *vdev = NULL;
59 
60     if (sch->driver_data) {
61         vdev = ((VirtioCcwDevice *)sch->driver_data)->vdev;
62     }
63     return vdev;
64 }
65 
66 VirtualCssBus *virtual_css_bus_init(void)
67 {
68     VirtualCssBus *cbus;
69     BusState *bus;
70     DeviceState *dev;
71 
72     /* Create bridge device */
73     dev = qdev_create(NULL, "virtual-css-bridge");
74     qdev_init_nofail(dev);
75 
76     /* Create bus on bridge device */
77     bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
78     cbus = VIRTUAL_CSS_BUS(bus);
79 
80     /* Enable hotplugging */
81     bus->allow_hotplug = 1;
82 
83     return cbus;
84 }
85 
86 /* Communication blocks used by several channel commands. */
87 typedef struct VqInfoBlock {
88     uint64_t queue;
89     uint32_t align;
90     uint16_t index;
91     uint16_t num;
92 } QEMU_PACKED VqInfoBlock;
93 
94 typedef struct VqConfigBlock {
95     uint16_t index;
96     uint16_t num_max;
97 } QEMU_PACKED VqConfigBlock;
98 
99 typedef struct VirtioFeatDesc {
100     uint32_t features;
101     uint8_t index;
102 } QEMU_PACKED VirtioFeatDesc;
103 
104 /* Specify where the virtqueues for the subchannel are in guest memory. */
105 static int virtio_ccw_set_vqs(SubchDev *sch, uint64_t addr, uint32_t align,
106                               uint16_t index, uint16_t num)
107 {
108     VirtioCcwDevice *dev = sch->driver_data;
109 
110     if (index > VIRTIO_PCI_QUEUE_MAX) {
111         return -EINVAL;
112     }
113 
114     /* Current code in virtio.c relies on 4K alignment. */
115     if (addr && (align != 4096)) {
116         return -EINVAL;
117     }
118 
119     if (!dev) {
120         return -EINVAL;
121     }
122 
123     virtio_queue_set_addr(dev->vdev, index, addr);
124     if (!addr) {
125         virtio_queue_set_vector(dev->vdev, index, 0);
126     } else {
127         /* Fail if we don't have a big enough queue. */
128         /* TODO: Add interface to handle vring.num changing */
129         if (virtio_queue_get_num(dev->vdev, index) > num) {
130             return -EINVAL;
131         }
132         virtio_queue_set_vector(dev->vdev, index, index);
133     }
134     /* tell notify handler in case of config change */
135     dev->vdev->config_vector = VIRTIO_PCI_QUEUE_MAX;
136     return 0;
137 }
138 
139 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
140 {
141     int ret;
142     VqInfoBlock info;
143     uint8_t status;
144     VirtioFeatDesc features;
145     void *config;
146     hwaddr indicators;
147     VqConfigBlock vq_config;
148     VirtioCcwDevice *dev = sch->driver_data;
149     bool check_len;
150     int len;
151     hwaddr hw_len;
152 
153     if (!dev) {
154         return -EINVAL;
155     }
156 
157     trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
158                                    ccw.cmd_code);
159     check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
160 
161     /* Look at the command. */
162     switch (ccw.cmd_code) {
163     case CCW_CMD_SET_VQ:
164         if (check_len) {
165             if (ccw.count != sizeof(info)) {
166                 ret = -EINVAL;
167                 break;
168             }
169         } else if (ccw.count < sizeof(info)) {
170             /* Can't execute command. */
171             ret = -EINVAL;
172             break;
173         }
174         if (!ccw.cda) {
175             ret = -EFAULT;
176         } else {
177             info.queue = ldq_phys(ccw.cda);
178             info.align = ldl_phys(ccw.cda + sizeof(info.queue));
179             info.index = lduw_phys(ccw.cda + sizeof(info.queue)
180                                    + sizeof(info.align));
181             info.num = lduw_phys(ccw.cda + sizeof(info.queue)
182                                  + sizeof(info.align)
183                                  + sizeof(info.index));
184             ret = virtio_ccw_set_vqs(sch, info.queue, info.align, info.index,
185                                      info.num);
186             sch->curr_status.scsw.count = 0;
187         }
188         break;
189     case CCW_CMD_VDEV_RESET:
190         virtio_reset(dev->vdev);
191         ret = 0;
192         break;
193     case CCW_CMD_READ_FEAT:
194         if (check_len) {
195             if (ccw.count != sizeof(features)) {
196                 ret = -EINVAL;
197                 break;
198             }
199         } else if (ccw.count < sizeof(features)) {
200             /* Can't execute command. */
201             ret = -EINVAL;
202             break;
203         }
204         if (!ccw.cda) {
205             ret = -EFAULT;
206         } else {
207             features.index = ldub_phys(ccw.cda + sizeof(features.features));
208             if (features.index < ARRAY_SIZE(dev->host_features)) {
209                 features.features = dev->host_features[features.index];
210             } else {
211                 /* Return zeroes if the guest supports more feature bits. */
212                 features.features = 0;
213             }
214             stl_le_phys(ccw.cda, features.features);
215             sch->curr_status.scsw.count = ccw.count - sizeof(features);
216             ret = 0;
217         }
218         break;
219     case CCW_CMD_WRITE_FEAT:
220         if (check_len) {
221             if (ccw.count != sizeof(features)) {
222                 ret = -EINVAL;
223                 break;
224             }
225         } else if (ccw.count < sizeof(features)) {
226             /* Can't execute command. */
227             ret = -EINVAL;
228             break;
229         }
230         if (!ccw.cda) {
231             ret = -EFAULT;
232         } else {
233             features.index = ldub_phys(ccw.cda + sizeof(features.features));
234             features.features = ldl_le_phys(ccw.cda);
235             if (features.index < ARRAY_SIZE(dev->host_features)) {
236                 if (dev->vdev->set_features) {
237                     dev->vdev->set_features(dev->vdev, features.features);
238                 }
239                 dev->vdev->guest_features = features.features;
240             } else {
241                 /*
242                  * If the guest supports more feature bits, assert that it
243                  * passes us zeroes for those we don't support.
244                  */
245                 if (features.features) {
246                     fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
247                             features.index, features.features);
248                     /* XXX: do a unit check here? */
249                 }
250             }
251             sch->curr_status.scsw.count = ccw.count - sizeof(features);
252             ret = 0;
253         }
254         break;
255     case CCW_CMD_READ_CONF:
256         if (check_len) {
257             if (ccw.count > dev->vdev->config_len) {
258                 ret = -EINVAL;
259                 break;
260             }
261         }
262         len = MIN(ccw.count, dev->vdev->config_len);
263         if (!ccw.cda) {
264             ret = -EFAULT;
265         } else {
266             dev->vdev->get_config(dev->vdev, dev->vdev->config);
267             /* XXX config space endianness */
268             cpu_physical_memory_write(ccw.cda, dev->vdev->config, len);
269             sch->curr_status.scsw.count = ccw.count - len;
270             ret = 0;
271         }
272         break;
273     case CCW_CMD_WRITE_CONF:
274         if (check_len) {
275             if (ccw.count > dev->vdev->config_len) {
276                 ret = -EINVAL;
277                 break;
278             }
279         }
280         len = MIN(ccw.count, dev->vdev->config_len);
281         hw_len = len;
282         if (!ccw.cda) {
283             ret = -EFAULT;
284         } else {
285             config = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
286             if (!config) {
287                 ret = -EFAULT;
288             } else {
289                 len = hw_len;
290                 /* XXX config space endianness */
291                 memcpy(dev->vdev->config, config, len);
292                 cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
293                 if (dev->vdev->set_config) {
294                     dev->vdev->set_config(dev->vdev, dev->vdev->config);
295                 }
296                 sch->curr_status.scsw.count = ccw.count - len;
297                 ret = 0;
298             }
299         }
300         break;
301     case CCW_CMD_WRITE_STATUS:
302         if (check_len) {
303             if (ccw.count != sizeof(status)) {
304                 ret = -EINVAL;
305                 break;
306             }
307         } else if (ccw.count < sizeof(status)) {
308             /* Can't execute command. */
309             ret = -EINVAL;
310             break;
311         }
312         if (!ccw.cda) {
313             ret = -EFAULT;
314         } else {
315             status = ldub_phys(ccw.cda);
316             virtio_set_status(dev->vdev, status);
317             if (dev->vdev->status == 0) {
318                 virtio_reset(dev->vdev);
319             }
320             sch->curr_status.scsw.count = ccw.count - sizeof(status);
321             ret = 0;
322         }
323         break;
324     case CCW_CMD_SET_IND:
325         if (check_len) {
326             if (ccw.count != sizeof(indicators)) {
327                 ret = -EINVAL;
328                 break;
329             }
330         } else if (ccw.count < sizeof(indicators)) {
331             /* Can't execute command. */
332             ret = -EINVAL;
333             break;
334         }
335         indicators = ldq_phys(ccw.cda);
336         if (!indicators) {
337             ret = -EFAULT;
338         } else {
339             dev->indicators = indicators;
340             sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
341             ret = 0;
342         }
343         break;
344     case CCW_CMD_SET_CONF_IND:
345         if (check_len) {
346             if (ccw.count != sizeof(indicators)) {
347                 ret = -EINVAL;
348                 break;
349             }
350         } else if (ccw.count < sizeof(indicators)) {
351             /* Can't execute command. */
352             ret = -EINVAL;
353             break;
354         }
355         indicators = ldq_phys(ccw.cda);
356         if (!indicators) {
357             ret = -EFAULT;
358         } else {
359             dev->indicators2 = indicators;
360             sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
361             ret = 0;
362         }
363         break;
364     case CCW_CMD_READ_VQ_CONF:
365         if (check_len) {
366             if (ccw.count != sizeof(vq_config)) {
367                 ret = -EINVAL;
368                 break;
369             }
370         } else if (ccw.count < sizeof(vq_config)) {
371             /* Can't execute command. */
372             ret = -EINVAL;
373             break;
374         }
375         if (!ccw.cda) {
376             ret = -EFAULT;
377         } else {
378             vq_config.index = lduw_phys(ccw.cda);
379             vq_config.num_max = virtio_queue_get_num(dev->vdev,
380                                                      vq_config.index);
381             stw_phys(ccw.cda + sizeof(vq_config.index), vq_config.num_max);
382             sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
383             ret = 0;
384         }
385         break;
386     default:
387         ret = -ENOSYS;
388         break;
389     }
390     return ret;
391 }
392 
393 static int virtio_ccw_device_init(VirtioCcwDevice *dev, VirtIODevice *vdev)
394 {
395     unsigned int cssid = 0;
396     unsigned int ssid = 0;
397     unsigned int schid;
398     unsigned int devno;
399     bool have_devno = false;
400     bool found = false;
401     SubchDev *sch;
402     int ret;
403     int num;
404     DeviceState *parent = DEVICE(dev);
405 
406     sch = g_malloc0(sizeof(SubchDev));
407 
408     sch->driver_data = dev;
409     dev->sch = sch;
410 
411     dev->vdev = vdev;
412     dev->indicators = 0;
413 
414     /* Initialize subchannel structure. */
415     sch->channel_prog = 0x0;
416     sch->last_cmd_valid = false;
417     sch->orb = NULL;
418     /*
419      * Use a device number if provided. Otherwise, fall back to subchannel
420      * number.
421      */
422     if (dev->bus_id) {
423         num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
424         if (num == 3) {
425             if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
426                 ret = -EINVAL;
427                 error_report("Invalid cssid or ssid: cssid %x, ssid %x",
428                              cssid, ssid);
429                 goto out_err;
430             }
431             /* Enforce use of virtual cssid. */
432             if (cssid != VIRTUAL_CSSID) {
433                 ret = -EINVAL;
434                 error_report("cssid %x not valid for virtio devices", cssid);
435                 goto out_err;
436             }
437             if (css_devno_used(cssid, ssid, devno)) {
438                 ret = -EEXIST;
439                 error_report("Device %x.%x.%04x already exists", cssid, ssid,
440                              devno);
441                 goto out_err;
442             }
443             sch->cssid = cssid;
444             sch->ssid = ssid;
445             sch->devno = devno;
446             have_devno = true;
447         } else {
448             ret = -EINVAL;
449             error_report("Malformed devno parameter '%s'", dev->bus_id);
450             goto out_err;
451         }
452     }
453 
454     /* Find the next free id. */
455     if (have_devno) {
456         for (schid = 0; schid <= MAX_SCHID; schid++) {
457             if (!css_find_subch(1, cssid, ssid, schid)) {
458                 sch->schid = schid;
459                 css_subch_assign(cssid, ssid, schid, devno, sch);
460                 found = true;
461                 break;
462             }
463         }
464         if (!found) {
465             ret = -ENODEV;
466             error_report("No free subchannel found for %x.%x.%04x", cssid, ssid,
467                          devno);
468             goto out_err;
469         }
470         trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
471                                     "user-configured");
472     } else {
473         cssid = VIRTUAL_CSSID;
474         for (ssid = 0; ssid <= MAX_SSID; ssid++) {
475             for (schid = 0; schid <= MAX_SCHID; schid++) {
476                 if (!css_find_subch(1, cssid, ssid, schid)) {
477                     sch->cssid = cssid;
478                     sch->ssid = ssid;
479                     sch->schid = schid;
480                     devno = schid;
481                     /*
482                      * If the devno is already taken, look further in this
483                      * subchannel set.
484                      */
485                     while (css_devno_used(cssid, ssid, devno)) {
486                         if (devno == MAX_SCHID) {
487                             devno = 0;
488                         } else if (devno == schid - 1) {
489                             ret = -ENODEV;
490                             error_report("No free devno found");
491                             goto out_err;
492                         } else {
493                             devno++;
494                         }
495                     }
496                     sch->devno = devno;
497                     css_subch_assign(cssid, ssid, schid, devno, sch);
498                     found = true;
499                     break;
500                 }
501             }
502             if (found) {
503                 break;
504             }
505         }
506         if (!found) {
507             ret = -ENODEV;
508             error_report("Virtual channel subsystem is full!");
509             goto out_err;
510         }
511         trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
512                                     "auto-configured");
513     }
514 
515     /* Build initial schib. */
516     css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
517 
518     sch->ccw_cb = virtio_ccw_cb;
519 
520     /* Build senseid data. */
521     memset(&sch->id, 0, sizeof(SenseId));
522     sch->id.reserved = 0xff;
523     sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
524     sch->id.cu_model = dev->vdev->device_id;
525 
526     virtio_bind_device(vdev, &virtio_ccw_bindings, DEVICE(dev));
527     /* Only the first 32 feature bits are used. */
528     dev->host_features[0] = vdev->get_features(vdev, dev->host_features[0]);
529     dev->host_features[0] |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
530     dev->host_features[0] |= 0x1 << VIRTIO_F_BAD_FEATURE;
531 
532     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
533                           parent->hotplugged, 1);
534     return 0;
535 
536 out_err:
537     dev->sch = NULL;
538     g_free(sch);
539     return ret;
540 }
541 
542 static int virtio_ccw_exit(VirtioCcwDevice *dev)
543 {
544     SubchDev *sch = dev->sch;
545 
546     if (sch) {
547         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
548         g_free(sch);
549     }
550     dev->indicators = 0;
551     return 0;
552 }
553 
554 static int virtio_ccw_net_init(VirtioCcwDevice *dev)
555 {
556     VirtIODevice *vdev;
557 
558     vdev = virtio_net_init((DeviceState *)dev, &dev->nic, &dev->net,
559                            dev->host_features[0]);
560     if (!vdev) {
561         return -1;
562     }
563 
564     return virtio_ccw_device_init(dev, vdev);
565 }
566 
567 static int virtio_ccw_net_exit(VirtioCcwDevice *dev)
568 {
569     virtio_net_exit(dev->vdev);
570     return virtio_ccw_exit(dev);
571 }
572 
573 static int virtio_ccw_blk_init(VirtioCcwDevice *ccw_dev)
574 {
575     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
576     DeviceState *vdev = DEVICE(&dev->vdev);
577     virtio_blk_set_conf(vdev, &(dev->blk));
578     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
579     if (qdev_init(vdev) < 0) {
580         return -1;
581     }
582 
583     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
584 }
585 
586 static void virtio_ccw_blk_instance_init(Object *obj)
587 {
588     VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
589     object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BLK);
590     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
591 }
592 
593 static int virtio_ccw_serial_init(VirtioCcwDevice *ccw_dev)
594 {
595     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
596     DeviceState *vdev = DEVICE(&dev->vdev);
597 
598     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
599     if (qdev_init(vdev) < 0) {
600         return -1;
601     }
602 
603     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
604 }
605 
606 
607 static void virtio_ccw_serial_instance_init(Object *obj)
608 {
609     VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
610     object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SERIAL);
611     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
612 }
613 
614 static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev)
615 {
616     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
617     DeviceState *vdev = DEVICE(&dev->vdev);
618 
619     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
620     if (qdev_init(vdev) < 0) {
621         return -1;
622     }
623 
624     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
625 }
626 
627 static void virtio_ccw_balloon_instance_init(Object *obj)
628 {
629     VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
630     object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON);
631     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
632 }
633 
634 static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
635 {
636     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
637     DeviceState *vdev = DEVICE(&dev->vdev);
638 
639     qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
640     if (qdev_init(vdev) < 0) {
641         return -1;
642     }
643 
644     return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
645 }
646 
647 static void virtio_ccw_scsi_instance_init(Object *obj)
648 {
649     VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
650     object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SCSI);
651     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
652 }
653 
654 static int virtio_ccw_rng_init(VirtioCcwDevice *dev)
655 {
656     VirtIODevice *vdev;
657 
658     if (dev->rng.rng == NULL) {
659         dev->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
660         object_property_add_child(OBJECT(dev), "default-backend",
661                                   OBJECT(dev->rng.default_backend), NULL);
662         object_property_set_link(OBJECT(dev), OBJECT(dev->rng.default_backend),
663                                  "rng", NULL);
664     }
665     vdev = virtio_rng_init((DeviceState *)dev, &dev->rng);
666     if (!vdev) {
667         return -1;
668     }
669     return virtio_ccw_device_init(dev, vdev);
670 }
671 
672 static int virtio_ccw_rng_exit(VirtioCcwDevice *dev)
673 {
674     virtio_rng_exit(dev->vdev);
675     return virtio_ccw_exit(dev);
676 }
677 
678 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
679  * be careful and test performance if you change this.
680  */
681 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
682 {
683     return container_of(d, VirtioCcwDevice, parent_obj);
684 }
685 
686 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
687 {
688     VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
689     SubchDev *sch = dev->sch;
690     uint64_t indicators;
691 
692     if (vector >= 128) {
693         return;
694     }
695 
696     if (vector < VIRTIO_PCI_QUEUE_MAX) {
697         indicators = ldq_phys(dev->indicators);
698         indicators |= 1ULL << vector;
699         stq_phys(dev->indicators, indicators);
700     } else {
701         vector = 0;
702         indicators = ldq_phys(dev->indicators2);
703         indicators |= 1ULL << vector;
704         stq_phys(dev->indicators2, indicators);
705     }
706 
707     css_conditional_io_interrupt(sch);
708 
709 }
710 
711 static unsigned virtio_ccw_get_features(DeviceState *d)
712 {
713     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
714 
715     /* Only the first 32 feature bits are used. */
716     return dev->host_features[0];
717 }
718 
719 static void virtio_ccw_reset(DeviceState *d)
720 {
721     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
722 
723     virtio_reset(dev->vdev);
724     css_reset_sch(dev->sch);
725 }
726 
727 /**************** Virtio-ccw Bus Device Descriptions *******************/
728 
729 static const VirtIOBindings virtio_ccw_bindings = {
730     .notify = virtio_ccw_notify,
731     .get_features = virtio_ccw_get_features,
732 };
733 
734 static Property virtio_ccw_net_properties[] = {
735     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
736     DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]),
737     DEFINE_NIC_PROPERTIES(VirtioCcwDevice, nic),
738     DEFINE_PROP_UINT32("x-txtimer", VirtioCcwDevice,
739                        net.txtimer, TX_TIMER_INTERVAL),
740     DEFINE_PROP_INT32("x-txburst", VirtioCcwDevice,
741                       net.txburst, TX_BURST),
742     DEFINE_PROP_STRING("tx", VirtioCcwDevice, net.tx),
743     DEFINE_PROP_END_OF_LIST(),
744 };
745 
746 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
747 {
748     DeviceClass *dc = DEVICE_CLASS(klass);
749     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
750 
751     k->init = virtio_ccw_net_init;
752     k->exit = virtio_ccw_net_exit;
753     dc->reset = virtio_ccw_reset;
754     dc->props = virtio_ccw_net_properties;
755 }
756 
757 static const TypeInfo virtio_ccw_net = {
758     .name          = "virtio-net-ccw",
759     .parent        = TYPE_VIRTIO_CCW_DEVICE,
760     .instance_size = sizeof(VirtioCcwDevice),
761     .class_init    = virtio_ccw_net_class_init,
762 };
763 
764 static Property virtio_ccw_blk_properties[] = {
765     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
766     DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice, host_features[0]),
767     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkCcw, blk),
768     DEFINE_PROP_END_OF_LIST(),
769 };
770 
771 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
772 {
773     DeviceClass *dc = DEVICE_CLASS(klass);
774     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
775 
776     k->init = virtio_ccw_blk_init;
777     k->exit = virtio_ccw_exit;
778     dc->reset = virtio_ccw_reset;
779     dc->props = virtio_ccw_blk_properties;
780 }
781 
782 static const TypeInfo virtio_ccw_blk = {
783     .name          = TYPE_VIRTIO_BLK_CCW,
784     .parent        = TYPE_VIRTIO_CCW_DEVICE,
785     .instance_size = sizeof(VirtIOBlkCcw),
786     .instance_init = virtio_ccw_blk_instance_init,
787     .class_init    = virtio_ccw_blk_class_init,
788 };
789 
790 static Property virtio_ccw_serial_properties[] = {
791     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
792     DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtioSerialCcw, vdev.serial),
793     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
794     DEFINE_PROP_END_OF_LIST(),
795 };
796 
797 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
798 {
799     DeviceClass *dc = DEVICE_CLASS(klass);
800     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
801 
802     k->init = virtio_ccw_serial_init;
803     k->exit = virtio_ccw_exit;
804     dc->reset = virtio_ccw_reset;
805     dc->props = virtio_ccw_serial_properties;
806 }
807 
808 static const TypeInfo virtio_ccw_serial = {
809     .name          = TYPE_VIRTIO_SERIAL_CCW,
810     .parent        = TYPE_VIRTIO_CCW_DEVICE,
811     .instance_size = sizeof(VirtioSerialCcw),
812     .instance_init = virtio_ccw_serial_instance_init,
813     .class_init    = virtio_ccw_serial_class_init,
814 };
815 
816 static Property virtio_ccw_balloon_properties[] = {
817     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
818     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
819     DEFINE_PROP_END_OF_LIST(),
820 };
821 
822 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
823 {
824     DeviceClass *dc = DEVICE_CLASS(klass);
825     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
826 
827     k->init = virtio_ccw_balloon_init;
828     k->exit = virtio_ccw_exit;
829     dc->reset = virtio_ccw_reset;
830     dc->props = virtio_ccw_balloon_properties;
831 }
832 
833 static const TypeInfo virtio_ccw_balloon = {
834     .name          = TYPE_VIRTIO_BALLOON_CCW,
835     .parent        = TYPE_VIRTIO_CCW_DEVICE,
836     .instance_size = sizeof(VirtIOBalloonCcw),
837     .instance_init = virtio_ccw_balloon_instance_init,
838     .class_init    = virtio_ccw_balloon_class_init,
839 };
840 
841 static Property virtio_ccw_scsi_properties[] = {
842     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
843     DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.conf),
844     DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]),
845     DEFINE_PROP_END_OF_LIST(),
846 };
847 
848 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
849 {
850     DeviceClass *dc = DEVICE_CLASS(klass);
851     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
852 
853     k->init = virtio_ccw_scsi_init;
854     k->exit = virtio_ccw_exit;
855     dc->reset = virtio_ccw_reset;
856     dc->props = virtio_ccw_scsi_properties;
857 }
858 
859 static const TypeInfo virtio_ccw_scsi = {
860     .name          = TYPE_VIRTIO_SCSI_CCW,
861     .parent        = TYPE_VIRTIO_CCW_DEVICE,
862     .instance_size = sizeof(VirtIOSCSICcw),
863     .instance_init = virtio_ccw_scsi_instance_init,
864     .class_init    = virtio_ccw_scsi_class_init,
865 };
866 
867 static void virtio_ccw_rng_initfn(Object *obj)
868 {
869     VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(obj);
870 
871     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
872                              (Object **)&dev->rng.rng, NULL);
873 }
874 
875 static Property virtio_ccw_rng_properties[] = {
876     DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
877     DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
878     DEFINE_PROP_UINT64("max-bytes", VirtioCcwDevice, rng.max_bytes, INT64_MAX),
879     DEFINE_PROP_UINT32("period", VirtioCcwDevice, rng.period_ms, 1 << 16),
880     DEFINE_PROP_END_OF_LIST(),
881 };
882 
883 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
884 {
885     DeviceClass *dc = DEVICE_CLASS(klass);
886     VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
887 
888     k->init = virtio_ccw_rng_init;
889     k->exit = virtio_ccw_rng_exit;
890     dc->reset = virtio_ccw_reset;
891     dc->props = virtio_ccw_rng_properties;
892 }
893 
894 static const TypeInfo virtio_ccw_rng = {
895     .name          = "virtio-rng-ccw",
896     .parent        = TYPE_VIRTIO_CCW_DEVICE,
897     .instance_size = sizeof(VirtioCcwDevice),
898     .instance_init = virtio_ccw_rng_initfn,
899     .class_init    = virtio_ccw_rng_class_init,
900 };
901 
902 static int virtio_ccw_busdev_init(DeviceState *dev)
903 {
904     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
905     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
906 
907     virtio_ccw_bus_new(&_dev->bus, _dev);
908 
909     return _info->init(_dev);
910 }
911 
912 static int virtio_ccw_busdev_exit(DeviceState *dev)
913 {
914     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
915     VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
916 
917     return _info->exit(_dev);
918 }
919 
920 static int virtio_ccw_busdev_unplug(DeviceState *dev)
921 {
922     VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
923     SubchDev *sch = _dev->sch;
924 
925     /*
926      * We should arrive here only for device_del, since we don't support
927      * direct hot(un)plug of channels, but only through virtio.
928      */
929     assert(sch != NULL);
930     /* Subchannel is now disabled and no longer valid. */
931     sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
932                                      PMCW_FLAGS_MASK_DNV);
933 
934     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
935 
936     qdev_free(dev);
937     return 0;
938 }
939 
940 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
941 {
942     DeviceClass *dc = DEVICE_CLASS(klass);
943 
944     dc->init = virtio_ccw_busdev_init;
945     dc->exit = virtio_ccw_busdev_exit;
946     dc->unplug = virtio_ccw_busdev_unplug;
947     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
948 
949 }
950 
951 static const TypeInfo virtio_ccw_device_info = {
952     .name = TYPE_VIRTIO_CCW_DEVICE,
953     .parent = TYPE_DEVICE,
954     .instance_size = sizeof(VirtioCcwDevice),
955     .class_init = virtio_ccw_device_class_init,
956     .class_size = sizeof(VirtIOCCWDeviceClass),
957     .abstract = true,
958 };
959 
960 /***************** Virtual-css Bus Bridge Device ********************/
961 /* Only required to have the virtio bus as child in the system bus */
962 
963 static int virtual_css_bridge_init(SysBusDevice *dev)
964 {
965     /* nothing */
966     return 0;
967 }
968 
969 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
970 {
971     DeviceClass *dc = DEVICE_CLASS(klass);
972     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
973 
974     k->init = virtual_css_bridge_init;
975     dc->no_user = 1;
976 }
977 
978 static const TypeInfo virtual_css_bridge_info = {
979     .name          = "virtual-css-bridge",
980     .parent        = TYPE_SYS_BUS_DEVICE,
981     .instance_size = sizeof(SysBusDevice),
982     .class_init    = virtual_css_bridge_class_init,
983 };
984 
985 /* virtio-ccw-bus */
986 
987 void virtio_ccw_bus_new(VirtioBusState *bus, VirtioCcwDevice *dev)
988 {
989     DeviceState *qdev = DEVICE(dev);
990     BusState *qbus;
991 
992     qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_CCW_BUS, qdev, NULL);
993     qbus = BUS(bus);
994     qbus->allow_hotplug = 1;
995 }
996 
997 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
998 {
999     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1000     BusClass *bus_class = BUS_CLASS(klass);
1001 
1002     bus_class->max_dev = 1;
1003     k->notify = virtio_ccw_notify;
1004     k->get_features = virtio_ccw_get_features;
1005 }
1006 
1007 static const TypeInfo virtio_ccw_bus_info = {
1008     .name = TYPE_VIRTIO_CCW_BUS,
1009     .parent = TYPE_VIRTIO_BUS,
1010     .instance_size = sizeof(VirtioCcwBusState),
1011     .class_init = virtio_ccw_bus_class_init,
1012 };
1013 
1014 static void virtio_ccw_register(void)
1015 {
1016     type_register_static(&virtio_ccw_bus_info);
1017     type_register_static(&virtual_css_bus_info);
1018     type_register_static(&virtio_ccw_device_info);
1019     type_register_static(&virtio_ccw_serial);
1020     type_register_static(&virtio_ccw_blk);
1021     type_register_static(&virtio_ccw_net);
1022     type_register_static(&virtio_ccw_balloon);
1023     type_register_static(&virtio_ccw_scsi);
1024     type_register_static(&virtio_ccw_rng);
1025     type_register_static(&virtual_css_bridge_info);
1026 }
1027 
1028 type_init(virtio_ccw_register)
1029