1
2
3
4
5
6
7
8
9
10
11#include <linux/kmod.h>
12#include <linux/console.h>
13#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/jiffies.h>
16#include <linux/bootmem.h>
17#include <linux/termios.h>
18#include <linux/err.h>
19#include <linux/reboot.h>
20
21#include "sclp.h"
22#include "sclp_rw.h"
23#include "sclp_tty.h"
24
25#define sclp_console_major 4
26#define sclp_console_minor 64
27#define sclp_console_name "ttyS"
28
29
30static spinlock_t sclp_con_lock;
31
32static struct list_head sclp_con_pages;
33
34static struct list_head sclp_con_outqueue;
35
36
37static int sclp_con_buffer_count;
38
39static struct sclp_buffer *sclp_conbuf;
40
41static struct timer_list sclp_con_timer;
42
43
44static unsigned short sclp_con_columns;
45static unsigned short sclp_con_width_htab;
46
47static void
48sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
49{
50 unsigned long flags;
51 void *page;
52
53 do {
54 page = sclp_unmake_buffer(buffer);
55 spin_lock_irqsave(&sclp_con_lock, flags);
56
57 list_del(&buffer->list);
58 sclp_con_buffer_count--;
59 list_add_tail((struct list_head *) page, &sclp_con_pages);
60
61 buffer = NULL;
62 if (!list_empty(&sclp_con_outqueue))
63 buffer = list_entry(sclp_con_outqueue.next,
64 struct sclp_buffer, list);
65 spin_unlock_irqrestore(&sclp_con_lock, flags);
66 } while (buffer && sclp_emit_buffer(buffer, sclp_conbuf_callback));
67}
68
69static void
70sclp_conbuf_emit(void)
71{
72 struct sclp_buffer* buffer;
73 unsigned long flags;
74 int count;
75 int rc;
76
77 spin_lock_irqsave(&sclp_con_lock, flags);
78 buffer = sclp_conbuf;
79 sclp_conbuf = NULL;
80 if (buffer == NULL) {
81 spin_unlock_irqrestore(&sclp_con_lock, flags);
82 return;
83 }
84 list_add_tail(&buffer->list, &sclp_con_outqueue);
85 count = sclp_con_buffer_count++;
86 spin_unlock_irqrestore(&sclp_con_lock, flags);
87 if (count)
88 return;
89 rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
90 if (rc)
91 sclp_conbuf_callback(buffer, rc);
92}
93
94
95
96
97
98static void
99sclp_console_timeout(unsigned long data)
100{
101 sclp_conbuf_emit();
102}
103
104
105
106
107static void
108sclp_console_write(struct console *console, const char *message,
109 unsigned int count)
110{
111 unsigned long flags;
112 void *page;
113 int written;
114
115 if (count == 0)
116 return;
117 spin_lock_irqsave(&sclp_con_lock, flags);
118
119
120
121
122 do {
123
124 if (sclp_conbuf == NULL) {
125 while (list_empty(&sclp_con_pages)) {
126 spin_unlock_irqrestore(&sclp_con_lock, flags);
127 sclp_sync_wait();
128 spin_lock_irqsave(&sclp_con_lock, flags);
129 }
130 page = sclp_con_pages.next;
131 list_del((struct list_head *) page);
132 sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
133 sclp_con_width_htab);
134 }
135
136 written = sclp_write(sclp_conbuf, (const unsigned char *)
137 message, count);
138 if (written == count)
139 break;
140
141
142
143
144
145 spin_unlock_irqrestore(&sclp_con_lock, flags);
146 sclp_conbuf_emit();
147 spin_lock_irqsave(&sclp_con_lock, flags);
148 message += written;
149 count -= written;
150 } while (count > 0);
151
152 if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
153 !timer_pending(&sclp_con_timer)) {
154 init_timer(&sclp_con_timer);
155 sclp_con_timer.function = sclp_console_timeout;
156 sclp_con_timer.data = 0UL;
157 sclp_con_timer.expires = jiffies + HZ/10;
158 add_timer(&sclp_con_timer);
159 }
160 spin_unlock_irqrestore(&sclp_con_lock, flags);
161}
162
163static struct tty_driver *
164sclp_console_device(struct console *c, int *index)
165{
166 *index = c->index;
167 return sclp_tty_driver;
168}
169
170
171
172
173
174
175static void
176sclp_console_flush(void)
177{
178 unsigned long flags;
179
180 sclp_conbuf_emit();
181 spin_lock_irqsave(&sclp_con_lock, flags);
182 if (timer_pending(&sclp_con_timer))
183 del_timer(&sclp_con_timer);
184 while (sclp_con_buffer_count > 0) {
185 spin_unlock_irqrestore(&sclp_con_lock, flags);
186 sclp_sync_wait();
187 spin_lock_irqsave(&sclp_con_lock, flags);
188 }
189 spin_unlock_irqrestore(&sclp_con_lock, flags);
190}
191
192static int
193sclp_console_notify(struct notifier_block *self,
194 unsigned long event, void *data)
195{
196 sclp_console_flush();
197 return NOTIFY_OK;
198}
199
200static struct notifier_block on_panic_nb = {
201 .notifier_call = sclp_console_notify,
202 .priority = 1,
203};
204
205static struct notifier_block on_reboot_nb = {
206 .notifier_call = sclp_console_notify,
207 .priority = 1,
208};
209
210
211
212
213
214static struct console sclp_console =
215{
216 .name = sclp_console_name,
217 .write = sclp_console_write,
218 .device = sclp_console_device,
219 .flags = CON_PRINTBUFFER,
220 .index = 0
221};
222
223
224
225
226static int __init
227sclp_console_init(void)
228{
229 void *page;
230 int i;
231 int rc;
232
233 if (!CONSOLE_IS_SCLP)
234 return 0;
235 rc = sclp_rw_init();
236 if (rc)
237 return rc;
238
239 INIT_LIST_HEAD(&sclp_con_pages);
240 for (i = 0; i < MAX_CONSOLE_PAGES; i++) {
241 page = alloc_bootmem_low_pages(PAGE_SIZE);
242 list_add_tail((struct list_head *) page, &sclp_con_pages);
243 }
244 INIT_LIST_HEAD(&sclp_con_outqueue);
245 spin_lock_init(&sclp_con_lock);
246 sclp_con_buffer_count = 0;
247 sclp_conbuf = NULL;
248 init_timer(&sclp_con_timer);
249
250
251 if (MACHINE_IS_VM)
252
253
254
255
256 sclp_con_columns = 76;
257 else
258 sclp_con_columns = 80;
259 sclp_con_width_htab = 8;
260
261
262 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
263 register_reboot_notifier(&on_reboot_nb);
264 register_console(&sclp_console);
265 return 0;
266}
267
268console_initcall(sclp_console_init);
269