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

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


#include <sys/types.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/modctl.h>
#include <sys/callb.h>
#include <sys/strlog.h>
#include <sys/cyclic.h>
#include <sys/rmc_comm_dp.h>
#include <sys/rmc_comm_dp_boot.h>
#include <sys/rmc_comm_drvintf.h>
#include <sys/rmc_comm.h>
#include <sys/machsystm.h>
#include <sys/sysevent.h>
#include <sys/sysevent/dr.h>
#include <sys/sysevent/env.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/file.h>
#include <sys/disp.h>
#include <sys/reboot.h>
#include <sys/envmon.h>
#include <sys/rmclomv_impl.h>
#include <sys/cpu_sgnblk_defs.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <sys/ddi.h>
#include <sys/time.h>
#include <sys/promif.h>
#include <sys/sysmacros.h>

#define	RMCRESBUFLEN	1024
#define	DATE_TIME_MSG_SIZE	78
#define	RMCLOMV_WATCHDOG_MODE	"rmclomv-watchdog-mode"
#define	DELAY_TIME	5000000	 /* 5 seconds, in microseconds */
#define	CPU_SIGNATURE_DELAY_TIME	5000000	 /* 5 secs, in microsecs */

extern void	pmugpio_watchdog_pat();

extern int	watchdog_activated;
static int	last_watchdog_msg = 1;
extern int	watchdog_enable;
extern int	boothowto;

int		rmclomv_watchdog_mode;

/*
 * functions local to this driver.
 */
static int	rmclomv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
    void **resultp);
static int	rmclomv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int	rmclomv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static uint_t	rmclomv_break_intr(caddr_t arg);
static int	rmclomv_add_intr_handlers(void);
static int	rmclomv_remove_intr_handlers(void);
static uint_t	rmclomv_event_data_handler(char *);
static void	rmclomv_dr_data_handler(const char *, int);
static int	rmclomv_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
static int	rmclomv_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
static int	rmclomv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
    cred_t *cred_p, int *rval_p);
static void	rmclomv_checkrmc_start(void);
static void	rmclomv_checkrmc_destroy(void);
static void	rmclomv_checkrmc_wakeup(void *);
static void	rmclomv_refresh_start(void);
static void	rmclomv_refresh_destroy(void);
static void	rmclomv_refresh_wakeup(void);
static void	rmclomv_reset_cache(rmclomv_cache_section_t *new_chain,
    rmclomv_cache_section_t *new_subchain, dp_get_sysinfo_r_t *sysinfo);
static rmclomv_cache_section_t *rmclomv_find_section(
    rmclomv_cache_section_t *start, uint16_t sensor);
static rmclomv_cache_section_t *create_cache_section(int sensor_type, int num);
static int	get_sensor_by_name(const rmclomv_cache_section_t *section,
    const char *name, int *index);
static int	validate_section_entry(rmclomv_cache_section_t *section,
    int index);
static int	add_names_to_section(rmclomv_cache_section_t *section);
static void	free_section(rmclomv_cache_section_t *section);
static void	add_section(rmclomv_cache_section_t **head,
    rmclomv_cache_section_t *section);
static int	rmclomv_do_cmd(int req_cmd, int resp_cmd, int resp_len,
    intptr_t arg_req, intptr_t arg_res);
static void	refresh_name_cache(int force_fail);
static void	set_val_unav(envmon_sensor_t *sensor);
static void	set_fan_unav(envmon_fan_t *fan);
static int	do_psu_cmd(intptr_t arg, int mode, envmon_indicator_t *env_ind,
    dp_get_psu_status_t *rmc_psu, dp_get_psu_status_r_t *rmc_psu_r,
    int detector_type);
static uint_t rmc_set_watchdog_timer(uint_t timeoutval);
static uint_t rmc_clear_watchdog_timer(void);
static void send_watchdog_msg(int msg);
static void plat_timesync(void *arg);

static kmutex_t		timesync_lock;
static clock_t		timesync_interval = 0;
static timeout_id_t	timesync_tid = 0;

/*
 * Driver entry points
 */
static struct cb_ops rmclomv_cb_ops = {
	rmclomv_open,	/* open */
	rmclomv_close,	/* close */
	nodev,		/* strategy() */
	nodev,		/* print() */
	nodev,		/* dump() */
	nodev,		/* read() */
	nodev,		/* write() */
	rmclomv_ioctl,	/* ioctl() */
	nodev,		/* devmap() */
	nodev,		/* mmap() */
	ddi_segmap,	/* segmap() */
	nochpoll,	/* poll() */
	ddi_prop_op,    /* prop_op() */
	NULL,		/* cb_str */
	D_NEW | D_MP	/* cb_flag */
};


static struct dev_ops rmclomv_ops = {
	DEVO_REV,
	0,			/* ref count */
	rmclomv_getinfo,	/* getinfo() */
	nulldev,		/* identify() */
	nulldev,		/* probe() */
	rmclomv_attach,		/* attach() */
	rmclomv_detach,		/* detach */
	nodev,			/* reset */
	&rmclomv_cb_ops,		/* pointer to cb_ops structure */
	(struct bus_ops *)NULL,
	nulldev,		/* power() */
	ddi_quiesce_not_supported,	/* devo_quiesce */
};

/*
 * Loadable module support.
 */
extern struct mod_ops mod_driverops;

static struct modldrv modldrv = {
	&mod_driverops,			/* Type of module. This is a driver */
	"rmclomv control driver",	/* Name of the module */
	&rmclomv_ops			/* pointer to the dev_ops structure */
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	NULL
};

/*
 * Device info
 */
static dev_info_t		*rmclomv_dip = NULL;
static int			rmclomv_break_requested = B_FALSE;
static ddi_softintr_t		rmclomv_softintr_id;
static ddi_iblock_cookie_t	rmclomv_soft_iblock_cookie;

extern void (*abort_seq_handler)();
/* key_position is effective key-position. Set to locked if unknown */
static rsci8 key_position = RMC_KEYSWITCH_POS_LOCKED;
/* real_key_position starts off as unknown and records value actually seen */
static rsci8 real_key_position = RMC_KEYSWITCH_POS_UNKNOWN;
static void rmclomv_abort_seq_handler(char *msg);

/*
 * mutexes which protect the interrupt handlers.
 */
static kmutex_t		rmclomv_event_hdlr_lock;
static kmutex_t		rmclomv_refresh_lock;
static kcondvar_t	rmclomv_refresh_sig_cv;
static kmutex_t		rmclomv_checkrmc_lock;
static kcondvar_t	rmclomv_checkrmc_sig_cv;

/*
 * mutex to protect the handle_name cache
 */
static kmutex_t		rmclomv_cache_lock;

/*
 * mutex to protect the RMC state
 */
static kmutex_t		rmclomv_state_lock;

/*
 * Payloads of the event handlers.
 */
static dp_event_notification_t	rmclomv_event_payload;
static rmc_comm_msg_t	rmclomv_event_payload_msg;

/*
 * Checkrmc commands..
 */
#define	RMCLOMV_CHECKRMC_EXITNOW	(-1)
#define	RMCLOMV_CHECKRMC_WAIT		0
#define	RMCLOMV_CHECKRMC_PROCESSNOW	1

/*
 * Checkrmc thread state
 */
static int rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_WAIT;
static kt_did_t rmclomv_checkrmc_tid = 0;

/*
 * RMC state data
 */
#define	RMCLOMV_RMCSTATE_UNKNOWN	0
#define	RMCLOMV_RMCSTATE_OK		1
#define	RMCLOMV_RMCSTATE_FAILED		2
#define	RMCLOMV_RMCSTATE_DOWNLOAD	3

/*
 * RMC error indicator values (status from last RMC command)
 */
#define	RMCLOMV_RMCERROR_NONE		0

/* fail RMC after 5 minutes without a good response */
#define	RMCLOMV_RMCFAILTHRESHOLD	5

/*
 * rmclomv_rmc_state is the state reported in OperationalStatus.
 * rmclomv_rmc_error reflects the result of the last RMC interaction.
 * rmclomv_rmcfailcount is used by the rmclomv_checkrmc thread to count
 * failures in its regular status polls. Once RMCLOMV_RMCFAILTHRESHOLD
 * is reached, rmclomv_rmc_state is marked as RMCLOMV_RMCSTATE_FAILED.
 */
static int	rmclomv_rmc_state = RMCLOMV_RMCSTATE_UNKNOWN;
static int	rmclomv_rmc_error = RMCLOMV_RMCERROR_NONE;
static int	rmclomv_rmcfailcount;

/*
 * Refresh commands..
 */
#define	RMCLOMV_REFRESH_EXITNOW		(-1)
#define	RMCLOMV_REFRESH_WAIT		0
#define	RMCLOMV_REFRESH_PROCESSNOW	1

/*
 * Refresh thread state
 */
static int rmclomv_refresh_sig = RMCLOMV_REFRESH_WAIT;
static kt_did_t rmclomv_refresh_tid = 0;

/*
 * timeout id
 */
static timeout_id_t	timer_id;

/*
 * Handle-name cache
 */
#define	LOCK_CACHE		mutex_enter(&rmclomv_cache_lock);
#define	RELEASE_CACHE		mutex_exit(&rmclomv_cache_lock);
static rmclomv_cache_section_t	*rmclomv_cache;		/* main handle-names */
static rmclomv_cache_section_t	*rmclomv_subcache;	/* derived names */
static dp_get_sysinfo_r_t	rmclomv_sysinfo_data;
static boolean_t		rmclomv_sysinfo_valid;
static int			rmclomv_cache_valid;

extern pri_t maxclsyspri;

/*
 * static strings
 */
static const char	str_percent[]		= "%";
static const char	str_rpm[]		= " rpm";
static const char	str_ip_volts_ind[]	= "P_PWR";
static const char	str_ip2_volts_ind[]	= "P_PWR2";
static const char	str_ff_pok_ind[]	= "FF_POK";
static const char	str_vlo_volts_ind[]	= "FF_UV";
static const char	str_vhi_volts_ind[]	= "FF_OV";
static const char	str_chi_amps_ind[]	= "FF_OC";
static const char	str_chi_nr_ind[]	= "FF_NR";
static const char	str_ot_tmpr_ind[]	= "FF_OT";
static const char	str_fan_ind[]		= "FF_FAN";
static const char	str_pdct_fan_ind[]	= "FF_PDCT_FAN";
static const char	str_sc[]		= "SC";

int
_init(void)
{
	int	error = 0;

	mutex_init(&rmclomv_event_hdlr_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&rmclomv_checkrmc_lock, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&rmclomv_refresh_lock, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&rmclomv_cache_lock, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&rmclomv_state_lock, NULL, MUTEX_DRIVER, NULL);
	mutex_init(&timesync_lock, NULL, MUTEX_DEFAULT, NULL);
	cv_init(&rmclomv_checkrmc_sig_cv, NULL, CV_DRIVER, NULL);
	cv_init(&rmclomv_refresh_sig_cv, NULL, CV_DRIVER, NULL);

	error = mod_install(&modlinkage);
	if (error) {
		cv_destroy(&rmclomv_refresh_sig_cv);
		cv_destroy(&rmclomv_checkrmc_sig_cv);
		mutex_destroy(&rmclomv_state_lock);
		mutex_destroy(&rmclomv_cache_lock);
		mutex_destroy(&rmclomv_refresh_lock);
		mutex_destroy(&rmclomv_checkrmc_lock);
		mutex_destroy(&rmclomv_event_hdlr_lock);
	}
	return (error);
}


int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}


int
_fini(void)
{
	int	error = 0;

	error = mod_remove(&modlinkage);
	if (error)
		return (error);
	cv_destroy(&rmclomv_refresh_sig_cv);
	cv_destroy(&rmclomv_checkrmc_sig_cv);
	mutex_destroy(&timesync_lock);
	mutex_destroy(&rmclomv_state_lock);
	mutex_destroy(&rmclomv_cache_lock);
	mutex_destroy(&rmclomv_refresh_lock);
	mutex_destroy(&rmclomv_checkrmc_lock);
	mutex_destroy(&rmclomv_event_hdlr_lock);
	return (error);
}


/* ARGSUSED */
static int
rmclomv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
	minor_t m = getminor((dev_t)arg);

	switch (cmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if ((m != 0) || (rmclomv_dip == NULL)) {
			*resultp = NULL;
			return (DDI_FAILURE);
		}
		*resultp = rmclomv_dip;
		return (DDI_SUCCESS);
	case DDI_INFO_DEVT2INSTANCE:
		*resultp = (void *)(uintptr_t)m;
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}


static int
rmclomv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	int			instance;
	int			err;
	char			*wdog_state;
	int			attaching = 1;

	switch (cmd) {
	case DDI_ATTACH:
		/*
		 * only allow one instance
		 */
		instance = ddi_get_instance(dip);
		if (instance != 0)
			return (DDI_FAILURE);

		err = ddi_create_minor_node(dip, "rmclomv", S_IFCHR,
		    instance, DDI_PSEUDO, NULL);
		if (err != DDI_SUCCESS)
			return (DDI_FAILURE);

		/*
		 * Register with rmc_comm to prevent it being detached
		 * (in the unlikely event that its attach succeeded on a
		 * platform whose platmod doesn't lock it down).
		 */
		err = rmc_comm_register();
		if (err != DDI_SUCCESS) {
			ddi_remove_minor_node(dip, NULL);
			return (DDI_FAILURE);
		}

		/* Remember the dev info */
		rmclomv_dip = dip;

		/*
		 * Add the handlers which watch for unsolicited messages
		 * and post event to Sysevent Framework.
		 */
		err = rmclomv_add_intr_handlers();
		if (err != DDI_SUCCESS) {
			rmc_comm_unregister();
			ddi_remove_minor_node(dip, NULL);
			rmclomv_dip = NULL;
			return (DDI_FAILURE);
		}

		rmclomv_checkrmc_start();
		rmclomv_refresh_start();

		abort_seq_handler = rmclomv_abort_seq_handler;
		ddi_report_dev(dip);

		/*
		 * Check whether we have an application watchdog
		 */
		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, RMCLOMV_WATCHDOG_MODE,
		    &wdog_state) == DDI_PROP_SUCCESS) {
			if (strcmp(wdog_state, "app") == 0) {
				rmclomv_watchdog_mode = 1;
				watchdog_enable = 0;
			}
			else
				rmclomv_watchdog_mode = 0;
			ddi_prop_free(wdog_state);
		}

		tod_ops.tod_set_watchdog_timer = rmc_set_watchdog_timer;
		tod_ops.tod_clear_watchdog_timer = rmc_clear_watchdog_timer;

		/*
		 * Now is a good time to activate hardware watchdog
		 * (if one exists).
		 */
		mutex_enter(&tod_lock);
		if (watchdog_enable && tod_ops.tod_set_watchdog_timer != NULL)
			err = tod_ops.tod_set_watchdog_timer(0);
		mutex_exit(&tod_lock);
		if (err != 0)
			printf("Hardware watchdog enabled\n");

		/*
		 * Set time interval and start timesync routine.
		 * Also just this once set the Solaris clock
		 * to the RMC clock.
		 */
		timesync_interval = drv_usectohz(5*60 * MICROSEC);
		plat_timesync((void *) &attaching);

		return (DDI_SUCCESS);
	case DDI_RESUME:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}


static int
rmclomv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	timeout_id_t	tid;
	int		instance;
	int		err;

	switch (cmd) {
	case DDI_DETACH:
		instance = ddi_get_instance(dip);
		if (instance != 0)
			return (DDI_FAILURE);

		/*
		 * Remove the handlers which watch for unsolicited messages
		 * and post event to Sysevent Framework.
		 */
		err = rmclomv_remove_intr_handlers();
		if (err != DDI_SUCCESS) {
			cmn_err(CE_WARN, "Failed to remove event handlers");
			return (DDI_FAILURE);
		}
		rmclomv_checkrmc_destroy();
		rmclomv_refresh_destroy();
		rmclomv_reset_cache(NULL, NULL, NULL);
		ddi_remove_minor_node(dip, NULL);

		mutex_enter(&timesync_lock);
		tid = timesync_tid;
		timesync_tid = 0;
		timesync_interval = 0;
		mutex_exit(&timesync_lock);
		(void) untimeout(tid);

		/* Forget the dev info */
		rmclomv_dip = NULL;
		rmc_comm_unregister();
		return (DDI_SUCCESS);
	case DDI_SUSPEND:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}

static int
rmclomv_add_intr_handlers()
{
	int	err;

	if (ddi_get_soft_iblock_cookie(rmclomv_dip, DDI_SOFTINT_HIGH,
	    &rmclomv_soft_iblock_cookie) != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}
	err = ddi_add_softintr(rmclomv_dip, DDI_SOFTINT_HIGH,
	    &rmclomv_softintr_id, &rmclomv_soft_iblock_cookie, NULL,
	    rmclomv_break_intr, NULL);
	if (err != DDI_SUCCESS)
		return (DDI_FAILURE);
	rmclomv_event_payload_msg.msg_buf = (caddr_t)&rmclomv_event_payload;
	rmclomv_event_payload_msg.msg_len = sizeof (rmclomv_event_payload);
	err = rmc_comm_reg_intr(DP_RMC_EVENTS, rmclomv_event_data_handler,
	    &rmclomv_event_payload_msg, NULL, &rmclomv_event_hdlr_lock);
	if (err != 0) {
		ddi_remove_softintr(rmclomv_softintr_id);
		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}

static int
rmclomv_remove_intr_handlers(void)
{
	int err = rmc_comm_unreg_intr(DP_RMC_EVENTS,
	    rmclomv_event_data_handler);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to unregister DP_RMC_EVENTS "
		    "handler. Err=%d", err);
		return (DDI_FAILURE);
	}
	ddi_remove_softintr(rmclomv_softintr_id);
	return (DDI_SUCCESS);
}

static void
rmclomv_abort_seq_handler(char *msg)
{
	if (key_position == RMC_KEYSWITCH_POS_LOCKED)
		cmn_err(CE_CONT, "KEY in LOCKED position, "
		    "ignoring debug enter sequence");
	else  {
		rmclomv_break_requested = B_TRUE;
		if (msg != NULL)
			prom_printf("%s\n", msg);

		ddi_trigger_softintr(rmclomv_softintr_id);
	}
}

/* ARGSUSED */
static uint_t
rmclomv_break_intr(caddr_t arg)
{
	if (rmclomv_break_requested) {
		rmclomv_break_requested = B_FALSE;
		debug_enter(NULL);
		return (DDI_INTR_CLAIMED);
	}

	return (DDI_INTR_UNCLAIMED);
}

/*
 * Create a cache section structure
 */
static rmclomv_cache_section_t *
create_cache_section(int sensor_type, int num)
{
	size_t len = offsetof(rmclomv_cache_section_t, entry[0]) +
	    num * sizeof (rmclomv_cache_entry_t);
	rmclomv_cache_section_t *ptr = kmem_zalloc(len, KM_SLEEP);
	ptr->next_section = NULL;
	ptr->sensor_type = sensor_type;
	ptr->num_entries = num;
	ptr->section_len = len;
	return (ptr);
}

/*
 * Free a cache_section.
 */
static void
free_section(rmclomv_cache_section_t *section)
{
	size_t len = section->section_len;
	kmem_free(section, len);
}

/*
 * adds supplied section to end of cache chain
 * must be called with cache locked
 */
static void
add_section(rmclomv_cache_section_t **head, rmclomv_cache_section_t *section)
{
	section->next_section = *head;
	*head = section;
}

/*
 * This function releases all cache sections and exchanges the two
 * chain heads for new values.
 */
static void
rmclomv_reset_cache(rmclomv_cache_section_t *new_chain,
    rmclomv_cache_section_t *new_subchain, dp_get_sysinfo_r_t *sysinfo)
{
	rmclomv_cache_section_t	*first;
	rmclomv_cache_section_t	*sub_first;
	rmclomv_cache_section_t	*next;

	LOCK_CACHE

	rmclomv_cache_valid = (new_chain != NULL);
	first = rmclomv_cache;
	rmclomv_cache = new_chain;
	sub_first = rmclomv_subcache;
	rmclomv_subcache = new_subchain;

	if (sysinfo == NULL)
		bzero(&rmclomv_sysinfo_data, sizeof (rmclomv_sysinfo_data));
	else
		bcopy(sysinfo, &rmclomv_sysinfo_data,
		    sizeof (rmclomv_sysinfo_data));

	rmclomv_sysinfo_valid = (sysinfo != NULL);

	RELEASE_CACHE

	while (first != NULL) {
		next = first->next_section;
		free_section(first);
		first = next;
	}

	while (sub_first != NULL) {
		next = sub_first->next_section;
		free_section(sub_first);
		sub_first = next;
	}
}

/*
 * cache must be locked before calling rmclomv_find_section
 */
static rmclomv_cache_section_t *
rmclomv_find_section(rmclomv_cache_section_t *start, uint16_t sensor)
{
	rmclomv_cache_section_t	*next = start;

	while ((next != NULL) && (next->sensor_type != sensor))
		next = next->next_section;

	return (next);
}

/*
 * Return a string presenting the keyswitch position
 * For unknown values returns "Unknown"
 */
static char *
rmclomv_key_position(enum rmc_keyswitch_pos pos)
{
	switch (pos) {

	case RMC_KEYSWITCH_POS_NORMAL:
		return ("NORMAL");
	case RMC_KEYSWITCH_POS_DIAG:
		return ("DIAG");
	case RMC_KEYSWITCH_POS_LOCKED:
		return ("LOCKED");
	case RMC_KEYSWITCH_POS_OFF:
		return ("STBY");
	default:
		return ("UNKNOWN");
	}
}

/*
 * The sensor id name is sought in the supplied section and if found
 * its index within the section is written to *index.
 * Return value is zero for success, otherwise -1.
 * The cache must be locked before calling get_sensor_by_name
 */
static int
get_sensor_by_name(const rmclomv_cache_section_t *section,
    const char *name, int *index)
{
	int i;

	for (i = 0; i < section->num_entries; i++) {
		if (strcmp(name, section->entry[i].handle_name.name) == 0) {
			*index = i;
			return (0);
		}
	}

	*index = 0;
	return (-1);
}

/*
 * fills in the envmon_handle name
 * if it is unknown (not cached), the dp_handle_t is returned as a hex-digit
 * string
 */
static void
rmclomv_hdl_to_envhdl(dp_handle_t hdl, envmon_handle_t *envhdl)
{
	rmclomv_cache_section_t *next;
	int			i;

	LOCK_CACHE

	for (next = rmclomv_cache; next != NULL; next = next->next_section) {
		for (i = 0; i < next->num_entries; i++) {
			if (next->entry[i].handle == hdl) {
				*envhdl = next->entry[i].handle_name;
					RELEASE_CACHE
					return;
			}
		}
	}

	/*
	 * Sought handle not currently cached.
	 */
	RELEASE_CACHE

	(void) snprintf(envhdl->name, sizeof (envhdl->name),
	    "Unknown SC node 0x%x", hdl);
}

static void
rmclomv_dr_data_handler(const char *fru_name, int hint)
{
	int				err = 0;
	nvlist_t			*attr_list;
	char				attach_pnt[MAXPATHLEN];

	(void) snprintf(attach_pnt, sizeof (attach_pnt), "%s", fru_name);

	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN,
		    "Failed to allocate name-value list for %s event", EC_DR);
		return;
	}

	err = nvlist_add_string(attr_list, DR_AP_ID, attach_pnt);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s event",
		    DR_AP_ID, EC_DR);
		nvlist_free(attr_list);
		return;
	}

	/*
	 * Add the hint
	 */
	err = nvlist_add_string(attr_list, DR_HINT, SE_HINT2STR(hint));
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s event",
		    DR_HINT, EC_DR);
		nvlist_free(attr_list);
		return;
	}

	err = ddi_log_sysevent(rmclomv_dip, DDI_VENDOR_SUNW, EC_DR,
	    ESC_DR_AP_STATE_CHANGE, attr_list, NULL, DDI_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to log %s/%s event",
		    DR_AP_ID, EC_DR);
	}

	nvlist_free(attr_list);
}

static void
fan_sysevent(char *fru_name, char *sensor_name, int sub_event)
{
	nvlist_t		*attr_list;
	char			fan_str[MAXNAMELEN];
	int			err;

	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN,
		    "Failed to allocate name-value list for %s/%s event",
		    EC_ENV, ESC_ENV_FAN);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_ID, fru_name);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_ID, EC_ENV, ESC_ENV_FAN);
		nvlist_free(attr_list);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_RESOURCE_ID, sensor_name);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_RESOURCE_ID, EC_ENV, ESC_ENV_FAN);
		nvlist_free(attr_list);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_DEVICE, ENV_RESERVED_ATTR);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_DEVICE, EC_ENV, ESC_ENV_FAN);
		nvlist_free(attr_list);
		return;
	}

	err = nvlist_add_int32(attr_list, ENV_FRU_STATE,
	    (sub_event == RMC_ENV_FAULT_EVENT) ? ENV_FAILED : ENV_OK);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_STATE, EC_ENV, ESC_ENV_FAN);
		nvlist_free(attr_list);
		return;
	}

	if (sub_event == RMC_ENV_FAULT_EVENT) {
		(void) snprintf(fan_str, sizeof (fan_str),
		    "fan %s/%s is now failed", fru_name, sensor_name);
	} else {
		(void) snprintf(fan_str, sizeof (fan_str),
		    "fan %s/%s is now ok", fru_name, sensor_name);
	}
	err = nvlist_add_string(attr_list, ENV_MSG, fan_str);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_MSG, EC_ENV, ESC_ENV_FAN);
		nvlist_free(attr_list);
		return;
	}

	err = ddi_log_sysevent(rmclomv_dip, DDI_VENDOR_SUNW, EC_ENV,
	    ESC_ENV_FAN, attr_list, NULL, DDI_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to log %s/%s event",
		    EC_ENV, ESC_ENV_FAN);
	}

	cmn_err(CE_NOTE, "%s", fan_str);
	nvlist_free(attr_list);
}

static void
threshold_sysevent(char *fru_name, char *sensor_name, int sub_event,
	char event_type)
{
	nvlist_t		*attr_list;
	int			err;
	char			*subclass;
	char			sensor_str[MAXNAMELEN];

	subclass = (event_type == 'T') ? ESC_ENV_TEMP : ESC_ENV_POWER;

	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, KM_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN,
		    "Failed to allocate name-value list for %s/%s event",
		    EC_ENV, subclass);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_ID, fru_name);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_ID, EC_ENV, subclass);
		nvlist_free(attr_list);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_RESOURCE_ID, sensor_name);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_RESOURCE_ID, EC_ENV, subclass);
		nvlist_free(attr_list);
		return;
	}

	err = nvlist_add_string(attr_list, ENV_FRU_DEVICE, ENV_RESERVED_ATTR);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_DEVICE, EC_ENV, subclass);
		nvlist_free(attr_list);
		return;
	}

	switch (sub_event) {
	case RMC_ENV_OK_EVENT:
		err = nvlist_add_int32(attr_list, ENV_FRU_STATE, ENV_OK);
		break;
	case RMC_ENV_WARNING_THRESHOLD_EVENT:
		err = nvlist_add_int32(attr_list, ENV_FRU_STATE, ENV_WARNING);
		break;
	case RMC_ENV_SHUTDOWN_THRESHOLD_EVENT:
		err = nvlist_add_int32(attr_list, ENV_FRU_STATE, ENV_FAILED);
		break;
	}
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_FRU_STATE, EC_ENV, subclass);
		nvlist_free(attr_list);
		return;
	}

	switch (sub_event) {
	case RMC_ENV_OK_EVENT:
		(void) snprintf(sensor_str, sizeof (sensor_str),
		    "sensor %s/%s is now ok", fru_name,
		    sensor_name);
		break;
	case RMC_ENV_WARNING_THRESHOLD_EVENT:
		(void) snprintf(sensor_str, sizeof (sensor_str),
		    "sensor %s/%s is now outside warning thresholds", fru_name,
		    sensor_name);
		break;
	case RMC_ENV_SHUTDOWN_THRESHOLD_EVENT:
		(void) snprintf(sensor_str, sizeof (sensor_str),
		    "sensor %s/%s is now outside shutdown thresholds", fru_name,
		    sensor_name);
		break;
	}
	err = nvlist_add_string(attr_list, ENV_MSG, sensor_str);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to add attr [%s] for %s/%s event",
		    ENV_MSG, EC_ENV, subclass);
		nvlist_free(attr_list);
		return;
	}

	err = ddi_log_sysevent(rmclomv_dip, DDI_VENDOR_SUNW, EC_ENV,
	    subclass, attr_list, NULL, DDI_NOSLEEP);
	if (err != 0) {
		cmn_err(CE_WARN, "Failed to log %s/%s event",
		    EC_ENV, subclass);
	}

	cmn_err(CE_NOTE, "%s", sensor_str);
	nvlist_free(attr_list);
}

static uint_t
rmclomv_event_data_handler(char *arg)
{
	dp_event_notification_t	*payload;
	rmc_comm_msg_t	*msg;
	envmon_handle_t envhdl;
	int hint;
	char *ptr, *save_ptr;

	if (arg == NULL) {
		return (DDI_INTR_CLAIMED);
	}

	msg = (rmc_comm_msg_t *)arg;
	if (msg->msg_buf == NULL) {
		return (DDI_INTR_CLAIMED);
	}

	payload = (dp_event_notification_t *)msg->msg_buf;
	switch (payload->event) {

	case RMC_KEYSWITCH_EVENT:
		real_key_position = payload->event_info.ev_keysw.key_position;
		cmn_err(CE_NOTE, "keyswitch change event - state = %s",
		    rmclomv_key_position(real_key_position));
		if ((real_key_position != RMC_KEYSWITCH_POS_UNKNOWN) &&
		    (real_key_position <= RMC_KEYSWITCH_POS_OFF)) {
			key_position = real_key_position;
		} else {
			/* treat unknown key position as locked */
			key_position = RMC_KEYSWITCH_POS_LOCKED;
		}
		break;

	case RMC_HPU_EVENT:
		/*
		 * send appropriate sysevent
		 */
		switch (payload->event_info.ev_hpunot.sub_event) {
		case RMC_HPU_REMOVE_EVENT:
			hint = SE_HINT_REMOVE;
			break;
		case RMC_HPU_INSERT_EVENT:
			hint = SE_HINT_INSERT;
			break;
		default:
			hint = SE_NO_HINT;
			break;
		}
		rmclomv_hdl_to_envhdl(payload->event_info.ev_hpunot.hpu_hdl,
		    &envhdl);
		rmclomv_dr_data_handler(envhdl.name, hint);
		break;

	case RMC_INIT_EVENT:
		/*
		 * Wake up the refresh thread.
		 */
		rmclomv_refresh_wakeup();

		/*
		 * Wake up the checkrmc thread for an early indication to PICL
		 */
		rmclomv_checkrmc_wakeup(NULL);
		break;

	case RMC_ENV_EVENT:
		rmclomv_hdl_to_envhdl(payload->event_info.ev_envnot.env_hdl,
		    &envhdl);

		/* split name into fru name and sensor name */
		ptr = strchr(envhdl.name, '.');

		/* must have at least one '.' */
		if (ptr == NULL)
			break;

		/* find last '.' - convert the others to '/' */
		for (;;) {
			save_ptr = ptr;
			ptr = strchr(ptr, '.');
			if (ptr == NULL) {
				ptr = save_ptr;
				break;
			}
			*save_ptr = '/';
		}
		*ptr = '\0';
		ptr++;
		/* is it a voltage or temperature sensor? */
		if ((*ptr == 'V' || *ptr == 'T') && *(ptr + 1) == '_') {
			switch (payload->event_info.ev_envnot.sub_event) {
			case RMC_ENV_WARNING_THRESHOLD_EVENT:
			case RMC_ENV_SHUTDOWN_THRESHOLD_EVENT:
			case RMC_ENV_OK_EVENT:
				threshold_sysevent(envhdl.name, ptr,
				    payload->event_info.ev_envnot.sub_event,
				    *ptr);
				break;
			default:
				break;
			}
		}

		/*
		 * is it a fan sensor?
		 * Fan sensor names end either in RS, F0 or F1
		 */
		if ((*ptr == 'R' && *(ptr + 1) == 'S' && *(ptr + 2) == '\0') ||
		    (*ptr == 'F' && *(ptr + 1) == '0' && *(ptr + 2) == '\0') ||
		    (*ptr == 'F' && *(ptr + 1) == '1' && *(ptr + 2) == '\0')) {
			switch (payload->event_info.ev_envnot.sub_event) {
			case RMC_ENV_FAULT_EVENT:
			case RMC_ENV_OK_EVENT:
				fan_sysevent(envhdl.name, ptr,
				    payload->event_info.ev_envnot.sub_event);
				break;
			default:
				break;
			}
		}
		break;

	case RMC_LOG_EVENT:
	{
		int level = 10;
		int flags = SL_NOTE | SL_CONSOLE;
		char *message =
		    (char *)payload->event_info.ev_rmclog.log_record;

		message[ payload->event_info.ev_rmclog.log_record_size] = '\0';

		/*
		 * Logs have a 10 character prefix - specifying the severity of
		 * the event being logged. Thus all the magic number 10s down
		 * here
		 */
		if (0 == strncmp("CRITICAL: ", message, 10)) {
			message += 10;
			level = 0;
			flags = SL_FATAL | SL_ERROR | SL_CONSOLE;
		} else if (0 == strncmp("MAJOR:    ", message, 10)) {
			message += 10;
			level = 5;
			flags = SL_WARN | SL_ERROR | SL_CONSOLE;
		} else if (0 == strncmp("MINOR:    ", message, 10)) {
			message += 10;
			level = 10;
			flags = SL_NOTE | SL_CONSOLE;
		}

		(void) strlog(0, 0, level, flags, message);
		break;
	}

	default:
		return (DDI_INTR_CLAIMED);
	}

	return (DDI_INTR_CLAIMED);
}

/*ARGSUSED*/
static int
rmclomv_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
	int error = 0;
	int instance = getminor(*dev_p);

	if (instance != 0)
		return (ENXIO);

	if ((flag & FWRITE) != 0 && (error = drv_priv(cred_p)) != 0)
		return (error);

	return (0);
}

/*ARGSUSED*/
static int
rmclomv_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
	return (DDI_SUCCESS);
}

static int
rmclomv_do_cmd(int req_cmd, int resp_cmd, int resp_len, intptr_t arg_req,
    intptr_t arg_res)
{
	rmc_comm_msg_t request, *reqp = &request;
	rmc_comm_msg_t response, *resp = &response;
	int rv = 0;

	bzero((caddr_t)&request, sizeof (request));
	reqp->msg_type = req_cmd;
	reqp->msg_buf = (caddr_t)arg_req;
	bzero((caddr_t)&response, sizeof (response));
	resp->msg_type = resp_cmd;
	resp->msg_buf = (caddr_t)arg_res;
	resp->msg_len = resp_len;

	switch (req_cmd) {
	case DP_GET_SYSINFO:
		resp->msg_len = sizeof (dp_get_sysinfo_r_t);
		break;
	case DP_GET_EVENT_LOG:
		resp->msg_len = sizeof (dp_get_event_log_r_t);
		break;
	case DP_GET_VOLTS:
		reqp->msg_len = sizeof (dp_get_volts_t);
		break;
	case DP_GET_TEMPERATURES:
		reqp->msg_len = sizeof (dp_get_temperatures_t);
		break;
	case DP_GET_CIRCUIT_BRKS:
		reqp->msg_len = sizeof (dp_get_circuit_brks_t);
		break;
	case DP_GET_FAN_STATUS:
		reqp->msg_len = sizeof (dp_get_fan_status_t);
		break;
	case DP_GET_PSU_STATUS:
		reqp->msg_len = sizeof (dp_get_psu_status_t);
		break;
	case DP_GET_LED_STATE:
		reqp->msg_len = sizeof (dp_get_led_state_t);
		break;
	case DP_SET_LED_STATE:
		reqp->msg_len = sizeof (dp_set_led_state_t);
		break;
	case DP_GET_FRU_STATUS:
		reqp->msg_len = sizeof (dp_get_fru_status_t);
		break;
	case DP_GET_HANDLE_NAME:
		reqp->msg_len = sizeof (dp_get_handle_name_t);
		break;
	case DP_GET_ALARM_STATE:
		reqp->msg_len = sizeof (dp_get_alarm_state_t);
		break;
	case DP_SET_ALARM_STATE:
		reqp->msg_len = sizeof (dp_set_alarm_state_t);
		break;
	case DP_GET_SDP_VERSION:
		resp->msg_len = sizeof (dp_get_sdp_version_r_t);
		break;
	case DP_GET_CHASSIS_SERIALNUM:
		reqp->msg_len = 0;
		break;
	case DP_GET_DATE_TIME:
		reqp->msg_len = 0;
		break;
	default:
		return (EINVAL);
	}

	rv = rmc_comm_request_response(reqp, resp,
	    RMCLOMV_DEFAULT_MAX_MBOX_WAIT_TIME);

	if (rv != RCNOERR) {
		/*
		 * RMC returned an error or failed to respond.
		 * Where the RMC itself is implicated, rmclomv_rmc_error
		 * is set non-zero. It is cleared after an error free exchange.
		 * Two failure cases are distinguished:
		 * RMCLOMV_RMCSTATE_FAILED and RMCLOMV_RMCSTATE_DOWNLOAD.
		 */
		switch (rv) {
		case RCENOSOFTSTATE:
			/* invalid/NULL soft state structure */
			return (EIO);
		case RCENODATALINK:
			/*
			 * firmware download in progress,
			 * can you come back later?
			 */
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_DOWNLOAD;
			rmclomv_rmc_state = RMCLOMV_RMCSTATE_DOWNLOAD;
			return (EAGAIN);
		case RCENOMEM:
			/* memory problems */
			return (ENOMEM);
		case RCECANTRESEND:
			/* resend failed */
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_FAILED;
			return (EIO);
		case RCEMAXRETRIES:
			/* reply not received - retries exceeded */
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_FAILED;
			return (EINTR);
		case RCETIMEOUT:
			/* reply not received - command has timed out */
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_FAILED;
			return (EINTR);
		case RCEINVCMD:
			/* data protocol cmd not supported */
			return (ENOTSUP);
		case RCEINVARG:
			/* invalid argument(s) */
			return (ENOTSUP);
		case RCEGENERIC:
			/* generic error */
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_FAILED;
			return (EIO);
		default:
			rmclomv_rmc_error = RMCLOMV_RMCSTATE_FAILED;
			return (EIO);
		}
	}

	rmclomv_rmc_error = RMCLOMV_RMCERROR_NONE;
	return (0);
}

/*
 * validate_section_entry checks that the entry at the specified index
 * is valid and not duplicated by an entry above. If these tests fail
 * the entry is removed and B_FALSE returned. Otherwise returns B_TRUE.
 */
static int
validate_section_entry(rmclomv_cache_section_t *section, int index)
{
	int			i;
	rmclomv_cache_entry_t	*entry;

	for (i = index; i < section->num_entries; i++) {
		entry = &section->entry[i];
		if (entry->handle_name.name[0] == '\0') {
			cmn_err(CE_WARN,
			    "rmclomv: empty handle_name, handle 0x%x type %x",
			    entry->handle, section->sensor_type);
		} else if (entry->ind_mask != 0) {
			continue;	/* skip special entries */
		} else if (entry->handle == DP_NULL_HANDLE) {
			cmn_err(CE_WARN,
			    "rmclomv: null handle id for \"%s\" type %x",
			    entry->handle_name.name, section->sensor_type);
		} else if (i == index) {
			continue;
		} else if (section->entry[index].handle == entry->handle) {
			cmn_err(CE_WARN,
			    "rmclomv: duplicate handle 0x%x type %x",
			    entry->handle, section->sensor_type);
		} else if (strcmp(entry->handle_name.name,
		    section->entry[index].handle_name.name) == 0) {
			cmn_err(CE_WARN,
			    "rmclomv: duplicate handle_name \"%s\", "
			    "handle 0x%x type %x", entry->handle_name.name,
			    entry->handle, section->sensor_type);
		} else
			continue;

		/*
		 * need to remove the entry at index
		 */
		section->num_entries--;

		for (i = index; i < section->num_entries; i++) {
			section->entry[i] = section->entry[i + 1];
		}

		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * Populate a section containing handles with corresponding names
 * The supplied section structure must not be publically visible and the
 * name cache must not be locked either (because RMC i/o is required).
 *
 * This is the place where a sanity check is applied. Entries containing
 * duplicate handles, duplicate names or empty names are removed and the
 * structure is compacted. As a result num_entries may be reduced.
 */
static int
add_names_to_section(rmclomv_cache_section_t *section)
{
	int			retval = 0;
	int			ditched = B_FALSE;
	int			index;
	dp_get_handle_name_r_t	handle_name_r;
	rmclomv_cache_entry_t	*entry;

	for (index = 0; index < section->num_entries; index++) {
		entry = &section->entry[index];
		if (entry->ind_mask != 0)
			continue;	/* skip special entries */
		handle_name_r.handle = entry->handle;
		retval = rmclomv_do_cmd(DP_GET_HANDLE_NAME,
		    DP_GET_HANDLE_NAME_R, sizeof (handle_name_r),
		    (intptr_t)&handle_name_r, (intptr_t)&handle_name_r);
		if (retval == 0)
			bcopy(handle_name_r.name,
			    entry->handle_name.name, DP_MAX_HANDLE_NAME);
	}

	/*
	 * now ditch invalid and duplicate entries
	 */
	for (index = 0; index < section->num_entries; index++) {
		while (validate_section_entry(section, index) == B_FALSE)
			ditched = B_TRUE;
	}

	if (ditched)
		cmn_err(CE_WARN, "Retaining %d nodes of type %d",
		    section->num_entries, section->sensor_type);

	return (retval);
}

/*
 * The supplied (PSU) cache section is traversed and entries are created
 * for the individual indicators belonging to a PSU. These entries are
 * placed in a private chain. The caller, subsequently acquires the
 * cache lock and copies the chain head to make it public.
 * The handle-names for PSU indicators are derived from the parent PSU
 * handle-name.
 * NOTE: add_names_to_section() may have reduced psu_section->num_entries
 *       so DON'T USE psu_resp->num_psus
 */
static void
make_psu_subsections(rmclomv_cache_section_t *psu_section,
    rmclomv_cache_section_t **chain_head, dp_get_psu_status_r_t *psu_resp)
{
	int			index;
	int			subindex = 0;
	rmclomv_cache_section_t	*subsection;
	rmclomv_cache_entry_t	*src_entry;
	rmclomv_cache_entry_t	*dst_entry;

	subsection = create_cache_section(RMCLOMV_VOLT_IND,
	    RMCLOMV_MAX_VI_PER_PSU * psu_section->num_entries);
	for (index = 0; index < psu_section->num_entries; index++) {
		src_entry = &psu_section->entry[index];
		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_INPUT_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_INPUT_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_ip_volts_ind);
		}

		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_SEC_INPUT_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_SEC_INPUT_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_ip2_volts_ind);
		}

		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_OUTPUT_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_OUTPUT_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_ff_pok_ind);
		}

		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_OUTPUT_VLO_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_OUTPUT_VLO_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_vlo_volts_ind);
		}

		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_OUTPUT_VHI_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_OUTPUT_VHI_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_vhi_volts_ind);
		}
	}
	/*
	 * Adjust number of entries value in cache section
	 * to match the facts.
	 */
	subsection->num_entries = subindex;
	add_section(chain_head, subsection);

	subsection = create_cache_section(RMCLOMV_AMP_IND,
	    RMCLOMV_MAX_CI_PER_PSU * psu_section->num_entries);
	subindex = 0;
	for (index = 0; index < psu_section->num_entries; index++) {
		int mask = psu_resp->psu_status[index].mask;
		src_entry = &psu_section->entry[index];
		if ((mask & DP_PSU_OUTPUT_AHI_STATUS) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_OUTPUT_AHI_STATUS;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_chi_amps_ind);
		}
		if ((mask & DP_PSU_NR_WARNING) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_NR_WARNING;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_chi_nr_ind);
		}
	}
	subsection->num_entries = subindex;
	add_section(chain_head, subsection);

	subsection = create_cache_section(RMCLOMV_TEMP_IND,
	    psu_section->num_entries);
	subindex = 0;
	for (index = 0; index < psu_section->num_entries; index++) {
		if ((psu_resp->psu_status[index].mask &
		    DP_PSU_OVERTEMP_FAULT) != 0) {
			src_entry = &psu_section->entry[index];
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_OVERTEMP_FAULT;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name,
			    str_ot_tmpr_ind);
		}
	}
	subsection->num_entries = subindex;
	add_section(chain_head, subsection);

	subsection = create_cache_section(RMCLOMV_FAN_IND,
	    RMCLOMV_MAX_FI_PER_PSU * psu_section->num_entries);
	subindex = 0;
	for (index = 0; index < psu_section->num_entries; index++) {
		int mask = psu_resp->psu_status[index].mask;
		src_entry = &psu_section->entry[index];
		if ((mask & DP_PSU_FAN_FAULT) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_FAN_FAULT;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name, str_fan_ind);
		}
		if ((mask & DP_PSU_PDCT_FAN) != 0) {
			dst_entry = &subsection->entry[subindex++];
			dst_entry->handle = src_entry->handle;
			dst_entry->ind_mask = DP_PSU_PDCT_FAN;
			(void) snprintf(dst_entry->handle_name.name,
			    ENVMON_MAXNAMELEN, "%s.%s",
			    src_entry->handle_name.name, str_pdct_fan_ind);
		}
	}
	subsection->num_entries = subindex;
	add_section(chain_head, subsection);
}

static void
refresh_name_cache(int force_fail)
{
	union {
		dp_get_volts_t		u_volts_cmd;
		dp_get_temperatures_t	u_temp_cmd;
		dp_get_circuit_brks_t	u_ampi_cmd;
		dp_get_fan_status_t	u_fan_cmd;
		dp_get_psu_status_t	u_psu_cmd;
		dp_get_fru_status_t	u_fru_cmd;
		dp_get_led_state_t	u_led_cmd;
		dp_set_led_state_t	u_setled_cmd;
		dp_get_alarm_state_t	u_alarm_cmd;
		dp_set_alarm_state_t	u_setalarm_cmd;
	} rmc_cmdbuf;

/* defines for accessing union fields */
#define	volts_cmd	rmc_cmdbuf.u_volts_cmd
#define	temp_cmd	rmc_cmdbuf.u_temp_cmd
#define	ampi_cmd	rmc_cmdbuf.u_ampi_cmd
#define	fan_cmd		rmc_cmdbuf.u_fan_cmd
#define	psu_cmd		rmc_cmdbuf.u_psu_cmd
#define	fru_cmd		rmc_cmdbuf.u_fru_cmd
#define	led_cmd		rmc_cmdbuf.u_led_cmd
#define	setled_cmd	rmc_cmdbuf.u_setled_cmd
#define	alarm_cmd	rmc_cmdbuf.u_alarm_cmd
#define	setalarm_cmd	rmc_cmdbuf.u_setalarm_cmd

	/*
	 * Data area to read sensor data into
	 */
	static union {
		char			reservation[RMCRESBUFLEN];
		dp_get_volts_r_t	u_volts_r;
		dp_get_temperatures_r_t	u_temp_r;
		dp_get_circuit_brks_r_t	u_ampi_r;
		dp_get_fan_status_r_t	u_fan_r;
		dp_get_psu_status_r_t	u_psu_r;
		dp_get_fru_status_r_t	u_fru_r;
		dp_get_led_state_r_t	u_led_r;
		dp_set_led_state_r_t	u_setled_r;
		dp_get_alarm_state_r_t	u_alarm_r;
		dp_set_alarm_state_r_t	u_setalarm_r;
	} rmc_sensbuf;

/* defines for accessing union fields */
#define	volts_r		rmc_sensbuf.u_volts_r
#define	temp_r		rmc_sensbuf.u_temp_r
#define	ampi_r		rmc_sensbuf.u_ampi_r
#define	fan_r		rmc_sensbuf.u_fan_r
#define	psu_r		rmc_sensbuf.u_psu_r
#define	fru_r		rmc_sensbuf.u_fru_r
#define	led_r		rmc_sensbuf.u_led_r
#define	setled_r	rmc_sensbuf.u_setled_r
#define	alarm_r		rmc_sensbuf.u_alarm_r
#define	setalarm_r	rmc_sensbuf.u_setalarm_r

	int			retval = force_fail;
	int			retval1 = retval;
	int			index;
	rmclomv_cache_section_t	*my_chain = NULL;
	rmclomv_cache_section_t	*derived_chain = NULL;
	rmclomv_cache_section_t	*section;
	rmclomv_cache_section_t	*psu_section;
	rmclomv_cache_section_t	*fru_section;
	dp_get_sysinfo_r_t	sysinfo;
	rmclomv_cache_entry_t	*entry;

	if (retval == 0) {
		retval = rmclomv_do_cmd(DP_GET_SYSINFO, DP_GET_SYSINFO_R,
		    sizeof (sysinfo), NULL, (intptr_t)&sysinfo);
	}
	if (retval == 0) {
		fru_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_FRU_STATUS, DP_GET_FRU_STATUS_R,
		    RMCRESBUFLEN, (intptr_t)&fru_cmd, (intptr_t)&fru_r);
	}
	if (retval != 0)
		fru_r.num_frus = 0;

	/*
	 * Reserve space for special additional entries in the FRU section
	 */
	fru_section = create_cache_section(RMCLOMV_HPU_IND,
	    RMCLOMV_NUM_SPECIAL_FRUS + fru_r.num_frus);

	/*
	 * add special entry for RMC itself
	 */
	entry = &fru_section->entry[0];
	(void) snprintf(entry->handle_name.name, sizeof (envmon_handle_t),
	    "SC");
	entry->handle = 0;
	entry->ind_mask = 1;	/* flag as a special entry */

	/*
	 * populate any other FRU entries
	 */
	for (index = 0; index < fru_r.num_frus; index++) {
		fru_section->entry[RMCLOMV_NUM_SPECIAL_FRUS + index].handle =
		    fru_r.fru_status[index].handle;
		fru_section->entry[RMCLOMV_NUM_SPECIAL_FRUS + index].ind_mask =
		    0;
	}

	my_chain = fru_section;

	if (retval == 0) {
		volts_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_VOLTS, DP_GET_VOLTS_R,
		    RMCRESBUFLEN, (intptr_t)&volts_cmd, (intptr_t)&volts_r);
	}
	if (retval == 0) {
		section = create_cache_section(RMCLOMV_VOLT_SENS,
		    volts_r.num_volts);
		for (index = 0; index < volts_r.num_volts; index++) {
			section->entry[index].handle =
			    volts_r.volt_status[index].handle;
		}
		add_section(&my_chain, section);
	}
	if (retval == 0) {
		temp_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_TEMPERATURES,
		    DP_GET_TEMPERATURES_R, RMCRESBUFLEN,
		    (intptr_t)&temp_cmd, (intptr_t)&temp_r);
	}
	if (retval == 0) {
		section = create_cache_section(RMCLOMV_TEMP_SENS,
		    temp_r.num_temps);
		for (index = 0; index < temp_r.num_temps; index++) {
			section->entry[index].handle =
			    temp_r.temp_status[index].handle;
		}
		add_section(&my_chain, section);
	}
	if (retval == 0) {
		fan_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_FAN_STATUS, DP_GET_FAN_STATUS_R,
		    RMCRESBUFLEN, (intptr_t)&fan_cmd, (intptr_t)&fan_r);
	}
	if (retval == 0) {
		section = create_cache_section(RMCLOMV_FAN_SENS,
		    fan_r.num_fans);
		for (index = 0; index < fan_r.num_fans; index++) {
			section->entry[index].handle =
			    fan_r.fan_status[index].handle;
		}
		add_section(&my_chain, section);
	}
	if (retval == 0) {
		ampi_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_CIRCUIT_BRKS,
		    DP_GET_CIRCUIT_BRKS_R, RMCRESBUFLEN,
		    (intptr_t)&ampi_cmd, (intptr_t)&ampi_r);
	}
	if (retval == 0) {
		section = create_cache_section(RMCLOMV_AMP_IND,
		    ampi_r.num_circuit_brks);
		for (index = 0; index < ampi_r.num_circuit_brks; index++) {
			section->entry[index].handle =
			    ampi_r.circuit_brk_status[index].handle;
		}
		add_section(&my_chain, section);
	}
	if (retval == 0) {
		led_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_LED_STATE, DP_GET_LED_STATE_R,
		    RMCRESBUFLEN, (intptr_t)&led_cmd, (intptr_t)&led_r);
	}
	if (retval == 0) {
		section = create_cache_section(RMCLOMV_LED_IND,
		    led_r.num_leds);
		for (index = 0; index < led_r.num_leds; index++) {
			section->entry[index].handle =
			    led_r.led_state[index].handle;
		}
		add_section(&my_chain, section);
	}
	/*
	 * The command DP_GET_ALARM_STATE may not be valid on
	 * some RMC versions, so we ignore the return value
	 * and proceed
	 */
	if (retval == 0) {
		alarm_cmd.handle = DP_NULL_HANDLE;
		retval1 = rmclomv_do_cmd(DP_GET_ALARM_STATE,
		    DP_GET_ALARM_STATE_R, RMCRESBUFLEN,
		    (intptr_t)&alarm_cmd, (intptr_t)&alarm_r);
		if ((retval1 == 0) && alarm_r.num_alarms) {
			section = create_cache_section(RMCLOMV_ALARM_IND,
			    alarm_r.num_alarms);
			for (index = 0; index < alarm_r.num_alarms; index++) {
				section->entry[index].handle =
				    alarm_r.alarm_state[index].handle;
			}
			add_section(&my_chain, section);
		}
	}
	if (retval == 0) {
		psu_cmd.handle = DP_NULL_HANDLE;
		retval = rmclomv_do_cmd(DP_GET_PSU_STATUS, DP_GET_PSU_STATUS_R,
		    RMCRESBUFLEN, (intptr_t)&psu_cmd, (intptr_t)&psu_r);
	}
	if (retval == 0) {
		/*
		 * WARNING:
		 * =======
		 * The PSUs must be probed last so that the response data
		 * (psu_r) is available for make_psu_subsections() below.
		 * Note that all the responses share the same data area
		 * which is declared as a union.
		 */
		psu_section = create_cache_section(RMCLOMV_PSU_IND,
		    psu_r.num_psus);
		for (index = 0; index < psu_r.num_psus; index++) {
			psu_section->entry[index].handle =
			    psu_r.psu_status[index].handle;
		}
		add_section(&my_chain, psu_section);
	}
	if (retval == 0) {
		for (section = my_chain;
		    section != NULL;
		    section = section->next_section) {
			retval = add_names_to_section(section);
			if (retval != 0) {
				break;
			}
		}
	}

	/*
	 * now add nodes derived from PSUs
	 */
	if (retval == 0) {
		make_psu_subsections(psu_section, &derived_chain, &psu_r);
		/*
		 * name cache sections all set, exchange new for old
		 */
		rmclomv_reset_cache(my_chain, derived_chain, &sysinfo);
	} else {
		/*
		 * RMC is not responding, ditch any existing cache
		 * and just leave the special SC FRU node
		 */
		rmclomv_reset_cache(my_chain, NULL, NULL);
	}
}

static void
set_val_unav(envmon_sensor_t *sensor)
{
	sensor->value = ENVMON_VAL_UNAVAILABLE;
	sensor->lowthresholds.warning = ENVMON_VAL_UNAVAILABLE;
	sensor->lowthresholds.shutdown = ENVMON_VAL_UNAVAILABLE;
	sensor->lowthresholds.poweroff = ENVMON_VAL_UNAVAILABLE;
	sensor->highthresholds.warning = ENVMON_VAL_UNAVAILABLE;
	sensor->highthresholds.shutdown = ENVMON_VAL_UNAVAILABLE;
	sensor->highthresholds.poweroff = ENVMON_VAL_UNAVAILABLE;
}

static void
set_fan_unav(envmon_fan_t *fan)
{
	fan->speed = ENVMON_VAL_UNAVAILABLE;
	fan->units[0] = '\0';
	fan->lowthresholds.warning = ENVMON_VAL_UNAVAILABLE;
	fan->lowthresholds.shutdown = ENVMON_VAL_UNAVAILABLE;
	fan->lowthresholds.poweroff = ENVMON_VAL_UNAVAILABLE;
}

static int
do_psu_cmd(intptr_t arg, int mode, envmon_indicator_t *env_ind,
    dp_get_psu_status_t *rmc_psu, dp_get_psu_status_r_t *rmc_psu_r,
    int detector_type)
{
	int			index;
	uint16_t		sensor_status;
	rmclomv_cache_section_t	*section;
	uint16_t		indicator_mask;

	if (ddi_copyin((caddr_t)arg, (caddr_t)env_ind,
	    sizeof (envmon_indicator_t), mode) != 0)
		return (EFAULT);

	/* ensure we've got PSU handles cached */
	LOCK_CACHE

	sensor_status = ENVMON_SENSOR_OK;
	section = rmclomv_find_section(rmclomv_subcache, detector_type);
	if (env_ind->id.name[0] == '\0') {
		/* request for first handle */
		if ((section == NULL) || (section->num_entries == 0))
			env_ind->next_id.name[0] = '\0';
		else
			env_ind->next_id = section->entry[0].handle_name;
		sensor_status = ENVMON_NOT_PRESENT;
	} else {
		/* ensure name is properly terminated */
		env_ind->id.name[ENVMON_MAXNAMELEN - 1] = '\0';
		if ((section == NULL) || (get_sensor_by_name(section,
		    env_ind->id.name, &index)) != 0) {
			env_ind->next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (index + 1 < section->num_entries)
			env_ind->next_id =
			    section->entry[index + 1].handle_name;
		else
			env_ind->next_id.name[0] = '\0';
	}
	if (sensor_status == ENVMON_SENSOR_OK) {
		/*
		 * user correctly identified a sensor, note its
		 * handle value and request the indicator status
		 */
		rmc_psu->handle = section->entry[index].handle;
		indicator_mask = section->entry[index].ind_mask;
	}

	RELEASE_CACHE

	if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
	    rmclomv_do_cmd(DP_GET_PSU_STATUS, DP_GET_PSU_STATUS_R,
	    sizeof (dp_get_psu_status_r_t), (intptr_t)rmc_psu,
	    (intptr_t)rmc_psu_r) != 0)) {
		sensor_status = ENVMON_INACCESSIBLE;
	}
	if ((env_ind->sensor_status = sensor_status) == ENVMON_SENSOR_OK) {
		/*
		 * copy results into buffer for user
		 */
		if ((rmc_psu_r->psu_status[0].flag & DP_PSU_PRESENCE) == 0)
			env_ind->sensor_status |= ENVMON_NOT_PRESENT;
		if (rmc_psu_r->psu_status[0].sensor_status !=
		    DP_SENSOR_DATA_AVAILABLE)
			env_ind->sensor_status |= ENVMON_INACCESSIBLE;
		env_ind->condition =
		    (rmc_psu_r->psu_status[0].flag & indicator_mask) == 0 ?
		    0 : 1;
	}

	if (rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE)
		env_ind->sensor_status = ENVMON_INACCESSIBLE;

	if (ddi_copyout((caddr_t)env_ind, (caddr_t)arg,
	    sizeof (envmon_indicator_t), mode) != 0)
		return (EFAULT);

	return (0);
}

/*ARGSUSED*/
static int
rmclomv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred_p,
    int *rval_p)
{
	int instance = getminor(dev);
	envmon_sysinfo_t lomv_sysinfo;
	union {
		envmon_sensor_t		u_env_sensor;
		envmon_indicator_t	u_env_ind;
		envmon_fan_t		u_env_fan;
		envmon_led_info_t	u_env_ledinfo;
		envmon_led_ctl_t	u_env_ledctl;
		envmon_hpu_t		u_env_hpu;
		envmon_alarm_info_t	u_env_alarminfo;
		envmon_alarm_ctl_t	u_env_alarmctl;
	} env_buf;
#define	env_sensor	env_buf.u_env_sensor
#define	env_ind		env_buf.u_env_ind
#define	env_fan		env_buf.u_env_fan
#define	env_ledinfo	env_buf.u_env_ledinfo
#define	env_ledctl	env_buf.u_env_ledctl
#define	env_hpu		env_buf.u_env_hpu
#define	env_alarminfo	env_buf.u_env_alarminfo
#define	env_alarmctl	env_buf.u_env_alarmctl

	union {
		dp_get_volts_t		u_rmc_volts;
		dp_get_temperatures_t	u_rmc_temp;
		dp_get_circuit_brks_t	u_rmc_ampi;
		dp_get_fan_status_t	u_rmc_fan;
		dp_get_psu_status_t	u_rmc_psu;
		dp_get_fru_status_t	u_rmc_fru;
		dp_get_led_state_t	u_rmc_led;
		dp_set_led_state_t	u_rmc_setled;
		dp_get_alarm_state_t	u_rmc_alarm;
		dp_set_alarm_state_t	u_rmc_setalarm;
	} rmc_reqbuf;
#define	rmc_volts	rmc_reqbuf.u_rmc_volts
#define	rmc_temp	rmc_reqbuf.u_rmc_temp
#define	rmc_ampi	rmc_reqbuf.u_rmc_ampi
#define	rmc_fan		rmc_reqbuf.u_rmc_fan
#define	rmc_psu		rmc_reqbuf.u_rmc_psu
#define	rmc_fru		rmc_reqbuf.u_rmc_fru
#define	rmc_led		rmc_reqbuf.u_rmc_led
#define	rmc_setled	rmc_reqbuf.u_rmc_setled
#define	rmc_alarm	rmc_reqbuf.u_rmc_alarm
#define	rmc_setalarm	rmc_reqbuf.u_rmc_setalarm

	union {
		dp_get_volts_r_t	u_rmc_volts_r;
		dp_get_temperatures_r_t	u_rmc_temp_r;
		dp_get_circuit_brks_r_t	u_rmc_ampi_r;
		dp_get_fan_status_r_t	u_rmc_fan_r;
		dp_get_psu_status_r_t	u_rmc_psu_r;
		dp_get_fru_status_r_t	u_rmc_fru_r;
		dp_get_led_state_r_t	u_rmc_led_r;
		dp_set_led_state_r_t	u_rmc_setled_r;
		dp_get_alarm_state_r_t	u_rmc_alarm_r;
		dp_set_alarm_state_r_t	u_rmc_setalarm_r;
		dp_get_sdp_version_r_t	u_rmc_sdpversion_r;
		dp_get_serialnum_r_t	u_rmc_serialnum_r;
	} rmc_resbuf;
#define	rmc_volts_r	rmc_resbuf.u_rmc_volts_r
#define	rmc_temp_r	rmc_resbuf.u_rmc_temp_r
#define	rmc_ampi_r	rmc_resbuf.u_rmc_ampi_r
#define	rmc_fan_r	rmc_resbuf.u_rmc_fan_r
#define	rmc_psu_r	rmc_resbuf.u_rmc_psu_r
#define	rmc_fru_r	rmc_resbuf.u_rmc_fru_r
#define	rmc_led_r	rmc_resbuf.u_rmc_led_r
#define	rmc_setled_r	rmc_resbuf.u_rmc_setled_r
#define	rmc_alarm_r	rmc_resbuf.u_rmc_alarm_r
#define	rmc_setalarm_r	rmc_resbuf.u_rmc_setalarm_r
#define	rmc_sdpver_r	rmc_resbuf.u_rmc_sdpversion_r
#define	rmc_serialnum_r	rmc_resbuf.u_rmc_serialnum_r

	int			retval = 0;
	int			special = 0;
	int			index;
	uint16_t		sensor_status;
	rmclomv_cache_section_t	*section;
	envmon_chassis_t chassis;

	if (instance != 0)
		return (ENXIO);

	switch (cmd) {
	case ENVMONIOCSYSINFO:

		LOCK_CACHE

		/*
		 * A number of OK/not_OK indicators are supported by PSUs
		 * (voltage, current, fan, temperature). So the maximum
		 * number of such indicators relates to the maximum number
		 * of power-supplies.
		 */
		if (rmclomv_sysinfo_valid) {
			lomv_sysinfo.maxVoltSens = rmclomv_sysinfo_data.maxVolt;
			lomv_sysinfo.maxVoltInd =
			    RMCLOMV_MAX_VI_PER_PSU *
			    rmclomv_sysinfo_data.maxPSU;
			/*
			 * the ALOM-Solaris interface does not include
			 * amp sensors, so we can hard code this value
			 */
			lomv_sysinfo.maxAmpSens = 0;
			lomv_sysinfo.maxAmpInd =
			    rmclomv_sysinfo_data.maxCircuitBrks +
			    (RMCLOMV_MAX_CI_PER_PSU *
			    rmclomv_sysinfo_data.maxPSU);
			lomv_sysinfo.maxTempSens = rmclomv_sysinfo_data.maxTemp;
			lomv_sysinfo.maxTempInd =
			    (RMCLOMV_MAX_TI_PER_PSU *
			    rmclomv_sysinfo_data.maxPSU);
			lomv_sysinfo.maxFanSens = rmclomv_sysinfo_data.maxFan;
			lomv_sysinfo.maxFanInd =
			    RMCLOMV_MAX_FI_PER_PSU *
			    rmclomv_sysinfo_data.maxPSU;
			lomv_sysinfo.maxLED = rmclomv_sysinfo_data.maxLED;
			lomv_sysinfo.maxHPU = RMCLOMV_NUM_SPECIAL_FRUS +
			    rmclomv_sysinfo_data.maxFRU;
		} else {
			bzero(&lomv_sysinfo, sizeof (lomv_sysinfo));
			lomv_sysinfo.maxHPU = 1;	/* just the SC node */
		}

		RELEASE_CACHE

		if (ddi_copyout((caddr_t)&lomv_sysinfo, (caddr_t)arg,
		    sizeof (lomv_sysinfo), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCVOLTSENSOR:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_sensor,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);

		/* see if we've got volts handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_VOLT_SENS)) == NULL)) {
			env_sensor.next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (env_sensor.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_sensor.next_id.name[0] = '\0';
			else
				env_sensor.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_sensor.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_sensor.id.name,
			    &index) != 0) {
				env_sensor.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_sensor.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_sensor.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified a sensor, note its
			 * handle value and request the sensor value
			 */
			rmc_volts.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_VOLTS, DP_GET_VOLTS_R,
		    sizeof (rmc_volts_r), (intptr_t)&rmc_volts,
		    (intptr_t)&rmc_volts_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmc_volts_r.volt_status[0].sensor_status ==
		    DP_SENSOR_NOT_PRESENT)) {
			sensor_status = ENVMON_NOT_PRESENT;
		}
		if ((env_sensor.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			/*
			 * copy results into buffer for user
			 */
			if (rmc_volts_r.volt_status[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_sensor.sensor_status = ENVMON_INACCESSIBLE;
			env_sensor.value =
			    rmc_volts_r.volt_status[0].reading;
			env_sensor.lowthresholds.warning =
			    rmc_volts_r.volt_status[0].low_warning;
			env_sensor.lowthresholds.shutdown =
			    rmc_volts_r.volt_status[0].low_soft_shutdown;
			env_sensor.lowthresholds.poweroff =
			    rmc_volts_r.volt_status[0].low_hard_shutdown;
			env_sensor.highthresholds.warning =
			    rmc_volts_r.volt_status[0].high_warning;
			env_sensor.highthresholds.shutdown =
			    rmc_volts_r.volt_status[0].high_soft_shutdown;
			env_sensor.highthresholds.poweroff =
			    rmc_volts_r.volt_status[0].high_hard_shutdown;
		}
		if (env_sensor.sensor_status != ENVMON_SENSOR_OK ||
		    rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE)
			set_val_unav(&env_sensor);

		if (ddi_copyout((caddr_t)&env_sensor, (caddr_t)arg,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCVOLTIND:
		return (do_psu_cmd(arg, mode, &env_ind, &rmc_psu, &rmc_psu_r,
		    RMCLOMV_VOLT_IND));

	case ENVMONIOCTEMPIND:
		return (do_psu_cmd(arg, mode, &env_ind, &rmc_psu, &rmc_psu_r,
		    RMCLOMV_TEMP_IND));

	case ENVMONIOCFANIND:
		return (do_psu_cmd(arg, mode, &env_ind, &rmc_psu, &rmc_psu_r,
		    RMCLOMV_FAN_IND));

	case ENVMONIOCAMPSENSOR:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_sensor,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);

		env_sensor.sensor_status = ENVMON_NOT_PRESENT;
		env_sensor.next_id.name[0] = '\0';

		if (ddi_copyout((caddr_t)&env_sensor, (caddr_t)arg,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCTEMPSENSOR:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_sensor,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);

		/* see if we've got temperature handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_TEMP_SENS)) == NULL)) {
			env_sensor.next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (env_sensor.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_sensor.next_id.name[0] = '\0';
			else
				env_sensor.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_sensor.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_sensor.id.name,
			    &index) != 0) {
				env_sensor.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_sensor.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_sensor.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified a sensor, note its
			 * handle value and request the sensor value
			 */
			rmc_temp.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_TEMPERATURES, DP_GET_TEMPERATURES_R,
		    sizeof (rmc_temp_r), (intptr_t)&rmc_temp,
		    (intptr_t)&rmc_temp_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmc_temp_r.temp_status[0].sensor_status ==
		    DP_SENSOR_NOT_PRESENT)) {
			sensor_status = ENVMON_NOT_PRESENT;
		}
		if ((env_sensor.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			/*
			 * copy results into buffer for user
			 */
			if (rmc_temp_r.temp_status[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_sensor.sensor_status = ENVMON_INACCESSIBLE;
			env_sensor.value =
			    rmc_temp_r.temp_status[0].value;
			env_sensor.lowthresholds.warning =
			    rmc_temp_r.temp_status[0].low_warning;
			env_sensor.lowthresholds.shutdown =
			    rmc_temp_r.temp_status[0].low_soft_shutdown;
			env_sensor.lowthresholds.poweroff =
			    rmc_temp_r.temp_status[0].low_hard_shutdown;
			env_sensor.highthresholds.warning =
			    rmc_temp_r.temp_status[0].high_warning;
			env_sensor.highthresholds.shutdown =
			    rmc_temp_r.temp_status[0].high_soft_shutdown;
			env_sensor.highthresholds.poweroff =
			    rmc_temp_r.temp_status[0].high_hard_shutdown;
		}
		if (env_sensor.sensor_status != ENVMON_SENSOR_OK ||
		    rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE)
			set_val_unav(&env_sensor);

		if (ddi_copyout((caddr_t)&env_sensor, (caddr_t)arg,
		    sizeof (envmon_sensor_t), mode) != 0)
			return (EFAULT);
		break;


	case ENVMONIOCFAN:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_fan,
		    sizeof (envmon_fan_t), mode) != 0)
			return (EFAULT);

		/* see if we've got fan handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_FAN_SENS)) == NULL)) {
			env_fan.next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (env_fan.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_fan.next_id.name[0] = '\0';
			else
				env_fan.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_fan.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_fan.id.name,
			    &index) != 0) {
				env_fan.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_fan.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_fan.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified a sensor, note its
			 * handle value and request the sensor value
			 */
			rmc_fan.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_FAN_STATUS, DP_GET_FAN_STATUS_R,
		    sizeof (rmc_fan_r), (intptr_t)&rmc_fan,
		    (intptr_t)&rmc_fan_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmc_fan_r.fan_status[0].sensor_status ==
		    DP_SENSOR_NOT_PRESENT)) {
			sensor_status = ENVMON_NOT_PRESENT;
		}
		if ((env_fan.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			if ((rmc_fan_r.fan_status[0].flag &
			    DP_FAN_PRESENCE) == 0)
				env_fan.sensor_status = ENVMON_NOT_PRESENT;
			if (rmc_fan_r.fan_status[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_fan.sensor_status |= ENVMON_INACCESSIBLE;
			if (env_fan.sensor_status == ENVMON_SENSOR_OK) {
				/*
				 * copy results into buffer for user
				 */
				env_fan.speed =
				    rmc_fan_r.fan_status[0].speed;
				env_fan.lowthresholds.warning =
				    rmc_fan_r.fan_status[0].minspeed;
				env_fan.lowthresholds.shutdown =
				    ENVMON_VAL_UNAVAILABLE;
				env_fan.lowthresholds.poweroff =
				    ENVMON_VAL_UNAVAILABLE;
				if ((rmc_fan_r.fan_status[0].flag &
				    DP_FAN_SPEED_VAL_UNIT) == 0)
					bcopy(str_rpm, env_fan.units,
					    sizeof (str_rpm));
				else
					bcopy(str_percent, env_fan.units,
					    sizeof (str_percent));
			}
		}
		if (env_fan.sensor_status != ENVMON_SENSOR_OK ||
		    rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE)
			set_fan_unav(&env_fan);

		if (ddi_copyout((caddr_t)&env_fan, (caddr_t)arg,
		    sizeof (envmon_fan_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCAMPIND:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_ind,
		    sizeof (envmon_indicator_t), mode) != 0)
			return (EFAULT);

		/* see if we've got amp indicator handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_AMP_IND)) == NULL)) {
			RELEASE_CACHE
			return (do_psu_cmd(arg, mode, &env_ind, &rmc_psu,
			    &rmc_psu_r, RMCLOMV_AMP_IND));
		} else if (env_ind.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0) {
				RELEASE_CACHE
				return (do_psu_cmd(arg, mode, &env_ind,
				    &rmc_psu, &rmc_psu_r, RMCLOMV_AMP_IND));
			}
			env_ind.next_id = section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_ind.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_ind.id.name,
			    &index) != 0) {
				RELEASE_CACHE
				return (do_psu_cmd(arg, mode, &env_ind,
				    &rmc_psu, &rmc_psu_r, RMCLOMV_AMP_IND));
			}
			if (index + 1 < section->num_entries) {
				env_ind.next_id =
				    section->entry[index + 1].handle_name;
			} else {
				rmclomv_cache_section_t	*sub_section =
				    rmclomv_find_section(rmclomv_subcache,
				    RMCLOMV_AMP_IND);
				if ((sub_section == NULL) ||
				    (sub_section->num_entries == 0))
					env_ind.next_id.name[0] = '\0';
				else
					env_ind.next_id =
					    sub_section->entry[0].handle_name;
			}
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified an indicator, note its
			 * handle value and request the indicator status
			 */
			rmc_ampi.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_CIRCUIT_BRKS, DP_GET_CIRCUIT_BRKS_R,
		    sizeof (rmc_ampi_r), (intptr_t)&rmc_ampi,
		    (intptr_t)&rmc_ampi_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmc_ampi_r.circuit_brk_status[0].sensor_status ==
		    DP_SENSOR_NOT_PRESENT)) {
			sensor_status = ENVMON_NOT_PRESENT;
		}
		if ((env_ind.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			/*
			 * copy results into buffer for user
			 */
			if (rmc_ampi_r.circuit_brk_status[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_ind.sensor_status = ENVMON_INACCESSIBLE;
			env_ind.condition =
			    rmc_ampi_r.circuit_brk_status[0].status;
		}

		/*
		 * If rmclomv_rmc_error is set there is no way
		 * that we read information from RSC. Just copy
		 * out an inaccessible evironmental.
		 */
		if (rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE) {
			env_ind.sensor_status = ENVMON_INACCESSIBLE;
			env_ind.condition = ENVMON_INACCESSIBLE;
		}

		if (ddi_copyout((caddr_t)&env_ind, (caddr_t)arg,
		    sizeof (envmon_indicator_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCHPU:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_hpu,
		    sizeof (envmon_hpu_t), mode) != 0)
			return (EFAULT);

		/* see if we've got hpu handles cached */
		LOCK_CACHE

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_HPU_IND)) == NULL)) {
			RELEASE_CACHE
			return (EAGAIN);
		}

		/*
		 * At this point the cache is locked and section points to
		 * the section relating to hpus.
		 */
		sensor_status = ENVMON_SENSOR_OK;
		if (env_hpu.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_hpu.next_id.name[0] = '\0';
			else
				env_hpu.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_hpu.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_hpu.id.name,
			    &index) != 0) {
				env_hpu.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_hpu.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_hpu.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified an hpu, note its
			 * handle value and request the hpu status
			 */
			rmc_fru.handle = section->entry[index].handle;
			special = section->entry[index].ind_mask;
		}
		RELEASE_CACHE
		if ((env_hpu.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			env_hpu.fru_status = ENVMON_FRU_PRESENT;

			if (special != 0) {
				/* this is the pseudo SC node */
				mutex_enter(&rmclomv_state_lock);
				switch (rmclomv_rmc_state) {
				case RMCLOMV_RMCSTATE_OK:
					break;
				case RMCLOMV_RMCSTATE_FAILED:
					env_hpu.fru_status = ENVMON_FRU_FAULT;
					break;
				case RMCLOMV_RMCSTATE_DOWNLOAD:
					env_hpu.fru_status =
					    ENVMON_FRU_DOWNLOAD;
					break;
				default:
					env_hpu.sensor_status =
					    ENVMON_INACCESSIBLE;
					break;
				}
				mutex_exit(&rmclomv_state_lock);
			} else if (rmclomv_rmc_error ||
			    rmclomv_do_cmd(DP_GET_FRU_STATUS,
			    DP_GET_FRU_STATUS_R, sizeof (rmc_fru_r),
			    (intptr_t)&rmc_fru, (intptr_t)&rmc_fru_r) != 0) {
				env_hpu.sensor_status = ENVMON_INACCESSIBLE;
			} else {
				/*
				 * copy results into buffer for user
				 */
				if (rmc_fru_r.fru_status[0].presence == 0) {
					env_hpu.sensor_status =
					    ENVMON_NOT_PRESENT;
					env_hpu.fru_status =
					    ENVMON_FRU_NOT_PRESENT;
				} else if (rmc_fru_r.fru_status[0].sensor_status
				    != DP_SENSOR_DATA_AVAILABLE) {
					env_hpu.sensor_status =
					    ENVMON_INACCESSIBLE;
				} else {
					uint8_t status =
					    rmc_fru_r.fru_status[0].status;
					if (status == DP_FRU_STATUS_UNKNOWN) {
						env_hpu.sensor_status =
						    ENVMON_INACCESSIBLE;
					} else if (status != DP_FRU_STATUS_OK) {
						env_hpu.fru_status =
						    ENVMON_FRU_FAULT;
					}
				}
			}
		}

		/*
		 * If rmclomv_rmc_error is set there is no way
		 * that we read information from RSC. Just copy
		 * out an inaccessible environmental.
		 */
		if (rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE) {
			env_hpu.sensor_status = ENVMON_INACCESSIBLE;
			env_hpu.fru_status = ENVMON_INACCESSIBLE;
		}

		if (ddi_copyout((caddr_t)&env_hpu, (caddr_t)arg,
		    sizeof (envmon_hpu_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCGETLED:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_ledinfo,
		    sizeof (envmon_led_info_t), mode) != 0)
			return (EFAULT);

		/* see if we've got LED handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_LED_IND)) == NULL)) {
			env_ledinfo.next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (env_ledinfo.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_ledinfo.next_id.name[0] = '\0';
			else
				env_ledinfo.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_ledinfo.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_ledinfo.id.name,
			    &index) != 0) {
				env_ledinfo.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_ledinfo.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_ledinfo.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified a LED, note its
			 * handle value and request the LED status
			 */
			rmc_led.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_LED_STATE, DP_GET_LED_STATE_R,
		    sizeof (rmc_led_r), (intptr_t)&rmc_led,
		    (intptr_t)&rmc_led_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmc_led_r.led_state[0].sensor_status ==
		    DP_SENSOR_NOT_PRESENT)) {
			sensor_status = ENVMON_NOT_PRESENT;
		}
		if ((env_ledinfo.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			/*
			 * copy results into buffer for user
			 * start with some defaults then override
			 */
			env_ledinfo.sensor_status = ENVMON_SENSOR_OK;
			env_ledinfo.led_state = ENVMON_LED_OFF;
			env_ledinfo.led_color = ENVMON_LED_CLR_NONE;

			if (rmc_led_r.led_state[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_ledinfo.sensor_status = ENVMON_INACCESSIBLE;
			else {
				dp_led_state_t ledState;
				ledState = rmc_led_r.led_state[0];
				env_ledinfo.led_color = (int8_t)ledState.colour;

				switch (ledState.state) {
				case (rsci8)DP_LED_OFF:
					break;
				case (rsci8)DP_LED_ON:
					env_ledinfo.led_state = ENVMON_LED_ON;
					break;
				case (rsci8)DP_LED_BLINKING:
					env_ledinfo.led_state =
					    ENVMON_LED_BLINKING;
					break;
				case (rsci8)DP_LED_FLASHING:
					env_ledinfo.led_state =
					    ENVMON_LED_FLASHING;
					break;
				default:
					break;
				}
			}
		}

		/*
		 * If rmclomv_rmc_error is set there is no way
		 * that we read information from RSC. Just copy
		 * out an inaccessible environmental.
		 */
		if (rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE) {
			env_ledinfo.sensor_status = ENVMON_INACCESSIBLE;
			env_ledinfo.led_state = ENVMON_INACCESSIBLE;
		}

		if (ddi_copyout((caddr_t)&env_ledinfo, (caddr_t)arg,
		    sizeof (envmon_led_info_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCSETLED:
		if ((mode & FWRITE) == 0)
			return (EACCES);
		if (drv_priv(cred_p) != 0)
			return (EPERM);
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_ledctl,
		    sizeof (envmon_led_ctl_t), mode) != 0)
			return (EFAULT);
		if (env_ledctl.led_state < RMCLOMV_MIN_LED_STATE ||
		    env_ledctl.led_state > RMCLOMV_MAX_LED_STATE)
			return (EINVAL);
		/*
		 * Ensure name is properly terminated.
		 */
		env_ledctl.id.name[ENVMON_MAXNAMELEN - 1] = '\0';

		/* see if we've got LED handles cached */
		LOCK_CACHE

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_LED_IND)) == NULL) ||
		    (get_sensor_by_name(section, env_ledctl.id.name,
		    &index) != 0)) {
			RELEASE_CACHE
			return (EINVAL);	/* no such LED */
		}
		/*
		 * user correctly identified a LED, note its handle value
		 */
		rmc_setled.handle = section->entry[index].handle;
		RELEASE_CACHE
		switch (env_ledctl.led_state) {
		case ENVMON_LED_ON:
			rmc_setled.state = DP_LED_ON;
			break;
		case ENVMON_LED_BLINKING:
			rmc_setled.state = DP_LED_BLINKING;
			break;
		case ENVMON_LED_FLASHING:
			rmc_setled.state = DP_LED_FLASHING;
			break;
		default:
			rmc_setled.state = DP_LED_OFF;
			break;
		}
		retval = rmclomv_do_cmd(DP_SET_LED_STATE, DP_SET_LED_STATE_R,
		    sizeof (rmc_setled_r), (intptr_t)&rmc_setled,
		    (intptr_t)&rmc_setled_r);

		if (retval != 0) {
			break;
		}

		if (rmc_setled_r.status != 0) {
			cmn_err(CE_WARN, "ENVMONIOCSETLED: \"%s\" status: 0x%x",
			    env_ledctl.id.name, rmc_setled_r.status);
			return (EIO);
		}
		break;

	case ENVMONIOCGETKEYSW:
	{
		enum rmc_keyswitch_pos	rmc_pos = real_key_position;
		envmon_keysw_pos_t	envmon_pos;

		/*
		 * Yes, I know this is ugly, but the V210 has no keyswitch,
		 * even though the ALOM returns a value for it
		 */
		if (strcmp(platform, "SUNW,Sun-Fire-V210") == 0) {
			return (ENOTSUP);
		}

		switch (rmc_pos) {

		case RMC_KEYSWITCH_POS_NORMAL:
			envmon_pos = ENVMON_KEYSW_POS_NORMAL;
			break;
		case RMC_KEYSWITCH_POS_DIAG:
			envmon_pos = ENVMON_KEYSW_POS_DIAG;
			break;
		case RMC_KEYSWITCH_POS_LOCKED:
			envmon_pos = ENVMON_KEYSW_POS_LOCKED;
			break;
		case RMC_KEYSWITCH_POS_OFF:
			envmon_pos = ENVMON_KEYSW_POS_OFF;
			break;
		default:
			envmon_pos = ENVMON_KEYSW_POS_UNKNOWN;
			break;
		}

		if (ddi_copyout((caddr_t)&envmon_pos, (caddr_t)arg,
		    sizeof (envmon_pos), mode) != 0)
			return (EFAULT);
		break;
	}

	case ENVMONIOCGETALARM:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_alarminfo,
		    sizeof (envmon_alarm_info_t), mode) != 0)
			return (EFAULT);

		/* see if we've got ALARM handles cached */
		LOCK_CACHE
		sensor_status = ENVMON_SENSOR_OK;

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_ALARM_IND)) == NULL)) {
			env_alarminfo.next_id.name[0] = '\0';
			sensor_status = ENVMON_NOT_PRESENT;
		} else if (env_alarminfo.id.name[0] == '\0') {
			/* request for first handle */
			if (section->num_entries == 0)
				env_alarminfo.next_id.name[0] = '\0';
			else
				env_alarminfo.next_id =
				    section->entry[0].handle_name;
			sensor_status = ENVMON_NOT_PRESENT;
		} else {
			/* ensure name is properly terminated */
			env_alarminfo.id.name[ENVMON_MAXNAMELEN - 1] = '\0';
			if (get_sensor_by_name(section, env_alarminfo.id.name,
			    &index) != 0) {
				env_alarminfo.next_id.name[0] = '\0';
				sensor_status = ENVMON_NOT_PRESENT;
			} else if (index + 1 < section->num_entries)
				env_alarminfo.next_id =
				    section->entry[index + 1].handle_name;
			else
				env_alarminfo.next_id.name[0] = '\0';
		}
		if (sensor_status == ENVMON_SENSOR_OK) {
			/*
			 * user correctly identified a ALARM, note its
			 * handle value and request the ALARM status
			 */
			rmc_alarm.handle = section->entry[index].handle;
		}
		RELEASE_CACHE
		if ((sensor_status == ENVMON_SENSOR_OK) &&
		    (rmclomv_rmc_error ||
		    rmclomv_do_cmd(DP_GET_ALARM_STATE, DP_GET_ALARM_STATE_R,
		    sizeof (rmc_alarm_r), (intptr_t)&rmc_alarm,
		    (intptr_t)&rmc_alarm_r) != 0)) {
			sensor_status = ENVMON_INACCESSIBLE;
		}
		if ((env_alarminfo.sensor_status = sensor_status) ==
		    ENVMON_SENSOR_OK) {
			/*
			 * copy results into buffer for user
			 * start with some defaults then override
			 */
			env_alarminfo.sensor_status = ENVMON_SENSOR_OK;
			env_alarminfo.alarm_state = ENVMON_ALARM_OFF;

			if (rmc_alarm_r.alarm_state[0].sensor_status !=
			    DP_SENSOR_DATA_AVAILABLE)
				env_alarminfo.sensor_status =
				    ENVMON_INACCESSIBLE;
			else {
				dp_alarm_state_t alarmState;
				alarmState = rmc_alarm_r.alarm_state[0];

				switch (alarmState.state) {
				case DP_ALARM_OFF:
					break;
				case DP_ALARM_ON:
					env_alarminfo.alarm_state =
					    ENVMON_ALARM_ON;
					break;
				default:
					break;
				}
			}
		}

		/*
		 * If rmclomv_rmc_error is set there is no way
		 * that we read information from RSC. Just copy
		 * out an inaccessible environmental.
		 */
		if (rmclomv_rmc_error != RMCLOMV_RMCERROR_NONE) {
			env_alarminfo.sensor_status = ENVMON_INACCESSIBLE;
			env_alarminfo.alarm_state = ENVMON_INACCESSIBLE;
		}

		if (ddi_copyout((caddr_t)&env_alarminfo, (caddr_t)arg,
		    sizeof (envmon_alarm_info_t), mode) != 0)
			return (EFAULT);
		break;

	case ENVMONIOCSETALARM:
		if ((mode & FWRITE) == 0)
			return (EACCES);
		if (drv_priv(cred_p) != 0)
			return (EPERM);
		if (ddi_copyin((caddr_t)arg, (caddr_t)&env_alarmctl,
		    sizeof (envmon_alarm_ctl_t), mode) != 0)
			return (EFAULT);
		if (env_alarmctl.alarm_state < RMCLOMV_MIN_ALARM_STATE ||
		    env_alarmctl.alarm_state > RMCLOMV_MAX_ALARM_STATE)
			return (EINVAL);
		/*
		 * Ensure name is properly terminated.
		 */
		env_alarmctl.id.name[ENVMON_MAXNAMELEN - 1] = '\0';

		/* see if we've got ALARM handles cached */
		LOCK_CACHE

		if ((rmclomv_cache_valid == B_FALSE) ||
		    ((section = rmclomv_find_section(rmclomv_cache,
		    RMCLOMV_ALARM_IND)) == NULL) ||
		    (get_sensor_by_name(section, env_alarmctl.id.name,
		    &index) != 0)) {
			RELEASE_CACHE
			return (EINVAL);	/* no such ALARM */
		}
		/*
		 * user correctly identified a ALARM, note its handle value
		 */
		rmc_setalarm.handle = section->entry[index].handle;
		RELEASE_CACHE
		rmc_setalarm.state = (rsci8)env_alarmctl.alarm_state;
		retval = rmclomv_do_cmd(DP_SET_ALARM_STATE,
		    DP_SET_ALARM_STATE_R,
		    sizeof (rmc_setalarm_r),
		    (intptr_t)&rmc_setalarm,
		    (intptr_t)&rmc_setalarm_r);

		if (retval != 0) {
			break;
		}

		if (rmc_setalarm_r.status != 0) {
			cmn_err(CE_WARN, "ENVMONIOCSETALARM: \"%s\" status: "
			    "0x%x", env_alarmctl.id.name,
			    rmc_setalarm_r.status);
			return (EIO);
		}
		break;

	case ENVMONIOCCHASSISSERIALNUM:
		retval = rmclomv_do_cmd(DP_GET_SDP_VERSION,
		    DP_GET_SDP_VERSION_R, sizeof (rmc_sdpver_r),
		    NULL, (intptr_t)&rmc_sdpver_r);

		if (retval != 0) {
			cmn_err(CE_WARN, "DP_GET_SDP_VERSION failed, ret=%d\n",
			    retval);
			break;
		} else if (rmc_sdpver_r.version < SDP_RESPONDS_TO_ALL_CMDS) {
			retval = ENOTSUP;
			break;
		}
		retval = rmclomv_do_cmd(DP_GET_CHASSIS_SERIALNUM,
		    DP_GET_CHASSIS_SERIALNUM_R, sizeof (rmc_serialnum_r),
		    NULL, (intptr_t)&rmc_serialnum_r);

		if (retval != 0) {
			break;
		}
		bcopy(rmc_serialnum_r.chassis_serial_number,
		    chassis.serial_number,
		    sizeof (rmc_serialnum_r.chassis_serial_number));

		if (ddi_copyout((caddr_t)&chassis, (caddr_t)arg,
		    sizeof (chassis), mode) != 0) {
			return (EFAULT);
		}
		sensor_status = ENVMON_SENSOR_OK;
		break;

	default:
		retval = ENOTSUP;
		break;
	}

	return (retval);
}

/* ARGSUSED */
static void
rmclomv_checkrmc(caddr_t arg)
{
	callb_cpr_t		cprinfo;
	int			err;
	int			retries;
	int			state;
	dp_get_sysinfo_r_t 	sysinfo;

	CALLB_CPR_INIT(&cprinfo, &rmclomv_checkrmc_lock, callb_generic_cpr,
	    "rmclomv_checkrmc");

	mutex_enter(&rmclomv_checkrmc_lock);
	for (;;) {
		/*
		 * Initial entry to this for loop is made with
		 * rmclomv_checkrmc_sig set to RMCLOMV_PROCESS_NOW. So the
		 * following while loop drops through the first time. A
		 * timeout call is made just before polling the RMC. Its
		 * interrupt routine sustains this loop by injecting additional
		 * state changes and cv events.
		 */
		/*
		 * Wait for someone to tell me to continue.
		 */
		while (rmclomv_checkrmc_sig == RMCLOMV_CHECKRMC_WAIT) {
			CALLB_CPR_SAFE_BEGIN(&cprinfo);
			cv_wait(&rmclomv_checkrmc_sig_cv,
			    &rmclomv_checkrmc_lock);
			CALLB_CPR_SAFE_END(&cprinfo, &rmclomv_checkrmc_lock);
		}

		mutex_exit(&rmclomv_checkrmc_lock);
		/*
		 * mustn't hold same lock as timeout called with
		 * when cancelling timer
		 */
		if (timer_id != 0) {
			(void) untimeout(timer_id);
			timer_id = 0;
		}
		mutex_enter(&rmclomv_checkrmc_lock);

		/* RMCLOMV_CHECKRMC_EXITNOW implies signal by _detach(). */
		if (rmclomv_checkrmc_sig == RMCLOMV_CHECKRMC_EXITNOW) {
			rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_WAIT;

			/* rmclomv_checkrmc_lock is held at this point! */
			CALLB_CPR_EXIT(&cprinfo);

			thread_exit();
			/* NOTREACHED */
		}

		rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_WAIT;

		/*
		 * If the RMC is not responding, rmclomv_do_cmd() takes a
		 * long time and eventually times out. We conclude that the
		 * RMC is broken if it doesn't respond to a number of polls
		 * made 60 secs apart. So that the rmclomv_do_cmd() time-out
		 * period isn't added to our 60 second timer, make the
		 * timeout() call before calling rmclomv_do_cmd().
		 */
		if (timer_id == 0) {
			timer_id = timeout(rmclomv_checkrmc_wakeup, NULL,
			    60 * drv_usectohz(1000000));
		}

		mutex_exit(&rmclomv_checkrmc_lock);

		err = rmclomv_do_cmd(DP_GET_SYSINFO, DP_GET_SYSINFO_R,
		    sizeof (sysinfo), NULL, (intptr_t)&sysinfo);
		if (err == 0) {
			mutex_enter(&rmclomv_state_lock);
			state = rmclomv_rmc_state;
			/* successful poll, reset fail count */
			rmclomv_rmcfailcount = 0;
			mutex_exit(&rmclomv_state_lock);

			if (state != RMCLOMV_RMCSTATE_OK) {
				rmclomv_refresh_wakeup();
			}
		}
		if ((err != 0) &&
		    (rmclomv_rmc_error != RMCLOMV_RMCSTATE_DOWNLOAD)) {
			/*
			 * Failed response or no response from RMC.
			 * Count the failure.
			 * If threshold exceeded, send a DR event.
			 */
			mutex_enter(&rmclomv_state_lock);
			retries = rmclomv_rmcfailcount;
			state = rmclomv_rmc_state;
			if (retries == RMCLOMV_RMCFAILTHRESHOLD)
				rmclomv_rmc_state = RMCLOMV_RMCSTATE_FAILED;
			if (rmclomv_rmcfailcount <= RMCLOMV_RMCFAILTHRESHOLD)
				rmclomv_rmcfailcount++;
			mutex_exit(&rmclomv_state_lock);

			if (retries == RMCLOMV_RMCFAILTHRESHOLD) {
				cmn_err(CE_WARN, "SC %s responding",
				    state == RMCLOMV_RMCSTATE_OK ?
				    "has stopped" : "is not");
				refresh_name_cache(B_TRUE);
				rmclomv_dr_data_handler(str_sc, SE_NO_HINT);
			}
		}

		/*
		 * Re-enter the lock to prepare for another iteration.
		 * We must have the lock here to protect rmclomv_checkrmc_sig.
		 */
		mutex_enter(&rmclomv_checkrmc_lock);
	}
}

static void
rmclomv_checkrmc_start(void)
{
	kthread_t *tp;

	mutex_enter(&rmclomv_checkrmc_lock);

	if (rmclomv_checkrmc_tid == 0) {
		rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_PROCESSNOW;

		tp = thread_create(NULL, 0, rmclomv_checkrmc, NULL, 0,
		    &p0, TS_RUN, maxclsyspri);
		rmclomv_checkrmc_tid = tp->t_did;
	}

	mutex_exit(&rmclomv_checkrmc_lock);
}

static void
rmclomv_checkrmc_destroy(void)
{
	kt_did_t tid;

	mutex_enter(&rmclomv_checkrmc_lock);
	tid = rmclomv_checkrmc_tid;
	if (tid != 0) {
		rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_EXITNOW;
		cv_signal(&rmclomv_checkrmc_sig_cv);
		rmclomv_checkrmc_tid = 0;
	}
	mutex_exit(&rmclomv_checkrmc_lock);

	/*
	 * Wait for rmclomv_checkrmc() to finish
	 */
	if (tid != 0)
		thread_join(tid);
}

/*ARGSUSED*/
static void
rmclomv_checkrmc_wakeup(void *arg)
{
	mutex_enter(&rmclomv_checkrmc_lock);

	if (rmclomv_checkrmc_sig != RMCLOMV_CHECKRMC_EXITNOW)
		rmclomv_checkrmc_sig = RMCLOMV_CHECKRMC_PROCESSNOW;
	cv_signal(&rmclomv_checkrmc_sig_cv);

	mutex_exit(&rmclomv_checkrmc_lock);
}

/* ARGSUSED */
static void
rmclomv_refresh(caddr_t arg)
{
	void			(*plat_nodename_set_fun)(void);
	sig_state_t		*current_sgn_p;
	callb_cpr_t		cprinfo;
	int			state;
	int			tmp_checkrmc_sig;

	CALLB_CPR_INIT(&cprinfo, &rmclomv_refresh_lock, callb_generic_cpr,
	    "rmclomv_refresh");

	/*
	 * Wait until the rmclomv_checkrmc() thread has had a chance to
	 * run its main loop.  This is done so that rmclomv_refresh will
	 * only run its main loop once at start of day; otherwise, it may
	 * run twice and generate warning messages when redundantly populating
	 * its internal cache.
	 */
	do {
		delay(drv_usectohz(DELAY_TIME));
		mutex_enter(&rmclomv_checkrmc_lock);
		tmp_checkrmc_sig = rmclomv_checkrmc_sig;
		mutex_exit(&rmclomv_checkrmc_lock);
	} while (tmp_checkrmc_sig != RMCLOMV_CHECKRMC_WAIT);

	mutex_enter(&rmclomv_refresh_lock);
	for (;;) {

		/*
		 * Wait for someone to tell me to continue.
		 */
		while (rmclomv_refresh_sig == RMCLOMV_REFRESH_WAIT) {
			CALLB_CPR_SAFE_BEGIN(&cprinfo);
			cv_wait(&rmclomv_refresh_sig_cv, &rmclomv_refresh_lock);
			CALLB_CPR_SAFE_END(&cprinfo, &rmclomv_refresh_lock);
		}

		/* RMCLOMV_REFRESH_EXITNOW implies signal by _detach(). */
		if (rmclomv_refresh_sig == RMCLOMV_REFRESH_EXITNOW) {
			rmclomv_refresh_sig = RMCLOMV_REFRESH_WAIT;

			/* rmclomv_refresh_lock is held at this point! */
			CALLB_CPR_EXIT(&cprinfo);

			thread_exit();
			/* NOTREACHED */
		}

		ASSERT(rmclomv_refresh_sig == RMCLOMV_REFRESH_PROCESSNOW);
		rmclomv_refresh_sig = RMCLOMV_REFRESH_WAIT;

		mutex_exit(&rmclomv_refresh_lock);

		refresh_name_cache(B_FALSE);

		/*
		 * We're not going to access rmclomv_sysinfo_data here,
		 * so there's no point in locking it before reading
		 * rmclomv_sysinfo_valid. Also this avoids holding two
		 * locks at once and the concommitant worry about deadlocks.
		 */
		if (rmclomv_sysinfo_valid) {
			/*
			 * We've just successfully read the RMC sysinfo
			 * so the RMC must be operational. Update its
			 * state and if it was previously not OK, refresh
			 * nodename, CPU signatures and watchdog settings.
			 */
			mutex_enter(&rmclomv_state_lock);
			rmclomv_rmcfailcount = 0;
			state = rmclomv_rmc_state;
			rmclomv_rmc_state = RMCLOMV_RMCSTATE_OK;
			mutex_exit(&rmclomv_state_lock);

			if (state != RMCLOMV_RMCSTATE_OK) {
				rmclomv_dr_data_handler(str_sc, SE_NO_HINT);
				if (state == RMCLOMV_RMCSTATE_FAILED) {
					cmn_err(CE_NOTE, "SC recovered");
				}
			}

			if (utsname.nodename[0] != 0) {
				plat_nodename_set_fun =
				    (void (*)(void))modgetsymvalue(
				    "plat_nodename_set", 0);
				if (plat_nodename_set_fun != NULL)
					plat_nodename_set_fun();
			}

			current_sgn_p = (sig_state_t *)modgetsymvalue(
			    "current_sgn", 0);

			/*
			 * Delay before calling CPU_SIGNATURE, to allow
			 * any pending asynchronous communications (i.e.
			 * plat_timesync()) to complete.  This helps to
			 * prevent the situation where the message associated
			 * with the CPU_SIGNATURE state cannot be sent to the
			 * system controller.
			 */
			if ((current_sgn_p != NULL) &&
			    (current_sgn_p->state_t.sig != 0)) {
				delay(drv_usectohz(CPU_SIGNATURE_DELAY_TIME));
				CPU_SIGNATURE(current_sgn_p->state_t.sig,
				    current_sgn_p->state_t.state,
				    current_sgn_p->state_t.sub_state, -1);

				if (!(boothowto & RB_DEBUG)) {
					/*
					 * Delay before calling
					 * send_watchdog_msg, to allow
					 * CPU_SIGNATURE() time to
					 * complete; this increases the
					 * chances of successfully sending
					 * the watchdog message to the
					 * system controller.
					 */
					delay(drv_usectohz(
					    CPU_SIGNATURE_DELAY_TIME));
					send_watchdog_msg(last_watchdog_msg);
				}
			}
		}

		/*
		 * update keyswitch value in case it changed while the
		 * RMC was out of action
		 */
		LOCK_CACHE
		if (rmclomv_sysinfo_valid) {
			real_key_position = rmclomv_sysinfo_data.keyswitch;
			if ((real_key_position != RMC_KEYSWITCH_POS_UNKNOWN) &&
			    (real_key_position <= RMC_KEYSWITCH_POS_OFF)) {
				key_position = real_key_position;
			} else {
				/* treat unknown key position as locked */
				key_position = RMC_KEYSWITCH_POS_LOCKED;
			}
		} else {
			/* treat unreadable key position as locked */
			key_position = RMC_KEYSWITCH_POS_LOCKED;
			real_key_position = RMC_KEYSWITCH_POS_UNKNOWN;
		}
		RELEASE_CACHE

		/*
		 * Re-enter the lock to prepare for another iteration.
		 * We must have the lock here to protect rmclomv_refresh_sig.
		 */
		mutex_enter(&rmclomv_refresh_lock);
	}
}

static void
rmclomv_refresh_start(void)
{
	kthread_t *tp;

	mutex_enter(&rmclomv_refresh_lock);

	if (rmclomv_refresh_tid == 0) {
		rmclomv_refresh_sig = RMCLOMV_REFRESH_PROCESSNOW;

		tp = thread_create(NULL, 0, rmclomv_refresh, NULL, 0,
		    &p0, TS_RUN, maxclsyspri);
		rmclomv_refresh_tid = tp->t_did;
	}

	mutex_exit(&rmclomv_refresh_lock);
}

static void
rmclomv_refresh_destroy(void)
{
	kt_did_t tid;

	mutex_enter(&rmclomv_refresh_lock);
	tid = rmclomv_refresh_tid;
	if (tid != 0) {
		rmclomv_refresh_sig = RMCLOMV_REFRESH_EXITNOW;
		cv_signal(&rmclomv_refresh_sig_cv);
		rmclomv_refresh_tid = 0;
	}
	mutex_exit(&rmclomv_refresh_lock);

	/*
	 * Wait for rmclomv_refresh() to finish
	 */
	if (tid != 0)
		thread_join(tid);
}

static void
rmclomv_refresh_wakeup(void)
{
	mutex_enter(&rmclomv_refresh_lock);

	if (rmclomv_refresh_sig != RMCLOMV_REFRESH_EXITNOW)
		rmclomv_refresh_sig = RMCLOMV_REFRESH_PROCESSNOW;
	cv_signal(&rmclomv_refresh_sig_cv);

	mutex_exit(&rmclomv_refresh_lock);
}

static void
send_watchdog_msg(int msg)
{
	rmc_comm_msg_t request;
	dp_set_host_watchdog_t watchdog_msg;

	if (rmclomv_watchdog_mode)
		return;

	watchdog_msg.enable = msg;
	request.msg_type = DP_SET_HOST_WATCHDOG;
	request.msg_len = sizeof (watchdog_msg);
	request.msg_buf = (caddr_t)&watchdog_msg;
	(void) rmc_comm_request_nowait(&request, (msg == 1) ?
	    RMC_COMM_DREQ_URGENT : 0);
}

/*ARGSUSED*/
static uint_t
rmc_set_watchdog_timer(uint_t timeoutval)
{
	ASSERT(MUTEX_HELD(&tod_lock));

	if ((watchdog_enable == 0) || (watchdog_available == 0)) {
		return (0);
	}

	/*
	 * If boothowto has RB_DEBUG set we never want to set the watchdog
	 * support on.
	 */
	if (boothowto & RB_DEBUG) {
		return (0);
	}

	/*
	 * When the watchdog is shut off last_watchdog_msg goes from a
	 * 0 to a 1. So we must test to see that last_watchdog_msg is
	 * set to 1 indicating that watchdog was shut off and
	 * After which we set last_watchdog_msg back to 0 so that we do not
	 * run this code
	 * again.
	 */
	if (last_watchdog_msg == 1) {
		send_watchdog_msg(0);
		last_watchdog_msg = 0;
	}

	pmugpio_watchdog_pat();

	watchdog_activated = 1;

	return (1);
}

static uint_t
rmc_clear_watchdog_timer(void)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	if ((watchdog_activated == 0) || (boothowto & RB_DEBUG))
		return (0);

	send_watchdog_msg(1);
	last_watchdog_msg = 1;
	watchdog_activated = 0;

	return (0);
}

static void
plat_timesync(void *arg)
{
	timestruc_t now;
	todinfo_t tod;
	rmc_comm_msg_t request;
	dp_set_date_time_t set_time_msg;
	int retval;
	timestruc_t ts;
	dp_get_date_time_r_t *date_and_time_info;
	int buffer[DATE_TIME_MSG_SIZE];

	/* Is the system coming up? */
	if (arg != NULL) {
		/* Request the time from the RMC clock. */
		retval = rmclomv_do_cmd(DP_GET_DATE_TIME, DP_GET_DATE_TIME_R,
		    DATE_TIME_MSG_SIZE, NULL, (intptr_t)&buffer);

		/*
		 * If we were able to get the time lets set the local clock.
		 * The time returned from RMC is in Unix time format.
		 *
		 * If we couldn't get the time we'll accept the drift so as not
		 * to cause congestion on the I2C bus or cause boot
		 * performance regressions.
		 */
		if (retval == RCNOERR) {
			date_and_time_info = (dp_get_date_time_r_t *)buffer;
			ts.tv_sec = date_and_time_info->current_datetime;
			ts.tv_nsec = 0;
			mutex_enter(&tod_lock);
			tod_set(ts);
			set_hrestime(&ts);
			mutex_exit(&tod_lock);
		}
	}

	gethrestime(&now);
	mutex_enter(&tod_lock);
	tod = utc_to_tod(now.tv_sec);
	mutex_exit(&tod_lock);

	set_time_msg.year	= tod.tod_year;
	set_time_msg.month	= tod.tod_month - 1;
	set_time_msg.day	= tod.tod_day;
	set_time_msg.hour	= tod.tod_hour;
	set_time_msg.minute	= tod.tod_min;
	set_time_msg.second	= tod.tod_sec;

	request.msg_type = DP_SET_DATE_TIME;
	request.msg_len = sizeof (set_time_msg);
	request.msg_buf = (caddr_t)&set_time_msg;

	(void) rmc_comm_request_nowait(&request, 0);

	mutex_enter(&timesync_lock);
	if (timesync_interval != 0)
		timesync_tid = timeout(plat_timesync, NULL, timesync_interval);
	mutex_exit(&timesync_lock);
}

/*
 * Interfaces to get/set alarm relays from outside
 */
int
rmclomv_alarm_get(int alarm_type, int *alarm_state)
{
	rmclomv_cache_section_t	*section;
	int			index;
	uint16_t		sensor_status;
	dp_get_alarm_state_t	u_rmc_alarm;
	dp_get_alarm_state_r_t	u_rmc_alarm_r;

	/* see if we've got ALARM handles cached */
	LOCK_CACHE
	sensor_status = ENVMON_SENSOR_OK;

	if ((rmclomv_cache_valid == B_FALSE) ||
	    ((section = rmclomv_find_section(rmclomv_cache,
	    RMCLOMV_ALARM_IND)) == NULL)) {
		sensor_status = ENVMON_NOT_PRESENT;
	}
	if (sensor_status == ENVMON_SENSOR_OK) {
		/*
		 * user correctly identified a ALARM, note its
		 * handle value and request the ALARM status
		 */
		index = alarm_type;
		if (index >= section->num_entries)
			sensor_status = ENVMON_INACCESSIBLE;
		else
			u_rmc_alarm.handle = section->entry[index].handle;
	}
	RELEASE_CACHE
	if ((sensor_status == ENVMON_SENSOR_OK) && (rmclomv_rmc_error ||
	    rmclomv_do_cmd(DP_GET_ALARM_STATE, DP_GET_ALARM_STATE_R,
	    sizeof (u_rmc_alarm_r), (intptr_t)&u_rmc_alarm,
	    (intptr_t)&u_rmc_alarm_r) != 0)) {
		sensor_status = ENVMON_INACCESSIBLE;
	}
	if (sensor_status == ENVMON_SENSOR_OK) {
		/*
		 * copy results into buffer for user
		 * start with some defaults then override
		 */
		*alarm_state = 0;

		if (u_rmc_alarm_r.alarm_state[0].sensor_status !=
		    DP_SENSOR_DATA_AVAILABLE)
			return (ENXIO);
		else {
			dp_alarm_state_t alarmState;
			alarmState = u_rmc_alarm_r.alarm_state[0];

			switch (alarmState.state) {
			case DP_ALARM_OFF:
				break;
			case DP_ALARM_ON:
				*alarm_state = 1;
				break;
			default:
				break;
			}
		}
	} else
		return (ENXIO);

	return (0);
}

int
rmclomv_alarm_set(int alarm_type, int new_state)
{
	rmclomv_cache_section_t	*section;
	int			index;
	uint16_t		sensor_status;
	dp_set_alarm_state_t	u_rmc_setalarm;
	dp_set_alarm_state_r_t	u_rmc_setalarm_r;

	/* see if we've got ALARM handles cached */
	LOCK_CACHE
	sensor_status = ENVMON_SENSOR_OK;

	if ((rmclomv_cache_valid == B_FALSE) ||
	    ((section = rmclomv_find_section(rmclomv_cache,
	    RMCLOMV_ALARM_IND)) == NULL)) {
		sensor_status = ENVMON_NOT_PRESENT;
	}
	if (sensor_status == ENVMON_SENSOR_OK) {
		/*
		 * user correctly identified a ALARM, note its
		 * handle value and request the ALARM status
		 */
		index = alarm_type;
		if (index >= section->num_entries)
			sensor_status = ENVMON_INACCESSIBLE;
		else {
			u_rmc_setalarm.handle = section->entry[index].handle;
			u_rmc_setalarm.state = new_state;
		}
	}
	RELEASE_CACHE
	if ((sensor_status == ENVMON_SENSOR_OK) &&
	    (rmclomv_rmc_error ||
	    rmclomv_do_cmd(DP_SET_ALARM_STATE, DP_SET_ALARM_STATE_R,
	    sizeof (u_rmc_setalarm_r), (intptr_t)&u_rmc_setalarm,
	    (intptr_t)&u_rmc_setalarm_r) != 0)) {
		sensor_status = ENVMON_INACCESSIBLE;
	}

	if (u_rmc_setalarm_r.status != DP_SET_ALARM_OK) {
		return (EIO);
	}

	if (sensor_status != ENVMON_SENSOR_OK) {
		return (ENXIO);
	}

	return (0);
}