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