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