blob: a23f7e8fba86a8795f540d85a6c788fece42a28f [file] [log] [blame]
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001/*
2 * hvc_iucv.c - z/VM IUCV back-end for the Hypervisor Console (HVC)
3 *
4 * This back-end for HVC provides terminal access via
5 * z/VM IUCV communication paths.
6 *
7 * Copyright IBM Corp. 2008.
8 *
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 */
11#define KMSG_COMPONENT "hvc_iucv"
12
13#include <linux/types.h>
14#include <asm/ebcdic.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010015#include <linux/delay.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010016#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/tty.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010019#include <linux/wait.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010020#include <net/iucv/iucv.h>
21
22#include "hvc_console.h"
23
24
25/* HVC backend for z/VM IUCV */
26#define HVC_IUCV_MAGIC 0xc9e4c3e5
27#define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
28#define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
29
30/* IUCV TTY message */
31#define MSG_VERSION 0x02 /* Message version */
32#define MSG_TYPE_ERROR 0x01 /* Error message */
33#define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
34#define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
35#define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
36#define MSG_TYPE_DATA 0x10 /* Terminal data */
37
38#define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
39struct 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));
46
47enum iucv_state_t {
48 IUCV_DISCONN = 0,
49 IUCV_CONNECTED = 1,
50 IUCV_SEVERED = 2,
51};
52
53enum tty_state_t {
54 TTY_CLOSED = 0,
55 TTY_OPENED = 1,
56};
57
58struct hvc_iucv_private {
59 struct hvc_struct *hvc; /* HVC console struct reference */
60 u8 srv_name[8]; /* IUCV service name (ebcdic) */
61 enum iucv_state_t iucv_state; /* IUCV connection status */
62 enum tty_state_t tty_state; /* TTY status */
63 struct iucv_path *path; /* IUCV path pointer */
64 spinlock_t lock; /* hvc_iucv_private lock */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010065#define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
66 void *sndbuf; /* send buffer */
67 size_t sndbuf_len; /* length of send buffer */
68#define QUEUE_SNDBUF_DELAY (HZ / 25)
69 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
70 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010071 struct list_head tty_outqueue; /* outgoing IUCV messages */
72 struct list_head tty_inqueue; /* incoming IUCV messages */
73};
74
75struct iucv_tty_buffer {
76 struct list_head list; /* list pointer */
77 struct iucv_message msg; /* store an incoming IUCV message */
78 size_t offset; /* data buffer offset */
79 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
80};
81
82/* IUCV callback handler */
83static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
84static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
85static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
86static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
87
88
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +010089/* Kernel module parameter: use one terminal device as default */
90static unsigned long hvc_iucv_devices = 1;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010091
92/* Array of allocated hvc iucv tty lines... */
93static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
94
95/* Kmem cache and mempool for iucv_tty_buffer elements */
96static struct kmem_cache *hvc_iucv_buffer_cache;
97static mempool_t *hvc_iucv_mempool;
98
99/* IUCV handler callback functions */
100static struct iucv_handler hvc_iucv_handler = {
101 .path_pending = hvc_iucv_path_pending,
102 .path_severed = hvc_iucv_path_severed,
103 .message_complete = hvc_iucv_msg_complete,
104 .message_pending = hvc_iucv_msg_pending,
105};
106
107
108/**
109 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
110 * @num: The HVC virtual terminal number (vtermno)
111 *
112 * This function returns the struct hvc_iucv_private instance that corresponds
113 * to the HVC virtual terminal number specified as parameter @num.
114 */
115struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
116{
117 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
118 return NULL;
119 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
120}
121
122/**
123 * alloc_tty_buffer() - Returns a new struct iucv_tty_buffer element.
124 * @size: Size of the internal buffer used to store data.
125 * @flags: Memory allocation flags passed to mempool.
126 *
127 * This function allocates a new struct iucv_tty_buffer element and, optionally,
128 * allocates an internal data buffer with the specified size @size.
129 * Note: The total message size arises from the internal buffer size and the
130 * members of the iucv_tty_msg structure.
131 *
132 * 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.
160 *
161 * The destroy_tty_buffer() function frees the internal data buffer and returns
162 * the struct iucv_tty_buffer element back to the mempool for freeing.
163 */
164static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
165{
166 kfree(bufp->mbuf);
167 mempool_free(bufp, hvc_iucv_mempool);
168}
169
170/**
171 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
172 * @list: List head pointer to a list containing struct iucv_tty_buffer
173 * elements.
174 *
175 * Calls destroy_tty_buffer() for each struct iucv_tty_buffer element in the
176 * list @list.
177 */
178static void destroy_tty_buffer_list(struct list_head *list)
179{
180 struct iucv_tty_buffer *ent, *next;
181
182 list_for_each_entry_safe(ent, next, list, list) {
183 list_del(&ent->list);
184 destroy_tty_buffer(ent);
185 }
186}
187
188/**
189 * hvc_iucv_write() - Receive IUCV message write data to HVC console buffer.
190 * @priv: Pointer to hvc_iucv_private structure.
191 * @buf: HVC console buffer for writing received terminal data.
192 * @count: HVC console buffer size.
193 * @has_more_data: Pointer to an int variable.
194 *
195 * The function picks up pending messages from the input queue and receives
196 * the message data that is then written to the specified buffer @buf.
197 * If the buffer size @count is less than the data message size, then the
198 * message is kept on the input queue and @has_more_data is set to 1.
199 * If the message data has been entirely written, the message is removed from
200 * the input queue.
201 *
202 * The function returns the number of bytes written to the terminal, zero if
203 * there are no pending data messages available or if there is no established
204 * IUCV path.
205 * If the IUCV path has been severed, then -EPIPE is returned to cause a
206 * hang up (that is issued by the HVC console layer).
207 */
208static int hvc_iucv_write(struct hvc_iucv_private *priv,
209 char *buf, int count, int *has_more_data)
210{
211 struct iucv_tty_buffer *rb;
212 int written;
213 int rc;
214
215 /* Immediately return if there is no IUCV connection */
216 if (priv->iucv_state == IUCV_DISCONN)
217 return 0;
218
219 /* If the IUCV path has been severed, return -EPIPE to inform the
220 * hvc console layer to hang up the tty device. */
221 if (priv->iucv_state == IUCV_SEVERED)
222 return -EPIPE;
223
224 /* check if there are pending messages */
225 if (list_empty(&priv->tty_inqueue))
226 return 0;
227
228 /* receive a iucv message and flip data to the tty (ldisc) */
229 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
230
231 written = 0;
232 if (!rb->mbuf) { /* message not yet received ... */
233 /* allocate mem to store msg data; if no memory is available
234 * then leave the buffer on the list and re-try later */
235 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC);
236 if (!rb->mbuf)
237 return -ENOMEM;
238
239 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
240 rb->mbuf, rb->msg.length, NULL);
241 switch (rc) {
242 case 0: /* Successful */
243 break;
244 case 2: /* No message found */
245 case 9: /* Message purged */
246 break;
247 default:
248 written = -EIO;
249 }
250 /* remove buffer if an error has occured or received data
251 * is not correct */
252 if (rc || (rb->mbuf->version != MSG_VERSION) ||
253 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
254 goto out_remove_buffer;
255 }
256
257 switch (rb->mbuf->type) {
258 case MSG_TYPE_DATA:
259 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
260 memcpy(buf, rb->mbuf->data + rb->offset, written);
261 if (written < (rb->mbuf->datalen - rb->offset)) {
262 rb->offset += written;
263 *has_more_data = 1;
264 goto out_written;
265 }
266 break;
267
268 case MSG_TYPE_WINSIZE:
269 if (rb->mbuf->datalen != sizeof(struct winsize))
270 break;
271 hvc_resize(priv->hvc, *((struct winsize *)rb->mbuf->data));
272 break;
273
274 case MSG_TYPE_ERROR: /* ignored ... */
275 case MSG_TYPE_TERMENV: /* ignored ... */
276 case MSG_TYPE_TERMIOS: /* ignored ... */
277 break;
278 }
279
280out_remove_buffer:
281 list_del(&rb->list);
282 destroy_tty_buffer(rb);
283 *has_more_data = !list_empty(&priv->tty_inqueue);
284
285out_written:
286 return written;
287}
288
289/**
290 * hvc_iucv_get_chars() - HVC get_chars operation.
291 * @vtermno: HVC virtual terminal number.
292 * @buf: Pointer to a buffer to store data
293 * @count: Size of buffer available for writing
294 *
295 * The hvc_console thread calls this method to read characters from
296 * the terminal backend. If an IUCV communication path has been established,
297 * pending IUCV messages are received and data is copied into buffer @buf
298 * up to @count bytes.
299 *
300 * Locking: The routine gets called under an irqsave() spinlock; and
301 * the routine locks the struct hvc_iucv_private->lock to call
302 * helper functions.
303 */
304static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
305{
306 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
307 int written;
308 int has_more_data;
309
310 if (count <= 0)
311 return 0;
312
313 if (!priv)
314 return -ENODEV;
315
316 spin_lock(&priv->lock);
317 has_more_data = 0;
318 written = hvc_iucv_write(priv, buf, count, &has_more_data);
319 spin_unlock(&priv->lock);
320
321 /* if there are still messages on the queue... schedule another run */
322 if (has_more_data)
323 hvc_kick();
324
325 return written;
326}
327
328/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100329 * hvc_iucv_queue() - Buffer terminal data for sending.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100330 * @priv: Pointer to struct hvc_iucv_private instance.
331 * @buf: Buffer containing data to send.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100332 * @count: Size of buffer and amount of data to send.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100333 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100334 * The function queues data for sending. To actually send the buffered data,
335 * a work queue function is * scheduled (with QUEUE_SNDBUF_DELAY).
336 * The function returns the number of data bytes that has been buffered.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100337 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100338 * If the device is not connected, data is ignored and the function returns
339 * @count.
340 * If the buffer is full, the function returns 0.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100341 * If an existing IUCV communicaton path has been severed, the function returns
342 * -EPIPE (can be passed to HVC layer to cause a tty hangup).
343 */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100344static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100345 int count)
346{
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100347 size_t len;
348
349 if (priv->iucv_state == IUCV_DISCONN)
350 return count; /* ignore data */
351
352 if (priv->iucv_state == IUCV_SEVERED)
353 return -EPIPE;
354
355 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
356 if (!len)
357 return 0;
358
359 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
360 priv->sndbuf_len += len;
361
362 if (priv->iucv_state == IUCV_CONNECTED)
363 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
364
365 return len;
366}
367
368/**
369 * hvc_iucv_send() - Send an IUCV message containing terminal data.
370 * @priv: Pointer to struct hvc_iucv_private instance.
371 *
372 * If an IUCV communication path has been established, the queued data
373 * for output are sent via an IUCV message.
374 *
375 * If there is no IUCV communication path established, the function returns 0.
376 * If an existing IUCV communicaton path has been severed, the function returns
377 * -EPIPE.
378 */
379static int hvc_iucv_send(struct hvc_iucv_private *priv)
380{
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100381 struct iucv_tty_buffer *sb;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100382 int rc, len;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100383
384 if (priv->iucv_state == IUCV_SEVERED)
385 return -EPIPE;
386
387 if (priv->iucv_state == IUCV_DISCONN)
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100388 return -EIO;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100389
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100390 if (!priv->sndbuf_len)
391 return 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100392
393 /* allocate internal buffer to store msg data and also compute total
394 * message length */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100395 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100396 if (!sb)
397 return -ENOMEM;
398
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100399 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
400 sb->mbuf->datalen = (u16) priv->sndbuf_len;
401 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100402
403 list_add_tail(&sb->list, &priv->tty_outqueue);
404
405 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
406 (void *) sb->mbuf, sb->msg.length);
407 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100408 /* drop the message here; however we might want to handle
409 * 0x03 (msg limit reached) by trying again... */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100410 list_del(&sb->list);
411 destroy_tty_buffer(sb);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100412 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100413 len = priv->sndbuf_len;
414 priv->sndbuf_len = 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100415
416 return len;
417}
418
419/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100420 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
421 * @work: Work structure.
422 *
423 * The function sends buffered output data over IUCV and, if necessary,
424 * reschedules itself if not all buffered data could be sent.
425 */
426static void hvc_iucv_sndbuf_work(struct work_struct *work)
427{
428 struct hvc_iucv_private *priv;
429
430 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
431
432 if (!priv)
433 return;
434
435 spin_lock_bh(&priv->lock);
436 hvc_iucv_send(priv);
437 spin_unlock_bh(&priv->lock);
438}
439
440/**
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100441 * hvc_iucv_put_chars() - HVC put_chars operation.
442 * @vtermno: HVC virtual terminal number.
443 * @buf: Pointer to an buffer to read data from
444 * @count: Size of buffer available for reading
445 *
446 * The hvc_console thread calls this method to write characters from
447 * to the terminal backend.
448 * The function calls hvc_iucv_send() under the lock of the
449 * struct hvc_iucv_private instance that corresponds to the tty @vtermno.
450 *
451 * Locking: The method gets called under an irqsave() spinlock; and
452 * locks struct hvc_iucv_private->lock.
453 */
454static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
455{
456 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100457 int queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100458
459 if (count <= 0)
460 return 0;
461
462 if (!priv)
463 return -ENODEV;
464
465 spin_lock(&priv->lock);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100466 queued = hvc_iucv_queue(priv, buf, count);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100467 spin_unlock(&priv->lock);
468
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100469 return queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100470}
471
472/**
473 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
474 * @hp: Pointer to the HVC device (struct hvc_struct)
475 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
476 * hvc_iucv_private instance.
477 *
478 * The function sets the tty state to TTY_OPEN for the struct hvc_iucv_private
479 * instance that is derived from @id. Always returns 0.
480 *
481 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
482 */
483static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
484{
485 struct hvc_iucv_private *priv;
486
487 priv = hvc_iucv_get_private(id);
488 if (!priv)
489 return 0;
490
491 spin_lock_bh(&priv->lock);
492 priv->tty_state = TTY_OPENED;
493 spin_unlock_bh(&priv->lock);
494
495 return 0;
496}
497
498/**
499 * hvc_iucv_cleanup() - Clean up function if the tty portion is finally closed.
500 * @priv: Pointer to the struct hvc_iucv_private instance.
501 *
502 * The functions severs the established IUCV communication path (if any), and
503 * destroy struct iucv_tty_buffer elements from the in- and outqueue. Finally,
504 * the functions resets the states to TTY_CLOSED and IUCV_DISCONN.
505 */
506static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
507{
508 destroy_tty_buffer_list(&priv->tty_outqueue);
509 destroy_tty_buffer_list(&priv->tty_inqueue);
510
511 priv->tty_state = TTY_CLOSED;
512 priv->iucv_state = IUCV_DISCONN;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100513
514 priv->sndbuf_len = 0;
515}
516
517/**
518 * tty_outqueue_empty() - Test if the tty outq is empty
519 * @priv: Pointer to struct hvc_iucv_private instance.
520 */
521static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
522{
523 int rc;
524
525 spin_lock_bh(&priv->lock);
526 rc = list_empty(&priv->tty_outqueue);
527 spin_unlock_bh(&priv->lock);
528
529 return rc;
530}
531
532/**
533 * flush_sndbuf_sync() - Flush send buffer and wait for completion
534 * @priv: Pointer to struct hvc_iucv_private instance.
535 *
536 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
537 * to flush any buffered terminal output data and waits for completion.
538 */
539static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
540{
541 int sync_wait;
542
543 cancel_delayed_work_sync(&priv->sndbuf_work);
544
545 spin_lock_bh(&priv->lock);
546 hvc_iucv_send(priv); /* force sending buffered data */
547 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
548 spin_unlock_bh(&priv->lock);
549
550 if (sync_wait)
551 wait_event_timeout(priv->sndbuf_waitq,
552 tty_outqueue_empty(priv), HZ);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100553}
554
555/**
556 * hvc_iucv_notifier_hangup() - HVC notifier for tty hangups.
557 * @hp: Pointer to the HVC device (struct hvc_struct)
558 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
559 * hvc_iucv_private instance.
560 *
561 * This routine notifies the HVC backend that a tty hangup (carrier loss,
562 * virtual or otherwise) has occured.
563 *
564 * The HVC backend for z/VM IUCV ignores virtual hangups (vhangup()), to keep
565 * an existing IUCV communication path established.
566 * (Background: vhangup() is called from user space (by getty or login) to
567 * disable writing to the tty by other applications).
568 *
569 * If the tty has been opened (e.g. getty) and an established IUCV path has been
570 * severed (we caused the tty hangup in that case), then the functions invokes
571 * hvc_iucv_cleanup() to clean up.
572 *
573 * Locking: struct hvc_iucv_private->lock
574 */
575static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
576{
577 struct hvc_iucv_private *priv;
578
579 priv = hvc_iucv_get_private(id);
580 if (!priv)
581 return;
582
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100583 flush_sndbuf_sync(priv);
584
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100585 spin_lock_bh(&priv->lock);
586 /* NOTE: If the hangup was scheduled by ourself (from the iucv
587 * path_servered callback [IUCV_SEVERED]), then we have to
588 * finally clean up the tty backend structure and set state to
589 * TTY_CLOSED.
590 *
591 * If the tty was hung up otherwise (e.g. vhangup()), then we
592 * ignore this hangup and keep an established IUCV path open...
593 * (...the reason is that we are not able to connect back to the
594 * client if we disconnect on hang up) */
595 priv->tty_state = TTY_CLOSED;
596
597 if (priv->iucv_state == IUCV_SEVERED)
598 hvc_iucv_cleanup(priv);
599 spin_unlock_bh(&priv->lock);
600}
601
602/**
603 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
604 * @hp: Pointer to the HVC device (struct hvc_struct)
605 * @id: Additional data (originally passed to hvc_alloc):
606 * the index of an struct hvc_iucv_private instance.
607 *
608 * This routine notifies the HVC backend that the last tty device file
609 * descriptor has been closed.
610 * The function calls hvc_iucv_cleanup() to clean up the struct hvc_iucv_private
611 * instance.
612 *
613 * Locking: struct hvc_iucv_private->lock
614 */
615static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
616{
617 struct hvc_iucv_private *priv;
618 struct iucv_path *path;
619
620 priv = hvc_iucv_get_private(id);
621 if (!priv)
622 return;
623
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100624 flush_sndbuf_sync(priv);
625
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100626 spin_lock_bh(&priv->lock);
627 path = priv->path; /* save reference to IUCV path */
628 priv->path = NULL;
629 hvc_iucv_cleanup(priv);
630 spin_unlock_bh(&priv->lock);
631
632 /* sever IUCV path outside of priv->lock due to lock ordering of:
633 * priv->lock <--> iucv_table_lock */
634 if (path) {
635 iucv_path_sever(path, NULL);
636 iucv_path_free(path);
637 }
638}
639
640/**
641 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
642 * @path: Pending path (struct iucv_path)
643 * @ipvmid: Originator z/VM system identifier
644 * @ipuser: User specified data for this path
645 * (AF_IUCV: port/service name and originator port)
646 *
647 * The function uses the @ipuser data to check to determine if the pending
648 * path belongs to a terminal managed by this HVC backend.
649 * If the check is successful, then an additional check is done to ensure
650 * that a terminal cannot be accessed multiple times (only one connection
651 * to a terminal is allowed). In that particular case, the pending path is
652 * severed. If it is the first connection, the pending path is accepted and
653 * associated to the struct hvc_iucv_private. The iucv state is updated to
654 * reflect that a communication path has been established.
655 *
656 * Returns 0 if the path belongs to a terminal managed by the this HVC backend;
657 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
658 *
659 * Locking: struct hvc_iucv_private->lock
660 */
661static int hvc_iucv_path_pending(struct iucv_path *path,
662 u8 ipvmid[8], u8 ipuser[16])
663{
664 struct hvc_iucv_private *priv;
665 u8 nuser_data[16];
666 int i, rc;
667
668 priv = NULL;
669 for (i = 0; i < hvc_iucv_devices; i++)
670 if (hvc_iucv_table[i] &&
671 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
672 priv = hvc_iucv_table[i];
673 break;
674 }
675
676 if (!priv)
677 return -ENODEV;
678
679 spin_lock(&priv->lock);
680
681 /* If the terminal is already connected or being severed, then sever
682 * this path to enforce that there is only ONE established communication
683 * path per terminal. */
684 if (priv->iucv_state != IUCV_DISCONN) {
685 iucv_path_sever(path, ipuser);
686 iucv_path_free(path);
687 goto out_path_handled;
688 }
689
690 /* accept path */
691 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
692 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
693 path->msglim = 0xffff; /* IUCV MSGLIMIT */
694 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
695 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
696 if (rc) {
697 iucv_path_sever(path, ipuser);
698 iucv_path_free(path);
699 goto out_path_handled;
700 }
701 priv->path = path;
702 priv->iucv_state = IUCV_CONNECTED;
703
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100704 /* flush buffered output data... */
705 schedule_delayed_work(&priv->sndbuf_work, 5);
706
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100707out_path_handled:
708 spin_unlock(&priv->lock);
709 return 0;
710}
711
712/**
713 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
714 * @path: Pending path (struct iucv_path)
715 * @ipuser: User specified data for this path
716 * (AF_IUCV: port/service name and originator port)
717 *
718 * The function also severs the path (as required by the IUCV protocol) and
719 * sets the iucv state to IUCV_SEVERED for the associated struct
720 * hvc_iucv_private instance. Later, the IUCV_SEVERED state triggers a tty
721 * hangup (hvc_iucv_get_chars() / hvc_iucv_write()).
722 *
723 * If tty portion of the HVC is closed then clean up the outqueue in addition.
724 *
725 * Locking: struct hvc_iucv_private->lock
726 */
727static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
728{
729 struct hvc_iucv_private *priv = path->private;
730
731 spin_lock(&priv->lock);
732 priv->iucv_state = IUCV_SEVERED;
733
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100734 /* If the tty has not yet been opened, clean up the hvc_iucv_private
735 * structure to allow re-connects.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100736 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100737 * If it has been opened, let get_chars() return -EPIPE to signal the
738 * HVC layer to hang up the tty.
739 * If so, we need to wake up the HVC thread to call get_chars()...
740 */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100741 priv->path = NULL;
742 if (priv->tty_state == TTY_CLOSED)
743 hvc_iucv_cleanup(priv);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100744 else
745 hvc_kick();
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100746 spin_unlock(&priv->lock);
747
748 /* finally sever path (outside of priv->lock due to lock ordering) */
749 iucv_path_sever(path, ipuser);
750 iucv_path_free(path);
751}
752
753/**
754 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
755 * @path: Pending path (struct iucv_path)
756 * @msg: Pointer to the IUCV message
757 *
758 * The function stores an incoming message on the input queue for later
759 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
760 * However, if the tty has not yet been opened, the message is rejected.
761 *
762 * Locking: struct hvc_iucv_private->lock
763 */
764static void hvc_iucv_msg_pending(struct iucv_path *path,
765 struct iucv_message *msg)
766{
767 struct hvc_iucv_private *priv = path->private;
768 struct iucv_tty_buffer *rb;
769
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100770 /* reject messages that exceed max size of iucv_tty_msg->datalen */
771 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
772 iucv_message_reject(path, msg);
773 return;
774 }
775
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100776 spin_lock(&priv->lock);
777
778 /* reject messages if tty has not yet been opened */
779 if (priv->tty_state == TTY_CLOSED) {
780 iucv_message_reject(path, msg);
781 goto unlock_return;
782 }
783
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100784 /* allocate tty buffer to save iucv msg only */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100785 rb = alloc_tty_buffer(0, GFP_ATOMIC);
786 if (!rb) {
787 iucv_message_reject(path, msg);
788 goto unlock_return; /* -ENOMEM */
789 }
790 rb->msg = *msg;
791
792 list_add_tail(&rb->list, &priv->tty_inqueue);
793
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100794 hvc_kick(); /* wake up hvc console thread */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100795
796unlock_return:
797 spin_unlock(&priv->lock);
798}
799
800/**
801 * hvc_iucv_msg_complete() - IUCV handler to process message completion
802 * @path: Pending path (struct iucv_path)
803 * @msg: Pointer to the IUCV message
804 *
805 * The function is called upon completion of message delivery and the
806 * message is removed from the outqueue. Additional delivery information
807 * can be found in msg->audit: rejected messages (0x040000 (IPADRJCT)) and
808 * purged messages (0x010000 (IPADPGNR)).
809 *
810 * Locking: struct hvc_iucv_private->lock
811 */
812static void hvc_iucv_msg_complete(struct iucv_path *path,
813 struct iucv_message *msg)
814{
815 struct hvc_iucv_private *priv = path->private;
816 struct iucv_tty_buffer *ent, *next;
817 LIST_HEAD(list_remove);
818
819 spin_lock(&priv->lock);
820 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
821 if (ent->msg.id == msg->id) {
822 list_move(&ent->list, &list_remove);
823 break;
824 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100825 wake_up(&priv->sndbuf_waitq);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100826 spin_unlock(&priv->lock);
827 destroy_tty_buffer_list(&list_remove);
828}
829
830
831/* HVC operations */
832static struct hv_ops hvc_iucv_ops = {
833 .get_chars = hvc_iucv_get_chars,
834 .put_chars = hvc_iucv_put_chars,
835 .notifier_add = hvc_iucv_notifier_add,
836 .notifier_del = hvc_iucv_notifier_del,
837 .notifier_hangup = hvc_iucv_notifier_hangup,
838};
839
840/**
841 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
842 * @id: hvc_iucv_table index
843 *
844 * This function allocates a new hvc_iucv_private struct and put the
845 * instance into hvc_iucv_table at index @id.
846 * Returns 0 on success; otherwise non-zero.
847 */
848static int __init hvc_iucv_alloc(int id)
849{
850 struct hvc_iucv_private *priv;
851 char name[9];
852 int rc;
853
854 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
855 if (!priv)
856 return -ENOMEM;
857
858 spin_lock_init(&priv->lock);
859 INIT_LIST_HEAD(&priv->tty_outqueue);
860 INIT_LIST_HEAD(&priv->tty_inqueue);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100861 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
862 init_waitqueue_head(&priv->sndbuf_waitq);
863
864 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
865 if (!priv->sndbuf) {
866 kfree(priv);
867 return -ENOMEM;
868 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100869
870 /* Finally allocate hvc */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100871 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
872 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100873 if (IS_ERR(priv->hvc)) {
874 rc = PTR_ERR(priv->hvc);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100875 free_page((unsigned long) priv->sndbuf);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100876 kfree(priv);
877 return rc;
878 }
879
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100880 /* kick khvcd thread; instead of using polling */
881 priv->hvc->irq_requested = 1;
882
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100883 /* setup iucv related information */
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +0100884 snprintf(name, 9, "lnxhvc%-2d", id);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100885 memcpy(priv->srv_name, name, 8);
886 ASCEBC(priv->srv_name, 8);
887
888 hvc_iucv_table[id] = priv;
889 return 0;
890}
891
892/**
893 * hvc_iucv_init() - Initialization of HVC backend for z/VM IUCV
894 */
895static int __init hvc_iucv_init(void)
896{
897 int rc, i;
898
899 if (!MACHINE_IS_VM) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100900 pr_info("The z/VM IUCV HVC device driver cannot "
901 "be used without z/VM\n");
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100902 return -ENODEV;
903 }
904
905 if (!hvc_iucv_devices)
906 return -ENODEV;
907
908 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES)
909 return -EINVAL;
910
911 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
912 sizeof(struct iucv_tty_buffer),
913 0, 0, NULL);
914 if (!hvc_iucv_buffer_cache) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100915 pr_err("Allocating memory failed with reason code=%d\n", 1);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100916 return -ENOMEM;
917 }
918
919 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
920 hvc_iucv_buffer_cache);
921 if (!hvc_iucv_mempool) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100922 pr_err("Allocating memory failed with reason code=%d\n", 2);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100923 kmem_cache_destroy(hvc_iucv_buffer_cache);
924 return -ENOMEM;
925 }
926
927 /* allocate hvc_iucv_private structs */
928 for (i = 0; i < hvc_iucv_devices; i++) {
929 rc = hvc_iucv_alloc(i);
930 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100931 pr_err("Creating a new HVC terminal device "
932 "failed with error code=%d\n", rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100933 goto out_error_hvc;
934 }
935 }
936
937 /* register IUCV callback handler */
938 rc = iucv_register(&hvc_iucv_handler, 0);
939 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100940 pr_err("Registering IUCV handlers failed with error code=%d\n",
941 rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100942 goto out_error_iucv;
943 }
944
945 return 0;
946
947out_error_iucv:
948 iucv_unregister(&hvc_iucv_handler, 0);
949out_error_hvc:
950 for (i = 0; i < hvc_iucv_devices; i++)
951 if (hvc_iucv_table[i]) {
952 if (hvc_iucv_table[i]->hvc)
953 hvc_remove(hvc_iucv_table[i]->hvc);
954 kfree(hvc_iucv_table[i]);
955 }
956 mempool_destroy(hvc_iucv_mempool);
957 kmem_cache_destroy(hvc_iucv_buffer_cache);
958 return rc;
959}
960
961/**
962 * hvc_iucv_console_init() - Early console initialization
963 */
964static int __init hvc_iucv_console_init(void)
965{
966 if (!MACHINE_IS_VM || !hvc_iucv_devices)
967 return -ENODEV;
968 return hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops);
969}
970
971/**
972 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
973 * @val: Parameter value (numeric)
974 */
975static int __init hvc_iucv_config(char *val)
976{
977 return strict_strtoul(val, 10, &hvc_iucv_devices);
978}
979
980
981module_init(hvc_iucv_init);
982console_initcall(hvc_iucv_console_init);
983__setup("hvc_iucv=", hvc_iucv_config);
984
985MODULE_LICENSE("GPL");
986MODULE_DESCRIPTION("HVC back-end for z/VM IUCV.");
987MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");