blob: 1bb9c0df33f1bb21e866a6ea6387504257c594d9 [file] [log] [blame]
Harshit Agherac20108d2025-05-07 16:20:16 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
3 * AFFILIATES. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "NvidiaGpuThresholds.hpp"
8
9#include <MctpRequester.hpp>
10#include <NvidiaGpuMctpVdm.hpp>
11#include <OcpMctpVdm.hpp>
12#include <phosphor-logging/lg2.hpp>
13
14#include <array>
15#include <cerrno>
16#include <cstddef>
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <span>
21#include <vector>
22
23void processReadThermalParameterResponse(
24 const std::function<void(uint8_t, int32_t)>& callback,
25 const std::span<const uint8_t> respMsg, int sendRecvMsgResult)
26{
27 if (sendRecvMsgResult != 0)
28 {
29 lg2::error(
30 "Error reading thermal parameter: sending message over MCTP failed, rc={RC}",
31 "RC", sendRecvMsgResult);
32 callback(EPROTO, 0);
33 return;
34 }
35
36 ocp::accelerator_management::CompletionCode cc{};
37 uint16_t reasonCode = 0;
38 int32_t threshold = 0;
39
40 auto rc = gpu::decodeReadThermalParametersResponse(respMsg, cc, reasonCode,
41 threshold);
42
43 if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS)
44 {
45 lg2::error(
46 "Error reading thermal parameter: decode failed, rc={RC}, cc={CC}, reasonCode={RESC}",
47 "RC", rc, "CC", cc, "RESC", reasonCode);
48 callback(EPROTO, 0);
49 return;
50 }
51
52 callback(0, threshold);
53};
54
55void readThermalParameter(uint8_t eid, uint8_t id,
56 mctp::MctpRequester& mctpRequester,
57 const std::function<void(uint8_t, int32_t)>& callback)
58{
59 static std::array<uint8_t, sizeof(gpu::ReadThermalParametersRequest)>
60 reqMsg{};
61
62 static std::array<uint8_t, sizeof(gpu::ReadThermalParametersResponse)>
63 respMsg{};
64
65 auto rc = gpu::encodeReadThermalParametersRequest(0, id, reqMsg);
66 if (rc != 0)
67 {
68 lg2::error(
69 "Error reading thermal parameter for eid {EID} and parameter id {PID} : encode failed. rc={RC}",
70 "EID", eid, "PID", id, "RC", rc);
71 callback(rc, 0);
72 return;
73 }
74
75 mctpRequester.sendRecvMsg(
76 eid, reqMsg, respMsg,
77 std::bind_front(processReadThermalParameterResponse, callback,
78 std::span{respMsg}));
79}
80
81void 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 lg2::error(
91 "Error reading thermal parameter for eid {EID} and parameter id {PID}. rc={RC}",
92 "EID", eid, "PID", (*ids)[index], "RC", rc);
93 return;
94 callback(rc, *thresholds);
95 return;
96 }
97
98 thresholds->push_back(threshold);
99
100 ++index;
101 if (index == ids->size())
102 {
103 callback(rc, *thresholds);
104 }
105 else
106 {
107 readThermalParameter(eid, (*ids)[index], mctpRequester,
108 std::bind_front(readThermalParameterCallback, eid,
109 ids, std::ref(mctpRequester),
110 callback, index, thresholds));
111 }
112}
113
114void readThermalParameters(
115 uint8_t eid, const std::vector<uint8_t>& ids,
116 mctp::MctpRequester& mctpRequester,
117 const std::function<void(uint8_t, std::vector<int32_t>)>& callback)
118{
119 auto thresholds = std::make_shared<std::vector<int32_t>>();
120 size_t index = 0;
121
122 readThermalParameter(
123 eid, ids[index], mctpRequester,
124 std::bind_front(readThermalParameterCallback, eid,
125 std::make_shared<std::vector<uint8_t>>(ids),
126 std::ref(mctpRequester), callback, index, thresholds));
127}