pc: acpi-build: generate pvpanic device description dynamically
[qemu/qmp-unstable.git] / hw / s390x / virtio-ccw.c
blob3fee4aa3457004096b9c7564ec2cc1a5208684e2
1 /*
2 * virtio ccw target implementation
4 * Copyright 2012,2014 IBM Corp.
5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
9 * directory.
12 #include "hw/hw.h"
13 #include "sysemu/block-backend.h"
14 #include "sysemu/blockdev.h"
15 #include "sysemu/sysemu.h"
16 #include "net/net.h"
17 #include "monitor/monitor.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/virtio/virtio-serial.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "hw/sysbus.h"
22 #include "qemu/bitops.h"
23 #include "hw/virtio/virtio-bus.h"
24 #include "hw/s390x/adapter.h"
25 #include "hw/s390x/s390_flic.h"
27 #include "ioinst.h"
28 #include "css.h"
29 #include "virtio-ccw.h"
30 #include "trace.h"
32 static QTAILQ_HEAD(, IndAddr) indicator_addresses =
33 QTAILQ_HEAD_INITIALIZER(indicator_addresses);
35 static IndAddr *get_indicator(hwaddr ind_addr, int len)
37 IndAddr *indicator;
39 QTAILQ_FOREACH(indicator, &indicator_addresses, sibling) {
40 if (indicator->addr == ind_addr) {
41 indicator->refcnt++;
42 return indicator;
45 indicator = g_new0(IndAddr, 1);
46 indicator->addr = ind_addr;
47 indicator->len = len;
48 indicator->refcnt = 1;
49 QTAILQ_INSERT_TAIL(&indicator_addresses, indicator, sibling);
50 return indicator;
53 static int s390_io_adapter_map(AdapterInfo *adapter, uint64_t map_addr,
54 bool do_map)
56 S390FLICState *fs = s390_get_flic();
57 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
59 return fsc->io_adapter_map(fs, adapter->adapter_id, map_addr, do_map);
62 static void release_indicator(AdapterInfo *adapter, IndAddr *indicator)
64 assert(indicator->refcnt > 0);
65 indicator->refcnt--;
66 if (indicator->refcnt > 0) {
67 return;
69 QTAILQ_REMOVE(&indicator_addresses, indicator, sibling);
70 if (indicator->map) {
71 s390_io_adapter_map(adapter, indicator->map, false);
73 g_free(indicator);
76 static int map_indicator(AdapterInfo *adapter, IndAddr *indicator)
78 int ret;
80 if (indicator->map) {
81 return 0; /* already mapped is not an error */
83 indicator->map = indicator->addr;
84 ret = s390_io_adapter_map(adapter, indicator->map, true);
85 if ((ret != 0) && (ret != -ENOSYS)) {
86 goto out_err;
88 return 0;
90 out_err:
91 indicator->map = 0;
92 return ret;
95 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
96 VirtioCcwDevice *dev);
98 static void virtual_css_bus_reset(BusState *qbus)
100 /* This should actually be modelled via the generic css */
101 css_reset();
105 static void virtual_css_bus_class_init(ObjectClass *klass, void *data)
107 BusClass *k = BUS_CLASS(klass);
109 k->reset = virtual_css_bus_reset;
112 static const TypeInfo virtual_css_bus_info = {
113 .name = TYPE_VIRTUAL_CSS_BUS,
114 .parent = TYPE_BUS,
115 .instance_size = sizeof(VirtualCssBus),
116 .class_init = virtual_css_bus_class_init,
119 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)
121 VirtIODevice *vdev = NULL;
122 VirtioCcwDevice *dev = sch->driver_data;
124 if (dev) {
125 vdev = virtio_bus_get_device(&dev->bus);
127 return vdev;
130 static int virtio_ccw_set_guest2host_notifier(VirtioCcwDevice *dev, int n,
131 bool assign, bool set_handler)
133 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
134 VirtQueue *vq = virtio_get_queue(vdev, n);
135 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
136 int r = 0;
137 SubchDev *sch = dev->sch;
138 uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid;
140 if (assign) {
141 r = event_notifier_init(notifier, 1);
142 if (r < 0) {
143 error_report("%s: unable to init event notifier: %d", __func__, r);
144 return r;
146 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
147 r = s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
148 if (r < 0) {
149 error_report("%s: unable to assign ioeventfd: %d", __func__, r);
150 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
151 event_notifier_cleanup(notifier);
152 return r;
154 } else {
155 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
156 s390_assign_subch_ioeventfd(notifier, sch_id, n, assign);
157 event_notifier_cleanup(notifier);
159 return r;
162 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev)
164 VirtIODevice *vdev;
165 int n, r;
167 if (!(dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) ||
168 dev->ioeventfd_disabled ||
169 dev->ioeventfd_started) {
170 return;
172 vdev = virtio_bus_get_device(&dev->bus);
173 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
174 if (!virtio_queue_get_num(vdev, n)) {
175 continue;
177 r = virtio_ccw_set_guest2host_notifier(dev, n, true, true);
178 if (r < 0) {
179 goto assign_error;
182 dev->ioeventfd_started = true;
183 return;
185 assign_error:
186 while (--n >= 0) {
187 if (!virtio_queue_get_num(vdev, n)) {
188 continue;
190 r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
191 assert(r >= 0);
193 dev->ioeventfd_started = false;
194 /* Disable ioeventfd for this device. */
195 dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD;
196 error_report("%s: failed. Fallback to userspace (slower).", __func__);
199 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev)
201 VirtIODevice *vdev;
202 int n, r;
204 if (!dev->ioeventfd_started) {
205 return;
207 vdev = virtio_bus_get_device(&dev->bus);
208 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
209 if (!virtio_queue_get_num(vdev, n)) {
210 continue;
212 r = virtio_ccw_set_guest2host_notifier(dev, n, false, false);
213 assert(r >= 0);
215 dev->ioeventfd_started = false;
218 VirtualCssBus *virtual_css_bus_init(void)
220 VirtualCssBus *cbus;
221 BusState *bus;
222 DeviceState *dev;
224 /* Create bridge device */
225 dev = qdev_create(NULL, "virtual-css-bridge");
226 qdev_init_nofail(dev);
228 /* Create bus on bridge device */
229 bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css");
230 cbus = VIRTUAL_CSS_BUS(bus);
232 /* Enable hotplugging */
233 qbus_set_hotplug_handler(bus, dev, &error_abort);
235 return cbus;
238 /* Communication blocks used by several channel commands. */
239 typedef struct VqInfoBlock {
240 uint64_t queue;
241 uint32_t align;
242 uint16_t index;
243 uint16_t num;
244 } QEMU_PACKED VqInfoBlock;
246 typedef struct VqConfigBlock {
247 uint16_t index;
248 uint16_t num_max;
249 } QEMU_PACKED VqConfigBlock;
251 typedef struct VirtioFeatDesc {
252 uint32_t features;
253 uint8_t index;
254 } QEMU_PACKED VirtioFeatDesc;
256 typedef struct VirtioThinintInfo {
257 hwaddr summary_indicator;
258 hwaddr device_indicator;
259 uint64_t ind_bit;
260 uint8_t isc;
261 } QEMU_PACKED VirtioThinintInfo;
263 /* Specify where the virtqueues for the subchannel are in guest memory. */
264 static int virtio_ccw_set_vqs(SubchDev *sch, uint64_t addr, uint32_t align,
265 uint16_t index, uint16_t num)
267 VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
269 if (index > VIRTIO_PCI_QUEUE_MAX) {
270 return -EINVAL;
273 /* Current code in virtio.c relies on 4K alignment. */
274 if (addr && (align != 4096)) {
275 return -EINVAL;
278 if (!vdev) {
279 return -EINVAL;
282 virtio_queue_set_addr(vdev, index, addr);
283 if (!addr) {
284 virtio_queue_set_vector(vdev, index, 0);
285 } else {
286 /* Fail if we don't have a big enough queue. */
287 /* TODO: Add interface to handle vring.num changing */
288 if (virtio_queue_get_num(vdev, index) > num) {
289 return -EINVAL;
291 virtio_queue_set_vector(vdev, index, index);
293 /* tell notify handler in case of config change */
294 vdev->config_vector = VIRTIO_PCI_QUEUE_MAX;
295 return 0;
298 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
300 int ret;
301 VqInfoBlock info;
302 uint8_t status;
303 VirtioFeatDesc features;
304 void *config;
305 hwaddr indicators;
306 VqConfigBlock vq_config;
307 VirtioCcwDevice *dev = sch->driver_data;
308 VirtIODevice *vdev = virtio_ccw_get_vdev(sch);
309 bool check_len;
310 int len;
311 hwaddr hw_len;
312 VirtioThinintInfo *thinint;
314 if (!dev) {
315 return -EINVAL;
318 trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid,
319 ccw.cmd_code);
320 check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
322 /* Look at the command. */
323 switch (ccw.cmd_code) {
324 case CCW_CMD_SET_VQ:
325 if (check_len) {
326 if (ccw.count != sizeof(info)) {
327 ret = -EINVAL;
328 break;
330 } else if (ccw.count < sizeof(info)) {
331 /* Can't execute command. */
332 ret = -EINVAL;
333 break;
335 if (!ccw.cda) {
336 ret = -EFAULT;
337 } else {
338 info.queue = ldq_phys(&address_space_memory, ccw.cda);
339 info.align = ldl_phys(&address_space_memory,
340 ccw.cda + sizeof(info.queue));
341 info.index = lduw_phys(&address_space_memory,
342 ccw.cda + sizeof(info.queue)
343 + sizeof(info.align));
344 info.num = lduw_phys(&address_space_memory,
345 ccw.cda + sizeof(info.queue)
346 + sizeof(info.align)
347 + sizeof(info.index));
348 ret = virtio_ccw_set_vqs(sch, info.queue, info.align, info.index,
349 info.num);
350 sch->curr_status.scsw.count = 0;
352 break;
353 case CCW_CMD_VDEV_RESET:
354 virtio_ccw_stop_ioeventfd(dev);
355 virtio_reset(vdev);
356 ret = 0;
357 break;
358 case CCW_CMD_READ_FEAT:
359 if (check_len) {
360 if (ccw.count != sizeof(features)) {
361 ret = -EINVAL;
362 break;
364 } else if (ccw.count < sizeof(features)) {
365 /* Can't execute command. */
366 ret = -EINVAL;
367 break;
369 if (!ccw.cda) {
370 ret = -EFAULT;
371 } else {
372 features.index = ldub_phys(&address_space_memory,
373 ccw.cda + sizeof(features.features));
374 if (features.index < ARRAY_SIZE(dev->host_features)) {
375 features.features = dev->host_features[features.index];
376 } else {
377 /* Return zeroes if the guest supports more feature bits. */
378 features.features = 0;
380 stl_le_phys(&address_space_memory, ccw.cda, features.features);
381 sch->curr_status.scsw.count = ccw.count - sizeof(features);
382 ret = 0;
384 break;
385 case CCW_CMD_WRITE_FEAT:
386 if (check_len) {
387 if (ccw.count != sizeof(features)) {
388 ret = -EINVAL;
389 break;
391 } else if (ccw.count < sizeof(features)) {
392 /* Can't execute command. */
393 ret = -EINVAL;
394 break;
396 if (!ccw.cda) {
397 ret = -EFAULT;
398 } else {
399 features.index = ldub_phys(&address_space_memory,
400 ccw.cda + sizeof(features.features));
401 features.features = ldl_le_phys(&address_space_memory, ccw.cda);
402 if (features.index < ARRAY_SIZE(dev->host_features)) {
403 virtio_set_features(vdev, features.features);
404 } else {
406 * If the guest supports more feature bits, assert that it
407 * passes us zeroes for those we don't support.
409 if (features.features) {
410 fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n",
411 features.index, features.features);
412 /* XXX: do a unit check here? */
415 sch->curr_status.scsw.count = ccw.count - sizeof(features);
416 ret = 0;
418 break;
419 case CCW_CMD_READ_CONF:
420 if (check_len) {
421 if (ccw.count > vdev->config_len) {
422 ret = -EINVAL;
423 break;
426 len = MIN(ccw.count, vdev->config_len);
427 if (!ccw.cda) {
428 ret = -EFAULT;
429 } else {
430 virtio_bus_get_vdev_config(&dev->bus, vdev->config);
431 /* XXX config space endianness */
432 cpu_physical_memory_write(ccw.cda, vdev->config, len);
433 sch->curr_status.scsw.count = ccw.count - len;
434 ret = 0;
436 break;
437 case CCW_CMD_WRITE_CONF:
438 if (check_len) {
439 if (ccw.count > vdev->config_len) {
440 ret = -EINVAL;
441 break;
444 len = MIN(ccw.count, vdev->config_len);
445 hw_len = len;
446 if (!ccw.cda) {
447 ret = -EFAULT;
448 } else {
449 config = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
450 if (!config) {
451 ret = -EFAULT;
452 } else {
453 len = hw_len;
454 /* XXX config space endianness */
455 memcpy(vdev->config, config, len);
456 cpu_physical_memory_unmap(config, hw_len, 0, hw_len);
457 virtio_bus_set_vdev_config(&dev->bus, vdev->config);
458 sch->curr_status.scsw.count = ccw.count - len;
459 ret = 0;
462 break;
463 case CCW_CMD_WRITE_STATUS:
464 if (check_len) {
465 if (ccw.count != sizeof(status)) {
466 ret = -EINVAL;
467 break;
469 } else if (ccw.count < sizeof(status)) {
470 /* Can't execute command. */
471 ret = -EINVAL;
472 break;
474 if (!ccw.cda) {
475 ret = -EFAULT;
476 } else {
477 status = ldub_phys(&address_space_memory, ccw.cda);
478 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
479 virtio_ccw_stop_ioeventfd(dev);
481 virtio_set_status(vdev, status);
482 if (vdev->status == 0) {
483 virtio_reset(vdev);
485 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
486 virtio_ccw_start_ioeventfd(dev);
488 sch->curr_status.scsw.count = ccw.count - sizeof(status);
489 ret = 0;
491 break;
492 case CCW_CMD_SET_IND:
493 if (check_len) {
494 if (ccw.count != sizeof(indicators)) {
495 ret = -EINVAL;
496 break;
498 } else if (ccw.count < sizeof(indicators)) {
499 /* Can't execute command. */
500 ret = -EINVAL;
501 break;
503 if (sch->thinint_active) {
504 /* Trigger a command reject. */
505 ret = -ENOSYS;
506 break;
508 if (!ccw.cda) {
509 ret = -EFAULT;
510 } else {
511 indicators = ldq_phys(&address_space_memory, ccw.cda);
512 dev->indicators = get_indicator(indicators, sizeof(uint64_t));
513 sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
514 ret = 0;
516 break;
517 case CCW_CMD_SET_CONF_IND:
518 if (check_len) {
519 if (ccw.count != sizeof(indicators)) {
520 ret = -EINVAL;
521 break;
523 } else if (ccw.count < sizeof(indicators)) {
524 /* Can't execute command. */
525 ret = -EINVAL;
526 break;
528 if (!ccw.cda) {
529 ret = -EFAULT;
530 } else {
531 indicators = ldq_phys(&address_space_memory, ccw.cda);
532 dev->indicators2 = get_indicator(indicators, sizeof(uint64_t));
533 sch->curr_status.scsw.count = ccw.count - sizeof(indicators);
534 ret = 0;
536 break;
537 case CCW_CMD_READ_VQ_CONF:
538 if (check_len) {
539 if (ccw.count != sizeof(vq_config)) {
540 ret = -EINVAL;
541 break;
543 } else if (ccw.count < sizeof(vq_config)) {
544 /* Can't execute command. */
545 ret = -EINVAL;
546 break;
548 if (!ccw.cda) {
549 ret = -EFAULT;
550 } else {
551 vq_config.index = lduw_phys(&address_space_memory, ccw.cda);
552 vq_config.num_max = virtio_queue_get_num(vdev,
553 vq_config.index);
554 stw_phys(&address_space_memory,
555 ccw.cda + sizeof(vq_config.index), vq_config.num_max);
556 sch->curr_status.scsw.count = ccw.count - sizeof(vq_config);
557 ret = 0;
559 break;
560 case CCW_CMD_SET_IND_ADAPTER:
561 if (check_len) {
562 if (ccw.count != sizeof(*thinint)) {
563 ret = -EINVAL;
564 break;
566 } else if (ccw.count < sizeof(*thinint)) {
567 /* Can't execute command. */
568 ret = -EINVAL;
569 break;
571 len = sizeof(*thinint);
572 hw_len = len;
573 if (!ccw.cda) {
574 ret = -EFAULT;
575 } else if (dev->indicators && !sch->thinint_active) {
576 /* Trigger a command reject. */
577 ret = -ENOSYS;
578 } else {
579 thinint = cpu_physical_memory_map(ccw.cda, &hw_len, 0);
580 if (!thinint) {
581 ret = -EFAULT;
582 } else {
583 len = hw_len;
584 dev->summary_indicator =
585 get_indicator(thinint->summary_indicator, sizeof(uint8_t));
586 dev->indicators = get_indicator(thinint->device_indicator,
587 thinint->ind_bit / 8 + 1);
588 dev->thinint_isc = thinint->isc;
589 dev->routes.adapter.ind_offset = thinint->ind_bit;
590 dev->routes.adapter.summary_offset = 7;
591 cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len);
592 ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
593 dev->thinint_isc, true, false,
594 &dev->routes.adapter.adapter_id);
595 assert(ret == 0);
596 sch->thinint_active = ((dev->indicators != NULL) &&
597 (dev->summary_indicator != NULL));
598 sch->curr_status.scsw.count = ccw.count - len;
599 ret = 0;
602 break;
603 default:
604 ret = -ENOSYS;
605 break;
607 return ret;
610 static int virtio_ccw_device_init(VirtioCcwDevice *dev, VirtIODevice *vdev)
612 unsigned int cssid = 0;
613 unsigned int ssid = 0;
614 unsigned int schid;
615 unsigned int devno;
616 bool have_devno = false;
617 bool found = false;
618 SubchDev *sch;
619 int ret;
620 int num;
621 DeviceState *parent = DEVICE(dev);
623 sch = g_malloc0(sizeof(SubchDev));
625 sch->driver_data = dev;
626 dev->sch = sch;
628 dev->indicators = NULL;
630 /* Initialize subchannel structure. */
631 sch->channel_prog = 0x0;
632 sch->last_cmd_valid = false;
633 sch->thinint_active = false;
635 * Use a device number if provided. Otherwise, fall back to subchannel
636 * number.
638 if (dev->bus_id) {
639 num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno);
640 if (num == 3) {
641 if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) {
642 ret = -EINVAL;
643 error_report("Invalid cssid or ssid: cssid %x, ssid %x",
644 cssid, ssid);
645 goto out_err;
647 /* Enforce use of virtual cssid. */
648 if (cssid != VIRTUAL_CSSID) {
649 ret = -EINVAL;
650 error_report("cssid %x not valid for virtio devices", cssid);
651 goto out_err;
653 if (css_devno_used(cssid, ssid, devno)) {
654 ret = -EEXIST;
655 error_report("Device %x.%x.%04x already exists", cssid, ssid,
656 devno);
657 goto out_err;
659 sch->cssid = cssid;
660 sch->ssid = ssid;
661 sch->devno = devno;
662 have_devno = true;
663 } else {
664 ret = -EINVAL;
665 error_report("Malformed devno parameter '%s'", dev->bus_id);
666 goto out_err;
670 /* Find the next free id. */
671 if (have_devno) {
672 for (schid = 0; schid <= MAX_SCHID; schid++) {
673 if (!css_find_subch(1, cssid, ssid, schid)) {
674 sch->schid = schid;
675 css_subch_assign(cssid, ssid, schid, devno, sch);
676 found = true;
677 break;
680 if (!found) {
681 ret = -ENODEV;
682 error_report("No free subchannel found for %x.%x.%04x", cssid, ssid,
683 devno);
684 goto out_err;
686 trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
687 "user-configured");
688 } else {
689 cssid = VIRTUAL_CSSID;
690 for (ssid = 0; ssid <= MAX_SSID; ssid++) {
691 for (schid = 0; schid <= MAX_SCHID; schid++) {
692 if (!css_find_subch(1, cssid, ssid, schid)) {
693 sch->cssid = cssid;
694 sch->ssid = ssid;
695 sch->schid = schid;
696 devno = schid;
698 * If the devno is already taken, look further in this
699 * subchannel set.
701 while (css_devno_used(cssid, ssid, devno)) {
702 if (devno == MAX_SCHID) {
703 devno = 0;
704 } else if (devno == schid - 1) {
705 ret = -ENODEV;
706 error_report("No free devno found");
707 goto out_err;
708 } else {
709 devno++;
712 sch->devno = devno;
713 css_subch_assign(cssid, ssid, schid, devno, sch);
714 found = true;
715 break;
718 if (found) {
719 break;
722 if (!found) {
723 ret = -ENODEV;
724 error_report("Virtual channel subsystem is full!");
725 goto out_err;
727 trace_virtio_ccw_new_device(cssid, ssid, schid, devno,
728 "auto-configured");
731 /* Build initial schib. */
732 css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE);
734 sch->ccw_cb = virtio_ccw_cb;
736 /* Build senseid data. */
737 memset(&sch->id, 0, sizeof(SenseId));
738 sch->id.reserved = 0xff;
739 sch->id.cu_type = VIRTIO_CCW_CU_TYPE;
740 sch->id.cu_model = vdev->device_id;
742 /* Only the first 32 feature bits are used. */
743 dev->host_features[0] = virtio_bus_get_vdev_features(&dev->bus,
744 dev->host_features[0]);
746 virtio_add_feature(&dev->host_features[0], VIRTIO_F_NOTIFY_ON_EMPTY);
747 virtio_add_feature(&dev->host_features[0], VIRTIO_F_BAD_FEATURE);
749 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
750 parent->hotplugged, 1);
751 return 0;
753 out_err:
754 dev->sch = NULL;
755 g_free(sch);
756 return ret;
759 static int virtio_ccw_exit(VirtioCcwDevice *dev)
761 SubchDev *sch = dev->sch;
763 if (sch) {
764 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
765 g_free(sch);
767 if (dev->indicators) {
768 release_indicator(&dev->routes.adapter, dev->indicators);
769 dev->indicators = NULL;
771 return 0;
774 static int virtio_ccw_net_init(VirtioCcwDevice *ccw_dev)
776 DeviceState *qdev = DEVICE(ccw_dev);
777 VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev);
778 DeviceState *vdev = DEVICE(&dev->vdev);
780 virtio_net_set_config_size(&dev->vdev, ccw_dev->host_features[0]);
781 virtio_net_set_netclient_name(&dev->vdev, qdev->id,
782 object_get_typename(OBJECT(qdev)));
783 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
784 if (qdev_init(vdev) < 0) {
785 return -1;
788 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
791 static void virtio_ccw_net_instance_init(Object *obj)
793 VirtIONetCcw *dev = VIRTIO_NET_CCW(obj);
795 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
796 TYPE_VIRTIO_NET);
797 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
798 "bootindex", &error_abort);
801 static int virtio_ccw_blk_init(VirtioCcwDevice *ccw_dev)
803 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev);
804 DeviceState *vdev = DEVICE(&dev->vdev);
805 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
806 if (qdev_init(vdev) < 0) {
807 return -1;
810 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
813 static void virtio_ccw_blk_instance_init(Object *obj)
815 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj);
817 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
818 TYPE_VIRTIO_BLK);
819 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
820 &error_abort);
821 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
822 "bootindex", &error_abort);
825 static int virtio_ccw_serial_init(VirtioCcwDevice *ccw_dev)
827 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev);
828 DeviceState *vdev = DEVICE(&dev->vdev);
829 DeviceState *proxy = DEVICE(ccw_dev);
830 char *bus_name;
833 * For command line compatibility, this sets the virtio-serial-device bus
834 * name as before.
836 if (proxy->id) {
837 bus_name = g_strdup_printf("%s.0", proxy->id);
838 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
839 g_free(bus_name);
842 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
843 if (qdev_init(vdev) < 0) {
844 return -1;
847 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
851 static void virtio_ccw_serial_instance_init(Object *obj)
853 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj);
855 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
856 TYPE_VIRTIO_SERIAL);
859 static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev)
861 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
862 DeviceState *vdev = DEVICE(&dev->vdev);
864 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
865 if (qdev_init(vdev) < 0) {
866 return -1;
869 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
872 static void balloon_ccw_stats_get_all(Object *obj, struct Visitor *v,
873 void *opaque, const char *name,
874 Error **errp)
876 VirtIOBalloonCcw *dev = opaque;
877 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
880 static void balloon_ccw_stats_get_poll_interval(Object *obj, struct Visitor *v,
881 void *opaque, const char *name,
882 Error **errp)
884 VirtIOBalloonCcw *dev = opaque;
885 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
886 errp);
889 static void balloon_ccw_stats_set_poll_interval(Object *obj, struct Visitor *v,
890 void *opaque, const char *name,
891 Error **errp)
893 VirtIOBalloonCcw *dev = opaque;
894 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
895 errp);
898 static void virtio_ccw_balloon_instance_init(Object *obj)
900 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
901 object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
902 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
903 object_unref(OBJECT(&dev->vdev));
904 object_property_add(obj, "guest-stats", "guest statistics",
905 balloon_ccw_stats_get_all, NULL, NULL, dev, NULL);
907 object_property_add(obj, "guest-stats-polling-interval", "int",
908 balloon_ccw_stats_get_poll_interval,
909 balloon_ccw_stats_set_poll_interval,
910 NULL, dev, NULL);
913 static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
915 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev);
916 DeviceState *vdev = DEVICE(&dev->vdev);
917 DeviceState *qdev = DEVICE(ccw_dev);
918 char *bus_name;
921 * For command line compatibility, this sets the virtio-scsi-device bus
922 * name as before.
924 if (qdev->id) {
925 bus_name = g_strdup_printf("%s.0", qdev->id);
926 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
927 g_free(bus_name);
930 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
931 if (qdev_init(vdev) < 0) {
932 return -1;
935 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
938 static void virtio_ccw_scsi_instance_init(Object *obj)
940 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj);
942 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
943 TYPE_VIRTIO_SCSI);
944 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
945 &error_abort);
948 #ifdef CONFIG_VHOST_SCSI
949 static int vhost_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
951 VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev);
952 DeviceState *vdev = DEVICE(&dev->vdev);
954 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
955 if (qdev_init(vdev) < 0) {
956 return -1;
959 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
962 static void vhost_ccw_scsi_instance_init(Object *obj)
964 VHostSCSICcw *dev = VHOST_SCSI_CCW(obj);
966 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
967 TYPE_VHOST_SCSI);
969 #endif
971 static int virtio_ccw_rng_init(VirtioCcwDevice *ccw_dev)
973 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev);
974 DeviceState *vdev = DEVICE(&dev->vdev);
976 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
977 if (qdev_init(vdev) < 0) {
978 return -1;
981 object_property_set_link(OBJECT(dev),
982 OBJECT(dev->vdev.conf.rng), "rng",
983 NULL);
985 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
988 /* DeviceState to VirtioCcwDevice. Note: used on datapath,
989 * be careful and test performance if you change this.
991 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d)
993 return container_of(d, VirtioCcwDevice, parent_obj);
996 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc,
997 uint8_t to_be_set)
999 uint8_t ind_old, ind_new;
1000 hwaddr len = 1;
1001 uint8_t *ind_addr;
1003 ind_addr = cpu_physical_memory_map(ind_loc, &len, 1);
1004 if (!ind_addr) {
1005 error_report("%s(%x.%x.%04x): unable to access indicator",
1006 __func__, sch->cssid, sch->ssid, sch->schid);
1007 return -1;
1009 do {
1010 ind_old = *ind_addr;
1011 ind_new = ind_old | to_be_set;
1012 } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old);
1013 cpu_physical_memory_unmap(ind_addr, len, 1, len);
1015 return ind_old;
1018 static void virtio_ccw_notify(DeviceState *d, uint16_t vector)
1020 VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d);
1021 SubchDev *sch = dev->sch;
1022 uint64_t indicators;
1024 if (vector >= 128) {
1025 return;
1028 if (vector < VIRTIO_PCI_QUEUE_MAX) {
1029 if (!dev->indicators) {
1030 return;
1032 if (sch->thinint_active) {
1034 * In the adapter interrupt case, indicators points to a
1035 * memory area that may be (way) larger than 64 bit and
1036 * ind_bit indicates the start of the indicators in a big
1037 * endian notation.
1039 uint64_t ind_bit = dev->routes.adapter.ind_offset;
1041 virtio_set_ind_atomic(sch, dev->indicators->addr +
1042 (ind_bit + vector) / 8,
1043 0x80 >> ((ind_bit + vector) % 8));
1044 if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr,
1045 0x01)) {
1046 css_adapter_interrupt(dev->thinint_isc);
1048 } else {
1049 indicators = ldq_phys(&address_space_memory, dev->indicators->addr);
1050 indicators |= 1ULL << vector;
1051 stq_phys(&address_space_memory, dev->indicators->addr, indicators);
1052 css_conditional_io_interrupt(sch);
1054 } else {
1055 if (!dev->indicators2) {
1056 return;
1058 vector = 0;
1059 indicators = ldq_phys(&address_space_memory, dev->indicators2->addr);
1060 indicators |= 1ULL << vector;
1061 stq_phys(&address_space_memory, dev->indicators2->addr, indicators);
1062 css_conditional_io_interrupt(sch);
1066 static unsigned virtio_ccw_get_features(DeviceState *d)
1068 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1070 /* Only the first 32 feature bits are used. */
1071 return dev->host_features[0];
1074 static void virtio_ccw_reset(DeviceState *d)
1076 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1077 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1079 virtio_ccw_stop_ioeventfd(dev);
1080 virtio_reset(vdev);
1081 css_reset_sch(dev->sch);
1082 if (dev->indicators) {
1083 release_indicator(&dev->routes.adapter, dev->indicators);
1084 dev->indicators = NULL;
1086 if (dev->indicators2) {
1087 release_indicator(&dev->routes.adapter, dev->indicators2);
1088 dev->indicators2 = NULL;
1090 if (dev->summary_indicator) {
1091 release_indicator(&dev->routes.adapter, dev->summary_indicator);
1092 dev->summary_indicator = NULL;
1096 static void virtio_ccw_vmstate_change(DeviceState *d, bool running)
1098 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1100 if (running) {
1101 virtio_ccw_start_ioeventfd(dev);
1102 } else {
1103 virtio_ccw_stop_ioeventfd(dev);
1107 static bool virtio_ccw_query_guest_notifiers(DeviceState *d)
1109 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1111 return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA);
1114 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign)
1116 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1118 /* Stop using the generic ioeventfd, we are doing eventfd handling
1119 * ourselves below */
1120 dev->ioeventfd_disabled = assign;
1121 if (assign) {
1122 virtio_ccw_stop_ioeventfd(dev);
1124 return virtio_ccw_set_guest2host_notifier(dev, n, assign, false);
1127 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev)
1129 int r;
1131 if (!dev->sch->thinint_active) {
1132 return -EINVAL;
1135 r = map_indicator(&dev->routes.adapter, dev->summary_indicator);
1136 if (r) {
1137 return r;
1139 r = map_indicator(&dev->routes.adapter, dev->indicators);
1140 if (r) {
1141 return r;
1143 dev->routes.adapter.summary_addr = dev->summary_indicator->map;
1144 dev->routes.adapter.ind_addr = dev->indicators->map;
1146 return 0;
1149 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs)
1151 int i;
1152 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1153 int ret;
1154 S390FLICState *fs = s390_get_flic();
1155 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1157 ret = virtio_ccw_get_mappings(dev);
1158 if (ret) {
1159 return ret;
1161 for (i = 0; i < nvqs; i++) {
1162 if (!virtio_queue_get_num(vdev, i)) {
1163 break;
1166 dev->routes.num_routes = i;
1167 return fsc->add_adapter_routes(fs, &dev->routes);
1170 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs)
1172 S390FLICState *fs = s390_get_flic();
1173 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs);
1175 fsc->release_adapter_routes(fs, &dev->routes);
1178 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n)
1180 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1181 VirtQueue *vq = virtio_get_queue(vdev, n);
1182 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1184 return kvm_irqchip_add_irqfd_notifier(kvm_state, notifier, NULL,
1185 dev->routes.gsi[n]);
1188 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n)
1190 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1191 VirtQueue *vq = virtio_get_queue(vdev, n);
1192 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1193 int ret;
1195 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, notifier,
1196 dev->routes.gsi[n]);
1197 assert(ret == 0);
1200 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1201 bool assign, bool with_irqfd)
1203 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1204 VirtQueue *vq = virtio_get_queue(vdev, n);
1205 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1206 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1208 if (assign) {
1209 int r = event_notifier_init(notifier, 0);
1211 if (r < 0) {
1212 return r;
1214 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1215 if (with_irqfd) {
1216 r = virtio_ccw_add_irqfd(dev, n);
1217 if (r) {
1218 virtio_queue_set_guest_notifier_fd_handler(vq, false,
1219 with_irqfd);
1220 return r;
1224 * We do not support individual masking for channel devices, so we
1225 * need to manually trigger any guest masking callbacks here.
1227 if (k->guest_notifier_mask) {
1228 k->guest_notifier_mask(vdev, n, false);
1230 /* get lost events and re-inject */
1231 if (k->guest_notifier_pending &&
1232 k->guest_notifier_pending(vdev, n)) {
1233 event_notifier_set(notifier);
1235 } else {
1236 if (k->guest_notifier_mask) {
1237 k->guest_notifier_mask(vdev, n, true);
1239 if (with_irqfd) {
1240 virtio_ccw_remove_irqfd(dev, n);
1242 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1243 event_notifier_cleanup(notifier);
1245 return 0;
1248 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs,
1249 bool assigned)
1251 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1252 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1253 bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled();
1254 int r, n;
1256 if (with_irqfd && assigned) {
1257 /* irq routes need to be set up before assigning irqfds */
1258 r = virtio_ccw_setup_irqroutes(dev, nvqs);
1259 if (r < 0) {
1260 goto irqroute_error;
1263 for (n = 0; n < nvqs; n++) {
1264 if (!virtio_queue_get_num(vdev, n)) {
1265 break;
1267 r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd);
1268 if (r < 0) {
1269 goto assign_error;
1272 if (with_irqfd && !assigned) {
1273 /* release irq routes after irqfds have been released */
1274 virtio_ccw_release_irqroutes(dev, nvqs);
1276 return 0;
1278 assign_error:
1279 while (--n >= 0) {
1280 virtio_ccw_set_guest_notifier(dev, n, !assigned, false);
1282 irqroute_error:
1283 if (with_irqfd && assigned) {
1284 virtio_ccw_release_irqroutes(dev, nvqs);
1286 return r;
1289 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f)
1291 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1292 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1294 qemu_put_be16(f, virtio_queue_vector(vdev, n));
1297 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f)
1299 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1300 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus);
1301 uint16_t vector;
1303 qemu_get_be16s(f, &vector);
1304 virtio_queue_set_vector(vdev, n , vector);
1306 return 0;
1309 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f)
1311 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1312 SubchDev *s = dev->sch;
1314 subch_device_save(s, f);
1315 if (dev->indicators != NULL) {
1316 qemu_put_be32(f, dev->indicators->len);
1317 qemu_put_be64(f, dev->indicators->addr);
1318 } else {
1319 qemu_put_be32(f, 0);
1320 qemu_put_be64(f, 0UL);
1322 if (dev->indicators2 != NULL) {
1323 qemu_put_be32(f, dev->indicators2->len);
1324 qemu_put_be64(f, dev->indicators2->addr);
1325 } else {
1326 qemu_put_be32(f, 0);
1327 qemu_put_be64(f, 0UL);
1329 if (dev->summary_indicator != NULL) {
1330 qemu_put_be32(f, dev->summary_indicator->len);
1331 qemu_put_be64(f, dev->summary_indicator->addr);
1332 } else {
1333 qemu_put_be32(f, 0);
1334 qemu_put_be64(f, 0UL);
1336 qemu_put_be64(f, dev->routes.adapter.ind_offset);
1337 qemu_put_byte(f, dev->thinint_isc);
1340 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)
1342 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);
1343 SubchDev *s = dev->sch;
1344 int len;
1346 s->driver_data = dev;
1347 subch_device_load(s, f);
1348 len = qemu_get_be32(f);
1349 if (len != 0) {
1350 dev->indicators = get_indicator(qemu_get_be64(f), len);
1351 } else {
1352 qemu_get_be64(f);
1353 dev->indicators = NULL;
1355 len = qemu_get_be32(f);
1356 if (len != 0) {
1357 dev->indicators2 = get_indicator(qemu_get_be64(f), len);
1358 } else {
1359 qemu_get_be64(f);
1360 dev->indicators2 = NULL;
1362 len = qemu_get_be32(f);
1363 if (len != 0) {
1364 dev->summary_indicator = get_indicator(qemu_get_be64(f), len);
1365 } else {
1366 qemu_get_be64(f);
1367 dev->summary_indicator = NULL;
1369 dev->routes.adapter.ind_offset = qemu_get_be64(f);
1370 dev->thinint_isc = qemu_get_byte(f);
1371 if (s->thinint_active) {
1372 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,
1373 dev->thinint_isc, true, false,
1374 &dev->routes.adapter.adapter_id);
1377 return 0;
1380 /**************** Virtio-ccw Bus Device Descriptions *******************/
1382 static Property virtio_ccw_net_properties[] = {
1383 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1384 DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]),
1385 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1386 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1387 DEFINE_PROP_END_OF_LIST(),
1390 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data)
1392 DeviceClass *dc = DEVICE_CLASS(klass);
1393 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1395 k->init = virtio_ccw_net_init;
1396 k->exit = virtio_ccw_exit;
1397 dc->reset = virtio_ccw_reset;
1398 dc->props = virtio_ccw_net_properties;
1401 static const TypeInfo virtio_ccw_net = {
1402 .name = TYPE_VIRTIO_NET_CCW,
1403 .parent = TYPE_VIRTIO_CCW_DEVICE,
1404 .instance_size = sizeof(VirtIONetCcw),
1405 .instance_init = virtio_ccw_net_instance_init,
1406 .class_init = virtio_ccw_net_class_init,
1409 static Property virtio_ccw_blk_properties[] = {
1410 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1411 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1412 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1413 DEFINE_PROP_END_OF_LIST(),
1416 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data)
1418 DeviceClass *dc = DEVICE_CLASS(klass);
1419 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1421 k->init = virtio_ccw_blk_init;
1422 k->exit = virtio_ccw_exit;
1423 dc->reset = virtio_ccw_reset;
1424 dc->props = virtio_ccw_blk_properties;
1427 static const TypeInfo virtio_ccw_blk = {
1428 .name = TYPE_VIRTIO_BLK_CCW,
1429 .parent = TYPE_VIRTIO_CCW_DEVICE,
1430 .instance_size = sizeof(VirtIOBlkCcw),
1431 .instance_init = virtio_ccw_blk_instance_init,
1432 .class_init = virtio_ccw_blk_class_init,
1435 static Property virtio_ccw_serial_properties[] = {
1436 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1437 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1438 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1439 DEFINE_PROP_END_OF_LIST(),
1442 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data)
1444 DeviceClass *dc = DEVICE_CLASS(klass);
1445 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1447 k->init = virtio_ccw_serial_init;
1448 k->exit = virtio_ccw_exit;
1449 dc->reset = virtio_ccw_reset;
1450 dc->props = virtio_ccw_serial_properties;
1453 static const TypeInfo virtio_ccw_serial = {
1454 .name = TYPE_VIRTIO_SERIAL_CCW,
1455 .parent = TYPE_VIRTIO_CCW_DEVICE,
1456 .instance_size = sizeof(VirtioSerialCcw),
1457 .instance_init = virtio_ccw_serial_instance_init,
1458 .class_init = virtio_ccw_serial_class_init,
1461 static Property virtio_ccw_balloon_properties[] = {
1462 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1463 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1464 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1465 DEFINE_PROP_END_OF_LIST(),
1468 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
1470 DeviceClass *dc = DEVICE_CLASS(klass);
1471 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1473 k->init = virtio_ccw_balloon_init;
1474 k->exit = virtio_ccw_exit;
1475 dc->reset = virtio_ccw_reset;
1476 dc->props = virtio_ccw_balloon_properties;
1479 static const TypeInfo virtio_ccw_balloon = {
1480 .name = TYPE_VIRTIO_BALLOON_CCW,
1481 .parent = TYPE_VIRTIO_CCW_DEVICE,
1482 .instance_size = sizeof(VirtIOBalloonCcw),
1483 .instance_init = virtio_ccw_balloon_instance_init,
1484 .class_init = virtio_ccw_balloon_class_init,
1487 static Property virtio_ccw_scsi_properties[] = {
1488 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1489 DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]),
1490 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1491 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1492 DEFINE_PROP_END_OF_LIST(),
1495 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data)
1497 DeviceClass *dc = DEVICE_CLASS(klass);
1498 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1500 k->init = virtio_ccw_scsi_init;
1501 k->exit = virtio_ccw_exit;
1502 dc->reset = virtio_ccw_reset;
1503 dc->props = virtio_ccw_scsi_properties;
1506 static const TypeInfo virtio_ccw_scsi = {
1507 .name = TYPE_VIRTIO_SCSI_CCW,
1508 .parent = TYPE_VIRTIO_CCW_DEVICE,
1509 .instance_size = sizeof(VirtIOSCSICcw),
1510 .instance_init = virtio_ccw_scsi_instance_init,
1511 .class_init = virtio_ccw_scsi_class_init,
1514 #ifdef CONFIG_VHOST_SCSI
1515 static Property vhost_ccw_scsi_properties[] = {
1516 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1517 DEFINE_PROP_END_OF_LIST(),
1520 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data)
1522 DeviceClass *dc = DEVICE_CLASS(klass);
1523 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1525 k->init = vhost_ccw_scsi_init;
1526 k->exit = virtio_ccw_exit;
1527 dc->reset = virtio_ccw_reset;
1528 dc->props = vhost_ccw_scsi_properties;
1531 static const TypeInfo vhost_ccw_scsi = {
1532 .name = TYPE_VHOST_SCSI_CCW,
1533 .parent = TYPE_VIRTIO_CCW_DEVICE,
1534 .instance_size = sizeof(VHostSCSICcw),
1535 .instance_init = vhost_ccw_scsi_instance_init,
1536 .class_init = vhost_ccw_scsi_class_init,
1538 #endif
1540 static void virtio_ccw_rng_instance_init(Object *obj)
1542 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj);
1544 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
1545 TYPE_VIRTIO_RNG);
1546 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev),
1547 "rng", &error_abort);
1550 static Property virtio_ccw_rng_properties[] = {
1551 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id),
1552 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags,
1553 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true),
1554 DEFINE_PROP_END_OF_LIST(),
1557 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data)
1559 DeviceClass *dc = DEVICE_CLASS(klass);
1560 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
1562 k->init = virtio_ccw_rng_init;
1563 k->exit = virtio_ccw_exit;
1564 dc->reset = virtio_ccw_reset;
1565 dc->props = virtio_ccw_rng_properties;
1568 static const TypeInfo virtio_ccw_rng = {
1569 .name = TYPE_VIRTIO_RNG_CCW,
1570 .parent = TYPE_VIRTIO_CCW_DEVICE,
1571 .instance_size = sizeof(VirtIORNGCcw),
1572 .instance_init = virtio_ccw_rng_instance_init,
1573 .class_init = virtio_ccw_rng_class_init,
1576 static int virtio_ccw_busdev_init(DeviceState *dev)
1578 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1579 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1581 virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev);
1583 return _info->init(_dev);
1586 static int virtio_ccw_busdev_exit(DeviceState *dev)
1588 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1589 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev);
1591 return _info->exit(_dev);
1594 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev,
1595 DeviceState *dev, Error **errp)
1597 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev;
1598 SubchDev *sch = _dev->sch;
1600 virtio_ccw_stop_ioeventfd(_dev);
1603 * We should arrive here only for device_del, since we don't support
1604 * direct hot(un)plug of channels, but only through virtio.
1606 assert(sch != NULL);
1607 /* Subchannel is now disabled and no longer valid. */
1608 sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA |
1609 PMCW_FLAGS_MASK_DNV);
1611 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0);
1613 object_unparent(OBJECT(dev));
1616 static Property virtio_ccw_properties[] = {
1617 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]),
1618 DEFINE_PROP_END_OF_LIST(),
1621 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
1623 DeviceClass *dc = DEVICE_CLASS(klass);
1625 dc->props = virtio_ccw_properties;
1626 dc->init = virtio_ccw_busdev_init;
1627 dc->exit = virtio_ccw_busdev_exit;
1628 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
1631 static const TypeInfo virtio_ccw_device_info = {
1632 .name = TYPE_VIRTIO_CCW_DEVICE,
1633 .parent = TYPE_DEVICE,
1634 .instance_size = sizeof(VirtioCcwDevice),
1635 .class_init = virtio_ccw_device_class_init,
1636 .class_size = sizeof(VirtIOCCWDeviceClass),
1637 .abstract = true,
1640 /***************** Virtual-css Bus Bridge Device ********************/
1641 /* Only required to have the virtio bus as child in the system bus */
1643 static int virtual_css_bridge_init(SysBusDevice *dev)
1645 /* nothing */
1646 return 0;
1649 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data)
1651 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
1652 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1654 k->init = virtual_css_bridge_init;
1655 hc->unplug = virtio_ccw_busdev_unplug;
1658 static const TypeInfo virtual_css_bridge_info = {
1659 .name = "virtual-css-bridge",
1660 .parent = TYPE_SYS_BUS_DEVICE,
1661 .instance_size = sizeof(SysBusDevice),
1662 .class_init = virtual_css_bridge_class_init,
1663 .interfaces = (InterfaceInfo[]) {
1664 { TYPE_HOTPLUG_HANDLER },
1669 /* virtio-ccw-bus */
1671 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size,
1672 VirtioCcwDevice *dev)
1674 DeviceState *qdev = DEVICE(dev);
1675 char virtio_bus_name[] = "virtio-bus";
1677 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS,
1678 qdev, virtio_bus_name);
1681 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data)
1683 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1684 BusClass *bus_class = BUS_CLASS(klass);
1686 bus_class->max_dev = 1;
1687 k->notify = virtio_ccw_notify;
1688 k->get_features = virtio_ccw_get_features;
1689 k->vmstate_change = virtio_ccw_vmstate_change;
1690 k->query_guest_notifiers = virtio_ccw_query_guest_notifiers;
1691 k->set_host_notifier = virtio_ccw_set_host_notifier;
1692 k->set_guest_notifiers = virtio_ccw_set_guest_notifiers;
1693 k->save_queue = virtio_ccw_save_queue;
1694 k->load_queue = virtio_ccw_load_queue;
1695 k->save_config = virtio_ccw_save_config;
1696 k->load_config = virtio_ccw_load_config;
1699 static const TypeInfo virtio_ccw_bus_info = {
1700 .name = TYPE_VIRTIO_CCW_BUS,
1701 .parent = TYPE_VIRTIO_BUS,
1702 .instance_size = sizeof(VirtioCcwBusState),
1703 .class_init = virtio_ccw_bus_class_init,
1706 static void virtio_ccw_register(void)
1708 type_register_static(&virtio_ccw_bus_info);
1709 type_register_static(&virtual_css_bus_info);
1710 type_register_static(&virtio_ccw_device_info);
1711 type_register_static(&virtio_ccw_serial);
1712 type_register_static(&virtio_ccw_blk);
1713 type_register_static(&virtio_ccw_net);
1714 type_register_static(&virtio_ccw_balloon);
1715 type_register_static(&virtio_ccw_scsi);
1716 #ifdef CONFIG_VHOST_SCSI
1717 type_register_static(&vhost_ccw_scsi);
1718 #endif
1719 type_register_static(&virtio_ccw_rng);
1720 type_register_static(&virtual_css_bridge_info);
1723 type_init(virtio_ccw_register)