blob: 96695b93d3d73714c9281e02fff978a3594f3085 [file] [log] [blame]
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05301/**
2 * Copyright © 2016 IBM 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 */
Patrick Venture0b02be92018-08-31 11:55:55 -070016#include "config.h"
17
18#include "softoff.hpp"
19
Vernon Mauery6a98fe72019-03-11 15:57:48 -070020#include <ipmid/utils.hpp>
George Liuce338fd2024-07-17 14:22:14 +080021#include <phosphor-logging/lg2.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070022#include <xyz/openbmc_project/Control/Host/server.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050023
24#include <chrono>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053025namespace phosphor
26{
27namespace ipmi
28{
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053029
Willy Tu523e2d12023-09-05 11:36:48 -070030using namespace sdbusplus::server::xyz::openbmc_project::control;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050031
32void SoftPowerOff::sendHostShutDownCmd()
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053033{
Patrick Williams1318a5e2024-08-16 15:19:54 -040034 auto ctrlHostPath =
35 std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
Patrick Venture0b02be92018-08-31 11:55:55 -070036 auto host = ::ipmi::getService(this->bus, CONTROL_HOST_BUSNAME,
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050037 ctrlHostPath.c_str());
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053038
Patrick Venture0b02be92018-08-31 11:55:55 -070039 auto method = bus.new_method_call(host.c_str(), ctrlHostPath.c_str(),
40 CONTROL_HOST_BUSNAME, "Execute");
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053041
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050042 method.append(convertForMessage(Host::Command::SoftOff).c_str());
George Liu3e3cc352023-07-26 15:59:31 +080043 try
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053044 {
George Liu3e3cc352023-07-26 15:59:31 +080045 auto reply = bus.call(method);
46 }
47 catch (const std::exception& e)
48 {
George Liuce338fd2024-07-17 14:22:14 +080049 lg2::error("Error in call to control host Execute: {ERROR}", "ERROR",
50 e);
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050051 // TODO openbmc/openbmc#851 - Once available, throw returned error
52 throw std::runtime_error("Error in call to control host Execute");
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053053 }
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053054}
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053055
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050056// Function called on host control signals
Patrick Williams5d82f472022-07-22 19:26:53 -050057void SoftPowerOff::hostControlEvent(sdbusplus::message_t& msg)
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053058{
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050059 std::string cmdCompleted{};
60 std::string cmdStatus{};
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053061
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050062 msg.read(cmdCompleted, cmdStatus);
63
George Liuce338fd2024-07-17 14:22:14 +080064 lg2::debug(
65 "Host control signal values, command: {COMMAND}, status:{STATUS}",
66 "COMMAND", cmdCompleted, "STATUS", cmdStatus);
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050067
Patrick Venture0b02be92018-08-31 11:55:55 -070068 if (Host::convertResultFromString(cmdStatus) == Host::Result::Success)
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053069 {
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050070 // Set our internal property indicating we got host attention
Willy Tu523e2d12023-09-05 11:36:48 -070071 sdbusplus::server::xyz::openbmc_project::ipmi::internal::SoftPowerOff::
Patrick Venture0b02be92018-08-31 11:55:55 -070072 responseReceived(HostResponse::SoftOffReceived);
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050073
74 // Start timer for host shutdown
75 using namespace std::chrono;
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053076 auto time = duration_cast<microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -070077 seconds(IPMI_HOST_SHUTDOWN_COMPLETE_TIMEOUT_SECS));
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053078 auto r = startTimer(time);
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053079 if (r < 0)
80 {
George Liuce338fd2024-07-17 14:22:14 +080081 lg2::error(
82 "Failure to start Host shutdown wait timer, ERRNO: {ERRNO}",
83 "ERRNO", lg2::hex, -r);
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053084 }
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053085 else
86 {
George Liuce338fd2024-07-17 14:22:14 +080087 lg2::info("Timer started waiting for host to shutdown, "
88 "TIMEOUT_IN_MSEC: {TIMEOUT_IN_MSEC}",
89 "TIMEOUT_IN_MSEC",
Patrick Venture0b02be92018-08-31 11:55:55 -070090 (duration_cast<milliseconds>(
91 seconds(IPMI_HOST_SHUTDOWN_COMPLETE_TIMEOUT_SECS)))
George Liuce338fd2024-07-17 14:22:14 +080092 .count());
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053093 }
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053094 }
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050095 else
96 {
Andrew Geissler5f4fe3c2017-05-31 19:34:41 -050097 // An error on the initial attention is not considered an error, just
98 // exit normally and allow remaining shutdown targets to run
George Liuce338fd2024-07-17 14:22:14 +080099 lg2::info("Timeout on host attention, continue with power down");
Andrew Geissler5f4fe3c2017-05-31 19:34:41 -0500100 completed = true;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500101 }
102 return;
103}
104
105// Starts a timer
106int SoftPowerOff::startTimer(const std::chrono::microseconds& usec)
107{
Vernon Mauery1181af72018-10-08 12:05:00 -0700108 return timer.start(usec);
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500109}
110
111// Host Response handler
112auto SoftPowerOff::responseReceived(HostResponse response) -> HostResponse
113{
114 using namespace std::chrono;
115
116 if (response == HostResponse::HostShutdown)
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530117 {
118 // Disable the timer since Host has quiesced and we are
119 // done with soft power off part
Vernon Mauery1181af72018-10-08 12:05:00 -0700120 auto r = timer.stop();
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530121 if (r < 0)
122 {
George Liuce338fd2024-07-17 14:22:14 +0800123 lg2::error("Failure to STOP the timer, ERRNO: {ERRNO}", "ERRNO",
124 lg2::hex, -r);
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530125 }
126
127 // This marks the completion of soft power off sequence.
128 completed = true;
129 }
130
Willy Tu523e2d12023-09-05 11:36:48 -0700131 return sdbusplus::server::xyz::openbmc_project::ipmi::internal::
Patrick Venture0b02be92018-08-31 11:55:55 -0700132 SoftPowerOff::responseReceived(response);
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530133}
134
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +0530135} // namespace ipmi
136} // namespace phosphor