blob: ce1a25f779ce86271bb39280e95c487803295f62 [file] [log] [blame]
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001/*
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01002 * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01003 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01004 * This HVC device driver provides terminal access using
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01005 * z/VM IUCV communication paths.
6 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01007 * Copyright IBM Corp. 2008
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01008 *
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 */
11#define KMSG_COMPONENT "hvc_iucv"
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010012#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010013
14#include <linux/types.h>
15#include <asm/ebcdic.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010016#include <linux/delay.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010017#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/tty.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010020#include <linux/wait.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010021#include <net/iucv/iucv.h>
22
23#include "hvc_console.h"
24
25
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010026/* General device driver settings */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010027#define HVC_IUCV_MAGIC 0xc9e4c3e5
28#define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
29#define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
30
31/* IUCV TTY message */
32#define MSG_VERSION 0x02 /* Message version */
33#define MSG_TYPE_ERROR 0x01 /* Error message */
34#define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
35#define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
36#define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
37#define MSG_TYPE_DATA 0x10 /* Terminal data */
38
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010039struct iucv_tty_msg {
40 u8 version; /* Message version */
41 u8 type; /* Message type */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010042#define MSG_MAX_DATALEN ((u16)(~0))
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010043 u16 datalen; /* Payload length */
44 u8 data[]; /* Payload buffer */
45} __attribute__((packed));
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010046#define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010047
48enum iucv_state_t {
49 IUCV_DISCONN = 0,
50 IUCV_CONNECTED = 1,
51 IUCV_SEVERED = 2,
52};
53
54enum tty_state_t {
55 TTY_CLOSED = 0,
56 TTY_OPENED = 1,
57};
58
59struct hvc_iucv_private {
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010060 struct hvc_struct *hvc; /* HVC struct reference */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010061 u8 srv_name[8]; /* IUCV service name (ebcdic) */
62 enum iucv_state_t iucv_state; /* IUCV connection status */
63 enum tty_state_t tty_state; /* TTY status */
64 struct iucv_path *path; /* IUCV path pointer */
65 spinlock_t lock; /* hvc_iucv_private lock */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010066#define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
67 void *sndbuf; /* send buffer */
68 size_t sndbuf_len; /* length of send buffer */
69#define QUEUE_SNDBUF_DELAY (HZ / 25)
70 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
71 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010072 struct list_head tty_outqueue; /* outgoing IUCV messages */
73 struct list_head tty_inqueue; /* incoming IUCV messages */
74};
75
76struct iucv_tty_buffer {
77 struct list_head list; /* list pointer */
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010078 struct iucv_message msg; /* store an IUCV message */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010079 size_t offset; /* data buffer offset */
80 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
81};
82
83/* IUCV callback handler */
84static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
85static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
86static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
87static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
88
89
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +010090/* Kernel module parameter: use one terminal device as default */
91static unsigned long hvc_iucv_devices = 1;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010092
93/* Array of allocated hvc iucv tty lines... */
94static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
95
96/* Kmem cache and mempool for iucv_tty_buffer elements */
97static struct kmem_cache *hvc_iucv_buffer_cache;
98static mempool_t *hvc_iucv_mempool;
99
100/* IUCV handler callback functions */
101static struct iucv_handler hvc_iucv_handler = {
102 .path_pending = hvc_iucv_path_pending,
103 .path_severed = hvc_iucv_path_severed,
104 .message_complete = hvc_iucv_msg_complete,
105 .message_pending = hvc_iucv_msg_pending,
106};
107
108
109/**
110 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
111 * @num: The HVC virtual terminal number (vtermno)
112 *
113 * This function returns the struct hvc_iucv_private instance that corresponds
114 * to the HVC virtual terminal number specified as parameter @num.
115 */
116struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
117{
118 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
119 return NULL;
120 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
121}
122
123/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100124 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100125 * @size: Size of the internal buffer used to store data.
126 * @flags: Memory allocation flags passed to mempool.
127 *
128 * This function allocates a new struct iucv_tty_buffer element and, optionally,
129 * allocates an internal data buffer with the specified size @size.
130 * Note: The total message size arises from the internal buffer size and the
131 * members of the iucv_tty_msg structure.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100132 * The function returns NULL if memory allocation has failed.
133 */
134static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
135{
136 struct iucv_tty_buffer *bufp;
137
138 bufp = mempool_alloc(hvc_iucv_mempool, flags);
139 if (!bufp)
140 return NULL;
141 memset(bufp, 0, sizeof(struct iucv_tty_buffer));
142
143 if (size > 0) {
144 bufp->msg.length = MSG_SIZE(size);
145 bufp->mbuf = kmalloc(bufp->msg.length, flags);
146 if (!bufp->mbuf) {
147 mempool_free(bufp, hvc_iucv_mempool);
148 return NULL;
149 }
150 bufp->mbuf->version = MSG_VERSION;
151 bufp->mbuf->type = MSG_TYPE_DATA;
152 bufp->mbuf->datalen = (u16) size;
153 }
154 return bufp;
155}
156
157/**
158 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
159 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100160 */
161static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
162{
163 kfree(bufp->mbuf);
164 mempool_free(bufp, hvc_iucv_mempool);
165}
166
167/**
168 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100169 * @list: List containing struct iucv_tty_buffer elements.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100170 */
171static void destroy_tty_buffer_list(struct list_head *list)
172{
173 struct iucv_tty_buffer *ent, *next;
174
175 list_for_each_entry_safe(ent, next, list, list) {
176 list_del(&ent->list);
177 destroy_tty_buffer(ent);
178 }
179}
180
181/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100182 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
183 * @priv: Pointer to struct hvc_iucv_private
184 * @buf: HVC buffer for writing received terminal data.
185 * @count: HVC buffer size.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100186 * @has_more_data: Pointer to an int variable.
187 *
188 * The function picks up pending messages from the input queue and receives
189 * the message data that is then written to the specified buffer @buf.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100190 * If the buffer size @count is less than the data message size, the
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100191 * message is kept on the input queue and @has_more_data is set to 1.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100192 * If all message data has been written, the message is removed from
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100193 * the input queue.
194 *
195 * The function returns the number of bytes written to the terminal, zero if
196 * there are no pending data messages available or if there is no established
197 * IUCV path.
198 * If the IUCV path has been severed, then -EPIPE is returned to cause a
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100199 * hang up (that is issued by the HVC layer).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100200 */
201static int hvc_iucv_write(struct hvc_iucv_private *priv,
202 char *buf, int count, int *has_more_data)
203{
204 struct iucv_tty_buffer *rb;
205 int written;
206 int rc;
207
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100208 /* immediately return if there is no IUCV connection */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100209 if (priv->iucv_state == IUCV_DISCONN)
210 return 0;
211
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100212 /* if the IUCV path has been severed, return -EPIPE to inform the
213 * HVC layer to hang up the tty device. */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100214 if (priv->iucv_state == IUCV_SEVERED)
215 return -EPIPE;
216
217 /* check if there are pending messages */
218 if (list_empty(&priv->tty_inqueue))
219 return 0;
220
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100221 /* receive an iucv message and flip data to the tty (ldisc) */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100222 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
223
224 written = 0;
225 if (!rb->mbuf) { /* message not yet received ... */
226 /* allocate mem to store msg data; if no memory is available
227 * then leave the buffer on the list and re-try later */
228 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC);
229 if (!rb->mbuf)
230 return -ENOMEM;
231
232 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
233 rb->mbuf, rb->msg.length, NULL);
234 switch (rc) {
235 case 0: /* Successful */
236 break;
237 case 2: /* No message found */
238 case 9: /* Message purged */
239 break;
240 default:
241 written = -EIO;
242 }
243 /* remove buffer if an error has occured or received data
244 * is not correct */
245 if (rc || (rb->mbuf->version != MSG_VERSION) ||
246 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
247 goto out_remove_buffer;
248 }
249
250 switch (rb->mbuf->type) {
251 case MSG_TYPE_DATA:
252 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
253 memcpy(buf, rb->mbuf->data + rb->offset, written);
254 if (written < (rb->mbuf->datalen - rb->offset)) {
255 rb->offset += written;
256 *has_more_data = 1;
257 goto out_written;
258 }
259 break;
260
261 case MSG_TYPE_WINSIZE:
262 if (rb->mbuf->datalen != sizeof(struct winsize))
263 break;
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100264 hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100265 break;
266
267 case MSG_TYPE_ERROR: /* ignored ... */
268 case MSG_TYPE_TERMENV: /* ignored ... */
269 case MSG_TYPE_TERMIOS: /* ignored ... */
270 break;
271 }
272
273out_remove_buffer:
274 list_del(&rb->list);
275 destroy_tty_buffer(rb);
276 *has_more_data = !list_empty(&priv->tty_inqueue);
277
278out_written:
279 return written;
280}
281
282/**
283 * hvc_iucv_get_chars() - HVC get_chars operation.
284 * @vtermno: HVC virtual terminal number.
285 * @buf: Pointer to a buffer to store data
286 * @count: Size of buffer available for writing
287 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100288 * The HVC thread calls this method to read characters from the back-end.
289 * If an IUCV communication path has been established, pending IUCV messages
290 * are received and data is copied into buffer @buf up to @count bytes.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100291 *
292 * Locking: The routine gets called under an irqsave() spinlock; and
293 * the routine locks the struct hvc_iucv_private->lock to call
294 * helper functions.
295 */
296static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
297{
298 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
299 int written;
300 int has_more_data;
301
302 if (count <= 0)
303 return 0;
304
305 if (!priv)
306 return -ENODEV;
307
308 spin_lock(&priv->lock);
309 has_more_data = 0;
310 written = hvc_iucv_write(priv, buf, count, &has_more_data);
311 spin_unlock(&priv->lock);
312
313 /* if there are still messages on the queue... schedule another run */
314 if (has_more_data)
315 hvc_kick();
316
317 return written;
318}
319
320/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100321 * hvc_iucv_queue() - Buffer terminal data for sending.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100322 * @priv: Pointer to struct hvc_iucv_private instance.
323 * @buf: Buffer containing data to send.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100324 * @count: Size of buffer and amount of data to send.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100325 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100326 * The function queues data for sending. To actually send the buffered data,
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100327 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100328 * The function returns the number of data bytes that has been buffered.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100329 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100330 * If the device is not connected, data is ignored and the function returns
331 * @count.
332 * If the buffer is full, the function returns 0.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100333 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
334 * (that can be passed to HVC layer to cause a tty hangup).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100335 */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100336static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100337 int count)
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100338{
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100339 size_t len;
340
341 if (priv->iucv_state == IUCV_DISCONN)
342 return count; /* ignore data */
343
344 if (priv->iucv_state == IUCV_SEVERED)
345 return -EPIPE;
346
347 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
348 if (!len)
349 return 0;
350
351 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
352 priv->sndbuf_len += len;
353
354 if (priv->iucv_state == IUCV_CONNECTED)
355 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
356
357 return len;
358}
359
360/**
361 * hvc_iucv_send() - Send an IUCV message containing terminal data.
362 * @priv: Pointer to struct hvc_iucv_private instance.
363 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100364 * If an IUCV communication path has been established, the buffered output data
365 * is sent via an IUCV message and the number of bytes sent is returned.
366 * Returns 0 if there is no established IUCV communication path or
367 * -EPIPE if an existing IUCV communicaton path has been severed.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100368 */
369static int hvc_iucv_send(struct hvc_iucv_private *priv)
370{
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100371 struct iucv_tty_buffer *sb;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100372 int rc, len;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100373
374 if (priv->iucv_state == IUCV_SEVERED)
375 return -EPIPE;
376
377 if (priv->iucv_state == IUCV_DISCONN)
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100378 return -EIO;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100379
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100380 if (!priv->sndbuf_len)
381 return 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100382
383 /* allocate internal buffer to store msg data and also compute total
384 * message length */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100385 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100386 if (!sb)
387 return -ENOMEM;
388
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100389 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
390 sb->mbuf->datalen = (u16) priv->sndbuf_len;
391 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100392
393 list_add_tail(&sb->list, &priv->tty_outqueue);
394
395 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
396 (void *) sb->mbuf, sb->msg.length);
397 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100398 /* drop the message here; however we might want to handle
399 * 0x03 (msg limit reached) by trying again... */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100400 list_del(&sb->list);
401 destroy_tty_buffer(sb);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100402 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100403 len = priv->sndbuf_len;
404 priv->sndbuf_len = 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100405
406 return len;
407}
408
409/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100410 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
411 * @work: Work structure.
412 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100413 * This work queue function sends buffered output data over IUCV and,
414 * if not all buffered data could be sent, reschedules itself.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100415 */
416static void hvc_iucv_sndbuf_work(struct work_struct *work)
417{
418 struct hvc_iucv_private *priv;
419
420 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100421 if (!priv)
422 return;
423
424 spin_lock_bh(&priv->lock);
425 hvc_iucv_send(priv);
426 spin_unlock_bh(&priv->lock);
427}
428
429/**
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100430 * hvc_iucv_put_chars() - HVC put_chars operation.
431 * @vtermno: HVC virtual terminal number.
432 * @buf: Pointer to an buffer to read data from
433 * @count: Size of buffer available for reading
434 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100435 * The HVC thread calls this method to write characters to the back-end.
436 * The function calls hvc_iucv_queue() to queue terminal data for sending.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100437 *
438 * Locking: The method gets called under an irqsave() spinlock; and
439 * locks struct hvc_iucv_private->lock.
440 */
441static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
442{
443 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100444 int queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100445
446 if (count <= 0)
447 return 0;
448
449 if (!priv)
450 return -ENODEV;
451
452 spin_lock(&priv->lock);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100453 queued = hvc_iucv_queue(priv, buf, count);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100454 spin_unlock(&priv->lock);
455
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100456 return queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100457}
458
459/**
460 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
461 * @hp: Pointer to the HVC device (struct hvc_struct)
462 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
463 * hvc_iucv_private instance.
464 *
465 * The function sets the tty state to TTY_OPEN for the struct hvc_iucv_private
466 * instance that is derived from @id. Always returns 0.
467 *
468 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
469 */
470static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
471{
472 struct hvc_iucv_private *priv;
473
474 priv = hvc_iucv_get_private(id);
475 if (!priv)
476 return 0;
477
478 spin_lock_bh(&priv->lock);
479 priv->tty_state = TTY_OPENED;
480 spin_unlock_bh(&priv->lock);
481
482 return 0;
483}
484
485/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100486 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100487 * @priv: Pointer to the struct hvc_iucv_private instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100488 */
489static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
490{
491 destroy_tty_buffer_list(&priv->tty_outqueue);
492 destroy_tty_buffer_list(&priv->tty_inqueue);
493
494 priv->tty_state = TTY_CLOSED;
495 priv->iucv_state = IUCV_DISCONN;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100496
497 priv->sndbuf_len = 0;
498}
499
500/**
501 * tty_outqueue_empty() - Test if the tty outq is empty
502 * @priv: Pointer to struct hvc_iucv_private instance.
503 */
504static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
505{
506 int rc;
507
508 spin_lock_bh(&priv->lock);
509 rc = list_empty(&priv->tty_outqueue);
510 spin_unlock_bh(&priv->lock);
511
512 return rc;
513}
514
515/**
516 * flush_sndbuf_sync() - Flush send buffer and wait for completion
517 * @priv: Pointer to struct hvc_iucv_private instance.
518 *
519 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
520 * to flush any buffered terminal output data and waits for completion.
521 */
522static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
523{
524 int sync_wait;
525
526 cancel_delayed_work_sync(&priv->sndbuf_work);
527
528 spin_lock_bh(&priv->lock);
529 hvc_iucv_send(priv); /* force sending buffered data */
530 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
531 spin_unlock_bh(&priv->lock);
532
533 if (sync_wait)
534 wait_event_timeout(priv->sndbuf_waitq,
535 tty_outqueue_empty(priv), HZ);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100536}
537
538/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100539 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
540 * @hp: Pointer to the HVC device (struct hvc_struct)
541 * @id: Additional data (originally passed to hvc_alloc):
542 * the index of an struct hvc_iucv_private instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100543 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100544 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100545 * virtual or otherwise) has occured.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100546 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
547 * to keep an existing IUCV communication path established.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100548 * (Background: vhangup() is called from user space (by getty or login) to
549 * disable writing to the tty by other applications).
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100550 * If the tty has been opened and an established IUCV path has been severed
551 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100552 *
553 * Locking: struct hvc_iucv_private->lock
554 */
555static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
556{
557 struct hvc_iucv_private *priv;
558
559 priv = hvc_iucv_get_private(id);
560 if (!priv)
561 return;
562
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100563 flush_sndbuf_sync(priv);
564
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100565 spin_lock_bh(&priv->lock);
566 /* NOTE: If the hangup was scheduled by ourself (from the iucv
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100567 * path_servered callback [IUCV_SEVERED]), we have to clean up
568 * our structure and to set state to TTY_CLOSED.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100569 * If the tty was hung up otherwise (e.g. vhangup()), then we
570 * ignore this hangup and keep an established IUCV path open...
571 * (...the reason is that we are not able to connect back to the
572 * client if we disconnect on hang up) */
573 priv->tty_state = TTY_CLOSED;
574
575 if (priv->iucv_state == IUCV_SEVERED)
576 hvc_iucv_cleanup(priv);
577 spin_unlock_bh(&priv->lock);
578}
579
580/**
581 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
582 * @hp: Pointer to the HVC device (struct hvc_struct)
583 * @id: Additional data (originally passed to hvc_alloc):
584 * the index of an struct hvc_iucv_private instance.
585 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100586 * This routine notifies the HVC back-end that the last tty device fd has been
587 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
588 * hvc_iucv_private instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100589 *
590 * Locking: struct hvc_iucv_private->lock
591 */
592static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
593{
594 struct hvc_iucv_private *priv;
595 struct iucv_path *path;
596
597 priv = hvc_iucv_get_private(id);
598 if (!priv)
599 return;
600
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100601 flush_sndbuf_sync(priv);
602
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100603 spin_lock_bh(&priv->lock);
604 path = priv->path; /* save reference to IUCV path */
605 priv->path = NULL;
606 hvc_iucv_cleanup(priv);
607 spin_unlock_bh(&priv->lock);
608
609 /* sever IUCV path outside of priv->lock due to lock ordering of:
610 * priv->lock <--> iucv_table_lock */
611 if (path) {
612 iucv_path_sever(path, NULL);
613 iucv_path_free(path);
614 }
615}
616
617/**
618 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
619 * @path: Pending path (struct iucv_path)
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100620 * @ipvmid: z/VM system identifier of originator
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100621 * @ipuser: User specified data for this path
622 * (AF_IUCV: port/service name and originator port)
623 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100624 * The function uses the @ipuser data to determine if the pending path belongs
625 * to a terminal managed by this device driver.
626 * If the path belongs to this driver, ensure that the terminal is not accessed
627 * multiple times (only one connection to a terminal is allowed).
628 * If the terminal is not yet connected, the pending path is accepted and is
629 * associated to the appropriate struct hvc_iucv_private instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100630 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100631 * Returns 0 if @path belongs to a terminal managed by the this device driver;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100632 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
633 *
634 * Locking: struct hvc_iucv_private->lock
635 */
636static int hvc_iucv_path_pending(struct iucv_path *path,
637 u8 ipvmid[8], u8 ipuser[16])
638{
639 struct hvc_iucv_private *priv;
640 u8 nuser_data[16];
641 int i, rc;
642
643 priv = NULL;
644 for (i = 0; i < hvc_iucv_devices; i++)
645 if (hvc_iucv_table[i] &&
646 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
647 priv = hvc_iucv_table[i];
648 break;
649 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100650 if (!priv)
651 return -ENODEV;
652
653 spin_lock(&priv->lock);
654
655 /* If the terminal is already connected or being severed, then sever
656 * this path to enforce that there is only ONE established communication
657 * path per terminal. */
658 if (priv->iucv_state != IUCV_DISCONN) {
659 iucv_path_sever(path, ipuser);
660 iucv_path_free(path);
661 goto out_path_handled;
662 }
663
664 /* accept path */
665 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
666 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
667 path->msglim = 0xffff; /* IUCV MSGLIMIT */
668 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
669 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
670 if (rc) {
671 iucv_path_sever(path, ipuser);
672 iucv_path_free(path);
673 goto out_path_handled;
674 }
675 priv->path = path;
676 priv->iucv_state = IUCV_CONNECTED;
677
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100678 /* flush buffered output data... */
679 schedule_delayed_work(&priv->sndbuf_work, 5);
680
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100681out_path_handled:
682 spin_unlock(&priv->lock);
683 return 0;
684}
685
686/**
687 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
688 * @path: Pending path (struct iucv_path)
689 * @ipuser: User specified data for this path
690 * (AF_IUCV: port/service name and originator port)
691 *
692 * The function also severs the path (as required by the IUCV protocol) and
693 * sets the iucv state to IUCV_SEVERED for the associated struct
694 * hvc_iucv_private instance. Later, the IUCV_SEVERED state triggers a tty
695 * hangup (hvc_iucv_get_chars() / hvc_iucv_write()).
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100696 * If tty portion of the HVC is closed, clean up the outqueue.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100697 *
698 * Locking: struct hvc_iucv_private->lock
699 */
700static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
701{
702 struct hvc_iucv_private *priv = path->private;
703
704 spin_lock(&priv->lock);
705 priv->iucv_state = IUCV_SEVERED;
706
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100707 /* If the tty has not yet been opened, clean up the hvc_iucv_private
708 * structure to allow re-connects.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100709 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100710 * If it has been opened, let get_chars() return -EPIPE to signal the
711 * HVC layer to hang up the tty.
712 * If so, we need to wake up the HVC thread to call get_chars()...
713 */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100714 priv->path = NULL;
715 if (priv->tty_state == TTY_CLOSED)
716 hvc_iucv_cleanup(priv);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100717 else
718 hvc_kick();
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100719 spin_unlock(&priv->lock);
720
721 /* finally sever path (outside of priv->lock due to lock ordering) */
722 iucv_path_sever(path, ipuser);
723 iucv_path_free(path);
724}
725
726/**
727 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
728 * @path: Pending path (struct iucv_path)
729 * @msg: Pointer to the IUCV message
730 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100731 * The function puts an incoming message on the input queue for later
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100732 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100733 * If the tty has not yet been opened, the message is rejected.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100734 *
735 * Locking: struct hvc_iucv_private->lock
736 */
737static void hvc_iucv_msg_pending(struct iucv_path *path,
738 struct iucv_message *msg)
739{
740 struct hvc_iucv_private *priv = path->private;
741 struct iucv_tty_buffer *rb;
742
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100743 /* reject messages that exceed max size of iucv_tty_msg->datalen */
744 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
745 iucv_message_reject(path, msg);
746 return;
747 }
748
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100749
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100750 spin_lock(&priv->lock);
751
752 /* reject messages if tty has not yet been opened */
753 if (priv->tty_state == TTY_CLOSED) {
754 iucv_message_reject(path, msg);
755 goto unlock_return;
756 }
757
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100758 /* allocate tty buffer to save iucv msg only */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100759 rb = alloc_tty_buffer(0, GFP_ATOMIC);
760 if (!rb) {
761 iucv_message_reject(path, msg);
762 goto unlock_return; /* -ENOMEM */
763 }
764 rb->msg = *msg;
765
766 list_add_tail(&rb->list, &priv->tty_inqueue);
767
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100768 hvc_kick(); /* wake up hvc thread */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100769
770unlock_return:
771 spin_unlock(&priv->lock);
772}
773
774/**
775 * hvc_iucv_msg_complete() - IUCV handler to process message completion
776 * @path: Pending path (struct iucv_path)
777 * @msg: Pointer to the IUCV message
778 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100779 * The function is called upon completion of message delivery to remove the
780 * message from the outqueue. Additional delivery information can be found
781 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
782 * purged messages (0x010000 (IPADPGNR)).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100783 *
784 * Locking: struct hvc_iucv_private->lock
785 */
786static void hvc_iucv_msg_complete(struct iucv_path *path,
787 struct iucv_message *msg)
788{
789 struct hvc_iucv_private *priv = path->private;
790 struct iucv_tty_buffer *ent, *next;
791 LIST_HEAD(list_remove);
792
793 spin_lock(&priv->lock);
794 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
795 if (ent->msg.id == msg->id) {
796 list_move(&ent->list, &list_remove);
797 break;
798 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100799 wake_up(&priv->sndbuf_waitq);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100800 spin_unlock(&priv->lock);
801 destroy_tty_buffer_list(&list_remove);
802}
803
804
805/* HVC operations */
806static struct hv_ops hvc_iucv_ops = {
807 .get_chars = hvc_iucv_get_chars,
808 .put_chars = hvc_iucv_put_chars,
809 .notifier_add = hvc_iucv_notifier_add,
810 .notifier_del = hvc_iucv_notifier_del,
811 .notifier_hangup = hvc_iucv_notifier_hangup,
812};
813
814/**
815 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
816 * @id: hvc_iucv_table index
817 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100818 * This function allocates a new hvc_iucv_private structure and stores
819 * the instance in hvc_iucv_table at index @id.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100820 * Returns 0 on success; otherwise non-zero.
821 */
822static int __init hvc_iucv_alloc(int id)
823{
824 struct hvc_iucv_private *priv;
825 char name[9];
826 int rc;
827
828 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
829 if (!priv)
830 return -ENOMEM;
831
832 spin_lock_init(&priv->lock);
833 INIT_LIST_HEAD(&priv->tty_outqueue);
834 INIT_LIST_HEAD(&priv->tty_inqueue);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100835 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
836 init_waitqueue_head(&priv->sndbuf_waitq);
837
838 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
839 if (!priv->sndbuf) {
840 kfree(priv);
841 return -ENOMEM;
842 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100843
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100844 /* finally allocate hvc */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100845 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
846 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100847 if (IS_ERR(priv->hvc)) {
848 rc = PTR_ERR(priv->hvc);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100849 free_page((unsigned long) priv->sndbuf);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100850 kfree(priv);
851 return rc;
852 }
853
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100854 /* notify HVC thread instead of using polling */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100855 priv->hvc->irq_requested = 1;
856
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100857 /* setup iucv related information */
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +0100858 snprintf(name, 9, "lnxhvc%-2d", id);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100859 memcpy(priv->srv_name, name, 8);
860 ASCEBC(priv->srv_name, 8);
861
862 hvc_iucv_table[id] = priv;
863 return 0;
864}
865
866/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100867 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100868 */
869static int __init hvc_iucv_init(void)
870{
871 int rc, i;
872
873 if (!MACHINE_IS_VM) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100874 pr_info("The z/VM IUCV HVC device driver cannot "
875 "be used without z/VM\n");
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100876 return -ENODEV;
877 }
878
879 if (!hvc_iucv_devices)
880 return -ENODEV;
881
882 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES)
883 return -EINVAL;
884
885 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
886 sizeof(struct iucv_tty_buffer),
887 0, 0, NULL);
888 if (!hvc_iucv_buffer_cache) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100889 pr_err("Allocating memory failed with reason code=%d\n", 1);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100890 return -ENOMEM;
891 }
892
893 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
894 hvc_iucv_buffer_cache);
895 if (!hvc_iucv_mempool) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100896 pr_err("Allocating memory failed with reason code=%d\n", 2);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100897 kmem_cache_destroy(hvc_iucv_buffer_cache);
898 return -ENOMEM;
899 }
900
901 /* allocate hvc_iucv_private structs */
902 for (i = 0; i < hvc_iucv_devices; i++) {
903 rc = hvc_iucv_alloc(i);
904 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100905 pr_err("Creating a new HVC terminal device "
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100906 "failed with error code=%d\n", rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100907 goto out_error_hvc;
908 }
909 }
910
911 /* register IUCV callback handler */
912 rc = iucv_register(&hvc_iucv_handler, 0);
913 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100914 pr_err("Registering IUCV handlers failed with error code=%d\n",
915 rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100916 goto out_error_iucv;
917 }
918
919 return 0;
920
921out_error_iucv:
922 iucv_unregister(&hvc_iucv_handler, 0);
923out_error_hvc:
924 for (i = 0; i < hvc_iucv_devices; i++)
925 if (hvc_iucv_table[i]) {
926 if (hvc_iucv_table[i]->hvc)
927 hvc_remove(hvc_iucv_table[i]->hvc);
928 kfree(hvc_iucv_table[i]);
929 }
930 mempool_destroy(hvc_iucv_mempool);
931 kmem_cache_destroy(hvc_iucv_buffer_cache);
932 return rc;
933}
934
935/**
936 * hvc_iucv_console_init() - Early console initialization
937 */
938static int __init hvc_iucv_console_init(void)
939{
940 if (!MACHINE_IS_VM || !hvc_iucv_devices)
941 return -ENODEV;
942 return hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops);
943}
944
945/**
946 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
947 * @val: Parameter value (numeric)
948 */
949static int __init hvc_iucv_config(char *val)
950{
951 return strict_strtoul(val, 10, &hvc_iucv_devices);
952}
953
954
955module_init(hvc_iucv_init);
956console_initcall(hvc_iucv_console_init);
957__setup("hvc_iucv=", hvc_iucv_config);
958
959MODULE_LICENSE("GPL");
960MODULE_DESCRIPTION("HVC back-end for z/VM IUCV.");
961MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");