blob: db61af807cc659e92e81fd162ce6e68844f7fa11 [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
Matt Spinler03c1d912019-07-10 14:12:15 -050023#include <phosphor-logging/log.hpp>
24
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050025#include <format>
Patrick Williams2544b412022-10-04 08:41:06 -050026#include <iostream>
27
Matt Spinler03c1d912019-07-10 14:12:15 -050028namespace 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);
Patrick Williams2544b412022-10-04 08:41:06 -050068 std::string subsystemString = pv::getValue(eventSubsystem,
69 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>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050073 std::format(
Matt Spinler23970b02022-02-25 16:34:46 -060074 "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;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500106
107 // Get the mfg severity & action flags
108 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500109 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500110 std::optional<uint8_t> sev = std::nullopt;
111 uint16_t val = 0;
112
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500113 if (entry.mfgSeverity)
114 {
115 // Find the mf severity possibly dependent on the system type.
116 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
117 }
118
119 if (entry.mfgActionFlags)
120 {
121 // Find the mfg action flags
122 val = entry.mfgActionFlags.value();
123 }
124
125 if (sev || val)
126 {
127 bool mfgProp = dataIface.getQuiesceOnError();
128 if (mfgProp)
129 {
130 if (sev)
131 {
132 _eventSeverity = *sev;
133 mfgSevStatus = true;
134 }
135
136 if (val)
137 {
138 _actionFlags = val;
139 mfgActionFlagStatus = true;
140 }
141 }
142 }
143 }
144
145 if (!mfgSevStatus)
146 {
147 // Get the severity from the registry if it's there, otherwise get
148 // it from the OpenBMC event log severity value.
149 if (!entry.severity)
150 {
151 _eventSeverity = convertOBMCSeverityToPEL(severity);
152 }
153 else
154 {
155 // Find the severity possibly dependent on the system type.
156 auto sev = getSeverity(entry.severity.value(), dataIface);
157 if (sev)
158 {
159 _eventSeverity = *sev;
160 }
161 else
162 {
163 // Either someone screwed up the message registry
164 // or getSystemNames failed.
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500165 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 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600231 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500232 std::format("Cannot unflatten user header: {}", e.what()).c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500233 _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 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600242 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500243 std::format("Invalid user header section ID: {0:#x}", header().id)
Matt Spinler8ac20502023-01-04 11:03:58 -0600244 .c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500245 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500246 }
247
248 if (header().version != userHeaderVersion)
249 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600250 log<level::ERR>(
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -0500251 std::format("Invalid user header version: {0:#x}", header().version)
Matt Spinler8ac20502023-01-04 11:03:58 -0600252 .c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500253 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500254 }
255
256 _valid = (failed) ? false : true;
257}
258
Matt Spinlerb832aa52023-03-21 15:32:34 -0500259std::optional<std::string> UserHeader::getJSON(uint8_t creatorID) const
Aatir Manzurad0e0472019-10-07 13:18:37 -0500260{
261 std::string severity;
262 std::string subsystem;
263 std::string eventScope;
264 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800265 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600266 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
267 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
268 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
269 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams2544b412022-10-04 08:41:06 -0500270 actionFlags = pv::getValuesBitwise(_actionFlags,
271 pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600272
273 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500274 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600275 auto iter = pv::transmissionStates.find(
276 static_cast<TransmissionState>(hostTransmissionState()));
277 if (iter != pv::transmissionStates.end())
278 {
279 hostState = iter->second;
280 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500281 auto iter1 = pv::transmissionStates.find(
282 static_cast<TransmissionState>(hmcTransmissionState()));
283 if (iter1 != pv::transmissionStates.end())
284 {
285 hmcState = iter1->second;
286 }
Matt Spinler455587e2020-01-15 14:31:52 -0600287
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800288 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800289 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
290 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
291 jsonInsert(uh, "Log Committed by",
Matt Spinlerb832aa52023-03-21 15:32:34 -0500292 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800293 jsonInsert(uh, "Subsystem", subsystem, 1);
294 jsonInsert(uh, "Event Scope", eventScope, 1);
295 jsonInsert(uh, "Event Severity", severity, 1);
296 jsonInsert(uh, "Event Type", eventType, 1);
297 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600298 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500299 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800300 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500301 return uh;
302}
Matt Spinleraadccc82020-04-10 14:33:42 -0500303
304std::optional<uint8_t> UserHeader::getSeverity(
305 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500306 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500307{
308 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500309 std::vector<std::string> systemNames;
310
311 // getSystemNames makes D-Bus calls, so only call it if we
312 // know we'll need it because there is a system name in the sev list
313 if (std::any_of(severities.begin(), severities.end(),
314 [](const auto& sev) { return !sev.system.empty(); }))
315 {
316 try
317 {
318 systemNames = dataIface.getSystemNames();
319 }
320 catch (const std::exception& e)
321 {
322 log<level::ERR>("Failed trying to look up system names on D-Bus",
323 entry("ERROR=%s", e.what()));
324 return std::nullopt;
325 }
326 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500327
328 // Find the severity to use for this system type, or use the default
329 // entry (where no system type is specified).
330 for (const auto& sev : severities)
331 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500332 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
333 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500334 {
335 s = &sev.severity;
336 break;
337 }
338 else if (sev.system.empty())
339 {
340 s = &sev.severity;
341 }
342 }
343
344 if (s)
345 {
346 return *s;
347 }
348
349 return std::nullopt;
350}
351
Matt Spinler03c1d912019-07-10 14:12:15 -0500352} // namespace pels
353} // namespace openpower