linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: bhelgaas@google.com
Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] pci: Add pci_walk_slot() interface
Date: Mon, 19 Aug 2013 14:04:10 -0600	[thread overview]
Message-ID: <20130819195917.9062.43580.stgit@bling.home> (raw)

We have a pci_walk_bus() interface, but it's exceptionally cumbersome
for the callback function to figure out if the device is relevant if
the caller is trying to walk all the devices in or below a slot.  Add
a variant to walk only slot devices.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---

This allows me to use pci_walk_bus() and pci_walk_slot() instead of
rolling my own for iterating devices for bus/slot reset.  Please
consider for 3.12 to go with the rest of the bus/slot infrastructure.

 drivers/pci/bus.c   |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pci.h |    2 ++
 2 files changed, 58 insertions(+)

diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index b1ff02a..316579a 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -283,6 +283,62 @@ void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
 }
 EXPORT_SYMBOL_GPL(pci_walk_bus);
 
+/** pci_walk_slot - walk devices in/under slot, calling callback.
+ *  @slot     slot whose devices should be walked
+ *  @cb       callback to be called for each device found
+ *  @userdata arbitrary pointer to be passed to callback.
+ *
+ *  Walk the given slot, including any bridged devices
+ *  on buses under this slot.  Call the provided callback
+ *  on each device found.
+ *
+ *  We check the return of @cb each time. If it returns anything
+ *  other than 0, we break out.
+ *
+ */
+void pci_walk_slot(struct pci_slot *slot, int (*cb)(struct pci_dev *, void *),
+		   void *userdata)
+{
+	struct pci_dev *dev;
+	struct pci_bus *bus;
+	struct list_head *next;
+	int retval;
+
+	bus = slot->bus;
+	down_read(&pci_bus_sem);
+	next = slot->bus->devices.next;
+	for (;;) {
+		if (next == &bus->devices) {
+			/* end of this bus, go up or finish */
+			if (bus == slot->bus)
+				break;
+			next = bus->self->bus_list.next;
+			bus = bus->self->bus;
+			continue;
+		}
+		dev = list_entry(next, struct pci_dev, bus_list);
+
+		/* skip anything on the top level bus not in our slot */
+		if (dev->bus == slot->bus && dev->slot != slot) {
+			next = dev->bus_list.next;
+			continue;
+		}
+
+		if (dev->subordinate) {
+			/* this is a pci-pci bridge, do its devices next */
+			next = dev->subordinate->devices.next;
+			bus = dev->subordinate;
+		} else
+			next = dev->bus_list.next;
+
+		retval = cb(dev, userdata);
+		if (retval)
+			break;
+	}
+	up_read(&pci_bus_sem);
+}
+EXPORT_SYMBOL_GPL(pci_walk_slot);
+
 struct pci_bus *pci_bus_get(struct pci_bus *bus)
 {
 	if (bus)
diff --git a/include/linux/pci.h b/include/linux/pci.h
index daf40cd..cbb291b 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1087,6 +1087,8 @@ int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max,
 
 void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *),
 		  void *userdata);
+void pci_walk_slot(struct pci_slot *slot, int (*cb)(struct pci_dev *, void *),
+		  void *userdata);
 int pci_cfg_space_size_ext(struct pci_dev *dev);
 int pci_cfg_space_size(struct pci_dev *dev);
 unsigned char pci_bus_max_busnr(struct pci_bus *bus);


             reply	other threads:[~2013-08-19 20:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-19 20:04 Alex Williamson [this message]
2013-08-22 21:34 ` [PATCH] pci: Add pci_walk_slot() interface Bjorn Helgaas
2013-08-22 21:49   ` Alex Williamson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130819195917.9062.43580.stgit@bling.home \
    --to=alex.williamson@redhat.com \
    --cc=bhelgaas@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).