summaryrefslogtreecommitdiffstats
path: root/drivers/scsi/aha152x.c
blob: 7ae1ee4a0b95fcb3129a32c7c11a921b17853987 (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
/* aha152x.c -- Adaptec AHA-152x driver
 * Author: Juergen E. Fischer, fischer@server.et-inf.fho-emden.de
 * Copyright 1993, 1994 Juergen E. Fischer
 *
 *
 * This driver is based on
 *   fdomain.c -- Future Domain TMC-16x0 driver
 * which is
 *   Copyright 1992, 1993 Rickard E. Faith (faith@cs.unc.edu)
 *

 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.

 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 
 *
 * $Id: aha152x.c,v 1.6 1994/11/24 20:35:27 root Exp $
 *

 * $Log: aha152x.c,v $
 * Revision 1.6  1994/11/24  20:35:27  root
 * - problem with odd number of bytes in fifo fixed
 *
 * Revision 1.5  1994/10/30  14:39:56  root
 * - abort code fixed
 * - debugging improved
 *
 * Revision 1.4  1994/09/12  11:33:01  root
 * - irqaction to request_irq
 * - abortion updated
 *
 * Revision 1.3  1994/08/04  13:53:05  root
 * - updates for mid-level-driver changes
 * - accept unexpected BUSFREE phase as error condition
 * - parity check now configurable
 *
 * Revision 1.2  1994/07/03  12:56:36  root
 * - cleaned up debugging code
 * - more tweaking on reset delays
 * - updated abort/reset code (pretty untested...)
 *
 * Revision 1.1  1994/05/28  21:18:49  root
 * - update for mid-level interface change (abort-reset)
 * - delays after resets adjusted for some slow devices
 *
 * Revision 1.0  1994/03/25  12:52:00  root
 * - Fixed "more data than expected" problem
 * - added new BIOS signatures
 *
 * Revision 0.102  1994/01/31  20:44:12  root
 * - minor changes in insw/outsw handling
 *
 * Revision 0.101  1993/12/13  01:16:27  root
 * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
 *   fixes problems with CD-ROM sector size detection & media change)
 *
 * Revision 0.100  1993/12/10  16:58:47  root
 * - fix for unsuccessful selections in case of non-continuous id assignments
 *   on the scsi bus.
 *
 * Revision 0.99  1993/10/24  16:19:59  root
 * - fixed DATA IN (rare read errors gone)
 *
 * Revision 0.98  1993/10/17  12:54:44  root
 * - fixed some recent fixes (shame on me)
 * - moved initialization of scratch area to aha152x_queue
 *
 * Revision 0.97  1993/10/09  18:53:53  root
 * - DATA IN fixed. Rarely left data in the fifo.
 *
 * Revision 0.96  1993/10/03  00:53:59  root
 * - minor changes on DATA IN
 *
 * Revision 0.95  1993/09/24  10:36:01  root
 * - change handling of MSGI after reselection
 * - fixed sti/cli
 * - minor changes
 *
 * Revision 0.94  1993/09/18  14:08:22  root
 * - fixed bug in multiple outstanding command code
 * - changed detection
 * - support for kernel command line configuration
 * - reset corrected
 * - changed message handling
 *
 * Revision 0.93  1993/09/15  20:41:19  root
 * - fixed bugs with multiple outstanding commands
 *
 * Revision 0.92  1993/09/13  02:46:33  root
 * - multiple outstanding commands work (no problems with IBM drive)
 *
 * Revision 0.91  1993/09/12  20:51:46  root
 * added multiple outstanding commands
 * (some problem with this $%&? IBM device remain)
 *
 * Revision 0.9  1993/09/12  11:11:22  root
 * - corrected auto-configuration
 * - changed the auto-configuration (added some '#define's)
 * - added support for dis-/reconnection
 *
 * Revision 0.8  1993/09/06  23:09:39  root
 * - added support for the drive activity light
 * - minor changes
 *
 * Revision 0.7  1993/09/05  14:30:15  root
 * - improved phase detection
 * - now using the new snarf_region code of 0.99pl13
 *
 * Revision 0.6  1993/09/02  11:01:38  root
 * first public release; added some signatures and biosparam()
 *
 * Revision 0.5  1993/08/30  10:23:30  root
 * fixed timing problems with my IBM drive
 *
 * Revision 0.4  1993/08/29  14:06:52  root
 * fixed some problems with timeouts due incomplete commands
 *
 * Revision 0.3  1993/08/28  15:55:03  root
 * writing data works too.  mounted and worked on a dos partition
 *
 * Revision 0.2  1993/08/27  22:42:07  root
 * reading data works.  Mounted a msdos partition.
 *
 * Revision 0.1  1993/08/25  13:38:30  root
 * first "damn thing doesn't work" version
 *
 * Revision 0.0  1993/08/14  19:54:25  root
 * empty function bodies; detect() works.
 *

 **************************************************************************


 
 DESCRIPTION:

 This is the Linux low-level SCSI driver for Adaptec AHA-1520/1522
 SCSI host adapters.


 PER-DEFINE CONFIGURABLE OPTIONS:

 AUTOCONF       : use configuration the controller reports (only 152x)
 IRQ            : override interrupt channel (9,10,11 or 12) (default 11)
 SCSI_ID        : override scsiid of AIC-6260 (0-7) (default 7)
 RECONNECT      : override target dis-/reconnection/multiple outstanding commands (default on)
 PARITY		: override parity check (default on)
 SKIP_BIOSTEST  : Don't test for BIOS signature (AHA-1510 or disabled BIOS)
 PORTBASE       : Force port base. Don't try to probe


 LILO COMMAND LINE OPTIONS:

 aha152x=<PORTBASE>[,<IRQ>[,<SCSI-ID>[,<RECONNECT>[,<PARITY>]]]]

 The normal configuration can be overridden by specifying a command line.
 When you do this, the BIOS test is skipped. Entered values have to be
 valid (known). Don't use values that aren't support under normal operation.
 If you think that you need other values: contact me.


 REFERENCES USED:

 "AIC-6260 SCSI Chip Specification", Adaptec Corporation.

 "SCSI COMPUTER SYSTEM INTERFACE - 2 (SCSI-2)", X3T9.2/86-109 rev. 10h

 "Writing a SCSI device driver for Linux", Rik Faith (faith@cs.unc.edu)

 "Kernel Hacker's Guide", Michael K. Johnson (johnsonm@sunsite.unc.edu)

 "Adaptec 1520/1522 User's Guide", Adaptec Corporation.
 
 Michael K. Johnson (johnsonm@sunsite.unc.edu)

 Drew Eckhardt (drew@cs.colorado.edu)

 Eric Youngdale (ericy@cais.com) 

 special thanks to Eric Youngdale for the free(!) supplying the
 documentation on the chip.

 **************************************************************************/

#include <linux/sched.h>
#include <asm/io.h>
#include "../block/blk.h"
#include "scsi.h"
#include "sd.h"
#include "hosts.h"
#include "constants.h"
#include <asm/system.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/wait.h>
#include <linux/ioport.h>

#include "aha152x.h"

/* DEFINES */


/* If auto configuration is disabled, IRQ, SCSI_ID and RECONNECT have to
   be predefined */
#if !defined(AUTOCONF)
#if !defined(IRQ)
#error undefined IRQ; define AUTOCONF or IRQ
#endif
#if !defined(SCSI_ID)
#error undefined SCSI_ID; define AUTOCONF or SCSI_ID
#endif
#if !defined(RECONNECT)
#error undefined RECONNECT; define AUTOCONF or RECONNECT
#endif
#if !defined(PARITY)
#error undefined PARITY; define AUTOCONF or PARITY
#endif
#endif

/* I use this when I'm looking for weird bugs */
#define DEBUG_TIMING 

#if defined(DEBUG_AHA152X)

#undef  SKIP_PORTS              /* don't display ports */

#undef  DEBUG_QUEUE             /* debug queue() */
#undef  DEBUG_RESET             /* debug reset() */
#undef  DEBUG_INTR              /* debug intr() */
#undef  DEBUG_SELECTION         /* debug selection part in intr() */
#undef  DEBUG_MSGO              /* debug message out phase in intr() */
#undef  DEBUG_MSGI              /* debug message in phase in intr() */
#undef  DEBUG_STATUS            /* debug status phase in intr() */
#undef  DEBUG_CMD               /* debug command phase in intr() */
#undef  DEBUG_DATAI             /* debug data in phase in intr() */
#undef  DEBUG_DATAO             /* debug data out phase in intr() */
#undef  DEBUG_ABORT             /* debug abort() */
#undef  DEBUG_DONE              /* debug done() */
#undef  DEBUG_BIOSPARAM         /* debug biosparam() */

#undef  DEBUG_RACE              /* debug race conditions */
#undef  DEBUG_PHASES            /* debug phases (useful to trace) */
#undef  DEBUG_QUEUES            /* debug reselection */

/* recently used for debugging */
#if 0
#endif

#define DEBUG_QUEUE
#define DEBUG_PHASES

#endif

#define DEBUG_RESET             /* resets should be rare */
#define DEBUG_ABORT             /* aborts too */

#define DEBUG_DEFAULT (debug_reset|debug_abort)

/* END OF DEFINES */

/* some additional "phases" for getphase() */
#define P_BUSFREE  1
#define P_PARITY   2

static int port_base      = 0;
static int this_host      = 0;
static int can_disconnect = 0;
static int can_doparity   = 0;
static int commands       = 0;

#ifdef DEBUG_AHA152X
unsigned int aha152x_debug  = DEBUG_DEFAULT;
#endif

/* set by aha152x_setup according to the command line */
static int setup_called    = 0;
static int setup_portbase  = 0;
static int setup_irq       = 0;
static int setup_scsiid    = 0;
static int setup_reconnect = 0;
static int setup_doparity  = 0;

#ifdef DEBUG_AHA152X
static int setup_debug     = 0;
#endif

static char *setup_str = (char *)NULL;

enum {
   not_issued   = 0x01,
   in_selection = 0x02,
   disconnected = 0x04,
   aborted      = 0x08,
   sent_ident   = 0x10,
   in_other     = 0x20,
};
 
/*
 * Command queues:
 * issue_SC        : commands that are queued to be issued 
 * current_SC      : command that's currently using the bus
 * disconnected_SC : commands that that have been disconnected 
 */
static Scsi_Cmnd            *issue_SC        = NULL;
static Scsi_Cmnd            *current_SC      = NULL;
static Scsi_Cmnd            *disconnected_SC = NULL;

static int aborting=0, abortion_complete=0, abort_result;

void aha152x_intr( int irqno );
void aha152x_done( int error );
void aha152x_setup( char *str, int *ints );

static void aha152x_reset_ports(void);
static void aha152x_panic(char *msg);

static void disp_ports(void);
static void show_command(Scsi_Cmnd *ptr);
static void show_queues(void);
static void disp_enintr(void);

#if defined(DEBUG_RACE)
static void enter_driver(const char *);
static void leave_driver(const char *);
#endif

/* possible locations for the Adaptec BIOS */
static void *addresses[] =
{
  (void *) 0xdc000,   /* default first */
  (void *) 0xc8000,
  (void *) 0xcc000,
  (void *) 0xd0000,
  (void *) 0xd4000,
  (void *) 0xd8000,
  (void *) 0xe0000,
  (void *) 0xf0000,
  (void *) 0xeb800,   /* VTech Platinum SMP */
};
#define ADDRESS_COUNT (sizeof( addresses ) / sizeof( void * ))

/* possible i/o addresses for the AIC-6260 */
static unsigned short ports[] =
{
  0x340,      /* default first */
  0x140
};
#define PORT_COUNT (sizeof( ports ) / sizeof( unsigned short ))

/* possible interrupt channels */
static unsigned short ints[] = { 9, 10, 11, 12 };

/* signatures for various AIC-6[23]60 based controllers.
   The point in detecting signatures is to avoid useless
   and maybe harmful probes on ports. I'm not sure that
   all listed boards pass auto-configuration. For those
   which fail the BIOS signature is obsolete, because
   user intervention to supply the configuration is 
   needed anyway. */
static struct signature {
  char *signature;
  int  sig_offset;
  int  sig_length;
} signatures[] =
{
  { "Adaptec AHA-1520 BIOS",      0x102e, 21 },  /* Adaptec 152x */
  { "Adaptec ASW-B626 BIOS",      0x1029, 21 },  /* on-board controller */
  { "Adaptec BIOS: ASW-B626",       0x0f, 22 },  /* on-board controller */
  { "Adaptec ASW-B626 S2",        0x2e6c, 19 },  /* on-board controller */
  { "Adaptec BIOS:AIC-6360",         0xc, 21 },  /* on-board controller */
  { "ScsiPro SP-360 BIOS",        0x2873, 19 },  /* ScsiPro-Controller with AIC-6360 */
  { "GA-400 LOCAL BUS SCSI BIOS", 0x102e, 26 },  /* Gigabyte Local-Bus-SCSI */
};
#define SIGNATURE_COUNT (sizeof( signatures ) / sizeof( struct signature ))


static void do_pause( unsigned amount ) /* Pause for amount*10 milliseconds */
{
   unsigned long the_time = jiffies + amount; /* 0.01 seconds per jiffy */

   while (jiffies < the_time)
     ;
}

/*
 *  queue services:
 */
static inline void append_SC( Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
{
  Scsi_Cmnd *end;

  new_SC->host_scribble = (unsigned char *) NULL;
  if(!*SC)
    *SC=new_SC;
  else
    {
      for( end=*SC;
           end->host_scribble;
           end = (Scsi_Cmnd *) end->host_scribble )
        ;
      end->host_scribble = (unsigned char *) new_SC;
    }
}

static inline Scsi_Cmnd *remove_first_SC( Scsi_Cmnd **SC )
{
  Scsi_Cmnd *ptr;

  ptr=*SC;
  if(ptr)
    *SC= (Scsi_Cmnd *) (*SC)->host_scribble;
  return ptr;
}

static inline Scsi_Cmnd *remove_SC( Scsi_Cmnd **SC, int target, int lun )
{
  Scsi_Cmnd *ptr, *prev;

  for( ptr=*SC, prev=NULL;
       ptr && ((ptr->target!=target) || (ptr->lun!=lun));
       prev = ptr, ptr = (Scsi_Cmnd *) ptr->host_scribble )
    ;

  if(ptr)
    if(prev)
      prev->host_scribble = ptr->host_scribble;
    else
      *SC= (Scsi_Cmnd *) ptr->host_scribble;
  return ptr;
}

/*
 * read inbound byte and wait for ACK to get low
 */
static void make_acklow(void)
{
  SETPORT( SXFRCTL0, CH1|SPIOEN );
  GETPORT(SCSIDAT);
  SETPORT( SXFRCTL0, CH1 );

  while( TESTHI( SCSISIG, ACKI ) )
    ;
}

/*
 * detect current phase more reliable:
 * phase is valid, when the target asserts REQ after we've deasserted ACK.
 *
 * return value is a valid phase or an error code.
 *
 * errorcodes:
 *   P_BUSFREE   BUS FREE phase detected
 *   P_PARITY    parity error in DATA phase
 */
static int getphase(void)
{
  int phase, sstat1;
  
  while( 1 )
    {
      do
        {
          while( !( ( sstat1 = GETPORT( SSTAT1 ) ) & (BUSFREE|SCSIRSTI|REQINIT ) ) )
            ;
          if( sstat1 & BUSFREE )
            return P_BUSFREE;
          if( sstat1 & SCSIRSTI )
            {
              /* IBM drive responds with RSTI to RSTO */
              printk("aha152x: RESET IN\n");
              SETPORT( SSTAT1, SCSIRSTI );
            }
        }
      while( TESTHI( SCSISIG, ACKI ) || TESTLO( SSTAT1, REQINIT ) );

      SETPORT( SSTAT1, CLRSCSIPERR );
  
      phase = GETPORT( SCSISIG ) & P_MASK ;

      if( TESTHI( SSTAT1, SCSIPERR ) )
        {
          if( (phase & (CDO|MSGO))==0 )                         /* DATA phase */
            return P_PARITY;

          make_acklow();
        }
      else
        return phase;
    }
}

/* called from init/main.c */
void aha152x_setup( char *str, int *ints)
{
  if(setup_called)
    panic("aha152x: aha152x_setup called twice.\n");

  setup_called=ints[0];
  setup_str=str;

  setup_portbase  = ints[0] >= 1 ? ints[1] : 0x340;
  setup_irq       = ints[0] >= 2 ? ints[2] : 11;
  setup_scsiid	  = ints[0] >= 3 ? ints[3] : 7;
  setup_reconnect = ints[0] >= 4 ? ints[4] : 1;
  setup_doparity  = ints[0] >= 5 ? ints[5] : 1;
#ifdef DEBUG_AHA152X
  setup_debug     = ints[0] >= 6 ? ints[6] : DEBUG_DEFAULT;
#endif
}

/*
   Test, if port_base is valid.
 */
static int aha152x_porttest(int port_base)
{
  int i;

  if(check_region(port_base, TEST-SCSISEQ))
    return 0;

  SETPORT( DMACNTRL1, 0 );          /* reset stack pointer */
  for(i=0; i<16; i++)
    SETPORT( STACK, i );

  SETPORT( DMACNTRL1, 0 );          /* reset stack pointer */
  for(i=0; i<16 && GETPORT(STACK)==i; i++)
    ;

  return(i==16);
}

int aha152x_detect(Scsi_Host_Template * tpnt)
{
  int                 i, j,  ok;
  aha152x_config      conf;
  int                 interrupt_level;
  
  if(setup_called)
    {
      printk("aha152x: processing commandline: ");
   
#ifdef DEBUG_AHA152X
      if(setup_called>6)
#else
      if(setup_called>5)
#endif
        {
          printk("\naha152x: %s\n", setup_str );
#ifdef DEBUG_AHA152X
          printk("aha152x: usage: aha152x=<PORTBASE>[,<IRQ>[,<SCSI ID>[,<RECONNECT>[,<PARITY>[,<DEBUG>]]]]]\n");
#else
          printk("aha152x: usage: aha152x=<PORTBASE>[,<IRQ>[,<SCSI ID>[,<RECONNECT>[,<PARITY>]]]]\n");
#endif
          panic("aha152x panics in line %d", __LINE__);
        }

      port_base       = setup_portbase;
      interrupt_level = setup_irq;
      this_host       = setup_scsiid;
      can_disconnect  = setup_reconnect;
      can_doparity    = setup_doparity;
#ifdef DEBUG_AHA152X
      aha152x_debug   = setup_debug;
#endif

      for( i=0; i<PORT_COUNT && (port_base != ports[i]); i++)
        ;

      if(i==PORT_COUNT)
        {
          printk("unknown portbase 0x%03x\n", port_base);
          panic("aha152x panics in line %d", __LINE__);
        }

      if(!aha152x_porttest(port_base))
        {
          printk("portbase 0x%03x fails probe\n", port_base);
          panic("aha152x panics in line %d", __LINE__);
        }

      i=0;
      while(ints[i] && (interrupt_level!=ints[i]))
        i++;
      if(!ints[i])
        {
          printk("illegal IRQ %d\n", interrupt_level);
          panic("aha152x panics in line %d", __LINE__);
        }

      if( (this_host < 0) || (this_host > 7) )
        {
          printk("illegal SCSI ID %d\n", this_host);
          panic("aha152x panics in line %d", __LINE__);
        }

      if( (can_disconnect < 0) || (can_disconnect > 1) )
        {
          printk("reconnect %d should be 0 or 1\n", can_disconnect);
          panic("aha152x panics in line %d", __LINE__);
        }

      if( (can_doparity < 0) || (can_doparity > 1) )
        {
          printk("parity %d should be 0 or 1\n", can_doparity);
          panic("aha152x panics in line %d", __LINE__);
        }
      printk("ok\n");
    }
  else
    {
#if !defined(SKIP_BIOSTEST)
      ok=0;
      for( i=0; i < ADDRESS_COUNT && !ok; i++)
        for( j=0; (j < SIGNATURE_COUNT) && !ok; j++)
          ok=!memcmp((void *) addresses[i]+signatures[j].sig_offset,
                     (void *) signatures[j].signature,
                     (int) signatures[j].sig_length);

      if(!ok)
        return 0;

      printk("aha152x: BIOS test: passed, ");
#else
      printk("aha152x: ");
#endif /* !SKIP_BIOSTEST */
 
#if !defined(PORTBASE)
      printk("porttest: ");
      for( i=0; i<PORT_COUNT && !aha152x_porttest(ports[i]); i++)
        ;

      if(i==PORT_COUNT)
        {
          printk("failed\n");
          return 0;
        }
      else
        port_base=ports[i];
      printk("ok, ");
#else
      port_base=PORTBASE;
#endif /* !PORTBASE */

#if defined(AUTOCONF)

      conf.cf_port = (GETPORT(PORTA)<<8) + GETPORT(PORTB);

      interrupt_level = ints[conf.cf_irq];
      this_host       = conf.cf_id;
      can_disconnect  = conf.cf_tardisc;
      can_doparity    = !conf.cf_parity;

      printk("auto configuration: ok, ");

#endif /* AUTOCONF */

#if defined(IRQ)
      interrupt_level = IRQ; 
#endif

#if defined(SCSI_ID)
      this_host = SCSI_ID;
#endif

#if defined(RECONNECT)
      can_disconnect=RECONNECT;
#endif

#if defined(PARITY)
      can_doparity=PARITY;
#endif
    }

  printk("detection complete\n");
 
  ok = request_irq(interrupt_level, aha152x_intr, SA_INTERRUPT, "aha152x");
  
  if(ok<0)
    {
      if(ok == -EINVAL)
        {
           printk("aha152x: bad IRQ %d.\n", interrupt_level);
           printk("         Contact author.\n");
        }
      else
        if( ok == -EBUSY)
          printk( "aha152x: IRQ %d already in use. Configure another.\n",
                  interrupt_level);
        else
          {
            printk( "\naha152x: Unexpected error code on requesting IRQ %d.\n",
                    interrupt_level);
            printk("         Contact author.\n");
          }
      panic("aha152x: driver needs an IRQ.\n");
    }

  SETPORT( SCSIID, this_host << 4 );
  tpnt->this_id=this_host;
  
  if(can_disconnect)
    tpnt->can_queue=AHA152X_MAXQUEUE;

  /* RESET OUT */
  SETBITS(SCSISEQ, SCSIRSTO );
  do_pause(30);
  CLRBITS(SCSISEQ, SCSIRSTO );
  do_pause(60);

  aha152x_reset(NULL);

  printk("aha152x: vital data: PORTBASE=0x%03x, IRQ=%d, SCSI ID=%d, reconnect=%s, parity=%s\n",
         port_base,
         interrupt_level,
         this_host,
         can_disconnect ? "enabled" : "disabled",
         can_doparity ? "enabled" : "disabled");

  snarf_region(port_base, TEST-SCSISEQ);        /* Register */
  
  /* not expecting any interrupts */
  SETPORT(SIMODE0, 0);
  SETPORT(SIMODE1, 0);

  SETBITS( DMACNTRL0, INTEN);
  return 1;
}

/* 
 *  Queue a command and setup interrupts for a free bus.
 */
int aha152x_queue( Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
{
#if defined(DEBUG_RACE)
  enter_driver("queue");
#else
#if defined(DEBUG_QUEUE)
  if(aha152x_debug & debug_queue)
    printk("aha152x: queue(), ");
#endif
#endif

#if defined(DEBUG_QUEUE)
  if(aha152x_debug & debug_queue)
  {
    printk( "SCpnt (target = %d lun = %d cmnd = ",
            SCpnt->target,
            SCpnt->lun);
    print_command(SCpnt->cmnd);
    printk( ", pieces = %d size = %u), ",
            SCpnt->use_sg,
            SCpnt->request_bufflen );
    disp_ports();
  }
#endif

  SCpnt->scsi_done =       done;

  /* setup scratch area
     SCp.ptr              : buffer pointer
     SCp.this_residual    : buffer length
     SCp.buffer           : next buffer
     SCp.buffers_residual : left buffers in list
     SCp.phase            : current state of the command */
  SCpnt->SCp.phase = not_issued;
  if (SCpnt->use_sg)
    {
      SCpnt->SCp.buffer           = (struct scatterlist *)SCpnt->request_buffer;
      SCpnt->SCp.ptr              = SCpnt->SCp.buffer->address;
      SCpnt->SCp.this_residual    = SCpnt->SCp.buffer->length;
      SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
    }
  else
    {
      SCpnt->SCp.ptr              = (char *)SCpnt->request_buffer;
      SCpnt->SCp.this_residual    = SCpnt->request_bufflen;
      SCpnt->SCp.buffer           = NULL;
      SCpnt->SCp.buffers_residual = 0;
    }
          
  SCpnt->SCp.Status              = CHECK_CONDITION;
  SCpnt->SCp.Message             = 0;
  SCpnt->SCp.have_data_in        = 0;
  SCpnt->SCp.sent_command        = 0;

  /* Turn led on, when this is the first command. */
  cli();
  commands++;
  if(commands==1)
    SETPORT( PORTA, 1 );

#if defined(DEBUG_QUEUES)
  if(aha152x_debug & debug_queues)
    printk("i+ (%d), ", commands );
#endif
  append_SC( &issue_SC, SCpnt);
  
  /* Enable bus free interrupt, when we aren't currently on the bus */
  if(!current_SC)
    {
      SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
      SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
    }
  sti();

#if defined(DEBUG_RACE)
  leave_driver("queue");
#endif

  return 0;
}

/*
 *  We only support command in interrupt-driven fashion
 */
int aha152x_command( Scsi_Cmnd *SCpnt )
{
  printk( "aha152x: interrupt driven driver; use aha152x_queue()\n" );
  return -1;
}

/*
 *  Abort a queued command
 *  (commands that are on the bus can't be aborted easily)
 */
int aha152x_abort( Scsi_Cmnd *SCpnt)
{
  Scsi_Cmnd *ptr, *prev;

  cli();

#if defined(DEBUG_ABORT)
  if(aha152x_debug & debug_abort)
  { 
    printk("aha152x: abort(), SCpnt=0x%08x, ", (unsigned int) SCpnt );

    show_queues();
  }
#endif

  /* look for command in issue queue */
  for( ptr=issue_SC, prev=NULL;
       ptr && ptr!=SCpnt;
       prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
    ;

  if(ptr)
    {
      /* dequeue */
      if(prev)
        prev->host_scribble = ptr->host_scribble;
      else
        issue_SC = (Scsi_Cmnd *) ptr->host_scribble;
      sti();

      ptr->host_scribble = NULL;
      ptr->result = DID_ABORT << 16;
      ptr->done(ptr);
      return SCSI_ABORT_SUCCESS;
    }

  /* if the bus is busy or a command is currently processed,
     we can't do anything more */
  if ( TESTLO(SSTAT1, BUSFREE) || (current_SC && current_SC!=SCpnt))
    {
      /* fail abortion, if bus is busy */

      if(!current_SC)
        printk("bus busy w/o current command, ");
 
      sti();
      return SCSI_ABORT_BUSY;
    }

  /* bus is free */

  if(current_SC)
  { 
    /* target entered bus free before COMMAND COMPLETE, nothing to abort */
    sti();
    current_SC->result = DID_ERROR << 16;
    current_SC->done(current_SC);
    current_SC = (Scsi_Cmnd *) NULL;
    return SCSI_ABORT_SUCCESS;
  }

  /* look for command in disconnected queue */
  for( ptr=disconnected_SC, prev=NULL;
       ptr && ptr!=SCpnt;
       prev=ptr, ptr=(Scsi_Cmnd *) ptr->host_scribble)
    ;

  if(ptr)
    if(!aborting)
      {
        /* dequeue */
        if(prev)
          prev->host_scribble = ptr->host_scribble;
        else
          disconnected_SC = (Scsi_Cmnd *) ptr->host_scribble;
  
        /* set command current and initiate selection,
           let the interrupt routine take care of the abortion */
        current_SC     = ptr;
        ptr->SCp.phase = in_selection|aborted;
        SETPORT( SCSIID, (this_host << OID_) | current_SC->target );
  
        /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
        SETPORT( SIMODE0, ENSELDO | (disconnected_SC ? ENSELDI : 0) );
        SETPORT( SIMODE1, ENSELTIMO );
  
        /* Enable SELECTION OUT sequence */
        SETBITS(SCSISEQ, ENSELO | ENAUTOATNO );
  
        SETBITS( DMACNTRL0, INTEN );
        abort_result=SCSI_ABORT_SUCCESS;
        aborting++;
        abortion_complete=0;
        sti();
  
        /* sleep until the abortion is complete */
        while(!abortion_complete)
	  ;
        aborting=0;
        return abort_result;
      }
    else
      {
        /* we're already aborting a command */
        sti();
        return( SCSI_ABORT_BUSY );
      }

  /* command wasn't found */
  printk("command not found\n");
  sti();
  return SCSI_ABORT_NOT_RUNNING;
}

/*
 *  Restore default values to the AIC-6260 registers and reset the fifos
 */
static void aha152x_reset_ports(void)
{
  /* disable interrupts */
  SETPORT(DMACNTRL0, RSTFIFO);

  SETPORT(SCSISEQ, 0);

  SETPORT(SXFRCTL1, 0);
  SETPORT( SCSISIG, 0);
  SETPORT(SCSIRATE, 0);

  /* clear all interrupt conditions */
  SETPORT(SSTAT0, 0x7f);
  SETPORT(SSTAT1, 0xef);

  SETPORT(SSTAT4, SYNCERR|FWERR|FRERR);

  SETPORT(DMACNTRL0, 0);
  SETPORT(DMACNTRL1, 0);

  SETPORT(BRSTCNTRL, 0xf1);

  /* clear SCSI fifo and transfer count */
  SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
  SETPORT(SXFRCTL0, CH1);

  /* enable interrupts */
  SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
  SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
}

/*
 *  Reset registers, reset a hanging bus and
 *  kill active and disconnected commands for target w/o soft reset
 */
int aha152x_reset(Scsi_Cmnd * __unused)
{
  Scsi_Cmnd *ptr, *prev, *next;

  aha152x_reset_ports();

  /* Reset, if bus hangs */
  if( TESTLO( SSTAT1, BUSFREE ) )
    {
       CLRBITS( DMACNTRL0, INTEN );

#if defined(DEBUG_RESET)
  if(aha152x_debug & debug_reset)
  {
       printk("aha152x: reset(), bus not free: SCSI RESET OUT\n");
       show_queues();
  }
#endif

       if(current_SC && !current_SC->device->soft_reset)
         {
           current_SC->host_scribble = NULL;
           current_SC->result = DID_RESET << 16;
           current_SC->done(current_SC);
           current_SC=NULL;
         }

       cli();
       prev=NULL; ptr=disconnected_SC;
       while(ptr)
	 {
	   if(!ptr->device->soft_reset)
	     {
	       if(prev)
		 prev->host_scribble = ptr->host_scribble;
	       else
		 disconnected_SC = (Scsi_Cmnd *) ptr->host_scribble;

	       next = (Scsi_Cmnd *) ptr->host_scribble;
  
	       ptr->host_scribble = NULL;
	       ptr->result        = DID_RESET << 16;
	       ptr->done(ptr);
  
	       ptr = next; 
	     }
	   else
	     {
	       prev=ptr;
	       ptr = (Scsi_Cmnd *) ptr->host_scribble;
	     }
	 }
       sti();

#if defined( DEBUG_RESET )
       if(aha152x_debug & debug_reset)
       {
         printk("commands on targets w/ soft-resets:\n");
         show_queues();
       }
#endif

       /* RESET OUT */
       SETPORT(SCSISEQ, SCSIRSTO);
       do_pause(30);
       SETPORT(SCSISEQ, 0);
       do_pause(60);

       SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
       SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);

       SETPORT( DMACNTRL0, INTEN );
    }

  return SCSI_RESET_SUCCESS;
}

/*
 * Return the "logical geometry"
 */
int aha152x_biosparam(Scsi_Disk * disk, int dev, int *info_array )
{
  int size = disk->capacity;

#if defined(DEBUG_BIOSPARAM)
  if(aha152x_debug & debug_biosparam)
    printk("aha152x_biosparam: dev=%x, size=%d, ", dev, size);
#endif
  
/* I took this from other SCSI drivers, since it provides
   the correct data for my devices. */
  info_array[0]=64;
  info_array[1]=32;
  info_array[2]=size>>11;

#if defined(DEBUG_BIOSPARAM)
  if(aha152x_debug & debug_biosparam)
  {
    printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
           info_array[0], info_array[1], info_array[2]);
    printk("WARNING: check, if the bios geometry is correct.\n");
  }
#endif

  return 0;
}

/*
 *  Internal done function
 */
void aha152x_done( int error )
{
  Scsi_Cmnd *done_SC;

#if defined(DEBUG_DONE)
  if(aha152x_debug & debug_done)
  {
    printk("\naha152x: done(), ");
    disp_ports();
  }
#endif

  if (current_SC)
    {
#if defined(DEBUG_DONE)
      if(aha152x_debug & debug_done)
        printk("done(%x), ", error);
#endif

      cli();

      done_SC = current_SC;
      current_SC = NULL;

      /* turn led off, when no commands are in the driver */
      commands--;
      if(!commands)
        SETPORT( PORTA, 0 );                                  /* turn led off */

#if defined(DEBUG_QUEUES)
      if(aha152x_debug & debug_queues) 
        printk("ok (%d), ", commands);
#endif
      sti();

      SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
      SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);

#if defined(DEBUG_PHASES)
      if(aha152x_debug & debug_phases)
        printk("BUS FREE loop, ");
#endif
      while( TESTLO( SSTAT1, BUSFREE ) )
        ;
#if defined(DEBUG_PHASES)
      if(aha152x_debug & debug_phases)
        printk("BUS FREE\n");
#endif

      done_SC->result = error;
      if(done_SC->scsi_done)
        {
#if defined(DEBUG_DONE)
          if(aha152x_debug & debug_done)
            printk("calling scsi_done, ");
#endif
          done_SC->scsi_done( done_SC );
#if defined(DEBUG_DONE)
          if(aha152x_debug & debug_done)
            printk("done returned, ");
#endif
        }
      else
        panic( "aha152x: current_SC->scsi_done() == NULL" );
    }
  else
    aha152x_panic( "done() called outside of command" );
}

/*
 * Interrupts handler (main routine of the driver)
 */
void aha152x_intr( int irqno )
{
  int done=0, phase;

#if defined(DEBUG_RACE)
  enter_driver("intr");
#else
#if defined(DEBUG_INTR)
  if(aha152x_debug & debug_intr)
    printk("\naha152x: intr(), ");
#endif
#endif

  /* no more interrupts from the controller, while we busy.
     INTEN has to be restored, when we're ready to leave
     intr(). To avoid race conditions we have to return
     immediately afterwards. */
  CLRBITS( DMACNTRL0, INTEN);
  sti();

  /* disconnected target is trying to reconnect.
     Only possible, if we have disconnected nexuses and
     nothing is occupying the bus.
  */
  if( TESTHI( SSTAT0, SELDI ) &&
      disconnected_SC &&
      ( !current_SC || ( current_SC->SCp.phase & in_selection ) )
    )
    {
      int identify_msg, target, i;

      /* Avoid conflicts when a target reconnects
         while we are trying to connect to another. */
      if(current_SC)
        {
#if defined(DEBUG_QUEUES)
	  if(aha152x_debug & debug_queues)
          printk("i+, ");
#endif
          cli();
          append_SC( &issue_SC, current_SC);
          current_SC=NULL;
          sti();
        }

      /* disable sequences */
      SETPORT( SCSISEQ, 0 );
      SETPORT( SSTAT0, CLRSELDI );
      SETPORT( SSTAT1, CLRBUSFREE );

#if defined(DEBUG_QUEUES) || defined(DEBUG_PHASES)
      if(aha152x_debug & (debug_queues|debug_phases))
        printk("reselected, ");
#endif

      i = GETPORT(SELID) & ~(1 << this_host);
      target=0;
      if(i)
        for( ; (i & 1)==0; target++, i>>=1)
          ;
      else
        aha152x_panic("reconnecting target unknown");

#if defined(DEBUG_QUEUES)
      if(aha152x_debug & debug_queues)
        printk("SELID=%02x, target=%d, ", GETPORT(SELID), target );
#endif
      SETPORT( SCSIID, (this_host << OID_) | target );
      SETPORT( SCSISEQ, ENRESELI );

      if(TESTLO( SSTAT0, SELDI ))
        aha152x_panic("RESELI failed");

      SETPORT( SCSISIG, P_MSGI );

      /* Get identify message */
      if((i=getphase())!=P_MSGI)
        {
          printk("target doesn't enter MSGI to identify (phase=%02x)\n", i);
          aha152x_panic("unknown lun");
        }
      SETPORT( SCSISEQ, 0 );

      SETPORT( SXFRCTL0, CH1);

      identify_msg = GETPORT(SCSIBUS);

      if(!(identify_msg & IDENTIFY_BASE))
        {
          printk("target=%d, inbound message (%02x) != IDENTIFY\n",
                 target, identify_msg);
          aha152x_panic("unknown lun");
        }

      make_acklow();
      getphase();

#if defined(DEBUG_QUEUES)
      if(aha152x_debug & debug_queues)
        printk("identify=%02x, lun=%d, ", identify_msg, identify_msg & 0x3f );
#endif

      cli();
#if defined(DEBUG_QUEUES)
      if(aha152x_debug & debug_queues)
        printk("d-, ");
#endif
      current_SC = remove_SC( &disconnected_SC,
                              target,
                              identify_msg & 0x3f );

      if(!current_SC)
        {
          printk("lun=%d, ", identify_msg & 0x3f );
          aha152x_panic("no disconnected command for that lun");
        }

      current_SC->SCp.phase &= ~disconnected;
      sti();

      SETPORT( SIMODE0, 0 );
      SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );
#if defined(DEBUG_RACE)
      leave_driver("(reselected) intr");
#endif
      SETBITS( DMACNTRL0, INTEN);
      return;
    }
  
  /* Check, if we aren't busy with a command */
  if(!current_SC)
    {
      /* bus is free to issue a queued command */
      if(TESTHI( SSTAT1, BUSFREE) && issue_SC)
        {
          cli();
#if defined(DEBUG_QUEUES)
          if(aha152x_debug & debug_queues)
            printk("i-, ");
#endif
          current_SC = remove_first_SC( &issue_SC );
          sti();

#if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
	  if(aha152x_debug & (debug_intr|debug_selection|debug_phases))
            printk("issuing command, ");
#endif
          current_SC->SCp.phase = in_selection;

  #if defined(DEBUG_INTR) || defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
	  if(aha152x_debug & (debug_intr|debug_selection|debug_phases))
            printk("selecting %d, ", current_SC->target); 
  #endif
          SETPORT( SCSIID, (this_host << OID_) | current_SC->target );

          /* Enable interrupts for SELECTION OUT DONE and SELECTION OUT INITIATED */
          SETPORT( SXFRCTL1, can_doparity ? (ENSPCHK|ENSTIMER) : ENSTIMER);

          /* enable interrupts for SELECTION OUT DONE and SELECTION TIME OUT */
          SETPORT( SIMODE0, ENSELDO | (disconnected_SC ? ENSELDI : 0) );
          SETPORT( SIMODE1, ENSELTIMO );

          /* Enable SELECTION OUT sequence */
          SETBITS(SCSISEQ, ENSELO | ENAUTOATNO );
        
  #if defined(DEBUG_RACE)
          leave_driver("(selecting) intr");
  #endif
          SETBITS( DMACNTRL0, INTEN );
          return;
        }

      /* No command we are busy with and no new to issue */
      printk("aha152x: ignoring spurious interrupt, nothing to do\n");
      return;
    }

  /* the bus is busy with something */

#if defined(DEBUG_INTR)
  if(aha152x_debug & debug_intr)
    disp_ports();
#endif

  /* we are waiting for the result of a selection attempt */
  if(current_SC->SCp.phase & in_selection)
    {
      if( TESTLO( SSTAT1, SELTO ) )
        /* no timeout */
        if( TESTHI( SSTAT0, SELDO ) )
          {
            /* clear BUS FREE interrupt */
            SETPORT( SSTAT1, CLRBUSFREE);

            /* Disable SELECTION OUT sequence */
            CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );

            /* Disable SELECTION OUT DONE interrupt */
            CLRBITS(SIMODE0, ENSELDO);
            CLRBITS(SIMODE1, ENSELTIMO);

            if( TESTLO(SSTAT0, SELDO) )
              {
                printk("aha152x: passing bus free condition\n");

#if defined(DEBUG_RACE)
                leave_driver("(passing bus free) intr");
#endif
                SETBITS( DMACNTRL0, INTEN);

                if(current_SC->SCp.phase & aborted)
                  {
                    abort_result=SCSI_ABORT_ERROR;
		    abortion_complete++;
                  }

                aha152x_done( DID_NO_CONNECT << 16 );
                return;
              }
#if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
	    if(aha152x_debug & (debug_selection|debug_phases))
              printk("SELDO (SELID=%x), ", GETPORT(SELID));
#endif

            /* selection was done */
            SETPORT( SSTAT0, CLRSELDO );

#if defined(DEBUG_ABORT)
            if((aha152x_debug & debug_abort) && (current_SC->SCp.phase & aborted))
              printk("(ABORT) target selected, ");
#endif

            current_SC->SCp.phase &= ~in_selection;
            current_SC->SCp.phase |= in_other;

#if defined(DEBUG_RACE)
            leave_driver("(SELDO) intr");
#endif

            SETPORT( SCSISIG, P_MSGO );

            SETPORT( SIMODE0, 0 );
            SETPORT( SIMODE1, ENREQINIT|ENBUSFREE );
            SETBITS( DMACNTRL0, INTEN);
            return;
          }
        else
          aha152x_panic("neither timeout nor selection\007");
      else
        {
#if defined(DEBUG_SELECTION) || defined(DEBUG_PHASES)
	  if(aha152x_debug & (debug_selection|debug_phases))
          printk("SELTO, ");
#endif
	  /* end selection attempt */
          CLRBITS(SCSISEQ, ENSELO|ENAUTOATNO );

          /* timeout */
          SETPORT( SSTAT1, CLRSELTIMO );

          SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
          SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
          SETBITS( DMACNTRL0, INTEN );
#if defined(DEBUG_RACE)
          leave_driver("(SELTO) intr");
#endif

          if(current_SC->SCp.phase & aborted)
            {
#if defined(DEBUG_ABORT)
	      if(aha152x_debug & debug_abort)
                printk("(ABORT) selection timeout, ");
#endif
              abort_result=SCSI_ABORT_ERROR;
              abortion_complete++;
            }

          if( TESTLO( SSTAT0, SELINGO ) )
            /* ARBITRATION not won */
            aha152x_done( DID_BUS_BUSY << 16 );
          else
            /* ARBITRATION won, but SELECTION failed */
            aha152x_done( DID_NO_CONNECT << 16 );
          return;
        }
    }

  /* enable interrupt, when target leaves current phase */
  phase = getphase();
  if(!(phase & ~P_MASK))                                      /* "real" phase */
    SETPORT(SCSISIG, phase);
  SETPORT(SSTAT1, CLRPHASECHG);
  current_SC->SCp.phase =
    (current_SC->SCp.phase & ~((P_MASK|1)<<16)) | (phase << 16 );

  /* information transfer phase */
  switch( phase )
    {
    case P_MSGO:                                               /* MESSAGE OUT */
      {
        unsigned char message;

#if defined(DEBUG_INTR) || defined(DEBUG_MSGO) || defined(DEBUG_PHASES)
        if(aha152x_debug & (debug_intr|debug_msgo|debug_phases))
          printk("MESSAGE OUT, ");
#endif

        if( current_SC->SCp.phase & aborted )
          {
#if defined(DEBUG_MSGO) || defined(DEBUG_ABORT)
            if(aha152x_debug & (debug_msgo|debug_abort))
              printk("ABORT, ");
#endif
            message=ABORT;
          }
        else
          /* If we didn't identify yet, do it. Otherwise there's nothing to do,
             but reject (probably we got an message before, that we have to
             reject (SDTR, WDTR, etc.) */
          if( !(current_SC->SCp.phase & sent_ident))
            {
              message=IDENTIFY(can_disconnect,current_SC->lun);
#if defined(DEBUG_MSGO)
              if(aha152x_debug & debug_msgo)
                printk("IDENTIFY (reconnect=%s;lun=%d), ", 
                        can_disconnect ? "enabled" : "disabled", current_SC->lun);
#endif
            }
          else
            {
              message=MESSAGE_REJECT;
#if defined(DEBUG_MSGO)
              if(aha152x_debug & debug_msgo)
                printk("REJECT, ");
#endif
            }
          
        CLRBITS( SXFRCTL0, ENDMA);

        SETPORT( SIMODE0, 0 );
        SETPORT( SIMODE1, ENPHASEMIS|ENREQINIT|ENBUSFREE );

        /* wait for data latch to become ready or a phase change */
        while( TESTLO( DMASTAT, INTSTAT ) )
          ;

        if( TESTHI( SSTAT1, PHASEMIS ) )
          aha152x_panic("unable to send message");

        /* Leave MESSAGE OUT after transfer */
        SETPORT( SSTAT1, CLRATNO);

        SETPORT( SCSIDAT, message );

        make_acklow();
        getphase();

        if(message==IDENTIFY(can_disconnect,current_SC->lun))
          current_SC->SCp.phase |= sent_ident;

        if(message==ABORT)
          {
            /* revive abort(); abort() enables interrupts */
            abort_result=SCSI_ABORT_SUCCESS;
            abortion_complete++;

            current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));

            /* exit */
            SETBITS( DMACNTRL0, INTEN );
#if defined(DEBUG_RACE)
            leave_driver("(ABORT) intr");
#endif
            aha152x_done(DID_ABORT<<16);
            return;
          }
      }
      break;

    case P_CMD:                                          /* COMMAND phase */
#if defined(DEBUG_INTR) || defined(DEBUG_CMD) || defined(DEBUG_PHASES)
      if(aha152x_debug & (debug_intr|debug_cmd|debug_phases))
        printk("COMMAND, ");
#endif
      if( !(current_SC->SCp.sent_command) )
        {
          if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
            printk("aha152x: P_CMD: %d(%d) bytes left in FIFO, resetting\n",
                   GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));

          /* reset fifo and enable writes */
          SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
          SETPORT(DMACNTRL0, ENDMA|WRITE_READ);

          /* clear transfer count and scsi fifo */
          SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
          SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
  
          /* missing phase raises INTSTAT */
          SETPORT( SIMODE0, 0 );
          SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );
  
#if defined(DEBUG_CMD)
          if(aha152x_debug & debug_cmd)
            printk("waiting, ");
#endif
          /* wait for FIFO to get empty */
          while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
            ;
  
          if( TESTHI( SSTAT1, PHASEMIS ) )
            aha152x_panic("target left COMMAND phase");

#if defined(DEBUG_CMD)
          if(aha152x_debug & debug_cmd)
          {
            printk("DFIFOEMP, outsw (%d words), ",
                   current_SC->cmd_len >>1 );
            disp_ports();
          }
#endif
  
          outsw( DATAPORT,
                 &current_SC->cmnd,
		current_SC->cmd_len >>1 );

#if defined(DEBUG_CMD)
	  if(aha152x_debug & debug_cmd)
          {
            printk("FCNT=%d, STCNT=%d, ", GETPORT(FIFOSTAT), GETSTCNT() );
            disp_ports();
          }
#endif

          /* wait for SCSI FIFO to get empty.
             very important to send complete commands. */
          while( TESTLO ( SSTAT2, SEMPTY ) )
            ;

          CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
          /* transfer can be considered ended, when SCSIEN reads back zero */
          while( TESTHI( SXFRCTL0, SCSIEN ) )
            ;

          CLRBITS(DMACNTRL0, ENDMA);

#if defined(DEBUG_CMD) || defined(DEBUG_INTR)
          if(debug_cmd & debug_intr)
            printk("sent %d/%d command bytes, ", GETSTCNT(),
                   current_SC->cmd_len);
#endif

        }
      else
        aha152x_panic("Nothing to sent while in COMMAND OUT");
      break;

    case P_MSGI:                                          /* MESSAGE IN phase */
#if defined(DEBUG_INTR) || defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
      if(aha152x_debug & (debug_intr|debug_msgi|debug_phases))
        printk("MESSAGE IN, ");
#endif
      SETPORT( SXFRCTL0, CH1);

      SETPORT( SIMODE0, 0);
      SETPORT( SIMODE1, ENBUSFREE);
  
      while( phase == P_MSGI ) 
        {
          current_SC->SCp.Message = GETPORT( SCSIBUS );
          switch(current_SC->SCp.Message)
            {
            case DISCONNECT:
#if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
	      if(aha152x_debug & (debug_msgi|debug_phases))
                printk("target disconnected, ");
#endif
              current_SC->SCp.Message = 0;
              current_SC->SCp.phase   |= disconnected;
              if(!can_disconnect)
                aha152x_panic("target was not allowed to disconnect");
              break;
        
            case COMMAND_COMPLETE:
#if defined(DEBUG_MSGI) || defined(DEBUG_PHASES)
	      if(aha152x_debug & (debug_msgi|debug_phases))
                printk("inbound message ( COMMAND COMPLETE ), ");
#endif
              done++;
              break;

            case MESSAGE_REJECT:
#if defined(DEBUG_MSGI)
	      if(aha152x_debug & debug_msgi)
                printk("inbound message ( MESSAGE REJECT ), ");
#endif
              break;

            case SAVE_POINTERS:
#if defined(DEBUG_MSGI)
	      if(aha152x_debug & debug_msgi)
                printk("inbound message ( SAVE DATA POINTERS ), ");
#endif
              break;

            case EXTENDED_MESSAGE:
              { 
                int           i, code;

#if defined(DEBUG_MSGI)
	        if(aha152x_debug & debug_msgi)
                  printk("inbound message ( EXTENDED MESSAGE ), ");
#endif
                make_acklow();
                if(getphase()!=P_MSGI)
                  break;
  
                i=GETPORT(SCSIBUS);

#if defined(DEBUG_MSGI)
	        if(aha152x_debug & debug_msgi)
                  printk("length (%d), code ( ", i);
#endif

                make_acklow();
                if(getphase()!=P_MSGI)
                  break;

                code = GETPORT(SCSIBUS);

                switch( code )
                  {
                  case 0x00:
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      printk("MODIFY DATA POINTER ");
#endif
                    SETPORT(SCSISIG, P_MSGI|ATNO);
                    break;
                  case 0x01:
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      printk("SYNCHRONOUS DATA TRANSFER REQUEST ");
#endif
                    SETPORT(SCSISIG, P_MSGI|ATNO);
                    break;
                  case 0x02:
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      printk("EXTENDED IDENTIFY ");
#endif
                    break;
                  case 0x03:
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      printk("WIDE DATA TRANSFER REQUEST ");
#endif
                    SETPORT(SCSISIG, P_MSGI|ATNO);
                    break;
                  default:
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      if( code & 0x80 )
                        printk("reserved (%d) ", code );
                      else
                        printk("vendor specific (%d) ", code);
#endif
                    SETPORT(SCSISIG, P_MSGI|ATNO);
                    break;
                  }
#if defined(DEBUG_MSGI)
	        if(aha152x_debug & debug_msgi)
                  printk(" ), data ( ");
#endif
                while( --i && (make_acklow(), getphase()==P_MSGI))
                  {
#if defined(DEBUG_MSGI)
	            if(aha152x_debug & debug_msgi)
                      printk("%x ", GETPORT(SCSIBUS) );
#else
                    GETPORT(SCSIBUS);
#endif
                  }
#if defined(DEBUG_MSGI)
	        if(aha152x_debug & debug_msgi)
                  printk(" ), ");
#endif
                /* We reject all extended messages. To do this
                   we just enter MSGO by asserting ATN. Since
                   we have already identified a REJECT message
                   will be sent. */
                SETPORT(SCSISIG, P_MSGI|ATNO);
              }
              break;
       
            default:
              printk("unsupported inbound message %x, ", current_SC->SCp.Message);
              break;

            }

          make_acklow();
          phase=getphase();
        } 

      /* clear SCSI fifo on BUSFREE */
      if(phase==P_BUSFREE)
        SETPORT(SXFRCTL0, CH1|CLRCH1);

      if(current_SC->SCp.phase & disconnected)
        {
          cli();
#if defined(DEBUG_QUEUES)
	  if(aha152x_debug & debug_queues)
            printk("d+, ");
#endif
          append_SC( &disconnected_SC, current_SC);
          current_SC = NULL;
          sti();

          SETBITS( SCSISEQ, ENRESELI );

          SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
          SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);

          SETBITS( DMACNTRL0, INTEN );
          return;
        }
      break;

    case P_STATUS:                                         /* STATUS IN phase */
#if defined(DEBUG_STATUS) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
      if(aha152x_debug & (debug_status|debug_intr|debug_phases))
        printk("STATUS, ");
#endif
      SETPORT( SXFRCTL0, CH1);

      SETPORT( SIMODE0, 0 );
      SETPORT( SIMODE1, ENREQINIT|ENBUSFREE );

      if( TESTHI( SSTAT1, PHASEMIS ) )
	printk("aha152x: passing STATUS phase");
	
      current_SC->SCp.Status = GETPORT( SCSIBUS );
      make_acklow();
      getphase();

#if defined(DEBUG_STATUS)
      if(aha152x_debug & debug_status)
      {
        printk("inbound status ");
        print_status( current_SC->SCp.Status );
        printk(", ");
      }
#endif
      break;

    case P_DATAI:                                            /* DATA IN phase */
      {
        int fifodata, data_count, done;

#if defined(DEBUG_DATAI) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
        if(aha152x_debug & (debug_datai|debug_intr|debug_phases))
          printk("DATA IN, ");
#endif

        if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT))
          printk("aha152x: P_DATAI: %d(%d) bytes left in FIFO, resetting\n",
                 GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT));

        /* reset host fifo */
        SETPORT(DMACNTRL0, RSTFIFO);
        SETPORT(DMACNTRL0, RSTFIFO|ENDMA);

        SETPORT(SXFRCTL0, CH1|SCSIEN|DMAEN );

        SETPORT( SIMODE0, 0 );
        SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );

        /* done is set when the FIFO is empty after the target left DATA IN */
        done=0;
      
        /* while the target stays in DATA to transfer data */
        while ( !done ) 
          {
#if defined(DEBUG_DATAI)
            if(aha152x_debug & debug_datai)
              printk("expecting data, ");
#endif
            /* wait for PHASEMIS or full FIFO */
            while( TESTLO ( DMASTAT, DFIFOFULL|INTSTAT ) )
              ;

            if( TESTHI( DMASTAT, DFIFOFULL ) )
              fifodata=GETPORT(FIFOSTAT);
            else
              {
                /* wait for SCSI fifo to get empty */
                while( TESTLO( SSTAT2, SEMPTY ) )
                  ;

                /* rest of data in FIFO */
                fifodata=GETPORT(FIFOSTAT);
#if defined(DEBUG_DATAI)
                if(aha152x_debug & debug_datai)
                  printk("last transfer, ");
#endif
                done=1;
              }
  
#if defined(DEBUG_DATAI)
            if(aha152x_debug & debug_datai)
              printk("fifodata=%d, ", fifodata);
#endif

            while( fifodata && current_SC->SCp.this_residual )
              {
                data_count=fifodata;
  
                /* limit data transfer to size of first sg buffer */
                if (data_count > current_SC->SCp.this_residual)
                  data_count = current_SC->SCp.this_residual;
  
                fifodata -= data_count;

#if defined(DEBUG_DATAI)
                if(aha152x_debug & debug_datai)
                  printk("data_count=%d, ", data_count);
#endif
  
                if(data_count&1)
                  {
                    /* get a single byte in byte mode */
                    SETBITS(DMACNTRL0, _8BIT );
                    *current_SC->SCp.ptr++ = GETPORT( DATAPORT );
                    current_SC->SCp.this_residual--;
                  }
                if(data_count>1)
                  {
                    CLRBITS(DMACNTRL0, _8BIT );
                    data_count >>= 1; /* Number of words */
                    insw( DATAPORT, current_SC->SCp.ptr, data_count );
#if defined(DEBUG_DATAI)
                    if(aha152x_debug & debug_datai)
/* show what comes with the last transfer */
                      if(done)
                        {
                          int           i;
                          unsigned char *data;
  
                          printk("data on last transfer (%d bytes: ",
                                 2*data_count);
                          data = (unsigned char *) current_SC->SCp.ptr;
                          for( i=0; i<2*data_count; i++)
                            printk("%2x ", *data++);
                          printk("), ");
                        }
#endif
                    current_SC->SCp.ptr           += 2 * data_count;
                    current_SC->SCp.this_residual -= 2 * data_count;
                  }
              
                /* if this buffer is full and there are more buffers left */
                if (!current_SC->SCp.this_residual &&
                     current_SC->SCp.buffers_residual)
                  {
                    /* advance to next buffer */
                    current_SC->SCp.buffers_residual--;
                    current_SC->SCp.buffer++;
                    current_SC->SCp.ptr =
                      current_SC->SCp.buffer->address;
                    current_SC->SCp.this_residual =
                      current_SC->SCp.buffer->length;
                  } 
              }
 
            /*
             * Fifo should be empty
             */
            if(fifodata>0)
              {
                printk("aha152x: more data than expected (%d bytes)\n",
                       GETPORT(FIFOSTAT));
                SETBITS(DMACNTRL0, _8BIT );
                printk("aha152x: data ( ");
                while(fifodata--)
                  printk("%2x ", GETPORT( DATAPORT ));
                printk(")\n");
              }

#if defined(DEBUG_DATAI)
            if(aha152x_debug & debug_datai)
              if(!fifodata)
                printk("fifo empty, ");
              else
                printk("something left in fifo, ");
#endif
          }

#if defined(DEBUG_DATAI)
        if((aha152x_debug & debug_datai) && (current_SC->SCp.buffers_residual || current_SC->SCp.this_residual))
          printk("left buffers (buffers=%d, bytes=%d), ",
                 current_SC->SCp.buffers_residual, 
                 current_SC->SCp.this_residual);
#endif
        /* transfer can be considered ended, when SCSIEN reads back zero */
        CLRBITS(SXFRCTL0, SCSIEN|DMAEN);
        while( TESTHI( SXFRCTL0, SCSIEN ) )
          ;
        CLRBITS(DMACNTRL0, ENDMA );

#if defined(DEBUG_DATAI) || defined(DEBUG_INTR)
        if(aha152x_debug & (debug_datai|debug_intr))
          printk("got %d bytes, ", GETSTCNT());
#endif

        current_SC->SCp.have_data_in++;
      }
      break;

    case P_DATAO:                                           /* DATA OUT phase */
      {
        int data_count;

#if defined(DEBUG_DATAO) || defined(DEBUG_INTR) || defined(DEBUG_PHASES)
        if(aha152x_debug & (debug_datao|debug_intr|debug_phases))
          printk("DATA OUT, ");
#endif
#if defined(DEBUG_DATAO)
        if(aha152x_debug & debug_datao)
          printk("got data to send (bytes=%d, buffers=%d), ",
                 current_SC->SCp.this_residual,
                 current_SC->SCp.buffers_residual );
#endif

        if(GETPORT(FIFOSTAT) || GETPORT(SSTAT2) & (SFULL|SFCNT) )
          {
            printk("%d(%d) left in FIFO, ", GETPORT(FIFOSTAT), GETPORT(SSTAT2) & (SFULL|SFCNT) );
            aha152x_panic("FIFO should be empty");
          }

        SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
        SETPORT(DMACNTRL0, ENDMA|WRITE_READ);

        SETPORT(SXFRCTL0, CH1|CLRSTCNT|CLRCH1 );
        SETPORT(SXFRCTL0, SCSIEN|DMAEN|CH1);
 
        SETPORT( SIMODE0, 0 );
        SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );

        /* while current buffer is not empty or
           there are more buffers to transfer */
        while( TESTLO( SSTAT1, PHASEMIS ) &&
                 (current_SC->SCp.this_residual ||
                  current_SC->SCp.buffers_residual) )
          {
#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("sending data (left: bytes=%d, buffers=%d), waiting, ",
                     current_SC->SCp.this_residual,
                     current_SC->SCp.buffers_residual);
#endif
            /* transfer rest of buffer, but max. 128 byte */
            data_count = current_SC->SCp.this_residual > 128 ?
                         128 : current_SC->SCp.this_residual ;

#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("data_count=%d, ", data_count);
#endif
  
            if(data_count&1)
              {
                /* put a single byte in byte mode */
                SETBITS(DMACNTRL0, _8BIT );
                SETPORT(DATAPORT, *current_SC->SCp.ptr++);
                current_SC->SCp.this_residual--;
              }
            if(data_count>1)
              {
                CLRBITS(DMACNTRL0, _8BIT );
                data_count >>= 1; /* Number of words */
                outsw( DATAPORT, current_SC->SCp.ptr, data_count );
                current_SC->SCp.ptr           += 2 * data_count;
                current_SC->SCp.this_residual -= 2 * data_count;
              }

            /* wait for FIFO to get empty */
            while( TESTLO ( DMASTAT, DFIFOEMP|INTSTAT ) )
              ;

#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("fifo (%d bytes), transfered (%d bytes), ",
                     GETPORT(FIFOSTAT), GETSTCNT() );
#endif

            /* if this buffer is empty and there are more buffers left */
            if ( TESTLO( SSTAT1, PHASEMIS ) &&
                 !current_SC->SCp.this_residual &&
                  current_SC->SCp.buffers_residual)
              {
                 /* advance to next buffer */
                 current_SC->SCp.buffers_residual--;
                 current_SC->SCp.buffer++;
                 current_SC->SCp.ptr =
                   current_SC->SCp.buffer->address;
                 current_SC->SCp.this_residual =
                 current_SC->SCp.buffer->length;
              }
          }

        if ( current_SC->SCp.this_residual ||
             current_SC->SCp.buffers_residual )
          {
            /* target leaves DATA OUT for an other phase
               (perhaps disconnect) */

            /* data in fifos has to be resend */
            data_count = GETPORT(SSTAT2) & (SFULL|SFCNT);

            data_count += GETPORT(FIFOSTAT) ;
            current_SC->SCp.ptr           -= data_count;
            current_SC->SCp.this_residual += data_count;
#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("left data (bytes=%d, buffers=%d), fifos (bytes=%d), transfer incomplete, resetting fifo, ",
                     current_SC->SCp.this_residual,
                     current_SC->SCp.buffers_residual,
                     data_count );
#endif
            SETPORT(DMACNTRL0, WRITE_READ|RSTFIFO);
            CLRBITS(SXFRCTL0, SCSIEN|DMAEN );
            CLRBITS(DMACNTRL0, ENDMA);
          }
        else
          {
#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("waiting for SCSI fifo to get empty, ");
#endif
            /* wait for SCSI fifo to get empty */
            while( TESTLO( SSTAT2, SEMPTY ) )
              ;
#if defined(DEBUG_DATAO)
            if(aha152x_debug & debug_datao)
              printk("ok, left data (bytes=%d, buffers=%d) ",
                     current_SC->SCp.this_residual,
                     current_SC->SCp.buffers_residual);
#endif
            CLRBITS(SXFRCTL0, SCSIEN|DMAEN);

            /* transfer can be considered ended, when SCSIEN reads back zero */
            while( TESTHI( SXFRCTL0, SCSIEN ) )
              ;

            CLRBITS(DMACNTRL0, ENDMA);
          }

#if defined(DEBUG_DATAO) || defined(DEBUG_INTR)
        if(aha152x_debug & (debug_datao|debug_intr))
          printk("sent %d data bytes, ", GETSTCNT() );
#endif
      }
      break;

    case P_BUSFREE:                                                /* BUSFREE */
#if defined(DEBUG_RACE)
      leave_driver("(BUSFREE) intr");
#endif
#if defined(DEBUG_PHASES)
      if(aha152x_debug & debug_phases)
        printk("unexpected BUS FREE, ");
#endif
      current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));

      aha152x_done( DID_ERROR << 16 );               /* Don't know any better */
      return;
      break;

    case P_PARITY:                              /* parity error in DATA phase */
#if defined(DEBUG_RACE)
      leave_driver("(DID_PARITY) intr");
#endif
      printk("PARITY error in DATA phase, ");

      current_SC->SCp.phase = (current_SC->SCp.phase & ~(P_MASK<<16));

      SETBITS( DMACNTRL0, INTEN );
      aha152x_done( DID_PARITY << 16 );
      return;
      break;

    default:
      printk("aha152x: unexpected phase\n");
      break;
    }

  if(done)
    {
#if defined(DEBUG_INTR)
      if(aha152x_debug & debug_intr)
        printk("command done.\n");
#endif
#if defined(DEBUG_RACE)
      leave_driver("(done) intr");
#endif

      SETPORT(SIMODE0, disconnected_SC ? ENSELDI : 0 );
      SETPORT(SIMODE1, issue_SC ? ENBUSFREE : 0);
      SETPORT(SCSISEQ, disconnected_SC ? ENRESELI : 0 );

      SETBITS( DMACNTRL0, INTEN );

      aha152x_done(   (current_SC->SCp.Status  & 0xff)
                    | ( (current_SC->SCp.Message & 0xff) << 8)
                    | ( DID_OK << 16) );

#if defined(DEBUG_RACE)
      printk("done returned (DID_OK: Status=%x; Message=%x).\n",
             current_SC->SCp.Status, current_SC->SCp.Message);
#endif
      return;
    }

  if(current_SC)
    current_SC->SCp.phase |= 1<<16 ;

  SETPORT( SIMODE0, 0 );
  SETPORT( SIMODE1, ENPHASEMIS|ENBUSFREE );
#if defined(DEBUG_INTR)
  if(aha152x_debug & debug_intr)
    disp_enintr();
#endif
#if defined(DEBUG_RACE)
  leave_driver("(PHASEEND) intr");
#endif

  SETBITS( DMACNTRL0, INTEN);
  return;
}

/* 
 * Dump the current driver status and panic...
 */
static void aha152x_panic(char *msg)
{
  printk("\naha152x_panic: %s\n", msg);
  show_queues();
  panic("aha152x panic");
}

/*
 * Display registers of AIC-6260
 */
static void disp_ports(void)
{
#ifdef DEBUG_AHA152X
  int s;

  if(aha152x_debug & debug_skipports)
	return;

  printk("\n%s: ", current_SC ? "on bus" : "waiting");

  s=GETPORT(SCSISEQ);
  printk("SCSISEQ ( ");
  if( s & TEMODEO )     printk("TARGET MODE ");
  if( s & ENSELO )      printk("SELO ");
  if( s & ENSELI )      printk("SELI ");
  if( s & ENRESELI )    printk("RESELI ");
  if( s & ENAUTOATNO )  printk("AUTOATNO ");
  if( s & ENAUTOATNI )  printk("AUTOATNI ");
  if( s & ENAUTOATNP )  printk("AUTOATNP ");
  if( s & SCSIRSTO )    printk("SCSIRSTO ");
  printk(");");

  printk(" SCSISIG ( ");
  s=GETPORT(SCSISIG);
  switch(s & P_MASK)
    {
    case P_DATAO:
      printk("DATA OUT");
      break;
    case P_DATAI:
      printk("DATA IN");
      break;
    case P_CMD:
      printk("COMMAND"); 
      break;
    case P_STATUS:
      printk("STATUS"); 
      break;
    case P_MSGO:
      printk("MESSAGE OUT");
      break;
    case P_MSGI:
      printk("MESSAGE IN");
      break;
    default:
      printk("*illegal*");
      break;
    }
  
  printk(" ); ");

  printk("INTSTAT ( %s ); ", TESTHI(DMASTAT, INTSTAT) ? "hi" : "lo");

  printk("SSTAT ( ");
  s=GETPORT(SSTAT0);
  if( s & TARGET )   printk("TARGET ");
  if( s & SELDO )    printk("SELDO ");
  if( s & SELDI )    printk("SELDI ");
  if( s & SELINGO )  printk("SELINGO ");
  if( s & SWRAP )    printk("SWRAP ");
  if( s & SDONE )    printk("SDONE ");
  if( s & SPIORDY )  printk("SPIORDY ");
  if( s & DMADONE )  printk("DMADONE ");

  s=GETPORT(SSTAT1);
  if( s & SELTO )     printk("SELTO ");
  if( s & ATNTARG )   printk("ATNTARG ");
  if( s & SCSIRSTI )  printk("SCSIRSTI ");
  if( s & PHASEMIS )  printk("PHASEMIS ");
  if( s & BUSFREE )   printk("BUSFREE ");
  if( s & SCSIPERR )  printk("SCSIPERR ");
  if( s & PHASECHG )  printk("PHASECHG ");
  if( s & REQINIT )   printk("REQINIT ");
  printk("); ");


  printk("SSTAT ( ");

  s=GETPORT(SSTAT0) & GETPORT(SIMODE0);

  if( s & TARGET )    printk("TARGET ");
  if( s & SELDO )     printk("SELDO ");
  if( s & SELDI )     printk("SELDI ");
  if( s & SELINGO )   printk("SELINGO ");
  if( s & SWRAP )     printk("SWRAP ");
  if( s & SDONE )     printk("SDONE ");
  if( s & SPIORDY )   printk("SPIORDY ");
  if( s & DMADONE )   printk("DMADONE ");

  s=GETPORT(SSTAT1) & GETPORT(SIMODE1);

  if( s & SELTO )     printk("SELTO ");
  if( s & ATNTARG )   printk("ATNTARG ");
  if( s & SCSIRSTI )  printk("SCSIRSTI ");
  if( s & PHASEMIS )  printk("PHASEMIS ");
  if( s & BUSFREE )   printk("BUSFREE ");
  if( s & SCSIPERR )  printk("SCSIPERR ");
  if( s & PHASECHG )  printk("PHASECHG ");
  if( s & REQINIT )   printk("REQINIT ");
  printk("); ");

  printk("SXFRCTL0 ( ");

  s=GETPORT(SXFRCTL0);
  if( s & SCSIEN )    printk("SCSIEN ");
  if( s & DMAEN )     printk("DMAEN ");
  if( s & CH1 )       printk("CH1 ");
  if( s & CLRSTCNT )  printk("CLRSTCNT ");
  if( s & SPIOEN )    printk("SPIOEN ");
  if( s & CLRCH1 )    printk("CLRCH1 ");
  printk("); ");

  printk("SIGNAL ( ");

  s=GETPORT(SCSISIG);
  if( s & ATNI )  printk("ATNI ");
  if( s & SELI )  printk("SELI ");
  if( s & BSYI )  printk("BSYI ");
  if( s & REQI )  printk("REQI ");
  if( s & ACKI )  printk("ACKI ");
  printk("); ");

  printk("SELID ( %02x ), ", GETPORT(SELID) );

  printk("SSTAT2 ( ");

  s=GETPORT(SSTAT2);
  if( s & SOFFSET)  printk("SOFFSET ");
  if( s & SEMPTY)   printk("SEMPTY ");
  if( s & SFULL)    printk("SFULL ");
  printk("); SFCNT ( %d ); ", s & (SFULL|SFCNT) );

#if 0
  printk("SSTAT4 ( ");
  s=GETPORT(SSTAT4);
  if( s & SYNCERR)   printk("SYNCERR ");
  if( s & FWERR)     printk("FWERR ");
  if( s & FRERR)     printk("FRERR ");
  printk("); ");
#endif

  printk("FCNT ( %d ); ", GETPORT(FIFOSTAT) );

  printk("DMACNTRL0 ( ");
  s=GETPORT(DMACNTRL0);
  printk( "%s ", s & _8BIT      ? "8BIT"  : "16BIT" );
  printk( "%s ", s & DMA        ? "DMA"   : "PIO"   );
  printk( "%s ", s & WRITE_READ ? "WRITE" : "READ"  );
  if( s & ENDMA )    printk("ENDMA ");
  if( s & INTEN )    printk("INTEN ");
  if( s & RSTFIFO )  printk("RSTFIFO ");
  if( s & SWINT )    printk("SWINT ");
  printk("); ");


#if 0
  printk("DMACNTRL1 ( ");

  s=GETPORT(DMACNTRL1);
  if( s & PWRDWN )    printk("PWRDN ");
  printk("); ");


  printk("STK ( %d ); ", s & 0xf);

  printk("DMASTAT (");
  s=GETPORT(DMASTAT);
  if( s & ATDONE )     printk("ATDONE ");
  if( s & WORDRDY )    printk("WORDRDY ");
  if( s & DFIFOFULL )  printk("DFIFOFULL ");
  if( s & DFIFOEMP )   printk("DFIFOEMP ");
  printk(")");

#endif

  printk("\n");
#endif
}

/*
 * display enabled interrupts
 */
static void disp_enintr(void)
{
  int s;

  printk("enabled interrupts ( ");
  
  s=GETPORT(SIMODE0);
  if( s & ENSELDO )    printk("ENSELDO ");
  if( s & ENSELDI )    printk("ENSELDI ");
  if( s & ENSELINGO )  printk("ENSELINGO ");
  if( s & ENSWRAP )    printk("ENSWRAP ");
  if( s & ENSDONE )    printk("ENSDONE ");
  if( s & ENSPIORDY )  printk("ENSPIORDY ");
  if( s & ENDMADONE )  printk("ENDMADONE ");

  s=GETPORT(SIMODE1);
  if( s & ENSELTIMO )    printk("ENSELTIMO ");
  if( s & ENATNTARG )    printk("ENATNTARG ");
  if( s & ENPHASEMIS )   printk("ENPHASEMIS ");
  if( s & ENBUSFREE )    printk("ENBUSFREE ");
  if( s & ENSCSIPERR )   printk("ENSCSIPERR ");
  if( s & ENPHASECHG )   printk("ENPHASECHG ");
  if( s & ENREQINIT )    printk("ENREQINIT ");
  printk(")\n");
}

#if defined(DEBUG_RACE)

static const char *should_leave;
static int in_driver=0;

/*
 * Only one routine can be in the driver at once.
 */
static void enter_driver(const char *func)
{
  cli();
  printk("aha152x: entering %s() (%x)\n", func, jiffies);
  if(in_driver)
    {
      printk("%s should leave first.\n", should_leave);
      panic("aha152x: already in driver\n");
    }

  in_driver++;
  should_leave=func;
  sti();
}

static void leave_driver(const char *func)
{
  cli();
  printk("\naha152x: leaving %s() (%x)\n", func, jiffies);
  if(!in_driver)
    {
      printk("aha152x: %s already left.\n", should_leave);
      panic("aha152x: %s already left driver.\n");
    }

  in_driver--;
  should_leave=func;
  sti();
}
#endif

/*
 * Show the command data of a command
 */
static void show_command(Scsi_Cmnd *ptr)
{
  printk("0x%08x: target=%d; lun=%d; cmnd=( ",
         (unsigned int) ptr, ptr->target, ptr->lun);
  
  print_command(ptr->cmnd);

  printk("); residual=%d; buffers=%d; phase |",
         ptr->SCp.this_residual, ptr->SCp.buffers_residual);

  if( ptr->SCp.phase & not_issued   )  printk("not issued|");
  if( ptr->SCp.phase & in_selection )  printk("in selection|");
  if( ptr->SCp.phase & disconnected )  printk("disconnected|");
  if( ptr->SCp.phase & aborted      )  printk("aborted|");
  if( ptr->SCp.phase & sent_ident   )  printk("send_ident|");
  if( ptr->SCp.phase & in_other )
    { 
      printk("; in other(");
      switch( (ptr->SCp.phase >> 16) & P_MASK )
        {
        case P_DATAO:
          printk("DATA OUT");
          break;
        case P_DATAI:
          printk("DATA IN");
          break;
        case P_CMD:
          printk("COMMAND");
          break;
        case P_STATUS:
          printk("STATUS");
          break;
        case P_MSGO:
          printk("MESSAGE OUT");
          break;
        case P_MSGI:
          printk("MESSAGE IN");
          break;
        default: 
          printk("*illegal*");
          break;
        }
      printk(")");
      if(ptr->SCp.phase & (1<<16))
        printk("; phaseend");
    }
  printk("; next=0x%08x\n", (unsigned int) ptr->host_scribble);
}
 
/*
 * Dump the queued data
 */
static void show_queues(void)
{
  Scsi_Cmnd *ptr;

  cli();
  printk("QUEUE STATUS:\nissue_SC:\n");
  for(ptr=issue_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
    show_command(ptr);

  printk("current_SC:\n");
  if(current_SC)
    show_command(current_SC);
  else
    printk("none\n");

  printk("disconnected_SC:\n");
  for(ptr=disconnected_SC; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble )
    show_command(ptr);

  disp_ports();
  disp_enintr();
  sti();
}