tools/pci: Use pci_device_enable() to enable bridge

Enabling the bridge by writing to the PCI config space does not enable
all the host resources required.
Use pci_device_enable api.

Signed-off-by: Vivekanand Veeracholan <vveerach@google.com>
Change-Id: I159a132b76eadc569efb8fb47488a4abd4c27404
diff --git a/tools/pci.cpp b/tools/pci.cpp
index 0efb75d..224a619 100644
--- a/tools/pci.cpp
+++ b/tools/pci.cpp
@@ -119,6 +119,19 @@
     std::uint8_t value;
     int ret;
 
+    /* TODO: pci_device_disable support is missing in libpciaccess. Add it
+     * to the disableBridge() once it is available.
+     * https://gitlab.freedesktop.org/xorg/lib/libpciaccess/-/merge_requests/17
+     */
+
+    pci->pci_device_enable(dev);
+
+    /* We need to retain this direct write to config space eventhough
+     * pci_device_enable() should do it. Because currently disabling is done
+     * through write to config space and not done through the proper api.
+     * So libpciaccess ref count does not reset on disable. The
+     * pci_device_enable() above will not do anything the second time.
+     */
     ret = pci->pci_device_cfg_read_u8(dev, &value, bridge);
     if (ret)
     {
@@ -170,6 +183,21 @@
      * the bridge enabled on the BMC.
      */
     std::uint32_t value;
+
+    /* TODO: pci_device_disable support is missing in libpciaccess. Add it
+     * to the disableBridge() once it is available.
+     * https://gitlab.freedesktop.org/xorg/lib/libpciaccess/-/merge_requests/17
+     */
+
+    pci->pci_device_enable(dev);
+
+    /* We need to retain this direct write to config space eventhough
+     * pci_device_enable() should do it. Because currently disabling is done
+     * through write to config space and not done through the proper api.
+     * So libpciaccess ref count does not reset on disable. The
+     * pci_device_enable() above will not do anything the second time.
+     */
+
     std::memcpy(&value, addr + config, sizeof(value));
 
     if (0 == (value & bridgeEnabled))
diff --git a/tools/pciaccess.cpp b/tools/pciaccess.cpp
index 0d2ae0b..e5b9a2f 100644
--- a/tools/pciaccess.cpp
+++ b/tools/pciaccess.cpp
@@ -34,6 +34,11 @@
     return ::pci_iterator_destroy(iter);
 }
 
+void PciAccessImpl::pci_device_enable(struct pci_device* dev) const
+{
+    return ::pci_device_enable(dev);
+}
+
 struct pci_device*
     PciAccessImpl::pci_device_next(struct pci_device_iterator* iter) const
 {
diff --git a/tools/pciaccess.hpp b/tools/pciaccess.hpp
index 3b25594..23fc4ab 100644
--- a/tools/pciaccess.hpp
+++ b/tools/pciaccess.hpp
@@ -37,6 +37,7 @@
         const struct pci_id_match* match) const = 0;
     virtual void
         pci_iterator_destroy(struct pci_device_iterator* iter) const = 0;
+    virtual void pci_device_enable(struct pci_device* dev) const = 0;
     virtual struct pci_device*
         pci_device_next(struct pci_device_iterator* iter) const = 0;
     virtual int pci_device_probe(struct pci_device* dev) const = 0;
@@ -66,6 +67,7 @@
     struct pci_device_iterator* pci_id_match_iterator_create(
         const struct pci_id_match* match) const override;
     void pci_iterator_destroy(struct pci_device_iterator* iter) const override;
+    void pci_device_enable(struct pci_device* dev) const override;
     struct pci_device*
         pci_device_next(struct pci_device_iterator* iter) const override;
     int pci_device_probe(struct pci_device* dev) const override;
diff --git a/tools/test/pciaccess_mock.hpp b/tools/test/pciaccess_mock.hpp
index a030a18..738a1b1 100644
--- a/tools/test/pciaccess_mock.hpp
+++ b/tools/test/pciaccess_mock.hpp
@@ -13,6 +13,7 @@
     MOCK_CONST_METHOD1(pci_id_match_iterator_create,
                        struct pci_device_iterator*(const struct pci_id_match*));
     MOCK_CONST_METHOD1(pci_iterator_destroy, void(struct pci_device_iterator*));
+    MOCK_CONST_METHOD1(pci_device_enable, void(struct pci_device*));
     MOCK_CONST_METHOD1(pci_device_next,
                        struct pci_device*(struct pci_device_iterator*));
     MOCK_CONST_METHOD1(pci_device_probe, int(struct pci_device*));
diff --git a/tools/test/tools_pci_unittest.cpp b/tools/test/tools_pci_unittest.cpp
index 7f69db2..6906851 100644
--- a/tools/test/tools_pci_unittest.cpp
+++ b/tools/test/tools_pci_unittest.cpp
@@ -317,6 +317,8 @@
                 pci_device_unmap_range(Eq(&dev), Eq(region), mockRegionSize))
         .WillOnce(Return(0));
 
+    EXPECT_CALL(pciMock, pci_device_enable(Eq(&dev))).Times(1);
+
     if (deviceExpectations)
         param->expectSetup(pciMock, dev);
 }