<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 8c838a8233c3862a6953eb23b041143a98110a9f Mon Sep 17 00:00:00 2001
From: Marcel Apfelbaum &lt;marcel@redhat.com&gt;
Date: Wed, 24 Jun 2015 13:53:24 +0200
Subject: [PATCH 15/31] hw/pci: extend PCI config access to support devices
 behind PXB

Message-id: &lt;1435154016-26233-32-git-send-email-marcel@redhat.com&gt;
Patchwork-id: 66463
O-Subject: [RHEL-7.2 qemu-kvm-rhev PATCH 31/43] hw/pci: extend PCI config access to support devices behind PXB
Bugzilla: 1103313
RH-Acked-by: Laszlo Ersek &lt;lersek@redhat.com&gt;
RH-Acked-by: Michael S. Tsirkin &lt;mst@redhat.com&gt;
RH-Acked-by: Igor Mammedov &lt;imammedo@redhat.com&gt;

PXB buses are assumed to be children of bus 0. Look for them
while scanning the buses.

Signed-off-by: Marcel Apfelbaum &lt;marcel@redhat.com&gt;
Reviewed-by: Michael S. Tsirkin &lt;mst@redhat.com&gt;
Signed-off-by: Michael S. Tsirkin &lt;mst@redhat.com&gt;
Acked-by: Laszlo Ersek &lt;lersek@redhat.com&gt;
(cherry picked from commit 09e5b81922179b6c52b42fd27587e64b474036c7)
Signed-off-by: Miroslav Rezanina &lt;mrezanin@redhat.com&gt;
---
 hw/pci/pci.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index ca6a708..38a9cb4 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -1714,10 +1714,28 @@ static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
 {
     return !(pci_get_word(dev-&gt;config + PCI_BRIDGE_CONTROL) &amp;
              PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &amp;&amp;
-        dev-&gt;config[PCI_SECONDARY_BUS] &lt; bus_num &amp;&amp;
+        dev-&gt;config[PCI_SECONDARY_BUS] &lt;= bus_num &amp;&amp;
         bus_num &lt;= dev-&gt;config[PCI_SUBORDINATE_BUS];
 }
 
+/* Whether a given bus number is in a range of a root bus */
+static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
+{
+    int i;
+
+    for (i = 0; i &lt; ARRAY_SIZE(bus-&gt;devices); ++i) {
+        PCIDevice *dev = bus-&gt;devices[i];
+
+        if (dev &amp;&amp; PCI_DEVICE_GET_CLASS(dev)-&gt;is_bridge) {
+            if (pci_secondary_bus_in_range(dev, bus_num)) {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}
+
 static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
 {
     PCIBus *sec;
@@ -1739,12 +1757,18 @@ static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
     /* try child bus */
     for (; bus; bus = sec) {
         QLIST_FOREACH(sec, &amp;bus-&gt;child, sibling) {
-            assert(!pci_bus_is_root(sec));
-            if (sec-&gt;parent_dev-&gt;config[PCI_SECONDARY_BUS] == bus_num) {
+            if (pci_bus_num(sec) == bus_num) {
                 return sec;
             }
-            if (pci_secondary_bus_in_range(sec-&gt;parent_dev, bus_num)) {
-                break;
+            /* PXB buses assumed to be children of bus 0 */
+            if (pci_bus_is_root(sec)) {
+                if (pci_root_bus_in_range(sec, bus_num)) {
+                    break;
+                }
+            } else {
+                if (pci_secondary_bus_in_range(sec-&gt;parent_dev, bus_num)) {
+                    break;
+                }
             }
         }
     }
-- 
1.8.3.1

</pre></body></html>