115d4d21cSHao Zhou // Copyright 2023 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14a2056e9cSWilly Tu 
15c87de558SPatrick Venture #pragma once
16c87de558SPatrick Venture 
176c71b0f9SWilly Tu #include "bifurcation.hpp"
1815d4d21cSHao Zhou #include "file_system_wrapper_impl.hpp"
19c87de558SPatrick Venture #include "handler.hpp"
20c87de558SPatrick Venture 
21c87de558SPatrick Venture #include <nlohmann/json.hpp>
224f0d1de6SSteve Foreman #include <sdbusplus/bus.hpp>
23444b5ea4SPatrick Williams 
24444b5ea4SPatrick Williams #include <cstdint>
25444b5ea4SPatrick Williams #include <map>
2615d4d21cSHao Zhou #include <memory>
27c87de558SPatrick Venture #include <string>
284f0d1de6SSteve Foreman #include <string_view>
29c87de558SPatrick Venture #include <tuple>
30c87de558SPatrick Venture #include <vector>
31c87de558SPatrick Venture 
32c87de558SPatrick Venture namespace google
33c87de558SPatrick Venture {
34c87de558SPatrick Venture namespace ipmi
35c87de558SPatrick Venture {
36c87de558SPatrick Venture 
3796ad9069SWilliam A. Kennington III constexpr char defaultConfigFile[] =
3896ad9069SWilliam A. Kennington III     "/usr/share/ipmi-entity-association/entity_association_map.json";
39c87de558SPatrick Venture 
40c87de558SPatrick Venture class Handler : public HandlerInterface
41c87de558SPatrick Venture {
42c87de558SPatrick Venture   public:
Handler(const std::string & entityConfigPath=defaultConfigFile)43c87de558SPatrick Venture     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
4415d4d21cSHao Zhou         fsPtr(std::make_unique<FileSystemWrapper>()),
456c71b0f9SWilly Tu         _configFile(entityConfigPath),
466c71b0f9SWilly Tu         bifurcationHelper(BifurcationStatic::createBifurcation()){};
Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,const std::string & entityConfigPath=defaultConfigFile)476c71b0f9SWilly Tu     Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,
486c71b0f9SWilly Tu             const std::string& entityConfigPath = defaultConfigFile) :
4915d4d21cSHao Zhou         fsPtr(std::make_unique<FileSystemWrapper>()),
5015d4d21cSHao Zhou         _configFile(entityConfigPath), bifurcationHelper(bifurcationHelper){};
51c87de558SPatrick Venture     ~Handler() = default;
52c87de558SPatrick Venture 
535e70dc8cSNikhil Namjoshi     uint8_t getBmcMode() override;
54b69209b4SWilliam A. Kennington III     std::tuple<std::uint8_t, std::string>
55b69209b4SWilliam A. Kennington III         getEthDetails(std::string intf) const override;
56c87de558SPatrick Venture     std::int64_t getRxPackets(const std::string& name) const override;
57c87de558SPatrick Venture     VersionTuple getCpldVersion(unsigned int id) const override;
58c87de558SPatrick Venture     void psuResetDelay(std::uint32_t delay) const override;
59ac4a16f7SShounak Mitra     void psuResetOnShutdown() const override;
60c87de558SPatrick Venture     std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
613b1b427cSWilly Tu     uint32_t getFlashSize() override;
6229f35bceSWilliam A. Kennington III     std::string getMachineName() override;
63c87de558SPatrick Venture     void buildI2cPcieMapping() override;
64c87de558SPatrick Venture     size_t getI2cPcieMappingSize() const override;
658cfa4c44Slinyuny     void hostPowerOffDelay(std::uint32_t delay) const override;
66c87de558SPatrick Venture     std::tuple<std::uint32_t, std::string>
67c87de558SPatrick Venture         getI2cEntry(unsigned int entry) const override;
686c71b0f9SWilly Tu     std::vector<uint8_t> pcieBifurcation(uint8_t) override;
69c87de558SPatrick Venture 
704f0d1de6SSteve Foreman     uint32_t accelOobDeviceCount() const override;
714f0d1de6SSteve Foreman     std::string accelOobDeviceName(size_t index) const override;
724f0d1de6SSteve Foreman     uint64_t accelOobRead(std::string_view name, uint64_t address,
734f0d1de6SSteve Foreman                           uint8_t num_bytes) const override;
744f0d1de6SSteve Foreman     void accelOobWrite(std::string_view name, uint64_t address,
754f0d1de6SSteve Foreman                        uint8_t num_bytes, uint64_t data) const override;
76a92d0e6bSJohn Wedig     void linuxBootDone() const override;
77d455bfd6SGaurav Gandhi     void accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
78d455bfd6SGaurav Gandhi                             uint8_t settings_id, uint16_t value) const override;
79d455bfd6SGaurav Gandhi     uint16_t accelGetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
80d455bfd6SGaurav Gandhi                                 uint8_t settings_id) const override;
81*559cb011SBrandon Kim     std::string getBMInstanceProperty(uint8_t propertyType) const override;
824f0d1de6SSteve Foreman 
834f0d1de6SSteve Foreman   protected:
844f0d1de6SSteve Foreman     // Exposed for dependency injection
8565371228SPatrick Williams     virtual sdbusplus::bus_t getDbus() const;
8615d4d21cSHao Zhou     virtual const std::unique_ptr<FileSystemInterface>& getFs() const;
874f0d1de6SSteve Foreman 
88c87de558SPatrick Venture   private:
8915d4d21cSHao Zhou     std::unique_ptr<FileSystemInterface> fsPtr;
9015d4d21cSHao Zhou 
91c87de558SPatrick Venture     std::string _configFile;
92c87de558SPatrick Venture 
93c87de558SPatrick Venture     bool _entityConfigParsed = false;
94c87de558SPatrick Venture 
95c87de558SPatrick Venture     const std::map<uint8_t, std::string> _entityIdToName{
96c87de558SPatrick Venture         {0x03, "cpu"},
97c87de558SPatrick Venture         {0x04, "storage_device"},
98c87de558SPatrick Venture         {0x06, "system_management_module"},
99c87de558SPatrick Venture         {0x07, "system_board"},
100c87de558SPatrick Venture         {0x08, "memory_module"},
101c87de558SPatrick Venture         {0x0B, "add_in_card"},
102c87de558SPatrick Venture         {0x0E, "power_system_board"},
103c87de558SPatrick Venture         {0x10, "system_internal_expansion_board"},
104c87de558SPatrick Venture         {0x11, "other_system_board"},
105c87de558SPatrick Venture         {0x17, "system_chassis"},
106c87de558SPatrick Venture         {0x1D, "fan"},
107c87de558SPatrick Venture         {0x1E, "cooling_unit"},
108c87de558SPatrick Venture         {0x20, "memory_device"}};
109c87de558SPatrick Venture 
110d455bfd6SGaurav Gandhi     const std::unordered_map<uint8_t, std::string> _vrSettingsMap{
1119846023cSGaurav Gandhi         {0, "idle_mode_"}, {1, "power_brake_"}, {2, "loadline_"}};
112d455bfd6SGaurav Gandhi 
113*559cb011SBrandon Kim     const std::unordered_map<uint8_t, std::string> bmInstanceTypeStringMap = {
114*559cb011SBrandon Kim         {0x00, "asset-tag"}, {0x01, "board-serial-number"},
115*559cb011SBrandon Kim         {0x02, "family"},    {0x03, "product-name"},
116*559cb011SBrandon Kim         {0x04, "sku"},       {0x05, "system-serial-number"},
117*559cb011SBrandon Kim         {0x06, "uuid"}};
118*559cb011SBrandon Kim 
119c87de558SPatrick Venture     nlohmann::json _entityConfig{};
120c87de558SPatrick Venture 
121c87de558SPatrick Venture     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
1226c71b0f9SWilly Tu 
1236c71b0f9SWilly Tu     std::reference_wrapper<BifurcationInterface> bifurcationHelper;
124c87de558SPatrick Venture };
125c87de558SPatrick Venture 
126a3f9c2d3SPatrick Venture /**
127a3f9c2d3SPatrick Venture  * Given a type, entity instance, and a configuration, return the name.
128a3f9c2d3SPatrick Venture  *
129a3f9c2d3SPatrick Venture  * @param[in] type - the entity type
130a3f9c2d3SPatrick Venture  * @param[in] instance - the entity instance
131a3f9c2d3SPatrick Venture  * @param[in] config - the json object holding the entity mapping
132a3f9c2d3SPatrick Venture  * @return the name of the entity from the map
133a3f9c2d3SPatrick Venture  */
134a3f9c2d3SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
135a3f9c2d3SPatrick Venture                                const nlohmann::json& config);
136a3f9c2d3SPatrick Venture 
137c87de558SPatrick Venture } // namespace ipmi
138c87de558SPatrick Venture } // namespace google
139