Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * virtio ccw target implementation |
| 3 | * |
Pierre Morel | de6a921 | 2015-05-27 13:11:59 +0200 | [diff] [blame] | 4 | * Copyright 2012,2015 IBM Corp. |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 5 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
Pierre Morel | de6a921 | 2015-05-27 13:11:59 +0200 | [diff] [blame] | 6 | * Pierre Morel <pmorel@linux.vnet.ibm.com> |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 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 | |
Peter Maydell | 9615495 | 2016-01-26 18:17:00 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 14 | #include "qapi/error.h" |
Paolo Bonzini | a43de79 | 2022-03-28 13:41:29 +0200 | [diff] [blame] | 15 | #include "exec/address-spaces.h" |
Paolo Bonzini | bd3f16a | 2015-12-04 12:06:26 +0100 | [diff] [blame] | 16 | #include "sysemu/kvm.h" |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 17 | #include "net/net.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 18 | #include "hw/virtio/virtio.h" |
Markus Armbruster | ca77ee2 | 2019-08-12 07:23:39 +0200 | [diff] [blame] | 19 | #include "migration/qemu-file-types.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 20 | #include "hw/virtio/virtio-net.h" |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 21 | #include "qemu/bitops.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 22 | #include "qemu/error-report.h" |
Paolo Bonzini | a43de79 | 2022-03-28 13:41:29 +0200 | [diff] [blame] | 23 | #include "qemu/log.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | #include "qemu/module.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 25 | #include "hw/virtio/virtio-bus.h" |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 26 | #include "hw/s390x/adapter.h" |
| 27 | #include "hw/s390x/s390_flic.h" |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 28 | |
Paolo Bonzini | bd3f16a | 2015-12-04 12:06:26 +0100 | [diff] [blame] | 29 | #include "hw/s390x/ioinst.h" |
| 30 | #include "hw/s390x/css.h" |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 31 | #include "virtio-ccw.h" |
| 32 | #include "trace.h" |
Jing Liu | dd70bd0 | 2015-11-06 12:32:40 +0100 | [diff] [blame] | 33 | #include "hw/s390x/css-bridge.h" |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 34 | #include "hw/s390x/s390-virtio-ccw.h" |
Pavel Dovgalyuk | 3909c07 | 2021-05-17 16:06:28 +0300 | [diff] [blame] | 35 | #include "sysemu/replay.h" |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 36 | |
Halil Pasic | 797b608 | 2015-12-10 12:55:06 +0100 | [diff] [blame] | 37 | #define NR_CLASSIC_INDICATOR_BITS 64 |
| 38 | |
Gerd Hoffmann | 2dd9d8c | 2021-03-17 10:56:21 +0100 | [diff] [blame] | 39 | bool have_virtio_ccw = true; |
| 40 | |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 41 | static int virtio_ccw_dev_post_load(void *opaque, int version_id) |
| 42 | { |
| 43 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(opaque); |
| 44 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 45 | CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); |
| 46 | |
| 47 | ccw_dev->sch->driver_data = dev; |
| 48 | if (ccw_dev->sch->thinint_active) { |
| 49 | dev->routes.adapter.adapter_id = css_get_adapter_id( |
| 50 | CSS_IO_ADAPTER_VIRTIO, |
| 51 | dev->thinint_isc); |
| 52 | } |
| 53 | /* Re-fill subch_id after loading the subchannel states.*/ |
| 54 | if (ck->refill_ids) { |
| 55 | ck->refill_ids(ccw_dev); |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | typedef struct VirtioCcwDeviceTmp { |
| 61 | VirtioCcwDevice *parent; |
| 62 | uint16_t config_vector; |
| 63 | } VirtioCcwDeviceTmp; |
| 64 | |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 65 | static int virtio_ccw_dev_tmp_pre_save(void *opaque) |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 66 | { |
| 67 | VirtioCcwDeviceTmp *tmp = opaque; |
| 68 | VirtioCcwDevice *dev = tmp->parent; |
| 69 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 70 | |
| 71 | tmp->config_vector = vdev->config_vector; |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 72 | |
| 73 | return 0; |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int virtio_ccw_dev_tmp_post_load(void *opaque, int version_id) |
| 77 | { |
| 78 | VirtioCcwDeviceTmp *tmp = opaque; |
| 79 | VirtioCcwDevice *dev = tmp->parent; |
| 80 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 81 | |
| 82 | vdev->config_vector = tmp->config_vector; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | const VMStateDescription vmstate_virtio_ccw_dev_tmp = { |
| 87 | .name = "s390_virtio_ccw_dev_tmp", |
| 88 | .pre_save = virtio_ccw_dev_tmp_pre_save, |
| 89 | .post_load = virtio_ccw_dev_tmp_post_load, |
Richard Henderson | b9b59a3 | 2023-12-21 14:16:31 +1100 | [diff] [blame] | 90 | .fields = (const VMStateField[]) { |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 91 | VMSTATE_UINT16(config_vector, VirtioCcwDeviceTmp), |
| 92 | VMSTATE_END_OF_LIST() |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | const VMStateDescription vmstate_virtio_ccw_dev = { |
| 97 | .name = "s390_virtio_ccw_dev", |
| 98 | .version_id = 1, |
| 99 | .minimum_version_id = 1, |
| 100 | .post_load = virtio_ccw_dev_post_load, |
Richard Henderson | b9b59a3 | 2023-12-21 14:16:31 +1100 | [diff] [blame] | 101 | .fields = (const VMStateField[]) { |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 102 | VMSTATE_CCW_DEVICE(parent_obj, VirtioCcwDevice), |
| 103 | VMSTATE_PTR_TO_IND_ADDR(indicators, VirtioCcwDevice), |
| 104 | VMSTATE_PTR_TO_IND_ADDR(indicators2, VirtioCcwDevice), |
| 105 | VMSTATE_PTR_TO_IND_ADDR(summary_indicator, VirtioCcwDevice), |
| 106 | /* |
| 107 | * Ugly hack because VirtIODevice does not migrate itself. |
| 108 | * This also makes legacy via vmstate_save_state possible. |
| 109 | */ |
| 110 | VMSTATE_WITH_TMP(VirtioCcwDevice, VirtioCcwDeviceTmp, |
| 111 | vmstate_virtio_ccw_dev_tmp), |
| 112 | VMSTATE_STRUCT(routes, VirtioCcwDevice, 1, vmstate_adapter_routes, |
| 113 | AdapterRoutes), |
| 114 | VMSTATE_UINT8(thinint_isc, VirtioCcwDevice), |
| 115 | VMSTATE_INT32(revision, VirtioCcwDevice), |
| 116 | VMSTATE_END_OF_LIST() |
| 117 | } |
| 118 | }; |
| 119 | |
Andreas Färber | 1bf4d7a | 2013-08-23 20:27:30 +0200 | [diff] [blame] | 120 | static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, |
| 121 | VirtioCcwDevice *dev); |
KONRAD Frederic | d51fcfa | 2013-04-24 10:21:18 +0200 | [diff] [blame] | 122 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 123 | VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch) |
| 124 | { |
| 125 | VirtIODevice *vdev = NULL; |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 126 | VirtioCcwDevice *dev = sch->driver_data; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 127 | |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 128 | if (dev) { |
| 129 | vdev = virtio_bus_get_device(&dev->bus); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 130 | } |
| 131 | return vdev; |
| 132 | } |
| 133 | |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 134 | static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev) |
| 135 | { |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 136 | virtio_bus_start_ioeventfd(&dev->bus); |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev) |
| 140 | { |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 141 | virtio_bus_stop_ioeventfd(&dev->bus); |
| 142 | } |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 143 | |
Paolo Bonzini | 8e93cef | 2016-10-21 22:48:08 +0200 | [diff] [blame] | 144 | static bool virtio_ccw_ioeventfd_enabled(DeviceState *d) |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 145 | { |
| 146 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 147 | |
Paolo Bonzini | 8e93cef | 2016-10-21 22:48:08 +0200 | [diff] [blame] | 148 | return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0; |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, |
| 152 | int n, bool assign) |
| 153 | { |
| 154 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 155 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 156 | SubchDev *sch = ccw_dev->sch; |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 157 | uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid; |
| 158 | |
| 159 | return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign); |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 162 | /* Communication blocks used by several channel commands. */ |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 163 | typedef struct VqInfoBlockLegacy { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 164 | uint64_t queue; |
| 165 | uint32_t align; |
| 166 | uint16_t index; |
| 167 | uint16_t num; |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 168 | } QEMU_PACKED VqInfoBlockLegacy; |
| 169 | |
| 170 | typedef struct VqInfoBlock { |
| 171 | uint64_t desc; |
| 172 | uint32_t res0; |
| 173 | uint16_t index; |
| 174 | uint16_t num; |
| 175 | uint64_t avail; |
| 176 | uint64_t used; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 177 | } QEMU_PACKED VqInfoBlock; |
| 178 | |
| 179 | typedef struct VqConfigBlock { |
| 180 | uint16_t index; |
| 181 | uint16_t num_max; |
| 182 | } QEMU_PACKED VqConfigBlock; |
| 183 | |
| 184 | typedef struct VirtioFeatDesc { |
| 185 | uint32_t features; |
| 186 | uint8_t index; |
| 187 | } QEMU_PACKED VirtioFeatDesc; |
| 188 | |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 189 | typedef struct VirtioThinintInfo { |
| 190 | hwaddr summary_indicator; |
| 191 | hwaddr device_indicator; |
| 192 | uint64_t ind_bit; |
| 193 | uint8_t isc; |
| 194 | } QEMU_PACKED VirtioThinintInfo; |
| 195 | |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 196 | typedef struct VirtioRevInfo { |
| 197 | uint16_t revision; |
| 198 | uint16_t length; |
Philippe Mathieu-Daudé | 880a781 | 2020-03-04 16:38:16 +0100 | [diff] [blame] | 199 | uint8_t data[]; |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 200 | } QEMU_PACKED VirtioRevInfo; |
| 201 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 202 | /* Specify where the virtqueues for the subchannel are in guest memory. */ |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 203 | static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info, |
| 204 | VqInfoBlockLegacy *linfo) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 205 | { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 206 | VirtIODevice *vdev = virtio_ccw_get_vdev(sch); |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 207 | uint16_t index = info ? info->index : linfo->index; |
| 208 | uint16_t num = info ? info->num : linfo->num; |
| 209 | uint64_t desc = info ? info->desc : linfo->queue; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 210 | |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 211 | if (index >= VIRTIO_QUEUE_MAX) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | /* Current code in virtio.c relies on 4K alignment. */ |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 216 | if (linfo && desc && (linfo->align != 4096)) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 220 | if (!vdev) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 221 | return -EINVAL; |
| 222 | } |
| 223 | |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 224 | if (info) { |
| 225 | virtio_queue_set_rings(vdev, index, desc, info->avail, info->used); |
| 226 | } else { |
| 227 | virtio_queue_set_addr(vdev, index, desc); |
| 228 | } |
| 229 | if (!desc) { |
Jason Wang | 955cc8c | 2015-04-23 14:21:40 +0800 | [diff] [blame] | 230 | virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 231 | } else { |
Cornelia Huck | 79cd0c8 | 2015-09-11 15:16:42 +0200 | [diff] [blame] | 232 | if (info) { |
| 233 | /* virtio-1 allows changing the ring size. */ |
Michael S. Tsirkin | 8c797e7 | 2017-01-12 23:26:22 +0200 | [diff] [blame] | 234 | if (virtio_queue_get_max_num(vdev, index) < num) { |
Cornelia Huck | 79cd0c8 | 2015-09-11 15:16:42 +0200 | [diff] [blame] | 235 | /* Fail if we exceed the maximum number. */ |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | virtio_queue_set_num(vdev, index, num); |
Carlos López | f0d634e | 2023-03-17 01:27:51 +0100 | [diff] [blame] | 239 | virtio_init_region_cache(vdev, index); |
Cornelia Huck | 79cd0c8 | 2015-09-11 15:16:42 +0200 | [diff] [blame] | 240 | } else if (virtio_queue_get_num(vdev, index) > num) { |
| 241 | /* Fail if we don't have a big enough queue. */ |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | } |
Cornelia Huck | 79cd0c8 | 2015-09-11 15:16:42 +0200 | [diff] [blame] | 244 | /* We ignore possible increased num for legacy for compatibility. */ |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 245 | virtio_queue_set_vector(vdev, index, index); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 246 | } |
| 247 | /* tell notify handler in case of config change */ |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 248 | vdev->config_vector = VIRTIO_QUEUE_MAX; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Paolo Bonzini | 997340f | 2022-06-09 08:35:45 +0200 | [diff] [blame] | 252 | static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev) |
Cornelia Huck | fa8b0ca | 2015-06-23 15:46:31 +0200 | [diff] [blame] | 253 | { |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 254 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 255 | |
Paolo Bonzini | 997340f | 2022-06-09 08:35:45 +0200 | [diff] [blame] | 256 | virtio_bus_reset(&dev->bus); |
Cornelia Huck | fa8b0ca | 2015-06-23 15:46:31 +0200 | [diff] [blame] | 257 | if (dev->indicators) { |
| 258 | release_indicator(&dev->routes.adapter, dev->indicators); |
| 259 | dev->indicators = NULL; |
| 260 | } |
| 261 | if (dev->indicators2) { |
| 262 | release_indicator(&dev->routes.adapter, dev->indicators2); |
| 263 | dev->indicators2 = NULL; |
| 264 | } |
| 265 | if (dev->summary_indicator) { |
| 266 | release_indicator(&dev->routes.adapter, dev->summary_indicator); |
| 267 | dev->summary_indicator = NULL; |
| 268 | } |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 269 | ccw_dev->sch->thinint_active = false; |
Cornelia Huck | fa8b0ca | 2015-06-23 15:46:31 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 272 | static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len, |
| 273 | bool is_legacy) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 274 | { |
| 275 | int ret; |
| 276 | VqInfoBlock info; |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 277 | VqInfoBlockLegacy linfo; |
| 278 | size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info); |
| 279 | |
| 280 | if (check_len) { |
| 281 | if (ccw.count != info_len) { |
| 282 | return -EINVAL; |
| 283 | } |
| 284 | } else if (ccw.count < info_len) { |
| 285 | /* Can't execute command. */ |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | if (!ccw.cda) { |
| 289 | return -EFAULT; |
| 290 | } |
| 291 | if (is_legacy) { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 292 | ret = ccw_dstream_read(&sch->cds, linfo); |
| 293 | if (ret) { |
| 294 | return ret; |
| 295 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 296 | linfo.queue = be64_to_cpu(linfo.queue); |
| 297 | linfo.align = be32_to_cpu(linfo.align); |
| 298 | linfo.index = be16_to_cpu(linfo.index); |
| 299 | linfo.num = be16_to_cpu(linfo.num); |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 300 | ret = virtio_ccw_set_vqs(sch, NULL, &linfo); |
| 301 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 302 | ret = ccw_dstream_read(&sch->cds, info); |
| 303 | if (ret) { |
| 304 | return ret; |
| 305 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 306 | info.desc = be64_to_cpu(info.desc); |
| 307 | info.index = be16_to_cpu(info.index); |
| 308 | info.num = be16_to_cpu(info.num); |
| 309 | info.avail = be64_to_cpu(info.avail); |
| 310 | info.used = be64_to_cpu(info.used); |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 311 | ret = virtio_ccw_set_vqs(sch, &info, NULL); |
| 312 | } |
| 313 | sch->curr_status.scsw.count = 0; |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) |
| 318 | { |
| 319 | int ret; |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 320 | VirtioRevInfo revinfo; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 321 | uint8_t status; |
| 322 | VirtioFeatDesc features; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 323 | hwaddr indicators; |
| 324 | VqConfigBlock vq_config; |
| 325 | VirtioCcwDevice *dev = sch->driver_data; |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 326 | VirtIODevice *vdev = virtio_ccw_get_vdev(sch); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 327 | bool check_len; |
| 328 | int len; |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 329 | VirtioThinintInfo thinint; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 330 | |
| 331 | if (!dev) { |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid, |
| 336 | ccw.cmd_code); |
| 337 | check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC)); |
| 338 | |
Cornelia Huck | 151fcdf | 2021-02-16 12:18:30 +0100 | [diff] [blame] | 339 | if (dev->revision < 0 && ccw.cmd_code != CCW_CMD_SET_VIRTIO_REV) { |
| 340 | if (dev->force_revision_1) { |
| 341 | /* |
| 342 | * virtio-1 drivers must start with negotiating to a revision >= 1, |
| 343 | * so post a command reject for all other commands |
| 344 | */ |
| 345 | return -ENOSYS; |
| 346 | } else { |
| 347 | /* |
| 348 | * If the driver issues any command that is not SET_VIRTIO_REV, |
| 349 | * we'll have to operate the device in legacy mode. |
| 350 | */ |
| 351 | dev->revision = 0; |
| 352 | } |
Halil Pasic | 47e13df | 2017-02-14 14:06:07 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 355 | /* Look at the command. */ |
| 356 | switch (ccw.cmd_code) { |
| 357 | case CCW_CMD_SET_VQ: |
Cornelia Huck | 0db87e0 | 2014-12-11 14:25:13 +0100 | [diff] [blame] | 358 | ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 359 | break; |
| 360 | case CCW_CMD_VDEV_RESET: |
Paolo Bonzini | 997340f | 2022-06-09 08:35:45 +0200 | [diff] [blame] | 361 | virtio_ccw_reset_virtio(dev); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 362 | ret = 0; |
| 363 | break; |
| 364 | case CCW_CMD_READ_FEAT: |
| 365 | if (check_len) { |
| 366 | if (ccw.count != sizeof(features)) { |
| 367 | ret = -EINVAL; |
| 368 | break; |
| 369 | } |
| 370 | } else if (ccw.count < sizeof(features)) { |
| 371 | /* Can't execute command. */ |
| 372 | ret = -EINVAL; |
| 373 | break; |
| 374 | } |
| 375 | if (!ccw.cda) { |
| 376 | ret = -EFAULT; |
| 377 | } else { |
Michael S. Tsirkin | 9b706db | 2016-11-04 12:04:23 +0200 | [diff] [blame] | 378 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 379 | |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 380 | ccw_dstream_advance(&sch->cds, sizeof(features.features)); |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 381 | ret = ccw_dstream_read(&sch->cds, features.index); |
| 382 | if (ret) { |
| 383 | break; |
| 384 | } |
Cornelia Huck | 6b8f102 | 2015-05-26 16:34:47 +0200 | [diff] [blame] | 385 | if (features.index == 0) { |
Cornelia Huck | 542571d | 2015-09-11 15:16:44 +0200 | [diff] [blame] | 386 | if (dev->revision >= 1) { |
| 387 | /* Don't offer legacy features for modern devices. */ |
| 388 | features.features = (uint32_t) |
Michael S. Tsirkin | 9b706db | 2016-11-04 12:04:23 +0200 | [diff] [blame] | 389 | (vdev->host_features & ~vdc->legacy_features); |
Cornelia Huck | 542571d | 2015-09-11 15:16:44 +0200 | [diff] [blame] | 390 | } else { |
| 391 | features.features = (uint32_t)vdev->host_features; |
| 392 | } |
Cornelia Huck | b4f8f9d | 2015-09-11 15:16:43 +0200 | [diff] [blame] | 393 | } else if ((features.index == 1) && (dev->revision >= 1)) { |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 394 | /* |
Cornelia Huck | b4f8f9d | 2015-09-11 15:16:43 +0200 | [diff] [blame] | 395 | * Only offer feature bits beyond 31 if the guest has |
| 396 | * negotiated at least revision 1. |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 397 | */ |
Cornelia Huck | b4f8f9d | 2015-09-11 15:16:43 +0200 | [diff] [blame] | 398 | features.features = (uint32_t)(vdev->host_features >> 32); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 399 | } else { |
| 400 | /* Return zeroes if the guest supports more feature bits. */ |
| 401 | features.features = 0; |
| 402 | } |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 403 | ccw_dstream_rewind(&sch->cds); |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 404 | features.features = cpu_to_le32(features.features); |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 405 | ret = ccw_dstream_write(&sch->cds, features.features); |
| 406 | if (!ret) { |
| 407 | sch->curr_status.scsw.count = ccw.count - sizeof(features); |
| 408 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 409 | } |
| 410 | break; |
| 411 | case CCW_CMD_WRITE_FEAT: |
| 412 | if (check_len) { |
| 413 | if (ccw.count != sizeof(features)) { |
| 414 | ret = -EINVAL; |
| 415 | break; |
| 416 | } |
| 417 | } else if (ccw.count < sizeof(features)) { |
| 418 | /* Can't execute command. */ |
| 419 | ret = -EINVAL; |
| 420 | break; |
| 421 | } |
| 422 | if (!ccw.cda) { |
| 423 | ret = -EFAULT; |
| 424 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 425 | ret = ccw_dstream_read(&sch->cds, features); |
| 426 | if (ret) { |
| 427 | break; |
| 428 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 429 | features.features = le32_to_cpu(features.features); |
Cornelia Huck | 6b8f102 | 2015-05-26 16:34:47 +0200 | [diff] [blame] | 430 | if (features.index == 0) { |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 431 | virtio_set_features(vdev, |
| 432 | (vdev->guest_features & 0xffffffff00000000ULL) | |
| 433 | features.features); |
Cornelia Huck | b4f8f9d | 2015-09-11 15:16:43 +0200 | [diff] [blame] | 434 | } else if ((features.index == 1) && (dev->revision >= 1)) { |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 435 | /* |
Cornelia Huck | b4f8f9d | 2015-09-11 15:16:43 +0200 | [diff] [blame] | 436 | * If the guest did not negotiate at least revision 1, |
| 437 | * we did not offer it any feature bits beyond 31. Such a |
| 438 | * guest passing us any bit here is therefore buggy. |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 439 | */ |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 440 | virtio_set_features(vdev, |
| 441 | (vdev->guest_features & 0x00000000ffffffffULL) | |
| 442 | ((uint64_t)features.features << 32)); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 443 | } else { |
| 444 | /* |
| 445 | * If the guest supports more feature bits, assert that it |
| 446 | * passes us zeroes for those we don't support. |
| 447 | */ |
| 448 | if (features.features) { |
Alistair Francis | 74a69e0 | 2017-12-20 09:24:41 -0800 | [diff] [blame] | 449 | qemu_log_mask(LOG_GUEST_ERROR, |
| 450 | "Guest bug: features[%i]=%x (expected 0)", |
| 451 | features.index, features.features); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 452 | /* XXX: do a unit check here? */ |
| 453 | } |
| 454 | } |
| 455 | sch->curr_status.scsw.count = ccw.count - sizeof(features); |
| 456 | ret = 0; |
| 457 | } |
| 458 | break; |
| 459 | case CCW_CMD_READ_CONF: |
| 460 | if (check_len) { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 461 | if (ccw.count > vdev->config_len) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 462 | ret = -EINVAL; |
| 463 | break; |
| 464 | } |
| 465 | } |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 466 | len = MIN(ccw.count, vdev->config_len); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 467 | if (!ccw.cda) { |
| 468 | ret = -EFAULT; |
| 469 | } else { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 470 | virtio_bus_get_vdev_config(&dev->bus, vdev->config); |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 471 | ret = ccw_dstream_write_buf(&sch->cds, vdev->config, len); |
| 472 | if (ret) { |
| 473 | sch->curr_status.scsw.count = ccw.count - len; |
| 474 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 475 | } |
| 476 | break; |
| 477 | case CCW_CMD_WRITE_CONF: |
| 478 | if (check_len) { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 479 | if (ccw.count > vdev->config_len) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 480 | ret = -EINVAL; |
| 481 | break; |
| 482 | } |
| 483 | } |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 484 | len = MIN(ccw.count, vdev->config_len); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 485 | if (!ccw.cda) { |
| 486 | ret = -EFAULT; |
| 487 | } else { |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 488 | ret = ccw_dstream_read_buf(&sch->cds, vdev->config, len); |
| 489 | if (!ret) { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 490 | virtio_bus_set_vdev_config(&dev->bus, vdev->config); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 491 | sch->curr_status.scsw.count = ccw.count - len; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | break; |
Pierre Morel | e32652f | 2016-09-07 12:58:40 +0200 | [diff] [blame] | 495 | case CCW_CMD_READ_STATUS: |
| 496 | if (check_len) { |
| 497 | if (ccw.count != sizeof(status)) { |
| 498 | ret = -EINVAL; |
| 499 | break; |
| 500 | } |
| 501 | } else if (ccw.count < sizeof(status)) { |
| 502 | /* Can't execute command. */ |
| 503 | ret = -EINVAL; |
| 504 | break; |
| 505 | } |
| 506 | if (!ccw.cda) { |
| 507 | ret = -EFAULT; |
| 508 | } else { |
| 509 | address_space_stb(&address_space_memory, ccw.cda, vdev->status, |
| 510 | MEMTXATTRS_UNSPECIFIED, NULL); |
Ladi Prosek | 3c254ab | 2017-10-17 16:40:51 +0200 | [diff] [blame] | 511 | sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status); |
Pierre Morel | e32652f | 2016-09-07 12:58:40 +0200 | [diff] [blame] | 512 | ret = 0; |
| 513 | } |
| 514 | break; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 515 | case CCW_CMD_WRITE_STATUS: |
| 516 | if (check_len) { |
| 517 | if (ccw.count != sizeof(status)) { |
| 518 | ret = -EINVAL; |
| 519 | break; |
| 520 | } |
| 521 | } else if (ccw.count < sizeof(status)) { |
| 522 | /* Can't execute command. */ |
| 523 | ret = -EINVAL; |
| 524 | break; |
| 525 | } |
| 526 | if (!ccw.cda) { |
| 527 | ret = -EFAULT; |
| 528 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 529 | ret = ccw_dstream_read(&sch->cds, status); |
| 530 | if (ret) { |
| 531 | break; |
| 532 | } |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 533 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
| 534 | virtio_ccw_stop_ioeventfd(dev); |
| 535 | } |
Cornelia Huck | 0b352fd | 2015-06-04 12:34:15 +0200 | [diff] [blame] | 536 | if (virtio_set_status(vdev, status) == 0) { |
| 537 | if (vdev->status == 0) { |
Paolo Bonzini | 997340f | 2022-06-09 08:35:45 +0200 | [diff] [blame] | 538 | virtio_ccw_reset_virtio(dev); |
Cornelia Huck | 0b352fd | 2015-06-04 12:34:15 +0200 | [diff] [blame] | 539 | } |
| 540 | if (status & VIRTIO_CONFIG_S_DRIVER_OK) { |
| 541 | virtio_ccw_start_ioeventfd(dev); |
| 542 | } |
| 543 | sch->curr_status.scsw.count = ccw.count - sizeof(status); |
| 544 | ret = 0; |
| 545 | } else { |
| 546 | /* Trigger a command reject. */ |
| 547 | ret = -ENOSYS; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 548 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 549 | } |
| 550 | break; |
| 551 | case CCW_CMD_SET_IND: |
| 552 | if (check_len) { |
| 553 | if (ccw.count != sizeof(indicators)) { |
| 554 | ret = -EINVAL; |
| 555 | break; |
| 556 | } |
| 557 | } else if (ccw.count < sizeof(indicators)) { |
| 558 | /* Can't execute command. */ |
| 559 | ret = -EINVAL; |
| 560 | break; |
| 561 | } |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 562 | if (sch->thinint_active) { |
| 563 | /* Trigger a command reject. */ |
| 564 | ret = -ENOSYS; |
| 565 | break; |
| 566 | } |
Halil Pasic | 797b608 | 2015-12-10 12:55:06 +0100 | [diff] [blame] | 567 | if (virtio_get_num_queues(vdev) > NR_CLASSIC_INDICATOR_BITS) { |
| 568 | /* More queues than indicator bits --> trigger a reject */ |
| 569 | ret = -ENOSYS; |
| 570 | break; |
| 571 | } |
Cornelia Huck | d1db1fa | 2013-06-05 17:13:05 +0200 | [diff] [blame] | 572 | if (!ccw.cda) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 573 | ret = -EFAULT; |
| 574 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 575 | ret = ccw_dstream_read(&sch->cds, indicators); |
| 576 | if (ret) { |
| 577 | break; |
| 578 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 579 | indicators = be64_to_cpu(indicators); |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 580 | dev->indicators = get_indicator(indicators, sizeof(uint64_t)); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 581 | sch->curr_status.scsw.count = ccw.count - sizeof(indicators); |
| 582 | ret = 0; |
| 583 | } |
| 584 | break; |
| 585 | case CCW_CMD_SET_CONF_IND: |
| 586 | if (check_len) { |
| 587 | if (ccw.count != sizeof(indicators)) { |
| 588 | ret = -EINVAL; |
| 589 | break; |
| 590 | } |
| 591 | } else if (ccw.count < sizeof(indicators)) { |
| 592 | /* Can't execute command. */ |
| 593 | ret = -EINVAL; |
| 594 | break; |
| 595 | } |
Cornelia Huck | d1db1fa | 2013-06-05 17:13:05 +0200 | [diff] [blame] | 596 | if (!ccw.cda) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 597 | ret = -EFAULT; |
| 598 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 599 | ret = ccw_dstream_read(&sch->cds, indicators); |
| 600 | if (ret) { |
| 601 | break; |
| 602 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 603 | indicators = be64_to_cpu(indicators); |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 604 | dev->indicators2 = get_indicator(indicators, sizeof(uint64_t)); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 605 | sch->curr_status.scsw.count = ccw.count - sizeof(indicators); |
| 606 | ret = 0; |
| 607 | } |
| 608 | break; |
| 609 | case CCW_CMD_READ_VQ_CONF: |
| 610 | if (check_len) { |
| 611 | if (ccw.count != sizeof(vq_config)) { |
| 612 | ret = -EINVAL; |
| 613 | break; |
| 614 | } |
| 615 | } else if (ccw.count < sizeof(vq_config)) { |
| 616 | /* Can't execute command. */ |
| 617 | ret = -EINVAL; |
| 618 | break; |
| 619 | } |
| 620 | if (!ccw.cda) { |
| 621 | ret = -EFAULT; |
| 622 | } else { |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 623 | ret = ccw_dstream_read(&sch->cds, vq_config.index); |
| 624 | if (ret) { |
| 625 | break; |
| 626 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 627 | vq_config.index = be16_to_cpu(vq_config.index); |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 628 | if (vq_config.index >= VIRTIO_QUEUE_MAX) { |
Cornelia Huck | d03a363 | 2015-03-20 13:16:20 +0100 | [diff] [blame] | 629 | ret = -EINVAL; |
| 630 | break; |
| 631 | } |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 632 | vq_config.num_max = virtio_queue_get_num(vdev, |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 633 | vq_config.index); |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 634 | vq_config.num_max = cpu_to_be16(vq_config.num_max); |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 635 | ret = ccw_dstream_write(&sch->cds, vq_config.num_max); |
| 636 | if (!ret) { |
| 637 | sch->curr_status.scsw.count = ccw.count - sizeof(vq_config); |
| 638 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 639 | } |
| 640 | break; |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 641 | case CCW_CMD_SET_IND_ADAPTER: |
| 642 | if (check_len) { |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 643 | if (ccw.count != sizeof(thinint)) { |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 644 | ret = -EINVAL; |
| 645 | break; |
| 646 | } |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 647 | } else if (ccw.count < sizeof(thinint)) { |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 648 | /* Can't execute command. */ |
| 649 | ret = -EINVAL; |
| 650 | break; |
| 651 | } |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 652 | if (!ccw.cda) { |
| 653 | ret = -EFAULT; |
| 654 | } else if (dev->indicators && !sch->thinint_active) { |
| 655 | /* Trigger a command reject. */ |
| 656 | ret = -ENOSYS; |
| 657 | } else { |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 658 | if (ccw_dstream_read(&sch->cds, thinint)) { |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 659 | ret = -EFAULT; |
| 660 | } else { |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 661 | thinint.ind_bit = be64_to_cpu(thinint.ind_bit); |
| 662 | thinint.summary_indicator = |
| 663 | be64_to_cpu(thinint.summary_indicator); |
| 664 | thinint.device_indicator = |
| 665 | be64_to_cpu(thinint.device_indicator); |
Cornelia Huck | 7d45285 | 2015-03-11 10:57:50 +0100 | [diff] [blame] | 666 | |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 667 | dev->summary_indicator = |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 668 | get_indicator(thinint.summary_indicator, sizeof(uint8_t)); |
Cornelia Huck | 7d45285 | 2015-03-11 10:57:50 +0100 | [diff] [blame] | 669 | dev->indicators = |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 670 | get_indicator(thinint.device_indicator, |
| 671 | thinint.ind_bit / 8 + 1); |
| 672 | dev->thinint_isc = thinint.isc; |
| 673 | dev->routes.adapter.ind_offset = thinint.ind_bit; |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 674 | dev->routes.adapter.summary_offset = 7; |
Fei Li | dde522b | 2016-11-24 11:10:39 +0100 | [diff] [blame] | 675 | dev->routes.adapter.adapter_id = css_get_adapter_id( |
| 676 | CSS_IO_ADAPTER_VIRTIO, |
| 677 | dev->thinint_isc); |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 678 | sch->thinint_active = ((dev->indicators != NULL) && |
| 679 | (dev->summary_indicator != NULL)); |
Halil Pasic | f57ba05 | 2017-09-21 20:08:39 +0200 | [diff] [blame] | 680 | sch->curr_status.scsw.count = ccw.count - sizeof(thinint); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 681 | ret = 0; |
| 682 | } |
| 683 | } |
| 684 | break; |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 685 | case CCW_CMD_SET_VIRTIO_REV: |
| 686 | len = sizeof(revinfo); |
| 687 | if (ccw.count < len) { |
| 688 | ret = -EINVAL; |
| 689 | break; |
| 690 | } |
| 691 | if (!ccw.cda) { |
| 692 | ret = -EFAULT; |
| 693 | break; |
| 694 | } |
Pierre Morel | d895d25 | 2021-04-08 18:32:09 +0200 | [diff] [blame] | 695 | ret = ccw_dstream_read_buf(&sch->cds, &revinfo, 4); |
| 696 | if (ret < 0) { |
| 697 | break; |
| 698 | } |
Peter Maydell | c9aacaa | 2018-12-10 12:04:36 +0000 | [diff] [blame] | 699 | revinfo.revision = be16_to_cpu(revinfo.revision); |
| 700 | revinfo.length = be16_to_cpu(revinfo.length); |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 701 | if (ccw.count < len + revinfo.length || |
| 702 | (check_len && ccw.count > len + revinfo.length)) { |
| 703 | ret = -EINVAL; |
| 704 | break; |
| 705 | } |
| 706 | /* |
| 707 | * Once we start to support revisions with additional data, we'll |
| 708 | * need to fetch it here. Nothing to do for now, though. |
| 709 | */ |
| 710 | if (dev->revision >= 0 || |
Halil Pasic | 47e13df | 2017-02-14 14:06:07 +0100 | [diff] [blame] | 711 | revinfo.revision > virtio_ccw_rev_max(dev) || |
| 712 | (dev->force_revision_1 && !revinfo.revision)) { |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 713 | ret = -ENOSYS; |
| 714 | break; |
| 715 | } |
| 716 | ret = 0; |
| 717 | dev->revision = revinfo.revision; |
| 718 | break; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 719 | default: |
Cornelia Huck | 8d034a6 | 2013-01-28 17:01:30 +0100 | [diff] [blame] | 720 | ret = -ENOSYS; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | return ret; |
| 724 | } |
| 725 | |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 726 | static void virtio_sch_disable_cb(SubchDev *sch) |
| 727 | { |
| 728 | VirtioCcwDevice *dev = sch->driver_data; |
| 729 | |
| 730 | dev->revision = -1; |
| 731 | } |
| 732 | |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 733 | static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 734 | { |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 735 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 736 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 737 | CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); |
Dong Jia Shi | 817d4a6 | 2017-05-17 02:48:05 +0200 | [diff] [blame] | 738 | SubchDev *sch; |
Sascha Silbe | cf24993 | 2016-06-15 17:16:05 +0200 | [diff] [blame] | 739 | Error *err = NULL; |
Cornelia Huck | 3c5fd80 | 2020-01-16 13:10:35 +0100 | [diff] [blame] | 740 | int i; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 741 | |
Cornelia Huck | 36699ab | 2018-07-23 18:32:21 +0200 | [diff] [blame] | 742 | sch = css_create_sch(ccw_dev->devno, errp); |
Sascha Silbe | cf24993 | 2016-06-15 17:16:05 +0200 | [diff] [blame] | 743 | if (!sch) { |
| 744 | return; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 745 | } |
Halil Pasic | 47e13df | 2017-02-14 14:06:07 +0100 | [diff] [blame] | 746 | if (!virtio_ccw_rev_max(dev) && dev->force_revision_1) { |
| 747 | error_setg(&err, "Invalid value of property max_rev " |
| 748 | "(is %d expected >= 1)", virtio_ccw_rev_max(dev)); |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 749 | goto out_err; |
Halil Pasic | 47e13df | 2017-02-14 14:06:07 +0100 | [diff] [blame] | 750 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 751 | |
Sascha Silbe | cf24993 | 2016-06-15 17:16:05 +0200 | [diff] [blame] | 752 | sch->driver_data = dev; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 753 | sch->ccw_cb = virtio_ccw_cb; |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 754 | sch->disable_cb = virtio_sch_disable_cb; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 755 | sch->id.reserved = 0xff; |
| 756 | sch->id.cu_type = VIRTIO_CCW_CU_TYPE; |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 757 | sch->do_subchannel_work = do_subchannel_work_virtual; |
Eric Farman | 0599a04 | 2021-06-18 01:25:36 +0200 | [diff] [blame] | 758 | sch->irb_cb = build_irb_virtual; |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 759 | ccw_dev->sch = sch; |
Sascha Silbe | cf24993 | 2016-06-15 17:16:05 +0200 | [diff] [blame] | 760 | dev->indicators = NULL; |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 761 | dev->revision = -1; |
Cornelia Huck | 3c5fd80 | 2020-01-16 13:10:35 +0100 | [diff] [blame] | 762 | for (i = 0; i < ADAPTER_ROUTES_MAX_GSI; i++) { |
| 763 | dev->routes.gsi[i] = -1; |
| 764 | } |
Sascha Silbe | cf24993 | 2016-06-15 17:16:05 +0200 | [diff] [blame] | 765 | css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE); |
| 766 | |
| 767 | trace_virtio_ccw_new_device( |
| 768 | sch->cssid, sch->ssid, sch->schid, sch->devno, |
Dong Jia Shi | 2a78ac6 | 2017-02-15 06:33:03 +0100 | [diff] [blame] | 769 | ccw_dev->devno.valid ? "user-configured" : "auto-configured"); |
Thomas Huth | c42767f | 2014-12-11 14:25:12 +0100 | [diff] [blame] | 770 | |
Pavel Dovgalyuk | 3909c07 | 2021-05-17 16:06:28 +0300 | [diff] [blame] | 771 | /* fd-based ioevents can't be synchronized in record/replay */ |
| 772 | if (replay_mode != REPLAY_MODE_NONE) { |
| 773 | dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD; |
| 774 | } |
| 775 | |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 776 | if (k->realize) { |
| 777 | k->realize(dev, &err); |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 778 | if (err) { |
| 779 | goto out_err; |
| 780 | } |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 781 | } |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 782 | |
| 783 | ck->realize(ccw_dev, &err); |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 784 | if (err) { |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 785 | goto out_err; |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 786 | } |
Dong Jia Shi | d8d98db | 2017-02-22 03:10:30 +0100 | [diff] [blame] | 787 | |
| 788 | return; |
| 789 | |
| 790 | out_err: |
| 791 | error_propagate(errp, err); |
| 792 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 793 | ccw_dev->sch = NULL; |
| 794 | g_free(sch); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 797 | static void virtio_ccw_device_unrealize(VirtioCcwDevice *dev) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 798 | { |
Thomas Huth | 2d6ff33 | 2018-07-25 14:20:15 +0200 | [diff] [blame] | 799 | VirtIOCCWDeviceClass *dc = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 800 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 801 | SubchDev *sch = ccw_dev->sch; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 802 | |
Thomas Huth | 2d6ff33 | 2018-07-25 14:20:15 +0200 | [diff] [blame] | 803 | if (dc->unrealize) { |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 804 | dc->unrealize(dev); |
Thomas Huth | 2d6ff33 | 2018-07-25 14:20:15 +0200 | [diff] [blame] | 805 | } |
| 806 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 807 | if (sch) { |
| 808 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 809 | g_free(sch); |
Nia Alarie | 24118af | 2018-03-07 16:29:58 +0000 | [diff] [blame] | 810 | ccw_dev->sch = NULL; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 811 | } |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 812 | if (dev->indicators) { |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 813 | release_indicator(&dev->routes.adapter, dev->indicators); |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 814 | dev->indicators = NULL; |
| 815 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 818 | /* DeviceState to VirtioCcwDevice. Note: used on datapath, |
| 819 | * be careful and test performance if you change this. |
| 820 | */ |
| 821 | static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) |
| 822 | { |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 823 | CcwDevice *ccw_dev = to_ccw_dev_fast(d); |
| 824 | |
| 825 | return container_of(ccw_dev, VirtioCcwDevice, parent_obj); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 828 | static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc, |
| 829 | uint8_t to_be_set) |
| 830 | { |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 831 | uint8_t expected, actual; |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 832 | hwaddr len = 1; |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 833 | /* avoid multiple fetches */ |
| 834 | uint8_t volatile *ind_addr; |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 835 | |
Philippe Mathieu-Daudé | 85eb7c1 | 2020-02-19 20:20:42 +0100 | [diff] [blame] | 836 | ind_addr = cpu_physical_memory_map(ind_loc, &len, true); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 837 | if (!ind_addr) { |
| 838 | error_report("%s(%x.%x.%04x): unable to access indicator", |
| 839 | __func__, sch->cssid, sch->ssid, sch->schid); |
| 840 | return -1; |
| 841 | } |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 842 | actual = *ind_addr; |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 843 | do { |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 844 | expected = actual; |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 845 | actual = qatomic_cmpxchg(ind_addr, expected, expected | to_be_set); |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 846 | } while (actual != expected); |
| 847 | trace_virtio_ccw_set_ind(ind_loc, actual, actual | to_be_set); |
| 848 | cpu_physical_memory_unmap((void *)ind_addr, len, 1, len); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 849 | |
Halil Pasic | 1a8242f | 2020-06-16 06:50:34 +0200 | [diff] [blame] | 850 | return actual; |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 853 | static void virtio_ccw_notify(DeviceState *d, uint16_t vector) |
| 854 | { |
| 855 | VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 856 | CcwDevice *ccw_dev = to_ccw_dev_fast(d); |
| 857 | SubchDev *sch = ccw_dev->sch; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 858 | uint64_t indicators; |
| 859 | |
Halil Pasic | 7a5342e | 2018-05-16 15:27:57 +0200 | [diff] [blame] | 860 | if (vector == VIRTIO_NO_VECTOR) { |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 861 | return; |
| 862 | } |
Halil Pasic | 7a5342e | 2018-05-16 15:27:57 +0200 | [diff] [blame] | 863 | /* |
| 864 | * vector < VIRTIO_QUEUE_MAX: notification for a virtqueue |
| 865 | * vector == VIRTIO_QUEUE_MAX: configuration change notification |
| 866 | * bits beyond that are unused and should never be notified for |
| 867 | */ |
| 868 | assert(vector <= VIRTIO_QUEUE_MAX); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 869 | |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 870 | if (vector < VIRTIO_QUEUE_MAX) { |
Cornelia Huck | 7c48697 | 2013-04-23 17:15:19 +0200 | [diff] [blame] | 871 | if (!dev->indicators) { |
| 872 | return; |
| 873 | } |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 874 | if (sch->thinint_active) { |
| 875 | /* |
| 876 | * In the adapter interrupt case, indicators points to a |
| 877 | * memory area that may be (way) larger than 64 bit and |
| 878 | * ind_bit indicates the start of the indicators in a big |
| 879 | * endian notation. |
| 880 | */ |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 881 | uint64_t ind_bit = dev->routes.adapter.ind_offset; |
| 882 | |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 883 | virtio_set_ind_atomic(sch, dev->indicators->addr + |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 884 | (ind_bit + vector) / 8, |
| 885 | 0x80 >> ((ind_bit + vector) % 8)); |
Cornelia Huck | 7bca389 | 2013-08-01 17:27:00 +0200 | [diff] [blame] | 886 | if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr, |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 887 | 0x01)) { |
Yi Min Zhao | 25a08b8 | 2017-02-17 15:26:48 +0800 | [diff] [blame] | 888 | css_adapter_interrupt(CSS_IO_ADAPTER_VIRTIO, dev->thinint_isc); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 889 | } |
| 890 | } else { |
Halil Pasic | 7a5342e | 2018-05-16 15:27:57 +0200 | [diff] [blame] | 891 | assert(vector < NR_CLASSIC_INDICATOR_BITS); |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 892 | indicators = address_space_ldq(&address_space_memory, |
| 893 | dev->indicators->addr, |
| 894 | MEMTXATTRS_UNSPECIFIED, |
| 895 | NULL); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 896 | indicators |= 1ULL << vector; |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 897 | address_space_stq(&address_space_memory, dev->indicators->addr, |
| 898 | indicators, MEMTXATTRS_UNSPECIFIED, NULL); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 899 | css_conditional_io_interrupt(sch); |
| 900 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 901 | } else { |
Cornelia Huck | 7c48697 | 2013-04-23 17:15:19 +0200 | [diff] [blame] | 902 | if (!dev->indicators2) { |
| 903 | return; |
| 904 | } |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 905 | indicators = address_space_ldq(&address_space_memory, |
| 906 | dev->indicators2->addr, |
| 907 | MEMTXATTRS_UNSPECIFIED, |
| 908 | NULL); |
Halil Pasic | 7a5342e | 2018-05-16 15:27:57 +0200 | [diff] [blame] | 909 | indicators |= 1ULL; |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 910 | address_space_stq(&address_space_memory, dev->indicators2->addr, |
| 911 | indicators, MEMTXATTRS_UNSPECIFIED, NULL); |
Cornelia Huck | 7e74946 | 2013-02-06 10:31:37 +0100 | [diff] [blame] | 912 | css_conditional_io_interrupt(sch); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 913 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Peter Maydell | 6affa00 | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 916 | static void virtio_ccw_reset_hold(Object *obj, ResetType type) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 917 | { |
Peter Maydell | 6affa00 | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 918 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(obj); |
Cornelia Huck | 838fb84 | 2018-05-07 15:27:57 +0200 | [diff] [blame] | 919 | VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 920 | |
Paolo Bonzini | 997340f | 2022-06-09 08:35:45 +0200 | [diff] [blame] | 921 | virtio_ccw_reset_virtio(dev); |
Peter Maydell | 6affa00 | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 922 | |
| 923 | if (vdc->parent_phases.hold) { |
| 924 | vdc->parent_phases.hold(obj, type); |
Cornelia Huck | 838fb84 | 2018-05-07 15:27:57 +0200 | [diff] [blame] | 925 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 928 | static void virtio_ccw_vmstate_change(DeviceState *d, bool running) |
| 929 | { |
| 930 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 931 | |
| 932 | if (running) { |
| 933 | virtio_ccw_start_ioeventfd(dev); |
| 934 | } else { |
| 935 | virtio_ccw_stop_ioeventfd(dev); |
| 936 | } |
| 937 | } |
| 938 | |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 939 | static bool virtio_ccw_query_guest_notifiers(DeviceState *d) |
| 940 | { |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 941 | CcwDevice *dev = CCW_DEVICE(d); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 942 | |
| 943 | return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA); |
| 944 | } |
| 945 | |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 946 | static int virtio_ccw_get_mappings(VirtioCcwDevice *dev) |
| 947 | { |
| 948 | int r; |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 949 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 950 | |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 951 | if (!ccw_dev->sch->thinint_active) { |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | r = map_indicator(&dev->routes.adapter, dev->summary_indicator); |
| 956 | if (r) { |
| 957 | return r; |
| 958 | } |
| 959 | r = map_indicator(&dev->routes.adapter, dev->indicators); |
| 960 | if (r) { |
| 961 | return r; |
| 962 | } |
| 963 | dev->routes.adapter.summary_addr = dev->summary_indicator->map; |
| 964 | dev->routes.adapter.ind_addr = dev->indicators->map; |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs) |
| 970 | { |
| 971 | int i; |
| 972 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 973 | int ret; |
| 974 | S390FLICState *fs = s390_get_flic(); |
David Hildenbrand | 6762808 | 2018-01-29 13:56:23 +0100 | [diff] [blame] | 975 | S390FLICStateClass *fsc = s390_get_flic_class(fs); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 976 | |
| 977 | ret = virtio_ccw_get_mappings(dev); |
| 978 | if (ret) { |
| 979 | return ret; |
| 980 | } |
| 981 | for (i = 0; i < nvqs; i++) { |
| 982 | if (!virtio_queue_get_num(vdev, i)) { |
| 983 | break; |
| 984 | } |
| 985 | } |
| 986 | dev->routes.num_routes = i; |
| 987 | return fsc->add_adapter_routes(fs, &dev->routes); |
| 988 | } |
| 989 | |
| 990 | static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs) |
| 991 | { |
| 992 | S390FLICState *fs = s390_get_flic(); |
David Hildenbrand | 6762808 | 2018-01-29 13:56:23 +0100 | [diff] [blame] | 993 | S390FLICStateClass *fsc = s390_get_flic_class(fs); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 994 | |
| 995 | fsc->release_adapter_routes(fs, &dev->routes); |
| 996 | } |
| 997 | |
| 998 | static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n) |
| 999 | { |
| 1000 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1001 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 1002 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 1003 | |
Eric Auger | 1c9b71a | 2015-07-06 12:15:13 -0600 | [diff] [blame] | 1004 | return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL, |
| 1005 | dev->routes.gsi[n]); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n) |
| 1009 | { |
| 1010 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1011 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 1012 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 1013 | int ret; |
| 1014 | |
Eric Auger | 1c9b71a | 2015-07-06 12:15:13 -0600 | [diff] [blame] | 1015 | ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier, |
| 1016 | dev->routes.gsi[n]); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1017 | assert(ret == 0); |
| 1018 | } |
| 1019 | |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1020 | static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n, |
| 1021 | bool assign, bool with_irqfd) |
| 1022 | { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1023 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1024 | VirtQueue *vq = virtio_get_queue(vdev, n); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1025 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1026 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1027 | |
| 1028 | if (assign) { |
| 1029 | int r = event_notifier_init(notifier, 0); |
| 1030 | |
| 1031 | if (r < 0) { |
| 1032 | return r; |
| 1033 | } |
| 1034 | virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1035 | if (with_irqfd) { |
| 1036 | r = virtio_ccw_add_irqfd(dev, n); |
| 1037 | if (r) { |
| 1038 | virtio_queue_set_guest_notifier_fd_handler(vq, false, |
| 1039 | with_irqfd); |
| 1040 | return r; |
| 1041 | } |
| 1042 | } |
| 1043 | /* |
| 1044 | * We do not support individual masking for channel devices, so we |
| 1045 | * need to manually trigger any guest masking callbacks here. |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1046 | */ |
Wei Huang | 2858bc6 | 2016-12-15 12:23:24 -0600 | [diff] [blame] | 1047 | if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1048 | k->guest_notifier_mask(vdev, n, false); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1049 | } |
| 1050 | /* get lost events and re-inject */ |
| 1051 | if (k->guest_notifier_pending && |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1052 | k->guest_notifier_pending(vdev, n)) { |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1053 | event_notifier_set(notifier); |
| 1054 | } |
| 1055 | } else { |
Wei Huang | 2858bc6 | 2016-12-15 12:23:24 -0600 | [diff] [blame] | 1056 | if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1057 | k->guest_notifier_mask(vdev, n, true); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1058 | } |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1059 | if (with_irqfd) { |
| 1060 | virtio_ccw_remove_irqfd(dev, n); |
| 1061 | } |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1062 | virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); |
| 1063 | event_notifier_cleanup(notifier); |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs, |
| 1069 | bool assigned) |
| 1070 | { |
| 1071 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
Paolo Bonzini | f24a684 | 2013-09-20 13:51:52 +0200 | [diff] [blame] | 1072 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1073 | CcwDevice *ccw_dev = CCW_DEVICE(d); |
| 1074 | bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled(); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1075 | int r, n; |
| 1076 | |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1077 | if (with_irqfd && assigned) { |
| 1078 | /* irq routes need to be set up before assigning irqfds */ |
| 1079 | r = virtio_ccw_setup_irqroutes(dev, nvqs); |
| 1080 | if (r < 0) { |
| 1081 | goto irqroute_error; |
| 1082 | } |
| 1083 | } |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1084 | for (n = 0; n < nvqs; n++) { |
| 1085 | if (!virtio_queue_get_num(vdev, n)) { |
| 1086 | break; |
| 1087 | } |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1088 | r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd); |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1089 | if (r < 0) { |
| 1090 | goto assign_error; |
| 1091 | } |
| 1092 | } |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1093 | if (with_irqfd && !assigned) { |
| 1094 | /* release irq routes after irqfds have been released */ |
| 1095 | virtio_ccw_release_irqroutes(dev, nvqs); |
| 1096 | } |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | |
| 1099 | assign_error: |
| 1100 | while (--n >= 0) { |
| 1101 | virtio_ccw_set_guest_notifier(dev, n, !assigned, false); |
| 1102 | } |
Cornelia Huck | d426d9f | 2013-07-15 17:45:03 +0200 | [diff] [blame] | 1103 | irqroute_error: |
| 1104 | if (with_irqfd && assigned) { |
| 1105 | virtio_ccw_release_irqroutes(dev, nvqs); |
| 1106 | } |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1107 | return r; |
| 1108 | } |
| 1109 | |
Jens Freimann | bcb2b58 | 2014-02-11 13:29:44 +0100 | [diff] [blame] | 1110 | static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f) |
| 1111 | { |
| 1112 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1113 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1114 | |
| 1115 | qemu_put_be16(f, virtio_queue_vector(vdev, n)); |
| 1116 | } |
| 1117 | |
| 1118 | static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f) |
| 1119 | { |
| 1120 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1121 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1122 | uint16_t vector; |
| 1123 | |
| 1124 | qemu_get_be16s(f, &vector); |
| 1125 | virtio_queue_set_vector(vdev, n , vector); |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f) |
| 1131 | { |
| 1132 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 1133 | vmstate_save_state(f, &vmstate_virtio_ccw_dev, dev, NULL); |
Jens Freimann | bcb2b58 | 2014-02-11 13:29:44 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f) |
| 1137 | { |
| 1138 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
Halil Pasic | 517ff12 | 2017-07-03 23:34:14 +0200 | [diff] [blame] | 1139 | return vmstate_load_state(f, &vmstate_virtio_ccw_dev, dev, 1); |
Jens Freimann | bcb2b58 | 2014-02-11 13:29:44 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Maxime Coquelin | d1b4259 | 2016-09-13 15:30:30 +0200 | [diff] [blame] | 1142 | static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp) |
| 1143 | { |
| 1144 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1145 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1146 | |
| 1147 | if (dev->max_rev >= 1) { |
| 1148 | virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Cornelia Huck | fb846a0 | 2015-04-21 16:36:56 +0200 | [diff] [blame] | 1152 | /* This is called by virtio-bus just after the device is plugged. */ |
Jason Wang | e839804 | 2015-05-29 14:15:25 +0800 | [diff] [blame] | 1153 | static void virtio_ccw_device_plugged(DeviceState *d, Error **errp) |
Cornelia Huck | fb846a0 | 2015-04-21 16:36:56 +0200 | [diff] [blame] | 1154 | { |
| 1155 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
Jason Wang | 10ceaa1 | 2015-05-29 14:15:28 +0800 | [diff] [blame] | 1156 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1157 | CcwDevice *ccw_dev = CCW_DEVICE(d); |
| 1158 | SubchDev *sch = ccw_dev->sch; |
Jason Wang | 10ceaa1 | 2015-05-29 14:15:28 +0800 | [diff] [blame] | 1159 | int n = virtio_get_num_queues(vdev); |
Halil Pasic | 0708afa | 2016-12-09 19:58:10 +0100 | [diff] [blame] | 1160 | S390FLICState *flic = s390_get_flic(); |
Jason Wang | 10ceaa1 | 2015-05-29 14:15:28 +0800 | [diff] [blame] | 1161 | |
Maxime Coquelin | d1b4259 | 2016-09-13 15:30:30 +0200 | [diff] [blame] | 1162 | if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) { |
| 1163 | dev->max_rev = 0; |
| 1164 | } |
| 1165 | |
Cornelia Huck | 9b3a35e | 2020-07-07 12:54:46 +0200 | [diff] [blame] | 1166 | if (!virtio_ccw_rev_max(dev) && !virtio_legacy_allowed(vdev)) { |
Stefano Garzarella | d55f518 | 2020-09-21 14:25:03 +0200 | [diff] [blame] | 1167 | /* |
| 1168 | * To avoid migration issues, we allow legacy mode when legacy |
| 1169 | * check is disabled in the old machine types (< 5.1). |
| 1170 | */ |
| 1171 | if (virtio_legacy_check_disabled(vdev)) { |
| 1172 | warn_report("device requires revision >= 1, but for backward " |
| 1173 | "compatibility max_revision=0 is allowed"); |
| 1174 | } else { |
| 1175 | error_setg(errp, "Invalid value of property max_rev " |
| 1176 | "(is %d expected >= 1)", virtio_ccw_rev_max(dev)); |
| 1177 | return; |
| 1178 | } |
Cornelia Huck | 9b3a35e | 2020-07-07 12:54:46 +0200 | [diff] [blame] | 1179 | } |
| 1180 | |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 1181 | if (virtio_get_num_queues(vdev) > VIRTIO_QUEUE_MAX) { |
Michael Tokarev | b34aee5 | 2016-05-18 15:41:59 +0300 | [diff] [blame] | 1182 | error_setg(errp, "The number of virtqueues %d " |
Halil Pasic | b1914b8 | 2015-12-07 16:45:17 +0100 | [diff] [blame] | 1183 | "exceeds virtio limit %d", n, |
| 1184 | VIRTIO_QUEUE_MAX); |
Jason Wang | 10ceaa1 | 2015-05-29 14:15:28 +0800 | [diff] [blame] | 1185 | return; |
| 1186 | } |
Halil Pasic | 0708afa | 2016-12-09 19:58:10 +0100 | [diff] [blame] | 1187 | if (virtio_get_num_queues(vdev) > flic->adapter_routes_max_batch) { |
| 1188 | error_setg(errp, "The number of virtqueues %d " |
| 1189 | "exceeds flic adapter route limit %d", n, |
| 1190 | flic->adapter_routes_max_batch); |
| 1191 | return; |
| 1192 | } |
Cornelia Huck | fb846a0 | 2015-04-21 16:36:56 +0200 | [diff] [blame] | 1193 | |
| 1194 | sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus); |
| 1195 | |
Cornelia Huck | 542571d | 2015-09-11 15:16:44 +0200 | [diff] [blame] | 1196 | |
Cornelia Huck | fb846a0 | 2015-04-21 16:36:56 +0200 | [diff] [blame] | 1197 | css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, |
| 1198 | d->hotplugged, 1); |
| 1199 | } |
| 1200 | |
| 1201 | static void virtio_ccw_device_unplugged(DeviceState *d) |
| 1202 | { |
| 1203 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1204 | |
| 1205 | virtio_ccw_stop_ioeventfd(dev); |
| 1206 | } |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1207 | /**************** Virtio-ccw Bus Device Descriptions *******************/ |
| 1208 | |
Markus Armbruster | 5e5ced3 | 2015-02-27 14:53:39 +0100 | [diff] [blame] | 1209 | static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1210 | { |
| 1211 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1212 | |
Andreas Färber | 1bf4d7a | 2013-08-23 20:27:30 +0200 | [diff] [blame] | 1213 | virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev); |
Cornelia Huck | 1fa7552 | 2015-04-21 16:36:55 +0200 | [diff] [blame] | 1214 | virtio_ccw_device_realize(_dev, errp); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 1217 | static void virtio_ccw_busdev_unrealize(DeviceState *dev) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1218 | { |
| 1219 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1220 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 1221 | virtio_ccw_device_unrealize(_dev); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1224 | static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev, |
| 1225 | DeviceState *dev, Error **errp) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1226 | { |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1227 | VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1228 | |
Paolo Bonzini | 0b81c1e | 2013-10-15 16:47:16 +0200 | [diff] [blame] | 1229 | virtio_ccw_stop_ioeventfd(_dev); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | static void virtio_ccw_device_class_init(ObjectClass *klass, void *data) |
| 1233 | { |
| 1234 | DeviceClass *dc = DEVICE_CLASS(klass); |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1235 | CCWDeviceClass *k = CCW_DEVICE_CLASS(dc); |
Cornelia Huck | 838fb84 | 2018-05-07 15:27:57 +0200 | [diff] [blame] | 1236 | VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_CLASS(klass); |
Peter Maydell | 6affa00 | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 1237 | ResettableClass *rc = RESETTABLE_CLASS(klass); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1238 | |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1239 | k->unplug = virtio_ccw_busdev_unplug; |
Markus Armbruster | 5e5ced3 | 2015-02-27 14:53:39 +0100 | [diff] [blame] | 1240 | dc->realize = virtio_ccw_busdev_realize; |
Nia Alarie | 24118af | 2018-03-07 16:29:58 +0000 | [diff] [blame] | 1241 | dc->unrealize = virtio_ccw_busdev_unrealize; |
Peter Maydell | 6affa00 | 2024-09-13 15:31:43 +0100 | [diff] [blame] | 1242 | resettable_class_set_parent_phases(rc, NULL, virtio_ccw_reset_hold, NULL, |
| 1243 | &vdc->parent_phases); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | static const TypeInfo virtio_ccw_device_info = { |
| 1247 | .name = TYPE_VIRTIO_CCW_DEVICE, |
Jing Liu | b804e8a | 2016-02-26 06:46:12 +0100 | [diff] [blame] | 1248 | .parent = TYPE_CCW_DEVICE, |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1249 | .instance_size = sizeof(VirtioCcwDevice), |
| 1250 | .class_init = virtio_ccw_device_class_init, |
| 1251 | .class_size = sizeof(VirtIOCCWDeviceClass), |
| 1252 | .abstract = true, |
| 1253 | }; |
| 1254 | |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1255 | /* virtio-ccw-bus */ |
| 1256 | |
Andreas Färber | 1bf4d7a | 2013-08-23 20:27:30 +0200 | [diff] [blame] | 1257 | static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, |
| 1258 | VirtioCcwDevice *dev) |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1259 | { |
| 1260 | DeviceState *qdev = DEVICE(dev); |
KONRAD Frederic | f4dd69a | 2013-04-30 16:08:47 +0200 | [diff] [blame] | 1261 | char virtio_bus_name[] = "virtio-bus"; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1262 | |
Peter Maydell | d637e1d | 2021-09-23 13:11:51 +0100 | [diff] [blame] | 1263 | qbus_init(bus, bus_size, TYPE_VIRTIO_CCW_BUS, qdev, virtio_bus_name); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) |
| 1267 | { |
| 1268 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); |
| 1269 | BusClass *bus_class = BUS_CLASS(klass); |
| 1270 | |
| 1271 | bus_class->max_dev = 1; |
| 1272 | k->notify = virtio_ccw_notify; |
Cornelia Huck | b4436a0 | 2013-02-15 10:18:43 +0100 | [diff] [blame] | 1273 | k->vmstate_change = virtio_ccw_vmstate_change; |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1274 | k->query_guest_notifiers = virtio_ccw_query_guest_notifiers; |
Cornelia Huck | 320ce85 | 2013-02-19 13:48:17 +0100 | [diff] [blame] | 1275 | k->set_guest_notifiers = virtio_ccw_set_guest_notifiers; |
Jens Freimann | bcb2b58 | 2014-02-11 13:29:44 +0100 | [diff] [blame] | 1276 | k->save_queue = virtio_ccw_save_queue; |
| 1277 | k->load_queue = virtio_ccw_load_queue; |
| 1278 | k->save_config = virtio_ccw_save_config; |
| 1279 | k->load_config = virtio_ccw_load_config; |
Maxime Coquelin | d1b4259 | 2016-09-13 15:30:30 +0200 | [diff] [blame] | 1280 | k->pre_plugged = virtio_ccw_pre_plugged; |
Cornelia Huck | fb846a0 | 2015-04-21 16:36:56 +0200 | [diff] [blame] | 1281 | k->device_plugged = virtio_ccw_device_plugged; |
| 1282 | k->device_unplugged = virtio_ccw_device_unplugged; |
Paolo Bonzini | 8e93cef | 2016-10-21 22:48:08 +0200 | [diff] [blame] | 1283 | k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled; |
Cornelia Huck | 7c55f68 | 2016-06-10 11:04:11 +0200 | [diff] [blame] | 1284 | k->ioeventfd_assign = virtio_ccw_ioeventfd_assign; |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static const TypeInfo virtio_ccw_bus_info = { |
| 1288 | .name = TYPE_VIRTIO_CCW_BUS, |
| 1289 | .parent = TYPE_VIRTIO_BUS, |
| 1290 | .instance_size = sizeof(VirtioCcwBusState), |
Cornelia Huck | 74ded8b | 2020-08-24 14:20:51 +0200 | [diff] [blame] | 1291 | .class_size = sizeof(VirtioCcwBusClass), |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1292 | .class_init = virtio_ccw_bus_class_init, |
| 1293 | }; |
| 1294 | |
| 1295 | static void virtio_ccw_register(void) |
| 1296 | { |
| 1297 | type_register_static(&virtio_ccw_bus_info); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1298 | type_register_static(&virtio_ccw_device_info); |
Cornelia Huck | a5cf2bb | 2013-01-24 06:08:55 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | type_init(virtio_ccw_register) |