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