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