nvmesensor: fix fw commit needs-reset check Previously we were checking status value directly with status code (SC) but we were not considering status code type (SCT) which was not correct. During SSD update, if the subsystem reset required, then the SSD returns 0x110 (272). But we were checking with 0x010 but we should check with 0x110 where SCT=1 and SC=0x10. The same applies to 0x10b and 0x111. src/NVMeError.cpp:translateLibNVMe handles this properly. Hence fixing the condition by also checking SCT. NVMeMiFake had the same check, so fix it too. Tested: A needs-reset commit (0x110) now reports RequireReset instead of Failed; an activation-prohibited commit (0x113) still reports Failed and logs the status. Full test logs are in b/578681755. Google-Bug-Id: 578681755 Change-Id: I98005675318e2aaee9019010434f9fdb3dd9f610 Signed-off-by: Munawar Hussain <munawarhussain@google.com>
diff --git a/src/NVMeIntf.hpp b/src/NVMeIntf.hpp index ea9e71c..298c7b9 100644 --- a/src/NVMeIntf.hpp +++ b/src/NVMeIntf.hpp
@@ -261,6 +261,13 @@ virtual ~NVMeBasicIntf() = default; }; +// Packs a Status Code Type and Status Code as in the NVMe status field, +// i.e. the (status & 0x7ff) of a libnvme return value. +constexpr int sctSc(int sct, int sc) +{ + return (sct << NVME_SCT_SHIFT) | sc; +} + class NVMeMiIntf { public:
diff --git a/src/NVMeMi.cpp b/src/NVMeMi.cpp index cdc5a49..5a4ba09 100644 --- a/src/NVMeMi.cpp +++ b/src/NVMeMi.cpp
@@ -2068,19 +2068,17 @@ switch (rc & 0x7ff) { case NVME_SC_SUCCESS: - case NVME_SC_FW_NEEDS_CONV_RESET: - case NVME_SC_FW_NEEDS_SUBSYS_RESET: - case NVME_SC_FW_NEEDS_RESET: + case sctSc(NVME_SCT_CMD_SPECIFIC, NVME_SC_FW_NEEDS_CONV_RESET): + case sctSc(NVME_SCT_CMD_SPECIFIC, NVME_SC_FW_NEEDS_SUBSYS_RESET): + case sctSc(NVME_SCT_CMD_SPECIFIC, NVME_SC_FW_NEEDS_RESET): boost::asio::post(self->io, [rc, cb{std::move(cb)}]() { cb({}, static_cast<nvme_status_field>(rc)); }); break; default: - std::string_view errMsg = - statusToString(static_cast<nvme_mi_resp_status>(rc)); lg2::error( - "[{ENDPOINT}]fail to nvme_mi_admin_fw_commit: {ERROR}", - "ENDPOINT", epDesc, "ERROR", errMsg.data()); + "[{ENDPOINT}]fail to nvme_mi_admin_fw_commit, status: {STATUS}", + "ENDPOINT", epDesc, "STATUS", lg2::hex, rc); boost::asio::post(self->io, [rc, cb{std::move(cb)}]() { cb(std::make_error_code(std::errc::bad_message), static_cast<nvme_status_field>(rc));
diff --git a/src/NVMeMiFake.hpp b/src/NVMeMiFake.hpp index 581c981..78debd5 100644 --- a/src/NVMeMiFake.hpp +++ b/src/NVMeMiFake.hpp
@@ -362,7 +362,7 @@ int rc = 1; if (rc < 0) { - lg2::error("fail to subsystem_health_status_poll: {ERROR}", + lg2::error("fail to nvme_mi_admin_fw_commit: {ERROR}", "ERROR", strerror(errno)); boost::asio::post(self->io, [cb{std::move(cb)}]() { cb(std::make_error_code(static_cast<std::errc>(errno)), @@ -374,9 +374,12 @@ { switch (rc & 0x7ff) { - case NVME_SC_FW_NEEDS_CONV_RESET: - case NVME_SC_FW_NEEDS_SUBSYS_RESET: - case NVME_SC_FW_NEEDS_RESET: + case sctSc(NVME_SCT_CMD_SPECIFIC, + NVME_SC_FW_NEEDS_CONV_RESET): + case sctSc(NVME_SCT_CMD_SPECIFIC, + NVME_SC_FW_NEEDS_SUBSYS_RESET): + case sctSc(NVME_SCT_CMD_SPECIFIC, + NVME_SC_FW_NEEDS_RESET): boost::asio::post(self->io, [rc, cb{std::move(cb)}]() { cb({}, static_cast<nvme_status_field>(rc)); @@ -386,7 +389,7 @@ std::string_view errMsg = statusToString( static_cast<nvme_mi_resp_status>(rc)); lg2::error( - "fail to subsystem_health_status_poll: {ERROR}", + "fail to nvme_mi_admin_fw_commit: {ERROR}", "ERROR", errMsg.data()); boost::asio::post(self->io, [rc, cb{std::move(cb)}]() {
diff --git a/tests/test_nvme_mi_recovery.cpp b/tests/test_nvme_mi_recovery.cpp index ec9ff30..082e137 100644 --- a/tests/test_nvme_mi_recovery.cpp +++ b/tests/test_nvme_mi_recovery.cpp
@@ -1208,6 +1208,57 @@ EXPECT_EQ(nvmeMiMockStaleCtrlOps, 0); } +// A firmware commit that succeeds but needs a reset to activate returns a +// Command Specific status (SCT 1): 0x10b, 0x110 or 0x111. adminFwCommit() +// must report it as success with that status, so NVMeController sets +// RequireReset, and keep reporting any other status as an error. +class FwCommitStatusTest : public testing::TestWithParam<std::pair<int, bool>> +{}; + +TEST_P(FwCommitStatusTest, classifiesStatus) +{ + const auto [commitStatus, isError] = GetParam(); + // A connected NVMeMi. + CancelFixture f; + // Result of the adminFwCommit() callback. + std::optional<std::pair<std::error_code, nvme_status_field>> result; + + mockNvmeMiAdminFwCommit = [commitStatus](nvme_mi_ctrl_t, + struct nvme_fw_commit_args*) { + return commitStatus; + }; + + boost::asio::post(f.io, [&]() { + f.start(); + f.testMi->adminFwCommit( + 0, NVME_FW_COMMIT_CA_REPLACE_AND_ACTIVATE, 2, false, + [&](const std::error_code& ec, nvme_status_field status) { + result.emplace(ec, status); + f.io.stop(); + }); + }); + f.io.run_for(std::chrono::seconds(2)); + mockNvmeMiAdminFwCommit = nullptr; + + ASSERT_TRUE(result.has_value()); + EXPECT_EQ(static_cast<bool>(result->first), isError); + EXPECT_EQ(static_cast<int>(result->second), commitStatus); +} + +INSTANTIATE_TEST_SUITE_P( + NVMeRecovery, FwCommitStatusTest, + testing::Values( + std::make_pair(0x000, false), // Success + std::make_pair(0x10b, false), // FW Needs Conventional Reset + std::make_pair(0x110, false), // FW Needs NVM Subsystem Reset + std::make_pair(0x111, false), // FW Needs Controller Level Reset + std::make_pair(0x4110, false), // FW Needs Subsystem Reset + DNR + std::make_pair(0x100, true), // Command Specific: CQ Invalid + std::make_pair(0x106, true), // Invalid Firmware Slot + std::make_pair(0x107, true), // Invalid Firmware Image + std::make_pair(0x010, true), // Generic: SGL Invalid Metadata + std::make_pair(0x011, true))); // Generic: SGL Invalid Type + // Unused, but required to link successfully std::unordered_map<std::string, void*> pluginLibMap = {};