18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * driver: reading from and writing to system console on S/390 via SCLP
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright IBM Corp. 1999, 2009
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author(s): Martin Peschke <mpeschke@de.ibm.com>
88c2ecf20Sopenharmony_ci *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kmod.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include <linux/string.h>
158c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
168c2ecf20Sopenharmony_ci#include <linux/ctype.h>
178c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "sclp.h"
208c2ecf20Sopenharmony_ci#include "sclp_rw.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci * The room for the SCCB (only for writing) is not equal to a pages size
248c2ecf20Sopenharmony_ci * (as it is specified as the maximum size in the SCLP documentation)
258c2ecf20Sopenharmony_ci * because of the additional data structure described above.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci#define MAX_SCCB_ROOM (PAGE_SIZE - sizeof(struct sclp_buffer))
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic void sclp_rw_pm_event(struct sclp_register *reg,
308c2ecf20Sopenharmony_ci			     enum sclp_pm_event sclp_pm_event)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	sclp_console_pm_event(sclp_pm_event);
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Event type structure for write message and write priority message */
368c2ecf20Sopenharmony_cistatic struct sclp_register sclp_rw_event = {
378c2ecf20Sopenharmony_ci	.send_mask = EVTYP_MSG_MASK,
388c2ecf20Sopenharmony_ci	.pm_event_fn = sclp_rw_pm_event,
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*
428c2ecf20Sopenharmony_ci * Setup a sclp write buffer. Gets a page as input (4K) and returns
438c2ecf20Sopenharmony_ci * a pointer to a struct sclp_buffer structure that is located at the
448c2ecf20Sopenharmony_ci * end of the input page. This reduces the buffer space by a few
458c2ecf20Sopenharmony_ci * bytes but simplifies things.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistruct sclp_buffer *
488c2ecf20Sopenharmony_cisclp_make_buffer(void *page, unsigned short columns, unsigned short htab)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct sclp_buffer *buffer;
518c2ecf20Sopenharmony_ci	struct sccb_header *sccb;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	sccb = (struct sccb_header *) page;
548c2ecf20Sopenharmony_ci	/*
558c2ecf20Sopenharmony_ci	 * We keep the struct sclp_buffer structure at the end
568c2ecf20Sopenharmony_ci	 * of the sccb page.
578c2ecf20Sopenharmony_ci	 */
588c2ecf20Sopenharmony_ci	buffer = ((struct sclp_buffer *) ((addr_t) sccb + PAGE_SIZE)) - 1;
598c2ecf20Sopenharmony_ci	buffer->sccb = sccb;
608c2ecf20Sopenharmony_ci	buffer->retry_count = 0;
618c2ecf20Sopenharmony_ci	buffer->messages = 0;
628c2ecf20Sopenharmony_ci	buffer->char_sum = 0;
638c2ecf20Sopenharmony_ci	buffer->current_line = NULL;
648c2ecf20Sopenharmony_ci	buffer->current_length = 0;
658c2ecf20Sopenharmony_ci	buffer->columns = columns;
668c2ecf20Sopenharmony_ci	buffer->htab = htab;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	/* initialize sccb */
698c2ecf20Sopenharmony_ci	memset(sccb, 0, sizeof(struct sccb_header));
708c2ecf20Sopenharmony_ci	sccb->length = sizeof(struct sccb_header);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return buffer;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/*
768c2ecf20Sopenharmony_ci * Return a pointer to the original page that has been used to create
778c2ecf20Sopenharmony_ci * the buffer.
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_civoid *
808c2ecf20Sopenharmony_cisclp_unmake_buffer(struct sclp_buffer *buffer)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	return buffer->sccb;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Initialize a new message the end of the provided buffer with
878c2ecf20Sopenharmony_ci * enough room for max_len characters. Return 0 on success.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_cistatic int
908c2ecf20Sopenharmony_cisclp_initialize_mto(struct sclp_buffer *buffer, int max_len)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct sccb_header *sccb;
938c2ecf20Sopenharmony_ci	struct msg_buf *msg;
948c2ecf20Sopenharmony_ci	struct mdb *mdb;
958c2ecf20Sopenharmony_ci	struct go *go;
968c2ecf20Sopenharmony_ci	struct mto *mto;
978c2ecf20Sopenharmony_ci	int msg_size;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* max size of new message including message text  */
1008c2ecf20Sopenharmony_ci	msg_size = sizeof(struct msg_buf) + max_len;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	/* check if current buffer sccb can contain the mto */
1038c2ecf20Sopenharmony_ci	sccb = buffer->sccb;
1048c2ecf20Sopenharmony_ci	if ((MAX_SCCB_ROOM - sccb->length) < msg_size)
1058c2ecf20Sopenharmony_ci		return -ENOMEM;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	msg = (struct msg_buf *)((addr_t) sccb + sccb->length);
1088c2ecf20Sopenharmony_ci	memset(msg, 0, sizeof(struct msg_buf));
1098c2ecf20Sopenharmony_ci	msg->header.length = sizeof(struct msg_buf);
1108c2ecf20Sopenharmony_ci	msg->header.type = EVTYP_MSG;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	mdb = &msg->mdb;
1138c2ecf20Sopenharmony_ci	mdb->header.length = sizeof(struct mdb);
1148c2ecf20Sopenharmony_ci	mdb->header.type = 1;
1158c2ecf20Sopenharmony_ci	mdb->header.tag = 0xD4C4C240;	/* ebcdic "MDB " */
1168c2ecf20Sopenharmony_ci	mdb->header.revision_code = 1;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	go = &mdb->go;
1198c2ecf20Sopenharmony_ci	go->length = sizeof(struct go);
1208c2ecf20Sopenharmony_ci	go->type = 1;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	mto = &mdb->mto;
1238c2ecf20Sopenharmony_ci	mto->length = sizeof(struct mto);
1248c2ecf20Sopenharmony_ci	mto->type = 4;	/* message text object */
1258c2ecf20Sopenharmony_ci	mto->line_type_flags = LNTPFLGS_ENDTEXT; /* end text */
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* set pointer to first byte after struct mto. */
1288c2ecf20Sopenharmony_ci	buffer->current_msg = msg;
1298c2ecf20Sopenharmony_ci	buffer->current_line = (char *) (mto + 1);
1308c2ecf20Sopenharmony_ci	buffer->current_length = 0;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	return 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * Finalize message initialized by sclp_initialize_mto(),
1378c2ecf20Sopenharmony_ci * updating the sizes of MTO, enclosing MDB, event buffer and SCCB.
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_cistatic void
1408c2ecf20Sopenharmony_cisclp_finalize_mto(struct sclp_buffer *buffer)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct sccb_header *sccb;
1438c2ecf20Sopenharmony_ci	struct msg_buf *msg;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * update values of sizes
1478c2ecf20Sopenharmony_ci	 * (SCCB, Event(Message) Buffer, Message Data Block)
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	sccb = buffer->sccb;
1508c2ecf20Sopenharmony_ci	msg = buffer->current_msg;
1518c2ecf20Sopenharmony_ci	msg->header.length += buffer->current_length;
1528c2ecf20Sopenharmony_ci	msg->mdb.header.length += buffer->current_length;
1538c2ecf20Sopenharmony_ci	msg->mdb.mto.length += buffer->current_length;
1548c2ecf20Sopenharmony_ci	sccb->length += msg->header.length;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/*
1578c2ecf20Sopenharmony_ci	 * count number of buffered messages (= number of Message Text
1588c2ecf20Sopenharmony_ci	 * Objects) and number of buffered characters
1598c2ecf20Sopenharmony_ci	 * for the SCCB currently used for buffering and at all
1608c2ecf20Sopenharmony_ci	 */
1618c2ecf20Sopenharmony_ci	buffer->messages++;
1628c2ecf20Sopenharmony_ci	buffer->char_sum += buffer->current_length;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	buffer->current_line = NULL;
1658c2ecf20Sopenharmony_ci	buffer->current_length = 0;
1668c2ecf20Sopenharmony_ci	buffer->current_msg = NULL;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/*
1708c2ecf20Sopenharmony_ci * processing of a message including escape characters,
1718c2ecf20Sopenharmony_ci * returns number of characters written to the output sccb
1728c2ecf20Sopenharmony_ci * ("processed" means that is not guaranteed that the character have already
1738c2ecf20Sopenharmony_ci *  been sent to the SCLP but that it will be done at least next time the SCLP
1748c2ecf20Sopenharmony_ci *  is not busy)
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_ciint
1778c2ecf20Sopenharmony_cisclp_write(struct sclp_buffer *buffer, const unsigned char *msg, int count)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	int spaces, i_msg;
1808c2ecf20Sopenharmony_ci	int rc;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/*
1838c2ecf20Sopenharmony_ci	 * parse msg for escape sequences (\t,\v ...) and put formated
1848c2ecf20Sopenharmony_ci	 * msg into an mto (created by sclp_initialize_mto).
1858c2ecf20Sopenharmony_ci	 *
1868c2ecf20Sopenharmony_ci	 * We have to do this work ourselfs because there is no support for
1878c2ecf20Sopenharmony_ci	 * these characters on the native machine and only partial support
1888c2ecf20Sopenharmony_ci	 * under VM (Why does VM interpret \n but the native machine doesn't ?)
1898c2ecf20Sopenharmony_ci	 *
1908c2ecf20Sopenharmony_ci	 * Depending on i/o-control setting the message is always written
1918c2ecf20Sopenharmony_ci	 * immediately or we wait for a final new line maybe coming with the
1928c2ecf20Sopenharmony_ci	 * next message. Besides we avoid a buffer overrun by writing its
1938c2ecf20Sopenharmony_ci	 * content.
1948c2ecf20Sopenharmony_ci	 *
1958c2ecf20Sopenharmony_ci	 * RESTRICTIONS:
1968c2ecf20Sopenharmony_ci	 *
1978c2ecf20Sopenharmony_ci	 * \r and \b work within one line because we are not able to modify
1988c2ecf20Sopenharmony_ci	 * previous output that have already been accepted by the SCLP.
1998c2ecf20Sopenharmony_ci	 *
2008c2ecf20Sopenharmony_ci	 * \t combined with following \r is not correctly represented because
2018c2ecf20Sopenharmony_ci	 * \t is expanded to some spaces but \r does not know about a
2028c2ecf20Sopenharmony_ci	 * previous \t and decreases the current position by one column.
2038c2ecf20Sopenharmony_ci	 * This is in order to a slim and quick implementation.
2048c2ecf20Sopenharmony_ci	 */
2058c2ecf20Sopenharmony_ci	for (i_msg = 0; i_msg < count; i_msg++) {
2068c2ecf20Sopenharmony_ci		switch (msg[i_msg]) {
2078c2ecf20Sopenharmony_ci		case '\n':	/* new line, line feed (ASCII)	*/
2088c2ecf20Sopenharmony_ci			/* check if new mto needs to be created */
2098c2ecf20Sopenharmony_ci			if (buffer->current_line == NULL) {
2108c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer, 0);
2118c2ecf20Sopenharmony_ci				if (rc)
2128c2ecf20Sopenharmony_ci					return i_msg;
2138c2ecf20Sopenharmony_ci			}
2148c2ecf20Sopenharmony_ci			sclp_finalize_mto(buffer);
2158c2ecf20Sopenharmony_ci			break;
2168c2ecf20Sopenharmony_ci		case '\a':	/* bell, one for several times	*/
2178c2ecf20Sopenharmony_ci			/* set SCLP sound alarm bit in General Object */
2188c2ecf20Sopenharmony_ci			if (buffer->current_line == NULL) {
2198c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer,
2208c2ecf20Sopenharmony_ci							 buffer->columns);
2218c2ecf20Sopenharmony_ci				if (rc)
2228c2ecf20Sopenharmony_ci					return i_msg;
2238c2ecf20Sopenharmony_ci			}
2248c2ecf20Sopenharmony_ci			buffer->current_msg->mdb.go.general_msg_flags |=
2258c2ecf20Sopenharmony_ci				GNRLMSGFLGS_SNDALRM;
2268c2ecf20Sopenharmony_ci			break;
2278c2ecf20Sopenharmony_ci		case '\t':	/* horizontal tabulator	 */
2288c2ecf20Sopenharmony_ci			/* check if new mto needs to be created */
2298c2ecf20Sopenharmony_ci			if (buffer->current_line == NULL) {
2308c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer,
2318c2ecf20Sopenharmony_ci							 buffer->columns);
2328c2ecf20Sopenharmony_ci				if (rc)
2338c2ecf20Sopenharmony_ci					return i_msg;
2348c2ecf20Sopenharmony_ci			}
2358c2ecf20Sopenharmony_ci			/* "go to (next htab-boundary + 1, same line)" */
2368c2ecf20Sopenharmony_ci			do {
2378c2ecf20Sopenharmony_ci				if (buffer->current_length >= buffer->columns)
2388c2ecf20Sopenharmony_ci					break;
2398c2ecf20Sopenharmony_ci				/* ok, add a blank */
2408c2ecf20Sopenharmony_ci				*buffer->current_line++ = 0x40;
2418c2ecf20Sopenharmony_ci				buffer->current_length++;
2428c2ecf20Sopenharmony_ci			} while (buffer->current_length % buffer->htab);
2438c2ecf20Sopenharmony_ci			break;
2448c2ecf20Sopenharmony_ci		case '\f':	/* form feed  */
2458c2ecf20Sopenharmony_ci		case '\v':	/* vertical tabulator  */
2468c2ecf20Sopenharmony_ci			/* "go to (actual column, actual line + 1)" */
2478c2ecf20Sopenharmony_ci			/* = new line, leading spaces */
2488c2ecf20Sopenharmony_ci			if (buffer->current_line != NULL) {
2498c2ecf20Sopenharmony_ci				spaces = buffer->current_length;
2508c2ecf20Sopenharmony_ci				sclp_finalize_mto(buffer);
2518c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer,
2528c2ecf20Sopenharmony_ci							 buffer->columns);
2538c2ecf20Sopenharmony_ci				if (rc)
2548c2ecf20Sopenharmony_ci					return i_msg;
2558c2ecf20Sopenharmony_ci				memset(buffer->current_line, 0x40, spaces);
2568c2ecf20Sopenharmony_ci				buffer->current_line += spaces;
2578c2ecf20Sopenharmony_ci				buffer->current_length = spaces;
2588c2ecf20Sopenharmony_ci			} else {
2598c2ecf20Sopenharmony_ci				/* one an empty line this is the same as \n */
2608c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer,
2618c2ecf20Sopenharmony_ci							 buffer->columns);
2628c2ecf20Sopenharmony_ci				if (rc)
2638c2ecf20Sopenharmony_ci					return i_msg;
2648c2ecf20Sopenharmony_ci				sclp_finalize_mto(buffer);
2658c2ecf20Sopenharmony_ci			}
2668c2ecf20Sopenharmony_ci			break;
2678c2ecf20Sopenharmony_ci		case '\b':	/* backspace  */
2688c2ecf20Sopenharmony_ci			/* "go to (actual column - 1, actual line)" */
2698c2ecf20Sopenharmony_ci			/* decrement counter indicating position, */
2708c2ecf20Sopenharmony_ci			/* do not remove last character */
2718c2ecf20Sopenharmony_ci			if (buffer->current_line != NULL &&
2728c2ecf20Sopenharmony_ci			    buffer->current_length > 0) {
2738c2ecf20Sopenharmony_ci				buffer->current_length--;
2748c2ecf20Sopenharmony_ci				buffer->current_line--;
2758c2ecf20Sopenharmony_ci			}
2768c2ecf20Sopenharmony_ci			break;
2778c2ecf20Sopenharmony_ci		case 0x00:	/* end of string  */
2788c2ecf20Sopenharmony_ci			/* transfer current line to SCCB */
2798c2ecf20Sopenharmony_ci			if (buffer->current_line != NULL)
2808c2ecf20Sopenharmony_ci				sclp_finalize_mto(buffer);
2818c2ecf20Sopenharmony_ci			/* skip the rest of the message including the 0 byte */
2828c2ecf20Sopenharmony_ci			i_msg = count - 1;
2838c2ecf20Sopenharmony_ci			break;
2848c2ecf20Sopenharmony_ci		default:	/* no escape character	*/
2858c2ecf20Sopenharmony_ci			/* do not output unprintable characters */
2868c2ecf20Sopenharmony_ci			if (!isprint(msg[i_msg]))
2878c2ecf20Sopenharmony_ci				break;
2888c2ecf20Sopenharmony_ci			/* check if new mto needs to be created */
2898c2ecf20Sopenharmony_ci			if (buffer->current_line == NULL) {
2908c2ecf20Sopenharmony_ci				rc = sclp_initialize_mto(buffer,
2918c2ecf20Sopenharmony_ci							 buffer->columns);
2928c2ecf20Sopenharmony_ci				if (rc)
2938c2ecf20Sopenharmony_ci					return i_msg;
2948c2ecf20Sopenharmony_ci			}
2958c2ecf20Sopenharmony_ci			*buffer->current_line++ = sclp_ascebc(msg[i_msg]);
2968c2ecf20Sopenharmony_ci			buffer->current_length++;
2978c2ecf20Sopenharmony_ci			break;
2988c2ecf20Sopenharmony_ci		}
2998c2ecf20Sopenharmony_ci		/* check if current mto is full */
3008c2ecf20Sopenharmony_ci		if (buffer->current_line != NULL &&
3018c2ecf20Sopenharmony_ci		    buffer->current_length >= buffer->columns)
3028c2ecf20Sopenharmony_ci			sclp_finalize_mto(buffer);
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* return number of processed characters */
3068c2ecf20Sopenharmony_ci	return i_msg;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci/*
3108c2ecf20Sopenharmony_ci * Return the number of free bytes in the sccb
3118c2ecf20Sopenharmony_ci */
3128c2ecf20Sopenharmony_ciint
3138c2ecf20Sopenharmony_cisclp_buffer_space(struct sclp_buffer *buffer)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct sccb_header *sccb;
3168c2ecf20Sopenharmony_ci	int count;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	sccb = buffer->sccb;
3198c2ecf20Sopenharmony_ci	count = MAX_SCCB_ROOM - sccb->length;
3208c2ecf20Sopenharmony_ci	if (buffer->current_line != NULL)
3218c2ecf20Sopenharmony_ci		count -= sizeof(struct msg_buf) + buffer->current_length;
3228c2ecf20Sopenharmony_ci	return count;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci/*
3268c2ecf20Sopenharmony_ci * Return number of characters in buffer
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_ciint
3298c2ecf20Sopenharmony_cisclp_chars_in_buffer(struct sclp_buffer *buffer)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	int count;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	count = buffer->char_sum;
3348c2ecf20Sopenharmony_ci	if (buffer->current_line != NULL)
3358c2ecf20Sopenharmony_ci		count += buffer->current_length;
3368c2ecf20Sopenharmony_ci	return count;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci/*
3408c2ecf20Sopenharmony_ci * called by sclp_console_init and/or sclp_tty_init
3418c2ecf20Sopenharmony_ci */
3428c2ecf20Sopenharmony_ciint
3438c2ecf20Sopenharmony_cisclp_rw_init(void)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	static int init_done = 0;
3468c2ecf20Sopenharmony_ci	int rc;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (init_done)
3498c2ecf20Sopenharmony_ci		return 0;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	rc = sclp_register(&sclp_rw_event);
3528c2ecf20Sopenharmony_ci	if (rc == 0)
3538c2ecf20Sopenharmony_ci		init_done = 1;
3548c2ecf20Sopenharmony_ci	return rc;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci#define SCLP_BUFFER_MAX_RETRY		1
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/*
3608c2ecf20Sopenharmony_ci * second half of Write Event Data-function that has to be done after
3618c2ecf20Sopenharmony_ci * interruption indicating completion of Service Call.
3628c2ecf20Sopenharmony_ci */
3638c2ecf20Sopenharmony_cistatic void
3648c2ecf20Sopenharmony_cisclp_writedata_callback(struct sclp_req *request, void *data)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	int rc;
3678c2ecf20Sopenharmony_ci	struct sclp_buffer *buffer;
3688c2ecf20Sopenharmony_ci	struct sccb_header *sccb;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	buffer = (struct sclp_buffer *) data;
3718c2ecf20Sopenharmony_ci	sccb = buffer->sccb;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (request->status == SCLP_REQ_FAILED) {
3748c2ecf20Sopenharmony_ci		if (buffer->callback != NULL)
3758c2ecf20Sopenharmony_ci			buffer->callback(buffer, -EIO);
3768c2ecf20Sopenharmony_ci		return;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci	/* check SCLP response code and choose suitable action	*/
3798c2ecf20Sopenharmony_ci	switch (sccb->response_code) {
3808c2ecf20Sopenharmony_ci	case 0x0020 :
3818c2ecf20Sopenharmony_ci		/* Normal completion, buffer processed, message(s) sent */
3828c2ecf20Sopenharmony_ci		rc = 0;
3838c2ecf20Sopenharmony_ci		break;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	case 0x0340: /* Contained SCLP equipment check */
3868c2ecf20Sopenharmony_ci		if (++buffer->retry_count > SCLP_BUFFER_MAX_RETRY) {
3878c2ecf20Sopenharmony_ci			rc = -EIO;
3888c2ecf20Sopenharmony_ci			break;
3898c2ecf20Sopenharmony_ci		}
3908c2ecf20Sopenharmony_ci		/* remove processed buffers and requeue rest */
3918c2ecf20Sopenharmony_ci		if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
3928c2ecf20Sopenharmony_ci			/* not all buffers were processed */
3938c2ecf20Sopenharmony_ci			sccb->response_code = 0x0000;
3948c2ecf20Sopenharmony_ci			buffer->request.status = SCLP_REQ_FILLED;
3958c2ecf20Sopenharmony_ci			rc = sclp_add_request(request);
3968c2ecf20Sopenharmony_ci			if (rc == 0)
3978c2ecf20Sopenharmony_ci				return;
3988c2ecf20Sopenharmony_ci		} else
3998c2ecf20Sopenharmony_ci			rc = 0;
4008c2ecf20Sopenharmony_ci		break;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	case 0x0040: /* SCLP equipment check */
4038c2ecf20Sopenharmony_ci	case 0x05f0: /* Target resource in improper state */
4048c2ecf20Sopenharmony_ci		if (++buffer->retry_count > SCLP_BUFFER_MAX_RETRY) {
4058c2ecf20Sopenharmony_ci			rc = -EIO;
4068c2ecf20Sopenharmony_ci			break;
4078c2ecf20Sopenharmony_ci		}
4088c2ecf20Sopenharmony_ci		/* retry request */
4098c2ecf20Sopenharmony_ci		sccb->response_code = 0x0000;
4108c2ecf20Sopenharmony_ci		buffer->request.status = SCLP_REQ_FILLED;
4118c2ecf20Sopenharmony_ci		rc = sclp_add_request(request);
4128c2ecf20Sopenharmony_ci		if (rc == 0)
4138c2ecf20Sopenharmony_ci			return;
4148c2ecf20Sopenharmony_ci		break;
4158c2ecf20Sopenharmony_ci	default:
4168c2ecf20Sopenharmony_ci		if (sccb->response_code == 0x71f0)
4178c2ecf20Sopenharmony_ci			rc = -ENOMEM;
4188c2ecf20Sopenharmony_ci		else
4198c2ecf20Sopenharmony_ci			rc = -EINVAL;
4208c2ecf20Sopenharmony_ci		break;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci	if (buffer->callback != NULL)
4238c2ecf20Sopenharmony_ci		buffer->callback(buffer, rc);
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/*
4278c2ecf20Sopenharmony_ci * Setup the request structure in the struct sclp_buffer to do SCLP Write
4288c2ecf20Sopenharmony_ci * Event Data and pass the request to the core SCLP loop. Return zero on
4298c2ecf20Sopenharmony_ci * success, non-zero otherwise.
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_ciint
4328c2ecf20Sopenharmony_cisclp_emit_buffer(struct sclp_buffer *buffer,
4338c2ecf20Sopenharmony_ci		 void (*callback)(struct sclp_buffer *, int))
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	/* add current line if there is one */
4368c2ecf20Sopenharmony_ci	if (buffer->current_line != NULL)
4378c2ecf20Sopenharmony_ci		sclp_finalize_mto(buffer);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* Are there messages in the output buffer ? */
4408c2ecf20Sopenharmony_ci	if (buffer->messages == 0)
4418c2ecf20Sopenharmony_ci		return -EIO;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	buffer->request.command = SCLP_CMDW_WRITE_EVENT_DATA;
4448c2ecf20Sopenharmony_ci	buffer->request.status = SCLP_REQ_FILLED;
4458c2ecf20Sopenharmony_ci	buffer->request.callback = sclp_writedata_callback;
4468c2ecf20Sopenharmony_ci	buffer->request.callback_data = buffer;
4478c2ecf20Sopenharmony_ci	buffer->request.sccb = buffer->sccb;
4488c2ecf20Sopenharmony_ci	buffer->callback = callback;
4498c2ecf20Sopenharmony_ci	return sclp_add_request(&buffer->request);
4508c2ecf20Sopenharmony_ci}
451