blob: e675be880281c98208791344213ffd96e9b2b3b1 [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>
10#include <libpldm/pldm.h>
11
Riya Dixit49cfb132023-03-02 04:26:53 -060012#include <phosphor-logging/lg2.hpp>
13
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060014#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -060015#include <cstring>
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050016#include <iostream>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060017#include <map>
18#include <stdexcept>
19#include <vector>
20
Sridevi Ramesh45e52542020-04-03 07:54:18 -050021#ifdef OEM_IBM
George Liuc453e162022-12-21 17:16:23 +080022#include <libpldm/file_io.h>
23#include <libpldm/host.h>
Sridevi Ramesh45e52542020-04-03 07:54:18 -050024#endif
25
Riya Dixit49cfb132023-03-02 04:26:53 -060026PHOSPHOR_LOG2_USING;
27
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060028namespace pldm
29{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060030using Type = uint8_t;
31
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060032namespace responder
33{
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060034using Cmd = std::vector<uint8_t>;
35
36static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050037 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080038 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
39 PLDM_GET_PLDM_COMMANDS}},
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050040 {PLDM_PLATFORM,
41 {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES, PLDM_SET_EVENT_RECEIVER,
42 PLDM_GET_SENSOR_READING, PLDM_GET_STATE_SENSOR_READINGS,
43 PLDM_SET_NUMERIC_EFFECTER_VALUE, PLDM_GET_NUMERIC_EFFECTER_VALUE,
44 PLDM_PLATFORM_EVENT_MESSAGE}},
John Wang8721ed62019-12-05 14:44:43 +080045 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060046 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080047 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050048 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE, PLDM_SET_BIOS_TABLE}},
John Wang5bdde3a2020-06-16 15:02:33 +080049 {PLDM_FRU,
50 {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE,
51 PLDM_GET_FRU_RECORD_BY_OPTION}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050052#ifdef OEM_IBM
53 {PLDM_OEM,
54 {PLDM_HOST_GET_ALERT_STATUS, PLDM_GET_FILE_TABLE, PLDM_READ_FILE,
55 PLDM_WRITE_FILE, PLDM_READ_FILE_INTO_MEMORY, PLDM_WRITE_FILE_FROM_MEMORY,
56 PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
57 PLDM_NEW_FILE_AVAILABLE, PLDM_READ_FILE_BY_TYPE, PLDM_WRITE_FILE_BY_TYPE,
58 PLDM_FILE_ACK}},
59#endif
60};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060061
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060062static const std::map<Type, ver32_t> versions{
Manojkiran Edacb454672022-06-29 11:05:42 +053063 {PLDM_BASE, {0x00, 0xf0, 0xf0, 0xf1}},
64 {PLDM_PLATFORM, {0x00, 0xf0, 0xf2, 0xf1}},
65 {PLDM_BIOS, {0x00, 0xf0, 0xf0, 0xf1}},
66 {PLDM_FRU, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050067#ifdef OEM_IBM
Manojkiran Edacb454672022-06-29 11:05:42 +053068 {PLDM_OEM, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050069#endif
Sampa Misra432e1872019-02-13 03:49:43 -060070};
71
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050072namespace base
73{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060074Response Handler::getPLDMTypes(const pldm_msg* request,
75 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060076{
77 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060078 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060079 for (const auto& type : capabilities)
80 {
81 auto index = type.first / 8;
82 // <Type Number> = <Array Index> * 8 + <bit position>
83 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060084 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060085 }
86
vkaverapa6575b82019-04-03 05:33:52 -050087 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
88 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080089 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
90 types.data(), responsePtr);
91 if (rc != PLDM_SUCCESS)
92 {
93 return CmdHandler::ccOnlyResponse(request, rc);
94 }
vkaverapa6575b82019-04-03 05:33:52 -050095
96 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060097}
98
Deepak Kodihallibc669f12019-11-28 08:52:07 -060099Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600100{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600101 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600102 Type type;
103
vkaverapa6575b82019-04-03 05:33:52 -0500104 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
105 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600106
Zahed Hossain223a73d2019-07-04 12:46:18 -0500107 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -0500108
109 if (rc != PLDM_SUCCESS)
110 {
George Liufb8611d2019-12-06 10:14:15 +0800111 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500112 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600113
114 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600115 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600116 if (capabilities.find(type) == capabilities.end())
117 {
George Liufb8611d2019-12-06 10:14:15 +0800118 return CmdHandler::ccOnlyResponse(request,
119 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600120 }
121
122 for (const auto& cmd : capabilities.at(type))
123 {
124 auto index = cmd / 8;
125 // <Type Number> = <Array Index> * 8 + <bit position>
126 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600127 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600128 }
129
George Liufb8611d2019-12-06 10:14:15 +0800130 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
131 cmds.data(), responsePtr);
132 if (rc != PLDM_SUCCESS)
133 {
134 return ccOnlyResponse(request, rc);
135 }
vkaverapa6575b82019-04-03 05:33:52 -0500136
137 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600138}
139
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600140Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600141{
142 uint32_t transferHandle;
143 Type type;
144 uint8_t transferFlag;
145
vkaverapa6575b82019-04-03 05:33:52 -0500146 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
147 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600148
Zahed Hossain223a73d2019-07-04 12:46:18 -0500149 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
150 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500151
152 if (rc != PLDM_SUCCESS)
153 {
George Liufb8611d2019-12-06 10:14:15 +0800154 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500155 }
Sampa Misra432e1872019-02-13 03:49:43 -0600156
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600157 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600158 auto search = versions.find(type);
159
160 if (search == versions.end())
161 {
George Liufb8611d2019-12-06 10:14:15 +0800162 return CmdHandler::ccOnlyResponse(request,
163 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600164 }
165
166 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800167 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
168 PLDM_START_AND_END, &version,
169 sizeof(pldm_version), responsePtr);
170 if (rc != PLDM_SUCCESS)
171 {
172 return ccOnlyResponse(request, rc);
173 }
vkaverapa6575b82019-04-03 05:33:52 -0500174
175 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600176}
177
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500178void Handler::processSetEventReceiver(
179 sdeventplus::source::EventBase& /*source */)
180{
181 survEvent.reset();
182 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
183 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
184 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930185 auto instanceId = instanceIdDb.next(eid);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500186 uint8_t eventMessageGlobalEnable =
187 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
188 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
189 uint8_t eventReceiverAddressInfo = pldm::responder::pdr::BmcMctpEid;
190 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
191
192 auto rc = encode_set_event_receiver_req(
193 instanceId, eventMessageGlobalEnable, transportProtocolType,
194 eventReceiverAddressInfo, heartbeatTimer, request);
195 if (rc != PLDM_SUCCESS)
196 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930197 instanceIdDb.free(eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600198 error("Failed to encode_set_event_receiver_req, rc = {RC}", "RC",
199 lg2::hex, rc);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500200 return;
201 }
202
Patrick Williams6da4f912023-05-10 07:50:53 -0500203 auto processSetEventReceiverResponse =
204 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500205 if (response == nullptr || !respMsgLen)
206 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600207 error("Failed to receive response for setEventReceiver command");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500208 return;
209 }
210
211 uint8_t completionCode{};
212 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
213 &completionCode);
214 if (rc || completionCode)
215 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600216 error(
217 "Failed to decode setEventReceiver command response, rc = {RC}, cc = {CC}",
218 "RC", rc, "CC", (unsigned)completionCode);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500219 pldm::utils::reportError(
220 "xyz.openbmc_project.bmc.pldm.InternalFailure");
221 }
222 };
223 rc = handler->registerRequest(
224 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
225 std::move(requestMsg), std::move(processSetEventReceiverResponse));
226
227 if (rc != PLDM_SUCCESS)
228 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600229 error("Failed to send the setEventReceiver request");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500230 }
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500231
232 if (oemPlatformHandler)
233 {
234 oemPlatformHandler->countSetEventReceiver();
235 oemPlatformHandler->checkAndDisableWatchDog();
236 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500237}
238
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600239Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800240{
John Wang5c4f80d2019-07-29 11:12:18 +0800241 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
242 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600243 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS,
244 TERMINUS_ID, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800245 if (rc != PLDM_SUCCESS)
246 {
247 return ccOnlyResponse(request, rc);
248 }
John Wang5c4f80d2019-07-29 11:12:18 +0800249
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500250 survEvent = std::make_unique<sdeventplus::source::Defer>(
251 event, std::bind_front(&Handler::processSetEventReceiver, this));
252
John Wang5c4f80d2019-07-29 11:12:18 +0800253 return response;
254}
255
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600256} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600257} // namespace responder
258} // namespace pldm