Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp324474pxb; Sat, 11 Sep 2021 06:18:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwtBXC0blFG+2DBXip61MePOT3kJQlBEbxG1X4sQWrazIhNikyzPG/xbdCKah8zjGpiPalr X-Received: by 2002:a05:6402:48b:: with SMTP id k11mr3313187edv.7.1631366313981; Sat, 11 Sep 2021 06:18:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631366313; cv=none; d=google.com; s=arc-20160816; b=OD6s6h3fxwc3kUIMTSh6unPcsN/Hb506pwnmRReCW/LGyyoAXzFAc2EHQ6fW9a1J4H NH3Zr58WKwEw/PsSe0UqbX4SNq5c9p9ZguavnHSeCn/nfL7RX6xrQqXJqqvqIo+Ygu33 AY6wuXzEh5PYQNrMLzQ5hkseMWWunkBffaxxYF/fmmgYo8Izxc7ezudPTuRP0nN7NE94 i+A+gZxCoKzCNXms3oYTJYhWOQj/I53g1c+VHKnO+eYToXddoLsdu1UH2pjDF3rDdH+D s9hJz7KrrfzY8oO/IcNDnSIHpkn/0wJUP/op2IvPI/DIPuuaSKoVxqfwZDx6oyRld3SJ 8GSQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gtNxRTbiMnNQOAjYq77vXMX2C134JnmaPWHdFul7Bp4=; b=IMpxKpT1Jc3x1ijGxMfSZYN05mcs71T/H65/D8xqz7Wm8cSkd6+ndYsd1yuvpKnFjk EzkP5GmdZFOYmqDa05HesUOyN4J2+ov3Aiow9pACO8AZslAAF9AYU92z4gBfup1DUmSO yVOGD26F6AGmhEK1wCg2XO01H3QXOAeetcM1YVHvX08hp7ZkkCehtMJ6ZJPxzR+NQOMp oKCDboe7HRrgyIudGThORCfMHPk6lJ3cN9owrW1czfbng3NDmkeFIp5o9AhaW3SeCOH9 YgQFQRUjiWNLQ2Rqta8/9TXwTcyPmidxsLM8cClI9QcJhb4rzdo44cXCp0caxmdXRlWD hxMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AcUGIroI; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w10si2173664ejv.754.2021.09.11.06.18.09; Sat, 11 Sep 2021 06:18:33 -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=@kernel.org header.s=k20201202 header.b=AcUGIroI; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236864AbhIKNPN (ORCPT + 99 others); Sat, 11 Sep 2021 09:15:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:38202 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236352AbhIKNNo (ORCPT ); Sat, 11 Sep 2021 09:13:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 544D2611F2; Sat, 11 Sep 2021 13:12:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631365952; bh=b1NkeVqnYrPA3DmXYYRWWj0Gnvrnh3Atp/VKObJ4sGA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AcUGIroIXSG8W4ic4WkFK3gONpuWJuyrbHxyenZsflZWJqF3V++2spZ+JCCOlOh7u DhF/7Br4vxFFAAlJ7QHHlbEDICwkyFB7OYVgzF+H7cLozKLS0A+J1ORazSP+QYxtfg 14BHtlWhBMmGaXUjrDDgcUYnX2Sqy3J/wFU2wBNB8QQ0KFi2dpo3QCLZrelyYvZ+9S HH25qnf94E03XFdIa/3J4neiAxc2WDZB01i66m7Ks1GnIuctHtaG5sCSbqc0RdxSYN f75N31mnIGfpgZkX1qQtQ3tXi0JSSz7Z7HObEvkuQaBSfxxlFsTeTJLTaVYPDVyXny 1MB5svzkrf2jw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: "Russell King (Oracle)" , Joakim Zhang , "David S . Miller" , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 5.14 32/32] net: phylink: add suspend/resume support Date: Sat, 11 Sep 2021 09:11:49 -0400 Message-Id: <20210911131149.284397-32-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210911131149.284397-1-sashal@kernel.org> References: <20210911131149.284397-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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