Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 1 | #include "softoff.hpp" |
| 2 | |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 3 | #include "common/instance_id.hpp" |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 4 | #include "common/transport.hpp" |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 5 | #include "common/utils.hpp" |
| 6 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 7 | #include <libpldm/entity.h> |
| 8 | #include <libpldm/platform.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 9 | #include <libpldm/state_set.h> |
| 10 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| 13 | #include <sdeventplus/clock.hpp> |
George Liu | a2767e6 | 2021-02-24 18:53:34 +0800 | [diff] [blame] | 14 | #include <sdeventplus/exception.hpp> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 15 | #include <sdeventplus/source/io.hpp> |
| 16 | #include <sdeventplus/source/time.hpp> |
| 17 | |
| 18 | #include <array> |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 19 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 20 | PHOSPHOR_LOG2_USING; |
| 21 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 22 | namespace pldm |
| 23 | { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 24 | using namespace sdeventplus; |
| 25 | using namespace sdeventplus::source; |
| 26 | constexpr auto clockId = sdeventplus::ClockId::RealTime; |
| 27 | using Clock = Clock<clockId>; |
| 28 | using Timer = Time<clockId>; |
| 29 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 30 | constexpr pldm::pdr::TerminusID TID = 0; // TID will be implemented later. |
| 31 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 32 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 33 | SoftPowerOff::SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event) : |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 34 | bus(bus), timer(event) |
| 35 | { |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 36 | getHostState(); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 37 | if (hasError || completed) |
| 38 | { |
| 39 | return; |
| 40 | } |
| 41 | |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 42 | auto rc = getEffecterID(); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 43 | if (completed) |
| 44 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 45 | error("pldm-softpoweroff: effecter to initiate softoff not found"); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | else if (rc != PLDM_SUCCESS) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 49 | { |
| 50 | hasError = true; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | rc = getSensorInfo(); |
| 55 | if (rc != PLDM_SUCCESS) |
| 56 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 57 | error("Message get Sensor PDRs error. PLDM error code = {RC}", "RC", |
| 58 | lg2::hex, static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 59 | hasError = true; |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Matches on the pldm StateSensorEvent signal |
| 64 | pldmEventSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 65 | bus, |
| 66 | sdbusRule::type::signal() + sdbusRule::member("StateSensorEvent") + |
| 67 | sdbusRule::path("/xyz/openbmc_project/pldm") + |
| 68 | sdbusRule::interface("xyz.openbmc_project.PLDM.Event"), |
| 69 | std::bind(std::mem_fn(&SoftPowerOff::hostSoftOffComplete), this, |
| 70 | std::placeholders::_1)); |
| 71 | } |
| 72 | |
| 73 | int SoftPowerOff::getHostState() |
| 74 | { |
| 75 | try |
| 76 | { |
| 77 | pldm::utils::PropertyValue propertyValue = |
| 78 | pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 79 | "/xyz/openbmc_project/state/host0", "CurrentHostState", |
| 80 | "xyz.openbmc_project.State.Host"); |
| 81 | |
Andrew Geissler | 5b5fa43 | 2021-01-22 16:27:24 -0600 | [diff] [blame] | 82 | if ((std::get<std::string>(propertyValue) != |
| 83 | "xyz.openbmc_project.State.Host.HostState.Running") && |
| 84 | (std::get<std::string>(propertyValue) != |
| 85 | "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 86 | { |
| 87 | // Host state is not "Running", this app should return success |
| 88 | completed = true; |
| 89 | return PLDM_SUCCESS; |
| 90 | } |
| 91 | } |
| 92 | catch (const std::exception& e) |
| 93 | { |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 94 | error("PLDM host soft off: Can't get current host state: {ERROR}", |
| 95 | "ERROR", e); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 96 | hasError = true; |
| 97 | return PLDM_ERROR; |
| 98 | } |
| 99 | |
| 100 | return PLDM_SUCCESS; |
| 101 | } |
| 102 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 103 | void SoftPowerOff::hostSoftOffComplete(sdbusplus::message_t& msg) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 104 | { |
| 105 | pldm::pdr::TerminusID msgTID; |
| 106 | pldm::pdr::SensorID msgSensorID; |
| 107 | pldm::pdr::SensorOffset msgSensorOffset; |
| 108 | pldm::pdr::EventState msgEventState; |
| 109 | pldm::pdr::EventState msgPreviousEventState; |
| 110 | |
| 111 | // Read the msg and populate each variable |
| 112 | msg.read(msgTID, msgSensorID, msgSensorOffset, msgEventState, |
| 113 | msgPreviousEventState); |
| 114 | |
| 115 | if (msgSensorID == sensorID && msgSensorOffset == sensorOffset && |
| 116 | msgEventState == PLDM_SW_TERM_GRACEFUL_SHUTDOWN) |
| 117 | { |
| 118 | // Receive Graceful shutdown completion event message. Disable the timer |
| 119 | auto rc = timer.stop(); |
| 120 | if (rc < 0) |
| 121 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 122 | error("PLDM soft off: Failure to STOP the timer. ERRNO={RC}", "RC", |
| 123 | rc); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // This marks the completion of pldm soft power off. |
| 127 | completed = true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | int SoftPowerOff::getEffecterID() |
| 132 | { |
| 133 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 134 | |
| 135 | // VMM is a logical entity, so the bit 15 in entity type is set. |
| 136 | pdr::EntityType entityType = PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER | 0x8000; |
| 137 | |
| 138 | try |
| 139 | { |
| 140 | std::vector<std::vector<uint8_t>> VMMResponse{}; |
| 141 | auto VMMMethod = bus.new_method_call( |
| 142 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 143 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
| 144 | VMMMethod.append(TID, entityType, |
| 145 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 146 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 147 | auto VMMResponseMsg = bus.call(VMMMethod, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 148 | |
| 149 | VMMResponseMsg.read(VMMResponse); |
| 150 | if (VMMResponse.size() != 0) |
| 151 | { |
| 152 | for (auto& rep : VMMResponse) |
| 153 | { |
| 154 | auto VMMPdr = |
| 155 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
| 156 | effecterID = VMMPdr->effecter_id; |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | VMMPdrExist = false; |
| 162 | } |
| 163 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 164 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 165 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 166 | error("PLDM soft off: Error get VMM PDR,ERROR={ERR_EXCEP}", "ERR_EXCEP", |
| 167 | e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 168 | VMMPdrExist = false; |
| 169 | } |
| 170 | |
| 171 | if (VMMPdrExist) |
| 172 | { |
| 173 | return PLDM_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | // If the Virtual Machine Manager PDRs doesn't exist, go find the System |
| 177 | // Firmware PDRs. |
| 178 | // System Firmware is a logical entity, so the bit 15 in entity type is set |
| 179 | entityType = PLDM_ENTITY_SYS_FIRMWARE | 0x8000; |
| 180 | try |
| 181 | { |
| 182 | std::vector<std::vector<uint8_t>> sysFwResponse{}; |
| 183 | auto sysFwMethod = bus.new_method_call( |
| 184 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 185 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
| 186 | sysFwMethod.append(TID, entityType, |
| 187 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 188 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 189 | auto sysFwResponseMsg = bus.call(sysFwMethod, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 190 | |
| 191 | sysFwResponseMsg.read(sysFwResponse); |
| 192 | |
| 193 | if (sysFwResponse.size() == 0) |
| 194 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 195 | error("No effecter ID has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 196 | return PLDM_ERROR; |
| 197 | } |
| 198 | |
| 199 | for (auto& rep : sysFwResponse) |
| 200 | { |
| 201 | auto sysFwPdr = |
| 202 | reinterpret_cast<pldm_state_effecter_pdr*>(rep.data()); |
| 203 | effecterID = sysFwPdr->effecter_id; |
| 204 | } |
| 205 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 206 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 207 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 208 | error("PLDM soft off: Error get system firmware PDR,ERROR={ERR_EXCEP}", |
| 209 | "ERR_EXCEP", e.what()); |
Tom Joseph | 29d2211 | 2020-11-18 15:05:15 +0530 | [diff] [blame] | 210 | completed = true; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 211 | return PLDM_ERROR; |
| 212 | } |
| 213 | |
| 214 | return PLDM_SUCCESS; |
| 215 | } |
| 216 | |
| 217 | int SoftPowerOff::getSensorInfo() |
| 218 | { |
| 219 | pldm::pdr::EntityType entityType; |
| 220 | |
| 221 | entityType = VMMPdrExist ? PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER |
| 222 | : PLDM_ENTITY_SYS_FIRMWARE; |
| 223 | |
| 224 | // The Virtual machine manager/System firmware is logical entity, so bit 15 |
| 225 | // need to be set. |
| 226 | entityType = entityType | 0x8000; |
| 227 | |
| 228 | try |
| 229 | { |
| 230 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 231 | std::vector<std::vector<uint8_t>> Response{}; |
| 232 | auto method = bus.new_method_call( |
| 233 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 234 | "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR"); |
| 235 | method.append(TID, entityType, |
| 236 | (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS); |
| 237 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 238 | auto ResponseMsg = bus.call(method, dbusTimeout); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 239 | |
| 240 | ResponseMsg.read(Response); |
| 241 | |
| 242 | if (Response.size() == 0) |
| 243 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 244 | error("No sensor PDR has been found that matches the criteria"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 245 | return PLDM_ERROR; |
| 246 | } |
| 247 | |
ThuBaNguyen | 499a29d | 2023-05-30 06:32:06 +0700 | [diff] [blame] | 248 | pldm_state_sensor_pdr* pdr = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 249 | for (auto& rep : Response) |
| 250 | { |
| 251 | pdr = reinterpret_cast<pldm_state_sensor_pdr*>(rep.data()); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 252 | if (!pdr) |
| 253 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 254 | error("Failed to get state sensor PDR."); |
Manojkiran Eda | 31a7844 | 2021-09-12 15:18:25 +0530 | [diff] [blame] | 255 | return PLDM_ERROR; |
| 256 | } |
Manojkiran Eda | bcf91ac | 2021-03-14 13:50:48 +0530 | [diff] [blame] | 257 | } |
| 258 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 259 | sensorID = pdr->sensor_id; |
| 260 | |
| 261 | auto compositeSensorCount = pdr->composite_sensor_count; |
| 262 | auto possibleStatesStart = pdr->possible_states; |
| 263 | |
| 264 | for (auto offset = 0; offset < compositeSensorCount; offset++) |
| 265 | { |
| 266 | auto possibleStates = |
| 267 | reinterpret_cast<state_sensor_possible_states*>( |
| 268 | possibleStatesStart); |
| 269 | auto setId = possibleStates->state_set_id; |
| 270 | auto possibleStateSize = possibleStates->possible_states_size; |
| 271 | |
| 272 | if (setId == PLDM_STATE_SET_SW_TERMINATION_STATUS) |
| 273 | { |
| 274 | sensorOffset = offset; |
| 275 | break; |
| 276 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 277 | possibleStatesStart += possibleStateSize + sizeof(setId) + |
| 278 | sizeof(possibleStateSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 279 | } |
| 280 | } |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 281 | catch (const sdbusplus::exception_t& e) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 282 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 283 | error("PLDM soft off: Error get State Sensor PDR,ERROR={ERR_EXCEP}", |
| 284 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 285 | return PLDM_ERROR; |
| 286 | } |
| 287 | |
| 288 | return PLDM_SUCCESS; |
| 289 | } |
| 290 | |
| 291 | int SoftPowerOff::hostSoftOff(sdeventplus::Event& event) |
| 292 | { |
| 293 | constexpr uint8_t effecterCount = 1; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 294 | PldmTransport pldmTransport{}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 295 | uint8_t instanceID; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 296 | uint8_t mctpEID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 297 | |
| 298 | mctpEID = pldm::utils::readHostEID(); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 299 | // TODO: fix mapping to work around OpenBMC ecosystem deficiencies |
| 300 | pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 301 | |
| 302 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterID) + |
| 303 | sizeof(effecterCount) + |
| 304 | sizeof(set_effecter_state_field)> |
| 305 | requestMsg{}; |
| 306 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 307 | set_effecter_state_field stateField{ |
| 308 | PLDM_REQUEST_SET, PLDM_SW_TERM_GRACEFUL_SHUTDOWN_REQUESTED}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 309 | pldm::InstanceIdDb instanceIdDb; |
| 310 | instanceID = instanceIdDb.next(pldmTID); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 311 | auto rc = encode_set_state_effecter_states_req( |
| 312 | instanceID, effecterID, effecterCount, &stateField, request); |
| 313 | if (rc != PLDM_SUCCESS) |
| 314 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 315 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 316 | error("Message encode failure. PLDM error code = {RC}", "RC", lg2::hex, |
| 317 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 318 | return PLDM_ERROR; |
| 319 | } |
| 320 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 321 | // Add a timer to the event loop, default 30s. |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 322 | auto timerCallback = [=, this](Timer& /*source*/, |
| 323 | Timer::TimePoint /*time*/) mutable { |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 324 | if (!responseReceived) |
| 325 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 326 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 327 | error( |
| 328 | "PLDM soft off: ERROR! Can't get the response for the PLDM request msg. Time out! Exit the pldm-softpoweroff"); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 329 | exit(-1); |
| 330 | } |
| 331 | return; |
| 332 | }; |
| 333 | Timer time(event, (Clock(event).now() + std::chrono::seconds{30}), |
| 334 | std::chrono::seconds{1}, std::move(timerCallback)); |
| 335 | |
| 336 | // Add a callback to handle EPOLLIN on fd |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 337 | auto callback = [=, &pldmTransport, this](IO& io, int fd, |
| 338 | uint32_t revents) mutable { |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 339 | if (fd != pldmTransport.getEventSource()) |
| 340 | { |
| 341 | return; |
| 342 | } |
| 343 | |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 344 | if (!(revents & EPOLLIN)) |
| 345 | { |
| 346 | return; |
| 347 | } |
| 348 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 349 | void* responseMsg = nullptr; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 350 | size_t responseMsgSize{}; |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 351 | pldm_tid_t srcTID = pldmTID; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 352 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 353 | auto rc = pldmTransport.recvMsg(pldmTID, responseMsg, responseMsgSize); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 354 | if (rc) |
| 355 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 356 | error("Soft off: failed to recv pldm data. PLDM RC = {RC}", "RC", |
| 357 | static_cast<int>(rc)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 361 | std::unique_ptr<void, decltype(std::free)*> responseMsgPtr{responseMsg, |
| 362 | std::free}; |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 363 | |
| 364 | // We've got the response meant for the PLDM request msg that was |
| 365 | // sent out |
| 366 | io.set_enabled(Enabled::Off); |
| 367 | auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get()); |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 368 | |
| 369 | if (srcTID != pldmTID || |
| 370 | !pldm_msg_hdr_correlate_response(&request->hdr, &response->hdr)) |
| 371 | { |
| 372 | /* This isn't the response we were looking for */ |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | /* We have the right response, release the instance ID and process */ |
| 377 | io.set_enabled(Enabled::Off); |
| 378 | instanceIdDb.free(pldmTID, instanceID); |
| 379 | |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 380 | if (response->payload[0] != PLDM_SUCCESS) |
| 381 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 382 | error("Getting the wrong response. PLDM RC = {RC}", "RC", |
| 383 | (unsigned)response->payload[0]); |
George Liu | 9915d02 | 2021-12-21 14:04:31 +0800 | [diff] [blame] | 384 | exit(-1); |
| 385 | } |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 386 | |
| 387 | responseReceived = true; |
| 388 | |
| 389 | // Start Timer |
| 390 | using namespace std::chrono; |
| 391 | auto timeMicroseconds = |
| 392 | duration_cast<microseconds>(seconds(SOFTOFF_TIMEOUT_SECONDS)); |
| 393 | |
| 394 | auto ret = startTimer(timeMicroseconds); |
| 395 | if (ret < 0) |
| 396 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 397 | error( |
| 398 | "Failure to start Host soft off wait timer, ERRNO = {RET}. Exit the pldm-softpoweroff", |
| 399 | "RET", ret); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 400 | exit(-1); |
| 401 | } |
| 402 | else |
| 403 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 404 | error( |
| 405 | "Timer started waiting for host soft off, TIMEOUT_IN_SEC = {TIMEOUT_SEC}", |
| 406 | "TIMEOUT_SEC", SOFTOFF_TIMEOUT_SECONDS); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 407 | } |
| 408 | return; |
| 409 | }; |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 410 | IO io(event, pldmTransport.getEventSource(), EPOLLIN, std::move(callback)); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 411 | |
Rashmica Gupta | 1ed5f7a | 2023-05-22 13:56:42 +1000 | [diff] [blame] | 412 | // Asynchronously send the PLDM request |
| 413 | rc = pldmTransport.sendMsg(pldmTID, requestMsg.data(), requestMsg.size()); |
Sampa Misra | 9f8d2b0 | 2021-03-24 08:33:14 +0000 | [diff] [blame] | 414 | if (0 > rc) |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 415 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 416 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 417 | error( |
| 418 | "Failed to send message/receive response. RC = {RC}, errno = {ERR}", |
| 419 | "RC", static_cast<int>(rc), "ERR", errno); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 420 | return PLDM_ERROR; |
| 421 | } |
| 422 | |
| 423 | // Time out or soft off complete |
| 424 | while (!isCompleted() && !isTimerExpired()) |
| 425 | { |
| 426 | try |
| 427 | { |
| 428 | event.run(std::nullopt); |
| 429 | } |
| 430 | catch (const sdeventplus::SdEventError& e) |
| 431 | { |
Dung Cao | 3d03f3f | 2023-09-07 06:51:33 +0000 | [diff] [blame] | 432 | instanceIdDb.free(pldmTID, instanceID); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 433 | error( |
| 434 | "PLDM host soft off: Failure in processing request.ERROR= {ERR_EXCEP}", |
| 435 | "ERR_EXCEP", e.what()); |
Chicago Duan | 184f602 | 2020-04-17 11:30:49 +0800 | [diff] [blame] | 436 | return PLDM_ERROR; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return PLDM_SUCCESS; |
| 441 | } |
| 442 | |
| 443 | int SoftPowerOff::startTimer(const std::chrono::microseconds& usec) |
| 444 | { |
| 445 | return timer.start(usec); |
| 446 | } |
| 447 | } // namespace pldm |