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