blob: ba6b3495b6ba1293e154da01954d7ea77896535c [file] [log] [blame]
Zane Shelleyd3b9bac2020-11-17 21:59:12 -06001#include <unistd.h>
2
3#include <hei_main.hpp>
4#include <phosphor-logging/elog.hpp>
5#include <sdbusplus/bus.hpp>
6#include <util/ffdc_file.hpp>
7#include <util/pdbg.hpp>
8#include <util/trace.hpp>
9#include <xyz/openbmc_project/Logging/Create/server.hpp>
10#include <xyz/openbmc_project/Logging/Entry/server.hpp>
11
Zane Shelley021dab32020-12-08 20:28:40 -060012#include <fstream>
13#include <memory>
14
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060015namespace LogSvr = sdbusplus::xyz::openbmc_project::Logging::server;
16
17namespace analyzer
18{
19
Zane Shelley021dab32020-12-08 20:28:40 -060020//------------------------------------------------------------------------------
21
22enum FfdcSubType_t : uint8_t
23{
24 FFDC_SIGNATURES = 0x01,
25 FFDC_CAPTURE_DATA = 0x02,
26
27 // For the callout section, the value of '0xCA' is required per the
28 // phosphor-logging openpower-pel extention spec.
29 FFDC_CALLOUTS = 0xCA,
30};
31
32enum FfdcVersion_t : uint8_t
33{
34 FFDC_VERSION1 = 0x01,
35};
36
37//------------------------------------------------------------------------------
38
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060039bool __isCheckstop(const libhei::IsolationData& i_isoData)
40{
41 // Look for any signature with a system checkstop attention.
42 auto list = i_isoData.getSignatureList();
43 auto itr = std::find_if(list.begin(), list.end(), [](const auto& s) {
44 return libhei::ATTN_TYPE_CHECKSTOP == s.getAttnType();
45 });
46
47 return list.end() != itr;
48}
49
50//------------------------------------------------------------------------------
51
Zane Shelley021dab32020-12-08 20:28:40 -060052void __getSrc(const libhei::Signature& i_signature, uint32_t& o_word6,
53 uint32_t& o_word7, uint32_t& o_word8)
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060054{
55 // [ 0:15] chip model
56 // [16:23] reserved space in chip ID
57 // [24:31] chip EC level
Zane Shelley021dab32020-12-08 20:28:40 -060058 o_word6 = i_signature.getChip().getType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060059
60 // [ 0:15] chip position
61 // [16:23] unused
62 // [24:31] signature attention type
Zane Shelley021dab32020-12-08 20:28:40 -060063 auto pos = util::pdbg::getChipPos(i_signature.getChip());
64 auto attn = i_signature.getAttnType();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060065
Zane Shelley021dab32020-12-08 20:28:40 -060066 o_word7 = (pos & 0xffff) << 16 | (attn & 0xff);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060067
68 // [ 0:15] signature ID
69 // [16:23] signature instance
70 // [24:31] signature bit position
Zane Shelley021dab32020-12-08 20:28:40 -060071 o_word8 = i_signature.toUint32();
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060072
73 // Word 9 is currently unused
Zane Shelley021dab32020-12-08 20:28:40 -060074}
75
76//------------------------------------------------------------------------------
77
78void __setSrc(const libhei::Signature& i_rootCause,
79 std::map<std::string, std::string>& io_logData)
80{
81 uint32_t word6 = 0, word7 = 0, word8 = 0;
82 __getSrc(i_rootCause, word6, word7, word8);
Zane Shelleyd3b9bac2020-11-17 21:59:12 -060083
84 io_logData["SRC6"] = std::to_string(word6);
85 io_logData["SRC7"] = std::to_string(word7);
86 io_logData["SRC8"] = std::to_string(word8);
87}
88
89//------------------------------------------------------------------------------
90
91void __captureSignatureList(const libhei::IsolationData& i_isoData,
92 std::vector<util::FFDCFile>& io_userDataFiles)
93{
Zane Shelley021dab32020-12-08 20:28:40 -060094 // Create a new entry for this user data section regardless if there are any
95 // signatures in the list.
96 io_userDataFiles.emplace_back(util::FFDCFormat::Custom, FFDC_SIGNATURES,
97 FFDC_VERSION1);
98
99 auto list = i_isoData.getSignatureList();
100
101 // The first entry in the buffer will be the number of signatures in the
102 // list.
103 uint32_t numSigs = list.size();
104 size_t sz_numSigs = sizeof(numSigs);
105
106 // Each signature in the buffer use the same format as the SRC.
107 uint32_t word6 = 0, word7 = 0, word8 = 0;
108 size_t sz_word = sizeof(uint32_t);
109
110 // Allocate the buffer.
111 size_t sz_buffer = sz_numSigs + (numSigs * (3 * sz_word));
112 std::unique_ptr<char[]> buffer{new char[sz_buffer]};
113
114 // Insert the number of signatures.
115 numSigs = htobe32(numSigs);
116 memcpy(&buffer[0], &numSigs, sz_numSigs);
117
118 // Insert each signature.
119 size_t idx = sz_numSigs;
120 for (const auto& sig : list)
121 {
122 __getSrc(sig, word6, word7, word8);
123
124 word6 = htobe32(word6);
125 word7 = htobe32(word7);
126 word8 = htobe32(word8);
127
128 // clang-format off
129 memcpy(&buffer[idx], &word6, sz_word); idx += sz_word;
130 memcpy(&buffer[idx], &word7, sz_word); idx += sz_word;
131 memcpy(&buffer[idx], &word8, sz_word); idx += sz_word;
132 // clang-format on
133 }
134
135 // Open the file for writing.
136 auto path = io_userDataFiles.back().getPath();
137 std::ofstream file{path, std::ios::binary};
138 if (!file.good())
139 {
140 trace::err("Unable to open file: %s", path.string().c_str());
141 }
142 else
143 {
144 // Write the buffer to file.
145 file.write(buffer.get(), sz_buffer);
146 if (!file.good())
147 {
148 trace::err("Unable to write file: %s", path.string().c_str());
149 }
150 }
Zane Shelleyd3b9bac2020-11-17 21:59:12 -0600151}
152
153//------------------------------------------------------------------------------
154
155std::string __getMessageRegistry(bool i_isCheckstop)
156{
157 // For now, there are only two choices:
158 return i_isCheckstop ? "org.open_power.HwDiags.Error.Checkstop"
159 : "org.open_power.HwDiags.Error.Predictive";
160}
161
162//------------------------------------------------------------------------------
163
164std::string __getMessageSeverity(bool i_isCheckstop)
165{
166 // We could specify the PEL severity in the message registry entry. However,
167 // that would require multiple copies of each entry for each possible
168 // severity. As a workaround, we will not explicitly state the PEL severity
169 // in the message registry. Instead, the message severity will be converted
170 // into a PEL severity via the openpower-pels extention of phosphor-logging.
171
172 // Initially, we'll use a severity that will generate a predictive PEL. This
173 // is intended for Terminate Immediate (TI) errors and will require service.
174 LogSvr::Entry::Level severity = LogSvr::Entry::Level::Warning;
175
176 // If the reason for analysis was due to a system checsktop, the severity
177 // will be upgraded to a unrecoverable PEL.
178 if (i_isCheckstop)
179 severity = LogSvr::Entry::Level::Error;
180
181 // Convert the message severity to a string.
182 return LogSvr::Entry::convertLevelToString(severity);
183}
184
185//------------------------------------------------------------------------------
186
187void createPel(const libhei::Signature& i_rootCause,
188 const libhei::IsolationData& i_isoData)
189{
190 // The message registry will require additional log data to fill in keywords
191 // and additional log data.
192 std::map<std::string, std::string> logData;
193
194 // Keep track of the temporary files associated with the user data FFDC.
195 // WARNING: Once the objects stored in this vector go out of scope, the
196 // temporary files will be deleted. So they must remain in scope
197 // until the PEL is submitted.
198 std::vector<util::FFDCFile> userDataFiles;
199
200 // In several cases, it is important to know if the reason for analysis was
201 // due to a system checsktop.
202 bool isCheckstop = __isCheckstop(i_isoData);
203
204 // Set words 6-9 of the SRC.
205 __setSrc(i_rootCause, logData);
206
207 // Capture the complete signature list.
208 __captureSignatureList(i_isoData, userDataFiles);
209
210 // Now, that all of the user data files have been created, transform the
211 // data into the proper format for the PEL.
212 std::vector<util::FFDCTuple> userData;
213 util::transformFFDC(userDataFiles, userData);
214
215 // Get access to logging interface and method for creating log.
216 auto bus = sdbusplus::bus::new_default_system();
217
218 // Using direct create method (for additional data).
219 auto method = bus.new_method_call(
220 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
221 "xyz.openbmc_project.Logging.Create", "CreateWithFFDCFiles");
222
223 // The "Create" method requires manually adding the process ID.
224 logData["_PID"] = std::to_string(getpid());
225
226 // Get the message registry entry for this failure.
227 auto message = __getMessageRegistry(isCheckstop);
228
229 // Get the message severity for this failure.
230 auto severity = __getMessageSeverity(isCheckstop);
231
232 // Add the message, with additional log and user data.
233 method.append(message, severity, logData, userData);
234
235 // Log the event.
236 bus.call_noreply(method);
237}
238
239} // namespace analyzer