blob: d1a2469e617392178c0c1b24ea52cabf321c8c86 [file] [log] [blame]
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -05001/**
2 * Copyright © 2021 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Williams2544b412022-10-04 08:41:06 -050017extern "C"
18{
Jayanth Othayoth66e186d2021-06-15 22:44:35 -050019#include <libpdbg.h>
20}
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050021
Jayanth Othayothc74c2202021-06-04 06:42:43 -050022#include "fapi_data_process.hpp"
23#include "pel.hpp"
Jayanth Othayoth66e186d2021-06-15 22:44:35 -050024#include "sbe_ffdc_handler.hpp"
Jayanth Othayothc74c2202021-06-04 06:42:43 -050025#include "temporary_file.hpp"
26
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -050027#include <ekb/hwpf/fapi2/include/return_code_defs.H>
Jayanth Othayotheff307e2021-07-15 03:27:01 -050028#include <libekb.H>
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050029
30#include <phosphor-logging/log.hpp>
31
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050032#include <format>
Patrick Williams2544b412022-10-04 08:41:06 -050033#include <new>
34
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050035namespace openpower
36{
37namespace pels
38{
39namespace sbe
40{
41
42using namespace phosphor::logging;
43
devenraob5693742024-02-16 18:33:32 +053044constexpr uint32_t sbeMaxFfdcPackets = 20;
45constexpr uint32_t ffdcPkgOneWord = 1;
46const uint16_t ffdcMagicCode = 0xFFDC;
47
48typedef struct
49{
50 uint32_t magic_bytes:16;
51 uint32_t lengthinWords:16;
52 uint32_t seqId:16;
53 uint32_t cmdClass:8;
54 uint32_t cmd:8;
55 uint32_t fapiRc;
56} __attribute__((packed)) fapiFfdcBufType;
57
Jayanth Othayoth742b00b2022-06-30 05:16:57 -050058SbeFFDC::SbeFFDC(const AdditionalData& aData, const PelFFDC& files) :
59 ffdcType(FFDC_TYPE_NONE)
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050060{
61 log<level::INFO>("SBE FFDC processing requested");
62
63 // SRC6 field in the additional data contains Processor position
64 // associated to the SBE FFDC
65 //"[0:15] chip position"
66 auto src6 = aData.getValue("SRC6");
67 if (src6 == std::nullopt)
68 {
69 log<level::ERR>("Fail to extract SRC6 data: failing to get proc index");
70 return;
71 }
72 try
73 {
Dhruvaraj Subhashchandran8edad2a2022-06-22 13:56:40 -050074 procPos = (std::stoi(src6.value()) & 0xFFFF0000) >> 16;
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050075 }
Patrick Williams66491c62021-10-06 12:23:37 -050076 catch (const std::exception& err)
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050077 {
78 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050079 std::format("Conversion failure errormsg({})", err.what()).c_str());
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050080 return;
81 }
82
83 if (files.empty())
84 {
85 log<level::INFO>("SbeFFDC : No files found, skipping ffdc processing");
86 return;
87 }
88
89 for (const auto& file : files)
90 {
91 if ((file.format == UserDataFormat::custom) &&
92 (file.subType == sbeFFDCSubType))
93 {
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -050094 // Process SBE file.
95 parse(file.fd);
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -050096 }
97 }
98}
99
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500100void SbeFFDC::parse(int fd)
101{
102 log<level::INFO>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500103 std::format("SBE FFDC file fd:({}), parsing started", fd).c_str());
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500104
105 uint32_t ffdcBufOffset = 0;
106 uint32_t pktCount = 0;
107 sbeFfdcPacketType ffdcPkt;
108
109 // get SBE FFDC data.
110 auto ffdcData = util::readFD(fd);
111 if (ffdcData.empty())
112 {
113 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500114 std::format("Empty SBE FFDC file fd:({}), skipping", fd).c_str());
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500115 return;
116 }
117
118 while ((ffdcBufOffset < ffdcData.size()) && (sbeMaxFfdcPackets != pktCount))
119 {
120 // Next un-extracted FFDC Packet
121 fapiFfdcBufType* ffdc =
122 reinterpret_cast<fapiFfdcBufType*>(ffdcData.data() + ffdcBufOffset);
123 auto magicBytes = ntohs(ffdc->magic_bytes);
124 auto lenWords = ntohs(ffdc->lengthinWords);
125 auto fapiRc = ntohl(ffdc->fapiRc);
126
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500127 auto msg = std::format("FFDC magic: {} length in words:{} Fapirc:{}",
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500128 magicBytes, lenWords, fapiRc);
129 log<level::INFO>(msg.c_str());
130
131 if (magicBytes != ffdcMagicCode)
132 {
133 log<level::ERR>("Invalid FFDC magic code in Header: Skipping ");
134 return;
135 }
136 ffdcPkt.fapiRc = fapiRc;
137 // Not interested in the first 2 words (these are not ffdc)
138 auto pktLenWords = lenWords - (2 * ffdcPkgOneWord);
139 ffdcPkt.ffdcLengthInWords = pktLenWords;
140 if (pktLenWords)
141 {
142 // Memory freeing will be taking care by ffdcPkt structure
143 // destructor
144 ffdcPkt.ffdcData = new uint32_t[pktLenWords];
145 memcpy(ffdcPkt.ffdcData,
146 ((reinterpret_cast<uint32_t*>(ffdc)) +
147 (2 * ffdcPkgOneWord)), // skip first 2 words
148 (pktLenWords * sizeof(uint32_t)));
149 }
150 else
151 {
152 log<level::ERR>("FFDC packet size is zero skipping");
153 return;
154 }
155
156 // SBE FFDC processing is not required for SBE Plat errors RCs.
157 // Plat errors processing is driven by SBE provided user data
158 // plugins, which need to link with PEL tool infrastructure.
159 if (ffdcPkt.fapiRc != fapi2::FAPI2_RC_PLAT_ERR_SEE_DATA)
160 {
161 process(ffdcPkt);
162 }
Jayanth Othayothfb9b8112021-10-07 04:10:18 -0500163 else
164 {
165 log<level::INFO>("SBE FFDC: Internal FFDC packet");
166 }
167
168 // Update Buffer offset in Bytes
169 ffdcBufOffset += lenWords * sizeof(uint32_t);
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500170 ++pktCount;
171 }
172 if (pktCount == sbeMaxFfdcPackets)
173 {
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500174 log<level::ERR>(std::format("Received more than the limit of ({})"
Jayanth Othayoth0866c3f2021-06-07 07:06:20 -0500175 " FFDC packets, processing only ({})",
176 sbeMaxFfdcPackets, pktCount)
177 .c_str());
178 }
179}
180
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500181void SbeFFDC::process(const sbeFfdcPacketType& ffdcPkt)
182{
183 using json = nlohmann::json;
184
185 // formated FFDC data structure after FFDC packet processing
186 FFDC ffdc;
187
Jayanth Othayoth66e186d2021-06-15 22:44:35 -0500188 if (!pdbg_targets_init(NULL))
189 {
190 log<level::ERR>("pdbg_targets_init failed, skipping ffdc processing");
191 return;
192 }
193
Jayanth Othayotheff307e2021-07-15 03:27:01 -0500194 if (libekb_init())
195 {
196 log<level::ERR>("libekb_init failed, skipping ffdc processing");
197 return;
198 }
199
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500200 try
201 {
202 // libekb provided wrapper function to convert SBE FFDC
203 // in to known ffdc structure.
204 libekb_get_sbe_ffdc(ffdc, ffdcPkt, procPos);
205 }
206 catch (...)
207 {
208 log<level::ERR>("libekb_get_sbe_ffdc failed, skipping ffdc processing");
209 return;
210 }
211
Jayanth Othayoth742b00b2022-06-30 05:16:57 -0500212 // update FFDC type class membeir for hwp specific packet
213 // Assumption SBE FFDC contains only one hwp FFDC packet.
214 ffdcType = ffdc.ffdc_type;
215
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500216 // To store callouts details in json format as per pel expectation.
217 json pelJSONFmtCalloutDataList;
218 pelJSONFmtCalloutDataList = json::array();
219
220 // To store other user data from FFDC.
221 openpower::pels::phal::FFDCData ffdcUserData;
222
223 // Get FFDC and required info to include in PEL
224 openpower::pels::phal::convertFAPItoPELformat(
225 ffdc, pelJSONFmtCalloutDataList, ffdcUserData);
226
227 // Get callout information and sore in to file.
228 auto calloutData = pelJSONFmtCalloutDataList.dump();
229 util::TemporaryFile ffdcFile(calloutData.c_str(), calloutData.size());
230
231 // Create json callout type pel FFDC file structre.
232 PelFFDCfile pf;
233 pf.format = openpower::pels::UserDataFormat::json;
234 pf.subType = openpower::pels::jsonCalloutSubtype;
235 pf.version = 0x01;
236 pf.fd = ffdcFile.getFd();
237 ffdcFiles.push_back(pf);
238
239 // save the file path to delete the file after usage.
Matt Spinler8c7bb862023-08-31 14:32:22 -0500240 paths.emplace_back(ffdcFile.getPath(), pf.fd);
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500241
242 // Format ffdc user data and create new file.
243 std::string data;
244 for (auto& d : ffdcUserData)
245 {
246 data += d.first + " = " + d.second + "\n";
247 }
248 util::TemporaryFile pelDataFile(data.c_str(), data.size());
249 PelFFDCfile pdf;
250 pdf.format = openpower::pels::UserDataFormat::text;
251 pdf.version = 0x01;
252 pdf.fd = pelDataFile.getFd();
Matt Spinlerbe952d22022-07-01 11:30:11 -0500253 pdf.subType = 0;
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500254 ffdcFiles.push_back(pdf);
255
Matt Spinler8c7bb862023-08-31 14:32:22 -0500256 paths.emplace_back(pelDataFile.getPath(), pdf.fd);
Jayanth Othayothc74c2202021-06-04 06:42:43 -0500257}
258
Jayanth Othayoth742b00b2022-06-30 05:16:57 -0500259std::optional<LogSeverity> SbeFFDC::getSeverity()
260{
261 if (ffdcType == FFDC_TYPE_SPARE_CLOCK_INFO)
262 {
263 log<level::INFO>(
264 "Found spare clock error, changing severity to informational");
265 return LogSeverity::Informational;
266 }
267 return std::nullopt;
268}
269
Jayanth Othayothe8bdeea2021-06-03 03:01:16 -0500270} // namespace sbe
271} // namespace pels
272} // namespace openpower