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 | |
Patrick Venture | b0431c7 | 2018-10-22 14:57:36 -0700 | [diff] [blame^] | 9 | #include <algorithm> |
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; |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 26 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); |
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 | } |
| 84 | for (auto& fru : frus) |
| 85 | { |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 86 | auto& instanceList = fru.second; |
Patrick Venture | b0431c7 | 2018-10-22 14:57:36 -0700 | [diff] [blame^] | 87 | |
| 88 | auto found = std::find_if( |
| 89 | instanceList.begin(), instanceList.end(), |
| 90 | [&path](const auto& iter) { return (iter.path == path); }); |
| 91 | |
| 92 | if (found != instanceList.end()) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 93 | { |
Patrick Venture | 4491a46 | 2018-10-13 13:00:42 -0700 | [diff] [blame] | 94 | auto& fruId = fru.first; |
| 95 | |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 96 | cache::fruMap.erase(fruId); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 102 | // register for fru property change |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 103 | int registerCallbackHandler() |
| 104 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 105 | if (matchPtr == nullptr) |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 106 | { |
| 107 | using namespace sdbusplus::bus::match::rules; |
| 108 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 109 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 110 | bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 111 | path_namespace(OBJ_PATH) + type::signal() + |
| 112 | member("PropertiesChanged") + interface(PROP_INTF), |
Marri Devender Rao | 908f750 | 2017-07-10 01:49:54 -0500 | [diff] [blame] | 113 | std::bind(processFruPropChange, std::placeholders::_1)); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 118 | /** |
| 119 | * @brief Read FRU property values from Inventory |
| 120 | * |
| 121 | * @param[in] fruNum FRU id |
| 122 | * @return populate FRU Inventory data |
| 123 | */ |
| 124 | FruInventoryData readDataFromInventory(const FRUId& fruNum) |
| 125 | { |
| 126 | auto iter = frus.find(fruNum); |
| 127 | if (iter == frus.end()) |
| 128 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 129 | log<level::ERR>("Unsupported FRU ID ", entry("FRUID=%d", fruNum)); |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 130 | elog<InternalFailure>(); |
| 131 | } |
| 132 | |
| 133 | FruInventoryData data; |
| 134 | auto& instanceList = iter->second; |
| 135 | for (auto& instance : instanceList) |
| 136 | { |
Ratan Gupta | 0033097 | 2018-01-19 16:23:10 +0530 | [diff] [blame] | 137 | for (auto& intf : instance.interfaces) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 138 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 139 | ipmi::PropertyMap allProp = |
| 140 | readAllProperties(intf.first, instance.path); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 141 | for (auto& properties : intf.second) |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 142 | { |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 143 | auto iter = allProp.find(properties.first); |
| 144 | if (iter != allProp.end()) |
| 145 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 146 | data[properties.second.section].emplace( |
| 147 | properties.first, |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 148 | std::move(variant_ns::get<std::string>( |
| 149 | allProp[properties.first]))); |
Marri Devender Rao | 18aae1f | 2017-07-26 00:33:26 -0500 | [diff] [blame] | 150 | } |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | return data; |
| 155 | } |
| 156 | |
| 157 | const FruAreaData& getFruAreaData(const FRUId& fruNum) |
| 158 | { |
| 159 | auto iter = cache::fruMap.find(fruNum); |
| 160 | if (iter != cache::fruMap.end()) |
| 161 | { |
| 162 | return iter->second; |
| 163 | } |
| 164 | auto invData = readDataFromInventory(fruNum); |
| 165 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 166 | // Build area info based on inventory data |
Marri Devender Rao | 0acf057 | 2017-07-03 12:25:47 -0500 | [diff] [blame] | 167 | FruAreaData newdata = buildFruAreaData(std::move(invData)); |
| 168 | cache::fruMap.emplace(fruNum, std::move(newdata)); |
| 169 | return cache::fruMap.at(fruNum); |
| 170 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 171 | } // namespace fru |
| 172 | } // namespace ipmi |