From patchwork Thu Dec 18 19:11:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Stach X-Patchwork-Id: 5515771 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7D230BEEA8 for ; Thu, 18 Dec 2014 19:11:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 02C172035B for ; Thu, 18 Dec 2014 19:11:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 073CE2086A for ; Thu, 18 Dec 2014 19:11:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751147AbaLRTLs (ORCPT ); Thu, 18 Dec 2014 14:11:48 -0500 Received: from metis.ext.pengutronix.de ([92.198.50.35]:51475 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751163AbaLRTLr (ORCPT ); Thu, 18 Dec 2014 14:11:47 -0500 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 1Y1gUC-0005lx-1J; Thu, 18 Dec 2014 20:11:44 +0100 From: Lucas Stach To: Bjorn Helgaas , Thierry Reding Cc: Alexandre Courbot , linux-tegra@vger.kernel.org, linux-pci@vger.kernel.org Subject: [PATCH v2 1/2] PCI: add helper function to find root port for device Date: Thu, 18 Dec 2014 20:11:42 +0100 Message-Id: <1418929903-8506-1-git-send-email-l.stach@pengutronix.de> X-Mailer: git-send-email 2.1.3 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-pci@vger.kernel.org Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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 a81f413083e4..c3ae1c52c7cf 100644 --- a/drivers/pci/search.c +++ b/drivers/pci/search.c @@ -385,3 +385,23 @@ int pci_dev_present(const struct pci_device_id *ids) return 0; } EXPORT_SYMBOL(pci_dev_present); + +/** + * pci_get_rootport - Returns the root port the given device is connected to. + * @dev: PCI device for which the root port should be found. + */ +struct pci_dev *pci_get_rootport(struct pci_dev *dev) +{ + struct pci_bus *bus = dev->bus; + + /* If there is no bridge on the bus the passed device is a root port. */ + if (!bus->self) + return dev; + + /* Walk up the PCI hierarchy to the first level below the root. */ + while (bus->parent && bus->parent->self) + bus = bus->parent; + + return bus->self; +} +EXPORT_SYMBOL(pci_get_rootport); diff --git a/include/linux/pci.h b/include/linux/pci.h index 4c8ac5fcc224..05442db50cad 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -843,6 +843,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 *pci_get_rootport(struct pci_dev *dev); int pci_bus_read_config_byte(struct pci_bus *bus, unsigned int devfn, int where, u8 *val);