Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 1 | /** |
| 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 Iyyar | 3af5c32 | 2020-12-04 00:38:42 -0600 | [diff] [blame^] | 16 | #include "pldm_oem_cmds.hpp" |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 17 | |
| 18 | #include "dump_utils.hpp" |
Ramesh Iyyar | 3af5c32 | 2020-12-04 00:38:42 -0600 | [diff] [blame^] | 19 | #include "pldm_utils.hpp" |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 20 | #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 | |
| 31 | namespace phosphor |
| 32 | { |
| 33 | namespace dump |
| 34 | { |
Dhruvaraj Subhashchandran | 59642e2 | 2020-03-19 03:37:44 -0500 | [diff] [blame] | 35 | namespace host |
| 36 | { |
| 37 | /** |
| 38 | * @brief Initiate offload of the dump with provided id |
| 39 | * |
| 40 | * @param[in] id - The Dump Source ID. |
| 41 | * |
| 42 | */ |
| 43 | void requestOffload(uint32_t id) |
| 44 | { |
| 45 | pldm::requestOffload(id); |
| 46 | } |
| 47 | } // namespace host |
| 48 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 49 | namespace pldm |
| 50 | { |
| 51 | |
| 52 | using namespace phosphor::logging; |
| 53 | |
| 54 | constexpr auto eidPath = "/usr/share/pldm/host_eid"; |
| 55 | constexpr mctp_eid_t defaultEIDValue = 9; |
| 56 | |
| 57 | using InternalFailure = |
| 58 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 59 | |
| 60 | void closeFD(int fd) |
| 61 | { |
| 62 | if (fd >= 0) |
| 63 | { |
| 64 | close(fd); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | mctp_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 Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 96 | void 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 Iyyar | 3af5c32 | 2020-12-04 00:38:42 -0600 | [diff] [blame^] | 127 | auto fd = openPLDM(); |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 128 | |
| 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 Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 147 | } // namespace pldm |
| 148 | } // namespace dump |
| 149 | } // namespace phosphor |