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