linux/drivers/s390/char/con3270.c
<<
>>
Prefs
   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
  11#include <linux/module.h>
  12#include <linux/console.h>
  13#include <linux/init.h>
  14#include <linux/interrupt.h>
  15#include <linux/list.h>
  16#include <linux/types.h>
  17#include <linux/slab.h>
  18#include <linux/err.h>
  19#include <linux/reboot.h>
  20
  21#include <asm/ccwdev.h>
  22#include <asm/cio.h>
  23#include <asm/cpcmd.h>
  24#include <asm/ebcdic.h>
  25
  26#include "raw3270.h"
  27#include "tty3270.h"
  28#include "ctrlchar.h"
  29
  30#define CON3270_OUTPUT_BUFFER_SIZE 1024
  31#define CON3270_STRING_PAGES 4
  32
  33static struct raw3270_fn con3270_fn;
  34
  35static bool auto_update = true;
  36module_param(auto_update, bool, 0);
  37
  38/*
  39 * Main 3270 console view data structure.
  40 */
  41struct con3270 {
  42        struct raw3270_view view;
  43        struct list_head freemem;       /* list of free memory for strings. */
  44
  45        /* Output stuff. */
  46        struct list_head lines;         /* list of lines. */
  47        struct list_head update;        /* list of lines to update. */
  48        int line_nr;                    /* line number for next update. */
  49        int nr_lines;                   /* # lines in list. */
  50        int nr_up;                      /* # lines up in history. */
  51        unsigned long update_flags;     /* Update indication bits. */
  52        struct string *cline;           /* current output line. */
  53        struct string *status;          /* last line of display. */
  54        struct raw3270_request *write;  /* single write request. */
  55        struct timer_list timer;
  56
  57        /* Input stuff. */
  58        struct string *input;           /* input string for read request. */
  59        struct raw3270_request *read;   /* single read request. */
  60        struct raw3270_request *kreset; /* single keyboard reset request. */
  61        struct tasklet_struct readlet;  /* tasklet to issue read request. */
  62};
  63
  64static struct con3270 *condev;
  65
  66/* con3270->update_flags. See con3270_update for details. */
  67#define CON_UPDATE_ERASE        1       /* Use EWRITEA instead of WRITE. */
  68#define CON_UPDATE_LIST         2       /* Update lines in tty3270->update. */
  69#define CON_UPDATE_STATUS       4       /* Update status line. */
  70#define CON_UPDATE_ALL          8       /* Recreate screen. */
  71
  72static void con3270_update(struct con3270 *);
  73
  74/*
  75 * Setup timeout for a device. On timeout trigger an update.
  76 */
  77static void con3270_set_timer(struct con3270 *cp, int expires)
  78{
  79        if (expires == 0)
  80                del_timer(&cp->timer);
  81        else
  82                mod_timer(&cp->timer, jiffies + expires);
  83}
  84
  85/*
  86 * The status line is the last line of the screen. It shows the string
  87 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
  88 * in the lower right corner of the screen.
  89 */
  90static void
  91con3270_update_status(struct con3270 *cp)
  92{
  93        char *str;
  94
  95        str = (cp->nr_up != 0) ? "History" : "Running";
  96        memcpy(cp->status->string + 24, str, 7);
  97        codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
  98        cp->update_flags |= CON_UPDATE_STATUS;
  99}
 100
 101static void
 102con3270_create_status(struct con3270 *cp)
 103{
 104        static const unsigned char blueprint[] =
 105                { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
 106                  'c','o','n','s','o','l','e',' ','v','i','e','w',
 107                  TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
 108
 109        cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
 110        /* Copy blueprint to status line */
 111        memcpy(cp->status->string, blueprint, sizeof(blueprint));
 112        /* Set TO_RA addresses. */
 113        raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
 114                               cp->view.cols * (cp->view.rows - 1));
 115        raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
 116                               cp->view.cols * cp->view.rows - 8);
 117        /* Convert strings to ebcdic. */
 118        codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
 119        codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
 120}
 121
 122/*
 123 * Set output offsets to 3270 datastream fragment of a console string.
 124 */
 125static void
 126con3270_update_string(struct con3270 *cp, struct string *s, int nr)
 127{
 128        if (s->len < 4) {
 129                /* This indicates a bug, but printing a warning would
 130                 * cause a deadlock. */
 131                return;
 132        }
 133        if (s->string[s->len - 4] != TO_RA)
 134                return;
 135        raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
 136                               cp->view.cols * (nr + 1));
 137}
 138
 139/*
 140 * Rebuild update list to print all lines.
 141 */
 142static void
 143con3270_rebuild_update(struct con3270 *cp)
 144{
 145        struct string *s, *n;
 146        int nr;
 147
 148        /* 
 149         * Throw away update list and create a new one,
 150         * containing all lines that will fit on the screen.
 151         */
 152        list_for_each_entry_safe(s, n, &cp->update, update)
 153                list_del_init(&s->update);
 154        nr = cp->view.rows - 2 + cp->nr_up;
 155        list_for_each_entry_reverse(s, &cp->lines, list) {
 156                if (nr < cp->view.rows - 1)
 157                        list_add(&s->update, &cp->update);
 158                if (--nr < 0)
 159                        break;
 160        }
 161        cp->line_nr = 0;
 162        cp->update_flags |= CON_UPDATE_LIST;
 163}
 164
 165/*
 166 * Alloc string for size bytes. Free strings from history if necessary.
 167 */
 168static struct string *
 169con3270_alloc_string(struct con3270 *cp, size_t size)
 170{
 171        struct string *s, *n;
 172
 173        s = alloc_string(&cp->freemem, size);
 174        if (s)
 175                return s;
 176        list_for_each_entry_safe(s, n, &cp->lines, list) {
 177                list_del(&s->list);
 178                if (!list_empty(&s->update))
 179                        list_del(&s->update);
 180                cp->nr_lines--;
 181                if (free_string(&cp->freemem, s) >= size)
 182                        break;
 183        }
 184        s = alloc_string(&cp->freemem, size);
 185        BUG_ON(!s);
 186        if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
 187                cp->nr_up = cp->nr_lines - cp->view.rows + 1;
 188                con3270_rebuild_update(cp);
 189                con3270_update_status(cp);
 190        }
 191        return s;
 192}
 193
 194/*
 195 * Write completion callback.
 196 */
 197static void
 198con3270_write_callback(struct raw3270_request *rq, void *data)
 199{
 200        raw3270_request_reset(rq);
 201        xchg(&((struct con3270 *) rq->view)->write, rq);
 202}
 203
 204/*
 205 * Update console display.
 206 */
 207static void
 208con3270_update(struct con3270 *cp)
 209{
 210        struct raw3270_request *wrq;
 211        char wcc, prolog[6];
 212        unsigned long flags;
 213        unsigned long updated;
 214        struct string *s, *n;
 215        int rc;
 216
 217        if (!auto_update && !raw3270_view_active(&cp->view))
 218                return;
 219        if (cp->view.dev)
 220                raw3270_activate_view(&cp->view);
 221
 222        wrq = xchg(&cp->write, 0);
 223        if (!wrq) {
 224                con3270_set_timer(cp, 1);
 225                return;
 226        }
 227
 228        spin_lock_irqsave(&cp->view.lock, flags);
 229        updated = 0;
 230        if (cp->update_flags & CON_UPDATE_ALL) {
 231                con3270_rebuild_update(cp);
 232                con3270_update_status(cp);
 233                cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
 234                        CON_UPDATE_STATUS;
 235        }
 236        if (cp->update_flags & CON_UPDATE_ERASE) {
 237                /* Use erase write alternate to initialize display. */
 238                raw3270_request_set_cmd(wrq, TC_EWRITEA);
 239                updated |= CON_UPDATE_ERASE;
 240        } else
 241                raw3270_request_set_cmd(wrq, TC_WRITE);
 242
 243        wcc = TW_NONE;
 244        raw3270_request_add_data(wrq, &wcc, 1);
 245
 246        /*
 247         * Update status line.
 248         */
 249        if (cp->update_flags & CON_UPDATE_STATUS)
 250                if (raw3270_request_add_data(wrq, cp->status->string,
 251                                             cp->status->len) == 0)
 252                        updated |= CON_UPDATE_STATUS;
 253
 254        if (cp->update_flags & CON_UPDATE_LIST) {
 255                prolog[0] = TO_SBA;
 256                prolog[3] = TO_SA;
 257                prolog[4] = TAT_COLOR;
 258                prolog[5] = TAC_TURQ;
 259                raw3270_buffer_address(cp->view.dev, prolog + 1,
 260                                       cp->view.cols * cp->line_nr);
 261                raw3270_request_add_data(wrq, prolog, 6);
 262                /* Write strings in the update list to the screen. */
 263                list_for_each_entry_safe(s, n, &cp->update, update) {
 264                        if (s != cp->cline)
 265                                con3270_update_string(cp, s, cp->line_nr);
 266                        if (raw3270_request_add_data(wrq, s->string,
 267                                                     s->len) != 0)
 268                                break;
 269                        list_del_init(&s->update);
 270                        if (s != cp->cline)
 271                                cp->line_nr++;
 272                }
 273                if (list_empty(&cp->update))
 274                        updated |= CON_UPDATE_LIST;
 275        }
 276        wrq->callback = con3270_write_callback;
 277        rc = raw3270_start(&cp->view, wrq);
 278        if (rc == 0) {
 279                cp->update_flags &= ~updated;
 280                if (cp->update_flags)
 281                        con3270_set_timer(cp, 1);
 282        } else {
 283                raw3270_request_reset(wrq);
 284                xchg(&cp->write, wrq);
 285        }
 286        spin_unlock_irqrestore(&cp->view.lock, flags);
 287}
 288
 289/*
 290 * Read tasklet.
 291 */
 292static void
 293con3270_read_tasklet(struct raw3270_request *rrq)
 294{
 295        static char kreset_data = TW_KR;
 296        struct con3270 *cp;
 297        unsigned long flags;
 298        int nr_up, deactivate;
 299
 300        cp = (struct con3270 *) rrq->view;
 301        spin_lock_irqsave(&cp->view.lock, flags);
 302        nr_up = cp->nr_up;
 303        deactivate = 0;
 304        /* Check aid byte. */
 305        switch (cp->input->string[0]) {
 306        case 0x7d:      /* enter: jump to bottom. */
 307                nr_up = 0;
 308                break;
 309        case 0xf3:      /* PF3: deactivate the console view. */
 310                deactivate = 1;
 311                break;
 312        case 0x6d:      /* clear: start from scratch. */
 313                cp->update_flags = CON_UPDATE_ALL;
 314                con3270_set_timer(cp, 1);
 315                break;
 316        case 0xf7:      /* PF7: do a page up in the console log. */
 317                nr_up += cp->view.rows - 2;
 318                if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
 319                        nr_up = cp->nr_lines - cp->view.rows + 1;
 320                        if (nr_up < 0)
 321                                nr_up = 0;
 322                }
 323                break;
 324        case 0xf8:      /* PF8: do a page down in the console log. */
 325                nr_up -= cp->view.rows - 2;
 326                if (nr_up < 0)
 327                        nr_up = 0;
 328                break;
 329        }
 330        if (nr_up != cp->nr_up) {
 331                cp->nr_up = nr_up;
 332                con3270_rebuild_update(cp);
 333                con3270_update_status(cp);
 334                con3270_set_timer(cp, 1);
 335        }
 336        spin_unlock_irqrestore(&cp->view.lock, flags);
 337
 338        /* Start keyboard reset command. */
 339        raw3270_request_reset(cp->kreset);
 340        raw3270_request_set_cmd(cp->kreset, TC_WRITE);
 341        raw3270_request_add_data(cp->kreset, &kreset_data, 1);
 342        raw3270_start(&cp->view, cp->kreset);
 343
 344        if (deactivate)
 345                raw3270_deactivate_view(&cp->view);
 346
 347        raw3270_request_reset(rrq);
 348        xchg(&cp->read, rrq);
 349        raw3270_put_view(&cp->view);
 350}
 351
 352/*
 353 * Read request completion callback.
 354 */
 355static void
 356con3270_read_callback(struct raw3270_request *rq, void *data)
 357{
 358        raw3270_get_view(rq->view);
 359        /* Schedule tasklet to pass input to tty. */
 360        tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
 361}
 362
 363/*
 364 * Issue a read request. Called only from interrupt function.
 365 */
 366static void
 367con3270_issue_read(struct con3270 *cp)
 368{
 369        struct raw3270_request *rrq;
 370        int rc;
 371
 372        rrq = xchg(&cp->read, 0);
 373        if (!rrq)
 374                /* Read already scheduled. */
 375                return;
 376        rrq->callback = con3270_read_callback;
 377        rrq->callback_data = cp;
 378        raw3270_request_set_cmd(rrq, TC_READMOD);
 379        raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
 380        /* Issue the read modified request. */
 381        rc = raw3270_start_irq(&cp->view, rrq);
 382        if (rc)
 383                raw3270_request_reset(rrq);
 384}
 385
 386/*
 387 * Switch to the console view.
 388 */
 389static int
 390con3270_activate(struct raw3270_view *view)
 391{
 392        struct con3270 *cp;
 393
 394        cp = (struct con3270 *) view;
 395        cp->update_flags = CON_UPDATE_ALL;
 396        con3270_set_timer(cp, 1);
 397        return 0;
 398}
 399
 400static void
 401con3270_deactivate(struct raw3270_view *view)
 402{
 403        struct con3270 *cp;
 404
 405        cp = (struct con3270 *) view;
 406        del_timer(&cp->timer);
 407}
 408
 409static void
 410con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
 411{
 412        /* Handle ATTN. Schedule tasklet to read aid. */
 413        if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
 414                con3270_issue_read(cp);
 415
 416        if (rq) {
 417                if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
 418                        rq->rc = -EIO;
 419                else
 420                        /* Normal end. Copy residual count. */
 421                        rq->rescnt = irb->scsw.cmd.count;
 422        } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
 423                /* Interrupt without an outstanding request -> update all */
 424                cp->update_flags = CON_UPDATE_ALL;
 425                con3270_set_timer(cp, 1);
 426        }
 427}
 428
 429/* Console view to a 3270 device. */
 430static struct raw3270_fn con3270_fn = {
 431        .activate = con3270_activate,
 432        .deactivate = con3270_deactivate,
 433        .intv = (void *) con3270_irq
 434};
 435
 436static inline void
 437con3270_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
 447static inline void
 448con3270_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
 458static inline void
 459con3270_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 */
 489static void
 490con3270_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
 514static struct tty_driver *
 515con3270_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 */
 524static void
 525con3270_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 */
 537static void
 538con3270_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
 562static int con3270_notify(struct notifier_block *self,
 563                          unsigned long event, void *data)
 564{
 565        con3270_flush();
 566        return NOTIFY_OK;
 567}
 568
 569static struct notifier_block on_panic_nb = {
 570        .notifier_call = con3270_notify,
 571        .priority = 0,
 572};
 573
 574static 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 */
 582static 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 */
 592static int __init
 593con3270_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        if (!condev)
 615                return -ENOMEM;
 616        condev->view.dev = rp;
 617
 618        condev->read = raw3270_request_alloc(0);
 619        condev->read->callback = con3270_read_callback;
 620        condev->read->callback_data = condev;
 621        condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
 622        condev->kreset = raw3270_request_alloc(1);
 623
 624        INIT_LIST_HEAD(&condev->lines);
 625        INIT_LIST_HEAD(&condev->update);
 626        setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
 627                    (unsigned long) condev);
 628        tasklet_init(&condev->readlet, 
 629                     (void (*)(unsigned long)) con3270_read_tasklet,
 630                     (unsigned long) condev->read);
 631
 632        raw3270_add_view(&condev->view, &con3270_fn, 1);
 633
 634        INIT_LIST_HEAD(&condev->freemem);
 635        for (i = 0; i < CON3270_STRING_PAGES; i++) {
 636                cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
 637                add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
 638        }
 639        condev->cline = alloc_string(&condev->freemem, condev->view.cols);
 640        condev->cline->len = 0;
 641        con3270_create_status(condev);
 642        condev->input = alloc_string(&condev->freemem, 80);
 643        atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
 644        register_reboot_notifier(&on_reboot_nb);
 645        register_console(&con3270);
 646        return 0;
 647}
 648
 649console_initcall(con3270_init);
 650