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 | { |
| 71 | static bool recover; |
| 72 | recover = recoverSystem; |
| 73 | std::cerr << "Starting crashdump\n"; |
| 74 | static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch; |
| 75 | |
| 76 | if (!crashdumpCompleteMatch) |
| 77 | { |
| 78 | crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>( |
| 79 | *conn, |
| 80 | "type='signal',interface='com.intel.crashdump.Stored',member='" |
| 81 | "CrashdumpComplete'", |
| 82 | [conn](sdbusplus::message::message& msg) { |
| 83 | std::cerr << "Crashdump completed\n"; |
| 84 | if (recover) |
| 85 | { |
| 86 | std::cerr << "Recovering the system\n"; |
| 87 | startWarmReset(conn); |
| 88 | } |
| 89 | crashdumpCompleteMatch.reset(); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | conn->async_method_call( |
| 94 | [](boost::system::error_code ec) { |
| 95 | if (ec) |
| 96 | { |
| 97 | std::cerr << "failed to start Crashdump\n"; |
| 98 | } |
| 99 | }, |
| 100 | "com.intel.crashdump", "/com/intel/crashdump", |
| 101 | "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType); |
| 102 | } |
| 103 | |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame^] | 104 | #ifdef LIBPECI |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 105 | static inline bool peciError(EPECIStatus peciStatus, uint8_t cc) |
| 106 | { |
| 107 | return ( |
| 108 | peciStatus != PECI_CC_SUCCESS || |
| 109 | (cc != PECI_DEV_CC_SUCCESS && cc != PECI_DEV_CC_FATAL_MCA_DETECTED)); |
| 110 | } |
| 111 | |
| 112 | static void printPECIError(const std::string& reg, const size_t addr, |
| 113 | const EPECIStatus peciStatus, const size_t cc) |
| 114 | { |
Jason M. Bills | 4a6e45c | 2021-03-17 16:00:38 -0700 | [diff] [blame] | 115 | std::cerr << "Failed to read " << reg << " on CPU address " << std::dec |
| 116 | << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex |
| 117 | << cc << "\n"; |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 118 | } |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame^] | 119 | #endif |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 120 | |
Jason M. Bills | 4700852 | 2020-10-07 16:42:34 -0700 | [diff] [blame] | 121 | static void beep(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 122 | const uint8_t& beepPriority) |
| 123 | { |
| 124 | conn->async_method_call( |
| 125 | [](boost::system::error_code ec) { |
| 126 | if (ec) |
| 127 | { |
| 128 | std::cerr << "beep returned error with " |
| 129 | "async_method_call (ec = " |
| 130 | << ec << ")\n"; |
| 131 | return; |
| 132 | } |
| 133 | }, |
| 134 | "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode", |
| 135 | "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority)); |
| 136 | } |
| 137 | |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 138 | static void checkErrPinCPUs(const size_t errPin, |
| 139 | std::bitset<MAX_CPUS>& errPinCPUs) |
| 140 | { |
| 141 | errPinCPUs.reset(); |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame^] | 142 | #ifdef LIBPECI |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 143 | for (size_t cpu = 0, addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR; |
| 144 | cpu++, addr++) |
| 145 | { |
| 146 | EPECIStatus peciStatus = PECI_CC_SUCCESS; |
| 147 | uint8_t cc = 0; |
| 148 | CPUModel model{}; |
| 149 | uint8_t stepping = 0; |
| 150 | peciStatus = peci_GetCPUID(addr, &model, &stepping, &cc); |
| 151 | if (peciStatus != PECI_CC_SUCCESS) |
| 152 | { |
| 153 | if (peciStatus != PECI_CC_CPU_NOT_PRESENT) |
| 154 | { |
| 155 | printPECIError("CPUID", addr, peciStatus, cc); |
| 156 | } |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | switch (model) |
| 161 | { |
| 162 | case skx: |
| 163 | { |
| 164 | // Check the ERRPINSTS to see if this is the CPU that |
| 165 | // caused the ERRx (B(0) D8 F0 offset 210h) |
| 166 | uint32_t errpinsts = 0; |
| 167 | peciStatus = peci_RdPCIConfigLocal(addr, 0, 8, 0, 0x210, |
| 168 | sizeof(uint32_t), |
| 169 | (uint8_t*)&errpinsts, &cc); |
| 170 | if (peciError(peciStatus, cc)) |
| 171 | { |
| 172 | printPECIError("ERRPINSTS", addr, peciStatus, cc); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0; |
| 177 | break; |
| 178 | } |
| 179 | case icx: |
| 180 | { |
| 181 | // Check the ERRPINSTS to see if this is the CPU that |
| 182 | // caused the ERRx (B(30) D0 F3 offset 274h) (Note: Bus |
| 183 | // 30 is accessed on PECI as bus 13) |
| 184 | uint32_t errpinsts = 0; |
| 185 | peciStatus = peci_RdEndPointConfigPciLocal( |
| 186 | addr, 0, 13, 0, 3, 0x274, sizeof(uint32_t), |
| 187 | (uint8_t*)&errpinsts, &cc); |
| 188 | if (peciError(peciStatus, cc)) |
| 189 | { |
| 190 | printPECIError("ERRPINSTS", addr, peciStatus, cc); |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | errPinCPUs[cpu] = (errpinsts & (1 << errPin)) != 0; |
| 195 | break; |
| 196 | } |
Jason M. Bills | d4645cb | 2022-11-04 15:24:14 -0700 | [diff] [blame] | 197 | default: |
| 198 | { |
| 199 | std::cerr << "Unsupported CPU Model: 0x" << std::hex |
| 200 | << static_cast<int>(model) << std::dec << "\n"; |
| 201 | } |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
Zev Weiss | 03ed41b | 2023-04-10 20:03:08 -0700 | [diff] [blame^] | 204 | #endif |
Jason M. Bills | 32a90c6 | 2022-03-14 13:33:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 207 | } // namespace host_error_monitor |