blob: f0555d7ae8ca8c3826d9c58c6cda626c8bd9548c [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#pragma once
2
Deepak Kodihalli4c164b02020-03-07 03:23:31 -06003#include "config.h"
4
Deepak Kodihallibc669f12019-11-28 08:52:07 -06005#include "handler.hpp"
George Liu83409572019-12-24 18:42:54 +08006#include "utils.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -06007
Deepak Kodihalli15211b42019-12-14 02:24:49 -06008#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05309#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060010#include <sys/stat.h>
11#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053012#include <unistd.h>
13
14#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060015#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050016#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053017
18#include "libpldm/base.h"
Deepak Kodihallida87ec62019-07-02 01:01:16 -050019#include "oem/ibm/libpldm/file_io.h"
George Liu89aad712020-03-12 13:34:51 +080020#include "oem/ibm/libpldm/host.h"
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053021
22namespace pldm
23{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053024namespace responder
25{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053026namespace dma
27{
28
29// The minimum data size of dma transfer in bytes
30constexpr uint32_t minSize = 16;
31
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060032constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053033
34namespace fs = std::filesystem;
35
36/**
37 * @class DMA
38 *
39 * Expose API to initiate transfer of data by DMA
40 *
41 * This class only exposes the public API transferDataHost to transfer data
42 * between BMC and host using DMA. This allows for mocking the transferDataHost
43 * for unit testing purposes.
44 */
45class DMA
46{
47 public:
48 /** @brief API to transfer data between BMC and host using DMA
49 *
50 * @param[in] path - pathname of the file to transfer data from or to
51 * @param[in] offset - offset in the file
52 * @param[in] length - length of the data to transfer
53 * @param[in] address - DMA address on the host
54 * @param[in] upstream - indicates direction of the transfer; true indicates
55 * transfer to the host
56 *
57 * @return returns 0 on success, negative errno on failure
58 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060059 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053060 uint64_t address, bool upstream);
61};
62
63/** @brief Transfer the data between BMC and host using DMA.
64 *
65 * There is a max size for each DMA operation, transferAll API abstracts this
66 * and the requested length is broken down into multiple DMA operations if the
67 * length exceed max size.
68 *
69 * @tparam[in] T - DMA interface type
70 * @param[in] intf - interface passed to invoke DMA transfer
71 * @param[in] command - PLDM command
72 * @param[in] path - pathname of the file to transfer data from or to
73 * @param[in] offset - offset in the file
74 * @param[in] length - length of the data to transfer
75 * @param[in] address - DMA address on the host
76 * @param[in] upstream - indicates direction of the transfer; true indicates
77 * transfer to the host
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053078 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053079 * @return PLDM response message
80 */
81
82template <class DMAInterface>
83Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
84 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053085 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053086{
87 uint32_t origLength = length;
88 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
89 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
90
Deepak Kodihalli15211b42019-12-14 02:24:49 -060091 int flags{};
92 if (upstream)
93 {
94 flags = O_RDONLY;
95 }
96 else if (fs::exists(path))
97 {
98 flags = O_RDWR;
99 }
100 else
101 {
102 flags = O_WRONLY;
103 }
104 int file = open(path.string().c_str(), flags);
105 if (file == -1)
106 {
107 std::cerr << "File does not exist, path = " << path.string() << "\n";
108 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
109 responsePtr);
110 return response;
111 }
George Liu83409572019-12-24 18:42:54 +0800112 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600113
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530114 while (length > dma::maxSize)
115 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600116 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530117 upstream);
118 if (rc < 0)
119 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530120 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
121 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530122 return response;
123 }
124
125 offset += dma::maxSize;
126 length -= dma::maxSize;
127 address += dma::maxSize;
128 }
129
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600130 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530131 if (rc < 0)
132 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530133 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
134 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530135 return response;
136 }
137
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530138 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530139 responsePtr);
140 return response;
141}
142
143} // namespace dma
144
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600145namespace oem_ibm
146{
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600147class Handler : public CmdHandler
148{
149 public:
150 Handler()
151 {
152 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
153 [this](const pldm_msg* request, size_t payloadLength) {
154 return this->readFileIntoMemory(request,
155 payloadLength);
156 });
157 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
158 [this](const pldm_msg* request, size_t payloadLength) {
159 return this->writeFileFromMemory(request,
160 payloadLength);
161 });
162 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
163 [this](const pldm_msg* request, size_t payloadLength) {
164 return this->writeFileByTypeFromMemory(
165 request, payloadLength);
166 });
167 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
168 [this](const pldm_msg* request, size_t payloadLength) {
169 return this->readFileByTypeIntoMemory(
170 request, payloadLength);
171 });
172 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
173 size_t payloadLength) {
174 return this->readFileByType(request, payloadLength);
175 });
176 handlers.emplace(PLDM_GET_FILE_TABLE,
177 [this](const pldm_msg* request, size_t payloadLength) {
178 return this->getFileTable(request, payloadLength);
179 });
180 handlers.emplace(PLDM_READ_FILE,
181 [this](const pldm_msg* request, size_t payloadLength) {
182 return this->readFile(request, payloadLength);
183 });
184 handlers.emplace(PLDM_WRITE_FILE,
185 [this](const pldm_msg* request, size_t payloadLength) {
186 return this->writeFile(request, payloadLength);
187 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600188 handlers.emplace(PLDM_FILE_ACK,
189 [this](const pldm_msg* request, size_t payloadLength) {
190 return this->fileAck(request, payloadLength);
191 });
George Liu89aad712020-03-12 13:34:51 +0800192 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
193 [this](const pldm_msg* request, size_t payloadLength) {
194 return this->getAlertStatus(request,
195 payloadLength);
196 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600197 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530198
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600199 /** @brief Handler for readFileIntoMemory command
200 *
201 * @param[in] request - pointer to PLDM request payload
202 * @param[in] payloadLength - length of the message
203 *
204 * @return PLDM response message
205 */
206 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500207
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600208 /** @brief Handler for writeFileIntoMemory command
209 *
210 * @param[in] request - pointer to PLDM request payload
211 * @param[in] payloadLength - length of the message
212 *
213 * @return PLDM response message
214 */
215 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500216
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600217 /** @brief Handler for writeFileByTypeFromMemory command
218 *
219 * @param[in] request - pointer to PLDM request payload
220 * @param[in] payloadLength - length of the message
221 *
222 * @return PLDM response message
223 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600224
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600225 Response writeFileByTypeFromMemory(const pldm_msg* request,
226 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600227
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600228 /** @brief Handler for readFileByTypeIntoMemory command
229 *
230 * @param[in] request - pointer to PLDM request payload
231 * @param[in] payloadLength - length of the message
232 *
233 * @return PLDM response message
234 */
235 Response readFileByTypeIntoMemory(const pldm_msg* request,
236 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500237
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600238 /** @brief Handler for readFileByType command
239 *
240 * @param[in] request - pointer to PLDM request payload
241 * @param[in] payloadLength - length of the message
242 *
243 * @return PLDM response message
244 */
245 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500246
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600247 /** @brief Handler for GetFileTable command
248 *
249 * @param[in] request - pointer to PLDM request payload
250 * @param[in] payloadLength - length of the message payload
251 *
252 * @return PLDM response message
253 */
254 Response getFileTable(const pldm_msg* request, size_t payloadLength);
255
256 /** @brief Handler for readFile command
257 *
258 * @param[in] request - PLDM request msg
259 * @param[in] payloadLength - length of the message payload
260 *
261 * @return PLDM response message
262 */
263 Response readFile(const pldm_msg* request, size_t payloadLength);
264
265 /** @brief Handler for writeFile command
266 *
267 * @param[in] request - PLDM request msg
268 * @param[in] payloadLength - length of the message payload
269 *
270 * @return PLDM response message
271 */
272 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600273
274 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800275
276 /** @brief Handler for getAlertStatus command
277 *
278 * @param[in] request - PLDM request msg
279 * @param[in] payloadLength - length of the message payload
280 *
281 * @return PLDM response message
282 */
283 Response getAlertStatus(const pldm_msg* request, size_t payloadLength);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600284};
285
286} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530287} // namespace responder
288} // namespace pldm