blob: 9f6492cfd2631f374b38594af7bb77e6cacddab1 [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
George Liu6492f522020-06-16 10:34:05 +08005#include "libpldm/base.h"
6#include "oem/ibm/libpldm/file_io.h"
7#include "oem/ibm/libpldm/host.h"
8
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05009#include "common/utils.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050010#include "oem_ibm_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050011#include "pldmd/handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060012
Deepak Kodihalli15211b42019-12-14 02:24:49 -060013#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053014#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060015#include <sys/stat.h>
16#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053017#include <unistd.h>
18
19#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060020#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050021#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053022
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023namespace pldm
24{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053025namespace responder
26{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053027namespace dma
28{
29
30// The minimum data size of dma transfer in bytes
31constexpr uint32_t minSize = 16;
32
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060033constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053034
35namespace fs = std::filesystem;
36
37/**
38 * @class DMA
39 *
40 * Expose API to initiate transfer of data by DMA
41 *
42 * This class only exposes the public API transferDataHost to transfer data
43 * between BMC and host using DMA. This allows for mocking the transferDataHost
44 * for unit testing purposes.
45 */
46class DMA
47{
48 public:
49 /** @brief API to transfer data between BMC and host using DMA
50 *
51 * @param[in] path - pathname of the file to transfer data from or to
52 * @param[in] offset - offset in the file
53 * @param[in] length - length of the data to transfer
54 * @param[in] address - DMA address on the host
55 * @param[in] upstream - indicates direction of the transfer; true indicates
56 * transfer to the host
57 *
58 * @return returns 0 on success, negative errno on failure
59 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060060 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053061 uint64_t address, bool upstream);
Ravi Tejace1c96f2020-10-05 23:13:01 -050062
63 /** @brief API to transfer data on to unix socket from host using DMA
64 *
65 * @param[in] path - pathname of the file to transfer data from or to
66 * @param[in] length - length of the data to transfer
67 * @param[in] address - DMA address on the host
68 *
69 * @return returns 0 on success, negative errno on failure
70 */
71 int transferHostDataToSocket(int fd, uint32_t length, uint64_t address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053072};
73
74/** @brief Transfer the data between BMC and host using DMA.
75 *
76 * There is a max size for each DMA operation, transferAll API abstracts this
77 * and the requested length is broken down into multiple DMA operations if the
78 * length exceed max size.
79 *
80 * @tparam[in] T - DMA interface type
81 * @param[in] intf - interface passed to invoke DMA transfer
82 * @param[in] command - PLDM command
83 * @param[in] path - pathname of the file to transfer data from or to
84 * @param[in] offset - offset in the file
85 * @param[in] length - length of the data to transfer
86 * @param[in] address - DMA address on the host
87 * @param[in] upstream - indicates direction of the transfer; true indicates
88 * transfer to the host
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053089 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053090 * @return PLDM response message
91 */
92
93template <class DMAInterface>
94Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
95 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053096 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053097{
98 uint32_t origLength = length;
99 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
100 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
101
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600102 int flags{};
103 if (upstream)
104 {
105 flags = O_RDONLY;
106 }
107 else if (fs::exists(path))
108 {
109 flags = O_RDWR;
110 }
111 else
112 {
113 flags = O_WRONLY;
114 }
115 int file = open(path.string().c_str(), flags);
116 if (file == -1)
117 {
118 std::cerr << "File does not exist, path = " << path.string() << "\n";
119 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
120 responsePtr);
121 return response;
122 }
George Liu83409572019-12-24 18:42:54 +0800123 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600124
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530125 while (length > dma::maxSize)
126 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600127 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530128 upstream);
129 if (rc < 0)
130 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530131 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
132 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530133 return response;
134 }
135
136 offset += dma::maxSize;
137 length -= dma::maxSize;
138 address += dma::maxSize;
139 }
140
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600141 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530142 if (rc < 0)
143 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530144 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
145 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530146 return response;
147 }
148
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530149 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530150 responsePtr);
151 return response;
152}
153
154} // namespace dma
155
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600156namespace oem_ibm
157{
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600158class Handler : public CmdHandler
159{
160 public:
Sampa Misraaea5dde2020-08-31 08:33:47 -0500161 Handler(oem_platform::Handler* oemPlatformHandler) :
162 oemPlatformHandler(oemPlatformHandler)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600163 {
164 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
165 [this](const pldm_msg* request, size_t payloadLength) {
166 return this->readFileIntoMemory(request,
167 payloadLength);
168 });
169 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
170 [this](const pldm_msg* request, size_t payloadLength) {
171 return this->writeFileFromMemory(request,
172 payloadLength);
173 });
174 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
175 [this](const pldm_msg* request, size_t payloadLength) {
176 return this->writeFileByTypeFromMemory(
177 request, payloadLength);
178 });
179 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
180 [this](const pldm_msg* request, size_t payloadLength) {
181 return this->readFileByTypeIntoMemory(
182 request, payloadLength);
183 });
184 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
185 size_t payloadLength) {
186 return this->readFileByType(request, payloadLength);
187 });
Sampa Misra18967162020-01-14 02:31:41 -0600188 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
189 [this](const pldm_msg* request, size_t payloadLength) {
190 return this->writeFileByType(request,
191 payloadLength);
192 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600193 handlers.emplace(PLDM_GET_FILE_TABLE,
194 [this](const pldm_msg* request, size_t payloadLength) {
195 return this->getFileTable(request, payloadLength);
196 });
197 handlers.emplace(PLDM_READ_FILE,
198 [this](const pldm_msg* request, size_t payloadLength) {
199 return this->readFile(request, payloadLength);
200 });
201 handlers.emplace(PLDM_WRITE_FILE,
202 [this](const pldm_msg* request, size_t payloadLength) {
203 return this->writeFile(request, payloadLength);
204 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600205 handlers.emplace(PLDM_FILE_ACK,
206 [this](const pldm_msg* request, size_t payloadLength) {
207 return this->fileAck(request, payloadLength);
208 });
George Liu89aad712020-03-12 13:34:51 +0800209 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
210 [this](const pldm_msg* request, size_t payloadLength) {
211 return this->getAlertStatus(request,
212 payloadLength);
213 });
Sampa Misra18967162020-01-14 02:31:41 -0600214 handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
215 [this](const pldm_msg* request, size_t payloadLength) {
216 return this->newFileAvailable(request,
217 payloadLength);
218 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600219 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530220
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600221 /** @brief Handler for readFileIntoMemory command
222 *
223 * @param[in] request - pointer to PLDM request payload
224 * @param[in] payloadLength - length of the message
225 *
226 * @return PLDM response message
227 */
228 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500229
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600230 /** @brief Handler for writeFileIntoMemory command
231 *
232 * @param[in] request - pointer to PLDM request payload
233 * @param[in] payloadLength - length of the message
234 *
235 * @return PLDM response message
236 */
237 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500238
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600239 /** @brief Handler for writeFileByTypeFromMemory command
240 *
241 * @param[in] request - pointer to PLDM request payload
242 * @param[in] payloadLength - length of the message
243 *
244 * @return PLDM response message
245 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600246
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600247 Response writeFileByTypeFromMemory(const pldm_msg* request,
248 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600249
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600250 /** @brief Handler for readFileByTypeIntoMemory command
251 *
252 * @param[in] request - pointer to PLDM request payload
253 * @param[in] payloadLength - length of the message
254 *
255 * @return PLDM response message
256 */
257 Response readFileByTypeIntoMemory(const pldm_msg* request,
258 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500259
Sampa Misra18967162020-01-14 02:31:41 -0600260 /** @brief Handler for writeFileByType command
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600261 *
262 * @param[in] request - pointer to PLDM request payload
263 * @param[in] payloadLength - length of the message
264 *
265 * @return PLDM response message
266 */
267 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500268
Sampa Misra18967162020-01-14 02:31:41 -0600269 Response writeFileByType(const pldm_msg* request, size_t payloadLength);
270
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600271 /** @brief Handler for GetFileTable command
272 *
273 * @param[in] request - pointer to PLDM request payload
274 * @param[in] payloadLength - length of the message payload
275 *
276 * @return PLDM response message
277 */
278 Response getFileTable(const pldm_msg* request, size_t payloadLength);
279
280 /** @brief Handler for readFile command
281 *
282 * @param[in] request - PLDM request msg
283 * @param[in] payloadLength - length of the message payload
284 *
285 * @return PLDM response message
286 */
287 Response readFile(const pldm_msg* request, size_t payloadLength);
288
289 /** @brief Handler for writeFile command
290 *
291 * @param[in] request - PLDM request msg
292 * @param[in] payloadLength - length of the message payload
293 *
294 * @return PLDM response message
295 */
296 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600297
298 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800299
300 /** @brief Handler for getAlertStatus command
301 *
302 * @param[in] request - PLDM request msg
303 * @param[in] payloadLength - length of the message payload
304 *
305 * @return PLDM response message
306 */
307 Response getAlertStatus(const pldm_msg* request, size_t payloadLength);
Sampa Misra18967162020-01-14 02:31:41 -0600308
309 /** @brief Handler for newFileAvailable command
310 *
311 * @param[in] request - PLDM request msg
312 * @param[in] payloadLength - length of the message payload
313 *
314 * @return PLDM response message
315 */
316 Response newFileAvailable(const pldm_msg* request, size_t payloadLength);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500317
318 private:
319 oem_platform::Handler* oemPlatformHandler;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600320};
321
322} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530323} // namespace responder
324} // namespace pldm