summaryrefslogtreecommitdiffstats
path: root/sys/cam/scsi/scsi_low.c
blob: 1d0a01d0c1d0cc859b043c57b480d986ebeb7963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
/*	$FreeBSD$	*/
/*	$NecBSD: scsi_low.c,v 1.24 1999/07/26 06:27:01 honda Exp $	*/
/*	$NetBSD$	*/

#define	SCSI_LOW_STATICS
#define SCSI_LOW_WARNINGS
#ifdef __NetBSD__
#define	SCSI_LOW_TARGET_OPEN
#define SCSI_LOW_INFORM
#endif
#ifdef __FreeBSD__
#define CAM
#endif

/*
 * [NetBSD for NEC PC-98 series]
 *  Copyright (c) 1995, 1996, 1997, 1998, 1999
 *	NetBSD/pc98 porting staff. All rights reserved.
 *  Copyright (c) 1995, 1996, 1997, 1998, 1999
 *	Naofumi HONDA. All rights reserved.
 * 
 *  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. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 */

/* <On the nexus establishment>
 * When our host is reselected, 
 * nexus establish processes are little complicated.
 * Normal steps are followings:
 * 1) Our host selected by target => target nexus (slp->sl_nexus) 
 * 2) Identify msgin => lun nexus (ti->ti_li)
 * 3) Qtag msg => slccb nexus (ti->ti_nexus)
 */
#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/disklabel.h>
#if defined(__FreeBSD__) && __FreeBSD_version >= 500001
#include <sys/bio.h>
#endif
#include <sys/buf.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/device_port.h>
#include <sys/errno.h>

#include <vm/vm.h>

#include <machine/bus.h>
#ifdef __NetBSD__
#include <machine/intr.h>
#endif
#include <machine/dvcfg.h>

#ifdef __NetBSD__
#include <dev/cons.h>

#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipiconf.h>
#include <dev/scsipi/scsipi_disk.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsiconf.h>

#include <i386/Cbus/dev/scsi_low.h>
#endif
#ifdef __FreeBSD__
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/cam_debug.h>
#include <cam/cam_periph.h>

#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>

#include <cam/scsi/scsi_low.h>

#if !defined(__FreeBSD__) || __FreeBSD_version < 400001
#include <i386/i386/cons.h>
#else
#include <sys/cons.h>
#endif
#include <machine/clock.h>
#define delay(time) DELAY(time)

/* from sys/dev/usb/usb_port.h 
  XXX Change this when FreeBSD has memset
 */
#define memset(d, v, s)	\
		do{			\
		if ((v) == 0)		\
			bzero((d), (s));	\
		else			\
			panic("Non zero filler for memset, cannot handle!"); \
		} while (0)
#endif

#define	SCSI_LOW_DONE_COMPLETE	0
#define	SCSI_LOW_DONE_RETRY	1

static void scsi_low_engage __P((void *));
static void scsi_low_info __P((struct scsi_low_softc *, struct targ_info *, u_char *));
static void scsi_low_init_msgsys __P((struct scsi_low_softc *, struct targ_info *));
static struct slccb *scsi_low_establish_ccb __P((struct targ_info *, struct lun_info *, scsi_low_tag_t));
static int scsi_low_done __P((struct scsi_low_softc *, struct slccb *));
static void scsi_low_twiddle_wait __P((void));
static struct lun_info *scsi_low_alloc_li __P((struct targ_info *, int, int));
static struct targ_info *scsi_low_alloc_ti __P((struct scsi_low_softc *, int));
static void scsi_low_calcf __P((struct targ_info *, struct lun_info *));
static struct lun_info *scsi_low_establish_lun __P((struct targ_info *, int));
#ifndef CAM
static void scsi_low_scsi_minphys __P((struct buf *));
#endif
#ifdef	SCSI_LOW_TARGET_OPEN
static int scsi_low_target_open __P((struct scsipi_link *, struct cfdata *));
#endif	/* SCSI_LOW_TARGET_OPEN */
#ifdef CAM
void scsi_low_scsi_action(struct cam_sim *sim, union ccb *ccb);
static void scsi_low_poll (struct cam_sim *sim);
#else
static int scsi_low_scsi_cmd __P((struct scsipi_xfer *));
#endif
static void scsi_low_reset_nexus __P((struct scsi_low_softc *, int));
static int scsi_low_init __P((struct scsi_low_softc *, u_int));
static void scsi_low_start __P((struct scsi_low_softc *));
static void scsi_low_free_ti __P((struct scsi_low_softc *));
static void scsi_low_clear_ccb __P((struct slccb *));

#ifdef	SCSI_LOW_STATICS
struct scsi_low_statics {
	int nexus_win;
	int nexus_fail;
	int nexus_disconnected;
	int nexus_reselected;
	int nexus_conflict;
} scsi_low_statics;
#endif	/* SCSI_LOW_STATICS */
/**************************************************************
 * power control
 **************************************************************/
static void
scsi_low_engage(arg)
	void *arg;
{
	struct scsi_low_softc *slp = arg;
	int s = splbio();

	switch (slp->sl_rstep)
	{
	case 0:
		slp->sl_rstep ++;
		(*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
#ifdef __FreeBSD__
		slp->engage_ch = 
#endif
		timeout(scsi_low_engage, slp, 1);
		break;

	case 1:
		slp->sl_rstep ++;
		slp->sl_flags &= ~HW_RESUME;
		scsi_low_start(slp);
		break;

	case 2:
		break;
	}
	splx(s);
}

static int
scsi_low_init(slp, flags)
	struct scsi_low_softc *slp;
	u_int flags;
{

	if ((slp->sl_flags & HW_POWERCTRL) != 0)
	{
#ifdef __FreeBSD__
		untimeout(scsi_low_engage, slp, slp->engage_ch);
#else /* NetBSD */
		untimeout(scsi_low_engage, slp);
#endif
		slp->sl_flags &= ~(HW_POWDOWN | HW_RESUME);
		slp->sl_active = 1;
		slp->sl_powc = SCSI_LOW_POWDOWN_TC;
	}

	/* reset current nexus */
	scsi_low_reset_nexus(slp, flags);
	if ((slp->sl_flags & HW_INACTIVE) != 0)
		return EBUSY;

	if (flags == SCSI_LOW_RESTART_SOFT)
		return 0;

	return ((*slp->sl_funcs->scsi_low_init) (slp, flags));
}

/**************************************************************
 * allocate lun_info
 **************************************************************/
static struct lun_info *
scsi_low_alloc_li(ti, lun, alloc)
	struct targ_info *ti;
	int lun;
	int alloc;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	struct lun_info *li;

	li = LIST_FIRST(&ti->ti_litab); 
	if (li != NULL)
	{ 
		if (li->li_lun == lun)
			return li;

		while ((li = LIST_NEXT(li, lun_chain)) != NULL)
		{
			if (li->li_lun == lun)
			{
				LIST_REMOVE(li, lun_chain);
				LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);
				return li;
			}
		}
	}

	if (alloc == 0)
		return li;

	li = malloc(ti->ti_lunsize, M_DEVBUF, M_NOWAIT);
	if (li == NULL)
		panic("no lun info mem\n");

	memset(li, 0, ti->ti_lunsize);
	li->li_lun = lun;
	li->li_ti = ti;
#if	defined(SDEV_NOPARITY) && defined(SDEV_NODISC)
	li->li_quirks = SDEV_NOPARITY | SDEV_NODISC;
#endif	/* SDEV_NOPARITY && SDEV_NODISC */
	li->li_cfgflags = 0xffff0000 | SCSI_LOW_SYNC;

	LIST_INSERT_HEAD(&ti->ti_litab, li, lun_chain);

	/* host specific structure initialization per lun */
	(void) ((*slp->sl_funcs->scsi_low_lun_init) (slp, ti, li));

	return li;
}

/**************************************************************
 * allocate targ_info
 **************************************************************/
static struct targ_info *
scsi_low_alloc_ti(slp, targ)
	struct scsi_low_softc *slp;
	int targ;
{
	struct targ_info *ti;

	if (slp->sl_titab.tqh_first == NULL)
		TAILQ_INIT(&slp->sl_titab);

	ti = malloc(sizeof(struct targ_info), M_DEVBUF, M_NOWAIT);
	if (ti == NULL)
		panic("%s short of memory\n", slp->sl_xname);

	memset(ti, 0, sizeof(struct targ_info));
	ti->ti_id = targ;
	ti->ti_sc = slp;

	slp->sl_ti[targ] = ti;
	TAILQ_INSERT_TAIL(&slp->sl_titab, ti, ti_chain);
	TAILQ_INIT(&ti->ti_discq);
	LIST_INIT(&ti->ti_litab);

	return ti;
}

static void
scsi_low_free_ti(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti, *tib;
	struct lun_info *li, *nli;

	for (ti = slp->sl_titab.tqh_first; ti; ti = tib)
	{
		tib = ti->ti_chain.tqe_next;
		for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
		{
			nli = LIST_NEXT(li, lun_chain);
			free(li, M_DEVBUF);
		}
		free(ti, M_DEVBUF);
	}
}

/**************************************************************
 * timeout
 **************************************************************/
void
scsi_low_timeout(arg)
	void *arg;
{
	struct scsi_low_softc *slp = arg;
	struct targ_info *ti;
	struct slccb *cb = NULL;		/* XXX */
	int s = splbio();

	/* check */
	if ((ti = slp->sl_nexus) != NULL && (cb = ti->ti_nexus) != NULL)
	{
		cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
		if (cb->ccb_tc < 0)
			goto bus_reset;
	}
	else if (slp->sl_disc > 0)
	{
		struct targ_info *ti;

		for (ti = slp->sl_titab.tqh_first; ti != NULL;
		     ti = ti->ti_chain.tqe_next)
		{
			for (cb = ti->ti_discq.tqh_first; cb != NULL;
			     cb = cb->ccb_chain.tqe_next)
			{
				cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
				if (cb->ccb_tc < 0)
					goto bus_reset;
			}
		}
	}
	else
	{
		cb = slp->sl_start.tqh_first;
		if (cb != NULL)
		{
			cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
			if (cb->ccb_tc < 0)
				goto bus_reset;
		}
		else if ((slp->sl_flags & HW_POWERCTRL) != 0)
		{
			if ((slp->sl_flags & (HW_POWDOWN | HW_RESUME)) != 0)
				goto out;

			if (slp->sl_active != 0)
			{
				slp->sl_powc = SCSI_LOW_POWDOWN_TC;
				slp->sl_active = 0;
				goto out;
			}

			slp->sl_powc --;
			if (slp->sl_powc < 0)
			{
				slp->sl_powc = SCSI_LOW_POWDOWN_TC;
				slp->sl_flags |= HW_POWDOWN;
				(*slp->sl_funcs->scsi_low_power)
						(slp, SCSI_LOW_POWDOWN);
			}
		}
	}

out:
#ifdef __FreeBSD__
	slp->timeout_ch = 
#endif
	timeout(scsi_low_timeout, slp, SCSI_LOW_TIMEOUT_CHECK_INTERVAL * hz);

	splx(s);
	return;

bus_reset:
	cb->ccb_error |= TIMEOUTIO;
	scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
	scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
	scsi_low_start(slp);
#ifdef __FreeBSD__
	slp->timeout_ch = 
#endif
	timeout(scsi_low_timeout, slp, SCSI_LOW_TIMEOUT_CHECK_INTERVAL * hz);

	splx(s);
}

/**************************************************************
 * CCB
 **************************************************************/
GENERIC_CCB_STATIC_ALLOC(scsi_low, slccb)
GENERIC_CCB(scsi_low, slccb, ccb_chain)

/**************************************************************
 * SCSI INTERFACE (XS)
 **************************************************************/
#define	SCSI_LOW_MINPHYS	0x10000

#ifdef __NetBSD__
struct scsipi_device scsi_low_dev = {
	NULL,	/* Use default error handler */
	NULL,	/* have a queue, served by this */
	NULL,	/* have no async handler */
	NULL,	/* Use default 'done' routine */
};
#endif

#ifdef CAM
static void
scsi_low_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
{
	xpt_free_path(ccb->ccb_h.path);
	free(ccb, M_DEVBUF);
#if defined(__FreeBSD__) && __FreeBSD_version < 400001
	free(periph, M_DEVBUF);
#endif
}

static void
scsi_low_rescan_bus(struct scsi_low_softc *slp)
{
  	struct cam_path *path;
	union ccb *ccb = malloc(sizeof(union ccb), M_DEVBUF, M_WAITOK);
#if defined(__FreeBSD__) && __FreeBSD_version < 400001
	struct cam_periph *xpt_periph = malloc(sizeof(struct cam_periph),
					       M_DEVBUF, M_WAITOK);
#endif
	cam_status status;

	bzero(ccb, sizeof(union ccb));

	status = xpt_create_path(&path, xpt_periph,
				 cam_sim_path(slp->sim), -1, 0);
	if (status != CAM_REQ_CMP)
		return;

	xpt_setup_ccb(&ccb->ccb_h, path, 5);
	ccb->ccb_h.func_code = XPT_SCAN_BUS;
	ccb->ccb_h.cbfcnp = scsi_low_cam_rescan_callback;
	ccb->crcn.flags = CAM_FLAG_NONE;
	xpt_action(ccb);
}
#endif

int
scsi_low_attach(slp, openings, ntargs, nluns, lunsize)
	struct scsi_low_softc *slp;
	int openings, ntargs, nluns, lunsize;
{
	struct targ_info *ti;
	struct lun_info *li;
#ifdef CAM
	struct cam_devq *devq;
#else
	struct scsipi_adapter *sap;
#endif
	int i, nccb;

#ifdef CAM
	OS_DEPEND(sprintf(slp->sl_xname, "%s%d",
			  DEVPORT_DEVNAME(slp->sl_dev), DEVPORT_DEVUNIT(slp->sl_dev)));
#else
	OS_DEPEND(strncpy(slp->sl_xname, DEVPORT_DEVNAME(slp->sl_dev), 16));
#endif
	if (ntargs > SCSI_LOW_NTARGETS)
	{
		printf("scsi_low: %d targets are too large\n", ntargs);
		printf("change kernel options SCSI_LOW_NTARGETS");
	}

	if (lunsize < sizeof(struct lun_info))
		lunsize = sizeof(struct lun_info);

	for (i = 0; i < ntargs; i ++)
	{
		ti = scsi_low_alloc_ti(slp, i);
		ti->ti_lunsize = lunsize;
		li = scsi_low_alloc_li(ti, 0, 1);
	}

#ifndef CAM
	sap = malloc(sizeof(*sap), M_DEVBUF, M_NOWAIT);
	if (sap == NULL)
		return ENOMEM;

	memset(sap, 0, sizeof(*sap));
	sap->scsipi_cmd = scsi_low_scsi_cmd;
	sap->scsipi_minphys = scsi_low_scsi_minphys;
#ifdef	SCSI_LOW_TARGET_OPEN
	sap->open_target_lu = scsi_low_target_open;
#endif	/* SCSI_LOW_TARGET_OPEN */
#endif

	if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
		return EINVAL;

	/* initialize queue */
	nccb = openings * (ntargs - 1);
	if (nccb >= SCSI_LOW_NCCB || nccb <= 0)
		nccb = SCSI_LOW_NCCB;
	scsi_low_init_ccbque(nccb);
	TAILQ_INIT(&slp->sl_start);

	slp->sl_openings = openings;
	slp->sl_ntargs = ntargs;
	slp->sl_nluns = nluns;

#ifdef CAM
	/*
	 * Prepare the scsibus_data area for the upperlevel
	 * scsi code.
	 */
	devq = cam_simq_alloc(256/*MAX_START*/);
	if (devq == NULL)
		return (0);
	/*	scbus->adapter_link = &slp->sc_link; */
	/*
	 * ask the adapter what subunits are present
	 */
	
	slp->sim = cam_sim_alloc(scsi_low_scsi_action, scsi_low_poll,
				 DEVPORT_DEVNAME(slp->sl_dev), slp,
				 DEVPORT_DEVUNIT(slp->sl_dev), 1, 32/*MAX_TAGS*/, devq);
       if (slp->sim == NULL) {
	 cam_simq_free(devq);
	 return 0;
       }

       if (xpt_bus_register(slp->sim, 0) != CAM_SUCCESS) {
	 free(slp->sim, M_DEVBUF);
	 return 0;
       }
       
       if (xpt_create_path(&slp->path, /*periph*/NULL,
                           cam_sim_path(slp->sim), CAM_TARGET_WILDCARD,
			   CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
	 xpt_bus_deregister(cam_sim_path(slp->sim));
	 cam_sim_free(slp->sim, /*free_simq*/TRUE);
	 free(slp->sim, M_DEVBUF);
	 return 0;
       }
#else /* !CAM */
	slp->sl_link.adapter_softc = slp;
	slp->sl_link.scsipi_scsi.adapter_target = slp->sl_hostid;
	slp->sl_link.scsipi_scsi.max_target = ntargs - 1;
	slp->sl_link.scsipi_scsi.max_lun = nluns - 1;
	slp->sl_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
	slp->sl_link.openings = openings;
	slp->sl_link.type = BUS_SCSI;
	slp->sl_link.adapter_softc = slp;
	slp->sl_link.adapter = sap;
	slp->sl_link.device = &scsi_low_dev;
#endif

	/* start watch dog */
	slp->sl_max_retry = SCSI_LOW_MAX_RETRY;
#ifdef __FreeBSD__
	slp->timeout_ch = 
#endif
	timeout(scsi_low_timeout, slp, SCSI_LOW_TIMEOUT_CHECK_INTERVAL * hz);
#ifdef CAM
	if (!cold)
		scsi_low_rescan_bus(slp);
#endif

	return 0;
}

#ifndef CAM
static void
scsi_low_scsi_minphys(bp)
	struct buf *bp;
{

	if (bp->b_bcount > SCSI_LOW_MINPHYS)
		bp->b_bcount = SCSI_LOW_MINPHYS;
	minphys(bp);
}
#endif

int
scsi_low_dettach(slp)
	struct scsi_low_softc *slp;
{

	if (slp->sl_disc > 0 || slp->sl_start.tqh_first != NULL)
		return EBUSY;

	/*
	 * scsipi does not have dettach bus fucntion.
	 *
	scsipi_dettach_scsibus(&slp->sl_link);
	*/

#ifdef CAM
	xpt_async(AC_LOST_DEVICE, slp->path, NULL);
	xpt_free_path(slp->path);
	xpt_bus_deregister(cam_sim_path(slp->sim));
	cam_sim_free(slp->sim, /* free_devq */ TRUE);
#endif

	scsi_low_free_ti(slp);
	return 0;
}

#ifdef CAM
static void
scsi_low_poll(struct cam_sim *sim)
{
	struct scsi_low_softc *slp = (struct scsi_low_softc *) cam_sim_softc(sim);
	(*slp->sl_funcs->scsi_low_poll) (slp);
}

void
scsi_low_scsi_action(struct cam_sim *sim, union ccb *ccb)
{
	struct scsi_low_softc *slp = (struct scsi_low_softc *) cam_sim_softc(sim);
	int s, target = (u_int) (ccb->ccb_h.target_id);
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;

#if 0
	printf("scsi_low_scsi_action() func code %d Target: %d, LUN: %d\n",
	       ccb->ccb_h.func_code, target, ccb->ccb_h.target_lun);
#endif
	switch (ccb->ccb_h.func_code) {
	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
		if (((cb = scsi_low_get_ccb()) == NULL)) {
			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
			xpt_done(ccb);
			return;
		}

		cb->ccb = ccb;
		cb->ccb_tag = SCSI_LOW_UNKTAG;
		cb->bp = (struct buf *)NULL;
		cb->ti = ti = slp->sl_ti[target];
		cb->li = scsi_low_alloc_li(ti, ccb->ccb_h.target_lun, 1);
		cb->ccb_flags = 0;
		cb->ccb_rcnt = 0;

		s = splcam();

		TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);

		if (slp->sl_nexus == NULL) {
			scsi_low_start(slp);
		}

		splx(s);
		break;
	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
	case XPT_EN_LUN:		/* Enable LUN as a target */
	case XPT_TARGET_IO:		/* Execute target I/O request */
	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
	case XPT_ABORT:			/* Abort the specified CCB */
		/* XXX Implement */
		ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;
	case XPT_SET_TRAN_SETTINGS:
		/* XXX Implement */
		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
		xpt_done(ccb);
		break;
	case XPT_GET_TRAN_SETTINGS: {
		struct ccb_trans_settings *cts;
		struct targ_info *ti;
		int lun = ccb->ccb_h.target_lun;
		/*int s;*/

		cts = &ccb->cts;
		ti = slp->sl_ti[ccb->ccb_h.target_id];
		li = LIST_FIRST(&ti->ti_litab); 
		if (li != NULL && li->li_lun != lun)
			while ((li = LIST_NEXT(li, lun_chain)) != NULL)
				if (li->li_lun == lun)
					break;
		s = splcam();
		if (li != NULL && (cts->flags & CCB_TRANS_USER_SETTINGS) != 0) {
			if (li->li_cfgflags & SCSI_LOW_DISC)
				cts->flags = CCB_TRANS_DISC_ENB;
			else
				cts->flags = 0;
			if (li->li_cfgflags & SCSI_LOW_QTAG)
				cts->flags |= CCB_TRANS_TAG_ENB;

			cts->bus_width = 0;/*HN2*/

			cts->valid = CCB_TRANS_SYNC_RATE_VALID
				   | CCB_TRANS_SYNC_OFFSET_VALID
				   | CCB_TRANS_BUS_WIDTH_VALID
				   | CCB_TRANS_DISC_VALID
				   | CCB_TRANS_TQ_VALID;
			ccb->ccb_h.status = CAM_REQ_CMP;
		} else
			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;

		splx(s);
		xpt_done(ccb);
		break;
	}
	case XPT_CALC_GEOMETRY: { /* not yet HN2 */
		struct	  ccb_calc_geometry *ccg;
		u_int32_t size_mb;
		u_int32_t secs_per_cylinder;
		int       extended;

		extended = 1;
		ccg = &ccb->ccg;
		size_mb = ccg->volume_size
			/ ((1024L * 1024L) / ccg->block_size);
		
		if (size_mb > 1024 && extended) {
		        ccg->heads = 255;
		        ccg->secs_per_track = 63;
		} else {
		        ccg->heads = 64;
		        ccg->secs_per_track = 32;
		}
		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
		ccb->ccb_h.status = CAM_REQ_CMP;
		xpt_done(ccb);
		break;
	}
	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
#if 0
	  	scsi_low_bus_reset(slp);
#endif
		ccb->ccb_h.status = CAM_REQ_CMP;
		xpt_done(ccb);
		break;
	case XPT_TERM_IO:		/* Terminate the I/O process */
		/* XXX Implement */
		ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;
	case XPT_PATH_INQ: {		/* Path routing inquiry */
		struct ccb_pathinq *cpi = &ccb->cpi;
		
		cpi->version_num = 1; /* XXX??? */
		cpi->hba_inquiry = PI_SDTR_ABLE;
		cpi->target_sprt = 0;
		cpi->hba_misc = 0;
		cpi->hba_eng_cnt = 0;
		cpi->max_target = SCSI_LOW_NTARGETS - 1;
		cpi->max_lun = 7;
		cpi->initiator_id = 7; /* HOST_SCSI_ID */
		cpi->bus_id = cam_sim_bus(sim);
		cpi->base_transfer_speed = 3300;
		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
		strncpy(cpi->hba_vid, "SCSI_LOW", HBA_IDLEN);
		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
		cpi->unit_number = cam_sim_unit(sim);
		cpi->ccb_h.status = CAM_REQ_CMP;
		xpt_done(ccb);
		break;
	}
	default:
	        printf("scsi_low: non support func_code = %d ", ccb->ccb_h.func_code);
		ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;
	}
}
#else /* !CAM */
static int
scsi_low_scsi_cmd(xs)
	struct scsipi_xfer *xs;
{
	struct scsi_low_softc *slp = xs->sc_link->adapter_softc;
	struct targ_info *ti;
	struct slccb *cb;
	int s, lun, timeo;

	if (slp->sl_cfgflags & CFG_NOATTEN)
	{
		if (xs->sc_link->scsipi_scsi.lun > 0)
		{
			xs->error = XS_DRIVER_STUFFUP;
			return COMPLETE;
		}
	}

	if ((cb = scsi_low_get_ccb(xs->flags & SCSI_NOSLEEP)) == NULL)
		return TRY_AGAIN_LATER;

	lun = xs->sc_link->scsipi_scsi.lun;
	cb->xs = xs;
	cb->ccb_tag = SCSI_LOW_UNKTAG;
	cb->ti = ti = slp->sl_ti[xs->sc_link->scsipi_scsi.target];
	cb->li = scsi_low_alloc_li(ti, lun, 1);
	cb->ccb_flags = 0;
	cb->ccb_rcnt = 0;

	s = splbio();

	TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
	if (slp->sl_nexus == NULL)
		scsi_low_start(slp);

	if ((xs->flags & SCSI_POLL) == 0)
	{
		splx(s);
		return SUCCESSFULLY_QUEUED;
	}

#define	SCSI_LOW_POLL_INTERVAL	1000		/* 1 ms */
	timeo = xs->timeout * (1000 / SCSI_LOW_POLL_INTERVAL);

	while ((xs->flags & ITSDONE) == 0 && timeo -- > 0)
	{
		delay(SCSI_LOW_POLL_INTERVAL);
		(*slp->sl_funcs->scsi_low_poll) (slp);
	}

	if ((xs->flags & ITSDONE) == 0)
	{
		cb->ccb_error |= (TIMEOUTIO | ABORTIO);
		SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
		scsi_low_disconnected(slp, ti);
		scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
	}

	scsipi_done(xs);
	splx(s);
	return COMPLETE;
}
#endif

/**************************************************************
 * Start & Done
 **************************************************************/
#ifdef __NetBSD__
static struct scsipi_start_stop ss_cmd = { START_STOP, 0, {0,0,}, SSS_START, };
static struct scsipi_test_unit_ready unit_ready_cmd;
#endif
#ifdef __FreeBSD__
static struct scsi_start_stop_unit ss_cmd = { START_STOP, 0, {0,0,}, SSS_START, };
static struct scsi_test_unit_ready unit_ready_cmd;
#endif
static void scsi_low_unit_ready_cmd __P((struct slccb *));

static void
scsi_low_unit_ready_cmd(cb)
	struct slccb *cb;
{

	cb->ccb_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
	cb->ccb_scp.scp_cmdlen = sizeof(unit_ready_cmd);
	cb->ccb_scp.scp_datalen = 0;
	cb->ccb_scp.scp_direction = SCSI_LOW_READ;
	cb->ccb_tcmax = 15;
}

static void
scsi_low_start(slp)
	struct scsi_low_softc *slp;
{
#ifdef CAM
        union ccb *ccb;
#else
	struct scsipi_xfer *xs;
#endif
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
	int rv;

	/* check hardware exists ? */
	if ((slp->sl_flags & HW_INACTIVE) != 0)
		return;

	/* check hardware power up ? */
	if ((slp->sl_flags & HW_POWERCTRL) != 0)
	{
		slp->sl_active ++;
		if (slp->sl_flags & (HW_POWDOWN | HW_RESUME))
		{
			if (slp->sl_flags & HW_RESUME)
				return;
			slp->sl_flags &= ~HW_POWDOWN;
			if (slp->sl_funcs->scsi_low_power != NULL)
			{
				slp->sl_flags |= HW_RESUME;
				slp->sl_rstep = 0;
				(*slp->sl_funcs->scsi_low_power)
					(slp, SCSI_LOW_ENGAGE);
#ifdef __FreeBSD__
				slp->engage_ch =
#endif
				timeout(scsi_low_engage, slp, 1);
				return;
			}
		}
	}

	/* setup nexus */
#ifdef	SCSI_LOW_DIAGNOSTIC
	ti = slp->sl_nexus;
	if (ti != NULL)
	{
		scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
		panic("%s: inconsistent(target)\n", slp->sl_xname);
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	for (cb = slp->sl_start.tqh_first; cb != NULL;
	     cb = cb->ccb_chain.tqe_next)
	{
		ti = cb->ti;
		li = cb->li;
		if (ti->ti_phase == PH_NULL)
			goto scsi_low_cmd_start;
		if (ti->ti_phase == PH_DISC && li->li_disc < li->li_maxnexus)
			goto scsi_low_cmd_start;
	}
	return;

scsi_low_cmd_start:
#ifdef CAM
	ccb = cb->ccb;
#else
	xs = cb->xs;
#endif
#ifdef	SCSI_LOW_DIAGNOSTIC
	if (ti->ti_nexus != NULL || ti->ti_li != NULL)
	{
		scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
		panic("%s: inconsistent(lun or ccb)\n", slp->sl_xname);
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	/* clear all error flag bits (for restart) */
	cb->ccb_error = 0;

	/* setup nexus pointer */
	ti->ti_nexus = cb;
	ti->ti_li = li;
	slp->sl_nexus = ti;

	/* initialize msgsys */
	scsi_low_init_msgsys(slp, ti);

	/* target lun state check */
#ifdef CAM
		li->li_maxstate = UNIT_OK;
#else
	if ((xs->flags & SCSI_POLL) != 0)
		li->li_maxstate = UNIT_NEGSTART;
	else
		li->li_maxstate = UNIT_OK;
#endif

	/* exec cmds */
scsi_low_cmd_exec:
	if ((cb->ccb_flags & CCB_SENSE) != 0)
	{
		memset(&cb->ccb_sense, 0, sizeof(cb->ccb_sense));
#ifdef CAM
#else
		cb->ccb_sense_cmd.opcode = REQUEST_SENSE;
		cb->ccb_sense_cmd.byte2 = (li->li_lun << 5);
		cb->ccb_sense_cmd.length = sizeof(cb->ccb_sense);
#endif
		cb->ccb_scp.scp_cmd = (u_int8_t *) &cb->ccb_sense_cmd;
		cb->ccb_scp.scp_cmdlen = sizeof(cb->ccb_sense_cmd);
		cb->ccb_scp.scp_data = (u_int8_t *) &cb->ccb_sense;
		cb->ccb_scp.scp_datalen = sizeof(cb->ccb_sense);
		cb->ccb_scp.scp_direction = SCSI_LOW_READ;
		cb->ccb_tcmax = 15;
	}
	else if (li->li_state >= li->li_maxstate)
	{
#ifdef CAM
		cb->ccb_scp.scp_cmd = ccb->csio.cdb_io.cdb_bytes;
		cb->ccb_scp.scp_cmdlen = (int) ccb->csio.cdb_len;
		cb->ccb_scp.scp_data = ccb->csio.data_ptr;
		cb->ccb_scp.scp_datalen = (int) ccb->csio.dxfer_len;
		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
			cb->ccb_scp.scp_direction = SCSI_LOW_WRITE;
		else /* if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) */
			cb->ccb_scp.scp_direction = SCSI_LOW_READ;
		cb->ccb_tcmax = (ccb->ccb_h.timeout >> 10);
#else
		cb->ccb_scp.scp_cmd = (u_int8_t *) xs->cmd;
		cb->ccb_scp.scp_cmdlen = xs->cmdlen;
		cb->ccb_scp.scp_data = xs->data;
		cb->ccb_scp.scp_datalen = xs->datalen;
		cb->ccb_scp.scp_direction = (xs->flags & SCSI_DATA_OUT) ? 
				SCSI_LOW_WRITE : SCSI_LOW_READ;
		cb->ccb_tcmax = (xs->timeout >> 10);
#endif

	}
	else switch(li->li_state)
	{
	case UNIT_SLEEP:
		scsi_low_unit_ready_cmd(cb);
		break;

	case UNIT_START:
		cb->ccb_scp.scp_cmd = (u_int8_t *) &ss_cmd;
		cb->ccb_scp.scp_cmdlen = sizeof(ss_cmd);
		cb->ccb_scp.scp_datalen = 0;
		cb->ccb_scp.scp_direction = SCSI_LOW_READ;
		cb->ccb_tcmax = 30;
		break;

	case UNIT_SYNCH:
		if (li->li_maxsynch.offset > 0)
		{
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
			scsi_low_unit_ready_cmd(cb);
			break;
		}
		li->li_state = UNIT_WIDE;

	case UNIT_WIDE:
#ifdef	SCSI_LOW_SUPPORT_WIDE
		if (li->li_width > 0)
		{
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
			scsi_low_unit_ready_cmd(cb);
			break;
		}
#endif	/* SCSI_LOW_SUPPORT_WIDE */
		li->li_state = UNIT_OK;

	case UNIT_OK:
		goto scsi_low_cmd_exec;
	}

	/* timeout */
	if (cb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
		cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
	cb->ccb_tc = cb->ccb_tcmax;

	/* setup saved scsi data pointer */
	cb->ccb_sscp = cb->ccb_scp;

	/* setup current scsi pointer */ 
	slp->sl_scp = cb->ccb_sscp;
	slp->sl_error = cb->ccb_error;

	/* selection start */
	slp->sl_selid = ti;
	rv = ((*slp->sl_funcs->scsi_low_start_bus) (slp, cb));
	if (rv == SCSI_LOW_START_OK)
	{
#ifdef	SCSI_LOW_STATICS
		scsi_low_statics.nexus_win ++;
#endif	/* SCSI_LOW_STATICS */
		return;
	}

#ifdef	SCSI_LOW_STATICS
	scsi_low_statics.nexus_fail ++;
#endif	/* SCSI_LOW_STATICS */
	SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
	scsi_low_clear_nexus(slp, ti);
}

void
scsi_low_clear_nexus(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{

	/* clear all nexus pointer */
	ti->ti_nexus = NULL;
	ti->ti_li = NULL;
	slp->sl_nexus = NULL;

	/* clear selection assert */
	slp->sl_selid = NULL;

	/* clear nexus data */
	slp->sl_nexus_call = 0;
	slp->sl_scp.scp_direction = SCSI_LOW_RWUNK;
}

static int
scsi_low_done(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
#ifdef CAM
	union ccb *ccb;
#else
	struct scsipi_xfer *xs;
#endif
	struct targ_info *ti;
	struct lun_info *li;

	ti = cb->ti;
	li = cb->li;
#ifdef CAM
	ccb = cb->ccb;
#else
	xs = cb->xs;
#endif
	if (cb->ccb_error == 0)
	{
		if ((cb->ccb_flags & CCB_SENSE) != 0)
		{
			cb->ccb_flags &= ~CCB_SENSE;
#ifdef CAM
			ccb->csio.sense_data = cb->ccb_sense;
			/* ccb->ccb_h.status = CAM_AUTOSENSE_FAIL; */
			ccb->ccb_h.status = CAM_REQ_CMP;
			/* ccb->ccb_h.status = CAM_AUTOSNS_VALID|CAM_SCSI_STATUS_ERROR; */
#else
			xs->sense.scsi_sense = cb->ccb_sense;
			xs->error = XS_SENSE;
#endif
		}
		else switch (ti->ti_status)
		{
		case ST_GOOD:
			if (slp->sl_scp.scp_datalen == 0)
			{
#ifdef CAM
				ccb->ccb_h.status = CAM_REQ_CMP;
#else
				xs->error = XS_NOERROR;
#endif
				break;
			}

#define	SCSIPI_SCSI_CD_COMPLETELY_BUGGY	"YES"
#ifdef	SCSIPI_SCSI_CD_COMPLETELY_BUGGY
#ifdef CAM
			if (/* cb->bp == NULL &&  */
			    slp->sl_scp.scp_datalen < cb->ccb_scp.scp_datalen)
#else
			if (xs->bp == NULL &&
			    slp->sl_scp.scp_datalen < cb->ccb_scp.scp_datalen)
#endif
			{
#ifdef CAM
				ccb->ccb_h.status = CAM_REQ_CMP;
#else
				xs->error = XS_NOERROR;
#endif
				break;
			}	
#endif	/* SCSIPI_SCSI_CD_COMPLETELY_BUGGY */

			cb->ccb_error |= PDMAERR;
#ifdef CAM
			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
#else
			xs->error = XS_DRIVER_STUFFUP;
#endif
			break;

		case ST_CHKCOND:
		case ST_MET:
			cb->ccb_flags |= CCB_SENSE;
#ifdef CAM
			ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
#else
			xs->error = XS_SENSE;
#endif
			goto retry;

		case ST_BUSY:
			cb->ccb_error |= BUSYERR;
#ifdef CAM
			ccb->ccb_h.status = CAM_BUSY; /* SCSI_STATUS_ERROR; */
#else
			xs->error = XS_BUSY;
#endif
			break;

		default:
			cb->ccb_error |= FATALIO;
#ifdef CAM
			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
#else
			xs->error = XS_DRIVER_STUFFUP;
#endif
			break;
		}
	}
	else
	{
		cb->ccb_flags &= ~CCB_SENSE;
		if (ti->ti_phase == PH_SELSTART)
		{
#ifdef CAM
			ccb->ccb_h.status = CAM_CMD_TIMEOUT;
#else
			xs->error = XS_TIMEOUT;
#endif
			slp->sl_error |= SELTIMEOUTIO;
			if (li->li_state == UNIT_SLEEP)
				cb->ccb_error |= ABORTIO;
		}
		else
		{
#ifdef CAM
			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
#else
			xs->error = XS_DRIVER_STUFFUP;
#endif
		}

		if ((cb->ccb_error & ABORTIO) != 0)
		{
			cb->ccb_rcnt = slp->sl_max_retry;
#ifdef CAM
			ccb->ccb_h.status = CAM_REQ_ABORTED;
#endif
		}
	}

	/* target state check */
	if (li->li_state < li->li_maxstate)
	{
		if (cb->ccb_rcnt < slp->sl_max_retry)
		{
			li->li_state ++;
			cb->ccb_rcnt = 0;
			goto retry;
		}
	}

	/* internal retry check */
#ifdef CAM
	if (ccb->ccb_h.status == CAM_REQ_CMP)
	{
		ccb->csio.resid = 0;
	}
	else
	{
#if 0
		if (ccb->ccb_h.status != CAM_AUTOSENSE_FAIL && 
		    cb->ccb_rcnt < slp->sl_max_retry)
			goto retry;
#endif
#else
	if (xs->error == XS_NOERROR)
	{
		xs->resid = 0;
	}
	else
	{
		if (xs->error != XS_SENSE && 
		    cb->ccb_rcnt < slp->sl_max_retry)
			goto retry;
#endif

#ifndef CAM
#ifdef SCSI_LOW_WARNINGS
		if (xs->bp != NULL)
		{
			scsi_low_print(slp, ti);
			printf("%s: WARNING: File system IO abort\n",
				slp->sl_xname);
		}
#endif	/* SCSI_LOW_WARNINGS */
#endif
	}

#ifdef CAM
	ccb->csio.scsi_status = ti->ti_status;
	xpt_done(ccb);
#else
	xs->flags |= ITSDONE;
	if ((xs->flags & SCSI_POLL) == 0)
		scsipi_done(xs);
#endif

	/* free our target */
	TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
	scsi_low_free_ccb(cb);
	return SCSI_LOW_DONE_COMPLETE;

retry:
	cb->ccb_rcnt ++;
	if (slp->sl_start.tqh_first != cb)
	{
		TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
		TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
	}
	return SCSI_LOW_DONE_RETRY;
}

/**************************************************************
 * Reset
 **************************************************************/
static void
scsi_low_clear_ccb(cb)
	struct slccb *cb;
{

	cb->ccb_flags &= ~CCB_SENSE;
	cb->ccb_tag = SCSI_LOW_UNKTAG;
}

static void
scsi_low_reset_nexus(slp, fdone)
	struct scsi_low_softc *slp;
	int fdone;
{
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb, *ncb;

	/* current nexus */
	ti = slp->sl_nexus;
	if (ti != NULL && (cb = ti->ti_nexus) != NULL)
	{
		scsi_low_clear_ccb(cb);
		if (fdone != 0 && cb->ccb_rcnt ++ >= slp->sl_max_retry)
		{
			cb->ccb_error |= FATALIO;
			scsi_low_done(slp, cb);
		}
	}

	/* disconnected nexus */
	for (ti = slp->sl_titab.tqh_first; ti != NULL;
	     ti = ti->ti_chain.tqe_next)
	{
		for (cb = ti->ti_discq.tqh_first; cb != NULL; cb = ncb)
		{
		     	ncb = cb->ccb_chain.tqe_next;
			TAILQ_REMOVE(&ti->ti_discq, cb, ccb_chain);
			TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
			scsi_low_clear_ccb(cb);
			if (fdone != 0 && cb->ccb_rcnt ++ >= slp->sl_max_retry)
			{
				cb->ccb_error |= FATALIO;
				scsi_low_done(slp, cb);
			}
		}

		for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
		     li = LIST_NEXT(li, lun_chain))
		{
			li->li_state = UNIT_SLEEP;
			li->li_disc = 0;
			((*slp->sl_funcs->scsi_low_lun_init) (slp, ti, li));
			scsi_low_calcf(ti, li);
		}

		scsi_low_init_msgsys(slp, ti);
		scsi_low_clear_nexus(slp, ti);
		SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
	}

	slp->sl_flags &= ~HW_PDMASTART;
	slp->sl_disc = 0;
}

/* misc */
static int tw_pos;
static char tw_chars[] = "|/-\\";

static void
scsi_low_twiddle_wait(void)
{

	cnputc('\b');
	cnputc(tw_chars[tw_pos++]);
	tw_pos %= (sizeof(tw_chars) - 1);
	delay(TWIDDLEWAIT);
}

void
scsi_low_bus_reset(slp)
	struct scsi_low_softc *slp;
{
	int i;

	(*slp->sl_funcs->scsi_low_bus_reset) (slp);

	printf("%s: try to reset scsi bus  ", slp->sl_xname);
	for (i = 0; i <= SCSI2_RESET_DELAY / TWIDDLEWAIT ; i++)
		scsi_low_twiddle_wait();
	cnputc('\b');
	printf("\n");
}

int
scsi_low_restart(slp, flags, s)
	struct scsi_low_softc *slp;
	int flags;
	u_char *s;
{
	int error;

	if (s != NULL)
		printf("%s: scsi bus restart. reason: %s\n", slp->sl_xname, s);

	if ((error = scsi_low_init(slp, flags)) != 0)
		return error;

	scsi_low_start(slp);
	return 0;
}

/**************************************************************
 * disconnect and reselect
 **************************************************************/
#define	MSGCMD_LUN(msg)	(msg & 0x07)

static struct lun_info *
scsi_low_establish_lun(ti, lun)
	struct targ_info *ti;
	int lun;
{
	struct lun_info *li;

	li = scsi_low_alloc_li(ti, lun, 0);
	if (li == NULL)
		return li;

	ti->ti_li = li;
	return li;
}

static struct slccb *
scsi_low_establish_ccb(ti, li, tag)
	struct targ_info *ti;
	struct lun_info *li;
	scsi_low_tag_t tag;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	struct slccb *cb;

	/*
	 * Search ccb matching with lun and tag.
	 */
	cb = ti->ti_discq.tqh_first;
	for ( ; cb != NULL; cb = cb->ccb_chain.tqe_next)
		if (cb->li == li && cb->ccb_tag == tag)
			goto found;
	return cb;

	/* 
	 * establish our ccb nexus
	 */
found:
	TAILQ_REMOVE(&ti->ti_discq, cb, ccb_chain);
	TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
	ti->ti_nexus = cb;

	slp->sl_scp = cb->ccb_sscp;
	slp->sl_error |= cb->ccb_error;

	slp->sl_disc --;
	li->li_disc --;

	/* inform "ccb nexus established" to the host driver */
	slp->sl_nexus_call = 1;
	(*slp->sl_funcs->scsi_low_establish_nexus) (slp, ti);
	return cb;
}

struct targ_info *
scsi_low_reselected(slp, targ)
	struct scsi_low_softc *slp;
	u_int targ;
{
	struct targ_info *ti;
	u_char *s;

	/* 
	 * Check select vs reselected collision.
	 */

	if ((ti = slp->sl_selid) != NULL)
	{
		scsi_low_clear_nexus(slp, ti);
		SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
#ifdef	SCSI_LOW_STATICS
		scsi_low_statics.nexus_conflict ++;
#endif	/* SCSI_LOW_STATICS */
	}
	else if (slp->sl_nexus != NULL)
	{
		s = "host busy";
		goto world_restart;
	}

	/* 
	 * Check a valid target id asserted ?
	 */
	if (targ >= slp->sl_ntargs || targ == slp->sl_hostid)
	{
		s = "scsi id illegal";
		goto world_restart;
	}

	/* 
	 * Check the target scsi status.
	 */
	ti = slp->sl_ti[targ];
	if (ti->ti_phase != PH_DISC)
	{
		s = "phase mismatch";
		goto world_restart;
	}

	/* 
	 * Setup lun and init msgsys
	 */
	slp->sl_error = 0;
	scsi_low_init_msgsys(slp, ti);

	/* 
	 * Establish our target nexus
	 * Remark: ccb and scsi pointer not yet restored 
	 * 	   if lun != SCSI_LOW_UNKLUN.
	 */
	SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
	slp->sl_nexus = ti;
#ifdef	SCSI_LOW_STATICS
	scsi_low_statics.nexus_reselected ++;
#endif	/* SCSI_LOW_STATICS */
	return ti;

world_restart:
	printf("%s: reselect(%x:unknown) %s\n", slp->sl_xname, targ, s);
	scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, 
		         "reselect: scsi world confused");
	return NULL;
}

int
scsi_low_disconnected(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{
	struct slccb *cb = ti->ti_nexus;

	/* check phase completion */
	switch (slp->sl_msgphase)
	{
	case MSGPH_DISC:
		if (cb != NULL)
		{
			TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
			TAILQ_INSERT_TAIL(&ti->ti_discq, cb, ccb_chain);
			cb->ccb_error |= slp->sl_error;
			cb->li->li_disc ++;
			slp->sl_disc ++;
		}
		SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
#ifdef	SCSI_LOW_STATICS
		scsi_low_statics.nexus_disconnected ++;
#endif	/* SCSI_LOW_STATICS */
		break;

	case MSGPH_NULL:
		slp->sl_error |= FATALIO;

	case MSGPH_CMDC:
		if (cb != NULL)
		{
			cb->ccb_error |= slp->sl_error;
			scsi_low_done(slp, cb);
		}
		SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
		break;
	}

	scsi_low_clear_nexus(slp, ti);	
	scsi_low_start(slp);
	return 1;
}

/**************************************************************
 * cmd out pointer setup
 **************************************************************/
int
scsi_low_cmd(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{
	struct slccb *cb = ti->ti_nexus;
	
	if (cb == NULL)
	{
		/*
		 * no slccb, abort!
		 */
		slp->sl_scp.scp_cmd = (u_int8_t *) &unit_ready_cmd;
		slp->sl_scp.scp_cmdlen = sizeof(unit_ready_cmd);
		slp->sl_scp.scp_datalen = 0;
		slp->sl_scp.scp_direction = SCSI_LOW_READ;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
		scsi_low_info(slp, ti, "CMDOUT: slccb nexus not found");
	}
	else if (slp->sl_nexus_call == 0)
	{
		slp->sl_nexus_call = 1;
		(*slp->sl_funcs->scsi_low_establish_nexus) (slp, ti);
	}
	return 0;
}

/**************************************************************
 * data out pointer setup
 **************************************************************/
int
scsi_low_data(slp, ti, bp, direction)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct buf **bp;
	int direction;
{
	struct slccb *cb = ti->ti_nexus;

	if (cb == NULL)
	{
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
		scsi_low_info(slp, ti, "DATA PHASE: slccb nexus not found");
		return EINVAL;
	}

	if (direction != cb->ccb_scp.scp_direction)
	{
		scsi_low_info(slp, ti, "DATA PHASE: xfer direction mismatch");
		return EINVAL;
	}

#ifdef CAM
	*bp = NULL; /* (cb->ccb == NULL) ? NULL : cb->bp; */
#else
	*bp = (cb->xs == NULL) ? NULL : cb->xs->bp;
#endif
	return 0;
}

/**************************************************************
 * MSG_SYS 
 **************************************************************/
#define	MSGINPTR_CLR(ti) {(ti)->ti_msginptr = 0; (ti)->ti_msginlen = 0;}
#define	MSGIN_PERIOD(ti) ((ti)->ti_msgin[3])
#define	MSGIN_OFFSET(ti) ((ti)->ti_msgin[4])
#define	MSGIN_DATA_LAST	0x30

static int scsi_low_errfunc_synch __P((struct targ_info *, u_int));
static int scsi_low_errfunc_wide __P((struct targ_info *, u_int));
static int scsi_low_errfunc_identify __P((struct targ_info *, u_int));

static int scsi_low_msgfunc_synch __P((struct targ_info *));
static int scsi_low_msgfunc_wide __P((struct targ_info *));
static int scsi_low_msgfunc_identify __P((struct targ_info *));
static int scsi_low_msgfunc_user __P((struct targ_info *));
static int scsi_low_msgfunc_abort __P((struct targ_info *));

struct scsi_low_msgout_data {
	u_int	md_flags;
	u_int8_t md_msg;
	int (*md_msgfunc) __P((struct targ_info *));
	int (*md_errfunc) __P((struct targ_info *, u_int));
};

struct scsi_low_msgout_data scsi_low_msgout_data[] = {
/* 0 */	{SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_abort, NULL},
/* 1 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL},
/* 2 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL},
/* 3 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL},
/* 4 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL},
/* 5 */ {SCSI_LOW_MSG_IDENTIFY, 0, scsi_low_msgfunc_identify, scsi_low_errfunc_identify},
/* 6 */ {SCSI_LOW_MSG_SYNCH, 0, scsi_low_msgfunc_synch, scsi_low_errfunc_synch},
/* 7 */ {SCSI_LOW_MSG_WIDE, 0, scsi_low_msgfunc_wide, scsi_low_errfunc_wide},
/* 8 */ {SCSI_LOW_MSG_USER, 0, scsi_low_msgfunc_user, NULL},
/* 9 */ {SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL},
/* 10 */ {SCSI_LOW_MSG_ALL, 0},
};

static int scsi_low_msginfunc_ext __P((struct targ_info *));
static int scsi_low_synch __P((struct targ_info *));
static int scsi_low_msginfunc_msg_reject __P((struct targ_info *));
static int scsi_low_msginfunc_rejop __P((struct targ_info *));
static int scsi_low_msginfunc_rdp __P((struct targ_info *));
static int scsi_low_msginfunc_sdp __P((struct targ_info *));
static int scsi_low_msginfunc_disc __P((struct targ_info *));
static int scsi_low_msginfunc_cc __P((struct targ_info *));
static int scsi_low_msginfunc_parity __P((struct targ_info *));
static int scsi_low_msginfunc_noop __P((struct targ_info *));
static void scsi_low_retry_phase __P((struct targ_info *));

struct scsi_low_msgin_data {
	u_int md_len;
	int (*md_msgfunc) __P((struct targ_info *));
};

struct scsi_low_msgin_data scsi_low_msgin_data[] = {
/* 0 */	{1,	scsi_low_msginfunc_cc},
/* 1 */ {2,	scsi_low_msginfunc_ext},
/* 2 */ {1,	scsi_low_msginfunc_sdp},
/* 3 */ {1,	scsi_low_msginfunc_rdp},
/* 4 */ {1,	scsi_low_msginfunc_disc},
/* 5 */ {1,	scsi_low_msginfunc_rejop},
/* 6 */ {1,	scsi_low_msginfunc_rejop},
/* 7 */ {1,	scsi_low_msginfunc_msg_reject},
/* 8 */ {1,	scsi_low_msginfunc_noop},
/* 9 */ {1,	scsi_low_msginfunc_parity},
/* a */ {1,	scsi_low_msginfunc_rejop},
/* b */ {1,	scsi_low_msginfunc_rejop},
/* c */ {1,	scsi_low_msginfunc_rejop},
/* d */ {2,	scsi_low_msginfunc_rejop},
/* e */ {1,	scsi_low_msginfunc_rejop},
/* f */ {1,	scsi_low_msginfunc_rejop},
/* 0x10 */ {1,	scsi_low_msginfunc_rejop},
/* 0x11 */ {1,	scsi_low_msginfunc_rejop},
/* 0x12 */ {1,	scsi_low_msginfunc_rejop},
/* 0x13 */ {1,	scsi_low_msginfunc_rejop},
/* 0x14 */ {1,	scsi_low_msginfunc_rejop},
/* 0x15 */ {1,	scsi_low_msginfunc_rejop},
/* 0x16 */ {1,	scsi_low_msginfunc_rejop},
/* 0x17 */ {1,	scsi_low_msginfunc_rejop},
/* 0x18 */ {1,	scsi_low_msginfunc_rejop},
/* 0x19 */ {1,	scsi_low_msginfunc_rejop},
/* 0x1a */ {1,	scsi_low_msginfunc_rejop},
/* 0x1b */ {1,	scsi_low_msginfunc_rejop},
/* 0x1c */ {1,	scsi_low_msginfunc_rejop},
/* 0x1d */ {1,	scsi_low_msginfunc_rejop},
/* 0x1e */ {1,	scsi_low_msginfunc_rejop},
/* 0x1f */ {1,	scsi_low_msginfunc_rejop},
/* 0x20 */ {2,	scsi_low_msginfunc_rejop},
/* 0x21 */ {2,	scsi_low_msginfunc_rejop},
/* 0x22 */ {2,	scsi_low_msginfunc_rejop},
/* 0x23 */ {2,	scsi_low_msginfunc_rejop},
/* 0x24 */ {2,	scsi_low_msginfunc_rejop},
/* 0x25 */ {2,	scsi_low_msginfunc_rejop},
/* 0x26 */ {2,	scsi_low_msginfunc_rejop},
/* 0x27 */ {2,	scsi_low_msginfunc_rejop},
/* 0x28 */ {2,	scsi_low_msginfunc_rejop},
/* 0x29 */ {2,	scsi_low_msginfunc_rejop},
/* 0x2a */ {2,	scsi_low_msginfunc_rejop},
/* 0x2b */ {2,	scsi_low_msginfunc_rejop},
/* 0x2c */ {2,	scsi_low_msginfunc_rejop},
/* 0x2d */ {2,	scsi_low_msginfunc_rejop},
/* 0x2e */ {2,	scsi_low_msginfunc_rejop},
/* 0x2f */ {2,	scsi_low_msginfunc_rejop},
/* 0x30 */ {1,	scsi_low_msginfunc_rejop}	/* default rej op */
};

static void
scsi_low_init_msgsys(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{

	ti->ti_msginptr = 0;
	ti->ti_emsgflags = ti->ti_msgflags = ti->ti_omsgflags = 0;
	ti->ti_tflags &= ~TARG_ASSERT_ATN;
	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
}

/**************************************************************
 * msgout
 **************************************************************/
static int
scsi_low_msgfunc_synch(ti)
	struct targ_info *ti;
{
	struct lun_info *li = ti->ti_li;
	int ptr = ti->ti_msgoutlen;

	if (li == NULL)
	{
		scsi_low_assert_msg(ti->ti_sc, ti, SCSI_LOW_MSG_ABORT, 0);
		return EINVAL;
	}

	ti->ti_msgoutstr[ptr + 0] = MSG_EXTEND;
	ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_SYNCHLEN;
	ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_SYNCHCODE;
	ti->ti_msgoutstr[ptr + 3] = li->li_maxsynch.period;
	ti->ti_msgoutstr[ptr + 4] = li->li_maxsynch.offset;
	return MSG_EXTEND_SYNCHLEN + 2;
}

static int
scsi_low_msgfunc_wide(ti)
	struct targ_info *ti;
{
	struct lun_info *li = ti->ti_li;
	int ptr = ti->ti_msgoutlen;

	if (li == NULL)
	{
		scsi_low_assert_msg(ti->ti_sc, ti, SCSI_LOW_MSG_ABORT, 0);
		return EINVAL;
	}

	ti->ti_msgoutstr[ptr + 0] = MSG_EXTEND;
	ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
	ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
	ti->ti_msgoutstr[ptr + 3] = li->li_width;
	return MSG_EXTEND_WIDELEN + 2;
}

static int
scsi_low_msgfunc_identify(ti)
	struct targ_info *ti;
{
	int ptr = ti->ti_msgoutlen;;

	if (ti->ti_li == NULL)
	{
		ti->ti_msgoutstr[ptr + 0] = 0x80;
		scsi_low_info(ti->ti_sc, ti, "MSGOUT: lun unknown");
		scsi_low_assert_msg(ti->ti_sc, ti, SCSI_LOW_MSG_ABORT, 0);
	}
	else
	{
		ti->ti_msgoutstr[ptr + 0] = ID_MSG_SETUP(ti);
	}
	return 1;
}

static int
scsi_low_msgfunc_user(ti)
	struct targ_info *ti;
{
#ifdef	SCSI_LOW_SUPPORT_USER_MSGOUT
	struct slccb *cb = ti->ti_nexus;
	int ptr = ti->ti_msgoutlen;;

	if (ti->ti_nexus == NULL)
	{
		ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
		return 1;
	}
	else
	{
		bcopy(cb->msgout, ti->ti_msgoutstr + ptr, SCSI_LOW_MAX_MSGLEN);
		return cb->msgoutlen;
	}
#else	/* !SCSI_LOW_SUPPORT_USER_MSGOUT */
	return 0;
#endif	/* !SCSI_LOW_SUPPORT_USER_MSGOUT */
}

static int
scsi_low_msgfunc_abort(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	/* The target should releases bus */
	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
	slp->sl_error |= /* ABORTIO */ FATALIO;
	return 1;
}

/*
 * The following functions are called when targets give unexpected
 * responces in msgin (after msgout).
 */
static int
scsi_low_errfunc_identify(ti, msgflags)
	struct targ_info *ti;
	u_int msgflags;
{
	struct lun_info *li = ti->ti_li;

	li->li_flags &= ~SCSI_LOW_DISC;
	return 0;
}

static int
scsi_low_errfunc_synch(ti, msgflags)
	struct targ_info *ti;
	u_int msgflags;
{

	/* XXX:
	 * illegal behavior, however
	 * there are buggy devices!
	 */
	MSGIN_PERIOD(ti) = 0;
	MSGIN_OFFSET(ti) = 0;
	scsi_low_synch(ti);
	return 0;
}

static int
scsi_low_errfunc_wide(ti, msgflags)
	struct targ_info *ti;
	u_int msgflags;
{
	struct lun_info *li = ti->ti_li;

	li->li_width = 0;
	return 0;
}

int
scsi_low_msgout(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{
	struct scsi_low_msgout_data *mdp;
	int len = 0;

	/* STEP I.
	 * Scsi phase changes.
	 * Previously msgs asserted are accepted by our target or
	 * processed by scsi_low_msgin.
	 * Thus clear all saved informations.
	 */
	if (ti->ti_ophase != ti->ti_phase)
	{
		ti->ti_omsgflags = 0;
		ti->ti_emsgflags = 0;
	}

	/* STEP II.
	 * We did not assert attention, however still our target required
	 * msgs. Resend previous msgs. 
	 */
	if (ti->ti_ophase == PH_MSGOUT && !(ti->ti_tflags & TARG_ASSERT_ATN))
	{
		ti->ti_msgflags |= ti->ti_omsgflags;
#ifdef	SCSI_LOW_DIAGNOSTIC
		printf("%s: scsi_low_msgout: retry msgout\n", slp->sl_xname);
#endif	/* SCSI_LOW_DIAGNOSTIC */
	}

	/*
	 * OK. clear flags.
	 */
	ti->ti_tflags &= ~TARG_ASSERT_ATN;

	/* STEP III.
	 * We have no msgs. send MSG_LOOP (OK?)
	 */
	if (scsi_low_is_msgout_continue(ti) == 0)
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_NOOP, 0);

	/* STEP IV.
	 * Process all msgs
	 */
	ti->ti_msgoutlen = 0;
	mdp = &scsi_low_msgout_data[0];
	for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
	{
		if ((ti->ti_msgflags & mdp->md_flags) != 0)
		{
			ti->ti_omsgflags |= mdp->md_flags;
			ti->ti_msgflags &= ~mdp->md_flags;
			ti->ti_emsgflags = mdp->md_flags;

			ti->ti_msgoutstr[ti->ti_msgoutlen] = mdp->md_msg;
			if (mdp->md_msgfunc != NULL)
				len = (*mdp->md_msgfunc) (ti);
			else
				len = 1;

			ti->ti_msgoutlen += len;
			if ((slp->sl_cfgflags & CFG_MSGUNIFY) == 0 || 
			    ti->ti_msgflags == 0)
				break;
			if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
				break;
		}
	}

	if (scsi_low_is_msgout_continue(ti) != 0)
	{
#ifdef	SCSI_LOW_DIAGNOSTIC
		printf("SCSI_LOW_ATTENTION(msgout): 0x%x\n", ti->ti_msgflags);
#endif	/* SCSI_LOW_DIAGNOSTIC */
		scsi_low_attention(slp, ti);
	}

	/*
	 * OK. advance old phase.
	 */
	ti->ti_ophase = ti->ti_phase;
	return ti->ti_msgoutlen;
}

/**************************************************************
 * msgin
 **************************************************************/
static int
scsi_low_msginfunc_noop(ti)
	struct targ_info *ti;
{

	return 0;
}

static int
scsi_low_msginfunc_rejop(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	u_int8_t msg = ti->ti_msgin[0];

	printf("%s: MSGIN: msg 0x%x reject\n", slp->sl_xname, (u_int) msg);
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

static int
scsi_low_msginfunc_cc(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);
	return 0;
}

static int
scsi_low_msginfunc_disc(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
	return 0;
}

static int
scsi_low_msginfunc_sdp(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	if (ti->ti_nexus != NULL)
		ti->ti_nexus->ccb_sscp = slp->sl_scp;
	else
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

static int
scsi_low_msginfunc_rdp(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	if (ti->ti_nexus != NULL)
		slp->sl_scp = ti->ti_nexus->ccb_sscp;
	else
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

static int
scsi_low_synch(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	struct lun_info *li = ti->ti_li;
	u_int period = 0, offset = 0, speed;
	u_char *s;
	int error;

	if (MSGIN_PERIOD(ti) >= li->li_maxsynch.period &&
	    MSGIN_OFFSET(ti) <= li->li_maxsynch.offset)
	{
		if ((offset = MSGIN_OFFSET(ti)) != 0)
			period = MSGIN_PERIOD(ti);
		s = offset ? "synchronous" : "async";
	}
	else
	{
		/* XXX:
		 * Target seems to be brain damaged.
		 * Force async transfer.
		 */
		li->li_maxsynch.period = 0;
		li->li_maxsynch.offset = 0;
		printf("%s: target brain damaged. async transfer\n",
			slp->sl_xname);
		return EINVAL;
	}

	li->li_maxsynch.period = period;
	li->li_maxsynch.offset = offset;

	error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_SYNCH);
	if (error != 0)
	{
		/* XXX:
		 * Current period and offset are not acceptable 
		 * for our adapter.
		 * The adapter changes max synch and max offset.
		 */
		printf("%s: synch neg failed. retry synch msg neg ...\n",
			slp->sl_xname);
		return error;
	}

#ifdef SCSI_LOW_INFORM
	/* inform data */
	printf("%s(%d:%d): <%s> offset %d period %dns ",
		slp->sl_xname, ti->ti_id, li->li_lun, s, offset, period * 4);
	if (period != 0)
	{
		speed = 1000 * 10 / (period * 4);
		printf("%d.%d M/s", speed / 10, speed % 10);
	}
	printf("\n");
#endif

	return 0;
}

static int
scsi_low_msginfunc_ext(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	struct slccb *cb = ti->ti_nexus;
	struct lun_info *li = ti->ti_li;
	int count, retry;
	u_int32_t *ptr;

	if (ti->ti_msginptr == 2)
	{
		ti->ti_msginlen = ti->ti_msgin[1] + 2;
		return 0;
	}

	switch (MKMSG_EXTEND(ti->ti_msgin[1], ti->ti_msgin[2]))
	{
	case MKMSG_EXTEND(MSG_EXTEND_MDPLEN, MSG_EXTEND_MDPCODE):
		if (cb == NULL)
			break;

		ptr = (u_int32_t *)(&ti->ti_msgin[3]);
		count = (int) htonl((long) (*ptr));
		if(slp->sl_scp.scp_datalen - count < 0 || 
		   slp->sl_scp.scp_datalen - count > cb->ccb_scp.scp_datalen)
			break;

		slp->sl_scp.scp_datalen -= count;
		slp->sl_scp.scp_data += count;
		return 0;

	case MKMSG_EXTEND(MSG_EXTEND_SYNCHLEN, MSG_EXTEND_SYNCHCODE):
		if (li == NULL)
			break;

		retry = scsi_low_synch(ti);
		if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);
		return 0;

	case MKMSG_EXTEND(MSG_EXTEND_WIDELEN, MSG_EXTEND_WIDECODE):
		if (li == NULL)
			break;

		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_WIDE, 0);
		return 0;

	default:
		break;
	}

	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return EINVAL;
}

static void
scsi_low_retry_phase(ti)
	struct targ_info *ti;
{

	switch (ti->ti_sphase)
	{
	case PH_MSGOUT:
		ti->ti_msgflags |= ti->ti_omsgflags;	
		break;

	default:
		break;
	}
}

static int
scsi_low_msginfunc_parity(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;

	if (ti->ti_sphase != PH_MSGOUT)
		slp->sl_error |= PARITYERR;
	scsi_low_retry_phase(ti);
	return 0;
}

static int
scsi_low_msginfunc_msg_reject(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	struct lun_info *li = ti->ti_li;
	struct scsi_low_msgout_data *mdp;
	u_int msgflags;

	if (li == NULL)
	{
		/* not yet lun nexus established! */
		goto out;
	}

	switch (ti->ti_sphase)
	{
	case PH_CMD:
		slp->sl_error |= CMDREJECT;
		break;

	case PH_MSGOUT:
		if (ti->ti_emsgflags == 0)
			break;

		msgflags = SCSI_LOW_MSG_REJECT;
		mdp = &scsi_low_msgout_data[0];
		for ( ; mdp->md_flags != SCSI_LOW_MSG_ALL; mdp ++)
		{
			if ((ti->ti_emsgflags & mdp->md_flags) != 0)
			{
				ti->ti_emsgflags &= ~mdp->md_flags;
				if (mdp->md_errfunc != NULL)
					(*mdp->md_errfunc) (ti, msgflags);
				break;
			}
		}
		break;

	default:
		break;
	}

out:
	scsi_low_info(slp, ti, "msg rejected");
	slp->sl_error |= MSGERR;
	return 0;
}

void
scsi_low_msgin(slp, ti, c)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	u_int8_t c;
{
	struct scsi_low_msgin_data *sdp;
	struct lun_info *li;
	u_int8_t msg;

	/*
	 * Phase changes, clear the pointer.
	 */
	if (ti->ti_ophase != ti->ti_phase)
	{
		ti->ti_sphase = ti->ti_ophase;
		ti->ti_ophase = ti->ti_phase;
		MSGINPTR_CLR(ti);
#ifdef	SCSI_LOW_DIAGNOSTIC
		ti->ti_msgin_hist_pointer = 0;
#endif	/* SCSI_LOW_DIAGNOSTIC */
	}

	/*
	 * Store a current messages byte into buffer and 
	 * wait for the completion of the current msg.
	 */
	ti->ti_msgin[ti->ti_msginptr ++] = c;
	if (ti->ti_msginptr >= SCSI_LOW_MAX_MSGLEN)
	{
		ti->ti_msginptr = SCSI_LOW_MAX_MSGLEN - 1;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	}	

	/*
	 * Calculate messages length.
	 */
	msg = ti->ti_msgin[0];
	if (msg < MSGIN_DATA_LAST)
		sdp = &scsi_low_msgin_data[msg];
	else
		sdp = &scsi_low_msgin_data[MSGIN_DATA_LAST];

	if (ti->ti_msginlen == 0)
	{
		ti->ti_msginlen = sdp->md_len;
#ifdef	SCSI_LOW_DIAGNOSTIC
	    	if (ti->ti_msgin_hist_pointer < MSGIN_HISTORY_LEN)
		{
			ti->ti_msgin_history[ti->ti_msgin_hist_pointer] = msg;
			ti->ti_msgin_hist_pointer ++;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */
	}

	/*
	 * Check comletion.
	 */
	if (ti->ti_msginptr < ti->ti_msginlen)
		return;

	/*
	 * Do process.
	 */
	if ((msg & MSG_IDENTIFY) == 0)
	{
		(void) ((*sdp->md_msgfunc) (ti));
	}
	else
	{
		li = ti->ti_li;
		if (li == NULL)
		{
			li = scsi_low_establish_lun(ti, MSGCMD_LUN(msg));
			if (li == NULL)
				goto badlun;
		}	

		if (ti->ti_nexus == NULL)
		{
			/* XXX:
		 	 * move the following functions to
			 * tag queue msg process in the future.
			 */
			if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
				goto badlun;
		}

		if (MSGCMD_LUN(msg) != li->li_lun)
			goto badlun;
	}

	/*
	 * Msg process completed, reset msin pointer and assert ATN if desired.
	 */
	if (ti->ti_msginptr >= ti->ti_msginlen)
	{
		ti->ti_sphase = ti->ti_phase;
		MSGINPTR_CLR(ti);

		if (scsi_low_is_msgout_continue(ti) != 0)
		{
#ifdef	SCSI_LOW_DIAGNOSTIC
			printf("SCSI_LOW_ATTETION(msgin): 0x%x\n", 
				ti->ti_msgflags);
#endif	/* SCSI_LOW_DIAGNOSTIC */
			scsi_low_attention(slp, ti);
		}
	}
	return;

badlun:
	scsi_low_info(slp, ti, "MSGIN: identify lun mismatch");
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
}

/**************************************************************
 * Qurik setup
 **************************************************************/
#define	MAXOFFSET	0x10

static void
scsi_low_calcf(ti, li)
	struct targ_info *ti;
	struct lun_info *li;
{
	u_int period;
	u_int8_t offset;
	struct scsi_low_softc *slp = ti->ti_sc;

	li->li_flags &= ~SCSI_LOW_DISC;
	if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
#ifdef	SDEV_NODISC
	    (li->li_quirks & SDEV_NODISC) == 0 &&
#endif	/* SDEV_NODISC */
	    (li->li_cfgflags & SCSI_LOW_DISC) != 0)
		li->li_flags |= SCSI_LOW_DISC;

	li->li_flags |= SCSI_LOW_NOPARITY;
	if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
#ifdef	SDEV_NOPARITY
	    (li->li_quirks & SDEV_NOPARITY) == 0 &&
#endif	/* SDEV_NOPARITY */
	    (li->li_cfgflags & SCSI_LOW_NOPARITY) == 0)
		li->li_flags &= ~SCSI_LOW_NOPARITY;

	li->li_flags &= ~SCSI_LOW_SYNC;
	if ((li->li_cfgflags & SCSI_LOW_SYNC) &&
	    (slp->sl_cfgflags & CFG_ASYNC) == 0)
	{
		offset = SCSI_LOW_OFFSET(li->li_cfgflags);
		if (offset > li->li_maxsynch.offset)
			offset = li->li_maxsynch.offset;
		li->li_flags |= SCSI_LOW_SYNC;
	}
	else
		offset = 0;
	
	if (offset > 0)
	{
		period = SCSI_LOW_PERIOD(li->li_cfgflags);
		if (period > SCSI_LOW_MAX_SYNCH_SPEED)
			period = SCSI_LOW_MAX_SYNCH_SPEED;
		if (period != 0)
			period = 1000 * 10 / (period * 4);
		if (period < li->li_maxsynch.period)
			period = li->li_maxsynch.period;
	}
	else
		period = 0;

	li->li_maxsynch.offset = offset;
	li->li_maxsynch.period = period;
}

#ifdef	SCSI_LOW_TARGET_OPEN
static int
scsi_low_target_open(link, cf)
	struct scsipi_link *link;
	struct cfdata *cf;
{
	u_int target = link->scsipi_scsi.target;
	u_int lun = link->scsipi_scsi.lun;
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;

	slp = (struct scsi_low_softc *) link->adapter_softc;
	ti = slp->sl_ti[target];
	li = scsi_low_alloc_li(ti, lun, 0);
	if (li == NULL)
		return 0;

	li->li_quirks = (u_int) link->quirks;
	li->li_cfgflags = cf->cf_flags;
	if (li->li_state > UNIT_SYNCH)
		li->li_state = UNIT_SYNCH;

	scsi_low_calcf(ti, li);

	printf("%s(%d:%d): max period(%dns) max offset(%d) flags 0x%b\n",
		slp->sl_xname, target, lun,
		li->li_maxsynch.period * 4,
		li->li_maxsynch.offset,
		li->li_flags, SCSI_LOW_BITS);
	return 0;
}
#endif	/* SCSI_LOW_TARGET_OPEN */

/**********************************************************
 * DEBUG SECTION
 **********************************************************/
static void
scsi_low_info(slp, ti, s)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	u_char *s;
{

	printf("%s: SCSI_LOW: %s\n", slp->sl_xname, s);
	if (ti == NULL)
	{
		for (ti = slp->sl_titab.tqh_first; ti != NULL;
		     ti = ti->ti_chain.tqe_next)
			scsi_low_print(slp, ti);
	}
	else
		scsi_low_print(slp, ti);
			
}

static u_char *phase[] =
{
	"FREE", "ARBSTART", "SELSTART", "SELECTED",
	"CMDOUT", "DATA", "MSGIN", "MSGOUT", "STATIN", "DISC", "RESEL"
};

void
scsi_low_print(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{
	struct slccb *cb = NULL;

	if (ti == NULL)
		ti = slp->sl_nexus;
	if (ti != NULL)
		cb = ti->ti_nexus;

	printf("%s: TARGET(0x%lx) T_NEXUS(0x%lx) C_NEXUS(0x%lx) NDISCS(%d)\n",
	       slp->sl_xname, (u_long) ti, (u_long) slp->sl_nexus,
	       (u_long) cb, slp->sl_disc);

	/* target stat */
	if (ti != NULL)
	{
		struct sc_p *sp = &slp->sl_scp;
		struct lun_info *li = ti->ti_li;
		u_int flags = 0;
		int lun = -1;

		if (li != NULL)
		{
			lun = li->li_lun;
			flags = li->li_flags;
		}

		printf("%s(%d:%d) ph<%s> => ph<%s>\n", slp->sl_xname,
		       ti->ti_id, lun, phase[(int) ti->ti_ophase], 
		       phase[(int) ti->ti_phase]);

printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] STATUSIN: 0x%x T_FLAGS: 0x%x\n",
		       (u_int) (ti->ti_msginptr), 
		       (u_int) (ti->ti_msgin[0]),
		       (u_int) (ti->ti_msgin[1]),
		       (u_int) (ti->ti_msgin[2]),
		       (u_int) (ti->ti_msgin[3]),
		       (u_int) (ti->ti_msgin[4]),
		       ti->ti_status, ti->ti_tflags);
#ifdef	SCSI_LOW_DIAGNOSTIC
printf("MSGIN HISTORY: (%d) [0x%x] => [0x%x] => [0x%x] => [0x%x] => [0x%x]\n",
			ti->ti_msgin_hist_pointer,
			(u_int) (ti->ti_msgin_history[0]),
			(u_int) (ti->ti_msgin_history[1]),
			(u_int) (ti->ti_msgin_history[2]),
			(u_int) (ti->ti_msgin_history[3]),
			(u_int) (ti->ti_msgin_history[4]));
#endif	/* SCSI_LOW_DIAGNOSTIC */

printf("MSGOUT: msgflags 0x%x [%x][%x][%x][%x][%x] msgoutlen %d C_FLAGS: %b\n",
			(u_int) ti->ti_msgflags,
			(u_int) (ti->ti_msgoutstr[0]), 
			(u_int) (ti->ti_msgoutstr[1]), 
			(u_int) (ti->ti_msgoutstr[2]), 
			(u_int) (ti->ti_msgoutstr[3]), 
			(u_int) (ti->ti_msgoutstr[4]), 
		        ti->ti_msgoutlen,
			flags, SCSI_LOW_BITS);

printf("SCP: datalen 0x%x dataaddr 0x%lx ",
			sp->scp_datalen,
			(u_long) sp->scp_data);

		if (cb != NULL)
		{
printf("CCB: cmdlen %x cmdaddr %lx cmd[0] %x datalen %x",
		       cb->ccb_scp.scp_cmdlen, 
		       (u_long) cb->ccb_scp.scp_cmd,
		       (u_int) cb->ccb_scp.scp_cmd[0],
		       cb->ccb_scp.scp_datalen);
		}
		printf("\n");
	}
	printf("error flags %b\n", slp->sl_error, SCSI_LOW_ERRORBITS);
}
OpenPOWER on IntegriCloud