blob: 3470c2f47519743dd0e905b11bff5ccb800cc7c8 [file] [log] [blame]
Jason M. Billsd711cc82020-12-04 16:46:39 -08001/*
2// Copyright (c) 2021 Intel 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#pragma once
Zev Weiss03ed41b2023-04-10 20:03:08 -070017#ifdef LIBPECI
Jason M. Billsdae0e922020-12-14 17:16:41 -080018#include <peci.h>
Zev Weiss03ed41b2023-04-10 20:03:08 -070019#else
20#define MAX_CPUS 8
21#endif
Jason M. Billsdae0e922020-12-14 17:16:41 -080022
Jason M. Billsd711cc82020-12-04 16:46:39 -080023#include <sdbusplus/asio/object_server.hpp>
24
Jason M. Bills32a90c62022-03-14 13:33:31 -070025#include <bitset>
Jason M. Billsd711cc82020-12-04 16:46:39 -080026#include <iostream>
27
28namespace host_error_monitor
29{
Jason M. Bills8fa1c962020-12-10 14:33:56 -080030using Association = std::tuple<std::string, std::string, std::string>;
31
Jason M. Billsd711cc82020-12-04 16:46:39 -080032bool hostIsOff();
33
34void startPowerCycle(std::shared_ptr<sdbusplus::asio::connection> conn)
35{
36 conn->async_method_call(
37 [](boost::system::error_code ec) {
38 if (ec)
39 {
40 std::cerr << "failed to set Chassis State\n";
41 }
42 },
43 "xyz.openbmc_project.State.Chassis",
44 "/xyz/openbmc_project/state/chassis0",
45 "org.freedesktop.DBus.Properties", "Set",
46 "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition",
47 std::variant<std::string>{
48 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle"});
49}
50
51void startWarmReset(std::shared_ptr<sdbusplus::asio::connection> conn)
52{
53 conn->async_method_call(
54 [](boost::system::error_code ec) {
55 if (ec)
56 {
57 std::cerr << "failed to set Host State\n";
58 }
59 },
60 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
61 "org.freedesktop.DBus.Properties", "Set",
62 "xyz.openbmc_project.State.Host", "RequestedHostTransition",
63 std::variant<std::string>{
64 "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot"});
65}
66
67void startCrashdumpAndRecovery(
68 std::shared_ptr<sdbusplus::asio::connection> conn, bool recoverSystem,
69 const std::string& triggerType)
70{
Zev Weiss08d16db2023-05-16 16:54:43 -070071#ifdef CRASHDUMP
Jason M. Billsd711cc82020-12-04 16:46:39 -080072 static bool recover;
73 recover = recoverSystem;
74 std::cerr << "Starting crashdump\n";
75 static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch;
76
77 if (!crashdumpCompleteMatch)
78 {
79 crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>(
80 *conn,
81 "type='signal',interface='com.intel.crashdump.Stored',member='"
82 "CrashdumpComplete'",
83 [conn](sdbusplus::message::message& msg) {
84 std::cerr << "Crashdump completed\n";
85 if (recover)
86 {
87 std::cerr << "Recovering the system\n";
88 startWarmReset(conn);
89 }
90 crashdumpCompleteMatch.reset();
91 });
92 }
93
94 conn->async_method_call(
95 [](boost::system::error_code ec) {
96 if (ec)
97 {
98 std::cerr << "failed to start Crashdump\n";
99 }
100 },
101 "com.intel.crashdump", "/com/intel/crashdump",
102 "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType);
Zev Weiss08d16db2023-05-16 16:54:43 -0700103#endif
Jason M. Billsd711cc82020-12-04 16:46:39 -0800104}
105
Zev Weiss03ed41b2023-04-10 20:03:08 -0700106#ifdef LIBPECI
Jason M. Bills0e06b842020-10-02 16:30:06 -0700107static inline bool peciError(EPECIStatus peciStatus, uint8_t cc)
108{
109 return (
110 peciStatus != PECI_CC_SUCCESS ||
111 (cc != PECI_DEV_CC_SUCCESS && cc != PECI_DEV_CC_FATAL_MCA_DETECTED));
112}
113
114static void printPECIError(const std::string& reg, const size_t addr,
115 const EPECIStatus peciStatus, const size_t cc)
116{
Jason M. Bills4a6e45c2021-03-17 16:00:38 -0700117 std::cerr << "Failed to read " << reg << " on CPU address " << std::dec
118 << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex
119 << cc << "\n";
Jason M. Bills0e06b842020-10-02 16:30:06 -0700120}
Zev Weiss03ed41b2023-04-10 20:03:08 -0700121#endif
Jason M. Bills0e06b842020-10-02 16:30:06 -0700122
Jason M. Bills47008522020-10-07 16:42:34 -0700123static void beep(std::shared_ptr<sdbusplus::asio::connection> conn,
124 const uint8_t& beepPriority)
125{
126 conn->async_method_call(
127 [](boost::system::error_code ec) {
128 if (ec)
129 {
130 std::cerr << "beep returned error with "
131 "async_method_call (ec = "
132 << ec << ")\n";
133 return;
134 }
135 },
136 "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode",
137 "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority));
138}
139
Jason M. Bills32a90c62022-03-14 13:33:31 -0700140static void checkErrPinCPUs(const size_t errPin,
141 std::bitset<MAX_CPUS>& errPinCPUs)
142{
143 errPinCPUs.reset();
Zev Weiss03ed41b2023-04-10 20:03:08 -0700144#ifdef LIBPECI
Jason M. Bills32a90c62022-03-14 13:33:31 -0700145 for (size_t cpu = 0, addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR;
146 cpu++, addr++)
147 {
148 EPECIStatus peciStatus = PECI_CC_SUCCESS;
149 uint8_t cc = 0;
150 CPUModel model{};
151 uint8_t stepping = 0;
152 peciStatus = peci_GetCPUID(addr, &model, &stepping, &cc);
153 if (peciStatus != PECI_CC_SUCCESS)
154 {
155 if (peciStatus != PECI_CC_CPU_NOT_PRESENT)
156 {
157 printPECIError("CPUID", addr, peciStatus, cc);
158 }
159 continue;
160 }
161
162 switch (model)
163 {
164 case skx:
165 {
166 // Check the ERRPINSTS to see if this is the CPU that
167 // caused the ERRx (B(0) D8 F0 offset 210h)
168 uint32_t errpinsts = 0;
169 peciStatus = peci_RdPCIConfigLocal(addr, 0, 8, 0, 0x210,
170 sizeof(uint32_t),
171 (uint8_t*)&errpinsts, &cc);
172 if (peciError(peciStatus, cc))
173 {
174 printPECIError("ERRPINSTS", addr, peciStatus, cc);
175 continue;
176 }
177
178 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
179 break;
180 }
181 case icx:
182 {
183 // Check the ERRPINSTS to see if this is the CPU that
184 // caused the ERRx (B(30) D0 F3 offset 274h) (Note: Bus
185 // 30 is accessed on PECI as bus 13)
186 uint32_t errpinsts = 0;
187 peciStatus = peci_RdEndPointConfigPciLocal(
188 addr, 0, 13, 0, 3, 0x274, sizeof(uint32_t),
189 (uint8_t*)&errpinsts, &cc);
190 if (peciError(peciStatus, cc))
191 {
192 printPECIError("ERRPINSTS", addr, peciStatus, cc);
193 continue;
194 }
195
196 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
197 break;
198 }
Jason M. Billsd4645cb2022-11-04 15:24:14 -0700199 default:
200 {
201 std::cerr << "Unsupported CPU Model: 0x" << std::hex
202 << static_cast<int>(model) << std::dec << "\n";
203 }
Jason M. Bills32a90c62022-03-14 13:33:31 -0700204 }
205 }
Zev Weiss03ed41b2023-04-10 20:03:08 -0700206#endif
Jason M. Bills32a90c62022-03-14 13:33:31 -0700207}
208
Jason M. Billsd711cc82020-12-04 16:46:39 -0800209} // namespace host_error_monitor