SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "manager.hpp" |
| 4 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 5 | #include "editor_impl.hpp" |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 6 | #include "gpioMonitor.hpp" |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 7 | #include "ibm_vpd_utils.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 8 | #include "ipz_parser.hpp" |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 9 | #include "reader_impl.hpp" |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 10 | #include "vpd_exceptions.hpp" |
| 11 | |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 13 | |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 14 | using namespace openpower::vpd::manager; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 15 | using namespace openpower::vpd::constants; |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 16 | using namespace openpower::vpd::inventory; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 17 | using namespace openpower::vpd::manager::editor; |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 18 | using namespace openpower::vpd::manager::reader; |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 19 | using namespace std; |
| 20 | using namespace openpower::vpd::parser; |
| 21 | using namespace openpower::vpd::exceptions; |
| 22 | using namespace phosphor::logging; |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 23 | |
| 24 | namespace openpower |
| 25 | { |
| 26 | namespace vpd |
| 27 | { |
| 28 | namespace manager |
| 29 | { |
| 30 | Manager::Manager(sdbusplus::bus::bus&& bus, const char* busName, |
Priyanga Ramasamy | 9d14934 | 2020-07-16 23:41:26 +0530 | [diff] [blame] | 31 | const char* objPath, const char* /*iFace*/) : |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 32 | ServerObject<ManagerIface>(bus, objPath), |
| 33 | _bus(std::move(bus)), _manager(_bus, objPath) |
| 34 | { |
| 35 | _bus.request_name(busName); |
| 36 | } |
| 37 | |
| 38 | void Manager::run() |
| 39 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 40 | try |
| 41 | { |
| 42 | processJSON(); |
Sunny Srivastava | ecb5c7d | 2021-09-02 07:20:24 -0500 | [diff] [blame^] | 43 | listenHostState(); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 44 | |
| 45 | auto event = sdeventplus::Event::get_default(); |
| 46 | GpioMonitor gpioMon1(jsonFile, event); |
| 47 | |
| 48 | _bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT); |
| 49 | cout << "VPD manager event loop started\n"; |
| 50 | event.loop(); |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 51 | } |
| 52 | catch (const std::exception& e) |
| 53 | { |
| 54 | std::cerr << e.what() << "\n"; |
| 55 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 56 | } |
| 57 | |
Sunny Srivastava | ecb5c7d | 2021-09-02 07:20:24 -0500 | [diff] [blame^] | 58 | void Manager::listenHostState() |
| 59 | { |
| 60 | static std::shared_ptr<sdbusplus::bus::match::match> hostState = |
| 61 | std::make_shared<sdbusplus::bus::match::match>( |
| 62 | _bus, |
| 63 | sdbusplus::bus::match::rules::propertiesChanged( |
| 64 | "/xyz/openbmc_project/state/host0", |
| 65 | "xyz.openbmc_project.State.Host"), |
| 66 | [this](sdbusplus::message::message& msg) { |
| 67 | hostStateCallBack(msg); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | void Manager::hostStateCallBack(sdbusplus::message::message& msg) |
| 72 | { |
| 73 | if (msg.is_method_error()) |
| 74 | { |
| 75 | std::cerr << "Error in reading signal " << std::endl; |
| 76 | } |
| 77 | |
| 78 | Path object; |
| 79 | PropertyMap propMap; |
| 80 | msg.read(object, propMap); |
| 81 | const auto itr = propMap.find("CurrentHostState"); |
| 82 | if (itr != propMap.end()) |
| 83 | { |
| 84 | if (auto hostState = std::get_if<std::string>(&(itr->second))) |
| 85 | { |
| 86 | // implies system is moving from standby to power on state |
| 87 | if (*hostState == "xyz.openbmc_project.State.Host.HostState." |
| 88 | "TransitioningToRunning") |
| 89 | { |
| 90 | // check and perfrom recollection for FRUs replaceable at |
| 91 | // standby. |
| 92 | performVPDRecollection(); |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | std::cerr << "Failed to read Host state" << std::endl; |
| 97 | } |
| 98 | } |
| 99 | |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 100 | void Manager::processJSON() |
| 101 | { |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 102 | std::ifstream json(INVENTORY_JSON_SYM_LINK, std::ios::binary); |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 103 | |
| 104 | if (!json) |
| 105 | { |
| 106 | throw std::runtime_error("json file not found"); |
| 107 | } |
| 108 | |
| 109 | jsonFile = nlohmann::json::parse(json); |
| 110 | if (jsonFile.find("frus") == jsonFile.end()) |
| 111 | { |
| 112 | throw std::runtime_error("frus group not found in json"); |
| 113 | } |
| 114 | |
| 115 | const nlohmann::json& groupFRUS = |
| 116 | jsonFile["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 117 | for (const auto& itemFRUS : groupFRUS.items()) |
| 118 | { |
| 119 | const std::vector<nlohmann::json>& groupEEPROM = |
| 120 | itemFRUS.value().get_ref<const nlohmann::json::array_t&>(); |
| 121 | for (const auto& itemEEPROM : groupEEPROM) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 122 | { |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 123 | bool isMotherboard = false; |
| 124 | if (itemEEPROM["extraInterfaces"].find( |
| 125 | "xyz.openbmc_project.Inventory.Item.Board.Motherboard") != |
| 126 | itemEEPROM["extraInterfaces"].end()) |
| 127 | { |
| 128 | isMotherboard = true; |
| 129 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 130 | frus.emplace(itemEEPROM["inventoryPath"] |
| 131 | .get_ref<const nlohmann::json::string_t&>(), |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 132 | std::make_pair(itemFRUS.key(), isMotherboard)); |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 133 | |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 134 | if (itemEEPROM["extraInterfaces"].find(IBM_LOCATION_CODE_INF) != |
Alpana Kumari | 920408d | 2020-05-14 00:07:03 -0500 | [diff] [blame] | 135 | itemEEPROM["extraInterfaces"].end()) |
| 136 | { |
| 137 | fruLocationCode.emplace( |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 138 | itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF] |
Alpana Kumari | 920408d | 2020-05-14 00:07:03 -0500 | [diff] [blame] | 139 | ["LocationCode"] |
| 140 | .get_ref<const nlohmann::json::string_t&>(), |
| 141 | itemEEPROM["inventoryPath"] |
| 142 | .get_ref<const nlohmann::json::string_t&>()); |
| 143 | } |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 144 | |
Sunny Srivastava | ecb5c7d | 2021-09-02 07:20:24 -0500 | [diff] [blame^] | 145 | if (itemEEPROM.value("replaceableAtStandby", false)) |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 146 | { |
| 147 | replaceableFrus.emplace_back(itemFRUS.key()); |
| 148 | } |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void Manager::writeKeyword(const sdbusplus::message::object_path path, |
| 154 | const std::string recordName, |
| 155 | const std::string keyword, const Binary value) |
| 156 | { |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 157 | try |
| 158 | { |
Santosh Puranik | 8c79681 | 2021-12-01 19:17:56 +0530 | [diff] [blame] | 159 | std::string objPath{path}; |
| 160 | // Strip any inventory prefix in path |
| 161 | if (objPath.find(INVENTORY_PATH) == 0) |
| 162 | { |
| 163 | objPath = objPath.substr(sizeof(INVENTORY_PATH) - 1); |
| 164 | } |
| 165 | |
| 166 | if (frus.find(objPath) == frus.end()) |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 167 | { |
| 168 | throw std::runtime_error("Inventory path not found"); |
| 169 | } |
| 170 | |
Santosh Puranik | 8c79681 | 2021-12-01 19:17:56 +0530 | [diff] [blame] | 171 | inventory::Path vpdFilePath = frus.find(objPath)->second.first; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 172 | |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 173 | // instantiate editor class to update the data |
Santosh Puranik | 8c79681 | 2021-12-01 19:17:56 +0530 | [diff] [blame] | 174 | EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword, objPath); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 175 | edit.updateKeyword(value, true); |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 176 | |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 177 | // if it is a motehrboard FRU need to check for location expansion |
Santosh Puranik | 8c79681 | 2021-12-01 19:17:56 +0530 | [diff] [blame] | 178 | if (frus.find(objPath)->second.second) |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 179 | { |
| 180 | if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE")) |
| 181 | { |
| 182 | edit.expandLocationCode("fcs"); |
| 183 | } |
| 184 | else if (recordName == "VSYS" && |
| 185 | (keyword == "TM" || keyword == "SE")) |
| 186 | { |
| 187 | edit.expandLocationCode("mts"); |
| 188 | } |
| 189 | } |
| 190 | |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 191 | return; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 192 | } |
| 193 | catch (const std::exception& e) |
| 194 | { |
| 195 | std::cerr << e.what() << std::endl; |
| 196 | } |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 197 | } |
| 198 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 199 | ListOfPaths |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 200 | Manager::getFRUsByUnexpandedLocationCode(const LocationCode locationCode, |
| 201 | const NodeNumber nodeNumber) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 202 | { |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 203 | ReaderImpl read; |
| 204 | return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 205 | } |
| 206 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 207 | ListOfPaths |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 208 | Manager::getFRUsByExpandedLocationCode(const LocationCode locationCode) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 209 | { |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 210 | ReaderImpl read; |
| 211 | return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 212 | } |
| 213 | |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 214 | LocationCode Manager::getExpandedLocationCode(const LocationCode locationCode, |
| 215 | const NodeNumber nodeNumber) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 216 | { |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 217 | ReaderImpl read; |
| 218 | return read.getExpandedLocationCode(locationCode, nodeNumber, |
| 219 | fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 220 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 221 | |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 222 | void Manager::performVPDRecollection() |
| 223 | { |
| 224 | // get list of FRUs replaceable at standby |
| 225 | for (const auto& item : replaceableFrus) |
| 226 | { |
| 227 | const vector<nlohmann::json>& groupEEPROM = jsonFile["frus"][item]; |
| 228 | const nlohmann::json& singleFru = groupEEPROM[0]; |
| 229 | |
| 230 | const string& inventoryPath = |
| 231 | singleFru["inventoryPath"] |
| 232 | .get_ref<const nlohmann::json::string_t&>(); |
| 233 | |
| 234 | if ((singleFru.find("devAddress") == singleFru.end()) || |
| 235 | (singleFru.find("driverType") == singleFru.end()) || |
| 236 | (singleFru.find("busType") == singleFru.end())) |
| 237 | { |
| 238 | // The FRUs is marked for replacement but missing mandatory |
| 239 | // fields for recollection. Skip to another replaceable fru. |
| 240 | log<level::ERR>( |
| 241 | "Recollection Failed as mandatory field missing in Json", |
| 242 | entry("ERROR=%s", |
| 243 | ("Recollection failed for " + inventoryPath).c_str())); |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | string str = "echo "; |
| 248 | string deviceAddress = singleFru["devAddress"]; |
| 249 | const string& driverType = singleFru["driverType"]; |
| 250 | const string& busType = singleFru["busType"]; |
| 251 | |
| 252 | // devTreeStatus flag is present in json as false to mention |
| 253 | // that the EEPROM is not mentioned in device tree. If this flag |
| 254 | // is absent consider the value to be true, i.e EEPROM is |
| 255 | // mentioned in device tree |
| 256 | if (!singleFru.value("devTreeStatus", true)) |
| 257 | { |
| 258 | auto pos = deviceAddress.find('-'); |
| 259 | if (pos != string::npos) |
| 260 | { |
| 261 | string busNum = deviceAddress.substr(0, pos); |
| 262 | deviceAddress = |
| 263 | "0x" + deviceAddress.substr(pos + 1, string::npos); |
| 264 | |
| 265 | string deleteDevice = str + deviceAddress + " > /sys/bus/" + |
| 266 | busType + "/devices/" + busType + "-" + |
| 267 | busNum + "/delete_device"; |
| 268 | executeCmd(deleteDevice); |
| 269 | |
| 270 | string addDevice = str + driverType + " " + deviceAddress + |
| 271 | " > /sys/bus/" + busType + "/devices/" + |
| 272 | busType + "-" + busNum + "/new_device"; |
| 273 | executeCmd(addDevice); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | log<level::ERR>( |
| 278 | "Wrong format of device address in Json", |
| 279 | entry( |
| 280 | "ERROR=%s", |
| 281 | ("Recollection failed for " + inventoryPath).c_str())); |
| 282 | continue; |
| 283 | } |
| 284 | } |
| 285 | else |
| 286 | { |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 287 | executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType, |
| 288 | driverType, "/unbind")); |
| 289 | executeCmd(createBindUnbindDriverCmnd(deviceAddress, busType, |
| 290 | driverType, "/bind")); |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 295 | } // namespace manager |
| 296 | } // namespace vpd |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 297 | } // namespace openpower |