summaryrefslogtreecommitdiffstats
path: root/sys/cam/scsi/scsi_low.c
blob: 8ca669e015ebd3155d5ea071b38b2c538a1703bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
/*	$NecBSD: scsi_low.c,v 1.24.10.8 2001/06/26 07:39:44 honda Exp $	*/
/*	$NetBSD$	*/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#define	SCSI_LOW_STATICS
#define	SCSI_LOW_DEBUG
#define SCSI_LOW_NEGOTIATE_BEFORE_SENSE
#define	SCSI_LOW_START_UP_CHECK

/* #define	SCSI_LOW_INFO_DETAIL */

/* #define	SCSI_LOW_QCLEAR_AFTER_CA */
/* #define	SCSI_LOW_FLAGS_QUIRKS_OK */

#define	SCSI_LOW_FLAGS_QUIRKS_OK

/*-
 * [NetBSD for NEC PC-98 series]
 *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
 *	NetBSD/pc98 porting staff. All rights reserved.
 *  Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
 *	Naofumi HONDA. All rights reserved.
 *
 * [Ported for FreeBSD CAM]
 *  Copyright (c) 2000, 2001
 *      MITSUNAGA Noriaki, NOKUBI Hirotaka and TAKAHASHI Yoshihiro.
 *      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_Tnexus) 
 * 2) Identify msgin => lun nexus (slp->sl_Lnexus)
 * 3) Qtag msg => ccb nexus (slp->sl_Qnexus)
 */
#include "opt_ddb.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/errno.h>

#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_sim.h>
#include <cam/cam_debug.h>
#include <cam/cam_periph.h>
#include <cam/cam_xpt_periph.h>

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

#include <cam/scsi/scsi_low.h>

#include <sys/cons.h>

/**************************************************************
 * Constants
 **************************************************************/
#define	SCSI_LOW_POLL_HZ	1000

/* functions return values */
#define	SCSI_LOW_START_NO_QTAG	0
#define	SCSI_LOW_START_QTAG	1

#define	SCSI_LOW_DONE_COMPLETE	0
#define	SCSI_LOW_DONE_RETRY	1

/* internal disk flags */
#define	SCSI_LOW_DISK_DISC	0x00000001
#define	SCSI_LOW_DISK_QTAG	0x00000002
#define	SCSI_LOW_DISK_LINK	0x00000004
#define	SCSI_LOW_DISK_PARITY	0x00000008
#define	SCSI_LOW_DISK_SYNC	0x00010000
#define	SCSI_LOW_DISK_WIDE_16	0x00020000
#define	SCSI_LOW_DISK_WIDE_32	0x00040000
#define	SCSI_LOW_DISK_WIDE	(SCSI_LOW_DISK_WIDE_16 | SCSI_LOW_DISK_WIDE_32)
#define	SCSI_LOW_DISK_LFLAGS	0x0000ffff
#define	SCSI_LOW_DISK_TFLAGS	0xffff0000

static MALLOC_DEFINE(M_SCSILOW, "SCSI low", "SCSI low buffers");

/**************************************************************
 * Declarations
 **************************************************************/
/* static */ void scsi_low_info(struct scsi_low_softc *, struct targ_info *, u_char *);
static void scsi_low_engage(void *);
static struct slccb *scsi_low_establish_ccb(struct targ_info *, struct lun_info *, scsi_low_tag_t);
static int scsi_low_done(struct scsi_low_softc *, struct slccb *);
static int scsi_low_setup_done(struct scsi_low_softc *, struct slccb *);
static void scsi_low_bus_release(struct scsi_low_softc *, struct targ_info *);
static void scsi_low_twiddle_wait(void);
static struct lun_info *scsi_low_alloc_li(struct targ_info *, int, int);
static struct targ_info *scsi_low_alloc_ti(struct scsi_low_softc *, int);
static void scsi_low_calcf_lun(struct lun_info *);
static void scsi_low_calcf_target(struct targ_info *);
static void scsi_low_calcf_show(struct lun_info *);
static void scsi_low_reset_nexus(struct scsi_low_softc *, int);
static void scsi_low_reset_nexus_target(struct scsi_low_softc *, struct targ_info *, int);
static void scsi_low_reset_nexus_lun(struct scsi_low_softc *, struct lun_info *, int);
static int scsi_low_init(struct scsi_low_softc *, u_int);
static void scsi_low_start(struct scsi_low_softc *);
static void scsi_low_free_ti(struct scsi_low_softc *);

static int scsi_low_alloc_qtag(struct slccb *);
static int scsi_low_dealloc_qtag(struct slccb *);
static int scsi_low_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *, u_int, u_int);
static int scsi_low_message_enqueue(struct scsi_low_softc *, struct targ_info *, struct lun_info *, u_int);
static void scsi_low_unit_ready_cmd(struct slccb *);
static void scsi_low_timeout(void *);
static int scsi_low_timeout_check(struct scsi_low_softc *);
#ifdef	SCSI_LOW_START_UP_CHECK
static int scsi_low_start_up(struct scsi_low_softc *);
#endif	/* SCSI_LOW_START_UP_CHECK */
static int scsi_low_abort_ccb(struct scsi_low_softc *, struct slccb *);
static struct slccb *scsi_low_revoke_ccb(struct scsi_low_softc *, struct slccb *, int);

int scsi_low_version_major = 2;
int scsi_low_version_minor = 17;

static struct scsi_low_softc_tab sl_tab = LIST_HEAD_INITIALIZER(sl_tab);

/**************************************************************
 * Debug, Run test and Statics
 **************************************************************/
#ifdef	SCSI_LOW_INFO_DETAIL
#define	SCSI_LOW_INFO(slp, ti, s) scsi_low_info((slp), (ti), (s))
#else	/* !SCSI_LOW_INFO_DETAIL */
#define	SCSI_LOW_INFO(slp, ti, s) device_printf((slp)->sl_dev, "%s\n", (s))
#endif	/* !SCSI_LOW_INFO_DETAIL */

#ifdef	SCSI_LOW_STATICS
static 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 */

#ifdef	SCSI_LOW_DEBUG
#define	SCSI_LOW_DEBUG_DONE	0x00001
#define	SCSI_LOW_DEBUG_DISC	0x00002
#define	SCSI_LOW_DEBUG_SENSE	0x00004
#define	SCSI_LOW_DEBUG_CALCF	0x00008
#define	SCSI_LOW_DEBUG_ACTION	0x10000
int scsi_low_debug = 0;

#define	SCSI_LOW_MAX_ATTEN_CHECK	32
#define	SCSI_LOW_ATTEN_CHECK	0x0001
#define	SCSI_LOW_CMDLNK_CHECK	0x0002
#define	SCSI_LOW_ABORT_CHECK	0x0004
#define	SCSI_LOW_NEXUS_CHECK	0x0008
int scsi_low_test = 0;
int scsi_low_test_id = 0;

static void scsi_low_test_abort(struct scsi_low_softc *, struct targ_info *, struct lun_info *);
static void scsi_low_test_cmdlnk(struct scsi_low_softc *, struct slccb *);
static void scsi_low_test_atten(struct scsi_low_softc *, struct targ_info *, u_int);
#define	SCSI_LOW_DEBUG_TEST_GO(fl, id) \
	((scsi_low_test & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
#define	SCSI_LOW_DEBUG_GO(fl, id) \
	((scsi_low_debug & (fl)) != 0 && (scsi_low_test_id & (1 << (id))) == 0)
#endif	/* SCSI_LOW_DEBUG */

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

/**************************************************************
 * Inline functions
 **************************************************************/
#define	SCSI_LOW_INLINE	static __inline
SCSI_LOW_INLINE void scsi_low_activate_qtag(struct slccb *);
SCSI_LOW_INLINE void scsi_low_deactivate_qtag(struct slccb *);
SCSI_LOW_INLINE void scsi_low_ccb_message_assert(struct slccb *, u_int);
SCSI_LOW_INLINE void scsi_low_ccb_message_exec(struct scsi_low_softc *, struct slccb *);
SCSI_LOW_INLINE void scsi_low_ccb_message_retry(struct slccb *);
SCSI_LOW_INLINE void scsi_low_ccb_message_clear(struct slccb *);
SCSI_LOW_INLINE void scsi_low_init_msgsys(struct scsi_low_softc *, struct targ_info *);

SCSI_LOW_INLINE void
scsi_low_activate_qtag(cb)
	struct slccb *cb;
{
	struct lun_info *li = cb->li;

	if (cb->ccb_tag != SCSI_LOW_UNKTAG)
		return;

	li->li_nqio ++;
	cb->ccb_tag = cb->ccb_otag;
}
	
SCSI_LOW_INLINE void
scsi_low_deactivate_qtag(cb)
	struct slccb *cb;
{
	struct lun_info *li = cb->li;

	if (cb->ccb_tag == SCSI_LOW_UNKTAG)
		return;

	li->li_nqio --;
	cb->ccb_tag = SCSI_LOW_UNKTAG;
}
	
SCSI_LOW_INLINE void
scsi_low_ccb_message_exec(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{

	scsi_low_assert_msg(slp, cb->ti, cb->ccb_msgoutflag, 0);
	cb->ccb_msgoutflag = 0;
}

SCSI_LOW_INLINE void
scsi_low_ccb_message_assert(cb, msg)
	struct slccb *cb;
	u_int msg;
{

	cb->ccb_msgoutflag = cb->ccb_omsgoutflag = msg;
}

SCSI_LOW_INLINE void
scsi_low_ccb_message_retry(cb)
	struct slccb *cb;
{
	cb->ccb_msgoutflag = cb->ccb_omsgoutflag;
}

SCSI_LOW_INLINE void
scsi_low_ccb_message_clear(cb)
	struct slccb *cb;
{
	cb->ccb_msgoutflag = 0;
}

SCSI_LOW_INLINE 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;
	SCSI_LOW_DEASSERT_ATN(slp);
	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_NULL);
}

/*=============================================================
 * START OF OS switch  (All OS depend fucntions should be here)
 =============================================================*/
/* common os depend utitlities */
#define	SCSI_LOW_CMD_RESIDUAL_CHK	0x0001
#define	SCSI_LOW_CMD_ORDERED_QTAG	0x0002
#define	SCSI_LOW_CMD_ABORT_WARNING	0x0004

static u_int8_t scsi_low_cmd_flags[256] = {
/*	0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f */
/*0*/	0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0,
/*1*/	0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0,
/*2*/	0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 5, 5,
/*3*/	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5,
};

struct scsi_low_error_code {
	int error_bits;
	int error_code;
};

static struct slccb *scsi_low_find_ccb(struct scsi_low_softc *, u_int, u_int, void *);
static int scsi_low_translate_error_code(struct slccb *, struct scsi_low_error_code *);

static struct slccb *
scsi_low_find_ccb(slp, target, lun, osdep)
	struct scsi_low_softc *slp;
	u_int target, lun;
	void *osdep;
{
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;

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

	if ((cb = slp->sl_Qnexus) != NULL && cb->osdep == osdep)
		return cb;

	TAILQ_FOREACH(cb, &slp->sl_start, ccb_chain)
	{
		if (cb->osdep == osdep)
			return cb;
	}

	TAILQ_FOREACH(cb, &li->li_discq, ccb_chain)
	{
		if (cb->osdep == osdep)
			return cb;
	}
	return NULL;
}

static int 
scsi_low_translate_error_code(cb, tp)
	struct slccb *cb;
	struct scsi_low_error_code *tp;
{

	if (cb->ccb_error == 0)
		return tp->error_code;

	for (tp ++; (cb->ccb_error & tp->error_bits) == 0; tp ++)
		;
	return tp->error_code;
}

/**************************************************************
 * SCSI INTERFACE (CAM)
 **************************************************************/
#define	SCSI_LOW_MALLOC(size)		malloc((size), M_SCSILOW, M_NOWAIT)
#define	SCSI_LOW_FREE(pt)		free((pt), M_SCSILOW)
#define	SCSI_LOW_ALLOC_CCB(flags)	scsi_low_get_ccb()

static void scsi_low_poll_cam(struct cam_sim *);
void scsi_low_scsi_action_cam(struct cam_sim *, union ccb *);

static int scsi_low_attach_cam(struct scsi_low_softc *);
static int scsi_low_world_start_cam(struct scsi_low_softc *);
static int scsi_low_dettach_cam(struct scsi_low_softc *);
static int scsi_low_ccb_setup_cam(struct scsi_low_softc *, struct slccb *);
static int scsi_low_done_cam(struct scsi_low_softc *, struct slccb *);
static void scsi_low_timeout_cam(struct scsi_low_softc *, int, int);

struct scsi_low_osdep_funcs scsi_low_osdep_funcs_cam = {
	scsi_low_attach_cam,
	scsi_low_world_start_cam,
	scsi_low_dettach_cam,
	scsi_low_ccb_setup_cam,
	scsi_low_done_cam,
	scsi_low_timeout_cam
};
	
struct scsi_low_error_code scsi_low_error_code_cam[] = {
	{0,			CAM_REQ_CMP},
	{SENSEIO, 		CAM_AUTOSNS_VALID | CAM_REQ_CMP_ERR},
	{SENSEERR,		CAM_AUTOSENSE_FAIL},
	{UACAERR,		CAM_SCSI_STATUS_ERROR},
	{BUSYERR | STATERR,	CAM_SCSI_STATUS_ERROR},
	{SELTIMEOUTIO,		CAM_SEL_TIMEOUT},	
	{TIMEOUTIO,		CAM_CMD_TIMEOUT},
	{PDMAERR,		CAM_DATA_RUN_ERR},
	{PARITYERR,		CAM_UNCOR_PARITY},
	{UBFERR,		CAM_UNEXP_BUSFREE},
	{ABORTIO,		CAM_REQ_ABORTED},
	{-1,			CAM_UNREC_HBA_ERROR}
};

#define	SIM2SLP(sim)	((struct scsi_low_softc *) cam_sim_softc((sim)))

/* XXX:
 * Please check a polling hz, currently we assume scsi_low_poll() is
 * called each 1 ms.
 */
#define	SCSI_LOW_CAM_POLL_HZ	1000	/* OK ? */

static void
scsi_low_poll_cam(sim)
	struct cam_sim *sim;
{
	struct scsi_low_softc *slp = SIM2SLP(sim);

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

	if (slp->sl_si.si_poll_count ++ >= 
	    SCSI_LOW_CAM_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
	{
		slp->sl_si.si_poll_count = 0;
		scsi_low_timeout_check(slp);
	}
}

void
scsi_low_scsi_action_cam(sim, ccb)
	struct cam_sim *sim;
	union ccb *ccb;
{
	struct scsi_low_softc *slp = SIM2SLP(sim);
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
	u_int lun, flags, msg, target;
	int s, rv;

	target = (u_int) (ccb->ccb_h.target_id);
	lun = (u_int) ccb->ccb_h.target_lun;

#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_ACTION, target) != 0)
	{
		device_printf(slp->sl_dev,
		    "cam_action: func code 0x%x target: %d, lun: %d\n",
		    ccb->ccb_h.func_code, target, lun);
	}
#endif	/* SCSI_LOW_DEBUG */

	switch (ccb->ccb_h.func_code) {
	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
#ifdef	SCSI_LOW_DIAGNOSTIC
		if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
		{
			device_printf(slp->sl_dev, "invalid target/lun\n");
			ccb->ccb_h.status = CAM_REQ_INVALID;
			xpt_done(ccb);
			return;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */

		if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)) {
			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
			xpt_done(ccb);
			return;
		}

		ti = slp->sl_ti[target];
		cb->osdep = ccb;
		cb->bp = NULL;
		if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
			flags = CCB_AUTOSENSE | CCB_SCSIIO;
		else
			flags = CCB_SCSIIO;

		s = splcam();
		li = scsi_low_alloc_li(ti, lun, 1);

		if (ti->ti_setup_msg != 0)
		{
			scsi_low_message_enqueue(slp, ti, li, CCB_AUTOSENSE);
		}

		scsi_low_enqueue(slp, ti, li, cb, flags, 0);

#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ABORT_CHECK, target) != 0)
		{
			scsi_low_test_abort(slp, ti, li);
		}
#endif	/* SCSI_LOW_DEBUG */
		splx(s);
		break;

	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*/
		/* XXX Implement */
		ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;

	case XPT_ABORT:			/* Abort the specified CCB */
#ifdef	SCSI_LOW_DIAGNOSTIC
		if (target == CAM_TARGET_WILDCARD || lun == CAM_LUN_WILDCARD)
		{
			device_printf(slp->sl_dev, "invalid target/lun\n");
			ccb->ccb_h.status = CAM_REQ_INVALID;
			xpt_done(ccb);
			return;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */

		s = splcam();
		cb = scsi_low_find_ccb(slp, target, lun, ccb->cab.abort_ccb);
		rv = scsi_low_abort_ccb(slp, cb);
		splx(s);

		if (rv == 0)
			ccb->ccb_h.status = CAM_REQ_CMP;
		else
			ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;

	case XPT_SET_TRAN_SETTINGS: {
		struct ccb_trans_settings_scsi *scsi;
        	struct ccb_trans_settings_spi *spi;
		struct ccb_trans_settings *cts;
		u_int val;

#ifdef	SCSI_LOW_DIAGNOSTIC
		if (target == CAM_TARGET_WILDCARD)
		{
			device_printf(slp->sl_dev, "invalid target\n");
			ccb->ccb_h.status = CAM_REQ_INVALID;
			xpt_done(ccb);
			return;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */
		cts = &ccb->cts;
		ti = slp->sl_ti[target];
		if (lun == CAM_LUN_WILDCARD)
			lun = 0;

		s = splcam();
		scsi = &cts->proto_specific.scsi;
		spi = &cts->xport_specific.spi;
		if ((spi->valid & (CTS_SPI_VALID_BUS_WIDTH |
				   CTS_SPI_VALID_SYNC_RATE |
				   CTS_SPI_VALID_SYNC_OFFSET)) != 0)
		{
			if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
				val = spi->bus_width;
				if (val < ti->ti_width)
					ti->ti_width = val;
			}
			if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
				val = spi->sync_period;
				if (val == 0 || val > ti->ti_maxsynch.period)
					ti->ti_maxsynch.period = val;
			}
			if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
				val = spi->sync_offset;
				if (val < ti->ti_maxsynch.offset)
					ti->ti_maxsynch.offset = val;
			}
			ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
			scsi_low_calcf_target(ti);
		}

		if ((spi->valid & CTS_SPI_FLAGS_DISC_ENB) != 0 ||
                    (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {

			li = scsi_low_alloc_li(ti, lun, 1);
			if (spi->valid & CTS_SPI_FLAGS_DISC_ENB) {
				li->li_quirks |= SCSI_LOW_DISK_DISC;
			} else {
				li->li_quirks &= ~SCSI_LOW_DISK_DISC;
			}

			if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
				li->li_quirks |= SCSI_LOW_DISK_QTAG;
			} else {
				li->li_quirks &= ~SCSI_LOW_DISK_QTAG;
			}
			li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
			scsi_low_calcf_target(ti);
			scsi_low_calcf_lun(li);
			if ((slp->sl_show_result & SHOW_CALCF_RES) != 0)
				scsi_low_calcf_show(li);
		}
		splx(s);

		ccb->ccb_h.status = CAM_REQ_CMP;
		xpt_done(ccb);
		break;
	}

	case XPT_GET_TRAN_SETTINGS: {
		struct ccb_trans_settings *cts;
		u_int diskflags;

		cts = &ccb->cts;
#ifdef	SCSI_LOW_DIAGNOSTIC
		if (target == CAM_TARGET_WILDCARD)
		{
			device_printf(slp->sl_dev, "invalid target\n");
			ccb->ccb_h.status = CAM_REQ_INVALID;
			xpt_done(ccb);
			return;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */
		ti = slp->sl_ti[target];
		if (lun == CAM_LUN_WILDCARD)
			lun = 0;

		s = splcam();
		li = scsi_low_alloc_li(ti, lun, 1);
		if (li != NULL && cts->type == CTS_TYPE_CURRENT_SETTINGS) {
			struct ccb_trans_settings_scsi *scsi =
				&cts->proto_specific.scsi;
			struct ccb_trans_settings_spi *spi =
				&cts->xport_specific.spi;
#ifdef	SCSI_LOW_DIAGNOSTIC
			if (li->li_flags_valid != SCSI_LOW_LUN_FLAGS_ALL_VALID)
			{
				ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
				device_printf(slp->sl_dev,
				    "invalid GET_TRANS_CURRENT_SETTINGS call\n");
				goto settings_out;
			}
#endif	/* SCSI_LOW_DIAGNOSTIC */
			cts->protocol = PROTO_SCSI;
			cts->protocol_version = SCSI_REV_2;
			cts->transport = XPORT_SPI;
			cts->transport_version = 2;

			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;

			diskflags = li->li_diskflags & li->li_cfgflags;
			if (diskflags & SCSI_LOW_DISK_DISC)
				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
			if (diskflags & SCSI_LOW_DISK_QTAG)
				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;

			spi->sync_period = ti->ti_maxsynch.period;
			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
			spi->sync_offset = ti->ti_maxsynch.offset;
			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;

			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
			spi->bus_width = ti->ti_width;

			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
				scsi->valid = CTS_SCSI_VALID_TQ;
				spi->valid |= CTS_SPI_VALID_DISC;
			} else
				scsi->valid = 0;
		} else
			ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
settings_out:
		splx(s);
		xpt_done(ccb);
		break;
	}

	case XPT_CALC_GEOMETRY: { /* not yet HN2 */
		cam_calc_geometry(&ccb->ccg, /*extended*/1);
		xpt_done(ccb);
		break;
	}

	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
		s = splcam();
		scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL);
		splx(s);
		ccb->ccb_h.status = CAM_REQ_CMP;
		xpt_done(ccb);
		break;

	case XPT_TERM_IO:	/* Terminate the I/O process */
		ccb->ccb_h.status = CAM_REQ_INVALID;
		xpt_done(ccb);
		break;

	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
#ifdef	SCSI_LOW_DIAGNOSTIC
		if (target == CAM_TARGET_WILDCARD)
		{
			device_printf(slp->sl_dev, "invalid target\n");
			ccb->ccb_h.status = CAM_REQ_INVALID;
			xpt_done(ccb);
			return;
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */

		msg = SCSI_LOW_MSG_RESET;
		if (((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL))
		{
			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
			xpt_done(ccb);
			return;
		}

		ti = slp->sl_ti[target];
		if (lun == CAM_LUN_WILDCARD)
			lun = 0;
		cb->osdep = ccb;
		cb->bp = NULL;
		if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
			flags = CCB_AUTOSENSE | CCB_NORETRY | CCB_URGENT;
		else
			flags = CCB_NORETRY | CCB_URGENT;

		s = splcam();
		li = scsi_low_alloc_li(ti, lun, 1);
		scsi_low_enqueue(slp, ti, li, cb, flags, msg);
		splx(s);
		break;

	case XPT_PATH_INQ: {		/* Path routing inquiry */
		struct ccb_pathinq *cpi = &ccb->cpi;
		
		cpi->version_num = scsi_low_version_major;
		cpi->hba_inquiry = PI_TAG_ABLE | PI_LINKED_CDB;
		ti = slp->sl_ti[slp->sl_hostid];	/* host id */
		if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
			cpi->hba_inquiry |= PI_WIDE_16;
		if (ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
			cpi->hba_inquiry |= PI_WIDE_32;
		if (ti->ti_maxsynch.offset > 0)
			cpi->hba_inquiry |= PI_SDTR_ABLE;
		cpi->target_sprt = 0;
		cpi->hba_misc = 0;
		cpi->hba_eng_cnt = 0;
		cpi->max_target = slp->sl_ntargs - 1;
		cpi->max_lun = slp->sl_nluns - 1;
		cpi->initiator_id = slp->sl_hostid;
		cpi->bus_id = cam_sim_bus(sim);
		cpi->base_transfer_speed = 3300;
		cpi->transport = XPORT_SPI;
		cpi->transport_version = 2;
		cpi->protocol = PROTO_SCSI;
		cpi->protocol_version = SCSI_REV_2;
		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;
	}
}

static int
scsi_low_attach_cam(slp)
	struct scsi_low_softc *slp;
{
	struct cam_devq *devq;
	int tagged_openings;

	devq = cam_simq_alloc(SCSI_LOW_NCCB);
	if (devq == NULL)
		return (ENOMEM);

	/*
	 * ask the adapter what subunits are present
	 */
	tagged_openings = min(slp->sl_openings, SCSI_LOW_MAXNEXUS);
	slp->sl_si.sim = cam_sim_alloc(scsi_low_scsi_action_cam,
				scsi_low_poll_cam,
				device_get_name(slp->sl_dev), slp,
				device_get_unit(slp->sl_dev), &Giant,
				slp->sl_openings, tagged_openings, devq);

	if (slp->sl_si.sim == NULL) {
		cam_simq_free(devq);
	 	return ENODEV;
	}

	if (xpt_bus_register(slp->sl_si.sim, NULL, 0) != CAM_SUCCESS) {
		free(slp->sl_si.sim, M_SCSILOW);
	 	return ENODEV;
	}
       
	if (xpt_create_path(&slp->sl_si.path, /*periph*/NULL,
			cam_sim_path(slp->sl_si.sim), CAM_TARGET_WILDCARD,
			CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
		xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
		cam_sim_free(slp->sl_si.sim, /*free_simq*/TRUE);
		return ENODEV;
	}

	slp->sl_show_result = SHOW_CALCF_RES;		/* OK ? */
	return 0;
}

static int
scsi_low_world_start_cam(slp)
	struct scsi_low_softc *slp;
{

	return 0;
}

static int
scsi_low_dettach_cam(slp)
	struct scsi_low_softc *slp;
{

	xpt_async(AC_LOST_DEVICE, slp->sl_si.path, NULL);
	xpt_free_path(slp->sl_si.path);
	xpt_bus_deregister(cam_sim_path(slp->sl_si.sim));
	cam_sim_free(slp->sl_si.sim, /* free_devq */ TRUE);
	return 0;
}

static int
scsi_low_ccb_setup_cam(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
        union ccb *ccb = (union ccb *) cb->osdep;

	if ((cb->ccb_flags & CCB_SCSIIO) != 0)
	{
		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 / 1000;
	}
	else
	{
		scsi_low_unit_ready_cmd(cb);
	}
	return SCSI_LOW_START_QTAG;
}

static int
scsi_low_done_cam(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	union ccb *ccb;

	ccb = (union ccb *) cb->osdep;
	if (cb->ccb_error == 0)
	{
		ccb->ccb_h.status = CAM_REQ_CMP;
		ccb->csio.resid = 0;
	}
	else 	
	{
	        if (cb->ccb_rcnt >= slp->sl_max_retry)
			cb->ccb_error |= ABORTIO;

		if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
		    (cb->ccb_error & ABORTIO) == 0)
			return EJUSTRETURN;

		if ((cb->ccb_error & SENSEIO) != 0)
		{
			memcpy(&ccb->csio.sense_data,
			       &cb->ccb_sense,
			       sizeof(ccb->csio.sense_data));
		}

		ccb->ccb_h.status = scsi_low_translate_error_code(cb,
					&scsi_low_error_code_cam[0]);
	
#ifdef	SCSI_LOW_DIAGNOSTIC
		if ((cb->ccb_flags & CCB_SILENT) == 0 &&
		    cb->ccb_scp.scp_cmdlen > 0 &&
		    (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
		     SCSI_LOW_CMD_ABORT_WARNING) != 0)
		{
			device_printf(slp->sl_dev,
			    "WARNING: scsi_low IO abort\n");
			scsi_low_print(slp, NULL);
		}
#endif	/* SCSI_LOW_DIAGNOSTIC */
	}

	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 0)
		ccb->ccb_h.status |= CAM_REQ_CMP_ERR;

	if (cb->ccb_scp.scp_status == ST_UNKNOWN)
		ccb->csio.scsi_status = 0;	/* XXX */
	else
		ccb->csio.scsi_status = cb->ccb_scp.scp_status;

	if ((cb->ccb_flags & CCB_NOSDONE) == 0)
		xpt_done(ccb);
	return 0;
}

static void
scsi_low_timeout_cam(slp, ch, action)
	struct scsi_low_softc *slp;
	int ch;
	int action;
{

	switch (ch)
	{
	case SCSI_LOW_TIMEOUT_CH_IO:
		switch (action)
		{
		case SCSI_LOW_TIMEOUT_START:
			slp->sl_si.timeout_ch = timeout(scsi_low_timeout, slp,
				hz / SCSI_LOW_TIMEOUT_HZ);
			break;
		case SCSI_LOW_TIMEOUT_STOP:
			untimeout(scsi_low_timeout, slp, slp->sl_si.timeout_ch);
			break;
		}
		break;

	case SCSI_LOW_TIMEOUT_CH_ENGAGE:
		switch (action)
		{
		case SCSI_LOW_TIMEOUT_START:
			slp->sl_si.engage_ch = timeout(scsi_low_engage, slp, 1);
			break;
		case SCSI_LOW_TIMEOUT_STOP:
			untimeout(scsi_low_engage, slp, slp->sl_si.engage_ch);
			break;
		}
		break;
	case SCSI_LOW_TIMEOUT_CH_RECOVER:
		break;
	}
}

/*=============================================================
 * END OF OS switch  (All OS depend fucntions should be above)
 =============================================================*/

/**************************************************************
 * scsi low deactivate and activate
 **************************************************************/
int
scsi_low_is_busy(slp)
	struct scsi_low_softc *slp;
{

	if (slp->sl_nio > 0)
		return EBUSY;
	return 0;
}

int
scsi_low_deactivate(slp)
	struct scsi_low_softc *slp;
{
	int s;

	s = splcam();
	slp->sl_flags |= HW_INACTIVE;
	(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
		(slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_STOP);
	(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
		(slp, SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
	splx(s);
	return 0;
}

int
scsi_low_activate(slp)
	struct scsi_low_softc *slp;
{
	int error, s;

	s = splcam();
	slp->sl_flags &= ~HW_INACTIVE;
	if ((error = scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, NULL)) != 0)
	{
		slp->sl_flags |= HW_INACTIVE;
		splx(s);
		return error;
	}

	slp->sl_timeout_count = 0;
	(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
		(slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
	splx(s);
	return 0;
}

/**************************************************************
 * scsi low log
 **************************************************************/
#ifdef	SCSI_LOW_DIAGNOSTIC
static void scsi_low_msg_log_init(struct scsi_low_msg_log *);
static void scsi_low_msg_log_write(struct scsi_low_msg_log *, u_int8_t *, int);
static void scsi_low_msg_log_show(struct scsi_low_msg_log *, char *, int);

static void
scsi_low_msg_log_init(slmlp)
	struct scsi_low_msg_log *slmlp;
{

	slmlp->slml_ptr = 0;
}

static void
scsi_low_msg_log_write(slmlp, datap, len)
	struct scsi_low_msg_log *slmlp;
	u_int8_t *datap;
	int len;
{
	int ptr, ind;

	if (slmlp->slml_ptr >= SCSI_LOW_MSG_LOG_DATALEN)
		return;

	ptr = slmlp->slml_ptr ++;
	for (ind = 0; ind < sizeof(slmlp->slml_msg[0]) && ind < len; ind ++)
		slmlp->slml_msg[ptr].msg[ind] = datap[ind];
	for ( ; ind < sizeof(slmlp->slml_msg[0]); ind ++)
		slmlp->slml_msg[ptr].msg[ind] = 0;
}
	
static void
scsi_low_msg_log_show(slmlp, s, len)
	struct scsi_low_msg_log *slmlp;
	char *s;
	int len;
{
	int ptr, ind;

	printf("%s: (%d) ", s, slmlp->slml_ptr);
	for (ptr = 0; ptr < slmlp->slml_ptr; ptr ++)
	{
		for (ind = 0; ind < len && ind < sizeof(slmlp->slml_msg[0]);
		     ind ++)
		{
			printf("[%x]", (u_int) slmlp->slml_msg[ptr].msg[ind]);
		}
		printf(">");
	}
	printf("\n");
}
#endif	/* SCSI_LOW_DIAGNOSTIC */

/**************************************************************
 * power control
 **************************************************************/
static void
scsi_low_engage(arg)
	void *arg;
{
	struct scsi_low_softc *slp = arg;
	int s = splcam();

	switch (slp->sl_rstep)
	{
	case 0:
		slp->sl_rstep ++;
		(*slp->sl_funcs->scsi_low_power) (slp, SCSI_LOW_ENGAGE);
		(*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp, 
			SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_START);
		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;
{
	int rv = 0;

	slp->sl_flags |= HW_INITIALIZING;

	/* clear power control timeout */
	if ((slp->sl_flags & HW_POWERCTRL) != 0)
	{
		(*slp->sl_osdep_fp->scsi_low_osdep_timeout) (slp, 
			SCSI_LOW_TIMEOUT_CH_ENGAGE, SCSI_LOW_TIMEOUT_STOP);
		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)
	{
		rv = EBUSY;
		goto out;
	}

	if (flags != SCSI_LOW_RESTART_SOFT)
	{
		rv = ((*slp->sl_funcs->scsi_low_init) (slp, flags));
	}

out:
	slp->sl_flags &= ~HW_INITIALIZING;
	return rv;
}

/**************************************************************
 * 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 = SCSI_LOW_MALLOC(ti->ti_lunsize);
	if (li == NULL)
		panic("no lun info mem");

	bzero(li, ti->ti_lunsize);
	li->li_lun = lun;
	li->li_ti = ti;

	li->li_cfgflags = SCSI_LOW_SYNC | SCSI_LOW_LINK | SCSI_LOW_DISC |
			  SCSI_LOW_QTAG;
	li->li_quirks = li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
	li->li_flags_valid = SCSI_LOW_LUN_FLAGS_USER_VALID;
#ifdef	SCSI_LOW_FLAGS_QUIRKS_OK
	li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_QUIRKS_VALID;
#endif	/* SCSI_LOW_FLAGS_QUIRKS_OK */

	li->li_qtagbits = (u_int) -1;

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

	/* host specific structure initialization per lun */
	if (slp->sl_funcs->scsi_low_lun_init != NULL)
		(*slp->sl_funcs->scsi_low_lun_init)
			(slp, ti, li, SCSI_LOW_INFO_ALLOC);
	scsi_low_calcf_lun(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 (TAILQ_FIRST(&slp->sl_titab) == NULL)
		TAILQ_INIT(&slp->sl_titab);

	ti = SCSI_LOW_MALLOC(slp->sl_targsize);
	if (ti == NULL)
		panic("%s short of memory", device_get_nameunit(slp->sl_dev));

	bzero(ti, slp->sl_targsize);
	ti->ti_id = targ;
	ti->ti_sc = slp;

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

	ti->ti_quirks = ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
	ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;
	ti->ti_flags_valid = SCSI_LOW_TARG_FLAGS_USER_VALID;
#ifdef	SCSI_LOW_FLAGS_QUIRKS_OK
	ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_QUIRKS_VALID;
#endif	/* SCSI_LOW_FLAGS_QUIRKS_OK */

	if (slp->sl_funcs->scsi_low_targ_init != NULL)
	{
		(*slp->sl_funcs->scsi_low_targ_init)
			(slp, ti, SCSI_LOW_INFO_ALLOC);
	}
	scsi_low_calcf_target(ti);
	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 = TAILQ_FIRST(&slp->sl_titab); ti; ti = tib)
	{
		for (li = LIST_FIRST(&ti->ti_litab); li != NULL; li = nli)
		{
			if (slp->sl_funcs->scsi_low_lun_init != NULL)
			{
				(*slp->sl_funcs->scsi_low_lun_init)
					(slp, ti, li, SCSI_LOW_INFO_DEALLOC);
			}
			nli = LIST_NEXT(li, lun_chain);
			SCSI_LOW_FREE(li);
		}

		if (slp->sl_funcs->scsi_low_targ_init != NULL)
		{
			(*slp->sl_funcs->scsi_low_targ_init)
				(slp, ti, SCSI_LOW_INFO_DEALLOC);
		}
		tib = TAILQ_NEXT(ti, ti_chain);
		SCSI_LOW_FREE(ti);
	}
}

/**************************************************************
 * timeout
 **************************************************************/
void
scsi_low_bus_idle(slp)
	struct scsi_low_softc *slp;
{

	slp->sl_retry_sel = 0;
	if (slp->sl_Tnexus == NULL)
		scsi_low_start(slp);
}

static void
scsi_low_timeout(arg)
	void *arg;
{
	struct scsi_low_softc *slp = arg;
	int s;

	s = splcam();
	(void) scsi_low_timeout_check(slp);
	(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
		(slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
	splx(s);
}

static int
scsi_low_timeout_check(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb = NULL;		/* XXX */

	/* selection restart */
	if (slp->sl_retry_sel != 0)
	{
		slp->sl_retry_sel = 0;
		if (slp->sl_Tnexus != NULL)
			goto step1;

		cb = TAILQ_FIRST(&slp->sl_start);
		if (cb == NULL)
			goto step1;

		if (cb->ccb_selrcnt >= SCSI_LOW_MAX_SELECTION_RETRY)
		{
			cb->ccb_flags |= CCB_NORETRY;
			cb->ccb_error |= SELTIMEOUTIO;
			if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
				panic("%s: ccb not finished",
				    device_get_nameunit(slp->sl_dev));
		}

		if (slp->sl_Tnexus == NULL)
			scsi_low_start(slp);
	}

	/* call hardware timeout */
step1:
	if (slp->sl_funcs->scsi_low_timeout != NULL)
	{
		(*slp->sl_funcs->scsi_low_timeout) (slp);
	}
	
	if (slp->sl_timeout_count ++ < 
	    SCSI_LOW_TIMEOUT_CHECK_INTERVAL * SCSI_LOW_TIMEOUT_HZ)
		return 0;

	slp->sl_timeout_count = 0;
	if (slp->sl_nio > 0)
	{
		if ((cb = slp->sl_Qnexus) != NULL)
		{
			cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
			if (cb->ccb_tc < 0)
				goto bus_reset;
		}
		else if (slp->sl_disc == 0)
		{
		        if ((cb = TAILQ_FIRST(&slp->sl_start)) == NULL)
				return 0;

			cb->ccb_tc -= SCSI_LOW_TIMEOUT_CHECK_INTERVAL;
			if (cb->ccb_tc < 0)
				goto bus_reset;
		}
		else for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
		          ti = TAILQ_NEXT(ti, ti_chain))
		{
			if (ti->ti_disc == 0)
				continue;

			for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
			     li = LIST_NEXT(li, lun_chain))
			{
				for (cb = TAILQ_FIRST(&li->li_discq); 
				     cb != NULL;
				     cb = TAILQ_NEXT(cb, ccb_chain))
				{
					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)
			return 0;

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

		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);
		}
	}
	return 0;

bus_reset:
	cb->ccb_error |= TIMEOUTIO;
	device_printf(slp->sl_dev, "slccb (0x%lx) timeout!\n", (u_long) cb);
	scsi_low_info(slp, NULL, "scsi bus hangup. try to recover.");
	scsi_low_init(slp, SCSI_LOW_RESTART_HARD);
	scsi_low_start(slp);
	return ERESTART;
}


static int
scsi_low_abort_ccb(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	struct targ_info *ti;
	struct lun_info *li;
	u_int msg;

	if (cb == NULL)
		return EINVAL;
	if ((cb->ccb_omsgoutflag & 
	     (SCSI_LOW_MSG_ABORT | SCSI_LOW_MSG_ABORT_QTAG)) != 0)
		return EBUSY;

	ti = cb->ti;
	li = cb->li;
	if (cb->ccb_tag == SCSI_LOW_UNKTAG)
		msg = SCSI_LOW_MSG_ABORT;
	else
		msg = SCSI_LOW_MSG_ABORT_QTAG;

	cb->ccb_error |= ABORTIO;
	cb->ccb_flags |= CCB_NORETRY;
	scsi_low_ccb_message_assert(cb, msg);

	if (cb == slp->sl_Qnexus)
	{
		scsi_low_assert_msg(slp, ti, msg, 1);
	}
	else if ((cb->ccb_flags & CCB_DISCQ) != 0)
	{
		if (scsi_low_revoke_ccb(slp, cb, 0) == NULL)
			panic("%s: revoked ccb done",
			    device_get_nameunit(slp->sl_dev));

		cb->ccb_flags |= CCB_STARTQ;
		TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);

		if (slp->sl_Tnexus == NULL)
			scsi_low_start(slp);
	}
	else
	{
		if (scsi_low_revoke_ccb(slp, cb, 1) != NULL)
			panic("%s: revoked ccb retried",
			    device_get_nameunit(slp->sl_dev));
	}
	return 0;
}

/**************************************************************
 * Generic SCSI INTERFACE
 **************************************************************/
int
scsi_low_attach(slp, openings, ntargs, nluns, targsize, lunsize)
	struct scsi_low_softc *slp;
	int openings, ntargs, nluns, targsize, lunsize;
{
	struct targ_info *ti;
	struct lun_info *li;
	int s, i, nccb, rv;

	slp->sl_osdep_fp = &scsi_low_osdep_funcs_cam;

	if (slp->sl_osdep_fp == NULL)
		panic("scsi_low: interface not spcified");

	if (ntargs > SCSI_LOW_NTARGETS)
	{
		printf("scsi_low: %d targets are too large\n", ntargs);
		printf("change kernel options SCSI_LOW_NTARGETS");
		return EINVAL;
	}

	if (openings <= 0)
		slp->sl_openings = (SCSI_LOW_NCCB / ntargs);
	else
		slp->sl_openings = openings;
	slp->sl_ntargs = ntargs;
	slp->sl_nluns = nluns;
	slp->sl_max_retry = SCSI_LOW_MAX_RETRY;

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

	if (targsize < sizeof(struct targ_info))
		targsize = sizeof(struct targ_info);

	slp->sl_targsize = targsize;
	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);
	}

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

	/* call os depend attach */
	s = splcam();
	rv = (*slp->sl_osdep_fp->scsi_low_osdep_attach) (slp);
	if (rv != 0)
	{
		splx(s);
		device_printf(slp->sl_dev,
		    "scsi_low_attach: osdep attach failed\n");
		return EINVAL;
	}

	/* check hardware */
	DELAY(1000);	/* wait for 1ms */
	if (scsi_low_init(slp, SCSI_LOW_RESTART_HARD) != 0)
	{
		splx(s);
		device_printf(slp->sl_dev,
		    "scsi_low_attach: initialization failed\n");
		return EINVAL;
	}

	/* start watch dog */
	slp->sl_timeout_count = 0;
	(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
		 (slp, SCSI_LOW_TIMEOUT_CH_IO, SCSI_LOW_TIMEOUT_START);
	LIST_INSERT_HEAD(&sl_tab, slp, sl_chain);

	/* fake call */
	scsi_low_abort_ccb(slp, scsi_low_find_ccb(slp, 0, 0, NULL));

#ifdef	SCSI_LOW_START_UP_CHECK
	/* probing devices */
	scsi_low_start_up(slp);
#endif	/* SCSI_LOW_START_UP_CHECK */

	/* call os depend attach done*/
	(*slp->sl_osdep_fp->scsi_low_osdep_world_start) (slp);
	splx(s);
	return 0;
}

int
scsi_low_dettach(slp)
	struct scsi_low_softc *slp;
{
	int s, rv;

	s = splcam();
	if (scsi_low_is_busy(slp) != 0)
	{
		splx(s);
		return EBUSY;
	}

	scsi_low_deactivate(slp);

	rv = (*slp->sl_osdep_fp->scsi_low_osdep_dettach) (slp);
	if (rv != 0)
	{
		splx(s);
		return EBUSY;
	}

	scsi_low_free_ti(slp);
	LIST_REMOVE(slp, sl_chain);
	splx(s);
	return 0;
}

/**************************************************************
 * Generic enqueue
 **************************************************************/
static int
scsi_low_enqueue(slp, ti, li, cb, flags, msg)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
	u_int flags, msg;
{	

	cb->ti = ti;
	cb->li = li;

	scsi_low_ccb_message_assert(cb, msg);

	cb->ccb_otag = cb->ccb_tag = SCSI_LOW_UNKTAG;
	scsi_low_alloc_qtag(cb);

	cb->ccb_flags = flags | CCB_STARTQ;
	cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
	cb->ccb_error |= PENDINGIO;

	if ((flags & CCB_URGENT) != 0)
	{
		TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
	}
	else
	{
		TAILQ_INSERT_TAIL(&slp->sl_start, cb, ccb_chain);
	}

	slp->sl_nio ++;

	if (slp->sl_Tnexus == NULL)
		scsi_low_start(slp);
	return 0;
}

static int
scsi_low_message_enqueue(slp, ti, li, flags)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;
	u_int flags;
{
	struct slccb *cb;
	u_int tmsgflags;

	tmsgflags = ti->ti_setup_msg;
	ti->ti_setup_msg = 0;

	flags |= CCB_NORETRY;
	if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
		return ENOMEM;

	cb->osdep = NULL;
	cb->bp = NULL;
	scsi_low_enqueue(slp, ti, li, cb, flags, tmsgflags);
	return 0;
}

/**************************************************************
 * Generic Start & Done
 **************************************************************/
#define	SLSC_MODE_SENSE_SHORT   0x1a
static u_int8_t ss_cmd[6] = {START_STOP, 0, 0, 0, SSS_START, 0}; 
static u_int8_t sms_cmd[6] = {SLSC_MODE_SENSE_SHORT, 0x08, 0x0a, 0, 
			      sizeof(struct scsi_low_mode_sense_data), 0}; 
static u_int8_t inq_cmd[6] = {INQUIRY, 0, 0, 0, 
			      sizeof(struct scsi_low_inq_data), 0}; 
static u_int8_t unit_ready_cmd[6];
static int scsi_low_setup_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
static int scsi_low_sense_abort_start(struct scsi_low_softc *, struct targ_info *, struct lun_info *, struct slccb *);
static int scsi_low_resume(struct scsi_low_softc *);

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

	cb->ccb_scp.scp_cmd = 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 int
scsi_low_sense_abort_start(slp, ti, li, cb)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
{

	cb->ccb_scp.scp_cmdlen = 6;
	bzero(cb->ccb_scsi_cmd, cb->ccb_scp.scp_cmdlen);
	cb->ccb_scsi_cmd[0] = REQUEST_SENSE;
	cb->ccb_scsi_cmd[4] = sizeof(cb->ccb_sense);
	cb->ccb_scp.scp_cmd = cb->ccb_scsi_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;
	scsi_low_ccb_message_clear(cb);
	if ((cb->ccb_flags & CCB_CLEARQ) != 0)
	{
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
	}
	else
	{
		bzero(&cb->ccb_sense, sizeof(cb->ccb_sense));
#ifdef	SCSI_LOW_NEGOTIATE_BEFORE_SENSE
		scsi_low_assert_msg(slp, ti, ti->ti_setup_msg_done, 0);
#endif	/* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */
	}

	return SCSI_LOW_START_NO_QTAG;
}

static int
scsi_low_setup_start(slp, ti, li, cb)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
{

	switch(li->li_state)
	{
	case SCSI_LOW_LUN_SLEEP:
		scsi_low_unit_ready_cmd(cb);
		break;

	case SCSI_LOW_LUN_START:
		cb->ccb_scp.scp_cmd = 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 SCSI_LOW_LUN_INQ:
		cb->ccb_scp.scp_cmd = inq_cmd;
		cb->ccb_scp.scp_cmdlen = sizeof(inq_cmd);
		cb->ccb_scp.scp_data = (u_int8_t *)&li->li_inq;
		cb->ccb_scp.scp_datalen = sizeof(li->li_inq);
		cb->ccb_scp.scp_direction = SCSI_LOW_READ;
		cb->ccb_tcmax = 15;
		break;

	case SCSI_LOW_LUN_MODEQ:
		cb->ccb_scp.scp_cmd = sms_cmd;
		cb->ccb_scp.scp_cmdlen = sizeof(sms_cmd);
		cb->ccb_scp.scp_data = (u_int8_t *)&li->li_sms;
		cb->ccb_scp.scp_datalen = sizeof(li->li_sms);
		cb->ccb_scp.scp_direction = SCSI_LOW_READ;
		cb->ccb_tcmax = 15;
		return SCSI_LOW_START_QTAG;

	default:
		panic("%s: no setup phase", device_get_nameunit(slp->sl_dev));
	}

	return SCSI_LOW_START_NO_QTAG;
}

static int
scsi_low_resume(slp)
	struct scsi_low_softc *slp;
{

	if (slp->sl_flags & HW_RESUME)
		return EJUSTRETURN;
	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);
		(*slp->sl_osdep_fp->scsi_low_osdep_timeout)
					(slp, SCSI_LOW_TIMEOUT_CH_ENGAGE,
				         SCSI_LOW_TIMEOUT_START);
		return EJUSTRETURN;
	}
	return 0;
}

static void
scsi_low_start(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
	int rv;

	/* check hardware exists or under initializations ? */
	if ((slp->sl_flags & (HW_INACTIVE | HW_INITIALIZING)) != 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 (scsi_low_resume(slp) == EJUSTRETURN)
				return;
		}
	}

	/* setup nexus */
#ifdef	SCSI_LOW_DIAGNOSTIC
	if (slp->sl_Tnexus || slp->sl_Lnexus || slp->sl_Qnexus)
	{
		scsi_low_info(slp, NULL, "NEXUS INCOSISTENT");
		panic("%s: inconsistent", device_get_nameunit(slp->sl_dev));
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	for (cb = TAILQ_FIRST(&slp->sl_start); cb != NULL;
	     cb = TAILQ_NEXT(cb, ccb_chain))
	{
		li = cb->li;

		if (li->li_disc == 0)
		{
			goto scsi_low_cmd_start;
		}
		else if (li->li_nqio > 0)
		{
			if (li->li_nqio < li->li_maxnqio ||
		            (cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
				goto scsi_low_cmd_start;
		}
	}
	return;

scsi_low_cmd_start:
	cb->ccb_flags &= ~CCB_STARTQ;
	TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
	ti = cb->ti;

	/* clear all error flag bits (for restart) */
	cb->ccb_error = 0;
	cb->ccb_datalen = -1;
	cb->ccb_scp.scp_status = ST_UNKNOWN;

	/* setup nexus pointer */
	slp->sl_Qnexus = cb;
	slp->sl_Lnexus = li;
	slp->sl_Tnexus = ti;

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

	/* exec cmd */
	if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
	{
		/* CA state or forced abort */
		rv = scsi_low_sense_abort_start(slp, ti, li, cb);
	}
	else if (li->li_state >= SCSI_LOW_LUN_OK)
	{
		cb->ccb_flags &= ~CCB_INTERNAL;
		rv = (*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, cb);
		if (cb->ccb_msgoutflag != 0)
		{
			scsi_low_ccb_message_exec(slp, cb);
		}
	}
	else
	{
		cb->ccb_flags |= CCB_INTERNAL;
		rv = scsi_low_setup_start(slp, ti, li, cb);
	}

	/* allocate qtag */
#define	SCSI_LOW_QTAG_OK (SCSI_LOW_QTAG | SCSI_LOW_DISC)

	if (rv == SCSI_LOW_START_QTAG &&
	    (li->li_flags & SCSI_LOW_QTAG_OK) == SCSI_LOW_QTAG_OK &&
	    li->li_maxnqio > 0)
	{
		u_int qmsg;

		scsi_low_activate_qtag(cb);
		if ((scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
		     SCSI_LOW_CMD_ORDERED_QTAG) != 0)
			qmsg = SCSI_LOW_MSG_ORDERED_QTAG;
		else if ((cb->ccb_flags & CCB_URGENT) != 0)
			qmsg = SCSI_LOW_MSG_HEAD_QTAG;
		else
			qmsg = SCSI_LOW_MSG_SIMPLE_QTAG;
		scsi_low_assert_msg(slp, ti, qmsg, 0);
	}

	/* 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;

	/* assert always an identify msg */
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_IDENTIFY, 0);

	/* debug section */
#ifdef	SCSI_LOW_DIAGNOSTIC
	scsi_low_msg_log_init(&ti->ti_log_msgin);
	scsi_low_msg_log_init(&ti->ti_log_msgout);
#endif	/* SCSI_LOW_DIAGNOSTIC */

	/* selection start */
	slp->sl_selid = cb;
	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;
	}

	scsi_low_arbit_fail(slp, cb);
#ifdef	SCSI_LOW_STATICS
	scsi_low_statics.nexus_fail ++;
#endif	/* SCSI_LOW_STATICS */
}

void
scsi_low_arbit_fail(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	struct targ_info *ti = cb->ti;

	scsi_low_deactivate_qtag(cb);
	scsi_low_ccb_message_retry(cb);
	cb->ccb_flags |= CCB_STARTQ;
	TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);

	scsi_low_bus_release(slp, ti);

	cb->ccb_selrcnt ++;
	if (slp->sl_disc == 0)
	{
#ifdef	SCSI_LOW_DIAGNOSTIC
		device_printf(slp->sl_dev, "try selection again\n");
#endif	/* SCSI_LOW_DIAGNOSTIC */
		slp->sl_retry_sel = 1;
	}
}

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

	if (ti->ti_disc > 0)
	{
		SCSI_LOW_SETUP_PHASE(ti, PH_DISC);
	}
	else
	{
		SCSI_LOW_SETUP_PHASE(ti, PH_NULL);
	}

	/* clear all nexus pointer */
	slp->sl_Qnexus = NULL;
	slp->sl_Lnexus = NULL;
	slp->sl_Tnexus = NULL;

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

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

	/* clear phase change counter */
	slp->sl_ph_count = 0;
}

static int
scsi_low_setup_done(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	struct targ_info *ti;
	struct lun_info *li;

	ti = cb->ti;
	li = cb->li;

	if (cb->ccb_rcnt >= slp->sl_max_retry)
	{
		cb->ccb_error |= ABORTIO;
		return SCSI_LOW_DONE_COMPLETE;
	}

	/* XXX: special huck for selection timeout */
	if (li->li_state == SCSI_LOW_LUN_SLEEP &&
	    (cb->ccb_error & SELTIMEOUTIO) != 0)
	{
		cb->ccb_error |= ABORTIO;
		return SCSI_LOW_DONE_COMPLETE;
	}

	switch(li->li_state)
	{
	case SCSI_LOW_LUN_INQ:
		if (cb->ccb_error != 0)
		{
			li->li_diskflags &= 
				~(SCSI_LOW_DISK_LINK | SCSI_LOW_DISK_QTAG);
			if (li->li_lun > 0)
				goto resume;
			ti->ti_diskflags &=
				~(SCSI_LOW_DISK_SYNC | SCSI_LOW_DISK_WIDE);
		}
		else if ((li->li_inq.sd_version & 7) >= 2 ||
		         (li->li_inq.sd_len >= 4))
		{
			if ((li->li_inq.sd_support & 0x2) == 0)
				li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
			if ((li->li_inq.sd_support & 0x8) == 0)
				li->li_diskflags &= ~SCSI_LOW_DISK_LINK;
			if (li->li_lun > 0)
				goto resume;
			if ((li->li_inq.sd_support & 0x10) == 0)
				ti->ti_diskflags &= ~SCSI_LOW_DISK_SYNC;
			if ((li->li_inq.sd_support & 0x20) == 0)
				ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_16;
			if ((li->li_inq.sd_support & 0x40) == 0)
				ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE_32;
		}
		else
		{
			li->li_diskflags &= 
				~(SCSI_LOW_DISK_QTAG | SCSI_LOW_DISK_LINK);
			if (li->li_lun > 0)
				goto resume;
			ti->ti_diskflags &= ~SCSI_LOW_DISK_WIDE;
		}
		ti->ti_flags_valid |= SCSI_LOW_TARG_FLAGS_DISK_VALID;
resume:
		scsi_low_calcf_target(ti);
		scsi_low_calcf_lun(li);
		break;

	case SCSI_LOW_LUN_MODEQ:
		if (cb->ccb_error != 0)
		{
			if (cb->ccb_error & SENSEIO)
			{
#ifdef	SCSI_LOW_DEBUG
				if (scsi_low_debug & SCSI_LOW_DEBUG_SENSE)
				{
					int error_code, sense_key, asc, ascq;

					scsi_extract_sense(&cb->ccb_sense,
							   &error_code,
							   &sense_key,
							   &asc,
							   &ascq);
					printf("SENSE: [%x][%x][%x][%x]\n",
					       error_code, sense_key, asc,
					       ascq);
				}
#endif	/* SCSI_LOW_DEBUG */
			}
			else
			{
				li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
			}
		}
		else if ((li->li_sms.sms_cmp.cmp_page & 0x3f) == 0x0a)
		{	
			if (li->li_sms.sms_cmp.cmp_qc & 0x02)
				li->li_qflags |= SCSI_LOW_QFLAG_CA_QCLEAR;
			else
				li->li_qflags &= ~SCSI_LOW_QFLAG_CA_QCLEAR;
			if ((li->li_sms.sms_cmp.cmp_qc & 0x01) != 0)
				li->li_diskflags &= ~SCSI_LOW_DISK_QTAG;
		}
		li->li_flags_valid |= SCSI_LOW_LUN_FLAGS_DISK_VALID;
		scsi_low_calcf_lun(li);
		break;

	default:
		break;
	}

	li->li_state ++;
	if (li->li_state == SCSI_LOW_LUN_OK)
	{
		scsi_low_calcf_target(ti);
		scsi_low_calcf_lun(li);
		if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID &&
	            (slp->sl_show_result & SHOW_CALCF_RES) != 0)
		{
			scsi_low_calcf_show(li);
		}	
	}

	cb->ccb_rcnt --;
	return SCSI_LOW_DONE_RETRY;
}

static int
scsi_low_done(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	int rv;

	if (cb->ccb_error == 0)
	{
		if ((cb->ccb_flags & (CCB_SENSE | CCB_CLEARQ)) != 0)
		{
#ifdef	SCSI_LOW_QCLEAR_AFTER_CA
			/* XXX:
			 * SCSI-2 draft suggests 
			 * page 0x0a QErr bit determins if
			 * the target aborts or continues
			 * the queueing io's after CA state resolved.
			 * However many targets seem not to support
			 * the page 0x0a. Thus we should manually clear the
			 * queuing io's after CA state.
			 */
			if ((cb->ccb_flags & CCB_CLEARQ) == 0)
			{
				cb->ccb_rcnt --;
				cb->ccb_flags |= CCB_CLEARQ;
				goto retry;
			}
#endif	/* SCSI_LOW_QCLEAR_AFTER_CA */

			if ((cb->ccb_flags & CCB_SENSE) != 0)
				cb->ccb_error |= (SENSEIO | ABORTIO);
			cb->ccb_flags &= ~(CCB_SENSE | CCB_CLEARQ);
		}
		else switch (cb->ccb_sscp.scp_status)
		{
		case ST_GOOD:
		case ST_MET:
		case ST_INTERGOOD:
		case ST_INTERMET:
			if (cb->ccb_datalen == 0 ||
			    cb->ccb_scp.scp_datalen == 0)
				break;

			if (cb->ccb_scp.scp_cmdlen > 0 &&
			    (scsi_low_cmd_flags[cb->ccb_scp.scp_cmd[0]] &
			     SCSI_LOW_CMD_RESIDUAL_CHK) == 0)
				break;

			cb->ccb_error |= PDMAERR;
			break;

		case ST_BUSY:
		case ST_QUEFULL:
			cb->ccb_error |= (BUSYERR | STATERR);
			break;

		case ST_CONFLICT:
			cb->ccb_error |= (STATERR | ABORTIO);
			break;

		case ST_CHKCOND:
		case ST_CMDTERM:
			if (cb->ccb_flags & (CCB_AUTOSENSE | CCB_INTERNAL))
			{
				cb->ccb_rcnt --;
				cb->ccb_flags |= CCB_SENSE;
				goto retry;
			}
			cb->ccb_error |= (UACAERR | STATERR | ABORTIO);
			break;

		case ST_UNKNOWN:
		default:
			cb->ccb_error |= FATALIO;
			break;
		}
	}
	else
	{
		if (cb->ccb_flags & CCB_SENSE)
		{
			cb->ccb_error |= (SENSEERR | ABORTIO);
		}
		cb->ccb_flags &= ~(CCB_CLEARQ | CCB_SENSE);
	}

	/* internal ccb */
	if ((cb->ccb_flags & CCB_INTERNAL) != 0)
	{
		if (scsi_low_setup_done(slp, cb) == SCSI_LOW_DONE_RETRY)
			goto retry;
	}

	/* check a ccb msgout flag */
	if (cb->ccb_omsgoutflag != 0)
	{
#define	SCSI_LOW_MSG_ABORT_OK	(SCSI_LOW_MSG_ABORT | \
				 SCSI_LOW_MSG_ABORT_QTAG | \
				 SCSI_LOW_MSG_CLEAR_QTAG | \
				 SCSI_LOW_MSG_TERMIO)

		if ((cb->ccb_omsgoutflag & SCSI_LOW_MSG_ABORT_OK) != 0)
		{
			cb->ccb_error |= ABORTIO;
		}
	}

	/* call OS depend done */
	if (cb->osdep != NULL)
	{
		rv = (*slp->sl_osdep_fp->scsi_low_osdep_done) (slp, cb);
		if (rv == EJUSTRETURN)
			goto retry;
	}
	else if (cb->ccb_error != 0)
	{
	        if (cb->ccb_rcnt >= slp->sl_max_retry)
			cb->ccb_error |= ABORTIO;

		if ((cb->ccb_flags & CCB_NORETRY) == 0 &&
		    (cb->ccb_error & ABORTIO) == 0)
			goto retry;
	}

	/* free our target */
#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
	{
		printf(">> SCSI_LOW_DONE_COMPLETE ===============\n");
		scsi_low_print(slp, NULL);
	}
#endif	/* SCSI_LOW_DEBUG */

	scsi_low_deactivate_qtag(cb);
	scsi_low_dealloc_qtag(cb);
	scsi_low_free_ccb(cb);
	slp->sl_nio --;
	return SCSI_LOW_DONE_COMPLETE;

retry:
#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DONE, cb->ti->ti_id) != 0)
	{
		printf("** SCSI_LOW_DONE_RETRY ===============\n");
		scsi_low_print(slp, NULL);
	}
#endif	/* SCSI_LOW_DEBUG */
		
	cb->ccb_rcnt ++;
	scsi_low_deactivate_qtag(cb);
	scsi_low_ccb_message_retry(cb);
	return SCSI_LOW_DONE_RETRY;
}

/**************************************************************
 * Reset
 **************************************************************/
static void
scsi_low_reset_nexus_target(slp, ti, fdone)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	int fdone;
{
	struct lun_info *li;

	for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
	     li = LIST_NEXT(li, lun_chain))
	{
		scsi_low_reset_nexus_lun(slp, li, fdone);
		li->li_state = SCSI_LOW_LUN_SLEEP;
		li->li_maxnqio = 0;
	}

	ti->ti_disc = 0;
	ti->ti_setup_msg = 0;
	ti->ti_setup_msg_done = 0;

	ti->ti_osynch.offset = ti->ti_osynch.period = 0;
	ti->ti_owidth = SCSI_LOW_BUS_WIDTH_8;

	ti->ti_diskflags = SCSI_LOW_DISK_TFLAGS;
	ti->ti_flags_valid &= ~SCSI_LOW_TARG_FLAGS_DISK_VALID;

	if (slp->sl_funcs->scsi_low_targ_init != NULL)
	{
		((*slp->sl_funcs->scsi_low_targ_init)
			(slp, ti, SCSI_LOW_INFO_REVOKE));
	}
	scsi_low_calcf_target(ti);

	for (li = LIST_FIRST(&ti->ti_litab); li != NULL;
	     li = LIST_NEXT(li, lun_chain))
	{
		li->li_flags = 0;

		li->li_diskflags = SCSI_LOW_DISK_LFLAGS;
		li->li_flags_valid &= ~SCSI_LOW_LUN_FLAGS_DISK_VALID;

		if (slp->sl_funcs->scsi_low_lun_init != NULL)
		{
			((*slp->sl_funcs->scsi_low_lun_init)
				(slp, ti, li, SCSI_LOW_INFO_REVOKE));
		}
		scsi_low_calcf_lun(li);
	}
}

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

	if ((cb = slp->sl_Qnexus) != NULL)
	{
		topcb = scsi_low_revoke_ccb(slp, cb, fdone);
	}
	else
	{
		topcb = NULL;
	}

	for (ti = TAILQ_FIRST(&slp->sl_titab); ti != NULL;
	     ti = TAILQ_NEXT(ti, ti_chain))
	{
		scsi_low_reset_nexus_target(slp, ti, fdone);
		scsi_low_bus_release(slp, ti);
		scsi_low_init_msgsys(slp, ti);
	}

	if (topcb != NULL)
	{
		topcb->ccb_flags |= CCB_STARTQ;
		TAILQ_INSERT_HEAD(&slp->sl_start, topcb, ccb_chain);
	}

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

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

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);

	device_printf(slp->sl_dev, "try to reset scsi bus  ");
	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)
		device_printf(slp->sl_dev, "scsi bus restart. reason: %s\n", 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 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;

	if (li == NULL)
		return NULL;

	cb = TAILQ_FIRST(&li->li_discq);
	for ( ; cb != NULL; cb = TAILQ_NEXT(cb, ccb_chain))
		if (cb->ccb_tag == tag)
			goto found;
	return cb;

	/* 
	 * establish our ccb nexus
	 */
found:
#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
	{
		device_printf(slp->sl_dev, "nexus(0x%lx) abort check start\n",
		    (u_long) cb);
		cb->ccb_flags |= (CCB_NORETRY | CCB_SILENT);
		scsi_low_revoke_ccb(slp, cb, 1);
		return NULL;
	}

	if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id) != 0)
	{
		if (cb->ccb_omsgoutflag == 0)
			scsi_low_ccb_message_assert(cb, SCSI_LOW_MSG_NOOP);
	}
#endif	/* SCSI_LOW_DEBUG */

	TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
	cb->ccb_flags &= ~CCB_DISCQ;
	slp->sl_Qnexus = cb;

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

	slp->sl_disc --;
	ti->ti_disc --;
	li->li_disc --;

	/* inform "ccb nexus established" to the host driver */
	(*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);

	/* check msg */
	if (cb->ccb_msgoutflag != 0)
	{
		scsi_low_ccb_message_exec(slp, cb);
	}

	return cb;
}

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

	/* 
	 * Check select vs reselected collision.
	 */

	if ((cb = slp->sl_selid) != NULL)
	{
		scsi_low_arbit_fail(slp, cb);
#ifdef	SCSI_LOW_STATICS
		scsi_low_statics.nexus_conflict ++;
#endif	/* SCSI_LOW_STATICS */
	}

	/* 
	 * Check if no current active nexus.
	 */
	if (slp->sl_Tnexus != 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 && ti->ti_phase != PH_NULL)
	{
		s = "phase mismatch";
		goto world_restart;
	}

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

	/* 
	 * Establish our target nexus
	 */
	SCSI_LOW_SETUP_PHASE(ti, PH_RESEL);
	slp->sl_Tnexus = ti;
#ifdef	SCSI_LOW_STATICS
	scsi_low_statics.nexus_reselected ++;
#endif	/* SCSI_LOW_STATICS */
	return ti;

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

/**************************************************************
 * cmd out pointer setup
 **************************************************************/
int
scsi_low_cmd(slp, ti)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
{
	struct slccb *cb = slp->sl_Qnexus;
	
	slp->sl_ph_count ++;
	if (cb == NULL)
	{
		/*
		 * no ccb, 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;
		slp->sl_error |= FATALIO;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
		SCSI_LOW_INFO(slp, ti, "CMDOUT: ccb nexus not found");
		return EINVAL;
	}
	else 
	{
#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_CMDLNK_CHECK, ti->ti_id))
		{
			scsi_low_test_cmdlnk(slp, cb);
		}
#endif	/* SCSI_LOW_DEBUG */
	}
	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 = slp->sl_Qnexus;

	if (cb != NULL && direction == cb->ccb_sscp.scp_direction)
	{
		*bp = cb->bp;
		return 0;
	}

	slp->sl_error |= (FATALIO | PDMAERR);
	slp->sl_scp.scp_datalen = 0;
	slp->sl_scp.scp_direction = direction;
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
	if (ti->ti_ophase != ti->ti_phase)
	{
		char *s;

		if (cb == NULL)
			s = "DATA PHASE: ccb nexus not found";
		else
			s = "DATA PHASE: xfer direction mismatch";
		SCSI_LOW_INFO(slp, ti, s);
	}

	*bp = NULL;
	return EINVAL;
}

/**************************************************************
 * 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_WIDTHP(ti) ((ti)->ti_msgin[3])
#define	MSGIN_DATA_LAST	0x30

static int scsi_low_errfunc_synch(struct scsi_low_softc *, u_int);
static int scsi_low_errfunc_wide(struct scsi_low_softc *, u_int);
static int scsi_low_errfunc_identify(struct scsi_low_softc *, u_int);
static int scsi_low_errfunc_qtag(struct scsi_low_softc *, u_int);

static int scsi_low_msgfunc_synch(struct scsi_low_softc *);
static int scsi_low_msgfunc_wide(struct scsi_low_softc *);
static int scsi_low_msgfunc_identify(struct scsi_low_softc *);
static int scsi_low_msgfunc_abort(struct scsi_low_softc *);
static int scsi_low_msgfunc_qabort(struct scsi_low_softc *);
static int scsi_low_msgfunc_qtag(struct scsi_low_softc *);
static int scsi_low_msgfunc_reset(struct scsi_low_softc *);

struct scsi_low_msgout_data {
	u_int	md_flags;
	u_int8_t md_msg;
	int (*md_msgfunc)(struct scsi_low_softc *);
	int (*md_errfunc)(struct scsi_low_softc *, u_int);
#define	MSG_RELEASE_ATN	0x0001
	u_int md_condition;
};

struct scsi_low_msgout_data scsi_low_msgout_data[] = {
/* 0 */	{SCSI_LOW_MSG_RESET, MSG_RESET, scsi_low_msgfunc_reset, NULL, MSG_RELEASE_ATN},
/* 1 */ {SCSI_LOW_MSG_REJECT, MSG_REJECT, NULL, NULL, MSG_RELEASE_ATN},
/* 2 */ {SCSI_LOW_MSG_PARITY, MSG_PARITY, NULL, NULL, MSG_RELEASE_ATN},
/* 3 */ {SCSI_LOW_MSG_ERROR, MSG_I_ERROR, NULL, NULL, MSG_RELEASE_ATN},
/* 4 */ {SCSI_LOW_MSG_IDENTIFY, MSG_IDENTIFY, scsi_low_msgfunc_identify, scsi_low_errfunc_identify, 0},
/* 5 */ {SCSI_LOW_MSG_ABORT, MSG_ABORT, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
/* 6 */ {SCSI_LOW_MSG_TERMIO, MSG_TERM_IO, NULL, NULL, MSG_RELEASE_ATN},
/* 7 */ {SCSI_LOW_MSG_SIMPLE_QTAG,  MSG_SIMPLE_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
/* 8 */ {SCSI_LOW_MSG_ORDERED_QTAG, MSG_ORDERED_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
/* 9 */{SCSI_LOW_MSG_HEAD_QTAG,  MSG_HEAD_QTAG, scsi_low_msgfunc_qtag, scsi_low_errfunc_qtag, 0},
/* 10 */ {SCSI_LOW_MSG_ABORT_QTAG, MSG_ABORT_QTAG, scsi_low_msgfunc_qabort, NULL,  MSG_RELEASE_ATN},
/* 11 */ {SCSI_LOW_MSG_CLEAR_QTAG, MSG_CLEAR_QTAG, scsi_low_msgfunc_abort, NULL, MSG_RELEASE_ATN},
/* 12 */{SCSI_LOW_MSG_WIDE, MSG_EXTEND, scsi_low_msgfunc_wide, scsi_low_errfunc_wide, MSG_RELEASE_ATN},
/* 13 */{SCSI_LOW_MSG_SYNCH, MSG_EXTEND, scsi_low_msgfunc_synch, scsi_low_errfunc_synch, MSG_RELEASE_ATN},
/* 14 */{SCSI_LOW_MSG_NOOP, MSG_NOOP, NULL, NULL, MSG_RELEASE_ATN},
/* 15 */{SCSI_LOW_MSG_ALL, 0},
};

static int scsi_low_msginfunc_ext(struct scsi_low_softc *);
static int scsi_low_synch(struct scsi_low_softc *);
static int scsi_low_wide(struct scsi_low_softc *);
static int scsi_low_msginfunc_msg_reject(struct scsi_low_softc *);
static int scsi_low_msginfunc_rejop(struct scsi_low_softc *);
static int scsi_low_msginfunc_rp(struct scsi_low_softc *);
static int scsi_low_msginfunc_sdp(struct scsi_low_softc *);
static int scsi_low_msginfunc_disc(struct scsi_low_softc *);
static int scsi_low_msginfunc_cc(struct scsi_low_softc *);
static int scsi_low_msginfunc_lcc(struct scsi_low_softc *);
static int scsi_low_msginfunc_parity(struct scsi_low_softc *);
static int scsi_low_msginfunc_noop(struct scsi_low_softc *);
static int scsi_low_msginfunc_simple_qtag(struct scsi_low_softc *);
static int scsi_low_msginfunc_i_wide_residue(struct scsi_low_softc *);

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

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_rp},
/* 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_lcc},
/* b */ {1,	scsi_low_msginfunc_lcc},
/* 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_simple_qtag},
/* 0x21 */ {2,	scsi_low_msginfunc_rejop},
/* 0x22 */ {2,	scsi_low_msginfunc_rejop},
/* 0x23 */ {2,	scsi_low_msginfunc_i_wide_residue},
/* 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 */
};

/**************************************************************
 * msgout
 **************************************************************/
static int
scsi_low_msgfunc_synch(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	int ptr = ti->ti_msgoutlen;

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

static int
scsi_low_msgfunc_wide(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	int ptr = ti->ti_msgoutlen;

	ti->ti_msgoutstr[ptr + 1] = MSG_EXTEND_WIDELEN;
	ti->ti_msgoutstr[ptr + 2] = MSG_EXTEND_WIDECODE;
	ti->ti_msgoutstr[ptr + 3] = ti->ti_width;
	return MSG_EXTEND_WIDELEN + 2;
}

static int
scsi_low_msgfunc_identify(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	struct lun_info *li = slp->sl_Lnexus;
	struct slccb *cb = slp->sl_Qnexus;
	int ptr = ti->ti_msgoutlen;
	u_int8_t msg;

	msg = MSG_IDENTIFY;
	if (cb == NULL)
	{
		slp->sl_error |= FATALIO;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
		SCSI_LOW_INFO(slp, ti, "MSGOUT: nexus unknown");
	}
	else
	{
		if (scsi_low_is_disconnect_ok(cb) != 0)
			msg |= (MSG_IDENTIFY_DISCPRIV | li->li_lun);
		else
			msg |= li->li_lun;

		if (ti->ti_phase == PH_MSGOUT)
		{
			(*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
			if (cb->ccb_tag == SCSI_LOW_UNKTAG)
			{
				(*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
			}
		}
	}
	ti->ti_msgoutstr[ptr + 0] = msg;
	return 1;
}

static int
scsi_low_msgfunc_abort(slp)
	struct scsi_low_softc *slp;
{

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_ABORT);
	return 1;
}

static int
scsi_low_msgfunc_qabort(slp)
	struct scsi_low_softc *slp;
{

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_TERM);
	return 1;
}

static int
scsi_low_msgfunc_reset(slp)
	struct scsi_low_softc *slp;
{

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_RESET);
	return 1;
}

static int
scsi_low_msgfunc_qtag(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	struct slccb *cb = slp->sl_Qnexus;
	int ptr = ti->ti_msgoutlen;

	if (cb == NULL || cb->ccb_tag == SCSI_LOW_UNKTAG)
	{
		ti->ti_msgoutstr[ptr + 0] = MSG_NOOP;
		return 1;
	}
	else
	{
		ti->ti_msgoutstr[ptr + 1] = (u_int8_t) cb->ccb_tag;
		if (ti->ti_phase == PH_MSGOUT)
		{
			(*slp->sl_funcs->scsi_low_establish_ccb_nexus) (slp);
		}
	}
	return 2;
}

/*
 * The following functions are called when targets give unexpected
 * responces in msgin (after msgout).
 */
static int
scsi_low_errfunc_identify(slp, msgflags)
	struct scsi_low_softc *slp;
	u_int msgflags;
{

	if (slp->sl_Lnexus != NULL)
	{
	        slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_DISC;
		scsi_low_calcf_lun(slp->sl_Lnexus);
	}
	return 0;
}

static int
scsi_low_errfunc_synch(slp, msgflags)
	struct scsi_low_softc *slp;
	u_int msgflags;
{
	struct targ_info *ti = slp->sl_Tnexus;

	MSGIN_PERIOD(ti) = 0;
	MSGIN_OFFSET(ti) = 0;
	scsi_low_synch(slp);
	return 0;
}

static int
scsi_low_errfunc_wide(slp, msgflags)
	struct scsi_low_softc *slp;
	u_int msgflags;
{
	struct targ_info *ti = slp->sl_Tnexus;

	MSGIN_WIDTHP(ti) = 0;
	scsi_low_wide(slp);
	return 0;
}

static int
scsi_low_errfunc_qtag(slp, msgflags)
	struct scsi_low_softc *slp;
	u_int msgflags;
{

	if ((msgflags & SCSI_LOW_MSG_REJECT) != 0)
	{
		if (slp->sl_Qnexus != NULL)
		{
			scsi_low_deactivate_qtag(slp->sl_Qnexus);
		}
		if (slp->sl_Lnexus != NULL)
		{
			slp->sl_Lnexus->li_cfgflags &= ~SCSI_LOW_QTAG;
			scsi_low_calcf_lun(slp->sl_Lnexus);
		}
		device_printf(slp->sl_dev, "scsi_low: qtag msg rejected\n");
	}
	return 0;
}


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

#ifdef	SCSI_LOW_DIAGNOSTIC
	if (ti != slp->sl_Tnexus)
	{
		scsi_low_print(slp, NULL);
		panic("scsi_low_msgout: Target nexus inconsistent");
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	slp->sl_ph_count ++;
	if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
	{
		device_printf(slp->sl_dev, "too many phase changes\n");
		slp->sl_error |= FATALIO;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 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 ((fl & SCSI_LOW_MSGOUT_INIT) != 0)
	{
		ti->ti_omsgflags = 0;
		ti->ti_emsgflags = 0;
	}
	else if (slp->sl_atten == 0)
	{
	/* STEP II.
	 * We did not assert attention, however still our target required
	 * msgs. Resend previous msgs. 
	 */
		ti->ti_msgflags |= ti->ti_omsgflags;
		ti->ti_omsgflags = 0;
#ifdef	SCSI_LOW_DIAGNOSTIC
		device_printf(slp->sl_dev, "scsi_low_msgout: retry msgout\n");
#endif	/* SCSI_LOW_DIAGNOSTIC */
	}

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

	/* STEP IV.
	 * Process all msgs
	 */
	ti->ti_msgoutlen = 0;
	slp->sl_clear_atten = 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) (slp);
			else
				len = 1;

#ifdef	SCSI_LOW_DIAGNOSTIC
			scsi_low_msg_log_write(&ti->ti_log_msgout,
			       &ti->ti_msgoutstr[ti->ti_msgoutlen], len);
#endif	/* SCSI_LOW_DIAGNOSTIC */

			ti->ti_msgoutlen += len;
			if ((mdp->md_condition & MSG_RELEASE_ATN) != 0)
			{
				slp->sl_clear_atten = 1;
				break;
			}

			if ((fl & SCSI_LOW_MSGOUT_UNIFY) == 0 ||
			    ti->ti_msgflags == 0)
				break;

			if (ti->ti_msgoutlen >= SCSI_LOW_MAX_MSGLEN - 5)
				break;
		}
	}

	if (scsi_low_is_msgout_continue(ti, 0) == 0)
		slp->sl_clear_atten = 1;

	return ti->ti_msgoutlen;
}

/**************************************************************
 * msgin
 **************************************************************/
static int
scsi_low_msginfunc_noop(slp)
	struct scsi_low_softc *slp;
{

	return 0;
}

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

	device_printf(slp->sl_dev, "MSGIN: msg 0x%x rejected\n", (u_int) msg);
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

static int
scsi_low_msginfunc_cc(slp)
	struct scsi_low_softc *slp;
{
	struct lun_info *li;

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_CMDC);

	/* validate status */
	if (slp->sl_Qnexus == NULL)
		return ENOENT;

	slp->sl_Qnexus->ccb_sscp.scp_status = slp->sl_scp.scp_status;
 	li = slp->sl_Lnexus;
	switch (slp->sl_scp.scp_status)
	{
	case ST_GOOD:
		li->li_maxnqio = li->li_maxnexus;
		break;

	case ST_CHKCOND:
		li->li_maxnqio = 0;
		if (li->li_qflags & SCSI_LOW_QFLAG_CA_QCLEAR)
			scsi_low_reset_nexus_lun(slp, li, 0);
		break;

	case ST_BUSY:
		li->li_maxnqio = 0;
		break;

	case ST_QUEFULL:
		if (li->li_maxnexus >= li->li_nqio)
			li->li_maxnexus = li->li_nqio - 1;
		li->li_maxnqio = li->li_maxnexus;
		break;

	case ST_INTERGOOD:
	case ST_INTERMET:
		slp->sl_error |= MSGERR;
		break;

	default:
		break;
	}
	return 0;
}

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

	ti = slp->sl_Tnexus;
 	li = slp->sl_Lnexus;
	if ((cb = slp->sl_Qnexus) == NULL)
		goto bad;
		
	cb->ccb_sscp.scp_status = slp->sl_scp.scp_status;
	switch (slp->sl_scp.scp_status)
	{
	case ST_INTERGOOD:
	case ST_INTERMET:
		li->li_maxnqio = li->li_maxnexus;
		break;

	default:
		slp->sl_error |= MSGERR;
		break;
	}

	if ((li->li_flags & SCSI_LOW_LINK) == 0)
		goto bad;

	cb->ccb_error |= slp->sl_error;
	if (cb->ccb_error != 0)
		goto bad;

	for (ncb = TAILQ_FIRST(&slp->sl_start); ncb != NULL;
	     ncb = TAILQ_NEXT(ncb, ccb_chain))
	{
		if (ncb->li == li)
			goto cmd_link_start;
	}


bad:
	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_LCTERM);
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return EIO;

cmd_link_start:
	ncb->ccb_flags &= ~CCB_STARTQ;
	TAILQ_REMOVE(&slp->sl_start, ncb, ccb_chain);

	scsi_low_dealloc_qtag(ncb);
	ncb->ccb_tag = cb->ccb_tag;
	ncb->ccb_otag = cb->ccb_otag;
	cb->ccb_tag = SCSI_LOW_UNKTAG;
	cb->ccb_otag = SCSI_LOW_UNKTAG;
	if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
		panic("%s: linked ccb retried",
		    device_get_nameunit(slp->sl_dev));

	slp->sl_Qnexus = ncb;
	slp->sl_ph_count = 0;

	ncb->ccb_error = 0;
	ncb->ccb_datalen = -1;
	ncb->ccb_scp.scp_status = ST_UNKNOWN;
	ncb->ccb_flags &= ~CCB_INTERNAL;

	scsi_low_init_msgsys(slp, ti);

	(*slp->sl_osdep_fp->scsi_low_osdep_ccb_setup) (slp, ncb);

	if (ncb->ccb_tcmax < SCSI_LOW_MIN_TOUT)
		ncb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
	ncb->ccb_tc = ncb->ccb_tcmax;

	/* setup saved scsi data pointer */
	ncb->ccb_sscp = ncb->ccb_scp;
	slp->sl_scp = ncb->ccb_sscp;
	slp->sl_error = ncb->ccb_error;

#ifdef	SCSI_LOW_DIAGNOSTIC
	scsi_low_msg_log_init(&ti->ti_log_msgin);
	scsi_low_msg_log_init(&ti->ti_log_msgout);
#endif	/* SCSI_LOW_DIAGNOSTIC */
	return EJUSTRETURN;
}

static int
scsi_low_msginfunc_disc(slp)
	struct scsi_low_softc *slp;
{

	SCSI_LOW_SETUP_MSGPHASE(slp, MSGPH_DISC);
	return 0;
}

static int
scsi_low_msginfunc_sdp(slp)
	struct scsi_low_softc *slp;
{
	struct slccb *cb = slp->sl_Qnexus;

	if (cb != NULL)
	{
		cb->ccb_sscp.scp_datalen = slp->sl_scp.scp_datalen;
		cb->ccb_sscp.scp_data = slp->sl_scp.scp_data;
	}
	else
		scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

static int
scsi_low_msginfunc_rp(slp)
	struct scsi_low_softc *slp;
{

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

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

	if ((MSGIN_PERIOD(ti) >= ti->ti_maxsynch.period &&
	     MSGIN_OFFSET(ti) <= ti->ti_maxsynch.offset) ||
	     MSGIN_OFFSET(ti) == 0)
	{
		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.
		 */
		ti->ti_maxsynch.period = 0;
		ti->ti_maxsynch.offset = 0;
		device_printf(slp->sl_dev,
		    "target brain damaged. async transfer\n");
		return EINVAL;
	}

	ti->ti_maxsynch.period = period;
	ti->ti_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.
		 */
		device_printf(slp->sl_dev,
		    "synch neg failed. retry synch msg neg ...\n");
		return error;
	}

	ti->ti_osynch = ti->ti_maxsynch;
	if (offset > 0)
	{
		ti->ti_setup_msg_done |= SCSI_LOW_MSG_SYNCH;
	}

	/* inform data */
	if ((slp->sl_show_result & SHOW_SYNCH_NEG) != 0)
	{
#ifdef	SCSI_LOW_NEGOTIATE_BEFORE_SENSE
		struct slccb *cb = slp->sl_Qnexus;

		if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
			return 0;
#endif	/* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */

		device_printf(slp->sl_dev,
		    "(%d:*): <%s> offset %d period %dns ",
		    ti->ti_id, s, offset, period * 4);

		if (period != 0)
		{
			speed = 1000 * 10 / (period * 4);
			printf("%d.%d M/s", speed / 10, speed % 10);
		}
		printf("\n");
	}
	return 0;
}

static int
scsi_low_wide(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	int error;

	ti->ti_width = MSGIN_WIDTHP(ti);
	error = (*slp->sl_funcs->scsi_low_msg) (slp, ti, SCSI_LOW_MSG_WIDE);
	if (error != 0)
	{
		/* XXX:
		 * Current width is not acceptable for our adapter.
		 * The adapter changes max width.
		 */
		device_printf(slp->sl_dev,
		    "wide neg failed. retry wide msg neg ...\n");
		return error;
	}

	ti->ti_owidth = ti->ti_width;
	if (ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
	{
		ti->ti_setup_msg_done |= 
			(SCSI_LOW_MSG_SYNCH | SCSI_LOW_MSG_WIDE);
	}
		
	/* inform data */
	if ((slp->sl_show_result & SHOW_WIDE_NEG) != 0)
	{
#ifdef	SCSI_LOW_NEGOTIATE_BEFORE_SENSE
		struct slccb *cb = slp->sl_Qnexus;

		if (cb != NULL && (cb->ccb_flags & CCB_SENSE) != 0)
			return 0;
#endif	/* SCSI_LOW_NEGOTIATE_BEFORE_SENSE */

		device_printf(slp->sl_dev, "(%d:*): transfer width %d bits\n",
		    ti->ti_id, 1 << (3 + ti->ti_width));
	}
	return 0;
}

static int
scsi_low_msginfunc_simple_qtag(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	scsi_low_tag_t etag = (scsi_low_tag_t) ti->ti_msgin[1];

	if (slp->sl_Qnexus != NULL)
	{
		if (slp->sl_Qnexus->ccb_tag != etag)
		{
			slp->sl_error |= FATALIO;
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
			SCSI_LOW_INFO(slp, ti, "MSGIN: qtag mismatch");
		}
	}
	else if (scsi_low_establish_ccb(ti, slp->sl_Lnexus, etag) == NULL)
	{
#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id))
			return 0;
#endif	/* SCSI_LOW_DEBUG */

		slp->sl_error |= FATALIO;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT_QTAG, 0);
		SCSI_LOW_INFO(slp, ti, "MSGIN: taged ccb not found");
	}
	return 0;
}

static int
scsi_low_msginfunc_i_wide_residue(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;
	struct slccb *cb = slp->sl_Qnexus;
	int res = (int) ti->ti_msgin[1];

	if (cb == NULL || res <= 0 ||
	    (ti->ti_width == SCSI_LOW_BUS_WIDTH_16 && res > 1) ||
	    (ti->ti_width == SCSI_LOW_BUS_WIDTH_32 && res > 3))
		return EINVAL;
		
	if (slp->sl_scp.scp_datalen + res > cb->ccb_scp.scp_datalen)
		return EINVAL;

	slp->sl_scp.scp_datalen += res;
	slp->sl_scp.scp_data -= res;
	scsi_low_data_finish(slp);
	return 0;
}

static int
scsi_low_msginfunc_ext(slp)
	struct scsi_low_softc *slp;
{
	struct slccb *cb = slp->sl_Qnexus;
	struct lun_info *li = slp->sl_Lnexus;
	struct targ_info *ti = slp->sl_Tnexus;
	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(slp);
		if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_SYNCH) == 0)
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_SYNCH, 0);

#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
		{
			scsi_low_test_atten(slp, ti, SCSI_LOW_MSG_SYNCH);
		}
#endif	/* SCSI_LOW_DEBUG */
		return 0;

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

		retry = scsi_low_wide(slp);
		if (retry != 0 || (ti->ti_emsgflags & SCSI_LOW_MSG_WIDE) == 0)
			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 int
scsi_low_msginfunc_parity(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti = slp->sl_Tnexus;

	/* only I -> T, invalid! */
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_REJECT, 0);
	return 0;
}

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

	if (ti->ti_emsgflags != 0)
	{
		device_printf(slp->sl_dev, "msg flags [0x%x] rejected\n",
		    ti->ti_emsgflags);
		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) (slp, msgflags);
				break;
			}
		}
		return 0;
	}
	else
	{
		SCSI_LOW_INFO(slp, ti, "MSGIN: rejected msg not found");
		slp->sl_error |= MSGERR;
	}
	return EINVAL;
}

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

#ifdef	SCSI_LOW_DIAGNOSTIC
	if (ti != slp->sl_Tnexus)
	{
		scsi_low_print(slp, NULL);
		panic("scsi_low_msgin: Target nexus inconsistent");
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	/*
	 * Phase changes, clear the pointer.
	 */
	if (ti->ti_ophase != ti->ti_phase)
	{
		MSGINPTR_CLR(ti);
		ti->ti_msgin_parity_error = 0;

		slp->sl_ph_count ++;
		if (slp->sl_ph_count > SCSI_LOW_MAX_PHCHANGES)
		{
			device_printf(slp->sl_dev, "too many phase changes\n");
			slp->sl_error |= FATALIO;
			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
		}
	}

	/*
	 * Store a current messages byte into buffer and 
	 * wait for the completion of the current msg.
	 */
	ti->ti_msgin[ti->ti_msginptr ++] = (u_int8_t) 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);
	}	

	/*
	 * Check parity errors.
	 */
	if ((c & SCSI_LOW_DATA_PE) != 0)
	{
		ti->ti_msgin_parity_error ++;
		scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
		goto out;
	}

	if (ti->ti_msgin_parity_error != 0)
		goto out;

	/*
	 * 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;
	}

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

	/*
	 * Do process.
	 */
	if ((msg & MSG_IDENTIFY) == 0)
	{
		if (((*sdp->md_msgfunc) (slp)) == EJUSTRETURN)
			return EJUSTRETURN;
	}
	else
	{
		li = slp->sl_Lnexus;
		if (li == NULL)
		{
			li = scsi_low_alloc_li(ti, MSGCMD_LUN(msg), 0);
			if (li == NULL)
				goto badlun;
			slp->sl_Lnexus = li;
			(*slp->sl_funcs->scsi_low_establish_lun_nexus) (slp);
		}	
		else
		{
			if (MSGCMD_LUN(msg) != li->li_lun)
				goto badlun;
		}

		if (slp->sl_Qnexus == NULL && li->li_nqio == 0)
		{
			if (!scsi_low_establish_ccb(ti, li, SCSI_LOW_UNKTAG))
			{
#ifdef	SCSI_LOW_DEBUG
				if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_NEXUS_CHECK, ti->ti_id) != 0)
				{
					goto out;
				}
#endif	/* SCSI_LOW_DEBUG */
				goto badlun;
			}
		}
	}
	goto out;

	/*
	 * Msg process completed, reset msgin pointer and assert ATN if desired.
	 */
badlun:
	slp->sl_error |= FATALIO;
	scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 0);
	SCSI_LOW_INFO(slp, ti, "MSGIN: identify wrong");

out:
	if (ti->ti_msginptr < ti->ti_msginlen)
		return EJUSTRETURN;

#ifdef	SCSI_LOW_DIAGNOSTIC
	scsi_low_msg_log_write(&ti->ti_log_msgin,
			       &ti->ti_msgin[0], ti->ti_msginlen);
#endif	/* SCSI_LOW_DIAGNOSTIC */

	MSGINPTR_CLR(ti);
	return 0;
}

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

	/* check phase completion */
	switch (slp->sl_msgphase)
	{
	case MSGPH_RESET:
		scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
		scsi_low_msginfunc_cc(slp);
		scsi_low_reset_nexus_target(slp, slp->sl_Tnexus, 0);
		goto io_resume;

	case MSGPH_ABORT:
		scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
		scsi_low_msginfunc_cc(slp);
		scsi_low_reset_nexus_lun(slp, slp->sl_Lnexus, 0);
		goto io_resume;

	case MSGPH_TERM:
		scsi_low_statusin(slp, slp->sl_Tnexus, ST_GOOD);
		scsi_low_msginfunc_cc(slp);
		goto io_resume;

	case MSGPH_DISC:
		if (cb != NULL)
		{
			struct lun_info *li;

			li = cb->li;
			TAILQ_INSERT_TAIL(&li->li_discq, cb, ccb_chain);
			cb->ccb_flags |= CCB_DISCQ;
			cb->ccb_error |= slp->sl_error;
			li->li_disc ++;
			ti->ti_disc ++;
			slp->sl_disc ++;
		}

#ifdef	SCSI_LOW_STATICS
		scsi_low_statics.nexus_disconnected ++;
#endif	/* SCSI_LOW_STATICS */

#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_DISC, ti->ti_id) != 0)
		{
			printf("## SCSI_LOW_DISCONNECTED ===============\n");
			scsi_low_print(slp, NULL);
		}
#endif	/* SCSI_LOW_DEBUG */
		break;

	case MSGPH_NULL:
		slp->sl_error |= FATALIO;
		if (ti->ti_phase == PH_SELSTART)
			slp->sl_error |= SELTIMEOUTIO;
		else
			slp->sl_error |= UBFERR;
		/* fall through */

	case MSGPH_LCTERM:
	case MSGPH_CMDC:
io_resume:
		if (cb == NULL)
			break;

#ifdef	SCSI_LOW_DEBUG
		if (SCSI_LOW_DEBUG_TEST_GO(SCSI_LOW_ATTEN_CHECK, ti->ti_id))
		{
			if (cb->ccb_omsgoutflag == SCSI_LOW_MSG_NOOP &&
			    (cb->ccb_msgoutflag != 0 ||
			     (ti->ti_msgflags & SCSI_LOW_MSG_NOOP)))
			{
				scsi_low_info(slp, ti, "ATTEN CHECK FAILED");
			}
		}
#endif	/* SCSI_LOW_DEBUG */

		cb->ccb_error |= slp->sl_error;
		if (scsi_low_done(slp, cb) == SCSI_LOW_DONE_RETRY)
		{
			cb->ccb_flags |= CCB_STARTQ;
			TAILQ_INSERT_HEAD(&slp->sl_start, cb, ccb_chain);
		}
		break;
	}

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

/**********************************************************
 * TAG operations
 **********************************************************/
static int
scsi_low_alloc_qtag(cb)
	struct slccb *cb;
{
	struct lun_info *li = cb->li;
	scsi_low_tag_t etag;

	if (cb->ccb_otag != SCSI_LOW_UNKTAG)
		return 0;

#ifndef	SCSI_LOW_ALT_QTAG_ALLOCATE
	etag = ffs(li->li_qtagbits);
	if (etag == 0)
		return ENOSPC;

	li->li_qtagbits &= ~(1 << (etag - 1));
	cb->ccb_otag = etag;
	return 0;

#else	/* SCSI_LOW_ALT_QTAG_ALLOCATE */
	for (etag = li->li_qd ; li->li_qd < SCSI_LOW_MAXNEXUS; li->li_qd ++)
		if (li->li_qtagarray[li->li_qd] == 0)
			goto found;

	for (li->li_qd = 0; li->li_qd < etag; li->li_qd ++)
		if (li->li_qtagarray[li->li_qd] == 0)
			goto found;

	return ENOSPC;

found:
	li->li_qtagarray[li->li_qd] ++;
	cb->ccb_otag = (li->li_qd ++);
	return 0;
#endif	/* SCSI_LOW_ALT_QTAG_ALLOCATE */
}
	
static int
scsi_low_dealloc_qtag(cb)
	struct slccb *cb;
{
	struct lun_info *li = cb->li;
	scsi_low_tag_t etag;

	if (cb->ccb_otag == SCSI_LOW_UNKTAG)
		return 0;

#ifndef	SCSI_LOW_ALT_QTAG_ALLOCATE
	etag = cb->ccb_otag - 1;
#ifdef	SCSI_LOW_DIAGNOSTIC
	if (etag >= sizeof(li->li_qtagbits) * NBBY)
		panic("scsi_low_dealloc_tag: illegal tag");
#endif	/* SCSI_LOW_DIAGNOSTIC */
	li->li_qtagbits |= (1 << etag);

#else	/* SCSI_LOW_ALT_QTAG_ALLOCATE */
	etag = cb->ccb_otag;
#ifdef	SCSI_LOW_DIAGNOSTIC
	if (etag >= SCSI_LOW_MAXNEXUS)
		panic("scsi_low_dealloc_tag: illegal tag");
#endif	/* SCSI_LOW_DIAGNOSTIC */
	li->li_qtagarray[etag] --;
#endif	/* SCSI_LOW_ALT_QTAG_ALLOCATE */

	cb->ccb_otag = SCSI_LOW_UNKTAG;
	return 0;
}

static struct slccb *
scsi_low_revoke_ccb(slp, cb, fdone)
	struct scsi_low_softc *slp;
	struct slccb *cb;
	int fdone;
{
	struct targ_info *ti = cb->ti;
	struct lun_info *li = cb->li;

#ifdef	SCSI_LOW_DIAGNOSTIC
	if ((cb->ccb_flags & (CCB_STARTQ | CCB_DISCQ)) == 
	    (CCB_STARTQ | CCB_DISCQ))
	{
		panic("%s: ccb in both queue",
		    device_get_nameunit(slp->sl_dev));
	}
#endif	/* SCSI_LOW_DIAGNOSTIC */

	if ((cb->ccb_flags & CCB_STARTQ) != 0)
	{
		TAILQ_REMOVE(&slp->sl_start, cb, ccb_chain);
	}

	if ((cb->ccb_flags & CCB_DISCQ) != 0)
	{
		TAILQ_REMOVE(&li->li_discq, cb, ccb_chain);
		li->li_disc --;
		ti->ti_disc --;
		slp->sl_disc --;
	}

	cb->ccb_flags &= ~(CCB_STARTQ | CCB_DISCQ | 
			   CCB_SENSE | CCB_CLEARQ | CCB_INTERNAL);

	if (fdone != 0 &&
	    (cb->ccb_rcnt ++ >= slp->sl_max_retry || 
	     (cb->ccb_flags & CCB_NORETRY) != 0))
	{
		cb->ccb_error |= FATALIO;
		cb->ccb_flags &= ~CCB_AUTOSENSE;
		if (scsi_low_done(slp, cb) != SCSI_LOW_DONE_COMPLETE)
			panic("%s: done ccb retried",
			    device_get_nameunit(slp->sl_dev));
		return NULL;
	}
	else
	{
		cb->ccb_error |= PENDINGIO;
		scsi_low_deactivate_qtag(cb);
		scsi_low_ccb_message_retry(cb);
		cb->ccb_tc = cb->ccb_tcmax = SCSI_LOW_MIN_TOUT;
		return cb;
	}
}

static void
scsi_low_reset_nexus_lun(slp, li, fdone)
	struct scsi_low_softc *slp;
	struct lun_info *li;
	int fdone;
{
	struct slccb *cb, *ncb, *ecb;

	if (li == NULL)
		return;

	ecb = NULL;
	for (cb = TAILQ_FIRST(&li->li_discq); cb != NULL; cb = ncb)
	{
		ncb = TAILQ_NEXT(cb, ccb_chain);
		cb = scsi_low_revoke_ccb(slp, cb, fdone);
		if (cb != NULL)
		{
			/*
			 * presumely keep ordering of io
			 */
			cb->ccb_flags |= CCB_STARTQ;
			if (ecb == NULL)
			{
				TAILQ_INSERT_HEAD(&slp->sl_start,\
						  cb, ccb_chain);
			}
			else
			{
				TAILQ_INSERT_AFTER(&slp->sl_start,\
						   ecb, cb, ccb_chain);
			}
			ecb = cb;
		}
	}
}
	
/**************************************************************
 * Qurik setup
 **************************************************************/
static void
scsi_low_calcf_lun(li)
	struct lun_info *li;
{
	struct targ_info *ti = li->li_ti;
	struct scsi_low_softc *slp = ti->ti_sc;
	u_int cfgflags, diskflags;

	if (li->li_flags_valid == SCSI_LOW_LUN_FLAGS_ALL_VALID)
		cfgflags = li->li_cfgflags;
	else
		cfgflags = 0;

	diskflags = li->li_diskflags & li->li_quirks;

	/* disconnect */
	li->li_flags &= ~SCSI_LOW_DISC;
	if ((slp->sl_cfgflags & CFG_NODISC) == 0 &&
	    (diskflags & SCSI_LOW_DISK_DISC) != 0 &&
	    (cfgflags & SCSI_LOW_DISC) != 0)
		li->li_flags |= SCSI_LOW_DISC;

	/* parity */
	li->li_flags |= SCSI_LOW_NOPARITY;
	if ((slp->sl_cfgflags & CFG_NOPARITY) == 0 &&
	    (diskflags & SCSI_LOW_DISK_PARITY) != 0 &&
	    (cfgflags & SCSI_LOW_NOPARITY) == 0)
		li->li_flags &= ~SCSI_LOW_NOPARITY;

	/* qtag */
	if ((slp->sl_cfgflags & CFG_NOQTAG) == 0 &&
	    (cfgflags & SCSI_LOW_QTAG) != 0 &&
	    (diskflags & SCSI_LOW_DISK_QTAG) != 0)
	{
		li->li_flags |= SCSI_LOW_QTAG;
		li->li_maxnexus = SCSI_LOW_MAXNEXUS;
		li->li_maxnqio = li->li_maxnexus;
	}
	else
	{
		li->li_flags &= ~SCSI_LOW_QTAG;
		li->li_maxnexus = 0;
		li->li_maxnqio = li->li_maxnexus;
	}

	/* cmd link */
	li->li_flags &= ~SCSI_LOW_LINK;
	if ((cfgflags & SCSI_LOW_LINK) != 0 &&
	    (diskflags & SCSI_LOW_DISK_LINK) != 0)
		li->li_flags |= SCSI_LOW_LINK;

	/* compatible flags */
	li->li_flags &= ~SCSI_LOW_SYNC;
	if (ti->ti_maxsynch.offset > 0)
		li->li_flags |= SCSI_LOW_SYNC;

#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
	{
		scsi_low_calcf_show(li);
	}
#endif	/* SCSI_LOW_DEBUG */
}

static void
scsi_low_calcf_target(ti)
	struct targ_info *ti;
{
	struct scsi_low_softc *slp = ti->ti_sc;
	u_int offset, period, diskflags;

	diskflags = ti->ti_diskflags & ti->ti_quirks;

	/* synch */
	if ((slp->sl_cfgflags & CFG_ASYNC) == 0 &&
	    (diskflags & SCSI_LOW_DISK_SYNC) != 0)
	{
		offset = ti->ti_maxsynch.offset;
		period = ti->ti_maxsynch.period;
		if (offset == 0 || period == 0)
			offset = period = 0;
	}
	else
	{
		offset = period = 0;
	}
	
	ti->ti_maxsynch.offset = offset;
	ti->ti_maxsynch.period = period;

	/* wide */
	if ((diskflags & SCSI_LOW_DISK_WIDE_32) == 0 &&
	     ti->ti_width > SCSI_LOW_BUS_WIDTH_16)
		ti->ti_width = SCSI_LOW_BUS_WIDTH_16;

	if ((diskflags & SCSI_LOW_DISK_WIDE_16) == 0 &&
	    ti->ti_width > SCSI_LOW_BUS_WIDTH_8)
		ti->ti_width = SCSI_LOW_BUS_WIDTH_8;

	if (ti->ti_flags_valid == SCSI_LOW_TARG_FLAGS_ALL_VALID)
	{
		if (ti->ti_maxsynch.offset != ti->ti_osynch.offset ||
		    ti->ti_maxsynch.period != ti->ti_osynch.period)
			ti->ti_setup_msg |= SCSI_LOW_MSG_SYNCH;
		if (ti->ti_width != ti->ti_owidth)
			ti->ti_setup_msg |= (SCSI_LOW_MSG_WIDE | SCSI_LOW_MSG_SYNCH);

		ti->ti_osynch = ti->ti_maxsynch;
		ti->ti_owidth = ti->ti_width;
	}

#ifdef	SCSI_LOW_DEBUG
	if (SCSI_LOW_DEBUG_GO(SCSI_LOW_DEBUG_CALCF, ti->ti_id) != 0)
	{
		device_printf(slp->sl_dev,
			"(%d:*): max period(%dns) offset(%d) width(%d)\n",
			ti->ti_id,
			ti->ti_maxsynch.period * 4,
			ti->ti_maxsynch.offset,
			ti->ti_width);
	}
#endif	/* SCSI_LOW_DEBUG */
}

static void
scsi_low_calcf_show(li)
	struct lun_info *li;
{
	struct targ_info *ti = li->li_ti;
	struct scsi_low_softc *slp = ti->ti_sc;

	device_printf(slp->sl_dev,
		"(%d:%d): period(%d ns) offset(%d) width(%d) flags 0x%b\n",
		ti->ti_id, li->li_lun,
		ti->ti_maxsynch.period * 4,
		ti->ti_maxsynch.offset,
		ti->ti_width,
		li->li_flags, SCSI_LOW_BITS);
}

#ifdef	SCSI_LOW_START_UP_CHECK
/**************************************************************
 * scsi world start up
 **************************************************************/
static int scsi_low_poll(struct scsi_low_softc *, struct slccb *);

static int
scsi_low_start_up(slp)
	struct scsi_low_softc *slp;
{
	struct targ_info *ti;
	struct lun_info *li;
	struct slccb *cb;
	int target, lun;

	device_printf(slp->sl_dev, "scsi_low: probing all devices ....\n");

	for (target = 0; target < slp->sl_ntargs; target ++)
	{
		if (target == slp->sl_hostid)
		{
			if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
			{
				device_printf(slp->sl_dev,
				    "scsi_low: target %d (host card)\n",
				    target);
			}
			continue;
		}

		if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
		{
			device_printf(slp->sl_dev, "scsi_low: target %d lun ",
			    target);
		}

		ti = slp->sl_ti[target];
		for (lun = 0; lun < slp->sl_nluns; lun ++)
		{
			if ((cb = SCSI_LOW_ALLOC_CCB(1)) == NULL)
				break;

			cb->osdep = NULL;
			cb->bp = NULL;

			li = scsi_low_alloc_li(ti, lun, 1);

			scsi_low_enqueue(slp, ti, li, cb,
					 CCB_AUTOSENSE | CCB_POLLED, 0);

			scsi_low_poll(slp, cb);

			if (li->li_state != SCSI_LOW_LUN_OK)
				break;

			if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
			{
				printf("%d ", lun); 		
			}
		}

		if ((slp->sl_show_result & SHOW_PROBE_RES) != 0)
		{
			printf("\n");
		}
	}
	return 0;
}

static int
scsi_low_poll(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
	int tcount;

	tcount = 0;
	while (slp->sl_nio > 0)
	{
		DELAY((1000 * 1000) / SCSI_LOW_POLL_HZ);

		(*slp->sl_funcs->scsi_low_poll) (slp);
		if (tcount ++ < SCSI_LOW_POLL_HZ / SCSI_LOW_TIMEOUT_HZ)
			continue;

		tcount = 0;
		scsi_low_timeout_check(slp);
	}

	return 0;
}
#endif	/* SCSI_LOW_START_UP_CHECK */

/**********************************************************
 * DEBUG SECTION
 **********************************************************/
#ifdef	SCSI_LOW_DEBUG
static void
scsi_low_test_abort(slp, ti, li)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	struct lun_info *li;
{
	struct slccb *acb;

	if (li->li_disc > 1)
	{
		acb = TAILQ_FIRST(&li->li_discq); 
		if (scsi_low_abort_ccb(slp, acb) == 0)
		{
			device_printf(slp->sl_dev,
			    "aborting ccb(0x%lx) start\n", (u_long) acb);
		}
	}
}

static void
scsi_low_test_atten(slp, ti, msg)
	struct scsi_low_softc *slp;
	struct targ_info *ti;
	u_int msg;
{

	if (slp->sl_ph_count < SCSI_LOW_MAX_ATTEN_CHECK)
		scsi_low_assert_msg(slp, ti, msg, 0);
	else
		device_printf(slp->sl_dev, "atten check OK\n");
}

static void
scsi_low_test_cmdlnk(slp, cb)
	struct scsi_low_softc *slp;
	struct slccb *cb;
{
#define	SCSI_LOW_CMDLNK_NOK	(CCB_INTERNAL | CCB_SENSE | CCB_CLEARQ)

	if ((cb->ccb_flags & SCSI_LOW_CMDLNK_NOK) != 0)
		return;

	memcpy(cb->ccb_scsi_cmd, slp->sl_scp.scp_cmd,
	       slp->sl_scp.scp_cmdlen);
	cb->ccb_scsi_cmd[slp->sl_scp.scp_cmdlen - 1] |= 1;
	slp->sl_scp.scp_cmd = cb->ccb_scsi_cmd;
}
#endif	/* SCSI_LOW_DEBUG */

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

	if (slp == NULL)
		slp = LIST_FIRST(&sl_tab);
	if (s == NULL)
		s = "no message";

	printf(">>>>> SCSI_LOW_INFO(0x%lx): %s\n", (u_long) slp->sl_Tnexus, s);
	if (ti == NULL)
	{
		TAILQ_FOREACH(ti, &slp->sl_titab, ti_chain)
		{
			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 lun_info *li;
	struct slccb *cb;
	struct sc_p *sp;

	if (ti == NULL || ti == slp->sl_Tnexus)
	{
		ti = slp->sl_Tnexus;
		li = slp->sl_Lnexus;
		cb = slp->sl_Qnexus;
	}
	else
	{
		li = LIST_FIRST(&ti->ti_litab);
		cb = TAILQ_FIRST(&li->li_discq);
	}
 	sp = &slp->sl_scp;

	device_printf(slp->sl_dev,
	    "=== NEXUS T(0x%lx) L(0x%lx) Q(0x%lx) NIO(%d) ===\n",
	    (u_long) ti, (u_long) li, (u_long) cb, slp->sl_nio);

	/* target stat */
	if (ti != NULL)
	{
		u_int flags = 0, maxnqio = 0, nqio = 0;
		int lun = CAM_LUN_WILDCARD;

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

		device_printf(slp->sl_dev,
		       "(%d:%d) ph<%s> => ph<%s> DISC(%d) QIO(%d:%d)\n",
		       ti->ti_id, lun, phase[(int) ti->ti_ophase], 
		       phase[(int) ti->ti_phase], ti->ti_disc,
		       nqio, maxnqio);

		if (cb != NULL)
		{
printf("CCB: cmd[0] 0x%x clen 0x%x dlen 0x%x<0x%x stat 0x%x err %b\n",
		       (u_int) cb->ccb_scp.scp_cmd[0],
		       cb->ccb_scp.scp_cmdlen, 
		       cb->ccb_datalen,
		       cb->ccb_scp.scp_datalen,
		       (u_int) cb->ccb_sscp.scp_status,
		       cb->ccb_error, SCSI_LOW_ERRORBITS);
		}

printf("MSGIN: ptr(%x) [%x][%x][%x][%x][%x] attention: %d\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]),
	       slp->sl_atten);

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);

#ifdef	SCSI_LOW_DIAGNOSTIC
		scsi_low_msg_log_show(&ti->ti_log_msgin, "MIN LOG ", 2);
		scsi_low_msg_log_show(&ti->ti_log_msgout, "MOUT LOG", 2);
#endif	/* SCSI_LOW_DIAGNOSTIC */

	}

	printf("SCB: daddr 0x%lx dlen 0x%x stat 0x%x err %b\n",
	       (u_long) sp->scp_data,
	       sp->scp_datalen,
	       (u_int) sp->scp_status,
	       slp->sl_error, SCSI_LOW_ERRORBITS);
}
OpenPOWER on IntegriCloud