xref: /openbmc/qemu/hw/char/sclpconsole.c (revision 1b111dc1)
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 
15 #include <hw/qdev.h>
16 #include "qemu/thread.h"
17 #include "qemu/error-report.h"
18 
19 #include "hw/s390x/sclp.h"
20 #include "hw/s390x/event-facility.h"
21 #include "sysemu/char.h"
22 
23 typedef struct ASCIIConsoleData {
24     EventBufferHeader ebh;
25     char data[0];
26 } QEMU_PACKED ASCIIConsoleData;
27 
28 /* max size for ASCII data in 4K SCCB page */
29 #define SIZE_BUFFER_VT220 4080
30 
31 typedef struct SCLPConsole {
32     SCLPEvent event;
33     CharDriverState *chr;
34     uint8_t iov[SIZE_BUFFER_VT220];
35     uint32_t iov_sclp;      /* offset in buf for SCLP read operation       */
36     uint32_t iov_bs;        /* offset in buf for char layer read operation */
37     uint32_t iov_data_len;  /* length of byte stream in buffer             */
38     uint32_t iov_sclp_rest; /* length of byte stream not read via SCLP     */
39     qemu_irq irq_read_vt220;
40 } SCLPConsole;
41 
42 /* character layer call-back functions */
43 
44 /* Return number of bytes that fit into iov buffer */
45 static int chr_can_read(void *opaque)
46 {
47     SCLPConsole *scon = opaque;
48 
49     return SIZE_BUFFER_VT220 - scon->iov_data_len;
50 }
51 
52 /* Receive n bytes from character layer, save in iov buffer,
53  * and set event pending */
54 static void receive_from_chr_layer(SCLPConsole *scon, const uint8_t *buf,
55                                    int size)
56 {
57     /* read data must fit into current buffer */
58     assert(size <= SIZE_BUFFER_VT220 - scon->iov_data_len);
59 
60     /* put byte-stream from character layer into buffer */
61     memcpy(&scon->iov[scon->iov_bs], buf, size);
62     scon->iov_data_len += size;
63     scon->iov_sclp_rest += size;
64     scon->iov_bs += size;
65     scon->event.event_pending = true;
66 }
67 
68 /* Send data from a char device over to the guest */
69 static void chr_read(void *opaque, const uint8_t *buf, int size)
70 {
71     SCLPConsole *scon = opaque;
72 
73     assert(scon);
74 
75     receive_from_chr_layer(scon, buf, size);
76     /* trigger SCLP read operation */
77     qemu_irq_raise(scon->irq_read_vt220);
78 }
79 
80 /* functions to be called by event facility */
81 
82 static bool can_handle_event(uint8_t type)
83 {
84     return type == SCLP_EVENT_ASCII_CONSOLE_DATA;
85 }
86 
87 static unsigned int send_mask(void)
88 {
89     return SCLP_EVENT_MASK_MSG_ASCII;
90 }
91 
92 static unsigned int receive_mask(void)
93 {
94     return SCLP_EVENT_MASK_MSG_ASCII;
95 }
96 
97 /* triggered by SCLP's read_event_data -
98  * copy console data byte-stream into provided (SCLP) buffer
99  */
100 static void get_console_data(SCLPEvent *event, uint8_t *buf, size_t *size,
101                              int avail)
102 {
103     SCLPConsole *cons = DO_UPCAST(SCLPConsole, event, event);
104 
105     /* first byte is hex 0 saying an ascii string follows */
106     *buf++ = '\0';
107     avail--;
108     /* if all data fit into provided SCLP buffer */
109     if (avail >= cons->iov_sclp_rest) {
110         /* copy character byte-stream to SCLP buffer */
111         memcpy(buf, &cons->iov[cons->iov_sclp], cons->iov_sclp_rest);
112         *size = cons->iov_sclp_rest + 1;
113         cons->iov_sclp = 0;
114         cons->iov_bs = 0;
115         cons->iov_data_len = 0;
116         cons->iov_sclp_rest = 0;
117         event->event_pending = false;
118         /* data provided and no more data pending */
119     } else {
120         /* if provided buffer is too small, just copy part */
121         memcpy(buf, &cons->iov[cons->iov_sclp], avail);
122         *size = avail + 1;
123         cons->iov_sclp_rest -= avail;
124         cons->iov_sclp += avail;
125         /* more data pending */
126     }
127 }
128 
129 static int read_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr,
130                            int *slen)
131 {
132     int avail;
133     size_t src_len;
134     uint8_t *to;
135     ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
136 
137     if (!event->event_pending) {
138         /* no data pending */
139         return 0;
140     }
141 
142     to = (uint8_t *)&acd->data;
143     avail = *slen - sizeof(ASCIIConsoleData);
144     get_console_data(event, to, &src_len, avail);
145 
146     acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len);
147     acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
148     acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED;
149     *slen = avail - src_len;
150 
151     return 1;
152 }
153 
154 /* triggered by SCLP's write_event_data
155  *  - write console data to character layer
156  *  returns < 0 if an error occurred
157  */
158 static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf,
159                                   size_t len)
160 {
161     SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event);
162 
163     if (!scon->chr) {
164         /* If there's no backend, we can just say we consumed all data. */
165         return len;
166     }
167 
168     return qemu_chr_fe_write_all(scon->chr, buf, len);
169 }
170 
171 static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
172 {
173     int rc;
174     int length;
175     ssize_t written;
176     ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr;
177 
178     length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader);
179     written = write_console_data(event, (uint8_t *)acd->data, length);
180 
181     rc = SCLP_RC_NORMAL_COMPLETION;
182     /* set event buffer accepted flag */
183     evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED;
184 
185     /* written will be zero if a pty is not connected - don't treat as error */
186     if (written < 0) {
187         /* event buffer not accepted due to error in character layer */
188         evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED);
189         rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK;
190     }
191 
192     return rc;
193 }
194 
195 static void trigger_ascii_console_data(void *opaque, int n, int level)
196 {
197     sclp_service_interrupt(0);
198 }
199 
200 static const VMStateDescription vmstate_sclpconsole = {
201     .name = "sclpconsole",
202     .version_id = 0,
203     .minimum_version_id = 0,
204     .minimum_version_id_old = 0,
205     .fields      = (VMStateField[]) {
206         VMSTATE_BOOL(event.event_pending, SCLPConsole),
207         VMSTATE_UINT8_ARRAY(iov, SCLPConsole, SIZE_BUFFER_VT220),
208         VMSTATE_UINT32(iov_sclp, SCLPConsole),
209         VMSTATE_UINT32(iov_bs, SCLPConsole),
210         VMSTATE_UINT32(iov_data_len, SCLPConsole),
211         VMSTATE_UINT32(iov_sclp_rest, SCLPConsole),
212         VMSTATE_END_OF_LIST()
213      }
214 };
215 
216 /* qemu object creation and initialization functions */
217 
218 /* tell character layer our call-back functions */
219 
220 static int console_init(SCLPEvent *event)
221 {
222     static bool console_available;
223 
224     SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event);
225 
226     if (console_available) {
227         error_report("Multiple VT220 operator consoles are not supported");
228         return -1;
229     }
230     console_available = true;
231     if (scon->chr) {
232         qemu_chr_add_handlers(scon->chr, chr_can_read,
233                               chr_read, NULL, scon);
234     }
235     scon->irq_read_vt220 = *qemu_allocate_irqs(trigger_ascii_console_data,
236                                                NULL, 1);
237 
238     return 0;
239 }
240 
241 static void console_reset(DeviceState *dev)
242 {
243    SCLPEvent *event = SCLP_EVENT(dev);
244    SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event);
245 
246    event->event_pending = false;
247    scon->iov_sclp = 0;
248    scon->iov_bs = 0;
249    scon->iov_data_len = 0;
250    scon->iov_sclp_rest = 0;
251 }
252 
253 static int console_exit(SCLPEvent *event)
254 {
255     return 0;
256 }
257 
258 static Property console_properties[] = {
259     DEFINE_PROP_CHR("chardev", SCLPConsole, chr),
260     DEFINE_PROP_END_OF_LIST(),
261 };
262 
263 static void console_class_init(ObjectClass *klass, void *data)
264 {
265     DeviceClass *dc = DEVICE_CLASS(klass);
266     SCLPEventClass *ec = SCLP_EVENT_CLASS(klass);
267 
268     dc->props = console_properties;
269     dc->reset = console_reset;
270     dc->vmsd = &vmstate_sclpconsole;
271     ec->init = console_init;
272     ec->exit = console_exit;
273     ec->get_send_mask = send_mask;
274     ec->get_receive_mask = receive_mask;
275     ec->can_handle_event = can_handle_event;
276     ec->read_event_data = read_event_data;
277     ec->write_event_data = write_event_data;
278 }
279 
280 static const TypeInfo sclp_console_info = {
281     .name          = "sclpconsole",
282     .parent        = TYPE_SCLP_EVENT,
283     .instance_size = sizeof(SCLPConsole),
284     .class_init    = console_class_init,
285     .class_size    = sizeof(SCLPEventClass),
286 };
287 
288 static void register_types(void)
289 {
290     type_register_static(&sclp_console_info);
291 }
292 
293 type_init(register_types)
294