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