From patchwork Fri May 10 21:18:46 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 2552241 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 80534400ED for ; Fri, 10 May 2013 21:19:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755483Ab3EJVTL (ORCPT ); Fri, 10 May 2013 17:19:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6323 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754670Ab3EJVTK (ORCPT ); Fri, 10 May 2013 17:19:10 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4ALIkpl006754 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 10 May 2013 17:18:47 -0400 Received: from bling.home (ovpn-113-58.phx2.redhat.com [10.3.113.58]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r4ALIkxk014969; Fri, 10 May 2013 17:18:46 -0400 Subject: [PATCH 1/3] pci: Add PCI walk function and PCIe bridge test To: bhelgaas@google.com, stephen@networkplumber.org From: Alex Williamson Cc: linux-pci@vger.kernel.org, iommu@lists.linux-foundation.org, dwmw2@infradead.org, ddutile@redhat.com Date: Fri, 10 May 2013 15:18:46 -0600 Message-ID: <20130510211845.32592.77464.stgit@bling.home> In-Reply-To: <20130510210937.32592.21950.stgit@bling.home> References: <20130510210937.32592.21950.stgit@bling.home> User-Agent: StGit/0.16 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org These will replace pci_find_upstream_pcie_bridge, which is difficult to use and rather specific to intel-iommu usage. A quirked pci_is_pcie_bridge function is provided to work around non-compliant PCIe-to-PCI bridges such as those found in https://bugzilla.kernel.org/show_bug.cgi?id=44881 Signed-off-by: Alex Williamson --- drivers/pci/search.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 23 ++++++++++++++++++++ 2 files changed, 80 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/pci/search.c b/drivers/pci/search.c index d0627fa..0357f74 100644 --- a/drivers/pci/search.c +++ b/drivers/pci/search.c @@ -17,6 +17,63 @@ DECLARE_RWSEM(pci_bus_sem); EXPORT_SYMBOL_GPL(pci_bus_sem); +/* Test for PCIe bridges. */ +bool pci_is_pcie_bridge(struct pci_dev *pdev) +{ + if (!pci_is_bridge(pdev)) + return false; + + if (pci_is_pcie(pdev)) + return true; + +#ifdef CONFIG_PCI_QUIRKS + /* + * If we're not on the root bus, look one device upstream of the + * current device. If that device is PCIe and is not a PCIe-to-PCI + * bridge, then the current device is effectively PCIe as it must + * be the PCIe-to-PCI bridge. This handles several bridges that + * violate the PCIe spec by not exposing a PCIe capability: + * https://bugzilla.kernel.org/show_bug.cgi?id=44881 + */ + if (!pci_is_root_bus(pdev->bus)) { + struct pci_dev *parent = pdev->bus->self; + + if (pci_is_pcie(parent) && + pci_pcie_type(parent) != PCI_EXP_TYPE_PCI_BRIDGE) + + return true; + } +#endif + return false; +} + +/* + * Walk upstream from the given pdev for the first device returning + * true for the provided match function. If no match is found, return + * NULL. *last records the previous step in the walk. + */ +struct pci_dev *pci_walk_up_to_first_match(struct pci_dev *pdev, + bool (*match)(struct pci_dev *), + struct pci_dev **last) +{ + *last = NULL; + + if (match(pdev)) + return pdev; + + *last = pdev; + + while (!pci_is_root_bus(pdev->bus)) { + *last = pdev; + pdev = pdev->bus->self; + + if (match(pdev)) + return pdev; + } + + return NULL; +} + /* * find the upstream PCIe-to-PCI bridge of a PCI device * if the device is PCIE, return NULL diff --git a/include/linux/pci.h b/include/linux/pci.h index bd8ec30..e87423a 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1855,6 +1855,29 @@ static inline struct eeh_dev *pci_dev_to_eeh_dev(struct pci_dev *pdev) #endif /** + * pci_walk_up_to_first_match - Generic upstream search function + * @pdev: starting PCI device to search + * @match: match function to call on each device (true = match) + * @last: last device examined prior to returned device + * + * Walk upstream from the given device, calling match() at each device. + * Returns the first device matching match(). If the root bus is reached + * without finding a match, return NULL. last returns the N-1 step in + * the search path. + */ +struct pci_dev *pci_walk_up_to_first_match(struct pci_dev *pdev, + bool (*match)(struct pci_dev *), + struct pci_dev **last); + +/** + * pci_is_pcie_bridge - Match a PCIe bridge device + * @pdev: device to test + * + * Return true if the given device is a PCIe bridge, false otherwise. + */ +bool pci_is_pcie_bridge(struct pci_dev *pdev); + +/** * pci_find_upstream_pcie_bridge - find upstream PCIe-to-PCI bridge of a device * @pdev: the PCI device *