1 #pragma once
2 
3 #include "occ_errors.hpp"
4 #include "utils.hpp"
5 
6 #include <org/open_power/OCC/PassThrough/server.hpp>
7 #include <sdbusplus/bus.hpp>
8 #include <sdbusplus/server/object.hpp>
9 
10 #include <string>
11 
12 namespace open_power
13 {
14 namespace occ
15 {
16 
17 // For waiting on signals
18 namespace sdbusRule = sdbusplus::bus::match::rules;
19 
20 enum class CmdType
21 {
22     POLL = 0x00,
23     CLEAR_ERROR_LOG = 0x12,
24     SET_MODE_AND_STATE = 0x20,
25     SET_CONFIG_DATA = 0x21,
26     SET_USER_PCAP = 0x22,
27     RESET_PREP = 0x25,
28     DEBUG_PASS_THROUGH = 0x40,
29     AME_PASS_THROUGH = 0x41,
30     GET_FIELD_DEBUG_DATA = 0x42,
31     MFG_TEST = 0x53
32 };
33 
34 enum class OccState
35 {
36     NO_CHANGE = 0x00,
37     STANDBY = 0x01,
38     OBSERVATION = 0x02,
39     ACTIVE = 0x03,
40     SAFE = 0x04,
41     CHARACTERIZATION = 0x05,
42 };
43 
44 enum class SysPwrMode
45 {
46     NO_CHANGE = 0,
47     DISABLE = 0x01,      // Disable / Static Base Frequencey
48     SFP = 0x03,          // Static Frequency Point (requires freqPt)
49     SAFE = 0x04,         // reported when system is in SAFE mode (not settable)
50     POWER_SAVING = 0x05, // Static Power Saving
51     DYNAMIC_PERF = 0x0A, // Dynamic / Balanced Performance
52     FFO = 0x0B,          // Fixed Frequency Override (requires freqPt)
53     MAX_PERF = 0x0C      // Maximum Performance
54 };
55 
56 // Only some of the SysPwrModes are currently supported and allowed to be set
57 #define VALID_POWER_MODE_SETTING(mode)                                         \
58     ((mode == SysPwrMode::DISABLE) || (mode == SysPwrMode::POWER_SAVING) ||    \
59      (mode == SysPwrMode::DYNAMIC_PERF) || (mode == SysPwrMode::MAX_PERF))
60 
61 enum class RspStatus
62 {
63     SUCCESS = 0x00,
64     CONDITIONAL_SUCCESS = 0x01,
65     INVALID_COMMAND = 0x11,
66     INVALID_COMMAND_LENGTH = 0x12,
67     INVALID_DATA_FIELD = 0x13,
68     CHECKSUM_FAILURE = 0x14,
69     INTERNAL_ERROR = 0x15,
70     PRESENT_STATE_PROHIBITS = 0x16,
71     COMMAND_IN_PROGRESS = 0xFF
72 };
73 
74 enum class CmdStatus
75 {
76     SUCCESS,
77     OPEN_FAILURE,
78     FAILURE,
79     INVALID_CHECKSUM
80 };
81 
82 /** @brief Trace block of data in hex with log<level:INFO>
83  *
84  *  @param[in] data - vector containing data to trace
85  *  @param[in] data_len - optional number of bytes to trace
86  *  If 0, entire vector will be traced.
87  */
88 void dump_hex(const std::vector<std::uint8_t>& data,
89               const unsigned int data_len = 0);
90 
91 /** @class OccCommand
92  *  @brief Send commands and process respsonses from the OCC
93  */
94 class OccCommand
95 {
96   public:
97     OccCommand() = delete;
98     OccCommand(const OccCommand&) = delete;
99     OccCommand& operator=(const OccCommand&) = delete;
100     OccCommand(OccCommand&&) = default;
101     OccCommand& operator=(OccCommand&&) = default;
102 
103     /** @brief Ctor to set up which OCC the command will go to
104      *
105      *  @param[in] instance - OCC instance
106      *  @param[in] path - Path to attach at
107      */
108     OccCommand(uint8_t instance, const char* path);
109 
110     /** @brief Dtor to clean up and close device */
111     ~OccCommand()
112     {
113         closeDevice();
114     }
115 
116     /** @brief Send the command to the OCC and collect the response.
117      * The checksum will be validated and removed from the response.
118      *
119      *  @param[in] command - command to pass-through
120      *  @param[out] response - response
121      *  returns SUCCESS if response was received
122      */
123     CmdStatus send(const std::vector<std::uint8_t>& command,
124                    std::vector<std::uint8_t>& response);
125 
126   private:
127     /** @brief Instance number of the target OCC */
128     uint8_t occInstance;
129 
130     /** @brief OCC path on the bus */
131     std::string path;
132 
133     /** @brief OCC device path
134      *  For now, here is the hard-coded mapping until
135      *  the udev rule is in.
136      *  occ0 --> /dev/occ1
137      *  occ1 --> /dev/occ2
138      *  ...
139      */
140     std::string devicePath;
141 
142     /** @brief Indicates whether or not the OCC is currently active */
143     bool occActive = false;
144 
145     /** brief file descriptor associated with occ device */
146     int fd = -1;
147 
148     /** @brief Subscribe to OCC Status signal
149      *
150      *  Once the OCC status gets to active, only then we will get /dev/occ2
151      *  populated and hence need to wait on that before opening that
152      */
153     sdbusplus::bus::match_t activeStatusSignal;
154 
155     /** Opens devicePath and populates file descriptor */
156     void openDevice();
157 
158     /** Closed the fd associated with opened device */
159     void closeDevice();
160 
161     /** @brief Callback function on OCC Status change signals
162      *
163      *  @param[in]  msg - Data associated with subscribed signal
164      */
165     void activeStatusEvent(sdbusplus::message::message& msg);
166 };
167 
168 } // namespace occ
169 } // namespace open_power
170