xref: /linux/drivers/s390/char/con3215.c (revision 2da68a77)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 3215 line mode terminal driver.
4  *
5  * Copyright IBM Corp. 1999, 2009
6  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7  *
8  * Updated:
9  *  Aug-2000: Added tab support
10  *	      Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
11  */
12 
13 #include <linux/types.h>
14 #include <linux/kdev_t.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/vt_kern.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/interrupt.h>
21 #include <linux/err.h>
22 #include <linux/panic_notifier.h>
23 #include <linux/reboot.h>
24 #include <linux/serial.h> /* ASYNC_* flags */
25 #include <linux/slab.h>
26 #include <asm/ccwdev.h>
27 #include <asm/cio.h>
28 #include <asm/io.h>
29 #include <asm/ebcdic.h>
30 #include <linux/uaccess.h>
31 #include <asm/delay.h>
32 #include <asm/cpcmd.h>
33 #include <asm/setup.h>
34 
35 #include "ctrlchar.h"
36 
37 #define NR_3215		    1
38 #define NR_3215_REQ	    (4*NR_3215)
39 #define RAW3215_BUFFER_SIZE 65536     /* output buffer size */
40 #define RAW3215_INBUF_SIZE  256	      /* input buffer size */
41 #define RAW3215_MIN_SPACE   128	      /* minimum free space for wakeup */
42 #define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */
43 #define RAW3215_MAX_BYTES   3968      /* max. bytes to write with one ssch */
44 #define RAW3215_MAX_NEWLINE 50	      /* max. lines to write with one ssch */
45 #define RAW3215_NR_CCWS	    3
46 #define RAW3215_TIMEOUT	    HZ/10     /* time for delayed output */
47 
48 #define RAW3215_FIXED	    1	      /* 3215 console device is not be freed */
49 #define RAW3215_WORKING	    4	      /* set if a request is being worked on */
50 #define RAW3215_THROTTLED   8	      /* set if reading is disabled */
51 #define RAW3215_STOPPED	    16	      /* set if writing is disabled */
52 #define RAW3215_TIMER_RUNS  64	      /* set if the output delay timer is on */
53 #define RAW3215_FLUSHING    128	      /* set to flush buffer (no delay) */
54 
55 #define TAB_STOP_SIZE	    8	      /* tab stop size */
56 
57 /*
58  * Request types for a 3215 device
59  */
60 enum raw3215_type {
61 	RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
62 };
63 
64 /*
65  * Request structure for a 3215 device
66  */
67 struct raw3215_req {
68 	enum raw3215_type type;	      /* type of the request */
69 	int start, len;		      /* start index & len in output buffer */
70 	int delayable;		      /* indication to wait for more data */
71 	int residual;		      /* residual count for read request */
72 	struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
73 	struct raw3215_info *info;    /* pointer to main structure */
74 	struct raw3215_req *next;     /* pointer to next request */
75 } __attribute__ ((aligned(8)));
76 
77 struct raw3215_info {
78 	struct tty_port port;
79 	struct ccw_device *cdev;      /* device for tty driver */
80 	spinlock_t *lock;	      /* pointer to irq lock */
81 	int flags;		      /* state flags */
82 	char *buffer;		      /* pointer to output buffer */
83 	char *inbuf;		      /* pointer to input buffer */
84 	int head;		      /* first free byte in output buffer */
85 	int count;		      /* number of bytes in output buffer */
86 	int written;		      /* number of bytes in write requests */
87 	struct raw3215_req *queued_read; /* pointer to queued read requests */
88 	struct raw3215_req *queued_write;/* pointer to queued write requests */
89 	wait_queue_head_t empty_wait; /* wait queue for flushing */
90 	struct timer_list timer;      /* timer for delayed output */
91 	int line_pos;		      /* position on the line (for tabs) */
92 	char ubuffer[80];	      /* copy_from_user buffer */
93 };
94 
95 /* array of 3215 devices structures */
96 static struct raw3215_info *raw3215[NR_3215];
97 /* spinlock to protect the raw3215 array */
98 static DEFINE_SPINLOCK(raw3215_device_lock);
99 /* list of free request structures */
100 static struct raw3215_req *raw3215_freelist;
101 /* spinlock to protect free list */
102 static DEFINE_SPINLOCK(raw3215_freelist_lock);
103 
104 static struct tty_driver *tty3215_driver;
105 static bool con3215_drop = true;
106 
107 /*
108  * Get a request structure from the free list
109  */
110 static inline struct raw3215_req *raw3215_alloc_req(void)
111 {
112 	struct raw3215_req *req;
113 	unsigned long flags;
114 
115 	spin_lock_irqsave(&raw3215_freelist_lock, flags);
116 	req = raw3215_freelist;
117 	raw3215_freelist = req->next;
118 	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
119 	return req;
120 }
121 
122 /*
123  * Put a request structure back to the free list
124  */
125 static inline void raw3215_free_req(struct raw3215_req *req)
126 {
127 	unsigned long flags;
128 
129 	if (req->type == RAW3215_FREE)
130 		return;		/* don't free a free request */
131 	req->type = RAW3215_FREE;
132 	spin_lock_irqsave(&raw3215_freelist_lock, flags);
133 	req->next = raw3215_freelist;
134 	raw3215_freelist = req;
135 	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
136 }
137 
138 /*
139  * Set up a read request that reads up to 160 byte from the 3215 device.
140  * If there is a queued read request it is used, but that shouldn't happen
141  * because a 3215 terminal won't accept a new read before the old one is
142  * completed.
143  */
144 static void raw3215_mk_read_req(struct raw3215_info *raw)
145 {
146 	struct raw3215_req *req;
147 	struct ccw1 *ccw;
148 
149 	/* there can only be ONE read request at a time */
150 	req = raw->queued_read;
151 	if (req == NULL) {
152 		/* no queued read request, use new req structure */
153 		req = raw3215_alloc_req();
154 		req->type = RAW3215_READ;
155 		req->info = raw;
156 		raw->queued_read = req;
157 	}
158 
159 	ccw = req->ccws;
160 	ccw->cmd_code = 0x0A; /* read inquiry */
161 	ccw->flags = 0x20;    /* ignore incorrect length */
162 	ccw->count = 160;
163 	ccw->cda = (__u32)__pa(raw->inbuf);
164 }
165 
166 /*
167  * Set up a write request with the information from the main structure.
168  * A ccw chain is created that writes as much as possible from the output
169  * buffer to the 3215 device. If a queued write exists it is replaced by
170  * the new, probably lengthened request.
171  */
172 static void raw3215_mk_write_req(struct raw3215_info *raw)
173 {
174 	struct raw3215_req *req;
175 	struct ccw1 *ccw;
176 	int len, count, ix, lines;
177 
178 	if (raw->count <= raw->written)
179 		return;
180 	/* check if there is a queued write request */
181 	req = raw->queued_write;
182 	if (req == NULL) {
183 		/* no queued write request, use new req structure */
184 		req = raw3215_alloc_req();
185 		req->type = RAW3215_WRITE;
186 		req->info = raw;
187 		raw->queued_write = req;
188 	} else {
189 		raw->written -= req->len;
190 	}
191 
192 	ccw = req->ccws;
193 	req->start = (raw->head - raw->count + raw->written) &
194 		     (RAW3215_BUFFER_SIZE - 1);
195 	/*
196 	 * now we have to count newlines. We can at max accept
197 	 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
198 	 * a restriction in VM
199 	 */
200 	lines = 0;
201 	ix = req->start;
202 	while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
203 		if (raw->buffer[ix] == 0x15)
204 			lines++;
205 		ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
206 	}
207 	len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
208 	if (len > RAW3215_MAX_BYTES)
209 		len = RAW3215_MAX_BYTES;
210 	req->len = len;
211 	raw->written += len;
212 
213 	/* set the indication if we should try to enlarge this request */
214 	req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
215 
216 	ix = req->start;
217 	while (len > 0) {
218 		if (ccw > req->ccws)
219 			ccw[-1].flags |= 0x40; /* use command chaining */
220 		ccw->cmd_code = 0x01; /* write, auto carrier return */
221 		ccw->flags = 0x20;    /* ignore incorrect length ind.  */
222 		ccw->cda = (__u32)__pa(raw->buffer + ix);
223 		count = len;
224 		if (ix + count > RAW3215_BUFFER_SIZE)
225 			count = RAW3215_BUFFER_SIZE - ix;
226 		ccw->count = count;
227 		len -= count;
228 		ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
229 		ccw++;
230 	}
231 	/*
232 	 * Add a NOP to the channel program. 3215 devices are purely
233 	 * emulated and its much better to avoid the channel end
234 	 * interrupt in this case.
235 	 */
236 	if (ccw > req->ccws)
237 		ccw[-1].flags |= 0x40; /* use command chaining */
238 	ccw->cmd_code = 0x03; /* NOP */
239 	ccw->flags = 0;
240 	ccw->cda = 0;
241 	ccw->count = 1;
242 }
243 
244 /*
245  * Start a read or a write request
246  */
247 static void raw3215_start_io(struct raw3215_info *raw)
248 {
249 	struct raw3215_req *req;
250 	int res;
251 
252 	req = raw->queued_read;
253 	if (req != NULL &&
254 	    !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
255 		/* dequeue request */
256 		raw->queued_read = NULL;
257 		res = ccw_device_start(raw->cdev, req->ccws,
258 				       (unsigned long) req, 0, 0);
259 		if (res != 0) {
260 			/* do_IO failed, put request back to queue */
261 			raw->queued_read = req;
262 		} else {
263 			raw->flags |= RAW3215_WORKING;
264 		}
265 	}
266 	req = raw->queued_write;
267 	if (req != NULL &&
268 	    !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
269 		/* dequeue request */
270 		raw->queued_write = NULL;
271 		res = ccw_device_start(raw->cdev, req->ccws,
272 				       (unsigned long) req, 0, 0);
273 		if (res != 0) {
274 			/* do_IO failed, put request back to queue */
275 			raw->queued_write = req;
276 		} else {
277 			raw->flags |= RAW3215_WORKING;
278 		}
279 	}
280 }
281 
282 /*
283  * Function to start a delayed output after RAW3215_TIMEOUT seconds
284  */
285 static void raw3215_timeout(struct timer_list *t)
286 {
287 	struct raw3215_info *raw = from_timer(raw, t, timer);
288 	unsigned long flags;
289 
290 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
291 	raw->flags &= ~RAW3215_TIMER_RUNS;
292 	raw3215_mk_write_req(raw);
293 	raw3215_start_io(raw);
294 	if ((raw->queued_read || raw->queued_write) &&
295 	    !(raw->flags & RAW3215_WORKING) &&
296 	    !(raw->flags & RAW3215_TIMER_RUNS)) {
297 		raw->timer.expires = RAW3215_TIMEOUT + jiffies;
298 		add_timer(&raw->timer);
299 		raw->flags |= RAW3215_TIMER_RUNS;
300 	}
301 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
302 }
303 
304 /*
305  * Function to conditionally start an IO. A read is started immediately,
306  * a write is only started immediately if the flush flag is on or the
307  * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
308  * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
309  */
310 static inline void raw3215_try_io(struct raw3215_info *raw)
311 {
312 	if (!tty_port_initialized(&raw->port))
313 		return;
314 	if (raw->queued_read != NULL)
315 		raw3215_start_io(raw);
316 	else if (raw->queued_write != NULL) {
317 		if ((raw->queued_write->delayable == 0) ||
318 		    (raw->flags & RAW3215_FLUSHING)) {
319 			/* execute write requests bigger than minimum size */
320 			raw3215_start_io(raw);
321 		}
322 	}
323 	if ((raw->queued_read || raw->queued_write) &&
324 	    !(raw->flags & RAW3215_WORKING) &&
325 	    !(raw->flags & RAW3215_TIMER_RUNS)) {
326 		raw->timer.expires = RAW3215_TIMEOUT + jiffies;
327 		add_timer(&raw->timer);
328 		raw->flags |= RAW3215_TIMER_RUNS;
329 	}
330 }
331 
332 /*
333  * Try to start the next IO and wake up processes waiting on the tty.
334  */
335 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
336 {
337 	raw3215_mk_write_req(raw);
338 	raw3215_try_io(raw);
339 	if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
340 		tty_wakeup(tty);
341 }
342 
343 /*
344  * Interrupt routine, called from common io layer
345  */
346 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
347 			struct irb *irb)
348 {
349 	struct raw3215_info *raw;
350 	struct raw3215_req *req;
351 	struct tty_struct *tty;
352 	int cstat, dstat;
353 	int count;
354 
355 	raw = dev_get_drvdata(&cdev->dev);
356 	req = (struct raw3215_req *) intparm;
357 	tty = tty_port_tty_get(&raw->port);
358 	cstat = irb->scsw.cmd.cstat;
359 	dstat = irb->scsw.cmd.dstat;
360 	if (cstat != 0)
361 		raw3215_next_io(raw, tty);
362 	if (dstat & 0x01) { /* we got a unit exception */
363 		dstat &= ~0x01;	 /* we can ignore it */
364 	}
365 	switch (dstat) {
366 	case 0x80:
367 		if (cstat != 0)
368 			break;
369 		/* Attention interrupt, someone hit the enter key */
370 		raw3215_mk_read_req(raw);
371 		raw3215_next_io(raw, tty);
372 		break;
373 	case 0x08:
374 	case 0x0C:
375 		/* Channel end interrupt. */
376 		if ((raw = req->info) == NULL)
377 			goto put_tty;	     /* That shouldn't happen ... */
378 		if (req->type == RAW3215_READ) {
379 			/* store residual count, then wait for device end */
380 			req->residual = irb->scsw.cmd.count;
381 		}
382 		if (dstat == 0x08)
383 			break;
384 		fallthrough;
385 	case 0x04:
386 		/* Device end interrupt. */
387 		if ((raw = req->info) == NULL)
388 			goto put_tty;	     /* That shouldn't happen ... */
389 		if (req->type == RAW3215_READ && tty != NULL) {
390 			unsigned int cchar;
391 
392 			count = 160 - req->residual;
393 			EBCASC(raw->inbuf, count);
394 			cchar = ctrlchar_handle(raw->inbuf, count, tty);
395 			switch (cchar & CTRLCHAR_MASK) {
396 			case CTRLCHAR_SYSRQ:
397 				break;
398 
399 			case CTRLCHAR_CTRL:
400 				tty_insert_flip_char(&raw->port, cchar,
401 						TTY_NORMAL);
402 				tty_flip_buffer_push(&raw->port);
403 				break;
404 
405 			case CTRLCHAR_NONE:
406 				if (count < 2 ||
407 				    (strncmp(raw->inbuf+count-2, "\252n", 2) &&
408 				     strncmp(raw->inbuf+count-2, "^n", 2)) ) {
409 					/* add the auto \n */
410 					raw->inbuf[count] = '\n';
411 					count++;
412 				} else
413 					count -= 2;
414 				tty_insert_flip_string(&raw->port, raw->inbuf,
415 						count);
416 				tty_flip_buffer_push(&raw->port);
417 				break;
418 			}
419 		} else if (req->type == RAW3215_WRITE) {
420 			raw->count -= req->len;
421 			raw->written -= req->len;
422 		}
423 		raw->flags &= ~RAW3215_WORKING;
424 		raw3215_free_req(req);
425 		/* check for empty wait */
426 		if (waitqueue_active(&raw->empty_wait) &&
427 		    raw->queued_write == NULL &&
428 		    raw->queued_read == NULL) {
429 			wake_up_interruptible(&raw->empty_wait);
430 		}
431 		raw3215_next_io(raw, tty);
432 		break;
433 	default:
434 		/* Strange interrupt, I'll do my best to clean up */
435 		if (req != NULL && req->type != RAW3215_FREE) {
436 			if (req->type == RAW3215_WRITE) {
437 				raw->count -= req->len;
438 				raw->written -= req->len;
439 			}
440 			raw->flags &= ~RAW3215_WORKING;
441 			raw3215_free_req(req);
442 		}
443 		raw3215_next_io(raw, tty);
444 	}
445 put_tty:
446 	tty_kref_put(tty);
447 }
448 
449 /*
450  * Need to drop data to avoid blocking. Drop as much data as possible.
451  * This is unqueued part in the buffer and the queued part in the request.
452  * Also adjust the head position to append new data and set count
453  * accordingly.
454  *
455  * Return number of bytes available in buffer.
456  */
457 static unsigned int raw3215_drop(struct raw3215_info *raw)
458 {
459 	struct raw3215_req *req;
460 
461 	req = raw->queued_write;
462 	if (req) {
463 		/* Drop queued data and delete request */
464 		raw->written -= req->len;
465 		raw3215_free_req(req);
466 		raw->queued_write = NULL;
467 	}
468 	raw->head = (raw->head - raw->count + raw->written) &
469 		    (RAW3215_BUFFER_SIZE - 1);
470 	raw->count = raw->written;
471 
472 	return RAW3215_BUFFER_SIZE - raw->count;
473 }
474 
475 /*
476  * Wait until length bytes are available int the output buffer.
477  * If drop mode is active and wait condition holds true, start dropping
478  * data.
479  * Has to be called with the s390irq lock held. Can be called
480  * disabled.
481  */
482 static unsigned int raw3215_make_room(struct raw3215_info *raw,
483 				      unsigned int length, bool drop)
484 {
485 	while (RAW3215_BUFFER_SIZE - raw->count < length) {
486 		if (drop)
487 			return raw3215_drop(raw);
488 
489 		/* there might be a request pending */
490 		raw->flags |= RAW3215_FLUSHING;
491 		raw3215_mk_write_req(raw);
492 		raw3215_try_io(raw);
493 		raw->flags &= ~RAW3215_FLUSHING;
494 #ifdef CONFIG_TN3215_CONSOLE
495 		ccw_device_wait_idle(raw->cdev);
496 #endif
497 		/* Enough room freed up ? */
498 		if (RAW3215_BUFFER_SIZE - raw->count >= length)
499 			break;
500 		/* there might be another cpu waiting for the lock */
501 		spin_unlock(get_ccwdev_lock(raw->cdev));
502 		udelay(100);
503 		spin_lock(get_ccwdev_lock(raw->cdev));
504 	}
505 	return length;
506 }
507 
508 #define	RAW3215_COUNT	1
509 #define	RAW3215_STORE	2
510 
511 /*
512  * Add text to console buffer. Find tabs in input and calculate size
513  * including tab replacement.
514  * This function operates in 2 different modes, depending on parameter
515  * opmode:
516  * RAW3215_COUNT: Get the size needed for the input string with
517  *	proper tab replacement calculation.
518  *	Return value is the number of bytes required to store the
519  *	input. However no data is actually stored.
520  *	The parameter todrop is not used.
521  * RAW3215_STORE: Add data to the console buffer. The parameter todrop is
522  *	valid and contains the number of bytes to be dropped from head of
523  *	string	without blocking.
524  *	Return value is the number of bytes copied.
525  */
526 static unsigned int raw3215_addtext(const char *str, unsigned int length,
527 				    struct raw3215_info *raw, int opmode,
528 				    unsigned int todrop)
529 {
530 	unsigned int c, ch, i, blanks, expanded_size = 0;
531 	unsigned int column = raw->line_pos;
532 
533 	if (opmode == RAW3215_COUNT)
534 		todrop = 0;
535 
536 	for (c = 0; c < length; ++c) {
537 		blanks = 1;
538 		ch = str[c];
539 
540 		switch (ch) {
541 		case '\n':
542 			expanded_size++;
543 			column = 0;
544 			break;
545 		case '\t':
546 			blanks = TAB_STOP_SIZE - (column % TAB_STOP_SIZE);
547 			column += blanks;
548 			expanded_size += blanks;
549 			ch = ' ';
550 			break;
551 		default:
552 			expanded_size++;
553 			column++;
554 			break;
555 		}
556 
557 		if (opmode == RAW3215_COUNT)
558 			continue;
559 		if (todrop && expanded_size < todrop)	/* Drop head data */
560 			continue;
561 		for (i = 0; i < blanks; i++) {
562 			raw->buffer[raw->head] = (char)_ascebc[(int)ch];
563 			raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
564 			raw->count++;
565 		}
566 		raw->line_pos = column;
567 	}
568 	return expanded_size - todrop;
569 }
570 
571 /*
572  * String write routine for 3215 devices
573  */
574 static void raw3215_write(struct raw3215_info *raw, const char *str,
575 			  unsigned int length)
576 {
577 	unsigned int count, avail;
578 	unsigned long flags;
579 
580 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
581 
582 	count = raw3215_addtext(str, length, raw, RAW3215_COUNT, 0);
583 
584 	avail = raw3215_make_room(raw, count, con3215_drop);
585 	if (avail) {
586 		raw3215_addtext(str, length, raw, RAW3215_STORE,
587 				count - avail);
588 	}
589 	if (!(raw->flags & RAW3215_WORKING)) {
590 		raw3215_mk_write_req(raw);
591 		/* start or queue request */
592 		raw3215_try_io(raw);
593 	}
594 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
595 }
596 
597 /*
598  * Put character routine for 3215 devices
599  */
600 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
601 {
602 	raw3215_write(raw, &ch, 1);
603 }
604 
605 /*
606  * Flush routine, it simply sets the flush flag and tries to start
607  * pending IO.
608  */
609 static void raw3215_flush_buffer(struct raw3215_info *raw)
610 {
611 	unsigned long flags;
612 
613 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
614 	if (raw->count > 0) {
615 		raw->flags |= RAW3215_FLUSHING;
616 		raw3215_try_io(raw);
617 		raw->flags &= ~RAW3215_FLUSHING;
618 	}
619 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
620 }
621 
622 /*
623  * Fire up a 3215 device.
624  */
625 static int raw3215_startup(struct raw3215_info *raw)
626 {
627 	unsigned long flags;
628 
629 	if (tty_port_initialized(&raw->port))
630 		return 0;
631 	raw->line_pos = 0;
632 	tty_port_set_initialized(&raw->port, 1);
633 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
634 	raw3215_try_io(raw);
635 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
636 
637 	return 0;
638 }
639 
640 /*
641  * Shutdown a 3215 device.
642  */
643 static void raw3215_shutdown(struct raw3215_info *raw)
644 {
645 	DECLARE_WAITQUEUE(wait, current);
646 	unsigned long flags;
647 
648 	if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
649 		return;
650 	/* Wait for outstanding requests, then free irq */
651 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
652 	if ((raw->flags & RAW3215_WORKING) ||
653 	    raw->queued_write != NULL ||
654 	    raw->queued_read != NULL) {
655 		add_wait_queue(&raw->empty_wait, &wait);
656 		set_current_state(TASK_INTERRUPTIBLE);
657 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
658 		schedule();
659 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
660 		remove_wait_queue(&raw->empty_wait, &wait);
661 		set_current_state(TASK_RUNNING);
662 		tty_port_set_initialized(&raw->port, 1);
663 	}
664 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
665 }
666 
667 static struct raw3215_info *raw3215_alloc_info(void)
668 {
669 	struct raw3215_info *info;
670 
671 	info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
672 	if (!info)
673 		return NULL;
674 
675 	info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
676 	info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
677 	if (!info->buffer || !info->inbuf) {
678 		kfree(info->inbuf);
679 		kfree(info->buffer);
680 		kfree(info);
681 		return NULL;
682 	}
683 
684 	timer_setup(&info->timer, raw3215_timeout, 0);
685 	init_waitqueue_head(&info->empty_wait);
686 	tty_port_init(&info->port);
687 
688 	return info;
689 }
690 
691 static void raw3215_free_info(struct raw3215_info *raw)
692 {
693 	kfree(raw->inbuf);
694 	kfree(raw->buffer);
695 	tty_port_destroy(&raw->port);
696 	kfree(raw);
697 }
698 
699 static int raw3215_probe(struct ccw_device *cdev)
700 {
701 	struct raw3215_info *raw;
702 	int line;
703 
704 	/* Console is special. */
705 	if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
706 		return 0;
707 
708 	raw = raw3215_alloc_info();
709 	if (raw == NULL)
710 		return -ENOMEM;
711 
712 	raw->cdev = cdev;
713 	dev_set_drvdata(&cdev->dev, raw);
714 	cdev->handler = raw3215_irq;
715 
716 	spin_lock(&raw3215_device_lock);
717 	for (line = 0; line < NR_3215; line++) {
718 		if (!raw3215[line]) {
719 			raw3215[line] = raw;
720 			break;
721 		}
722 	}
723 	spin_unlock(&raw3215_device_lock);
724 	if (line == NR_3215) {
725 		raw3215_free_info(raw);
726 		return -ENODEV;
727 	}
728 
729 	return 0;
730 }
731 
732 static void raw3215_remove(struct ccw_device *cdev)
733 {
734 	struct raw3215_info *raw;
735 	unsigned int line;
736 
737 	ccw_device_set_offline(cdev);
738 	raw = dev_get_drvdata(&cdev->dev);
739 	if (raw) {
740 		spin_lock(&raw3215_device_lock);
741 		for (line = 0; line < NR_3215; line++)
742 			if (raw3215[line] == raw)
743 				break;
744 		raw3215[line] = NULL;
745 		spin_unlock(&raw3215_device_lock);
746 		dev_set_drvdata(&cdev->dev, NULL);
747 		raw3215_free_info(raw);
748 	}
749 }
750 
751 static int raw3215_set_online(struct ccw_device *cdev)
752 {
753 	struct raw3215_info *raw;
754 
755 	raw = dev_get_drvdata(&cdev->dev);
756 	if (!raw)
757 		return -ENODEV;
758 
759 	return raw3215_startup(raw);
760 }
761 
762 static int raw3215_set_offline(struct ccw_device *cdev)
763 {
764 	struct raw3215_info *raw;
765 
766 	raw = dev_get_drvdata(&cdev->dev);
767 	if (!raw)
768 		return -ENODEV;
769 
770 	raw3215_shutdown(raw);
771 
772 	return 0;
773 }
774 
775 static struct ccw_device_id raw3215_id[] = {
776 	{ CCW_DEVICE(0x3215, 0) },
777 	{ /* end of list */ },
778 };
779 
780 static ssize_t con_drop_store(struct device_driver *dev, const char *buf, size_t count)
781 {
782 	bool drop;
783 	int rc;
784 
785 	rc = kstrtobool(buf, &drop);
786 	if (!rc)
787 		con3215_drop = drop;
788 	return rc ?: count;
789 }
790 
791 static ssize_t con_drop_show(struct device_driver *dev, char *buf)
792 {
793 	return sysfs_emit(buf, "%d\n", con3215_drop ? 1 : 0);
794 }
795 
796 static DRIVER_ATTR_RW(con_drop);
797 
798 static struct attribute *con3215_drv_attrs[] = {
799 	&driver_attr_con_drop.attr,
800 	NULL,
801 };
802 
803 static struct attribute_group con3215_drv_attr_group = {
804 	.attrs = con3215_drv_attrs,
805 	NULL,
806 };
807 
808 static const struct attribute_group *con3215_drv_attr_groups[] = {
809 	&con3215_drv_attr_group,
810 	NULL,
811 };
812 
813 static struct ccw_driver raw3215_ccw_driver = {
814 	.driver = {
815 		.name	= "3215",
816 		.groups = con3215_drv_attr_groups,
817 		.owner	= THIS_MODULE,
818 	},
819 	.ids		= raw3215_id,
820 	.probe		= &raw3215_probe,
821 	.remove		= &raw3215_remove,
822 	.set_online	= &raw3215_set_online,
823 	.set_offline	= &raw3215_set_offline,
824 	.int_class	= IRQIO_C15,
825 };
826 
827 static void handle_write(struct raw3215_info *raw, const char *str, int count)
828 {
829 	int i;
830 
831 	while (count > 0) {
832 		i = min_t(int, count, RAW3215_BUFFER_SIZE - 1);
833 		raw3215_write(raw, str, i);
834 		count -= i;
835 		str += i;
836 	}
837 }
838 
839 #ifdef CONFIG_TN3215_CONSOLE
840 /*
841  * Write a string to the 3215 console
842  */
843 static void con3215_write(struct console *co, const char *str, unsigned int count)
844 {
845 	handle_write(raw3215[0], str, count);
846 }
847 
848 static struct tty_driver *con3215_device(struct console *c, int *index)
849 {
850 	*index = c->index;
851 	return tty3215_driver;
852 }
853 
854 /*
855  * The below function is called as a panic/reboot notifier before the
856  * system enters a disabled, endless loop.
857  *
858  * Notice we must use the spin_trylock() alternative, to prevent lockups
859  * in atomic context (panic routine runs with secondary CPUs, local IRQs
860  * and preemption disabled).
861  */
862 static int con3215_notify(struct notifier_block *self,
863 			  unsigned long event, void *data)
864 {
865 	struct raw3215_info *raw;
866 	unsigned long flags;
867 
868 	raw = raw3215[0];  /* console 3215 is the first one */
869 	if (!spin_trylock_irqsave(get_ccwdev_lock(raw->cdev), flags))
870 		return NOTIFY_DONE;
871 	raw3215_make_room(raw, RAW3215_BUFFER_SIZE, false);
872 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
873 
874 	return NOTIFY_DONE;
875 }
876 
877 static struct notifier_block on_panic_nb = {
878 	.notifier_call = con3215_notify,
879 	.priority = INT_MIN + 1, /* run the callback late */
880 };
881 
882 static struct notifier_block on_reboot_nb = {
883 	.notifier_call = con3215_notify,
884 	.priority = INT_MIN + 1, /* run the callback late */
885 };
886 
887 /*
888  *  The console structure for the 3215 console
889  */
890 static struct console con3215 = {
891 	.name	 = "ttyS",
892 	.write	 = con3215_write,
893 	.device	 = con3215_device,
894 	.flags	 = CON_PRINTBUFFER,
895 };
896 
897 /*
898  * 3215 console initialization code called from console_init().
899  */
900 static int __init con3215_init(void)
901 {
902 	struct ccw_device *cdev;
903 	struct raw3215_info *raw;
904 	struct raw3215_req *req;
905 	int i;
906 
907 	/* Check if 3215 is to be the console */
908 	if (!CONSOLE_IS_3215)
909 		return -ENODEV;
910 
911 	/* Set the console mode for VM */
912 	if (MACHINE_IS_VM) {
913 		cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
914 		cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
915 	}
916 
917 	/* allocate 3215 request structures */
918 	raw3215_freelist = NULL;
919 	for (i = 0; i < NR_3215_REQ; i++) {
920 		req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
921 		if (!req)
922 			return -ENOMEM;
923 		req->next = raw3215_freelist;
924 		raw3215_freelist = req;
925 	}
926 
927 	cdev = ccw_device_create_console(&raw3215_ccw_driver);
928 	if (IS_ERR(cdev))
929 		return -ENODEV;
930 
931 	raw3215[0] = raw = raw3215_alloc_info();
932 	raw->cdev = cdev;
933 	dev_set_drvdata(&cdev->dev, raw);
934 	cdev->handler = raw3215_irq;
935 
936 	raw->flags |= RAW3215_FIXED;
937 	if (ccw_device_enable_console(cdev)) {
938 		ccw_device_destroy_console(cdev);
939 		raw3215_free_info(raw);
940 		raw3215[0] = NULL;
941 		return -ENODEV;
942 	}
943 
944 	/* Request the console irq */
945 	if (raw3215_startup(raw) != 0) {
946 		raw3215_free_info(raw);
947 		raw3215[0] = NULL;
948 		return -ENODEV;
949 	}
950 	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
951 	register_reboot_notifier(&on_reboot_nb);
952 	register_console(&con3215);
953 	return 0;
954 }
955 console_initcall(con3215_init);
956 #endif
957 
958 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
959 {
960 	struct raw3215_info *raw;
961 
962 	raw = raw3215[tty->index];
963 	if (raw == NULL)
964 		return -ENODEV;
965 
966 	tty->driver_data = raw;
967 
968 	return tty_port_install(&raw->port, driver, tty);
969 }
970 
971 /*
972  * tty3215_open
973  *
974  * This routine is called whenever a 3215 tty is opened.
975  */
976 static int tty3215_open(struct tty_struct *tty, struct file * filp)
977 {
978 	struct raw3215_info *raw = tty->driver_data;
979 
980 	tty_port_tty_set(&raw->port, tty);
981 
982 	/*
983 	 * Start up 3215 device
984 	 */
985 	return raw3215_startup(raw);
986 }
987 
988 /*
989  * tty3215_close()
990  *
991  * This routine is called when the 3215 tty is closed. We wait
992  * for the remaining request to be completed. Then we clean up.
993  */
994 static void tty3215_close(struct tty_struct *tty, struct file * filp)
995 {
996 	struct raw3215_info *raw = tty->driver_data;
997 
998 	if (raw == NULL || tty->count > 1)
999 		return;
1000 	tty->closing = 1;
1001 	/* Shutdown the terminal */
1002 	raw3215_shutdown(raw);
1003 	tty->closing = 0;
1004 	tty_port_tty_set(&raw->port, NULL);
1005 }
1006 
1007 /*
1008  * Returns the amount of free space in the output buffer.
1009  */
1010 static unsigned int tty3215_write_room(struct tty_struct *tty)
1011 {
1012 	struct raw3215_info *raw = tty->driver_data;
1013 
1014 	/* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
1015 	if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
1016 		return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
1017 	else
1018 		return 0;
1019 }
1020 
1021 /*
1022  * String write routine for 3215 ttys
1023  */
1024 static int tty3215_write(struct tty_struct *tty,
1025 			 const unsigned char *buf, int count)
1026 {
1027 	handle_write(tty->driver_data, buf, count);
1028 	return count;
1029 }
1030 
1031 /*
1032  * Put character routine for 3215 ttys
1033  */
1034 static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
1035 {
1036 	struct raw3215_info *raw = tty->driver_data;
1037 
1038 	raw3215_putchar(raw, ch);
1039 
1040 	return 1;
1041 }
1042 
1043 static void tty3215_flush_chars(struct tty_struct *tty)
1044 {
1045 }
1046 
1047 /*
1048  * Returns the number of characters in the output buffer
1049  */
1050 static unsigned int tty3215_chars_in_buffer(struct tty_struct *tty)
1051 {
1052 	struct raw3215_info *raw = tty->driver_data;
1053 
1054 	return raw->count;
1055 }
1056 
1057 static void tty3215_flush_buffer(struct tty_struct *tty)
1058 {
1059 	struct raw3215_info *raw = tty->driver_data;
1060 
1061 	raw3215_flush_buffer(raw);
1062 	tty_wakeup(tty);
1063 }
1064 
1065 /*
1066  * Disable reading from a 3215 tty
1067  */
1068 static void tty3215_throttle(struct tty_struct *tty)
1069 {
1070 	struct raw3215_info *raw = tty->driver_data;
1071 
1072 	raw->flags |= RAW3215_THROTTLED;
1073 }
1074 
1075 /*
1076  * Enable reading from a 3215 tty
1077  */
1078 static void tty3215_unthrottle(struct tty_struct *tty)
1079 {
1080 	struct raw3215_info *raw = tty->driver_data;
1081 	unsigned long flags;
1082 
1083 	if (raw->flags & RAW3215_THROTTLED) {
1084 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1085 		raw->flags &= ~RAW3215_THROTTLED;
1086 		raw3215_try_io(raw);
1087 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1088 	}
1089 }
1090 
1091 /*
1092  * Disable writing to a 3215 tty
1093  */
1094 static void tty3215_stop(struct tty_struct *tty)
1095 {
1096 	struct raw3215_info *raw = tty->driver_data;
1097 
1098 	raw->flags |= RAW3215_STOPPED;
1099 }
1100 
1101 /*
1102  * Enable writing to a 3215 tty
1103  */
1104 static void tty3215_start(struct tty_struct *tty)
1105 {
1106 	struct raw3215_info *raw = tty->driver_data;
1107 	unsigned long flags;
1108 
1109 	if (raw->flags & RAW3215_STOPPED) {
1110 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1111 		raw->flags &= ~RAW3215_STOPPED;
1112 		raw3215_try_io(raw);
1113 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1114 	}
1115 }
1116 
1117 static const struct tty_operations tty3215_ops = {
1118 	.install = tty3215_install,
1119 	.open = tty3215_open,
1120 	.close = tty3215_close,
1121 	.write = tty3215_write,
1122 	.put_char = tty3215_put_char,
1123 	.flush_chars = tty3215_flush_chars,
1124 	.write_room = tty3215_write_room,
1125 	.chars_in_buffer = tty3215_chars_in_buffer,
1126 	.flush_buffer = tty3215_flush_buffer,
1127 	.throttle = tty3215_throttle,
1128 	.unthrottle = tty3215_unthrottle,
1129 	.stop = tty3215_stop,
1130 	.start = tty3215_start,
1131 };
1132 
1133 static int __init con3215_setup_drop(char *str)
1134 {
1135 	bool drop;
1136 	int rc;
1137 
1138 	rc = kstrtobool(str, &drop);
1139 	if (!rc)
1140 		con3215_drop = drop;
1141 	return rc;
1142 }
1143 early_param("con3215_drop", con3215_setup_drop);
1144 
1145 /*
1146  * 3215 tty registration code called from tty_init().
1147  * Most kernel services (incl. kmalloc) are available at this poimt.
1148  */
1149 static int __init tty3215_init(void)
1150 {
1151 	struct tty_driver *driver;
1152 	int ret;
1153 
1154 	if (!CONSOLE_IS_3215)
1155 		return 0;
1156 
1157 	driver = tty_alloc_driver(NR_3215, TTY_DRIVER_REAL_RAW);
1158 	if (IS_ERR(driver))
1159 		return PTR_ERR(driver);
1160 
1161 	ret = ccw_driver_register(&raw3215_ccw_driver);
1162 	if (ret) {
1163 		tty_driver_kref_put(driver);
1164 		return ret;
1165 	}
1166 	/*
1167 	 * Initialize the tty_driver structure
1168 	 * Entries in tty3215_driver that are NOT initialized:
1169 	 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1170 	 */
1171 
1172 	driver->driver_name = "tty3215";
1173 	driver->name = "ttyS";
1174 	driver->major = TTY_MAJOR;
1175 	driver->minor_start = 64;
1176 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
1177 	driver->subtype = SYSTEM_TYPE_TTY;
1178 	driver->init_termios = tty_std_termios;
1179 	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1180 	driver->init_termios.c_oflag = ONLCR;
1181 	driver->init_termios.c_lflag = ISIG;
1182 	tty_set_operations(driver, &tty3215_ops);
1183 	ret = tty_register_driver(driver);
1184 	if (ret) {
1185 		tty_driver_kref_put(driver);
1186 		return ret;
1187 	}
1188 	tty3215_driver = driver;
1189 	return 0;
1190 }
1191 device_initcall(tty3215_init);
1192