Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 3 | #include <algorithm> |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 4 | #include <array> |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 5 | #include <sbe_interfaces.hpp> |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 6 | #include <sstream> |
| 7 | #include <stdexcept> |
| 8 | #include <vector> |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 9 | |
| 10 | namespace openpower |
| 11 | { |
| 12 | namespace sbe |
| 13 | { |
| 14 | |
| 15 | using sbe_word_t = uint32_t; |
| 16 | |
| 17 | namespace internal |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @brief Helper function for invokeSBEChipOperation(),to write to the SBE FIFO |
| 22 | * device and obtain the expected response .Internal device driver failures |
| 23 | * will be conveyed via respective exceptions. |
| 24 | * |
| 25 | * Exceptions thrown for: |
| 26 | * - Device driver internal failures |
| 27 | * |
| 28 | * @param[in] FIFO device path associated with SBE. |
| 29 | * @param[in] Command buffer to be written to the SBE FIFO |
| 30 | * @param[in] Length of command buffer |
| 31 | * @param[in] Expected response buffer length |
| 32 | * |
| 33 | * @return Response buffer returned by the SBE for the input command. |
| 34 | */ |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 35 | std::vector<sbe_word_t> writeToFifo(const char* devPath, |
| 36 | const sbe_word_t* cmdBuffer, |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 37 | size_t cmdBufLen, size_t respBufLen); |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 38 | |
| 39 | /** |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 40 | * @brief Helper function for invokeSBEChipOperation(), to parse and validate |
| 41 | * the data obtained from the SBE. Input buffer will be validated and on failure |
| 42 | * the FFDC content will be extracted and returned to the caller via |
| 43 | * respective exception. On success the input buffer will be modified to have |
| 44 | * only valid response data after removing the header content. |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 45 | * |
| 46 | * Exceptions thrown for: |
| 47 | * - SBE Internal failures |
| 48 | * |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 49 | * @param[in/out] On input - SBE data obtained from the SBE FIFO device. |
| 50 | * On output - Chip operation data after removing the response |
| 51 | * header. |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 52 | */ |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 53 | void parseResponse(std::vector<sbe_word_t>& sbeDataBuf); |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 54 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 55 | } // namespace internal |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @brief Interface to invoke a SBE chip operation.It calls internal API to |
| 59 | * write to the SBE FIFO and validates the data obtained by the SBE. It throws |
| 60 | * exception for any SBE internal failures. |
| 61 | * |
| 62 | * Runtime exceptions thrown for: |
| 63 | * - Device driver failures |
| 64 | * - SBE internal failures |
| 65 | * |
| 66 | * @param[in] FIFO device path associated with the SBE. |
| 67 | * @param[in] Request packet for the data to be read. |
| 68 | * @param[in] Data obtained by the SBE. |
| 69 | * @tparam S1 Length of request buffer to be send to SBE |
| 70 | * @tparam S2 Expected length of data from the SBE |
| 71 | */ |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 72 | template <size_t S1, size_t S2> |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 73 | inline void invokeSBEChipOperation(const char* devPath, |
| 74 | const std::array<sbe_word_t, S1>& request, |
| 75 | std::array<sbe_word_t, S2>& chipOpData) |
| 76 | { |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 77 | // Write and read from the FIFO device. |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 78 | auto sbeFifoResp = internal::writeToFifo(devPath, request.data(), |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 79 | request.size(), chipOpData.size()); |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 80 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 81 | // Parse the obtained data |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 82 | internal::parseResponse(sbeFifoResp); |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 83 | // Above interface would have stripped the SBE header content from the input |
| 84 | // response buffer. |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 85 | if (sbeFifoResp.size() > chipOpData.size()) |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 86 | { |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 87 | // TODO:use elog infrastructure |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 88 | std::ostringstream errMsg; |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 89 | errMsg << "Obtained chip operation response length (" |
| 90 | << sbeFifoResp.size() |
| 91 | << "from SBE is greater than maximum expected" |
| 92 | " length:" |
| 93 | << chipOpData.size(); |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 94 | |
| 95 | throw std::runtime_error(errMsg.str().c_str()); |
| 96 | } |
| 97 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 98 | // Move the contents of response buffer into the output buffer. |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 99 | std::move(sbeFifoResp.begin(), sbeFifoResp.end(), chipOpData.begin()); |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Patrick Venture | da79c9c | 2018-11-01 15:35:52 -0700 | [diff] [blame] | 102 | } // namespace sbe |
| 103 | } // namespace openpower |