2 * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
4 * This HVC device driver provides terminal access using
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"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/types.h>
15 #include <asm/ebcdic.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/mempool.h>
19 #include <linux/module.h>
20 #include <linux/tty.h>
21 #include <linux/wait.h>
22 #include <net/iucv/iucv.h>
24 #include "hvc_console.h"
27 /* General device driver settings */
28 #define HVC_IUCV_MAGIC 0xc9e4c3e5
29 #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
30 #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
32 /* IUCV TTY message */
33 #define MSG_VERSION 0x02 /* Message version */
34 #define MSG_TYPE_ERROR 0x01 /* Error message */
35 #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
36 #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
37 #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
38 #define MSG_TYPE_DATA 0x10 /* Terminal data */
41 u8 version; /* Message version */
42 u8 type; /* Message type */
43 #define MSG_MAX_DATALEN ((u16)(~0))
44 u16 datalen; /* Payload length */
45 u8 data[]; /* Payload buffer */
46 } __attribute__((packed));
47 #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
60 struct hvc_iucv_private {
61 struct hvc_struct *hvc; /* HVC struct reference */
62 u8 srv_name[8]; /* IUCV service name (ebcdic) */
63 enum iucv_state_t iucv_state; /* IUCV connection status */
64 enum tty_state_t tty_state; /* TTY status */
65 struct iucv_path *path; /* IUCV path pointer */
66 spinlock_t lock; /* hvc_iucv_private lock */
67 #define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
68 void *sndbuf; /* send buffer */
69 size_t sndbuf_len; /* length of send buffer */
70 #define QUEUE_SNDBUF_DELAY (HZ / 25)
71 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
72 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
73 struct list_head tty_outqueue; /* outgoing IUCV messages */
74 struct list_head tty_inqueue; /* incoming IUCV messages */
77 struct iucv_tty_buffer {
78 struct list_head list; /* list pointer */
79 struct iucv_message msg; /* store an IUCV message */
80 size_t offset; /* data buffer offset */
81 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
84 /* IUCV callback handler */
85 static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
86 static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
87 static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
88 static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
91 /* Kernel module parameter: use one terminal device as default */
92 static unsigned long hvc_iucv_devices = 1;
94 /* Array of allocated hvc iucv tty lines... */
95 static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
97 /* Kmem cache and mempool for iucv_tty_buffer elements */
98 static struct kmem_cache *hvc_iucv_buffer_cache;
99 static mempool_t *hvc_iucv_mempool;
101 /* IUCV handler callback functions */
102 static struct iucv_handler hvc_iucv_handler = {
103 .path_pending = hvc_iucv_path_pending,
104 .path_severed = hvc_iucv_path_severed,
105 .message_complete = hvc_iucv_msg_complete,
106 .message_pending = hvc_iucv_msg_pending,
111 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
112 * @num: The HVC virtual terminal number (vtermno)
114 * This function returns the struct hvc_iucv_private instance that corresponds
115 * to the HVC virtual terminal number specified as parameter @num.
117 struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
119 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
121 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
125 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
126 * @size: Size of the internal buffer used to store data.
127 * @flags: Memory allocation flags passed to mempool.
129 * This function allocates a new struct iucv_tty_buffer element and, optionally,
130 * allocates an internal data buffer with the specified size @size.
131 * Note: The total message size arises from the internal buffer size and the
132 * members of the iucv_tty_msg structure.
133 * The function returns NULL if memory allocation has failed.
135 static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
137 struct iucv_tty_buffer *bufp;
139 bufp = mempool_alloc(hvc_iucv_mempool, flags);
142 memset(bufp, 0, sizeof(struct iucv_tty_buffer));
145 bufp->msg.length = MSG_SIZE(size);
146 bufp->mbuf = kmalloc(bufp->msg.length, flags);
148 mempool_free(bufp, hvc_iucv_mempool);
151 bufp->mbuf->version = MSG_VERSION;
152 bufp->mbuf->type = MSG_TYPE_DATA;
153 bufp->mbuf->datalen = (u16) size;
159 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
160 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
162 static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
165 mempool_free(bufp, hvc_iucv_mempool);
169 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
170 * @list: List containing struct iucv_tty_buffer elements.
172 static void destroy_tty_buffer_list(struct list_head *list)
174 struct iucv_tty_buffer *ent, *next;
176 list_for_each_entry_safe(ent, next, list, list) {
177 list_del(&ent->list);
178 destroy_tty_buffer(ent);
183 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
184 * @priv: Pointer to struct hvc_iucv_private
185 * @buf: HVC buffer for writing received terminal data.
186 * @count: HVC buffer size.
187 * @has_more_data: Pointer to an int variable.
189 * The function picks up pending messages from the input queue and receives
190 * the message data that is then written to the specified buffer @buf.
191 * If the buffer size @count is less than the data message size, the
192 * message is kept on the input queue and @has_more_data is set to 1.
193 * If all message data has been written, the message is removed from
196 * The function returns the number of bytes written to the terminal, zero if
197 * there are no pending data messages available or if there is no established
199 * If the IUCV path has been severed, then -EPIPE is returned to cause a
200 * hang up (that is issued by the HVC layer).
202 static int hvc_iucv_write(struct hvc_iucv_private *priv,
203 char *buf, int count, int *has_more_data)
205 struct iucv_tty_buffer *rb;
209 /* immediately return if there is no IUCV connection */
210 if (priv->iucv_state == IUCV_DISCONN)
213 /* if the IUCV path has been severed, return -EPIPE to inform the
214 * HVC layer to hang up the tty device. */
215 if (priv->iucv_state == IUCV_SEVERED)
218 /* check if there are pending messages */
219 if (list_empty(&priv->tty_inqueue))
222 /* receive an iucv message and flip data to the tty (ldisc) */
223 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
226 if (!rb->mbuf) { /* message not yet received ... */
227 /* allocate mem to store msg data; if no memory is available
228 * then leave the buffer on the list and re-try later */
229 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC);
233 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
234 rb->mbuf, rb->msg.length, NULL);
236 case 0: /* Successful */
238 case 2: /* No message found */
239 case 9: /* Message purged */
244 /* remove buffer if an error has occured or received data
246 if (rc || (rb->mbuf->version != MSG_VERSION) ||
247 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
248 goto out_remove_buffer;
251 switch (rb->mbuf->type) {
253 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
254 memcpy(buf, rb->mbuf->data + rb->offset, written);
255 if (written < (rb->mbuf->datalen - rb->offset)) {
256 rb->offset += written;
262 case MSG_TYPE_WINSIZE:
263 if (rb->mbuf->datalen != sizeof(struct winsize))
265 hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
268 case MSG_TYPE_ERROR: /* ignored ... */
269 case MSG_TYPE_TERMENV: /* ignored ... */
270 case MSG_TYPE_TERMIOS: /* ignored ... */
276 destroy_tty_buffer(rb);
277 *has_more_data = !list_empty(&priv->tty_inqueue);
284 * hvc_iucv_get_chars() - HVC get_chars operation.
285 * @vtermno: HVC virtual terminal number.
286 * @buf: Pointer to a buffer to store data
287 * @count: Size of buffer available for writing
289 * The HVC thread calls this method to read characters from the back-end.
290 * If an IUCV communication path has been established, pending IUCV messages
291 * are received and data is copied into buffer @buf up to @count bytes.
293 * Locking: The routine gets called under an irqsave() spinlock; and
294 * the routine locks the struct hvc_iucv_private->lock to call
297 static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
299 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
309 spin_lock(&priv->lock);
311 written = hvc_iucv_write(priv, buf, count, &has_more_data);
312 spin_unlock(&priv->lock);
314 /* if there are still messages on the queue... schedule another run */
322 * hvc_iucv_queue() - Buffer terminal data for sending.
323 * @priv: Pointer to struct hvc_iucv_private instance.
324 * @buf: Buffer containing data to send.
325 * @count: Size of buffer and amount of data to send.
327 * The function queues data for sending. To actually send the buffered data,
328 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
329 * The function returns the number of data bytes that has been buffered.
331 * If the device is not connected, data is ignored and the function returns
333 * If the buffer is full, the function returns 0.
334 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
335 * (that can be passed to HVC layer to cause a tty hangup).
337 static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
342 if (priv->iucv_state == IUCV_DISCONN)
343 return count; /* ignore data */
345 if (priv->iucv_state == IUCV_SEVERED)
348 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
352 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
353 priv->sndbuf_len += len;
355 if (priv->iucv_state == IUCV_CONNECTED)
356 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
362 * hvc_iucv_send() - Send an IUCV message containing terminal data.
363 * @priv: Pointer to struct hvc_iucv_private instance.
365 * If an IUCV communication path has been established, the buffered output data
366 * is sent via an IUCV message and the number of bytes sent is returned.
367 * Returns 0 if there is no established IUCV communication path or
368 * -EPIPE if an existing IUCV communicaton path has been severed.
370 static int hvc_iucv_send(struct hvc_iucv_private *priv)
372 struct iucv_tty_buffer *sb;
375 if (priv->iucv_state == IUCV_SEVERED)
378 if (priv->iucv_state == IUCV_DISCONN)
381 if (!priv->sndbuf_len)
384 /* allocate internal buffer to store msg data and also compute total
386 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
390 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
391 sb->mbuf->datalen = (u16) priv->sndbuf_len;
392 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
394 list_add_tail(&sb->list, &priv->tty_outqueue);
396 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
397 (void *) sb->mbuf, sb->msg.length);
399 /* drop the message here; however we might want to handle
400 * 0x03 (msg limit reached) by trying again... */
402 destroy_tty_buffer(sb);
404 len = priv->sndbuf_len;
405 priv->sndbuf_len = 0;
411 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
412 * @work: Work structure.
414 * This work queue function sends buffered output data over IUCV and,
415 * if not all buffered data could be sent, reschedules itself.
417 static void hvc_iucv_sndbuf_work(struct work_struct *work)
419 struct hvc_iucv_private *priv;
421 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
425 spin_lock_bh(&priv->lock);
427 spin_unlock_bh(&priv->lock);
431 * hvc_iucv_put_chars() - HVC put_chars operation.
432 * @vtermno: HVC virtual terminal number.
433 * @buf: Pointer to an buffer to read data from
434 * @count: Size of buffer available for reading
436 * The HVC thread calls this method to write characters to the back-end.
437 * The function calls hvc_iucv_queue() to queue terminal data for sending.
439 * Locking: The method gets called under an irqsave() spinlock; and
440 * locks struct hvc_iucv_private->lock.
442 static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
444 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
453 spin_lock(&priv->lock);
454 queued = hvc_iucv_queue(priv, buf, count);
455 spin_unlock(&priv->lock);
461 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
462 * @hp: Pointer to the HVC device (struct hvc_struct)
463 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
464 * hvc_iucv_private instance.
466 * The function sets the tty state to TTY_OPEN for the struct hvc_iucv_private
467 * instance that is derived from @id. Always returns 0.
469 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
471 static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
473 struct hvc_iucv_private *priv;
475 priv = hvc_iucv_get_private(id);
479 spin_lock_bh(&priv->lock);
480 priv->tty_state = TTY_OPENED;
481 spin_unlock_bh(&priv->lock);
487 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
488 * @priv: Pointer to the struct hvc_iucv_private instance.
490 static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
492 destroy_tty_buffer_list(&priv->tty_outqueue);
493 destroy_tty_buffer_list(&priv->tty_inqueue);
495 priv->tty_state = TTY_CLOSED;
496 priv->iucv_state = IUCV_DISCONN;
498 priv->sndbuf_len = 0;
502 * tty_outqueue_empty() - Test if the tty outq is empty
503 * @priv: Pointer to struct hvc_iucv_private instance.
505 static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
509 spin_lock_bh(&priv->lock);
510 rc = list_empty(&priv->tty_outqueue);
511 spin_unlock_bh(&priv->lock);
517 * flush_sndbuf_sync() - Flush send buffer and wait for completion
518 * @priv: Pointer to struct hvc_iucv_private instance.
520 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
521 * to flush any buffered terminal output data and waits for completion.
523 static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
527 cancel_delayed_work_sync(&priv->sndbuf_work);
529 spin_lock_bh(&priv->lock);
530 hvc_iucv_send(priv); /* force sending buffered data */
531 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
532 spin_unlock_bh(&priv->lock);
535 wait_event_timeout(priv->sndbuf_waitq,
536 tty_outqueue_empty(priv), HZ);
540 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
541 * @hp: Pointer to the HVC device (struct hvc_struct)
542 * @id: Additional data (originally passed to hvc_alloc):
543 * the index of an struct hvc_iucv_private instance.
545 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
546 * virtual or otherwise) has occured.
547 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
548 * to keep an existing IUCV communication path established.
549 * (Background: vhangup() is called from user space (by getty or login) to
550 * disable writing to the tty by other applications).
551 * If the tty has been opened and an established IUCV path has been severed
552 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
554 * Locking: struct hvc_iucv_private->lock
556 static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
558 struct hvc_iucv_private *priv;
560 priv = hvc_iucv_get_private(id);
564 flush_sndbuf_sync(priv);
566 spin_lock_bh(&priv->lock);
567 /* NOTE: If the hangup was scheduled by ourself (from the iucv
568 * path_servered callback [IUCV_SEVERED]), we have to clean up
569 * our structure and to set state to TTY_CLOSED.
570 * If the tty was hung up otherwise (e.g. vhangup()), then we
571 * ignore this hangup and keep an established IUCV path open...
572 * (...the reason is that we are not able to connect back to the
573 * client if we disconnect on hang up) */
574 priv->tty_state = TTY_CLOSED;
576 if (priv->iucv_state == IUCV_SEVERED)
577 hvc_iucv_cleanup(priv);
578 spin_unlock_bh(&priv->lock);
582 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
583 * @hp: Pointer to the HVC device (struct hvc_struct)
584 * @id: Additional data (originally passed to hvc_alloc):
585 * the index of an struct hvc_iucv_private instance.
587 * This routine notifies the HVC back-end that the last tty device fd has been
588 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
589 * hvc_iucv_private instance.
591 * Locking: struct hvc_iucv_private->lock
593 static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
595 struct hvc_iucv_private *priv;
596 struct iucv_path *path;
598 priv = hvc_iucv_get_private(id);
602 flush_sndbuf_sync(priv);
604 spin_lock_bh(&priv->lock);
605 path = priv->path; /* save reference to IUCV path */
607 hvc_iucv_cleanup(priv);
608 spin_unlock_bh(&priv->lock);
610 /* sever IUCV path outside of priv->lock due to lock ordering of:
611 * priv->lock <--> iucv_table_lock */
613 iucv_path_sever(path, NULL);
614 iucv_path_free(path);
619 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
620 * @path: Pending path (struct iucv_path)
621 * @ipvmid: z/VM system identifier of originator
622 * @ipuser: User specified data for this path
623 * (AF_IUCV: port/service name and originator port)
625 * The function uses the @ipuser data to determine if the pending path belongs
626 * to a terminal managed by this device driver.
627 * If the path belongs to this driver, ensure that the terminal is not accessed
628 * multiple times (only one connection to a terminal is allowed).
629 * If the terminal is not yet connected, the pending path is accepted and is
630 * associated to the appropriate struct hvc_iucv_private instance.
632 * Returns 0 if @path belongs to a terminal managed by the this device driver;
633 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
635 * Locking: struct hvc_iucv_private->lock
637 static int hvc_iucv_path_pending(struct iucv_path *path,
638 u8 ipvmid[8], u8 ipuser[16])
640 struct hvc_iucv_private *priv;
645 for (i = 0; i < hvc_iucv_devices; i++)
646 if (hvc_iucv_table[i] &&
647 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
648 priv = hvc_iucv_table[i];
654 spin_lock(&priv->lock);
656 /* If the terminal is already connected or being severed, then sever
657 * this path to enforce that there is only ONE established communication
658 * path per terminal. */
659 if (priv->iucv_state != IUCV_DISCONN) {
660 iucv_path_sever(path, ipuser);
661 iucv_path_free(path);
662 goto out_path_handled;
666 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
667 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
668 path->msglim = 0xffff; /* IUCV MSGLIMIT */
669 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
670 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
672 iucv_path_sever(path, ipuser);
673 iucv_path_free(path);
674 goto out_path_handled;
677 priv->iucv_state = IUCV_CONNECTED;
679 /* flush buffered output data... */
680 schedule_delayed_work(&priv->sndbuf_work, 5);
683 spin_unlock(&priv->lock);
688 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
689 * @path: Pending path (struct iucv_path)
690 * @ipuser: User specified data for this path
691 * (AF_IUCV: port/service name and originator port)
693 * The function also severs the path (as required by the IUCV protocol) and
694 * sets the iucv state to IUCV_SEVERED for the associated struct
695 * hvc_iucv_private instance. Later, the IUCV_SEVERED state triggers a tty
696 * hangup (hvc_iucv_get_chars() / hvc_iucv_write()).
697 * If tty portion of the HVC is closed, clean up the outqueue.
699 * Locking: struct hvc_iucv_private->lock
701 static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
703 struct hvc_iucv_private *priv = path->private;
705 spin_lock(&priv->lock);
706 priv->iucv_state = IUCV_SEVERED;
708 /* If the tty has not yet been opened, clean up the hvc_iucv_private
709 * structure to allow re-connects.
711 * If it has been opened, let get_chars() return -EPIPE to signal the
712 * HVC layer to hang up the tty.
713 * If so, we need to wake up the HVC thread to call get_chars()...
716 if (priv->tty_state == TTY_CLOSED)
717 hvc_iucv_cleanup(priv);
720 spin_unlock(&priv->lock);
722 /* finally sever path (outside of priv->lock due to lock ordering) */
723 iucv_path_sever(path, ipuser);
724 iucv_path_free(path);
728 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
729 * @path: Pending path (struct iucv_path)
730 * @msg: Pointer to the IUCV message
732 * The function puts an incoming message on the input queue for later
733 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
734 * If the tty has not yet been opened, the message is rejected.
736 * Locking: struct hvc_iucv_private->lock
738 static void hvc_iucv_msg_pending(struct iucv_path *path,
739 struct iucv_message *msg)
741 struct hvc_iucv_private *priv = path->private;
742 struct iucv_tty_buffer *rb;
744 /* reject messages that exceed max size of iucv_tty_msg->datalen */
745 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
746 iucv_message_reject(path, msg);
751 spin_lock(&priv->lock);
753 /* reject messages if tty has not yet been opened */
754 if (priv->tty_state == TTY_CLOSED) {
755 iucv_message_reject(path, msg);
759 /* allocate tty buffer to save iucv msg only */
760 rb = alloc_tty_buffer(0, GFP_ATOMIC);
762 iucv_message_reject(path, msg);
763 goto unlock_return; /* -ENOMEM */
767 list_add_tail(&rb->list, &priv->tty_inqueue);
769 hvc_kick(); /* wake up hvc thread */
772 spin_unlock(&priv->lock);
776 * hvc_iucv_msg_complete() - IUCV handler to process message completion
777 * @path: Pending path (struct iucv_path)
778 * @msg: Pointer to the IUCV message
780 * The function is called upon completion of message delivery to remove the
781 * message from the outqueue. Additional delivery information can be found
782 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
783 * purged messages (0x010000 (IPADPGNR)).
785 * Locking: struct hvc_iucv_private->lock
787 static void hvc_iucv_msg_complete(struct iucv_path *path,
788 struct iucv_message *msg)
790 struct hvc_iucv_private *priv = path->private;
791 struct iucv_tty_buffer *ent, *next;
792 LIST_HEAD(list_remove);
794 spin_lock(&priv->lock);
795 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
796 if (ent->msg.id == msg->id) {
797 list_move(&ent->list, &list_remove);
800 wake_up(&priv->sndbuf_waitq);
801 spin_unlock(&priv->lock);
802 destroy_tty_buffer_list(&list_remove);
807 static struct hv_ops hvc_iucv_ops = {
808 .get_chars = hvc_iucv_get_chars,
809 .put_chars = hvc_iucv_put_chars,
810 .notifier_add = hvc_iucv_notifier_add,
811 .notifier_del = hvc_iucv_notifier_del,
812 .notifier_hangup = hvc_iucv_notifier_hangup,
816 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
817 * @id: hvc_iucv_table index
819 * This function allocates a new hvc_iucv_private structure and stores
820 * the instance in hvc_iucv_table at index @id.
821 * Returns 0 on success; otherwise non-zero.
823 static int __init hvc_iucv_alloc(int id)
825 struct hvc_iucv_private *priv;
829 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
833 spin_lock_init(&priv->lock);
834 INIT_LIST_HEAD(&priv->tty_outqueue);
835 INIT_LIST_HEAD(&priv->tty_inqueue);
836 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
837 init_waitqueue_head(&priv->sndbuf_waitq);
839 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
845 /* finally allocate hvc */
846 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
847 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
848 if (IS_ERR(priv->hvc)) {
849 rc = PTR_ERR(priv->hvc);
850 free_page((unsigned long) priv->sndbuf);
855 /* notify HVC thread instead of using polling */
856 priv->hvc->irq_requested = 1;
858 /* setup iucv related information */
859 snprintf(name, 9, "lnxhvc%-2d", id);
860 memcpy(priv->srv_name, name, 8);
861 ASCEBC(priv->srv_name, 8);
863 hvc_iucv_table[id] = priv;
868 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
870 static int __init hvc_iucv_init(void)
874 if (!MACHINE_IS_VM) {
875 pr_info("The z/VM IUCV HVC device driver cannot "
876 "be used without z/VM\n");
880 if (!hvc_iucv_devices)
883 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES)
886 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
887 sizeof(struct iucv_tty_buffer),
889 if (!hvc_iucv_buffer_cache) {
890 pr_err("Allocating memory failed with reason code=%d\n", 1);
894 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
895 hvc_iucv_buffer_cache);
896 if (!hvc_iucv_mempool) {
897 pr_err("Allocating memory failed with reason code=%d\n", 2);
898 kmem_cache_destroy(hvc_iucv_buffer_cache);
902 /* register the first terminal device as console
903 * (must be done before allocating hvc terminal devices) */
904 rc = hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops);
906 pr_warning("Registering HVC terminal device as "
907 "Linux console failed\n");
909 /* allocate hvc_iucv_private structs */
910 for (i = 0; i < hvc_iucv_devices; i++) {
911 rc = hvc_iucv_alloc(i);
913 pr_err("Creating a new HVC terminal device "
914 "failed with error code=%d\n", rc);
919 /* register IUCV callback handler */
920 rc = iucv_register(&hvc_iucv_handler, 0);
922 pr_err("Registering IUCV handlers failed with error code=%d\n",
930 iucv_unregister(&hvc_iucv_handler, 0);
932 for (i = 0; i < hvc_iucv_devices; i++)
933 if (hvc_iucv_table[i]) {
934 if (hvc_iucv_table[i]->hvc)
935 hvc_remove(hvc_iucv_table[i]->hvc);
936 kfree(hvc_iucv_table[i]);
938 mempool_destroy(hvc_iucv_mempool);
939 kmem_cache_destroy(hvc_iucv_buffer_cache);
944 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
945 * @val: Parameter value (numeric)
947 static int __init hvc_iucv_config(char *val)
949 return strict_strtoul(val, 10, &hvc_iucv_devices);
953 device_initcall(hvc_iucv_init);
954 __setup("hvc_iucv=", hvc_iucv_config);