summaryrefslogtreecommitdiffstats
path: root/src/import/chips/p9/procedures/hwp/memory/lib/workarounds/dp16_workarounds.C
blob: 386765b3da34ee5750c1fe3702e148a1c7e38a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/memory/lib/workarounds/dp16_workarounds.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                                                     */

///
/// @file workarounds/dp16.C
/// @brief Workarounds for the DP16 logic blocks
/// Workarounds are very device specific, so there is no attempt to generalize
/// this code in any way.
///
// *HWP HWP Owner: Stephen Glancy <sglancy@us.ibm.com>
// *HWP HWP Backup: Andre Marin <aamarin@us.ibm.com>
// *HWP Team: Memory
// *HWP Level: 3
// *HWP Consumed by: FSP:HB

#include <lib/shared/nimbus_defaults.H>
#include <fapi2.H>
#include <p9_mc_scom_addresses.H>
#include <p9_mc_scom_addresses_fld.H>

#include <generic/memory/lib/utils/scom.H>
#include <generic/memory/lib/utils/pos.H>
#include <lib/workarounds/dp16_workarounds.H>
#include <lib/phy/dp16.H>
#include <lib/phy/ddr_phy.H>
#include <lib/phy/phy_cntrl.H>
#include <lib/phy/mss_training.H>
#include <lib/dimm/rank.H>
#include <lib/utils/bit_count.H>
#include <lib/fir/check.H>
#include <lib/dimm/ddr4/mrs_load_ddr4.H>
#include <lib/dimm/eff_dimm.H>

namespace mss
{

namespace workarounds
{

namespace dp16
{

///
/// @brief DQS polarity workaround
/// For Monza DDR port 2, one pair of DQS P/N is swapped polarity.  Not in DDR port 6
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note This function is called during the phy scom init procedure, after the initfile is
/// processed. It is specific to the Monza module, but can be called for all modules as it
/// will enforce its requirements internally
///
fapi2::ReturnCode dqs_polarity( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // Receiver config provided by S. Wyatt 8/16
    constexpr uint64_t rx_config = 0x4000;

    // For Monza DDR port 2, one pair of DQS P/N is swapped polarity.  Not in DDR port 6
    // For Monza DDR port 3, one pair of DQS P/N is swapped polarity.  Not in DDR port 7
    const auto l_pos = mss::pos(i_target);

    if (! mss::chip_ec_feature_mss_dqs_polarity(i_target) )
    {
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // So we need to make sure our position is 2 or 3 and skip for the other ports.
    if (l_pos == 2)
    {
        FAPI_TRY( mss::putScom(i_target, MCA_DDRPHY_DP16_RX_PEAK_AMP_P0_4, rx_config) );
    }

    if (l_pos == 3)
    {
        FAPI_TRY( mss::putScom(i_target, MCA_DDRPHY_DP16_RX_PEAK_AMP_P0_0, rx_config) );
    }

    // Can't just return current_err, if we're not ports 2,3 we didn't touch it ...
    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Modifies the VREF sense bit based upon the passed in value
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_state the state to set bit 62 to
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note this is a helper function to reduce repeated code in cleanup and workaround functions below
///
fapi2::ReturnCode modify_vref_sense(const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target, const mss::states i_state )
{
    // Runs the cleanup here
    static const std::vector<uint64_t> l_addrs =
    {
        MCA_DDRPHY_DP16_DATA_BIT_DIR1_P0_0,
        MCA_DDRPHY_DP16_DATA_BIT_DIR1_P0_1,
        MCA_DDRPHY_DP16_DATA_BIT_DIR1_P0_2,
        MCA_DDRPHY_DP16_DATA_BIT_DIR1_P0_3,
        MCA_DDRPHY_DP16_DATA_BIT_DIR1_P0_4,
    };

    // Note: this bit does not exist in our scom def, so constexpr'ing it here
    constexpr uint64_t VREFSENSE_BIT = 62;

    for(const auto& l_reg : l_addrs)
    {
        fapi2::buffer<uint64_t> l_data;

        // Gets the data
        FAPI_TRY(mss::getScom(i_target, l_reg, l_data));

        // Modifies the data
        l_data.writeBit<VREFSENSE_BIT>(i_state);

        // Writes the data
        FAPI_TRY(mss::putScom(i_target, l_reg, l_data));
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Workarounds for after training
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note This function is called after training - it will only be run after coarse wr/rd
///
fapi2::ReturnCode rd_vref_vref_sense_cleanup( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // If the workaround does not need to be run, return success
    if(mss::chip_ec_feature_skip_rd_vref_vrefsense_override(i_target))
    {
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Per Ryan King, this needs to be set to OFF for mainline mode
    return modify_vref_sense(i_target, mss::states::OFF);
}

///
/// @brief Sets up the VREF sense values before training
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note This function is called before training
///
fapi2::ReturnCode rd_vref_vref_sense_setup( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // If the workaround does not need to be run, return success
    if(mss::chip_ec_feature_skip_rd_vref_vrefsense_override(i_target))
    {
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Per Ryan King, this needs to be set to ON to run training
    return modify_vref_sense(i_target, mss::states::ON);
}

///
/// @brief Workarounds for after training
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_cal_steps_enable the enabled cal steps
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note This function is called after training - it will only be run after coarse wr/rd
///
fapi2::ReturnCode post_training_workarounds( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const fapi2::buffer<uint32_t>& i_cal_steps_enabled )
{
    // Only runs on the last cal steps (coarse wr/rd)
    if (i_cal_steps_enabled.getBit<mss::cal_steps::COARSE_RD>() ||
        i_cal_steps_enabled.getBit<mss::cal_steps::COARSE_WR>())
    {
        FAPI_TRY( mss::workarounds::dp16::modify_calibration_results( i_target ) );
    }

    // Returns success, as we might not have run these workarounds, depending upon cal step enable
    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 Read Diagnostic Configuration 5 work around
/// Not in the Model 67 spydef, so we scom them. Should be removed when they are
/// added to the spydef.
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode rd_dia_config5( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // Config provided by S. Wyatt 8/16
    // 8 corresponds to bit 52 and is a workaround for periodic calibration related fails.
    // 4 corresponds to bit 49 and is a workaround to keep the DQ/DQS always terminated
    // Currently, it looks like these workarounds will be required for all versions of Nimbus, so just hard coding it
    constexpr uint64_t rd_dia_config = 0x4810;

    // Not checking fo EC level as this isn't an EC feature workaround, it's a incorrect documentation workaround.
    static const std::vector<uint64_t> l_addrs =
    {
        MCA_DDRPHY_DP16_RD_DIA_CONFIG5_P0_0,
        MCA_DDRPHY_DP16_RD_DIA_CONFIG5_P0_1,
        MCA_DDRPHY_DP16_RD_DIA_CONFIG5_P0_2,
        MCA_DDRPHY_DP16_RD_DIA_CONFIG5_P0_3,
        MCA_DDRPHY_DP16_RD_DIA_CONFIG5_P0_4,
    };

    FAPI_TRY( mss::scom_blastah(i_target, l_addrs, rd_dia_config) );

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 DQSCLK Offset work around
/// Not in the Model 67 spydef, so we scom them. Should be removed when they are
/// added to the spydef.
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode dqsclk_offset( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // Config provided by S. Wyatt 9/13
    constexpr uint64_t offset_val = 0x0800;

    // Not checking fo EC level as this isn't an EC feature workaround, it's a incorrect documentation workaround.
    static const std::vector<uint64_t> l_addrs =
    {
        MCA_DDRPHY_DP16_DQSCLK_OFFSET_P0_0,
        MCA_DDRPHY_DP16_DQSCLK_OFFSET_P0_1,
        MCA_DDRPHY_DP16_DQSCLK_OFFSET_P0_2,
        MCA_DDRPHY_DP16_DQSCLK_OFFSET_P0_3,
        MCA_DDRPHY_DP16_DQSCLK_OFFSET_P0_4,
    };

    FAPI_TRY( mss::scom_blastah(i_target, l_addrs, offset_val) );

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 workarounds to be run after PHY reset
/// In DD1.0 Nimbus various work arounds are needed
/// @param[in] i_target the fapi2 target of the controller
/// @return fapi2::ReturnValue FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode after_phy_reset( const fapi2::Target<fapi2::TARGET_TYPE_MCBIST>& i_target )
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    uint8_t l_sim = 0;
    FAPI_TRY( mss::is_simulation(l_sim) );

    // This is a collection or workarounds called after phy reset is called. Each individual workaround
    // below does its own checking for applicable feature/ec levels.
    for (const auto& p : mss::find_targets<fapi2::TARGET_TYPE_MCA>(i_target))
    {
        std::vector< fapi2::buffer<uint64_t> > l_vref_cntl;

        // Fix up vref dac
        if (!l_sim)
        {
            const auto& RD_VREF_CNTRL_REG = mss::chip_ec_nimbus_lt_2_0(i_target) ? TT::DD1_RD_VREF_CNTRL_REG :
                                            TT::DD2_RD_VREF_CNTRL_REG;
            FAPI_TRY( mss::scom_suckah(p, RD_VREF_CNTRL_REG, l_vref_cntl) );
            std::for_each(l_vref_cntl.begin(), l_vref_cntl.end(),
                          [&p](fapi2::buffer<uint64_t>& b)
            {
                // Checks for EC level
                b  = mss::workarounds::dp16::vref_dac(p, b);
            });
            FAPI_TRY( mss::scom_blastah(p, RD_VREF_CNTRL_REG, l_vref_cntl) );
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Fixes red waterfall values in a port
/// @param[in] i_target - the target to operate on
/// @param[in] i_rp - the rank pair to change
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode fix_red_waterfall_gate( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target, const uint64_t i_rp)
{
    // Checks if the workaround needs to be run
    const bool l_attr_value = mss::chip_ec_feature_red_waterfall_adjust(i_target);

    if(!l_attr_value)
    {
        FAPI_DBG("Skipping running fix_red_waterfall_gate %s", mss::c_str(i_target) );
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Loops through the first 4 DP's as the last DP is a DP08 so we only need to do 2 quads
    const std::vector<std::vector<uint64_t>> l_dp16_registers =
    {
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_3},
        }
    };
    const std::vector<uint64_t> l_dp08_registers =
    {
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_4},
    };

    // Gets the number of primary ranks to loop through
    std::vector<uint64_t> l_rank_pairs;
    FAPI_TRY(mss::rank::get_rank_pairs(i_target, l_rank_pairs));

    FAPI_INF("Changing red waterfalls for %s", mss::c_str(i_target));

    // Loops through all DP16s
    for(const auto& l_reg : l_dp16_registers[i_rp])
    {
        fapi2::buffer<uint64_t> l_waterfall;

        // Getscoms
        FAPI_TRY(mss::getScom(i_target, l_reg, l_waterfall), "%s Failed to getScom from 0x%016lx",
                 mss::c_str(i_target), l_reg);

        // Updates the data for all quads
        update_red_waterfall_for_quad<0>(l_waterfall);
        update_red_waterfall_for_quad<1>(l_waterfall);
        update_red_waterfall_for_quad<2>(l_waterfall);
        update_red_waterfall_for_quad<3>(l_waterfall);

        // Putscoms
        FAPI_TRY(mss::putScom(i_target, l_reg, l_waterfall), "%s Failed to putScom to 0x%016lx",
                 mss::c_str(i_target), l_reg);
    }

    // Now for the odd man out - the DP08, as it only has two quads.
    // Note: We are not modifying the non-existant quads, as changing values in the non-existant DP08 has caused FIRs
    {
        fapi2::buffer<uint64_t> l_waterfall;

        // Getscoms
        FAPI_TRY(mss::getScom(i_target, l_dp08_registers[i_rp], l_waterfall), "%s Failed to getScom from 0x%016lx",
                 mss::c_str(i_target), l_dp08_registers[i_rp]);

        // Updates the data for all quads
        update_red_waterfall_for_quad<0>(l_waterfall);
        update_red_waterfall_for_quad<1>(l_waterfall);

        // Putscoms
        FAPI_TRY(mss::putScom(i_target, l_dp08_registers[i_rp], l_waterfall), "%s Failed to putScom to 0x%016lx",
                 mss::c_str(i_target), l_dp08_registers[i_rp]);
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Fixes blue waterfall values in a port
/// @param[in] i_target - the target to operate on
/// @param[in] i_always_run - ignores the attribute and always run - default = false
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode fix_blue_waterfall_gate( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const bool i_always_run )
{
    // Checks if the workaround needs to be run
    const bool l_attr_value = mss::chip_ec_feature_blue_waterfall_adjust(i_target);

    if(!l_attr_value && !i_always_run)
    {
        FAPI_DBG("Skipping running fix_blue_waterfall_gate i_always_run %s attr %s", i_always_run ? "true" : "false",
                 l_attr_value ? "true" : "false" );
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Loops through the first 4 DP's as the last DP is a DP08 so we only need to do 2 quads
    // TK update to hit all rank pairs
    const std::vector<std::vector<std::pair<uint64_t, uint64_t>>> l_dp16_registers =
    {
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_0, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP0_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_1, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP0_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_2, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP0_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_3, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP0_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_0, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP1_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_1, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP1_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_2, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP1_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_3, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP1_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_0, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP2_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_1, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP2_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_2, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP2_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_3, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP2_P0_3},
        },
        {
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_0, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP3_P0_0},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_1, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP3_P0_1},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_2, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP3_P0_2},
            {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_3, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP3_P0_3},
        },
    };
    const std::vector<std::pair<uint64_t, uint64_t>> l_dp08_registers =
    {
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR0_P0_4, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP0_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR1_P0_4, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP1_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR2_P0_4, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP2_P0_4},
        {MCA_DDRPHY_DP16_DQS_RD_PHASE_SELECT_RANK_PAIR3_P0_4, MCA_DDRPHY_DP16_DQS_GATE_DELAY_RP3_P0_4},
    };

    // Gets the number of primary ranks to loop through
    std::vector<uint64_t> l_rank_pairs;
    FAPI_TRY(mss::rank::get_rank_pairs(i_target, l_rank_pairs));

    // Loops through all configured rank pairs
    for(const auto& l_rp : l_rank_pairs)
    {
        // Loops through all DP16s
        for(const auto& l_reg : l_dp16_registers[l_rp])
        {
            fapi2::buffer<uint64_t> l_waterfall;
            fapi2::buffer<uint64_t> l_gate_delay;

            // Getscoms
            FAPI_TRY(mss::getScom(i_target, l_reg.first, l_waterfall), "Failed to getScom from 0x%016lx", l_reg.first);
            FAPI_TRY(mss::getScom(i_target, l_reg.second, l_gate_delay), "Failed to getScom from 0x%016lx", l_reg.second);

            // Updates the data for all quads
            update_blue_waterfall_gate_delay_for_quad<0>(l_waterfall, l_gate_delay);
            update_blue_waterfall_gate_delay_for_quad<1>(l_waterfall, l_gate_delay);
            update_blue_waterfall_gate_delay_for_quad<2>(l_waterfall, l_gate_delay);
            update_blue_waterfall_gate_delay_for_quad<3>(l_waterfall, l_gate_delay);

            // Putscoms
            FAPI_TRY(mss::putScom(i_target, l_reg.first, l_waterfall), "Failed to putScom to 0x%016lx", l_reg.first);
            FAPI_TRY(mss::putScom(i_target, l_reg.second, l_gate_delay), "Failed to putScom to 0x%016lx", l_reg.second);
        }

        // Now for the odd man out - the DP08, as it only has two quads.
        // Note: We are not modifying the non-existant quads, as changing values in the non-existant DP08 has caused FIRs
        {
            fapi2::buffer<uint64_t> l_waterfall;
            fapi2::buffer<uint64_t> l_gate_delay;

            // Getscoms
            FAPI_TRY(mss::getScom(i_target, l_dp08_registers[l_rp].first, l_waterfall), "Failed to getScom from 0x%016lx",
                     l_dp08_registers[l_rp].first);
            FAPI_TRY(mss::getScom(i_target, l_dp08_registers[l_rp].second, l_gate_delay), "Failed to getScom from 0x%016lx",
                     l_dp08_registers[l_rp].second);

            // Updates the data for all quads
            update_blue_waterfall_gate_delay_for_quad<0>(l_waterfall, l_gate_delay);
            update_blue_waterfall_gate_delay_for_quad<1>(l_waterfall, l_gate_delay);

            // Putscoms
            FAPI_TRY(mss::putScom(i_target, l_dp08_registers[l_rp].first, l_waterfall), "Failed to putScom to 0x%016lx",
                     l_dp08_registers[l_rp].first);
            FAPI_TRY(mss::putScom(i_target, l_dp08_registers[l_rp].second, l_gate_delay), "Failed to putScom to 0x%016lx",
                     l_dp08_registers[l_rp].second);
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Modifies HW calibration results based upon workarounds
/// @param[in]  i_target - the target to operate on
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode modify_calibration_results( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    return fix_blue_waterfall_gate( i_target );
}

namespace dqs_align
{

///
/// @brief Runs the DQS workaround
/// @param[in] i_target MCA target
/// @param[in] i_rp the rank pair
/// @param[in] i_abort_on_error CAL_ABORT_ON_ERROR override
/// @return FAPI2_RC_SUCCESS if and only if ok
///
fapi2::ReturnCode dqs_align_workaround(const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                       const uint64_t i_rp,
                                       const uint8_t i_abort_on_error)
{
    constexpr uint64_t MAX_LOOPS = 10;

    // Variable declarations
    auto l_skip = mss::states::ON;
    std::map<uint64_t, uint64_t> l_passing_values;
    uint64_t l_num_loops = 0;
    uint8_t l_dram_width[mss::PORTS_PER_MCS] = {};
    bool l_is_x8 = false;

    // Let's check to see if we can run the workaround
    // If we can't, exit with success
    if (! chip_ec_feature_mss_dqs_workaround(i_target) )
    {
        FAPI_DBG("%s Skipping DQS workaround because of ec feature attribute", mss::c_str(i_target));
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Boolean to keep track of if a fail was calibration related, or scom related
    bool l_cal_fail = false;

    // Create a DQS align calibration step, as it might need to be used in the loop below
    const auto l_dqs_align_step = mss::training::dqs_align();

    FAPI_TRY( eff_dram_width( i_target, l_dram_width) );

    l_is_x8 = ((l_dram_width[0] == fapi2::ENUM_ATTR_EFF_DRAM_WIDTH_X8) ||
               (l_dram_width[1] == fapi2::ENUM_ATTR_EFF_DRAM_WIDTH_X8) );

    // First check if the workaround is needed - we could have fails, just not due to DQS align
    FAPI_TRY(mss::workarounds::dp16::dqs_align::check_workaround(i_target, i_rp, l_skip));

    FAPI_INF("%s i_rp %lu %s the DQS align workaround", mss::c_str(i_target), i_rp,
             l_skip == mss::states::OFF ? "running" : "skipping");

    // Skip the workaround
    if(l_skip == mss::states::ON)
    {
        // Clear the disable registers so we can keep running calibration on other ranks
        FAPI_TRY(mss::workarounds::dp16::dqs_align::reset_disables(i_target, i_rp));
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Now the fun begins....
    // Note: adding this call outside the loop to avoid hitting calibration an extra time in case we're fully passing
    FAPI_TRY(mss::workarounds::dp16::dqs_align::record_passing_values(i_target, i_rp, l_passing_values));

    // Loop until we pass or timeout
    while(!mss::workarounds::dp16::dqs_align::check_completed(l_passing_values, l_is_x8 ? MAX_DRAMS_X8 : MAX_DRAMS_X4)
          && l_num_loops < MAX_LOOPS)
    {
        FAPI_INF("%s i_rp %lu starting DQS align workaround loop number %lu", mss::c_str(i_target), i_rp, l_num_loops);

        // I'm making the bold(?) assumption that whenever we fail, disable bits get set. If that's not the case, this workaround will break - SPG

        // Clear all disable bits - this will cause calibration to re-run everything that failed, including WR LVL fails
        FAPI_TRY(mss::workarounds::dp16::dqs_align::reset_disables(i_target, i_rp));

        // Hit calibration one more time
        FAPI_TRY(l_dqs_align_step.execute(i_target, i_rp, i_abort_on_error));

        // Get the current passing states
        // We override any states that were passing previously and are still passing
        // We really just want to add on new passing values
        FAPI_TRY(mss::workarounds::dp16::dqs_align::record_passing_values(i_target, i_rp, l_passing_values));

        ++l_num_loops;
    }

    // Clear all disable bits - this will cause calibration to re-run everything that failed, including WR LVL fails
    FAPI_TRY(mss::workarounds::dp16::dqs_align::reset_disables(i_target, i_rp));

    // Next, we're checking for CAL fails, so make sure to check the FIR's below
    l_cal_fail = true;

    // If the loop timed out, bomb out
    // If this is firmware, they'll log it as info and run to memdiags
    // If we're cronus, we're bombing out
    // Either way, let's do ffdc and collect the regs
    FAPI_ASSERT( l_num_loops < MAX_LOOPS,
                 fapi2::MSS_DRAMINIT_TRAINING_DQS_ALIGNMENT_WORKAROUND_FAILED()
                 .set_NUM_LOOPS(l_num_loops)
                 .set_RP(i_rp)
                 .set_ABORT_ON_ERROR(i_abort_on_error)
                 .set_MCA_TARGET(i_target),
                 "%s i_rp %lu DQS workaround failed! 10 loops reached without everything passing",
                 mss::c_str(i_target), i_rp);

    // Below, the errors are scom related, no need to check the FIR's
    l_cal_fail = false;

    // Now plop the delays back in to the registers
    FAPI_TRY(mss::workarounds::dp16::dqs_align::set_passing_values( i_target, i_rp, l_passing_values));

fapi_try_exit:

    // If the FIR's are cal fails, then check to see if FIR's or PLL's could be the cause
    return mss::check::fir_or_pll_fail(i_target, fapi2::current_err, l_cal_fail);
}

///
/// @brief Checks if the DQS align workaround needs to be run
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rp the rank pair to check
/// @param[out] o_skip_workaround - YES if cal should be skipped
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note the workaround needs to be run IFF we failed DQS align
///
fapi2::ReturnCode check_workaround( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                    const uint64_t i_rp,
                                    mss::states& o_skip_workaround )
{
    // bit location of the two DQS error bits in the dp16 error register
    // Looking at NO_DQS and NO_LOCK which comes right after NO_DQS
    constexpr uint64_t NO_DQS = MCA_DDRPHY_DP16_RD_STATUS0_P0_0_01_NO_DQS;
    constexpr uint64_t NO_LOCK = MCA_DDRPHY_DP16_RD_STATUS0_P0_0_01_NO_LOCK;

    static const std::vector<uint64_t> RD_STATUS0 =
    {
        MCA_DDRPHY_DP16_RD_STATUS0_P0_0,
        MCA_DDRPHY_DP16_RD_STATUS0_P0_1,
        MCA_DDRPHY_DP16_RD_STATUS0_P0_2,
        MCA_DDRPHY_DP16_RD_STATUS0_P0_3,
        MCA_DDRPHY_DP16_RD_STATUS0_P0_4,
    };

    // Default to skip
    o_skip_workaround = mss::states::ON;
    std::vector<fapi2::buffer<uint64_t>> l_data;

    // Gets the reg
    FAPI_TRY(mss::scom_suckah(i_target, RD_STATUS0, l_data), "%s failed to read RD_STATUS0 regs", mss::c_str(i_target));

    // See if either of the the error bits signaling a DQS fail were triggered
    for(const auto& l_val : l_data)
    {
        if(l_val.getBit<NO_DQS>() || l_val.getBit<NO_LOCK>())
        {
            o_skip_workaround = mss::states::OFF;
            break;
        }
    }

    FAPI_INF("%s the DQS workaround will be %s", mss::c_str(i_target),
             o_skip_workaround == mss::states::OFF ? "run" : "skipped");

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Resets bad DQ/DQS bits
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rp - the rank pair to operate on
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode reset_disables( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target, const uint64_t i_rp )
{
    // Traits declaration
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;
    constexpr uint64_t RESET_VAL = 0;

    return mss::scom_blastah(i_target, TT::BIT_DISABLE_REG[i_rp], RESET_VAL);
}

///
/// @brief Sets the passing values from the map into the registers
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rp - the rank pair to operate on
/// @param[in] i_passing_values - the passing values, a map from the DQS number to the value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode set_passing_values( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target, const uint64_t i_rp,
                                      std::map<uint64_t, uint64_t>& i_passing_values)
{
    // Declares constexprs to beautify the code
    constexpr uint64_t NUM_BYTES = MAX_DQ_BITS / BITS_PER_BYTE;
    constexpr uint64_t EVEN_START = MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_0_01_ROT_CLK_N0;
    constexpr uint64_t ODD_START = MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_0_01_ROT_CLK_N1;
    constexpr uint64_t LEN = MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_0_01_ROT_CLK_N0_LEN;

    // Registers in terms of RP, then 0/1
    static const std::vector<std::vector<uint64_t>> RD_CLK_REGS
    {
        // RANK_PAIR0
        {
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_4,
        },
        // RANK_PAIR1
        {
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_4,
        },
        // RANK_PAIR2
        {
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_4,
        },
        // RANK_PAIR3
        {
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_4,
        },
    };

    // Loop through all of the DP values
    for(uint64_t l_byte = 0; l_byte < NUM_BYTES; ++l_byte)
    {
        const auto l_nibble = l_byte * NIBBLES_PER_BYTE;
        const auto l_even = l_nibble;
        const auto l_odd = l_nibble + 1;

        // Sets up the data to plop into the register
        fapi2::buffer<uint64_t> l_data;
        const auto l_even_val = i_passing_values[l_even];
        const auto l_odd_val = i_passing_values[l_odd];

        // TK should we add get/insert into the API? we can then just call that function
        l_data.insertFromRight<EVEN_START, LEN>(l_even_val);
        l_data.insertFromRight<ODD_START, LEN>(l_odd_val);

        FAPI_INF("%s setting good values for i_rp %lu  l_byte %lu to register 0x%016lx", mss::c_str(i_target), i_rp, l_byte,
                 RD_CLK_REGS[i_rp][l_byte]);

        // Scoms that reg
        FAPI_TRY(mss::putScom(i_target, RD_CLK_REGS[i_rp][l_byte], l_data), "%s failed getscom 0x%016lx", mss::c_str(i_target),
                 RD_CLK_REGS[i_rp][l_byte]);
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Records the passing values into a map
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rp - the rank pair to operate on
/// @param[in,out] io_passing_values - the passing values, a map from the DQS number to the value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode record_passing_values( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        std::map<uint64_t, uint64_t>& io_passing_values)
{
    // Traits declaration
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    constexpr uint64_t MAX_DP = 4;

    // Registers in terms of RP, then 0/1
    static const std::vector<std::vector<std::pair<uint64_t, uint64_t>>> RD_CLK_REGS
    {
        // RANK_PAIR0
        {
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_0,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_1,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_2,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_3,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR0_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR0_P0_4,},
        },
        // RANK_PAIR1
        {
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_0,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_1,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_2,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_3,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR1_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR1_P0_4,},
        },
        // RANK_PAIR2
        {
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_0,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_1,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_2,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_3,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR2_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR2_P0_4,},
        },
        // RANK_PAIR3
        {
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_0, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_0,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_1, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_1,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_2, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_2,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_3, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_3,},
            {MCA_DDRPHY_DP16_DQSCLK_PR0_RANK_PAIR3_P0_4, MCA_DDRPHY_DP16_DQSCLK_PR1_RANK_PAIR3_P0_4,},
        },
    };

    // Gets out the values for DQS CLK and the disable bits
    std::vector<std::pair<fapi2::buffer<uint64_t>, fapi2::buffer<uint64_t>>> l_rd_clk_values;
    std::vector<std::pair<fapi2::buffer<uint64_t>, fapi2::buffer<uint64_t>>> l_disable_bits;

    FAPI_TRY(mss::scom_suckah(i_target, RD_CLK_REGS[i_rp], l_rd_clk_values));
    FAPI_TRY(mss::scom_suckah(i_target, TT::BIT_DISABLE_REG[i_rp], l_disable_bits));

    FAPI_ASSERT( l_disable_bits.size() == l_rd_clk_values.size(),
                 fapi2::MSS_RDCLK_ALIGN_VECTOR_MISMATCH()
                 .set_BITVECTOR_SIZE(l_disable_bits.size())
                 .set_RDCLK_SIZE(l_rd_clk_values.size()),
                 "%s disable bit vector size %lu is not the same as rd clk vector size %lu", mss::c_str(i_target),
                 l_disable_bits.size(), l_rd_clk_values.size());

    // Now, loops through both vectors and checks the errors
    {
        auto l_rd_it = l_rd_clk_values.begin();
        auto l_disable_it = l_disable_bits.begin();
        uint64_t l_dp = 0;

        FAPI_ASSERT( l_dp <= MAX_DP,
                     fapi2::MSS_EXCEED_NUMBER_OF_DP()
                     .set_BAD_DP_NUM(l_dp)
                     .set_MAX_DP(MAX_DP)
                     .set_TARGET(i_target),
                     "%s error looping over DP's, found dp %d, max is %d?",
                     mss::c_str(i_target),
                     l_dp,
                     MAX_DP);

        // rd_clk_values == disabled_bits, asserted above
        for(; l_rd_it < l_rd_clk_values.end(); ++ l_rd_it, ++l_dp, ++l_disable_it)
        {
            FAPI_INF("%s i_rp %lu checking on DP%lu for good values", mss::c_str(i_target), i_rp, l_dp);

            FAPI_TRY(record_passing_values_per_dqs<0>(i_target,
                     i_rp,
                     l_dp,
                     *l_disable_it,
                     *l_rd_it,
                     io_passing_values));

            FAPI_TRY(record_passing_values_per_dqs<1>(i_target,
                     i_rp,
                     l_dp,
                     *l_disable_it,
                     *l_rd_it,
                     io_passing_values));

            FAPI_TRY(record_passing_values_per_dqs<2>(i_target,
                     i_rp,
                     l_dp,
                     *l_disable_it,
                     *l_rd_it,
                     io_passing_values));

            FAPI_TRY(record_passing_values_per_dqs<3>(i_target,
                     i_rp,
                     l_dp,
                     *l_disable_it,
                     *l_rd_it,
                     io_passing_values));
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

} // close namespace dqs_align

namespace rd_dq
{

///
/// @brief Reads out the read dq registers and stores the values in a vector
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rank_pair the rank pair to operate on
/// @param[out] o_reg_data register conglomerate
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode get_delay_data(const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                 const uint64_t i_rank_pair,
                                 std::vector<delay_data>& o_reg_data)
{
    static const std::vector<std::vector<uint64_t>> REGISTER =
    {
        // RANK_PAIR0
        {
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR0_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR0_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR0_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR0_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR0_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR0_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR0_P0_4,
        },
        // RANK_PAIR1
        {
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR1_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR1_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR1_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR1_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR1_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR1_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR1_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR1_P0_4,
        },
        // RANK_PAIR2
        {
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR2_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR2_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR2_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR2_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR2_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR2_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR2_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR2_P0_4,
        },
        // RANK_PAIR3
        {
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR3_P0_0,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR3_P0_1,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR3_P0_2,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY4_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY5_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY6_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY7_RANK_PAIR3_P0_3,
            MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR3_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY1_RANK_PAIR3_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY2_RANK_PAIR3_P0_4,
            MCA_DDRPHY_DP16_READ_DELAY3_RANK_PAIR3_P0_4,
        },
    };
    // Bombs out if the rank pair is out of range

    FAPI_ASSERT( i_rank_pair < MAX_RANK_PAIRS,
                 fapi2::MSS_INVALID_RANK_PAIR()
                 .set_RANK_PAIR(i_rank_pair)
                 .set_MCA_TARGET(i_target)
                 .set_FUNCTION(RD_CTR_WORKAROUND_READ_DATA),
                 "%s Invalid rank pair (%d) in get_ranks_in_pair",
                 mss::c_str(i_target),
                 i_rank_pair);

    // loops and gets the registers
    o_reg_data.clear();

    for(const auto l_reg : REGISTER[i_rank_pair])
    {
        constexpr uint64_t EVEN_START = MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_0_01_RD;
        constexpr uint64_t ODD_START = MCA_DDRPHY_DP16_READ_DELAY0_RANK_PAIR0_P0_0_01_RD_DELAY1;
        fapi2::buffer<uint64_t> l_buff;
        uint64_t l_data = 0;

        // Reads the register information
        FAPI_TRY(mss::getScom(i_target, l_reg, l_buff), "%s failed getscom to register 0x%016lx", mss::c_str(i_target), l_reg);

        // Gets out the specific information and stores it
        // Even delay
        l_buff.extractToRight<EVEN_START, delay_data::LEN>(l_data);
        o_reg_data.push_back({l_reg, EVEN_START, l_data});

        // Odd delay
        l_buff.extractToRight<ODD_START, delay_data::LEN>(l_data);
        o_reg_data.push_back({l_reg, ODD_START, l_data});
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Finds the median and sorts the vector
/// @param[in,out] io_reg_data register data
/// @param[out] o_median the median value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode find_median_and_sort(std::vector<delay_data>& io_reg_data, uint64_t& o_median)
{

    // The fapi_try is in an if statement, this ensures we have a good value
    fapi2::current_err = fapi2::FAPI2_RC_SUCCESS;

    // Note: the null constructor is deleted
    // We need to return the data value, so we're creating a temporary variable with 0's for it's values to get the median positon
    // Below, we'll get the data and return it to o_median
    delay_data l_temp(0, 0, 0);

    // Sorts first
    std::sort(io_reg_data.begin(), io_reg_data.end());

    // Returns the median value
    FAPI_TRY(find_median(io_reg_data, l_temp));
    o_median = l_temp.iv_data;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Overrides any bad (out of range) read delay values with the median value
/// @param[in] i_target the fapi2 target of the port
/// @param[in] i_rank_pair the rank pair to operate on
/// @param[in] i_percent the percentage below the median outside of which to override values to be the median - OPTIONAL - defaults to 66
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode fix_delay_values(const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                   const uint64_t i_rank_pair,
                                   const uint64_t i_percent)
{
    if(!mss::chip_ec_feature_mss_run_rd_ctr_workaround(i_target))
    {
        FAPI_INF("%s skipping the rd centering workaround!", mss::c_str(i_target));
        return fapi2::FAPI2_RC_SUCCESS;
    }

    std::vector<delay_data> l_reg_data;
    uint64_t l_median = 0;

    FAPI_TRY(get_delay_data(i_target, i_rank_pair, l_reg_data), "%s i_rank_pair %d failed to get the delay data",
             mss::c_str(i_target), i_rank_pair);

    FAPI_TRY(find_median_and_sort(l_reg_data, l_median), "%s i_rank_pair %d failed to find median value and sort",
             mss::c_str(i_target), i_rank_pair);

    FAPI_INF("%s i_rank_pair %d found a median of 0x%02x any value below %d %% of the median will be modified",
             mss::c_str(i_target), i_rank_pair, l_median, i_percent);

    // Overrides the bad delay values - the ones that are i_percent below the median
    {
        // Finds the iterator corresponding to the first location where we are within i_percent of the median
        const auto l_last = std::find_if(l_reg_data.begin(), l_reg_data.end(), [ i_percent,
                                         l_median]( const delay_data & i_rhs) -> bool
        {
            // Does this funky compare to avoid underflow
            const auto l_comp_value = (i_percent * l_median) / 100;
            return l_comp_value < i_rhs.iv_data;
        });

        // Loops and does the overrides
        // Note: does a simple RMW, we can speed this up by doing a more complex algorithm to sort registers together.
        // Keeping it simple for now
        for( auto l_it = l_reg_data.begin(); l_it < l_last; ++l_it)
        {
            fapi2::buffer<uint64_t> l_buff;
            FAPI_INF("%s i_rank_pair %d modifying delay position %d on register 0x%016lx from 0x%02x to be 0x%02x",
                     mss::c_str(i_target), i_rank_pair, l_it->iv_bit, l_it->iv_register, l_it->iv_data, l_median);

            // Read
            FAPI_TRY(mss::getScom(i_target, l_it->iv_register, l_buff));

            // Modify
            FAPI_TRY(l_buff.insertFromRight(l_median, l_it->iv_bit, delay_data::LEN));

            // Write
            FAPI_TRY(mss::putScom(i_target, l_it->iv_register, l_buff));
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

} // close namespace rd_dq

namespace wr_vref
{

///
/// @brief DP16 WR VREF error latching workaround
/// In DD1 Nimbus in the WR VREF algorithm, DRAM's 2/3 latch over error information from DRAM's 0/1.
/// The workaround is to set the error mask for DRAM's 2/3 to be 0xFFFF (informational but not errors)
/// @param[in] i_target the fapi2 target of the port
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode error_dram23( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    // constants to pretty up code
    constexpr uint64_t DP0 = 0;
    constexpr uint64_t DP1 = 1;
    constexpr uint64_t DP2 = 2;
    constexpr uint64_t DP3 = 3;
    constexpr uint64_t DP4 = 4;
    constexpr uint64_t DRAM23 = 2;
    // disable errors in the reg
    constexpr uint64_t DISABLE_ERRORS = 0xffff;

    // EC level taken care of in the caller

    // note DRAM's 2/3 use the same scom register, so only one scom is needed per DP
    // extra paretheses keep FAPI_TRY happy for two parameter template functions
    FAPI_TRY((mss::dp16::write_wr_vref_error_mask<DP0, DRAM23>(i_target, DISABLE_ERRORS)));
    FAPI_TRY((mss::dp16::write_wr_vref_error_mask<DP1, DRAM23>(i_target, DISABLE_ERRORS)));
    FAPI_TRY((mss::dp16::write_wr_vref_error_mask<DP2, DRAM23>(i_target, DISABLE_ERRORS)));
    FAPI_TRY((mss::dp16::write_wr_vref_error_mask<DP3, DRAM23>(i_target, DISABLE_ERRORS)));
    FAPI_TRY((mss::dp16::write_wr_vref_error_mask<DP4, DRAM23>(i_target, DISABLE_ERRORS)));

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 big step/small step check and modify workaround - MCA specialization
/// In DD1 Nimbus in the WR VREF algorithm, the out of bounds checks are broken.
/// One aspect of fixing this bug is to ensure that the big step is divisible by the small step
/// This function converts the small step value over to the closest allowable value as to what was entered
/// @param[in] i_big_step - WR VREF big step value
/// @param[in,out] io_small_step - WR VREF small step value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
template<  >
fapi2::ReturnCode modify_small_step_for_big_step<fapi2::TARGET_TYPE_MCA>(const uint8_t i_big_step,
        uint8_t& io_small_step)
{
    // EC level taken care of in the caller

    // Step size constants
    constexpr uint8_t MAX_BIG_STEP = 0x10;
    constexpr uint8_t MAX_SMALL_STEP = 0x08;

    // Conversion over to the nearest allowable small step
    // The algorithm takes the register value + 1 when it runs.
    // This avoids cases where it would infinite loop if it had a 0 value in the register
    // As such, the math to figure out if a big/small step combination is allowed is ((big_step + 1) %
    // (small_step + 1)) == 0
    // This below chart takes the allowable combinations or the next smallest allowable value.
    constexpr uint8_t SMALL_STEP_CONVERSION[MAX_BIG_STEP][MAX_SMALL_STEP] =
    {
        {0, 0, 0, 0, 0, 0, 0, 0,},
        {0, 1, 1, 1, 1, 1, 1, 1,},
        {0, 0, 2, 2, 2, 2, 2, 2,},
        {0, 1, 1, 3, 3, 3, 3, 3,},
        {0, 0, 0, 0, 4, 4, 4, 4,},
        {0, 1, 2, 2, 2, 5, 5, 5,},
        {0, 0, 0, 0, 0, 0, 6, 6,},
        {0, 1, 1, 3, 3, 3, 3, 7,},
        {0, 0, 2, 2, 2, 2, 2, 2,},
        {0, 1, 1, 1, 4, 4, 4, 4,},
        {0, 0, 0, 0, 0, 0, 0, 0,},
        {0, 1, 2, 3, 3, 5, 5, 5,},
        {0, 0, 0, 0, 0, 0, 0, 0,},
        {0, 1, 1, 1, 1, 1, 6, 6,},
        {0, 0, 2, 2, 4, 4, 4, 4,},
        {0, 1, 1, 3, 3, 3, 3, 7,},
    };

    // Makes sure that the values passed in were not out of range
    FAPI_ASSERT( MAX_BIG_STEP > i_big_step,
                 fapi2::MSS_WR_VREF_WORKAROUND_BIG_STEPS_OUTOFBOUNDS()
                 .set_MAX_BIG_STEP(MAX_BIG_STEP)
                 .set_ACTUAL_BIG_STEP(i_big_step),
                 "WR VREF %s step is out of range. %s step 0x%02x max", "big", "big",
                 i_big_step);
    FAPI_ASSERT( MAX_SMALL_STEP > io_small_step,
                 fapi2::MSS_WR_VREF_WORKAROUND_SMALL_STEPS_OUTOFBOUNDS()
                 .set_MAX_SMALL_STEP(MAX_SMALL_STEP)
                 .set_ACTUAL_SMALL_STEP(io_small_step),
                 "WR VREF %s step is out of range. %s step 0x%02x max", "big", "big",
                 i_big_step);

    // Converts the value
    io_small_step = SMALL_STEP_CONVERSION[i_big_step][io_small_step];

    return fapi2::FAPI2_RC_SUCCESS;
fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 writes config 0 overriding bad small step/big step values
/// In DD1 Nimbus in the WR VREF algorithm, the out of bounds checks are broken.
/// One aspect of fixing this bug is to ensure that the big step is divisible by the small step
/// This function converts the small step value over to the closest allowable value as to what was entered
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[out] o_big_step - WR VREF big step value
/// @param[out] o_small_step - WR VREF small step value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode write_config0( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target, uint8_t& o_big_step,
                                 uint8_t& o_small_step)
{
    // EC level taken care of in the caller

    // Traits declaration
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    // Variable declaration
    fapi2::buffer<uint64_t> l_data;

    // Reads the register - note assumes ALL DP's have the same value. Will overwrite different values
    FAPI_TRY(mss::dp16::read_wr_vref_config0<0>(i_target, l_data));

    // Sets these values to 0's to avoid undesired values
    o_small_step = 0;
    o_big_step = 0;

    // Gets out the current values of the big and small step
    l_data.extractToRight<TT::WR_VREF_CONFIG0_2D_SMALL_STEP_VAL, TT::WR_VREF_CONFIG0_2D_SMALL_STEP_VAL_LEN>(o_small_step)
    .extractToRight<TT::WR_VREF_CONFIG0_2D_BIG_STEP_VAL, TT::WR_VREF_CONFIG0_2D_BIG_STEP_VAL_LEN>(o_big_step);

    // Does the workaround conversion
    FAPI_TRY(modify_small_step_for_big_step<fapi2::TARGET_TYPE_MCA>(o_big_step, o_small_step));

    // Sets up the good values
    l_data.insertFromRight<TT::WR_VREF_CONFIG0_2D_SMALL_STEP_VAL, TT::WR_VREF_CONFIG0_2D_SMALL_STEP_VAL_LEN>(o_small_step)
    .insertFromRight<TT::WR_VREF_CONFIG0_2D_BIG_STEP_VAL, TT::WR_VREF_CONFIG0_2D_BIG_STEP_VAL_LEN>(o_big_step);

    // Blasts em out
    FAPI_TRY(scom_blastah(i_target, TT::WR_VREF_CONFIG0_REG, l_data));

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 converts the VREF training values to start calibration
/// In DD1 Nimbus in the WR VREF algorithm, the out of bounds checks are broken.
/// One aspect of fixing this bug is to ensure the WR VREF is an integer number of big steps away from the 0 value and one big step from the top of the range
/// This function converts values over to the required rules
/// @tparam T fapi2 Target Type - just here to ensure that this won't be called on a non-Nimbus system
/// @param[in] i_big_step - WR VREF big step value
/// @param[in,out] io_train_range - VREF train range converted value
/// @param[in,out] io_train_value - VREF train value converted value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
template <>
fapi2::ReturnCode convert_train_values<fapi2::TARGET_TYPE_MCA>( const uint8_t i_big_step,
        uint8_t& io_train_range,
        uint8_t& io_train_value)
{
    // EC level taken care of in the caller

    // Number of big steps
    constexpr uint8_t NUM_BIG_STEP = 16;

    // Value at which Range 2 equals Range 1's 0 value
    constexpr uint8_t CROSSOVER_RANGE = 0b011000;

    // List of allowable maximum ranges.  The math is max = (absolute_max - (big_step_val + 1)) -
    // (absolute_max - (big_step_val + 1)) % (big_step_val + 1)
    constexpr uint8_t l_max_allowable_values[NUM_BIG_STEP] =
    {
        73, 72, 69, 68,
        65, 66, 63, 64,
        63, 60, 55, 60,
        52, 56, 45, 48,
    };

    // Declares temporary variable
    uint8_t l_continuous_range = 0;
    const uint8_t l_big_step_alg = i_big_step + 1;

    // Errors out if the big step is out of range

    // Makes sure that the values passed in were not out of range
    FAPI_ASSERT( NUM_BIG_STEP > i_big_step,
                 fapi2::MSS_WR_VREF_TRAIN_WORKAROUND_BIG_STEPS_OUTOFBOUNDS()
                 .set_MAX_BIG_STEP(NUM_BIG_STEP)
                 .set_ACTUAL_BIG_STEP(i_big_step),
                 "Big step of %d passed in. Max allowable value is %d", i_big_step, NUM_BIG_STEP - 1);

    // Converts the values over to the algorithm's continuous range
    l_continuous_range = io_train_range == fapi2::ENUM_ATTR_EFF_VREF_DQ_TRAIN_RANGE_RANGE1 ?
                         (io_train_value + CROSSOVER_RANGE) : (io_train_value);

    // Does the math for what makes a good continuous range
    // At least 1 big step away from the top of the range
    l_continuous_range = std::min(l_continuous_range, l_max_allowable_values[i_big_step]);

    // At least 1 big step away from the bottom of the range
    l_continuous_range = std::max(l_continuous_range, l_big_step_alg);

    // Moves to be an even number of big steps away
    l_continuous_range -= (l_continuous_range % l_big_step_alg);

    // Now converts the range/value from the continuous range - defaults to range 1
    // If above the crossover range, use range1. If not, use range 2.
    io_train_range = (l_continuous_range >= CROSSOVER_RANGE) ? fapi2::ENUM_ATTR_EFF_VREF_DQ_TRAIN_RANGE_RANGE1 :
                     fapi2::ENUM_ATTR_EFF_VREF_DQ_TRAIN_RANGE_RANGE2;
    // If above the crossover range, convert to range 1. If not use range 2
    io_train_value = (l_continuous_range >= CROSSOVER_RANGE) ? (l_continuous_range - CROSSOVER_RANGE) :
                     (l_continuous_range);

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 gets the VREF training values to start calibration
/// In DD1 Nimbus in the WR VREF algorithm, the out of bounds checks are broken.
/// One aspect of fixing this bug is to ensure that the big step is divisible by the small step
/// This function converts the small step value over to the closest allowable value as to what was entered
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_big_step - WR VREF big step value
/// @param[out] o_train_range - JEDEC MR6 WR VREF training range
/// @param[out] o_train_value - JEDEC MR6 WR VREF training value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode get_train_values( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                    const uint64_t i_rp,
                                    const uint8_t i_big_step,
                                    uint8_t& o_train_range,
                                    uint8_t& o_train_value)
{
    // EC level taken care of in the caller

    // Declares variables
    std::vector<uint64_t> l_ranks;
    uint64_t l_rank = 0;
    uint8_t l_vrefdq_train_value[MAX_DIMM_PER_PORT][MAX_RANK_PER_DIMM] = {0};
    uint8_t l_vrefdq_train_range[MAX_DIMM_PER_PORT][MAX_RANK_PER_DIMM] = {0};

    // Gets the ranks on which to latch the VREF's
    FAPI_TRY(mss::rank::get_ranks_in_pair( i_target, i_rp, l_ranks));

    // Only uses the first rank in the rank pair, as there is only one set of registers per rank pair
    // If no ranks are configured, exit
    l_rank = l_ranks[0];

    if(l_rank == NO_RANK)
    {
        return fapi2::FAPI2_RC_SUCCESS;
    }

    // Now gets the train value and range for the given rank
    FAPI_TRY( mss::eff_vref_dq_train_value(i_target, &(l_vrefdq_train_value[0][0])) );
    FAPI_TRY( mss::eff_vref_dq_train_range(i_target, &(l_vrefdq_train_range[0][0])) );

    o_train_range = l_vrefdq_train_range[l_rank / MAX_RANK_PER_DIMM][l_rank % MAX_RANK_PER_DIMM];
    o_train_value = l_vrefdq_train_value[l_rank / MAX_RANK_PER_DIMM][l_rank % MAX_RANK_PER_DIMM];

    // Converts over the values
    FAPI_TRY(convert_train_values<fapi2::TARGET_TYPE_MCA>(i_big_step, o_train_range, o_train_value));

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief DP16 sets up the VREF train range and value
/// In DD1 Nimbus in the WR VREF algorithm, the out of bounds checks are broken.
/// One aspect of fixing this bug is to ensure the WR VREF is an integer number of big steps away from the 0 value and one big step from the top of the range
/// This function converts the WR VREF values over to be good values
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_rp - rank pair to check and modify
/// @param[out] o_train_range - JEDEC MR6 WR VREF training range
/// @param[out] o_train_value - JEDEC MR6 WR VREF training value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode setup_values( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                const uint64_t i_rp,
                                uint8_t& o_train_range,
                                uint8_t& o_train_value)
{
    // EC level taken care of in the caller

    // Traits declaration
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    // Declares variables
    uint8_t l_big_step = 0;
    uint8_t l_small_step = 0;

    // Sets up the wr_vref_config0 register and returns out the big/small step values
    FAPI_TRY(write_config0(i_target, l_big_step, l_small_step));

    // Gets the train values
    FAPI_TRY(get_train_values(i_target, i_rp, l_big_step, o_train_range, o_train_value));

    // Sets up the WR VREF values in the RP register
    {
        // Creates and sets up the buffer
        fapi2::buffer<uint64_t> l_buff;

        l_buff.writeBit<TT::WR_VREF_VALUE_RANGE_DRAM_EVEN>(o_train_range)
        .writeBit<TT::WR_VREF_VALUE_RANGE_DRAM_ODD>(o_train_range)
        .insertFromRight<TT::WR_VREF_VALUE_VALUE_DRAM_EVEN, TT::WR_VREF_VALUE_VALUE_DRAM_EVEN_LEN>(o_train_value)
        .insertFromRight<TT::WR_VREF_VALUE_VALUE_DRAM_ODD, TT::WR_VREF_VALUE_VALUE_DRAM_ODD_LEN>(o_train_value);

        // Hits only the desired RP
        switch(i_rp)
        {
            case 0:
                FAPI_TRY(mss::scom_blastah(i_target, TT::WR_VREF_VALUE_RP0_REG, l_buff));
                break;

            case 1:
                FAPI_TRY(mss::scom_blastah(i_target, TT::WR_VREF_VALUE_RP1_REG, l_buff));
                break;

            case 2:
                FAPI_TRY(mss::scom_blastah(i_target, TT::WR_VREF_VALUE_RP2_REG, l_buff));
                break;

            case 3:
                FAPI_TRY(mss::scom_blastah(i_target, TT::WR_VREF_VALUE_RP3_REG, l_buff));
                break;

            // Weird. There shouldn't be more than RP3. Error out
            default:
                FAPI_TRY(fapi2::FAPI2_RC_INVALID_PARAMETER, "%s RP%lu was passed in. Maximum allowable value is 3.",
                         mss::c_str(i_target), i_rp);
                break;
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Gets the disable register and bit position for the DRAM
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_rp - rank pair to check and modify
/// @param[in] i_dram - the DRAM to be checked
/// @param[out] o_reg - the register to access from
/// @param[out] o_bit_pos - the bit position in the register to access from
/// @param[out] o_bit_len - the bit length to access
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode get_disable_reg_and_pos_for_dram( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram,
        uint64_t& o_reg,
        uint64_t& o_bit_pos,
        uint64_t& o_bit_len)
{
    // Gets the DRAM width
    uint8_t l_width[MAX_DIMM_PER_PORT] = {};
    FAPI_TRY( mss::eff_dram_width(i_target, l_width) );

    // Checks inputs
    FAPI_TRY(mss::dp16::wr_vref::check_rp_and_dram(i_target,
             i_rp,
             i_dram,
             ffdc_function_codes::GET_DRAM_DISABLE_REG_AND_POS));

    // Get the register and bit positions
    {
        typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

        // Gets the DP associated with the DRAM and the DRAM's position in the DP
        const auto l_dram_per_dp = (l_width[0] == fapi2::ENUM_ATTR_EFF_DRAM_WIDTH_X8) ? BYTES_PER_DP : NIBBLES_PER_DP;
        const auto l_dp = i_dram / l_dram_per_dp;
        const auto l_dram_pos = i_dram % l_dram_per_dp;

        // Bit position and length
        o_bit_pos = TT::BIT_DISABLE + (l_dram_pos * l_width[0]);
        o_bit_len = l_width[0];

        // Gets the register address to read
        o_reg = TT::BIT_DISABLE_REG[i_rp][l_dp].first;
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Gets the disables for a specific DRAM
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_rp - rank pair to check and modify
/// @param[in] i_dram - the DRAM to be checked
/// @param[out] o_disables - true if the whole DRAM is disabled
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode get_disables_for_dram( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram,
        uint64_t& o_disables)
{
    uint64_t l_reg = 0;
    uint64_t l_bit_pos = 0;
    uint64_t l_len = 0;
    fapi2::buffer<uint64_t> l_data;
    FAPI_TRY(get_disable_reg_and_pos_for_dram(i_target, i_rp, i_dram, l_reg, l_bit_pos, l_len));

    // Reads
    FAPI_TRY( mss::getScom(i_target, l_reg, l_data) );

    // Gets the DRAM's data
    FAPI_TRY(l_data.extractToRight(o_disables, l_bit_pos, l_len));

    FAPI_INF("%s RP%lu DRAM%lu reg:0x%016lx reg_value:0x%016lx DRAM disables: 0x%lx", mss::c_str(i_target), i_rp, i_dram,
             l_reg, l_data, o_disables);

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Identifies if an inputted DRAM is wholely disabled
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_rp - rank pair to check and modify
/// @param[in] i_dram - the DRAM to be checked
/// @param[out] o_disabled - true if the whole DRAM is disabled
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode is_dram_disabled( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                    const uint64_t i_rp,
                                    const uint64_t i_dram,
                                    bool& o_disabled)
{
    // Checks inputs
    uint8_t l_width[MAX_DIMM_PER_PORT] = {};
    uint64_t l_disables = 0;
    FAPI_TRY( mss::eff_dram_width(i_target, l_width) );

    // Gets the disables for this DRAM
    FAPI_TRY(get_disables_for_dram(i_target, i_rp, i_dram, l_disables));

    // A DRAM is disabled if all of it's bits are disabled
    {
        const uint64_t l_disabled_value = (l_width[0] == fapi2::ENUM_ATTR_EFF_DRAM_WIDTH_X8) ? 0xff : 0xf;
        o_disabled = (l_disables == l_disabled_value);
        FAPI_INF("%s RP%lu DRAM%lu DRAM disables: 0x%lx check value: 0x%lx %s disabled", mss::c_str(i_target), i_rp, i_dram,
                 l_disables, l_disabled_value, o_disabled ? "is" : "isn't");
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Identifies if an inputted DRAM has any disables
/// @param[in] i_target the fapi2 target type MCA of the port
/// @param[in] i_rp - rank pair to check and modify
/// @param[in] i_dram - the DRAM to be checked
/// @param[out] o_has_disables - true if the whole DRAM has any disables
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode dram_has_disables( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                     const uint64_t i_rp,
                                     const uint64_t i_dram,
                                     bool& o_has_disables)
{
    // Checks inputs
    uint64_t l_disables = 0;

    // Gets the disables for this DRAM
    FAPI_TRY(get_disables_for_dram(i_target, i_rp, i_dram, l_disables));

    // A DRAM is disabled if all of it's bits are disabled
    {
        constexpr uint64_t CLEAR = 0;
        o_has_disables = (CLEAR != l_disables);
        FAPI_INF("%s RP%lu DRAM%lu DRAM disables: 0x%lx clear value: 0x%lx %s disables", mss::c_str(i_target), i_rp, i_dram,
                 l_disables, CLEAR, o_has_disables ? "has" : "has no");
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Disables bits based upon RD VREF values that differ from the median significantly
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note The differing values can cause WR VREF fail, so the bit(s) that differ are disabled temporarily
///
fapi2::ReturnCode disable_bits( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                const uint64_t i_rp,
                                const uint64_t i_dram )
{
    // Gets the register for the disable bits
    uint64_t l_reg = 0;
    uint64_t l_dram_start_pos = 0;
    uint64_t l_len = 0;
    FAPI_TRY(get_disable_reg_and_pos_for_dram(i_target, i_rp, i_dram, l_reg, l_dram_start_pos, l_len));

    // Sets up this DRAM's disable bits
    {
        // Gets the bit to test
        uint64_t l_bit = 0;
        fapi2::buffer<uint64_t> l_data;
        std::vector<uint64_t> l_rd_vref_values;
        FAPI_TRY(read_rd_vref_for_dram( i_target, i_rp, i_dram, l_rd_vref_values ));
        FAPI_TRY(identify_first_good_rd_vref( i_target, i_rp, i_dram, l_rd_vref_values, l_bit ));

        // Read...
        FAPI_TRY(mss::getScom(i_target, l_reg, l_data));

        // Modify...
        // First of all, disable all bits - for the workaround, we only want to run one bit in the DRAM
        FAPI_TRY(l_data.setBit(l_dram_start_pos, l_len));
        // Next, clear the bit (set it as good)
        FAPI_TRY(l_data.clearBit(l_dram_start_pos + l_bit));

        // Write...
        FAPI_TRY(mss::putScom(i_target, l_reg, l_data));
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Clears all disable bits for a recovered DRAM
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode clear_dram_disable_bits( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram )
{
    // Gets the register for the disable bits
    uint64_t l_reg = 0;
    uint64_t l_bit_pos = 0;
    uint64_t l_len = 0;
    FAPI_TRY(get_disable_reg_and_pos_for_dram(i_target, i_rp, i_dram, l_reg, l_bit_pos, l_len));

    // Restores the disabled bits
    {
        // Read...
        fapi2::buffer<uint64_t> l_data;
        FAPI_TRY(mss::getScom(i_target, l_reg, l_data));

        // Modify...
        FAPI_TRY(l_data.clearBit(l_bit_pos, l_len));

        // Write...
        FAPI_TRY(mss::putScom(i_target, l_reg, l_data));
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Configures the WR VREF value of a DRAM to be the nominal values
/// @param[in] i_target the fapi2 target type DIMM
/// @param[in] i_rp - rank pair to check and modify
/// @param[in] i_dram - the DRAM
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode configure_wr_vref_to_nominal( const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram)
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;
    const auto& l_mca = mss::find_target<fapi2::TARGET_TYPE_MCA>(i_target);

    uint64_t l_reg = 0;

    // Get the WR VREF register
    FAPI_TRY(mss::dp16::wr_vref::get_wr_vref_rp_reg(l_mca, i_rp, i_dram, l_reg));

    {

        const auto l_dram_pos = i_dram % TT::NUM_DRAM_PER_REG;
        const auto l_range_pos = (l_dram_pos == 0) ? TT::WR_VREF_VALUE_RANGE_DRAM_EVEN : TT::WR_VREF_VALUE_RANGE_DRAM_ODD;
        const auto l_value_pos = (l_dram_pos == 0) ? TT::WR_VREF_VALUE_VALUE_DRAM_EVEN : TT::WR_VREF_VALUE_VALUE_DRAM_ODD;

        // Now for read modify write
        fapi2::buffer<uint64_t> l_data;

        // Gets the VPD JEDEC WR VREF information
        fapi2::buffer<uint8_t> l_train_value;
        fapi2::buffer<uint8_t> l_train_range;
        FAPI_TRY(mss::get_vpd_wr_vref_range_and_value(i_target, l_train_range, l_train_value));

        FAPI_TRY(mss::getScom(l_mca, l_reg, l_data));

        // Sends the data
        FAPI_TRY(l_data.writeBit(l_train_range, l_range_pos));
        FAPI_TRY(l_data.insertFromRight(l_train_value, l_value_pos, TT::WR_VREF_VALUE_VALUE_DRAM_EVEN_LEN));

        FAPI_TRY(mss::putScom(l_mca, l_reg, l_data));
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Configures the skip bits to be 0x7 for an entire port for the workaround
/// @param[in] i_target the fapi2 target type DIMM
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode configure_skip_bits( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;
    constexpr uint64_t SKIP_ALL = 0x7;

    std::vector<fapi2::buffer<uint64_t>> l_wr_vref_config;
    FAPI_TRY( mss::scom_suckah(i_target, TT::WR_VREF_CONFIG0_REG, l_wr_vref_config) );

    // Loops and sets or clears the 2D VREF bit on all DPs
    for(auto& l_data : l_wr_vref_config)
    {
        // Updates the skip bits to be skip all
        l_data.insertFromRight<TT::WR_VREF_CONFIG0_NUM_BITS_TO_SKIP, TT::WR_VREF_CONFIG0_NUM_BITS_TO_SKIP_LEN>(SKIP_ALL);
    }

    FAPI_TRY(mss::scom_blastah(i_target, TT::WR_VREF_CONFIG0_REG, l_wr_vref_config));

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Modifies the WR VREF value in an MRS06 to have the VPD, not eff_config attribute values
/// @param[in] i_target - the DIMM target on which to operate
/// @param[in,out] io_mrs06 - the MRS data class to modify
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note The differing values can cause WR VREF fail, so the bit(s) that differ are disabled temporarily
///
fapi2::ReturnCode modify_mrs_vref_to_vpd( const fapi2::Target<fapi2::TARGET_TYPE_DIMM>& i_target,
        mss::ddr4::mrs06_data& io_mrs06 )
{
    // Gets the VPD JEDEC WR VREF information
    uint8_t l_train_value = 0;
    uint8_t l_train_range = 0;
    FAPI_TRY(mss::get_vpd_wr_vref_range_and_value(i_target, l_train_range, l_train_value));

    FAPI_INF("%s setting MRS06 to have the correct VPD value from 0x%02lx to range: %lu value 0x%02lx from 0x%02x",
             mss::c_str(i_target), io_mrs06.iv_vrefdq_train_range[0], l_train_range, io_mrs06.iv_vrefdq_train_value[0],
             l_train_value);

    for(uint64_t l_rank = 0; l_rank < MAX_RANK_PER_DIMM; ++l_rank)
    {
        io_mrs06.iv_vrefdq_train_value[l_rank] = l_train_value;
        io_mrs06.iv_vrefdq_train_range[l_rank] = l_train_range;
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Resets the WR DQ delays for a given DRAM to be a quarter clock before the DQS
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @param[in] i_delay - the write DQ delay value
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note The differing values can cause WR VREF fail, so the bit(s) that differ are disabled temporarily
///
fapi2::ReturnCode reset_wr_dq_delay( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                     const uint64_t i_rp,
                                     const uint64_t i_dram,
                                     const uint64_t i_delay )
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    // Gets the DRAM width
    uint8_t l_width[MAX_DIMM_PER_PORT] = {};
    FAPI_TRY( mss::eff_dram_width(i_target, l_width) );

    // Checks inputs
    FAPI_TRY(mss::dp16::wr_vref::check_rp_and_dram(i_target, i_rp, i_dram, ffdc_function_codes::RESET_WR_DQ_DELAY));

    // Restores the inputted delay values
    {
        const uint64_t l_start_bit = i_dram * l_width[0];
        const uint64_t l_end_bit   = l_start_bit + l_width[0];

        // And restore the good DQ values
        for(uint64_t l_bit = l_start_bit; l_bit < l_end_bit; ++l_bit)
        {
            const auto l_dq_reg = TT::WR_DQ_DELAY_REG[i_rp][l_bit];
            fapi2::buffer<uint64_t> l_data;
            l_data.insertFromRight<TT::WR_DELAY, TT::WR_DELAY_LEN>(i_delay);

            FAPI_INF("%s RP%lu DRAM %lu DQ reg 0x%016lx DQ val 0x%lx reg val 0x%016lx",
                     mss::c_str(i_target), i_rp, i_dram, l_dq_reg, i_delay, l_data);
            FAPI_TRY(mss::putScom(i_target, l_dq_reg, l_data));
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Reads out the RD VREF values for a given DRAM
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @param[out] o_rd_vref_values - the RD VREF values for a given DRAM
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note The differing values can cause WR VREF fail, so the bit(s) that differ are disabled temporarily
///
fapi2::ReturnCode read_rd_vref_for_dram( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram,
        std::vector<uint64_t>& o_rd_vref_values )
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    // Clears the output, just in case
    o_rd_vref_values.clear();

    // Gets the DRAM width
    uint8_t l_width[MAX_DIMM_PER_PORT] = {};
    FAPI_TRY( mss::eff_dram_width(i_target, l_width) );

    // Checks inputs
    FAPI_TRY(mss::dp16::wr_vref::check_rp_and_dram(i_target, i_rp, i_dram,
             ffdc_function_codes::READ_RD_VREF_VALUES_FOR_DRAM));

    // Gets the RD VREF values
    {
        // There are two RD VREF values stored per register
        // One RD VREF value corresponds to one bit
        // For a x4 DRAM, there are 4 bits per DRAM, meaning two registers need to be read
        // For a x8 DRAM, there are 8 bits per DRAM, meaning four registers need to be read
        constexpr uint64_t NUM_REGS_X4 = 2;
        constexpr uint64_t NUM_REGS_X8 = 4;
        const auto NUM_REGS = (l_width[0] == fapi2::ENUM_ATTR_EFF_DRAM_WIDTH_X8) ? NUM_REGS_X8 : NUM_REGS_X4;

        // Computes the starting register's position
        const uint64_t START_REG = i_dram * NUM_REGS;

        // Loops through all of the registers
        for(uint64_t l_reg = 0; l_reg < NUM_REGS; ++l_reg)
        {
            const auto l_reg_pos = l_reg + START_REG;
            uint64_t l_value = 0;
            fapi2::buffer<uint64_t> l_buff;

            FAPI_TRY(mss::getScom(i_target, TT::DD2_RD_VREF_CNTRL_REG[l_reg_pos], l_buff));
            l_buff.extractToRight<TT::RD_VREF_BYTE0_NIB0, TT::RD_VREF_BYTE0_NIB0_LEN>(l_value);
            FAPI_INF("%s RP%lu DRAM%lu has RD VREF value of 0x%02lx", mss::c_str(i_target), i_rp, i_dram, l_value);
            o_rd_vref_values.push_back(l_value);
            l_buff.extractToRight<TT::RD_VREF_BYTE0_NIB1, TT::RD_VREF_BYTE0_NIB1_LEN>(l_value);
            FAPI_INF("%s RP%lu DRAM%lu has RD VREF value of 0x%02lx", mss::c_str(i_target), i_rp, i_dram, l_value);
            o_rd_vref_values.push_back(l_value);
        }
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Identifies the first good RD VREF bit
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @param[in] i_rd_vref_values - the RD VREF values for a given DRAM
/// @param[out] o_bit - the bit for the first good RD VREF
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note The differing values can cause WR VREF fail, so the bit(s) that differ are disabled temporarily
///
fapi2::ReturnCode identify_first_good_rd_vref( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram,
        const std::vector<uint64_t>& i_rd_vref_values,
        uint64_t& o_bit )
{
    constexpr uint64_t THRESHOLD = 3;
    // Loop through all RD VREF values
    uint64_t l_bit = 0;

    // Gets the median value
    uint64_t l_median = 0;
    FAPI_TRY(rd_dq::find_median(i_rd_vref_values, l_median));

    for(const auto l_value : i_rd_vref_values)
    {
        // Error case, continue on...
        if((l_median > (l_value + THRESHOLD)) || (l_value > (l_median + THRESHOLD)))
        {
            FAPI_INF("%s RP%lu DRAM%lu bit: %lu with value of %lu is out of the median (%lu) +/- threshold range (%lu). Testing the next bit",
                     mss::c_str(i_target), i_rp, i_dram, l_bit, l_value, l_median, THRESHOLD);
        }
        // The first bit inside the threshold is good, note it and exit out successfully
        else
        {
            FAPI_INF("%s RP%lu DRAM%lu bit: %lu with value of %lu is inside of the median (%lu) +/- threshold range (%lu). Returning this as the bit to test",
                     mss::c_str(i_target), i_rp, i_dram, l_bit, l_value, l_median, THRESHOLD);
            o_bit = l_bit;
            break;
        }

        ++l_bit;
    }

    return fapi2::FAPI2_RC_SUCCESS;

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Reads out the WR DQ delay value for a given DRAM
/// @param[in] i_target - the MCA target on which to operate
/// @param[in] i_rp - the rank pair on which to operate
/// @param[in] i_dram - the DRAM that needs to have the workaround applied to it
/// @param[out] o_wr_dq_delay - the WR DQ delay value for a given DRAM
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
/// @note At this point, WR CTR hasn't been run, so all WR DQ delays should be the same on the DRAM
///
fapi2::ReturnCode get_starting_wr_dq_delay( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
        const uint64_t i_rp,
        const uint64_t i_dram,
        uint64_t& o_wr_dq_delay )
{
    typedef dp16Traits<fapi2::TARGET_TYPE_MCA> TT;

    // Gets the DRAM width
    uint8_t l_width[MAX_DIMM_PER_PORT] = {};
    FAPI_TRY( mss::eff_dram_width(i_target, l_width) );

    // Checks inputs
    FAPI_TRY(mss::dp16::wr_vref::check_rp_and_dram(i_target, i_rp, i_dram,
             ffdc_function_codes::GET_STARTING_WR_DQ_DELAY_VALUE));

    // Gets the data
    {
        // Gets the register - we only want bit 0 of the DRAM
        const auto l_first_bit = i_dram * l_width[0];
        const auto l_reg = TT::WR_DQ_DELAY_REG[i_rp][l_first_bit];

        // Gets the register
        fapi2::buffer<uint64_t> l_data;
        FAPI_TRY(mss::getScom(i_target, l_reg, l_data));

        // Returns the data
        o_wr_dq_delay = 0;
        l_data.extractToRight<TT::WR_DELAY, TT::WR_DELAY_LEN>(o_wr_dq_delay);
    }

fapi_try_exit:
    return fapi2::current_err;
}

///
/// @brief Logs an informational callout detailing if we were able to recover
/// @param[in] i_target - the MCA target under calibration
/// @param[in] i_rp - the rank pair under calibration
/// @param[in] i_dram - the DRAM in question
/// @param[in] i_bad - true iff the DRAM failed to recover
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode log_dram_results( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target,
                                    const uint64_t i_rp,
                                    const uint64_t i_dram,
                                    const bool i_bad )
{
    // Log this information for cronus, as the logs are normally supressed
#ifndef __HOSTBOOT_MODULE
    FAPI_ERR("%s WR VREF workaround recovery %s on RP%lu DRAM%lu",
             mss::c_str(i_target), i_bad ? "failed" : "succeeded", i_rp, i_dram);
#endif

    // If we call this function, we have a fail
    FAPI_ASSERT( true,
                 fapi2::MSS_WR_VREF_DRAM_RECOVERY()
                 .set_TARGET(i_target)
                 .set_RP(i_rp)
                 .set_DRAM(i_dram)
                 .set_IS_BAD(i_bad),
                 "%s WR VREF workaround recovery %s on RP%lu DRAM%lu",
                 mss::c_str(i_target),
                 i_bad ? "failed" : "succeeded",
                 i_rp,
                 i_dram
               );

    return fapi2::FAPI2_RC_SUCCESS;
fapi_try_exit:

    // Log the error as recovered - we want this error to be informational
    fapi2::logError(fapi2::current_err, fapi2::FAPI2_ERRL_SEV_RECOVERED);
    fapi2::current_err = fapi2::FAPI2_RC_SUCCESS;
    return fapi2::current_err;
}

///
/// @brief Clears the PHY training FIRs for a given port
/// @param[in] i_target - the MCA target under calibration
/// @return fapi2::ReturnCode FAPI2_RC_SUCCESS if ok
///
fapi2::ReturnCode clear_training_firs( const fapi2::Target<fapi2::TARGET_TYPE_MCA>& i_target )
{

    fapi2::buffer<uint64_t> l_phyfir_mask;
    l_phyfir_mask.setBit<MCA_IOM_PHY0_DDRPHY_FIR_REG_ERROR_2>();

    // Clear the PHY FIR ERROR 2 bit so we don't keep failing training and training advance on this port
    FAPI_TRY( mss::putScom(i_target, MCA_IOM_PHY0_DDRPHY_FIR_REG_AND, l_phyfir_mask.invert()) );

    FAPI_TRY( mss::getScom(i_target, MCA_IOM_PHY0_DDRPHY_FIR_REG, l_phyfir_mask));

fapi_try_exit:
    return fapi2::current_err;
}


} // close namespace wr_vref
} // close namespace dp16
} // close namespace workarounds
} // close namespace mss
OpenPOWER on IntegriCloud