From patchwork Fri Aug 26 01:47:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ley Foon Tan X-Patchwork-Id: 9300751 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6175D60757 for ; Fri, 26 Aug 2016 02:02:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C40D292E3 for ; Fri, 26 Aug 2016 02:02:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 40E422943B; Fri, 26 Aug 2016 02:02:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81EF7292E3 for ; Fri, 26 Aug 2016 02:02:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751745AbcHZCCR (ORCPT ); Thu, 25 Aug 2016 22:02:17 -0400 Received: from mail-by2nam01on0074.outbound.protection.outlook.com ([104.47.34.74]:12928 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751393AbcHZCCQ (ORCPT ); Thu, 25 Aug 2016 22:02:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=altera.onmicrosoft.com; s=selector1-altera-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=6A2z0EAxeIo1qeYws1b2rjK1oj9IuEalg3JpNrrt1dc=; b=Kx7BcYe/jcCjzrbvsbiv1jF2kV2r3441Yso/8EpE1gr4iNSlVIU/aCkjArk0hToPBvaxxGvc6bhflRQNnJ89nbgo3eAxMrIIGf376hZrTSR1mjNV+C6DgnCUBYQ+TjTU4GcUkRwPVCzdS8LWQ0RnQSzR5VpdkcqCvB0pKrCGGvg= Received: from BLUPR0301CA0031.namprd03.prod.outlook.com (10.162.113.169) by BY2PR0301MB2087.namprd03.prod.outlook.com (10.163.197.140) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.9; Fri, 26 Aug 2016 01:47:42 +0000 Received: from BY2FFO11FD038.protection.gbl (2a01:111:f400:7c0c::154) by BLUPR0301CA0031.outlook.office365.com (2a01:111:e400:5259::41) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.599.9 via Frontend Transport; Fri, 26 Aug 2016 01:47:41 +0000 Authentication-Results: spf=softfail (sender IP is 66.35.236.227) smtp.mailfrom=altera.com; vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=altera.com; Received-SPF: SoftFail (protection.outlook.com: domain of transitioning altera.com discourages use of 66.35.236.227 as permitted sender) Received: from sj-itexedge03.altera.priv.altera.com (66.35.236.227) by BY2FFO11FD038.mail.protection.outlook.com (10.1.14.223) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.587.6 via Frontend Transport; Fri, 26 Aug 2016 01:47:41 +0000 Received: from sj-mail01.altera.com (137.57.1.6) by webmail.altera.com (66.35.236.227) with Microsoft SMTP Server (TLS) id 14.3.174.1; Thu, 25 Aug 2016 18:47:27 -0700 Received: from ubuntu (pg-lftan-440b.altera.priv.altera.com [137.57.162.77]) by sj-mail01.altera.com (8.13.7+Sun/8.13.7) with SMTP id u7Q1lZLD005302; Thu, 25 Aug 2016 18:47:36 -0700 (PDT) Received: by ubuntu (sSMTP sendmail emulation); Fri, 26 Aug 2016 09:47:34 +0800 From: Ley Foon Tan To: Bjorn Helgaas CC: , , Ley Foon Tan , Ley Foon Tan Subject: [PATCH v4 2/2] PCI: altera: Move retrain from _FIXUP to altera_pcie_host_init() Date: Fri, 26 Aug 2016 09:47:25 +0800 Message-ID: <1472176045-27815-3-git-send-email-lftan@altera.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1472176045-27815-1-git-send-email-lftan@altera.com> References: <1472176045-27815-1-git-send-email-lftan@altera.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:66.35.236.227; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(199003)(189002)(4326007)(106466001)(81166006)(356003)(8936002)(7846002)(105596002)(50986999)(19580405001)(8676002)(229853001)(586003)(2906002)(76176999)(86362001)(48376002)(50226002)(4001430100002)(81156014)(19580395003)(50466002)(87936001)(42186005)(110136002)(33646002)(97736004)(626004)(36756003)(5003940100001)(92566002)(11100500001)(107886002)(68736007)(305945005)(2950100001)(5660300001)(189998001)(47776003)(7099028); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR0301MB2087; H:sj-itexedge03.altera.priv.altera.com; FPR:; SPF:SoftFail; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11FD038; 1:LWIKsRDOGWVgDLbmwrN3QfMn4sHJQPXVHtS90vjwQIpAECcMkqyDuHRl8X7f3VMgafuCuslHJW3T0HHF2gtPLa4EVCK56z8bImL7tm/IcK9IMfeKdbcvy7cD4RQTqc6Mvg0gmNJ/blkcMjdL5CnZkatTkzNBJ6OkMVEx8v8+p584M+pOEQiKmnwSzxXRagsoBZd3/WiwxGL7bPkw7poBnte9TvaDKa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WMhFI8WnJJ2stTwrNXSaSmv2Vc35u1an+5r1epcNt4jMFN2+hX/OVIJRHt45191nj69n3b1UmY9pjOjRJIvsgBeXbrYmgnZLibX/rIfRqNp3aTEFXHEf59ZsMDNMQwNDcbQUknVzbLJtMWGlOAOOx5XHvroHCuEAPWpmVJK/hg vDRFLF8pdatCM3/jDCV1WYH9sdyYR2iTu8c= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(80048183373757); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(13024025)(13015025)(13018025)(13023025)(13017025)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:BY2PR0301MB2087; BCL:0; PCL:0; RULEID:; SRVR:BY2PR0301MB2087; X-Forefront-PRVS: 00462943DE X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR0301MB2087; 23:Lhy5o9BIa+uZKbZN/DyJy1kK/1/VbY+0IcEHZLW?= =?us-ascii?Q?OwWiauwkH+9tUelwquIpK5V1J1bjxS5f0tAzvNWmjd8U+k1028BjCjGCuHRs?= =?us-ascii?Q?XBic1kO1clU7nzioe56uNGGOI9F9aIi13l5Cin/TsHlnkYAhOQLuqKh29TSG?= =?us-ascii?Q?LmMENRaT9R4ueHYek7jc4acJWEvQopiRV7gDNau5hlV16d+FGq+vZEOOemRn?= =?us-ascii?Q?pqhKMJ9WbJFKiUxG6sJd2Fsek8UiKrbmypAQ5zNrVJhP7jKpky0jVzm4zlxO?= =?us-ascii?Q?QDh03dvwukZfgg4bVAXqGwt22Po2RiKLsZMZREt+pIhTR0Bi8ZLaEpTG/4Be?= =?us-ascii?Q?vlTnpi+HofLEY6Io61W+DZ/9Ro7FTOwaYcSTI7GZUjNas2ZVVadLlegapbLf?= =?us-ascii?Q?wX0AwjGSUlMbqOIsc24LNz1Z+l5P6DLaLitWi8fawwhcdi3oXDK3gp4Dx2Gu?= =?us-ascii?Q?qreYY6yHflrUSrr6P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lc7xuP5+LwM2BIggD5yCSuU22xXizC53r8Q870OzO8x024fAbiJbcXA==; 5:BM0vd569NqtsVhVCMWmncggiOCs0oVb+PdexPjNcMdLe+YUAY7aamhXPt7yZbmgX0CtDOfvmaig2iZ3cJcIHOL0aDfqsVmk5qfu2XLNKCRGZCUKVE+JcZqT9No4E6q7Wq4IfMlKNfUXKAdueuT1WAw==; 24:NVL5GZl1wus6OEcGBSXgsMZtbz8/vIj3uRvvIjCy9J+Fmhz/R5q/ryxRfXerFNxHdLkeSHgtpxxQKN0TV6TMerreTiVxLANukn0BWbNnvWc=; 7:2Q5D3J6lJeCaif/Xh7MKEFGJiEWc0RbLX8S83UEUJ8RHkEewd23q5mSLZCJZm/P5UzcbHX5Tp+RGMFNcBg8ZJSLHMfF4QvrvesP6rg0uDA7AqQkfKfd72YbyJaYIpqcYaexMDt/w2Fq3YWwv+kAI2lgu859lKoIkx43UDHjaH9KaBIQYIGy6Qfsz2MuBiI/aLSqIexenIMjei6bRG7wXBNga9SftKqM63XMYpGeoG/jtagfVtttN7VMrX8O2vVgm SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: altera.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Aug 2016 01:47:41.9118 (UTC) X-MS-Exchange-CrossTenant-Id: fbd72e03-d4a5-4110-adce-614d51f2077a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fbd72e03-d4a5-4110-adce-614d51f2077a; Ip=[66.35.236.227]; Helo=[sj-itexedge03.altera.priv.altera.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR0301MB2087 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Altera PCIe IP can be configured as rootport or device and they might have same vendor ID. It will cause the system hang issue if Altera PCIe is in endpoint mode and work with other PCIe rootport that from other vendors. Move link retrain function from _FIXUP to altera_pcie_host_init(). Signed-off-by: Ley Foon Tan --- drivers/pci/host/pcie-altera.c | 151 +++++++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 60 deletions(-) diff --git a/drivers/pci/host/pcie-altera.c b/drivers/pci/host/pcie-altera.c index 34e6258..4ca50a2 100644 --- a/drivers/pci/host/pcie-altera.c +++ b/drivers/pci/host/pcie-altera.c @@ -43,6 +43,7 @@ #define RP_LTSSM_MASK 0x1f #define LTSSM_L0 0xf +#define PCIE_CAP_OFFSET 0x80 /* TLP configuration type 0 and 1 */ #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */ #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */ @@ -100,66 +101,6 @@ static bool altera_pcie_link_is_up(struct altera_pcie *pcie) return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0); } -static void altera_wait_link_retrain(struct pci_dev *dev) -{ - u16 reg16; - unsigned long start_jiffies; - struct altera_pcie *pcie = dev->bus->sysdata; - - /* Wait for link training end. */ - start_jiffies = jiffies; - for (;;) { - pcie_capability_read_word(dev, PCI_EXP_LNKSTA, ®16); - if (!(reg16 & PCI_EXP_LNKSTA_LT)) - break; - - if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) { - dev_err(&pcie->pdev->dev, "link retrain timeout\n"); - break; - } - udelay(100); - } - - /* Wait for link is up */ - start_jiffies = jiffies; - for (;;) { - if (altera_pcie_link_is_up(pcie)) - break; - - if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) { - dev_err(&pcie->pdev->dev, "link up timeout\n"); - break; - } - udelay(100); - } -} - -static void altera_pcie_retrain(struct pci_dev *dev) -{ - u16 linkcap, linkstat; - struct altera_pcie *pcie = dev->bus->sysdata; - - if (!altera_pcie_link_is_up(pcie)) - return; - - /* - * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but - * current speed is 2.5 GB/s. - */ - pcie_capability_read_word(dev, PCI_EXP_LNKCAP, &linkcap); - - if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB) - return; - - pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &linkstat); - if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) { - pcie_capability_set_word(dev, PCI_EXP_LNKCTL, - PCI_EXP_LNKCTL_RL); - altera_wait_link_retrain(dev); - } -} -DECLARE_PCI_FIXUP_EARLY(0x1172, PCI_ANY_ID, altera_pcie_retrain); - /* * Altera PCIe port uses BAR0 of RC's configuration space as the translation * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space @@ -434,6 +375,90 @@ static struct pci_ops altera_pcie_ops = { .write = altera_pcie_cfg_write, }; +static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno, + unsigned int devfn, int offset, u16 *value) +{ + u32 data; + int ret; + + ret = _altera_pcie_cfg_read(pcie, busno, devfn, + PCIE_CAP_OFFSET + offset, sizeof(*value), + &data); + *value = data; + return ret; +} + +static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno, + unsigned int devfn, int offset, u16 value) +{ + return _altera_pcie_cfg_write(pcie, busno, devfn, + PCIE_CAP_OFFSET + offset, sizeof(value), + value); +} + +static void altera_wait_link_retrain(struct altera_pcie *pcie) +{ + u16 reg16; + unsigned long start_jiffies; + + /* Wait for link training end. */ + start_jiffies = jiffies; + for (;;) { + altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, + PCI_EXP_LNKSTA, ®16); + if (!(reg16 & PCI_EXP_LNKSTA_LT)) + break; + + if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) { + dev_err(&pcie->pdev->dev, "link retrain timeout\n"); + break; + } + udelay(100); + } + + /* Wait for link is up */ + start_jiffies = jiffies; + for (;;) { + if (altera_pcie_link_is_up(pcie)) + break; + + if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) { + dev_err(&pcie->pdev->dev, "link up timeout\n"); + break; + } + udelay(100); + } +} + +static void altera_pcie_retrain(struct altera_pcie *pcie) +{ + u16 linkcap, linkstat, linkctl; + + if (!altera_pcie_link_is_up(pcie)) + return; + + /* + * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but + * current speed is 2.5 GB/s. + */ + altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP, + &linkcap); + if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB) + return; + + altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA, + &linkstat); + if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) { + altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, + PCI_EXP_LNKCTL, &linkctl); + linkctl |= PCI_EXP_LNKCTL_RL; + altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, + PCI_EXP_LNKCTL, linkctl); + + altera_wait_link_retrain(pcie); + } +} + static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq, irq_hw_number_t hwirq) { @@ -555,6 +580,11 @@ static int altera_pcie_parse_dt(struct altera_pcie *pcie) return 0; } +static void altera_pcie_host_init(struct altera_pcie *pcie) +{ + altera_pcie_retrain(pcie); +} + static int altera_pcie_probe(struct platform_device *pdev) { struct altera_pcie *pcie; @@ -592,6 +622,7 @@ static int altera_pcie_probe(struct platform_device *pdev) cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS); /* enable all interrupts */ cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE); + altera_pcie_host_init(pcie); bus = pci_scan_root_bus(&pdev->dev, pcie->root_bus_nr, &altera_pcie_ops, pcie, &pcie->resources);