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