Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 2 | /* |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 3 | * z/VM IUCV hypervisor console (HVC) device driver |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 4 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 5 | * This HVC device driver provides terminal access using |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 6 | * z/VM IUCV communication paths. |
| 7 | * |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 8 | * Copyright IBM Corp. 2008, 2013 |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 9 | * |
| 10 | * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> |
| 11 | */ |
| 12 | #define KMSG_COMPONENT "hvc_iucv" |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 13 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 14 | |
| 15 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 17 | #include <asm/ebcdic.h> |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 19 | #include <linux/delay.h> |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 20 | #include <linux/device.h> |
Hendrik Brueckner | 68c6b3d | 2009-01-09 12:15:00 +0100 | [diff] [blame] | 21 | #include <linux/init.h> |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 22 | #include <linux/mempool.h> |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 23 | #include <linux/moduleparam.h> |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 24 | #include <linux/tty.h> |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 25 | #include <linux/wait.h> |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 26 | #include <net/iucv/iucv.h> |
| 27 | |
| 28 | #include "hvc_console.h" |
| 29 | |
| 30 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 31 | /* General device driver settings */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 32 | #define HVC_IUCV_MAGIC 0xc9e4c3e5 |
| 33 | #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS |
| 34 | #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4) |
| 35 | |
| 36 | /* IUCV TTY message */ |
| 37 | #define MSG_VERSION 0x02 /* Message version */ |
| 38 | #define MSG_TYPE_ERROR 0x01 /* Error message */ |
| 39 | #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */ |
| 40 | #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */ |
| 41 | #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */ |
| 42 | #define MSG_TYPE_DATA 0x10 /* Terminal data */ |
| 43 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 44 | struct iucv_tty_msg { |
| 45 | u8 version; /* Message version */ |
| 46 | u8 type; /* Message type */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 47 | #define MSG_MAX_DATALEN ((u16)(~0)) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 48 | u16 datalen; /* Payload length */ |
| 49 | u8 data[]; /* Payload buffer */ |
| 50 | } __attribute__((packed)); |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 51 | #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data)) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 52 | |
| 53 | enum iucv_state_t { |
| 54 | IUCV_DISCONN = 0, |
| 55 | IUCV_CONNECTED = 1, |
| 56 | IUCV_SEVERED = 2, |
| 57 | }; |
| 58 | |
| 59 | enum tty_state_t { |
| 60 | TTY_CLOSED = 0, |
| 61 | TTY_OPENED = 1, |
| 62 | }; |
| 63 | |
| 64 | struct hvc_iucv_private { |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 65 | struct hvc_struct *hvc; /* HVC struct reference */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 66 | u8 srv_name[8]; /* IUCV service name (ebcdic) */ |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 67 | unsigned char is_console; /* Linux console usage flag */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 68 | enum iucv_state_t iucv_state; /* IUCV connection status */ |
| 69 | enum tty_state_t tty_state; /* TTY status */ |
| 70 | struct iucv_path *path; /* IUCV path pointer */ |
| 71 | spinlock_t lock; /* hvc_iucv_private lock */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 72 | #define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */ |
| 73 | void *sndbuf; /* send buffer */ |
| 74 | size_t sndbuf_len; /* length of send buffer */ |
| 75 | #define QUEUE_SNDBUF_DELAY (HZ / 25) |
| 76 | struct delayed_work sndbuf_work; /* work: send iucv msg(s) */ |
| 77 | wait_queue_head_t sndbuf_waitq; /* wait for send completion */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 78 | struct list_head tty_outqueue; /* outgoing IUCV messages */ |
| 79 | struct list_head tty_inqueue; /* incoming IUCV messages */ |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 80 | struct device *dev; /* device structure */ |
Hendrik Brueckner | f1206ba | 2014-01-17 14:42:00 +0100 | [diff] [blame] | 81 | u8 info_path[16]; /* IUCV path info (dev attr) */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct iucv_tty_buffer { |
| 85 | struct list_head list; /* list pointer */ |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 86 | struct iucv_message msg; /* store an IUCV message */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 87 | size_t offset; /* data buffer offset */ |
| 88 | struct iucv_tty_msg *mbuf; /* buffer to store input/output data */ |
| 89 | }; |
| 90 | |
| 91 | /* IUCV callback handler */ |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 92 | static int hvc_iucv_path_pending(struct iucv_path *, u8 *, u8 *); |
| 93 | static void hvc_iucv_path_severed(struct iucv_path *, u8 *); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 94 | static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *); |
| 95 | static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *); |
| 96 | |
| 97 | |
Hendrik Brueckner | 2dc184c | 2009-01-09 12:14:57 +0100 | [diff] [blame] | 98 | /* Kernel module parameter: use one terminal device as default */ |
| 99 | static unsigned long hvc_iucv_devices = 1; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 100 | |
| 101 | /* Array of allocated hvc iucv tty lines... */ |
| 102 | static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES]; |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 103 | #define IUCV_HVC_CON_IDX (0) |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 104 | /* List of z/VM user ID filter entries (struct iucv_vmid_filter) */ |
| 105 | #define MAX_VMID_FILTER (500) |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 106 | #define FILTER_WILDCARD_CHAR '*' |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 107 | static size_t hvc_iucv_filter_size; |
| 108 | static void *hvc_iucv_filter; |
| 109 | static const char *hvc_iucv_filter_string; |
| 110 | static DEFINE_RWLOCK(hvc_iucv_filter_lock); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 111 | |
| 112 | /* Kmem cache and mempool for iucv_tty_buffer elements */ |
| 113 | static struct kmem_cache *hvc_iucv_buffer_cache; |
| 114 | static mempool_t *hvc_iucv_mempool; |
| 115 | |
| 116 | /* IUCV handler callback functions */ |
| 117 | static struct iucv_handler hvc_iucv_handler = { |
| 118 | .path_pending = hvc_iucv_path_pending, |
| 119 | .path_severed = hvc_iucv_path_severed, |
| 120 | .message_complete = hvc_iucv_msg_complete, |
| 121 | .message_pending = hvc_iucv_msg_pending, |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance. |
| 127 | * @num: The HVC virtual terminal number (vtermno) |
| 128 | * |
| 129 | * This function returns the struct hvc_iucv_private instance that corresponds |
| 130 | * to the HVC virtual terminal number specified as parameter @num. |
| 131 | */ |
Hendrik Brueckner | 2ee13c6 | 2014-01-17 16:07:46 +0100 | [diff] [blame] | 132 | static struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 133 | { |
| 134 | if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices)) |
| 135 | return NULL; |
| 136 | return hvc_iucv_table[num - HVC_IUCV_MAGIC]; |
| 137 | } |
| 138 | |
| 139 | /** |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 140 | * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 141 | * @size: Size of the internal buffer used to store data. |
| 142 | * @flags: Memory allocation flags passed to mempool. |
| 143 | * |
| 144 | * This function allocates a new struct iucv_tty_buffer element and, optionally, |
| 145 | * allocates an internal data buffer with the specified size @size. |
Hendrik Brueckner | 91a970d | 2010-03-08 12:25:15 +0100 | [diff] [blame] | 146 | * The internal data buffer is always allocated with GFP_DMA which is |
| 147 | * required for receiving and sending data with IUCV. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 148 | * Note: The total message size arises from the internal buffer size and the |
| 149 | * members of the iucv_tty_msg structure. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 150 | * The function returns NULL if memory allocation has failed. |
| 151 | */ |
| 152 | static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags) |
| 153 | { |
| 154 | struct iucv_tty_buffer *bufp; |
| 155 | |
| 156 | bufp = mempool_alloc(hvc_iucv_mempool, flags); |
| 157 | if (!bufp) |
| 158 | return NULL; |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 159 | memset(bufp, 0, sizeof(*bufp)); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 160 | |
| 161 | if (size > 0) { |
| 162 | bufp->msg.length = MSG_SIZE(size); |
Hendrik Brueckner | 91a970d | 2010-03-08 12:25:15 +0100 | [diff] [blame] | 163 | bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 164 | if (!bufp->mbuf) { |
| 165 | mempool_free(bufp, hvc_iucv_mempool); |
| 166 | return NULL; |
| 167 | } |
| 168 | bufp->mbuf->version = MSG_VERSION; |
| 169 | bufp->mbuf->type = MSG_TYPE_DATA; |
| 170 | bufp->mbuf->datalen = (u16) size; |
| 171 | } |
| 172 | return bufp; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * destroy_tty_buffer() - destroy struct iucv_tty_buffer element. |
| 177 | * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 178 | */ |
| 179 | static void destroy_tty_buffer(struct iucv_tty_buffer *bufp) |
| 180 | { |
| 181 | kfree(bufp->mbuf); |
| 182 | mempool_free(bufp, hvc_iucv_mempool); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element. |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 187 | * @list: List containing struct iucv_tty_buffer elements. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 188 | */ |
| 189 | static void destroy_tty_buffer_list(struct list_head *list) |
| 190 | { |
| 191 | struct iucv_tty_buffer *ent, *next; |
| 192 | |
| 193 | list_for_each_entry_safe(ent, next, list, list) { |
| 194 | list_del(&ent->list); |
| 195 | destroy_tty_buffer(ent); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 200 | * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer. |
| 201 | * @priv: Pointer to struct hvc_iucv_private |
| 202 | * @buf: HVC buffer for writing received terminal data. |
| 203 | * @count: HVC buffer size. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 204 | * @has_more_data: Pointer to an int variable. |
| 205 | * |
| 206 | * The function picks up pending messages from the input queue and receives |
| 207 | * the message data that is then written to the specified buffer @buf. |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 208 | * If the buffer size @count is less than the data message size, the |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 209 | * message is kept on the input queue and @has_more_data is set to 1. |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 210 | * If all message data has been written, the message is removed from |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 211 | * the input queue. |
| 212 | * |
| 213 | * The function returns the number of bytes written to the terminal, zero if |
| 214 | * there are no pending data messages available or if there is no established |
| 215 | * IUCV path. |
| 216 | * If the IUCV path has been severed, then -EPIPE is returned to cause a |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 217 | * hang up (that is issued by the HVC layer). |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 218 | */ |
| 219 | static int hvc_iucv_write(struct hvc_iucv_private *priv, |
| 220 | char *buf, int count, int *has_more_data) |
| 221 | { |
| 222 | struct iucv_tty_buffer *rb; |
| 223 | int written; |
| 224 | int rc; |
| 225 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 226 | /* immediately return if there is no IUCV connection */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 227 | if (priv->iucv_state == IUCV_DISCONN) |
| 228 | return 0; |
| 229 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 230 | /* if the IUCV path has been severed, return -EPIPE to inform the |
| 231 | * HVC layer to hang up the tty device. */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 232 | if (priv->iucv_state == IUCV_SEVERED) |
| 233 | return -EPIPE; |
| 234 | |
| 235 | /* check if there are pending messages */ |
| 236 | if (list_empty(&priv->tty_inqueue)) |
| 237 | return 0; |
| 238 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 239 | /* receive an iucv message and flip data to the tty (ldisc) */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 240 | rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list); |
| 241 | |
| 242 | written = 0; |
| 243 | if (!rb->mbuf) { /* message not yet received ... */ |
| 244 | /* allocate mem to store msg data; if no memory is available |
| 245 | * then leave the buffer on the list and re-try later */ |
Hendrik Brueckner | 91a970d | 2010-03-08 12:25:15 +0100 | [diff] [blame] | 246 | rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 247 | if (!rb->mbuf) |
| 248 | return -ENOMEM; |
| 249 | |
| 250 | rc = __iucv_message_receive(priv->path, &rb->msg, 0, |
| 251 | rb->mbuf, rb->msg.length, NULL); |
| 252 | switch (rc) { |
| 253 | case 0: /* Successful */ |
| 254 | break; |
| 255 | case 2: /* No message found */ |
| 256 | case 9: /* Message purged */ |
| 257 | break; |
| 258 | default: |
| 259 | written = -EIO; |
| 260 | } |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 261 | /* remove buffer if an error has occurred or received data |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 262 | * is not correct */ |
| 263 | if (rc || (rb->mbuf->version != MSG_VERSION) || |
| 264 | (rb->msg.length != MSG_SIZE(rb->mbuf->datalen))) |
| 265 | goto out_remove_buffer; |
| 266 | } |
| 267 | |
| 268 | switch (rb->mbuf->type) { |
| 269 | case MSG_TYPE_DATA: |
| 270 | written = min_t(int, rb->mbuf->datalen - rb->offset, count); |
| 271 | memcpy(buf, rb->mbuf->data + rb->offset, written); |
| 272 | if (written < (rb->mbuf->datalen - rb->offset)) { |
| 273 | rb->offset += written; |
| 274 | *has_more_data = 1; |
| 275 | goto out_written; |
| 276 | } |
| 277 | break; |
| 278 | |
| 279 | case MSG_TYPE_WINSIZE: |
| 280 | if (rb->mbuf->datalen != sizeof(struct winsize)) |
| 281 | break; |
Hendrik Brueckner | 254be49 | 2009-08-27 01:45:39 +0000 | [diff] [blame] | 282 | /* The caller must ensure that the hvc is locked, which |
| 283 | * is the case when called from hvc_iucv_get_chars() */ |
| 284 | __hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data)); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | case MSG_TYPE_ERROR: /* ignored ... */ |
| 288 | case MSG_TYPE_TERMENV: /* ignored ... */ |
| 289 | case MSG_TYPE_TERMIOS: /* ignored ... */ |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | out_remove_buffer: |
| 294 | list_del(&rb->list); |
| 295 | destroy_tty_buffer(rb); |
| 296 | *has_more_data = !list_empty(&priv->tty_inqueue); |
| 297 | |
| 298 | out_written: |
| 299 | return written; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * hvc_iucv_get_chars() - HVC get_chars operation. |
| 304 | * @vtermno: HVC virtual terminal number. |
| 305 | * @buf: Pointer to a buffer to store data |
| 306 | * @count: Size of buffer available for writing |
| 307 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 308 | * The HVC thread calls this method to read characters from the back-end. |
| 309 | * If an IUCV communication path has been established, pending IUCV messages |
| 310 | * are received and data is copied into buffer @buf up to @count bytes. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 311 | * |
| 312 | * Locking: The routine gets called under an irqsave() spinlock; and |
| 313 | * the routine locks the struct hvc_iucv_private->lock to call |
| 314 | * helper functions. |
| 315 | */ |
| 316 | static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count) |
| 317 | { |
| 318 | struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno); |
| 319 | int written; |
| 320 | int has_more_data; |
| 321 | |
| 322 | if (count <= 0) |
| 323 | return 0; |
| 324 | |
| 325 | if (!priv) |
| 326 | return -ENODEV; |
| 327 | |
| 328 | spin_lock(&priv->lock); |
| 329 | has_more_data = 0; |
| 330 | written = hvc_iucv_write(priv, buf, count, &has_more_data); |
| 331 | spin_unlock(&priv->lock); |
| 332 | |
| 333 | /* if there are still messages on the queue... schedule another run */ |
| 334 | if (has_more_data) |
| 335 | hvc_kick(); |
| 336 | |
| 337 | return written; |
| 338 | } |
| 339 | |
| 340 | /** |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 341 | * hvc_iucv_queue() - Buffer terminal data for sending. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 342 | * @priv: Pointer to struct hvc_iucv_private instance. |
| 343 | * @buf: Buffer containing data to send. |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 344 | * @count: Size of buffer and amount of data to send. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 345 | * |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 346 | * The function queues data for sending. To actually send the buffered data, |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 347 | * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY). |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 348 | * The function returns the number of data bytes that has been buffered. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 349 | * |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 350 | * If the device is not connected, data is ignored and the function returns |
| 351 | * @count. |
| 352 | * If the buffer is full, the function returns 0. |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 353 | * If an existing IUCV communicaton path has been severed, -EPIPE is returned |
| 354 | * (that can be passed to HVC layer to cause a tty hangup). |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 355 | */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 356 | static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf, |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 357 | int count) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 358 | { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 359 | size_t len; |
| 360 | |
| 361 | if (priv->iucv_state == IUCV_DISCONN) |
| 362 | return count; /* ignore data */ |
| 363 | |
| 364 | if (priv->iucv_state == IUCV_SEVERED) |
| 365 | return -EPIPE; |
| 366 | |
| 367 | len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len); |
| 368 | if (!len) |
| 369 | return 0; |
| 370 | |
| 371 | memcpy(priv->sndbuf + priv->sndbuf_len, buf, len); |
| 372 | priv->sndbuf_len += len; |
| 373 | |
| 374 | if (priv->iucv_state == IUCV_CONNECTED) |
| 375 | schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY); |
| 376 | |
| 377 | return len; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * hvc_iucv_send() - Send an IUCV message containing terminal data. |
| 382 | * @priv: Pointer to struct hvc_iucv_private instance. |
| 383 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 384 | * If an IUCV communication path has been established, the buffered output data |
| 385 | * is sent via an IUCV message and the number of bytes sent is returned. |
| 386 | * Returns 0 if there is no established IUCV communication path or |
| 387 | * -EPIPE if an existing IUCV communicaton path has been severed. |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 388 | */ |
| 389 | static int hvc_iucv_send(struct hvc_iucv_private *priv) |
| 390 | { |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 391 | struct iucv_tty_buffer *sb; |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 392 | int rc, len; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 393 | |
| 394 | if (priv->iucv_state == IUCV_SEVERED) |
| 395 | return -EPIPE; |
| 396 | |
| 397 | if (priv->iucv_state == IUCV_DISCONN) |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 398 | return -EIO; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 399 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 400 | if (!priv->sndbuf_len) |
| 401 | return 0; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 402 | |
| 403 | /* allocate internal buffer to store msg data and also compute total |
| 404 | * message length */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 405 | sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 406 | if (!sb) |
| 407 | return -ENOMEM; |
| 408 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 409 | memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len); |
| 410 | sb->mbuf->datalen = (u16) priv->sndbuf_len; |
| 411 | sb->msg.length = MSG_SIZE(sb->mbuf->datalen); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 412 | |
| 413 | list_add_tail(&sb->list, &priv->tty_outqueue); |
| 414 | |
| 415 | rc = __iucv_message_send(priv->path, &sb->msg, 0, 0, |
| 416 | (void *) sb->mbuf, sb->msg.length); |
| 417 | if (rc) { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 418 | /* drop the message here; however we might want to handle |
| 419 | * 0x03 (msg limit reached) by trying again... */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 420 | list_del(&sb->list); |
| 421 | destroy_tty_buffer(sb); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 422 | } |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 423 | len = priv->sndbuf_len; |
| 424 | priv->sndbuf_len = 0; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 425 | |
| 426 | return len; |
| 427 | } |
| 428 | |
| 429 | /** |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 430 | * hvc_iucv_sndbuf_work() - Send buffered data over IUCV |
| 431 | * @work: Work structure. |
| 432 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 433 | * This work queue function sends buffered output data over IUCV and, |
| 434 | * if not all buffered data could be sent, reschedules itself. |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 435 | */ |
| 436 | static void hvc_iucv_sndbuf_work(struct work_struct *work) |
| 437 | { |
| 438 | struct hvc_iucv_private *priv; |
| 439 | |
| 440 | priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work); |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 441 | if (!priv) |
| 442 | return; |
| 443 | |
| 444 | spin_lock_bh(&priv->lock); |
| 445 | hvc_iucv_send(priv); |
| 446 | spin_unlock_bh(&priv->lock); |
| 447 | } |
| 448 | |
| 449 | /** |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 450 | * hvc_iucv_put_chars() - HVC put_chars operation. |
| 451 | * @vtermno: HVC virtual terminal number. |
| 452 | * @buf: Pointer to an buffer to read data from |
| 453 | * @count: Size of buffer available for reading |
| 454 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 455 | * The HVC thread calls this method to write characters to the back-end. |
| 456 | * The function calls hvc_iucv_queue() to queue terminal data for sending. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 457 | * |
| 458 | * Locking: The method gets called under an irqsave() spinlock; and |
| 459 | * locks struct hvc_iucv_private->lock. |
| 460 | */ |
| 461 | static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count) |
| 462 | { |
| 463 | struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno); |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 464 | int queued; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 465 | |
| 466 | if (count <= 0) |
| 467 | return 0; |
| 468 | |
| 469 | if (!priv) |
| 470 | return -ENODEV; |
| 471 | |
| 472 | spin_lock(&priv->lock); |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 473 | queued = hvc_iucv_queue(priv, buf, count); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 474 | spin_unlock(&priv->lock); |
| 475 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 476 | return queued; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time. |
| 481 | * @hp: Pointer to the HVC device (struct hvc_struct) |
| 482 | * @id: Additional data (originally passed to hvc_alloc): the index of an struct |
| 483 | * hvc_iucv_private instance. |
| 484 | * |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 485 | * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 486 | * instance that is derived from @id. Always returns 0. |
| 487 | * |
| 488 | * Locking: struct hvc_iucv_private->lock, spin_lock_bh |
| 489 | */ |
| 490 | static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id) |
| 491 | { |
| 492 | struct hvc_iucv_private *priv; |
| 493 | |
| 494 | priv = hvc_iucv_get_private(id); |
| 495 | if (!priv) |
| 496 | return 0; |
| 497 | |
| 498 | spin_lock_bh(&priv->lock); |
| 499 | priv->tty_state = TTY_OPENED; |
| 500 | spin_unlock_bh(&priv->lock); |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | /** |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 506 | * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 507 | * @priv: Pointer to the struct hvc_iucv_private instance. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 508 | */ |
| 509 | static void hvc_iucv_cleanup(struct hvc_iucv_private *priv) |
| 510 | { |
| 511 | destroy_tty_buffer_list(&priv->tty_outqueue); |
| 512 | destroy_tty_buffer_list(&priv->tty_inqueue); |
| 513 | |
| 514 | priv->tty_state = TTY_CLOSED; |
| 515 | priv->iucv_state = IUCV_DISCONN; |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 516 | |
| 517 | priv->sndbuf_len = 0; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * tty_outqueue_empty() - Test if the tty outq is empty |
| 522 | * @priv: Pointer to struct hvc_iucv_private instance. |
| 523 | */ |
| 524 | static inline int tty_outqueue_empty(struct hvc_iucv_private *priv) |
| 525 | { |
| 526 | int rc; |
| 527 | |
| 528 | spin_lock_bh(&priv->lock); |
| 529 | rc = list_empty(&priv->tty_outqueue); |
| 530 | spin_unlock_bh(&priv->lock); |
| 531 | |
| 532 | return rc; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * flush_sndbuf_sync() - Flush send buffer and wait for completion |
| 537 | * @priv: Pointer to struct hvc_iucv_private instance. |
| 538 | * |
| 539 | * The routine cancels a pending sndbuf work, calls hvc_iucv_send() |
| 540 | * to flush any buffered terminal output data and waits for completion. |
| 541 | */ |
| 542 | static void flush_sndbuf_sync(struct hvc_iucv_private *priv) |
| 543 | { |
| 544 | int sync_wait; |
| 545 | |
| 546 | cancel_delayed_work_sync(&priv->sndbuf_work); |
| 547 | |
| 548 | spin_lock_bh(&priv->lock); |
| 549 | hvc_iucv_send(priv); /* force sending buffered data */ |
| 550 | sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */ |
| 551 | spin_unlock_bh(&priv->lock); |
| 552 | |
| 553 | if (sync_wait) |
| 554 | wait_event_timeout(priv->sndbuf_waitq, |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 555 | tty_outqueue_empty(priv), HZ/10); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up |
| 560 | * @priv: Pointer to hvc_iucv_private structure |
| 561 | * |
| 562 | * This routine severs an existing IUCV communication path and hangs |
| 563 | * up the underlying HVC terminal device. |
| 564 | * The hang-up occurs only if an IUCV communication path is established; |
| 565 | * otherwise there is no need to hang up the terminal device. |
| 566 | * |
| 567 | * The IUCV HVC hang-up is separated into two steps: |
| 568 | * 1. After the IUCV path has been severed, the iucv_state is set to |
| 569 | * IUCV_SEVERED. |
| 570 | * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the |
| 571 | * IUCV_SEVERED state causes the tty hang-up in the HVC layer. |
| 572 | * |
| 573 | * If the tty has not yet been opened, clean up the hvc_iucv_private |
| 574 | * structure to allow re-connects. |
| 575 | * If the tty has been opened, let get_chars() return -EPIPE to signal |
| 576 | * the HVC layer to hang up the tty and, if so, wake up the HVC thread |
| 577 | * to call get_chars()... |
| 578 | * |
| 579 | * Special notes on hanging up a HVC terminal instantiated as console: |
| 580 | * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops) |
| 581 | * 2. do_tty_hangup() calls tty->ops->close() for console_filp |
| 582 | * => no hangup notifier is called by HVC (default) |
| 583 | * 2. hvc_close() returns because of tty_hung_up_p(filp) |
| 584 | * => no delete notifier is called! |
| 585 | * Finally, the back-end is not being notified, thus, the tty session is |
| 586 | * kept active (TTY_OPEN) to be ready for re-connects. |
| 587 | * |
| 588 | * Locking: spin_lock(&priv->lock) w/o disabling bh |
| 589 | */ |
| 590 | static void hvc_iucv_hangup(struct hvc_iucv_private *priv) |
| 591 | { |
| 592 | struct iucv_path *path; |
| 593 | |
| 594 | path = NULL; |
| 595 | spin_lock(&priv->lock); |
| 596 | if (priv->iucv_state == IUCV_CONNECTED) { |
| 597 | path = priv->path; |
| 598 | priv->path = NULL; |
| 599 | priv->iucv_state = IUCV_SEVERED; |
| 600 | if (priv->tty_state == TTY_CLOSED) |
| 601 | hvc_iucv_cleanup(priv); |
| 602 | else |
| 603 | /* console is special (see above) */ |
| 604 | if (priv->is_console) { |
| 605 | hvc_iucv_cleanup(priv); |
| 606 | priv->tty_state = TTY_OPENED; |
| 607 | } else |
| 608 | hvc_kick(); |
| 609 | } |
| 610 | spin_unlock(&priv->lock); |
| 611 | |
| 612 | /* finally sever path (outside of priv->lock due to lock ordering) */ |
| 613 | if (path) { |
| 614 | iucv_path_sever(path, NULL); |
| 615 | iucv_path_free(path); |
| 616 | } |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 620 | * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups. |
| 621 | * @hp: Pointer to the HVC device (struct hvc_struct) |
| 622 | * @id: Additional data (originally passed to hvc_alloc): |
| 623 | * the index of an struct hvc_iucv_private instance. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 624 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 625 | * This routine notifies the HVC back-end that a tty hangup (carrier loss, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 626 | * virtual or otherwise) has occurred. |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 627 | * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup()) |
| 628 | * to keep an existing IUCV communication path established. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 629 | * (Background: vhangup() is called from user space (by getty or login) to |
| 630 | * disable writing to the tty by other applications). |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 631 | * If the tty has been opened and an established IUCV path has been severed |
| 632 | * (we caused the tty hangup), the function calls hvc_iucv_cleanup(). |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 633 | * |
| 634 | * Locking: struct hvc_iucv_private->lock |
| 635 | */ |
| 636 | static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id) |
| 637 | { |
| 638 | struct hvc_iucv_private *priv; |
| 639 | |
| 640 | priv = hvc_iucv_get_private(id); |
| 641 | if (!priv) |
| 642 | return; |
| 643 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 644 | flush_sndbuf_sync(priv); |
| 645 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 646 | spin_lock_bh(&priv->lock); |
| 647 | /* NOTE: If the hangup was scheduled by ourself (from the iucv |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 648 | * path_servered callback [IUCV_SEVERED]), we have to clean up |
| 649 | * our structure and to set state to TTY_CLOSED. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 650 | * If the tty was hung up otherwise (e.g. vhangup()), then we |
| 651 | * ignore this hangup and keep an established IUCV path open... |
| 652 | * (...the reason is that we are not able to connect back to the |
| 653 | * client if we disconnect on hang up) */ |
| 654 | priv->tty_state = TTY_CLOSED; |
| 655 | |
| 656 | if (priv->iucv_state == IUCV_SEVERED) |
| 657 | hvc_iucv_cleanup(priv); |
| 658 | spin_unlock_bh(&priv->lock); |
| 659 | } |
| 660 | |
| 661 | /** |
Hendrik Brueckner | 74b3b4c | 2013-07-02 17:07:15 +0200 | [diff] [blame] | 662 | * hvc_iucv_dtr_rts() - HVC notifier for handling DTR/RTS |
| 663 | * @hp: Pointer the HVC device (struct hvc_struct) |
| 664 | * @raise: Non-zero to raise or zero to lower DTR/RTS lines |
| 665 | * |
| 666 | * This routine notifies the HVC back-end to raise or lower DTR/RTS |
| 667 | * lines. Raising DTR/RTS is ignored. Lowering DTR/RTS indicates to |
| 668 | * drop the IUCV connection (similar to hang up the modem). |
| 669 | */ |
| 670 | static void hvc_iucv_dtr_rts(struct hvc_struct *hp, int raise) |
| 671 | { |
| 672 | struct hvc_iucv_private *priv; |
| 673 | struct iucv_path *path; |
| 674 | |
| 675 | /* Raising the DTR/RTS is ignored as IUCV connections can be |
| 676 | * established at any times. |
| 677 | */ |
| 678 | if (raise) |
| 679 | return; |
| 680 | |
| 681 | priv = hvc_iucv_get_private(hp->vtermno); |
| 682 | if (!priv) |
| 683 | return; |
| 684 | |
| 685 | /* Lowering the DTR/RTS lines disconnects an established IUCV |
| 686 | * connection. |
| 687 | */ |
| 688 | flush_sndbuf_sync(priv); |
| 689 | |
| 690 | spin_lock_bh(&priv->lock); |
| 691 | path = priv->path; /* save reference to IUCV path */ |
| 692 | priv->path = NULL; |
| 693 | priv->iucv_state = IUCV_DISCONN; |
| 694 | spin_unlock_bh(&priv->lock); |
| 695 | |
| 696 | /* Sever IUCV path outside of priv->lock due to lock ordering of: |
| 697 | * priv->lock <--> iucv_table_lock */ |
| 698 | if (path) { |
| 699 | iucv_path_sever(path, NULL); |
| 700 | iucv_path_free(path); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /** |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 705 | * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time. |
| 706 | * @hp: Pointer to the HVC device (struct hvc_struct) |
| 707 | * @id: Additional data (originally passed to hvc_alloc): |
| 708 | * the index of an struct hvc_iucv_private instance. |
| 709 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 710 | * This routine notifies the HVC back-end that the last tty device fd has been |
Hendrik Brueckner | 74b3b4c | 2013-07-02 17:07:15 +0200 | [diff] [blame] | 711 | * closed. The function cleans up tty resources. The clean-up of the IUCV |
| 712 | * connection is done in hvc_iucv_dtr_rts() and depends on the HUPCL termios |
| 713 | * control setting. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 714 | * |
| 715 | * Locking: struct hvc_iucv_private->lock |
| 716 | */ |
| 717 | static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id) |
| 718 | { |
| 719 | struct hvc_iucv_private *priv; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 720 | |
| 721 | priv = hvc_iucv_get_private(id); |
| 722 | if (!priv) |
| 723 | return; |
| 724 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 725 | flush_sndbuf_sync(priv); |
| 726 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 727 | spin_lock_bh(&priv->lock); |
Hendrik Brueckner | 74b3b4c | 2013-07-02 17:07:15 +0200 | [diff] [blame] | 728 | destroy_tty_buffer_list(&priv->tty_outqueue); |
| 729 | destroy_tty_buffer_list(&priv->tty_inqueue); |
| 730 | priv->tty_state = TTY_CLOSED; |
| 731 | priv->sndbuf_len = 0; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 732 | spin_unlock_bh(&priv->lock); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /** |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 736 | * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID |
| 737 | * @ipvmid: Originating z/VM user ID (right padded with blanks) |
| 738 | * |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 739 | * Returns 0 if the z/VM user ID that is specified with @ipvmid is permitted to |
| 740 | * connect, otherwise non-zero. |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 741 | */ |
| 742 | static int hvc_iucv_filter_connreq(u8 ipvmid[8]) |
| 743 | { |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 744 | const char *wildcard, *filter_entry; |
| 745 | size_t i, len; |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 746 | |
| 747 | /* Note: default policy is ACCEPT if no filter is set */ |
| 748 | if (!hvc_iucv_filter_size) |
| 749 | return 0; |
| 750 | |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 751 | for (i = 0; i < hvc_iucv_filter_size; i++) { |
| 752 | filter_entry = hvc_iucv_filter + (8 * i); |
| 753 | |
| 754 | /* If a filter entry contains the filter wildcard character, |
| 755 | * reduce the length to match the leading portion of the user |
| 756 | * ID only (wildcard match). Characters following the wildcard |
| 757 | * are ignored. |
| 758 | */ |
| 759 | wildcard = strnchr(filter_entry, 8, FILTER_WILDCARD_CHAR); |
| 760 | len = (wildcard) ? wildcard - filter_entry : 8; |
| 761 | if (0 == memcmp(ipvmid, filter_entry, len)) |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 762 | return 0; |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 763 | } |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 764 | return 1; |
| 765 | } |
| 766 | |
| 767 | /** |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 768 | * hvc_iucv_path_pending() - IUCV handler to process a connection request. |
| 769 | * @path: Pending path (struct iucv_path) |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 770 | * @ipvmid: z/VM system identifier of originator |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 771 | * @ipuser: User specified data for this path |
| 772 | * (AF_IUCV: port/service name and originator port) |
| 773 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 774 | * The function uses the @ipuser data to determine if the pending path belongs |
| 775 | * to a terminal managed by this device driver. |
| 776 | * If the path belongs to this driver, ensure that the terminal is not accessed |
| 777 | * multiple times (only one connection to a terminal is allowed). |
| 778 | * If the terminal is not yet connected, the pending path is accepted and is |
| 779 | * associated to the appropriate struct hvc_iucv_private instance. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 780 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 781 | * Returns 0 if @path belongs to a terminal managed by the this device driver; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 782 | * otherwise returns -ENODEV in order to dispatch this path to other handlers. |
| 783 | * |
| 784 | * Locking: struct hvc_iucv_private->lock |
| 785 | */ |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 786 | static int hvc_iucv_path_pending(struct iucv_path *path, u8 *ipvmid, |
| 787 | u8 *ipuser) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 788 | { |
Hendrik Brueckner | 4f59221 | 2014-01-17 16:11:07 +0100 | [diff] [blame] | 789 | struct hvc_iucv_private *priv, *tmp; |
| 790 | u8 wildcard[9] = "lnxhvc "; |
| 791 | int i, rc, find_unused; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 792 | u8 nuser_data[16]; |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 793 | u8 vm_user_id[9]; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 794 | |
Hendrik Brueckner | 4f59221 | 2014-01-17 16:11:07 +0100 | [diff] [blame] | 795 | ASCEBC(wildcard, sizeof(wildcard)); |
| 796 | find_unused = !memcmp(wildcard, ipuser, 8); |
| 797 | |
| 798 | /* First, check if the pending path request is managed by this |
| 799 | * IUCV handler: |
| 800 | * - find a disconnected device if ipuser contains the wildcard |
| 801 | * - find the device that matches the terminal ID in ipuser |
| 802 | */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 803 | priv = NULL; |
Hendrik Brueckner | 4f59221 | 2014-01-17 16:11:07 +0100 | [diff] [blame] | 804 | for (i = 0; i < hvc_iucv_devices; i++) { |
| 805 | tmp = hvc_iucv_table[i]; |
| 806 | if (!tmp) |
| 807 | continue; |
| 808 | |
| 809 | if (find_unused) { |
| 810 | spin_lock(&tmp->lock); |
| 811 | if (tmp->iucv_state == IUCV_DISCONN) |
| 812 | priv = tmp; |
| 813 | spin_unlock(&tmp->lock); |
| 814 | |
| 815 | } else if (!memcmp(tmp->srv_name, ipuser, 8)) |
| 816 | priv = tmp; |
| 817 | if (priv) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 818 | break; |
Hendrik Brueckner | 4f59221 | 2014-01-17 16:11:07 +0100 | [diff] [blame] | 819 | } |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 820 | if (!priv) |
| 821 | return -ENODEV; |
| 822 | |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 823 | /* Enforce that ipvmid is allowed to connect to us */ |
| 824 | read_lock(&hvc_iucv_filter_lock); |
| 825 | rc = hvc_iucv_filter_connreq(ipvmid); |
| 826 | read_unlock(&hvc_iucv_filter_lock); |
| 827 | if (rc) { |
| 828 | iucv_path_sever(path, ipuser); |
| 829 | iucv_path_free(path); |
| 830 | memcpy(vm_user_id, ipvmid, 8); |
| 831 | vm_user_id[8] = 0; |
| 832 | pr_info("A connection request from z/VM user ID %s " |
| 833 | "was refused\n", vm_user_id); |
| 834 | return 0; |
| 835 | } |
| 836 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 837 | spin_lock(&priv->lock); |
| 838 | |
| 839 | /* If the terminal is already connected or being severed, then sever |
| 840 | * this path to enforce that there is only ONE established communication |
| 841 | * path per terminal. */ |
| 842 | if (priv->iucv_state != IUCV_DISCONN) { |
| 843 | iucv_path_sever(path, ipuser); |
| 844 | iucv_path_free(path); |
| 845 | goto out_path_handled; |
| 846 | } |
| 847 | |
| 848 | /* accept path */ |
| 849 | memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */ |
| 850 | memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */ |
| 851 | path->msglim = 0xffff; /* IUCV MSGLIMIT */ |
| 852 | path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */ |
| 853 | rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv); |
| 854 | if (rc) { |
| 855 | iucv_path_sever(path, ipuser); |
| 856 | iucv_path_free(path); |
| 857 | goto out_path_handled; |
| 858 | } |
| 859 | priv->path = path; |
| 860 | priv->iucv_state = IUCV_CONNECTED; |
| 861 | |
Hendrik Brueckner | f1206ba | 2014-01-17 14:42:00 +0100 | [diff] [blame] | 862 | /* store path information */ |
| 863 | memcpy(priv->info_path, ipvmid, 8); |
| 864 | memcpy(priv->info_path + 8, ipuser + 8, 8); |
| 865 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 866 | /* flush buffered output data... */ |
| 867 | schedule_delayed_work(&priv->sndbuf_work, 5); |
| 868 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 869 | out_path_handled: |
| 870 | spin_unlock(&priv->lock); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * hvc_iucv_path_severed() - IUCV handler to process a path sever. |
| 876 | * @path: Pending path (struct iucv_path) |
| 877 | * @ipuser: User specified data for this path |
| 878 | * (AF_IUCV: port/service name and originator port) |
| 879 | * |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 880 | * This function calls the hvc_iucv_hangup() function for the |
| 881 | * respective IUCV HVC terminal. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 882 | * |
| 883 | * Locking: struct hvc_iucv_private->lock |
| 884 | */ |
Ursula Braun | 91e60eb | 2015-09-18 16:06:52 +0200 | [diff] [blame] | 885 | static void hvc_iucv_path_severed(struct iucv_path *path, u8 *ipuser) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 886 | { |
| 887 | struct hvc_iucv_private *priv = path->private; |
| 888 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 889 | hvc_iucv_hangup(priv); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /** |
| 893 | * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message. |
| 894 | * @path: Pending path (struct iucv_path) |
| 895 | * @msg: Pointer to the IUCV message |
| 896 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 897 | * The function puts an incoming message on the input queue for later |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 898 | * processing (by hvc_iucv_get_chars() / hvc_iucv_write()). |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 899 | * If the tty has not yet been opened, the message is rejected. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 900 | * |
| 901 | * Locking: struct hvc_iucv_private->lock |
| 902 | */ |
| 903 | static void hvc_iucv_msg_pending(struct iucv_path *path, |
| 904 | struct iucv_message *msg) |
| 905 | { |
| 906 | struct hvc_iucv_private *priv = path->private; |
| 907 | struct iucv_tty_buffer *rb; |
| 908 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 909 | /* reject messages that exceed max size of iucv_tty_msg->datalen */ |
| 910 | if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) { |
| 911 | iucv_message_reject(path, msg); |
| 912 | return; |
| 913 | } |
| 914 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 915 | spin_lock(&priv->lock); |
| 916 | |
| 917 | /* reject messages if tty has not yet been opened */ |
| 918 | if (priv->tty_state == TTY_CLOSED) { |
| 919 | iucv_message_reject(path, msg); |
| 920 | goto unlock_return; |
| 921 | } |
| 922 | |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 923 | /* allocate tty buffer to save iucv msg only */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 924 | rb = alloc_tty_buffer(0, GFP_ATOMIC); |
| 925 | if (!rb) { |
| 926 | iucv_message_reject(path, msg); |
| 927 | goto unlock_return; /* -ENOMEM */ |
| 928 | } |
| 929 | rb->msg = *msg; |
| 930 | |
| 931 | list_add_tail(&rb->list, &priv->tty_inqueue); |
| 932 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 933 | hvc_kick(); /* wake up hvc thread */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 934 | |
| 935 | unlock_return: |
| 936 | spin_unlock(&priv->lock); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * hvc_iucv_msg_complete() - IUCV handler to process message completion |
| 941 | * @path: Pending path (struct iucv_path) |
| 942 | * @msg: Pointer to the IUCV message |
| 943 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 944 | * The function is called upon completion of message delivery to remove the |
| 945 | * message from the outqueue. Additional delivery information can be found |
| 946 | * msg->audit: rejected messages (0x040000 (IPADRJCT)), and |
| 947 | * purged messages (0x010000 (IPADPGNR)). |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 948 | * |
| 949 | * Locking: struct hvc_iucv_private->lock |
| 950 | */ |
| 951 | static void hvc_iucv_msg_complete(struct iucv_path *path, |
| 952 | struct iucv_message *msg) |
| 953 | { |
| 954 | struct hvc_iucv_private *priv = path->private; |
| 955 | struct iucv_tty_buffer *ent, *next; |
| 956 | LIST_HEAD(list_remove); |
| 957 | |
| 958 | spin_lock(&priv->lock); |
| 959 | list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list) |
| 960 | if (ent->msg.id == msg->id) { |
| 961 | list_move(&ent->list, &list_remove); |
| 962 | break; |
| 963 | } |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 964 | wake_up(&priv->sndbuf_waitq); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 965 | spin_unlock(&priv->lock); |
| 966 | destroy_tty_buffer_list(&list_remove); |
| 967 | } |
| 968 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 969 | /** |
| 970 | * hvc_iucv_pm_freeze() - Freeze PM callback |
| 971 | * @dev: IUVC HVC terminal device |
| 972 | * |
| 973 | * Sever an established IUCV communication path and |
| 974 | * trigger a hang-up of the underlying HVC terminal. |
| 975 | */ |
| 976 | static int hvc_iucv_pm_freeze(struct device *dev) |
| 977 | { |
| 978 | struct hvc_iucv_private *priv = dev_get_drvdata(dev); |
| 979 | |
| 980 | local_bh_disable(); |
| 981 | hvc_iucv_hangup(priv); |
| 982 | local_bh_enable(); |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback |
| 989 | * @dev: IUVC HVC terminal device |
| 990 | * |
| 991 | * Wake up the HVC thread to trigger hang-up and respective |
| 992 | * HVC back-end notifier invocations. |
| 993 | */ |
| 994 | static int hvc_iucv_pm_restore_thaw(struct device *dev) |
| 995 | { |
| 996 | hvc_kick(); |
| 997 | return 0; |
| 998 | } |
| 999 | |
Hendrik Brueckner | f1206ba | 2014-01-17 14:42:00 +0100 | [diff] [blame] | 1000 | static ssize_t hvc_iucv_dev_termid_show(struct device *dev, |
| 1001 | struct device_attribute *attr, |
| 1002 | char *buf) |
| 1003 | { |
| 1004 | struct hvc_iucv_private *priv = dev_get_drvdata(dev); |
| 1005 | size_t len; |
| 1006 | |
| 1007 | len = sizeof(priv->srv_name); |
| 1008 | memcpy(buf, priv->srv_name, len); |
| 1009 | EBCASC(buf, len); |
| 1010 | buf[len++] = '\n'; |
| 1011 | return len; |
| 1012 | } |
| 1013 | |
| 1014 | static ssize_t hvc_iucv_dev_state_show(struct device *dev, |
| 1015 | struct device_attribute *attr, |
| 1016 | char *buf) |
| 1017 | { |
| 1018 | struct hvc_iucv_private *priv = dev_get_drvdata(dev); |
| 1019 | return sprintf(buf, "%u:%u\n", priv->iucv_state, priv->tty_state); |
| 1020 | } |
| 1021 | |
| 1022 | static ssize_t hvc_iucv_dev_peer_show(struct device *dev, |
| 1023 | struct device_attribute *attr, |
| 1024 | char *buf) |
| 1025 | { |
| 1026 | struct hvc_iucv_private *priv = dev_get_drvdata(dev); |
| 1027 | char vmid[9], ipuser[9]; |
| 1028 | |
| 1029 | memset(vmid, 0, sizeof(vmid)); |
| 1030 | memset(ipuser, 0, sizeof(ipuser)); |
| 1031 | |
| 1032 | spin_lock_bh(&priv->lock); |
| 1033 | if (priv->iucv_state == IUCV_CONNECTED) { |
| 1034 | memcpy(vmid, priv->info_path, 8); |
| 1035 | memcpy(ipuser, priv->info_path + 8, 8); |
| 1036 | } |
| 1037 | spin_unlock_bh(&priv->lock); |
| 1038 | EBCASC(ipuser, 8); |
| 1039 | |
| 1040 | return sprintf(buf, "%s:%s\n", vmid, ipuser); |
| 1041 | } |
| 1042 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1043 | |
| 1044 | /* HVC operations */ |
Rusty Russell | 1dff399 | 2009-11-28 12:20:26 +0530 | [diff] [blame] | 1045 | static const struct hv_ops hvc_iucv_ops = { |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1046 | .get_chars = hvc_iucv_get_chars, |
| 1047 | .put_chars = hvc_iucv_put_chars, |
| 1048 | .notifier_add = hvc_iucv_notifier_add, |
| 1049 | .notifier_del = hvc_iucv_notifier_del, |
| 1050 | .notifier_hangup = hvc_iucv_notifier_hangup, |
Hendrik Brueckner | 74b3b4c | 2013-07-02 17:07:15 +0200 | [diff] [blame] | 1051 | .dtr_rts = hvc_iucv_dtr_rts, |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1052 | }; |
| 1053 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1054 | /* Suspend / resume device operations */ |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1055 | static const struct dev_pm_ops hvc_iucv_pm_ops = { |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1056 | .freeze = hvc_iucv_pm_freeze, |
| 1057 | .thaw = hvc_iucv_pm_restore_thaw, |
| 1058 | .restore = hvc_iucv_pm_restore_thaw, |
| 1059 | }; |
| 1060 | |
| 1061 | /* IUCV HVC device driver */ |
| 1062 | static struct device_driver hvc_iucv_driver = { |
| 1063 | .name = KMSG_COMPONENT, |
| 1064 | .bus = &iucv_bus, |
| 1065 | .pm = &hvc_iucv_pm_ops, |
| 1066 | }; |
| 1067 | |
Hendrik Brueckner | f1206ba | 2014-01-17 14:42:00 +0100 | [diff] [blame] | 1068 | /* IUCV HVC device attributes */ |
| 1069 | static DEVICE_ATTR(termid, 0640, hvc_iucv_dev_termid_show, NULL); |
| 1070 | static DEVICE_ATTR(state, 0640, hvc_iucv_dev_state_show, NULL); |
| 1071 | static DEVICE_ATTR(peer, 0640, hvc_iucv_dev_peer_show, NULL); |
| 1072 | static struct attribute *hvc_iucv_dev_attrs[] = { |
| 1073 | &dev_attr_termid.attr, |
| 1074 | &dev_attr_state.attr, |
| 1075 | &dev_attr_peer.attr, |
| 1076 | NULL, |
| 1077 | }; |
| 1078 | static struct attribute_group hvc_iucv_dev_attr_group = { |
| 1079 | .attrs = hvc_iucv_dev_attrs, |
| 1080 | }; |
| 1081 | static const struct attribute_group *hvc_iucv_dev_attr_groups[] = { |
| 1082 | &hvc_iucv_dev_attr_group, |
| 1083 | NULL, |
| 1084 | }; |
| 1085 | |
| 1086 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1087 | /** |
| 1088 | * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1089 | * @id: hvc_iucv_table index |
| 1090 | * @is_console: Flag if the instance is used as Linux console |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1091 | * |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 1092 | * This function allocates a new hvc_iucv_private structure and stores |
| 1093 | * the instance in hvc_iucv_table at index @id. |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1094 | * Returns 0 on success; otherwise non-zero. |
| 1095 | */ |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1096 | static int __init hvc_iucv_alloc(int id, unsigned int is_console) |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1097 | { |
| 1098 | struct hvc_iucv_private *priv; |
| 1099 | char name[9]; |
| 1100 | int rc; |
| 1101 | |
| 1102 | priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL); |
| 1103 | if (!priv) |
| 1104 | return -ENOMEM; |
| 1105 | |
| 1106 | spin_lock_init(&priv->lock); |
| 1107 | INIT_LIST_HEAD(&priv->tty_outqueue); |
| 1108 | INIT_LIST_HEAD(&priv->tty_inqueue); |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1109 | INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work); |
| 1110 | init_waitqueue_head(&priv->sndbuf_waitq); |
| 1111 | |
| 1112 | priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL); |
| 1113 | if (!priv->sndbuf) { |
| 1114 | kfree(priv); |
| 1115 | return -ENOMEM; |
| 1116 | } |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1117 | |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1118 | /* set console flag */ |
| 1119 | priv->is_console = is_console; |
| 1120 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1121 | /* allocate hvc device */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1122 | priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */ |
| 1123 | HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1124 | if (IS_ERR(priv->hvc)) { |
| 1125 | rc = PTR_ERR(priv->hvc); |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1126 | goto out_error_hvc; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 1129 | /* notify HVC thread instead of using polling */ |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1130 | priv->hvc->irq_requested = 1; |
| 1131 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1132 | /* setup iucv related information */ |
Hendrik Brueckner | 2dc184c | 2009-01-09 12:14:57 +0100 | [diff] [blame] | 1133 | snprintf(name, 9, "lnxhvc%-2d", id); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1134 | memcpy(priv->srv_name, name, 8); |
| 1135 | ASCEBC(priv->srv_name, 8); |
| 1136 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1137 | /* create and setup device */ |
| 1138 | priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL); |
| 1139 | if (!priv->dev) { |
| 1140 | rc = -ENOMEM; |
| 1141 | goto out_error_dev; |
| 1142 | } |
| 1143 | dev_set_name(priv->dev, "hvc_iucv%d", id); |
| 1144 | dev_set_drvdata(priv->dev, priv); |
| 1145 | priv->dev->bus = &iucv_bus; |
| 1146 | priv->dev->parent = iucv_root; |
| 1147 | priv->dev->driver = &hvc_iucv_driver; |
Hendrik Brueckner | f1206ba | 2014-01-17 14:42:00 +0100 | [diff] [blame] | 1148 | priv->dev->groups = hvc_iucv_dev_attr_groups; |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1149 | priv->dev->release = (void (*)(struct device *)) kfree; |
| 1150 | rc = device_register(priv->dev); |
| 1151 | if (rc) { |
Sebastian Ott | c630493 | 2009-09-11 10:28:38 +0200 | [diff] [blame] | 1152 | put_device(priv->dev); |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1153 | goto out_error_dev; |
| 1154 | } |
| 1155 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1156 | hvc_iucv_table[id] = priv; |
| 1157 | return 0; |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1158 | |
| 1159 | out_error_dev: |
| 1160 | hvc_remove(priv->hvc); |
| 1161 | out_error_hvc: |
| 1162 | free_page((unsigned long) priv->sndbuf); |
| 1163 | kfree(priv); |
| 1164 | |
| 1165 | return rc; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances |
| 1170 | */ |
| 1171 | static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv) |
| 1172 | { |
| 1173 | hvc_remove(priv->hvc); |
| 1174 | device_unregister(priv->dev); |
| 1175 | free_page((unsigned long) priv->sndbuf); |
| 1176 | kfree(priv); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /** |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1180 | * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID |
| 1181 | * @filter: String containing a comma-separated list of z/VM user IDs |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 1182 | * @dest: Location where to store the parsed z/VM user ID |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1183 | */ |
| 1184 | static const char *hvc_iucv_parse_filter(const char *filter, char *dest) |
| 1185 | { |
| 1186 | const char *nextdelim, *residual; |
| 1187 | size_t len; |
| 1188 | |
| 1189 | nextdelim = strchr(filter, ','); |
| 1190 | if (nextdelim) { |
| 1191 | len = nextdelim - filter; |
| 1192 | residual = nextdelim + 1; |
| 1193 | } else { |
| 1194 | len = strlen(filter); |
| 1195 | residual = filter + len; |
| 1196 | } |
| 1197 | |
| 1198 | if (len == 0) |
| 1199 | return ERR_PTR(-EINVAL); |
| 1200 | |
| 1201 | /* check for '\n' (if called from sysfs) */ |
| 1202 | if (filter[len - 1] == '\n') |
| 1203 | len--; |
| 1204 | |
Hendrik Brueckner | 926a733 | 2013-12-16 10:48:00 +0100 | [diff] [blame] | 1205 | /* prohibit filter entries containing the wildcard character only */ |
| 1206 | if (len == 1 && *filter == FILTER_WILDCARD_CHAR) |
| 1207 | return ERR_PTR(-EINVAL); |
| 1208 | |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1209 | if (len > 8) |
| 1210 | return ERR_PTR(-EINVAL); |
| 1211 | |
| 1212 | /* pad with blanks and save upper case version of user ID */ |
| 1213 | memset(dest, ' ', 8); |
| 1214 | while (len--) |
| 1215 | dest[len] = toupper(filter[len]); |
| 1216 | return residual; |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * hvc_iucv_setup_filter() - Set up z/VM user ID filter |
| 1221 | * @filter: String consisting of a comma-separated list of z/VM user IDs |
| 1222 | * |
| 1223 | * The function parses the @filter string and creates an array containing |
| 1224 | * the list of z/VM user ID filter entries. |
| 1225 | * Return code 0 means success, -EINVAL if the filter is syntactically |
| 1226 | * incorrect, -ENOMEM if there was not enough memory to allocate the |
| 1227 | * filter list array, or -ENOSPC if too many z/VM user IDs have been specified. |
| 1228 | */ |
| 1229 | static int hvc_iucv_setup_filter(const char *val) |
| 1230 | { |
| 1231 | const char *residual; |
| 1232 | int err; |
| 1233 | size_t size, count; |
| 1234 | void *array, *old_filter; |
| 1235 | |
| 1236 | count = strlen(val); |
| 1237 | if (count == 0 || (count == 1 && val[0] == '\n')) { |
| 1238 | size = 0; |
| 1239 | array = NULL; |
| 1240 | goto out_replace_filter; /* clear filter */ |
| 1241 | } |
| 1242 | |
| 1243 | /* count user IDs in order to allocate sufficient memory */ |
| 1244 | size = 1; |
| 1245 | residual = val; |
| 1246 | while ((residual = strchr(residual, ',')) != NULL) { |
| 1247 | residual++; |
| 1248 | size++; |
| 1249 | } |
| 1250 | |
| 1251 | /* check if the specified list exceeds the filter limit */ |
| 1252 | if (size > MAX_VMID_FILTER) |
| 1253 | return -ENOSPC; |
| 1254 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1255 | array = kcalloc(size, 8, GFP_KERNEL); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1256 | if (!array) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | count = size; |
| 1260 | residual = val; |
| 1261 | while (*residual && count) { |
| 1262 | residual = hvc_iucv_parse_filter(residual, |
| 1263 | array + ((size - count) * 8)); |
| 1264 | if (IS_ERR(residual)) { |
| 1265 | err = PTR_ERR(residual); |
| 1266 | kfree(array); |
| 1267 | goto out_err; |
| 1268 | } |
| 1269 | count--; |
| 1270 | } |
| 1271 | |
| 1272 | out_replace_filter: |
| 1273 | write_lock_bh(&hvc_iucv_filter_lock); |
| 1274 | old_filter = hvc_iucv_filter; |
| 1275 | hvc_iucv_filter_size = size; |
| 1276 | hvc_iucv_filter = array; |
| 1277 | write_unlock_bh(&hvc_iucv_filter_lock); |
| 1278 | kfree(old_filter); |
| 1279 | |
| 1280 | err = 0; |
| 1281 | out_err: |
| 1282 | return err; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * param_set_vmidfilter() - Set z/VM user ID filter parameter |
| 1287 | * @val: String consisting of a comma-separated list of z/VM user IDs |
| 1288 | * @kp: Kernel parameter pointing to hvc_iucv_filter array |
| 1289 | * |
| 1290 | * The function sets up the z/VM user ID filter specified as comma-separated |
| 1291 | * list of user IDs in @val. |
| 1292 | * Note: If it is called early in the boot process, @val is stored and |
| 1293 | * parsed later in hvc_iucv_init(). |
| 1294 | */ |
Sachin Sant | 549a8a0 | 2009-11-17 18:51:03 +0530 | [diff] [blame] | 1295 | static int param_set_vmidfilter(const char *val, const struct kernel_param *kp) |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1296 | { |
| 1297 | int rc; |
| 1298 | |
| 1299 | if (!MACHINE_IS_VM || !hvc_iucv_devices) |
| 1300 | return -ENODEV; |
| 1301 | |
| 1302 | if (!val) |
| 1303 | return -EINVAL; |
| 1304 | |
| 1305 | rc = 0; |
| 1306 | if (slab_is_available()) |
| 1307 | rc = hvc_iucv_setup_filter(val); |
| 1308 | else |
| 1309 | hvc_iucv_filter_string = val; /* defer... */ |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | /** |
| 1314 | * param_get_vmidfilter() - Get z/VM user ID filter |
| 1315 | * @buffer: Buffer to store z/VM user ID filter, |
| 1316 | * (buffer size assumption PAGE_SIZE) |
| 1317 | * @kp: Kernel parameter pointing to the hvc_iucv_filter array |
| 1318 | * |
| 1319 | * The function stores the filter as a comma-separated list of z/VM user IDs |
| 1320 | * in @buffer. Typically, sysfs routines call this function for attr show. |
| 1321 | */ |
Sachin Sant | 549a8a0 | 2009-11-17 18:51:03 +0530 | [diff] [blame] | 1322 | static int param_get_vmidfilter(char *buffer, const struct kernel_param *kp) |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1323 | { |
| 1324 | int rc; |
| 1325 | size_t index, len; |
| 1326 | void *start, *end; |
| 1327 | |
| 1328 | if (!MACHINE_IS_VM || !hvc_iucv_devices) |
| 1329 | return -ENODEV; |
| 1330 | |
| 1331 | rc = 0; |
| 1332 | read_lock_bh(&hvc_iucv_filter_lock); |
| 1333 | for (index = 0; index < hvc_iucv_filter_size; index++) { |
| 1334 | start = hvc_iucv_filter + (8 * index); |
| 1335 | end = memchr(start, ' ', 8); |
| 1336 | len = (end) ? end - start : 8; |
| 1337 | memcpy(buffer + rc, start, len); |
| 1338 | rc += len; |
| 1339 | buffer[rc++] = ','; |
| 1340 | } |
| 1341 | read_unlock_bh(&hvc_iucv_filter_lock); |
| 1342 | if (rc) |
| 1343 | buffer[--rc] = '\0'; /* replace last comma and update rc */ |
| 1344 | return rc; |
| 1345 | } |
| 1346 | |
| 1347 | #define param_check_vmidfilter(name, p) __param_check(name, p, void) |
| 1348 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 1349 | static const struct kernel_param_ops param_ops_vmidfilter = { |
Sachin Sant | 549a8a0 | 2009-11-17 18:51:03 +0530 | [diff] [blame] | 1350 | .set = param_set_vmidfilter, |
| 1351 | .get = param_get_vmidfilter, |
| 1352 | }; |
| 1353 | |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1354 | /** |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 1355 | * hvc_iucv_init() - z/VM IUCV HVC device driver initialization |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1356 | */ |
| 1357 | static int __init hvc_iucv_init(void) |
| 1358 | { |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1359 | int rc; |
| 1360 | unsigned int i; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1361 | |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1362 | if (!hvc_iucv_devices) |
| 1363 | return -ENODEV; |
| 1364 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1365 | if (!MACHINE_IS_VM) { |
Hendrik Brueckner | 82f3a79 | 2009-03-26 15:23:54 +0100 | [diff] [blame] | 1366 | pr_notice("The z/VM IUCV HVC device driver cannot " |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1367 | "be used without z/VM\n"); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1368 | rc = -ENODEV; |
| 1369 | goto out_error; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
Hendrik Brueckner | 82f3a79 | 2009-03-26 15:23:54 +0100 | [diff] [blame] | 1372 | if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) { |
| 1373 | pr_err("%lu is not a valid value for the hvc_iucv= " |
| 1374 | "kernel parameter\n", hvc_iucv_devices); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1375 | rc = -EINVAL; |
| 1376 | goto out_error; |
| 1377 | } |
| 1378 | |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1379 | /* register IUCV HVC device driver */ |
| 1380 | rc = driver_register(&hvc_iucv_driver); |
| 1381 | if (rc) |
| 1382 | goto out_error; |
| 1383 | |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1384 | /* parse hvc_iucv_allow string and create z/VM user ID filter list */ |
| 1385 | if (hvc_iucv_filter_string) { |
| 1386 | rc = hvc_iucv_setup_filter(hvc_iucv_filter_string); |
| 1387 | switch (rc) { |
| 1388 | case 0: |
| 1389 | break; |
| 1390 | case -ENOMEM: |
| 1391 | pr_err("Allocating memory failed with " |
| 1392 | "reason code=%d\n", 3); |
| 1393 | goto out_error; |
| 1394 | case -EINVAL: |
| 1395 | pr_err("hvc_iucv_allow= does not specify a valid " |
| 1396 | "z/VM user ID list\n"); |
| 1397 | goto out_error; |
| 1398 | case -ENOSPC: |
| 1399 | pr_err("hvc_iucv_allow= specifies too many " |
| 1400 | "z/VM user IDs\n"); |
| 1401 | goto out_error; |
| 1402 | default: |
| 1403 | goto out_error; |
| 1404 | } |
Hendrik Brueckner | 82f3a79 | 2009-03-26 15:23:54 +0100 | [diff] [blame] | 1405 | } |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1406 | |
| 1407 | hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT, |
| 1408 | sizeof(struct iucv_tty_buffer), |
| 1409 | 0, 0, NULL); |
| 1410 | if (!hvc_iucv_buffer_cache) { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1411 | pr_err("Allocating memory failed with reason code=%d\n", 1); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1412 | rc = -ENOMEM; |
| 1413 | goto out_error; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR, |
| 1417 | hvc_iucv_buffer_cache); |
| 1418 | if (!hvc_iucv_mempool) { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1419 | pr_err("Allocating memory failed with reason code=%d\n", 2); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1420 | kmem_cache_destroy(hvc_iucv_buffer_cache); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1421 | rc = -ENOMEM; |
| 1422 | goto out_error; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
Hendrik Brueckner | 68c6b3d | 2009-01-09 12:15:00 +0100 | [diff] [blame] | 1425 | /* register the first terminal device as console |
| 1426 | * (must be done before allocating hvc terminal devices) */ |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1427 | rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops); |
| 1428 | if (rc) { |
| 1429 | pr_err("Registering HVC terminal device as " |
| 1430 | "Linux console failed\n"); |
| 1431 | goto out_error_memory; |
| 1432 | } |
Hendrik Brueckner | 68c6b3d | 2009-01-09 12:15:00 +0100 | [diff] [blame] | 1433 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1434 | /* allocate hvc_iucv_private structs */ |
| 1435 | for (i = 0; i < hvc_iucv_devices; i++) { |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1436 | rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1437 | if (rc) { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1438 | pr_err("Creating a new HVC terminal device " |
Hendrik Brueckner | 17e19f0 | 2009-01-09 12:14:59 +0100 | [diff] [blame] | 1439 | "failed with error code=%d\n", rc); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1440 | goto out_error_hvc; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | /* register IUCV callback handler */ |
| 1445 | rc = iucv_register(&hvc_iucv_handler, 0); |
| 1446 | if (rc) { |
Hendrik Brueckner | c45ce4b | 2009-01-09 12:14:58 +0100 | [diff] [blame] | 1447 | pr_err("Registering IUCV handlers failed with error code=%d\n", |
| 1448 | rc); |
Hendrik Brueckner | c77f7cf | 2010-10-25 16:10:22 +0200 | [diff] [blame] | 1449 | goto out_error_hvc; |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | return 0; |
| 1453 | |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1454 | out_error_hvc: |
| 1455 | for (i = 0; i < hvc_iucv_devices; i++) |
Hendrik Brueckner | 0259162 | 2009-06-16 10:30:45 +0200 | [diff] [blame] | 1456 | if (hvc_iucv_table[i]) |
| 1457 | hvc_iucv_destroy(hvc_iucv_table[i]); |
Hendrik Brueckner | 6c089fd | 2009-01-09 12:15:01 +0100 | [diff] [blame] | 1458 | out_error_memory: |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1459 | mempool_destroy(hvc_iucv_mempool); |
| 1460 | kmem_cache_destroy(hvc_iucv_buffer_cache); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1461 | out_error: |
Hendrik Brueckner | ac526f4 | 2013-10-25 16:01:04 +0200 | [diff] [blame] | 1462 | kfree(hvc_iucv_filter); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1463 | hvc_iucv_devices = 0; /* ensure that we do not provide any device */ |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1464 | return rc; |
| 1465 | } |
| 1466 | |
| 1467 | /** |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1468 | * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter |
| 1469 | * @val: Parameter value (numeric) |
| 1470 | */ |
| 1471 | static int __init hvc_iucv_config(char *val) |
| 1472 | { |
Jingoo Han | 86b4056 | 2013-06-01 16:30:06 +0900 | [diff] [blame] | 1473 | return kstrtoul(val, 10, &hvc_iucv_devices); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | |
Hendrik Brueckner | 68c6b3d | 2009-01-09 12:15:00 +0100 | [diff] [blame] | 1477 | device_initcall(hvc_iucv_init); |
Hendrik Brueckner | 44a01d5 | 2008-12-25 13:38:57 +0100 | [diff] [blame] | 1478 | __setup("hvc_iucv=", hvc_iucv_config); |
Hendrik Brueckner | 431429f | 2009-03-26 15:23:55 +0100 | [diff] [blame] | 1479 | core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640); |