public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Paolo Abeni <pabeni@redhat.com>
To: Raju Rangoju <Raju.Rangoju@amd.com>, netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kuba@kernel.org,
	edumazet@google.com, davem@davemloft.net, andrew+netdev@lunn.ch,
	Thomas.Lendacky@amd.com, maxime.chevallier@bootlin.com
Subject: Re: [PATCH v3 net] amd-xgbe: synchronize KR training with device operations
Date: Tue, 7 Apr 2026 12:48:43 +0200	[thread overview]
Message-ID: <ea5ba55d-c212-402a-a977-93c442610d17@redhat.com> (raw)
In-Reply-To: <20260403072157.1042806-1-Raju.Rangoju@amd.com>

On 4/3/26 9:21 AM, Raju Rangoju wrote:
> +static bool xgbe_kr_training_in_progress(struct xgbe_prv_data *pdata)
> +{
> +	struct xgbe_phy_data *phy_data = pdata->phy_data;
> +	unsigned long kr_start, kr_end;
> +
> +	/* Only wait for KR training in specific conditions:
> +	 *  - Inphi re-driver is present, OR
> +	 *  - Currently in KR mode with autoneg enabled
> +	 */
> +	if (!xgbe_phy_port_is_inphi(pdata) &&
> +	    !(phy_data->cur_mode == XGBE_MODE_KR &&
> +	      pdata->phy.autoneg == AUTONEG_ENABLE))
> +		return false;
> +
> +	/* If training hasn't completed, ensure it actually started */
> +	kr_start = READ_ONCE(pdata->kr_start_time);
> +	if (!kr_start)
> +		return false;

AFAICS kr_start_time is set to the current jiffies value at
initialization time and 0 is a valid - even if unlikely - jiffies value.
The above test is false-positive prone.

> +
> +	/* Training is complete - no need to wait */
> +	if (READ_ONCE(pdata->an_result) == XGBE_AN_COMPLETE)
> +		return false;
> +
> +	kr_end = kr_start +
> +		msecs_to_jiffies(XGBE_AN_MS_TIMEOUT + XGBE_KRTR_TIME);
> +
> +	/* If we're already past the training window, it's not "in progress" */
> +	if (time_after(jiffies, kr_end))
> +		return false;

Sashiko notes wrap-around (32 bits systems) will lead to wrong return value.

> +
> +	return true;
> +}
> +
> +static void xgbe_wait_for_kr_training_inprogress(struct xgbe_prv_data *pdata)
> +{
> +	unsigned long kr_end;
> +
> +	if (!xgbe_kr_training_in_progress(pdata))
> +		return;
> +
> +	/* Don't block the auto-negotiation state machine work item */
> +	if (current_work() == &pdata->an_work)
> +		return;
> +
> +	kr_end = READ_ONCE(pdata->kr_start_time) +
> +		msecs_to_jiffies(XGBE_AN_MS_TIMEOUT + XGBE_KRTR_TIME);
> +
> +	/* Poll until training completes or the training window expires */
> +	while (time_before(jiffies, kr_end)) {
> +		if (READ_ONCE(pdata->an_result) == XGBE_AN_COMPLETE)
> +			break;
> +
> +		usleep_range(10000, 11000);
> +	}
> +}
> +
>  static void xgbe_phy_perform_ratechange(struct xgbe_prv_data *pdata,
> -					enum xgbe_mb_cmd cmd, enum xgbe_mb_subcmd sub_cmd)
> +					enum xgbe_mb_cmd cmd,
> +					enum xgbe_mb_subcmd sub_cmd)
>  {
>  	unsigned int s0 = 0;
>  	unsigned int wait;
> @@ -2104,6 +2171,13 @@ static void xgbe_phy_perform_ratechange(struct xgbe_prv_data *pdata,
>  	/* Disable PLL re-initialization during FW command processing */
>  	xgbe_phy_pll_ctrl(pdata, false);
>  
> +	/* Serialize firmware mailbox access.
> +	 * Protects entire command sequence including busy check, PLL control,
> +	 * and command execution. Uses explicit lock/unlock for compatibility
> +	 * with goto-based cleanup (per cleanup.h guidelines).
> +	 */
> +	mutex_lock(&pdata->mailbox_lock);

The comment is confusing, due the the `xgbe_phy_pll_ctrl()` invocation
just before acquiring the lock.

> +
>  	/* Log if a previous command did not complete */
>  	if (XP_IOREAD_BITS(pdata, XP_DRIVER_INT_RO, STATUS)) {
>  		netif_dbg(pdata, link, pdata->netdev,
> @@ -2115,7 +2189,7 @@ static void xgbe_phy_perform_ratechange(struct xgbe_prv_data *pdata,
>  	XP_SET_BITS(s0, XP_DRIVER_SCRATCH_0, COMMAND, cmd);
>  	XP_SET_BITS(s0, XP_DRIVER_SCRATCH_0, SUB_COMMAND, sub_cmd);
>  
> -	/* Issue the command */
> +	/* Issue the firmware command */
>  	XP_IOWRITE(pdata, XP_DRIVER_SCRATCH_0, s0);
>  	XP_IOWRITE(pdata, XP_DRIVER_SCRATCH_1, 0);
>  	XP_IOWRITE_BITS(pdata, XP_DRIVER_INT_REQ, REQUEST, 1);
> @@ -2123,11 +2197,13 @@ static void xgbe_phy_perform_ratechange(struct xgbe_prv_data *pdata,
>  	/* Wait for command to complete */
>  	wait = XGBE_RATECHANGE_COUNT;
>  	while (wait--) {
> -		if (!XP_IOREAD_BITS(pdata, XP_DRIVER_INT_RO, STATUS))
> +		if (!XP_IOREAD_BITS(pdata, XP_DRIVER_INT_RO, STATUS)) {
> +			mutex_unlock(&pdata->mailbox_lock);
>  			goto do_rx_adaptation;
> -
> +		}
>  		usleep_range(1000, 2000);
>  	}
> +	mutex_unlock(&pdata->mailbox_lock);
>  
>  	netif_dbg(pdata, link, pdata->netdev,
>  		  "firmware mailbox command did not complete\n");

A `xgbe_phy_rx_reset()` invocation will happen here, outside the lock.
Can that race? will that corrupt the nic status?

/P


      reply	other threads:[~2026-04-07 10:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-03  7:21 [PATCH v3 net] amd-xgbe: synchronize KR training with device operations Raju Rangoju
2026-04-07 10:48 ` Paolo Abeni [this message]

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=ea5ba55d-c212-402a-a977-93c442610d17@redhat.com \
    --to=pabeni@redhat.com \
    --cc=Raju.Rangoju@amd.com \
    --cc=Thomas.Lendacky@amd.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    /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