Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 1 | #include "host_pdr_handler.hpp" |
| 2 | |
Andrew Jeffery | 4668f5c | 2024-01-15 14:59:17 +1030 | [diff] [blame] | 3 | #include <libpldm/fru.h> |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 4 | #ifdef OEM_IBM |
Andrew Jeffery | 21f128d | 2024-01-15 15:34:26 +1030 | [diff] [blame] | 5 | #include <libpldm/oem/ibm/fru.h> |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 6 | #endif |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 7 | #include "custom_dbus.hpp" |
| 8 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 11 | #include <nlohmann/json.hpp> |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 13 | #include <sdeventplus/clock.hpp> |
| 14 | #include <sdeventplus/exception.hpp> |
| 15 | #include <sdeventplus/source/io.hpp> |
| 16 | #include <sdeventplus/source/time.hpp> |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 17 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 18 | #include <fstream> |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 19 | #include <type_traits> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 20 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 21 | PHOSPHOR_LOG2_USING; |
| 22 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 23 | namespace pldm |
| 24 | { |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 25 | using namespace pldm::responder::events; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 26 | using namespace pldm::utils; |
| 27 | using namespace sdbusplus::bus::match::rules; |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 28 | using namespace pldm::responder::pdr_utils; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 29 | using Json = nlohmann::json; |
| 30 | namespace fs = std::filesystem; |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 31 | using namespace pldm::dbus; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 32 | constexpr auto fruJson = "host_frus.json"; |
| 33 | const Json emptyJson{}; |
| 34 | const std::vector<Json> emptyJsonList{}; |
| 35 | |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 36 | template <typename T> |
| 37 | uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr) |
| 38 | { |
| 39 | T* var = nullptr; |
| 40 | if (std::is_same<T, pldm_pdr_fru_record_set>::value) |
| 41 | { |
| 42 | var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr)); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | var = (T*)(pdr.data()); |
| 47 | } |
| 48 | if (var != nullptr) |
| 49 | { |
| 50 | return var->terminus_handle; |
| 51 | } |
| 52 | return TERMINUS_HANDLE; |
| 53 | } |
| 54 | |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 55 | template <typename T> |
| 56 | void updateContainerId(pldm_entity_association_tree* entityTree, |
| 57 | std::vector<uint8_t>& pdr) |
| 58 | { |
| 59 | T* t = nullptr; |
| 60 | if (entityTree == nullptr) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | if (std::is_same<T, pldm_pdr_fru_record_set>::value) |
| 65 | { |
| 66 | t = (T*)(pdr.data() + sizeof(pldm_pdr_hdr)); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | t = (T*)(pdr.data()); |
| 71 | } |
| 72 | if (t == nullptr) |
| 73 | { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | pldm_entity entity{t->entity_type, t->entity_instance, t->container_id}; |
| 78 | auto node = pldm_entity_association_tree_find_with_locality(entityTree, |
| 79 | &entity, true); |
| 80 | if (node) |
| 81 | { |
| 82 | pldm_entity e = pldm_entity_extract(node); |
| 83 | t->container_id = e.entity_container_id; |
| 84 | } |
| 85 | } |
| 86 | |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 87 | HostPDRHandler::HostPDRHandler( |
| 88 | int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, pldm_pdr* repo, |
| 89 | const std::string& eventsJsonsDir, pldm_entity_association_tree* entityTree, |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 90 | pldm_entity_association_tree* bmcEntityTree, |
| 91 | pldm::InstanceIdDb& instanceIdDb, |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 92 | pldm::requester::Handler<pldm::requester::Request>* handler, |
| 93 | pldm::responder::oem_platform::Handler* oemPlatformHandler) : |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 94 | mctp_fd(mctp_fd), |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 95 | mctp_eid(mctp_eid), event(event), repo(repo), |
| 96 | stateSensorHandler(eventsJsonsDir), entityTree(entityTree), |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 97 | bmcEntityTree(bmcEntityTree), instanceIdDb(instanceIdDb), handler(handler), |
| 98 | oemPlatformHandler(oemPlatformHandler) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 99 | { |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 100 | mergedHostParents = false; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 101 | fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson); |
| 102 | if (fs::exists(hostFruJson)) |
| 103 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 104 | // Note parent entities for entities sent down by the host firmware. |
| 105 | // This will enable a merge of entity associations. |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 106 | try |
| 107 | { |
| 108 | std::ifstream jsonFile(hostFruJson); |
| 109 | auto data = Json::parse(jsonFile, nullptr, false); |
| 110 | if (data.is_discarded()) |
| 111 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 112 | error("Parsing Host FRU json file failed"); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 113 | } |
| 114 | else |
| 115 | { |
| 116 | auto entities = data.value("entities", emptyJsonList); |
| 117 | for (auto& entity : entities) |
| 118 | { |
| 119 | EntityType entityType = entity.value("entity_type", 0); |
| 120 | auto parent = entity.value("parent", emptyJson); |
| 121 | pldm_entity p{}; |
| 122 | p.entity_type = parent.value("entity_type", 0); |
| 123 | p.entity_instance_num = parent.value("entity_instance", 0); |
| 124 | parents.emplace(entityType, std::move(p)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | catch (const std::exception& e) |
| 129 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 130 | error("Parsing Host FRU json file failed, exception = {ERR_EXCEP}", |
| 131 | "ERR_EXCEP", e.what()); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 134 | |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 135 | hostOffMatch = std::make_unique<sdbusplus::bus::match_t>( |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 136 | pldm::utils::DBusHandler::getBus(), |
| 137 | propertiesChanged("/xyz/openbmc_project/state/host0", |
| 138 | "xyz.openbmc_project.State.Host"), |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 139 | [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 140 | DbusChangedProps props{}; |
| 141 | std::string intf; |
| 142 | msg.read(intf, props); |
| 143 | const auto itr = props.find("CurrentHostState"); |
| 144 | if (itr != props.end()) |
| 145 | { |
| 146 | PropertyValue value = itr->second; |
| 147 | auto propVal = std::get<std::string>(value); |
| 148 | if (propVal == "xyz.openbmc_project.State.Host.HostState.Off") |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 149 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 150 | // Delete all the remote terminus information |
| 151 | std::erase_if(tlPDRInfo, [](const auto& item) { |
| 152 | auto const& [key, value] = item; |
| 153 | return key != TERMINUS_HANDLE; |
| 154 | }); |
| 155 | pldm_pdr_remove_remote_pdrs(repo); |
| 156 | pldm_entity_association_tree_destroy_root(entityTree); |
| 157 | pldm_entity_association_tree_copy_root(bmcEntityTree, |
| 158 | entityTree); |
| 159 | this->sensorMap.clear(); |
| 160 | this->responseReceived = false; |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 161 | this->mergedHostParents = false; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 162 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 163 | } |
Patrick Williams | a675662 | 2023-10-20 11:19:15 -0500 | [diff] [blame] | 164 | }); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 167 | void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles) |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 168 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 169 | pdrRecordHandles.clear(); |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 170 | modifiedPDRRecordHandles.clear(); |
| 171 | |
| 172 | if (isHostPdrModified) |
| 173 | { |
| 174 | modifiedPDRRecordHandles = std::move(recordHandles); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | pdrRecordHandles = std::move(recordHandles); |
| 179 | } |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 180 | |
| 181 | // Defer the actual fetch of PDRs from the host (by queuing the call on the |
| 182 | // main event loop). That way, we can respond to the platform event msg from |
| 183 | // the host firmware. |
| 184 | pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>( |
| 185 | event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this, |
| 186 | std::placeholders::_1)); |
| 187 | } |
| 188 | |
| 189 | void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/) |
| 190 | { |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 191 | getHostPDR(); |
| 192 | } |
| 193 | |
| 194 | void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle) |
| 195 | { |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 196 | pdrFetchEvent.reset(); |
| 197 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 198 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 199 | PLDM_GET_PDR_REQ_BYTES); |
| 200 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 201 | uint32_t recordHandle{}; |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 202 | if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) && |
| 203 | isHostPdrModified) |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 204 | { |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 205 | recordHandle = modifiedPDRRecordHandles.front(); |
| 206 | modifiedPDRRecordHandles.pop_front(); |
| 207 | } |
| 208 | else if (!nextRecordHandle && (!pdrRecordHandles.empty())) |
| 209 | { |
| 210 | recordHandle = pdrRecordHandles.front(); |
| 211 | pdrRecordHandles.pop_front(); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 212 | } |
| 213 | else |
| 214 | { |
| 215 | recordHandle = nextRecordHandle; |
| 216 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 217 | auto instanceId = instanceIdDb.next(mctp_eid); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 218 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 219 | auto rc = encode_get_pdr_req(instanceId, recordHandle, 0, |
| 220 | PLDM_GET_FIRSTPART, UINT16_MAX, 0, request, |
| 221 | PLDM_GET_PDR_REQ_BYTES); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 222 | if (rc != PLDM_SUCCESS) |
| 223 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 224 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 225 | error("Failed to encode_get_pdr_req, rc = {RC}", "RC", rc); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 226 | return; |
| 227 | } |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 228 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 229 | rc = handler->registerRequest( |
| 230 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR, |
| 231 | std::move(requestMsg), |
| 232 | std::move(std::bind_front(&HostPDRHandler::processHostPDRs, this))); |
| 233 | if (rc) |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 234 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 235 | error("Failed to send the GetPDR request to Host"); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 236 | } |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 239 | int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry, |
| 240 | pdr::EventState state) |
| 241 | { |
| 242 | auto rc = stateSensorHandler.eventAction(entry, state); |
| 243 | if (rc != PLDM_SUCCESS) |
| 244 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 245 | error("Failed to fetch and update D-bus property, rc = {RC}", "RC", rc); |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 246 | return rc; |
| 247 | } |
| 248 | return PLDM_SUCCESS; |
| 249 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 250 | |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 251 | void HostPDRHandler::mergeEntityAssociations( |
| 252 | const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size, |
| 253 | [[maybe_unused]] const uint32_t& record_handle) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 254 | { |
| 255 | size_t numEntities{}; |
| 256 | pldm_entity* entities = nullptr; |
| 257 | bool merged = false; |
| 258 | auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>( |
| 259 | const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr)); |
| 260 | |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 261 | if (oemPlatformHandler && |
| 262 | oemPlatformHandler->checkRecordHandleInRange(record_handle)) |
| 263 | { |
| 264 | // Adding the remote range PDRs to the repo before merging it |
| 265 | uint32_t handle = record_handle; |
| 266 | pldm_pdr_add_check(repo, pdr.data(), size, true, 0xFFFF, &handle); |
| 267 | } |
| 268 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 269 | pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities, |
| 270 | &entities); |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 271 | if (numEntities > 0) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 272 | { |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 273 | pldm_entity_node* pNode = nullptr; |
| 274 | if (!mergedHostParents) |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 275 | { |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 276 | pNode = pldm_entity_association_tree_find_with_locality( |
| 277 | entityTree, &entities[0], false); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 278 | } |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 279 | else |
| 280 | { |
| 281 | pNode = pldm_entity_association_tree_find_with_locality( |
| 282 | entityTree, &entities[0], true); |
| 283 | } |
| 284 | if (!pNode) |
| 285 | { |
| 286 | return; |
| 287 | } |
| 288 | |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 289 | Entities entityAssoc; |
| 290 | entityAssoc.push_back(pNode); |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 291 | for (size_t i = 1; i < numEntities; ++i) |
| 292 | { |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 293 | bool isUpdateContainerId = true; |
| 294 | if (oemPlatformHandler) |
| 295 | { |
| 296 | isUpdateContainerId = |
Sagar Srinivas | 5db6e87 | 2023-12-01 10:03:30 -0600 | [diff] [blame] | 297 | checkIfLogicalBitSet(entities[i].entity_container_id); |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 298 | } |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 299 | auto node = pldm_entity_association_tree_add_entity( |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 300 | entityTree, &entities[i], entities[i].entity_instance_num, |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 301 | pNode, entityPdr->association_type, true, isUpdateContainerId, |
| 302 | 0xFFFF); |
| 303 | if (!node) |
| 304 | { |
| 305 | continue; |
| 306 | } |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 307 | merged = true; |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 308 | entityAssoc.push_back(node); |
George Liu | acf2c8c | 2021-05-10 14:08:52 +0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | mergedHostParents = true; |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 312 | if (merged) |
| 313 | { |
| 314 | entityAssociations.push_back(entityAssoc); |
| 315 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 316 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 317 | |
| 318 | if (merged) |
| 319 | { |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 320 | // Update our PDR repo with the merged entity association PDRs |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 321 | pldm_entity_node* node = nullptr; |
| 322 | pldm_find_entity_ref_in_tree(entityTree, entities[0], &node); |
| 323 | if (node == nullptr) |
| 324 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 325 | error("could not find referrence of the entity in the tree"); |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 329 | int rc = 0; |
| 330 | if (oemPlatformHandler) |
| 331 | { |
| 332 | auto record = oemPlatformHandler->fetchLastBMCRecord(repo); |
| 333 | |
| 334 | uint32_t record_handle = pldm_pdr_get_record_handle(repo, |
| 335 | record); |
| 336 | |
| 337 | rc = |
| 338 | pldm_entity_association_pdr_add_from_node_with_record_handle( |
| 339 | node, repo, &entities, numEntities, true, |
| 340 | TERMINUS_HANDLE, (record_handle + 1)); |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | rc = pldm_entity_association_pdr_add_from_node_check( |
| 345 | node, repo, &entities, numEntities, true, TERMINUS_HANDLE); |
| 346 | } |
| 347 | |
Andrew Jeffery | 1fd3c51 | 2023-07-03 13:02:03 +0930 | [diff] [blame] | 348 | if (rc) |
| 349 | { |
| 350 | error( |
| 351 | "Failed to add entity association PDR from node: {LIBPLDM_ERROR}", |
| 352 | "LIBPLDM_ERROR", rc); |
| 353 | } |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 354 | } |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 355 | } |
Sampa Misra | 719ed39 | 2021-06-04 05:15:13 -0500 | [diff] [blame] | 356 | free(entities); |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 357 | } |
| 358 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 359 | void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, |
| 360 | uint8_t eventDataFormat) |
| 361 | { |
| 362 | assert(eventDataFormat == FORMAT_IS_PDR_HANDLES); |
| 363 | |
| 364 | // Extract from the PDR repo record handles of PDRs we want the host |
| 365 | // to pull up. |
| 366 | std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED}; |
| 367 | std::vector<uint8_t> numsOfChangeEntries(1); |
| 368 | std::vector<std::vector<ChangeEntry>> changeEntries( |
| 369 | numsOfChangeEntries.size()); |
| 370 | for (auto pdrType : pdrTypes) |
| 371 | { |
| 372 | const pldm_pdr_record* record{}; |
| 373 | do |
| 374 | { |
| 375 | record = pldm_pdr_find_record_by_type(repo, pdrType, record, |
| 376 | nullptr, nullptr); |
| 377 | if (record && pldm_pdr_record_is_remote(record)) |
| 378 | { |
| 379 | changeEntries[0].push_back( |
| 380 | pldm_pdr_get_record_handle(repo, record)); |
| 381 | } |
| 382 | } while (record); |
| 383 | } |
| 384 | if (changeEntries.empty()) |
| 385 | { |
| 386 | return; |
| 387 | } |
| 388 | numsOfChangeEntries[0] = changeEntries[0].size(); |
| 389 | |
| 390 | // Encode PLDM platform event msg to indicate a PDR repo change. |
| 391 | size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH + |
| 392 | PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH + |
| 393 | changeEntries[0].size() * sizeof(uint32_t); |
| 394 | std::vector<uint8_t> eventDataVec{}; |
| 395 | eventDataVec.resize(maxSize); |
| 396 | auto eventData = |
| 397 | reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>( |
| 398 | eventDataVec.data()); |
| 399 | size_t actualSize{}; |
| 400 | auto firstEntry = changeEntries[0].data(); |
| 401 | auto rc = encode_pldm_pdr_repository_chg_event_data( |
| 402 | eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(), |
| 403 | &firstEntry, eventData, &actualSize, maxSize); |
| 404 | if (rc != PLDM_SUCCESS) |
| 405 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 406 | error("Failed to encode_pldm_pdr_repository_chg_event_data, rc = {RC}", |
| 407 | "RC", rc); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 408 | return; |
| 409 | } |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 410 | auto instanceId = instanceIdDb.next(mctp_eid); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 411 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 412 | PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + |
| 413 | actualSize); |
| 414 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 415 | rc = encode_platform_event_message_req( |
ArchanaKakani | 6c39c7a | 2022-12-05 04:36:35 -0600 | [diff] [blame] | 416 | instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT, |
| 417 | eventDataVec.data(), actualSize, request, |
Christian Geddes | 3bdb3c2 | 2020-05-01 14:55:39 -0500 | [diff] [blame] | 418 | actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 419 | if (rc != PLDM_SUCCESS) |
| 420 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 421 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 422 | error("Failed to encode_platform_event_message_req, rc = {RC}", "RC", |
| 423 | rc); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 427 | auto platformEventMessageResponseHandler = |
| 428 | [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 429 | if (response == nullptr || !respMsgLen) |
| 430 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 431 | error( |
| 432 | "Failed to receive response for the PDR repository changed event"); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
| 436 | uint8_t completionCode{}; |
| 437 | uint8_t status{}; |
| 438 | auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response); |
Pavithra Barithaya | 54b5a56 | 2021-09-27 06:07:10 -0500 | [diff] [blame] | 439 | auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen, |
| 440 | &completionCode, &status); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 441 | if (rc || completionCode) |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 442 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 443 | error( |
| 444 | "Failed to decode_platform_event_message_resp: {RC}, cc = {CC}", |
| 445 | "RC", rc, "CC", static_cast<unsigned>(completionCode)); |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 446 | } |
| 447 | }; |
| 448 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 449 | rc = handler->registerRequest( |
Sampa Misra | 626c565 | 2021-08-11 10:28:48 -0500 | [diff] [blame] | 450 | mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE, |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 451 | std::move(requestMsg), std::move(platformEventMessageResponseHandler)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 452 | if (rc) |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 453 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 454 | error("Failed to send the PDR repository changed event request"); |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 458 | void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs) |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 459 | { |
| 460 | for (const auto& pdr : stateSensorPDRs) |
| 461 | { |
| 462 | SensorEntry sensorEntry{}; |
| 463 | const auto& [terminusHandle, sensorID, sensorInfo] = |
| 464 | responder::pdr_utils::parseStateSensorPDR(pdr); |
| 465 | sensorEntry.sensorID = sensorID; |
| 466 | try |
| 467 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 468 | sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle)); |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 469 | } |
| 470 | // If there is no mapping for terminusHandle assign the reserved TID |
| 471 | // value of 0xFF to indicate that. |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 472 | catch (const std::out_of_range&) |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 473 | { |
| 474 | sensorEntry.terminusID = PLDM_TID_RESERVED; |
| 475 | } |
| 476 | sensorMap.emplace(sensorEntry, std::move(sensorInfo)); |
| 477 | } |
| 478 | } |
| 479 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 480 | void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/, |
| 481 | const pldm_msg* response, |
| 482 | size_t respMsgLen) |
| 483 | { |
| 484 | static bool merged = false; |
| 485 | static PDRList stateSensorPDRs{}; |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 486 | static PDRList fruRecordSetPDRs{}; |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 487 | uint32_t nextRecordHandle{}; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 488 | uint8_t tlEid = 0; |
| 489 | bool tlValid = true; |
| 490 | uint32_t rh = 0; |
| 491 | uint16_t terminusHandle = 0; |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 492 | uint16_t pdrTerminusHandle = 0; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 493 | uint8_t tid = 0; |
| 494 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 495 | uint8_t completionCode{}; |
| 496 | uint32_t nextDataTransferHandle{}; |
| 497 | uint8_t transferFlag{}; |
| 498 | uint16_t respCount{}; |
| 499 | uint8_t transferCRC{}; |
| 500 | if (response == nullptr || !respMsgLen) |
| 501 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 502 | error("Failed to receive response for the GetPDR command"); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
| 506 | auto rc = decode_get_pdr_resp( |
| 507 | response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode, |
| 508 | &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount, |
| 509 | nullptr, 0, &transferCRC); |
| 510 | std::vector<uint8_t> responsePDRMsg; |
| 511 | responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr)); |
| 512 | memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 513 | if (rc != PLDM_SUCCESS) |
| 514 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 515 | error("Failed to decode_get_pdr_resp, rc = {RC}", "RC", rc); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | std::vector<uint8_t> pdr(respCount, 0); |
| 521 | rc = decode_get_pdr_resp(response, respMsgLen, &completionCode, |
| 522 | &nextRecordHandle, &nextDataTransferHandle, |
| 523 | &transferFlag, &respCount, pdr.data(), |
| 524 | respCount, &transferCRC); |
| 525 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 526 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 527 | error("Failed to decode_get_pdr_resp: rc = {RC}, cc = {CC}", "RC", |
| 528 | rc, "CC", static_cast<unsigned>(completionCode)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | else |
| 532 | { |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 533 | // when nextRecordHandle is 0, we need the recordHandle of the last |
| 534 | // PDR and not 0-1. |
| 535 | if (!nextRecordHandle) |
| 536 | { |
| 537 | rh = nextRecordHandle; |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | rh = nextRecordHandle - 1; |
| 542 | } |
| 543 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 544 | auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data()); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 545 | if (!rh) |
| 546 | { |
| 547 | rh = pdrHdr->record_handle; |
| 548 | } |
| 549 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 550 | if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION) |
| 551 | { |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 552 | this->mergeEntityAssociations(pdr, respCount, rh); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 553 | merged = true; |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR) |
| 558 | { |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 559 | pdrTerminusHandle = |
| 560 | extractTerminusHandle<pldm_terminus_locator_pdr>(pdr); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 561 | auto tlpdr = |
| 562 | reinterpret_cast<const pldm_terminus_locator_pdr*>( |
| 563 | pdr.data()); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 564 | |
| 565 | terminusHandle = tlpdr->terminus_handle; |
| 566 | tid = tlpdr->tid; |
| 567 | auto terminus_locator_type = tlpdr->terminus_locator_type; |
| 568 | if (terminus_locator_type == |
| 569 | PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID) |
| 570 | { |
| 571 | auto locatorValue = reinterpret_cast< |
| 572 | const pldm_terminus_locator_type_mctp_eid*>( |
| 573 | tlpdr->terminus_locator_value); |
| 574 | tlEid = static_cast<uint8_t>(locatorValue->eid); |
| 575 | } |
| 576 | if (tlpdr->validity == 0) |
| 577 | { |
| 578 | tlValid = false; |
| 579 | } |
Pavithra Barithaya | 52aad39 | 2022-08-02 04:18:52 -0500 | [diff] [blame] | 580 | for (const auto& terminusMap : tlPDRInfo) |
| 581 | { |
| 582 | if ((terminusHandle == (terminusMap.first)) && |
| 583 | (get<1>(terminusMap.second) == tlEid) && |
| 584 | (get<2>(terminusMap.second) == tlpdr->validity)) |
| 585 | { |
| 586 | // TL PDR already present with same validity don't |
| 587 | // add the PDR to the repo just return |
| 588 | return; |
| 589 | } |
| 590 | } |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 591 | tlPDRInfo.insert_or_assign( |
| 592 | tlpdr->terminus_handle, |
| 593 | std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity)); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 594 | } |
| 595 | else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR) |
| 596 | { |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 597 | pdrTerminusHandle = |
| 598 | extractTerminusHandle<pldm_state_sensor_pdr>(pdr); |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 599 | updateContainerId<pldm_state_sensor_pdr>(entityTree, pdr); |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 600 | stateSensorPDRs.emplace_back(pdr); |
| 601 | } |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 602 | else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET) |
| 603 | { |
| 604 | pdrTerminusHandle = |
| 605 | extractTerminusHandle<pldm_pdr_fru_record_set>(pdr); |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 606 | updateContainerId<pldm_pdr_fru_record_set>(entityTree, pdr); |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 607 | fruRecordSetPDRs.emplace_back(pdr); |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 608 | } |
| 609 | else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR) |
| 610 | { |
| 611 | pdrTerminusHandle = |
| 612 | extractTerminusHandle<pldm_state_effecter_pdr>(pdr); |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 613 | updateContainerId<pldm_state_effecter_pdr>(entityTree, pdr); |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 614 | } |
| 615 | else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR) |
| 616 | { |
| 617 | pdrTerminusHandle = |
| 618 | extractTerminusHandle<pldm_numeric_effecter_value_pdr>( |
| 619 | pdr); |
George Liu | 96af8cb | 2021-07-31 15:23:45 +0800 | [diff] [blame] | 620 | updateContainerId<pldm_numeric_effecter_value_pdr>( |
| 621 | entityTree, pdr); |
Manojkiran Eda | 3ca4045 | 2021-10-04 22:51:37 +0530 | [diff] [blame] | 622 | } |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 623 | // if the TLPDR is invalid update the repo accordingly |
| 624 | if (!tlValid) |
| 625 | { |
| 626 | pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid, |
| 627 | tlValid); |
Sagar Srinivas | 3687e2b | 2023-04-10 05:08:28 -0500 | [diff] [blame] | 628 | |
| 629 | if (!isHostUp()) |
| 630 | { |
| 631 | // The terminus PDR becomes invalid when the terminus |
| 632 | // itself is down. We don't need to do PDR exchange in |
| 633 | // that case, so setting the next record handle to 0. |
| 634 | nextRecordHandle = 0; |
| 635 | } |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 636 | } |
| 637 | else |
| 638 | { |
Andrew Jeffery | 64f37fe | 2023-07-03 15:41:13 +0930 | [diff] [blame] | 639 | rc = pldm_pdr_add_check(repo, pdr.data(), respCount, true, |
| 640 | pdrTerminusHandle, &rh); |
| 641 | if (rc) |
| 642 | { |
| 643 | // pldm_pdr_add() assert()ed on failure to add a PDR. |
| 644 | throw std::runtime_error("Failed to add PDR"); |
| 645 | } |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 646 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | } |
| 650 | if (!nextRecordHandle) |
| 651 | { |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 652 | updateEntityAssociation(entityAssociations, entityTree, objPathMap); |
| 653 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 654 | /*received last record*/ |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 655 | this->parseStateSensorPDRs(stateSensorPDRs); |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 656 | this->createDbusObjects(fruRecordSetPDRs); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 657 | if (isHostUp()) |
| 658 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 659 | this->setHostSensorState(stateSensorPDRs); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 660 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 661 | stateSensorPDRs.clear(); |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 662 | fruRecordSetPDRs.clear(); |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 663 | entityAssociations.clear(); |
| 664 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 665 | if (merged) |
| 666 | { |
| 667 | merged = false; |
| 668 | deferredPDRRepoChgEvent = |
| 669 | std::make_unique<sdeventplus::source::Defer>( |
| 670 | event, |
| 671 | std::bind( |
| 672 | std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)), |
| 673 | this, std::placeholders::_1)); |
| 674 | } |
| 675 | } |
| 676 | else |
| 677 | { |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 678 | if (modifiedPDRRecordHandles.empty() && isHostPdrModified) |
| 679 | { |
| 680 | isHostPdrModified = false; |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | deferredFetchPDREvent = |
| 685 | std::make_unique<sdeventplus::source::Defer>( |
| 686 | event, |
| 687 | std::bind( |
| 688 | std::mem_fn((&HostPDRHandler::_processFetchPDREvent)), |
| 689 | this, nextRecordHandle, std::placeholders::_1)); |
| 690 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| 694 | void HostPDRHandler::_processPDRRepoChgEvent( |
| 695 | sdeventplus::source::EventBase& /*source */) |
| 696 | { |
| 697 | deferredPDRRepoChgEvent.reset(); |
| 698 | this->sendPDRRepositoryChgEvent( |
| 699 | std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)), |
| 700 | FORMAT_IS_PDR_HANDLES); |
| 701 | } |
| 702 | |
| 703 | void HostPDRHandler::_processFetchPDREvent( |
| 704 | uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */) |
| 705 | { |
| 706 | deferredFetchPDREvent.reset(); |
| 707 | if (!this->pdrRecordHandles.empty()) |
| 708 | { |
| 709 | nextRecordHandle = this->pdrRecordHandles.front(); |
| 710 | this->pdrRecordHandles.pop_front(); |
| 711 | } |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 712 | if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty())) |
| 713 | { |
| 714 | nextRecordHandle = this->modifiedPDRRecordHandles.front(); |
| 715 | this->modifiedPDRRecordHandles.pop_front(); |
| 716 | } |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 717 | this->getHostPDR(nextRecordHandle); |
| 718 | } |
| 719 | |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 720 | void HostPDRHandler::setHostFirmwareCondition() |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 721 | { |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 722 | responseReceived = false; |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 723 | auto instanceId = instanceIdDb.next(mctp_eid); |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 724 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 725 | PLDM_GET_VERSION_REQ_BYTES); |
| 726 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 727 | auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART, |
| 728 | PLDM_BASE, request); |
| 729 | if (rc != PLDM_SUCCESS) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 730 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 731 | error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC", |
| 732 | lg2::hex, rc); |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 733 | instanceIdDb.free(mctp_eid, instanceId); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 737 | auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/, |
| 738 | const pldm_msg* response, |
| 739 | size_t respMsgLen) { |
| 740 | if (response == nullptr || !respMsgLen) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 741 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 742 | error( |
| 743 | "Failed to receive response for getPLDMVersion command, Host seems to be off"); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 744 | return; |
| 745 | } |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 746 | info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex, |
| 747 | static_cast<uint16_t>(response->payload[0])); |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 748 | this->responseReceived = true; |
| 749 | getHostPDR(); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 750 | }; |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 751 | rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE, |
| 752 | PLDM_GET_PLDM_VERSION, std::move(requestMsg), |
| 753 | std::move(getPLDMVersionHandler)); |
| 754 | if (rc) |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 755 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 756 | error("Failed to discover Host state. Assuming Host as off"); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | bool HostPDRHandler::isHostUp() |
| 761 | { |
| 762 | return responseReceived; |
| 763 | } |
| 764 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 765 | void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 766 | { |
| 767 | for (const auto& stateSensorPDR : stateSensorPDRs) |
| 768 | { |
| 769 | auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>( |
| 770 | stateSensorPDR.data()); |
| 771 | |
| 772 | if (!pdr) |
| 773 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 774 | error("Failed to get State sensor PDR"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 775 | pldm::utils::reportError( |
| 776 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | uint16_t sensorId = pdr->sensor_id; |
| 781 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 782 | for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 783 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 784 | if (terminusHandle == pdr->terminus_handle) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 785 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 786 | if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 787 | { |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 788 | mctp_eid = std::get<1>(terminusInfo); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | bitfield8_t sensorRearm; |
| 792 | sensorRearm.byte = 0; |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 793 | uint8_t tid = std::get<0>(terminusInfo); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 794 | |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 795 | auto instanceId = instanceIdDb.next(mctp_eid); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 796 | std::vector<uint8_t> requestMsg( |
| 797 | sizeof(pldm_msg_hdr) + |
| 798 | PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES); |
| 799 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 800 | auto rc = encode_get_state_sensor_readings_req( |
| 801 | instanceId, sensorId, sensorRearm, 0, request); |
| 802 | |
| 803 | if (rc != PLDM_SUCCESS) |
| 804 | { |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 805 | instanceIdDb.free(mctp_eid, instanceId); |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 806 | error( |
| 807 | "Failed to encode_get_state_sensor_readings_req, rc = {RC}", |
| 808 | "RC", rc); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 809 | pldm::utils::reportError( |
| 810 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 811 | return; |
| 812 | } |
| 813 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 814 | auto getStateSensorReadingRespHandler = |
| 815 | [=, this](mctp_eid_t /*eid*/, const pldm_msg* response, |
| 816 | size_t respMsgLen) { |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 817 | if (response == nullptr || !respMsgLen) |
| 818 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 819 | error( |
| 820 | "Failed to receive response for getStateSensorReading command"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 821 | return; |
| 822 | } |
| 823 | std::array<get_sensor_state_field, 8> stateField{}; |
| 824 | uint8_t completionCode = 0; |
| 825 | uint8_t comp_sensor_count = 0; |
| 826 | |
| 827 | auto rc = decode_get_state_sensor_readings_resp( |
| 828 | response, respMsgLen, &completionCode, |
| 829 | &comp_sensor_count, stateField.data()); |
| 830 | |
| 831 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 832 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 833 | error( |
| 834 | "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}", |
| 835 | "RC", rc, "CC", |
| 836 | static_cast<unsigned>(completionCode)); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 837 | pldm::utils::reportError( |
| 838 | "xyz.openbmc_project.bmc.pldm.InternalFailure"); |
| 839 | } |
| 840 | |
| 841 | uint8_t eventState; |
| 842 | uint8_t previousEventState; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 843 | |
RIYA DIXIT | bb5fda4 | 2022-09-27 06:48:08 -0500 | [diff] [blame] | 844 | for (uint8_t sensorOffset = 0; |
| 845 | sensorOffset < comp_sensor_count; sensorOffset++) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 846 | { |
RIYA DIXIT | bb5fda4 | 2022-09-27 06:48:08 -0500 | [diff] [blame] | 847 | eventState = stateField[sensorOffset].present_state; |
| 848 | previousEventState = |
| 849 | stateField[sensorOffset].previous_state; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 850 | |
| 851 | emitStateSensorEventSignal(tid, sensorId, sensorOffset, |
| 852 | eventState, |
| 853 | previousEventState); |
| 854 | |
| 855 | SensorEntry sensorEntry{tid, sensorId}; |
| 856 | |
| 857 | pldm::pdr::EntityInfo entityInfo{}; |
| 858 | pldm::pdr::CompositeSensorStates |
| 859 | compositeSensorStates{}; |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame^] | 860 | std::vector<pldm::pdr::StateSetId> stateSetIds{}; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 861 | |
| 862 | try |
| 863 | { |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame^] | 864 | std::tie(entityInfo, compositeSensorStates, |
| 865 | stateSetIds) = |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 866 | lookupSensorInfo(sensorEntry); |
| 867 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 868 | catch (const std::out_of_range&) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 869 | { |
| 870 | try |
| 871 | { |
| 872 | sensorEntry.terminusID = PLDM_TID_RESERVED; |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame^] | 873 | std::tie(entityInfo, compositeSensorStates, |
| 874 | stateSetIds) = |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 875 | lookupSensorInfo(sensorEntry); |
| 876 | } |
Kamalkumar Patel | 58cbcaf | 2023-10-06 03:48:25 -0500 | [diff] [blame] | 877 | catch (const std::out_of_range&) |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 878 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 879 | error("No mapping for the events"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | if (sensorOffset > compositeSensorStates.size()) |
| 884 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 885 | error("Error Invalid data, Invalid sensor offset"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 886 | return; |
| 887 | } |
| 888 | |
| 889 | const auto& possibleStates = |
| 890 | compositeSensorStates[sensorOffset]; |
| 891 | if (possibleStates.find(eventState) == |
| 892 | possibleStates.end()) |
| 893 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 894 | error("Error invalid_data, Invalid event state"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 895 | return; |
| 896 | } |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 897 | const auto& [containerId, entityType, |
| 898 | entityInstance] = entityInfo; |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame^] | 899 | auto stateSetId = stateSetIds[sensorOffset]; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 900 | pldm::responder::events::StateSensorEntry |
| 901 | stateSensorEntry{containerId, entityType, |
Sagar Srinivas | e3607a3 | 2024-02-16 03:50:53 -0600 | [diff] [blame^] | 902 | entityInstance, sensorOffset, |
| 903 | stateSetId}; |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 904 | handleStateSensorEvent(stateSensorEntry, eventState); |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | rc = handler->registerRequest( |
| 909 | mctp_eid, instanceId, PLDM_PLATFORM, |
| 910 | PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg), |
| 911 | std::move(getStateSensorReadingRespHandler)); |
| 912 | |
| 913 | if (rc != PLDM_SUCCESS) |
| 914 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 915 | error( |
| 916 | "Failed to send request to get State sensor reading on Host"); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 922 | |
| 923 | void HostPDRHandler::getFRURecordTableMetadataByRemote( |
| 924 | const PDRList& fruRecordSetPDRs) |
| 925 | { |
| 926 | auto instanceId = instanceIdDb.next(mctp_eid); |
| 927 | std::vector<uint8_t> requestMsg( |
| 928 | sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES); |
| 929 | |
| 930 | // GetFruRecordTableMetadata |
| 931 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 932 | auto rc = encode_get_fru_record_table_metadata_req( |
| 933 | instanceId, request, requestMsg.size() - sizeof(pldm_msg_hdr)); |
| 934 | if (rc != PLDM_SUCCESS) |
| 935 | { |
| 936 | instanceIdDb.free(mctp_eid, instanceId); |
| 937 | lg2::error( |
| 938 | "Failed to encode_get_fru_record_table_metadata_req, rc = {RC}", |
| 939 | "RC", lg2::hex, rc); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | auto getFruRecordTableMetadataResponseHandler = |
| 944 | [this, fruRecordSetPDRs](mctp_eid_t /*eid*/, const pldm_msg* response, |
| 945 | size_t respMsgLen) { |
| 946 | if (response == nullptr || !respMsgLen) |
| 947 | { |
| 948 | lg2::error( |
| 949 | "Failed to receive response for the Get FRU Record Table Metadata"); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | uint8_t cc = 0; |
| 954 | uint8_t fru_data_major_version, fru_data_minor_version; |
| 955 | uint32_t fru_table_maximum_size, fru_table_length; |
| 956 | uint16_t total_record_set_identifiers; |
| 957 | uint16_t total; |
| 958 | uint32_t checksum; |
| 959 | |
| 960 | auto rc = decode_get_fru_record_table_metadata_resp( |
| 961 | response, respMsgLen, &cc, &fru_data_major_version, |
| 962 | &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length, |
| 963 | &total_record_set_identifiers, &total, &checksum); |
| 964 | |
| 965 | if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS) |
| 966 | { |
| 967 | lg2::error( |
| 968 | "Faile to decode get fru record table metadata resp, Message Error: {RC}, cc: {CC}", |
| 969 | "RC", lg2::hex, rc, "CC", cc); |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | // pass total to getFRURecordTableByRemote |
| 974 | this->getFRURecordTableByRemote(fruRecordSetPDRs, total); |
| 975 | }; |
| 976 | |
| 977 | rc = handler->registerRequest( |
| 978 | mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE_METADATA, |
| 979 | std::move(requestMsg), |
| 980 | std::move(getFruRecordTableMetadataResponseHandler)); |
| 981 | if (rc != PLDM_SUCCESS) |
| 982 | { |
| 983 | lg2::error("Failed to send the the Set State Effecter States request"); |
| 984 | } |
| 985 | |
| 986 | return; |
| 987 | } |
| 988 | |
| 989 | void HostPDRHandler::getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs, |
| 990 | uint16_t totalTableRecords) |
| 991 | { |
| 992 | fruRecordData.clear(); |
| 993 | |
| 994 | if (!totalTableRecords) |
| 995 | { |
| 996 | lg2::error("Failed to get fru record table"); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | auto instanceId = instanceIdDb.next(mctp_eid); |
| 1001 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 1002 | PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES); |
| 1003 | |
| 1004 | // send the getFruRecordTable command |
| 1005 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1006 | auto rc = encode_get_fru_record_table_req( |
| 1007 | instanceId, 0, PLDM_GET_FIRSTPART, request, |
| 1008 | requestMsg.size() - sizeof(pldm_msg_hdr)); |
| 1009 | if (rc != PLDM_SUCCESS) |
| 1010 | { |
| 1011 | instanceIdDb.free(mctp_eid, instanceId); |
| 1012 | lg2::error("Failed to encode_get_fru_record_table_req, rc = {RC}", "RC", |
| 1013 | lg2::hex, rc); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | auto getFruRecordTableResponseHandler = |
| 1018 | [totalTableRecords, this, fruRecordSetPDRs]( |
| 1019 | mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) { |
| 1020 | if (response == nullptr || !respMsgLen) |
| 1021 | { |
| 1022 | lg2::error( |
| 1023 | "Failed to receive response for the Get FRU Record Table"); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | uint8_t cc = 0; |
| 1028 | uint32_t next_data_transfer_handle = 0; |
| 1029 | uint8_t transfer_flag = 0; |
| 1030 | size_t fru_record_table_length = 0; |
| 1031 | std::vector<uint8_t> fru_record_table_data(respMsgLen - |
| 1032 | sizeof(pldm_msg_hdr)); |
| 1033 | auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response); |
| 1034 | auto rc = decode_get_fru_record_table_resp( |
Manojkiran Eda | 33a9cd0 | 2024-01-22 11:06:59 +0530 | [diff] [blame] | 1035 | responsePtr, respMsgLen, &cc, &next_data_transfer_handle, |
| 1036 | &transfer_flag, fru_record_table_data.data(), |
| 1037 | &fru_record_table_length); |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 1038 | |
| 1039 | if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS) |
| 1040 | { |
| 1041 | lg2::error( |
| 1042 | "Failed to decode get fru record table resp, Message Error: {RC}, cc: {CC}", |
| 1043 | "RC", lg2::hex, rc, "CC", cc); |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | fruRecordData = responder::pdr_utils::parseFruRecordTable( |
| 1048 | fru_record_table_data.data(), fru_record_table_length); |
| 1049 | |
| 1050 | if (totalTableRecords != fruRecordData.size()) |
| 1051 | { |
| 1052 | fruRecordData.clear(); |
| 1053 | |
| 1054 | lg2::error("failed to parse fru recrod data format."); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | this->setFRUDataOnDBus(fruRecordSetPDRs, fruRecordData); |
| 1059 | }; |
| 1060 | |
| 1061 | rc = handler->registerRequest( |
| 1062 | mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE, |
| 1063 | std::move(requestMsg), std::move(getFruRecordTableResponseHandler)); |
| 1064 | if (rc != PLDM_SUCCESS) |
| 1065 | { |
| 1066 | lg2::error("Failed to send the the Set State Effecter States request"); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | std::optional<uint16_t> HostPDRHandler::getRSI(const PDRList& fruRecordSetPDRs, |
| 1071 | const pldm_entity& entity) |
| 1072 | { |
| 1073 | for (const auto& pdr : fruRecordSetPDRs) |
| 1074 | { |
| 1075 | auto fruPdr = reinterpret_cast<const pldm_pdr_fru_record_set*>( |
| 1076 | const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr)); |
| 1077 | |
| 1078 | if (fruPdr->entity_type == entity.entity_type && |
Sagar Srinivas | ebf8bb5 | 2023-10-06 01:15:55 -0500 | [diff] [blame] | 1079 | fruPdr->entity_instance == entity.entity_instance_num && |
| 1080 | fruPdr->container_id == entity.entity_container_id) |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 1081 | { |
| 1082 | return fruPdr->fru_rsi; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | return std::nullopt; |
| 1087 | } |
| 1088 | |
| 1089 | void HostPDRHandler::setFRUDataOnDBus( |
Patrick Williams | d310f82 | 2023-10-07 19:01:19 -0500 | [diff] [blame] | 1090 | [[maybe_unused]] const PDRList& fruRecordSetPDRs, |
| 1091 | [[maybe_unused]] const std::vector< |
| 1092 | responder::pdr_utils::FruRecordDataFormat>& fruRecordData) |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 1093 | { |
Patrick Williams | d310f82 | 2023-10-07 19:01:19 -0500 | [diff] [blame] | 1094 | #ifdef OEM_IBM |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 1095 | for (const auto& entity : objPathMap) |
| 1096 | { |
| 1097 | pldm_entity node = pldm_entity_extract(entity.second); |
| 1098 | auto fruRSI = getRSI(fruRecordSetPDRs, node); |
| 1099 | |
| 1100 | for (const auto& data : fruRecordData) |
| 1101 | { |
| 1102 | if (!fruRSI || *fruRSI != data.fruRSI) |
| 1103 | { |
| 1104 | continue; |
| 1105 | } |
| 1106 | |
| 1107 | if (data.fruRecType == PLDM_FRU_RECORD_TYPE_OEM) |
| 1108 | { |
| 1109 | for (const auto& tlv : data.fruTLV) |
| 1110 | { |
| 1111 | if (tlv.fruFieldType == |
| 1112 | PLDM_OEM_FRU_FIELD_TYPE_LOCATION_CODE) |
| 1113 | { |
| 1114 | CustomDBus::getCustomDBus().setLocationCode( |
| 1115 | entity.first, |
| 1116 | std::string(reinterpret_cast<const char*>( |
| 1117 | tlv.fruFieldValue.data()), |
| 1118 | tlv.fruFieldLen)); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
Patrick Williams | d310f82 | 2023-10-07 19:01:19 -0500 | [diff] [blame] | 1124 | #endif |
George Liu | 682ee18 | 2020-12-25 15:24:33 +0800 | [diff] [blame] | 1125 | } |
| 1126 | void HostPDRHandler::createDbusObjects(const PDRList& fruRecordSetPDRs) |
| 1127 | { |
| 1128 | // TODO: Creating and Refreshing dbus hosted by remote PLDM entity Fru PDRs |
| 1129 | |
| 1130 | getFRURecordTableMetadataByRemote(fruRecordSetPDRs); |
| 1131 | } |
| 1132 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 1133 | } // namespace pldm |