xref: /openbmc/google-ipmi-sys/handler_impl.hpp (revision 4134c74bda1bef375e155e193bad98741912c252)
1 // Copyright 2023 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 "bifurcation.hpp"
18 #include "file_system_wrapper_impl.hpp"
19 #include "handler.hpp"
20 
21 #include <nlohmann/json.hpp>
22 #include <sdbusplus/bus.hpp>
23 
24 #include <cstdint>
25 #include <map>
26 #include <memory>
27 #include <optional>
28 #include <string>
29 #include <string_view>
30 #include <tuple>
31 #include <vector>
32 
33 namespace google
34 {
35 namespace ipmi
36 {
37 
38 constexpr char defaultConfigFile[] =
39     "/usr/share/ipmi-entity-association/entity_association_map.json";
40 
41 class Handler : public HandlerInterface
42 {
43   public:
44     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
45         fsPtr(std::make_unique<FileSystemWrapper>()),
46         _configFile(entityConfigPath),
47         bifurcationHelper(BifurcationStatic::createBifurcation()) {};
48     Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,
49             const std::string& entityConfigPath = defaultConfigFile) :
50         fsPtr(std::make_unique<FileSystemWrapper>()),
51         _configFile(entityConfigPath), bifurcationHelper(bifurcationHelper) {};
52     ~Handler() = default;
53 
54     uint8_t getBmcMode() override;
55     std::tuple<std::uint8_t, std::string> getEthDetails(
56         std::string intf) const override;
57     std::int64_t getRxPackets(const std::string& name) const override;
58     VersionTuple getCpldVersion(unsigned int id) const override;
59     void psuResetDelay(std::uint32_t delay) const override;
60     void psuResetOnShutdown() const override;
61     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
62     uint32_t getFlashSize() override;
63     std::string getMachineName() override;
64     void buildI2cPcieMapping() override;
65     size_t getI2cPcieMappingSize() const override;
66     void hostPowerOffDelay(std::uint32_t delay) const override;
67     std::tuple<std::uint32_t, std::string> getI2cEntry(
68         unsigned int entry) const override;
69     std::vector<uint8_t> pcieBifurcation(uint8_t) override;
70 
71     uint32_t accelOobDeviceCount() const override;
72     std::string accelOobDeviceName(size_t index) const override;
73     uint64_t accelOobRead(std::string_view name, uint64_t address,
74                           uint8_t num_bytes) const override;
75     void accelOobWrite(std::string_view name, uint64_t address,
76                        uint8_t num_bytes, uint64_t data) const override;
77     void linuxBootDone() const override;
78     void accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
79                             uint8_t settings_id, uint16_t value) const override;
80     uint16_t accelGetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
81                                 uint8_t settings_id) const override;
82     std::string getBMInstanceProperty(uint8_t propertyType) const override;
83     std::optional<uint16_t> getCoreCount(
84         const std::string& filePath) const override;
85 
86   protected:
87     // Exposed for dependency injection
88     virtual sdbusplus::bus_t getDbus() const;
89     virtual const std::unique_ptr<FileSystemInterface>& getFs() const;
90 
91   private:
92     std::unique_ptr<FileSystemInterface> fsPtr;
93 
94     std::string _configFile;
95 
96     bool _entityConfigParsed = false;
97 
98     const std::map<uint8_t, std::string> _entityIdToName{
99         {0x03, "cpu"},
100         {0x04, "storage_device"},
101         {0x06, "system_management_module"},
102         {0x07, "system_board"},
103         {0x08, "memory_module"},
104         {0x0B, "add_in_card"},
105         {0x0E, "power_system_board"},
106         {0x10, "system_internal_expansion_board"},
107         {0x11, "other_system_board"},
108         {0x17, "system_chassis"},
109         {0x1D, "fan"},
110         {0x1E, "cooling_unit"},
111         {0x20, "memory_device"}};
112 
113     const std::unordered_map<uint8_t, std::string> _vrSettingsMap{
114         {0, "idle_mode_"},     {1, "power_brake_"},
115         {2, "loadline_"},      {3, "vout_margin_"},
116         {4, "vout_setpoint_"}, {5, "nominal_target_vout_"}};
117 
118     const std::unordered_map<uint8_t, std::string> bmInstanceTypeStringMap = {
119         {0x00, "asset-tag"}, {0x01, "board-serial-number"},
120         {0x02, "family"},    {0x03, "product-name"},
121         {0x04, "sku"},       {0x05, "system-serial-number"},
122         {0x06, "uuid"}};
123 
124     nlohmann::json _entityConfig{};
125 
126     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
127 
128     std::reference_wrapper<BifurcationInterface> bifurcationHelper;
129 };
130 
131 /**
132  * Given a type, entity instance, and a configuration, return the name.
133  *
134  * @param[in] type - the entity type
135  * @param[in] instance - the entity instance
136  * @param[in] config - the json object holding the entity mapping
137  * @return the name of the entity from the map
138  */
139 std::string readNameFromConfig(const std::string& type, uint8_t instance,
140                                const nlohmann::json& config);
141 
142 } // namespace ipmi
143 } // namespace google
144