blob: da42bbfb193223e1374169bffb8e1846339127d7 [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
Matt Spinler03c1d912019-07-10 14:12:15 -050025#include <phosphor-logging/log.hpp>
26
Patrick Williams2544b412022-10-04 08:41:06 -050027#include <iostream>
28
Matt Spinler03c1d912019-07-10 14:12:15 -050029namespace openpower
30{
31namespace pels
32{
33
Aatirc1489352019-12-09 13:13:20 -060034namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050035using namespace phosphor::logging;
36
Matt Spinlercf5a8d02019-09-05 12:58:53 -050037void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050038{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050039 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
40 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060041 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050042}
43
Matt Spinler06885452019-11-06 10:35:42 -060044void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050045{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050046 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
47 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060048 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050049}
50
Matt Spinlerfdb6a202019-09-20 14:09:20 -050051UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050052 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050053 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050054 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050055{
56 _header.id = static_cast<uint16_t>(SectionID::userHeader);
57 _header.size = UserHeader::flattenedSize();
58 _header.version = userHeaderVersion;
59 _header.subType = 0;
60 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
61
Matt Spinler23970b02022-02-25 16:34:46 -060062 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050063
Sumit Kumar50bfa692022-01-06 06:48:26 -060064 // Check for additional data - PEL_SUBSYSTEM
65 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
66 if (ss)
67 {
68 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Patrick Williams2544b412022-10-04 08:41:06 -050069 std::string subsystemString = pv::getValue(eventSubsystem,
70 pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060071 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060072 {
73 log<level::WARNING>(
Matt Spinler23970b02022-02-25 16:34:46 -060074 fmt::format(
75 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {:#X}",
76 eventSubsystem)
Sumit Kumar50bfa692022-01-06 06:48:26 -060077 .c_str());
78 }
79 else
80 {
Matt Spinler23970b02022-02-25 16:34:46 -060081 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060082 }
83 }
Matt Spinler23970b02022-02-25 16:34:46 -060084 else
85 {
86 subsys = entry.subsystem;
87 }
88
89 if (subsys)
90 {
91 _eventSubsystem = *subsys;
92 }
93 else
94 {
95 // Gotta use something, how about 'others'.
96 log<level::WARNING>(
97 "No PEL subystem value supplied for error, using 'others'");
98 _eventSubsystem = 0x70;
99 }
Sumit Kumar50bfa692022-01-06 06:48:26 -0600100
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500101 _eventScope = entry.eventScope.value_or(
102 static_cast<uint8_t>(EventScope::entirePlatform));
103
Matt Spinleraadccc82020-04-10 14:33:42 -0500104 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500105 bool mfgSevStatus = false;
106 bool mfgActionFlagStatus = false;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500107
108 // Get the mfg severity & action flags
109 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500110 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500111 std::optional<uint8_t> sev = std::nullopt;
112 uint16_t val = 0;
113
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500114 if (entry.mfgSeverity)
115 {
116 // Find the mf severity possibly dependent on the system type.
117 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
118 }
119
120 if (entry.mfgActionFlags)
121 {
122 // Find the mfg action flags
123 val = entry.mfgActionFlags.value();
124 }
125
126 if (sev || val)
127 {
128 bool mfgProp = dataIface.getQuiesceOnError();
129 if (mfgProp)
130 {
131 if (sev)
132 {
133 _eventSeverity = *sev;
134 mfgSevStatus = true;
135 }
136
137 if (val)
138 {
139 _actionFlags = val;
140 mfgActionFlagStatus = true;
141 }
142 }
143 }
144 }
145
146 if (!mfgSevStatus)
147 {
148 // Get the severity from the registry if it's there, otherwise get
149 // it from the OpenBMC event log severity value.
150 if (!entry.severity)
151 {
152 _eventSeverity = convertOBMCSeverityToPEL(severity);
153 }
154 else
155 {
156 // Find the severity possibly dependent on the system type.
157 auto sev = getSeverity(entry.severity.value(), dataIface);
158 if (sev)
159 {
160 _eventSeverity = *sev;
161 }
162 else
163 {
164 // Either someone screwed up the message registry
165 // or getSystemNames failed.
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500166 log<level::ERR>(
167 "Failed finding the severity in the message registry",
168 phosphor::logging::entry("ERROR=%s",
169 entry.name.c_str()));
170
171 // Have to choose something, just use informational.
172 _eventSeverity = 0;
173 }
174 }
175 }
176
177 // Convert Critical error (0x50) to Critical Error-System Termination
178 // (0x51), if the AdditionalData is set to SYSTEM_TERM
179 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
180 if ((_eventSeverity & 0xF0) == 0x50)
181 {
182 if (sevLevel.value_or("") == "SYSTEM_TERM")
183 {
184 // Change to Critical Error, System Termination
185 _eventSeverity = 0x51;
186 }
187 }
188
189 if (entry.eventType)
190 {
191 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500192 }
193 else
194 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500195 // There are different default event types for info errors
196 // vs non info ones.
197 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
198 _eventType =
199 (sevType == SeverityType::nonError)
200 ? static_cast<uint8_t>(EventType::miscInformational)
201 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500202 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500203
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500204 _reserved4Byte1 = 0;
205
206 // No uses for problem domain or vector
207 _problemDomain = 0;
208 _problemVector = 0;
209
210 // These will be set in pel_rules::check() if they're still
211 // at the default value.
212 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500213 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500214 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500215 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500216
217 _states = 0;
218
219 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500220 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500221}
222
Matt Spinler03c1d912019-07-10 14:12:15 -0500223UserHeader::UserHeader(Stream& pel)
224{
225 try
226 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500227 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500228 validate();
229 }
230 catch (const std::exception& e)
231 {
232 log<level::ERR>("Cannot unflatten user header",
233 entry("ERROR=%s", e.what()));
234 _valid = false;
235 }
236}
237
238void UserHeader::validate()
239{
240 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500241 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500242 {
243 log<level::ERR>("Invalid user header section ID",
244 entry("ID=0x%X", header().id));
245 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500246 }
247
248 if (header().version != userHeaderVersion)
249 {
250 log<level::ERR>("Invalid user header version",
251 entry("VERSION=0x%X", header().version));
252 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500253 }
254
255 _valid = (failed) ? false : true;
256}
257
Aatir Manzurad0e0472019-10-07 13:18:37 -0500258std::optional<std::string> UserHeader::getJSON() const
259{
260 std::string severity;
261 std::string subsystem;
262 std::string eventScope;
263 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800264 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600265 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
266 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
267 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
268 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams2544b412022-10-04 08:41:06 -0500269 actionFlags = pv::getValuesBitwise(_actionFlags,
270 pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600271
272 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500273 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600274 auto iter = pv::transmissionStates.find(
275 static_cast<TransmissionState>(hostTransmissionState()));
276 if (iter != pv::transmissionStates.end())
277 {
278 hostState = iter->second;
279 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500280 auto iter1 = pv::transmissionStates.find(
281 static_cast<TransmissionState>(hmcTransmissionState()));
282 if (iter1 != pv::transmissionStates.end())
283 {
284 hmcState = iter1->second;
285 }
Matt Spinler455587e2020-01-15 14:31:52 -0600286
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800287 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800288 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
289 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
290 jsonInsert(uh, "Log Committed by",
291 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800292 jsonInsert(uh, "Subsystem", subsystem, 1);
293 jsonInsert(uh, "Event Scope", eventScope, 1);
294 jsonInsert(uh, "Event Severity", severity, 1);
295 jsonInsert(uh, "Event Type", eventType, 1);
296 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600297 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500298 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800299 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500300 return uh;
301}
Matt Spinleraadccc82020-04-10 14:33:42 -0500302
303std::optional<uint8_t> UserHeader::getSeverity(
304 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500305 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500306{
307 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500308 std::vector<std::string> systemNames;
309
310 // getSystemNames makes D-Bus calls, so only call it if we
311 // know we'll need it because there is a system name in the sev list
312 if (std::any_of(severities.begin(), severities.end(),
313 [](const auto& sev) { return !sev.system.empty(); }))
314 {
315 try
316 {
317 systemNames = dataIface.getSystemNames();
318 }
319 catch (const std::exception& e)
320 {
321 log<level::ERR>("Failed trying to look up system names on D-Bus",
322 entry("ERROR=%s", e.what()));
323 return std::nullopt;
324 }
325 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500326
327 // Find the severity to use for this system type, or use the default
328 // entry (where no system type is specified).
329 for (const auto& sev : severities)
330 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500331 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
332 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500333 {
334 s = &sev.severity;
335 break;
336 }
337 else if (sev.system.empty())
338 {
339 s = &sev.severity;
340 }
341 }
342
343 if (s)
344 {
345 return *s;
346 }
347
348 return std::nullopt;
349}
350
Matt Spinler03c1d912019-07-10 14:12:15 -0500351} // namespace pels
352} // namespace openpower