2 * hvc_iucv.c - z/VM IUCV back-end for the Hypervisor Console (HVC)
4 * This back-end for HVC provides terminal access via
5 * z/VM IUCV communication paths.
7 * Copyright IBM Corp. 2008.
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
11 #define KMSG_COMPONENT "hvc_iucv"
13 #include <linux/types.h>
14 #include <asm/ebcdic.h>
15 #include <linux/delay.h>
16 #include <linux/mempool.h>
17 #include <linux/module.h>
18 #include <linux/tty.h>
19 #include <linux/wait.h>
20 #include <net/iucv/iucv.h>
22 #include "hvc_console.h"
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)
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 */
38 #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
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));
58 struct 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 */
65 #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 */
71 struct list_head tty_outqueue; /* outgoing IUCV messages */
72 struct list_head tty_inqueue; /* incoming IUCV messages */
75 struct 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 */
82 /* IUCV callback handler */
83 static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
84 static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
85 static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
86 static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
89 /* Kernel module parameter: use one terminal device as default */
90 static unsigned long hvc_iucv_devices = 1;
92 /* Array of allocated hvc iucv tty lines... */
93 static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
95 /* Kmem cache and mempool for iucv_tty_buffer elements */
96 static struct kmem_cache *hvc_iucv_buffer_cache;
97 static mempool_t *hvc_iucv_mempool;
99 /* IUCV handler callback functions */
100 static 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,
109 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
110 * @num: The HVC virtual terminal number (vtermno)
112 * This function returns the struct hvc_iucv_private instance that corresponds
113 * to the HVC virtual terminal number specified as parameter @num.
115 struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
117 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
119 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
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.
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.
132 * The function returns NULL if memory allocation has failed.
134 static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
136 struct iucv_tty_buffer *bufp;
138 bufp = mempool_alloc(hvc_iucv_mempool, flags);
141 memset(bufp, 0, sizeof(struct iucv_tty_buffer));
144 bufp->msg.length = MSG_SIZE(size);
145 bufp->mbuf = kmalloc(bufp->msg.length, flags);
147 mempool_free(bufp, hvc_iucv_mempool);
150 bufp->mbuf->version = MSG_VERSION;
151 bufp->mbuf->type = MSG_TYPE_DATA;
152 bufp->mbuf->datalen = (u16) size;
158 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
159 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
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.
164 static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
167 mempool_free(bufp, hvc_iucv_mempool);
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
175 * Calls destroy_tty_buffer() for each struct iucv_tty_buffer element in the
178 static void destroy_tty_buffer_list(struct list_head *list)
180 struct iucv_tty_buffer *ent, *next;
182 list_for_each_entry_safe(ent, next, list, list) {
183 list_del(&ent->list);
184 destroy_tty_buffer(ent);
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.
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
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
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).
208 static int hvc_iucv_write(struct hvc_iucv_private *priv,
209 char *buf, int count, int *has_more_data)
211 struct iucv_tty_buffer *rb;
215 /* Immediately return if there is no IUCV connection */
216 if (priv->iucv_state == IUCV_DISCONN)
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)
224 /* check if there are pending messages */
225 if (list_empty(&priv->tty_inqueue))
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);
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);
239 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
240 rb->mbuf, rb->msg.length, NULL);
242 case 0: /* Successful */
244 case 2: /* No message found */
245 case 9: /* Message purged */
250 /* remove buffer if an error has occured or received data
252 if (rc || (rb->mbuf->version != MSG_VERSION) ||
253 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
254 goto out_remove_buffer;
257 switch (rb->mbuf->type) {
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;
268 case MSG_TYPE_WINSIZE:
269 if (rb->mbuf->datalen != sizeof(struct winsize))
271 hvc_resize(priv->hvc, *((struct winsize *)rb->mbuf->data));
274 case MSG_TYPE_ERROR: /* ignored ... */
275 case MSG_TYPE_TERMENV: /* ignored ... */
276 case MSG_TYPE_TERMIOS: /* ignored ... */
282 destroy_tty_buffer(rb);
283 *has_more_data = !list_empty(&priv->tty_inqueue);
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
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.
300 * Locking: The routine gets called under an irqsave() spinlock; and
301 * the routine locks the struct hvc_iucv_private->lock to call
304 static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
306 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
316 spin_lock(&priv->lock);
318 written = hvc_iucv_write(priv, buf, count, &has_more_data);
319 spin_unlock(&priv->lock);
321 /* if there are still messages on the queue... schedule another run */
329 * hvc_iucv_queue() - Buffer terminal data for sending.
330 * @priv: Pointer to struct hvc_iucv_private instance.
331 * @buf: Buffer containing data to send.
332 * @count: Size of buffer and amount of data to send.
334 * 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.
338 * If the device is not connected, data is ignored and the function returns
340 * If the buffer is full, the function returns 0.
341 * 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).
344 static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
349 if (priv->iucv_state == IUCV_DISCONN)
350 return count; /* ignore data */
352 if (priv->iucv_state == IUCV_SEVERED)
355 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
359 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
360 priv->sndbuf_len += len;
362 if (priv->iucv_state == IUCV_CONNECTED)
363 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
369 * hvc_iucv_send() - Send an IUCV message containing terminal data.
370 * @priv: Pointer to struct hvc_iucv_private instance.
372 * If an IUCV communication path has been established, the queued data
373 * for output are sent via an IUCV message.
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
379 static int hvc_iucv_send(struct hvc_iucv_private *priv)
381 struct iucv_tty_buffer *sb;
384 if (priv->iucv_state == IUCV_SEVERED)
387 if (priv->iucv_state == IUCV_DISCONN)
390 if (!priv->sndbuf_len)
393 /* allocate internal buffer to store msg data and also compute total
395 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
399 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);
403 list_add_tail(&sb->list, &priv->tty_outqueue);
405 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
406 (void *) sb->mbuf, sb->msg.length);
408 /* drop the message here; however we might want to handle
409 * 0x03 (msg limit reached) by trying again... */
411 destroy_tty_buffer(sb);
413 len = priv->sndbuf_len;
414 priv->sndbuf_len = 0;
420 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
421 * @work: Work structure.
423 * The function sends buffered output data over IUCV and, if necessary,
424 * reschedules itself if not all buffered data could be sent.
426 static void hvc_iucv_sndbuf_work(struct work_struct *work)
428 struct hvc_iucv_private *priv;
430 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
435 spin_lock_bh(&priv->lock);
437 spin_unlock_bh(&priv->lock);
441 * 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
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.
451 * Locking: The method gets called under an irqsave() spinlock; and
452 * locks struct hvc_iucv_private->lock.
454 static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
456 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
465 spin_lock(&priv->lock);
466 queued = hvc_iucv_queue(priv, buf, count);
467 spin_unlock(&priv->lock);
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.
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.
481 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
483 static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
485 struct hvc_iucv_private *priv;
487 priv = hvc_iucv_get_private(id);
491 spin_lock_bh(&priv->lock);
492 priv->tty_state = TTY_OPENED;
493 spin_unlock_bh(&priv->lock);
499 * hvc_iucv_cleanup() - Clean up function if the tty portion is finally closed.
500 * @priv: Pointer to the struct hvc_iucv_private instance.
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.
506 static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
508 destroy_tty_buffer_list(&priv->tty_outqueue);
509 destroy_tty_buffer_list(&priv->tty_inqueue);
511 priv->tty_state = TTY_CLOSED;
512 priv->iucv_state = IUCV_DISCONN;
514 priv->sndbuf_len = 0;
518 * tty_outqueue_empty() - Test if the tty outq is empty
519 * @priv: Pointer to struct hvc_iucv_private instance.
521 static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
525 spin_lock_bh(&priv->lock);
526 rc = list_empty(&priv->tty_outqueue);
527 spin_unlock_bh(&priv->lock);
533 * flush_sndbuf_sync() - Flush send buffer and wait for completion
534 * @priv: Pointer to struct hvc_iucv_private instance.
536 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
537 * to flush any buffered terminal output data and waits for completion.
539 static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
543 cancel_delayed_work_sync(&priv->sndbuf_work);
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);
551 wait_event_timeout(priv->sndbuf_waitq,
552 tty_outqueue_empty(priv), HZ);
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.
561 * This routine notifies the HVC backend that a tty hangup (carrier loss,
562 * virtual or otherwise) has occured.
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).
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.
573 * Locking: struct hvc_iucv_private->lock
575 static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
577 struct hvc_iucv_private *priv;
579 priv = hvc_iucv_get_private(id);
583 flush_sndbuf_sync(priv);
585 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
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;
597 if (priv->iucv_state == IUCV_SEVERED)
598 hvc_iucv_cleanup(priv);
599 spin_unlock_bh(&priv->lock);
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.
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
613 * Locking: struct hvc_iucv_private->lock
615 static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
617 struct hvc_iucv_private *priv;
618 struct iucv_path *path;
620 priv = hvc_iucv_get_private(id);
624 flush_sndbuf_sync(priv);
626 spin_lock_bh(&priv->lock);
627 path = priv->path; /* save reference to IUCV path */
629 hvc_iucv_cleanup(priv);
630 spin_unlock_bh(&priv->lock);
632 /* sever IUCV path outside of priv->lock due to lock ordering of:
633 * priv->lock <--> iucv_table_lock */
635 iucv_path_sever(path, NULL);
636 iucv_path_free(path);
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)
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.
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.
659 * Locking: struct hvc_iucv_private->lock
661 static int hvc_iucv_path_pending(struct iucv_path *path,
662 u8 ipvmid[8], u8 ipuser[16])
664 struct hvc_iucv_private *priv;
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];
679 spin_lock(&priv->lock);
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;
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);
697 iucv_path_sever(path, ipuser);
698 iucv_path_free(path);
699 goto out_path_handled;
702 priv->iucv_state = IUCV_CONNECTED;
704 /* flush buffered output data... */
705 schedule_delayed_work(&priv->sndbuf_work, 5);
708 spin_unlock(&priv->lock);
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)
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()).
723 * If tty portion of the HVC is closed then clean up the outqueue in addition.
725 * Locking: struct hvc_iucv_private->lock
727 static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
729 struct hvc_iucv_private *priv = path->private;
731 spin_lock(&priv->lock);
732 priv->iucv_state = IUCV_SEVERED;
734 /* If the tty has not yet been opened, clean up the hvc_iucv_private
735 * structure to allow re-connects.
737 * 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()...
742 if (priv->tty_state == TTY_CLOSED)
743 hvc_iucv_cleanup(priv);
746 spin_unlock(&priv->lock);
748 /* finally sever path (outside of priv->lock due to lock ordering) */
749 iucv_path_sever(path, ipuser);
750 iucv_path_free(path);
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
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.
762 * Locking: struct hvc_iucv_private->lock
764 static void hvc_iucv_msg_pending(struct iucv_path *path,
765 struct iucv_message *msg)
767 struct hvc_iucv_private *priv = path->private;
768 struct iucv_tty_buffer *rb;
770 /* 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);
776 spin_lock(&priv->lock);
778 /* reject messages if tty has not yet been opened */
779 if (priv->tty_state == TTY_CLOSED) {
780 iucv_message_reject(path, msg);
784 /* allocate tty buffer to save iucv msg only */
785 rb = alloc_tty_buffer(0, GFP_ATOMIC);
787 iucv_message_reject(path, msg);
788 goto unlock_return; /* -ENOMEM */
792 list_add_tail(&rb->list, &priv->tty_inqueue);
794 hvc_kick(); /* wake up hvc console thread */
797 spin_unlock(&priv->lock);
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
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)).
810 * Locking: struct hvc_iucv_private->lock
812 static void hvc_iucv_msg_complete(struct iucv_path *path,
813 struct iucv_message *msg)
815 struct hvc_iucv_private *priv = path->private;
816 struct iucv_tty_buffer *ent, *next;
817 LIST_HEAD(list_remove);
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);
825 wake_up(&priv->sndbuf_waitq);
826 spin_unlock(&priv->lock);
827 destroy_tty_buffer_list(&list_remove);
832 static 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,
841 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
842 * @id: hvc_iucv_table index
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.
848 static int __init hvc_iucv_alloc(int id)
850 struct hvc_iucv_private *priv;
854 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
858 spin_lock_init(&priv->lock);
859 INIT_LIST_HEAD(&priv->tty_outqueue);
860 INIT_LIST_HEAD(&priv->tty_inqueue);
861 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
862 init_waitqueue_head(&priv->sndbuf_waitq);
864 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
870 /* Finally allocate hvc */
871 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
872 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
873 if (IS_ERR(priv->hvc)) {
874 rc = PTR_ERR(priv->hvc);
875 free_page((unsigned long) priv->sndbuf);
880 /* kick khvcd thread; instead of using polling */
881 priv->hvc->irq_requested = 1;
883 /* setup iucv related information */
884 snprintf(name, 9, "lnxhvc%-2d", id);
885 memcpy(priv->srv_name, name, 8);
886 ASCEBC(priv->srv_name, 8);
888 hvc_iucv_table[id] = priv;
893 * hvc_iucv_init() - Initialization of HVC backend for z/VM IUCV
895 static int __init hvc_iucv_init(void)
899 if (!MACHINE_IS_VM) {
900 pr_info("The z/VM IUCV HVC device driver cannot "
901 "be used without z/VM\n");
905 if (!hvc_iucv_devices)
908 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES)
911 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
912 sizeof(struct iucv_tty_buffer),
914 if (!hvc_iucv_buffer_cache) {
915 pr_err("Allocating memory failed with reason code=%d\n", 1);
919 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
920 hvc_iucv_buffer_cache);
921 if (!hvc_iucv_mempool) {
922 pr_err("Allocating memory failed with reason code=%d\n", 2);
923 kmem_cache_destroy(hvc_iucv_buffer_cache);
927 /* allocate hvc_iucv_private structs */
928 for (i = 0; i < hvc_iucv_devices; i++) {
929 rc = hvc_iucv_alloc(i);
931 pr_err("Creating a new HVC terminal device "
932 "failed with error code=%d\n", rc);
937 /* register IUCV callback handler */
938 rc = iucv_register(&hvc_iucv_handler, 0);
940 pr_err("Registering IUCV handlers failed with error code=%d\n",
948 iucv_unregister(&hvc_iucv_handler, 0);
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]);
956 mempool_destroy(hvc_iucv_mempool);
957 kmem_cache_destroy(hvc_iucv_buffer_cache);
962 * hvc_iucv_console_init() - Early console initialization
964 static int __init hvc_iucv_console_init(void)
966 if (!MACHINE_IS_VM || !hvc_iucv_devices)
968 return hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops);
972 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
973 * @val: Parameter value (numeric)
975 static int __init hvc_iucv_config(char *val)
977 return strict_strtoul(val, 10, &hvc_iucv_devices);
981 module_init(hvc_iucv_init);
982 console_initcall(hvc_iucv_console_init);
983 __setup("hvc_iucv=", hvc_iucv_config);
985 MODULE_LICENSE("GPL");
986 MODULE_DESCRIPTION("HVC back-end for z/VM IUCV.");
987 MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");