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" |
| 5 | #include "utils.hpp" |
| 6 | |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 7 | #include <host-ipmid/ipmid-api.h> |
| 8 | |
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; |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 25 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 26 | |
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()}; |
| 53 | auto service = ipmi::getService(bus, INV_INTF, OBJ_PATH); |
| 54 | std::string objPath = OBJ_PATH + path; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 55 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 56 | PROP_INTF, "GetAll"); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 57 | method.append(intf); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 58 | auto reply = bus.call(method); |
| 59 | if (reply.is_method_error()) |
| 60 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 61 | // If property is not found simply return empty value |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 62 | log<level::ERR>("Error in reading property values from inventory", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 63 | entry("INTERFACE=%s", intf.c_str()), |
| 64 | entry("PATH=%s", objPath.c_str())); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 65 | return properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 66 | } |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 67 | reply.read(properties); |
| 68 | return properties; |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 71 | void processFruPropChange(sdbusplus::message::message& msg) |
| 72 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 73 | if (cache::fruMap.empty()) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 74 | { |
| 75 | return; |
| 76 | } |
| 77 | std::string path = msg.get_path(); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 78 | // trim the object base path, if found at the beginning |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 79 | if (path.compare(0, strlen(OBJ_PATH), OBJ_PATH) == 0) |
| 80 | { |
| 81 | path.erase(0, strlen(OBJ_PATH)); |
| 82 | } |
| 83 | for (auto& fru : frus) |
| 84 | { |
| 85 | bool found = false; |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 86 | auto& instanceList = fru.second; |
| 87 | for (auto& instance : instanceList) |
| 88 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 89 | if (instance.path == path) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 90 | { |
| 91 | found = true; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | if (found) |
| 96 | { |
Patrick Venture | 4491a46 | 2018-10-13 13:00:42 -0700 | [diff] [blame] | 97 | auto& fruId = fru.first; |
| 98 | |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 99 | cache::fruMap.erase(fruId); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 105 | // register for fru property change |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 106 | int registerCallbackHandler() |
| 107 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 108 | if (matchPtr == nullptr) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 109 | { |
| 110 | using namespace sdbusplus::bus::match::rules; |
| 111 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 112 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 113 | bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 114 | path_namespace(OBJ_PATH) + type::signal() + |
| 115 | member("PropertiesChanged") + interface(PROP_INTF), |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 116 | std::bind(processFruPropChange, std::placeholders::_1)); |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 121 | /** |
| 122 | * @brief Read FRU property values from Inventory |
| 123 | * |
| 124 | * @param[in] fruNum FRU id |
| 125 | * @return populate FRU Inventory data |
| 126 | */ |
| 127 | FruInventoryData readDataFromInventory(const FRUId& fruNum) |
| 128 | { |
| 129 | auto iter = frus.find(fruNum); |
| 130 | if (iter == frus.end()) |
| 131 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 132 | log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 133 | elog<InternalFailure>(); |
| 134 | } |
| 135 | |
| 136 | FruInventoryData data; |
| 137 | auto& instanceList = iter->second; |
| 138 | for (auto& instance : instanceList) |
| 139 | { |
Ratan Gupta | 0033097 | 2018-01-19 16:23:10 +0530 | [diff] [blame] | 140 | for (auto& intf : instance.interfaces) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 141 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 142 | ipmi::PropertyMap allProp = |
| 143 | readAllProperties(intf.first, instance.path); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 144 | for (auto& properties : intf.second) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 145 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 146 | auto iter = allProp.find(properties.first); |
| 147 | if (iter != allProp.end()) |
| 148 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 149 | data[properties.second.section].emplace( |
| 150 | properties.first, |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 151 | std::move(variant_ns::get<std::string>( |
| 152 | allProp[properties.first]))); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 153 | } |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | return data; |
| 158 | } |
| 159 | |
| 160 | const FruAreaData& getFruAreaData(const FRUId& fruNum) |
| 161 | { |
| 162 | auto iter = cache::fruMap.find(fruNum); |
| 163 | if (iter != cache::fruMap.end()) |
| 164 | { |
| 165 | return iter->second; |
| 166 | } |
| 167 | auto invData = readDataFromInventory(fruNum); |
| 168 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 169 | // Build area info based on inventory data |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 170 | FruAreaData newdata = buildFruAreaData(std::move(invData)); |
| 171 | cache::fruMap.emplace(fruNum, std::move(newdata)); |
| 172 | return cache::fruMap.at(fruNum); |
| 173 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 174 | } // namespace fru |
| 175 | } // namespace ipmi |