[PATCH 2/2] nvme: Handle shut down controllers during initialization

From: Hector Martin
Date: Tue Jan 10 2023 - 23:39:18 EST


According to the spec, controllers need an explicit reset to become
active again after a controller shutdown. Check for this state in
nvme_enable_ctrl and issue an explicit disable if required, which will
trigger the required reset.

Fixes: c76b8308e4c9 ("nvme-apple: fix controller shutdown in apple_nvme_disable")
Signed-off-by: Hector Martin <marcan@xxxxxxxxx>
---
drivers/nvme/host/core.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 7be562a4e1aa..84e5db192ff9 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2410,6 +2410,19 @@ int nvme_enable_ctrl(struct nvme_ctrl *ctrl)
if (ret)
return ret;

+ /*
+ * If the controller is enabled but shut down, we need to disable it to
+ * reset it and have it come up. If the controller has completed a
+ * shutdown and is disabled, then we need to clear the shutdown request
+ * and enable it in the same write to CC.
+ * See NVMe Base Spec 2.0c Figure 47.
+ */
+ if (ctrl->ctrl_config & NVME_CC_SHN_MASK && ctrl->ctrl_config & NVME_CC_ENABLE) {
+ ret = nvme_disable_ctrl(ctrl, false);
+ if (ret)
+ return ret;
+ }
+ ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
ctrl->ctrl_config |= NVME_CC_ENABLE;
ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
if (ret)
--
2.35.1