Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2354295pxb; Mon, 20 Sep 2021 19:53:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1fUSjOy73561dQDj+7FISj/jNuCqIvpIlhGe1VgIFa81X8tkcuSLYf8Ehd+wRSsNc/291 X-Received: by 2002:a17:906:348b:: with SMTP id g11mr2719659ejb.202.1632192814458; Mon, 20 Sep 2021 19:53:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632192814; cv=none; d=google.com; s=arc-20160816; b=g7JLTGn4Zp7u1Y/oDcR0hm3Qin+9CZgo6dNDtwSWHPhsrWGJCp1eTXugqIhA1kMOzy /1S+O/k5ZKr8yellbrWGJLDbEdo5753HTi0p9BtFTrWVq9v38GRKcBHhtiATUbBPo0uY sUH3SH7+C1KZEivQcYmdN/jM+E+5x1gt1Q4D8ukuXRDqvh0xd/GB765qF8yTfV/wMchR 0XWNUyIwRd7tjtm/ZsBEx5a+Shba3nbzKjv8Sm8nHFe0NCqky23UeK5MzP2WoUgdSEwy qXTyG6UIbzNRvg7fI49QAqlMJMOcA790efHltpJnSDGiP9HPR5Qx1W8+HrBZyZhPL4MO 93zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=c1GMqLLAKEka4479Pn9iP48i4jc47qJfa/QlogKM62U=; b=uuaelBS6FAihEcp6jQBPscVo5wa1SvccEqtngxW2rlwSCoRRBt6qoSXeQgsegQPmgn P4k5YHTfWBLTG02UmmUanGbwCdaHDkKFkNzbWTX/XwUV/r15hN8+T4TEplWZhTX0Jv9y 9mGnbYT153d85a4/jQaXt1AgKDxWiGXCYrWO/gssOR9wkHDYWu18WbTqilbvK7Nu2M+R inl0d5IfFNxGqctVjZiNkDEhC2ihyQIgt+F/KdZEGnNdxpv1AhvRxwV0j5HyIMK4pFo9 +OT0Ppvgu3cG6B01/0cL0wLGwHpX5rqtlReJpvpxwYSvvBOxXtxJRI7BArsqq/JGz9b1 T5Qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fxl+6GZW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si22249884edu.126.2021.09.20.19.53.11; Mon, 20 Sep 2021 19:53:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fxl+6GZW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384096AbhITSuJ (ORCPT + 99 others); Mon, 20 Sep 2021 14:50:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:33240 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1383998AbhITSqU (ORCPT ); Mon, 20 Sep 2021 14:46:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 178F16335F; Mon, 20 Sep 2021 17:33:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632159196; bh=m6Wm/qddtfW+l7ogkdQ6+MMFbsJpdP3j+V1APxkGLdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fxl+6GZWo4QUA153pM22GhuZOrWiSYHtQ3J3sS5wOIEyd1+hpSbniuqjpS4MbR29N u7JFq7qvtkeeJpGqrpI6Oj3pGFeVFavZSOp63kzX6xou529N1m2I5YvuPuZJoP+CAZ EXjYy6dYg8AvlQdBU+qT48bBS/H37Tl5m3P3f/SQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joakim Zhang , "Russell King (Oracle)" , "David S. Miller" , Sasha Levin Subject: [PATCH 5.14 120/168] net: phylink: add suspend/resume support Date: Mon, 20 Sep 2021 18:44:18 +0200 Message-Id: <20210920163925.591274966@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163921.633181900@linuxfoundation.org> References: <20210920163921.633181900@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Russell King (Oracle) [ Upstream commit f97493657c6372eeefe70faadd214bf31488c44e ] Joakim Zhang reports that Wake-on-Lan with the stmmac ethernet driver broke when moving the incorrect handling of mac link state out of mac_config(). This reason this breaks is because the stmmac's WoL is handled by the MAC rather than the PHY, and phylink doesn't cater for that scenario. This patch adds the necessary phylink code to handle suspend/resume events according to whether the MAC still needs a valid link or not. This is the barest minimum for this support. Reported-by: Joakim Zhang Tested-by: Joakim Zhang Signed-off-by: Russell King (Oracle) Signed-off-by: Joakim Zhang Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/phy/phylink.c | 82 +++++++++++++++++++++++++++++++++++++++ include/linux/phylink.h | 3 ++ 2 files changed, 85 insertions(+) diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index eb29ef53d971..42e5a681183f 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -33,6 +33,7 @@ enum { PHYLINK_DISABLE_STOPPED, PHYLINK_DISABLE_LINK, + PHYLINK_DISABLE_MAC_WOL, }; /** @@ -1281,6 +1282,9 @@ EXPORT_SYMBOL_GPL(phylink_start); * network device driver's &struct net_device_ops ndo_stop() method. The * network device's carrier state should not be changed prior to calling this * function. + * + * This will synchronously bring down the link if the link is not already + * down (in other words, it will trigger a mac_link_down() method call.) */ void phylink_stop(struct phylink *pl) { @@ -1300,6 +1304,84 @@ void phylink_stop(struct phylink *pl) } EXPORT_SYMBOL_GPL(phylink_stop); +/** + * phylink_suspend() - handle a network device suspend event + * @pl: a pointer to a &struct phylink returned from phylink_create() + * @mac_wol: true if the MAC needs to receive packets for Wake-on-Lan + * + * Handle a network device suspend event. There are several cases: + * - If Wake-on-Lan is not active, we can bring down the link between + * the MAC and PHY by calling phylink_stop(). + * - If Wake-on-Lan is active, and being handled only by the PHY, we + * can also bring down the link between the MAC and PHY. + * - If Wake-on-Lan is active, but being handled by the MAC, the MAC + * still needs to receive packets, so we can not bring the link down. + */ +void phylink_suspend(struct phylink *pl, bool mac_wol) +{ + ASSERT_RTNL(); + + if (mac_wol && (!pl->netdev || pl->netdev->wol_enabled)) { + /* Wake-on-Lan enabled, MAC handling */ + mutex_lock(&pl->state_mutex); + + /* Stop the resolver bringing the link up */ + __set_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state); + + /* Disable the carrier, to prevent transmit timeouts, + * but one would hope all packets have been sent. This + * also means phylink_resolve() will do nothing. + */ + netif_carrier_off(pl->netdev); + + /* We do not call mac_link_down() here as we want the + * link to remain up to receive the WoL packets. + */ + mutex_unlock(&pl->state_mutex); + } else { + phylink_stop(pl); + } +} +EXPORT_SYMBOL_GPL(phylink_suspend); + +/** + * phylink_resume() - handle a network device resume event + * @pl: a pointer to a &struct phylink returned from phylink_create() + * + * Undo the effects of phylink_suspend(), returning the link to an + * operational state. + */ +void phylink_resume(struct phylink *pl) +{ + ASSERT_RTNL(); + + if (test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state)) { + /* Wake-on-Lan enabled, MAC handling */ + + /* Call mac_link_down() so we keep the overall state balanced. + * Do this under the state_mutex lock for consistency. This + * will cause a "Link Down" message to be printed during + * resume, which is harmless - the true link state will be + * printed when we run a resolve. + */ + mutex_lock(&pl->state_mutex); + phylink_link_down(pl); + mutex_unlock(&pl->state_mutex); + + /* Re-apply the link parameters so that all the settings get + * restored to the MAC. + */ + phylink_mac_initial_config(pl, true); + + /* Re-enable and re-resolve the link parameters */ + clear_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state); + phylink_run_resolve(pl); + } else { + phylink_start(pl); + } +} +EXPORT_SYMBOL_GPL(phylink_resume); + /** * phylink_ethtool_get_wol() - get the wake on lan parameters for the PHY * @pl: a pointer to a &struct phylink returned from phylink_create() diff --git a/include/linux/phylink.h b/include/linux/phylink.h index afb3ded0b691..237291196ce2 100644 --- a/include/linux/phylink.h +++ b/include/linux/phylink.h @@ -451,6 +451,9 @@ void phylink_mac_change(struct phylink *, bool up); void phylink_start(struct phylink *); void phylink_stop(struct phylink *); +void phylink_suspend(struct phylink *pl, bool mac_wol); +void phylink_resume(struct phylink *pl); + void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *); int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *); -- 2.30.2