blob: e1d86ecb0f040556c0083aa23a399fb6e23e82b1 [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include "base.hpp"
George Liuc453e162022-12-21 17:16:23 +08002
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05003#include "common/utils.hpp"
4#include "libpldmresponder/pdr.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06005
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/base.h>
7#include <libpldm/bios.h>
8#include <libpldm/fru.h>
9#include <libpldm/platform.h>
George Liuc453e162022-12-21 17:16:23 +080010
Riya Dixit49cfb132023-03-02 04:26:53 -060011#include <phosphor-logging/lg2.hpp>
12
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060013#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -060014#include <cstring>
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050015#include <iostream>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060016#include <map>
17#include <stdexcept>
18#include <vector>
19
Sridevi Ramesh45e52542020-04-03 07:54:18 -050020#ifdef OEM_IBM
George Liuc453e162022-12-21 17:16:23 +080021#include <libpldm/file_io.h>
22#include <libpldm/host.h>
Sridevi Ramesh45e52542020-04-03 07:54:18 -050023#endif
24
Riya Dixit49cfb132023-03-02 04:26:53 -060025PHOSPHOR_LOG2_USING;
26
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060027namespace pldm
28{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060029using Type = uint8_t;
30
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060031namespace responder
32{
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060033using Cmd = std::vector<uint8_t>;
34
35static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050036 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080037 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
38 PLDM_GET_PLDM_COMMANDS}},
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050039 {PLDM_PLATFORM,
40 {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES, PLDM_SET_EVENT_RECEIVER,
41 PLDM_GET_SENSOR_READING, PLDM_GET_STATE_SENSOR_READINGS,
42 PLDM_SET_NUMERIC_EFFECTER_VALUE, PLDM_GET_NUMERIC_EFFECTER_VALUE,
43 PLDM_PLATFORM_EVENT_MESSAGE}},
John Wang8721ed62019-12-05 14:44:43 +080044 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060045 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080046 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050047 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE, PLDM_SET_BIOS_TABLE}},
John Wang5bdde3a2020-06-16 15:02:33 +080048 {PLDM_FRU,
49 {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE,
50 PLDM_GET_FRU_RECORD_BY_OPTION}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050051#ifdef OEM_IBM
52 {PLDM_OEM,
53 {PLDM_HOST_GET_ALERT_STATUS, PLDM_GET_FILE_TABLE, PLDM_READ_FILE,
54 PLDM_WRITE_FILE, PLDM_READ_FILE_INTO_MEMORY, PLDM_WRITE_FILE_FROM_MEMORY,
55 PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
56 PLDM_NEW_FILE_AVAILABLE, PLDM_READ_FILE_BY_TYPE, PLDM_WRITE_FILE_BY_TYPE,
57 PLDM_FILE_ACK}},
58#endif
59};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060060
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060061static const std::map<Type, ver32_t> versions{
Manojkiran Edacb454672022-06-29 11:05:42 +053062 {PLDM_BASE, {0x00, 0xf0, 0xf0, 0xf1}},
63 {PLDM_PLATFORM, {0x00, 0xf0, 0xf2, 0xf1}},
64 {PLDM_BIOS, {0x00, 0xf0, 0xf0, 0xf1}},
65 {PLDM_FRU, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050066#ifdef OEM_IBM
Manojkiran Edacb454672022-06-29 11:05:42 +053067 {PLDM_OEM, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050068#endif
Sampa Misra432e1872019-02-13 03:49:43 -060069};
70
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050071namespace base
72{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060073Response Handler::getPLDMTypes(const pldm_msg* request,
74 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060075{
76 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060077 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060078 for (const auto& type : capabilities)
79 {
80 auto index = type.first / 8;
81 // <Type Number> = <Array Index> * 8 + <bit position>
82 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060083 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060084 }
85
vkaverapa6575b82019-04-03 05:33:52 -050086 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
87 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080088 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
89 types.data(), responsePtr);
90 if (rc != PLDM_SUCCESS)
91 {
92 return CmdHandler::ccOnlyResponse(request, rc);
93 }
vkaverapa6575b82019-04-03 05:33:52 -050094
95 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060096}
97
Deepak Kodihallibc669f12019-11-28 08:52:07 -060098Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060099{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600100 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600101 Type type;
102
vkaverapa6575b82019-04-03 05:33:52 -0500103 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
104 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600105
Zahed Hossain223a73d2019-07-04 12:46:18 -0500106 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -0500107
108 if (rc != PLDM_SUCCESS)
109 {
George Liufb8611d2019-12-06 10:14:15 +0800110 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500111 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600112
113 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600114 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600115 if (capabilities.find(type) == capabilities.end())
116 {
George Liufb8611d2019-12-06 10:14:15 +0800117 return CmdHandler::ccOnlyResponse(request,
118 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600119 }
120
121 for (const auto& cmd : capabilities.at(type))
122 {
123 auto index = cmd / 8;
124 // <Type Number> = <Array Index> * 8 + <bit position>
125 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600126 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600127 }
128
George Liufb8611d2019-12-06 10:14:15 +0800129 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
130 cmds.data(), responsePtr);
131 if (rc != PLDM_SUCCESS)
132 {
133 return ccOnlyResponse(request, rc);
134 }
vkaverapa6575b82019-04-03 05:33:52 -0500135
136 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600137}
138
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600139Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600140{
141 uint32_t transferHandle;
142 Type type;
143 uint8_t transferFlag;
144
vkaverapa6575b82019-04-03 05:33:52 -0500145 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
146 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600147
Zahed Hossain223a73d2019-07-04 12:46:18 -0500148 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
149 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500150
151 if (rc != PLDM_SUCCESS)
152 {
George Liufb8611d2019-12-06 10:14:15 +0800153 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500154 }
Sampa Misra432e1872019-02-13 03:49:43 -0600155
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600156 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600157 auto search = versions.find(type);
158
159 if (search == versions.end())
160 {
George Liufb8611d2019-12-06 10:14:15 +0800161 return CmdHandler::ccOnlyResponse(request,
162 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600163 }
164
165 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800166 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
167 PLDM_START_AND_END, &version,
168 sizeof(pldm_version), responsePtr);
169 if (rc != PLDM_SUCCESS)
170 {
171 return ccOnlyResponse(request, rc);
172 }
vkaverapa6575b82019-04-03 05:33:52 -0500173
174 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600175}
176
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500177void Handler::processSetEventReceiver(
178 sdeventplus::source::EventBase& /*source */)
179{
180 survEvent.reset();
181 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
182 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
183 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930184 auto instanceId = instanceIdDb.next(eid);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500185 uint8_t eventMessageGlobalEnable =
186 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
187 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
188 uint8_t eventReceiverAddressInfo = pldm::responder::pdr::BmcMctpEid;
189 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
190
191 auto rc = encode_set_event_receiver_req(
192 instanceId, eventMessageGlobalEnable, transportProtocolType,
193 eventReceiverAddressInfo, heartbeatTimer, request);
194 if (rc != PLDM_SUCCESS)
195 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930196 instanceIdDb.free(eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600197 error("Failed to encode_set_event_receiver_req, rc = {RC}", "RC",
198 lg2::hex, rc);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500199 return;
200 }
201
Patrick Williams6da4f912023-05-10 07:50:53 -0500202 auto processSetEventReceiverResponse =
203 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500204 if (response == nullptr || !respMsgLen)
205 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600206 error("Failed to receive response for setEventReceiver command");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500207 return;
208 }
209
210 uint8_t completionCode{};
211 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
212 &completionCode);
213 if (rc || completionCode)
214 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600215 error(
216 "Failed to decode setEventReceiver command response, rc = {RC}, cc = {CC}",
217 "RC", rc, "CC", (unsigned)completionCode);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500218 pldm::utils::reportError(
219 "xyz.openbmc_project.bmc.pldm.InternalFailure");
220 }
221 };
222 rc = handler->registerRequest(
223 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
224 std::move(requestMsg), std::move(processSetEventReceiverResponse));
225
226 if (rc != PLDM_SUCCESS)
227 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600228 error("Failed to send the setEventReceiver request");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500229 }
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500230
231 if (oemPlatformHandler)
232 {
233 oemPlatformHandler->countSetEventReceiver();
234 oemPlatformHandler->checkAndDisableWatchDog();
235 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500236}
237
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600238Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800239{
John Wang5c4f80d2019-07-29 11:12:18 +0800240 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
241 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600242 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS,
243 TERMINUS_ID, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800244 if (rc != PLDM_SUCCESS)
245 {
246 return ccOnlyResponse(request, rc);
247 }
John Wang5c4f80d2019-07-29 11:12:18 +0800248
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500249 survEvent = std::make_unique<sdeventplus::source::Defer>(
250 event, std::bind_front(&Handler::processSetEventReceiver, this));
251
John Wang5c4f80d2019-07-29 11:12:18 +0800252 return response;
253}
254
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600255} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600256} // namespace responder
257} // namespace pldm