3 * Operations Command - Line Mode input
4 * Message - Line Mode output
6 * Copyright IBM, Corp. 2013
9 * Heinz Graalfs <graalfs@linux.vnet.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
12 * option) any later version. See the COPYING file in the top-level directory.
17 #include "qemu/thread.h"
18 #include "qemu/error-report.h"
19 #include "sysemu/char.h"
21 #include "hw/s390x/sclp.h"
22 #include "hw/s390x/event-facility.h"
23 #include "hw/s390x/ebcdic.h"
25 #define SIZE_BUFFER 4096
28 typedef struct OprtnsCommand
{
29 EventBufferHeader header
;
32 } QEMU_PACKED OprtnsCommand
;
34 /* max size for line-mode data in 4K SCCB page */
35 #define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand))
37 typedef struct SCLPConsoleLM
{
40 bool echo
; /* immediate echo of input if true */
41 uint32_t write_errors
; /* errors writing to char layer */
42 uint32_t length
; /* length of byte stream in buffer */
43 uint8_t buf
[SIZE_CONSOLE_BUFFER
];
44 qemu_irq irq_console_read
;
48 * Character layer call-back functions
50 * Allow 1 character at a time
52 * Accumulate bytes from character layer in console buffer,
53 * event_pending is set when a newline character is encountered
55 * The maximum command line length is limited by the maximum
56 * space available in an SCCB
59 static int chr_can_read(void *opaque
)
61 SCLPConsoleLM
*scon
= opaque
;
63 if (scon
->event
.event_pending
) {
65 } else if (SIZE_CONSOLE_BUFFER
- scon
->length
) {
71 static void receive_from_chr_layer(SCLPConsoleLM
*scon
, const uint8_t *buf
,
76 if (*buf
== '\r' || *buf
== '\n') {
77 scon
->event
.event_pending
= true;
80 scon
->buf
[scon
->length
] = *buf
;
83 qemu_chr_fe_write(scon
->chr
, buf
, size
);
88 * Send data from a char device over to the guest
90 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
92 SCLPConsoleLM
*scon
= opaque
;
94 receive_from_chr_layer(scon
, buf
, size
);
95 if (scon
->event
.event_pending
) {
96 /* trigger SCLP read operation */
97 qemu_irq_raise(scon
->irq_console_read
);
101 /* functions to be called by event facility */
103 static bool can_handle_event(uint8_t type
)
105 return type
== SCLP_EVENT_MESSAGE
|| type
== SCLP_EVENT_PMSGCMD
;
108 static unsigned int send_mask(void)
110 return SCLP_EVENT_MASK_OP_CMD
| SCLP_EVENT_MASK_PMSGCMD
;
113 static unsigned int receive_mask(void)
115 return SCLP_EVENT_MASK_MSG
| SCLP_EVENT_MASK_PMSGCMD
;
119 * Triggered by SCLP's read_event_data
120 * - convert ASCII byte stream to EBCDIC and
121 * - copy converted data into provided (SCLP) buffer
123 static int get_console_data(SCLPEvent
*event
, uint8_t *buf
, size_t *size
,
128 SCLPConsoleLM
*cons
= DO_UPCAST(SCLPConsoleLM
, event
, event
);
131 /* data need to fit into provided SCLP buffer */
136 ebcdic_put(buf
, (char *)&cons
->buf
, len
);
139 /* data provided and no more data pending */
140 event
->event_pending
= false;
144 static int read_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
,
150 OprtnsCommand
*oc
= (OprtnsCommand
*) evt_buf_hdr
;
152 if (!event
->event_pending
) {
153 /* no data pending */
157 to
= (uint8_t *)&oc
->data
;
158 avail
= *slen
- sizeof(OprtnsCommand
);
159 rc
= get_console_data(event
, to
, &src_len
, avail
);
161 /* data didn't fit, try next SCCB */
165 oc
->message_unit
.mdmsu
.gds_id
= GDS_ID_MDSMU
;
166 oc
->message_unit
.mdmsu
.length
= cpu_to_be16(sizeof(struct MDMSU
));
168 oc
->message_unit
.cpmsu
.gds_id
= GDS_ID_CPMSU
;
169 oc
->message_unit
.cpmsu
.length
=
170 cpu_to_be16(sizeof(struct MDMSU
) - sizeof(GdsVector
));
172 oc
->message_unit
.text_command
.gds_id
= GDS_ID_TEXTCMD
;
173 oc
->message_unit
.text_command
.length
=
174 cpu_to_be16(sizeof(struct MDMSU
) - (2 * sizeof(GdsVector
)));
176 oc
->message_unit
.self_def_text_message
.key
= GDS_KEY_SELFDEFTEXTMSG
;
177 oc
->message_unit
.self_def_text_message
.length
=
178 cpu_to_be16(sizeof(struct MDMSU
) - (3 * sizeof(GdsVector
)));
180 oc
->message_unit
.text_message
.key
= GDS_KEY_TEXTMSG
;
181 oc
->message_unit
.text_message
.length
=
182 cpu_to_be16(sizeof(GdsSubvector
) + src_len
);
184 oc
->header
.length
= cpu_to_be16(sizeof(OprtnsCommand
) + src_len
);
185 oc
->header
.type
= SCLP_EVENT_OPRTNS_COMMAND
;
186 *slen
= avail
- src_len
;
192 * Triggered by SCLP's write_event_data
193 * - write console data to character layer
194 * returns < 0 if an error occurred
196 static int write_console_data(SCLPEvent
*event
, const uint8_t *buf
, int len
)
199 const uint8_t *buf_offset
;
201 SCLPConsoleLM
*scon
= DO_UPCAST(SCLPConsoleLM
, event
, event
);
204 /* If there's no backend, we can just say we consumed all data. */
210 ret
= qemu_chr_fe_write(scon
->chr
, buf
, len
);
212 /* a pty doesn't seem to be connected - no error */
214 } else if (ret
== -EAGAIN
|| (ret
> 0 && ret
< len
)) {
225 static int process_mdb(SCLPEvent
*event
, MDBO
*mdbo
)
229 uint8_t buffer
[SIZE_BUFFER
];
231 len
= be16_to_cpu(mdbo
->length
);
232 len
-= sizeof(mdbo
->length
) + sizeof(mdbo
->type
)
233 + sizeof(mdbo
->mto
.line_type_flags
)
234 + sizeof(mdbo
->mto
.alarm_control
)
235 + sizeof(mdbo
->mto
._reserved
);
237 assert(len
<= SIZE_BUFFER
);
239 /* convert EBCDIC SCLP contents to ASCII console message */
240 ascii_put(buffer
, mdbo
->mto
.message
, len
);
241 rc
= write_console_data(event
, (uint8_t *)NEWLINE
, 1);
245 return write_console_data(event
, buffer
, len
);
248 static int write_event_data(SCLPEvent
*event
, EventBufferHeader
*ebh
)
254 SclpMsg
*data
= (SclpMsg
*) ebh
;
255 SCLPConsoleLM
*scon
= DO_UPCAST(SCLPConsoleLM
, event
, event
);
257 len
= be16_to_cpu(data
->mdb
.header
.length
);
258 if (len
< sizeof(data
->mdb
.header
)) {
259 return SCLP_RC_INCONSISTENT_LENGTHS
;
261 len
-= sizeof(data
->mdb
.header
);
263 /* first check message buffers */
264 mdbo
= data
->mdb
.mdbo
;
266 if (be16_to_cpu(mdbo
->length
) > len
267 || be16_to_cpu(mdbo
->length
) == 0) {
268 return SCLP_RC_INCONSISTENT_LENGTHS
;
270 len
-= be16_to_cpu(mdbo
->length
);
271 mdbo
= (void *) mdbo
+ be16_to_cpu(mdbo
->length
);
275 len
= be16_to_cpu(data
->mdb
.header
.length
) - sizeof(data
->mdb
.header
);
276 mdbo
= data
->mdb
.mdbo
;
278 switch (be16_to_cpu(mdbo
->type
)) {
280 /* message text object */
281 written
= process_mdb(event
, mdbo
);
283 /* character layer error */
287 default: /* ignore */
290 len
-= be16_to_cpu(mdbo
->length
);
291 mdbo
= (void *) mdbo
+ be16_to_cpu(mdbo
->length
);
294 scon
->write_errors
+= errors
;
296 data
->header
.flags
= SCLP_EVENT_BUFFER_ACCEPTED
;
298 return SCLP_RC_NORMAL_COMPLETION
;
301 static void trigger_console_data(void *opaque
, int n
, int level
)
303 sclp_service_interrupt(0);
306 /* functions for live migration */
308 static const VMStateDescription vmstate_sclplmconsole
= {
309 .name
= "sclplmconsole",
311 .minimum_version_id
= 0,
312 .minimum_version_id_old
= 0,
313 .fields
= (VMStateField
[]) {
314 VMSTATE_BOOL(event
.event_pending
, SCLPConsoleLM
),
315 VMSTATE_UINT32(write_errors
, SCLPConsoleLM
),
316 VMSTATE_UINT32(length
, SCLPConsoleLM
),
317 VMSTATE_UINT8_ARRAY(buf
, SCLPConsoleLM
, SIZE_CONSOLE_BUFFER
),
318 VMSTATE_END_OF_LIST()
322 /* qemu object creation and initialization functions */
324 /* tell character layer our call-back functions */
326 static int console_init(SCLPEvent
*event
)
328 static bool console_available
;
330 SCLPConsoleLM
*scon
= DO_UPCAST(SCLPConsoleLM
, event
, event
);
332 if (console_available
) {
333 error_report("Multiple line-mode operator consoles are not supported");
336 console_available
= true;
339 qemu_chr_add_handlers(scon
->chr
, chr_can_read
, chr_read
, NULL
, scon
);
341 scon
->irq_console_read
= *qemu_allocate_irqs(trigger_console_data
, NULL
, 1);
346 static int console_exit(SCLPEvent
*event
)
351 static void console_reset(DeviceState
*dev
)
353 SCLPEvent
*event
= SCLP_EVENT(dev
);
354 SCLPConsoleLM
*scon
= DO_UPCAST(SCLPConsoleLM
, event
, event
);
356 event
->event_pending
= false;
358 scon
->write_errors
= 0;
361 static Property console_properties
[] = {
362 DEFINE_PROP_CHR("chardev", SCLPConsoleLM
, chr
),
363 DEFINE_PROP_UINT32("write_errors", SCLPConsoleLM
, write_errors
, 0),
364 DEFINE_PROP_BOOL("echo", SCLPConsoleLM
, echo
, true),
365 DEFINE_PROP_END_OF_LIST(),
368 static void console_class_init(ObjectClass
*klass
, void *data
)
370 DeviceClass
*dc
= DEVICE_CLASS(klass
);
371 SCLPEventClass
*ec
= SCLP_EVENT_CLASS(klass
);
373 dc
->props
= console_properties
;
374 dc
->reset
= console_reset
;
375 dc
->vmsd
= &vmstate_sclplmconsole
;
376 ec
->init
= console_init
;
377 ec
->exit
= console_exit
;
378 ec
->get_send_mask
= send_mask
;
379 ec
->get_receive_mask
= receive_mask
;
380 ec
->can_handle_event
= can_handle_event
;
381 ec
->read_event_data
= read_event_data
;
382 ec
->write_event_data
= write_event_data
;
385 static const TypeInfo sclp_console_info
= {
386 .name
= "sclplmconsole",
387 .parent
= TYPE_SCLP_EVENT
,
388 .instance_size
= sizeof(SCLPConsoleLM
),
389 .class_init
= console_class_init
,
390 .class_size
= sizeof(SCLPEventClass
),
393 static void register_types(void)
395 type_register_static(&sclp_console_info
);
398 type_init(register_types
)