blob: 774fb0a5c26a3e78b44e4b014ed704352fe9ed7d [file] [log] [blame]
Zane Shelleyd84ed6e2020-06-08 13:41:48 -05001#include <assert.h>
Ben Tyner87eabc62020-05-14 17:56:54 -05002#include <libpdbg.h>
Zane Shelley9fb73932020-09-15 13:34:57 -05003#include <unistd.h>
Ben Tyner87eabc62020-05-14 17:56:54 -05004
Ben Tyner0205f3b2020-02-24 10:24:47 -06005#include <hei_main.hpp>
Zane Shelley9fb73932020-09-15 13:34:57 -05006#include <phosphor-logging/log.hpp>
Zane Shelleyd84ed6e2020-06-08 13:41:48 -05007#include <util/trace.hpp>
Ben Tyner0205f3b2020-02-24 10:24:47 -06008
Zane Shelleyd84ed6e2020-06-08 13:41:48 -05009#include <algorithm>
Ben Tyner87eabc62020-05-14 17:56:54 -050010#include <fstream>
11#include <iostream>
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050012#include <map>
13#include <string>
14
Ben Tyner0205f3b2020-02-24 10:24:47 -060015namespace analyzer
16{
17
Ben Tyner87eabc62020-05-14 17:56:54 -050018/**
19 * @brief send chip data file to isolator
20 *
21 * Read a chip data file into memory and then send it to the isolator via
22 * the initialize interface.
23 *
24 * @param i_filePath The file path and name to read into memory
25 *
26 * @return Returns true if the isolator was successfully initialized with
27 * a single chip data file. Returns false otherwise.
28 *
29 */
Zane Shelley2e994bc2020-06-08 14:38:14 -050030void initWithFile(const char* i_filePath)
Ben Tyner0205f3b2020-02-24 10:24:47 -060031{
Ben Tyner87eabc62020-05-14 17:56:54 -050032 // open the file and seek to the end to get length
33 std::ifstream fileStream(i_filePath, std::ios::binary | std::ios::ate);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050034
Zane Shelley2e994bc2020-06-08 14:38:14 -050035 if (!fileStream.good())
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050036 {
Zane Shelley2e994bc2020-06-08 14:38:14 -050037 trace::err("Unable to open file: %s", i_filePath);
38 assert(0);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050039 }
40 else
41 {
Ben Tyner87eabc62020-05-14 17:56:54 -050042 // get file size based on seek position
Zane Shelley2e994bc2020-06-08 14:38:14 -050043 fileStream.seekg(0, std::ios::end);
Ben Tyner87eabc62020-05-14 17:56:54 -050044 std::ifstream::pos_type fileSize = fileStream.tellg();
45
46 // create a buffer large enough to hold the entire file
47 std::vector<char> fileBuffer(fileSize);
48
49 // seek to the beginning of the file
50 fileStream.seekg(0, std::ios::beg);
51
52 // read the entire file into the buffer
53 fileStream.read(fileBuffer.data(), fileSize);
54
55 // done with the file
56 fileStream.close();
57
Zane Shelley2e994bc2020-06-08 14:38:14 -050058 // initialize the isolator with the chip data
59 libhei::initialize(fileBuffer.data(), fileSize);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050060 }
Ben Tyner87eabc62020-05-14 17:56:54 -050061}
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050062
Zane Shelleyd84ed6e2020-06-08 13:41:48 -050063//------------------------------------------------------------------------------
64
Zane Shelley2f263182020-07-10 21:41:21 -050065uint8_t __attrType(pdbg_target* i_trgt)
66{
67 uint8_t attr = 0;
68 pdbg_target_get_attribute(i_trgt, "ATTR_TYPE", 1, 1, &attr);
69 return attr;
70}
71
72uint32_t __attrFapiPos(pdbg_target* i_trgt)
73{
74 uint32_t attr = 0;
75 pdbg_target_get_attribute(i_trgt, "ATTR_FAPI_POS", 4, 1, &attr);
76 return attr;
77}
78
79//------------------------------------------------------------------------------
80
81const char* __path(const libhei::Chip& i_chip)
82{
83 return pdbg_target_path((pdbg_target*)i_chip.getChip());
84}
85
86const char* __attn(libhei::AttentionType_t i_attnType)
87{
88 const char* str = "";
89 switch (i_attnType)
90 {
91 case libhei::ATTN_TYPE_CHECKSTOP:
92 str = "CHECKSTOP";
93 break;
94 case libhei::ATTN_TYPE_UNIT_CS:
95 str = "UNIT_CS";
96 break;
97 case libhei::ATTN_TYPE_RECOVERABLE:
98 str = "RECOVERABLE";
99 break;
100 case libhei::ATTN_TYPE_SP_ATTN:
101 str = "SP_ATTN";
102 break;
103 case libhei::ATTN_TYPE_HOST_ATTN:
104 str = "HOST_ATTN";
105 break;
106 default:
107 trace::err("Unsupported attention type: %u", i_attnType);
108 assert(0);
109 }
110 return str;
111}
112
113uint32_t __trgt(const libhei::Signature& i_sig)
114{
115 auto trgt = (pdbg_target*)i_sig.getChip().getChip();
116
117 uint8_t type = __attrType(trgt);
118 uint32_t pos = __attrFapiPos(trgt);
119
120 // Technically, the FapiPos attribute is 32-bit, but not likely to ever go
121 // over 24-bit.
122
123 return type << 24 | (pos & 0xffffff);
124}
125
126uint32_t __sig(const libhei::Signature& i_sig)
127{
128 return i_sig.getId() << 16 | i_sig.getInstance() << 8 | i_sig.getBit();
129}
130
131//------------------------------------------------------------------------------
132
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500133// Returns the chip model/level of the given target. Also, adds the chip
134// model/level to the list of type types needed to initialize the isolator.
135libhei::ChipType_t __getChipType(pdbg_target* i_trgt,
136 std::vector<libhei::ChipType_t>& o_types)
137{
138 libhei::ChipType_t type;
139
140 // START WORKAROUND
141 // TODO: Will need to grab the model/level from the target attributes when
142 // they are available. For now, use ATTR_TYPE to determine which
143 // currently supported value to use supported.
Zane Shelley2f263182020-07-10 21:41:21 -0500144 uint8_t attrType = __attrType(i_trgt);
145 switch (attrType)
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500146 {
147 case 0x05: // PROC
148 type = 0x120DA049;
149 break;
150
151 case 0x4b: // OCMB_CHIP
152 type = 0x160D2000;
153 break;
154
155 default:
Zane Shelley2f263182020-07-10 21:41:21 -0500156 trace::err("Unsupported ATTR_TYPE value: 0x%02x", attrType);
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500157 assert(0);
158 }
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500159 // END WORKAROUND
160
Zane Shelley06e10f92020-08-10 20:35:08 -0500161 // Make sure the model/level list contains unique values only.
162 // This is O(n*n), but the list size will likely be very low, probably
163 // maxing around a half dozen. So, opting for simplicity.
164 if (o_types.end() == std::find(o_types.begin(), o_types.end(), type))
165 {
166 o_types.push_back(type);
167 }
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500168
169 return type;
170}
171
172//------------------------------------------------------------------------------
173
174// Gathers list of active chips to analyze. Also, returns the list of chip types
175// needed to initialize the isolator.
176void __getActiveChips(std::vector<libhei::Chip>& o_chips,
177 std::vector<libhei::ChipType_t>& o_types)
178{
179 // Iterate each processor.
180 pdbg_target* procTrgt;
181 pdbg_for_each_class_target("proc", procTrgt)
182 {
183 // Active processors only.
184 if (PDBG_TARGET_ENABLED != pdbg_target_probe(procTrgt))
185 continue;
186
187 // Add the processor to the list.
188 o_chips.emplace_back(procTrgt, __getChipType(procTrgt, o_types));
189
190 // Iterate the connected OCMBs, if they exist.
191 pdbg_target* ocmbTrgt;
Zane Shelley06e10f92020-08-10 20:35:08 -0500192 pdbg_for_each_target("ocmb", procTrgt, ocmbTrgt)
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500193 {
194 // Active OCMBs only.
195 if (PDBG_TARGET_ENABLED != pdbg_target_probe(ocmbTrgt))
196 continue;
197
198 // Add the OCMB to the list.
199 o_chips.emplace_back(ocmbTrgt, __getChipType(ocmbTrgt, o_types));
200 }
201 }
202
Zane Shelley2f263182020-07-10 21:41:21 -0500203 // For debug, trace out all of the chips found.
204 for (const auto& chip : o_chips)
205 {
206 trace::inf("chip:%s type:0x%0" PRIx32, __path(chip), chip.getType());
207 }
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500208}
209
210//------------------------------------------------------------------------------
211
Zane Shelley2e994bc2020-06-08 14:38:14 -0500212// Initializes the isolator for each specified chip type.
213void __initializeIsolator(const std::vector<libhei::ChipType_t>& i_types)
214{
215 // START WORKAROUND
216 // TODO: The chip data will eventually come from the CHIPDATA section of the
217 // PNOR. Until that support is available, we'll use temporary chip
218 // data files.
219 for (const auto& type : i_types)
220 {
221 switch (type)
222 {
223 case 0x120DA049: // PROC
224 initWithFile(
225 "/usr/share/openpower-hw-diags/chip_data_proc.cdb");
226 break;
227
228 case 0x160D2000: // OCMB_CHIP
229 initWithFile(
230 "/usr/share/openpower-hw-diags/chip_data_ocmb.cdb");
231 break;
232
233 default:
234 trace::err("Unsupported ChipType_t value: 0x%0" PRIx32, type);
235 assert(0);
236 }
237 }
238 // END WORKAROUND
239}
240
241//------------------------------------------------------------------------------
242
Zane Shelley097a71a2020-06-08 15:55:29 -0500243// Takes a signature list that will be filtered and sorted. The first entry in
244// the returned list will be the root cause. If the returned list is empty,
245// analysis failed.
246void __filterRootCause(std::vector<libhei::Signature>& io_list)
247{
Zane Shelley2f263182020-07-10 21:41:21 -0500248 // For debug, trace out the original list of signatures before filtering.
249 for (const auto& sig : io_list)
250 {
251 trace::inf("Signature: %s 0x%0" PRIx32 " %s", __path(sig.getChip()),
252 __sig(sig), __attn(sig.getAttnType()));
253 }
254
Zane Shelley097a71a2020-06-08 15:55:29 -0500255 // Special and host attentions are not supported by this user application.
256 auto newEndItr =
257 std::remove_if(io_list.begin(), io_list.end(), [&](const auto& t) {
258 return (libhei::ATTN_TYPE_SP_ATTN == t.getAttnType() ||
259 libhei::ATTN_TYPE_HOST_ATTN == t.getAttnType());
260 });
261
262 // Shrink the vector, if needed.
263 io_list.resize(std::distance(io_list.begin(), newEndItr));
264
265 // START WORKAROUND
266 // TODO: Filtering should be determined by the RAS Data Files provided by
267 // the host firmware via the PNOR (similar to the Chip Data Files).
268 // Until that support is available, use a rudimentary filter that
269 // first looks for any recoverable attention, then any unit checkstop,
270 // and then any system checkstop. This is built on the premise that
271 // recoverable errors could be the root cause of an system checkstop
272 // attentions. Fortunately, we just need to sort the list by the
273 // greater attention type value.
274 std::sort(io_list.begin(), io_list.end(),
275 [&](const auto& a, const auto& b) {
276 return a.getAttnType() > b.getAttnType();
277 });
278 // END WORKAROUND
279}
280
281//------------------------------------------------------------------------------
282
Zane Shelley9fb73932020-09-15 13:34:57 -0500283bool __logError(const std::vector<libhei::Signature>& i_sigList,
284 const libhei::IsolationData& i_isoData)
285{
286 bool attnFound = false;
287
288 // Get numerical values for the root cause.
289 uint32_t word6 = 0; // [ 0: 7]: chip target type
290 // [ 8:31]: chip FAPI position
291 // uint32_t word7 = 0; // TODO: chip target info
292 uint32_t word8 = 0; // [ 0:15]: node ID
293 // [16:23]: node instance
294 // [24:31]: bit position
295 // uint32_t word9 = 0; // [ 0: 7]: attention type
296
297 if (i_sigList.empty())
298 {
299 trace::inf("No active attentions found");
300 }
301 else
302 {
303 attnFound = true;
304
305 // The root cause attention is the first in the filtered list.
306 libhei::Signature root = i_sigList.front();
307
308 word6 = __trgt(root);
309 word8 = __sig(root);
310
311 trace::inf("Root cause attention: %s 0x%0" PRIx32 " %s",
312 __path(root.getChip()), word8, __attn(root.getAttnType()));
313 }
314
315 // Get the log data.
316 std::map<std::string, std::string> logData;
317 logData["_PID"] = std::to_string(getpid());
318 logData["CHIP_ID"] = std::to_string(word6);
319 logData["SIGNATURE"] = std::to_string(word8);
320
321 // Get access to logging interface and method for creating log.
322 auto bus = sdbusplus::bus::new_default_system();
323
324 // Using direct create method (for additional data)
325 auto method = bus.new_method_call(
326 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
327 "xyz.openbmc_project.Logging.Create", "Create");
328
329 // Attach additional data
330 method.append("org.open_power.HwDiags.Error.Checkstop",
331 "xyz.openbmc_project.Logging.Entry.Level.Error", logData);
332
333 // Log the event.
334 // TODO: Should the reply be handled?
335 bus.call(method);
336
337 return attnFound;
338}
339
340//------------------------------------------------------------------------------
341
342bool analyzeHardware()
Ben Tyner87eabc62020-05-14 17:56:54 -0500343{
Zane Shelley097a71a2020-06-08 15:55:29 -0500344 bool attnFound = false;
Ben Tyner87eabc62020-05-14 17:56:54 -0500345
Zane Shelley2f263182020-07-10 21:41:21 -0500346 trace::inf(">>> enter analyzeHardware()");
347
Zane Shelleyd84ed6e2020-06-08 13:41:48 -0500348 // Get the active chips to be analyzed and their types.
349 std::vector<libhei::Chip> chipList;
350 std::vector<libhei::ChipType_t> chipTypes;
351 __getActiveChips(chipList, chipTypes);
Ben Tyner87eabc62020-05-14 17:56:54 -0500352
Zane Shelley2e994bc2020-06-08 14:38:14 -0500353 // Initialize the isolator for all chip types.
Zane Shelley2f263182020-07-10 21:41:21 -0500354 trace::inf("Initializing isolator: # of types=%u", chipTypes.size());
Zane Shelley2e994bc2020-06-08 14:38:14 -0500355 __initializeIsolator(chipTypes);
356
Zane Shelley097a71a2020-06-08 15:55:29 -0500357 // Isolate attentions.
Zane Shelley2f263182020-07-10 21:41:21 -0500358 trace::inf("Isolating errors: # of chips=%u", chipList.size());
Zane Shelley097a71a2020-06-08 15:55:29 -0500359 libhei::IsolationData isoData{};
360 libhei::isolate(chipList, isoData);
Ben Tyner87eabc62020-05-14 17:56:54 -0500361
Zane Shelley2f263182020-07-10 21:41:21 -0500362 // Filter signatures to determine root cause. We'll need to make a copy of
363 // the list so that the original list is maintained for the log.
Zane Shelley097a71a2020-06-08 15:55:29 -0500364 std::vector<libhei::Signature> sigList{isoData.getSignatureList()};
365 __filterRootCause(sigList);
366
Zane Shelley9fb73932020-09-15 13:34:57 -0500367 // Create and commit a log.
368 attnFound = __logError(sigList, isoData);
Ben Tyner87eabc62020-05-14 17:56:54 -0500369
Zane Shelley097a71a2020-06-08 15:55:29 -0500370 // All done, clean up the isolator.
Zane Shelley2f263182020-07-10 21:41:21 -0500371 trace::inf("Uninitializing isolator");
Zane Shelley097a71a2020-06-08 15:55:29 -0500372 libhei::uninitialize();
Ben Tyner87eabc62020-05-14 17:56:54 -0500373
Zane Shelley2f263182020-07-10 21:41:21 -0500374 trace::inf("<<< exit analyzeHardware()");
375
Zane Shelley097a71a2020-06-08 15:55:29 -0500376 return attnFound;
Ben Tyner0205f3b2020-02-24 10:24:47 -0600377}
378
379} // namespace analyzer