blob: eacb1cb07efd461ec366e0858a2364ff0ed07076 [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,
Jason M. Bills8b7c55e2023-07-10 09:24:03 -070081 "type='signal',interface='com.intel.crashdump',member='"
Jason M. Billsd711cc82020-12-04 16:46:39 -080082 "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 {
Jason M. Bills1c0d0472023-04-25 12:42:00 -070098 if (ec.value() == boost::system::errc::device_or_resource_busy)
99 {
100 std::cerr << "Crashdump already in progress. Waiting for "
101 "completion signal\n";
102 return;
103 }
104
Jason M. Billsd711cc82020-12-04 16:46:39 -0800105 std::cerr << "failed to start Crashdump\n";
106 }
107 },
108 "com.intel.crashdump", "/com/intel/crashdump",
109 "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType);
Zev Weiss08d16db2023-05-16 16:54:43 -0700110#endif
Jason M. Billsd711cc82020-12-04 16:46:39 -0800111}
112
Zev Weiss03ed41b2023-04-10 20:03:08 -0700113#ifdef LIBPECI
Jason M. Bills0e06b842020-10-02 16:30:06 -0700114static inline bool peciError(EPECIStatus peciStatus, uint8_t cc)
115{
116 return (
117 peciStatus != PECI_CC_SUCCESS ||
118 (cc != PECI_DEV_CC_SUCCESS && cc != PECI_DEV_CC_FATAL_MCA_DETECTED));
119}
120
121static void printPECIError(const std::string& reg, const size_t addr,
122 const EPECIStatus peciStatus, const size_t cc)
123{
Jason M. Bills4a6e45c2021-03-17 16:00:38 -0700124 std::cerr << "Failed to read " << reg << " on CPU address " << std::dec
125 << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex
126 << cc << "\n";
Jason M. Bills0e06b842020-10-02 16:30:06 -0700127}
Zev Weiss03ed41b2023-04-10 20:03:08 -0700128#endif
Jason M. Bills0e06b842020-10-02 16:30:06 -0700129
Jason M. Bills47008522020-10-07 16:42:34 -0700130static void beep(std::shared_ptr<sdbusplus::asio::connection> conn,
131 const uint8_t& beepPriority)
132{
133 conn->async_method_call(
134 [](boost::system::error_code ec) {
135 if (ec)
136 {
137 std::cerr << "beep returned error with "
138 "async_method_call (ec = "
139 << ec << ")\n";
140 return;
141 }
142 },
143 "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode",
144 "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority));
145}
146
Jason M. Bills32a90c62022-03-14 13:33:31 -0700147static void checkErrPinCPUs(const size_t errPin,
148 std::bitset<MAX_CPUS>& errPinCPUs)
149{
150 errPinCPUs.reset();
Zev Weiss03ed41b2023-04-10 20:03:08 -0700151#ifdef LIBPECI
Jason M. Bills32a90c62022-03-14 13:33:31 -0700152 for (size_t cpu = 0, addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR;
153 cpu++, addr++)
154 {
155 EPECIStatus peciStatus = PECI_CC_SUCCESS;
156 uint8_t cc = 0;
157 CPUModel model{};
158 uint8_t stepping = 0;
159 peciStatus = peci_GetCPUID(addr, &model, &stepping, &cc);
160 if (peciStatus != PECI_CC_SUCCESS)
161 {
162 if (peciStatus != PECI_CC_CPU_NOT_PRESENT)
163 {
164 printPECIError("CPUID", addr, peciStatus, cc);
165 }
166 continue;
167 }
168
169 switch (model)
170 {
171 case skx:
172 {
173 // Check the ERRPINSTS to see if this is the CPU that
174 // caused the ERRx (B(0) D8 F0 offset 210h)
175 uint32_t errpinsts = 0;
176 peciStatus = peci_RdPCIConfigLocal(addr, 0, 8, 0, 0x210,
177 sizeof(uint32_t),
178 (uint8_t*)&errpinsts, &cc);
179 if (peciError(peciStatus, cc))
180 {
181 printPECIError("ERRPINSTS", addr, peciStatus, cc);
182 continue;
183 }
184
185 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
186 break;
187 }
188 case icx:
189 {
190 // Check the ERRPINSTS to see if this is the CPU that
191 // caused the ERRx (B(30) D0 F3 offset 274h) (Note: Bus
192 // 30 is accessed on PECI as bus 13)
193 uint32_t errpinsts = 0;
194 peciStatus = peci_RdEndPointConfigPciLocal(
195 addr, 0, 13, 0, 3, 0x274, sizeof(uint32_t),
196 (uint8_t*)&errpinsts, &cc);
197 if (peciError(peciStatus, cc))
198 {
199 printPECIError("ERRPINSTS", addr, peciStatus, cc);
200 continue;
201 }
202
203 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
204 break;
205 }
Jason M. Billsd4645cb2022-11-04 15:24:14 -0700206 default:
207 {
208 std::cerr << "Unsupported CPU Model: 0x" << std::hex
209 << static_cast<int>(model) << std::dec << "\n";
210 }
Jason M. Bills32a90c62022-03-14 13:33:31 -0700211 }
212 }
Zev Weiss03ed41b2023-04-10 20:03:08 -0700213#endif
Jason M. Bills32a90c62022-03-14 13:33:31 -0700214}
215
Jason M. Billsd711cc82020-12-04 16:46:39 -0800216} // namespace host_error_monitor