1 /* 2 * SCLP 3 * Event Facility 4 * handles SCLP event types 5 * - Signal Quiesce - system power down 6 * - ASCII Console Data - VT220 read and write 7 * 8 * Copyright IBM, Corp. 2012 9 * 10 * Authors: 11 * Heinz Graalfs <graalfs@de.ibm.com> 12 * 13 * This work is licensed under the terms of the GNU GPL, version 2 or (at your 14 * option) any later version. See the COPYING file in the top-level directory. 15 * 16 */ 17 18 #include "qemu/osdep.h" 19 #include "qapi/error.h" 20 #include "sysemu/sysemu.h" 21 22 #include "hw/s390x/sclp.h" 23 #include "hw/s390x/event-facility.h" 24 25 typedef struct SCLPEventsBus { 26 BusState qbus; 27 } SCLPEventsBus; 28 29 struct SCLPEventFacility { 30 SysBusDevice parent_obj; 31 SCLPEventsBus sbus; 32 /* guest's receive mask */ 33 sccb_mask_t receive_mask; 34 /* 35 * when false, we keep the same broken, backwards compatible behaviour as 36 * before, allowing only masks of size exactly 4; when true, we implement 37 * the architecture correctly, allowing all valid mask sizes. Needed for 38 * migration toward older versions. 39 */ 40 bool allow_all_mask_sizes; 41 /* length of the receive mask */ 42 uint16_t mask_length; 43 }; 44 45 /* return true if any child has event pending set */ 46 static bool event_pending(SCLPEventFacility *ef) 47 { 48 BusChild *kid; 49 SCLPEvent *event; 50 SCLPEventClass *event_class; 51 52 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) { 53 DeviceState *qdev = kid->child; 54 event = DO_UPCAST(SCLPEvent, qdev, qdev); 55 event_class = SCLP_EVENT_GET_CLASS(event); 56 if (event->event_pending && 57 event_class->get_send_mask() & ef->receive_mask) { 58 return true; 59 } 60 } 61 return false; 62 } 63 64 static sccb_mask_t get_host_send_mask(SCLPEventFacility *ef) 65 { 66 sccb_mask_t mask; 67 BusChild *kid; 68 SCLPEventClass *child; 69 70 mask = 0; 71 72 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) { 73 DeviceState *qdev = kid->child; 74 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev); 75 mask |= child->get_send_mask(); 76 } 77 return mask; 78 } 79 80 static sccb_mask_t get_host_receive_mask(SCLPEventFacility *ef) 81 { 82 sccb_mask_t mask; 83 BusChild *kid; 84 SCLPEventClass *child; 85 86 mask = 0; 87 88 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) { 89 DeviceState *qdev = kid->child; 90 child = SCLP_EVENT_GET_CLASS((SCLPEvent *) qdev); 91 mask |= child->get_receive_mask(); 92 } 93 return mask; 94 } 95 96 static uint16_t write_event_length_check(SCCB *sccb) 97 { 98 int slen; 99 unsigned elen = 0; 100 EventBufferHeader *event; 101 WriteEventData *wed = (WriteEventData *) sccb; 102 103 event = (EventBufferHeader *) &wed->ebh; 104 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) { 105 elen = be16_to_cpu(event->length); 106 if (elen < sizeof(*event) || elen > slen) { 107 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR; 108 } 109 event = (void *) event + elen; 110 } 111 if (slen) { 112 return SCLP_RC_INCONSISTENT_LENGTHS; 113 } 114 return SCLP_RC_NORMAL_COMPLETION; 115 } 116 117 static uint16_t handle_write_event_buf(SCLPEventFacility *ef, 118 EventBufferHeader *event_buf, SCCB *sccb) 119 { 120 uint16_t rc; 121 BusChild *kid; 122 SCLPEvent *event; 123 SCLPEventClass *ec; 124 125 rc = SCLP_RC_INVALID_FUNCTION; 126 127 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) { 128 DeviceState *qdev = kid->child; 129 event = (SCLPEvent *) qdev; 130 ec = SCLP_EVENT_GET_CLASS(event); 131 132 if (ec->write_event_data && 133 ec->can_handle_event(event_buf->type)) { 134 rc = ec->write_event_data(event, event_buf); 135 break; 136 } 137 } 138 return rc; 139 } 140 141 static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb) 142 { 143 uint16_t rc; 144 int slen; 145 unsigned elen = 0; 146 EventBufferHeader *event_buf; 147 WriteEventData *wed = (WriteEventData *) sccb; 148 149 event_buf = &wed->ebh; 150 rc = SCLP_RC_NORMAL_COMPLETION; 151 152 /* loop over all contained event buffers */ 153 for (slen = sccb_data_len(sccb); slen > 0; slen -= elen) { 154 elen = be16_to_cpu(event_buf->length); 155 156 /* in case of a previous error mark all trailing buffers 157 * as not accepted */ 158 if (rc != SCLP_RC_NORMAL_COMPLETION) { 159 event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED); 160 } else { 161 rc = handle_write_event_buf(ef, event_buf, sccb); 162 } 163 event_buf = (void *) event_buf + elen; 164 } 165 return rc; 166 } 167 168 static void write_event_data(SCLPEventFacility *ef, SCCB *sccb) 169 { 170 if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) { 171 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION); 172 goto out; 173 } 174 if (be16_to_cpu(sccb->h.length) < 8) { 175 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); 176 goto out; 177 } 178 /* first do a sanity check of the write events */ 179 sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb)); 180 181 /* if no early error, then execute */ 182 if (sccb->h.response_code == be16_to_cpu(SCLP_RC_NORMAL_COMPLETION)) { 183 sccb->h.response_code = 184 cpu_to_be16(handle_sccb_write_events(ef, sccb)); 185 } 186 187 out: 188 return; 189 } 190 191 static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb, 192 sccb_mask_t mask) 193 { 194 uint16_t rc; 195 int slen; 196 unsigned elen; 197 BusChild *kid; 198 SCLPEvent *event; 199 SCLPEventClass *ec; 200 EventBufferHeader *event_buf; 201 ReadEventData *red = (ReadEventData *) sccb; 202 203 event_buf = &red->ebh; 204 event_buf->length = 0; 205 slen = sizeof(sccb->data); 206 207 rc = SCLP_RC_NO_EVENT_BUFFERS_STORED; 208 209 QTAILQ_FOREACH(kid, &ef->sbus.qbus.children, sibling) { 210 DeviceState *qdev = kid->child; 211 event = (SCLPEvent *) qdev; 212 ec = SCLP_EVENT_GET_CLASS(event); 213 214 if (mask & ec->get_send_mask()) { 215 if (ec->read_event_data(event, event_buf, &slen)) { 216 elen = be16_to_cpu(event_buf->length); 217 event_buf = (EventBufferHeader *) ((char *)event_buf + elen); 218 rc = SCLP_RC_NORMAL_COMPLETION; 219 } 220 } 221 } 222 223 if (sccb->h.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE) { 224 /* architecture suggests to reset variable-length-response bit */ 225 sccb->h.control_mask[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE; 226 /* with a new length value */ 227 sccb->h.length = cpu_to_be16(SCCB_SIZE - slen); 228 } 229 return rc; 230 } 231 232 /* copy up to src_len bytes and fill the rest of dst with zeroes */ 233 static void copy_mask(uint8_t *dst, uint8_t *src, uint16_t dst_len, 234 uint16_t src_len) 235 { 236 int i; 237 238 for (i = 0; i < dst_len; i++) { 239 dst[i] = i < src_len ? src[i] : 0; 240 } 241 } 242 243 static void read_event_data(SCLPEventFacility *ef, SCCB *sccb) 244 { 245 sccb_mask_t sclp_active_selection_mask; 246 sccb_mask_t sclp_cp_receive_mask; 247 248 ReadEventData *red = (ReadEventData *) sccb; 249 250 if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) { 251 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); 252 goto out; 253 } 254 255 sclp_cp_receive_mask = ef->receive_mask; 256 257 /* get active selection mask */ 258 switch (sccb->h.function_code) { 259 case SCLP_UNCONDITIONAL_READ: 260 sclp_active_selection_mask = sclp_cp_receive_mask; 261 break; 262 case SCLP_SELECTIVE_READ: 263 copy_mask((uint8_t *)&sclp_active_selection_mask, (uint8_t *)&red->mask, 264 sizeof(sclp_active_selection_mask), ef->mask_length); 265 sclp_active_selection_mask = be32_to_cpu(sclp_active_selection_mask); 266 if (!sclp_cp_receive_mask || 267 (sclp_active_selection_mask & ~sclp_cp_receive_mask)) { 268 sccb->h.response_code = 269 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK); 270 goto out; 271 } 272 break; 273 default: 274 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION); 275 goto out; 276 } 277 sccb->h.response_code = cpu_to_be16( 278 handle_sccb_read_events(ef, sccb, sclp_active_selection_mask)); 279 280 out: 281 return; 282 } 283 284 static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb) 285 { 286 WriteEventMask *we_mask = (WriteEventMask *) sccb; 287 uint16_t mask_length = be16_to_cpu(we_mask->mask_length); 288 sccb_mask_t tmp_mask; 289 290 if (!mask_length || (mask_length > SCLP_EVENT_MASK_LEN_MAX) || 291 ((mask_length != 4) && !ef->allow_all_mask_sizes)) { 292 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH); 293 goto out; 294 } 295 296 /* 297 * Note: We currently only support masks up to 4 byte length; 298 * the remainder is filled up with zeroes. Linux uses 299 * a 4 byte mask length. 300 */ 301 302 /* keep track of the guest's capability masks */ 303 copy_mask((uint8_t *)&tmp_mask, WEM_CP_RECEIVE_MASK(we_mask, mask_length), 304 sizeof(tmp_mask), mask_length); 305 ef->receive_mask = be32_to_cpu(tmp_mask); 306 307 /* return the SCLP's capability masks to the guest */ 308 tmp_mask = cpu_to_be32(get_host_receive_mask(ef)); 309 copy_mask(WEM_RECEIVE_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask, 310 mask_length, sizeof(tmp_mask)); 311 tmp_mask = cpu_to_be32(get_host_send_mask(ef)); 312 copy_mask(WEM_SEND_MASK(we_mask, mask_length), (uint8_t *)&tmp_mask, 313 mask_length, sizeof(tmp_mask)); 314 315 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION); 316 ef->mask_length = mask_length; 317 318 out: 319 return; 320 } 321 322 /* qemu object creation and initialization functions */ 323 324 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus" 325 326 static void sclp_events_bus_realize(BusState *bus, Error **errp) 327 { 328 BusChild *kid; 329 330 /* TODO: recursive realization has to be done in common code */ 331 QTAILQ_FOREACH(kid, &bus->children, sibling) { 332 DeviceState *dev = kid->child; 333 334 object_property_set_bool(OBJECT(dev), true, "realized", errp); 335 if (*errp) { 336 return; 337 } 338 } 339 } 340 341 static void sclp_events_bus_class_init(ObjectClass *klass, void *data) 342 { 343 BusClass *bc = BUS_CLASS(klass); 344 345 bc->realize = sclp_events_bus_realize; 346 } 347 348 static const TypeInfo sclp_events_bus_info = { 349 .name = TYPE_SCLP_EVENTS_BUS, 350 .parent = TYPE_BUS, 351 .class_init = sclp_events_bus_class_init, 352 }; 353 354 static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code) 355 { 356 switch (code & SCLP_CMD_CODE_MASK) { 357 case SCLP_CMD_READ_EVENT_DATA: 358 read_event_data(ef, sccb); 359 break; 360 case SCLP_CMD_WRITE_EVENT_DATA: 361 write_event_data(ef, sccb); 362 break; 363 case SCLP_CMD_WRITE_EVENT_MASK: 364 write_event_mask(ef, sccb); 365 break; 366 default: 367 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); 368 break; 369 } 370 } 371 372 static bool vmstate_event_facility_mask_length_needed(void *opaque) 373 { 374 SCLPEventFacility *ef = opaque; 375 376 return ef->allow_all_mask_sizes; 377 } 378 379 static const VMStateDescription vmstate_event_facility_mask_length = { 380 .name = "vmstate-event-facility/mask_length", 381 .version_id = 0, 382 .minimum_version_id = 0, 383 .needed = vmstate_event_facility_mask_length_needed, 384 .fields = (VMStateField[]) { 385 VMSTATE_UINT16(mask_length, SCLPEventFacility), 386 VMSTATE_END_OF_LIST() 387 } 388 }; 389 390 static const VMStateDescription vmstate_event_facility = { 391 .name = "vmstate-event-facility", 392 .version_id = 0, 393 .minimum_version_id = 0, 394 .fields = (VMStateField[]) { 395 VMSTATE_UINT32(receive_mask, SCLPEventFacility), 396 VMSTATE_END_OF_LIST() 397 }, 398 .subsections = (const VMStateDescription * []) { 399 &vmstate_event_facility_mask_length, 400 NULL 401 } 402 }; 403 404 static void sclp_event_set_allow_all_mask_sizes(Object *obj, bool value, 405 Error **errp) 406 { 407 SCLPEventFacility *ef = (SCLPEventFacility *)obj; 408 409 ef->allow_all_mask_sizes = value; 410 } 411 412 static bool sclp_event_get_allow_all_mask_sizes(Object *obj, Error **e) 413 { 414 SCLPEventFacility *ef = (SCLPEventFacility *)obj; 415 416 return ef->allow_all_mask_sizes; 417 } 418 419 static void init_event_facility(Object *obj) 420 { 421 SCLPEventFacility *event_facility = EVENT_FACILITY(obj); 422 DeviceState *sdev = DEVICE(obj); 423 Object *new; 424 425 event_facility->mask_length = 4; 426 event_facility->allow_all_mask_sizes = true; 427 object_property_add_bool(obj, "allow_all_mask_sizes", 428 sclp_event_get_allow_all_mask_sizes, 429 sclp_event_set_allow_all_mask_sizes, NULL); 430 /* Spawn a new bus for SCLP events */ 431 qbus_create_inplace(&event_facility->sbus, sizeof(event_facility->sbus), 432 TYPE_SCLP_EVENTS_BUS, sdev, NULL); 433 434 new = object_new(TYPE_SCLP_QUIESCE); 435 object_property_add_child(obj, TYPE_SCLP_QUIESCE, new, NULL); 436 object_unref(new); 437 qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus); 438 439 new = object_new(TYPE_SCLP_CPU_HOTPLUG); 440 object_property_add_child(obj, TYPE_SCLP_CPU_HOTPLUG, new, NULL); 441 object_unref(new); 442 qdev_set_parent_bus(DEVICE(new), &event_facility->sbus.qbus); 443 /* the facility will automatically realize the devices via the bus */ 444 } 445 446 static void reset_event_facility(DeviceState *dev) 447 { 448 SCLPEventFacility *sdev = EVENT_FACILITY(dev); 449 450 sdev->receive_mask = 0; 451 } 452 453 static void init_event_facility_class(ObjectClass *klass, void *data) 454 { 455 SysBusDeviceClass *sbdc = SYS_BUS_DEVICE_CLASS(klass); 456 DeviceClass *dc = DEVICE_CLASS(sbdc); 457 SCLPEventFacilityClass *k = EVENT_FACILITY_CLASS(dc); 458 459 dc->reset = reset_event_facility; 460 dc->vmsd = &vmstate_event_facility; 461 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 462 k->command_handler = command_handler; 463 k->event_pending = event_pending; 464 } 465 466 static const TypeInfo sclp_event_facility_info = { 467 .name = TYPE_SCLP_EVENT_FACILITY, 468 .parent = TYPE_SYS_BUS_DEVICE, 469 .instance_init = init_event_facility, 470 .instance_size = sizeof(SCLPEventFacility), 471 .class_init = init_event_facility_class, 472 .class_size = sizeof(SCLPEventFacilityClass), 473 }; 474 475 static void event_realize(DeviceState *qdev, Error **errp) 476 { 477 SCLPEvent *event = SCLP_EVENT(qdev); 478 SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event); 479 480 if (child->init) { 481 int rc = child->init(event); 482 if (rc < 0) { 483 error_setg(errp, "SCLP event initialization failed."); 484 return; 485 } 486 } 487 } 488 489 static void event_class_init(ObjectClass *klass, void *data) 490 { 491 DeviceClass *dc = DEVICE_CLASS(klass); 492 493 dc->bus_type = TYPE_SCLP_EVENTS_BUS; 494 dc->realize = event_realize; 495 } 496 497 static const TypeInfo sclp_event_type_info = { 498 .name = TYPE_SCLP_EVENT, 499 .parent = TYPE_DEVICE, 500 .instance_size = sizeof(SCLPEvent), 501 .class_init = event_class_init, 502 .class_size = sizeof(SCLPEventClass), 503 .abstract = true, 504 }; 505 506 static void register_types(void) 507 { 508 type_register_static(&sclp_events_bus_info); 509 type_register_static(&sclp_event_facility_info); 510 type_register_static(&sclp_event_type_info); 511 } 512 513 type_init(register_types) 514