]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
62b74942 | 2 | * SCLP line mode console driver |
1da177e4 | 3 | * |
62b74942 MH |
4 | * Copyright IBM Corp. 1999, 2009 |
5 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> | |
6 | * Martin Schwidefsky <schwidefsky@de.ibm.com> | |
1da177e4 LT |
7 | */ |
8 | ||
1da177e4 LT |
9 | #include <linux/kmod.h> |
10 | #include <linux/console.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/timer.h> | |
13 | #include <linux/jiffies.h> | |
095761d2 | 14 | #include <linux/termios.h> |
1da177e4 | 15 | #include <linux/err.h> |
2332ce1a | 16 | #include <linux/reboot.h> |
5a0e3ad6 | 17 | #include <linux/gfp.h> |
1da177e4 LT |
18 | |
19 | #include "sclp.h" | |
20 | #include "sclp_rw.h" | |
21 | #include "sclp_tty.h" | |
22 | ||
1da177e4 LT |
23 | #define sclp_console_major 4 /* TTYAUX_MAJOR */ |
24 | #define sclp_console_minor 64 | |
25 | #define sclp_console_name "ttyS" | |
26 | ||
27 | /* Lock to guard over changes to global variables */ | |
28 | static spinlock_t sclp_con_lock; | |
29 | /* List of free pages that can be used for console output buffering */ | |
30 | static struct list_head sclp_con_pages; | |
31 | /* List of full struct sclp_buffer structures ready for output */ | |
32 | static struct list_head sclp_con_outqueue; | |
1da177e4 LT |
33 | /* Pointer to current console buffer */ |
34 | static struct sclp_buffer *sclp_conbuf; | |
35 | /* Timer for delayed output of console messages */ | |
36 | static struct timer_list sclp_con_timer; | |
62b74942 MH |
37 | /* Suspend mode flag */ |
38 | static int sclp_con_suspended; | |
39 | /* Flag that output queue is currently running */ | |
40 | static int sclp_con_queue_running; | |
1da177e4 LT |
41 | |
42 | /* Output format for console messages */ | |
43 | static unsigned short sclp_con_columns; | |
44 | static unsigned short sclp_con_width_htab; | |
45 | ||
46 | static void | |
47 | sclp_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); | |
62b74942 | 55 | |
1da177e4 LT |
56 | /* Remove buffer from outqueue */ |
57 | list_del(&buffer->list); | |
1da177e4 | 58 | list_add_tail((struct list_head *) page, &sclp_con_pages); |
62b74942 | 59 | |
1da177e4 LT |
60 | /* Check if there is a pending buffer on the out queue. */ |
61 | buffer = NULL; | |
62 | if (!list_empty(&sclp_con_outqueue)) | |
62b74942 MH |
63 | buffer = list_first_entry(&sclp_con_outqueue, |
64 | struct sclp_buffer, list); | |
65 | if (!buffer || sclp_con_suspended) { | |
66 | sclp_con_queue_running = 0; | |
67 | spin_unlock_irqrestore(&sclp_con_lock, flags); | |
68 | break; | |
69 | } | |
1da177e4 | 70 | spin_unlock_irqrestore(&sclp_con_lock, flags); |
62b74942 | 71 | } while (sclp_emit_buffer(buffer, sclp_conbuf_callback)); |
1da177e4 LT |
72 | } |
73 | ||
62b74942 MH |
74 | /* |
75 | * Finalize and emit first pending buffer. | |
76 | */ | |
77 | static void sclp_conbuf_emit(void) | |
1da177e4 LT |
78 | { |
79 | struct sclp_buffer* buffer; | |
80 | unsigned long flags; | |
1da177e4 LT |
81 | int rc; |
82 | ||
83 | spin_lock_irqsave(&sclp_con_lock, flags); | |
62b74942 MH |
84 | if (sclp_conbuf) |
85 | list_add_tail(&sclp_conbuf->list, &sclp_con_outqueue); | |
1da177e4 | 86 | sclp_conbuf = NULL; |
62b74942 MH |
87 | if (sclp_con_queue_running || sclp_con_suspended) |
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; | |
1da177e4 | 94 | spin_unlock_irqrestore(&sclp_con_lock, flags); |
62b74942 | 95 | |
1da177e4 LT |
96 | rc = sclp_emit_buffer(buffer, sclp_conbuf_callback); |
97 | if (rc) | |
98 | sclp_conbuf_callback(buffer, rc); | |
62b74942 MH |
99 | return; |
100 | out_unlock: | |
101 | spin_unlock_irqrestore(&sclp_con_lock, flags); | |
102 | } | |
103 | ||
104 | /* | |
105 | * Wait until out queue is empty | |
106 | */ | |
107 | static 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)) | |
f3dfa86c | 113 | del_timer(&sclp_con_timer); |
62b74942 MH |
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); | |
1da177e4 LT |
120 | } |
121 | ||
122 | /* | |
123 | * When this routine is called from the timer then we flush the | |
124 | * temporary write buffer without further waiting on a final new line. | |
125 | */ | |
126 | static void | |
127 | sclp_console_timeout(unsigned long data) | |
128 | { | |
129 | sclp_conbuf_emit(); | |
130 | } | |
131 | ||
25b41a7b MS |
132 | /* |
133 | * Drop oldest console buffer if sclp_con_drop is set | |
134 | */ | |
135 | static int | |
136 | sclp_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 | /* The first element is in I/O */ | |
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 | ||
1da177e4 LT |
157 | /* |
158 | * Writes the given message to S390 system console | |
159 | */ | |
160 | static void | |
161 | sclp_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 | * process escape characters, write message into buffer, | |
173 | * send buffer to SCLP | |
174 | */ | |
175 | do { | |
176 | /* make sure we have a console output buffer */ | |
177 | if (sclp_conbuf == NULL) { | |
25b41a7b MS |
178 | if (list_empty(&sclp_con_pages)) |
179 | sclp_console_full++; | |
1da177e4 | 180 | while (list_empty(&sclp_con_pages)) { |
62b74942 MH |
181 | if (sclp_con_suspended) |
182 | goto out; | |
25b41a7b MS |
183 | if (sclp_console_drop_buffer()) |
184 | break; | |
1da177e4 LT |
185 | spin_unlock_irqrestore(&sclp_con_lock, flags); |
186 | sclp_sync_wait(); | |
187 | spin_lock_irqsave(&sclp_con_lock, flags); | |
188 | } | |
189 | page = sclp_con_pages.next; | |
190 | list_del((struct list_head *) page); | |
191 | sclp_conbuf = sclp_make_buffer(page, sclp_con_columns, | |
192 | sclp_con_width_htab); | |
193 | } | |
194 | /* try to write the string to the current output buffer */ | |
195 | written = sclp_write(sclp_conbuf, (const unsigned char *) | |
196 | message, count); | |
197 | if (written == count) | |
198 | break; | |
199 | /* | |
200 | * Not all characters could be written to the current | |
201 | * output buffer. Emit the buffer, create a new buffer | |
202 | * and then output the rest of the string. | |
203 | */ | |
204 | spin_unlock_irqrestore(&sclp_con_lock, flags); | |
205 | sclp_conbuf_emit(); | |
206 | spin_lock_irqsave(&sclp_con_lock, flags); | |
207 | message += written; | |
208 | count -= written; | |
209 | } while (count > 0); | |
210 | /* Setup timer to output current console buffer after 1/10 second */ | |
211 | if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 && | |
212 | !timer_pending(&sclp_con_timer)) { | |
213 | init_timer(&sclp_con_timer); | |
214 | sclp_con_timer.function = sclp_console_timeout; | |
215 | sclp_con_timer.data = 0UL; | |
216 | sclp_con_timer.expires = jiffies + HZ/10; | |
217 | add_timer(&sclp_con_timer); | |
218 | } | |
62b74942 | 219 | out: |
1da177e4 LT |
220 | spin_unlock_irqrestore(&sclp_con_lock, flags); |
221 | } | |
222 | ||
223 | static struct tty_driver * | |
224 | sclp_console_device(struct console *c, int *index) | |
225 | { | |
226 | *index = c->index; | |
227 | return sclp_tty_driver; | |
228 | } | |
229 | ||
230 | /* | |
62b74942 | 231 | * Make sure that all buffers will be flushed to the SCLP. |
1da177e4 LT |
232 | */ |
233 | static void | |
2332ce1a | 234 | sclp_console_flush(void) |
62b74942 MH |
235 | { |
236 | sclp_conbuf_emit(); | |
237 | sclp_console_sync_queue(); | |
238 | } | |
239 | ||
240 | /* | |
241 | * Resume console: If there are cached messages, emit them. | |
242 | */ | |
243 | static void sclp_console_resume(void) | |
1da177e4 LT |
244 | { |
245 | unsigned long flags; | |
246 | ||
62b74942 MH |
247 | spin_lock_irqsave(&sclp_con_lock, flags); |
248 | sclp_con_suspended = 0; | |
249 | spin_unlock_irqrestore(&sclp_con_lock, flags); | |
1da177e4 | 250 | sclp_conbuf_emit(); |
62b74942 MH |
251 | } |
252 | ||
253 | /* | |
254 | * Suspend console: Set suspend flag and flush console | |
255 | */ | |
256 | static void sclp_console_suspend(void) | |
257 | { | |
258 | unsigned long flags; | |
259 | ||
1da177e4 | 260 | spin_lock_irqsave(&sclp_con_lock, flags); |
62b74942 | 261 | sclp_con_suspended = 1; |
1da177e4 | 262 | spin_unlock_irqrestore(&sclp_con_lock, flags); |
62b74942 | 263 | sclp_console_flush(); |
1da177e4 LT |
264 | } |
265 | ||
62b74942 MH |
266 | static int sclp_console_notify(struct notifier_block *self, |
267 | unsigned long event, void *data) | |
2332ce1a HS |
268 | { |
269 | sclp_console_flush(); | |
270 | return NOTIFY_OK; | |
271 | } | |
272 | ||
273 | static struct notifier_block on_panic_nb = { | |
274 | .notifier_call = sclp_console_notify, | |
62b74942 | 275 | .priority = SCLP_PANIC_PRIO_CLIENT, |
2332ce1a HS |
276 | }; |
277 | ||
278 | static struct notifier_block on_reboot_nb = { | |
279 | .notifier_call = sclp_console_notify, | |
280 | .priority = 1, | |
281 | }; | |
282 | ||
1da177e4 LT |
283 | /* |
284 | * used to register the SCLP console to the kernel and to | |
285 | * give printk necessary information | |
286 | */ | |
287 | static struct console sclp_console = | |
288 | { | |
289 | .name = sclp_console_name, | |
290 | .write = sclp_console_write, | |
291 | .device = sclp_console_device, | |
1da177e4 LT |
292 | .flags = CON_PRINTBUFFER, |
293 | .index = 0 /* ttyS0 */ | |
294 | }; | |
295 | ||
62b74942 MH |
296 | /* |
297 | * This function is called for SCLP suspend and resume events. | |
298 | */ | |
299 | void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event) | |
300 | { | |
301 | switch (sclp_pm_event) { | |
302 | case SCLP_PM_EVENT_FREEZE: | |
303 | sclp_console_suspend(); | |
304 | break; | |
305 | case SCLP_PM_EVENT_RESTORE: | |
306 | case SCLP_PM_EVENT_THAW: | |
307 | sclp_console_resume(); | |
308 | break; | |
309 | } | |
310 | } | |
311 | ||
1da177e4 LT |
312 | /* |
313 | * called by console_init() in drivers/char/tty_io.c at boot-time. | |
314 | */ | |
315 | static int __init | |
316 | sclp_console_init(void) | |
317 | { | |
318 | void *page; | |
319 | int i; | |
320 | int rc; | |
321 | ||
8f50af49 PO |
322 | /* SCLP consoles are handled together */ |
323 | if (!(CONSOLE_IS_SCLP || CONSOLE_IS_VT220)) | |
1da177e4 LT |
324 | return 0; |
325 | rc = sclp_rw_init(); | |
326 | if (rc) | |
327 | return rc; | |
328 | /* Allocate pages for output buffering */ | |
329 | INIT_LIST_HEAD(&sclp_con_pages); | |
25b41a7b | 330 | for (i = 0; i < sclp_console_pages; i++) { |
4c8f4794 HC |
331 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
332 | list_add_tail(page, &sclp_con_pages); | |
1da177e4 LT |
333 | } |
334 | INIT_LIST_HEAD(&sclp_con_outqueue); | |
335 | spin_lock_init(&sclp_con_lock); | |
1da177e4 LT |
336 | sclp_conbuf = NULL; |
337 | init_timer(&sclp_con_timer); | |
338 | ||
339 | /* Set output format */ | |
340 | if (MACHINE_IS_VM) | |
341 | /* | |
342 | * save 4 characters for the CPU number | |
343 | * written at start of each line by VM/CP | |
344 | */ | |
345 | sclp_con_columns = 76; | |
346 | else | |
347 | sclp_con_columns = 80; | |
348 | sclp_con_width_htab = 8; | |
349 | ||
350 | /* enable printk-access to this driver */ | |
2332ce1a HS |
351 | atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb); |
352 | register_reboot_notifier(&on_reboot_nb); | |
1da177e4 LT |
353 | register_console(&sclp_console); |
354 | return 0; | |
355 | } | |
356 | ||
357 | console_initcall(sclp_console_init); |