xref: /OK3568_Linux_fs/kernel/drivers/tty/hvc/hvc_iucv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * z/VM IUCV hypervisor console (HVC) device driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This HVC device driver provides terminal access using
6*4882a593Smuzhiyun  * z/VM IUCV communication paths.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright IBM Corp. 2008, 2013
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Author(s):	Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun #define KMSG_COMPONENT		"hvc_iucv"
13*4882a593Smuzhiyun #define pr_fmt(fmt)		KMSG_COMPONENT ": " fmt
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <asm/ebcdic.h>
18*4882a593Smuzhiyun #include <linux/ctype.h>
19*4882a593Smuzhiyun #include <linux/delay.h>
20*4882a593Smuzhiyun #include <linux/device.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/mempool.h>
23*4882a593Smuzhiyun #include <linux/moduleparam.h>
24*4882a593Smuzhiyun #include <linux/tty.h>
25*4882a593Smuzhiyun #include <linux/wait.h>
26*4882a593Smuzhiyun #include <net/iucv/iucv.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include "hvc_console.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /* General device driver settings */
32*4882a593Smuzhiyun #define HVC_IUCV_MAGIC		0xc9e4c3e5
33*4882a593Smuzhiyun #define MAX_HVC_IUCV_LINES	HVC_ALLOC_TTY_ADAPTERS
34*4882a593Smuzhiyun #define MEMPOOL_MIN_NR		(PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* IUCV TTY message  */
37*4882a593Smuzhiyun #define MSG_VERSION		0x02	/* Message version */
38*4882a593Smuzhiyun #define MSG_TYPE_ERROR		0x01	/* Error message */
39*4882a593Smuzhiyun #define MSG_TYPE_TERMENV	0x02	/* Terminal environment variable */
40*4882a593Smuzhiyun #define MSG_TYPE_TERMIOS	0x04	/* Terminal IO struct update */
41*4882a593Smuzhiyun #define MSG_TYPE_WINSIZE	0x08	/* Terminal window size update */
42*4882a593Smuzhiyun #define MSG_TYPE_DATA		0x10	/* Terminal data */
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct iucv_tty_msg {
45*4882a593Smuzhiyun 	u8	version;		/* Message version */
46*4882a593Smuzhiyun 	u8	type;			/* Message type */
47*4882a593Smuzhiyun #define MSG_MAX_DATALEN		((u16)(~0))
48*4882a593Smuzhiyun 	u16	datalen;		/* Payload length */
49*4882a593Smuzhiyun 	u8	data[];			/* Payload buffer */
50*4882a593Smuzhiyun } __attribute__((packed));
51*4882a593Smuzhiyun #define MSG_SIZE(s)		((s) + offsetof(struct iucv_tty_msg, data))
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun enum iucv_state_t {
54*4882a593Smuzhiyun 	IUCV_DISCONN	= 0,
55*4882a593Smuzhiyun 	IUCV_CONNECTED	= 1,
56*4882a593Smuzhiyun 	IUCV_SEVERED	= 2,
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun enum tty_state_t {
60*4882a593Smuzhiyun 	TTY_CLOSED	= 0,
61*4882a593Smuzhiyun 	TTY_OPENED	= 1,
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun struct hvc_iucv_private {
65*4882a593Smuzhiyun 	struct hvc_struct	*hvc;		/* HVC struct reference */
66*4882a593Smuzhiyun 	u8			srv_name[8];	/* IUCV service name (ebcdic) */
67*4882a593Smuzhiyun 	unsigned char		is_console;	/* Linux console usage flag */
68*4882a593Smuzhiyun 	enum iucv_state_t	iucv_state;	/* IUCV connection status */
69*4882a593Smuzhiyun 	enum tty_state_t	tty_state;	/* TTY status */
70*4882a593Smuzhiyun 	struct iucv_path	*path;		/* IUCV path pointer */
71*4882a593Smuzhiyun 	spinlock_t		lock;		/* hvc_iucv_private lock */
72*4882a593Smuzhiyun #define SNDBUF_SIZE		(PAGE_SIZE)	/* must be < MSG_MAX_DATALEN */
73*4882a593Smuzhiyun 	void			*sndbuf;	/* send buffer		  */
74*4882a593Smuzhiyun 	size_t			sndbuf_len;	/* length of send buffer  */
75*4882a593Smuzhiyun #define QUEUE_SNDBUF_DELAY	(HZ / 25)
76*4882a593Smuzhiyun 	struct delayed_work	sndbuf_work;	/* work: send iucv msg(s) */
77*4882a593Smuzhiyun 	wait_queue_head_t	sndbuf_waitq;	/* wait for send completion */
78*4882a593Smuzhiyun 	struct list_head	tty_outqueue;	/* outgoing IUCV messages */
79*4882a593Smuzhiyun 	struct list_head	tty_inqueue;	/* incoming IUCV messages */
80*4882a593Smuzhiyun 	struct device		*dev;		/* device structure */
81*4882a593Smuzhiyun 	u8			info_path[16];	/* IUCV path info (dev attr) */
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun struct iucv_tty_buffer {
85*4882a593Smuzhiyun 	struct list_head	list;	/* list pointer */
86*4882a593Smuzhiyun 	struct iucv_message	msg;	/* store an IUCV message */
87*4882a593Smuzhiyun 	size_t			offset;	/* data buffer offset */
88*4882a593Smuzhiyun 	struct iucv_tty_msg	*mbuf;	/* buffer to store input/output data */
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /* IUCV callback handler */
92*4882a593Smuzhiyun static	int hvc_iucv_path_pending(struct iucv_path *, u8 *, u8 *);
93*4882a593Smuzhiyun static void hvc_iucv_path_severed(struct iucv_path *, u8 *);
94*4882a593Smuzhiyun static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
95*4882a593Smuzhiyun static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /* Kernel module parameter: use one terminal device as default */
99*4882a593Smuzhiyun static unsigned long hvc_iucv_devices = 1;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* Array of allocated hvc iucv tty lines... */
102*4882a593Smuzhiyun static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
103*4882a593Smuzhiyun #define IUCV_HVC_CON_IDX	(0)
104*4882a593Smuzhiyun /* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
105*4882a593Smuzhiyun #define MAX_VMID_FILTER		(500)
106*4882a593Smuzhiyun #define FILTER_WILDCARD_CHAR	'*'
107*4882a593Smuzhiyun static size_t hvc_iucv_filter_size;
108*4882a593Smuzhiyun static void *hvc_iucv_filter;
109*4882a593Smuzhiyun static const char *hvc_iucv_filter_string;
110*4882a593Smuzhiyun static DEFINE_RWLOCK(hvc_iucv_filter_lock);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /* Kmem cache and mempool for iucv_tty_buffer elements */
113*4882a593Smuzhiyun static struct kmem_cache *hvc_iucv_buffer_cache;
114*4882a593Smuzhiyun static mempool_t *hvc_iucv_mempool;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /* IUCV handler callback functions */
117*4882a593Smuzhiyun static struct iucv_handler hvc_iucv_handler = {
118*4882a593Smuzhiyun 	.path_pending  = hvc_iucv_path_pending,
119*4882a593Smuzhiyun 	.path_severed  = hvc_iucv_path_severed,
120*4882a593Smuzhiyun 	.message_complete = hvc_iucv_msg_complete,
121*4882a593Smuzhiyun 	.message_pending  = hvc_iucv_msg_pending,
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /**
126*4882a593Smuzhiyun  * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
127*4882a593Smuzhiyun  * @num:	The HVC virtual terminal number (vtermno)
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * This function returns the struct hvc_iucv_private instance that corresponds
130*4882a593Smuzhiyun  * to the HVC virtual terminal number specified as parameter @num.
131*4882a593Smuzhiyun  */
hvc_iucv_get_private(uint32_t num)132*4882a593Smuzhiyun static struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
135*4882a593Smuzhiyun 		return NULL;
136*4882a593Smuzhiyun 	return hvc_iucv_table[num - HVC_IUCV_MAGIC];
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun  * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
141*4882a593Smuzhiyun  * @size:	Size of the internal buffer used to store data.
142*4882a593Smuzhiyun  * @flags:	Memory allocation flags passed to mempool.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * This function allocates a new struct iucv_tty_buffer element and, optionally,
145*4882a593Smuzhiyun  * allocates an internal data buffer with the specified size @size.
146*4882a593Smuzhiyun  * The internal data buffer is always allocated with GFP_DMA which is
147*4882a593Smuzhiyun  * required for receiving and sending data with IUCV.
148*4882a593Smuzhiyun  * Note: The total message size arises from the internal buffer size and the
149*4882a593Smuzhiyun  *	 members of the iucv_tty_msg structure.
150*4882a593Smuzhiyun  * The function returns NULL if memory allocation has failed.
151*4882a593Smuzhiyun  */
alloc_tty_buffer(size_t size,gfp_t flags)152*4882a593Smuzhiyun static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct iucv_tty_buffer *bufp;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	bufp = mempool_alloc(hvc_iucv_mempool, flags);
157*4882a593Smuzhiyun 	if (!bufp)
158*4882a593Smuzhiyun 		return NULL;
159*4882a593Smuzhiyun 	memset(bufp, 0, sizeof(*bufp));
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	if (size > 0) {
162*4882a593Smuzhiyun 		bufp->msg.length = MSG_SIZE(size);
163*4882a593Smuzhiyun 		bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
164*4882a593Smuzhiyun 		if (!bufp->mbuf) {
165*4882a593Smuzhiyun 			mempool_free(bufp, hvc_iucv_mempool);
166*4882a593Smuzhiyun 			return NULL;
167*4882a593Smuzhiyun 		}
168*4882a593Smuzhiyun 		bufp->mbuf->version = MSG_VERSION;
169*4882a593Smuzhiyun 		bufp->mbuf->type    = MSG_TYPE_DATA;
170*4882a593Smuzhiyun 		bufp->mbuf->datalen = (u16) size;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 	return bufp;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /**
176*4882a593Smuzhiyun  * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
177*4882a593Smuzhiyun  * @bufp:	Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
178*4882a593Smuzhiyun  */
destroy_tty_buffer(struct iucv_tty_buffer * bufp)179*4882a593Smuzhiyun static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	kfree(bufp->mbuf);
182*4882a593Smuzhiyun 	mempool_free(bufp, hvc_iucv_mempool);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun  * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
187*4882a593Smuzhiyun  * @list:	List containing struct iucv_tty_buffer elements.
188*4882a593Smuzhiyun  */
destroy_tty_buffer_list(struct list_head * list)189*4882a593Smuzhiyun static void destroy_tty_buffer_list(struct list_head *list)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct iucv_tty_buffer *ent, *next;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	list_for_each_entry_safe(ent, next, list, list) {
194*4882a593Smuzhiyun 		list_del(&ent->list);
195*4882a593Smuzhiyun 		destroy_tty_buffer(ent);
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun /**
200*4882a593Smuzhiyun  * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
201*4882a593Smuzhiyun  * @priv:		Pointer to struct hvc_iucv_private
202*4882a593Smuzhiyun  * @buf:		HVC buffer for writing received terminal data.
203*4882a593Smuzhiyun  * @count:		HVC buffer size.
204*4882a593Smuzhiyun  * @has_more_data:	Pointer to an int variable.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * The function picks up pending messages from the input queue and receives
207*4882a593Smuzhiyun  * the message data that is then written to the specified buffer @buf.
208*4882a593Smuzhiyun  * If the buffer size @count is less than the data message size, the
209*4882a593Smuzhiyun  * message is kept on the input queue and @has_more_data is set to 1.
210*4882a593Smuzhiyun  * If all message data has been written, the message is removed from
211*4882a593Smuzhiyun  * the input queue.
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * The function returns the number of bytes written to the terminal, zero if
214*4882a593Smuzhiyun  * there are no pending data messages available or if there is no established
215*4882a593Smuzhiyun  * IUCV path.
216*4882a593Smuzhiyun  * If the IUCV path has been severed, then -EPIPE is returned to cause a
217*4882a593Smuzhiyun  * hang up (that is issued by the HVC layer).
218*4882a593Smuzhiyun  */
hvc_iucv_write(struct hvc_iucv_private * priv,char * buf,int count,int * has_more_data)219*4882a593Smuzhiyun static int hvc_iucv_write(struct hvc_iucv_private *priv,
220*4882a593Smuzhiyun 			  char *buf, int count, int *has_more_data)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct iucv_tty_buffer *rb;
223*4882a593Smuzhiyun 	int written;
224*4882a593Smuzhiyun 	int rc;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* immediately return if there is no IUCV connection */
227*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_DISCONN)
228*4882a593Smuzhiyun 		return 0;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* if the IUCV path has been severed, return -EPIPE to inform the
231*4882a593Smuzhiyun 	 * HVC layer to hang up the tty device. */
232*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_SEVERED)
233*4882a593Smuzhiyun 		return -EPIPE;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* check if there are pending messages */
236*4882a593Smuzhiyun 	if (list_empty(&priv->tty_inqueue))
237*4882a593Smuzhiyun 		return 0;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/* receive an iucv message and flip data to the tty (ldisc) */
240*4882a593Smuzhiyun 	rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	written = 0;
243*4882a593Smuzhiyun 	if (!rb->mbuf) { /* message not yet received ... */
244*4882a593Smuzhiyun 		/* allocate mem to store msg data; if no memory is available
245*4882a593Smuzhiyun 		 * then leave the buffer on the list and re-try later */
246*4882a593Smuzhiyun 		rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
247*4882a593Smuzhiyun 		if (!rb->mbuf)
248*4882a593Smuzhiyun 			return -ENOMEM;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 		rc = __iucv_message_receive(priv->path, &rb->msg, 0,
251*4882a593Smuzhiyun 					    rb->mbuf, rb->msg.length, NULL);
252*4882a593Smuzhiyun 		switch (rc) {
253*4882a593Smuzhiyun 		case 0: /* Successful	    */
254*4882a593Smuzhiyun 			break;
255*4882a593Smuzhiyun 		case 2:	/* No message found */
256*4882a593Smuzhiyun 		case 9: /* Message purged   */
257*4882a593Smuzhiyun 			break;
258*4882a593Smuzhiyun 		default:
259*4882a593Smuzhiyun 			written = -EIO;
260*4882a593Smuzhiyun 		}
261*4882a593Smuzhiyun 		/* remove buffer if an error has occurred or received data
262*4882a593Smuzhiyun 		 * is not correct */
263*4882a593Smuzhiyun 		if (rc || (rb->mbuf->version != MSG_VERSION) ||
264*4882a593Smuzhiyun 			  (rb->msg.length    != MSG_SIZE(rb->mbuf->datalen)))
265*4882a593Smuzhiyun 			goto out_remove_buffer;
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	switch (rb->mbuf->type) {
269*4882a593Smuzhiyun 	case MSG_TYPE_DATA:
270*4882a593Smuzhiyun 		written = min_t(int, rb->mbuf->datalen - rb->offset, count);
271*4882a593Smuzhiyun 		memcpy(buf, rb->mbuf->data + rb->offset, written);
272*4882a593Smuzhiyun 		if (written < (rb->mbuf->datalen - rb->offset)) {
273*4882a593Smuzhiyun 			rb->offset += written;
274*4882a593Smuzhiyun 			*has_more_data = 1;
275*4882a593Smuzhiyun 			goto out_written;
276*4882a593Smuzhiyun 		}
277*4882a593Smuzhiyun 		break;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	case MSG_TYPE_WINSIZE:
280*4882a593Smuzhiyun 		if (rb->mbuf->datalen != sizeof(struct winsize))
281*4882a593Smuzhiyun 			break;
282*4882a593Smuzhiyun 		/* The caller must ensure that the hvc is locked, which
283*4882a593Smuzhiyun 		 * is the case when called from hvc_iucv_get_chars() */
284*4882a593Smuzhiyun 		__hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
285*4882a593Smuzhiyun 		break;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	case MSG_TYPE_ERROR:	/* ignored ... */
288*4882a593Smuzhiyun 	case MSG_TYPE_TERMENV:	/* ignored ... */
289*4882a593Smuzhiyun 	case MSG_TYPE_TERMIOS:	/* ignored ... */
290*4882a593Smuzhiyun 		break;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun out_remove_buffer:
294*4882a593Smuzhiyun 	list_del(&rb->list);
295*4882a593Smuzhiyun 	destroy_tty_buffer(rb);
296*4882a593Smuzhiyun 	*has_more_data = !list_empty(&priv->tty_inqueue);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun out_written:
299*4882a593Smuzhiyun 	return written;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /**
303*4882a593Smuzhiyun  * hvc_iucv_get_chars() - HVC get_chars operation.
304*4882a593Smuzhiyun  * @vtermno:	HVC virtual terminal number.
305*4882a593Smuzhiyun  * @buf:	Pointer to a buffer to store data
306*4882a593Smuzhiyun  * @count:	Size of buffer available for writing
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * The HVC thread calls this method to read characters from the back-end.
309*4882a593Smuzhiyun  * If an IUCV communication path has been established, pending IUCV messages
310*4882a593Smuzhiyun  * are received and data is copied into buffer @buf up to @count bytes.
311*4882a593Smuzhiyun  *
312*4882a593Smuzhiyun  * Locking:	The routine gets called under an irqsave() spinlock; and
313*4882a593Smuzhiyun  *		the routine locks the struct hvc_iucv_private->lock to call
314*4882a593Smuzhiyun  *		helper functions.
315*4882a593Smuzhiyun  */
hvc_iucv_get_chars(uint32_t vtermno,char * buf,int count)316*4882a593Smuzhiyun static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
319*4882a593Smuzhiyun 	int written;
320*4882a593Smuzhiyun 	int has_more_data;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (count <= 0)
323*4882a593Smuzhiyun 		return 0;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (!priv)
326*4882a593Smuzhiyun 		return -ENODEV;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	spin_lock(&priv->lock);
329*4882a593Smuzhiyun 	has_more_data = 0;
330*4882a593Smuzhiyun 	written = hvc_iucv_write(priv, buf, count, &has_more_data);
331*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* if there are still messages on the queue... schedule another run */
334*4882a593Smuzhiyun 	if (has_more_data)
335*4882a593Smuzhiyun 		hvc_kick();
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	return written;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun  * hvc_iucv_queue() - Buffer terminal data for sending.
342*4882a593Smuzhiyun  * @priv:	Pointer to struct hvc_iucv_private instance.
343*4882a593Smuzhiyun  * @buf:	Buffer containing data to send.
344*4882a593Smuzhiyun  * @count:	Size of buffer and amount of data to send.
345*4882a593Smuzhiyun  *
346*4882a593Smuzhiyun  * The function queues data for sending. To actually send the buffered data,
347*4882a593Smuzhiyun  * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
348*4882a593Smuzhiyun  * The function returns the number of data bytes that has been buffered.
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  * If the device is not connected, data is ignored and the function returns
351*4882a593Smuzhiyun  * @count.
352*4882a593Smuzhiyun  * If the buffer is full, the function returns 0.
353*4882a593Smuzhiyun  * If an existing IUCV communicaton path has been severed, -EPIPE is returned
354*4882a593Smuzhiyun  * (that can be passed to HVC layer to cause a tty hangup).
355*4882a593Smuzhiyun  */
hvc_iucv_queue(struct hvc_iucv_private * priv,const char * buf,int count)356*4882a593Smuzhiyun static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
357*4882a593Smuzhiyun 			  int count)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	size_t len;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_DISCONN)
362*4882a593Smuzhiyun 		return count;			/* ignore data */
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_SEVERED)
365*4882a593Smuzhiyun 		return -EPIPE;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
368*4882a593Smuzhiyun 	if (!len)
369*4882a593Smuzhiyun 		return 0;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
372*4882a593Smuzhiyun 	priv->sndbuf_len += len;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_CONNECTED)
375*4882a593Smuzhiyun 		schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	return len;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun /**
381*4882a593Smuzhiyun  * hvc_iucv_send() - Send an IUCV message containing terminal data.
382*4882a593Smuzhiyun  * @priv:	Pointer to struct hvc_iucv_private instance.
383*4882a593Smuzhiyun  *
384*4882a593Smuzhiyun  * If an IUCV communication path has been established, the buffered output data
385*4882a593Smuzhiyun  * is sent via an IUCV message and the number of bytes sent is returned.
386*4882a593Smuzhiyun  * Returns 0 if there is no established IUCV communication path or
387*4882a593Smuzhiyun  * -EPIPE if an existing IUCV communicaton path has been severed.
388*4882a593Smuzhiyun  */
hvc_iucv_send(struct hvc_iucv_private * priv)389*4882a593Smuzhiyun static int hvc_iucv_send(struct hvc_iucv_private *priv)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	struct iucv_tty_buffer *sb;
392*4882a593Smuzhiyun 	int rc, len;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_SEVERED)
395*4882a593Smuzhiyun 		return -EPIPE;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_DISCONN)
398*4882a593Smuzhiyun 		return -EIO;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	if (!priv->sndbuf_len)
401*4882a593Smuzhiyun 		return 0;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	/* allocate internal buffer to store msg data and also compute total
404*4882a593Smuzhiyun 	 * message length */
405*4882a593Smuzhiyun 	sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
406*4882a593Smuzhiyun 	if (!sb)
407*4882a593Smuzhiyun 		return -ENOMEM;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
410*4882a593Smuzhiyun 	sb->mbuf->datalen = (u16) priv->sndbuf_len;
411*4882a593Smuzhiyun 	sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	list_add_tail(&sb->list, &priv->tty_outqueue);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
416*4882a593Smuzhiyun 				 (void *) sb->mbuf, sb->msg.length);
417*4882a593Smuzhiyun 	if (rc) {
418*4882a593Smuzhiyun 		/* drop the message here; however we might want to handle
419*4882a593Smuzhiyun 		 * 0x03 (msg limit reached) by trying again... */
420*4882a593Smuzhiyun 		list_del(&sb->list);
421*4882a593Smuzhiyun 		destroy_tty_buffer(sb);
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun 	len = priv->sndbuf_len;
424*4882a593Smuzhiyun 	priv->sndbuf_len = 0;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	return len;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun /**
430*4882a593Smuzhiyun  * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
431*4882a593Smuzhiyun  * @work:	Work structure.
432*4882a593Smuzhiyun  *
433*4882a593Smuzhiyun  * This work queue function sends buffered output data over IUCV and,
434*4882a593Smuzhiyun  * if not all buffered data could be sent, reschedules itself.
435*4882a593Smuzhiyun  */
hvc_iucv_sndbuf_work(struct work_struct * work)436*4882a593Smuzhiyun static void hvc_iucv_sndbuf_work(struct work_struct *work)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
441*4882a593Smuzhiyun 	if (!priv)
442*4882a593Smuzhiyun 		return;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
445*4882a593Smuzhiyun 	hvc_iucv_send(priv);
446*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun  * hvc_iucv_put_chars() - HVC put_chars operation.
451*4882a593Smuzhiyun  * @vtermno:	HVC virtual terminal number.
452*4882a593Smuzhiyun  * @buf:	Pointer to an buffer to read data from
453*4882a593Smuzhiyun  * @count:	Size of buffer available for reading
454*4882a593Smuzhiyun  *
455*4882a593Smuzhiyun  * The HVC thread calls this method to write characters to the back-end.
456*4882a593Smuzhiyun  * The function calls hvc_iucv_queue() to queue terminal data for sending.
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * Locking:	The method gets called under an irqsave() spinlock; and
459*4882a593Smuzhiyun  *		locks struct hvc_iucv_private->lock.
460*4882a593Smuzhiyun  */
hvc_iucv_put_chars(uint32_t vtermno,const char * buf,int count)461*4882a593Smuzhiyun static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
464*4882a593Smuzhiyun 	int queued;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	if (count <= 0)
467*4882a593Smuzhiyun 		return 0;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	if (!priv)
470*4882a593Smuzhiyun 		return -ENODEV;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	spin_lock(&priv->lock);
473*4882a593Smuzhiyun 	queued = hvc_iucv_queue(priv, buf, count);
474*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	return queued;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun  * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
481*4882a593Smuzhiyun  * @hp:	Pointer to the HVC device (struct hvc_struct)
482*4882a593Smuzhiyun  * @id:	Additional data (originally passed to hvc_alloc): the index of an struct
483*4882a593Smuzhiyun  *	hvc_iucv_private instance.
484*4882a593Smuzhiyun  *
485*4882a593Smuzhiyun  * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
486*4882a593Smuzhiyun  * instance that is derived from @id. Always returns 0.
487*4882a593Smuzhiyun  *
488*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock, spin_lock_bh
489*4882a593Smuzhiyun  */
hvc_iucv_notifier_add(struct hvc_struct * hp,int id)490*4882a593Smuzhiyun static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	priv = hvc_iucv_get_private(id);
495*4882a593Smuzhiyun 	if (!priv)
496*4882a593Smuzhiyun 		return 0;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
499*4882a593Smuzhiyun 	priv->tty_state = TTY_OPENED;
500*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	return 0;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun  * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
507*4882a593Smuzhiyun  * @priv:	Pointer to the struct hvc_iucv_private instance.
508*4882a593Smuzhiyun  */
hvc_iucv_cleanup(struct hvc_iucv_private * priv)509*4882a593Smuzhiyun static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	destroy_tty_buffer_list(&priv->tty_outqueue);
512*4882a593Smuzhiyun 	destroy_tty_buffer_list(&priv->tty_inqueue);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	priv->tty_state = TTY_CLOSED;
515*4882a593Smuzhiyun 	priv->iucv_state = IUCV_DISCONN;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	priv->sndbuf_len = 0;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /**
521*4882a593Smuzhiyun  * tty_outqueue_empty() - Test if the tty outq is empty
522*4882a593Smuzhiyun  * @priv:	Pointer to struct hvc_iucv_private instance.
523*4882a593Smuzhiyun  */
tty_outqueue_empty(struct hvc_iucv_private * priv)524*4882a593Smuzhiyun static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun 	int rc;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
529*4882a593Smuzhiyun 	rc = list_empty(&priv->tty_outqueue);
530*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	return rc;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun /**
536*4882a593Smuzhiyun  * flush_sndbuf_sync() - Flush send buffer and wait for completion
537*4882a593Smuzhiyun  * @priv:	Pointer to struct hvc_iucv_private instance.
538*4882a593Smuzhiyun  *
539*4882a593Smuzhiyun  * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
540*4882a593Smuzhiyun  * to flush any buffered terminal output data and waits for completion.
541*4882a593Smuzhiyun  */
flush_sndbuf_sync(struct hvc_iucv_private * priv)542*4882a593Smuzhiyun static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun 	int sync_wait;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	cancel_delayed_work_sync(&priv->sndbuf_work);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
549*4882a593Smuzhiyun 	hvc_iucv_send(priv);		/* force sending buffered data */
550*4882a593Smuzhiyun 	sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
551*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	if (sync_wait)
554*4882a593Smuzhiyun 		wait_event_timeout(priv->sndbuf_waitq,
555*4882a593Smuzhiyun 				   tty_outqueue_empty(priv), HZ/10);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun /**
559*4882a593Smuzhiyun  * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
560*4882a593Smuzhiyun  * @priv:	Pointer to hvc_iucv_private structure
561*4882a593Smuzhiyun  *
562*4882a593Smuzhiyun  * This routine severs an existing IUCV communication path and hangs
563*4882a593Smuzhiyun  * up the underlying HVC terminal device.
564*4882a593Smuzhiyun  * The hang-up occurs only if an IUCV communication path is established;
565*4882a593Smuzhiyun  * otherwise there is no need to hang up the terminal device.
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * The IUCV HVC hang-up is separated into two steps:
568*4882a593Smuzhiyun  * 1. After the IUCV path has been severed, the iucv_state is set to
569*4882a593Smuzhiyun  *    IUCV_SEVERED.
570*4882a593Smuzhiyun  * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
571*4882a593Smuzhiyun  *    IUCV_SEVERED state causes the tty hang-up in the HVC layer.
572*4882a593Smuzhiyun  *
573*4882a593Smuzhiyun  * If the tty has not yet been opened, clean up the hvc_iucv_private
574*4882a593Smuzhiyun  * structure to allow re-connects.
575*4882a593Smuzhiyun  * If the tty has been opened, let get_chars() return -EPIPE to signal
576*4882a593Smuzhiyun  * the HVC layer to hang up the tty and, if so, wake up the HVC thread
577*4882a593Smuzhiyun  * to call get_chars()...
578*4882a593Smuzhiyun  *
579*4882a593Smuzhiyun  * Special notes on hanging up a HVC terminal instantiated as console:
580*4882a593Smuzhiyun  * Hang-up:	1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
581*4882a593Smuzhiyun  *		2. do_tty_hangup() calls tty->ops->close() for console_filp
582*4882a593Smuzhiyun  *			=> no hangup notifier is called by HVC (default)
583*4882a593Smuzhiyun  *		2. hvc_close() returns because of tty_hung_up_p(filp)
584*4882a593Smuzhiyun  *			=> no delete notifier is called!
585*4882a593Smuzhiyun  * Finally, the back-end is not being notified, thus, the tty session is
586*4882a593Smuzhiyun  * kept active (TTY_OPEN) to be ready for re-connects.
587*4882a593Smuzhiyun  *
588*4882a593Smuzhiyun  * Locking:	spin_lock(&priv->lock) w/o disabling bh
589*4882a593Smuzhiyun  */
hvc_iucv_hangup(struct hvc_iucv_private * priv)590*4882a593Smuzhiyun static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	struct iucv_path *path;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	path = NULL;
595*4882a593Smuzhiyun 	spin_lock(&priv->lock);
596*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_CONNECTED) {
597*4882a593Smuzhiyun 		path = priv->path;
598*4882a593Smuzhiyun 		priv->path = NULL;
599*4882a593Smuzhiyun 		priv->iucv_state = IUCV_SEVERED;
600*4882a593Smuzhiyun 		if (priv->tty_state == TTY_CLOSED)
601*4882a593Smuzhiyun 			hvc_iucv_cleanup(priv);
602*4882a593Smuzhiyun 		else
603*4882a593Smuzhiyun 			/* console is special (see above) */
604*4882a593Smuzhiyun 			if (priv->is_console) {
605*4882a593Smuzhiyun 				hvc_iucv_cleanup(priv);
606*4882a593Smuzhiyun 				priv->tty_state = TTY_OPENED;
607*4882a593Smuzhiyun 			} else
608*4882a593Smuzhiyun 				hvc_kick();
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/* finally sever path (outside of priv->lock due to lock ordering) */
613*4882a593Smuzhiyun 	if (path) {
614*4882a593Smuzhiyun 		iucv_path_sever(path, NULL);
615*4882a593Smuzhiyun 		iucv_path_free(path);
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun /**
620*4882a593Smuzhiyun  * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
621*4882a593Smuzhiyun  * @hp:		Pointer to the HVC device (struct hvc_struct)
622*4882a593Smuzhiyun  * @id:		Additional data (originally passed to hvc_alloc):
623*4882a593Smuzhiyun  *		the index of an struct hvc_iucv_private instance.
624*4882a593Smuzhiyun  *
625*4882a593Smuzhiyun  * This routine notifies the HVC back-end that a tty hangup (carrier loss,
626*4882a593Smuzhiyun  * virtual or otherwise) has occurred.
627*4882a593Smuzhiyun  * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
628*4882a593Smuzhiyun  * to keep an existing IUCV communication path established.
629*4882a593Smuzhiyun  * (Background: vhangup() is called from user space (by getty or login) to
630*4882a593Smuzhiyun  *		disable writing to the tty by other applications).
631*4882a593Smuzhiyun  * If the tty has been opened and an established IUCV path has been severed
632*4882a593Smuzhiyun  * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
633*4882a593Smuzhiyun  *
634*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
635*4882a593Smuzhiyun  */
hvc_iucv_notifier_hangup(struct hvc_struct * hp,int id)636*4882a593Smuzhiyun static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	priv = hvc_iucv_get_private(id);
641*4882a593Smuzhiyun 	if (!priv)
642*4882a593Smuzhiyun 		return;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	flush_sndbuf_sync(priv);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
647*4882a593Smuzhiyun 	/* NOTE: If the hangup was scheduled by ourself (from the iucv
648*4882a593Smuzhiyun 	 *	 path_servered callback [IUCV_SEVERED]), we have to clean up
649*4882a593Smuzhiyun 	 *	 our structure and to set state to TTY_CLOSED.
650*4882a593Smuzhiyun 	 *	 If the tty was hung up otherwise (e.g. vhangup()), then we
651*4882a593Smuzhiyun 	 *	 ignore this hangup and keep an established IUCV path open...
652*4882a593Smuzhiyun 	 *	 (...the reason is that we are not able to connect back to the
653*4882a593Smuzhiyun 	 *	 client if we disconnect on hang up) */
654*4882a593Smuzhiyun 	priv->tty_state = TTY_CLOSED;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_SEVERED)
657*4882a593Smuzhiyun 		hvc_iucv_cleanup(priv);
658*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun /**
662*4882a593Smuzhiyun  * hvc_iucv_dtr_rts() - HVC notifier for handling DTR/RTS
663*4882a593Smuzhiyun  * @hp:		Pointer the HVC device (struct hvc_struct)
664*4882a593Smuzhiyun  * @raise:	Non-zero to raise or zero to lower DTR/RTS lines
665*4882a593Smuzhiyun  *
666*4882a593Smuzhiyun  * This routine notifies the HVC back-end to raise or lower DTR/RTS
667*4882a593Smuzhiyun  * lines.  Raising DTR/RTS is ignored.  Lowering DTR/RTS indicates to
668*4882a593Smuzhiyun  * drop the IUCV connection (similar to hang up the modem).
669*4882a593Smuzhiyun  */
hvc_iucv_dtr_rts(struct hvc_struct * hp,int raise)670*4882a593Smuzhiyun static void hvc_iucv_dtr_rts(struct hvc_struct *hp, int raise)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
673*4882a593Smuzhiyun 	struct iucv_path        *path;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	/* Raising the DTR/RTS is ignored as IUCV connections can be
676*4882a593Smuzhiyun 	 * established at any times.
677*4882a593Smuzhiyun 	 */
678*4882a593Smuzhiyun 	if (raise)
679*4882a593Smuzhiyun 		return;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	priv = hvc_iucv_get_private(hp->vtermno);
682*4882a593Smuzhiyun 	if (!priv)
683*4882a593Smuzhiyun 		return;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	/* Lowering the DTR/RTS lines disconnects an established IUCV
686*4882a593Smuzhiyun 	 * connection.
687*4882a593Smuzhiyun 	 */
688*4882a593Smuzhiyun 	flush_sndbuf_sync(priv);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
691*4882a593Smuzhiyun 	path = priv->path;		/* save reference to IUCV path */
692*4882a593Smuzhiyun 	priv->path = NULL;
693*4882a593Smuzhiyun 	priv->iucv_state = IUCV_DISCONN;
694*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	/* Sever IUCV path outside of priv->lock due to lock ordering of:
697*4882a593Smuzhiyun 	 * priv->lock <--> iucv_table_lock */
698*4882a593Smuzhiyun 	if (path) {
699*4882a593Smuzhiyun 		iucv_path_sever(path, NULL);
700*4882a593Smuzhiyun 		iucv_path_free(path);
701*4882a593Smuzhiyun 	}
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun /**
705*4882a593Smuzhiyun  * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
706*4882a593Smuzhiyun  * @hp:		Pointer to the HVC device (struct hvc_struct)
707*4882a593Smuzhiyun  * @id:		Additional data (originally passed to hvc_alloc):
708*4882a593Smuzhiyun  *		the index of an struct hvc_iucv_private instance.
709*4882a593Smuzhiyun  *
710*4882a593Smuzhiyun  * This routine notifies the HVC back-end that the last tty device fd has been
711*4882a593Smuzhiyun  * closed.  The function cleans up tty resources.  The clean-up of the IUCV
712*4882a593Smuzhiyun  * connection is done in hvc_iucv_dtr_rts() and depends on the HUPCL termios
713*4882a593Smuzhiyun  * control setting.
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
716*4882a593Smuzhiyun  */
hvc_iucv_notifier_del(struct hvc_struct * hp,int id)717*4882a593Smuzhiyun static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	priv = hvc_iucv_get_private(id);
722*4882a593Smuzhiyun 	if (!priv)
723*4882a593Smuzhiyun 		return;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	flush_sndbuf_sync(priv);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
728*4882a593Smuzhiyun 	destroy_tty_buffer_list(&priv->tty_outqueue);
729*4882a593Smuzhiyun 	destroy_tty_buffer_list(&priv->tty_inqueue);
730*4882a593Smuzhiyun 	priv->tty_state = TTY_CLOSED;
731*4882a593Smuzhiyun 	priv->sndbuf_len = 0;
732*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun /**
736*4882a593Smuzhiyun  * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
737*4882a593Smuzhiyun  * @ipvmid:	Originating z/VM user ID (right padded with blanks)
738*4882a593Smuzhiyun  *
739*4882a593Smuzhiyun  * Returns 0 if the z/VM user ID that is specified with @ipvmid is permitted to
740*4882a593Smuzhiyun  * connect, otherwise non-zero.
741*4882a593Smuzhiyun  */
hvc_iucv_filter_connreq(u8 ipvmid[8])742*4882a593Smuzhiyun static int hvc_iucv_filter_connreq(u8 ipvmid[8])
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun 	const char *wildcard, *filter_entry;
745*4882a593Smuzhiyun 	size_t i, len;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	/* Note: default policy is ACCEPT if no filter is set */
748*4882a593Smuzhiyun 	if (!hvc_iucv_filter_size)
749*4882a593Smuzhiyun 		return 0;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	for (i = 0; i < hvc_iucv_filter_size; i++) {
752*4882a593Smuzhiyun 		filter_entry = hvc_iucv_filter + (8 * i);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		/* If a filter entry contains the filter wildcard character,
755*4882a593Smuzhiyun 		 * reduce the length to match the leading portion of the user
756*4882a593Smuzhiyun 		 * ID only (wildcard match).  Characters following the wildcard
757*4882a593Smuzhiyun 		 * are ignored.
758*4882a593Smuzhiyun 		 */
759*4882a593Smuzhiyun 		wildcard = strnchr(filter_entry, 8, FILTER_WILDCARD_CHAR);
760*4882a593Smuzhiyun 		len = (wildcard) ? wildcard - filter_entry : 8;
761*4882a593Smuzhiyun 		if (0 == memcmp(ipvmid, filter_entry, len))
762*4882a593Smuzhiyun 			return 0;
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 	return 1;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun /**
768*4882a593Smuzhiyun  * hvc_iucv_path_pending() - IUCV handler to process a connection request.
769*4882a593Smuzhiyun  * @path:	Pending path (struct iucv_path)
770*4882a593Smuzhiyun  * @ipvmid:	z/VM system identifier of originator
771*4882a593Smuzhiyun  * @ipuser:	User specified data for this path
772*4882a593Smuzhiyun  *		(AF_IUCV: port/service name and originator port)
773*4882a593Smuzhiyun  *
774*4882a593Smuzhiyun  * The function uses the @ipuser data to determine if the pending path belongs
775*4882a593Smuzhiyun  * to a terminal managed by this device driver.
776*4882a593Smuzhiyun  * If the path belongs to this driver, ensure that the terminal is not accessed
777*4882a593Smuzhiyun  * multiple times (only one connection to a terminal is allowed).
778*4882a593Smuzhiyun  * If the terminal is not yet connected, the pending path is accepted and is
779*4882a593Smuzhiyun  * associated to the appropriate struct hvc_iucv_private instance.
780*4882a593Smuzhiyun  *
781*4882a593Smuzhiyun  * Returns 0 if @path belongs to a terminal managed by the this device driver;
782*4882a593Smuzhiyun  * otherwise returns -ENODEV in order to dispatch this path to other handlers.
783*4882a593Smuzhiyun  *
784*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
785*4882a593Smuzhiyun  */
hvc_iucv_path_pending(struct iucv_path * path,u8 * ipvmid,u8 * ipuser)786*4882a593Smuzhiyun static	int hvc_iucv_path_pending(struct iucv_path *path, u8 *ipvmid,
787*4882a593Smuzhiyun 				  u8 *ipuser)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun 	struct hvc_iucv_private *priv, *tmp;
790*4882a593Smuzhiyun 	u8 wildcard[9] = "lnxhvc  ";
791*4882a593Smuzhiyun 	int i, rc, find_unused;
792*4882a593Smuzhiyun 	u8 nuser_data[16];
793*4882a593Smuzhiyun 	u8 vm_user_id[9];
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	ASCEBC(wildcard, sizeof(wildcard));
796*4882a593Smuzhiyun 	find_unused = !memcmp(wildcard, ipuser, 8);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	/* First, check if the pending path request is managed by this
799*4882a593Smuzhiyun 	 * IUCV handler:
800*4882a593Smuzhiyun 	 * - find a disconnected device if ipuser contains the wildcard
801*4882a593Smuzhiyun 	 * - find the device that matches the terminal ID in ipuser
802*4882a593Smuzhiyun 	 */
803*4882a593Smuzhiyun 	priv = NULL;
804*4882a593Smuzhiyun 	for (i = 0; i < hvc_iucv_devices; i++) {
805*4882a593Smuzhiyun 		tmp = hvc_iucv_table[i];
806*4882a593Smuzhiyun 		if (!tmp)
807*4882a593Smuzhiyun 			continue;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		if (find_unused) {
810*4882a593Smuzhiyun 			spin_lock(&tmp->lock);
811*4882a593Smuzhiyun 			if (tmp->iucv_state == IUCV_DISCONN)
812*4882a593Smuzhiyun 				priv = tmp;
813*4882a593Smuzhiyun 			spin_unlock(&tmp->lock);
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 		} else if (!memcmp(tmp->srv_name, ipuser, 8))
816*4882a593Smuzhiyun 				priv = tmp;
817*4882a593Smuzhiyun 		if (priv)
818*4882a593Smuzhiyun 			break;
819*4882a593Smuzhiyun 	}
820*4882a593Smuzhiyun 	if (!priv)
821*4882a593Smuzhiyun 		return -ENODEV;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	/* Enforce that ipvmid is allowed to connect to us */
824*4882a593Smuzhiyun 	read_lock(&hvc_iucv_filter_lock);
825*4882a593Smuzhiyun 	rc = hvc_iucv_filter_connreq(ipvmid);
826*4882a593Smuzhiyun 	read_unlock(&hvc_iucv_filter_lock);
827*4882a593Smuzhiyun 	if (rc) {
828*4882a593Smuzhiyun 		iucv_path_sever(path, ipuser);
829*4882a593Smuzhiyun 		iucv_path_free(path);
830*4882a593Smuzhiyun 		memcpy(vm_user_id, ipvmid, 8);
831*4882a593Smuzhiyun 		vm_user_id[8] = 0;
832*4882a593Smuzhiyun 		pr_info("A connection request from z/VM user ID %s "
833*4882a593Smuzhiyun 			"was refused\n", vm_user_id);
834*4882a593Smuzhiyun 		return 0;
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	spin_lock(&priv->lock);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	/* If the terminal is already connected or being severed, then sever
840*4882a593Smuzhiyun 	 * this path to enforce that there is only ONE established communication
841*4882a593Smuzhiyun 	 * path per terminal. */
842*4882a593Smuzhiyun 	if (priv->iucv_state != IUCV_DISCONN) {
843*4882a593Smuzhiyun 		iucv_path_sever(path, ipuser);
844*4882a593Smuzhiyun 		iucv_path_free(path);
845*4882a593Smuzhiyun 		goto out_path_handled;
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	/* accept path */
849*4882a593Smuzhiyun 	memcpy(nuser_data, ipuser + 8, 8);  /* remote service (for af_iucv) */
850*4882a593Smuzhiyun 	memcpy(nuser_data + 8, ipuser, 8);  /* local service  (for af_iucv) */
851*4882a593Smuzhiyun 	path->msglim = 0xffff;		    /* IUCV MSGLIMIT */
852*4882a593Smuzhiyun 	path->flags &= ~IUCV_IPRMDATA;	    /* TODO: use IUCV_IPRMDATA */
853*4882a593Smuzhiyun 	rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
854*4882a593Smuzhiyun 	if (rc) {
855*4882a593Smuzhiyun 		iucv_path_sever(path, ipuser);
856*4882a593Smuzhiyun 		iucv_path_free(path);
857*4882a593Smuzhiyun 		goto out_path_handled;
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 	priv->path = path;
860*4882a593Smuzhiyun 	priv->iucv_state = IUCV_CONNECTED;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* store path information */
863*4882a593Smuzhiyun 	memcpy(priv->info_path, ipvmid, 8);
864*4882a593Smuzhiyun 	memcpy(priv->info_path + 8, ipuser + 8, 8);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	/* flush buffered output data... */
867*4882a593Smuzhiyun 	schedule_delayed_work(&priv->sndbuf_work, 5);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun out_path_handled:
870*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
871*4882a593Smuzhiyun 	return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun /**
875*4882a593Smuzhiyun  * hvc_iucv_path_severed() - IUCV handler to process a path sever.
876*4882a593Smuzhiyun  * @path:	Pending path (struct iucv_path)
877*4882a593Smuzhiyun  * @ipuser:	User specified data for this path
878*4882a593Smuzhiyun  *		(AF_IUCV: port/service name and originator port)
879*4882a593Smuzhiyun  *
880*4882a593Smuzhiyun  * This function calls the hvc_iucv_hangup() function for the
881*4882a593Smuzhiyun  * respective IUCV HVC terminal.
882*4882a593Smuzhiyun  *
883*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
884*4882a593Smuzhiyun  */
hvc_iucv_path_severed(struct iucv_path * path,u8 * ipuser)885*4882a593Smuzhiyun static void hvc_iucv_path_severed(struct iucv_path *path, u8 *ipuser)
886*4882a593Smuzhiyun {
887*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = path->private;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	hvc_iucv_hangup(priv);
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun /**
893*4882a593Smuzhiyun  * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
894*4882a593Smuzhiyun  * @path:	Pending path (struct iucv_path)
895*4882a593Smuzhiyun  * @msg:	Pointer to the IUCV message
896*4882a593Smuzhiyun  *
897*4882a593Smuzhiyun  * The function puts an incoming message on the input queue for later
898*4882a593Smuzhiyun  * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
899*4882a593Smuzhiyun  * If the tty has not yet been opened, the message is rejected.
900*4882a593Smuzhiyun  *
901*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
902*4882a593Smuzhiyun  */
hvc_iucv_msg_pending(struct iucv_path * path,struct iucv_message * msg)903*4882a593Smuzhiyun static void hvc_iucv_msg_pending(struct iucv_path *path,
904*4882a593Smuzhiyun 				 struct iucv_message *msg)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = path->private;
907*4882a593Smuzhiyun 	struct iucv_tty_buffer *rb;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	/* reject messages that exceed max size of iucv_tty_msg->datalen */
910*4882a593Smuzhiyun 	if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
911*4882a593Smuzhiyun 		iucv_message_reject(path, msg);
912*4882a593Smuzhiyun 		return;
913*4882a593Smuzhiyun 	}
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	spin_lock(&priv->lock);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	/* reject messages if tty has not yet been opened */
918*4882a593Smuzhiyun 	if (priv->tty_state == TTY_CLOSED) {
919*4882a593Smuzhiyun 		iucv_message_reject(path, msg);
920*4882a593Smuzhiyun 		goto unlock_return;
921*4882a593Smuzhiyun 	}
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/* allocate tty buffer to save iucv msg only */
924*4882a593Smuzhiyun 	rb = alloc_tty_buffer(0, GFP_ATOMIC);
925*4882a593Smuzhiyun 	if (!rb) {
926*4882a593Smuzhiyun 		iucv_message_reject(path, msg);
927*4882a593Smuzhiyun 		goto unlock_return;	/* -ENOMEM */
928*4882a593Smuzhiyun 	}
929*4882a593Smuzhiyun 	rb->msg = *msg;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	list_add_tail(&rb->list, &priv->tty_inqueue);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	hvc_kick();	/* wake up hvc thread */
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun unlock_return:
936*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun /**
940*4882a593Smuzhiyun  * hvc_iucv_msg_complete() - IUCV handler to process message completion
941*4882a593Smuzhiyun  * @path:	Pending path (struct iucv_path)
942*4882a593Smuzhiyun  * @msg:	Pointer to the IUCV message
943*4882a593Smuzhiyun  *
944*4882a593Smuzhiyun  * The function is called upon completion of message delivery to remove the
945*4882a593Smuzhiyun  * message from the outqueue. Additional delivery information can be found
946*4882a593Smuzhiyun  * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
947*4882a593Smuzhiyun  *	       purged messages	 (0x010000 (IPADPGNR)).
948*4882a593Smuzhiyun  *
949*4882a593Smuzhiyun  * Locking:	struct hvc_iucv_private->lock
950*4882a593Smuzhiyun  */
hvc_iucv_msg_complete(struct iucv_path * path,struct iucv_message * msg)951*4882a593Smuzhiyun static void hvc_iucv_msg_complete(struct iucv_path *path,
952*4882a593Smuzhiyun 				  struct iucv_message *msg)
953*4882a593Smuzhiyun {
954*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = path->private;
955*4882a593Smuzhiyun 	struct iucv_tty_buffer	*ent, *next;
956*4882a593Smuzhiyun 	LIST_HEAD(list_remove);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	spin_lock(&priv->lock);
959*4882a593Smuzhiyun 	list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
960*4882a593Smuzhiyun 		if (ent->msg.id == msg->id) {
961*4882a593Smuzhiyun 			list_move(&ent->list, &list_remove);
962*4882a593Smuzhiyun 			break;
963*4882a593Smuzhiyun 		}
964*4882a593Smuzhiyun 	wake_up(&priv->sndbuf_waitq);
965*4882a593Smuzhiyun 	spin_unlock(&priv->lock);
966*4882a593Smuzhiyun 	destroy_tty_buffer_list(&list_remove);
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun /**
970*4882a593Smuzhiyun  * hvc_iucv_pm_freeze() - Freeze PM callback
971*4882a593Smuzhiyun  * @dev:	IUVC HVC terminal device
972*4882a593Smuzhiyun  *
973*4882a593Smuzhiyun  * Sever an established IUCV communication path and
974*4882a593Smuzhiyun  * trigger a hang-up of the underlying HVC terminal.
975*4882a593Smuzhiyun  */
hvc_iucv_pm_freeze(struct device * dev)976*4882a593Smuzhiyun static int hvc_iucv_pm_freeze(struct device *dev)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = dev_get_drvdata(dev);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	local_bh_disable();
981*4882a593Smuzhiyun 	hvc_iucv_hangup(priv);
982*4882a593Smuzhiyun 	local_bh_enable();
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	return 0;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun /**
988*4882a593Smuzhiyun  * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
989*4882a593Smuzhiyun  * @dev:	IUVC HVC terminal device
990*4882a593Smuzhiyun  *
991*4882a593Smuzhiyun  * Wake up the HVC thread to trigger hang-up and respective
992*4882a593Smuzhiyun  * HVC back-end notifier invocations.
993*4882a593Smuzhiyun  */
hvc_iucv_pm_restore_thaw(struct device * dev)994*4882a593Smuzhiyun static int hvc_iucv_pm_restore_thaw(struct device *dev)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	hvc_kick();
997*4882a593Smuzhiyun 	return 0;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun 
hvc_iucv_dev_termid_show(struct device * dev,struct device_attribute * attr,char * buf)1000*4882a593Smuzhiyun static ssize_t hvc_iucv_dev_termid_show(struct device *dev,
1001*4882a593Smuzhiyun 					struct device_attribute *attr,
1002*4882a593Smuzhiyun 					char *buf)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = dev_get_drvdata(dev);
1005*4882a593Smuzhiyun 	size_t len;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	len = sizeof(priv->srv_name);
1008*4882a593Smuzhiyun 	memcpy(buf, priv->srv_name, len);
1009*4882a593Smuzhiyun 	EBCASC(buf, len);
1010*4882a593Smuzhiyun 	buf[len++] = '\n';
1011*4882a593Smuzhiyun 	return len;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun 
hvc_iucv_dev_state_show(struct device * dev,struct device_attribute * attr,char * buf)1014*4882a593Smuzhiyun static ssize_t hvc_iucv_dev_state_show(struct device *dev,
1015*4882a593Smuzhiyun 					struct device_attribute *attr,
1016*4882a593Smuzhiyun 					char *buf)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = dev_get_drvdata(dev);
1019*4882a593Smuzhiyun 	return sprintf(buf, "%u:%u\n", priv->iucv_state, priv->tty_state);
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun 
hvc_iucv_dev_peer_show(struct device * dev,struct device_attribute * attr,char * buf)1022*4882a593Smuzhiyun static ssize_t hvc_iucv_dev_peer_show(struct device *dev,
1023*4882a593Smuzhiyun 				      struct device_attribute *attr,
1024*4882a593Smuzhiyun 				      char *buf)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun 	struct hvc_iucv_private *priv = dev_get_drvdata(dev);
1027*4882a593Smuzhiyun 	char vmid[9], ipuser[9];
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	memset(vmid, 0, sizeof(vmid));
1030*4882a593Smuzhiyun 	memset(ipuser, 0, sizeof(ipuser));
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun 	spin_lock_bh(&priv->lock);
1033*4882a593Smuzhiyun 	if (priv->iucv_state == IUCV_CONNECTED) {
1034*4882a593Smuzhiyun 		memcpy(vmid, priv->info_path, 8);
1035*4882a593Smuzhiyun 		memcpy(ipuser, priv->info_path + 8, 8);
1036*4882a593Smuzhiyun 	}
1037*4882a593Smuzhiyun 	spin_unlock_bh(&priv->lock);
1038*4882a593Smuzhiyun 	EBCASC(ipuser, 8);
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	return sprintf(buf, "%s:%s\n", vmid, ipuser);
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun /* HVC operations */
1045*4882a593Smuzhiyun static const struct hv_ops hvc_iucv_ops = {
1046*4882a593Smuzhiyun 	.get_chars = hvc_iucv_get_chars,
1047*4882a593Smuzhiyun 	.put_chars = hvc_iucv_put_chars,
1048*4882a593Smuzhiyun 	.notifier_add = hvc_iucv_notifier_add,
1049*4882a593Smuzhiyun 	.notifier_del = hvc_iucv_notifier_del,
1050*4882a593Smuzhiyun 	.notifier_hangup = hvc_iucv_notifier_hangup,
1051*4882a593Smuzhiyun 	.dtr_rts = hvc_iucv_dtr_rts,
1052*4882a593Smuzhiyun };
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun /* Suspend / resume device operations */
1055*4882a593Smuzhiyun static const struct dev_pm_ops hvc_iucv_pm_ops = {
1056*4882a593Smuzhiyun 	.freeze	  = hvc_iucv_pm_freeze,
1057*4882a593Smuzhiyun 	.thaw	  = hvc_iucv_pm_restore_thaw,
1058*4882a593Smuzhiyun 	.restore  = hvc_iucv_pm_restore_thaw,
1059*4882a593Smuzhiyun };
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun /* IUCV HVC device driver */
1062*4882a593Smuzhiyun static struct device_driver hvc_iucv_driver = {
1063*4882a593Smuzhiyun 	.name = KMSG_COMPONENT,
1064*4882a593Smuzhiyun 	.bus  = &iucv_bus,
1065*4882a593Smuzhiyun 	.pm   = &hvc_iucv_pm_ops,
1066*4882a593Smuzhiyun };
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun /* IUCV HVC device attributes */
1069*4882a593Smuzhiyun static DEVICE_ATTR(termid, 0640, hvc_iucv_dev_termid_show, NULL);
1070*4882a593Smuzhiyun static DEVICE_ATTR(state, 0640, hvc_iucv_dev_state_show, NULL);
1071*4882a593Smuzhiyun static DEVICE_ATTR(peer, 0640, hvc_iucv_dev_peer_show, NULL);
1072*4882a593Smuzhiyun static struct attribute *hvc_iucv_dev_attrs[] = {
1073*4882a593Smuzhiyun 	&dev_attr_termid.attr,
1074*4882a593Smuzhiyun 	&dev_attr_state.attr,
1075*4882a593Smuzhiyun 	&dev_attr_peer.attr,
1076*4882a593Smuzhiyun 	NULL,
1077*4882a593Smuzhiyun };
1078*4882a593Smuzhiyun static struct attribute_group hvc_iucv_dev_attr_group = {
1079*4882a593Smuzhiyun 	.attrs = hvc_iucv_dev_attrs,
1080*4882a593Smuzhiyun };
1081*4882a593Smuzhiyun static const struct attribute_group *hvc_iucv_dev_attr_groups[] = {
1082*4882a593Smuzhiyun 	&hvc_iucv_dev_attr_group,
1083*4882a593Smuzhiyun 	NULL,
1084*4882a593Smuzhiyun };
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun /**
1088*4882a593Smuzhiyun  * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
1089*4882a593Smuzhiyun  * @id:			hvc_iucv_table index
1090*4882a593Smuzhiyun  * @is_console:		Flag if the instance is used as Linux console
1091*4882a593Smuzhiyun  *
1092*4882a593Smuzhiyun  * This function allocates a new hvc_iucv_private structure and stores
1093*4882a593Smuzhiyun  * the instance in hvc_iucv_table at index @id.
1094*4882a593Smuzhiyun  * Returns 0 on success; otherwise non-zero.
1095*4882a593Smuzhiyun  */
hvc_iucv_alloc(int id,unsigned int is_console)1096*4882a593Smuzhiyun static int __init hvc_iucv_alloc(int id, unsigned int is_console)
1097*4882a593Smuzhiyun {
1098*4882a593Smuzhiyun 	struct hvc_iucv_private *priv;
1099*4882a593Smuzhiyun 	char name[9];
1100*4882a593Smuzhiyun 	int rc;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
1103*4882a593Smuzhiyun 	if (!priv)
1104*4882a593Smuzhiyun 		return -ENOMEM;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	spin_lock_init(&priv->lock);
1107*4882a593Smuzhiyun 	INIT_LIST_HEAD(&priv->tty_outqueue);
1108*4882a593Smuzhiyun 	INIT_LIST_HEAD(&priv->tty_inqueue);
1109*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
1110*4882a593Smuzhiyun 	init_waitqueue_head(&priv->sndbuf_waitq);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
1113*4882a593Smuzhiyun 	if (!priv->sndbuf) {
1114*4882a593Smuzhiyun 		kfree(priv);
1115*4882a593Smuzhiyun 		return -ENOMEM;
1116*4882a593Smuzhiyun 	}
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	/* set console flag */
1119*4882a593Smuzhiyun 	priv->is_console = is_console;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	/* allocate hvc device */
1122*4882a593Smuzhiyun 	priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /*		  PAGE_SIZE */
1123*4882a593Smuzhiyun 			      HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
1124*4882a593Smuzhiyun 	if (IS_ERR(priv->hvc)) {
1125*4882a593Smuzhiyun 		rc = PTR_ERR(priv->hvc);
1126*4882a593Smuzhiyun 		goto out_error_hvc;
1127*4882a593Smuzhiyun 	}
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	/* notify HVC thread instead of using polling */
1130*4882a593Smuzhiyun 	priv->hvc->irq_requested = 1;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	/* setup iucv related information */
1133*4882a593Smuzhiyun 	snprintf(name, 9, "lnxhvc%-2d", id);
1134*4882a593Smuzhiyun 	memcpy(priv->srv_name, name, 8);
1135*4882a593Smuzhiyun 	ASCEBC(priv->srv_name, 8);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	/* create and setup device */
1138*4882a593Smuzhiyun 	priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
1139*4882a593Smuzhiyun 	if (!priv->dev) {
1140*4882a593Smuzhiyun 		rc = -ENOMEM;
1141*4882a593Smuzhiyun 		goto out_error_dev;
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 	dev_set_name(priv->dev, "hvc_iucv%d", id);
1144*4882a593Smuzhiyun 	dev_set_drvdata(priv->dev, priv);
1145*4882a593Smuzhiyun 	priv->dev->bus = &iucv_bus;
1146*4882a593Smuzhiyun 	priv->dev->parent = iucv_root;
1147*4882a593Smuzhiyun 	priv->dev->driver = &hvc_iucv_driver;
1148*4882a593Smuzhiyun 	priv->dev->groups = hvc_iucv_dev_attr_groups;
1149*4882a593Smuzhiyun 	priv->dev->release = (void (*)(struct device *)) kfree;
1150*4882a593Smuzhiyun 	rc = device_register(priv->dev);
1151*4882a593Smuzhiyun 	if (rc) {
1152*4882a593Smuzhiyun 		put_device(priv->dev);
1153*4882a593Smuzhiyun 		goto out_error_dev;
1154*4882a593Smuzhiyun 	}
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	hvc_iucv_table[id] = priv;
1157*4882a593Smuzhiyun 	return 0;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun out_error_dev:
1160*4882a593Smuzhiyun 	hvc_remove(priv->hvc);
1161*4882a593Smuzhiyun out_error_hvc:
1162*4882a593Smuzhiyun 	free_page((unsigned long) priv->sndbuf);
1163*4882a593Smuzhiyun 	kfree(priv);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	return rc;
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun /**
1169*4882a593Smuzhiyun  * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1170*4882a593Smuzhiyun  */
hvc_iucv_destroy(struct hvc_iucv_private * priv)1171*4882a593Smuzhiyun static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1172*4882a593Smuzhiyun {
1173*4882a593Smuzhiyun 	hvc_remove(priv->hvc);
1174*4882a593Smuzhiyun 	device_unregister(priv->dev);
1175*4882a593Smuzhiyun 	free_page((unsigned long) priv->sndbuf);
1176*4882a593Smuzhiyun 	kfree(priv);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun  * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
1181*4882a593Smuzhiyun  * @filter:	String containing a comma-separated list of z/VM user IDs
1182*4882a593Smuzhiyun  * @dest:	Location where to store the parsed z/VM user ID
1183*4882a593Smuzhiyun  */
hvc_iucv_parse_filter(const char * filter,char * dest)1184*4882a593Smuzhiyun static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1185*4882a593Smuzhiyun {
1186*4882a593Smuzhiyun 	const char *nextdelim, *residual;
1187*4882a593Smuzhiyun 	size_t len;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	nextdelim = strchr(filter, ',');
1190*4882a593Smuzhiyun 	if (nextdelim) {
1191*4882a593Smuzhiyun 		len = nextdelim - filter;
1192*4882a593Smuzhiyun 		residual = nextdelim + 1;
1193*4882a593Smuzhiyun 	} else {
1194*4882a593Smuzhiyun 		len = strlen(filter);
1195*4882a593Smuzhiyun 		residual = filter + len;
1196*4882a593Smuzhiyun 	}
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	if (len == 0)
1199*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	/* check for '\n' (if called from sysfs) */
1202*4882a593Smuzhiyun 	if (filter[len - 1] == '\n')
1203*4882a593Smuzhiyun 		len--;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	/* prohibit filter entries containing the wildcard character only */
1206*4882a593Smuzhiyun 	if (len == 1 && *filter == FILTER_WILDCARD_CHAR)
1207*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	if (len > 8)
1210*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	/* pad with blanks and save upper case version of user ID */
1213*4882a593Smuzhiyun 	memset(dest, ' ', 8);
1214*4882a593Smuzhiyun 	while (len--)
1215*4882a593Smuzhiyun 		dest[len] = toupper(filter[len]);
1216*4882a593Smuzhiyun 	return residual;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun /**
1220*4882a593Smuzhiyun  * hvc_iucv_setup_filter() - Set up z/VM user ID filter
1221*4882a593Smuzhiyun  * @filter:	String consisting of a comma-separated list of z/VM user IDs
1222*4882a593Smuzhiyun  *
1223*4882a593Smuzhiyun  * The function parses the @filter string and creates an array containing
1224*4882a593Smuzhiyun  * the list of z/VM user ID filter entries.
1225*4882a593Smuzhiyun  * Return code 0 means success, -EINVAL if the filter is syntactically
1226*4882a593Smuzhiyun  * incorrect, -ENOMEM if there was not enough memory to allocate the
1227*4882a593Smuzhiyun  * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
1228*4882a593Smuzhiyun  */
hvc_iucv_setup_filter(const char * val)1229*4882a593Smuzhiyun static int hvc_iucv_setup_filter(const char *val)
1230*4882a593Smuzhiyun {
1231*4882a593Smuzhiyun 	const char *residual;
1232*4882a593Smuzhiyun 	int err;
1233*4882a593Smuzhiyun 	size_t size, count;
1234*4882a593Smuzhiyun 	void *array, *old_filter;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	count = strlen(val);
1237*4882a593Smuzhiyun 	if (count == 0 || (count == 1 && val[0] == '\n')) {
1238*4882a593Smuzhiyun 		size  = 0;
1239*4882a593Smuzhiyun 		array = NULL;
1240*4882a593Smuzhiyun 		goto out_replace_filter;	/* clear filter */
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	/* count user IDs in order to allocate sufficient memory */
1244*4882a593Smuzhiyun 	size = 1;
1245*4882a593Smuzhiyun 	residual = val;
1246*4882a593Smuzhiyun 	while ((residual = strchr(residual, ',')) != NULL) {
1247*4882a593Smuzhiyun 		residual++;
1248*4882a593Smuzhiyun 		size++;
1249*4882a593Smuzhiyun 	}
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* check if the specified list exceeds the filter limit */
1252*4882a593Smuzhiyun 	if (size > MAX_VMID_FILTER)
1253*4882a593Smuzhiyun 		return -ENOSPC;
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	array = kcalloc(size, 8, GFP_KERNEL);
1256*4882a593Smuzhiyun 	if (!array)
1257*4882a593Smuzhiyun 		return -ENOMEM;
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	count = size;
1260*4882a593Smuzhiyun 	residual = val;
1261*4882a593Smuzhiyun 	while (*residual && count) {
1262*4882a593Smuzhiyun 		residual = hvc_iucv_parse_filter(residual,
1263*4882a593Smuzhiyun 						 array + ((size - count) * 8));
1264*4882a593Smuzhiyun 		if (IS_ERR(residual)) {
1265*4882a593Smuzhiyun 			err = PTR_ERR(residual);
1266*4882a593Smuzhiyun 			kfree(array);
1267*4882a593Smuzhiyun 			goto out_err;
1268*4882a593Smuzhiyun 		}
1269*4882a593Smuzhiyun 		count--;
1270*4882a593Smuzhiyun 	}
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun out_replace_filter:
1273*4882a593Smuzhiyun 	write_lock_bh(&hvc_iucv_filter_lock);
1274*4882a593Smuzhiyun 	old_filter = hvc_iucv_filter;
1275*4882a593Smuzhiyun 	hvc_iucv_filter_size = size;
1276*4882a593Smuzhiyun 	hvc_iucv_filter = array;
1277*4882a593Smuzhiyun 	write_unlock_bh(&hvc_iucv_filter_lock);
1278*4882a593Smuzhiyun 	kfree(old_filter);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	err = 0;
1281*4882a593Smuzhiyun out_err:
1282*4882a593Smuzhiyun 	return err;
1283*4882a593Smuzhiyun }
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun /**
1286*4882a593Smuzhiyun  * param_set_vmidfilter() - Set z/VM user ID filter parameter
1287*4882a593Smuzhiyun  * @val:	String consisting of a comma-separated list of z/VM user IDs
1288*4882a593Smuzhiyun  * @kp:		Kernel parameter pointing to hvc_iucv_filter array
1289*4882a593Smuzhiyun  *
1290*4882a593Smuzhiyun  * The function sets up the z/VM user ID filter specified as comma-separated
1291*4882a593Smuzhiyun  * list of user IDs in @val.
1292*4882a593Smuzhiyun  * Note: If it is called early in the boot process, @val is stored and
1293*4882a593Smuzhiyun  *	 parsed later in hvc_iucv_init().
1294*4882a593Smuzhiyun  */
param_set_vmidfilter(const char * val,const struct kernel_param * kp)1295*4882a593Smuzhiyun static int param_set_vmidfilter(const char *val, const struct kernel_param *kp)
1296*4882a593Smuzhiyun {
1297*4882a593Smuzhiyun 	int rc;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	if (!MACHINE_IS_VM || !hvc_iucv_devices)
1300*4882a593Smuzhiyun 		return -ENODEV;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	if (!val)
1303*4882a593Smuzhiyun 		return -EINVAL;
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	rc = 0;
1306*4882a593Smuzhiyun 	if (slab_is_available())
1307*4882a593Smuzhiyun 		rc = hvc_iucv_setup_filter(val);
1308*4882a593Smuzhiyun 	else
1309*4882a593Smuzhiyun 		hvc_iucv_filter_string = val;	/* defer... */
1310*4882a593Smuzhiyun 	return rc;
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun /**
1314*4882a593Smuzhiyun  * param_get_vmidfilter() - Get z/VM user ID filter
1315*4882a593Smuzhiyun  * @buffer:	Buffer to store z/VM user ID filter,
1316*4882a593Smuzhiyun  *		(buffer size assumption PAGE_SIZE)
1317*4882a593Smuzhiyun  * @kp:		Kernel parameter pointing to the hvc_iucv_filter array
1318*4882a593Smuzhiyun  *
1319*4882a593Smuzhiyun  * The function stores the filter as a comma-separated list of z/VM user IDs
1320*4882a593Smuzhiyun  * in @buffer. Typically, sysfs routines call this function for attr show.
1321*4882a593Smuzhiyun  */
param_get_vmidfilter(char * buffer,const struct kernel_param * kp)1322*4882a593Smuzhiyun static int param_get_vmidfilter(char *buffer, const struct kernel_param *kp)
1323*4882a593Smuzhiyun {
1324*4882a593Smuzhiyun 	int rc;
1325*4882a593Smuzhiyun 	size_t index, len;
1326*4882a593Smuzhiyun 	void *start, *end;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	if (!MACHINE_IS_VM || !hvc_iucv_devices)
1329*4882a593Smuzhiyun 		return -ENODEV;
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	rc = 0;
1332*4882a593Smuzhiyun 	read_lock_bh(&hvc_iucv_filter_lock);
1333*4882a593Smuzhiyun 	for (index = 0; index < hvc_iucv_filter_size; index++) {
1334*4882a593Smuzhiyun 		start = hvc_iucv_filter + (8 * index);
1335*4882a593Smuzhiyun 		end   = memchr(start, ' ', 8);
1336*4882a593Smuzhiyun 		len   = (end) ? end - start : 8;
1337*4882a593Smuzhiyun 		memcpy(buffer + rc, start, len);
1338*4882a593Smuzhiyun 		rc += len;
1339*4882a593Smuzhiyun 		buffer[rc++] = ',';
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 	read_unlock_bh(&hvc_iucv_filter_lock);
1342*4882a593Smuzhiyun 	if (rc)
1343*4882a593Smuzhiyun 		buffer[--rc] = '\0';	/* replace last comma and update rc */
1344*4882a593Smuzhiyun 	return rc;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun #define param_check_vmidfilter(name, p) __param_check(name, p, void)
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun static const struct kernel_param_ops param_ops_vmidfilter = {
1350*4882a593Smuzhiyun 	.set = param_set_vmidfilter,
1351*4882a593Smuzhiyun 	.get = param_get_vmidfilter,
1352*4882a593Smuzhiyun };
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun /**
1355*4882a593Smuzhiyun  * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
1356*4882a593Smuzhiyun  */
hvc_iucv_init(void)1357*4882a593Smuzhiyun static int __init hvc_iucv_init(void)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun 	int rc;
1360*4882a593Smuzhiyun 	unsigned int i;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	if (!hvc_iucv_devices)
1363*4882a593Smuzhiyun 		return -ENODEV;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	if (!MACHINE_IS_VM) {
1366*4882a593Smuzhiyun 		pr_notice("The z/VM IUCV HVC device driver cannot "
1367*4882a593Smuzhiyun 			   "be used without z/VM\n");
1368*4882a593Smuzhiyun 		rc = -ENODEV;
1369*4882a593Smuzhiyun 		goto out_error;
1370*4882a593Smuzhiyun 	}
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
1373*4882a593Smuzhiyun 		pr_err("%lu is not a valid value for the hvc_iucv= "
1374*4882a593Smuzhiyun 			"kernel parameter\n", hvc_iucv_devices);
1375*4882a593Smuzhiyun 		rc = -EINVAL;
1376*4882a593Smuzhiyun 		goto out_error;
1377*4882a593Smuzhiyun 	}
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	/* register IUCV HVC device driver */
1380*4882a593Smuzhiyun 	rc = driver_register(&hvc_iucv_driver);
1381*4882a593Smuzhiyun 	if (rc)
1382*4882a593Smuzhiyun 		goto out_error;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	/* parse hvc_iucv_allow string and create z/VM user ID filter list */
1385*4882a593Smuzhiyun 	if (hvc_iucv_filter_string) {
1386*4882a593Smuzhiyun 		rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
1387*4882a593Smuzhiyun 		switch (rc) {
1388*4882a593Smuzhiyun 		case 0:
1389*4882a593Smuzhiyun 			break;
1390*4882a593Smuzhiyun 		case -ENOMEM:
1391*4882a593Smuzhiyun 			pr_err("Allocating memory failed with "
1392*4882a593Smuzhiyun 				"reason code=%d\n", 3);
1393*4882a593Smuzhiyun 			goto out_error;
1394*4882a593Smuzhiyun 		case -EINVAL:
1395*4882a593Smuzhiyun 			pr_err("hvc_iucv_allow= does not specify a valid "
1396*4882a593Smuzhiyun 				"z/VM user ID list\n");
1397*4882a593Smuzhiyun 			goto out_error;
1398*4882a593Smuzhiyun 		case -ENOSPC:
1399*4882a593Smuzhiyun 			pr_err("hvc_iucv_allow= specifies too many "
1400*4882a593Smuzhiyun 				"z/VM user IDs\n");
1401*4882a593Smuzhiyun 			goto out_error;
1402*4882a593Smuzhiyun 		default:
1403*4882a593Smuzhiyun 			goto out_error;
1404*4882a593Smuzhiyun 		}
1405*4882a593Smuzhiyun 	}
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 	hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
1408*4882a593Smuzhiyun 					   sizeof(struct iucv_tty_buffer),
1409*4882a593Smuzhiyun 					   0, 0, NULL);
1410*4882a593Smuzhiyun 	if (!hvc_iucv_buffer_cache) {
1411*4882a593Smuzhiyun 		pr_err("Allocating memory failed with reason code=%d\n", 1);
1412*4882a593Smuzhiyun 		rc = -ENOMEM;
1413*4882a593Smuzhiyun 		goto out_error;
1414*4882a593Smuzhiyun 	}
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1417*4882a593Smuzhiyun 						    hvc_iucv_buffer_cache);
1418*4882a593Smuzhiyun 	if (!hvc_iucv_mempool) {
1419*4882a593Smuzhiyun 		pr_err("Allocating memory failed with reason code=%d\n", 2);
1420*4882a593Smuzhiyun 		kmem_cache_destroy(hvc_iucv_buffer_cache);
1421*4882a593Smuzhiyun 		rc = -ENOMEM;
1422*4882a593Smuzhiyun 		goto out_error;
1423*4882a593Smuzhiyun 	}
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	/* register the first terminal device as console
1426*4882a593Smuzhiyun 	 * (must be done before allocating hvc terminal devices) */
1427*4882a593Smuzhiyun 	rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
1428*4882a593Smuzhiyun 	if (rc) {
1429*4882a593Smuzhiyun 		pr_err("Registering HVC terminal device as "
1430*4882a593Smuzhiyun 		       "Linux console failed\n");
1431*4882a593Smuzhiyun 		goto out_error_memory;
1432*4882a593Smuzhiyun 	}
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* allocate hvc_iucv_private structs */
1435*4882a593Smuzhiyun 	for (i = 0; i < hvc_iucv_devices; i++) {
1436*4882a593Smuzhiyun 		rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
1437*4882a593Smuzhiyun 		if (rc) {
1438*4882a593Smuzhiyun 			pr_err("Creating a new HVC terminal device "
1439*4882a593Smuzhiyun 				"failed with error code=%d\n", rc);
1440*4882a593Smuzhiyun 			goto out_error_hvc;
1441*4882a593Smuzhiyun 		}
1442*4882a593Smuzhiyun 	}
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	/* register IUCV callback handler */
1445*4882a593Smuzhiyun 	rc = iucv_register(&hvc_iucv_handler, 0);
1446*4882a593Smuzhiyun 	if (rc) {
1447*4882a593Smuzhiyun 		pr_err("Registering IUCV handlers failed with error code=%d\n",
1448*4882a593Smuzhiyun 			rc);
1449*4882a593Smuzhiyun 		goto out_error_hvc;
1450*4882a593Smuzhiyun 	}
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	return 0;
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun out_error_hvc:
1455*4882a593Smuzhiyun 	for (i = 0; i < hvc_iucv_devices; i++)
1456*4882a593Smuzhiyun 		if (hvc_iucv_table[i])
1457*4882a593Smuzhiyun 			hvc_iucv_destroy(hvc_iucv_table[i]);
1458*4882a593Smuzhiyun out_error_memory:
1459*4882a593Smuzhiyun 	mempool_destroy(hvc_iucv_mempool);
1460*4882a593Smuzhiyun 	kmem_cache_destroy(hvc_iucv_buffer_cache);
1461*4882a593Smuzhiyun out_error:
1462*4882a593Smuzhiyun 	kfree(hvc_iucv_filter);
1463*4882a593Smuzhiyun 	hvc_iucv_devices = 0; /* ensure that we do not provide any device */
1464*4882a593Smuzhiyun 	return rc;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun /**
1468*4882a593Smuzhiyun  * hvc_iucv_config() - Parsing of hvc_iucv=  kernel command line parameter
1469*4882a593Smuzhiyun  * @val:	Parameter value (numeric)
1470*4882a593Smuzhiyun  */
hvc_iucv_config(char * val)1471*4882a593Smuzhiyun static	int __init hvc_iucv_config(char *val)
1472*4882a593Smuzhiyun {
1473*4882a593Smuzhiyun 	if (kstrtoul(val, 10, &hvc_iucv_devices))
1474*4882a593Smuzhiyun 		pr_warn("hvc_iucv= invalid parameter value '%s'\n", val);
1475*4882a593Smuzhiyun 	return 1;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun device_initcall(hvc_iucv_init);
1480*4882a593Smuzhiyun __setup("hvc_iucv=", hvc_iucv_config);
1481*4882a593Smuzhiyun core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);
1482