blob: 99acdc742f95b4d1adef4cef0be6c317717cb2a0 [file] [log] [blame]
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +05301#include "config.h"
2
Sunny Srivastava867ee752025-04-15 12:24:23 +05303#include "ibm_handler.hpp"
4
Anupama B R56e45372025-06-19 12:54:44 -05005#include "listener.hpp"
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +05306#include "parser.hpp"
7
Sunny Srivastava78a50422025-04-25 11:17:56 +05308#include <utility/common_utility.hpp>
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +05309#include <utility/dbus_utility.hpp>
10#include <utility/json_utility.hpp>
11#include <utility/vpd_specific_utility.hpp>
12
Sunny Srivastava867ee752025-04-15 12:24:23 +053013namespace vpd
14{
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053015IbmHandler::IbmHandler(
16 std::shared_ptr<Worker>& o_worker,
17 std::shared_ptr<BackupAndRestore>& o_backupAndRestoreObj,
18 const std::shared_ptr<sdbusplus::asio::dbus_interface>& i_iFace,
Anupama B Rda9806b2025-08-29 02:41:10 -050019 const std::shared_ptr<sdbusplus::asio::dbus_interface>& i_progressiFace,
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053020 const std::shared_ptr<boost::asio::io_context>& i_ioCon,
21 const std::shared_ptr<sdbusplus::asio::connection>& i_asioConnection) :
22 m_worker(o_worker), m_backupAndRestoreObj(o_backupAndRestoreObj),
Anupama B Rda9806b2025-08-29 02:41:10 -050023 m_interface(i_iFace), m_progressInterface(i_progressiFace),
Souvik Roya5e18b82025-09-25 05:59:56 +000024 m_ioContext(i_ioCon), m_asioConnection(i_asioConnection),
25 m_logger(Logger::getLoggerInstance())
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053026{
Souvik Royc1171732025-10-16 09:27:38 +000027 uint16_t l_errCode{0};
28
29 // check VPD collection mode
30 const auto l_vpdCollectionMode =
31 commonUtility::isFieldModeEnabled()
32 ? types::VpdCollectionMode::DEFAULT_MODE
33 : commonUtility::getVpdCollectionMode(l_errCode);
34
35 if (l_errCode)
36 {
37 m_logger->logMessage(
38 "Error while trying to read VPD collection mode: " +
39 commonUtility::getErrCodeMsg(l_errCode));
40 l_errCode = 0;
41 }
42
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053043 if (dbusUtility::isChassisPowerOn())
44 {
45 // At power on, less number of FRU(s) needs collection. we can scale
46 // down the threads to reduce CPU utilization.
Souvik Royc1171732025-10-16 09:27:38 +000047 m_worker = std::make_shared<Worker>(
48 INVENTORY_JSON_DEFAULT, constants::VALUE_1, l_vpdCollectionMode);
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053049 }
50 else
51 {
52 // Initialize with default configuration
Souvik Royc1171732025-10-16 09:27:38 +000053 m_worker = std::make_shared<Worker>(INVENTORY_JSON_DEFAULT,
54 constants::MAX_THREADS,
55 l_vpdCollectionMode);
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053056 }
57
58 // Set up minimal things that is needed before bus name is claimed.
Sunny Srivastava78a50422025-04-25 11:17:56 +053059 performInitialSetup();
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053060
61 if (!m_sysCfgJsonObj.empty() &&
Rekha Aparna196e3082025-09-08 20:40:35 -050062 jsonUtility::isBackupAndRestoreRequired(m_sysCfgJsonObj, l_errCode))
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053063 {
64 try
65 {
66 m_backupAndRestoreObj =
67 std::make_shared<BackupAndRestore>(m_sysCfgJsonObj);
68 }
69 catch (const std::exception& l_ex)
70 {
71 logging::logMessage("Back up and restore instantiation failed. {" +
72 std::string(l_ex.what()) + "}");
73
74 EventLogger::createSyncPel(
75 EventLogger::getErrorType(l_ex), types::SeverityType::Warning,
76 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
77 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
78 }
79 }
Rekha Aparna196e3082025-09-08 20:40:35 -050080 else if (l_errCode)
81 {
82 logging::logMessage(
83 "Failed to check if backup & restore required. Error : " +
Rekha Aparnac6159a22025-10-09 12:20:20 +053084 commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna196e3082025-09-08 20:40:35 -050085 }
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053086
Anupama B Rc7565ed2025-06-19 02:08:39 -050087 // Instantiate Listener object
Anupama B R56e45372025-06-19 12:54:44 -050088 m_eventListener = std::make_shared<Listener>(m_worker, m_asioConnection);
89 m_eventListener->registerAssetTagChangeCallback();
90 m_eventListener->registerHostStateChangeCallback();
Souvik Roy5c3a1562025-07-02 01:39:44 -050091 m_eventListener->registerPresenceChangeCallback();
Anupama B Rc7565ed2025-06-19 02:08:39 -050092
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053093 // Instantiate GpioMonitor class
94 m_gpioMonitor =
95 std::make_shared<GpioMonitor>(m_sysCfgJsonObj, m_worker, m_ioContext);
96}
97
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +053098void IbmHandler::SetTimerToDetectVpdCollectionStatus()
99{
100 // Keeping max retry for 2 minutes. TODO: Make it configurable based on
101 // system type.
102 static constexpr auto MAX_RETRY = 12;
103
104 static boost::asio::steady_timer l_timer(*m_ioContext);
105 static uint8_t l_timerRetry = 0;
106
107 auto l_asyncCancelled = l_timer.expires_after(std::chrono::seconds(10));
108
109 (l_asyncCancelled == 0)
110 ? logging::logMessage("Collection Timer started")
111 : logging::logMessage("Collection Timer re-started");
112
113 l_timer.async_wait([this](const boost::system::error_code& ec) {
114 if (ec == boost::asio::error::operation_aborted)
115 {
116 throw std::runtime_error(
117 "Timer to detect thread collection status was aborted");
118 }
119
120 if (ec)
121 {
122 throw std::runtime_error(
123 "Timer to detect thread collection failed");
124 }
125
126 if (m_worker->isAllFruCollectionDone())
127 {
128 // cancel the timer
129 l_timer.cancel();
130 processFailedEeproms();
131
132 // update VPD for powerVS system.
133 ConfigurePowerVsSystem();
134
135 std::cout << "m_worker->isSystemVPDOnDBus() completed" << std::endl;
Anupama B Rda9806b2025-08-29 02:41:10 -0500136 m_progressInterface->set_property(
137 "Status", std::string(constants::vpdCollectionCompleted));
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530138
139 if (m_backupAndRestoreObj)
140 {
141 m_backupAndRestoreObj->backupAndRestore();
142 }
Souvik Roy3d4af4a2025-06-23 04:45:27 -0500143
144 if (m_eventListener)
145 {
146 m_eventListener->registerCorrPropCallBack();
147 }
Souvik Roya5e18b82025-09-25 05:59:56 +0000148
149 // terminate collection logger
150 m_logger->terminateVpdCollectionLogging();
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530151 }
152 else
153 {
154 auto l_threadCount = m_worker->getActiveThreadCount();
155 if (l_timerRetry == MAX_RETRY)
156 {
157 l_timer.cancel();
158 logging::logMessage("Taking too long. Active thread = " +
159 std::to_string(l_threadCount));
Souvik Roya5e18b82025-09-25 05:59:56 +0000160
161 // terminate collection logger
162 m_logger->terminateVpdCollectionLogging();
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530163 }
164 else
165 {
166 l_timerRetry++;
167 logging::logMessage("Collection is in progress for [" +
168 std::to_string(l_threadCount) + "] FRUs.");
169
170 SetTimerToDetectVpdCollectionStatus();
171 }
172 }
173 });
174}
175
176void IbmHandler::checkAndUpdatePowerVsVpd(
177 const nlohmann::json& i_powerVsJsonObj,
178 std::vector<std::string>& o_failedPathList)
179{
180 for (const auto& [l_fruPath, l_recJson] : i_powerVsJsonObj.items())
181 {
182 nlohmann::json l_sysCfgJsonObj{};
183 if (m_worker.get() != nullptr)
184 {
185 l_sysCfgJsonObj = m_worker->getSysCfgJsonObj();
186 }
187
188 // The utility method will handle emty JSON case. No explicit
189 // handling required here.
Rekha Aparna017567a2025-08-13 02:07:06 -0500190 uint16_t l_errCode = 0;
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530191 auto l_inventoryPath = jsonUtility::getInventoryObjPathFromJson(
Rekha Aparna017567a2025-08-13 02:07:06 -0500192 l_sysCfgJsonObj, l_fruPath, l_errCode);
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530193
194 // Mark it as failed if inventory path not found in JSON.
195 if (l_inventoryPath.empty())
196 {
Rekha Aparna017567a2025-08-13 02:07:06 -0500197 if (l_errCode)
198 {
199 logging::logMessage(
200 "Failed to get inventory object path from JSON for FRU [" +
Rekha Aparnac6159a22025-10-09 12:20:20 +0530201 l_fruPath +
202 "], error : " + commonUtility::getErrCodeMsg(l_errCode));
Rekha Aparna017567a2025-08-13 02:07:06 -0500203 }
204
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530205 o_failedPathList.push_back(l_fruPath);
206 continue;
207 }
208
209 // check if the FRU is present
210 if (!dbusUtility::isInventoryPresent(l_inventoryPath))
211 {
212 logging::logMessage(
213 "Inventory not present, skip updating part number. Path: " +
214 l_inventoryPath);
215 continue;
216 }
217
218 // check if the FRU needs CCIN check before updating PN.
219 if (l_recJson.contains("CCIN"))
220 {
221 const auto& l_ccinFromDbus =
222 vpdSpecificUtility::getCcinFromDbus(l_inventoryPath);
223
224 // Not an ideal situation as CCIN can't be empty.
225 if (l_ccinFromDbus.empty())
226 {
227 o_failedPathList.push_back(l_fruPath);
228 continue;
229 }
230
231 std::vector<std::string> l_ccinListFromJson = l_recJson["CCIN"];
232
233 if (find(l_ccinListFromJson.begin(), l_ccinListFromJson.end(),
234 l_ccinFromDbus) == l_ccinListFromJson.end())
235 {
236 // Don't update PN in this case.
237 continue;
238 }
239 }
240
241 for (const auto& [l_recordName, l_kwdJson] : l_recJson.items())
242 {
243 // Record name can't be CCIN, skip processing as it is there for PN
244 // update based on CCIN check.
245 if (l_recordName == constants::kwdCCIN)
246 {
247 continue;
248 }
249
250 for (const auto& [l_kwdName, l_kwdValue] : l_kwdJson.items())
251 {
252 // Is value of type array.
253 if (!l_kwdValue.is_array())
254 {
255 o_failedPathList.push_back(l_fruPath);
256 continue;
257 }
258
259 // Get current FRU Part number.
260 auto l_retVal = dbusUtility::readDbusProperty(
261 constants::pimServiceName, l_inventoryPath,
262 constants::viniInf, constants::kwdFN);
263
264 auto l_ptrToFn = std::get_if<types::BinaryVector>(&l_retVal);
265
266 if (!l_ptrToFn)
267 {
268 o_failedPathList.push_back(l_fruPath);
269 continue;
270 }
271
272 types::BinaryVector l_binaryKwdValue =
273 l_kwdValue.get<types::BinaryVector>();
274 if (l_binaryKwdValue == (*l_ptrToFn))
275 {
276 continue;
277 }
278
279 // Update part number only if required.
280 std::shared_ptr<Parser> l_parserObj =
281 std::make_shared<Parser>(l_fruPath, l_sysCfgJsonObj);
282 if (l_parserObj->updateVpdKeyword(std::make_tuple(
283 l_recordName, l_kwdName, l_binaryKwdValue)) ==
284 constants::FAILURE)
285 {
286 o_failedPathList.push_back(l_fruPath);
287 continue;
288 }
289
290 // update the Asset interface Spare part number explicitly.
291 if (!dbusUtility::callPIM(types::ObjectMap{
292 {l_inventoryPath,
293 {{constants::assetInf,
294 {{"SparePartNumber",
295 std::string(l_binaryKwdValue.begin(),
296 l_binaryKwdValue.end())}}}}}}))
297 {
298 logging::logMessage(
299 "Updating Spare Part Number under Asset interface failed for path [" +
300 l_inventoryPath + "]");
301 }
302
303 // Just needed for logging.
304 std::string l_initialPartNum((*l_ptrToFn).begin(),
305 (*l_ptrToFn).end());
306 std::string l_finalPartNum(l_binaryKwdValue.begin(),
307 l_binaryKwdValue.end());
308 logging::logMessage(
309 "FRU Part number updated for path [" + l_inventoryPath +
310 "]" + "From [" + l_initialPartNum + "]" + " to [" +
311 l_finalPartNum + "]");
312 }
313 }
314 }
315}
316
317void IbmHandler::ConfigurePowerVsSystem()
318{
319 std::vector<std::string> l_failedPathList;
320 try
321 {
322 types::BinaryVector l_imValue = dbusUtility::getImFromDbus();
323 if (l_imValue.empty())
324 {
325 throw DbusException("Invalid IM value read from Dbus");
326 }
327
Rekha Aparnaed09af82025-10-22 20:34:41 -0500328 uint16_t l_errCode = 0;
329 if (!vpdSpecificUtility::isPowerVsConfiguration(l_imValue, l_errCode))
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530330 {
331 // TODO: Should booting be blocked in case of some
332 // misconfigurations?
Rekha Aparnaed09af82025-10-22 20:34:41 -0500333 if (l_errCode)
334 {
335 logging::logMessage(
336 "Failed to check if the system is powerVs Configuration, error : " +
337 commonUtility::getErrCodeMsg(l_errCode));
338 }
339
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530340 return;
341 }
342
343 const nlohmann::json& l_powerVsJsonObj =
Rekha Aparnaadf85262025-09-09 00:41:00 -0500344 jsonUtility::getPowerVsJson(l_imValue, l_errCode);
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530345
346 if (l_powerVsJsonObj.empty())
347 {
Rekha Aparnac6159a22025-10-09 12:20:20 +0530348 throw std::runtime_error("PowerVS Json not found. Error : " +
349 commonUtility::getErrCodeMsg(l_errCode));
Sunny Srivastavac74c8ef2025-04-16 12:45:27 +0530350 }
351
352 checkAndUpdatePowerVsVpd(l_powerVsJsonObj, l_failedPathList);
353
354 if (!l_failedPathList.empty())
355 {
356 throw std::runtime_error(
357 "Part number update failed for following paths: ");
358 }
359 }
360 catch (const std::exception& l_ex)
361 {
362 // TODO log appropriate PEL
363 }
364}
365
366void IbmHandler::processFailedEeproms()
367{
368 if (m_worker.get() != nullptr)
369 {
370 // TODO:
371 // - iterate through list of EEPROMs for which thread creation has
372 // failed
373 // - For each failed EEPROM, trigger VPD collection
374 m_worker->getFailedEepromPaths().clear();
375 }
376}
Sunny Srivastava380efbb2025-04-25 10:28:30 +0530377
Sunny Srivastava78a50422025-04-25 11:17:56 +0530378void IbmHandler::enableMuxChips()
379{
380 if (m_sysCfgJsonObj.empty())
381 {
382 // config JSON should not be empty at this point of execution.
383 throw std::runtime_error("Config JSON is empty. Can't enable muxes");
384 return;
385 }
386
387 if (!m_sysCfgJsonObj.contains("muxes"))
388 {
389 logging::logMessage("No mux defined for the system in config JSON");
390 return;
391 }
392
393 // iterate over each MUX detail and enable them.
394 for (const auto& item : m_sysCfgJsonObj["muxes"])
395 {
396 if (item.contains("holdidlepath"))
397 {
398 std::string cmd = "echo 0 > ";
399 cmd += item["holdidlepath"];
400
401 logging::logMessage("Enabling mux with command = " + cmd);
402
403 commonUtility::executeCmd(cmd);
404 continue;
405 }
406
407 logging::logMessage(
408 "Mux Entry does not have hold idle path. Can't enable the mux");
409 }
410}
411
412void IbmHandler::performInitialSetup()
413{
414 try
415 {
Anupama B R281e2d42025-05-05 10:05:13 -0500416 if (m_worker.get() == nullptr)
417 {
418 throw std::runtime_error(
419 "Worker object not found. Can't perform initial setup.");
420 }
421
422 m_sysCfgJsonObj = m_worker->getSysCfgJsonObj();
Sunny Srivastava78a50422025-04-25 11:17:56 +0530423 if (!dbusUtility::isChassisPowerOn())
424 {
Anupama B R281e2d42025-05-05 10:05:13 -0500425 m_worker->setDeviceTreeAndJson();
426
427 // Since the above function setDeviceTreeAndJson can change the json
428 // which is used, we would need to reacquire the json object again
429 // here.
Sunny Srivastava78a50422025-04-25 11:17:56 +0530430 m_sysCfgJsonObj = m_worker->getSysCfgJsonObj();
431 }
432
Anupama B R95083d42025-10-27 04:54:58 -0500433 // Update BMC postion for RBMC prototype system
434 // Ignore BMC position update in case of any error
435 uint16_t l_errCode = 0;
436 if (isRbmcProtoTypeSystem(l_errCode) && l_errCode == 0)
437 {
438 size_t l_rbmcPosition = constants::VALUE_1;
439 if (isMotherboardEepromAccessible())
440 {
441 l_rbmcPosition = constants::VALUE_0;
442 }
443
444 (void)l_rbmcPosition;
445 // ToDo: Create Object interface map for position property and
446 // publish it on DBus.
447 }
448
Sunny Srivastava78a50422025-04-25 11:17:56 +0530449 // Enable all mux which are used for connecting to the i2c on the
450 // pcie slots for pcie cards. These are not enabled by kernel due to
451 // an issue seen with Castello cards, where the i2c line hangs on a
452 // probe.
453 enableMuxChips();
454
455 // Nothing needs to be done. Service restarted or BMC re-booted for
456 // some reason at system power on.
Sunny Srivastava78a50422025-04-25 11:17:56 +0530457 }
458 catch (const std::exception& l_ex)
459 {
Anupama B R4c65fcd2025-09-01 08:09:00 -0500460 m_worker->setCollectionStatusProperty(SYSTEM_VPD_FILE_PATH,
461 constants::vpdCollectionFailed);
Sunny Srivastava78a50422025-04-25 11:17:56 +0530462 // Any issue in system's inital set up is handled in this catch. Error
463 // will not propogate to manager.
464 EventLogger::createSyncPel(
465 EventLogger::getErrorType(l_ex), types::SeverityType::Critical,
466 __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex),
467 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
468 }
469}
470
Anupama B R7127ab42025-06-26 01:39:08 -0500471void IbmHandler::collectAllFruVpd()
472{
Souvik Roya5e18b82025-09-25 05:59:56 +0000473 // initialize VPD collection logger
474 m_logger->initiateVpdCollectionLogging();
475
Anupama B R7127ab42025-06-26 01:39:08 -0500476 // Setting status to "InProgress", before trigeering VPD collection.
Anupama B Rda9806b2025-08-29 02:41:10 -0500477 m_progressInterface->set_property(
478 "Status", std::string(constants::vpdCollectionInProgress));
Anupama B R7127ab42025-06-26 01:39:08 -0500479 m_worker->collectFrusFromJson();
480 SetTimerToDetectVpdCollectionStatus();
481}
Anupama B R95083d42025-10-27 04:54:58 -0500482
483bool IbmHandler::isRbmcProtoTypeSystem(
484 [[maybe_unused]] uint16_t& o_errCode) const noexcept
485{
486 // TODO:
487 // Parse the system VPD from EEPROM.
488 // Check the IM keyword value. If the IM value indicates an RBMC prototype
489 // system, return true otherwise false.
490 // In case of any error or IM value not found in the map, set error code and
491 // return false.
492
493 return false;
494}
495
496bool IbmHandler::isMotherboardEepromAccessible() const noexcept
497{
498 // TODO: Check whether the motherboard EEPROM is accessible.
499
500 return false;
501}
Sunny Srivastava867ee752025-04-15 12:24:23 +0530502} // namespace vpd