Commit | Line | Data |
---|---|---|
44a01d5b | 1 | /* |
17e19f04 | 2 | * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver |
44a01d5b | 3 | * |
17e19f04 | 4 | * This HVC device driver provides terminal access using |
44a01d5b HB |
5 | * z/VM IUCV communication paths. |
6 | * | |
17e19f04 | 7 | * Copyright IBM Corp. 2008 |
44a01d5b HB |
8 | * |
9 | * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> | |
10 | */ | |
11 | #define KMSG_COMPONENT "hvc_iucv" | |
17e19f04 | 12 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
44a01d5b HB |
13 | |
14 | #include <linux/types.h> | |
15 | #include <asm/ebcdic.h> | |
c45ce4b5 | 16 | #include <linux/delay.h> |
68c6b3d2 | 17 | #include <linux/init.h> |
44a01d5b HB |
18 | #include <linux/mempool.h> |
19 | #include <linux/module.h> | |
20 | #include <linux/tty.h> | |
c45ce4b5 | 21 | #include <linux/wait.h> |
44a01d5b HB |
22 | #include <net/iucv/iucv.h> |
23 | ||
24 | #include "hvc_console.h" | |
25 | ||
26 | ||
17e19f04 | 27 | /* General device driver settings */ |
44a01d5b HB |
28 | #define HVC_IUCV_MAGIC 0xc9e4c3e5 |
29 | #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS | |
30 | #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4) | |
31 | ||
32 | /* IUCV TTY message */ | |
33 | #define MSG_VERSION 0x02 /* Message version */ | |
34 | #define MSG_TYPE_ERROR 0x01 /* Error message */ | |
35 | #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */ | |
36 | #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */ | |
37 | #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */ | |
38 | #define MSG_TYPE_DATA 0x10 /* Terminal data */ | |
39 | ||
44a01d5b HB |
40 | struct iucv_tty_msg { |
41 | u8 version; /* Message version */ | |
42 | u8 type; /* Message type */ | |
c45ce4b5 | 43 | #define MSG_MAX_DATALEN ((u16)(~0)) |
44a01d5b HB |
44 | u16 datalen; /* Payload length */ |
45 | u8 data[]; /* Payload buffer */ | |
46 | } __attribute__((packed)); | |
17e19f04 | 47 | #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data)) |
44a01d5b HB |
48 | |
49 | enum iucv_state_t { | |
50 | IUCV_DISCONN = 0, | |
51 | IUCV_CONNECTED = 1, | |
52 | IUCV_SEVERED = 2, | |
53 | }; | |
54 | ||
55 | enum tty_state_t { | |
56 | TTY_CLOSED = 0, | |
57 | TTY_OPENED = 1, | |
58 | }; | |
59 | ||
60 | struct hvc_iucv_private { | |
17e19f04 | 61 | struct hvc_struct *hvc; /* HVC struct reference */ |
44a01d5b HB |
62 | u8 srv_name[8]; /* IUCV service name (ebcdic) */ |
63 | enum iucv_state_t iucv_state; /* IUCV connection status */ | |
64 | enum tty_state_t tty_state; /* TTY status */ | |
65 | struct iucv_path *path; /* IUCV path pointer */ | |
66 | spinlock_t lock; /* hvc_iucv_private lock */ | |
c45ce4b5 HB |
67 | #define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */ |
68 | void *sndbuf; /* send buffer */ | |
69 | size_t sndbuf_len; /* length of send buffer */ | |
70 | #define QUEUE_SNDBUF_DELAY (HZ / 25) | |
71 | struct delayed_work sndbuf_work; /* work: send iucv msg(s) */ | |
72 | wait_queue_head_t sndbuf_waitq; /* wait for send completion */ | |
44a01d5b HB |
73 | struct list_head tty_outqueue; /* outgoing IUCV messages */ |
74 | struct list_head tty_inqueue; /* incoming IUCV messages */ | |
75 | }; | |
76 | ||
77 | struct iucv_tty_buffer { | |
78 | struct list_head list; /* list pointer */ | |
17e19f04 | 79 | struct iucv_message msg; /* store an IUCV message */ |
44a01d5b HB |
80 | size_t offset; /* data buffer offset */ |
81 | struct iucv_tty_msg *mbuf; /* buffer to store input/output data */ | |
82 | }; | |
83 | ||
84 | /* IUCV callback handler */ | |
85 | static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]); | |
86 | static void hvc_iucv_path_severed(struct iucv_path *, u8[16]); | |
87 | static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *); | |
88 | static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *); | |
89 | ||
90 | ||
2dc184c0 HB |
91 | /* Kernel module parameter: use one terminal device as default */ |
92 | static unsigned long hvc_iucv_devices = 1; | |
44a01d5b HB |
93 | |
94 | /* Array of allocated hvc iucv tty lines... */ | |
95 | static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES]; | |
96 | ||
97 | /* Kmem cache and mempool for iucv_tty_buffer elements */ | |
98 | static struct kmem_cache *hvc_iucv_buffer_cache; | |
99 | static mempool_t *hvc_iucv_mempool; | |
100 | ||
101 | /* IUCV handler callback functions */ | |
102 | static struct iucv_handler hvc_iucv_handler = { | |
103 | .path_pending = hvc_iucv_path_pending, | |
104 | .path_severed = hvc_iucv_path_severed, | |
105 | .message_complete = hvc_iucv_msg_complete, | |
106 | .message_pending = hvc_iucv_msg_pending, | |
107 | }; | |
108 | ||
109 | ||
110 | /** | |
111 | * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance. | |
112 | * @num: The HVC virtual terminal number (vtermno) | |
113 | * | |
114 | * This function returns the struct hvc_iucv_private instance that corresponds | |
115 | * to the HVC virtual terminal number specified as parameter @num. | |
116 | */ | |
117 | struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num) | |
118 | { | |
119 | if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices)) | |
120 | return NULL; | |
121 | return hvc_iucv_table[num - HVC_IUCV_MAGIC]; | |
122 | } | |
123 | ||
124 | /** | |
17e19f04 | 125 | * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element. |
44a01d5b HB |
126 | * @size: Size of the internal buffer used to store data. |
127 | * @flags: Memory allocation flags passed to mempool. | |
128 | * | |
129 | * This function allocates a new struct iucv_tty_buffer element and, optionally, | |
130 | * allocates an internal data buffer with the specified size @size. | |
131 | * Note: The total message size arises from the internal buffer size and the | |
132 | * members of the iucv_tty_msg structure. | |
44a01d5b HB |
133 | * The function returns NULL if memory allocation has failed. |
134 | */ | |
135 | static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags) | |
136 | { | |
137 | struct iucv_tty_buffer *bufp; | |
138 | ||
139 | bufp = mempool_alloc(hvc_iucv_mempool, flags); | |
140 | if (!bufp) | |
141 | return NULL; | |
142 | memset(bufp, 0, sizeof(struct iucv_tty_buffer)); | |
143 | ||
144 | if (size > 0) { | |
145 | bufp->msg.length = MSG_SIZE(size); | |
146 | bufp->mbuf = kmalloc(bufp->msg.length, flags); | |
147 | if (!bufp->mbuf) { | |
148 | mempool_free(bufp, hvc_iucv_mempool); | |
149 | return NULL; | |
150 | } | |
151 | bufp->mbuf->version = MSG_VERSION; | |
152 | bufp->mbuf->type = MSG_TYPE_DATA; | |
153 | bufp->mbuf->datalen = (u16) size; | |
154 | } | |
155 | return bufp; | |
156 | } | |
157 | ||
158 | /** | |
159 | * destroy_tty_buffer() - destroy struct iucv_tty_buffer element. | |
160 | * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL. | |
44a01d5b HB |
161 | */ |
162 | static void destroy_tty_buffer(struct iucv_tty_buffer *bufp) | |
163 | { | |
164 | kfree(bufp->mbuf); | |
165 | mempool_free(bufp, hvc_iucv_mempool); | |
166 | } | |
167 | ||
168 | /** | |
169 | * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element. | |
17e19f04 | 170 | * @list: List containing struct iucv_tty_buffer elements. |
44a01d5b HB |
171 | */ |
172 | static void destroy_tty_buffer_list(struct list_head *list) | |
173 | { | |
174 | struct iucv_tty_buffer *ent, *next; | |
175 | ||
176 | list_for_each_entry_safe(ent, next, list, list) { | |
177 | list_del(&ent->list); | |
178 | destroy_tty_buffer(ent); | |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
17e19f04 HB |
183 | * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer. |
184 | * @priv: Pointer to struct hvc_iucv_private | |
185 | * @buf: HVC buffer for writing received terminal data. | |
186 | * @count: HVC buffer size. | |
44a01d5b HB |
187 | * @has_more_data: Pointer to an int variable. |
188 | * | |
189 | * The function picks up pending messages from the input queue and receives | |
190 | * the message data that is then written to the specified buffer @buf. | |
17e19f04 | 191 | * If the buffer size @count is less than the data message size, the |
44a01d5b | 192 | * message is kept on the input queue and @has_more_data is set to 1. |
17e19f04 | 193 | * If all message data has been written, the message is removed from |
44a01d5b HB |
194 | * the input queue. |
195 | * | |
196 | * The function returns the number of bytes written to the terminal, zero if | |
197 | * there are no pending data messages available or if there is no established | |
198 | * IUCV path. | |
199 | * If the IUCV path has been severed, then -EPIPE is returned to cause a | |
17e19f04 | 200 | * hang up (that is issued by the HVC layer). |
44a01d5b HB |
201 | */ |
202 | static int hvc_iucv_write(struct hvc_iucv_private *priv, | |
203 | char *buf, int count, int *has_more_data) | |
204 | { | |
205 | struct iucv_tty_buffer *rb; | |
206 | int written; | |
207 | int rc; | |
208 | ||
17e19f04 | 209 | /* immediately return if there is no IUCV connection */ |
44a01d5b HB |
210 | if (priv->iucv_state == IUCV_DISCONN) |
211 | return 0; | |
212 | ||
17e19f04 HB |
213 | /* if the IUCV path has been severed, return -EPIPE to inform the |
214 | * HVC layer to hang up the tty device. */ | |
44a01d5b HB |
215 | if (priv->iucv_state == IUCV_SEVERED) |
216 | return -EPIPE; | |
217 | ||
218 | /* check if there are pending messages */ | |
219 | if (list_empty(&priv->tty_inqueue)) | |
220 | return 0; | |
221 | ||
17e19f04 | 222 | /* receive an iucv message and flip data to the tty (ldisc) */ |
44a01d5b HB |
223 | rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list); |
224 | ||
225 | written = 0; | |
226 | if (!rb->mbuf) { /* message not yet received ... */ | |
227 | /* allocate mem to store msg data; if no memory is available | |
228 | * then leave the buffer on the list and re-try later */ | |
229 | rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC); | |
230 | if (!rb->mbuf) | |
231 | return -ENOMEM; | |
232 | ||
233 | rc = __iucv_message_receive(priv->path, &rb->msg, 0, | |
234 | rb->mbuf, rb->msg.length, NULL); | |
235 | switch (rc) { | |
236 | case 0: /* Successful */ | |
237 | break; | |
238 | case 2: /* No message found */ | |
239 | case 9: /* Message purged */ | |
240 | break; | |
241 | default: | |
242 | written = -EIO; | |
243 | } | |
244 | /* remove buffer if an error has occured or received data | |
245 | * is not correct */ | |
246 | if (rc || (rb->mbuf->version != MSG_VERSION) || | |
247 | (rb->msg.length != MSG_SIZE(rb->mbuf->datalen))) | |
248 | goto out_remove_buffer; | |
249 | } | |
250 | ||
251 | switch (rb->mbuf->type) { | |
252 | case MSG_TYPE_DATA: | |
253 | written = min_t(int, rb->mbuf->datalen - rb->offset, count); | |
254 | memcpy(buf, rb->mbuf->data + rb->offset, written); | |
255 | if (written < (rb->mbuf->datalen - rb->offset)) { | |
256 | rb->offset += written; | |
257 | *has_more_data = 1; | |
258 | goto out_written; | |
259 | } | |
260 | break; | |
261 | ||
262 | case MSG_TYPE_WINSIZE: | |
263 | if (rb->mbuf->datalen != sizeof(struct winsize)) | |
264 | break; | |
17e19f04 | 265 | hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data)); |
44a01d5b HB |
266 | break; |
267 | ||
268 | case MSG_TYPE_ERROR: /* ignored ... */ | |
269 | case MSG_TYPE_TERMENV: /* ignored ... */ | |
270 | case MSG_TYPE_TERMIOS: /* ignored ... */ | |
271 | break; | |
272 | } | |
273 | ||
274 | out_remove_buffer: | |
275 | list_del(&rb->list); | |
276 | destroy_tty_buffer(rb); | |
277 | *has_more_data = !list_empty(&priv->tty_inqueue); | |
278 | ||
279 | out_written: | |
280 | return written; | |
281 | } | |
282 | ||
283 | /** | |
284 | * hvc_iucv_get_chars() - HVC get_chars operation. | |
285 | * @vtermno: HVC virtual terminal number. | |
286 | * @buf: Pointer to a buffer to store data | |
287 | * @count: Size of buffer available for writing | |
288 | * | |
17e19f04 HB |
289 | * The HVC thread calls this method to read characters from the back-end. |
290 | * If an IUCV communication path has been established, pending IUCV messages | |
291 | * are received and data is copied into buffer @buf up to @count bytes. | |
44a01d5b HB |
292 | * |
293 | * Locking: The routine gets called under an irqsave() spinlock; and | |
294 | * the routine locks the struct hvc_iucv_private->lock to call | |
295 | * helper functions. | |
296 | */ | |
297 | static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count) | |
298 | { | |
299 | struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno); | |
300 | int written; | |
301 | int has_more_data; | |
302 | ||
303 | if (count <= 0) | |
304 | return 0; | |
305 | ||
306 | if (!priv) | |
307 | return -ENODEV; | |
308 | ||
309 | spin_lock(&priv->lock); | |
310 | has_more_data = 0; | |
311 | written = hvc_iucv_write(priv, buf, count, &has_more_data); | |
312 | spin_unlock(&priv->lock); | |
313 | ||
314 | /* if there are still messages on the queue... schedule another run */ | |
315 | if (has_more_data) | |
316 | hvc_kick(); | |
317 | ||
318 | return written; | |
319 | } | |
320 | ||
321 | /** | |
c45ce4b5 | 322 | * hvc_iucv_queue() - Buffer terminal data for sending. |
44a01d5b HB |
323 | * @priv: Pointer to struct hvc_iucv_private instance. |
324 | * @buf: Buffer containing data to send. | |
c45ce4b5 | 325 | * @count: Size of buffer and amount of data to send. |
44a01d5b | 326 | * |
c45ce4b5 | 327 | * The function queues data for sending. To actually send the buffered data, |
17e19f04 | 328 | * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY). |
c45ce4b5 | 329 | * The function returns the number of data bytes that has been buffered. |
44a01d5b | 330 | * |
c45ce4b5 HB |
331 | * If the device is not connected, data is ignored and the function returns |
332 | * @count. | |
333 | * If the buffer is full, the function returns 0. | |
17e19f04 HB |
334 | * If an existing IUCV communicaton path has been severed, -EPIPE is returned |
335 | * (that can be passed to HVC layer to cause a tty hangup). | |
44a01d5b | 336 | */ |
c45ce4b5 | 337 | static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf, |
17e19f04 | 338 | int count) |
c45ce4b5 HB |
339 | { |
340 | size_t len; | |
341 | ||
342 | if (priv->iucv_state == IUCV_DISCONN) | |
343 | return count; /* ignore data */ | |
344 | ||
345 | if (priv->iucv_state == IUCV_SEVERED) | |
346 | return -EPIPE; | |
347 | ||
348 | len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len); | |
349 | if (!len) | |
350 | return 0; | |
351 | ||
352 | memcpy(priv->sndbuf + priv->sndbuf_len, buf, len); | |
353 | priv->sndbuf_len += len; | |
354 | ||
355 | if (priv->iucv_state == IUCV_CONNECTED) | |
356 | schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY); | |
357 | ||
358 | return len; | |
359 | } | |
360 | ||
361 | /** | |
362 | * hvc_iucv_send() - Send an IUCV message containing terminal data. | |
363 | * @priv: Pointer to struct hvc_iucv_private instance. | |
364 | * | |
17e19f04 HB |
365 | * If an IUCV communication path has been established, the buffered output data |
366 | * is sent via an IUCV message and the number of bytes sent is returned. | |
367 | * Returns 0 if there is no established IUCV communication path or | |
368 | * -EPIPE if an existing IUCV communicaton path has been severed. | |
c45ce4b5 HB |
369 | */ |
370 | static int hvc_iucv_send(struct hvc_iucv_private *priv) | |
44a01d5b HB |
371 | { |
372 | struct iucv_tty_buffer *sb; | |
c45ce4b5 | 373 | int rc, len; |
44a01d5b HB |
374 | |
375 | if (priv->iucv_state == IUCV_SEVERED) | |
376 | return -EPIPE; | |
377 | ||
378 | if (priv->iucv_state == IUCV_DISCONN) | |
c45ce4b5 | 379 | return -EIO; |
44a01d5b | 380 | |
c45ce4b5 HB |
381 | if (!priv->sndbuf_len) |
382 | return 0; | |
44a01d5b HB |
383 | |
384 | /* allocate internal buffer to store msg data and also compute total | |
385 | * message length */ | |
c45ce4b5 | 386 | sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC); |
44a01d5b HB |
387 | if (!sb) |
388 | return -ENOMEM; | |
389 | ||
c45ce4b5 HB |
390 | memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len); |
391 | sb->mbuf->datalen = (u16) priv->sndbuf_len; | |
392 | sb->msg.length = MSG_SIZE(sb->mbuf->datalen); | |
44a01d5b HB |
393 | |
394 | list_add_tail(&sb->list, &priv->tty_outqueue); | |
395 | ||
396 | rc = __iucv_message_send(priv->path, &sb->msg, 0, 0, | |
397 | (void *) sb->mbuf, sb->msg.length); | |
398 | if (rc) { | |
c45ce4b5 HB |
399 | /* drop the message here; however we might want to handle |
400 | * 0x03 (msg limit reached) by trying again... */ | |
44a01d5b HB |
401 | list_del(&sb->list); |
402 | destroy_tty_buffer(sb); | |
44a01d5b | 403 | } |
c45ce4b5 HB |
404 | len = priv->sndbuf_len; |
405 | priv->sndbuf_len = 0; | |
44a01d5b HB |
406 | |
407 | return len; | |
408 | } | |
409 | ||
c45ce4b5 HB |
410 | /** |
411 | * hvc_iucv_sndbuf_work() - Send buffered data over IUCV | |
412 | * @work: Work structure. | |
413 | * | |
17e19f04 HB |
414 | * This work queue function sends buffered output data over IUCV and, |
415 | * if not all buffered data could be sent, reschedules itself. | |
c45ce4b5 HB |
416 | */ |
417 | static void hvc_iucv_sndbuf_work(struct work_struct *work) | |
418 | { | |
419 | struct hvc_iucv_private *priv; | |
420 | ||
421 | priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work); | |
c45ce4b5 HB |
422 | if (!priv) |
423 | return; | |
424 | ||
425 | spin_lock_bh(&priv->lock); | |
426 | hvc_iucv_send(priv); | |
427 | spin_unlock_bh(&priv->lock); | |
428 | } | |
429 | ||
44a01d5b HB |
430 | /** |
431 | * hvc_iucv_put_chars() - HVC put_chars operation. | |
432 | * @vtermno: HVC virtual terminal number. | |
433 | * @buf: Pointer to an buffer to read data from | |
434 | * @count: Size of buffer available for reading | |
435 | * | |
17e19f04 HB |
436 | * The HVC thread calls this method to write characters to the back-end. |
437 | * The function calls hvc_iucv_queue() to queue terminal data for sending. | |
44a01d5b HB |
438 | * |
439 | * Locking: The method gets called under an irqsave() spinlock; and | |
440 | * locks struct hvc_iucv_private->lock. | |
441 | */ | |
442 | static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count) | |
443 | { | |
444 | struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno); | |
c45ce4b5 | 445 | int queued; |
44a01d5b HB |
446 | |
447 | if (count <= 0) | |
448 | return 0; | |
449 | ||
450 | if (!priv) | |
451 | return -ENODEV; | |
452 | ||
453 | spin_lock(&priv->lock); | |
c45ce4b5 | 454 | queued = hvc_iucv_queue(priv, buf, count); |
44a01d5b HB |
455 | spin_unlock(&priv->lock); |
456 | ||
c45ce4b5 | 457 | return queued; |
44a01d5b HB |
458 | } |
459 | ||
460 | /** | |
461 | * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time. | |
462 | * @hp: Pointer to the HVC device (struct hvc_struct) | |
463 | * @id: Additional data (originally passed to hvc_alloc): the index of an struct | |
464 | * hvc_iucv_private instance. | |
465 | * | |
466 | * The function sets the tty state to TTY_OPEN for the struct hvc_iucv_private | |
467 | * instance that is derived from @id. Always returns 0. | |
468 | * | |
469 | * Locking: struct hvc_iucv_private->lock, spin_lock_bh | |
470 | */ | |
471 | static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id) | |
472 | { | |
473 | struct hvc_iucv_private *priv; | |
474 | ||
475 | priv = hvc_iucv_get_private(id); | |
476 | if (!priv) | |
477 | return 0; | |
478 | ||
479 | spin_lock_bh(&priv->lock); | |
480 | priv->tty_state = TTY_OPENED; | |
481 | spin_unlock_bh(&priv->lock); | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | /** | |
17e19f04 | 487 | * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance. |
44a01d5b | 488 | * @priv: Pointer to the struct hvc_iucv_private instance. |
44a01d5b HB |
489 | */ |
490 | static void hvc_iucv_cleanup(struct hvc_iucv_private *priv) | |
491 | { | |
492 | destroy_tty_buffer_list(&priv->tty_outqueue); | |
493 | destroy_tty_buffer_list(&priv->tty_inqueue); | |
494 | ||
495 | priv->tty_state = TTY_CLOSED; | |
496 | priv->iucv_state = IUCV_DISCONN; | |
c45ce4b5 HB |
497 | |
498 | priv->sndbuf_len = 0; | |
499 | } | |
500 | ||
501 | /** | |
502 | * tty_outqueue_empty() - Test if the tty outq is empty | |
503 | * @priv: Pointer to struct hvc_iucv_private instance. | |
504 | */ | |
505 | static inline int tty_outqueue_empty(struct hvc_iucv_private *priv) | |
506 | { | |
507 | int rc; | |
508 | ||
509 | spin_lock_bh(&priv->lock); | |
510 | rc = list_empty(&priv->tty_outqueue); | |
511 | spin_unlock_bh(&priv->lock); | |
512 | ||
513 | return rc; | |
514 | } | |
515 | ||
516 | /** | |
517 | * flush_sndbuf_sync() - Flush send buffer and wait for completion | |
518 | * @priv: Pointer to struct hvc_iucv_private instance. | |
519 | * | |
520 | * The routine cancels a pending sndbuf work, calls hvc_iucv_send() | |
521 | * to flush any buffered terminal output data and waits for completion. | |
522 | */ | |
523 | static void flush_sndbuf_sync(struct hvc_iucv_private *priv) | |
524 | { | |
525 | int sync_wait; | |
526 | ||
527 | cancel_delayed_work_sync(&priv->sndbuf_work); | |
528 | ||
529 | spin_lock_bh(&priv->lock); | |
530 | hvc_iucv_send(priv); /* force sending buffered data */ | |
531 | sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */ | |
532 | spin_unlock_bh(&priv->lock); | |
533 | ||
534 | if (sync_wait) | |
535 | wait_event_timeout(priv->sndbuf_waitq, | |
536 | tty_outqueue_empty(priv), HZ); | |
44a01d5b HB |
537 | } |
538 | ||
539 | /** | |
17e19f04 HB |
540 | * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups. |
541 | * @hp: Pointer to the HVC device (struct hvc_struct) | |
542 | * @id: Additional data (originally passed to hvc_alloc): | |
543 | * the index of an struct hvc_iucv_private instance. | |
44a01d5b | 544 | * |
17e19f04 | 545 | * This routine notifies the HVC back-end that a tty hangup (carrier loss, |
44a01d5b | 546 | * virtual or otherwise) has occured. |
17e19f04 HB |
547 | * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup()) |
548 | * to keep an existing IUCV communication path established. | |
44a01d5b HB |
549 | * (Background: vhangup() is called from user space (by getty or login) to |
550 | * disable writing to the tty by other applications). | |
17e19f04 HB |
551 | * If the tty has been opened and an established IUCV path has been severed |
552 | * (we caused the tty hangup), the function calls hvc_iucv_cleanup(). | |
44a01d5b HB |
553 | * |
554 | * Locking: struct hvc_iucv_private->lock | |
555 | */ | |
556 | static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id) | |
557 | { | |
558 | struct hvc_iucv_private *priv; | |
559 | ||
560 | priv = hvc_iucv_get_private(id); | |
561 | if (!priv) | |
562 | return; | |
563 | ||
c45ce4b5 HB |
564 | flush_sndbuf_sync(priv); |
565 | ||
44a01d5b HB |
566 | spin_lock_bh(&priv->lock); |
567 | /* NOTE: If the hangup was scheduled by ourself (from the iucv | |
17e19f04 HB |
568 | * path_servered callback [IUCV_SEVERED]), we have to clean up |
569 | * our structure and to set state to TTY_CLOSED. | |
44a01d5b HB |
570 | * If the tty was hung up otherwise (e.g. vhangup()), then we |
571 | * ignore this hangup and keep an established IUCV path open... | |
572 | * (...the reason is that we are not able to connect back to the | |
573 | * client if we disconnect on hang up) */ | |
574 | priv->tty_state = TTY_CLOSED; | |
575 | ||
576 | if (priv->iucv_state == IUCV_SEVERED) | |
577 | hvc_iucv_cleanup(priv); | |
578 | spin_unlock_bh(&priv->lock); | |
579 | } | |
580 | ||
581 | /** | |
582 | * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time. | |
583 | * @hp: Pointer to the HVC device (struct hvc_struct) | |
584 | * @id: Additional data (originally passed to hvc_alloc): | |
585 | * the index of an struct hvc_iucv_private instance. | |
586 | * | |
17e19f04 HB |
587 | * This routine notifies the HVC back-end that the last tty device fd has been |
588 | * closed. The function calls hvc_iucv_cleanup() to clean up the struct | |
589 | * hvc_iucv_private instance. | |
44a01d5b HB |
590 | * |
591 | * Locking: struct hvc_iucv_private->lock | |
592 | */ | |
593 | static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id) | |
594 | { | |
595 | struct hvc_iucv_private *priv; | |
596 | struct iucv_path *path; | |
597 | ||
598 | priv = hvc_iucv_get_private(id); | |
599 | if (!priv) | |
600 | return; | |
601 | ||
c45ce4b5 HB |
602 | flush_sndbuf_sync(priv); |
603 | ||
44a01d5b HB |
604 | spin_lock_bh(&priv->lock); |
605 | path = priv->path; /* save reference to IUCV path */ | |
606 | priv->path = NULL; | |
607 | hvc_iucv_cleanup(priv); | |
608 | spin_unlock_bh(&priv->lock); | |
609 | ||
610 | /* sever IUCV path outside of priv->lock due to lock ordering of: | |
611 | * priv->lock <--> iucv_table_lock */ | |
612 | if (path) { | |
613 | iucv_path_sever(path, NULL); | |
614 | iucv_path_free(path); | |
615 | } | |
616 | } | |
617 | ||
618 | /** | |
619 | * hvc_iucv_path_pending() - IUCV handler to process a connection request. | |
620 | * @path: Pending path (struct iucv_path) | |
17e19f04 | 621 | * @ipvmid: z/VM system identifier of originator |
44a01d5b HB |
622 | * @ipuser: User specified data for this path |
623 | * (AF_IUCV: port/service name and originator port) | |
624 | * | |
17e19f04 HB |
625 | * The function uses the @ipuser data to determine if the pending path belongs |
626 | * to a terminal managed by this device driver. | |
627 | * If the path belongs to this driver, ensure that the terminal is not accessed | |
628 | * multiple times (only one connection to a terminal is allowed). | |
629 | * If the terminal is not yet connected, the pending path is accepted and is | |
630 | * associated to the appropriate struct hvc_iucv_private instance. | |
44a01d5b | 631 | * |
17e19f04 | 632 | * Returns 0 if @path belongs to a terminal managed by the this device driver; |
44a01d5b HB |
633 | * otherwise returns -ENODEV in order to dispatch this path to other handlers. |
634 | * | |
635 | * Locking: struct hvc_iucv_private->lock | |
636 | */ | |
637 | static int hvc_iucv_path_pending(struct iucv_path *path, | |
638 | u8 ipvmid[8], u8 ipuser[16]) | |
639 | { | |
640 | struct hvc_iucv_private *priv; | |
641 | u8 nuser_data[16]; | |
642 | int i, rc; | |
643 | ||
644 | priv = NULL; | |
645 | for (i = 0; i < hvc_iucv_devices; i++) | |
646 | if (hvc_iucv_table[i] && | |
647 | (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) { | |
648 | priv = hvc_iucv_table[i]; | |
649 | break; | |
650 | } | |
44a01d5b HB |
651 | if (!priv) |
652 | return -ENODEV; | |
653 | ||
654 | spin_lock(&priv->lock); | |
655 | ||
656 | /* If the terminal is already connected or being severed, then sever | |
657 | * this path to enforce that there is only ONE established communication | |
658 | * path per terminal. */ | |
659 | if (priv->iucv_state != IUCV_DISCONN) { | |
660 | iucv_path_sever(path, ipuser); | |
661 | iucv_path_free(path); | |
662 | goto out_path_handled; | |
663 | } | |
664 | ||
665 | /* accept path */ | |
666 | memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */ | |
667 | memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */ | |
668 | path->msglim = 0xffff; /* IUCV MSGLIMIT */ | |
669 | path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */ | |
670 | rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv); | |
671 | if (rc) { | |
672 | iucv_path_sever(path, ipuser); | |
673 | iucv_path_free(path); | |
674 | goto out_path_handled; | |
675 | } | |
676 | priv->path = path; | |
677 | priv->iucv_state = IUCV_CONNECTED; | |
678 | ||
c45ce4b5 HB |
679 | /* flush buffered output data... */ |
680 | schedule_delayed_work(&priv->sndbuf_work, 5); | |
681 | ||
44a01d5b HB |
682 | out_path_handled: |
683 | spin_unlock(&priv->lock); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | /** | |
688 | * hvc_iucv_path_severed() - IUCV handler to process a path sever. | |
689 | * @path: Pending path (struct iucv_path) | |
690 | * @ipuser: User specified data for this path | |
691 | * (AF_IUCV: port/service name and originator port) | |
692 | * | |
693 | * The function also severs the path (as required by the IUCV protocol) and | |
694 | * sets the iucv state to IUCV_SEVERED for the associated struct | |
695 | * hvc_iucv_private instance. Later, the IUCV_SEVERED state triggers a tty | |
696 | * hangup (hvc_iucv_get_chars() / hvc_iucv_write()). | |
17e19f04 | 697 | * If tty portion of the HVC is closed, clean up the outqueue. |
44a01d5b HB |
698 | * |
699 | * Locking: struct hvc_iucv_private->lock | |
700 | */ | |
701 | static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16]) | |
702 | { | |
703 | struct hvc_iucv_private *priv = path->private; | |
704 | ||
705 | spin_lock(&priv->lock); | |
706 | priv->iucv_state = IUCV_SEVERED; | |
707 | ||
c45ce4b5 HB |
708 | /* If the tty has not yet been opened, clean up the hvc_iucv_private |
709 | * structure to allow re-connects. | |
44a01d5b | 710 | * |
c45ce4b5 HB |
711 | * If it has been opened, let get_chars() return -EPIPE to signal the |
712 | * HVC layer to hang up the tty. | |
713 | * If so, we need to wake up the HVC thread to call get_chars()... | |
714 | */ | |
44a01d5b HB |
715 | priv->path = NULL; |
716 | if (priv->tty_state == TTY_CLOSED) | |
717 | hvc_iucv_cleanup(priv); | |
c45ce4b5 HB |
718 | else |
719 | hvc_kick(); | |
44a01d5b HB |
720 | spin_unlock(&priv->lock); |
721 | ||
722 | /* finally sever path (outside of priv->lock due to lock ordering) */ | |
723 | iucv_path_sever(path, ipuser); | |
724 | iucv_path_free(path); | |
725 | } | |
726 | ||
727 | /** | |
728 | * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message. | |
729 | * @path: Pending path (struct iucv_path) | |
730 | * @msg: Pointer to the IUCV message | |
731 | * | |
17e19f04 | 732 | * The function puts an incoming message on the input queue for later |
44a01d5b | 733 | * processing (by hvc_iucv_get_chars() / hvc_iucv_write()). |
17e19f04 | 734 | * If the tty has not yet been opened, the message is rejected. |
44a01d5b HB |
735 | * |
736 | * Locking: struct hvc_iucv_private->lock | |
737 | */ | |
738 | static void hvc_iucv_msg_pending(struct iucv_path *path, | |
739 | struct iucv_message *msg) | |
740 | { | |
741 | struct hvc_iucv_private *priv = path->private; | |
742 | struct iucv_tty_buffer *rb; | |
743 | ||
c45ce4b5 HB |
744 | /* reject messages that exceed max size of iucv_tty_msg->datalen */ |
745 | if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) { | |
746 | iucv_message_reject(path, msg); | |
747 | return; | |
748 | } | |
749 | ||
17e19f04 | 750 | |
44a01d5b HB |
751 | spin_lock(&priv->lock); |
752 | ||
753 | /* reject messages if tty has not yet been opened */ | |
754 | if (priv->tty_state == TTY_CLOSED) { | |
755 | iucv_message_reject(path, msg); | |
756 | goto unlock_return; | |
757 | } | |
758 | ||
c45ce4b5 | 759 | /* allocate tty buffer to save iucv msg only */ |
44a01d5b HB |
760 | rb = alloc_tty_buffer(0, GFP_ATOMIC); |
761 | if (!rb) { | |
762 | iucv_message_reject(path, msg); | |
763 | goto unlock_return; /* -ENOMEM */ | |
764 | } | |
765 | rb->msg = *msg; | |
766 | ||
767 | list_add_tail(&rb->list, &priv->tty_inqueue); | |
768 | ||
17e19f04 | 769 | hvc_kick(); /* wake up hvc thread */ |
44a01d5b HB |
770 | |
771 | unlock_return: | |
772 | spin_unlock(&priv->lock); | |
773 | } | |
774 | ||
775 | /** | |
776 | * hvc_iucv_msg_complete() - IUCV handler to process message completion | |
777 | * @path: Pending path (struct iucv_path) | |
778 | * @msg: Pointer to the IUCV message | |
779 | * | |
17e19f04 HB |
780 | * The function is called upon completion of message delivery to remove the |
781 | * message from the outqueue. Additional delivery information can be found | |
782 | * msg->audit: rejected messages (0x040000 (IPADRJCT)), and | |
783 | * purged messages (0x010000 (IPADPGNR)). | |
44a01d5b HB |
784 | * |
785 | * Locking: struct hvc_iucv_private->lock | |
786 | */ | |
787 | static void hvc_iucv_msg_complete(struct iucv_path *path, | |
788 | struct iucv_message *msg) | |
789 | { | |
790 | struct hvc_iucv_private *priv = path->private; | |
791 | struct iucv_tty_buffer *ent, *next; | |
792 | LIST_HEAD(list_remove); | |
793 | ||
794 | spin_lock(&priv->lock); | |
795 | list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list) | |
796 | if (ent->msg.id == msg->id) { | |
797 | list_move(&ent->list, &list_remove); | |
798 | break; | |
799 | } | |
c45ce4b5 | 800 | wake_up(&priv->sndbuf_waitq); |
44a01d5b HB |
801 | spin_unlock(&priv->lock); |
802 | destroy_tty_buffer_list(&list_remove); | |
803 | } | |
804 | ||
805 | ||
806 | /* HVC operations */ | |
807 | static struct hv_ops hvc_iucv_ops = { | |
808 | .get_chars = hvc_iucv_get_chars, | |
809 | .put_chars = hvc_iucv_put_chars, | |
810 | .notifier_add = hvc_iucv_notifier_add, | |
811 | .notifier_del = hvc_iucv_notifier_del, | |
812 | .notifier_hangup = hvc_iucv_notifier_hangup, | |
813 | }; | |
814 | ||
815 | /** | |
816 | * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance | |
817 | * @id: hvc_iucv_table index | |
818 | * | |
17e19f04 HB |
819 | * This function allocates a new hvc_iucv_private structure and stores |
820 | * the instance in hvc_iucv_table at index @id. | |
44a01d5b HB |
821 | * Returns 0 on success; otherwise non-zero. |
822 | */ | |
823 | static int __init hvc_iucv_alloc(int id) | |
824 | { | |
825 | struct hvc_iucv_private *priv; | |
826 | char name[9]; | |
827 | int rc; | |
828 | ||
829 | priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL); | |
830 | if (!priv) | |
831 | return -ENOMEM; | |
832 | ||
833 | spin_lock_init(&priv->lock); | |
834 | INIT_LIST_HEAD(&priv->tty_outqueue); | |
835 | INIT_LIST_HEAD(&priv->tty_inqueue); | |
c45ce4b5 HB |
836 | INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work); |
837 | init_waitqueue_head(&priv->sndbuf_waitq); | |
838 | ||
839 | priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL); | |
840 | if (!priv->sndbuf) { | |
841 | kfree(priv); | |
842 | return -ENOMEM; | |
843 | } | |
44a01d5b | 844 | |
17e19f04 | 845 | /* finally allocate hvc */ |
c45ce4b5 HB |
846 | priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */ |
847 | HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256); | |
44a01d5b HB |
848 | if (IS_ERR(priv->hvc)) { |
849 | rc = PTR_ERR(priv->hvc); | |
c45ce4b5 | 850 | free_page((unsigned long) priv->sndbuf); |
44a01d5b HB |
851 | kfree(priv); |
852 | return rc; | |
853 | } | |
854 | ||
17e19f04 | 855 | /* notify HVC thread instead of using polling */ |
c45ce4b5 HB |
856 | priv->hvc->irq_requested = 1; |
857 | ||
44a01d5b | 858 | /* setup iucv related information */ |
2dc184c0 | 859 | snprintf(name, 9, "lnxhvc%-2d", id); |
44a01d5b HB |
860 | memcpy(priv->srv_name, name, 8); |
861 | ASCEBC(priv->srv_name, 8); | |
862 | ||
863 | hvc_iucv_table[id] = priv; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | /** | |
17e19f04 | 868 | * hvc_iucv_init() - z/VM IUCV HVC device driver initialization |
44a01d5b HB |
869 | */ |
870 | static int __init hvc_iucv_init(void) | |
871 | { | |
872 | int rc, i; | |
873 | ||
874 | if (!MACHINE_IS_VM) { | |
c45ce4b5 HB |
875 | pr_info("The z/VM IUCV HVC device driver cannot " |
876 | "be used without z/VM\n"); | |
44a01d5b HB |
877 | return -ENODEV; |
878 | } | |
879 | ||
880 | if (!hvc_iucv_devices) | |
881 | return -ENODEV; | |
882 | ||
883 | if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) | |
884 | return -EINVAL; | |
885 | ||
886 | hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT, | |
887 | sizeof(struct iucv_tty_buffer), | |
888 | 0, 0, NULL); | |
889 | if (!hvc_iucv_buffer_cache) { | |
c45ce4b5 | 890 | pr_err("Allocating memory failed with reason code=%d\n", 1); |
44a01d5b HB |
891 | return -ENOMEM; |
892 | } | |
893 | ||
894 | hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR, | |
895 | hvc_iucv_buffer_cache); | |
896 | if (!hvc_iucv_mempool) { | |
c45ce4b5 | 897 | pr_err("Allocating memory failed with reason code=%d\n", 2); |
44a01d5b HB |
898 | kmem_cache_destroy(hvc_iucv_buffer_cache); |
899 | return -ENOMEM; | |
900 | } | |
901 | ||
68c6b3d2 HB |
902 | /* register the first terminal device as console |
903 | * (must be done before allocating hvc terminal devices) */ | |
904 | rc = hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops); | |
905 | if (rc) | |
906 | pr_warning("Registering HVC terminal device as " | |
907 | "Linux console failed\n"); | |
908 | ||
44a01d5b HB |
909 | /* allocate hvc_iucv_private structs */ |
910 | for (i = 0; i < hvc_iucv_devices; i++) { | |
911 | rc = hvc_iucv_alloc(i); | |
912 | if (rc) { | |
c45ce4b5 | 913 | pr_err("Creating a new HVC terminal device " |
17e19f04 | 914 | "failed with error code=%d\n", rc); |
44a01d5b HB |
915 | goto out_error_hvc; |
916 | } | |
917 | } | |
918 | ||
919 | /* register IUCV callback handler */ | |
920 | rc = iucv_register(&hvc_iucv_handler, 0); | |
921 | if (rc) { | |
c45ce4b5 HB |
922 | pr_err("Registering IUCV handlers failed with error code=%d\n", |
923 | rc); | |
44a01d5b HB |
924 | goto out_error_iucv; |
925 | } | |
926 | ||
927 | return 0; | |
928 | ||
929 | out_error_iucv: | |
930 | iucv_unregister(&hvc_iucv_handler, 0); | |
931 | out_error_hvc: | |
932 | for (i = 0; i < hvc_iucv_devices; i++) | |
933 | if (hvc_iucv_table[i]) { | |
934 | if (hvc_iucv_table[i]->hvc) | |
935 | hvc_remove(hvc_iucv_table[i]->hvc); | |
936 | kfree(hvc_iucv_table[i]); | |
937 | } | |
938 | mempool_destroy(hvc_iucv_mempool); | |
939 | kmem_cache_destroy(hvc_iucv_buffer_cache); | |
940 | return rc; | |
941 | } | |
942 | ||
44a01d5b HB |
943 | /** |
944 | * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter | |
945 | * @val: Parameter value (numeric) | |
946 | */ | |
947 | static int __init hvc_iucv_config(char *val) | |
948 | { | |
949 | return strict_strtoul(val, 10, &hvc_iucv_devices); | |
950 | } | |
951 | ||
952 | ||
68c6b3d2 | 953 | device_initcall(hvc_iucv_init); |
44a01d5b | 954 | __setup("hvc_iucv=", hvc_iucv_config); |