con3270.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IBM/3270 Driver - console view.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * Copyright IBM Corp. 2003, 2009
  9. */
  10. #include <linux/module.h>
  11. #include <linux/console.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/reboot.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/cio.h>
  21. #include <asm/cpcmd.h>
  22. #include <asm/ebcdic.h>
  23. #include "raw3270.h"
  24. #include "tty3270.h"
  25. #include "ctrlchar.h"
  26. #define CON3270_OUTPUT_BUFFER_SIZE 1024
  27. #define CON3270_STRING_PAGES 4
  28. static struct raw3270_fn con3270_fn;
  29. static bool auto_update = true;
  30. module_param(auto_update, bool, 0);
  31. /*
  32. * Main 3270 console view data structure.
  33. */
  34. struct con3270 {
  35. struct raw3270_view view;
  36. struct list_head freemem; /* list of free memory for strings. */
  37. /* Output stuff. */
  38. struct list_head lines; /* list of lines. */
  39. struct list_head update; /* list of lines to update. */
  40. int line_nr; /* line number for next update. */
  41. int nr_lines; /* # lines in list. */
  42. int nr_up; /* # lines up in history. */
  43. unsigned long update_flags; /* Update indication bits. */
  44. struct string *cline; /* current output line. */
  45. struct string *status; /* last line of display. */
  46. struct raw3270_request *write; /* single write request. */
  47. struct timer_list timer;
  48. /* Input stuff. */
  49. struct string *input; /* input string for read request. */
  50. struct raw3270_request *read; /* single read request. */
  51. struct raw3270_request *kreset; /* single keyboard reset request. */
  52. struct tasklet_struct readlet; /* tasklet to issue read request. */
  53. };
  54. static struct con3270 *condev;
  55. /* con3270->update_flags. See con3270_update for details. */
  56. #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
  57. #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
  58. #define CON_UPDATE_STATUS 4 /* Update status line. */
  59. #define CON_UPDATE_ALL 8 /* Recreate screen. */
  60. static void con3270_update(struct timer_list *);
  61. /*
  62. * Setup timeout for a device. On timeout trigger an update.
  63. */
  64. static void con3270_set_timer(struct con3270 *cp, int expires)
  65. {
  66. if (expires == 0)
  67. del_timer(&cp->timer);
  68. else
  69. mod_timer(&cp->timer, jiffies + expires);
  70. }
  71. /*
  72. * The status line is the last line of the screen. It shows the string
  73. * "console view" in the lower left corner and "Running"/"More..."/"Holding"
  74. * in the lower right corner of the screen.
  75. */
  76. static void
  77. con3270_update_status(struct con3270 *cp)
  78. {
  79. char *str;
  80. str = (cp->nr_up != 0) ? "History" : "Running";
  81. memcpy(cp->status->string + 24, str, 7);
  82. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  83. cp->update_flags |= CON_UPDATE_STATUS;
  84. }
  85. static void
  86. con3270_create_status(struct con3270 *cp)
  87. {
  88. static const unsigned char blueprint[] =
  89. { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
  90. 'c','o','n','s','o','l','e',' ','v','i','e','w',
  91. TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
  92. cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
  93. /* Copy blueprint to status line */
  94. memcpy(cp->status->string, blueprint, sizeof(blueprint));
  95. /* Set TO_RA addresses. */
  96. raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
  97. cp->view.cols * (cp->view.rows - 1));
  98. raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
  99. cp->view.cols * cp->view.rows - 8);
  100. /* Convert strings to ebcdic. */
  101. codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
  102. codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  103. }
  104. /*
  105. * Set output offsets to 3270 datastream fragment of a console string.
  106. */
  107. static void
  108. con3270_update_string(struct con3270 *cp, struct string *s, int nr)
  109. {
  110. if (s->len < 4) {
  111. /* This indicates a bug, but printing a warning would
  112. * cause a deadlock. */
  113. return;
  114. }
  115. if (s->string[s->len - 4] != TO_RA)
  116. return;
  117. raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
  118. cp->view.cols * (nr + 1));
  119. }
  120. /*
  121. * Rebuild update list to print all lines.
  122. */
  123. static void
  124. con3270_rebuild_update(struct con3270 *cp)
  125. {
  126. struct string *s, *n;
  127. int nr;
  128. /*
  129. * Throw away update list and create a new one,
  130. * containing all lines that will fit on the screen.
  131. */
  132. list_for_each_entry_safe(s, n, &cp->update, update)
  133. list_del_init(&s->update);
  134. nr = cp->view.rows - 2 + cp->nr_up;
  135. list_for_each_entry_reverse(s, &cp->lines, list) {
  136. if (nr < cp->view.rows - 1)
  137. list_add(&s->update, &cp->update);
  138. if (--nr < 0)
  139. break;
  140. }
  141. cp->line_nr = 0;
  142. cp->update_flags |= CON_UPDATE_LIST;
  143. }
  144. /*
  145. * Alloc string for size bytes. Free strings from history if necessary.
  146. */
  147. static struct string *
  148. con3270_alloc_string(struct con3270 *cp, size_t size)
  149. {
  150. struct string *s, *n;
  151. s = alloc_string(&cp->freemem, size);
  152. if (s)
  153. return s;
  154. list_for_each_entry_safe(s, n, &cp->lines, list) {
  155. list_del(&s->list);
  156. if (!list_empty(&s->update))
  157. list_del(&s->update);
  158. cp->nr_lines--;
  159. if (free_string(&cp->freemem, s) >= size)
  160. break;
  161. }
  162. s = alloc_string(&cp->freemem, size);
  163. BUG_ON(!s);
  164. if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
  165. cp->nr_up = cp->nr_lines - cp->view.rows + 1;
  166. con3270_rebuild_update(cp);
  167. con3270_update_status(cp);
  168. }
  169. return s;
  170. }
  171. /*
  172. * Write completion callback.
  173. */
  174. static void
  175. con3270_write_callback(struct raw3270_request *rq, void *data)
  176. {
  177. raw3270_request_reset(rq);
  178. xchg(&((struct con3270 *) rq->view)->write, rq);
  179. }
  180. /*
  181. * Update console display.
  182. */
  183. static void
  184. con3270_update(struct timer_list *t)
  185. {
  186. struct con3270 *cp = from_timer(cp, t, timer);
  187. struct raw3270_request *wrq;
  188. char wcc, prolog[6];
  189. unsigned long flags;
  190. unsigned long updated;
  191. struct string *s, *n;
  192. int rc;
  193. if (!auto_update && !raw3270_view_active(&cp->view))
  194. return;
  195. if (cp->view.dev)
  196. raw3270_activate_view(&cp->view);
  197. wrq = xchg(&cp->write, 0);
  198. if (!wrq) {
  199. con3270_set_timer(cp, 1);
  200. return;
  201. }
  202. spin_lock_irqsave(&cp->view.lock, flags);
  203. updated = 0;
  204. if (cp->update_flags & CON_UPDATE_ALL) {
  205. con3270_rebuild_update(cp);
  206. con3270_update_status(cp);
  207. cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
  208. CON_UPDATE_STATUS;
  209. }
  210. if (cp->update_flags & CON_UPDATE_ERASE) {
  211. /* Use erase write alternate to initialize display. */
  212. raw3270_request_set_cmd(wrq, TC_EWRITEA);
  213. updated |= CON_UPDATE_ERASE;
  214. } else
  215. raw3270_request_set_cmd(wrq, TC_WRITE);
  216. wcc = TW_NONE;
  217. raw3270_request_add_data(wrq, &wcc, 1);
  218. /*
  219. * Update status line.
  220. */
  221. if (cp->update_flags & CON_UPDATE_STATUS)
  222. if (raw3270_request_add_data(wrq, cp->status->string,
  223. cp->status->len) == 0)
  224. updated |= CON_UPDATE_STATUS;
  225. if (cp->update_flags & CON_UPDATE_LIST) {
  226. prolog[0] = TO_SBA;
  227. prolog[3] = TO_SA;
  228. prolog[4] = TAT_COLOR;
  229. prolog[5] = TAC_TURQ;
  230. raw3270_buffer_address(cp->view.dev, prolog + 1,
  231. cp->view.cols * cp->line_nr);
  232. raw3270_request_add_data(wrq, prolog, 6);
  233. /* Write strings in the update list to the screen. */
  234. list_for_each_entry_safe(s, n, &cp->update, update) {
  235. if (s != cp->cline)
  236. con3270_update_string(cp, s, cp->line_nr);
  237. if (raw3270_request_add_data(wrq, s->string,
  238. s->len) != 0)
  239. break;
  240. list_del_init(&s->update);
  241. if (s != cp->cline)
  242. cp->line_nr++;
  243. }
  244. if (list_empty(&cp->update))
  245. updated |= CON_UPDATE_LIST;
  246. }
  247. wrq->callback = con3270_write_callback;
  248. rc = raw3270_start(&cp->view, wrq);
  249. if (rc == 0) {
  250. cp->update_flags &= ~updated;
  251. if (cp->update_flags)
  252. con3270_set_timer(cp, 1);
  253. } else {
  254. raw3270_request_reset(wrq);
  255. xchg(&cp->write, wrq);
  256. }
  257. spin_unlock_irqrestore(&cp->view.lock, flags);
  258. }
  259. /*
  260. * Read tasklet.
  261. */
  262. static void
  263. con3270_read_tasklet(struct raw3270_request *rrq)
  264. {
  265. static char kreset_data = TW_KR;
  266. struct con3270 *cp;
  267. unsigned long flags;
  268. int nr_up, deactivate;
  269. cp = (struct con3270 *) rrq->view;
  270. spin_lock_irqsave(&cp->view.lock, flags);
  271. nr_up = cp->nr_up;
  272. deactivate = 0;
  273. /* Check aid byte. */
  274. switch (cp->input->string[0]) {
  275. case 0x7d: /* enter: jump to bottom. */
  276. nr_up = 0;
  277. break;
  278. case 0xf3: /* PF3: deactivate the console view. */
  279. deactivate = 1;
  280. break;
  281. case 0x6d: /* clear: start from scratch. */
  282. cp->update_flags = CON_UPDATE_ALL;
  283. con3270_set_timer(cp, 1);
  284. break;
  285. case 0xf7: /* PF7: do a page up in the console log. */
  286. nr_up += cp->view.rows - 2;
  287. if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
  288. nr_up = cp->nr_lines - cp->view.rows + 1;
  289. if (nr_up < 0)
  290. nr_up = 0;
  291. }
  292. break;
  293. case 0xf8: /* PF8: do a page down in the console log. */
  294. nr_up -= cp->view.rows - 2;
  295. if (nr_up < 0)
  296. nr_up = 0;
  297. break;
  298. }
  299. if (nr_up != cp->nr_up) {
  300. cp->nr_up = nr_up;
  301. con3270_rebuild_update(cp);
  302. con3270_update_status(cp);
  303. con3270_set_timer(cp, 1);
  304. }
  305. spin_unlock_irqrestore(&cp->view.lock, flags);
  306. /* Start keyboard reset command. */
  307. raw3270_request_reset(cp->kreset);
  308. raw3270_request_set_cmd(cp->kreset, TC_WRITE);
  309. raw3270_request_add_data(cp->kreset, &kreset_data, 1);
  310. raw3270_start(&cp->view, cp->kreset);
  311. if (deactivate)
  312. raw3270_deactivate_view(&cp->view);
  313. raw3270_request_reset(rrq);
  314. xchg(&cp->read, rrq);
  315. raw3270_put_view(&cp->view);
  316. }
  317. /*
  318. * Read request completion callback.
  319. */
  320. static void
  321. con3270_read_callback(struct raw3270_request *rq, void *data)
  322. {
  323. raw3270_get_view(rq->view);
  324. /* Schedule tasklet to pass input to tty. */
  325. tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
  326. }
  327. /*
  328. * Issue a read request. Called only from interrupt function.
  329. */
  330. static void
  331. con3270_issue_read(struct con3270 *cp)
  332. {
  333. struct raw3270_request *rrq;
  334. int rc;
  335. rrq = xchg(&cp->read, 0);
  336. if (!rrq)
  337. /* Read already scheduled. */
  338. return;
  339. rrq->callback = con3270_read_callback;
  340. rrq->callback_data = cp;
  341. raw3270_request_set_cmd(rrq, TC_READMOD);
  342. raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
  343. /* Issue the read modified request. */
  344. rc = raw3270_start_irq(&cp->view, rrq);
  345. if (rc)
  346. raw3270_request_reset(rrq);
  347. }
  348. /*
  349. * Switch to the console view.
  350. */
  351. static int
  352. con3270_activate(struct raw3270_view *view)
  353. {
  354. struct con3270 *cp;
  355. cp = (struct con3270 *) view;
  356. cp->update_flags = CON_UPDATE_ALL;
  357. con3270_set_timer(cp, 1);
  358. return 0;
  359. }
  360. static void
  361. con3270_deactivate(struct raw3270_view *view)
  362. {
  363. struct con3270 *cp;
  364. cp = (struct con3270 *) view;
  365. del_timer(&cp->timer);
  366. }
  367. static void
  368. con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
  369. {
  370. /* Handle ATTN. Schedule tasklet to read aid. */
  371. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
  372. con3270_issue_read(cp);
  373. if (rq) {
  374. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  375. rq->rc = -EIO;
  376. else
  377. /* Normal end. Copy residual count. */
  378. rq->rescnt = irb->scsw.cmd.count;
  379. } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  380. /* Interrupt without an outstanding request -> update all */
  381. cp->update_flags = CON_UPDATE_ALL;
  382. con3270_set_timer(cp, 1);
  383. }
  384. }
  385. /* Console view to a 3270 device. */
  386. static struct raw3270_fn con3270_fn = {
  387. .activate = con3270_activate,
  388. .deactivate = con3270_deactivate,
  389. .intv = (void *) con3270_irq
  390. };
  391. static inline void
  392. con3270_cline_add(struct con3270 *cp)
  393. {
  394. if (!list_empty(&cp->cline->list))
  395. /* Already added. */
  396. return;
  397. list_add_tail(&cp->cline->list, &cp->lines);
  398. cp->nr_lines++;
  399. con3270_rebuild_update(cp);
  400. }
  401. static inline void
  402. con3270_cline_insert(struct con3270 *cp, unsigned char c)
  403. {
  404. cp->cline->string[cp->cline->len++] =
  405. cp->view.ascebc[(c < ' ') ? ' ' : c];
  406. if (list_empty(&cp->cline->update)) {
  407. list_add_tail(&cp->cline->update, &cp->update);
  408. cp->update_flags |= CON_UPDATE_LIST;
  409. }
  410. }
  411. static inline void
  412. con3270_cline_end(struct con3270 *cp)
  413. {
  414. struct string *s;
  415. unsigned int size;
  416. /* Copy cline. */
  417. size = (cp->cline->len < cp->view.cols - 5) ?
  418. cp->cline->len + 4 : cp->view.cols;
  419. s = con3270_alloc_string(cp, size);
  420. memcpy(s->string, cp->cline->string, cp->cline->len);
  421. if (cp->cline->len < cp->view.cols - 5) {
  422. s->string[s->len - 4] = TO_RA;
  423. s->string[s->len - 1] = 0;
  424. } else {
  425. while (--size >= cp->cline->len)
  426. s->string[size] = cp->view.ascebc[' '];
  427. }
  428. /* Replace cline with allocated line s and reset cline. */
  429. list_add(&s->list, &cp->cline->list);
  430. list_del_init(&cp->cline->list);
  431. if (!list_empty(&cp->cline->update)) {
  432. list_add(&s->update, &cp->cline->update);
  433. list_del_init(&cp->cline->update);
  434. }
  435. cp->cline->len = 0;
  436. }
  437. /*
  438. * Write a string to the 3270 console
  439. */
  440. static void
  441. con3270_write(struct console *co, const char *str, unsigned int count)
  442. {
  443. struct con3270 *cp;
  444. unsigned long flags;
  445. unsigned char c;
  446. cp = condev;
  447. spin_lock_irqsave(&cp->view.lock, flags);
  448. while (count-- > 0) {
  449. c = *str++;
  450. if (cp->cline->len == 0)
  451. con3270_cline_add(cp);
  452. if (c != '\n')
  453. con3270_cline_insert(cp, c);
  454. if (c == '\n' || cp->cline->len >= cp->view.cols)
  455. con3270_cline_end(cp);
  456. }
  457. /* Setup timer to output current console buffer after 1/10 second */
  458. cp->nr_up = 0;
  459. if (cp->view.dev && !timer_pending(&cp->timer))
  460. con3270_set_timer(cp, HZ/10);
  461. spin_unlock_irqrestore(&cp->view.lock,flags);
  462. }
  463. static struct tty_driver *
  464. con3270_device(struct console *c, int *index)
  465. {
  466. *index = c->index;
  467. return tty3270_driver;
  468. }
  469. /*
  470. * Wait for end of write request.
  471. */
  472. static void
  473. con3270_wait_write(struct con3270 *cp)
  474. {
  475. while (!cp->write) {
  476. raw3270_wait_cons_dev(cp->view.dev);
  477. barrier();
  478. }
  479. }
  480. /*
  481. * panic() calls con3270_flush through a panic_notifier
  482. * before the system enters a disabled, endless loop.
  483. */
  484. static void
  485. con3270_flush(void)
  486. {
  487. struct con3270 *cp;
  488. unsigned long flags;
  489. cp = condev;
  490. if (!cp->view.dev)
  491. return;
  492. raw3270_pm_unfreeze(&cp->view);
  493. raw3270_activate_view(&cp->view);
  494. spin_lock_irqsave(&cp->view.lock, flags);
  495. con3270_wait_write(cp);
  496. cp->nr_up = 0;
  497. con3270_rebuild_update(cp);
  498. con3270_update_status(cp);
  499. while (cp->update_flags != 0) {
  500. spin_unlock_irqrestore(&cp->view.lock, flags);
  501. con3270_update(&cp->timer);
  502. spin_lock_irqsave(&cp->view.lock, flags);
  503. con3270_wait_write(cp);
  504. }
  505. spin_unlock_irqrestore(&cp->view.lock, flags);
  506. }
  507. static int con3270_notify(struct notifier_block *self,
  508. unsigned long event, void *data)
  509. {
  510. con3270_flush();
  511. return NOTIFY_OK;
  512. }
  513. static struct notifier_block on_panic_nb = {
  514. .notifier_call = con3270_notify,
  515. .priority = 0,
  516. };
  517. static struct notifier_block on_reboot_nb = {
  518. .notifier_call = con3270_notify,
  519. .priority = 0,
  520. };
  521. /*
  522. * The console structure for the 3270 console
  523. */
  524. static struct console con3270 = {
  525. .name = "tty3270",
  526. .write = con3270_write,
  527. .device = con3270_device,
  528. .flags = CON_PRINTBUFFER,
  529. };
  530. /*
  531. * 3270 console initialization code called from console_init().
  532. */
  533. static int __init
  534. con3270_init(void)
  535. {
  536. struct raw3270 *rp;
  537. void *cbuf;
  538. int i;
  539. /* Check if 3270 is to be the console */
  540. if (!CONSOLE_IS_3270)
  541. return -ENODEV;
  542. /* Set the console mode for VM */
  543. if (MACHINE_IS_VM) {
  544. cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
  545. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  546. }
  547. rp = raw3270_setup_console();
  548. if (IS_ERR(rp))
  549. return PTR_ERR(rp);
  550. condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
  551. if (!condev)
  552. return -ENOMEM;
  553. condev->view.dev = rp;
  554. condev->read = raw3270_request_alloc(0);
  555. condev->read->callback = con3270_read_callback;
  556. condev->read->callback_data = condev;
  557. condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
  558. condev->kreset = raw3270_request_alloc(1);
  559. INIT_LIST_HEAD(&condev->lines);
  560. INIT_LIST_HEAD(&condev->update);
  561. timer_setup(&condev->timer, con3270_update, 0);
  562. tasklet_init(&condev->readlet,
  563. (void (*)(unsigned long)) con3270_read_tasklet,
  564. (unsigned long) condev->read);
  565. raw3270_add_view(&condev->view, &con3270_fn, 1, RAW3270_VIEW_LOCK_IRQ);
  566. INIT_LIST_HEAD(&condev->freemem);
  567. for (i = 0; i < CON3270_STRING_PAGES; i++) {
  568. cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  569. add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
  570. }
  571. condev->cline = alloc_string(&condev->freemem, condev->view.cols);
  572. condev->cline->len = 0;
  573. con3270_create_status(condev);
  574. condev->input = alloc_string(&condev->freemem, 80);
  575. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  576. register_reboot_notifier(&on_reboot_nb);
  577. register_console(&con3270);
  578. return 0;
  579. }
  580. console_initcall(con3270_init);