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