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