1858d1aafSDhruvaraj Subhashchandran #pragma once
2858d1aafSDhruvaraj Subhashchandran 
3858d1aafSDhruvaraj Subhashchandran extern "C"
4858d1aafSDhruvaraj Subhashchandran {
5858d1aafSDhruvaraj Subhashchandran #include <libpdbg.h>
6858d1aafSDhruvaraj Subhashchandran #include <libpdbg_sbe.h>
7858d1aafSDhruvaraj Subhashchandran }
8858d1aafSDhruvaraj Subhashchandran 
9a699e31eSDhruvaraj Subhashchandran #include "dump_utils.hpp"
10a699e31eSDhruvaraj Subhashchandran #include "sbe_consts.hpp"
116feeebd6SDhruvaraj Subhashchandran #include "sbe_type.hpp"
126feeebd6SDhruvaraj Subhashchandran 
136feeebd6SDhruvaraj Subhashchandran #include <phal_exception.H>
14a699e31eSDhruvaraj Subhashchandran 
15858d1aafSDhruvaraj Subhashchandran #include <cstdint>
16858d1aafSDhruvaraj Subhashchandran #include <filesystem>
17858d1aafSDhruvaraj Subhashchandran #include <future>
18858d1aafSDhruvaraj Subhashchandran #include <vector>
19858d1aafSDhruvaraj Subhashchandran 
20858d1aafSDhruvaraj Subhashchandran namespace openpower::dump::sbe_chipop
21858d1aafSDhruvaraj Subhashchandran {
22858d1aafSDhruvaraj Subhashchandran 
23e74e9167SDhruvaraj Subhashchandran using TargetMap =
24e74e9167SDhruvaraj Subhashchandran     std::map<struct pdbg_target*, std::vector<struct pdbg_target*>>;
25e74e9167SDhruvaraj Subhashchandran 
26858d1aafSDhruvaraj Subhashchandran /**
27858d1aafSDhruvaraj Subhashchandran  * @class SbeDumpCollector
28858d1aafSDhruvaraj Subhashchandran  * @brief Manages the collection of dumps from SBEs on failure.
29858d1aafSDhruvaraj Subhashchandran  *
30858d1aafSDhruvaraj Subhashchandran  * This class provides functionalities to orchestrate the collection of
31858d1aafSDhruvaraj Subhashchandran  * diagnostic dumps from Self Boot Engines across multiple processors
32858d1aafSDhruvaraj Subhashchandran  * in response to failures or for diagnostic purposes.
33858d1aafSDhruvaraj Subhashchandran  */
34858d1aafSDhruvaraj Subhashchandran class SbeDumpCollector
35858d1aafSDhruvaraj Subhashchandran {
36858d1aafSDhruvaraj Subhashchandran   public:
37858d1aafSDhruvaraj Subhashchandran     /**
38858d1aafSDhruvaraj Subhashchandran      * @brief Constructs a new SbeDumpCollector object.
39858d1aafSDhruvaraj Subhashchandran      */
40858d1aafSDhruvaraj Subhashchandran     SbeDumpCollector() = default;
41858d1aafSDhruvaraj Subhashchandran 
42858d1aafSDhruvaraj Subhashchandran     /**
43858d1aafSDhruvaraj Subhashchandran      * @brief Destroys the SbeDumpCollector object.
44858d1aafSDhruvaraj Subhashchandran      */
45858d1aafSDhruvaraj Subhashchandran     ~SbeDumpCollector() = default;
46858d1aafSDhruvaraj Subhashchandran 
47858d1aafSDhruvaraj Subhashchandran     /**
48858d1aafSDhruvaraj Subhashchandran      * @brief Orchestrates the collection of dumps from all available SBEs.
49858d1aafSDhruvaraj Subhashchandran      *
50858d1aafSDhruvaraj Subhashchandran      * Initiates the process of collecting diagnostic dumps from SBEs. This
51858d1aafSDhruvaraj Subhashchandran      * involves identifying available processors, initiating the dump
52858d1aafSDhruvaraj Subhashchandran      * collection process, and managing the collected dump files.
53858d1aafSDhruvaraj Subhashchandran      *
54858d1aafSDhruvaraj Subhashchandran      * @param type The type of dump to collect.
55858d1aafSDhruvaraj Subhashchandran      * @param id A unique identifier for the dump collection operation.
56858d1aafSDhruvaraj Subhashchandran      * @param failingUnit The identifier of the failing unit prompting the dump
57858d1aafSDhruvaraj Subhashchandran      * collection.
58858d1aafSDhruvaraj Subhashchandran      * @param path The filesystem path where collected dumps should be stored.
59858d1aafSDhruvaraj Subhashchandran      */
60858d1aafSDhruvaraj Subhashchandran     void collectDump(uint8_t type, uint32_t id, uint64_t failingUnit,
61858d1aafSDhruvaraj Subhashchandran                      const std::filesystem::path& path);
62858d1aafSDhruvaraj Subhashchandran 
63858d1aafSDhruvaraj Subhashchandran   private:
64858d1aafSDhruvaraj Subhashchandran     /**
65858d1aafSDhruvaraj Subhashchandran      * @brief Collects a dump from a single SBE.
66858d1aafSDhruvaraj Subhashchandran      *
67858d1aafSDhruvaraj Subhashchandran      * Executes the low-level operations required to collect a diagnostic
68858d1aafSDhruvaraj Subhashchandran      * dump from the specified SBE.
69858d1aafSDhruvaraj Subhashchandran      *
70858d1aafSDhruvaraj Subhashchandran      * @param chip A pointer to the pdbg_target structure representing the SBE.
71858d1aafSDhruvaraj Subhashchandran      * @param path The filesystem path where the dump should be stored.
72858d1aafSDhruvaraj Subhashchandran      * @param id The unique identifier for this dump collection operation.
73858d1aafSDhruvaraj Subhashchandran      * @param type The type of dump to collect.
74858d1aafSDhruvaraj Subhashchandran      * @param clockState The clock state of the SBE during dump collection.
75858d1aafSDhruvaraj Subhashchandran      * @param failingUnit The identifier of the failing unit.
76858d1aafSDhruvaraj Subhashchandran      */
77858d1aafSDhruvaraj Subhashchandran     void collectDumpFromSBE(struct pdbg_target* chip,
78858d1aafSDhruvaraj Subhashchandran                             const std::filesystem::path& path, uint32_t id,
79858d1aafSDhruvaraj Subhashchandran                             uint8_t type, uint8_t clockState,
80858d1aafSDhruvaraj Subhashchandran                             uint64_t failingUnit);
81858d1aafSDhruvaraj Subhashchandran 
82858d1aafSDhruvaraj Subhashchandran     /**
83858d1aafSDhruvaraj Subhashchandran      * @brief Initializes the PDBG library.
84858d1aafSDhruvaraj Subhashchandran      *
85858d1aafSDhruvaraj Subhashchandran      * Prepares the PDBG library for interacting with processor targets. This
86858d1aafSDhruvaraj Subhashchandran      * must be called before any PDBG-related operations are performed.
87858d1aafSDhruvaraj Subhashchandran      */
88858d1aafSDhruvaraj Subhashchandran     void initializePdbg();
89858d1aafSDhruvaraj Subhashchandran 
90858d1aafSDhruvaraj Subhashchandran     /**
91858d1aafSDhruvaraj Subhashchandran      * @brief Launches asynchronous dump collection tasks for a set of targets.
92858d1aafSDhruvaraj Subhashchandran      *
93858d1aafSDhruvaraj Subhashchandran      * This method initiates the dump collection process asynchronously for each
94858d1aafSDhruvaraj Subhashchandran      * target provided in the `targets` vector. It launches a separate
95858d1aafSDhruvaraj Subhashchandran      * asynchronous task for each target, where each task calls
96858d1aafSDhruvaraj Subhashchandran      * `collectDumpFromSBE` with the specified parameters, including the clock
97858d1aafSDhruvaraj Subhashchandran      * state.
98858d1aafSDhruvaraj Subhashchandran      *
99858d1aafSDhruvaraj Subhashchandran      * @param type The type of the dump to collect. This could be a hardware
100858d1aafSDhruvaraj Subhashchandran      * dump, software dump, etc., as defined by the SBE dump type enumeration.
101858d1aafSDhruvaraj Subhashchandran      * @param id A unique identifier for the dump collection operation. This ID
102858d1aafSDhruvaraj Subhashchandran      * is used to tag the collected dump for identification.
103858d1aafSDhruvaraj Subhashchandran      * @param path The filesystem path where the collected dumps should be
104858d1aafSDhruvaraj Subhashchandran      * stored. Each dump file will be stored under this directory.
105858d1aafSDhruvaraj Subhashchandran      * @param failingUnit The identifier of the unit or component that is
106858d1aafSDhruvaraj Subhashchandran      * failing or suspected to be the cause of the issue prompting the dump
107858d1aafSDhruvaraj Subhashchandran      * collection. This is used for diagnostic purposes.
108858d1aafSDhruvaraj Subhashchandran      * @param cstate The clock state during the dump collection. This parameter
109858d1aafSDhruvaraj Subhashchandran      *               dictates whether the dump should be collected with the
110858d1aafSDhruvaraj Subhashchandran      * clocks running (SBE_CLOCK_ON) or with the clocks stopped (SBE_CLOCK_OFF).
111e74e9167SDhruvaraj Subhashchandran      * @param targetMap A map of `pdbg_target*` representing the targets from
112e74e9167SDhruvaraj Subhashchandran      * which dumps should be collected. The key is the proc target with the
113e74e9167SDhruvaraj Subhashchandran      * list of ocmb targets associated with the proc.
114858d1aafSDhruvaraj Subhashchandran      *
115858d1aafSDhruvaraj Subhashchandran      * @return A vector of `std::future<void>` objects. Each future represents
116858d1aafSDhruvaraj Subhashchandran      * the completion state of an asynchronous dump collection task. The caller
117858d1aafSDhruvaraj Subhashchandran      *         can wait on these futures to determine when all dump collection
118858d1aafSDhruvaraj Subhashchandran      * tasks have completed. Exceptions thrown by the asynchronous tasks are
119858d1aafSDhruvaraj Subhashchandran      * captured by the futures and can be rethrown when the futures are
120858d1aafSDhruvaraj Subhashchandran      * accessed.
121858d1aafSDhruvaraj Subhashchandran      */
122858d1aafSDhruvaraj Subhashchandran     std::vector<std::future<void>> spawnDumpCollectionProcesses(
123858d1aafSDhruvaraj Subhashchandran         uint8_t type, uint32_t id, const std::filesystem::path& path,
124e74e9167SDhruvaraj Subhashchandran         uint64_t failingUnit, uint8_t cstate, const TargetMap& targetMap);
125a699e31eSDhruvaraj Subhashchandran 
126a699e31eSDhruvaraj Subhashchandran     /** @brief This function creates the new dump file in dump file name
127a699e31eSDhruvaraj Subhashchandran      * format and then writes the contents into it.
128a699e31eSDhruvaraj Subhashchandran      *  @param path - Path to dump file
129a699e31eSDhruvaraj Subhashchandran      *  @param id - A unique id assigned to dump to be collected
130a699e31eSDhruvaraj Subhashchandran      *  @param clockState - Clock state, ON or Off
131a699e31eSDhruvaraj Subhashchandran      *  @param nodeNum - Node containing the chip
132a699e31eSDhruvaraj Subhashchandran      *  @param chipName - Name of the chip
133a699e31eSDhruvaraj Subhashchandran      *  @param chipPos - Chip position of the failing unit
134a699e31eSDhruvaraj Subhashchandran      *  @param dataPtr - Content to write to file
135a699e31eSDhruvaraj Subhashchandran      *  @param len - Length of the content
136a699e31eSDhruvaraj Subhashchandran      */
137a699e31eSDhruvaraj Subhashchandran     void writeDumpFile(const std::filesystem::path& path, const uint32_t id,
138a699e31eSDhruvaraj Subhashchandran                        const uint8_t clockState, const uint8_t nodeNum,
1396feeebd6SDhruvaraj Subhashchandran                        const std::string& chipName, const uint8_t chipPos,
140a699e31eSDhruvaraj Subhashchandran                        util::DumpDataPtr& dataPtr, const uint32_t len);
141a699e31eSDhruvaraj Subhashchandran 
142a699e31eSDhruvaraj Subhashchandran     /**
143a699e31eSDhruvaraj Subhashchandran      * @brief Determines if fastarray collection is needed based on dump type
144a699e31eSDhruvaraj Subhashchandran      * and unit.
145a699e31eSDhruvaraj Subhashchandran      *
146a699e31eSDhruvaraj Subhashchandran      * @param clockState The current state of the clock.
147a699e31eSDhruvaraj Subhashchandran      * @param type The type of the dump being collected.
148a699e31eSDhruvaraj Subhashchandran      * @param failingUnit The ID of the failing unit.
149a699e31eSDhruvaraj Subhashchandran      * @param chipPos The position of the chip for which the dump is being
150a699e31eSDhruvaraj Subhashchandran      * collected.
151a699e31eSDhruvaraj Subhashchandran      *
152a699e31eSDhruvaraj Subhashchandran      * @return uint8_t - Returns 1 if fastarray collection is needed, 0
153a699e31eSDhruvaraj Subhashchandran      * otherwise.
154a699e31eSDhruvaraj Subhashchandran      */
checkFastarrayCollectionNeeded(const uint8_t clockState,const uint8_t type,uint64_t failingUnit,const uint8_t chipPos) const155a699e31eSDhruvaraj Subhashchandran     inline uint8_t checkFastarrayCollectionNeeded(const uint8_t clockState,
156a699e31eSDhruvaraj Subhashchandran                                                   const uint8_t type,
157a699e31eSDhruvaraj Subhashchandran                                                   uint64_t failingUnit,
158a699e31eSDhruvaraj Subhashchandran                                                   const uint8_t chipPos) const
159a699e31eSDhruvaraj Subhashchandran     {
160a699e31eSDhruvaraj Subhashchandran         using namespace openpower::dump::SBE;
161a699e31eSDhruvaraj Subhashchandran 
162a699e31eSDhruvaraj Subhashchandran         return (clockState == SBE_CLOCK_OFF &&
163a699e31eSDhruvaraj Subhashchandran                 (type == SBE_DUMP_TYPE_HOSTBOOT ||
164a699e31eSDhruvaraj Subhashchandran                  (type == SBE_DUMP_TYPE_HARDWARE && chipPos == failingUnit)))
165a699e31eSDhruvaraj Subhashchandran                    ? 1
166a699e31eSDhruvaraj Subhashchandran                    : 0;
167a699e31eSDhruvaraj Subhashchandran     }
1686feeebd6SDhruvaraj Subhashchandran 
1696feeebd6SDhruvaraj Subhashchandran     /**
1706feeebd6SDhruvaraj Subhashchandran      * Logs an error and creates a PEL for SBE chip-op failures.
1716feeebd6SDhruvaraj Subhashchandran      *
1726feeebd6SDhruvaraj Subhashchandran      * @param sbeError - An error object encapsulating details about the SBE
1736feeebd6SDhruvaraj Subhashchandran      * error.
1746feeebd6SDhruvaraj Subhashchandran      * @param chipPos - The position of the chip where the error occurred.
1756feeebd6SDhruvaraj Subhashchandran      * @param sbeType - The type of SBE, used to determine the event log
1766feeebd6SDhruvaraj Subhashchandran      * message.
1776feeebd6SDhruvaraj Subhashchandran      * @param cmdClass - The command class associated with the SBE operation.
1786feeebd6SDhruvaraj Subhashchandran      * @param cmdType - The specific type of command within the command class.
1796feeebd6SDhruvaraj Subhashchandran      *
1806feeebd6SDhruvaraj Subhashchandran      */
181*f229889dSDhruvaraj Subhashchandran     bool logErrorAndCreatePEL(const openpower::phal::sbeError_t& sbeError,
1826feeebd6SDhruvaraj Subhashchandran                               uint64_t chipPos, SBETypes sbeType,
1836feeebd6SDhruvaraj Subhashchandran                               uint32_t cmdClass, uint32_t cmdType);
1846feeebd6SDhruvaraj Subhashchandran 
1856feeebd6SDhruvaraj Subhashchandran     /**
1866feeebd6SDhruvaraj Subhashchandran      * Determines the type of SBE for a given chip target.
1876feeebd6SDhruvaraj Subhashchandran      *
1886feeebd6SDhruvaraj Subhashchandran      * @param chip - A pointer to a pdbg_target structure representing the chip.
1896feeebd6SDhruvaraj Subhashchandran      * @return The SBE type for the given chip target.
1906feeebd6SDhruvaraj Subhashchandran      */
getSBEType(struct pdbg_target * chip)1916feeebd6SDhruvaraj Subhashchandran     inline SBETypes getSBEType([[maybe_unused]] struct pdbg_target* chip)
1926feeebd6SDhruvaraj Subhashchandran     {
193e74e9167SDhruvaraj Subhashchandran         if (is_ody_ocmb_chip(chip))
194e74e9167SDhruvaraj Subhashchandran         {
195e74e9167SDhruvaraj Subhashchandran             return SBETypes::OCMB;
196e74e9167SDhruvaraj Subhashchandran         }
1976feeebd6SDhruvaraj Subhashchandran         return SBETypes::PROC;
1986feeebd6SDhruvaraj Subhashchandran     }
199f9f65b82SDhruvaraj Subhashchandran 
200f9f65b82SDhruvaraj Subhashchandran     /**
201f9f65b82SDhruvaraj Subhashchandran      * @brief Executes thread stop on a processor target
202f9f65b82SDhruvaraj Subhashchandran      *
203f9f65b82SDhruvaraj Subhashchandran      * If the Self Boot Engine (SBE) is not ready to accept chip operations
204f9f65b82SDhruvaraj Subhashchandran      * (chip-ops), it logs the condition and excludes the processor from the
205f9f65b82SDhruvaraj Subhashchandran      * dump collection process. For critical errors, such as a timeout during
206f9f65b82SDhruvaraj Subhashchandran      * the stop operation, it logs the error and again excludes the processor.
207f9f65b82SDhruvaraj Subhashchandran      * In case of SBE command failure or non-critical errors, it continues with
208f9f65b82SDhruvaraj Subhashchandran      * the dump collection process.
209f9f65b82SDhruvaraj Subhashchandran      *
210f9f65b82SDhruvaraj Subhashchandran      * @param target Pointer to the pdbg target structure representing the
211f9f65b82SDhruvaraj Subhashchandran      *               processor to perform the thread stop on.
212f9f65b82SDhruvaraj Subhashchandran      * @return true If the thread stop was successful or in case of non-critical
213f9f65b82SDhruvaraj Subhashchandran      *              errors where dump collection can proceed.
214f9f65b82SDhruvaraj Subhashchandran      * @return false If the SBE is not ready for chip-ops or in case of critical
215f9f65b82SDhruvaraj Subhashchandran      *               errors like timeouts, indicating the processor should be
216f9f65b82SDhruvaraj Subhashchandran      *               excluded from the dump collection.
217f9f65b82SDhruvaraj Subhashchandran      */
218f9f65b82SDhruvaraj Subhashchandran     bool executeThreadStop(struct pdbg_target* target);
219858d1aafSDhruvaraj Subhashchandran };
220858d1aafSDhruvaraj Subhashchandran 
221858d1aafSDhruvaraj Subhashchandran } // namespace openpower::dump::sbe_chipop
222