2 * drivers/s390/char/sclp_rw.c
3 * driver: reading from and writing to system console on S/390 via SCLP
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 #include <linux/config.h>
12 #include <linux/kmod.h>
13 #include <linux/types.h>
14 #include <linux/err.h>
15 #include <linux/string.h>
16 #include <linux/spinlock.h>
17 #include <linux/ctype.h>
18 #include <asm/uaccess.h>
23 #define SCLP_RW_PRINT_HEADER "sclp low level driver: "
26 * The room for the SCCB (only for writing) is not equal to a pages size
27 * (as it is specified as the maximum size in the the SCLP ducumentation)
28 * because of the additional data structure described above.
30 #define MAX_SCCB_ROOM (PAGE_SIZE - sizeof(struct sclp_buffer))
32 /* Event type structure for write message and write priority message */
33 static struct sclp_register sclp_rw_event
= {
34 .send_mask
= EvTyp_Msg_Mask
| EvTyp_PMsgCmd_Mask
38 * Setup a sclp write buffer. Gets a page as input (4K) and returns
39 * a pointer to a struct sclp_buffer structure that is located at the
40 * end of the input page. This reduces the buffer space by a few
41 * bytes but simplifies things.
44 sclp_make_buffer(void *page
, unsigned short columns
, unsigned short htab
)
46 struct sclp_buffer
*buffer
;
47 struct write_sccb
*sccb
;
49 sccb
= (struct write_sccb
*) page
;
51 * We keep the struct sclp_buffer structure at the end
54 buffer
= ((struct sclp_buffer
*) ((addr_t
) sccb
+ PAGE_SIZE
)) - 1;
56 buffer
->retry_count
= 0;
57 init_timer(&buffer
->retry_timer
);
58 buffer
->mto_number
= 0;
59 buffer
->mto_char_sum
= 0;
60 buffer
->current_line
= NULL
;
61 buffer
->current_length
= 0;
62 buffer
->columns
= columns
;
66 memset(sccb
, 0, sizeof(struct write_sccb
));
67 sccb
->header
.length
= sizeof(struct write_sccb
);
68 sccb
->msg_buf
.header
.length
= sizeof(struct msg_buf
);
69 sccb
->msg_buf
.header
.type
= EvTyp_Msg
;
70 sccb
->msg_buf
.mdb
.header
.length
= sizeof(struct mdb
);
71 sccb
->msg_buf
.mdb
.header
.type
= 1;
72 sccb
->msg_buf
.mdb
.header
.tag
= 0xD4C4C240; /* ebcdic "MDB " */
73 sccb
->msg_buf
.mdb
.header
.revision_code
= 1;
74 sccb
->msg_buf
.mdb
.go
.length
= sizeof(struct go
);
75 sccb
->msg_buf
.mdb
.go
.type
= 1;
81 * Return a pointer to the orignal page that has been used to create
85 sclp_unmake_buffer(struct sclp_buffer
*buffer
)
91 * Initialize a new Message Text Object (MTO) at the end of the provided buffer
92 * with enough room for max_len characters. Return 0 on success.
95 sclp_initialize_mto(struct sclp_buffer
*buffer
, int max_len
)
97 struct write_sccb
*sccb
;
101 /* max size of new Message Text Object including message text */
102 mto_size
= sizeof(struct mto
) + max_len
;
104 /* check if current buffer sccb can contain the mto */
106 if ((MAX_SCCB_ROOM
- sccb
->header
.length
) < mto_size
)
109 /* find address of new message text object */
110 mto
= (struct mto
*)(((addr_t
) sccb
) + sccb
->header
.length
);
113 * fill the new Message-Text Object,
114 * starting behind the former last byte of the SCCB
116 memset(mto
, 0, sizeof(struct mto
));
117 mto
->length
= sizeof(struct mto
);
118 mto
->type
= 4; /* message text object */
119 mto
->line_type_flags
= LnTpFlgs_EndText
; /* end text */
121 /* set pointer to first byte after struct mto. */
122 buffer
->current_line
= (char *) (mto
+ 1);
123 buffer
->current_length
= 0;
129 * Finalize MTO initialized by sclp_initialize_mto(), updating the sizes of
130 * MTO, enclosing MDB, event buffer and SCCB.
133 sclp_finalize_mto(struct sclp_buffer
*buffer
)
135 struct write_sccb
*sccb
;
137 int str_len
, mto_size
;
139 str_len
= buffer
->current_length
;
140 buffer
->current_line
= NULL
;
141 buffer
->current_length
= 0;
143 /* real size of new Message Text Object including message text */
144 mto_size
= sizeof(struct mto
) + str_len
;
146 /* find address of new message text object */
148 mto
= (struct mto
*)(((addr_t
) sccb
) + sccb
->header
.length
);
150 /* set size of message text object */
151 mto
->length
= mto_size
;
154 * update values of sizes
155 * (SCCB, Event(Message) Buffer, Message Data Block)
157 sccb
->header
.length
+= mto_size
;
158 sccb
->msg_buf
.header
.length
+= mto_size
;
159 sccb
->msg_buf
.mdb
.header
.length
+= mto_size
;
162 * count number of buffered messages (= number of Message Text
163 * Objects) and number of buffered characters
164 * for the SCCB currently used for buffering and at all
166 buffer
->mto_number
++;
167 buffer
->mto_char_sum
+= str_len
;
171 * processing of a message including escape characters,
172 * returns number of characters written to the output sccb
173 * ("processed" means that is not guaranteed that the character have already
174 * been sent to the SCLP but that it will be done at least next time the SCLP
178 sclp_write(struct sclp_buffer
*buffer
, const unsigned char *msg
, int count
)
184 * parse msg for escape sequences (\t,\v ...) and put formated
185 * msg into an mto (created by sclp_initialize_mto).
187 * We have to do this work ourselfs because there is no support for
188 * these characters on the native machine and only partial support
189 * under VM (Why does VM interpret \n but the native machine doesn't ?)
191 * Depending on i/o-control setting the message is always written
192 * immediately or we wait for a final new line maybe coming with the
193 * next message. Besides we avoid a buffer overrun by writing its
198 * \r and \b work within one line because we are not able to modify
199 * previous output that have already been accepted by the SCLP.
201 * \t combined with following \r is not correctly represented because
202 * \t is expanded to some spaces but \r does not know about a
203 * previous \t and decreases the current position by one column.
204 * This is in order to a slim and quick implementation.
206 for (i_msg
= 0; i_msg
< count
; i_msg
++) {
207 switch (msg
[i_msg
]) {
208 case '\n': /* new line, line feed (ASCII) */
209 /* check if new mto needs to be created */
210 if (buffer
->current_line
== NULL
) {
211 rc
= sclp_initialize_mto(buffer
, 0);
215 sclp_finalize_mto(buffer
);
217 case '\a': /* bell, one for several times */
218 /* set SCLP sound alarm bit in General Object */
219 buffer
->sccb
->msg_buf
.mdb
.go
.general_msg_flags
|=
222 case '\t': /* horizontal tabulator */
223 /* check if new mto needs to be created */
224 if (buffer
->current_line
== NULL
) {
225 rc
= sclp_initialize_mto(buffer
,
230 /* "go to (next htab-boundary + 1, same line)" */
232 if (buffer
->current_length
>= buffer
->columns
)
234 /* ok, add a blank */
235 *buffer
->current_line
++ = 0x40;
236 buffer
->current_length
++;
237 } while (buffer
->current_length
% buffer
->htab
);
239 case '\f': /* form feed */
240 case '\v': /* vertical tabulator */
241 /* "go to (actual column, actual line + 1)" */
242 /* = new line, leading spaces */
243 if (buffer
->current_line
!= NULL
) {
244 spaces
= buffer
->current_length
;
245 sclp_finalize_mto(buffer
);
246 rc
= sclp_initialize_mto(buffer
,
250 memset(buffer
->current_line
, 0x40, spaces
);
251 buffer
->current_line
+= spaces
;
252 buffer
->current_length
= spaces
;
254 /* one an empty line this is the same as \n */
255 rc
= sclp_initialize_mto(buffer
,
259 sclp_finalize_mto(buffer
);
262 case '\b': /* backspace */
263 /* "go to (actual column - 1, actual line)" */
264 /* decrement counter indicating position, */
265 /* do not remove last character */
266 if (buffer
->current_line
!= NULL
&&
267 buffer
->current_length
> 0) {
268 buffer
->current_length
--;
269 buffer
->current_line
--;
272 case 0x00: /* end of string */
273 /* transfer current line to SCCB */
274 if (buffer
->current_line
!= NULL
)
275 sclp_finalize_mto(buffer
);
276 /* skip the rest of the message including the 0 byte */
279 default: /* no escape character */
280 /* do not output unprintable characters */
281 if (!isprint(msg
[i_msg
]))
283 /* check if new mto needs to be created */
284 if (buffer
->current_line
== NULL
) {
285 rc
= sclp_initialize_mto(buffer
,
290 *buffer
->current_line
++ = sclp_ascebc(msg
[i_msg
]);
291 buffer
->current_length
++;
294 /* check if current mto is full */
295 if (buffer
->current_line
!= NULL
&&
296 buffer
->current_length
>= buffer
->columns
)
297 sclp_finalize_mto(buffer
);
300 /* return number of processed characters */
305 * Return the number of free bytes in the sccb
308 sclp_buffer_space(struct sclp_buffer
*buffer
)
312 count
= MAX_SCCB_ROOM
- buffer
->sccb
->header
.length
;
313 if (buffer
->current_line
!= NULL
)
314 count
-= sizeof(struct mto
) + buffer
->current_length
;
319 * Return number of characters in buffer
322 sclp_chars_in_buffer(struct sclp_buffer
*buffer
)
326 count
= buffer
->mto_char_sum
;
327 if (buffer
->current_line
!= NULL
)
328 count
+= buffer
->current_length
;
333 * sets or provides some values that influence the drivers behaviour
336 sclp_set_columns(struct sclp_buffer
*buffer
, unsigned short columns
)
338 buffer
->columns
= columns
;
339 if (buffer
->current_line
!= NULL
&&
340 buffer
->current_length
> buffer
->columns
)
341 sclp_finalize_mto(buffer
);
345 sclp_set_htab(struct sclp_buffer
*buffer
, unsigned short htab
)
351 * called by sclp_console_init and/or sclp_tty_init
356 static int init_done
= 0;
362 rc
= sclp_register(&sclp_rw_event
);
369 sclp_buffer_retry(unsigned long data
)
371 struct sclp_buffer
*buffer
= (struct sclp_buffer
*) data
;
372 buffer
->request
.status
= SCLP_REQ_FILLED
;
373 buffer
->sccb
->header
.response_code
= 0x0000;
374 sclp_add_request(&buffer
->request
);
377 #define SCLP_BUFFER_MAX_RETRY 5
378 #define SCLP_BUFFER_RETRY_INTERVAL 2
381 * second half of Write Event Data-function that has to be done after
382 * interruption indicating completion of Service Call.
385 sclp_writedata_callback(struct sclp_req
*request
, void *data
)
388 struct sclp_buffer
*buffer
;
389 struct write_sccb
*sccb
;
391 buffer
= (struct sclp_buffer
*) data
;
394 if (request
->status
== SCLP_REQ_FAILED
) {
395 if (buffer
->callback
!= NULL
)
396 buffer
->callback(buffer
, -EIO
);
399 /* check SCLP response code and choose suitable action */
400 switch (sccb
->header
.response_code
) {
402 /* Normal completion, buffer processed, message(s) sent */
406 case 0x0340: /* Contained SCLP equipment check */
407 if (buffer
->retry_count
++ > SCLP_BUFFER_MAX_RETRY
) {
411 /* remove processed buffers and requeue rest */
412 if (sclp_remove_processed((struct sccb_header
*) sccb
) > 0) {
413 /* not all buffers were processed */
414 sccb
->header
.response_code
= 0x0000;
415 buffer
->request
.status
= SCLP_REQ_FILLED
;
416 sclp_add_request(request
);
422 case 0x0040: /* SCLP equipment check */
423 case 0x05f0: /* Target resource in improper state */
424 if (buffer
->retry_count
++ > SCLP_BUFFER_MAX_RETRY
) {
428 /* wait some time, then retry request */
429 buffer
->retry_timer
.function
= sclp_buffer_retry
;
430 buffer
->retry_timer
.data
= (unsigned long) buffer
;
431 buffer
->retry_timer
.expires
= jiffies
+
432 SCLP_BUFFER_RETRY_INTERVAL
*HZ
;
433 add_timer(&buffer
->retry_timer
);
437 if (sccb
->header
.response_code
== 0x71f0)
443 if (buffer
->callback
!= NULL
)
444 buffer
->callback(buffer
, rc
);
448 * Setup the request structure in the struct sclp_buffer to do SCLP Write
449 * Event Data and pass the request to the core SCLP loop.
452 sclp_emit_buffer(struct sclp_buffer
*buffer
,
453 void (*callback
)(struct sclp_buffer
*, int))
455 struct write_sccb
*sccb
;
457 /* add current line if there is one */
458 if (buffer
->current_line
!= NULL
)
459 sclp_finalize_mto(buffer
);
461 /* Are there messages in the output buffer ? */
462 if (buffer
->mto_number
== 0) {
463 if (callback
!= NULL
)
469 if (sclp_rw_event
.sclp_send_mask
& EvTyp_Msg_Mask
)
470 /* Use normal write message */
471 sccb
->msg_buf
.header
.type
= EvTyp_Msg
;
472 else if (sclp_rw_event
.sclp_send_mask
& EvTyp_PMsgCmd_Mask
)
473 /* Use write priority message */
474 sccb
->msg_buf
.header
.type
= EvTyp_PMsgCmd
;
476 if (callback
!= NULL
)
477 callback(buffer
, -ENOSYS
);
480 buffer
->request
.command
= SCLP_CMDW_WRITEDATA
;
481 buffer
->request
.status
= SCLP_REQ_FILLED
;
482 buffer
->request
.callback
= sclp_writedata_callback
;
483 buffer
->request
.callback_data
= buffer
;
484 buffer
->request
.sccb
= sccb
;
485 buffer
->callback
= callback
;
486 sclp_add_request(&buffer
->request
);