xref: /openbmc/google-ipmi-sys/handler.hpp (revision 559cb011)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <ipmid/api-types.hpp>
18 #include <ipmid/message.hpp>
19 
20 #include <cstdint>
21 #include <map>
22 #include <span>
23 #include <string>
24 #include <string_view>
25 #include <tuple>
26 #include <vector>
27 
28 namespace google
29 {
30 namespace ipmi
31 {
32 
33 using Resp = ::ipmi::RspType<std::uint8_t, std::vector<uint8_t>>;
34 
35 using VersionTuple =
36     std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
37 
38 class HandlerInterface
39 {
40   public:
41     virtual ~HandlerInterface() = default;
42 
43     /**
44      * Return the operation mode of BMC
45      *
46      * @return the BMC operation mode
47      */
48     virtual uint8_t getBmcMode() = 0;
49 
50     /**
51      * Return ethernet details (hard-coded).
52      *
53      * @return tuple of ethernet details (channel, if name).
54      */
55     virtual std::tuple<std::uint8_t, std::string>
56         getEthDetails(std::string intf) const = 0;
57 
58     /**
59      * Return the value of rx_packets, given a if_name.
60      *
61      * @param[in] name, the interface name.
62      * @return the number of packets received.
63      * @throw IpmiException on failure.
64      */
65     virtual std::int64_t getRxPackets(const std::string& name) const = 0;
66 
67     /**
68      * Return the values from a cpld version file.
69      *
70      * @param[in] id - the cpld id number.
71      * @return the quad of numbers as a tuple (maj,min,pt,subpt)
72      * @throw IpmiException on failure.
73      */
74     virtual VersionTuple getCpldVersion(unsigned int id) const = 0;
75 
76     /**
77      * Set the PSU Reset delay.
78      *
79      * @param[in] delay - delay in seconds.
80      * @throw IpmiException on failure.
81      */
82     virtual void psuResetDelay(std::uint32_t delay) const = 0;
83 
84     /**
85      * Arm for PSU reset on host shutdown.
86      *
87      * @throw IpmiException on failure.
88      */
89     virtual void psuResetOnShutdown() const = 0;
90 
91     /**
92      * Return the entity name.
93      * On the first call to this method it'll build the list of entities.
94      * @todo Consider moving the list building to construction time (and ignore
95      * failures).
96      *
97      * @param[in] id - the entity id value
98      * @param[in] instance - the entity instance
99      * @return the entity's name
100      * @throw IpmiException on failure.
101      */
102     virtual std::string getEntityName(std::uint8_t id,
103                                       std::uint8_t instance) = 0;
104 
105     /**
106      * Return the flash size of bmc chip.
107      *
108      * @return the flash size of bmc chip
109      * @throw IpmiException on failure.
110      */
111     virtual uint32_t getFlashSize() = 0;
112 
113     /**
114      * Return the name of the machine, parsed from release information.
115      *
116      * @return the machine name
117      * @throw IpmiException on failure.
118      */
119     virtual std::string getMachineName() = 0;
120 
121     /**
122      * Populate the i2c-pcie mapping vector.
123      */
124     virtual void buildI2cPcieMapping() = 0;
125 
126     /**
127      * Return the size of the i2c-pcie mapping vector.
128      *
129      * @return the size of the vector holding the i2c-pcie mapping tuples.
130      */
131     virtual size_t getI2cPcieMappingSize() const = 0;
132 
133     /**
134      * Return a copy of the entry in the vector.
135      *
136      * @param[in] entry - the index into the vector.
137      * @return the tuple at that index.
138      */
139     virtual std::tuple<std::uint32_t, std::string>
140         getI2cEntry(unsigned int entry) const = 0;
141 
142     /**
143      * Set the Host Power Off delay.
144      *
145      * @param[in] delay - delay in seconds.
146      * @throw IpmiException on failure.
147      */
148     virtual void hostPowerOffDelay(std::uint32_t delay) const = 0;
149 
150     /**
151      * Return the number of devices from the CustomAccel service.
152      *
153      * @return the number of devices.
154      * @throw IpmiException on failure.
155      */
156     virtual uint32_t accelOobDeviceCount() const = 0;
157 
158     /**
159      * Return the name of a single device from the CustomAccel service.
160      *
161      * Valid indexes start at 0 and go up to (but don't include) the number of
162      * devices. The number of devices can be queried with accelOobDeviceCount.
163      *
164      * @param[in] index - the index of the device, starting at 0.
165      * @return the name of the device.
166      * @throw IpmiException on failure.
167      */
168     virtual std::string accelOobDeviceName(size_t index) const = 0;
169 
170     /**
171      * Read from a single CustomAccel service device.
172      *
173      * Valid device names can be queried with accelOobDeviceName.
174      * If num_bytes < 8, all unused MSBs are padded with 0s.
175      *
176      * @param[in] name - the name of the device (from DeviceName).
177      * @param[in] address - the address to read from.
178      * @param[in] num_bytes - the size of the read, in bytes.
179      * @return the data read, with 0s padding any unused MSBs.
180      * @throw IpmiException on failure.
181      */
182     virtual uint64_t accelOobRead(std::string_view name, uint64_t address,
183                                   uint8_t num_bytes) const = 0;
184 
185     /**
186      * Write to a single CustomAccel service device.
187      *
188      * Valid device names can be queried with accelOobDeviceName.
189      * If num_bytes < 8, all unused MSBs are ignored.
190      *
191      * @param[in] name - the name of the device (from DeviceName).
192      * @param[in] address - the address to read from.
193      * @param[in] num_bytes - the size of the read, in bytes.
194      * @param[in] data - the data to write.
195      * @throw IpmiException on failure.
196      */
197     virtual void accelOobWrite(std::string_view name, uint64_t address,
198                                uint8_t num_bytes, uint64_t data) const = 0;
199 
200     /**
201      * Parse the I2C tree to get the highest level of bifurcation in target bus.
202      *
203      * @param[in] index    - PCIe Slot Index
204      * @return list of lanes taken by each device.
205      */
206     virtual std::vector<uint8_t> pcieBifurcation(uint8_t index) = 0;
207 
208     /**
209      * Prepare for OS boot.
210      *
211      * If in bare metal mode, the BMC will disable IPMI, to protect against an
212      * untrusted OS.
213      */
214     virtual void linuxBootDone() const = 0;
215 
216     /**
217      * Update the VR settings for the given settings_id
218      *
219      * @param[in] chip_id    - Accel Device#
220      * @param[in] settings_id  - ID of the setting to update
221      * @param[in] value  - Value of the setting
222      */
223     virtual void accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
224                                     uint8_t settings_id,
225                                     uint16_t value) const = 0;
226 
227     /**
228      * Read current VR settings value for the given settings_id
229      *
230      * @param[in] chip_id    - Accel Device#
231      * @param[in] settings_id  - ID of the setting to read
232      */
233     virtual uint16_t accelGetVrSettings(::ipmi::Context::ptr ctx,
234                                         uint8_t chip_id,
235                                         uint8_t settings_id) const = 0;
236 
237     /**
238      * Get the BM instance property from /run/<propertyType>
239      *
240      * @param[in] propertyType  - BM instance property type
241      * @return - string of the requested BM instance property
242      */
243     virtual std::string getBMInstanceProperty(uint8_t propertyType) const = 0;
244 };
245 
246 } // namespace ipmi
247 } // namespace google
248