linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Pankaj Raghav <pankydev8@gmail.com>
Cc: Christoph Hellwig <hch@lst.de>, Keith Busch <kbusch@kernel.org>,
	Sagi Grimberg <sagi@grimberg.me>,
	James Smart <james.smart@broadcom.com>,
	Chaitanya Kulkarni <kch@nvidia.com>,
	Hector Martin <marcan@marcan.st>, Sven Peter <sven@svenpeter.dev>,
	asahi@lists.linux.dev, linux-nvme@lists.infradead.org,
	Pankaj Raghav <p.raghav@samsung.com>
Subject: Re: [PATCH 2/9] nvme: use nvme_wait_ready in nvme_shutdown_ctrl
Date: Wed, 30 Nov 2022 14:57:49 +0100	[thread overview]
Message-ID: <20221130135749.GA4786@lst.de> (raw)
In-Reply-To: <20221129155718.poqztp4zudt47zmq@quentin>

On Tue, Nov 29, 2022 at 04:57:18PM +0100, Pankaj Raghav wrote:
> The timeout_jiffies calculation in nvme_shutdown_ctrl is:
> unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
> 
> Aren't we changing the timeout with this change to something
> different compared to what it was before for shutdown?

Indeed.  the timeout fields in the spec are in a different granularity
than the shutdown_timeout field.  This version should fix that:

---
From 2f862232c4428d57dcfdc3b332866a9b2743711d Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <hch@lst.de>
Date: Wed, 16 Nov 2022 08:54:26 +0100
Subject: nvme: use nvme_wait_ready in nvme_shutdown_ctrl

Refactor the code to wait for CSTS state changes so that it can be reused
by nvme_shutdown_ctrl.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/nvme/host/core.c | 38 ++++++++++++--------------------------
 1 file changed, 12 insertions(+), 26 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 7961e146bbb163..03b2e34dcf7249 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2252,16 +2252,17 @@ static const struct block_device_operations nvme_bdev_ops = {
 	.pr_ops		= &nvme_pr_ops,
 };
 
-static int nvme_wait_ready(struct nvme_ctrl *ctrl, u32 timeout, bool enabled)
+static int nvme_wait_ready(struct nvme_ctrl *ctrl, u32 mask, u32 val,
+		u32 timeout, const char *op)
 {
-	unsigned long timeout_jiffies = ((timeout + 1) * HZ / 2) + jiffies;
-	u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
+	unsigned long timeout_jiffies = jiffies + timeout * HZ;
+	u32 csts;
 	int ret;
 
 	while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
 		if (csts == ~0)
 			return -ENODEV;
-		if ((csts & NVME_CSTS_RDY) == bit)
+		if ((csts & mask) == val)
 			break;
 
 		usleep_range(1000, 2000);
@@ -2270,7 +2271,7 @@ static int nvme_wait_ready(struct nvme_ctrl *ctrl, u32 timeout, bool enabled)
 		if (time_after(jiffies, timeout_jiffies)) {
 			dev_err(ctrl->device,
 				"Device not ready; aborting %s, CSTS=0x%x\n",
-				enabled ? "initialisation" : "reset", csts);
+				op, csts);
 			return -ENODEV;
 		}
 	}
@@ -2297,8 +2298,8 @@ int nvme_disable_ctrl(struct nvme_ctrl *ctrl)
 
 	if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
 		msleep(NVME_QUIRK_DELAY_AMOUNT);
-
-	return nvme_wait_ready(ctrl, NVME_CAP_TIMEOUT(ctrl->cap), false);
+	return nvme_wait_ready(ctrl, NVME_CSTS_RDY, 0,
+			       (NVME_CAP_TIMEOUT(ctrl->cap) + 1) / 2, "reset");
 }
 EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
 
@@ -2363,14 +2364,13 @@ int nvme_enable_ctrl(struct nvme_ctrl *ctrl)
 	ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
 	if (ret)
 		return ret;
-	return nvme_wait_ready(ctrl, timeout, true);
+	return nvme_wait_ready(ctrl, NVME_CSTS_RDY, NVME_CSTS_RDY,
+			       (timeout + 1) / 2, "initialisation");
 }
 EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
 
 int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
 {
-	unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
-	u32 csts;
 	int ret;
 
 	ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
@@ -2379,22 +2379,8 @@ int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
 	ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
 	if (ret)
 		return ret;
-
-	while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
-		if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
-			break;
-
-		msleep(100);
-		if (fatal_signal_pending(current))
-			return -EINTR;
-		if (time_after(jiffies, timeout)) {
-			dev_err(ctrl->device,
-				"Device shutdown incomplete; abort shutdown\n");
-			return -ENODEV;
-		}
-	}
-
-	return ret;
+	return nvme_wait_ready(ctrl, NVME_CSTS_SHST_MASK, NVME_CSTS_SHST_CMPLT,
+			       ctrl->shutdown_timeout, "shutdown");
 }
 EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
 
-- 
2.30.2



  reply	other threads:[~2022-11-30 13:58 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-29 13:21 remove path cleanups Christoph Hellwig
2022-11-29 13:22 ` [PATCH 1/9] nvme-apple: fix controller shutdown in apple_nvme_disable Christoph Hellwig
2022-11-29 15:41   ` Hector Martin
2023-01-10 17:47     ` Janne Grunau
2023-01-11  4:09       ` Hector Martin
2023-01-11  9:30       ` Linux kernel regression tracking (#adding)
2022-12-06 10:37   ` Eric Curtin
2022-12-06 12:15   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 2/9] nvme: use nvme_wait_ready in nvme_shutdown_ctrl Christoph Hellwig
2022-11-29 15:57   ` Pankaj Raghav
2022-11-30 13:57     ` Christoph Hellwig [this message]
2022-11-30 14:27       ` Pankaj Raghav
2022-11-30 16:15         ` Keith Busch
2022-12-06 13:33         ` Christoph Hellwig
2022-11-29 13:22 ` [PATCH 3/9] nvme: merge nvme_shutdown_ctrl into nvme_disable_ctrl Christoph Hellwig
2022-11-29 15:44   ` Hector Martin
2022-12-06 12:18   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 4/9] nvme-pci: remove nvme_disable_admin_queue Christoph Hellwig
2022-12-06 10:38   ` Eric Curtin
2022-12-06 12:18   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 5/9] nvme-pci: remove nvme_pci_disable Christoph Hellwig
2022-12-06 10:39   ` Eric Curtin
2022-12-06 12:19   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 6/9] nvme-pci: cleanup nvme_suspend_queue Christoph Hellwig
2022-12-06 12:20   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 7/9] nvme-pci: rename nvme_disable_io_queues Christoph Hellwig
2022-12-06 12:21   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 8/9] nvme-pci: return early on ctrl state mismatch in nvme_reset_work Christoph Hellwig
2022-12-06 11:26   ` Keith Busch
2022-12-06 13:38     ` Christoph Hellwig
2022-12-06 12:21   ` Sagi Grimberg
2022-11-29 13:22 ` [PATCH 9/9] nvme-pci: split out a nvme_pci_ctrl_is_dead helper Christoph Hellwig
2022-12-06 12:23   ` Sagi Grimberg
2022-12-06  8:18 ` remove path cleanups Christoph Hellwig
2022-12-06 11:26 ` Keith Busch

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=20221130135749.GA4786@lst.de \
    --to=hch@lst.de \
    --cc=asahi@lists.linux.dev \
    --cc=james.smart@broadcom.com \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=marcan@marcan.st \
    --cc=p.raghav@samsung.com \
    --cc=pankydev8@gmail.com \
    --cc=sagi@grimberg.me \
    --cc=sven@svenpeter.dev \
    /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).