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