]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/sclpconsole.c
Use DECLARE_*CHECKER* macros
[mirror_qemu.git] / hw / char / sclpconsole.c
CommitLineData
130c57c0
HG
1/*
2 * SCLP event type
3 * Ascii Console Data (VT220 Console)
4 *
5 * Copyright IBM, Corp. 2012
6 *
7 * Authors:
8 * Heinz Graalfs <graalfs@de.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11 * option) any later version. See the COPYING file in the top-level directory.
12 *
13 */
14
9615495a 15#include "qemu/osdep.h"
1de7afc9 16#include "qemu/thread.h"
b4a42f81 17#include "qemu/error-report.h"
0b8fa32f 18#include "qemu/module.h"
130c57c0 19
83c9f4ca 20#include "hw/s390x/sclp.h"
d6454270 21#include "migration/vmstate.h"
a27bd6c7 22#include "hw/qdev-properties.h"
83c9f4ca 23#include "hw/s390x/event-facility.h"
4d43a603 24#include "chardev/char-fe.h"
db1015e9 25#include "qom/object.h"
130c57c0
HG
26
27typedef struct ASCIIConsoleData {
28 EventBufferHeader ebh;
880a7817 29 char data[];
130c57c0
HG
30} QEMU_PACKED ASCIIConsoleData;
31
32/* max size for ASCII data in 4K SCCB page */
33#define SIZE_BUFFER_VT220 4080
34
db1015e9 35struct SCLPConsole {
130c57c0 36 SCLPEvent event;
becdfa00 37 CharBackend chr;
ea9ad3e9
HG
38 uint8_t iov[SIZE_BUFFER_VT220];
39 uint32_t iov_sclp; /* offset in buf for SCLP read operation */
40 uint32_t iov_bs; /* offset in buf for char layer read operation */
41 uint32_t iov_data_len; /* length of byte stream in buffer */
42 uint32_t iov_sclp_rest; /* length of byte stream not read via SCLP */
bb3e9e1f 43 bool notify; /* qemu_notify_event() req'd if true */
db1015e9
EH
44};
45typedef struct SCLPConsole SCLPConsole;
130c57c0 46
3f6ec642 47#define TYPE_SCLP_CONSOLE "sclpconsole"
8110fa1d
EH
48DECLARE_INSTANCE_CHECKER(SCLPConsole, SCLP_CONSOLE,
49 TYPE_SCLP_CONSOLE)
3f6ec642 50
130c57c0
HG
51/* character layer call-back functions */
52
53/* Return number of bytes that fit into iov buffer */
54static int chr_can_read(void *opaque)
55{
130c57c0 56 SCLPConsole *scon = opaque;
bb3e9e1f 57 int avail = SIZE_BUFFER_VT220 - scon->iov_data_len;
130c57c0 58
bb3e9e1f
HG
59 if (avail == 0) {
60 scon->notify = true;
61 }
62 return avail;
130c57c0
HG
63}
64
b074e622
CB
65/* Send data from a char device over to the guest */
66static void chr_read(void *opaque, const uint8_t *buf, int size)
130c57c0 67{
b074e622
CB
68 SCLPConsole *scon = opaque;
69
70 assert(scon);
130c57c0
HG
71 /* read data must fit into current buffer */
72 assert(size <= SIZE_BUFFER_VT220 - scon->iov_data_len);
73
74 /* put byte-stream from character layer into buffer */
ea9ad3e9 75 memcpy(&scon->iov[scon->iov_bs], buf, size);
130c57c0
HG
76 scon->iov_data_len += size;
77 scon->iov_sclp_rest += size;
78 scon->iov_bs += size;
79 scon->event.event_pending = true;
b074e622 80 sclp_service_interrupt(0);
130c57c0
HG
81}
82
130c57c0
HG
83/* functions to be called by event facility */
84
c3d9f24a 85static bool can_handle_event(uint8_t type)
130c57c0 86{
c3d9f24a 87 return type == SCLP_EVENT_ASCII_CONSOLE_DATA;
130c57c0
HG
88}
89
1ffed98f 90static sccb_mask_t send_mask(void)
130c57c0
HG
91{
92 return SCLP_EVENT_MASK_MSG_ASCII;
93}
94
1ffed98f 95static sccb_mask_t receive_mask(void)
130c57c0
HG
96{
97 return SCLP_EVENT_MASK_MSG_ASCII;
98}
99
100/* triggered by SCLP's read_event_data -
101 * copy console data byte-stream into provided (SCLP) buffer
102 */
103static void get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
104 int avail)
105{
3f6ec642 106 SCLPConsole *cons = SCLP_CONSOLE(event);
130c57c0
HG
107
108 /* first byte is hex 0 saying an ascii string follows */
109 *buf++ = '\0';
110 avail--;
111 /* if all data fit into provided SCLP buffer */
112 if (avail >= cons->iov_sclp_rest) {
113 /* copy character byte-stream to SCLP buffer */
ea9ad3e9 114 memcpy(buf, &cons->iov[cons->iov_sclp], cons->iov_sclp_rest);
130c57c0 115 *size = cons->iov_sclp_rest + 1;
ea9ad3e9
HG
116 cons->iov_sclp = 0;
117 cons->iov_bs = 0;
130c57c0
HG
118 cons->iov_data_len = 0;
119 cons->iov_sclp_rest = 0;
120 event->event_pending = false;
121 /* data provided and no more data pending */
122 } else {
123 /* if provided buffer is too small, just copy part */
ea9ad3e9 124 memcpy(buf, &cons->iov[cons->iov_sclp], avail);
130c57c0
HG
125 *size = avail + 1;
126 cons->iov_sclp_rest -= avail;
127 cons->iov_sclp += avail;
128 /* more data pending */
129 }
bb3e9e1f
HG
130 if (cons->notify) {
131 cons->notify = false;
132 qemu_notify_event();
133 }
130c57c0
HG
134}
135
136static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
137 int *slen)
138{
139 int avail;
140 size_t src_len;
141 uint8_t *to;
142 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
143
144 if (!event->event_pending) {
145 /* no data pending */
146 return 0;
147 }
148
149 to = (uint8_t *)&acd->data;
150 avail = *slen - sizeof(ASCIIConsoleData);
151 get_console_data(event, to, &src_len, avail);
152
153 acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len);
154 acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
155 acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED;
156 *slen = avail - src_len;
157
158 return 1;
159}
160
161/* triggered by SCLP's write_event_data
8367a14f
SW
162 * - write console data to character layer
163 * returns < 0 if an error occurred
130c57c0
HG
164 */
165static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf,
166 size_t len)
167{
3f6ec642 168 SCLPConsole *scon = SCLP_CONSOLE(event);
130c57c0 169
30650701 170 if (!qemu_chr_fe_backend_connected(&scon->chr)) {
130c57c0
HG
171 /* If there's no backend, we can just say we consumed all data. */
172 return len;
173 }
174
6ab3fc32
DB
175 /* XXX this blocks entire thread. Rewrite to use
176 * qemu_chr_fe_write and background I/O callbacks */
5345fdb4 177 return qemu_chr_fe_write_all(&scon->chr, buf, len);
130c57c0
HG
178}
179
180static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
181{
182 int rc;
183 int length;
184 ssize_t written;
185 ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
186
187 length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader);
188 written = write_console_data(event, (uint8_t *)acd->data, length);
189
190 rc = SCLP_RC_NORMAL_COMPLETION;
191 /* set event buffer accepted flag */
192 evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED;
193
194 /* written will be zero if a pty is not connected - don't treat as error */
195 if (written < 0) {
196 /* event buffer not accepted due to error in character layer */
197 evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
198 rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK;
199 }
200
201 return rc;
202}
203
cb335beb
HG
204static const VMStateDescription vmstate_sclpconsole = {
205 .name = "sclpconsole",
206 .version_id = 0,
207 .minimum_version_id = 0,
35d08458 208 .fields = (VMStateField[]) {
cb335beb
HG
209 VMSTATE_BOOL(event.event_pending, SCLPConsole),
210 VMSTATE_UINT8_ARRAY(iov, SCLPConsole, SIZE_BUFFER_VT220),
211 VMSTATE_UINT32(iov_sclp, SCLPConsole),
212 VMSTATE_UINT32(iov_bs, SCLPConsole),
213 VMSTATE_UINT32(iov_data_len, SCLPConsole),
214 VMSTATE_UINT32(iov_sclp_rest, SCLPConsole),
215 VMSTATE_END_OF_LIST()
216 }
217};
218
130c57c0
HG
219/* qemu object creation and initialization functions */
220
221/* tell character layer our call-back functions */
cb335beb 222
130c57c0
HG
223static int console_init(SCLPEvent *event)
224{
225 static bool console_available;
226
3f6ec642 227 SCLPConsole *scon = SCLP_CONSOLE(event);
130c57c0
HG
228
229 if (console_available) {
230 error_report("Multiple VT220 operator consoles are not supported");
231 return -1;
232 }
233 console_available = true;
fa394ed6 234 qemu_chr_fe_set_handlers(&scon->chr, chr_can_read,
81517ba3 235 chr_read, NULL, NULL, scon, NULL, true);
130c57c0
HG
236
237 return 0;
238}
239
3af6de32
HG
240static void console_reset(DeviceState *dev)
241{
242 SCLPEvent *event = SCLP_EVENT(dev);
3f6ec642 243 SCLPConsole *scon = SCLP_CONSOLE(event);
3af6de32
HG
244
245 event->event_pending = false;
246 scon->iov_sclp = 0;
247 scon->iov_bs = 0;
248 scon->iov_data_len = 0;
249 scon->iov_sclp_rest = 0;
bb3e9e1f 250 scon->notify = false;
3af6de32
HG
251}
252
130c57c0
HG
253static Property console_properties[] = {
254 DEFINE_PROP_CHR("chardev", SCLPConsole, chr),
255 DEFINE_PROP_END_OF_LIST(),
256};
257
258static void console_class_init(ObjectClass *klass, void *data)
259{
260 DeviceClass *dc = DEVICE_CLASS(klass);
261 SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
262
4f67d30b 263 device_class_set_props(dc, console_properties);
3af6de32 264 dc->reset = console_reset;
cb335beb 265 dc->vmsd = &vmstate_sclpconsole;
130c57c0 266 ec->init = console_init;
130c57c0
HG
267 ec->get_send_mask = send_mask;
268 ec->get_receive_mask = receive_mask;
c3d9f24a 269 ec->can_handle_event = can_handle_event;
130c57c0
HG
270 ec->read_event_data = read_event_data;
271 ec->write_event_data = write_event_data;
183f6b8d 272 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
130c57c0
HG
273}
274
8c43a6f0 275static const TypeInfo sclp_console_info = {
1a3bae79 276 .name = TYPE_SCLP_CONSOLE,
130c57c0
HG
277 .parent = TYPE_SCLP_EVENT,
278 .instance_size = sizeof(SCLPConsole),
279 .class_init = console_class_init,
280 .class_size = sizeof(SCLPEventClass),
281};
282
283static void register_types(void)
284{
285 type_register_static(&sclp_console_info);
286}
287
288type_init(register_types)