summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/plat/mem/prdfP9Mca.C
blob: fac29fce3f5a27b1d7f256b87b06eb60b0d01e52 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/plat/mem/prdfP9Mca.C $                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2019                        */
/* [+] 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                                                     */

// Framework includes
#include <iipServiceDataCollector.h>
#include <prdfExtensibleChip.H>
#include <prdfPluginMap.H>

// Platform includes
#include <prdfMemDbUtils.H>
#include <prdfMemEccAnalysis.H>
#include <prdfP9McbistDataBundle.H>
#include <prdfPlatServices.H>
#ifdef __HOSTBOOT_RUNTIME
  #include <prdfMemTps.H>
#endif

#ifdef CONFIG_NVDIMM
    #include <nvdimm.H>
#endif

using namespace TARGETING;

namespace PRDF
{

using namespace PlatServices;

namespace nimbus_mca
{

//##############################################################################
//
//                             Special plugins
//
//##############################################################################

/**
 * @brief  Plugin function called after analysis is complete but before PRD
 *         exits.
 * @param  i_chip An MCA chip.
 * @param  io_sc  The step code data struct.
 * @note   This is especially useful for any analysis that still needs to be
 *         done after the framework clears the FIR bits that were at attention.
 * @return SUCCESS.
 */
int32_t PostAnalysis( ExtensibleChip * i_chip, STEP_CODE_DATA_STRUCT & io_sc )
{
    #define PRDF_FUNC "[nimbus_mca::PostAnalysis] "

    #ifdef __HOSTBOOT_RUNTIME

    // If the IUE threshold in our data bundle has been reached, we trigger
    // a port fail. Once we trigger the port fail, the system may crash
    // right away. Since PRD is running in the hypervisor, it is possible we
    // may not get the error log. To better our chances, we trigger the port
    // fail here after the error log has been committed.
    if ( MemEcc::queryIueTh<TYPE_MCA>(i_chip, io_sc) )
    {
        if ( SUCCESS != MemEcc::triggerPortFail<TYPE_MCA>(i_chip) )
        {
            PRDF_ERR( PRDF_FUNC "triggerPortFail(0x%08x) failed",
            i_chip->getHuid() );
        }
    }

    #endif // __HOSTBOOT_RUNTIME

    return SUCCESS; // Always return SUCCESS for this plugin.

    #undef PRDF_FUNC
}
PRDF_PLUGIN_DEFINE( nimbus_mca, PostAnalysis );


//##############################################################################
//
//                               MCACALFIR
//
//##############################################################################

/**
 * @brief  MCACALFIR[4] - RCD Parity Error.
 * @param  i_mcaChip A P9 MCA chip.
 * @param  io_sc     The step code data struct.
 * @return SUCCESS
 */
int32_t RcdParityError( ExtensibleChip * i_mcaChip,
                        STEP_CODE_DATA_STRUCT & io_sc )
{
    #define PRDF_FUNC "[nimbus_mca::RcdParityError] "

    // The callouts have already been made in the rule code. All other actions
    // documented below.

    // Nothing more to do if this is a checkstop attention.
    if ( CHECK_STOP == io_sc.service_data->getPrimaryAttnType() )
        return SUCCESS;

    uint32_t l_rc = SUCCESS;

    // If MCBISTFIR[3] is found to be on at the same time, mask it so it won't
    // be logged as a separate event.
    ExtensibleChip * mcbChip = getConnectedParent( i_mcaChip, TYPE_MCBIST );

    SCAN_COMM_REGISTER_CLASS * mcbistfir = mcbChip->getRegister( "MCBISTFIR" );
    l_rc = mcbistfir->Read();
    if ( SUCCESS != l_rc )
    {
        PRDF_ERR( PRDF_FUNC "Read() failed on MCBISTFIR");
    }
    else if ( mcbistfir->IsBitSet(3) )
    {
        SCAN_COMM_REGISTER_CLASS * mcbistfir_mask_or =
            mcbChip->getRegister( "MCBISTFIR_MASK_OR" );
        mcbistfir_mask_or->SetBit(3);
        l_rc = mcbistfir_mask_or->Write();
        if ( SUCCESS != l_rc )
        {
            PRDF_ERR( PRDF_FUNC "Write() failed on MCBIST_MASK_OR: "
                      "mcbChip=0x%08x", mcbChip->getHuid() );
        }
    }

    #ifdef __HOSTBOOT_RUNTIME // TPS only supported at runtime.

    // Recovery is always enabled during runtime. If the threshold is reached,
    // make the error log predictive and start TPS on all slave ranks behind
    // the MCA.
    if ( getMcaDataBundle(i_mcaChip)->iv_rcdParityTh.inc(io_sc) )
    {
        io_sc.service_data->setServiceCall();

        std::vector<MemRank> list;
        getSlaveRanks<TYPE_MCA>( i_mcaChip->getTrgt(), list );
        PRDF_ASSERT( !list.empty() ); // target configured with no ranks

        for ( auto & r : list )
        {
            TdEntry * entry = new TpsEvent<TYPE_MCA>( i_mcaChip, r );
            MemDbUtils::pushToQueue<TYPE_MCA>( i_mcaChip, entry );
            uint32_t rc = MemDbUtils::handleTdEvent<TYPE_MCA>(i_mcaChip, io_sc);
            if ( SUCCESS != rc )
            {
                PRDF_ERR( PRDF_FUNC "handleTdEvent() failed on 0x%08x",
                          i_mcaChip->getHuid() );

                continue; // Try the other ranks.
            }
        }
    }

    #else // IPL

    SCAN_COMM_REGISTER_CLASS * farb0 = i_mcaChip->getRegister("FARB0");
    if ( SUCCESS != farb0->Read() )
    {
        PRDF_ERR( PRDF_FUNC "Read() failed on MCAECCFIR: i_mcaChip=0x%08x",
                  i_mcaChip->getHuid() );

        // Ensure the reg is zero so that we will use the recovery threshold and
        // guarantee we don't try to do a reconfig.
        farb0->clearAllBits();
    }

    if ( farb0->IsBitSet(54) )
    {
        // Recovery is disabled. Issue a reconfig loop. Make the error log
        // predictive if threshold is reached.
        if ( rcdParityErrorReconfigLoop(i_mcaChip->getTrgt()) )
            io_sc.service_data->setServiceCall();

        if ( isInMdiaMode() )
        {
            SCAN_COMM_REGISTER_CLASS * mask = nullptr;

            // Stop any further commands on this MCBIST to avoid subsequent RCD
            // errors or potential AUEs.
            l_rc = mdiaSendEventMsg( mcbChip->getTrgt(), MDIA::STOP_TESTING );
            if ( SUCCESS != l_rc )
            {
                PRDF_ERR( PRDF_FUNC "mdiaSendEventMsg(STOP_TESTING) failed" );
            }

            // Mask the maintenance AUE/IAUE attentions on this MCA because they
            // are potential side-effects of the RCD parity errors.
            mask = i_mcaChip->getRegister( "MCAECCFIR_MASK_OR" );
            mask->SetBit(33); // maintenance AUE
            mask->SetBit(36); // maintenance IAUE
            l_rc = mask->Write();
            if ( SUCCESS != l_rc )
            {
                PRDF_ERR( PRDF_FUNC "Write() failed on MCAECCFIR_MASK_OR: "
                          "i_mcaChip=0x%08x", i_mcaChip->getHuid() );
            }

            // Mask the maintenance command complete bits to avoid false
            // attentions.
            mask = mcbChip->getRegister( "MCBISTFIR_MASK_OR" );
            mask->SetBit(10); // Command complete
            mask->SetBit(12); // WAT workaround
            l_rc = mask->Write();
            if ( SUCCESS != l_rc )
            {
                PRDF_ERR( PRDF_FUNC "Write() failed on MCBISTFIR_MASK_OR: "
                          "mcbChip=0x%08x", mcbChip->getHuid() );
            }
        }
    }
    else
    {
        // Make the error log predictive if the recovery threshold is reached.
        // Don't bother with TPS on all ranks because it is too complicated to
        // handle during Memory Diagnostics and we don't have time to complete
        // the procedures at any other point during the IPL. The DIMMs will be
        // deconfigured during the IPL anyways. So not really much benefit
        // except for extra FFDC.
        if ( getMcaDataBundle(i_mcaChip)->iv_rcdParityTh.inc(io_sc) )
            io_sc.service_data->setServiceCall();
    }

    #endif

    if ( io_sc.service_data->queryServiceCall() )
    {
        // Mask both RCD parity error bits to prevent any flooding.
        SCAN_COMM_REGISTER_CLASS * mask
                                = i_mcaChip->getRegister( "MCACALFIR_MASK_OR" );
        mask->SetBit( 4);
        mask->SetBit(14);
        if ( SUCCESS != mask->Write() )
        {
            PRDF_ERR( PRDF_FUNC "Write() failed on MCACALFIR_MASK_OR: "
                      "i_mcaChip=0x%08x", i_mcaChip->getHuid() );
        }
    }

    return SUCCESS;

    #undef PRDF_FUNC
}
PRDF_PLUGIN_DEFINE( nimbus_mca, RcdParityError );

//------------------------------------------------------------------------------

/**
 * @brief  MCACALFIR[13] - Persistent RCD error, port failed.
 * @param  i_chip MCA chip.
 * @param  io_sc  The step code data struct.
 * @return SUCCESS
 */
int32_t MemPortFailure( ExtensibleChip * i_chip,
                        STEP_CODE_DATA_STRUCT & io_sc )
{
    #define PRDF_FUNC "[nimbus_mca::MemPortFailure] "

    if ( CHECK_STOP != io_sc.service_data->getPrimaryAttnType() )
    {
        // The port is dead. Mask off the entire port.
        uint32_t l_rc = MemEcc::maskMemPort<TYPE_MCA>( i_chip );
        if ( SUCCESS != l_rc )
        {
            PRDF_ERR( PRDF_FUNC "MemEcc::maskMemPort<TYPE_MCA>(0x%08x) failed",
                      i_chip->getHuid() );
        }
    }

    return SUCCESS; // nothing to return to rule code

    #undef PRDF_FUNC
}
PRDF_PLUGIN_DEFINE( nimbus_mca, MemPortFailure );

//##############################################################################
//
//                               NVDIMM
//
//##############################################################################

#ifdef CONFIG_NVDIMM
#ifdef __HOSTBOOT_RUNTIME

/**
 * @brief  Gets a map list of which bits are set from a uint8_t bit list (7:0)
 * @param  i_data uint8_t bit list (7:0)
 * @return map<uint8_t, bool> with which bits were set in the bit list.
 */
std::map<uint8_t,bool> __nvdimmGetActiveBits( uint8_t i_data )
{
    // NOTE: Bit position in i_data that we get from the NVDIMM status register
    //       will be in descending order, ie ordered 7 to 0 (left to right).
    std::map<uint8_t,bool> bitList;
    for ( uint8_t n = 0; n < 8; n++ )
    {
        if ( i_data & (0x01 << n) ) bitList[n] = true;
    }
    return bitList;
}

/**
 * @brief  Adds a callout of the NVDIMM backup power module
 * @param  i_dimm     The target dimm.
 * @param  i_priority The callout priority.
 * @return FAIL if unable to get the global error log, else SUCCESS
 */
uint32_t __addBpmCallout( TargetHandle_t i_dimm,
                          HWAS::callOutPriority i_priority )
{
    #define PRDF_FUNC "[__addBpmCallout] "

    uint32_t o_rc = SUCCESS;

    do
    {
        errlHndl_t mainErrl = nullptr;
        mainErrl = ServiceGeneratorClass::ThisServiceGenerator().getErrl();
        if ( nullptr == mainErrl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to get the global error log." );
            o_rc = FAIL;
            break;
        }

        // addPartCallout will default to GARD_NULL, NO_DECONFIG
        mainErrl->addPartCallout( i_dimm, HWAS::BPM_PART_TYPE,
                                  i_priority );

    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

/**
 * @brief  Adds a callout of the cable connecting an NVDIMM to its
 *         backup power module (BPM)
 * @param  i_dimm     The target dimm.
 * @param  i_priority The callout priority.
 * @return FAIL if unable to get the global error log, else SUCCESS
 */
uint32_t __addNvdimmCableCallout( TargetHandle_t i_dimm,
                                  HWAS::callOutPriority i_priority )
{
    #define PRDF_FUNC "[__addNvdimmCableCallout] "

    uint32_t o_rc = SUCCESS;

    do
    {
        errlHndl_t mainErrl = nullptr;
        mainErrl = ServiceGeneratorClass::ThisServiceGenerator().getErrl();
        if ( nullptr == mainErrl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to get the global error log." );
            o_rc = FAIL;
            break;
        }

        // addPartCallout will default to GARD_NULL, NO_DECONFIG
        mainErrl->addPartCallout( i_dimm, HWAS::BPM_CABLE_PART_TYPE,
                                  i_priority );

    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

/**
 * @brief  If a previous error has been found, add a signature to the
 *         multi-signature list, else set the primary signature.
 * @param  io_sc      The step code data struct.
 * @param  i_trgt     The target.
 * @param  i_errFound Whether an error has already been found or not.
 * @param  i_sig      The signature to be set.
 */
void __addSignature( STEP_CODE_DATA_STRUCT & io_sc, TargetHandle_t i_trgt,
                     bool i_errFound, uint32_t i_sig )
{
    if ( i_errFound )
    {
        io_sc.service_data->AddSignatureList( i_trgt, i_sig );
    }
    else
    {
        io_sc.service_data->setSignature( getHuid(i_trgt), i_sig );
    }
}

/**
 * @brief  Analyze NVDIMM Health Status0 Register for errors
 * @param  io_sc       The step code data struct.
 * @param  i_dimm      The target dimm.
 * @param  io_errFound Whether an error has already been found or not.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __analyzeHealthStatus0Reg(STEP_CODE_DATA_STRUCT & io_sc,
                                   TargetHandle_t i_dimm, bool & io_errFound)
{
    #define PRDF_FUNC "[__analyzeHealthStatus0Reg] "

    uint32_t o_rc = SUCCESS;
    uint8_t data = 0;

    // Get MCA, for signatures
    TargetHandle_t mca = getConnectedParent( i_dimm, TYPE_MCA );

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the Health Status0 Register (0xA1) 7:0
        errlHndl_t errl = deviceRead( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::MODULE_HEALTH_STATUS0) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Health Status0 Register. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }
        std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( data );

        // BIT 0: Voltage Regulator Fail
        if ( bitList.count(0) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_VoltRegFail );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 1: VDD Lost
        if ( bitList.count(1) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_VddLost );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 2: VPP Lost
        if ( bitList.count(2) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_VppLost );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 3: VTT Lost
        if ( bitList.count(3) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_VttLost );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 4: DRAM not Self Refresh
        if ( bitList.count(4) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_NotSelfRefr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 5: Controller HW Error
        if ( bitList.count(5) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_CtrlHwErr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 6: NVM Controller Error
        if ( bitList.count(6) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_NvmCtrlErr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 7: NVM Lifetime Error
        if ( bitList.count(7) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_NvmLifeErr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }

    }while(0);

    return o_rc;

    #undef PRDF_FUNC

}

/**
 * @brief  Analyze NVDIMM Health Status1 Register for errors
 * @param  io_sc       The step code data struct.
 * @param  i_dimm      The target dimm.
 * @param  io_errFound Whether an error has already been found or not.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __analyzeHealthStatus1Reg( STEP_CODE_DATA_STRUCT & io_sc,
                                    TargetHandle_t i_dimm, bool & io_errFound )
{
    #define PRDF_FUNC "[__analyzeHealthStatus1Reg] "

    uint32_t o_rc = SUCCESS;
    uint8_t data = 0;

    // Get MCA, for signatures
    TargetHandle_t mca = getConnectedParent( i_dimm, TYPE_MCA );

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the Health Status1 Register (0xA2) 7:0
        errlHndl_t errl = deviceRead( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::MODULE_HEALTH_STATUS1) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Health Status1 Register. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }
        std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( data );

        // BIT 0: Insufficient Energy
        if ( bitList.count(0) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_InsuffEnergy );

            // Callout BPM (backup power module) high, cable high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;
            o_rc = __addNvdimmCableCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 1: Invalid Firmware
        if ( bitList.count(1) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_InvFwErr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 2: Configuration Data Error
        if ( bitList.count(2) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_CnfgDataErr );
            // Callout NVDIMM on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            io_errFound = true;
        }
        // BIT 3: No Energy Source
        if ( bitList.count(3) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_NoEsPres );

            // Callout BPM (backup power module) high, cable high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;
            o_rc = __addNvdimmCableCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 4: Energy Policy Not Set
        if ( bitList.count(4) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_EsPolNotSet );

            // Callout FW (Level2 Support) High
            io_sc.service_data->SetCallout( LEVEL2_SUPPORT, MRU_HIGH, NO_GARD );

            // Callout NVDIMM low on 1st, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 5: Energy Source HW Error
        if ( bitList.count(5) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_EsHwFail );

            // Callout BPM (backup power module) high, cable high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;
            o_rc = __addNvdimmCableCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 6: Energy Source Health Assessment Error
        if ( bitList.count(6) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_EsHlthAssess);

            // Callout BPM (backup power module) high, cable high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;
            o_rc = __addNvdimmCableCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 7: Reserved

    }while(0);

    return o_rc;

    #undef PRDF_FUNC

}

/**
 * @brief  Reads and merges the data from two ES_TEMP registers to get the
 *         correct temperature format.
 * @param  i_dimm       The target nvdimm.
 * @param  i_tempMsbReg The address of the register that contains the most
 *                      significant byte of the temperature data.
 * @param  i_tempLsbReg The address of the register that contains the least
 *                      significant byte of the temperature data.
 * @param  o_tempData   The 16 bit temperature data.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __readTemp( TargetHandle_t i_dimm, uint16_t i_tempMsbReg,
                     uint16_t i_tempLsbReg, uint16_t & o_tempData )
{
    #define PRDF_FUNC "[__readTemp] "

    /*
     * -NOTE: Example showing how to read the temperature format:
     * ES_TEMP1  = 0x03 (MSB: bits 15-8)
     * ES_TEMP0  = 0x48 (LSB: bits 7-0)
     *
     * 0x0348 = 0000 0011 0100 1000 = 52.5 C
     *
     * -NOTE: bit definition:
     * [15:13]Reserved
     * [12]Sign 0 = positive, 1 = negative; 0°C should be expressed as positive
     * [11]  128°C
     * [10]   64°C
     * [9]    32°C
     * [8]    16°C
     * [7]     8°C
     * [6]     4°C
     * [5]     2°C
     * [4]     1°C
     * [3]   0.5°C
     * [2]  0.25°C
     * [1] 0.125°C Optional for temp fields; not used for temp th fields
     * [0]0.0625°C Optional for temp fields; not used for temp th fields
     */
    uint32_t o_rc = SUCCESS;

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;
        uint8_t msbData = 0;
        uint8_t lsbData = 0;

        // Read the two inputted temperature registers.
        errlHndl_t errl = deviceRead( i_dimm, &msbData, NVDIMM_SIZE,
                                      DEVICE_NVDIMM_ADDRESS(i_tempMsbReg) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read ES Temperature MSB Register. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }

        errl = deviceRead( i_dimm, &lsbData, NVDIMM_SIZE,
                           DEVICE_NVDIMM_ADDRESS(i_tempLsbReg) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read ES Temperature LSB Register. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }

        o_tempData = ((uint16_t)msbData << 8) | lsbData;

    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

/**
 * @brief  Analyze NVDIMM Error Threshold Status Register for errors
 * @param  io_sc       The step code data struct.
 * @param  i_dimm      The target dimm.
 * @param  io_errFound Whether an error has already been found or not.
 * @param  o_esTempErr A flag for whether we hit an ES TEMP error or not.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __analyzeErrorThrStatusReg( STEP_CODE_DATA_STRUCT & io_sc,
                                     TargetHandle_t i_dimm, bool & io_errFound,
                                     bool & o_esTempErr )
{
    #define PRDF_FUNC "[__analyzeErrorThrStatusReg] "

    uint32_t o_rc = SUCCESS;
    uint8_t data = 0;
    o_esTempErr = false;

    // Get MCA, for signatures
    TargetHandle_t mca = getConnectedParent( i_dimm, TYPE_MCA );

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the Error Threshold Status Register (0xA5) 7:0
        errlHndl_t errl = deviceRead( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::ERROR_THRESHOLD_STATUS) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Error Threshold Status Reg. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }
        std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( data );

        // BIT 0: NVM Lifetime Error -- ignore
        // BIT 1: ES Lifetime Error
        if ( bitList.count(1) )
        {
            __addSignature( io_sc, mca, io_errFound, PRDFSIG_EsLifeErr );

            // Callout BPM (backup power module) high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            io_errFound = true;
        }
        // BIT 2: ES Temperature Error
        if ( bitList.count(2) )
        {
            // Sleep two seconds to avoid exiting PRD analysis faster than the
            // ES_TEMP sample rate.
            PlatServices::milliSleep( 2, 0 );

            // Read the ES_TEMP and ES_TEMP_ERROR_HIGH_THRESHOLD values
            uint16_t msbEsTempReg = NVDIMM::i2cReg::ES_TEMP1;
            uint16_t lsbEsTempReg = NVDIMM::i2cReg::ES_TEMP0;
            uint16_t esTemp = 0;
            o_rc = __readTemp( i_dimm, msbEsTempReg, lsbEsTempReg, esTemp );
            if ( SUCCESS != o_rc ) break;

            uint16_t msbThReg = NVDIMM::i2cReg::ES_TEMP_ERROR_HIGH_THRESHOLD1;
            uint16_t lsbThReg = NVDIMM::i2cReg::ES_TEMP_ERROR_HIGH_THRESHOLD0;
            uint16_t esTempHighTh = 0;
            o_rc = __readTemp( i_dimm, msbThReg, lsbThReg, esTempHighTh );
            if ( SUCCESS != o_rc ) break;

            msbThReg = NVDIMM::i2cReg::ES_TEMP_ERROR_LOW_THRESHOLD1;
            lsbThReg = NVDIMM::i2cReg::ES_TEMP_ERROR_LOW_THRESHOLD0;
            uint16_t esTempLowTh = 0;
            o_rc = __readTemp( i_dimm, msbThReg, lsbThReg, esTempLowTh );
            if ( SUCCESS != o_rc ) break;

            // Check to see if the ES_TEMP is negative (bit 12)
            bool esTempNeg = false;
            if ( esTemp & 0x1000 ) esTempNeg = true;

            // If ES_TEMP is equal or above ES_TEMP_ERROR_HIGH_THRESHOLD
            // Just in case ES_TEMP has moved before we read it out, we'll add
            // a 2°C margin when comparing to the threshold.
            if ( (esTemp >= (esTempHighTh - 0x0020)) && !esTempNeg )
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpErrHigh );
            }
            // Else check if the error hit the low threshold, again with the
            // same 2°C margin.
            else if ( (esTemp <= (esTempLowTh + 0x0020)) || esTempNeg )
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpErrLow );
            }
            // Else the temperature must have gone back to a normal value, so
            // we will label this as a false alarm case.
            else
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpErrFa );
            }

            // Callout BPM (backup power module) high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );

            o_esTempErr = true;
            io_errFound = true;
        }
        // BIT 3:7: Reserved

    }while(0);

    return o_rc;

    #undef PRDF_FUNC

}

/**
 * @brief  Adjusts the warning threshold so that future warnings are allowed
 *         to report.
 * @param  io_sc       The step code data struct.
 * @param  i_dimm      The target nvdimm.
 * @param  i_warnThReg The address of the relevant warning threshold register.
 * @param  i_errThReg  The address of the relevant error threshold register.
 * @param  o_firstWarn Flag if this is the first warning of this type.
 * @param  o_statusErr Flag to tell if we found an error from checking the
 *                     notification status register.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __adjustThreshold( STEP_CODE_DATA_STRUCT & io_sc,
                            TargetHandle_t i_dimm, uint16_t i_warnThReg,
                            uint16_t i_errThReg, bool & o_firstWarn,
                            bool & o_statusErr )
{
    #define PRDF_FUNC "[__adjustThreshold] "

    uint32_t o_rc = SUCCESS;
    uint16_t notifCmdReg    = NVDIMM::i2cReg::SET_EVENT_NOTIFICATION_CMD;
    uint16_t notifStatusReg = NVDIMM::i2cReg::SET_EVENT_NOTIFICATION_STATUS;
    o_firstWarn = false;
    o_statusErr = false;

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the corresponding warning threshold
        uint8_t warnTh = 0;
        errlHndl_t errl = deviceRead( i_dimm, &warnTh, NVDIMM_SIZE,
                                      DEVICE_NVDIMM_ADDRESS(i_warnThReg) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Warning Threshold Reg. HUID: "
                      "0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }

        // Read the corresponding error threshold
        uint8_t errTh = 0;
        errl = deviceRead( i_dimm, &errTh, NVDIMM_SIZE,
                           DEVICE_NVDIMM_ADDRESS(i_errThReg) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Error Threshold Reg. HUID: "
                      "0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }

        // If the warning threshold is not set to the error threshold+1,
        // move the threshold.
        if ( warnTh != (errTh+1) )
        {
            o_firstWarn = true;

            // SET_EVENT_NOTIFICATION_CMD is a write only register that is
            // used to change the SET_EVENT_NOTIFICATION_STATUS register.
            // The only bits within it that are used are bits 0 and 1, as such
            // we can safely set the rest to 0. It is defined as:
            // [0]:   Persistency Notification
            // [1]:   Warning Threshold Notification
            // [2]:   Obsolete
            // [3]:   Firmware Activation Notification (Not Used)
            // [4:7]: Reserved

            // Clear SET_EVENT_NOTIFICATION_CMD bit 1 and keep bit 0 set
            uint8_t notifCmd = 0x01;
            errl = deviceWrite( i_dimm, &notifCmd, NVDIMM_SIZE,
                                DEVICE_NVDIMM_ADDRESS(notifCmdReg) );
            if ( errl )
            {
                PRDF_ERR( PRDF_FUNC "Failed to clear Set Event Notification "
                          "Cmd Reg. HUID: 0x%08x", getHuid(i_dimm) );
                PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
                o_rc = FAIL;
                break;
            }

            // Check SET_EVENT_NOTIFICATION_STATUS to ensure everything is set
            // as we expect and we don't see any errors.
            uint8_t notifStat = 0;
            errl = deviceRead( i_dimm, &notifStat, NVDIMM_SIZE,
                               DEVICE_NVDIMM_ADDRESS(notifStatusReg) );
            if ( errl )
            {
                PRDF_ERR( PRDF_FUNC "Failed to read Set Event Notification "
                          "Status Reg. HUID: 0x%08x", getHuid(i_dimm) );
                PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
                o_rc = FAIL;
                break;
            }
            std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( notifStat );

            // if Bit [1]: SET_EVENT_NOTIFICATION_ERROR = 1
            // or Bit [2]: PERSISTENCY_ENABLED = 0
            // or Bit [3]: WARNING_THRESHOLD_ENABLED = 1
            if ( bitList.count(1)  || !bitList.count(2) || bitList.count(3) )
            {
                o_statusErr = true;

                // Make the log predictive and mask the fir
                io_sc.service_data->SetThresholdMaskId(0);

                // Callout the NVDIMM, no gard
                io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );

                // Send message to PHYP that save/restore may work
                o_rc = PlatServices::nvdimmNotifyProtChange( i_dimm,
                    NVDIMM::NVDIMM_RISKY_HW_ERROR );
                if ( SUCCESS != o_rc ) break;

                break;
            }


            // Set the warning threshold to error threshold + 1
            warnTh = errTh+1;
            errl = deviceWrite( i_dimm, &warnTh, NVDIMM_SIZE,
                                DEVICE_NVDIMM_ADDRESS(i_warnThReg) );
            if ( errl )
            {
                PRDF_ERR( PRDF_FUNC "Failed to write Warning Threshold Reg. "
                          "HUID: 0x%08x", getHuid(i_dimm) );
                PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
                o_rc = FAIL;
                break;
            }

            // Set SET_EVENT_NOTIFICATION_CMD bit 1 and keep bit 0 set
            notifCmd = 0x03;
            errl = deviceWrite( i_dimm, &notifCmd, NVDIMM_SIZE,
                                DEVICE_NVDIMM_ADDRESS(notifCmdReg) );
            if ( errl )
            {
                PRDF_ERR( PRDF_FUNC "Failed to write Set Event Notification "
                          "Cmd Reg. HUID: 0x%08x", getHuid(i_dimm) );
                PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
                o_rc = FAIL;
                break;
            }

            // Recheck SET_EVENT_NOTIFICATION_STATUS to ensure everything is set
            // as we expect and we don't see any errors.
            errl = deviceRead( i_dimm, &notifStat, NVDIMM_SIZE,
                               DEVICE_NVDIMM_ADDRESS(notifStatusReg) );
            if ( errl )
            {
                PRDF_ERR( PRDF_FUNC "Failed to read Set Event Notification "
                          "Status Reg. HUID: 0x%08x", getHuid(i_dimm) );
                PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
                o_rc = FAIL;
                break;
            }
            bitList = __nvdimmGetActiveBits( notifStat );

            // if Bit [1]: SET_EVENT_NOTIFICATION_ERROR = 1
            // or Bit [2]: PERSISTENCY_ENABLED = 0
            // or Bit [3]: WARNING_THRESHOLD_ENABLED = 0
            if ( bitList.count(1)  || !bitList.count(2) || !bitList.count(3) )
            {
                o_statusErr = true;

                // Make the log predictive and mask the fir
                io_sc.service_data->SetThresholdMaskId(0);

                // Callout the NVDIMM, no gard
                io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );

                // Send message to PHYP that save/restore may work
                o_rc = PlatServices::nvdimmNotifyProtChange( i_dimm,
                    NVDIMM::NVDIMM_RISKY_HW_ERROR );
                if ( SUCCESS != o_rc ) break;

                break;
            }
        }
        // Note: moving the threshold should clear the warning from
        // WARNING_THRESHOLD_STATUS, which allows future warnings to report.

    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

/**
 * @brief  Analyze NVDIMM Warning Threshold Status Register for errors
 * @param  io_sc       The step code data struct.
 * @param  i_dimm      The target dimm.
 * @param  io_errFound Whether an error has already been found or not.
 * @return FAIL if unable to read register, else SUCCESS
 */
uint32_t __analyzeWarningThrStatusReg(STEP_CODE_DATA_STRUCT & io_sc,
                                      TargetHandle_t i_dimm, bool & io_errFound)
{
    #define PRDF_FUNC "[__analyzeWarningThrStatusReg] "

    uint32_t o_rc = SUCCESS;
    uint8_t data = 0;

    // Get MCA, for signatures
    TargetHandle_t mca = getConnectedParent( i_dimm, TYPE_MCA );

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the Warning Threshold Status Register (0xA7) 7:0
        errlHndl_t errl = deviceRead( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::WARNING_THRESHOLD_STATUS) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Warning Threshold Status Reg. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }
        std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( data );

        // Analyze Bit 2 First
        // BIT 2: ES_TEMP_WARNING
        if ( bitList.count(2) )
        {
            // Sleep two seconds to avoid exiting PRD analysis faster than the
            // ES_TEMP sample rate.
            PlatServices::milliSleep( 2, 0 );

            // Read the ES_TEMP and ES_TEMP_WARNING_HIGH_THRESHOLD values
            uint16_t msbEsTempReg = NVDIMM::i2cReg::ES_TEMP1;
            uint16_t lsbEsTempReg = NVDIMM::i2cReg::ES_TEMP0;
            uint16_t esTemp = 0;
            o_rc = __readTemp( i_dimm, msbEsTempReg, lsbEsTempReg, esTemp );
            if ( SUCCESS != o_rc ) break;

            uint16_t msbThReg = NVDIMM::i2cReg::ES_TEMP_WARNING_HIGH_THRESHOLD1;
            uint16_t lsbThReg = NVDIMM::i2cReg::ES_TEMP_WARNING_HIGH_THRESHOLD0;
            uint16_t esTempHighTh = 0;
            o_rc = __readTemp( i_dimm, msbThReg, lsbThReg, esTempHighTh );
            if ( SUCCESS != o_rc ) break;

            msbThReg = NVDIMM::i2cReg::ES_TEMP_WARNING_LOW_THRESHOLD1;
            lsbThReg = NVDIMM::i2cReg::ES_TEMP_WARNING_LOW_THRESHOLD0;
            uint16_t esTempLowTh = 0;
            o_rc = __readTemp( i_dimm, msbThReg, lsbThReg, esTempLowTh );
            if ( SUCCESS != o_rc ) break;

            // Check to see if the ES_TEMP is negative (bit 12)
            bool esTempNeg = false;
            if ( esTemp & 0x1000 ) esTempNeg = true;

            // If ES_TEMP is equal or above ES_TEMP_WARNING_HIGH_THRESHOLD
            // Just in case ES_TEMP has moved before we read it out, we'll add
            // a 2°C margin when comparing to the threshold.
            if ( (esTemp >= (esTempHighTh - 0x0020)) && !esTempNeg )
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpWarnHigh );
            }
            // Else check if the warning hit the low threshold, again with the
            // same 2°C margin.
            else if ( (esTemp <= (esTempLowTh + 0x0020)) || esTempNeg )
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpWarnLow );
            }
            // Else the temperature must have gone back to a normal value, so
            // we will label this as a false alarm case.
            else
            {
                __addSignature( io_sc, mca, io_errFound,
                                PRDFSIG_EsTmpWarnFa );
            }

            // Callout BPM (backup power module) high
            o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != o_rc ) break;

            // Callout NVDIMM low, no gard
            io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );

            // Because of the possibility of intermittent ES temperature
            // false alarm readings, we will keep the log hidden. If there is
            // an actual ES temperature problem, we assume we will continue
            // to be called to handle the temperature warning and hit threshold.

            // Only send the save/restore message to PHYP if we hit threshold.
            if ( io_sc.service_data->IsAtThreshold() )
            {
                // Send message to PHYP that save/restore may work
                o_rc = PlatServices::nvdimmNotifyProtChange( i_dimm,
                        NVDIMM::NVDIMM_RISKY_HW_ERROR );
                if ( SUCCESS != o_rc ) break;
            }

            io_errFound = true;
        }
        // BIT 0: NVM_LIFETIME_WARNING
        if ( bitList.count(0) )
        {
            // Adjust warning threshold.
            uint16_t warnThReg = NVDIMM::i2cReg::NVM_LIFETIME_WARNING_THRESHOLD;
            uint16_t errThReg  = NVDIMM::i2cReg::NVM_LIFETIME_ERROR_THRESHOLD;
            bool firstWarn = false;
            bool statusErr = false;
            o_rc = __adjustThreshold( io_sc, i_dimm, warnThReg, errThReg,
                                      firstWarn, statusErr );
            if ( SUCCESS != o_rc ) break;

            // Make the log predictive, but do not mask the FIR
            io_sc.service_data->setServiceCall();

            // If we got a set event notification status error, add the
            // signature for that before adding the signature for the warning.
            // Also do not take our normal callout action since we already will
            // have called out the NVDIMM because of the status error.
            if ( statusErr )
            {
                __addSignature( io_sc, mca, io_errFound, PRDFSIG_NotifStatErr );

                // Need to set io_errFound here so the warning signature is
                // added to the multi-signature list instead of as the primary
                // signature.
                io_errFound = true;
            }
            else
            {
                // Callout NVDIMM on 1st, no gard
                io_sc.service_data->SetCallout( i_dimm, MRU_MED, NO_GARD );
            }

            // Update signature depending on whether this is the first or second
            // warning of this type.
            if ( firstWarn )
            {
                __addSignature( io_sc, mca, io_errFound, PRDFSIG_NvmLifeWarn1 );
            }
            else
            {
                __addSignature( io_sc, mca, io_errFound, PRDFSIG_NvmLifeWarn2 );
            }


            io_errFound = true;
        }
        // BIT 1: ES_LIFETIME_WARNING
        if ( bitList.count(1) )
        {
            // Adjust warning threshold.
            uint16_t warnThReg = NVDIMM::i2cReg::ES_LIFETIME_WARNING_THRESHOLD;
            uint16_t errThReg  = NVDIMM::i2cReg::ES_LIFETIME_ERROR_THRESHOLD;
            bool firstWarn = false;
            bool statusErr = false;
            o_rc = __adjustThreshold( io_sc, i_dimm, warnThReg, errThReg,
                                      firstWarn, statusErr );
            if ( SUCCESS != o_rc ) break;

            // Make the log predictive, but do not mask the FIR
            io_sc.service_data->setServiceCall();

            // If we got a set event notification status error, add the
            // signature for that before adding the signature for the warning.
            // Also do not take our normal callout action since we already will
            // have called out the NVDIMM because of the status error.
            if ( statusErr )
            {
                __addSignature( io_sc, mca, io_errFound, PRDFSIG_NotifStatErr );

                // Need to set io_errFound here so the warning signature is
                // added to the multi-signature list instead of as the primary
                // signature.
                io_errFound = true;
            }
            else
            {
                // Callout BPM (backup power module) high
                o_rc = __addBpmCallout( i_dimm, HWAS::SRCI_PRIORITY_HIGH );
                if ( SUCCESS != o_rc ) break;

                // Callout NVDIMM low, no gard
                io_sc.service_data->SetCallout( i_dimm, MRU_LOW, NO_GARD );
            }

            // Update signature depending on whether this is the first or second
            // warning of this type.
            if ( firstWarn )
            {
                __addSignature(io_sc, mca, io_errFound, PRDFSIG_EsLifeWarn1);
            }
            else
            {
                __addSignature(io_sc, mca, io_errFound, PRDFSIG_EsLifeWarn2);
            }

            io_errFound = true;
        }

    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

/**
 * @brief  De-assert the EVENT_N pin by setting bit 2 in NVDIMM_MGT_CMD1 (0x41)
 * @param  i_dimm The target dimm.
 * @return FAIL if unable to read/write register, else SUCCESS
 */
uint32_t __deassertEventN( TargetHandle_t i_dimm )
{
    #define PRDF_FUNC "[__deassertEventN] "

    uint32_t o_rc = SUCCESS;
    uint8_t data = 0;

    do
    {
        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the NVDIMM_MGT_CMD1 register (0x41) 7:0
        errlHndl_t errl = deviceRead( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::NVDIMM_MGT_CMD1) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read NVDIMM_MGT_CMD1. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }

        // Set bit 2
        data |= 0x04;

        // Write the updated data back to NVDIMM_MGT_CMD1
        errl = deviceWrite( i_dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::NVDIMM_MGT_CMD1) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to write NVDIMM_MGT_CMD1. "
                      "HUID: 0x%08x", getHuid(i_dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            o_rc = FAIL;
            break;
        }


    }while(0);

    return o_rc;

    #undef PRDF_FUNC
}

#endif // HOSTBOOT_RUNTIME
#endif // CONFIG_NVDIMM

/**
 * @brief  MCACALFIR[8] - Error from NVDIMM health status registers
 * @param  i_chip MCA chip.
 * @param  io_sc  The step code data struct.
 * @return SUCCESS
 */
int32_t AnalyzeNvdimmHealthStatRegs( ExtensibleChip * i_chip,
                                     STEP_CODE_DATA_STRUCT & io_sc )
{
    #define PRDF_FUNC "[nimbus_mca::AnalyzeNvdimmHealthStatRegs] "

    #ifdef CONFIG_NVDIMM
    #ifdef __HOSTBOOT_RUNTIME

    uint32_t l_rc = SUCCESS;
    bool errFound = false;

    // We need to check both dimms for errors
    for ( auto & dimm : getConnected(i_chip->getTrgt(), TYPE_DIMM) )
    {
        // Skip any non-NVDIMMs
        if ( !isNVDIMM(dimm) ) continue;

        // Add SMART-specific, page 4 registers to FFDC
        errlHndl_t mainErrl = nullptr;
        mainErrl = ServiceGeneratorClass::ThisServiceGenerator().getErrl();
        if ( nullptr == mainErrl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to get the global error log." );
            continue;
        }
        PlatServices::nvdimmAddFfdc( dimm, mainErrl );

        // De-assert the EVENT_N pin by setting bit 2 in NVDIMM_MGT_CMD1
        l_rc = __deassertEventN( dimm );
        if ( SUCCESS != l_rc ) continue;

        uint8_t data = 0;

        // NVDIMM health status registers size = 1 byte
        size_t NVDIMM_SIZE = 1;

        // Read the Module Health Register (0xA0) 7:0
        errlHndl_t errl = deviceRead( dimm, &data, NVDIMM_SIZE,
            DEVICE_NVDIMM_ADDRESS(NVDIMM::i2cReg::MODULE_HEALTH) );
        if ( errl )
        {
            PRDF_ERR( PRDF_FUNC "Failed to read Module Health Register. "
                      "HUID: 0x%08x", getHuid(dimm) );
            PRDF_COMMIT_ERRL( errl, ERRL_ACTION_REPORT );
            continue;
        }
        std::map<uint8_t,bool> bitList = __nvdimmGetActiveBits( data );

        // BIT 0: Persistency Lost
        if ( bitList.count(0) )
        {
            // Analyze Health Status0 Reg, Health Status1 Reg,
            // and Error Theshold Status Reg
            l_rc = __analyzeHealthStatus0Reg( io_sc, dimm, errFound );
            if ( SUCCESS != l_rc ) continue;
            l_rc = __analyzeHealthStatus1Reg( io_sc, dimm, errFound );
            if ( SUCCESS != l_rc ) continue;
            bool esTempErr = false;
            l_rc = __analyzeErrorThrStatusReg(io_sc, dimm, errFound, esTempErr);
            if ( SUCCESS != l_rc ) continue;

            // If we hit an ES temperature error and have not yet hit threshold,
            // then keep the log hidden.
            if ( esTempErr && !io_sc.service_data->IsAtThreshold() ) continue;

            // If we didn't find any error, then keep the log hidden.
            if ( !errFound )
            {
                io_sc.service_data->setSignature( i_chip->getHuid(),
                    PRDFSIG_FirEvntGone );
                // Callout NVDIMM
                io_sc.service_data->SetCallout( dimm, MRU_MED, NO_GARD );
                continue;
            }

            // EVENT_N cannot be retriggered on a new PERSISTENCY_LOST_ERROR
            // if a previous PERSISTENCY_LOST_ERROR still exists. Meaning, we
            // cannot detect/report multiple errors that happen at different
            // points in time. As such, mask the EVENT_N bit here (MCACALFIR[8])
            // and make the log predictive.
            io_sc.service_data->SetThresholdMaskId(0);

            // Send message to PHYP that save/restore may work
            l_rc = PlatServices::nvdimmNotifyProtChange( dimm,
                NVDIMM::NVDIMM_RISKY_HW_ERROR );
            if ( SUCCESS != l_rc ) continue;

        }
        // BIT 1: Warning Threshold Exceeded
        else if ( bitList.count(1) )
        {
            l_rc = __analyzeWarningThrStatusReg( io_sc, dimm, errFound );
            if ( SUCCESS != l_rc ) continue;

            if ( !errFound )
            {
                io_sc.service_data->setSignature( i_chip->getHuid(),
                    PRDFSIG_FirEvntGone );
                // Callout NVDIMM
                io_sc.service_data->SetCallout( dimm, MRU_MED, NO_GARD );
                continue;
            }
        }
        // BIT 2: Persistency Restored
        else if ( bitList.count(2) )
        {
            // It would be rare to have an intermittent error that comes and
            // goes so fast we only see PERSISTENCY_RESTORED and not
            // PERSISTENCY_LOST_ERROR. Set predictive on threshold of 32
            // per day (rule code handles the thresholding), else just keep
            // as a hidden log.
            __addSignature( io_sc, i_chip->getTrgt(), errFound,
                            PRDFSIG_NvdimmPersRes );

            // Callout NVDIMM
            io_sc.service_data->SetCallout( dimm, MRU_MED, NO_GARD );
        }
        // BIT 3: Below Warning Threshold
        else if ( bitList.count(3) )
        {
            // Much like the persistency restored bit above, we don't expect
            // to see this, so just make a hidden log.
            __addSignature( io_sc, i_chip->getTrgt(), errFound,
                            PRDFSIG_BelowWarnTh );

            // Callout NVDIMM
            io_sc.service_data->SetCallout( dimm, MRU_MED, NO_GARD );
        }
        // BIT 4: Hardware Failure -- ignore - no logic feeding this
        // BIT 5: EVENT_N_LOW -- ignore
        // BIT 6:7: Unused

        // If we reach a threshold on MCACALFIR[8] of 32 per day, we assume
        // some intermittent error must be triggering the FIR that isn't a
        // persistency lost error which would cause us to mask. The rule code
        // handles the actual thresholding here.
        if ( io_sc.service_data->IsAtThreshold() && !errFound )
        {
            io_sc.service_data->setSignature( i_chip->getHuid(),
                                              PRDFSIG_IntNvdimmErr );

            // callout NVDIMM high, cable high, BPM high, no gard
            io_sc.service_data->SetCallout( dimm, MRU_HIGH, NO_GARD );
            l_rc = __addBpmCallout( dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != l_rc ) continue;
            l_rc = __addNvdimmCableCallout( dimm, HWAS::SRCI_PRIORITY_HIGH );
            if ( SUCCESS != l_rc ) continue;

            // Send message to PHYP that save/restore may work
            l_rc = PlatServices::nvdimmNotifyProtChange( dimm,
                    NVDIMM::NVDIMM_RISKY_HW_ERROR );
            if ( SUCCESS != l_rc ) continue;
        }
    }
    #else // IPL only

    // We don't expect to analyze NVDIMMs during IPL, so callout level 2 support
    PRDF_ERR( PRDF_FUNC "Unexpected call to analyze NVDIMMs at IPL." );
    io_sc.service_data->SetCallout( LEVEL2_SUPPORT, MRU_HIGH, NO_GARD );

    #endif // end runtime vs IPL check

    #else // CONFIG_NVDIMM not defined

    PRDF_ERR( PRDF_FUNC "CONFIG_NVDIMM not defined." );
    io_sc.service_data->SetCallout( LEVEL2_SUPPORT, MRU_HIGH, NO_GARD );

    #endif // end CONFIG_NVDIMM check

    return SUCCESS; // nothing to return to rule code

    #undef PRDF_FUNC
}
PRDF_PLUGIN_DEFINE( nimbus_mca, AnalyzeNvdimmHealthStatRegs );

} // end namespace nimbus_mca

} // end namespace PRDF

OpenPOWER on IntegriCloud