Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 1 | /* |
| 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 Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 17 | #ifdef LIBPECI |
Jason M. Bills | dae0e92 | 2020-12-14 17:16:41 -0800 | [diff] [blame] | 18 | #include <peci.h> |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 19 | #else |
| 20 | #define MAX_CPUS 8 |
| 21 | #endif |
Jason M. Bills | dae0e92 | 2020-12-14 17:16:41 -0800 | [diff] [blame] | 22 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 23 | #include <sdbusplus/asio/object_server.hpp> |
| 24 | |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 25 | #include <bitset> |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 26 | #include <iostream> |
| 27 | |
| 28 | namespace host_error_monitor |
| 29 | { |
Jason M. Bills | 8fa1c96 | 2020-12-10 14:33:56 -0800 | [diff] [blame] | 30 | using Association = std::tuple<std::string, std::string, std::string>; |
| 31 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 32 | bool hostIsOff(); |
| 33 | |
| 34 | void 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 | |
| 51 | void 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 | |
| 67 | void startCrashdumpAndRecovery( |
| 68 | std::shared_ptr<sdbusplus::asio::connection> conn, bool recoverSystem, |
| 69 | const std::string& triggerType) |
| 70 | { |
Zev Weiss | 08d16db | 2023-05-16 16:54:43 -0700 | [diff] [blame] | 71 | #ifdef CRASHDUMP |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 72 | 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. Bills | 8b7c55e | 2023-07-10 09:24:03 -0700 | [diff] [blame^] | 81 | "type='signal',interface='com.intel.crashdump',member='" |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 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 | { |
Jason M. Bills | 1c0d047 | 2023-04-25 12:42:00 -0700 | [diff] [blame] | 98 | 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. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 105 | std::cerr << "failed to start Crashdump\n"; |
| 106 | } |
| 107 | }, |
| 108 | "com.intel.crashdump", "/com/intel/crashdump", |
| 109 | "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType); |
Zev Weiss | 08d16db | 2023-05-16 16:54:43 -0700 | [diff] [blame] | 110 | #endif |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 113 | #ifdef LIBPECI |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 114 | static 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 | |
| 121 | static void printPECIError(const std::string& reg, const size_t addr, |
| 122 | const EPECIStatus peciStatus, const size_t cc) |
| 123 | { |
Jason M. Bills | 4a6e45c | 2021-03-17 16:00:38 -0700 | [diff] [blame] | 124 | std::cerr << "Failed to read " << reg << " on CPU address " << std::dec |
| 125 | << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex |
| 126 | << cc << "\n"; |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 127 | } |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 128 | #endif |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 129 | |
Jason M. Bills | 4700852 | 2020-10-07 16:42:34 -0700 | [diff] [blame] | 130 | static 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. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 147 | static void checkErrPinCPUs(const size_t errPin, |
| 148 | std::bitset<MAX_CPUS>& errPinCPUs) |
| 149 | { |
| 150 | errPinCPUs.reset(); |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 151 | #ifdef LIBPECI |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 152 | 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. Bills | d4645cb | 2022-11-04 15:24:14 -0700 | [diff] [blame] | 206 | default: |
| 207 | { |
| 208 | std::cerr << "Unsupported CPU Model: 0x" << std::hex |
| 209 | << static_cast<int>(model) << std::dec << "\n"; |
| 210 | } |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame] | 213 | #endif |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 216 | } // namespace host_error_monitor |