1 #pragma once 2 3 #include "inband_code_update.hpp" 4 #include "libpldmresponder/oem_handler.hpp" 5 #include "libpldmresponder/pdr_utils.hpp" 6 #include "libpldmresponder/platform.hpp" 7 #include "requester/handler.hpp" 8 9 #include <libpldm/entity.h> 10 #include <libpldm/oem/ibm/state_set.h> 11 #include <libpldm/platform.h> 12 13 #include <sdbusplus/bus/match.hpp> 14 #include <sdeventplus/event.hpp> 15 #include <sdeventplus/utility/timer.hpp> 16 17 typedef ibm_oem_pldm_state_set_firmware_update_state_values CodeUpdateState; 18 19 namespace pldm 20 { 21 namespace responder 22 { 23 namespace oem_ibm_platform 24 { 25 constexpr uint16_t ENTITY_INSTANCE_0 = 0; 26 constexpr uint16_t ENTITY_INSTANCE_1 = 1; 27 28 constexpr uint32_t BMC_PDR_START_RANGE = 0x00000000; 29 constexpr uint32_t BMC_PDR_END_RANGE = 0x00FFFFFF; 30 constexpr uint32_t HOST_PDR_START_RANGE = 0x01000000; 31 constexpr uint32_t HOST_PDR_END_RANGE = 0x01FFFFFF; 32 33 const pldm::pdr::TerminusID HYPERVISOR_TID = 208; 34 35 static constexpr uint8_t HEARTBEAT_TIMEOUT_DELTA = 10; 36 37 enum SetEventReceiverCount 38 { 39 SET_EVENT_RECEIVER_SENT = 0x2, 40 }; 41 42 class Handler : public oem_platform::Handler 43 { 44 public: 45 Handler(const pldm::utils::DBusHandler* dBusIntf, 46 pldm::responder::CodeUpdate* codeUpdate, int mctp_fd, 47 uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb, 48 sdeventplus::Event& event, 49 pldm::requester::Handler<pldm::requester::Request>* handler) : 50 oem_platform::Handler(dBusIntf), 51 codeUpdate(codeUpdate), platformHandler(nullptr), mctp_fd(mctp_fd), 52 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), event(event), 53 handler(handler), 54 timer(event, std::bind(std::mem_fn(&Handler::setSurvTimer), this, 55 HYPERVISOR_TID, false)), 56 hostTransitioningToOff(true) 57 { 58 codeUpdate->setVersions(); 59 setEventReceiverCnt = 0; 60 61 using namespace sdbusplus::bus::match::rules; 62 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>( 63 pldm::utils::DBusHandler::getBus(), 64 propertiesChanged("/xyz/openbmc_project/state/host0", 65 "xyz.openbmc_project.State.Host"), 66 [this](sdbusplus::message_t& msg) { 67 pldm::utils::DbusChangedProps props{}; 68 std::string intf; 69 msg.read(intf, props); 70 const auto itr = props.find("CurrentHostState"); 71 if (itr != props.end()) 72 { 73 pldm::utils::PropertyValue value = itr->second; 74 auto propVal = std::get<std::string>(value); 75 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off") 76 { 77 hostOff = true; 78 setEventReceiverCnt = 0; 79 disableWatchDogTimer(); 80 startStopTimer(false); 81 } 82 else if (propVal == 83 "xyz.openbmc_project.State.Host.HostState.Running") 84 { 85 hostOff = false; 86 hostTransitioningToOff = false; 87 } 88 else if ( 89 propVal == 90 "xyz.openbmc_project.State.Host.HostState.TransitioningToOff") 91 { 92 hostTransitioningToOff = true; 93 } 94 } 95 }); 96 97 powerStateOffMatch = std::make_unique<sdbusplus::bus::match::match>( 98 pldm::utils::DBusHandler::getBus(), 99 propertiesChanged("/xyz/openbmc_project/state/chassis0", 100 "xyz.openbmc_project.State.Chassis"), 101 [this](sdbusplus::message_t& msg) { 102 pldm::utils::DbusChangedProps props{}; 103 std::string intf; 104 msg.read(intf, props); 105 const auto itr = props.find("CurrentPowerState"); 106 if (itr != props.end()) 107 { 108 pldm::utils::PropertyValue value = itr->second; 109 auto propVal = std::get<std::string>(value); 110 if (propVal == 111 "xyz.openbmc_project.State.Chassis.PowerState.Off") 112 { 113 static constexpr auto searchpath = 114 "/xyz/openbmc_project/inventory/system/chassis/motherboard"; 115 int depth = 0; 116 std::vector<std::string> powerInterface = { 117 "xyz.openbmc_project.State.Decorator.PowerState"}; 118 pldm::utils::GetSubTreeResponse response = 119 pldm::utils::DBusHandler().getSubtree(searchpath, depth, 120 powerInterface); 121 for (const auto& [objPath, serviceMap] : response) 122 { 123 pldm::utils::DBusMapping dbusMapping{ 124 objPath, 125 "xyz.openbmc_project.State.Decorator.PowerState", 126 "PowerState", "string"}; 127 value = 128 "xyz.openbmc_project.State.Decorator.PowerState.State.Off"; 129 try 130 { 131 pldm::utils::DBusHandler().setDbusProperty( 132 dbusMapping, value); 133 } 134 catch (const std::exception& e) 135 { 136 error( 137 "Unable to set the slot power state to Off error - {ERROR}", 138 "ERROR", e); 139 } 140 } 141 } 142 } 143 }); 144 } 145 146 int getOemStateSensorReadingsHandler( 147 EntityType entityType, pldm::pdr::EntityInstance entityInstance, 148 pldm::pdr::StateSetId stateSetId, 149 pldm::pdr::CompositeCount compSensorCnt, 150 std::vector<get_sensor_state_field>& stateField); 151 152 int oemSetStateEffecterStatesHandler( 153 uint16_t entityType, uint16_t entityInstance, uint16_t stateSetId, 154 uint8_t compEffecterCnt, 155 std::vector<set_effecter_state_field>& stateField, uint16_t effecterId); 156 157 /** @brief Method to set the platform handler in the 158 * oem_ibm_handler class 159 * @param[in] handler - pointer to PLDM platform handler 160 */ 161 void setPlatformHandler(pldm::responder::platform::Handler* handler); 162 163 /** @brief Method to fetch the effecter ID of the code update PDRs 164 * 165 * @return platformHandler->getNextEffecterId() - returns the 166 * effecter ID from the platform handler 167 */ 168 virtual uint16_t getNextEffecterId() 169 { 170 return platformHandler->getNextEffecterId(); 171 } 172 173 /** @brief Method to fetch the sensor ID of the code update PDRs 174 * 175 * @return platformHandler->getNextSensorId() - returns the 176 * Sensor ID from the platform handler 177 */ 178 virtual uint16_t getNextSensorId() 179 { 180 return platformHandler->getNextSensorId(); 181 } 182 183 /** @brief Method to Generate the OEM PDRs 184 * 185 * @param[in] repo - instance of concrete implementation of Repo 186 */ 187 void buildOEMPDR(pdr_utils::Repo& repo); 188 189 /** @brief Method to send code update event to host 190 * @param[in] sensorId - sendor ID 191 * @param[in] sensorEventClass - event class of sensor 192 * @param[in] sensorOffset - sensor offset 193 * @param[in] eventState - new code update event state 194 * @param[in] prevEventState - previous code update event state 195 * @return none 196 */ 197 void sendStateSensorEvent(uint16_t sensorId, 198 enum sensor_event_class_states sensorEventClass, 199 uint8_t sensorOffset, uint8_t eventState, 200 uint8_t prevEventState); 201 202 /** @brief Method to send encoded request msg of code update event to host 203 * @param[in] requestMsg - encoded request msg 204 * @param[in] instanceId - instance id of the message 205 * @return PLDM status code 206 */ 207 int sendEventToHost(std::vector<uint8_t>& requestMsg, uint8_t instanceId); 208 209 /** @brief _processEndUpdate processes the actual work that needs 210 * to be carried out after EndUpdate effecter is set. This is done async 211 * after sending response for EndUpdate set effecter 212 * @param[in] source - sdeventplus event source 213 */ 214 void _processEndUpdate(sdeventplus::source::EventBase& source); 215 216 /** @brief _processStartUpdate processes the actual work that needs 217 * to be carried out after StartUpdate effecter is set. This is done async 218 * after sending response for StartUpdate set effecter 219 * @param[in] source - sdeventplus event source 220 */ 221 void _processStartUpdate(sdeventplus::source::EventBase& source); 222 223 /** @brief _processSystemReboot processes the actual work that needs to be 224 * carried out after the System Power State effecter is set to reboot 225 * the system 226 * @param[in] source - sdeventplus event source 227 */ 228 void _processSystemReboot(sdeventplus::source::EventBase& source); 229 230 /*keeps track how many times setEventReceiver is sent */ 231 void countSetEventReceiver() 232 { 233 setEventReceiverCnt++; 234 } 235 236 /* disables watchdog if running and Host is up */ 237 void checkAndDisableWatchDog(); 238 239 /** @brief To check if the watchdog app is running 240 * 241 * @return the running status of watchdog app 242 */ 243 bool watchDogRunning(); 244 245 /** @brief Method to reset the Watchdog timer on receiving platform Event 246 * Message for heartbeat elapsed time from Hostboot 247 */ 248 void resetWatchDogTimer(); 249 250 /** @brief To disable to the watchdog timer on host poweron completion*/ 251 void disableWatchDogTimer(); 252 253 /** @brief to check the BMC state*/ 254 int checkBMCState(); 255 256 /** @brief update the dbus object paths */ 257 void updateOemDbusPaths(std::string& dbusPath); 258 259 /** @brief Method to fetch the last BMC record from the PDR repo 260 * 261 * @param[in] repo - pointer to BMC's primary PDR repo 262 * 263 * @return the last BMC record from the repo 264 */ 265 const pldm_pdr_record* fetchLastBMCRecord(const pldm_pdr* repo); 266 267 /** @brief Method to check if the record handle passed is in remote PDR 268 * record handle range 269 * 270 * @param[in] record_handle - record handle of the PDR 271 * 272 * @return true if record handle passed is in host PDR record handle range 273 */ 274 bool checkRecordHandleInRange(const uint32_t& record_handle); 275 276 /** *brief Method to call the setEventReceiver command*/ 277 void processSetEventReceiver(); 278 279 /** @brief Method to call the setEventReceiver through the platform 280 * handler 281 */ 282 virtual void setEventReceiver() 283 { 284 platformHandler->setEventReceiver(); 285 } 286 287 /** @brief Method to Enable/Disable timer to see if remote terminus sends 288 * the surveillance ping and logs informational error if remote terminus 289 * fails to send the surveillance pings 290 * 291 * @param[in] tid - TID of the remote terminus 292 * @param[in] value - true or false, to indicate if the timer is 293 * running or not 294 */ 295 void setSurvTimer(uint8_t tid, bool value); 296 297 ~Handler() = default; 298 299 pldm::responder::CodeUpdate* codeUpdate; //!< pointer to CodeUpdate object 300 pldm::responder::platform::Handler* 301 platformHandler; //!< pointer to PLDM platform handler 302 303 /** @brief fd of MCTP communications socket */ 304 int mctp_fd; 305 306 /** @brief MCTP EID of host firmware */ 307 uint8_t mctp_eid; 308 309 /** @brief reference to an InstanceIdDb object, used to obtain a PLDM 310 * instance id. */ 311 pldm::InstanceIdDb& instanceIdDb; 312 /** @brief sdeventplus event source */ 313 std::unique_ptr<sdeventplus::source::Defer> assembleImageEvent; 314 std::unique_ptr<sdeventplus::source::Defer> startUpdateEvent; 315 std::unique_ptr<sdeventplus::source::Defer> systemRebootEvent; 316 317 /** @brief reference of main event loop of pldmd, primarily used to schedule 318 * work 319 */ 320 sdeventplus::Event& event; 321 322 private: 323 /** @brief Method to reset or stop the surveillance timer 324 * 325 * @param[in] value - true or false, to indicate if the timer 326 * should be reset or turned off 327 */ 328 void startStopTimer(bool value); 329 330 /** @brief D-Bus property changed signal match for CurrentPowerState*/ 331 std::unique_ptr<sdbusplus::bus::match_t> chassisOffMatch; 332 333 /** @brief PLDM request handler */ 334 pldm::requester::Handler<pldm::requester::Request>* handler; 335 336 /** @brief D-Bus property changed signal match */ 337 std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch; 338 339 /** @brief D-Bus property changed signal match */ 340 std::unique_ptr<sdbusplus::bus::match_t> powerStateOffMatch; 341 342 /** @brief Timer used for monitoring surveillance pings from host */ 343 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; 344 345 bool hostOff = true; 346 347 bool hostTransitioningToOff; 348 349 int setEventReceiverCnt = 0; 350 }; 351 352 /** @brief Method to encode code update event msg 353 * @param[in] eventType - type of event 354 * @param[in] eventDataVec - vector of event data to be sent to host 355 * @param[in/out] requestMsg - request msg to be encoded 356 * @param[in] instanceId - instance ID 357 * @return PLDM status code 358 */ 359 int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec, 360 std::vector<uint8_t>& requestMsg, uint8_t instanceId); 361 362 } // namespace oem_ibm_platform 363 364 } // namespace responder 365 366 } // namespace pldm 367