summaryrefslogtreecommitdiffstats
path: root/src/usr/ipmiext/ipmisensor.C
blob: ae6032353002365c248a70d7571abe020534adfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/ipmiext/ipmisensor.C $                                */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2019                        */
/* [+] Google Inc.                                                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/**
 * @file ipmisensor.C
 * @brief IPMI sensor manipulation
 */

#include <ipmi/ipmisensor.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <targeting/common/target.H>
#include <attributetraits.H>
#include <targeting/common/utilFilter.H>
#include <ipmi/ipmi_reasoncodes.H>
#include <endian.h>
#include <vpd/pvpdenums.H>
#include <devicefw/userif.H>
#include <hdat/hdat.H>


extern trace_desc_t * g_trac_ipmi;

namespace SENSOR
{

    //
    // Base class for sensor construction.  It is expected that this object will
    // be used as the base for any additional sensors defined.
    //
    SensorBase::SensorBase( TARGETING::SENSOR_NAME i_name,
                             const TARGETING::Target * i_target)
        :iv_name(i_name) ,iv_target(i_target)
    {
        // allocate a new message structure to use with our sensors
        // this will be the payload for the IPMI send/sendrecv sensor message.
        iv_msg = new setSensorReadingRequest;
    };

    SensorBase::~SensorBase()
    {
        // The memory allocated for the set sensor reading command is deleted
        // by the IPMI transport layer.  Since we are sending messages
        // asynchronously, the IPMI resource provider deletes the message
        // and there is nothing to delete here.
    };

    //
    // Helper function to process completion codes returned from the BMC.
    // If the completion code warrants a PEL the function will build and
    // return an error log with the correct data captured.
    //
    errlHndl_t SensorBase::processCompletionCode( IPMI::completion_code i_rc )
    {
        errlHndl_t l_err = NULL;

        IPMI::IPMIReasonCode l_reasonCode;

        if( i_rc != IPMI::CC_OK )
        {
            // bad rc from the BMC
            TRACFCOMP(g_trac_ipmi,"completion code 0x%x returned from the BMC"
                      " , creating error log", i_rc);

            switch(i_rc)
            {
                case  SENSOR::CC_SENSOR_READING_NOT_SETTABLE:
                {
                   /*@
                    * @errortype       ERRL_SEV_UNRECOVERABLE
                    * @moduleid        IPMI::MOD_IPMISENSOR
                    * @reasoncode      IPMI::RC_SENSOR_NOT_SETTABLE
                    * @userdata1       BMC IPMI Completion code.
                    * @userdata2       bytes [0-1]sensor name
                    *                  bytes [2-3]sensor number
                    *                  bytes [4-7]HUID of target.
                    * @devdesc         Set sensor reading command failed.
                    */
                    l_reasonCode = IPMI::RC_SENSOR_NOT_SETTABLE;
                    TRACFCOMP(g_trac_ipmi,"Attempt to change sensor reading or"
                              "set/clear status bits that are not settable"
                              " via this command");
                    break;
                }

                case  SENSOR::CC_EVENT_DATA_BYTES_NOT_SETTABLE:
                {
                   /*@
                    * @errortype       ERRL_SEV_UNRECOVERABLE
                    * @moduleid        IPMI::MOD_IPMISENSOR
                    * @reasoncode      IPMI::RC_EVENT_DATA_NOT_SETTABLE
                    * @userdata1       BMC IPMI Completion code.
                    * @userdata2       bytes[0-3]sensor number
                    *                  bytes[4-7]HUID of target.
                    * @devdesc         Set sensor reading command failed.
                    */
                    l_reasonCode = IPMI::RC_EVENT_DATA_NOT_SETTABLE;
                    TRACFCOMP(g_trac_ipmi,"Attempted to set event data bytes "
                            "but setting event data bytes is not supported for"
                            " this sensor");
                    break;
                }

                case IPMI::CC_CMDSENSOR:
                {
                   /*@
                    * @errortype       ERRL_SEV_UNRECOVERABLE
                    * @moduleid        IPMI::MOD_IPMISENSOR
                    * @reasoncode      IPMI::RC_INVALID_SENSOR_CMD
                    * @userdata1       BMC IPMI Completion code.
                    * @userdata2       bytes [0-1]sensor name
                    *                  bytes [2-3]sensor number
                    *                  bytes [4-7]HUID of target.
                    * @devdesc         Command not valid for this sensor.
                    */
                    l_reasonCode = IPMI::RC_INVALID_SENSOR_CMD;
                    TRACFCOMP(g_trac_ipmi,"Command not valid for this sensor");
                    break;
                }

                case IPMI::CC_BADSENSOR:
                {
                   /*@
                    * @errortype       ERRL_SEV_UNRECOVERABLE
                    * @moduleid        IPMI::MOD_IPMISENSOR
                    * @reasoncode      IPMI::RC_SENSOR_NOT_PRESENT
                    * @userdata1       BMC IPMI Completion code.
                    * @userdata2       bytes [0-1]sensor name
                    *                  bytes [2-3]sensor number
                    *                  bytes [4-7]HUID of target.
                    * @devdesc         Requested sensor is not present.
                    */
                    l_reasonCode = IPMI::RC_SENSOR_NOT_PRESENT;
                    TRACFCOMP(g_trac_ipmi,"Requested sensor not present");
                    break;
                }

                default:
                {
                    // lump everything else into a general failure for
                    // now.
                   /*@
                    * @errortype       ERRL_SEV_UNRECOVERABLE
                    * @moduleid        IPMI::MOD_IPMISENSOR
                    * @reasoncode      IPMI::RC_SET_SENSOR_FAILURE
                    * @userdata1       BMC IPMI Completion code.
                    * @userdata2       bytes [0-1]sensor name
                    *                  bytes [2-3]sensor number
                    *                  bytes [4-7]HUID of target.
                    * @devdesc         Set sensor reading command failed.
                    */
                    TRACFCOMP(g_trac_ipmi,"Set sensor reading command failed");
                    l_reasonCode = IPMI::RC_SET_SENSOR_FAILURE;
                    break;
                }
            }

                // shift the sensor number into to bytes 0-3 and then
                // or in the HUID to bytes 4-7
                uint32_t sensor_number = getSensorNumber();
                uint32_t huid = TARGETING::get_huid( iv_target );

                TRACFCOMP(g_trac_ipmi,"Sensor Number: 0x%X, HUID: 0x%X", sensor_number, huid );

                l_err = new ERRORLOG::ErrlEntry(
                                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                IPMI::MOD_IPMISENSOR,
                                l_reasonCode,
                                i_rc,
                                TWO_UINT32_TO_UINT64(
                                       TWO_UINT16_TO_UINT32(iv_name,
                                                            sensor_number),
                                       huid ),
                                true);

                l_err->collectTrace(IPMI_COMP_NAME);

        }
        return l_err;
    }

    //
    // Helper function to send the data to the BMC using the correct interface
    // protocol
    //
    errlHndl_t SensorBase::writeSensorData()
    {

        errlHndl_t l_err = NULL;

        iv_msg->iv_sensor_number = static_cast<uint8_t>(getSensorNumber());

        if( iv_msg->iv_sensor_number != TARGETING::UTIL::INVALID_IPMI_SENSOR )
        {

            // iv_msg is deleted by the IPMI resource provider.
            l_err = sendSetSensorReading( iv_msg);

            if( l_err )
            {
                TRACFCOMP(g_trac_ipmi,"error returned from "
                        "sendSetSensorReading() for sensor number 0x%x",
                        getSensorNumber());
            }
        }
        else
        {
            TRACFCOMP(g_trac_ipmi,"We were not able to find a sensor number in"
                      " the IPMI_SENSORS attribute for sensor_name=0x%x "
                      "for target with huid=0x%x, skipping call to "
                      "sendSetSensorReading()",
                    iv_name, TARGETING::get_huid( iv_target ));

            /*@
             * @errortype       ERRL_SEV_UNRECOVERABLE
             * @moduleid        IPMI::MOD_IPMISENSOR
             * @reasoncode      IPMI::RC_SENSOR_NOT_FOUND
             * @userdata1       Returned sensor number.
             * @userdata2       bytes [0-3]sensor name
             *                  bytes [4-7]HUID of target.
             * @devdesc         Requested sensor attribute not found.
             */
            l_err = new ERRORLOG::ErrlEntry(
                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                IPMI::MOD_IPMISENSOR,
                IPMI::RC_SENSOR_NOT_FOUND,
                iv_msg->iv_sensor_number,
                TWO_UINT32_TO_UINT64( iv_name,
                                      TARGETING::get_huid( iv_target ) ),
                true);

            delete iv_msg;
        }

        return l_err;
    };

    //
    // Helper function to set the bit in the assertion/deassertion mask
    // associated with the desired sensor specific offset
    //
    uint16_t SensorBase::setMask( const uint8_t offset, bool swap )
    {
        const uint16_t mask = (0x0001 << offset);

        if(swap)
        {
            // need to byte swap the mask (see set sensor reading in spec)
            return le16toh(mask);
        }
        else
        {
            return mask;
        }

    };

    //
    // Helper function to translate the assertion/deassertion mask into
    // the correct event offset.
    //
    uint8_t SensorBase::getOffset( uint16_t mask )
    {
        // $TODO RTC:117872
        return 0;
    };

    // read data from the sensor.
    errlHndl_t SensorBase::readSensorData( getSensorReadingData& o_data)
    {

        // get sensor reading command only requires one byte of extra data,
        // which will be the sensor number, the command will return between
        // 3 and 5 bytes of data.
        size_t len =  1;

        // need to allocate some memory to hold the sensor number this will be
        // deleted by the IPMI transport layer
        uint8_t * l_data = new uint8_t[len];

        l_data[0] = static_cast<uint8_t>(getSensorNumber());

        IPMI::completion_code cc = IPMI::CC_UNKBAD;

        // o_data will hold the response when this returns
        errlHndl_t l_err = sendrecv(IPMI::get_sensor_reading(), cc, len,
                                  l_data);

        // if we didn't get an error back from the BT interface, but see a
        // bad completion code from the BMC, process the CC to see if we
        // need to create a PEL - if an error occurs sendrcv will clean up
        // l_data for us
        if(  l_err == NULL )
        {
            l_err = processCompletionCode( cc );

            if( l_err == NULL )
            {
                // populate the output structure with the sensor data
                o_data.completion_code = cc;

                o_data.sensor_status = l_data[0];

                o_data.sensor_reading = l_data[1];

                // bytes 3-5 of the reading are optional and will be dependent
                // on the value of the sensor status byte.
                if( !( o_data.sensor_status &
                     ( SENSOR::SENSOR_DISABLED |
                       SENSOR::SENSOR_SCANNING_DISABLED )) ||
                     ( o_data.sensor_status & SENSOR::READING_UNAVAILABLE ))
                {
                    // sensor reading is available
                    o_data.event_status =
                                (( (uint16_t) l_data[3]) << 8  | l_data[2] );

                    // spec indicates that the high order bit should be
                    // ignored on a read, so lets mask it off now.
                    o_data.event_status &= 0x7FFF;
                }
                else
                {
                    uint32_t l_sensorNumber = getSensorNumber();

                    TRACFCOMP(g_trac_ipmi,"Sensor reading not available: status = 0x%x",o_data.sensor_status);
                    TRACFCOMP(g_trac_ipmi,"sensor number 0x%x, huid 0x%x",l_sensorNumber ,get_huid(iv_target));

                    // something happened log an error to indicate the request
                    // failed
                    /*@
                     * @errortype           ERRL_SEV_UNRECOVERABLE
                     * @moduleid            IPMI::MOD_IPMISENSOR
                     * @reasoncode          IPMI::RC_SENSOR_READING_NOT_AVAIL
                     * @userdata1           sensor status indicating reason for
                     *                      reading not available
                     * @userdata2[0:31]     sensor number
                     * @userdata2[32:64]    HUID of target
                     *
                     * @devdesc             Set sensor reading command failed.
                     * @custdesc            Request to get sensor reading
                     *                      IPMI completion code can be seen
                     *                      in userdata1 field of the log.
                     */
                    l_err = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            IPMI::MOD_IPMISENSOR,
                            IPMI::RC_SENSOR_READING_NOT_AVAIL,
                            o_data.sensor_status,
                            TWO_UINT32_TO_UINT64( l_sensorNumber,
                                TARGETING::get_huid(iv_target)), true);

                    l_err->collectTrace(IPMI_COMP_NAME);

                }

            }

            delete[] l_data;
        }
        return l_err;
    };

    //
    //  Asynchronously send a set sensor reading command to the BMC.
    //
    errlHndl_t SensorBase::sendSetSensorReading(
                            setSensorReadingRequest * i_data)
    {

        size_t l_len = sizeof( setSensorReadingRequest );

        // i_data will hold the response when this returns
        errlHndl_t l_err = send(IPMI::set_sensor_reading(),
                            l_len, (uint8_t *)i_data);

        return l_err;
    }

    // return the sensor type and event reading data
    errlHndl_t SensorBase::getSensorType(uint32_t i_sensorNumber,
                                         uint8_t &o_sensorType,
                                         uint8_t &o_eventReadingType )
    {

        size_t len =  1;

        o_sensorType = INVALID_TYPE;
        o_eventReadingType = INVALID_TYPE;

        // need to allocate some memory to hold the sensor number this will be
        // deleted by the IPMI transport layer
        uint8_t *l_data = new uint8_t[len];

        l_data[0] = i_sensorNumber;

        IPMI::completion_code cc = IPMI::CC_UNKBAD;

        // l_data will hold the response when this returns
        errlHndl_t l_err = sendrecv(IPMI::get_sensor_type(), cc, len,
                                  l_data);

        // if we didn't get an error back from the BT interface,
        // process the CC to see if we need to create a PEL
        if( l_err == NULL )
        {
            // check the completion code
            if( cc!= IPMI::CC_OK )
            {
                TRACFCOMP(g_trac_ipmi,"bad completion code from BMC=0x%x",cc);

                /*@
                 * @errortype       ERRL_SEV_INFORMATIONAL
                 * @moduleid        IPMI::MOD_IPMISENSOR
                 * @reasoncode      IPMI::RC_GET_SENSOR_TYPE_CMD_FAILED
                 * @userdata1       BMC IPMI Completion code.
                 * @devdesc         Request to get sensor type form the bmc
                 *                  failed.
                 */
                l_err = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_INFORMATIONAL,
                            IPMI::MOD_IPMISENSOR,
                            IPMI::RC_GET_SENSOR_TYPE_CMD_FAILED,
                            static_cast<uint64_t>(cc),0, true);

                l_err->collectTrace(IPMI_COMP_NAME);


            }
            else
            {
                    // grab the type and reading code to pass back to the caller
                    o_sensorType = l_data[0];

                    // high order bit is reserved
                    o_eventReadingType = ( 0x7f & l_data[1]);

            }
            delete[] l_data;
        }
        return l_err;
    };


    /**
     *  @brief Returns major type of input sensor name
     *
     *  @param[in] i_sensorName
     *      Name of the sensor
     *
     *  @return Major type of input sensor name
     */
    static inline uint16_t getMajorType(
        const uint16_t i_sensorName)
    {
        return (i_sensorName & SENSOR_NAME_MAJOR_MASK);
    }

    /**
     *  @brief Returns minor type of input sensor name
     *
     *  @param[in] i_sensorName
     *      Name of the sensor
     *
     *  @return Minor type of input sensor name
     */
    static inline uint16_t getMinorType(
        const uint16_t i_sensorName)
    {
        return (i_sensorName & SENSOR_NAME_MINOR_MASK);
    }

    /**
     *  @brief Returns whether the supplied sensor record's major type is less
     *      than the major type of the supplied sensor name
     *
     *  @param[in] i_sensorRecord
     *      Sensor record to compare
     *
     *  @param[in] i_sensorName
     *      Name of the sensor to compare
     *
     *  @retval true  Major type of sensor record is less than major type of
     *      sensor name
     *  @retval false Major type of sensor record is not less than major type of
     *      sensor name
     */
    static inline bool compare_major(
        const uint16_t (&i_sensorRecord)[2],
        const uint16_t i_sensorName)
    {
        return getMajorType(i_sensorRecord[0]) < getMajorType(i_sensorName);
    }

    /**
     *  @brief Returns whether the supplied sensor record's major type equals
     *      the major type of the supplied sensor name
     *
     *  @param[in] i_sensorRecord
     *      Sensor record to compare
     *
     *  @param[in] i_sensorName
     *      Name of the sensor to compare
     *
     *  @retval true  Major type of sensor record equals major type of
     *      sensor name
     *  @retval false Major type of sensor record does not equal major type of
     *      sensor name
     */
    static inline bool equals_major(
        const uint16_t (&i_sensorRecord)[2],
        const uint16_t i_sensorName)
    {
        return getMajorType(i_sensorRecord[0]) == getMajorType(i_sensorName);
    }

    ///
    // FirmwareProgressSensor constructor - uses system target
    //
    FirmwareProgressSensor::FirmwareProgressSensor( )
        :SensorBase(TARGETING::SENSOR_NAME_FW_BOOT_PROGRESS, NULL)
    {
        // message buffer created and initialized in base object.

        // assert the system firmware progress offset.
        iv_msg->iv_assertion_mask = setMask( SYSTEM_FIRMWARE_PROGRESS );
    };

    //
    // FirmwareProgressSensor destructor
    //
    FirmwareProgressSensor::~FirmwareProgressSensor( )
    {

    };

    //
    // setBootProgressPhase - update the boot progress sensor of the BMC
    //
    errlHndl_t FirmwareProgressSensor::setBootProgressPhase(
            INITSERVICE::firmwareProgressPhase phase )
    {
        // event data 2 holds the progress info
        iv_msg->iv_event_data[1] = phase;

        return writeSensorData();
    };

    //
    // RebootCountSensor constructor - uses system target
    //
    RebootCountSensor::RebootCountSensor()
        :SensorBase(TARGETING::SENSOR_NAME_REBOOT_COUNT, NULL)
    {
        // message buffer created and initialized in base object.

    }

    //
    // RebootCountSensor destructor
    //
    RebootCountSensor::~RebootCountSensor(){};

    //
    // setRebootCount - send a new value for the reboot count to the BMC.
    //
    errlHndl_t RebootCountSensor::setRebootCount( uint16_t i_count )
    {
        // adjust the operation to overwrite the sensor reading
        // to the value we send.
        iv_msg->iv_operation = SET_SENSOR_VALUE_OPERATION;

        // the Reboot_count sensor is defined as a discrete sensor
        // but the assertion bytes are being used to transfer the count
        // to the bmc, will need to byte swap the data
        iv_msg->iv_assertion_mask = le16toh(i_count);

        return writeSensorData();

    }

    //
    // getRebootCount - get the reboot count from the BMC
    //
    errlHndl_t RebootCountSensor::getRebootCount( uint16_t &o_rebootCount )
    {

        // the Reboot_count sensor is defined as a discrete sensor
        // but the assertion bytes are being used to transfer the count
        // from the BMC
        getSensorReadingData l_data;

        errlHndl_t l_err = readSensorData( l_data );

        if( l_err == NULL )
        {
            // this value is already byteswapped
            o_rebootCount = l_data.event_status;
        }
        return l_err;

    }

    //
    // RebootControlSensor constructor - uses system target
    //
    RebootControlSensor::RebootControlSensor()
        :SensorBase(TARGETING::SENSOR_NAME_HOST_AUTO_REBOOT_CONTROL, NULL)
    {
        // message buffer created and initialized in base object.

    }

    //
    // RebootCountSensor destructor
    //
    RebootControlSensor::~RebootControlSensor(){};

    //
    // setRebootControl - turn reboots on or off to the BMC
    //
    errlHndl_t RebootControlSensor::setRebootControl(
                                                autoRebootSetting i_setting )
    {
        // adjust the operation to overwrite the sensor reading
        // to the value we send.
        iv_msg->iv_operation = SET_SENSOR_VALUE_OPERATION;

        // the Reboot Control Sensor is defined as a discrete sensor
        // but the assertion bytes are being used to transfer the state
        iv_msg->iv_assertion_mask = le16toh(i_setting);

        TRACFCOMP(g_trac_ipmi,"RebootControlSensor::setRebootControl(%d)",
                    i_setting);

        return writeSensorData();
    }

    //
    // getRebootCount - get the reboot setting from the BMC
    //
    errlHndl_t RebootControlSensor::getRebootControl(
                                                autoRebootSetting &o_setting )
    {
        // the Reboot control sensor is defined as a discrete sensor
        // DISABLE_REBOOT - keep current state (no reboot)
        // ENABLE_REBOOT  - Allow analysis of FIRDATA on XSTOP
        getSensorReadingData l_data;

        errlHndl_t l_err = readSensorData( l_data );

        if( l_err == NULL )
        {
            // Check that this value is a valid enum value
            if ( l_data.event_status == ENABLE_REBOOTS ||
                 l_data.event_status == DISABLE_REBOOTS )
            {
                o_setting = static_cast<autoRebootSetting>(l_data.event_status);
            }
            else
            {
                TRACFCOMP(g_trac_ipmi,"Unknown reboot control setting: %d",
                    l_data.event_status);

                /*@
                 * @errortype    ERRL_SEV_UNRECOVERABLE
                 * @moduleid     IPMI::MOD_IPMISENSOR_REBOOTCNTRL
                 * @reasoncode   IPMI::RC_INVALID_SENSOR_SETTING
                 * @userdata1    Invalid reboot control setting
                 * @userdata2    <unused>
                 * @devdesc      The sensor returned an invalid setting
                 * @custdesc     Unable to find a valid sensor setting.
                 */
                l_err = new ERRORLOG::ErrlEntry(
                                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            IPMI::MOD_IPMISENSOR_REBOOTCNTRL,
                                            IPMI::RC_INVALID_SENSOR_SETTING,
                                            l_data.event_status,
                                            0,
                                            false);
            }
        }
        return l_err;
    }


    //
    // StatusSensor constructor - uses system DIMM/CORE/PROC target
    //
    StatusSensor::StatusSensor( TARGETING::ConstTargetHandle_t i_target )
        :SensorBase(TARGETING::SENSOR_NAME_STATE, i_target)
    {
        iv_functionalOffset  = PROC_DISABLED;
        iv_presentOffset     = PROC_PRESENCE_DETECTED;

        switch ( i_target->getAttr<TARGETING::ATTR_TYPE>() )
        {
            case TARGETING::TYPE_DIMM:
                {
                    iv_functionalOffset  = MEMORY_DEVICE_DISABLED;
                    iv_presentOffset     = MEM_DEVICE_PRESENCE_DETECTED;
                    iv_name = TARGETING::SENSOR_NAME_DIMM_STATE;
                    break;
                }
            case TARGETING::TYPE_MEMBUF:
                {
                    iv_functionalOffset  = MEMORY_DEVICE_DISABLED;
                    iv_presentOffset     = MEM_DEVICE_PRESENCE_DETECTED;
                    iv_name = TARGETING::SENSOR_NAME_MEMBUF_STATE;
                    break;
                }
            case TARGETING::TYPE_PROC:
                iv_name = TARGETING::SENSOR_NAME_PROC_STATE;
                break;

            case TARGETING::TYPE_CORE:
                iv_name = TARGETING::SENSOR_NAME_CORE_STATE;
                break;

            default:
                TRACFCOMP(g_trac_ipmi,"INF>>No status sensor associated with target type 0x%x",
                         i_target->getAttr<TARGETING::ATTR_TYPE>());
                iv_functionalOffset = INVALID_OFFSET;
                iv_presentOffset    = INVALID_OFFSET;
                break;
        }

    };

    //
    // StatusSensor destructor
    //
    //
    StatusSensor::~StatusSensor()
    {};


    // Convert the input status to the correct sensor offset value, then
    // send the message to the BMC to update the event status for this sensor.
    errlHndl_t StatusSensor::setStatus( statusEnum i_state )
    {

        errlHndl_t l_err = NULL;

        if( iv_functionalOffset != INVALID_OFFSET
                && iv_presentOffset != INVALID_OFFSET )
        {
            uint16_t func_mask = setMask( iv_functionalOffset );
            uint16_t pres_mask = setMask( iv_presentOffset );

            switch ( i_state )
            {
                case NOT_PRESENT:
                    // turn off the present bit
                    iv_msg->iv_deassertion_mask = pres_mask;

                    // turn off the disabled bit in case it was on
                    iv_msg->iv_deassertion_mask |= func_mask;
                    break;

                case PRESENT:
                    // turn on the present bit
                    iv_msg->iv_assertion_mask = pres_mask;
                    break;

                case FUNCTIONAL:
                    // turn off the disabled bit
                    iv_msg->iv_deassertion_mask = func_mask;
                    break;

                case PRESENT_FUNCTIONAL:
                    // assert the present bit
                    iv_msg->iv_assertion_mask = pres_mask;
                    // turn off the disabled bit
                    iv_msg->iv_deassertion_mask = func_mask;
                    break;

                case PRESENT_NONFUNCTIONAL:
                    // assert the present bit
                    iv_msg->iv_assertion_mask = pres_mask;
                    // assert the disabled bit
                    iv_msg->iv_assertion_mask |= func_mask;
                    break;

                case NON_FUNCTIONAL:
                    // assert the disabled bit
                    iv_msg->iv_assertion_mask = func_mask;
                    break;

                default:
                    // mark as not present
                    iv_msg->iv_deassertion_mask = pres_mask;
                    iv_msg->iv_assertion_mask = func_mask;
                    break;
            }

            l_err = writeSensorData();
        }
        return l_err;

    };

    //**************************************************************************
    // GpuSensor constructor
    //**************************************************************************
    GpuSensor::GpuSensor(TARGETING::SENSOR_NAME i_name, uint16_t i_num,
                     TARGETING::ConstTargetHandle_t i_target)
             : StatusSensor(i_target)

    {
        /* Note: StatusSensor sets these for processor target */
        //iv_functionalOffset  = PROC_DISABLED;
        //iv_presentOffset     = PROC_PRESENCE_DETECTED;

        // Override iv_name set by parent constructor
        iv_name = i_name;

        // 3 numbers possible (1 per GPU) for each name, so save which one
        iv_sensorNumber = i_num;
    };

    //**************************************************************************
    // GpuSensor destructor
    //**************************************************************************
    GpuSensor::~GpuSensor()
    {
    }

    //**************************************************************************
    // FaultSensor constructor
    //**************************************************************************

    FaultSensor::FaultSensor(
            TARGETING::ConstTargetHandle_t i_pTarget)
        : SensorBase(TARGETING::SENSOR_NAME_FAULT, i_pTarget)
    {
    }

    //**************************************************************************
    // FaultSensor constructor for associated targets
    //**************************************************************************

    FaultSensor::FaultSensor(
            TARGETING::ConstTargetHandle_t i_pTarget,
            const TARGETING::ENTITY_ID i_associatedType)
        : SensorBase(
                static_cast<TARGETING::SENSOR_NAME>(
                    TARGETING::SENSOR_NAME_FAULT | i_associatedType),
                i_pTarget)
    {
    }

    //**************************************************************************
    // FaultSensor destructor
    //**************************************************************************

    FaultSensor::~FaultSensor()
    {
    }

    //**************************************************************************
    // FaultSensor::setStatus
    //**************************************************************************

    errlHndl_t FaultSensor::setStatus(
            const FAULT_STATE i_faultState)
    {
        errlHndl_t pError = NULL;

        switch(i_faultState)
        {
            case FAULT_STATE_ASSERTED:
                iv_msg->iv_assertion_mask = setMask(FAULT_ASSERTED_OFFSET);
                break;
            case FAULT_STATE_DEASSERTED:
                iv_msg->iv_deassertion_mask = setMask(FAULT_ASSERTED_OFFSET);
                break;
            default:
                assert(0,"Caller passed unsupported fault state of 0x%X",
                        i_faultState);
        }

        pError = writeSensorData();
        if(pError)
        {
            TRACFCOMP(g_trac_ipmi, ERR_MRK " "
                    "Failed to write sensor data for sensor name 0x%X",
                    iv_name);
        }

        return pError;
    }

    //
    // OCC Active Sensor - uses occ target
    //
    //
    OCCActiveSensor::OCCActiveSensor( TARGETING::Target * i_pTarget )
        :SensorBase(TARGETING::SENSOR_NAME_OCC_ACTIVE,
                (TARGETING::ConstTargetHandle_t) i_pTarget )
    {
    };

    //
    // OCCActiveSensor destructor
    //
    //
    OCCActiveSensor::~OCCActiveSensor(){};

    // Convert the input status to the correct sensor offset value, then
    // send the message to the BMC to update the event status for this sensor.
    errlHndl_t OCCActiveSensor::setState( OccStateEnum i_state )
    {
        errlHndl_t l_err = NULL;

        // assert the specified state
        iv_msg->iv_assertion_mask = setMask(i_state);

        // there are two offsets used with this sensor, when
        // asserting one, we need to deassert the other as only
        // one state is valid at any given time.
        OccStateEnum other_state =
            (i_state == OCC_ACTIVE) ? OCC_NOT_ACTIVE : OCC_ACTIVE;

        iv_msg->iv_deassertion_mask = setMask( other_state );

        l_err = writeSensorData();

        return l_err;

    };

    // send the message to the BMC to read the event status for this sensor,
    // will return true if the "disabled" state of the sensor is not
    // asserted
    bool OCCActiveSensor::isActive( )
    {
        getSensorReadingData l_data;

        bool is_active = false;

        // set the mask, but dont swap the bytes since we are using it locally
        // not passing it in the set sensor cmd
        uint16_t mask = setMask( OCC_ACTIVE, false );

        errlHndl_t l_err = readSensorData( l_data );

        if( l_err == NULL )
        {
            // check if "disabled" offset has been asserted -
            // this would indicate that the OCC was not yet enabled
            if( l_data.event_status & mask )
            {
                is_active = true;
            }
        }
        else
        {
            // commit the error and return "not active" by default
            errlCommit( l_err, IPMI_COMP_ID );
        }

        return is_active;
    }


    //
    // HostStausSensor constructor - uses system target
    //
    //
    HostStatusSensor::HostStatusSensor()
        :SensorBase(TARGETING::SENSOR_NAME_HOST_STATUS, NULL)
    {

    };

    //
    // HostStatusSensor destructor
    //
    //
    HostStatusSensor::~HostStatusSensor(){};

    //
    // updateHostStaus - update the BMC HostStatus sensor with the passed in
    //                   value.
    //
    //
    errlHndl_t HostStatusSensor::updateHostStatus( hostStatus status )
    {
        iv_msg->iv_operation = SET_SENSOR_VALUE_OPERATION;
        iv_msg->iv_assertion_mask = setMask((uint8_t)status);

        return writeSensorData();
    };

    //
    //  Used to update the sensor status for a specific set of target types
    //  currently supported types are TYPE_DIMM, TYPE_MEMBUF, TYPE_CORE,
    //  TYPE_PROC.  These are virtual sensors where Hostboot updates the
    //  present and functional states and the BMC maintains the sensor.
    //
    void updateBMCSensorStatus(TARGETING::TYPE i_type)
    {

        TARGETING::TargetHandleList l_tList;

        // get all targets of the passed in type, functional or not
        switch( i_type )
        {

            case TARGETING::TYPE_DIMM:
                getAllLogicalCards( l_tList, TARGETING::TYPE_DIMM, false );
                break;

            case TARGETING::TYPE_MEMBUF:
                getAllChips( l_tList, TARGETING::TYPE_MEMBUF, false );
                break;

            case TARGETING::TYPE_PROC:
                getAllChips( l_tList, TARGETING::TYPE_PROC, false );
                break;

            case TARGETING::TYPE_CORE:
                getAllChiplets( l_tList, TARGETING::TYPE_CORE, false);
                break;

            default:
                assert(0, "invalid target type for BMC update");

        }

        // have a list of targets now set the status sensor on the BMC for each
        // one.
        for(TARGETING::TargetHandleList::const_iterator pTargetIt =
            l_tList.begin();
            pTargetIt != l_tList.end();
            ++pTargetIt )
        {

            StatusSensor::statusEnum l_status
                                    = StatusSensor::PRESENT_FUNCTIONAL;

            // create a status sensor for our needs
            StatusSensor l_sensor((*pTargetIt));

            TARGETING::HwasState l_state =
                    (*pTargetIt)->getAttr<TARGETING::ATTR_HWAS_STATE>();

            if( l_state.present == true )
            {
                if( l_state.functional == false )
                {
                    l_status = StatusSensor::PRESENT_NONFUNCTIONAL;
                }
            }
            else
            {
                l_status = StatusSensor::NOT_PRESENT;
            }

            // send the status to the BMC
            errlHndl_t l_err = l_sensor.setStatus( l_status );

            // commit the error and move to the next target
            if( l_err )
            {
               errlCommit( l_err, IPMI_COMP_ID );
            }
       }

     }

    void updateBMCFaultSensorStatus(void)
    {
        TARGETING::ATTR_IPMI_SENSORS_type noSensors = {{0}};

        // No sensor attribute is all 0's; therefore if a sensor attribute is
        // found and is not all zeros (using the predicate value inversion
        // feature) then the sensor attribute has potential
        // sensors to iterate through
        TARGETING::PredicateAttrVal<TARGETING::ATTR_IPMI_SENSORS>
            hasSensors(noSensors,true);
        TARGETING::TargetRangeFilter targetsWithSensorsItr(
            TARGETING::targetService().begin(),
            TARGETING::targetService().end(),
            &hasSensors);
        for (; targetsWithSensorsItr; ++targetsWithSensorsItr)
        {
            // Cache the target for ease of reading/usage
            TARGETING::TargetHandle_t pTarget = *targetsWithSensorsItr;

            TARGETING::ATTR_IPMI_SENSORS_type sensors = {{0}};
            assert(pTarget->tryGetAttr<TARGETING::ATTR_IPMI_SENSORS>(sensors));

            // Derive number of sensor records by dividing attribute size by
            // size of each sensor record
            uint16_t sensorRows = (sizeof(sensors)/sizeof(sensors[0]));

            // Ceate an iterator pointing to the first element of the array
            uint16_t (*begin)[2] = &sensors[0];

            // Using the number entries as the index into the array will set the
            // end iterator to the correct position (one entry past the last
            // element of the array)
            uint16_t (*end)[2] = &sensors[sensorRows];

            // Locate the first record that could possibly match the criteria
            uint16_t (*ptr)[2] =
                std::lower_bound(begin, end,
                    TARGETING::SENSOR_NAME_FAULT, &compare_major);

            // Process any match, and all remaining matches after that, until
            // there is no additional match.  Here we are matching the major
            // sensor type only
            while(   (ptr != end)
                  && (   getMajorType((*ptr)[0])
                      == TARGETING::SENSOR_NAME_FAULT ))
            {
                TRACFCOMP(g_trac_ipmi, INFO_MRK " "
                    "Found fault sensor name 0x%X and ID 0x%X for HUID 0x%X",
                    (*ptr)[0], (*ptr)[1], TARGETING::get_huid(pTarget));

                FaultSensor faultSensor(pTarget,
                    static_cast<TARGETING::ENTITY_ID>(
                        getMinorType((*ptr)[0])));

                errlHndl_t pError = faultSensor.setStatus(
                    FaultSensor::FAULT_STATE_DEASSERTED);
                if(pError)
                {
                    TRACFCOMP(g_trac_ipmi, ERR_MRK " "
                        "Failed setting fault sensor name 0x%X and ID 0x%X for "
                        "HUID 0x%X",
                        (*ptr)[0], (*ptr)[1], TARGETING::get_huid(pTarget));
                    errlCommit(pError, IPMI_COMP_ID);
                }

                ++ptr;
            }
        }
    }

    void updateBMCSensorStatus()
    {

        // send status of all MEMBUF targets
        updateBMCSensorStatus(TARGETING::TYPE_MEMBUF);

        // send status of all DIMM targets
        updateBMCSensorStatus(TARGETING::TYPE_DIMM);

        // send status for all PROC targets
        updateBMCSensorStatus(TARGETING::TYPE_PROC);

        updateBMCSensorStatus(TARGETING::TYPE_CORE);

        // Send status for all simple fault sensors in the system
        updateBMCFaultSensorStatus();
    };

    // returns a sensor number for the FRU based on input target type
    // there are currently 4 frus defined system, backplane, DIMM, PROC
    //
    uint32_t getFaultSensorNumber( TARGETING::ConstTargetHandle_t i_pTarget )
    {
        TRACDCOMP(g_trac_ipmi,">>getFaultSensorNumber()");

        TARGETING::TYPE l_type = i_pTarget->getAttr<TARGETING::ATTR_TYPE>();

        uint32_t l_sensor_number = TARGETING::UTIL::INVALID_IPMI_SENSOR;

        switch( l_type )
        {

            case TARGETING::TYPE_SYS:
                {
                    TRACDCOMP(g_trac_ipmi, "returning the \"System Event\" sensor\n");
                    l_sensor_number = TARGETING::UTIL::getSensorNumber(
                            i_pTarget,
                            TARGETING::SENSOR_NAME_SYSTEM_EVENT );

                    TRACDCOMP(g_trac_ipmi,"Sensor Number = 0x%x", l_sensor_number);
                    break;
                }

            case TARGETING::TYPE_NODE:
                {
                    TRACDCOMP(g_trac_ipmi, "returning the \"BACKPLANE_FAULT\" sensor\n");
                    l_sensor_number = TARGETING::UTIL::getSensorNumber(
                            i_pTarget,
                            TARGETING::SENSOR_NAME_BACKPLANE_FAULT );

                    TRACDCOMP(g_trac_ipmi,"Sensor Number = 0x%x", l_sensor_number);
                    break;
                }

            // these targets have specific status sensors
            case TARGETING::TYPE_DIMM:
            case TARGETING::TYPE_MEMBUF:
            case TARGETING::TYPE_PROC:
                {
                    l_sensor_number =
                                StatusSensor(i_pTarget).getSensorNumber();

                    TRACDCOMP(g_trac_ipmi,"Sensor Number = 0x%x", l_sensor_number);
                    break;
                }

            default:
                {

                    TARGETING::EntityPath l_targetPath =
                        i_pTarget->getAttr<TARGETING::ATTR_PHYS_PATH>();

                    // chop off the last part and go again.
                    l_targetPath.removeLast();

                    TARGETING::TargetHandle_t l_target = NULL;
                    l_target =
                        TARGETING::targetService().toTarget(l_targetPath);

                    l_sensor_number =  getFaultSensorNumber(
                        static_cast<TARGETING::ConstTargetHandle_t>(l_target));

                    break;
                }
        }

        TRACDCOMP(g_trac_ipmi,"<<getFaultSensorNumber() returning sensor number %#x", l_sensor_number);

        return l_sensor_number;
    }

    // interface to retrieve the APSS channel sensor numbers.
    errlHndl_t getAPSSChannelSensorNumbers(
            const uint32_t (* &o_sensor_numbers)[16])
    {

        TARGETING::TargetHandle_t l_sys;

        // get the "system error" sensor number associated with the
        // system target.

        TARGETING::targetService().getTopLevelTarget(l_sys);

        static TARGETING::ATTR_ADC_CHANNEL_SENSOR_NUMBERS_type
                                                apss_sensors;

        if( l_sys->tryGetAttr<TARGETING::
                ATTR_ADC_CHANNEL_SENSOR_NUMBERS>(apss_sensors) )
        {
            o_sensor_numbers = &apss_sensors;
        }
        else
        {
            // need that attribute or things dont work
            assert(0,"Missing ADC_CHANNEL_SENSOR_NUMBERS attribute");
        }

        return NULL;
    }

    uint16_t getSensorOffsets( TARGETING::SENSOR_NAME i_name,
                             sensorReadingType &o_readType )
    {

        uint16_t offsets = 0;

        // most of our sensors use generic sensor specific reading types
        // so use that as the default value
        o_readType = SENSOR_SPECIFIC;

        // sensor type is lower byte of sensor name, if we dont match
        // based on name, then try the sensor type
        uint16_t t = ( i_name >> 8 ) & 0x00FF;

        switch( i_name )
        {
            case TARGETING::SENSOR_NAME_FW_BOOT_PROGRESS:
                {
                    offsets = ( 1 << SYSTEM_FIRMWARE_PROGRESS );
                    break;
                }
            case TARGETING::SENSOR_NAME_OCC_ACTIVE:
                {
                    offsets = ( 1 << DEVICE_DISABLED ) |
                              ( 1 << DEVICE_ENABLED );
                    o_readType = DIGITAL_ENABLE_DISABLE;
                    break;
                }
            case TARGETING::SENSOR_NAME_HOST_STATUS:
                {
                    offsets = ( 1 << S0_G0_WORKING ) |
                              ( 1 << G5_SOFT_OFF )   |
                              ( 1 << LEGACY_ON );
                    break;
                }
            case TARGETING::SENSOR_NAME_PCI_ACTIVE:
            case TARGETING::SENSOR_NAME_OS_BOOT:
                {
                    // default all offsets enabled
                    offsets = 0x7FFF;
                    break;
                }

            default:
                {
                    // try sensor type
                    switch (t)
                    {
                        case TARGETING::SENSOR_TYPE_FAULT:
                            offsets = ( 1 << ASSERTED );
                            o_readType = DIGITAL_ASSERT_DEASSERT;
                            break;

                        case TARGETING::SENSOR_TYPE_PROCESSOR:
                            offsets = ( 1 << PROC_PRESENCE_DETECTED ) |
                                      ( 1 << PROC_DISABLED )          |
                                      ( 1 << IERR );
                            break;

                        case TARGETING::SENSOR_TYPE_MEMORY:
                            offsets = ( 1 << MEMORY_DEVICE_DISABLED ) |
                                ( 1 << MEM_DEVICE_PRESENCE_DETECTED );
                            break;
                        default:
                            offsets = 0;
                            o_readType = THRESHOLD;
                            break;
                    }

                }
        }

        return offsets;
    }

    uint8_t getBackPlaneFaultSensor()
    {
        TARGETING::TargetHandle_t sys = NULL;
        TARGETING::TargetHandleList nodes;
        TARGETING::targetService().getTopLevelTarget(sys);
        assert(sys != NULL);
        getChildAffinityTargets(nodes, sys, TARGETING::CLASS_ENC,
                                TARGETING::TYPE_NODE, false);
        assert(!nodes.empty());

        //Backplane sensor ID
        return TARGETING::UTIL::getSensorNumber(nodes[0],
                                        TARGETING::SENSOR_NAME_BACKPLANE_FAULT);
    }

    /**
     * @brief  All sensors returned cfgID bit
     */
    static const uint16_t NVCFG_ALL_SENSORS_RETURNED = 0xFFFF;

    /**
     * @brief  Helper function to getGpuSensors()
     *         NV keyword tells us what backplane is installed,
     *         thus what GPUs are supported
     *
     * @param[out] returns NV keyword in bitwise format
     *
     * @return  Error log handle if a deviceRead fails
     */
    errlHndl_t getNVCfgIDBit(uint16_t & o_cfgID_bitwise)
    {
        static uint16_t L_NV_bits = 0;
        errlHndl_t l_err = nullptr;

        do {
            // only do the lookup once
            if (L_NV_bits != 0)
            {
                break;
            }

            // grab system enclosure node
            TARGETING::TargetHandle_t l_sys = NULL;
            TARGETING::TargetHandleList l_nodeTargets;
            TARGETING::targetService().getTopLevelTarget(l_sys);
            assert(l_sys != NULL);
            getChildAffinityTargets(l_nodeTargets, l_sys, TARGETING::CLASS_ENC,
                                    TARGETING::TYPE_NODE);
            if(l_nodeTargets.empty())
            {
                TRACFCOMP(g_trac_ipmi,"getNVCfgIDBit(): No functional nodes - forcing invalid config");
                break;
            }

            // get keyword size first
            PVPD::pvpdRecord l_Record = PVPD::VNDR;
            PVPD::pvpdKeyword l_KeyWord = PVPD::NV;
            size_t l_nvKwdSize = 0;
            l_err = deviceRead(l_nodeTargets[0],NULL,l_nvKwdSize,
                               DEVICE_PVPD_ADDRESS(l_Record,l_KeyWord));
            if (l_err)
            {
                TRACFCOMP(g_trac_ipmi,"getNVCfgIDBit(): Error getting VNDR:NV size");
                break;
            }

            if (l_nvKwdSize != sizeof(HDAT::hdatNVKwdStruct_t))
            {
                TRACFCOMP(g_trac_ipmi,"Invalid NV keyword size: %d, expected %d",l_nvKwdSize,sizeof(HDAT::hdatNVKwdStruct_t));
                break;
            }

            uint8_t l_kwd[l_nvKwdSize] = {0};
            // now read the keyword
            l_err = deviceRead(l_nodeTargets[0],l_kwd,l_nvKwdSize,
                               DEVICE_PVPD_ADDRESS(l_Record,l_KeyWord));
            if (l_err)
            {
                TRACFCOMP(g_trac_ipmi,"getNVCfgIDBit(): Error reading VNDR:NV");
                break;
            }

            HDAT::hdatNVKwdStruct_t * NVptr =
              reinterpret_cast<HDAT::hdatNVKwdStruct_t*>(l_kwd);

            // Valid NV keyword config has NV00 as magic header
            if ( !memcmp((char*)&(NVptr->magic),"NV00", 4) )
            {
                uint8_t cfgID = NVptr->config;
                if (cfgID < 16) // maximum setting (bits 0-15)
                {
                    L_NV_bits = 0x0001 << cfgID;
                }
                else
                {
                    TRACFCOMP(g_trac_ipmi,"getNVCfgIDBit(): Invalid NV config 0x%02X", cfgID);
                    break;
                }
            }
            else
            {
                TRACFCOMP(g_trac_ipmi, "Invalid NV magic header: 0x%.8X", NVptr->magic);
                TRACFBIN(g_trac_ipmi, "NV KEYWORD", l_kwd, l_nvKwdSize);
            }
        } while(0);

        o_cfgID_bitwise = L_NV_bits;

        return l_err;
    }

    /**
     *  @brief Grab the GPU sensor type IDs for a particular processor target
     *
     *   Will return all sensor ids that match the type for a given target.
     *
     *  @param[in] - i_proc  - processor target
     *  @param[in] - i_type  - Functional/state, gpucoretemp, gpumemtemp
     *  @param[out] - o_num_ids - number of valid IDs returned in o_ids
     *  @param[out] - o_ids     - ordered list of sensor IDs
     *
     *  @return Errorlog handle
     */
    errlHndl_t getGpuSensors( TARGETING::Target* i_proc,
                              HWAS::sensorTypeEnum i_type,
                              uint8_t & o_num_ids,
                              uint32_t o_ids[MAX_GPU_SENSORS_PER_PROCESSOR] )
    {
        static uint16_t L_obus_cfgID_bit = 0;
        errlHndl_t l_errl = nullptr;

        // default to no ids returned
        o_num_ids = 0;

        TARGETING::AttributeTraits<TARGETING::ATTR_GPU_SENSORS>::Type
                                                                 l_sensorArray;

        bool foundSensors = i_proc->tryGetAttr<TARGETING::ATTR_GPU_SENSORS>
                                                                (l_sensorArray);

        // Verify we are getting non-default values
        if (foundSensors && l_sensorArray[0][0] != 0)
        {
            // Figure out which backplane we have
            // Only read NV keyword once (if possible)
            if (L_obus_cfgID_bit == 0)
            {
                l_errl = getNVCfgIDBit(L_obus_cfgID_bit);
                if (l_errl || (L_obus_cfgID_bit == 0))
                {
                    delete l_errl;
                    l_errl = nullptr;
                    // default to full list of GPU sensors
                    L_obus_cfgID_bit = NVCFG_ALL_SENSORS_RETURNED;
                }
            }

            uint32_t elementCount = (sizeof(l_sensorArray)/
                                     sizeof(l_sensorArray[0]));
            TRACFCOMP(g_trac_ipmi,"getGpuSensors() -> GPU_SENSORS array size = %d, cfgBit = 0x%x",
                     elementCount, L_obus_cfgID_bit);

            // verify array index won't exceed output array (o_ids)
            assert(elementCount <= MAX_GPU_SENSORS_PER_PROCESSOR);

            // now cycle through each GPU row
            for (uint32_t index = 0; index < elementCount; index++)
            {
                uint16_t * row_ptr = &l_sensorArray[index][0];

                TRACFCOMP(g_trac_ipmi,"getGpuSensors() -> ROW %d, 0x%04X, 0x%X, 0x%04X, 0x%X, 0x%04X, 0x%X, 0x%X",
                        index, row_ptr[0], row_ptr[1], row_ptr[2],
                        row_ptr[3], row_ptr[4], row_ptr[5], row_ptr[6]);

                // Include Sensor if the GPU is present in the current OBUS_CFG
                if ( (L_obus_cfgID_bit == NVCFG_ALL_SENSORS_RETURNED) ||
                    ((L_obus_cfgID_bit &
                      row_ptr[TARGETING::GPU_SENSOR_ARRAY_OBUS_CFG_OFFSET])
                    == L_obus_cfgID_bit) )
                {
                    switch(i_type)
                    {
                        case HWAS::GPU_FUNC_SENSOR:
                            o_ids[index] =
                            row_ptr[TARGETING::GPU_SENSOR_ARRAY_FUNC_ID_OFFSET];
                            o_num_ids++;
                        break;
                        case HWAS::GPU_MEMORY_TEMP_SENSOR:
                            o_ids[index] =
                        row_ptr[TARGETING::GPU_SENSOR_ARRAY_MEM_TEMP_ID_OFFSET];
                            o_num_ids++;
                        break;
                        case HWAS::GPU_TEMPERATURE_SENSOR:
                            o_ids[index] =
                            row_ptr[TARGETING::GPU_SENSOR_ARRAY_TEMP_ID_OFFSET];
                            o_num_ids++;
                        break;
                        default:
                            TRACFCOMP(g_trac_ipmi,"getGpuSensors() -> unknown sensor type 0x%02X", i_type);
                            o_ids[index] = TARGETING::UTIL::INVALID_IPMI_SENSOR;
                    }
                }
                else
                {
                    o_ids[index] = TARGETING::UTIL::INVALID_IPMI_SENSOR;
                }
                TRACFCOMP(g_trac_ipmi,
                    "getGpuSensors() -> o_id[%d] = 0x%X", index, o_ids[index]);
            } // end of for loop
        } // end of if check for non-default values

        return NULL;
    } // end getGpuSensors()


    /**
     * @brief   Helper function that sends GPU sensor status to BMC
     *
     * @param[in] sensor name (IPMI_SENSOR_TYPE with IPMI_ENTITY_ID)
     * @param[in] sensor id number
     * @param[in] processor target
     * @param[in] status to send for the identified GPU sensor
     */
    void sendGpuSensorStatus(uint16_t i_sensor_name_value,
                            uint16_t i_sensor_id,
                            TARGETING::ConstTargetHandle_t i_target,
                            StatusSensor::statusEnum & i_status)
    {
        TRACFCOMP(g_trac_ipmi, "sendGpuSensorStatus(0x%0X, 0x%X, Target 0x%X, status: %d)",
            i_sensor_name_value, i_sensor_id,
            TARGETING::get_huid(i_target), i_status);

        TARGETING::SENSOR_NAME l_sensor_name;
        switch(i_sensor_name_value)
        {
            case TARGETING::SENSOR_NAME_GPU_TEMP:
                l_sensor_name = TARGETING::SENSOR_NAME_GPU_TEMP;
                break;
            case TARGETING::SENSOR_NAME_GPU_STATE:
                l_sensor_name = TARGETING::SENSOR_NAME_GPU_STATE;
                break;
            case TARGETING::SENSOR_NAME_GPU_MEM_TEMP:
                l_sensor_name = TARGETING::SENSOR_NAME_GPU_MEM_TEMP;
                break;
            default:
                TRACFCOMP(g_trac_ipmi, "sendGpuSensorStatus(0x%0X, 0x%X) - unknown GPU sensor name",
                    i_sensor_name_value, i_sensor_id);
                l_sensor_name = TARGETING::SENSOR_NAME_FAULT;
            break;
        }

        // Only update if we found a valid gpu sensor name
        if (l_sensor_name != TARGETING::SENSOR_NAME_FAULT)
        {
            // create a GPU status sensor for our needs
            GpuSensor l_sensor(l_sensor_name, i_sensor_id, i_target);

            // send the status to the BMC
            errlHndl_t l_err = l_sensor.setStatus( i_status );

            // commit the error and move to the next target
            if( l_err )
            {
               errlCommit( l_err, IPMI_COMP_ID );
            }
        }
    }

    /**
     *  @brief  Updates GPU sensor status for GPUs on this
     *          particular processor target
     *
     *  @param[in] - i_proc       - processor target
     *  @param[in] - i_gpu_status - status of GPU0, GPU1 and GPU2
     */
    void updateGpuSensorStatus( TARGETING::Target* i_proc,
                    StatusSensor::statusEnum i_gpu_status[MAX_PROCESSOR_GPUS] )
    {
        uint16_t obus_cfgID_bit = 0;

        TARGETING::AttributeTraits<TARGETING::ATTR_GPU_SENSORS>::Type
                                                                  l_sensorArray;

        bool foundSensors = i_proc->tryGetAttr<TARGETING::ATTR_GPU_SENSORS>
                                                                (l_sensorArray);

        // Verify we are getting non-default values
        if (foundSensors && (l_sensorArray[0][0] != 0))
        {
            // Figure out which backplane we have
            // Only read NV keyword once (if possible)
            errlHndl_t l_errl = getNVCfgIDBit(obus_cfgID_bit);
            if (l_errl || (obus_cfgID_bit == 0))
            {
                // default to all sensors
                obus_cfgID_bit = NVCFG_ALL_SENSORS_RETURNED;
                delete l_errl;
            }

            uint32_t elementCount = (sizeof(l_sensorArray)/
                                     sizeof(l_sensorArray[0]));
            TRACDCOMP(g_trac_ipmi,"updateGpuSensorStatus() -> array size = %d, cfgBit = 0x%x",
                     elementCount, obus_cfgID_bit);

            // verify array index won't exceed output array (o_ids)
            assert(elementCount <= MAX_PROCESSOR_GPUS);

            // now cycle through each GPU row
            for (uint8_t index = 0; index < MAX_PROCESSOR_GPUS; index++)
            {
                uint16_t * sensor_row_ptr = &l_sensorArray[index][0];
                StatusSensor::statusEnum newStatus = i_gpu_status[index];

                // Include Sensor if the GPU is present in the current OBUS_CFG
                if ( (obus_cfgID_bit == NVCFG_ALL_SENSORS_RETURNED) ||
                    ((obus_cfgID_bit &
                    sensor_row_ptr[TARGETING::GPU_SENSOR_ARRAY_OBUS_CFG_OFFSET])
                    == obus_cfgID_bit) )
                {
                    // Only update the GPU status sensors, skip temperature ones
                    // GPU core Status/Functional Sensor
                    uint16_t sensor_name =
                sensor_row_ptr[TARGETING::GPU_SENSOR_ARRAY_FUNC_OFFSET];
                    uint16_t sensor_id =
                sensor_row_ptr[TARGETING::GPU_SENSOR_ARRAY_FUNC_ID_OFFSET];
                    sendGpuSensorStatus(sensor_name,sensor_id,i_proc,newStatus);
                }
            } // end of GPU loop
        } // end of if check for non-default values
    } // end of updateGpuSensorStatus()


    //
    // HbVolatileSensor constructor - uses system target
    //
    HbVolatileSensor::HbVolatileSensor()
        :SensorBase(TARGETING::SENSOR_NAME_HB_VOLATILE, NULL)
    {
        // message buffer created and initialized in base object.
    }

    //
    // HbVolatileSensor destructor
    //
    HbVolatileSensor::~HbVolatileSensor(){};

    //
    // setHbVolatile - tell BMC to make hostboot volatile memory
    //                 (volatile(2) or not(1))
    //
    errlHndl_t HbVolatileSensor::setHbVolatile( hbVolatileSetting i_setting )
    {
        // adjust the operation to overwrite the sensor reading
        // to the value we send.
        iv_msg->iv_operation = SET_SENSOR_VALUE_OPERATION;

        // the HB_VOLATILE Sensor is defined as a discrete sensor
        // but the assertion bytes are being used to transfer the state
        iv_msg->iv_assertion_mask = le16toh(i_setting);

        TRACFCOMP(g_trac_ipmi,"HbVolatileSensor::setHbVolatile(%d)",
                    i_setting);

        return writeSensorData();
    }

    //
    // getHbVolatile - get the HB volatile memory setting from the BMC
    //
    errlHndl_t HbVolatileSensor::getHbVolatile( hbVolatileSetting &o_setting )
    {
        // the HB_VOLATILE sensor is defined as a discrete sensor
        getSensorReadingData l_data;

        errlHndl_t l_err = readSensorData( l_data );

        if( l_err == nullptr )
        {
            // check if in valid range of hbVolatileSetting enums
            if ( l_data.event_status == ENABLE_VOLATILE ||
                 l_data.event_status == DISABLE_VOLATILE )
            {
                o_setting = static_cast<hbVolatileSetting>(l_data.event_status);
            }
            else
            {
                TRACFCOMP(g_trac_ipmi,"Unknown hb volatile setting: %d",
                    l_data.event_status);

                /*@
                 * @errortype    ERRL_SEV_UNRECOVERABLE
                 * @moduleid     IPMI::MOD_IPMISENSOR_HBVOLATILE
                 * @reasoncode   IPMI::RC_INVALID_SENSOR_SETTING
                 * @userdata1    Invalid hb volatile control setting
                 * @userdata2    <unused>
                 * @devdesc      The sensor returned an invalid setting
                 * @custdesc     Unable to find a valid sensor setting.
                 */
                l_err = new ERRORLOG::ErrlEntry(
                                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                            IPMI::MOD_IPMISENSOR_HBVOLATILE,
                                            IPMI::RC_INVALID_SENSOR_SETTING,
                                            l_data.event_status,
                                            0,
                                            false);
            }
        }
        return l_err;
    }

}; // end name space
OpenPOWER on IntegriCloud