blob: a79c677de958499f85b3df3cb07491cf39015d75 [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"
Jayashankar Padathdb124362021-01-28 21:12:34 -060010#include "oem/ibm/requester/dbus_to_file_handler.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -050011#include "oem_ibm_handler.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050012#include "pldmd/handler.hpp"
Deepak Kodihallibc669f12019-11-28 08:52:07 -060013
Deepak Kodihalli15211b42019-12-14 02:24:49 -060014#include <fcntl.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053015#include <stdint.h>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060016#include <sys/stat.h>
17#include <sys/types.h>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053018#include <unistd.h>
19
20#include <filesystem>
Deepak Kodihalli15211b42019-12-14 02:24:49 -060021#include <iostream>
Priyanga8b976652019-06-27 11:30:33 -050022#include <vector>
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053023
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053024namespace pldm
25{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053026namespace responder
27{
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053028namespace dma
29{
30
31// The minimum data size of dma transfer in bytes
32constexpr uint32_t minSize = 16;
33
Deepak Kodihalli4c164b02020-03-07 03:23:31 -060034constexpr size_t maxSize = DMA_MAXSIZE;
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053035
36namespace fs = std::filesystem;
37
38/**
39 * @class DMA
40 *
41 * Expose API to initiate transfer of data by DMA
42 *
43 * This class only exposes the public API transferDataHost to transfer data
44 * between BMC and host using DMA. This allows for mocking the transferDataHost
45 * for unit testing purposes.
46 */
47class DMA
48{
49 public:
50 /** @brief API to transfer data between BMC and host using DMA
51 *
52 * @param[in] path - pathname of the file to transfer data from or to
53 * @param[in] offset - offset in the file
54 * @param[in] length - length of the data to transfer
55 * @param[in] address - DMA address on the host
56 * @param[in] upstream - indicates direction of the transfer; true indicates
57 * transfer to the host
58 *
59 * @return returns 0 on success, negative errno on failure
60 */
Deepak Kodihalli15211b42019-12-14 02:24:49 -060061 int transferDataHost(int fd, uint32_t offset, uint32_t length,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053062 uint64_t address, bool upstream);
Ravi Tejace1c96f2020-10-05 23:13:01 -050063
64 /** @brief API to transfer data on to unix socket from host using DMA
65 *
66 * @param[in] path - pathname of the file to transfer data from or to
67 * @param[in] length - length of the data to transfer
68 * @param[in] address - DMA address on the host
69 *
70 * @return returns 0 on success, negative errno on failure
71 */
72 int transferHostDataToSocket(int fd, uint32_t length, uint64_t address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053073};
74
75/** @brief Transfer the data between BMC and host using DMA.
76 *
77 * There is a max size for each DMA operation, transferAll API abstracts this
78 * and the requested length is broken down into multiple DMA operations if the
79 * length exceed max size.
80 *
81 * @tparam[in] T - DMA interface type
82 * @param[in] intf - interface passed to invoke DMA transfer
83 * @param[in] command - PLDM command
84 * @param[in] path - pathname of the file to transfer data from or to
85 * @param[in] offset - offset in the file
86 * @param[in] length - length of the data to transfer
87 * @param[in] address - DMA address on the host
88 * @param[in] upstream - indicates direction of the transfer; true indicates
89 * transfer to the host
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053090 * @param[in] instanceId - Message's instance id
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053091 * @return PLDM response message
92 */
93
94template <class DMAInterface>
95Response transferAll(DMAInterface* intf, uint8_t command, fs::path& path,
96 uint32_t offset, uint32_t length, uint64_t address,
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +053097 bool upstream, uint8_t instanceId)
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053098{
99 uint32_t origLength = length;
100 Response response(sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES, 0);
101 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
102
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600103 int flags{};
104 if (upstream)
105 {
106 flags = O_RDONLY;
107 }
108 else if (fs::exists(path))
109 {
110 flags = O_RDWR;
111 }
112 else
113 {
114 flags = O_WRONLY;
115 }
116 int file = open(path.string().c_str(), flags);
117 if (file == -1)
118 {
119 std::cerr << "File does not exist, path = " << path.string() << "\n";
120 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
121 responsePtr);
122 return response;
123 }
George Liu83409572019-12-24 18:42:54 +0800124 pldm::utils::CustomFD fd(file);
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600125
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530126 while (length > dma::maxSize)
127 {
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600128 auto rc = intf->transferDataHost(fd(), offset, dma::maxSize, address,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530129 upstream);
130 if (rc < 0)
131 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530132 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
133 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530134 return response;
135 }
136
137 offset += dma::maxSize;
138 length -= dma::maxSize;
139 address += dma::maxSize;
140 }
141
Deepak Kodihalli15211b42019-12-14 02:24:49 -0600142 auto rc = intf->transferDataHost(fd(), offset, length, address, upstream);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530143 if (rc < 0)
144 {
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530145 encode_rw_file_memory_resp(instanceId, command, PLDM_ERROR, 0,
146 responsePtr);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530147 return response;
148 }
149
Jinu Joy Thomas33705fd2019-07-02 16:03:05 +0530150 encode_rw_file_memory_resp(instanceId, command, PLDM_SUCCESS, origLength,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530151 responsePtr);
152 return response;
153}
154
155} // namespace dma
156
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600157namespace oem_ibm
158{
Jayashankar Padathdb124362021-01-28 21:12:34 -0600159static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump/resource/entry/";
160static constexpr auto resDumpEntry = "com.ibm.Dump.Entry.Resource";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600161class Handler : public CmdHandler
162{
163 public:
Jayashankar Padathdb124362021-01-28 21:12:34 -0600164 Handler(oem_platform::Handler* oemPlatformHandler, int hostSockFd,
165 uint8_t hostEid, dbus_api::Requester* dbusImplReqester) :
166 oemPlatformHandler(oemPlatformHandler),
167 hostSockFd(hostSockFd), hostEid(hostEid),
168 dbusImplReqester(dbusImplReqester)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600169 {
170 handlers.emplace(PLDM_READ_FILE_INTO_MEMORY,
171 [this](const pldm_msg* request, size_t payloadLength) {
172 return this->readFileIntoMemory(request,
173 payloadLength);
174 });
175 handlers.emplace(PLDM_WRITE_FILE_FROM_MEMORY,
176 [this](const pldm_msg* request, size_t payloadLength) {
177 return this->writeFileFromMemory(request,
178 payloadLength);
179 });
180 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY,
181 [this](const pldm_msg* request, size_t payloadLength) {
182 return this->writeFileByTypeFromMemory(
183 request, payloadLength);
184 });
185 handlers.emplace(PLDM_READ_FILE_BY_TYPE_INTO_MEMORY,
186 [this](const pldm_msg* request, size_t payloadLength) {
187 return this->readFileByTypeIntoMemory(
188 request, payloadLength);
189 });
190 handlers.emplace(PLDM_READ_FILE_BY_TYPE, [this](const pldm_msg* request,
191 size_t payloadLength) {
192 return this->readFileByType(request, payloadLength);
193 });
Sampa Misra18967162020-01-14 02:31:41 -0600194 handlers.emplace(PLDM_WRITE_FILE_BY_TYPE,
195 [this](const pldm_msg* request, size_t payloadLength) {
196 return this->writeFileByType(request,
197 payloadLength);
198 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600199 handlers.emplace(PLDM_GET_FILE_TABLE,
200 [this](const pldm_msg* request, size_t payloadLength) {
201 return this->getFileTable(request, payloadLength);
202 });
203 handlers.emplace(PLDM_READ_FILE,
204 [this](const pldm_msg* request, size_t payloadLength) {
205 return this->readFile(request, payloadLength);
206 });
207 handlers.emplace(PLDM_WRITE_FILE,
208 [this](const pldm_msg* request, size_t payloadLength) {
209 return this->writeFile(request, payloadLength);
210 });
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600211 handlers.emplace(PLDM_FILE_ACK,
212 [this](const pldm_msg* request, size_t payloadLength) {
213 return this->fileAck(request, payloadLength);
214 });
George Liu89aad712020-03-12 13:34:51 +0800215 handlers.emplace(PLDM_HOST_GET_ALERT_STATUS,
216 [this](const pldm_msg* request, size_t payloadLength) {
217 return this->getAlertStatus(request,
218 payloadLength);
219 });
Sampa Misra18967162020-01-14 02:31:41 -0600220 handlers.emplace(PLDM_NEW_FILE_AVAILABLE,
221 [this](const pldm_msg* request, size_t payloadLength) {
222 return this->newFileAvailable(request,
223 payloadLength);
224 });
Jayashankar Padathdb124362021-01-28 21:12:34 -0600225
226 resDumpMatcher = std::make_unique<sdbusplus::bus::match::match>(
227 pldm::utils::DBusHandler::getBus(),
228 sdbusplus::bus::match::rules::interfacesAdded() +
229 sdbusplus::bus::match::rules::argNpath(0, dumpObjPath),
230 [hostSockFd, hostEid,
231 dbusImplReqester](sdbusplus::message::message& msg) {
232 std::map<
233 std::string,
234 std::map<std::string, std::variant<std::string, uint32_t>>>
235 interfaces;
236 sdbusplus::message::object_path path;
237 msg.read(path, interfaces);
238 std::string vspstring;
239 std::string password;
240
241 for (auto& interface : interfaces)
242 {
243 if (interface.first == resDumpEntry)
244 {
245 for (const auto& property : interface.second)
246 {
247 if (property.first == "VSPString")
248 {
249 vspstring =
250 std::get<std::string>(property.second);
251 }
252 else if (property.first == "Password")
253 {
254 password =
255 std::get<std::string>(property.second);
256 }
257 }
258 auto dbusToFileHandler = std::make_unique<
259 pldm::requester::oem_ibm::DbusToFileHandler>(
260 hostSockFd, hostEid, dbusImplReqester, path);
261 dbusToFileHandler->processNewResourceDump(vspstring,
262 password);
263 break;
264 }
265 }
266 });
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600267 }
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530268
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600269 /** @brief Handler for readFileIntoMemory command
270 *
271 * @param[in] request - pointer to PLDM request payload
272 * @param[in] payloadLength - length of the message
273 *
274 * @return PLDM response message
275 */
276 Response readFileIntoMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500277
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600278 /** @brief Handler for writeFileIntoMemory command
279 *
280 * @param[in] request - pointer to PLDM request payload
281 * @param[in] payloadLength - length of the message
282 *
283 * @return PLDM response message
284 */
285 Response writeFileFromMemory(const pldm_msg* request, size_t payloadLength);
Sampa Misra854e61f2019-08-22 04:36:47 -0500286
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600287 /** @brief Handler for writeFileByTypeFromMemory command
288 *
289 * @param[in] request - pointer to PLDM request payload
290 * @param[in] payloadLength - length of the message
291 *
292 * @return PLDM response message
293 */
Deepak Kodihallif6d3a832019-11-19 07:00:29 -0600294
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600295 Response writeFileByTypeFromMemory(const pldm_msg* request,
296 size_t payloadLength);
Deepak Kodihalli75e02f82019-11-20 02:51:05 -0600297
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600298 /** @brief Handler for readFileByTypeIntoMemory command
299 *
300 * @param[in] request - pointer to PLDM request payload
301 * @param[in] payloadLength - length of the message
302 *
303 * @return PLDM response message
304 */
305 Response readFileByTypeIntoMemory(const pldm_msg* request,
306 size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500307
Sampa Misra18967162020-01-14 02:31:41 -0600308 /** @brief Handler for writeFileByType command
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600309 *
310 * @param[in] request - pointer to PLDM request payload
311 * @param[in] payloadLength - length of the message
312 *
313 * @return PLDM response message
314 */
315 Response readFileByType(const pldm_msg* request, size_t payloadLength);
vkaverap5b914c32019-06-30 22:23:54 -0500316
Sampa Misra18967162020-01-14 02:31:41 -0600317 Response writeFileByType(const pldm_msg* request, size_t payloadLength);
318
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600319 /** @brief Handler for GetFileTable command
320 *
321 * @param[in] request - pointer to PLDM request payload
322 * @param[in] payloadLength - length of the message payload
323 *
324 * @return PLDM response message
325 */
326 Response getFileTable(const pldm_msg* request, size_t payloadLength);
327
328 /** @brief Handler for readFile command
329 *
330 * @param[in] request - PLDM request msg
331 * @param[in] payloadLength - length of the message payload
332 *
333 * @return PLDM response message
334 */
335 Response readFile(const pldm_msg* request, size_t payloadLength);
336
337 /** @brief Handler for writeFile command
338 *
339 * @param[in] request - PLDM request msg
340 * @param[in] payloadLength - length of the message payload
341 *
342 * @return PLDM response message
343 */
344 Response writeFile(const pldm_msg* request, size_t payloadLength);
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -0600345
346 Response fileAck(const pldm_msg* request, size_t payloadLength);
George Liu89aad712020-03-12 13:34:51 +0800347
348 /** @brief Handler for getAlertStatus command
349 *
350 * @param[in] request - PLDM request msg
351 * @param[in] payloadLength - length of the message payload
352 *
353 * @return PLDM response message
354 */
355 Response getAlertStatus(const pldm_msg* request, size_t payloadLength);
Sampa Misra18967162020-01-14 02:31:41 -0600356
357 /** @brief Handler for newFileAvailable command
358 *
359 * @param[in] request - PLDM request msg
360 * @param[in] payloadLength - length of the message payload
361 *
362 * @return PLDM response message
363 */
364 Response newFileAvailable(const pldm_msg* request, size_t payloadLength);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500365
366 private:
367 oem_platform::Handler* oemPlatformHandler;
Jayashankar Padathdb124362021-01-28 21:12:34 -0600368 int hostSockFd;
369 uint8_t hostEid;
370 dbus_api::Requester* dbusImplReqester;
371 using DBusInterfaceAdded = std::vector<std::pair<
372 std::string,
373 std::vector<std::pair<std::string, std::variant<std::string>>>>>;
374 std::unique_ptr<sdbusplus::bus::match::match>
375 resDumpMatcher; //!< Pointer to capture the interface added signal
376 //!< for new resource dump
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600377};
378
379} // namespace oem_ibm
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530380} // namespace responder
381} // namespace pldm