PEL: Support for going between PELs & registry

Add tables that allow one to go between how a PEL field actually shows
up in the PEL (raw bytes) and how it shows up in the message registry (a
string enumeration).  The tables also have a column to show a string
description of that value that can be used by the parser, though for now
those descriptions are all left at "TODO".

There only needs to be a table for a PEL field when there is a
corresponding message registry field that is a string enumeration, so
that when code looks up an error in the message registry it knows what
to fill in the PEL with.

Also provide APIs to look up a row in the table by either the PEL value
or the message registry value.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Iac849bcd2b0449a8d03fac7eb067484e91d28259
diff --git a/extensions/openpower-pels/openpower-pels.mk b/extensions/openpower-pels/openpower-pels.mk
index 4f424f7..14e1966 100644
--- a/extensions/openpower-pels/openpower-pels.mk
+++ b/extensions/openpower-pels/openpower-pels.mk
@@ -8,6 +8,7 @@
 	extensions/openpower-pels/mtms.cpp \
 	extensions/openpower-pels/paths.cpp \
 	extensions/openpower-pels/pel.cpp \
+	extensions/openpower-pels/pel_values.cpp \
 	extensions/openpower-pels/private_header.cpp \
 	extensions/openpower-pels/repository.cpp \
 	extensions/openpower-pels/user_header.cpp
diff --git a/extensions/openpower-pels/pel_values.cpp b/extensions/openpower-pels/pel_values.cpp
new file mode 100644
index 0000000..c9b206b
--- /dev/null
+++ b/extensions/openpower-pels/pel_values.cpp
@@ -0,0 +1,196 @@
+#include "pel_values.hpp"
+
+#include <algorithm>
+
+namespace openpower
+{
+namespace pels
+{
+namespace pel_values
+{
+
+// Note: The description fields will be filled in as part of the
+//       PEL parser work.
+
+/**
+ * The possible values for the subsystem field  in the User Header.
+ */
+const PELValues subsystemValues = {{0x10, "processor", "TODO"},
+                                   {0x11, "processor_fru", "TODO"},
+                                   {0x12, "processor_chip", "TODO"},
+                                   {0x13, "processor_unit", "TODO"},
+                                   {0x14, "processor_bus", "TODO"},
+
+                                   {0x20, "memory", "TODO"},
+                                   {0x21, "memory_ctlr", "TODO"},
+                                   {0x22, "memory_bus", "TODO"},
+                                   {0x23, "memory_dimm", "TODO"},
+                                   {0x24, "memory_fru", "TODO"},
+                                   {0x25, "external_cache", "TODO"},
+
+                                   {0x30, "io", "TODO"},
+                                   {0x31, "io_hub", "TODO"},
+                                   {0x32, "io_bridge", "TODO"},
+                                   {0x33, "io_bus", "TODO"},
+                                   {0x34, "io_processor", "TODO"},
+                                   {0x35, "io_hub_other", "TODO"},
+                                   {0x38, "phb", "TODO"},
+
+                                   {0x40, "io_adapter", "TODO"},
+                                   {0x41, "io_adapter_comm", "TODO"},
+                                   {0x46, "io_device", "TODO"},
+                                   {0x47, "io_device_dasd", "TODO"},
+                                   {0x4C, "io_external_general", "TODO"},
+                                   {0x4D, "io_external_workstation", "TODO"},
+                                   {0x4E, "io_storage_mezz", "TODO"},
+
+                                   {0x50, "cec_hardware", "TODO"},
+                                   {0x51, "cec_sp_a", "TODO"},
+                                   {0x52, "cec_sp_b", "TODO"},
+                                   {0x53, "cec_node_controller", "TODO"},
+                                   {0x55, "cec_vpd", "TODO"},
+                                   {0x56, "cec_i2c", "TODO"},
+                                   {0x57, "cec_chip_iface", "TODO"},
+                                   {0x58, "cec_clocks", "TODO"},
+                                   {0x59, "cec_op_panel", "TODO"},
+                                   {0x5A, "cec_tod", "TODO"},
+                                   {0x5B, "cec_storage_device", "TODO"},
+                                   {0x5C, "cec_sp_hyp_iface", "TODO"},
+                                   {0x5D, "cec_service_network", "TODO"},
+                                   {0x5E, "cec_sp_hostboot_iface", "TODO"},
+
+                                   {0x60, "power", "TODO"},
+                                   {0x61, "power_supply", "TODO"},
+                                   {0x62, "power_control_hw", "TODO"},
+                                   {63, "power_fans", "TODO"},
+                                   {0x64, "power_sequencer", "TODO"},
+
+                                   {0x70, "others", "TODO"},
+                                   {0x71, "other_hmc", "TODO"},
+                                   {0x72, "other_test_tool", "TODO"},
+                                   {0x73, "other_media", "TODO"},
+                                   {0x74, "other_multiple_subsystems", "TODO"},
+                                   {0x75, "other_na", "TODO"},
+                                   {0x76, "other_info_src", "TODO"},
+
+                                   {0x7A, "surv_hyp_lost_sp", "TODO"},
+                                   {0x7B, "surv_sp_lost_hyp", "TODO"},
+                                   {0x7C, "surv_sp_lost_hmc", "TODO"},
+                                   {0x7D, "surv_hmc_lost_lpar", "TODO"},
+                                   {0x7E, "surv_hmc_lost_bpa", "TODO"},
+                                   {0x7F, "surv_hmc_lost_hmc", "TODO"},
+
+                                   {0x80, "platform_firmware", "TODO"},
+                                   {0x81, "sp_firmware", "TODO"},
+                                   {0x82, "hyp_firmware", "TODO"},
+                                   {0x83, "partition_firmware", "TODO"},
+                                   {0x84, "slic_firmware", "TODO"},
+                                   {0x85, "spcn_firmware", "TODO"},
+                                   {0x86, "bulk_power_firmware_side_a", "TODO"},
+                                   {0x87, "hmc_code_firmware", "TODO"},
+                                   {0x88, "bulk_power_firmware_side_b", "TODO"},
+                                   {0x89, "virtual_sp", "TODO"},
+                                   {0x8A, "hostboot", "TODO"},
+                                   {0x8B, "occ", "TODO"},
+                                   {0x8D, "bmc_firmware", "TODO"},
+
+                                   {0x90, "software", "TODO"},
+                                   {0x91, "os_software", "TODO"},
+                                   {0x92, "xpf_software", "TODO"},
+                                   {0x93, "app_software", "TODO"},
+
+                                   {0xA0, "ext_env", "TODO"},
+                                   {0xA1, "input_power_source", "TODO"},
+                                   {0xA2, "ambient_temp", "TODO"},
+                                   {0xA3, "user_error", "TODO"},
+                                   {0xA4, "corrosion", "TODO"}};
+
+/**
+ * The possible values for the severity field in the User Header.
+ */
+const PELValues severityValues = {
+    {0x00, "non_error", "TODO"},
+
+    {0x10, "recovered", "TODO"},
+    {0x20, "predictive", "TODO"},
+    {0x21, "predictive_degraded_perf", "TODO"},
+    {0x22, "predictive_reboot", "TODO"},
+    {0x23, "predictive_reboot_degraded", "TODO"},
+    {0x24, "predictive_redundancy_loss", "TODO"},
+
+    {0x40, "unrecoverable", "TODO"},
+    {0x41, "unrecoverable_degraded_perf", "TODO"},
+    {0x44, "unrecoverable_redundancy_loss", "TODO"},
+    {0x45, "unrecoverable_redundancy_loss_perf", "TODO"},
+    {0x48, "unrecoverable_loss_of_function", "TODO"},
+
+    {0x50, "critical", "TODO"},
+    {0x51, "critical_system_term", "TODO"},
+    {0x52, "critical_imminent_failure", "TODO"},
+    {0x53, "critical_partition_term", "TODO"},
+    {0x54, "critical_partition_imminent_failure", "TODO"},
+
+    {0x60, "diagnostic_error", "TODO"},
+    {0x61, "diagnostic_error_incorrect_results", "TODO"},
+
+    {0x71, "symptom_recovered", "TODO"},
+    {0x72, "symptom_predictive", "TODO"},
+    {0x74, "symptom_unrecoverable", "TODO"},
+    {0x75, "symptom_critical", "TODO"},
+    {0x76, "symptom_diag_err", "TODO"}};
+
+/**
+ * The possible values for the Event Type field in the User Header.
+ */
+const PELValues eventTypeValues = {{0x00, "na", "TODO"},
+                                   {0x01, "misc_information_only", "TODO"},
+                                   {0x02, "tracing_event", "TODO"},
+                                   {0x08, "dump_notification", "TODO"}};
+
+/**
+ * The possible values for the Event Scope field in the User Header.
+ */
+const PELValues eventScopeValues = {
+    {0x01, "single_partition", "TODO"},
+    {0x02, "multiple_partitions", "TODO"},
+    {0x03, "entire_platform", "TODO"},
+    {0x04, "possibly_multiple_platforms", "TODO"}};
+
+/**
+ * The possible values for the Action Flags field in the User Header.
+ */
+const PELValues actionFlagsValues = {
+    {0x8000, "service_action", "TODO"}, {0x4000, "hidden", "TODO"},
+    {0x2000, "report", "TODO"},         {0x1000, "dont_report", "TODO"},
+    {0x0800, "call_home", "TODO"},      {0x0100, "termination", "TODO"}};
+
+/**
+ * The possible values for the Callout Priority field in the SRC.
+ */
+const PELValues calloutPriorityValues = {
+    {'H', "high", "TODO"},           {'M', "medium", "TODO"},
+    {'A', "medium_group_a", "TODO"}, {'B', "medium_group_b", "TODO"},
+    {'C', "medium_group_c", "TODO"}, {'L', "low", "TODO"}};
+
+PELValues::const_iterator findByValue(uint32_t value, const PELValues& fields)
+{
+    return std::find_if(fields.begin(), fields.end(),
+                        [value](const auto& entry) {
+                            return value == std::get<fieldValuePos>(entry);
+                        });
+}
+
+PELValues::const_iterator findByName(const std::string& name,
+                                     const PELValues& fields)
+
+{
+    return std::find_if(fields.begin(), fields.end(),
+                        [&name](const auto& entry) {
+                            return name == std::get<registryNamePos>(entry);
+                        });
+}
+
+} // namespace pel_values
+
+} // namespace pels
+} // namespace openpower
diff --git a/extensions/openpower-pels/pel_values.hpp b/extensions/openpower-pels/pel_values.hpp
new file mode 100644
index 0000000..6092f62
--- /dev/null
+++ b/extensions/openpower-pels/pel_values.hpp
@@ -0,0 +1,81 @@
+#pragma once
+
+#include <string>
+#include <tuple>
+#include <vector>
+
+namespace openpower
+{
+namespace pels
+{
+namespace pel_values
+{
+
+// The actual value as it shows up in the PEL
+const int fieldValuePos = 0;
+
+// The name of the value as specified in the message registry
+const int registryNamePos = 1;
+
+// The description of the field, used by PEL parsers
+const int descriptionPos = 2;
+
+using PELFieldValue = std::tuple<uint32_t, const char*, const char*>;
+using PELValues = std::vector<PELFieldValue>;
+
+/**
+ * @brief Find the desired entry in a PELValues table based on the
+ *        field value.
+ *
+ * @param[in] value - the PEL value to find
+ * @param[in] fields - the PEL values table to use
+ *
+ * @return PELValues::const_iterator - an iterator to the table entry
+ */
+PELValues::const_iterator findByValue(uint32_t value, const PELValues& fields);
+
+/**
+ * @brief Find the desired entry in a PELValues table based on the
+ *        field message registry name.
+ *
+ * @param[in] name - the PEL message registry enum name
+ * @param[in] fields - the PEL values table to use
+ *
+ * @return PELValues::const_iterator - an iterator to the table entry
+ */
+PELValues::const_iterator findByName(const std::string& name,
+                                     const PELValues& fields);
+
+/**
+ * @brief The values for the 'subsystem' field in the User Header
+ */
+extern const PELValues subsystemValues;
+
+/**
+ * @brief The values for the 'severity' field in the User Header
+ */
+extern const PELValues severityValues;
+
+/**
+ * @brief The values for the 'Event Type' field in the User Header
+ */
+extern const PELValues eventTypeValues;
+
+/**
+ * @brief The values for the 'Event Scope' field in the User Header
+ */
+extern const PELValues eventScopeValues;
+
+/**
+ * @brief The values for the 'Action Flags' field in the User Header
+ */
+extern const PELValues actionFlagsValues;
+
+/**
+ * @brief The values for callout priorities in the SRC section
+ */
+extern const PELValues calloutPriorityValues;
+
+} // namespace pel_values
+} // namespace pels
+} // namespace openpower
diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include
index fefbbc6..5705f73 100644
--- a/test/openpower-pels/Makefile.include
+++ b/test/openpower-pels/Makefile.include
@@ -7,6 +7,7 @@
 	log_id_test \
 	mtms_test \
 	pel_test \
+	pel_values_test \
 	pel_manager_test \
 	private_header_test \
 	repository_test \
@@ -123,3 +124,11 @@
 	$(top_builddir)/extensions/openpower-pels/failing_mtms.o \
 	$(top_builddir)/extensions/openpower-pels/mtms.o
 failing_mtms_test_LDFLAGS = $(test_ldflags)
+
+pel_values_test_SOURCES = %reldir%/pel_values_test.cpp
+pel_values_test_CPPFLAGS = $(test_cppflags)
+pel_values_test_CXXFLAGS = $(test_cxxflags)
+pel_values_test_LDADD = \
+	$(test_ldflags) \
+	$(top_builddir)/extensions/openpower-pels/pel_values.o
+pel_values_test_LDFLAGS = $(test_ldflags)
diff --git a/test/openpower-pels/pel_values_test.cpp b/test/openpower-pels/pel_values_test.cpp
new file mode 100644
index 0000000..d44153c
--- /dev/null
+++ b/test/openpower-pels/pel_values_test.cpp
@@ -0,0 +1,24 @@
+#include "extensions/openpower-pels/pel_values.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace openpower::pels::pel_values;
+
+TEST(PELFieldsTest, TestFindFields)
+{
+    auto s = findByValue(0x5D, subsystemValues);
+    ASSERT_NE(s, subsystemValues.end());
+    ASSERT_EQ(0x5D, std::get<fieldValuePos>(*s));
+    ASSERT_EQ("cec_service_network", std::get<registryNamePos>(*s));
+
+    s = findByName("cec_clocks", subsystemValues);
+    ASSERT_NE(s, subsystemValues.end());
+    ASSERT_EQ(0x58, std::get<fieldValuePos>(*s));
+    ASSERT_EQ("cec_clocks", std::get<registryNamePos>(*s));
+
+    s = findByValue(0xFF, subsystemValues);
+    ASSERT_EQ(s, subsystemValues.end());
+
+    s = findByName("foo", subsystemValues);
+    ASSERT_EQ(s, subsystemValues.end());
+}