blob: 895c9f210fd009578809392e1e4e8d7203b24c5f [file] [log] [blame]
Sampa Misraaea5dde2020-08-31 08:33:47 -05001#pragma once
Varsha Kaverappabb585b22020-09-10 06:15:42 -05002
Manojkiran Edaa31ceb92021-07-22 09:19:02 +05303#include "collect_slot_vpd.hpp"
4#include "common/utils.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -05005#include "inband_code_update.hpp"
6#include "libpldmresponder/oem_handler.hpp"
Varsha Kaverappabb585b22020-09-10 06:15:42 -05007#include "libpldmresponder/pdr_utils.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -05008#include "libpldmresponder/platform.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05009#include "requester/handler.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050010
George Liuc453e162022-12-21 17:16:23 +080011#include <libpldm/entity.h>
Andrew Jeffery21f128d2024-01-15 15:34:26 +103012#include <libpldm/oem/ibm/state_set.h>
George Liuc453e162022-12-21 17:16:23 +080013#include <libpldm/platform.h>
George Liuc453e162022-12-21 17:16:23 +080014
Sagar Srinivas18145f72022-04-11 07:38:26 -050015#include <sdbusplus/bus/match.hpp>
16#include <sdeventplus/event.hpp>
17#include <sdeventplus/utility/timer.hpp>
18
Christian Geddes7f9523c2021-08-03 13:44:38 -050019typedef ibm_oem_pldm_state_set_firmware_update_state_values CodeUpdateState;
20
Sampa Misraaea5dde2020-08-31 08:33:47 -050021namespace pldm
22{
Sampa Misraaea5dde2020-08-31 08:33:47 -050023namespace responder
24{
Manojkiran Edaa31ceb92021-07-22 09:19:02 +053025using ObjectPath = std::string;
26using AssociatedEntityMap = std::map<ObjectPath, pldm_entity>;
Sampa Misraaea5dde2020-08-31 08:33:47 -050027namespace oem_ibm_platform
28{
Archana Kakanie26d13f2025-03-09 23:17:46 -050029using AttributeName = std::string;
30using AttributeType = std::string;
31using ReadonlyStatus = bool;
32using DisplayName = std::string;
33using Description = std::string;
34using MenuPath = std::string;
35using CurrentValue = std::variant<int64_t, std::string>;
36using DefaultValue = std::variant<int64_t, std::string>;
37using OptionString = std::string;
38using OptionValue = std::variant<int64_t, std::string>;
39using Option = std::vector<std::tuple<OptionString, OptionValue>>;
40using BIOSTableObj =
41 std::tuple<AttributeType, ReadonlyStatus, DisplayName, Description,
42 MenuPath, CurrentValue, DefaultValue, Option>;
43using BaseBIOSTable = std::map<AttributeName, BIOSTableObj>;
44using PendingObj = std::tuple<AttributeType, CurrentValue>;
45using PendingAttributes = std::map<AttributeName, PendingObj>;
46
Sagar Srinivas78a225a2020-08-27 00:52:20 -050047constexpr uint16_t ENTITY_INSTANCE_0 = 0;
48constexpr uint16_t ENTITY_INSTANCE_1 = 1;
49
Sagar Srinivas3687e2b2023-04-10 05:08:28 -050050constexpr uint32_t BMC_PDR_START_RANGE = 0x00000000;
51constexpr uint32_t BMC_PDR_END_RANGE = 0x00FFFFFF;
52constexpr uint32_t HOST_PDR_START_RANGE = 0x01000000;
53constexpr uint32_t HOST_PDR_END_RANGE = 0x01FFFFFF;
54
Sagar Srinivas18145f72022-04-11 07:38:26 -050055const pldm::pdr::TerminusID HYPERVISOR_TID = 208;
56
57static constexpr uint8_t HEARTBEAT_TIMEOUT_DELTA = 10;
58
Sagar Srinivas7f760b32021-05-12 07:46:56 -050059enum SetEventReceiverCount
60{
61 SET_EVENT_RECEIVER_SENT = 0x2,
62};
63
Sampa Misraaea5dde2020-08-31 08:33:47 -050064class Handler : public oem_platform::Handler
65{
66 public:
67 Handler(const pldm::utils::DBusHandler* dBusIntf,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +053068 pldm::responder::CodeUpdate* codeUpdate,
69 pldm::responder::SlotHandler* slotHandler, int mctp_fd,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093070 uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
Brad Bishop5079ac42021-08-19 18:35:06 -040071 sdeventplus::Event& event,
Sampa Misrac0c79482021-06-02 08:01:54 -050072 pldm::requester::Handler<pldm::requester::Request>* handler) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040073 oem_platform::Handler(dBusIntf), codeUpdate(codeUpdate),
Manojkiran Edaa31ceb92021-07-22 09:19:02 +053074 slotHandler(slotHandler), platformHandler(nullptr), mctp_fd(mctp_fd),
75 mctp_eid(mctp_eid), instanceIdDb(instanceIdDb), event(event),
76 handler(handler),
Sagar Srinivas18145f72022-04-11 07:38:26 -050077 timer(event, std::bind(std::mem_fn(&Handler::setSurvTimer), this,
78 HYPERVISOR_TID, false)),
79 hostTransitioningToOff(true)
Sampa Misraaea5dde2020-08-31 08:33:47 -050080 {
81 codeUpdate->setVersions();
Sagar Srinivas7f760b32021-05-12 07:46:56 -050082 setEventReceiverCnt = 0;
83
84 using namespace sdbusplus::bus::match::rules;
Patrick Williams84b790c2022-07-22 19:26:56 -050085 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Sagar Srinivas7f760b32021-05-12 07:46:56 -050086 pldm::utils::DBusHandler::getBus(),
87 propertiesChanged("/xyz/openbmc_project/state/host0",
88 "xyz.openbmc_project.State.Host"),
Patrick Williams84b790c2022-07-22 19:26:56 -050089 [this](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040090 pldm::utils::DbusChangedProps props{};
91 std::string intf;
92 msg.read(intf, props);
93 const auto itr = props.find("CurrentHostState");
94 if (itr != props.end())
Sagar Srinivas7f760b32021-05-12 07:46:56 -050095 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040096 pldm::utils::PropertyValue value = itr->second;
97 auto propVal = std::get<std::string>(value);
98 if (propVal ==
99 "xyz.openbmc_project.State.Host.HostState.Off")
100 {
101 hostOff = true;
102 setEventReceiverCnt = 0;
103 disableWatchDogTimer();
104 startStopTimer(false);
105 }
106 else if (propVal ==
107 "xyz.openbmc_project.State.Host.HostState.Running")
108 {
109 hostOff = false;
110 hostTransitioningToOff = false;
111 }
112 else if (
113 propVal ==
114 "xyz.openbmc_project.State.Host.HostState.TransitioningToOff")
115 {
116 hostTransitioningToOff = true;
117 }
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500118 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400119 });
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500120
Patrick Williams4f152f92024-06-18 02:38:34 -0500121 powerStateOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500122 pldm::utils::DBusHandler::getBus(),
123 propertiesChanged("/xyz/openbmc_project/state/chassis0",
124 "xyz.openbmc_project.State.Chassis"),
Andrew Jefferya933c092024-07-25 20:51:47 +0930125 [](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400126 pldm::utils::DbusChangedProps props{};
127 std::string intf;
128 msg.read(intf, props);
129 const auto itr = props.find("CurrentPowerState");
130 if (itr != props.end())
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500131 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400132 pldm::utils::PropertyValue value = itr->second;
133 auto propVal = std::get<std::string>(value);
134 if (propVal ==
135 "xyz.openbmc_project.State.Chassis.PowerState.Off")
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500136 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400137 static constexpr auto searchpath =
138 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
139 int depth = 0;
140 std::vector<std::string> powerInterface = {
141 "xyz.openbmc_project.State.Decorator.PowerState"};
142 pldm::utils::GetSubTreeResponse response =
143 pldm::utils::DBusHandler().getSubtree(
144 searchpath, depth, powerInterface);
145 for (const auto& [objPath, serviceMap] : response)
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500146 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400147 pldm::utils::DBusMapping dbusMapping{
148 objPath,
149 "xyz.openbmc_project.State.Decorator.PowerState",
150 "PowerState", "string"};
151 value =
152 "xyz.openbmc_project.State.Decorator.PowerState.State.Off";
153 try
154 {
155 pldm::utils::DBusHandler().setDbusProperty(
156 dbusMapping, value);
157 }
158 catch (const std::exception& e)
159 {
160 error(
161 "Unable to set the slot power state to Off error - {ERROR}",
162 "ERROR", e);
163 }
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500164 }
165 }
166 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400167 });
Archana Kakanie26d13f2025-03-09 23:17:46 -0500168 updateBIOSMatch = std::make_unique<sdbusplus::bus::match_t>(
169 pldm::utils::DBusHandler::getBus(),
170 propertiesChanged("/xyz/openbmc_project/bios_config/manager",
171 "xyz.openbmc_project.BIOSConfig.Manager"),
172 [codeUpdate](sdbusplus::message_t& msg) {
173 constexpr auto propertyName = "PendingAttributes";
174 using Value =
175 std::variant<std::string, PendingAttributes, BaseBIOSTable>;
176 using Properties = std::map<pldm::utils::DbusProp, Value>;
177 Properties props{};
178 std::string intf;
179 msg.read(intf, props);
180 auto valPropMap = props.find(propertyName);
181 if (valPropMap == props.end())
182 {
183 return;
184 }
185
186 PendingAttributes pendingAttributes =
187 std::get<PendingAttributes>(valPropMap->second);
188 for (auto it : pendingAttributes)
189 {
190 if (it.first == "fw_boot_side")
191 {
192 auto& [attributeType, attributevalue] = it.second;
193 std::string nextBootSideAttr =
194 std::get<std::string>(attributevalue);
195 std::string nextBootSide =
196 (nextBootSideAttr == "Perm" ? Pside : Tside);
197 codeUpdate->setNextBootSide(nextBootSide);
198 }
199 }
200 });
Sampa Misraaea5dde2020-08-31 08:33:47 -0500201 }
202
203 int getOemStateSensorReadingsHandler(
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500204 pldm::pdr::EntityType entityType,
205 pldm::pdr::EntityInstance entityInstance,
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530206 pldm::pdr::ContainerID containerId, pldm::pdr::StateSetId stateSetId,
207 pldm::pdr::CompositeCount compSensorCnt, uint16_t sensorId,
Sampa Misraaea5dde2020-08-31 08:33:47 -0500208 std::vector<get_sensor_state_field>& stateField);
209
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500210 int oemSetStateEffecterStatesHandler(
Varsha Kaverappa3fbd39e2020-09-28 01:40:22 -0500211 uint16_t entityType, uint16_t entityInstance, uint16_t stateSetId,
212 uint8_t compEffecterCnt,
213 std::vector<set_effecter_state_field>& stateField, uint16_t effecterId);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500214
215 /** @brief Method to set the platform handler in the
216 * oem_ibm_handler class
217 * @param[in] handler - pointer to PLDM platform handler
218 */
219 void setPlatformHandler(pldm::responder::platform::Handler* handler);
220
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500221 /** @brief Method to fetch the effecter ID of the code update PDRs
222 *
223 * @return platformHandler->getNextEffecterId() - returns the
224 * effecter ID from the platform handler
225 */
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530226 virtual uint16_t getNextEffecterId()
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500227 {
228 return platformHandler->getNextEffecterId();
229 }
230
231 /** @brief Method to fetch the sensor ID of the code update PDRs
232 *
233 * @return platformHandler->getNextSensorId() - returns the
234 * Sensor ID from the platform handler
235 */
Manojkiran Eda6b1d8832021-03-27 11:25:06 +0530236 virtual uint16_t getNextSensorId()
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500237 {
238 return platformHandler->getNextSensorId();
239 }
240
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530241 /** @brief Get std::map associated with the entity
242 * key: object path
243 * value: pldm_entity
244 *
245 * @return std::map<ObjectPath, pldm_entity>
246 */
247 virtual const AssociatedEntityMap& getAssociateEntityMap()
248 {
249 return platformHandler->getAssociateEntityMap();
250 }
251
Sagar Srinivas78a225a2020-08-27 00:52:20 -0500252 /** @brief Method to Generate the OEM PDRs
253 *
254 * @param[in] repo - instance of concrete implementation of Repo
255 */
256 void buildOEMPDR(pdr_utils::Repo& repo);
257
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500258 /** @brief Method to send code update event to host
259 * @param[in] sensorId - sendor ID
260 * @param[in] sensorEventClass - event class of sensor
261 * @param[in] sensorOffset - sensor offset
262 * @param[in] eventState - new code update event state
263 * @param[in] prevEventState - previous code update event state
264 * @return none
265 */
266 void sendStateSensorEvent(uint16_t sensorId,
267 enum sensor_event_class_states sensorEventClass,
268 uint8_t sensorOffset, uint8_t eventState,
269 uint8_t prevEventState);
270
271 /** @brief Method to send encoded request msg of code update event to host
272 * @param[in] requestMsg - encoded request msg
Sampa Misrac0c79482021-06-02 08:01:54 -0500273 * @param[in] instanceId - instance id of the message
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500274 * @return PLDM status code
275 */
Sampa Misrac0c79482021-06-02 08:01:54 -0500276 int sendEventToHost(std::vector<uint8_t>& requestMsg, uint8_t instanceId);
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500277
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500278 /** @brief _processEndUpdate processes the actual work that needs
279 * to be carried out after EndUpdate effecter is set. This is done async
280 * after sending response for EndUpdate set effecter
281 * @param[in] source - sdeventplus event source
282 */
283 void _processEndUpdate(sdeventplus::source::EventBase& source);
284
285 /** @brief _processStartUpdate processes the actual work that needs
286 * to be carried out after StartUpdate effecter is set. This is done async
287 * after sending response for StartUpdate set effecter
288 * @param[in] source - sdeventplus event source
289 */
290 void _processStartUpdate(sdeventplus::source::EventBase& source);
291
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600292 /** @brief _processSystemReboot processes the actual work that needs to be
293 * carried out after the System Power State effecter is set to reboot
294 * the system
295 * @param[in] source - sdeventplus event source
296 */
297 void _processSystemReboot(sdeventplus::source::EventBase& source);
298
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500299 /*keeps track how many times setEventReceiver is sent */
300 void countSetEventReceiver()
301 {
302 setEventReceiverCnt++;
303 }
304
305 /* disables watchdog if running and Host is up */
306 void checkAndDisableWatchDog();
307
Sagar Srinivas79669c92021-04-28 15:43:30 -0500308 /** @brief To check if the watchdog app is running
309 *
310 * @return the running status of watchdog app
311 */
312 bool watchDogRunning();
313
314 /** @brief Method to reset the Watchdog timer on receiving platform Event
315 * Message for heartbeat elapsed time from Hostboot
316 */
317 void resetWatchDogTimer();
318
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500319 /** @brief To disable to the watchdog timer on host poweron completion*/
320 void disableWatchDogTimer();
321
Pavithra Barithaya99854a72021-09-29 06:58:11 -0500322 /** @brief to check the BMC state*/
323 int checkBMCState();
324
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500325 /** @brief update the dbus object paths */
326 void updateOemDbusPaths(std::string& dbusPath);
327
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500328 /** @brief Method to fetch the last BMC record from the PDR repo
329 *
330 * @param[in] repo - pointer to BMC's primary PDR repo
331 *
332 * @return the last BMC record from the repo
333 */
334 const pldm_pdr_record* fetchLastBMCRecord(const pldm_pdr* repo);
335
336 /** @brief Method to check if the record handle passed is in remote PDR
337 * record handle range
338 *
339 * @param[in] record_handle - record handle of the PDR
340 *
341 * @return true if record handle passed is in host PDR record handle range
342 */
343 bool checkRecordHandleInRange(const uint32_t& record_handle);
344
Sagar Srinivas90314a32023-10-17 10:38:03 -0500345 /** *brief Method to call the setEventReceiver command*/
346 void processSetEventReceiver();
347
348 /** @brief Method to call the setEventReceiver through the platform
349 * handler
350 */
351 virtual void setEventReceiver()
352 {
353 platformHandler->setEventReceiver();
354 }
355
Sagar Srinivas18145f72022-04-11 07:38:26 -0500356 /** @brief Method to Enable/Disable timer to see if remote terminus sends
357 * the surveillance ping and logs informational error if remote terminus
358 * fails to send the surveillance pings
359 *
360 * @param[in] tid - TID of the remote terminus
361 * @param[in] value - true or false, to indicate if the timer is
362 * running or not
363 */
364 void setSurvTimer(uint8_t tid, bool value);
365
Sampa Misraaea5dde2020-08-31 08:33:47 -0500366 ~Handler() = default;
367
368 pldm::responder::CodeUpdate* codeUpdate; //!< pointer to CodeUpdate object
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530369
370 pldm::responder::SlotHandler*
371 slotHandler; //!< pointer to SlotHandler object
372
Sampa Misraaea5dde2020-08-31 08:33:47 -0500373 pldm::responder::platform::Handler*
374 platformHandler; //!< pointer to PLDM platform handler
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500375
376 /** @brief fd of MCTP communications socket */
377 int mctp_fd;
378
379 /** @brief MCTP EID of host firmware */
380 uint8_t mctp_eid;
381
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930382 /** @brief reference to an InstanceIdDb object, used to obtain a PLDM
383 * instance id. */
384 pldm::InstanceIdDb& instanceIdDb;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500385 /** @brief sdeventplus event source */
386 std::unique_ptr<sdeventplus::source::Defer> assembleImageEvent;
387 std::unique_ptr<sdeventplus::source::Defer> startUpdateEvent;
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600388 std::unique_ptr<sdeventplus::source::Defer> systemRebootEvent;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500389
Manojkiran Edaa31ceb92021-07-22 09:19:02 +0530390 /** @brief Effecterid to dbus object path map
391 */
392 std::unordered_map<uint16_t, std::string> effecterIdToDbusMap;
393
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500394 /** @brief reference of main event loop of pldmd, primarily used to schedule
395 * work
396 */
397 sdeventplus::Event& event;
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600398
399 private:
Sagar Srinivas18145f72022-04-11 07:38:26 -0500400 /** @brief Method to reset or stop the surveillance timer
401 *
402 * @param[in] value - true or false, to indicate if the timer
403 * should be reset or turned off
404 */
405 void startStopTimer(bool value);
406
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600407 /** @brief D-Bus property changed signal match for CurrentPowerState*/
Patrick Williams84b790c2022-07-22 19:26:56 -0500408 std::unique_ptr<sdbusplus::bus::match_t> chassisOffMatch;
Sampa Misrac0c79482021-06-02 08:01:54 -0500409
410 /** @brief PLDM request handler */
411 pldm::requester::Handler<pldm::requester::Request>* handler;
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500412
413 /** @brief D-Bus property changed signal match */
Archana Kakanie26d13f2025-03-09 23:17:46 -0500414 std::unique_ptr<sdbusplus::bus::match_t> updateBIOSMatch;
415
416 /** @brief D-Bus property changed signal match */
Patrick Williams84b790c2022-07-22 19:26:56 -0500417 std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500418
Pavithra Barithaya0fd6a092024-05-31 08:24:27 -0500419 /** @brief D-Bus property changed signal match */
420 std::unique_ptr<sdbusplus::bus::match_t> powerStateOffMatch;
421
Sagar Srinivas18145f72022-04-11 07:38:26 -0500422 /** @brief Timer used for monitoring surveillance pings from host */
423 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
424
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500425 bool hostOff = true;
426
Sagar Srinivas18145f72022-04-11 07:38:26 -0500427 bool hostTransitioningToOff;
428
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500429 int setEventReceiverCnt = 0;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500430};
431
Varsha Kaverappabb585b22020-09-10 06:15:42 -0500432/** @brief Method to encode code update event msg
433 * @param[in] eventType - type of event
434 * @param[in] eventDataVec - vector of event data to be sent to host
435 * @param[in/out] requestMsg - request msg to be encoded
436 * @param[in] instanceId - instance ID
437 * @return PLDM status code
438 */
439int encodeEventMsg(uint8_t eventType, const std::vector<uint8_t>& eventDataVec,
440 std::vector<uint8_t>& requestMsg, uint8_t instanceId);
441
Sampa Misraaea5dde2020-08-31 08:33:47 -0500442} // namespace oem_ibm_platform
443
444} // namespace responder
445
446} // namespace pldm