aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ciss/ciss.c
blob: 3cfeb90e9f063f7a45c794ab9dbf8e6c763dd58b (plain) (blame)
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
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2001 Michael Smith
 * Copyright (c) 2004 Paul Saab
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

/*
 * Common Interface for SCSI-3 Support driver.
 *
 * CISS claims to provide a common interface between a generic SCSI
 * transport and an intelligent host adapter.
 *
 * This driver supports CISS as defined in the document "CISS Command
 * Interface for SCSI-3 Support Open Specification", Version 1.04,
 * Valence Number 1, dated 20001127, produced by Compaq Computer
 * Corporation.  This document appears to be a hastily and somewhat
 * arbitrarlily cut-down version of a larger (and probably even more
 * chaotic and inconsistent) Compaq internal document.  Various
 * details were also gleaned from Compaq's "cciss" driver for Linux.
 *
 * We provide a shim layer between the CISS interface and CAM,
 * offloading most of the queueing and being-a-disk chores onto CAM.
 * Entry to the driver is via the PCI bus attachment (ciss_probe,
 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
 * ciss_cam_poll).  The Compaq CISS adapters are, however, poor SCSI
 * citizens and we have to fake up some responses to get reasonable
 * behaviour out of them.  In addition, the CISS command set is by no
 * means adequate to support the functionality of a RAID controller,
 * and thus the supported Compaq adapters utilise portions of the
 * control protocol from earlier Compaq adapter families.
 *
 * Note that we only support the "simple" transport layer over PCI.
 * This interface (ab)uses the I2O register set (specifically the post
 * queues) to exchange commands with the adapter.  Other interfaces
 * are available, but we aren't supposed to know about them, and it is
 * dubious whether they would provide major performance improvements
 * except under extreme load.
 *
 * Currently the only supported CISS adapters are the Compaq Smart
 * Array 5* series (5300, 5i, 532).  Even with only three adapters,
 * Compaq still manage to have interface variations.
 *
 *
 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
 * well as Paul Saab at Yahoo! for their assistance in making this
 * driver happen.
 *
 * More thanks must go to John Cagle at HP for the countless hours
 * spent making this driver "work" with the MSA* series storage
 * enclosures.  Without his help (and nagging), this driver could not
 * be used with these enclosures.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/kthread.h>
#include <sys/queue.h>
#include <sys/sysctl.h>

#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/cam_periph.h>
#include <cam/cam_sim.h>
#include <cam/cam_xpt_sim.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_message.h>

#include <machine/bus.h>
#include <machine/endian.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <dev/ciss/cissreg.h>
#include <dev/ciss/cissio.h>
#include <dev/ciss/cissvar.h>

#ifdef CISS_DEBUG
#include "opt_ddb.h"
#endif

static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
    "ciss internal data buffers");

/* pci interface */
static int	ciss_lookup(device_t dev);
static int	ciss_probe(device_t dev);
static int	ciss_attach(device_t dev);
static int	ciss_detach(device_t dev);
static int	ciss_shutdown(device_t dev);

/* (de)initialisation functions, control wrappers */
static int	ciss_init_pci(struct ciss_softc *sc);
static int	ciss_setup_msix(struct ciss_softc *sc);
static int	ciss_init_perf(struct ciss_softc *sc);
static int	ciss_wait_adapter(struct ciss_softc *sc);
static int	ciss_flush_adapter(struct ciss_softc *sc);
static int	ciss_init_requests(struct ciss_softc *sc);
static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
					int nseg, int error);
static int	ciss_identify_adapter(struct ciss_softc *sc);
static int	ciss_init_logical(struct ciss_softc *sc);
static int	ciss_init_physical(struct ciss_softc *sc);
static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
static int	ciss_update_config(struct ciss_softc *sc);
static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
static void	ciss_init_sysctl(struct ciss_softc *sc);
static void	ciss_soft_reset(struct ciss_softc *sc);
static void	ciss_free(struct ciss_softc *sc);
static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
static void	ciss_kill_notify_thread(struct ciss_softc *sc);

/* request submission/completion */
static int	ciss_start(struct ciss_request *cr);
static void	ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
static void	ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
static void	ciss_intr(void *arg);
static void	ciss_perf_intr(void *arg);
static void	ciss_perf_msi_intr(void *arg);
static void	ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
static int	_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
static int	ciss_synch_request(struct ciss_request *cr, int timeout);
static int	ciss_poll_request(struct ciss_request *cr, int timeout);
static int	ciss_wait_request(struct ciss_request *cr, int timeout);
#if 0
static int	ciss_abort_request(struct ciss_request *cr);
#endif

/* request queueing */
static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
static void	ciss_preen_command(struct ciss_request *cr);
static void 	ciss_release_request(struct ciss_request *cr);

/* request helpers */
static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
				      int opcode, void **bufp, size_t bufsize);
static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);

/* DMA map/unmap */
static int	ciss_map_request(struct ciss_request *cr);
static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
					int nseg, int error);
static void	ciss_unmap_request(struct ciss_request *cr);

/* CAM interface */
static int	ciss_cam_init(struct ciss_softc *sc);
static void	ciss_cam_rescan_target(struct ciss_softc *sc,
				       int bus, int target);
static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
static void	ciss_cam_poll(struct cam_sim *sim);
static void	ciss_cam_complete(struct ciss_request *cr);
static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);

/* periodic status monitoring */
static void	ciss_periodic(void *arg);
static void	ciss_nop_complete(struct ciss_request *cr);
static void	ciss_disable_adapter(struct ciss_softc *sc);
static void	ciss_notify_event(struct ciss_softc *sc);
static void	ciss_notify_complete(struct ciss_request *cr);
static int	ciss_notify_abort(struct ciss_softc *sc);
static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);

/* debugging output */
#ifdef DDB
static void	ciss_print_request(struct ciss_request *cr);
#endif
static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
static const char *ciss_name_ldrive_status(int status);
static int	ciss_decode_ldrive_status(int status);
static const char *ciss_name_ldrive_org(int org);
static const char *ciss_name_command_status(int status);

/*
 * PCI bus interface.
 */
static device_method_t ciss_methods[] = {
    /* Device interface */
    DEVMETHOD(device_probe,	ciss_probe),
    DEVMETHOD(device_attach,	ciss_attach),
    DEVMETHOD(device_detach,	ciss_detach),
    DEVMETHOD(device_shutdown,	ciss_shutdown),
    { 0, 0 }
};

static driver_t ciss_pci_driver = {
    "ciss",
    ciss_methods,
    sizeof(struct ciss_softc)
};

/*
 * Control device interface.
 */
static d_open_t		ciss_open;
static d_close_t	ciss_close;
static d_ioctl_t	ciss_ioctl;

static struct cdevsw ciss_cdevsw = {
	.d_version =	D_VERSION,
	.d_flags =	0,
	.d_open =	ciss_open,
	.d_close =	ciss_close,
	.d_ioctl =	ciss_ioctl,
	.d_name =	"ciss",
};

/*
 * This tunable can be set at boot time and controls whether physical devices
 * that are marked hidden by the firmware should be exposed anyways.
 */
static unsigned int ciss_expose_hidden_physical = 0;
TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);

static unsigned int ciss_nop_message_heartbeat = 0;
TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);

/*
 * This tunable can force a particular transport to be used:
 * <= 0 : use default
 *    1 : force simple
 *    2 : force performant
 */
static int ciss_force_transport = 0;
TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);

/*
 * This tunable can force a particular interrupt delivery method to be used:
 * <= 0 : use default
 *    1 : force INTx
 *    2 : force MSIX
 */
static int ciss_force_interrupt = 0;
TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);


/************************************************************************
 * CISS adapters amazingly don't have a defined programming interface
 * value.  (One could say some very despairing things about PCI and
 * people just not getting the general idea.)  So we are forced to
 * stick with matching against subvendor/subdevice, and thus have to
 * be updated for every new CISS adapter that appears.
 */
#define CISS_BOARD_UNKNWON	0
#define CISS_BOARD_SA5		1
#define CISS_BOARD_SA5B		2
#define CISS_BOARD_NOMSI	(1<<4)
#define CISS_BOARD_SIMPLE       (1<<5)

static struct
{
    u_int16_t	subvendor;
    u_int16_t	subdevice;
    int		flags;
    char	*desc;
} ciss_vendor_data[] = {
    { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
                                                        "Compaq Smart Array 5300" },
    { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 5i" },
    { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"Compaq Smart Array 532" },
    { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI,	"HP Smart Array 5312" },
    { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
    { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
    { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
    { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
    { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
    { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
    { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
    { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
    { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
    { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
    { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
    { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
    { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
    { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
    { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array E500" },
    { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x323D, CISS_BOARD_SA5,	"HP Smart Array P700m" },
    { 0x103C, 0x3241, CISS_BOARD_SA5,	"HP Smart Array P212" },
    { 0x103C, 0x3243, CISS_BOARD_SA5,	"HP Smart Array P410" },
    { 0x103C, 0x3245, CISS_BOARD_SA5,	"HP Smart Array P410i" },
    { 0x103C, 0x3247, CISS_BOARD_SA5,	"HP Smart Array P411" },
    { 0x103C, 0x3249, CISS_BOARD_SA5,	"HP Smart Array P812" },
    { 0x103C, 0x324A, CISS_BOARD_SA5,	"HP Smart Array P712m" },
    { 0x103C, 0x324B, CISS_BOARD_SA5,	"HP Smart Array" },
    { 0x103C, 0x3350, CISS_BOARD_SA5,   "HP Smart Array P222" },
    { 0x103C, 0x3351, CISS_BOARD_SA5,   "HP Smart Array P420" },
    { 0x103C, 0x3352, CISS_BOARD_SA5,   "HP Smart Array P421" },
    { 0x103C, 0x3353, CISS_BOARD_SA5,   "HP Smart Array P822" },
    { 0x103C, 0x3354, CISS_BOARD_SA5,   "HP Smart Array P420i" },
    { 0x103C, 0x3355, CISS_BOARD_SA5,   "HP Smart Array P220i" },
    { 0x103C, 0x3356, CISS_BOARD_SA5,   "HP Smart Array P721m" },
    { 0x103C, 0x1920, CISS_BOARD_SA5,   "HP Smart Array P430i" },
    { 0x103C, 0x1921, CISS_BOARD_SA5,   "HP Smart Array P830i" },
    { 0x103C, 0x1922, CISS_BOARD_SA5,   "HP Smart Array P430" },
    { 0x103C, 0x1923, CISS_BOARD_SA5,   "HP Smart Array P431" },
    { 0x103C, 0x1924, CISS_BOARD_SA5,   "HP Smart Array P830" },
    { 0x103C, 0x1926, CISS_BOARD_SA5,   "HP Smart Array P731m" },
    { 0x103C, 0x1928, CISS_BOARD_SA5,   "HP Smart Array P230i" },
    { 0x103C, 0x1929, CISS_BOARD_SA5,   "HP Smart Array P530" },
    { 0x103C, 0x192A, CISS_BOARD_SA5,   "HP Smart Array P531" },
    { 0x103C, 0x21BD, CISS_BOARD_SA5,   "HP Smart Array P244br" },
    { 0x103C, 0x21BE, CISS_BOARD_SA5,   "HP Smart Array P741m" },
    { 0x103C, 0x21BF, CISS_BOARD_SA5,   "HP Smart Array H240ar" },
    { 0x103C, 0x21C0, CISS_BOARD_SA5,   "HP Smart Array P440ar" },
    { 0x103C, 0x21C1, CISS_BOARD_SA5,   "HP Smart Array P840ar" },
    { 0x103C, 0x21C2, CISS_BOARD_SA5,   "HP Smart Array P440" },
    { 0x103C, 0x21C3, CISS_BOARD_SA5,   "HP Smart Array P441" },
    { 0x103C, 0x21C5, CISS_BOARD_SA5,   "HP Smart Array P841" },
    { 0x103C, 0x21C6, CISS_BOARD_SA5,   "HP Smart Array H244br" },
    { 0x103C, 0x21C7, CISS_BOARD_SA5,   "HP Smart Array H240" },
    { 0x103C, 0x21C8, CISS_BOARD_SA5,   "HP Smart Array H241" },
    { 0x103C, 0x21CA, CISS_BOARD_SA5,   "HP Smart Array P246br" },
    { 0x103C, 0x21CB, CISS_BOARD_SA5,   "HP Smart Array P840" },
    { 0x103C, 0x21CC, CISS_BOARD_SA5,   "HP Smart Array P542d" },
    { 0x103C, 0x21CD, CISS_BOARD_SA5,   "HP Smart Array P240nr" },
    { 0x103C, 0x21CE, CISS_BOARD_SA5,   "HP Smart Array H240nr" },
    { 0, 0, 0, NULL }
};

static devclass_t	ciss_devclass;
DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
MODULE_PNP_INFO("U16:vendor;U16:device;", pci, ciss, ciss_vendor_data,
    nitems(ciss_vendor_data) - 1);
MODULE_DEPEND(ciss, cam, 1, 1, 1);
MODULE_DEPEND(ciss, pci, 1, 1, 1);

/************************************************************************
 * Find a match for the device in our list of known adapters.
 */
static int
ciss_lookup(device_t dev)
{
    int 	i;

    for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
	    return(i);
	}
    return(-1);
}

/************************************************************************
 * Match a known CISS adapter.
 */
static int
ciss_probe(device_t dev)
{
    int		i;

    i = ciss_lookup(dev);
    if (i != -1) {
	device_set_desc(dev, ciss_vendor_data[i].desc);
	return(BUS_PROBE_DEFAULT);
    }
    return(ENOENT);
}

/************************************************************************
 * Attach the driver to this adapter.
 */
static int
ciss_attach(device_t dev)
{
    struct ciss_softc	*sc;
    int			error;

    debug_called(1);

#ifdef CISS_DEBUG
    /* print structure/union sizes */
    debug_struct(ciss_command);
    debug_struct(ciss_header);
    debug_union(ciss_device_address);
    debug_struct(ciss_cdb);
    debug_struct(ciss_report_cdb);
    debug_struct(ciss_notify_cdb);
    debug_struct(ciss_notify);
    debug_struct(ciss_message_cdb);
    debug_struct(ciss_error_info_pointer);
    debug_struct(ciss_error_info);
    debug_struct(ciss_sg_entry);
    debug_struct(ciss_config_table);
    debug_struct(ciss_bmic_cdb);
    debug_struct(ciss_bmic_id_ldrive);
    debug_struct(ciss_bmic_id_lstatus);
    debug_struct(ciss_bmic_id_table);
    debug_struct(ciss_bmic_id_pdrive);
    debug_struct(ciss_bmic_blink_pdrive);
    debug_struct(ciss_bmic_flush_cache);
    debug_const(CISS_MAX_REQUESTS);
    debug_const(CISS_MAX_LOGICAL);
    debug_const(CISS_INTERRUPT_COALESCE_DELAY);
    debug_const(CISS_INTERRUPT_COALESCE_COUNT);
    debug_const(CISS_COMMAND_ALLOC_SIZE);
    debug_const(CISS_COMMAND_SG_LENGTH);

    debug_type(cciss_pci_info_struct);
    debug_type(cciss_coalint_struct);
    debug_type(cciss_coalint_struct);
    debug_type(NodeName_type);
    debug_type(NodeName_type);
    debug_type(Heartbeat_type);
    debug_type(BusTypes_type);
    debug_type(FirmwareVer_type);
    debug_type(DriverVer_type);
    debug_type(IOCTL_Command_struct);
#endif

    sc = device_get_softc(dev);
    sc->ciss_dev = dev;
    mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
    callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);

    /*
     * Do PCI-specific init.
     */
    if ((error = ciss_init_pci(sc)) != 0)
	goto out;

    /*
     * Initialise driver queues.
     */
    ciss_initq_free(sc);
    ciss_initq_notify(sc);

    /*
     * Initialize device sysctls.
     */
    ciss_init_sysctl(sc);

    /*
     * Initialise command/request pool.
     */
    if ((error = ciss_init_requests(sc)) != 0)
	goto out;

    /*
     * Get adapter information.
     */
    if ((error = ciss_identify_adapter(sc)) != 0)
	goto out;

    /*
     * Find all the physical devices.
     */
    if ((error = ciss_init_physical(sc)) != 0)
	goto out;

    /*
     * Build our private table of logical devices.
     */
    if ((error = ciss_init_logical(sc)) != 0)
	goto out;

    /*
     * Enable interrupts so that the CAM scan can complete.
     */
    CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);

    /*
     * Initialise the CAM interface.
     */
    if ((error = ciss_cam_init(sc)) != 0)
	goto out;

    /*
     * Start the heartbeat routine and event chain.
     */
    ciss_periodic(sc);

   /*
     * Create the control device.
     */
    sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
			      "ciss%d", device_get_unit(sc->ciss_dev));
    sc->ciss_dev_t->si_drv1 = sc;

    /*
     * The adapter is running; synchronous commands can now sleep
     * waiting for an interrupt to signal completion.
     */
    sc->ciss_flags |= CISS_FLAG_RUNNING;

    ciss_spawn_notify_thread(sc);

    error = 0;
 out:
    if (error != 0) {
	/* ciss_free() expects the mutex to be held */
	mtx_lock(&sc->ciss_mtx);
	ciss_free(sc);
    }
    return(error);
}

/************************************************************************
 * Detach the driver from this adapter.
 */
static int
ciss_detach(device_t dev)
{
    struct ciss_softc	*sc = device_get_softc(dev);

    debug_called(1);

    mtx_lock(&sc->ciss_mtx);
    if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
	mtx_unlock(&sc->ciss_mtx);
	return (EBUSY);
    }

    /* flush adapter cache */
    ciss_flush_adapter(sc);

    /* release all resources.  The mutex is released and freed here too. */
    ciss_free(sc);

    return(0);
}

/************************************************************************
 * Prepare adapter for system shutdown.
 */
static int
ciss_shutdown(device_t dev)
{
    struct ciss_softc	*sc = device_get_softc(dev);

    debug_called(1);

    mtx_lock(&sc->ciss_mtx);
    /* flush adapter cache */
    ciss_flush_adapter(sc);

    if (sc->ciss_soft_reset)
	ciss_soft_reset(sc);
    mtx_unlock(&sc->ciss_mtx);

    return(0);
}

static void
ciss_init_sysctl(struct ciss_softc *sc)
{

    SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
}

/************************************************************************
 * Perform PCI-specific attachment actions.
 */
static int
ciss_init_pci(struct ciss_softc *sc)
{
    uintptr_t		cbase, csize, cofs;
    uint32_t		method, supported_methods;
    int			error, sqmask, i;
    void		*intr;

    debug_called(1);

    /*
     * Work out adapter type.
     */
    i = ciss_lookup(sc->ciss_dev);
    if (i < 0) {
	ciss_printf(sc, "unknown adapter type\n");
	return (ENXIO);
    }

    if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
    } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
	sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
    } else {
	/*
	 * XXX Big hammer, masks/unmasks all possible interrupts.  This should
	 * work on all hardware variants.  Need to add code to handle the
	 * "controller crashed" interrupt bit that this unmasks.
	 */
	sqmask = ~0;
    }

    /*
     * Allocate register window first (we need this to find the config
     * struct).
     */
    error = ENXIO;
    sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
    if ((sc->ciss_regs_resource =
	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
	ciss_printf(sc, "can't allocate register window\n");
	return(ENXIO);
    }
    sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
    sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);

    /*
     * Find the BAR holding the config structure.  If it's not the one
     * we already mapped for registers, map it too.
     */
    sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
    if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
	if ((sc->ciss_cfg_resource =
	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
	    ciss_printf(sc, "can't allocate config window\n");
	    return(ENXIO);
	}
	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
	csize = rman_get_end(sc->ciss_cfg_resource) -
	    rman_get_start(sc->ciss_cfg_resource) + 1;
    } else {
	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
	csize = rman_get_end(sc->ciss_regs_resource) -
	    rman_get_start(sc->ciss_regs_resource) + 1;
    }
    cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);

    /*
     * Use the base/size/offset values we just calculated to
     * sanity-check the config structure.  If it's OK, point to it.
     */
    if ((cofs + sizeof(struct ciss_config_table)) > csize) {
	ciss_printf(sc, "config table outside window\n");
	return(ENXIO);
    }
    sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
    debug(1, "config struct at %p", sc->ciss_cfg);

    /*
     * Calculate the number of request structures/commands we are
     * going to provide for this adapter.
     */
    sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);

    /*
     * Validate the config structure.  If we supported other transport
     * methods, we could select amongst them at this point in time.
     */
    if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
	return(ENXIO);
    }

    /*
     * Select the mode of operation, prefer Performant.
     */
    if (!(sc->ciss_cfg->supported_methods &
	(CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
	ciss_printf(sc, "No supported transport layers: 0x%x\n",
	    sc->ciss_cfg->supported_methods);
    }

    switch (ciss_force_transport) {
    case 1:
	supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
	break;
    case 2:
	supported_methods = CISS_TRANSPORT_METHOD_PERF;
	break;
    default:
        /*
         * Override the capabilities of the BOARD and specify SIMPLE
         * MODE 
         */
        if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
                supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
        else
                supported_methods = sc->ciss_cfg->supported_methods;
        break;
    }

setup:
    if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
	method = CISS_TRANSPORT_METHOD_PERF;
	sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
	    sc->ciss_cfg->transport_offset);
	if (ciss_init_perf(sc)) {
	    supported_methods &= ~method;
	    goto setup;
	}
    } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
	method = CISS_TRANSPORT_METHOD_SIMPLE;
    } else {
	ciss_printf(sc, "No supported transport methods: 0x%x\n",
	    sc->ciss_cfg->supported_methods);
	return(ENXIO);
    }

    /*
     * Tell it we're using the low 4GB of RAM.  Set the default interrupt
     * coalescing options.
     */
    sc->ciss_cfg->requested_method = method;
    sc->ciss_cfg->command_physlimit = 0;
    sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
    sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;

#ifdef __i386__
    sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
#endif

    if (ciss_update_config(sc)) {
	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
	return(ENXIO);
    }
    if ((sc->ciss_cfg->active_method & method) == 0) {
	supported_methods &= ~method;
	if (supported_methods == 0) {
	    ciss_printf(sc, "adapter refuses to go into available transports "
		"mode (0x%x, 0x%x)\n", supported_methods,
		sc->ciss_cfg->active_method);
	    return(ENXIO);
	} else 
	    goto setup;
    }

    /*
     * Wait for the adapter to come ready.
     */
    if ((error = ciss_wait_adapter(sc)) != 0)
	return(error);

    /* Prepare to possibly use MSIX and/or PERFORMANT interrupts.  Normal
     * interrupts have a rid of 0, this will be overridden if MSIX is used.
     */
    sc->ciss_irq_rid[0] = 0;
    if (method == CISS_TRANSPORT_METHOD_PERF) {
	ciss_printf(sc, "PERFORMANT Transport\n");
	if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
	    intr = ciss_perf_msi_intr;
	} else {
	    intr = ciss_perf_intr;
	}
	/* XXX The docs say that the 0x01 bit is only for SAS controllers.
	 * Unfortunately, there is no good way to know if this is a SAS
	 * controller.  Hopefully enabling this bit universally will work OK.
	 * It seems to work fine for SA6i controllers.
	 */
	sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;

    } else {
	ciss_printf(sc, "SIMPLE Transport\n");
	/* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
	if (ciss_force_interrupt == 2)
	    /* If this fails, we automatically revert to INTx */
	    ciss_setup_msix(sc);
	sc->ciss_perf = NULL;
	intr = ciss_intr;
	sc->ciss_interrupt_mask = sqmask;
    }

    /*
     * Turn off interrupts before we go routing anything.
     */
    CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);

    /*
     * Allocate and set up our interrupt.
     */
    if ((sc->ciss_irq_resource =
	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
	ciss_printf(sc, "can't allocate interrupt\n");
	return(ENXIO);
    }

    if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
		       &sc->ciss_intr)) {
	ciss_printf(sc, "can't set up interrupt\n");
	return(ENXIO);
    }

    /*
     * Allocate the parent bus DMA tag appropriate for our PCI
     * interface.
     *
     * Note that "simple" adapters can only address within a 32-bit
     * span.
     */
    if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
			   1, 0, 			/* alignment, boundary */
			   BUS_SPACE_MAXADDR,		/* lowaddr */
			   BUS_SPACE_MAXADDR, 		/* highaddr */
			   NULL, NULL, 			/* filter, filterarg */
			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
			   BUS_SPACE_UNRESTRICTED,	/* nsegments */
			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
			   0,				/* flags */
			   NULL, NULL,			/* lockfunc, lockarg */
			   &sc->ciss_parent_dmat)) {
	ciss_printf(sc, "can't allocate parent DMA tag\n");
	return(ENOMEM);
    }

    /*
     * Create DMA tag for mapping buffers into adapter-addressable
     * space.
     */
    if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
			   1, 0, 			/* alignment, boundary */
			   BUS_SPACE_MAXADDR,		/* lowaddr */
			   BUS_SPACE_MAXADDR, 		/* highaddr */
			   NULL, NULL, 			/* filter, filterarg */
			   (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE, /* maxsize */
			   CISS_MAX_SG_ELEMENTS,	/* nsegments */
			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
			   BUS_DMA_ALLOCNOW,		/* flags */
			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
			   &sc->ciss_buffer_dmat)) {
	ciss_printf(sc, "can't allocate buffer DMA tag\n");
	return(ENOMEM);
    }
    return(0);
}

/************************************************************************
 * Setup MSI/MSIX operation (Performant only)
 * Four interrupts are available, but we only use 1 right now.  If MSI-X
 * isn't avaialble, try using MSI instead.
 */
static int
ciss_setup_msix(struct ciss_softc *sc)
{
    int val, i;

    /* Weed out devices that don't actually support MSI */
    i = ciss_lookup(sc->ciss_dev);
    if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
	return (EINVAL);

    /*
     * Only need to use the minimum number of MSI vectors, as the driver
     * doesn't support directed MSIX interrupts.
     */
    val = pci_msix_count(sc->ciss_dev);
    if (val < CISS_MSI_COUNT) {
	val = pci_msi_count(sc->ciss_dev);
	device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
	if (val < CISS_MSI_COUNT)
	    return (EINVAL);
    }
    val = MIN(val, CISS_MSI_COUNT);
    if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
	if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
	    return (EINVAL);
    }

    sc->ciss_msi = val;
    if (bootverbose)
	ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
	    (val != 1) ? "s" : "");

    for (i = 0; i < val; i++)
	sc->ciss_irq_rid[i] = i + 1;

    return (0);

}

/************************************************************************
 * Setup the Performant structures.
 */
static int
ciss_init_perf(struct ciss_softc *sc)
{
    struct ciss_perf_config *pc = sc->ciss_perf;
    int reply_size;

    /*
     * Create the DMA tag for the reply queue.
     */
    reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
    if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
			   1, 0, 			/* alignment, boundary */
			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
			   BUS_SPACE_MAXADDR, 		/* highaddr */
			   NULL, NULL, 			/* filter, filterarg */
			   reply_size, 1,		/* maxsize, nsegments */
			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
			   0,				/* flags */
			   NULL, NULL,			/* lockfunc, lockarg */
			   &sc->ciss_reply_dmat)) {
	ciss_printf(sc, "can't allocate reply DMA tag\n");
	return(ENOMEM);
    }
    /*
     * Allocate memory and make it available for DMA.
     */
    if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
			 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
	ciss_printf(sc, "can't allocate reply memory\n");
	return(ENOMEM);
    }
    bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
		    reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
    bzero(sc->ciss_reply, reply_size);

    sc->ciss_cycle = 0x1;
    sc->ciss_rqidx = 0;

    /*
     * Preload the fetch table with common command sizes.  This allows the
     * hardware to not waste bus cycles for typical i/o commands, but also not
     * tax the driver to be too exact in choosing sizes.  The table is optimized
     * for page-aligned i/o's, but since most i/o comes from the various pagers,
     * it's a reasonable assumption to make.
     */
    pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_1] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_2] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_4] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_8] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_16] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_32] =
	(sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
    pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;

    pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
    pc->rq_count = 1;	/* XXX Hardcode for a single queue */
    pc->rq_bank_hi = 0;
    pc->rq_bank_lo = 0;
    pc->rq[0].rq_addr_hi = 0x0;
    pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;

    return(0);
}

/************************************************************************
 * Wait for the adapter to come ready.
 */
static int
ciss_wait_adapter(struct ciss_softc *sc)
{
    int		i;

    debug_called(1);

    /*
     * Wait for the adapter to come ready.
     */
    if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
	ciss_printf(sc, "waiting for adapter to come ready...\n");
	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
	    DELAY(1000000);	/* one second */
	    if (i > 30) {
		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
		return(EIO);
	    }
	}
    }
    return(0);
}

/************************************************************************
 * Flush the adapter cache.
 */
static int
ciss_flush_adapter(struct ciss_softc *sc)
{
    struct ciss_request			*cr;
    struct ciss_bmic_flush_cache	*cbfc;
    int					error, command_status;

    debug_called(1);

    cr = NULL;
    cbfc = NULL;

    /*
     * Build a BMIC request to flush the cache.  We don't disable
     * it, as we may be going to do more I/O (eg. we are emulating
     * the Synchronise Cache command).
     */
    if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
	error = ENOMEM;
	goto out;
    }
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
				       (void **)&cbfc, sizeof(*cbfc))) != 0)
	goto out;

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:
	break;
    default:
	ciss_printf(sc, "error flushing cache (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }

out:
    if (cbfc != NULL)
	free(cbfc, CISS_MALLOC_CLASS);
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

static void
ciss_soft_reset(struct ciss_softc *sc)
{
    struct ciss_request		*cr = NULL;
    struct ciss_command		*cc;
    int				i, error = 0;

    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	/* only reset proxy controllers */
	if (sc->ciss_controllers[i].physical.bus == 0)
	    continue;

	if ((error = ciss_get_request(sc, &cr)) != 0)
	    break;

	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
					   NULL, 0)) != 0)
	    break;

	cc = cr->cr_cc;
	cc->header.address = sc->ciss_controllers[i];

	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
	    break;

	ciss_release_request(cr);
    }

    if (error)
	ciss_printf(sc, "error resetting controller (%d)\n", error);

    if (cr != NULL)
	ciss_release_request(cr);
}

/************************************************************************
 * Allocate memory for the adapter command structures, initialise
 * the request structures.
 *
 * Note that the entire set of commands are allocated in a single
 * contiguous slab.
 */
static int
ciss_init_requests(struct ciss_softc *sc)
{
    struct ciss_request	*cr;
    int			i;

    debug_called(1);

    if (bootverbose)
	ciss_printf(sc, "using %d of %d available commands\n",
		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);

    /*
     * Create the DMA tag for commands.
     */
    if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
			   32, 0, 			/* alignment, boundary */
			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
			   BUS_SPACE_MAXADDR, 		/* highaddr */
			   NULL, NULL, 			/* filter, filterarg */
			   CISS_COMMAND_ALLOC_SIZE *
			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
			   0,				/* flags */
			   NULL, NULL,			/* lockfunc, lockarg */
			   &sc->ciss_command_dmat)) {
	ciss_printf(sc, "can't allocate command DMA tag\n");
	return(ENOMEM);
    }
    /*
     * Allocate memory and make it available for DMA.
     */
    if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
	ciss_printf(sc, "can't allocate command memory\n");
	return(ENOMEM);
    }
    bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
		    ciss_command_map_helper, &sc->ciss_command_phys, 0);
    bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);

    /*
     * Set up the request and command structures, push requests onto
     * the free queue.
     */
    for (i = 1; i < sc->ciss_max_requests; i++) {
	cr = &sc->ciss_request[i];
	cr->cr_sc = sc;
	cr->cr_tag = i;
	cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
	    CISS_COMMAND_ALLOC_SIZE * i);
	cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
	ciss_enqueue_free(cr);
    }
    return(0);
}

static void
ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
    uint32_t *addr;

    addr = arg;
    *addr = segs[0].ds_addr;
}

/************************************************************************
 * Identify the adapter, print some information about it.
 */
static int
ciss_identify_adapter(struct ciss_softc *sc)
{
    struct ciss_request	*cr;
    int			error, command_status;

    debug_called(1);

    cr = NULL;

    /*
     * Get a request, allocate storage for the adapter data.
     */
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
				       (void **)&sc->ciss_id,
				       sizeof(*sc->ciss_id))) != 0)
	goto out;

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
	break;
    case CISS_CMD_STATUS_DATA_UNDERRUN:
    case CISS_CMD_STATUS_DATA_OVERRUN:
	ciss_printf(sc, "data over/underrun reading adapter information\n");
    default:
	ciss_printf(sc, "error reading adapter information (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }

    /* sanity-check reply */
    if (!(sc->ciss_id->controller_flags & CONTROLLER_FLAGS_BIG_MAP_SUPPORT)) {
	ciss_printf(sc, "adapter does not support BIG_MAP\n");
	error = ENXIO;
	goto out;
    }

#if 0
    /* XXX later revisions may not need this */
    sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
#endif

    /* XXX only really required for old 5300 adapters? */
    sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;

    /*
     * Earlier controller specs do not contain these config
     * entries, so assume that a 0 means its old and assign
     * these values to the defaults that were established 
     * when this driver was developed for them
     */
    if (sc->ciss_cfg->max_logical_supported == 0) 
        sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
    if (sc->ciss_cfg->max_physical_supported == 0) 
	sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
    /* print information */
    if (bootverbose) {
	ciss_printf(sc, "  %d logical drive%s configured\n",
		    sc->ciss_id->configured_logical_drives,
		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_chip_count);

	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
	ciss_printf(sc, "  supported I/O methods 0x%b\n",
		    sc->ciss_cfg->supported_methods,
		    "\20\1READY\2simple\3performant\4MEMQ\n");
	ciss_printf(sc, "  active I/O method 0x%b\n",
		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
	ciss_printf(sc, "  4G page base 0x%08x\n",
		    sc->ciss_cfg->command_physlimit);
	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
		    sc->ciss_cfg->interrupt_coalesce_delay);
	ciss_printf(sc, "  interrupt coalesce count %d\n",
		    sc->ciss_cfg->interrupt_coalesce_count);
	ciss_printf(sc, "  max outstanding commands %d\n",
		    sc->ciss_cfg->max_outstanding_commands);
	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
    	ciss_printf(sc, "  max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
    	ciss_printf(sc, "  max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
    	ciss_printf(sc, "  max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
	ciss_printf(sc, "  JBOD Support is %s\n", (sc->ciss_id->uiYetMoreControllerFlags & YMORE_CONTROLLER_FLAGS_JBOD_SUPPORTED) ?
			"Available" : "Unavailable");
	ciss_printf(sc, "  JBOD Mode is %s\n", (sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED) ?
			"Enabled" : "Disabled");
    }

out:
    if (error) {
	if (sc->ciss_id != NULL) {
	    free(sc->ciss_id, CISS_MALLOC_CLASS);
	    sc->ciss_id = NULL;
	}
    }
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

/************************************************************************
 * Helper routine for generating a list of logical and physical luns.
 */
static struct ciss_lun_report *
ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_report_cdb	*crc;
    struct ciss_lun_report	*cll;
    int				command_status;
    int				report_size;
    int				error = 0;

    debug_called(1);

    cr = NULL;
    cll = NULL;

    /*
     * Get a request, allocate storage for the address list.
     */
    if ((error = ciss_get_request(sc, &cr)) != 0)
	goto out;
    report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
    if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
	ciss_printf(sc, "can't allocate memory for lun report\n");
	error = ENOMEM;
	goto out;
    }

    /*
     * Build the Report Logical/Physical LUNs command.
     */
    cc = cr->cr_cc;
    cr->cr_data = cll;
    cr->cr_length = report_size;
    cr->cr_flags = CISS_REQ_DATAIN;

    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
    cc->header.address.physical.bus = 0;
    cc->header.address.physical.target = 0;
    cc->cdb.cdb_length = sizeof(*crc);
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
    cc->cdb.timeout = 30;	/* XXX better suggestions? */

    crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
    bzero(crc, sizeof(*crc));
    crc->opcode = opcode;
    crc->length = htonl(report_size);			/* big-endian field */
    cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */

    /*
     * Submit the request and wait for it to complete.  (timeout
     * here should be much greater than above)
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
	goto out;
    }

    /*
     * Check response.  Note that data over/underrun is OK.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
    case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
	break;
    case CISS_CMD_STATUS_DATA_OVERRUN:
	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
		    sc->ciss_cfg->max_logical_supported);
	break;
    default:
	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }
    ciss_release_request(cr);
    cr = NULL;

out:
    if (cr != NULL)
	ciss_release_request(cr);
    if (error && cll != NULL) {
	free(cll, CISS_MALLOC_CLASS);
	cll = NULL;
    }
    return(cll);
}

/************************************************************************
 * Find logical drives on the adapter.
 */
static int
ciss_init_logical(struct ciss_softc *sc)
{
    struct ciss_lun_report	*cll;
    int				error = 0, i, j;
    int				ndrives;

    debug_called(1);

    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
			   sc->ciss_cfg->max_logical_supported);
    if (cll == NULL) {
	error = ENXIO;
	goto out;
    }

    /* sanity-check reply */
    ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
    if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
	    	ndrives, sc->ciss_cfg->max_logical_supported);
	error = ENXIO;
	goto out;
    }

    /*
     * Save logical drive information.
     */
    if (bootverbose) {
	ciss_printf(sc, "%d logical drive%s\n",
	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
    }

    sc->ciss_logical =
	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
    if (sc->ciss_logical == NULL) {
	error = ENXIO;
	goto out;
    }

    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	sc->ciss_logical[i] =
	    malloc(sc->ciss_cfg->max_logical_supported *
		   sizeof(struct ciss_ldrive),
		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
	if (sc->ciss_logical[i] == NULL) {
	    error = ENXIO;
	    goto out;
	}

	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
    }


    for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
	if (i < ndrives) {
	    struct ciss_ldrive	*ld;
	    int			bus, target;

	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
	    ld		= &sc->ciss_logical[bus][target];

	    ld->cl_address	= cll->lun[i];
	    ld->cl_controller	= &sc->ciss_controllers[bus];
	    if (ciss_identify_logical(sc, ld) != 0)
		continue;
	    /*
	     * If the drive has had media exchanged, we should bring it online.
	     */
	    if (ld->cl_lstatus->media_exchanged)
		ciss_accept_media(sc, ld);

	}
    }

 out:
    if (cll != NULL)
	free(cll, CISS_MALLOC_CLASS);
    return(error);
}

static int
ciss_init_physical(struct ciss_softc *sc)
{
    struct ciss_lun_report	*cll;
    int				error = 0, i;
    int				nphys;
    int				bus, target;

    debug_called(1);

    bus = 0;
    target = 0;

    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
			   sc->ciss_cfg->max_physical_supported);
    if (cll == NULL) {
	error = ENXIO;
	goto out;
    }

    nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));

    if (bootverbose) {
	ciss_printf(sc, "%d physical device%s\n",
	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
    }

    /*
     * Figure out the bus mapping.
     * Logical buses include both the local logical bus for local arrays and
     * proxy buses for remote arrays.  Physical buses are numbered by the
     * controller and represent physical buses that hold physical devices.
     * We shift these bus numbers so that everything fits into a single flat
     * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
     * numbers, and the physical bus numbers are shifted to be above that.
     * This results in the various driver arrays being indexed as follows:
     *
     * ciss_controllers[] - indexed by logical bus
     * ciss_cam_sim[]     - indexed by both logical and physical, with physical
     *                      being shifted by 32.
     * ciss_logical[][]   - indexed by logical bus
     * ciss_physical[][]  - indexed by physical bus
     *
     * XXX This is getting more and more hackish.  CISS really doesn't play
     *     well with a standard SCSI model; devices are addressed via magic
     *     cookies, not via b/t/l addresses.  Since there is no way to store
     *     the cookie in the CAM device object, we have to keep these lookup
     *     tables handy so that the devices can be found quickly at the cost
     *     of wasting memory and having a convoluted lookup scheme.  This
     *     driver should probably be converted to block interface.
     */
    /*
     * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
     * controller. A proxy controller is another physical controller
     * behind the primary PCI controller. We need to know about this
     * so that BMIC commands can be properly targeted.  There can be
     * proxy controllers attached to a single PCI controller, so
     * find the highest numbered one so the array can be properly
     * sized.
     */
    sc->ciss_max_logical_bus = 1;
    for (i = 0; i < nphys; i++) {
	if (cll->lun[i].physical.extra_address == 0) {
	    bus = cll->lun[i].physical.bus;
	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
	} else {
	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
	}
    }

    sc->ciss_controllers =
	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);

    if (sc->ciss_controllers == NULL) {
	ciss_printf(sc, "Could not allocate memory for controller map\n");
	error = ENOMEM;
	goto out;
    }

    /* setup a map of controller addresses */
    for (i = 0; i < nphys; i++) {
	if (cll->lun[i].physical.extra_address == 0) {
	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
	}
    }

    sc->ciss_physical =
	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
    if (sc->ciss_physical == NULL) {
	ciss_printf(sc, "Could not allocate memory for physical device map\n");
	error = ENOMEM;
	goto out;
    }

    for (i = 0; i < sc->ciss_max_physical_bus; i++) {
	sc->ciss_physical[i] =
	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
	if (sc->ciss_physical[i] == NULL) {
	    ciss_printf(sc, "Could not allocate memory for target map\n");
	    error = ENOMEM;
	    goto out;
	}
    }

    ciss_filter_physical(sc, cll);

out:
    if (cll != NULL)
	free(cll, CISS_MALLOC_CLASS);

    return(error);
}

static int
ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
{
    u_int32_t ea;
    int i, nphys;
    int	bus, target;

    nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
    for (i = 0; i < nphys; i++) {
	if (cll->lun[i].physical.extra_address == 0)
	    continue;

	/*
	 * Filter out devices that we don't want.  Level 3 LUNs could
	 * probably be supported, but the docs don't give enough of a
	 * hint to know how.
	 *
	 * The mode field of the physical address is likely set to have
	 * hard disks masked out.  Honor it unless the user has overridden
	 * us with the tunable.  We also munge the inquiry data for these
	 * disks so that they only show up as passthrough devices.  Keeping
	 * them visible in this fashion is useful for doing things like
	 * flashing firmware.
	 */
	ea = cll->lun[i].physical.extra_address;
	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
	    (CISS_EXTRA_MODE2(ea) == 0x3))
	    continue;
	if ((ciss_expose_hidden_physical == 0) &&
	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
	    continue;

	/*
	 * Note: CISS firmware numbers physical busses starting at '1', not
	 *       '0'.  This numbering is internal to the firmware and is only
	 *       used as a hint here.
	 */
	bus = CISS_EXTRA_BUS2(ea) - 1;
	target = CISS_EXTRA_TARGET2(ea);
	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
	sc->ciss_physical[bus][target].cp_online = 1;
    }

    return (0);
}

static int
ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
{
    struct ciss_request			*cr;
    struct ciss_command			*cc;
    struct scsi_inquiry			*inq;
    int					error;
    int					command_status;

    cr = NULL;

    bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));

    if ((error = ciss_get_request(sc, &cr)) != 0)
	goto out;

    cc = cr->cr_cc;
    cr->cr_data = &ld->cl_geometry;
    cr->cr_length = sizeof(ld->cl_geometry);
    cr->cr_flags = CISS_REQ_DATAIN;

    cc->header.address = ld->cl_address;
    cc->cdb.cdb_length = 6;
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
    cc->cdb.timeout = 30;

    inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
    inq->opcode = INQUIRY;
    inq->byte2 = SI_EVPD;
    inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
    scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);

    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error getting geometry (%d)\n", error);
	goto out;
    }

    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:
    case CISS_CMD_STATUS_DATA_UNDERRUN:
	break;
    case CISS_CMD_STATUS_DATA_OVERRUN:
	ciss_printf(sc, "WARNING: Data overrun\n");
	break;
    default:
	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
		    ciss_name_command_status(command_status));
	break;
    }

out:
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}
/************************************************************************
 * Identify a logical drive, initialise state related to it.
 */
static int
ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_bmic_cdb	*cbc;
    int				error, command_status;

    debug_called(1);

    cr = NULL;

    /*
     * Build a BMIC request to fetch the drive ID.
     */
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
				       (void **)&ld->cl_ldrive,
				       sizeof(*ld->cl_ldrive))) != 0)
	goto out;
    cc = cr->cr_cc;
    cc->header.address = *ld->cl_controller;	/* target controller */
    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
    cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
	break;
    case CISS_CMD_STATUS_DATA_UNDERRUN:
    case CISS_CMD_STATUS_DATA_OVERRUN:
	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
    default:
	ciss_printf(sc, "error reading logical drive ID (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }
    ciss_release_request(cr);
    cr = NULL;

    /*
     * Build a CISS BMIC command to get the logical drive status.
     */
    if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
	goto out;

    /*
     * Get the logical drive geometry.
     */
    if ((error = ciss_inquiry_logical(sc, ld)) != 0)
	goto out;

    /*
     * Print the drive's basic characteristics.
     */
    if (bootverbose) {
	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
		     ld->cl_ldrive->block_size));

	ciss_print_ldrive(sc, ld);
    }
out:
    if (error != 0) {
	/* make the drive not-exist */
	ld->cl_status = CISS_LD_NONEXISTENT;
	if (ld->cl_ldrive != NULL) {
	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
	    ld->cl_ldrive = NULL;
	}
	if (ld->cl_lstatus != NULL) {
	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
	    ld->cl_lstatus = NULL;
	}
    }
    if (cr != NULL)
	ciss_release_request(cr);

    return(error);
}

/************************************************************************
 * Get status for a logical drive.
 *
 * XXX should we also do this in response to Test Unit Ready?
 */
static int
ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_bmic_cdb	*cbc;
    int				error, command_status;

    /*
     * Build a CISS BMIC command to get the logical drive status.
     */
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
				       (void **)&ld->cl_lstatus,
				       sizeof(*ld->cl_lstatus))) != 0)
	goto out;
    cc = cr->cr_cc;
    cc->header.address = *ld->cl_controller;	/* target controller */
    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
    cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
	break;
    case CISS_CMD_STATUS_DATA_UNDERRUN:
    case CISS_CMD_STATUS_DATA_OVERRUN:
	ciss_printf(sc, "data over/underrun reading logical drive status\n");
    default:
	ciss_printf(sc, "error reading logical drive status (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }

    /*
     * Set the drive's summary status based on the returned status.
     *
     * XXX testing shows that a failed JBOD drive comes back at next
     * boot in "queued for expansion" mode.  WTF?
     */
    ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);

out:
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

/************************************************************************
 * Notify the adapter of a config update.
 */
static int
ciss_update_config(struct ciss_softc *sc)
{
    int		i;

    debug_called(1);

    CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
    for (i = 0; i < 1000; i++) {
	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
	    return(0);
	}
	DELAY(1000);
    }
    return(1);
}

/************************************************************************
 * Accept new media into a logical drive.
 *
 * XXX The drive has previously been offline; it would be good if we
 *     could make sure it's not open right now.
 */
static int
ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_bmic_cdb	*cbc;
    int				command_status;
    int				error = 0, ldrive;

    ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);

    debug(0, "bringing logical drive %d back online", ldrive);

    /*
     * Build a CISS BMIC command to bring the drive back online.
     */
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
				       NULL, 0)) != 0)
	goto out;
    cc = cr->cr_cc;
    cc->header.address = *ld->cl_controller;	/* target controller */
    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
    cbc->log_drive = ldrive;

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:		/* all OK */
	/* we should get a logical drive status changed event here */
	break;
    default:
	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
		    ciss_name_command_status(command_status));
	break;
    }

out:
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

/************************************************************************
 * Release adapter resources.
 */
static void
ciss_free(struct ciss_softc *sc)
{
    struct ciss_request *cr;
    int			i, j;

    debug_called(1);

    /* we're going away */
    sc->ciss_flags |= CISS_FLAG_ABORTING;

    /* terminate the periodic heartbeat routine */
    callout_stop(&sc->ciss_periodic);

    /* cancel the Event Notify chain */
    ciss_notify_abort(sc);

    ciss_kill_notify_thread(sc);

    /* disconnect from CAM */
    if (sc->ciss_cam_sim) {
	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	    if (sc->ciss_cam_sim[i]) {
		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
		cam_sim_free(sc->ciss_cam_sim[i], 0);
	    }
	}
	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
	     CISS_PHYSICAL_BASE; i++) {
	    if (sc->ciss_cam_sim[i]) {
		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
		cam_sim_free(sc->ciss_cam_sim[i], 0);
	    }
	}
	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
    }
    if (sc->ciss_cam_devq)
	cam_simq_free(sc->ciss_cam_devq);

    /* remove the control device */
    mtx_unlock(&sc->ciss_mtx);
    if (sc->ciss_dev_t != NULL)
	destroy_dev(sc->ciss_dev_t);

    /* Final cleanup of the callout. */
    callout_drain(&sc->ciss_periodic);
    mtx_destroy(&sc->ciss_mtx);

    /* free the controller data */
    if (sc->ciss_id != NULL)
	free(sc->ciss_id, CISS_MALLOC_CLASS);

    /* release I/O resources */
    if (sc->ciss_regs_resource != NULL)
	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
			     sc->ciss_regs_rid, sc->ciss_regs_resource);
    if (sc->ciss_cfg_resource != NULL)
	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
    if (sc->ciss_intr != NULL)
	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
    if (sc->ciss_irq_resource != NULL)
	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
			     sc->ciss_irq_rid[0], sc->ciss_irq_resource);
    if (sc->ciss_msi)
	pci_release_msi(sc->ciss_dev);

    while ((cr = ciss_dequeue_free(sc)) != NULL)
	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
    if (sc->ciss_buffer_dmat)
	bus_dma_tag_destroy(sc->ciss_buffer_dmat);

    /* destroy command memory and DMA tag */
    if (sc->ciss_command != NULL) {
	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
    }
    if (sc->ciss_command_dmat)
	bus_dma_tag_destroy(sc->ciss_command_dmat);

    if (sc->ciss_reply) {
	bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
	bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
    }
    if (sc->ciss_reply_dmat)
	bus_dma_tag_destroy(sc->ciss_reply_dmat);

    /* destroy DMA tags */
    if (sc->ciss_parent_dmat)
	bus_dma_tag_destroy(sc->ciss_parent_dmat);
    if (sc->ciss_logical) {
	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	    for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
		if (sc->ciss_logical[i][j].cl_ldrive)
		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
		if (sc->ciss_logical[i][j].cl_lstatus)
		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
	    }
	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
	}
	free(sc->ciss_logical, CISS_MALLOC_CLASS);
    }

    if (sc->ciss_physical) {
	for (i = 0; i < sc->ciss_max_physical_bus; i++)
	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
	free(sc->ciss_physical, CISS_MALLOC_CLASS);
    }

    if (sc->ciss_controllers)
	free(sc->ciss_controllers, CISS_MALLOC_CLASS);

}

/************************************************************************
 * Give a command to the adapter.
 *
 * Note that this uses the simple transport layer directly.  If we
 * want to add support for other layers, we'll need a switch of some
 * sort.
 *
 * Note that the simple transport layer has no way of refusing a
 * command; we only have as many request structures as the adapter
 * supports commands, so we don't have to check (this presumes that
 * the adapter can handle commands as fast as we throw them at it).
 */
static int
ciss_start(struct ciss_request *cr)
{
    struct ciss_command	*cc;	/* XXX debugging only */
    int			error;

    cc = cr->cr_cc;
    debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);

    /*
     * Map the request's data.
     */
    if ((error = ciss_map_request(cr)))
	return(error);

#if 0
    ciss_print_request(cr);
#endif

    return(0);
}

/************************************************************************
 * Fetch completed request(s) from the adapter, queue them for
 * completion handling.
 *
 * Note that this uses the simple transport layer directly.  If we
 * want to add support for other layers, we'll need a switch of some
 * sort.
 *
 * Note that the simple transport mechanism does not require any
 * reentrancy protection; the OPQ read is atomic.  If there is a
 * chance of a race with something else that might move the request
 * off the busy list, then we will have to lock against that
 * (eg. timeouts, etc.)
 */
static void
ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
{
    struct ciss_request	*cr;
    struct ciss_command	*cc;
    u_int32_t		tag, index;

    debug_called(3);

    /*
     * Loop quickly taking requests from the adapter and moving them
     * to the completed queue.
     */
    for (;;) {

	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
	    break;
	index = tag >> 2;
	debug(2, "completed command %d%s", index,
	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
	if (index >= sc->ciss_max_requests) {
	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
	    continue;
	}
	cr = &(sc->ciss_request[index]);
	cc = cr->cr_cc;
	cc->header.host_tag = tag;	/* not updated by adapter */
	ciss_enqueue_complete(cr, qh);
    }

}

static void
ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
{
    struct ciss_request	*cr;
    struct ciss_command	*cc;
    u_int32_t		tag, index;

    debug_called(3);

    /*
     * Loop quickly taking requests from the adapter and moving them
     * to the completed queue.
     */
    for (;;) {
	tag = sc->ciss_reply[sc->ciss_rqidx];
	if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
	    break;
	index = tag >> 2;
	debug(2, "completed command %d%s\n", index,
	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
	if (index < sc->ciss_max_requests) {
	    cr = &(sc->ciss_request[index]);
	    cc = cr->cr_cc;
	    cc->header.host_tag = tag;	/* not updated by adapter */
	    ciss_enqueue_complete(cr, qh);
	} else {
	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
	}
	if (++sc->ciss_rqidx == sc->ciss_max_requests) {
	    sc->ciss_rqidx = 0;
	    sc->ciss_cycle ^= 1;
	}
    }

}

/************************************************************************
 * Take an interrupt from the adapter.
 */
static void
ciss_intr(void *arg)
{
    cr_qhead_t qh;
    struct ciss_softc	*sc = (struct ciss_softc *)arg;

    /*
     * The only interrupt we recognise indicates that there are
     * entries in the outbound post queue.
     */
    STAILQ_INIT(&qh);
    ciss_done(sc, &qh);
    mtx_lock(&sc->ciss_mtx);
    ciss_complete(sc, &qh);
    mtx_unlock(&sc->ciss_mtx);
}

static void
ciss_perf_intr(void *arg)
{
    struct ciss_softc	*sc = (struct ciss_softc *)arg;

    /* Clear the interrupt and flush the bridges.  Docs say that the flush
     * needs to be done twice, which doesn't seem right.
     */
    CISS_TL_PERF_CLEAR_INT(sc);
    CISS_TL_PERF_FLUSH_INT(sc);

    ciss_perf_msi_intr(sc);
}

static void
ciss_perf_msi_intr(void *arg)
{
    cr_qhead_t qh;
    struct ciss_softc	*sc = (struct ciss_softc *)arg;

    STAILQ_INIT(&qh);
    ciss_perf_done(sc, &qh);
    mtx_lock(&sc->ciss_mtx);
    ciss_complete(sc, &qh);
    mtx_unlock(&sc->ciss_mtx);
}


/************************************************************************
 * Process completed requests.
 *
 * Requests can be completed in three fashions:
 *
 * - by invoking a callback function (cr_complete is non-null)
 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
 */
static void
ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
{
    struct ciss_request	*cr;

    debug_called(2);

    /*
     * Loop taking requests off the completed queue and performing
     * completion processing on them.
     */
    for (;;) {
	if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
	    break;
	ciss_unmap_request(cr);

	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
	    ciss_printf(sc, "WARNING: completing non-busy request\n");
	cr->cr_flags &= ~CISS_REQ_BUSY;

	/*
	 * If the request has a callback, invoke it.
	 */
	if (cr->cr_complete != NULL) {
	    cr->cr_complete(cr);
	    continue;
	}

	/*
	 * If someone is sleeping on this request, wake them up.
	 */
	if (cr->cr_flags & CISS_REQ_SLEEP) {
	    cr->cr_flags &= ~CISS_REQ_SLEEP;
	    wakeup(cr);
	    continue;
	}

	/*
	 * If someone is polling this request for completion, signal.
	 */
	if (cr->cr_flags & CISS_REQ_POLL) {
	    cr->cr_flags &= ~CISS_REQ_POLL;
	    continue;
	}

	/*
	 * Give up and throw the request back on the free queue.  This
	 * should never happen; resources will probably be lost.
	 */
	ciss_printf(sc, "WARNING: completed command with no submitter\n");
	ciss_enqueue_free(cr);
    }
}

/************************************************************************
 * Report on the completion status of a request, and pass back SCSI
 * and command status values.
 */
static int
_ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
{
    struct ciss_command		*cc;
    struct ciss_error_info	*ce;

    debug_called(2);

    cc = cr->cr_cc;
    ce = (struct ciss_error_info *)&(cc->sg[0]);

    /*
     * We don't consider data under/overrun an error for the Report
     * Logical/Physical LUNs commands.
     */
    if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
	 (cc->cdb.cdb[0] == INQUIRY))) {
	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
	debug(2, "ignoring irrelevant under/overrun error");
    }

    /*
     * Check the command's error bit, if clear, there's no status and
     * everything is OK.
     */
    if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
	if (scsi_status != NULL)
	    *scsi_status = SCSI_STATUS_OK;
	if (command_status != NULL)
	    *command_status = CISS_CMD_STATUS_SUCCESS;
	return(0);
    } else {
	if (command_status != NULL)
	    *command_status = ce->command_status;
	if (scsi_status != NULL) {
	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
		*scsi_status = ce->scsi_status;
	    } else {
		*scsi_status = -1;
	    }
	}
	if (bootverbose)
	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
			ce->command_status, ciss_name_command_status(ce->command_status),
			ce->scsi_status);
	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
			ce->additional_error_info.invalid_command.offense_size,
			ce->additional_error_info.invalid_command.offense_offset,
			ce->additional_error_info.invalid_command.offense_value,
			func);
	}
    }
#if 0
    ciss_print_request(cr);
#endif
    return(1);
}

/************************************************************************
 * Issue a request and don't return until it's completed.
 *
 * Depending on adapter status, we may poll or sleep waiting for
 * completion.
 */
static int
ciss_synch_request(struct ciss_request *cr, int timeout)
{
    if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
	return(ciss_wait_request(cr, timeout));
    } else {
	return(ciss_poll_request(cr, timeout));
    }
}

/************************************************************************
 * Issue a request and poll for completion.
 *
 * Timeout in milliseconds.
 */
static int
ciss_poll_request(struct ciss_request *cr, int timeout)
{
    cr_qhead_t qh;
    struct ciss_softc *sc;
    int		error;

    debug_called(2);

    STAILQ_INIT(&qh);
    sc = cr->cr_sc;
    cr->cr_flags |= CISS_REQ_POLL;
    if ((error = ciss_start(cr)) != 0)
	return(error);

    do {
	if (sc->ciss_perf)
	    ciss_perf_done(sc, &qh);
	else
	    ciss_done(sc, &qh);
	ciss_complete(sc, &qh);
	if (!(cr->cr_flags & CISS_REQ_POLL))
	    return(0);
	DELAY(1000);
    } while (timeout-- >= 0);
    return(EWOULDBLOCK);
}

/************************************************************************
 * Issue a request and sleep waiting for completion.
 *
 * Timeout in milliseconds.  Note that a spurious wakeup will reset
 * the timeout.
 */
static int
ciss_wait_request(struct ciss_request *cr, int timeout)
{
    int		error;

    debug_called(2);

    cr->cr_flags |= CISS_REQ_SLEEP;
    if ((error = ciss_start(cr)) != 0)
	return(error);

    while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
	error = msleep_sbt(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ",
	    SBT_1MS * timeout, 0, 0);
    }
    return(error);
}

#if 0
/************************************************************************
 * Abort a request.  Note that a potential exists here to race the
 * request being completed; the caller must deal with this.
 */
static int
ciss_abort_request(struct ciss_request *ar)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_message_cdb	*cmc;
    int				error;

    debug_called(1);

    /* get a request */
    if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
	return(error);

    /* build the abort command */
    cc = cr->cr_cc;
    cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
    cc->header.address.physical.target = 0;
    cc->header.address.physical.bus = 0;
    cc->cdb.cdb_length = sizeof(*cmc);
    cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
    cc->cdb.timeout = 30;

    cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
    cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
    cmc->type = CISS_MESSAGE_ABORT_TASK;
    cmc->abort_tag = ar->cr_tag;	/* endianness?? */

    /*
     * Send the request and wait for a response.  If we believe we
     * aborted the request OK, clear the flag that indicates it's
     * running.
     */
    error = ciss_synch_request(cr, 35 * 1000);
    if (!error)
	error = ciss_report_request(cr, NULL, NULL);
    ciss_release_request(cr);

    return(error);
}
#endif


/************************************************************************
 * Fetch and initialise a request
 */
static int
ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
{
    struct ciss_request *cr;

    debug_called(2);

    /*
     * Get a request and clean it up.
     */
    if ((cr = ciss_dequeue_free(sc)) == NULL)
	return(ENOMEM);

    cr->cr_data = NULL;
    cr->cr_flags = 0;
    cr->cr_complete = NULL;
    cr->cr_private = NULL;
    cr->cr_sg_tag = CISS_SG_MAX;	/* Backstop to prevent accidents */

    ciss_preen_command(cr);
    *crp = cr;
    return(0);
}

static void
ciss_preen_command(struct ciss_request *cr)
{
    struct ciss_command	*cc;
    u_int32_t		cmdphys;

    /*
     * Clean up the command structure.
     *
     * Note that we set up the error_info structure here, since the
     * length can be overwritten by any command.
     */
    cc = cr->cr_cc;
    cc->header.sg_in_list = 0;		/* kinda inefficient this way */
    cc->header.sg_total = 0;
    cc->header.host_tag = cr->cr_tag << 2;
    cc->header.host_tag_zeroes = 0;
    bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
    cmdphys = cr->cr_ccphys;
    cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
    cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
}

/************************************************************************
 * Release a request to the free list.
 */
static void
ciss_release_request(struct ciss_request *cr)
{
    struct ciss_softc	*sc;

    debug_called(2);

    sc = cr->cr_sc;

    /* release the request to the free queue */
    ciss_requeue_free(cr);
}

/************************************************************************
 * Allocate a request that will be used to send a BMIC command.  Do some
 * of the common setup here to avoid duplicating it everywhere else.
 */
static int
ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
		      int opcode, void **bufp, size_t bufsize)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_bmic_cdb	*cbc;
    void			*buf;
    int				error;
    int				dataout;

    debug_called(2);

    cr = NULL;
    buf = NULL;

    /*
     * Get a request.
     */
    if ((error = ciss_get_request(sc, &cr)) != 0)
	goto out;

    /*
     * Allocate data storage if requested, determine the data direction.
     */
    dataout = 0;
    if ((bufsize > 0) && (bufp != NULL)) {
	if (*bufp == NULL) {
	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
		error = ENOMEM;
		goto out;
	    }
	} else {
	    buf = *bufp;
	    dataout = 1;	/* we are given a buffer, so we are writing */
	}
    }

    /*
     * Build a CISS BMIC command to get the logical drive ID.
     */
    cr->cr_data = buf;
    cr->cr_length = bufsize;
    if (!dataout)
	cr->cr_flags = CISS_REQ_DATAIN;

    cc = cr->cr_cc;
    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
    cc->header.address.physical.bus = 0;
    cc->header.address.physical.target = 0;
    cc->cdb.cdb_length = sizeof(*cbc);
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
    cc->cdb.timeout = 0;

    cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
    bzero(cbc, sizeof(*cbc));
    cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
    cbc->bmic_opcode = opcode;
    cbc->size = htons((u_int16_t)bufsize);

out:
    if (error) {
	if (cr != NULL)
	    ciss_release_request(cr);
    } else {
	*crp = cr;
	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
	    *bufp = buf;
    }
    return(error);
}

/************************************************************************
 * Handle a command passed in from userspace.
 */
static int
ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_error_info	*ce;
    int				error = 0;

    debug_called(1);

    cr = NULL;

    /*
     * Get a request.
     */
    while (ciss_get_request(sc, &cr) != 0)
	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
    cc = cr->cr_cc;

    /*
     * Allocate an in-kernel databuffer if required, copy in user data.
     */
    mtx_unlock(&sc->ciss_mtx);
    cr->cr_length = ioc->buf_size;
    if (ioc->buf_size > 0) {
	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
	    error = ENOMEM;
	    goto out_unlocked;
	}
	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
	    goto out_unlocked;
	}
    }

    /*
     * Build the request based on the user command.
     */
    bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
    bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));

    /* XXX anything else to populate here? */
    mtx_lock(&sc->ciss_mtx);

    /*
     * Run the command.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000))) {
	debug(0, "request failed - %d", error);
	goto out;
    }

    /*
     * Check to see if the command succeeded.
     */
    ce = (struct ciss_error_info *)&(cc->sg[0]);
    if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
	bzero(ce, sizeof(*ce));

    /*
     * Copy the results back to the user.
     */
    bcopy(ce, &ioc->error_info, sizeof(*ce));
    mtx_unlock(&sc->ciss_mtx);
    if ((ioc->buf_size > 0) &&
	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
	goto out_unlocked;
    }

    /* done OK */
    error = 0;

out_unlocked:
    mtx_lock(&sc->ciss_mtx);

out:
    if ((cr != NULL) && (cr->cr_data != NULL))
	free(cr->cr_data, CISS_MALLOC_CLASS);
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

/************************************************************************
 * Map a request into bus-visible space, initialise the scatter/gather
 * list.
 */
static int
ciss_map_request(struct ciss_request *cr)
{
    struct ciss_softc	*sc;
    int			error = 0;

    debug_called(2);

    sc = cr->cr_sc;

    /* check that mapping is necessary */
    if (cr->cr_flags & CISS_REQ_MAPPED)
	return(0);

    cr->cr_flags |= CISS_REQ_MAPPED;

    bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
		    BUS_DMASYNC_PREWRITE);

    if (cr->cr_data != NULL) {
	if (cr->cr_flags & CISS_REQ_CCB)
		error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
					cr->cr_datamap, cr->cr_data,
					ciss_request_map_helper, cr, 0);
	else
		error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
					cr->cr_data, cr->cr_length,
					ciss_request_map_helper, cr, 0);
	if (error != 0)
	    return (error);
    } else {
	/*
	 * Post the command to the adapter.
	 */
	cr->cr_sg_tag = CISS_SG_NONE;
	cr->cr_flags |= CISS_REQ_BUSY;
	if (sc->ciss_perf)
	    CISS_TL_PERF_POST_CMD(sc, cr);
	else
	    CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
    }

    return(0);
}

static void
ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
    struct ciss_command	*cc;
    struct ciss_request *cr;
    struct ciss_softc	*sc;
    int			i;

    debug_called(2);

    cr = (struct ciss_request *)arg;
    sc = cr->cr_sc;
    cc = cr->cr_cc;

    for (i = 0; i < nseg; i++) {
	cc->sg[i].address = segs[i].ds_addr;
	cc->sg[i].length = segs[i].ds_len;
	cc->sg[i].extension = 0;
    }
    /* we leave the s/g table entirely within the command */
    cc->header.sg_in_list = nseg;
    cc->header.sg_total = nseg;

    if (cr->cr_flags & CISS_REQ_DATAIN)
	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
    if (cr->cr_flags & CISS_REQ_DATAOUT)
	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);

    if (nseg == 0)
	cr->cr_sg_tag = CISS_SG_NONE;
    else if (nseg == 1)
	cr->cr_sg_tag = CISS_SG_1;
    else if (nseg == 2)
	cr->cr_sg_tag = CISS_SG_2;
    else if (nseg <= 4)
	cr->cr_sg_tag = CISS_SG_4;
    else if (nseg <= 8)
	cr->cr_sg_tag = CISS_SG_8;
    else if (nseg <= 16)
	cr->cr_sg_tag = CISS_SG_16;
    else if (nseg <= 32)
	cr->cr_sg_tag = CISS_SG_32;
    else
	cr->cr_sg_tag = CISS_SG_MAX;

    /*
     * Post the command to the adapter.
     */
    cr->cr_flags |= CISS_REQ_BUSY;
    if (sc->ciss_perf)
	CISS_TL_PERF_POST_CMD(sc, cr);
    else
	CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
}

/************************************************************************
 * Unmap a request from bus-visible space.
 */
static void
ciss_unmap_request(struct ciss_request *cr)
{
    struct ciss_softc	*sc;

    debug_called(2);

    sc = cr->cr_sc;

    /* check that unmapping is necessary */
    if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
	return;

    bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
		    BUS_DMASYNC_POSTWRITE);

    if (cr->cr_data == NULL)
	goto out;

    if (cr->cr_flags & CISS_REQ_DATAIN)
	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
    if (cr->cr_flags & CISS_REQ_DATAOUT)
	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);

    bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
out:
    cr->cr_flags &= ~CISS_REQ_MAPPED;
}

/************************************************************************
 * Attach the driver to CAM.
 *
 * We put all the logical drives on a single SCSI bus.
 */
static int
ciss_cam_init(struct ciss_softc *sc)
{
    int			i, maxbus;

    debug_called(1);

    /*
     * Allocate a devq.  We can reuse this for the masked physical
     * devices if we decide to export these as well.
     */
    if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
	ciss_printf(sc, "can't allocate CAM SIM queue\n");
	return(ENOMEM);
    }

    /*
     * Create a SIM.
     *
     * This naturally wastes a bit of memory.  The alternative is to allocate
     * and register each bus as it is found, and then track them on a linked
     * list.  Unfortunately, the driver has a few places where it needs to
     * look up the SIM based solely on bus number, and it's unclear whether
     * a list traversal would work for these situations.
     */
    maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
		 CISS_PHYSICAL_BASE);
    sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
    if (sc->ciss_cam_sim == NULL) {
	ciss_printf(sc, "can't allocate memory for controller SIM\n");
	return(ENOMEM);
    }

    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
						 "ciss", sc,
						 device_get_unit(sc->ciss_dev),
						 &sc->ciss_mtx,
						 2,
						 sc->ciss_max_requests - 2,
						 sc->ciss_cam_devq)) == NULL) {
	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
	    return(ENOMEM);
	}

	/*
	 * Register bus with this SIM.
	 */
	mtx_lock(&sc->ciss_mtx);
	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) { 
	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
		ciss_printf(sc, "can't register SCSI bus %d\n", i);
		mtx_unlock(&sc->ciss_mtx);
		return (ENXIO);
	    }
	}
	mtx_unlock(&sc->ciss_mtx);
    }

    for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
	 CISS_PHYSICAL_BASE; i++) {
	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
						 "ciss", sc,
						 device_get_unit(sc->ciss_dev),
						 &sc->ciss_mtx, 1,
						 sc->ciss_max_requests - 2,
						 sc->ciss_cam_devq)) == NULL) {
	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
	    return (ENOMEM);
	}

	mtx_lock(&sc->ciss_mtx);
	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
	    mtx_unlock(&sc->ciss_mtx);
	    return (ENXIO);
	}
	mtx_unlock(&sc->ciss_mtx);
    }

    return(0);
}

/************************************************************************
 * Initiate a rescan of the 'logical devices' SIM
 */
static void
ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
{
    union ccb		*ccb;

    debug_called(1);

    if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
	return;
    }

    if (xpt_create_path(&ccb->ccb_h.path, NULL,
	    cam_sim_path(sc->ciss_cam_sim[bus]),
	    target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
	ciss_printf(sc, "rescan failed (can't create path)\n");
	xpt_free_ccb(ccb);
	return;
    }
    xpt_rescan(ccb);
    /* scan is now in progress */
}

/************************************************************************
 * Handle requests coming from CAM
 */
static void
ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
{
    struct ciss_softc	*sc;
    struct ccb_scsiio	*csio;
    int			bus, target;
    int			physical;

    sc = cam_sim_softc(sim);
    bus = cam_sim_bus(sim);
    csio = (struct ccb_scsiio *)&ccb->csio;
    target = csio->ccb_h.target_id;
    physical = CISS_IS_PHYSICAL(bus);

    switch (ccb->ccb_h.func_code) {

	/* perform SCSI I/O */
    case XPT_SCSI_IO:
	if (!ciss_cam_action_io(sim, csio))
	    return;
	break;

	/* perform geometry calculations */
    case XPT_CALC_GEOMETRY:
    {
	struct ccb_calc_geometry	*ccg = &ccb->ccg;
	struct ciss_ldrive		*ld;

	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);

	ld = NULL;
	if (!physical)
	    ld = &sc->ciss_logical[bus][target];
	    
	/*
	 * Use the cached geometry settings unless the fault tolerance
	 * is invalid.
	 */
	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
	    u_int32_t			secs_per_cylinder;

	    ccg->heads = 255;
	    ccg->secs_per_track = 32;
	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
	} else {
	    ccg->heads = ld->cl_geometry.heads;
	    ccg->secs_per_track = ld->cl_geometry.sectors;
	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
	}
	ccb->ccb_h.status = CAM_REQ_CMP;
        break;
    }

	/* handle path attribute inquiry */
    case XPT_PATH_INQ:
    {
	struct ccb_pathinq	*cpi = &ccb->cpi;
	int			sg_length;

	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);

	cpi->version_num = 1;
	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
	cpi->target_sprt = 0;
	cpi->hba_misc = 0;
	cpi->max_target = sc->ciss_cfg->max_logical_supported;
	cpi->max_lun = 0;		/* 'logical drive' channel only */
	cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
	strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
	strlcpy(cpi->hba_vid, "CISS", HBA_IDLEN);
	strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
	cpi->unit_number = cam_sim_unit(sim);
	cpi->bus_id = cam_sim_bus(sim);
	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
	cpi->transport = XPORT_SPI;
	cpi->transport_version = 2;
	cpi->protocol = PROTO_SCSI;
	cpi->protocol_version = SCSI_REV_2;
	if (sc->ciss_cfg->max_sg_length == 0) {
		sg_length = 17;
	} else {
	/* XXX Fix for ZMR cards that advertise max_sg_length == 32
	 * Confusing bit here. max_sg_length is usually a power of 2. We always
	 * need to subtract 1 to account for partial pages. Then we need to 
	 * align on a valid PAGE_SIZE so we round down to the nearest power of 2. 
	 * Add 1 so we can then subtract it out in the assignment to maxio.
	 * The reason for all these shenanigans is to create a maxio value that
	 * creates IO operations to volumes that yield consistent operations
	 * with good performance.
	 */
		sg_length = sc->ciss_cfg->max_sg_length - 1;
		sg_length = (1 << (fls(sg_length) - 1)) + 1;
	}
	cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
	ccb->ccb_h.status = CAM_REQ_CMP;
	break;
    }

    case XPT_GET_TRAN_SETTINGS:
    {
	struct ccb_trans_settings	*cts = &ccb->cts;
	int				bus, target;
	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;

	bus = cam_sim_bus(sim);
	target = cts->ccb_h.target_id;

	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
	/* disconnect always OK */
	cts->protocol = PROTO_SCSI;
	cts->protocol_version = SCSI_REV_2;
	cts->transport = XPORT_SPI;
	cts->transport_version = 2;

	spi->valid = CTS_SPI_VALID_DISC;
	spi->flags = CTS_SPI_FLAGS_DISC_ENB;

	scsi->valid = CTS_SCSI_VALID_TQ;
	scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;

	cts->ccb_h.status = CAM_REQ_CMP;
	break;
    }

    default:		/* we can't do this */
	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
	ccb->ccb_h.status = CAM_REQ_INVALID;
	break;
    }

    xpt_done(ccb);
}

/************************************************************************
 * Handle a CAM SCSI I/O request.
 */
static int
ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
{
    struct ciss_softc	*sc;
    int			bus, target;
    struct ciss_request	*cr;
    struct ciss_command	*cc;
    int			error;

    sc = cam_sim_softc(sim);
    bus = cam_sim_bus(sim);
    target = csio->ccb_h.target_id;

    debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);

    /* check that the CDB pointer is not to a physical address */
    if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
	debug(3, "  CDB pointer is to physical address");
	csio->ccb_h.status = CAM_REQ_CMP_ERR;
    }

    /* abandon aborted ccbs or those that have failed validation */
    if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
	debug(3, "abandoning CCB due to abort/validation failure");
	return(EINVAL);
    }

    /* handle emulation of some SCSI commands ourself */
    if (ciss_cam_emulate(sc, csio))
	return(0);

    /*
     * Get a request to manage this command.  If we can't, return the
     * ccb, freeze the queue and flag so that we unfreeze it when a
     * request completes.
     */
    if ((error = ciss_get_request(sc, &cr)) != 0) {
	xpt_freeze_simq(sim, 1);
	sc->ciss_flags |= CISS_FLAG_BUSY;
	csio->ccb_h.status |= CAM_REQUEUE_REQ;
	return(error);
    }

    /*
     * Build the command.
     */
    cc = cr->cr_cc;
    cr->cr_data = csio;
    cr->cr_length = csio->dxfer_len;
    cr->cr_complete = ciss_cam_complete;
    cr->cr_private = csio;

    /*
     * Target the right logical volume.
     */
    if (CISS_IS_PHYSICAL(bus))
	cc->header.address =
	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
    else
	cc->header.address =
	    sc->ciss_logical[bus][target].cl_address;
    cc->cdb.cdb_length = csio->cdb_len;
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
    if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
	cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
    } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
	cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
    } else {
	cr->cr_data = NULL;
	cr->cr_flags = 0;
	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
    }
    cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
    if (csio->ccb_h.flags & CAM_CDB_POINTER) {
	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
    } else {
	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
    }

    /*
     * Submit the request to the adapter.
     *
     * Note that this may fail if we're unable to map the request (and
     * if we ever learn a transport layer other than simple, may fail
     * if the adapter rejects the command).
     */
    if ((error = ciss_start(cr)) != 0) {
	xpt_freeze_simq(sim, 1);
	csio->ccb_h.status |= CAM_RELEASE_SIMQ;
	if (error == EINPROGRESS) {
	    error = 0;
	} else {
	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
	    ciss_release_request(cr);
	}
	return(error);
    }

    return(0);
}

/************************************************************************
 * Emulate SCSI commands the adapter doesn't handle as we might like.
 */
static int
ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
{
    int		bus, target;
    u_int8_t	opcode;

    target = csio->ccb_h.target_id;
    bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
    opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];

    if (CISS_IS_PHYSICAL(bus)) {
	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
	    csio->ccb_h.status |= CAM_SEL_TIMEOUT;
	    xpt_done((union ccb *)csio);
	    return(1);
	} else
	    return(0);
    }

    /*
     * Handle requests for volumes that don't exist or are not online.
     * A selection timeout is slightly better than an illegal request.
     * Other errors might be better.
     */
    if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
	csio->ccb_h.status |= CAM_SEL_TIMEOUT;
	xpt_done((union ccb *)csio);
	return(1);
    }

    /* if we have to fake Synchronise Cache */
    if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
	/*
	 * If this is a Synchronise Cache command, typically issued when
	 * a device is closed, flush the adapter and complete now.
	 */
	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
	    ciss_flush_adapter(sc);
	    csio->ccb_h.status |= CAM_REQ_CMP;
	    xpt_done((union ccb *)csio);
	    return(1);
	}
    }

    /* 
     * A CISS target can only ever have one lun per target. REPORT_LUNS requires
     * at least one LUN field to be pre created for us, so snag it and fill in
     * the least significant byte indicating 1 LUN here.  Emulate the command
     * return to shut up warning on console of a CDB error.  swb 
     */
    if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
       csio->data_ptr[3] = 8;
       csio->ccb_h.status |= CAM_REQ_CMP;
       xpt_done((union ccb *)csio);
       return(1);
    }

    return(0);
}

/************************************************************************
 * Check for possibly-completed commands.
 */
static void
ciss_cam_poll(struct cam_sim *sim)
{
    cr_qhead_t qh;
    struct ciss_softc	*sc = cam_sim_softc(sim);

    debug_called(2);

    STAILQ_INIT(&qh);
    if (sc->ciss_perf)
	ciss_perf_done(sc, &qh);
    else
	ciss_done(sc, &qh);
    ciss_complete(sc, &qh);
}

/************************************************************************
 * Handle completion of a command - pass results back through the CCB
 */
static void
ciss_cam_complete(struct ciss_request *cr)
{
    struct ciss_softc		*sc;
    struct ciss_command		*cc;
    struct ciss_error_info	*ce;
    struct ccb_scsiio		*csio;
    int				scsi_status;
    int				command_status;

    debug_called(2);

    sc = cr->cr_sc;
    cc = cr->cr_cc;
    ce = (struct ciss_error_info *)&(cc->sg[0]);
    csio = (struct ccb_scsiio *)cr->cr_private;

    /*
     * Extract status values from request.
     */
    ciss_report_request(cr, &command_status, &scsi_status);
    csio->scsi_status = scsi_status;

    /*
     * Handle specific SCSI status values.
     */
    switch(scsi_status) {
	/* no status due to adapter error */
    case -1:
	debug(0, "adapter error");
	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
	break;

	/* no status due to command completed OK */
    case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
	debug(2, "SCSI_STATUS_OK");
	csio->ccb_h.status |= CAM_REQ_CMP;
	break;

	/* check condition, sense data included */
    case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
	      ce->sense_length, ce->residual_count);
	bzero(&csio->sense_data, SSD_FULL_SIZE);
	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
	if (csio->sense_len > ce->sense_length)
		csio->sense_resid = csio->sense_len - ce->sense_length;
	else
		csio->sense_resid = 0;
	csio->resid = ce->residual_count;
	csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
#ifdef CISS_DEBUG
	{
	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
	    debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
		  csio->sense_resid, /*show_errors*/ 1));
	}
#endif
	break;

    case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
	debug(0, "SCSI_STATUS_BUSY");
	csio->ccb_h.status |= CAM_SCSI_BUSY;
	break;

    default:
	debug(0, "unknown status 0x%x", csio->scsi_status);
	csio->ccb_h.status |= CAM_REQ_CMP_ERR;
	break;
    }

    /* handle post-command fixup */
    ciss_cam_complete_fixup(sc, csio);

    ciss_release_request(cr);
    if (sc->ciss_flags & CISS_FLAG_BUSY) {
	sc->ciss_flags &= ~CISS_FLAG_BUSY;
	if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
	    xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
	else
	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
    }
    xpt_done((union ccb *)csio);
}

/********************************************************************************
 * Fix up the result of some commands here.
 */
static void
ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
{
    struct scsi_inquiry_data	*inq;
    struct ciss_ldrive		*cl;
    uint8_t			*cdb;
    int				bus, target;

    cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
	 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
    if (cdb[0] == INQUIRY && 
	(cdb[1] & SI_EVPD) == 0 &&
	(csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
	csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {

	inq = (struct scsi_inquiry_data *)csio->data_ptr;
	target = csio->ccb_h.target_id;
	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));

	/*
	 * If the controller is in JBOD mode, there are no logical volumes.
	 * Let the disks be probed and dealt with via CAM.  Else, mask off 
	 * the physical disks and setup the parts of the inq structure for
	 * the logical volume.  swb
	 */
	if( !(sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED)){
		if (CISS_IS_PHYSICAL(bus)) {
	    		if (SID_TYPE(inq) == T_DIRECT)
				inq->device = (inq->device & 0xe0) | T_NODEVICE;
	    		return;
		}
		cl = &sc->ciss_logical[bus][target];

		padstr(inq->vendor, "HP",
	       		SID_VENDOR_SIZE);
		padstr(inq->product,
	       		ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
	       		SID_PRODUCT_SIZE);
		padstr(inq->revision,
	       		ciss_name_ldrive_status(cl->cl_lstatus->status),
	       		SID_REVISION_SIZE);
	}
    }
}


/********************************************************************************
 * Name the device at (target)
 *
 * XXX is this strictly correct?
 */
static int
ciss_name_device(struct ciss_softc *sc, int bus, int target)
{
    struct cam_periph	*periph;
    struct cam_path	*path;
    int			status;

    if (CISS_IS_PHYSICAL(bus))
	return (0);

    status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
			     target, 0);

    if (status == CAM_REQ_CMP) {
	xpt_path_lock(path);
	periph = cam_periph_find(path, NULL);
	xpt_path_unlock(path);
	xpt_free_path(path);
	if (periph != NULL) {
		sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
			periph->periph_name, periph->unit_number);
		return(0);
	}
    }
    sc->ciss_logical[bus][target].cl_name[0] = 0;
    return(ENOENT);
}

/************************************************************************
 * Periodic status monitoring.
 */
static void
ciss_periodic(void *arg)
{
    struct ciss_softc	*sc;
    struct ciss_request	*cr = NULL;
    struct ciss_command	*cc = NULL;
    int			error = 0;

    debug_called(1);

    sc = (struct ciss_softc *)arg;

    /*
     * Check the adapter heartbeat.
     */
    if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
	sc->ciss_heart_attack++;
	debug(0, "adapter heart attack in progress 0x%x/%d",
	      sc->ciss_heartbeat, sc->ciss_heart_attack);
	if (sc->ciss_heart_attack == 3) {
	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
	    ciss_disable_adapter(sc);
	    return;
	}
    } else {
	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
	sc->ciss_heart_attack = 0;
	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
    }

    /*
     * Send the NOP message and wait for a response.
     */
    if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
	cc = cr->cr_cc;
	cr->cr_complete = ciss_nop_complete;
	cc->cdb.cdb_length = 1;
	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
	cc->cdb.timeout = 0;
	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;

	if ((error = ciss_start(cr)) != 0) {
	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
	}
    }

    /*
     * If the notify event request has died for some reason, or has
     * not started yet, restart it.
     */
    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
	debug(0, "(re)starting Event Notify chain");
	ciss_notify_event(sc);
    }

    /*
     * Reschedule.
     */
    callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
}

static void
ciss_nop_complete(struct ciss_request *cr)
{
    struct ciss_softc		*sc;
    static int			first_time = 1;

    sc = cr->cr_sc;
    if (ciss_report_request(cr, NULL, NULL) != 0) {
	if (first_time == 1) {
	    first_time = 0;
	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
	}
    }

    ciss_release_request(cr);
}

/************************************************************************
 * Disable the adapter.
 *
 * The all requests in completed queue is failed with hardware error.
 * This will cause failover in a multipath configuration.
 */
static void
ciss_disable_adapter(struct ciss_softc *sc)
{
    cr_qhead_t			qh;
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_error_info	*ce;
    int				i;

    CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
    pci_disable_busmaster(sc->ciss_dev);
    sc->ciss_flags &= ~CISS_FLAG_RUNNING;

    STAILQ_INIT(&qh);
    for (i = 1; i < sc->ciss_max_requests; i++) {
	cr = &sc->ciss_request[i];
	if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
	    continue;

	cc = cr->cr_cc;
	ce = (struct ciss_error_info *)&(cc->sg[0]);
	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
	ciss_enqueue_complete(cr, &qh);
    }

    for (;;) {
	if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
	    break;
    
	/*
	 * If the request has a callback, invoke it.
	 */
	if (cr->cr_complete != NULL) {
	    cr->cr_complete(cr);
	    continue;
	}

	/*
	 * If someone is sleeping on this request, wake them up.
	 */
	if (cr->cr_flags & CISS_REQ_SLEEP) {
	    cr->cr_flags &= ~CISS_REQ_SLEEP;
	    wakeup(cr);
	    continue;
	}
    }
}

/************************************************************************
 * Request a notification response from the adapter.
 *
 * If (cr) is NULL, this is the first request of the adapter, so
 * reset the adapter's message pointer and start with the oldest
 * message available.
 */
static void
ciss_notify_event(struct ciss_softc *sc)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_notify_cdb	*cnc;
    int				error;

    debug_called(1);

    cr = sc->ciss_periodic_notify;

    /* get a request if we don't already have one */
    if (cr == NULL) {
	if ((error = ciss_get_request(sc, &cr)) != 0) {
	    debug(0, "can't get notify event request");
	    goto out;
	}
	sc->ciss_periodic_notify = cr;
	cr->cr_complete = ciss_notify_complete;
	debug(1, "acquired request %d", cr->cr_tag);
    }

    /*
     * Get a databuffer if we don't already have one, note that the
     * adapter command wants a larger buffer than the actual
     * structure.
     */
    if (cr->cr_data == NULL) {
	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
	    debug(0, "can't get notify event request buffer");
	    error = ENOMEM;
	    goto out;
	}
	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
    }

    /* re-setup the request's command (since we never release it) XXX overkill*/
    ciss_preen_command(cr);

    /* (re)build the notify event command */
    cc = cr->cr_cc;
    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
    cc->header.address.physical.bus = 0;
    cc->header.address.physical.target = 0;

    cc->cdb.cdb_length = sizeof(*cnc);
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
    cc->cdb.timeout = 0;	/* no timeout, we hope */

    cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
    bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
    cnc->opcode = CISS_OPCODE_READ;
    cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
    cnc->timeout = 0;		/* no timeout, we hope */
    cnc->synchronous = 0;
    cnc->ordered = 0;
    cnc->seek_to_oldest = 0;
    if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
	cnc->new_only = 1;
    else
	cnc->new_only = 0;
    cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);

    /* submit the request */
    error = ciss_start(cr);

 out:
    if (error) {
	if (cr != NULL) {
	    if (cr->cr_data != NULL)
		free(cr->cr_data, CISS_MALLOC_CLASS);
	    ciss_release_request(cr);
	}
	sc->ciss_periodic_notify = NULL;
	debug(0, "can't submit notify event request");
	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
    } else {
	debug(1, "notify event submitted");
	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
    }
}

static void
ciss_notify_complete(struct ciss_request *cr)
{
    struct ciss_command	*cc;
    struct ciss_notify	*cn;
    struct ciss_softc	*sc;
    int			scsi_status;
    int			command_status;
    debug_called(1);

    cc = cr->cr_cc;
    cn = (struct ciss_notify *)cr->cr_data;
    sc = cr->cr_sc;

    /*
     * Report request results, decode status.
     */
    ciss_report_request(cr, &command_status, &scsi_status);

    /*
     * Abort the chain on a fatal error.
     *
     * XXX which of these are actually errors?
     */
    if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
		    ciss_name_command_status(command_status));
	ciss_release_request(cr);
	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
	return;
    }

    /*
     * If the adapter gave us a text message, print it.
     */
    if (cn->message[0] != 0)
	ciss_printf(sc, "*** %.80s\n", cn->message);

    debug(0, "notify event class %d subclass %d detail %d",
		cn->class, cn->subclass, cn->detail);

    /*
     * If the response indicates that the notifier has been aborted,
     * release the notifier command.
     */
    if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
	(cn->detail == 1)) {
	debug(0, "notifier exiting");
	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
	ciss_release_request(cr);
	sc->ciss_periodic_notify = NULL;
	wakeup(&sc->ciss_periodic_notify);
    } else {
	/* Handle notify events in a kernel thread */
	ciss_enqueue_notify(cr);
	sc->ciss_periodic_notify = NULL;
	wakeup(&sc->ciss_periodic_notify);
	wakeup(&sc->ciss_notify);
    }
    /*
     * Send a new notify event command, if we're not aborting.
     */
    if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
	ciss_notify_event(sc);
    }
}

/************************************************************************
 * Abort the Notify Event chain.
 *
 * Note that we can't just abort the command in progress; we have to
 * explicitly issue an Abort Notify Event command in order for the
 * adapter to clean up correctly.
 *
 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
 * the chain will not restart itself.
 */
static int
ciss_notify_abort(struct ciss_softc *sc)
{
    struct ciss_request		*cr;
    struct ciss_command		*cc;
    struct ciss_notify_cdb	*cnc;
    int				error, command_status, scsi_status;

    debug_called(1);

    cr = NULL;
    error = 0;

    /* verify that there's an outstanding command */
    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
	goto out;

    /* get a command to issue the abort with */
    if ((error = ciss_get_request(sc, &cr)))
	goto out;

    /* get a buffer for the result */
    if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
	debug(0, "can't get notify event request buffer");
	error = ENOMEM;
	goto out;
    }
    cr->cr_length = CISS_NOTIFY_DATA_SIZE;

    /* build the CDB */
    cc = cr->cr_cc;
    cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
    cc->header.address.physical.bus = 0;
    cc->header.address.physical.target = 0;
    cc->cdb.cdb_length = sizeof(*cnc);
    cc->cdb.type = CISS_CDB_TYPE_COMMAND;
    cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
    cc->cdb.direction = CISS_CDB_DIRECTION_READ;
    cc->cdb.timeout = 0;	/* no timeout, we hope */

    cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
    bzero(cnc, sizeof(*cnc));
    cnc->opcode = CISS_OPCODE_WRITE;
    cnc->command = CISS_COMMAND_ABORT_NOTIFY;
    cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
#if 0
    ciss_print_request(cr);
#endif

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, &scsi_status);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:
	break;
    case CISS_CMD_STATUS_INVALID_COMMAND:
	/*
	 * Some older adapters don't support the CISS version of this
	 * command.  Fall back to using the BMIC version.
	 */
	error = ciss_notify_abort_bmic(sc);
	if (error != 0)
	    goto out;
	break;

    case CISS_CMD_STATUS_TARGET_STATUS:
	/*
	 * This can happen if the adapter thinks there wasn't an outstanding
	 * Notify Event command but we did.  We clean up here.
	 */
	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
	    if (sc->ciss_periodic_notify != NULL)
		ciss_release_request(sc->ciss_periodic_notify);
	    error = 0;
	    goto out;
	}
	/* FALLTHROUGH */

    default:
	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }

    /*
     * Sleep waiting for the notifier command to complete.  Note
     * that if it doesn't, we may end up in a bad situation, since
     * the adapter may deliver it later.  Also note that the adapter
     * requires the Notify Event command to be cancelled in order to
     * maintain internal bookkeeping.
     */
    while (sc->ciss_periodic_notify != NULL) {
	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
	if (error == EWOULDBLOCK) {
	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
	    break;
	}
    }

 out:
    /* release the cancel request */
    if (cr != NULL) {
	if (cr->cr_data != NULL)
	    free(cr->cr_data, CISS_MALLOC_CLASS);
	ciss_release_request(cr);
    }
    if (error == 0)
	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
    return(error);
}

/************************************************************************
 * Abort the Notify Event chain using a BMIC command.
 */
static int
ciss_notify_abort_bmic(struct ciss_softc *sc)
{
    struct ciss_request			*cr;
    int					error, command_status;

    debug_called(1);

    cr = NULL;
    error = 0;

    /* verify that there's an outstanding command */
    if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
	goto out;

    /*
     * Build a BMIC command to cancel the Notify on Event command.
     *
     * Note that we are sending a CISS opcode here.  Odd.
     */
    if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
				       NULL, 0)) != 0)
	goto out;

    /*
     * Submit the request and wait for it to complete.
     */
    if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
	goto out;
    }

    /*
     * Check response.
     */
    ciss_report_request(cr, &command_status, NULL);
    switch(command_status) {
    case CISS_CMD_STATUS_SUCCESS:
	break;
    default:
	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
		    ciss_name_command_status(command_status));
	error = EIO;
	goto out;
    }

out:
    if (cr != NULL)
	ciss_release_request(cr);
    return(error);
}

/************************************************************************
 * Handle rescanning all the logical volumes when a notify event
 * causes the drives to come online or offline.
 */
static void
ciss_notify_rescan_logical(struct ciss_softc *sc)
{
    struct ciss_lun_report      *cll;
    struct ciss_ldrive		*ld;
    int                         i, j, ndrives;

    /*
     * We must rescan all logical volumes to get the right logical
     * drive address.
     */
    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
                           sc->ciss_cfg->max_logical_supported);
    if (cll == NULL)
        return;

    ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));

    /*
     * Delete any of the drives which were destroyed by the
     * firmware.
     */
    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
	    ld = &sc->ciss_logical[i][j];

	    if (ld->cl_update == 0)
		continue;

	    if (ld->cl_status != CISS_LD_ONLINE) {
		ciss_cam_rescan_target(sc, i, j);
		ld->cl_update = 0;
		if (ld->cl_ldrive)
		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
		if (ld->cl_lstatus)
		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);

		ld->cl_ldrive = NULL;
		ld->cl_lstatus = NULL;
	    }
	}
    }

    /*
     * Scan for new drives.
     */
    for (i = 0; i < ndrives; i++) {
	int	bus, target;

	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
	ld	= &sc->ciss_logical[bus][target];

	if (ld->cl_update == 0)
		continue;

	ld->cl_update		= 0;
	ld->cl_address		= cll->lun[i];
	ld->cl_controller	= &sc->ciss_controllers[bus];
	if (ciss_identify_logical(sc, ld) == 0) {
	    ciss_cam_rescan_target(sc, bus, target);
	}
    }
    free(cll, CISS_MALLOC_CLASS);
}

/************************************************************************
 * Handle a notify event relating to the status of a logical drive.
 *
 * XXX need to be able to defer some of these to properly handle
 *     calling the "ID Physical drive" command, unless the 'extended'
 *     drive IDs are always in BIG_MAP format.
 */
static void
ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
{
    struct ciss_ldrive	*ld;
    int			ostatus, bus, target;

    debug_called(2);

    bus		= cn->device.physical.bus;
    target	= cn->data.logical_status.logical_drive;
    ld		= &sc->ciss_logical[bus][target];

    switch (cn->subclass) {
    case CISS_NOTIFY_LOGICAL_STATUS:
	switch (cn->detail) {
	case 0:
	    ciss_name_device(sc, bus, target);
	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
			cn->data.logical_status.logical_drive, ld->cl_name,
			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
			ciss_name_ldrive_status(cn->data.logical_status.new_state),
			cn->data.logical_status.spare_state,
			"\20\1configured\2rebuilding\3failed\4in use\5available\n");

	    /*
	     * Update our idea of the drive's status.
	     */
	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
	    if (ld->cl_lstatus != NULL)
		ld->cl_lstatus->status = cn->data.logical_status.new_state;

	    /*
	     * Have CAM rescan the drive if its status has changed.
	     */
	    if (ostatus != ld->cl_status) {
		ld->cl_update = 1;
		ciss_notify_rescan_logical(sc);
	    }

	    break;

	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
	    ciss_name_device(sc, bus, target);
	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
			cn->data.logical_status.logical_drive, ld->cl_name);
	    ciss_accept_media(sc, ld);

	    ld->cl_update = 1;
	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
	    ciss_notify_rescan_logical(sc);
	    break;

	case 2:
	case 3:
	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
			cn->data.rebuild_aborted.logical_drive,
			ld->cl_name,
			(cn->detail == 2) ? "read" : "write");
	    break;
	}
	break;

    case CISS_NOTIFY_LOGICAL_ERROR:
	if (cn->detail == 0) {
	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
			cn->data.io_error.logical_drive,
			ld->cl_name,
			cn->data.io_error.failure_bus,
			cn->data.io_error.failure_drive);
	    /* XXX should we take the drive down at this point, or will we be told? */
	}
	break;

    case CISS_NOTIFY_LOGICAL_SURFACE:
	if (cn->detail == 0)
	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
			cn->data.consistency_completed.logical_drive,
			ld->cl_name);
	break;
    }
}

/************************************************************************
 * Handle a notify event relating to the status of a physical drive.
 */
static void
ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
{
}

/************************************************************************
 * Handle a notify event relating to the status of a physical drive.
 */
static void
ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
{
    struct ciss_lun_report *cll = NULL;
    int bus, target;

    switch (cn->subclass) {
    case CISS_NOTIFY_HOTPLUG_PHYSICAL:
    case CISS_NOTIFY_HOTPLUG_NONDISK:
	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
	target =
	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);

	if (cn->detail == 0) {
	    /*
	     * Mark the device offline so that it'll start producing selection
	     * timeouts to the upper layer.
	     */
	    if ((bus >= 0) && (target >= 0))
		sc->ciss_physical[bus][target].cp_online = 0;
	} else {
	    /*
	     * Rescan the physical lun list for new items
	     */
	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
				   sc->ciss_cfg->max_physical_supported);
	    if (cll == NULL) {
		ciss_printf(sc, "Warning, cannot get physical lun list\n");
		break;
	    }
	    ciss_filter_physical(sc, cll);
	}
	break;

    default:
	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
	return;
    }

    if (cll != NULL)
	free(cll, CISS_MALLOC_CLASS);
}

/************************************************************************
 * Handle deferred processing of notify events.  Notify events may need
 * sleep which is unsafe during an interrupt.
 */
static void
ciss_notify_thread(void *arg)
{
    struct ciss_softc		*sc;
    struct ciss_request		*cr;
    struct ciss_notify		*cn;

    sc = (struct ciss_softc *)arg;
    mtx_lock(&sc->ciss_mtx);

    for (;;) {
	if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
	}

	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
	    break;

	cr = ciss_dequeue_notify(sc);

	if (cr == NULL)
		panic("cr null");
	cn = (struct ciss_notify *)cr->cr_data;

	switch (cn->class) {
	case CISS_NOTIFY_HOTPLUG:
	    ciss_notify_hotplug(sc, cn);
	    break;
	case CISS_NOTIFY_LOGICAL:
	    ciss_notify_logical(sc, cn);
	    break;
	case CISS_NOTIFY_PHYSICAL:
	    ciss_notify_physical(sc, cn);
	    break;
	}

	ciss_release_request(cr);

    }
    sc->ciss_notify_thread = NULL;
    wakeup(&sc->ciss_notify_thread);

    mtx_unlock(&sc->ciss_mtx);
    kproc_exit(0);
}

/************************************************************************
 * Start the notification kernel thread.
 */
static void
ciss_spawn_notify_thread(struct ciss_softc *sc)
{

    if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
		       device_get_unit(sc->ciss_dev)))
	panic("Could not create notify thread\n");
}

/************************************************************************
 * Kill the notification kernel thread.
 */
static void
ciss_kill_notify_thread(struct ciss_softc *sc)
{

    if (sc->ciss_notify_thread == NULL)
	return;

    sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
    wakeup(&sc->ciss_notify);
    msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
}

/************************************************************************
 * Print a request.
 */
#ifdef DDB
static void
ciss_print_request(struct ciss_request *cr)
{
    struct ciss_softc	*sc;
    struct ciss_command	*cc;
    int			i;

    sc = cr->cr_sc;
    cc = cr->cr_cc;

    ciss_printf(sc, "REQUEST @ %p\n", cr);
    ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
    ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
    switch(cc->header.address.mode.mode) {
    case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
    case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
	ciss_printf(sc, "  physical bus %d target %d\n",
		    cc->header.address.physical.bus, cc->header.address.physical.target);
	break;
    case CISS_HDR_ADDRESS_MODE_LOGICAL:
	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
	break;
    }
    ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
		cc->cdb.cdb_length,
		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
    ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");

    if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
	/* XXX print error info */
    } else {
	/* since we don't use chained s/g, don't support it here */
	for (i = 0; i < cc->header.sg_in_list; i++) {
	    if ((i % 4) == 0)
		ciss_printf(sc, "   ");
	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
		printf("\n");
	}
    }
}
#endif

/************************************************************************
 * Print information about the status of a logical drive.
 */
static void
ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
{
    int		bus, target, i;

    if (ld->cl_lstatus == NULL) {
	printf("does not exist\n");
	return;
    }

    /* print drive status */
    switch(ld->cl_lstatus->status) {
    case CISS_LSTATUS_OK:
	printf("online\n");
	break;
    case CISS_LSTATUS_INTERIM_RECOVERY:
	printf("in interim recovery mode\n");
	break;
    case CISS_LSTATUS_READY_RECOVERY:
	printf("ready to begin recovery\n");
	break;
    case CISS_LSTATUS_RECOVERING:
	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
	       bus, target, ld->cl_lstatus->blocks_to_recover);
	break;
    case CISS_LSTATUS_EXPANDING:
	printf("being expanded, %u blocks remaining\n",
	       ld->cl_lstatus->blocks_to_recover);
	break;
    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
	printf("queued for expansion\n");
	break;
    case CISS_LSTATUS_FAILED:
	printf("queued for expansion\n");
	break;
    case CISS_LSTATUS_WRONG_PDRIVE:
	printf("wrong physical drive inserted\n");
	break;
    case CISS_LSTATUS_MISSING_PDRIVE:
	printf("missing a needed physical drive\n");
	break;
    case CISS_LSTATUS_BECOMING_READY:
	printf("becoming ready\n");
	break;
    }

    /* print failed physical drives */
    for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
	if (bus == -1)
	    continue;
	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
		    ld->cl_lstatus->drive_failure_map[i]);
    }
}

#ifdef DDB
#include <ddb/ddb.h>
/************************************************************************
 * Print information about the controller/driver.
 */
static void
ciss_print_adapter(struct ciss_softc *sc)
{
    int		i, j;

    ciss_printf(sc, "ADAPTER:\n");
    for (i = 0; i < CISSQ_COUNT; i++) {
	ciss_printf(sc, "%s     %d/%d\n",
	    i == 0 ? "free" :
	    i == 1 ? "busy" : "complete",
	    sc->ciss_qstat[i].q_length,
	    sc->ciss_qstat[i].q_max);
    }
    ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
    ciss_printf(sc, "flags %b\n", sc->ciss_flags,
	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");

    for (i = 0; i < sc->ciss_max_logical_bus; i++) {
	for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
	}
    }

    /* XXX Should physical drives be printed out here? */

    for (i = 1; i < sc->ciss_max_requests; i++)
	ciss_print_request(sc->ciss_request + i);
}

/* DDB hook */
DB_COMMAND(ciss_prt, db_ciss_prt)
{
    struct ciss_softc	*sc;
    devclass_t dc;
    int maxciss, i;

    dc = devclass_find("ciss");
    if ( dc == NULL ) {
        printf("%s: can't find devclass!\n", __func__);
        return;
    }
    maxciss = devclass_get_maxunit(dc);
    for (i = 0; i < maxciss; i++) {
        sc = devclass_get_softc(dc, i);
	ciss_print_adapter(sc);
    }
}
#endif

/************************************************************************
 * Return a name for a logical drive status value.
 */
static const char *
ciss_name_ldrive_status(int status)
{
    switch (status) {
    case CISS_LSTATUS_OK:
	return("OK");
    case CISS_LSTATUS_FAILED:
	return("failed");
    case CISS_LSTATUS_NOT_CONFIGURED:
	return("not configured");
    case CISS_LSTATUS_INTERIM_RECOVERY:
	return("interim recovery");
    case CISS_LSTATUS_READY_RECOVERY:
	return("ready for recovery");
    case CISS_LSTATUS_RECOVERING:
	return("recovering");
    case CISS_LSTATUS_WRONG_PDRIVE:
	return("wrong physical drive inserted");
    case CISS_LSTATUS_MISSING_PDRIVE:
	return("missing physical drive");
    case CISS_LSTATUS_EXPANDING:
	return("expanding");
    case CISS_LSTATUS_BECOMING_READY:
	return("becoming ready");
    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
	return("queued for expansion");
    }
    return("unknown status");
}

/************************************************************************
 * Return an online/offline/nonexistent value for a logical drive
 * status value.
 */
static int
ciss_decode_ldrive_status(int status)
{
    switch(status) {
    case CISS_LSTATUS_NOT_CONFIGURED:
	return(CISS_LD_NONEXISTENT);

    case CISS_LSTATUS_OK:
    case CISS_LSTATUS_INTERIM_RECOVERY:
    case CISS_LSTATUS_READY_RECOVERY:
    case CISS_LSTATUS_RECOVERING:
    case CISS_LSTATUS_EXPANDING:
    case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
	return(CISS_LD_ONLINE);

    case CISS_LSTATUS_FAILED:
    case CISS_LSTATUS_WRONG_PDRIVE:
    case CISS_LSTATUS_MISSING_PDRIVE:
    case CISS_LSTATUS_BECOMING_READY:
    default:
	return(CISS_LD_OFFLINE);
    }
}


/************************************************************************
 * Return a name for a logical drive's organisation.
 */
static const char *
ciss_name_ldrive_org(int org)
{
    switch(org) {
    case CISS_LDRIVE_RAID0:
	return("RAID 0");
    case CISS_LDRIVE_RAID1:
	return("RAID 1(1+0)");
    case CISS_LDRIVE_RAID4:
	return("RAID 4");
    case CISS_LDRIVE_RAID5:
	return("RAID 5");
    case CISS_LDRIVE_RAID51:
	return("RAID 5+1");
    case CISS_LDRIVE_RAIDADG:
	return("RAID ADG");
    }
    return("unknown");
}

/************************************************************************
 * Return a name for a command status value.
 */
static const char *
ciss_name_command_status(int status)
{
    switch(status) {
    case CISS_CMD_STATUS_SUCCESS:
	return("success");
    case CISS_CMD_STATUS_TARGET_STATUS:
	return("target status");
    case CISS_CMD_STATUS_DATA_UNDERRUN:
	return("data underrun");
    case CISS_CMD_STATUS_DATA_OVERRUN:
	return("data overrun");
    case CISS_CMD_STATUS_INVALID_COMMAND:
	return("invalid command");
    case CISS_CMD_STATUS_PROTOCOL_ERROR:
	return("protocol error");
    case CISS_CMD_STATUS_HARDWARE_ERROR:
	return("hardware error");
    case CISS_CMD_STATUS_CONNECTION_LOST:
	return("connection lost");
    case CISS_CMD_STATUS_ABORTED:
	return("aborted");
    case CISS_CMD_STATUS_ABORT_FAILED:
	return("abort failed");
    case CISS_CMD_STATUS_UNSOLICITED_ABORT:
	return("unsolicited abort");
    case CISS_CMD_STATUS_TIMEOUT:
	return("timeout");
    case CISS_CMD_STATUS_UNABORTABLE:
	return("unabortable");
    }
    return("unknown status");
}

/************************************************************************
 * Handle an open on the control device.
 */
static int
ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
{
    struct ciss_softc	*sc;

    debug_called(1);

    sc = (struct ciss_softc *)dev->si_drv1;

    /* we might want to veto if someone already has us open */

    mtx_lock(&sc->ciss_mtx);
    sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
    mtx_unlock(&sc->ciss_mtx);
    return(0);
}

/************************************************************************
 * Handle the last close on the control device.
 */
static int
ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
{
    struct ciss_softc	*sc;

    debug_called(1);

    sc = (struct ciss_softc *)dev->si_drv1;

    mtx_lock(&sc->ciss_mtx);
    sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
    mtx_unlock(&sc->ciss_mtx);
    return (0);
}

/********************************************************************************
 * Handle adapter-specific control operations.
 *
 * Note that the API here is compatible with the Linux driver, in order to
 * simplify the porting of Compaq's userland tools.
 */
static int
ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
{
    struct ciss_softc		*sc;
    IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
#ifdef __amd64__
    IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
    IOCTL_Command_struct	ioc_swab;
#endif
    int				error;

    debug_called(1);

    sc = (struct ciss_softc *)dev->si_drv1;
    error = 0;
    mtx_lock(&sc->ciss_mtx);

    switch(cmd) {
    case CCISS_GETQSTATS:
    {
	union ciss_statrequest *cr = (union ciss_statrequest *)addr;

	switch (cr->cs_item) {
	case CISSQ_FREE:
	case CISSQ_NOTIFY:
	    bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
		sizeof(struct ciss_qstat));
	    break;
	default:
	    error = ENOIOCTL;
	    break;
	}

	break;
    }

    case CCISS_GETPCIINFO:
    {
	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;

	pis->bus = pci_get_bus(sc->ciss_dev);
	pis->dev_fn = pci_get_slot(sc->ciss_dev);
        pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
                pci_get_subdevice(sc->ciss_dev);

	break;
    }

    case CCISS_GETINTINFO:
    {
	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;

	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
	cis->count = sc->ciss_cfg->interrupt_coalesce_count;

	break;
    }

    case CCISS_SETINTINFO:
    {
	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;

	if ((cis->delay == 0) && (cis->count == 0)) {
	    error = EINVAL;
	    break;
	}

	/*
	 * XXX apparently this is only safe if the controller is idle,
	 *     we should suspend it before doing this.
	 */
	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
	sc->ciss_cfg->interrupt_coalesce_count = cis->count;

	if (ciss_update_config(sc))
	    error = EIO;

	/* XXX resume the controller here */
	break;
    }

    case CCISS_GETNODENAME:
	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
	      sizeof(NodeName_type));
	break;

    case CCISS_SETNODENAME:
	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
	      sizeof(NodeName_type));
	if (ciss_update_config(sc))
	    error = EIO;
	break;

    case CCISS_GETHEARTBEAT:
	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
	break;

    case CCISS_GETBUSTYPES:
	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
	break;

    case CCISS_GETFIRMVER:
	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
	      sizeof(FirmwareVer_type));
	break;

    case CCISS_GETDRIVERVER:
	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
	break;

    case CCISS_REVALIDVOLS:
	/*
	 * This is a bit ugly; to do it "right" we really need
	 * to find any disks that have changed, kick CAM off them,
	 * then rescan only these disks.  It'd be nice if they
	 * a) told us which disk(s) they were going to play with,
	 * and b) which ones had arrived. 8(
	 */
	break;

#ifdef __amd64__
    case CCISS_PASSTHRU32:
	ioc_swab.LUN_info	= ioc32->LUN_info;
	ioc_swab.Request	= ioc32->Request;
	ioc_swab.error_info	= ioc32->error_info;
	ioc_swab.buf_size	= ioc32->buf_size;
	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
	ioc			= &ioc_swab;
	/* FALLTHROUGH */
#endif

    case CCISS_PASSTHRU:
	error = ciss_user_command(sc, ioc);
	break;

    default:
	debug(0, "unknown ioctl 0x%lx", cmd);

	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);

	error = ENOIOCTL;
	break;
    }

    mtx_unlock(&sc->ciss_mtx);
    return(error);
}