Harshit Aghera | 09f6f2c | 2025-05-07 16:20:16 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #include <GpuMctpVdm.hpp> |
| 7 | #include <MctpRequester.hpp> |
| 8 | #include <OcpMctpVdm.hpp> |
| 9 | #include <phosphor-logging/lg2.hpp> |
| 10 | |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <functional> |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
| 17 | void readThermalParameter(uint8_t eid, uint8_t id, |
| 18 | mctp::MctpRequester& mctpRequester, |
| 19 | const std::function<void(uint8_t, int32_t)>& callback) |
| 20 | { |
| 21 | std::vector<uint8_t> reqMsg( |
| 22 | sizeof(ocp::accelerator_management::BindingPciVid) + |
| 23 | sizeof(gpu::ReadThermalParametersRequest)); |
| 24 | |
| 25 | auto* msg = new (reqMsg.data()) ocp::accelerator_management::Message; |
| 26 | |
| 27 | auto rc = gpu::encodeReadThermalParametersRequest(0, id, *msg); |
| 28 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 29 | { |
| 30 | lg2::error("encodeReadThermalParametersRequest failed, rc={RC}", "RC", |
| 31 | static_cast<int>(rc)); |
| 32 | |
| 33 | callback(-1, 0); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | mctpRequester.sendRecvMsg( |
| 38 | eid, reqMsg, |
| 39 | [callback](int sendRecvMsgResult, std::vector<uint8_t> respMsg) { |
| 40 | if (sendRecvMsgResult != 0) |
| 41 | { |
| 42 | lg2::error("MctpRequester::sendRecvMsg() failed, rc={RC}", "RC", |
| 43 | sendRecvMsgResult); |
| 44 | |
| 45 | callback(-2, 0); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (respMsg.empty()) |
| 50 | { |
| 51 | lg2::error("MctpRequester::sendRecvMsg() failed, respMsgLen=0"); |
| 52 | |
| 53 | callback(-3, 0); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | uint8_t cc = 0; |
| 58 | uint16_t reasonCode = 0; |
| 59 | int32_t threshold = 0; |
| 60 | |
| 61 | auto rc = gpu::decodeReadThermalParametersResponse( |
| 62 | *new (respMsg.data()) ocp::accelerator_management::Message, |
| 63 | respMsg.size(), cc, reasonCode, threshold); |
| 64 | |
| 65 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS || |
| 66 | cc != static_cast<uint8_t>( |
| 67 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 68 | { |
| 69 | lg2::error( |
| 70 | "decodeReadThermalParametersResponse() failed, rc={RC} cc={CC} reasonCode={RESC}", |
| 71 | "RC", static_cast<int>(rc), "CC", cc, "RESC", reasonCode); |
| 72 | |
| 73 | callback(-4, 0); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | callback(0, threshold); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | void readThermalParameterCallback( |
| 82 | uint8_t eid, const std::shared_ptr<std::vector<uint8_t>>& ids, |
| 83 | mctp::MctpRequester& mctpRequester, |
| 84 | const std::function<void(uint8_t, std::vector<int32_t>)>& callback, |
| 85 | size_t index, const std::shared_ptr<std::vector<int32_t>>& thresholds, |
| 86 | uint8_t rc, int32_t threshold) |
| 87 | { |
| 88 | if (rc != 0) |
| 89 | { |
| 90 | callback(rc, *thresholds); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | thresholds->push_back(threshold); |
| 95 | |
| 96 | ++index; |
| 97 | if (index == ids->size()) |
| 98 | { |
| 99 | callback(rc, *thresholds); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | readThermalParameter(eid, (*ids)[index], mctpRequester, |
| 104 | std::bind_front(readThermalParameterCallback, eid, |
| 105 | ids, std::ref(mctpRequester), |
| 106 | callback, index, thresholds)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void readThermalParametersBatched( |
| 111 | uint8_t eid, const std::shared_ptr<std::vector<uint8_t>>& ids, |
| 112 | mctp::MctpRequester& mctpRequester, |
| 113 | const std::function<void(uint8_t, std::vector<int32_t>)>& callback) |
| 114 | { |
| 115 | auto thresholds = std::make_shared<std::vector<int32_t>>(); |
| 116 | size_t index = 0; |
| 117 | |
| 118 | readThermalParameter( |
| 119 | eid, (*ids)[index], mctpRequester, |
| 120 | std::bind_front(readThermalParameterCallback, eid, ids, |
| 121 | std::ref(mctpRequester), callback, index, thresholds)); |
| 122 | } |