sclp_con.c 8.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3
 * SCLP line mode console driver
L
Linus Torvalds 已提交
4
 *
5 6 7
 * Copyright IBM Corp. 1999, 2009
 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
 *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 */

#include <linux/kmod.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
15
#include <linux/termios.h>
L
Linus Torvalds 已提交
16
#include <linux/err.h>
17
#include <linux/reboot.h>
18
#include <linux/gfp.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28

#include "sclp.h"
#include "sclp_rw.h"
#include "sclp_tty.h"

#define sclp_console_major 4		/* TTYAUX_MAJOR */
#define sclp_console_minor 64
#define sclp_console_name  "ttyS"

/* Lock to guard over changes to global variables */
29
static DEFINE_SPINLOCK(sclp_con_lock);
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
/* List of free pages that can be used for console output buffering */
static struct list_head sclp_con_pages;
/* List of full struct sclp_buffer structures ready for output */
static struct list_head sclp_con_outqueue;
/* Pointer to current console buffer */
static struct sclp_buffer *sclp_conbuf;
/* Timer for delayed output of console messages */
static struct timer_list sclp_con_timer;
38 39 40 41
/* Suspend mode flag */
static int sclp_con_suspended;
/* Flag that output queue is currently running */
static int sclp_con_queue_running;
L
Linus Torvalds 已提交
42 43

/* Output format for console messages */
44 45
#define SCLP_CON_COLUMNS	320
#define SPACES_PER_TAB		8
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55

static void
sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
{
	unsigned long flags;
	void *page;

	do {
		page = sclp_unmake_buffer(buffer);
		spin_lock_irqsave(&sclp_con_lock, flags);
56

L
Linus Torvalds 已提交
57 58 59
		/* Remove buffer from outqueue */
		list_del(&buffer->list);
		list_add_tail((struct list_head *) page, &sclp_con_pages);
60

L
Linus Torvalds 已提交
61 62 63
		/* Check if there is a pending buffer on the out queue. */
		buffer = NULL;
		if (!list_empty(&sclp_con_outqueue))
64 65 66 67 68 69 70
			buffer = list_first_entry(&sclp_con_outqueue,
						  struct sclp_buffer, list);
		if (!buffer || sclp_con_suspended) {
			sclp_con_queue_running = 0;
			spin_unlock_irqrestore(&sclp_con_lock, flags);
			break;
		}
L
Linus Torvalds 已提交
71
		spin_unlock_irqrestore(&sclp_con_lock, flags);
72
	} while (sclp_emit_buffer(buffer, sclp_conbuf_callback));
L
Linus Torvalds 已提交
73 74
}

75 76 77 78
/*
 * Finalize and emit first pending buffer.
 */
static void sclp_conbuf_emit(void)
L
Linus Torvalds 已提交
79 80 81 82 83 84
{
	struct sclp_buffer* buffer;
	unsigned long flags;
	int rc;

	spin_lock_irqsave(&sclp_con_lock, flags);
85 86
	if (sclp_conbuf)
		list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue);
L
Linus Torvalds 已提交
87
	sclp_conbuf = NULL;
88 89 90 91 92 93 94
	if (sclp_con_queue_running || sclp_con_suspended)
		goto out_unlock;
	if (list_empty(&sclp_con_outqueue))
		goto out_unlock;
	buffer = list_first_entry(&sclp_con_outqueue, struct sclp_buffer,
				  list);
	sclp_con_queue_running = 1;
L
Linus Torvalds 已提交
95
	spin_unlock_irqrestore(&sclp_con_lock, flags);
96

L
Linus Torvalds 已提交
97 98 99
	rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
	if (rc)
		sclp_conbuf_callback(buffer, rc);
100 101 102 103 104 105 106 107 108 109 110 111 112 113
	return;
out_unlock:
	spin_unlock_irqrestore(&sclp_con_lock, flags);
}

/*
 * Wait until out queue is empty
 */
static void sclp_console_sync_queue(void)
{
	unsigned long flags;

	spin_lock_irqsave(&sclp_con_lock, flags);
	if (timer_pending(&sclp_con_timer))
114
		del_timer(&sclp_con_timer);
115 116 117 118 119 120
	while (sclp_con_queue_running) {
		spin_unlock_irqrestore(&sclp_con_lock, flags);
		sclp_sync_wait();
		spin_lock_irqsave(&sclp_con_lock, flags);
	}
	spin_unlock_irqrestore(&sclp_con_lock, flags);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127
}

/*
 * When this routine is called from the timer then we flush the
 * temporary write buffer without further waiting on a final new line.
 */
static void
128
sclp_console_timeout(struct timer_list *unused)
L
Linus Torvalds 已提交
129 130 131 132
{
	sclp_conbuf_emit();
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*
 * Drop oldest console buffer if sclp_con_drop is set
 */
static int
sclp_console_drop_buffer(void)
{
	struct list_head *list;
	struct sclp_buffer *buffer;
	void *page;

	if (!sclp_console_drop)
		return 0;
	list = sclp_con_outqueue.next;
	if (sclp_con_queue_running)
		/* The first element is in I/O */
		list = list->next;
	if (list == &sclp_con_outqueue)
		return 0;
	list_del(list);
	buffer = list_entry(list, struct sclp_buffer, list);
	page = sclp_unmake_buffer(buffer);
	list_add_tail((struct list_head *) page, &sclp_con_pages);
	return 1;
}

L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
 * Writes the given message to S390 system console
 */
static void
sclp_console_write(struct console *console, const char *message,
		   unsigned int count)
{
	unsigned long flags;
	void *page;
	int written;

	if (count == 0)
		return;
	spin_lock_irqsave(&sclp_con_lock, flags);
	/*
	 * process escape characters, write message into buffer,
	 * send buffer to SCLP
	 */
	do {
		/* make sure we have a console output buffer */
		if (sclp_conbuf == NULL) {
179 180
			if (list_empty(&sclp_con_pages))
				sclp_console_full++;
L
Linus Torvalds 已提交
181
			while (list_empty(&sclp_con_pages)) {
182 183
				if (sclp_con_suspended)
					goto out;
184 185
				if (sclp_console_drop_buffer())
					break;
L
Linus Torvalds 已提交
186 187 188 189 190 191
				spin_unlock_irqrestore(&sclp_con_lock, flags);
				sclp_sync_wait();
				spin_lock_irqsave(&sclp_con_lock, flags);
			}
			page = sclp_con_pages.next;
			list_del((struct list_head *) page);
192 193
			sclp_conbuf = sclp_make_buffer(page, SCLP_CON_COLUMNS,
						       SPACES_PER_TAB);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		}
		/* try to write the string to the current output buffer */
		written = sclp_write(sclp_conbuf, (const unsigned char *)
				     message, count);
		if (written == count)
			break;
		/*
		 * Not all characters could be written to the current
		 * output buffer. Emit the buffer, create a new buffer
		 * and then output the rest of the string.
		 */
		spin_unlock_irqrestore(&sclp_con_lock, flags);
		sclp_conbuf_emit();
		spin_lock_irqsave(&sclp_con_lock, flags);
		message += written;
		count -= written;
	} while (count > 0);
	/* Setup timer to output current console buffer after 1/10 second */
	if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
	    !timer_pending(&sclp_con_timer)) {
214
		mod_timer(&sclp_con_timer, jiffies + HZ / 10);
L
Linus Torvalds 已提交
215
	}
216
out:
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226 227
	spin_unlock_irqrestore(&sclp_con_lock, flags);
}

static struct tty_driver *
sclp_console_device(struct console *c, int *index)
{
	*index = c->index;
	return sclp_tty_driver;
}

/*
228
 * Make sure that all buffers will be flushed to the SCLP.
L
Linus Torvalds 已提交
229 230
 */
static void
231
sclp_console_flush(void)
232 233 234 235 236 237 238 239 240
{
	sclp_conbuf_emit();
	sclp_console_sync_queue();
}

/*
 * Resume console: If there are cached messages, emit them.
 */
static void sclp_console_resume(void)
L
Linus Torvalds 已提交
241 242 243
{
	unsigned long flags;

244 245 246
	spin_lock_irqsave(&sclp_con_lock, flags);
	sclp_con_suspended = 0;
	spin_unlock_irqrestore(&sclp_con_lock, flags);
L
Linus Torvalds 已提交
247
	sclp_conbuf_emit();
248 249 250 251 252 253 254 255 256
}

/*
 * Suspend console: Set suspend flag and flush console
 */
static void sclp_console_suspend(void)
{
	unsigned long flags;

L
Linus Torvalds 已提交
257
	spin_lock_irqsave(&sclp_con_lock, flags);
258
	sclp_con_suspended = 1;
L
Linus Torvalds 已提交
259
	spin_unlock_irqrestore(&sclp_con_lock, flags);
260
	sclp_console_flush();
L
Linus Torvalds 已提交
261 262
}

263 264
static int sclp_console_notify(struct notifier_block *self,
			       unsigned long event, void *data)
265 266 267 268 269 270 271
{
	sclp_console_flush();
	return NOTIFY_OK;
}

static struct notifier_block on_panic_nb = {
	.notifier_call = sclp_console_notify,
272
	.priority = SCLP_PANIC_PRIO_CLIENT,
273 274 275 276 277 278 279
};

static struct notifier_block on_reboot_nb = {
	.notifier_call = sclp_console_notify,
	.priority = 1,
};

L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
/*
 * used to register the SCLP console to the kernel and to
 * give printk necessary information
 */
static struct console sclp_console =
{
	.name = sclp_console_name,
	.write = sclp_console_write,
	.device = sclp_console_device,
	.flags = CON_PRINTBUFFER,
	.index = 0 /* ttyS0 */
};

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/*
 * This function is called for SCLP suspend and resume events.
 */
void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event)
{
	switch (sclp_pm_event) {
	case SCLP_PM_EVENT_FREEZE:
		sclp_console_suspend();
		break;
	case SCLP_PM_EVENT_RESTORE:
	case SCLP_PM_EVENT_THAW:
		sclp_console_resume();
		break;
	}
}

L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318
/*
 * called by console_init() in drivers/char/tty_io.c at boot-time.
 */
static int __init
sclp_console_init(void)
{
	void *page;
	int i;
	int rc;

319 320
	/* SCLP consoles are handled together */
	if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220))
L
Linus Torvalds 已提交
321 322 323 324 325 326
		return 0;
	rc = sclp_rw_init();
	if (rc)
		return rc;
	/* Allocate pages for output buffering */
	INIT_LIST_HEAD(&sclp_con_pages);
327
	for (i = 0; i < sclp_console_pages; i++) {
328 329
		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
		list_add_tail(page, &sclp_con_pages);
L
Linus Torvalds 已提交
330 331 332
	}
	INIT_LIST_HEAD(&sclp_con_outqueue);
	sclp_conbuf = NULL;
333
	timer_setup(&sclp_con_timer, sclp_console_timeout, 0);
L
Linus Torvalds 已提交
334 335

	/* enable printk-access to this driver */
336 337
	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
	register_reboot_notifier(&on_reboot_nb);
L
Linus Torvalds 已提交
338 339 340 341 342
	register_console(&sclp_console);
	return 0;
}

console_initcall(sclp_console_init);