1 extern "C"
2 {
3 #include <libpdbg.h>
4 }
5 
6 #include "create_pel.hpp"
7 #include "dump_utils.hpp"
8 #include "extensions/phal/common_utils.hpp"
9 #include "phal_error.hpp"
10 
11 #include <attributes_info.H>
12 #include <fmt/format.h>
13 #include <libekb.H>
14 #include <libphal.H>
15 
16 #include <nlohmann/json.hpp>
17 #include <phosphor-logging/elog.hpp>
18 
19 #include <algorithm>
20 #include <cstdlib>
21 #include <cstring>
22 #include <iomanip>
23 #include <list>
24 #include <map>
25 #include <sstream>
26 #include <string>
27 
28 namespace openpower
29 {
30 namespace phal
31 {
32 using namespace phosphor::logging;
33 
34 /**
35  * Used to pass buffer to pdbg callback api to get required target
36  * data (attributes) based on given data (attribute).
37  */
38 struct TargetInfo
39 {
40     ATTR_PHYS_BIN_PATH_Type physBinPath;
41     ATTR_LOCATION_CODE_Type locationCode;
42     ATTR_PHYS_DEV_PATH_Type physDevPath;
43     ATTR_MRU_ID_Type mruId;
44 
45     bool deconfigure;
46 
47     TargetInfo()
48     {
49         memset(&physBinPath, '\0', sizeof(physBinPath));
50         memset(&locationCode, '\0', sizeof(locationCode));
51         memset(&physDevPath, '\0', sizeof(physDevPath));
52         mruId = 0;
53         deconfigure = false;
54     }
55 };
56 
57 /**
58  * Used to return in callback function which are used to get
59  * physical path value and it binary format value.
60  *
61  * The value for constexpr defined based on pdbg_target_traverse function usage.
62  */
63 constexpr int continueTgtTraversal = 0;
64 constexpr int requireAttrFound = 1;
65 constexpr int requireAttrNotFound = 2;
66 
67 /**
68  * @brief Used to get target location code from phal device tree
69  *
70  * @param[in] target current device tree target
71  * @param[out] appPrivData used for accessing|storing from|to application
72  *
73  * @return 0 to continue traverse, non-zero to stop traverse
74  */
75 int pdbgCallbackToGetTgtReqAttrsVal(struct pdbg_target* target,
76                                     void* appPrivData)
77 {
78     TargetInfo* targetInfo = static_cast<TargetInfo*>(appPrivData);
79 
80     ATTR_PHYS_BIN_PATH_Type physBinPath;
81     /**
82      * TODO: Issue: phal/pdata#16
83      * Should not use direct pdbg api to read attribute. Need to use DT_GET_PROP
84      * macro for bmc app's and this will call libdt-api api but, it will print
85      * "pdbg_target_get_attribute failed" trace if attribute is not found and
86      * this callback will call recursively by using pdbg_target_traverse() until
87      * find expected attribute based on return code from this callback. Because,
88      * need to do target iteration to get actual attribute (ATTR_PHYS_BIN_PATH)
89      * value when device tree target info doesn't know to read attribute from
90      * device tree. So, Due to this error trace user will get confusion while
91      * looking traces. Hence using pdbg api to avoid trace until libdt-api
92      * provides log level setup.
93      */
94     if (!pdbg_target_get_attribute(
95             target, "ATTR_PHYS_BIN_PATH",
96             std::stoi(dtAttr::fapi2::ATTR_PHYS_BIN_PATH_Spec),
97             dtAttr::fapi2::ATTR_PHYS_BIN_PATH_ElementCount, physBinPath))
98     {
99         return continueTgtTraversal;
100     }
101 
102     if (std::memcmp(physBinPath, targetInfo->physBinPath,
103                     sizeof(physBinPath)) != 0)
104     {
105         return continueTgtTraversal;
106     }
107 
108     if (DT_GET_PROP(ATTR_LOCATION_CODE, target, targetInfo->locationCode))
109     {
110         log<level::ERR>("Could not read LOCATION_CODE attribute");
111         return requireAttrNotFound;
112     }
113 
114     if (DT_GET_PROP(ATTR_PHYS_DEV_PATH, target, targetInfo->physDevPath))
115     {
116         log<level::ERR>("Could not read PHYS_DEV_PATH attribute");
117         return requireAttrNotFound;
118     }
119 
120     if (DT_GET_PROP(ATTR_MRU_ID, target, targetInfo->mruId))
121     {
122         log<level::ERR>("Could not read MRU_ID attribute");
123         return requireAttrNotFound;
124     }
125 
126     if (targetInfo->deconfigure)
127     {
128         ATTR_HWAS_STATE_Type hwasState;
129         if (DT_GET_PROP(ATTR_HWAS_STATE, target, hwasState))
130         {
131             log<level::ERR>("Could not read HWAS_STATE attribute");
132             return requireAttrNotFound;
133         }
134 
135         log<level::INFO>(fmt::format("Marking target({}) as Non-Functional",
136                                      targetInfo->physDevPath)
137                              .c_str());
138         hwasState.functional = 0;
139 
140         if (DT_SET_PROP(ATTR_HWAS_STATE, target, hwasState))
141         {
142             log<level::ERR>("Could not write HWAS_STATE attribute");
143             return requireAttrNotFound;
144         }
145     }
146 
147     return requireAttrFound;
148 }
149 
150 /**
151  * @brief Used to get target info (attributes data)
152  *
153  * To get target required attributes value using another attribute value
154  * ("PHYS_BIN_PATH" which is present in same target attributes list) by using
155  * "ipdbg_target_traverse" api because, here we have attribute value only and
156  * doesn't have respective device tree target info to get required attributes
157  * values from it attributes list.
158  *
159  * @param[in] physBinPath to pass PHYS_BIN_PATH value
160  * @param[out] targetInfo to pas buufer to fill with required attributes
161  *
162  * @return true on success otherwise false
163  */
164 bool getTgtReqAttrsVal(const std::vector<uint8_t>& physBinPath,
165                        TargetInfo& targetInfo)
166 {
167     std::memcpy(&targetInfo.physBinPath, physBinPath.data(),
168                 sizeof(targetInfo.physBinPath));
169 
170     int ret = pdbg_target_traverse(NULL, pdbgCallbackToGetTgtReqAttrsVal,
171                                    &targetInfo);
172     if (ret == 0)
173     {
174         log<level::ERR>(fmt::format("Given ATTR_PHYS_BIN_PATH value({}) "
175                                     "not found in phal device tree",
176                                     targetInfo.physBinPath)
177                             .c_str());
178         return false;
179     }
180     else if (ret == requireAttrNotFound)
181     {
182         return false;
183     }
184 
185     return true;
186 }
187 } // namespace phal
188 
189 namespace pel
190 {
191 using namespace phosphor::logging;
192 
193 namespace detail
194 {
195 using json = nlohmann::json;
196 
197 // keys need to be unique so using counter value to generate unique key
198 static int counter = 0;
199 
200 // list of debug traces
201 static std::vector<std::pair<std::string, std::string>> traceLog;
202 
203 void processLogTraceCallback(void*, const char* fmt, va_list ap)
204 {
205     va_list vap;
206     va_copy(vap, ap);
207     std::vector<char> logData(1 + std::vsnprintf(nullptr, 0, fmt, ap));
208     std::vsnprintf(logData.data(), logData.size(), fmt, vap);
209     va_end(vap);
210     std::string logstr(logData.begin(), logData.end());
211 
212     log<level::INFO>(logstr.c_str());
213 
214     char timeBuf[80];
215     time_t t = time(0);
216     tm myTm{};
217     gmtime_r(&t, &myTm);
218     strftime(timeBuf, 80, "%Y-%m-%d %H:%M:%S", &myTm);
219 
220     // key values need to be unique for PEL
221     // TODO #openbmc/dev/issues/1563
222     // If written to Json no need to worry about unique KEY
223     std::stringstream str;
224     str << std::setfill('0');
225     str << "LOG" << std::setw(3) << counter;
226     str << " " << timeBuf;
227     traceLog.emplace_back(std::make_pair(str.str(), std::move(logstr)));
228     counter++;
229 }
230 
231 /**
232  * @brief GET PEL priority from pHAL priority
233  *
234  * The pHAL callout priority is in different format than PEL format
235  * so, this api is used to return current phal supported priority into
236  * PEL expected format.
237  *
238  * @param[in] phalPriority used to pass phal priority format string
239  *
240  * @return pel priority format string else empty if failure
241  *
242  * @note For "NONE" returning "L" (LOW)
243  */
244 static std::string getPelPriority(const std::string& phalPriority)
245 {
246     const std::map<std::string, std::string> priorityMap = {
247         {"HIGH", "H"}, {"MEDIUM", "M"}, {"LOW", "L"}, {"NONE", "L"}};
248 
249     auto it = priorityMap.find(phalPriority);
250     if (it == priorityMap.end())
251     {
252         log<level::ERR>(fmt::format("Unsupported phal priority({}) is given "
253                                     "to get pel priority format",
254                                     phalPriority)
255                             .c_str());
256         return "H";
257     }
258 
259     return it->second;
260 }
261 
262 void processIplErrorCallback(const ipl_error_info& errInfo)
263 {
264     log<level::INFO>(
265         fmt::format("processIplErrorCallback: Error type({})", errInfo.type)
266             .c_str());
267 
268     if (errInfo.type == IPL_ERR_OK)
269     {
270         // reset trace log and exit
271         reset();
272         return;
273     }
274 
275     if (errInfo.type == IPL_ERR_SBE_BOOT)
276     {
277         processSbeBootError();
278         return;
279     }
280 
281     // TODO: Keeping the existing behaviour now
282     // Handle errors based on special reason codes once support is available
283     processBootError(false);
284 }
285 
286 void processBootError(bool status)
287 {
288     log<level::INFO>("processBootError ", entry("STATUS=%d", status));
289     try
290     {
291         // return If no failure during hwp execution
292         if (status)
293             return;
294 
295         // Collecting ffdc details from phal
296         FFDC ffdc;
297         libekb_get_ffdc(ffdc);
298 
299         log<level::INFO>(
300             fmt::format("PHAL FFDC: Return Message[{}]", ffdc.message).c_str());
301 
302         // To store callouts details in json format as per pel expectation.
303         json jsonCalloutDataList;
304         jsonCalloutDataList = json::array();
305 
306         // To store phal trace and other additional data about ffdc.
307         FFDCData pelAdditionalData;
308 
309         if (ffdc.ffdc_type == FFDC_TYPE_HWP)
310         {
311             // Adding hardware procedures return code details
312             pelAdditionalData.emplace_back("HWP_RC", ffdc.hwp_errorinfo.rc);
313             pelAdditionalData.emplace_back("HWP_RC_DESC",
314                                            ffdc.hwp_errorinfo.rc_desc);
315 
316             // Adding hardware procedures required ffdc data for debug
317             for_each(ffdc.hwp_errorinfo.ffdcs_data.begin(),
318                      ffdc.hwp_errorinfo.ffdcs_data.end(),
319                      [&pelAdditionalData](
320                          std::pair<std::string, std::string>& ele) -> void {
321                          std::string keyWithPrefix("HWP_FFDC_");
322                          keyWithPrefix.append(ele.first);
323 
324                          pelAdditionalData.emplace_back(keyWithPrefix,
325                                                         ele.second);
326                      });
327 
328             // Adding hardware callout details
329             int calloutCount = 0;
330             for_each(ffdc.hwp_errorinfo.hwcallouts.begin(),
331                      ffdc.hwp_errorinfo.hwcallouts.end(),
332                      [&pelAdditionalData, &calloutCount, &jsonCalloutDataList](
333                          const HWCallout& hwCallout) -> void {
334                          calloutCount++;
335                          std::stringstream keyPrefix;
336                          keyPrefix << "HWP_HW_CO_" << std::setfill('0')
337                                    << std::setw(2) << calloutCount << "_";
338 
339                          pelAdditionalData.emplace_back(
340                              std::string(keyPrefix.str()).append("HW_ID"),
341                              hwCallout.hwid);
342 
343                          pelAdditionalData.emplace_back(
344                              std::string(keyPrefix.str()).append("PRIORITY"),
345                              hwCallout.callout_priority);
346 
347                          phal::TargetInfo targetInfo;
348                          phal::getTgtReqAttrsVal(hwCallout.target_entity_path,
349                                                  targetInfo);
350 
351                          std::string locationCode =
352                              std::string(targetInfo.locationCode);
353                          pelAdditionalData.emplace_back(
354                              std::string(keyPrefix.str()).append("LOC_CODE"),
355                              locationCode);
356 
357                          std::string physPath =
358                              std::string(targetInfo.physDevPath);
359                          pelAdditionalData.emplace_back(
360                              std::string(keyPrefix.str()).append("PHYS_PATH"),
361                              physPath);
362 
363                          pelAdditionalData.emplace_back(
364                              std::string(keyPrefix.str()).append("CLK_POS"),
365                              std::to_string(hwCallout.clkPos));
366 
367                          json jsonCalloutData;
368                          jsonCalloutData["LocationCode"] = locationCode;
369                          std::string pelPriority =
370                              getPelPriority(hwCallout.callout_priority);
371                          jsonCalloutData["Priority"] = pelPriority;
372 
373                          if (targetInfo.mruId != 0)
374                          {
375                              jsonCalloutData["MRUs"] = json::array({
376                                  {{"ID", targetInfo.mruId},
377                                   {"Priority", pelPriority}},
378                              });
379                          }
380 
381                          jsonCalloutDataList.emplace_back(jsonCalloutData);
382                      });
383 
384             // Adding CDG (callout, deconfigure and guard) targets details
385             calloutCount = 0;
386             for_each(ffdc.hwp_errorinfo.cdg_targets.begin(),
387                      ffdc.hwp_errorinfo.cdg_targets.end(),
388                      [&pelAdditionalData, &calloutCount,
389                       &jsonCalloutDataList](const CDG_Target& cdg_tgt) -> void {
390                          calloutCount++;
391                          std::stringstream keyPrefix;
392                          keyPrefix << "HWP_CDG_TGT_" << std::setfill('0')
393                                    << std::setw(2) << calloutCount << "_";
394 
395                          phal::TargetInfo targetInfo;
396                          targetInfo.deconfigure = cdg_tgt.deconfigure;
397 
398                          phal::getTgtReqAttrsVal(cdg_tgt.target_entity_path,
399                                                  targetInfo);
400 
401                          std::string locationCode =
402                              std::string(targetInfo.locationCode);
403                          pelAdditionalData.emplace_back(
404                              std::string(keyPrefix.str()).append("LOC_CODE"),
405                              locationCode);
406                          std::string physPath =
407                              std::string(targetInfo.physDevPath);
408                          pelAdditionalData.emplace_back(
409                              std::string(keyPrefix.str()).append("PHYS_PATH"),
410                              physPath);
411 
412                          pelAdditionalData.emplace_back(
413                              std::string(keyPrefix.str()).append("CO_REQ"),
414                              (cdg_tgt.callout == true ? "true" : "false"));
415 
416                          pelAdditionalData.emplace_back(
417                              std::string(keyPrefix.str()).append("CO_PRIORITY"),
418                              cdg_tgt.callout_priority);
419 
420                          pelAdditionalData.emplace_back(
421                              std::string(keyPrefix.str()).append("DECONF_REQ"),
422                              (cdg_tgt.deconfigure == true ? "true" : "false"));
423 
424                          pelAdditionalData.emplace_back(
425                              std::string(keyPrefix.str()).append("GUARD_REQ"),
426                              (cdg_tgt.guard == true ? "true" : "false"));
427 
428                          pelAdditionalData.emplace_back(
429                              std::string(keyPrefix.str()).append("GUARD_TYPE"),
430                              cdg_tgt.guard_type);
431 
432                          json jsonCalloutData;
433                          jsonCalloutData["LocationCode"] = locationCode;
434                          std::string pelPriority =
435                              getPelPriority(cdg_tgt.callout_priority);
436                          jsonCalloutData["Priority"] = pelPriority;
437 
438                          if (targetInfo.mruId != 0)
439                          {
440                              jsonCalloutData["MRUs"] = json::array({
441                                  {{"ID", targetInfo.mruId},
442                                   {"Priority", pelPriority}},
443                              });
444                          }
445                          jsonCalloutData["Deconfigured"] = cdg_tgt.deconfigure;
446                          jsonCalloutData["Guarded"] = cdg_tgt.guard;
447 
448                          jsonCalloutDataList.emplace_back(jsonCalloutData);
449                      });
450         }
451         else if ((ffdc.ffdc_type != FFDC_TYPE_NONE) &&
452                  (ffdc.ffdc_type != FFDC_TYPE_UNSUPPORTED))
453         {
454             log<level::ERR>(
455                 fmt::format("Unsupported phal FFDC type to create PEL. "
456                             "MSG: {}",
457                             ffdc.message)
458                     .c_str());
459         }
460 
461         // Adding collected phal logs into PEL additional data
462         for_each(traceLog.begin(), traceLog.end(),
463                  [&pelAdditionalData](
464                      std::pair<std::string, std::string>& ele) -> void {
465                      pelAdditionalData.emplace_back(ele.first, ele.second);
466                  });
467 
468         // TODO: #ibm-openbmc/dev/issues/2595 : Once enabled this support,
469         // callout details is not required to sort in H,M and L orders which
470         // are expected by pel because, pel will take care for sorting callouts
471         // based on priority so, now adding support to send callout in order
472         // i.e High -> Medium -> Low.
473         std::sort(
474             jsonCalloutDataList.begin(), jsonCalloutDataList.end(),
475             [](const json& aEle, const json& bEle) -> bool {
476                 // Considering b element having higher priority than a element
477                 // or Both element will be same priorty (to keep same order
478                 // which are given by phal when two callouts are having same
479                 // priority)
480                 if (((aEle["Priority"] == "M") && (bEle["Priority"] == "H")) ||
481                     ((aEle["Priority"] == "L") &&
482                      ((bEle["Priority"] == "H") ||
483                       (bEle["Priority"] == "M"))) ||
484                     (aEle["Priority"] == bEle["Priority"]))
485                 {
486                     return false;
487                 }
488 
489                 // Considering a element having higher priority than b element
490                 return true;
491             });
492 
493         openpower::pel::createBootErrorPEL(pelAdditionalData,
494                                            jsonCalloutDataList);
495     }
496     catch (const std::exception& ex)
497     {
498         reset();
499         throw ex;
500     }
501     reset();
502 }
503 
504 void processSbeBootError()
505 {
506     log<level::INFO>("processSbeBootError : Entered ");
507 
508     using namespace openpower::phal::sbe;
509     using namespace openpower::phal::exception;
510 
511     // To store phal trace and other additional data about ffdc.
512     FFDCData pelAdditionalData;
513 
514     // Adding collected phal logs into PEL additional data
515     for_each(
516         traceLog.begin(), traceLog.end(),
517         [&pelAdditionalData](std::pair<std::string, std::string>& ele) -> void {
518             pelAdditionalData.emplace_back(ele.first, ele.second);
519         });
520 
521     // reset the trace log and counter
522     reset();
523 
524     // get primary processor to collect FFDC/Dump information.
525     struct pdbg_target* procTarget;
526     pdbg_for_each_class_target("proc", procTarget)
527     {
528         if (openpower::phal::isPrimaryProc(procTarget))
529             break;
530         procTarget = nullptr;
531     }
532     // check valid primary processor is available
533     if (procTarget == nullptr)
534     {
535         log<level::ERR>("processSbeBootError: fail to get primary processor");
536         // Initialise the SRC6 with default data, not used in this use case.
537         pelAdditionalData.emplace_back("SRC6", "00000000");
538         openpower::pel::createPEL(
539             "org.open_power.Processor.Error.SbeBootFailure", pelAdditionalData);
540         return;
541     }
542     // SBE error object.
543     sbeError_t sbeError;
544     bool dumpIsRequired = false;
545 
546     try
547     {
548         // Capture FFDC information on primary processor
549         sbeError = captureFFDC(procTarget);
550     }
551     catch (const std::exception& e)
552     {
553         // Fail to collect FFDC information , trigger Dump
554         log<level::ERR>(
555             fmt::format("captureFFDC: Exception{}", e.what()).c_str());
556         dumpIsRequired = true;
557     }
558 
559     std::string event;
560 
561     if ((sbeError.errType() == SBE_FFDC_NO_DATA) ||
562         (sbeError.errType() == SBE_CMD_TIMEOUT) || (dumpIsRequired))
563     {
564         event = "org.open_power.Processor.Error.SbeBootTimeout";
565         dumpIsRequired = true;
566     }
567     else
568     {
569         event = "org.open_power.Processor.Error.SbeBootFailure";
570     }
571     // SRC6 : [0:15] chip position
572     uint32_t index = pdbg_target_index(procTarget);
573     pelAdditionalData.emplace_back("SRC6", std::to_string(index << 16));
574     // Create SBE Error with FFDC data.
575     auto logId = createSbeErrorPEL(event, sbeError, pelAdditionalData);
576 
577     if (dumpIsRequired)
578     {
579         using namespace openpower::phal::dump;
580         DumpParameters dumpParameters = {logId, index, SBE_DUMP_TIMEOUT,
581                                          DumpType::SBE};
582         try
583         {
584             requestDump(dumpParameters);
585         }
586         catch (const std::runtime_error& e)
587         {
588             // Allowing call back to handle the error gracefully.
589             log<level::ERR>("Dump collection failed");
590             // TODO revist error handling.
591         }
592     }
593 }
594 
595 void reset()
596 {
597     // reset the trace log and counter
598     traceLog.clear();
599     counter = 0;
600 }
601 
602 void pDBGLogTraceCallbackHelper(int, const char* fmt, va_list ap)
603 {
604     processLogTraceCallback(NULL, fmt, ap);
605 }
606 } // namespace detail
607 
608 static inline uint8_t getLogLevelFromEnv(const char* env, const uint8_t dValue)
609 {
610     auto logLevel = dValue;
611     try
612     {
613         if (const char* env_p = std::getenv(env))
614         {
615             logLevel = std::stoi(env_p);
616         }
617     }
618     catch (const std::exception& e)
619     {
620         log<level::ERR>(("Conversion Failure"), entry("ENVIRONMENT=%s", env),
621                         entry("EXCEPTION=%s", e.what()));
622     }
623     return logLevel;
624 }
625 
626 void addBootErrorCallbacks()
627 {
628     // Get individual phal repos log level from environment variable
629     // and update the  log level.
630     pdbg_set_loglevel(getLogLevelFromEnv("PDBG_LOG", PDBG_INFO));
631     libekb_set_loglevel(getLogLevelFromEnv("LIBEKB_LOG", LIBEKB_LOG_IMP));
632     ipl_set_loglevel(getLogLevelFromEnv("IPL_LOG", IPL_INFO));
633 
634     // add callback for debug traces
635     pdbg_set_logfunc(detail::pDBGLogTraceCallbackHelper);
636     libekb_set_logfunc(detail::processLogTraceCallback, NULL);
637     ipl_set_logfunc(detail::processLogTraceCallback, NULL);
638 
639     // add callback for ipl failures
640     ipl_set_error_callback_func(detail::processIplErrorCallback);
641 }
642 
643 } // namespace pel
644 } // namespace openpower
645