4 * handles SCLP event types
5 * - Signal Quiesce - system power down
6 * - ASCII Console Data - VT220 read and write
8 * Copyright IBM, Corp. 2012
11 * Heinz Graalfs <graalfs@de.ibm.com>
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.
22 #include "event-facility.h"
24 typedef struct EventTypesBus
{
28 struct SCLPEventFacility
{
31 /* guest' receive mask */
32 unsigned int receive_mask
;
35 /* return true if any child has event pending set */
36 static bool event_pending(SCLPEventFacility
*ef
)
40 SCLPEventClass
*event_class
;
42 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
43 DeviceState
*qdev
= kid
->child
;
44 event
= DO_UPCAST(SCLPEvent
, qdev
, qdev
);
45 event_class
= SCLP_EVENT_GET_CLASS(event
);
46 if (event
->event_pending
&&
47 event_class
->get_send_mask() & ef
->receive_mask
) {
54 static unsigned int get_host_send_mask(SCLPEventFacility
*ef
)
58 SCLPEventClass
*child
;
62 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
63 DeviceState
*qdev
= kid
->child
;
64 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
65 mask
|= child
->get_send_mask();
70 static unsigned int get_host_receive_mask(SCLPEventFacility
*ef
)
74 SCLPEventClass
*child
;
78 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
79 DeviceState
*qdev
= kid
->child
;
80 child
= SCLP_EVENT_GET_CLASS((SCLPEvent
*) qdev
);
81 mask
|= child
->get_receive_mask();
86 static uint16_t write_event_length_check(SCCB
*sccb
)
90 EventBufferHeader
*event
;
91 WriteEventData
*wed
= (WriteEventData
*) sccb
;
93 event
= (EventBufferHeader
*) &wed
->ebh
;
94 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
95 elen
= be16_to_cpu(event
->length
);
96 if (elen
< sizeof(*event
) || elen
> slen
) {
97 return SCLP_RC_EVENT_BUFFER_SYNTAX_ERROR
;
99 event
= (void *) event
+ elen
;
102 return SCLP_RC_INCONSISTENT_LENGTHS
;
104 return SCLP_RC_NORMAL_COMPLETION
;
107 static uint16_t handle_write_event_buf(SCLPEventFacility
*ef
,
108 EventBufferHeader
*event_buf
, SCCB
*sccb
)
115 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
116 DeviceState
*qdev
= kid
->child
;
117 event
= (SCLPEvent
*) qdev
;
118 ec
= SCLP_EVENT_GET_CLASS(event
);
120 rc
= SCLP_RC_INVALID_FUNCTION
;
121 if (ec
->write_event_data
&&
122 ec
->event_type() == event_buf
->type
) {
123 rc
= ec
->write_event_data(event
, event_buf
);
130 static uint16_t handle_sccb_write_events(SCLPEventFacility
*ef
, SCCB
*sccb
)
135 EventBufferHeader
*event_buf
;
136 WriteEventData
*wed
= (WriteEventData
*) sccb
;
138 event_buf
= &wed
->ebh
;
139 rc
= SCLP_RC_NORMAL_COMPLETION
;
141 /* loop over all contained event buffers */
142 for (slen
= sccb_data_len(sccb
); slen
> 0; slen
-= elen
) {
143 elen
= be16_to_cpu(event_buf
->length
);
145 /* in case of a previous error mark all trailing buffers
147 if (rc
!= SCLP_RC_NORMAL_COMPLETION
) {
148 event_buf
->flags
&= ~(SCLP_EVENT_BUFFER_ACCEPTED
);
150 rc
= handle_write_event_buf(ef
, event_buf
, sccb
);
152 event_buf
= (void *) event_buf
+ elen
;
157 static void write_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
159 if (sccb
->h
.function_code
!= SCLP_FC_NORMAL_WRITE
) {
160 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
163 if (be16_to_cpu(sccb
->h
.length
) < 8) {
164 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
167 /* first do a sanity check of the write events */
168 sccb
->h
.response_code
= cpu_to_be16(write_event_length_check(sccb
));
170 /* if no early error, then execute */
171 if (sccb
->h
.response_code
== be16_to_cpu(SCLP_RC_NORMAL_COMPLETION
)) {
172 sccb
->h
.response_code
=
173 cpu_to_be16(handle_sccb_write_events(ef
, sccb
));
180 static uint16_t handle_sccb_read_events(SCLPEventFacility
*ef
, SCCB
*sccb
,
189 EventBufferHeader
*event_buf
;
190 ReadEventData
*red
= (ReadEventData
*) sccb
;
192 event_buf
= &red
->ebh
;
193 event_buf
->length
= 0;
194 slen
= sizeof(sccb
->data
);
196 rc
= SCLP_RC_NO_EVENT_BUFFERS_STORED
;
198 QTAILQ_FOREACH(kid
, &ef
->sbus
.qbus
.children
, sibling
) {
199 DeviceState
*qdev
= kid
->child
;
200 event
= (SCLPEvent
*) qdev
;
201 ec
= SCLP_EVENT_GET_CLASS(event
);
203 if (mask
& ec
->get_send_mask()) {
204 if (ec
->read_event_data(event
, event_buf
, &slen
)) {
205 rc
= SCLP_RC_NORMAL_COMPLETION
;
208 elen
= be16_to_cpu(event_buf
->length
);
209 event_buf
= (void *) event_buf
+ elen
;
212 if (sccb
->h
.control_mask
[2] & SCLP_VARIABLE_LENGTH_RESPONSE
) {
213 /* architecture suggests to reset variable-length-response bit */
214 sccb
->h
.control_mask
[2] &= ~SCLP_VARIABLE_LENGTH_RESPONSE
;
215 /* with a new length value */
216 sccb
->h
.length
= cpu_to_be16(SCCB_SIZE
- slen
);
221 static void read_event_data(SCLPEventFacility
*ef
, SCCB
*sccb
)
223 unsigned int sclp_active_selection_mask
;
224 unsigned int sclp_cp_receive_mask
;
226 ReadEventData
*red
= (ReadEventData
*) sccb
;
228 if (be16_to_cpu(sccb
->h
.length
) != SCCB_SIZE
) {
229 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
233 sclp_cp_receive_mask
= ef
->receive_mask
;
235 /* get active selection mask */
236 switch (sccb
->h
.function_code
) {
237 case SCLP_UNCONDITIONAL_READ
:
238 sclp_active_selection_mask
= sclp_cp_receive_mask
;
240 case SCLP_SELECTIVE_READ
:
241 if (!(sclp_cp_receive_mask
& be32_to_cpu(red
->mask
))) {
242 sccb
->h
.response_code
=
243 cpu_to_be16(SCLP_RC_INVALID_SELECTION_MASK
);
246 sclp_active_selection_mask
= be32_to_cpu(red
->mask
);
249 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_FUNCTION
);
252 sccb
->h
.response_code
= cpu_to_be16(
253 handle_sccb_read_events(ef
, sccb
, sclp_active_selection_mask
));
259 static void write_event_mask(SCLPEventFacility
*ef
, SCCB
*sccb
)
261 WriteEventMask
*we_mask
= (WriteEventMask
*) sccb
;
263 /* Attention: We assume that Linux uses 4-byte masks, what it actually
264 does. Architecture allows for masks of variable size, though */
265 if (be16_to_cpu(we_mask
->mask_length
) != 4) {
266 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_MASK_LENGTH
);
270 /* keep track of the guest's capability masks */
271 ef
->receive_mask
= be32_to_cpu(we_mask
->cp_receive_mask
);
273 /* return the SCLP's capability masks to the guest */
274 we_mask
->send_mask
= cpu_to_be32(get_host_send_mask(ef
));
275 we_mask
->receive_mask
= cpu_to_be32(get_host_receive_mask(ef
));
277 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
283 /* qemu object creation and initialization functions */
285 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
287 static void sclp_events_bus_class_init(ObjectClass
*klass
, void *data
)
291 static const TypeInfo s390_sclp_events_bus_info
= {
292 .name
= TYPE_SCLP_EVENTS_BUS
,
294 .class_init
= sclp_events_bus_class_init
,
297 static void command_handler(SCLPEventFacility
*ef
, SCCB
*sccb
, uint64_t code
)
300 case SCLP_CMD_READ_EVENT_DATA
:
301 read_event_data(ef
, sccb
);
303 case SCLP_CMD_WRITE_EVENT_DATA
:
304 write_event_data(ef
, sccb
);
306 case SCLP_CMD_WRITE_EVENT_MASK
:
307 write_event_mask(ef
, sccb
);
310 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
315 static int init_event_facility(S390SCLPDevice
*sdev
)
317 SCLPEventFacility
*event_facility
;
319 event_facility
= g_malloc0(sizeof(SCLPEventFacility
));
320 sdev
->ef
= event_facility
;
321 sdev
->sclp_command_handler
= command_handler
;
322 sdev
->event_pending
= event_pending
;
324 /* Spawn a new sclp-events facility */
325 qbus_create_inplace(&event_facility
->sbus
.qbus
,
326 TYPE_SCLP_EVENTS_BUS
, (DeviceState
*)sdev
, NULL
);
327 event_facility
->sbus
.qbus
.allow_hotplug
= 0;
328 event_facility
->qdev
= (DeviceState
*) sdev
;
333 static void init_event_facility_class(ObjectClass
*klass
, void *data
)
335 S390SCLPDeviceClass
*k
= SCLP_S390_DEVICE_CLASS(klass
);
337 k
->init
= init_event_facility
;
340 static TypeInfo s390_sclp_event_facility_info
= {
341 .name
= "s390-sclp-event-facility",
342 .parent
= TYPE_DEVICE_S390_SCLP
,
343 .instance_size
= sizeof(S390SCLPDevice
),
344 .class_init
= init_event_facility_class
,
347 static int event_qdev_init(DeviceState
*qdev
)
349 SCLPEvent
*event
= DO_UPCAST(SCLPEvent
, qdev
, qdev
);
350 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
352 return child
->init(event
);
355 static int event_qdev_exit(DeviceState
*qdev
)
357 SCLPEvent
*event
= DO_UPCAST(SCLPEvent
, qdev
, qdev
);
358 SCLPEventClass
*child
= SCLP_EVENT_GET_CLASS(event
);
365 static void event_class_init(ObjectClass
*klass
, void *data
)
367 DeviceClass
*dc
= DEVICE_CLASS(klass
);
369 dc
->bus_type
= TYPE_SCLP_EVENTS_BUS
;
370 dc
->unplug
= qdev_simple_unplug_cb
;
371 dc
->init
= event_qdev_init
;
372 dc
->exit
= event_qdev_exit
;
375 static TypeInfo s390_sclp_event_type_info
= {
376 .name
= TYPE_SCLP_EVENT
,
377 .parent
= TYPE_DEVICE
,
378 .instance_size
= sizeof(SCLPEvent
),
379 .class_init
= event_class_init
,
380 .class_size
= sizeof(SCLPEventClass
),
384 static void register_types(void)
386 type_register_static(&s390_sclp_events_bus_info
);
387 type_register_static(&s390_sclp_event_facility_info
);
388 type_register_static(&s390_sclp_event_type_info
);
391 type_init(register_types
)