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