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
/* 
 * Core I2O structure managment 
 * 
 * (C) Copyright 1999   Red Hat Software 
 *
 * Written by Alan Cox, Building Number Three Ltd 
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 
 * 2 of the License, or (at your option) any later version.  
 * 
 * A lot of the I2O message side code from this is taken from the 
 * Red Creek RCPCI45 adapter driver by Red Creek Communications 
 * 
 * Fixes by: 
 *		Philipp Rumpf 
 *		Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI> 
 *		Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI> 
 *		Deepak Saxena <deepak@plexity.net> 
 *		Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
 * 
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>

#include <linux/i2o.h>

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/smp_lock.h>

#include <linux/bitops.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/tqueue.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <asm/semaphore.h>

#include <asm/io.h>
#include <linux/reboot.h>

#include "i2o_lan.h"

// #define DRIVERDEBUG

#ifdef DRIVERDEBUG
#define dprintk(s, args...) printk(s, ## args)
#else
#define dprintk(s, args...)
#endif

/* OSM table */
static struct i2o_handler *i2o_handlers[MAX_I2O_MODULES] = {NULL};

/* Controller list */
static struct i2o_controller *i2o_controllers[MAX_I2O_CONTROLLERS] = {NULL};
struct i2o_controller *i2o_controller_chain = NULL;
int i2o_num_controllers = 0;

/* Initiator Context for Core message */
static int core_context = 0;

/* Initialization && shutdown functions */
static void i2o_sys_init(void);
static void i2o_sys_shutdown(void);
static int i2o_reset_controller(struct i2o_controller *);
static int i2o_reboot_event(struct notifier_block *, unsigned long , void *);
static int i2o_online_controller(struct i2o_controller *);
static int i2o_init_outbound_q(struct i2o_controller *);
static int i2o_post_outbound_messages(struct i2o_controller *);

/* Reply handler */
static void i2o_core_reply(struct i2o_handler *, struct i2o_controller *,
			   struct i2o_message *);

/* Various helper functions */
static int i2o_lct_get(struct i2o_controller *);
static int i2o_lct_notify(struct i2o_controller *);
static int i2o_hrt_get(struct i2o_controller *);

static int i2o_build_sys_table(void);
static int i2o_systab_send(struct i2o_controller *c);

/* I2O core event handler */
static int i2o_core_evt(void *);
static int evt_pid;
static int evt_running;

/* Dynamic LCT update handler */
static int i2o_dyn_lct(void *);

void i2o_report_controller_unit(struct i2o_controller *, struct i2o_device *);

/*
 * I2O System Table.  Contains information about
 * all the IOPs in the system.  Used to inform IOPs
 * about each other's existence.
 *
 * sys_tbl_ver is the CurrentChangeIndicator that is
 * used by IOPs to track changes.
 */
static struct i2o_sys_tbl *sys_tbl = NULL;
static int sys_tbl_ind = 0;
static int sys_tbl_len = 0;

/*
 * This spin lock is used to keep a device from being
 * added and deleted concurrently across CPUs or interrupts.
 * This can occur when a user creates a device and immediatelly
 * deletes it before the new_dev_notify() handler is called.
 */
static spinlock_t i2o_dev_lock = SPIN_LOCK_UNLOCKED;

#ifdef MODULE
/* 
 * Function table to send to bus specific layers
 * See <include/linux/i2o.h> for explanation of this
 */
static struct i2o_core_func_table i2o_core_functions =
{
	i2o_install_controller,
	i2o_activate_controller,
	i2o_find_controller,
	i2o_unlock_controller,
	i2o_run_queue,
	i2o_delete_controller
};

#ifdef CONFIG_I2O_PCI_MODULE
extern int i2o_pci_core_attach(struct i2o_core_func_table *);
extern void i2o_pci_core_detach(void);
#endif /* CONFIG_I2O_PCI_MODULE */

#endif /* MODULE */

/*
 * Structures and definitions for synchronous message posting.
 * See i2o_post_wait() for description.
 */ 
struct i2o_post_wait_data
{
	int status;
	u32 id;
	wait_queue_head_t *wq;
	struct i2o_post_wait_data *next;
};
static struct i2o_post_wait_data *post_wait_queue = NULL;
static u32 post_wait_id = 0;	// Unique ID for each post_wait
static spinlock_t post_wait_lock = SPIN_LOCK_UNLOCKED;
static void i2o_post_wait_complete(u32, int);

/* OSM descriptor handler */ 
static struct i2o_handler i2o_core_handler =
{
	(void *)i2o_core_reply,
	NULL,
	NULL,
	NULL,
	"I2O core layer",
	0,
	I2O_CLASS_EXECUTIVE
};


/*
 * Used when queing a reply to be handled later
 */
struct reply_info
{
	struct i2o_controller *iop;
	u32 msg[MSG_FRAME_SIZE];
};
static struct reply_info evt_reply;
static struct reply_info events[I2O_EVT_Q_LEN];
static int evt_in = 0;
static int evt_out = 0;
static int evt_q_len = 0;
#define MODINC(x,y) ((x) = ((x) + 1) % (y))

/*
 * I2O configuration spinlock. This isnt a big deal for contention
 * so we have one only
 */

static DECLARE_MUTEX(i2o_configuration_lock);

/* 
 * Event spinlock.  Used to keep event queue sane and from
 * handling multiple events simultaneously.
 */
static spinlock_t i2o_evt_lock = SPIN_LOCK_UNLOCKED;

/*
 * Semaphore used to syncrhonize event handling thread with 
 * interrupt handler.
 */
DECLARE_MUTEX(evt_sem);
DECLARE_WAIT_QUEUE_HEAD(evt_wait);

static struct notifier_block i2o_reboot_notifier =
{
        i2o_reboot_event,
        NULL,
        0
};


/*
 * I2O Core reply handler
 */
static void i2o_core_reply(struct i2o_handler *h, struct i2o_controller *c,
		    struct i2o_message *m)
{
	u32 *msg=(u32 *)m;
	u32 status;
	u32 context = msg[2];

	if (msg[0] & MSG_FAIL) // Fail bit is set
	{
		u32 *preserved_msg = (u32*)(c->mem_offset + msg[7]);

		i2o_report_status(KERN_INFO, "i2o_core", msg);
		i2o_dump_message(preserved_msg);

		/* If the failed request needs special treatment,
		 * it should be done here. */

                /* Release the preserved msg by resubmitting it as a NOP */

		preserved_msg[0] = THREE_WORD_MSG_SIZE | SGL_OFFSET_0;
		preserved_msg[1] = I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0;
		preserved_msg[2] = 0;
		i2o_post_message(c, msg[7]);

		/* If reply to i2o_post_wait failed, return causes a timeout */

		return;
	}       

#ifdef DRIVERDEBUG
	i2o_report_status(KERN_INFO, "i2o_core", msg);
#endif

	if(msg[2]&0x80000000)	// Post wait message
	{
		if (msg[4] >> 24)
			status = -(msg[4] & 0xFFFF);
		else
			status = I2O_POST_WAIT_OK;
	
		i2o_post_wait_complete(context, status);
		return;
	}

	if(m->function == I2O_CMD_UTIL_EVT_REGISTER)
	{
		memcpy(events[evt_in].msg, msg, (msg[0]>>16)<<2);
		events[evt_in].iop = c;

		spin_lock(&i2o_evt_lock);
		MODINC(evt_in, I2O_EVT_Q_LEN);
		if(evt_q_len == I2O_EVT_Q_LEN)
			MODINC(evt_out, I2O_EVT_Q_LEN);
		else
			evt_q_len++;
		spin_unlock(&i2o_evt_lock);

		up(&evt_sem);
		wake_up_interruptible(&evt_wait);
		return;
	}

	if(m->function == I2O_CMD_LCT_NOTIFY)
	{
		up(&c->lct_sem);
		return;
	}

	/*
	 * If this happens, we want to dump the message to the syslog so
	 * it can be sent back to the card manufacturer by the end user
	 * to aid in debugging.
	 * 
	 */
	printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
			"Message dumped to syslog\n", 
			c->name);
	i2o_dump_message(msg);

	return;
}

/**
 *	i2o_install_handler - install a message handler
 *	@h: Handler structure
 *
 *	Install an I2O handler - these handle the asynchronous messaging
 *	from the card once it has initialised. If the table of handlers is
 *	full then -ENOSPC is returned. On a success 0 is returned and the
 *	context field is set by the function. The structure is part of the
 *	system from this time onwards. It must not be freed until it has
 *	been uninstalled
 */
 
int i2o_install_handler(struct i2o_handler *h)
{
	int i;
	down(&i2o_configuration_lock);
	for(i=0;i<MAX_I2O_MODULES;i++)
	{
		if(i2o_handlers[i]==NULL)
		{
			h->context = i;
			i2o_handlers[i]=h;
			up(&i2o_configuration_lock);
			return 0;
		}
	}
	up(&i2o_configuration_lock);
	return -ENOSPC;
}

/**
 *	i2o_remove_handler - remove an i2o message handler
 *	@h: handler
 *
 *	Remove a message handler previously installed with i2o_install_handler.
 *	After this function returns the handler object can be freed or re-used
 */
 
int i2o_remove_handler(struct i2o_handler *h)
{
	i2o_handlers[h->context]=NULL;
	return 0;
}
	

/*
 *	Each I2O controller has a chain of devices on it.
 * Each device has a pointer to it's LCT entry to be used
 * for fun purposes.
 */

/**
 *	i2o_install_device	-	attach a device to a controller
 *	@c: controller
 *	@d: device
 * 	
 *	Add a new device to an i2o controller. This can be called from
 *	non interrupt contexts only. It adds the device and marks it as
 *	unclaimed. The device memory becomes part of the kernel and must
 *	be uninstalled before being freed or reused. Zero is returned
 *	on success.
 */
 
int i2o_install_device(struct i2o_controller *c, struct i2o_device *d)
{
	int i;

	down(&i2o_configuration_lock);
	d->controller=c;
	d->owner=NULL;
	d->next=c->devices;
	c->devices=d;
	*d->dev_name = 0;

	for(i = 0; i < I2O_MAX_MANAGERS; i++)
		d->managers[i] = NULL;

	up(&i2o_configuration_lock);
	return 0;
}

/* we need this version to call out of i2o_delete_controller */

int __i2o_delete_device(struct i2o_device *d)
{
	struct i2o_device **p;
	int i;

	p=&(d->controller->devices);

	/*
	 *	Hey we have a driver!
	 * Check to see if the driver wants us to notify it of 
	 * device deletion. If it doesn't we assume that it
	 * is unsafe to delete a device with an owner and 
	 * fail.
	 */
	if(d->owner)
	{
		if(d->owner->dev_del_notify)
		{
			dprintk(KERN_INFO "Device has owner, notifying\n");
			d->owner->dev_del_notify(d->controller, d);
			if(d->owner)
			{
				printk(KERN_WARNING 
					"Driver \"%s\" did not release device!\n", d->owner->name);
				return -EBUSY;
			}
		}
		else
			return -EBUSY;
	}

	/*
	 * Tell any other users who are talking to this device
	 * that it's going away.  We assume that everything works.
	 */
	for(i=0; i < I2O_MAX_MANAGERS; i++)
	{
		if(d->managers[i] && d->managers[i]->dev_del_notify)
			d->managers[i]->dev_del_notify(d->controller, d);
	}
	 			
	while(*p!=NULL)
	{
		if(*p==d)
		{
			/*
			 *	Destroy
			 */
			*p=d->next;
			kfree(d);
			return 0;
		}
		p=&((*p)->next);
	}
	printk(KERN_ERR "i2o_delete_device: passed invalid device.\n");
	return -EINVAL;
}

/**
 *	i2o_delete_device	-	remove an i2o device
 *	@d: device to remove
 *
 *	This function unhooks a device from a controller. The device
 *	will not be unhooked if it has an owner who does not wish to free
 *	it, or if the owner lacks a dev_del_notify function. In that case
 *	-EBUSY is returned. On success 0 is returned. Other errors cause
 *	negative errno values to be returned
 */
 
int i2o_delete_device(struct i2o_device *d)
{
	int ret;

	down(&i2o_configuration_lock);

	/*
	 *	Seek, locate
	 */

	ret = __i2o_delete_device(d);

	up(&i2o_configuration_lock);

	return ret;
}

/**
 *	i2o_install_controller	-	attach a controller
 *	@c: controller
 * 	
 *	Add a new controller to the i2o layer. This can be called from
 *	non interrupt contexts only. It adds the controller and marks it as
 *	unused with no devices. If the tables are full or memory allocations
 *	fail then a negative errno code is returned. On success zero is
 *	returned and the controller is bound to the system. The structure
 *	must not be freed or reused until being uninstalled.
 */
 
int i2o_install_controller(struct i2o_controller *c)
{
	int i;
	down(&i2o_configuration_lock);
	for(i=0;i<MAX_I2O_CONTROLLERS;i++)
	{
		if(i2o_controllers[i]==NULL)
		{
			c->dlct = (i2o_lct*)kmalloc(8192, GFP_KERNEL);
			if(c->dlct==NULL)
			{
				up(&i2o_configuration_lock);
				return -ENOMEM;
			}
			i2o_controllers[i]=c;
			c->devices = NULL;
			c->next=i2o_controller_chain;
			i2o_controller_chain=c;
			c->unit = i;
			c->page_frame = NULL;
			c->hrt = NULL;
			c->lct = NULL;
			c->status_block = NULL;
			sprintf(c->name, "i2o/iop%d", i);
			i2o_num_controllers++;
			init_MUTEX_LOCKED(&c->lct_sem);
			up(&i2o_configuration_lock);
			return 0;
		}
	}
	printk(KERN_ERR "No free i2o controller slots.\n");
	up(&i2o_configuration_lock);
	return -EBUSY;
}

/**
 *	i2o_delete_controller	- delete a controller
 *	@c: controller
 *	
 *	Remove an i2o controller from the system. If the controller or its
 *	devices are busy then -EBUSY is returned. On a failure a negative
 *	errno code is returned. On success zero is returned.
 */
  
int i2o_delete_controller(struct i2o_controller *c)
{
	struct i2o_controller **p;
	int users;
	char name[16];
	int stat;

	dprintk(KERN_INFO "Deleting controller %s\n", c->name);

	/*
	 * Clear event registration as this can cause weird behavior
	 */
	if(c->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
		i2o_event_register(c, core_context, 0, 0, 0);

	down(&i2o_configuration_lock);
	if((users=atomic_read(&c->users)))
	{
		dprintk(KERN_INFO "I2O: %d users for controller %s\n", users,
			c->name);
		up(&i2o_configuration_lock);
		return -EBUSY;
	}
	while(c->devices)
	{
		if(__i2o_delete_device(c->devices)<0)
		{
			/* Shouldnt happen */
			c->bus_disable(c);
			up(&i2o_configuration_lock);
			return -EBUSY;
		}
	}

	/*
	 * If this is shutdown time, the thread's already been killed
	 */
	if(c->lct_running) {
		stat = kill_proc(c->lct_pid, SIGTERM, 1);
		if(!stat) {
			int count = 10 * 100;
			while(c->lct_running && --count) {
				current->state = TASK_INTERRUPTIBLE;
				schedule_timeout(1);
			}
		
			if(!count)
				printk(KERN_ERR 
					"%s: LCT thread still running!\n", 
					c->name);
		}
	}

	p=&i2o_controller_chain;

	while(*p)
	{
		if(*p==c)
		{
 			/* Ask the IOP to switch to RESET state */
			i2o_reset_controller(c);

			/* Release IRQ */
			c->destructor(c);

			*p=c->next;
			up(&i2o_configuration_lock);

			if(c->page_frame)
				kfree(c->page_frame);
			if(c->hrt)
				kfree(c->hrt);
			if(c->lct)
				kfree(c->lct);
			if(c->status_block)
				kfree(c->status_block);
			if(c->dlct)
				kfree(c->dlct);

			i2o_controllers[c->unit]=NULL;
			memcpy(name, c->name, strlen(c->name)+1);
			kfree(c);
			dprintk(KERN_INFO "%s: Deleted from controller chain.\n", name);
			
			i2o_num_controllers--;
			return 0;
		}
		p=&((*p)->next);
	}
	up(&i2o_configuration_lock);
	printk(KERN_ERR "i2o_delete_controller: bad pointer!\n");
	return -ENOENT;
}

/**
 *	i2o_unlock_controller	-	unlock a controller
 *	@c: controller to unlock
 *
 *	Take a lock on an i2o controller. This prevents it being deleted.
 *	i2o controllers are not refcounted so a deletion of an in use device
 *	will fail, not take affect on the last dereference.
 */
 
void i2o_unlock_controller(struct i2o_controller *c)
{
	atomic_dec(&c->users);
}

/**
 *	i2o_find_controller - return a locked controller
 *	@n: controller number
 *
 *	Returns a pointer to the controller object. The controller is locked
 *	on return. NULL is returned if the controller is not found.
 */
 
struct i2o_controller *i2o_find_controller(int n)
{
	struct i2o_controller *c;
	
	if(n<0 || n>=MAX_I2O_CONTROLLERS)
		return NULL;
	
	down(&i2o_configuration_lock);
	c=i2o_controllers[n];
	if(c!=NULL)
		atomic_inc(&c->users);
	up(&i2o_configuration_lock);
	return c;
}

/**
 *	i2o_issue_claim	- claim or release a device
 *	@cmd: command
 *	@c: controller to claim for
 *	@tid: i2o task id
 *	@type: type of claim
 *
 *	Issue I2O UTIL_CLAIM and UTIL_RELEASE messages. The message to be sent
 *	is set by cmd. The tid is the task id of the object to claim and the
 *	type is the claim type (see the i2o standard)
 *
 *	Zero is returned on success.
 */
 
static int i2o_issue_claim(u32 cmd, struct i2o_controller *c, int tid, u32 type)
{
	u32 msg[5];

	msg[0] = FIVE_WORD_MSG_SIZE | SGL_OFFSET_0;
	msg[1] = cmd << 24 | HOST_TID<<12 | tid;
	msg[3] = 0;
	msg[4] = type;
	
	return i2o_post_wait(c, msg, sizeof(msg), 60);
}

/*
 * 	i2o_claim_device - claim a device for use by an OSM
 *	@d: device to claim
 *	@h: handler for this device
 *
 *	Do the leg work to assign a device to a given OSM on Linux. The
 *	kernel updates the internal handler data for the device and then
 *	performs an I2O claim for the device, attempting to claim the
 *	device as primary. If the attempt fails a negative errno code
 *	is returned. On success zero is returned.
 */
 
int i2o_claim_device(struct i2o_device *d, struct i2o_handler *h)
{
	down(&i2o_configuration_lock);
	if (d->owner) {
		printk(KERN_INFO "Device claim called, but dev allready owned by %s!",
		       h->name);
		up(&i2o_configuration_lock);
		return -EBUSY;
	}
	d->owner=h;

	if(i2o_issue_claim(I2O_CMD_UTIL_CLAIM ,d->controller,d->lct_data.tid, 
			   I2O_CLAIM_PRIMARY))
	{
		d->owner = NULL;
		return -EBUSY;
	}
	up(&i2o_configuration_lock);
	return 0;
}

/**
 *	i2o_release_device - release a device that the OSM is using
 *	@d: device to claim
 *	@h: handler for this device
 *
 *	Drop a claim by an OSM on a given I2O device. The handler is cleared
 *	and 0 is returned on success.
 *
 */

int i2o_release_device(struct i2o_device *d, struct i2o_handler *h)
{
	int err = 0;

	down(&i2o_configuration_lock);
	if (d->owner != h) {
		printk(KERN_INFO "Claim release called, but not owned by %s!",
		       h->name);
		up(&i2o_configuration_lock);
		return -ENOENT;
	}	

	d->owner = NULL;

	if(i2o_issue_claim(I2O_CMD_UTIL_RELEASE, d->controller, d->lct_data.tid, 
			   I2O_CLAIM_PRIMARY))
	{
		err = -ENXIO;
		d->owner = h;
	}

	up(&i2o_configuration_lock);
	return err;
}

/*
 * Called by OSMs to let the core know that they want to be
 * notified if the given device is deleted from the system.
 */
int i2o_device_notify_on(struct i2o_device *d, struct i2o_handler *h)
{
	int i;

	if(d->num_managers == I2O_MAX_MANAGERS)
		return -ENOSPC;

	for(i = 0; i < I2O_MAX_MANAGERS; i++)
	{
		if(!d->managers[i])
		{
			d->managers[i] = h;
			break;
		}
	}
	
	d->num_managers++;
	
	return 0;
}

/*
 * Called by OSMs to let the core know that they no longer
 * are interested in the fate of the given device.
 */
int i2o_device_notify_off(struct i2o_device *d, struct i2o_handler *h)
{
	int i;

	for(i=0; i < I2O_MAX_MANAGERS; i++)
	{
		if(d->managers[i] == h)
		{
			d->managers[i] = NULL;
			d->num_managers--;
			return 0;
		}
	}

	return -ENOENT;
}

/*
 * Event registration API
 */
int i2o_event_register(struct i2o_controller *c, u32 tid, 
		u32 init_context, u32 tr_context, u32 evt_mask)
{
	u32 msg[5];	// Not performance critical, so we just 
			// i2o_post_this it instead of building it
			// in IOP memory
	
	msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1] = I2O_CMD_UTIL_EVT_REGISTER<<24 | HOST_TID<<12 | tid;
	msg[2] = init_context;
	msg[3] = tr_context;
	msg[4] = evt_mask;

	return i2o_post_this(c, msg, sizeof(msg));
}

/*
 * Event ack API
 *
 * We just take a pointer to the original UTIL_EVENT_REGISTER reply
 * message and change the function code since that's what spec
 * describes an EventAck message looking like.
 */
 
int i2o_event_ack(struct i2o_controller *c, u32 *msg)
{
	struct i2o_message *m = (struct i2o_message *)msg;

	m->function = I2O_CMD_UTIL_EVT_ACK;

	return i2o_post_wait(c, msg, m->size * 4, 2);
}

/*
 * Core event handler.  Runs as a separate thread and is woken
 * up whenever there is an Executive class event.
 */
static int i2o_core_evt(void *reply_data)
{
	struct reply_info *reply = (struct reply_info *) reply_data;
	u32 *msg = reply->msg;
	struct i2o_controller *c = NULL;
	int flags;

	lock_kernel();
	daemonize();
	unlock_kernel();

	strcpy(current->comm, "i2oevtd");
	evt_running = 1;

	while(1)
	{
		down_interruptible(&evt_sem);
		if(signal_pending(current))
		{
			dprintk(KERN_INFO "I2O event thread dead\n");
			evt_running = 0;
			return 0;	
		}

		/* 
		 * Copy the data out of the queue so that we don't have to lock
		 * around the whole function and just around the qlen update
		 */
		spin_lock_irqsave(&i2o_evt_lock, flags);
		memcpy(reply, &events[evt_out], sizeof(struct reply_info));
		MODINC(evt_out, I2O_EVT_Q_LEN);
		evt_q_len--;
		spin_unlock_irqrestore(&i2o_evt_lock, flags);
	
		c = reply->iop;
	 	dprintk(KERN_INFO "I2O IRTOS EVENT: iop%d, event %#10x\n", c->unit, msg[4]);

		/* 
		 * We do not attempt to delete/quiesce/etc. the controller if
		 * some sort of error indidication occurs.  We may want to do
		 * so in the future, but for now we just let the user deal with 
		 * it.  One reason for this is that what to do with an error
		 * or when to send what ærror is not really agreed on, so
		 * we get errors that may not be fatal but just look like they
		 * are...so let the user deal with it.
		 */
		switch(msg[4])
		{
			case I2O_EVT_IND_EXEC_RESOURCE_LIMITS:
				printk(KERN_ERR "%s: Out of resources\n", c->name);
				break;

			case I2O_EVT_IND_EXEC_POWER_FAIL:
				printk(KERN_ERR "%s: Power failure\n", c->name);
				break;

			case I2O_EVT_IND_EXEC_HW_FAIL:
			{
				char *fail[] = 
					{ 
						"Unknown Error",
						"Power Lost",
						"Code Violation",
						"Parity Error",
						"Code Execution Exception",
						"Watchdog Timer Expired" 
					};

				if(msg[5] <= 6)
					printk(KERN_ERR "%s: Hardware Failure: %s\n", 
						c->name, fail[msg[5]]);
				else
					printk(KERN_ERR "%s: Unknown Hardware Failure\n", c->name);

				break;
			}

			/*
		 	 * New device created
		 	 * - Create a new i2o_device entry
		 	 * - Inform all interested drivers about this device's existence
		 	 */
			case I2O_EVT_IND_EXEC_NEW_LCT_ENTRY:
			{
				struct i2o_device *d = (struct i2o_device *)
					kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
				int i;

				memcpy(&d->lct_data, &msg[5], sizeof(i2o_lct_entry));
	
				d->next = NULL;
				d->controller = c;
				d->flags = 0;
	
				i2o_report_controller_unit(c, d);
				i2o_install_device(c,d);
	
				for(i = 0; i < MAX_I2O_MODULES; i++)
				{
					if(i2o_handlers[i] && 
						i2o_handlers[i]->new_dev_notify &&
						(i2o_handlers[i]->class&d->lct_data.class_id))
						{
						spin_lock(&i2o_dev_lock);
						i2o_handlers[i]->new_dev_notify(c,d);
						spin_unlock(&i2o_dev_lock);
						}
				}
			
				break;
			}
	
			/*
 		 	 * LCT entry for a device has been modified, so update it
		 	 * internally.
		 	 */
			case I2O_EVT_IND_EXEC_MODIFIED_LCT:
			{
				struct i2o_device *d;
				i2o_lct_entry *new_lct = (i2o_lct_entry *)&msg[5];

				for(d = c->devices; d; d = d->next)
				{
					if(d->lct_data.tid == new_lct->tid)
					{
						memcpy(&d->lct_data, new_lct, sizeof(i2o_lct_entry));
						break;
					}
				}
				break;
			}
	
			case I2O_EVT_IND_CONFIGURATION_FLAG:
				printk(KERN_WARNING "%s requires user configuration\n", c->name);
				break;
	
			case I2O_EVT_IND_GENERAL_WARNING:
				printk(KERN_WARNING "%s: Warning notification received!"
					"Check configuration for errors!\n", c->name);
				break;
	
			default:
				printk(KERN_WARNING "%s: No handler for event (0x%08x)\n", c->name, msg[4]);
				break;
		}
	}

	return 0;
}

/*
 * Dynamic LCT update.  This compares the LCT with the currently
 * installed devices to check for device deletions..this needed b/c there
 * is no DELETED_LCT_ENTRY EventIndicator for the Executive class so
 * we can't just have the event handler do this...annoying
 *
 * This is a hole in the spec that will hopefully be fixed someday.
 */
static int i2o_dyn_lct(void *foo)
{
	struct i2o_controller *c = (struct i2o_controller *)foo;
	struct i2o_device *d = NULL;
	struct i2o_device *d1 = NULL;
	int i = 0;
	int found = 0;
	int entries;
	void *tmp;
	char name[16];

	lock_kernel();
	daemonize();
	unlock_kernel();

	sprintf(name, "iop%d_lctd", c->unit);
	strcpy(current->comm, name);	
	
	c->lct_running = 1;

	while(1)
	{
		down_interruptible(&c->lct_sem);
		if(signal_pending(current))
		{
			dprintk(KERN_ERR "%s: LCT thread dead\n", c->name);
			c->lct_running = 0;
			return 0;
		}

		entries = c->dlct->table_size;
		entries -= 3;
		entries /= 9;

		dprintk(KERN_INFO "%s: Dynamic LCT Update\n",c->name);
		dprintk(KERN_INFO "%s: Dynamic LCT contains %d entries\n", c->name, entries);

		if(!entries)
		{
			printk(KERN_INFO "%s: Empty LCT???\n", c->name);
			continue;
		}

		/*
		 * Loop through all the devices on the IOP looking for their
		 * LCT data in the LCT.  We assume that TIDs are not repeated.
		 * as that is the only way to really tell.  It's been confirmed
		 * by the IRTOS vendor(s?) that TIDs are not reused until they 
		 * wrap arround(4096), and I doubt a system will up long enough
		 * to create/delete that many devices.
		 */
		for(d = c->devices; d; )
		{
			found = 0;
			d1 = d->next;
			
			for(i = 0; i < entries; i++) 
			{ 
				if(d->lct_data.tid == c->dlct->lct_entry[i].tid) 
				{ 
					found = 1; 
					break; 
				} 
			} 
			if(!found) 
			{
				dprintk(KERN_INFO "i2o_core: Deleted device!\n"); 
				spin_lock(&i2o_dev_lock);
				i2o_delete_device(d); 
				spin_unlock(&i2o_dev_lock);
			} 
			d = d1; 
		}

		/* 
		 * Tell LCT to renotify us next time there is a change
	 	 */
		i2o_lct_notify(c);

		/*
		 * Copy new LCT into public LCT
		 *
		 * Possible race if someone is reading LCT while  we are copying 
		 * over it. If this happens, we'll fix it then. but I doubt that
		 * the LCT will get updated often enough or will get read by
		 * a user often enough to worry.
		 */
		if(c->lct->table_size < c->dlct->table_size)
		{
			tmp = c->lct;
			c->lct = kmalloc(c->dlct->table_size<<2, GFP_KERNEL);
			if(!c->lct)
			{
				printk(KERN_ERR "%s: No memory for LCT!\n", c->name);
				c->lct = tmp;
				continue;
			}
			kfree(tmp);
		}
		memcpy(c->lct, c->dlct, c->dlct->table_size<<2);
	}

	return 0;
}

/**
 *	i2o_run_queue	-	process pending events on a controller
 *	@c: controller to process
 *
 *	This is called by the bus specific driver layer when an interrupt
 *	or poll of this card interface is desired.
 */
 
void i2o_run_queue(struct i2o_controller *c)
{
	struct i2o_message *m;
	u32 mv;
	u32 *msg;

	/*
	 * Old 960 steppings had a bug in the I2O unit that caused
	 * the queue to appear empty when it wasn't.
	 */
	if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
		mv=I2O_REPLY_READ32(c);

	while(mv!=0xFFFFFFFF)
	{
		struct i2o_handler *i;
		m=(struct i2o_message *)bus_to_virt(mv);
		msg=(u32*)m;

		/*
		 *	Temporary Debugging
		 */
		if(m->function==0x15)
			printk(KERN_ERR "%s: UTFR!\n", c->name);

		i=i2o_handlers[m->initiator_context&(MAX_I2O_MODULES-1)];
		if(i && i->reply)
			i->reply(i,c,m);
		else
		{
			printk(KERN_WARNING "I2O: Spurious reply to handler %d\n", 
				m->initiator_context&(MAX_I2O_MODULES-1));
		}	
	 	i2o_flush_reply(c,mv);
		mb();

		/* That 960 bug again... */	
		if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
			mv=I2O_REPLY_READ32(c);
	}		
}


/**
 *	i2o_get_class_name - 	do i2o class name lookup
 *	@class: class number
 *
 *	Return a descriptive string for an i2o class
 */
 
const char *i2o_get_class_name(int class)
{
	int idx = 16;
	static char *i2o_class_name[] = {
		"Executive",
		"Device Driver Module",
		"Block Device",
		"Tape Device",
		"LAN Interface",
		"WAN Interface",
		"Fibre Channel Port",
		"Fibre Channel Device",
		"SCSI Device",
		"ATE Port",
		"ATE Device",
		"Floppy Controller",
		"Floppy Device",
		"Secondary Bus Port",
		"Peer Transport Agent",
		"Peer Transport",
		"Unknown"
	};
	
	switch(class&0xFFF)
	{
		case I2O_CLASS_EXECUTIVE:
			idx = 0; break;
		case I2O_CLASS_DDM:
			idx = 1; break;
		case I2O_CLASS_RANDOM_BLOCK_STORAGE:
			idx = 2; break;
		case I2O_CLASS_SEQUENTIAL_STORAGE:
			idx = 3; break;
		case I2O_CLASS_LAN:
			idx = 4; break;
		case I2O_CLASS_WAN:
			idx = 5; break;
		case I2O_CLASS_FIBRE_CHANNEL_PORT:
			idx = 6; break;
		case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
			idx = 7; break;
		case I2O_CLASS_SCSI_PERIPHERAL:
			idx = 8; break;
		case I2O_CLASS_ATE_PORT:
			idx = 9; break;
		case I2O_CLASS_ATE_PERIPHERAL:
			idx = 10; break;
		case I2O_CLASS_FLOPPY_CONTROLLER:
			idx = 11; break;
		case I2O_CLASS_FLOPPY_DEVICE:
			idx = 12; break;
		case I2O_CLASS_BUS_ADAPTER_PORT:
			idx = 13; break;
		case I2O_CLASS_PEER_TRANSPORT_AGENT:
			idx = 14; break;
		case I2O_CLASS_PEER_TRANSPORT:
			idx = 15; break;
	}

	return i2o_class_name[idx];
}


/**
 *	i2o_wait_message
 *	@c: controller
 *	@why: explanation
 *
 *	This function waits up to 5 seconds for a message slot to be
 *	available. If no message is available it prints an error message
 *	that is expected to be what the message will be used for (eg
 *	"get_status"). 0xFFFFFFFF is returned on a failure.
 *
 *	On a success the message is returned. This is the physical page
 *	frame offset address from the read port. (See the i2o spec)
 */
 
u32 i2o_wait_message(struct i2o_controller *c, char *why)
{
	long time=jiffies;
	u32 m;
	while((m=I2O_POST_READ32(c))==0xFFFFFFFF)
	{
		if((jiffies-time)>=5*HZ)
		{
			dprintk(KERN_ERR "%s: Timeout waiting for message frame to send %s.\n", 
				c->name, why);
			return 0xFFFFFFFF;
		}
		schedule();
		barrier();
	}
	return m;
}
	
/**
 *	i2o_report_controller_unit - print information about a tid
 *	@c: controller
 *	@d: device
 *	
 *	Dump an information block associated with a given unit (TID). The
 *	tables are read and a block of text is output to printk that is
 *	formatted intended for the user.
 */
 
void i2o_report_controller_unit(struct i2o_controller *c, struct i2o_device *d)
{
	char buf[64];
	char str[22];
	int ret;
	int unit = d->lct_data.tid;
	
	printk(KERN_INFO "Target ID %d.\n", unit);

	if((ret=i2o_query_scalar(c, unit, 0xF100, 3, buf, 16))>=0)
	{
		buf[16]=0;
		printk(KERN_INFO "     Vendor: %s\n", buf);
	}
	if((ret=i2o_query_scalar(c, unit, 0xF100, 4, buf, 16))>=0)
	{
		buf[16]=0;
		printk(KERN_INFO "     Device: %s\n", buf);
	}
#if 0
	if(i2o_query_scalar(c, unit, 0xF100, 5, buf, 16)>=0)
	{
		buf[16]=0;
		printk(KERN_INFO "     Description: %s\n", buf);
	}
#endif	
	if((ret=i2o_query_scalar(c, unit, 0xF100, 6, buf, 8))>=0)
	{
		buf[8]=0;
		printk(KERN_INFO "        Rev: %s\n", buf);
	}

	printk(KERN_INFO "    Class: ");
	sprintf(str, "%-21s", i2o_get_class_name(d->lct_data.class_id));
	printk("%s\n", str);
		
	printk(KERN_INFO "  Subclass: 0x%04X\n", d->lct_data.sub_class);
	printk(KERN_INFO "     Flags: ");
		
	if(d->lct_data.device_flags&(1<<0))
		printk("C");		// ConfigDialog requested
	if(d->lct_data.device_flags&(1<<1))
		printk("U");		// Multi-user capable
	if(!(d->lct_data.device_flags&(1<<4)))
		printk("P");		// Peer service enabled!
	if(!(d->lct_data.device_flags&(1<<5)))
		printk("M");		// Mgmt service enabled!
	printk("\n");
			
}


/*
 *	Parse the hardware resource table. Right now we print it out
 *	and don't do a lot with it. We should collate these and then
 *	interact with the Linux resource allocation block.
 *
 *	Lets prove we can read it first eh ?
 *
 *	This is full of endianisms!
 */
 
static int i2o_parse_hrt(struct i2o_controller *c)
{
#ifdef DRIVERDEBUG
	u32 *rows=(u32*)c->hrt;
	u8 *p=(u8 *)c->hrt;
	u8 *d;
	int count;
	int length;
	int i;
	int state;
	
	if(p[3]!=0)
	{
		printk(KERN_ERR "%s: HRT table for controller is too new a version.\n",
			c->name);
		return -1;
	}
		
	count=p[0]|(p[1]<<8);
	length = p[2];
	
	printk(KERN_INFO "%s: HRT has %d entries of %d bytes each.\n",
		c->name, count, length<<2);

	rows+=2;
	
	for(i=0;i<count;i++)
	{
		printk(KERN_INFO "Adapter %08X: ", rows[0]);
		p=(u8 *)(rows+1);
		d=(u8 *)(rows+2);
		state=p[1]<<8|p[0];
		
		printk("TID %04X:[", state&0xFFF);
		state>>=12;
		if(state&(1<<0))
			printk("H");		/* Hidden */
		if(state&(1<<2))
		{
			printk("P");		/* Present */
			if(state&(1<<1))
				printk("C");	/* Controlled */
		}
		if(state>9)
			printk("*");		/* Hard */
		
		printk("]:");
		
		switch(p[3]&0xFFFF)
		{
			case 0:
				/* Adapter private bus - easy */
				printk("Local bus %d: I/O at 0x%04X Mem 0x%08X", 
					p[2], d[1]<<8|d[0], *(u32 *)(d+4));
				break;
			case 1:
				/* ISA bus */
				printk("ISA %d: CSN %d I/O at 0x%04X Mem 0x%08X",
					p[2], d[2], d[1]<<8|d[0], *(u32 *)(d+4));
				break;
					
			case 2: /* EISA bus */
				printk("EISA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
					p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
				break;

			case 3: /* MCA bus */
				printk("MCA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
					p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
				break;

			case 4: /* PCI bus */
				printk("PCI %d: Bus %d Device %d Function %d",
					p[2], d[2], d[1], d[0]);
				break;

			case 0x80: /* Other */
			default:
				printk("Unsupported bus type.");
				break;
		}
		printk("\n");
		rows+=length;
	}
#endif
	return 0;
}
	
/*
 *	The logical configuration table tells us what we can talk to
 *	on the board. Most of the stuff isn't interesting to us. 
 */

static int i2o_parse_lct(struct i2o_controller *c)
{
	int i;
	int max;
	int tid;
	struct i2o_device *d;
	i2o_lct *lct = c->lct;

	if (lct == NULL) {
		printk(KERN_ERR "%s: LCT is empty???\n", c->name);
		return -1;
	}

	max = lct->table_size;
	max -= 3;
	max /= 9;
	
	printk(KERN_INFO "%s: LCT has %d entries.\n", c->name, max);
	
	if(lct->iop_flags&(1<<0))
		printk(KERN_WARNING "%s: Configuration dialog desired.\n", c->name);
		
	for(i=0;i<max;i++)
	{
		d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
		if(d==NULL)
		{
			printk(KERN_CRIT "i2o_core: Out of memory for I2O device data.\n");
			return -ENOMEM;
		}
		
		d->controller = c;
		d->next = NULL;

		memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));

		d->flags = 0;
		tid = d->lct_data.tid;
		
		i2o_report_controller_unit(c, d);
		
		i2o_install_device(c, d);
	}
	return 0;
}


/**
 *	i2o_quiesce_controller - quiesce controller
 *	@c: controller 
 *
 *	Quiesce an IOP. Causes IOP to make external operation quiescent
 *	(i2o 'READY' state). Internal operation of the IOP continues normally.
 */
 
int i2o_quiesce_controller(struct i2o_controller *c)
{
	u32 msg[4];
	int ret;

	i2o_status_get(c);

	/* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */

	if ((c->status_block->iop_state != ADAPTER_STATE_READY) &&
		(c->status_block->iop_state != ADAPTER_STATE_OPERATIONAL))
	{
		return 0;
	}

	msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
	msg[3] = 0;

	/* Long timeout needed for quiesce if lots of devices */

	if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
		printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
			c->name, -ret);
	else
		dprintk(KERN_INFO "%s: Quiesced.\n", c->name);

	i2o_status_get(c); // Entered READY state
	return ret;
}

/**
 *	i2o_enable_controller - move controller from ready to operational
 *	@c: controller
 *
 *	Enable IOP. This allows the IOP to resume external operations and
 *	reverses the effect of a quiesce. In the event of an error a negative
 *	errno code is returned.
 */
 
int i2o_enable_controller(struct i2o_controller *c)
{
	u32 msg[4];
	int ret;

	i2o_status_get(c);
	
	/* Enable only allowed on READY state */	
	if(c->status_block->iop_state != ADAPTER_STATE_READY)
		return -EINVAL;

	msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;

	/* How long of a timeout do we need? */

	if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
		printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
			c->name, -ret);
	else
		dprintk(KERN_INFO "%s: Enabled.\n", c->name);

	i2o_status_get(c); // entered OPERATIONAL state

	return ret;
}

/**
 *	i2o_clear_controller	-	clear a controller
 *	@c: controller
 *
 *	Clear an IOP to HOLD state, ie. terminate external operations, clear all
 *	input queues and prepare for a system restart. IOP's internal operation
 *	continues normally and the outbound queue is alive.
 *	The IOP is not expected to rebuild its LCT.
 */
 
int i2o_clear_controller(struct i2o_controller *c)
{
	struct i2o_controller *iop;
	u32 msg[4];
	int ret;

	/* Quiesce all IOPs first */

	for (iop = i2o_controller_chain; iop; iop = iop->next)
		i2o_quiesce_controller(iop);

	msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1]=I2O_CMD_ADAPTER_CLEAR<<24|HOST_TID<<12|ADAPTER_TID;
	msg[3]=0;

	if ((ret=i2o_post_wait(c, msg, sizeof(msg), 30)))
		printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
			c->name, -ret);
	else
		dprintk(KERN_INFO "%s: Cleared.\n",c->name);

	i2o_status_get(c);

	/* Enable other IOPs */

	for (iop = i2o_controller_chain; iop; iop = iop->next)
		if (iop != c)
			i2o_enable_controller(iop);

	return ret;
}


/**
 *	i2o_reset_controller
 *	@c: controller to reset
 *
 *	Reset the IOP into INIT state and wait until IOP gets into RESET state.
 *	Terminate all external operations, clear IOP's inbound and outbound
 *	queues, terminate all DDMs, and reload the IOP's operating environment
 *	and all local DDMs. The IOP rebuilds its LCT.
 */
 
static int i2o_reset_controller(struct i2o_controller *c)
{
	struct i2o_controller *iop;
	u32 m;
	u8 *status;
	u32 *msg;
	long time;

	/* Quiesce all IOPs first */

	for (iop = i2o_controller_chain; iop; iop = iop->next)
		i2o_quiesce_controller(iop);

	m=i2o_wait_message(c, "AdapterReset");
	if(m==0xFFFFFFFF)	
		return -ETIMEDOUT;
	msg=(u32 *)(c->mem_offset+m);
	
	status=(void *)kmalloc(4, GFP_KERNEL);
	if(status==NULL) {
		printk(KERN_ERR "IOP reset failed - no free memory.\n");
		return -ENOMEM;
	}
	memset(status, 0, 4);
	
	msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
	msg[2]=core_context;
	msg[3]=0;
	msg[4]=0;
	msg[5]=0;
	msg[6]=virt_to_bus(status);
	msg[7]=0;	/* 64bit host FIXME */

	i2o_post_message(c,m);

	/* Wait for a reply */
	time=jiffies;
	while(status[0]==0)
	{
		if((jiffies-time)>=20*HZ)
		{
			printk(KERN_ERR "IOP reset timeout.\n");
			kfree(status);
			return -ETIMEDOUT;
		}
		schedule();
		barrier();
	}

	if (status[0]==I2O_CMD_IN_PROGRESS)
	{ 
		/* 
		 * Once the reset is sent, the IOP goes into the INIT state 
		 * which is indeterminate.  We need to wait until the IOP 
		 * has rebooted before we can let the system talk to 
		 * it. We read the inbound Free_List until a message is 
		 * available.  If we can't read one in the given ammount of 
		 * time, we assume the IOP could not reboot properly.  
		 */ 

		dprintk(KERN_INFO "%s: Reset in progress, waiting for reboot...\n",
			c->name); 

		time = jiffies; 
		m = I2O_POST_READ32(c); 
		while(m == 0XFFFFFFFF) 
		{ 
			if((jiffies-time) >= 30*HZ)
			{
				printk(KERN_ERR "%s: Timeout waiting for IOP reset.\n", 
						c->name); 
				return -ETIMEDOUT; 
			} 
			schedule(); 
			barrier(); 
			m = I2O_POST_READ32(c); 
		}
		i2o_flush_reply(c,m);
	}

	/* If IopReset was rejected or didn't perform reset, try IopClear */

	i2o_status_get(c);
	if (status[0] == I2O_CMD_REJECTED || 
		c->status_block->iop_state != ADAPTER_STATE_RESET)
	{
		printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",c->name);
		i2o_clear_controller(c);
	}
	else
		dprintk(KERN_INFO "%s: Reset completed.\n", c->name);

	/* Enable other IOPs */

	for (iop = i2o_controller_chain; iop; iop = iop->next)
		if (iop != c)
			i2o_enable_controller(iop);

	kfree(status);
	return 0;
}


/**
 * 	i2o_status_get	-	get the status block for the IOP
 *	@c: controller
 *
 *	Issue a status query on the controller. This updates the
 *	attached status_block. If the controller fails to reply or an
 *	error occurs then a negative errno code is returned. On success
 *	zero is returned and the status_blok is updated.
 */
 
int i2o_status_get(struct i2o_controller *c)
{
	long time;
	u32 m;
	u32 *msg;
	u8 *status_block;

	if (c->status_block == NULL) 
	{
		c->status_block = (i2o_status_block *)
			kmalloc(sizeof(i2o_status_block),GFP_KERNEL);
		if (c->status_block == NULL)
		{
			printk(KERN_CRIT "%s: Get Status Block failed; Out of memory.\n",
				c->name);
			return -ENOMEM;
		}
	}

	status_block = (u8*)c->status_block;
	memset(c->status_block,0,sizeof(i2o_status_block));
	
	m=i2o_wait_message(c, "StatusGet");
	if(m==0xFFFFFFFF)
		return -ETIMEDOUT;	
	msg=(u32 *)(c->mem_offset+m);

	msg[0]=NINE_WORD_MSG_SIZE|SGL_OFFSET_0;
	msg[1]=I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID;
	msg[2]=core_context;
	msg[3]=0;
	msg[4]=0;
	msg[5]=0;
	msg[6]=virt_to_bus(c->status_block);
	msg[7]=0;   /* 64bit host FIXME */
	msg[8]=sizeof(i2o_status_block); /* always 88 bytes */

	i2o_post_message(c,m);

	/* Wait for a reply */

	time=jiffies;
	while(status_block[87]!=0xFF)
	{
		if((jiffies-time)>=5*HZ)
		{
			printk(KERN_ERR "%s: Get status timeout.\n",c->name);
			return -ETIMEDOUT;
		}
		schedule();
		barrier();
	}

#ifdef DRIVERDEBUG
	printk(KERN_INFO "%s: State = ", c->name);
	switch (c->status_block->iop_state) {
		case 0x01:  
			printk("INIT\n");
			break;
		case 0x02:
			printk("RESET\n");
			break;
		case 0x04:
			printk("HOLD\n");
			break;
		case 0x05:
			printk("READY\n");
			break;
		case 0x08:
			printk("OPERATIONAL\n");
			break;
		case 0x10:
			printk("FAILED\n");
			break;
		case 0x11:
			printk("FAULTED\n");
			break;
		default: 
			printk("%x (unknown !!)\n",c->status_block->iop_state);
}     
#endif   

	return 0;
}

/*
 * Get the Hardware Resource Table for the device.
 * The HRT contains information about possible hidden devices
 * but is mostly useless to us 
 */
int i2o_hrt_get(struct i2o_controller *c)
{
	u32 msg[6];
	int ret, size = sizeof(i2o_hrt);

	/* Read first just the header to figure out the real size */

	do  {
		if (c->hrt == NULL) {
			c->hrt=kmalloc(size, GFP_KERNEL);
			if (c->hrt == NULL) {
				printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", c->name);
				return -ENOMEM;
			}
		}

		msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
		msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
		msg[3]= 0;
		msg[4]= (0xD0000000 | size);	/* Simple transaction */
		msg[5]= virt_to_bus(c->hrt);	/* Dump it here */

		if ((ret = i2o_post_wait(c, msg, sizeof(msg), 20))) {
			printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
				c->name, -ret);	
			return ret;
		}

		if (c->hrt->num_entries * c->hrt->entry_len << 2 > size) {
			size = c->hrt->num_entries * c->hrt->entry_len << 2;
			kfree(c->hrt);
			c->hrt = NULL;
		}
	} while (c->hrt == NULL);

	i2o_parse_hrt(c); // just for debugging

	return 0;
}

/*
 * Send the I2O System Table to the specified IOP
 *
 * The system table contains information about all the IOPs in the
 * system.  It is build and then sent to each IOP so that IOPs can
 * establish connections between each other.
 *
 */
static int i2o_systab_send(struct i2o_controller *iop)
{
	u32 msg[12];
	u32 privmem[2];
	u32 privio[2];
	int ret;

	privmem[0] = iop->status_block->current_mem_base;
	privmem[1] = iop->status_block->current_mem_size;
	privio[0] = iop->status_block->current_io_base;
	privio[1] = iop->status_block->current_io_size;

	msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
	msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
	msg[3] = 0;
	msg[4] = (0<<16) | ((iop->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
	msg[5] = 0;                               /* Segment 0 */

	/* 
 	 * Provide three SGL-elements:
 	 * System table (SysTab), Private memory space declaration and 
 	 * Private i/o space declaration  
 	 */
	msg[6] = 0x54000000 | sys_tbl_len;
	msg[7] = virt_to_bus(sys_tbl);
	msg[8] = 0x54000000 | 0;
	msg[9] = virt_to_bus(privmem);
	msg[10] = 0xD4000000 | 0;
	msg[11] = virt_to_bus(privio);

	if ((ret=i2o_post_wait(iop, msg, sizeof(msg), 120)))
		printk(KERN_INFO "%s: Unable to set SysTab (status=%#x).\n", 
			iop->name, -ret);
	else
		dprintk(KERN_INFO "%s: SysTab set.\n", iop->name);

	i2o_status_get(iop); // Entered READY state

	return ret;	

 }

/*
 * Initialize I2O subsystem.
 */
static void __init i2o_sys_init(void)
{
	struct i2o_controller *iop, *niop = NULL;

	printk(KERN_INFO "Activating I2O controllers...\n");
	printk(KERN_INFO "This may take a few minutes if there are many devices\n");
	
	/* In INIT state, Activate IOPs */
	for (iop = i2o_controller_chain; iop; iop = niop) {
		dprintk(KERN_INFO "Calling i2o_activate_controller for %s...\n", 
			iop->name);
		niop = iop->next;
		if (i2o_activate_controller(iop) < 0)
			i2o_delete_controller(iop);
	}

	/* Active IOPs in HOLD state */

rebuild_sys_tab:
	if (i2o_controller_chain == NULL)
		return;

	/*
	 * If build_sys_table fails, we kill everything and bail
	 * as we can't init the IOPs w/o a system table
	 */	
	dprintk(KERN_INFO "i2o_core: Calling i2o_build_sys_table...\n");
	if (i2o_build_sys_table() < 0) {
		i2o_sys_shutdown();
		return;
	}

	/* If IOP don't get online, we need to rebuild the System table */
	for (iop = i2o_controller_chain; iop; iop = niop) {
		niop = iop->next;
		dprintk(KERN_INFO "Calling i2o_online_controller for %s...\n", iop->name);
		if (i2o_online_controller(iop) < 0) {
			i2o_delete_controller(iop);	
			goto rebuild_sys_tab;
		}
	}
	
	/* Active IOPs now in OPERATIONAL state */

	/*
	 * Register for status updates from all IOPs
	 */
	for(iop = i2o_controller_chain; iop; iop=iop->next) {

		/* Create a kernel thread to deal with dynamic LCT updates */
		iop->lct_pid = kernel_thread(i2o_dyn_lct, iop, CLONE_SIGHAND);
	
		/* Update change ind on DLCT */
		iop->dlct->change_ind = iop->lct->change_ind;

		/* Start dynamic LCT updates */
		i2o_lct_notify(iop);

		/* Register for all events from IRTOS */
		i2o_event_register(iop, core_context, 0, 0, 0xFFFFFFFF);
	}
}

/**
 *	i2o_sys_shutdown - shutdown I2O system
 *
 *	Bring down each i2o controller and then return. Each controller
 *	is taken through an orderly shutdown
 */
 
static void i2o_sys_shutdown(void)
{
	struct i2o_controller *iop, *niop;

	/* Delete all IOPs from the controller chain */
	/* that will reset all IOPs too */

	for (iop = i2o_controller_chain; iop; iop = niop) {
		niop = iop->next;
		i2o_delete_controller(iop);
	}
}

/**
 *	i2o_activate_controller	-	bring controller up to HOLD
 *	@iop: controller
 *
 *	This function brings an I2O controller into HOLD state. The adapter
 *	is reset if neccessary and then the queues and resource table
 *	are read. -1 is returned on a failure, 0 on success.
 *	
 */
 
int i2o_activate_controller(struct i2o_controller *iop)
{
	/* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
	/* In READY state, Get status */

	if (i2o_status_get(iop) < 0) {
		printk(KERN_INFO "Unable to obtain status of %s, "
			"attempting a reset.\n", iop->name);
		if (i2o_reset_controller(iop) < 0)
			return -1;
	}

	if(iop->status_block->iop_state == ADAPTER_STATE_FAULTED) {
		printk(KERN_CRIT "%s: hardware fault\n", iop->name);
		return -1;
	}

	if (iop->status_block->i2o_version > I2OVER15) {
		printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O Specification.\n",
			iop->name);
		return -1;
	}

	if (iop->status_block->iop_state == ADAPTER_STATE_READY ||
	    iop->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
	    iop->status_block->iop_state == ADAPTER_STATE_HOLD ||
	    iop->status_block->iop_state == ADAPTER_STATE_FAILED)
	{
		dprintk(KERN_INFO "%s: Already running, trying to reset...\n",
			iop->name);
		if (i2o_reset_controller(iop) < 0)
			return -1;
	}

	if (i2o_init_outbound_q(iop) < 0)
		return -1;

	if (i2o_post_outbound_messages(iop)) 
		return -1;

	/* In HOLD state */
	
	if (i2o_hrt_get(iop) < 0)
		return -1;

	return 0;
}


/**
 *	i2o_init_outbound_queue	- setup the outbound queue
 *	@c: controller
 *
 *	Clear and (re)initialize IOP's outbound queue. Returns 0 on
 *	success or a negative errno code on a failure.
 */
 
int i2o_init_outbound_q(struct i2o_controller *c)
{
	u8 *status;
	u32 m;
	u32 *msg;
	u32 time;

	dprintk(KERN_INFO "%s: Initializing Outbound Queue...\n", c->name);
	m=i2o_wait_message(c, "OutboundInit");
	if(m==0xFFFFFFFF)
		return -ETIMEDOUT;
	msg=(u32 *)(c->mem_offset+m);

	status = kmalloc(4,GFP_KERNEL);
	if (status==NULL) {
		printk(KERN_ERR "%s: Outbound Queue initialization failed - no free memory.\n",
			c->name);
		return -ENOMEM;
	}
	memset(status, 0, 4);

	msg[0]= EIGHT_WORD_MSG_SIZE| TRL_OFFSET_6;
	msg[1]= I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID;
	msg[2]= core_context;
	msg[3]= 0x0106;				/* Transaction context */
	msg[4]= 4096;				/* Host page frame size */
	/* Frame size is in words. Pick 128, its what everyone elses uses and
		other sizes break some adapters. */
	msg[5]= MSG_FRAME_SIZE<<16|0x80;	/* Outbound msg frame size and Initcode */
	msg[6]= 0xD0000004;			/* Simple SG LE, EOB */
	msg[7]= virt_to_bus(status);

	i2o_post_message(c,m);
	
	barrier();
	time=jiffies;
	while(status[0] < I2O_CMD_REJECTED)
	{
		if((jiffies-time)>=30*HZ)
		{
			if(status[0]==0x00)
				printk(KERN_ERR "%s: Ignored queue initialize request.\n",
					c->name);
			else  
				printk(KERN_ERR "%s: Outbound queue initialize timeout.\n",
					c->name);
			kfree(status);
			return -ETIMEDOUT;
		}  
		schedule();
		barrier();
	}  

	if(status[0] != I2O_CMD_COMPLETED)
	{
		printk(KERN_ERR "%s: IOP outbound initialise failed.\n", c->name);
		kfree(status);
		return -ETIMEDOUT;
	}

	return 0;
}

/**
 *	i2o_post_outbound_messages	-	fill message queue
 *	@c: controller
 *
 *	Allocate a message frame and load the messages into the IOP. The
 *	function returns zero on success or a negative errno code on
 *	failure.
 */

int i2o_post_outbound_messages(struct i2o_controller *c)
{
	int i;
	u32 m;
	/* Alloc space for IOP's outbound queue message frames */

	c->page_frame = kmalloc(MSG_POOL_SIZE, GFP_KERNEL);
	if(c->page_frame==NULL) {
		printk(KERN_CRIT "%s: Outbound Q initialize failed; out of memory.\n",
			c->name);
		return -ENOMEM;
	}
	m=virt_to_bus(c->page_frame);

	/* Post frames */

	for(i=0; i< NMBR_MSG_FRAMES; i++) {
		I2O_REPLY_WRITE32(c,m);
		mb();
		m += MSG_FRAME_SIZE;
	}

	return 0;
}

/*
 * Get the IOP's Logical Configuration Table
 */
int i2o_lct_get(struct i2o_controller *c)
{
	u32 msg[8];
	int ret, size = c->status_block->expected_lct_size;

	do {
		if (c->lct == NULL) {
			c->lct = kmalloc(size, GFP_KERNEL);
			if(c->lct == NULL) {
				printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
					c->name);
				return -ENOMEM;
			}
		}
		memset(c->lct, 0, size);

		msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
		msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
		/* msg[2] filled in i2o_post_wait */
		msg[3] = 0;
		msg[4] = 0xFFFFFFFF;	/* All devices */
		msg[5] = 0x00000000;	/* Report now */
		msg[6] = 0xD0000000|size;
		msg[7] = virt_to_bus(c->lct);

		if ((ret=i2o_post_wait(c, msg, sizeof(msg), 120))) {
			printk(KERN_ERR "%s: LCT Get failed (status=%#x.\n", 
				c->name, -ret);	
			return ret;
		}

		if (c->lct->table_size << 2 > size) {
			size = c->lct->table_size << 2;
			kfree(c->lct);
			c->lct = NULL;
		}
	} while (c->lct == NULL);

        if ((ret=i2o_parse_lct(c)) < 0)
                return ret;

	return 0;
}

/*
 * Like above, but used for async notification.  The main
 * difference is that we keep track of the CurrentChangeIndiicator
 * so that we only get updates when it actually changes.
 *
 */
int i2o_lct_notify(struct i2o_controller *c)
{
	u32 msg[8];

	msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
	msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
	msg[2] = core_context;
	msg[3] = 0xDEADBEEF;	
	msg[4] = 0xFFFFFFFF;	/* All devices */
	msg[5] = c->dlct->change_ind+1;	/* Next change */
	msg[6] = 0xD0000000|8192;
	msg[7] = virt_to_bus(c->dlct);

	return i2o_post_this(c, msg, sizeof(msg));
}
		
/*
 *	Bring a controller online into OPERATIONAL state. 
 */
int i2o_online_controller(struct i2o_controller *iop)
{
	if (i2o_systab_send(iop) < 0)
		return -1;

	/* In READY state */

	dprintk(KERN_INFO "%s: Attempting to enable...\n", iop->name);
	if (i2o_enable_controller(iop) < 0)
		return -1;

	/* In OPERATIONAL state  */

	dprintk(KERN_INFO "%s: Attempting to get/parse lct...\n", iop->name);
	if (i2o_lct_get(iop) < 0)
		return -1;

	return 0;
}

/*
 * Build system table
 *
 * The system table contains information about all the IOPs in the
 * system (duh) and is used by the Executives on the IOPs to establish
 * peer2peer connections.  We're not supporting peer2peer at the moment,
 * but this will be needed down the road for things like lan2lan forwarding.
 */
static int i2o_build_sys_table(void)
{
	struct i2o_controller *iop = NULL;
	struct i2o_controller *niop = NULL;
	int count = 0;

	sys_tbl_len = sizeof(struct i2o_sys_tbl) +	// Header + IOPs
				(i2o_num_controllers) *
					sizeof(struct i2o_sys_tbl_entry);

	if(sys_tbl)
		kfree(sys_tbl);

	sys_tbl = kmalloc(sys_tbl_len, GFP_KERNEL);
	if(!sys_tbl) {
		printk(KERN_CRIT "SysTab Set failed. Out of memory.\n");
		return -ENOMEM;
	}
	memset((void*)sys_tbl, 0, sys_tbl_len);

	sys_tbl->num_entries = i2o_num_controllers;
	sys_tbl->version = I2OVERSION; /* TODO: Version 2.0 */
	sys_tbl->change_ind = sys_tbl_ind++;

	for(iop = i2o_controller_chain; iop; iop = niop)
	{
		niop = iop->next;

		/* 
		 * Get updated IOP state so we have the latest information
		 *
		 * We should delete the controller at this point if it
		 * doesn't respond since  if it's not on the system table 
		 * it is techninically not part of the I2O subsyßtem...
		 */
		if(i2o_status_get(iop)) {
			printk(KERN_ERR "%s: Deleting b/c could not get status while"
				"attempting to build system table\n", iop->name);
			i2o_delete_controller(iop);		
			sys_tbl->num_entries--;
			continue; // try the next one
		}

		sys_tbl->iops[count].org_id = iop->status_block->org_id;
		sys_tbl->iops[count].iop_id = iop->unit + 2;
		sys_tbl->iops[count].seg_num = 0;
		sys_tbl->iops[count].i2o_version = 
				iop->status_block->i2o_version;
		sys_tbl->iops[count].iop_state = 
				iop->status_block->iop_state;
		sys_tbl->iops[count].msg_type = 
				iop->status_block->msg_type;
		sys_tbl->iops[count].frame_size = 
				iop->status_block->inbound_frame_size;
		sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
		sys_tbl->iops[count].iop_capabilities = 
				iop->status_block->iop_capabilities;
		sys_tbl->iops[count].inbound_low = 
				(u32)virt_to_bus(iop->post_port);
		sys_tbl->iops[count].inbound_high = 0;	// TODO: 64-bit support

		count++;
	}

#ifdef DRIVERDEBUG
{
	u32 *table;
	table = (u32*)sys_tbl;
	for(count = 0; count < (sys_tbl_len >>2); count++)
		printk(KERN_INFO "sys_tbl[%d] = %0#10x\n", count, table[count]);
}
#endif

	return 0;
}


/*
 *	Run time support routines
 */
 
/*
 *	Generic "post and forget" helpers. This is less efficient - we do
 *	a memcpy for example that isnt strictly needed, but for most uses
 *	this is simply not worth optimising
 */

int i2o_post_this(struct i2o_controller *c, u32 *data, int len)
{
	u32 m;
	u32 *msg;
	unsigned long t=jiffies;

	do
	{
		mb();
		m = I2O_POST_READ32(c);
	}
	while(m==0xFFFFFFFF && (jiffies-t)<HZ);
	
	if(m==0xFFFFFFFF)
	{
		printk(KERN_ERR "%s: Timeout waiting for message frame!\n",
		       c->name);
		return -ETIMEDOUT;
	}
	msg = (u32 *)(c->mem_offset + m);
 	memcpy_toio(msg, data, len);
	i2o_post_message(c,m);
	return 0;
}

/*
 * This core API allows an OSM to post a message and then be told whether
 * or not the system received a successful reply.  It is useful when
 * the OSM does not want to know the exact 3
 */
int i2o_post_wait(struct i2o_controller *c, u32 *msg, int len, int timeout)
{
	DECLARE_WAIT_QUEUE_HEAD(wq_i2o_post);
	int status = 0;
	int flags = 0;
	struct i2o_post_wait_data *p1, *p2;
	struct i2o_post_wait_data *wait_data =
		kmalloc(sizeof(struct i2o_post_wait_data), GFP_KERNEL);

	if(!wait_data)
		return -ENOMEM;

	/* 
	 * The spin locking is needed to keep anyone from playing 
	 * with the queue pointers and id while we do the same
	 */
	spin_lock_irqsave(&post_wait_lock, flags);
	wait_data->next = post_wait_queue;
	post_wait_queue = wait_data;
	wait_data->id = (++post_wait_id) & 0x7fff;
	spin_unlock_irqrestore(&post_wait_lock, flags);

	wait_data->wq = &wq_i2o_post;
	wait_data->status = -ETIMEDOUT;

	msg[2] = 0x80000000|(u32)core_context|((u32)wait_data->id<<16);
	
	if ((status = i2o_post_this(c, msg, len))==0) {
		interruptible_sleep_on_timeout(&wq_i2o_post, HZ * timeout);
		status = wait_data->status;
	}  

#ifdef DRIVERDEBUG
	if(status == -ETIMEDOUT)
		printk(KERN_INFO "%s: POST WAIT TIMEOUT\n",c->name);
#endif

	/* 
	 * Remove the entry from the queue.
	 * Since i2o_post_wait() may have been called again by
	 * a different thread while we were waiting for this 
	 * instance to complete, we're not guaranteed that 
	 * this entry is at the head of the queue anymore, so 
	 * we need to search for it, find it, and delete it.
	 */
	p2 = NULL;
	spin_lock_irqsave(&post_wait_lock, flags);
	for(p1 = post_wait_queue; p1; p2 = p1, p1 = p1->next) {
		if(p1 == wait_data) {
			if(p2)
				p2->next = p1->next;
			else
				post_wait_queue = p1->next;

			break;
		}
	}
	spin_unlock_irqrestore(&post_wait_lock, flags);
	
	kfree(wait_data);

	return status;
}

/*
 * i2o_post_wait is completed and we want to wake up the 
 * sleeping proccess. Called by core's reply handler.
 */
static void i2o_post_wait_complete(u32 context, int status)
{
	struct i2o_post_wait_data *p1 = NULL;

	/* 
	 * We need to search through the post_wait 
	 * queue to see if the given message is still
	 * outstanding.  If not, it means that the IOP 
	 * took longer to respond to the message than we 
	 * had allowed and timer has already expired.  
	 * Not much we can do about that except log
	 * it for debug purposes, increase timeout, and recompile
	 *
	 * Lock needed to keep anyone from moving queue pointers 
	 * around while we're looking through them.
	 */
	spin_lock(&post_wait_lock);
	for(p1 = post_wait_queue; p1; p1 = p1->next) {
		if(p1->id == ((context >> 16) & 0x7fff)) {
			p1->status = status;
			wake_up_interruptible(p1->wq);
			spin_unlock(&post_wait_lock);
			return;
		}
	}
	spin_unlock(&post_wait_lock);

	printk(KERN_DEBUG "i2o_post_wait reply after timeout!\n");
}

/*	Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
 *
 *	This function can be used for all UtilParamsGet/Set operations.
 *	The OperationList is given in oplist-buffer, 
 *	and results are returned in reslist-buffer.
 *	Note that the minimum sized reslist is 8 bytes and contains
 *	ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
 */
int i2o_issue_params(int cmd, struct i2o_controller *iop, int tid, 
                void *oplist, int oplen, void *reslist, int reslen)
{
	u32 msg[9]; 
	u8 *res = (u8 *)reslist;
	u32 *res32 = (u32*)reslist;
	u32 *restmp = (u32*)reslist;
	int len = 0;
	int i = 0;
	int wait_status;

	msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
	msg[1] = cmd << 24 | HOST_TID << 12 | tid; 
	msg[3] = 0;
	msg[4] = 0;
	msg[5] = 0x54000000 | oplen;	/* OperationList */
	msg[6] = virt_to_bus(oplist);
	msg[7] = 0xD0000000 | reslen;	/* ResultList */
	msg[8] = virt_to_bus(reslist);

	if((wait_status = i2o_post_wait(iop, msg, sizeof(msg), 10)))
		return wait_status; 	/* -DetailedStatus */

	/*
	 * Calculate number of bytes of Result LIST
	 * We need to loop through each Result BLOCK and grab the length
	 */
	restmp = res32 + 1;
	len = 1;
	for(i = 0; i < (res32[0]&0X0000FFFF); i++)
	{
		if(restmp[0]&0x00FF0000)	/* BlockStatus != SUCCESS */
		{
			printk(KERN_WARNING "%s - Error:\n  ErrorInfoSize = 0x%02x, " 
					"BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
					(cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
					: "PARAMS_GET",   
					res32[1]>>24, (res32[1]>>16)&0xFF, res32[1]&0xFFFF);
	
			/*
			 *	If this is the only request,than we return an error
			 */
			if((res32[0]&0x0000FFFF) == 1)
				return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
		}

		len += restmp[0] & 0x0000FFFF;	/* Length of res BLOCK */
		restmp += restmp[0] & 0x0000FFFF;	/* Skip to next BLOCK */
	}

	return (len << 2);  /* bytes used by result list */
}

/*
 *	 Query one scalar group value or a whole scalar group.
 */                  	
int i2o_query_scalar(struct i2o_controller *iop, int tid, 
                     int group, int field, void *buf, int buflen)
{
	u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
	u8  resblk[8+buflen]; /* 8 bytes for header */
	int size;

	if (field == -1)  		/* whole group */
       		opblk[4] = -1;
              
	size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, iop, tid, 
		opblk, sizeof(opblk), resblk, sizeof(resblk));
		
	if (size < 0)
		return size;	

	memcpy(buf, resblk+8, buflen);  /* cut off header */
	return size;
}

/*
 *	Set a scalar group value or a whole group.
 */
int i2o_set_scalar(struct i2o_controller *iop, int tid, 
		   int group, int field, void *buf, int buflen)
{
	u16 *opblk;
	u8  resblk[8+buflen]; /* 8 bytes for header */
        int size;

	opblk = kmalloc(buflen+64, GFP_KERNEL);
	if (opblk == NULL)
	{
		printk(KERN_ERR "i2o: no memory for operation buffer.\n");
		return -ENOMEM;
	}

	opblk[0] = 1;                        /* operation count */
	opblk[1] = 0;                        /* pad */
	opblk[2] = I2O_PARAMS_FIELD_SET;
	opblk[3] = group;

	if(field == -1) {               /* whole group */
		opblk[4] = -1;
		memcpy(opblk+5, buf, buflen);
	}
	else                            /* single field */
	{
		opblk[4] = 1;
		opblk[5] = field;
		memcpy(opblk+6, buf, buflen);
	}   

	size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
				opblk, 12+buflen, resblk, sizeof(resblk));

	kfree(opblk);
	return size;
}

/* 
 * 	if oper == I2O_PARAMS_TABLE_GET, get from all rows 
 * 		if fieldcount == -1 return all fields
 *			ibuf and ibuflen are unused (use NULL, 0)
 * 		else return specific fields
 *  			ibuf contains fieldindexes
 *
 * 	if oper == I2O_PARAMS_LIST_GET, get from specific rows
 * 		if fieldcount == -1 return all fields
 *			ibuf contains rowcount, keyvalues
 * 		else return specific fields
 *			fieldcount is # of fieldindexes
 *  			ibuf contains fieldindexes, rowcount, keyvalues
 *
 *	You could also use directly function i2o_issue_params().
 */
int i2o_query_table(int oper, struct i2o_controller *iop, int tid, int group,
		int fieldcount, void *ibuf, int ibuflen,
		void *resblk, int reslen) 
{
	u16 *opblk;
	int size;

	opblk = kmalloc(10 + ibuflen, GFP_KERNEL);
	if (opblk == NULL)
	{
		printk(KERN_ERR "i2o: no memory for query buffer.\n");
		return -ENOMEM;
	}

	opblk[0] = 1;				/* operation count */
	opblk[1] = 0;				/* pad */
	opblk[2] = oper;
	opblk[3] = group;		
	opblk[4] = fieldcount;
	memcpy(opblk+5, ibuf, ibuflen);		/* other params */

	size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET,iop, tid, 
				opblk, 10+ibuflen, resblk, reslen);

	kfree(opblk);
	return size;
}

/*
 * 	Clear table group, i.e. delete all rows.
 */
int i2o_clear_table(struct i2o_controller *iop, int tid, int group)
{
	u16 opblk[] = { 1, 0, I2O_PARAMS_TABLE_CLEAR, group };
	u8  resblk[32]; /* min 8 bytes for result header */

	return i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
				opblk, sizeof(opblk), resblk, sizeof(resblk));
}

/*
 * 	Add a new row into a table group.
 *
 * 	if fieldcount==-1 then we add whole rows
 *		buf contains rowcount, keyvalues
 * 	else just specific fields are given, rest use defaults
 *  		buf contains fieldindexes, rowcount, keyvalues
 */	
int i2o_row_add_table(struct i2o_controller *iop, int tid,
		    int group, int fieldcount, void *buf, int buflen)
{
	u16 *opblk;
	u8  resblk[32]; /* min 8 bytes for header */
	int size;

	opblk = kmalloc(buflen+64, GFP_KERNEL);
	if (opblk == NULL)
	{
		printk(KERN_ERR "i2o: no memory for operation buffer.\n");
		return -ENOMEM;
	}

	opblk[0] = 1;			/* operation count */
	opblk[1] = 0;			/* pad */
	opblk[2] = I2O_PARAMS_ROW_ADD;
	opblk[3] = group;	
	opblk[4] = fieldcount;
	memcpy(opblk+5, buf, buflen);

	size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
				opblk, 10+buflen, resblk, sizeof(resblk));

	kfree(opblk);
	return size;
}


/*
 * Used for error reporting/debugging purposes.
 * Following fail status are common to all classes.
 * The preserved message must be handled in the reply handler. 
 */
void i2o_report_fail_status(u8 req_status, u32* msg)
{
	static char *FAIL_STATUS[] = { 
		"0x80",				/* not used */
		"SERVICE_SUSPENDED", 		/* 0x81 */
		"SERVICE_TERMINATED", 		/* 0x82 */
		"CONGESTION",
		"FAILURE",
		"STATE_ERROR",
		"TIME_OUT",
		"ROUTING_FAILURE",
		"INVALID_VERSION",
		"INVALID_OFFSET",
		"INVALID_MSG_FLAGS",
		"FRAME_TOO_SMALL",
		"FRAME_TOO_LARGE",
		"INVALID_TARGET_ID",
		"INVALID_INITIATOR_ID",
		"INVALID_INITIATOR_CONTEX",	/* 0x8F */
		"UNKNOWN_FAILURE"		/* 0xFF */
	};

	if (req_status == I2O_FSC_TRANSPORT_UNKNOWN_FAILURE)
		printk("TRANSPORT_UNKNOWN_FAILURE (%0#2x)\n.", req_status);
	else
		printk("TRANSPORT_%s.\n", FAIL_STATUS[req_status & 0x0F]);

	/* Dump some details */

	printk(KERN_ERR "  InitiatorId = %d, TargetId = %d\n",
		(msg[1] >> 12) & 0xFFF, msg[1] & 0xFFF); 
	printk(KERN_ERR "  LowestVersion = 0x%02X, HighestVersion = 0x%02X\n",
		(msg[4] >> 8) & 0xFF, msg[4] & 0xFF);
	printk(KERN_ERR "  FailingHostUnit = 0x%04X,  FailingIOP = 0x%03X\n",
		msg[5] >> 16, msg[5] & 0xFFF);

	printk(KERN_ERR "  Severity:  0x%02X ", (msg[4] >> 16) & 0xFF); 
	if (msg[4] & (1<<16))
		printk("(FormatError), "
			"this msg can never be delivered/processed.\n");
	if (msg[4] & (1<<17))
		printk("(PathError), "
			"this msg can no longer be delivered/processed.\n");
	if (msg[4] & (1<<18))
		printk("(PathState), "
			"the system state does not allow delivery.\n");
	if (msg[4] & (1<<19))
		printk("(Congestion), resources temporarily not available;"
			"do not retry immediately.\n");
}

/*
 * Used for error reporting/debugging purposes.
 * Following reply status are common to all classes.
 */
void i2o_report_common_status(u8 req_status)
{
	static char *REPLY_STATUS[] = { 
		"SUCCESS", 
		"ABORT_DIRTY", 
		"ABORT_NO_DATA_TRANSFER",
		"ABORT_PARTIAL_TRANSFER",
		"ERROR_DIRTY",
		"ERROR_NO_DATA_TRANSFER",
		"ERROR_PARTIAL_TRANSFER",
		"PROCESS_ABORT_DIRTY",
		"PROCESS_ABORT_NO_DATA_TRANSFER",
		"PROCESS_ABORT_PARTIAL_TRANSFER",
		"TRANSACTION_ERROR",
		"PROGRESS_REPORT"	
	};

	if (req_status > I2O_REPLY_STATUS_PROGRESS_REPORT)
		printk("RequestStatus = %0#2x", req_status);
	else
		printk("%s", REPLY_STATUS[req_status]);
}

/*
 * Used for error reporting/debugging purposes.
 * Following detailed status are valid  for executive class, 
 * utility class, DDM class and for transaction error replies.
 */
static void i2o_report_common_dsc(u16 detailed_status)
{
	static char *COMMON_DSC[] = { 
		"SUCCESS",
		"0x01",				// not used
		"BAD_KEY",
		"TCL_ERROR",
		"REPLY_BUFFER_FULL",
		"NO_SUCH_PAGE",
		"INSUFFICIENT_RESOURCE_SOFT",
		"INSUFFICIENT_RESOURCE_HARD",
		"0x08",				// not used
		"CHAIN_BUFFER_TOO_LARGE",
		"UNSUPPORTED_FUNCTION",
		"DEVICE_LOCKED",
		"DEVICE_RESET",
		"INAPPROPRIATE_FUNCTION",
		"INVALID_INITIATOR_ADDRESS",
		"INVALID_MESSAGE_FLAGS",
		"INVALID_OFFSET",
		"INVALID_PARAMETER",
		"INVALID_REQUEST",
		"INVALID_TARGET_ADDRESS",
		"MESSAGE_TOO_LARGE",
		"MESSAGE_TOO_SMALL",
		"MISSING_PARAMETER",
		"TIMEOUT",
		"UNKNOWN_ERROR",
		"UNKNOWN_FUNCTION",
		"UNSUPPORTED_VERSION",
		"DEVICE_BUSY",
		"DEVICE_NOT_AVAILABLE"		
	};

	if (detailed_status > I2O_DSC_DEVICE_NOT_AVAILABLE)
		printk(" / DetailedStatus = %0#4x.\n", detailed_status);
	else
		printk(" / %s.\n", COMMON_DSC[detailed_status]);
}

/*
 * Used for error reporting/debugging purposes
 */
static void i2o_report_lan_dsc(u16 detailed_status)
{
	static char *LAN_DSC[] = {	// Lan detailed status code strings
		"SUCCESS",
		"DEVICE_FAILURE",
		"DESTINATION_NOT_FOUND",
		"TRANSMIT_ERROR",
		"TRANSMIT_ABORTED",
		"RECEIVE_ERROR",
		"RECEIVE_ABORTED",
		"DMA_ERROR",
		"BAD_PACKET_DETECTED",
		"OUT_OF_MEMORY",
		"BUCKET_OVERRUN",
		"IOP_INTERNAL_ERROR",
		"CANCELED",
		"INVALID_TRANSACTION_CONTEXT",
		"DEST_ADDRESS_DETECTED",
		"DEST_ADDRESS_OMITTED",
		"PARTIAL_PACKET_RETURNED",
		"TEMP_SUSPENDED_STATE",	// last Lan detailed status code
		"INVALID_REQUEST"	// general detailed status code
	};

	if (detailed_status > I2O_DSC_INVALID_REQUEST)
		printk(" / %0#4x.\n", detailed_status);
	else
		printk(" / %s.\n", LAN_DSC[detailed_status]);
}

/*
 * Used for error reporting/debugging purposes
 */
static void i2o_report_util_cmd(u8 cmd)
{
	switch (cmd) {
	case I2O_CMD_UTIL_NOP:
		printk("UTIL_NOP, ");
		break;			
	case I2O_CMD_UTIL_ABORT:
		printk("UTIL_ABORT, ");
		break;
	case I2O_CMD_UTIL_CLAIM:
		printk("UTIL_CLAIM, ");
		break;
	case I2O_CMD_UTIL_RELEASE:
		printk("UTIL_CLAIM_RELEASE, ");
		break;
	case I2O_CMD_UTIL_CONFIG_DIALOG:
		printk("UTIL_CONFIG_DIALOG, ");
		break;
	case I2O_CMD_UTIL_DEVICE_RESERVE:
		printk("UTIL_DEVICE_RESERVE, ");
		break;
	case I2O_CMD_UTIL_DEVICE_RELEASE:
		printk("UTIL_DEVICE_RELEASE, ");
		break;
	case I2O_CMD_UTIL_EVT_ACK:
		printk("UTIL_EVENT_ACKNOWLEDGE, ");
		break;
	case I2O_CMD_UTIL_EVT_REGISTER:
		printk("UTIL_EVENT_REGISTER, ");
		break;
	case I2O_CMD_UTIL_LOCK:
		printk("UTIL_LOCK, ");
		break;
	case I2O_CMD_UTIL_LOCK_RELEASE:
		printk("UTIL_LOCK_RELEASE, ");
		break;
	case I2O_CMD_UTIL_PARAMS_GET:
		printk("UTIL_PARAMS_GET, ");
		break;
	case I2O_CMD_UTIL_PARAMS_SET:
		printk("UTIL_PARAMS_SET, ");
		break;
	case I2O_CMD_UTIL_REPLY_FAULT_NOTIFY:
		printk("UTIL_REPLY_FAULT_NOTIFY, ");
		break;
	default:
		printk("Cmd = %0#2x, ",cmd);	
	}
}

/*
 * Used for error reporting/debugging purposes
 */
static void i2o_report_exec_cmd(u8 cmd)
{
	switch (cmd) {
	case I2O_CMD_ADAPTER_ASSIGN:
		printk("EXEC_ADAPTER_ASSIGN, ");
		break;
	case I2O_CMD_ADAPTER_READ:
		printk("EXEC_ADAPTER_READ, ");
		break;
	case I2O_CMD_ADAPTER_RELEASE:
		printk("EXEC_ADAPTER_RELEASE, ");
		break;
	case I2O_CMD_BIOS_INFO_SET:
		printk("EXEC_BIOS_INFO_SET, ");
		break;
	case I2O_CMD_BOOT_DEVICE_SET:
		printk("EXEC_BOOT_DEVICE_SET, ");
		break;
	case I2O_CMD_CONFIG_VALIDATE:
		printk("EXEC_CONFIG_VALIDATE, ");
		break;
	case I2O_CMD_CONN_SETUP:
		printk("EXEC_CONN_SETUP, ");
		break;
	case I2O_CMD_DDM_DESTROY:
		printk("EXEC_DDM_DESTROY, ");
		break;
	case I2O_CMD_DDM_ENABLE:
		printk("EXEC_DDM_ENABLE, ");
		break;
	case I2O_CMD_DDM_QUIESCE:
		printk("EXEC_DDM_QUIESCE, ");
		break;
	case I2O_CMD_DDM_RESET:
		printk("EXEC_DDM_RESET, ");
		break;
	case I2O_CMD_DDM_SUSPEND:
		printk("EXEC_DDM_SUSPEND, ");
		break;
	case I2O_CMD_DEVICE_ASSIGN:
		printk("EXEC_DEVICE_ASSIGN, ");
		break;
	case I2O_CMD_DEVICE_RELEASE:
		printk("EXEC_DEVICE_RELEASE, ");
		break;
	case I2O_CMD_HRT_GET:
		printk("EXEC_HRT_GET, ");
		break;
	case I2O_CMD_ADAPTER_CLEAR:
		printk("EXEC_IOP_CLEAR, ");
		break;
	case I2O_CMD_ADAPTER_CONNECT:
		printk("EXEC_IOP_CONNECT, ");
		break;
	case I2O_CMD_ADAPTER_RESET:
		printk("EXEC_IOP_RESET, ");
		break;
	case I2O_CMD_LCT_NOTIFY:
		printk("EXEC_LCT_NOTIFY, ");
		break;
	case I2O_CMD_OUTBOUND_INIT:
		printk("EXEC_OUTBOUND_INIT, ");
		break;
	case I2O_CMD_PATH_ENABLE:
		printk("EXEC_PATH_ENABLE, ");
		break;
	case I2O_CMD_PATH_QUIESCE:
		printk("EXEC_PATH_QUIESCE, ");
		break;
	case I2O_CMD_PATH_RESET:
		printk("EXEC_PATH_RESET, ");
		break;
	case I2O_CMD_STATIC_MF_CREATE:
		printk("EXEC_STATIC_MF_CREATE, ");
		break;
	case I2O_CMD_STATIC_MF_RELEASE:
		printk("EXEC_STATIC_MF_RELEASE, ");
		break;
	case I2O_CMD_STATUS_GET:
		printk("EXEC_STATUS_GET, ");
		break;
	case I2O_CMD_SW_DOWNLOAD:
		printk("EXEC_SW_DOWNLOAD, ");
		break;
	case I2O_CMD_SW_UPLOAD:
		printk("EXEC_SW_UPLOAD, ");
		break;
	case I2O_CMD_SW_REMOVE:
		printk("EXEC_SW_REMOVE, ");
		break;
	case I2O_CMD_SYS_ENABLE:
		printk("EXEC_SYS_ENABLE, ");
		break;
	case I2O_CMD_SYS_MODIFY:
		printk("EXEC_SYS_MODIFY, ");
		break;
	case I2O_CMD_SYS_QUIESCE:
		printk("EXEC_SYS_QUIESCE, ");
		break;
	case I2O_CMD_SYS_TAB_SET:
		printk("EXEC_SYS_TAB_SET, ");
		break;
	default:
		printk("Cmd = %#02x, ",cmd);	
	}
}

/*
 * Used for error reporting/debugging purposes
 */
static void i2o_report_lan_cmd(u8 cmd)
{
	switch (cmd) {
	case LAN_PACKET_SEND:
		printk("LAN_PACKET_SEND, "); 
		break;
	case LAN_SDU_SEND:
		printk("LAN_SDU_SEND, ");
		break;
	case LAN_RECEIVE_POST:
		printk("LAN_RECEIVE_POST, ");
		break;
	case LAN_RESET:
		printk("LAN_RESET, ");
		break;
	case LAN_SUSPEND:
		printk("LAN_SUSPEND, ");
		break;
	default:
		printk("Cmd = %0#2x, ",cmd);	
	}	
}

/*
 * Used for error reporting/debugging purposes.
 * Report Cmd name, Request status, Detailed Status.
 */
void i2o_report_status(const char *severity, const char *str, u32 *msg)
{
	u8 cmd = (msg[1]>>24)&0xFF;
	u8 req_status = (msg[4]>>24)&0xFF;
	u16 detailed_status = msg[4]&0xFFFF;
	struct i2o_handler *h = i2o_handlers[msg[2] & (MAX_I2O_MODULES-1)];

	printk("%s%s: ", severity, str);

	if (cmd < 0x1F) 			// Utility cmd
		i2o_report_util_cmd(cmd);
	
	else if (cmd >= 0xA0 && cmd <= 0xEF) 	// Executive cmd
		i2o_report_exec_cmd(cmd);
	
	else if (h->class == I2O_CLASS_LAN && cmd >= 0x30 && cmd <= 0x3F)
		i2o_report_lan_cmd(cmd);	// LAN cmd
	else
        	printk("Cmd = %0#2x, ", cmd);	// Other cmds

	if (msg[0] & MSG_FAIL) {
		i2o_report_fail_status(req_status, msg);
		return;
	}
	
	i2o_report_common_status(req_status);

	if (cmd < 0x1F || (cmd >= 0xA0 && cmd <= 0xEF))
		i2o_report_common_dsc(detailed_status);	
	else if (h->class == I2O_CLASS_LAN && cmd >= 0x30 && cmd <= 0x3F)
		i2o_report_lan_dsc(detailed_status);
	else
		printk(" / DetailedStatus = %0#4x.\n", detailed_status); 
}

/* Used to dump a message to syslog during debugging */
void i2o_dump_message(u32 *msg)
{
#ifdef DRIVERDEBUG
	int i;
	printk(KERN_INFO "Dumping I2O message size %d @ %p\n", 
		msg[0]>>16&0xffff, msg);
	for(i = 0; i < ((msg[0]>>16)&0xffff); i++)
		printk(KERN_INFO "  msg[%d] = %0#10x\n", i, msg[i]);
#endif
}

/*
 * I2O reboot/shutdown notification.
 *
 * - Call each OSM's reboot notifier (if one exists)
 * - Quiesce each IOP in the system
 *
 * Each IOP has to be quiesced before we can ensure that the system
 * can be properly shutdown as a transaction that has already been
 * acknowledged still needs to be placed in permanent store on the IOP.
 * The SysQuiesce causes the IOP to force all HDMs to complete their
 * transactions before returning, so only at that point is it safe
 * 
 */
static int i2o_reboot_event(struct notifier_block *n, unsigned long code, void
*p)
{
	int i = 0;
	struct i2o_controller *c = NULL;

	if(code != SYS_RESTART && code != SYS_HALT && code != SYS_POWER_OFF)
		return NOTIFY_DONE;

	printk(KERN_INFO "Shutting down I2O system.\n");
	printk(KERN_INFO 
		"   This could take a few minutes if there are many devices attached\n");

	for(i = 0; i < MAX_I2O_MODULES; i++)
	{
		if(i2o_handlers[i] && i2o_handlers[i]->reboot_notify)
			i2o_handlers[i]->reboot_notify();
	}

	for(c = i2o_controller_chain; c; c = c->next)
	{
		if(i2o_quiesce_controller(c))
		{
			printk(KERN_WARNING "i2o: Could not quiesce %s."  "
				Verify setup on next system power up.\n", c->name);
		}
	}

	printk(KERN_INFO "I2O system down.\n");
	return NOTIFY_DONE;
}


#ifdef MODULE

EXPORT_SYMBOL(i2o_controller_chain);
EXPORT_SYMBOL(i2o_num_controllers);
EXPORT_SYMBOL(i2o_find_controller);
EXPORT_SYMBOL(i2o_unlock_controller);
EXPORT_SYMBOL(i2o_status_get);

EXPORT_SYMBOL(i2o_install_handler);
EXPORT_SYMBOL(i2o_remove_handler);

EXPORT_SYMBOL(i2o_claim_device);
EXPORT_SYMBOL(i2o_release_device);
EXPORT_SYMBOL(i2o_device_notify_on);
EXPORT_SYMBOL(i2o_device_notify_off);

EXPORT_SYMBOL(i2o_post_this);
EXPORT_SYMBOL(i2o_post_wait);

EXPORT_SYMBOL(i2o_query_scalar);
EXPORT_SYMBOL(i2o_set_scalar);
EXPORT_SYMBOL(i2o_query_table);
EXPORT_SYMBOL(i2o_clear_table);
EXPORT_SYMBOL(i2o_row_add_table);
EXPORT_SYMBOL(i2o_issue_params);

EXPORT_SYMBOL(i2o_event_register);
EXPORT_SYMBOL(i2o_event_ack);

EXPORT_SYMBOL(i2o_report_status);
EXPORT_SYMBOL(i2o_dump_message);

EXPORT_SYMBOL(i2o_get_class_name);

MODULE_AUTHOR("Red Hat Software");
MODULE_DESCRIPTION("I2O Core");


int init_module(void)
{
	printk(KERN_INFO "I2O Core - (C) Copyright 1999 Red Hat Software\n");
	if (i2o_install_handler(&i2o_core_handler) < 0)
	{
		printk(KERN_ERR 
			"i2o_core: Unable to install core handler.\nI2O stack not loaded!");
		return 0;
	}

	core_context = i2o_core_handler.context;

	/*
	 * Attach core to I2O PCI transport (and others as they are developed)
	 */
#ifdef CONFIG_I2O_PCI_MODULE
	if(i2o_pci_core_attach(&i2o_core_functions) < 0)
		printk(KERN_INFO "i2o: No PCI I2O controllers found\n");
#endif

	/*
	 * Initialize event handling thread
	 */	
	init_MUTEX_LOCKED(&evt_sem);
	evt_pid = kernel_thread(i2o_core_evt, &evt_reply, CLONE_SIGHAND);
	if(evt_pid < 0)
	{
		printk(KERN_ERR "I2O: Could not create event handler kernel thread\n");
		i2o_remove_handler(&i2o_core_handler);
		return 0;
	}
	else
		printk(KERN_INFO "I2O: Event thread created as pid %d\n", evt_pid);

	if(i2o_num_controllers)
		i2o_sys_init();

	register_reboot_notifier(&i2o_reboot_notifier);

	return 0;
}

void cleanup_module(void)
{
	int stat;

	unregister_reboot_notifier(&i2o_reboot_notifier);

	if(i2o_num_controllers)
		i2o_sys_shutdown();

	/*
	 * If this is shutdown time, the thread has already been killed
	 */
	if(evt_running) {
		stat = kill_proc(evt_pid, SIGTERM, 1);
		if(!stat) {
			int count = 10 * 100;
			while(evt_running && count--) {
				current->state = TASK_INTERRUPTIBLE;
				schedule_timeout(1);
			}
	
			if(!count)
				printk(KERN_ERR "i2o: Event thread still running!\n");
		}
	}

#ifdef CONFIG_I2O_PCI_MODULE
	i2o_pci_core_detach();
#endif

	i2o_remove_handler(&i2o_core_handler);

	unregister_reboot_notifier(&i2o_reboot_notifier);
}

#else

extern int i2o_block_init(void);
extern int i2o_config_init(void);
extern int i2o_lan_init(void);
extern int i2o_pci_init(void);
extern int i2o_proc_init(void);
extern int i2o_scsi_init(void);

int __init i2o_init(void)
{
	printk(KERN_INFO "Loading I2O Core - (c) Copyright 1999 Red Hat Software\n");
	
	if (i2o_install_handler(&i2o_core_handler) < 0)
	{
		printk(KERN_ERR 
			"i2o_core: Unable to install core handler.\nI2O stack not loaded!");
		return 0;
	}

	core_context = i2o_core_handler.context;

	/*
	 * Initialize event handling thread
	 * We may not find any controllers, but still want this as 
	 * down the road we may have hot pluggable controllers that
	 * need to be dealt with.
	 */	
	init_MUTEX_LOCKED(&evt_sem);
	if((evt_pid = kernel_thread(i2o_core_evt, &evt_reply, CLONE_SIGHAND)) < 0)
	{
		printk(KERN_ERR "I2O: Could not create event handler kernel thread\n");
		i2o_remove_handler(&i2o_core_handler);
		return 0;
	}


#ifdef CONFIG_I2O_PCI
	i2o_pci_init();
#endif

	if(i2o_num_controllers)
		i2o_sys_init();

	register_reboot_notifier(&i2o_reboot_notifier);

	i2o_config_init();
#ifdef CONFIG_I2O_BLOCK
	i2o_block_init();
#endif
#ifdef CONFIG_I2O_LAN
	i2o_lan_init();
#endif
#ifdef CONFIG_I2O_PROC
	i2o_proc_init();
#endif
	return 0;
}

#endif