blob: e5bbf2fdd0b3de8e12a7399a82331950035c5f0b [file] [log] [blame]
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05001#include "config.h"
2
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06003#include "base.hpp"
George Liuc453e162022-12-21 17:16:23 +08004
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05005#include "common/utils.hpp"
6#include "libpldmresponder/pdr.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06007
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/base.h>
9#include <libpldm/bios.h>
10#include <libpldm/fru.h>
11#include <libpldm/platform.h>
12#include <libpldm/pldm.h>
13
Riya Dixit49cfb132023-03-02 04:26:53 -060014#include <phosphor-logging/lg2.hpp>
15
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060016#include <array>
Sampa Misra432e1872019-02-13 03:49:43 -060017#include <cstring>
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050018#include <iostream>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060019#include <map>
20#include <stdexcept>
21#include <vector>
22
Sridevi Ramesh45e52542020-04-03 07:54:18 -050023#ifdef OEM_IBM
George Liuc453e162022-12-21 17:16:23 +080024#include <libpldm/file_io.h>
25#include <libpldm/host.h>
Sridevi Ramesh45e52542020-04-03 07:54:18 -050026#endif
27
Riya Dixit49cfb132023-03-02 04:26:53 -060028PHOSPHOR_LOG2_USING;
29
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060030namespace pldm
31{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060032using Type = uint8_t;
33
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060034namespace responder
35{
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060036using Cmd = std::vector<uint8_t>;
37
38static const std::map<Type, Cmd> capabilities{
Zahed Hossain5e4e3832019-07-05 03:43:18 -050039 {PLDM_BASE,
John Wang5c4f80d2019-07-29 11:12:18 +080040 {PLDM_GET_TID, PLDM_GET_PLDM_VERSION, PLDM_GET_PLDM_TYPES,
41 PLDM_GET_PLDM_COMMANDS}},
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050042 {PLDM_PLATFORM,
43 {PLDM_GET_PDR, PLDM_SET_STATE_EFFECTER_STATES, PLDM_SET_EVENT_RECEIVER,
44 PLDM_GET_SENSOR_READING, PLDM_GET_STATE_SENSOR_READINGS,
45 PLDM_SET_NUMERIC_EFFECTER_VALUE, PLDM_GET_NUMERIC_EFFECTER_VALUE,
46 PLDM_PLATFORM_EVENT_MESSAGE}},
John Wang8721ed62019-12-05 14:44:43 +080047 {PLDM_BIOS,
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060048 {PLDM_GET_DATE_TIME, PLDM_SET_DATE_TIME, PLDM_GET_BIOS_TABLE,
Xiaochao Mad3cf57e2019-12-31 14:22:38 +080049 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE,
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050050 PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE, PLDM_SET_BIOS_TABLE}},
John Wang5bdde3a2020-06-16 15:02:33 +080051 {PLDM_FRU,
52 {PLDM_GET_FRU_RECORD_TABLE_METADATA, PLDM_GET_FRU_RECORD_TABLE,
53 PLDM_GET_FRU_RECORD_BY_OPTION}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050054#ifdef OEM_IBM
55 {PLDM_OEM,
56 {PLDM_HOST_GET_ALERT_STATUS, PLDM_GET_FILE_TABLE, PLDM_READ_FILE,
57 PLDM_WRITE_FILE, PLDM_READ_FILE_INTO_MEMORY, PLDM_WRITE_FILE_FROM_MEMORY,
58 PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
59 PLDM_NEW_FILE_AVAILABLE, PLDM_READ_FILE_BY_TYPE, PLDM_WRITE_FILE_BY_TYPE,
60 PLDM_FILE_ACK}},
61#endif
62};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060063
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060064static const std::map<Type, ver32_t> versions{
Manojkiran Edacb454672022-06-29 11:05:42 +053065 {PLDM_BASE, {0x00, 0xf0, 0xf0, 0xf1}},
66 {PLDM_PLATFORM, {0x00, 0xf0, 0xf2, 0xf1}},
67 {PLDM_BIOS, {0x00, 0xf0, 0xf0, 0xf1}},
68 {PLDM_FRU, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050069#ifdef OEM_IBM
Manojkiran Edacb454672022-06-29 11:05:42 +053070 {PLDM_OEM, {0x00, 0xf0, 0xf0, 0xf1}},
Sridevi Ramesh45e52542020-04-03 07:54:18 -050071#endif
Sampa Misra432e1872019-02-13 03:49:43 -060072};
73
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050074namespace base
75{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060076Response Handler::getPLDMTypes(const pldm_msg* request,
77 size_t /*payloadLength*/)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060078{
79 // DSP0240 has this as a bitfield8[N], where N = 0 to 7
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060080 std::array<bitfield8_t, 8> types{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060081 for (const auto& type : capabilities)
82 {
83 auto index = type.first / 8;
84 // <Type Number> = <Array Index> * 8 + <bit position>
85 auto bit = type.first - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060086 types[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060087 }
88
vkaverapa6575b82019-04-03 05:33:52 -050089 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES, 0);
90 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liufb8611d2019-12-06 10:14:15 +080091 auto rc = encode_get_types_resp(request->hdr.instance_id, PLDM_SUCCESS,
92 types.data(), responsePtr);
93 if (rc != PLDM_SUCCESS)
94 {
95 return CmdHandler::ccOnlyResponse(request, rc);
96 }
vkaverapa6575b82019-04-03 05:33:52 -050097
98 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060099}
100
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600101Response Handler::getPLDMCommands(const pldm_msg* request, size_t payloadLength)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600102{
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600103 ver32_t version{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600104 Type type;
105
vkaverapa6575b82019-04-03 05:33:52 -0500106 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES, 0);
107 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600108
Zahed Hossain223a73d2019-07-04 12:46:18 -0500109 auto rc = decode_get_commands_req(request, payloadLength, &type, &version);
vkaverapa6575b82019-04-03 05:33:52 -0500110
111 if (rc != PLDM_SUCCESS)
112 {
George Liufb8611d2019-12-06 10:14:15 +0800113 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500114 }
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600115
116 // DSP0240 has this as a bitfield8[N], where N = 0 to 31
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600117 std::array<bitfield8_t, 32> cmds{};
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600118 if (capabilities.find(type) == capabilities.end())
119 {
George Liufb8611d2019-12-06 10:14:15 +0800120 return CmdHandler::ccOnlyResponse(request,
121 PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600122 }
123
124 for (const auto& cmd : capabilities.at(type))
125 {
126 auto index = cmd / 8;
127 // <Type Number> = <Array Index> * 8 + <bit position>
128 auto bit = cmd - (index * 8);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600129 cmds[index].byte |= 1 << bit;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600130 }
131
George Liufb8611d2019-12-06 10:14:15 +0800132 rc = encode_get_commands_resp(request->hdr.instance_id, PLDM_SUCCESS,
133 cmds.data(), responsePtr);
134 if (rc != PLDM_SUCCESS)
135 {
136 return ccOnlyResponse(request, rc);
137 }
vkaverapa6575b82019-04-03 05:33:52 -0500138
139 return response;
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600140}
141
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600142Response Handler::getPLDMVersion(const pldm_msg* request, size_t payloadLength)
Sampa Misra432e1872019-02-13 03:49:43 -0600143{
144 uint32_t transferHandle;
145 Type type;
146 uint8_t transferFlag;
147
vkaverapa6575b82019-04-03 05:33:52 -0500148 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES, 0);
149 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -0600150
Zahed Hossain223a73d2019-07-04 12:46:18 -0500151 uint8_t rc = decode_get_version_req(request, payloadLength, &transferHandle,
152 &transferFlag, &type);
vkaverapa6575b82019-04-03 05:33:52 -0500153
154 if (rc != PLDM_SUCCESS)
155 {
George Liufb8611d2019-12-06 10:14:15 +0800156 return CmdHandler::ccOnlyResponse(request, rc);
vkaverapa6575b82019-04-03 05:33:52 -0500157 }
Sampa Misra432e1872019-02-13 03:49:43 -0600158
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600159 ver32_t version{};
Sampa Misra432e1872019-02-13 03:49:43 -0600160 auto search = versions.find(type);
161
162 if (search == versions.end())
163 {
George Liufb8611d2019-12-06 10:14:15 +0800164 return CmdHandler::ccOnlyResponse(request,
165 PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra432e1872019-02-13 03:49:43 -0600166 }
167
168 memcpy(&version, &(search->second), sizeof(version));
George Liufb8611d2019-12-06 10:14:15 +0800169 rc = encode_get_version_resp(request->hdr.instance_id, PLDM_SUCCESS, 0,
170 PLDM_START_AND_END, &version,
171 sizeof(pldm_version), responsePtr);
172 if (rc != PLDM_SUCCESS)
173 {
174 return ccOnlyResponse(request, rc);
175 }
vkaverapa6575b82019-04-03 05:33:52 -0500176
177 return response;
Sampa Misra432e1872019-02-13 03:49:43 -0600178}
179
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500180void Handler::processSetEventReceiver(
181 sdeventplus::source::EventBase& /*source */)
182{
183 survEvent.reset();
184 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
185 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
186 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930187 auto instanceId = instanceIdDb.next(eid);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500188 uint8_t eventMessageGlobalEnable =
189 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
190 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
191 uint8_t eventReceiverAddressInfo = pldm::responder::pdr::BmcMctpEid;
192 uint16_t heartbeatTimer = HEARTBEAT_TIMEOUT;
193
194 auto rc = encode_set_event_receiver_req(
195 instanceId, eventMessageGlobalEnable, transportProtocolType,
196 eventReceiverAddressInfo, heartbeatTimer, request);
197 if (rc != PLDM_SUCCESS)
198 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930199 instanceIdDb.free(eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600200 error("Failed to encode_set_event_receiver_req, rc = {RC}", "RC",
201 lg2::hex, rc);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500202 return;
203 }
204
Patrick Williams6da4f912023-05-10 07:50:53 -0500205 auto processSetEventReceiverResponse =
206 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500207 if (response == nullptr || !respMsgLen)
208 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600209 error("Failed to receive response for setEventReceiver command");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500210 return;
211 }
212
213 uint8_t completionCode{};
214 auto rc = decode_set_event_receiver_resp(response, respMsgLen,
215 &completionCode);
216 if (rc || completionCode)
217 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600218 error(
219 "Failed to decode setEventReceiver command response, rc = {RC}, cc = {CC}",
220 "RC", rc, "CC", (unsigned)completionCode);
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500221 pldm::utils::reportError(
222 "xyz.openbmc_project.bmc.pldm.InternalFailure");
223 }
224 };
225 rc = handler->registerRequest(
226 eid, instanceId, PLDM_PLATFORM, PLDM_SET_EVENT_RECEIVER,
227 std::move(requestMsg), std::move(processSetEventReceiverResponse));
228
229 if (rc != PLDM_SUCCESS)
230 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600231 error("Failed to send the setEventReceiver request");
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500232 }
Sagar Srinivas7f760b32021-05-12 07:46:56 -0500233
234 if (oemPlatformHandler)
235 {
236 oemPlatformHandler->countSetEventReceiver();
237 oemPlatformHandler->checkAndDisableWatchDog();
238 }
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500239}
240
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600241Response Handler::getTID(const pldm_msg* request, size_t /*payloadLength*/)
John Wang5c4f80d2019-07-29 11:12:18 +0800242{
John Wang5c4f80d2019-07-29 11:12:18 +0800243 Response response(sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES, 0);
244 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600245 auto rc = encode_get_tid_resp(request->hdr.instance_id, PLDM_SUCCESS,
246 TERMINUS_ID, responsePtr);
George Liufb8611d2019-12-06 10:14:15 +0800247 if (rc != PLDM_SUCCESS)
248 {
249 return ccOnlyResponse(request, rc);
250 }
John Wang5c4f80d2019-07-29 11:12:18 +0800251
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500252 survEvent = std::make_unique<sdeventplus::source::Defer>(
253 event, std::bind_front(&Handler::processSetEventReceiver, this));
254
John Wang5c4f80d2019-07-29 11:12:18 +0800255 return response;
256}
257
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600258} // namespace base
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600259} // namespace responder
260} // namespace pldm