blob: 37b0542a4eeb055e52fbe7e137a7cc2f65081ce1 [file] [log] [blame]
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001/*
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01002 * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01003 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01004 * This HVC device driver provides terminal access using
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01005 * z/VM IUCV communication paths.
6 *
Hendrik Brueckner02591622009-06-16 10:30:45 +02007 * Copyright IBM Corp. 2008, 2009
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01008 *
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 */
11#define KMSG_COMPONENT "hvc_iucv"
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010012#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010013
14#include <linux/types.h>
15#include <asm/ebcdic.h>
Hendrik Brueckner431429f2009-03-26 15:23:55 +010016#include <linux/ctype.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010017#include <linux/delay.h>
Hendrik Brueckner02591622009-06-16 10:30:45 +020018#include <linux/device.h>
Hendrik Brueckner68c6b3d2009-01-09 12:15:00 +010019#include <linux/init.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010020#include <linux/mempool.h>
Hendrik Brueckner431429f2009-03-26 15:23:55 +010021#include <linux/moduleparam.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010022#include <linux/tty.h>
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010023#include <linux/wait.h>
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010024#include <net/iucv/iucv.h>
25
26#include "hvc_console.h"
27
28
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010029/* General device driver settings */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010030#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)
33
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 */
41
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010042struct iucv_tty_msg {
43 u8 version; /* Message version */
44 u8 type; /* Message type */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010045#define MSG_MAX_DATALEN ((u16)(~0))
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010046 u16 datalen; /* Payload length */
47 u8 data[]; /* Payload buffer */
48} __attribute__((packed));
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010049#define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010050
51enum iucv_state_t {
52 IUCV_DISCONN = 0,
53 IUCV_CONNECTED = 1,
54 IUCV_SEVERED = 2,
55};
56
57enum tty_state_t {
58 TTY_CLOSED = 0,
59 TTY_OPENED = 1,
60};
61
62struct hvc_iucv_private {
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010063 struct hvc_struct *hvc; /* HVC struct reference */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010064 u8 srv_name[8]; /* IUCV service name (ebcdic) */
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +010065 unsigned char is_console; /* Linux console usage flag */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010066 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 */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +010070#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 */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010076 struct list_head tty_outqueue; /* outgoing IUCV messages */
77 struct list_head tty_inqueue; /* incoming IUCV messages */
Hendrik Brueckner02591622009-06-16 10:30:45 +020078 struct device *dev; /* device structure */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010079};
80
81struct iucv_tty_buffer {
82 struct list_head list; /* list pointer */
Hendrik Brueckner17e19f02009-01-09 12:14:59 +010083 struct iucv_message msg; /* store an IUCV message */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010084 size_t offset; /* data buffer offset */
85 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
86};
87
88/* IUCV callback handler */
89static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
90static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
91static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
92static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
93
94
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +010095/* Kernel module parameter: use one terminal device as default */
96static unsigned long hvc_iucv_devices = 1;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +010097
98/* Array of allocated hvc iucv tty lines... */
99static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100100#define IUCV_HVC_CON_IDX (0)
Hendrik Brueckner431429f2009-03-26 15:23:55 +0100101/* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
102#define MAX_VMID_FILTER (500)
103static size_t hvc_iucv_filter_size;
104static void *hvc_iucv_filter;
105static const char *hvc_iucv_filter_string;
106static DEFINE_RWLOCK(hvc_iucv_filter_lock);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100107
108/* Kmem cache and mempool for iucv_tty_buffer elements */
109static struct kmem_cache *hvc_iucv_buffer_cache;
110static mempool_t *hvc_iucv_mempool;
111
112/* IUCV handler callback functions */
113static 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,
118};
119
120
121/**
122 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
123 * @num: The HVC virtual terminal number (vtermno)
124 *
125 * This function returns the struct hvc_iucv_private instance that corresponds
126 * to the HVC virtual terminal number specified as parameter @num.
127 */
128struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
129{
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];
133}
134
135/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100136 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100137 * @size: Size of the internal buffer used to store data.
138 * @flags: Memory allocation flags passed to mempool.
139 *
140 * This function allocates a new struct iucv_tty_buffer element and, optionally,
141 * allocates an internal data buffer with the specified size @size.
Hendrik Brueckner91a970d2010-03-08 12:25:15 +0100142 * The internal data buffer is always allocated with GFP_DMA which is
143 * required for receiving and sending data with IUCV.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100144 * Note: The total message size arises from the internal buffer size and the
145 * members of the iucv_tty_msg structure.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100146 * The function returns NULL if memory allocation has failed.
147 */
148static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
149{
150 struct iucv_tty_buffer *bufp;
151
152 bufp = mempool_alloc(hvc_iucv_mempool, flags);
153 if (!bufp)
154 return NULL;
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100155 memset(bufp, 0, sizeof(*bufp));
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100156
157 if (size > 0) {
158 bufp->msg.length = MSG_SIZE(size);
Hendrik Brueckner91a970d2010-03-08 12:25:15 +0100159 bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100160 if (!bufp->mbuf) {
161 mempool_free(bufp, hvc_iucv_mempool);
162 return NULL;
163 }
164 bufp->mbuf->version = MSG_VERSION;
165 bufp->mbuf->type = MSG_TYPE_DATA;
166 bufp->mbuf->datalen = (u16) size;
167 }
168 return bufp;
169}
170
171/**
172 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
173 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100174 */
175static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
176{
177 kfree(bufp->mbuf);
178 mempool_free(bufp, hvc_iucv_mempool);
179}
180
181/**
182 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100183 * @list: List containing struct iucv_tty_buffer elements.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100184 */
185static void destroy_tty_buffer_list(struct list_head *list)
186{
187 struct iucv_tty_buffer *ent, *next;
188
189 list_for_each_entry_safe(ent, next, list, list) {
190 list_del(&ent->list);
191 destroy_tty_buffer(ent);
192 }
193}
194
195/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100196 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100200 * @has_more_data: Pointer to an int variable.
201 *
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.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100204 * If the buffer size @count is less than the data message size, the
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100205 * message is kept on the input queue and @has_more_data is set to 1.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100206 * If all message data has been written, the message is removed from
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100207 * the input queue.
208 *
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
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100213 * hang up (that is issued by the HVC layer).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100214 */
215static int hvc_iucv_write(struct hvc_iucv_private *priv,
216 char *buf, int count, int *has_more_data)
217{
218 struct iucv_tty_buffer *rb;
219 int written;
220 int rc;
221
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100222 /* immediately return if there is no IUCV connection */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100223 if (priv->iucv_state == IUCV_DISCONN)
224 return 0;
225
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100226 /* if the IUCV path has been severed, return -EPIPE to inform the
227 * HVC layer to hang up the tty device. */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100228 if (priv->iucv_state == IUCV_SEVERED)
229 return -EPIPE;
230
231 /* check if there are pending messages */
232 if (list_empty(&priv->tty_inqueue))
233 return 0;
234
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100235 /* receive an iucv message and flip data to the tty (ldisc) */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100236 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
237
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 */
Hendrik Brueckner91a970d2010-03-08 12:25:15 +0100242 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100243 if (!rb->mbuf)
244 return -ENOMEM;
245
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;
256 }
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;
262 }
263
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;
272 }
273 break;
274
275 case MSG_TYPE_WINSIZE:
276 if (rb->mbuf->datalen != sizeof(struct winsize))
277 break;
Hendrik Brueckner254be492009-08-27 01:45:39 +0000278 /* 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));
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100281 break;
282
283 case MSG_TYPE_ERROR: /* ignored ... */
284 case MSG_TYPE_TERMENV: /* ignored ... */
285 case MSG_TYPE_TERMIOS: /* ignored ... */
286 break;
287 }
288
289out_remove_buffer:
290 list_del(&rb->list);
291 destroy_tty_buffer(rb);
292 *has_more_data = !list_empty(&priv->tty_inqueue);
293
294out_written:
295 return written;
296}
297
298/**
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
303 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100304 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100307 *
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.
311 */
312static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
313{
314 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
315 int written;
316 int has_more_data;
317
318 if (count <= 0)
319 return 0;
320
321 if (!priv)
322 return -ENODEV;
323
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);
328
329 /* if there are still messages on the queue... schedule another run */
330 if (has_more_data)
331 hvc_kick();
332
333 return written;
334}
335
336/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100337 * hvc_iucv_queue() - Buffer terminal data for sending.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100338 * @priv: Pointer to struct hvc_iucv_private instance.
339 * @buf: Buffer containing data to send.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100340 * @count: Size of buffer and amount of data to send.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100341 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100342 * The function queues data for sending. To actually send the buffered data,
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100343 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100344 * The function returns the number of data bytes that has been buffered.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100345 *
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100346 * 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.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100349 * 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).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100351 */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100352static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100353 int count)
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100354{
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100355 size_t len;
356
357 if (priv->iucv_state == IUCV_DISCONN)
358 return count; /* ignore data */
359
360 if (priv->iucv_state == IUCV_SEVERED)
361 return -EPIPE;
362
363 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
364 if (!len)
365 return 0;
366
367 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
368 priv->sndbuf_len += len;
369
370 if (priv->iucv_state == IUCV_CONNECTED)
371 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
372
373 return len;
374}
375
376/**
377 * hvc_iucv_send() - Send an IUCV message containing terminal data.
378 * @priv: Pointer to struct hvc_iucv_private instance.
379 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100380 * 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.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100384 */
385static int hvc_iucv_send(struct hvc_iucv_private *priv)
386{
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100387 struct iucv_tty_buffer *sb;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100388 int rc, len;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100389
390 if (priv->iucv_state == IUCV_SEVERED)
391 return -EPIPE;
392
393 if (priv->iucv_state == IUCV_DISCONN)
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100394 return -EIO;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100395
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100396 if (!priv->sndbuf_len)
397 return 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100398
399 /* allocate internal buffer to store msg data and also compute total
400 * message length */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100401 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100402 if (!sb)
403 return -ENOMEM;
404
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100405 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);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100408
409 list_add_tail(&sb->list, &priv->tty_outqueue);
410
411 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
412 (void *) sb->mbuf, sb->msg.length);
413 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100414 /* drop the message here; however we might want to handle
415 * 0x03 (msg limit reached) by trying again... */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100416 list_del(&sb->list);
417 destroy_tty_buffer(sb);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100418 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100419 len = priv->sndbuf_len;
420 priv->sndbuf_len = 0;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100421
422 return len;
423}
424
425/**
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100426 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
427 * @work: Work structure.
428 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100429 * This work queue function sends buffered output data over IUCV and,
430 * if not all buffered data could be sent, reschedules itself.
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100431 */
432static void hvc_iucv_sndbuf_work(struct work_struct *work)
433{
434 struct hvc_iucv_private *priv;
435
436 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100437 if (!priv)
438 return;
439
440 spin_lock_bh(&priv->lock);
441 hvc_iucv_send(priv);
442 spin_unlock_bh(&priv->lock);
443}
444
445/**
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100446 * 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
450 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100451 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100453 *
454 * Locking: The method gets called under an irqsave() spinlock; and
455 * locks struct hvc_iucv_private->lock.
456 */
457static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
458{
459 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100460 int queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100461
462 if (count <= 0)
463 return 0;
464
465 if (!priv)
466 return -ENODEV;
467
468 spin_lock(&priv->lock);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100469 queued = hvc_iucv_queue(priv, buf, count);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100470 spin_unlock(&priv->lock);
471
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100472 return queued;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100473}
474
475/**
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.
480 *
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100481 * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100482 * instance that is derived from @id. Always returns 0.
483 *
484 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
485 */
486static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
487{
488 struct hvc_iucv_private *priv;
489
490 priv = hvc_iucv_get_private(id);
491 if (!priv)
492 return 0;
493
494 spin_lock_bh(&priv->lock);
495 priv->tty_state = TTY_OPENED;
496 spin_unlock_bh(&priv->lock);
497
498 return 0;
499}
500
501/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100502 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100503 * @priv: Pointer to the struct hvc_iucv_private instance.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100504 */
505static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
506{
507 destroy_tty_buffer_list(&priv->tty_outqueue);
508 destroy_tty_buffer_list(&priv->tty_inqueue);
509
510 priv->tty_state = TTY_CLOSED;
511 priv->iucv_state = IUCV_DISCONN;
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100512
513 priv->sndbuf_len = 0;
514}
515
516/**
517 * tty_outqueue_empty() - Test if the tty outq is empty
518 * @priv: Pointer to struct hvc_iucv_private instance.
519 */
520static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
521{
522 int rc;
523
524 spin_lock_bh(&priv->lock);
525 rc = list_empty(&priv->tty_outqueue);
526 spin_unlock_bh(&priv->lock);
527
528 return rc;
529}
530
531/**
532 * flush_sndbuf_sync() - Flush send buffer and wait for completion
533 * @priv: Pointer to struct hvc_iucv_private instance.
534 *
535 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
536 * to flush any buffered terminal output data and waits for completion.
537 */
538static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
539{
540 int sync_wait;
541
542 cancel_delayed_work_sync(&priv->sndbuf_work);
543
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);
548
549 if (sync_wait)
550 wait_event_timeout(priv->sndbuf_waitq,
Hendrik Brueckner02591622009-06-16 10:30:45 +0200551 tty_outqueue_empty(priv), HZ/10);
552}
553
554/**
555 * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
556 * @priv: Pointer to hvc_iucv_private structure
557 *
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.
562 *
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.
568 *
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()...
574 *
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.
583 *
584 * Locking: spin_lock(&priv->lock) w/o disabling bh
585 */
586static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
587{
588 struct iucv_path *path;
589
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();
605 }
606 spin_unlock(&priv->lock);
607
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);
612 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100613}
614
615/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100616 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100620 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100621 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100622 * virtual or otherwise) has occured.
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100623 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
624 * to keep an existing IUCV communication path established.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100625 * (Background: vhangup() is called from user space (by getty or login) to
626 * disable writing to the tty by other applications).
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100627 * 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().
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100629 *
630 * Locking: struct hvc_iucv_private->lock
631 */
632static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
633{
634 struct hvc_iucv_private *priv;
635
636 priv = hvc_iucv_get_private(id);
637 if (!priv)
638 return;
639
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100640 flush_sndbuf_sync(priv);
641
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100642 spin_lock_bh(&priv->lock);
643 /* NOTE: If the hangup was scheduled by ourself (from the iucv
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100644 * path_servered callback [IUCV_SEVERED]), we have to clean up
645 * our structure and to set state to TTY_CLOSED.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100646 * 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;
651
652 if (priv->iucv_state == IUCV_SEVERED)
653 hvc_iucv_cleanup(priv);
654 spin_unlock_bh(&priv->lock);
655}
656
657/**
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.
662 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100663 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100666 *
667 * Locking: struct hvc_iucv_private->lock
668 */
669static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
670{
671 struct hvc_iucv_private *priv;
672 struct iucv_path *path;
673
674 priv = hvc_iucv_get_private(id);
675 if (!priv)
676 return;
677
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100678 flush_sndbuf_sync(priv);
679
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100680 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);
685
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);
691 }
692}
693
694/**
Hendrik Brueckner431429f2009-03-26 15:23:55 +0100695 * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
696 * @ipvmid: Originating z/VM user ID (right padded with blanks)
697 *
698 * Returns 0 if the z/VM user ID @ipvmid is allowed to connection, otherwise
699 * non-zero.
700 */
701static int hvc_iucv_filter_connreq(u8 ipvmid[8])
702{
703 size_t i;
704
705 /* Note: default policy is ACCEPT if no filter is set */
706 if (!hvc_iucv_filter_size)
707 return 0;
708
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;
713}
714
715/**
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100716 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
717 * @path: Pending path (struct iucv_path)
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100718 * @ipvmid: z/VM system identifier of originator
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100719 * @ipuser: User specified data for this path
720 * (AF_IUCV: port/service name and originator port)
721 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100722 * 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.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100728 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100729 * Returns 0 if @path belongs to a terminal managed by the this device driver;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100730 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
731 *
732 * Locking: struct hvc_iucv_private->lock
733 */
734static int hvc_iucv_path_pending(struct iucv_path *path,
735 u8 ipvmid[8], u8 ipuser[16])
736{
737 struct hvc_iucv_private *priv;
738 u8 nuser_data[16];
Hendrik Brueckner431429f2009-03-26 15:23:55 +0100739 u8 vm_user_id[9];
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100740 int i, rc;
741
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;
748 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100749 if (!priv)
750 return -ENODEV;
751
Hendrik Brueckner431429f2009-03-26 15:23:55 +0100752 /* 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;
764 }
765
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100766 spin_lock(&priv->lock);
767
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;
775 }
776
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;
787 }
788 priv->path = path;
789 priv->iucv_state = IUCV_CONNECTED;
790
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100791 /* flush buffered output data... */
792 schedule_delayed_work(&priv->sndbuf_work, 5);
793
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100794out_path_handled:
795 spin_unlock(&priv->lock);
796 return 0;
797}
798
799/**
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)
804 *
Hendrik Brueckner02591622009-06-16 10:30:45 +0200805 * This function calls the hvc_iucv_hangup() function for the
806 * respective IUCV HVC terminal.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100807 *
808 * Locking: struct hvc_iucv_private->lock
809 */
810static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
811{
812 struct hvc_iucv_private *priv = path->private;
813
Hendrik Brueckner02591622009-06-16 10:30:45 +0200814 hvc_iucv_hangup(priv);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100815}
816
817/**
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
821 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100822 * The function puts an incoming message on the input queue for later
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100823 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100824 * If the tty has not yet been opened, the message is rejected.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100825 *
826 * Locking: struct hvc_iucv_private->lock
827 */
828static void hvc_iucv_msg_pending(struct iucv_path *path,
829 struct iucv_message *msg)
830{
831 struct hvc_iucv_private *priv = path->private;
832 struct iucv_tty_buffer *rb;
833
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100834 /* 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;
838 }
839
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100840 spin_lock(&priv->lock);
841
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;
846 }
847
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100848 /* allocate tty buffer to save iucv msg only */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100849 rb = alloc_tty_buffer(0, GFP_ATOMIC);
850 if (!rb) {
851 iucv_message_reject(path, msg);
852 goto unlock_return; /* -ENOMEM */
853 }
854 rb->msg = *msg;
855
856 list_add_tail(&rb->list, &priv->tty_inqueue);
857
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100858 hvc_kick(); /* wake up hvc thread */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100859
860unlock_return:
861 spin_unlock(&priv->lock);
862}
863
864/**
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
868 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100869 * 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)).
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100873 *
874 * Locking: struct hvc_iucv_private->lock
875 */
876static void hvc_iucv_msg_complete(struct iucv_path *path,
877 struct iucv_message *msg)
878{
879 struct hvc_iucv_private *priv = path->private;
880 struct iucv_tty_buffer *ent, *next;
881 LIST_HEAD(list_remove);
882
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;
888 }
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100889 wake_up(&priv->sndbuf_waitq);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100890 spin_unlock(&priv->lock);
891 destroy_tty_buffer_list(&list_remove);
892}
893
Hendrik Brueckner02591622009-06-16 10:30:45 +0200894/**
895 * hvc_iucv_pm_freeze() - Freeze PM callback
896 * @dev: IUVC HVC terminal device
897 *
898 * Sever an established IUCV communication path and
899 * trigger a hang-up of the underlying HVC terminal.
900 */
901static int hvc_iucv_pm_freeze(struct device *dev)
902{
903 struct hvc_iucv_private *priv = dev_get_drvdata(dev);
904
905 local_bh_disable();
906 hvc_iucv_hangup(priv);
907 local_bh_enable();
908
909 return 0;
910}
911
912/**
913 * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
914 * @dev: IUVC HVC terminal device
915 *
916 * Wake up the HVC thread to trigger hang-up and respective
917 * HVC back-end notifier invocations.
918 */
919static int hvc_iucv_pm_restore_thaw(struct device *dev)
920{
921 hvc_kick();
922 return 0;
923}
924
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100925
926/* HVC operations */
Rusty Russell1dff3992009-11-28 12:20:26 +0530927static const struct hv_ops hvc_iucv_ops = {
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100928 .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,
933};
934
Hendrik Brueckner02591622009-06-16 10:30:45 +0200935/* Suspend / resume device operations */
Alexey Dobriyan47145212009-12-14 18:00:08 -0800936static const struct dev_pm_ops hvc_iucv_pm_ops = {
Hendrik Brueckner02591622009-06-16 10:30:45 +0200937 .freeze = hvc_iucv_pm_freeze,
938 .thaw = hvc_iucv_pm_restore_thaw,
939 .restore = hvc_iucv_pm_restore_thaw,
940};
941
942/* IUCV HVC device driver */
943static struct device_driver hvc_iucv_driver = {
944 .name = KMSG_COMPONENT,
945 .bus = &iucv_bus,
946 .pm = &hvc_iucv_pm_ops,
947};
948
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100949/**
950 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100951 * @id: hvc_iucv_table index
952 * @is_console: Flag if the instance is used as Linux console
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100953 *
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100954 * This function allocates a new hvc_iucv_private structure and stores
955 * the instance in hvc_iucv_table at index @id.
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100956 * Returns 0 on success; otherwise non-zero.
957 */
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100958static int __init hvc_iucv_alloc(int id, unsigned int is_console)
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100959{
960 struct hvc_iucv_private *priv;
961 char name[9];
962 int rc;
963
964 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
965 if (!priv)
966 return -ENOMEM;
967
968 spin_lock_init(&priv->lock);
969 INIT_LIST_HEAD(&priv->tty_outqueue);
970 INIT_LIST_HEAD(&priv->tty_inqueue);
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100971 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
972 init_waitqueue_head(&priv->sndbuf_waitq);
973
974 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
975 if (!priv->sndbuf) {
976 kfree(priv);
977 return -ENOMEM;
978 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100979
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +0100980 /* set console flag */
981 priv->is_console = is_console;
982
Hendrik Brueckner02591622009-06-16 10:30:45 +0200983 /* allocate hvc device */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100984 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
985 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100986 if (IS_ERR(priv->hvc)) {
987 rc = PTR_ERR(priv->hvc);
Hendrik Brueckner02591622009-06-16 10:30:45 +0200988 goto out_error_hvc;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100989 }
990
Hendrik Brueckner17e19f02009-01-09 12:14:59 +0100991 /* notify HVC thread instead of using polling */
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +0100992 priv->hvc->irq_requested = 1;
993
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100994 /* setup iucv related information */
Hendrik Brueckner2dc184c2009-01-09 12:14:57 +0100995 snprintf(name, 9, "lnxhvc%-2d", id);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +0100996 memcpy(priv->srv_name, name, 8);
997 ASCEBC(priv->srv_name, 8);
998
Hendrik Brueckner02591622009-06-16 10:30:45 +0200999 /* 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;
1004 }
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) {
Sebastian Ottc6304932009-09-11 10:28:38 +02001013 put_device(priv->dev);
Hendrik Brueckner02591622009-06-16 10:30:45 +02001014 goto out_error_dev;
1015 }
1016
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001017 hvc_iucv_table[id] = priv;
1018 return 0;
Hendrik Brueckner02591622009-06-16 10:30:45 +02001019
1020out_error_dev:
1021 hvc_remove(priv->hvc);
1022out_error_hvc:
1023 free_page((unsigned long) priv->sndbuf);
1024 kfree(priv);
1025
1026 return rc;
1027}
1028
1029/**
1030 * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1031 */
1032static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1033{
1034 hvc_remove(priv->hvc);
1035 device_unregister(priv->dev);
1036 free_page((unsigned long) priv->sndbuf);
1037 kfree(priv);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001038}
1039
1040/**
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001041 * 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
1043 */
1044static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1045{
1046 const char *nextdelim, *residual;
1047 size_t len;
1048
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;
1056 }
1057
1058 if (len == 0)
1059 return ERR_PTR(-EINVAL);
1060
1061 /* check for '\n' (if called from sysfs) */
1062 if (filter[len - 1] == '\n')
1063 len--;
1064
1065 if (len > 8)
1066 return ERR_PTR(-EINVAL);
1067
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;
1073}
1074
1075/**
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
1078 *
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.
1084 */
1085static int hvc_iucv_setup_filter(const char *val)
1086{
1087 const char *residual;
1088 int err;
1089 size_t size, count;
1090 void *array, *old_filter;
1091
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 */
1097 }
1098
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++;
1105 }
1106
1107 /* check if the specified list exceeds the filter limit */
1108 if (size > MAX_VMID_FILTER)
1109 return -ENOSPC;
1110
1111 array = kzalloc(size * 8, GFP_KERNEL);
1112 if (!array)
1113 return -ENOMEM;
1114
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;
1124 }
1125 count--;
1126 }
1127
1128out_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);
1135
1136 err = 0;
1137out_err:
1138 return err;
1139}
1140
1141/**
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
1145 *
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().
1150 */
1151static int param_set_vmidfilter(const char *val, struct kernel_param *kp)
1152{
1153 int rc;
1154
1155 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1156 return -ENODEV;
1157
1158 if (!val)
1159 return -EINVAL;
1160
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;
1167}
1168
1169/**
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
1174 *
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.
1177 */
1178static int param_get_vmidfilter(char *buffer, struct kernel_param *kp)
1179{
1180 int rc;
1181 size_t index, len;
1182 void *start, *end;
1183
1184 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1185 return -ENODEV;
1186
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++] = ',';
1196 }
1197 read_unlock_bh(&hvc_iucv_filter_lock);
1198 if (rc)
1199 buffer[--rc] = '\0'; /* replace last comma and update rc */
1200 return rc;
1201}
1202
1203#define param_check_vmidfilter(name, p) __param_check(name, p, void)
1204
1205/**
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01001206 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001207 */
1208static int __init hvc_iucv_init(void)
1209{
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +01001210 int rc;
1211 unsigned int i;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001212
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001213 if (!hvc_iucv_devices)
1214 return -ENODEV;
1215
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001216 if (!MACHINE_IS_VM) {
Hendrik Brueckner82f3a792009-03-26 15:23:54 +01001217 pr_notice("The z/VM IUCV HVC device driver cannot "
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +01001218 "be used without z/VM\n");
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001219 rc = -ENODEV;
1220 goto out_error;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001221 }
1222
Hendrik Brueckner82f3a792009-03-26 15:23:54 +01001223 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);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001226 rc = -EINVAL;
1227 goto out_error;
1228 }
1229
Hendrik Brueckner02591622009-06-16 10:30:45 +02001230 /* register IUCV HVC device driver */
1231 rc = driver_register(&hvc_iucv_driver);
1232 if (rc)
1233 goto out_error;
1234
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001235 /* 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;
1255 }
Hendrik Brueckner82f3a792009-03-26 15:23:54 +01001256 }
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001257
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) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +01001262 pr_err("Allocating memory failed with reason code=%d\n", 1);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001263 rc = -ENOMEM;
1264 goto out_error;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001265 }
1266
1267 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1268 hvc_iucv_buffer_cache);
1269 if (!hvc_iucv_mempool) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +01001270 pr_err("Allocating memory failed with reason code=%d\n", 2);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001271 kmem_cache_destroy(hvc_iucv_buffer_cache);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001272 rc = -ENOMEM;
1273 goto out_error;
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001274 }
1275
Hendrik Brueckner68c6b3d2009-01-09 12:15:00 +01001276 /* register the first terminal device as console
1277 * (must be done before allocating hvc terminal devices) */
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +01001278 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;
1283 }
Hendrik Brueckner68c6b3d2009-01-09 12:15:00 +01001284
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001285 /* allocate hvc_iucv_private structs */
1286 for (i = 0; i < hvc_iucv_devices; i++) {
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +01001287 rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001288 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +01001289 pr_err("Creating a new HVC terminal device "
Hendrik Brueckner17e19f02009-01-09 12:14:59 +01001290 "failed with error code=%d\n", rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001291 goto out_error_hvc;
1292 }
1293 }
1294
1295 /* register IUCV callback handler */
1296 rc = iucv_register(&hvc_iucv_handler, 0);
1297 if (rc) {
Hendrik Bruecknerc45ce4b2009-01-09 12:14:58 +01001298 pr_err("Registering IUCV handlers failed with error code=%d\n",
1299 rc);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001300 goto out_error_iucv;
1301 }
1302
1303 return 0;
1304
1305out_error_iucv:
1306 iucv_unregister(&hvc_iucv_handler, 0);
1307out_error_hvc:
1308 for (i = 0; i < hvc_iucv_devices; i++)
Hendrik Brueckner02591622009-06-16 10:30:45 +02001309 if (hvc_iucv_table[i])
1310 hvc_iucv_destroy(hvc_iucv_table[i]);
Hendrik Brueckner6c089fd2009-01-09 12:15:01 +01001311out_error_memory:
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001312 mempool_destroy(hvc_iucv_mempool);
1313 kmem_cache_destroy(hvc_iucv_buffer_cache);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001314out_error:
Hendrik Brueckner02591622009-06-16 10:30:45 +02001315 if (hvc_iucv_filter)
1316 kfree(hvc_iucv_filter);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001317 hvc_iucv_devices = 0; /* ensure that we do not provide any device */
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001318 return rc;
1319}
1320
1321/**
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001322 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
1323 * @val: Parameter value (numeric)
1324 */
1325static int __init hvc_iucv_config(char *val)
1326{
1327 return strict_strtoul(val, 10, &hvc_iucv_devices);
1328}
1329
1330
Hendrik Brueckner68c6b3d2009-01-09 12:15:00 +01001331device_initcall(hvc_iucv_init);
Hendrik Brueckner44a01d52008-12-25 13:38:57 +01001332__setup("hvc_iucv=", hvc_iucv_config);
Hendrik Brueckner431429f2009-03-26 15:23:55 +01001333core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);