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