File: device.cpp

package info (click to toggle)
raidutils 0.0.6-20
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,468 kB
  • ctags: 22,178
  • sloc: cpp: 39,795; ansic: 22,774; sh: 9,964; makefile: 21
file content (5047 lines) | stat: -rw-r--r-- 136,351 bytes parent folder | download | duplicates (5)
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
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
/* Copyright (c) 1996-2004, Adaptec Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - 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.
 * - Neither the name of the Adaptec Corporation nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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. */

//File - DEVICE.CPP
//***************************************************************************
//
//Description:
//
//    This file contains the function definitions for the dptDevice_C
//class.
//
//Author:       Doug Anderson
//Date:         4/8/93
//
//Editors:
//
//Remarks:
//
//
//***************************************************************************

#include "allfiles.hpp"

#if defined (_DPT_MACINTOSH)
#include <SCSI.h>
#include "SCSITypes.h"
#endif

#if !defined _DPT_UNIX && !defined _DPT_NETWARE && !defined _DPT_DOS
extern "C" {
	uLONG osdTargetBusy(uLONG HbaNum, uLONG Channel, uLONG TargetId, uLONG LUN);
}
#else
	uLONG osdTargetBusy(uLONG HbaNum, uLONG Channel, uLONG TargetId, uLONG LUN);
#endif // _DPT_UNIX

const uINT	I2O_LAP_SIZE = 4096;


//Function - dptDevice_C::preDelete() - start
//===========================================================================
//
//Description:
//
//    This function is called prior to deleting this object from the
//engine.
//
//Parameters:
//
//Return Value:
//
//   0 = Take no action
//   1 = Remove from engine core and free from memory
//   2 = Remove from engine core but do not free from memory
//       (The object must be maintained at a higher level)
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uSHORT  dptDevice_C::preDelete()
{

	uSHORT       retVal = 1;

	if (isComponent())
		retVal = 0;
	else if (raidType!=0xffff) {
		dptDevice_C *comp_P = NULL;

		if (isReal()) {
			// Enter this device into the ex-real RAID list
			myMgr_P()->enterExRR(this);

#ifdef	_DPT_AIX
			if (getRAIDtype() == RAID_1) {
				uCHAR firstDrive = 1;

				// Flag the component devices for a partition zap (block zero)
				comp_P = (dptDevice_C *) compList.reset();
				while (comp_P!=NULL) {

					if (!firstDrive)
						// Flag the device for a partition table zap
						comp_P->setPartitionZap();

					firstDrive = 0;

					// Get the next component
					comp_P = (dptDevice_C *) compList.next();
				}
			} 
#endif

		} // if (isReal())

		// Flag the component devices for a partition zap (block zero)
		comp_P = (dptDevice_C *) compList.reset();
		while (comp_P!=NULL) {
			// Clear the manual JBOD configured flag
			comp_P->scsiFlags2 &= ~FLG_DEV_MANUAL_JBOD_CONFIGURED;

			// Get the next component
			comp_P = (dptDevice_C *) compList.next();
		}

		// Free this device's components
		freeComponents();
		// This device no longer needs its component list
		// (Don't want components freed when this device is deleted)
		compList.flush();
		retVal = 2;
	}

	return (retVal);
}
//dptDevice_C::preDelete() - end


//Function - dptDevice_C::launchCCB() - start
//===========================================================================
//
//Description:
//
//    This function is the starting point for sending a CCB to hardware.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::launchCCB(engCCB_C *ccb_P)
{

   DPT_RTN_T    retVal = MSG_RTN_FAILED | ERR_ARTIFICIAL_IO;

  // Only send the CCB if this device is real and
  // The device cannot be failed and send a non-interpret command
if (!isArtificial()) {
     // Target this device
   ccb_P->target(this);
     // Pass the CCB to this device's manager
	if ((retVal = myMgr_P()->passCCB(ccb_P)) == MSG_RTN_COMPLETED)
	// If the command completed with an error condition...
      if (!ccb_P->ok())
	 retVal = MSG_RTN_FAILED | ERR_SCSI_IO;
}

return (retVal);

}
//dptDevice_C::launchCCB() - end


//Function - dptDevice_C::initRealLogical() - start
//===========================================================================
//
//Description:
//
//    This function initializes a logical device utilizing a logical array
//page (Set RAID type, build component list...)
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::initRealLogical()
{

  // Indicate that this is a real device
status.flags |= FLG_STAT_REAL;

  // Get the page code used by this device's manager
uCHAR pageCode = (uCHAR) myMgr_P()->getLAPpage();
  // If this device's manager supports a logical array page
if (pageCode) {
	 // Get a CCB
	engCCB_C *ccb_P = getCCB();
	if (ccb_P!=NULL) {
		char	*buff_P = NULL;
		if (myHBA_P()->isI2O()) {
			buff_P = new char[I2O_LAP_SIZE];
			if (buff_P != NULL)
				ccb_P->setDataBuff(ptrToLong(buff_P), I2O_LAP_SIZE);
		}
		if (pageCode == LAP_NCR1)
			// Initialize the CCB to do a mode sense page
			ccb_P->modeSense6(pageCode);
		else
			// Initialize the CCB to do a mode sense page
			ccb_P->modeSense(pageCode);
		// Indicate that this is a RAID command
		ccb_P->setRAIDcmd();
		// Send the CCB to hardware
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			// Insure the proper mode page was returned
			if ((ccb_P->modeParam_P->getPageCode() & 0x3f)==pageCode)
				  // Initialize the device from the logical array page data
				myMgr_P()->initRL(this,ccb_P);
		} // end if (launchCCB==MSG_RTN_COMPLETED)

		if (buff_P != NULL)
			delete[] buff_P;

		// Free the CCB
		ccb_P->clrInUse();

	} // end if (ccb_P!=NULL)
} // end if (pageCode)

}
//dptDevice_C::initRealLogical() - end


//Function - dptDevice_C::raidLAPcmd() - start
//===========================================================================
//
//Description:
//
//    This function issues a logical array page command to hardware.
//
//Parameters:
//
//Return Value:
//
// 0 =
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
// 1. This command can only be issued by logical devices.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::raidLAPcmd(uCHAR action,uCHAR inFlags)
{

   DPT_RTN_T    retVal  = MSG_RTN_IGNORED;
   dptDevice_C *dev_P;

if (raidType!=RAID_NONE) {
	// If a redirectable command...
	if ((action == LAP_CMD_BUILD) || (action == LAP_CMD_REBUILD) ||
		(action == LAP_CMD_VERIFY) || (action == LAP_CMD_VERIFY_FIX) ||
		(action == LAP_CMD_VERIFY_ABORT) || (action == LAP_CMD_ABORT) ||
		(action == LAP_CMD_MODIFY)) {
		// If not the top-level array on an I2O board...
		if (myHBA_P()->isI2O() && (parent.dev_P != NULL)) {
			dev_P = (dptDevice_C *) parent.dev_P;
			// Pass the command to the parent device
			return dev_P->raidLAPcmd(action, inFlags);
		}
		// If a driver level array on a non-I2O board...
		else if (!myHBA_P()->isI2O() && (getLevel() == 0)) {
			DPT_RTN_T    tempRtn  = MSG_RTN_COMPLETED;
			retVal = tempRtn;
			// Pass the command to each component device
			dev_P = (dptDevice_C *) compList.reset();
			while (dev_P!=NULL) {
				tempRtn = dev_P->raidLAPcmd(action, inFlags);
				if (retVal == MSG_RTN_COMPLETED)
					retVal = tempRtn;
				dev_P = (dptDevice_C *) compList.next();
			}
			return retVal;
		}
	}
	// If a rebuild command...
	if (action == LAP_CMD_REBUILD) {
		// Ensure proper component size
		retVal = checkRebuild();
	}

	// If the HBA is operating in wolfpack cluster mode...
	if (myHBA_P()->isClusterMode()) {
		if (action == LAP_CMD_ADD) {
			// Attempt to SCSI reserve all component devices...
			 dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
			 while (comp_P) {
				 retVal = comp_P->reserveDevice();
				 if (retVal != MSG_RTN_COMPLETED) {
					 break;
				 }
				 comp_P = (dptDevice_C *) compList.next();
			 }
		}
		else {
			// Attempt to SCSI reserve this logical device
			retVal = reserveDevice();
		}
		if (retVal == MSG_RTN_COMPLETED) {
			retVal = MSG_RTN_IGNORED;
		}
		else {
			retVal = ERR_RESERVATION_CONFLICT;
		}
	}

	// If no previous failures...
	if (retVal == MSG_RTN_IGNORED) {
		retVal = MSG_RTN_FAILED | ERR_GET_CCB;
		 // Get a CCB
		engCCB_C *ccb_P = getCCB();
		if (ccb_P!=NULL) {

			char	*buff_P = NULL;
			//If an I2O HBA...
			if (myHBA_P()->isI2O()) {
				buff_P = new char[I2O_LAP_SIZE];
				if (buff_P != NULL)
					ccb_P->setDataBuff(ptrToLong(buff_P), I2O_LAP_SIZE);
			}

			if ((getLevel() >= 2) && (action == LAP_CMD_BUILD)) {
				// If this device's HBA supports the "interpret format"...
				if (myHBA_P()->isInterpretFormat())
					retVal = doFormat(ccb_P,NULL,0x02,1);
				else
					retVal = doFormat(ccb_P,NULL,0x00,0);
				if (retVal == MSG_RTN_STARTED)
					retVal = MSG_RTN_COMPLETED;
			}
			// Build a logical array page select command
			else if (myMgr_P()->buildLAPselect(ccb_P,this,action,inFlags)) {
				// If deleting an array...
				if (action == LAP_CMD_DELETE) {
					  // Get the latest status...
					updateLAPstatus();
	//
	// *** Always do an action abort in case there is a diagnostic in
	// *** progress on a component device
	//
					// Send an abort action command
					ncrLAP1_S *mode_P = (ncrLAP1_S *) ccb_P->modeParam_P->getData();
					mode_P->setStatus(LAP_CMD_ABORT);
					launchCCB(ccb_P);
					// Wait for the abort to complete
					time_t   startTime,curTime;
					time(&startTime);
					do {
						// Get the latest status
						updateLAPstatus();
						time(&curTime);
					// While not timed out and still performing the action...
					} while (((curTime - startTime) <= 15) &&
							((status.main == LAPM_REBUILD) ||
							 (status.main == LAPM_BUILD) ||
							 (status.main == LAPM_VERIFY) ||
							 (status.main == SMAIN_FW_DIAGNOSTIC)));
					// Send the original delete command
					mode_P->setStatus(LAP_CMD_DELETE);
				}
				// Send the CCB to hardware
				retVal = launchCCB(ccb_P);
			}
			else
				retVal = MSG_RTN_IGNORED;

			if (buff_P != NULL)
				delete[] buff_P;

			// Free the CCB
			ccb_P->clrInUse();

		} // end if (ccb_P!=NULL)
	} // (retVal == MSG_RTN_IGNORED)
} // end if (raidType != RAID_NONE)

return (retVal);

}
//dptDevice_C::raidLAPcmd() - end


//Function - dptDevice_C::raidPAPcmd() - start
//===========================================================================
//
//Description:
//
//    This function issues a physical array page command to hardware.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::raidPAPcmd(uCHAR action)
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

if (isReal()) {
	if (isLogical() || !myMgr_P()->isRAIDcapable()) {
		// If two manager's above is valid...
		if (myMgr_P()->myMgr_P()!=NULL)
			// Set the PAP using the second manager up the chain
			retVal = myMgr_P()->myMgr_P()->sendPAPcmd(this,action);
	}
	// If this is a physical HBA device...
	else {
		// Set the PAP using this device's manager
		retVal = myMgr_P()->sendPAPcmd(this,action);
	}

	// If this is a RAID component...
	if (parent.dev_P != NULL) {
		if (parent.dev_P->getRAIDtype() == RAID_HOT_SPARE) {
			((dptDevice_C *)parent.dev_P)->updateStatus(1);
			// Adjust the Hot-Spare coverage
			myHBA_P()->updateHSprotection();
		}
		else
			((dptDevice_C *)parent.dev_P)->updateStatus();
	}
}

return (retVal);

}
//dptDevice_C::raidPAPcmd() - end


//Function - dptDevice_C::realInit() - start
//===========================================================================
//
//Description:
//
//    This function initializes a real device.
//
//---------------------------------------------------------------------------

void    dptDevice_C::realInit()
{

  // Determine if the device is ready
checkIfReady();

  // Get the SCSI inquiry information
engType = selfInquiry();
  // Fix to make a segmented array with no LUN 0 look like a DASD device
if (isLogical() && (engType == 0x1f)) {
	engType = 0;
}

  // If the device is a physical device...
  // (Logical devices get their capacity from the RAID data
if (!isLogical())
     // Get the device's capacity
   getCapacity();

  // Get the device's DPT name
getDPTname();

  // Get the device's transfer speed
getXfrSpeed();

  // Get the dual loop status
getDualLoopInfo();

  // Determine the drive's SMART status
updateSmartStatus();

// Note: Emulation is checked after a logical device scan

  // Check for a reserve block and initialize dependent data
initReserveBlockInfo();

  // Set this device's diagnostic status
updateDiagStatus();

}
//dptDevice_C::realInit() - end


//Function - dptDevice_C::checkForEmulation() - start
//===========================================================================
//
//Description:
//
//    This function determines if this drive is being emulated.
//
//Parameters:
//
//Return Value:
//
//   1 = If there are no emulated drives associated with this HBA or
//       the emulated mode command is not supported by the HBA
//   0 = Otherwise
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uSHORT  dptDevice_C::checkForEmulation()
{

    dptAddr_S           emulAddr;
    dptEmulation_S      *emul_P;
    uSHORT              retVal = 0;
    uCHAR               drive0cmos=1,drive1cmos=1;

if ( (getObjType()==DPT_SCSI_DASD) &&
     ((getLevel()==1) || (getLevel()==2)) ) {
     // Get a CCB
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
	// Get the system information
      osdGetSysInfo((sysInfo_S *)ccb_P->defData);
	// Get the CMOS drive entries
      drive0cmos = ccb_P->defData[0];
      drive1cmos = ccb_P->defData[1];
	// Initialize the CCB to do an emulated drive mode sense
	// Limit data buffer to 0x60 bytes
      ccb_P->modeSense(0x3d,0,0,0x60);
	// HBA should interpret this command
      ccb_P->setInterpret();
	// Send the CCB to hardware
      DPT_RTN_T rtnStatus = launchCCB(ccb_P);
      if (rtnStatus == (MSG_RTN_FAILED | ERR_SCSI_IO))
	   // Only allow to fail 1 time
	 retVal = 1;
      else if (rtnStatus == MSG_RTN_COMPLETED) {
	   // Default = drive not emulated
	 scsiFlags &= ~FLG_ENG_EMULATED;
	   // Cast the return data as emulation data
	 emul_P = (dptEmulation_S *) ccb_P->modeParam_P->getData();
		// Reverse multi-byte data
	 emul_P->swapCylinders();
//          reverseBytes(emul_P->driveType);
	   // Set HBA to this device's HBA
	 emulAddr.hba = getHBA();
	   // Set to ID other than this device's SCSI ID
	 emulAddr.id = getID() + 1;
	 if ((emul_P->getStatus() & 0x03)==0)
	      // Indicate that there are no emulated drives
	    retVal = 1;
	 else {
	      // If this drive is emulated drive C: ...
	    if (emul_P->getStatus() & 0x01) {
	       scsiFlags        &= ~FLG_ENG_EMU_01;
	       emulAddr.chan    = emul_P->getChanID0() >> 5;
	       emulAddr.id      = emul_P->getChanID0() & 0x1f;
	       emulAddr.lun     = emul_P->getLun0();
		 // If this drive is being emulated...
	       if ((emulAddr==getAddr()) && (drive0cmos!=0) && (emul_P->getCylinders()!=0))
		    // Indicate that this drive is being emulated
		  scsiFlags |= FLG_ENG_EMULATED;
	    }
	      // If this drive is emulated drive D: ...
	    if ((emul_P->getStatus() & 0x2) && !isEmulated()) {
	       scsiFlags        |= FLG_ENG_EMU_01;
	       emulAddr.chan    = emul_P->getChanID1() >> 5;
	       emulAddr.id      = emul_P->getChanID1() & 0x1f;
	       emulAddr.lun     = emul_P->getLun1();
		 // If this drive is being emulated...
	       if ((emulAddr==getAddr()) && (drive1cmos!=0) && (emul_P->getCylinders()!=0))
		    // Indicate that this drive is being emulated
		  scsiFlags |= FLG_ENG_EMULATED;
	    }
	      // If this drive is being emulated...
	    if (isEmulated()) {
	       emulation.cylinders      = emul_P->getCylinders();
	       emulation.heads  = emul_P->getHeads();
	       emulation.sectors        = emul_P->getSectors();
	       emulation.driveType      = emul_P->getDriveType();
	    }
	 } // end if (emul_P->status!=0)
      } // end if (status==MSG_RTN_COMPLETED)

	// Free the CCB
      ccb_P->clrInUse();
	} // end if (ccb_P!=NULL)
} // end if (getObjType==DPT_SCSI_DASD...)

return (retVal);

}
//dptDevice_C::checkForEmulation() - end


//Function - dptDevice_C::setEmulation() - start
//===========================================================================
//
//Description:
//
//    This function makes this device an emulated drive or removes
//the emulation parameters from this drive.  If the specified # of
//cylinders is zero, this function removes this drive from being an
//emulated drive.  If the specified # of cylinders is not zero this
//function makes this drive an emulated drive.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::setEmulation(dptBuffer_S *toEng_P)
{

   DPT_RTN_T            retVal = MSG_RTN_IGNORED;
   uSHORT               driveNum;
   engCCB_C             *ccb_P;
   dptEmulation_S       *emul_P;

  // If this device can be emulated or is alredy emulated...
if (canEmulate() || isEmulated()) {
   retVal = MSG_RTN_DATA_UNDERFLOW;
     // Read the drive # (C: or D:)
   toEng_P->extract(driveNum);
     // Read the emulation parameters
	if (toEng_P->extract(&emulation,dptEmuParam_size)) {
      retVal = MSG_RTN_FAILED | ERR_GET_CCB;
      ccb_P = getCCB();
      if (ccb_P!=NULL) {
	   // Zero the data buffer
	 ccb_P->clrData();
	   // The HBA should interpret this command
	 ccb_P->setInterpret();
	   // Initialize the CCB to do an emulation mode select
	 ccb_P->modeSelect(0x3d,0x10+2);
	 emul_P = (dptEmulation_S *) ccb_P->modeParam_P->getData();
	   // Set the emulation parameters
	 emul_P->setCylinders(emulation.cylinders);
	 emul_P->setHeads(emulation.heads);
	 emul_P->setSectors(emulation.sectors);
	 emul_P->setDriveType(emulation.driveType);
	   // If this drive is to be emulated...
	 if (emulation.cylinders!=0)
	    emul_P->setStatus(emul_P->getStatus() | 0x10);
	   // If emulation is to be removed...
	 else if (scsiFlags & FLG_ENG_EMU_01)
	    driveNum = 1;
	 else
	    driveNum = 0;

	   // If drive C: ...
	 if (driveNum==0)
	    emul_P->setStatus(emul_P->getStatus() | 0x01);
	 else
	    emul_P->setStatus(emul_P->getStatus() | 0x02);

	   // Reverse multi-byte data
	 emul_P->swapCylinders();
	   // Send the CCB to hardware
	 retVal = launchCCB(ccb_P);

	    // Free the CCB
	 ccb_P->clrInUse();

	   // Set the emulation status
	 checkForEmulation();
      }
   }
}

return (retVal);

}
//dptDevice_C::setEmulation() - end


//Function - dptDevice_C::canEmulate() - start
//===========================================================================
//
//Description:
//
//    This function determines if this device can be an emulated drive.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uSHORT  dptDevice_C::canEmulate()
{

uSHORT  retVal = 0;

  // Determine if this device is capable of being emulated
if ( !isComponent() && ((getLevel()==1) || (getLevel()==2)) &&
     (getObjType()==DPT_SCSI_DASD) )
     // Determine if the HBA can emulate a WD1003
   if ( (myHBA_P()->getIRQnum()==14) && (myHBA_P()->isEdge()) &&
	myHBA_P()->isPrimary() )
      retVal = 1;

return (retVal);

}
//dptDevice_C::canEmulate() - end


//Function - dptDevice_C::checkIfCompsReady() - start
//===========================================================================
//Description:
//    This function issues a test unit ready command to all underlying
//physical components and sets the logical device's ready status
//based on the component statuses.
//---------------------------------------------------------------------------

void    dptDevice_C::checkIfCompsReady()
{
	clrResConflict(); // clear the reservation conflict bit
	status.flags &= ~FLG_STAT_READY;  // clear the ready bit
	scsiFlags3 &= ~FLG_DEV_SPUN_DOWN; // clear the spun down bit

	dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
	// If no components...
	if (!comp_P) {
		checkIfReady(); // issue an actual test unit ready command
	}
	while (comp_P) {
		comp_P->checkIfCompsReady();
		if (comp_P->isResConflict()) {
			setResConflict(); // set the reservation conflict bit
		}
		if (comp_P->isReady()) {
			status.flags |= FLG_STAT_READY;  // set the ready bit
		}
		if (comp_P->scsiFlags3 & FLG_DEV_SPUN_DOWN) {
			scsiFlags3 |= FLG_DEV_SPUN_DOWN; // set the spun bit
		}

		comp_P = (dptDevice_C *) compList.next();
	}

}
//dptDevice_C::checkIfCompsReady() - end


//Function - dptDevice_C::checkIfReady() - start
//===========================================================================
//
//Description:
//
//    This function issues test unit ready command to determine if the
//device is ready.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::checkIfReady()
{

   uSHORT               repeatCnt       = 2;
   uSHORT               ready           = 0;

  // Get a CCB
engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
	clrResConflict(); // clear the reservation conflict flag
	// Initialize the CCB to do a test unit ready
	ccb_P->testUnitReady();
	while (!ready && repeatCnt--) {
		// Issue a Test Unit Ready command
		// (This will clear a unit attention)
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			ready = 1;
			scsiFlags3 &= ~FLG_DEV_SPUN_DOWN; // clear the spun down flag
		}
		// If the drive is spun down...
		else if ((ccb_P->scsiStatus == 2) && (ccb_P->defReqSense[12] == 0x04) && (ccb_P->defReqSense[13] = 0x02)) {
			scsiFlags3 |= FLG_DEV_SPUN_DOWN; // set the spun down flag
		}
		// If a reservation conflict...
		else if (ccb_P->scsiStatus == 0x18) {
			setResConflict(); // set the reservation conflict flag
		}
	}
	// Free the CCB
	ccb_P->clrInUse();

	if (ready) {
		// Set the ready bit
		status.flags |= FLG_STAT_READY;
	}
	else {
		// Clear the ready bit
		status.flags &= ~FLG_STAT_READY;
	}
} // end if (ccb_P!=NULL)


}
//dptDevice_C::checkIfReady() - end


//Function - dptDevice_C::resetSCSI() - start
//===========================================================================
//
//Description:
//
//    This function issues test unit ready and sets the EATA SCSI reset
//bit to reset the SCSI bus.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::resetSCSI()
{

   DPT_RTN_T    retVal = MSG_RTN_FAILED | ERR_GET_CCB;

  // Get a CCB
engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
     // Initialize the CCB to do a test unit ready
   ccb_P->testUnitReady();
     // Set the SCSI reset bit
   ccb_P->eataCP.flags |= CP_SCSI_RESET;
	// Issue a Test Unit Ready command w/ SCSI reset
   retVal = launchCCB(ccb_P);

     // Free the CCB
   ccb_P->clrInUse();
} // end if (ccb_P!=NULL)

return (retVal);

}
//dptDevice_C::resetSCSI() - end


//Function - dptDevice_C::getCapacity() - start
//===========================================================================
//
//Description:
//
//    This function attempts to get the capacity of a device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::getCapacity()
{

   sdRdCapacity_S       *cap_P;

  // Initialize the capacity to zero
capacity.maxPhysLBA     = 0;
capacity.maxLBA         = 0;
capacity.blockSize      = 0;
clrBlkSzNoChange();

  // Get a CCB
engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
	if ((getObjType()==DPT_SCSI_DASD) || (getObjType()==DPT_SCSI_CD_ROM) ||
		(getObjType()==DPT_SCSI_WORM) || (getObjType()==DPT_SCSI_OPTICAL)) {
		// Initialize the CCB to do a read capacity
		ccb_P->readCapacity();
		// Issue the read capacity command
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			cap_P = (sdRdCapacity_S *) ccb_P->defData;
			// Get into proper format
			cap_P->swapMaxLBA();
			cap_P->swapBlockSize();
			// Set the maximum physical capacity
			capacity.maxPhysLBA    = cap_P->getMaxLBA();
			// Set the maximum logical block address (LBA)
			capacity.maxLBA        = cap_P->getMaxLBA();
			if (getLevel()==2) {
				capacity.maxLBA  = getLastUserBlk();
			}
			// Set the block size
			capacity.blockSize     = cap_P->getBlockSize();
			phyBlockSize           = (uSHORT) cap_P->getBlockSize();
		}
		else {
			// Try to determine if the device is formatting
			setFmtStatus(getRequestSense(ccb_P));
		}
	} // end if (devType==DASD)

	// If an HBA physical DASD device...
	if ((getObjType()==DPT_SCSI_DASD) && (getLevel()==2)) {
		// Indicates that this device cannot change its block size
		setBlkSzNoChange();
		ccb_P->reInit();
		// Get the format device page
		ccb_P->modeSense6(0x03);
		if (launchCCB(ccb_P) == MSG_RTN_COMPLETED) {
			// Set the physical block size
			phyBlockSize = *(uSHORT *)(ccb_P->modeParam_P->getData()+10);
			reverseBytes(phyBlockSize);
			// If the format parameters are savable...
			if (ccb_P->modeParam_P->getPageCode() & 0x80)
				// Indicate that the block size may be changed
				clrBlkSzNoChange();
		}
	}
	// Free the CCB
	ccb_P->clrInUse();

} // end if (ccb_P!=NULL)

  // Determine if this device is protected by DPT's ECC algorithm
if ((phyBlockSize == 528) && (capacity.blockSize == 512) && (myHBA_P()->isECCenabled()))
	setECCprotected();
else
	clrECCprotected();

}
//dptDevice_C::getCapacity() - end


//Function - dptDevice_C::getXfrSpeed() - start
//===========================================================================
//
//Description:
//
//    This function determines the negotiated SCSI transfer speed for
//this device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::getXfrSpeed()
{

if (getLevel()==2) {
     // Get a CCB
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {


	// perform 2 test unit readies so that the FW negotiates
	// width and synch. These values are dynamic in the FW
	for (int x = 0; x < 2; x++) {
		ccb_P->target(this);
		ccb_P->testUnitReady();
		launchCCB(ccb_P);
		ccb_P->reInit();
	}

	// Initialize the CCB to do a log sense page 0x33
	// Limit data buffer to 0xff bytes
	ccb_P->logSense(0x33,0,0xff);
	// Set the interpret bit
	ccb_P->setInterpret();
	// Send the CCB to hardware
	if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
		ccb_P->initLogSense();
		// Find parameter #5
		if (ccb_P->log.find(0x5)!=NULL) {

			// Get the SCSI offset
			scsiOffset = ccb_P->log.data_P()[0];
			// Get the SCSI chip clock speed
			xfrSpeed = ccb_P->log.data_P()[2];
			xfrSpeed <<= 8;
			xfrSpeed |= ccb_P->log.data_P()[3];

			// If synchronous, get the negotiated xfr speed
			if (scsiOffset && ccb_P->log.data_P()[1])
				xfrSpeed /= ccb_P->log.data_P()[1];

			// If wide transfers were negotiated...
			if (ccb_P->log.flags() & 0x40)
				// Double the negotiated transfer speed
				xfrSpeed <<= 1;
		}

	} // end if (launchCCB()==MSG_RTN_COMPLETED)

	if (myHBA_P()->isUDMA()) {
		udmaModeSupported = 0;
		udmaModeSelected = 0;
		ccb_P->reInit();
		// Initialize the CCB to do a log sense page 0x39
		// Limit data buffer to 0xff bytes
		ccb_P->logSense(0x39,0,0xff);
		// Set the interpret bit
		ccb_P->setInterpret();
		// Send the CCB to hardware
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			ccb_P->initLogSense();
			// Find parameter 0x01
			if (ccb_P->log.find(0x01)!=NULL) {
				uCHAR tempMode = ccb_P->log.data_P()[180];
				tempMode >>= 1;
				while (tempMode) {
					udmaModeSupported++;
					tempMode >>= 1;
				}
				tempMode = ccb_P->log.data_P()[181];
				tempMode >>= 1;
				while (tempMode) {
					udmaModeSelected++;
					tempMode >>= 1;
				}
			}
		}
	}

	// Free the CCB
      ccb_P->clrInUse();
   } // end if (ccb_!=NULL)
}

}
//dptDevice_C::getXfrSpeed() - end


//Function - dptDevice_C::getDualLoopInfo() - start
//===========================================================================
//Description:
//    This function gets the dual loop status of the device.
//---------------------------------------------------------------------------

void    dptDevice_C::getDualLoopInfo()
{

if (getLevel()==2) {
	// Get a CCB
	engCCB_C *ccb_P = getCCB();
	if (ccb_P!=NULL) {

		// Initialize the CCB to do a log sense page 0x33
		// Limit data buffer to 0xff bytes
		ccb_P->logSense(0x33,0,0xff);
		// Set the interpret bit
		ccb_P->setInterpret();
		// Send the CCB to hardware
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			ccb_P->initLogSense();
			// Find parameter 0x11
			if (ccb_P->log.find(0x11)!=NULL) {
				scsiFlags3 &= ~0xff;
				scsiFlags3 |= FLG_DEV_SPEED_VALID;
				scsiFlags3 |= ccb_P->log.data_P()[0];
				p2Flags = ccb_P->log.data_P()[3];
				uLONG tempSpeed = getU4(ccb_P->log.data_P(), 4);
				#ifndef      _DPT_BIG_ENDIAN
					osdSwap4(&tempSpeed);
				#endif
				busSpeed = (uSHORT)(tempSpeed / 1000000L);
			}
		} // end if (launchCCB()==MSG_RTN_COMPLETED)

	// Free the CCB
	ccb_P->clrInUse();
	} // end if (ccb_!=NULL)
}

}
//dptDevice_C::getDualLoopInfo() - end


//Function - dptDevice_C::checkForPartition() - start
//===========================================================================
//
//Description:
//
//    This function checks for a valid partition table on this drive.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::checkForPartition()
{

    uLONG               lastBlock = 0;
    uSHORT              i = 0;

  // Clear the partition table flags
scsiFlags &= ~FLG_ENG_PTABLE;
scsiFlags &= ~FLG_ENG_PTBL_OVERRUN;
  // Clear the last partition block used
lastPartitionBlk = 0;

  // If this is a drive...
if ((getObjType()==DPT_SCSI_DASD) && !isFailed()) {
     // Get a CCB
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
	// Initialize the CCB to perform a read operation of sector 0
		ccb_P->read(0L,1,512,ptrToLong(ccb_P->dataBuff_P));
	#if defined (_DPT_MACINTOSH)
		if(launchCCB(ccb_P)==MSG_RTN_COMPLETED)
		{
			Block0 *blk0 = (Block0 *) ccb_P->dataBuff_P;
			// make sure block 0 is valid
			if(blk0->sbSig == sbSIGWord)
			{
				// Initialize the CCB to perform a read operation of sector 1
			ccb_P->read(1L,1,512,ptrToLong(ccb_P->dataBuff_P));
			// Read the first partition map block
				if(launchCCB(ccb_P)==MSG_RTN_COMPLETED)
				{
					Partition *prtn = (Partition *)ccb_P->dataBuff_P;
					// remember how many partition block there are
					unsigned long pmMapBlkCnt = prtn->pmMapBlkCnt;
					for(unsigned long i = 0; i < pmMapBlkCnt; i++)
					{
						// read a partition map entry
						ccb_P->read((unsigned long)i+1,1,512,ptrToLong(ccb_P->dataBuff_P));
						if(launchCCB(ccb_P)==MSG_RTN_COMPLETED)
						{
							prtn = (Partition *)ccb_P->dataBuff_P;
							// make sure the partition block is valid
							if(prtn->pmSig == pMapSIG || prtn->pmSig == pdSigWord)
							{
								// Indicate that a valid partition table was found
								scsiFlags |= FLG_ENG_PTABLE;

								// figure out where the partition ends
								lastBlock = prtn->pmPyPartStart + prtn->pmPartBlkCnt;
								if(lastBlock > lastPartitionBlk)
								{
								lastPartitionBlk = lastBlock;
								if (lastPartitionBlk > getMaxPhyLBA())
								{
										lastPartitionBlk = getMaxPhyLBA();
										scsiFlags |= FLG_ENG_PTBL_OVERRUN;
								}
								}

							}
						}
					}
				}
			}
		 }
	#else
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			// If a standard FDISK partition
			if ((ccb_P->dataBuff_P[510] == 0x55) && (ccb_P->dataBuff_P[511] == 0xaa)) {
				partition_S *part_P = NULL;
				sectorZero_S *zero_P = (sectorZero_S *) ccb_P->dataBuff_P;
				// Indicate that a valid partition table was found
				scsiFlags |= FLG_ENG_PTABLE;
				// Check all four partition structures for
				for (i=0;i<=3;i++) {
					part_P = &zero_P->partition[i];
					// If this is a valid partition...
					if (part_P->opSys && part_P->numSectors) {
						// Calculate the last block in the partition
						lastBlock = part_P->precedeSectors + part_P->numSectors - 1;
						if (lastBlock > lastPartitionBlk) {
							lastPartitionBlk = lastBlock;
							if (lastPartitionBlk > getMaxPhyLBA()) {
								lastPartitionBlk = getMaxPhyLBA();
								scsiFlags |= FLG_ENG_PTBL_OVERRUN;
							}
						}
					}
				}
			}
			// If a Solaris partition was detected
			else if ((ccb_P->dataBuff_P[508] == 0xda) && (ccb_P->dataBuff_P[509] == 0xbe)) {
				// Indicate that a valid partition table was found
				scsiFlags |= FLG_ENG_PTABLE;
				// Indicate that a Solaris partition was found
				setSolarisPartition();
				// Assume we can only grab RESERVED_SPACE_SUN blocks
				//lastPartitionBlk = getMaxPhyLBA() - RESERVED_SPACE_SOLARIS;
			}
		}
#endif
	// Free the CCB
      ccb_P->clrInUse();
   }
}

}
//dptDevice_C::checkForPartition() - end


/*
//Function - dptDevice_C::dsplyPartInfo() - start
//===========================================================================
//
//Description:
//
//    This function displays partition table information.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::dsplyPartInfo(sectorZero_S *zero_P)
{

extern    void  cout1024(float,int=0);

    partition_S         *part_P;
    uSHORT              i;
    float               floatCap;

  // Display drive information
cout << "Partition table detected: ";
  // Display the vendor ID
cout << setw(8) << descr.vendorID;
cout << setw(16) << descr.productID;
cout << ", HBA=" << (uSHORT)getHBA();
cout << ", Chan=" << (uSHORT)getChan();
cout << ", ID=" << dec << (uSHORT) getID();
cout << ", LUN=" << dec << (uSHORT) getLUN();
cout << endl;

  // Display the capacity
cout << "Disk Capacity: ";
cout.setf(ios::right,ios::adjustfield);
cout.width(5);
if (capacity.maxLBA!=0) {
   floatCap = ((float)capacity.maxLBA+1) * capacity.blockSize;
   cout1024(floatCap);
   cout << ' ';
}
else
   cout << "N/" << setw(2) << "A ";

cout << ",   " << hex << setw(8) << capacity.maxLBA << "h blocks" << endl;

  // Indicate that a valid partition table was found
scsiFlags |= FLG_ENG_PTABLE;
  // Check all four partition structures for
for (i=0;i<=3;i++) {
   part_P = &zero_P->partition[i];
     // If this is a valid partition...
   if (part_P->opSys && part_P->numSectors) {
      cout << "   Partition #" << dec << i << ": OS = ";
      cout.width(14);
      cout.setf(ios::left,ios::adjustfield);
      switch (part_P->opSys) {
	 case 0x00: cout << "Unknown";          break;
	 case 0x01: cout << "DOS - 12 bit";     break;
	 case 0x04: cout << "DOS - 16 bit";     break;
	 case 0x05: cout << "DOS - Extend";     break;
	 case 0x06: cout << "DOS 5.0";          break;
	 case 0x63: cout << "UNIX";             break;
	 case 0x64: cout << "Novell";           break;
	 case 0x65: cout << "Novell";           break;
	 case 0x75: cout << "PCIX";             break;
	 case 0xdb: cout << "CP/M";             break;
	 case 0xff: cout << "BBT";              break;
	 default:   cout.setf(ios::right,ios::adjustfield);
		    cout.width(4);
		    cout << hex << (uSHORT) part_P->opSys << 'h';
		    cout << "   *** Record Code # and Operating System ***";
		    cout << "\a\a";
		    break;
      }
      cout << endl;

	// If the partition table includes the last LBA...
//      if (lastPartitionBlk>=capacity.maxPhysLBA)
	   // Indicate that the reserve block may be in danger
//       scsiFlags |= FLG_ENG_PTBL_OVERRUN;

      cout.setf(ios::right,ios::adjustfield);
      cout << "      Start Block   = " << setw(8) << hex << part_P->precedeSectors << "h,  (";
      floatCap = ((float)part_P->precedeSectors) * capacity.blockSize;
      cout1024(floatCap);
      cout << " bytes)" << endl;
      cout << "      End Block     = " << setw(8) << hex << part_P->precedeSectors + part_P->numSectors - 1 << "h,  (";
      floatCap = ((float)part_P->precedeSectors + part_P->numSectors - 1) * capacity.blockSize;
      cout1024(floatCap);
      cout << " bytes)" << endl;
      cout << "      Total Blocks  = " << setw(8) << hex << part_P->numSectors << "h,  (";
      floatCap = ((float)part_P->numSectors) * capacity.blockSize;
      cout1024(floatCap);
      cout << " bytes)";
      cout << "   <<< Press any key >>>";
      getch();
      cout << endl;
   }
}

cout << endl;

}
//dptDevice_C::dsplyPartInfo() - end
*/


//Function - dptDevice_C::readReserveBlock() - start
//===========================================================================
//
//Description:
//
//    This function reads the reserve block associated with this device.
//If a valid reserve block is found, a pointer to the CCB with the
//reserve block data is returned.  If a valid reserve block is not
//found a NULL pointer is returned.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//  1. This function assumes the device capacity has already been
//     obtained.
//
//  2. It is the responsibility of the calling routine to insure
//     that the CCB gets freed.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::readReserveBlock(engCCB_C *ccb_P)
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

  // If this is a non-removable FW physical DASD device...
if ((getObjType()==DPT_SCSI_DASD) && (getLevel()==2) && !isRemovable() &&
    (capacity.maxPhysLBA>0)) {
     // Initialize the CCB to perform a read operation
   ccb_P->read(capacity.maxPhysLBA,1,512,ptrToLong(ccb_P->dataBuff_P));
   retVal = launchCCB(ccb_P);
   if (retVal == MSG_RTN_COMPLETED) {
	// If the reserve block ID does not exist...
      if (getU4(ccb_P->dataBuff_P,0)!=FW_RB_INDICATOR)
	 retVal = MSG_RTN_FAILED | ERR_RESERVE_BLK_SIG;
   }
}

return (retVal);

}
//dptDevice_C::readReserveBlock() - end


//Function - dptDevice_C::initReserveBlockInfo() - start
//===========================================================================
//
//Description:
//
//    This function reads the DPT reserve block and initializes data
//structures dependent on reserve block information.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::initReserveBlockInfo()
{

if ((getObjType() == DPT_SCSI_DASD) && (getLevel() == 2)) {

     // Clear the valid reserve block flag and downloaded FW flag
   scsiFlags &= ~FLG_ENG_RESERVE_BLOCK & ~FLG_ENG_DFW;

     // Get a CCB
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
      if (readReserveBlock(ccb_P)==MSG_RTN_COMPLETED) {
	 scsiFlags |= FLG_ENG_RESERVE_BLOCK;
	   // Check for the downloaded firmware indicator
	 if ((ccb_P->dataBuff_P[0x2c]=='F') &&
	     (ccb_P->dataBuff_P[0x2d]=='W'))
	      // Indicate that there is downloaded FW on this drive
	    scsiFlags |= FLG_ENG_DFW;
	   // Check for a format failure
	 checkFmtFailure(ccb_P);
      }
	// Free the CCB
      ccb_P->clrInUse();
   }
}

}
//dptDevice_C::initReserveBlockInfo() - end


//Function - dptDevice_C::rtnSWreserveBlock() - start
//===========================================================================
//
//Description:
//
//    This function is the message handler to return the 128 byte
//software portion of the DPT reserve block.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::rtnSWreserveBlock(dptBuffer_S *fromEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_FAILED | ERR_GET_CCB;

  // Get a CCB
engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
     // Read the reserve block
   retVal = readReserveBlock(ccb_P);
   if (retVal == MSG_RTN_COMPLETED) {
      retVal = MSG_RTN_DATA_OVERFLOW;
	// Prepare the output buffer for new data
      fromEng_P->reset();
	// Return the software portion of the DPT reserve block
      if (fromEng_P->insert(ccb_P->dataBuff_P+384,128))
	 retVal = MSG_RTN_COMPLETED;
   }
     // Free the CCB
   ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::rtnSWreserveBlock() - end


//Function - dptDevice_C::writeSWreserveBlock() - start
//===========================================================================
//
//Description:
//
//    This function writes to the 128 byte SW portion of the DPT
//reserve block.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::writeSWreserveBlock(dptBuffer_S *toEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_FAILED | ERR_GET_CCB;

  // Read the DPT reserve block
engCCB_C *ccb_P = getCCB();
  // If a valid DPT reserve block was read...
if (ccb_P!=NULL) {
     // Read the reserve block
   retVal = readReserveBlock(ccb_P);
   if (retVal == MSG_RTN_COMPLETED) {
      retVal = MSG_RTN_DATA_UNDERFLOW;
	// Get the data to save to the SW portion of the reserve block
      if (toEng_P->extract(ccb_P->dataBuff_P+384,128)) {
	   // Reinitialize the CCB
	 ccb_P->reInit();
	   // Initialize the CCB to perform a write operation
	 ccb_P->write(capacity.maxPhysLBA,1,512,ptrToLong(ccb_P->dataBuff_P));
	   // Send the CCB to hardware
	 retVal = launchCCB(ccb_P);
      }
   }
     // Free the CCB
   ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::writeSWreserveBlock() - end


//Function - dptDevice_C::setLAPstatus() - start
//===========================================================================
//
//Description:
//
//    This function sets the logical array page status for this device.
//If this device is a software array, the status of all FW level arrays
//are checked and the worst FW array status is returned as the software
//array status.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uLONG   dptDevice_C::setLAPstatus(uCHAR inStatus,
				  uLONG *rtnErrCnt_P,
				  uLONG *rtnTotalBlks_P
				 )
{

	uLONG blksCompleted = 1;
	uLONG errCnt = 0;
	uLONG totalBlks = 1;


	DEBUG_BEGIN(1, dptDevice_C::setLAPstatus());

	if (isLogical() && (getRAIDtype()!=RAID_NONE)) {

		// Indicate that the main & sub status fields are LAP status
		status.flags &= ~FLG_STAT_PAP;
		status.flags |= FLG_STAT_LAP;
		// Set the device's status
		status.main = inStatus & 0xf;
		status.sub = inStatus >> 4;

	DEBUG(1, "LSU " << PRT_ADDR << "level" << (int)getLevel() << \
		 PRT_STAT << "rtype=" << getRAIDtype() );

		switch (status.main) {
			case 0:
				status.display  = DSPLY_STAT_OPTIMAL;
			break;

			case 2:
			case 3:
				status.display = DSPLY_STAT_BUILD;
			break;

			case 4:
				if (status.sub==LAPS_BUILD)
					status.display  = DSPLY_STAT_BUILD;
				else if (status.sub==LAPS_BUILD_REQUIRED)
					status.display  = DSPLY_STAT_WARNING;
				else
					status.display  = DSPLY_STAT_FAILED;
			break;

			case 11:
				status.display = DSPLY_STAT_BUILD;
			break;


			case SMAIN_FW_DIAGNOSTIC:
			case 10:
				status.display = DSPLY_STAT_IMPACTED;
			break;

			default:
				status.display = DSPLY_STAT_WARNING;
			break;
		}
		// If driver level array and not a dual level firmware array...
		if ((getLevel()==0) && (status.main!=4) && !myHBA_P()->isI2O()) {
			uLONG compBlksCompleted = 0;
			uLONG compErrCnt;
			uLONG compPercent,curPercent,compMaxLBA; uSHORT compPriority=6;
			// Get a component failure count
			uSHORT statPriority = 7;
			dptDevice_C *dev_P = (dptDevice_C *) compList.reset();

			while (dev_P!=NULL) {
				// If logical component...
				if (dev_P->isLogical()) {

					// check for FW diags
					dev_P->updateStatus();
					// if FW diags are not going
					if (dev_P->status.main != SMAIN_FW_DIAGNOSTIC) {
//						compPriority = 8;
						// Update the component's LAP status
						dev_P->updateLAPstatus(0,&compBlksCompleted,&compErrCnt);
					}

					switch (dev_P->status.main) {
						case 0:
							compPriority = 6;
						break; // Optimal
						case 1:
							compPriority = 2;
						break;
						// Degraded
						case 2:
							compPriority = 3;
						break;
						// Rebuilding
						case 4:
							compPriority = 0;
						break;
						// Dead
						case 5:
							compPriority = 5;
						break;
						// Warning
						case 10:
							compPriority = 4;
						break;
						// Verifying
						case 11:
							compPriority = 1;
						break;
						// Building
					}

					if (dev_P->isMissing()) {
						// Mark the SW array as failed
						status.main = 4;
						status.sub = 0;
						status.display = DSPLY_STAT_FAILED;
						statPriority = 0;
					} else if (compPriority < statPriority) {

						// do nothing if a component is doing diags
						if (dev_P->status.main != SMAIN_FW_DIAGNOSTIC) {

							statPriority = compPriority;
						// Use the component status
							status = dev_P->status;
							// Initialize the progress
							blksCompleted = compBlksCompleted;
							totalBlks = dev_P->getLastLBA();
							errCnt  = compErrCnt;
						}
					} else if (compPriority==statPriority) {
						compMaxLBA = dev_P->getLastLBA();
						// Prevent divide by zero
						if (compMaxLBA==0)
							compMaxLBA = 1;
						if (totalBlks==0)
							totalBlks = 1;
						// Compute the percentage complete
						// Note: This will not work for LSUs greater than 21G
						compPercent = (compBlksCompleted*100)/compMaxLBA;
						curPercent = (blksCompleted*100)/totalBlks;
						// If this device is the least complete...
						if (compPercent < curPercent) {
							// Use this device's progress
							blksCompleted = compBlksCompleted;
							totalBlks = compMaxLBA;
						}
						errCnt += compErrCnt;
					}

				} else {
					// Update the physical device status
					dev_P->updateStatus();
					// If a physical device is failed...
					if (dev_P->isFailed() || dev_P->isMissing()) {
						// Mark the SW array as failed
						status.main = 4;
						status.sub = 0;
						status.display = DSPLY_STAT_FAILED;
					}
				}
				dev_P = (dptDevice_C *) compList.next();
			}
		}

		// Question: Does it make sense to check physical device status here ?? // If firmware level array...
		if ((getLevel()==1) && (status.main!=4) && (status.main!=2)) {

			uSHORT PhysFailed = 0;
			uSHORT RaidType = getRAIDtype();

			dptDevice_C *dev_P = (dptDevice_C *) compList.reset();

			// look thru all components
			while (dev_P!=NULL) {
				// If logical component...
				if (dev_P->isPhysical()) {
					// Update the physical device status
					dev_P->updateStatus();
					// Note: updateDiagStatus() is called by updateStatus().

					DEBUG(1, "comp" << PRT_DADDR(dev_P) << "fail=" << \
						 (int)dev_P->isFailed() << PRT_DSTAT(dev_P) << \
						 "f_cnt=" << PhysFailed);

					  // If a physical device is failed && the array is optimal...
					if ((dev_P->isFailed() || dev_P->isMissing()) &&
						(status.main == 0)) {
					   dev_P->status.main = PAPM_FAILED;
					   if ((RaidType == RAID_0) || (PhysFailed)) {
						  // Mark the FW array as missing
						//status.display = DSPLY_STAT_MISSING;
						status.display = DSPLY_STAT_FAILED;
						status.main = LAPM_FAILED;
						status.sub = LAPS_MULTIPLE_DRIVES;
						status.flags |= FLG_STAT_ALARM_ON;
					   } else {
						  // Mark the FW array as impacted
						  //(firmware will degrade upon first write operation)
						status.display = DSPLY_STAT_IMPACTED;
						status.flags &= ~(FLG_STAT_LAP | FLG_STAT_PAP);
						status.main = 0;
						status.sub = 0;
					   }
					   PhysFailed++;
					}
#ifdef RAID_FIX
					if (RaidType == RAID_HOT_SPARE) {
					    if (dev_P->status.main == SMAIN_FW_DIAGNOSTIC) {
						dev_P->setUserDiagFlag();
						//dev_P->status.flags |= FLG_STAT_PAP;
						// status.main = dev_P->status.main;
						// status.sub = dev_P->status.sub;
					    } else
						dev_P->clrUserDiagFlag();
					    }
					if (dev_P->status.flags & FLG_STAT_DIAGNOSTICS) {
						dev_P->status.main == SMAIN_FW_DIAGNOSTIC;
						status.display = DSPLY_STAT_IMPACTED;
						status.flags &= ~(FLG_STAT_LAP | FLG_STAT_PAP);
					}
#endif
				}
				dev_P = (dptDevice_C *) compList.next();
			}
		}
	}

	if (rtnErrCnt_P!=NULL)
		*rtnErrCnt_P = errCnt;
	if (rtnTotalBlks_P!=NULL)
		*rtnTotalBlks_P = totalBlks;

	return (blksCompleted);

}
//dptDevice_C::setLAPstatus() - end


//Function - dptDevice_C::getPAPindex() - start
//===========================================================================
//Description:
//    This function returns an index into the physical array page
//corresponding to this device.
//---------------------------------------------------------------------------

uSHORT  dptDevice_C::getPAPindex(engCCB_C *ccb_P)
{

	uSHORT       index = 0xffff;

	if (myHBA_P()->isI2OVer2()) {
		if (ccb_P == NULL)
			index = 0;
		else {
			uCHAR	*data_P = ccb_P->modeParam_P->getData();
			uLONG	numBytes = getU4(data_P,0);
			#ifndef      _DPT_BIG_ENDIAN
				osdSwap4(&numBytes);
			#endif
			numBytes <<= 2;
			data_P += 4;
			uLONG	i;
			if (numBytes > 0xffff) //TEMP
				return index;
			for (i=0; i < numBytes; i += 4) {
				if ((getChan() == (data_P[i] >> 4)) &&
					(getLUN() == (data_P[i] & 0x0f)) &&
					(getID() == data_P[i + 1])) {
					index = (uSHORT) (4 + i + 2);
					break;
				}
			}
		}
	}
	else if (getLUN()==0) {

		if (myHBA_P()->isI2O()) {
			index = (getID() & 0x1f) | ((getID() & 0x60) << 1) | (getChan() << 5);
		}
		else {
			index = (getID() * 15) + (getChan()-myMgr_P()->getMinAddr().chan);
		}
	}

	return (index);

}
//dptDevice_C::getPAPindex() - end


//Function - dptDevice_C::setPAPstatus() - start
//===========================================================================
//
//Description:
//
//    This function sets the physical array page status for this device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::setPAPstatus(engCCB_C *ccb_P)
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;
   uCHAR        papStatus;

uSHORT index = getPAPindex(ccb_P);
if ((index!=0xffff) && (getRAIDtype()==RAID_NONE) && isReal()) {
	retVal = MSG_RTN_COMPLETED;
	// Indicate that the main & sub status fields are PAP status
	status.flags &= ~FLG_STAT_LAP;
	status.flags |= FLG_STAT_PAP;
	if ((status.main != PMAIN_STAT_FORMAT) &&
	(status.main != SMAIN_FW_DIAGNOSTIC) && !isHWmismatch()) {
		// Get the PAP status
		papStatus = ccb_P->modeParam_P->getData()[index];

		uCHAR buffer[512];
		memcpy(buffer, ccb_P->modeParam_P->getData(), 512);

		status.main       = papStatus & 0x0f;
		status.sub        = (papStatus & 0x70) >> 4;
		// Set display status based on physical array page status
		switch (status.main) {
			case PAPM_NON_EXISTENT:
				break;

			case PAPM_OPTIMAL:
			case 10:
			case PAPM_UNCONFIGURED:
				status.display  = DSPLY_STAT_OPTIMAL;
				break;

			case PAPM_FAILED:
			case PAPM_PARAMETER_MISMATCH:
				status.display  = DSPLY_STAT_FAILED;
				break;

			// If building or rebuilding...
			case PAPM_REPLACED:
				if ((status.sub==1) || (status.sub==2))
					status.display  = DSPLY_STAT_IMPACTED;
				else
					status.display = DSPLY_STAT_FAILED;
				break;

			case PAPM_VERIFY:
				status.display = DSPLY_STAT_IMPACTED;
				break;

			case PAPM_BUILD:
				if (status.sub == PAPS_BUILD_EXPAND)
					status.display = DSPLY_STAT_IMPACTED;
				else
					status.display = DSPLY_STAT_BUILD;
				break;

			default:
				status.display = DSPLY_STAT_WARNING;
				break;
		}
	}

	// If dual loop is active
	if (p2Flags & FLG_DEV_P2_VALID) {
		// If one of the dual loop channels is down (not active)
		if ((scsiFlags3 & (FLG_DEV_P2_ACTIVE | FLG_DEV_P1_ACTIVE)) != (FLG_DEV_P2_ACTIVE | FLG_DEV_P1_ACTIVE)) {
			if (status.display != DSPLY_STAT_FAILED)
				status.display = DSPLY_STAT_WARNING;
		}
	}

} // end if (index!=0xffff)

return(retVal);

}
//dptDevice_C::setPAPstatus() - end


/*DPT_RTN_T     dptDevice_C::updateLAPstatus(uSHORT setCapacity,
					     uLONG *blksComplete_P,
					     uLONG *errCnt_P,
					     uLONG *totalBlks_P
					    )
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;
   uLONG        progress;
   ncrLAP1_S    *mode_P;

  // Get the page code used by this device's manager
uCHAR pageCode = (uCHAR) myMgr_P()->getLAPpage();
  // If this device's manager supports a logical array page
if (pageCode && isReal() && isLogical()) {
   retVal = MSG_RTN_FAILED | ERR_GET_CCB;
     // Get a CCB
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
      if (pageCode == LAP_NCR1)
		// Initialize the CCB to do a mode sense page
	 ccb_P->modeSense6(pageCode);
      else
	   // Initialize the CCB to do a mode sense page
	 ccb_P->modeSense(pageCode);
	// Indicate that this is a RAID command
      ccb_P->setRAIDcmd();
	// Send the CCB to hardware
      retVal = launchCCB(ccb_P);
      if (retVal == MSG_RTN_COMPLETED) {
	   // Insure the proper mode page was returned
	 if ((ccb_P->modeParam_P->getPageCode() & 0x3f)==pageCode) {
	      // Cast the return data as NCR LAP data
	    mode_P = (ncrLAP1_S *) ccb_P->modeParam_P->getData();
	      // If SW level array...
	    if (getLevel()==0) {
		 // Set the LAP status - get array progress
	       progress = setLAPstatus(mode_P->getStatus(),errCnt_P,totalBlks_P);
	    }
	    else {
		 // Set the logical array page status
	       setLAPstatus(mode_P->getStatus());
		 // Get the RAID build/rebuild/verify progress
	       progress = mode_P->swapCompleted();
		 // If the verify error count is desired...
	       if (errCnt_P!=NULL)
		    // Return the verify error count
		  *errCnt_P = mode_P->swapVerifyErrCnt();
		 // If the total # of blocks is desired...
	       if (totalBlks_P!=NULL) {
		  *totalBlks_P = 0;
		  if ((pageCode == LAP_DPT1) && myHBA_P()->isECCsizeErr())
		       // Get the last ECC conversion block
		     *totalBlks_P = ((dptLAP1_S *)mode_P)->swapLastECCblk();
		    // If no ECC conversion block is available...
		  if (*totalBlks_P == 0)
		     *totalBlks_P = capacity.maxLBA;
	       }
	    }
	      // If the RAID build/rebuild/verify progress is desired...
	    if (blksComplete_P!=NULL)
	       *blksComplete_P = progress;
	      // If capacity should be updated...
	    if (setCapacity) {
		 // Update the capacity information
	       mode_P->swapNumBlocks();
	       mode_P->swapLsuBlockSize();
	       capacity.maxLBA = capacity.maxPhysLBA = mode_P->getNumBlocks() - 1;
	       if (getLevel()==2)
		  capacity.maxLBA = getLastUserBlk();
	       capacity.blockSize = mode_P->getLsuBlockSize();
	    }
	 }
      } // end if (retVal==MSG_RTN_COMPLETED)

	// Free the CCB
      ccb_P->clrInUse();

   } // end if (ccb_P!=NULL)
} // end if (pageCode)

return (retVal);

} */






//Function - dptDevice_C::updateLAPstatus() - start
//===========================================================================
//
//Description:
//
//    This function gets a logical array page.  This device's status
//and optionally its capacity information are updated.
//
//Parameters:
//
//Return Value:
//
//   The build/rebuild/verify progress.
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateLAPstatus(uSHORT setCapacity,
					     uLONG *blksComplete_P,
					     uLONG *errCnt_P,
					     uLONG *totalBlks_P
					    )
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

   DEBUG_BEGIN(5, dptDevice_C::updateLAPstatus());

  // Get the page code used by this device's manager
uCHAR pageCode = (uCHAR) myMgr_P()->getLAPpage();

#ifdef _SINIX_ADDON
if (pageCode == LAP_DPT1)
	// SNI Fix: Verify does not work with FW UX2. - michiz
	// We know, that FW UX2 uses LAP_DPT1, newer ones will use LAP_DPT2.
	scsiFlags2 |= FLG_DEV_NO_VERIFY;
#endif

/* Not supposed to redirect status requests
// If not the top-level array on an I2O board...
if (myHBA_P()->isI2O() && (parent.dev_P != NULL)) {
	dev_P = (dptDevice_C *) parent.dev_P;
	// Pass the command to the parent device
	return dev_P->updateLAPstatus(setCapacity, blksComplete_P, errCnt_P, totalBlks_P);
}
*/

  // If this device's manager supports a logical array page
if (pageCode && isReal() && isLogical()) {
	retVal = MSG_RTN_FAILED | ERR_GET_CCB;
	// Get a CCB
	engCCB_C *ccb_P = getCCB();
	if (ccb_P!=NULL) {
		if (pageCode == LAP_NCR1)
			// Initialize the CCB to do a mode sense page
			ccb_P->modeSense6(pageCode);
		else
			// Initialize the CCB to do a mode sense page
			ccb_P->modeSense(pageCode);

		// Indicate that this is a RAID command
		ccb_P->setRAIDcmd();
		// Send the CCB to hardware
		retVal = launchCCB(ccb_P);
		if (retVal == MSG_RTN_COMPLETED) {
			// Insure the proper mode page was returned
			if ((ccb_P->modeParam_P->getPageCode() & 0x3f)==pageCode) {

				// check to see what LAP page we got
				if ((pageCode == LAP_DPT1) || (pageCode == LAP_NCR1))
					retVal = updateLAP1status(ccb_P->modeParam_P->getData(), pageCode, setCapacity, blksComplete_P,
											  errCnt_P, totalBlks_P);
				else if (pageCode == LAP_DPT2)
					retVal = updateLAP2status(ccb_P->modeParam_P->getData(), setCapacity, blksComplete_P,
											  errCnt_P, totalBlks_P);
			}
		}

			// Free the CCB
		ccb_P->clrInUse();
	}
}

return (retVal);

}
//dptDevice_C::updateLAPstatus() - end


//Function - dptDevice_C::updateLAP1status() - start
//===========================================================================
//
//Description:
//
//    This function gets a logical array page (NCR or DPT 1).  This device's status
//and optionally its capacity information are updated.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateLAP1status(void *data_P,
					     uSHORT pageCode,
					     uSHORT setCapacity,
					     uLONG *blksComplete_P,
					     uLONG *errCnt_P,
					     uLONG *totalBlks_P
					    )
{


	DEBUG_BEGIN(5, dptDevice_C::updateLAP1status());

	DPT_RTN_T    retVal = MSG_RTN_COMPLETED;
	uLONG        progress;
	ncrLAP1_S    *mode_P;

	// Cast the return data as NCR LAP data
	mode_P = (ncrLAP1_S *) data_P;
	// If SW level array...
	if (getLevel()==0) {
		// Set the LAP status - get array progress
		progress = setLAPstatus(mode_P->getStatus(),errCnt_P,totalBlks_P);
	}
	else {
		// Set the logical array page status
		setLAPstatus(mode_P->getStatus());
		// Get the RAID build/rebuild/verify progress
		#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
			progress = mode_P->getCompleted();
		#else
			progress = mode_P->swapCompleted();
		#endif
		// If the verify error count is desired...
		if (errCnt_P!=NULL) {
			// Return the verify error count
			#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
				*errCnt_P = mode_P->getVerifyErrCnt();
			#else
				*errCnt_P = mode_P->swapVerifyErrCnt();
			#endif
		}
		// If the total # of blocks is desired...
		if (totalBlks_P!=NULL) {
			*totalBlks_P = 0;
			if ((pageCode == LAP_DPT1) && myHBA_P()->isECCsizeErr()) {
				// Get the last ECC conversion block
				#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
					*totalBlks_P = ((dptLAP1_S *)mode_P)->getLastECCblk();
				#else
					*totalBlks_P = ((dptLAP1_S *)mode_P)->swapLastECCblk();
				#endif
			}
			// If no ECC conversion block is available...
			if (*totalBlks_P == 0)
				*totalBlks_P = capacity.maxLBA;
		}
	}

	// If the RAID build/rebuild/verify progress is desired...
	if (blksComplete_P!=NULL)
		*blksComplete_P = progress;
	// If capacity should be updated...
	if (setCapacity) {
		// Update the capacity information
		#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
			mode_P->getNumBlocks();
			mode_P->getLsuBlockSize();
		#else
			mode_P->swapNumBlocks();
			mode_P->swapLsuBlockSize();
		#endif
		capacity.maxLBA = capacity.maxPhysLBA = mode_P->getNumBlocks() - 1;
		if (getLevel()==2)
			capacity.maxLBA = getLastUserBlk();

		capacity.blockSize = mode_P->getLsuBlockSize();
	}


	DEBUG(5, PRT_ADDR << "capacity=" << capacity.maxLBA << " blocks");

	return (retVal);

}
//dptDevice_C::updateLAP1status() - end


//Function - dptDevice_C::updateLAP2status() - start
//===========================================================================
//
//Description:
//
//    This function gets a logical array page (DPT 2).  This device's status
//and optionally its capacity information are updated.
//
//Parameters:
//
//Return Value:
//
//   The build/rebuild/verify progress.
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateLAP2status(void *data_P,
					     uSHORT setCapacity,
					     uLONG *blksComplete_P,
					     uLONG *errCnt_P,
					     uLONG *totalBlks_P
					    )
{


	DEBUG_BEGIN(5, dptDevice_C::updateLAP2status());

	DPT_RTN_T    retVal = MSG_RTN_COMPLETED;
	uLONG        progress;
	dptLAP2_S    *mode_P = (dptLAP2_S *)data_P;

	// If SW level array and not dual level I2O...
	if ((getLevel()==0) && !myHBA_P()->isI2O()) {
		// Set the LAP status - get array progress
		progress = setLAPstatus(mode_P->getStatus(),errCnt_P,totalBlks_P);
	}
	else {
		// Set the logical array page status
		setLAPstatus(mode_P->getStatus());
		// Get the RAID build/rebuild/verify progress
		#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
			progress = mode_P->getBlksComplete();
		#else
			progress = mode_P->swapBlksComplete();
		#endif
		// If the verify error count is desired...
		if (errCnt_P!=NULL) {
			// Return the verify error count
			#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
				*errCnt_P = mode_P->getVerifyErrors();
			#else
				*errCnt_P = mode_P->swapVerifyErrors();
			#endif
		}
		// If the total # of blocks is desired...
		if (totalBlks_P!=NULL)
			*totalBlks_P = capacity.maxLBA;
	}
	// If the RAID build/rebuild/verify progress is desired...
	if (blksComplete_P!=NULL)
		*blksComplete_P = progress;
	// If capacity should be updated...
	if (setCapacity) {
		// Update the capacity information
		#if defined(SNI_MIPS) && (_DPT_BIG_ENDIAN)
			mode_P->getLsuCapacity();
		#else
			mode_P->swapLsuCapacity();
		#endif
		mode_P->swapLsuBlockSize();
		capacity.maxLBA = capacity.maxPhysLBA = mode_P->getLsuCapacity() - 1;
		if (getLevel()==2)
			capacity.maxLBA = getLastUserBlk();

		capacity.blockSize = mode_P->getLsuBlockSize();
	}

	DEBUG(5, PRT_ADDR << "capacity=" << capacity.maxLBA << " blocks");

	return (retVal);

}
//dptDevice_C::updateLAP2status() - end


//Function - dptDevice_C::updatePAPstatus() - start
//===========================================================================
//
//Description:
//
//    This function updates this device's physical array page status.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updatePAPstatus()
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

if (isLogical() || !myMgr_P()->isRAIDcapable()) {
	// If two manager's above is valid...
	if (myMgr_P()->myMgr_P()!=NULL)
		// Set the PAP using the second manager up the chain
		retVal = myMgr_P()->myMgr_P()->setPAPinfo(this);
}
// if (isPhysical())...
else {
	// If formatting...
	if (status.main == PMAIN_STAT_FORMAT) {
		// Check the format status
		checkFmtStatus();
		retVal = MSG_RTN_COMPLETED;
	}
	else
		// Set the PAP using this device's manager
		retVal = myMgr_P()->setPAPinfo(this);
}

return (retVal);

}
//dptDevice_C::updatePAPstatus() - end


//Function - dptDevice_C::updateStatus() - start
//===========================================================================
//
//Description:
//
//    This function updates the status for this device.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateStatus(uSHORT updateCapacity,
					  dptBuffer_S *fromEng_P
					 )
{


   DEBUG_BEGIN(2, dptDevice_C::updateStatus());

   DPT_RTN_T    retVal = MSG_RTN_COMPLETED;

  // Update the status of a firmware based diagnostic
updateDiagStatus();

  // If there is not an active F/W diagnostic and this is a DASD device...
if ((status.main != SMAIN_FW_DIAGNOSTIC) && (getObjType() == DPT_SCSI_DASD)) {
	if (isLogical() && (getRAIDtype()!=RAID_NONE)) {
		if (!isExpandedArray()) {
			// Update this device's status using the logical array page
			retVal = updateLAPstatus(updateCapacity);
		}
		// Determine the ready status of this logical device
		checkIfCompsReady();
	}
	else {
		 // Update the dual loop status
		getDualLoopInfo();
		// Update this device's status using the physical array page
		retVal = updatePAPstatus();
	}

	// Don't allow an ignored
	if (retVal == MSG_RTN_IGNORED)
		retVal = MSG_RTN_COMPLETED;
}

  // If the device is optimal and application diagnostics are active...
if ((status.display==DSPLY_STAT_OPTIMAL) &&
    (status.flags & FLG_STAT_DIAGNOSTICS))
   status.display = DSPLY_STAT_IMPACTED;

  // If a physical device...
if (isPhysical()) {
     // Perform a test unit ready cmd
   checkIfReady();
     // If removable media or no capacity info
   if (isReady() && (isRemovable() || (capacity.maxLBA == 0))) {
	// Try to get the media capacity
      getCapacity();
	// If valid capacity info...
      if (capacity.maxLBA != 0)
	   // Initialize the reserve block info
	 initReserveBlockInfo();
   }

	// if the device is not ready and it is not formatting (and not removable)
	if (!isReady() && !(status.main == PMAIN_STAT_FORMAT) && !isRemovable()) {
	    status.display = DSPLY_STAT_MISSING;
	    DEBUG(1, "Device " << PRT_ADDR << "set to missing" );
	}
	else if (status.display == DSPLY_STAT_MISSING) {
	    DEBUG(1, "Device " << PRT_ADDR << "set to optimal" );
	    status.display = DSPLY_STAT_OPTIMAL;
	}

     // Update the device's SMART status
	updateSmartStatus();
}

if (fromEng_P != NULL) {
   if (!fromEng_P->insert(&status,sizeof(dptStatus_S))) {
	// If not another type of error...
      if (retVal == MSG_RTN_COMPLETED)
	 retVal = MSG_RTN_DATA_OVERFLOW;
   }
}

return (retVal);

}
//dptDevice_C::updateStatus() - end


//Function - dptDevice_C::updateDiagStatus() - start
//===========================================================================
//
//Description:
//
//      This function updates the diagnostic status of a device.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateDiagStatus(dptBuffer_S *fromEng_P)
{

  // If F/W diags not supported or not a F/W logical or physical device...
if (!isFWdiagCapable())
   return(ERR_DIAG_NOT_ACTIVE);

DPT_RTN_T       retVal = ERR_GET_CCB;

engCCB_C *ccb_P = getCCB();
if (ccb_P != NULL) {

	  // Determine if there is an active diagnostic
	ccb_P->mfCmd(MFC_DIAG_STATUS);
	ccb_P->target(this);
	ccb_P->setInterpret();
	ccb_P->input();
	retVal = ERR_DIAG_NOT_ACTIVE;
	  // If the diagnostic is active...
	if (launchCCB(ccb_P) == MSG_RTN_COMPLETED) {
		retVal = MSG_RTN_COMPLETED;
	// If a verify, rebuild, build, expand, don't update the status
	// through the diag status command (let the LAP command do it)
		if ((ccb_P->defData[0] != SSUB_VERIFY) &&
	  (ccb_P->defData[0] != SSUB_REBUILD) &&
	  (ccb_P->defData[0] != SSUB_BUILD) &&
	  (ccb_P->defData[0] != SSUB_EXPAND)) {
		// If a diagnostic was not previously active...
	 if (status.main != SMAIN_FW_DIAGNOSTIC) {

		status.main = SMAIN_FW_DIAGNOSTIC;
		status.sub = ccb_P->defData[0];
		status.display = DSPLY_STAT_IMPACTED;
		// Clear the LAP & PAP status indicators
		status.flags &= ~(FLG_STAT_LAP | FLG_STAT_PAP);


		 dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
		 while (comp_P) {
		 // Indicate that the component is impacted
			 comp_P->setUserDiagFlag();
			 comp_P->updateStatus();
			 comp_P = (dptDevice_C *) compList.next();
		 }
	 }
	 status.main = SMAIN_FW_DIAGNOSTIC;
	 status.sub = ccb_P->defData[0];
	 status.display = DSPLY_STAT_IMPACTED;
	 // Clear the LAP & PAP status indicators
	 status.flags &= ~(FLG_STAT_LAP | FLG_STAT_PAP);


		}
	}

	if ((status.main == SMAIN_FW_DIAGNOSTIC) && (retVal != MSG_RTN_COMPLETED)) {
	// Initialize the status to a non-diagnostic state

		status.main = 0;
		status.sub = 0;
		status.display = DSPLY_STAT_OPTIMAL;
		dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
		while (comp_P) {
		// Indicate that the component is no longer impacted
	 comp_P->clrUserDiagFlag();
	 comp_P->updateStatus();
	 comp_P = (dptDevice_C *) compList.next();
      }
	}

	  // If returning progress information...
	if (fromEng_P != NULL && (status.main != LAPM_EXPAND)) {
	// If a diagnostic is active...
		if (retVal == MSG_RTN_COMPLETED) {
		// Return the diagnostic progress information
	 dptDiagProgress_S *dp_P = (dptDiagProgress_S *) ccb_P->defData;
	 dp_P->scsiSwap();
	 uLONG tempLong = dp_P->getCurBlock();
	 fromEng_P->insert(tempLong);
	 tempLong = 0;
		// If a device buffer test...
	 if ((dp_P->getTestType()==SSUB_BUFFER_READ)
	     || (dp_P->getTestType()==SSUB_BUFFER_RW))
	    fromEng_P->insert(tempLong);
	   // If random sector media test...
	 else if (!(dp_P->getFlags() & DIAG_FLG_ALL_SECTORS) &&
		  ((dp_P->getTestType()==SSUB_MEDIA_READ) ||
		   (dp_P->getTestType()==SSUB_MEDIA_RW)))
	    fromEng_P->insert(tempLong);
	 else
	    fromEng_P->insert(capacity.maxPhysLBA);

	 fromEng_P->insert(&status,sizeof(dptStatus_S));
	 tempLong = dp_P->getCurNumErrors();
	 fromEng_P->insert(tempLong);
	 tempLong = dp_P->getIterations();
	 fromEng_P->insert(tempLong);
	 tempLong = dp_P->getCurIteration();
	 fromEng_P->insert(tempLong);
	 tempLong = dp_P->getFirstErrBlk();
	 fromEng_P->insert(tempLong);
	 uCHAR tempChar = dp_P->getTestType();
	 fromEng_P->insert(tempChar);
	 tempChar = dp_P->getFlags();
	 fromEng_P->insert(tempChar);
	 uSHORT tempShort = dp_P->getMaxErrCnt();
	 fromEng_P->insert(tempShort);
	 tempChar = dp_P->getPercent();
	 if (!fromEng_P->insert(tempChar))
	    retVal = MSG_RTN_DATA_OVERFLOW;
      }
      else {
	   // Zero the progress information
	 memset(fromEng_P->data,0,6*sizeof(uLONG)+sizeof(dptStatus_S));
	 fromEng_P->writeIndex = 6*sizeof(uLONG)+sizeof(dptStatus_S);
      }
	} else if (status.main == LAPM_EXPAND)
			retVal = MSG_RTN_IGNORED;

     // Determine if there is a scheduled diagnostic
   ccb_P->reInit();
   ccb_P->mfCmd(MFC_DIAG_QUERY_SCHEDULE);
   ccb_P->target(this);
   ccb_P->setInterpret();
   ccb_P->input();
     // Update the scheduled diagnostic test type
   scheduledDiag = (launchCCB(ccb_P) == MSG_RTN_COMPLETED) ?
		   ccb_P->defData[0] : 0;

	  // Free the CCB
	ccb_P->clrInUse();


}

return (retVal);

}
//dptDevice_C::updateDiagStatus() - end


//Function - dptDevice_C::checkFmtStatus() - start
//===========================================================================
//
//Description:
//
//      This function determines if this drive is formatting.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uLONG   dptDevice_C::checkFmtStatus(uLONG *totalBlks_P)
{

uLONG   blksComplete = 0;

  // To insure a zero doesn't get returned in totalBlks_P
if (totalBlks_P != NULL) {
   if ((*totalBlks_P = getMaxPhyLBA()) == 0)
      *totalBlks_P = 0xffff;
}

  // If a format failure... Don't update the status
if ((status.main == PMAIN_STAT_FORMAT) &&
    ((status.sub == PSUB_STAT_FMT_FAILED) || (status.sub == PSUB_STAT_CLR_FAILED)))
   return (blksComplete);

engCCB_C *ccb_P = getCCB();
if (ccb_P != NULL) {
   status.display = DSPLY_STAT_OPTIMAL;
   status.main = 2;
   status.sub = 0;
     // Initialize the CCB to perform a request sense
   ccb_P->reqSense();
   if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
	// Set the format status
      setFmtStatus(ccb_P->dataBuff_P);
	// If progress is available and desired...
      if ((ccb_P->dataBuff_P[15] & 0x80) && (totalBlks_P != NULL)) {
	 uSHORT SCSIpercent = *(uSHORT *)(ccb_P->dataBuff_P+16);
	 reverseBytes(SCSIpercent);
	 *totalBlks_P = getMaxPhyLBA();
	   // If no capacity info is available...
	 if (*totalBlks_P == 0) {
	      // Return the request sense info directly
	    blksComplete = SCSIpercent;
	    *totalBlks_P = 0xffff;
	 }
	 else
	      // Return the block completed
	    blksComplete = (*totalBlks_P >> 16) * SCSIpercent;
      }
   }
     // Free the CCB
   ccb_P->clrInUse();
}

  // If the format has completed...
if (status.main != PMAIN_STAT_FORMAT) {
	// Re-initialize the device
	realInit();
	// If an HBA physical device...
	if (getLevel()==2) {
		#ifdef _SINIX_ADDON
			// SNI: Reserve only one block on scsi disks.
			reserveEndOfDisk(0x1);
		#else
			// Reserve space for use by DPT
			reserveEndOfDisk(RESERVED_SPACE_DISK);
		#endif
	}
}

return (blksComplete);

}
//dptDevice_C::checkFmtStatus() - end


//Function - dptDevice_C::setFmtStatus() - start
//===========================================================================
//
//Description:
//
//      This function sets this device's format status based on the
//information in the specified request sense buffer.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::setFmtStatus(uCHAR *reqSense_P)
{

if (reqSense_P != NULL) {
     // If sense key == Not Ready
   if (((reqSense_P[2] & 0x0f) == 0x02) && (reqSense_P[12] == 0x04)) {
	// If format in progress or DPT format clearing phase
      if ((reqSense_P[13] == 0x04) || (reqSense_P[13] == 0x84)) {
	 status.main = PMAIN_STAT_FORMAT;
	 status.display = DSPLY_STAT_BUILD;
	   // If clearing...
	 if (reqSense_P[13] == 0x84)
	    status.sub = PSUB_STAT_CLEARING;
	 else
	    status.sub = 0;
      }
   }
}

}
//dptDevice_C::setFmtStatus() - end


//Function - dptDevice_C::checkFmtFailure() - start
//===========================================================================
//
//Description:
//
//      This function checks the format failed and format clear failed
//bits in the reserve block.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::checkFmtFailure(engCCB_C *ccb_P)
{

  // Check the reserve block version #
if ((ccb_P->dataBuff_P[0x3f] >= 4) && !myHBA_P()->isI2O()) {
     // If a format failure...
   if (ccb_P->dataBuff_P[0x12] & 0x08) {
      status.display    = DSPLY_STAT_BUILD;
      status.main       = PMAIN_STAT_FORMAT;
      status.sub        = PSUB_STAT_FMT_FAILED;
   }
    // If a format clear failure...
   else if (ccb_P->dataBuff_P[0x12] & 0x10) {
      status.display    = DSPLY_STAT_BUILD;
      status.main       = PMAIN_STAT_FORMAT;
      status.sub        = PSUB_STAT_CLR_FAILED;
   }
}

}
//dptDevice_C::checkFmtFailure() - end


//Function - dptDevice_C::handleMessage() - start
//===========================================================================
//
//Description:
//
//    This routine handles DPT events for the dptRAIDdev_C class.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::handleMessage(DPT_MSG_T    message,
					   dptBuffer_S *fromEng_P,
					   dptBuffer_S *toEng_P
					  )
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

switch (message) {

     // Perform a destructive build
   case MSG_RAID_BUILD:
	if (isReal())
	   retVal = raidLAPcmd(LAP_CMD_BUILD);
	break;

     // Perform a non-destructive build (rebuild)
   case MSG_RAID_REBUILD:
	if (isReal())
	   retVal = raidLAPcmd(LAP_CMD_REBUILD);
	break;

     // Perform a verify
   case MSG_RAID_VERIFY:
	if (isReal()) {
	   uCHAR flags = 0;
	   if ( ((dptBuffer_S *)toEng_P)->extract(flags) )
		// Allow user to set abort on error flag
	      flags &= 0x04;
	   retVal = raidLAPcmd(LAP_CMD_VERIFY,flags);
	}
	break;

     // Perform a verify and fix on error
   case MSG_RAID_VERIFY_FIX:
	if (isReal()) {
	   uCHAR flags = 0;
	   if ( ((dptBuffer_S *)toEng_P)->extract(flags) )
		// Allow user to set abort on error flag
	      flags &= 0x04;
	   retVal = raidLAPcmd(LAP_CMD_VERIFY_FIX,flags);
	}
	break;

     // Perform a verify and abort on error
   case MSG_RAID_VERIFY_ABORT:
	if (isReal())
	   retVal = raidLAPcmd(LAP_CMD_VERIFY_ABORT);
	break;

     // Abort the current RAID action
   case MSG_RAID_ABORT:
	if (isReal())
	   retVal = raidLAPcmd(LAP_CMD_ABORT);
	break;

     // Update this device's status
   case MSG_UPDATE_STATUS:
	if (isReal())
	   retVal = updateStatus(0,fromEng_P);
	break;

     // Get the RAID build/rebuild/verify progress
   case MSG_RAID_GET_PROGRESS:
	if (isReal())
	   retVal = rtnProgress(fromEng_P);
	break;

     // Force the device into the specified state
   case MSG_FORCE_STATE:
	  // If no state was specified...
	if (toEng_P->writeIndex < 1L)
	   retVal = MSG_RTN_DATA_UNDERFLOW;
	else
	     // Attempt to force this device's state
	   retVal = raidPAPcmd(toEng_P->data[0]);
	break;

     // Perform a SCSI read command
   case MSG_SCSI_READ:
	retVal = readHandler(toEng_P,fromEng_P);
	break;

     // Perform a SCSI write command
   case MSG_SCSI_WRITE:
	retVal = writeHandler(toEng_P);
	break;

     // Perform a low level SCSI format on this device
   case MSG_SCSI_FORMAT:
	retVal = fmtHandler(toEng_P,fromEng_P);
	break;

     // Add or remove this drive as an emulated drive
   case MSG_SET_EMULATION:
	retVal = setEmulation(toEng_P);
	break;

     // Save the 128 byte software buffer in the reserve block
   case MSG_SET_RB_BUFFER:
	retVal = writeSWreserveBlock(toEng_P);
	break;

     // Read the 128 byte software buffer from the reserve block
   case MSG_GET_RB_BUFFER:
	retVal = rtnSWreserveBlock(fromEng_P);
	break;

     // Return the device statistics information
   case MSG_GET_IO_STATS:
	retVal = rtnIOstats(fromEng_P);
	break;

     // Clear the device statistics information
   case MSG_CLEAR_IO_STATS:
	retVal = getIOstats(NULL,0);
	break;

     // Indicate that the device is performing diagnostics
   case MSG_DIAGNOSTICS_ON:
	retVal = MSG_RTN_COMPLETED;
	setUserDiagFlag();
	break;

     // Clear the diagnostics flag
   case MSG_DIAGNOSTICS_OFF:
	retVal = MSG_RTN_COMPLETED;
	clrUserDiagFlag();
	break;

     // Reset the SCSI bus
   case MSG_RESET_SCSI_BUS:
	retVal = resetSCSI();
	break;

     // Reserve the specified # of blocks at the end of the disk
   case MSG_RESERVE_BLOCKS:
	retVal = handleREOD(toEng_P);
	break;

     // Quiet the SCSI bus/blink this device's LED
   case MSG_QUIET:
	retVal = quietBus(toEng_P);
	break;

     // Schedule a F/W based diagnostic on this device
   case MSG_DIAG_SCHEDULE:
	if (isReal() && isFWdiagCapable())
	   retVal = scheduleDiag(toEng_P);
	break;

     // Unschedule a F/W based diagnostic on this device
   case MSG_DIAG_UNSCHEDULE:
	if (isReal() && isFWdiagCapable())
	   retVal = sendMFC(MFC_DIAG_UNSCHEDULE);
	break;

     // Stop an active F/W based diagnostic on this device
   case MSG_DIAG_STOP:
	if (isReal() && isFWdiagCapable())
	   retVal = sendMFC(MFC_DIAG_STOP);
	break;

     // Return F/W diagnostic scheduling information for this device
   case MSG_DIAG_GET_SCHEDULE:
	if (isReal() && isFWdiagCapable())
	   retVal = queryDiag(fromEng_P);
	break;

     // Enable SMART emulation
   case MSG_SMART_EMUL_ON:
	  // If a real DASD device...
	if (isReal() && (getObjType() == DPT_SCSI_DASD))
	   retVal = sendMFC(MFC_SMART_EMUL_ON,0x01);
	break;

     // Disable SMART emulation
   case MSG_SMART_EMUL_OFF:
	  // If a real DASD device...
	if (isReal() && (getObjType() == DPT_SCSI_DASD))
	   retVal = sendMFC(MFC_SMART_EMUL_OFF,0x01);
	break;

//dz
	case MSG_STATS_LOG_READ:
	case MSG_STATS_LOG_CLEAR:
   case MSG_STATS_LOG_GET_STATUS:
   case MSG_STATS_LOG_SET_STATUS:
	  // Send to the statistics logger
		retVal = osdLoggerCmd(message, (void*)toEng_P,(dptData_S*)fromEng_P,(unsigned short)myConn_P()->getIOmethod(),
			      (unsigned long)0,(unsigned long)magicNum);
		break;	
//dz

	case MSG_GET_ACCESS_RIGHTS:
		retVal = GetAccessRights(fromEng_P);
	break;

	case MSG_SET_ACCESS_RIGHTS:
		retVal = SetAccessRights(fromEng_P, toEng_P);
	break;

	// Initialize the device busy logic
	case MSG_CHECK_BUSY:
		retVal = checkBusy(fromEng_P);
		break;

	case MSG_RAID_SET_LUN_SEGMENTS:
		retVal = setLunSegments(toEng_P);
		break;

	case MSG_RAID_GET_LUN_SEGMENTS:
		retVal = getLunSegments(fromEng_P);
		break;

   default:
	  // Call base class event handler
	retVal = dptRAIDdev_C::handleMessage(message,fromEng_P,toEng_P);
	break;


} // end switch

return (retVal);

}
//dptDevice_C::handleMessage() - end


//Function - dptDevice_C::setLunSegments() - start
//===========================================================================
//Description:
//    This function sets the LUN segmenting for this array.
//---------------------------------------------------------------------------

DPT_RTN_T	dptDevice_C::setLunSegments(dptBuffer_S *toEng_P)
{

   DPT_RTN_T	retVal = MSG_RTN_IGNORED;

if (myHBA_P()->isSegSupported()) {
	// Note: The caller should be able to specify numSegs == 0 and segSize == 0
	//       and this code will still work with the default internal sizes.
	//       numSegs is ignored completey and segSize just skips extraneous data
	//       if it is greater than the default internal size (presently 36 bytes).
	uLONG numSegs = 0;
	uLONG segSize = 0;
	toEng_P->extract(numSegs);
	toEng_P->extract(segSize);
	if (segSize < 36) {
		segSize = 36;
	}
	if (segment_P == NULL) {
		segment_P = new dptLAP2segment_S[8];
		maxSegments = 8;
	}
	memset(segment_P, 0, dptLAP2segment_S::size() * 8);
	uLONG i = 0;
	arraySegment_S inSegment;
	char *ch_P = NULL;
	for (i=0; i < maxSegments; i++) {
		segment_P[i].setPageCode(0x33);
		segment_P[i].setPageLength(0x26);
		segment_P[i].setSegmentIndex((uCHAR)i);
		segment_P[i].setSegmentCount(8);
		if (toEng_P->extract(&inSegment, 36)) {
			ch_P = (char *) (segment_P+i);
			ch_P += 4;
			memcpy(ch_P, &inSegment, 36);
			if (segSize > 36) {
				toEng_P->skip(segSize - 36);
			}
		}
	}

	retVal = raidLAPcmd(LAP_CMD_SEGMENT);

}

return (retVal);

}
//dptDevice_C::setLunSegments() - end


//Function - dptDevice_C::getLunSegments() - start
//===========================================================================
//Description:
//    This function returns the LUN segmenting for this array.
//---------------------------------------------------------------------------

DPT_RTN_T	dptDevice_C::getLunSegments(dptBuffer_S *fromEng_P)
{

   DPT_RTN_T	retVal = MSG_RTN_IGNORED;

if (segment_P != NULL) {
	retVal = MSG_RTN_COMPLETED;
	uLONG tempUL = maxSegments;
	fromEng_P->insert(tempUL);
	tempUL = 36;
	fromEng_P->insert(tempUL);
	char *outSegment_P = NULL;
	uLONG i = 0;
	for (i=0; i < maxSegments; i++) {
		outSegment_P = (char *) (segment_P + i);
		outSegment_P += 4;
		if (!fromEng_P->insert(outSegment_P, 36)) {
			retVal = MSG_RTN_DATA_OVERFLOW;
		}
	}
}

return (retVal);

}
//dptDevice_C::getLunSegments() - end


//Function - dptDevice_C::checkBusy() - start
//===========================================================================
//Description:
//   This function checks if this device is flagged as busy.  A device
//may be flagged as busy if it is mounted by the OS or is known to be
//in use and is not to be included in an array.
//---------------------------------------------------------------------------

DPT_RTN_T dptDevice_C::checkBusy(dptBuffer_S *fromEng_P)
{

uLONG busyStatus = osdTargetBusy(getHBA(), getChan(), getID(), getLUN());

DPT_RTN_T retVal =  (fromEng_P->insert(busyStatus)) ? MSG_RTN_COMPLETED : MSG_RTN_DATA_OVERFLOW;

if ((busyStatus & 0x80000000) && (busyStatus != 0xffffffff)) {
	retVal = ERR_BUSY_CHECK_FAILED;
}
else if (busyStatus == 2) {
	retVal = MSG_RTN_IGNORED;
}

return (retVal);

}
//dptDevice_C::checkBusy()


//Function - dptDevice_C::queryDiag() - start
//===========================================================================
//
//Description:
//
//      This function returns detailed information about a scheduled
//diagnostic.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::queryDiag(dptBuffer_S *fromEng_P)
{

DPT_RTN_T retVal = ERR_GET_CCB;

engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
     // Initialize the CCB for a DPT multi-function command
   ccb_P->mfCmd(MFC_DIAG_QUERY_SCHEDULE);
     // Target this device
   ccb_P->target(this);
     // The HBA must intercept this command
   ccb_P->setInterpret();
     // Transfer data from the HBA
   ccb_P->input();
     // Send the CCB to hardware
   if ((retVal = launchCCB(ccb_P)) == MSG_RTN_COMPLETED) {
	// Swap the data into little endian format
      dptScheduleDiag_S *sd_P = (dptScheduleDiag_S *) ccb_P->defData;
      sd_P->scsiSwap();
	// Return the diagnostic scheduling information
      if (!fromEng_P->insert(ccb_P->defData,dptScheduleDiag_S::size()))
	 retVal = MSG_RTN_DATA_OVERFLOW;
   }

     // Free the CCB
   ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::queryDiag() - end


//Function - dptDevice_C::scheduleDiag() - start
//===========================================================================
//
//Description:
//
//      This function schedules a firmware based diagnostic on this
//device.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::scheduleDiag(dptBuffer_S *toEng_P)
{

DPT_RTN_T       retVal = ERR_GET_CCB;

  // If their is already a diagnostic scheduled on this device...
if (scheduledDiag)
   retVal = MSG_RTN_FAILED | ERR_DIAG_SCHEDULED;
  // If this is a component of a firmware array...
else if (isComponent() && (getLevel() == 2))
     // If the parent or any components have a diagnostic scheduled...
   if (parent.dev_P->chkCompDiags())
      retVal = MSG_RTN_FAILED | ERR_DIAG_SCHEDULED;

  // Copy the specified data into the user
if (toEng_P->writeIndex > dptScheduleDiag_S::size())
   retVal = MSG_RTN_DATA_OVERFLOW;
else if (toEng_P->writeIndex == 0)
   retVal = MSG_RTN_DATA_UNDERFLOW;

  // If its OK to start the diagnostic...
if (retVal == ERR_GET_CCB) {
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
	// Initialize the CCB for a DPT multi-function command
      ccb_P->mfCmd(MFC_DIAG_SCHEDULE);
	// Target this device
      ccb_P->target(this);
	// The HBA must intercept this command
      ccb_P->setInterpret();
	// Initialize the output data buffer
      ccb_P->clrData();
	// Transfer data to the HBA
      ccb_P->output();
	// Copy the user data into the output buffer
      toEng_P->extract(ccb_P->defData,toEng_P->writeIndex);
	// Swap the bytes into big endian format
      dptScheduleDiag_S *sd_P = (dptScheduleDiag_S *) ccb_P->defData;
      sd_P->scsiSwap();
#ifdef _SINIX_ADDON
      if ((sd_P->getTestType() == SSUB_VERIFY) && (scsiFlags2 & FLG_DEV_NO_VERIFY))
	 retVal = ERR_DIAG_NOT_ACTIVE;
      else
	// Send the CCB to hardware
      if ((retVal = launchCCB(ccb_P)) == MSG_RTN_COMPLETED)
	   // Set the diagnostic status
	 updateDiagStatus();
#else
	// Send the CCB to hardware
      if ((retVal = launchCCB(ccb_P)) == MSG_RTN_COMPLETED)
	   // Set the diagnostic status
	 updateDiagStatus();
#endif

	// Free the CCB
      ccb_P->clrInUse();
   }
}

return (retVal);

}
//dptDevice_C::scheduleDiag() - end


//Function - dptDevice_C::isFWdiagCapable() - start
//===========================================================================
//
//Description:
//
//      This function determines if this device is capable of performing
//firmware based diagnostics.
//
//---------------------------------------------------------------------------

uINT    dptDevice_C::isFWdiagCapable()
{

return (myHBA_P()->isFWdiagCapable() && (getLevel() >= 1) && (getLevel() <=2));


}
//dptDevice_C::isFWdiagCapable() - end


//Function - dptDevice_C::checkRebuild() - start
//===========================================================================
//Description:
//      This function checks all components to ensure a successful
//rebuild operation can be performed.
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::checkRebuild()
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

if (isReal() && isLogical()) {
	// If a software array...
	if (getLevel() == 0) {
		// Check the 
		dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
		while (comp_P != NULL) {
			// If a logical component (safety)...
			if (comp_P->isLogical()) {
				// Check the logical's components
				retVal = comp_P->checkRebuild();
				if (retVal != MSG_RTN_IGNORED)
					break;
			}
			comp_P = (dptDevice_C *) compList.next();
		}
	}
	else {
		// # of data drives in the array
		uLONG numDataDrives = compList.getNumObjs() - redundants;
		// Minimum component capacity
		uLONG minCompCapacity = capacity.maxLBA;
		uLONG raidTableSize = (myHBA_P()->isI2O()) ? GEN5_RAID_TABLE_SIZE : RAID_TABLE_SIZE;
		if (numDataDrives)
			minCompCapacity = (minCompCapacity / numDataDrives) + raidTableSize;

		// Check all component capacities
		dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
		while (comp_P != NULL) {
			// If a physical component...
			if (!comp_P->isLogical())
				// Update the component's capacity
				comp_P->getCapacity();
			// If a non-zero capacity and the component is too small...
			if (comp_P->getMaxPhyLBA() && (comp_P->getMaxPhyLBA() < minCompCapacity))
				retVal = MSG_RTN_FAILED | ERR_RAID_COMP_SIZE;

			comp_P = (dptDevice_C *) compList.next();
		}
	}
}

return (retVal);

}
//dptDevice_C::checkRebuild() - end


//Function - dptDevice_C::rtnProgress() - start
//===========================================================================
//
//Description:
//
//    This function returns the progress status of a build/rebuild/verify
//operation.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::rtnProgress(dptBuffer_S *fromEng_P)
{

   DPT_RTN_T            retVal = MSG_RTN_IGNORED;
   uLONG                verifyErrorCnt = 0;
   uLONG                totalBlks;
   uLONG                progress;


	// If formatting...
	if (status.main == PMAIN_STAT_FORMAT) {
		retVal = MSG_RTN_COMPLETED;
		progress = checkFmtStatus(&totalBlks);
	}
	else {
		// Update the diagnostic status and return the progress info
		retVal = updateDiagStatus(fromEng_P);
		// If progress information was returned from the diag status cmd...
		if ((retVal == MSG_RTN_COMPLETED) || (retVal == MSG_RTN_DATA_OVERFLOW))
			return (retVal);

		retVal = MSG_RTN_IGNORED;
		fromEng_P->reset();
		// If an array...
		if (getRAIDtype()!=RAID_NONE) {
			// Determine if any component has a diagnostic in progress
			uINT compDiagInProgress = 0;
			dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
			while (comp_P) {
				comp_P->updateDiagStatus();
				if (comp_P->status.main == SMAIN_FW_DIAGNOSTIC) {
					compDiagInProgress = 1;
					break;
				}
				comp_P = (dptDevice_C *) compList.next();
			}
			// If no component has a diagnostic in progress
			if (!compDiagInProgress)
				// Update the current progress
				retVal = updateLAPstatus(0,&progress,&verifyErrorCnt,&totalBlks);
		}
	}

	if (retVal == MSG_RTN_COMPLETED) {
		// Return the last block completed
		fromEng_P->insert(progress);
		// Return the capacity
		fromEng_P->insert(totalBlks);
		// Return the device's status
		fromEng_P->insert(&status,sizeof(dptStatus_S));
		// Return the device's verify error count
		fromEng_P->insert(verifyErrorCnt);
		// Insert all zeros for the diagnostic specific information
		uLONG tempLong = 0;
		uCHAR tempChar = 0;
		fromEng_P->insert(tempLong);
		fromEng_P->insert(tempLong);
		fromEng_P->insert(tempLong);
		fromEng_P->insert(tempLong);
		if (!fromEng_P->insert(tempChar))
			retVal = MSG_RTN_DATA_OVERFLOW;
	}

return (retVal);

}
//dptDevice_C::rtnProgress() - end


//Function - dptDevice_C::saveDPTname() - start
//===========================================================================
//
//Description:
//
//    This function saves the device's DPT name to hardware.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::saveDPTname()
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

if ( isReal() && (getRAIDtype()!=RAID_NONE) && (getLevel() <= 1) &&
     (scsiFlags & FLG_ENG_NEW_NAME) ) {
   retVal = MSG_RTN_FAILED | ERR_GET_CCB;
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
	// Clear the data buffer
      ccb_P->clrData();
	// Prepare the CCB to do a mode select
      ccb_P->modeSelect(DPT_NAME_PAGE,DPT_NAME_SIZE+2);
	//Indicate that this is a RAID command
      ccb_P->setRAIDcmd();
	// Copy the DPT name into the output buffer
      memcpy(ccb_P->modeParam_P->getData(),dptName,DPT_NAME_SIZE);
	// Send the CCB to hardware
      retVal = launchCCB(ccb_P);
      if (retVal == MSG_RTN_COMPLETED)
	   // Clear the new name flag
	 scsiFlags &= ~FLG_ENG_NEW_NAME;

	// Free the CCB
      ccb_P->clrInUse();
   }
}

return (retVal);

}
//dptDevice_C::saveDPTname() - end


//Function - dptDevice_C::getDPTname() - start
//===========================================================================
//
//Description:
//
//    This function reads the device's DPT name from hardware.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::getDPTname()
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;

if ( (getRAIDtype()!=RAID_NONE) && (getLevel() <= 1) ) {
   retVal = MSG_RTN_FAILED | ERR_GET_CCB;
   engCCB_C *ccb_P = getCCB();
   if (ccb_P!=NULL) {
	// Prepare the CCB to do a mode select
      ccb_P->modeSense(DPT_NAME_PAGE);
	//Indicate that this is a RAID command
      ccb_P->setRAIDcmd();
	// Send the CCB to hardware
      if ((retVal = launchCCB(ccb_P))==MSG_RTN_COMPLETED) {
	   // Copy the DPT name into the output buffer
	 memcpy(dptName,ccb_P->modeParam_P->getData(),DPT_NAME_SIZE);
	   // Null terminate the DPT name string
	 dptName[DPT_NAME_SIZE] = 0;
	   // Clear the new name flag
	 scsiFlags &= ~FLG_ENG_NEW_NAME;
      }

	// Free the CCB
      ccb_P->clrInUse();
   }
}

return (retVal);

}
//dptDevice_C::getDPTname() - end


//Function - dptDevice_C::writeHandler() - start
//===========================================================================
//
//Description:
//
//    This function performs a SCSI write operation for this device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::writeHandler(dptBuffer_S *toEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;
   engCCB_C     *ccb_P;
   uLONG        rwStartLBA;
   uSHORT       rwBlocks;

if (isReal()) {
   retVal = MSG_RTN_DATA_UNDERFLOW;
     // Get the read/write start LBA
   toEng_P->extract(rwStartLBA);
     // Get the read/write # of blocks
   if (toEng_P->extract(rwBlocks)) {
	// Insure the data buffer is as large as the block count
      if ((capacity.blockSize*rwBlocks) <= (toEng_P->allocSize-toEng_P->readIndex)) {
	   // Insure the request doesn't exceed the device's capacity
	 if ((rwStartLBA+rwBlocks-1)>capacity.maxPhysLBA)
	    retVal = MSG_RTN_FAILED | ERR_RW_EXCEEDS_CAPACITY;
	 else {
	    retVal = MSG_RTN_FAILED | ERR_GET_CCB;
	    ccb_P = getCCB();
	    if (ccb_P!=NULL) {
		 // Initialize the CCB to do a SCSI write
	       ccb_P->write(rwStartLBA,rwBlocks,(uSHORT)capacity.blockSize,
			    ptrToLong(toEng_P->data+toEng_P->readIndex));
		 // Send the CCB to hardware
	       retVal = launchCCB(ccb_P);

		 // Free the CCB
	       ccb_P->clrInUse();
	    }
	 }
      }
   }
}

return (retVal);

}
//dptDevice_C::writeHandler() - end


//Function - dptDevice_C::readHandler() - start
//===========================================================================
//
//Description:
//
//    This function performs a SCSI read operation for this device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::readHandler(dptBuffer_S *toEng_P,
					 dptBuffer_S *fromEng_P
					)
{

   DPT_RTN_T    retVal = MSG_RTN_IGNORED;
   engCCB_C     *ccb_P;
   uLONG        rwStartLBA;
   uSHORT       rwBlocks;

if (isReal()) {
   retVal = MSG_RTN_DATA_UNDERFLOW;
     // Get the read/write start LBA
   toEng_P->extract(rwStartLBA);
     // Get the read/write # of blocks
   if (toEng_P->extract(rwBlocks)) {
      retVal = MSG_RTN_DATA_OVERFLOW;
	// Insure the data buffer is as large as the block count
      if ((capacity.blockSize*rwBlocks) <= fromEng_P->allocSize) {
	   // Insure the request doesn't exceed the device's capacity
	 if ((rwStartLBA+rwBlocks-1)>capacity.maxPhysLBA)
	    retVal = MSG_RTN_FAILED | ERR_RW_EXCEEDS_CAPACITY;
	 else {
	    retVal = MSG_RTN_FAILED | ERR_GET_CCB;
	    ccb_P = getCCB();
	    if (ccb_P!=NULL) {
		 // Initialize the CCB to do a SCSI read
	       ccb_P->read(rwStartLBA,rwBlocks,(uSHORT)capacity.blockSize,
			   ptrToLong(fromEng_P->data));
		 // Send the CCB to hardware
	       retVal = launchCCB(ccb_P);
	       if (retVal == MSG_RTN_COMPLETED)
		    // Set the return data size
		  fromEng_P->writeIndex = capacity.blockSize*rwBlocks;

		 // Free the CCB
	       ccb_P->clrInUse();
	    }
	 }
      }
   }
}

return (retVal);

}
//dptDevice_C::readHandler() - end


//Function - dptDevice_C::fmtHandler() - start
//===========================================================================
//
//Description:
//
//      This function attempts to perform a low-level SCSI format on
//this device.
//
// fmtControl:
//    Bit:      Description, If set...
//    ----      ----------------------
//      0       Disable certification
//      1       Attempt to terminate immediately
//      2       (Used internally as the valid init pattern bit)
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

// header lengths
#define	MODE_PARAM_HEADER_LEN				4
#define	MODE_PARAM_BLOCK_LEN				8
#define	FORMAT_DEVICE_BLOCK_LEN				24

// Mode param header field offsets
#define	MODE_PARAM_BLOCK_HEADER_BLK_DESC_LEN_OFST	3

// Mode param block field offsets
#define	MODE_PARAM_BLOCK_BLOCK_SIZE_OFST	5
#define	MODE_PARAM_BLOCK_NUM_BLOCKS_OFST	1

// format block field offsets
#define	FORMAT_DEVICE_BLOCK_LEN_OFST		5
#define	TRUE								1
#define	FALSE								0

DPT_RTN_T       dptDevice_C::fmtHandler(dptBuffer_S *toEng_P,
					dptBuffer_S *fromEng_P
				       )
{

	DPT_RTN_T    retVal = MSG_RTN_IGNORED;
	uSHORT       fmtControl;
	uSHORT       blockSize = 0;
	uSHORT       initPattern;
	uSHORT       blockSizeFailure = 0;
	engCCB_C     *ccb_P;
	int          is_Cheetah = FALSE;

  // If this device is real...
if (isReal()) {
	// If a format is currently in progress...
	if (((status.main == PMAIN_STAT_FORMAT) &&
		((status.sub == PSUB_STAT_IN_PROGRESS) || status.sub == PSUB_STAT_CLEARING)))
		return (MSG_RTN_FAILED | ERR_FORMATTING);
	// Get the format control flags
	if (!toEng_P->extract(fmtControl))
		// Use defaults
		fmtControl = 0;
	// Get the specified block size
	if (!toEng_P->extract(blockSize))
		// Indicate that a block size was not specified
		blockSize = 0;
	// Get the specified initialization pattern
	if (toEng_P->extract(initPattern))
		// Indicate that an initialization pattern was specified
		fmtControl |= 0x04;
	else {
		initPattern = 0;
		// Indicate that no initilization pattern was specified
		fmtControl &= ~0x04;
	}

	retVal = MSG_RTN_FAILED | ERR_GET_CCB;
	 // Get the CCB
	ccb_P = getCCB();
	if (ccb_P!=NULL) {
		int isSeagate = 0;

		// If the block size needs to be changed...
		if (blockSize && (blockSize != phyBlockSize)) {

			reverseBytes(blockSize);

			// If a Seagate drive... (Fix for Seagate Cheetah drives)
			if (findSubString((uCHAR *)"SEAGATE", (uCHAR *)descr.vendorID, 8, 8, 0x02)) {
				isSeagate = 1;
				memset(ccb_P->dataBuff_P, 0, 12);
				ccb_P->dataBuff_P[3] = 8;
				// Set the last 2 bytes to the desired block size
				setU2(ccb_P->dataBuff_P, 10, blockSize);
				// Initialize the CDB
				ccb_P->modeSelect6(0, 0, 0);
				// Send the block descriptor only
				if (launchCCB(ccb_P) == MSG_RTN_COMPLETED)
					blockSizeFailure = 0;
				ccb_P->reInit();
			}

			blockSizeFailure = 1;
			// Get the format device page
			ccb_P->modeSense6(0x03,0,8);
			if (launchCCB(ccb_P) == MSG_RTN_COMPLETED) {
				ccb_P->reInit();

				// Clear the mode parameter header and block descriptor bytes
				memset(ccb_P->dataBuff_P, 0, 3);
				long blockDescriptorLen = ccb_P->dataBuff_P[3];
				uCHAR *blockDescriptor_P = ccb_P->dataBuff_P + sizeof(modeHeader6_S);
				// For each block descriptor...
				while (blockDescriptorLen > 0) {
					// Clear the first 6 bytes
					memset(blockDescriptor_P, 0, 6);
					// Set the last 2 bytes to the desired block size
					setU2(blockDescriptor_P, 6, blockSize);
					// Decrement the block descriptor count
					blockDescriptorLen -= 8;
					// Point to the next block descriptor
					blockDescriptor_P += 8;
				}
				// Initialize the CCB
				ccb_P->modeSelect6(0x03,0x16+2);
				// Set the desired block size in the mode page data
				setU2(ccb_P->modeParam_P->getData(),10,blockSize);
				if (fmtControl & 0x08)
					// Allow drive to compute sectors/track
					setU2(ccb_P->modeParam_P->getData(),8,0);
				// Send the format device page
				if (launchCCB(ccb_P) == MSG_RTN_COMPLETED)
					blockSizeFailure = 0;

			}
			ccb_P->reInit();
		}

		// If the desired block size could not be set...
		if (blockSizeFailure)
			retVal = MSG_RTN_FAILED | ERR_FORMAT_BLK_SIZE;
		else {
			// Clear all unused flags
			fmtControl &= 0x07;
			// If a Seagate drive on a Gen-4 or older controller...
			if (isSeagate && !myHBA_P()->isI2O()) {
					// Send an un-embellished format command directly to the device
					ccb_P->eataCP.scsiCDB[0] = 0x04;
					// Send the format command
					if (launchCCB(ccb_P) == MSG_RTN_COMPLETED)
						blockSizeFailure = 0;
			}
			// If this device's HBA supports the "interpret format"...
			else if (myHBA_P()->isInterpretFormat())
				  // Try to let the HBA disconnect & initialize the drive
				retVal = doFormat(ccb_P,fromEng_P,fmtControl,initPattern,1);
			// Send the format command directly to the device
			else {
				// Issue the SCSI format directly to the device
				retVal = doFormat(ccb_P,fromEng_P,fmtControl,initPattern,0);
				// If terminate immediate was set & a failure occurred...
				if ((fmtControl & 0x02) && (retVal & MSG_RTN_FAILED)) {
					// Clear the terminate immediate bit and try again
					fmtControl &= ~0x0002;
					// Issue the SCSI format directly to the device
					retVal = doFormat(ccb_P,fromEng_P,fmtControl,initPattern,0);
				}
			}
			// If the format has been started...
			if ((retVal == MSG_RTN_STARTED) || (retVal == MSG_RTN_COMPLETED)) {
				status.display = DSPLY_STAT_BUILD;
				status.main = PMAIN_STAT_FORMAT;
				status.sub  = 0;
				if (retVal == MSG_RTN_COMPLETED)
					updateStatus();
			}
		}
		// Free the CCB
		ccb_P->clrInUse();
	}
}

return (retVal);

}
//dptDevice_C::fmtHandler() - end


//Function - dptDevice_C::doFormat() - start
//===========================================================================
//
//Description:
//
//      This function actually issues a SCSI format to a device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::doFormat(engCCB_C          *ccb_P,
				      dptBuffer_S       *fromEng_P,
				      uSHORT            fmtFlags,
				      uSHORT            initPattern,
				      uSHORT            goThruHBA
				     )
{

  // If quantum drive...
if (memcmp(descr.vendorID,"QUANTUM",7)==0) {
   ccb_P->reInit();
   ccb_P->modeSense6(0x39);
     // Send the CCB to hardware
   launchCCB(ccb_P);
   ccb_P->reInit();
   ccb_P->defData[0] = 0;
     // Enable the Quantum unique initialization byte (byte #2)
     // in the format CDB
   ccb_P->modeParam_P->getData()[0] |= 0x08;
   ccb_P->modeSelect6(0x39,6+2);
     // Send the CCB to hardware
   launchCCB(ccb_P);
}

ccb_P->reInit();
ccb_P->clrData();
  // Initialize the CCB to perform a SCSI format
ccb_P->format(fmtFlags,initPattern);
  // If it is desired to let the HBA disconnect and initialize the device...
if (goThruHBA)
   ccb_P->setInterpret();

  // Send the CCB to hardware
DPT_RTN_T retVal = launchCCB(ccb_P);
if (retVal != MSG_RTN_COMPLETED) {
     // Get a pointer to the request sense info
   uCHAR *reqSense_P = getRequestSense(ccb_P);
   if (reqSense_P !=NULL) {
		fromEng_P->reset();
	// Return the request sense data
      fromEng_P->insert(reqSense_P,ccb_P->eataCP.reqSenseLen);
   }
}
  // If the terminate immediate bit was set...
else if (fmtFlags & 0x02)
     // Indicate that a format is in progress...
   retVal = MSG_RTN_STARTED;

return (retVal);

}
//dptDevice_C::doFormat() - end


//Function - dptDevice_C::getRequestSense() - start
//===========================================================================
//
//Description:
//
//      This function returns a pointer to the request sense buffer
//associated with the command in the specified CCB.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uCHAR * dptDevice_C::getRequestSense(engCCB_C *ccb_P)
{

   uCHAR        *reqSense_P = NULL;

  // If a check condition..
if (ccb_P->scsiStatus == 2) {
     // If auto request sense was enabled...
   if (ccb_P->eataCP.flags & CP_REQ_SENSE)
	// Return the sense information to the user
      reqSense_P = ccb_P->defReqSense;
   else {
      ccb_P->reInit();
	// Initialize the CCB to perform a request sense
      ccb_P->reqSense();
      if (launchCCB(ccb_P) == MSG_RTN_COMPLETED)
	 reqSense_P = ccb_P->dataBuff_P;
   }
}

return (reqSense_P);

}
//dptDevice_C::getRequestSense() - end


//Function - dptDevice_C::rtnConfigInfo() - start
//===========================================================================
//
//Description:
//
//    This function returns this object's configuration information.
//This is the information that is stored in the system configuration
//file.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::rtnConfigInfo(dptBuffer_S *fromEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_DATA_OVERFLOW;
   uLONG        bytesLeft,bytesNeeded;
   uLONG        compSize;
   dptAddr_S    devAddr;
   dptBasic_S   *basic_P;
   uLONG        startObjIndex;
   //uLONG        actualBytesWritten;
   uLONG        *objSize_P;

  // Get the # of bytes remaining in the buffer
bytesLeft   = fromEng_P->allocSize - fromEng_P->writeIndex;

  // Calculate the component list size
compSize = compList.size() * (sizeof(raidRange_S) + sizeof(dptAddr_S));

  // Calculate the # of bytes required to save the config. info
bytesNeeded = sizeof(uLONG) + infoSize() + sizeof(uLONG) + compSize;

  // If there is enough space to return this object's information...
if (bytesLeft >= bytesNeeded) {
     // Indicate that this object's info was returned
   retVal = MSG_RTN_COMPLETED;
     // Return the size of the object info
   objSize_P = (uLONG *) (fromEng_P->data+fromEng_P->writeIndex);
   fromEng_P->insert(infoSize());
	  // Get a pointer to this object's config. info
   basic_P = (dptBasic_S *) (fromEng_P->data+fromEng_P->writeIndex);
     // Return this object's information
   startObjIndex = fromEng_P->writeIndex;
   rtnInfo(fromEng_P);
   
   // hnt - CR 2392 - Some of the members in the dptDevInfo_S struct are packed
   // on a 2-byte boundary so when infoSize() returns the size, it may actually
   // be greater than the number of bytes written out due to the packing under
   // GNU C  
   *objSize_P = fromEng_P->writeIndex - startObjIndex;

   if (basic_P->attachedTo!=0) {
	// Return this object's manager SCSI ID instead of tag
      basic_P->attachedTo = myMgr_P()->getAddrL();
	// Reverse the SCSI address bytes
      reverseBytes((uLONG &) basic_P->attachedTo);
   }
     // Return the component list size
   fromEng_P->insert(compSize);

     // Return the component information
   dptDevice_C *dev_P = (dptDevice_C *) compList.reset();
   while (dev_P!=NULL) {
      devAddr = dev_P->getAddr();
	// Return the component's SCSI address
      fromEng_P->insert(&devAddr,sizeof(dptAddr_S));
	// Return the component's stripe information
      fromEng_P->insert(dev_P->parent.startLBA);
      fromEng_P->insert(dev_P->parent.stripeSize);
      fromEng_P->insert(dev_P->parent.numStripes);
	// Get the next device
      dev_P = (dptDevice_C *) compList.next();
   }
}

return (retVal);

}
//dptDevice_C::rtnConfigInfo() - end


//Function - dptDevice_C::rtnIOstats() - start
//===========================================================================
//
//Description:
//
//    Return this device's statistics information.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::rtnIOstats(dptBuffer_S *fromEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_DATA_OVERFLOW;

if (fromEng_P->allocSize>=sizeof(devStats_S)) {
     // Initialize the return statistics to zero
   memset(fromEng_P->data,0,sizeof(devStats_S));
     // Get this device's statistics
   retVal = getIOstats((uLONG *)fromEng_P->data,1);
     // Set the buffer's write indexes
   fromEng_P->writeIndex = sizeof(devStats_S);
}

return (retVal);

}
//dptDevice_C::rtnIOstats() - end


//Function - dptDevice_C::getIOstats() - start
//===========================================================================
//
//Description:
//
//    This function gets the read/write statistics information for
//this device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::getIOstats(uLONG *statBuff_P,uCHAR savePage)
{

    DPT_RTN_T   retVal = MSG_RTN_IGNORED;

if (getObjType()==DPT_SCSI_DASD) {

	if (myHBA_P()->isI2O()) {
		// If a physical device or top level array
		if ((getLevel() == 2) || (parent.dev_P == NULL))
			// Add this device's read/write stats to the stat buffer
			retVal = addRWstats(statBuff_P,savePage);

		// If clearing the stats...
		if (!savePage) {
			if (retVal == MSG_RTN_IGNORED)
				retVal = MSG_RTN_COMPLETED;

			// For all components...
			dptDevice_C *dev_P = (dptDevice_C *) compList.reset();
			while (dev_P!=NULL) {
				// Return the component's statistic information
				if (retVal == MSG_RTN_COMPLETED)
					retVal = dev_P->getIOstats(statBuff_P,savePage);
				else
					dev_P->getIOstats(statBuff_P,savePage);

				if (retVal == MSG_RTN_IGNORED)
					retVal = MSG_RTN_COMPLETED;

				// Get the next component
				dev_P = (dptDevice_C *) compList.next();
			}
		}

	}
	else {
		// If clearing stats or an array...
		if ((getLevel()<=1) || !savePage) {
			retVal = MSG_RTN_COMPLETED;
			// For all components...
			dptDevice_C *dev_P = (dptDevice_C *) compList.reset();
			while (dev_P!=NULL) {
				// Don't duplicate stats for phys device at FW array ID
				if ((getLevel()!=1) || (getID()!=dev_P->getID())) {
					// Return the component's statistic information
					if (retVal == MSG_RTN_COMPLETED)
						retVal = dev_P->getIOstats(statBuff_P,savePage);
					else
						dev_P->getIOstats(statBuff_P,savePage);
				}
				// Get the next component
				dev_P = (dptDevice_C *) compList.next();
			}
		}

		// If clearing stats or a firmware level logical or physical
		if (!savePage || ((getLevel()>=1) && (getLevel()<=2)))
			// Add this device's read/write stats to the stat buffer
			retVal = addRWstats(statBuff_P,savePage);
	}

} // end if (getObjType()==DPT_SCSI_DASD)

return (retVal);

}
//dptDevice_C::getIOstats() - end


//Function - dptDevice_C::setLAPcopyDir() - start
//===========================================================================
//
//Description:
//
//    This function sets the logical array page RAID-1 rebuild (copy)
//direction.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::setLAPcopyDir(uCHAR &copyDir)
{

   dptDevice_C          *comp1_P,*comp2_P;
   dptDevice_C          *target_P = NULL;
   dptDevice_C          *source_P = NULL;

  // If HBA level RAID-1...
if ((getRAIDtype()==1) && (getLevel()==1)) {
     // Get a pointer to the first component
   comp1_P = (dptDevice_C *) compList.reset();
     // Get a pointer to the second component
   comp2_P = (dptDevice_C *) compList.next();
   if ((comp1_P!=NULL) && (comp2_P!=NULL)) {
	// If the first component is the copy target...
      if (comp1_P->isCopyTarget()) {
	 target_P = comp1_P;
	 source_P = comp2_P;
      }
	 // If the second component is the copy target...
      else if (comp2_P->isCopyTarget()) {
	 target_P = comp2_P;
	 source_P = comp1_P;
      }
      if (target_P!=NULL) {
	   // If the target's SCSI address is greater than the source...
	 if (target_P->getAddr()>source_P->getAddr())
	    copyDir |= 0xc0;
	 else
		 copyDir |= 0x80;
      }
   }
}

}
//dptDevice_C::setLAPcopyDir() - end


/*
//Function - dptDevice_C::writeFWD() - start
//===========================================================================
//
//Description:
//
//    This function writes to the downloaded firmware portion of the
//reserve block.  If numBlocks or startAddr is zero, this function
//will clear the downloaded firmware indicator.  Otherwise, the
//downloaded firmware number of blocks and start address will be set
//to the specified values.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::writeFWD(engCCB_C *ccb_P,
				      uSHORT numBlocks,
				      uLONG startAddr
				     )
{

   DPT_RTN_T    retVal;

  // Re-initialize the CCB
ccb_P->reInit();
  // Initialize the CCB to perfrom a SCSI read
ccb_P->read(getMaxPhyLBA(),1,512,ptrToLong(ccb_P->defData));
  // Read the reserve block
if ((retVal = launchCCB(ccb_P))==MSG_RTN_COMPLETED) {
     // Set the downloaded FW indicator ("FW" | numBlocks)
   uLONG indicator = 0x46570000L | numBlocks;
     // If no blocks or an invalid start address specified...
   if ((numBlocks==0) || (startAddr==0)) {
	// Clear the downloaded FW indicator
      indicator = 0;
      startAddr = 0;
   }
   reverseBytes(indicator);
   setU4(ccb_P->defData,0x2c,indicator);
     // Set the downloaded FW start address
   reverseBytes(startAddr);
   setU4(ccb_P->defData,0x30,startAddr);
     // Initialize the CCB to perform a SCSI write
   ccb_P->write(getMaxPhyLBA(),1,512,ptrToLong(ccb_P->defData));
     // Write the modified reserve block
   retVal = launchCCB(ccb_P);
}

return (retVal);

}
//dptDevice_C::writeFWD() - end
*/


//Function - dptDevice_C::handleREOD() - start
//===========================================================================
//
//Description:
//
//    This function reserves space at the end of a non-removable DASD
//device for use by DPT.  This space is currently used for a reserve block,
//a RAID table, and downloaded FW code.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::handleREOD(dptBuffer_S *toEng_P)
{

   DPT_RTN_T    retVal = MSG_RTN_DATA_UNDERFLOW;
   uSHORT       numBlocks;

  // Get the number of blocks to reserve
if (toEng_P->extract(numBlocks))
     // Reserve the specified # of blocks
   retVal = reserveEndOfDisk(numBlocks);

return(retVal);

}
//dptDevice_C::handleREOD() - end


//Function - dptDevice_C::reserveEndOfDisk() - start
//===========================================================================
//
//Description:
//
//    This function reserves space at the end of a non-removable DASD
//device for use by DPT.  This space is currently used for a reserve block,
//a RAID table, and downloaded FW code.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

#ifdef _SINIX_ADDON
DPT_RTN_T	dptDevice_C::reserveEndOfDisk(uLONG numBlocks)
#else
DPT_RTN_T       dptDevice_C::reserveEndOfDisk(uSHORT numBlocks)
#endif
{


   DEBUG_BEGIN(6, dptDevice_C::reserveEndOfDisk());

   DPT_RTN_T    retVal;

	if (isRemovable())
		retVal = MSG_RTN_FAILED | ERR_RSV_REMOVABLE;
	else if (getObjType()!=DPT_SCSI_DASD)
		retVal = MSG_RTN_FAILED | ERR_RSV_NOT_DASD;
	else if (numBlocks==0)
		retVal = MSG_RTN_FAILED | ERR_RSV_NON_ZERO;
	else {
		retVal = MSG_RTN_FAILED | ERR_GET_CCB;
		engCCB_C *ccb_P = getCCB();
		if (ccb_P!=NULL) {
			// Zero the data buffer
			ccb_P->clrData();
			// Initialize the CCB to do a mode select page 0x3e
			ccb_P->modeSelect6(0x3e,0x010+2);
			// Set the interpret bit
			ccb_P->setInterpret();
			uLONG startLBA = capacity.maxPhysLBA - numBlocks + 1;
			reverseBytes(startLBA);
			setU4(ccb_P->modeParam_P->getData(),0,startLBA);
			// Send the CCB to hardware
			retVal = launchCCB(ccb_P);
			if (retVal == MSG_RTN_COMPLETED)
				capacity.maxLBA = capacity.maxPhysLBA - numBlocks;

			// Free the CCB
			ccb_P->clrInUse();

			DEBUG(6, PRT_ADDR << (int)numBlocks << "reserved at end of disk");

		} // end if (ccb_P!=NULL)
	}

return(retVal);

}
//dptDevice_C::reserveEndOfDisk() - end


//Function - dptDevice_C::getLastUserBlk() - start
//===========================================================================
//
//Description:
//
//    This function reads the first reserved block used by DPT and
//returns the last block available for use by an OS.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

uLONG   dptDevice_C::getLastUserBlk()
{

  // Default is the physical capacity
uLONG   retVal = capacity.maxPhysLBA;

  // If this is an HBA physical DASD device...
if ((getLevel()==2) && (getObjType()==DPT_SCSI_DASD)) {
	// Get a CCB
	engCCB_C *ccb_P = getCCB();
	if (ccb_P!=NULL) {
		// Initialize the CCB to do a log sense page 0x3e
		// Limit data buffer to 0x60 bytes
		ccb_P->modeSense(0x3e,0x60);
		// Set the interpret bit
		ccb_P->setInterpret();
		// Send the CCB to hardware
		if (launchCCB(ccb_P)==MSG_RTN_COMPLETED) {
			// Get the first reserved block #
			uLONG startLBA = getU4(ccb_P->modeParam_P->getData(),0);
			reverseBytes(startLBA);
			// If there is a valid first reserved block #
			if ((startLBA>0) && (startLBA<=capacity.maxPhysLBA)) {
				// Return the first block available for use
				retVal = startLBA - 1;
			}
		} // end if (launchCCB())
		// Free the CCB
		ccb_P->clrInUse();
	} // end if (ccb_P!=NULL)
} // end if (HBA physical DASD)

return(retVal);

}
//dptDevice_C::getLastUserBlk() - end


//Function - dptDevice_C::enableTempSpace() - start
//===========================================================================
//
//Description:
//
//    This function attempts to reserve 0x200 blocks at the end of
//all HBA physical component devices.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::enableTempSpace()
{

DEBUG_BEGIN(1, dptDevice_C::enableTempSpace());

	#ifdef _SINIX_ADDON
		uLONG        spaceToReserve;
	#else
		uSHORT       spaceToReserve;
	#endif

	dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
	while (comp_P!=NULL) {
		// If temporary space has been allocated...
		if (comp_P->prevMaxLBA!=0) {

			// Determine how much space to reserve
			#ifdef _SINIX_ADDON
				spaceToReserve = comp_P->getMaxPhyLBA() - comp_P->getLastLBA();
				DEBUG(1, PRT_DADDR(comp_P) << \
				" reserveEndOfDisk: new=" << spaceToReserve << " old=" << \
				(int) (comp_P->getMaxPhyLBA() - comp_P->prevMaxLBA));
			#else
				spaceToReserve = (uSHORT) (comp_P->getMaxPhyLBA() - comp_P->getLastLBA());

				// Limit reserved space (safety)
				if (spaceToReserve > RESERVED_SPACE_RAID)
					spaceToReserve = RESERVED_SPACE_RAID;

				DEBUG(1, PRT_DADDR(comp_P) << " spaceToReserve: " << spaceToReserve);
			#endif

			// Reserve the space
			comp_P->reserveEndOfDisk(spaceToReserve);
			// Clear the previous max LBA
			comp_P->prevMaxLBA = 0;
		}
		comp_P = (dptDevice_C *) compList.next();
	}

}
//dptDevice_C::enableTempSpace() - end


//Function - dptDevice_C::zapPartition() - start
//===========================================================================
//
//Description:
//
//    This function clears block zero of this device to delete a
//partition table that may exist on the device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::zapPartition()
{

   DPT_RTN_T    retVal = MSG_RTN_COMPLETED;

#ifdef _DPT_AIX
	uLONG zap = myConn_P()->isPartZap();
	myConn_P()->setPartZap();
#endif

  // If partition table zapping is enabled...
if (myConn_P()->isPartZap()) {
	retVal = MSG_RTN_FAILED | ERR_GET_CCB;
	engCCB_C *ccb_P = getCCB();
	if (ccb_P!=NULL) {
		// Zero the data bytes
		ccb_P->clrData();
		// If FW physical level or higher...
		//if (getLevel()<=2) - Fix zapping a valid drive's parition table when redirection is active
			// Don't set the EATA physical unit bit
			//ccb_P->setNoEATAphys();

		// Clear block 0
		ccb_P->write(0x00L,1,(uSHORT)capacity.blockSize,ptrToLong(ccb_P->defData));
		// Send the CCB to hardware
		retVal = launchCCB(ccb_P);
		// Clear block 1-16
#		if (defined(_DPT_FREE_BSD) || defined(_DPT_BSDI))
			long block = 0;
			while (retVal == MSG_RTN_COMPLETED) {
				ccb_P->reInit();
				ccb_P->write(++block,1,(uSHORT)capacity.blockSize,ptrToLong(ccb_P->defData));
				// Send the CCB to hardware
				retVal = launchCCB(ccb_P);
				if (block == 15) {
					break;
				}
			}
#		else
			/* Solaris and others */
			if (retVal == MSG_RTN_COMPLETED) {
				// Clear block 1
				ccb_P->reInit();
				ccb_P->write(0x01L,1,(uSHORT)capacity.blockSize,ptrToLong(ccb_P->defData));
				// Send the CCB to hardware
				retVal = launchCCB(ccb_P);
			}
#		endif

		// Free the CCB
		ccb_P->clrInUse();
	}
}

#ifdef _DPT_AIX
	if (!zap)
		myConn_P()->clrPartZap();
#endif

return (retVal);

}
//dptDevice_C::zapPartition() - end


//Function - dptDevice_C::zapCompPartitions() - start
//===========================================================================
//
//Description:
//
//    This function clears block zero of this device to delete a
//partition table that may exist on the device.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

void    dptDevice_C::zapCompPartitions()
{

dptDevice_C *comp_P = (dptDevice_C *) compList.reset();
while (comp_P!=NULL) {
     // Delete the component device's partition table
   comp_P->zapPartition();
     // Get the next component
   comp_P = (dptDevice_C *) compList.next();
}

}
//dptDevice_C::zapCompPartitions() - end


//Function - dptDevice_C::quietBus() - start
//===========================================================================
//
//Description:
//
//      This function quiets the SCSI bus and optionally blinks the
//LED of this device.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::quietBus(dptBuffer_S *toEng_P)
{

   uSHORT       blinkMode = 1;
   uCHAR        modifier = 0x80;

if (!toEng_P->extract(blinkMode))
   blinkMode = 0;

if (blinkMode)
   modifier = 0xa0;

return (sendMFC(MFC_QUIET,modifier));

}
//dptDevice_C::quietBus() - end


//Function - dptDevice_C::sendMFC() - start
//===========================================================================
//
//Description:
//
//    This function sends a RAID multi-function command to this HBA.
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::sendMFC(uCHAR inCmd,uCHAR inModifier)
{

   DPT_RTN_T    retVal = MSG_RTN_FAILED | ERR_GET_CCB;

engCCB_C *ccb_P = getCCB();
if (ccb_P!=NULL) {
     // Initialize the CCB for a DPT multi-function command
   ccb_P->mfCmd(inCmd,inModifier);
     // Target this device
   ccb_P->target(this);
     // The controller should interpret this command
   ccb_P->setInterpret();
     // Send the CCB to hardware
   retVal = launchCCB(ccb_P);

     // Free the CCB
   ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::sendMFC() - end


//Function - dptDevice_C::setPhyMagicNum() - start
//===========================================================================
//
//Description:
//      This function attempts to set a physical device's magic number
//when the physical device is located on a different HBA than the array
//being created.
//
// Warning: Because this function may call genMagicNum(), it should
//          never be called from within an objectList loop since
//          genMagicNum() also iterates through the objectList loop
//          and a re-entrancy error would occur.
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::setPhyMagicNum()
{

DPT_RTN_T       retVal = ERR_GET_CCB;

engCCB_C *ccb_P = getCCB();
if (ccb_P) {
   ccb_P->clrData();
     // The HBA should interpret this command
   ccb_P->setInterpret();
     // Initialize the CCB to do a physical device magic # page
   ccb_P->modeSelect(0x32,0x04+2);
   uLONG *magic_P = (uLONG *) ccb_P->modeParam_P->getData();
     // If no magic number has been assigned to this drive...
   if (!getMagicNum())
	// Assign a magic number
      magicNum = myConn_P()->genMagicNum();
     // Set the magic #
   setU4(magic_P,0,getMagicNum());

#if !defined (SNI_MIPS) && !defined(_DPT_BIG_ENDIAN)
   osdSwap4(magic_P);
#endif

     // If the command was successful
   retVal = launchCCB(ccb_P);

     // Free the CCB
	ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::setPhyMagicNum() - end



//Function - dptDevice_C::updateSmartStatus() - start
//===========================================================================
//
//Description:
//      This function updates a drive's SMART status.
//
//---------------------------------------------------------------------------

DPT_RTN_T       dptDevice_C::updateSmartStatus()
{

DPT_RTN_T retVal = ERR_GET_CCB;

engCCB_C *ccb_P = getCCB();
if (ccb_P) {
     // Initialize the CCB for a DPT multi-function command
   ccb_P->mfCmd(MFC_SMART_STATUS);
     // Target this device
   ccb_P->target(this);
     // The controller should interpret this command
   ccb_P->setInterpret();
     // The status command
   ccb_P->input();
     // Send the CCB to hardware
   if ((retVal = launchCCB(ccb_P)) == MSG_RTN_COMPLETED) {
	// If the device's SMART feature is enabled...
      if (ccb_P->defData[0] & 0x01)
	 scsiFlags2 |= FLG_DEV_SMART_ACTIVE;
      else
	 scsiFlags2 &= ~FLG_DEV_SMART_ACTIVE;
	// If SMART emulation is enabled...
      if (ccb_P->defData[0] & 0x02)
	 scsiFlags2 |= FLG_DEV_SMART_EMULATION;
      else
	 scsiFlags2 &= ~FLG_DEV_SMART_EMULATION;
   }

     // Free the CCB
   ccb_P->clrInUse();
}

return (retVal);

}
//dptDevice_C::updateSmartStatus() - end



//Function -  - start
//===========================================================================
//
//Description:
//
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------
DPT_RTN_T dptDevice_C::SetAccessRights(dptBuffer_S *from_P, dptBuffer_S *to_P)
{
	// remote hbas do not show up in the list
	if ((strncmp(descr.vendorID, "DPT", 3) == 0) && (engType == 3))
		return MSG_RTN_COMPLETED;

	DPT_RTN_T rtnVal = ERR_GET_CCB;

	// we are a component so set the rights from the raid parent
	if (isComponent() && getLevel() == 0) {
		((dptDevice_C *)parent.dev_P)->SetAccessRights(from_P, to_P);

	} else {

		uCHAR acquire;

		// pull out the rights
		if (!to_P->extract(acquire))
			rtnVal = MSG_RTN_DATA_UNDERFLOW;

		// pull out the new rights
		dptMultiInitList_S devRights;
		if (!to_P->extract(&devRights, devRights.size()))
			rtnVal = MSG_RTN_DATA_UNDERFLOW;

		// get the chan id
		uCHAR chanID = getAddr().chan << 5;
		chanID |= getAddr().id & 0x1f;

		devRights.setChanID(chanID);
		if (getAddr().id > 0x1f)
			devRights.setExtendedId(getAddr().id);

		// swap the rights
		devRights.scsiSwap();

		engCCB_C *ccb_P = getCCB();
		if (ccb_P) {

			// target the hba
			ccb_P->target(myHBA_P());

			// get the hba's portion of the rights
			ccb_P->modeSense(0x2d);

			// send it
			if ((rtnVal = myHBA_P()->passCCB(ccb_P)) == MSG_RTN_COMPLETED) {

				dptMultiInitPage_S page, *page_P;

				// copy the hba's poritons of the access rights
				memcpy(&page, ccb_P->modeParam_P->getData(), page.size());
				ccb_P->reInit();
				ccb_P->target(myHBA_P());
				// get the access rights mode page
				ccb_P->modeSelect(0x2d,(uSHORT) (2+dptMultiInitPage_S::size() + dptMultiInitList_S::size()), (acquire >> 1) | 0x80);

				ccb_P->modeParam_P->setPageCode(0x2d);

				// copy the data over
				page_P = (dptMultiInitPage_S *) ccb_P->modeParam_P->getData();

				// what are we doing?
				memcpy(page_P, &page, page.size());
				page_P->setAction((acquire & 0x01)+1);

				// go to just after the page
				page_P++;

				memcpy(page_P, &devRights, devRights.size());

				// send the command
				if ((rtnVal = myHBA_P()->passCCB(ccb_P)) != MSG_RTN_COMPLETED)
					from_P->insert(tag());
			}

			ccb_P->clrInUse();
		}
	}

	return rtnVal;
}
// - end


//Function -  - start
//===========================================================================
//
//Description:
//
//
//Parameters:
//
//Return Value:
//
//Global Variables Affected:
//
//Remarks: (Side effects, Assumptions, Warnings...)
//
//
//---------------------------------------------------------------------------
DPT_RTN_T dptDevice_C::GetAccessRights(dptBuffer_S *from_P)
{

	// remote hbas do not show up in the list
	if ((strncmp(descr.vendorID, "DPT", 3) == 0) && (engType == 3))
		return MSG_RTN_COMPLETED;

	DPT_RTN_T rtnVal = ERR_GET_CCB;

		// we are a component so set the rights from the raid parent
	if (isComponent() && getLevel() == 0) {
		((dptDevice_C *)parent.dev_P)->GetAccessRights(from_P);

	} else {

		// my address
		dptAddr_S lookFor = getAddr();
		engCCB_C *ccb_P = getCCB();

		if (ccb_P) {

			// we are a component so get the rights from the raid parent
			if (isComponent())
				lookFor = parent.dev_P->getAddr();

			// target the hba
			ccb_P->target(myHBA_P());

			// get the access rights mode page
			ccb_P->modeSense(0x2d);

			// send the command
			if ((rtnVal = myHBA_P()->passCCB(ccb_P)) == MSG_RTN_COMPLETED) {

				rtnVal = ERR_INVALID_TGT_TAG;

				// how many devices are there in the list
				uLONG numDevs = ccb_P->modeParam_P->getLength();
				numDevs -= dptMultiInitPage_S::size();
				numDevs /= dptMultiInitList_S::size();
				int found = 0;

				// the first device in the list
				dptMultiInitList_S *devRights_P = (dptMultiInitList_S *) &ccb_P->modeParam_P->getData()[dptMultiInitPage_S::size()];

				// setup the address to look for
				uCHAR chanID = lookFor.chan << 5;
				chanID |= lookFor.id & 0x1f;
				uCHAR extendedId = (lookFor.id > 0x1f) ? lookFor.id : 0;

				// for all there are or we found one
				while(numDevs-- && !found) {

					// swap it
					devRights_P->scsiSwap();

					// do they match?
					if ((devRights_P->getChanID() == chanID) &&
						(devRights_P->getExtendedId() == extendedId)) {

						// we found it, copy the data over
						found = 1;
						from_P->insert(devRights_P, devRights_P->size());

						rtnVal = MSG_RTN_COMPLETED;
					}

					// next device
					devRights_P++;
				}
			}
			// Free the CCB
			ccb_P->clrInUse();
		}
	}

	return rtnVal;
}