Free Electrons

Embedded Linux Experts

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
/*
 * IBM eServer Hypervisor Virtual Console Server Device Driver
 * Copyright (C) 2003, 2004 IBM Corp.
 *  Ryan S. Arnold (rsa@us.ibm.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * Author(s) :  Ryan S. Arnold <rsa@us.ibm.com>
 *
 * This is the device driver for the IBM Hypervisor Virtual Console Server,
 * "hvcs".  The IBM hvcs provides a tty driver interface to allow Linux
 * user space applications access to the system consoles of logically
 * partitioned operating systems, e.g. Linux, running on the same partitioned
 * Power5 ppc64 system.  Physical hardware consoles per partition are not
 * practical on this hardware so system consoles are accessed by this driver
 * using inter-partition firmware interfaces to virtual terminal devices.
 *
 * A vty is known to the HMC as a "virtual serial server adapter".  It is a
 * virtual terminal device that is created by firmware upon partition creation
 * to act as a partitioned OS's console device.
 *
 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
 * Linux system upon their creation by the HMC or their exposure during boot.
 * The non-user interactive backend of this driver is implemented as a vio
 * device driver so that it can receive notification of vty-server lifetimes
 * after it registers with the vio bus to handle vty-server probe and remove
 * callbacks.
 *
 * Many vty-servers can be configured to connect to one vty, but a vty can
 * only be actively connected to by a single vty-server, in any manner, at one
 * time.  If the HMC is currently hosting the console for a target Linux
 * partition; attempts to open the tty device to the partition's console using
 * the hvcs on any partition will return -EBUSY with every open attempt until
 * the HMC frees the connection between its vty-server and the desired
 * partition's vty device.  Conversely, a vty-server may only be connected to
 * a single vty at one time even though it may have several configured vty
 * partner possibilities.
 *
 * Firmware does not provide notification of vty partner changes to this
 * driver.  This means that an HMC Super Admin may add or remove partner vtys
 * from a vty-server's partner list but the changes will not be signaled to
 * the vty-server.  Firmware only notifies the driver when a vty-server is
 * added or removed from the system.  To compensate for this deficiency, this
 * driver implements a sysfs update attribute which provides a method for
 * rescanning partner information upon a user's request.
 *
 * Each vty-server, prior to being exposed to this driver is reference counted
 * using the 2.6 Linux kernel kref construct.
 *
 * For direction on installation and usage of this driver please reference
 * Documentation/powerpc/hvcs.txt.
 */

#include <linux/device.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/kref.h>
#include <linux/kthread.h>
#include <linux/list.h>
#include <linux/major.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stat.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <asm/hvconsole.h>
#include <asm/hvcserver.h>
#include <asm/uaccess.h>
#include <asm/vio.h>

/*
 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
 * Removed braces around single statements following conditionals.  Removed '=
 * 0' after static int declarations since these default to zero.  Removed
 * list_for_each_safe() and replaced with list_for_each_entry() in
 * hvcs_get_by_index().  The 'safe' version is un-needed now that the driver is
 * using spinlocks.  Changed spin_lock_irqsave() to spin_lock() when locking
 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
 * handler.  Initialized hvcs_structs_lock and hvcs_pi_lock to
 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
 * list traversals from a deletion.  Removed '= NULL' from pointer declaration
 * statements since they are initialized NULL by default.  Removed wmb()
 * instances from hvcs_try_write().  They probably aren't needed with locking in
 * place.  Added check and cleanup for hvcs_pi_buff = kmalloc() in
 * hvcs_module_init().  Exposed hvcs_struct.index via a sysfs attribute so that
 * the coupling between /dev/hvcs* and a vty-server can be automatically
 * determined.  Moved kobject_put() in hvcs_open outside of the
 * spin_unlock_irqrestore().
 *
 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
 * align with how the tty layer always assigns the lowest index available.  This
 * change resulted in a list of ints that denotes which indexes are available.
 * Device additions and removals use the new hvcs_get_index() and
 * hvcs_return_index() helper functions.  The list is created with
 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
 * Without these fixes hotplug vty-server adapter support goes crazy with this
 * driver if the user removes a vty-server adapter.  Moved free_irq() outside of
 * the hvcs_final_close() function in order to get it out of the spinlock.
 * Rearranged hvcs_close().  Cleaned up some printks and did some housekeeping
 * on the changelog.  Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
 * arch/powerepc/include/asm/hvcserver.h
 *
 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
 * prevent possible lockup with realtime scheduling as similarly pointed out by
 * akpm in hvc_console.  Changed resulted in the removal of hvcs_final_close()
 * to reorder cleanup operations and prevent discarding of pending data during
 * an hvcs_close().  Removed spinlock protection of hvcs_struct data members in
 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
 */

#define HVCS_DRIVER_VERSION "1.3.3"

MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(HVCS_DRIVER_VERSION);

/*
 * Wait this long per iteration while trying to push buffered data to the
 * hypervisor before allowing the tty to complete a close operation.
 */
#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */

/*
 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
 * addition of tty derived devices and we shouldn't allocate thousands of
 * tty_device pointers when the number of vty-server & vty partner connections
 * will most often be much lower than this, we'll arbitrarily allocate
 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
 * register the tty_driver. This can be overridden using an insmod parameter.
 */
#define HVCS_DEFAULT_SERVER_ADAPTERS	64

/*
 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
 * nodes as a sanity check.  Theoretically there can be over 1 Billion
 * vty-server & vty partner connections.
 */
#define HVCS_MAX_SERVER_ADAPTERS	1024

/*
 * We let Linux assign us a major number and we start the minors at zero.  There
 * is no intuitive mapping between minor number and the target vty-server
 * adapter except that each new vty-server adapter is always assigned to the
 * smallest minor number available.
 */
#define HVCS_MINOR_START	0

/*
 * The hcall interface involves putting 8 chars into each of two registers.
 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
 * by casting char[16] to long[2].  It would work without __ALIGNED__, but a 
 * little (tiny) bit slower because an unaligned load is slower than aligned 
 * load.
 */
#define __ALIGNED__	__attribute__((__aligned__(8)))

/*
 * How much data can firmware send with each hvc_put_chars()?  Maybe this
 * should be moved into an architecture specific area.
 */
#define HVCS_BUFF_LEN	16

/*
 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
 * once in a chunk as a sanity check.
 */
#define HVCS_MAX_FROM_USER	4096

/*
 * Be careful when adding flags to this line discipline.  Don't add anything
 * that will cause echoing or we'll go into recursive loop echoing chars back
 * and forth with the console drivers.
 */
static struct ktermios hvcs_tty_termios = {
	.c_iflag = IGNBRK | IGNPAR,
	.c_oflag = OPOST,
	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
	.c_cc = INIT_C_CC,
	.c_ispeed = 38400,
	.c_ospeed = 38400
};

/*
 * This value is used to take the place of a command line parameter when the
 * module is inserted.  It starts as -1 and stays as such if the user doesn't
 * specify a module insmod parameter.  If they DO specify one then it is set to
 * the value of the integer passed in.
 */
static int hvcs_parm_num_devs = -1;
module_param(hvcs_parm_num_devs, int, 0);

static const char hvcs_driver_name[] = "hvcs";
static const char hvcs_device_node[] = "hvcs";
static const char hvcs_driver_string[]
	= "IBM hvcs (Hypervisor Virtual Console Server) Driver";

/* Status of partner info rescan triggered via sysfs. */
static int hvcs_rescan_status;

static struct tty_driver *hvcs_tty_driver;

/*
 * In order to be somewhat sane this driver always associates the hvcs_struct
 * index element with the numerically equal tty->index.  This means that a
 * hotplugged vty-server adapter will always map to the lowest index valued
 * device node.  If vty-servers were hotplug removed from the system and then
 * new ones added the new vty-server may have the largest slot number of all
 * the vty-server adapters in the partition but it may have the lowest dev node
 * index of all the adapters due to the hole left by the hotplug removed
 * adapter.  There are a set of functions provided to get the lowest index for
 * a new device as well as return the index to the list.  This list is allocated
 * with a number of elements equal to the number of device nodes requested when
 * the module was inserted.
 */
static int *hvcs_index_list;

/*
 * How large is the list?  This is kept for traversal since the list is
 * dynamically created.
 */
static int hvcs_index_count;

/*
 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
 */
static int hvcs_kicked;

/*
 * Use by the kthread construct for task operations like waking the sleeping
 * thread and stopping the kthread.
 */
static struct task_struct *hvcs_task;

/*
 * We allocate this for the use of all of the hvcs_structs when they fetch
 * partner info.
 */
static unsigned long *hvcs_pi_buff;

/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
static DEFINE_SPINLOCK(hvcs_pi_lock);

/* One vty-server per hvcs_struct */
struct hvcs_struct {
	spinlock_t lock;

	/*
	 * This index identifies this hvcs device as the complement to a
	 * specific tty index.
	 */
	unsigned int index;

	struct tty_struct *tty;
	int open_count;

	/*
	 * Used to tell the driver kernel_thread what operations need to take
	 * place upon this hvcs_struct instance.
	 */
	int todo_mask;

	/*
	 * This buffer is required so that when hvcs_write_room() reports that
	 * it can send HVCS_BUFF_LEN characters that it will buffer the full
	 * HVCS_BUFF_LEN characters if need be.  This is essential for opost
	 * writes since they do not do high level buffering and expect to be
	 * able to send what the driver commits to sending buffering
	 * [e.g. tab to space conversions in n_tty.c opost()].
	 */
	char buffer[HVCS_BUFF_LEN];
	int chars_in_buffer;

	/*
	 * Any variable below the kref is valid before a tty is connected and
	 * stays valid after the tty is disconnected.  These shouldn't be
	 * whacked until the kobject refcount reaches zero though some entries
	 * may be changed via sysfs initiatives.
	 */
	struct kref kref; /* ref count & hvcs_struct lifetime */
	int connected; /* is the vty-server currently connected to a vty? */
	uint32_t p_unit_address; /* partner unit address */
	uint32_t p_partition_ID; /* partner partition ID */
	char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
	struct list_head next; /* list management */
	struct vio_dev *vdev;
};

/* Required to back map a kref to its containing object */
#define from_kref(k) container_of(k, struct hvcs_struct, kref)

static LIST_HEAD(hvcs_structs);
static DEFINE_SPINLOCK(hvcs_structs_lock);
static DEFINE_MUTEX(hvcs_init_mutex);

static void hvcs_unthrottle(struct tty_struct *tty);
static void hvcs_throttle(struct tty_struct *tty);
static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);

static int hvcs_write(struct tty_struct *tty,
		const unsigned char *buf, int count);
static int hvcs_write_room(struct tty_struct *tty);
static int hvcs_chars_in_buffer(struct tty_struct *tty);

static int hvcs_has_pi(struct hvcs_struct *hvcsd);
static void hvcs_set_pi(struct hvcs_partner_info *pi,
		struct hvcs_struct *hvcsd);
static int hvcs_get_pi(struct hvcs_struct *hvcsd);
static int hvcs_rescan_devices_list(void);

static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
static void hvcs_partner_free(struct hvcs_struct *hvcsd);

static int hvcs_enable_device(struct hvcs_struct *hvcsd,
		uint32_t unit_address, unsigned int irq, struct vio_dev *dev);

static int hvcs_open(struct tty_struct *tty, struct file *filp);
static void hvcs_close(struct tty_struct *tty, struct file *filp);
static void hvcs_hangup(struct tty_struct * tty);

static int __devinit hvcs_probe(struct vio_dev *dev,
		const struct vio_device_id *id);
static int __devexit hvcs_remove(struct vio_dev *dev);
static int __init hvcs_module_init(void);
static void __exit hvcs_module_exit(void);
static int __devinit hvcs_initialize(void);

#define HVCS_SCHED_READ	0x00000001
#define HVCS_QUICK_READ	0x00000002
#define HVCS_TRY_WRITE	0x00000004
#define HVCS_READ_MASK	(HVCS_SCHED_READ | HVCS_QUICK_READ)

static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
{
	return dev_get_drvdata(&viod->dev);
}
/* The sysfs interface for the driver and devices */

static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;
	int retval;

	spin_lock_irqsave(&hvcsd->lock, flags);
	retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return retval;
}
static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);

static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;
	int retval;

	spin_lock_irqsave(&hvcsd->lock, flags);
	retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return retval;
}
static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);

static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
		size_t count)
{
	/*
	 * Don't need this feature at the present time because firmware doesn't
	 * yet support multiple partners.
	 */
	printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
	return -EPERM;
}

static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;
	int retval;

	spin_lock_irqsave(&hvcsd->lock, flags);
	retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return retval;
}

static DEVICE_ATTR(current_vty,
	S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);

static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
		size_t count)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;

	/* writing a '0' to this sysfs entry will result in the disconnect. */
	if (simple_strtol(buf, NULL, 0) != 0)
		return -EINVAL;

	spin_lock_irqsave(&hvcsd->lock, flags);

	if (hvcsd->open_count > 0) {
		spin_unlock_irqrestore(&hvcsd->lock, flags);
		printk(KERN_INFO "HVCS: vterm state unchanged.  "
				"The hvcs device node is still in use.\n");
		return -EPERM;
	}

	if (hvcsd->connected == 0) {
		spin_unlock_irqrestore(&hvcsd->lock, flags);
		printk(KERN_INFO "HVCS: vterm state unchanged. The"
				" vty-server is not connected to a vty.\n");
		return -EPERM;
	}

	hvcs_partner_free(hvcsd);
	printk(KERN_INFO "HVCS: Closed vty-server@%X and"
			" partner vty@%X:%d connection.\n",
			hvcsd->vdev->unit_address,
			hvcsd->p_unit_address,
			(uint32_t)hvcsd->p_partition_ID);

	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return count;
}

static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;
	int retval;

	spin_lock_irqsave(&hvcsd->lock, flags);
	retval = sprintf(buf, "%d\n", hvcsd->connected);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return retval;
}
static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
		hvcs_vterm_state_show, hvcs_vterm_state_store);

static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct vio_dev *viod = to_vio_dev(dev);
	struct hvcs_struct *hvcsd = from_vio_dev(viod);
	unsigned long flags;
	int retval;

	spin_lock_irqsave(&hvcsd->lock, flags);
	retval = sprintf(buf, "%d\n", hvcsd->index);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return retval;
}

static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);

static struct attribute *hvcs_attrs[] = {
	&dev_attr_partner_vtys.attr,
	&dev_attr_partner_clcs.attr,
	&dev_attr_current_vty.attr,
	&dev_attr_vterm_state.attr,
	&dev_attr_index.attr,
	NULL,
};

static struct attribute_group hvcs_attr_group = {
	.attrs = hvcs_attrs,
};

static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
{
	/* A 1 means it is updating, a 0 means it is done updating */
	return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
}

static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
		size_t count)
{
	if ((simple_strtol(buf, NULL, 0) != 1)
		&& (hvcs_rescan_status != 0))
		return -EINVAL;

	hvcs_rescan_status = 1;
	printk(KERN_INFO "HVCS: rescanning partner info for all"
		" vty-servers.\n");
	hvcs_rescan_devices_list();
	hvcs_rescan_status = 0;
	return count;
}

static DRIVER_ATTR(rescan,
	S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);

static void hvcs_kick(void)
{
	hvcs_kicked = 1;
	wmb();
	wake_up_process(hvcs_task);
}

static void hvcs_unthrottle(struct tty_struct *tty)
{
	struct hvcs_struct *hvcsd = tty->driver_data;
	unsigned long flags;

	spin_lock_irqsave(&hvcsd->lock, flags);
	hvcsd->todo_mask |= HVCS_SCHED_READ;
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	hvcs_kick();
}

static void hvcs_throttle(struct tty_struct *tty)
{
	struct hvcs_struct *hvcsd = tty->driver_data;
	unsigned long flags;

	spin_lock_irqsave(&hvcsd->lock, flags);
	vio_disable_interrupts(hvcsd->vdev);
	spin_unlock_irqrestore(&hvcsd->lock, flags);
}

/*
 * If the device is being removed we don't have to worry about this interrupt
 * handler taking any further interrupts because they are disabled which means
 * the hvcs_struct will always be valid in this handler.
 */
static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
{
	struct hvcs_struct *hvcsd = dev_instance;

	spin_lock(&hvcsd->lock);
	vio_disable_interrupts(hvcsd->vdev);
	hvcsd->todo_mask |= HVCS_SCHED_READ;
	spin_unlock(&hvcsd->lock);
	hvcs_kick();

	return IRQ_HANDLED;
}

/* This function must be called with the hvcsd->lock held */
static void hvcs_try_write(struct hvcs_struct *hvcsd)
{
	uint32_t unit_address = hvcsd->vdev->unit_address;
	struct tty_struct *tty = hvcsd->tty;
	int sent;

	if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
		/* won't send partial writes */
		sent = hvc_put_chars(unit_address,
				&hvcsd->buffer[0],
				hvcsd->chars_in_buffer );
		if (sent > 0) {
			hvcsd->chars_in_buffer = 0;
			/* wmb(); */
			hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
			/* wmb(); */

			/*
			 * We are still obligated to deliver the data to the
			 * hypervisor even if the tty has been closed because
			 * we committed to delivering it.  But don't try to wake
			 * a non-existent tty.
			 */
			if (tty) {
				tty_wakeup(tty);
			}
		}
	}
}

static int hvcs_io(struct hvcs_struct *hvcsd)
{
	uint32_t unit_address;
	struct tty_struct *tty;
	char buf[HVCS_BUFF_LEN] __ALIGNED__;
	unsigned long flags;
	int got = 0;

	spin_lock_irqsave(&hvcsd->lock, flags);

	unit_address = hvcsd->vdev->unit_address;
	tty = hvcsd->tty;

	hvcs_try_write(hvcsd);

	if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
		hvcsd->todo_mask &= ~(HVCS_READ_MASK);
		goto bail;
	} else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
		goto bail;

	/* remove the read masks */
	hvcsd->todo_mask &= ~(HVCS_READ_MASK);

	if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
		got = hvc_get_chars(unit_address,
				&buf[0],
				HVCS_BUFF_LEN);
		tty_insert_flip_string(tty, buf, got);
	}

	/* Give the TTY time to process the data we just sent. */
	if (got)
		hvcsd->todo_mask |= HVCS_QUICK_READ;

	spin_unlock_irqrestore(&hvcsd->lock, flags);
	/* This is synch because tty->low_latency == 1 */
	if(got)
		tty_flip_buffer_push(tty);

	if (!got) {
		/* Do this _after_ the flip_buffer_push */
		spin_lock_irqsave(&hvcsd->lock, flags);
		vio_enable_interrupts(hvcsd->vdev);
		spin_unlock_irqrestore(&hvcsd->lock, flags);
	}

	return hvcsd->todo_mask;

 bail:
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	return hvcsd->todo_mask;
}

static int khvcsd(void *unused)
{
	struct hvcs_struct *hvcsd;
	int hvcs_todo_mask;

	__set_current_state(TASK_RUNNING);

	do {
		hvcs_todo_mask = 0;
		hvcs_kicked = 0;
		wmb();

		spin_lock(&hvcs_structs_lock);
		list_for_each_entry(hvcsd, &hvcs_structs, next) {
			hvcs_todo_mask |= hvcs_io(hvcsd);
		}
		spin_unlock(&hvcs_structs_lock);

		/*
		 * If any of the hvcs adapters want to try a write or quick read
		 * don't schedule(), yield a smidgen then execute the hvcs_io
		 * thread again for those that want the write.
		 */
		 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
			yield();
			continue;
		}

		set_current_state(TASK_INTERRUPTIBLE);
		if (!hvcs_kicked)
			schedule();
		__set_current_state(TASK_RUNNING);
	} while (!kthread_should_stop());

	return 0;
}

static struct vio_device_id hvcs_driver_table[] __devinitdata= {
	{"serial-server", "hvterm2"},
	{ "", "" }
};
MODULE_DEVICE_TABLE(vio, hvcs_driver_table);

static void hvcs_return_index(int index)
{
	/* Paranoia check */
	if (!hvcs_index_list)
		return;
	if (index < 0 || index >= hvcs_index_count)
		return;
	if (hvcs_index_list[index] == -1)
		return;
	else
		hvcs_index_list[index] = -1;
}

/* callback when the kref ref count reaches zero */
static void destroy_hvcs_struct(struct kref *kref)
{
	struct hvcs_struct *hvcsd = from_kref(kref);
	struct vio_dev *vdev;
	unsigned long flags;

	spin_lock(&hvcs_structs_lock);
	spin_lock_irqsave(&hvcsd->lock, flags);

	/* the list_del poisons the pointers */
	list_del(&(hvcsd->next));

	if (hvcsd->connected == 1) {
		hvcs_partner_free(hvcsd);
		printk(KERN_INFO "HVCS: Closed vty-server@%X and"
				" partner vty@%X:%d connection.\n",
				hvcsd->vdev->unit_address,
				hvcsd->p_unit_address,
				(uint32_t)hvcsd->p_partition_ID);
	}
	printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
			hvcsd->vdev->unit_address);

	vdev = hvcsd->vdev;
	hvcsd->vdev = NULL;

	hvcsd->p_unit_address = 0;
	hvcsd->p_partition_ID = 0;
	hvcs_return_index(hvcsd->index);
	memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);

	spin_unlock_irqrestore(&hvcsd->lock, flags);
	spin_unlock(&hvcs_structs_lock);

	sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);

	kfree(hvcsd);
}

static int hvcs_get_index(void)
{
	int i;
	/* Paranoia check */
	if (!hvcs_index_list) {
		printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
		return -EFAULT;
	}
	/* Find the numerically lowest first free index. */
	for(i = 0; i < hvcs_index_count; i++) {
		if (hvcs_index_list[i] == -1) {
			hvcs_index_list[i] = 0;
			return i;
		}
	}
	return -1;
}

static int __devinit hvcs_probe(
	struct vio_dev *dev,
	const struct vio_device_id *id)
{
	struct hvcs_struct *hvcsd;
	int index, rc;
	int retval;

	if (!dev || !id) {
		printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
		return -EPERM;
	}

	/* Make sure we are properly initialized */
	rc = hvcs_initialize();
	if (rc) {
		pr_err("HVCS: Failed to initialize core driver.\n");
		return rc;
	}

	/* early to avoid cleanup on failure */
	index = hvcs_get_index();
	if (index < 0) {
		return -EFAULT;
	}

	hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
	if (!hvcsd)
		return -ENODEV;


	spin_lock_init(&hvcsd->lock);
	/* Automatically incs the refcount the first time */
	kref_init(&hvcsd->kref);

	hvcsd->vdev = dev;
	dev_set_drvdata(&dev->dev, hvcsd);

	hvcsd->index = index;

	/* hvcsd->index = ++hvcs_struct_count; */
	hvcsd->chars_in_buffer = 0;
	hvcsd->todo_mask = 0;
	hvcsd->connected = 0;

	/*
	 * This will populate the hvcs_struct's partner info fields for the
	 * first time.
	 */
	if (hvcs_get_pi(hvcsd)) {
		printk(KERN_ERR "HVCS: Failed to fetch partner"
			" info for vty-server@%X on device probe.\n",
			hvcsd->vdev->unit_address);
	}

	/*
	 * If a user app opens a tty that corresponds to this vty-server before
	 * the hvcs_struct has been added to the devices list then the user app
	 * will get -ENODEV.
	 */
	spin_lock(&hvcs_structs_lock);
	list_add_tail(&(hvcsd->next), &hvcs_structs);
	spin_unlock(&hvcs_structs_lock);

	retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
	if (retval) {
		printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
		       hvcsd->vdev->unit_address);
		return retval;
	}

	printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);

	/*
	 * DON'T enable interrupts here because there is no user to receive the
	 * data.
	 */
	return 0;
}

static int __devexit hvcs_remove(struct vio_dev *dev)
{
	struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
	unsigned long flags;
	struct tty_struct *tty;

	if (!hvcsd)
		return -ENODEV;

	/* By this time the vty-server won't be getting any more interrupts */

	spin_lock_irqsave(&hvcsd->lock, flags);

	tty = hvcsd->tty;

	spin_unlock_irqrestore(&hvcsd->lock, flags);

	/*
	 * Let the last holder of this object cause it to be removed, which
	 * would probably be tty_hangup below.
	 */
	kref_put(&hvcsd->kref, destroy_hvcs_struct);

	/*
	 * The hangup is a scheduled function which will auto chain call
	 * hvcs_hangup.  The tty should always be valid at this time unless a
	 * simultaneous tty close already cleaned up the hvcs_struct.
	 */
	if (tty)
		tty_hangup(tty);

	printk(KERN_INFO "HVCS: vty-server@%X removed from the"
			" vio bus.\n", dev->unit_address);
	return 0;
};

static struct vio_driver hvcs_vio_driver = {
	.id_table	= hvcs_driver_table,
	.probe		= hvcs_probe,
	.remove		= __devexit_p(hvcs_remove),
	.name		= hvcs_driver_name,
};

/* Only called from hvcs_get_pi please */
static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
{
	int clclength;

	hvcsd->p_unit_address = pi->unit_address;
	hvcsd->p_partition_ID  = pi->partition_ID;
	clclength = strlen(&pi->location_code[0]);
	if (clclength > HVCS_CLC_LENGTH)
		clclength = HVCS_CLC_LENGTH;

	/* copy the null-term char too */
	strncpy(&hvcsd->p_location_code[0],
			&pi->location_code[0], clclength + 1);
}

/*
 * Traverse the list and add the partner info that is found to the hvcs_struct
 * struct entry. NOTE: At this time I know that partner info will return a
 * single entry but in the future there may be multiple partner info entries per
 * vty-server and you'll want to zero out that list and reset it.  If for some
 * reason you have an old version of this driver but there IS more than one
 * partner info then hvcsd->p_* will hold the last partner info data from the
 * firmware query.  A good way to update this code would be to replace the three
 * partner info fields in hvcs_struct with a list of hvcs_partner_info
 * instances.
 *
 * This function must be called with the hvcsd->lock held.
 */
static int hvcs_get_pi(struct hvcs_struct *hvcsd)
{
	struct hvcs_partner_info *pi;
	uint32_t unit_address = hvcsd->vdev->unit_address;
	struct list_head head;
	int retval;

	spin_lock(&hvcs_pi_lock);
	if (!hvcs_pi_buff) {
		spin_unlock(&hvcs_pi_lock);
		return -EFAULT;
	}
	retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
	spin_unlock(&hvcs_pi_lock);
	if (retval) {
		printk(KERN_ERR "HVCS: Failed to fetch partner"
			" info for vty-server@%x.\n", unit_address);
		return retval;
	}

	/* nixes the values if the partner vty went away */
	hvcsd->p_unit_address = 0;
	hvcsd->p_partition_ID = 0;

	list_for_each_entry(pi, &head, node)
		hvcs_set_pi(pi, hvcsd);

	hvcs_free_partner_info(&head);
	return 0;
}

/*
 * This function is executed by the driver "rescan" sysfs entry.  It shouldn't
 * be executed elsewhere, in order to prevent deadlock issues.
 */
static int hvcs_rescan_devices_list(void)
{
	struct hvcs_struct *hvcsd;
	unsigned long flags;

	spin_lock(&hvcs_structs_lock);

	list_for_each_entry(hvcsd, &hvcs_structs, next) {
		spin_lock_irqsave(&hvcsd->lock, flags);
		hvcs_get_pi(hvcsd);
		spin_unlock_irqrestore(&hvcsd->lock, flags);
	}

	spin_unlock(&hvcs_structs_lock);

	return 0;
}

/*
 * Farm this off into its own function because it could be more complex once
 * multiple partners support is added. This function should be called with
 * the hvcsd->lock held.
 */
static int hvcs_has_pi(struct hvcs_struct *hvcsd)
{
	if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
		return 0;
	return 1;
}

/*
 * NOTE: It is possible that the super admin removed a partner vty and then
 * added a different vty as the new partner.
 *
 * This function must be called with the hvcsd->lock held.
 */
static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
{
	int retval;
	unsigned int unit_address = hvcsd->vdev->unit_address;

	/*
	 * If there wasn't any pi when the device was added it doesn't meant
	 * there isn't any now.  This driver isn't notified when a new partner
	 * vty is added to a vty-server so we discover changes on our own.
	 * Please see comments in hvcs_register_connection() for justification
	 * of this bizarre code.
	 */
	retval = hvcs_register_connection(unit_address,
			hvcsd->p_partition_ID,
			hvcsd->p_unit_address);
	if (!retval) {
		hvcsd->connected = 1;
		return 0;
	} else if (retval != -EINVAL)
		return retval;

	/*
	 * As per the spec re-get the pi and try again if -EINVAL after the
	 * first connection attempt.
	 */
	if (hvcs_get_pi(hvcsd))
		return -ENOMEM;

	if (!hvcs_has_pi(hvcsd))
		return -ENODEV;

	retval = hvcs_register_connection(unit_address,
			hvcsd->p_partition_ID,
			hvcsd->p_unit_address);
	if (retval != -EINVAL) {
		hvcsd->connected = 1;
		return retval;
	}

	/*
	 * EBUSY is the most likely scenario though the vty could have been
	 * removed or there really could be an hcall error due to the parameter
	 * data but thanks to ambiguous firmware return codes we can't really
	 * tell.
	 */
	printk(KERN_INFO "HVCS: vty-server or partner"
			" vty is busy.  Try again later.\n");
	return -EBUSY;
}

/* This function must be called with the hvcsd->lock held */
static void hvcs_partner_free(struct hvcs_struct *hvcsd)
{
	int retval;
	do {
		retval = hvcs_free_connection(hvcsd->vdev->unit_address);
	} while (retval == -EBUSY);
	hvcsd->connected = 0;
}

/* This helper function must be called WITHOUT the hvcsd->lock held */
static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
		unsigned int irq, struct vio_dev *vdev)
{
	unsigned long flags;
	int rc;

	/*
	 * It is possible that the vty-server was removed between the time that
	 * the conn was registered and now.
	 */
	if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
				0, "ibmhvcs", hvcsd))) {
		/*
		 * It is possible the vty-server was removed after the irq was
		 * requested but before we have time to enable interrupts.
		 */
		if (vio_enable_interrupts(vdev) == H_SUCCESS)
			return 0;
		else {
			printk(KERN_ERR "HVCS: int enable failed for"
					" vty-server@%X.\n", unit_address);
			free_irq(irq, hvcsd);
		}
	} else
		printk(KERN_ERR "HVCS: irq req failed for"
				" vty-server@%X.\n", unit_address);

	spin_lock_irqsave(&hvcsd->lock, flags);
	hvcs_partner_free(hvcsd);
	spin_unlock_irqrestore(&hvcsd->lock, flags);

	return rc;

}

/*
 * This always increments the kref ref count if the call is successful.
 * Please remember to dec when you are done with the instance.
 *
 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
 * calling this function or you will get deadlock.
 */
static struct hvcs_struct *hvcs_get_by_index(int index)
{
	struct hvcs_struct *hvcsd;
	unsigned long flags;

	spin_lock(&hvcs_structs_lock);
	list_for_each_entry(hvcsd, &hvcs_structs, next) {
		spin_lock_irqsave(&hvcsd->lock, flags);
		if (hvcsd->index == index) {
			kref_get(&hvcsd->kref);
			spin_unlock_irqrestore(&hvcsd->lock, flags);
			spin_unlock(&hvcs_structs_lock);
			return hvcsd;
		}
		spin_unlock_irqrestore(&hvcsd->lock, flags);
	}
	spin_unlock(&hvcs_structs_lock);

	return NULL;
}

/*
 * This is invoked via the tty_open interface when a user app connects to the
 * /dev node.
 */
static int hvcs_open(struct tty_struct *tty, struct file *filp)
{
	struct hvcs_struct *hvcsd;
	int rc, retval = 0;
	unsigned long flags;
	unsigned int irq;
	struct vio_dev *vdev;
	unsigned long unit_address;

	if (tty->driver_data)
		goto fast_open;

	/*
	 * Is there a vty-server that shares the same index?
	 * This function increments the kref index.
	 */
	if (!(hvcsd = hvcs_get_by_index(tty->index))) {
		printk(KERN_WARNING "HVCS: open failed, no device associated"
				" with tty->index %d.\n", tty->index);
		return -ENODEV;
	}

	spin_lock_irqsave(&hvcsd->lock, flags);

	if (hvcsd->connected == 0)
		if ((retval = hvcs_partner_connect(hvcsd)))
			goto error_release;

	hvcsd->open_count = 1;
	hvcsd->tty = tty;
	tty->driver_data = hvcsd;

	memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);

	/*
	 * Save these in the spinlock for the enable operations that need them
	 * outside of the spinlock.
	 */
	irq = hvcsd->vdev->irq;
	vdev = hvcsd->vdev;
	unit_address = hvcsd->vdev->unit_address;

	hvcsd->todo_mask |= HVCS_SCHED_READ;
	spin_unlock_irqrestore(&hvcsd->lock, flags);

	/*
	 * This must be done outside of the spinlock because it requests irqs
	 * and will grab the spinlock and free the connection if it fails.
	 */
	if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
		kref_put(&hvcsd->kref, destroy_hvcs_struct);
		printk(KERN_WARNING "HVCS: enable device failed.\n");
		return rc;
	}

	goto open_success;

fast_open:
	hvcsd = tty->driver_data;

	spin_lock_irqsave(&hvcsd->lock, flags);
	kref_get(&hvcsd->kref);
	hvcsd->open_count++;
	hvcsd->todo_mask |= HVCS_SCHED_READ;
	spin_unlock_irqrestore(&hvcsd->lock, flags);

open_success:
	hvcs_kick();

	printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
		hvcsd->vdev->unit_address );

	return 0;

error_release:
	spin_unlock_irqrestore(&hvcsd->lock, flags);
	kref_put(&hvcsd->kref, destroy_hvcs_struct);

	printk(KERN_WARNING "HVCS: partner connect failed.\n");
	return retval;
}

static void hvcs_close(struct tty_struct *tty, struct file *filp)
{
	struct hvcs_struct *hvcsd;
	unsigned long flags;
	int irq;

	/*
	 * Is someone trying to close the file associated with this device after
	 * we have hung up?  If so tty->driver_data wouldn't be valid.
	 */
	if (tty_hung_up_p(filp))
		return;

	/*
	 * No driver_data means that this close was probably issued after a
	 * failed hvcs_open by the tty layer's release_dev() api and we can just
	 * exit cleanly.
	 */
	if (!tty->driver_data)
		return;

	hvcsd = tty->driver_data;

	spin_lock_irqsave(&hvcsd->lock, flags);
	if (--hvcsd->open_count == 0) {

		vio_disable_interrupts(hvcsd->vdev);

		/*
		 * NULL this early so that the kernel_thread doesn't try to
		 * execute any operations on the TTY even though it is obligated
		 * to deliver any pending I/O to the hypervisor.
		 */
		hvcsd->tty = NULL;

		irq = hvcsd->vdev->irq;
		spin_unlock_irqrestore(&hvcsd->lock, flags);

		tty_wait_until_sent_from_close(tty, HVCS_CLOSE_WAIT);

		/*
		 * This line is important because it tells hvcs_open that this
		 * device needs to be re-configured the next time hvcs_open is
		 * called.
		 */
		tty->driver_data = NULL;

		free_irq(irq, hvcsd);
		kref_put(&hvcsd->kref, destroy_hvcs_struct);
		return;
	} else if (hvcsd->open_count < 0) {
		printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
				" is missmanaged.\n",
		hvcsd->vdev->unit_address, hvcsd->open_count);
	}

	spin_unlock_irqrestore(&hvcsd->lock, flags);
	kref_put(&hvcsd->kref, destroy_hvcs_struct);
}

static void hvcs_hangup(struct tty_struct * tty)
{
	struct hvcs_struct *hvcsd = tty->driver_data;
	unsigned long flags;
	int temp_open_count;
	int irq;

	spin_lock_irqsave(&hvcsd->lock, flags);
	/* Preserve this so that we know how many kref refs to put */
	temp_open_count = hvcsd->open_count;

	/*
	 * Don't kref put inside the spinlock because the destruction
	 * callback may use the spinlock and it may get called before the
	 * spinlock has been released.
	 */
	vio_disable_interrupts(hvcsd->vdev);

	hvcsd->todo_mask = 0;

	/* I don't think the tty needs the hvcs_struct pointer after a hangup */
	hvcsd->tty->driver_data = NULL;
	hvcsd->tty = NULL;

	hvcsd->open_count = 0;

	/* This will drop any buffered data on the floor which is OK in a hangup
	 * scenario. */
	memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
	hvcsd->chars_in_buffer = 0;

	irq = hvcsd->vdev->irq;

	spin_unlock_irqrestore(&hvcsd->lock, flags);

	free_irq(irq, hvcsd);

	/*
	 * We need to kref_put() for every open_count we have since the
	 * tty_hangup() function doesn't invoke a close per open connection on a
	 * non-console device.
	 */
	while(temp_open_count) {
		--temp_open_count;
		/*
		 * The final put will trigger destruction of the hvcs_struct.
		 * NOTE:  If this hangup was signaled from user space then the
		 * final put will never happen.
		 */
		kref_put(&hvcsd->kref, destroy_hvcs_struct);
	}
}

/*
 * NOTE: This is almost always from_user since user level apps interact with the
 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
 * hvcs_remove (which removes the target device and executes tty_hangup()) that
 * tty_hangup will allow hvcs_write time to complete execution before it
 * terminates our device.
 */
static int hvcs_write(struct tty_struct *tty,
		const unsigned char *buf, int count)
{
	struct hvcs_struct *hvcsd = tty->driver_data;
	unsigned int unit_address;
	const unsigned char *charbuf;
	unsigned long flags;
	int total_sent = 0;
	int tosend = 0;
	int result = 0;

	/*
	 * If they don't check the return code off of their open they may
	 * attempt this even if there is no connected device.
	 */
	if (!hvcsd)
		return -ENODEV;

	/* Reasonable size to prevent user level flooding */
	if (count > HVCS_MAX_FROM_USER) {
		printk(KERN_WARNING "HVCS write: count being truncated to"
				" HVCS_MAX_FROM_USER.\n");
		count = HVCS_MAX_FROM_USER;
	}

	charbuf = buf;

	spin_lock_irqsave(&hvcsd->lock, flags);

	/*
	 * Somehow an open succeeded but the device was removed or the
	 * connection terminated between the vty-server and partner vty during
	 * the middle of a write operation?  This is a crummy place to do this
	 * but we want to keep it all in the spinlock.
	 */
	if (hvcsd->open_count <= 0) {
		spin_unlock_irqrestore(&hvcsd->lock, flags);
		return -ENODEV;
	}

	unit_address = hvcsd->vdev->unit_address;

	while (count > 0) {
		tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
		/*
		 * No more space, this probably means that the last call to
		 * hvcs_write() didn't succeed and the buffer was filled up.
		 */
		if (!tosend)
			break;

		memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
				&charbuf[total_sent],
				tosend);

		hvcsd->chars_in_buffer += tosend;

		result = 0;

		/*
		 * If this is true then we don't want to try writing to the
		 * hypervisor because that is the kernel_threads job now.  We'll
		 * just add to the buffer.
		 */
		if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
			/* won't send partial writes */
			result = hvc_put_chars(unit_address,
					&hvcsd->buffer[0],
					hvcsd->chars_in_buffer);

		/*
		 * Since we know we have enough room in hvcsd->buffer for
		 * tosend we record that it was sent regardless of whether the
		 * hypervisor actually took it because we have it buffered.
		 */
		total_sent+=tosend;
		count-=tosend;
		if (result == 0) {
			hvcsd->todo_mask |= HVCS_TRY_WRITE;
			hvcs_kick();
			break;
		}

		hvcsd->chars_in_buffer = 0;
		/*
		 * Test after the chars_in_buffer reset otherwise this could
		 * deadlock our writes if hvc_put_chars fails.
		 */
		if (result < 0)
			break;
	}

	spin_unlock_irqrestore(&hvcsd->lock, flags);

	if (result == -1)
		return -EIO;
	else
		return total_sent;
}

/*
 * This is really asking how much can we guarantee that we can send or that we
 * absolutely WILL BUFFER if we can't send it.  This driver MUST honor the
 * return value, hence the reason for hvcs_struct buffering.
 */
static int hvcs_write_room(struct tty_struct *tty)
{
	struct hvcs_struct *hvcsd = tty->driver_data;

	if (!hvcsd || hvcsd->open_count <= 0)
		return 0;

	return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
}

static int hvcs_chars_in_buffer(struct tty_struct *tty)
{
	struct hvcs_struct *hvcsd = tty->driver_data;

	return hvcsd->chars_in_buffer;
}

static const struct tty_operations hvcs_ops = {
	.open = hvcs_open,
	.close = hvcs_close,
	.hangup = hvcs_hangup,
	.write = hvcs_write,
	.write_room = hvcs_write_room,
	.chars_in_buffer = hvcs_chars_in_buffer,
	.unthrottle = hvcs_unthrottle,
	.throttle = hvcs_throttle,
};

static int hvcs_alloc_index_list(int n)
{
	int i;

	hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
	if (!hvcs_index_list)
		return -ENOMEM;
	hvcs_index_count = n;
	for (i = 0; i < hvcs_index_count; i++)
		hvcs_index_list[i] = -1;
	return 0;
}

static void hvcs_free_index_list(void)
{
	/* Paranoia check to be thorough. */
	kfree(hvcs_index_list);
	hvcs_index_list = NULL;
	hvcs_index_count = 0;
}

static int __devinit hvcs_initialize(void)
{
	int rc, num_ttys_to_alloc;

	mutex_lock(&hvcs_init_mutex);
	if (hvcs_task) {
		mutex_unlock(&hvcs_init_mutex);
		return 0;
	}

	/* Has the user specified an overload with an insmod param? */
	if (hvcs_parm_num_devs <= 0 ||
		(hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
		num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
	} else
		num_ttys_to_alloc = hvcs_parm_num_devs;

	hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
	if (!hvcs_tty_driver)
		return -ENOMEM;

	if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
		rc = -ENOMEM;
		goto index_fail;
	}

	hvcs_tty_driver->driver_name = hvcs_driver_name;
	hvcs_tty_driver->name = hvcs_device_node;

	/*
	 * We'll let the system assign us a major number, indicated by leaving
	 * it blank.
	 */

	hvcs_tty_driver->minor_start = HVCS_MINOR_START;
	hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;

	/*
	 * We role our own so that we DONT ECHO.  We can't echo because the
	 * device we are connecting to already echoes by default and this would
	 * throw us into a horrible recursive echo-echo-echo loop.
	 */
	hvcs_tty_driver->init_termios = hvcs_tty_termios;
	hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;

	tty_set_operations(hvcs_tty_driver, &hvcs_ops);

	/*
	 * The following call will result in sysfs entries that denote the
	 * dynamically assigned major and minor numbers for our devices.
	 */
	if (tty_register_driver(hvcs_tty_driver)) {
		printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
		rc = -EIO;
		goto register_fail;
	}

	hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
	if (!hvcs_pi_buff) {
		rc = -ENOMEM;
		goto buff_alloc_fail;
	}

	hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
	if (IS_ERR(hvcs_task)) {
		printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
		rc = -EIO;
		goto kthread_fail;
	}
	mutex_unlock(&hvcs_init_mutex);
	return 0;

kthread_fail:
	free_page((unsigned long)hvcs_pi_buff);
buff_alloc_fail:
	tty_unregister_driver(hvcs_tty_driver);
register_fail:
	hvcs_free_index_list();
index_fail:
	put_tty_driver(hvcs_tty_driver);
	hvcs_tty_driver = NULL;
	mutex_unlock(&hvcs_init_mutex);
	return rc;
}

static int __init hvcs_module_init(void)
{
	int rc = vio_register_driver(&hvcs_vio_driver);
	if (rc) {
		printk(KERN_ERR "HVCS: can't register vio driver\n");
		return rc;
	}

	pr_info("HVCS: Driver registered.\n");

	/* This needs to be done AFTER the vio_register_driver() call or else
	 * the kobjects won't be initialized properly.
	 */
	rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
	if (rc)
		pr_warning(KERN_ERR "HVCS: Failed to create rescan file (err %d)\n", rc);

	return 0;
}

static void __exit hvcs_module_exit(void)
{
	/*
	 * This driver receives hvcs_remove callbacks for each device upon
	 * module removal.
	 */
	vio_unregister_driver(&hvcs_vio_driver);
	if (!hvcs_task)
		return;

	/*
	 * This synchronous operation  will wake the khvcsd kthread if it is
	 * asleep and will return when khvcsd has terminated.
	 */
	kthread_stop(hvcs_task);

	spin_lock(&hvcs_pi_lock);
	free_page((unsigned long)hvcs_pi_buff);
	hvcs_pi_buff = NULL;
	spin_unlock(&hvcs_pi_lock);

	driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);

	tty_unregister_driver(hvcs_tty_driver);

	hvcs_free_index_list();

	put_tty_driver(hvcs_tty_driver);

	printk(KERN_INFO "HVCS: driver module removed.\n");
}

module_init(hvcs_module_init);
module_exit(hvcs_module_exit);