1 #pragma once
2 
3 #include "additional_data.hpp"
4 #include "pel.hpp"
5 
6 #include <libekb.H>
7 
8 namespace openpower
9 {
10 namespace pels
11 {
12 namespace sbe
13 {
14 
15 // SBE FFDC sub type.
16 constexpr uint8_t sbeFFDCSubType = 0xCB;
17 
18 /**
19  *   @brief FFDC Package structure and definitions based on SBE chip-op spec.
20  *
21  *   SBE FFDC Starts with a header word (Word 0) that has an unique magic
22  *   identifier code of 0xFFDC followed by the length of the FFDC package
23  *   including the header itself. Word 1 contains a sequence id ,
24  *   command-class and command fields.
25  *   The sequence id field is ignored on the BMC side.
26  *   Word 2 contains a 32 bit Return Code which acts like the key to the
27  *   contents of subsequent FFDC Data Words (0-N).
28  *
29  *   A FFDC package can typically contain debug data from either:
30  *    1. A failed hardware procedure (e.g. local variable values
31  *       at point of failure) or
32  *    2. SBE firmware (e.g. traces, attributes and other information).
33  *    ___________________________________________________________
34  *   |        |  Byte 0   |  Byte 1  |  Byte 2    |    Byte 3   |
35  *   |----------------------------------------------------------|
36  *   | Word 0 | Magic Bytes : 0xFFDC | Length in words (N+4)    |
37  *   | Word 1 | [Sequence ID]        | Command-Class | Command  |
38  *   | Word 2 |           Return Code 0..31                     |
39  *   | Word 3 |           FFDC Data – Word 0                    |
40  *   |  ...                                                     |
41  *   | Word N+3 |  FFDC Data – Word N                           |
42  *    -----------------------------------------------------------
43  **/
44 
45 constexpr uint32_t sbeMaxFfdcPackets = 20;
46 constexpr uint32_t ffdcPkgOneWord = 1;
47 const uint16_t ffdcMagicCode = 0xFFDC;
48 
49 typedef struct
50 {
51     uint32_t magic_bytes : 16;
52     uint32_t lengthinWords : 16;
53     uint32_t seqId : 16;
54     uint32_t cmdClass : 8;
55     uint32_t cmd : 8;
56     uint32_t fapiRc;
57 } __attribute__((packed)) fapiFfdcBufType;
58 
59 using LogSeverity = phosphor::logging::Entry::Level;
60 
61 /** @class SbeFFDC
62  *
63  * @brief This class provides higher level interface to process SBE ffdc
64  * for PEL based error logging infrastructure.
65  * Key Functionalities included here
66  *    - Process the SBE FFDC data with the help of FAPI infrastructure and
67  *      and create PEL required format Callout and user data for hardware
68  *      procedure failures specific reason code
69  *    - Add the user data section with SBE FFDC data to support SBE provided
70  *      parser tool usage.
71  *    - Any SBE FFDC processing will result additional log message in journal
72  *      and will continue to create Error log with available data. This is to
73  *      help user to analyse the failure.
74  */
75 class SbeFFDC
76 {
77   public:
78     SbeFFDC() = delete;
79     SbeFFDC(const SbeFFDC&) = delete;
80     SbeFFDC& operator=(const SbeFFDC&) = delete;
81     SbeFFDC(SbeFFDC&&) = delete;
82     SbeFFDC& operator=(SbeFFDC&&) = delete;
83 
84     /**
85      * @brief Constructor
86      *
87      * Create PEL required format data from SBE provided FFDC data.
88      *
89      * @param[in] data - The AdditionalData properties in this PEL event
90      * @param[in] files - FFDC files that go into UserData sections
91      */
92     SbeFFDC(const AdditionalData& data, const PelFFDC& files);
93 
94     /**
95      * @brief Destructor
96      *
97      * Deletes the temporary files
98      */
99     ~SbeFFDC()
100     {
101 
102         try
103         {
104             for (auto path : paths)
105             {
106                 if (!path.empty())
107                 {
108                     // Delete temporary file from file system
109                     std::error_code ec;
110                     std::filesystem::remove(path, ec);
111                     // Clear path to indicate file has been deleted
112                     path.clear();
113                 }
114             }
115         }
116         catch (...)
117         {
118             // Destructors should not throw exceptions
119         }
120     }
121 
122     /**
123      * @brief Helper function to return FFDC files information, which
124      *        includes SBE FFDC specific callout information.
125      *
126      * return PelFFDC - pel formated FFDC files.
127      */
128     const PelFFDC& getSbeFFDC()
129     {
130         return ffdcFiles;
131     }
132 
133     /**
134      * @brief Helper function to get severity type
135      *
136      * @return severity type as informational for spare clock
137      *         failure type ffdc. Otherwise null string.
138      */
139     std::optional<LogSeverity> getSeverity();
140 
141   private:
142     /**
143      * @brief Helper function to parse SBE FFDC file.
144      *        parsing is based on the FFDC structure definition
145      *        define initially in this file.
146      *
147      * @param fd  SBE ffdc file descriptor
148      *
149      * Any failure during the process stops the function
150      * execution to support the raw SBE FFDC data based
151      * PEL creation.
152      */
153     void parse(int fd);
154 
155     /**
156      * @brief Helper function to process SBE FFDC packet.
157      * This function call libekb function to process the
158      * FFDC packet and convert in to known format for PEL
159      * specific file creation. This function also creates
160      * json callout file and text type file, which includes
161      * the addition debug data included in SBE FFDC packet.
162      *
163      * @param ffdcPkt  SBE FFDC packet
164      *
165      * Any failure during the process stops the function
166      * execution to support the raw SBE FFDC data based
167      * PEL creation.
168      */
169     void process(const sbeFfdcPacketType& ffdcPkt);
170 
171     /**
172      * @brief  Temporary files path information created as part of FFDC
173      *         processing.
174      */
175     std::vector<std::filesystem::path> paths;
176 
177     /**
178      * @brief PEL FFDC files, which includes the user data sections and the
179      *        added callout details if any, found during SBE FFDC processing.
180      */
181     PelFFDC ffdcFiles;
182 
183     /**
184      * @brief Processor position associated to SBE FFDC
185      */
186     uint32_t procPos;
187 
188     /**
189      * @brief Used to get type of ffdc
190      */
191     FFDC_TYPE ffdcType;
192 };
193 
194 } // namespace sbe
195 } // namespace pels
196 } // namespace openpower
197