blob: 336af02f30bac59152944d25807b43d4a03f39cd [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
Jason M. Billsdae0e922020-12-14 17:16:41 -080017#include <peci.h>
18
Jason M. Billsd711cc82020-12-04 16:46:39 -080019#include <sdbusplus/asio/object_server.hpp>
20
21#include <iostream>
22
23namespace host_error_monitor
24{
Jason M. Bills8fa1c962020-12-10 14:33:56 -080025using Association = std::tuple<std::string, std::string, std::string>;
26
Jason M. Billsd711cc82020-12-04 16:46:39 -080027bool hostIsOff();
28
29void 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
46void 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
62void 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. Bills0e06b842020-10-02 16:30:06 -070099static 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
106static void printPECIError(const std::string& reg, const size_t addr,
107 const EPECIStatus peciStatus, const size_t cc)
108{
109 std::cerr << "Failed to read " << reg << " on CPU address " << addr
110 << ". Error: " << peciStatus << ": cc: 0x" << std::hex << cc
111 << "\n";
112}
113
Jason M. Bills47008522020-10-07 16:42:34 -0700114static 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. Billsd711cc82020-12-04 16:46:39 -0800131} // namespace host_error_monitor