Skip to content

Commit 6cc834b

Browse files
committed
nvme: avoid bogus CRTO values
Some devices are reporting controller ready mode support, but return 0 for CRTO. These devices require a much higher time to ready than that, so they are failing to initialize after the driver starter preferring that value over CAP.TO. The spec requires that CAP.TO match the appropritate CRTO value, or be set to 0xff if CRTO is larger than that. This means that CAP.TO can be used to validate if CRTO is reliable, and provides an appropriate fallback for setting the timeout value if not. Use whichever is larger. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217863 Reported-by: Cláudio Sampaio <patola@gmail.com> Reported-by: Felix Yan <felixonmars@archlinux.org> Tested-by: Felix Yan <felixonmars@archlinux.org> Based-on-a-patch-by: Felix Yan <felixonmars@archlinux.org> Cc: stable@vger.kernel.org Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent dad651b commit 6cc834b

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

drivers/nvme/host/core.c

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,25 +2245,8 @@ int nvme_enable_ctrl(struct nvme_ctrl *ctrl)
22452245
else
22462246
ctrl->ctrl_config = NVME_CC_CSS_NVM;
22472247

2248-
if (ctrl->cap & NVME_CAP_CRMS_CRWMS) {
2249-
u32 crto;
2250-
2251-
ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CRTO, &crto);
2252-
if (ret) {
2253-
dev_err(ctrl->device, "Reading CRTO failed (%d)\n",
2254-
ret);
2255-
return ret;
2256-
}
2257-
2258-
if (ctrl->cap & NVME_CAP_CRMS_CRIMS) {
2259-
ctrl->ctrl_config |= NVME_CC_CRIME;
2260-
timeout = NVME_CRTO_CRIMT(crto);
2261-
} else {
2262-
timeout = NVME_CRTO_CRWMT(crto);
2263-
}
2264-
} else {
2265-
timeout = NVME_CAP_TIMEOUT(ctrl->cap);
2266-
}
2248+
if (ctrl->cap & NVME_CAP_CRMS_CRWMS && ctrl->cap & NVME_CAP_CRMS_CRIMS)
2249+
ctrl->ctrl_config |= NVME_CC_CRIME;
22672250

22682251
ctrl->ctrl_config |= (NVME_CTRL_PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
22692252
ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
@@ -2277,6 +2260,39 @@ int nvme_enable_ctrl(struct nvme_ctrl *ctrl)
22772260
if (ret)
22782261
return ret;
22792262

2263+
/* CAP value may change after initial CC write */
2264+
ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap);
2265+
if (ret)
2266+
return ret;
2267+
2268+
timeout = NVME_CAP_TIMEOUT(ctrl->cap);
2269+
if (ctrl->cap & NVME_CAP_CRMS_CRWMS) {
2270+
u32 crto, ready_timeout;
2271+
2272+
ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CRTO, &crto);
2273+
if (ret) {
2274+
dev_err(ctrl->device, "Reading CRTO failed (%d)\n",
2275+
ret);
2276+
return ret;
2277+
}
2278+
2279+
/*
2280+
* CRTO should always be greater or equal to CAP.TO, but some
2281+
* devices are known to get this wrong. Use the larger of the
2282+
* two values.
2283+
*/
2284+
if (ctrl->ctrl_config & NVME_CC_CRIME)
2285+
ready_timeout = NVME_CRTO_CRIMT(crto);
2286+
else
2287+
ready_timeout = NVME_CRTO_CRWMT(crto);
2288+
2289+
if (ready_timeout < timeout)
2290+
dev_warn_once(ctrl->device, "bad crto:%x cap:%llx\n",
2291+
crto, ctrl->cap);
2292+
else
2293+
timeout = ready_timeout;
2294+
}
2295+
22802296
ctrl->ctrl_config |= NVME_CC_ENABLE;
22812297
ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
22822298
if (ret)

0 commit comments

Comments
 (0)