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