Expanded PEL support for analyzer logs

Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: Ie33c5b777d537ab054a06c09087edcddcc129f15
diff --git a/analyzer/analyzer_main.cpp b/analyzer/analyzer_main.cpp
index 30efd74..b68a375 100644
--- a/analyzer/analyzer_main.cpp
+++ b/analyzer/analyzer_main.cpp
@@ -20,8 +20,20 @@
 
 // Forward references for externally defined functions.
 
+/**
+ * @brief Will get the list of active chip and initialize the isolator.
+ * @param o_chips The returned list of active chips.
+ */
 void initializeIsolator(std::vector<libhei::Chip>& o_chips);
 
+/**
+ * @brief Will create and submit a PEL using the given data.
+ * @param i_rootCause A signature defining the attention root cause.
+ * @param i_isoData   The data gathered during isolation (for FFDC).
+ */
+void createPel(const libhei::Signature& i_rootCause,
+               const libhei::IsolationData& i_isoData);
+
 //------------------------------------------------------------------------------
 
 const char* __attn(libhei::AttentionType_t i_attnType)
@@ -115,15 +127,6 @@
 {
     bool attnFound = false;
 
-    // Get numerical values for the root cause.
-    uint32_t word6 = 0; // [ 0: 7]: chip target type
-                        // [ 8:31]: chip FAPI position
-                        //    uint32_t word7 = 0; // TODO: chip target info
-    uint32_t word8 = 0; // [ 0:15]: node ID
-                        // [16:23]: node instance
-                        // [24:31]: bit position
-                        //    uint32_t word9 = 0; // [ 0: 7]: attention type
-
     if (i_sigList.empty())
     {
         trace::inf("No active attentions found");
@@ -135,36 +138,14 @@
         // The root cause attention is the first in the filtered list.
         libhei::Signature root = i_sigList.front();
 
-        word6 = __trgt(root);
-        word8 = __sig(root);
-
         trace::inf("Root cause attention: %s 0x%0" PRIx32 " %s",
-                   util::pdbg::getPath(root.getChip()), word8,
+                   util::pdbg::getPath(root.getChip()), root.toUint32(),
                    __attn(root.getAttnType()));
+
+        // Create and commit a PEL.
+        createPel(root, i_isoData);
     }
 
-    // Get the log data.
-    std::map<std::string, std::string> logData;
-    logData["_PID"]      = std::to_string(getpid());
-    logData["CHIP_ID"]   = std::to_string(word6);
-    logData["SIGNATURE"] = std::to_string(word8);
-
-    // Get access to logging interface and method for creating log.
-    auto bus = sdbusplus::bus::new_default_system();
-
-    // Using direct create method (for additional data)
-    auto method = bus.new_method_call(
-        "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
-        "xyz.openbmc_project.Logging.Create", "Create");
-
-    // Attach additional data
-    method.append("org.open_power.HwDiags.Error.Checkstop",
-                  "xyz.openbmc_project.Logging.Entry.Level.Error", logData);
-
-    // Log the event.
-    // TODO: Should the reply be handled?
-    bus.call(method);
-
     return attnFound;
 }
 
diff --git a/analyzer/create_pel.cpp b/analyzer/create_pel.cpp
new file mode 100644
index 0000000..2871da6
--- /dev/null
+++ b/analyzer/create_pel.cpp
@@ -0,0 +1,153 @@
+#include <unistd.h>
+
+#include <hei_main.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <sdbusplus/bus.hpp>
+#include <util/ffdc_file.hpp>
+#include <util/pdbg.hpp>
+#include <util/trace.hpp>
+#include <xyz/openbmc_project/Logging/Create/server.hpp>
+#include <xyz/openbmc_project/Logging/Entry/server.hpp>
+
+namespace LogSvr = sdbusplus::xyz::openbmc_project::Logging::server;
+
+namespace analyzer
+{
+
+bool __isCheckstop(const libhei::IsolationData& i_isoData)
+{
+    // Look for any signature with a system checkstop attention.
+    auto list = i_isoData.getSignatureList();
+    auto itr  = std::find_if(list.begin(), list.end(), [](const auto& s) {
+        return libhei::ATTN_TYPE_CHECKSTOP == s.getAttnType();
+    });
+
+    return list.end() != itr;
+}
+
+//------------------------------------------------------------------------------
+
+void __setSrc(const libhei::Signature& i_rootCause,
+              std::map<std::string, std::string>& io_logData)
+{
+    // [ 0:15] chip model
+    // [16:23] reserved space in chip ID
+    // [24:31] chip EC level
+    uint32_t word6 = i_rootCause.getChip().getType();
+
+    // [ 0:15] chip position
+    // [16:23] unused
+    // [24:31] signature attention type
+    auto pos  = util::pdbg::getChipPos(i_rootCause.getChip());
+    auto attn = i_rootCause.getAttnType();
+
+    uint32_t word7 = (pos & 0xffff) << 16 | (attn & 0xff);
+
+    // [ 0:15] signature ID
+    // [16:23] signature instance
+    // [24:31] signature bit position
+    uint32_t word8 = i_rootCause.toUint32();
+
+    // Word 9 is currently unused
+
+    io_logData["SRC6"] = std::to_string(word6);
+    io_logData["SRC7"] = std::to_string(word7);
+    io_logData["SRC8"] = std::to_string(word8);
+}
+
+//------------------------------------------------------------------------------
+
+void __captureSignatureList(const libhei::IsolationData& i_isoData,
+                            std::vector<util::FFDCFile>& io_userDataFiles)
+{
+    // TODO: Create a user data section that contains the complete list of
+    //       signatures found during isolation.
+}
+
+//------------------------------------------------------------------------------
+
+std::string __getMessageRegistry(bool i_isCheckstop)
+{
+    // For now, there are only two choices:
+    return i_isCheckstop ? "org.open_power.HwDiags.Error.Checkstop"
+                         : "org.open_power.HwDiags.Error.Predictive";
+}
+
+//------------------------------------------------------------------------------
+
+std::string __getMessageSeverity(bool i_isCheckstop)
+{
+    // We could specify the PEL severity in the message registry entry. However,
+    // that would require multiple copies of each entry for each possible
+    // severity. As a workaround, we will not explicitly state the PEL severity
+    // in the message registry. Instead, the message severity will be converted
+    // into a PEL severity via the openpower-pels extention of phosphor-logging.
+
+    // Initially, we'll use a severity that will generate a predictive PEL. This
+    // is intended for Terminate Immediate (TI) errors and will require service.
+    LogSvr::Entry::Level severity = LogSvr::Entry::Level::Warning;
+
+    // If the reason for analysis was due to a system checsktop, the severity
+    // will be upgraded to a unrecoverable PEL.
+    if (i_isCheckstop)
+        severity = LogSvr::Entry::Level::Error;
+
+    // Convert the message severity to a string.
+    return LogSvr::Entry::convertLevelToString(severity);
+}
+
+//------------------------------------------------------------------------------
+
+void createPel(const libhei::Signature& i_rootCause,
+               const libhei::IsolationData& i_isoData)
+{
+    // The message registry will require additional log data to fill in keywords
+    // and additional log data.
+    std::map<std::string, std::string> logData;
+
+    // Keep track of the temporary files associated with the user data FFDC.
+    // WARNING: Once the objects stored in this vector go out of scope, the
+    //          temporary files will be deleted. So they must remain in scope
+    //          until the PEL is submitted.
+    std::vector<util::FFDCFile> userDataFiles;
+
+    // In several cases, it is important to know if the reason for analysis was
+    // due to a system checsktop.
+    bool isCheckstop = __isCheckstop(i_isoData);
+
+    // Set words 6-9 of the SRC.
+    __setSrc(i_rootCause, logData);
+
+    // Capture the complete signature list.
+    __captureSignatureList(i_isoData, userDataFiles);
+
+    // Now, that all of the user data files have been created, transform the
+    // data into the proper format for the PEL.
+    std::vector<util::FFDCTuple> userData;
+    util::transformFFDC(userDataFiles, userData);
+
+    // Get access to logging interface and method for creating log.
+    auto bus = sdbusplus::bus::new_default_system();
+
+    // Using direct create method (for additional data).
+    auto method = bus.new_method_call(
+        "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
+        "xyz.openbmc_project.Logging.Create", "CreateWithFFDCFiles");
+
+    // The "Create" method requires manually adding the process ID.
+    logData["_PID"] = std::to_string(getpid());
+
+    // Get the message registry entry for this failure.
+    auto message = __getMessageRegistry(isCheckstop);
+
+    // Get the message severity for this failure.
+    auto severity = __getMessageSeverity(isCheckstop);
+
+    // Add the message, with additional log and user data.
+    method.append(message, severity, logData, userData);
+
+    // Log the event.
+    bus.call_noreply(method);
+}
+
+} // namespace analyzer
diff --git a/analyzer/meson.build b/analyzer/meson.build
index 40a65d3..0c69315 100644
--- a/analyzer/meson.build
+++ b/analyzer/meson.build
@@ -1,6 +1,7 @@
 # gather analyzer sources to be used here and elsewhere if needed
 analyzer_src = files(
     'analyzer_main.cpp',
+    'create_pel.cpp',
     'hei_user_interface.cpp',
     'initialize_isolator.cpp',
     '../util/ffdc_file.cpp',