powerpc: use consistent types in mktree
[zen-stable.git] / drivers / char / hvc_iucv.c
blob86105efb4eb6cb948314a69b3b754ffdc290771a
1 /*
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, 2009
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/ctype.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/mempool.h>
21 #include <linux/moduleparam.h>
22 #include <linux/tty.h>
23 #include <linux/wait.h>
24 #include <net/iucv/iucv.h>
26 #include "hvc_console.h"
29 /* General device driver settings */
30 #define HVC_IUCV_MAGIC 0xc9e4c3e5
31 #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
32 #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
34 /* IUCV TTY message */
35 #define MSG_VERSION 0x02 /* Message version */
36 #define MSG_TYPE_ERROR 0x01 /* Error message */
37 #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
38 #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
39 #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
40 #define MSG_TYPE_DATA 0x10 /* Terminal data */
42 struct iucv_tty_msg {
43 u8 version; /* Message version */
44 u8 type; /* Message type */
45 #define MSG_MAX_DATALEN ((u16)(~0))
46 u16 datalen; /* Payload length */
47 u8 data[]; /* Payload buffer */
48 } __attribute__((packed));
49 #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
51 enum iucv_state_t {
52 IUCV_DISCONN = 0,
53 IUCV_CONNECTED = 1,
54 IUCV_SEVERED = 2,
57 enum tty_state_t {
58 TTY_CLOSED = 0,
59 TTY_OPENED = 1,
62 struct hvc_iucv_private {
63 struct hvc_struct *hvc; /* HVC struct reference */
64 u8 srv_name[8]; /* IUCV service name (ebcdic) */
65 unsigned char is_console; /* Linux console usage flag */
66 enum iucv_state_t iucv_state; /* IUCV connection status */
67 enum tty_state_t tty_state; /* TTY status */
68 struct iucv_path *path; /* IUCV path pointer */
69 spinlock_t lock; /* hvc_iucv_private lock */
70 #define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
71 void *sndbuf; /* send buffer */
72 size_t sndbuf_len; /* length of send buffer */
73 #define QUEUE_SNDBUF_DELAY (HZ / 25)
74 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
75 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
76 struct list_head tty_outqueue; /* outgoing IUCV messages */
77 struct list_head tty_inqueue; /* incoming IUCV messages */
78 struct device *dev; /* device structure */
81 struct iucv_tty_buffer {
82 struct list_head list; /* list pointer */
83 struct iucv_message msg; /* store an IUCV message */
84 size_t offset; /* data buffer offset */
85 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
88 /* IUCV callback handler */
89 static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
90 static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
91 static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
92 static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
95 /* Kernel module parameter: use one terminal device as default */
96 static unsigned long hvc_iucv_devices = 1;
98 /* Array of allocated hvc iucv tty lines... */
99 static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
100 #define IUCV_HVC_CON_IDX (0)
101 /* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
102 #define MAX_VMID_FILTER (500)
103 static size_t hvc_iucv_filter_size;
104 static void *hvc_iucv_filter;
105 static const char *hvc_iucv_filter_string;
106 static DEFINE_RWLOCK(hvc_iucv_filter_lock);
108 /* Kmem cache and mempool for iucv_tty_buffer elements */
109 static struct kmem_cache *hvc_iucv_buffer_cache;
110 static mempool_t *hvc_iucv_mempool;
112 /* IUCV handler callback functions */
113 static struct iucv_handler hvc_iucv_handler = {
114 .path_pending = hvc_iucv_path_pending,
115 .path_severed = hvc_iucv_path_severed,
116 .message_complete = hvc_iucv_msg_complete,
117 .message_pending = hvc_iucv_msg_pending,
122 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
123 * @num: The HVC virtual terminal number (vtermno)
125 * This function returns the struct hvc_iucv_private instance that corresponds
126 * to the HVC virtual terminal number specified as parameter @num.
128 struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
130 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
131 return NULL;
132 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
136 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
137 * @size: Size of the internal buffer used to store data.
138 * @flags: Memory allocation flags passed to mempool.
140 * This function allocates a new struct iucv_tty_buffer element and, optionally,
141 * allocates an internal data buffer with the specified size @size.
142 * Note: The total message size arises from the internal buffer size and the
143 * members of the iucv_tty_msg structure.
144 * The function returns NULL if memory allocation has failed.
146 static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
148 struct iucv_tty_buffer *bufp;
150 bufp = mempool_alloc(hvc_iucv_mempool, flags);
151 if (!bufp)
152 return NULL;
153 memset(bufp, 0, sizeof(*bufp));
155 if (size > 0) {
156 bufp->msg.length = MSG_SIZE(size);
157 bufp->mbuf = kmalloc(bufp->msg.length, flags);
158 if (!bufp->mbuf) {
159 mempool_free(bufp, hvc_iucv_mempool);
160 return NULL;
162 bufp->mbuf->version = MSG_VERSION;
163 bufp->mbuf->type = MSG_TYPE_DATA;
164 bufp->mbuf->datalen = (u16) size;
166 return bufp;
170 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
171 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
173 static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
175 kfree(bufp->mbuf);
176 mempool_free(bufp, hvc_iucv_mempool);
180 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
181 * @list: List containing struct iucv_tty_buffer elements.
183 static void destroy_tty_buffer_list(struct list_head *list)
185 struct iucv_tty_buffer *ent, *next;
187 list_for_each_entry_safe(ent, next, list, list) {
188 list_del(&ent->list);
189 destroy_tty_buffer(ent);
194 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
195 * @priv: Pointer to struct hvc_iucv_private
196 * @buf: HVC buffer for writing received terminal data.
197 * @count: HVC buffer size.
198 * @has_more_data: Pointer to an int variable.
200 * The function picks up pending messages from the input queue and receives
201 * the message data that is then written to the specified buffer @buf.
202 * If the buffer size @count is less than the data message size, the
203 * message is kept on the input queue and @has_more_data is set to 1.
204 * If all message data has been written, the message is removed from
205 * the input queue.
207 * The function returns the number of bytes written to the terminal, zero if
208 * there are no pending data messages available or if there is no established
209 * IUCV path.
210 * If the IUCV path has been severed, then -EPIPE is returned to cause a
211 * hang up (that is issued by the HVC layer).
213 static int hvc_iucv_write(struct hvc_iucv_private *priv,
214 char *buf, int count, int *has_more_data)
216 struct iucv_tty_buffer *rb;
217 int written;
218 int rc;
220 /* immediately return if there is no IUCV connection */
221 if (priv->iucv_state == IUCV_DISCONN)
222 return 0;
224 /* if the IUCV path has been severed, return -EPIPE to inform the
225 * HVC layer to hang up the tty device. */
226 if (priv->iucv_state == IUCV_SEVERED)
227 return -EPIPE;
229 /* check if there are pending messages */
230 if (list_empty(&priv->tty_inqueue))
231 return 0;
233 /* receive an iucv message and flip data to the tty (ldisc) */
234 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
236 written = 0;
237 if (!rb->mbuf) { /* message not yet received ... */
238 /* allocate mem to store msg data; if no memory is available
239 * then leave the buffer on the list and re-try later */
240 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC);
241 if (!rb->mbuf)
242 return -ENOMEM;
244 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
245 rb->mbuf, rb->msg.length, NULL);
246 switch (rc) {
247 case 0: /* Successful */
248 break;
249 case 2: /* No message found */
250 case 9: /* Message purged */
251 break;
252 default:
253 written = -EIO;
255 /* remove buffer if an error has occured or received data
256 * is not correct */
257 if (rc || (rb->mbuf->version != MSG_VERSION) ||
258 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
259 goto out_remove_buffer;
262 switch (rb->mbuf->type) {
263 case MSG_TYPE_DATA:
264 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
265 memcpy(buf, rb->mbuf->data + rb->offset, written);
266 if (written < (rb->mbuf->datalen - rb->offset)) {
267 rb->offset += written;
268 *has_more_data = 1;
269 goto out_written;
271 break;
273 case MSG_TYPE_WINSIZE:
274 if (rb->mbuf->datalen != sizeof(struct winsize))
275 break;
276 hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
277 break;
279 case MSG_TYPE_ERROR: /* ignored ... */
280 case MSG_TYPE_TERMENV: /* ignored ... */
281 case MSG_TYPE_TERMIOS: /* ignored ... */
282 break;
285 out_remove_buffer:
286 list_del(&rb->list);
287 destroy_tty_buffer(rb);
288 *has_more_data = !list_empty(&priv->tty_inqueue);
290 out_written:
291 return written;
295 * hvc_iucv_get_chars() - HVC get_chars operation.
296 * @vtermno: HVC virtual terminal number.
297 * @buf: Pointer to a buffer to store data
298 * @count: Size of buffer available for writing
300 * The HVC thread calls this method to read characters from the back-end.
301 * If an IUCV communication path has been established, pending IUCV messages
302 * are received and data is copied into buffer @buf up to @count bytes.
304 * Locking: The routine gets called under an irqsave() spinlock; and
305 * the routine locks the struct hvc_iucv_private->lock to call
306 * helper functions.
308 static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
310 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
311 int written;
312 int has_more_data;
314 if (count <= 0)
315 return 0;
317 if (!priv)
318 return -ENODEV;
320 spin_lock(&priv->lock);
321 has_more_data = 0;
322 written = hvc_iucv_write(priv, buf, count, &has_more_data);
323 spin_unlock(&priv->lock);
325 /* if there are still messages on the queue... schedule another run */
326 if (has_more_data)
327 hvc_kick();
329 return written;
333 * hvc_iucv_queue() - Buffer terminal data for sending.
334 * @priv: Pointer to struct hvc_iucv_private instance.
335 * @buf: Buffer containing data to send.
336 * @count: Size of buffer and amount of data to send.
338 * The function queues data for sending. To actually send the buffered data,
339 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
340 * The function returns the number of data bytes that has been buffered.
342 * If the device is not connected, data is ignored and the function returns
343 * @count.
344 * If the buffer is full, the function returns 0.
345 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
346 * (that can be passed to HVC layer to cause a tty hangup).
348 static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
349 int count)
351 size_t len;
353 if (priv->iucv_state == IUCV_DISCONN)
354 return count; /* ignore data */
356 if (priv->iucv_state == IUCV_SEVERED)
357 return -EPIPE;
359 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
360 if (!len)
361 return 0;
363 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
364 priv->sndbuf_len += len;
366 if (priv->iucv_state == IUCV_CONNECTED)
367 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
369 return len;
373 * hvc_iucv_send() - Send an IUCV message containing terminal data.
374 * @priv: Pointer to struct hvc_iucv_private instance.
376 * If an IUCV communication path has been established, the buffered output data
377 * is sent via an IUCV message and the number of bytes sent is returned.
378 * Returns 0 if there is no established IUCV communication path or
379 * -EPIPE if an existing IUCV communicaton path has been severed.
381 static int hvc_iucv_send(struct hvc_iucv_private *priv)
383 struct iucv_tty_buffer *sb;
384 int rc, len;
386 if (priv->iucv_state == IUCV_SEVERED)
387 return -EPIPE;
389 if (priv->iucv_state == IUCV_DISCONN)
390 return -EIO;
392 if (!priv->sndbuf_len)
393 return 0;
395 /* allocate internal buffer to store msg data and also compute total
396 * message length */
397 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
398 if (!sb)
399 return -ENOMEM;
401 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
402 sb->mbuf->datalen = (u16) priv->sndbuf_len;
403 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
405 list_add_tail(&sb->list, &priv->tty_outqueue);
407 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
408 (void *) sb->mbuf, sb->msg.length);
409 if (rc) {
410 /* drop the message here; however we might want to handle
411 * 0x03 (msg limit reached) by trying again... */
412 list_del(&sb->list);
413 destroy_tty_buffer(sb);
415 len = priv->sndbuf_len;
416 priv->sndbuf_len = 0;
418 return len;
422 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
423 * @work: Work structure.
425 * This work queue function sends buffered output data over IUCV and,
426 * if not all buffered data could be sent, reschedules itself.
428 static void hvc_iucv_sndbuf_work(struct work_struct *work)
430 struct hvc_iucv_private *priv;
432 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
433 if (!priv)
434 return;
436 spin_lock_bh(&priv->lock);
437 hvc_iucv_send(priv);
438 spin_unlock_bh(&priv->lock);
442 * hvc_iucv_put_chars() - HVC put_chars operation.
443 * @vtermno: HVC virtual terminal number.
444 * @buf: Pointer to an buffer to read data from
445 * @count: Size of buffer available for reading
447 * The HVC thread calls this method to write characters to the back-end.
448 * The function calls hvc_iucv_queue() to queue terminal data for sending.
450 * Locking: The method gets called under an irqsave() spinlock; and
451 * locks struct hvc_iucv_private->lock.
453 static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
455 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
456 int queued;
458 if (count <= 0)
459 return 0;
461 if (!priv)
462 return -ENODEV;
464 spin_lock(&priv->lock);
465 queued = hvc_iucv_queue(priv, buf, count);
466 spin_unlock(&priv->lock);
468 return queued;
472 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
473 * @hp: Pointer to the HVC device (struct hvc_struct)
474 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
475 * hvc_iucv_private instance.
477 * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
478 * instance that is derived from @id. Always returns 0.
480 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
482 static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
484 struct hvc_iucv_private *priv;
486 priv = hvc_iucv_get_private(id);
487 if (!priv)
488 return 0;
490 spin_lock_bh(&priv->lock);
491 priv->tty_state = TTY_OPENED;
492 spin_unlock_bh(&priv->lock);
494 return 0;
498 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
499 * @priv: Pointer to the struct hvc_iucv_private instance.
501 static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
503 destroy_tty_buffer_list(&priv->tty_outqueue);
504 destroy_tty_buffer_list(&priv->tty_inqueue);
506 priv->tty_state = TTY_CLOSED;
507 priv->iucv_state = IUCV_DISCONN;
509 priv->sndbuf_len = 0;
513 * tty_outqueue_empty() - Test if the tty outq is empty
514 * @priv: Pointer to struct hvc_iucv_private instance.
516 static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
518 int rc;
520 spin_lock_bh(&priv->lock);
521 rc = list_empty(&priv->tty_outqueue);
522 spin_unlock_bh(&priv->lock);
524 return rc;
528 * flush_sndbuf_sync() - Flush send buffer and wait for completion
529 * @priv: Pointer to struct hvc_iucv_private instance.
531 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
532 * to flush any buffered terminal output data and waits for completion.
534 static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
536 int sync_wait;
538 cancel_delayed_work_sync(&priv->sndbuf_work);
540 spin_lock_bh(&priv->lock);
541 hvc_iucv_send(priv); /* force sending buffered data */
542 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
543 spin_unlock_bh(&priv->lock);
545 if (sync_wait)
546 wait_event_timeout(priv->sndbuf_waitq,
547 tty_outqueue_empty(priv), HZ/10);
551 * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
552 * @priv: Pointer to hvc_iucv_private structure
554 * This routine severs an existing IUCV communication path and hangs
555 * up the underlying HVC terminal device.
556 * The hang-up occurs only if an IUCV communication path is established;
557 * otherwise there is no need to hang up the terminal device.
559 * The IUCV HVC hang-up is separated into two steps:
560 * 1. After the IUCV path has been severed, the iucv_state is set to
561 * IUCV_SEVERED.
562 * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
563 * IUCV_SEVERED state causes the tty hang-up in the HVC layer.
565 * If the tty has not yet been opened, clean up the hvc_iucv_private
566 * structure to allow re-connects.
567 * If the tty has been opened, let get_chars() return -EPIPE to signal
568 * the HVC layer to hang up the tty and, if so, wake up the HVC thread
569 * to call get_chars()...
571 * Special notes on hanging up a HVC terminal instantiated as console:
572 * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
573 * 2. do_tty_hangup() calls tty->ops->close() for console_filp
574 * => no hangup notifier is called by HVC (default)
575 * 2. hvc_close() returns because of tty_hung_up_p(filp)
576 * => no delete notifier is called!
577 * Finally, the back-end is not being notified, thus, the tty session is
578 * kept active (TTY_OPEN) to be ready for re-connects.
580 * Locking: spin_lock(&priv->lock) w/o disabling bh
582 static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
584 struct iucv_path *path;
586 path = NULL;
587 spin_lock(&priv->lock);
588 if (priv->iucv_state == IUCV_CONNECTED) {
589 path = priv->path;
590 priv->path = NULL;
591 priv->iucv_state = IUCV_SEVERED;
592 if (priv->tty_state == TTY_CLOSED)
593 hvc_iucv_cleanup(priv);
594 else
595 /* console is special (see above) */
596 if (priv->is_console) {
597 hvc_iucv_cleanup(priv);
598 priv->tty_state = TTY_OPENED;
599 } else
600 hvc_kick();
602 spin_unlock(&priv->lock);
604 /* finally sever path (outside of priv->lock due to lock ordering) */
605 if (path) {
606 iucv_path_sever(path, NULL);
607 iucv_path_free(path);
612 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
613 * @hp: Pointer to the HVC device (struct hvc_struct)
614 * @id: Additional data (originally passed to hvc_alloc):
615 * the index of an struct hvc_iucv_private instance.
617 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
618 * virtual or otherwise) has occured.
619 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
620 * to keep an existing IUCV communication path established.
621 * (Background: vhangup() is called from user space (by getty or login) to
622 * disable writing to the tty by other applications).
623 * If the tty has been opened and an established IUCV path has been severed
624 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
626 * Locking: struct hvc_iucv_private->lock
628 static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
630 struct hvc_iucv_private *priv;
632 priv = hvc_iucv_get_private(id);
633 if (!priv)
634 return;
636 flush_sndbuf_sync(priv);
638 spin_lock_bh(&priv->lock);
639 /* NOTE: If the hangup was scheduled by ourself (from the iucv
640 * path_servered callback [IUCV_SEVERED]), we have to clean up
641 * our structure and to set state to TTY_CLOSED.
642 * If the tty was hung up otherwise (e.g. vhangup()), then we
643 * ignore this hangup and keep an established IUCV path open...
644 * (...the reason is that we are not able to connect back to the
645 * client if we disconnect on hang up) */
646 priv->tty_state = TTY_CLOSED;
648 if (priv->iucv_state == IUCV_SEVERED)
649 hvc_iucv_cleanup(priv);
650 spin_unlock_bh(&priv->lock);
654 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
655 * @hp: Pointer to the HVC device (struct hvc_struct)
656 * @id: Additional data (originally passed to hvc_alloc):
657 * the index of an struct hvc_iucv_private instance.
659 * This routine notifies the HVC back-end that the last tty device fd has been
660 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
661 * hvc_iucv_private instance.
663 * Locking: struct hvc_iucv_private->lock
665 static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
667 struct hvc_iucv_private *priv;
668 struct iucv_path *path;
670 priv = hvc_iucv_get_private(id);
671 if (!priv)
672 return;
674 flush_sndbuf_sync(priv);
676 spin_lock_bh(&priv->lock);
677 path = priv->path; /* save reference to IUCV path */
678 priv->path = NULL;
679 hvc_iucv_cleanup(priv);
680 spin_unlock_bh(&priv->lock);
682 /* sever IUCV path outside of priv->lock due to lock ordering of:
683 * priv->lock <--> iucv_table_lock */
684 if (path) {
685 iucv_path_sever(path, NULL);
686 iucv_path_free(path);
691 * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
692 * @ipvmid: Originating z/VM user ID (right padded with blanks)
694 * Returns 0 if the z/VM user ID @ipvmid is allowed to connection, otherwise
695 * non-zero.
697 static int hvc_iucv_filter_connreq(u8 ipvmid[8])
699 size_t i;
701 /* Note: default policy is ACCEPT if no filter is set */
702 if (!hvc_iucv_filter_size)
703 return 0;
705 for (i = 0; i < hvc_iucv_filter_size; i++)
706 if (0 == memcmp(ipvmid, hvc_iucv_filter + (8 * i), 8))
707 return 0;
708 return 1;
712 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
713 * @path: Pending path (struct iucv_path)
714 * @ipvmid: z/VM system identifier of originator
715 * @ipuser: User specified data for this path
716 * (AF_IUCV: port/service name and originator port)
718 * The function uses the @ipuser data to determine if the pending path belongs
719 * to a terminal managed by this device driver.
720 * If the path belongs to this driver, ensure that the terminal is not accessed
721 * multiple times (only one connection to a terminal is allowed).
722 * If the terminal is not yet connected, the pending path is accepted and is
723 * associated to the appropriate struct hvc_iucv_private instance.
725 * Returns 0 if @path belongs to a terminal managed by the this device driver;
726 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
728 * Locking: struct hvc_iucv_private->lock
730 static int hvc_iucv_path_pending(struct iucv_path *path,
731 u8 ipvmid[8], u8 ipuser[16])
733 struct hvc_iucv_private *priv;
734 u8 nuser_data[16];
735 u8 vm_user_id[9];
736 int i, rc;
738 priv = NULL;
739 for (i = 0; i < hvc_iucv_devices; i++)
740 if (hvc_iucv_table[i] &&
741 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
742 priv = hvc_iucv_table[i];
743 break;
745 if (!priv)
746 return -ENODEV;
748 /* Enforce that ipvmid is allowed to connect to us */
749 read_lock(&hvc_iucv_filter_lock);
750 rc = hvc_iucv_filter_connreq(ipvmid);
751 read_unlock(&hvc_iucv_filter_lock);
752 if (rc) {
753 iucv_path_sever(path, ipuser);
754 iucv_path_free(path);
755 memcpy(vm_user_id, ipvmid, 8);
756 vm_user_id[8] = 0;
757 pr_info("A connection request from z/VM user ID %s "
758 "was refused\n", vm_user_id);
759 return 0;
762 spin_lock(&priv->lock);
764 /* If the terminal is already connected or being severed, then sever
765 * this path to enforce that there is only ONE established communication
766 * path per terminal. */
767 if (priv->iucv_state != IUCV_DISCONN) {
768 iucv_path_sever(path, ipuser);
769 iucv_path_free(path);
770 goto out_path_handled;
773 /* accept path */
774 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
775 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
776 path->msglim = 0xffff; /* IUCV MSGLIMIT */
777 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
778 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
779 if (rc) {
780 iucv_path_sever(path, ipuser);
781 iucv_path_free(path);
782 goto out_path_handled;
784 priv->path = path;
785 priv->iucv_state = IUCV_CONNECTED;
787 /* flush buffered output data... */
788 schedule_delayed_work(&priv->sndbuf_work, 5);
790 out_path_handled:
791 spin_unlock(&priv->lock);
792 return 0;
796 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
797 * @path: Pending path (struct iucv_path)
798 * @ipuser: User specified data for this path
799 * (AF_IUCV: port/service name and originator port)
801 * This function calls the hvc_iucv_hangup() function for the
802 * respective IUCV HVC terminal.
804 * Locking: struct hvc_iucv_private->lock
806 static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
808 struct hvc_iucv_private *priv = path->private;
810 hvc_iucv_hangup(priv);
814 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
815 * @path: Pending path (struct iucv_path)
816 * @msg: Pointer to the IUCV message
818 * The function puts an incoming message on the input queue for later
819 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
820 * If the tty has not yet been opened, the message is rejected.
822 * Locking: struct hvc_iucv_private->lock
824 static void hvc_iucv_msg_pending(struct iucv_path *path,
825 struct iucv_message *msg)
827 struct hvc_iucv_private *priv = path->private;
828 struct iucv_tty_buffer *rb;
830 /* reject messages that exceed max size of iucv_tty_msg->datalen */
831 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
832 iucv_message_reject(path, msg);
833 return;
836 spin_lock(&priv->lock);
838 /* reject messages if tty has not yet been opened */
839 if (priv->tty_state == TTY_CLOSED) {
840 iucv_message_reject(path, msg);
841 goto unlock_return;
844 /* allocate tty buffer to save iucv msg only */
845 rb = alloc_tty_buffer(0, GFP_ATOMIC);
846 if (!rb) {
847 iucv_message_reject(path, msg);
848 goto unlock_return; /* -ENOMEM */
850 rb->msg = *msg;
852 list_add_tail(&rb->list, &priv->tty_inqueue);
854 hvc_kick(); /* wake up hvc thread */
856 unlock_return:
857 spin_unlock(&priv->lock);
861 * hvc_iucv_msg_complete() - IUCV handler to process message completion
862 * @path: Pending path (struct iucv_path)
863 * @msg: Pointer to the IUCV message
865 * The function is called upon completion of message delivery to remove the
866 * message from the outqueue. Additional delivery information can be found
867 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
868 * purged messages (0x010000 (IPADPGNR)).
870 * Locking: struct hvc_iucv_private->lock
872 static void hvc_iucv_msg_complete(struct iucv_path *path,
873 struct iucv_message *msg)
875 struct hvc_iucv_private *priv = path->private;
876 struct iucv_tty_buffer *ent, *next;
877 LIST_HEAD(list_remove);
879 spin_lock(&priv->lock);
880 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
881 if (ent->msg.id == msg->id) {
882 list_move(&ent->list, &list_remove);
883 break;
885 wake_up(&priv->sndbuf_waitq);
886 spin_unlock(&priv->lock);
887 destroy_tty_buffer_list(&list_remove);
891 * hvc_iucv_pm_freeze() - Freeze PM callback
892 * @dev: IUVC HVC terminal device
894 * Sever an established IUCV communication path and
895 * trigger a hang-up of the underlying HVC terminal.
897 static int hvc_iucv_pm_freeze(struct device *dev)
899 struct hvc_iucv_private *priv = dev_get_drvdata(dev);
901 local_bh_disable();
902 hvc_iucv_hangup(priv);
903 local_bh_enable();
905 return 0;
909 * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
910 * @dev: IUVC HVC terminal device
912 * Wake up the HVC thread to trigger hang-up and respective
913 * HVC back-end notifier invocations.
915 static int hvc_iucv_pm_restore_thaw(struct device *dev)
917 hvc_kick();
918 return 0;
922 /* HVC operations */
923 static struct hv_ops hvc_iucv_ops = {
924 .get_chars = hvc_iucv_get_chars,
925 .put_chars = hvc_iucv_put_chars,
926 .notifier_add = hvc_iucv_notifier_add,
927 .notifier_del = hvc_iucv_notifier_del,
928 .notifier_hangup = hvc_iucv_notifier_hangup,
931 /* Suspend / resume device operations */
932 static struct dev_pm_ops hvc_iucv_pm_ops = {
933 .freeze = hvc_iucv_pm_freeze,
934 .thaw = hvc_iucv_pm_restore_thaw,
935 .restore = hvc_iucv_pm_restore_thaw,
938 /* IUCV HVC device driver */
939 static struct device_driver hvc_iucv_driver = {
940 .name = KMSG_COMPONENT,
941 .bus = &iucv_bus,
942 .pm = &hvc_iucv_pm_ops,
946 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
947 * @id: hvc_iucv_table index
948 * @is_console: Flag if the instance is used as Linux console
950 * This function allocates a new hvc_iucv_private structure and stores
951 * the instance in hvc_iucv_table at index @id.
952 * Returns 0 on success; otherwise non-zero.
954 static int __init hvc_iucv_alloc(int id, unsigned int is_console)
956 struct hvc_iucv_private *priv;
957 char name[9];
958 int rc;
960 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
961 if (!priv)
962 return -ENOMEM;
964 spin_lock_init(&priv->lock);
965 INIT_LIST_HEAD(&priv->tty_outqueue);
966 INIT_LIST_HEAD(&priv->tty_inqueue);
967 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
968 init_waitqueue_head(&priv->sndbuf_waitq);
970 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
971 if (!priv->sndbuf) {
972 kfree(priv);
973 return -ENOMEM;
976 /* set console flag */
977 priv->is_console = is_console;
979 /* allocate hvc device */
980 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
981 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
982 if (IS_ERR(priv->hvc)) {
983 rc = PTR_ERR(priv->hvc);
984 goto out_error_hvc;
987 /* notify HVC thread instead of using polling */
988 priv->hvc->irq_requested = 1;
990 /* setup iucv related information */
991 snprintf(name, 9, "lnxhvc%-2d", id);
992 memcpy(priv->srv_name, name, 8);
993 ASCEBC(priv->srv_name, 8);
995 /* create and setup device */
996 priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
997 if (!priv->dev) {
998 rc = -ENOMEM;
999 goto out_error_dev;
1001 dev_set_name(priv->dev, "hvc_iucv%d", id);
1002 dev_set_drvdata(priv->dev, priv);
1003 priv->dev->bus = &iucv_bus;
1004 priv->dev->parent = iucv_root;
1005 priv->dev->driver = &hvc_iucv_driver;
1006 priv->dev->release = (void (*)(struct device *)) kfree;
1007 rc = device_register(priv->dev);
1008 if (rc) {
1009 kfree(priv->dev);
1010 goto out_error_dev;
1013 hvc_iucv_table[id] = priv;
1014 return 0;
1016 out_error_dev:
1017 hvc_remove(priv->hvc);
1018 out_error_hvc:
1019 free_page((unsigned long) priv->sndbuf);
1020 kfree(priv);
1022 return rc;
1026 * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1028 static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1030 hvc_remove(priv->hvc);
1031 device_unregister(priv->dev);
1032 free_page((unsigned long) priv->sndbuf);
1033 kfree(priv);
1037 * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
1038 * @filter: String containing a comma-separated list of z/VM user IDs
1040 static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1042 const char *nextdelim, *residual;
1043 size_t len;
1045 nextdelim = strchr(filter, ',');
1046 if (nextdelim) {
1047 len = nextdelim - filter;
1048 residual = nextdelim + 1;
1049 } else {
1050 len = strlen(filter);
1051 residual = filter + len;
1054 if (len == 0)
1055 return ERR_PTR(-EINVAL);
1057 /* check for '\n' (if called from sysfs) */
1058 if (filter[len - 1] == '\n')
1059 len--;
1061 if (len > 8)
1062 return ERR_PTR(-EINVAL);
1064 /* pad with blanks and save upper case version of user ID */
1065 memset(dest, ' ', 8);
1066 while (len--)
1067 dest[len] = toupper(filter[len]);
1068 return residual;
1072 * hvc_iucv_setup_filter() - Set up z/VM user ID filter
1073 * @filter: String consisting of a comma-separated list of z/VM user IDs
1075 * The function parses the @filter string and creates an array containing
1076 * the list of z/VM user ID filter entries.
1077 * Return code 0 means success, -EINVAL if the filter is syntactically
1078 * incorrect, -ENOMEM if there was not enough memory to allocate the
1079 * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
1081 static int hvc_iucv_setup_filter(const char *val)
1083 const char *residual;
1084 int err;
1085 size_t size, count;
1086 void *array, *old_filter;
1088 count = strlen(val);
1089 if (count == 0 || (count == 1 && val[0] == '\n')) {
1090 size = 0;
1091 array = NULL;
1092 goto out_replace_filter; /* clear filter */
1095 /* count user IDs in order to allocate sufficient memory */
1096 size = 1;
1097 residual = val;
1098 while ((residual = strchr(residual, ',')) != NULL) {
1099 residual++;
1100 size++;
1103 /* check if the specified list exceeds the filter limit */
1104 if (size > MAX_VMID_FILTER)
1105 return -ENOSPC;
1107 array = kzalloc(size * 8, GFP_KERNEL);
1108 if (!array)
1109 return -ENOMEM;
1111 count = size;
1112 residual = val;
1113 while (*residual && count) {
1114 residual = hvc_iucv_parse_filter(residual,
1115 array + ((size - count) * 8));
1116 if (IS_ERR(residual)) {
1117 err = PTR_ERR(residual);
1118 kfree(array);
1119 goto out_err;
1121 count--;
1124 out_replace_filter:
1125 write_lock_bh(&hvc_iucv_filter_lock);
1126 old_filter = hvc_iucv_filter;
1127 hvc_iucv_filter_size = size;
1128 hvc_iucv_filter = array;
1129 write_unlock_bh(&hvc_iucv_filter_lock);
1130 kfree(old_filter);
1132 err = 0;
1133 out_err:
1134 return err;
1138 * param_set_vmidfilter() - Set z/VM user ID filter parameter
1139 * @val: String consisting of a comma-separated list of z/VM user IDs
1140 * @kp: Kernel parameter pointing to hvc_iucv_filter array
1142 * The function sets up the z/VM user ID filter specified as comma-separated
1143 * list of user IDs in @val.
1144 * Note: If it is called early in the boot process, @val is stored and
1145 * parsed later in hvc_iucv_init().
1147 static int param_set_vmidfilter(const char *val, struct kernel_param *kp)
1149 int rc;
1151 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1152 return -ENODEV;
1154 if (!val)
1155 return -EINVAL;
1157 rc = 0;
1158 if (slab_is_available())
1159 rc = hvc_iucv_setup_filter(val);
1160 else
1161 hvc_iucv_filter_string = val; /* defer... */
1162 return rc;
1166 * param_get_vmidfilter() - Get z/VM user ID filter
1167 * @buffer: Buffer to store z/VM user ID filter,
1168 * (buffer size assumption PAGE_SIZE)
1169 * @kp: Kernel parameter pointing to the hvc_iucv_filter array
1171 * The function stores the filter as a comma-separated list of z/VM user IDs
1172 * in @buffer. Typically, sysfs routines call this function for attr show.
1174 static int param_get_vmidfilter(char *buffer, struct kernel_param *kp)
1176 int rc;
1177 size_t index, len;
1178 void *start, *end;
1180 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1181 return -ENODEV;
1183 rc = 0;
1184 read_lock_bh(&hvc_iucv_filter_lock);
1185 for (index = 0; index < hvc_iucv_filter_size; index++) {
1186 start = hvc_iucv_filter + (8 * index);
1187 end = memchr(start, ' ', 8);
1188 len = (end) ? end - start : 8;
1189 memcpy(buffer + rc, start, len);
1190 rc += len;
1191 buffer[rc++] = ',';
1193 read_unlock_bh(&hvc_iucv_filter_lock);
1194 if (rc)
1195 buffer[--rc] = '\0'; /* replace last comma and update rc */
1196 return rc;
1199 #define param_check_vmidfilter(name, p) __param_check(name, p, void)
1202 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
1204 static int __init hvc_iucv_init(void)
1206 int rc;
1207 unsigned int i;
1209 if (!hvc_iucv_devices)
1210 return -ENODEV;
1212 if (!MACHINE_IS_VM) {
1213 pr_notice("The z/VM IUCV HVC device driver cannot "
1214 "be used without z/VM\n");
1215 rc = -ENODEV;
1216 goto out_error;
1219 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
1220 pr_err("%lu is not a valid value for the hvc_iucv= "
1221 "kernel parameter\n", hvc_iucv_devices);
1222 rc = -EINVAL;
1223 goto out_error;
1226 /* register IUCV HVC device driver */
1227 rc = driver_register(&hvc_iucv_driver);
1228 if (rc)
1229 goto out_error;
1231 /* parse hvc_iucv_allow string and create z/VM user ID filter list */
1232 if (hvc_iucv_filter_string) {
1233 rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
1234 switch (rc) {
1235 case 0:
1236 break;
1237 case -ENOMEM:
1238 pr_err("Allocating memory failed with "
1239 "reason code=%d\n", 3);
1240 goto out_error;
1241 case -EINVAL:
1242 pr_err("hvc_iucv_allow= does not specify a valid "
1243 "z/VM user ID list\n");
1244 goto out_error;
1245 case -ENOSPC:
1246 pr_err("hvc_iucv_allow= specifies too many "
1247 "z/VM user IDs\n");
1248 goto out_error;
1249 default:
1250 goto out_error;
1254 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
1255 sizeof(struct iucv_tty_buffer),
1256 0, 0, NULL);
1257 if (!hvc_iucv_buffer_cache) {
1258 pr_err("Allocating memory failed with reason code=%d\n", 1);
1259 rc = -ENOMEM;
1260 goto out_error;
1263 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1264 hvc_iucv_buffer_cache);
1265 if (!hvc_iucv_mempool) {
1266 pr_err("Allocating memory failed with reason code=%d\n", 2);
1267 kmem_cache_destroy(hvc_iucv_buffer_cache);
1268 rc = -ENOMEM;
1269 goto out_error;
1272 /* register the first terminal device as console
1273 * (must be done before allocating hvc terminal devices) */
1274 rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
1275 if (rc) {
1276 pr_err("Registering HVC terminal device as "
1277 "Linux console failed\n");
1278 goto out_error_memory;
1281 /* allocate hvc_iucv_private structs */
1282 for (i = 0; i < hvc_iucv_devices; i++) {
1283 rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
1284 if (rc) {
1285 pr_err("Creating a new HVC terminal device "
1286 "failed with error code=%d\n", rc);
1287 goto out_error_hvc;
1291 /* register IUCV callback handler */
1292 rc = iucv_register(&hvc_iucv_handler, 0);
1293 if (rc) {
1294 pr_err("Registering IUCV handlers failed with error code=%d\n",
1295 rc);
1296 goto out_error_iucv;
1299 return 0;
1301 out_error_iucv:
1302 iucv_unregister(&hvc_iucv_handler, 0);
1303 out_error_hvc:
1304 for (i = 0; i < hvc_iucv_devices; i++)
1305 if (hvc_iucv_table[i])
1306 hvc_iucv_destroy(hvc_iucv_table[i]);
1307 out_error_memory:
1308 mempool_destroy(hvc_iucv_mempool);
1309 kmem_cache_destroy(hvc_iucv_buffer_cache);
1310 out_error:
1311 if (hvc_iucv_filter)
1312 kfree(hvc_iucv_filter);
1313 hvc_iucv_devices = 0; /* ensure that we do not provide any device */
1314 return rc;
1318 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
1319 * @val: Parameter value (numeric)
1321 static int __init hvc_iucv_config(char *val)
1323 return strict_strtoul(val, 10, &hvc_iucv_devices);
1327 device_initcall(hvc_iucv_init);
1328 __setup("hvc_iucv=", hvc_iucv_config);
1329 core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);