From c7758856c1938e85d44341d629d0c783eff80fe6 Mon Sep 17 00:00:00 2001
From: Blue Swirl <blauwirbel@gmail.com>
Date: Sat, 29 Aug 2009 14:01:56 +0000
Subject: [PATCH] PCI: use device info

Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
 hw/pci.c        |   46 +++++++++++++++++++++++-----------------------
 hw/pci.h        |    2 --
 monitor.c       |    2 --
 qemu-monitor.hx |    2 --
 4 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/hw/pci.c b/hw/pci.c
index 41e99a9..93c9c62 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -100,6 +100,8 @@ static void pci_bus_reset(void *opaque)
     }
 }
 
+static void pci_info(Monitor *mon, void *opaque);
+
 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
                          pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
                          qemu_irq *pic, int devfn_min, int nirq)
@@ -118,6 +120,7 @@ PCIBus *pci_register_bus(DeviceState *parent, const char *name,
     first_bus = bus;
     vmstate_register(nbus++, &vmstate_pcibus, bus);
     qemu_register_reset(pci_bus_reset, bus);
+    monitor_register_device_info(name, pci_info, bus);
     return bus;
 }
 
@@ -695,9 +698,24 @@ static const pci_class_desc pci_class_descriptions[] =
     { 0, NULL}
 };
 
-static void pci_info_device(PCIDevice *d)
+static void pci_bus_for_each_device(PCIBus *bus,
+                                    void (*fn)(PCIDevice *d, void *fn_opaque),
+                                    void *fn_opaque)
 {
-    Monitor *mon = cur_mon;
+    PCIDevice *d;
+    int devfn;
+
+    for(devfn = 0; devfn < 256; devfn++) {
+        d = bus->devices[devfn];
+        if (d) {
+            fn(d, fn_opaque);
+        }
+    }
+}
+
+static void pci_info_device(PCIDevice *d, void *opaque)
+{
+    Monitor *mon = opaque;
     int i, class;
     PCIIORegion *r;
     const pci_class_desc *desc;
@@ -739,31 +757,13 @@ static void pci_info_device(PCIDevice *d)
         }
     }
     monitor_printf(mon, "      id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
-    if (class == 0x0604 && d->config[0x19] != 0) {
-        pci_for_each_device(d->config[0x19], pci_info_device);
-    }
 }
 
-void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d))
+static void pci_info(Monitor *mon, void *opaque)
 {
-    PCIBus *bus = first_bus;
-    PCIDevice *d;
-    int devfn;
-
-    while (bus && bus->bus_num != bus_num)
-        bus = bus->next;
-    if (bus) {
-        for(devfn = 0; devfn < 256; devfn++) {
-            d = bus->devices[devfn];
-            if (d)
-                fn(d);
-        }
-    }
-}
+    PCIBus *bus = opaque;
 
-void pci_info(Monitor *mon)
-{
-    pci_for_each_device(0, pci_info_device);
+    pci_bus_for_each_device(bus, pci_info_device, mon);
 }
 
 PCIDevice *pci_create(const char *name, const char *devaddr)
diff --git a/hw/pci.h b/hw/pci.h
index fd1d35c..a3e7ba9 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -250,14 +250,12 @@ PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len);
 uint32_t pci_data_read(void *opaque, uint32_t addr, int len);
 int pci_bus_num(PCIBus *s);
-void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d));
 PCIBus *pci_find_bus(int bus_num);
 PCIDevice *pci_find_device(int bus_num, int slot, int function);
 
 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
                      unsigned *slotp);
 
-void pci_info(Monitor *mon);
 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
                         pci_map_irq_fn map_irq, const char *name);
 
diff --git a/monitor.c b/monitor.c
index d5b39eb..4f507f2 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1799,8 +1799,6 @@ static const mon_cmd_t info_cmds[] = {
       "", "show infos for each CPU" },
     { "history", "", do_info_history,
       "", "show the command line history", },
-    { "pci", "", pci_info,
-      "", "show PCI info", },
 #if defined(TARGET_I386) || defined(TARGET_SH4)
     { "tlb", "", tlb_info,
       "", "show virtual to physical memory mappings", },
diff --git a/qemu-monitor.hx b/qemu-monitor.hx
index 2026921..6ad960e 100644
--- a/qemu-monitor.hx
+++ b/qemu-monitor.hx
@@ -45,8 +45,6 @@ show the cpu registers
 show infos for each CPU
 @item info history
 show the command line history
-@item info pci
-show emulated PCI device info
 @item info tlb
 show virtual to physical memory mappings (i386 only)
 @item info mem
-- 
1.5.6.5

