diff mbox series

[kvm-unit-tests,v2,6/6] s390x: Add linemode console

Message ID 20181205153918.29480-7-frankja@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series 390x: Add cross hypervisor and disk boot | expand

Commit Message

Janosch Frank Dec. 5, 2018, 3:39 p.m. UTC
z/VM isn't fond of vt220, so we need line mode when running under z/VM.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 lib/s390x/sclp-console.c | 199 ++++++++++++++++++++++++++++++++++++++++++-----
 lib/s390x/sclp.h         |  67 ++++++++++++++++
 2 files changed, 245 insertions(+), 21 deletions(-)

Comments

David Hildenbrand Dec. 7, 2018, 4:19 p.m. UTC | #1
On 05.12.18 16:39, Janosch Frank wrote:
> z/VM isn't fond of vt220, so we need line mode when running under z/VM.
> 
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> ---
>  lib/s390x/sclp-console.c | 199 ++++++++++++++++++++++++++++++++++++++++++-----
>  lib/s390x/sclp.h         |  67 ++++++++++++++++
>  2 files changed, 245 insertions(+), 21 deletions(-)
> 
> diff --git a/lib/s390x/sclp-console.c b/lib/s390x/sclp-console.c
> index 13ab03d..c31e178 100644
> --- a/lib/s390x/sclp-console.c
> +++ b/lib/s390x/sclp-console.c
> @@ -11,23 +11,183 @@
>  #include <libcflat.h>
>  #include <string.h>
>  #include <asm/page.h>
> +#include <asm/arch_def.h>
> +#include <asm/io.h>
>  #include "sclp.h"
>  
> -static void sclp_set_write_mask(void)
> +/*
> + * ASCII (IBM PC 437) -> EBCDIC 037
> + */
> +static uint8_t _ascebc[256] =
> +{
> + /*00 NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL */
> +     0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
> + /*08  BS    HT    LF    VT    FF    CR    SO    SI */
> + /*              ->NL                               */
> +     0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
> + /*10 DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
> +     0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
> + /*18 CAN    EM   SUB   ESC    FS    GS    RS    US */
> + /*                               ->IGS ->IRS ->IUS */
> +     0x18, 0x19, 0x3F, 0x27, 0x22, 0x1D, 0x1E, 0x1F,
> + /*20  SP     !     "     #     $     %     &     ' */
> +     0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
> + /*28   (     )     *     +     ,     -    .      / */
> +     0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
> + /*30   0     1     2     3     4     5     6     7 */
> +     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
> + /*38   8     9     :     ;     <     =     >     ? */
> +     0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
> + /*40   @     A     B     C     D     E     F     G */
> +     0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
> + /*48   H     I     J     K     L     M     N     O */
> +     0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
> + /*50   P     Q     R     S     T     U     V     W */
> +     0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
> + /*58   X     Y     Z     [     \     ]     ^     _ */
> +     0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D,
> + /*60   `     a     b     c     d     e     f     g */
> +     0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
> + /*68   h     i     j     k     l     m     n     o */
> +     0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
> + /*70   p     q     r     s     t     u     v     w */
> +     0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
> + /*78   x     y     z     {     |     }     ~    DL */
> +     0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
> + /*80*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*88*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*90*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*98*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*A0*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*A8*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*B0*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*B8*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*C0*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*C8*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*D0*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*D8*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*E0        sz						*/
> +     0x3F, 0x59, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*E8*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*F0*/
> +     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
> + /*F8*/
> +     0x90, 0x3F, 0x3F, 0x3F, 0x3F, 0xEA, 0x3F, 0xFF
> +};
> +
> +static bool initialized = false;

false is the default after your BSS clearing patch.

> +
> +static void sclp_print_ascii(const char *str)
> +{
> +	int len = strlen(str);
> +	WriteEventData *sccb = (void *)_sccb;
> +
> +	while (sclp_busy)
> +		/* Wait for SCLP request to complete */;
> +	sclp_busy = true;
> +	memset(sccb, 0, sizeof(*sccb));
> +	sccb->h.length = sizeof(WriteEventData) + len;
> +	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
> +	sccb->ebh.length = sizeof(EventBufferHeader) + len;
> +	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
> +	memcpy(sccb->data, str, len);
> +
> +	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);

Shouldn't we wait for a response? (see below)

> +}
> +
> +static void sclp_print_lm(const char *str)
> +{
> +	unsigned char *ptr, *end, ch;
> +	unsigned int count, offset, len;
> +	struct write_sccb *sccb;
> +	struct msg_buf *msg;
> +	struct mdb *mdb;
> +	struct mto *mto;
> +	struct go *go;
> +
> +	while (sclp_busy)
> +		/* Wait for SCLP request to complete */;
> +	sclp_busy = true;
> +	sccb = (struct write_sccb *) _sccb;
> +	end = (unsigned char *) sccb + 4096 - 1;
> +	memset(sccb, 0, sizeof(*sccb));
> +	ptr = (unsigned char *) &sccb->msg.mdb.mto;
> +	len = strlen(str);
> +	offset = 0;
> +	do {
> +		for (count = sizeof(*mto); offset < len; count++) {
> +			ch = str[offset++];
> +			if ((ch == 0x0a) || (ptr + count > end))
> +				break;
> +			ptr[count] = _ascebc[ch];
> +		}
> +		mto = (struct mto *) ptr;
> +		memset(mto, 0, sizeof(*mto));
> +		mto->length = count;
> +		mto->type = 4;
> +		mto->line_type_flags = LNTPFLGS_ENDTEXT;
> +		ptr += count;
> +	} while ((offset < len) && (ptr + sizeof(*mto) <= end));
> +	len = ptr - (unsigned char *) sccb;
> +	sccb->header.length = len - offsetof(struct write_sccb, header);
> +	msg = &sccb->msg;
> +	msg->header.type = EVTYP_MSG;
> +	msg->header.length = len - offsetof(struct write_sccb, msg.header);
> +	mdb = &msg->mdb;
> +	mdb->header.type = 1;
> +	mdb->header.tag = 0xD4C4C240;
> +	mdb->header.revision_code = 1;
> +	mdb->header.length = len - offsetof(struct write_sccb, msg.mdb.header);
> +	go = &mdb->go;
> +	go->length = sizeof(*go);
> +	go->type = 1;
> +	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);

Shouldn't we wait for a response? (see below)

> +}
> +
> +/*
> + * SCLP needs to be initialized by setting a send and receive mask,
> + * indicating which messages the control program (we) want(s) to
> + * send/receive.
> + */
> +static bool sclp_set_write_mask(void)
>  {
>  	WriteEventMask *sccb = (void *)_sccb;
>  
>  	while (sclp_busy)
>  		/* Wait for SCLP request to complete */;

Can this actually happen?

I'd say whoever issues a request has to wait for the response. Somebody
who didn't, doesn't have to wait.

(for multiple CPUs later on that has to be guarded by a lock in addition
to the bool)

>  	sclp_busy = true;
> +	memset(_sccb, 0, sizeof(*sccb));
>  	sccb->h.length = sizeof(WriteEventMask);
> -	sccb->mask_length = sizeof(unsigned int);
> -	sccb->receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
> -	sccb->cp_receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
> -	sccb->send_mask = SCLP_EVENT_MASK_MSG_ASCII;
> -	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII;
> +	sccb->h.function_code = 0;
> +	sccb->mask_length = sizeof(sccb_mask_t);
> +
> +	/* For now we don't process sclp input. */
> +	sccb->cp_receive_mask = 0;
> +	/* We send ASCII and line mode. */
> +	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG;
>  
>  	sclp_service_call(SCLP_CMD_WRITE_EVENT_MASK, sccb);
> +	while (sclp_busy) {
> +	}
> +	if (sccb->h.response_code != SCLP_RC_NORMAL_COMPLETION)
> +		return false;

Hmm, what do do in this case?  I don't see that the caller is fixed up
(you changed the return type from void to bool)

I "initialized" actually worth keeping around? I'd say let's just
continue as if it succeeded. (we cannot really do anything about it, can
we?)

> +	else {
> +		initialized = true;
> +		return true;
> +	}
>  }
>  
>  void sclp_console_setup(void)
> @@ -37,21 +197,18 @@ void sclp_console_setup(void)
>  
>  void sclp_print(const char *str)
>  {
> -	int len = strlen(str);
> -	WriteEventData *sccb = (void *)_sccb;
> -
> +	if (!initialized)
> +		return;
> +	/*
> +	 * z/VM advertises a vt220 console which is not functional:
> +	 * (response code 05F0, "not active because of the state of
> +	 * the machine"). Hence testing the masks would only work if
> +	 * we also use stsi data to distinguish z/VM.
> +	 *
> +	 * Let's rather print on all available consoles.

Makes sense to me.

> +	 */
> +	sclp_print_ascii(str);
> +	sclp_print_lm(str);
>  	while (sclp_busy)
>  		/* Wait for SCLP request to complete */;
> -	sclp_busy = true;
> -	sccb->h.length = sizeof(WriteEventData) + len;
> -	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
> -	sccb->ebh.length = sizeof(EventBufferHeader) + len;
> -	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
> -	sccb->ebh.flags = 0;
> -	memcpy(sccb->data, str, len);
> -
> -	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
> -	while (sclp_busy)
> -		/* Wait for SCLP request to complete */;
> -
>  }
> diff --git a/lib/s390x/sclp.h b/lib/s390x/sclp.h
> index a91aad1..6174451 100644
> --- a/lib/s390x/sclp.h
> +++ b/lib/s390x/sclp.h
> @@ -174,6 +174,7 @@ typedef struct SCCB {
>  /* SCLP event masks */
>  #define SCLP_EVENT_MASK_SIGNAL_QUIESCE          0x00000008
>  #define SCLP_EVENT_MASK_MSG_ASCII               0x00000040
> +#define SCLP_EVENT_MASK_MSG          		0x40000000
>  
>  #define SCLP_UNCONDITIONAL_READ                 0x00
>  #define SCLP_SELECTIVE_READ                     0x01
> @@ -207,6 +208,72 @@ typedef struct ReadEventData {
>      uint32_t mask;
>  } __attribute__((packed)) ReadEventData;
>  
> +#define MDBTYP_GO               0x0001
> +#define MDBTYP_MTO              0x0004
> +#define EVTYP_MSG               0x02
> +#define LNTPFLGS_CNTLTEXT       0x8000
> +#define LNTPFLGS_LABELTEXT      0x4000
> +#define LNTPFLGS_DATATEXT       0x2000
> +#define LNTPFLGS_ENDTEXT        0x1000
> +#define LNTPFLGS_PROMPTTEXT     0x0800
> +
> +typedef uint32_t sccb_mask_t;
> +
> +/* SCLP line mode console related structures. */
> +
> +struct mto {
> +	u16 length;
> +	u16 type;
> +	u16 line_type_flags;
> +	u8  alarm_control;
> +	u8  _reserved[3];
> +} __attribute__((packed));
> +
> +struct go {
> +	u16 length;
> +	u16 type;
> +	u32 domid;
> +	u8  hhmmss_time[8];
> +	u8  th_time[3];
> +	u8  reserved_0;
> +	u8  dddyyyy_date[7];
> +	u8  _reserved_1;
> +	u16 general_msg_flags;
> +	u8  _reserved_2[10];
> +	u8  originating_system_name[8];
> +	u8  job_guest_name[8];
> +} __attribute__((packed));
> +
> +struct mdb_header {
> +	u16 length;
> +	u16 type;
> +	u32 tag;
> +	u32 revision_code;
> +} __attribute__((packed));
> +
> +struct mdb {
> +	struct mdb_header header;
> +	struct go go;
> +	struct mto mto;
> +} __attribute__((packed));
> +
> +struct evbuf_header {
> +	u16	length;
> +	u8	type;
> +	u8	flags;
> +	u16	_reserved;
> +} __attribute__((packed));
> +
> +struct msg_buf {
> +	struct evbuf_header header;
> +	struct mdb mdb;
> +} __attribute__((packed));
> +
> +struct write_sccb {
> +	struct SCCBHeader header;
> +	struct msg_buf msg;
> +} __packed;
> +
>  extern char _sccb[];
>  volatile bool sclp_busy;
>  void sclp_handle_ext(void);
>
Janosch Frank Dec. 7, 2018, 5:02 p.m. UTC | #2
On 07.12.18 17:19, David Hildenbrand wrote:
> On 05.12.18 16:39, Janosch Frank wrote:
>> z/VM isn't fond of vt220, so we need line mode when running under z/VM.
>>
>> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
>> ---
>>  lib/s390x/sclp-console.c | 199 ++++++++++++++++++++++++++++++++++++++++++-----
>>  lib/s390x/sclp.h         |  67 ++++++++++++++++
>>  2 files changed, 245 insertions(+), 21 deletions(-)
>>
>> diff --git a/lib/s390x/sclp-console.c b/lib/s390x/sclp-console.c
>> index 13ab03d..c31e178 100644
>> --- a/lib/s390x/sclp-console.c
>> +++ b/lib/s390x/sclp-console.c
>> @@ -11,23 +11,183 @@
[...]
>> +
>> +static bool initialized = false;
> 
> false is the default after your BSS clearing patch.

Yes, already fixed

> 
>> +
>> +static void sclp_print_ascii(const char *str)
>> +{
>> +	int len = strlen(str);
>> +	WriteEventData *sccb = (void *)_sccb;
>> +
>> +	while (sclp_busy)
>> +		/* Wait for SCLP request to complete */;
>> +	sclp_busy = true;
>> +	memset(sccb, 0, sizeof(*sccb));
>> +	sccb->h.length = sizeof(WriteEventData) + len;
>> +	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
>> +	sccb->ebh.length = sizeof(EventBufferHeader) + len;
>> +	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
>> +	memcpy(sccb->data, str, len);
>> +
>> +	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
> 
> Shouldn't we wait for a response? (see below)

Yes, already fixed

> 
>> +}
>> +
>> +static void sclp_print_lm(const char *str)
>> +{
>> +	unsigned char *ptr, *end, ch;
>> +	unsigned int count, offset, len;
>> +	struct write_sccb *sccb;
>> +	struct msg_buf *msg;
>> +	struct mdb *mdb;
>> +	struct mto *mto;
>> +	struct go *go;
>> +
>> +	while (sclp_busy)
>> +		/* Wait for SCLP request to complete */;
>> +	sclp_busy = true;
>> +	sccb = (struct write_sccb *) _sccb;
>> +	end = (unsigned char *) sccb + 4096 - 1;
>> +	memset(sccb, 0, sizeof(*sccb));
>> +	ptr = (unsigned char *) &sccb->msg.mdb.mto;
>> +	len = strlen(str);
>> +	offset = 0;
>> +	do {
>> +		for (count = sizeof(*mto); offset < len; count++) {
>> +			ch = str[offset++];
>> +			if ((ch == 0x0a) || (ptr + count > end))
>> +				break;
>> +			ptr[count] = _ascebc[ch];
>> +		}
>> +		mto = (struct mto *) ptr;
>> +		memset(mto, 0, sizeof(*mto));
>> +		mto->length = count;
>> +		mto->type = 4;
>> +		mto->line_type_flags = LNTPFLGS_ENDTEXT;
>> +		ptr += count;
>> +	} while ((offset < len) && (ptr + sizeof(*mto) <= end));
>> +	len = ptr - (unsigned char *) sccb;
>> +	sccb->header.length = len - offsetof(struct write_sccb, header);
>> +	msg = &sccb->msg;
>> +	msg->header.type = EVTYP_MSG;
>> +	msg->header.length = len - offsetof(struct write_sccb, msg.header);
>> +	mdb = &msg->mdb;
>> +	mdb->header.type = 1;
>> +	mdb->header.tag = 0xD4C4C240;
>> +	mdb->header.revision_code = 1;
>> +	mdb->header.length = len - offsetof(struct write_sccb, msg.mdb.header);
>> +	go = &mdb->go;
>> +	go->length = sizeof(*go);
>> +	go->type = 1;
>> +	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
> 
> Shouldn't we wait for a response? (see below)

Yes, already fixed

> 
>> +}
>> +
>> +/*
>> + * SCLP needs to be initialized by setting a send and receive mask,
>> + * indicating which messages the control program (we) want(s) to
>> + * send/receive.
>> + */
>> +static bool sclp_set_write_mask(void)
>>  {
>>  	WriteEventMask *sccb = (void *)_sccb;
>>  
>>  	while (sclp_busy)
>>  		/* Wait for SCLP request to complete */;
> 
> Can this actually happen?
> 
> I'd say whoever issues a request has to wait for the response. Somebody
> who didn't, doesn't have to wait.

We could put the sclp_wait_busy() at the end of the sclp call function
and effectively make it synchronous by enforcing the wait. We don't
really need the async performance gain.

> 
> (for multiple CPUs later on that has to be guarded by a lock in addition
> to the bool)

Yeah, smp is currently on another branch, so I might just start to add
another patch and introduce locks. :-)

> 
>>  	sclp_busy = true;
>> +	memset(_sccb, 0, sizeof(*sccb));
>>  	sccb->h.length = sizeof(WriteEventMask);
>> -	sccb->mask_length = sizeof(unsigned int);
>> -	sccb->receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
>> -	sccb->cp_receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
>> -	sccb->send_mask = SCLP_EVENT_MASK_MSG_ASCII;
>> -	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII;
>> +	sccb->h.function_code = 0;
>> +	sccb->mask_length = sizeof(sccb_mask_t);
>> +
>> +	/* For now we don't process sclp input. */
>> +	sccb->cp_receive_mask = 0;
>> +	/* We send ASCII and line mode. */
>> +	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG;
>>  
>>  	sclp_service_call(SCLP_CMD_WRITE_EVENT_MASK, sccb);
>> +	while (sclp_busy) {
>> +	}
>> +	if (sccb->h.response_code != SCLP_RC_NORMAL_COMPLETION)
>> +		return false;
> 
> Hmm, what do do in this case?  I don't see that the caller is fixed up
> (you changed the return type from void to bool)

I don't know, there used to be a TODO there...
I thought about calling exit(1);

> 
> I "initialized" actually worth keeping around? I'd say let's just
> continue as if it succeeded. (we cannot really do anything about it, can
> we?)

There is the problem of prints that come before the initialization.
PGM interrupts while sclp_busy == true is also fun if they want to print.
diff mbox series

Patch

diff --git a/lib/s390x/sclp-console.c b/lib/s390x/sclp-console.c
index 13ab03d..c31e178 100644
--- a/lib/s390x/sclp-console.c
+++ b/lib/s390x/sclp-console.c
@@ -11,23 +11,183 @@ 
 #include <libcflat.h>
 #include <string.h>
 #include <asm/page.h>
+#include <asm/arch_def.h>
+#include <asm/io.h>
 #include "sclp.h"
 
-static void sclp_set_write_mask(void)
+/*
+ * ASCII (IBM PC 437) -> EBCDIC 037
+ */
+static uint8_t _ascebc[256] =
+{
+ /*00 NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL */
+     0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
+ /*08  BS    HT    LF    VT    FF    CR    SO    SI */
+ /*              ->NL                               */
+     0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+ /*10 DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
+     0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
+ /*18 CAN    EM   SUB   ESC    FS    GS    RS    US */
+ /*                               ->IGS ->IRS ->IUS */
+     0x18, 0x19, 0x3F, 0x27, 0x22, 0x1D, 0x1E, 0x1F,
+ /*20  SP     !     "     #     $     %     &     ' */
+     0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
+ /*28   (     )     *     +     ,     -    .      / */
+     0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
+ /*30   0     1     2     3     4     5     6     7 */
+     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
+ /*38   8     9     :     ;     <     =     >     ? */
+     0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
+ /*40   @     A     B     C     D     E     F     G */
+     0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
+ /*48   H     I     J     K     L     M     N     O */
+     0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
+ /*50   P     Q     R     S     T     U     V     W */
+     0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
+ /*58   X     Y     Z     [     \     ]     ^     _ */
+     0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D,
+ /*60   `     a     b     c     d     e     f     g */
+     0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ /*68   h     i     j     k     l     m     n     o */
+     0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
+ /*70   p     q     r     s     t     u     v     w */
+     0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
+ /*78   x     y     z     {     |     }     ~    DL */
+     0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
+ /*80*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*88*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*90*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*98*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*A0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*A8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*B0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*B8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*C0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*C8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*D0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*D8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*E0        sz						*/
+     0x3F, 0x59, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*E8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*F0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*F8*/
+     0x90, 0x3F, 0x3F, 0x3F, 0x3F, 0xEA, 0x3F, 0xFF
+};
+
+static bool initialized = false;
+
+static void sclp_print_ascii(const char *str)
+{
+	int len = strlen(str);
+	WriteEventData *sccb = (void *)_sccb;
+
+	while (sclp_busy)
+		/* Wait for SCLP request to complete */;
+	sclp_busy = true;
+	memset(sccb, 0, sizeof(*sccb));
+	sccb->h.length = sizeof(WriteEventData) + len;
+	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
+	sccb->ebh.length = sizeof(EventBufferHeader) + len;
+	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
+	memcpy(sccb->data, str, len);
+
+	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
+}
+
+static void sclp_print_lm(const char *str)
+{
+	unsigned char *ptr, *end, ch;
+	unsigned int count, offset, len;
+	struct write_sccb *sccb;
+	struct msg_buf *msg;
+	struct mdb *mdb;
+	struct mto *mto;
+	struct go *go;
+
+	while (sclp_busy)
+		/* Wait for SCLP request to complete */;
+	sclp_busy = true;
+	sccb = (struct write_sccb *) _sccb;
+	end = (unsigned char *) sccb + 4096 - 1;
+	memset(sccb, 0, sizeof(*sccb));
+	ptr = (unsigned char *) &sccb->msg.mdb.mto;
+	len = strlen(str);
+	offset = 0;
+	do {
+		for (count = sizeof(*mto); offset < len; count++) {
+			ch = str[offset++];
+			if ((ch == 0x0a) || (ptr + count > end))
+				break;
+			ptr[count] = _ascebc[ch];
+		}
+		mto = (struct mto *) ptr;
+		memset(mto, 0, sizeof(*mto));
+		mto->length = count;
+		mto->type = 4;
+		mto->line_type_flags = LNTPFLGS_ENDTEXT;
+		ptr += count;
+	} while ((offset < len) && (ptr + sizeof(*mto) <= end));
+	len = ptr - (unsigned char *) sccb;
+	sccb->header.length = len - offsetof(struct write_sccb, header);
+	msg = &sccb->msg;
+	msg->header.type = EVTYP_MSG;
+	msg->header.length = len - offsetof(struct write_sccb, msg.header);
+	mdb = &msg->mdb;
+	mdb->header.type = 1;
+	mdb->header.tag = 0xD4C4C240;
+	mdb->header.revision_code = 1;
+	mdb->header.length = len - offsetof(struct write_sccb, msg.mdb.header);
+	go = &mdb->go;
+	go->length = sizeof(*go);
+	go->type = 1;
+	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
+}
+
+/*
+ * SCLP needs to be initialized by setting a send and receive mask,
+ * indicating which messages the control program (we) want(s) to
+ * send/receive.
+ */
+static bool sclp_set_write_mask(void)
 {
 	WriteEventMask *sccb = (void *)_sccb;
 
 	while (sclp_busy)
 		/* Wait for SCLP request to complete */;
 	sclp_busy = true;
+	memset(_sccb, 0, sizeof(*sccb));
 	sccb->h.length = sizeof(WriteEventMask);
-	sccb->mask_length = sizeof(unsigned int);
-	sccb->receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
-	sccb->cp_receive_mask = SCLP_EVENT_MASK_MSG_ASCII;
-	sccb->send_mask = SCLP_EVENT_MASK_MSG_ASCII;
-	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII;
+	sccb->h.function_code = 0;
+	sccb->mask_length = sizeof(sccb_mask_t);
+
+	/* For now we don't process sclp input. */
+	sccb->cp_receive_mask = 0;
+	/* We send ASCII and line mode. */
+	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG;
 
 	sclp_service_call(SCLP_CMD_WRITE_EVENT_MASK, sccb);
+	while (sclp_busy) {
+	}
+	if (sccb->h.response_code != SCLP_RC_NORMAL_COMPLETION)
+		return false;
+	else {
+		initialized = true;
+		return true;
+	}
 }
 
 void sclp_console_setup(void)
@@ -37,21 +197,18 @@  void sclp_console_setup(void)
 
 void sclp_print(const char *str)
 {
-	int len = strlen(str);
-	WriteEventData *sccb = (void *)_sccb;
-
+	if (!initialized)
+		return;
+	/*
+	 * z/VM advertises a vt220 console which is not functional:
+	 * (response code 05F0, "not active because of the state of
+	 * the machine"). Hence testing the masks would only work if
+	 * we also use stsi data to distinguish z/VM.
+	 *
+	 * Let's rather print on all available consoles.
+	 */
+	sclp_print_ascii(str);
+	sclp_print_lm(str);
 	while (sclp_busy)
 		/* Wait for SCLP request to complete */;
-	sclp_busy = true;
-	sccb->h.length = sizeof(WriteEventData) + len;
-	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
-	sccb->ebh.length = sizeof(EventBufferHeader) + len;
-	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
-	sccb->ebh.flags = 0;
-	memcpy(sccb->data, str, len);
-
-	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
-	while (sclp_busy)
-		/* Wait for SCLP request to complete */;
-
 }
diff --git a/lib/s390x/sclp.h b/lib/s390x/sclp.h
index a91aad1..6174451 100644
--- a/lib/s390x/sclp.h
+++ b/lib/s390x/sclp.h
@@ -174,6 +174,7 @@  typedef struct SCCB {
 /* SCLP event masks */
 #define SCLP_EVENT_MASK_SIGNAL_QUIESCE          0x00000008
 #define SCLP_EVENT_MASK_MSG_ASCII               0x00000040
+#define SCLP_EVENT_MASK_MSG          		0x40000000
 
 #define SCLP_UNCONDITIONAL_READ                 0x00
 #define SCLP_SELECTIVE_READ                     0x01
@@ -207,6 +208,72 @@  typedef struct ReadEventData {
     uint32_t mask;
 } __attribute__((packed)) ReadEventData;
 
+#define MDBTYP_GO               0x0001
+#define MDBTYP_MTO              0x0004
+#define EVTYP_MSG               0x02
+#define LNTPFLGS_CNTLTEXT       0x8000
+#define LNTPFLGS_LABELTEXT      0x4000
+#define LNTPFLGS_DATATEXT       0x2000
+#define LNTPFLGS_ENDTEXT        0x1000
+#define LNTPFLGS_PROMPTTEXT     0x0800
+
+typedef uint32_t sccb_mask_t;
+
+/* SCLP line mode console related structures. */
+
+struct mto {
+	u16 length;
+	u16 type;
+	u16 line_type_flags;
+	u8  alarm_control;
+	u8  _reserved[3];
+} __attribute__((packed));
+
+struct go {
+	u16 length;
+	u16 type;
+	u32 domid;
+	u8  hhmmss_time[8];
+	u8  th_time[3];
+	u8  reserved_0;
+	u8  dddyyyy_date[7];
+	u8  _reserved_1;
+	u16 general_msg_flags;
+	u8  _reserved_2[10];
+	u8  originating_system_name[8];
+	u8  job_guest_name[8];
+} __attribute__((packed));
+
+struct mdb_header {
+	u16 length;
+	u16 type;
+	u32 tag;
+	u32 revision_code;
+} __attribute__((packed));
+
+struct mdb {
+	struct mdb_header header;
+	struct go go;
+	struct mto mto;
+} __attribute__((packed));
+
+struct evbuf_header {
+	u16	length;
+	u8	type;
+	u8	flags;
+	u16	_reserved;
+} __attribute__((packed));
+
+struct msg_buf {
+	struct evbuf_header header;
+	struct mdb mdb;
+} __attribute__((packed));
+
+struct write_sccb {
+	struct SCCBHeader header;
+	struct msg_buf msg;
+} __packed;
+
 extern char _sccb[];
 volatile bool sclp_busy;
 void sclp_handle_ext(void);