blob: b2365a9d8703c11137e5c0580562638fe830b3d3 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 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 */
Matt Spinler03c1d912019-07-10 14:12:15 -050016#include "user_header.hpp"
17
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080018#include "json_utils.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050019#include "pel_types.hpp"
Aatirc1489352019-12-09 13:13:20 -060020#include "pel_values.hpp"
Matt Spinlerfdb6a202019-09-20 14:09:20 -050021#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050022
Sumit Kumar50bfa692022-01-06 06:48:26 -060023#include <fmt/format.h>
24
Aatir Manzurad0e0472019-10-07 13:18:37 -050025#include <iostream>
Matt Spinler03c1d912019-07-10 14:12:15 -050026#include <phosphor-logging/log.hpp>
27
28namespace openpower
29{
30namespace pels
31{
32
Aatirc1489352019-12-09 13:13:20 -060033namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050034using namespace phosphor::logging;
35
Matt Spinlercf5a8d02019-09-05 12:58:53 -050036void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050037{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050038 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
39 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060040 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050041}
42
Matt Spinler06885452019-11-06 10:35:42 -060043void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050044{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050045 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
46 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060047 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050048}
49
Matt Spinlerfdb6a202019-09-20 14:09:20 -050050UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050051 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050052 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050053 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050054{
55 _header.id = static_cast<uint16_t>(SectionID::userHeader);
56 _header.size = UserHeader::flattenedSize();
57 _header.version = userHeaderVersion;
58 _header.subType = 0;
59 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
60
Matt Spinler23970b02022-02-25 16:34:46 -060061 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050062
Sumit Kumar50bfa692022-01-06 06:48:26 -060063 // Check for additional data - PEL_SUBSYSTEM
64 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
65 if (ss)
66 {
67 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Matt Spinler23970b02022-02-25 16:34:46 -060068 std::string subsystemString =
Sumit Kumar50bfa692022-01-06 06:48:26 -060069 pv::getValue(eventSubsystem, pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060070 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060071 {
72 log<level::WARNING>(
Matt Spinler23970b02022-02-25 16:34:46 -060073 fmt::format(
74 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {:#X}",
75 eventSubsystem)
Sumit Kumar50bfa692022-01-06 06:48:26 -060076 .c_str());
77 }
78 else
79 {
Matt Spinler23970b02022-02-25 16:34:46 -060080 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060081 }
82 }
Matt Spinler23970b02022-02-25 16:34:46 -060083 else
84 {
85 subsys = entry.subsystem;
86 }
87
88 if (subsys)
89 {
90 _eventSubsystem = *subsys;
91 }
92 else
93 {
94 // Gotta use something, how about 'others'.
95 log<level::WARNING>(
96 "No PEL subystem value supplied for error, using 'others'");
97 _eventSubsystem = 0x70;
98 }
Sumit Kumar50bfa692022-01-06 06:48:26 -060099
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500100 _eventScope = entry.eventScope.value_or(
101 static_cast<uint8_t>(EventScope::entirePlatform));
102
Matt Spinleraadccc82020-04-10 14:33:42 -0500103 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500104 bool mfgSevStatus = false;
105 bool mfgActionFlagStatus = false;
106 std::optional<uint8_t> sev = std::nullopt;
107 uint16_t val = 0;
108
109 // Get the mfg severity & action flags
110 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500111 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500112 if (entry.mfgSeverity)
113 {
114 // Find the mf severity possibly dependent on the system type.
115 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
116 }
117
118 if (entry.mfgActionFlags)
119 {
120 // Find the mfg action flags
121 val = entry.mfgActionFlags.value();
122 }
123
124 if (sev || val)
125 {
126 bool mfgProp = dataIface.getQuiesceOnError();
127 if (mfgProp)
128 {
129 if (sev)
130 {
131 _eventSeverity = *sev;
132 mfgSevStatus = true;
133 }
134
135 if (val)
136 {
137 _actionFlags = val;
138 mfgActionFlagStatus = true;
139 }
140 }
141 }
142 }
143
144 if (!mfgSevStatus)
145 {
146 // Get the severity from the registry if it's there, otherwise get
147 // it from the OpenBMC event log severity value.
148 if (!entry.severity)
149 {
150 _eventSeverity = convertOBMCSeverityToPEL(severity);
151 }
152 else
153 {
154 // Find the severity possibly dependent on the system type.
155 auto sev = getSeverity(entry.severity.value(), dataIface);
156 if (sev)
157 {
158 _eventSeverity = *sev;
159 }
160 else
161 {
162 // Either someone screwed up the message registry
163 // or getSystemNames failed.
164 std::string types;
165 log<level::ERR>(
166 "Failed finding the severity in the message registry",
167 phosphor::logging::entry("ERROR=%s",
168 entry.name.c_str()));
169
170 // Have to choose something, just use informational.
171 _eventSeverity = 0;
172 }
173 }
174 }
175
176 // Convert Critical error (0x50) to Critical Error-System Termination
177 // (0x51), if the AdditionalData is set to SYSTEM_TERM
178 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
179 if ((_eventSeverity & 0xF0) == 0x50)
180 {
181 if (sevLevel.value_or("") == "SYSTEM_TERM")
182 {
183 // Change to Critical Error, System Termination
184 _eventSeverity = 0x51;
185 }
186 }
187
188 if (entry.eventType)
189 {
190 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500191 }
192 else
193 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500194 // There are different default event types for info errors
195 // vs non info ones.
196 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
197 _eventType =
198 (sevType == SeverityType::nonError)
199 ? static_cast<uint8_t>(EventType::miscInformational)
200 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500201 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500202
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500203 _reserved4Byte1 = 0;
204
205 // No uses for problem domain or vector
206 _problemDomain = 0;
207 _problemVector = 0;
208
209 // These will be set in pel_rules::check() if they're still
210 // at the default value.
211 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500212 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500213 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500214 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500215
216 _states = 0;
217
218 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500219 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500220}
221
Matt Spinler03c1d912019-07-10 14:12:15 -0500222UserHeader::UserHeader(Stream& pel)
223{
224 try
225 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500226 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500227 validate();
228 }
229 catch (const std::exception& e)
230 {
231 log<level::ERR>("Cannot unflatten user header",
232 entry("ERROR=%s", e.what()));
233 _valid = false;
234 }
235}
236
237void UserHeader::validate()
238{
239 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500240 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500241 {
242 log<level::ERR>("Invalid user header section ID",
243 entry("ID=0x%X", header().id));
244 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500245 }
246
247 if (header().version != userHeaderVersion)
248 {
249 log<level::ERR>("Invalid user header version",
250 entry("VERSION=0x%X", header().version));
251 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500252 }
253
254 _valid = (failed) ? false : true;
255}
256
Aatir Manzurad0e0472019-10-07 13:18:37 -0500257std::optional<std::string> UserHeader::getJSON() const
258{
259 std::string severity;
260 std::string subsystem;
261 std::string eventScope;
262 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800263 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600264 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
265 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
266 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
267 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800268 actionFlags =
269 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600270
271 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500272 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600273 auto iter = pv::transmissionStates.find(
274 static_cast<TransmissionState>(hostTransmissionState()));
275 if (iter != pv::transmissionStates.end())
276 {
277 hostState = iter->second;
278 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500279 auto iter1 = pv::transmissionStates.find(
280 static_cast<TransmissionState>(hmcTransmissionState()));
281 if (iter1 != pv::transmissionStates.end())
282 {
283 hmcState = iter1->second;
284 }
Matt Spinler455587e2020-01-15 14:31:52 -0600285
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800286 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800287 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
288 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
289 jsonInsert(uh, "Log Committed by",
290 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800291 jsonInsert(uh, "Subsystem", subsystem, 1);
292 jsonInsert(uh, "Event Scope", eventScope, 1);
293 jsonInsert(uh, "Event Severity", severity, 1);
294 jsonInsert(uh, "Event Type", eventType, 1);
295 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600296 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500297 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800298 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500299 return uh;
300}
Matt Spinleraadccc82020-04-10 14:33:42 -0500301
302std::optional<uint8_t> UserHeader::getSeverity(
303 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500304 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500305{
306 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500307 std::vector<std::string> systemNames;
308
309 // getSystemNames makes D-Bus calls, so only call it if we
310 // know we'll need it because there is a system name in the sev list
311 if (std::any_of(severities.begin(), severities.end(),
312 [](const auto& sev) { return !sev.system.empty(); }))
313 {
314 try
315 {
316 systemNames = dataIface.getSystemNames();
317 }
318 catch (const std::exception& e)
319 {
320 log<level::ERR>("Failed trying to look up system names on D-Bus",
321 entry("ERROR=%s", e.what()));
322 return std::nullopt;
323 }
324 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500325
326 // Find the severity to use for this system type, or use the default
327 // entry (where no system type is specified).
328 for (const auto& sev : severities)
329 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500330 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
331 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500332 {
333 s = &sev.severity;
334 break;
335 }
336 else if (sev.system.empty())
337 {
338 s = &sev.severity;
339 }
340 }
341
342 if (s)
343 {
344 return *s;
345 }
346
347 return std::nullopt;
348}
349
Matt Spinler03c1d912019-07-10 14:12:15 -0500350} // namespace pels
351} // namespace openpower