Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
/**********************************************************************
 * iph5526.c: IP/SCSI driver for the Interphase 5526 PCI Fibre Channel
 *			  Card.
 * Copyright (C) 1999 Vineet M Abraham <vma@iol.unh.edu>
 *
 * 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, 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.
 *********************************************************************/
/**********************************************************************
Log:
Vineet M Abraham
02.12.99 Support multiple cards.
03.15.99 Added Fabric support.
04.04.99 Added N_Port support.
04.15.99 Added SCSI support.
06.18.99 Added ABTS Protocol.
06.24.99 Fixed data corruption when multiple XFER_RDYs are received.
07.07.99 Can be loaded as part of the Kernel. Changed semaphores. Added
         more checks before invalidating SEST entries.
07.08.99 Added Broadcast IP stuff and fixed an unicast timeout bug.
***********************************************************************/
/* TODO:
	R_T_TOV set to 15msec in Loop topology. Need to be 100 msec.
    SMP testing.
	Fix ADISC Tx before completing FLOGI. 
*/	

static const char *version =
    "iph5526.c:v1.0 07.08.99 Vineet Abraham (vma@iol.unh.edu)\n";

#include <linux/module.h>
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/delay.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <asm/system.h>
#include <asm/io.h>

#include <linux/netdevice.h>
#include <linux/fcdevice.h> /* had the declarations for init_fcdev among others + includes if_fcdevice.h */

#include <linux/blk.h>
#include "../../scsi/sd.h"
#include "../../scsi/scsi.h"
#include "../../scsi/hosts.h"
#include "../../fc4/fcp.h"

/* driver specific header files */
#include "tach.h"
#include "tach_structs.h"
#include "iph5526_ip.h"
#include "iph5526_scsi.h"
#include "iph5526_novram.c"

#define RUN_AT(x) (jiffies + (x))

#define DEBUG_5526_0 0
#define DEBUG_5526_1 0
#define DEBUG_5526_2 0

#if DEBUG_5526_0
#define DPRINTK(format, a...) {printk("%s: ", fi->name); \
							   printk(format, ##a); \
							   printk("\n");}
#define ENTER(x)	{printk("%s: ", fi->name); \
					 printk("iph5526.c : entering %s()\n", x);}
#define LEAVE(x)	{printk("%s: ", fi->name); \
					 printk("iph5526.c : leaving %s()\n",x);}

#else
#define DPRINTK(format, a...) {}
#define ENTER(x)	{}
#define LEAVE(x)	{}
#endif

#if DEBUG_5526_1
#define DPRINTK1(format, a...) {printk("%s: ", fi->name); \
							   printk(format, ##a); \
							   printk("\n");}
#else
#define DPRINTK1(format, a...) {}
#endif

#if DEBUG_5526_2
#define DPRINTK2(format, a...) {printk("%s: ", fi->name); \
							   printk(format, ##a); \
							   printk("\n");}
#else
#define DPRINTK2(format, a...) {}
#endif

#define T_MSG(format, a...) {printk("%s: ", fi->name); \
							 printk(format, ##a);\
							 printk("\n");}

#define ALIGNED_SFS_ADDR(addr) ((((unsigned long)(addr) + (SFS_BUFFER_SIZE - 1)) & ~(SFS_BUFFER_SIZE - 1)) - (unsigned long)(addr))
#define ALIGNED_ADDR(addr, len) ((((unsigned long)(addr) + (len - 1)) & ~(len - 1)) - (unsigned long)(addr))


#define MAX_FC_CARDS 2
static struct fc_info *fc[MAX_FC_CARDS+1];
static unsigned int pci_irq_line = 0;
static struct {
	unsigned short vendor_id;
	unsigned short device_id;
	char *name;
}
clone_list[] __initdata  = {
	{PCI_VENDOR_ID_INTERPHASE, PCI_DEVICE_ID_INTERPHASE_5526, "Interphase Fibre Channel HBA"},
	{PCI_VENDOR_ID_INTERPHASE, PCI_DEVICE_ID_INTERPHASE_55x6, "Interphase Fibre Channel HBA"},
	{0,}
};

static void tachyon_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static void tachyon_interrupt_handler(int irq, void* dev_id, struct pt_regs* regs);

static int initialize_register_pointers(struct fc_info *fi);
void clean_up_memory(struct fc_info *fi);

static int tachyon_init(struct fc_info *fi);
static int build_queues(struct fc_info *fi);
static void build_tachyon_header(struct fc_info *fi, u_int my_id, u_int r_ctl, u_int d_id, u_int type, u_char seq_id, u_char df_ctl, u_short ox_id, u_short rx_id, char *data);
static int get_free_header(struct fc_info *fi);
static void build_EDB(struct fc_info *fi, char *data, u_short flags, u_short len);
static int get_free_EDB(struct fc_info *fi);
static void build_ODB(struct fc_info *fi, u_char seq_id, u_int d_id, u_int len, u_int cntl, u_short mtu, u_short ox_id, u_short rx_id, int NW_header, int int_required, u_int frame_class);
static void write_to_tachyon_registers(struct fc_info *fi);
static void reset_latch(struct fc_info *fi);
static void reset_tachyon(struct fc_info *fi, u_int value);
static void take_tachyon_offline(struct fc_info *fi);
static void read_novram(struct fc_info *fi);
static void reset_ichip(struct fc_info *fi);
static void update_OCQ_indx(struct fc_info *fi);
static void update_IMQ_indx(struct fc_info *fi, int count);
static void update_SFSBQ_indx(struct fc_info *fi);
static void update_MFSBQ_indx(struct fc_info *fi, int count);
static void update_tachyon_header_indx(struct fc_info *fi);
static void update_EDB_indx(struct fc_info *fi);
static void handle_FM_interrupt(struct fc_info *fi);
static void handle_MFS_interrupt(struct fc_info *fi);
static void handle_OOO_interrupt(struct fc_info *fi);
static void handle_SFS_interrupt(struct fc_info *fi);
static void handle_OCI_interrupt(struct fc_info *fi);
static void handle_SFS_BUF_WARN_interrupt(struct fc_info *fi);
static void handle_MFS_BUF_WARN_interrupt(struct fc_info *fi);
static void handle_IMQ_BUF_WARN_interrupt(struct fc_info *fi);
static void handle_Unknown_Frame_interrupt(struct fc_info *fi);
static void handle_Busied_Frame_interrupt(struct fc_info *fi);
static void handle_Bad_SCSI_Frame_interrupt(struct fc_info *fi);
static void handle_Inbound_SCSI_Status_interrupt(struct fc_info *fi);
static void handle_Inbound_SCSI_Command_interrupt(struct fc_info *fi);
static void completion_message_handler(struct fc_info *fi, u_int imq_int_type);
static void fill_login_frame(struct fc_info *fi, u_int logi);

static int tx_exchange(struct fc_info *fi, char *data, u_int len, u_int r_ctl, u_int type, u_int d_id, u_int mtu, int int_required, u_short ox_id, u_int frame_class);
static int tx_sequence(struct fc_info *fi, char *data, u_int len, u_int mtu, u_int d_id, u_short ox_id, u_short rx_id, u_char seq_id, int NW_flag, int int_required, u_int frame_class);
static int validate_login(struct fc_info *fi, u_int *base_ptr);
static void add_to_address_cache(struct fc_info *fi, u_int *base_ptr);
static void remove_from_address_cache(struct fc_info *fi, u_int *data, u_int cmnd_code);
static int node_logged_in_prev(struct fc_info *fi, u_int *buff_addr);
static int sid_logged_in(struct fc_info *fi, u_int s_id);
static struct fc_node_info *look_up_cache(struct fc_info *fi, char *data);
static int display_cache(struct fc_info *fi);

static void tx_logi(struct fc_info *fi, u_int logi, u_int d_id);
static void tx_logi_acc(struct fc_info *fi, u_int logi, u_int d_id, u_short received_ox_id);
static void tx_prli(struct fc_info *fi, u_int command_code, u_int d_id, u_short received_ox_id);
static void tx_logo(struct fc_info *fi, u_int d_id, u_short received_ox_id);
static void tx_adisc(struct fc_info *fi, u_int cmnd_code, u_int d_id, u_short received_ox_id);
static void tx_ls_rjt(struct fc_info *fi, u_int d_id, u_short received_ox_id, u_short reason_code, u_short expln_code);
static u_int plogi_ok(struct fc_info *fi, u_int *buff_addr, int size);
static void tx_acc(struct fc_info *fi, u_int d_id, u_short received_ox_id);
static void tx_name_server_req(struct fc_info *fi, u_int req);
static void rscn_handler(struct fc_info *fi, u_int node_id);
static void tx_scr(struct fc_info *fi);
static void scr_timer(unsigned long data);
static void explore_fabric(struct fc_info *fi, u_int *buff_addr);
static void perform_adisc(struct fc_info *fi);
static void local_port_discovery(struct fc_info *fi);
static void add_to_ox_id_list(struct fc_info *fi, u_int transaction_id, u_int cmnd_code);
static u_int remove_from_ox_id_list(struct fc_info *fi, u_short received_ox_id);
static void add_display_cache_timer(struct fc_info *fi);

/* Timers... */
static void nos_ols_timer(unsigned long data);
static void loop_timer(unsigned long data);
static void fabric_explore_timer(unsigned long data);
static void port_discovery_timer(unsigned long data);
static void display_cache_timer(unsigned long data);

/* SCSI Stuff */
static int add_to_sest(struct fc_info *fi, Scsi_Cmnd *Cmnd, struct fc_node_info *ni);
static struct fc_node_info *resolve_target(struct fc_info *fi, u_char target);
static void update_FCP_CMND_indx(struct fc_info *fi);
static int get_free_SDB(struct fc_info *fi);
static void update_SDB_indx(struct fc_info *fi);
static void mark_scsi_sid(struct fc_info *fi, u_int *buff_addr, u_char action);
static void invalidate_SEST_entry(struct fc_info *fi, u_short received_ox_id);
static int abort_exchange(struct fc_info *fi, u_short ox_id);
static void flush_tachyon_cache(struct fc_info *fi, u_short ox_id);
static int get_scsi_oxid(struct fc_info *fi);
static void update_scsi_oxid(struct fc_info *fi);

Scsi_Host_Template driver_template = IPH5526_SCSI_FC;


#ifdef CONFIG_PCI
static int iph5526_probe_pci(struct net_device *dev);
#endif


int __init iph5526_probe(struct net_device *dev)
{
#ifdef CONFIG_PCI
	if (pci_present() && (iph5526_probe_pci(dev) == 0))
		return 0;
#endif
    return -ENODEV;
}

#ifdef CONFIG_PCI
static int __init iph5526_probe_pci(struct net_device *dev)
{
#ifndef MODULE
struct fc_info *fi;
static int count = 0;
#endif
#ifdef MODULE
struct fc_info *fi = (struct fc_info *)dev->priv;
#endif

#ifndef MODULE
	if(fc[count] != NULL) {
		if (dev == NULL) {
			dev = init_fcdev(NULL, 0);
			if (dev == NULL)
				return -ENOMEM;
		}
		fi = fc[count];
#endif
		fi->dev = dev;
		dev->base_addr = fi->base_addr;
		dev->irq = fi->irq;
		if (dev->priv == NULL) 
			dev->priv = fi; 
		fcdev_init(dev);
		/* Assign ur MAC address.
		 */
		dev->dev_addr[0] = (fi->g.my_port_name_high & 0x0000FF00) >> 8;
		dev->dev_addr[1] = fi->g.my_port_name_high;
		dev->dev_addr[2] = (fi->g.my_port_name_low & 0xFF000000) >> 24;
		dev->dev_addr[3] = (fi->g.my_port_name_low & 0x00FF0000) >> 16;
		dev->dev_addr[4] = (fi->g.my_port_name_low & 0x0000FF00) >> 8;
		dev->dev_addr[5] = fi->g.my_port_name_low;
#ifndef MODULE
		count++;
	}
	else
		return -ENODEV;
#endif
	display_cache(fi);
	return 0;
}
#endif  /* CONFIG_PCI */

static int __init fcdev_init(struct net_device *dev)
{
	dev->open = iph5526_open;
	dev->stop = iph5526_close;
	dev->hard_start_xmit = iph5526_send_packet;
	dev->get_stats = iph5526_get_stats;
	dev->set_multicast_list = NULL;
	dev->change_mtu = iph5526_change_mtu; 
#ifndef MODULE
	fc_setup(dev);
#endif
	return 0;
}

/* initialize tachyon and take it OnLine */
static int tachyon_init(struct fc_info *fi)
{
	ENTER("tachyon_init");
	if (build_queues(fi) == 0) {
		T_MSG("build_queues() failed");
		return 0;
	}

	/* Retrieve your port/node name.
	 */
	read_novram(fi);

	reset_ichip(fi);

	reset_tachyon(fi, SOFTWARE_RESET);

	LEAVE("tachyon_init");
	return 1;
}

/* Build the 4 Qs - IMQ, OCQ, MFSBQ, SFSBQ */
/* Lots of dma_pages needed as Tachyon DMAs almost everything into 
 * host memory.
 */
static int build_queues(struct fc_info *fi)
{
int i,j;
u_char *addr;
	ENTER("build_queues");
	/* Initializing Queue Variables.
	 */
	fi->q.ptr_host_ocq_cons_indx = NULL;
	fi->q.ptr_host_hpcq_cons_indx = NULL;
	fi->q.ptr_host_imq_prod_indx = NULL;

	fi->q.ptr_ocq_base = NULL;
	fi->q.ocq_len = 0;
	fi->q.ocq_end = 0;
	fi->q.ocq_prod_indx = 0;

	fi->q.ptr_imq_base = NULL;
	fi->q.imq_len = 0;
	fi->q.imq_end = 0;
	fi->q.imq_cons_indx = 0;
	fi->q.imq_prod_indx = 0;

	fi->q.ptr_mfsbq_base = NULL;
	fi->q.mfsbq_len = 0;
	fi->q.mfsbq_end = 0;
	fi->q.mfsbq_prod_indx = 0;
	fi->q.mfsbq_cons_indx = 0;
	fi->q.mfsbuff_len = 0;
	fi->q.mfsbuff_end = 0;
	fi->g.mfs_buffer_count = 0;

	fi->q.ptr_sfsbq_base = NULL;
	fi->q.sfsbq_len = 0;
	fi->q.sfsbq_end = 0;
	fi->q.sfsbq_prod_indx = 0;
	fi->q.sfsbq_cons_indx = 0;
	fi->q.sfsbuff_len = 0;
	fi->q.sfsbuff_end = 0;

	fi->q.sdb_indx = 0;
	fi->q.fcp_cmnd_indx = 0;

	fi->q.ptr_edb_base = NULL;
	fi->q.edb_buffer_indx = 0;
	fi->q.ptr_tachyon_header_base = NULL;
	fi->q.tachyon_header_indx = 0;
	fi->node_info_list = NULL;
	fi->ox_id_list = NULL;
	fi->g.loop_up = FALSE;
	fi->g.ptp_up = FALSE;
	fi->g.link_up = FALSE;
	fi->g.fabric_present = FALSE;
	fi->g.n_port_try = FALSE;
	fi->g.dont_init = FALSE;
	fi->g.nport_timer_set = FALSE;
	fi->g.lport_timer_set = FALSE;
	fi->g.no_of_targets = 0;
	fi->g.sem = 0;
	fi->g.perform_adisc = FALSE;
	fi->g.e_i = 0;

	/* build OCQ */
	if ( (fi->q.ptr_ocq_base = (u_int *)__get_free_pages(GFP_KERNEL, 0)) == 0) {
		T_MSG("failed to get OCQ page");
		return 0;
	}
	/* set up the OCQ structures */
	for (i = 0; i < OCQ_LENGTH; i++)
		fi->q.ptr_odb[i] = fi->q.ptr_ocq_base + NO_OF_ENTRIES*i;

	/* build IMQ */
	if ( (fi->q.ptr_imq_base = (u_int *)__get_free_pages(GFP_KERNEL, 0)) == 0) {
		T_MSG("failed to get IMQ page");
		return 0;
	}
	for (i = 0; i < IMQ_LENGTH; i++)
		fi->q.ptr_imqe[i] = fi->q.ptr_imq_base + NO_OF_ENTRIES*i;

	/* build MFSBQ */
	if ( (fi->q.ptr_mfsbq_base = (u_int *)__get_free_pages(GFP_KERNEL, 0)) == 0) {
		T_MSG("failed to get MFSBQ page");
		return 0;
	}
	memset((char *)fi->q.ptr_mfsbq_base, 0, MFSBQ_LENGTH * 32);
	/* Allocate one huge chunk of memory... helps while reassembling
	 * frames.
	 */
	if ( (addr = (u_char *)__get_free_pages(GFP_KERNEL, 5) ) == 0) {
		T_MSG("failed to get MFSBQ page");
		return 0;
	}
	/* fill in addresses of empty buffers */
	for (i = 0; i < MFSBQ_LENGTH; i++) {
		for (j = 0; j < NO_OF_ENTRIES; j++) {
				*(fi->q.ptr_mfsbq_base + i*NO_OF_ENTRIES + j) = htonl(virt_to_bus(addr));
				addr += MFS_BUFFER_SIZE;
		}
	}

	/* The number of entries in each MFS buffer is 8. There are 8
	 * MFS buffers. That leaves us with 4096-256 bytes. We use them
	 * as temporary space for ELS frames. This is done to make sure that
	 * the addresses are aligned.
	 */
	fi->g.els_buffer[0] = fi->q.ptr_mfsbq_base + MFSBQ_LENGTH*NO_OF_ENTRIES;
	for (i = 1; i < MAX_PENDING_FRAMES; i++)
		fi->g.els_buffer[i] = fi->g.els_buffer[i-1] + 64;
	
	/* build SFSBQ */
	if ( (fi->q.ptr_sfsbq_base = (u_int *)__get_free_pages(GFP_KERNEL, 0)) == 0) {
		T_MSG("failed to get SFSBQ page");
		return 0;
	}
	memset((char *)fi->q.ptr_sfsbq_base, 0, SFSBQ_LENGTH * 32);
	/* fill in addresses of empty buffers */
	for (i = 0; i < SFSBQ_LENGTH; i++)
		for (j = 0; j < NO_OF_ENTRIES; j++){
			addr = kmalloc(SFS_BUFFER_SIZE*2, GFP_KERNEL);
			if (addr == NULL){ 
				T_MSG("ptr_sfs_buffer : memory not allocated");
				return 0;
			}
			else {
			int offset = ALIGNED_SFS_ADDR(addr);
				memset((char *)addr, 0, SFS_BUFFER_SIZE);
				fi->q.ptr_sfs_buffers[i*NO_OF_ENTRIES +j] = (u_int *)addr;
				addr += offset;
				*(fi->q.ptr_sfsbq_base + i*NO_OF_ENTRIES + j) = htonl(virt_to_bus(addr));
			}
		}

	/* The number of entries in each SFS buffer is 8. There are 8
	 * MFS buffers. That leaves us with 4096-256 bytes. We use them
	 * as temporary space for ARP frames. This is done inorder to 
	 * support HW_Types of 0x1 and 0x6. 
	 */
	fi->g.arp_buffer = (char *)fi->q.ptr_sfsbq_base + SFSBQ_LENGTH*NO_OF_ENTRIES*4;
	
	/* build EDB */
	if ((fi->q.ptr_edb_base = (u_int *)__get_free_pages(GFP_KERNEL, 5) ) == 0) {
		T_MSG("failed to get EDB page");
		return 0;
	}
	for (i = 0; i < EDB_LEN; i++)
		fi->q.ptr_edb[i] = fi->q.ptr_edb_base + 2*i;

	/* build SEST */

	/* OX_IDs range from 0x0 - 0x4FFF.
	 */
	if ((fi->q.ptr_sest_base = (u_int *)__get_free_pages(GFP_KERNEL, 5)) == 0) {
		T_MSG("failed to get SEST page");
		return 0;
	}
	for (i = 0; i < SEST_LENGTH; i++)
		fi->q.ptr_sest[i] = fi->q.ptr_sest_base + NO_OF_ENTRIES*i;
	
	if ((fi->q.ptr_sdb_base = (u_int *)__get_free_pages(GFP_KERNEL, 5)) == 0) {
		T_MSG("failed to get SDB page");
		return 0;
	}
	for (i = 0 ; i < NO_OF_SDB_ENTRIES; i++)
		fi->q.ptr_sdb_slot[i] = fi->q.ptr_sdb_base + (SDB_SIZE/4)*i;

	if ((fi->q.ptr_fcp_cmnd_base = (u_int *)__get_free_pages(GFP_KERNEL, 0)) == 0) {
		T_MSG("failed to get FCP_CMND page");
		return 0;
	}
	for (i = 0; i < NO_OF_FCP_CMNDS; i++)
		fi->q.ptr_fcp_cmnd[i] = fi->q.ptr_fcp_cmnd_base + NO_OF_ENTRIES*i;

	/* Allocate space for Tachyon Header as well... 
	 */
	if ((fi->q.ptr_tachyon_header_base = (u_int *)__get_free_pages(GFP_KERNEL, 0) ) == 0) {
		T_MSG("failed to get tachyon_header page");
		return 0;
	}
	for (i = 0; i < NO_OF_TACH_HEADERS; i++) 
		fi->q.ptr_tachyon_header[i] = fi->q.ptr_tachyon_header_base + 16*i;
	
	/* Allocate memory for indices.
	 * Indices should be aligned on 32 byte boundries. 
	 */
	fi->q.host_ocq_cons_indx = kmalloc(2*32, GFP_KERNEL);
	if (fi->q.host_ocq_cons_indx == NULL){ 
		T_MSG("fi->q.host_ocq_cons_indx : memory not allocated");
		return 0;
	}
	fi->q.ptr_host_ocq_cons_indx = fi->q.host_ocq_cons_indx; 
	if ((u_long)(fi->q.host_ocq_cons_indx) % 32)
		fi->q.host_ocq_cons_indx++;
	
	fi->q.host_hpcq_cons_indx = kmalloc(2*32, GFP_KERNEL);
	if (fi->q.host_hpcq_cons_indx == NULL){ 
		T_MSG("fi->q.host_hpcq_cons_indx : memory not allocated");
		return 0;
	}
	fi->q.ptr_host_hpcq_cons_indx= fi->q.host_hpcq_cons_indx;
	if ((u_long)(fi->q.host_hpcq_cons_indx) % 32)
		fi->q.host_hpcq_cons_indx++;

	fi->q.host_imq_prod_indx = kmalloc(2*32, GFP_KERNEL);
	if (fi->q.host_imq_prod_indx == NULL){ 
		T_MSG("fi->q.host_imq_prod_indx : memory not allocated");
		return 0;
	}
	fi->q.ptr_host_imq_prod_indx = fi->q.host_imq_prod_indx;
	if ((u_long)(fi->q.host_imq_prod_indx) % 32)
		fi->q.host_imq_prod_indx++;

	LEAVE("build_queues");
	return 1;
}


static void write_to_tachyon_registers(struct fc_info *fi)
{
u_int bus_addr, bus_indx_addr, i;

	ENTER("write_to_tachyon_registers");

	/* Clear Queues each time Tachyon is reset */
	memset((char *)fi->q.ptr_ocq_base, 0, OCQ_LENGTH * 32);
	memset((char *)fi->q.ptr_imq_base, 0, IMQ_LENGTH * 32);
	memset((char *)fi->q.ptr_edb_base, 0, EDB_LEN * 8);
	memset((char *)fi->q.ptr_sest_base, 0, SEST_LENGTH * 32);
	memset((char *)fi->q.ptr_sdb_base, 0, NO_OF_SDB_ENTRIES * SDB_SIZE);
	memset((char *)fi->q.ptr_tachyon_header_base, 0xFF, NO_OF_TACH_HEADERS * TACH_HEADER_SIZE);
	for (i = 0; i < SEST_LENGTH; i++)
		fi->q.free_scsi_oxid[i] = OXID_AVAILABLE;
	for (i = 0; i < NO_OF_SDB_ENTRIES; i++)
		fi->q.sdb_slot_status[i] = SDB_FREE;

	take_tachyon_offline(fi);
	writel(readl(fi->t_r.ptr_tach_config_reg) | SCSI_ENABLE | WRITE_STREAM_SIZE | READ_STREAM_SIZE | PARITY_EVEN | OOO_REASSEMBLY_DISABLE, fi->t_r.ptr_tach_config_reg);

	/* Write OCQ registers */
	fi->q.ocq_prod_indx = 0;
	*(fi->q.host_ocq_cons_indx) = 0;
	
	/* The Tachyon needs to be passed the "real" address */
	bus_addr = virt_to_bus(fi->q.ptr_ocq_base);
	writel(bus_addr, fi->t_r.ptr_ocq_base_reg);
	writel(OCQ_LENGTH - 1, fi->t_r. ptr_ocq_len_reg);
	bus_indx_addr = virt_to_bus(fi->q.host_ocq_cons_indx);
	writel(bus_indx_addr, fi->t_r.ptr_ocq_cons_indx_reg);

	/* Write IMQ registers */
	fi->q.imq_cons_indx = 0;
	*(fi->q.host_imq_prod_indx) = 0;
	bus_addr = virt_to_bus(fi->q.ptr_imq_base);
	writel(bus_addr, fi->t_r.ptr_imq_base_reg);
	writel(IMQ_LENGTH - 1, fi->t_r.ptr_imq_len_reg);
	bus_indx_addr = virt_to_bus(fi->q.host_imq_prod_indx);
	writel(bus_indx_addr, fi->t_r.ptr_imq_prod_indx_reg);
	
	/* Write MFSBQ registers */
	fi->q.mfsbq_prod_indx = MFSBQ_LENGTH - 1;
	fi->q.mfsbuff_end = MFS_BUFFER_SIZE - 1;
	fi->q.mfsbq_cons_indx = 0;
	bus_addr = virt_to_bus(fi->q.ptr_mfsbq_base);
	writel(bus_addr, fi->t_r.ptr_mfsbq_base_reg);
	writel(MFSBQ_LENGTH - 1, fi->t_r.ptr_mfsbq_len_reg);
	writel(fi->q.mfsbuff_end, fi->t_r.ptr_mfsbuff_len_reg);
	/* Do this last as tachyon will prefetch the 
	 * first entry as soon as we write to it.
	 */
	writel(fi->q.mfsbq_prod_indx, fi->t_r.ptr_mfsbq_prod_reg);

	/* Write SFSBQ registers */
	fi->q.sfsbq_prod_indx = SFSBQ_LENGTH - 1;
	fi->q.sfsbuff_end = SFS_BUFFER_SIZE - 1;
	fi->q.sfsbq_cons_indx = 0;
	bus_addr = virt_to_bus(fi->q.ptr_sfsbq_base);
	writel(bus_addr, fi->t_r.ptr_sfsbq_base_reg);
	writel(SFSBQ_LENGTH - 1, fi->t_r.ptr_sfsbq_len_reg);
	writel(fi->q.sfsbuff_end, fi->t_r.ptr_sfsbuff_len_reg);
	/* Do this last as tachyon will prefetch the first 
	 * entry as soon as we write to it. 
	 */
	writel(fi->q.sfsbq_prod_indx, fi->t_r.ptr_sfsbq_prod_reg);

	/* Write SEST registers */
	bus_addr = virt_to_bus(fi->q.ptr_sest_base);
	writel(bus_addr, fi->t_r.ptr_sest_base_reg);
	writel(SEST_LENGTH - 1, fi->t_r.ptr_sest_len_reg);
	/* the last 2 bits _should_ be 1 */
	writel(SEST_BUFFER_SIZE - 1, fi->t_r.ptr_scsibuff_len_reg);

	/* write AL_TIME & E_D_TOV into the registers */
	writel(TOV_VALUES, fi->t_r.ptr_fm_tov_reg);
	/* Tell Tachyon to pick a Soft Assigned AL_PA */
	writel(LOOP_INIT_SOFT_ADDRESS, fi->t_r.ptr_fm_config_reg);

	/* Read the WWN from EEPROM . But, for now we assign it here. */
	writel(WORLD_WIDE_NAME_LOW, fi->t_r.ptr_fm_wwn_low_reg);
	writel(WORLD_WIDE_NAME_HIGH, fi->t_r.ptr_fm_wwn_hi_reg);

	DPRINTK1("TACHYON initializing as L_Port...\n");
	writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
			
	LEAVE("write_to_tachyon_registers");
}


static void tachyon_interrupt(int irq, void* dev_id, struct pt_regs* regs)
{
struct Scsi_Host *host = dev_id;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata *)host->hostdata;
struct fc_info *fi = hostdata->fi; 
u_long flags;
	spin_lock_irqsave(&fi->fc_lock, flags);
	tachyon_interrupt_handler(irq, dev_id, regs);
	spin_unlock_irqrestore(&fi->fc_lock, flags);
}

static void tachyon_interrupt_handler(int irq, void* dev_id, struct pt_regs* regs)
{
struct Scsi_Host *host = dev_id;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata *)host->hostdata;
struct fc_info *fi = hostdata->fi; 
u_int *ptr_imq_entry;
u_int imq_int_type, current_IMQ_index = 0, prev_IMQ_index;
int index, no_of_entries = 0;

	DPRINTK("\n");
	ENTER("tachyon_interrupt");
	if (fi->q.host_imq_prod_indx != NULL) {
		current_IMQ_index =  ntohl(*(fi->q.host_imq_prod_indx));
	}
	else {
		/* _Should not_ happen */
		T_MSG("IMQ_indx NULL. DISABLING INTERRUPTS!!!\n");
		writel(0x0, fi->i_r.ptr_ichip_hw_control_reg);
	}

	if (current_IMQ_index > fi->q.imq_cons_indx)
		no_of_entries = current_IMQ_index - fi->q.imq_cons_indx;
	else
	if (current_IMQ_index < fi->q.imq_cons_indx)
		no_of_entries = IMQ_LENGTH - (fi->q.imq_cons_indx - current_IMQ_index);

	if (no_of_entries == 0) {
	u_int ichip_status;
		ichip_status = readl(fi->i_r.ptr_ichip_hw_status_reg);
		if (ichip_status & 0x20) {
			/* Should _never_ happen. Might require a hard reset */
			T_MSG("Too bad... PCI Bus Error. Resetting (i)chip"); 
			reset_ichip(fi);
			T_MSG("DISABLING INTERRUPTS!!!\n");
			writel(0x0, fi->i_r.ptr_ichip_hw_control_reg);
		}
	}

	prev_IMQ_index = current_IMQ_index;
	for (index = 0; index < no_of_entries; index++) {
		ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
		imq_int_type = ntohl(*ptr_imq_entry);

		completion_message_handler(fi, imq_int_type);
		if ((fi->g.link_up == FALSE) && ((imq_int_type == MFS_BUF_WARN) || (imq_int_type == SFS_BUF_WARN) || (imq_int_type == IMQ_BUF_WARN))) 
			break;
		update_IMQ_indx(fi, 1);
	
		/* Check for more entries */
		current_IMQ_index =  ntohl(*(fi->q.host_imq_prod_indx));
		if (current_IMQ_index != prev_IMQ_index) {
			no_of_entries++;
			prev_IMQ_index = current_IMQ_index;
		}
	} /*end of for loop*/		
	return;
	LEAVE("tachyon_interrupt");
}


static void handle_SFS_BUF_WARN_interrupt(struct fc_info *fi)
{
int i;
	ENTER("handle_SFS_BUF_WARN_interrupt");
	if (fi->g.link_up == FALSE) {
		reset_tachyon(fi, SOFTWARE_RESET);
		return;
	}
	/* Free up all but one entry in the Q. 
	 */
	for (i = 0; i < ((SFSBQ_LENGTH - 1) * NO_OF_ENTRIES); i++) {
		handle_SFS_interrupt(fi);
		update_IMQ_indx(fi, 1);
	}
	LEAVE("handle_SFS_BUF_WARN_interrupt");
}

/* Untested_Code_Begin */ 
static void handle_MFS_BUF_WARN_interrupt(struct fc_info *fi)
{
int i;
	ENTER("handle_MFS_BUF_WARN_interrupt");
	if (fi->g.link_up == FALSE) {
		reset_tachyon(fi, SOFTWARE_RESET);
		return;
	}
	/* FIXME: freeing up 8 entries. 
	 */
	for (i = 0; i < NO_OF_ENTRIES; i++) {
		handle_MFS_interrupt(fi);
		update_IMQ_indx(fi, 1);
	}
	LEAVE("handle_MFS_BUF_WARN_interrupt");
}
/*Untested_Code_End */

static void handle_IMQ_BUF_WARN_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
u_int imq_int_type, current_IMQ_index = 0, temp_imq_cons_indx;
int index, no_of_entries = 0;

	ENTER("handle_IMQ_BUF_WARN_interrupt");
	if (fi->g.link_up == FALSE) {
		reset_tachyon(fi, SOFTWARE_RESET);
		return;
	}
	current_IMQ_index =  ntohl(*(fi->q.host_imq_prod_indx));

	if (current_IMQ_index > fi->q.imq_cons_indx)
 		no_of_entries = current_IMQ_index - fi->q.imq_cons_indx;
	else
		if (current_IMQ_index < fi->q.imq_cons_indx)
			no_of_entries = IMQ_LENGTH - (fi->q.imq_cons_indx - current_IMQ_index);
	/* We dont want to look at the same IMQ entry again. 
	 */
	temp_imq_cons_indx = fi->q.imq_cons_indx + 1;
	if (no_of_entries != 0)
		no_of_entries -= 1;
	for (index = 0; index < no_of_entries; index++) {
		ptr_imq_entry = fi->q.ptr_imqe[temp_imq_cons_indx];
		imq_int_type = ntohl(*ptr_imq_entry);
		if (imq_int_type != IMQ_BUF_WARN)
			completion_message_handler(fi, imq_int_type);
		temp_imq_cons_indx++;
		if (temp_imq_cons_indx == IMQ_LENGTH)
			temp_imq_cons_indx = 0;
	} /*end of for loop*/	
	if (no_of_entries != 0)
		update_IMQ_indx(fi, no_of_entries);
	LEAVE("handle_IMQ_BUF_WARN_interrupt");
}

static void completion_message_handler(struct fc_info *fi, u_int imq_int_type)
{
	switch(imq_int_type) {
		case OUTBOUND_COMPLETION:
			DPRINTK("OUTBOUND_COMPLETION message received");
			break;
		case OUTBOUND_COMPLETION_I:
			DPRINTK("OUTBOUND_COMPLETION_I message received");
			handle_OCI_interrupt(fi);
			break;
		case OUT_HI_PRI_COMPLETION:
			DPRINTK("OUT_HI_PRI_COMPLETION message received");
			break;
		case OUT_HI_PRI_COMPLETION_I:
			DPRINTK("OUT_HI_PRI_COMPLETION_I message received");
			break;
		case INBOUND_MFS_COMPLETION:
			DPRINTK("INBOUND_MFS_COMPLETION message received");
			handle_MFS_interrupt(fi);
			break;
		case INBOUND_OOO_COMPLETION:
			DPRINTK("INBOUND_OOO_COMPLETION message received");
			handle_OOO_interrupt(fi);
			break;
		case INBOUND_SFS_COMPLETION:
			DPRINTK("INBOUND_SFS_COMPLETION message received");
			handle_SFS_interrupt(fi);
			break;
		case INBOUND_UNKNOWN_FRAME_I:
			DPRINTK("INBOUND_UNKNOWN_FRAME message received");
			handle_Unknown_Frame_interrupt(fi);
			break;
		case INBOUND_BUSIED_FRAME:
			DPRINTK("INBOUND_BUSIED_FRAME message received");
			handle_Busied_Frame_interrupt(fi);
			break;
		case FRAME_MGR_INTERRUPT:
			DPRINTK("FRAME_MGR_INTERRUPT message received");
			handle_FM_interrupt(fi);
			break;
		case READ_STATUS:
			DPRINTK("READ_STATUS message received");
			break;
		case SFS_BUF_WARN:
			DPRINTK("SFS_BUF_WARN message received");
			handle_SFS_BUF_WARN_interrupt(fi);
			break;
		case MFS_BUF_WARN:
			DPRINTK("MFS_BUF_WARN message received");
			handle_MFS_BUF_WARN_interrupt(fi);
			break;
		case IMQ_BUF_WARN:
			DPRINTK("IMQ_BUF_WARN message received");
			handle_IMQ_BUF_WARN_interrupt(fi);
			break;
		case INBOUND_C1_TIMEOUT:
			DPRINTK("INBOUND_C1_TIMEOUT message received");
			break;
		case BAD_SCSI_FRAME:
			DPRINTK("BAD_SCSI_FRAME message received");
			handle_Bad_SCSI_Frame_interrupt(fi);
			break;
		case INB_SCSI_STATUS_COMPLETION:
			DPRINTK("INB_SCSI_STATUS_COMPL message received");
			handle_Inbound_SCSI_Status_interrupt(fi);
			break;
		case INBOUND_SCSI_COMMAND:
			DPRINTK("INBOUND_SCSI_COMMAND message received");
			handle_Inbound_SCSI_Command_interrupt(fi);
			break;
		case INBOUND_SCSI_DATA_COMPLETION:
			DPRINTK("INBOUND_SCSI_DATA message received");
			/* Only for targets */
			break;
		default:		
			T_MSG("DEFAULT message received, type = %x", imq_int_type);
			return;
	}
	reset_latch(fi);
}

static void handle_OCI_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
u_long transaction_id = 0;
unsigned short status, seq_count, transmitted_ox_id;
struct Scsi_Host *host = fi->host;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata *)host->hostdata;
Scsi_Cmnd *Cmnd;
u_int tag;

	ENTER("handle_OCI_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	transaction_id = ntohl(*(ptr_imq_entry + 1));
	status = ntohl(*(ptr_imq_entry + 2)) >> 16;
	seq_count = ntohl(*(ptr_imq_entry + 3));
	DPRINTK("transaction_id= %x", (u_int)transaction_id);
	tag = transaction_id & 0xFFFF0000;
	transmitted_ox_id = transaction_id;

	/* The INT could be either due to TIME_OUT | BAD_ALPA. 
	 * But we check only for TimeOuts. Bad AL_PA will 
	 * caught by FM_interrupt handler. 
	 */

	if ((status == OCM_TIMEOUT_OR_BAD_ALPA) && (!fi->g.port_discovery) && (!fi->g.perform_adisc)){
		DPRINTK("Frame TimeOut on OX_ID = %x", (u_int)transaction_id);

		/* Is it a SCSI frame that is timing out ? Not a very good check... 
		 */
		if ((transmitted_ox_id <= MAX_SCSI_OXID) && ((tag == FC_SCSI_BAD_TARGET) || (tag < 0x00FF0000))) {
			/* If it is a Bad AL_PA, we report it as BAD_TARGET.
			 * Else, we allow the command to time-out. A Link
			 * re-initialization could be taking place.
			 */
			if (tag == FC_SCSI_BAD_TARGET) {
				Cmnd = hostdata->cmnd_handler[transmitted_ox_id & MAX_SCSI_XID];
				hostdata->cmnd_handler[transmitted_ox_id & MAX_SCSI_XID] = NULL;
				if (Cmnd != NULL) {
					Cmnd->result = DID_BAD_TARGET << 16;
					(*Cmnd->scsi_done) (Cmnd);
				}
				else
					T_MSG("NULL Command out of handler!");
			} /* if Bad Target */
			else {
			u_char missing_target = tag >> 16;
			struct fc_node_info *q = fi->node_info_list;
				/* A Node that we thought was logged in has gone
				 * away. We are the optimistic kind and we keep
				 * hoping that our dear little Target will come back
				 * to us. For now we log him out.
				 */
				DPRINTK2("Missing Target = %d", missing_target);
				while (q != NULL) {
					if (q->target_id == missing_target) {
						T_MSG("Target %d Logged out", q->target_id);
						q->login = LOGIN_ATTEMPTED;
						if (fi->num_nodes > 0)
							fi->num_nodes--;
						tx_logi(fi, ELS_PLOGI, q->d_id);
						break;
					}
					else
						q = q->next;
				}
			}
		} /* End of SCSI frame timing out. */
		else {
			if (seq_count > 1) {
				/* An IP frame was transmitted to a Bad AL_PA. Free up
			 	 * the skb used.
			 	 */
				dev_kfree_skb((struct sk_buff *)(bus_to_virt(transaction_id)));
			}
		} /* End of IP frame timing out. */
	} /* End of frame timing out. */
	else {
		/* Frame was transmitted successfully. Check if it was an ELS
		 * frame or an IP frame or a Bad_Target_Notification frame (in
		 * case of a ptp_link). Ugly!
		 */
		if ((status == 0) && (seq_count == 0)) {
		u_int tag = transaction_id & 0xFFFF0000;
		/* Continue with port discovery after an ELS is successfully 
		 * transmitted. (status == 0). 
		 */
			DPRINTK("tag = %x", tag);
			switch(tag) {
				case ELS_FLOGI:
					/* Letz use the Name Server instead */
					fi->g.explore_fabric = TRUE;
					fi->g.port_discovery = FALSE;
					fi->g.alpa_list_index = MAX_NODES;
					add_to_ox_id_list(fi, transaction_id, tag);
					break;
				case ELS_PLOGI:
					if (fi->g.fabric_present && (fi->g.name_server == FALSE))
						add_to_ox_id_list(fi,transaction_id,ELS_NS_PLOGI);
					else
						add_to_ox_id_list(fi, transaction_id, tag);
					break;
				case FC_SCSI_BAD_TARGET:
					Cmnd = hostdata->cmnd_handler[transmitted_ox_id & MAX_SCSI_XID];
					hostdata->cmnd_handler[transmitted_ox_id & MAX_SCSI_XID] = NULL;
					if (Cmnd != NULL) {
						Cmnd->result = DID_BAD_TARGET << 16;
						(*Cmnd->scsi_done) (Cmnd);
					}
					else
						T_MSG("NULL Command out of handler!");
					break;
				default:
					add_to_ox_id_list(fi, transaction_id, tag);
			}
		
			if (fi->g.alpa_list_index >= MAX_NODES) {
				if (fi->g.port_discovery == TRUE) {
					fi->g.port_discovery = FALSE;
					add_display_cache_timer(fi);
				}
				fi->g.alpa_list_index = MAX_NODES;
			}
			if (fi->g.port_discovery == TRUE) 
				local_port_discovery(fi);
		}
		else {
			/* An IP frame has been successfully transmitted.
			 * Free the skb that was used for this IP frame.
			 */
			if ((status == 0) && (seq_count > 1)) {
				dev_kfree_skb((struct sk_buff *)(bus_to_virt(transaction_id)));
			}
		}
	}
	LEAVE("handle_OCI_interrupt");
}

/* Right now we discard OOO frames */
static void handle_OOO_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
int queue_indx, offset, payload_size;
int no_of_buffers = 1; /* header is in a separate buffer */
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	payload_size = ntohl(*(ptr_imq_entry + 2)) - TACHYON_HEADER_LEN;
	/* Calculate total number of buffers */
	no_of_buffers += payload_size / MFS_BUFFER_SIZE;
	if (payload_size % MFS_BUFFER_SIZE)
		no_of_buffers++;

	/* provide Tachyon will another set of buffers */
	fi->g.mfs_buffer_count += no_of_buffers;
	if (fi->g.mfs_buffer_count >= NO_OF_ENTRIES) {
	int count = fi->g.mfs_buffer_count / NO_OF_ENTRIES;
		fi->g.mfs_buffer_count -= NO_OF_ENTRIES * count;
		update_MFSBQ_indx(fi, count);
	}
}

static void handle_MFS_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry, *buff_addr;
u_int type_of_frame, s_id;
int queue_indx, offset, payload_size, starting_indx, starting_offset;
u_short received_ox_id;
int no_of_buffers = 1; /* header is in a separate buffer */
struct sk_buff *skb;
int wrap_around = FALSE, no_of_wrap_buffs = NO_OF_ENTRIES - 1;
	ENTER("handle_MFS_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	DPRINTK("queue_indx = %d, offset  = %d\n", queue_indx, offset);
	payload_size = ntohl(*(ptr_imq_entry + 2)) - TACHYON_HEADER_LEN;
	DPRINTK("payload_size = %d", payload_size);
	/* Calculate total number of buffers */
	no_of_buffers += payload_size / MFS_BUFFER_SIZE;
	if (payload_size % MFS_BUFFER_SIZE)
		no_of_buffers++;
	DPRINTK("no_of_buffers = %d", no_of_buffers);

	if ((no_of_buffers - 1) <= offset) {
		starting_offset = offset - (no_of_buffers - 1);
		starting_indx = queue_indx;
	}
	else {
	int temp = no_of_buffers - (offset + 1);
	int no_of_queues = temp / NO_OF_ENTRIES;
		starting_offset = temp % NO_OF_ENTRIES;
		if (starting_offset != 0) {
			no_of_wrap_buffs = starting_offset - 1; //exclude header
			starting_offset = NO_OF_ENTRIES - starting_offset;
			no_of_queues++;
		}
		starting_indx = queue_indx - no_of_queues;
		if (starting_indx < 0) {
			no_of_wrap_buffs -= (starting_indx + 1) * NO_OF_ENTRIES; 
			starting_indx = MFSBQ_LENGTH + starting_indx;
			wrap_around = TRUE;
		}
	}
	
	DPRINTK("starting_indx = %d, starting offset = %d no_of_wrap_buffs = %d\n", starting_indx, starting_offset, no_of_wrap_buffs);
	/* Get Tachyon Header from first buffer */
	buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_mfsbq_base + starting_indx*NO_OF_ENTRIES + starting_offset)));
	

	/* extract Type of Frame */
	type_of_frame = (u_int)ntohl(*(buff_addr + 4)) & 0xFF000000;
	s_id = (u_int)ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	received_ox_id = ntohl(*(buff_addr + 6)) >> 16;
	buff_addr += MFS_BUFFER_SIZE/4;
	DPRINTK("type_of_frame = %x, s_id = %x, ox_id = %x", type_of_frame, s_id, received_ox_id);

 	switch(type_of_frame) {
	  case TYPE_LLC_SNAP:
		skb = dev_alloc_skb(payload_size);
		if (skb == NULL) {
			printk(KERN_NOTICE "%s: In handle_MFS_interrupt() Memory squeeze, dropping packet.\n", fi->name);
			fi->fc_stats.rx_dropped++;
			fi->g.mfs_buffer_count += no_of_buffers;
			if (fi->g.mfs_buffer_count >= NO_OF_ENTRIES) {
			int count = fi->g.mfs_buffer_count / NO_OF_ENTRIES;
				fi->g.mfs_buffer_count -= NO_OF_ENTRIES * count;
				update_MFSBQ_indx(fi, count);
				return;
			}
		}
		if (wrap_around) {
		int wrap_size = no_of_wrap_buffs * MFS_BUFFER_SIZE;
		int tail_size = payload_size - wrap_size;
			DPRINTK("wrap_size = %d, tail_size = %d\n", wrap_size, tail_size);
			if (no_of_wrap_buffs) 
				memcpy(skb_put(skb, wrap_size), buff_addr, wrap_size);
			buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_mfsbq_base)));
			memcpy(skb_put(skb, tail_size), buff_addr, tail_size);
		}
		else
			memcpy(skb_put(skb, payload_size), buff_addr, payload_size);
		rx_net_mfs_packet(fi, skb);
	  	break;
	default:
		T_MSG("Unknown Frame Type received. Type = %x", type_of_frame);
	}

	/* provide Tachyon will another set of buffers */
	fi->g.mfs_buffer_count += no_of_buffers;
	if (fi->g.mfs_buffer_count >= NO_OF_ENTRIES) {
	int count = fi->g.mfs_buffer_count / NO_OF_ENTRIES;
		fi->g.mfs_buffer_count -= NO_OF_ENTRIES * count;
		update_MFSBQ_indx(fi, count);
	}
	LEAVE("handle_MFS_interrupt");
}

static void handle_Unknown_Frame_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
int queue_indx, offset;
	ENTER("handle_Unknown_Frame_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	/* We discard the "unknown" frame */
	/* provide Tachyon will another set of buffers */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_Unknown_Frame_interrupt");
}

static void handle_Busied_Frame_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
int queue_indx, offset;
	ENTER("handle_Busied_Frame_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	/* We discard the "busied" frame */
	/* provide Tachyon will another set of buffers */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_Busied_Frame_interrupt");
}

static void handle_Bad_SCSI_Frame_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry, *buff_addr, *tach_header, *ptr_edb;
u_int s_id, rctl, frame_class, burst_len, transfered_len, len = 0;
int queue_indx, offset, payload_size, i;
u_short ox_id, rx_id, x_id, mtu = 512;
u_char target_id = 0xFF;

	ENTER("handle_Bad_SCSI_Frame_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	payload_size = ntohl(*(ptr_imq_entry + 2));

	buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_sfsbq_base + queue_indx*NO_OF_ENTRIES + offset)));

	rctl = ntohl(*(buff_addr + 2)) & 0xFF000000;
	s_id = ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	ox_id = ntohl(*(buff_addr + 6)) >> 16;
	rx_id = ntohl(*(buff_addr + 6));
	x_id = ox_id & MAX_SCSI_XID;

	/* Any frame that comes in with OX_ID that matches an OX_ID 
	 * that has been allocated for SCSI, will be called a Bad
	 * SCSI frame if the Exchange is not valid any more.
	 *
	 * We will also get a Bad SCSI frame interrupt if we receive
	 * a XFER_RDY with offset != 0. Tachyon washes its hands off
	 * this Exchange. We have to take care of ourselves. Grrr...
	 */
	if (rctl == DATA_DESCRIPTOR) {
	struct fc_node_info *q = fi->node_info_list;
		while (q != NULL) {
			if (q->d_id == s_id) {
				target_id = q->target_id;
				mtu = q->mtu;
				break;
			}
			else
				q = q->next;
		}
		frame_class = target_id;
		transfered_len = ntohl(*(buff_addr + 8));
		burst_len = ntohl(*(buff_addr + 9));

		build_ODB(fi, fi->g.seq_id, s_id, burst_len, 0, mtu, ox_id, rx_id, 0, 0, frame_class << 16);
		/* Update the SEQ_ID and Relative Offset in the 
		 * Tachyon Header Structure.
		 */
		tach_header = bus_to_virt(ntohl(*(fi->q.ptr_sest[x_id] + 5)));
		*(tach_header + 5) = htonl(fi->g.seq_id << 24);
		*(tach_header + 7) = htonl(transfered_len);
		fi->g.odb.hdr_addr = *(fi->q.ptr_sest[x_id] + 5);

		/* Invalidate the EDBs used 
		 */
		ptr_edb = bus_to_virt(ntohl(*(fi->q.ptr_sest[x_id] + 7)));

		for (i = 0; i < EDB_LEN; i++)
			if (fi->q.ptr_edb[i] == ptr_edb)
				break;
		ptr_edb--;	
		
		if (i < EDB_LEN) {
		int j;
			do {
				ptr_edb += 2;
				len += (htonl(*ptr_edb) & 0xFFFF);
				j = i;
				fi->q.free_edb_list[i++] = EDB_FREE;
				if (i == EDB_LEN) {
					i = 0;
					ptr_edb = fi->q.ptr_edb_base - 1;
				}
			} while (len < transfered_len);
			if (len > transfered_len) {
				ptr_edb--;
				fi->q.free_edb_list[j] = EDB_BUSY;
			}
			else
				ptr_edb++;
		}
		else {
			T_MSG("EDB not found while freeing");
			if (offset == (NO_OF_ENTRIES - 1))
				update_SFSBQ_indx(fi);
			return;
		}

		/* Update the EDB pointer in the ODB.
		 */
		fi->g.odb.edb_addr = htonl(virt_to_bus(ptr_edb));
		memcpy(fi->q.ptr_odb[fi->q.ocq_prod_indx], &(fi->g.odb), sizeof(ODB));
		/* Update the EDB pointer in the SEST entry. We might need
		 * this if get another XFER_RDY for the same Exchange.
		 */
		*(fi->q.ptr_sest[x_id] + 7) = htonl(virt_to_bus(ptr_edb));

		update_OCQ_indx(fi);
		if (fi->g.seq_id == MAX_SEQ_ID)
			fi->g.seq_id = 0;
		else
			fi->g.seq_id++;
	}
	else 
	/* Could be a BA_ACC or a BA_RJT.
	 */
	if (rctl == RCTL_BASIC_ACC) {
	u_int bls_type = remove_from_ox_id_list(fi, ox_id);
		DPRINTK1("BA_ACC received from S_ID 0x%x with OX_ID = %x in response to %x", s_id, ox_id, bls_type);
		if (bls_type == RCTL_BASIC_ABTS) {
		u_int STE_bit;
			/* Invalidate resources for that Exchange.
			 */
			STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
			if (STE_bit & SEST_V) {
				*(fi->q.ptr_sest[x_id]) &= htonl(SEST_INV);
				invalidate_SEST_entry(fi, ox_id);
			}
		}
	}
	else
	if (rctl == RCTL_BASIC_RJT) {
	u_int bls_type = remove_from_ox_id_list(fi, ox_id);
		DPRINTK1("BA_RJT received from S_ID 0x%x with OX_ID = %x in response to %x", s_id, ox_id, bls_type);
		if (bls_type == RCTL_BASIC_ABTS) {
		u_int STE_bit;
			/* Invalidate resources for that Exchange.
			 */
			STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
			if (STE_bit & SEST_V) {
				*(fi->q.ptr_sest[x_id]) &= htonl(SEST_INV);
				invalidate_SEST_entry(fi, ox_id);
			}
		}
	}
	else
		DPRINTK1("Frame with R_CTL = %x received from S_ID 0x%x with OX_ID %x", rctl, s_id, ox_id);

	/* Else, discard the "Bad" SCSI frame.
	 */

	/* provide Tachyon will another set of buffers 
	 */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_Bad_SCSI_Frame_interrupt");
}

static void handle_Inbound_SCSI_Status_interrupt(struct fc_info *fi)
{
struct Scsi_Host *host = fi->host;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata *)host->hostdata;
u_int *ptr_imq_entry, *buff_addr, *ptr_rsp_info, *ptr_sense_info = NULL;
int queue_indx, offset, payload_size;
u_short received_ox_id, x_id;
Scsi_Cmnd *Cmnd;
u_int fcp_status, fcp_rsp_info_len = 0, fcp_sense_info_len = 0, s_id;
	ENTER("handle_SCSI_status_interrupt");

	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_sfsbq_base + queue_indx*NO_OF_ENTRIES + offset)));
	payload_size = ntohl(*(ptr_imq_entry + 2));
	received_ox_id = ntohl(*(buff_addr + 6)) >> 16;

	buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_sfsbq_base + queue_indx*NO_OF_ENTRIES + offset)));

	fcp_status = ntohl(*(buff_addr + 10));
	ptr_rsp_info = buff_addr + 14;
	if (fcp_status & FCP_STATUS_RSP_LEN)
		fcp_rsp_info_len = ntohl(*(buff_addr + 13));
		
	if (fcp_status & FCP_STATUS_SENSE_LEN) {
		ptr_sense_info = ptr_rsp_info + fcp_rsp_info_len / 4;
		fcp_sense_info_len = ntohl(*(buff_addr + 12));
		DPRINTK("sense_info = %x", (u_int)ntohl(*ptr_sense_info));
	}
	DPRINTK("fcp_status = %x, fcp_rsp_len = %x", fcp_status, fcp_rsp_info_len);
	x_id = received_ox_id & MAX_SCSI_XID;
	Cmnd = hostdata->cmnd_handler[x_id];
	hostdata->cmnd_handler[x_id] = NULL;
	if (Cmnd != NULL) {
		memset(Cmnd->sense_buffer, 0, sizeof(Cmnd->sense_buffer));
		/* Check if there is a Sense field */
		if (fcp_status & FCP_STATUS_SENSE_LEN) {
		int size = sizeof(Cmnd->sense_buffer);
			if (fcp_sense_info_len < size)
				size = fcp_sense_info_len;
			memcpy(Cmnd->sense_buffer, (char *)ptr_sense_info, size);
		}
		Cmnd->result = fcp_status & FCP_STATUS_MASK;
		(*Cmnd->scsi_done) (Cmnd);
	}
	else
		T_MSG("NULL Command out of handler!");

	invalidate_SEST_entry(fi, received_ox_id);
	s_id = ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	fi->q.free_scsi_oxid[x_id] = OXID_AVAILABLE;

	/* provide Tachyon will another set of buffers */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_SCSI_status_interrupt");
}

static void invalidate_SEST_entry(struct fc_info *fi, u_short received_ox_id)
{
u_short x_id = received_ox_id & MAX_SCSI_XID;
	/* Invalidate SEST entry if it is an OutBound SEST Entry 
	 */
	if (!(received_ox_id & SCSI_READ_BIT)) {
	u_int *ptr_tach_header, *ptr_edb;
	u_short temp_ox_id = NOT_SCSI_XID;
	int i;
		*(fi->q.ptr_sest[x_id]) &= htonl(SEST_INV);

		/* Invalidate the Tachyon Header structure 
		 */
		ptr_tach_header = bus_to_virt(ntohl(*(fi->q.ptr_sest[x_id] + 5)));
		for (i = 0; i < NO_OF_TACH_HEADERS; i++) 
			if(fi->q.ptr_tachyon_header[i] == ptr_tach_header)
				break;
		if (i < NO_OF_TACH_HEADERS) 
			memset(ptr_tach_header, 0xFF, 32);
		else
			T_MSG("Tachyon Header not found while freeing in invalidate_SEST_entry()");

		/* Invalidate the EDB used 
		 */
		ptr_edb = bus_to_virt(ntohl(*(fi->q.ptr_sest[x_id] + 7)));
		for (i = 0; i < EDB_LEN; i++)
			if (fi->q.ptr_edb[i] == ptr_edb)
				break;
		ptr_edb--;	
		if (i < EDB_LEN) {
			do {
				ptr_edb += 2;
				fi->q.free_edb_list[i++] = EDB_FREE;
				if (i == EDB_LEN) {
					i = 0;
					ptr_edb = fi->q.ptr_edb_base - 1;
				}
			} while ((htonl(*ptr_edb) & 0x80000000) != 0x80000000);
		}
		else
			T_MSG("EDB not found while freeing in invalidate_SEST_entry()");
		
		/* Search for its other header structure and destroy it! 
		 */
		if ((ptr_tach_header + 16) < (fi->q.ptr_tachyon_header_base + (MY_PAGE_SIZE/4)))
			ptr_tach_header += 16;
		else
			ptr_tach_header = fi->q.ptr_tachyon_header_base;
		while (temp_ox_id != x_id) {
			temp_ox_id = ntohl(*(ptr_tach_header + 6)) >> 16;
			if (temp_ox_id == x_id) {
				/* Paranoid checking...
				 */
				for (i = 0; i < NO_OF_TACH_HEADERS; i++) 
					if(fi->q.ptr_tachyon_header[i] == ptr_tach_header)
						break;
				if (i < NO_OF_TACH_HEADERS)
					memset(ptr_tach_header, 0xFF, 32);
				else
					T_MSG("Tachyon Header not found while freeing in invalidate_SEST_entry()");
				break;
			}
			else {
				if ((ptr_tach_header + 16) < (fi->q.ptr_tachyon_header_base + (MY_PAGE_SIZE/4)))
					ptr_tach_header += 16;
				else
					ptr_tach_header = fi->q.ptr_tachyon_header_base;
			}
		}
	}
	else {
	u_short sdb_table_indx;
		/* An Inbound Command has completed or needs to be Aborted. 
	 	 * Clear up the SDB buffers.
		 */
		sdb_table_indx = *(fi->q.ptr_sest[x_id] + 5);
		fi->q.sdb_slot_status[sdb_table_indx] = SDB_FREE;
	}
}

static void handle_Inbound_SCSI_Command_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry;
int queue_indx, offset;
	ENTER("handle_Inbound_SCSI_Command_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	/* We discard the SCSI frame as we shouldn't be receiving
	 * a SCSI Command in the first place 
	 */
	/* provide Tachyon will another set of buffers */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_Inbound_SCSI_Command_interrupt");
}

static void handle_SFS_interrupt(struct fc_info *fi)
{
u_int *ptr_imq_entry, *buff_addr;
u_int class_of_frame, type_of_frame, s_id, els_type = 0, rctl;
int queue_indx, offset, payload_size, login_state;
u_short received_ox_id, fs_cmnd_code;
	ENTER("handle_SFS_interrupt");
	ptr_imq_entry = fi->q.ptr_imqe[fi->q.imq_cons_indx];
	offset = ntohl(*(ptr_imq_entry + 1)) & 0x00000007;
	queue_indx = ntohl(*(ptr_imq_entry + 1)) & 0xFFFF0000;
	queue_indx = queue_indx >> 16;
	DPRINTK("queue_indx = %d, offset  = %d\n", queue_indx, offset);
	payload_size = ntohl(*(ptr_imq_entry + 2));
	DPRINTK("payload_size = %d", payload_size);

	buff_addr = bus_to_virt(ntohl(*(fi->q.ptr_sfsbq_base + queue_indx*NO_OF_ENTRIES + offset)));

	/* extract Type of Frame */
	type_of_frame = ntohl(*(buff_addr + 4)) & 0xFF000000;
	s_id = ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	received_ox_id = ntohl(*(buff_addr + 6)) >> 16;
	switch(type_of_frame) {
		case TYPE_BLS:
			rctl = ntohl(*(buff_addr + 2)) & 0xFF000000;
			switch(rctl) {
				case RCTL_BASIC_ABTS:
					/* As an Initiator, we should never be receiving 
					 * this.
		 			 */
					DPRINTK1("ABTS received from S_ID 0x%x with OX_ID = %x", s_id, received_ox_id);
					break;
			}
			break;
		case TYPE_ELS:
			class_of_frame = ntohl(*(buff_addr + 8));
			login_state = sid_logged_in(fi, s_id);
			switch(class_of_frame & 0xFF000000) {
				case ELS_PLOGI:
					if (s_id != fi->g.my_id) {
						u_int ret_code;
						DPRINTK1("PLOGI received from D_ID 0x%x with 0X_ID = %x", s_id, received_ox_id);
						if ((ret_code = plogi_ok(fi, buff_addr, payload_size)) == 0){
							tx_logi_acc(fi, ELS_ACC, s_id, received_ox_id);
							add_to_address_cache(fi, buff_addr);
						}
						else {
							u_short cmnd_code = ret_code >> 16;
							u_short expln_code =  ret_code;
							tx_ls_rjt(fi, s_id, received_ox_id, cmnd_code, expln_code);
						}
					}
					break;
				case ELS_ACC:
					els_type = remove_from_ox_id_list(fi, received_ox_id);
					DPRINTK1("ELS_ACC received from D_ID 0x%x in response to ELS %x", s_id, els_type);
					switch(els_type) {
						case ELS_PLOGI:
							add_to_address_cache(fi, buff_addr);
							tx_prli(fi, ELS_PRLI, s_id, OX_ID_FIRST_SEQUENCE);
							break;
						case ELS_FLOGI:
							add_to_address_cache(fi, buff_addr);
							fi->g.my_id = ntohl(*(buff_addr + 2)) & 0x00FFFFFF;
							fi->g.fabric_present = TRUE;
							fi->g.my_ddaa = fi->g.my_id & 0xFFFF00;
							/* Login to the Name Server 
							 */
							tx_logi(fi, ELS_PLOGI, DIRECTORY_SERVER); 
							break;
						case ELS_NS_PLOGI:
							fi->g.name_server = TRUE;
							add_to_address_cache(fi, buff_addr);
							tx_name_server_req(fi, FCS_RFC_4);
							tx_scr(fi);
							/* Some devices have a delay before 
							 * registering with the Name Server 
							 */
							udelay(500); 
							tx_name_server_req(fi, FCS_GP_ID4);
							break;
						case ELS_PRLI:
							mark_scsi_sid(fi, buff_addr, ADD_ENTRY);
							break;
						case ELS_ADISC:
							if (!(validate_login(fi, buff_addr)))
								tx_logo(fi, s_id, OX_ID_FIRST_SEQUENCE);
							break;
					}
					break;
				case ELS_PDISC:
					DPRINTK1("ELS_PDISC received from D_ID 0x%x", s_id);
					tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_ADISC:
					DPRINTK1("ELS_ADISC received from D_ID 0x%x", s_id);
					if (node_logged_in_prev(fi, buff_addr))
						tx_adisc(fi, ELS_ACC, s_id, received_ox_id);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_PRLI:
					DPRINTK1("ELS_PRLI received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN)) {
						tx_prli(fi, ELS_ACC, s_id, received_ox_id);
						mark_scsi_sid(fi, buff_addr, ADD_ENTRY);
					}
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_PRLO:
					DPRINTK1("ELS_PRLO received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_OUT) || (login_state == NODE_NOT_PRESENT))
						tx_logo(fi, s_id, received_ox_id);
					else
					if (login_state == NODE_LOGGED_IN)

						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
					if (login_state == NODE_PROCESS_LOGGED_IN) {
						tx_prli(fi, ELS_ACC, s_id, received_ox_id);
						mark_scsi_sid(fi, buff_addr, DELETE_ENTRY);
					}
					break;
				case ELS_LS_RJT:
					els_type = remove_from_ox_id_list(fi, received_ox_id);
					DPRINTK1("ELS_LS_RJT received from D_ID 0x%x in response to %x", s_id, els_type);
					/* We should be chking the reason code.
					 */
					switch (els_type) {
						case ELS_ADISC:
							tx_logi(fi, ELS_PLOGI, s_id);
							break;
					}		
					break;
				case ELS_LOGO:
					els_type = remove_from_ox_id_list(fi, received_ox_id);
					DPRINTK1("ELS_LOGO received from D_ID 0x%x in response to %x", s_id, els_type);
					remove_from_address_cache(fi, buff_addr, ELS_LOGO);
					tx_acc(fi, s_id, received_ox_id);
					if (els_type == ELS_ADISC)
						tx_logi(fi, ELS_PLOGI, s_id);
					break;
				case ELS_RSCN:
					DPRINTK1("ELS_RSCN received from D_ID 0x%x", s_id);
					tx_acc(fi, s_id, received_ox_id);
					remove_from_address_cache(fi, buff_addr, ELS_RSCN);
					break;
				case ELS_FARP_REQ:
					/* We do not support FARP.
					   So, silently discard it */
					DPRINTK1("ELS_FARP_REQ received from D_ID 0x%x", s_id);
					break;
				case ELS_ABTX:
					DPRINTK1("ELS_ABTX received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_FLOGI:
					DPRINTK1("ELS_FLOGI received from D_ID 0x%x", s_id);
					if (fi->g.ptp_up == TRUE) {
						/* The node could have come up as an N_Port
						 * in a Loop! So,try initializing as an NL_port
						 */
						take_tachyon_offline(fi);
						/* write AL_TIME & E_D_TOV into the registers */
						writel(TOV_VALUES, fi->t_r.ptr_fm_tov_reg);
						writel(LOOP_INIT_SOFT_ADDRESS, fi->t_r.ptr_fm_config_reg);
						DPRINTK1("FLOGI received, TACHYON initializing as L_Port...\n");
						writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
					}
					else {
						if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
							tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
						else
							tx_logo(fi, s_id, received_ox_id);
					}
					break;
				case ELS_ADVC:
					DPRINTK1("ELS_ADVC received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_ECHO:
					DPRINTK1("ELS_ECHO received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_ESTC:
					DPRINTK1("ELS_ESTC received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_ESTS:
					DPRINTK1("ELS_ESTS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RCS:
					DPRINTK1("ELS_RCS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RES:
					DPRINTK1("ELS_RES received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RLS:
					DPRINTK1("ELS_RLS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RRQ:
					DPRINTK1("ELS_RRQ received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RSS:
					DPRINTK1("ELS_RSS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RTV:
					DPRINTK1("ELS_RTV received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RSI:
					DPRINTK1("ELS_RSI received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_TEST:
					/* No reply sequence */
					DPRINTK1("ELS_TEST received from D_ID 0x%x", s_id);
					break;
				case ELS_RNC:
					DPRINTK1("ELS_RNC received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_RVCS:
					DPRINTK1("ELS_RVCS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_TPLS:
					DPRINTK1("ELS_TPLS received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_GAID:
					DPRINTK1("ELS_GAID received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_FACT:
					DPRINTK1("ELS_FACT received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_FAN:
					/* Hmmm... You don't support FAN ??? */
					DPRINTK1("ELS_FAN received from D_ID 0x%x", s_id);
					tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					break;
				case ELS_FDACT:
					DPRINTK1("ELS_FDACT received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_NACT:
					DPRINTK1("ELS_NACT received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_NDACT:
					DPRINTK1("ELS_NDACT received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_QoSR:
					DPRINTK1("ELS_QoSR received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				case ELS_FDISC:
					DPRINTK1("ELS_FDISC received from D_ID 0x%x", s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
				default:
					DPRINTK1("ELS Frame %x received from D_ID 0x%x", class_of_frame, s_id);
					if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN))
						tx_ls_rjt(fi, s_id, received_ox_id, CMND_NOT_SUPP, NO_EXPLN);
					else
						tx_logo(fi, s_id, received_ox_id);
					break;
			}
			break;
		case TYPE_FC_SERVICES:
			fs_cmnd_code = (ntohl(*(buff_addr + 10)) & 0xFFFF0000) >>16;
			switch(fs_cmnd_code) {
				case FCS_ACC:
					els_type = remove_from_ox_id_list(fi, received_ox_id);
					DPRINTK1("FCS_ACC received from D_ID 0x%x in response to %x", s_id, els_type);
					if (els_type == FCS_GP_ID4) 
						explore_fabric(fi, buff_addr);
					break;
				case FCS_REJECT:
					DPRINTK1("FCS_REJECT received from D_ID 0x%x in response to %x", s_id, els_type);
					break;
			}
			break;
		case TYPE_LLC_SNAP:
			rx_net_packet(fi, (u_char *)buff_addr, payload_size);
			break;
		default:
			T_MSG("Frame Type %x received from %x", type_of_frame, s_id);
	}

	/* provide Tachyon will another set of buffers */
	if (offset == (NO_OF_ENTRIES - 1))
		update_SFSBQ_indx(fi);
	LEAVE("handle_SFS_interrupt");
}

static void handle_FM_interrupt(struct fc_info *fi)
{
u_int fm_status;
u_int tachyon_status;

	ENTER("handle_FM_interrupt");
	fm_status = readl(fi->t_r.ptr_fm_status_reg);
	tachyon_status = readl(fi->t_r.ptr_tach_status_reg);
	DPRINTK("FM_status = %x, Tachyon_status = %x", fm_status, tachyon_status);
	if (fm_status & LINK_DOWN) {
		T_MSG("Fibre Channel Link DOWN");
		fm_status = readl(fi->t_r.ptr_fm_status_reg);
		
		del_timer(&fi->explore_timer);
		del_timer(&fi->nport_timer);
		del_timer(&fi->lport_timer);
		del_timer(&fi->display_cache_timer);
		fi->g.link_up = FALSE;
		if (fi->g.ptp_up == TRUE)
			fi->g.n_port_try = FALSE;
		fi->g.ptp_up = FALSE;
		fi->g.port_discovery = FALSE;
		fi->g.explore_fabric = FALSE;
		fi->g.perform_adisc = FALSE;

		/* Logout will all nodes */
		if (fi->node_info_list) {
			struct fc_node_info *temp_list = fi->node_info_list;
				while(temp_list) {
					temp_list->login = LOGIN_ATTEMPTED;
					temp_list = temp_list->next;
				}
				fi->num_nodes = 0;
		}

		if ((fi->g.n_port_try == FALSE) && (fi->g.dont_init == FALSE)){
			take_tachyon_offline(fi);
			/* write AL_TIME & E_D_TOV into the registers */
			writel(TOV_VALUES, fi->t_r.ptr_fm_tov_reg);
			
			if ((fi->g.fabric_present == TRUE) && (fi->g.loop_up == TRUE)) {
			u_int al_pa = fi->g.my_id & 0xFF;
				writel((al_pa << 24) | LOOP_INIT_FABRIC_ADDRESS | LOOP_INIT_PREVIOUS_ADDRESS, fi->t_r.ptr_fm_config_reg);
			}
			else 
			if (fi->g.loop_up == TRUE) {
			u_int al_pa = fi->g.my_id & 0xFF;
				writel((al_pa << 24) | LOOP_INIT_PREVIOUS_ADDRESS, fi->t_r.ptr_fm_config_reg);
			}
			else 
				writel(LOOP_INIT_SOFT_ADDRESS, fi->t_r.ptr_fm_config_reg);
			fi->g.loop_up = FALSE;
			DPRINTK1("In LDWN TACHYON initializing as L_Port...\n");
			writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
		}
	}

    if (fm_status & NON_PARTICIPATING) {
	  	T_MSG("Did not acquire an AL_PA. I am not participating");
    }
	else
	if ((fm_status & LINK_UP) && ((fm_status & LINK_DOWN) == 0)) {
	  T_MSG("Fibre Channel Link UP");
	  if ((fm_status & NON_PARTICIPATING) != TRUE) {
		fi->g.link_up = TRUE;
		if (tachyon_status & OSM_FROZEN) {
			reset_tachyon(fi, ERROR_RELEASE);
			reset_tachyon(fi, OCQ_RESET);
		}
		init_timer(&fi->explore_timer);
		init_timer(&fi->nport_timer);
		init_timer(&fi->lport_timer);
		init_timer(&fi->display_cache_timer);
		if ((fm_status & OLD_PORT) == 0) {
			fi->g.loop_up = TRUE;
			fi->g.ptp_up = FALSE;
			fi->g.my_id = readl(fi->t_r.ptr_fm_config_reg) >> 24;
			DPRINTK1("My AL_PA = %x", fi->g.my_id);
			fi->g.port_discovery = TRUE;
			fi->g.explore_fabric = FALSE;
		}
		else
		if (((fm_status & 0xF0) == OLD_PORT) && ((fm_status & 0x0F) == PORT_STATE_ACTIVE)) {
			fi->g.loop_up = FALSE;
			fi->g.my_id = 0x0;
			/* In a point-to-point configuration, we expect to be
			 * connected to an F_Port. This driver does not yet support
			 * a configuration where it is connected to another N_Port
			 * directly.
			 */
			fi->g.explore_fabric = TRUE;
			fi->g.port_discovery = FALSE;
			if (fi->g.n_port_try == FALSE) {
				take_tachyon_offline(fi);
				/* write R_T_TOV & E_D_TOV into the registers */
				writel(PTP_TOV_VALUES, fi->t_r.ptr_fm_tov_reg);
				writel(BB_CREDIT | NPORT, fi->t_r.ptr_fm_config_reg);
				fi->g.n_port_try = TRUE;
				DPRINTK1("In LUP TACHYON initializing as N_Port...\n");
				writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
			}
			else {
				fi->g.ptp_up = TRUE;
				tx_logi(fi, ELS_FLOGI, F_PORT); 
			}
		}
		fi->g.my_ddaa = 0x0;
		fi->g.fabric_present = FALSE; 
		/* We havn't sent out any Name Server Reqs */
		fi->g.name_server = FALSE;
		fi->g.alpa_list_index = 0;
		fi->g.ox_id = NOT_SCSI_XID;
		fi->g.my_mtu = FRAME_SIZE;
		
		/* Implicitly LOGO with all logged-in nodes. 
		 */
		if (fi->node_info_list) {
		struct fc_node_info *temp_list = fi->node_info_list;
			while(temp_list) {
				temp_list->login = LOGIN_ATTEMPTED;
				temp_list = temp_list->next;
			}
			fi->num_nodes = 0;
			fi->g.perform_adisc = TRUE;
			//fi->g.perform_adisc = FALSE;
			fi->g.port_discovery = FALSE;
			tx_logi(fi, ELS_FLOGI, F_PORT); 
		}
		else { 
			/* If Link coming up for the _first_ time or no nodes
			 * were logged in before...
			 */
			fi->g.scsi_oxid = 0;
			fi->g.seq_id = 0x00;
			fi->g.perform_adisc = FALSE;
		}

		/* reset OX_ID table */
		while (fi->ox_id_list) {
		struct ox_id_els_map *temp = fi->ox_id_list;
			fi->ox_id_list = fi->ox_id_list->next;
			kfree(temp);
		}
		fi->ox_id_list = NULL;
	  } /* End of if partipating */
	}

	if (fm_status & ELASTIC_STORE_ERROR) {
		/* Too much junk on the Link 
		 */
		/* Trying to clear it up by Txing PLOGI to urself */
		if (fi->g.link_up == TRUE)
			tx_logi(fi, ELS_PLOGI, fi->g.my_id); 
	}

	if (fm_status & LOOP_UP) {
		if (tachyon_status & OSM_FROZEN) {
			reset_tachyon(fi, ERROR_RELEASE);
			reset_tachyon(fi, OCQ_RESET);
		}
	}
	
	if (fm_status & NOS_OLS_RECEIVED){
		if (fi->g.nport_timer_set == FALSE) {
			DPRINTK("NOS/OLS Received");
			DPRINTK("FM_status = %x", fm_status);
			fi->nport_timer.function = nos_ols_timer;
			fi->nport_timer.data = (unsigned long)fi;
			fi->nport_timer.expires = RUN_AT((3*HZ)/100); /* 30 msec */
			init_timer(&fi->nport_timer);
			add_timer(&fi->nport_timer);
			fi->g.nport_timer_set = TRUE;
		}
	}

	if (((fm_status & 0xF0) == OLD_PORT) && (((fm_status & 0x0F) == PORT_STATE_LF1) || ((fm_status & 0x0F) == PORT_STATE_LF2))) {
		DPRINTK1("Link Fail-I in OLD-PORT.");
		take_tachyon_offline(fi);
		reset_tachyon(fi, SOFTWARE_RESET);
	}

	if (fm_status & LOOP_STATE_TIMEOUT){
		if ((fm_status & 0xF0) == ARBITRATING) 
			DPRINTK1("ED_TOV timesout.In ARBITRATING state...");
		if ((fm_status & 0xF0) == ARB_WON)
			DPRINTK1("ED_TOV timesout.In ARBITRATION WON state...");
		if ((fm_status & 0xF0) == OPEN)
			DPRINTK1("ED_TOV timesout.In OPEN state...");
		if ((fm_status & 0xF0) == OPENED)
			DPRINTK1("ED_TOV timesout.In OPENED state...");
		if ((fm_status & 0xF0) == TX_CLS)
			DPRINTK1("ED_TOV timesout.In XMITTED CLOSE state...");
		if ((fm_status & 0xF0) == RX_CLS)
			DPRINTK1("ED_TOV timesout.In RECEIVED CLOSE state...");
		if ((fm_status & 0xF0) == INITIALIZING)
			DPRINTK1("ED_TOV timesout.In INITIALIZING state...");
		DPRINTK1("Initializing Loop...");
		writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
	}
	
	if ((fm_status & BAD_ALPA) && (fi->g.loop_up == TRUE)) {
	u_char bad_alpa = (readl(fi->t_r.ptr_fm_rx_al_pa_reg) & 0xFF00) >> 8;
		if (tachyon_status & OSM_FROZEN) {
			reset_tachyon(fi, ERROR_RELEASE);
			reset_tachyon(fi, OCQ_RESET);
		}
		/* Fix for B34 */
		tx_logi(fi, ELS_PLOGI, fi->g.my_id); 

		if (!fi->g.port_discovery && !fi->g.perform_adisc) {
			if (bad_alpa != 0xFE)
				DPRINTK("Bad AL_PA = %x", bad_alpa);
		}
		else {
			if ((fi->g.perform_adisc == TRUE) && (bad_alpa == 0x00)) {
				DPRINTK1("Performing ADISC...");
				fi->g.fabric_present = FALSE;
				perform_adisc(fi);
			}
		}
	}
	
	if (fm_status & LIPF_RECEIVED){
		DPRINTK("LIP(F8) Received");
	}

	if (fm_status & LINK_FAILURE) {
		if (fm_status & LOSS_OF_SIGNAL)
			DPRINTK1("Detected Loss of Signal.");
		if (fm_status & OUT_OF_SYNC)
			DPRINTK1("Detected Loss of Synchronization.");
	}

	if (fm_status & TRANSMIT_PARITY_ERROR) {
		/* Bad! Should not happen. Solution-> Hard Reset.
		 */
		T_MSG("Parity Error. Perform Hard Reset!");
	}

	if (fi->g.alpa_list_index >= MAX_NODES){
		if (fi->g.port_discovery == TRUE) {
			fi->g.port_discovery = FALSE;
			add_display_cache_timer(fi);
		}
		fi->g.alpa_list_index = MAX_NODES;
	}

	if (fi->g.port_discovery == TRUE) 
		local_port_discovery(fi);

	LEAVE("handle_FM_interrupt");
	return;
}

static void local_port_discovery(struct fc_info *fi)
{
	if (fi->g.loop_up == TRUE) {
		/* If this is not here, some of the Bad AL_PAs are missed. 
		 */
		udelay(20); 
		if ((fi->g.alpa_list_index == 0) && (fi->g.fabric_present == FALSE)){
			tx_logi(fi, ELS_FLOGI, F_PORT); 
		}
		else {
		int login_state = sid_logged_in(fi, fi->g.my_ddaa | alpa_list[fi->g.alpa_list_index]);
			while ((fi->g.alpa_list_index == 0) || ((fi->g.alpa_list_index < MAX_NODES) && ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN) || (alpa_list[fi->g.alpa_list_index] == (fi->g.my_id & 0xFF)))))
				fi->g.alpa_list_index++;
			if (fi->g.alpa_list_index < MAX_NODES)
				tx_logi(fi, ELS_PLOGI, alpa_list[fi->g.alpa_list_index]); 
		}
		fi->g.alpa_list_index++;
		if (fi->g.alpa_list_index >= MAX_NODES){
			if (fi->g.port_discovery == TRUE) {
				fi->g.port_discovery = FALSE;
				add_display_cache_timer(fi);
			}
			fi->g.alpa_list_index = MAX_NODES;
		}
	}
}

static void nos_ols_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info*)data;
u_int fm_status;
	fm_status = readl(fi->t_r.ptr_fm_status_reg);
	DPRINTK1("FM_status in timer= %x", fm_status);
	fi->g.nport_timer_set = FALSE;
	del_timer(&fi->nport_timer);
	if ((fi->g.ptp_up == TRUE) || (fi->g.loop_up == TRUE))
		return;
	if (((fm_status & 0xF0) == OLD_PORT) && (((fm_status & 0x0F) == PORT_STATE_ACTIVE) || ((fm_status & 0x0F) == PORT_STATE_OFFLINE))) {
		DPRINTK1("In OLD-PORT after E_D_TOV.");
		take_tachyon_offline(fi);
		/* write R_T_TOV & E_D_TOV into the registers */
		writel(PTP_TOV_VALUES, fi->t_r.ptr_fm_tov_reg);
		writel(BB_CREDIT | NPORT, fi->t_r.ptr_fm_config_reg);
		fi->g.n_port_try = TRUE;
		DPRINTK1("In timer, TACHYON initializing as N_Port...\n");
		writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
	}
	else
	if ((fi->g.lport_timer_set == FALSE) && ((fm_status & 0xF0) == LOOP_FAIL)) {
		DPRINTK1("Loop Fail after E_D_TOV.");
		fi->lport_timer.function = loop_timer;
		fi->lport_timer.data = (unsigned long)fi;
		fi->lport_timer.expires = RUN_AT((8*HZ)/100); 
		init_timer(&fi->lport_timer);
		add_timer(&fi->lport_timer);
		fi->g.lport_timer_set = TRUE;
		take_tachyon_offline(fi);
		reset_tachyon(fi, SOFTWARE_RESET);
	}
	else
	if (((fm_status & 0xF0) == OLD_PORT) && (((fm_status & 0x0F) == PORT_STATE_LF1) || ((fm_status & 0x0F) == PORT_STATE_LF2))) {
		DPRINTK1("Link Fail-II in OLD-PORT.");
		take_tachyon_offline(fi);
		reset_tachyon(fi, SOFTWARE_RESET);
	}
}

static void loop_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info*)data;
	fi->g.lport_timer_set = FALSE;
	del_timer(&fi->lport_timer);
	if ((fi->g.ptp_up == TRUE) || (fi->g.loop_up == TRUE))
		return;
}

static void add_display_cache_timer(struct fc_info *fi)
{
	fi->display_cache_timer.function = display_cache_timer;
	fi->display_cache_timer.data = (unsigned long)fi;
	fi->display_cache_timer.expires = RUN_AT(fi->num_nodes * HZ); 
	init_timer(&fi->display_cache_timer);
	add_timer(&fi->display_cache_timer);
}

static void display_cache_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info*)data;
	del_timer(&fi->display_cache_timer);
	display_cache(fi);
	return;
}

static void reset_tachyon(struct fc_info *fi, u_int value)
{
u_int tachyon_status, reset_done = OCQ_RESET_STATUS | SCSI_FREEZE_STATUS;
int not_done = 1, i = 0;
	writel(value, fi->t_r.ptr_tach_control_reg);
	if (value == OCQ_RESET) 
		fi->q.ocq_prod_indx = 0;
	tachyon_status = readl(fi->t_r.ptr_tach_status_reg);

	/* Software resets are immediately done, whereas other aren't. It 
	about 30 clocks to do the reset */
	if (value != SOFTWARE_RESET) {
		while(not_done) {
			if (i++ > 100000) {
				T_MSG("Reset was unsuccessful! Tachyon Status = %x", tachyon_status);
				break;
			}
			tachyon_status = readl(fi->t_r.ptr_tach_status_reg);
			if ((tachyon_status & reset_done) == 0)
				not_done = 0;
		}
	}
	else {
		write_to_tachyon_registers(fi);
	}
}

static void take_tachyon_offline(struct fc_info *fi)
{
u_int fm_status = readl(fi->t_r.ptr_fm_status_reg);

	/* The first two conditions will never be true. The Manual and
	 * the errata say this. But the current implementation is
	 * decently stable.
	 */	 
	//if ((fm_status & 0xF0) == LOOP_FAIL) {
	if (fm_status == LOOP_FAIL) {
		// workaround as in P. 89 
		writel(HOST_CONTROL, fi->t_r.ptr_fm_control_reg);
		if (fi->g.loop_up == TRUE)
			writel(SOFTWARE_RESET, fi->t_r.ptr_tach_control_reg);
		else {
			writel(OFFLINE, fi->t_r.ptr_fm_control_reg);
			writel(EXIT_HOST_CONTROL, fi->t_r.ptr_fm_control_reg);
		}
	}
	else
	//if ((fm_status & LOOP_UP) == LOOP_UP) {
	if (fm_status == LOOP_UP) {
		writel(SOFTWARE_RESET, fi->t_r.ptr_tach_control_reg);
	}
	else
		writel(OFFLINE, fi->t_r.ptr_fm_control_reg);
}


static void read_novram(struct fc_info *fi)
{
int off = 0;
	fi->n_r.ptr_novram_hw_control_reg = fi->i_r.ptr_ichip_hw_control_reg; 
	fi->n_r.ptr_novram_hw_status_reg = fi->i_r.ptr_ichip_hw_status_reg; 
	iph5526_nr_do_init(fi);
	if (fi->clone_id == PCI_VENDOR_ID_INTERPHASE)
		off = 32;
	
	fi->g.my_node_name_high = (fi->n_r.data[off] << 16) | fi->n_r.data[off+1];
	fi->g.my_node_name_low = (fi->n_r.data[off+2] << 16) | fi->n_r.data[off+3];
	fi->g.my_port_name_high = (fi->n_r.data[off+4] << 16) | fi->n_r.data[off+5];
	fi->g.my_port_name_low = (fi->n_r.data[off+6] << 16) | fi->n_r.data[off+7];
	DPRINTK("node_name = %x %x", fi->g.my_node_name_high, fi->g.my_node_name_low);
	DPRINTK("port_name = %x %x", fi->g.my_port_name_high, fi->g.my_port_name_low);
}

static void reset_ichip(struct fc_info *fi)
{
	/* (i)chip reset */
	writel(ICHIP_HCR_RESET, fi->i_r.ptr_ichip_hw_control_reg);
	/*wait for chip to get reset */
	udelay(10000);
	/*de-assert reset */
	writel(ICHIP_HCR_DERESET, fi->i_r.ptr_ichip_hw_control_reg);
	
	/* enable INT lines on the (i)chip */
	writel(ICHIP_HCR_ENABLE_INTA , fi->i_r.ptr_ichip_hw_control_reg);
	/* enable byte swap */
	writel(ICHIP_HAMR_BYTE_SWAP_ADDR_TR, fi->i_r.ptr_ichip_hw_addr_mask_reg);
}

static void tx_logi(struct fc_info *fi, u_int logi, u_int d_id)
{
int int_required = 1;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
u_int r_ctl = RCTL_ELS_UCTL;
u_int type  = TYPE_ELS | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_logi");
	/* We dont want interrupted for our own logi. 
	 * It screws up the port discovery process. 
	 */
	if (d_id == fi->g.my_id)
		int_required = 0;
	fill_login_frame(fi, logi);	
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.login, sizeof(LOGIN));
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),sizeof(LOGIN), r_ctl, type, d_id, my_mtu, int_required, ox_id, logi);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_logi");
	return;
}

static void tx_logi_acc(struct fc_info *fi, u_int logi, u_int d_id, u_short received_ox_id)
{
int int_required = 0;
u_int r_ctl = RCTL_ELS_SCTL;
u_int type  = TYPE_ELS | EXCHANGE_RESPONDER | LAST_SEQUENCE;
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_logi_acc");
	fill_login_frame(fi, logi);	
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.login, sizeof(LOGIN));
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),sizeof(LOGIN), r_ctl, type, d_id, my_mtu, int_required, received_ox_id, logi);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_logi_acc");
	return;
}

static void tx_prli(struct fc_info *fi, u_int command_code, u_int d_id, u_short received_ox_id)
{
int int_required = 1;
u_int r_ctl = RCTL_ELS_UCTL;
u_int type  = TYPE_ELS | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_prli");
	if (command_code == ELS_PRLI)
		fi->g.prli.cmnd_code = htons((ELS_PRLI | PAGE_LEN) >> 16);
	else {
		fi->g.prli.cmnd_code = htons((ELS_ACC | PAGE_LEN) >> 16);
		int_required = 0;
		type  = TYPE_ELS | EXCHANGE_RESPONDER | LAST_SEQUENCE;
		r_ctl = RCTL_ELS_SCTL;
	}
	fi->g.prli.payload_length = htons(PRLI_LEN);
	fi->g.prli.type_code = htons(FCP_TYPE_CODE);
	fi->g.prli.est_image_pair = htons(IMAGE_PAIR);
	fi->g.prli.responder_pa = 0;
	fi->g.prli.originator_pa = 0;
	fi->g.prli.service_params = htonl(INITIATOR_FUNC | READ_XFER_RDY_DISABLED);
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.prli, sizeof(PRLI));
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]), sizeof(PRLI), r_ctl, type, d_id, my_mtu, int_required, received_ox_id, command_code);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_prli");
	return;
}

static void tx_logo(struct fc_info *fi, u_int d_id, u_short received_ox_id)
{
int int_required = 1;
u_int r_ctl = RCTL_ELS_UCTL;
u_int type  = TYPE_ELS | EXCHANGE_RESPONDER | SEQUENCE_RESPONDER | FIRST_SEQUENCE | END_SEQUENCE | SEQUENCE_INITIATIVE;
int size = sizeof(LOGO);
char fc_id[3];
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_logo");
	fi->g.logo.logo_cmnd = htonl(ELS_LOGO);
	fi->g.logo.reserved = 0;
	memcpy(fc_id, &(fi->g.my_id), 3);
	fi->g.logo.n_port_id_0 = fc_id[0];
	fi->g.logo.n_port_id_1 = fc_id[1];
	fi->g.logo.n_port_id_2 = fc_id[2];
	fi->g.logo.port_name_up = htonl(N_PORT_NAME_HIGH);
	fi->g.logo.port_name_low = htonl(N_PORT_NAME_LOW);
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.logo, sizeof(LOGO));
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, received_ox_id, ELS_LOGO);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_logo");
}

static void tx_adisc(struct fc_info *fi, u_int cmnd_code, u_int d_id, u_short received_ox_id)
{
int int_required = 0;
u_int r_ctl = RCTL_ELS_SCTL;
u_int type  = TYPE_ELS | EXCHANGE_RESPONDER | SEQUENCE_RESPONDER | FIRST_SEQUENCE | END_SEQUENCE;
int size = sizeof(ADISC);
u_int my_mtu = fi->g.my_mtu;
	fi->g.adisc.ls_cmnd_code = htonl(cmnd_code);
	fi->g.adisc.hard_address = htonl(0);
	fi->g.adisc.port_name_high = htonl(N_PORT_NAME_HIGH);	
	fi->g.adisc.port_name_low = htonl(N_PORT_NAME_LOW);	
	fi->g.adisc.node_name_high = htonl(NODE_NAME_HIGH);	
	fi->g.adisc.node_name_low = htonl(NODE_NAME_LOW);	
	fi->g.adisc.n_port_id = htonl(fi->g.my_id);
	if (cmnd_code == ELS_ADISC) {
		int_required = 1;
		r_ctl = RCTL_ELS_UCTL;
		type  = TYPE_ELS | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
	}
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.adisc, size);
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, received_ox_id, cmnd_code);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
}

static void tx_ls_rjt(struct fc_info *fi, u_int d_id, u_short received_ox_id, u_short reason_code, u_short expln_code)
{
int int_required = 0;
u_int r_ctl = RCTL_ELS_SCTL;
u_int type  = TYPE_ELS | EXCHANGE_RESPONDER | LAST_SEQUENCE;
int size = sizeof(LS_RJT);
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_ls_rjt");
	fi->g.ls_rjt.cmnd_code = htonl(ELS_LS_RJT);
	fi->g.ls_rjt.reason_code = htonl((reason_code << 16) | expln_code); 
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.ls_rjt, size);
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, received_ox_id, ELS_LS_RJT);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_ls_rjt");
}

static void tx_abts(struct fc_info *fi, u_int d_id, u_short ox_id)
{
int int_required = 1;
u_int r_ctl = RCTL_BASIC_ABTS;
u_int type  = TYPE_BLS | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
int size = 0;
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_abts");
	fi->g.type_of_frame = FC_BLS;
	tx_exchange(fi, NULL, size, r_ctl, type, d_id, my_mtu, int_required, ox_id, RCTL_BASIC_ABTS);
	LEAVE("tx_abts");
}

static u_int plogi_ok(struct fc_info *fi, u_int *buff_addr, int size)
{
int ret_code = 0;
u_short mtu = ntohl(*(buff_addr + 10)) & 0x00000FFF;
u_short class3 = ntohl(*(buff_addr + 25)) >> 16;
u_short class3_conc_seq = ntohl(*(buff_addr + 27)) >> 16;
u_short open_seq = ntohl(*(buff_addr + 28)) >> 16;
	DPRINTK1("mtu = %x class3 = %x conc_seq = %x open_seq = %x", mtu, class3, class3_conc_seq, open_seq);	
	size -= TACHYON_HEADER_LEN;
	if (!(class3 & 0x8000)) {
		DPRINTK1("Received PLOGI with class3 = %x", class3);
		ret_code = (LOGICAL_ERR << 16) | NO_EXPLN;
		return ret_code;
	}
	if (mtu < 256) {
		DPRINTK1("Received PLOGI with MTU set to %x", mtu);
		ret_code = (LOGICAL_ERR << 16) | RECV_FIELD_SIZE;
		return ret_code;
	}
	if (size != PLOGI_LEN) {	
		DPRINTK1("Received PLOGI of size %x", size);
		ret_code = (LOGICAL_ERR << 16) | INV_PAYLOAD_LEN;
		return ret_code;
	}
	if (class3_conc_seq == 0) {	
		DPRINTK1("Received PLOGI with conc_seq == 0");
		ret_code = (LOGICAL_ERR << 16) | CONC_SEQ;
		return ret_code;
	}
	if (open_seq == 0) {	
		DPRINTK1("Received PLOGI with open_seq == 0");
		ret_code = (LOGICAL_ERR << 16) | NO_EXPLN;
		return ret_code;
	}

	/* Could potentially check for more fields, but might end up
	   not talking to most of the devices. ;-) */
	/* Things that could get checked are:
	   common_features = 0x8800
	   total_concurrent_seq = at least 1
	*/
	return ret_code;
}

static void tx_acc(struct fc_info *fi, u_int d_id, u_short received_ox_id)
{
int int_required = 0;
u_int r_ctl = RCTL_ELS_SCTL;
u_int type  = TYPE_ELS | EXCHANGE_RESPONDER | LAST_SEQUENCE;
int size = sizeof(ACC);
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_acc");
	fi->g.acc.cmnd_code = htonl(ELS_ACC);
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.acc, size);
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, received_ox_id, ELS_ACC);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_acc");
}


static void tx_name_server_req(struct fc_info *fi, u_int req)
{
int int_required = 1, i, size = 0;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
u_int type  = TYPE_FC_SERVICES | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
u_int r_ctl = FC4_DEVICE_DATA | UNSOLICITED_CONTROL;
u_int my_mtu = fi->g.my_mtu, d_id = DIRECTORY_SERVER;
CT_HDR ct_hdr;
	ENTER("tx_name_server_req");
	/* Fill up CT_Header */
	ct_hdr.rev_in_id = htonl(FC_CT_REV);
	ct_hdr.fs_type = DIRECTORY_SERVER_APP;
	ct_hdr.fs_subtype = NAME_SERVICE;
	ct_hdr.options = 0;
	ct_hdr.resv1 = 0;
	ct_hdr.cmnd_resp_code = htons(req >> 16);
	ct_hdr.max_res_size = 0;
	ct_hdr.resv2 = 0;
	ct_hdr.reason_code = 0;
	ct_hdr.expln_code = 0;
	ct_hdr.vendor_unique = 0;
	
	fi->g.type_of_frame = FC_ELS;
	switch(req) {
		case FCS_RFC_4:
			memcpy(&(fi->g.rfc_4.ct_hdr), &ct_hdr, sizeof(CT_HDR));
			fi->g.rfc_4.s_id = htonl(fi->g.my_id);
			for (i = 0; i < 32; i++)
				fi->g.rfc_4.bit_map[i] = 0;
			/* We support IP & SCSI */
			fi->g.rfc_4.bit_map[2] = 0x01;
			fi->g.rfc_4.bit_map[3] = 0x20;
			size = sizeof(RFC_4);
			memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.rfc_4, size);
			tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, ox_id, req);
			break;
		case FCS_GP_ID4:
			memcpy(&(fi->g.gp_id4.ct_hdr), &ct_hdr, sizeof(CT_HDR));
			fi->g.gp_id4.port_type = htonl(PORT_TYPE_NX_PORTS);
			size = sizeof(GP_ID4);
			memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.gp_id4, size);
			tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, ox_id, req);
			break;
	}
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_name_server_req");
}

static void tx_scr(struct fc_info *fi)
{
int int_required = 1, size = sizeof(SCR);
u_short ox_id = OX_ID_FIRST_SEQUENCE;
u_int type  = TYPE_ELS | SEQUENCE_INITIATIVE | FIRST_SEQUENCE;
u_int r_ctl = RCTL_ELS_UCTL;
u_int my_mtu = fi->g.my_mtu, d_id = FABRIC_CONTROLLER;
	ENTER("tx_scr");
	fi->g.scr.cmnd_code = htonl(ELS_SCR);
	fi->g.scr.reg_function = htonl(FULL_REGISTRATION);
	fi->g.type_of_frame = FC_ELS;
	memcpy(fi->g.els_buffer[fi->g.e_i], &fi->g.scr, size);
	tx_exchange(fi, (char *)(fi->g.els_buffer[fi->g.e_i]),size, r_ctl, type, d_id, my_mtu, int_required, ox_id, ELS_SCR);
	fi->g.e_i++;
	if (fi->g.e_i == MAX_PENDING_FRAMES)
		fi->g.e_i = 0;
	LEAVE("tx_scr");
}

static void perform_adisc(struct fc_info *fi)
{
int count = 0;
	/* Will be set to TRUE when timer expires in a PLDA environment. 
	 */
	fi->g.port_discovery = FALSE;

	if (fi->node_info_list) {
		struct fc_node_info *temp_list = fi->node_info_list;
		while(temp_list) {
			/* Tx ADISC to all non-fabric based 
	 	 	 * entities.
	 	 	 */
			if ((temp_list->d_id & 0xFF0000) != 0xFF0000)
				tx_adisc(fi, ELS_ADISC, temp_list->d_id, OX_ID_FIRST_SEQUENCE);
			temp_list = temp_list->next;
			udelay(20);
			count++;
		}
	}
	/* Perform Port Discovery after timer expires.
	 * We are giving time for the ADISCed nodes to respond
	 * so that we dont have to perform PLOGI to those whose
	 * login are _still_ valid.
	 */
	fi->explore_timer.function = port_discovery_timer;
	fi->explore_timer.data = (unsigned long)fi;
	fi->explore_timer.expires = RUN_AT((count*3*HZ)/100); 
	init_timer(&fi->explore_timer);
	add_timer(&fi->explore_timer);
}

static void explore_fabric(struct fc_info *fi, u_int *buff_addr)
{
u_int *addr = buff_addr + 12; /* index into payload */
u_char control_code;
u_int d_id;
int count = 0;
	ENTER("explore_fabric");
	DPRINTK1("entering explore_fabric");

	/*fi->g.perform_adisc = TRUE;
	fi->g.explore_fabric = TRUE;
	perform_adisc(fi);*/
	
	do {
		d_id = ntohl(*addr) & 0x00FFFFFF;
		if (d_id != fi->g.my_id) {
			if (sid_logged_in(fi, d_id) == NODE_NOT_PRESENT)
				tx_logi(fi, ELS_PLOGI, d_id); 
			else
			if (sid_logged_in(fi, d_id) == NODE_LOGGED_OUT)
				tx_adisc(fi, ELS_ADISC, d_id, OX_ID_FIRST_SEQUENCE); 
			count++;
		}
		control_code = (ntohl(*addr) & 0xFF000000) >> 24;
		addr++;
		DPRINTK1("cc = %x, d_id = %x", control_code, d_id);
	} while (control_code != 0x80);
	
	fi->explore_timer.function = fabric_explore_timer;
	fi->explore_timer.data = (unsigned long)fi;
	/* We give 30 msec for each device to respond and then send out
	 * our SCSI enquiries. 
	 */
	fi->explore_timer.expires = RUN_AT((count*3*HZ)/100); 
	init_timer(&fi->explore_timer);
	add_timer(&fi->explore_timer);

	DPRINTK1("leaving explore_fabric");
	LEAVE("explore_fabric");
}

static void fabric_explore_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info*)data;
	del_timer(&fi->explore_timer);

	if ((fi->g.loop_up == TRUE) && (fi->g.ptp_up == FALSE)) {
		/* Initiate Local Port Discovery on the Local Loop.
		 */
		fi->g.port_discovery = TRUE;
		fi->g.alpa_list_index = 1;
		local_port_discovery(fi);
	}
	fi->g.explore_fabric = FALSE;
	return;
}

static void port_discovery_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info*)data;
	del_timer(&fi->explore_timer);
	
	if ((fi->g.loop_up == TRUE) && (fi->g.explore_fabric != TRUE)) {
		fi->g.port_discovery = TRUE;
		fi->g.alpa_list_index = 1;
		local_port_discovery(fi);
	}
	fi->g.perform_adisc = FALSE;
	return;
}

static void add_to_ox_id_list(struct fc_info *fi, u_int transaction_id, u_int cmnd_code)
{
struct ox_id_els_map *p, *q = fi->ox_id_list, *r = NULL;
int size = sizeof(struct ox_id_els_map);
	while (q != NULL) {
		r = q;
		q = q->next;
	}
	p = (struct ox_id_els_map *)kmalloc(size, GFP_ATOMIC);
	if (p == NULL) {
		T_MSG("kmalloc failed in add_to_ox_id_list()");
		return;
	}
	p->ox_id = transaction_id;
	p->els = cmnd_code;
	p->next = NULL;
	if (fi->ox_id_list == NULL)
		fi->ox_id_list = p;
	else
		r->next = p;
	return;
}

static u_int remove_from_ox_id_list(struct fc_info *fi, u_short received_ox_id)
{
struct ox_id_els_map *p = fi->ox_id_list, *q = fi->ox_id_list;
u_int els_type;
	while (q != NULL) {
		if (q->ox_id == received_ox_id) {

			if (q == fi->ox_id_list) 
				fi->ox_id_list = fi->ox_id_list->next;
			else
				if (q->next == NULL) 
					p->next = NULL;
			else 
					p->next = q->next;

			els_type = q->els;
			kfree(q);
			return els_type;
		}
		p = q;
		q = q->next;
	}
	if (q == NULL)
		DPRINTK2("Could not find ox_id %x in ox_id_els_map", received_ox_id);
	return 0;
}

static void build_tachyon_header(struct fc_info *fi, u_int my_id, u_int r_ctl, u_int d_id, u_int type, u_char seq_id, u_char df_ctl, u_short ox_id, u_short rx_id, char *data)
{
u_char alpa = d_id & 0x0000FF;
u_int dest_ddaa = d_id &0xFFFF00;

	ENTER("build_tachyon_header");
	DPRINTK("d_id = %x, my_ddaa = %x", d_id, fi->g.my_ddaa);
	/* Does it have to go to/thru a Fabric? */
	if ((dest_ddaa != 0) && ((d_id == F_PORT) || (fi->g.fabric_present && (dest_ddaa != fi->g.my_ddaa))))
		alpa = 0x00;
	fi->g.tach_header.resv = 0x00000000;
	fi->g.tach_header.sof_and_eof = SOFI3 | EOFN;
	fi->g.tach_header.dest_alpa = alpa;
	/* Set LCr properly to have enuff credit */
	if (alpa == REPLICATE)
		fi->g.tach_header.lcr_and_time_stamp = htons(0xC00);/* LCr=3 */
	else
		fi->g.tach_header.lcr_and_time_stamp = 0;
	fi->g.tach_header.r_ctl_and_d_id = htonl(r_ctl | d_id);
	fi->g.tach_header.vc_id_and_s_id = htonl(my_id);
	fi->g.tach_header.type_and_f_cntl = htonl(type);
	fi->g.tach_header.seq_id = seq_id;
	fi->g.tach_header.df_cntl = df_ctl;
	fi->g.tach_header.seq_cnt = 0;
	fi->g.tach_header.ox_id = htons(ox_id);
	fi->g.tach_header.rx_id = htons(rx_id);
	fi->g.tach_header.ro = 0;
	if (data) {
		/* We use the Seq_Count to keep track of IP frames in the
		 * OCI_interrupt handler. Initial Seq_Count of IP frames is 1.
		 */
		if (fi->g.type_of_frame == FC_BROADCAST)
			fi->g.tach_header.seq_cnt = htons(0x1);
		else
			fi->g.tach_header.seq_cnt = htons(0x2);
		fi->g.tach_header.nw_header.d_naa = htons(0x1000);
		fi->g.tach_header.nw_header.s_naa = htons(0x1000);
		memcpy(&(fi->g.tach_header.nw_header.dest_high), data, 2);
		memcpy(&(fi->g.tach_header.nw_header.dest_low), data + 2, 4);
		memcpy(&(fi->g.tach_header.nw_header.source_high), data + 6, 2);
		memcpy(&(fi->g.tach_header.nw_header.source_low), data + 8, 4);
	}
	LEAVE("build_tachyon_header");
}

static void build_EDB(struct fc_info *fi, char *data, u_short flags, u_short len)
{
	fi->g.edb.buf_addr = ntohl((u_int)virt_to_bus(data));
	fi->g.edb.ehf = ntohs(flags);
	if (len % 4)
		len += (4 - (len % 4));
	fi->g.edb.buf_len = ntohs(len);
}

static void build_ODB(struct fc_info *fi, u_char seq_id, u_int d_id, u_int len, u_int cntl, u_short mtu, u_short ox_id, u_short rx_id, int NW_header, int int_required, u_int frame_class)
{
	fi->g.odb.seq_d_id = htonl(seq_id << 24 | d_id);
	fi->g.odb.tot_len = len;
	if (NW_header)
		fi->g.odb.tot_len += NW_HEADER_LEN;
	if (fi->g.odb.tot_len % 4)
		fi->g.odb.tot_len += (4 - (fi->g.odb.tot_len % 4));
	fi->g.odb.tot_len = htonl(fi->g.odb.tot_len);
	switch(int_required) {
		case NO_COMP_AND_INT:
			fi->g.odb.cntl = htons(ODB_CLASS_3 | ODB_EE_CREDIT | ODB_NO_INT | ODB_NO_COMP | cntl);
			break;
		case INT_AND_COMP_REQ:
			fi->g.odb.cntl = htons(ODB_CLASS_3 | ODB_EE_CREDIT | cntl);
			break;
		case NO_INT_COMP_REQ:
			fi->g.odb.cntl = htons(ODB_CLASS_3 | ODB_EE_CREDIT | ODB_NO_INT | cntl);
			break;
	}
	fi->g.odb.rx_id = htons(rx_id);
	fi->g.odb.cs_enable = 0;
	fi->g.odb.cs_seed = htons(1);

	fi->g.odb.hdr_addr = htonl(virt_to_bus(fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx]));
	fi->g.odb.frame_len = htons(mtu);

	if (NW_header) {
		/* The pointer to the sk_buff is in here. Freed up when the
		 * OCI_interrupt is received.
		 */
		fi->g.odb.trans_id = htonl(frame_class);
		fi->g.odb.hdr_len = TACHYON_HEADER_LEN + NW_HEADER_LEN;
	}
	else {
		/* helps in tracking transmitted OX_IDs */
		fi->g.odb.trans_id = htonl((frame_class & 0xFFFF0000) | ox_id);
		fi->g.odb.hdr_len = TACHYON_HEADER_LEN;
	}
	fi->g.odb.hdr_len = htons(fi->g.odb.hdr_len);
		
	fi->g.odb.edb_addr = htonl(virt_to_bus(fi->q.ptr_edb[fi->q.edb_buffer_indx]));
}

static void fill_login_frame(struct fc_info *fi, u_int logi)
{
int i;
	fi->g.login.ls_cmnd_code= htonl(logi);
	fi->g.login.fc_ph_version = htons(PH_VERSION);
	if (fi->g.loop_up)
		fi->g.login.buff_to_buff_credit = htons(LOOP_BB_CREDIT);
	else
	if (fi->g.ptp_up)
		fi->g.login.buff_to_buff_credit = htons(PT2PT_BB_CREDIT);
	if ((logi != ELS_FLOGI) || (logi == ELS_ACC))
		fi->g.login.common_features = htons(PLOGI_C_F);
	else
	if (logi == ELS_FLOGI)
		fi->g.login.common_features = htons(FLOGI_C_F);
	fi->g.login.recv_data_field_size = htons(FRAME_SIZE);
	fi->g.login.n_port_total_conc_seq = htons(CONCURRENT_SEQUENCES);
	fi->g.login.rel_off_by_info_cat = htons(RO_INFO_CATEGORY);
	fi->g.login.ED_TOV = htonl(E_D_TOV);
	fi->g.login.n_port_name_high = htonl(N_PORT_NAME_HIGH);
	fi->g.login.n_port_name_low = htonl(N_PORT_NAME_LOW);
	fi->g.login.node_name_high = htonl(NODE_NAME_HIGH);
	fi->g.login.node_name_low = htonl(NODE_NAME_LOW);
	
	/* Fill Class 1 parameters */
	fi->g.login.c_of_s[0].service_options = htons(0);
	fi->g.login.c_of_s[0].initiator_ctl = htons(0);
	fi->g.login.c_of_s[0].recipient_ctl = htons(0);
	fi->g.login.c_of_s[0].recv_data_field_size = htons(0);
	fi->g.login.c_of_s[0].concurrent_sequences = htons(0);
	fi->g.login.c_of_s[0].n_port_end_to_end_credit = htons(0);
	fi->g.login.c_of_s[0].open_seq_per_exchange = htons(0);
	fi->g.login.c_of_s[0].resv = htons(0);

	/* Fill Class 2 parameters */
	fi->g.login.c_of_s[1].service_options = htons(0);
	fi->g.login.c_of_s[1].initiator_ctl = htons(0);
	fi->g.login.c_of_s[1].recipient_ctl = htons(0);
	fi->g.login.c_of_s[1].recv_data_field_size = htons(0);
	fi->g.login.c_of_s[1].concurrent_sequences = htons(0);
	fi->g.login.c_of_s[1].n_port_end_to_end_credit = htons(0);
	fi->g.login.c_of_s[1].open_seq_per_exchange = htons(0);
	fi->g.login.c_of_s[1].resv = htons(0);

	/* Fill Class 3 parameters */
	if (logi == ELS_FLOGI)
		fi->g.login.c_of_s[2].service_options  = htons(SERVICE_VALID | SEQUENCE_DELIVERY);
	else
		fi->g.login.c_of_s[2].service_options  = htons(SERVICE_VALID);
	fi->g.login.c_of_s[2].initiator_ctl = htons(0);
	fi->g.login.c_of_s[2].recipient_ctl = htons(0);
	fi->g.login.c_of_s[2].recv_data_field_size = htons(FRAME_SIZE);
	fi->g.login.c_of_s[2].concurrent_sequences = htons(CLASS3_CONCURRENT_SEQUENCE);
	fi->g.login.c_of_s[2].n_port_end_to_end_credit = htons(0);
	fi->g.login.c_of_s[2].open_seq_per_exchange = htons(CLASS3_OPEN_SEQUENCE);
	fi->g.login.c_of_s[2].resv = htons(0);
	
	for(i = 0; i < 4; i++) {
		fi->g.login.resv[i] = 0;
		fi->g.login.vendor_version_level[i] = 0;
	}
}


/* clear the Interrupt Latch on the (i)chip, so that you can receive 
 * Interrupts from Tachyon in future 
 */
static void reset_latch(struct fc_info *fi)
{
	writel(readl(fi->i_r.ptr_ichip_hw_status_reg) | ICHIP_HSR_INT_LATCH, fi->i_r.ptr_ichip_hw_status_reg);
}

static void update_OCQ_indx(struct fc_info *fi)
{
	fi->q.ocq_prod_indx++;
	if (fi->q.ocq_prod_indx == OCQ_LENGTH)
		fi->q.ocq_prod_indx = 0;
	writel(fi->q.ocq_prod_indx, fi->t_r.ptr_ocq_prod_indx_reg);
}

static void update_IMQ_indx(struct fc_info *fi, int count)
{
	fi->q.imq_cons_indx += count;
	if (fi->q.imq_cons_indx >= IMQ_LENGTH)
		fi->q.imq_cons_indx -= IMQ_LENGTH;
	writel(fi->q.imq_cons_indx, fi->t_r.ptr_imq_cons_indx_reg);
}

static void update_SFSBQ_indx(struct fc_info *fi)
{
	fi->q.sfsbq_prod_indx++;
	if (fi->q.sfsbq_prod_indx == SFSBQ_LENGTH)
		fi->q.sfsbq_prod_indx = 0;
	writel(fi->q.sfsbq_prod_indx, fi->t_r.ptr_sfsbq_prod_reg);
}

static void update_MFSBQ_indx(struct fc_info *fi, int count)
{
	fi->q.mfsbq_prod_indx += count;
	if (fi->q.mfsbq_prod_indx >= MFSBQ_LENGTH)
		fi->q.mfsbq_prod_indx -= MFSBQ_LENGTH;
	writel(fi->q.mfsbq_prod_indx, fi->t_r.ptr_mfsbq_prod_reg);
}


static void update_tachyon_header_indx(struct fc_info *fi)
{
	fi->q.tachyon_header_indx++;
	if (fi->q.tachyon_header_indx == NO_OF_TACH_HEADERS)
		fi->q.tachyon_header_indx = 0;
}

static void update_EDB_indx(struct fc_info *fi)
{
	fi->q.edb_buffer_indx++;
	if (fi->q.edb_buffer_indx == EDB_LEN)
		fi->q.edb_buffer_indx = 0;
}

static int iph5526_open(struct net_device *dev)
{
	dev->tbusy = 0;
	dev->interrupt = 0;
	dev->start = 1;
	MOD_INC_USE_COUNT;
	return 0;
}

static int iph5526_close(struct net_device *dev)
{
	dev->tbusy = 1;
	dev->start = 0;
	MOD_DEC_USE_COUNT;
	return 0;
}

static int iph5526_send_packet(struct sk_buff *skb, struct net_device *dev)
{
struct fc_info *fi = (struct fc_info*)dev->priv;
int status = 0;
short type = 0;
u_long flags;
	ENTER("iph5526_send_packet");
	if (dev->tbusy) {
		printk(KERN_WARNING "%s: DEVICE BUSY\n", dev->name);
		dev->tbusy = 0;
		fi->fc_stats.rx_dropped++;
		dev->trans_start = jiffies;
		return 0;
	}
	if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
		printk(KERN_WARNING "%s: Transmitter access conflict.\n", 
dev->name);
		fi->fc_stats.rx_dropped++;
		return 1;
	}
	else {
		struct fcllc *fcllc;
		/* Strip off the pseudo header.
		 */
		skb->data = skb->data + 2*FC_ALEN; 
		skb->len = skb->len - 2*FC_ALEN;
		fcllc = (struct fcllc *)skb->data;
		type = ntohs(fcllc->ethertype);

		spin_lock_irqsave(&fi->fc_lock, flags);
		switch(type) {
			case ETH_P_IP:
				status = tx_ip_packet(skb, skb->len, fi);
				break;
			case ETH_P_ARP:
				status = tx_arp_packet(skb->data, skb->len, fi);
				break;
			default:
				T_MSG("WARNING!!! Received Unknown Packet Type... Discarding...");
				fi->fc_stats.rx_dropped++;
				break;
		}
		spin_unlock_irqrestore(&fi->fc_lock, flags);
	}

	if (status) {
		fi->fc_stats.tx_bytes += skb->len;
		fi->fc_stats.tx_packets++;
	}
	else
		fi->fc_stats.rx_dropped++;
	dev->trans_start = jiffies;
	dev->tbusy = 0;
	/* We free up the IP buffers in the OCI_interrupt handler.
	 * status == 0 implies that the frame was not transmitted. So the
	 * skb is freed here.
	 */
	if ((type == ETH_P_ARP) || (status == 0))
		dev_kfree_skb(skb);
	mark_bh(NET_BH);
	LEAVE("iph5526_send_packet");
	return 0;
}

static int iph5526_change_mtu(struct net_device *dev, int mtu)
{
	return 0;
}

static int tx_ip_packet(struct sk_buff *skb, unsigned long len, struct fc_info *fi)
{
u_int d_id;
int int_required = 1;
u_int r_ctl = FC4_DEVICE_DATA | UNSOLICITED_DATA;
u_int type = TYPE_LLC_SNAP;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
u_int mtu;
struct fc_node_info *q;

	ENTER("tx_ip_packet");
	q = look_up_cache(fi, skb->data - 2*FC_ALEN);
	if (q != NULL) {
		d_id = q->d_id;
		DPRINTK("Look-Up Cache Succeeded for d_id = %x", d_id);
		mtu = q->mtu;
		if (q->login == LOGIN_COMPLETED){
			fi->g.type_of_frame = FC_IP;
			return tx_exchange(fi, skb->data, len, r_ctl, type, d_id, mtu, int_required, ox_id, virt_to_bus(skb));
		}
		
		if (q->d_id == BROADCAST) {
		struct fc_node_info *p = fi->node_info_list;
		int return_value = FALSE;
			fi->g.type_of_frame = FC_BROADCAST;
			/* Do unicast to local nodes.
			 */
			int_required = 0;
			while(p != NULL) {
				d_id = p->d_id;
				if ((d_id & 0xFFFF00) == fi->g.my_ddaa)
					return_value |= tx_exchange(fi, skb->data, len, r_ctl, type, d_id, fi->g.my_mtu, int_required, ox_id, TYPE_LLC_SNAP);
				p = p->next;
			}
			kfree(q);
			return return_value;
		}
		
		if (q->login != LOGIN_COMPLETED) {	
			DPRINTK1("Node not logged in... Txing PLOGI to %x", d_id);
			/* FIXME: we are dumping the frame here */
			tx_logi(fi, ELS_PLOGI, d_id); 
		}
	}
	DPRINTK2("Look-Up Cache Failed");
	LEAVE("tx_ip_packet");
	return 0;
}

static int tx_arp_packet(char *data, unsigned long len, struct fc_info *fi)
{
u_int opcode = data[ARP_OPCODE_0]; 
u_int d_id;
int int_required = 0, return_value = FALSE;
u_int r_ctl = FC4_DEVICE_DATA | UNSOLICITED_DATA;
u_int type = TYPE_LLC_SNAP;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
u_int my_mtu = fi->g.my_mtu;
	ENTER("tx_arp_packet");

	opcode = opcode << 8 | data[ARP_OPCODE_1];
	fi->g.type_of_frame = FC_IP;

	if (opcode == ARPOP_REQUEST) {
	struct fc_node_info *q = fi->node_info_list;
		d_id = BROADCAST;
		return_value |= tx_exchange(fi, data, len, r_ctl, type, d_id, my_mtu, int_required, ox_id, TYPE_LLC_SNAP);
		/* Some devices support HW_TYPE 0x01 */
		memcpy(fi->g.arp_buffer, data - 2*FC_ALEN, len + 2*FC_ALEN);
		fi->g.arp_buffer[9 + 2*FC_ALEN] = 0x01;
		return_value |= tx_exchange(fi, (char *)(fi->g.arp_buffer + 2*FC_ALEN), len, r_ctl, type, d_id, my_mtu, int_required, ox_id, TYPE_LLC_SNAP);

		/* Do unicast to local nodes.
		 */
		while(q != NULL) {
			fi->g.type_of_frame = FC_BROADCAST;
			d_id = q->d_id;
			if ((d_id & 0xFFFF00) == fi->g.my_ddaa) {
				return_value |= tx_exchange(fi, data, len, r_ctl, type, d_id, my_mtu, int_required, ox_id, TYPE_LLC_SNAP);
				// Some devices support HW_TYPE 0x01
				memcpy(fi->g.arp_buffer, data - 2*FC_ALEN, len + 2*FC_ALEN);
				fi->g.arp_buffer[9 + 2*FC_ALEN] = 0x01;
				return_value |= tx_exchange(fi, (char *)(fi->g.arp_buffer + 2*FC_ALEN), len, r_ctl, type, d_id, my_mtu, int_required, ox_id, TYPE_LLC_SNAP);
			}
			q = q->next;
		}
		return return_value;
	}
	else
	if (opcode == ARPOP_REPLY) {
	struct fc_node_info *q; u_int mtu;
		DPRINTK("We are sending out an ARP reply");
		q = look_up_cache(fi, data - 2*FC_ALEN);
		if (q != NULL) {
			d_id = q->d_id;
			DPRINTK("Look-Up Cache Succeeded for d_id = %x", d_id);
			mtu = q->mtu;
			if (q->login == LOGIN_COMPLETED){
				tx_exchange(fi, data, len, r_ctl, type, d_id, mtu, int_required, ox_id, TYPE_LLC_SNAP);
				/* Some devices support HW_TYPE 0x01 */
				memcpy(fi->g.arp_buffer, data - 2*FC_ALEN, len + 2*FC_ALEN);
				fi->g.arp_buffer[9 + 2*FC_ALEN] = 0x01;
				return tx_exchange(fi, (char *)(fi->g.arp_buffer + 2*FC_ALEN), len, r_ctl, type, d_id, my_mtu, int_required, ox_id, TYPE_LLC_SNAP);
			}
			else {
				DPRINTK1("Node not logged in... Txing PLOGI to %x", d_id);
				tx_logi(fi, ELS_PLOGI, d_id); /* FIXME: we are dumping the frame here */
			}
		}
		DPRINTK2("Look-Up Cache Failed");
	}
	else {
		T_MSG("Warning!!! Invalid Opcode in ARP Packet!");
	}
	LEAVE("tx_arp_packet");
	return 0;
}


static void rx_net_packet(struct fc_info *fi, u_char *buff_addr, int payload_size)
{
struct net_device *dev = fi->dev;
struct sk_buff *skb;
u_int skb_size = 0;
struct fch_hdr fch;
	ENTER("rx_net_packet");
	skb_size = payload_size - TACHYON_HEADER_LEN;
	DPRINTK("skb_size = %d", skb_size);
	fi->fc_stats.rx_bytes += skb_size - 2;
	skb = dev_alloc_skb(skb_size);
	if (skb == NULL) {
		printk(KERN_NOTICE "%s: In rx_net_packet() Memory squeeze, dropping packet.\n", dev->name);
		fi->fc_stats.rx_dropped++;
		return;
	}
	/* Skip over the Tachyon Frame Header.
	 */
	buff_addr += TACHYON_HEADER_LEN; 

	memcpy(fch.daddr, buff_addr + 2, FC_ALEN);
	memcpy(fch.saddr, buff_addr + 10, FC_ALEN);
	buff_addr += 2;
	memcpy(buff_addr, fch.daddr, FC_ALEN);
	memcpy(buff_addr + 6, fch.saddr, FC_ALEN);
	skb_reserve(skb, 2);
	memcpy(skb_put(skb, skb_size - 2), buff_addr, skb_size - 2);
	skb->dev = dev;
	skb->protocol = fc_type_trans(skb, dev);
	DPRINTK("protocol = %x", skb->protocol);
	
	/* Hmmm... to accept HW Type 0x01 as well... 
	 */
	if (skb->protocol == ntohs(ETH_P_ARP))
		skb->data[1] = 0x06;
	netif_rx(skb);
	fi->fc_stats.rx_packets++;
	LEAVE("rx_net_packet");
}


static void rx_net_mfs_packet(struct fc_info *fi, struct sk_buff *skb)
{
struct net_device *dev = fi->dev;
struct fch_hdr fch;
	ENTER("rx_net_mfs_packet");
	/* Construct your Hard Header */
	memcpy(fch.daddr, skb->data + 2, FC_ALEN);
	memcpy(fch.saddr, skb->data + 10, FC_ALEN);
	skb_pull(skb, 2);
	memcpy(skb->data, fch.daddr, FC_ALEN);
	memcpy(skb->data + 6, fch.saddr, FC_ALEN);
	skb->dev = dev;
	skb->protocol = fc_type_trans(skb, dev);
	DPRINTK("protocol = %x", skb->protocol);
	netif_rx(skb);
	LEAVE("rx_net_mfs_packet");
}

unsigned short fc_type_trans(struct sk_buff *skb, struct net_device *dev) 
{
struct fch_hdr *fch=(struct fch_hdr *)skb->data;
struct fcllc *fcllc;
	skb->mac.raw = skb->data;
	fcllc = (struct fcllc *)(skb->data + sizeof(struct fch_hdr) + 2);
	skb_pull(skb,sizeof(struct fch_hdr) + 2);

	if(*fch->daddr & 1) {
		if(!memcmp(fch->daddr,dev->broadcast,FC_ALEN)) 	
			skb->pkt_type = PACKET_BROADCAST;
		else
			skb->pkt_type = PACKET_MULTICAST;
	}
	else if(dev->flags & IFF_PROMISC) {
		if(memcmp(fch->daddr, dev->dev_addr, FC_ALEN))
			skb->pkt_type=PACKET_OTHERHOST;
	}
	
	/* Strip the SNAP header from ARP packets since we don't 
	 * pass them through to the 802.2/SNAP layers.
	 */

	if (fcllc->dsap == EXTENDED_SAP &&
		(fcllc->ethertype == ntohs(ETH_P_IP) ||
		 fcllc->ethertype == ntohs(ETH_P_ARP))) {
		skb_pull(skb, sizeof(struct fcllc));
		return fcllc->ethertype;
	}
	return ntohs(ETH_P_802_2);
}

static int tx_exchange(struct fc_info *fi, char *data, u_int len, u_int r_ctl, u_int type, u_int d_id, u_int mtu, int int_required, u_short tx_ox_id, u_int frame_class)
{
u_char df_ctl; 
int NW_flag = 0, h_size, return_value;
u_short rx_id = RX_ID_FIRST_SEQUENCE;
u_int tachyon_status;
u_int my_id = fi->g.my_id;
	ENTER("tx_exchange");

	tachyon_status = readl(fi->t_r.ptr_tach_status_reg);
	DPRINTK("Tachyon Status = %x len = %d MTU = %d", tachyon_status, len, mtu);
	if (tachyon_status & OSM_FROZEN) {
		reset_tachyon(fi, ERROR_RELEASE);
		reset_tachyon(fi, OCQ_RESET);
		DPRINTK("Tachyon Status = %x len = %d MTU = %d", tachyon_status, len, mtu);
	}
	if (tx_ox_id == OX_ID_FIRST_SEQUENCE) {
		switch(fi->g.type_of_frame) {
			case FC_SCSI_READ:
				tx_ox_id = fi->g.scsi_oxid | SCSI_READ_BIT;
				break;
			case FC_SCSI_WRITE:
				tx_ox_id = fi->g.scsi_oxid;
				break;
			default:
				tx_ox_id = fi->g.ox_id;
				break;
		}
	}
	else {
		switch(fi->g.type_of_frame) {
			case FC_SCSI_READ:
				rx_id = fi->g.scsi_oxid | SCSI_READ_BIT;
				break;
			case FC_SCSI_WRITE:
				rx_id = fi->g.scsi_oxid;
				break;
			case FC_BLS:
				rx_id = RX_ID_FIRST_SEQUENCE;
				break;
			default:
				rx_id = fi->g.ox_id;
				break;
		}
	}

	if (type == TYPE_LLC_SNAP) {
		df_ctl = 0x20;
		NW_flag = 1;
		/* Multi Frame Sequence ? If yes, set RO bit */
		if (len > mtu)
			type |= RELATIVE_OFF_PRESENT;
		build_tachyon_header(fi, my_id, r_ctl, d_id, type, fi->g.seq_id, df_ctl, tx_ox_id, rx_id, data - 2*FC_ALEN);
	}
	else {
		df_ctl = 0;
		/* Multi Frame Sequence ? If yes, set RO bit */
		if (len > mtu)
			type |= RELATIVE_OFF_PRESENT;
		build_tachyon_header(fi, my_id, r_ctl, d_id, type, fi->g.seq_id, df_ctl, tx_ox_id, rx_id, NULL);
	}

	/* Get free Tachyon Headers and EDBs */
	if (get_free_header(fi) || get_free_EDB(fi))
		return 0;

	if ((type & 0xFF000000) == TYPE_LLC_SNAP) {
		h_size =  TACHYON_HEADER_LEN + NW_HEADER_LEN;
		memcpy(fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx], &(fi->g.tach_header), h_size);
	}
	else 
		memcpy(fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx], &(fi->g.tach_header), TACHYON_HEADER_LEN);

	return_value = tx_sequence(fi, data, len, mtu, d_id, tx_ox_id, rx_id, fi->g.seq_id, NW_flag, int_required, frame_class);
	
	switch(fi->g.type_of_frame) {
		case FC_SCSI_READ:
		case FC_SCSI_WRITE:
			update_scsi_oxid(fi);
			break;
		case FC_BLS:
			break;
		default:
			fi->g.ox_id++;
			if (fi->g.ox_id == 0xFFFF)
				fi->g.ox_id = NOT_SCSI_XID;
			break;
	}

	if (fi->g.seq_id == MAX_SEQ_ID)
		fi->g.seq_id = 0;
	else
		fi->g.seq_id++;
	LEAVE("tx_exchange");
	return return_value;
}

static int tx_sequence(struct fc_info *fi, char *data, u_int len, u_int mtu, u_int d_id, u_short ox_id, u_short rx_id, u_char seq_id, int NW_flag, int int_required, u_int frame_class)
{
u_int cntl = 0;
int return_value;
	ENTER("tx_sequence");
	build_EDB(fi, data, EDB_END, len);
	memcpy(fi->q.ptr_edb[fi->q.edb_buffer_indx], &(fi->g.edb), sizeof(EDB));
	build_ODB(fi, seq_id, d_id, len, cntl, mtu, ox_id, rx_id, NW_flag, int_required, frame_class);
	memcpy(fi->q.ptr_odb[fi->q.ocq_prod_indx], &(fi->g.odb), sizeof(ODB));
	if (fi->g.link_up != TRUE) {
		DPRINTK2("Fibre Channel Link not up. Dropping Exchange!");
		return_value = FALSE;
	}
	else {
		/* To be on the safe side, a check should be included
		 * at this point to check if we are overrunning 
		 * Tachyon.
		 */
		update_OCQ_indx(fi);
		return_value = TRUE;
	}
	update_EDB_indx(fi);
	update_tachyon_header_indx(fi);
	LEAVE("tx_sequence");
	return return_value;
}

static int get_free_header(struct fc_info *fi)
{
u_short temp_ox_id;
u_int *tach_header, initial_indx = fi->q.tachyon_header_indx;
	/* Check if the header is in use.
	 * We could have an outstanding command.
	 * We should find a free slot as we can queue a
	 * maximum of 32 SCSI commands only. 
	 */
	tach_header = fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx];
	temp_ox_id = ntohl(*(tach_header + 6)) >> 16;
	/* We care about the SCSI writes only. Those are the wicked ones
	 * that need an additional set of buffers.
	 */
	while(temp_ox_id <= MAX_SCSI_XID) {
		update_tachyon_header_indx(fi);
		if (fi->q.tachyon_header_indx == initial_indx) {
			/* Should never happen.
			 */
			T_MSG("No free Tachyon headers available");
			reset_tachyon(fi, SOFTWARE_RESET);
			return 1;
		}
		tach_header = fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx];
		temp_ox_id = ntohl(*(tach_header + 6)) >> 16;
	}
	return 0;
}

static int get_free_EDB(struct fc_info *fi)
{
unsigned int initial_indx = fi->q.edb_buffer_indx;
	/* Check if the EDB is in use.
	 * We could have an outstanding SCSI Write command.
	 * We should find a free slot as we can queue a
	 * maximum of 32 SCSI commands only. 
	 */
	while (fi->q.free_edb_list[fi->q.edb_buffer_indx] != EDB_FREE) {
		update_EDB_indx(fi);
		if (fi->q.edb_buffer_indx == initial_indx) {
			T_MSG("No free EDB buffers avaliable")
			reset_tachyon(fi, SOFTWARE_RESET);
			return 1;
		}
	}
	return 0;
}		

static int validate_login(struct fc_info *fi, u_int *base_ptr)
{
struct fc_node_info *q = fi->node_info_list;
char n_port_name[PORT_NAME_LEN];
char node_name[NODE_NAME_LEN];
u_int s_id;
	ENTER("validate_login");
	/*index to Port Name in the payload. We need the 8 byte Port Name */
	memcpy(n_port_name, base_ptr + 10, PORT_NAME_LEN);
	memcpy(node_name, base_ptr + 12, NODE_NAME_LEN);
	s_id = ntohl(*(base_ptr + 3)) & 0x00FFFFFF;
	
	/* check if Fibre Channel IDs have changed */
	while(q != NULL) {	
		if (memcmp(n_port_name, q->hw_addr, PORT_NAME_LEN) == 0) {
			if ((s_id != q->d_id) || (memcmp(node_name, q->node_name, NODE_NAME_LEN) != 0)) {
				DPRINTK1("Fibre Channel ID of Node has changed. Txing LOGO.");
				return 0;
			}
			q->login = LOGIN_COMPLETED;
#if DEBUG_5526_2
			display_cache(fi);
#endif
			return 1;
		}
		q = q->next;
	}
	DPRINTK1("Port Name does not match. Txing LOGO.");
	return 0;
	LEAVE("validate_login");
}

static void add_to_address_cache(struct fc_info *fi, u_int *base_ptr)
{
int size = sizeof(struct fc_node_info);
struct fc_node_info *p, *q = fi->node_info_list, *r = NULL;
char n_port_name[PORT_NAME_LEN];
u_int s_id;
	ENTER("add_to_address_cache");
	/*index to Port Name in the payload. We need the 8 byte Port Name */
	memcpy(n_port_name, base_ptr + 13, PORT_NAME_LEN);
	s_id = ntohl(*(base_ptr + 3)) & 0x00FFFFFF;
	
	/* check if info already exists */
	while(q != NULL) {	
		if (memcmp(n_port_name, q->hw_addr, PORT_NAME_LEN) == 0) {
			if (s_id != q->d_id) {
				memcpy(&(q->c_of_s[0]), base_ptr + 17, 3 * sizeof(CLASS_OF_SERVICE));
				q->mtu = ntohl(*(base_ptr + 10)) & 0x00000FFF;
				q->d_id = s_id;
				memcpy(q->node_name, base_ptr + 15, NODE_NAME_LEN);
			}
			q->login = LOGIN_COMPLETED;
			q->scsi = FALSE;
			fi->num_nodes++;
#if DEBUG_5526_2
			display_cache(fi);
#endif
			return;
		}
		r = q;
		q = q->next;
	}
	p = (struct fc_node_info *)kmalloc(size, GFP_ATOMIC);
	if (p == NULL) {
		T_MSG("kmalloc failed in add_to_address_cache()");
		return;
	}
	memcpy(&(p->c_of_s[0]), base_ptr + 17, 3 * sizeof(CLASS_OF_SERVICE));
	p->mtu = ntohl(*(base_ptr + 10)) & 0x00000FFF;
	p->d_id = s_id;
	memcpy(p->hw_addr, base_ptr + 13, PORT_NAME_LEN);
	memcpy(p->node_name, base_ptr + 15, NODE_NAME_LEN);
	p->login = LOGIN_COMPLETED;
	p->scsi = FALSE;
	p->target_id = 0xFF;
	p->next = NULL;
	if (fi->node_info_list == NULL)
		fi->node_info_list = p;
	else
		r->next = p;
	fi->num_nodes++;
#if DEBUG_5526_2
	display_cache(fi);
#endif
	LEAVE("add_to_address_cache");
	return;
}

static void remove_from_address_cache(struct fc_info *fi, u_int *base_ptr, u_int cmnd_code)
{
struct fc_node_info *q = fi->node_info_list;
u_int s_id;
	ENTER("remove_from_address_cache");
	s_id = ntohl(*(base_ptr + 3)) & 0x00FFFFFF;
	switch(cmnd_code) {
		case ELS_LOGO:
			/* check if info exists */
			while (q != NULL) {
				if (s_id == q->d_id) {
					if (q->login == LOGIN_COMPLETED)
						q->login = LOGIN_ATTEMPTED;
					if (fi->num_nodes > 0)
						fi->num_nodes--;
#if DEBUG_5526_2
					display_cache(fi);
#endif
					return;
				}
				q = q->next;
			}
			DPRINTK1("ELS_LOGO received from node 0x%x which is not logged-in", s_id);
			break;
		case ELS_RSCN:
		{
		int payload_len = ntohl(*(base_ptr + 8)) & 0xFF;
		int no_of_pages, i;
		u_char address_format;
		u_short received_ox_id = ntohl(*(base_ptr + 6)) >> 16;
		u_int node_id, mask, *page_ptr = base_ptr + 9;
			if ((payload_len < 4) || (payload_len > 256)) {
				DPRINTK1("RSCN with invalid payload length received");
				tx_ls_rjt(fi, s_id, received_ox_id, LOGICAL_ERR, RECV_FIELD_SIZE);
				return;
			}
			/* Page_size includes the Command Code */
			no_of_pages = (payload_len / 4) - 1;
			for (i = 0; i < no_of_pages; i++) {
				address_format = ntohl(*page_ptr) >> 24; 
				node_id = ntohl(*page_ptr) & 0x00FFFFFF;
				switch(address_format) {
					case PORT_ADDRESS_FORMAT:
						rscn_handler(fi, node_id);
						break;
					case AREA_ADDRESS_FORMAT:
					case DOMAIN_ADDRESS_FORMAT:
						if (address_format == AREA_ADDRESS_FORMAT)
							mask = 0xFFFF00;
						else
							mask = 0xFF0000;
						while(q != NULL) {
							if ((q->d_id & mask) == (node_id & mask)) 
								rscn_handler(fi, q->d_id);
							q = q->next;
						}
						/* There might be some new nodes to be 
						 * discovered. But, some of the earlier 
						 * requests as a result of the RSCN might be 
						 * in progress. We dont want to duplicate that 
						 * effort. So letz call SCR after a lag.
						 */
						fi->explore_timer.function = scr_timer;
						fi->explore_timer.data = (unsigned long)fi;
						fi->explore_timer.expires = RUN_AT((no_of_pages*3*HZ)/100); 
						init_timer(&fi->explore_timer);
						add_timer(&fi->explore_timer);
						break;
					default:
						T_MSG("RSCN with invalid address format received");
						tx_ls_rjt(fi, s_id, received_ox_id, LOGICAL_ERR, NO_EXPLN);
				}
				page_ptr += 1;
			} /* end of for loop */
		} /* end of case RSCN: */	
		break;
	}
#if DEBUG_5526_2
	display_cache(fi);
#endif
	LEAVE("remove_from_address_cache");
}

static void rscn_handler(struct fc_info *fi, u_int node_id)
{
struct fc_node_info *q = fi->node_info_list;
int login_state = sid_logged_in(fi, node_id);
	if ((login_state == NODE_LOGGED_IN) || (login_state == NODE_PROCESS_LOGGED_IN)) {
		while(q != NULL) {
			if (q->d_id == node_id) {
				q->login = LOGIN_ATTEMPTED;
				if (fi->num_nodes > 0)
					fi->num_nodes--;
				break;
			}
			else
				q = q->next;
		}
	}
	else
	if (login_state == NODE_LOGGED_OUT)
		tx_adisc(fi, ELS_ADISC, node_id, OX_ID_FIRST_SEQUENCE); 
	else
	if (login_state == NODE_LOGGED_OUT)
		tx_logi(fi, ELS_PLOGI, node_id);
}

static void scr_timer(unsigned long data)
{
struct fc_info *fi = (struct fc_info *)data;
	del_timer(&fi->explore_timer);
	tx_name_server_req(fi, FCS_GP_ID4);
}

static int sid_logged_in(struct fc_info *fi, u_int s_id)
{
struct fc_node_info *temp = fi->node_info_list;
	while(temp != NULL)
		if ((temp->d_id == s_id) && (temp->login == LOGIN_COMPLETED)) {
			if (temp->scsi != FALSE)
				return NODE_PROCESS_LOGGED_IN;
			else
				return NODE_LOGGED_IN;
		}
		else
		if ((temp->d_id == s_id) && (temp->login != LOGIN_COMPLETED))
			return NODE_LOGGED_OUT;
		else
			temp = temp->next;
	return NODE_NOT_PRESENT;
}

static void mark_scsi_sid(struct fc_info *fi, u_int *buff_addr, u_char action)
{
struct fc_node_info *temp = fi->node_info_list;
u_int s_id;
u_int service_params;
	s_id = ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	service_params = ntohl(*(buff_addr + 12)) & 0x000000F0;
	while(temp != NULL)
		if ((temp->d_id == s_id) && (temp->login == LOGIN_COMPLETED)) {
			if (action == DELETE_ENTRY) {
				temp->scsi = FALSE;
#if DEBUG_5526_2
				display_cache(fi);
#endif
				return;
			}
			/* Check if it is a SCSI Target */
			if (!(service_params & TARGET_FUNC)) {
				temp->scsi = INITIATOR;	
#if DEBUG_5526_2
				display_cache(fi);
#endif
				return;
			}
			temp->scsi = TARGET;
			/* This helps to maintain the target_id no matter what your
			 *  Fibre Channel ID is.
			 */
			if (temp->target_id == 0xFF) {
				if (fi->g.no_of_targets <= MAX_SCSI_TARGETS)
					temp->target_id = fi->g.no_of_targets++;
				else
					T_MSG("MAX TARGETS reached!");
			}
			else
				DPRINTK1("Target_id %d already present", temp->target_id);
#if DEBUG_5526_2
			display_cache(fi);
#endif
			return;
		}
		else
			temp = temp->next;
	return;
}

static int node_logged_in_prev(struct fc_info *fi, u_int *buff_addr)
{
struct fc_node_info *temp;
u_char *data = (u_char *)buff_addr;
u_int s_id;
char node_name[NODE_NAME_LEN];
	s_id = ntohl(*(buff_addr + 3)) & 0x00FFFFFF;
	memcpy(node_name, buff_addr + 12, NODE_NAME_LEN);
	/* point to port_name in the ADISC payload */
	data += 10 * 4;
	/* point to last 6 bytes of port_name */
	data += 2;
	temp = look_up_cache(fi, data);
	if (temp != NULL) {
		if ((temp->d_id == s_id) && (memcmp(node_name, temp->node_name, NODE_NAME_LEN) == 0)) {
			temp->login = LOGIN_COMPLETED;
#if DEBUG_5526_2
			display_cache(fi);
#endif
			return TRUE;
		}
	}
	return FALSE;
}

static struct fc_node_info *look_up_cache(struct fc_info *fi, char *data)
{
struct fc_node_info *temp_list = fi->node_info_list, *q;
u_char n_port_name[FC_ALEN], temp_addr[FC_ALEN];
	ENTER("look_up_cache");
	memcpy(n_port_name, data, FC_ALEN);
	while(temp_list) {
		if (memcmp(n_port_name, &(temp_list->hw_addr[2]), FC_ALEN) == 0)
			return temp_list;
		else
			temp_list = temp_list->next;
	}
	
	/* Broadcast IP ?
	 */
	temp_addr[0] = temp_addr[1] = temp_addr[2] = 0xFF;
	temp_addr[3] = temp_addr[4] = temp_addr[5] = 0xFF;
	if (memcmp(n_port_name, temp_addr, FC_ALEN) == 0) {
		q = (struct fc_node_info *)kmalloc(sizeof(struct fc_node_info), GFP_ATOMIC);
		if (q == NULL) {
			T_MSG("kmalloc failed in look_up_cache()");
			return NULL;
		}
		q->d_id = BROADCAST;
		return q;
	}
	LEAVE("look_up_cache");
	return NULL;
}

static int display_cache(struct fc_info *fi)
{
struct fc_node_info *q = fi->node_info_list;
#if DEBUG_5526_2
struct ox_id_els_map *temp_ox_id_list = fi->ox_id_list;
#endif
int count = 0, j;
	printk("\nFibre Channel Node Information for %s\n", fi->name);
	printk("My FC_ID = %x, My WWN = %x %x, ", fi->g.my_id, fi->g.my_node_name_high, fi->g.my_node_name_low);
	if (fi->g.ptp_up == TRUE)
		printk("Port_Type = N_Port\n");
	if (fi->g.loop_up == TRUE)
		printk("Port_Type = L_Port\n");
	while(q != NULL) {
		printk("WWN = ");
		for (j = 0; j < PORT_NAME_LEN; j++)
			printk("%x ", q->hw_addr[j]); 
		printk("FC_ID = %x, ", q->d_id);
		printk("Login = ");
		if (q->login == LOGIN_COMPLETED)
			printk("ON ");
		else
			printk("OFF ");
		if (q->scsi == TARGET)
			printk("Target_ID = %d ", q->target_id);
		printk("\n");
		q = q->next;
		count++;
	}

#if DEBUG_5526_2
	printk("OX_ID -> ELS Map\n");
	while(temp_ox_id_list) {
			printk("ox_id = %x, ELS = %x\n", temp_ox_id_list->ox_id, temp_ox_id_list->els);
			temp_ox_id_list = temp_ox_id_list->next;
	}
#endif

	return 0;
}

static struct net_device_stats * iph5526_get_stats(struct net_device *dev)
{	
struct fc_info *fi = (struct fc_info*)dev->priv; 
	return (struct net_device_stats *) &fi->fc_stats;
}


/* SCSI stuff starts here */

int iph5526_detect(Scsi_Host_Template *tmpt)
{
struct Scsi_Host *host = NULL;
struct iph5526_hostdata *hostdata;
struct fc_info *fi = NULL;
int no_of_hosts = 0, timeout, i, j, count = 0;
u_int pci_maddr = 0;
struct pci_dev *pdev = NULL;

	tmpt->proc_name = "iph5526";
	if (pci_present() == 0) {
		printk("iph5526: PCI not present\n");
		return 0;
	}

	for (i = 0; i <= MAX_FC_CARDS; i++) 
		fc[i] = NULL;

	for (i = 0; i < clone_list[i].vendor_id != 0; i++)
	while ((pdev = pci_find_device(clone_list[i].vendor_id, clone_list[i].device_id, pdev))) {
	unsigned short pci_command;
		if (count < MAX_FC_CARDS) {
			fc[count] = kmalloc(sizeof(struct fc_info), GFP_ATOMIC);
			if (fc[count] == NULL) {
				printk("iph5526.c: Unable to register card # %d\n", count + 1);
				return no_of_hosts;
			}
			memset(fc[count], 0, sizeof(struct fc_info));
		}
		else {
			printk("iph5526.c: Maximum Number of cards reached.\n");
			return no_of_hosts;
		}
			
		fi = fc[count];
		sprintf(fi->name, "fc%d", count);

		host = scsi_register(tmpt, sizeof(struct iph5526_hostdata));
		hostdata = (struct iph5526_hostdata *)host->hostdata;
		memset(hostdata, 0 , sizeof(struct iph5526_hostdata));
		for (j = 0; j < MAX_SCSI_TARGETS; j++)
			hostdata->tag_ages[j] = jiffies;
		hostdata->fi = fi;
		fi->host = host;
		//host->max_id = MAX_SCSI_TARGETS;
		host->max_id = 5;
		host->hostt->use_new_eh_code = 1;
		host->this_id = tmpt->this_id;

		pci_maddr = pdev->resource[0].start;
		if ( (pdev->resource[0].flags & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
			printk("iph5526.c : Cannot find proper PCI device base address.\n");
			scsi_unregister(host);
			kfree(fc[count]);
			fc[count] = NULL;
			continue;
		}
		
		DPRINTK("pci_maddr = %x", pci_maddr);
		pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
			
		pci_irq_line = pdev->irq;
		printk("iph5526.c: PCI BIOS reports %s at i/o %#x, irq %d.\n", clone_list[i].name, pci_maddr, pci_irq_line);
		fi->g.mem_base = ioremap(pci_maddr & PAGE_MASK, 1024);
		
		/* We use Memory Mapped IO. The initial space contains the
		 * PCI Configuration registers followed by the (i) chip
		 * registers followed by the Tachyon registers.
		 */
		/* Thatz where (i)chip maps Tachyon Address Space.
		 */
		fi->g.tachyon_base = (u_long)fi->g.mem_base + TACHYON_OFFSET + ( pci_maddr & ~PAGE_MASK );
		DPRINTK("fi->g.tachyon_base = %x", (u_int)fi->g.tachyon_base);
		if (fi->g.mem_base == NULL) {
			printk("iph5526.c : ioremap failed!!!\n");
			scsi_unregister(host);
			kfree(fc[count]);
			fc[count] = NULL;
			continue;
		}	
		DPRINTK("IRQ1 = %d\n", pci_irq_line);
		printk(version);
		fi->base_addr = (long) pdev;

		if (pci_irq_line) {
		int irqval = 0;
			/* Found it, get IRQ.
			 */
			irqval = request_irq(pci_irq_line, &tachyon_interrupt, pci_irq_line ? SA_SHIRQ : 0, fi->name, host);
			if (irqval) {
				printk("iph5526.c : Unable to get IRQ %d (irqval = %d).\n", pci_irq_line, irqval);
				scsi_unregister(host);
				kfree(fc[count]);
				fc[count] = NULL;
				continue;
			}
			host->irq = fi->irq = pci_irq_line;
			pci_irq_line = 0;
			fi->clone_id = clone_list[i].vendor_id;
		}

		if (!initialize_register_pointers(fi) || !tachyon_init(fi)) {
			printk("iph5526.c: TACHYON initialization failed for card # %d!!!\n", count + 1);
			free_irq(host->irq, host);
			scsi_unregister(host);
			if (fi) 
				clean_up_memory(fi);
			kfree(fc[count]);
			fc[count] = NULL;
			break;
		}
		DPRINTK1("Fibre Channel card initialized");
		/* Wait for the Link to come up and the login process 
		 * to complete. 
		 */
		for(timeout = jiffies + 10*HZ; (timeout > jiffies) && ((fi->g.link_up == FALSE) || (fi->g.port_discovery == TRUE) || (fi->g.explore_fabric == TRUE) || (fi->g.perform_adisc == TRUE));)
			barrier();
		
		count++;
		no_of_hosts++;
	}
	DPRINTK1("no_of_hosts = %d",no_of_hosts);
	
	/* This is to make sure that the ACC to the PRLI comes in 
	 * for the last ALPA. 
	 */
	udelay(1000000); /* Ugly! Let the Gods forgive me */

	DPRINTK1("leaving iph5526_detect\n");
	return no_of_hosts;
}


int iph5526_biosparam(Disk * disk, kdev_t n, int ip[])
{
int size = disk->capacity;
	ip[0] = 64;
	ip[1] = 32;
	ip[2] = size >> 11;
	if (ip[2] > 1024) {
		ip[0] = 255;
		ip[1] = 63;
		ip[2] = size / (ip[0] * ip[1]);
	}
	return 0;
}

int iph5526_queuecommand(Scsi_Cmnd *Cmnd, void (*done) (Scsi_Cmnd *))
{
int int_required = 0;
u_int r_ctl = FC4_DEVICE_DATA | UNSOLICITED_COMMAND;
u_int type = TYPE_FCP | SEQUENCE_INITIATIVE;
u_int frame_class = Cmnd->target;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
struct Scsi_Host *host = Cmnd->host;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata*)host->hostdata;
struct fc_info *fi = hostdata->fi;
struct fc_node_info *q;
u_long flags;
	ENTER("iph5526_queuecommand");

	spin_lock_irqsave(&fi->fc_lock, flags);
	Cmnd->scsi_done = done;

	if (Cmnd->device->tagged_supported) {
		switch(Cmnd->tag) {
			case SIMPLE_QUEUE_TAG:
				hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
				break;
			case HEAD_OF_QUEUE_TAG:
				hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_HEAD_OF_Q;
				break;
			case  ORDERED_QUEUE_TAG:
				hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
				break;
			default:
				if ((jiffies - hostdata->tag_ages[Cmnd->target]) > (5 * HZ)) {
					hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
					hostdata->tag_ages[Cmnd->target] = jiffies;
				}
				else
					hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
				break;
		}
	}
	/*else
		hostdata->cmnd.fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
	*/

	hostdata->cmnd.fcp_addr[3] = 0;
	hostdata->cmnd.fcp_addr[2] = 0;
	hostdata->cmnd.fcp_addr[1] = 0;
	hostdata->cmnd.fcp_addr[0] = htons(Cmnd->lun);

	memcpy(&hostdata->cmnd.fcp_cdb, Cmnd->cmnd, Cmnd->cmd_len);
	hostdata->cmnd.fcp_data_len = htonl(Cmnd->request_bufflen);

	/* Get an used OX_ID. We could have pending commands.
	 */
	if (get_scsi_oxid(fi))
		return 1;
	fi->q.free_scsi_oxid[fi->g.scsi_oxid] = OXID_INUSE;	

	/* Maintain a handler so that we can associate the done() function
	 * on completion of the SCSI command. 
	 */
	hostdata->cmnd_handler[fi->g.scsi_oxid] = Cmnd;

	switch(Cmnd->cmnd[0]) {
		case WRITE_6:
		case WRITE_10:
		case WRITE_12:
			fi->g.type_of_frame = FC_SCSI_WRITE;
			hostdata->cmnd.fcp_cntl = htonl(FCP_CNTL_WRITE | hostdata->cmnd.fcp_cntl);
			break;
		default:
			fi->g.type_of_frame = FC_SCSI_READ;
			hostdata->cmnd.fcp_cntl = htonl(FCP_CNTL_READ | hostdata->cmnd.fcp_cntl);
	}
	
	memcpy(fi->q.ptr_fcp_cmnd[fi->q.fcp_cmnd_indx], &(hostdata->cmnd), sizeof(fcp_cmd));	
	
	q = resolve_target(fi, Cmnd->target);

	if (q == NULL) {
	u_int bad_id = fi->g.my_ddaa | 0xFE;
		/* We transmit to an non-existant AL_PA so that the "done" 
		 * function can be called while receiving the interrupt 
		 * due to a Timeout for a bad AL_PA. In a PTP configuration,
		 * the int_required field is set, since there is no notion
		 * of AL_PAs. This approach sucks, but works alright!
		 */
		if (fi->g.ptp_up == TRUE)
			int_required = 1;
		tx_exchange(fi, (char *)(&(hostdata->cmnd)), sizeof(fcp_cmd), r_ctl, type, bad_id, fi->g.my_mtu, int_required, ox_id, FC_SCSI_BAD_TARGET);
		spin_unlock_irqrestore(&fi->fc_lock, flags);
		DPRINTK1("Target ID %x not present", Cmnd->target);
		return 0;
	}
	if (q->login == LOGIN_COMPLETED) {
		if (add_to_sest(fi, Cmnd, q)) {
			DPRINTK1("add_to_sest() failed.");
			spin_unlock_irqrestore(&fi->fc_lock, flags);
			return 0;
		}
		tx_exchange(fi, (char *)(fi->q.ptr_fcp_cmnd[fi->q.fcp_cmnd_indx]), sizeof(fcp_cmd), r_ctl, type, q->d_id, q->mtu, int_required, ox_id, frame_class << 16);
		update_FCP_CMND_indx(fi);
	}
	spin_unlock_irqrestore(&fi->fc_lock, flags);
	/* If q != NULL, then we have a SCSI Target. 
	 * If q->login != LOGIN_COMPLETED, then that device could be 
	 * offline temporarily. So we let the command to time-out. 
	 */
	LEAVE("iph5526_queuecommand");
	return 0;
}

int iph5526_abort(Scsi_Cmnd *Cmnd)
{
struct Scsi_Host *host = Cmnd->host;
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata *)host->hostdata;
struct fc_info *fi = hostdata->fi;
struct fc_node_info *q;
u_int r_ctl = FC4_DEVICE_DATA | UNSOLICITED_COMMAND;
u_int type = TYPE_FCP | SEQUENCE_INITIATIVE;
u_short ox_id = OX_ID_FIRST_SEQUENCE;
int int_required = 1, i, abort_status = FALSE;
u_long flags;
	
	ENTER("iph5526_abort");
	
	spin_lock_irqsave(&fi->fc_lock, flags);
	
	q = resolve_target(fi, Cmnd->target);
	if (q == NULL) {
	u_int bad_id = fi->g.my_ddaa | 0xFE;
		/* This should not happen as we should always be able to
		 * resolve a target id. But, jus in case...
		 * We transmit to an non-existant AL_PA so that the done 
		 * function can be called while receiving the interrupt 
		 * for a bad AL_PA. 
		 */
		DPRINTK1("Unresolved Target ID!");
		tx_exchange(fi, (char *)(&(hostdata->cmnd)), sizeof(fcp_cmd), r_ctl, type, bad_id, fi->g.my_mtu, int_required, ox_id, FC_SCSI_BAD_TARGET);
		DPRINTK1("Target ID %x not present", Cmnd->target);
		spin_unlock_irqrestore(&fi->fc_lock, flags);
		return FAILED;
	}

	/* If q != NULL, then we have a SCSI Target. If 
	 * q->login != LOGIN_COMPLETED, then that device could 
	 * be offline temporarily. So we let the command to time-out. 
	 */

	/* Get the OX_ID for the Command to be aborted.
	 */
	for (i = 0; i <= MAX_SCSI_XID; i++) {
		if (hostdata->cmnd_handler[i] == Cmnd) {
			hostdata->cmnd_handler[i] = NULL;
			ox_id = i;
			break;
		}
	}
	if (i > MAX_SCSI_XID) {
		T_MSG("Command could not be resolved to OX_ID");
		spin_unlock_irqrestore(&fi->fc_lock, flags);
		return FAILED;
	}

	switch(Cmnd->cmnd[0]) {
		case WRITE_6:
		case WRITE_10:
		case WRITE_12:
			break;
		default:
			ox_id |= SCSI_READ_BIT;
	}
	abort_status = abort_exchange(fi, ox_id);
	
	if ((q->login == LOGIN_COMPLETED) && (abort_status == TRUE)) {
		/* Then, transmit an ABTS to the target. The rest 
		 * is done when the BA_ACC is received for the ABTS.
 	 	 */
		tx_abts(fi, q->d_id, ox_id);
	}
	else {
	u_int STE_bit;
	u_short x_id;
		/* Invalidate resources for that Exchange.
		 */
		x_id = ox_id & MAX_SCSI_XID;
		STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
		if (STE_bit & SEST_V) {
			*(fi->q.ptr_sest[x_id]) &= htonl(SEST_INV);
			invalidate_SEST_entry(fi, ox_id);
		}
	}

	LEAVE("iph5526_abort");
	spin_unlock_irqrestore(&fi->fc_lock, flags);
	return SUCCESS;
}

static int abort_exchange(struct fc_info *fi, u_short ox_id)
{
u_short x_id;
volatile u_int flush_SEST, STE_bit;
	x_id = ox_id & MAX_SCSI_XID;
	DPRINTK1("Aborting Exchange %x", ox_id);

	STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
	/* Is the Exchange still active?.
	 */
	if (STE_bit & SEST_V) {
		if (ox_id & SCSI_READ_BIT) {
			/* If the Exchange to be aborted is Inbound, 
			 * Flush the SEST Entry from Tachyon's Cache.
			 */
			*(fi->q.ptr_sest[x_id]) &= htonl(SEST_INV);
			flush_tachyon_cache(fi, ox_id);
			flush_SEST = readl(fi->t_r.ptr_tach_flush_oxid_reg);
			while ((flush_SEST & 0x80000000) != 0) 
				flush_SEST = readl(fi->t_r.ptr_tach_flush_oxid_reg);
			STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
			while ((STE_bit & 0x80000000) != 0)
				STE_bit = ntohl(*fi->q.ptr_sest[x_id]);
			flush_SEST = readl(fi->t_r.ptr_tach_flush_oxid_reg);
			invalidate_SEST_entry(fi, ox_id);
		}
		else {
		int i;
		u_int *ptr_edb;
			/* For In-Order Reassembly, the following is done:
			 * First, write zero as the buffer length in the EDB. 
		 	 */
			ptr_edb = bus_to_virt(ntohl(*(fi->q.ptr_sest[x_id] + 7)));
			for (i = 0; i < EDB_LEN; i++)
				if (fi->q.ptr_edb[i] == ptr_edb)
					break;
			if (i < EDB_LEN) 
				*ptr_edb = *ptr_edb & 0x0000FFFF;
			else
				T_MSG("EDB not found while clearing in abort_exchange()");
		}
		DPRINTK1("Exchange %x invalidated", ox_id);
		return TRUE;
	}
	else {
		DPRINTK1("SEST Entry for exchange %x not valid", ox_id);
		return FALSE;
	}	
}

static void flush_tachyon_cache(struct fc_info *fi, u_short ox_id)
{
volatile u_int tachyon_status;
	if (fi->g.loop_up == TRUE) {
		writel(HOST_CONTROL, fi->t_r.ptr_fm_control_reg);
		/* Make sure that the Inbound FIFO is empty.
		 */
		do {
			tachyon_status = readl(fi->t_r.ptr_tach_status_reg);
			udelay(200);
		}while ((tachyon_status & RECEIVE_FIFO_EMPTY) == 0);
		/* Ok. Go ahead and flushhhhhhhhh!
		 */
		writel(0x80000000 | ox_id, fi->t_r.ptr_tach_flush_oxid_reg);
		writel(EXIT_HOST_CONTROL, fi->t_r.ptr_fm_control_reg);
		return;
	}
	if (fi->g.ptp_up == TRUE) {
		take_tachyon_offline(fi);
		/* Make sure that the Inbound FIFO is empty.
		 */
		do {
			tachyon_status = readl(fi->t_r.ptr_tach_status_reg);
			udelay(200);
		}while ((tachyon_status & RECEIVE_FIFO_EMPTY) == 0);
		writel(0x80000000 | ox_id, fi->t_r.ptr_tach_flush_oxid_reg);
		/* Write the Initialize command to the FM Control reg.
		 */
		fi->g.n_port_try = TRUE;
		DPRINTK1("In abort_exchange, TACHYON initializing as N_Port...\n");
		writel(INITIALIZE, fi->t_r.ptr_fm_control_reg);
	}
}

static struct fc_node_info *resolve_target(struct fc_info *fi, u_char target)
{
struct fc_node_info *temp = fi->node_info_list;
	while(temp != NULL)
		if (temp->target_id == target) {
			if ((temp->scsi == TARGET) && (temp->login == LOGIN_COMPLETED))
				return temp;
			else {
				if (temp->login != LOGIN_COMPLETED) {
					/* The Target is not currently logged in.
					 * It could be a Target on the Local Loop or
					 * on a Remote Loop connected through a switch.
					 * In either case, we will know whenever the Target
					 * comes On-Line again. We let the command to 
					 * time-out so that it gets retried.
					 */
					T_MSG("Target %d not logged in.", temp->target_id);
					tx_logi(fi, ELS_PLOGI, temp->d_id);
					return temp;
				}
				else {
					if (temp->scsi != TARGET) {
						/* For some reason, we did not get a response to
						 * PRLI. Letz try it again...
						 */
						DPRINTK1("Node not PRLIied. Txing PRLI...");
						tx_prli(fi, ELS_PRLI, temp->d_id, OX_ID_FIRST_SEQUENCE);
					}
				}
				return temp;
			}
		}
		else
			temp = temp->next;
	return NULL;
}

static int add_to_sest(struct fc_info *fi, Scsi_Cmnd *Cmnd, struct fc_node_info *ni)
{
/* we have at least 1 buffer, the terminator */
int no_of_sdb_buffers = 1, i; 
int no_of_edb_buffers = 0; 
u_int *req_buffer = (u_int *)Cmnd->request_buffer;
u_int *ptr_sdb = NULL;
struct scatterlist *sl1, *sl2 = NULL;
int no_of_sg = 0;

	switch(fi->g.type_of_frame) {
		case FC_SCSI_READ:
			fi->g.inb_sest_entry.flags_and_byte_offset = htonl(INB_SEST_VED);
			fi->g.inb_sest_entry.byte_count = 0;
			fi->g.inb_sest_entry.no_of_recvd_frames = 0;
			fi->g.inb_sest_entry.no_of_expected_frames = 0;
			fi->g.inb_sest_entry.last_fctl = 0;

			if (Cmnd->use_sg) {
				no_of_sg = Cmnd->use_sg;
				sl1 = sl2 = (struct scatterlist *)Cmnd->request_buffer;
				for (i = 0; i < no_of_sg; i++) {
					no_of_sdb_buffers += sl1->length / SEST_BUFFER_SIZE;
					if (sl1->length % SEST_BUFFER_SIZE)
						no_of_sdb_buffers++;
					sl1++;
				}
			}
			else {
				no_of_sdb_buffers += Cmnd->request_bufflen / SEST_BUFFER_SIZE;
				if (Cmnd->request_bufflen % SEST_BUFFER_SIZE)
					no_of_sdb_buffers++;
			} /* if !use_sg */

			/* We are working with the premise that at the max we would
			 * get a scatter-gather buffer containing 63 buffers
			 * of size 1024 bytes each. Is it a _bad_ assumption?
			 */
			if (no_of_sdb_buffers > 512) {
				T_MSG("Number of SDB buffers needed = %d", no_of_sdb_buffers);
				T_MSG("Disable Scatter-Gather!!!");
				return 1;
			}
				

			/* Store it in the sdb_table so that we can retrieve that
			 * free up the memory when the Read Command completes.
			 */
			if (get_free_SDB(fi))
				return 1;
			ptr_sdb = fi->q.ptr_sdb_slot[fi->q.sdb_indx];
			fi->q.sdb_slot_status[fi->q.sdb_indx] = SDB_BUSY;
			fi->g.inb_sest_entry.sdb_address = htonl(virt_to_bus(ptr_sdb));

			if (Cmnd->use_sg) {
			int count = 0, j;
				for(i = 0; i < no_of_sg; i++) {
				char *addr_ptr = sl2->address;
					count = sl2->length / SEST_BUFFER_SIZE;
					if (sl2->length % SEST_BUFFER_SIZE)
						count++;
					for (j = 0; j < count; j++) {
						*(ptr_sdb) = htonl(virt_to_bus(addr_ptr));
						addr_ptr += SEST_BUFFER_SIZE;
						ptr_sdb++;
					}
					count = 0;
					sl2++;
				}
			}
			else {
				for (i = 0; i < no_of_sdb_buffers - 1; i++) {
					*(ptr_sdb) = htonl(virt_to_bus(req_buffer));
					req_buffer += SEST_BUFFER_SIZE/4;
					ptr_sdb++;
				}
			}
			*(ptr_sdb) = htonl(0x1); /* Terminator */
			
			/* The scratch pad is used to hold the index into the SDB.
			 */
			fi->g.inb_sest_entry.scratch_pad = fi->q.sdb_indx;
			fi->g.inb_sest_entry.expected_ro = 0;
			fi->g.inb_sest_entry.buffer_index = 0;
			fi->g.inb_sest_entry.buffer_offset = 0;
			memcpy(fi->q.ptr_sest[fi->g.scsi_oxid], &fi->g.inb_sest_entry, sizeof(INB_SEST_ENTRY));
			break;
		case FC_SCSI_WRITE:
			fi->g.outb_sest_entry.flags_and_did = htonl(OUTB_SEST_VED | ni->d_id);
			fi->g.outb_sest_entry.max_frame_len = htons(ni->mtu << 4);
			fi->g.outb_sest_entry.cntl = htons(ODB_CLASS_3 | ODB_EE_CREDIT | ODB_NO_INT | ODB_NO_COMP);
			fi->g.outb_sest_entry.total_seq_length = INV_SEQ_LEN;
			fi->g.outb_sest_entry.link = htons(OUTB_SEST_LINK);
			fi->g.outb_sest_entry.transaction_id = htonl(fi->g.scsi_oxid);
			fi->g.outb_sest_entry.seq_id = fi->g.seq_id;
			fi->g.outb_sest_entry.reserved = 0x0;
			fi->g.outb_sest_entry.header_length = htons(TACHYON_HEADER_LEN);
		
			{
			u_char df_ctl = 0;
			u_short rx_id = RX_ID_FIRST_SEQUENCE;
			u_int r_ctl = FC4_DEVICE_DATA | SOLICITED_DATA;
			u_int type = TYPE_FCP | SEQUENCE_INITIATIVE;
				/* Multi Frame Sequence ? If yes, set RO bit. 
				 */
				if (Cmnd->request_bufflen > ni->mtu)
					type |= RELATIVE_OFF_PRESENT;
				build_tachyon_header(fi, fi->g.my_id, r_ctl, ni->d_id, type, fi->g.seq_id, df_ctl, fi->g.scsi_oxid, rx_id, NULL);
				if (get_free_header(fi) || get_free_EDB(fi))
					return 1;
				memcpy(fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx], &(fi->g.tach_header), TACHYON_HEADER_LEN);
				fi->g.outb_sest_entry.header_address = htonl(virt_to_bus(fi->q.ptr_tachyon_header[fi->q.tachyon_header_indx]));
				update_tachyon_header_indx(fi);
			}

			if (Cmnd->use_sg) {
				no_of_sg = Cmnd->use_sg;
				sl1 = sl2 = (struct scatterlist *)Cmnd->request_buffer;
				for (i = 0; i < no_of_sg; i++) {
					no_of_edb_buffers += sl1->length / SEST_BUFFER_SIZE;
					if (sl1->length % SEST_BUFFER_SIZE)
						no_of_edb_buffers++;
					sl1++;
				}
			}
			else {
				no_of_edb_buffers += Cmnd->request_bufflen / SEST_BUFFER_SIZE;
				if (Cmnd->request_bufflen % SEST_BUFFER_SIZE)
					no_of_edb_buffers++;
			} /* if !use_sg */


			/* We need "no_of_edb_buffers" _contiguous_ EDBs 
			 * that are FREE. Check for that first.
			 */
			for (i = 0; i < no_of_edb_buffers; i++) {
			int j;
				if ((fi->q.edb_buffer_indx + no_of_edb_buffers) >= EDB_LEN)
					fi->q.edb_buffer_indx = 0;
				if (fi->q.free_edb_list[fi->q.edb_buffer_indx + i] != EDB_FREE) {
					for (j = 0; j < i; j++)
						update_EDB_indx(fi);
					if (get_free_EDB(fi))
						return 1;
					i = 0;
				}
			}

			/* We got enuff FREE EDBs.
			 */
			if (Cmnd->use_sg) {
				fi->g.outb_sest_entry.edb_address = htonl(virt_to_bus(fi->q.ptr_edb[fi->q.edb_buffer_indx]));
				sl1 = (struct scatterlist *)Cmnd->request_buffer;
				for(i = 0; i < no_of_sg; i++) {
				int count = 0, j;
					count = sl1->length / SEST_BUFFER_SIZE;
					for (j = 0; j < count; j++) {
						build_EDB(fi, (char *)sl1->address, 0, SEST_BUFFER_SIZE);
						memcpy(fi->q.ptr_edb[fi->q.edb_buffer_indx], &(fi->g.edb), sizeof(EDB));
						/* Mark this EDB as being in use */
						fi->q.free_edb_list[fi->q.edb_buffer_indx] = EDB_BUSY;
						/* We have already made sure that we have enuff
				 	 	 * free EDBs that are contiguous. So this is 
						 * safe.
				 	 	 */
						update_EDB_indx(fi);
						sl1->address += SEST_BUFFER_SIZE;
					}
					/* Just in case itz not a multiple of 
					 * SEST_BUFFER_SIZE bytes.
					 */
					if (sl1->length % SEST_BUFFER_SIZE) {
						build_EDB(fi, (char *)sl1->address, 0, sl1->length % SEST_BUFFER_SIZE);
						memcpy(fi->q.ptr_edb[fi->q.edb_buffer_indx], &(fi->g.edb), sizeof(EDB));
						fi->q.free_edb_list[fi->q.edb_buffer_indx] = EDB_BUSY;
						update_EDB_indx(fi);
					}
					sl1++;
				}
				/* The last EDB is special. It needs the "end bit" to
				 * be set.
				 */
				*(fi->q.ptr_edb[fi->q.edb_buffer_indx - 1] + 1) = *(fi->q.ptr_edb[fi->q.edb_buffer_indx - 1] + 1) | ntohs(EDB_END);
			}
			else {
			int count = 0, j;
				fi->g.outb_sest_entry.edb_address = htonl(virt_to_bus(fi->q.ptr_edb[fi->q.edb_buffer_indx]));
				count = Cmnd->request_bufflen / SEST_BUFFER_SIZE;
				for (j = 0; j < count; j++) {
					build_EDB(fi, (char *)req_buffer, 0, SEST_BUFFER_SIZE);
					memcpy(fi->q.ptr_edb[fi->q.edb_buffer_indx], &(fi->g.edb), sizeof(EDB));
					/* Mark this EDB as being in use */
					fi->q.free_edb_list[fi->q.edb_buffer_indx] = EDB_BUSY;
					/* We have already made sure that we have enuff
			 	 	 * free EDBs that are contiguous. So this is 
					 * safe.
			 	 	 */
					update_EDB_indx(fi);
					req_buffer += SEST_BUFFER_SIZE;
				}
				/* Just in case itz not a multiple of 
				 * SEST_BUFFER_SIZE bytes.
				 */
				if (Cmnd->request_bufflen % SEST_BUFFER_SIZE) {
					build_EDB(fi, (char *)req_buffer, EDB_END, Cmnd->request_bufflen % SEST_BUFFER_SIZE);
					memcpy(fi->q.ptr_edb[fi->q.edb_buffer_indx], &(fi->g.edb), sizeof(EDB));
					fi->q.free_edb_list[fi->q.edb_buffer_indx] = EDB_BUSY;
					update_EDB_indx(fi);
				}
				else {
					/* Mark the last EDB as the "end edb".
					 */
					*(fi->q.ptr_edb[fi->q.edb_buffer_indx - 1] + 1) = *(fi->q.ptr_edb[fi->q.edb_buffer_indx - 1] + 1) | htons(EDB_END);
				}
			}

			/* Finally we have something to send!.
			 */
			memcpy(fi->q.ptr_sest[fi->g.scsi_oxid], &fi->g.outb_sest_entry, sizeof(OUTB_SEST_ENTRY));
			break;
		}		
	return 0;
}

static void update_FCP_CMND_indx(struct fc_info *fi)
{
	fi->q.fcp_cmnd_indx++;
	if (fi->q.fcp_cmnd_indx == NO_OF_FCP_CMNDS)
		fi->q.fcp_cmnd_indx = 0;
}

static int get_scsi_oxid(struct fc_info *fi)
{
u_short initial_oxid = fi->g.scsi_oxid;
	/* Check if the OX_ID is in use.
	 * We could have an outstanding SCSI command.
	 */
	while (fi->q.free_scsi_oxid[fi->g.scsi_oxid] != OXID_AVAILABLE) {
		update_scsi_oxid(fi);
		if (fi->g.scsi_oxid == initial_oxid) {
			T_MSG("No free OX_IDs avaliable")
			reset_tachyon(fi, SOFTWARE_RESET);
			return 1;
		}
	}
	return 0;
}		

static void update_scsi_oxid(struct fc_info *fi)
{
	fi->g.scsi_oxid++;
	if (fi->g.scsi_oxid == (MAX_SCSI_XID + 1))
		fi->g.scsi_oxid = 0;
}

static int get_free_SDB(struct fc_info *fi)
{
unsigned int initial_indx = fi->q.sdb_indx;
	/* Check if the SDB is in use.
	 * We could have an outstanding SCSI Read command.
	 * We should find a free slot as we can queue a
	 * maximum of 32 SCSI commands only. 
	 */
	while (fi->q.sdb_slot_status[fi->q.sdb_indx] != SDB_FREE) {
		update_SDB_indx(fi);
		if (fi->q.sdb_indx == initial_indx) {
			T_MSG("No free SDB buffers avaliable")
			reset_tachyon(fi, SOFTWARE_RESET);
			return 1;
		}
	}
	return 0;
}		

static void update_SDB_indx(struct fc_info *fi)
{
	fi->q.sdb_indx++;
	if (fi->q.sdb_indx == NO_OF_SDB_ENTRIES)
		fi->q.sdb_indx = 0;
}

int iph5526_release(struct Scsi_Host *host)
{
struct iph5526_hostdata *hostdata = (struct iph5526_hostdata*)host->hostdata;
struct fc_info *fi = hostdata->fi;
	free_irq(host->irq, host);
	iounmap(fi->g.mem_base);
	return 0;
}

const char *iph5526_info(struct Scsi_Host *host)
{
static char buf[80];
	sprintf(buf, "Interphase 5526 Fibre Channel PCI SCSI Adapter using IRQ %d\n", host->irq);
	return buf;
}

#ifdef MODULE

#define NAMELEN		8	/* # of chars for storing dev->name */

static struct net_device *dev_fc[MAX_FC_CARDS];

static int io =  0;
static int irq  = 0;
static int bad  = 0;	/* 0xbad = bad sig or no reset ack */
static int scsi_registered;


int init_module(void)
{
int i = 0;

	driver_template.module = &__this_module;
	scsi_register_module(MODULE_SCSI_HA, &driver_template);
	if (driver_template.present)
		scsi_registered = TRUE; 
	else {
		printk("iph5526: SCSI registeration failed!!!\n");
		scsi_registered = FALSE;
		scsi_unregister_module(MODULE_SCSI_HA, &driver_template);
	}

	while(fc[i] != NULL) {
		dev_fc[i] = NULL;
		dev_fc[i] = init_fcdev(dev_fc[i], 0);	
		if (dev_fc[i] == NULL) {
			printk("iph5526.c: init_fcdev failed for card #%d\n", i+1);
			break;
		}
		dev_fc[i]->irq = irq;
		dev_fc[i]->mem_end = bad;
		dev_fc[i]->base_addr = io;
		dev_fc[i]->init = iph5526_probe;
		dev_fc[i]->priv = fc[i];
		fc[i]->dev = dev_fc[i];
		if (register_fcdev(dev_fc[i]) != 0) {
			kfree_s(dev_fc[i], sizeof(struct net_device));
			dev_fc[i] = NULL;
			if (i == 0) {
				printk("iph5526.c: IP registeration failed!!!\n");
				return -ENODEV;
			}
		}
		i++;
	}
	if (i == 0)
		return -ENODEV;
	
	return 0;
}

void cleanup_module(void)
{
int i = 0;
	while(fc[i] != NULL) {
	struct net_device *dev = fc[i]->dev;
	void *priv = dev->priv;
		fc[i]->g.dont_init = TRUE;
		take_tachyon_offline(fc[i]);
		unregister_fcdev(dev);
		clean_up_memory(fc[i]);
		if (dev->priv)
			kfree(priv);
		kfree(dev);
		dev = NULL;
		i++;
	}
	if (scsi_registered == TRUE)
		scsi_unregister_module(MODULE_SCSI_HA, &driver_template); 
}
#endif /* MODULE */

void clean_up_memory(struct fc_info *fi)
{
int i,j;
	ENTER("clean_up_memory");
	if (fi->q.ptr_mfsbq_base)
		free_pages((u_long)bus_to_virt(ntohl(*(fi->q.ptr_mfsbq_base))), 5);
	DPRINTK("after kfree2");
	for (i = 0; i < SFSBQ_LENGTH; i++)
		for (j = 0; j < NO_OF_ENTRIES; j++)
			if (fi->q.ptr_sfs_buffers[i*NO_OF_ENTRIES + j])
				kfree(fi->q.ptr_sfs_buffers[i*NO_OF_ENTRIES + j]);
	DPRINTK("after kfree1");
	if (fi->q.ptr_ocq_base)
		free_page((u_long)fi->q.ptr_ocq_base);
	if (fi->q.ptr_imq_base)
		free_page((u_long)fi->q.ptr_imq_base);
	if (fi->q.ptr_mfsbq_base)
		free_page((u_long)fi->q.ptr_mfsbq_base);
	if (fi->q.ptr_sfsbq_base)
		free_page((u_long)fi->q.ptr_sfsbq_base);
	if (fi->q.ptr_edb_base)
		free_pages((u_long)fi->q.ptr_edb_base, 5);
	if (fi->q.ptr_sest_base)
		free_pages((u_long)fi->q.ptr_sest_base, 5);
	if (fi->q.ptr_tachyon_header_base)
		free_page((u_long)fi->q.ptr_tachyon_header_base);
	if (fi->q.ptr_sdb_base)
		free_pages((u_long)fi->q.ptr_sdb_base, 5);
	if (fi->q.ptr_fcp_cmnd_base)
		free_page((u_long)fi->q.ptr_fcp_cmnd_base);
	DPRINTK("after free_pages"); 
	if (fi->q.ptr_host_ocq_cons_indx)
		kfree(fi->q.ptr_host_ocq_cons_indx);
	if (fi->q.ptr_host_hpcq_cons_indx)
		kfree(fi->q.ptr_host_hpcq_cons_indx);
	if (fi->q.ptr_host_imq_prod_indx)
		kfree(fi->q.ptr_host_imq_prod_indx);
	DPRINTK("after kfree3");
	while (fi->node_info_list) {
	struct fc_node_info *temp_list = fi->node_info_list;
		fi->node_info_list = fi->node_info_list->next;
		kfree(temp_list);
	}
	while (fi->ox_id_list) {
	struct ox_id_els_map *temp = fi->ox_id_list;
		fi->ox_id_list = fi->ox_id_list->next;
		kfree(temp);
	}
	LEAVE("clean_up_memory");
}

static int initialize_register_pointers(struct fc_info *fi)
{
ENTER("initialize_register_pointers");
if(fi->g.tachyon_base == 0)
	return -ENOMEM; 
	
fi->i_r.ptr_ichip_hw_control_reg	= ICHIP_HW_CONTROL_REG_OFF + fi->g.tachyon_base;
fi->i_r.ptr_ichip_hw_status_reg = ICHIP_HW_STATUS_REG_OFF + fi->g.tachyon_base;
fi->i_r.ptr_ichip_hw_addr_mask_reg = ICHIP_HW_ADDR_MASK_REG_OFF + fi->g.tachyon_base;
fi->t_r.ptr_ocq_base_reg = OCQ_BASE_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_ocq_len_reg = OCQ_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_ocq_prod_indx_reg = OCQ_PRODUCER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_ocq_cons_indx_reg = OCQ_CONSUMER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_imq_base_reg = IMQ_BASE_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_imq_len_reg = IMQ_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_imq_cons_indx_reg = IMQ_CONSUMER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_imq_prod_indx_reg = IMQ_PRODUCER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_mfsbq_base_reg = MFSBQ_BASE_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_mfsbq_len_reg = MFSBQ_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_mfsbq_prod_reg = MFSBQ_PRODUCER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_mfsbq_cons_reg = MFSBQ_CONSUMER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_mfsbuff_len_reg = MFS_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sfsbq_base_reg = SFSBQ_BASE_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sfsbq_len_reg = SFSBQ_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sfsbq_prod_reg = SFSBQ_PRODUCER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sfsbq_cons_reg = SFSBQ_CONSUMER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sfsbuff_len_reg = SFS_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sest_base_reg = SEST_BASE_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_sest_len_reg = SEST_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_scsibuff_len_reg = SCSI_LENGTH_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_tach_config_reg = TACHYON_CONFIG_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_tach_control_reg = TACHYON_CONTROL_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_tach_status_reg = TACHYON_STATUS_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_tach_flush_oxid_reg = TACHYON_FLUSH_SEST_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_config_reg = FMGR_CONFIG_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_control_reg = FMGR_CONTROL_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_status_reg = FMGR_STATUS_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_tov_reg = FMGR_TIMER_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_wwn_hi_reg = FMGR_WWN_HI_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_wwn_low_reg = FMGR_WWN_LO_REGISTER_OFFSET + fi->g.tachyon_base;
fi->t_r.ptr_fm_rx_al_pa_reg = FMGR_RCVD_ALPA_REGISTER_OFFSET + fi->g.tachyon_base;

LEAVE("initialize_register_pointers");
return 1;
}



/*
 * Local variables:
 *  compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c iph5526.c"
 *  version-control: t
 *  kept-new-versions: 5
 * End:
 */