16
#include "qemu/osdep.h"
17
#include "qemu/thread.h"
18
#include "qemu/error-report.h"
19
#include "qemu/module.h"
20
#include "chardev/char-fe.h"
22
#include "hw/s390x/sclp.h"
23
#include "migration/vmstate.h"
24
#include "hw/s390x/event-facility.h"
25
#include "hw/qdev-properties.h"
26
#include "hw/qdev-properties-system.h"
27
#include "hw/s390x/ebcdic.h"
28
#include "qom/object.h"
30
#define SIZE_BUFFER 4096
33
typedef struct OprtnsCommand {
34
EventBufferHeader header;
37
} QEMU_PACKED OprtnsCommand;
40
#define SIZE_CONSOLE_BUFFER (SCCB_DATA_LEN - sizeof(OprtnsCommand))
46
uint32_t write_errors;
48
uint8_t buf[SIZE_CONSOLE_BUFFER];
50
typedef struct SCLPConsoleLM SCLPConsoleLM;
52
#define TYPE_SCLPLM_CONSOLE "sclplmconsole"
53
DECLARE_INSTANCE_CHECKER(SCLPConsoleLM, SCLPLM_CONSOLE,
69
static int chr_can_read(void *opaque)
71
SCLPConsoleLM *scon = opaque;
73
if (scon->event.event_pending) {
79
static void chr_read(void *opaque, const uint8_t *buf, int size)
81
SCLPConsoleLM *scon = opaque;
85
if (*buf == '\r' || *buf == '\n') {
86
scon->event.event_pending = true;
87
sclp_service_interrupt(0);
90
if (scon->length == SIZE_CONSOLE_BUFFER) {
94
scon->buf[scon->length] = *buf;
99
qemu_chr_fe_write_all(&scon->chr, buf, size);
105
static bool can_handle_event(uint8_t type)
107
return type == SCLP_EVENT_MESSAGE || type == SCLP_EVENT_PMSGCMD;
110
static sccb_mask_t send_mask(void)
112
return SCLP_EVENT_MASK_OP_CMD | SCLP_EVENT_MASK_PMSGCMD;
115
static sccb_mask_t receive_mask(void)
117
return SCLP_EVENT_MASK_MSG | SCLP_EVENT_MASK_PMSGCMD;
125
static int get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
130
SCLPConsoleLM *cons = SCLPLM_CONSOLE(event);
138
ebcdic_put(buf, (char *)&cons->buf, len);
142
event->event_pending = false;
147
static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
153
OprtnsCommand *oc = (OprtnsCommand *) evt_buf_hdr;
155
if (!event->event_pending) {
160
to = (uint8_t *)&oc->data;
161
avail = *slen - sizeof(OprtnsCommand);
162
rc = get_console_data(event, to, &src_len, avail);
168
oc->message_unit.mdmsu.gds_id = GDS_ID_MDSMU;
169
oc->message_unit.mdmsu.length = cpu_to_be16(sizeof(struct MDMSU));
171
oc->message_unit.cpmsu.gds_id = GDS_ID_CPMSU;
172
oc->message_unit.cpmsu.length =
173
cpu_to_be16(sizeof(struct MDMSU) - sizeof(GdsVector));
175
oc->message_unit.text_command.gds_id = GDS_ID_TEXTCMD;
176
oc->message_unit.text_command.length =
177
cpu_to_be16(sizeof(struct MDMSU) - (2 * sizeof(GdsVector)));
179
oc->message_unit.self_def_text_message.key = GDS_KEY_SELFDEFTEXTMSG;
180
oc->message_unit.self_def_text_message.length =
181
cpu_to_be16(sizeof(struct MDMSU) - (3 * sizeof(GdsVector)));
183
oc->message_unit.text_message.key = GDS_KEY_TEXTMSG;
184
oc->message_unit.text_message.length =
185
cpu_to_be16(sizeof(GdsSubvector) + src_len);
187
oc->header.length = cpu_to_be16(sizeof(OprtnsCommand) + src_len);
188
oc->header.type = SCLP_EVENT_OPRTNS_COMMAND;
189
*slen = avail - src_len;
199
static int write_console_data(SCLPEvent *event, const uint8_t *buf, int len)
201
SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
203
if (!qemu_chr_fe_backend_connected(&scon->chr)) {
210
return qemu_chr_fe_write_all(&scon->chr, buf, len);
213
static int process_mdb(SCLPEvent *event, MDBO *mdbo)
217
uint8_t buffer[SIZE_BUFFER];
219
len = be16_to_cpu(mdbo->length);
220
len -= sizeof(mdbo->length) + sizeof(mdbo->type)
221
+ sizeof(mdbo->mto.line_type_flags)
222
+ sizeof(mdbo->mto.alarm_control)
223
+ sizeof(mdbo->mto._reserved);
225
assert(len <= SIZE_BUFFER);
228
ascii_put(buffer, mdbo->mto.message, len);
229
rc = write_console_data(event, (uint8_t *)NEWLINE, 1);
233
return write_console_data(event, buffer, len);
236
static int write_event_data(SCLPEvent *event, EventBufferHeader *ebh)
242
SclpMsg *data = (SclpMsg *) ebh;
243
SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
245
len = be16_to_cpu(data->mdb.header.length);
246
if (len < sizeof(data->mdb.header)) {
247
return SCLP_RC_INCONSISTENT_LENGTHS;
249
len -= sizeof(data->mdb.header);
252
mdbo = data->mdb.mdbo;
254
if (be16_to_cpu(mdbo->length) > len
255
|| be16_to_cpu(mdbo->length) == 0) {
256
return SCLP_RC_INCONSISTENT_LENGTHS;
258
len -= be16_to_cpu(mdbo->length);
259
mdbo = (void *) mdbo + be16_to_cpu(mdbo->length);
263
len = be16_to_cpu(data->mdb.header.length) - sizeof(data->mdb.header);
264
mdbo = data->mdb.mdbo;
266
switch (be16_to_cpu(mdbo->type)) {
269
written = process_mdb(event, mdbo);
278
len -= be16_to_cpu(mdbo->length);
279
mdbo = (void *) mdbo + be16_to_cpu(mdbo->length);
282
scon->write_errors += errors;
284
data->header.flags = SCLP_EVENT_BUFFER_ACCEPTED;
286
return SCLP_RC_NORMAL_COMPLETION;
291
static const VMStateDescription vmstate_sclplmconsole = {
292
.name = "sclplmconsole",
294
.minimum_version_id = 0,
295
.fields = (const VMStateField[]) {
296
VMSTATE_BOOL(event.event_pending, SCLPConsoleLM),
297
VMSTATE_UINT32(write_errors, SCLPConsoleLM),
298
VMSTATE_UINT32(length, SCLPConsoleLM),
299
VMSTATE_UINT8_ARRAY(buf, SCLPConsoleLM, SIZE_CONSOLE_BUFFER),
300
VMSTATE_END_OF_LIST()
308
static int console_init(SCLPEvent *event)
310
static bool console_available;
312
SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
314
if (console_available) {
315
error_report("Multiple line-mode operator consoles are not supported");
318
console_available = true;
320
qemu_chr_fe_set_handlers(&scon->chr, chr_can_read,
321
chr_read, NULL, NULL, scon, NULL, true);
326
static void console_reset(DeviceState *dev)
328
SCLPEvent *event = SCLP_EVENT(dev);
329
SCLPConsoleLM *scon = SCLPLM_CONSOLE(event);
331
event->event_pending = false;
333
scon->write_errors = 0;
336
static Property console_properties[] = {
337
DEFINE_PROP_CHR("chardev", SCLPConsoleLM, chr),
338
DEFINE_PROP_UINT32("write_errors", SCLPConsoleLM, write_errors, 0),
339
DEFINE_PROP_BOOL("echo", SCLPConsoleLM, echo, true),
340
DEFINE_PROP_END_OF_LIST(),
343
static void console_class_init(ObjectClass *klass, void *data)
345
DeviceClass *dc = DEVICE_CLASS(klass);
346
SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
348
device_class_set_props(dc, console_properties);
349
dc->reset = console_reset;
350
dc->vmsd = &vmstate_sclplmconsole;
351
ec->init = console_init;
352
ec->get_send_mask = send_mask;
353
ec->get_receive_mask = receive_mask;
354
ec->can_handle_event = can_handle_event;
355
ec->read_event_data = read_event_data;
356
ec->write_event_data = write_event_data;
357
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
360
static const TypeInfo sclp_console_info = {
361
.name = TYPE_SCLPLM_CONSOLE,
362
.parent = TYPE_SCLP_EVENT,
363
.instance_size = sizeof(SCLPConsoleLM),
364
.class_init = console_class_init,
365
.class_size = sizeof(SCLPEventClass),
368
static void register_types(void)
370
type_register_static(&sclp_console_info);
373
type_init(register_types)