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.
16 #include "qemu/osdep.h"
18 #include "qemu/thread.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/char.h"
22 #include "hw/s390x/sclp.h"
23 #include "hw/s390x/event-facility.h"
24 #include "hw/s390x/ebcdic.h"
26 #define SIZE_BUFFER 4096
29 typedef struct OprtnsCommand
{
30 EventBufferHeader header
;
33 } QEMU_PACKED OprtnsCommand
;
35 /* max size for line-mode data in 4K SCCB page */
36 #define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand))
38 typedef struct SCLPConsoleLM
{
41 bool echo
; /* immediate echo of input if true */
42 uint32_t write_errors
; /* errors writing to char layer */
43 uint32_t length
; /* length of byte stream in buffer */
44 uint8_t buf
[SIZE_CONSOLE_BUFFER
];
47 #define TYPE_SCLPLM_CONSOLE "sclplmconsole"
48 #define SCLPLM_CONSOLE(obj) \
49 OBJECT_CHECK(SCLPConsoleLM, (obj), TYPE_SCLPLM_CONSOLE)
52 * Character layer call-back functions
54 * Allow 1 character at a time
56 * Accumulate bytes from character layer in console buffer,
57 * event_pending is set when a newline character is encountered
59 * The maximum command line length is limited by the maximum
60 * space available in an SCCB. Line mode console input is sent
61 * truncated to the guest in case it doesn't fit into the SCCB.
64 static int chr_can_read(void *opaque
)
66 SCLPConsoleLM
*scon
= opaque
;
68 if (scon
->event
.event_pending
) {
74 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
76 SCLPConsoleLM
*scon
= opaque
;
80 if (*buf
== '\r' || *buf
== '\n') {
81 scon
->event
.event_pending
= true;
82 sclp_service_interrupt(0);
85 if (scon
->length
== SIZE_CONSOLE_BUFFER
) {
86 /* Eat the character, but still process CR and LF. */
89 scon
->buf
[scon
->length
] = *buf
;
92 /* XXX this blocks entire thread. Rewrite to use
93 * qemu_chr_fe_write and background I/O callbacks */
94 qemu_chr_fe_write_all(scon
->chr
, buf
, size
);
98 /* functions to be called by event facility */
100 static bool can_handle_event(uint8_t type
)
102 return type
== SCLP_EVENT_MESSAGE
|| type
== SCLP_EVENT_PMSGCMD
;
105 static unsigned int send_mask(void)
107 return SCLP_EVENT_MASK_OP_CMD
| SCLP_EVENT_MASK_PMSGCMD
;
110 static unsigned int receive_mask(void)
112 return SCLP_EVENT_MASK_MSG
| SCLP_EVENT_MASK_PMSGCMD
;
116 * Triggered by SCLP's read_event_data
117 * - convert ASCII byte stream to EBCDIC and
118 * - copy converted data into provided (SCLP) buffer
120 static int get_console_data(SCLPEvent
*event
, uint8_t *buf
, size_t *size
,
125 SCLPConsoleLM
*cons
= SCLPLM_CONSOLE(event
);
128 /* data need to fit into provided SCLP buffer */
133 ebcdic_put(buf
, (char *)&cons
->buf
, len
);
136 /* data provided and no more data pending */
137 event
->event_pending
= false;
142 static int read_event_data(SCLPEvent
*event
, EventBufferHeader
*evt_buf_hdr
,
148 OprtnsCommand
*oc
= (OprtnsCommand
*) evt_buf_hdr
;
150 if (!event
->event_pending
) {
151 /* no data pending */
155 to
= (uint8_t *)&oc
->data
;
156 avail
= *slen
- sizeof(OprtnsCommand
);
157 rc
= get_console_data(event
, to
, &src_len
, avail
);
159 /* data didn't fit, try next SCCB */
163 oc
->message_unit
.mdmsu
.gds_id
= GDS_ID_MDSMU
;
164 oc
->message_unit
.mdmsu
.length
= cpu_to_be16(sizeof(struct MDMSU
));
166 oc
->message_unit
.cpmsu
.gds_id
= GDS_ID_CPMSU
;
167 oc
->message_unit
.cpmsu
.length
=
168 cpu_to_be16(sizeof(struct MDMSU
) - sizeof(GdsVector
));
170 oc
->message_unit
.text_command
.gds_id
= GDS_ID_TEXTCMD
;
171 oc
->message_unit
.text_command
.length
=
172 cpu_to_be16(sizeof(struct MDMSU
) - (2 * sizeof(GdsVector
)));
174 oc
->message_unit
.self_def_text_message
.key
= GDS_KEY_SELFDEFTEXTMSG
;
175 oc
->message_unit
.self_def_text_message
.length
=
176 cpu_to_be16(sizeof(struct MDMSU
) - (3 * sizeof(GdsVector
)));
178 oc
->message_unit
.text_message
.key
= GDS_KEY_TEXTMSG
;
179 oc
->message_unit
.text_message
.length
=
180 cpu_to_be16(sizeof(GdsSubvector
) + src_len
);
182 oc
->header
.length
= cpu_to_be16(sizeof(OprtnsCommand
) + src_len
);
183 oc
->header
.type
= SCLP_EVENT_OPRTNS_COMMAND
;
184 *slen
= avail
- src_len
;
190 * Triggered by SCLP's write_event_data
191 * - write console data to character layer
192 * returns < 0 if an error occurred
194 static int write_console_data(SCLPEvent
*event
, const uint8_t *buf
, int len
)
196 SCLPConsoleLM
*scon
= SCLPLM_CONSOLE(event
);
199 /* If there's no backend, we can just say we consumed all data. */
203 /* XXX this blocks entire thread. Rewrite to use
204 * qemu_chr_fe_write and background I/O callbacks */
205 return qemu_chr_fe_write_all(scon
->chr
, buf
, len
);
208 static int process_mdb(SCLPEvent
*event
, MDBO
*mdbo
)
212 uint8_t buffer
[SIZE_BUFFER
];
214 len
= be16_to_cpu(mdbo
->length
);
215 len
-= sizeof(mdbo
->length
) + sizeof(mdbo
->type
)
216 + sizeof(mdbo
->mto
.line_type_flags
)
217 + sizeof(mdbo
->mto
.alarm_control
)
218 + sizeof(mdbo
->mto
._reserved
);
220 assert(len
<= SIZE_BUFFER
);
222 /* convert EBCDIC SCLP contents to ASCII console message */
223 ascii_put(buffer
, mdbo
->mto
.message
, len
);
224 rc
= write_console_data(event
, (uint8_t *)NEWLINE
, 1);
228 return write_console_data(event
, buffer
, len
);
231 static int write_event_data(SCLPEvent
*event
, EventBufferHeader
*ebh
)
237 SclpMsg
*data
= (SclpMsg
*) ebh
;
238 SCLPConsoleLM
*scon
= SCLPLM_CONSOLE(event
);
240 len
= be16_to_cpu(data
->mdb
.header
.length
);
241 if (len
< sizeof(data
->mdb
.header
)) {
242 return SCLP_RC_INCONSISTENT_LENGTHS
;
244 len
-= sizeof(data
->mdb
.header
);
246 /* first check message buffers */
247 mdbo
= data
->mdb
.mdbo
;
249 if (be16_to_cpu(mdbo
->length
) > len
250 || be16_to_cpu(mdbo
->length
) == 0) {
251 return SCLP_RC_INCONSISTENT_LENGTHS
;
253 len
-= be16_to_cpu(mdbo
->length
);
254 mdbo
= (void *) mdbo
+ be16_to_cpu(mdbo
->length
);
258 len
= be16_to_cpu(data
->mdb
.header
.length
) - sizeof(data
->mdb
.header
);
259 mdbo
= data
->mdb
.mdbo
;
261 switch (be16_to_cpu(mdbo
->type
)) {
263 /* message text object */
264 written
= process_mdb(event
, mdbo
);
266 /* character layer error */
270 default: /* ignore */
273 len
-= be16_to_cpu(mdbo
->length
);
274 mdbo
= (void *) mdbo
+ be16_to_cpu(mdbo
->length
);
277 scon
->write_errors
+= errors
;
279 data
->header
.flags
= SCLP_EVENT_BUFFER_ACCEPTED
;
281 return SCLP_RC_NORMAL_COMPLETION
;
284 /* functions for live migration */
286 static const VMStateDescription vmstate_sclplmconsole
= {
287 .name
= "sclplmconsole",
289 .minimum_version_id
= 0,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_BOOL(event
.event_pending
, SCLPConsoleLM
),
292 VMSTATE_UINT32(write_errors
, SCLPConsoleLM
),
293 VMSTATE_UINT32(length
, SCLPConsoleLM
),
294 VMSTATE_UINT8_ARRAY(buf
, SCLPConsoleLM
, SIZE_CONSOLE_BUFFER
),
295 VMSTATE_END_OF_LIST()
299 /* qemu object creation and initialization functions */
301 /* tell character layer our call-back functions */
303 static int console_init(SCLPEvent
*event
)
305 static bool console_available
;
307 SCLPConsoleLM
*scon
= SCLPLM_CONSOLE(event
);
309 if (console_available
) {
310 error_report("Multiple line-mode operator consoles are not supported");
313 console_available
= true;
316 qemu_chr_add_handlers(scon
->chr
, chr_can_read
, chr_read
, NULL
, scon
);
322 static int console_exit(SCLPEvent
*event
)
327 static void console_reset(DeviceState
*dev
)
329 SCLPEvent
*event
= SCLP_EVENT(dev
);
330 SCLPConsoleLM
*scon
= SCLPLM_CONSOLE(event
);
332 event
->event_pending
= false;
334 scon
->write_errors
= 0;
337 static Property console_properties
[] = {
338 DEFINE_PROP_CHR("chardev", SCLPConsoleLM
, chr
),
339 DEFINE_PROP_UINT32("write_errors", SCLPConsoleLM
, write_errors
, 0),
340 DEFINE_PROP_BOOL("echo", SCLPConsoleLM
, echo
, true),
341 DEFINE_PROP_END_OF_LIST(),
344 static void console_class_init(ObjectClass
*klass
, void *data
)
346 DeviceClass
*dc
= DEVICE_CLASS(klass
);
347 SCLPEventClass
*ec
= SCLP_EVENT_CLASS(klass
);
349 dc
->props
= console_properties
;
350 dc
->reset
= console_reset
;
351 dc
->vmsd
= &vmstate_sclplmconsole
;
352 ec
->init
= console_init
;
353 ec
->exit
= console_exit
;
354 ec
->get_send_mask
= send_mask
;
355 ec
->get_receive_mask
= receive_mask
;
356 ec
->can_handle_event
= can_handle_event
;
357 ec
->read_event_data
= read_event_data
;
358 ec
->write_event_data
= write_event_data
;
359 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
362 static const TypeInfo sclp_console_info
= {
363 .name
= "sclplmconsole",
364 .parent
= TYPE_SCLP_EVENT
,
365 .instance_size
= sizeof(SCLPConsoleLM
),
366 .class_init
= console_class_init
,
367 .class_size
= sizeof(SCLPEventClass
),
370 static void register_types(void)
372 type_register_static(&sclp_console_info
);
375 type_init(register_types
)