Patchwork [1/7] qbus: add functions to walk both devices and busses

login
register
mail settings
Submitter Isaku Yamahata
Date Nov. 17, 2010, 4:50 a.m.
Message ID <61e9609833bd34a1e1766d7b7b9faae6a86503cb.1289969012.git.yamahata@valinux.co.jp>
Download mbox | patch
Permalink /patch/71512/
State New
Headers show

Comments

Isaku Yamahata - Nov. 17, 2010, 4:50 a.m.
From: Anthony Liguori <anthony@codemonkey.ws>

There are some cases where you want to walk the busses, in particular, when
searching for a bus either by name or DeviceInfo.
Paolo suggested that we model the return values on how GCC's walkers work which
allows an actor to skip child transversal, or terminate walking with a positive
value that's returned as the qbus_walk_children's result.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 hw/qdev.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/qdev.h |    9 +++++++++
 2 files changed, 55 insertions(+), 0 deletions(-)
Paolo Bonzini - Nov. 17, 2010, 11:57 a.m.
On 11/17/2010 05:50 AM, Isaku Yamahata wrote:
> +/* Returns 0 to walk children,>  0 to terminate walk,<  0 to skip walk. */

Shouldn't this be the other way round according to the code (< 0 to 
terminate, > 0 to skip children)?

> +/* Returns > 0 if either devfn or busfn terminate walk, 0 otherwise. */

This should be "returns != 0".  It can return a value > 0 if the 
toplevel callback asks to skip the children, < 0 if any callback 
(including the toplevel) asks to terminate the walk, 0 otherwise.

Paolo

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index 35858cb..11d845a 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -449,6 +449,52 @@  BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
     return NULL;
 }
 
+int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque)
+{
+    DeviceState *dev;
+    int err;
+
+    if (busfn) {
+        err = busfn(bus, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
+    QLIST_FOREACH(dev, &bus->children, sibling) {
+        err = qdev_walk_children(dev, devfn, busfn, opaque);
+        if (err < 0) {
+            return err;
+        }
+    }
+
+    return 0;
+}
+
+int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque)
+{
+    BusState *bus;
+    int err;
+
+    if (devfn) {
+        err = devfn(dev, opaque);
+        if (err) {
+            return err;
+        }
+    }
+
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
+        err = qbus_walk_children(bus, devfn, busfn, opaque);
+        if (err < 0) {
+            return err;
+        }
+    }
+
+    return 0;
+}
+
 static BusState *qbus_find_recursive(BusState *bus, const char *name,
                                      const BusInfo *info)
 {
diff --git a/hw/qdev.h b/hw/qdev.h
index 579328a..0cf50b1 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -173,9 +173,18 @@  BusState *qdev_get_parent_bus(DeviceState *dev);
 
 /*** BUS API. ***/
 
+/* Returns 0 to walk children, > 0 to terminate walk, < 0 to skip walk. */
+typedef int (qbus_walkerfn)(BusState *bus, void *opaque);
+typedef int (qdev_walkerfn)(DeviceState *dev, void *opaque);
+
 void qbus_create_inplace(BusState *bus, BusInfo *info,
                          DeviceState *parent, const char *name);
 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name);
+/* Returns > 0 if either devfn or busfn terminate walk, 0 otherwise. */
+int qbus_walk_children(BusState *bus, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque);
+int qdev_walk_children(DeviceState *dev, qdev_walkerfn *devfn,
+                       qbus_walkerfn *busfn, void *opaque);
 void qbus_free(BusState *bus);
 
 #define FROM_QBUS(type, dev) DO_UPCAST(type, qbus, dev)