summaryrefslogtreecommitdiff
path: root/drivers/scsi/pci2220i.c
blob: e395e4203154fe6eeacce37da94e3ec3e5754bf4 (plain)
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
/****************************************************************************
 * Perceptive Solutions, Inc. PCI-2220I device driver for Linux.
 *
 * pci2220i.c - Linux Host Driver for PCI-2220I EIDE RAID Adapters
 *
 * Copyright (c) 1997-1999 Perceptive Solutions, Inc.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that redistributions of source
 * code retain the above copyright notice and this comment without
 * modification.
 *
 * Technical updates and product information at:
 *  http://www.psidisk.com
 *
 * Please send questions, comments, bug reports to:
 *  tech@psidisk.com Technical Support
 *
 *
 *	Revisions 1.10		Mar-26-1999
 *		- Updated driver for RAID and hot reconstruct support.
 *
 *	Revisions 1.11		Mar-26-1999
 *		- Fixed spinlock and PCI configuration.
 *
 *	Revision 2.00		December-1-1999
 *		- Added code for the PCI-2240I controller
 *		- Added code for ATAPI devices.
 *		- Double buffer for scatter/gather support
 *
 *	Revision 2.10		March-27-2000
 *		- Added support for dynamic DMA
 *
 ****************************************************************************/

#error Convert me to understand page+offset based scatterlists

//#define DEBUG 1

#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/blkdev.h>
#include <linux/timer.h>
#include <linux/spinlock.h>

#include <asm/dma.h>
#include <asm/system.h>
#include <asm/io.h>

#include "scsi.h"
#include <scsi/scsi_host.h>
#include "pci2220i.h"
#include "psi_dale.h"


#define	PCI2220I_VERSION		"2.10"
#define	READ_CMD				IDE_CMD_READ_MULTIPLE
#define	WRITE_CMD				IDE_CMD_WRITE_MULTIPLE
#define	MAX_BUS_MASTER_BLOCKS	SECTORSXFER		// This is the maximum we can bus master

#ifdef DEBUG
#define DEB(x) x
#define STOP_HERE()	{int st;for(st=0;st<100;st++){st=1;}}
#else
#define DEB(x)
#define STOP_HERE()
#endif

#define MAXADAPTER 4					// Increase this and the sizes of the arrays below, if you need more.


typedef struct
	{
	UCHAR			byte6;				// device select register image
	UCHAR			spigot;				// spigot number
	UCHAR			spigots[2];			// RAID spigots
	UCHAR			deviceID[2];		// device ID codes
	USHORT			sectors;			// number of sectors per track
	USHORT			heads;				// number of heads
	USHORT			cylinders;			// number of cylinders for this device
	USHORT			spareword;			// placeholder
	ULONG			blocks;				// number of blocks on device
	DISK_MIRROR		DiskMirror[2];		// RAID status and control
	ULONG			lastsectorlba[2];	// last addressable sector on the drive
	USHORT			raid;				// RAID active flag
	USHORT			mirrorRecon;
	UCHAR			reconOn;
	USHORT			reconCount;
	USHORT			reconIsStarting;	// indicate hot reconstruct is starting
	UCHAR			cmdDrqInt;			// flag for command interrupt
	UCHAR			packet;				// command packet size in bytes
	}	OUR_DEVICE, *POUR_DEVICE;	

typedef struct
	{
	USHORT		 bigD;					// identity is a PCI-2240I if true, otherwise a PCI-2220I
	USHORT		 atapi;					// this interface is for ATAPI devices only
	ULONG		 regDmaDesc;			// address of the DMA discriptor register for direction of transfer
	ULONG		 regDmaCmdStat;			// Byte #1 of DMA command status register
	ULONG		 regDmaAddrPci;			// 32 bit register for PCI address of DMA
	ULONG		 regDmaAddrLoc;			// 32 bit register for local bus address of DMA
	ULONG		 regDmaCount;			// 32 bit register for DMA transfer count
	ULONG		 regDmaMode;			// 32 bit register for DMA mode control
	ULONG		 regRemap;				// 32 bit local space remap
	ULONG		 regDesc;				// 32 bit local region descriptor
	ULONG		 regRange;				// 32 bit local range
	ULONG		 regIrqControl;			// 16 bit Interrupt enable/disable and status
	ULONG		 regScratchPad;			// scratch pad I/O base address
	ULONG		 regBase;				// Base I/O register for data space
	ULONG		 regData;				// data register I/O address
	ULONG		 regError;				// error register I/O address
	ULONG		 regSectCount;			// sector count register I/O address
	ULONG		 regLba0;				// least significant byte of LBA
	ULONG		 regLba8;				// next least significant byte of LBA
	ULONG		 regLba16;				// next most significan byte of LBA
	ULONG		 regLba24;				// head and most 4 significant bits of LBA
	ULONG		 regStatCmd;			// status on read and command on write register
	ULONG		 regStatSel;			// board status on read and spigot select on write register
	ULONG		 regFail;				// fail bits control register
	ULONG		 regAltStat;			// alternate status and drive control register
	ULONG		 basePort;				// PLX base I/O port
	USHORT		 timingMode;			// timing mode currently set for adapter
	USHORT		 timingPIO;				// TRUE if PIO timing is active
	struct pci_dev	*pcidev;
	ULONG		 timingAddress;			// address to use on adapter for current timing mode
	ULONG		 irqOwned;				// owned IRQ or zero if shared
	UCHAR		 numberOfDrives;		// saved number of drives on this controller
	UCHAR		 failRegister;			// current inverted data in fail register
	OUR_DEVICE	 device[BIGD_MAXDRIVES];
	DISK_MIRROR	*raidData[BIGD_MAXDRIVES];
	ULONG		 startSector;
	USHORT		 sectorCount;
	ULONG		 readCount;
	UCHAR		*currentSgBuffer;
	ULONG		 currentSgCount;
	USHORT		 nextSg;
	UCHAR		 cmd;
	Scsi_Cmnd	*SCpnt;
	POUR_DEVICE	 pdev;					// current device opearating on
	USHORT		 devInReconIndex;
	USHORT		 expectingIRQ;
	USHORT		 reconOn;				// Hot reconstruct is to be done.
	USHORT		 reconPhase;			// Hot reconstruct operation is in progress.
	ULONG		 reconSize;
	USHORT		 demoFail;				// flag for RAID failure demonstration
	USHORT		 survivor;
	USHORT		 failinprog;
	struct timer_list	reconTimer;	
	struct timer_list	timer;
	UCHAR		*kBuffer;
	dma_addr_t	 kBufferDma;
	UCHAR		 reqSense;
	UCHAR		 atapiCdb[16];
	UCHAR		 atapiSpecial;
	}	ADAPTER2220I, *PADAPTER2220I;

#define HOSTDATA(host) ((PADAPTER2220I)&host->hostdata)

#define	RECON_PHASE_READY		0x01
#define	RECON_PHASE_COPY		0x02
#define	RECON_PHASE_UPDATE		0x03
#define	RECON_PHASE_LAST		0x04
#define	RECON_PHASE_END			0x07	
#define	RECON_PHASE_MARKING		0x80
#define	RECON_PHASE_FAILOVER	0xFF

static struct	Scsi_Host 	   *PsiHost[MAXADAPTER] = {NULL,};  // One for each adapter
static			int				NumAdapters = 0;
static			int				Installed = 0;
static			SETUP			DaleSetup;
static			DISK_MIRROR		DiskMirror[BIGD_MAXDRIVES];
static			ULONG			ModeArray[] = {DALE_DATA_MODE2, DALE_DATA_MODE3, DALE_DATA_MODE4, DALE_DATA_MODE5};
static			ULONG			ModeArray2[] = {BIGD_DATA_MODE2, BIGD_DATA_MODE3, BIGD_DATA_MODE4, BIGD_DATA_MODE5};

static void ReconTimerExpiry (unsigned long data);

/*******************************************************************************************************
 *	Name:			Alarm
 *
 *	Description:	Sound the for the given device
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					device	 - Device number.
 *	
 *	Returns:		Nothing.
 *
 ******************************************************************************************************/
static void Alarm (PADAPTER2220I padapter, UCHAR device)
	{
	UCHAR	zc;

	if ( padapter->bigD )
		{
		zc = device | (FAIL_ANY | FAIL_AUDIBLE);
		if ( padapter->failRegister & FAIL_ANY ) 
			zc |= FAIL_MULTIPLE;
		
		padapter->failRegister = zc;
		outb_p (~zc, padapter->regFail);
		}
	else
		outb_p (0x3C | (1 << device), padapter->regFail);			// sound alarm and set fail light		
	}
/****************************************************************
 *	Name:	MuteAlarm	:LOCAL
 *
 *	Description:	Mute the audible alarm.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static void MuteAlarm (PADAPTER2220I padapter)
	{
	UCHAR	old;

	if ( padapter->bigD )
		{
		padapter->failRegister &= ~FAIL_AUDIBLE;
		outb_p (~padapter->failRegister, padapter->regFail);
		}
	else
		{
		old = (inb_p (padapter->regStatSel) >> 3) | (inb_p (padapter->regStatSel) & 0x83);
		outb_p (old | 0x40, padapter->regFail);
		}
	}
/****************************************************************
 *	Name:	WaitReady	:LOCAL
 *
 *	Description:	Wait for device ready.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int WaitReady (PADAPTER2220I padapter)
	{
	ULONG	z;
	UCHAR	status;

	for ( z = 0;  z < (TIMEOUT_READY * 4);  z++ )
		{
		status = inb_p (padapter->regStatCmd);
		if ( (status & (IDE_STATUS_DRDY | IDE_STATUS_BUSY)) == IDE_STATUS_DRDY )
			return 0;
		udelay (250);
		}
	return status;
	}
/****************************************************************
 *	Name:	WaitReadyReset	:LOCAL
 *
 *	Description:	Wait for device ready.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int WaitReadyReset (PADAPTER2220I padapter)
	{
	ULONG	z;
	UCHAR	status;

	for ( z = 0;  z < (125 * 16);  z++ )				// wait up to 1/4 second
		{
		status = inb_p (padapter->regStatCmd);
		if ( (status & (IDE_STATUS_DRDY | IDE_STATUS_BUSY)) == IDE_STATUS_DRDY )
			{
			DEB (printk ("\nPCI2220I:  Reset took %ld mSec to be ready", z / 8));
			return 0;
			}
		udelay (125);
		}
	DEB (printk ("\nPCI2220I:  Reset took more than 2 Seconds to come ready, Disk Failure"));
	return status;
	}
/****************************************************************
 *	Name:	WaitDrq	:LOCAL
 *
 *	Description:	Wait for device ready for data transfer.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int WaitDrq (PADAPTER2220I padapter)
	{
	ULONG	z;
	UCHAR	status;

	for ( z = 0;  z < (TIMEOUT_DRQ * 4);  z++ )
		{
		status = inb_p (padapter->regStatCmd);
		if ( status & IDE_STATUS_DRQ )
			return 0;
		udelay (250);
		}
	return status;
	}
/****************************************************************
 *	Name:	AtapiWaitReady	:LOCAL
 *
 *	Description:	Wait for device busy and DRQ to be cleared.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					msec	 - Number of milliseconds to wait.
 *
 *	Returns:		TRUE if drive does not clear busy in time.
 *
 ****************************************************************/
static int AtapiWaitReady (PADAPTER2220I padapter, int msec)
	{
	int z;

	for ( z = 0;  z < (msec * 16);  z++ )
		{
		if ( !(inb_p (padapter->regStatCmd) & (IDE_STATUS_BUSY | IDE_STATUS_DRQ)) )
			return FALSE;
		udelay (125);
		}
	return TRUE;
	}
/****************************************************************
 *	Name:	AtapiWaitDrq	:LOCAL
 *
 *	Description:	Wait for device ready for data transfer.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					msec	 - Number of milliseconds to wait.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int AtapiWaitDrq (PADAPTER2220I padapter, int msec)
	{
	ULONG	z;

	for ( z = 0;  z < (msec * 16);  z++ )
		{
		if ( inb_p (padapter->regStatCmd) & IDE_STATUS_DRQ )
			return 0;
		udelay (128);
		}
	return TRUE;
	}
/****************************************************************
 *	Name:	HardReset	:LOCAL
 *
 *	Description:	Wait for device ready for data transfer.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *					spigot	 - Spigot number.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int HardReset (PADAPTER2220I padapter, POUR_DEVICE pdev, UCHAR spigot)
	{
	DEB (printk ("\npci2220i:RESET  spigot = %X  devices = %d, %d", spigot, pdev->deviceID[0], pdev->deviceID[1]));
	mdelay (100);										// just wait 100 mSec to let drives flush	
	SelectSpigot (padapter, spigot | SEL_IRQ_OFF);
	
	outb_p (0x0E, padapter->regAltStat);					// reset the suvivor
	udelay (100);											// wait a little	
	outb_p (0x08, padapter->regAltStat);					// clear the reset
	udelay (100);

	outb_p (0xA0, padapter->regLba24);						// select the master drive
	if ( WaitReadyReset (padapter) )
		{
		DEB (printk ("\npci2220i: master not ready after reset"));
		return TRUE;
		}
	outb_p (0xB0, padapter->regLba24);						// try the slave drive
	if ( (inb_p (padapter->regStatCmd) & (IDE_STATUS_DRDY | IDE_STATUS_BUSY)) == IDE_STATUS_DRDY ) 
		{
		DEB (printk ("\nPCI2220I: initializing slave drive on spigot %X", spigot));
		outb_p (SECTORSXFER, padapter->regSectCount);
		WriteCommand (padapter, IDE_CMD_SET_MULTIPLE);	
		if ( WaitReady (padapter) )
			{
			DEB (printk ("\npci2220i: slave not ready after set multiple"));
			return TRUE;
			}
		}
	
	outb_p (0xA0, padapter->regLba24);				// select the drive
	outb_p (SECTORSXFER, padapter->regSectCount);
	WriteCommand (padapter, IDE_CMD_SET_MULTIPLE);	
	if ( WaitReady (padapter) )
		{
		DEB (printk ("\npci2220i: master not ready after set multiple"));
		return TRUE;
		}
	return FALSE;
	}
/****************************************************************
 *	Name:	AtapiReset	:LOCAL
 *
 *	Description:	Wait for device ready for data transfer.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *
 *	Returns:		TRUE if drive does not come ready.
 *
 ****************************************************************/
static int AtapiReset (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	SelectSpigot (padapter, pdev->spigot);
	AtapiDevice (padapter, pdev->byte6);
	AtapiCountLo (padapter, 0);
	AtapiCountHi (padapter, 0);
	WriteCommand (padapter, IDE_COMMAND_ATAPI_RESET);
	udelay (125);
	if ( AtapiWaitReady (padapter, 1000) )
		return TRUE;
	if ( inb_p (padapter->regStatCmd) || (inb_p (padapter->regLba8) != 0x14) || (inb_p (padapter->regLba16) != 0xEB) )
		return TRUE;
	return FALSE;
	}
/****************************************************************
 *	Name:	WalkScatGath	:LOCAL
 *
 *	Description:	Transfer data to/from scatter/gather buffers.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					datain   - TRUE if data read.
 *					length   - Number of bytes to transfer.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void WalkScatGath (PADAPTER2220I padapter, UCHAR datain, ULONG length)
	{
	ULONG	 count;
	UCHAR	*buffer = padapter->kBuffer;

	while ( length )
		{
		count = ( length > padapter->currentSgCount ) ? padapter->currentSgCount : length; 
		
		if ( datain )
			memcpy (padapter->currentSgBuffer, buffer, count);
		else
			memcpy (buffer, padapter->currentSgBuffer, count);

		padapter->currentSgCount -= count;
		if ( !padapter->currentSgCount )
			{
			if ( padapter->nextSg < padapter->SCpnt->use_sg )
				{
				padapter->currentSgBuffer = ((struct scatterlist *)padapter->SCpnt->request_buffer)[padapter->nextSg].address;
				padapter->currentSgCount = ((struct scatterlist *)padapter->SCpnt->request_buffer)[padapter->nextSg].length;
				padapter->nextSg++;
				}
			}
		else
			padapter->currentSgBuffer += count;

		length -= count;
		buffer += count;
		}
	}
/****************************************************************
 *	Name:	BusMaster	:LOCAL
 *
 *	Description:	Do a bus master I/O.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					datain	 - TRUE if data read.
 *					irq		 - TRUE if bus master interrupt expected.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void BusMaster (PADAPTER2220I padapter, UCHAR datain, UCHAR irq)
	{
	ULONG zl;
	
	zl = ( padapter->sectorCount > MAX_BUS_MASTER_BLOCKS ) ? MAX_BUS_MASTER_BLOCKS : padapter->sectorCount;
	padapter->sectorCount -= zl;
	zl *= (ULONG)BYTES_PER_SECTOR;

	if ( datain )
		{
		padapter->readCount = zl;
		outb_p (8, padapter->regDmaDesc);							// read operation
		if ( padapter->bigD )
			{
			if ( irq && !padapter->sectorCount )
				outb_p (0x0C, padapter->regDmaMode);				// interrupt on
			else
				outb_p (0x08, padapter->regDmaMode);				// no interrupt
			}
		else 
			{
			if ( irq && !padapter->sectorCount )
				outb_p (0x05, padapter->regDmaMode);				// interrupt on
			else
				outb_p (0x01, padapter->regDmaMode);				// no interrupt
			}
		}
	else
		{
		outb_p (0x00, padapter->regDmaDesc);						// write operation
		if ( padapter->bigD )
			outb_p (0x08, padapter->regDmaMode);					// no interrupt						
		else
			outb_p (0x01, padapter->regDmaMode);					// no interrupt
		WalkScatGath (padapter, FALSE, zl);	
		}
	
	outl (padapter->timingAddress, padapter->regDmaAddrLoc);
	outl (padapter->kBufferDma, padapter->regDmaAddrPci);
	outl (zl, padapter->regDmaCount);
	outb_p (0x03, padapter->regDmaCmdStat);							// kick the DMA engine in gear
	}
/****************************************************************
 *	Name:	AtapiBusMaster	:LOCAL
 *
 *	Description:	Do a bus master I/O.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					datain	 - TRUE if data read.
 *					length	 - Number of bytes to transfer.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void AtapiBusMaster (PADAPTER2220I padapter, UCHAR datain, ULONG length)
	{
	outl (padapter->timingAddress, padapter->regDmaAddrLoc);
	outl (padapter->kBufferDma, padapter->regDmaAddrPci);
	outl (length, padapter->regDmaCount);
	if ( datain )
		{
		if ( padapter->readCount )
				WalkScatGath (padapter, TRUE, padapter->readCount);
		outb_p (0x08, padapter->regDmaDesc);						// read operation
		outb_p (0x08, padapter->regDmaMode);						// no interrupt
		padapter->readCount = length;
		}
	else
		{
		outb_p (0x00, padapter->regDmaDesc);						// write operation
		outb_p (0x08, padapter->regDmaMode);						// no interrupt						
		if ( !padapter->atapiSpecial )
			WalkScatGath (padapter, FALSE, length);	
		}
	outb_p (0x03, padapter->regDmaCmdStat);							// kick the DMA engine in gear
	}
/****************************************************************
 *	Name:	WriteData	:LOCAL
 *
 *	Description:	Write data to device.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *
 *	Returns:		TRUE if drive does not assert DRQ in time.
 *
 ****************************************************************/
static int WriteData (PADAPTER2220I padapter)
	{
	ULONG	zl;
	
	if ( !WaitDrq (padapter) )
		{
		if ( padapter->timingPIO )
			{
			zl = (padapter->sectorCount > MAX_BUS_MASTER_BLOCKS) ? MAX_BUS_MASTER_BLOCKS : padapter->sectorCount;
			WalkScatGath (padapter, FALSE, zl * BYTES_PER_SECTOR);
			outsw (padapter->regData, padapter->kBuffer, zl * (BYTES_PER_SECTOR / 2));
			padapter->sectorCount -= zl;
			}
		else
			BusMaster (padapter, 0, 0);
		return 0;
		}
	padapter->cmd = 0;												// null out the command byte
	return 1;
	}
/****************************************************************
 *	Name:	WriteDataBoth	:LOCAL
 *
 *	Description:	Write data to device.
 *
 *	Parameters:		padapter - Pointer to adapter structure.
 *					pdev	 - Pointer to device structure
 *
 *	Returns:		Index + 1 of drive not failed or zero for OK.
 *
 ****************************************************************/
static int WriteDataBoth (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	ULONG	zl;
	UCHAR	status0, status1;

	SelectSpigot (padapter, pdev->spigots[0]);
	status0 = WaitDrq (padapter);
	if ( !status0 )
		{
		SelectSpigot (padapter, pdev->spigots[1]);
		status1 = WaitDrq (padapter);
		if ( !status1 )
			{
			SelectSpigot (padapter, pdev->spigots[0] | pdev->spigots[1] | padapter->bigD);
			if ( padapter->timingPIO )
				{
				zl = (padapter->sectorCount > MAX_BUS_MASTER_BLOCKS) ? MAX_BUS_MASTER_BLOCKS : padapter->sectorCount;
				WalkScatGath (padapter, FALSE, zl * BYTES_PER_SECTOR);
				outsw (padapter->regData, padapter->kBuffer, zl * (BYTES_PER_SECTOR / 2));
				padapter->sectorCount -= zl;
				}
			else
				BusMaster (padapter, 0, 0);
			return 0;
			}
		}
	padapter->cmd = 0;												// null out the command byte
	if ( status0 )
		return 2;
	return 1;
	}
/****************************************************************
 *	Name:	IdeCmd	:LOCAL
 *
 *	Description:	Process an IDE command.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *
 *	Returns:		Zero if no error or status register contents on error.
 *
 ****************************************************************/
static UCHAR IdeCmd (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	UCHAR	status;

	SelectSpigot (padapter, pdev->spigot | padapter->bigD);							// select the spigot
	outb_p (pdev->byte6 | ((UCHAR *)(&padapter->startSector))[3], padapter->regLba24);			// select the drive
	status = WaitReady (padapter);
	if ( !status )
		{
		outb_p (padapter->sectorCount, padapter->regSectCount);
		outb_p (((UCHAR *)(&padapter->startSector))[0], padapter->regLba0);
		outb_p (((UCHAR *)(&padapter->startSector))[1], padapter->regLba8);
		outb_p (((UCHAR *)(&padapter->startSector))[2], padapter->regLba16);
		padapter->expectingIRQ = TRUE;
		WriteCommand (padapter, padapter->cmd);
		return 0;
		}

	padapter->cmd = 0;									// null out the command byte
	return status;
	}
/****************************************************************
 *	Name:	IdeCmdBoth	:LOCAL
 *
 *	Description:	Process an IDE command to both drivers.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device structure
 *
 *	Returns:		Index + 1 of drive not failed or zero for OK.
 *
 ****************************************************************/
static UCHAR IdeCmdBoth (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	UCHAR	status0;
	UCHAR	status1;

	SelectSpigot (padapter, pdev->spigots[0] | pdev->spigots[1]);								// select the spigots
	outb_p (padapter->pdev->byte6 | ((UCHAR *)(&padapter->startSector))[3], padapter->regLba24);// select the drive
	SelectSpigot (padapter, pdev->spigots[0]);
	status0 = WaitReady (padapter);
	if ( !status0 )
		{
		SelectSpigot (padapter, pdev->spigots[1]);
		status1 = WaitReady (padapter);
		if ( !status1 )
			{
			SelectSpigot (padapter, pdev->spigots[0] | pdev->spigots[1] | padapter->bigD);
			outb_p (padapter->sectorCount, padapter->regSectCount);
			outb_p (((UCHAR *)(&padapter->startSector))[0], padapter->regLba0);
			outb_p (((UCHAR *)(&padapter->startSector))[1], padapter->regLba8);
			outb_p (((UCHAR *)(&padapter->startSector))[2], padapter->regLba16);
			padapter->expectingIRQ = TRUE;
			WriteCommand (padapter, padapter->cmd);
			return 0;
			}
		}
	padapter->cmd = 0;									// null out the command byte
	if ( status0 )
		return 2;
	return 1;
	}
/****************************************************************
 *	Name:	OpDone	:LOCAL
 *
 *	Description:	Complete an operatoin done sequence.
 *
 *	Parameters:		padapter - Pointer to host data block.
 *					spigot	 - Spigot select code.
 *					device	 - Device byte code.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void OpDone (PADAPTER2220I padapter, ULONG result)
	{
	Scsi_Cmnd	   *SCpnt = padapter->SCpnt;
	
	if ( padapter->reconPhase )
		{
		padapter->reconPhase = 0;
		if ( padapter->SCpnt )
			{
			Pci2220i_QueueCommand (SCpnt, SCpnt->scsi_done);
			}
		else
			{
			if ( padapter->reconOn )
				{
				ReconTimerExpiry ((unsigned long)padapter);
				}
			}
		}
	else
		{
		padapter->cmd = 0;
		padapter->SCpnt = NULL;	
		padapter->pdev = NULL;
		SCpnt->result = result;
		SCpnt->scsi_done (SCpnt);
		if ( padapter->reconOn && !padapter->reconTimer.data )
			{
			padapter->reconTimer.expires = jiffies + (HZ / 4);	// start in 1/4 second
			padapter->reconTimer.data = (unsigned long)padapter;
			add_timer (&padapter->reconTimer);
			}
		}
	}
/****************************************************************
 *	Name:	InlineIdentify	:LOCAL
 *
 *	Description:	Do an intline inquiry on a drive.
 *
 *	Parameters:		padapter - Pointer to host data block.
 *					spigot	 - Spigot select code.
 *					device	 - Device byte code.
 *
 *	Returns:		Last addressable sector or zero if none.
 *
 ****************************************************************/
static ULONG InlineIdentify (PADAPTER2220I padapter, UCHAR spigot, UCHAR device)
	{
	PIDENTIFY_DATA	pid = (PIDENTIFY_DATA)padapter->kBuffer;

	SelectSpigot (padapter, spigot | SEL_IRQ_OFF);					// select the spigot
	outb_p ((device << 4) | 0xA0, padapter->regLba24);				// select the drive
	if ( WaitReady (padapter) )
		return 0;
	WriteCommand (padapter, IDE_COMMAND_IDENTIFY);	
	if ( WaitDrq (padapter) )
		return 0;
	insw (padapter->regData, padapter->kBuffer, sizeof (IDENTIFY_DATA) >> 1);
	return (pid->LBATotalSectors - 1);
	}
/****************************************************************
 *	Name:	AtapiIdentify	:LOCAL
 *
 *	Description:	Do an intline inquiry on a drive.
 *
 *	Parameters:		padapter - Pointer to host data block.
 *					pdev	 - Pointer to device table.
 *
 *	Returns:		TRUE on error.
 *
 ****************************************************************/
static ULONG AtapiIdentify (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	ATAPI_GENERAL_0		ag0;
	USHORT				zs;
	int					z;

	AtapiDevice (padapter, pdev->byte6);	
	WriteCommand (padapter, IDE_COMMAND_ATAPI_IDENTIFY);	
	if ( AtapiWaitDrq (padapter, 3000) )
		return TRUE;

	*(USHORT *)&ag0 = inw_p (padapter->regData);
	for ( z = 0;  z < 255;  z++ )
		zs = inw_p (padapter->regData);

	if ( ag0.ProtocolType == 2 )
		{
		if ( ag0.CmdDrqType == 1 )
			pdev->cmdDrqInt = TRUE;
		switch ( ag0.CmdPacketSize )
			{
			case 0:
				pdev->packet = 6;
				break;
			case 1:
				pdev->packet = 8;
				break;
			default:
				pdev->packet = 6;
				break;
			}
		return FALSE;
		}
	return TRUE;
	}
/****************************************************************
 *	Name:	Atapi2Scsi
 *
 *	Description:	Convert ATAPI data to SCSI data.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					SCpnt	 - Pointer to SCSI command structure.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
void Atapi2Scsi (PADAPTER2220I padapter, Scsi_Cmnd *SCpnt)
	{
	UCHAR	*buff = padapter->currentSgBuffer;
 
	switch ( SCpnt->cmnd[0] )
		{
		case SCSIOP_MODE_SENSE:
			buff[0] = padapter->kBuffer[1];
			buff[1] = padapter->kBuffer[2];
			buff[2] = padapter->kBuffer[3];
			buff[3] = padapter->kBuffer[7];
			memcpy (&buff[4], &padapter->kBuffer[8], padapter->atapiCdb[8] - 8);
			break;
		case SCSIOP_INQUIRY:
			padapter->kBuffer[2] = 2;
			memcpy (buff, padapter->kBuffer, padapter->currentSgCount);
			break;		
		default:
			if ( padapter->readCount )
				WalkScatGath (padapter, TRUE, padapter->readCount);
			break;
		}
	}
/****************************************************************
 *	Name:	Scsi2Atapi
 *
 *	Description:	Convert SCSI packet command to Atapi packet command.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					SCpnt	 - Pointer to SCSI command structure.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void Scsi2Atapi (PADAPTER2220I padapter, Scsi_Cmnd *SCpnt)
	{
	UCHAR	*cdb = SCpnt->cmnd;
	UCHAR	*buff = padapter->currentSgBuffer;

	switch (cdb[0]) 
		{
		case SCSIOP_READ6:
            padapter->atapiCdb[0] = SCSIOP_READ;
			padapter->atapiCdb[1] = cdb[1] & 0xE0;
            padapter->atapiCdb[3] = cdb[1] & 0x1F;
			padapter->atapiCdb[4] = cdb[2];
			padapter->atapiCdb[5] = cdb[3];
			padapter->atapiCdb[8] = cdb[4];
			padapter->atapiCdb[9] = cdb[5];
			break;
		case SCSIOP_WRITE6:
            padapter->atapiCdb[0] = SCSIOP_WRITE;
			padapter->atapiCdb[1] = cdb[1] & 0xE0;
            padapter->atapiCdb[3] = cdb[1] & 0x1F;
			padapter->atapiCdb[4] = cdb[2];
			padapter->atapiCdb[5] = cdb[3];
			padapter->atapiCdb[8] = cdb[4];
			padapter->atapiCdb[9] = cdb[5];
			break;
        case SCSIOP_MODE_SENSE: 
            padapter->atapiCdb[0] = SCSIOP_MODE_SENSE10;
			padapter->atapiCdb[2] = cdb[2];
			padapter->atapiCdb[8] = cdb[4] + 4;
            break;

        case SCSIOP_MODE_SELECT: 
			padapter->atapiSpecial = TRUE;
			padapter->atapiCdb[0] = SCSIOP_MODE_SELECT10;
			padapter->atapiCdb[1] = cdb[1] | 0x10;
			memcpy (padapter->kBuffer, buff, 4);
			padapter->kBuffer[4] = padapter->kBuffer[5] = 0;
			padapter->kBuffer[6] = padapter->kBuffer[7] = 0;
			memcpy (&padapter->kBuffer[8], &buff[4], cdb[4] - 4);
			padapter->atapiCdb[8] = cdb[4] + 4;
			break;
	    }
	}
/****************************************************************
 *	Name:	AtapiSendCdb
 *
 *	Description:	Send the CDB packet to the device.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *					cdb		 - Pointer to 16 byte SCSI cdb.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void AtapiSendCdb (PADAPTER2220I padapter, POUR_DEVICE pdev, CHAR *cdb)
	{
	DEB (printk ("\nPCI2242I: CDB: %X %X %X %X %X %X %X %X %X %X %X %X", cdb[0], cdb[1], cdb[2], cdb[3], cdb[4], cdb[5], cdb[6], cdb[7], cdb[8], cdb[9], cdb[10], cdb[11]));
	outsw (padapter->regData, cdb, pdev->packet);
	}
/****************************************************************
 *	Name:	AtapiRequestSense
 *
 *	Description:	Send the CDB packet to the device.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *					SCpnt	 - Pointer to SCSI command structure.
 *					pass	 - If true then this is the second pass to send cdb.
 *
 *	Returns:		TRUE on error.
 *
 ****************************************************************/
static int AtapiRequestSense (PADAPTER2220I padapter, POUR_DEVICE pdev, Scsi_Cmnd *SCpnt, UCHAR pass)
	{
	UCHAR	cdb[16] = {SCSIOP_REQUEST_SENSE,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0};		
	
	DEB (printk ("\nPCI2242I: AUTO REQUEST SENSE"));
	cdb[4] = (UCHAR)(sizeof (SCpnt->sense_buffer));
	if ( !pass )
		{
		padapter->reqSense = TRUE;
	 	AtapiCountLo (padapter, cdb[4]);						
		AtapiCountHi (padapter, 0);						
		outb_p (0, padapter->regError);						
		WriteCommand (padapter, IDE_COMMAND_ATAPI_PACKET);
		if ( pdev->cmdDrqInt )
			return FALSE;

		if ( AtapiWaitDrq (padapter, 500) )
			return TRUE;
		}
	AtapiSendCdb (padapter, pdev, cdb);	
	return FALSE;
	}
/****************************************************************
 *	Name:	InlineReadSignature	:LOCAL
 *
 *	Description:	Do an inline read RAID sigature.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to device.
 *					index	 - index of data to read.
 *
 *	Returns:		Zero if no error or status register contents on error.
 *
 ****************************************************************/
static UCHAR InlineReadSignature (PADAPTER2220I padapter, POUR_DEVICE pdev, int index)
	{
	UCHAR	status;
	ULONG	zl = pdev->lastsectorlba[index];

	SelectSpigot (padapter, pdev->spigots[index] | SEL_IRQ_OFF);	// select the spigot without interrupts
	outb_p (pdev->byte6 | ((UCHAR *)&zl)[3], padapter->regLba24);		
	status = WaitReady (padapter);
	if ( !status )
		{
		outb_p (((UCHAR *)&zl)[2], padapter->regLba16);
		outb_p (((UCHAR *)&zl)[1], padapter->regLba8); 
		outb_p (((UCHAR *)&zl)[0], padapter->regLba0);
		outb_p (1, padapter->regSectCount);
		WriteCommand (padapter, IDE_COMMAND_READ);
		status = WaitDrq (padapter);
		if ( !status )
			{
			insw (padapter->regData, padapter->kBuffer, BYTES_PER_SECTOR / 2);
			((ULONG *)(&pdev->DiskMirror[index]))[0] = ((ULONG *)(&padapter->kBuffer[DISK_MIRROR_POSITION]))[0];
			((ULONG *)(&pdev->DiskMirror[index]))[1] = ((ULONG *)(&padapter->kBuffer[DISK_MIRROR_POSITION]))[1];
			// some drives assert DRQ before IRQ so let's make sure we clear the IRQ
			WaitReady (padapter);
			return 0;			
			}
		}
	return status;
	}
/****************************************************************
 *	Name:	DecodeError	:LOCAL
 *
 *	Description:	Decode and process device errors.
 *
 *	Parameters:		padapter - Pointer to adapter data.
 *					status - Status register code.
 *
 *	Returns:		The driver status code.
 *
 ****************************************************************/
static ULONG DecodeError (PADAPTER2220I	padapter, UCHAR status)
	{
	UCHAR			error;

	padapter->expectingIRQ = 0;
	if ( status & IDE_STATUS_WRITE_FAULT )
		{
		return DID_PARITY << 16;
		}
	if ( status & IDE_STATUS_BUSY )
		return DID_BUS_BUSY << 16;

	error = inb_p (padapter->regError);
	DEB(printk ("\npci2220i error register: %x", error));
	switch ( error )
		{
		case IDE_ERROR_AMNF:
		case IDE_ERROR_TKONF:
		case IDE_ERROR_ABRT:
		case IDE_ERROR_IDFN:
		case IDE_ERROR_UNC:
		case IDE_ERROR_BBK:
		default:
			return DID_ERROR << 16;
		}
	return DID_ERROR << 16;
	}
/****************************************************************
 *	Name:	StartTimer	:LOCAL
 *
 *	Description:	Start the timer.
 *
 *	Parameters:		ipadapter - Pointer adapter data structure.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void StartTimer (PADAPTER2220I padapter)
	{
	padapter->timer.expires = jiffies + TIMEOUT_DATA;
	add_timer (&padapter->timer);
	}
/****************************************************************
 *	Name:	WriteSignature	:LOCAL
 *
 *	Description:	Start the timer.
 *
 *	Parameters:		padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to our device.
 *					spigot	 - Selected spigot.
 *					index	 - index of mirror signature on device.
 *
 *	Returns:		TRUE on any error.
 *
 ****************************************************************/
static int WriteSignature (PADAPTER2220I padapter, POUR_DEVICE pdev, UCHAR spigot, int index)
	{
	ULONG	zl;

	SelectSpigot (padapter, spigot);
	zl = pdev->lastsectorlba[index];
	outb_p (pdev->byte6 | ((UCHAR *)&zl)[3], padapter->regLba24);		
	outb_p (((UCHAR *)&zl)[2], padapter->regLba16);
	outb_p (((UCHAR *)&zl)[1], padapter->regLba8);
	outb_p (((UCHAR *)&zl)[0], padapter->regLba0);
	outb_p (1, padapter->regSectCount);

	WriteCommand (padapter, IDE_COMMAND_WRITE);	
	if ( WaitDrq (padapter) )
		return TRUE;
	StartTimer (padapter);	
	padapter->expectingIRQ = TRUE;
	
	((ULONG *)(&padapter->kBuffer[DISK_MIRROR_POSITION]))[0] = ((ULONG *)(&pdev->DiskMirror[index]))[0];
	((ULONG *)(&padapter->kBuffer[DISK_MIRROR_POSITION]))[1] = ((ULONG *)(&pdev->DiskMirror[index]))[1];
	outsw (padapter->regData, padapter->kBuffer, BYTES_PER_SECTOR / 2);
	return FALSE;
	}
/*******************************************************************************************************
 *	Name:			InitFailover
 *
 *	Description:	This is the beginning of the failover routine
 *
 *	Parameters:		SCpnt	 - Pointer to SCSI command structure.
 *					padapter - Pointer adapter data structure.
 *					pdev	 - Pointer to our device.
 *	
 *	Returns:		TRUE on error.
 *
 ******************************************************************************************************/
static int InitFailover (PADAPTER2220I padapter, POUR_DEVICE pdev)
	{
	UCHAR	spigot;
	
	DEB (printk ("\npci2220i:  Initialize failover process - survivor = %d", pdev->deviceID[padapter->survivor]));
	pdev->raid = FALSE;									//initializes system for non raid mode
	pdev->reconOn = FALSE;
	spigot = pdev->spigots[padapter->survivor];	

	if ( pdev->DiskMirror[padapter->survivor].status & UCBF_REBUILD )
		{
		DEB (printk ("\n         failed, is survivor"));
		return (TRUE); 
		}

	if ( HardReset (padapter, pdev, spigot) )
		{
		DEB (printk ("\n         failed, reset"));
		return TRUE;
		}

	Alarm (padapter, pdev->deviceID[padapter->survivor ^ 1]);
	pdev->DiskMirror[padapter->survivor].status = UCBF_MIRRORED | UCBF_SURVIVOR;	//clear present status
	
	if ( WriteSignature (padapter, pdev, spigot, padapter->survivor) )
		{
		DEB (printk ("\n         failed, write signature"));
		return TRUE;
		}
	padapter->failinprog = TRUE;
	return FALSE;
	}
/****************************************************************
 *	Name:	TimerExpiry	:LOCAL
 *
 *	Description:	Timer expiry routine.
 *
 *	Parameters:		data - Pointer adapter data structure.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void TimerExpiry (unsigned long data)
	{
	PADAPTER2220I	padapter = (PADAPTER2220I)data;
	struct Scsi_Host *host = padapter->SCpnt->device->host;
	POUR_DEVICE		pdev = padapter->pdev;
	UCHAR			status = IDE_STATUS_BUSY;
	UCHAR			temp, temp1;
    unsigned long		flags;

    /*
     * Disable interrupts, if they aren't already disabled and acquire
     * the I/O spinlock.
     */
    spin_lock_irqsave (host->host_lock, flags);
	DEB (printk ("\nPCI2220I: Timeout expired "));

	if ( padapter->failinprog )
		{
		DEB (printk ("in failover process"));
		OpDone (padapter, DecodeError (padapter, inb_p (padapter->regStatCmd)));
		goto timerExpiryDone;
		}
	
	while ( padapter->reconPhase )
		{
		DEB (printk ("in recon phase %X", padapter->reconPhase));
		switch ( padapter->reconPhase )
			{
			case RECON_PHASE_MARKING:
			case RECON_PHASE_LAST:
				padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 1 : 0;
				DEB (printk ("\npci2220i: FAILURE 1"));
				if ( InitFailover (padapter, pdev) )
					OpDone (padapter, DID_ERROR << 16);
				goto timerExpiryDone;
			
			case RECON_PHASE_READY:
				OpDone (padapter, DID_ERROR << 16);
				goto timerExpiryDone;

			case RECON_PHASE_COPY:
				padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
				DEB (printk ("\npci2220i: FAILURE 2"));
				DEB (printk ("\n       spig/stat = %X", inb_p (padapter->regStatSel));
				if ( InitFailover (padapter, pdev) )
					OpDone (padapter, DID_ERROR << 16);
				goto timerExpiryDone;

			case RECON_PHASE_UPDATE:
				padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
				DEB (printk ("\npci2220i: FAILURE 3")));
				if ( InitFailover (padapter, pdev) )
					OpDone (padapter, DID_ERROR << 16);
				goto timerExpiryDone;

			case RECON_PHASE_END:
				padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
				DEB (printk ("\npci2220i: FAILURE 4"));
				if ( InitFailover (padapter, pdev) )
					OpDone (padapter, DID_ERROR << 16);
				goto timerExpiryDone;
			
			default:
				goto timerExpiryDone;
			}
		}
	
	while ( padapter->cmd )
		{
		outb_p (0x08, padapter->regDmaCmdStat);					// cancel interrupt from DMA engine
		if ( pdev->raid )
			{
			if ( padapter->cmd == WRITE_CMD )
				{
				DEB (printk ("in RAID write operation"));
				temp = ( pdev->spigot & (SEL_1 | SEL_2) ) ? SEL_1 : SEL_3;
				if ( inb_p (padapter->regStatSel) & temp )
					{
					DEB (printk ("\npci2220i: Determined A OK"));
					SelectSpigot (padapter, temp | SEL_IRQ_OFF); // Masking the interrupt during spigot select
					temp = inb_p (padapter->regStatCmd);
					}
				else
					temp = IDE_STATUS_BUSY;

				temp1 = ( pdev->spigot & (SEL_1 | SEL_2) ) ? SEL_2 : SEL_4;
				if ( inb (padapter->regStatSel) & temp1 )
					{
					DEB (printk ("\npci2220i: Determined B OK"));
					SelectSpigot (padapter, temp1 | SEL_IRQ_OFF); // Masking the interrupt during spigot select
					temp1 = inb_p (padapter->regStatCmd);
					}
				else
					temp1 = IDE_STATUS_BUSY;
			
				if ( (temp & IDE_STATUS_BUSY) || (temp1 & IDE_STATUS_BUSY) )
					{
					DEB (printk ("\npci2220i: Status A: %X   B: %X", temp & 0xFF, temp1 & 0xFF));
	 				if ( (temp & IDE_STATUS_BUSY) && (temp1 & IDE_STATUS_BUSY) ) 
						{
						status = temp;
						break;
						}		
					else	
						{
						if ( temp & IDE_STATUS_BUSY )
							padapter->survivor = 1;
						else
							padapter->survivor = 0;
						if ( InitFailover (padapter, pdev) )
							{
							status = inb_p (padapter->regStatCmd);
							break;
							}
						goto timerExpiryDone;
						}
					}
				}
			else
				{
				DEB (printk ("in RAID read operation"));
				padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
				DEB (printk ("\npci2220i: FAILURE 6"));
				if ( InitFailover (padapter, pdev) )
					{
					status = inb_p (padapter->regStatCmd);
					break;
					}
				goto timerExpiryDone;
				}
			}
		else
			{
			DEB (printk ("in I/O operation"));
			status = inb_p (padapter->regStatCmd);
			}
		break;
		}
	
	OpDone (padapter, DecodeError (padapter, status));

timerExpiryDone:;
    /*
     * Release the I/O spinlock and restore the original flags
     * which will enable interrupts if and only if they were
     * enabled on entry.
     */
    spin_unlock_irqrestore (host->host_lock, flags);
	}
/****************************************************************
 *	Name:			SetReconstruct	:LOCAL
 *
 *	Description:	Set the reconstruct up.
 *
 *	Parameters:		pdev	- Pointer to device structure.
 *					index	- Mirror index number.
 *
 *	Returns:		Number of sectors on new disk required.
 *
 ****************************************************************/
static LONG SetReconstruct (POUR_DEVICE pdev, int index)
	{
	pdev->DiskMirror[index].status = UCBF_MIRRORED;							// setup the flags
	pdev->DiskMirror[index ^ 1].status = UCBF_MIRRORED | UCBF_REBUILD;
	pdev->DiskMirror[index ^ 1].reconstructPoint = 0;						// start the reconstruct
	pdev->reconCount = 1990;												// mark target drive early
	return pdev->DiskMirror[index].reconstructPoint;
	}
/****************************************************************
 *	Name:	ReconTimerExpiry	:LOCAL
 *
 *	Description:	Reconstruct timer expiry routine.
 *
 *	Parameters:		data - Pointer adapter data structure.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static void ReconTimerExpiry (unsigned long data)
	{
	PADAPTER2220I	padapter = (PADAPTER2220I)data;
	struct Scsi_Host *host = padapter->SCpnt->device->host;
	POUR_DEVICE		pdev;
	ULONG			testsize = 0;
	PIDENTIFY_DATA	pid;
	USHORT			minmode;
	ULONG			zl;
	UCHAR			zc;
	USHORT			z;
    unsigned long	flags;

    /*
     * Disable interrupts, if they aren't already disabled and acquire
     * the I/O spinlock.
     */
    spin_lock_irqsave(host->host_lock, flags);

	if ( padapter->SCpnt )
		goto reconTimerExpiry;

	padapter->reconTimer.data = 0;
	for ( z = padapter->devInReconIndex + 1;  z < BIGD_MAXDRIVES;  z++ )
		{
		if ( padapter->device[z].reconOn )
			break;
		}
	if ( z < BIGD_MAXDRIVES )
		pdev = &padapter->device[z];
	else
		{
		for ( z = 0;  z < BIGD_MAXDRIVES;  z++ )
			{
			if ( padapter->device[z].reconOn )
				break;
			}
		if ( z < BIGD_MAXDRIVES )
			pdev = &padapter->device[z];
		else
			{
			padapter->reconOn = FALSE;
			goto reconTimerExpiry;
			}
		}

	padapter->devInReconIndex = z;
	pid = (PIDENTIFY_DATA)padapter->kBuffer;
	padapter->pdev = pdev;
	if ( pdev->reconIsStarting )
		{
		pdev->reconIsStarting = FALSE;
		pdev->reconOn = FALSE;

		while ( (pdev->DiskMirror[0].signature == SIGNATURE) && (pdev->DiskMirror[1].signature == SIGNATURE) &&
			 (pdev->DiskMirror[0].pairIdentifier == (pdev->DiskMirror[1].pairIdentifier ^ 1)) )
			{
			if ( (pdev->DiskMirror[0].status & UCBF_MATCHED) && (pdev->DiskMirror[1].status & UCBF_MATCHED) )
				break;

			if ( pdev->DiskMirror[0].status & UCBF_SURVIVOR )				// is first drive survivor?
				testsize = SetReconstruct (pdev, 0);
			else
				if ( pdev->DiskMirror[1].status & UCBF_SURVIVOR )			// is second drive survivor?
					testsize = SetReconstruct (pdev, 1);

			if ( (pdev->DiskMirror[0].status & UCBF_REBUILD) || (pdev->DiskMirror[1].status & UCBF_REBUILD) )
				{
				if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
					pdev->mirrorRecon = 0;
				else
					pdev->mirrorRecon = 1;
				pdev->reconOn = TRUE;
				}
			break;
			}

		if ( !pdev->reconOn )
			goto reconTimerExpiry;

		if ( padapter->bigD )
			{
			padapter->failRegister = 0;
			outb_p (~padapter->failRegister, padapter->regFail);
			}
		else
			{
			zc = ((inb_p (padapter->regStatSel) >> 3) | inb_p (padapter->regStatSel)) & 0x83;		// mute the alarm
			outb_p (0xFF, padapter->regFail);
			}

		while ( 1 )
			{
			DEB (printk ("\npci2220i: hard reset issue"));
			if ( HardReset (padapter, pdev, pdev->spigots[pdev->mirrorRecon]) )
				{
				DEB (printk ("\npci2220i: sub 1"));
				break;
				}

			pdev->lastsectorlba[pdev->mirrorRecon] = InlineIdentify (padapter, pdev->spigots[pdev->mirrorRecon], pdev->deviceID[pdev->mirrorRecon] & 1);

			if ( pdev->lastsectorlba[pdev->mirrorRecon] < testsize )
				{
				DEB (printk ("\npci2220i: sub 2 %ld %ld", pdev->lastsectorlba[pdev->mirrorRecon], testsize));
				break;
				}

	        // test LBA and multiper sector transfer compatibility
			if (!pid->SupportLBA || (pid->NumSectorsPerInt < SECTORSXFER) || !pid->Valid_64_70 )
				{
				DEB (printk ("\npci2220i: sub 3"));
				break;
				}

	        // test PIO/bus matering mode compatibility
			if ( (pid->MinPIOCycleWithoutFlow > 240) && !pid->SupportIORDYDisable && !padapter->timingPIO )
				{
				DEB (printk ("\npci2220i: sub 4"));
				break;
				}

			if ( pid->MinPIOCycleWithoutFlow <= 120 )	// setup timing mode of drive
				minmode = 5;
			else
				{
				if ( pid->MinPIOCylceWithFlow <= 150 )
					minmode = 4;
				else
					{
					if ( pid->MinPIOCylceWithFlow <= 180 )
						minmode = 3;
					else
						{
						if ( pid->MinPIOCylceWithFlow <= 240 )
							minmode = 2;
						else
							{
							DEB (printk ("\npci2220i: sub 5"));
							break;
							}
						}
					}
				}

			if ( padapter->timingMode > minmode )									// set minimum timing mode
				padapter->timingMode = minmode;
			if ( padapter->timingMode >= 2 )
				padapter->timingAddress	= ModeArray[padapter->timingMode - 2];
			else
				padapter->timingPIO = TRUE;

			padapter->reconOn = TRUE;
			break;
			}

		if ( !pdev->reconOn )
			{		
			padapter->survivor = pdev->mirrorRecon ^ 1;
			padapter->reconPhase = RECON_PHASE_FAILOVER;
			DEB (printk ("\npci2220i: FAILURE 7"));
			InitFailover (padapter, pdev);
			goto reconTimerExpiry;
			}

		pdev->raid = TRUE;
	
		if ( WriteSignature (padapter, pdev, pdev->spigot, pdev->mirrorRecon ^ 1) )
			goto reconTimerExpiry;
		padapter->reconPhase = RECON_PHASE_MARKING;
		goto reconTimerExpiry;
		}

	//**********************************
	// reconstruct copy starts here	
	//**********************************
	if ( pdev->reconCount++ > 2000 )
		{
		pdev->reconCount = 0;
		if ( WriteSignature (padapter, pdev, pdev->spigots[pdev->mirrorRecon], pdev->mirrorRecon) )
			{
			padapter->survivor = pdev->mirrorRecon ^ 1;
			padapter->reconPhase = RECON_PHASE_FAILOVER;
			DEB (printk ("\npci2220i: FAILURE 8"));
			InitFailover (padapter, pdev);
			goto reconTimerExpiry;
			}
		padapter->reconPhase = RECON_PHASE_UPDATE;
		goto reconTimerExpiry;
		}

	zl = pdev->DiskMirror[pdev->mirrorRecon].reconstructPoint;	
	padapter->reconSize = pdev->DiskMirror[pdev->mirrorRecon ^ 1].reconstructPoint - zl;
	if ( padapter->reconSize > MAX_BUS_MASTER_BLOCKS )
		padapter->reconSize = MAX_BUS_MASTER_BLOCKS;

	if ( padapter->reconSize )
		{
		SelectSpigot (padapter, pdev->spigots[0] | pdev->spigots[1]);	// select the spigots
		outb_p (pdev->byte6 | ((UCHAR *)(&zl))[3], padapter->regLba24);	// select the drive
		SelectSpigot (padapter, pdev->spigot);
		if ( WaitReady (padapter) )
			goto reconTimerExpiry;

		SelectSpigot (padapter, pdev->spigots[pdev->mirrorRecon]);
		if ( WaitReady (padapter) )
			{
			padapter->survivor = pdev->mirrorRecon ^ 1;
			padapter->reconPhase = RECON_PHASE_FAILOVER;
			DEB (printk ("\npci2220i: FAILURE 9"));
			InitFailover (padapter, pdev);
			goto reconTimerExpiry;
			}
	
		SelectSpigot (padapter, pdev->spigots[0] | pdev->spigots[1]);
		outb_p (padapter->reconSize & 0xFF, padapter->regSectCount);
		outb_p (((UCHAR *)(&zl))[0], padapter->regLba0);
		outb_p (((UCHAR *)(&zl))[1], padapter->regLba8);
		outb_p (((UCHAR *)(&zl))[2], padapter->regLba16);
		padapter->expectingIRQ = TRUE;
		padapter->reconPhase = RECON_PHASE_READY;
		SelectSpigot (padapter, pdev->spigots[pdev->mirrorRecon]);
		WriteCommand (padapter, WRITE_CMD);
		StartTimer (padapter);
		SelectSpigot (padapter, pdev->spigot);
		WriteCommand (padapter, READ_CMD);
		goto reconTimerExpiry;
		}

	pdev->DiskMirror[pdev->mirrorRecon].status = UCBF_MIRRORED | UCBF_MATCHED;
	pdev->DiskMirror[pdev->mirrorRecon ^ 1].status = UCBF_MIRRORED | UCBF_MATCHED;
	if ( WriteSignature (padapter, pdev, pdev->spigot, pdev->mirrorRecon ^ 1) )
		goto reconTimerExpiry;
	padapter->reconPhase = RECON_PHASE_LAST;

reconTimerExpiry:;
    /*
     * Release the I/O spinlock and restore the original flags
     * which will enable interrupts if and only if they were
     * enabled on entry.
     */
    spin_unlock_irqrestore(host->host_lock, flags);
	}
/****************************************************************
 *	Name:	Irq_Handler	:LOCAL
 *
 *	Description:	Interrupt handler.
 *
 *	Parameters:		irq		- Hardware IRQ number.
 *					dev_id	-
 *					regs	-
 *
 *	Returns:		TRUE if drive is not ready in time.
 *
 ****************************************************************/
static irqreturn_t Irq_Handler (int irq, void *dev_id, struct pt_regs *regs)
	{
	struct Scsi_Host   *shost = NULL;	// Pointer to host data block
	PADAPTER2220I		padapter;		// Pointer to adapter control structure
	POUR_DEVICE			pdev;
	Scsi_Cmnd		   *SCpnt;
	UCHAR				status;
	UCHAR				status1;
	ATAPI_STATUS		statusa;
	ATAPI_REASON		reasona;
	ATAPI_ERROR			errora;
	int					z;
	ULONG				zl;
    unsigned long		flags;
    int handled = 0;

//	DEB (printk ("\npci2220i received interrupt\n"));

	for ( z = 0; z < NumAdapters;  z++ )								// scan for interrupt to process
		{
		if ( PsiHost[z]->irq == (UCHAR)(irq & 0xFF) )
			{
			if ( inw_p (HOSTDATA(PsiHost[z])->regIrqControl) & 0x8000 )
				{
				shost = PsiHost[z];
				break;
				}
			}
		}

	if ( !shost )
		{
		DEB (printk ("\npci2220i: not my interrupt"));
		goto out;
		}

	handled = 1;
	spin_lock_irqsave(shost->host_lock, flags);
	padapter = HOSTDATA(shost);
	pdev = padapter->pdev;
	SCpnt = padapter->SCpnt;
	outb_p (0x08, padapter->regDmaCmdStat);									// cancel interrupt from DMA engine

	if ( padapter->atapi && SCpnt )
		{
		*(char *)&statusa = inb_p (padapter->regStatCmd);						// read the device status
		*(char *)&reasona = inb_p (padapter->regSectCount);						// read the device interrupt reason
	
		if ( !statusa.bsy )
			{
			if ( statusa.drq )													// test for transfer phase
				{
				if ( !reasona.cod )												// test for data phase
					{
					z = (ULONG)inb_p (padapter->regLba8) | (ULONG)(inb_p (padapter->regLba16) << 8);
					if ( padapter->reqSense )
						insw (padapter->regData, SCpnt->sense_buffer, z / 2);
					else
						AtapiBusMaster (padapter, reasona.io, z);
					goto irq_return;
					}
				if ( reasona.cod && !reasona.io )								// test for command packet phase
					{
					if ( padapter->reqSense )
						AtapiRequestSense (padapter, pdev, SCpnt, TRUE);
					else
						AtapiSendCdb (padapter, pdev, padapter->atapiCdb);
					goto irq_return;
					}
				}
			else
				{
				if ( reasona.io && statusa.drdy )								// test for status phase
					{
					Atapi2Scsi (padapter, SCpnt);
					if ( statusa.check )
						{
						*(UCHAR *)&errora = inb_p (padapter->regError);			// read the device error
						if ( errora.senseKey )
							{
							if ( padapter->reqSense || AtapiRequestSense (padapter, pdev, SCpnt, FALSE) )
								OpDone (padapter, DID_ERROR << 16);
							}
						else
							{
							if ( errora.ili || errora.abort )
								OpDone (padapter, DID_ERROR << 16);
							else
								OpDone (padapter, DID_OK << 16);	
							}
						}
					else
						if ( padapter->reqSense )
							{
							DEB (printk ("PCI2242I: Sense codes - %X %X %X ", ((UCHAR *)SCpnt->sense_buffer)[0], ((UCHAR *)SCpnt->sense_buffer)[12], ((UCHAR *)SCpnt->sense_buffer)[13]));
							OpDone (padapter, (DRIVER_SENSE << 24) | (DID_OK << 16) | 2);
							}
						else
							OpDone (padapter, DID_OK << 16);	
					}
				}
			}		
		goto irq_return;
		}
	
	if ( !padapter->expectingIRQ || !(SCpnt || padapter->reconPhase) )
		{
		DEB(printk ("\npci2220i Unsolicited interrupt\n"));
		STOP_HERE ();
		goto irq_return;
		}
	padapter->expectingIRQ = 0;

	if ( padapter->failinprog )
		{
		DEB (printk ("\npci2220i interrupt failover complete"));
		padapter->failinprog = FALSE;
		status = inb_p (padapter->regStatCmd);								// read the device status
		if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
			{
			DEB (printk ("\npci2220i: interrupt failover error from drive %X", status));
			padapter->cmd = 0;
			}
		else
			{
			DEB (printk ("\npci2220i: restarting failed opertation."));
			pdev->spigot = (padapter->survivor) ? pdev->spigots[1] : pdev->spigots[0];
			del_timer (&padapter->timer);
			if ( padapter->reconPhase )
				OpDone (padapter, DID_OK << 16);
			else
				Pci2220i_QueueCommand (SCpnt, SCpnt->scsi_done);
			goto irq_return;		
			}
		}

	if ( padapter->reconPhase )
		{
		switch ( padapter->reconPhase )
			{
			case RECON_PHASE_MARKING:
			case RECON_PHASE_LAST:
				status = inb_p (padapter->regStatCmd);						// read the device status
				del_timer (&padapter->timer);
				if ( padapter->reconPhase == RECON_PHASE_LAST )
					{
					if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
						{
						padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 1 : 0;
						DEB (printk ("\npci2220i: FAILURE 10"));
						if ( InitFailover (padapter, pdev) )
							OpDone (padapter, DecodeError (padapter, status));
						goto irq_return;
						}
					if ( WriteSignature (padapter, pdev, pdev->spigots[pdev->mirrorRecon], pdev->mirrorRecon) )
						{
						padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
						DEB (printk ("\npci2220i: FAILURE 11"));
						if ( InitFailover (padapter, pdev) )
							OpDone (padapter, DecodeError (padapter, status));
						goto irq_return;
						}
					padapter->reconPhase = RECON_PHASE_END;	
					goto irq_return;
					}
				OpDone (padapter, DID_OK << 16);
				goto irq_return;

			case RECON_PHASE_READY:
				status = inb_p (padapter->regStatCmd);						// read the device status
				if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
					{
					del_timer (&padapter->timer);
					OpDone (padapter, DecodeError (padapter, status));
					goto irq_return;
					}
				SelectSpigot (padapter, pdev->spigots[pdev->mirrorRecon]);
				if ( WaitDrq (padapter) )
					{
					del_timer (&padapter->timer);
					padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
					DEB (printk ("\npci2220i: FAILURE 12"));
					if ( InitFailover (padapter, pdev) )
						OpDone (padapter, DecodeError (padapter, status));
					goto irq_return;
					}
				SelectSpigot (padapter, pdev->spigot | SEL_COPY | padapter->bigD);
				padapter->reconPhase = RECON_PHASE_COPY;
				padapter->expectingIRQ = TRUE;
				if ( padapter->timingPIO )
					{
					insw (padapter->regData, padapter->kBuffer, padapter->reconSize * (BYTES_PER_SECTOR / 2));
					}
				else
					{
					if ( (padapter->timingMode > 3) )
						{
						if ( padapter->bigD )
							outl (BIGD_DATA_MODE3, padapter->regDmaAddrLoc);
						else
							outl (DALE_DATA_MODE3, padapter->regDmaAddrLoc);
						}
					else
						outl (padapter->timingAddress, padapter->regDmaAddrLoc);
					outl (padapter->kBufferDma, padapter->regDmaAddrPci);
					outl (padapter->reconSize * BYTES_PER_SECTOR, padapter->regDmaCount);
					outb_p (8, padapter->regDmaDesc);						// read operation
					if ( padapter->bigD )
						outb_p (8, padapter->regDmaMode);					// no interrupt
					else
						outb_p (1, padapter->regDmaMode);					// no interrupt
					outb_p (0x03, padapter->regDmaCmdStat);					// kick the DMA engine in gear
					}
				goto irq_return;

			case RECON_PHASE_COPY:
				pdev->DiskMirror[pdev->mirrorRecon].reconstructPoint += padapter->reconSize;

			case RECON_PHASE_UPDATE:
				SelectSpigot (padapter, pdev->spigots[pdev->mirrorRecon] | SEL_IRQ_OFF);
				status = inb_p (padapter->regStatCmd);						// read the device status
				del_timer (&padapter->timer);
				if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
					{
					padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
					DEB (printk ("\npci2220i: FAILURE 13"));
					DEB (printk ("\n  status register = %X   error = %X", status, inb_p (padapter->regError)));
					if ( InitFailover (padapter, pdev) )
						OpDone (padapter, DecodeError (padapter, status));
					goto irq_return;
					}
				OpDone (padapter, DID_OK << 16);
				goto irq_return;

			case RECON_PHASE_END:
				status = inb_p (padapter->regStatCmd);						// read the device status
				del_timer (&padapter->timer);
				if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
					{
					padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 0 : 1;
					DEB (printk ("\npci2220i: FAILURE 14"));
					if ( InitFailover (padapter, pdev) )
						OpDone (padapter, DecodeError (padapter, status));
					goto irq_return;
					}
				pdev->reconOn = 0;
				if ( padapter->bigD )
					{
					for ( z = 0;  z < padapter->numberOfDrives;  z++ )
						{
						if ( padapter->device[z].DiskMirror[0].status & UCBF_SURVIVOR )
							{
							Alarm (padapter, padapter->device[z].deviceID[0] ^ 2);
							MuteAlarm (padapter);
							}
						if ( padapter->device[z].DiskMirror[1].status & UCBF_SURVIVOR )
							{
							Alarm (padapter, padapter->device[z].deviceID[1] ^ 2);
							MuteAlarm (padapter);
							}
						}
					}
				OpDone (padapter, DID_OK << 16);
				goto irq_return;

			default:
				goto irq_return;
			}
		}
		
	switch ( padapter->cmd )												// decide how to handle the interrupt
		{
		case READ_CMD:
			if ( padapter->sectorCount )
				{
				status = inb_p (padapter->regStatCmd);						// read the device status
				if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
					{
					if ( pdev->raid )
						{
						padapter->survivor = ( pdev->spigot == pdev->spigots[0] ) ? 1 : 0;
						del_timer (&padapter->timer);
						DEB (printk ("\npci2220i: FAILURE 15"));
						if ( !InitFailover (padapter, pdev) )
							goto irq_return;
						}
					break;	
					}
				if ( padapter->timingPIO )
					{
					insw (padapter->regData, padapter->kBuffer, padapter->readCount / 2);
					padapter->sectorCount -= padapter->readCount / BYTES_PER_SECTOR;
					WalkScatGath (padapter, TRUE, padapter->readCount);
					if ( !padapter->sectorCount )
						{
						status = 0;
						break;
						}
					}
				else
					{
					if ( padapter->readCount )
						WalkScatGath (padapter, TRUE, padapter->readCount);
					BusMaster (padapter, 1, 1);
					}
				padapter->expectingIRQ = TRUE;
				goto irq_return;
				}
			if ( padapter->readCount && !padapter->timingPIO )
				WalkScatGath (padapter, TRUE, padapter->readCount);
			status = 0;
			break;

		case WRITE_CMD:
			if ( pdev->raid )
				{
				SelectSpigot (padapter, pdev->spigots[0] | SEL_IRQ_OFF);				
				status = inb_p (padapter->regStatCmd);								// read the device status
				SelectSpigot (padapter, pdev->spigots[1] | SEL_IRQ_OFF);				
				status1 = inb_p (padapter->regStatCmd);								// read the device status
				}
			else
				SelectSpigot (padapter, pdev->spigot | SEL_IRQ_OFF);				
				status = inb_p (padapter->regStatCmd);								// read the device status
				status1 = 0;
		
			if ( status & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
				{	
				if ( pdev->raid && !(status1 & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT)) )
					{
					padapter->survivor = 1;
					del_timer (&padapter->timer);
					SelectSpigot (padapter, pdev->spigot | SEL_IRQ_OFF);
					DEB (printk ("\npci2220i: FAILURE 16  status = %X  error = %X", status, inb_p (padapter->regError)));
					if ( !InitFailover (padapter, pdev) )
						goto irq_return;
					}
				break;
				}
			if ( pdev->raid )
				{
				if ( status1 & (IDE_STATUS_ERROR | IDE_STATUS_WRITE_FAULT) )
					{	
					padapter->survivor = 0;
					del_timer (&padapter->timer);
					DEB (printk ("\npci2220i: FAILURE 17  status = %X  error = %X", status1, inb_p (padapter->regError)));
					if ( !InitFailover (padapter, pdev) )
						goto irq_return;
					status = status1;
					break;
					}
				if ( padapter->sectorCount )
					{
					status = WriteDataBoth (padapter, pdev);
					if ( status )
						{
						padapter->survivor = status >> 1;
						del_timer (&padapter->timer);
						DEB (printk ("\npci2220i: FAILURE 18"));
						if ( !InitFailover (padapter, pdev) )
							goto irq_return;
						SelectSpigot (padapter, pdev->spigots[status] | SEL_IRQ_OFF);				
						status = inb_p (padapter->regStatCmd);								// read the device status
						break;
						}
					padapter->expectingIRQ = TRUE;
					goto irq_return;
					}
				status = 0;
				break;
				}
			if ( padapter->sectorCount )	
				{	
				SelectSpigot (padapter, pdev->spigot | padapter->bigD);
				status = WriteData (padapter);
				if ( status )
					break;
				padapter->expectingIRQ = TRUE;
				goto irq_return;
				}
			status = 0;
			break;

		case IDE_COMMAND_IDENTIFY:
			{
			PINQUIRYDATA	pinquiryData  = SCpnt->request_buffer;
			PIDENTIFY_DATA	pid = (PIDENTIFY_DATA)padapter->kBuffer;

			status = inb_p (padapter->regStatCmd);
			if ( status & IDE_STATUS_DRQ )
				{
				insw (padapter->regData, pid, sizeof (IDENTIFY_DATA) >> 1);

				memset (pinquiryData, 0, SCpnt->request_bufflen);		// Zero INQUIRY data structure.
				pinquiryData->DeviceType = 0;
				pinquiryData->Versions = 2;
				pinquiryData->AdditionalLength = 35 - 4;

				// Fill in vendor identification fields.
				for ( z = 0;  z < 20;  z += 2 )
					{
					pinquiryData->VendorId[z]	  = ((UCHAR *)pid->ModelNumber)[z + 1];
					pinquiryData->VendorId[z + 1] = ((UCHAR *)pid->ModelNumber)[z];
					}

				// Initialize unused portion of product id.
				for ( z = 0;  z < 4;  z++ )
					pinquiryData->ProductId[12 + z] = ' ';

				// Move firmware revision from IDENTIFY data to
				// product revision in INQUIRY data.
				for ( z = 0;  z < 4;  z += 2 )
					{
					pinquiryData->ProductRevisionLevel[z]	 = ((UCHAR *)pid->FirmwareRevision)[z + 1];
					pinquiryData->ProductRevisionLevel[z + 1] = ((UCHAR *)pid->FirmwareRevision)[z];
					}
				if ( pdev == padapter->device )
					*((USHORT *)(&pinquiryData->VendorSpecific)) = DEVICE_DALE_1;
				
				status = 0;
				}
			break;
			}

		default:
			status = 0;
			break;
		}

	del_timer (&padapter->timer);
	if ( status )
		{
		DEB (printk ("\npci2220i Interrupt handler return error"));
		zl = DecodeError (padapter, status);
		}
	else
		zl = DID_OK << 16;

	OpDone (padapter, zl);
irq_return:
    spin_unlock_irqrestore(shost->host_lock, flags);
out:
	return IRQ_RETVAL(handled);
}

/****************************************************************
 *	Name:	Pci2220i_QueueCommand
 *
 *	Description:	Process a queued command from the SCSI manager.
 *
 *	Parameters:		SCpnt - Pointer to SCSI command structure.
 *					done  - Pointer to done function to call.
 *
 *	Returns:		Status code.
 *
 ****************************************************************/
int Pci2220i_QueueCommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
	{
	UCHAR		   *cdb = (UCHAR *)SCpnt->cmnd;					// Pointer to SCSI CDB
	PADAPTER2220I	padapter = HOSTDATA(SCpnt->device->host);			// Pointer to adapter control structure
	POUR_DEVICE		pdev	 = &padapter->device[SCpnt->device->id];// Pointer to device information
	UCHAR			rc;											// command return code
	int				z; 
	PDEVICE_RAID1	pdr;

	SCpnt->scsi_done = done;
	padapter->SCpnt = SCpnt;  									// Save this command data
	padapter->readCount = 0;

	if ( SCpnt->use_sg )
		{
		padapter->currentSgBuffer = ((struct scatterlist *)SCpnt->request_buffer)[0].address;
		padapter->currentSgCount = ((struct scatterlist *)SCpnt->request_buffer)[0].length;
		}
	else
		{
		padapter->currentSgBuffer = SCpnt->request_buffer;
		padapter->currentSgCount = SCpnt->request_bufflen;
		}
	padapter->nextSg = 1;

	if ( !done )
		{
		printk("pci2220i_queuecommand: %02X: done can't be NULL\n", *cdb);
		return 0;
		}
	
	if ( padapter->atapi )
		{
		UCHAR			zlo, zhi;

		DEB (printk ("\nPCI2242I: ID %d, LUN %d opcode %X ", SCpnt->device->id, SCpnt->device->lun, *cdb));
		padapter->pdev = pdev;
		if ( !pdev->byte6 || SCpnt->device->lun )
			{
			OpDone (padapter, DID_BAD_TARGET << 16);
			return 0;
			}
	
		padapter->atapiSpecial = FALSE;
		padapter->reqSense = FALSE;
		memset (padapter->atapiCdb, 0, 16);
		SelectSpigot (padapter, pdev->spigot);									// select the spigot
		AtapiDevice (padapter, pdev->byte6);									// select the drive
		if ( AtapiWaitReady (padapter, 100) )
			{
			OpDone (padapter, DID_NO_CONNECT << 16);
			return 0;
			}

		switch ( cdb[0] ) 
			{
			case SCSIOP_MODE_SENSE:
			case SCSIOP_MODE_SELECT:
				Scsi2Atapi (padapter, SCpnt);
				z = SCpnt->request_bufflen + 4;
				break;
			case SCSIOP_READ6:
			case SCSIOP_WRITE6:
				Scsi2Atapi (padapter, SCpnt);
				z = SCpnt->request_bufflen;
				break;
			default:
				memcpy (padapter->atapiCdb, cdb, SCpnt->cmd_len);
				z = SCpnt->request_bufflen;
				break;
			}
		if ( z > ATAPI_TRANSFER )
			z = ATAPI_TRANSFER;
	    zlo = (UCHAR)(z & 0xFF);
	    zhi = (UCHAR)(z >> 8);

	 	AtapiCountLo (padapter, zlo);						
	   	AtapiCountHi (padapter, zhi);						
	   	outb_p (0, padapter->regError);						
		WriteCommand (padapter, IDE_COMMAND_ATAPI_PACKET);
		if ( pdev->cmdDrqInt )
			return 0;

		if ( AtapiWaitDrq (padapter, 500) )
			{
			OpDone (padapter, DID_ERROR << 16);
			return 0;
			}
		AtapiSendCdb (padapter, pdev, padapter->atapiCdb);	
		return 0;
		}
	
	if ( padapter->reconPhase )
		return 0;
	if ( padapter->reconTimer.data )
		{
		del_timer (&padapter->reconTimer);
		padapter->reconTimer.data = 0;
		}
		
	if ( (SCpnt->device->id >= padapter->numberOfDrives) || SCpnt->device->lun )
		{
		OpDone (padapter, DID_BAD_TARGET << 16);
		return 0;
		}
	
	switch ( *cdb )
		{
		case SCSIOP_INQUIRY:   					// inquiry CDB
			{
			if ( cdb[2] == SC_MY_RAID )
				{
				switch ( cdb[3] ) 
					{
					case MY_SCSI_REBUILD:
						for ( z = 0;  z < padapter->numberOfDrives;  z++ )
							{
							pdev = &padapter->device[z];
							if ( ((pdev->DiskMirror[0].status & UCBF_SURVIVOR) && (pdev->DiskMirror[1].status & UCBF_MIRRORED)) ||
								 ((pdev->DiskMirror[1].status & UCBF_SURVIVOR) && (pdev->DiskMirror[0].status & UCBF_MIRRORED)) )
								{
								padapter->reconOn = pdev->reconOn = pdev->reconIsStarting = TRUE;
								}
							}
						OpDone (padapter, DID_OK << 16);
						break;
					case MY_SCSI_ALARMMUTE:
						MuteAlarm (padapter);
						OpDone (padapter, DID_OK << 16);
						break;
					case MY_SCSI_DEMOFAIL:
						padapter->demoFail = TRUE;				
						OpDone (padapter, DID_OK << 16);
						break;
					default:
						z = cdb[5];				// get index
						pdr = (PDEVICE_RAID1)SCpnt->request_buffer;
						if ( padapter->raidData[z] )
							{
							memcpy (&pdr->DiskRaid1, padapter->raidData[z], sizeof (DISK_MIRROR));
							if ( padapter->raidData[z]->reconstructPoint > padapter->raidData[z ^ 2]->reconstructPoint )
								pdr->TotalSectors = padapter->raidData[z]->reconstructPoint;
							else
								pdr->TotalSectors = padapter->raidData[z ^ 2]->reconstructPoint;
							}
						else
							memset (pdr, 0, sizeof (DEVICE_RAID1));
						OpDone (padapter, DID_OK << 16);
						break;
					}	
				return 0;
				}
			padapter->cmd = IDE_COMMAND_IDENTIFY;
			break;
			}

		case SCSIOP_TEST_UNIT_READY:			// test unit ready CDB
			OpDone (padapter, DID_OK << 16);
			return 0;
		case SCSIOP_READ_CAPACITY:			  	// read capctiy CDB
			{
			PREAD_CAPACITY_DATA	pdata = (PREAD_CAPACITY_DATA)SCpnt->request_buffer;

			pdata->blksiz = 0x20000;
			XANY2SCSI ((UCHAR *)&pdata->blks, pdev->blocks);
			OpDone (padapter, DID_OK << 16);
			return 0;
			}
		case SCSIOP_VERIFY:						// verify CDB
			padapter->startSector = XSCSI2LONG (&cdb[2]);
			padapter->sectorCount = (UCHAR)((USHORT)cdb[8] | ((USHORT)cdb[7] << 8));
			padapter->cmd = IDE_COMMAND_VERIFY;
			break;
		case SCSIOP_READ:						// read10 CDB
			padapter->startSector = XSCSI2LONG (&cdb[2]);
			padapter->sectorCount = (USHORT)cdb[8] | ((USHORT)cdb[7] << 8);
			padapter->cmd = READ_CMD;
			break;
		case SCSIOP_READ6:						// read6  CDB
			padapter->startSector = SCSI2LONG (&cdb[1]);
			padapter->sectorCount = cdb[4];
			padapter->cmd = READ_CMD;
			break;
		case SCSIOP_WRITE:						// write10 CDB
			padapter->startSector = XSCSI2LONG (&cdb[2]);
			padapter->sectorCount = (USHORT)cdb[8] | ((USHORT)cdb[7] << 8);
			padapter->cmd = WRITE_CMD;
			break;
		case SCSIOP_WRITE6:						// write6  CDB
			padapter->startSector = SCSI2LONG (&cdb[1]);
			padapter->sectorCount = cdb[4];
			padapter->cmd = WRITE_CMD;
			break;
		default:
			DEB (printk ("pci2220i_queuecommand: Unsupported command %02X\n", *cdb));
			OpDone (padapter, DID_ERROR << 16);
			return 0;
		}

	if ( padapter->reconPhase )
		return 0;
	
	padapter->pdev = pdev;

	while ( padapter->demoFail )
		{
		pdev = padapter->pdev = &padapter->device[0];
		padapter->demoFail = FALSE;
		if ( !pdev->raid || 
			 (pdev->DiskMirror[0].status & UCBF_SURVIVOR) || 
			 (pdev->DiskMirror[1].status & UCBF_SURVIVOR) )
			{
			break;
			}
		if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
			padapter->survivor = 1;
		else
			padapter->survivor = 0;
				DEB (printk ("\npci2220i: FAILURE 19"));
		if ( InitFailover (padapter, pdev) )
			break;
		return 0;
		}

	StartTimer (padapter);
	if ( pdev->raid && (padapter->cmd == WRITE_CMD) )
		{
		rc = IdeCmdBoth (padapter, pdev);
		if ( !rc )
			rc = WriteDataBoth (padapter, pdev);
		if ( rc )
			{
			del_timer (&padapter->timer);
			padapter->expectingIRQ = 0;
			padapter->survivor = rc >> 1;
				DEB (printk ("\npci2220i: FAILURE 20"));
			if ( InitFailover (padapter, pdev) )
				{
				OpDone (padapter, DID_ERROR << 16);
				return 0;
				}
			}
		}
	else
		{
		rc = IdeCmd (padapter, pdev);
		if ( (padapter->cmd == WRITE_CMD) && !rc )
			rc = WriteData (padapter);
		if ( rc )
			{
			del_timer (&padapter->timer);
			padapter->expectingIRQ = 0;
			if ( pdev->raid )
				{
				padapter->survivor = (pdev->spigot ^ 3) >> 1;
				DEB (printk ("\npci2220i: FAILURE 21"));
				if ( !InitFailover (padapter, pdev) )
					return 0;
				}
			OpDone (padapter, DID_ERROR << 16);
			return 0;
			}
		}
	return 0;
	}
/****************************************************************
 *	Name:			ReadFlash
 *
 *	Description:	Read information from controller Flash memory.
 *
 *	Parameters:		padapter - Pointer to host interface data structure.
 *					pdata	 - Pointer to data structures.
 *					base	 - base address in Flash.
 *					length	 - lenght of data space in bytes.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
static VOID ReadFlash (PADAPTER2220I padapter, VOID *pdata, ULONG base, ULONG length)
	{
	ULONG	 oldremap;
	UCHAR	 olddesc;
	ULONG	 z;
	UCHAR	*pd = (UCHAR *)pdata;

	oldremap = inl (padapter->regRemap);							// save values to restore later
	olddesc  = inb_p (padapter->regDesc);

	outl (base | 1, padapter->regRemap);							// remap to Flash space as specified
	outb_p (0x40, padapter->regDesc);								// describe remap region as 8 bit
	for ( z = 0;  z < length;  z++)									// get "length" data count
		*pd++ = inb_p (padapter->regBase + z);						// read in the data

	outl (oldremap, padapter->regRemap);							// restore remap register values
	outb_p (olddesc, padapter->regDesc);
	}
/****************************************************************
 *	Name:			GetRegs
 *
 *	Description:	Initialize the regester information.
 *
 *	Parameters:		pshost		  - Pointer to SCSI host data structure.
 *					bigd		  - PCI-2240I identifier
 *					pcidev		  - Pointer to device data structure.
 *
 *	Returns:		TRUE if failure to install.
 *
 ****************************************************************/
static USHORT GetRegs (struct Scsi_Host *pshost, BOOL bigd, struct pci_dev *pcidev)
	{
	PADAPTER2220I	padapter;
	int				setirq;
	int				z;
	USHORT			zr, zl;
	UCHAR		   *consistent;
	dma_addr_t		consistentDma;

	padapter = HOSTDATA(pshost);
	memset (padapter, 0, sizeof (ADAPTER2220I));
	memset (&DaleSetup, 0, sizeof (DaleSetup));
	memset (DiskMirror, 0, sizeof (DiskMirror));

	zr = pci_resource_start (pcidev, 1);
	zl = pci_resource_start (pcidev, 2);

	padapter->basePort = zr;
	padapter->regRemap		= zr + RTR_LOCAL_REMAP;					// 32 bit local space remap
	padapter->regDesc		= zr + RTR_REGIONS;	  					// 32 bit local region descriptor
	padapter->regRange		= zr + RTR_LOCAL_RANGE;					// 32 bit local range
	padapter->regIrqControl	= zr + RTR_INT_CONTROL_STATUS;			// 16 bit interrupt control and status
	padapter->regScratchPad	= zr + RTR_MAILBOX;	  					// 16 byte scratchpad I/O base address

	padapter->regBase		= zl;
	padapter->regData		= zl + REG_DATA;						// data register I/O address
	padapter->regError		= zl + REG_ERROR;						// error register I/O address
	padapter->regSectCount	= zl + REG_SECTOR_COUNT;				// sector count register I/O address
	padapter->regLba0		= zl + REG_LBA_0;						// least significant byte of LBA
	padapter->regLba8		= zl + REG_LBA_8;						// next least significant byte of LBA
	padapter->regLba16		= zl + REG_LBA_16;						// next most significan byte of LBA
	padapter->regLba24		= zl + REG_LBA_24;						// head and most 4 significant bits of LBA
	padapter->regStatCmd	= zl + REG_STAT_CMD;					// status on read and command on write register
	padapter->regStatSel	= zl + REG_STAT_SEL;					// board status on read and spigot select on write register
	padapter->regFail		= zl + REG_FAIL;
	padapter->regAltStat	= zl + REG_ALT_STAT;
	padapter->pcidev		= pcidev;

	if ( bigd )
		{
		padapter->regDmaDesc	= zr + RTR_DMA0_DESC_PTR;			// address of the DMA discriptor register for direction of transfer
		padapter->regDmaCmdStat	= zr + RTR_DMA_COMMAND_STATUS;		// Byte #0 of DMA command status register
		padapter->regDmaAddrPci	= zr + RTR_DMA0_PCI_ADDR;			// 32 bit register for PCI address of DMA
		padapter->regDmaAddrLoc	= zr + RTR_DMA0_LOCAL_ADDR;			// 32 bit register for local bus address of DMA
		padapter->regDmaCount	= zr + RTR_DMA0_COUNT;				// 32 bit register for DMA transfer count
		padapter->regDmaMode	= zr + RTR_DMA0_MODE + 1;			// 32 bit register for DMA mode control
		padapter->bigD			= SEL_NEW_SPEED_1;					// set spigot speed control bit
		}
	else
		{
		padapter->regDmaDesc	= zl + RTL_DMA1_DESC_PTR;			// address of the DMA discriptor register for direction of transfer
		padapter->regDmaCmdStat	= zl + RTL_DMA_COMMAND_STATUS + 1;	// Byte #1 of DMA command status register
		padapter->regDmaAddrPci	= zl + RTL_DMA1_PCI_ADDR;			// 32 bit register for PCI address of DMA
		padapter->regDmaAddrLoc	= zl + RTL_DMA1_LOCAL_ADDR;			// 32 bit register for local bus address of DMA
		padapter->regDmaCount	= zl + RTL_DMA1_COUNT;				// 32 bit register for DMA transfer count
		padapter->regDmaMode	= zl + RTL_DMA1_MODE + 1;			// 32 bit register for DMA mode control
		}

	padapter->numberOfDrives = inb_p (padapter->regScratchPad + BIGD_NUM_DRIVES);
	if ( !bigd && !padapter->numberOfDrives )						// if no devices on this board
		return TRUE;

	pshost->irq = pcidev->irq;
	setirq = 1;
	for ( z = 0;  z < Installed;  z++ )								// scan for shared interrupts
		{
		if ( PsiHost[z]->irq == pshost->irq )						// if shared then, don't posses
			setirq = 0;
		}
	if ( setirq )													// if not shared, posses
		{
		if ( request_irq (pshost->irq, Irq_Handler, SA_SHIRQ, "pci2220i", padapter) < 0 )
			{
			if ( request_irq (pshost->irq, Irq_Handler, SA_INTERRUPT | SA_SHIRQ, "pci2220i", padapter) < 0 )
				{
				printk ("Unable to allocate IRQ for PCI-2220I controller.\n");
				return TRUE;
				}
			}
		padapter->irqOwned = pshost->irq;							// set IRQ as owned
		}

	if ( padapter->numberOfDrives )
		consistent = pci_alloc_consistent (pcidev, SECTORSXFER * BYTES_PER_SECTOR, &consistentDma);
	else
		consistent = pci_alloc_consistent (pcidev, ATAPI_TRANSFER, &consistentDma);
	if ( !consistent )
		{
		printk ("Unable to allocate DMA buffer for PCI-2220I controller.\n");
		free_irq (pshost->irq, padapter);
		return TRUE;
		}
	padapter->kBuffer = consistent;
	padapter->kBufferDma = consistentDma;

	PsiHost[Installed]	= pshost;									// save SCSI_HOST pointer
	pshost->io_port		= padapter->basePort;
	pshost->n_io_port	= 0xFF;
	pshost->unique_id	= padapter->regBase;

	outb_p (0x01, padapter->regRange);								// fix our range register because other drivers want to tromp on it

	padapter->timingMode = inb_p (padapter->regScratchPad + DALE_TIMING_MODE);
	if ( padapter->timingMode >= 2 )
		{
		if ( bigd )
			padapter->timingAddress	= ModeArray2[padapter->timingMode - 2];
		else
			padapter->timingAddress	= ModeArray[padapter->timingMode - 2];
		}
	else
		padapter->timingPIO = TRUE;

	ReadFlash (padapter, &DaleSetup, DALE_FLASH_SETUP, sizeof (SETUP));
	ReadFlash (padapter, &DiskMirror, DALE_FLASH_RAID, sizeof (DiskMirror));

	return FALSE;
	}
/****************************************************************
 *	Name:			SetupFinish
 *
 *	Description:	Complete the driver initialization process for a card
 *
 *	Parameters:		padapter  - Pointer to SCSI host data structure.
 *					str		  - Pointer to board type string.
 *
 *	Returns:		Nothing.
 *
 ****************************************************************/
VOID SetupFinish (PADAPTER2220I padapter, char *str, int irq)
	{
	init_timer (&padapter->timer);
	padapter->timer.function = TimerExpiry;
	padapter->timer.data = (unsigned long)padapter;
	init_timer (&padapter->reconTimer);
	padapter->reconTimer.function = ReconTimerExpiry;
	padapter->reconTimer.data = (unsigned long)padapter;
	printk("\nPCI-%sI EIDE CONTROLLER: at I/O = %lX/%lX  IRQ = %d\n", str, padapter->basePort, padapter->regBase, irq);
	printk("Version %s, Compiled %s %s\n\n", PCI2220I_VERSION, __DATE__, __TIME__);
	}	
/****************************************************************
 *	Name:	Pci2220i_Detect
 *
 *	Description:	Detect and initialize our boards.
 *
 *	Parameters:		tpnt - Pointer to SCSI host template structure.
 *
 *	Returns:		Number of adapters installed.
 *
 ****************************************************************/
int Pci2220i_Detect (Scsi_Host_Template *tpnt)
	{
	struct Scsi_Host   *pshost;
	PADAPTER2220I	    padapter;
	POUR_DEVICE			pdev;
	int					unit;
	int					z;
	USHORT				raidon;
	UCHAR				spigot1, spigot2;
	UCHAR				device;
	struct pci_dev	   *pcidev = NULL;

	while ( (pcidev = pci_find_device (VENDOR_PSI, DEVICE_DALE_1, pcidev)) != NULL )
		{
		if (pci_enable_device(pcidev))
			continue;
		pshost = scsi_register (tpnt, sizeof(ADAPTER2220I));
		if(pshost==NULL)
			continue;
			
		padapter = HOSTDATA(pshost);

		if ( GetRegs (pshost, FALSE, pcidev) )
			goto unregister;

		scsi_set_device(pshost, &pcidev->dev);
		pshost->max_id = padapter->numberOfDrives;
		for ( z = 0;  z < padapter->numberOfDrives;  z++ )
			{
			unit = inb_p (padapter->regScratchPad + DALE_CHANNEL_DEVICE_0 + z) & 0x0F;
			pdev = &padapter->device[z];
			pdev->byte6		= (UCHAR)(((unit & 1) << 4) | 0xE0);
			pdev->spigot	= (UCHAR)(1 << (unit >> 1));
			pdev->sectors	= DaleSetup.setupDevice[unit].sectors;
			pdev->heads		= DaleSetup.setupDevice[unit].heads;
			pdev->cylinders = DaleSetup.setupDevice[unit].cylinders;
			pdev->blocks	= DaleSetup.setupDevice[unit].blocks;

			if ( !z )
				{
				DiskMirror[0].status = inb_p (padapter->regScratchPad + DALE_RAID_0_STATUS);		
				DiskMirror[1].status = inb_p (padapter->regScratchPad + DALE_RAID_1_STATUS);		
				if ( (DiskMirror[0].signature == SIGNATURE) && (DiskMirror[1].signature == SIGNATURE) &&
				     (DiskMirror[0].pairIdentifier == (DiskMirror[1].pairIdentifier ^ 1)) )
					{			 
					raidon = TRUE;
					if ( unit > (unit ^ 2) )
						unit = unit ^ 2;
					}	
				else
					raidon = FALSE;

				memcpy (pdev->DiskMirror, DiskMirror, sizeof (DiskMirror));
				padapter->raidData[0] = &pdev->DiskMirror[0];
				padapter->raidData[2] = &pdev->DiskMirror[1];
				
				spigot1 = spigot2 = FALSE;
				pdev->spigots[0] = 1;
				pdev->spigots[1] = 2;
				pdev->lastsectorlba[0] = InlineIdentify (padapter, 1, 0);
				pdev->lastsectorlba[1] = InlineIdentify (padapter, 2, 0);
						
				if ( !(pdev->DiskMirror[1].status & UCBF_SURVIVOR) && pdev->lastsectorlba[0] )
					spigot1 = TRUE;
				if ( !(pdev->DiskMirror[0].status & UCBF_SURVIVOR) && pdev->lastsectorlba[1] )
					spigot2 = TRUE;
				if ( pdev->DiskMirror[0].status & DiskMirror[1].status & UCBF_SURVIVOR )
					spigot1 = TRUE;

				if ( spigot1 && (pdev->DiskMirror[0].status & UCBF_REBUILD) )
					InlineReadSignature (padapter, pdev, 0);
				if ( spigot2 && (pdev->DiskMirror[1].status & UCBF_REBUILD) )
					InlineReadSignature (padapter, pdev, 1);

				if ( spigot1 && spigot2 && raidon )
					{
					pdev->raid = 1;
					if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
						pdev->spigot = 2;
					else
						pdev->spigot = 1;
					if ( (pdev->DiskMirror[0].status & UCBF_REBUILD) || (pdev->DiskMirror[1].status & UCBF_REBUILD) )
						padapter->reconOn = pdev->reconOn = pdev->reconIsStarting = TRUE;
					}
				else
					{
					if ( spigot1 )
						{
						if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
							goto unregister;
						pdev->DiskMirror[0].status = UCBF_MIRRORED | UCBF_SURVIVOR;
						pdev->spigot = 1;
						}
					else
						{
						if ( pdev->DiskMirror[1].status & UCBF_REBUILD )
							goto unregister;
						pdev->DiskMirror[1].status = UCBF_MIRRORED | UCBF_SURVIVOR;
						pdev->spigot = 2;
						}
					if ( DaleSetup.rebootRebuild && raidon )
						padapter->reconOn = pdev->reconOn = pdev->reconIsStarting = TRUE;
					}
			
				if ( raidon )
					break;
				}
			}

		SetupFinish (padapter, "2220", pshost->irq);
	
		if ( ++Installed < MAXADAPTER )
			continue;
		break;
unregister:;
		scsi_unregister (pshost);
		}

	while ( (pcidev = pci_find_device (VENDOR_PSI, DEVICE_BIGD_1, pcidev)) != NULL )
		{
		pshost = scsi_register (tpnt, sizeof(ADAPTER2220I));
		padapter = HOSTDATA(pshost);

		if ( GetRegs (pshost, TRUE, pcidev) )
			goto unregister1;

		for ( z = 0;  z < BIGD_MAXDRIVES;  z++ )
			DiskMirror[z].status = inb_p (padapter->regScratchPad + BIGD_RAID_0_STATUS + z);		

		scsi_set_pci_device(pshost, pcidev);
		pshost->max_id = padapter->numberOfDrives;
		padapter->failRegister = inb_p (padapter->regScratchPad + BIGD_ALARM_IMAGE);
		for ( z = 0;  z < padapter->numberOfDrives;  z++ )
			{
			unit = inb_p (padapter->regScratchPad + BIGD_DEVICE_0 + z);
			pdev = &padapter->device[z];
			pdev->byte6		= (UCHAR)(((unit & 1) << 4) | 0xE0);
			pdev->spigot	= (UCHAR)(1 << (unit >> 1));
			pdev->sectors	= DaleSetup.setupDevice[unit].sectors;
			pdev->heads		= DaleSetup.setupDevice[unit].heads;
			pdev->cylinders = DaleSetup.setupDevice[unit].cylinders;
			pdev->blocks	= DaleSetup.setupDevice[unit].blocks;
			
			if ( (DiskMirror[unit].signature == SIGNATURE) && (DiskMirror[unit ^ 2].signature == SIGNATURE) &&
			     (DiskMirror[unit].pairIdentifier == (DiskMirror[unit ^ 2].pairIdentifier ^ 1)) )
				{			 
				raidon = TRUE;
				if ( unit > (unit ^ 2) )
					unit = unit ^ 2;
				}	
			else
				raidon = FALSE;
				
			spigot1 = spigot2 = FALSE;
			memcpy (&pdev->DiskMirror[0], &DiskMirror[unit], sizeof (DISK_MIRROR));
			memcpy (&pdev->DiskMirror[1], &DiskMirror[unit ^ 2], sizeof (DISK_MIRROR));
			padapter->raidData[unit]	 = &pdev->DiskMirror[0];
			padapter->raidData[unit ^ 2] = &pdev->DiskMirror[1];
			pdev->spigots[0] = 1 << (unit >> 1);
			pdev->spigots[1] = 1 << ((unit ^ 2) >> 1);
			pdev->deviceID[0] = unit;
			pdev->deviceID[1] = unit ^ 2;
			pdev->lastsectorlba[0] = InlineIdentify (padapter, pdev->spigots[0], unit & 1);
			pdev->lastsectorlba[1] = InlineIdentify (padapter, pdev->spigots[1], unit & 1);

			if ( !(pdev->DiskMirror[1].status & UCBF_SURVIVOR) && pdev->lastsectorlba[0] )
				spigot1 = TRUE;
			if ( !(pdev->DiskMirror[0].status & UCBF_SURVIVOR) && pdev->lastsectorlba[1] )
				spigot2 = TRUE;
			if ( pdev->DiskMirror[0].status & pdev->DiskMirror[1].status & UCBF_SURVIVOR )
				spigot1 = TRUE;

			if ( spigot1 && (pdev->DiskMirror[0].status & UCBF_REBUILD) )
				InlineReadSignature (padapter, pdev, 0);
			if ( spigot2 && (pdev->DiskMirror[1].status & UCBF_REBUILD) )
				InlineReadSignature (padapter, pdev, 1);

			if ( spigot1 && spigot2 && raidon )
				{
				pdev->raid = 1;
				if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
					pdev->spigot = pdev->spigots[1];
				else
					pdev->spigot = pdev->spigots[0];
				if ( (pdev->DiskMirror[0].status & UCBF_REBUILD) || (pdev->DiskMirror[1].status & UCBF_REBUILD) )
					padapter->reconOn = pdev->reconOn = pdev->reconIsStarting = TRUE;
				}
			else
				{
				if ( spigot1 )
					{
					if ( pdev->DiskMirror[0].status & UCBF_REBUILD )
						goto unregister1;
					pdev->DiskMirror[0].status = UCBF_MIRRORED | UCBF_SURVIVOR;
					pdev->spigot = pdev->spigots[0];
					}
				else
					{
					if ( pdev->DiskMirror[1].status & UCBF_REBUILD )
						goto unregister;
					pdev->DiskMirror[1].status = UCBF_MIRRORED | UCBF_SURVIVOR;
					pdev->spigot = pdev->spigots[1];
					}
				if ( DaleSetup.rebootRebuild && raidon )
					padapter->reconOn = pdev->reconOn = pdev->reconIsStarting = TRUE;
				}
			}
		
		if ( !padapter->numberOfDrives )									// If no ATA devices then scan ATAPI
			{
			unit = 0;
			for ( spigot1 = 0;  spigot1 < 4;  spigot1++ )
				{
				for ( device = 0;  device < 2;  device++ )
					{
					DEB (printk ("\nPCI2242I: scanning for ID %d ", (spigot1 * 2) + device));
					pdev = &(padapter->device[(spigot1 * 2) + device]);
					pdev->byte6 = 0x0A | (device << 4);
					pdev->spigot = 1 << spigot1;
					if ( !AtapiReset (padapter, pdev) )
						{
						DEB (printk (" Device found "));
						if ( !AtapiIdentify (padapter, pdev) )
							{
							DEB (printk (" Device verified"));
							unit++;
							continue;
							}
						}
					pdev->spigot = pdev->byte6 = 0;
					}
				}

			if ( unit )
				{
				padapter->atapi = TRUE;
				padapter->timingAddress = DALE_DATA_MODE3;
				outw_p (0x0900, padapter->regIrqControl);					// Turn our interrupts on
				outw_p (0x0C41, padapter->regDmaMode - 1);					// setup for 16 bits, ready enabled, done IRQ enabled, no incriment
				outb_p (0xFF, padapter->regFail);							// all fail lights and alarm off
				pshost->max_id = 8;
				}
			}
		SetupFinish (padapter, "2240", pshost->irq);
		
		if ( ++Installed < MAXADAPTER )
			continue;
		break;
unregister1:;
		scsi_unregister (pshost);
		}

	NumAdapters = Installed;
	return Installed;
	}
/****************************************************************
 *	Name:	Pci2220i_Abort
 *
 *	Description:	Process the Abort command from the SCSI manager.
 *
 *	Parameters:		SCpnt - Pointer to SCSI command structure.
 *
 *	Returns:		Allways snooze.
 *
 ****************************************************************/
int Pci2220i_Abort (Scsi_Cmnd *SCpnt)
	{
	PADAPTER2220I	padapter = HOSTDATA(SCpnt->device->host);			// Pointer to adapter control structure
	POUR_DEVICE		pdev	 = &padapter->device[SCpnt->device->id];// Pointer to device information

	if ( !padapter->SCpnt )
		return SCSI_ABORT_NOT_RUNNING;
	
	if ( padapter->atapi )
		{
		if ( AtapiReset (padapter, pdev) )
			return SCSI_ABORT_ERROR;
		OpDone (padapter, DID_ABORT << 16);
		return SCSI_ABORT_SUCCESS;
		}
	return SCSI_ABORT_SNOOZE;
	}
/****************************************************************
 *	Name:	Pci2220i_Reset
 *
 *	Description:	Process the Reset command from the SCSI manager.
 *
 *	Parameters:		SCpnt - Pointer to SCSI command structure.
 *					flags - Flags about the reset command
 *
 *	Returns:		No active command at this time, so this means
 *					that each time we got some kind of response the
 *					last time through.  Tell the mid-level code to
 *					request sense information in order to decide what
 *					to do next.
 *
 ****************************************************************/
int Pci2220i_Reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
	{
	PADAPTER2220I	padapter = HOSTDATA(SCpnt->device->host);			// Pointer to adapter control structure
	POUR_DEVICE		pdev	 = &padapter->device[SCpnt->device->id];// Pointer to device information

	if ( padapter->atapi )
		{
		if ( AtapiReset (padapter, pdev) )
			return SCSI_RESET_ERROR;
		return SCSI_RESET_SUCCESS;
		}
	return SCSI_RESET_PUNT;
	}
/****************************************************************
 *	Name:	Pci2220i_Release
 *
 *	Description:	Release resources allocated for a single each adapter.
 *
 *	Parameters:		pshost - Pointer to SCSI command structure.
 *
 *	Returns:		zero.
 *
 ****************************************************************/
int Pci2220i_Release (struct Scsi_Host *pshost)
	{
    PADAPTER2220I	padapter = HOSTDATA (pshost);
	USHORT			z;

	if ( padapter->reconOn )
		{
		padapter->reconOn = FALSE;						// shut down the hot reconstruct
		if ( padapter->reconPhase )
			mdelay (300);
		if ( padapter->reconTimer.data )				// is the timer running?
			{
			del_timer (&padapter->reconTimer);
			padapter->reconTimer.data = 0;
			}
		}

	// save RAID status on the board
	if ( padapter->bigD )
		{
		outb_p (padapter->failRegister, padapter->regScratchPad + BIGD_ALARM_IMAGE);
		for ( z = 0;  z < BIGD_MAXDRIVES;  z++ )
			{
			if ( padapter->raidData )
				outb_p (padapter->raidData[z]->status, padapter->regScratchPad + BIGD_RAID_0_STATUS + z);	
			else
				outb_p (0, padapter->regScratchPad + BIGD_RAID_0_STATUS);	
			}
		}
	else
		{
		outb_p (padapter->device[0].DiskMirror[0].status, padapter->regScratchPad + DALE_RAID_0_STATUS);		
		outb_p (padapter->device[0].DiskMirror[1].status, padapter->regScratchPad + DALE_RAID_1_STATUS);		
		}

	if ( padapter->irqOwned )
		free_irq (pshost->irq, padapter);
    release_region (pshost->io_port, pshost->n_io_port);
	if ( padapter->numberOfDrives )
		pci_free_consistent (padapter->pcidev, SECTORSXFER * BYTES_PER_SECTOR, padapter->kBuffer, padapter->kBufferDma);
	else	
		pci_free_consistent (padapter->pcidev, ATAPI_TRANSFER, padapter->kBuffer, padapter->kBufferDma);
    scsi_unregister(pshost);
    return 0;
	}

/****************************************************************
 *	Name:	Pci2220i_BiosParam
 *
 *	Description:	Process the biosparam request from the SCSI manager to
 *					return C/H/S data.
 *
 *	Parameters:		disk - Pointer to SCSI disk structure.
 *					dev	 - Major/minor number from kernel.
 *					geom - Pointer to integer array to place geometry data.
 *
 *	Returns:		zero.
 *
 ****************************************************************/
int Pci2220i_BiosParam (struct scsi_device *sdev, struct block_device *dev,
		sector_t capacity, int geom[])
	{
	POUR_DEVICE	pdev;

	if ( !(HOSTDATA(sdev->host))->atapi )
		{
		pdev = &(HOSTDATA(sdev->host)->device[sdev->id]);

		geom[0] = pdev->heads;
		geom[1] = pdev->sectors;
		geom[2] = pdev->cylinders;
		}
	return 0;
	}

MODULE_LICENSE("Dual BSD/GPL");

static Scsi_Host_Template driver_template = {
	.proc_name		= "pci2220i",
	.name			= "PCI-2220I/PCI-2240I",
	.detect			= Pci2220i_Detect,
	.release		= Pci2220i_Release,
	.queuecommand		= Pci2220i_QueueCommand,
	.abort			= Pci2220i_Abort,
	.reset			= Pci2220i_Reset,
	.bios_param		= Pci2220i_BiosParam,
	.can_queue		= 1,
	.this_id		= -1,
	.sg_tablesize		= SG_ALL,
	.cmd_per_lun		= 1,
	.use_clustering		= DISABLE_CLUSTERING,
};
#include "scsi_module.c"