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