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