platform: x86: asus_acpi: world-writable procfs files
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / hvc_iucv.c
blob170cc4bde498d47d95ce1a5b438178a9a62d943f
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 * The internal data buffer is always allocated with GFP_DMA which is
143 * required for receiving and sending data with IUCV.
144 * Note: The total message size arises from the internal buffer size and the
145 * members of the iucv_tty_msg structure.
146 * The function returns NULL if memory allocation has failed.
148 static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
150 struct iucv_tty_buffer *bufp;
152 bufp = mempool_alloc(hvc_iucv_mempool, flags);
153 if (!bufp)
154 return NULL;
155 memset(bufp, 0, sizeof(*bufp));
157 if (size > 0) {
158 bufp->msg.length = MSG_SIZE(size);
159 bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
160 if (!bufp->mbuf) {
161 mempool_free(bufp, hvc_iucv_mempool);
162 return NULL;
164 bufp->mbuf->version = MSG_VERSION;
165 bufp->mbuf->type = MSG_TYPE_DATA;
166 bufp->mbuf->datalen = (u16) size;
168 return bufp;
172 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
173 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
175 static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
177 kfree(bufp->mbuf);
178 mempool_free(bufp, hvc_iucv_mempool);
182 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
183 * @list: List containing struct iucv_tty_buffer elements.
185 static void destroy_tty_buffer_list(struct list_head *list)
187 struct iucv_tty_buffer *ent, *next;
189 list_for_each_entry_safe(ent, next, list, list) {
190 list_del(&ent->list);
191 destroy_tty_buffer(ent);
196 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
197 * @priv: Pointer to struct hvc_iucv_private
198 * @buf: HVC buffer for writing received terminal data.
199 * @count: HVC buffer size.
200 * @has_more_data: Pointer to an int variable.
202 * The function picks up pending messages from the input queue and receives
203 * the message data that is then written to the specified buffer @buf.
204 * If the buffer size @count is less than the data message size, the
205 * message is kept on the input queue and @has_more_data is set to 1.
206 * If all message data has been written, the message is removed from
207 * the input queue.
209 * The function returns the number of bytes written to the terminal, zero if
210 * there are no pending data messages available or if there is no established
211 * IUCV path.
212 * If the IUCV path has been severed, then -EPIPE is returned to cause a
213 * hang up (that is issued by the HVC layer).
215 static int hvc_iucv_write(struct hvc_iucv_private *priv,
216 char *buf, int count, int *has_more_data)
218 struct iucv_tty_buffer *rb;
219 int written;
220 int rc;
222 /* immediately return if there is no IUCV connection */
223 if (priv->iucv_state == IUCV_DISCONN)
224 return 0;
226 /* if the IUCV path has been severed, return -EPIPE to inform the
227 * HVC layer to hang up the tty device. */
228 if (priv->iucv_state == IUCV_SEVERED)
229 return -EPIPE;
231 /* check if there are pending messages */
232 if (list_empty(&priv->tty_inqueue))
233 return 0;
235 /* receive an iucv message and flip data to the tty (ldisc) */
236 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
238 written = 0;
239 if (!rb->mbuf) { /* message not yet received ... */
240 /* allocate mem to store msg data; if no memory is available
241 * then leave the buffer on the list and re-try later */
242 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
243 if (!rb->mbuf)
244 return -ENOMEM;
246 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
247 rb->mbuf, rb->msg.length, NULL);
248 switch (rc) {
249 case 0: /* Successful */
250 break;
251 case 2: /* No message found */
252 case 9: /* Message purged */
253 break;
254 default:
255 written = -EIO;
257 /* remove buffer if an error has occured or received data
258 * is not correct */
259 if (rc || (rb->mbuf->version != MSG_VERSION) ||
260 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
261 goto out_remove_buffer;
264 switch (rb->mbuf->type) {
265 case MSG_TYPE_DATA:
266 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
267 memcpy(buf, rb->mbuf->data + rb->offset, written);
268 if (written < (rb->mbuf->datalen - rb->offset)) {
269 rb->offset += written;
270 *has_more_data = 1;
271 goto out_written;
273 break;
275 case MSG_TYPE_WINSIZE:
276 if (rb->mbuf->datalen != sizeof(struct winsize))
277 break;
278 /* The caller must ensure that the hvc is locked, which
279 * is the case when called from hvc_iucv_get_chars() */
280 __hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
281 break;
283 case MSG_TYPE_ERROR: /* ignored ... */
284 case MSG_TYPE_TERMENV: /* ignored ... */
285 case MSG_TYPE_TERMIOS: /* ignored ... */
286 break;
289 out_remove_buffer:
290 list_del(&rb->list);
291 destroy_tty_buffer(rb);
292 *has_more_data = !list_empty(&priv->tty_inqueue);
294 out_written:
295 return written;
299 * hvc_iucv_get_chars() - HVC get_chars operation.
300 * @vtermno: HVC virtual terminal number.
301 * @buf: Pointer to a buffer to store data
302 * @count: Size of buffer available for writing
304 * The HVC thread calls this method to read characters from the back-end.
305 * If an IUCV communication path has been established, pending IUCV messages
306 * are received and data is copied into buffer @buf up to @count bytes.
308 * Locking: The routine gets called under an irqsave() spinlock; and
309 * the routine locks the struct hvc_iucv_private->lock to call
310 * helper functions.
312 static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
314 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
315 int written;
316 int has_more_data;
318 if (count <= 0)
319 return 0;
321 if (!priv)
322 return -ENODEV;
324 spin_lock(&priv->lock);
325 has_more_data = 0;
326 written = hvc_iucv_write(priv, buf, count, &has_more_data);
327 spin_unlock(&priv->lock);
329 /* if there are still messages on the queue... schedule another run */
330 if (has_more_data)
331 hvc_kick();
333 return written;
337 * hvc_iucv_queue() - Buffer terminal data for sending.
338 * @priv: Pointer to struct hvc_iucv_private instance.
339 * @buf: Buffer containing data to send.
340 * @count: Size of buffer and amount of data to send.
342 * The function queues data for sending. To actually send the buffered data,
343 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
344 * The function returns the number of data bytes that has been buffered.
346 * If the device is not connected, data is ignored and the function returns
347 * @count.
348 * If the buffer is full, the function returns 0.
349 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
350 * (that can be passed to HVC layer to cause a tty hangup).
352 static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
353 int count)
355 size_t len;
357 if (priv->iucv_state == IUCV_DISCONN)
358 return count; /* ignore data */
360 if (priv->iucv_state == IUCV_SEVERED)
361 return -EPIPE;
363 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
364 if (!len)
365 return 0;
367 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
368 priv->sndbuf_len += len;
370 if (priv->iucv_state == IUCV_CONNECTED)
371 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
373 return len;
377 * hvc_iucv_send() - Send an IUCV message containing terminal data.
378 * @priv: Pointer to struct hvc_iucv_private instance.
380 * If an IUCV communication path has been established, the buffered output data
381 * is sent via an IUCV message and the number of bytes sent is returned.
382 * Returns 0 if there is no established IUCV communication path or
383 * -EPIPE if an existing IUCV communicaton path has been severed.
385 static int hvc_iucv_send(struct hvc_iucv_private *priv)
387 struct iucv_tty_buffer *sb;
388 int rc, len;
390 if (priv->iucv_state == IUCV_SEVERED)
391 return -EPIPE;
393 if (priv->iucv_state == IUCV_DISCONN)
394 return -EIO;
396 if (!priv->sndbuf_len)
397 return 0;
399 /* allocate internal buffer to store msg data and also compute total
400 * message length */
401 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
402 if (!sb)
403 return -ENOMEM;
405 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
406 sb->mbuf->datalen = (u16) priv->sndbuf_len;
407 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
409 list_add_tail(&sb->list, &priv->tty_outqueue);
411 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
412 (void *) sb->mbuf, sb->msg.length);
413 if (rc) {
414 /* drop the message here; however we might want to handle
415 * 0x03 (msg limit reached) by trying again... */
416 list_del(&sb->list);
417 destroy_tty_buffer(sb);
419 len = priv->sndbuf_len;
420 priv->sndbuf_len = 0;
422 return len;
426 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
427 * @work: Work structure.
429 * This work queue function sends buffered output data over IUCV and,
430 * if not all buffered data could be sent, reschedules itself.
432 static void hvc_iucv_sndbuf_work(struct work_struct *work)
434 struct hvc_iucv_private *priv;
436 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
437 if (!priv)
438 return;
440 spin_lock_bh(&priv->lock);
441 hvc_iucv_send(priv);
442 spin_unlock_bh(&priv->lock);
446 * hvc_iucv_put_chars() - HVC put_chars operation.
447 * @vtermno: HVC virtual terminal number.
448 * @buf: Pointer to an buffer to read data from
449 * @count: Size of buffer available for reading
451 * The HVC thread calls this method to write characters to the back-end.
452 * The function calls hvc_iucv_queue() to queue terminal data for sending.
454 * Locking: The method gets called under an irqsave() spinlock; and
455 * locks struct hvc_iucv_private->lock.
457 static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
459 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
460 int queued;
462 if (count <= 0)
463 return 0;
465 if (!priv)
466 return -ENODEV;
468 spin_lock(&priv->lock);
469 queued = hvc_iucv_queue(priv, buf, count);
470 spin_unlock(&priv->lock);
472 return queued;
476 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
477 * @hp: Pointer to the HVC device (struct hvc_struct)
478 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
479 * hvc_iucv_private instance.
481 * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
482 * instance that is derived from @id. Always returns 0.
484 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
486 static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
488 struct hvc_iucv_private *priv;
490 priv = hvc_iucv_get_private(id);
491 if (!priv)
492 return 0;
494 spin_lock_bh(&priv->lock);
495 priv->tty_state = TTY_OPENED;
496 spin_unlock_bh(&priv->lock);
498 return 0;
502 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
503 * @priv: Pointer to the struct hvc_iucv_private instance.
505 static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
507 destroy_tty_buffer_list(&priv->tty_outqueue);
508 destroy_tty_buffer_list(&priv->tty_inqueue);
510 priv->tty_state = TTY_CLOSED;
511 priv->iucv_state = IUCV_DISCONN;
513 priv->sndbuf_len = 0;
517 * tty_outqueue_empty() - Test if the tty outq is empty
518 * @priv: Pointer to struct hvc_iucv_private instance.
520 static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
522 int rc;
524 spin_lock_bh(&priv->lock);
525 rc = list_empty(&priv->tty_outqueue);
526 spin_unlock_bh(&priv->lock);
528 return rc;
532 * flush_sndbuf_sync() - Flush send buffer and wait for completion
533 * @priv: Pointer to struct hvc_iucv_private instance.
535 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
536 * to flush any buffered terminal output data and waits for completion.
538 static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
540 int sync_wait;
542 cancel_delayed_work_sync(&priv->sndbuf_work);
544 spin_lock_bh(&priv->lock);
545 hvc_iucv_send(priv); /* force sending buffered data */
546 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
547 spin_unlock_bh(&priv->lock);
549 if (sync_wait)
550 wait_event_timeout(priv->sndbuf_waitq,
551 tty_outqueue_empty(priv), HZ/10);
555 * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
556 * @priv: Pointer to hvc_iucv_private structure
558 * This routine severs an existing IUCV communication path and hangs
559 * up the underlying HVC terminal device.
560 * The hang-up occurs only if an IUCV communication path is established;
561 * otherwise there is no need to hang up the terminal device.
563 * The IUCV HVC hang-up is separated into two steps:
564 * 1. After the IUCV path has been severed, the iucv_state is set to
565 * IUCV_SEVERED.
566 * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
567 * IUCV_SEVERED state causes the tty hang-up in the HVC layer.
569 * If the tty has not yet been opened, clean up the hvc_iucv_private
570 * structure to allow re-connects.
571 * If the tty has been opened, let get_chars() return -EPIPE to signal
572 * the HVC layer to hang up the tty and, if so, wake up the HVC thread
573 * to call get_chars()...
575 * Special notes on hanging up a HVC terminal instantiated as console:
576 * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
577 * 2. do_tty_hangup() calls tty->ops->close() for console_filp
578 * => no hangup notifier is called by HVC (default)
579 * 2. hvc_close() returns because of tty_hung_up_p(filp)
580 * => no delete notifier is called!
581 * Finally, the back-end is not being notified, thus, the tty session is
582 * kept active (TTY_OPEN) to be ready for re-connects.
584 * Locking: spin_lock(&priv->lock) w/o disabling bh
586 static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
588 struct iucv_path *path;
590 path = NULL;
591 spin_lock(&priv->lock);
592 if (priv->iucv_state == IUCV_CONNECTED) {
593 path = priv->path;
594 priv->path = NULL;
595 priv->iucv_state = IUCV_SEVERED;
596 if (priv->tty_state == TTY_CLOSED)
597 hvc_iucv_cleanup(priv);
598 else
599 /* console is special (see above) */
600 if (priv->is_console) {
601 hvc_iucv_cleanup(priv);
602 priv->tty_state = TTY_OPENED;
603 } else
604 hvc_kick();
606 spin_unlock(&priv->lock);
608 /* finally sever path (outside of priv->lock due to lock ordering) */
609 if (path) {
610 iucv_path_sever(path, NULL);
611 iucv_path_free(path);
616 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
617 * @hp: Pointer to the HVC device (struct hvc_struct)
618 * @id: Additional data (originally passed to hvc_alloc):
619 * the index of an struct hvc_iucv_private instance.
621 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
622 * virtual or otherwise) has occured.
623 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
624 * to keep an existing IUCV communication path established.
625 * (Background: vhangup() is called from user space (by getty or login) to
626 * disable writing to the tty by other applications).
627 * If the tty has been opened and an established IUCV path has been severed
628 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
630 * Locking: struct hvc_iucv_private->lock
632 static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
634 struct hvc_iucv_private *priv;
636 priv = hvc_iucv_get_private(id);
637 if (!priv)
638 return;
640 flush_sndbuf_sync(priv);
642 spin_lock_bh(&priv->lock);
643 /* NOTE: If the hangup was scheduled by ourself (from the iucv
644 * path_servered callback [IUCV_SEVERED]), we have to clean up
645 * our structure and to set state to TTY_CLOSED.
646 * If the tty was hung up otherwise (e.g. vhangup()), then we
647 * ignore this hangup and keep an established IUCV path open...
648 * (...the reason is that we are not able to connect back to the
649 * client if we disconnect on hang up) */
650 priv->tty_state = TTY_CLOSED;
652 if (priv->iucv_state == IUCV_SEVERED)
653 hvc_iucv_cleanup(priv);
654 spin_unlock_bh(&priv->lock);
658 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
659 * @hp: Pointer to the HVC device (struct hvc_struct)
660 * @id: Additional data (originally passed to hvc_alloc):
661 * the index of an struct hvc_iucv_private instance.
663 * This routine notifies the HVC back-end that the last tty device fd has been
664 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
665 * hvc_iucv_private instance.
667 * Locking: struct hvc_iucv_private->lock
669 static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
671 struct hvc_iucv_private *priv;
672 struct iucv_path *path;
674 priv = hvc_iucv_get_private(id);
675 if (!priv)
676 return;
678 flush_sndbuf_sync(priv);
680 spin_lock_bh(&priv->lock);
681 path = priv->path; /* save reference to IUCV path */
682 priv->path = NULL;
683 hvc_iucv_cleanup(priv);
684 spin_unlock_bh(&priv->lock);
686 /* sever IUCV path outside of priv->lock due to lock ordering of:
687 * priv->lock <--> iucv_table_lock */
688 if (path) {
689 iucv_path_sever(path, NULL);
690 iucv_path_free(path);
695 * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
696 * @ipvmid: Originating z/VM user ID (right padded with blanks)
698 * Returns 0 if the z/VM user ID @ipvmid is allowed to connection, otherwise
699 * non-zero.
701 static int hvc_iucv_filter_connreq(u8 ipvmid[8])
703 size_t i;
705 /* Note: default policy is ACCEPT if no filter is set */
706 if (!hvc_iucv_filter_size)
707 return 0;
709 for (i = 0; i < hvc_iucv_filter_size; i++)
710 if (0 == memcmp(ipvmid, hvc_iucv_filter + (8 * i), 8))
711 return 0;
712 return 1;
716 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
717 * @path: Pending path (struct iucv_path)
718 * @ipvmid: z/VM system identifier of originator
719 * @ipuser: User specified data for this path
720 * (AF_IUCV: port/service name and originator port)
722 * The function uses the @ipuser data to determine if the pending path belongs
723 * to a terminal managed by this device driver.
724 * If the path belongs to this driver, ensure that the terminal is not accessed
725 * multiple times (only one connection to a terminal is allowed).
726 * If the terminal is not yet connected, the pending path is accepted and is
727 * associated to the appropriate struct hvc_iucv_private instance.
729 * Returns 0 if @path belongs to a terminal managed by the this device driver;
730 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
732 * Locking: struct hvc_iucv_private->lock
734 static int hvc_iucv_path_pending(struct iucv_path *path,
735 u8 ipvmid[8], u8 ipuser[16])
737 struct hvc_iucv_private *priv;
738 u8 nuser_data[16];
739 u8 vm_user_id[9];
740 int i, rc;
742 priv = NULL;
743 for (i = 0; i < hvc_iucv_devices; i++)
744 if (hvc_iucv_table[i] &&
745 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
746 priv = hvc_iucv_table[i];
747 break;
749 if (!priv)
750 return -ENODEV;
752 /* Enforce that ipvmid is allowed to connect to us */
753 read_lock(&hvc_iucv_filter_lock);
754 rc = hvc_iucv_filter_connreq(ipvmid);
755 read_unlock(&hvc_iucv_filter_lock);
756 if (rc) {
757 iucv_path_sever(path, ipuser);
758 iucv_path_free(path);
759 memcpy(vm_user_id, ipvmid, 8);
760 vm_user_id[8] = 0;
761 pr_info("A connection request from z/VM user ID %s "
762 "was refused\n", vm_user_id);
763 return 0;
766 spin_lock(&priv->lock);
768 /* If the terminal is already connected or being severed, then sever
769 * this path to enforce that there is only ONE established communication
770 * path per terminal. */
771 if (priv->iucv_state != IUCV_DISCONN) {
772 iucv_path_sever(path, ipuser);
773 iucv_path_free(path);
774 goto out_path_handled;
777 /* accept path */
778 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
779 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
780 path->msglim = 0xffff; /* IUCV MSGLIMIT */
781 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
782 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
783 if (rc) {
784 iucv_path_sever(path, ipuser);
785 iucv_path_free(path);
786 goto out_path_handled;
788 priv->path = path;
789 priv->iucv_state = IUCV_CONNECTED;
791 /* flush buffered output data... */
792 schedule_delayed_work(&priv->sndbuf_work, 5);
794 out_path_handled:
795 spin_unlock(&priv->lock);
796 return 0;
800 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
801 * @path: Pending path (struct iucv_path)
802 * @ipuser: User specified data for this path
803 * (AF_IUCV: port/service name and originator port)
805 * This function calls the hvc_iucv_hangup() function for the
806 * respective IUCV HVC terminal.
808 * Locking: struct hvc_iucv_private->lock
810 static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
812 struct hvc_iucv_private *priv = path->private;
814 hvc_iucv_hangup(priv);
818 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
819 * @path: Pending path (struct iucv_path)
820 * @msg: Pointer to the IUCV message
822 * The function puts an incoming message on the input queue for later
823 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
824 * If the tty has not yet been opened, the message is rejected.
826 * Locking: struct hvc_iucv_private->lock
828 static void hvc_iucv_msg_pending(struct iucv_path *path,
829 struct iucv_message *msg)
831 struct hvc_iucv_private *priv = path->private;
832 struct iucv_tty_buffer *rb;
834 /* reject messages that exceed max size of iucv_tty_msg->datalen */
835 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
836 iucv_message_reject(path, msg);
837 return;
840 spin_lock(&priv->lock);
842 /* reject messages if tty has not yet been opened */
843 if (priv->tty_state == TTY_CLOSED) {
844 iucv_message_reject(path, msg);
845 goto unlock_return;
848 /* allocate tty buffer to save iucv msg only */
849 rb = alloc_tty_buffer(0, GFP_ATOMIC);
850 if (!rb) {
851 iucv_message_reject(path, msg);
852 goto unlock_return; /* -ENOMEM */
854 rb->msg = *msg;
856 list_add_tail(&rb->list, &priv->tty_inqueue);
858 hvc_kick(); /* wake up hvc thread */
860 unlock_return:
861 spin_unlock(&priv->lock);
865 * hvc_iucv_msg_complete() - IUCV handler to process message completion
866 * @path: Pending path (struct iucv_path)
867 * @msg: Pointer to the IUCV message
869 * The function is called upon completion of message delivery to remove the
870 * message from the outqueue. Additional delivery information can be found
871 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
872 * purged messages (0x010000 (IPADPGNR)).
874 * Locking: struct hvc_iucv_private->lock
876 static void hvc_iucv_msg_complete(struct iucv_path *path,
877 struct iucv_message *msg)
879 struct hvc_iucv_private *priv = path->private;
880 struct iucv_tty_buffer *ent, *next;
881 LIST_HEAD(list_remove);
883 spin_lock(&priv->lock);
884 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
885 if (ent->msg.id == msg->id) {
886 list_move(&ent->list, &list_remove);
887 break;
889 wake_up(&priv->sndbuf_waitq);
890 spin_unlock(&priv->lock);
891 destroy_tty_buffer_list(&list_remove);
895 * hvc_iucv_pm_freeze() - Freeze PM callback
896 * @dev: IUVC HVC terminal device
898 * Sever an established IUCV communication path and
899 * trigger a hang-up of the underlying HVC terminal.
901 static int hvc_iucv_pm_freeze(struct device *dev)
903 struct hvc_iucv_private *priv = dev_get_drvdata(dev);
905 local_bh_disable();
906 hvc_iucv_hangup(priv);
907 local_bh_enable();
909 return 0;
913 * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
914 * @dev: IUVC HVC terminal device
916 * Wake up the HVC thread to trigger hang-up and respective
917 * HVC back-end notifier invocations.
919 static int hvc_iucv_pm_restore_thaw(struct device *dev)
921 hvc_kick();
922 return 0;
926 /* HVC operations */
927 static struct hv_ops hvc_iucv_ops = {
928 .get_chars = hvc_iucv_get_chars,
929 .put_chars = hvc_iucv_put_chars,
930 .notifier_add = hvc_iucv_notifier_add,
931 .notifier_del = hvc_iucv_notifier_del,
932 .notifier_hangup = hvc_iucv_notifier_hangup,
935 /* Suspend / resume device operations */
936 static const struct dev_pm_ops hvc_iucv_pm_ops = {
937 .freeze = hvc_iucv_pm_freeze,
938 .thaw = hvc_iucv_pm_restore_thaw,
939 .restore = hvc_iucv_pm_restore_thaw,
942 /* IUCV HVC device driver */
943 static struct device_driver hvc_iucv_driver = {
944 .name = KMSG_COMPONENT,
945 .bus = &iucv_bus,
946 .pm = &hvc_iucv_pm_ops,
950 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
951 * @id: hvc_iucv_table index
952 * @is_console: Flag if the instance is used as Linux console
954 * This function allocates a new hvc_iucv_private structure and stores
955 * the instance in hvc_iucv_table at index @id.
956 * Returns 0 on success; otherwise non-zero.
958 static int __init hvc_iucv_alloc(int id, unsigned int is_console)
960 struct hvc_iucv_private *priv;
961 char name[9];
962 int rc;
964 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
965 if (!priv)
966 return -ENOMEM;
968 spin_lock_init(&priv->lock);
969 INIT_LIST_HEAD(&priv->tty_outqueue);
970 INIT_LIST_HEAD(&priv->tty_inqueue);
971 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
972 init_waitqueue_head(&priv->sndbuf_waitq);
974 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
975 if (!priv->sndbuf) {
976 kfree(priv);
977 return -ENOMEM;
980 /* set console flag */
981 priv->is_console = is_console;
983 /* allocate hvc device */
984 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
985 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
986 if (IS_ERR(priv->hvc)) {
987 rc = PTR_ERR(priv->hvc);
988 goto out_error_hvc;
991 /* notify HVC thread instead of using polling */
992 priv->hvc->irq_requested = 1;
994 /* setup iucv related information */
995 snprintf(name, 9, "lnxhvc%-2d", id);
996 memcpy(priv->srv_name, name, 8);
997 ASCEBC(priv->srv_name, 8);
999 /* create and setup device */
1000 priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
1001 if (!priv->dev) {
1002 rc = -ENOMEM;
1003 goto out_error_dev;
1005 dev_set_name(priv->dev, "hvc_iucv%d", id);
1006 dev_set_drvdata(priv->dev, priv);
1007 priv->dev->bus = &iucv_bus;
1008 priv->dev->parent = iucv_root;
1009 priv->dev->driver = &hvc_iucv_driver;
1010 priv->dev->release = (void (*)(struct device *)) kfree;
1011 rc = device_register(priv->dev);
1012 if (rc) {
1013 put_device(priv->dev);
1014 goto out_error_dev;
1017 hvc_iucv_table[id] = priv;
1018 return 0;
1020 out_error_dev:
1021 hvc_remove(priv->hvc);
1022 out_error_hvc:
1023 free_page((unsigned long) priv->sndbuf);
1024 kfree(priv);
1026 return rc;
1030 * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1032 static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1034 hvc_remove(priv->hvc);
1035 device_unregister(priv->dev);
1036 free_page((unsigned long) priv->sndbuf);
1037 kfree(priv);
1041 * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
1042 * @filter: String containing a comma-separated list of z/VM user IDs
1044 static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1046 const char *nextdelim, *residual;
1047 size_t len;
1049 nextdelim = strchr(filter, ',');
1050 if (nextdelim) {
1051 len = nextdelim - filter;
1052 residual = nextdelim + 1;
1053 } else {
1054 len = strlen(filter);
1055 residual = filter + len;
1058 if (len == 0)
1059 return ERR_PTR(-EINVAL);
1061 /* check for '\n' (if called from sysfs) */
1062 if (filter[len - 1] == '\n')
1063 len--;
1065 if (len > 8)
1066 return ERR_PTR(-EINVAL);
1068 /* pad with blanks and save upper case version of user ID */
1069 memset(dest, ' ', 8);
1070 while (len--)
1071 dest[len] = toupper(filter[len]);
1072 return residual;
1076 * hvc_iucv_setup_filter() - Set up z/VM user ID filter
1077 * @filter: String consisting of a comma-separated list of z/VM user IDs
1079 * The function parses the @filter string and creates an array containing
1080 * the list of z/VM user ID filter entries.
1081 * Return code 0 means success, -EINVAL if the filter is syntactically
1082 * incorrect, -ENOMEM if there was not enough memory to allocate the
1083 * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
1085 static int hvc_iucv_setup_filter(const char *val)
1087 const char *residual;
1088 int err;
1089 size_t size, count;
1090 void *array, *old_filter;
1092 count = strlen(val);
1093 if (count == 0 || (count == 1 && val[0] == '\n')) {
1094 size = 0;
1095 array = NULL;
1096 goto out_replace_filter; /* clear filter */
1099 /* count user IDs in order to allocate sufficient memory */
1100 size = 1;
1101 residual = val;
1102 while ((residual = strchr(residual, ',')) != NULL) {
1103 residual++;
1104 size++;
1107 /* check if the specified list exceeds the filter limit */
1108 if (size > MAX_VMID_FILTER)
1109 return -ENOSPC;
1111 array = kzalloc(size * 8, GFP_KERNEL);
1112 if (!array)
1113 return -ENOMEM;
1115 count = size;
1116 residual = val;
1117 while (*residual && count) {
1118 residual = hvc_iucv_parse_filter(residual,
1119 array + ((size - count) * 8));
1120 if (IS_ERR(residual)) {
1121 err = PTR_ERR(residual);
1122 kfree(array);
1123 goto out_err;
1125 count--;
1128 out_replace_filter:
1129 write_lock_bh(&hvc_iucv_filter_lock);
1130 old_filter = hvc_iucv_filter;
1131 hvc_iucv_filter_size = size;
1132 hvc_iucv_filter = array;
1133 write_unlock_bh(&hvc_iucv_filter_lock);
1134 kfree(old_filter);
1136 err = 0;
1137 out_err:
1138 return err;
1142 * param_set_vmidfilter() - Set z/VM user ID filter parameter
1143 * @val: String consisting of a comma-separated list of z/VM user IDs
1144 * @kp: Kernel parameter pointing to hvc_iucv_filter array
1146 * The function sets up the z/VM user ID filter specified as comma-separated
1147 * list of user IDs in @val.
1148 * Note: If it is called early in the boot process, @val is stored and
1149 * parsed later in hvc_iucv_init().
1151 static int param_set_vmidfilter(const char *val, struct kernel_param *kp)
1153 int rc;
1155 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1156 return -ENODEV;
1158 if (!val)
1159 return -EINVAL;
1161 rc = 0;
1162 if (slab_is_available())
1163 rc = hvc_iucv_setup_filter(val);
1164 else
1165 hvc_iucv_filter_string = val; /* defer... */
1166 return rc;
1170 * param_get_vmidfilter() - Get z/VM user ID filter
1171 * @buffer: Buffer to store z/VM user ID filter,
1172 * (buffer size assumption PAGE_SIZE)
1173 * @kp: Kernel parameter pointing to the hvc_iucv_filter array
1175 * The function stores the filter as a comma-separated list of z/VM user IDs
1176 * in @buffer. Typically, sysfs routines call this function for attr show.
1178 static int param_get_vmidfilter(char *buffer, struct kernel_param *kp)
1180 int rc;
1181 size_t index, len;
1182 void *start, *end;
1184 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1185 return -ENODEV;
1187 rc = 0;
1188 read_lock_bh(&hvc_iucv_filter_lock);
1189 for (index = 0; index < hvc_iucv_filter_size; index++) {
1190 start = hvc_iucv_filter + (8 * index);
1191 end = memchr(start, ' ', 8);
1192 len = (end) ? end - start : 8;
1193 memcpy(buffer + rc, start, len);
1194 rc += len;
1195 buffer[rc++] = ',';
1197 read_unlock_bh(&hvc_iucv_filter_lock);
1198 if (rc)
1199 buffer[--rc] = '\0'; /* replace last comma and update rc */
1200 return rc;
1203 #define param_check_vmidfilter(name, p) __param_check(name, p, void)
1206 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
1208 static int __init hvc_iucv_init(void)
1210 int rc;
1211 unsigned int i;
1213 if (!hvc_iucv_devices)
1214 return -ENODEV;
1216 if (!MACHINE_IS_VM) {
1217 pr_notice("The z/VM IUCV HVC device driver cannot "
1218 "be used without z/VM\n");
1219 rc = -ENODEV;
1220 goto out_error;
1223 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
1224 pr_err("%lu is not a valid value for the hvc_iucv= "
1225 "kernel parameter\n", hvc_iucv_devices);
1226 rc = -EINVAL;
1227 goto out_error;
1230 /* register IUCV HVC device driver */
1231 rc = driver_register(&hvc_iucv_driver);
1232 if (rc)
1233 goto out_error;
1235 /* parse hvc_iucv_allow string and create z/VM user ID filter list */
1236 if (hvc_iucv_filter_string) {
1237 rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
1238 switch (rc) {
1239 case 0:
1240 break;
1241 case -ENOMEM:
1242 pr_err("Allocating memory failed with "
1243 "reason code=%d\n", 3);
1244 goto out_error;
1245 case -EINVAL:
1246 pr_err("hvc_iucv_allow= does not specify a valid "
1247 "z/VM user ID list\n");
1248 goto out_error;
1249 case -ENOSPC:
1250 pr_err("hvc_iucv_allow= specifies too many "
1251 "z/VM user IDs\n");
1252 goto out_error;
1253 default:
1254 goto out_error;
1258 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
1259 sizeof(struct iucv_tty_buffer),
1260 0, 0, NULL);
1261 if (!hvc_iucv_buffer_cache) {
1262 pr_err("Allocating memory failed with reason code=%d\n", 1);
1263 rc = -ENOMEM;
1264 goto out_error;
1267 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1268 hvc_iucv_buffer_cache);
1269 if (!hvc_iucv_mempool) {
1270 pr_err("Allocating memory failed with reason code=%d\n", 2);
1271 kmem_cache_destroy(hvc_iucv_buffer_cache);
1272 rc = -ENOMEM;
1273 goto out_error;
1276 /* register the first terminal device as console
1277 * (must be done before allocating hvc terminal devices) */
1278 rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
1279 if (rc) {
1280 pr_err("Registering HVC terminal device as "
1281 "Linux console failed\n");
1282 goto out_error_memory;
1285 /* allocate hvc_iucv_private structs */
1286 for (i = 0; i < hvc_iucv_devices; i++) {
1287 rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
1288 if (rc) {
1289 pr_err("Creating a new HVC terminal device "
1290 "failed with error code=%d\n", rc);
1291 goto out_error_hvc;
1295 /* register IUCV callback handler */
1296 rc = iucv_register(&hvc_iucv_handler, 0);
1297 if (rc) {
1298 pr_err("Registering IUCV handlers failed with error code=%d\n",
1299 rc);
1300 goto out_error_iucv;
1303 return 0;
1305 out_error_iucv:
1306 iucv_unregister(&hvc_iucv_handler, 0);
1307 out_error_hvc:
1308 for (i = 0; i < hvc_iucv_devices; i++)
1309 if (hvc_iucv_table[i])
1310 hvc_iucv_destroy(hvc_iucv_table[i]);
1311 out_error_memory:
1312 mempool_destroy(hvc_iucv_mempool);
1313 kmem_cache_destroy(hvc_iucv_buffer_cache);
1314 out_error:
1315 if (hvc_iucv_filter)
1316 kfree(hvc_iucv_filter);
1317 hvc_iucv_devices = 0; /* ensure that we do not provide any device */
1318 return rc;
1322 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
1323 * @val: Parameter value (numeric)
1325 static int __init hvc_iucv_config(char *val)
1327 return strict_strtoul(val, 10, &hvc_iucv_devices);
1331 device_initcall(hvc_iucv_init);
1332 __setup("hvc_iucv=", hvc_iucv_config);
1333 core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);