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