sclp_con.c 8.6 KB

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