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