blob: 9e5fd59f19123262d6af3656b612b501648ef8c3 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "read_fru_data.hpp"
2
3#include "fruread.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Vernon Mauerye08fbff2019-04-03 09:19:34 -07005#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07006#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07007#include <ipmid/utils.hpp>
Marri Devender Rao0acf0572017-07-03 12:25:47 -05008#include <phosphor-logging/elog-errors.hpp>
George Liu52591a92024-07-19 09:18:50 +08009#include <phosphor-logging/lg2.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070010#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070011#include <xyz/openbmc_project/Common/error.hpp>
12
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050013#include <algorithm>
14#include <map>
15
Marri Devender Rao0acf0572017-07-03 12:25:47 -050016extern const FruMap frus;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050017namespace ipmi
18{
19namespace fru
20{
William A. Kennington III4c008022018-10-12 17:18:14 -070021
Marri Devender Rao0acf0572017-07-03 12:25:47 -050022using namespace phosphor::logging;
23using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -070024 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Lei YU4b0ddb62019-01-25 16:43:50 +080025std::unique_ptr<sdbusplus::bus::match_t> matchPtr
26 __attribute__((init_priority(101)));
Marri Devender Rao0acf0572017-07-03 12:25:47 -050027
Marri Devender Rao0acf0572017-07-03 12:25:47 -050028namespace cache
29{
Patrick Venture0b02be92018-08-31 11:55:55 -070030// User initiate read FRU info area command followed by
31// FRU read command. Also data is read in small chunks of
32// the specified offset and count.
33// Caching the data which will be invalidated when ever there
34// is a change in FRU properties.
35FRUAreaMap fruMap;
36} // namespace cache
Marri Devender Rao0acf0572017-07-03 12:25:47 -050037/**
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050038 * @brief Read all the property value's for the specified interface
39 * from Inventory.
Marri Devender Rao0acf0572017-07-03 12:25:47 -050040 *
Marri Devender Rao0acf0572017-07-03 12:25:47 -050041 * @param[in] intf Interface
Marri Devender Rao0acf0572017-07-03 12:25:47 -050042 * @param[in] path Object path
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050043 * @return map of properties
Marri Devender Rao0acf0572017-07-03 12:25:47 -050044 */
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050045ipmi::PropertyMap readAllProperties(const std::string& intf,
Patrick Venture0b02be92018-08-31 11:55:55 -070046 const std::string& path)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050047{
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050048 ipmi::PropertyMap properties;
Patrick Williams5d82f472022-07-22 19:26:53 -050049 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Patrick Venturec26cc712019-07-25 15:11:59 -070050 std::string service;
51 std::string objPath;
52
53 // Is the path the full dbus path?
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030054 if (path.find(xyzPrefix) != std::string::npos)
Patrick Venturec26cc712019-07-25 15:11:59 -070055 {
56 service = ipmi::getService(bus, intf, path);
57 objPath = path;
58 }
59 else
60 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030061 service = ipmi::getService(bus, invMgrInterface, invObjPath);
62 objPath = invObjPath + path;
Patrick Venturec26cc712019-07-25 15:11:59 -070063 }
64
Patrick Venture0b02be92018-08-31 11:55:55 -070065 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030066 propInterface, "GetAll");
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050067 method.append(intf);
Patrick Venturec26cc712019-07-25 15:11:59 -070068 try
69 {
70 auto reply = bus.call(method);
71 reply.read(properties);
72 }
Patrick Williams5d82f472022-07-22 19:26:53 -050073 catch (const sdbusplus::exception_t& e)
Marri Devender Rao0acf0572017-07-03 12:25:47 -050074 {
Patrick Venture0b02be92018-08-31 11:55:55 -070075 // If property is not found simply return empty value
George Liu52591a92024-07-19 09:18:50 +080076 lg2::error("Error in reading property values: {ERROR}, path: {PATH}, "
77 "interface: {INTERFACE}",
78 "ERROR", e, "PATH", objPath, "INTERFACE", intf);
Marri Devender Rao0acf0572017-07-03 12:25:47 -050079 }
Patrick Venturec26cc712019-07-25 15:11:59 -070080
Marri Devender Rao18aae1f2017-07-26 00:33:26 -050081 return properties;
Marri Devender Rao0acf0572017-07-03 12:25:47 -050082}
83
Patrick Williams5d82f472022-07-22 19:26:53 -050084void processFruPropChange(sdbusplus::message_t& msg)
Marri Devender Rao908f7502017-07-10 01:49:54 -050085{
Patrick Venture0b02be92018-08-31 11:55:55 -070086 if (cache::fruMap.empty())
Marri Devender Rao908f7502017-07-10 01:49:54 -050087 {
88 return;
89 }
90 std::string path = msg.get_path();
Patrick Venture0b02be92018-08-31 11:55:55 -070091 // trim the object base path, if found at the beginning
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030092 if (path.compare(0, strlen(invObjPath), invObjPath) == 0)
Marri Devender Rao908f7502017-07-10 01:49:54 -050093 {
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +030094 path.erase(0, strlen(invObjPath));
Marri Devender Rao908f7502017-07-10 01:49:54 -050095 }
Patrick Venture438fb792018-10-23 19:51:10 -070096 for (const auto& [fruId, instanceList] : frus)
Marri Devender Rao908f7502017-07-10 01:49:54 -050097 {
Patrick Williams369824e2023-10-20 11:18:23 -050098 auto found = std::find_if(
99 instanceList.begin(), instanceList.end(),
100 [&path](const auto& iter) { return (iter.path == path); });
Patrick Ventureb0431c72018-10-22 14:57:36 -0700101
102 if (found != instanceList.end())
Marri Devender Rao908f7502017-07-10 01:49:54 -0500103 {
104 cache::fruMap.erase(fruId);
105 break;
106 }
107 }
108}
109
Patrick Venture0b02be92018-08-31 11:55:55 -0700110// register for fru property change
Marri Devender Rao908f7502017-07-10 01:49:54 -0500111int registerCallbackHandler()
112{
Patrick Venture0b02be92018-08-31 11:55:55 -0700113 if (matchPtr == nullptr)
Marri Devender Rao908f7502017-07-10 01:49:54 -0500114 {
115 using namespace sdbusplus::bus::match::rules;
Patrick Williams5d82f472022-07-22 19:26:53 -0500116 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Marri Devender Rao908f7502017-07-10 01:49:54 -0500117 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
118 bus,
Kirill Pakhomovb118c4b2020-05-08 18:38:26 +0300119 path_namespace(invObjPath) + type::signal() +
120 member("PropertiesChanged") + interface(propInterface),
Marri Devender Rao908f7502017-07-10 01:49:54 -0500121 std::bind(processFruPropChange, std::placeholders::_1));
122 }
123 return 0;
124}
125
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500126/**
127 * @brief Read FRU property values from Inventory
128 *
129 * @param[in] fruNum FRU id
130 * @return populate FRU Inventory data
131 */
132FruInventoryData readDataFromInventory(const FRUId& fruNum)
133{
134 auto iter = frus.find(fruNum);
135 if (iter == frus.end())
136 {
George Liu52591a92024-07-19 09:18:50 +0800137 lg2::error("Unsupported FRU ID: {FRUID}", "FRUID", fruNum);
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500138 elog<InternalFailure>();
139 }
140
141 FruInventoryData data;
142 auto& instanceList = iter->second;
143 for (auto& instance : instanceList)
144 {
Ratan Gupta00330972018-01-19 16:23:10 +0530145 for (auto& intf : instance.interfaces)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500146 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400147 ipmi::PropertyMap allProp =
148 readAllProperties(intf.first, instance.path);
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500149 for (auto& properties : intf.second)
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500150 {
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500151 auto iter = allProp.find(properties.first);
152 if (iter != allProp.end())
153 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700154 data[properties.second.section].emplace(
Patrick Venture45e93cb2019-07-25 15:03:19 -0700155 properties.second.property,
156 std::move(
157 std::get<std::string>(allProp[properties.first])));
Marri Devender Rao18aae1f2017-07-26 00:33:26 -0500158 }
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500159 }
160 }
161 }
162 return data;
163}
164
165const FruAreaData& getFruAreaData(const FRUId& fruNum)
166{
167 auto iter = cache::fruMap.find(fruNum);
168 if (iter != cache::fruMap.end())
169 {
170 return iter->second;
171 }
172 auto invData = readDataFromInventory(fruNum);
173
Patrick Venture0b02be92018-08-31 11:55:55 -0700174 // Build area info based on inventory data
Marri Devender Rao0acf0572017-07-03 12:25:47 -0500175 FruAreaData newdata = buildFruAreaData(std::move(invData));
176 cache::fruMap.emplace(fruNum, std::move(newdata));
177 return cache::fruMap.at(fruNum);
178}
Patrick Venture0b02be92018-08-31 11:55:55 -0700179} // namespace fru
180} // namespace ipmi