All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme: Do a clean NVMe shutdown
@ 2022-07-31  6:31 Hector Martin
  2022-07-31 11:11 ` Mark Kettenis
  2022-08-27 12:06 ` Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Hector Martin @ 2022-07-31  6:31 UTC (permalink / raw)
  To: Bin Meng; +Cc: Sven Peter, Mark Kettenis, u-boot, asahi, Hector Martin

The brute-force controller disable method can end up racing controller
initilization and causing a crash when we shut down Apple ANS2 NVMe
controllers. Do a proper controlled shutdown, which does block until
things are quiesced properly. This is nicer in general for all
controllers.

Signed-off-by: Hector Martin <marcan@marcan.st>
---
 drivers/nvme/nvme.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/drivers/nvme/nvme.c b/drivers/nvme/nvme.c
index a305305885ec..5fd2fb9ed6a6 100644
--- a/drivers/nvme/nvme.c
+++ b/drivers/nvme/nvme.c
@@ -27,9 +27,8 @@
 #define IO_TIMEOUT		30
 #define MAX_PRP_POOL		512
 
-static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
+static int nvme_wait_csts(struct nvme_dev *dev, u32 mask, u32 val)
 {
-	u32 bit = enabled ? NVME_CSTS_RDY : 0;
 	int timeout;
 	ulong start;
 
@@ -38,7 +37,7 @@ static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
 
 	start = get_timer(0);
 	while (get_timer(start) < timeout) {
-		if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
+		if ((readl(&dev->bar->csts) & mask) == val)
 			return 0;
 	}
 
@@ -295,7 +294,7 @@ static int nvme_enable_ctrl(struct nvme_dev *dev)
 	dev->ctrl_config |= NVME_CC_ENABLE;
 	writel(dev->ctrl_config, &dev->bar->cc);
 
-	return nvme_wait_ready(dev, true);
+	return nvme_wait_csts(dev, NVME_CSTS_RDY, NVME_CSTS_RDY);
 }
 
 static int nvme_disable_ctrl(struct nvme_dev *dev)
@@ -304,7 +303,16 @@ static int nvme_disable_ctrl(struct nvme_dev *dev)
 	dev->ctrl_config &= ~NVME_CC_ENABLE;
 	writel(dev->ctrl_config, &dev->bar->cc);
 
-	return nvme_wait_ready(dev, false);
+	return nvme_wait_csts(dev, NVME_CSTS_RDY, 0);
+}
+
+static int nvme_shutdown_ctrl(struct nvme_dev *dev)
+{
+	dev->ctrl_config &= ~NVME_CC_SHN_MASK;
+	dev->ctrl_config |= NVME_CC_SHN_NORMAL;
+	writel(dev->ctrl_config, &dev->bar->cc);
+
+	return nvme_wait_csts(dev, NVME_CSTS_SHST_MASK, NVME_CSTS_SHST_CMPLT);
 }
 
 static void nvme_free_queue(struct nvme_queue *nvmeq)
@@ -904,6 +912,13 @@ free_nvme:
 int nvme_shutdown(struct udevice *udev)
 {
 	struct nvme_dev *ndev = dev_get_priv(udev);
+	int ret;
+
+	ret = nvme_shutdown_ctrl(ndev);
+	if (ret < 0) {
+		printf("Error: %s: Shutdown timed out!\n", udev->name);
+		return ret;
+	}
 
 	return nvme_disable_ctrl(ndev);
 }
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme: Do a clean NVMe shutdown
  2022-07-31  6:31 [PATCH] nvme: Do a clean NVMe shutdown Hector Martin
@ 2022-07-31 11:11 ` Mark Kettenis
  2022-08-27 12:06 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Kettenis @ 2022-07-31 11:11 UTC (permalink / raw)
  To: Hector Martin; +Cc: bmeng.cn, sven, kettenis, u-boot, asahi, marcan

> From: Hector Martin <marcan@marcan.st>
> Date: Sun, 31 Jul 2022 15:31:31 +0900
> 
> The brute-force controller disable method can end up racing controller
> initilization and causing a crash when we shut down Apple ANS2 NVMe
> controllers. Do a proper controlled shutdown, which does block until
> things are quiesced properly. This is nicer in general for all
> controllers.
> 
> Signed-off-by: Hector Martin <marcan@marcan.st>

Tested-by: Mark Kettenis <kettenis@openbsd.org> [firefly-rk3399]

> ---
>  drivers/nvme/nvme.c | 25 ++++++++++++++++++++-----
>  1 file changed, 20 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/nvme/nvme.c b/drivers/nvme/nvme.c
> index a305305885ec..5fd2fb9ed6a6 100644
> --- a/drivers/nvme/nvme.c
> +++ b/drivers/nvme/nvme.c
> @@ -27,9 +27,8 @@
>  #define IO_TIMEOUT		30
>  #define MAX_PRP_POOL		512
>  
> -static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
> +static int nvme_wait_csts(struct nvme_dev *dev, u32 mask, u32 val)
>  {
> -	u32 bit = enabled ? NVME_CSTS_RDY : 0;
>  	int timeout;
>  	ulong start;
>  
> @@ -38,7 +37,7 @@ static int nvme_wait_ready(struct nvme_dev *dev, bool enabled)
>  
>  	start = get_timer(0);
>  	while (get_timer(start) < timeout) {
> -		if ((readl(&dev->bar->csts) & NVME_CSTS_RDY) == bit)
> +		if ((readl(&dev->bar->csts) & mask) == val)
>  			return 0;
>  	}
>  
> @@ -295,7 +294,7 @@ static int nvme_enable_ctrl(struct nvme_dev *dev)
>  	dev->ctrl_config |= NVME_CC_ENABLE;
>  	writel(dev->ctrl_config, &dev->bar->cc);
>  
> -	return nvme_wait_ready(dev, true);
> +	return nvme_wait_csts(dev, NVME_CSTS_RDY, NVME_CSTS_RDY);
>  }
>  
>  static int nvme_disable_ctrl(struct nvme_dev *dev)
> @@ -304,7 +303,16 @@ static int nvme_disable_ctrl(struct nvme_dev *dev)
>  	dev->ctrl_config &= ~NVME_CC_ENABLE;
>  	writel(dev->ctrl_config, &dev->bar->cc);
>  
> -	return nvme_wait_ready(dev, false);
> +	return nvme_wait_csts(dev, NVME_CSTS_RDY, 0);
> +}
> +
> +static int nvme_shutdown_ctrl(struct nvme_dev *dev)
> +{
> +	dev->ctrl_config &= ~NVME_CC_SHN_MASK;
> +	dev->ctrl_config |= NVME_CC_SHN_NORMAL;
> +	writel(dev->ctrl_config, &dev->bar->cc);
> +
> +	return nvme_wait_csts(dev, NVME_CSTS_SHST_MASK, NVME_CSTS_SHST_CMPLT);
>  }
>  
>  static void nvme_free_queue(struct nvme_queue *nvmeq)
> @@ -904,6 +912,13 @@ free_nvme:
>  int nvme_shutdown(struct udevice *udev)
>  {
>  	struct nvme_dev *ndev = dev_get_priv(udev);
> +	int ret;
> +
> +	ret = nvme_shutdown_ctrl(ndev);
> +	if (ret < 0) {
> +		printf("Error: %s: Shutdown timed out!\n", udev->name);
> +		return ret;
> +	}
>  
>  	return nvme_disable_ctrl(ndev);
>  }
> -- 
> 2.35.1
> 
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme: Do a clean NVMe shutdown
  2022-07-31  6:31 [PATCH] nvme: Do a clean NVMe shutdown Hector Martin
  2022-07-31 11:11 ` Mark Kettenis
@ 2022-08-27 12:06 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2022-08-27 12:06 UTC (permalink / raw)
  To: Hector Martin; +Cc: Bin Meng, Sven Peter, Mark Kettenis, u-boot, asahi

[-- Attachment #1: Type: text/plain, Size: 537 bytes --]

On Sun, Jul 31, 2022 at 03:31:31PM +0900, Hector Martin wrote:

> The brute-force controller disable method can end up racing controller
> initilization and causing a crash when we shut down Apple ANS2 NVMe
> controllers. Do a proper controlled shutdown, which does block until
> things are quiesced properly. This is nicer in general for all
> controllers.
> 
> Signed-off-by: Hector Martin <marcan@marcan.st>
> Tested-by: Mark Kettenis <kettenis@openbsd.org> [firefly-rk3399]

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-27 12:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-31  6:31 [PATCH] nvme: Do a clean NVMe shutdown Hector Martin
2022-07-31 11:11 ` Mark Kettenis
2022-08-27 12:06 ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.