Netdev List
 help / color / mirror / Atom feed
From: Cyrill Gorcunov <gorcunov@gmail.com>
To: netdev@vger.kernel.org
Cc: Simon Horman <horms@kernel.org>,
	linux-kernel@vger.kernel.org, anthony.l.nguyen@intel.com,
	przemyslaw.kitszel@intel.com
Subject: Re: [PATCH v2] ice: Fix wrong dsn read in ice_adapter_put
Date: Mon, 18 May 2026 17:36:17 +0300	[thread overview]
Message-ID: <agsj4Rrexhkqao0c@grain> (raw)
In-Reply-To: <agrjuAlzsV0RqAwU@grain>

On Mon, May 18, 2026 at 01:02:32PM +0300, Cyrill Gorcunov wrote:
...
> 
> Thinking more I think I got what sashiko meant here: the pullout of the
> adapter when it been in recovery mode already, and reading register state
> is obviously incorrect here too, instead we have to save the state upon
> probing in some flag and use it later. I'll update the patch.

Here is an updated. Actually even in recovery mode the ice_deinit_devlink()
may call for rd32() with undefined result as

 | ice_health_deinit
 |  ice_config_health_events
 |    ice_sq_send_cmd_retry
 |      ice_sq_send_cmd

unless I miss something obvious... (i don't address this in the patch).
Please take a look once time permits.

---
From: Cyrill Gorcunov <gorcunov@gmail.com>
Subject: [PATCH v3] ice: Fix wrong dsn read in ice_adapter_put

When registering an adapter instance, we read the PCI configuration
space to fetch the DSN and generate an adapter index for lookups.

However, if the adapter has been physically unplugged, the PCI space
is no longer accessible. Reading it returns a zero value, which results
in either an incorrect adapter instance being put or the proper instance
not being put at all. To fix this, we will use the previously known
index instead.

Similar applies to recovery mode detection -- if card has been in
recovery mode and physically removed we're not allowed to read its
state from hardware registers but rather provide state flag for
this.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 drivers/net/ethernet/intel/ice/ice.h         |    1 +
 drivers/net/ethernet/intel/ice/ice_adapter.c |   18 +++++++++++++-----
 drivers/net/ethernet/intel/ice/ice_main.c    |    9 ++++++++-
 3 files changed, 22 insertions(+), 6 deletions(-)

Index: linux-tip.git/drivers/net/ethernet/intel/ice/ice.h
===================================================================
--- linux-tip.git.orig/drivers/net/ethernet/intel/ice/ice.h
+++ linux-tip.git/drivers/net/ethernet/intel/ice/ice.h
@@ -310,6 +310,7 @@ enum ice_pf_state {
 	ICE_PHY_INIT_COMPLETE,
 	ICE_FD_VF_FLUSH_CTX,		/* set at FD Rx IRQ or timeout */
 	ICE_AUX_ERR_PENDING,
+	ICE_FW_RECOVERY_MODE,	/* Firmware in recovery mode */
 	ICE_STATE_NBITS		/* must be last */
 };
 
Index: linux-tip.git/drivers/net/ethernet/intel/ice/ice_adapter.c
===================================================================
--- linux-tip.git.orig/drivers/net/ethernet/intel/ice/ice_adapter.c
+++ linux-tip.git/drivers/net/ethernet/intel/ice/ice_adapter.c
@@ -40,10 +40,8 @@ static u64 ice_adapter_index(struct pci_
 	}
 }
 
-static unsigned long ice_adapter_xa_index(struct pci_dev *pdev)
+static unsigned long xa_index_mangle(u64 index)
 {
-	u64 index = ice_adapter_index(pdev);
-
 #if BITS_PER_LONG == 64
 	return index;
 #else
@@ -51,6 +49,12 @@ static unsigned long ice_adapter_xa_inde
 #endif
 }
 
+static unsigned long ice_adapter_xa_index(struct pci_dev *pdev)
+{
+	u64 index = ice_adapter_index(pdev);
+	return xa_index_mangle(index);
+}
+
 static struct ice_adapter *ice_adapter_new(struct pci_dev *pdev)
 {
 	struct ice_adapter *adapter;
@@ -130,13 +134,17 @@ struct ice_adapter *ice_adapter_get(stru
  */
 void ice_adapter_put(struct pci_dev *pdev)
 {
+	const struct ice_pf *pf = pci_get_drvdata(pdev);
 	struct ice_adapter *adapter;
 	unsigned long index;
 
-	index = ice_adapter_xa_index(pdev);
+	if (WARN_ON(!pf->adapter))
+		return;
+
 	scoped_guard(mutex, &ice_adapters_mutex) {
+		index = xa_index_mangle(pf->adapter->index);
 		adapter = xa_load(&ice_adapters, index);
-		if (WARN_ON(!adapter))
+		if (WARN_ON(!adapter || adapter != pf->adapter))
 			return;
 		if (!refcount_dec_and_test(&adapter->refcount))
 			return;
Index: linux-tip.git/drivers/net/ethernet/intel/ice/ice_main.c
===================================================================
--- linux-tip.git.orig/drivers/net/ethernet/intel/ice/ice_main.c
+++ linux-tip.git/drivers/net/ethernet/intel/ice/ice_main.c
@@ -5121,6 +5121,8 @@ static int ice_probe_recovery_mode(struc
 
 	dev_err(dev, "Firmware recovery mode detected. Limiting functionality. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode\n");
 
+	set_bit(ICE_FW_RECOVERY_MODE, pf->state);
+
 	INIT_HLIST_HEAD(&pf->aq_wait_list);
 	spin_lock_init(&pf->aq_wait_lock);
 	init_waitqueue_head(&pf->aq_wait_queue);
@@ -5366,7 +5368,12 @@ static void ice_remove(struct pci_dev *p
 		msleep(100);
 	}
 
-	if (ice_is_recovery_mode(&pf->hw)) {
+	/*
+	 * .remove() may be called when adapter is physically
+	 * unplugged so we can't read the fw state but use
+	 * the flag instead.
+	 */
+	if (test_bit(ICE_FW_RECOVERY_MODE, pf->state)) {
 		ice_service_task_stop(pf);
 		scoped_guard(devl, priv_to_devlink(pf)) {
 			ice_deinit_devlink(pf);

      reply	other threads:[~2026-05-18 14:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13  9:09 [PATCH] ice: Fix wrong dsn read in ice_adapter_put Cyrill Gorcunov
2026-05-17 12:53 ` Simon Horman
2026-05-17 22:21   ` [PATCH v2] " Cyrill Gorcunov
2026-05-18 10:02     ` Cyrill Gorcunov
2026-05-18 14:36       ` Cyrill Gorcunov [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=agsj4Rrexhkqao0c@grain \
    --to=gorcunov@gmail.com \
    --cc=anthony.l.nguyen@intel.com \
    --cc=horms@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=przemyslaw.kitszel@intel.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