blob: 1c9d7038f40b43cb5860777dbf5c599da6ca464e [file] [log] [blame]
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060016#include "pldm_oem_cmds.hpp"
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060017
18#include "dump_utils.hpp"
Ramesh Iyyar3af5c322020-12-04 00:38:42 -060019#include "pldm_utils.hpp"
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060020#include "xyz/openbmc_project/Common/error.hpp"
21
George Liu858fbb22021-07-01 12:25:44 +080022#include <fmt/core.h>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060023#include <libpldm/base.h>
Ramesh Iyyar22793862020-12-04 04:03:03 -060024#include <libpldm/file_io.h>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060025#include <libpldm/platform.h>
26#include <unistd.h>
27
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060028#include <phosphor-logging/elog-errors.hpp>
29#include <phosphor-logging/log.hpp>
30#include <sdbusplus/bus.hpp>
31
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050032#include <fstream>
33
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060034namespace phosphor
35{
36namespace dump
37{
Dhruvaraj Subhashchandran59642e22020-03-19 03:37:44 -050038namespace host
39{
40/**
41 * @brief Initiate offload of the dump with provided id
42 *
43 * @param[in] id - The Dump Source ID.
44 *
45 */
46void requestOffload(uint32_t id)
47{
48 pldm::requestOffload(id);
49}
Ramesh Iyyar22793862020-12-04 04:03:03 -060050
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -060051void requestDelete(uint32_t id, uint32_t dumpType)
Ramesh Iyyar22793862020-12-04 04:03:03 -060052{
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -060053 pldm::requestDelete(id, dumpType);
Ramesh Iyyar22793862020-12-04 04:03:03 -060054}
Dhruvaraj Subhashchandran59642e22020-03-19 03:37:44 -050055} // namespace host
56
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060057namespace pldm
58{
59
60using namespace phosphor::logging;
61
62constexpr auto eidPath = "/usr/share/pldm/host_eid";
63constexpr mctp_eid_t defaultEIDValue = 9;
64
Ramesh Iyyar22793862020-12-04 04:03:03 -060065using NotAllowed = sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
66using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060067
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060068mctp_eid_t readEID()
69{
70 mctp_eid_t eid = defaultEIDValue;
71
72 std::ifstream eidFile{eidPath};
73 if (!eidFile.good())
74 {
75 log<level::ERR>("Could not open host EID file");
Ramesh Iyyar22793862020-12-04 04:03:03 -060076 elog<NotAllowed>(Reason("Required host dump action via pldm is not "
77 "allowed due to mctp end point read failed"));
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060078 }
79 else
80 {
81 std::string eid;
82 eidFile >> eid;
83 if (!eid.empty())
84 {
85 eid = strtol(eid.c_str(), nullptr, 10);
86 }
87 else
88 {
89 log<level::ERR>("EID file was empty");
Ramesh Iyyar22793862020-12-04 04:03:03 -060090 elog<NotAllowed>(
91 Reason("Required host dump action via pldm is not "
92 "allowed due to mctp end point read failed"));
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060093 }
94 }
95
96 return eid;
97}
98
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060099void requestOffload(uint32_t id)
100{
101 uint16_t effecterId = 0x05; // TODO PhyP temporary Hardcoded value.
102
103 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(id) +
104 sizeof(uint8_t)>
105 requestMsg{};
106 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
107
108 std::array<uint8_t, sizeof(id)> effecterValue{};
109
110 memcpy(effecterValue.data(), &id, sizeof(id));
111
112 mctp_eid_t eid = readEID();
113
114 auto instanceID = getPLDMInstanceID(eid);
115
116 auto rc = encode_set_numeric_effecter_value_req(
117 instanceID, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
118 effecterValue.data(), request,
119 requestMsg.size() - sizeof(pldm_msg_hdr));
120
121 if (rc != PLDM_SUCCESS)
122 {
George Liu858fbb22021-07-01 12:25:44 +0800123 log<level::ERR>(
124 fmt::format("Message encode failure. RC({})", rc).c_str());
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600125 elog<NotAllowed>(Reason("Host dump offload via pldm is not "
Ramesh Iyyar22793862020-12-04 04:03:03 -0600126 "allowed due to encode failed"));
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600127 }
128
Ramesh Iyyar5765b1d2020-12-04 01:29:44 -0600129 CustomFd fd(openPLDM());
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600130
Dhruvaraj Subhashchandranf5e53852022-03-03 02:05:49 -0600131 log<level::INFO>(
132 fmt::format("Sending request to offload dump id({}), eid({})", id, eid)
133 .c_str());
134
135 rc = pldm_send(eid, fd(), requestMsg.data(), requestMsg.size());
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600136 if (rc < 0)
137 {
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600138 auto e = errno;
George Liu858fbb22021-07-01 12:25:44 +0800139 log<level::ERR>(
140 fmt::format("pldm_send failed, RC({}), errno({})", rc, e).c_str());
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600141 elog<NotAllowed>(Reason("Host dump offload via pldm is not "
Ramesh Iyyar22793862020-12-04 04:03:03 -0600142 "allowed due to fileack send failed"));
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600143 }
Dhruvaraj Subhashchandranf5e53852022-03-03 02:05:49 -0600144 log<level::INFO>(
145 fmt::format("Done. PLDM message, id({} )RC({})", id, rc).c_str());
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600146}
147
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600148void requestDelete(uint32_t dumpId, uint32_t dumpType)
Ramesh Iyyar22793862020-12-04 04:03:03 -0600149{
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600150 pldm_fileio_file_type pldmDumpType;
151 switch (dumpType)
152 {
153 case PLDM_FILE_TYPE_DUMP:
154 pldmDumpType = PLDM_FILE_TYPE_DUMP;
155 break;
Dhruvaraj Subhashchandran0c782d62021-03-24 13:27:13 -0500156 case PLDM_FILE_TYPE_RESOURCE_DUMP:
157 pldmDumpType = PLDM_FILE_TYPE_RESOURCE_DUMP;
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600158 break;
159 default:
160 throw std::runtime_error("Unknown pldm dump file-io type to delete "
161 "host dump");
162 }
Ramesh Iyyar22793862020-12-04 04:03:03 -0600163 const size_t pldmMsgHdrSize = sizeof(pldm_msg_hdr);
164 std::array<uint8_t, pldmMsgHdrSize + PLDM_FILE_ACK_REQ_BYTES> fileAckReqMsg;
165
166 mctp_eid_t mctpEndPointId = readEID();
167
168 auto pldmInstanceId = getPLDMInstanceID(mctpEndPointId);
169
Ramesh Iyyar22793862020-12-04 04:03:03 -0600170 // - PLDM_SUCCESS - To indicate dump was readed (offloaded) or user decided,
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600171 // no longer host dump is not required so, initiate deletion from
Ramesh Iyyar22793862020-12-04 04:03:03 -0600172 // host memory
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600173 int retCode =
174 encode_file_ack_req(pldmInstanceId, pldmDumpType, dumpId, PLDM_SUCCESS,
175 reinterpret_cast<pldm_msg*>(fileAckReqMsg.data()));
Ramesh Iyyar22793862020-12-04 04:03:03 -0600176
177 if (retCode != PLDM_SUCCESS)
178 {
George Liu858fbb22021-07-01 12:25:44 +0800179 log<level::ERR>(
180 fmt::format("Failed to encode pldm FileAck to delete host "
181 "dump,SRC_DUMP_ID({}), "
182 "PLDM_FILE_IO_TYPE({}),PLDM_RETURN_CODE({})",
183 dumpId, pldmDumpType, retCode)
184 .c_str());
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600185 elog<NotAllowed>(Reason("Host dump deletion via pldm is not "
Ramesh Iyyar22793862020-12-04 04:03:03 -0600186 "allowed due to encode fileack failed"));
187 }
188
Ramesh Iyyar22793862020-12-04 04:03:03 -0600189 CustomFd pldmFd(openPLDM());
190
Dhruvaraj Subhashchandranb5a75472022-02-03 03:52:01 -0600191 retCode = pldm_send(mctpEndPointId, pldmFd(), fileAckReqMsg.data(),
192 fileAckReqMsg.size());
Ramesh Iyyar22793862020-12-04 04:03:03 -0600193 if (retCode != PLDM_REQUESTER_SUCCESS)
194 {
195 auto errorNumber = errno;
George Liu858fbb22021-07-01 12:25:44 +0800196 log<level::ERR>(
197 fmt::format("Failed to send pldm FileAck to delete host dump, "
198 "SRC_DUMP_ID({}), PLDM_FILE_IO_TYPE({}), "
199 "PLDM_RETURN_CODE({}), ERRNO({}), ERRMSG({})",
200 dumpId, pldmDumpType, retCode, errorNumber,
201 strerror(errorNumber))
202 .c_str());
Dhruvaraj Subhashchandran4c63ce52020-12-18 02:07:22 -0600203 elog<NotAllowed>(Reason("Host dump deletion via pldm is not "
Ramesh Iyyar22793862020-12-04 04:03:03 -0600204 "allowed due to fileack send failed"));
205 }
206
George Liu858fbb22021-07-01 12:25:44 +0800207 log<level::INFO>(
Dhruvaraj Subhashchandranb5a75472022-02-03 03:52:01 -0600208 fmt::format("Sent request to host to delete the dump, SRC_DUMP_ID({})",
209 dumpId)
210 .c_str());
Ramesh Iyyar22793862020-12-04 04:03:03 -0600211}
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600212} // namespace pldm
213} // namespace dump
214} // namespace phosphor