1 // SPDX-License-Identifier: GPL-2.0
3 * driver: reading from and writing to system console on S/390 via SCLP
5 * Copyright IBM Corp. 1999, 2009
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 #include <linux/kmod.h>
12 #include <linux/types.h>
13 #include <linux/err.h>
14 #include <linux/string.h>
15 #include <linux/spinlock.h>
16 #include <linux/ctype.h>
17 #include <linux/uaccess.h>
23 * The room for the SCCB (only for writing) is not equal to a pages size
24 * (as it is specified as the maximum size in the SCLP documentation)
25 * because of the additional data structure described above.
27 #define MAX_SCCB_ROOM (PAGE_SIZE - sizeof(struct sclp_buffer))
29 static void sclp_rw_pm_event(struct sclp_register
*reg
,
30 enum sclp_pm_event sclp_pm_event
)
32 sclp_console_pm_event(sclp_pm_event
);
35 /* Event type structure for write message and write priority message */
36 static struct sclp_register sclp_rw_event
= {
37 .send_mask
= EVTYP_MSG_MASK
,
38 .pm_event_fn
= sclp_rw_pm_event
,
42 * Setup a sclp write buffer. Gets a page as input (4K) and returns
43 * a pointer to a struct sclp_buffer structure that is located at the
44 * end of the input page. This reduces the buffer space by a few
45 * bytes but simplifies things.
48 sclp_make_buffer(void *page
, unsigned short columns
, unsigned short htab
)
50 struct sclp_buffer
*buffer
;
51 struct sccb_header
*sccb
;
53 sccb
= (struct sccb_header
*) page
;
55 * We keep the struct sclp_buffer structure at the end
58 buffer
= ((struct sclp_buffer
*) ((addr_t
) sccb
+ PAGE_SIZE
)) - 1;
60 buffer
->retry_count
= 0;
63 buffer
->current_line
= NULL
;
64 buffer
->current_length
= 0;
65 buffer
->columns
= columns
;
69 memset(sccb
, 0, sizeof(struct sccb_header
));
70 sccb
->length
= sizeof(struct sccb_header
);
76 * Return a pointer to the original page that has been used to create
80 sclp_unmake_buffer(struct sclp_buffer
*buffer
)
86 * Initialize a new message the end of the provided buffer with
87 * enough room for max_len characters. Return 0 on success.
90 sclp_initialize_mto(struct sclp_buffer
*buffer
, int max_len
)
92 struct sccb_header
*sccb
;
99 /* max size of new message including message text */
100 msg_size
= sizeof(struct msg_buf
) + max_len
;
102 /* check if current buffer sccb can contain the mto */
104 if ((MAX_SCCB_ROOM
- sccb
->length
) < msg_size
)
107 msg
= (struct msg_buf
*)((addr_t
) sccb
+ sccb
->length
);
108 memset(msg
, 0, sizeof(struct msg_buf
));
109 msg
->header
.length
= sizeof(struct msg_buf
);
110 msg
->header
.type
= EVTYP_MSG
;
113 mdb
->header
.length
= sizeof(struct mdb
);
114 mdb
->header
.type
= 1;
115 mdb
->header
.tag
= 0xD4C4C240; /* ebcdic "MDB " */
116 mdb
->header
.revision_code
= 1;
119 go
->length
= sizeof(struct go
);
123 mto
->length
= sizeof(struct mto
);
124 mto
->type
= 4; /* message text object */
125 mto
->line_type_flags
= LNTPFLGS_ENDTEXT
; /* end text */
127 /* set pointer to first byte after struct mto. */
128 buffer
->current_msg
= msg
;
129 buffer
->current_line
= (char *) (mto
+ 1);
130 buffer
->current_length
= 0;
136 * Finalize message initialized by sclp_initialize_mto(),
137 * updating the sizes of MTO, enclosing MDB, event buffer and SCCB.
140 sclp_finalize_mto(struct sclp_buffer
*buffer
)
142 struct sccb_header
*sccb
;
146 * update values of sizes
147 * (SCCB, Event(Message) Buffer, Message Data Block)
150 msg
= buffer
->current_msg
;
151 msg
->header
.length
+= buffer
->current_length
;
152 msg
->mdb
.header
.length
+= buffer
->current_length
;
153 msg
->mdb
.mto
.length
+= buffer
->current_length
;
154 sccb
->length
+= msg
->header
.length
;
157 * count number of buffered messages (= number of Message Text
158 * Objects) and number of buffered characters
159 * for the SCCB currently used for buffering and at all
162 buffer
->char_sum
+= buffer
->current_length
;
164 buffer
->current_line
= NULL
;
165 buffer
->current_length
= 0;
166 buffer
->current_msg
= NULL
;
170 * processing of a message including escape characters,
171 * returns number of characters written to the output sccb
172 * ("processed" means that is not guaranteed that the character have already
173 * been sent to the SCLP but that it will be done at least next time the SCLP
177 sclp_write(struct sclp_buffer
*buffer
, const unsigned char *msg
, int count
)
183 * parse msg for escape sequences (\t,\v ...) and put formated
184 * msg into an mto (created by sclp_initialize_mto).
186 * We have to do this work ourselfs because there is no support for
187 * these characters on the native machine and only partial support
188 * under VM (Why does VM interpret \n but the native machine doesn't ?)
190 * Depending on i/o-control setting the message is always written
191 * immediately or we wait for a final new line maybe coming with the
192 * next message. Besides we avoid a buffer overrun by writing its
197 * \r and \b work within one line because we are not able to modify
198 * previous output that have already been accepted by the SCLP.
200 * \t combined with following \r is not correctly represented because
201 * \t is expanded to some spaces but \r does not know about a
202 * previous \t and decreases the current position by one column.
203 * This is in order to a slim and quick implementation.
205 for (i_msg
= 0; i_msg
< count
; i_msg
++) {
206 switch (msg
[i_msg
]) {
207 case '\n': /* new line, line feed (ASCII) */
208 /* check if new mto needs to be created */
209 if (buffer
->current_line
== NULL
) {
210 rc
= sclp_initialize_mto(buffer
, 0);
214 sclp_finalize_mto(buffer
);
216 case '\a': /* bell, one for several times */
217 /* set SCLP sound alarm bit in General Object */
218 if (buffer
->current_line
== NULL
) {
219 rc
= sclp_initialize_mto(buffer
,
224 buffer
->current_msg
->mdb
.go
.general_msg_flags
|=
227 case '\t': /* horizontal tabulator */
228 /* check if new mto needs to be created */
229 if (buffer
->current_line
== NULL
) {
230 rc
= sclp_initialize_mto(buffer
,
235 /* "go to (next htab-boundary + 1, same line)" */
237 if (buffer
->current_length
>= buffer
->columns
)
239 /* ok, add a blank */
240 *buffer
->current_line
++ = 0x40;
241 buffer
->current_length
++;
242 } while (buffer
->current_length
% buffer
->htab
);
244 case '\f': /* form feed */
245 case '\v': /* vertical tabulator */
246 /* "go to (actual column, actual line + 1)" */
247 /* = new line, leading spaces */
248 if (buffer
->current_line
!= NULL
) {
249 spaces
= buffer
->current_length
;
250 sclp_finalize_mto(buffer
);
251 rc
= sclp_initialize_mto(buffer
,
255 memset(buffer
->current_line
, 0x40, spaces
);
256 buffer
->current_line
+= spaces
;
257 buffer
->current_length
= spaces
;
259 /* one an empty line this is the same as \n */
260 rc
= sclp_initialize_mto(buffer
,
264 sclp_finalize_mto(buffer
);
267 case '\b': /* backspace */
268 /* "go to (actual column - 1, actual line)" */
269 /* decrement counter indicating position, */
270 /* do not remove last character */
271 if (buffer
->current_line
!= NULL
&&
272 buffer
->current_length
> 0) {
273 buffer
->current_length
--;
274 buffer
->current_line
--;
277 case 0x00: /* end of string */
278 /* transfer current line to SCCB */
279 if (buffer
->current_line
!= NULL
)
280 sclp_finalize_mto(buffer
);
281 /* skip the rest of the message including the 0 byte */
284 default: /* no escape character */
285 /* do not output unprintable characters */
286 if (!isprint(msg
[i_msg
]))
288 /* check if new mto needs to be created */
289 if (buffer
->current_line
== NULL
) {
290 rc
= sclp_initialize_mto(buffer
,
295 *buffer
->current_line
++ = sclp_ascebc(msg
[i_msg
]);
296 buffer
->current_length
++;
299 /* check if current mto is full */
300 if (buffer
->current_line
!= NULL
&&
301 buffer
->current_length
>= buffer
->columns
)
302 sclp_finalize_mto(buffer
);
305 /* return number of processed characters */
310 * Return the number of free bytes in the sccb
313 sclp_buffer_space(struct sclp_buffer
*buffer
)
315 struct sccb_header
*sccb
;
319 count
= MAX_SCCB_ROOM
- sccb
->length
;
320 if (buffer
->current_line
!= NULL
)
321 count
-= sizeof(struct msg_buf
) + buffer
->current_length
;
326 * Return number of characters in buffer
329 sclp_chars_in_buffer(struct sclp_buffer
*buffer
)
333 count
= buffer
->char_sum
;
334 if (buffer
->current_line
!= NULL
)
335 count
+= buffer
->current_length
;
340 * called by sclp_console_init and/or sclp_tty_init
345 static int init_done
= 0;
351 rc
= sclp_register(&sclp_rw_event
);
357 #define SCLP_BUFFER_MAX_RETRY 1
360 * second half of Write Event Data-function that has to be done after
361 * interruption indicating completion of Service Call.
364 sclp_writedata_callback(struct sclp_req
*request
, void *data
)
367 struct sclp_buffer
*buffer
;
368 struct sccb_header
*sccb
;
370 buffer
= (struct sclp_buffer
*) data
;
373 if (request
->status
== SCLP_REQ_FAILED
) {
374 if (buffer
->callback
!= NULL
)
375 buffer
->callback(buffer
, -EIO
);
378 /* check SCLP response code and choose suitable action */
379 switch (sccb
->response_code
) {
381 /* Normal completion, buffer processed, message(s) sent */
385 case 0x0340: /* Contained SCLP equipment check */
386 if (++buffer
->retry_count
> SCLP_BUFFER_MAX_RETRY
) {
390 /* remove processed buffers and requeue rest */
391 if (sclp_remove_processed((struct sccb_header
*) sccb
) > 0) {
392 /* not all buffers were processed */
393 sccb
->response_code
= 0x0000;
394 buffer
->request
.status
= SCLP_REQ_FILLED
;
395 rc
= sclp_add_request(request
);
402 case 0x0040: /* SCLP equipment check */
403 case 0x05f0: /* Target resource in improper state */
404 if (++buffer
->retry_count
> SCLP_BUFFER_MAX_RETRY
) {
409 sccb
->response_code
= 0x0000;
410 buffer
->request
.status
= SCLP_REQ_FILLED
;
411 rc
= sclp_add_request(request
);
416 if (sccb
->response_code
== 0x71f0)
422 if (buffer
->callback
!= NULL
)
423 buffer
->callback(buffer
, rc
);
427 * Setup the request structure in the struct sclp_buffer to do SCLP Write
428 * Event Data and pass the request to the core SCLP loop. Return zero on
429 * success, non-zero otherwise.
432 sclp_emit_buffer(struct sclp_buffer
*buffer
,
433 void (*callback
)(struct sclp_buffer
*, int))
435 /* add current line if there is one */
436 if (buffer
->current_line
!= NULL
)
437 sclp_finalize_mto(buffer
);
439 /* Are there messages in the output buffer ? */
440 if (buffer
->messages
== 0)
443 buffer
->request
.command
= SCLP_CMDW_WRITE_EVENT_DATA
;
444 buffer
->request
.status
= SCLP_REQ_FILLED
;
445 buffer
->request
.callback
= sclp_writedata_callback
;
446 buffer
->request
.callback_data
= buffer
;
447 buffer
->request
.sccb
= buffer
->sccb
;
448 buffer
->callback
= callback
;
449 return sclp_add_request(&buffer
->request
);