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/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 <asm/uaccess.h>
 
  22 #define SCLP_RW_PRINT_HEADER "sclp low level driver: "
 
  25  * The room for the SCCB (only for writing) is not equal to a pages size
 
  26  * (as it is specified as the maximum size in the SCLP documentation)
 
  27  * because of the additional data structure described above.
 
  29 #define MAX_SCCB_ROOM (PAGE_SIZE - sizeof(struct sclp_buffer))
 
  31 /* Event type structure for write message and write priority message */
 
  32 static struct sclp_register sclp_rw_event = {
 
  33         .send_mask = EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK
 
  37  * Setup a sclp write buffer. Gets a page as input (4K) and returns
 
  38  * a pointer to a struct sclp_buffer structure that is located at the
 
  39  * end of the input page. This reduces the buffer space by a few
 
  40  * bytes but simplifies things.
 
  43 sclp_make_buffer(void *page, unsigned short columns, unsigned short htab)
 
  45         struct sclp_buffer *buffer;
 
  46         struct write_sccb *sccb;
 
  48         sccb = (struct write_sccb *) page;
 
  50          * We keep the struct sclp_buffer structure at the end
 
  53         buffer = ((struct sclp_buffer *) ((addr_t) sccb + PAGE_SIZE)) - 1;
 
  55         buffer->retry_count = 0;
 
  56         buffer->mto_number = 0;
 
  57         buffer->mto_char_sum = 0;
 
  58         buffer->current_line = NULL;
 
  59         buffer->current_length = 0;
 
  60         buffer->columns = columns;
 
  64         memset(sccb, 0, sizeof(struct write_sccb));
 
  65         sccb->header.length = sizeof(struct write_sccb);
 
  66         sccb->msg_buf.header.length = sizeof(struct msg_buf);
 
  67         sccb->msg_buf.header.type = EVTYP_MSG;
 
  68         sccb->msg_buf.mdb.header.length = sizeof(struct mdb);
 
  69         sccb->msg_buf.mdb.header.type = 1;
 
  70         sccb->msg_buf.mdb.header.tag = 0xD4C4C240;      /* ebcdic "MDB " */
 
  71         sccb->msg_buf.mdb.header.revision_code = 1;
 
  72         sccb->msg_buf.mdb.go.length = sizeof(struct go);
 
  73         sccb->msg_buf.mdb.go.type = 1;
 
  79  * Return a pointer to the original page that has been used to create
 
  83 sclp_unmake_buffer(struct sclp_buffer *buffer)
 
  89  * Initialize a new Message Text Object (MTO) at the end of the provided buffer
 
  90  * with enough room for max_len characters. Return 0 on success.
 
  93 sclp_initialize_mto(struct sclp_buffer *buffer, int max_len)
 
  95         struct write_sccb *sccb;
 
  99         /* max size of new Message Text Object including message text  */
 
 100         mto_size = sizeof(struct mto) + max_len;
 
 102         /* check if current buffer sccb can contain the mto */
 
 104         if ((MAX_SCCB_ROOM - sccb->header.length) < mto_size)
 
 107         /* find address of new message text object */
 
 108         mto = (struct mto *)(((addr_t) sccb) + sccb->header.length);
 
 111          * fill the new Message-Text Object,
 
 112          * starting behind the former last byte of the SCCB
 
 114         memset(mto, 0, sizeof(struct mto));
 
 115         mto->length = sizeof(struct mto);
 
 116         mto->type = 4;  /* message text object */
 
 117         mto->line_type_flags = LNTPFLGS_ENDTEXT; /* end text */
 
 119         /* set pointer to first byte after struct mto. */
 
 120         buffer->current_line = (char *) (mto + 1);
 
 121         buffer->current_length = 0;
 
 127  * Finalize MTO initialized by sclp_initialize_mto(), updating the sizes of
 
 128  * MTO, enclosing MDB, event buffer and SCCB.
 
 131 sclp_finalize_mto(struct sclp_buffer *buffer)
 
 133         struct write_sccb *sccb;
 
 135         int str_len, mto_size;
 
 137         str_len = buffer->current_length;
 
 138         buffer->current_line = NULL;
 
 139         buffer->current_length = 0;
 
 141         /* real size of new Message Text Object including message text  */
 
 142         mto_size = sizeof(struct mto) + str_len;
 
 144         /* find address of new message text object */
 
 146         mto = (struct mto *)(((addr_t) sccb) + sccb->header.length);
 
 148         /* set size of message text object */
 
 149         mto->length = mto_size;
 
 152          * update values of sizes
 
 153          * (SCCB, Event(Message) Buffer, Message Data Block)
 
 155         sccb->header.length += mto_size;
 
 156         sccb->msg_buf.header.length += mto_size;
 
 157         sccb->msg_buf.mdb.header.length += mto_size;
 
 160          * count number of buffered messages (= number of Message Text
 
 161          * Objects) and number of buffered characters
 
 162          * for the SCCB currently used for buffering and at all
 
 164         buffer->mto_number++;
 
 165         buffer->mto_char_sum += str_len;
 
 169  * processing of a message including escape characters,
 
 170  * returns number of characters written to the output sccb
 
 171  * ("processed" means that is not guaranteed that the character have already
 
 172  *  been sent to the SCLP but that it will be done at least next time the SCLP
 
 176 sclp_write(struct sclp_buffer *buffer, const unsigned char *msg, int count)
 
 182          * parse msg for escape sequences (\t,\v ...) and put formated
 
 183          * msg into an mto (created by sclp_initialize_mto).
 
 185          * We have to do this work ourselfs because there is no support for
 
 186          * these characters on the native machine and only partial support
 
 187          * under VM (Why does VM interpret \n but the native machine doesn't ?)
 
 189          * Depending on i/o-control setting the message is always written
 
 190          * immediately or we wait for a final new line maybe coming with the
 
 191          * next message. Besides we avoid a buffer overrun by writing its
 
 196          * \r and \b work within one line because we are not able to modify
 
 197          * previous output that have already been accepted by the SCLP.
 
 199          * \t combined with following \r is not correctly represented because
 
 200          * \t is expanded to some spaces but \r does not know about a
 
 201          * previous \t and decreases the current position by one column.
 
 202          * This is in order to a slim and quick implementation.
 
 204         for (i_msg = 0; i_msg < count; i_msg++) {
 
 205                 switch (msg[i_msg]) {
 
 206                 case '\n':      /* new line, line feed (ASCII)  */
 
 207                         /* check if new mto needs to be created */
 
 208                         if (buffer->current_line == NULL) {
 
 209                                 rc = sclp_initialize_mto(buffer, 0);
 
 213                         sclp_finalize_mto(buffer);
 
 215                 case '\a':      /* bell, one for several times  */
 
 216                         /* set SCLP sound alarm bit in General Object */
 
 217                         buffer->sccb->msg_buf.mdb.go.general_msg_flags |=
 
 220                 case '\t':      /* horizontal tabulator  */
 
 221                         /* check if new mto needs to be created */
 
 222                         if (buffer->current_line == NULL) {
 
 223                                 rc = sclp_initialize_mto(buffer,
 
 228                         /* "go to (next htab-boundary + 1, same line)" */
 
 230                                 if (buffer->current_length >= buffer->columns)
 
 232                                 /* ok, add a blank */
 
 233                                 *buffer->current_line++ = 0x40;
 
 234                                 buffer->current_length++;
 
 235                         } while (buffer->current_length % buffer->htab);
 
 237                 case '\f':      /* form feed  */
 
 238                 case '\v':      /* vertical tabulator  */
 
 239                         /* "go to (actual column, actual line + 1)" */
 
 240                         /* = new line, leading spaces */
 
 241                         if (buffer->current_line != NULL) {
 
 242                                 spaces = buffer->current_length;
 
 243                                 sclp_finalize_mto(buffer);
 
 244                                 rc = sclp_initialize_mto(buffer,
 
 248                                 memset(buffer->current_line, 0x40, spaces);
 
 249                                 buffer->current_line += spaces;
 
 250                                 buffer->current_length = spaces;
 
 252                                 /* one an empty line this is the same as \n */
 
 253                                 rc = sclp_initialize_mto(buffer,
 
 257                                 sclp_finalize_mto(buffer);
 
 260                 case '\b':      /* backspace  */
 
 261                         /* "go to (actual column - 1, actual line)" */
 
 262                         /* decrement counter indicating position, */
 
 263                         /* do not remove last character */
 
 264                         if (buffer->current_line != NULL &&
 
 265                             buffer->current_length > 0) {
 
 266                                 buffer->current_length--;
 
 267                                 buffer->current_line--;
 
 270                 case 0x00:      /* end of string  */
 
 271                         /* transfer current line to SCCB */
 
 272                         if (buffer->current_line != NULL)
 
 273                                 sclp_finalize_mto(buffer);
 
 274                         /* skip the rest of the message including the 0 byte */
 
 277                 default:        /* no escape character  */
 
 278                         /* do not output unprintable characters */
 
 279                         if (!isprint(msg[i_msg]))
 
 281                         /* check if new mto needs to be created */
 
 282                         if (buffer->current_line == NULL) {
 
 283                                 rc = sclp_initialize_mto(buffer,
 
 288                         *buffer->current_line++ = sclp_ascebc(msg[i_msg]);
 
 289                         buffer->current_length++;
 
 292                 /* check if current mto is full */
 
 293                 if (buffer->current_line != NULL &&
 
 294                     buffer->current_length >= buffer->columns)
 
 295                         sclp_finalize_mto(buffer);
 
 298         /* return number of processed characters */
 
 303  * Return the number of free bytes in the sccb
 
 306 sclp_buffer_space(struct sclp_buffer *buffer)
 
 310         count = MAX_SCCB_ROOM - buffer->sccb->header.length;
 
 311         if (buffer->current_line != NULL)
 
 312                 count -= sizeof(struct mto) + buffer->current_length;
 
 317  * Return number of characters in buffer
 
 320 sclp_chars_in_buffer(struct sclp_buffer *buffer)
 
 324         count = buffer->mto_char_sum;
 
 325         if (buffer->current_line != NULL)
 
 326                 count += buffer->current_length;
 
 331  * sets or provides some values that influence the drivers behaviour
 
 334 sclp_set_columns(struct sclp_buffer *buffer, unsigned short columns)
 
 336         buffer->columns = columns;
 
 337         if (buffer->current_line != NULL &&
 
 338             buffer->current_length > buffer->columns)
 
 339                 sclp_finalize_mto(buffer);
 
 343 sclp_set_htab(struct sclp_buffer *buffer, unsigned short htab)
 
 349  * called by sclp_console_init and/or sclp_tty_init
 
 354         static int init_done = 0;
 
 360         rc = sclp_register(&sclp_rw_event);
 
 366 #define SCLP_BUFFER_MAX_RETRY           1
 
 369  * second half of Write Event Data-function that has to be done after
 
 370  * interruption indicating completion of Service Call.
 
 373 sclp_writedata_callback(struct sclp_req *request, void *data)
 
 376         struct sclp_buffer *buffer;
 
 377         struct write_sccb *sccb;
 
 379         buffer = (struct sclp_buffer *) data;
 
 382         if (request->status == SCLP_REQ_FAILED) {
 
 383                 if (buffer->callback != NULL)
 
 384                         buffer->callback(buffer, -EIO);
 
 387         /* check SCLP response code and choose suitable action  */
 
 388         switch (sccb->header.response_code) {
 
 390                 /* Normal completion, buffer processed, message(s) sent */
 
 394         case 0x0340: /* Contained SCLP equipment check */
 
 395                 if (++buffer->retry_count > SCLP_BUFFER_MAX_RETRY) {
 
 399                 /* remove processed buffers and requeue rest */
 
 400                 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
 
 401                         /* not all buffers were processed */
 
 402                         sccb->header.response_code = 0x0000;
 
 403                         buffer->request.status = SCLP_REQ_FILLED;
 
 404                         rc = sclp_add_request(request);
 
 411         case 0x0040: /* SCLP equipment check */
 
 412         case 0x05f0: /* Target resource in improper state */
 
 413                 if (++buffer->retry_count > SCLP_BUFFER_MAX_RETRY) {
 
 418                 sccb->header.response_code = 0x0000;
 
 419                 buffer->request.status = SCLP_REQ_FILLED;
 
 420                 rc = sclp_add_request(request);
 
 425                 if (sccb->header.response_code == 0x71f0)
 
 431         if (buffer->callback != NULL)
 
 432                 buffer->callback(buffer, rc);
 
 436  * Setup the request structure in the struct sclp_buffer to do SCLP Write
 
 437  * Event Data and pass the request to the core SCLP loop. Return zero on
 
 438  * success, non-zero otherwise.
 
 441 sclp_emit_buffer(struct sclp_buffer *buffer,
 
 442                  void (*callback)(struct sclp_buffer *, int))
 
 444         struct write_sccb *sccb;
 
 446         /* add current line if there is one */
 
 447         if (buffer->current_line != NULL)
 
 448                 sclp_finalize_mto(buffer);
 
 450         /* Are there messages in the output buffer ? */
 
 451         if (buffer->mto_number == 0)
 
 455         if (sclp_rw_event.sclp_receive_mask & EVTYP_MSG_MASK)
 
 456                 /* Use normal write message */
 
 457                 sccb->msg_buf.header.type = EVTYP_MSG;
 
 458         else if (sclp_rw_event.sclp_receive_mask & EVTYP_PMSGCMD_MASK)
 
 459                 /* Use write priority message */
 
 460                 sccb->msg_buf.header.type = EVTYP_PMSGCMD;
 
 463         buffer->request.command = SCLP_CMDW_WRITE_EVENT_DATA;
 
 464         buffer->request.status = SCLP_REQ_FILLED;
 
 465         buffer->request.callback = sclp_writedata_callback;
 
 466         buffer->request.callback_data = buffer;
 
 467         buffer->request.sccb = sccb;
 
 468         buffer->callback = callback;
 
 469         return sclp_add_request(&buffer->request);