Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 1 | #include "pldm.hpp" |
| 2 | |
| 3 | #include "file.hpp" |
| 4 | |
| 5 | #include <fmt/core.h> |
| 6 | #include <libpldm/entity.h> |
| 7 | #include <libpldm/platform.h> |
| 8 | #include <libpldm/state_set.h> |
Eddie James | 36313c7 | 2021-11-18 08:27:03 -0600 | [diff] [blame] | 9 | #include <libpldm/state_set_oem_ibm.h> |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 10 | |
| 11 | #include <phosphor-logging/log.hpp> |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| 13 | #include <sdeventplus/clock.hpp> |
| 14 | #include <sdeventplus/exception.hpp> |
| 15 | #include <sdeventplus/source/io.hpp> |
| 16 | #include <sdeventplus/source/time.hpp> |
| 17 | |
| 18 | #include <algorithm> |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 19 | |
| 20 | namespace pldm |
| 21 | { |
| 22 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 23 | using namespace phosphor::logging; |
| 24 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 25 | using namespace sdeventplus; |
| 26 | using namespace sdeventplus::source; |
| 27 | constexpr auto clockId = sdeventplus::ClockId::RealTime; |
| 28 | using Clock = sdeventplus::Clock<clockId>; |
| 29 | using Timer = Time<clockId>; |
| 30 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 31 | void Interface::fetchSensorInfo(uint16_t stateSetId, |
| 32 | SensorToInstance& sensorInstanceMap, |
| 33 | SensorOffset& sensorOffset) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 34 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 35 | PdrList pdrs{}; |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 36 | static bool tracedError = false; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 37 | |
| 38 | auto& bus = open_power::occ::utils::getBus(); |
| 39 | try |
| 40 | { |
| 41 | auto method = bus.new_method_call( |
| 42 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 43 | "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR"); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 44 | method.append(tid, static_cast<uint16_t>(PLDM_ENTITY_PROC), stateSetId); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 45 | |
| 46 | auto responseMsg = bus.call(method); |
| 47 | responseMsg.read(pdrs); |
| 48 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 49 | catch (const sdbusplus::exception_t& e) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 50 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 51 | if (!tracedError) |
| 52 | { |
| 53 | log<level::ERR>( |
| 54 | fmt::format( |
| 55 | "fetchSensorInfo: Failed to find stateSetID:{} PDR: {}", |
| 56 | stateSetId, e.what()) |
| 57 | .c_str()); |
| 58 | tracedError = true; |
| 59 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if (pdrs.empty()) |
| 63 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 64 | if (!tracedError) |
| 65 | { |
| 66 | log<level::ERR>( |
| 67 | fmt::format( |
| 68 | "fetchSensorInfo: state sensor PDRs ({}) not present", |
| 69 | stateSetId) |
| 70 | .c_str()); |
| 71 | tracedError = true; |
| 72 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 76 | // Found PDR |
| 77 | if (tracedError) |
| 78 | { |
| 79 | log<level::INFO>( |
| 80 | fmt::format("fetchSensorInfo: found {} PDRs", pdrs.size()).c_str()); |
| 81 | tracedError = false; |
| 82 | } |
| 83 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 84 | bool offsetFound = false; |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 85 | auto stateSensorPDR = |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 86 | reinterpret_cast<const pldm_state_sensor_pdr*>(pdrs.front().data()); |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 87 | auto possibleStatesPtr = stateSensorPDR->possible_states; |
| 88 | for (auto offset = 0; offset < stateSensorPDR->composite_sensor_count; |
| 89 | offset++) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 90 | { |
| 91 | auto possibleStates = |
| 92 | reinterpret_cast<const state_sensor_possible_states*>( |
| 93 | possibleStatesPtr); |
| 94 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 95 | if (possibleStates->state_set_id == stateSetId) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 96 | { |
| 97 | sensorOffset = offset; |
| 98 | offsetFound = true; |
| 99 | break; |
| 100 | } |
| 101 | possibleStatesPtr += sizeof(possibleStates->state_set_id) + |
| 102 | sizeof(possibleStates->possible_states_size) + |
| 103 | possibleStates->possible_states_size; |
| 104 | } |
| 105 | |
| 106 | if (!offsetFound) |
| 107 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 108 | log<level::ERR>("pldm: state sensor PDR not found"); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | // To order SensorID based on the EntityInstance. |
| 113 | // Note that when a proc is on a DCM, the PDRs for these sensors |
| 114 | // could have the same instance IDs but different container IDs. |
| 115 | std::map<uint32_t, SensorID> entityInstMap{}; |
| 116 | for (auto& pdr : pdrs) |
| 117 | { |
| 118 | auto pdrPtr = |
| 119 | reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data()); |
Chris Cain | 72d01aa | 2022-06-14 16:28:03 -0500 | [diff] [blame] | 120 | uint32_t key = pdrPtr->sensor_id; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 121 | entityInstMap.emplace(key, static_cast<SensorID>(pdrPtr->sensor_id)); |
| 122 | } |
| 123 | |
| 124 | open_power::occ::instanceID count = start; |
| 125 | for (auto const& pair : entityInstMap) |
| 126 | { |
| 127 | sensorInstanceMap.emplace(pair.second, count); |
| 128 | count++; |
| 129 | } |
| 130 | } |
| 131 | |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 132 | void Interface::sensorEvent(sdbusplus::message_t& msg) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 133 | { |
| 134 | if (!isOCCSensorCacheValid()) |
| 135 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 136 | fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS, |
| 137 | sensorToOCCInstance, OCCSensorOffset); |
| 138 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 139 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 140 | if (sensorToSBEInstance.empty()) |
| 141 | { |
| 142 | fetchSensorInfo(PLDM_OEM_IBM_SBE_HRESET_STATE, sensorToSBEInstance, |
| 143 | SBESensorOffset); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Chris Cain | 72d01aa | 2022-06-14 16:28:03 -0500 | [diff] [blame] | 146 | TerminusID sensorTid{}; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 147 | SensorID sensorId{}; |
| 148 | SensorOffset msgSensorOffset{}; |
| 149 | EventState eventState{}; |
| 150 | EventState previousEventState{}; |
| 151 | |
Chris Cain | 72d01aa | 2022-06-14 16:28:03 -0500 | [diff] [blame] | 152 | msg.read(sensorTid, sensorId, msgSensorOffset, eventState, |
| 153 | previousEventState); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 154 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 155 | if (msgSensorOffset == OCCSensorOffset) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 156 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 157 | auto sensorEntry = sensorToOCCInstance.find(sensorId); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 158 | |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 159 | if (sensorEntry != sensorToOCCInstance.end()) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 160 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 161 | const uint8_t instance = sensorEntry->second; |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 162 | if (eventState == |
| 163 | static_cast<EventState>( |
| 164 | PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_IN_SERVICE)) |
| 165 | { |
| 166 | log<level::INFO>( |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 167 | fmt::format("PLDM: OCC{} is RUNNING", instance).c_str()); |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 168 | callBack(sensorEntry->second, true); |
| 169 | } |
| 170 | else if (eventState == |
| 171 | static_cast<EventState>( |
| 172 | PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPED)) |
| 173 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 174 | log<level::INFO>( |
| 175 | fmt::format("PLDM: OCC{} has now STOPPED", instance) |
| 176 | .c_str()); |
| 177 | callBack(instance, false); |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 178 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 179 | else if (eventState == |
| 180 | static_cast<EventState>( |
| 181 | PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_DORMANT)) |
| 182 | { |
| 183 | log<level::INFO>( |
| 184 | fmt::format( |
| 185 | "PLDM: OCC{} has now STOPPED and system is in SAFE MODE", |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 186 | instance) |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 187 | .c_str()); |
Sheldon Bailey | 31a2f13 | 2022-05-20 11:31:52 -0500 | [diff] [blame] | 188 | |
| 189 | // Setting safe mode true |
| 190 | safeModeCallBack(true); |
| 191 | |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 192 | callBack(instance, false); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 193 | } |
| 194 | else |
| 195 | { |
| 196 | log<level::INFO>( |
| 197 | fmt::format("PLDM: Unexpected PLDM state {} for OCC{}", |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 198 | eventState, instance) |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 199 | .c_str()); |
| 200 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 201 | return; |
| 202 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 203 | } |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 204 | |
| 205 | if (msgSensorOffset == SBESensorOffset) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 206 | { |
| 207 | auto sensorEntry = sensorToSBEInstance.find(sensorId); |
| 208 | |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 209 | if (sensorEntry != sensorToSBEInstance.end()) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 210 | { |
Chris Cain | 12d0b82 | 2022-04-22 17:29:18 -0500 | [diff] [blame] | 211 | const uint8_t instance = sensorEntry->second; |
| 212 | auto match = std::find(outstandingHResets.begin(), |
| 213 | outstandingHResets.end(), instance); |
| 214 | if (match != outstandingHResets.end()) |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 215 | { |
Chris Cain | 12d0b82 | 2022-04-22 17:29:18 -0500 | [diff] [blame] | 216 | outstandingHResets.erase(match); |
| 217 | if (eventState == static_cast<EventState>(SBE_HRESET_NOT_READY)) |
| 218 | { |
| 219 | log<level::INFO>( |
| 220 | fmt::format("pldm: HRESET is NOT READY (OCC{})", |
| 221 | instance) |
| 222 | .c_str()); |
| 223 | } |
| 224 | else if (eventState == |
| 225 | static_cast<EventState>(SBE_HRESET_READY)) |
| 226 | { |
| 227 | sbeCallBack(instance, true); |
| 228 | } |
| 229 | else if (eventState == |
| 230 | static_cast<EventState>(SBE_HRESET_FAILED)) |
| 231 | { |
| 232 | sbeCallBack(instance, false); |
| 233 | } |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 234 | } |
Chris Cain | 12d0b82 | 2022-04-22 17:29:18 -0500 | [diff] [blame] | 235 | // else request was not from us |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 236 | } |
| 237 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 240 | void Interface::hostStateEvent(sdbusplus::message_t& msg) |
Chris Cain | 157467d | 2022-06-24 11:25:23 -0500 | [diff] [blame] | 241 | { |
| 242 | std::map<std::string, std::variant<std::string>> properties{}; |
| 243 | std::string interface; |
| 244 | msg.read(interface, properties); |
| 245 | const auto stateEntry = properties.find("CurrentHostState"); |
| 246 | if (stateEntry != properties.end()) |
| 247 | { |
| 248 | auto stateEntryValue = stateEntry->second; |
| 249 | auto propVal = std::get<std::string>(stateEntryValue); |
| 250 | if (propVal == "xyz.openbmc_project.State.Host.HostState.Off") |
| 251 | { |
| 252 | clearData(); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 257 | void Interface::clearData() |
| 258 | { |
Chris Cain | 72d01aa | 2022-06-14 16:28:03 -0500 | [diff] [blame] | 259 | if (!sensorToOCCInstance.empty()) |
| 260 | { |
| 261 | log<level::INFO>( |
| 262 | fmt::format("clearData: Clearing sensorToOCCInstance ({} entries)", |
| 263 | sensorToOCCInstance.size()) |
| 264 | .c_str()); |
| 265 | for (auto entry : sensorToOCCInstance) |
| 266 | { |
| 267 | log<level::INFO>( |
| 268 | fmt::format("clearData: OCC{} / sensorID: 0x{:04X}", |
| 269 | entry.second, entry.first) |
| 270 | .c_str()); |
| 271 | } |
| 272 | sensorToOCCInstance.clear(); |
| 273 | } |
| 274 | if (!occInstanceToEffecter.empty()) |
| 275 | { |
| 276 | log<level::DEBUG>( |
| 277 | fmt::format( |
| 278 | "clearData: Clearing occInstanceToEffecter ({} entries)", |
| 279 | occInstanceToEffecter.size()) |
| 280 | .c_str()); |
| 281 | occInstanceToEffecter.clear(); |
| 282 | } |
| 283 | if (!sensorToSBEInstance.empty()) |
| 284 | { |
| 285 | log<level::DEBUG>( |
| 286 | fmt::format("clearData: Clearing sensorToSBEInstance ({} entries)", |
| 287 | sensorToSBEInstance.size()) |
| 288 | .c_str()); |
| 289 | sensorToSBEInstance.clear(); |
| 290 | } |
| 291 | if (!sbeInstanceToEffecter.empty()) |
| 292 | { |
| 293 | log<level::DEBUG>( |
| 294 | fmt::format( |
| 295 | "clearData: Clearing sbeInstanceToEffecter ({} entries)", |
| 296 | sbeInstanceToEffecter.size()) |
| 297 | .c_str()); |
| 298 | sbeInstanceToEffecter.clear(); |
| 299 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 300 | } |
| 301 | |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 302 | void Interface::fetchEffecterInfo(uint16_t stateSetId, |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 303 | InstanceToEffecter& instanceToEffecterMap, |
| 304 | CompositeEffecterCount& effecterCount, |
| 305 | uint8_t& stateIdPos) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 306 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 307 | PdrList pdrs{}; |
| 308 | |
| 309 | auto& bus = open_power::occ::utils::getBus(); |
| 310 | try |
| 311 | { |
| 312 | auto method = bus.new_method_call( |
| 313 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 314 | "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR"); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 315 | method.append(tid, static_cast<uint16_t>(PLDM_ENTITY_PROC), stateSetId); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 316 | |
| 317 | auto responseMsg = bus.call(method); |
| 318 | responseMsg.read(pdrs); |
| 319 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 320 | catch (const sdbusplus::exception_t& e) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 321 | { |
| 322 | log<level::ERR>("pldm: Failed to fetch the state effecter PDRs", |
| 323 | entry("ERROR=%s", e.what())); |
| 324 | } |
| 325 | |
| 326 | if (!pdrs.size()) |
| 327 | { |
| 328 | log<level::ERR>("pldm: state effecter PDRs not present"); |
| 329 | return; |
| 330 | } |
| 331 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 332 | bool offsetFound = false; |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 333 | auto stateEffecterPDR = |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 334 | reinterpret_cast<const pldm_state_effecter_pdr*>(pdrs.front().data()); |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 335 | auto possibleStatesPtr = stateEffecterPDR->possible_states; |
| 336 | for (auto offset = 0; offset < stateEffecterPDR->composite_effecter_count; |
| 337 | offset++) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 338 | { |
| 339 | auto possibleStates = |
| 340 | reinterpret_cast<const state_effecter_possible_states*>( |
| 341 | possibleStatesPtr); |
| 342 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 343 | if (possibleStates->state_set_id == stateSetId) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 344 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 345 | stateIdPos = offset; |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 346 | effecterCount = stateEffecterPDR->composite_effecter_count; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 347 | offsetFound = true; |
| 348 | break; |
| 349 | } |
| 350 | possibleStatesPtr += sizeof(possibleStates->state_set_id) + |
| 351 | sizeof(possibleStates->possible_states_size) + |
| 352 | possibleStates->possible_states_size; |
| 353 | } |
| 354 | |
| 355 | if (!offsetFound) |
| 356 | { |
| 357 | return; |
| 358 | } |
| 359 | |
Chris Cain | 0f51652 | 2022-02-07 14:48:28 -0600 | [diff] [blame] | 360 | std::map<uint32_t, EffecterID> entityInstMap{}; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 361 | for (auto& pdr : pdrs) |
| 362 | { |
| 363 | auto pdrPtr = |
| 364 | reinterpret_cast<const pldm_state_effecter_pdr*>(pdr.data()); |
Chris Cain | 72d01aa | 2022-06-14 16:28:03 -0500 | [diff] [blame] | 365 | uint32_t key = pdrPtr->effecter_id; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 366 | entityInstMap.emplace(key, static_cast<SensorID>(pdrPtr->effecter_id)); |
| 367 | } |
| 368 | |
| 369 | open_power::occ::instanceID position = start; |
| 370 | for (auto const& pair : entityInstMap) |
| 371 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 372 | instanceToEffecterMap.emplace(position, pair.second); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 373 | position++; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | std::vector<uint8_t> |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 378 | Interface::prepareSetEffecterReq(EffecterID effecterId, |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 379 | CompositeEffecterCount effecterCount, |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 380 | uint8_t stateIdPos, uint8_t stateSetValue) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 381 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 382 | if (!getMctpInstanceId()) |
| 383 | { |
| 384 | return std::vector<uint8_t>(); |
| 385 | } |
| 386 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 387 | std::vector<uint8_t> request( |
| 388 | sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(effecterCount) + |
| 389 | (effecterCount * sizeof(set_effecter_state_field))); |
| 390 | auto requestMsg = reinterpret_cast<pldm_msg*>(request.data()); |
| 391 | std::vector<set_effecter_state_field> stateField; |
| 392 | |
| 393 | for (uint8_t effecterPos = 0; effecterPos < effecterCount; effecterPos++) |
| 394 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 395 | if (effecterPos == stateIdPos) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 396 | { |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 397 | stateField.emplace_back( |
| 398 | set_effecter_state_field{PLDM_REQUEST_SET, stateSetValue}); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 399 | } |
| 400 | else |
| 401 | { |
| 402 | stateField.emplace_back( |
| 403 | set_effecter_state_field{PLDM_NO_CHANGE, 0}); |
| 404 | } |
| 405 | } |
| 406 | auto rc = encode_set_state_effecter_states_req( |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 407 | mctpInstance.value(), effecterId, effecterCount, stateField.data(), |
| 408 | requestMsg); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 409 | if (rc != PLDM_SUCCESS) |
| 410 | { |
| 411 | log<level::ERR>("encode set effecter states request returned error ", |
| 412 | entry("RC=%d", rc)); |
| 413 | request.clear(); |
| 414 | } |
| 415 | return request; |
| 416 | } |
| 417 | |
| 418 | void Interface::resetOCC(open_power::occ::instanceID occInstanceId) |
| 419 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 420 | if (open_power::occ::utils::isHostRunning()) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 421 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 422 | if (!isPDREffecterCacheValid()) |
| 423 | { |
| 424 | fetchEffecterInfo(PLDM_STATE_SET_BOOT_RESTART_CAUSE, |
| 425 | occInstanceToEffecter, OCCEffecterCount, |
| 426 | bootRestartPosition); |
| 427 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 428 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 429 | // Find the matching effecter for the OCC instance |
| 430 | auto effecterEntry = occInstanceToEffecter.find(occInstanceId); |
| 431 | if (effecterEntry == occInstanceToEffecter.end()) |
| 432 | { |
| 433 | log<level::ERR>( |
| 434 | fmt::format( |
| 435 | "pldm: Failed to find a matching effecter for OCC instance {}", |
| 436 | occInstanceId) |
| 437 | .c_str()); |
| 438 | |
| 439 | return; |
| 440 | } |
| 441 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 442 | // Prepare the SetStateEffecterStates request to reset the OCC |
| 443 | auto request = prepareSetEffecterReq( |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 444 | effecterEntry->second, OCCEffecterCount, bootRestartPosition, |
| 445 | PLDM_STATE_SET_BOOT_RESTART_CAUSE_WARM_RESET); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 446 | |
| 447 | if (request.empty()) |
| 448 | { |
| 449 | log<level::ERR>( |
| 450 | "pldm: SetStateEffecterStates OCC reset request empty"); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // Send request to reset the OCCs/PM Complex (ignore response) |
| 455 | sendPldm(request, occInstanceId, false); |
| 456 | } |
| 457 | else |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 458 | { |
| 459 | log<level::ERR>( |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 460 | fmt::format("resetOCC: HOST is not running (OCC{})", occInstanceId) |
Chris Cain | 0f51652 | 2022-02-07 14:48:28 -0600 | [diff] [blame] | 461 | .c_str()); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 462 | clearData(); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 463 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void Interface::sendHRESET(open_power::occ::instanceID sbeInstanceId) |
| 467 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 468 | if (open_power::occ::utils::isHostRunning()) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 469 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 470 | if (sbeInstanceToEffecter.empty()) |
| 471 | { |
| 472 | fetchEffecterInfo(PLDM_OEM_IBM_SBE_MAINTENANCE_STATE, |
| 473 | sbeInstanceToEffecter, SBEEffecterCount, |
| 474 | sbeMaintenanceStatePosition); |
| 475 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 476 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 477 | auto effecterEntry = sbeInstanceToEffecter.find(sbeInstanceId); |
| 478 | if (effecterEntry == sbeInstanceToEffecter.end()) |
| 479 | { |
| 480 | log<level::ERR>( |
| 481 | "pldm: Failed to find a matching effecter for SBE instance", |
| 482 | entry("SBE=%d", sbeInstanceId)); |
| 483 | return; |
| 484 | } |
| 485 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 486 | // Prepare the SetStateEffecterStates request to HRESET the SBE |
| 487 | auto request = prepareSetEffecterReq( |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 488 | effecterEntry->second, SBEEffecterCount, |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 489 | sbeMaintenanceStatePosition, SBE_RETRY_REQUIRED); |
| 490 | |
| 491 | if (request.empty()) |
| 492 | { |
| 493 | log<level::ERR>( |
| 494 | "pldm: SetStateEffecterStates HRESET request empty"); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | // Send request to issue HRESET of SBE (ignore response) |
| 499 | sendPldm(request, sbeInstanceId, false); |
Chris Cain | 12d0b82 | 2022-04-22 17:29:18 -0500 | [diff] [blame] | 500 | outstandingHResets.insert(sbeInstanceId); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 501 | } |
| 502 | else |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 503 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 504 | log<level::ERR>(fmt::format("sendHRESET: HOST is not running (OCC{})", |
| 505 | sbeInstanceId) |
| 506 | .c_str()); |
| 507 | clearData(); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 508 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 511 | bool Interface::getMctpInstanceId() |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 512 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 513 | if (!mctpInstance) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 514 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 515 | // Request new instance ID |
| 516 | auto& bus = open_power::occ::utils::getBus(); |
| 517 | try |
| 518 | { |
| 519 | auto method = bus.new_method_call( |
| 520 | "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm", |
| 521 | "xyz.openbmc_project.PLDM.Requester", "GetInstanceId"); |
| 522 | method.append(mctpEid); |
| 523 | auto reply = bus.call(method); |
| 524 | uint8_t newInstanceId; |
| 525 | reply.read(newInstanceId); |
| 526 | mctpInstance = newInstanceId; |
| 527 | log<level::INFO>(fmt::format("pldm: got new InstanceId: {}", |
| 528 | mctpInstance.value()) |
| 529 | .c_str()); |
| 530 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 531 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 532 | { |
| 533 | log<level::ERR>( |
| 534 | fmt::format("pldm: GetInstanceId failed: {}", e.what()) |
| 535 | .c_str()); |
| 536 | return false; |
| 537 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 540 | return true; |
| 541 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 542 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 543 | void Interface::sendPldm(const std::vector<uint8_t>& request, |
| 544 | const uint8_t instance, const bool rspExpected) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 545 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 546 | if (!mctpInstance) |
| 547 | { |
| 548 | log<level::ERR>("sendPldm: No MCTP Instance ID found!"); |
| 549 | return; |
| 550 | } |
| 551 | |
Chris Cain | c9dc441 | 2023-03-06 15:56:34 -0600 | [diff] [blame^] | 552 | // Connect to MCTP socket |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 553 | pldmFd = pldm_open(); |
| 554 | auto openErrno = errno; |
| 555 | if (pldmFd == PLDM_REQUESTER_OPEN_FAIL) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 556 | { |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 557 | log<level::ERR>( |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 558 | fmt::format( |
| 559 | "sendPldm: Failed to connect to MCTP socket, errno={}/{}", |
| 560 | openErrno, strerror(openErrno)) |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 561 | .c_str()); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 562 | return; |
| 563 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 564 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 565 | // Send the PLDM request message to HBRT |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 566 | if (rspExpected) |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 567 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 568 | // Register callback when response is available |
| 569 | registerPldmRspCallback(); |
| 570 | |
| 571 | // Send PLDM request |
| 572 | log<level::INFO>( |
| 573 | fmt::format( |
| 574 | "sendPldm: calling pldm_send(OCC{}, instance:{}, {} bytes)", |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 575 | instance, mctpInstance.value(), request.size()) |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 576 | .c_str()); |
| 577 | pldmResponseReceived = false; |
| 578 | pldmResponseTimeout = false; |
| 579 | pldmResponseOcc = instance; |
| 580 | auto pldmRc = |
| 581 | pldm_send(mctpEid, pldmFd, request.data(), request.size()); |
| 582 | auto sendErrno = errno; |
| 583 | if (pldmRc != PLDM_REQUESTER_SUCCESS) |
| 584 | { |
| 585 | log<level::ERR>( |
| 586 | fmt::format( |
| 587 | "sendPldm: pldm_send failed with rc={} and errno={}/{}", |
| 588 | pldmRc, sendErrno, strerror(sendErrno)) |
| 589 | .c_str()); |
| 590 | pldmClose(); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | // start timer waiting for the response |
| 595 | using namespace std::literals::chrono_literals; |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame] | 596 | pldmRspTimer.restartOnce(8s); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 597 | |
| 598 | // Wait for response/timeout |
| 599 | } |
| 600 | else // not expecting the response |
| 601 | { |
| 602 | log<level::INFO>( |
| 603 | fmt::format( |
| 604 | "sendPldm: calling pldm_send(mctpID:{}, fd:{}, {} bytes) for OCC{}", |
| 605 | mctpEid, pldmFd, request.size(), instance) |
| 606 | .c_str()); |
| 607 | auto rc = pldm_send(mctpEid, pldmFd, request.data(), request.size()); |
| 608 | auto sendErrno = errno; |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 609 | if (rc) |
| 610 | { |
| 611 | log<level::ERR>( |
| 612 | fmt::format( |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 613 | "sendPldm: pldm_send(mctpID:{}, fd:{}, {} bytes) failed with rc={} and errno={}/{}", |
| 614 | mctpEid, pldmFd, request.size(), rc, sendErrno, |
| 615 | strerror(sendErrno)) |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 616 | .c_str()); |
| 617 | } |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 618 | else |
| 619 | { |
| 620 | // Not waiting for response, instance ID should be freed |
| 621 | mctpInstance = std::nullopt; |
| 622 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 623 | pldmClose(); |
| 624 | } |
| 625 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 626 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 627 | // Attaches the FD to event loop and registers the callback handler |
| 628 | void Interface::registerPldmRspCallback() |
| 629 | { |
| 630 | decltype(eventSource.get()) sourcePtr = nullptr; |
| 631 | auto rc = sd_event_add_io(event.get(), &sourcePtr, pldmFd, EPOLLIN, |
| 632 | pldmRspCallback, this); |
| 633 | if (rc < 0) |
| 634 | { |
| 635 | log<level::ERR>( |
| 636 | fmt::format( |
| 637 | "registerPldmRspCallback: sd_event_add_io: Error({})={} : fd={}", |
| 638 | rc, strerror(-rc), pldmFd) |
| 639 | .c_str()); |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 640 | } |
| 641 | else |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 642 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 643 | // puts sourcePtr in the event source. |
| 644 | eventSource.reset(sourcePtr); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // Add a timer to the event loop, default 30s. |
| 649 | void Interface::pldmRspExpired() |
| 650 | { |
| 651 | if (!pldmResponseReceived) |
| 652 | { |
| 653 | log<level::ERR>( |
| 654 | fmt::format( |
| 655 | "pldmRspExpired: timerCallback - timeout waiting for pldm response for OCC{}", |
| 656 | pldmResponseOcc) |
| 657 | .c_str()); |
| 658 | pldmResponseTimeout = true; |
| 659 | if (pldmFd) |
| 660 | { |
| 661 | pldmClose(); |
| 662 | } |
| 663 | } |
| 664 | return; |
| 665 | }; |
| 666 | |
| 667 | void Interface::pldmClose() |
| 668 | { |
| 669 | if (pldmRspTimer.isEnabled()) |
| 670 | { |
| 671 | // stop PLDM response timer |
| 672 | pldmRspTimer.setEnabled(false); |
| 673 | } |
| 674 | close(pldmFd); |
| 675 | pldmFd = -1; |
| 676 | eventSource.reset(); |
| 677 | } |
| 678 | |
| 679 | int Interface::pldmRspCallback(sd_event_source* /*es*/, int fd, |
| 680 | uint32_t revents, void* userData) |
| 681 | { |
| 682 | if (!(revents & EPOLLIN)) |
| 683 | { |
| 684 | log<level::INFO>( |
| 685 | fmt::format("pldmRspCallback - revents={:08X}", revents).c_str()); |
| 686 | return -1; |
| 687 | } |
| 688 | |
| 689 | auto pldmIface = static_cast<Interface*>(userData); |
| 690 | |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 691 | if (!pldmIface->mctpInstance) |
| 692 | { |
| 693 | log<level::ERR>( |
| 694 | "pldmRspCallback: No outstanding MCTP Instance ID found"); |
| 695 | return -1; |
| 696 | } |
| 697 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 698 | uint8_t* responseMsg = nullptr; |
| 699 | size_t responseMsgSize{}; |
| 700 | |
| 701 | log<level::INFO>( |
| 702 | fmt::format("pldmRspCallback: calling pldm_recv() instance:{}", |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 703 | pldmIface->mctpInstance.value()) |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 704 | .c_str()); |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 705 | auto rc = pldm_recv(mctpEid, fd, pldmIface->mctpInstance.value(), |
| 706 | &responseMsg, &responseMsgSize); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 707 | int lastErrno = errno; |
| 708 | if (rc) |
| 709 | { |
| 710 | log<level::ERR>( |
| 711 | fmt::format( |
| 712 | "pldmRspCallback: pldm_recv failed with rc={}, errno={}/{}", rc, |
| 713 | lastErrno, strerror(lastErrno)) |
| 714 | .c_str()); |
| 715 | return -1; |
| 716 | } |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 717 | |
| 718 | // We got the response for the PLDM request msg that was sent |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 719 | log<level::INFO>( |
| 720 | fmt::format("pldmRspCallback: pldm_recv() rsp was {} bytes", |
| 721 | responseMsgSize) |
| 722 | .c_str()); |
| 723 | |
| 724 | if (pldmIface->pldmRspTimer.isEnabled()) |
| 725 | { |
| 726 | // stop PLDM response timer |
| 727 | pldmIface->pldmRspTimer.setEnabled(false); |
| 728 | } |
| 729 | |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 730 | // instance ID should be freed |
| 731 | pldmIface->mctpInstance = std::nullopt; |
| 732 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 733 | // Set pointer to autodelete |
| 734 | std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{responseMsg, |
| 735 | std::free}; |
| 736 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 737 | auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get()); |
| 738 | if (response->payload[0] != PLDM_SUCCESS) |
| 739 | { |
| 740 | log<level::ERR>( |
| 741 | fmt::format("pldmRspCallback: payload[0] was not success: {}", |
| 742 | response->payload[0]) |
| 743 | .c_str()); |
| 744 | pldmIface->pldmClose(); |
| 745 | return -1; |
| 746 | } |
| 747 | |
| 748 | // Decode the response |
| 749 | uint8_t compCode = 0, sensorCount = 1; |
| 750 | get_sensor_state_field field[6]; |
| 751 | responseMsgSize -= sizeof(pldm_msg_hdr); |
| 752 | auto msgRc = decode_get_state_sensor_readings_resp( |
| 753 | response, responseMsgSize, &compCode, &sensorCount, field); |
| 754 | if ((msgRc != PLDM_SUCCESS) || (compCode != PLDM_SUCCESS)) |
| 755 | { |
| 756 | log<level::ERR>( |
| 757 | fmt::format( |
| 758 | "pldmRspCallback: decode_get_state_sensor_readings failed with rc={} and compCode={}", |
| 759 | msgRc, compCode) |
| 760 | .c_str()); |
| 761 | pldmIface->pldmClose(); |
| 762 | return -1; |
| 763 | } |
| 764 | |
| 765 | pldmIface->pldmClose(); |
| 766 | |
| 767 | const uint8_t instance = pldmIface->pldmResponseOcc; |
| 768 | const uint8_t occSensorState = field[0].present_state; |
| 769 | pldmIface->pldmResponseReceived = true; |
| 770 | |
| 771 | if (occSensorState == PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_IN_SERVICE) |
| 772 | { |
| 773 | log<level::INFO>( |
| 774 | fmt::format("pldmRspCallback: OCC{} is RUNNING", instance).c_str()); |
| 775 | pldmIface->callBack(instance, true); |
| 776 | } |
Chris Cain | 733b201 | 2022-05-04 11:54:06 -0500 | [diff] [blame] | 777 | else if (occSensorState == |
| 778 | PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_DORMANT) |
| 779 | { |
| 780 | log<level::INFO>( |
| 781 | fmt::format( |
| 782 | "pldmRspCallback: OCC{} has now STOPPED and system is in SAFE MODE", |
| 783 | instance) |
| 784 | .c_str()); |
Sheldon Bailey | 31a2f13 | 2022-05-20 11:31:52 -0500 | [diff] [blame] | 785 | |
| 786 | // Setting safe mode true |
| 787 | pldmIface->safeModeCallBack(true); |
| 788 | |
Chris Cain | 733b201 | 2022-05-04 11:54:06 -0500 | [diff] [blame] | 789 | pldmIface->callBack(instance, false); |
| 790 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 791 | else |
| 792 | { |
| 793 | log<level::INFO>( |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 794 | fmt::format( |
| 795 | "pldmRspCallback: OCC{} is not running (sensor state:{})", |
| 796 | instance, occSensorState) |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 797 | .c_str()); |
Chris Cain | c9dc441 | 2023-03-06 15:56:34 -0600 | [diff] [blame^] | 798 | if (occSensorState != PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPED) |
| 799 | { |
| 800 | const size_t rspLength = responseMsgSize + sizeof(pldm_msg_hdr); |
| 801 | std::vector<std::uint8_t> pldmResponse(rspLength); |
| 802 | memcpy(&pldmResponse[0], reinterpret_cast<std::uint8_t*>(response), |
| 803 | rspLength); |
| 804 | log<level::ERR>( |
| 805 | fmt::format( |
| 806 | "pldmRspCallback: Bad State - PLDM response ({} bytes) for OCC{}:", |
| 807 | rspLength, instance) |
| 808 | .c_str()); |
| 809 | dump_hex(pldmResponse); |
| 810 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 811 | pldmIface->callBack(instance, false); |
| 812 | } |
| 813 | |
| 814 | return 0; |
| 815 | }; |
| 816 | |
| 817 | std::vector<uint8_t> Interface::encodeGetStateSensorRequest(uint8_t instance, |
| 818 | uint16_t sensorId) |
| 819 | { |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 820 | if (!getMctpInstanceId()) |
| 821 | { |
| 822 | log<level::ERR>( |
| 823 | "encodeGetStateSensorRequest: failed to getMctpInstanceId"); |
| 824 | return std::vector<uint8_t>(); |
| 825 | } |
| 826 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 827 | bitfield8_t sRearm = {0}; |
| 828 | const size_t msgSize = |
| 829 | sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES; |
| 830 | std::vector<uint8_t> request(msgSize); |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 831 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 832 | auto msg = reinterpret_cast<pldm_msg*>(request.data()); |
Chris Cain | 8b508bf | 2022-05-26 14:01:31 -0500 | [diff] [blame] | 833 | auto msgRc = encode_get_state_sensor_readings_req(mctpInstance.value(), |
| 834 | sensorId, sRearm, 0, msg); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 835 | if (msgRc != PLDM_SUCCESS) |
| 836 | { |
| 837 | log<level::ERR>( |
| 838 | fmt::format( |
| 839 | "encodeGetStateSensorRequest: Failed to encode sensorId:0x{:08X} for OCC{} (rc={})", |
| 840 | sensorId, instance, msgRc) |
| 841 | .c_str()); |
| 842 | } |
| 843 | return request; |
| 844 | } |
| 845 | |
| 846 | // Initiate query of the specified OCC Active Sensor |
| 847 | void Interface::checkActiveSensor(uint8_t instance) |
| 848 | { |
| 849 | static bool tracedOnce = false; |
| 850 | if (pldmFd > 0) |
| 851 | { |
| 852 | if (!tracedOnce) |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 853 | { |
| 854 | log<level::ERR>( |
| 855 | fmt::format( |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 856 | "checkActiveSensor: already waiting on OCC{} (fd={})", |
| 857 | pldmResponseOcc, pldmFd) |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 858 | .c_str()); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 859 | tracedOnce = true; |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 860 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | tracedOnce = false; |
| 864 | |
| 865 | if (!isOCCSensorCacheValid()) |
| 866 | { |
| 867 | fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS, |
| 868 | sensorToOCCInstance, OCCSensorOffset); |
| 869 | } |
| 870 | |
| 871 | // look up sensor id (key) based on instance |
| 872 | auto entry = std::find_if( |
| 873 | sensorToOCCInstance.begin(), sensorToOCCInstance.end(), |
| 874 | [instance](const auto& entry) { return instance == entry.second; }); |
| 875 | if (entry != sensorToOCCInstance.end()) |
| 876 | { |
| 877 | // Query the OCC Active Sensor state for this instance |
| 878 | // SensorID sID = entry->first; |
| 879 | log<level::INFO>( |
| 880 | fmt::format("checkActiveSensor: OCC{} / sensorID: 0x{:04X}", |
| 881 | instance, entry->first) |
| 882 | .c_str()); |
| 883 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 884 | // Encode GetStateSensorReadings PLDM message |
| 885 | auto request = encodeGetStateSensorRequest(instance, entry->first); |
| 886 | if (request.empty()) |
| 887 | { |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | // Send request to PLDM and setup callback for response |
| 892 | sendPldm(request, instance, true); |
| 893 | } |
| 894 | else |
| 895 | { |
| 896 | log<level::ERR>( |
| 897 | fmt::format( |
| 898 | "checkActiveSensor: Unable to find PLDM sensor for OCC{}", |
| 899 | instance) |
| 900 | .c_str()); |
Chris Cain | 8cf7496 | 2022-06-29 08:45:16 -0500 | [diff] [blame] | 901 | log<level::INFO>( |
| 902 | "checkActiveSensor: fetching STATE_SET_OPERATIONAL_RUNNING_STATUS"); |
| 903 | fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS, |
| 904 | sensorToOCCInstance, OCCSensorOffset); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 905 | } |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | } // namespace pldm |