blob: d90558aeca7053d65bccb056a3aada4619802b72 [file] [log] [blame]
Murulidhar Nataraju1adec022017-04-20 12:05:51 -05001#pragma once
2
Murulidhar Nataraju1adec022017-04-20 12:05:51 -05003#include <algorithm>
Patrick Ventureda79c9c2018-11-01 15:35:52 -07004#include <array>
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -05005#include <sbe_interfaces.hpp>
Patrick Ventureda79c9c2018-11-01 15:35:52 -07006#include <sstream>
7#include <stdexcept>
8#include <vector>
Murulidhar Nataraju1adec022017-04-20 12:05:51 -05009
10namespace openpower
11{
12namespace sbe
13{
14
15using sbe_word_t = uint32_t;
16
17namespace 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 Nataraju06a0c2c2017-07-11 08:55:33 -050035std::vector<sbe_word_t> writeToFifo(const char* devPath,
36 const sbe_word_t* cmdBuffer,
Patrick Ventureda79c9c2018-11-01 15:35:52 -070037 size_t cmdBufLen, size_t respBufLen);
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050038
39/**
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050040 * @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 Nataraju1adec022017-04-20 12:05:51 -050045 *
46 * Exceptions thrown for:
47 * - SBE Internal failures
48 *
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050049 * @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 Nataraju1adec022017-04-20 12:05:51 -050052 */
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050053void parseResponse(std::vector<sbe_word_t>& sbeDataBuf);
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050054
Patrick Ventureda79c9c2018-11-01 15:35:52 -070055} // namespace internal
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050056
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 Ventureda79c9c2018-11-01 15:35:52 -070072template <size_t S1, size_t S2>
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050073inline void invokeSBEChipOperation(const char* devPath,
74 const std::array<sbe_word_t, S1>& request,
75 std::array<sbe_word_t, S2>& chipOpData)
76{
Patrick Ventureda79c9c2018-11-01 15:35:52 -070077 // Write and read from the FIFO device.
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050078 auto sbeFifoResp = internal::writeToFifo(devPath, request.data(),
Patrick Ventureda79c9c2018-11-01 15:35:52 -070079 request.size(), chipOpData.size());
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050080
Patrick Ventureda79c9c2018-11-01 15:35:52 -070081 // Parse the obtained data
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050082 internal::parseResponse(sbeFifoResp);
Patrick Ventureda79c9c2018-11-01 15:35:52 -070083 // Above interface would have stripped the SBE header content from the input
84 // response buffer.
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050085 if (sbeFifoResp.size() > chipOpData.size())
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050086 {
Patrick Ventureda79c9c2018-11-01 15:35:52 -070087 // TODO:use elog infrastructure
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050088 std::ostringstream errMsg;
Patrick Ventureda79c9c2018-11-01 15:35:52 -070089 errMsg << "Obtained chip operation response length ("
90 << sbeFifoResp.size()
91 << "from SBE is greater than maximum expected"
92 " length:"
93 << chipOpData.size();
Murulidhar Nataraju1adec022017-04-20 12:05:51 -050094
95 throw std::runtime_error(errMsg.str().c_str());
96 }
97
Patrick Ventureda79c9c2018-11-01 15:35:52 -070098 // Move the contents of response buffer into the output buffer.
Murulidhar Nataraju06a0c2c2017-07-11 08:55:33 -050099 std::move(sbeFifoResp.begin(), sbeFifoResp.end(), chipOpData.begin());
Murulidhar Nataraju1adec022017-04-20 12:05:51 -0500100}
101
Patrick Ventureda79c9c2018-11-01 15:35:52 -0700102} // namespace sbe
103} // namespace openpower