summaryrefslogtreecommitdiff
path: root/sys/dev/ic/adwlib.c
blob: d55eb20c8d59150e13fd37d6487ceabedb06303c (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
/*	$OpenBSD: adwlib.c,v 1.9 2000/06/29 00:04:31 krw Exp $ */
/* $NetBSD: adwlib.c,v 1.17 2000/05/27 18:24:50 dante Exp $        */

/*
 * Low level routines for the Advanced Systems Inc. SCSI controllers chips
 *
 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Author: Baldassare Dante Profeta <dante@mclink.it>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Ported from:
 */
/*
 * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
 * 
 * Copyright (c) 1995-2000 Advanced System Products, 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.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/queue.h>
#include <sys/device.h>

#include <machine/bus.h>
#include <machine/intr.h>

#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>

#include <dev/pci/pcidevs.h>

#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>

#include <dev/ic/adwlib.h>
#include <dev/ic/adwmcode.h>
#include <dev/ic/adw.h>


/* Static Functions */

int AdwRamSelfTest __P((bus_space_tag_t, bus_space_handle_t, u_int8_t));
int AdwLoadMCode __P((bus_space_tag_t, bus_space_handle_t, u_int16_t *,
								u_int8_t));
int AdwASC3550Cabling __P((bus_space_tag_t, bus_space_handle_t, ADW_DVC_CFG *));
int AdwASC38C0800Cabling __P((bus_space_tag_t, bus_space_handle_t,
								ADW_DVC_CFG *));
int AdwASC38C1600Cabling __P((bus_space_tag_t, bus_space_handle_t,
								ADW_DVC_CFG *));

static u_int16_t AdwGetEEPROMConfig __P((bus_space_tag_t, bus_space_handle_t,
     							ADW_EEPROM *));
static void AdwSetEEPROMConfig __P((bus_space_tag_t, bus_space_handle_t,
					                 ADW_EEPROM *));
static u_int16_t AdwReadEEPWord __P((bus_space_tag_t, bus_space_handle_t, int));
static void AdwWaitEEPCmd __P((bus_space_tag_t, bus_space_handle_t));

static void AdwInquiryHandling __P((ADW_SOFTC *, ADW_SCSI_REQ_Q *));

static void AdwSleepMilliSecond __P((u_int32_t));
static void AdwDelayMicroSecond __P((u_int32_t));


/*
 * EEPROM Configuration.
 *
 * All drivers should use this structure to set the default EEPROM
 * configuration. The BIOS now uses this structure when it is built.
 * Additional structure information can be found in adwlib.h where
 * the structure is defined.
 */
const static ADW_EEPROM adw_3550_Default_EEPROM = {
	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
	0x0000,			/* 01 cfg_msw */
	0xFFFF,			/* 02 disc_enable */
	0xFFFF,			/* 03 wdtr_able */
	{ 0xFFFF },		/* 04 sdtr_able */
	0xFFFF,			/* 05 start_motor */
	0xFFFF,			/* 06 tagqng_able */
	0xFFFF,			/* 07 bios_scan */
	0,			/* 08 scam_tolerant */
	7,			/* 09 adapter_scsi_id */
	0,			/*    bios_boot_delay */
	3,			/* 10 scsi_reset_delay */
	0,			/*    bios_id_lun */
	0,			/* 11 termination */
	0,			/*    reserved1 */
	0xFFE7,			/* 12 bios_ctrl */
	{ 0xFFFF },		/* 13 ultra_able */
	{ 0 },			/* 14 reserved2 */
	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
	0,			/* 16 dvc_cntl */
	{ 0 },			/* 17 bug_fix */
	{ 0,0,0 },		/* 18-20 serial_number[3] */
	0,			/* 21 check_sum */
	{			/* 22-29 oem_name[16] */
	  0,0,0,0,0,0,0,0,
	  0,0,0,0,0,0,0,0
	},
	0,			/* 30 dvc_err_code */
	0,			/* 31 adv_err_code */
	0,			/* 32 adv_err_addr */
	0,			/* 33 saved_dvc_err_code */
	0,			/* 34 saved_adv_err_code */
	0			/* 35 saved_adv_err_addr */
};

const static ADW_EEPROM adw_38C0800_Default_EEPROM = {
	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
	0x0000,			/* 01 cfg_msw */
	0xFFFF,			/* 02 disc_enable */
	0xFFFF,			/* 03 wdtr_able */
	{ 0x4444 },		/* 04 sdtr_speed1 */
	0xFFFF,			/* 05 start_motor */
	0xFFFF,			/* 06 tagqng_able */
	0xFFFF,			/* 07 bios_scan */
	0,			/* 08 scam_tolerant */
	7,			/* 09 adapter_scsi_id */
	0,			/*    bios_boot_delay */
	3,			/* 10 scsi_reset_delay */
	0,			/*    bios_id_lun */
	0,			/* 11 termination_se */
	0,			/*    termination_lvd */
	0xFFE7,			/* 12 bios_ctrl */
	{ 0x4444 },		/* 13 sdtr_speed2 */
	{ 0x4444 },		/* 14 sdtr_speed3 */
	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
	0,			/* 16 dvc_cntl */
	{ 0x4444 },		/* 17 sdtr_speed4 */
	{ 0,0,0 },		/* 18-20 serial_number[3] */
	0,			/* 21 check_sum */
	{			/* 22-29 oem_name[16] */
	  0,0,0,0,0,0,0,0,
	  0,0,0,0,0,0,0,0
	},
	0,			/* 30 dvc_err_code */
	0,			/* 31 adv_err_code */
	0,			/* 32 adv_err_addr */
	0,			/* 33 saved_dvc_err_code */
	0,			/* 34 saved_adv_err_code */
	0,			/* 35 saved_adv_err_addr */
	{			/* 36-55 reserved1[16] */
	  0,0,0,0,0,0,0,0,0,0,
	  0,0,0,0,0,0,0,0,0,0
	},
	0,			/* 56 cisptr_lsw */
	0,			/* 57 cisprt_msw */
	PCI_VENDOR_ADVSYS,	/* 58 subsysvid */
	PCI_PRODUCT_ADVSYS_U2W,	/* 59 subsysid */
	{ 0,0,0,0 }		/* 60-63 reserved2[4] */
};

const static ADW_EEPROM adw_38C1600_Default_EEPROM = {
	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
	0x0000,			/* 01 cfg_msw */
	0xFFFF,			/* 02 disc_enable */
	0xFFFF,			/* 03 wdtr_able */
	{ 0x5555 },		/* 04 sdtr_speed1 */
	0xFFFF,			/* 05 start_motor */
	0xFFFF,			/* 06 tagqng_able */
	0xFFFF,			/* 07 bios_scan */
	0,			/* 08 scam_tolerant */
	7,			/* 09 adapter_scsi_id */
	0,			/*    bios_boot_delay */
	3,			/* 10 scsi_reset_delay */
	0,			/*    bios_id_lun */
	0,			/* 11 termination_se */
	0,			/*    termination_lvd */
	0xFFE7,			/* 12 bios_ctrl */
	{ 0x5555 },		/* 13 sdtr_speed2 */
	{ 0x5555 },		/* 14 sdtr_speed3 */
	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
	0,			/* 16 dvc_cntl */
	{ 0x5555 },		/* 17 sdtr_speed4 */
	{ 0,0,0 },		/* 18-20 serial_number[3] */
	0,			/* 21 check_sum */
	{			/* 22-29 oem_name[16] */
	  0,0,0,0,0,0,0,0,
	  0,0,0,0,0,0,0,0
	},
	0,			/* 30 dvc_err_code */
	0,			/* 31 adv_err_code */
	0,			/* 32 adv_err_addr */
	0,			/* 33 saved_dvc_err_code */
	0,			/* 34 saved_adv_err_code */
	0,			/* 35 saved_adv_err_addr */
	{			/* 36-55 reserved1[16] */
	  0,0,0,0,0,0,0,0,0,0,
	  0,0,0,0,0,0,0,0,0,0
	},
	0,			/* 56 cisptr_lsw */
	0,			/* 57 cisprt_msw */
	PCI_VENDOR_ADVSYS,	/* 58 subsysvid */
	PCI_PRODUCT_ADVSYS_U3W, /* 59 subsysid */
	{ 0,0,0,0 }		/* 60-63 reserved2[4] */
};


/*
 * Read the board's EEPROM configuration. Set fields in ADW_SOFTC and
 * ADW_DVC_CFG based on the EEPROM settings. The chip is stopped while
 * all of this is done.
 *
 * For a non-fatal error return a warning code. If there are no warnings
 * then 0 is returned.
 *
 * Note: Chip is stopped on entry.
 */
int
AdwInitFromEEPROM(sc)
ADW_SOFTC      *sc;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	ADW_EEPROM		eep_config;
	u_int16_t		warn_code;
	u_int16_t		sdtr_speed = 0;
	u_int8_t		tid, termination;
	int			i, j;


	warn_code = 0;

	/*
	 * Read the board's EEPROM configuration.
	 *
	 * Set default values if a bad checksum is found.
	 *
	 * XXX - Don't handle big-endian access to EEPROM yet.
	 */
	if (AdwGetEEPROMConfig(iot, ioh, &eep_config) != eep_config.check_sum) {
		warn_code |= ADW_WARN_EEPROM_CHKSUM;

		/*
		 * Set EEPROM default values.
		 */
		switch(sc->chip_type) {
		case ADW_CHIP_ASC3550:
			eep_config = adw_3550_Default_EEPROM;
			break;
		case ADW_CHIP_ASC38C0800:
			eep_config = adw_38C0800_Default_EEPROM;
			break;
		case ADW_CHIP_ASC38C1600:
			eep_config = adw_38C1600_Default_EEPROM;

// XXX	  TODO!!!	if (ASC_PCI_ID2FUNC(sc->cfg.pci_slot_info) != 0) {
			if (sc->cfg.pci_slot_info != 0) {
				u_int8_t lsw_msb;

				lsw_msb = eep_config.cfg_lsw >> 8;
				/*
				 * Set Function 1 EEPROM Word 0 MSB
				 *
				 * Clear the BIOS_ENABLE (bit 14) and
				 * INTAB (bit 11) EEPROM bits.
				 *
				 * Disable Bit 14 (BIOS_ENABLE) to fix
				 * SPARC Ultra 60 and old Mac system booting
				 * problem. The Expansion ROM must
				 * be disabled in Function 1 for these systems.
				 */
				lsw_msb &= ~(((ADW_EEPROM_BIOS_ENABLE |
						ADW_EEPROM_INTAB) >> 8) & 0xFF);
				/*
				 * Set the INTAB (bit 11) if the GPIO 0 input
				 * indicates the Function 1 interrupt line is
				 * wired to INTA.
				 *
				 * Set/Clear Bit 11 (INTAB) from
				 * the GPIO bit 0 input:
				 *   1 - Function 1 intr line wired to INT A.
				 *   0 - Function 1 intr line wired to INT B.
				 *
				 * Note: Adapter boards always have Function 0
				 * wired to INTA.
				 * Put all 5 GPIO bits in input mode and then
				 * read their input values.
				 */
				ADW_WRITE_BYTE_REGISTER(iot, ioh,
							IOPB_GPIO_CNTL, 0);
				if (ADW_READ_BYTE_REGISTER(iot, ioh,
						IOPB_GPIO_DATA) & 0x01) {
					/*
					 * Function 1 interrupt wired to INTA;
					 * Set EEPROM bit.
					 */
					lsw_msb |= (ADW_EEPROM_INTAB >> 8)
							 & 0xFF;
				 }
				 eep_config.cfg_lsw &= 0x00FF;
				 eep_config.cfg_lsw |= lsw_msb << 8;
			}
			break;
		}

		/*
		 * Assume the 6 byte board serial number that was read
		 * from EEPROM is correct even if the EEPROM checksum
		 * failed.
		 */
		for (i=2, j=1; i>=0; i--, j++) {
		eep_config.serial_number[i] =
			AdwReadEEPWord(iot, ioh, ASC_EEP_DVC_CFG_END - j);
		}

		AdwSetEEPROMConfig(iot, ioh, &eep_config);
	}
	/*
	 * Set sc and sc->cfg variables from the EEPROM configuration
	 * that was read.
	 *
	 * This is the mapping of EEPROM fields to Adw Library fields.
	 */
	sc->wdtr_able = eep_config.wdtr_able;
	if (sc->chip_type == ADW_CHIP_ASC3550) {
		sc->sdtr_able = eep_config.sdtr1.sdtr_able;
		sc->ultra_able = eep_config.sdtr2.ultra_able;
	} else {
		sc->sdtr_speed1 = eep_config.sdtr1.sdtr_speed1;
		sc->sdtr_speed2 = eep_config.sdtr2.sdtr_speed2;
		sc->sdtr_speed3 = eep_config.sdtr3.sdtr_speed3;
		sc->sdtr_speed4 = eep_config.sdtr4.sdtr_speed4;
	}
	sc->ppr_able = 0;
	sc->tagqng_able = eep_config.tagqng_able;
	sc->cfg.disc_enable = eep_config.disc_enable;
	sc->max_host_qng = eep_config.max_host_qng;
	sc->max_dvc_qng = eep_config.max_dvc_qng;
	sc->chip_scsi_id = (eep_config.adapter_scsi_id & ADW_MAX_TID);
	sc->start_motor = eep_config.start_motor;
	sc->scsi_reset_wait = eep_config.scsi_reset_delay;
	sc->bios_ctrl = eep_config.bios_ctrl;
	sc->no_scam = eep_config.scam_tolerant;
	sc->cfg.serial1 = eep_config.serial_number[0];
	sc->cfg.serial2 = eep_config.serial_number[1];
	sc->cfg.serial3 = eep_config.serial_number[2];

	if (sc->chip_type == ADW_CHIP_ASC38C0800 ||
	    sc->chip_type == ADW_CHIP_ASC38C1600) {
		sc->sdtr_able = 0;
		for (tid = 0; tid <= ADW_MAX_TID; tid++) {
			if (tid == 0) {
				sdtr_speed = sc->sdtr_speed1;
			} else if (tid == 4) {
				sdtr_speed = sc->sdtr_speed2;
			} else if (tid == 8) {
				sdtr_speed = sc->sdtr_speed3;
			} else if (tid == 12) {
				sdtr_speed = sc->sdtr_speed4;
			}
			if (sdtr_speed & ADW_MAX_TID) {
				sc->sdtr_able |= (1 << tid);
			}
			sdtr_speed >>= 4;
		}
	}

	/*
	 * Set the host maximum queuing (max. 253, min. 16) and the per device
	 * maximum queuing (max. 63, min. 4).
	 */
	if (eep_config.max_host_qng > ADW_DEF_MAX_HOST_QNG) {
		eep_config.max_host_qng = ADW_DEF_MAX_HOST_QNG;
	} else if (eep_config.max_host_qng < ADW_DEF_MIN_HOST_QNG)
	{
		/* If the value is zero, assume it is uninitialized. */
		if (eep_config.max_host_qng == 0) {
			eep_config.max_host_qng = ADW_DEF_MAX_HOST_QNG;
		} else {
			eep_config.max_host_qng = ADW_DEF_MIN_HOST_QNG;
		}
	}

	if (eep_config.max_dvc_qng > ADW_DEF_MAX_DVC_QNG) {
		eep_config.max_dvc_qng = ADW_DEF_MAX_DVC_QNG;
	} else if (eep_config.max_dvc_qng < ADW_DEF_MIN_DVC_QNG) {
		/* If the value is zero, assume it is uninitialized. */
		if (eep_config.max_dvc_qng == 0) {
			eep_config.max_dvc_qng = ADW_DEF_MAX_DVC_QNG;
		} else {
			eep_config.max_dvc_qng = ADW_DEF_MIN_DVC_QNG;
		}
	}

	/*
	 * If 'max_dvc_qng' is greater than 'max_host_qng', then
	 * set 'max_dvc_qng' to 'max_host_qng'.
	 */
	if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
		eep_config.max_dvc_qng = eep_config.max_host_qng;
	}

	/*
	 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
	 * values based on possibly adjusted EEPROM values.
	 */
	sc->max_host_qng = eep_config.max_host_qng;
	sc->max_dvc_qng = eep_config.max_dvc_qng;


	/*
	 * If the EEPROM 'termination' field is set to automatic (0), then set
	 * the ADV_DVC_CFG 'termination' field to automatic also.
	 *
	 * If the termination is specified with a non-zero 'termination'
	 * value check that a legal value is set and set the ADV_DVC_CFG
	 * 'termination' field appropriately.
	 */

	switch(sc->chip_type) {
	case ADW_CHIP_ASC3550:
		sc->cfg.termination = 0;	/* auto termination */
		switch(eep_config.termination_se) {
		case 3:
			/* Enable manual control with low on / high on. */
			sc->cfg.termination |= ADW_TERM_CTL_L;
		case 2:
			/* Enable manual control with low off / high on. */
			sc->cfg.termination |= ADW_TERM_CTL_H;
		case 1:
			/* Enable manual control with low off / high off. */
			sc->cfg.termination |= ADW_TERM_CTL_SEL;
		case 0:
			break;
		default:
			warn_code |= ADW_WARN_EEPROM_TERMINATION;
		}
		break;

	case ADW_CHIP_ASC38C0800:
	case ADW_CHIP_ASC38C1600:
		switch(eep_config.termination_se) {
		case 0:
			/* auto termination for SE */
			termination = 0;
			break;
		case 1:
			/* Enable manual control with low off / high off. */
			termination = 0;
			break;
		case 2:
			/* Enable manual control with low off / high on. */
			termination = ADW_TERM_SE_HI;
			break;
		case 3:
			/* Enable manual control with low on / high on. */
			termination = ADW_TERM_SE;
			break;
		default:
			/*
			 * The EEPROM 'termination_se' field contains a
			 * bad value. Use automatic termination instead.
			 */
			termination = 0;
			warn_code |= ADW_WARN_EEPROM_TERMINATION;
		}

		switch(eep_config.termination_lvd) {
		case 0:
			/* auto termination for LVD */
			sc->cfg.termination = termination;
			break;
		case 1:
			/* Enable manual control with low off / high off. */
			sc->cfg.termination = termination;
			break;
		case 2:
			/* Enable manual control with low off / high on. */
			sc->cfg.termination = termination | ADW_TERM_LVD_HI;
			break;
		case 3:
			/* Enable manual control with low on / high on. */
			sc->cfg.termination = termination | ADW_TERM_LVD;
			break;
		default:
			/*
			 * The EEPROM 'termination_lvd' field contains a
			 * bad value. Use automatic termination instead.
			 */
			sc->cfg.termination = termination;
			warn_code |= ADW_WARN_EEPROM_TERMINATION;
		}
		break;
	}

	return warn_code;
}


/*
 * Initialize the ASC-3550/ASC-38C0800/ASC-38C1600.
 *
 * On failure return the error code.
 */
int
AdwInitDriver(sc)
ADW_SOFTC      *sc;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	u_int16_t	error_code;
	int		word;
	int		i;
	u_int16_t	bios_mem[ADW_MC_BIOSLEN/2];	/* BIOS RISC Memory
								0x40-0x8F. */
	u_int16_t	wdtr_able = 0, sdtr_able, ppr_able, tagqng_able;
	u_int8_t	max_cmd[ADW_MAX_TID + 1];
	u_int8_t	tid;


	error_code = 0;

	/*
	 * Save the RISC memory BIOS region before writing the microcode.
	 * The BIOS may already be loaded and using its RISC LRAM region
	 * so its region must be saved and restored.
	 *
	 * Note: This code makes the assumption, which is currently true,
	 * that a chip reset does not clear RISC LRAM.
	 */
	for (i = 0; i < ADW_MC_BIOSLEN/2; i++) {
		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_BIOSMEM+(2*i), bios_mem[i]);
	}

	/*
	 * Save current per TID negotiated values.
	 */
	switch (sc->chip_type) {
	case ADW_CHIP_ASC3550:
		if (bios_mem[(ADW_MC_BIOS_SIGNATURE-ADW_MC_BIOSMEM)/2]==0x55AA){

			u_int16_t  bios_version, major, minor;

			bios_version = bios_mem[(ADW_MC_BIOS_VERSION -
					ADW_MC_BIOSMEM) / 2];
			major = (bios_version  >> 12) & 0xF;
			minor = (bios_version  >> 8) & 0xF;
			if (major < 3 || (major == 3 && minor == 1)) {
			    /*
			     * BIOS 3.1 and earlier location of
			     * 'wdtr_able' variable.
			     */
			    ADW_READ_WORD_LRAM(iot, ioh, 0x120, wdtr_able);
			} else {
			    ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
					    wdtr_able);
			}
		}
		break;

	case ADW_CHIP_ASC38C1600:
		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
		/* FALLTHROUGH */
	case ADW_CHIP_ASC38C0800:
		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
		break;
	}
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
			max_cmd[tid]);
	}

	/*
	 * Perform a RAM Built-In Self Test
	 */
	if((error_code = AdwRamSelfTest(iot, ioh, sc->chip_type))) {
		return error_code;
	}

	/*
	 * Load the Microcode
	 */
	;
	if((error_code = AdwLoadMCode(iot, ioh, bios_mem, sc->chip_type))) {
		return error_code;
	}

	/*
	 * Read microcode version and date.
	 */
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_VERSION_DATE, sc->cfg.mcode_date);
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_VERSION_NUM, sc->cfg.mcode_version);

	/*
	 * If the PCI Configuration Command Register "Parity Error Response
	 * Control" Bit was clear (0), then set the microcode variable
	 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
	 * to ignore DMA parity errors.
	 */
	if (sc->cfg.control_flag & CONTROL_FLAG_IGNORE_PERR) {
		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG, word);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG,
					word | CONTROL_FLAG_IGNORE_PERR);
	}

	switch (sc->chip_type) {
	case ADW_CHIP_ASC3550:
		/*
		 * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a
		 * FIFO threshold of 128 bytes.
		 * This register is only accessible to the host.
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
				START_CTL_EMFU | READ_CMD_MRM);
		break;

	case ADW_CHIP_ASC38C0800:
		/*
		 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
		 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
		 * cable detection and then we are able to read C_DET[3:0].
		 *
		 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
		 * Microcode Default Value' section below.
		 */
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1,
				ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1)
				| ADW_DIS_TERM_DRV);

		/*
		 * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and
		 * START_CTL_TH [3:2] bits for the default FIFO threshold.
		 *
		 * Note: ASC-38C0800 FIFO threshold has been changed to
		 * 256 bytes.
		 *
		 * For DMA Errata #4 set the BC_THRESH_ENB bit.
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
						BC_THRESH_ENB | FIFO_THRESH_80B
						| START_CTL_TH | READ_CMD_MRM);
		break;

	case ADW_CHIP_ASC38C1600:
		/*
		 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
		 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
		 * cable detection and then we are able to read C_DET[3:0].
		 *
		 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
		 * Microcode Default Value' section below.
		 */
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1,
				ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1)
				| ADW_DIS_TERM_DRV);

		/*
		 * If the BIOS control flag AIPP (Asynchronous Information
		 * Phase Protection) disable bit is not set, then set the
		 * firmware 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to
		 * enable AIPP checking and encoding.
		 */
		if ((sc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG, word);
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG,
					word | CONTROL_FLAG_ENABLE_AIPP);
		}

		/*
		 * For ASC-38C1600 use DMA_CFG0 default values:
		 * FIFO_THRESH_80B [6:4], and START_CTL_TH [3:2].
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
				FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
		break;
	}

	/*
	 * Microcode operating variables for WDTR, SDTR, and command tag
	 * queuing will be set in AdvInquiryHandling() based on what a
	 * device reports it is capable of in Inquiry byte 7.
	 *
	 * If SCSI Bus Resets have been disabled, then directly set
	 * SDTR and WDTR from the EEPROM configuration. This will allow
	 * the BIOS and warm boot to work without a SCSI bus hang on
	 * the Inquiry caused by host and target mismatched DTR values.
	 * Without the SCSI Bus Reset, before an Inquiry a device can't
	 * be assumed to be in Asynchronous, Narrow mode.
	 */
	if ((sc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, sc->wdtr_able);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sc->sdtr_able);
	}

	/*
	 * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
	 * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
	 * bitmask. These values determine the maximum SDTR speed negotiated
	 * with a device.
	 *
	 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
	 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
	 * without determining here whether the device supports SDTR.
	 */
	switch (sc->chip_type) {
	case ADW_CHIP_ASC3550:
		word = 0;
		for (tid = 0; tid <= ADW_MAX_TID; tid++) {
			if (ADW_TID_TO_TIDMASK(tid) & sc->ultra_able) {
				/* Set Ultra speed for TID 'tid'. */
				word |= (0x3 << (4 * (tid % 4)));
			} else {
				/* Set Fast speed for TID 'tid'. */
				word |= (0x2 << (4 * (tid % 4)));
			}
			/* Check if done with sdtr_speed1. */
			if (tid == 3) {
				ADW_WRITE_WORD_LRAM(iot, ioh,
						ADW_MC_SDTR_SPEED1, word);
				word = 0;
			/* Check if done with sdtr_speed2. */
			} else if (tid == 7) {
				ADW_WRITE_WORD_LRAM(iot, ioh,
						ADW_MC_SDTR_SPEED2, word);
				word = 0;
			/* Check if done with sdtr_speed3. */
			} else if (tid == 11) {
				ADW_WRITE_WORD_LRAM(iot, ioh,
						ADW_MC_SDTR_SPEED3, word);
				word = 0;
			/* Check if done with sdtr_speed4. */
			} else if (tid == 15) {
				ADW_WRITE_WORD_LRAM(iot, ioh,
						ADW_MC_SDTR_SPEED4, word);
				/* End of loop. */
			}
		}

		/*
		 * Set microcode operating variable for the
		 * disconnect per TID bitmask.
		 */
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DISC_ENABLE,
							sc->cfg.disc_enable);
		break;

	case ADW_CHIP_ASC38C0800:
		/* FALLTHROUGH */
	case ADW_CHIP_ASC38C1600:
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DISC_ENABLE,
							sc->cfg.disc_enable);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED1,
							sc->sdtr_speed1);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED2,
							sc->sdtr_speed2);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED3,
							sc->sdtr_speed3);
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED4,
							sc->sdtr_speed4);
		break;
	}


	/*
	 * Set SCSI_CFG0 Microcode Default Value.
	 *
	 * The microcode will set the SCSI_CFG0 register using this value
	 * after it is started below.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG0,
		ADW_PARITY_EN | ADW_QUEUE_128 | ADW_SEL_TMO_LONG |
		ADW_OUR_ID_EN | sc->chip_scsi_id);


	switch(sc->chip_type) {
	case ADW_CHIP_ASC3550:
		error_code = AdwASC3550Cabling(iot, ioh, &sc->cfg);
		break;

	case ADW_CHIP_ASC38C0800:
		error_code = AdwASC38C0800Cabling(iot, ioh, &sc->cfg);
		break;

	case ADW_CHIP_ASC38C1600:
		error_code = AdwASC38C1600Cabling(iot, ioh, &sc->cfg);
		break;
	}
	if(error_code) {
		return error_code;
	}

	/*
	 * Set SEL_MASK Microcode Default Value
	 *
	 * The microcode will set the SEL_MASK register using this value
	 * after it is started below.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SEL_MASK,
		ADW_TID_TO_TIDMASK(sc->chip_scsi_id));

	/*
	 * Create and Initialize Host->RISC Carrier lists
	 */
	sc->carr_freelist = AdwInitCarriers(sc->sc_dmamap_carrier,
						sc->sc_control->carriers);

	/*
	 * Set-up the Host->RISC Initiator Command Queue (ICQ).
	 */

	if ((sc->icq_sp = sc->carr_freelist) == NULL) {
		return ADW_IERR_NO_CARRIER;
	}
	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
			ASC_GET_CARRP(sc->icq_sp->next_ba));

	/*
	 * The first command issued will be placed in the stopper carrier.
	 */
	sc->icq_sp->next_ba = ASC_CQ_STOPPER;

	/*
	 * Set RISC ICQ physical address start value.
	 */
	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_ICQ, sc->icq_sp->carr_ba);

	/*
	 * Initialize the COMMA register to the same value otherwise
	 * the RISC will prematurely detect a command is available.
	 */
	if(sc->chip_type == ADW_CHIP_ASC38C1600) {
		ADW_WRITE_DWORD_REGISTER(iot, ioh, IOPDW_COMMA,
							sc->icq_sp->carr_ba);
	}

	/*
	 * Set-up the RISC->Host Initiator Response Queue (IRQ).
	 */
	if ((sc->irq_sp = sc->carr_freelist) == NULL) {
		return ADW_IERR_NO_CARRIER;
	}
	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
			ASC_GET_CARRP(sc->irq_sp->next_ba));

	/*
	 * The first command completed by the RISC will be placed in
	 * the stopper.
	 *
	 * Note: Set 'next_ba' to ASC_CQ_STOPPER. When the request is
	 * completed the RISC will set the ASC_RQ_DONE bit.
	 */
	sc->irq_sp->next_ba = ASC_CQ_STOPPER;

	/*
	 * Set RISC IRQ physical address start value.
	 */
	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_IRQ, sc->irq_sp->carr_ba);
	sc->carr_pending_cnt = 0;

	ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_INTR_ENABLES,
		(ADW_INTR_ENABLE_HOST_INTR | ADW_INTR_ENABLE_GLOBAL_INTR));
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_BEGIN_ADDR, word);
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_PC, word);

	/* finally, finally, gentlemen, start your engine */
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RISC_CSR, ADW_RISC_CSR_RUN);

	/*
	 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
	 * Resets should be performed. The RISC has to be running
	 * to issue a SCSI Bus Reset.
	 */
	if (sc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS)
	{
		/*
		 * If the BIOS Signature is present in memory, restore the
		 * BIOS Handshake Configuration Table and do not perform
		 * a SCSI Bus Reset.
		 */
		if (bios_mem[(ADW_MC_BIOS_SIGNATURE - ADW_MC_BIOSMEM)/2] ==
				0x55AA) {
			/*
			 * Restore per TID negotiated values.
			 */
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
					wdtr_able);
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,
					sdtr_able);
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
					tagqng_able);
			for (tid = 0; tid <= ADW_MAX_TID; tid++) {
				ADW_WRITE_BYTE_LRAM(iot, ioh,
						ADW_MC_NUMBER_OF_MAX_CMD + tid,
						max_cmd[tid]);
			}
		} else {
			if (AdwResetCCB(sc) != ADW_TRUE) {
				error_code = ADW_WARN_BUSRESET_ERROR;
			}
		}
	}

	return error_code;
}


int
AdwRamSelfTest(iot, ioh, chip_type)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	u_int8_t chip_type;
{
	int		i;
	u_int8_t	byte;


	if ((chip_type == ADW_CHIP_ASC38C0800) ||
	    (chip_type == ADW_CHIP_ASC38C1600)) {
		/*
		 * RAM BIST (RAM Built-In Self Test)
		 *
		 * Address : I/O base + offset 0x38h register (byte).
		 * Function: Bit 7-6(RW) : RAM mode
		 *			    Normal Mode   : 0x00
		 *			    Pre-test Mode : 0x40
		 *			    RAM Test Mode : 0x80
		 *	     Bit 5	 : unused
		 *	     Bit 4(RO)   : Done bit
		 *	     Bit 3-0(RO) : Status
		 *			    Host Error    : 0x08
		 *			    Int_RAM Error : 0x04
		 *			    RISC Error    : 0x02
		 *			    SCSI Error    : 0x01
		 *			    No Error	  : 0x00
		 *
		 * Note: RAM BIST code should be put right here, before loading
		 * the microcode and after saving the RISC memory BIOS region.
		 */

		/*
		 * LRAM Pre-test
		 *
		 * Write PRE_TEST_MODE (0x40) to register and wait for
		 * 10 milliseconds.
		 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05),
		 * return an error. Reset to NORMAL_MODE (0x00) and do again.
		 * If cannot reset to NORMAL_MODE, return an error too.
		 */
		for (i = 0; i < 2; i++) {
			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST,
					PRE_TEST_MODE);
			 /* Wait for 10ms before reading back. */
			AdwSleepMilliSecond(10);
			byte = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST);
			if ((byte & RAM_TEST_DONE) == 0 || (byte & 0x0F) !=
					PRE_TEST_VALUE) {
				return ADW_IERR_BIST_PRE_TEST;
			}

			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST,
								NORMAL_MODE);
			/* Wait for 10ms before reading back. */
			AdwSleepMilliSecond(10);
			if (ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST)
			    != NORMAL_VALUE) {
				return ADW_IERR_BIST_PRE_TEST;
			}
		}

		/*
		 * LRAM Test - It takes about 1.5 ms to run through the test.
		 *
		 * Write RAM_TEST_MODE (0x80) to register and wait for
		 * 10 milliseconds.
		 * If Done bit not set or Status not 0, save register byte,
		 * set the err_code, and return an error.
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST, RAM_TEST_MODE);
		/* Wait for 10ms before checking status. */
		AdwSleepMilliSecond(10);

		byte = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST);
		if ((byte & RAM_TEST_DONE)==0 || (byte & RAM_TEST_STATUS)!=0) {
			/* Get here if Done bit not set or Status not 0. */
			return ADW_IERR_BIST_RAM_TEST;
		}

		/* We need to reset back to normal mode after LRAM test passes*/
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST, NORMAL_MODE);
	}

	return 0;
}


int
AdwLoadMCode(iot, ioh, bios_mem, chip_type)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	u_int16_t *bios_mem;
	u_int8_t chip_type;
{
	u_int8_t	*mcode_data;
	u_int32_t	 mcode_chksum;
	u_int16_t	 mcode_size;
	u_int32_t	sum;
	u_int16_t	code_sum;
	int		begin_addr;
	int		end_addr;
	int		word;
	int		adw_memsize;
	int		adw_mcode_expanded_size;
	int		i, j;


	switch(chip_type) {
	case ADW_CHIP_ASC3550:
		mcode_data = (u_int8_t *)adw_asc3550_mcode_data.mcode_data;
		mcode_chksum = (u_int32_t)adw_asc3550_mcode_data.mcode_chksum;
		mcode_size = (u_int16_t)adw_asc3550_mcode_data.mcode_size;
		adw_memsize = ADW_3550_MEMSIZE;
		break;

	case ADW_CHIP_ASC38C0800:
		mcode_data = (u_int8_t *)adw_asc38C0800_mcode_data.mcode_data;
		mcode_chksum =(u_int32_t)adw_asc38C0800_mcode_data.mcode_chksum;
		mcode_size = (u_int16_t)adw_asc38C0800_mcode_data.mcode_size;
		adw_memsize = ADW_38C0800_MEMSIZE;
		break;

	case ADW_CHIP_ASC38C1600:
		mcode_data = (u_int8_t *)adw_asc38C1600_mcode_data.mcode_data;
		mcode_chksum =(u_int32_t)adw_asc38C1600_mcode_data.mcode_chksum;
		mcode_size = (u_int16_t)adw_asc38C1600_mcode_data.mcode_size;
		adw_memsize = ADW_38C1600_MEMSIZE;
		break;
	}

	/*
	 * Write the microcode image to RISC memory starting at address 0.
	 */
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, 0);

	/* Assume the following compressed format of the microcode buffer:
	 *
	 *  254 word (508 byte) table indexed by byte code followed
	 *  by the following byte codes:
	 *
	 *    1-Byte Code:
	 *	00: Emit word 0 in table.
	 *	01: Emit word 1 in table.
	 *	.
	 *	FD: Emit word 253 in table.
	 *
	 *    Multi-Byte Code:
	 *	FE WW WW: (3 byte code) Word to emit is the next word WW WW.
	 *	FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
	 */
	word = 0;
	for (i = 253 * 2; i < mcode_size; i++) {
		if (mcode_data[i] == 0xff) {
			for (j = 0; j < mcode_data[i + 1]; j++) {
				ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh,
				  (((u_int16_t)mcode_data[i + 3] << 8) |
				  mcode_data[i + 2]));
				word++;
			}
			i += 3;
		} else if (mcode_data[i] == 0xfe) {
			ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh,
			    (((u_int16_t)mcode_data[i + 2] << 8) |
			    mcode_data[i + 1]));
			i += 2;
			word++;
		} else {
			ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh, (((u_int16_t)
			 mcode_data[(mcode_data[i] * 2) + 1] <<8) |
			 mcode_data[mcode_data[i] * 2]));
			word++;
		}
	}

	/*
	 * Set 'word' for later use to clear the rest of memory and save
	 * the expanded mcode size.
	 */
	word *= 2;
	adw_mcode_expanded_size = word;

	/*
	 * Clear the rest of the Internal RAM.
	 */
	for (; word < adw_memsize; word += 2) {
		ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh, 0);
	}

	/*
	 * Verify the microcode checksum.
	 */
	sum = 0;
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, 0);

	for (word = 0; word < adw_mcode_expanded_size; word += 2) {
		sum += ADW_READ_WORD_AUTO_INC_LRAM(iot, ioh);
	}

	if (sum != mcode_chksum) {
		return ADW_IERR_MCODE_CHKSUM;
	}

	/*
	 * Restore the RISC memory BIOS region.
	 */
	for (i = 0; i < ADW_MC_BIOSLEN/2; i++) {
		if(chip_type == ADW_CHIP_ASC3550) {
			ADW_WRITE_BYTE_LRAM(iot, ioh, ADW_MC_BIOSMEM + (2 * i),
								bios_mem[i]);
		} else {
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOSMEM + (2 * i),
								bios_mem[i]);
		}
	}

	/*
	 * Calculate and write the microcode code checksum to the microcode
	 * code checksum location ADW_MC_CODE_CHK_SUM (0x2C).
	 */
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_BEGIN_ADDR, begin_addr);
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_END_ADDR, end_addr);
	code_sum = 0;
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, begin_addr);
	for (word = begin_addr; word < end_addr; word += 2) {
		code_sum += ADW_READ_WORD_AUTO_INC_LRAM(iot, ioh);
	}
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CODE_CHK_SUM, code_sum);

	/*
	 * Set the chip type.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CHIP_TYPE, chip_type);

	return 0;
}


int
AdwASC3550Cabling(iot, ioh, cfg)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	ADW_DVC_CFG *cfg;
{
	u_int16_t	scsi_cfg1;


	/*
	 * Determine SCSI_CFG1 Microcode Default Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 */

	/* Read current SCSI_CFG1 Register value. */
	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);

	/*
	 * If all three connectors are in use in ASC3550, return an error.
	 */
	if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
	     (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
		return ADW_IERR_ILLEGAL_CONNECTION;
	}

	/*
	 * If the cable is reversed all of the SCSI_CTRL register signals
	 * will be set. Check for and return an error if this condition is
	 * found.
	 */
	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
		return ADW_IERR_REVERSED_CABLE;
	}

	/*
	 * If this is a differential board and a single-ended device
	 * is attached to one of the connectors, return an error.
	 */
	if ((scsi_cfg1 & ADW_DIFF_MODE) &&
	    (scsi_cfg1 & ADW_DIFF_SENSE) == 0) {
		return ADW_IERR_SINGLE_END_DEVICE;
	}

	/*
	 * If automatic termination control is enabled, then set the
	 * termination value based on a table listed in a_condor.h.
	 *
	 * If manual termination was specified with an EEPROM setting
	 * then 'termination' was set-up in AdwInitFromEEPROM() and
	 * is ready to be 'ored' into SCSI_CFG1.
	 */
	if (cfg->termination == 0) {
		/*
		 * The software always controls termination by setting
		 * TERM_CTL_SEL.
		 * If TERM_CTL_SEL were set to 0, the hardware would set
		 * termination.
		 */
		cfg->termination |= ADW_TERM_CTL_SEL;

		switch(scsi_cfg1 & ADW_CABLE_DETECT) {
			/* TERM_CTL_H: on, TERM_CTL_L: on */
			case 0x3: case 0x7: case 0xB:
			case 0xD: case 0xE: case 0xF:
				cfg->termination |=
				(ADW_TERM_CTL_H | ADW_TERM_CTL_L);
				break;

			/* TERM_CTL_H: on, TERM_CTL_L: off */
			case 0x1: case 0x5: case 0x9:
			case 0xA: case 0xC:
				cfg->termination |= ADW_TERM_CTL_H;
				break;

			/* TERM_CTL_H: off, TERM_CTL_L: off */
			case 0x2: case 0x6:
				break;
		}
	}

	/*
	 * Clear any set TERM_CTL_H and TERM_CTL_L bits.
	 */
	scsi_cfg1 &= ~ADW_TERM_CTL;

	/*
	 * Invert the TERM_CTL_H and TERM_CTL_L bits and then
	 * set 'scsi_cfg1'. The TERM_POL bit does not need to be
	 * referenced, because the hardware internally inverts
	 * the Termination High and Low bits if TERM_POL is set.
	 */
	scsi_cfg1 |= (ADW_TERM_CTL_SEL | (~cfg->termination & ADW_TERM_CTL));

	/*
	 * Set SCSI_CFG1 Microcode Default Value
	 *
	 * Set filter value and possibly modified termination control
	 * bits in the Microcode SCSI_CFG1 Register Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1,
						ADW_FLTR_DISABLE | scsi_cfg1);

	/*
	 * Set MEM_CFG Microcode Default Value
	 *
	 * The microcode will set the MEM_CFG register using this value
	 * after it is started below.
	 *
	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
	 * are defined.
	 *
	 * ASC-3550 has 8KB internal memory.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
						ADW_BIOS_EN | ADW_RAM_SZ_8KB);

	return 0;
}


int
AdwASC38C0800Cabling(iot, ioh, cfg)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	ADW_DVC_CFG *cfg;
{
	u_int16_t	scsi_cfg1;


	/*
	 * Determine SCSI_CFG1 Microcode Default Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 */

	/* Read current SCSI_CFG1 Register value. */
	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);

	/*
	 * If the cable is reversed all of the SCSI_CTRL register signals
	 * will be set. Check for and return an error if this condition is
	 * found.
	 */
	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
		return ADW_IERR_REVERSED_CABLE;
	}

	/*
	 * All kind of combinations of devices attached to one of four
	 * connectors are acceptable except HVD device attached.
	 * For example, LVD device can be attached to SE connector while
	 * SE device attached to LVD connector.
	 * If LVD device attached to SE connector, it only runs up to
	 * Ultra speed.
	 *
	 * If an HVD device is attached to one of LVD connectors, return
	 * an error.
	 * However, there is no way to detect HVD device attached to
	 * SE connectors.
	 */
	if (scsi_cfg1 & ADW_HVD) {
		return ADW_IERR_HVD_DEVICE;
	}

	/*
	 * If either SE or LVD automatic termination control is enabled, then
	 * set the termination value based on a table listed in a_condor.h.
	 *
	 * If manual termination was specified with an EEPROM setting then
	 * 'termination' was set-up in AdwInitFromEEPROM() and is ready
	 * to be 'ored' into SCSI_CFG1.
	 */
	if ((cfg->termination & ADW_TERM_SE) == 0) {
		/* SE automatic termination control is enabled. */
		switch(scsi_cfg1 & ADW_C_DET_SE) {
			/* TERM_SE_HI: on, TERM_SE_LO: on */
			case 0x1: case 0x2: case 0x3:
				cfg->termination |= ADW_TERM_SE;
				break;

			/* TERM_SE_HI: on, TERM_SE_LO: off */
			case 0x0:
				cfg->termination |= ADW_TERM_SE_HI;
				break;
		}
	}

	if ((cfg->termination & ADW_TERM_LVD) == 0) {
		/* LVD automatic termination control is enabled. */
		switch(scsi_cfg1 & ADW_C_DET_LVD) {
			/* TERM_LVD_HI: on, TERM_LVD_LO: on */
			case 0x4: case 0x8: case 0xC:
				cfg->termination |= ADW_TERM_LVD;
				break;

			/* TERM_LVD_HI: off, TERM_LVD_LO: off */
			case 0x0:
				break;
		}
	}

	/*
	 * Clear any set TERM_SE and TERM_LVD bits.
	 */
	scsi_cfg1 &= (~ADW_TERM_SE & ~ADW_TERM_LVD);

	/*
	 * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
	 */
	scsi_cfg1 |= (~cfg->termination & 0xF0);

	/*
	 * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and
	 * HVD/LVD/SE bits and set possibly modified termination control bits
	 * in the Microcode SCSI_CFG1 Register Value.
	 */
	scsi_cfg1 &= (~ADW_BIG_ENDIAN & ~ADW_DIS_TERM_DRV &
					~ADW_TERM_POL & ~ADW_HVD_LVD_SE);

	/*
	 * Set SCSI_CFG1 Microcode Default Value
	 *
	 * Set possibly modified termination control and reset DIS_TERM_DRV
	 * bits in the Microcode SCSI_CFG1 Register Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);

	/*
	 * Set MEM_CFG Microcode Default Value
	 *
	 * The microcode will set the MEM_CFG register using this value
	 * after it is started below.
	 *
	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
	 * are defined.
	 *
	 * ASC-38C0800 has 16KB internal memory.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
						ADW_BIOS_EN | ADW_RAM_SZ_16KB);

	return 0;
}


int
AdwASC38C1600Cabling(iot, ioh, cfg)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	ADW_DVC_CFG *cfg;
{
	u_int16_t	scsi_cfg1;


	/*
	 * Determine SCSI_CFG1 Microcode Default Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 * Each ASC-38C1600 function has only two cable detect bits.
	 * The bus mode override bits are in IOPB_SOFT_OVER_WR.
	 */

	/* Read current SCSI_CFG1 Register value. */
	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);

	/*
	 * If the cable is reversed all of the SCSI_CTRL register signals
	 * will be set. Check for and return an error if this condition is
	 * found.
	 */
	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
		return ADW_IERR_REVERSED_CABLE;
	}

	/*
	 * Each ASC-38C1600 function has two connectors. Only an HVD device
	 * can not be connected to either connector. An LVD device or SE device
	 * may be connected to either connecor. If an SE device is connected,
	 * then at most Ultra speed (20 Mhz) can be used on both connectors.
	 *
	 * If an HVD device is attached, return an error.
	 */
	if (scsi_cfg1 & ADW_HVD) {
		return ADW_IERR_HVD_DEVICE;
	}

	/*
	 * Each function in the ASC-38C1600 uses only the SE cable detect and
	 * termination because there are two connectors for each function.
	 * Each function may use either LVD or SE mode.
	 * Corresponding the SE automatic termination control EEPROM bits are
	 * used for each function.
	 * Each function has its own EEPROM. If SE automatic control is enabled
	 * for the function, then set the termination value based on a table
	 * listed in adwlib.h.
	 *
	 * If manual termination is specified in the EEPROM for the function,
	 * then 'termination' was set-up in AdwInitFromEEPROM() and is
	 * ready to be 'ored' into SCSI_CFG1.
	 */
	if ((cfg->termination & ADW_TERM_SE) == 0) {
		/* SE automatic termination control is enabled. */
		switch(scsi_cfg1 & ADW_C_DET_SE) {
			/* TERM_SE_HI: on, TERM_SE_LO: on */
			case 0x1: case 0x2: case 0x3:
				cfg->termination |= ADW_TERM_SE;
				break;

			case 0x0:
	/* !!!!TODO!!!! */
//				if (ASC_PCI_ID2FUNC(cfg->pci_slot_info) == 0) {
				/* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
//				}
//				else
//				{
				/* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
					cfg->termination |= ADW_TERM_SE_HI;
//				}
				break;
			}
	}

	/*
	 * Clear any set TERM_SE bits.
	 */
	scsi_cfg1 &= ~ADW_TERM_SE;

	/*
	 * Invert the TERM_SE bits and then set 'scsi_cfg1'.
	 */
	scsi_cfg1 |= (~cfg->termination & ADW_TERM_SE);

	/*
	 * Clear Big Endian and Terminator Polarity bits and set possibly
	 * modified termination control bits in the Microcode SCSI_CFG1
	 * Register Value.
	 */
	scsi_cfg1 &= (~ADW_BIG_ENDIAN & ~ADW_DIS_TERM_DRV & ~ADW_TERM_POL);

	/*
	 * Set SCSI_CFG1 Microcode Default Value
	 *
	 * Set possibly modified termination control bits in the Microcode
	 * SCSI_CFG1 Register Value.
	 *
	 * The microcode will set the SCSI_CFG1 register using this value
	 * after it is started below.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);

	/*
	 * Set MEM_CFG Microcode Default Value
	 *
	 * The microcode will set the MEM_CFG register using this value
	 * after it is started below.
	 *
	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
	 * are defined.
	 *
	 * ASC-38C1600 has 32KB internal memory.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
						ADW_BIOS_EN | ADW_RAM_SZ_32KB);

	return 0;
}


/*
 * Read EEPROM configuration into the specified buffer.
 *
 * Return a checksum based on the EEPROM configuration read.
 */
static u_int16_t
AdwGetEEPROMConfig(iot, ioh, cfg_buf)
	bus_space_tag_t		iot;
	bus_space_handle_t	ioh;
	ADW_EEPROM		*cfg_buf;
{
	u_int16_t	       wval, chksum;
	u_int16_t	       *wbuf;
	int		    eep_addr;


	wbuf = (u_int16_t *) cfg_buf;
	chksum = 0;

	for (eep_addr = ASC_EEP_DVC_CFG_BEGIN;
		eep_addr < ASC_EEP_DVC_CFG_END;
		eep_addr++, wbuf++) {
		wval = AdwReadEEPWord(iot, ioh, eep_addr);
		chksum += wval;
		*wbuf = wval;
	}

	*wbuf = AdwReadEEPWord(iot, ioh, eep_addr);
	wbuf++;
	for (eep_addr = ASC_EEP_DVC_CTL_BEGIN;
			eep_addr < ASC_EEP_MAX_WORD_ADDR;
			eep_addr++, wbuf++) {
		*wbuf = AdwReadEEPWord(iot, ioh, eep_addr);
	}

	return chksum;
}


/*
 * Read the EEPROM from specified location
 */
static u_int16_t
AdwReadEEPWord(iot, ioh, eep_word_addr)
	bus_space_tag_t		iot;
	bus_space_handle_t	ioh;
	int			eep_word_addr;
{
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
		ASC_EEP_CMD_READ | eep_word_addr);
	AdwWaitEEPCmd(iot, ioh);

	return ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_DATA);
}


/*
 * Wait for EEPROM command to complete
 */
static void
AdwWaitEEPCmd(iot, ioh)
	bus_space_tag_t		iot;
	bus_space_handle_t	ioh;
{
	int eep_delay_ms;


	for (eep_delay_ms = 0; eep_delay_ms < ASC_EEP_DELAY_MS; eep_delay_ms++){
		if (ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_CMD) &
				ASC_EEP_CMD_DONE) {
			break;
		}
		AdwSleepMilliSecond(1);
	}

	ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_CMD);
}


/*
 * Write the EEPROM from 'cfg_buf'.
 */
static void
AdwSetEEPROMConfig(iot, ioh, cfg_buf)
	bus_space_tag_t		iot;
	bus_space_handle_t	ioh;
	ADW_EEPROM		*cfg_buf;
{
	u_int16_t *wbuf;
	u_int16_t addr, chksum;


	wbuf = (u_int16_t *) cfg_buf;
	chksum = 0;

	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
	AdwWaitEEPCmd(iot, ioh);

	/*
	 * Write EEPROM from word 0 to word 20
	 */
	for (addr = ASC_EEP_DVC_CFG_BEGIN;
	     addr < ASC_EEP_DVC_CFG_END; addr++, wbuf++) {
		chksum += *wbuf;
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, *wbuf);
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
				ASC_EEP_CMD_WRITE | addr);
		AdwWaitEEPCmd(iot, ioh);
		AdwSleepMilliSecond(ASC_EEP_DELAY_MS);
	}

	/*
	 * Write EEPROM checksum at word 21
	 */
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, chksum);
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
			ASC_EEP_CMD_WRITE | addr);
	AdwWaitEEPCmd(iot, ioh);
	wbuf++;        /* skip over check_sum */

	/*
	 * Write EEPROM OEM name at words 22 to 29
	 */
	for (addr = ASC_EEP_DVC_CTL_BEGIN;
	     addr < ASC_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, *wbuf);
		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
				ASC_EEP_CMD_WRITE | addr);
		AdwWaitEEPCmd(iot, ioh);
	}

	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
			ASC_EEP_CMD_WRITE_DISABLE);
	AdwWaitEEPCmd(iot, ioh);

	return;
}


/*
 * AdwExeScsiQueue() - Send a request to the RISC microcode program.
 *
 *   Allocate a carrier structure, point the carrier to the ADW_SCSI_REQ_Q,
 *   add the carrier to the ICQ (Initiator Command Queue), and tickle the
 *   RISC to notify it a new command is ready to be executed.
 *
 * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
 * set to SCSI_MAX_RETRY.
 *
 * Return:
 *      ADW_SUCCESS(1) - The request was successfully queued.
 *      ADW_BUSY(0) -    Resource unavailable; Retry again after pending
 *                       request completes.
 *      ADW_ERROR(-1) -  Invalid ADW_SCSI_REQ_Q request structure
 *                       host IC error.
 */
int
AdwExeScsiQueue(sc, scsiq)
ADW_SOFTC	*sc;
ADW_SCSI_REQ_Q	*scsiq;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	ADW_CCB		*ccb;
	long		req_size;
	u_int32_t	req_paddr;
	ADW_CARRIER	*new_carrp;

	/*
	 * The ADW_SCSI_REQ_Q 'target_id' field should never exceed ADW_MAX_TID.
	 */
	if (scsiq->target_id > ADW_MAX_TID) {
		scsiq->host_status = QHSTA_M_INVALID_DEVICE;
		scsiq->done_status = QD_WITH_ERROR;
		return ADW_ERROR;
	}

	/*
	 * Begin of CRITICAL SECTION: Must be protected within splbio/splx pair
	 */
	
	ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr);

	/*
	 * Allocate a carrier and initialize fields.
	 */
	if ((new_carrp = sc->carr_freelist) == NULL) {
		return ADW_BUSY;
	}
	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
			ASC_GET_CARRP(new_carrp->next_ba));
	sc->carr_pending_cnt++;

	/*
	 * Set the carrier to be a stopper by setting 'next_ba'
	 * to the stopper value. The current stopper will be changed
	 * below to point to the new stopper.
	 */
	new_carrp->next_ba = ASC_CQ_STOPPER;

	req_size = sizeof(ADW_SCSI_REQ_Q);
	req_paddr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
		ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsiq);

	/* Save physical address of ADW_SCSI_REQ_Q and Carrier. */
	scsiq->scsiq_rptr = req_paddr;

	/*
	 * Every ADV_CARR_T.carr_ba is byte swapped to little-endian
	 * order during initialization.
	 */
	scsiq->carr_ba = sc->icq_sp->carr_ba;
	scsiq->carr_va = sc->icq_sp->carr_ba;

	/*
	 * Use the current stopper to send the ADW_SCSI_REQ_Q command to
	 * the microcode. The newly allocated stopper will become the new
	 * stopper.
	 */
	sc->icq_sp->areq_ba = req_paddr;

	/*
	 * Set the 'next_ba' pointer for the old stopper to be the
	 * physical address of the new stopper. The RISC can only
	 * follow physical addresses.
	 */
	sc->icq_sp->next_ba = new_carrp->carr_ba;

#if ADW_DEBUG
	printf("icq 0x%x, 0x%x, 0x%x, 0x%x\n",
			sc->icq_sp->carr_id,
			sc->icq_sp->carr_ba,
			sc->icq_sp->areq_ba,
			sc->icq_sp->next_ba);
#endif
	/*
	 * Set the host adapter stopper pointer to point to the new carrier.
	 */
	sc->icq_sp = new_carrp;

	if (sc->chip_type == ADW_CHIP_ASC3550 ||
	    sc->chip_type == ADW_CHIP_ASC38C0800) {
		/*
		 * Tickle the RISC to tell it to read its Command Queue Head
		 * pointer.
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADV_TICKLE_A);
		if (sc->chip_type == ADW_CHIP_ASC3550) {
			/*
			 * Clear the tickle value. In the ASC-3550 the RISC flag
			 * command 'clr_tickle_a' does not work unless the host
			 * value is cleared.
			 */
			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE,
					ADV_TICKLE_NOP);
		}
	} else if (sc->chip_type == ADW_CHIP_ASC38C1600) {
		/*
		 * Notify the RISC a carrier is ready by writing the physical
		 * address of the new carrier stopper to the COMMA register.
		 */
		ADW_WRITE_DWORD_REGISTER(iot, ioh, IOPDW_COMMA,
				new_carrp->carr_ba);
	}

	/*
	 * End of CRITICAL SECTION: Must be protected within splbio/splx pair
	 */
	
	return ADW_SUCCESS;
}


void
AdwResetChip(iot, ioh)
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
{

	/*
	 * Reset Chip.
	 */
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
			ADW_CTRL_REG_CMD_RESET);
	AdwSleepMilliSecond(100);
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
			ADW_CTRL_REG_CMD_WR_IO_REG);
}


/*
 * Reset SCSI Bus and purge all outstanding requests.
 *
 * Return Value:
 *      ADW_TRUE(1) -   All requests are purged and SCSI Bus is reset.
 *      ADW_FALSE(0) -  Microcode command failed.
 *      ADW_ERROR(-1) - Microcode command timed-out. Microcode or IC
 *                      may be hung which requires driver recovery.
 */
int
AdwResetCCB(sc)
ADW_SOFTC	*sc;
{
	int	    status;

	/*
	 * Send the SCSI Bus Reset idle start idle command which asserts
	 * the SCSI Bus Reset signal.
	 */
	status = AdwSendIdleCmd(sc, (u_int16_t) IDLE_CMD_SCSI_RESET_START, 0L);
	if (status != ADW_TRUE) {
		return status;
	}

	/*
	 * Delay for the specified SCSI Bus Reset hold time.
	 *
	 * The hold time delay is done on the host because the RISC has no
	 * microsecond accurate timer.
	 */
	AdwDelayMicroSecond((u_int16_t) ASC_SCSI_RESET_HOLD_TIME_US);

	/*
	 * Send the SCSI Bus Reset end idle command which de-asserts
	 * the SCSI Bus Reset signal and purges any pending requests.
	 */
	status = AdwSendIdleCmd(sc, (u_int16_t) IDLE_CMD_SCSI_RESET_END, 0L);
	if (status != ADW_TRUE) {
		return status;
	}

	AdwSleepMilliSecond((u_int32_t) sc->scsi_reset_wait * 1000);

	return status;
}


/*
 * Reset chip and SCSI Bus.
 *
 * Return Value:
 *      ADW_TRUE(1) -   Chip re-initialization and SCSI Bus Reset successful.
 *      ADW_FALSE(0) -  Chip re-initialization and SCSI Bus Reset failure.
 */
int
AdwResetSCSIBus(sc)
ADW_SOFTC	*sc;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	int		status;
	u_int16_t	wdtr_able, sdtr_able, ppr_able, tagqng_able;
	u_int8_t	tid, max_cmd[ADW_MAX_TID + 1];
	u_int16_t	bios_sig;


	/*
	 * Save current per TID negotiated values.
	 */
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
	if (sc->chip_type == ADW_CHIP_ASC38C1600) {
		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
	}
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
			max_cmd[tid]);
	}

	/*
	 * Force the AdwInitAscDriver() function to perform a SCSI Bus Reset
	 * by clearing the BIOS signature word.
	 * The initialization functions assumes a SCSI Bus Reset is not
	 * needed if the BIOS signature word is present.
	 */
	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, bios_sig);
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, 0);

	/*
	 * Stop chip and reset it.
	 */
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RISC_CSR, ADW_RISC_CSR_STOP);
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
			ADW_CTRL_REG_CMD_RESET);
	AdwSleepMilliSecond(100);
	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
			ADW_CTRL_REG_CMD_WR_IO_REG);

	/*
	 * Reset Adv Library error code, if any, and try
	 * re-initializing the chip.
	 * Then translate initialization return value to status value.
	 */
	status = (AdwInitDriver(sc) == 0)? ADW_TRUE : ADW_FALSE;

	/*
	 * Restore the BIOS signature word.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, bios_sig);

	/*
	 * Restore per TID negotiated values.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
	if (sc->chip_type == ADW_CHIP_ASC38C1600) {
		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
	}
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
		ADW_WRITE_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
			max_cmd[tid]);
	}

	return status;
}


/*
 * Adv Library Interrupt Service Routine
 *
 *  This function is called by a driver's interrupt service routine.
 *  The function disables and re-enables interrupts.
 *
 *  When a microcode idle command is completed, the ADV_DVC_VAR
 *  'idle_cmd_done' field is set to ADW_TRUE.
 *
 *  Note: AdwISR() can be called when interrupts are disabled or even
 *  when there is no hardware interrupt condition present. It will
 *  always check for completed idle commands and microcode requests.
 *  This is an important feature that shouldn't be changed because it
 *  allows commands to be completed from polling mode loops.
 *
 * Return:
 *   ADW_TRUE(1) - interrupt was pending
 *   ADW_FALSE(0) - no interrupt was pending
 */
int
AdwISR(sc)
ADW_SOFTC	*sc;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	u_int8_t	int_stat;
	u_int16_t	target_bit;
	ADW_CARRIER	*free_carrp/*, *ccb_carr*/;
	u_int32_t	irq_next_pa;
	ADW_SCSI_REQ_Q	*scsiq;
	ADW_CCB		*ccb;
	int		s;


	s = splbio();

	/* Reading the register clears the interrupt. */
	int_stat = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_INTR_STATUS_REG);

	if ((int_stat & (ADW_INTR_STATUS_INTRA | ADW_INTR_STATUS_INTRB |
	     ADW_INTR_STATUS_INTRC)) == 0) {
		splx(s);
		return ADW_FALSE;
	}

	/*
	 * Notify the driver of an asynchronous microcode condition by
	 * calling the ADV_DVC_VAR.async_callback function. The function
	 * is passed the microcode ADW_MC_INTRB_CODE byte value.
	 */
	if (int_stat & ADW_INTR_STATUS_INTRB) {
		u_int8_t intrb_code;

		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_INTRB_CODE, intrb_code);

		if (sc->chip_type == ADW_CHIP_ASC3550 ||
	    	    sc->chip_type == ADW_CHIP_ASC38C0800) {
			if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
				sc->carr_pending_cnt != 0) {
				ADW_WRITE_BYTE_REGISTER(iot, ioh,
					IOPB_TICKLE, ADV_TICKLE_A);
				if (sc->chip_type == ADW_CHIP_ASC3550) {
					ADW_WRITE_BYTE_REGISTER(iot, ioh,
						IOPB_TICKLE, ADV_TICKLE_NOP);
				}
			}
		}

		if (sc->async_callback != 0) {
		    (*(ADW_ASYNC_CALLBACK)sc->async_callback)(sc, intrb_code);
		}
	}

	/*
	 * Check if the IRQ stopper carrier contains a completed request.
	 */
	while (((irq_next_pa = sc->irq_sp->next_ba) & ASC_RQ_DONE) != 0)
	{
#if ADW_DEBUG
		printf("irq 0x%x, 0x%x, 0x%x, 0x%x\n",
				sc->irq_sp->carr_id,
				sc->irq_sp->carr_ba,
				sc->irq_sp->areq_ba,
				sc->irq_sp->next_ba);
#endif
		/*
		 * Get a pointer to the newly completed ADW_SCSI_REQ_Q
		 * structure.
		 * The RISC will have set 'areq_ba' to a virtual address.
		 *
		 * The firmware will have copied the ASC_SCSI_REQ_Q.ccb_ptr
		 * field to the carrier ADV_CARR_T.areq_ba field.
		 * The conversion below complements the conversion of
		 * ASC_SCSI_REQ_Q.scsiq_ptr' in AdwExeScsiQueue().
		 */
		ccb = adw_ccb_phys_kv(sc, sc->irq_sp->areq_ba);
		scsiq = &ccb->scsiq;
		scsiq->ccb_ptr = sc->irq_sp->areq_ba;

		/*
		 * Request finished with good status and the queue was not
		 * DMAed to host memory by the firmware. Set all status fields
		 * to indicate good status.
		 */
		if ((irq_next_pa & ASC_RQ_GOOD) != 0) {
			scsiq->done_status = QD_NO_ERROR;
			scsiq->host_status = scsiq->scsi_status = 0;
			scsiq->data_cnt = 0L;
		}

		/*
		 * Advance the stopper pointer to the next carrier
		 * ignoring the lower four bits. Free the previous
		 * stopper carrier.
		 */
		free_carrp = sc->irq_sp;
		sc->irq_sp = ADW_CARRIER_VADDR(sc, ASC_GET_CARRP(irq_next_pa));

		free_carrp->next_ba = (sc->carr_freelist == NULL)? NULL
					: sc->carr_freelist->carr_ba;
		sc->carr_freelist = free_carrp;
		sc->carr_pending_cnt--;


		target_bit = ADW_TID_TO_TIDMASK(scsiq->target_id);

		/*
		 * Clear request microcode control flag.
		 */
		scsiq->cntl = 0;

		/*
		 * Check Condition handling
		 */
		/*
		 * If the command that completed was a SCSI INQUIRY and
		 * LUN 0 was sent the command, then process the INQUIRY
		 * command information for the device.
		 */
		if (scsiq->done_status == QD_NO_ERROR &&
		    scsiq->cdb[0] == INQUIRY &&
		    scsiq->target_lun == 0) {
			AdwInquiryHandling(sc, scsiq);
		}

		/*
		 * Notify the driver of the completed request by passing
		 * the ADW_SCSI_REQ_Q pointer to its callback function.
		 */
		(*(ADW_ISR_CALLBACK)sc->isr_callback)(sc, scsiq);
		/*
		 * Note: After the driver callback function is called, 'scsiq'
		 * can no longer be referenced.
		 *
		 * Fall through and continue processing other completed
		 * requests...
		 */
	}

	splx(s);

	return ADW_TRUE;
}


/*
 * Send an idle command to the chip and wait for completion.
 *
 * Command completion is polled for once per microsecond.
 *
 * The function can be called from anywhere including an interrupt handler.
 * But the function is not re-entrant, so it uses the splbio/splx()
 * functions to prevent reentrancy.
 *
 * Return Values:
 *   ADW_TRUE - command completed successfully
 *   ADW_FALSE - command failed
 *   ADW_ERROR - command timed out
 */
int
AdwSendIdleCmd(sc, idle_cmd, idle_cmd_parameter)
ADW_SOFTC      *sc;
u_int16_t       idle_cmd;
u_int32_t       idle_cmd_parameter;
{
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	u_int16_t	result;
	u_int32_t	i, j, s;

	s = splbio();

	/*
	 * Clear the idle command status which is set by the microcode
	 * to a non-zero value to indicate when the command is completed.
	 */
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_STATUS, (u_int16_t) 0);

	/*
	 * Write the idle command value after the idle command parameter
	 * has been written to avoid a race condition. If the order is not
	 * followed, the microcode may process the idle command before the
	 * parameters have been written to LRAM.
	 */
	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_PARAMETER,
			idle_cmd_parameter);
	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD, idle_cmd);

	/*
	 * Tickle the RISC to tell it to process the idle command.
	 */
	ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADV_TICKLE_B);
	if (sc->chip_type == ADW_CHIP_ASC3550) {
		/*
		 * Clear the tickle value. In the ASC-3550 the RISC flag
		 * command 'clr_tickle_b' does not work unless the host
		 * value is cleared.
		 */
		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADV_TICKLE_NOP);
	}

	/* Wait for up to 100 millisecond for the idle command to timeout. */
	for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
		/* Poll once each microsecond for command completion. */
		for (j = 0; j < SCSI_US_PER_MSEC; j++) {
			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_STATUS,
									result);
			if (result != 0) {
				splx(s);
				return result;
			}
			AdwDelayMicroSecond(1);
		}
	}

	splx(s);
	return ADW_ERROR;
}


/*
 * Inquiry Information Byte 7 Handling
 *
 * Handle SCSI Inquiry Command information for a device by setting
 * microcode operating variables that affect WDTR, SDTR, and Tag
 * Queuing.
 */
static void
AdwInquiryHandling(sc, scsiq)
ADW_SOFTC	*sc;
ADW_SCSI_REQ_Q *scsiq;
{
#ifndef FAILSAFE
	bus_space_tag_t iot = sc->sc_iot;
	bus_space_handle_t ioh = sc->sc_ioh;
	u_int8_t		tid;
	ADW_SCSI_INQUIRY	*inq;
	u_int16_t		tidmask;
	u_int16_t		cfg_word;


	/*
	 * AdwInquiryHandling() requires up to INQUIRY information Byte 7
	 * to be available.
	 *
	 * If less than 8 bytes of INQUIRY information were requested or less
	 * than 8 bytes were transferred, then return. cdb[4] is the request
	 * length and the ADW_SCSI_REQ_Q 'data_cnt' field is set by the
	 * microcode to the transfer residual count.
	 */

	if (scsiq->cdb[4] < 8 || (scsiq->cdb[4] - scsiq->data_cnt) < 8) {
		return;
	}

	tid = scsiq->target_id;

        inq = (ADW_SCSI_INQUIRY *) scsiq->vdata_addr;

	/*
	 * WDTR, SDTR, and Tag Queuing cannot be enabled for old devices.
	 */
	if ((inq->rsp_data_fmt < 2) /*SCSI-1 | CCS*/ &&
	    (inq->ansi_apr_ver < 2)) {
		return;
	} else {
		/*
		 * INQUIRY Byte 7 Handling
		 *
		 * Use a device's INQUIRY byte 7 to determine whether it
		 * supports WDTR, SDTR, and Tag Queuing. If the feature
		 * is enabled in the EEPROM and the device supports the
		 * feature, then enable it in the microcode.
		 */

		tidmask = ADW_TID_TO_TIDMASK(tid);

		/*
		 * Wide Transfers
		 *
		 * If the EEPROM enabled WDTR for the device and the device
		 * supports wide bus (16 bit) transfers, then turn on the
		 * device's 'wdtr_able' bit and write the new value to the
		 * microcode.
		 */
#ifdef SCSI_ADW_WDTR_DISABLE
	if(!(tidmask & SCSI_ADW_WDTR_DISABLE))
#endif /* SCSI_ADW_WDTR_DISABLE */
		if ((sc->wdtr_able & tidmask) && inq->WBus16) {
			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
					cfg_word);
			if ((cfg_word & tidmask) == 0) {
				cfg_word |= tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
						cfg_word);

				/*
				 * Clear the microcode "SDTR negotiation" and
				 * "WDTR negotiation" done indicators for the
				 * target to cause it to negotiate with the new
				 * setting set above.
				 * WDTR when accepted causes the target to enter
				 * asynchronous mode, so SDTR must be negotiated
				 */
				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
						cfg_word);
				cfg_word &= ~tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
						cfg_word);
				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_DONE,
						cfg_word);
				cfg_word &= ~tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_DONE,
						cfg_word);
			}
		}

		/*
		 * Synchronous Transfers
		 *
		 * If the EEPROM enabled SDTR for the device and the device
		 * supports synchronous transfers, then turn on the device's
		 * 'sdtr_able' bit. Write the new value to the microcode.
		 */
#ifdef SCSI_ADW_SDTR_DISABLE
	if(!(tidmask & SCSI_ADW_SDTR_DISABLE))
#endif /* SCSI_ADW_SDTR_DISABLE */
		if ((sc->sdtr_able & tidmask) && inq->Sync) {
			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,cfg_word);
			if ((cfg_word & tidmask) == 0) {
				cfg_word |= tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,
						cfg_word);

				/*
				 * Clear the microcode "SDTR negotiation"
				 * done indicator for the target to cause it
				 * to negotiate with the new setting set above.
				 */
				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
						cfg_word);
				cfg_word &= ~tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
						cfg_word);
			}
		}
		/*
		 * If the Inquiry data included enough space for the SPI-3
		 * Clocking field, then check if DT mode is supported.
		 */
		if (sc->chip_type == ADW_CHIP_ASC38C1600 &&
		   (scsiq->cdb[4] >= 57 ||
		   (scsiq->cdb[4] - scsiq->data_cnt) >= 57)) {
			/*
			 * PPR (Parallel Protocol Request) Capable
			 *
			 * If the device supports DT mode, then it must be
			 * PPR capable.
			 * The PPR message will be used in place of the SDTR
			 * and WDTR messages to negotiate synchronous speed
			 * and offset, transfer width, and protocol options.
			 */
                         if((inq->Clocking) & INQ_CLOCKING_DT_ONLY){
				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE,
						sc->ppr_able);
				sc->ppr_able |= tidmask;
				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE,
						sc->ppr_able);
			}
		}

		/*
		 * If the EEPROM enabled Tag Queuing for the device and the
		 * device supports Tag Queueing, then turn on the device's
		 * 'tagqng_enable' bit in the microcode and set the microcode
		 * maximum command count to the ADV_DVC_VAR 'max_dvc_qng'
		 * value.
		 *
		 * Tag Queuing is disabled for the BIOS which runs in polled
		 * mode and would see no benefit from Tag Queuing. Also by
		 * disabling Tag Queuing in the BIOS devices with Tag Queuing
		 * bugs will at least work with the BIOS.
		 */
#ifdef SCSI_ADW_TAGQ_DISABLE
	if(!(tidmask & SCSI_ADW_TAGQ_DISABLE))
#endif /* SCSI_ADW_TAGQ_DISABLE */
		if ((sc->tagqng_able & tidmask) && inq->CmdQue) {
			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
					cfg_word);
			cfg_word |= tidmask;
			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
					cfg_word);

			ADW_WRITE_BYTE_LRAM(iot, ioh,
					ADW_MC_NUMBER_OF_MAX_CMD + tid,
					sc->max_dvc_qng);
		}
	}
#endif /* FAILSAFE */
}


static void
AdwSleepMilliSecond(n)
u_int32_t	n;
{

	DELAY(n * 1000);
}


static void
AdwDelayMicroSecond(n)
u_int32_t	n;
{

	DELAY(n);
}