Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "read_fru_data.hpp" |
| 2 | |
| 3 | #include "fruread.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 4 | |
Vernon Mauery | e08fbff | 2019-04-03 09:19:34 -0700 | [diff] [blame] | 5 | #include <ipmid/api.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 6 | #include <ipmid/types.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 7 | #include <ipmid/utils.hpp> |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 9 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <map> |
| 14 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 15 | extern const FruMap frus; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 16 | namespace ipmi |
| 17 | { |
| 18 | namespace fru |
| 19 | { |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 20 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 21 | using namespace phosphor::logging; |
| 22 | using InternalFailure = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 23 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 4b0ddb6 | 2019-01-25 16:43:50 +0800 | [diff] [blame] | 24 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr |
| 25 | __attribute__((init_priority(101))); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 26 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 27 | namespace cache |
| 28 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 29 | // User initiate read FRU info area command followed by |
| 30 | // FRU read command. Also data is read in small chunks of |
| 31 | // the specified offset and count. |
| 32 | // Caching the data which will be invalidated when ever there |
| 33 | // is a change in FRU properties. |
| 34 | FRUAreaMap fruMap; |
| 35 | } // namespace cache |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 36 | /** |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 37 | * @brief Read all the property value's for the specified interface |
| 38 | * from Inventory. |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 39 | * |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 40 | * @param[in] intf Interface |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 41 | * @param[in] path Object path |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 42 | * @return map of properties |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 43 | */ |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 44 | ipmi::PropertyMap readAllProperties(const std::string& intf, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 45 | const std::string& path) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 46 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 47 | ipmi::PropertyMap properties; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 48 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 49 | std::string service; |
| 50 | std::string objPath; |
| 51 | |
| 52 | // Is the path the full dbus path? |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 53 | if (path.find(xyzPrefix) != std::string::npos) |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 54 | { |
| 55 | service = ipmi::getService(bus, intf, path); |
| 56 | objPath = path; |
| 57 | } |
| 58 | else |
| 59 | { |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 60 | service = ipmi::getService(bus, invMgrInterface, invObjPath); |
| 61 | objPath = invObjPath + path; |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 64 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 65 | propInterface, "GetAll"); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 66 | method.append(intf); |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 67 | try |
| 68 | { |
| 69 | auto reply = bus.call(method); |
| 70 | reply.read(properties); |
| 71 | } |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 72 | catch (const sdbusplus::exception_t& e) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 73 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 74 | // If property is not found simply return empty value |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 75 | log<level::ERR>("Error in reading property values", |
| 76 | entry("EXCEPTION=%s", e.what()), |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 77 | entry("INTERFACE=%s", intf.c_str()), |
| 78 | entry("PATH=%s", objPath.c_str())); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 79 | } |
Patrick Venture | c26cc71 | 2019-07-25 15:11:59 -0700 | [diff] [blame] | 80 | |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 81 | return properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 84 | void processFruPropChange(sdbusplus::message_t& msg) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 85 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 86 | if (cache::fruMap.empty()) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 87 | { |
| 88 | return; |
| 89 | } |
| 90 | std::string path = msg.get_path(); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 91 | // trim the object base path, if found at the beginning |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 92 | if (path.compare(0, strlen(invObjPath), invObjPath) == 0) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 93 | { |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 94 | path.erase(0, strlen(invObjPath)); |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 95 | } |
Patrick Venture | 438fb79 | 2018-10-23 19:51:10 -0700 | [diff] [blame] | 96 | for (const auto& [fruId, instanceList] : frus) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 97 | { |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 98 | auto found = std::find_if(instanceList.begin(), instanceList.end(), |
| 99 | [&path](const auto& iter) { |
| 100 | return (iter.path == path); |
| 101 | }); |
Patrick Venture | b0431c7 | 2018-10-22 14:57:36 -0700 | [diff] [blame] | 102 | |
| 103 | if (found != instanceList.end()) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 104 | { |
| 105 | cache::fruMap.erase(fruId); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 111 | // register for fru property change |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 112 | int registerCallbackHandler() |
| 113 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 114 | if (matchPtr == nullptr) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 115 | { |
| 116 | using namespace sdbusplus::bus::match::rules; |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 117 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 118 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 119 | bus, |
Kirill Pakhomov | b118c4b | 2020-05-08 18:38:26 +0300 | [diff] [blame] | 120 | path_namespace(invObjPath) + type::signal() + |
| 121 | member("PropertiesChanged") + interface(propInterface), |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 122 | std::bind(processFruPropChange, std::placeholders::_1)); |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 127 | /** |
| 128 | * @brief Read FRU property values from Inventory |
| 129 | * |
| 130 | * @param[in] fruNum FRU id |
| 131 | * @return populate FRU Inventory data |
| 132 | */ |
| 133 | FruInventoryData readDataFromInventory(const FRUId& fruNum) |
| 134 | { |
| 135 | auto iter = frus.find(fruNum); |
| 136 | if (iter == frus.end()) |
| 137 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 138 | log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 139 | elog<InternalFailure>(); |
| 140 | } |
| 141 | |
| 142 | FruInventoryData data; |
| 143 | auto& instanceList = iter->second; |
| 144 | for (auto& instance : instanceList) |
| 145 | { |
Ratan Gupta | 0033097 | 2018-01-19 16:23:10 +0530 | [diff] [blame] | 146 | for (auto& intf : instance.interfaces) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 147 | { |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 148 | ipmi::PropertyMap allProp = readAllProperties(intf.first, |
| 149 | instance.path); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 150 | for (auto& properties : intf.second) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 151 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 152 | auto iter = allProp.find(properties.first); |
| 153 | if (iter != allProp.end()) |
| 154 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 155 | data[properties.second.section].emplace( |
Patrick Venture | 45e93cb | 2019-07-25 15:03:19 -0700 | [diff] [blame] | 156 | properties.second.property, |
| 157 | std::move( |
| 158 | std::get<std::string>(allProp[properties.first]))); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 159 | } |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | return data; |
| 164 | } |
| 165 | |
| 166 | const FruAreaData& getFruAreaData(const FRUId& fruNum) |
| 167 | { |
| 168 | auto iter = cache::fruMap.find(fruNum); |
| 169 | if (iter != cache::fruMap.end()) |
| 170 | { |
| 171 | return iter->second; |
| 172 | } |
| 173 | auto invData = readDataFromInventory(fruNum); |
| 174 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 175 | // Build area info based on inventory data |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 176 | FruAreaData newdata = buildFruAreaData(std::move(invData)); |
| 177 | cache::fruMap.emplace(fruNum, std::move(newdata)); |
| 178 | return cache::fruMap.at(fruNum); |
| 179 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 180 | } // namespace fru |
| 181 | } // namespace ipmi |