pcie_bifurcation: Fetch bifurcation at a PCIe slot with hardcoded values

This will read a json config and return the bifurcation based on that.
It will read the configure file in a persistent file to see if it is
valid machine config and return the bifurcation information
accordingly.

For example,
If it is valid config, it will return 8x8 for PE1,3,4,6.

Tested:
Unit tests passed.

Physical Tests.

PE0 -> no bifurcation
PE1 -> x8x8

```
$ ipmitool raw 0x2e 0x32 0x79 0x2b 0x00 0x0f 0
 79 2b 00 0b 00

$ ipmitool raw 0x2e 0x32 0x79 0x2b 0x00 0x0f 1
 79 2b 00 0b 02 08 08
```

Change-Id: I44aefbfb26372f8bc0069343c8a6d07d3cf6f42d
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/README.md b/README.md
index 70fa020..95ec033 100644
--- a/README.md
+++ b/README.md
@@ -400,3 +400,22 @@
 |n+2..n+10| |Address
 |n+11| |Number of bytes
 |n+12..n+20| |Data
+
+### PCIe Bifurcation - SubCommand 0x0F
+Sys command to return the highest level of bifurcation for the
+target PCIe Slot.
+
+Request
+
+|Byte(s) |Value  |Data
+|--------|-------|----
+|0x00|0x0F|Subcommand
+|0x01|PE slot number|Index of the PE slot
+
+Response
+
+|Byte(s) |Value  |Data
+|--------|-------|----
+|0x00|0x0F|Subcommand
+|0x01|Config length (N)|Number of bytes needed for the bifurcation config
+|0x02..0x02 + N - 1|Lanes per device|Each byte represents the number of lanes bonded together for each endpoint device
diff --git a/bifurcation/bifurcation.hpp b/bifurcation/bifurcation.hpp
new file mode 100644
index 0000000..c0bd23d
--- /dev/null
+++ b/bifurcation/bifurcation.hpp
@@ -0,0 +1,67 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#pragma once
+
+#include <cstdint>
+#include <functional>
+#include <optional>
+#include <string>
+#include <string_view>
+#include <unordered_map>
+#include <vector>
+
+namespace google
+{
+namespace ipmi
+{
+
+class BifurcationInterface
+{
+  public:
+    virtual ~BifurcationInterface() = default;
+
+    /**
+     * Get the Bifurcation of the device at the i2c bus
+     *
+     * @param[in] bus    - I2C bus of the device
+     * @return the bifurcation at the i2c bus
+     */
+    virtual std::optional<std::vector<uint8_t>>
+        getBifurcation(uint8_t bus) noexcept = 0;
+};
+
+class BifurcationStatic : public BifurcationInterface
+{
+  public:
+    static std::reference_wrapper<BifurcationInterface> createBifurcation()
+    {
+        static BifurcationStatic bifurcationStatic;
+
+        return std::ref(bifurcationStatic);
+    }
+
+    BifurcationStatic(std::string_view bifurcationFile);
+
+    std::optional<std::vector<uint8_t>>
+        getBifurcation(uint8_t index) noexcept override;
+
+  protected:
+    BifurcationStatic();
+
+  private:
+    std::string bifurcationFile;
+};
+
+} // namespace ipmi
+} // namespace google
diff --git a/bifurcation/bifurcation_static.cpp b/bifurcation/bifurcation_static.cpp
new file mode 100644
index 0000000..66b0c9a
--- /dev/null
+++ b/bifurcation/bifurcation_static.cpp
@@ -0,0 +1,91 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+#include "config.h"
+
+#include "bifurcation.hpp"
+
+#include <fmt/format.h>
+
+#include <charconv>
+#include <filesystem>
+#include <fstream>
+#include <nlohmann/json.hpp>
+#include <optional>
+#include <string_view>
+#include <vector>
+
+namespace google
+{
+namespace ipmi
+{
+
+BifurcationStatic::BifurcationStatic() :
+    BifurcationStatic(STATIC_BIFURCATION_CONFIG)
+{
+}
+
+BifurcationStatic::BifurcationStatic(std::string_view bifurcationFile) :
+    bifurcationFile(bifurcationFile)
+{
+}
+
+std::optional<std::vector<uint8_t>>
+    BifurcationStatic::getBifurcation(uint8_t index) noexcept
+{
+    // Example valid data:
+    // {
+    //     "1": [8,8],
+    //     "2": [4, 4, 12]
+    // }
+    std::ifstream jsonFile(bifurcationFile.c_str());
+    if (!jsonFile.is_open())
+    {
+        fmt::print(stderr, "Unable to open file {} for bifurcation.\n",
+                   bifurcationFile.data());
+        return std::nullopt;
+    }
+
+    nlohmann::json jsonData;
+    try
+    {
+        jsonData = nlohmann::json::parse(jsonFile, nullptr, false);
+    }
+    catch (const nlohmann::json::parse_error& ex)
+    {
+        fmt::print(
+            stderr,
+            "Failed to parse the static config. Parse error at byte {}\n",
+            ex.byte);
+        return std::nullopt;
+    }
+
+    std::vector<uint8_t> vec;
+    try
+    {
+        std::string key = std::to_string(index);
+        auto value = jsonData[key];
+        value.get_to(vec);
+    }
+    catch (std::exception const& e)
+    {
+        fmt::print(stderr,
+                   "Failed to convert bifurcation value to vec[uin8_t]\n");
+        return std::nullopt;
+    }
+
+    return vec;
+}
+
+} // namespace ipmi
+} // namespace google
diff --git a/bifurcation/meson.build b/bifurcation/meson.build
new file mode 100644
index 0000000..d653099
--- /dev/null
+++ b/bifurcation/meson.build
@@ -0,0 +1,20 @@
+bifurcation_inc = include_directories('.')
+
+bifurcation_deps = [
+  fmt_dep,
+]
+
+bifurcation_lib = static_library(
+  'bifurcation',
+  'bifurcation_static.cpp',
+  conf_h,
+  dependencies: bifurcation_deps,
+  include_directories: [bifurcation_inc, root_inc],
+  install: false,
+)
+
+bifurcation_dep = declare_dependency(
+  dependencies: bifurcation_deps,
+  include_directories: bifurcation_inc,
+  link_with: bifurcation_lib,
+)
diff --git a/handler.cpp b/handler.cpp
index 8ec7863..28408f2 100644
--- a/handler.cpp
+++ b/handler.cpp
@@ -599,5 +599,11 @@
     }
 }
 
+std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
+{
+    return bifurcationHelper.get().getBifurcation(index).value_or(
+        std::vector<uint8_t>{});
+}
+
 } // namespace ipmi
 } // namespace google
diff --git a/handler.hpp b/handler.hpp
index 8c36c52..d919f05 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -187,6 +187,14 @@
      */
     virtual void accelOobWrite(std::string_view name, uint64_t address,
                                uint8_t num_bytes, uint64_t data) const = 0;
+
+    /**
+     * Prase the I2C tree to get the highest level of bifurcation in target bus.
+     *
+     * @param[in] index    - PCIe Slot Index
+     * @return list of lanes taken by each device.
+     */
+    virtual std::vector<uint8_t> pcieBifurcation(uint8_t index) = 0;
 };
 
 } // namespace ipmi
diff --git a/handler_impl.hpp b/handler_impl.hpp
index f46e79c..4a3f5dc 100644
--- a/handler_impl.hpp
+++ b/handler_impl.hpp
@@ -14,6 +14,7 @@
 
 #pragma once
 
+#include "bifurcation.hpp"
 #include "handler.hpp"
 
 #include <cstdint>
@@ -37,7 +38,12 @@
 {
   public:
     explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
-        _configFile(entityConfigPath){};
+        _configFile(entityConfigPath),
+        bifurcationHelper(BifurcationStatic::createBifurcation()){};
+    Handler(std::reference_wrapper<BifurcationInterface> bifurcationHelper,
+            const std::string& entityConfigPath = defaultConfigFile) :
+        _configFile(entityConfigPath),
+        bifurcationHelper(bifurcationHelper){};
     ~Handler() = default;
 
     std::tuple<std::uint8_t, std::string>
@@ -54,6 +60,7 @@
     void hostPowerOffDelay(std::uint32_t delay) const override;
     std::tuple<std::uint32_t, std::string>
         getI2cEntry(unsigned int entry) const override;
+    std::vector<uint8_t> pcieBifurcation(uint8_t) override;
 
     uint32_t accelOobDeviceCount() const override;
     std::string accelOobDeviceName(size_t index) const override;
@@ -89,6 +96,8 @@
     nlohmann::json _entityConfig{};
 
     std::vector<std::tuple<uint32_t, std::string>> _pcie_i2c_map;
+
+    std::reference_wrapper<BifurcationInterface> bifurcationHelper;
 };
 
 /**
diff --git a/ipmi.cpp b/ipmi.cpp
index 715264b..0638a82 100644
--- a/ipmi.cpp
+++ b/ipmi.cpp
@@ -24,6 +24,7 @@
 #include "handler.hpp"
 #include "host_power_off.hpp"
 #include "machine_name.hpp"
+#include "pcie_bifurcation.hpp"
 #include "pcie_i2c.hpp"
 #include "psu.hpp"
 
@@ -76,6 +77,8 @@
             return accelOobRead(data, handler);
         case SysAccelOobWrite:
             return accelOobWrite(data, handler);
+        case SysPCIeSlotBifurcation:
+            return pcieBifurcation(data, handler);
         default:
             std::fprintf(stderr, "Invalid subcommand: 0x%x\n", cmd);
             return ::ipmi::responseInvalidCommand();
diff --git a/meson.build b/meson.build
index d561fdc..a2e8d1b 100644
--- a/meson.build
+++ b/meson.build
@@ -9,6 +9,14 @@
     'werror=true',
   ])
 
+root_inc = include_directories('.')
+
+conf_data = configuration_data()
+conf_data.set_quoted('STATIC_BIFURCATION_CONFIG', get_option('static-bifurcation'))
+conf_h = configure_file(
+  output: 'config.h',
+  configuration: conf_data)
+
 meson.get_compiler('cpp').has_header_symbol(
   'ipmid/api.h',
   'ipmid_get_sd_bus_connection')
@@ -19,13 +27,30 @@
   'nlohmann::json',
   dependencies: json_dep)
 
+fmt_dep = dependency('fmt', required: false)
+if not fmt_dep.found()
+  fmt_proj = import('cmake').subproject(
+    'fmt',
+    cmake_options: [
+      '-DCMAKE_POSITION_INDEPENDENT_CODE=ON',
+      '-DMASTER_PROJECT=OFF'
+    ],
+    required: false)
+  assert(fmt_proj.found(), 'fmtlib is required')
+  fmt_dep = fmt_proj.dependency('fmt')
+endif
+
+subdir('bifurcation')
+
 sys_pre = declare_dependency(
-  include_directories: include_directories('.'),
+  include_directories: root_inc,
   dependencies: [
     json_dep,
+    fmt_dep,
     dependency('phosphor-dbus-interfaces'),
     dependency('phosphor-logging'),
     dependency('sdbusplus'),
+    bifurcation_dep,
   ])
 
 sys_lib = static_library(
@@ -41,6 +66,7 @@
   'machine_name.cpp',
   'pcie_i2c.cpp',
   'google_accel_oob.cpp',
+  'pcie_bifurcation.cpp',
   'psu.cpp',
   'util.cpp',
   implicit_include_directories: false,
diff --git a/meson_options.txt b/meson_options.txt
index 0fc2767..45af3e9 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1 +1,2 @@
 option('tests', type: 'feature', description: 'Build tests')
+option('static-bifurcation', type: 'string', value: '/usr/share/google-ipmi-sys/bifurcation.json', description: 'Path to Static Bifurcation Json config')
diff --git a/pcie_bifurcation.cpp b/pcie_bifurcation.cpp
new file mode 100644
index 0000000..71c25cd
--- /dev/null
+++ b/pcie_bifurcation.cpp
@@ -0,0 +1,75 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "config.h"
+
+#include "pcie_bifurcation.hpp"
+
+#include "commands.hpp"
+#include "errors.hpp"
+#include "handler.hpp"
+
+#include <fmt/format.h>
+
+#include <ipmid/api-types.hpp>
+#include <span>
+#include <vector>
+
+namespace google
+{
+namespace ipmi
+{
+
+namespace
+{
+#ifndef MAX_IPMI_BUFFER
+#define MAX_IPMI_BUFFER 64
+#endif
+} // namespace
+
+struct PcieBifurcationRequest
+{
+    uint8_t pcieIndex;
+} __attribute__((packed));
+
+Resp pcieBifurcation(std::span<const uint8_t> data, HandlerInterface* handler)
+{
+    if (data.size() < sizeof(struct PcieBifurcationRequest))
+    {
+        fmt::print(stderr, "Invalid command length: {}\n",
+                   static_cast<uint32_t>(data.size()));
+        return ::ipmi::responseReqDataLenInvalid();
+    }
+
+    auto bifurcation = handler->pcieBifurcation(/*index=*/data[0]);
+
+    int length = sizeof(struct PcieBifurcationReply) + bifurcation.size();
+
+    if (length > MAX_IPMI_BUFFER)
+    {
+        std::fprintf(stderr, "Response would overflow response buffer\n");
+        return ::ipmi::responseInvalidCommand();
+    }
+
+    std::vector<std::uint8_t> reply;
+    reply.reserve(bifurcation.size() + sizeof(struct PcieBifurcationReply));
+    reply.emplace_back(bifurcation.size()); /* bifurcationLength */
+    reply.insert(reply.end(), bifurcation.begin(),
+                 bifurcation.end()); /* bifurcation */
+
+    return ::ipmi::responseSuccess(SysOEMCommands::SysPCIeSlotBifurcation,
+                                   reply);
+}
+} // namespace ipmi
+} // namespace google
diff --git a/pcie_bifurcation.hpp b/pcie_bifurcation.hpp
new file mode 100644
index 0000000..e854490
--- /dev/null
+++ b/pcie_bifurcation.hpp
@@ -0,0 +1,34 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include "handler.hpp"
+
+#include <span>
+
+namespace google
+{
+namespace ipmi
+{
+
+struct PcieBifurcationReply
+{
+    uint8_t bifurcationLength;
+} __attribute__((packed));
+
+Resp pcieBifurcation(std::span<const uint8_t> data, HandlerInterface* handler);
+
+} // namespace ipmi
+} // namespace google
diff --git a/test/handler_mock.hpp b/test/handler_mock.hpp
index 2fb8e2a..9961fa8 100644
--- a/test/handler_mock.hpp
+++ b/test/handler_mock.hpp
@@ -62,6 +62,7 @@
     MOCK_METHOD(void, accelOobWrite,
                 (std::string_view, uint64_t, uint8_t, uint64_t),
                 (const, override));
+    MOCK_METHOD(std::vector<uint8_t>, pcieBifurcation, (uint8_t), (override));
 };
 
 } // namespace ipmi
diff --git a/test/handler_unittest.cpp b/test/handler_unittest.cpp
index 418ba46..2edb977 100644
--- a/test/handler_unittest.cpp
+++ b/test/handler_unittest.cpp
@@ -18,7 +18,10 @@
 
 #include <systemd/sd-bus.h>
 
+#include <charconv>
+#include <filesystem>
 #include <fstream>
+#include <functional>
 #include <nlohmann/json.hpp>
 #include <sdbusplus/message.hpp>
 #include <sdbusplus/test/sdbus_mock.hpp>
@@ -32,6 +35,9 @@
 namespace ipmi
 {
 
+using testing::_;
+using testing::Return;
+
 TEST(HandlerTest, EthCheckValidHappy)
 {
     Handler h;
@@ -112,6 +118,7 @@
 
 using ::testing::_;
 using ::testing::AnyNumber;
+using ::testing::ContainerEq;
 using ::testing::DoAll;
 using ::testing::ElementsAre;
 using ::testing::Eq;
@@ -586,6 +593,50 @@
                  IpmiException);
 }
 
+TEST(HandlerTest, PcieBifurcation)
+{
+    const std::string& testJson = "/tmp/test-json";
+    auto j = R"(
+        {
+            "1": [ 1, 3 ],
+            "3": [ 3, 6 ],
+            "4": [ 3, 4, 1 ],
+            "6": [ 8 ]
+        }
+    )"_json;
+
+    std::ofstream bifurcationJson(testJson);
+    bifurcationJson << j.dump();
+    bifurcationJson.flush();
+    bifurcationJson.close();
+
+    BifurcationStatic bifurcationHelper(testJson);
+    Handler h(std::ref(bifurcationHelper));
+
+    std::unordered_map<uint8_t, std::vector<uint8_t>> expectedMapping = {
+        {1, {1, 3}}, {3, {3, 6}}, {4, {3, 4, 1}}, {6, {8}}};
+    std::vector<uint8_t> invalidBus = {0, 2, 5, 7};
+
+    for (const auto& [bus, output] : expectedMapping)
+    {
+        EXPECT_THAT(h.pcieBifurcation(bus), ContainerEq(output));
+    }
+
+    for (const auto& bus : invalidBus)
+    {
+        EXPECT_TRUE(h.pcieBifurcation(bus).empty());
+    }
+
+    std::filesystem::remove(testJson.data());
+    bifurcationHelper = BifurcationStatic(testJson);
+    Handler h2(std::ref(bifurcationHelper));
+    for (uint8_t i = 0; i < 8; ++i)
+    {
+        auto bifurcation = h2.pcieBifurcation(i);
+        EXPECT_TRUE(bifurcation.empty());
+    }
+}
+
 // TODO: Add checks for other functions of handler.
 
 } // namespace ipmi
diff --git a/test/meson.build b/test/meson.build
index 9c579bd..f2c11a2 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -27,6 +27,7 @@
   'pcie',
   'poweroff',
   'psu',
+  'pcie_bifurcation',
 ]
 
 foreach t : tests
diff --git a/test/pcie_bifurcation_unittest.cpp b/test/pcie_bifurcation_unittest.cpp
new file mode 100644
index 0000000..90e81e4
--- /dev/null
+++ b/test/pcie_bifurcation_unittest.cpp
@@ -0,0 +1,74 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "commands.hpp"
+#include "handler_mock.hpp"
+#include "helper.hpp"
+#include "pcie_bifurcation.hpp"
+
+#include <vector>
+
+#include <gtest/gtest.h>
+
+using ::testing::Return;
+
+namespace google
+{
+namespace ipmi
+{
+
+using testing::_;
+using ::testing::ContainerEq;
+
+TEST(PcieBifurcationCommandTest, InvalidRequest)
+{
+    std::vector<uint8_t> request = {};
+
+    HandlerMock hMock;
+    EXPECT_EQ(::ipmi::responseReqDataLenInvalid(),
+              pcieBifurcation(request, &hMock));
+}
+
+TEST(PcieBifurcationCommandTest, ValidRequest)
+{
+    std::vector<uint8_t> request = {5};
+    std::vector<uint8_t> expectedOutput = {4, 8, 1, 2};
+
+    HandlerMock hMock;
+    EXPECT_CALL(hMock, pcieBifurcation(5)).WillOnce(Return(expectedOutput));
+
+    auto reply = pcieBifurcation(request, &hMock);
+    auto result = ValidateReply(reply);
+    auto& data = result.second;
+
+    EXPECT_EQ(sizeof(struct PcieBifurcationReply) + expectedOutput.size(),
+              data.size());
+    EXPECT_EQ(SysOEMCommands::SysPCIeSlotBifurcation, result.first);
+    EXPECT_THAT(std::vector<uint8_t>(data.begin() + 1, data.end()),
+                ContainerEq(expectedOutput));
+}
+
+TEST(PcieBifurcationCommandTest, ReplyExceddedMaxValue)
+{
+    std::vector<uint8_t> request = {5};
+    std::vector<uint8_t> expectedOutput(64, 1);
+
+    HandlerMock hMock;
+    EXPECT_CALL(hMock, pcieBifurcation(5)).WillOnce(Return(expectedOutput));
+    EXPECT_EQ(::ipmi::responseInvalidCommand(),
+              pcieBifurcation(request, &hMock));
+}
+
+} // namespace ipmi
+} // namespace google