Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5075126rwb; Sun, 13 Nov 2022 21:14:34 -0800 (PST) X-Google-Smtp-Source: AA0mqf4DUzIxQ8WDeuvy0OAAJFZs/qHNcOkYa0HBZBmXCiTX6ZWHl5VoPO0bjvHmErrfjQcoycg5 X-Received: by 2002:a05:6a00:3492:b0:56b:a80f:38d4 with SMTP id cp18-20020a056a00349200b0056ba80f38d4mr12597654pfb.12.1668402874423; Sun, 13 Nov 2022 21:14:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668402874; cv=none; d=google.com; s=arc-20160816; b=guOCvFvAKdNVhtPt0KS3h6REGO6njIFz6WRvO2zAtGo+sEnxgYmmmdnDBV9vSOk68t kY9nMuDPLwsO24x0hDYbFzYOFdO67medwjyru89yaQbU8z54HUTx/IHHqa2WNP2YiWVK FaznMMst9aZBOaBecnpKaBmPMBYwha+cbGx9olgqqki0uUhIplza7LznQ+UEzr20yb78 z76ZJPLlVmTtZhOybi23aFkQwUU/1VICgpwRqzml4D1gqJPyjjLQNPiIqXYlObope6ww UKHjoZmO42yu6D84grJhbQ/93eQszfSXElwWZvKoDDGv300D2deXT6ZREJZpKsuuOmFc JMaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=t32Vju5XIgtrJ4jgA9bvoDKcNVYN0sL+9zOrih8YQXY=; b=hh1PlUdL/VDaQsD/XTcoK0HaeppyUD4OPpzNDusuGgvHR643IItmf2NaLCmxVUApcb 3eKMHpY6YP4kdkqlHlI9/Zi5BHQlHgqpIcyoA/IzlxUatwuknzFedrcRftoIo12FFYMx ouNyVaNmoCUp3FCtmVZxaZoV8HVrOs4cbgBsteTK18NlIrhOG3VudhDSW+2fze9DASui 8M0hDOB9YF2jy4TUiao++Mc4JquOgqQzssCnPKERaID6L2/TRZ+9aF2EjpI0PiX3Ln+I 84uGe92QjkelIk4OmtuLfcqJHdJwAXNl0H6j1CpBAEQrJiHCiqSZoH1qhD/TP9xaT5Iu GPsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nathanrossi.com header.s=google header.b=CMA5IyCy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nathanrossi.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b128-20020a636786000000b004704321e058si9370182pgc.247.2022.11.13.21.14.22; Sun, 13 Nov 2022 21:14:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@nathanrossi.com header.s=google header.b=CMA5IyCy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nathanrossi.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235770AbiKNEA1 (ORCPT + 88 others); Sun, 13 Nov 2022 23:00:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235771AbiKNEAO (ORCPT ); Sun, 13 Nov 2022 23:00:14 -0500 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9DF115FF3 for ; Sun, 13 Nov 2022 20:00:09 -0800 (PST) Received: by mail-ed1-x532.google.com with SMTP id l11so15548094edb.4 for ; Sun, 13 Nov 2022 20:00:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nathanrossi.com; s=google; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=t32Vju5XIgtrJ4jgA9bvoDKcNVYN0sL+9zOrih8YQXY=; b=CMA5IyCyGJHr/SsizAbheGNFPyx9JCmnZqxdXIktai3oYWi5BkddRDpuDHV2etUgLZ b/RqScmBCWFae1tZja4l50ohZVPHtyTC9X2N37rzguLHL8PRT96Jqs8HT9rF2CWUISbN ZJtkcz+Xh06JRi0ndmcnEkN8gk+1wYE+DCQF/T2Db9h128kPVCPsHwowMPpMS/t3Days Up1WZRTWb6fC8Wu/ZEbGJhCH3HRSSq4lUDMRqiR4926bgi2lltptBiw68lodAGyLJPKZ gh2Lr1ptl5XDE/7GObR3Hp3S4gs8r0WmK4sXrHR/DuneK1hNa2VRY8cMGDj9XkXimx45 Ap+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t32Vju5XIgtrJ4jgA9bvoDKcNVYN0sL+9zOrih8YQXY=; b=5Bwb9FVdZAqRKML92ypfO7MdtlAVyU/7yfK18Dj/8oQDTyFks3vNdf5GZGohxKKndY czBnT+25wCMttcKFN1kxMhPy9yjDMBWzrm/5Lgl4E024BPyrzvuMYbjWpA0Lzii191vS SL505fTSt/pdmj1fxZmZrbau7vOP3saF4H2wHGkQsWcMjnh81fEZf0UDdAmeiuoKtKkd tIHL4AWTy/s1Fs2v+GKhwzDc8DpvO9gjbVNtUIyRI9+Axux4t6i1LoVt/Xh3sheAxy0v GtgePgbG0ng1It5c7Iukc8t2alckqZbceHUziAZaDAWot85pmkxsHx110sap2yygmEi5 Lq+Q== X-Gm-Message-State: ANoB5pno3tbHqVw0hPf2DMCVewKFGvz2Qoga35Z8ULVGbSrzkjn6KVVy vaVpNSKowp+vYdMrSp8a3Zn4AhsUtR+aypljVnZREg== X-Received: by 2002:a50:d485:0:b0:461:af68:9bcd with SMTP id s5-20020a50d485000000b00461af689bcdmr10222403edi.67.1668398408026; Sun, 13 Nov 2022 20:00:08 -0800 (PST) MIME-Version: 1.0 References: <20221108222944.GA504625@bhelgaas> <20221109173454.GA567052@bhelgaas> In-Reply-To: <20221109173454.GA567052@bhelgaas> From: Nathan Rossi Date: Mon, 14 Nov 2022 13:59:56 +1000 Message-ID: Subject: Re: [PATCH] PCI/ASPM: Wait for data link active after retraining To: Bjorn Helgaas Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Nathan Rossi , Bjorn Helgaas , "Maciej W. Rozycki" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 10 Nov 2022 at 03:34, Bjorn Helgaas wrote: > > [+cc Maciej for similar retrain issue] > > On Tue, Nov 08, 2022 at 04:29:44PM -0600, Bjorn Helgaas wrote: > > On Thu, Jun 02, 2022 at 06:55:44AM +0000, Nathan Rossi wrote: > > > From: Nathan Rossi > > > > > > When retraining the link either the child or the parent device may ha= ve > > > the data link layer state machine of the respective devices move out = of > > > the active state despite the physical link training being completed. > > > Depending on how long is takes for the devices to return to the activ= e > > > state, the device may not be ready and any further reads/writes to th= e > > > device can fail. > > > > > > This issue is present with the pci-mvebu controller paired with a dev= ice > > > supporting ASPM but without advertising the Slot Clock, where during > > > boot the pcie_aspm_cap_init call would cause common clocks to be made > > > consistent and then retrain the link. However the data link layer wou= ld > > > not be active before any device initialization (e.g. ASPM capability > > > queries, BAR configuration) causing improper configuration of the dev= ice > > > without error. > > > > > > To ensure the child device is accessible, after the link retraining u= se > > > pcie_wait_for_link to perform the associated state checks and any nee= ded > > > delays. > > > > > > Signed-off-by: Nathan Rossi > > > --- > > > drivers/pci/pcie/aspm.c | 3 ++- > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c > > > index a96b7424c9..4b8a1810be 100644 > > > --- a/drivers/pci/pcie/aspm.c > > > +++ b/drivers/pci/pcie/aspm.c > > > @@ -288,7 +288,8 @@ static void pcie_aspm_configure_common_clock(stru= ct pcie_link_state *link) > > > reg16 &=3D ~PCI_EXP_LNKCTL_CCC; > > > pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16); > > > > > > - if (pcie_retrain_link(link)) > > > + /* Retrain link and then wait for the link to become active */ > > > + if (pcie_retrain_link(link) && pcie_wait_for_link(parent, true)) > > > > pcie_retrain_link() waits for PCI_EXP_LNKSTA_LT (Link Training) to be > > cleared, which means the LTSSM has exited the Configuration/Recovery > > state. pcie_wait_for_link() waits for PCI_EXP_LNKSTA_DLLLA (Data Link > > Layer Link Active) to be set, which means the link is in DL_Active. > > > > I don't see an explicit procedure in the spec for determining when > > a link retrain is complete, but from PCIe r6.0, sec 6.2.11 (DPC): > > > > After software releases the Downstream Port from DPC, the Port=E2=80= =99s > > LTSSM must transition to the Detect state, where the Link will > > attempt to retrain. Software can use Data Link Layer State Changed > > interrupts, DL_ACTIVE ERR_COR signaling, or both, to signal when the > > Link reaches the DL_Active state again. > > > > and sec 6.6: > > > > On the completion of Link Training (entering the DL_Active state, > > see Section 3.2), a component must be able to receive and process > > TLPs and DLLPs. > > > > The only use mentioned in the spec for the Link Training bit is the > > implementation note in sec 7.5.3.7 about avoiding race conditions when > > using the Retrain Link bit, where software should poll Link Training > > until it returns to zero before setting the Retrain Link bit to change > > link parameters. > > > > And I think you're absolutely right that what we *want* here is the > > data link layer DL_Active state, not just the link layer L0 state. > > > > This all makes me think that checking the Link Training bit might be > > the wrong thing to begin with. > > > > Of course, the Data Link Layer Link Active bit wasn't added until PCIe > > r1.1, and even now it's optional. Without it, I don't know if there's > > a way to make sure the link is in DL_Active. My understanding is there is no way to check for the DL_Active state on these devices. Which is why pcie_wait_for_link_delay uses a fixed delay in that case. > > > > Maybe pcie_retrain_link() should wait for Data Link Layer Link Active > > if it is supported, and use the existing behavior of waiting for Link > > Training to be cleared otherwise? I think it still makes sense for pcie_retrain_link to wait for the Link Training bit, since it is typical for the retraining to never go to a LTSSM state where the LinkUp is 0 (e.g. Recovery -> Configuration), thus the Data Link Layer will stay in DL_Active. But it is still important to wait for the link training to complete in that case, but also because the training may not cause the Data Link state to change immediately. However since DLLLA reporting is optional, it is probably ideal if the pcie_retrain_link only calls pcie_wait_for_link if reporting is available? This would avoid the fixed delay of 1s upon link retraining for devices without reporting, which is unnecessary in the majority of cases. Thanks, Nathan > > Nathan, I meant to cc you on this thread, which is doing something > very similar. Just FYI: > > https://lore.kernel.org/all/alpine.DEB.2.21.2209130050380.60554@angie.orc= am.me.uk/