linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Efstathiades <john.efstathiades@pebblebay.com>
To: unlisted-recipients:; (no To-header on input)
Cc: UNGLinuxDriver@microchip.com, woojung.huh@microchip.com,
	davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org,
	linux-usb@vger.kernel.org, john.efstathiades@pebblebay.com
Subject: [PATCH net-next v2 06/10] lan78xx: Fix exception on link speed change
Date: Tue, 24 Aug 2021 19:56:09 +0100	[thread overview]
Message-ID: <20210824185613.49545-7-john.efstathiades@pebblebay.com> (raw)
In-Reply-To: <20210824185613.49545-1-john.efstathiades@pebblebay.com>

An exception is sometimes seen when the link speed is changed
from auto-negotiation to a fixed speed, or vice versa. The
exception occurs when the MAC is reset (due to the link speed
change) at the same time as the PHY state machine is accessing
a PHY register. The following changes fix this problem.

Rework the MAC reset to ensure there is no outstanding MDIO
register transaction before the reset and then wait until the
reset is complete before allowing any further MAC register access.

Signed-off-by: John Efstathiades <john.efstathiades@pebblebay.com>
---
 drivers/net/usb/lan78xx.c | 54 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 6 deletions(-)

diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c
index 1909d6003453..2eb853b13c2a 100644
--- a/drivers/net/usb/lan78xx.c
+++ b/drivers/net/usb/lan78xx.c
@@ -1163,6 +1163,52 @@ static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex,
 	return 0;
 }
 
+static int lan78xx_mac_reset(struct lan78xx_net *dev)
+{
+	unsigned long start_time = jiffies;
+	u32 val;
+	int ret;
+
+	mutex_lock(&dev->phy_mutex);
+
+	/* Resetting the device while there is activity on the MDIO
+	 * bus can result in the MAC interface locking up and not
+	 * completing register access transactions.
+	 */
+	ret = lan78xx_phy_wait_not_busy(dev);
+	if (ret < 0)
+		goto done;
+
+	ret = lan78xx_read_reg(dev, MAC_CR, &val);
+	if (ret < 0)
+		goto done;
+
+	val |= MAC_CR_RST_;
+	ret = lan78xx_write_reg(dev, MAC_CR, val);
+	if (ret < 0)
+		goto done;
+
+	/* Wait for the reset to complete before allowing any further
+	 * MAC register accesses otherwise the MAC may lock up.
+	 */
+	do {
+		ret = lan78xx_read_reg(dev, MAC_CR, &val);
+		if (ret < 0)
+			goto done;
+
+		if (!(val & MAC_CR_RST_)) {
+			ret = 0;
+			goto done;
+		}
+	} while (!time_after(jiffies, start_time + HZ));
+
+	ret = -ETIMEDOUT;
+done:
+	mutex_unlock(&dev->phy_mutex);
+
+	return ret;
+}
+
 static int lan78xx_link_reset(struct lan78xx_net *dev)
 {
 	struct phy_device *phydev = dev->net->phydev;
@@ -1184,12 +1230,8 @@ static int lan78xx_link_reset(struct lan78xx_net *dev)
 		dev->link_on = false;
 
 		/* reset MAC */
-		ret = lan78xx_read_reg(dev, MAC_CR, &buf);
-		if (unlikely(ret < 0))
-			return ret;
-		buf |= MAC_CR_RST_;
-		ret = lan78xx_write_reg(dev, MAC_CR, buf);
-		if (unlikely(ret < 0))
+		ret = lan78xx_mac_reset(dev);
+		if (ret < 0)
 			return ret;
 
 		del_timer(&dev->stat_monitor);
-- 
2.25.1


  parent reply	other threads:[~2021-08-24 18:57 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24 18:56 [PATCH net-next v2 00/10] LAN7800 driver improvements John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 01/10] lan78xx: Fix white space and style issues John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 02/10] lan78xx: Remove unused timer John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 03/10] lan78xx: Set flow control threshold to prevent packet loss John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 04/10] lan78xx: Remove unused pause frame queue John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 05/10] lan78xx: Add missing return code checks John Efstathiades
2021-08-24 18:56 ` John Efstathiades [this message]
2021-08-24 18:56 ` [PATCH net-next v2 07/10] lan78xx: Fix partial packet errors on suspend/resume John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 08/10] lan78xx: Fix race conditions in suspend/resume handling John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 09/10] lan78xx: Fix race condition in disconnect handling John Efstathiades
2021-08-24 18:56 ` [PATCH net-next v2 10/10] lan78xx: Limit number of driver warning messages John Efstathiades
2021-08-25 10:00 ` [PATCH net-next v2 00/10] LAN7800 driver improvements patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210824185613.49545-7-john.efstathiades@pebblebay.com \
    --to=john.efstathiades@pebblebay.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=davem@davemloft.net \
    --cc=kuba@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=woojung.huh@microchip.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).