1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "hw/qdev.h"
17#include "qemu/thread.h"
18#include "qemu/error-report.h"
19
20#include "hw/s390x/sclp.h"
21#include "hw/s390x/event-facility.h"
22#include "chardev/char-fe.h"
23
24typedef struct ASCIIConsoleData {
25 EventBufferHeader ebh;
26 char data[0];
27} QEMU_PACKED ASCIIConsoleData;
28
29
30#define SIZE_BUFFER_VT220 4080
31
32typedef struct SCLPConsole {
33 SCLPEvent event;
34 CharBackend chr;
35 uint8_t iov[SIZE_BUFFER_VT220];
36 uint32_t iov_sclp;
37 uint32_t iov_bs;
38 uint32_t iov_data_len;
39 uint32_t iov_sclp_rest;
40 bool notify;
41} SCLPConsole;
42
43#define TYPE_SCLP_CONSOLE "sclpconsole"
44#define SCLP_CONSOLE(obj) \
45 OBJECT_CHECK(SCLPConsole, (obj), TYPE_SCLP_CONSOLE)
46
47
48
49
50static int chr_can_read(void *opaque)
51{
52 SCLPConsole *scon = opaque;
53 int avail = SIZE_BUFFER_VT220 - scon->iov_data_len;
54
55 if (avail == 0) {
56 scon->notify = true;
57 }
58 return avail;
59}
60
61
62static void chr_read(void *opaque, const uint8_t *buf, int size)
63{
64 SCLPConsole *scon = opaque;
65
66 assert(scon);
67
68 assert(size <= SIZE_BUFFER_VT220 - scon->iov_data_len);
69
70
71 memcpy(&scon->iov[scon->iov_bs], buf, size);
72 scon->iov_data_len += size;
73 scon->iov_sclp_rest += size;
74 scon->iov_bs += size;
75 scon->event.event_pending = true;
76 sclp_service_interrupt(0);
77}
78
79
80
81static bool can_handle_event(uint8_t type)
82{
83 return type == SCLP_EVENT_ASCII_CONSOLE_DATA;
84}
85
86static unsigned int send_mask(void)
87{
88 return SCLP_EVENT_MASK_MSG_ASCII;
89}
90
91static unsigned int receive_mask(void)
92{
93 return SCLP_EVENT_MASK_MSG_ASCII;
94}
95
96
97
98
99static void get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
100 int avail)
101{
102 SCLPConsole *cons = SCLP_CONSOLE(event);
103
104
105 *buf++ = '\0';
106 avail--;
107
108 if (avail >= cons->iov_sclp_rest) {
109
110 memcpy(buf, &cons->iov[cons->iov_sclp], cons->iov_sclp_rest);
111 *size = cons->iov_sclp_rest + 1;
112 cons->iov_sclp = 0;
113 cons->iov_bs = 0;
114 cons->iov_data_len = 0;
115 cons->iov_sclp_rest = 0;
116 event->event_pending = false;
117
118 } else {
119
120 memcpy(buf, &cons->iov[cons->iov_sclp], avail);
121 *size = avail + 1;
122 cons->iov_sclp_rest -= avail;
123 cons->iov_sclp += avail;
124
125 }
126 if (cons->notify) {
127 cons->notify = false;
128 qemu_notify_event();
129 }
130}
131
132static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
133 int *slen)
134{
135 int avail;
136 size_t src_len;
137 uint8_t *to;
138 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
139
140 if (!event->event_pending) {
141
142 return 0;
143 }
144
145 to = (uint8_t *)&acd->data;
146 avail = *slen - sizeof(ASCIIConsoleData);
147 get_console_data(event, to, &src_len, avail);
148
149 acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len);
150 acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
151 acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED;
152 *slen = avail - src_len;
153
154 return 1;
155}
156
157
158
159
160
161static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf,
162 size_t len)
163{
164 SCLPConsole *scon = SCLP_CONSOLE(event);
165
166 if (!qemu_chr_fe_backend_connected(&scon->chr)) {
167
168 return len;
169 }
170
171
172
173 return qemu_chr_fe_write_all(&scon->chr, buf, len);
174}
175
176static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
177{
178 int rc;
179 int length;
180 ssize_t written;
181 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
182
183 length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader);
184 written = write_console_data(event, (uint8_t *)acd->data, length);
185
186 rc = SCLP_RC_NORMAL_COMPLETION;
187
188 evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED;
189
190
191 if (written < 0) {
192
193 evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
194 rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK;
195 }
196
197 return rc;
198}
199
200static const VMStateDescription vmstate_sclpconsole = {
201 .name = "sclpconsole",
202 .version_id = 0,
203 .minimum_version_id = 0,
204 .fields = (VMStateField[]) {
205 VMSTATE_BOOL(event.event_pending, SCLPConsole),
206 VMSTATE_UINT8_ARRAY(iov, SCLPConsole, SIZE_BUFFER_VT220),
207 VMSTATE_UINT32(iov_sclp, SCLPConsole),
208 VMSTATE_UINT32(iov_bs, SCLPConsole),
209 VMSTATE_UINT32(iov_data_len, SCLPConsole),
210 VMSTATE_UINT32(iov_sclp_rest, SCLPConsole),
211 VMSTATE_END_OF_LIST()
212 }
213};
214
215
216
217
218
219static int console_init(SCLPEvent *event)
220{
221 static bool console_available;
222
223 SCLPConsole *scon = SCLP_CONSOLE(event);
224
225 if (console_available) {
226 error_report("Multiple VT220 operator consoles are not supported");
227 return -1;
228 }
229 console_available = true;
230 qemu_chr_fe_set_handlers(&scon->chr, chr_can_read,
231 chr_read, NULL, NULL, scon, NULL, true);
232
233 return 0;
234}
235
236static void console_reset(DeviceState *dev)
237{
238 SCLPEvent *event = SCLP_EVENT(dev);
239 SCLPConsole *scon = SCLP_CONSOLE(event);
240
241 event->event_pending = false;
242 scon->iov_sclp = 0;
243 scon->iov_bs = 0;
244 scon->iov_data_len = 0;
245 scon->iov_sclp_rest = 0;
246 scon->notify = false;
247}
248
249static int console_exit(SCLPEvent *event)
250{
251 return 0;
252}
253
254static Property console_properties[] = {
255 DEFINE_PROP_CHR("chardev", SCLPConsole, chr),
256 DEFINE_PROP_END_OF_LIST(),
257};
258
259static void console_class_init(ObjectClass *klass, void *data)
260{
261 DeviceClass *dc = DEVICE_CLASS(klass);
262 SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
263
264 dc->props = console_properties;
265 dc->reset = console_reset;
266 dc->vmsd = &vmstate_sclpconsole;
267 ec->init = console_init;
268 ec->exit = console_exit;
269 ec->get_send_mask = send_mask;
270 ec->get_receive_mask = receive_mask;
271 ec->can_handle_event = can_handle_event;
272 ec->read_event_data = read_event_data;
273 ec->write_event_data = write_event_data;
274 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
275}
276
277static const TypeInfo sclp_console_info = {
278 .name = "sclpconsole",
279 .parent = TYPE_SCLP_EVENT,
280 .instance_size = sizeof(SCLPConsole),
281 .class_init = console_class_init,
282 .class_size = sizeof(SCLPEventClass),
283};
284
285static void register_types(void)
286{
287 type_register_static(&sclp_console_info);
288}
289
290type_init(register_types)
291