]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/hvc_iucv.c
ce1a25f779ce86271bb39280e95c487803295f62
[linux-2.6-omap-h63xx.git] / drivers / char / hvc_iucv.c
1 /*
2  * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
3  *
4  * This HVC device driver provides terminal access using
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 #define pr_fmt(fmt)             KMSG_COMPONENT ": " fmt
13
14 #include <linux/types.h>
15 #include <asm/ebcdic.h>
16 #include <linux/delay.h>
17 #include <linux/mempool.h>
18 #include <linux/module.h>
19 #include <linux/tty.h>
20 #include <linux/wait.h>
21 #include <net/iucv/iucv.h>
22
23 #include "hvc_console.h"
24
25
26 /* General device driver settings */
27 #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
39 struct iucv_tty_msg {
40         u8      version;                /* Message version */
41         u8      type;                   /* Message type */
42 #define MSG_MAX_DATALEN         ((u16)(~0))
43         u16     datalen;                /* Payload length */
44         u8      data[];                 /* Payload buffer */
45 } __attribute__((packed));
46 #define MSG_SIZE(s)             ((s) + offsetof(struct iucv_tty_msg, data))
47
48 enum iucv_state_t {
49         IUCV_DISCONN    = 0,
50         IUCV_CONNECTED  = 1,
51         IUCV_SEVERED    = 2,
52 };
53
54 enum tty_state_t {
55         TTY_CLOSED      = 0,
56         TTY_OPENED      = 1,
57 };
58
59 struct hvc_iucv_private {
60         struct hvc_struct       *hvc;           /* HVC struct reference */
61         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 */
66 #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 */
72         struct list_head        tty_outqueue;   /* outgoing IUCV messages */
73         struct list_head        tty_inqueue;    /* incoming IUCV messages */
74 };
75
76 struct iucv_tty_buffer {
77         struct list_head        list;   /* list pointer */
78         struct iucv_message     msg;    /* store an IUCV message */
79         size_t                  offset; /* data buffer offset */
80         struct iucv_tty_msg     *mbuf;  /* buffer to store input/output data */
81 };
82
83 /* IUCV callback handler */
84 static  int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
85 static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
86 static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
87 static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
88
89
90 /* Kernel module parameter: use one terminal device as default */
91 static unsigned long hvc_iucv_devices = 1;
92
93 /* Array of allocated hvc iucv tty lines... */
94 static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
95
96 /* Kmem cache and mempool for iucv_tty_buffer elements */
97 static struct kmem_cache *hvc_iucv_buffer_cache;
98 static mempool_t *hvc_iucv_mempool;
99
100 /* IUCV handler callback functions */
101 static 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  */
116 struct 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 /**
124  * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
125  * @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.
132  * The function returns NULL if memory allocation has failed.
133  */
134 static 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 static 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.
169  * @list:       List containing struct iucv_tty_buffer elements.
170  */
171 static 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 /**
182  * 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.
186  * @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.
190  * If the buffer size @count is less than the data message size, the
191  * message is kept on the input queue and @has_more_data is set to 1.
192  * If all message data has been written, the message is removed from
193  * 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
199  * hang up (that is issued by the HVC layer).
200  */
201 static 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
208         /* immediately return if there is no IUCV connection */
209         if (priv->iucv_state == IUCV_DISCONN)
210                 return 0;
211
212         /* if the IUCV path has been severed, return -EPIPE to inform the
213          * HVC layer to hang up the tty device. */
214         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
221         /* receive an iucv message and flip data to the tty (ldisc) */
222         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;
264                 hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
265                 break;
266
267         case MSG_TYPE_ERROR:    /* ignored ... */
268         case MSG_TYPE_TERMENV:  /* ignored ... */
269         case MSG_TYPE_TERMIOS:  /* ignored ... */
270                 break;
271         }
272
273 out_remove_buffer:
274         list_del(&rb->list);
275         destroy_tty_buffer(rb);
276         *has_more_data = !list_empty(&priv->tty_inqueue);
277
278 out_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  *
288  * 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.
291  *
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  */
296 static 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 /**
321  * hvc_iucv_queue() - Buffer terminal data for sending.
322  * @priv:       Pointer to struct hvc_iucv_private instance.
323  * @buf:        Buffer containing data to send.
324  * @count:      Size of buffer and amount of data to send.
325  *
326  * The function queues data for sending. To actually send the buffered data,
327  * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
328  * The function returns the number of data bytes that has been buffered.
329  *
330  * 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.
333  * 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).
335  */
336 static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
337                           int count)
338 {
339         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  *
364  * 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.
368  */
369 static int hvc_iucv_send(struct hvc_iucv_private *priv)
370 {
371         struct iucv_tty_buffer *sb;
372         int rc, len;
373
374         if (priv->iucv_state == IUCV_SEVERED)
375                 return -EPIPE;
376
377         if (priv->iucv_state == IUCV_DISCONN)
378                 return -EIO;
379
380         if (!priv->sndbuf_len)
381                 return 0;
382
383         /* allocate internal buffer to store msg data and also compute total
384          * message length */
385         sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
386         if (!sb)
387                 return -ENOMEM;
388
389         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);
392
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) {
398                 /* drop the message here; however we might want to handle
399                  * 0x03 (msg limit reached) by trying again... */
400                 list_del(&sb->list);
401                 destroy_tty_buffer(sb);
402         }
403         len = priv->sndbuf_len;
404         priv->sndbuf_len = 0;
405
406         return len;
407 }
408
409 /**
410  * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
411  * @work:       Work structure.
412  *
413  * This work queue function sends buffered output data over IUCV and,
414  * if not all buffered data could be sent, reschedules itself.
415  */
416 static 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);
421         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 /**
430  * 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  *
435  * 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.
437  *
438  * Locking:     The method gets called under an irqsave() spinlock; and
439  *              locks struct hvc_iucv_private->lock.
440  */
441 static 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);
444         int queued;
445
446         if (count <= 0)
447                 return 0;
448
449         if (!priv)
450                 return -ENODEV;
451
452         spin_lock(&priv->lock);
453         queued = hvc_iucv_queue(priv, buf, count);
454         spin_unlock(&priv->lock);
455
456         return queued;
457 }
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  */
470 static 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 /**
486  * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
487  * @priv:       Pointer to the struct hvc_iucv_private instance.
488  */
489 static 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;
496
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  */
504 static 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  */
522 static 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);
536 }
537
538 /**
539  * 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.
543  *
544  * This routine notifies the HVC back-end that a tty hangup (carrier loss,
545  * virtual or otherwise) has occured.
546  * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
547  * to keep an existing IUCV communication path established.
548  * (Background: vhangup() is called from user space (by getty or login) to
549  *              disable writing to the tty by other applications).
550  * 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().
552  *
553  * Locking:     struct hvc_iucv_private->lock
554  */
555 static 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
563         flush_sndbuf_sync(priv);
564
565         spin_lock_bh(&priv->lock);
566         /* NOTE: If the hangup was scheduled by ourself (from the iucv
567          *       path_servered callback [IUCV_SEVERED]), we have to clean up
568          *       our structure and to set state to TTY_CLOSED.
569          *       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  *
586  * 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.
589  *
590  * Locking:     struct hvc_iucv_private->lock
591  */
592 static 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
601         flush_sndbuf_sync(priv);
602
603         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)
620  * @ipvmid:     z/VM system identifier of originator
621  * @ipuser:     User specified data for this path
622  *              (AF_IUCV: port/service name and originator port)
623  *
624  * 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.
630  *
631  * Returns 0 if @path belongs to a terminal managed by the this device driver;
632  * otherwise returns -ENODEV in order to dispatch this path to other handlers.
633  *
634  * Locking:     struct hvc_iucv_private->lock
635  */
636 static  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                 }
650         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
678         /* flush buffered output data... */
679         schedule_delayed_work(&priv->sndbuf_work, 5);
680
681 out_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()).
696  * If tty portion of the HVC is closed, clean up the outqueue.
697  *
698  * Locking:     struct hvc_iucv_private->lock
699  */
700 static 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
707         /* If the tty has not yet been opened, clean up the hvc_iucv_private
708          * structure to allow re-connects.
709          *
710          * 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          */
714         priv->path = NULL;
715         if (priv->tty_state == TTY_CLOSED)
716                 hvc_iucv_cleanup(priv);
717         else
718                 hvc_kick();
719         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  *
731  * The function puts an incoming message on the input queue for later
732  * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
733  * If the tty has not yet been opened, the message is rejected.
734  *
735  * Locking:     struct hvc_iucv_private->lock
736  */
737 static 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
743         /* 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
749
750         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
758         /* allocate tty buffer to save iucv msg only */
759         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
768         hvc_kick();     /* wake up hvc thread */
769
770 unlock_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  *
779  * 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)).
783  *
784  * Locking:     struct hvc_iucv_private->lock
785  */
786 static 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                 }
799         wake_up(&priv->sndbuf_waitq);
800         spin_unlock(&priv->lock);
801         destroy_tty_buffer_list(&list_remove);
802 }
803
804
805 /* HVC operations */
806 static 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  *
818  * This function allocates a new hvc_iucv_private structure and stores
819  * the instance in hvc_iucv_table at index @id.
820  * Returns 0 on success; otherwise non-zero.
821  */
822 static 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);
835         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         }
843
844         /* finally allocate hvc */
845         priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /*             PAGE_SIZE */
846                               HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
847         if (IS_ERR(priv->hvc)) {
848                 rc = PTR_ERR(priv->hvc);
849                 free_page((unsigned long) priv->sndbuf);
850                 kfree(priv);
851                 return rc;
852         }
853
854         /* notify HVC thread instead of using polling */
855         priv->hvc->irq_requested = 1;
856
857         /* setup iucv related information */
858         snprintf(name, 9, "lnxhvc%-2d", id);
859         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 /**
867  * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
868  */
869 static int __init hvc_iucv_init(void)
870 {
871         int rc, i;
872
873         if (!MACHINE_IS_VM) {
874                 pr_info("The z/VM IUCV HVC device driver cannot "
875                            "be used without z/VM\n");
876                 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) {
889                 pr_err("Allocating memory failed with reason code=%d\n", 1);
890                 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) {
896                 pr_err("Allocating memory failed with reason code=%d\n", 2);
897                 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) {
905                         pr_err("Creating a new HVC terminal device "
906                                 "failed with error code=%d\n", rc);
907                         goto out_error_hvc;
908                 }
909         }
910
911         /* register IUCV callback handler */
912         rc = iucv_register(&hvc_iucv_handler, 0);
913         if (rc) {
914                 pr_err("Registering IUCV handlers failed with error code=%d\n",
915                         rc);
916                 goto out_error_iucv;
917         }
918
919         return 0;
920
921 out_error_iucv:
922         iucv_unregister(&hvc_iucv_handler, 0);
923 out_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  */
938 static  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  */
949 static  int __init hvc_iucv_config(char *val)
950 {
951          return strict_strtoul(val, 10, &hvc_iucv_devices);
952 }
953
954
955 module_init(hvc_iucv_init);
956 console_initcall(hvc_iucv_console_init);
957 __setup("hvc_iucv=", hvc_iucv_config);
958
959 MODULE_LICENSE("GPL");
960 MODULE_DESCRIPTION("HVC back-end for z/VM IUCV.");
961 MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");