File: mc.c

package info (click to toggle)
openipmi 2.0.16-1.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,592 kB
  • sloc: ansic: 135,418; sh: 9,807; python: 7,785; perl: 5,840; makefile: 546
file content (5362 lines) | stat: -rw-r--r-- 126,554 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
/*
 * mc.c
 *
 * MontaVista IPMI code for handling management controllers
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002,2003 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <OpenIPMI/ipmiif.h>
#include <OpenIPMI/ipmi_sdr.h>
#include <OpenIPMI/ipmi_msgbits.h>
#include <OpenIPMI/ipmi_err.h>
#include <OpenIPMI/ipmi_user.h>
#include <OpenIPMI/ipmi_mc.h>

#include <OpenIPMI/internal/locked_list.h>
#include <OpenIPMI/internal/opq.h>
#include <OpenIPMI/internal/ipmi_domain.h>
#include <OpenIPMI/internal/ipmi_mc.h>
#include <OpenIPMI/internal/ipmi_sel.h>
#include <OpenIPMI/internal/ipmi_oem.h>
#include <OpenIPMI/internal/ipmi_int.h>

#define MAX_SEL_TIME_SET_RETRIES 10

#undef DEBUG_INFO_TRACKING

/* Timer structure for rereading the SEL. */
typedef struct mc_reread_sel_s
{
    char                name[IPMI_MC_NAME_LEN+1];
    int                 timer_running;
    ipmi_lock_t         *lock;
    int                 cancelled;
    ipmi_mc_t           *mc;
    ipmi_mcid_t         mc_id;
    ipmi_sels_fetched_t handler;
    void                *cb_data;
    os_handler_t        *os_hnd;
    os_hnd_timer_id_t   *sel_timer;

    int                 timer_should_run;
    unsigned int        retries;
    int                 sel_time_set;
    int                 processing;

    ipmi_mc_ptr_cb sels_first_read_handler;
    void           *sels_first_read_cb_data;

#ifdef DEBUG_INFO_TRACKING
#define DIT_SIZE 100
#define DIT_LAST (DIT_SIZE-1)
    struct {
	int            line;
	const char     *filename;
	const char     *function;
    } last[DIT_SIZE];
#define DEBUG_INFO(info) (memcpy(info->last, info->last+1,	\
				 sizeof(info->last[0]) * (DIT_LAST)),	\
			  info->last[DIT_LAST].filename = __FILE__,	\
			  info->last[DIT_LAST].line = __LINE__,	\
			  info->last[DIT_LAST].function = __FUNCTION__)
#else
#define DEBUG_INFO(info)
#endif
} mc_reread_sel_t;


typedef struct mc_devid_data_s
{    
    uint8_t device_id;

    uint8_t device_revision;

    unsigned int provides_device_sdrs : 1;
    unsigned int device_available : 1;

    unsigned int chassis_support : 1;
    unsigned int bridge_support : 1;
    unsigned int IPMB_event_generator_support : 1;
    unsigned int IPMB_event_receiver_support : 1;
    unsigned int FRU_inventory_support : 1;
    unsigned int SEL_device_support : 1;
    unsigned int SDR_repository_support : 1;
    unsigned int sensor_device_support : 1;

    uint8_t major_fw_revision;
    uint8_t minor_fw_revision;

    uint8_t major_version;
    uint8_t minor_version;

    uint32_t manufacturer_id;
    uint16_t product_id;

    uint8_t  aux_fw_revision[4];
} mc_devid_data_t;

/*
 * The MC follows a state machine for it's status to keep reporting of
 * active/inactive/fully up sane.  It is driven by the following inputs:
 *
 * _ipmi_mc_handle_new - function call from domain code when MC is detected
 * _ipmi_cleanup_mc - function call from domain code when MC removal is
 *	detected
 * put_done - When the _ipmi_mc_put() calls cause the count to go to 0
 * startup_get - When a startup operation starts
 * startup_done - When a startup operation completes
 *
 * It has the following states:
 *
 * MC_INACTIVE - startup state
 *  _ipmi_mc_handle_new
 *	state = MC_INACTIVE_PEND_STARTUP
 *  _ipmi_cleanup_mc
 *	nil
 *  put_done
 *	nil
 *  startup_get
 *	nil
 *  startup_done
 *	nil
 *
 * MC_INACTIVE_PEND_STARTUP - A startup has been requested.  Wait for the
 * mc to be put_done and start up the MC.
 *  _ipmi_mc_handle_new
 *	nil
 *  _ipmi_cleanup_mc
 *	state = MC_INACTIVE
 *  put_done
 *	state = MC_ACTIVE_IN_STARTUP
 *	startup MC
 *      startup_count = 1
 *      startup_called = False
 *      active = 1
 *      call active handlers
 *  startup_get
 *	nil
 *  startup_done
 *	nil
 *
 * MC_ACTIVE_IN_STARTUP - MC is startup up
 *  _ipmi_mc_handle_new
 *	nil
 *  _ipmi_cleanup_mc
 *	state = MC_ACTIVE_PENDING_CLEANUP
 *  put_done
 *	nil
 *  startup_get
 *	startup_count++
 *  startup_done
 *	startup_count--
 *      if (startup_count == 0 and NOT startup_called)
 *        startup_called = True
 *	  state = MC_ACTIVE_PENDING_FULLY_UP
 *
 * MC_ACTIVE_PEND_FULLY_UP - We have gone fully up, waiting for
 * put_done to go to active and report fully up
 *  _ipmi_mc_handle_new
 *	nil
 *  _ipmi_cleanup_mc
 *	state = MC_ACTIVE_PENDING_CLEANUP
 *  put_done
 *	state = MC_ACTIVE
 *      call fully up handlers
 *  startup_get
 *	nil
 *  startup_done
 *	nil
 *
 * MC_ACTIVE - MC is fully operational
 *  _ipmi_mc_handle_new
 *	nil
 *  _ipmi_cleanup_mc
 *	state = MC_ACTIVE_PENDING_CLEANUP
 *  put_done
 *	nil
 *  startup_get
 *	nil
 *  startup_done
 *	nil
 *
 * MC_ACTIVE_PEND_CLEANUP - A cleanup has been requested, pending for
 * the startup_count to go to zero in a put_done.
 *  _ipmi_mc_handle_new
 *	state = MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP
 *  _ipmi_cleanup_mc
 *	nil
 *  put_done
 *	if (startup_count == 0)
 *        state = MC_INACTIVE
 *	  active = 0
 *	  cleanup MC
 *        call active handlers
 *  startup_get
 *	startup_count++
 *  startup_done
 *	startup_count--
 *
 * MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP - When we were pending close, an
 * _ipmi_mc_handle_new event came in.  We need to finish cleaning up
 * before we restart the MC.
 *  _ipmi_mc_handle_new
 *	nil
 *  _ipmi_cleanup_mc
 *	state = MC_ACTIVE_PENDING_CLEANUP
 *  put_done
 *	if (startup_count == 0)
 *        state = MC_INACTIVE
 *	  active = 0
 *	  cleanup MC
 *        call active handlers
 *        state = MC_ACTIVE_IN_STARTUP
 *	  active = 0
 *	  startup MC
 *        startup_count = True
 *        startup_called = False
 *        active = 1
 *        call active handlers
 *  startup_get
 *	startup_count++
 *  startup_done
 *	startup_count--
 *
 * A few notes:
 *
 * We don't do anything in the startup_get and startup_put operations
 * because the caller must be holding an mc and we want to wait until
 * the put operation to do anything.
 *
 * Same with the handle_new and cleanup calls
 *
 * For the user, you will never get a fully_up call while the MC is
 * inactive.
 */
typedef enum {
    MC_INACTIVE,
    MC_INACTIVE_PEND_STARTUP,
    MC_ACTIVE_IN_STARTUP,
    MC_ACTIVE_PEND_FULLY_UP,
    MC_ACTIVE,
    MC_ACTIVE_PEND_CLEANUP,
    MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP
} mc_state_e;

struct ipmi_mc_s
{
    unsigned int usecount;
    ipmi_lock_t *lock;

    int           in_destroy;

    ipmi_domain_t *domain;
    long          seq;
    ipmi_addr_t   addr;
    int           addr_len;

    mc_state_e state;

    /* How many startup items are pending? */
    unsigned int startup_count;
    int startup_reported;

    /* If we have any external users that do not have direct
       references, we increment the usercount.  This is primarily the
       internal uses in the active_handlers list, but we cannot use
       that list being empty because it also may have external
       users. */
    int usercount;

    /* If the MC is known to be good in the system, then active is
       true.  If active is false, that means that there are sensors
       that refer to this MC, but the MC is not currently in the
       system. */
    int active;

    /* Used to generate unique numbers for the MC. */
    unsigned int uniq_num;

    /* The device SDRs on the MC. */
    ipmi_sdr_info_t *sdrs;

    /* The sensors that came from the device SDR on this MC. */
    ipmi_sensor_t **sensors_in_my_sdr;
    unsigned int  sensors_in_my_sdr_count;

    /* The entities that came from the device SDR on this MC are
       somehow stored in this data structure. */
    void *entities_in_my_sdr;

    /* Sensors that this MC owns (you message this MC to talk to this
       sensor, and events report the MC as the owner. */
    ipmi_sensor_info_t  *sensors;

    ipmi_control_info_t *controls;

    unsigned int in_domain_list : 1; /* Tells if we are in the list of
					our domain yet. */

    /* The system event log, for querying and storing events. */
    ipmi_sel_info_t *sel;

    /* The handler to call for add/delete event operations.  This is NULL
       normally and is only used if the MC has a special delete event
       handler. */
    ipmi_mc_del_event_cb sel_del_event_handler;
    ipmi_mc_add_event_cb sel_add_event_handler;
    ipmi_mc_del_event_cb sel_clear_handler;

    /* Timer for rescanning the sel periodically. */
    mc_reread_sel_t   *sel_timer_info;
    unsigned int      sel_scan_interval; /* seconds between SEL scans */

    /* Is the global events enable for the MC enabled? */
    int events_enabled;

    /* The SEL time when the connection first came up.  Only used at
       startup, after the SEL has been read the first time this will
       be set to zero. */
    ipmi_time_t startup_SEL_time;

    /* The MC's GUID. */
    unsigned int  guid_set : 1;
    unsigned char guid[16];

    void *oem_data;

    ipmi_mc_oem_fixup_sdrs_cb fixup_sdrs_handler;
    void                      *fixup_sdrs_cb_data;

    ipmi_mc_oem_new_sensor_cb new_sensor_handler;
    void                      *new_sensor_cb_data;

    ipmi_oem_event_handler_cb oem_event_handler;
    void                      *oem_event_cb_data;

    ipmi_oem_event_handler_cb sel_oem_event_handler;
    void                      *sel_oem_event_cb_data;

    ipmi_mc_ptr_cb sdrs_first_read_handler;
    void           *sdrs_first_read_cb_data;

    /* Call these when the MC is destroyed. */
    locked_list_t *removed_handlers;

    /* Call these when the MC changes from active to inactive. */
    locked_list_t *active_handlers, *active_handlers_cl;

    /* Called after going active when the MC is fully up. */
    locked_list_t *fully_up_handlers, *fully_up_handlers_cl;

    /* Set if we are treating main SDRs like device SDRs. */
    int treat_main_as_device_sdrs;

    /* The rest is the actual data from the get device id and SDRs.
       There's the normal version, the pending version, and the
       version.  The real version is the one from the get device id
       response, and normal version may have been adjusted by the OEM
       code.  The pending version is used to hold the data until the
       usecount goes to 0; we don't change the user data until no one
       else is using it. */

    mc_devid_data_t devid;
    mc_devid_data_t real_devid;
    mc_devid_data_t pending_devid;
    int pending_devid_data;
    int pending_new_mc;

    /* Name used for reporting.  We add a ' ' onto the end, thus
       the +1. */
    char name[IPMI_MC_NAME_LEN+1];
};

/* Cna the MC do normal operations like check SDRs, fetch the SEL,
   etc?  Must be called with the MC lock held. */
#define mc_op_ready(mc) \
    (((mc)->state == MC_ACTIVE_IN_STARTUP) \
     || ((mc)->state == MC_ACTIVE_PEND_FULLY_UP) \
     || ((mc)->state == MC_ACTIVE))

static void mc_sel_new_event_handler(ipmi_sel_info_t *sel,
				     ipmi_mc_t       *mc,
				     ipmi_event_t    *event,
				     void            *cb_data);

static void sels_start_timer(mc_reread_sel_t *info);
static void start_sel_time_set(ipmi_mc_t *mc, mc_reread_sel_t *info);

static void call_active_handlers(ipmi_mc_t *mc);
static void call_fully_up_handlers(ipmi_mc_t *mc);

/***********************************************************************
 *
 * Routines for creating and destructing MCs.
 *
 **********************************************************************/

static void
mc_set_name(ipmi_mc_t *mc)
{
    int         length;
    ipmi_mcid_t id = ipmi_mc_convert_to_id(mc);

    ipmi_lock(mc->lock);
    length = ipmi_domain_get_name(mc->domain, mc->name, sizeof(mc->name)-3);
    mc->name[length] = '(';
    length++;
    length += snprintf(mc->name+length, IPMI_MC_NAME_LEN-length-3, "%x.%x",
		       id.channel, id.mc_num);
    mc->name[length] = ')';
    length++;
    mc->name[length] = ' ';
    length++;
    mc->name[length] = '\0';
    length++;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_get_name(ipmi_mc_t *mc, char *name, int length)
{
    int  slen;

    if (length <= 0)
	return 0;

    /* Never changes, no lock needed. */
    slen = strlen(mc->name);
    if (slen == 0) {
	if (name)
	    *name = '\0';
	goto out;
    }

    slen -= 1; /* Remove the trailing ' ' */
    if (slen >= length)
	slen = length - 1;

    if (name) {
	memcpy(name, mc->name, slen);
	name[slen] = '\0';
    }
 out:
    return slen;
}

const char *
_ipmi_mc_name(const ipmi_mc_t *mc)
{
    return mc->name;
}

static os_handler_t *
mc_get_os_hnd(ipmi_mc_t *mc)
{
    ipmi_domain_t *domain = mc->domain;
    return ipmi_domain_get_os_hnd(domain);
}

typedef struct fully_up_cl_info_s
{
    ipmi_mc_ptr_cb handler;
    void           *handler_data;
} fully_up_cl_info_t;

static int
iterate_fully_up_cl(void *cb_data, void *item1, void *item2)
{
    fully_up_cl_info_t     *info = cb_data;
    ipmi_mc_fully_up_cl_cb handler = item1;

    handler(info->handler, info->handler_data, item2);
    return LOCKED_LIST_ITER_CONTINUE;
}

static int
fully_up_cleanup(void *cb_data, void *item1, void *item2)
{
    ipmi_mc_t *mc = cb_data;
    fully_up_cl_info_t info;

    info.handler = item1;
    info.handler_data = item2;
    locked_list_iterate(mc->fully_up_handlers_cl, iterate_fully_up_cl, &info);
    return LOCKED_LIST_ITER_CONTINUE;
}

typedef struct active_cl_info_s
{
    ipmi_mc_active_cb handler;
    void              *handler_data;
} active_cl_info_t;

static int
iterate_active_cl(void *cb_data, void *item1, void *item2)
{
    active_cl_info_t     *info = cb_data;
    ipmi_mc_active_cl_cb handler = item1;

    handler(info->handler, info->handler_data, item2);
    return LOCKED_LIST_ITER_CONTINUE;
}

static int
active_cleanup(void *cb_data, void *item1, void *item2)
{
    ipmi_mc_t *mc = cb_data;
    active_cl_info_t info;

    info.handler = item1;
    info.handler_data = item2;
    locked_list_iterate(mc->active_handlers_cl, iterate_active_cl, &info);
    return LOCKED_LIST_ITER_CONTINUE;
}

static int
check_mc_destroy(ipmi_mc_t *mc)
{
    ipmi_domain_t *domain = mc->domain;
    os_handler_t  *os_hnd = mc_get_os_hnd(mc);
    int           rv;

    if ((mc->state == MC_INACTIVE)
	&& (ipmi_controls_get_count(mc->controls) == 0)
	&& (ipmi_sensors_get_count(mc->sensors) == 0)
	&& (mc->usercount == 0))
    {
	mc->in_destroy = 1;
	ipmi_unlock(mc->lock);

	/* There are no sensors associated with this MC, so it's safe
           to delete it.  If there are sensors that still reference
           this MC (such as from another MC's SDR repository, or the
           main SDR repository) we have to leave it inactive but not
           delete it.  The active handlers come from MCDLR and FRUDLR
           SDRs that monitor the MC. */
	_ipmi_remove_mc_from_domain(domain, mc);

	if (mc->sel_timer_info) {
	    if (mc->sel_timer_info->lock) {
		ipmi_lock(mc->sel_timer_info->lock);
		if (mc->sel_timer_info->timer_running) {
		    mc->sel_timer_info->cancelled = 1;
		    rv = os_hnd->stop_timer(os_hnd,
					    mc->sel_timer_info->sel_timer);
		    ipmi_unlock(mc->sel_timer_info->lock);
		    if (!rv) {
			/* If we can stop the timer, free it and it's info.
			   If we can't stop the timer, that means that the
			   code is currently in the timer handler, so we let
			   the "cancelled" value do this for us. */
			ipmi_destroy_lock(mc->sel_timer_info->lock);
			os_hnd->free_timer(os_hnd,
					   mc->sel_timer_info->sel_timer);
			ipmi_mem_free(mc->sel_timer_info);
		    }
		} else {
		    ipmi_unlock(mc->sel_timer_info->lock);
		    ipmi_destroy_lock(mc->sel_timer_info->lock);
		    os_hnd->free_timer(os_hnd, mc->sel_timer_info->sel_timer);
		    ipmi_mem_free(mc->sel_timer_info);
		}
	    } else {
		/* Timer wasn't completely created. */
		if (mc->sel_timer_info->sel_timer)
		    os_hnd->free_timer(os_hnd, mc->sel_timer_info->sel_timer);
		ipmi_mem_free(mc->sel_timer_info);
	    }
	}

	if (mc->removed_handlers)
	    locked_list_destroy(mc->removed_handlers);
    	if (mc->active_handlers) {
	    locked_list_iterate(mc->active_handlers, active_cleanup, mc);
	    locked_list_destroy(mc->active_handlers);
	}
    	if (mc->active_handlers_cl)
	    locked_list_destroy(mc->active_handlers_cl);
    	if (mc->fully_up_handlers) {
	    locked_list_iterate(mc->fully_up_handlers, fully_up_cleanup, mc);
	    locked_list_destroy(mc->fully_up_handlers);
	}
    	if (mc->fully_up_handlers_cl)
	    locked_list_destroy(mc->fully_up_handlers_cl);
	if (mc->sensors)
	    ipmi_sensors_destroy(mc->sensors);
	if (mc->controls)
	    ipmi_controls_destroy(mc->controls);
	if (mc->sdrs)
	    ipmi_sdr_info_destroy(mc->sdrs, NULL, NULL);
	if (mc->sel)
	    ipmi_sel_destroy(mc->sel, NULL, NULL);
	if (mc->lock)
	    ipmi_destroy_lock(mc->lock);

	ipmi_mem_free(mc);
	return 1;
    }
    return 0;
}

int
_ipmi_create_mc(ipmi_domain_t *domain,
		ipmi_addr_t   *addr,
		unsigned int  addr_len,
		ipmi_mc_t     **new_mc)
{
    ipmi_mc_t    *mc;
    int          rv = 0;
    os_handler_t *os_hnd = ipmi_domain_get_os_hnd(domain);

    if (addr_len > sizeof(ipmi_addr_t))
	return EINVAL;

    mc = ipmi_mem_alloc(sizeof(*mc));
    if (!mc)
	return ENOMEM;
    memset(mc, 0, sizeof(*mc));

    mc->state = MC_INACTIVE;

    mc->usecount = 1; /* Require a release */

    mc->domain = domain;

    mc->seq = ipmi_get_seq();

    mc->events_enabled = 1;

    mc->active = 0; /* Start assuming inactive. */

    mc->sensors = NULL;
    mc->sensors_in_my_sdr = NULL;
    mc->sensors_in_my_sdr_count = 0;
    mc->entities_in_my_sdr = NULL;
    mc->controls = NULL;
    mc->new_sensor_handler = NULL;
    rv = ipmi_create_lock(domain, &mc->lock);
    if (rv)
	goto out_err;
    mc->removed_handlers = locked_list_alloc(os_hnd);
    if (!mc->removed_handlers) {
	rv = ENOMEM;
	goto out_err;
    }

    mc->active_handlers_cl = locked_list_alloc(os_hnd);
    if (!mc->active_handlers_cl) {
	rv = ENOMEM;
	goto out_err;
    }

    mc->active_handlers = locked_list_alloc(os_hnd);
    if (!mc->active_handlers) {
	rv = ENOMEM;
	goto out_err;
    }

    mc->fully_up_handlers_cl = locked_list_alloc(os_hnd);
    if (!mc->fully_up_handlers_cl) {
	rv = ENOMEM;
	goto out_err;
    }
    mc->fully_up_handlers = locked_list_alloc(os_hnd);
    if (!mc->fully_up_handlers) {
	rv = ENOMEM;
	goto out_err;
    }

    mc->sel = NULL;
    mc->sel_scan_interval = ipmi_domain_get_sel_rescan_time(domain);

    memcpy(&(mc->addr), addr, addr_len);
    mc->addr_len = addr_len;
    mc->sdrs = NULL;

    rv = ipmi_sensors_alloc(mc, &(mc->sensors));
    if (rv)
	goto out_err;

    rv = ipmi_controls_alloc(mc, &(mc->controls));
    if (rv)
	goto out_err;

    mc_set_name(mc);

    rv = ipmi_sel_alloc(mc, 0, &(mc->sel));
    if (rv)
	goto out_err;

    mc->sel_timer_info = ipmi_mem_alloc(sizeof(*mc->sel_timer_info));
    if (!mc->sel_timer_info) {
	rv = ENOMEM;
	goto out_err;
    }
    memset(mc->sel_timer_info, 0, sizeof(*mc->sel_timer_info));
    strncpy(mc->sel_timer_info->name, mc->name,
	    sizeof(mc->sel_timer_info->name));
    mc->sel_timer_info->mc_id = ipmi_mc_convert_to_id(mc);
    mc->sel_timer_info->mc = mc;
    mc->sel_timer_info->os_hnd = os_hnd;
    rv = os_hnd->alloc_timer(os_hnd, &mc->sel_timer_info->sel_timer);
    if (rv)
	goto out_err;

    rv = ipmi_create_lock(domain, &mc->sel_timer_info->lock);
    if (rv)
	goto out_err;

    rv = ipmi_sdr_info_alloc(domain, mc, 0, 1, &(mc->sdrs));
    if (rv)
	goto out_err;

    /* When we get new logs, handle them. */
    ipmi_sel_set_new_event_handler(mc->sel,
				   mc_sel_new_event_handler,
				   domain);

 out_err:
    if (rv)
	check_mc_destroy(mc);
    else
	*new_mc = mc;

    return rv;
}

static int
call_removed_handler(void *cb_data, void *item1, void *item2)
{
    ipmi_mc_oem_removed_cb handler = item1;
    ipmi_mc_t              *mc = cb_data;

    ipmi_mc_remove_oem_removed_handler(mc, handler, item2);
    handler(mc->domain, mc, item2);
    return LOCKED_LIST_ITER_CONTINUE;
}

/* Must be called with the mc lock held. */
static void
mc_stop_timer(ipmi_mc_t *mc)
{
    os_handler_t *os_hnd = mc_get_os_hnd(mc);
    int          rv;

    /* Make sure the timer stops. */
    ipmi_lock(mc->sel_timer_info->lock);
    mc->sel_timer_info->timer_should_run = 0;
    if (mc->sel_timer_info->timer_running) {
	rv = os_hnd->stop_timer(os_hnd, mc->sel_timer_info->sel_timer);
	if (!rv) {
	    mc->sel_timer_info->timer_running = 0;
	    mc->sel_timer_info->processing = 0;
	}
    }
    if ((mc->startup_count > 0) && !mc->sel_timer_info->processing)
	/* Hack: If we are processing, we will fail the processing or
	   it will complete later and finish.  If we were not
	   processing, then we were just waiting on the timer that was
	   just cancelled.  We decrement if we were waiting on the
	   timer. */
	mc->startup_count--;
    ipmi_unlock(mc->sel_timer_info->lock);
}

static void
mc_cleanup(ipmi_mc_t *mc)
{
    unsigned int  i;
    ipmi_domain_t *domain = mc->domain;

    /* Call the OEM handlers for removal, if it has been registered. */
    locked_list_iterate(mc->removed_handlers, call_removed_handler, mc);
    
    /* First the device SDR sensors, since they can be there for any
       MC. */
    if (mc->sensors_in_my_sdr) {
	for (i=0; i<mc->sensors_in_my_sdr_count; i++) {
	    ipmi_sensor_t *sensor;
	    _ipmi_domain_entity_lock(domain);
	    sensor = mc->sensors_in_my_sdr[i];
	    if (sensor) {
		ipmi_entity_t *entity = ipmi_sensor_get_entity(sensor);
		_ipmi_entity_get(entity);
		_ipmi_sensor_get(sensor);
		_ipmi_domain_entity_unlock(domain);
		ipmi_sensor_destroy(mc->sensors_in_my_sdr[i]);
		_ipmi_sensor_put(sensor);
		_ipmi_entity_put(entity);
	    } else {
		_ipmi_domain_entity_unlock(domain);
	    }
	}
	ipmi_mem_free(mc->sensors_in_my_sdr);
	mc->sensors_in_my_sdr = NULL;
    }

    if (mc->entities_in_my_sdr) {
	ipmi_sdr_entity_destroy(mc->entities_in_my_sdr);
	mc->entities_in_my_sdr = NULL;
    }

    if (mc->sdrs)
	ipmi_sdr_clean_out_sdrs(mc->sdrs);
}

/***********************************************************************
 *
 * Reset routines for MCs.
 *
 **********************************************************************/

typedef struct mc_reset_info_s
{
    ipmi_mc_done_cb done;
    void            *cb_data;
} mc_reset_info_t;

static void
mc_reset_done(ipmi_mc_t  *mc,
	      ipmi_msg_t *rsp,
	      void       *rsp_data)
{
    int             err = 0;
    mc_reset_info_t *info = rsp_data;

    if (rsp->data[0] != 0)
	err = IPMI_IPMI_ERR_VAL(rsp->data[0]);

    if (info->done)
	info->done(mc, err, info->cb_data);

    ipmi_mem_free(info);
}

int
ipmi_mc_reset(ipmi_mc_t       *mc,
	      int             reset_type,
	      ipmi_mc_done_cb done,
	      void            *cb_data)
{
    int             rv;
    ipmi_msg_t      msg;
    mc_reset_info_t *info;

    CHECK_MC_LOCK(mc);

    if (reset_type == IPMI_MC_RESET_COLD)
	msg.cmd = IPMI_COLD_RESET_CMD;
    else if (reset_type == IPMI_MC_RESET_WARM)
	msg.cmd = IPMI_WARM_RESET_CMD;
    else
	return EINVAL;

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;
    info->done = done;
    info->cb_data = cb_data;

    msg.netfn = IPMI_APP_NETFN;
    msg.data = NULL;
    msg.data_len = 0;
    rv = ipmi_mc_send_command(mc, 0, &msg, mc_reset_done, info);
    if (rv)
	ipmi_mem_free(info);

    return rv;
}

/***********************************************************************
 *
 * Event handling.
 *
 **********************************************************************/

/* Got a new event in the system event log that we didn't have before. */
static void
mc_sel_new_event_handler(ipmi_sel_info_t *sel,
			 ipmi_mc_t       *mc,
			 ipmi_event_t    *event,
			 void            *cb_data)
{
    _ipmi_domain_system_event_handler(cb_data, mc, event);
}

int
_ipmi_mc_check_oem_event_handler(ipmi_mc_t *mc, ipmi_event_t *event)
{
    if (mc->oem_event_handler)
	return (mc->oem_event_handler(mc, event, mc->oem_event_cb_data));
    else
	return 0;
}

int
_ipmi_mc_check_sel_oem_event_handler(ipmi_mc_t *mc, ipmi_event_t *event)
{
    if (mc->sel_oem_event_handler)
	return (mc->sel_oem_event_handler(mc, event,
					  mc->sel_oem_event_cb_data));
    else
	return 0;
}


/***********************************************************************
 *
 * SEL handling.
 *
 **********************************************************************/

int
_ipmi_mc_sel_event_add(ipmi_mc_t *mc, ipmi_event_t *event)
{
    return ipmi_sel_event_add(mc->sel, event);
}

ipmi_time_t
ipmi_mc_get_startup_SEL_time(ipmi_mc_t *mc)
{
    return mc->startup_SEL_time;
}

void
ipmi_mc_set_del_event_handler(ipmi_mc_t            *mc,
			      ipmi_mc_del_event_cb handler)
{
    mc->sel_del_event_handler = handler;
}

void
ipmi_mc_set_add_event_handler(ipmi_mc_t            *mc,
			      ipmi_mc_add_event_cb handler)
{
    mc->sel_add_event_handler = handler;
}

void
ipmi_mc_set_sel_clear_handler(ipmi_mc_t            *mc,
			      ipmi_mc_del_event_cb handler)
{
    mc->sel_clear_handler = handler;
}

void
ipmi_mc_set_sel_rescan_time(ipmi_mc_t *mc, unsigned int seconds)
{
    unsigned int old_time;
    CHECK_MC_LOCK(mc);

    if (mc->sel_scan_interval == seconds)
	return;

    old_time = mc->sel_scan_interval;

    mc->sel_scan_interval = seconds;
    if (old_time == 0) {
	/* The old time was zero, so we must restart the timer. */
	ipmi_lock(mc->sel_timer_info->lock);
	sels_start_timer(mc->sel_timer_info);
	ipmi_unlock(mc->sel_timer_info->lock);
    }
}

unsigned int
ipmi_mc_get_sel_rescan_time(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);

    return mc->sel_scan_interval;
}

typedef struct sel_op_done_info_s
{
    ipmi_mc_t       *mc;
    ipmi_mc_done_cb done;
    void            *cb_data;
} sel_op_done_info_t;

static void
sel_op_done(ipmi_sel_info_t *sel,
	    void            *cb_data,
	    int             err)
{
    sel_op_done_info_t *info = cb_data;

    /* No need to refcount, the domain/mc should already be locked. */
    if (info->done)
        info->done(info->mc, err, info->cb_data);
    ipmi_mem_free(info);
}

int
ipmi_mc_del_event(ipmi_mc_t                 *mc,
		  ipmi_event_t              *event, 
		  ipmi_mc_del_event_done_cb handler,
		  void                      *cb_data)
{
    sel_op_done_info_t *sel_info;
    int                rv;

    if (!mc->devid.SEL_device_support)
	return EINVAL;

    /* If we have an OEM handler, call it instead. */
    if (mc->sel_del_event_handler) {
	rv = mc->sel_del_event_handler(mc, event, handler, cb_data);
	return rv;
    }

    sel_info = ipmi_mem_alloc(sizeof(*sel_info));
    if (!sel_info)
	return ENOMEM;

    sel_info->mc = mc;
    sel_info->done = handler;
    sel_info->cb_data = cb_data;

    rv = ipmi_sel_del_event(mc->sel, event, sel_op_done, sel_info);
    if (rv)
	ipmi_mem_free(sel_info);

    return rv;
}

int
ipmi_mc_sel_clear(ipmi_mc_t                 *mc,
		  ipmi_event_t              *last_event, 
		  ipmi_mc_del_event_done_cb handler,
		  void                      *cb_data)
{
    sel_op_done_info_t *sel_info;
    int                rv;

    if (!mc->devid.SEL_device_support)
	return EINVAL;

    /* If we have an OEM handler, call it instead. */
    if (mc->sel_clear_handler) {
	rv = mc->sel_clear_handler(mc, last_event, handler, cb_data);
	return rv;
    }

    sel_info = ipmi_mem_alloc(sizeof(*sel_info));
    if (!sel_info)
	return ENOMEM;

    sel_info->mc = mc;
    sel_info->done = handler;
    sel_info->cb_data = cb_data;

    rv = ipmi_sel_clear(mc->sel, last_event, sel_op_done, sel_info);
    if (rv)
	ipmi_mem_free(sel_info);

    return rv;
}

typedef struct sel_add_op_done_info_s
{
    ipmi_mc_t                 *mc;
    ipmi_mc_add_event_done_cb done;
    void                      *cb_data;
} sel_add_op_done_info_t;

static void sel_add_op_done(ipmi_sel_info_t *sel,
			    void            *cb_data,
			    int             err,
			    unsigned int    record_id)
{
    sel_add_op_done_info_t *info = cb_data;

    /* No need to lock, the domain/mc should already be locked. */
    if (info->done)
        info->done(info->mc, record_id, err, info->cb_data);
    ipmi_mem_free(info);
}

int
ipmi_mc_add_event_to_sel(ipmi_mc_t                 *mc,
			 ipmi_event_t              *event,
			 ipmi_mc_add_event_done_cb handler,
			 void                      *cb_data)
{
    sel_add_op_done_info_t *sel_info;
    int                    rv;

    if (!mc->devid.SEL_device_support)
	return EINVAL;

    /* If we have an OEM handler, call it instead. */
    if (mc->sel_add_event_handler) {
	rv = mc->sel_add_event_handler(mc, event, handler, cb_data);
	return rv;
    }

    sel_info = ipmi_mem_alloc(sizeof(*sel_info));
    if (!sel_info)
	return ENOMEM;

    sel_info->mc = mc;
    sel_info->done = handler;
    sel_info->cb_data = cb_data;

    rv = ipmi_sel_add_event_to_sel(mc->sel, event, sel_add_op_done, sel_info);
    if (rv)
	ipmi_mem_free(sel_info);

    return rv;
}

ipmi_event_t *
ipmi_mc_next_event(ipmi_mc_t *mc, const ipmi_event_t *event)
{
    return ipmi_sel_get_next_event(mc->sel, event);
}

ipmi_event_t *
ipmi_mc_prev_event(ipmi_mc_t *mc, const ipmi_event_t *event)
{
    return ipmi_sel_get_prev_event(mc->sel, event);
}

ipmi_event_t *
ipmi_mc_last_event(ipmi_mc_t *mc)
{
    return ipmi_sel_get_last_event(mc->sel);
}

ipmi_event_t *
ipmi_mc_first_event(ipmi_mc_t *mc)
{
    return ipmi_sel_get_first_event(mc->sel);
}

ipmi_event_t *
ipmi_mc_event_by_recid(ipmi_mc_t    *mc,
                       unsigned int record_id)
{
    return ipmi_sel_get_event_by_recid(mc->sel, record_id);
}

int
ipmi_mc_sel_count(ipmi_mc_t *mc)
{
    unsigned int val = 0;

    ipmi_get_sel_count(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_entries_used(ipmi_mc_t *mc)
{
    unsigned int val = 0;

    ipmi_get_sel_entries_used(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_major_version(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_major_version(mc->sel, &val);
    return val;
}

int 
ipmi_mc_sel_get_minor_version(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_minor_version(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_num_entries(ipmi_mc_t *mc)
{
    int val = 0;
    
    ipmi_sel_get_num_entries(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_free_bytes(ipmi_mc_t *mc)
{
    int val = 0;
    
    ipmi_sel_get_free_bytes(mc->sel, &val);
    return val;
}

int 
ipmi_mc_sel_get_overflow(ipmi_mc_t *mc)
{
    int val = 0;
    
    ipmi_sel_get_overflow(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_supports_delete_sel(ipmi_mc_t *mc)
{
    int val = 0;
    
    ipmi_sel_get_supports_delete_sel(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_supports_partial_add_sel(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_supports_partial_add_sel(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_supports_reserve_sel(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_supports_reserve_sel(mc->sel, &val);
    return val;
}

int 
ipmi_mc_sel_get_supports_get_sel_allocation(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_supports_get_sel_allocation(mc->sel, &val);
    return val;
}

int
ipmi_mc_sel_get_last_addition_timestamp(ipmi_mc_t *mc)
{
    int val = 0;

    ipmi_sel_get_last_addition_timestamp(mc->sel, &val);
    return val;
}

int
ipmi_mc_set_oem_event_handler(ipmi_mc_t                 *mc,
			      ipmi_oem_event_handler_cb handler,
			      void                      *cb_data)
{
    mc->oem_event_handler = handler;
    mc->oem_event_cb_data = cb_data;
    return 0;
}

int
ipmi_mc_set_sel_oem_event_handler(ipmi_mc_t                 *mc,
				  ipmi_oem_event_handler_cb handler,
				  void                      *cb_data)
{
    mc->sel_oem_event_handler = handler;
    mc->sel_oem_event_cb_data = cb_data;
    return 0;
}

static void mc_reread_sel_timeout(void *cb_data, os_hnd_timer_id_t *id);

/* Must be called with the info lock held. */
static void
sels_start_timer(mc_reread_sel_t *info)
{
    DEBUG_INFO(info);
    info->processing = 0;
    if (info->mc->sel_scan_interval != 0) {
	os_handler_t   *os_hnd = info->os_hnd;
	struct timeval timeout;

	timeout.tv_sec = info->mc->sel_scan_interval;
	timeout.tv_usec = 0;
	info->timer_running = 1;
	os_hnd->start_timer(os_hnd,
			    info->sel_timer,
			    &timeout,
			    mc_reread_sel_timeout,
			    info);
    } else {
	info->timer_running = 0;
    }
}

/* Must be called with the info lock held, will release the lock. */
static void
sels_fetched_call_handler(mc_reread_sel_t *info, int err, int changed,
			  int count)
{
    ipmi_sels_fetched_t handler = NULL;
    void                *cb_data = NULL;
    ipmi_mc_ptr_cb      handler2 = NULL;
    void                *cb_data2 = NULL;

    DEBUG_INFO(info);
    if (info->handler) {
	handler = info->handler;
	cb_data = info->cb_data;
	info->handler = NULL;
    }
    if (info->sels_first_read_handler) {
	handler2 = info->sels_first_read_handler;
	cb_data2 = info->sels_first_read_cb_data;
	info->sels_first_read_handler = NULL;
    }
    ipmi_unlock(info->lock);

    if (handler2)
	handler2(info->mc, cb_data2);

    if (handler)
	handler(info->mc->sel, err, changed, count, cb_data);
}

static void
sels_restart(mc_reread_sel_t *info)
{
    /* After the first SEL fetch, disable looking at the timestamp, in
       case someone messes with the SEL time. */
    DEBUG_INFO(info);
    info->mc->startup_SEL_time = 0;
    info->sel_time_set = 1;

    sels_start_timer(info);
}

static void
sels_fetched_start_timer(ipmi_sel_info_t *sel,
			 int             err,
			 int             changed,
			 unsigned int    count,
			 void            *cb_data)
{
    mc_reread_sel_t *info = cb_data;

    ipmi_lock(info->lock);
    DEBUG_INFO(info);
    if (info->cancelled) {
	DEBUG_INFO(info);
	ipmi_unlock(info->lock);
	info->os_hnd->free_timer(info->os_hnd, info->sel_timer);
	ipmi_destroy_lock(info->lock);
	ipmi_mem_free(info);
	return;
    } else if (! info->timer_should_run) {
	DEBUG_INFO(info);
	info->processing = 0;
	info->timer_running = 0;
	sels_fetched_call_handler(info, ECANCELED, 0, 0);
	return;
    }

    /* After the first SEL fetch, disable looking at the timestamp, in
       case someone messes with the SEL time. */
    info->mc->startup_SEL_time = 0;

    sels_start_timer(info);
    sels_fetched_call_handler(info, err, changed, count);
}

static void
mc_reread_sel_timeout_cb(ipmi_mc_t *mc, void *cb_data)
{
    mc_reread_sel_t *info = cb_data;
    int             rv = EINVAL;

    DEBUG_INFO(info);
    info->processing = 1;
    if (! info->sel_time_set) {
	DEBUG_INFO(mc->sel_timer_info);
	start_sel_time_set(mc, info);
    } else {
	/* Only fetch the SEL if we know the connection is up. */
	if (ipmi_domain_con_up(mc->domain)) {
	    DEBUG_INFO(mc->sel_timer_info);
	    rv = ipmi_sel_get(mc->sel, sels_fetched_start_timer, info);
	}

	/* If we couldn't run the SEL get, then restart the timer now. */
	if (rv) {
	    DEBUG_INFO(mc->sel_timer_info);
	    sels_start_timer(info);
	}
    }

    /* Have to unlock here, because the MC put processing may claim
       this lock. */
    ipmi_unlock(info->lock);
}

static void
mc_reread_sel_timeout(void *cb_data, os_hnd_timer_id_t *id)
{
    mc_reread_sel_t *info = cb_data;
    ipmi_mcid_t     mc_id;
    int             rv;

    ipmi_lock(info->lock);
    DEBUG_INFO(info);
    if (info->cancelled) {
	DEBUG_INFO(info);
	ipmi_unlock(info->lock);
	info->os_hnd->free_timer(info->os_hnd, info->sel_timer);
	ipmi_destroy_lock(info->lock);
	ipmi_mem_free(info);
	return;
    } else if (! info->timer_should_run) {
	DEBUG_INFO(info);
	info->processing = 0;
	info->timer_running = 0;
	sels_fetched_call_handler(info, ECANCELED, 0, 0);
	return;
    }

    mc_id = info->mc_id;

    rv = ipmi_mc_pointer_cb(mc_id, mc_reread_sel_timeout_cb, info);
    if (rv) {
	/* Strange, but correct.  If we get here but the MC no longer
	   exists, we raced with it's destroy.  We still hold the info
	   lock, so just don't start the timer and everything should
	   be happy. */
	DEBUG_INFO(info);
	info->processing = 0;
	info->timer_running = 0;
	ipmi_unlock(info->lock);
    }
}

typedef struct sel_reread_s
{
    ipmi_mc_done_cb handler;
    void            *cb_data;
    ipmi_mcid_t     mcid;
    int             err;
} sel_reread_t;

static void
mc_reread_sel_cb(ipmi_mc_t *mc, void *cb_data)
{
    sel_reread_t *info = cb_data;

    info->handler(mc, info->err, info->cb_data);
}

static void
reread_sel_done(ipmi_sel_info_t *sel,
		int             err,
		int             changed,
		unsigned int    count,
		void            *cb_data)
{
    sel_reread_t *info = cb_data;
    int          rv;

    if (info->handler) {
	if (!sel) {
	    info->handler(NULL, ECANCELED, info->cb_data);
	    goto out;
	}

	rv = ipmi_mc_pointer_cb(info->mcid, mc_reread_sel_cb, info);
	if (rv) {
	    info->handler(NULL, ECANCELED, info->cb_data);
	    goto out;
	}
    }
 out:
    ipmi_mem_free(info);
}

static int start_sel_ops(ipmi_mc_t           *mc,
			 int                 fail_if_down,
			 ipmi_sels_fetched_t handler,
			 void                *cb_data);

int
ipmi_mc_reread_sel(ipmi_mc_t       *mc,
		   ipmi_mc_done_cb handler,
		   void            *cb_data)
{
    sel_reread_t        *info = NULL;
    ipmi_sels_fetched_t cb = NULL;
    int                 rv;

    if (handler) {
	info = ipmi_mem_alloc(sizeof(*info));
	if (!info)
	    return ENOMEM;

	info->handler = handler;
	info->cb_data = cb_data;
	info->mcid = ipmi_mc_convert_to_id(mc);
	info->err = 0;
	cb = reread_sel_done;
    }

    ipmi_lock(mc->lock);
    if (! mc_op_ready(mc)) {
	rv = ECANCELED;
    } else if (mc->sel_timer_info) {
	/* SEL is already set up, just do a request. */
	rv = ipmi_sel_get(mc->sel, cb, info);
    } else {
	/* SEL is not set up, start it. */
	rv = start_sel_ops(mc, 1, cb, info);
    }
    ipmi_unlock(mc->lock);

    if (rv && info) {
	ipmi_mem_free(info);
    }

    return rv;
}

typedef struct sel_get_time_s
{
    sel_get_time_cb handler;
    void            *cb_data;
    char            name[IPMI_MC_NAME_LEN];
} sel_get_time_t;

static void
get_sel_time(ipmi_mc_t  *mc,
	     ipmi_msg_t *rsp,
	     void       *rsp_data)
{
    sel_get_time_t *info = rsp_data;

    if (!mc) {
	/* The MC went away, deliver an error. */
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(get_sel_time): "
		 "MC went away during SEL time fetch.",
		 info->name);
	if (info->handler)
	    info->handler(mc, ECANCELED, 0, info->cb_data);
	goto out;
    }

    if (rsp->data[0] != 0) {
	/* Error setting the event receiver, report it. */
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(get_sel_time): "
		 "Could not get SEL time for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	if (info->handler)
	    info->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), 0,
			  info->cb_data);
	goto out;
    }

    if (rsp->data_len < 5) {
	/* Not enough data? */
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(get_sel_time): "
		 "Get SEL time response too short for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	if (info->handler)
	    info->handler(mc, EINVAL, 0, info->cb_data);
	goto out;
    }

    if (info->handler)
	info->handler(mc, 0, ipmi_get_uint32(rsp->data+1), info->cb_data);

 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_get_current_sel_time(ipmi_mc_t       *mc,
			     sel_get_time_cb handler,
			     void            *cb_data)
{
    ipmi_msg_t     msg;
    sel_get_time_t *info;
    int            rv;

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;

    info->handler = handler;
    info->cb_data = cb_data;
    strncpy(info->name, mc->name, sizeof(info->name));

    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_GET_SEL_TIME_CMD;
    msg.data = NULL;
    msg.data_len = 0;
    rv = ipmi_mc_send_command(mc, 0, &msg, get_sel_time, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}

typedef struct set_sel_time_s
{
    ipmi_mc_done_cb handler;
    void            *cb_data;
    char            name[IPMI_MC_NAME_LEN];
} set_sel_time_t;

static void
set_sel_time(ipmi_mc_t  *mc,
	     ipmi_msg_t *rsp,
	     void       *rsp_data)
{
    set_sel_time_t *info = rsp_data;

    if (!mc) {
	/* The MC went away, deliver an error. */
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(set_sel_time): "
		 "MC went away during SEL time fetch.",
		 info->name);
	if (info->handler)
	    info->handler(mc, ECANCELED, info->cb_data);
	goto out;
    }

    if (rsp->data[0] != 0) {
	/* Error setting the event receiver, report it. */
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(set_sel_time): "
		 "Could not get SEL time for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	if (info->handler)
	    info->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info->cb_data);
	goto out;
    }

    if (info->handler)
	info->handler(mc, 0, info->cb_data);

 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_set_current_sel_time(ipmi_mc_t             *mc,
			     const struct timeval  *time,
			     ipmi_mc_done_cb       handler,
			     void                  *cb_data)
{
    ipmi_msg_t     msg;
    int            rv;
    unsigned char  data[4];
    set_sel_time_t *info;


    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;

    info->handler = handler;
    info->cb_data = cb_data;
    strncpy(info->name, mc->name, sizeof(info->name));

    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_SET_SEL_TIME_CMD;
    msg.data = data;
    msg.data_len = 4;
    ipmi_set_uint32(data, time->tv_sec);
    rv = ipmi_mc_send_command(mc, 0, &msg, set_sel_time, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}


/***********************************************************************
 *
 * Handling startup of a new MC
 *
 **********************************************************************/

typedef struct set_event_rcvr_info_s
{
    ipmi_mc_done_cb done;
    void            *cb_data;
} set_event_rcvr_info_t;

static void
set_event_rcvr_done(ipmi_mc_t  *mc,
		    ipmi_msg_t *rsp,
		    void       *rsp_data)
{
    ipmi_mc_done_cb done = NULL;
    void            *cb_data = NULL;
    int             rv = 0;

    if (rsp_data) {
	set_event_rcvr_info_t *info = rsp_data;
	done = info->done;
	cb_data = info->cb_data;
	ipmi_mem_free(info);
    }

    if (!mc) {
	rv = ECANCELED;
	goto out; /* The MC went away, no big deal. */
    }

    if (rsp->data[0] != 0) {
	/* Error setting the event receiver, report it. */
	ipmi_log(IPMI_LOG_WARNING,
		 "%smc.c(set_event_rcvr_done): "
		 "Could not set event receiver for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	rv = IPMI_IPMI_ERR_VAL(rsp->data[0]);
    }

 out:
    if (done)
	done(mc, rv, cb_data);
}

static int
send_set_event_rcvr(ipmi_mc_t       *mc,
		    unsigned int    addr,
		    ipmi_mc_done_cb done,
		    void            *cb_data)
{
    ipmi_msg_t            msg;
    unsigned char         data[2];
    set_event_rcvr_info_t *info = NULL;

    if (done) {
	info = ipmi_mem_alloc(sizeof(*info));
	if (!info)
	    return ENOMEM;
	info->done = done;
	info->cb_data = cb_data;
    }
    
    msg.netfn = IPMI_SENSOR_EVENT_NETFN;
    msg.cmd = IPMI_SET_EVENT_RECEIVER_CMD;
    msg.data = data;
    msg.data_len = 2;
    data[0] = addr;
    data[1] = 0; /* LUN is 0 per the spec (section 7.2 of 1.5 spec). */
    return ipmi_mc_send_command(mc, 0, &msg, set_event_rcvr_done, info);
    /* No care about return values, if this fails it will be done
       again later. */
}

static void
get_event_rcvr_done(ipmi_mc_t  *mc,
		    ipmi_msg_t *rsp,
		    void       *rsp_data)
{
    if (!mc)
	return; /* The MC went away, no big deal. */

    if (rsp->data[0] != 0) {
	/* Error getting the event receiver, report it. */
	ipmi_log(IPMI_LOG_WARNING,
		 "%smc.c(get_event_rcvr_done): "
		 "Could not get event receiver for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
    } else if (rsp->data_len < 2) {
	ipmi_log(IPMI_LOG_WARNING,
		 "%smc.c(get_event_rcvr_done): "
		 "Get event receiver length invalid for MC at 0x%x",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr));
    } else if ((rsp->data[1] == 0) && (!mc->events_enabled))  {
	/* Nothing to do, our event receiver is disabled. */
    } else {
	ipmi_domain_t    *domain = ipmi_mc_get_domain(mc);
	ipmi_mc_t        *destmc;
	ipmi_ipmb_addr_t ipmb;

	ipmb.addr_type = IPMI_IPMB_ADDR_TYPE;
	ipmb.channel = ipmi_mc_get_channel(mc);
	ipmb.slave_addr = rsp->data[1];
	ipmb.lun = 0;

	if (mc->events_enabled) {
	    destmc = _ipmi_find_mc_by_addr(domain, (ipmi_addr_t *) &ipmb,
					   sizeof(ipmb));
	    if (!destmc || !ipmi_mc_ipmb_event_receiver_support(destmc)) {
		/* The current event receiver doesn't exist or cannot
		   receive events, change it. */
		unsigned int event_rcvr = ipmi_domain_get_event_rcvr(mc->domain);
		if (event_rcvr)
		    send_set_event_rcvr(mc, event_rcvr, NULL, NULL);
	    }
	    if (destmc)
		_ipmi_mc_put(destmc);
	} else {
	    send_set_event_rcvr(mc, 0, NULL, NULL);
	}
    }
}

static void
send_get_event_rcvr(ipmi_mc_t *mc)
{
    ipmi_msg_t    msg;
    
    msg.netfn = IPMI_SENSOR_EVENT_NETFN;
    msg.cmd = IPMI_GET_EVENT_RECEIVER_CMD;
    msg.data = NULL;
    msg.data_len = 0;
    ipmi_mc_send_command(mc, 0, &msg, get_event_rcvr_done, NULL);
    /* No care about return values, if this fails it will be done
       again later. */
}

void
_ipmi_mc_check_event_rcvr(ipmi_mc_t *mc)
{
    if (mc && mc->devid.IPMB_event_generator_support
	&& ipmi_option_set_event_rcvr(mc->domain))
    {
	/* We have an MC that is live (or still live) and generates
	   events, make sure the event receiver is set properly. */
	unsigned int event_rcvr = ipmi_domain_get_event_rcvr(mc->domain);

	/* Don't bother if we have no possible event receivers.*/
	if (event_rcvr) {
	    send_get_event_rcvr(mc);
	}
    }
}

static void
startup_set_sel_time(ipmi_mc_t  *mc,
		     ipmi_msg_t *rsp,
		     void       *rsp_data)
{
    mc_reread_sel_t *info = rsp_data;
    int             rv;

    ipmi_lock(info->lock);
    DEBUG_INFO(info);
    if (info->cancelled) {
	DEBUG_INFO(info);
	ipmi_unlock(info->lock);
	info->os_hnd->free_timer(info->os_hnd, info->sel_timer);
	ipmi_destroy_lock(info->lock);
	ipmi_mem_free(info);
	return;
    } else if (! info->timer_should_run) {
	DEBUG_INFO(info);
	info->processing = 0;
	info->timer_running = 0;
	sels_fetched_call_handler(info, ECANCELED, 0, 0);
	return;
    }

    mc = info->mc;

    if (rsp->data[0] != 0) {
	info->retries++;
	if (info->retries > MAX_SEL_TIME_SET_RETRIES) {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(startup_set_sel_time): "
		     "Unable to set the SEL time due to error: %x, aborting",
		     mc->name, rsp->data[0]);
	    mc->startup_SEL_time = 0;
	    info->sel_time_set = 1;
	    sels_restart(info);
	} else {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(startup_set_sel_time): "
		     "Unable to set the SEL time due to error: %x, retrying",
		     mc->name, rsp->data[0]);
	    sels_start_timer(info);
	}
	goto out;
    }

    info->sel_time_set = 1;

    rv = ipmi_sel_get(mc->sel, sels_fetched_start_timer, mc->sel_timer_info);
    if (rv) {
	DEBUG_INFO(mc->sel_timer_info);
	ipmi_log(IPMI_LOG_WARNING,
		 "%smc.c(startup_set_sel_time): "
		 "Unable to start an SEL get due to error: %x",
		 mc->name, rsp->data[0]);
	sels_restart(info);
    }

 out:
    ipmi_unlock(info->lock);
}

static void
do_sel_time_set(ipmi_mc_t *mc, mc_reread_sel_t *info)
{
    ipmi_msg_t     msg;
    int            rv;
    unsigned char  data[4];
    struct timeval now;

    DEBUG_INFO(info);
    /* Set the current system event log time.  We do this here so
       we can be sure that the entities are all there before
       reporting events. */
    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_SET_SEL_TIME_CMD;
    msg.data = data;
    msg.data_len = 4;
    gettimeofday(&now, NULL);
    ipmi_set_uint32(data, now.tv_sec);
    mc->startup_SEL_time = ipmi_seconds_to_time(now.tv_sec);
    rv = ipmi_mc_send_command(mc, 0, &msg, startup_set_sel_time, info);
    if (rv) {
	info->retries++;
	if (info->retries > MAX_SEL_TIME_SET_RETRIES) {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(first_sel_op): "
		     "Unable to start SEL time set due to error: %x, aborting",
		     mc->name, rv);
	    mc->startup_SEL_time = 0;
	    sels_restart(info);
	} else {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(first_sel_op): "
		     "Unable to start SEL time set due to error: %x, retrying",
		     mc->name, rv);
	    sels_start_timer(info);
	}
    }
}

static void
startup_got_sel_time(ipmi_mc_t  *mc,
		     ipmi_msg_t *rsp,
		     void       *rsp_data)
{
    mc_reread_sel_t *info = rsp_data;
    struct timeval  now;
    uint32_t        time;
    int             rv;

    ipmi_lock(info->lock);
    DEBUG_INFO(info);
    if (info->cancelled) {
	DEBUG_INFO(info);
	ipmi_unlock(info->lock);
	info->os_hnd->free_timer(info->os_hnd, info->sel_timer);
	ipmi_destroy_lock(info->lock);
	ipmi_mem_free(info);
	return;
    } else if (! info->timer_should_run) {
	DEBUG_INFO(info);
	info->processing = 0;
	info->timer_running = 0;
	sels_fetched_call_handler(info, ECANCELED, 0, 0);
	return;
    }

    /* MC must be valid if we are not cancelled. */
    mc = info->mc;
    
    if (rsp->data[0] != 0) {
	info->retries++;
	if (info->retries > MAX_SEL_TIME_SET_RETRIES) {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_WARNING,
		     "%smc.c(startup_set_sel_time): "
		     "Unable to get the SEL time due to error: %x, aborting",
		     mc->name, rsp->data[0]);
	    mc->startup_SEL_time = 0;
	    sels_restart(info);
	} else {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(startup_set_sel_time): "
		     "Unable to get the SEL time due to error: %x, retrying",
		     mc->name, rsp->data[0]);
	    sels_start_timer(info);
	}
	goto out;
    }

    if (rsp->data_len < 5) {
	info->retries++;
	if (info->retries > MAX_SEL_TIME_SET_RETRIES) {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_WARNING,
		     "%smc.c(startup_got_sel_time): "
		     "Get SEL time response too short for MC at 0x%x,"
		     " aborting",
		     mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	    mc->startup_SEL_time = 0;
	    sels_restart(info);
	} else {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_WARNING,
		     "%smc.c(startup_got_sel_time): "
		     "Get SEL time response too short for MC at 0x%x,"
		     " retrying",
		     mc->name, ipmi_addr_get_slave_addr(&mc->addr));
	    sels_start_timer(info);
	}
	goto out;
    }

    gettimeofday(&now, NULL);
    time = ipmi_get_uint32(rsp->data+1);

    if ((time < (uint32_t)now.tv_sec) && ipmi_option_set_sel_time(mc->domain)) {
	/* Time is in the past and setting time is requested, move it
	   forward. */
	DEBUG_INFO(mc->sel_timer_info);
	do_sel_time_set(mc, info);
    } else {
	struct timeval tv;
	/* Time is current or in the future, don't move it backwards
	   as that may mess other things up. */
	DEBUG_INFO(mc->sel_timer_info);
	tv.tv_sec = time;
	tv.tv_usec = 0;
	mc->startup_SEL_time = ipmi_timeval_to_time(tv);
	info->sel_time_set = 1;

	rv = ipmi_sel_get(mc->sel, sels_fetched_start_timer,
			  mc->sel_timer_info);
	if (rv) {
	    DEBUG_INFO(mc->sel_timer_info);
	    ipmi_log(IPMI_LOG_WARNING,
		     "%smc.c(startup_got_sel_time): "
		     "Unable to start SEL fetch due to error 0x%x",
		     mc->name, rv);
	    sels_restart(info);
	}
    }

 out:
    ipmi_unlock(info->lock);
}

static void
start_sel_time_set(ipmi_mc_t *mc, mc_reread_sel_t *info)
{
    ipmi_msg_t      msg;
    int             rv;

    DEBUG_INFO(info);
    /* Set the current system event log time.  We do this here so we
       can be sure that the entities are all there before reporting
       events.  But first we fetch it to make sure it needs to be
       changed. */
    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_GET_SEL_TIME_CMD;
    msg.data = NULL;
    msg.data_len = 0;
    rv = ipmi_mc_send_command(mc, 0, &msg, startup_got_sel_time, info);
    if (rv) {
	DEBUG_INFO(mc->sel_timer_info);
	info->retries++;
	if (info->retries > MAX_SEL_TIME_SET_RETRIES) {
	    ipmi_log(IPMI_LOG_WARNING,
		     "%smc.c(start_sel_time_set): "
		     "Unable to start SEL time set due to error: %x, aborting",
		     mc->name, rv);
	    sels_restart(info);
	} else {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(start_sel_time_set): "
		     "Unable to start SEL time set due to error: %x, retrying",
		     mc->name, rv);
	    sels_start_timer(info);
	}
    }
}

static int
start_sel_ops(ipmi_mc_t           *mc,
	      int                 fail_if_down,
	      ipmi_sels_fetched_t handler,
	      void                *cb_data)
{
    ipmi_domain_t   *domain = ipmi_mc_get_domain(mc);
    mc_reread_sel_t *info = mc->sel_timer_info;
    int             rv = 0;

    ipmi_lock(info->lock);
    DEBUG_INFO(info);
    if (info->timer_should_run) {
	DEBUG_INFO(info);
	ipmi_unlock(info->lock);
	return EBUSY; /* Already started. */
    }

    info->timer_should_run = 1;
    info->retries = 0;
    info->sel_time_set = 0;

    info->handler = handler;
    info->cb_data = cb_data;

    if (ipmi_domain_con_up(domain)) {
	/* The domain is already up, just start the process. */
	DEBUG_INFO(info);
	info->processing = 1;
	start_sel_time_set(mc, info);
	ipmi_unlock(info->lock);
    } else if (fail_if_down) {
	ipmi_mc_ptr_cb  handler2 = NULL;
	void            *cb_data2 = NULL;
	DEBUG_INFO(info);
	rv = EAGAIN;
	info->timer_should_run = 0;
	info->processing = 0;
	/* SELs not started, just call the handler. */
	if (mc->sel_timer_info->sels_first_read_handler) {
	    handler2 = mc->sel_timer_info->sels_first_read_handler;
	    cb_data2 = mc->sel_timer_info->sels_first_read_cb_data;
	    mc->sel_timer_info->sels_first_read_handler = NULL;
	}
	ipmi_unlock(info->lock);

	if (handler2)
	    handler2(info->mc, cb_data2);
    } else {
	/* The domain is not up yet, wait for it to come up then start
           the process. */
	DEBUG_INFO(info);
	sels_start_timer(info);
	ipmi_unlock(info->lock);
    }
    return rv;
}

void
_ipmi_mc_startup_get(ipmi_mc_t *mc, char *name)
{
    ipmi_lock(mc->lock);
    mc->startup_count++;
    ipmi_unlock(mc->lock);
}

void
_ipmi_mc_startup_put(ipmi_mc_t *mc, char *name)
{
    ipmi_lock(mc->lock);
    DEBUG_INFO(mc->sel_timer_info);
    mc->sel_timer_info->processing = 0;
    mc->startup_count--;
    if (mc->startup_reported || (mc->startup_count > 0)) {
	ipmi_unlock(mc->lock);
	return;
    }
    mc->startup_reported = 1;
    if (mc->state == MC_ACTIVE_IN_STARTUP)
	mc->state = MC_ACTIVE_PEND_FULLY_UP;
    ipmi_unlock(mc->lock);
    _ipmi_put_domain_fully_up(mc->domain, "_ipmi_mc_startup_put");
}

static void
mc_first_sels_read(ipmi_sel_info_t *sel,
		   int             err,
		   int             changed,
		   unsigned int    count,
		   void            *cb_data)
{
    ipmi_mc_t *mc = cb_data;

    _ipmi_mc_startup_put(mc, "mc_first_sels_read");
}

/* This is called after the first sensor scan for the MC, we start up
   timers and things like that here. */
static void
sensors_reread(ipmi_mc_t *mc, int err, void *cb_data)
{
    unsigned int event_rcvr = 0;

    if (!mc) {
	/* MC data is still valid, but the MC is not good any more.
	   We saved it in rsp_data. */
        mc = cb_data;
	DEBUG_INFO(mc->sel_timer_info);
	_ipmi_mc_startup_put(mc, "sensors_reread(3)");
	return; /* domain went away while processing. */
    }

    DEBUG_INFO(mc->sel_timer_info);
    /* See if any presence has changed with the new sensors. */ 
    ipmi_detect_domain_presence_changes(mc->domain, 0);

    /* We set the event receiver here, so that we know all the SDRs
       are installed.  That way any incoming events from the device
       will have the proper sensor set. */
    if (mc->devid.IPMB_event_generator_support
	&& ipmi_option_set_event_rcvr(mc->domain))
    {
	event_rcvr = ipmi_domain_get_event_rcvr(mc->domain);
    }

    if (event_rcvr)
	send_set_event_rcvr(mc, event_rcvr, NULL, NULL);

    ipmi_lock(mc->lock);
    if (mc->sdrs_first_read_handler) {
	ipmi_mc_ptr_cb handler = mc->sdrs_first_read_handler;
	void           *cb_data = mc->sdrs_first_read_cb_data;
	mc->sdrs_first_read_handler = NULL;
	ipmi_unlock(mc->lock);
	handler(mc, cb_data);
    } else
	ipmi_unlock(mc->lock);

    if (mc->devid.SEL_device_support && ipmi_option_SEL(mc->domain)) {
	int rv;
	/* If the MC supports an SEL, start scanning its SEL. */
	DEBUG_INFO(mc->sel_timer_info);
	ipmi_lock(mc->lock);
	rv = start_sel_ops(mc, 0, mc_first_sels_read, mc);
	ipmi_unlock(mc->lock);
	if (rv) {
	    DEBUG_INFO(mc->sel_timer_info);
	    _ipmi_mc_startup_put(mc, "sensors_reread(2)");
	}
    } else {
	DEBUG_INFO(mc->sel_timer_info);
	_ipmi_mc_startup_put(mc, "sensors_reread");
    }
}

static void
got_guid(ipmi_mc_t  *mc,
	 ipmi_msg_t *rsp,
	 void       *rsp_data)
{
    int rv;

    if (!mc) {
	/* MC data is still valid, but the MC is not good any more.
	   We saved it in rsp_data. */
        mc = rsp_data;
	_ipmi_mc_startup_put(mc, "got_guid");
	return; /* domain went away while processing. */
    }

    DEBUG_INFO(mc->sel_timer_info);
    if ((rsp->data[0] == 0) && (rsp->data_len >= 17)) {
	/* We have a GUID, save it */
	ipmi_mc_set_guid(mc, rsp->data+1);
    }

    if (((mc->devid.provides_device_sdrs) || (mc->treat_main_as_device_sdrs))
	&& ipmi_option_SDRs(ipmi_mc_get_domain(mc)))
    {
	DEBUG_INFO(mc->sel_timer_info);
	rv = ipmi_mc_reread_sensors(mc, sensors_reread, mc);
	if (rv) {
	    DEBUG_INFO(mc->sel_timer_info);
	    sensors_reread(mc, 0, NULL);
	}
    } else {
	DEBUG_INFO(mc->sel_timer_info);
	sensors_reread(mc, 0, NULL);
    }
}

static void
mc_startup(ipmi_mc_t *mc)
{
    ipmi_msg_t msg;
    int        rv = 0;

    DEBUG_INFO(mc->sel_timer_info);
    mc->sel_timer_info->processing = 1;
    mc->startup_count = 1;
    mc->startup_reported = 0;

    if (mc->devid.chassis_support && (ipmi_mc_get_address(mc) == 0x20)) {
        rv = _ipmi_chassis_create_controls(mc);
	if (rv) {
	    ipmi_log(IPMI_LOG_SEVERE,
		     "%smc.c(ipmi_mc_setup_new): "
		     "Unable to create chassis controls.",
		     mc->name);
	    _ipmi_mc_startup_put(mc, "mc_startup(2)");
	    return;
	}
    }

    /* FIXME - handle errors setting up OEM comain information.
       Handle errors so they get retried. */

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_DEVICE_GUID_CMD;
    msg.data_len = 0;
    msg.data = NULL;

    rv = ipmi_mc_send_command(mc, 0, &msg, got_guid, mc);
    if (rv) {
	DEBUG_INFO(mc->sel_timer_info);
	ipmi_log(IPMI_LOG_SEVERE,
		 "%smc.c(ipmi_mc_setup_new): "
		 "Unable to send get guid command.",
		 mc->name);
	_ipmi_mc_startup_put(mc, "mc_startup");
    }
}

/***********************************************************************
 *
 * MC ID and state handling
 *
 **********************************************************************/

void
_ipmi_mc_use(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    mc->usercount++;
}

void
_ipmi_mc_release(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    mc->usercount--;
}

/* Must be holding the domain->mc_lock to call these. */
int
_ipmi_mc_get(ipmi_mc_t *mc)
{
    mc->usecount++;
    return 0;
}

static void
mc_apply_pending(ipmi_mc_t *mc)
{
    if (mc->pending_devid_data) {
	mc->devid = mc->pending_devid;
	mc->pending_devid_data = 0;
	if (mc->pending_new_mc) {
	    _ipmi_mc_handle_new(mc);
	    mc->pending_new_mc = 0;
	}
    }
}

void
_ipmi_mc_put(ipmi_mc_t *mc)
{
    _ipmi_domain_mc_lock(mc->domain);
    if (mc->usecount == 1) {
	/* Make sure this code cannot run when we release the lock. */
	mc->usecount++;
	ipmi_lock(mc->lock);
	switch (mc->state) {
	case MC_INACTIVE_PEND_STARTUP:
	    mc->state = MC_ACTIVE_IN_STARTUP;
	    mc->active = 1;
	    mc_apply_pending(mc);
	    ipmi_unlock(mc->lock);
	    _ipmi_domain_mc_unlock(mc->domain);
	    mc_startup(mc);
	    call_active_handlers(mc);
	    _ipmi_domain_mc_lock(mc->domain);
	    break;

	case MC_ACTIVE_PEND_FULLY_UP:
	    mc->state = MC_ACTIVE;
	    ipmi_unlock(mc->lock);
	    _ipmi_domain_mc_unlock(mc->domain);
	    call_fully_up_handlers(mc);
	    _ipmi_domain_mc_lock(mc->domain);
	    break;

	case MC_ACTIVE_PEND_CLEANUP:
	    mc_stop_timer(mc);
	    if (mc->startup_count > 0) {
		ipmi_unlock(mc->lock);
		goto still_in_startup;
	    }
	    mc->state = MC_INACTIVE;
	    mc->active = 0;
	    ipmi_unlock(mc->lock);
	    _ipmi_domain_mc_unlock(mc->domain);
	    mc_cleanup(mc);
	    call_active_handlers(mc);
	    _ipmi_domain_mc_lock(mc->domain);
	    break;

	case MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP:
	    mc_stop_timer(mc);
	    if (mc->startup_count > 0) {
		ipmi_unlock(mc->lock);
		goto still_in_startup;
	    }
	    mc->state = MC_INACTIVE;
	    mc->active = 0;
	    ipmi_unlock(mc->lock);
	    _ipmi_domain_mc_unlock(mc->domain);
	    mc_cleanup(mc);
	    call_active_handlers(mc);
	    _ipmi_domain_mc_lock(mc->domain);
	    ipmi_lock(mc->lock);
	    mc->state = MC_ACTIVE_IN_STARTUP;
	    mc->active = 1;
	    mc_apply_pending(mc);
	    ipmi_unlock(mc->lock);
	    _ipmi_domain_mc_unlock(mc->domain);
	    mc_startup(mc);
	    call_active_handlers(mc);
	    _ipmi_domain_mc_lock(mc->domain);
	    break;

	default:
	    ipmi_unlock(mc->lock);
	    break;
	}
    still_in_startup:
	mc->usecount--;

	/* Only attempt the destroy if no one else has gotten the MC
	   while we were holding it. */
	if (mc->usecount == 1) {
	    ipmi_lock(mc->lock);
	    if (check_mc_destroy(mc))
		return;
	    ipmi_unlock(mc->lock);
	}
    }
    mc->usecount--;
    _ipmi_domain_mc_unlock(mc->domain);
}

int
_ipmi_mc_handle_new(ipmi_mc_t *mc)
{
    ipmi_lock(mc->lock);
    switch (mc->state) {
    case MC_INACTIVE:
	_ipmi_get_domain_fully_up(mc->domain, "_ipmi_mc_handle_new");
	mc->state = MC_INACTIVE_PEND_STARTUP;
	break;
    case MC_ACTIVE_PEND_CLEANUP:
	_ipmi_get_domain_fully_up(mc->domain, "_ipmi_mc_handle_new");
	mc->state = MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP;
	break;
    default:
	break;
    }
    ipmi_unlock(mc->lock);
    return 0;
}

void
_ipmi_cleanup_mc(ipmi_mc_t *mc)
{
    ipmi_lock(mc->lock);
    switch (mc->state) {
    case MC_INACTIVE_PEND_STARTUP:
	_ipmi_put_domain_fully_up(mc->domain, "_ipmi_cleanup_mc");
	mc->state = MC_INACTIVE;
	break;
    case MC_ACTIVE_IN_STARTUP:
	mc->state = MC_ACTIVE_PEND_CLEANUP;
	ipmi_unlock(mc->lock);
	ipmi_sdr_cleanout_timer(mc->sdrs);
	/* FIXME - shut down startup code */
	goto out;
    case MC_ACTIVE:
    case MC_ACTIVE_PEND_FULLY_UP:
	mc->state = MC_ACTIVE_PEND_CLEANUP;
	ipmi_unlock(mc->lock);
	ipmi_sdr_cleanout_timer(mc->sdrs);
	goto out;
    case MC_ACTIVE_PEND_CLEANUP_PEND_STARTUP:
	_ipmi_put_domain_fully_up(mc->domain, "_ipmi_cleanup_mc");
	mc->state = MC_ACTIVE_PEND_CLEANUP;
	break;
    default:
	break;
    }
    ipmi_unlock(mc->lock);
 out:
    return;
}

ipmi_mcid_t
ipmi_mc_convert_to_id(ipmi_mc_t *mc)
{
    ipmi_mcid_t val;

    CHECK_MC_LOCK(mc);

    val.domain_id = ipmi_domain_convert_to_id(mc->domain);
    val.mc_num = ipmi_mc_get_address(mc);
    val.channel = ipmi_mc_get_channel(mc);
    val.seq = mc->seq;
    return val;
}

typedef struct mc_ptr_info_s
{
    int            err;
    int            cmp_seq;
    ipmi_mcid_t    id;
    ipmi_mc_ptr_cb handler;
    void           *cb_data;
} mc_ptr_info_t;

static void
mc_ptr_cb(ipmi_domain_t *domain, void *cb_data)
{
    mc_ptr_info_t *info = cb_data;
    ipmi_addr_t   addr;
    unsigned int  addr_len;
    ipmi_mc_t     *mc;

    if (info->id.channel == IPMI_BMC_CHANNEL) {
	ipmi_system_interface_addr_t *si = (void *) &addr;

	si->addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
	si->channel = info->id.mc_num;
	si->lun = 0;
	addr_len = sizeof(*si);
    } else {
	ipmi_ipmb_addr_t *ipmb = (void *) &addr;

	ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
	ipmb->channel = info->id.channel;
	ipmb->slave_addr = info->id.mc_num;
	ipmb->lun = 0;
	addr_len = sizeof(*ipmb);
    }

    mc = _ipmi_find_mc_by_addr(domain, &addr, addr_len);
    if (mc) {
	if (info->cmp_seq && (mc->seq != info->id.seq)) {
	    _ipmi_mc_put(mc);
	    return;
	}

	info->err = 0;
	info->handler(mc, info->cb_data);
	_ipmi_mc_put(mc);
    }
}

int
ipmi_mc_pointer_cb(ipmi_mcid_t id, ipmi_mc_ptr_cb handler, void *cb_data)
{
    int           rv;
    mc_ptr_info_t info;

    info.err = EINVAL;
    info.id = id;
    info.handler = handler;
    info.cb_data = cb_data;
    info.cmp_seq = 1;
    rv = ipmi_domain_pointer_cb(id.domain_id, mc_ptr_cb, &info);
    if (!rv)
	rv = info.err;
    return rv;
}

int
ipmi_mc_pointer_noseq_cb(ipmi_mcid_t    id,
			 ipmi_mc_ptr_cb handler,
			 void           *cb_data)
{
    int           rv;
    mc_ptr_info_t info;

    info.err = EINVAL;
    info.id = id;
    info.handler = handler;
    info.cb_data = cb_data;
    info.cmp_seq = 0;
    rv = ipmi_domain_pointer_cb(id.domain_id, mc_ptr_cb, &info);
    if (!rv)
	rv = info.err;
    return rv;
}

int
ipmi_cmp_mc_id_noseq(ipmi_mcid_t id1, ipmi_mcid_t id2)
{
    int d;

    d = ipmi_cmp_domain_id(id1.domain_id, id2.domain_id);
    if (d)
	return d;

    if (id1.mc_num > id2.mc_num)
	return 1;
    if (id1.mc_num < id2.mc_num)
	return -1;
    if (id1.channel > id2.channel)
	return 1;
    if (id1.channel < id2.channel)
	return -1;
    return 0;
}

int
ipmi_cmp_mc_id(ipmi_mcid_t id1, ipmi_mcid_t id2)
{
    int d;

    d = ipmi_cmp_mc_id_noseq(id1, id2);
    if (d)
	return d;

    if (id1.seq > id2.seq)
	return 1;
    if (id1.seq < id2.seq)
	return -1;
    return 0;
}

void
ipmi_mc_id_set_invalid(ipmi_mcid_t *id)
{
    memset(id, 0, sizeof(*id));
}

int
ipmi_mc_id_is_invalid(ipmi_mcid_t *id)
{
    return (id->domain_id.domain == NULL);
}

/***********************************************************************
 *
 * Handle sending commands and getting responses.
 *
 **********************************************************************/

static int
addr_rsp_handler(ipmi_domain_t *domain, ipmi_msgi_t *rspi)
{
    ipmi_addr_t                *addr = &rspi->addr;
    unsigned int               addr_len = rspi->addr_len;
    ipmi_msg_t                 *msg = &rspi->msg;
    ipmi_mc_response_handler_t rsp_handler = rspi->data2;
    ipmi_mc_t                  *mc;

    if (rsp_handler) {
	if (domain)
	    mc = _ipmi_find_mc_by_addr(domain, addr, addr_len);
	else
	    mc = NULL;
	rsp_handler(mc, msg, rspi->data1);
	if (mc)
	    _ipmi_mc_put(mc);
    }
    return IPMI_MSG_ITEM_NOT_USED;
}

int
ipmi_mc_send_command(ipmi_mc_t                  *mc,
		     unsigned int               lun,
		     const ipmi_msg_t           *msg,
		     ipmi_mc_response_handler_t rsp_handler,
		     void                       *rsp_data)
{
    int           rv;
    ipmi_addr_t   addr = mc->addr;
    ipmi_domain_t *domain;

    CHECK_MC_LOCK(mc);

    rv = ipmi_addr_set_lun(&addr, lun);
    if (rv)
	return rv;

    domain = ipmi_mc_get_domain(mc);

    rv = ipmi_send_command_addr(domain,
				&addr, mc->addr_len,
				msg,
				addr_rsp_handler,
				rsp_data,
				rsp_handler);
    return rv;
}

int
ipmi_mc_send_command_sideeff(ipmi_mc_t                  *mc,
			     unsigned int               lun,
			     const ipmi_msg_t           *msg,
			     ipmi_mc_response_handler_t rsp_handler,
			     void                       *rsp_data)
{
    int           rv;
    ipmi_addr_t   addr = mc->addr;
    ipmi_domain_t *domain;

    CHECK_MC_LOCK(mc);

    rv = ipmi_addr_set_lun(&addr, lun);
    if (rv)
	return rv;

    domain = ipmi_mc_get_domain(mc);

    rv = ipmi_send_command_addr_sideeff(domain,
					&addr, mc->addr_len,
					msg,
					addr_rsp_handler,
					rsp_data,
					rsp_handler);
    return rv;
}

/***********************************************************************
 *
 * Handle global OEM callbacks for new MCs.
 *
 **********************************************************************/

typedef struct oem_handlers_s {
    unsigned int                 manufacturer_id;
    unsigned int                 first_product_id;
    unsigned int                 last_product_id;
    ipmi_oem_mc_match_handler_cb handler;
    ipmi_oem_shutdown_handler_cb shutdown;
    void                         *cb_data;
} oem_handlers_t;

static locked_list_t *oem_handlers;

int
ipmi_register_oem_handler(unsigned int                 manufacturer_id,
			  unsigned int                 product_id,
			  ipmi_oem_mc_match_handler_cb handler,
			  ipmi_oem_shutdown_handler_cb shutdown,
			  void                         *cb_data)
{
    oem_handlers_t *new_item;
    int            rv;

    /* This might be called before initialization, so be 100% sure. */
    rv = _ipmi_mc_init();
    if (rv)
	return rv;

    new_item = ipmi_mem_alloc(sizeof(*new_item));
    if (!new_item)
	return ENOMEM;

    new_item->manufacturer_id = manufacturer_id;
    new_item->first_product_id = product_id;
    new_item->last_product_id = product_id;
    new_item->handler = handler;
    new_item->shutdown = shutdown;
    new_item->cb_data = cb_data;

    if (! locked_list_add(oem_handlers, new_item, NULL)) {
	ipmi_mem_free(new_item);
	return ENOMEM;
    }

    return 0;
}

int
ipmi_register_oem_handler_range(unsigned int                 manufacturer_id,
				unsigned int                 first_product_id,
				unsigned int                 last_product_id,
				ipmi_oem_mc_match_handler_cb handler,
				ipmi_oem_shutdown_handler_cb shutdown,
				void                         *cb_data)
{
    oem_handlers_t *new_item;
    int            rv;

    /* This might be called before initialization, so be 100% sure. */
    rv = _ipmi_mc_init();
    if (rv)
	return rv;

    new_item = ipmi_mem_alloc(sizeof(*new_item));
    if (!new_item)
	return ENOMEM;

    new_item->manufacturer_id = manufacturer_id;
    new_item->first_product_id = first_product_id;
    new_item->last_product_id = last_product_id;
    new_item->handler = handler;
    new_item->shutdown = shutdown;
    new_item->cb_data = cb_data;

    if (! locked_list_add(oem_handlers, new_item, NULL)) {
	ipmi_mem_free(new_item);
	return ENOMEM;
    }

    return 0;
}

typedef struct handler_cmp_s
{
    int          rv;
    unsigned int manufacturer_id;
    unsigned int first_product_id;
    unsigned int last_product_id;
    ipmi_mc_t    *mc;
} handler_cmp_t;

static int
oem_handler_cmp_dereg(void *cb_data, void *item1, void *item2)
{
    oem_handlers_t *hndlr = item1;
    handler_cmp_t  *cmp = cb_data;

    if ((hndlr->manufacturer_id == cmp->manufacturer_id)
	&& (hndlr->first_product_id <= cmp->first_product_id)
	&& (hndlr->last_product_id >= cmp->last_product_id))
    {
	cmp->rv = 0;
	locked_list_remove(oem_handlers, item1, item2);
	ipmi_mem_free(hndlr);
	return LOCKED_LIST_ITER_STOP;
    }
    return LOCKED_LIST_ITER_CONTINUE;
}

int
ipmi_deregister_oem_handler(unsigned int manufacturer_id,
			    unsigned int product_id)
{
    handler_cmp_t  tmp;

    tmp.rv = ENOENT;
    tmp.manufacturer_id = manufacturer_id;
    tmp.first_product_id = product_id;
    tmp.last_product_id = product_id;
    locked_list_iterate(oem_handlers, oem_handler_cmp_dereg, &tmp);
    return tmp.rv;
}

int
ipmi_deregister_oem_handler_range(unsigned int manufacturer_id,
				  unsigned int first_product_id,
				  unsigned int last_product_id)
{
    handler_cmp_t  tmp;

    tmp.rv = ENOENT;
    tmp.manufacturer_id = manufacturer_id;
    tmp.first_product_id = first_product_id;
    tmp.last_product_id = last_product_id;
    locked_list_iterate(oem_handlers, oem_handler_cmp_dereg, &tmp);
    return tmp.rv;
}

static int
oem_handler_call(void *cb_data, void *item1, void *item2)
{
    oem_handlers_t *hndlr = item1;
    handler_cmp_t  *cmp = cb_data;

    if ((hndlr->manufacturer_id == cmp->manufacturer_id)
	&& (hndlr->first_product_id <= cmp->first_product_id)
	&& (hndlr->last_product_id >= cmp->last_product_id))
    {
	cmp->rv = hndlr->handler(cmp->mc, hndlr->cb_data);
	return LOCKED_LIST_ITER_STOP;
    }
    return LOCKED_LIST_ITER_CONTINUE;
}

static int
check_oem_handlers(ipmi_mc_t *mc)
{
    handler_cmp_t  tmp;

    tmp.rv = 0;
    tmp.manufacturer_id = mc->pending_devid.manufacturer_id;
    tmp.first_product_id = mc->pending_devid.product_id;
    tmp.last_product_id = mc->pending_devid.product_id;
    tmp.mc = mc;
    locked_list_iterate(oem_handlers, oem_handler_call, &tmp);
    return tmp.rv;
}


/***********************************************************************
 *
 * device SDR handling.
 *
 **********************************************************************/

typedef struct sdr_fetch_info_s
{
    ipmi_domain_t    *domain;
    ipmi_mcid_t      source_mc; /* This is used to scan the SDRs. */
    ipmi_mc_done_cb  done;
    void             *done_data;
    int              err;
    int              changed;
    ipmi_sdr_info_t  *sdrs;
} sdr_fetch_info_t;

int
ipmi_mc_set_main_sdrs_as_device(ipmi_mc_t *mc)
{
    int             rv;
    ipmi_sdr_info_t *new_sdrs;

    rv = ipmi_sdr_info_alloc(ipmi_mc_get_domain(mc), mc, 0, 0, &new_sdrs);
    if (rv)
	return rv;

    mc->treat_main_as_device_sdrs = 1;
    if (mc->sdrs)
	ipmi_sdr_info_destroy(mc->sdrs, NULL, NULL);
    mc->sdrs = new_sdrs;

    /* Note that we don't reread the sensors, so this must be done
       before the sensor read operation. */
    return 0;
}

static void
sdr_reread_done(sdr_fetch_info_t *info, ipmi_mc_t *mc, int err)
{
    if (info->done)
	info->done(mc, err, info->done_data);
    ipmi_mem_free(info);
}

static void
sdrs_fetched_mc_cb(ipmi_mc_t *mc, void *cb_data)
{
    sdr_fetch_info_t *info = (sdr_fetch_info_t *) cb_data;
    int              rv = 0;

    if (info->err) {
	sdr_reread_done(info, mc, info->err);
	return;
    }

    if (mc->fixup_sdrs_handler)
	mc->fixup_sdrs_handler(mc, info->sdrs, mc->fixup_sdrs_cb_data);

    if (info->changed) {
	ipmi_entity_scan_sdrs(info->domain, mc,
			      ipmi_domain_get_entities(info->domain),
			      info->sdrs);
	rv = ipmi_sensor_handle_sdrs(info->domain, mc, info->sdrs);

	if (!rv)
	    ipmi_detect_domain_presence_changes(info->domain, 0);
    }

    sdr_reread_done(info, mc, rv);
}

static void
sdrs_fetched(ipmi_sdr_info_t *sdrs,
	     int             err,
	     int             changed,
	     unsigned int    count,
	     void            *cb_data)
{
    sdr_fetch_info_t *info = (sdr_fetch_info_t *) cb_data;
    int              rv = 0;

    info->err = err;
    info->changed = changed;
    info->sdrs = sdrs;
    rv = ipmi_mc_pointer_cb(info->source_mc, sdrs_fetched_mc_cb, info);
    if (rv)
	sdr_reread_done(info, NULL, ECANCELED);
}

int
ipmi_mc_reread_sensors(ipmi_mc_t       *mc,
		       ipmi_mc_done_cb done,
		       void            *done_data)
{
    sdr_fetch_info_t   *info;
    int                rv = 0;
    ipmi_sensor_info_t *sensors;

    CHECK_MC_LOCK(mc);

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;

    sensors = _ipmi_mc_get_sensors(mc);

    info->source_mc = ipmi_mc_convert_to_id(mc);
    info->domain = ipmi_mc_get_domain(mc);
    info->done = done;
    info->done_data = done_data;

    ipmi_lock(mc->lock);
    if (! mc_op_ready(mc)) {
	ipmi_unlock(mc->lock);
	rv = ECANCELED;
    } else {
	ipmi_unlock(mc->lock);
	rv = ipmi_sdr_fetch(ipmi_mc_get_sdrs(mc), sdrs_fetched, info);
    }
    if (rv)
	ipmi_mem_free(info);

    return rv;
}

/***********************************************************************
 *
 * Checking for the validity and currentness of MC data.
 *
 **********************************************************************/

/* Check the MC, we reread the SDRs and check the event receiver. */
void
_ipmi_mc_check_mc(ipmi_mc_t *mc)
{
    if ((mc->devid.provides_device_sdrs) || (mc->treat_main_as_device_sdrs))
	ipmi_mc_reread_sensors(mc, NULL, NULL);
    _ipmi_mc_check_event_rcvr(mc);
}



/***********************************************************************
 *
 * Handle the boatloads of information from a get device id.
 *
 **********************************************************************/

int
_ipmi_mc_get_device_id_data_from_rsp(ipmi_mc_t *mc, ipmi_msg_t *rsp)
{
    unsigned char *rsp_data = rsp->data;
    int           rv = 0;

    if (rsp_data[0] != 0) {
	return IPMI_IPMI_ERR_VAL(rsp_data[0]);
    }

    if (rsp->data_len < 12) {
	if ((rsp->data[0] == 0) && (rsp->data_len >= 6)) {
	    int major_version = rsp->data[5] & 0xf;
	    int minor_version = (rsp->data[5] >> 4) & 0xf;

	    if (major_version < 1) {
		ipmi_log(IPMI_LOG_ERR_INFO,
			 "%smc.c(_ipmi_mc_get_device_id_data_from_rsp): "
			 "IPMI version of the MC at address 0x%2.2x is %d.%d,"
			 " which is older than OpenIPMI supports",
			 mc->name, ipmi_addr_get_slave_addr(&mc->addr),
			 major_version, minor_version);
		return EINVAL;
	    }
	}
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(_ipmi_mc_get_device_id_data_from_rsp): "
		 "Invalid return from IPMI Get Device ID from address 0x%2.2x,"
		 " something is seriously wrong with the MC, length is %d",
		 mc->name, ipmi_addr_get_slave_addr(&mc->addr), rsp->data_len);
	return EINVAL;
    }

    ipmi_lock(mc->lock);

    /* Pend these to be installed when nobody is using them. */
    mc->pending_devid.device_id = rsp_data[1];
    mc->pending_devid.device_revision = rsp_data[2] & 0xf;
    mc->pending_devid.provides_device_sdrs = (rsp_data[2] & 0x80) == 0x80;
    mc->pending_devid.device_available = (rsp_data[3] & 0x80) == 0x80;
    mc->pending_devid.major_fw_revision = rsp_data[3] & 0x7f;
    mc->pending_devid.minor_fw_revision = rsp_data[4];
    mc->pending_devid.major_version = rsp_data[5] & 0xf;
    mc->pending_devid.minor_version = (rsp_data[5] >> 4) & 0xf;
    mc->pending_devid.chassis_support = (rsp_data[6] & 0x80) == 0x80;
    mc->pending_devid.bridge_support = (rsp_data[6] & 0x40) == 0x40;
    mc->pending_devid.IPMB_event_generator_support
	= (rsp_data[6] & 0x20) == 0x20;
    mc->pending_devid.IPMB_event_receiver_support
	= (rsp_data[6] & 0x10) == 0x10;
    mc->pending_devid.FRU_inventory_support = (rsp_data[6] & 0x08) == 0x08;
    mc->pending_devid.SEL_device_support = (rsp_data[6] & 0x04) == 0x04;
    mc->pending_devid.SDR_repository_support = (rsp_data[6] & 0x02) == 0x02;
    mc->pending_devid.sensor_device_support = (rsp_data[6] & 0x01) == 0x01;
    mc->pending_devid.manufacturer_id = (rsp_data[7]
				 | (rsp_data[8] << 8)
				 | (rsp_data[9] << 16));
    mc->pending_devid.product_id = rsp_data[10] | (rsp_data[11] << 8);

    if (rsp->data_len < 16) {
	/* no aux revision. */
	memset(mc->pending_devid.aux_fw_revision, 0, 4);
    } else {
	memcpy(mc->pending_devid.aux_fw_revision, rsp_data + 12, 4);
    }

    /* Copy these to the version we use for comparison. */
    mc->real_devid = mc->pending_devid;

    /* Either copy it or mark it to be copied. */
    if (mc->usecount == 1) {
	mc->devid = mc->pending_devid;
	mc->pending_devid_data = 0;
	mc->pending_new_mc = 0;
	ipmi_unlock(mc->lock);

	/* OEM handlers set the pending data. */
	rv = check_oem_handlers(mc);
    } else {
	mc->pending_devid_data = 1;
	mc->pending_new_mc = 1;
	rv = EAGAIN; /* Tell the user that they must call the OEM
			handlers check later when the MC is
			released. */
	ipmi_unlock(mc->lock);
    }

    return rv;
}

int
_ipmi_mc_device_data_compares(ipmi_mc_t  *mc,
			      ipmi_msg_t *rsp)
{
    unsigned char *rsp_data = rsp->data;

    if (rsp->data_len < 12) {
	return EINVAL;
    }

    if (mc->real_devid.device_id != rsp_data[1])
	return 0;

    if (mc->real_devid.device_revision != (rsp_data[2] & 0xf))
	return 0;
    
    if (mc->real_devid.provides_device_sdrs != ((rsp_data[2] & 0x80) == 0x80))
	return 0;

    if (mc->real_devid.device_available != ((rsp_data[3] & 0x80) == 0x80))
	return 0;

    if (mc->real_devid.major_fw_revision != (rsp_data[3] & 0x7f))
	return 0;

    if (mc->real_devid.minor_fw_revision != (rsp_data[4]))
	return 0;

    if (mc->real_devid.major_version != (rsp_data[5] & 0xf))
	return 0;

    if (mc->real_devid.minor_version != ((rsp_data[5] >> 4) & 0xf))
	return 0;

    if (mc->real_devid.chassis_support != ((rsp_data[6] & 0x80) == 0x80))
	return 0;

    if (mc->real_devid.bridge_support != ((rsp_data[6] & 0x40) == 0x40))
	return 0;

    if (mc->real_devid.IPMB_event_generator_support
	!= ((rsp_data[6] & 0x20)==0x20))
	return 0;

    if (mc->real_devid.IPMB_event_receiver_support
	!= ((rsp_data[6] & 0x10) == 0x10))
	return 0;

    if (mc->real_devid.FRU_inventory_support != ((rsp_data[6] & 0x08) == 0x08))
	return 0;

    if (mc->real_devid.SEL_device_support != ((rsp_data[6] & 0x04) == 0x04))
	return 0;

    if (mc->real_devid.SDR_repository_support
	!= ((rsp_data[6] & 0x02) == 0x02))
	return 0;

    if (mc->real_devid.sensor_device_support != ((rsp_data[6] & 0x01) == 0x01))
	return 0;

    if (mc->real_devid.manufacturer_id != (uint32_t) (rsp_data[7]
						      | (rsp_data[8] << 8)
						      | (rsp_data[9] << 16)))
	return 0;

    if (mc->real_devid.product_id != (rsp_data[10] | (rsp_data[11] << 8)))
	return 0;

    if (rsp->data_len < 16) {
	/* no aux revision, it should be all zeros. */
	if ((mc->real_devid.aux_fw_revision[0] != 0)
	    || (mc->real_devid.aux_fw_revision[1] != 0)
	    || (mc->real_devid.aux_fw_revision[2] != 0)
	    || (mc->real_devid.aux_fw_revision[3] != 0))
	    return 0;
    } else {
	if (memcmp(mc->real_devid.aux_fw_revision, rsp_data + 12, 4) != 0)
	    return 0;
    }

    /* Everything's the same. */
    return 1;
}

/***********************************************************************
 *
 * Get/set the information for an MC.
 *
 **********************************************************************/

void
_ipmi_mc_get_sdr_sensors(ipmi_mc_t     *mc,
			 ipmi_sensor_t ***sensors,
			 unsigned int  *count)
{
    *sensors = mc->sensors_in_my_sdr;
    *count = mc->sensors_in_my_sdr_count;
}

void
_ipmi_mc_set_sdr_sensors(ipmi_mc_t     *mc,
			 ipmi_sensor_t **sensors,
			 unsigned int  count)
{
    mc->sensors_in_my_sdr = sensors;
    mc->sensors_in_my_sdr_count = count;
}

void *
_ipmi_mc_get_sdr_entities(ipmi_mc_t *mc)
{
    return mc->entities_in_my_sdr;
}

void
_ipmi_mc_set_sdr_entities(ipmi_mc_t *mc, void *entities)
{
    mc->entities_in_my_sdr = entities;
}

int
ipmi_mc_provides_device_sdrs(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.provides_device_sdrs;
}

int
ipmi_mc_set_sdrs_first_read_handler(ipmi_mc_t      *mc,
				    ipmi_mc_ptr_cb handler,
				    void           *cb_data)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->sdrs_first_read_handler = handler;
    mc->sdrs_first_read_cb_data = cb_data;
    ipmi_unlock(mc->lock);
    return 0;
}

int ipmi_mc_set_sels_first_read_handler(ipmi_mc_t      *mc,
					ipmi_mc_ptr_cb handler,
					void           *cb_data)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->sel_timer_info->lock);
    mc->sel_timer_info->sels_first_read_handler = handler;
    mc->sel_timer_info->sels_first_read_cb_data = cb_data;
    ipmi_unlock(mc->sel_timer_info->lock);
    return 0;
}

static int
call_active_handler(void *cb_data, void *item1, void *item2)
{
    ipmi_mc_active_cb handler = item1;
    ipmi_mc_t         *mc = cb_data;

    handler(mc, mc->active, item2);
    return LOCKED_LIST_ITER_CONTINUE;
}

static void
call_active_handlers(ipmi_mc_t *mc)
{
    locked_list_iterate(mc->active_handlers, call_active_handler, mc);
}

int
ipmi_mc_add_active_handler(ipmi_mc_t         *mc,
			   ipmi_mc_active_cb handler,
			   void              *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_add(mc->active_handlers, handler, cb_data))
	return 0;
    else
	return ENOMEM;
}

int
ipmi_mc_remove_active_handler(ipmi_mc_t         *mc,
			      ipmi_mc_active_cb handler,
			      void              *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_remove(mc->active_handlers, handler, cb_data))
	return 0;
    else
	return EINVAL;
}

int
ipmi_mc_add_active_handler_cl(ipmi_mc_t            *mc,
			      ipmi_mc_active_cl_cb handler,
			      void                 *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_add(mc->active_handlers_cl, handler, cb_data))
	return 0;
    else
	return ENOMEM;
}

int
ipmi_mc_remove_active_handler_cl(ipmi_mc_t            *mc,
				 ipmi_mc_active_cl_cb handler,
				 void                 *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_remove(mc->active_handlers_cl, handler, cb_data))
	return 0;
    else
	return EINVAL;
}

int
ipmi_mc_is_active(ipmi_mc_t *mc)
{
    return mc->active;
}

void
_ipmi_mc_force_active(ipmi_mc_t *mc, int val)
{
    ipmi_lock(mc->lock);
    mc->active = val;
    ipmi_unlock(mc->lock);
}

static int
call_fully_up_handler(void *cb_data, void *item1, void *item2)
{
    ipmi_mc_ptr_cb handler = item1;
    ipmi_mc_t      *mc = cb_data;

    handler(mc, item2);
    return LOCKED_LIST_ITER_CONTINUE;
}

static void
call_fully_up_handlers(ipmi_mc_t *mc)
{
    locked_list_iterate(mc->fully_up_handlers, call_fully_up_handler, mc);
}

int
ipmi_mc_add_fully_up_handler(ipmi_mc_t      *mc,
			     ipmi_mc_ptr_cb handler,
			     void           *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_add(mc->fully_up_handlers, handler, cb_data))
	return 0;
    else
	return ENOMEM;
}

int
ipmi_mc_remove_fully_up_handler(ipmi_mc_t      *mc,
				ipmi_mc_ptr_cb handler,
				void           *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_remove(mc->fully_up_handlers, handler, cb_data))
	return 0;
    else
	return EINVAL;
}

int
ipmi_mc_add_fully_up_handler_cl(ipmi_mc_t              *mc,
				ipmi_mc_fully_up_cl_cb handler,
				void                   *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_add(mc->fully_up_handlers_cl, handler, cb_data))
	return 0;
    else
	return ENOMEM;
}

int
ipmi_mc_remove_fully_up_handler_cl(ipmi_mc_t              *mc,
				   ipmi_mc_fully_up_cl_cb handler,
				   void                   *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_remove(mc->fully_up_handlers_cl, handler, cb_data))
	return 0;
    else
	return EINVAL;
}

void
ipmi_mc_set_provides_device_sdrs(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.provides_device_sdrs = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_device_available(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.device_available;
}

void
ipmi_mc_set_device_available(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.device_available = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_chassis_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.chassis_support;
}

void
ipmi_mc_set_chassis_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.chassis_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_bridge_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.bridge_support;
}

void
ipmi_mc_set_bridge_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.bridge_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_ipmb_event_generator_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.IPMB_event_generator_support;
}

void
ipmi_mc_set_ipmb_event_generator_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.IPMB_event_generator_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_ipmb_event_receiver_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.IPMB_event_receiver_support;
}

void
ipmi_mc_set_ipmb_event_receiver_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.IPMB_event_receiver_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_fru_inventory_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.FRU_inventory_support;
}

void
ipmi_mc_set_fru_inventory_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.FRU_inventory_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_sel_device_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.SEL_device_support;
}

void
ipmi_mc_set_sel_device_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.SEL_device_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_sdr_repository_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.SDR_repository_support;
}

void
ipmi_mc_set_sdr_repository_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.SDR_repository_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_sensor_device_support(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.sensor_device_support;
}

void
ipmi_mc_set_sensor_device_support(ipmi_mc_t *mc, int val)
{
    CHECK_MC_LOCK(mc);
    ipmi_lock(mc->lock);
    mc->pending_devid.sensor_device_support = val;
    mc->pending_devid_data = 1;
    ipmi_unlock(mc->lock);
}

int
ipmi_mc_device_id(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.device_id;
}

int
ipmi_mc_device_revision(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.device_revision;
}

int
ipmi_mc_major_fw_revision(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.major_fw_revision;
}

int
ipmi_mc_minor_fw_revision(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.minor_fw_revision;
}

int
ipmi_mc_major_version(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.major_version;
}

int
ipmi_mc_minor_version(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.minor_version;
}

int
ipmi_mc_manufacturer_id(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.manufacturer_id;
}

int
ipmi_mc_product_id(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->devid.product_id;
}

void
ipmi_mc_aux_fw_revision(ipmi_mc_t *mc, unsigned char val[])
{
    CHECK_MC_LOCK(mc);
    memcpy(val, mc->devid.aux_fw_revision, sizeof(mc->devid.aux_fw_revision));
}

int
ipmi_mc_get_guid(ipmi_mc_t *mc, unsigned char *guid)
{
    CHECK_MC_LOCK(mc);
    if (!mc->guid_set)
	return ENOSYS;
    memcpy(guid, mc->guid, 16);
    return 0;
}

void
ipmi_mc_set_guid(ipmi_mc_t *mc, unsigned char *data)
{
    memcpy(mc->guid, data, 16);
    mc->guid_set = 1;
}

void
ipmi_mc_set_oem_data(ipmi_mc_t *mc, void *data)
{
    CHECK_MC_LOCK(mc);
    mc->oem_data = data;
}

void *
ipmi_mc_get_oem_data(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->oem_data;
}

ipmi_sensor_info_t *
_ipmi_mc_get_sensors(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->sensors;
}

ipmi_control_info_t *
_ipmi_mc_get_controls(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->controls;
}

ipmi_sdr_info_t *
ipmi_mc_get_sdrs(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    return mc->sdrs;
}

unsigned int
ipmi_mc_get_address(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    if (mc->addr.addr_type == IPMI_IPMB_ADDR_TYPE) {
	ipmi_ipmb_addr_t *ipmb = (ipmi_ipmb_addr_t *) &(mc->addr);
	return ipmb->slave_addr;
    } else if (mc->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE) {
	ipmi_system_interface_addr_t *si = (void *) &(mc->addr);
	return si->channel;
    }

    /* Address is ignore for other types. */
    return 0;
}

void
ipmi_mc_get_ipmi_address(ipmi_mc_t    *mc,
			 ipmi_addr_t  *addr,
			 unsigned int *addr_len)
{
    /* We don't check the lock here because this is used as part of
       the lock grabbing. */
    if (addr)
	memcpy(addr, &mc->addr, mc->addr_len);
    if (addr_len)
	*addr_len = mc->addr_len;
}

unsigned int
ipmi_mc_get_channel(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);
    if (mc->addr.addr_type == IPMI_SYSTEM_INTERFACE_ADDR_TYPE)
	return IPMI_BMC_CHANNEL;
    else
	return mc->addr.channel;
}

ipmi_domain_t *ipmi_mc_get_domain(ipmi_mc_t *mc)
{
    return mc->domain;
}

unsigned int
ipmi_mc_get_unique_num(ipmi_mc_t *mc)
{
    unsigned int rv;

    ipmi_lock(mc->lock);
    rv = mc->uniq_num;
    mc->uniq_num++;
    ipmi_unlock(mc->lock);
    return rv;
}

int
_ipmi_mc_new_sensor(ipmi_mc_t     *mc,
		    ipmi_entity_t *ent,
		    ipmi_sensor_t *sensor,
		    void          *link)
{
    int rv = 0;

    CHECK_MC_LOCK(mc);

    if (mc->new_sensor_handler)
	rv = mc->new_sensor_handler(mc, ent, sensor, link,
				    mc->new_sensor_cb_data);
    return rv;
}

int
ipmi_mc_set_oem_new_sensor_handler(ipmi_mc_t                 *mc,
				   ipmi_mc_oem_new_sensor_cb handler,
				   void                      *cb_data)
{
    CHECK_MC_LOCK(mc);
    mc->new_sensor_handler = handler;
    mc->new_sensor_cb_data = cb_data;
    return 0;
}

int
ipmi_mc_set_sdrs_fixup_handler(ipmi_mc_t                 *mc,
			       ipmi_mc_oem_fixup_sdrs_cb handler,
			       void                      *cb_data)
{
    CHECK_MC_LOCK(mc);
    mc->fixup_sdrs_handler = handler;
    mc->fixup_sdrs_cb_data = cb_data;
    return 0;
}

int
ipmi_mc_add_oem_removed_handler(ipmi_mc_t              *mc,
				ipmi_mc_oem_removed_cb handler,
				void                   *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_add(mc->removed_handlers, handler, cb_data))
	return 0;
    else
	return ENOMEM;
}

int
ipmi_mc_remove_oem_removed_handler(ipmi_mc_t              *mc,
				   ipmi_mc_oem_removed_cb handler,
				   void                   *cb_data)
{
    CHECK_MC_LOCK(mc);

    if (locked_list_remove(mc->removed_handlers, handler, cb_data))
	return 0;
    else
	return EINVAL;
}

int
ipmi_mc_get_events_enable(ipmi_mc_t *mc)
{
    CHECK_MC_LOCK(mc);

    return mc->events_enabled;
}

int
ipmi_mc_set_events_enable(ipmi_mc_t       *mc,
			  int             val,
			  ipmi_mc_done_cb done,
			  void            *cb_data)
{
    int rv;

    CHECK_MC_LOCK(mc);

    if (!ipmi_mc_ipmb_event_generator_support(mc))
	return ENOSYS;

    val = val != 0;

    ipmi_lock(mc->lock);
    if (val == mc->events_enabled) {
	/* Didn't changed, just finish the operation. */
	ipmi_unlock(mc->lock);
	if (done)
	    done(mc, 0, cb_data);
	return 0;
    }

    mc->events_enabled = val;
    
    if (val) {
	unsigned int event_rcvr = ipmi_domain_get_event_rcvr(mc->domain);
	rv = send_set_event_rcvr(mc, event_rcvr, done, cb_data);
    } else {
	rv = send_set_event_rcvr(mc, 0, done, cb_data);
    }
    ipmi_unlock(mc->lock);

    return rv;
}

typedef struct ipmi_get_event_log_info_s
{
    ipmi_mc_data_done_cb done;
    void                 *cb_data;
} ipmi_get_event_log_info_t;

static void
got_event_log_enable(ipmi_mc_t  *mc,
		     ipmi_msg_t *rsp,
		     void       *cb_data)
{
    ipmi_get_event_log_info_t *info = cb_data;

    if (rsp->data[0] != 0) {
	info->done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), 0, info->cb_data);
	goto out;
    }

    if (rsp->data_len < 2) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(got_event_log_enable): response too small",
		 mc->name);
	info->done(mc, EINVAL, 0, info->cb_data);
	goto out;
    }

    info->done(mc, 0, (rsp->data[1] >> 3) & 1, info->cb_data);

 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_get_event_log_enable(ipmi_mc_t            *mc,
			     ipmi_mc_data_done_cb done,
			     void                 *cb_data)
{
    int                       rv;
    ipmi_msg_t                msg;
    ipmi_get_event_log_info_t *info;

    info = ipmi_mem_alloc(sizeof(*info));
    if(!info)
	return ENOMEM;

    info->done = done;
    info->cb_data = cb_data;

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
    msg.data = NULL;
    msg.data_len = 0;

    rv = ipmi_mc_send_command(mc, 0, &msg, got_event_log_enable, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}

typedef struct ipmi_set_event_log_info_s
{
    ipmi_mc_done_cb done;
    void            *cb_data;
    int             val;
} ipmi_set_event_log_info_t;

static void
set_event_log_enable_2(ipmi_mc_t  *mc,
		       ipmi_msg_t *rsp,
		       void       *cb_data)
{
    ipmi_set_event_log_info_t *info = cb_data;


    if (rsp->data[0] != 0) {
	if (info->done)
	    info->done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info->cb_data);
	goto out;
    }

    if (info->done)
	info->done(mc, 0, info->cb_data);
 out:
    ipmi_mem_free(info);
}

static void
set_event_log_enable(ipmi_mc_t  *mc,
		     ipmi_msg_t *rsp,
		     void       *cb_data)
{
    ipmi_set_event_log_info_t *info = cb_data;
    int                       rv;
    ipmi_msg_t                msg;
    unsigned char             data[1];


    if (rsp->data[0] != 0) {
	if (info->done)
	    info->done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info->cb_data);
	goto out_err;
    }

    if (rsp->data_len < 2) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(set_event_log_enable): response too small",
		 mc->name);
	if (info->done)
	    info->done(mc, EINVAL, info->cb_data);
	goto out_err;
    }

    data[0] = (rsp->data[1] & ~0x08) | (info->val << 3);
    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_BMC_GLOBAL_ENABLES_CMD;
    msg.data = data;
    msg.data_len = 1;

    rv = ipmi_mc_send_command(mc, 0, &msg, set_event_log_enable_2, info);
    if (rv) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(set_event_log_enable): Can't send set: 0x%x",
		 mc->name, rv);
	if (info->done)
	    info->done(mc, rv, info->cb_data);
	goto out_err;
    }
    return;

 out_err:
    ipmi_mem_free(info);
}

int
ipmi_mc_set_event_log_enable(ipmi_mc_t       *mc,
			     int             val,
			     ipmi_mc_done_cb done,
			     void            *cb_data)
{
    int                       rv;
    ipmi_msg_t                msg;
    ipmi_set_event_log_info_t *info;

    info = ipmi_mem_alloc(sizeof(*info));
    if(!info)
	return ENOMEM;

    info->done = done;
    info->cb_data = cb_data;
    info->val = val != 0;

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_BMC_GLOBAL_ENABLES_CMD;
    msg.data = NULL;
    msg.data_len = 0;

    rv = ipmi_mc_send_command(mc, 0, &msg, set_event_log_enable, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}

/***********************************************************************
 *
 * Global initialization and shutdown
 *
 **********************************************************************/

static int mc_initialized = 0;

int
_ipmi_mc_init(void)
{
    if (mc_initialized)
	return 0;

    oem_handlers = locked_list_alloc(ipmi_get_global_os_handler());
    if (!oem_handlers)
	return ENOMEM;

    mc_initialized = 1;

    return 0;
}

static int
oem_handler_free(void *cb_data, void *item1, void *item2)
{
    oem_handlers_t *hndlr = item1;

    locked_list_remove(oem_handlers, item1, item2);
    ipmi_mem_free(hndlr);
    return LOCKED_LIST_ITER_CONTINUE;
}

void
_ipmi_mc_shutdown(void)
{
    if (mc_initialized) {
	/* Destroy the members of the OEM list. */
	locked_list_iterate(oem_handlers, oem_handler_free, NULL);
	locked_list_destroy(oem_handlers);
	oem_handlers = NULL;
	mc_initialized = 0;
    }
}

/***********************************************************************
 *
 * Lock checking
 *
 **********************************************************************/

void
__ipmi_check_mc_lock(const ipmi_mc_t *mc)
{
    if (!mc)
	return;

    if (!DEBUG_LOCKS)
	return;

    if (mc->usecount == 0)
	ipmi_report_lock_error(ipmi_domain_get_os_hnd(mc->domain),
			       "MC not locked when it should have been");
	
}

/***********************************************************************
 *
 * Channel handling
 *
 **********************************************************************/

struct ipmi_channel_info_s
{
    unsigned int channel : 4;
    unsigned int medium : 7;
    unsigned int protocol : 5;
    unsigned int session_support : 2;
    unsigned char vendor_id[3];
    unsigned char aux_info[2];

    ipmi_channel_info_cb handler;
    void                 *cb_data;
};

static void
got_chan_info(ipmi_mc_t  *mc,
	      ipmi_msg_t *rsp,
	      void       *cb_data)
{
    ipmi_channel_info_t *info = cb_data;

    if (rsp->data[0] != 0) {
	info->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info,
		      info->cb_data);
	goto out;
    }

    if (rsp->data_len < 10) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(got_chan_info): Channel info response too small",
		 mc->name);
	info->handler(mc, EINVAL, info, info->cb_data);
	goto out;
    }

    info->channel = rsp->data[1] & 0xf;
    info->medium = rsp->data[2] & 0x7f;
    info->protocol = rsp->data[3] & 0x1f;
    info->session_support = rsp->data[4] >> 6;
    memcpy(info->vendor_id, rsp->data+5, 3);
    memcpy(info->aux_info, rsp->data+8, 2);
    info->handler(mc, 0, info, info->cb_data);

 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_channel_get_info(ipmi_mc_t            *mc,
			 unsigned int         channel,
			 ipmi_channel_info_cb handler,
			 void                 *cb_data)
{
    int                 rv;
    ipmi_msg_t          msg;
    unsigned char       data[1];
    ipmi_channel_info_t *info;

    if (channel > 15)
	return EINVAL;

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;
    memset(info, 0, sizeof(*info));

    info->handler = handler;
    info->cb_data = cb_data;

    data[0] = channel & 0xf;
    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_CHANNEL_INFO_CMD;
    msg.data = data;
    msg.data_len = 1;

    rv = ipmi_mc_send_command(mc, 0, &msg, got_chan_info, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}

ipmi_channel_info_t *
ipmi_channel_info_copy(ipmi_channel_info_t *info)
{
    ipmi_channel_info_t *rv = ipmi_mem_alloc(sizeof(*rv));
    if (!rv)
	return NULL;
    memcpy(rv, info, sizeof(*rv));
    return rv;
}

void
ipmi_channel_info_free(ipmi_channel_info_t *info)
{
    ipmi_mem_free(info);
}

int
ipmi_channel_info_get_channel(ipmi_channel_info_t *info,
			      unsigned int        *channel)
{
    *channel = info->channel;
    return 0;
}

int
ipmi_channel_info_get_medium(ipmi_channel_info_t *info,
			     unsigned int        *medium)
{
    *medium = info->medium;
    return 0;
}

int
ipmi_channel_info_get_protocol_type(ipmi_channel_info_t *info,
				    unsigned int        *prot_type)
{
    *prot_type = info->protocol;
    return 0;
}

int
ipmi_channel_info_get_session_support(ipmi_channel_info_t *info,
				      unsigned int        *sup)
{
    *sup = info->session_support;
    return 0;
}

int
ipmi_channel_info_get_vendor_id(ipmi_channel_info_t *info,
				unsigned char       *data)
{
    memcpy(data, info->vendor_id, 3);
    return 0;
}

int
ipmi_channel_info_get_aux_info(ipmi_channel_info_t *info,
			       unsigned char *data)
{
    memcpy(data, info->aux_info, 2);
    return 0;
}

struct ipmi_channel_access_s
{
    unsigned int channel : 4;
    unsigned int alert_set : 1;
    unsigned int alert_val : 1;
    unsigned int msg_auth_set : 1;
    unsigned int msg_auth_val : 1;
    unsigned int user_auth_set : 1;
    unsigned int user_auth_val : 1;
    unsigned int access_mode_set : 1;
    unsigned int access_mode_val : 3;
    unsigned int privilege_set : 1;
    unsigned int privilege_val : 4;

    ipmi_channel_access_cb handler;
    ipmi_mc_done_cb        done;
    void                   *cb_data;
};

static void
got_chan_access(ipmi_mc_t  *mc,
		ipmi_msg_t *rsp,
		void       *cb_data)
{
    ipmi_channel_access_t *info = cb_data;

    if (rsp->data[0] != 0) {
	info->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info,
		      info->cb_data);
	goto out;
    }

    if (rsp->data_len < 3) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(got_chan_info): Channel access response too small",
		 mc->name);
	info->handler(mc, EINVAL, info, info->cb_data);
	goto out;
    }

    /* For these, the values in the message are the inverse of their
       boolean value. */
    info->alert_val = !((rsp->data[1] >> 5) & 1);
    info->msg_auth_val = !((rsp->data[1] >> 4) & 1);
    info->user_auth_val = !((rsp->data[1] >> 3) & 1);

    info->access_mode_val = rsp->data[1] & 0x7;
    info->privilege_val = rsp->data[2] & 0xf;
    info->handler(mc, 0, info, info->cb_data);
 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_channel_get_access(ipmi_mc_t              *mc,
			   unsigned int           channel,
			   enum ipmi_set_dest_e   dest,
			   ipmi_channel_access_cb handler,
			   void                   *cb_data)
{
    int                   rv;
    ipmi_msg_t            msg;
    unsigned char         data[2];
    ipmi_channel_access_t *info;

    if (channel > 15)
	return EINVAL;
    if ((dest < IPMI_SET_DEST_NON_VOLATILE) || (dest > IPMI_SET_DEST_VOLATILE))
	return EINVAL;

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return ENOMEM;
    memset(info, 0, sizeof(*info));

    info->channel = channel;
    info->handler = handler;
    info->cb_data = cb_data;

    data[0] = channel & 0xf;
    data[1] = dest << 6;
    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_CHANNEL_ACCESS_CMD;
    msg.data = data;
    msg.data_len = 2;

    rv = ipmi_mc_send_command(mc, 0, &msg, got_chan_access, info);
    if (rv)
	ipmi_mem_free(info);
    return rv;
}

static void
set_chan_access(ipmi_mc_t  *mc,
		ipmi_msg_t *rsp,
		void       *cb_data)
{
    ipmi_channel_access_t *info = cb_data;

    if (rsp->data[0] != 0) {
	if (info->done)
	    info->done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), info->cb_data);
	goto out;
    }

    if (info->done)
	info->done(mc, 0, info->cb_data);

 out:
    ipmi_mem_free(info);
}

int
ipmi_mc_channel_set_access(ipmi_mc_t             *mc,
			   unsigned int           channel,
			   enum ipmi_set_dest_e  dest,
			   ipmi_channel_access_t *access,
			   ipmi_mc_done_cb       handler,
			   void                  *cb_data)
{
    ipmi_channel_access_t *info;
    ipmi_msg_t            msg;
    unsigned char         data[3];
    int                   rv;


    if (channel > 15)
	return EINVAL;
    if ((dest < IPMI_SET_DEST_NON_VOLATILE) || (dest > IPMI_SET_DEST_VOLATILE))
	return EINVAL;

    info = ipmi_mem_alloc(sizeof(*info));
    if (!info)
	return EINVAL;

    memcpy(info, access, sizeof(*info));
    info->channel = channel;
    info->done = handler;
    info->cb_data = cb_data;

    data[0] = channel & 0xf;

    data[1] = ((!info->alert_val << 5)
	       | (!info->msg_auth_val << 4)
	       | (!info->user_auth_val << 3)
	       | info->access_mode_val);
    if (info->alert_set || info->msg_auth_set || info->user_auth_set
	|| info->access_mode_set)
    {
	data[1] |= dest << 6;
    }

    data[2] = info->privilege_val;
    if (info->privilege_set)
	data[2] |= dest << 6;

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_CHANNEL_ACCESS_CMD;
    msg.data = data;
    msg.data_len = 3;

    rv = ipmi_mc_send_command(mc, 0, &msg, set_chan_access, info);
    if (rv)
	ipmi_mem_free(info);

    return rv;
}

ipmi_channel_access_t *
ipmi_channel_access_copy(ipmi_channel_access_t *access)
{
    ipmi_channel_access_t *rv = ipmi_mem_alloc(sizeof(*rv));
    if (!rv)
	return NULL;
    memcpy(rv, access, sizeof(*rv));
    return rv;
}

void
ipmi_channel_access_free(ipmi_channel_access_t *access)
{
    ipmi_mem_free(access);
}

int
ipmi_channel_access_get_channel(ipmi_channel_access_t *info,
				unsigned int          *channel)
{
    *channel = info->channel;
    return 0;
}

int
ipmi_channel_access_get_alerting_enabled(ipmi_channel_access_t *info,
					 unsigned int          *enab)
{
    *enab = info->alert_val;
    return 0;
}

int
ipmi_channel_access_set_alerting_enabled(ipmi_channel_access_t *info,
					 unsigned int          enab)
{
    info->alert_val = enab;
    info->alert_set = 1;
    return 0;
}

int
ipmi_channel_access_get_per_msg_auth(ipmi_channel_access_t *info,
				     unsigned int          *msg_auth)
{
    *msg_auth = info->msg_auth_val;
    return 0;
}

int
ipmi_channel_access_set_per_msg_auth(ipmi_channel_access_t *info,
				     unsigned int          msg_auth)
{
    info->msg_auth_val = msg_auth;
    info->msg_auth_set = 1;
    return 0;
}

int
ipmi_channel_access_get_user_auth(ipmi_channel_access_t *info,
				  unsigned int          *user_auth)
{
    *user_auth = info->user_auth_val;
    return 0;
}

int
ipmi_channel_access_set_user_auth(ipmi_channel_access_t *info,
				  unsigned int          user_auth)
{
    info->user_auth_val = user_auth;
    info->user_auth_set = 1;
    return 0;
}

int
ipmi_channel_access_get_access_mode(ipmi_channel_access_t *info,
				    unsigned int          *access_mode)
{
    *access_mode = info->access_mode_val;
    return 0;
}

int
ipmi_channel_access_set_access_mode(ipmi_channel_access_t *info,
				    unsigned int          access_mode)
{
    info->access_mode_val = access_mode;
    info->access_mode_set = 1;
    return 0;
}

int
ipmi_channel_access_get_priv_limit(ipmi_channel_access_t *info,
				   unsigned int          *priv_limit)
{
    *priv_limit = info->privilege_val;
    return 0;
}

int
ipmi_channel_access_set_priv_limit(ipmi_channel_access_t *info,
				   unsigned int          priv_limit)
{
    info->privilege_val = priv_limit;
    info->privilege_set = 1;
    return 0;
}

int
ipmi_channel_access_setall(ipmi_channel_access_t *info)
{
    info->alert_set = 1;
    info->msg_auth_set = 1;
    info->user_auth_set = 1;
    info->access_mode_set = 1;
    info->privilege_set = 1;
    return 0;
}

/***********************************************************************
 *
 * User management
 *
 **********************************************************************/

struct ipmi_user_s
{
    int  num;
    unsigned int link_enabled_set : 1;
    unsigned int link_enabled : 1;
    unsigned int msg_enabled_set : 1;
    unsigned int msg_enabled : 1;
    unsigned int privilege_limit_set : 1;
    unsigned int privilege_limit : 4;
    unsigned int cb_only_set : 1;
    unsigned int cb_only : 1;
    unsigned int session_limit_set : 1;
    unsigned int session_limit_read : 1;
    unsigned int session_limit : 4;
    unsigned int enable_set : 1;
    unsigned int enable_read : 1;
    unsigned int enable : 4;
    unsigned int name_set : 1;
    char name[17];
    unsigned int pw_set : 1;
    unsigned int pw2_set : 1;
    unsigned int can_use_pw2 : 1;
    char pw[20];

    unsigned int channel : 4;
    ipmi_mc_done_cb handler;
    void            *cb_data;
};

struct ipmi_user_list_s
{
    unsigned int      channel;
    unsigned int      curr;
    unsigned int      idx;
    unsigned int      max;
    unsigned int      enabled;
    unsigned int      fixed;
    ipmi_user_t       *users;
    int               supports_rmcpp;

    ipmi_user_list_cb handler;
    void              *cb_data;
};

ipmi_user_list_t *
ipmi_user_list_copy(ipmi_user_list_t *list)
{
    ipmi_user_list_t *rv;

    rv = ipmi_mem_alloc(sizeof(*rv));
    if (!rv)
	return NULL;
    memcpy(rv, list, sizeof(*rv));
    rv->users = ipmi_mem_alloc(sizeof(ipmi_user_t) * list->idx);
    if (!rv->users) {
	ipmi_mem_free(rv);
	return NULL;
    }
    memcpy(rv->users, list->users, sizeof(ipmi_user_t) * list->idx);
    return rv;
}

void
ipmi_user_list_free(ipmi_user_list_t *list)
{
    if (list->users)
	ipmi_mem_free(list->users);
    ipmi_mem_free(list);
}

unsigned int
ipmi_user_list_get_user_count(ipmi_user_list_t *list)
{
  return list->idx;
}

ipmi_user_t *
ipmi_user_list_get_user(ipmi_user_list_t *list,
			unsigned int     idx)
{
  if (idx >= list->idx)
      return NULL;
  return ipmi_user_copy(&list->users[idx]);
}

int
ipmi_user_list_get_channel(ipmi_user_list_t *list, unsigned int *channel)
{
    *channel = list->channel;
    return 0;
}

int
ipmi_user_list_get_max_user(ipmi_user_list_t *list, unsigned int *max)
{
    *max = list->max;
    return 0;
}

int
ipmi_user_list_get_enabled_users(ipmi_user_list_t *list, unsigned int *e)
{
    *e = list->enabled;
    return 0;
}

int
ipmi_user_list_get_fixed_users(ipmi_user_list_t *list, unsigned int *f)
{
    *f = list->fixed;
    return 0;
}


static int list_next_user(ipmi_mc_t *mc, ipmi_user_list_t *list);

static void
user_list_done(ipmi_mc_t *mc, ipmi_user_list_t *list)
{
    list->handler(mc, 0, list, list->cb_data);
    ipmi_user_list_free(list);
}

static void
got_user2(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_list_t *list = cb_data;
    int              rv;

    if (rsp->data[0] != 0) {
	list->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), list,
		      list->cb_data);
	goto out_err;
    }

    if (rsp->data_len < 17) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(got_chan_info): user name response too small",
		 mc->name);
	list->handler(mc, EINVAL, list, list->cb_data);
	goto out_err;
    }

    memcpy(list->users[list->idx].name, rsp->data+1, 16);
    list->users[list->idx].name[16] = '\0';
    list->idx++;

    if (list->curr >= list->max)
	user_list_done(mc, list);
    else {
	list->curr++;
	rv = list_next_user(mc, list);
	if (rv) {
	    list->handler(mc, rv, list, list->cb_data);
	    goto out_err;
	}
    }
    return;

 out_err:
    ipmi_user_list_free(list);
}

static void
got_user1(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_list_t *list = cb_data;
    int              rv;
    int              idx;
    ipmi_msg_t       msg;
    unsigned char    data[1];


    if (rsp->data[0] != 0) {
	list->handler(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), list,
		      list->cb_data);
	goto out_err;
    }

    if (rsp->data_len < 5) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%smc.c(got_chan_info): user access response too small",
		 mc->name);
	list->handler(mc, EINVAL, list, list->cb_data);
	goto out_err;
    }

    if (! list->users) {
	if (list->max == 0) {
	    list->max = rsp->data[1] & 0x3f;
	    list->enabled = rsp->data[2] & 0x3f;
	    list->fixed = rsp->data[3] & 0x3f;
	}
	if (list->max < 1) {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%smc.c(got_chan_info): user access num uses is < 1",
		     mc->name);
	    list->handler(mc, EINVAL, list, list->cb_data);
	    goto out_err;
	}
	list->users = ipmi_mem_alloc(sizeof(ipmi_user_t)
				     * (list->max - list->curr + 1));
	if (!list->users) {
	    list->handler(mc, EINVAL, list, list->cb_data);
	    goto out_err;
	}
	memset(list->users, 0,
	       sizeof(ipmi_user_t) * (list->max - list->curr + 1));
    }

    idx = list->idx;
    list->users[idx].num = list->curr;
    list->users[idx].cb_only = (rsp->data[4] >> 6) & 1;
    list->users[idx].link_enabled = (rsp->data[4] >> 5) & 1;
    list->users[idx].msg_enabled = (rsp->data[4] >> 4) & 1;
    list->users[idx].privilege_limit = rsp->data[4] & 0x0f;
    list->users[idx].channel = list->channel;
    list->users[idx].can_use_pw2 = list->supports_rmcpp;

    if (list->curr == 1) {
	/* User 1 does not have a name, don't try to fetch it. */
	memset(list->users[list->idx].name, 0, 17);
	list->idx++;
	if (list->curr >= list->max) {
	    user_list_done(mc, list);
	    rv = 0;
	} else {
	    list->curr++;
	    rv = list_next_user(mc, list);
	}
    } else {
	msg.netfn = IPMI_APP_NETFN;
	msg.cmd = IPMI_GET_USER_NAME_CMD;
	msg.data = data;
	msg.data_len = 1;
	data[0] = list->curr;

	rv = ipmi_mc_send_command(mc, 0, &msg, got_user2, list);
    }
    if (rv) {
	list->handler(mc, rv, list, list->cb_data);
	goto out_err;
    }
    
    return;

 out_err:
    ipmi_user_list_free(list);
}

static int
list_next_user(ipmi_mc_t *mc, ipmi_user_list_t *info)
{
    ipmi_msg_t      msg;
    unsigned char   data[2];

    if ((info->curr > 0x3f) || (info->curr < 1))
	return EINVAL;

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_USER_ACCESS_CMD;
    msg.data = data;
    msg.data_len = 2;
    data[0] = info->channel & 0xf;
    data[1] = info->curr;

    return ipmi_mc_send_command(mc, 0, &msg, got_user1, info);
}

static void
got_user0(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_list_t *list = cb_data;
    int              rv;

    if (rsp->data[0] != 0) {
	/* We possibly have 2.0 support. */
	 list->supports_rmcpp
	     = ((rsp->data[2] & (1 << 7)) /* Supports 2.0 capabilities */
		&& (rsp->data[4] & (1 << 1))); /* 2.0 connection support */
    }

    rv = list_next_user(mc, list);
    if (rv) {
	list->handler(mc, rv, list, list->cb_data);
	ipmi_mem_free(list);
    }
}

int
ipmi_mc_get_users(ipmi_mc_t         *mc,
		  unsigned int      channel,
		  unsigned int      user,
		  ipmi_user_list_cb handler,
		  void              *cb_data)
{
    int              rv;
    ipmi_user_list_t *list = NULL;
    ipmi_msg_t       msg;
    unsigned char    data[2];

    if (channel > 15)
	return EINVAL;
    if (user > 0x3f)
	return EINVAL;

    list = ipmi_mem_alloc(sizeof(*list));
    if (!list)
	return ENOMEM;
    memset(list, 0, sizeof(*list));

    list->channel = channel;
    list->handler = handler;
    list->cb_data = cb_data;
    if (user) {
	list->curr = user;
	list->max = user;
    } else {
	list->curr = 1;
	list->max = 0;
    }

    /* First determine if we have 2.0 (RMCP+) support. */
    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_GET_CHANNEL_AUTH_CAPABILITIES_CMD;
    msg.data = data;
    msg.data_len = 2;
    data[0] = (channel & 0xf) | (1 << 7); /* Request IPMI 2.0 data */
    data[1] = 2; /* Request user level access */

    rv = ipmi_mc_send_command(mc, 0, &msg, got_user0, list);
    if (rv)
	ipmi_mem_free(list);
    return rv;
}

ipmi_user_t *
ipmi_user_copy(ipmi_user_t *user)
{
    ipmi_user_t *rv;

    rv = ipmi_mem_alloc(sizeof(*rv));
    if (rv)
	memcpy(rv, user, sizeof(*rv));
    return rv;
}

void
ipmi_user_free(ipmi_user_t *user)
{
    ipmi_mem_free(user);
}

static void
set_user_done(ipmi_mc_t *mc, int err, ipmi_user_t *user)
{
    if (user->handler)
	user->handler(mc, err, user->cb_data);
    ipmi_user_free(user);
}

static void
set_user5(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_t     *user = cb_data;

    if (rsp->data[0] != 0) {
	set_user_done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), user);
	return;
    }

    set_user_done(mc, 0, user);
}

static int set_enable(ipmi_mc_t *mc, ipmi_user_t *user)
{
    ipmi_msg_t      msg;
    unsigned char   data[2];

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_USER_PASSWORD_CMD;
    msg.data = data;
    msg.data_len = 2;


    data[0] = user->num;
    if (user->enable)
	data[1] = 0x01; /* enable */
    else
	data[1] = 0x00; /* disable */
	
    return ipmi_mc_send_command(mc, 0, &msg, set_user5, user);
}

static void
set_user4(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_t *user = cb_data;
    int         rv = 0;

    if (rsp->data[0] != 0) {
	set_user_done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), user);
	return;
    }

    if (user->enable_set)
	rv = set_enable(mc, user);
    else
	set_user_done(mc, 0, user);

    if (rv)
	set_user_done(mc, rv, user);
}

static int set_pw(ipmi_mc_t *mc, ipmi_user_t *user)
{
    ipmi_msg_t      msg;
    unsigned char   data[22];

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_USER_PASSWORD_CMD;
    msg.data = data;


    data[0] = user->num;
    data[1] = 0x02; /* set password */
    if (user->pw2_set) {
	msg.data_len = 22;
	memcpy(data+2, user->pw, 20);
	data[0] |= 0x80;
    } else {
	msg.data_len = 18;
	memcpy(data+2, user->pw, 16);
    }
	
    return ipmi_mc_send_command(mc, 0, &msg, set_user4, user);
}

static void
set_user3(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_t     *user = cb_data;
    int             rv = 0;

    if (rsp->data[0] != 0) {
	set_user_done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), user);
	return;
    }

    if (user->pw_set || user->pw2_set)
	rv = set_pw(mc, user);
    else if (user->enable_set)
	rv = set_enable(mc, user);
    else
	set_user_done(mc, 0, user);

    if (rv)
	set_user_done(mc, rv, user);
}

static int set_name(ipmi_mc_t *mc, ipmi_user_t *user)
{
    ipmi_msg_t      msg;
    unsigned char   data[17];

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_USER_NAME_CMD;
    msg.data = data;
    msg.data_len = 17;


    data[0] = user->num;
    memcpy(data+1, user->name, 16);
	
    return ipmi_mc_send_command(mc, 0, &msg, set_user3, user);
}

static void
set_user2(ipmi_mc_t  *mc,
	  ipmi_msg_t *rsp,
	  void       *cb_data)
{
    ipmi_user_t     *user = cb_data;
    int             rv = 0;

    if (rsp->data[0] != 0) {
	set_user_done(mc, IPMI_IPMI_ERR_VAL(rsp->data[0]), user);
	return;
    }

    if (user->name_set)
	rv = set_name(mc, user);
    else if (user->pw_set || user->pw2_set)
	rv = set_pw(mc, user);
    else if (user->enable_set)
	rv = set_enable(mc, user);
    else
	set_user_done(mc, 0, user);

    if (rv)
	set_user_done(mc, rv, user);
}

static int
set_user_access(ipmi_mc_t *mc, ipmi_user_t *user)
{
    ipmi_msg_t      msg;
    unsigned char   data[4];

    msg.netfn = IPMI_APP_NETFN;
    msg.cmd = IPMI_SET_USER_ACCESS_CMD;
    msg.data = data;
    msg.data_len = 3;

    data[0] = user->channel;
    if (user->cb_only_set || user->link_enabled_set || user->msg_enabled_set) {
	data[0] |= user->channel;
	data[0] |= user->cb_only << 6;
	data[0] |= user->link_enabled << 5;
	data[0] |= user->msg_enabled << 4;
	data[0] |= 0x80;
    }
    data[1] = user->num;
    data[2] = user->privilege_limit;
    if (user->session_limit_set) {
	/* Optional value, afaict there is no way to get this value. */
	data[3] = user->session_limit;
	msg.data_len++;
    }
	
    return ipmi_mc_send_command(mc, 0, &msg, set_user2, user);
}

int
ipmi_mc_set_user(ipmi_mc_t       *mc,
		 unsigned int    channel,
		 unsigned int    num,
		 ipmi_user_t     *iuser,
		 ipmi_mc_done_cb handler,
		 void            *cb_data)
{
    int             rv = 0;
    ipmi_user_t     *user;

    if (channel > 15)
	return EINVAL;
    if (num > 0x3f)
	return EINVAL;

    user = ipmi_user_copy(iuser);
    if (!user)
	return ENOMEM;
    user->num = num;
    user->channel = channel;
    user->handler = handler;
    user->cb_data = cb_data;

    if (user->cb_only_set || user->link_enabled_set || user->msg_enabled_set
	|| user->privilege_limit_set || user->session_limit_set)
    	rv = set_user_access(mc, user);
    else if (user->name_set)
	rv = set_name(mc, user);
    else if (user->pw_set || user->pw2_set)
	rv = set_pw(mc, user);
    else if (user->enable_set)
	rv = set_enable(mc, user);
    else {
	/* Nothing to do. */
	if (handler)
	    handler(mc, 0, cb_data);
	ipmi_user_free(user);
    }

    if (rv)
	ipmi_user_free(user);

    return rv;
}

int
ipmi_user_get_channel(ipmi_user_t *user, unsigned int *channel)
{
    *channel = user->channel;
    return 0;
}

int
ipmi_user_get_num(ipmi_user_t *user, unsigned int *num)
{
    *num = user->num;
    return 0;
}

int
ipmi_user_set_num(ipmi_user_t *user, unsigned int num)
{
    if (num > 0x3f)
	return EINVAL;
    user->num = num;
    return 0;
}

int
ipmi_user_get_name_len(ipmi_user_t *user, unsigned int *len)
{
    *len = 16;
    return 0;
}

int
ipmi_user_get_name(ipmi_user_t *user, char *name, unsigned int *len)
{
    if (*len > 17)
	*len = 17;
    memcpy(name, user->name, *len);
    return 0;
}

int
ipmi_user_set_name(ipmi_user_t *user, char *name, unsigned int len)
{
    if (len > 16)
	return EINVAL;
    memcpy(user->name, name, len);
    user->name_set = 1;
    return 0;
}

int
ipmi_user_set_password(ipmi_user_t *user, char *pw, unsigned int len)
{
    if (len > 16)
	return EINVAL;
    memcpy(user->pw, pw, len);
    user->pw_set = 1;
    return 0;
}

int
ipmi_user_set_password2(ipmi_user_t *user, char *pw, unsigned int len)
{
    if (! user->can_use_pw2)
	return ENOSYS;
    if (len > 20)
	return EINVAL;
    memcpy(user->pw, pw, len);
    user->pw2_set = 1;
    return 0;
}

int
ipmi_user_get_link_auth_enabled(ipmi_user_t *user, unsigned int *val)
{
    *val = user->link_enabled;
    return 0;
}

int
ipmi_user_set_link_auth_enabled(ipmi_user_t *user, unsigned int val)
{
    user->link_enabled = val;
    user->link_enabled_set = 1;
    return 0;
}

int
ipmi_user_get_msg_auth_enabled(ipmi_user_t *user, unsigned int *val)
{
    *val = user->msg_enabled;
    return 0;
}

int
ipmi_user_set_msg_auth_enabled(ipmi_user_t *user, unsigned int val)
{
    user->msg_enabled = val;
    user->msg_enabled_set = 1;
    return 0;
}

int
ipmi_user_get_access_cb_only(ipmi_user_t *user, unsigned int *val)
{
    *val = user->cb_only;
    return 0;
}

int
ipmi_user_set_access_cb_only(ipmi_user_t *user, unsigned int val)
{
    user->cb_only = val;
    user->cb_only_set = 1;
    return 0;
}

int
ipmi_user_get_privilege_limit(ipmi_user_t *user, unsigned int *val)
{
    *val = user->privilege_limit;
    return 0;
}

int
ipmi_user_set_privilege_limit(ipmi_user_t *user, unsigned int val)
{
    user->privilege_limit = val;
    user->privilege_limit_set = 1;
    return 0;
}

int
ipmi_user_get_session_limit(ipmi_user_t *user, unsigned int *val)
{
    if (!user->session_limit_read)
	return ENOSYS;
    *val = user->session_limit;
    return 0;
}

int
ipmi_user_set_session_limit(ipmi_user_t *user, unsigned int val)
{
    user->session_limit = val;
    user->session_limit_set = 1;
    user->session_limit_read = 1;
    return 0;
}

int
ipmi_user_get_enable(ipmi_user_t *user, unsigned int *val)
{
    if (!user->enable_read)
	return ENOSYS;
    *val = user->enable;
    return 0;
}

int
ipmi_user_set_enable(ipmi_user_t *user, unsigned int val)
{
    user->enable = val;
    user->enable_set = 1;
    user->enable_read = 1;
    return 0;
}

int
ipmi_user_set_all(ipmi_user_t *user)
{
    user->cb_only_set = 1;
    user->link_enabled_set = 1;
    user->msg_enabled_set = 1;
    user->privilege_limit_set = 1;
    user->session_limit_set = user->session_limit_read;
    user->enable_set = user->enable_read;
    user->name_set = 1;
    return 0;
}