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