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