Move collectSBEDump to openpower-debug-collector

 Hardware and Hostboot dump collection are being handled in
 openpower-debug-collector. SBE dump collection should also be performed
 from openpower-debug-collector instead of from IPL.This change
 implements the collectSBEDump function as a SbeDumpCollector member
 function which executes the SBE dump collection process. collectSBEDump
 function is being removed from IPL in this commit[1]

 Declared functions which will be called from collectSBEDump
 in libphal.H file via commit[1]

 Implemented collectSBEDump(for SBE dump collection) and
 collectHWHBDump(for Hardware and Hostboot dump collection)
 as private member functions in SbeDumpCollector class. These
 private member functions will now be called via collectDump
 public method based on the type of dump that needs to be collected.

 Tested on a Everest DDR5 machine. Able to generate Proc and
 Odyssey SBE dumps

 [1] https://github.com/open-power/ipl/pull/96

Change-Id: I7919fdf31dc9a985a72bc06444d9d55fe78aa13b
Signed-off-by: Nabil Ananthamangalath <nabilmanjeri@gmail.com>
diff --git a/dump/dump_collect_main.cpp b/dump/dump_collect_main.cpp
index 319309a..d33fd30 100644
--- a/dump/dump_collect_main.cpp
+++ b/dump/dump_collect_main.cpp
@@ -77,14 +77,7 @@
 
     try
     {
-        if ((type == SBE_DUMP_TYPE_SBE) || (type == SBE_DUMP_TYPE_MSBE))
-        {
-            collectSBEDump(id, failingUnitId, pathStr, type);
-        }
-        else
-        {
-            dumpCollector.collectDump(type, id, failingUnitId, pathStr);
-        }
+        dumpCollector.collectDump(type, id, failingUnitId, pathStr);
     }
     catch (const std::exception& e)
     {
diff --git a/dump/sbe_dump_collector.cpp b/dump/sbe_dump_collector.cpp
index 8beda76..7e1a50c 100644
--- a/dump/sbe_dump_collector.cpp
+++ b/dump/sbe_dump_collector.cpp
@@ -24,6 +24,7 @@
 #include <filesystem>
 #include <format>
 #include <fstream>
+#include <map>
 #include <stdexcept>
 
 namespace openpower::dump::sbe_chipop
@@ -31,12 +32,25 @@
 
 using namespace phosphor::logging;
 using namespace openpower::dump::SBE;
+using namespace openpower::phal::dump;
 using Severity = sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level;
 
 void SbeDumpCollector::collectDump(uint8_t type, uint32_t id,
-                                   uint64_t failingUnit,
+                                   uint32_t failingUnit,
                                    const std::filesystem::path& path)
 {
+    if ((type == SBE_DUMP_TYPE_SBE) || (type == SBE_DUMP_TYPE_MSBE))
+    {
+        collectSBEDump(id, failingUnit, path, type);
+        return;
+    }
+    collectHWHBDump(type, id, failingUnit, path);
+}
+
+void SbeDumpCollector::collectHWHBDump(uint8_t type, uint32_t id,
+                                       uint64_t failingUnit,
+                                       const std::filesystem::path& path)
+{
     lg2::error("Starting dump collection: type:{TYPE} id:{ID} "
                "failingUnit:{FAILINGUNIT}, path:{PATH}",
                "TYPE", type, "ID", id, "FAILINGUNIT", failingUnit, "PATH",
@@ -129,6 +143,78 @@
     lg2::info("Dump collection completed");
 }
 
+void SbeDumpCollector::collectSBEDump(uint32_t id, uint32_t failingUnit,
+                                      const std::filesystem::path& dumpPath,
+                                      const int sbeTypeId)
+{
+    lg2::info("Collecting SBE dump: path={PATH}, id={ID}, "
+              "chip position={FAILINGUNIT}",
+              "PATH", dumpPath.string().c_str(), "ID", id, "FAILINGUNIT",
+              failingUnit);
+
+    struct pdbg_target* proc_ody = nullptr;
+    struct pdbg_target* pibFsiTarget = nullptr;
+    std::string sbeChipType;
+
+    try
+    {
+        // Execute pre-collection steps and get the proc target
+        initializePdbgLibEkb();
+
+        proc_ody = getTargetFromFailingId(failingUnit, sbeTypeId);
+        if (PROC_SBE_DUMP == sbeTypeId)
+        {
+            pibFsiTarget = probeTarget(proc_ody, "pib", sbeTypeId);
+            sbeChipType = "_p10_";
+        }
+        else
+        {
+            pibFsiTarget = probeTarget(proc_ody, "fsi", sbeTypeId);
+            sbeChipType = "_ody_";
+        }
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error("Failed to collect the SBE dump: {ERROR}", "ERROR",
+                   e.what());
+        throw;
+    }
+
+    std::stringstream ss;
+    ss << std::setw(8) << std::setfill('0') << id;
+
+    std::string baseFilename = ss.str() + ".0_" + std::to_string(failingUnit) +
+                               "_SbeData" + sbeChipType;
+
+    try
+    {
+        checkSbeState(pibFsiTarget, sbeTypeId);
+
+        executeSbeExtractRc(proc_ody, dumpPath, sbeTypeId);
+
+        // Collect various dumps
+        collectLocalRegDump(proc_ody, dumpPath, baseFilename, sbeTypeId);
+        collectPIBMSRegDump(proc_ody, dumpPath, baseFilename, sbeTypeId);
+        collectPIBMEMDump(proc_ody, dumpPath, baseFilename, sbeTypeId);
+        collectPPEState(proc_ody, dumpPath, baseFilename, sbeTypeId);
+
+        // Finalize the collection process and indicate successful completion
+        finalizeCollection(pibFsiTarget, dumpPath, true, sbeTypeId);
+
+        lg2::info("SBE dump collection completed successfully");
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error("Failed to collect the SBE dump: {ERROR}", "ERROR",
+                   e.what());
+        // In case of any exception, attempt to finalize with a failure
+        // state
+        if (proc_ody)
+            finalizeCollection(pibFsiTarget, dumpPath, false, sbeTypeId);
+        throw;
+    }
+}
+
 void SbeDumpCollector::initializePdbg()
 {
     openpower::phal::pdbg::init();
diff --git a/dump/sbe_dump_collector.hpp b/dump/sbe_dump_collector.hpp
index c8a41cd..a912c57 100644
--- a/dump/sbe_dump_collector.hpp
+++ b/dump/sbe_dump_collector.hpp
@@ -45,6 +45,23 @@
     ~SbeDumpCollector() = default;
 
     /**
+     * @brief Drives all type of dump collection process from SBEs.
+     *
+     * Triggers SBE, Hardware/Hostboot dump collection process from SBEs.
+     * Internally calls private method collectHWHBDump(for Hardware/Hostboot
+     * dump) or collectSBEDump(for SBE dump) based on the parameter type's value
+     *
+     * @param type The type of dump which needs to be collected.
+     * @param id ID of the collected dump.
+     * @param failingUnit ID of the failing unit from which the dump is
+     * collected.
+     * @param path Path where the collected dump will be stored.
+     */
+    void collectDump(uint8_t type, uint32_t id, uint32_t failingUnit,
+                     const std::filesystem::path& path);
+
+  private:
+    /**
      * @brief Orchestrates the collection of dumps from all available SBEs.
      *
      * Initiates the process of collecting diagnostic dumps from SBEs. This
@@ -57,10 +74,24 @@
      * collection.
      * @param path The filesystem path where collected dumps should be stored.
      */
-    void collectDump(uint8_t type, uint32_t id, uint64_t failingUnit,
-                     const std::filesystem::path& path);
+    void collectHWHBDump(uint8_t type, uint32_t id, uint64_t failingUnit,
+                         const std::filesystem::path& path);
 
-  private:
+    /**
+     * @brief Execute HWPs to collect SBE dump.
+     *
+     * @param[in] id Id of the dump.
+     * @param[in] failingUnit Id of proc containing failing SBE.
+     * @param[in] dumpPath Path to stored the dump files.
+     * @param[in] sbeTypeId ID for SBE type i.e.; Odyssey or normal memory chip
+     *                                             0xA-->Normal SBE type,
+     * 0xB-->Odyssey SBE type Exceptions: PDBG_INIT_FAIL for any pdbg init
+     * related failure.
+     */
+    void collectSBEDump(uint32_t id, uint32_t failingUnit,
+                        const std::filesystem::path& dumpPath,
+                        const int sbeTypeId);
+
     /**
      * @brief Collects a dump from a single SBE.
      *
@@ -231,5 +262,4 @@
     void addLogDataToDump(uint32_t logId, std::string src, std::string chipName,
                           uint64_t chipPos, const std::filesystem::path& path);
 };
-
 } // namespace openpower::dump::sbe_chipop