blob: be78718c972bf6f3357216fa8bf1a35754d8f9a7 [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
22#include <libpldm/base.h>
23#include <libpldm/platform.h>
24#include <unistd.h>
25
26#include <fstream>
27#include <phosphor-logging/elog-errors.hpp>
28#include <phosphor-logging/log.hpp>
29#include <sdbusplus/bus.hpp>
30
31namespace phosphor
32{
33namespace dump
34{
Dhruvaraj Subhashchandran59642e22020-03-19 03:37:44 -050035namespace host
36{
37/**
38 * @brief Initiate offload of the dump with provided id
39 *
40 * @param[in] id - The Dump Source ID.
41 *
42 */
43void requestOffload(uint32_t id)
44{
45 pldm::requestOffload(id);
46}
47} // namespace host
48
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060049namespace pldm
50{
51
52using namespace phosphor::logging;
53
54constexpr auto eidPath = "/usr/share/pldm/host_eid";
55constexpr mctp_eid_t defaultEIDValue = 9;
56
57using InternalFailure =
58 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
59
60void closeFD(int fd)
61{
62 if (fd >= 0)
63 {
64 close(fd);
65 }
66}
67
68mctp_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");
76 elog<InternalFailure>();
77 }
78 else
79 {
80 std::string eid;
81 eidFile >> eid;
82 if (!eid.empty())
83 {
84 eid = strtol(eid.c_str(), nullptr, 10);
85 }
86 else
87 {
88 log<level::ERR>("EID file was empty");
89 elog<InternalFailure>();
90 }
91 }
92
93 return eid;
94}
95
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060096void requestOffload(uint32_t id)
97{
98 uint16_t effecterId = 0x05; // TODO PhyP temporary Hardcoded value.
99
100 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(id) +
101 sizeof(uint8_t)>
102 requestMsg{};
103 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
104
105 std::array<uint8_t, sizeof(id)> effecterValue{};
106
107 memcpy(effecterValue.data(), &id, sizeof(id));
108
109 mctp_eid_t eid = readEID();
110
111 auto instanceID = getPLDMInstanceID(eid);
112
113 auto rc = encode_set_numeric_effecter_value_req(
114 instanceID, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
115 effecterValue.data(), request,
116 requestMsg.size() - sizeof(pldm_msg_hdr));
117
118 if (rc != PLDM_SUCCESS)
119 {
120 log<level::ERR>("Message encode failure. ", entry("RC=%d", rc));
121 elog<InternalFailure>();
122 }
123
124 uint8_t* responseMsg = nullptr;
125 size_t responseMsgSize{};
126
Ramesh Iyyar3af5c322020-12-04 00:38:42 -0600127 auto fd = openPLDM();
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600128
129 rc = pldm_send_recv(eid, fd, requestMsg.data(), requestMsg.size(),
130 &responseMsg, &responseMsgSize);
131 if (rc < 0)
132 {
133 closeFD(fd);
134 auto e = errno;
135 log<level::ERR>("pldm_send failed", entry("RC=%d", rc),
136 entry("ERRNO=%d", e));
137 elog<InternalFailure>();
138 }
139 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg);
140 log<level::INFO>(
141 "Done. PLDM message",
142 entry("RC=%d", static_cast<uint16_t>(response->payload[0])));
143
144 closeFD(fd);
145}
146
Jayanth Othayothd31be2c2020-02-04 02:56:45 -0600147} // namespace pldm
148} // namespace dump
149} // namespace phosphor