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