From patchwork Mon Mar 30 10:55:12 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 456054 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 0D8541401AB for ; Mon, 30 Mar 2015 21:55:27 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751961AbbC3KzX (ORCPT ); Mon, 30 Mar 2015 06:55:23 -0400 Received: from metis.ext.pengutronix.de ([92.198.50.35]:58697 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751953AbbC3KzS (ORCPT ); Mon, 30 Mar 2015 06:55:18 -0400 Received: from dude.hi.4.pengutronix.de ([10.1.0.7] helo=dude.pengutronix.de.) by metis.ext.pengutronix.de with esmtp (Exim 4.72) (envelope-from ) id 1YcXLf-00025E-5i; Mon, 30 Mar 2015 12:55:15 +0200 From: Lucas Stach To: Bjorn Helgaas Cc: Thierry Reding , Stephen Warren , Alexandre Courbot , linux-tegra@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH v4 1/2] PCI: add helper function to find root port for device Date: Mon, 30 Mar 2015 12:55:12 +0200 Message-Id: <1427712913-13678-1-git-send-email-l.stach@pengutronix.de> X-Mailer: git-send-email 2.1.4 X-SA-Exim-Connect-IP: 10.1.0.7 X-SA-Exim-Mail-From: l.stach@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-tegra@vger.kernel.org Sender: linux-tegra-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-tegra@vger.kernel.org This adds a simple way to get the root port a given device is connected to. Signed-off-by: Lucas Stach --- v2: new patch in v2 v3: rename to pci_find_rootport to fit better with other API v4: - rename to make it obvious that this function is PCIe specific - fixes wrong assumption about what is a root bus in the presence virtual buses --- drivers/pci/search.c | 20 ++++++++++++++++++++ include/linux/pci.h | 1 + 2 files changed, 21 insertions(+) diff --git a/drivers/pci/search.c b/drivers/pci/search.c index a20ce7d5e2a7..d7c599103ae1 100644 --- a/drivers/pci/search.c +++ b/drivers/pci/search.c @@ -384,3 +384,23 @@ int pci_dev_present(const struct pci_device_id *ids) return 0; } EXPORT_SYMBOL(pci_dev_present); + +/** + * pcie_find_root_port - Returns the root port the given device is connected to. + * @dev: PCI device for which the root port should be found. + */ +struct pci_dev *pcie_find_root_port(struct pci_dev *dev) +{ + struct pci_bus *bus = dev->bus; + + /* if this device is located on the root bus, it is a root port */ + if (pci_is_root_bus(bus)) + return dev; + + /* walk up the PCI hierarchy to the first level below the root */ + while (bus->parent && bus->parent->parent) + bus = bus->parent; + + return bus->self; +} +EXPORT_SYMBOL(pcie_find_root_port); diff --git a/include/linux/pci.h b/include/linux/pci.h index 211e9da8a7d7..308c71081034 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -844,6 +844,7 @@ static inline struct pci_dev *pci_get_bus_and_slot(unsigned int bus, } struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from); int pci_dev_present(const struct pci_device_id *ids); +struct pci_dev *pcie_find_root_port(struct pci_dev *dev); int pci_bus_read_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 *val);