xref: /openbmc/phosphor-logging/extensions/openpower-pels/sbe_ffdc_handler.cpp (revision 40fb54935ce7367636a7156039396ee91cc4d5e2)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2021 IBM Corporation
3 
4 extern "C"
5 {
6 #include <libpdbg.h>
7 }
8 
9 #include "fapi_data_process.hpp"
10 #include "pel.hpp"
11 #include "sbe_ffdc_handler.hpp"
12 #include "temporary_file.hpp"
13 
14 #include <ekb/hwpf/fapi2/include/return_code_defs.H>
15 #include <ekb/hwpf/fapi2/include/target_types.H>
16 #include <libekb.H>
17 
18 #include <phosphor-logging/lg2.hpp>
19 
20 #include <new>
21 
22 namespace openpower
23 {
24 namespace pels
25 {
26 namespace sbe
27 {
28 
29 constexpr uint32_t sbeMaxFfdcPackets = 20;
30 constexpr uint16_t p10FfdcMagicCode = 0xFFDC;
31 constexpr uint16_t pozFfdcMagicCode = 0xFBAD;
32 constexpr uint16_t p10FfdcSkipWords = 2;
33 constexpr uint16_t pozFfdcSkipWords = 3;
34 
35 struct p10FfdcHeader
36 {
37     uint32_t magic_bytes:16;
38     uint32_t lengthinWords:16;
39     uint32_t seqId:16;
40     uint32_t cmdClass:8;
41     uint32_t cmd:8;
42     uint32_t fapiRc;
43 } __attribute__((packed));
44 
45 struct pozFfdcHeader
46 {
47     uint32_t magicByte:16;
48     uint32_t lengthinWords:16;
49     uint32_t seqId:16;
50     uint32_t cmdClass:8;
51     uint32_t cmd:8;
52     uint32_t slid:16;
53     uint32_t severity:8;
54     uint32_t chipId:8;
55     uint32_t fapiRc;
56 } __attribute__((packed));
57 
58 using namespace phosphor::logging;
59 
SbeFFDC(const AdditionalData & aData,const PelFFDC & files)60 SbeFFDC::SbeFFDC(const AdditionalData& aData, const PelFFDC& files) :
61     ffdcType(FFDC_TYPE_NONE), chipType(fapi2::TARGET_TYPE_PROC_CHIP)
62 {
63     lg2::info("SBE FFDC processing requested");
64 
65     // SRC6 field in the additional data contains Processor position
66     // associated to the SBE FFDC
67     //"[0:15] chip position"
68     auto src6 = aData.getValue("SRC6");
69     if (src6 == std::nullopt)
70     {
71         lg2::error("Fail to extract SRC6 data: failing to get proc index");
72         return;
73     }
74     try
75     {
76         chipPos = (std::stoi(src6.value()) & 0xFFFF0000) >> 16;
77     }
78     catch (const std::exception& err)
79     {
80         lg2::error("Conversion failure errormsg({ERR})", "ERR", err);
81         return;
82     }
83     auto type = aData.getValue("CHIP_TYPE");
84     if (type != std::nullopt)
85     {
86         try
87         {
88             chipType = std::stoi(type.value());
89         }
90         catch (const std::exception& err)
91         {
92             lg2::error("Conversion failure errormsg({ERR})", "ERR", err);
93             return;
94         }
95     }
96 
97     if (files.empty())
98     {
99         lg2::info("SbeFFDC : No files found, skipping ffdc processing");
100         return;
101     }
102 
103     for (const auto& file : files)
104     {
105         if ((file.format == UserDataFormat::custom) &&
106             (file.subType == sbeFFDCSubType))
107         {
108             // Process SBE file.
109             parse(file.fd);
110         }
111     }
112 }
113 
parse(int fd)114 void SbeFFDC::parse(int fd)
115 {
116     lg2::info("SBE FFDC file fd:({FD}), parsing started", "FD", fd);
117 
118     uint32_t ffdcBufOffset = 0;
119     uint32_t pktCount = 0;
120 
121     // get SBE FFDC data.
122     auto ffdcData = util::readFD(fd);
123     if (ffdcData.empty())
124     {
125         lg2::error("Empty SBE FFDC file fd:({FD}), skipping", "FD", fd);
126         return;
127     }
128 
129     while ((ffdcBufOffset < ffdcData.size()) && (sbeMaxFfdcPackets != pktCount))
130     {
131         sbeFfdcPacketType ffdcPkt;
132         // Next un-extracted FFDC Packet
133         uint16_t magicBytes =
134             *(reinterpret_cast<uint16_t*>(ffdcData.data() + ffdcBufOffset));
135         magicBytes = ntohs(magicBytes);
136         uint32_t pktLenWords = 0;
137         uint16_t lenWords = 0;
138         if (magicBytes == p10FfdcMagicCode)
139         {
140             p10FfdcHeader* ffdc = reinterpret_cast<p10FfdcHeader*>(
141                 ffdcData.data() + ffdcBufOffset);
142             lenWords = ntohs(ffdc->lengthinWords);
143             auto fapiRc = ntohl(ffdc->fapiRc);
144 
145             lg2::info(
146                 "P10 FFDC magic: {MAGIC_BYTES} length in words:{LEN_WORDS} "
147                 "Fapirc:{FAPI_RC}",
148                 "MAGIC_BYTES", magicBytes, "LEN_WORDS", lenWords, "FAPI_RC",
149                 fapiRc);
150 
151             ffdcPkt.fapiRc = fapiRc;
152             // Not interested in the first 2 words (these are not ffdc)
153             pktLenWords = lenWords - p10FfdcSkipWords;
154             ffdcPkt.ffdcLengthInWords = pktLenWords;
155             if (pktLenWords)
156             {
157                 ffdcPkt.ffdcData = new uint32_t[pktLenWords];
158                 memcpy(ffdcPkt.ffdcData,
159                        ((reinterpret_cast<uint32_t*>(ffdc)) + p10FfdcSkipWords),
160                        (pktLenWords * sizeof(uint32_t)));
161             }
162             else
163             {
164                 lg2::error("FFDC packet size is zero skipping");
165                 return;
166             }
167             pktCount++;
168         }
169         else if (magicBytes == pozFfdcMagicCode)
170         {
171             pozFfdcHeader* ffdc = reinterpret_cast<pozFfdcHeader*>(
172                 ffdcData.data() + ffdcBufOffset);
173             lenWords = ntohs(ffdc->lengthinWords);
174             auto fapiRc = ntohl(ffdc->fapiRc);
175 
176             lg2::info(
177                 "P0Z FFDC magic: {MAGIC_BYTES} length in words:{LEN_WORDS} "
178                 "Fapirc:{FAPI_RC}",
179                 "MAGIC_BYTES", magicBytes, "LEN_WORDS", lenWords, "FAPI_RC",
180                 fapiRc);
181 
182             ffdcPkt.fapiRc = fapiRc;
183             // Not interested in the first 3 words (these are not ffdc)
184             pktLenWords = lenWords - pozFfdcSkipWords;
185             ffdcPkt.ffdcLengthInWords = pktLenWords;
186             if (pktLenWords)
187             {
188                 ffdcPkt.ffdcData = new uint32_t[pktLenWords];
189                 memcpy(ffdcPkt.ffdcData,
190                        ((reinterpret_cast<uint32_t*>(ffdc)) + pozFfdcSkipWords),
191                        (pktLenWords * sizeof(uint32_t)));
192             }
193             else
194             {
195                 lg2::error("FFDC packet size is zero skipping");
196                 return;
197             }
198         }
199         else
200         {
201             lg2::error("Invalid FFDC magic code in Header: Skipping ");
202             return;
203         }
204 
205         // SBE FFDC processing is not required for SBE Plat errors RCs.
206         // Plat errors processing is driven by SBE provided user data
207         // plugins, which need to link with PEL tool infrastructure.
208         if (ffdcPkt.fapiRc != fapi2::FAPI2_RC_PLAT_ERR_SEE_DATA)
209         {
210             process(ffdcPkt);
211         }
212         else
213         {
214             lg2::info("SBE FFDC: Internal FFDC packet");
215         }
216 
217         // Update Buffer offset in Bytes
218         ffdcBufOffset += lenWords * sizeof(uint32_t);
219     }
220     if (pktCount == sbeMaxFfdcPackets)
221     {
222         lg2::error("Received more than the limit of ({SBEMAXFFDCPACKETS}) FFDC "
223                    "packets, processing only ({PKTCOUNT})",
224                    "SBEMAXFFDCPACKETS", sbeMaxFfdcPackets, "PKTCOUNT",
225                    pktCount);
226     }
227 }
228 
process(const sbeFfdcPacketType & ffdcPkt)229 void SbeFFDC::process(const sbeFfdcPacketType& ffdcPkt)
230 {
231     using json = nlohmann::json;
232 
233     // formated FFDC data structure after FFDC packet processing
234     FFDC ffdc;
235 
236     try
237     {
238         // libekb provided wrapper function to convert SBE FFDC
239         // in to known ffdc structure.
240         libekb_get_sbe_ffdc(ffdc, ffdcPkt, chipPos, chipType);
241     }
242     catch (...)
243     {
244         lg2::error("libekb_get_sbe_ffdc failed, skipping ffdc processing");
245         return;
246     }
247 
248     // update FFDC type class membeir for hwp specific packet
249     // Assumption SBE FFDC contains only one hwp FFDC packet.
250     ffdcType = ffdc.ffdc_type;
251 
252     // To store callouts details in json format as per pel expectation.
253     json pelJSONFmtCalloutDataList;
254     pelJSONFmtCalloutDataList = json::array();
255 
256     // To store other user data from FFDC.
257     openpower::pels::phal::FFDCData ffdcUserData;
258 
259     // Get FFDC and required info to include in PEL
260     openpower::pels::phal::convertFAPItoPELformat(
261         ffdc, pelJSONFmtCalloutDataList, ffdcUserData);
262 
263     // Get callout information and sore in to file.
264     auto calloutData = pelJSONFmtCalloutDataList.dump();
265     util::TemporaryFile ffdcFile(calloutData.c_str(), calloutData.size());
266 
267     // Create json callout type pel FFDC file structre.
268     PelFFDCfile pf;
269     pf.format = openpower::pels::UserDataFormat::json;
270     pf.subType = openpower::pels::jsonCalloutSubtype;
271     pf.version = 0x01;
272     pf.fd = ffdcFile.getFd();
273     ffdcFiles.push_back(pf);
274 
275     // save the file path to delete the file after usage.
276     paths.emplace_back(ffdcFile.getPath(), pf.fd);
277 
278     // Format ffdc user data and create new file.
279     std::string data;
280     for (auto& d : ffdcUserData)
281     {
282         data += d.first + " = " + d.second + "\n";
283     }
284     util::TemporaryFile pelDataFile(data.c_str(), data.size());
285     PelFFDCfile pdf;
286     pdf.format = openpower::pels::UserDataFormat::text;
287     pdf.version = 0x01;
288     pdf.fd = pelDataFile.getFd();
289     pdf.subType = 0;
290     ffdcFiles.push_back(pdf);
291 
292     paths.emplace_back(pelDataFile.getPath(), pdf.fd);
293 }
294 
getSeverity()295 std::optional<LogSeverity> SbeFFDC::getSeverity()
296 {
297     if (ffdcType == FFDC_TYPE_SPARE_CLOCK_INFO)
298     {
299         lg2::info(
300             "Found spare clock error, changing severity to informational");
301         return LogSeverity::Informational;
302     }
303     return std::nullopt;
304 }
305 
306 } // namespace sbe
307 } // namespace pels
308 } // namespace openpower
309