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 |
Jason M. Bills | dae0e92 | 2020-12-14 17:16:41 -0800 | [diff] [blame] | 17 | #include <peci.h> |
| 18 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 19 | #include <sdbusplus/asio/object_server.hpp> |
| 20 | |
| 21 | #include <iostream> |
| 22 | |
| 23 | namespace host_error_monitor |
| 24 | { |
Jason M. Bills | 8fa1c96 | 2020-12-10 14:33:56 -0800 | [diff] [blame] | 25 | using Association = std::tuple<std::string, std::string, std::string>; |
| 26 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 27 | bool hostIsOff(); |
| 28 | |
| 29 | void startPowerCycle(std::shared_ptr<sdbusplus::asio::connection> conn) |
| 30 | { |
| 31 | conn->async_method_call( |
| 32 | [](boost::system::error_code ec) { |
| 33 | if (ec) |
| 34 | { |
| 35 | std::cerr << "failed to set Chassis State\n"; |
| 36 | } |
| 37 | }, |
| 38 | "xyz.openbmc_project.State.Chassis", |
| 39 | "/xyz/openbmc_project/state/chassis0", |
| 40 | "org.freedesktop.DBus.Properties", "Set", |
| 41 | "xyz.openbmc_project.State.Chassis", "RequestedPowerTransition", |
| 42 | std::variant<std::string>{ |
| 43 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle"}); |
| 44 | } |
| 45 | |
| 46 | void startWarmReset(std::shared_ptr<sdbusplus::asio::connection> conn) |
| 47 | { |
| 48 | conn->async_method_call( |
| 49 | [](boost::system::error_code ec) { |
| 50 | if (ec) |
| 51 | { |
| 52 | std::cerr << "failed to set Host State\n"; |
| 53 | } |
| 54 | }, |
| 55 | "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0", |
| 56 | "org.freedesktop.DBus.Properties", "Set", |
| 57 | "xyz.openbmc_project.State.Host", "RequestedHostTransition", |
| 58 | std::variant<std::string>{ |
| 59 | "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot"}); |
| 60 | } |
| 61 | |
| 62 | void startCrashdumpAndRecovery( |
| 63 | std::shared_ptr<sdbusplus::asio::connection> conn, bool recoverSystem, |
| 64 | const std::string& triggerType) |
| 65 | { |
| 66 | static bool recover; |
| 67 | recover = recoverSystem; |
| 68 | std::cerr << "Starting crashdump\n"; |
| 69 | static std::shared_ptr<sdbusplus::bus::match::match> crashdumpCompleteMatch; |
| 70 | |
| 71 | if (!crashdumpCompleteMatch) |
| 72 | { |
| 73 | crashdumpCompleteMatch = std::make_shared<sdbusplus::bus::match::match>( |
| 74 | *conn, |
| 75 | "type='signal',interface='com.intel.crashdump.Stored',member='" |
| 76 | "CrashdumpComplete'", |
| 77 | [conn](sdbusplus::message::message& msg) { |
| 78 | std::cerr << "Crashdump completed\n"; |
| 79 | if (recover) |
| 80 | { |
| 81 | std::cerr << "Recovering the system\n"; |
| 82 | startWarmReset(conn); |
| 83 | } |
| 84 | crashdumpCompleteMatch.reset(); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | conn->async_method_call( |
| 89 | [](boost::system::error_code ec) { |
| 90 | if (ec) |
| 91 | { |
| 92 | std::cerr << "failed to start Crashdump\n"; |
| 93 | } |
| 94 | }, |
| 95 | "com.intel.crashdump", "/com/intel/crashdump", |
| 96 | "com.intel.crashdump.Stored", "GenerateStoredLog", triggerType); |
| 97 | } |
| 98 | |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 99 | static inline bool peciError(EPECIStatus peciStatus, uint8_t cc) |
| 100 | { |
| 101 | return ( |
| 102 | peciStatus != PECI_CC_SUCCESS || |
| 103 | (cc != PECI_DEV_CC_SUCCESS && cc != PECI_DEV_CC_FATAL_MCA_DETECTED)); |
| 104 | } |
| 105 | |
| 106 | static void printPECIError(const std::string& reg, const size_t addr, |
| 107 | const EPECIStatus peciStatus, const size_t cc) |
| 108 | { |
Jason M. Bills | 4a6e45c | 2021-03-17 16:00:38 -0700 | [diff] [blame^] | 109 | std::cerr << "Failed to read " << reg << " on CPU address " << std::dec |
| 110 | << addr << ". Error: " << peciStatus << ": cc: 0x" << std::hex |
| 111 | << cc << "\n"; |
Jason M. Bills | 0e06b84 | 2020-10-02 16:30:06 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jason M. Bills | 4700852 | 2020-10-07 16:42:34 -0700 | [diff] [blame] | 114 | static void beep(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 115 | const uint8_t& beepPriority) |
| 116 | { |
| 117 | conn->async_method_call( |
| 118 | [](boost::system::error_code ec) { |
| 119 | if (ec) |
| 120 | { |
| 121 | std::cerr << "beep returned error with " |
| 122 | "async_method_call (ec = " |
| 123 | << ec << ")\n"; |
| 124 | return; |
| 125 | } |
| 126 | }, |
| 127 | "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode", |
| 128 | "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority)); |
| 129 | } |
| 130 | |
Jason M. Bills | d711cc8 | 2020-12-04 16:46:39 -0800 | [diff] [blame] | 131 | } // namespace host_error_monitor |