blob: c4652d050947820f84b03c2f9cc8876ea2b85454 [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
Jason M. Billsc4a241e2023-07-26 14:15:04 -070067enum class RecoveryType
68{
69 noRecovery,
70 powerCycle,
71 warmReset,
72};
73
74static inline void
75 handleRecovery(RecoveryType recovery, boost::asio::io_context& io,
76 std::shared_ptr<sdbusplus::asio::connection> conn)
77{
78 switch (recovery)
79 {
80 case RecoveryType::noRecovery:
81 std::cerr << "Recovery is disabled. Leaving the system "
82 "in the failed state.\n";
83 break;
84 case RecoveryType::powerCycle:
85 std::cerr << "Recovering the system with a power cycle\n";
86 startPowerCycle(conn);
87 break;
88 case RecoveryType::warmReset:
89 std::cerr << "Recovering the system with a warm reset\n";
90 startWarmReset(conn);
91 break;
92 }
93}
94
Jason M. Billsd711cc82020-12-04 16:46:39 -080095void startCrashdumpAndRecovery(
Jason M. Billsc4a241e2023-07-26 14:15:04 -070096 std::shared_ptr<sdbusplus::asio::connection> conn,
97 RecoveryType requestedRecovery, const std::string& triggerType)
Jason M. Billsd711cc82020-12-04 16:46:39 -080098{
Zev Weiss08d16db2023-05-16 16:54:43 -070099#ifdef CRASHDUMP
Jason M. Billsc4a241e2023-07-26 14:15:04 -0700100 static RecoveryType recovery;
101 recovery = requestedRecovery;
Jason M. Billsd711cc82020-12-04 16:46:39 -0800102 std::cerr << "Starting crashdump\n";
103 static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch;
104
105 if (!crashdumpCompleteMatch)
106 {
107 crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>(
108 *conn,
Jason M. Bills8b7c55e2023-07-10 09:24:03 -0700109 "type='signal',interface='com.intel.crashdump',member='"
Jason M. Billsd711cc82020-12-04 16:46:39 -0800110 "CrashdumpComplete'",
111 [conn](sdbusplus::message::message& msg) {
112 std::cerr << "Crashdump completed\n";
Jason M. Billsc4a241e2023-07-26 14:15:04 -0700113 handleRecovery(recovery, io, conn);
Jason M. Billsd711cc82020-12-04 16:46:39 -0800114 crashdumpCompleteMatch.reset();
115 });
116 }
117
118 conn->async_method_call(
119 [](boost::system::error_code ec) {
120 if (ec)
121 {
Jason M. Bills1c0d0472023-04-25 12:42:00 -0700122 if (ec.value() == boost::system::errc::device_or_resource_busy)
123 {
124 std::cerr << "Crashdump already in progress. Waiting for "
125 "completion signal\n";
126 return;
127 }
128
Jason M. Billsd711cc82020-12-04 16:46:39 -0800129 std::cerr << "failed to start Crashdump\n";
130 }
131 },
132 "com.intel.crashdump", "/com/intel/crashdump",
133 "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType);
Zev Weiss08d16db2023-05-16 16:54:43 -0700134#endif
Jason M. Billsd711cc82020-12-04 16:46:39 -0800135}
136
Zev Weiss03ed41b2023-04-10 20:03:08 -0700137#ifdef LIBPECI
Jason M. Bills0e06b842020-10-02 16:30:06 -0700138static inline bool peciError(EPECIStatus peciStatus, uint8_t cc)
139{
140 return (
141 peciStatus != PECI_CC_SUCCESS ||
142 (cc != PECI_DEV_CC_SUCCESS && cc != PECI_DEV_CC_FATAL_MCA_DETECTED));
143}
144
145static void printPECIError(const std::string& reg, const size_t addr,
146 const EPECIStatus peciStatus, const size_t cc)
147{
Jason M. Bills4a6e45c2021-03-17 16:00:38 -0700148 std::cerr << "Failed to read " << reg << " on CPU address " << std::dec
149 << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex
150 << cc << "\n";
Jason M. Bills0e06b842020-10-02 16:30:06 -0700151}
Zev Weiss03ed41b2023-04-10 20:03:08 -0700152#endif
Jason M. Bills0e06b842020-10-02 16:30:06 -0700153
Jason M. Bills47008522020-10-07 16:42:34 -0700154static void beep(std::shared_ptr<sdbusplus::asio::connection> conn,
155 const uint8_t& beepPriority)
156{
157 conn->async_method_call(
158 [](boost::system::error_code ec) {
159 if (ec)
160 {
161 std::cerr << "beep returned error with "
162 "async_method_call (ec = "
163 << ec << ")\n";
164 return;
165 }
166 },
167 "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode",
168 "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority));
169}
170
Jason M. Bills32a90c62022-03-14 13:33:31 -0700171static void checkErrPinCPUs(const size_t errPin,
172 std::bitset<MAX_CPUS>& errPinCPUs)
173{
174 errPinCPUs.reset();
Zev Weiss03ed41b2023-04-10 20:03:08 -0700175#ifdef LIBPECI
Jason M. Bills32a90c62022-03-14 13:33:31 -0700176 for (size_t cpu = 0, addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR;
177 cpu++, addr++)
178 {
179 EPECIStatus peciStatus = PECI_CC_SUCCESS;
180 uint8_t cc = 0;
181 CPUModel model{};
182 uint8_t stepping = 0;
183 peciStatus = peci_GetCPUID(addr, &model, &stepping, &cc);
184 if (peciStatus != PECI_CC_SUCCESS)
185 {
186 if (peciStatus != PECI_CC_CPU_NOT_PRESENT)
187 {
188 printPECIError("CPUID", addr, peciStatus, cc);
189 }
190 continue;
191 }
192
193 switch (model)
194 {
195 case skx:
196 {
197 // Check the ERRPINSTS to see if this is the CPU that
198 // caused the ERRx (B(0) D8 F0 offset 210h)
199 uint32_t errpinsts = 0;
200 peciStatus = peci_RdPCIConfigLocal(addr, 0, 8, 0, 0x210,
201 sizeof(uint32_t),
202 (uint8_t*)&errpinsts, &cc);
203 if (peciError(peciStatus, cc))
204 {
205 printPECIError("ERRPINSTS", addr, peciStatus, cc);
206 continue;
207 }
208
209 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
210 break;
211 }
212 case icx:
213 {
214 // Check the ERRPINSTS to see if this is the CPU that
215 // caused the ERRx (B(30) D0 F3 offset 274h) (Note: Bus
216 // 30 is accessed on PECI as bus 13)
217 uint32_t errpinsts = 0;
218 peciStatus = peci_RdEndPointConfigPciLocal(
219 addr, 0, 13, 0, 3, 0x274, sizeof(uint32_t),
220 (uint8_t*)&errpinsts, &cc);
221 if (peciError(peciStatus, cc))
222 {
223 printPECIError("ERRPINSTS", addr, peciStatus, cc);
224 continue;
225 }
226
227 errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0;
228 break;
229 }
Jason M. Billsd4645cb2022-11-04 15:24:14 -0700230 default:
231 {
232 std::cerr << "Unsupported CPU Model: 0x" << std::hex
233 << static_cast<int>(model) << std::dec << "\n";
234 }
Jason M. Bills32a90c62022-03-14 13:33:31 -0700235 }
236 }
Zev Weiss03ed41b2023-04-10 20:03:08 -0700237#endif
Jason M. Bills32a90c62022-03-14 13:33:31 -0700238}
239
Jason M. Billsd711cc82020-12-04 16:46:39 -0800240} // namespace host_error_monitor