Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <filesystem> |
| 7 | |
| 8 | #include "libpldm/base.h" |
| 9 | #include "libpldm/file_io.h" |
| 10 | |
| 11 | namespace pldm |
| 12 | { |
| 13 | |
| 14 | namespace responder |
| 15 | { |
| 16 | |
| 17 | namespace utils |
| 18 | { |
| 19 | |
| 20 | /** @struct CustomFD |
| 21 | * |
| 22 | * RAII wrapper for file descriptor. |
| 23 | */ |
| 24 | struct CustomFD |
| 25 | { |
| 26 | CustomFD(const CustomFD&) = delete; |
| 27 | CustomFD& operator=(const CustomFD&) = delete; |
| 28 | CustomFD(CustomFD&&) = delete; |
| 29 | CustomFD& operator=(CustomFD&&) = delete; |
| 30 | |
| 31 | CustomFD(int fd) : fd(fd) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | ~CustomFD() |
| 36 | { |
| 37 | if (fd >= 0) |
| 38 | { |
| 39 | close(fd); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int operator()() const |
| 44 | { |
| 45 | return fd; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | int fd = -1; |
| 50 | }; |
| 51 | |
| 52 | } // namespace utils |
| 53 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 54 | namespace fs = std::filesystem; |
| 55 | |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 56 | namespace dma |
| 57 | { |
| 58 | |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 59 | // The minimum data size of dma transfer in bytes |
| 60 | constexpr uint32_t minSize = 16; |
| 61 | |
| 62 | // 16MB - 4096B (16773120 bytes) is the maximum data size of DMA transfer |
| 63 | constexpr size_t maxSize = (16 * 1024 * 1024) - 4096; |
| 64 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 65 | /** @brief API to transfer data between BMC and host using DMA |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 66 | * |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 67 | * @param[in] path - pathname of the file to transfer data from or to |
| 68 | * @param[in] offset - offset in the file |
| 69 | * @param[in] length - length of the data to transfer |
| 70 | * @param[in] address - DMA address on the host |
| 71 | * @param[in] upstream - indicates directon of the transfer; true indicates |
| 72 | * transfer to the host |
| 73 | * |
| 74 | * @return - returns 0 on success, negative errno on failure |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 75 | */ |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 76 | int transferDataHost(const fs::path& path, uint32_t offset, uint32_t length, |
| 77 | uint64_t address, bool upstream); |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 78 | } // namespace dma |
| 79 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 80 | /** @brief Transfer the data between BMC and host using DMA. |
| 81 | * |
| 82 | * There is a max size for each DMA operation, transferAll API abstracts this |
| 83 | * and the requested length is broken down into multiple DMA operations if the |
| 84 | * length exceed max size. |
| 85 | * |
| 86 | * @param[in] command - PLDM command |
| 87 | * @param[in] path - pathname of the file to transfer data from or to |
| 88 | * @param[in] offset - offset in the file |
| 89 | * @param[in] length - length of the data to transfer |
| 90 | * @param[in] address - DMA address on the host |
| 91 | * @param[in] upstream - indicates direction of the transfer; true indicates |
| 92 | * transfer to the host |
| 93 | * @param[out] response - response message location |
| 94 | */ |
| 95 | void transferAll(uint8_t command, fs::path& path, uint32_t offset, |
| 96 | uint32_t length, uint64_t address, bool upstream, |
| 97 | pldm_msg* response); |
| 98 | |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 99 | /** @brief Handler for readFileIntoMemory command |
| 100 | * |
| 101 | * @param[in] request - pointer to PLDM request payload |
| 102 | * @param[in] payloadLength - length of the message payload |
| 103 | * @param[out] response - response message location |
| 104 | */ |
| 105 | void readFileIntoMemory(const uint8_t* request, size_t payloadLength, |
| 106 | pldm_msg* response); |
| 107 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame^] | 108 | /** @brief Handler for writeFileIntoMemory command |
| 109 | * |
| 110 | * @param[in] request - pointer to PLDM request payload |
| 111 | * @param[in] payloadLength - length of the message payload |
| 112 | * @param[out] response - response message location |
| 113 | */ |
| 114 | void writeFileFromMemory(const uint8_t* request, size_t payloadLength, |
| 115 | pldm_msg* response); |
| 116 | |
Tom Joseph | f8329ac | 2019-04-11 22:13:22 +0530 | [diff] [blame] | 117 | } // namespace responder |
| 118 | } // namespace pldm |