xref: /openbmc/qemu/hw/s390x/event-facility.c (revision 81dee729c1a8fccaab8cd978721acca0282f43c9)
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 "monitor.h"
19 #include "sysemu.h"
20 
21 #include "sclp.h"
22 #include "event-facility.h"
23 
24 typedef struct EventTypesBus {
25     BusState qbus;
26 } EventTypesBus;
27 
28 struct SCLPEventFacility {
29     EventTypesBus sbus;
30     DeviceState *qdev;
31     /* guest' receive mask */
32     unsigned int receive_mask;
33 };
34 
35 /* return true if any child has event pending set */
36 static bool event_pending(SCLPEventFacility *ef)
37 {
38     BusChild *kid;
39     SCLPEvent *event;
40     SCLPEventClass *event_class;
41 
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) {
48             return true;
49         }
50     }
51     return false;
52 }
53 
54 static unsigned int get_host_send_mask(SCLPEventFacility *ef)
55 {
56     unsigned int mask;
57     BusChild *kid;
58     SCLPEventClass *child;
59 
60     mask = 0;
61 
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();
66     }
67     return mask;
68 }
69 
70 static unsigned int get_host_receive_mask(SCLPEventFacility *ef)
71 {
72     unsigned int mask;
73     BusChild *kid;
74     SCLPEventClass *child;
75 
76     mask = 0;
77 
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();
82     }
83     return mask;
84 }
85 
86 static uint16_t write_event_length_check(SCCB *sccb)
87 {
88     int slen;
89     unsigned elen = 0;
90     EventBufferHeader *event;
91     WriteEventData *wed = (WriteEventData *) sccb;
92 
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;
98         }
99         event = (void *) event + elen;
100     }
101     if (slen) {
102         return SCLP_RC_INCONSISTENT_LENGTHS;
103     }
104     return SCLP_RC_NORMAL_COMPLETION;
105 }
106 
107 static uint16_t handle_write_event_buf(SCLPEventFacility *ef,
108                                        EventBufferHeader *event_buf, SCCB *sccb)
109 {
110     uint16_t rc;
111     BusChild *kid;
112     SCLPEvent *event;
113     SCLPEventClass *ec;
114 
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);
119 
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);
124             break;
125         }
126     }
127     return rc;
128 }
129 
130 static uint16_t handle_sccb_write_events(SCLPEventFacility *ef, SCCB *sccb)
131 {
132     uint16_t rc;
133     int slen;
134     unsigned elen = 0;
135     EventBufferHeader *event_buf;
136     WriteEventData *wed = (WriteEventData *) sccb;
137 
138     event_buf = &wed->ebh;
139     rc = SCLP_RC_NORMAL_COMPLETION;
140 
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);
144 
145         /* in case of a previous error mark all trailing buffers
146          * as not accepted */
147         if (rc != SCLP_RC_NORMAL_COMPLETION) {
148             event_buf->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
149         } else {
150             rc = handle_write_event_buf(ef, event_buf, sccb);
151         }
152         event_buf = (void *) event_buf + elen;
153     }
154     return rc;
155 }
156 
157 static void write_event_data(SCLPEventFacility *ef, SCCB *sccb)
158 {
159     if (sccb->h.function_code != SCLP_FC_NORMAL_WRITE) {
160         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
161         goto out;
162     }
163     if (be16_to_cpu(sccb->h.length) < 8) {
164         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
165         goto out;
166     }
167     /* first do a sanity check of the write events */
168     sccb->h.response_code = cpu_to_be16(write_event_length_check(sccb));
169 
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));
174     }
175 
176 out:
177     return;
178 }
179 
180 static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb,
181                                         unsigned int mask)
182 {
183     uint16_t rc;
184     int slen;
185     unsigned elen = 0;
186     BusChild *kid;
187     SCLPEvent *event;
188     SCLPEventClass *ec;
189     EventBufferHeader *event_buf;
190     ReadEventData *red = (ReadEventData *) sccb;
191 
192     event_buf = &red->ebh;
193     event_buf->length = 0;
194     slen = sizeof(sccb->data);
195 
196     rc = SCLP_RC_NO_EVENT_BUFFERS_STORED;
197 
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);
202 
203         if (mask & ec->get_send_mask()) {
204             if (ec->read_event_data(event, event_buf, &slen)) {
205                 rc = SCLP_RC_NORMAL_COMPLETION;
206             }
207         }
208         elen = be16_to_cpu(event_buf->length);
209         event_buf = (void *) event_buf + elen;
210     }
211 
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);
217     }
218     return rc;
219 }
220 
221 static void read_event_data(SCLPEventFacility *ef, SCCB *sccb)
222 {
223     unsigned int sclp_active_selection_mask;
224     unsigned int sclp_cp_receive_mask;
225 
226     ReadEventData *red = (ReadEventData *) sccb;
227 
228     if (be16_to_cpu(sccb->h.length) != SCCB_SIZE) {
229         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
230         goto out;
231     }
232 
233     sclp_cp_receive_mask = ef->receive_mask;
234 
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;
239         break;
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);
244             goto out;
245         }
246         sclp_active_selection_mask = be32_to_cpu(red->mask);
247         break;
248     default:
249         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_FUNCTION);
250         goto out;
251     }
252     sccb->h.response_code = cpu_to_be16(
253             handle_sccb_read_events(ef, sccb, sclp_active_selection_mask));
254 
255 out:
256     return;
257 }
258 
259 static void write_event_mask(SCLPEventFacility *ef, SCCB *sccb)
260 {
261     WriteEventMask *we_mask = (WriteEventMask *) sccb;
262 
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);
267         goto out;
268     }
269 
270     /* keep track of the guest's capability masks */
271     ef->receive_mask = be32_to_cpu(we_mask->cp_receive_mask);
272 
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));
276 
277     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
278 
279 out:
280     return;
281 }
282 
283 /* qemu object creation and initialization functions */
284 
285 #define TYPE_SCLP_EVENTS_BUS "s390-sclp-events-bus"
286 
287 static void sclp_events_bus_class_init(ObjectClass *klass, void *data)
288 {
289 }
290 
291 static const TypeInfo s390_sclp_events_bus_info = {
292     .name = TYPE_SCLP_EVENTS_BUS,
293     .parent = TYPE_BUS,
294     .class_init = sclp_events_bus_class_init,
295 };
296 
297 static void command_handler(SCLPEventFacility *ef, SCCB *sccb, uint64_t code)
298 {
299     switch (code) {
300     case SCLP_CMD_READ_EVENT_DATA:
301         read_event_data(ef, sccb);
302         break;
303     case SCLP_CMD_WRITE_EVENT_DATA:
304         write_event_data(ef, sccb);
305         break;
306     case SCLP_CMD_WRITE_EVENT_MASK:
307         write_event_mask(ef, sccb);
308         break;
309     default:
310         sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
311         break;
312     }
313 }
314 
315 static int init_event_facility(S390SCLPDevice *sdev)
316 {
317     SCLPEventFacility *event_facility;
318     DeviceState *quiesce;
319 
320     event_facility = g_malloc0(sizeof(SCLPEventFacility));
321     sdev->ef = event_facility;
322     sdev->sclp_command_handler = command_handler;
323     sdev->event_pending = event_pending;
324 
325     /* Spawn a new sclp-events facility */
326     qbus_create_inplace(&event_facility->sbus.qbus,
327                         TYPE_SCLP_EVENTS_BUS, (DeviceState *)sdev, NULL);
328     event_facility->sbus.qbus.allow_hotplug = 0;
329     event_facility->qdev = (DeviceState *) sdev;
330 
331     quiesce = qdev_create(&event_facility->sbus.qbus, "sclpquiesce");
332     if (!quiesce) {
333         return -1;
334     }
335     qdev_init_nofail(quiesce);
336 
337     return 0;
338 }
339 
340 static void init_event_facility_class(ObjectClass *klass, void *data)
341 {
342     S390SCLPDeviceClass *k = SCLP_S390_DEVICE_CLASS(klass);
343 
344     k->init = init_event_facility;
345 }
346 
347 static TypeInfo s390_sclp_event_facility_info = {
348     .name          = "s390-sclp-event-facility",
349     .parent        = TYPE_DEVICE_S390_SCLP,
350     .instance_size = sizeof(S390SCLPDevice),
351     .class_init    = init_event_facility_class,
352 };
353 
354 static int event_qdev_init(DeviceState *qdev)
355 {
356     SCLPEvent *event = DO_UPCAST(SCLPEvent, qdev, qdev);
357     SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
358 
359     return child->init(event);
360 }
361 
362 static int event_qdev_exit(DeviceState *qdev)
363 {
364     SCLPEvent *event = DO_UPCAST(SCLPEvent, qdev, qdev);
365     SCLPEventClass *child = SCLP_EVENT_GET_CLASS(event);
366     if (child->exit) {
367         child->exit(event);
368     }
369     return 0;
370 }
371 
372 static void event_class_init(ObjectClass *klass, void *data)
373 {
374     DeviceClass *dc = DEVICE_CLASS(klass);
375 
376     dc->bus_type = TYPE_SCLP_EVENTS_BUS;
377     dc->unplug = qdev_simple_unplug_cb;
378     dc->init = event_qdev_init;
379     dc->exit = event_qdev_exit;
380 }
381 
382 static TypeInfo s390_sclp_event_type_info = {
383     .name = TYPE_SCLP_EVENT,
384     .parent = TYPE_DEVICE,
385     .instance_size = sizeof(SCLPEvent),
386     .class_init = event_class_init,
387     .class_size = sizeof(SCLPEventClass),
388     .abstract = true,
389 };
390 
391 static void register_types(void)
392 {
393     type_register_static(&s390_sclp_events_bus_info);
394     type_register_static(&s390_sclp_event_facility_info);
395     type_register_static(&s390_sclp_event_type_info);
396 }
397 
398 type_init(register_types)
399