blob: 0c2c9bdee1255e884a88515a04941839e6759e39 [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>
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053021#include <phosphor-logging/log.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
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050030using namespace phosphor::logging;
31using namespace sdbusplus::xyz::openbmc_project::Control::server;
32
33void SoftPowerOff::sendHostShutDownCmd()
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053034{
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050035 auto ctrlHostPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME +
36 '0';
Patrick Venture0b02be92018-08-31 11:55:55 -070037 auto host = ::ipmi::getService(this->bus, CONTROL_HOST_BUSNAME,
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050038 ctrlHostPath.c_str());
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053039
Patrick Venture0b02be92018-08-31 11:55:55 -070040 auto method = bus.new_method_call(host.c_str(), ctrlHostPath.c_str(),
41 CONTROL_HOST_BUSNAME, "Execute");
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053042
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050043 method.append(convertForMessage(Host::Command::SoftOff).c_str());
44
45 auto reply = bus.call(method);
46 if (reply.is_method_error())
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053047 {
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050048 log<level::ERR>("Error in call to control host Execute");
49 // TODO openbmc/openbmc#851 - Once available, throw returned error
50 throw std::runtime_error("Error in call to control host Execute");
Vishwanatha Subbannad27e71e2017-02-01 18:02:38 +053051 }
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050052
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053053 return;
54}
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
64 log<level::DEBUG>("Host control signal values",
Patrick Venture0b02be92018-08-31 11:55:55 -070065 entry("COMMAND=%s", cmdCompleted.c_str()),
66 entry("STATUS=%s", cmdStatus.c_str()));
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
Patrick Venture0b02be92018-08-31 11:55:55 -070071 sdbusplus::xyz::openbmc_project::Ipmi::Internal ::server::SoftPowerOff::
72 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 {
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053081 log<level::ERR>("Failure to start Host shutdown wait timer",
Patrick Venture0b02be92018-08-31 11:55:55 -070082 entry("ERRNO=0x%X", -r));
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053083 }
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053084 else
85 {
Patrick Venture0b02be92018-08-31 11:55:55 -070086 log<level::INFO>(
87 "Timer started waiting for host to shutdown",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050088 entry("TIMEOUT_IN_MSEC=%llu",
Patrick Venture0b02be92018-08-31 11:55:55 -070089 (duration_cast<milliseconds>(
90 seconds(IPMI_HOST_SHUTDOWN_COMPLETE_TIMEOUT_SECS)))
91 .count()));
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053092 }
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053093 }
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050094 else
95 {
Andrew Geissler5f4fe3c2017-05-31 19:34:41 -050096 // An error on the initial attention is not considered an error, just
97 // exit normally and allow remaining shutdown targets to run
98 log<level::INFO>("Timeout on host attention, continue with power down");
99 completed = true;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500100 }
101 return;
102}
103
104// Starts a timer
105int SoftPowerOff::startTimer(const std::chrono::microseconds& usec)
106{
Vernon Mauery1181af72018-10-08 12:05:00 -0700107 return timer.start(usec);
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500108}
109
110// Host Response handler
111auto SoftPowerOff::responseReceived(HostResponse response) -> HostResponse
112{
113 using namespace std::chrono;
114
115 if (response == HostResponse::HostShutdown)
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530116 {
117 // Disable the timer since Host has quiesced and we are
118 // done with soft power off part
Vernon Mauery1181af72018-10-08 12:05:00 -0700119 auto r = timer.stop();
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530120 if (r < 0)
121 {
122 log<level::ERR>("Failure to STOP the timer",
Patrick Venture0b02be92018-08-31 11:55:55 -0700123 entry("ERRNO=0x%X", -r));
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530124 }
125
126 // This marks the completion of soft power off sequence.
127 completed = true;
128 }
129
Patrick Venture0b02be92018-08-31 11:55:55 -0700130 return sdbusplus::xyz::openbmc_project::Ipmi::Internal ::server::
131 SoftPowerOff::responseReceived(response);
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +0530132}
133
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +0530134} // namespace ipmi
135} // namespace phosphor