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