Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <phosphor-logging/log.hpp> |
| 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 6 | #include <systemintfcmds.h> |
| 7 | #include <utils.hpp> |
| 8 | #include <config.h> |
| 9 | #include <host-cmd-manager.hpp> |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 10 | #include <timer.hpp> |
| 11 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace host |
| 15 | { |
| 16 | namespace command |
| 17 | { |
| 18 | |
| 19 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 20 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 21 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 22 | constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0"; |
| 23 | constexpr auto HOST_STATE_INTERFACE = "xyz.openbmc_project.State.Host"; |
| 24 | constexpr auto HOST_TRANS_PROP = "RequestedHostTransition"; |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 25 | |
| 26 | // For throwing exceptions |
| 27 | using namespace phosphor::logging; |
| 28 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 29 | Error::InternalFailure; |
| 30 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 31 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 32 | |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 33 | Manager::Manager(sdbusplus::bus::bus& bus, sd_event* event) : |
| 34 | bus(bus), |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 35 | timer(event, std::bind(&Manager::hostTimeout, this)), |
| 36 | hostTransitionMatch(bus, |
| 37 | sdbusRule::propertiesChanged( |
| 38 | HOST_STATE_PATH, |
| 39 | HOST_STATE_INTERFACE), |
| 40 | std::bind(&Manager::clearQueueOnPowerOn, this, |
| 41 | std::placeholders::_1)) |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 42 | { |
| 43 | // Nothing to do here. |
| 44 | } |
| 45 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 46 | // Called as part of READ_MSG_DATA command |
| 47 | IpmiCmdData Manager::getNextCommand() |
| 48 | { |
| 49 | // Stop the timer. Don't have to Err failure doing so. |
| 50 | auto r = timer.setTimer(SD_EVENT_OFF); |
| 51 | if (r < 0) |
| 52 | { |
| 53 | log<level::ERR>("Failure to STOP the timer", |
| 54 | entry("ERROR=%s", strerror(-r))); |
| 55 | } |
| 56 | |
| 57 | if(this->workQueue.empty()) |
| 58 | { |
| 59 | // Just return a heartbeat in this case. A spurious SMS_ATN was |
| 60 | // asserted for the host (probably from a previous boot). |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 61 | log<level::DEBUG>("Control Host work queue is empty!"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 62 | |
| 63 | return std::make_pair(CMD_HEARTBEAT, 0x00); |
| 64 | } |
| 65 | |
| 66 | // Pop the processed entry off the queue |
| 67 | auto command = this->workQueue.front(); |
| 68 | this->workQueue.pop(); |
| 69 | |
| 70 | // IPMI command is the first element in pair |
| 71 | auto ipmiCmdData = std::get<0>(command); |
| 72 | |
| 73 | // Now, call the user registered functions so that |
| 74 | // implementation specific CommandComplete signals |
| 75 | // can be sent. `true` indicating Success. |
| 76 | std::get<CallBack>(command)(ipmiCmdData, true); |
| 77 | |
| 78 | // Check for another entry in the queue and kick it off |
| 79 | this->checkQueueAndAlertHost(); |
| 80 | |
| 81 | // Tuple of command and data |
| 82 | return ipmiCmdData; |
| 83 | } |
| 84 | |
| 85 | // Called when initial timer goes off post sending SMS_ATN |
| 86 | void Manager::hostTimeout() |
| 87 | { |
| 88 | log<level::ERR>("Host control timeout hit!"); |
| 89 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 90 | clearQueue(); |
| 91 | } |
| 92 | |
| 93 | void Manager::clearQueue() |
| 94 | { |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 95 | // Dequeue all entries and send fail signal |
| 96 | while(!this->workQueue.empty()) |
| 97 | { |
| 98 | auto command = this->workQueue.front(); |
| 99 | this->workQueue.pop(); |
| 100 | |
| 101 | // IPMI command is the first element in pair |
| 102 | auto ipmiCmdData = std::get<0>(command); |
| 103 | |
| 104 | // Call the implementation specific Command Failure. |
| 105 | // `false` indicating Failure |
| 106 | std::get<CallBack>(command)(ipmiCmdData, false); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Called for alerting the host |
| 111 | void Manager::checkQueueAndAlertHost() |
| 112 | { |
| 113 | if (this->workQueue.size() >= 1) |
| 114 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 115 | log<level::DEBUG>("Asserting SMS Attention"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 116 | |
| 117 | std::string IPMI_PATH("/org/openbmc/HostIpmi/1"); |
| 118 | std::string IPMI_INTERFACE("org.openbmc.HostIpmi"); |
| 119 | |
| 120 | auto host = ::ipmi::getService(this->bus,IPMI_INTERFACE,IPMI_PATH); |
| 121 | |
| 122 | // Start the timer for this transaction |
| 123 | auto time = std::chrono::duration_cast<std::chrono::microseconds>( |
| 124 | std::chrono::seconds(IPMI_SMS_ATN_ACK_TIMEOUT_SECS)); |
| 125 | |
| 126 | auto r = timer.startTimer(time); |
| 127 | if (r < 0) |
| 128 | { |
| 129 | log<level::ERR>("Error starting timer for control host"); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | auto method = this->bus.new_method_call(host.c_str(), |
| 134 | IPMI_PATH.c_str(), |
| 135 | IPMI_INTERFACE.c_str(), |
| 136 | "setAttention"); |
| 137 | auto reply = this->bus.call(method); |
| 138 | |
| 139 | if (reply.is_method_error()) |
| 140 | { |
| 141 | log<level::ERR>("Error in setting SMS attention"); |
| 142 | elog<InternalFailure>(); |
| 143 | } |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 144 | log<level::DEBUG>("SMS Attention asserted"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | // Called by specific implementations that provide commands |
| 149 | void Manager::execute(CommandHandler command) |
| 150 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 151 | log<level::DEBUG>("Pushing cmd on to queue", |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 152 | entry("COMMAND=%d", std::get<0>(command).first)); |
| 153 | |
| 154 | this->workQueue.emplace(command); |
| 155 | |
| 156 | // Alert host if this is only command in queue otherwise host will |
| 157 | // be notified of next message after processing the current one |
| 158 | if (this->workQueue.size() == 1) |
| 159 | { |
| 160 | this->checkQueueAndAlertHost(); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | log<level::INFO>("Command in process, no attention"); |
| 165 | } |
| 166 | |
| 167 | return; |
| 168 | } |
| 169 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 170 | void Manager::clearQueueOnPowerOn(sdbusplus::message::message& msg) |
| 171 | { |
| 172 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 173 | |
| 174 | ::ipmi::DbusInterface interface; |
| 175 | ::ipmi::PropertyMap properties; |
| 176 | |
| 177 | msg.read(interface, properties); |
| 178 | |
| 179 | if (properties.find(HOST_TRANS_PROP) == properties.end()) |
| 180 | { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | auto& requestedState = properties.at(HOST_TRANS_PROP).get<std::string>(); |
| 185 | |
| 186 | if (server::Host::convertTransitionFromString(requestedState) == |
| 187 | server::Host::Transition::On) |
| 188 | { |
| 189 | clearQueue(); |
| 190 | } |
| 191 | } |
| 192 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 193 | } // namespace command |
| 194 | } // namespace host |
| 195 | } // namepsace phosphor |