Skip to content
Merged
273 changes: 267 additions & 6 deletions src/nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,125 @@ static void nvme_ans_drain(void)
printf("nvme: discarded %u stale ANS message%s\n", count, count == 1 ? "" : "s");
}

/* The firmware's start after a power loss: the run bit, then the HELLO it sends unasked. */
static bool nvme_ans_start_warm(void)
{
if (!rtkit_boot_warm(nvme_rtkit, BIT(0))) {
printf("nvme: no warm handshake (CPU status 0x%x)\n", asc_cpu_status(nvme_asc));
return false;
}
if (poll32(nvme_base + NVME_BOOT_STATUS, 0xffffffff, NVME_BOOT_STATUS_OK, USEC_PER_SEC) < 0) {
printf("nvme: BOOT_STATUS 0x%x after the handshake\n",
read32(nvme_base + NVME_BOOT_STATUS));
return false;
}
return true;
}

/*
* The firmware's power report may come late after a start from a reset, as after a power loss
* (rtkit_boot_warm()); a start that says HELLO and never reports fails rather than hanging.
*/
#define NVME_ANS_COLD_TIMEOUT_US (6 * USEC_PER_SEC)

/* The normal RTKit start, for a reset ANS: the run bit, INIT, then HELLO. */
static bool nvme_ans_start_cold(void)
{
if (!rtkit_boot_timed(nvme_rtkit, NVME_ANS_COLD_TIMEOUT_US)) {
printf("nvme: no cold handshake (CPU status 0x%x)\n", asc_cpu_status(nvme_asc));
return false;
}
if (poll32(nvme_base + NVME_BOOT_STATUS, 0xffffffff, NVME_BOOT_STATUS_OK, USEC_PER_SEC) < 0) {
printf("nvme: BOOT_STATUS 0x%x after the handshake\n",
read32(nvme_base + NVME_BOOT_STATUS));
return false;
}
return true;
}

/* The last failed start said HELLO and then went quiet (nvme_ans_silent()). */
static bool nvme_ans_went_quiet;
/* nvme_ans_reset_for_start() got as far as stopping ANS's CPU. */
static bool nvme_ans_stopped_for_reset;

/*
* Stop ANS's CPU and reset ANS alone through PMGR, after a power cycle of ANS and its PCIe block
* when asked, then put the SART entries back and take a fresh RTKit instance for the next start.
* A start that got far enough to grant the firmware buffers keeps them, as rtkit_free() does,
* and isn't retried.
*/
static bool nvme_ans_reset_for_start(bool power_cycle)
{
if (rtkit_has_owned_buffers(nvme_rtkit)) {
printf("nvme: the failed start granted ANS buffers; not starting it again\n");
return false;
}
/* Last look before the stop discards the mailbox: a quiet firmware that spoke isn't reset. */
if (nvme_ans_went_quiet && rtkit_can_recv(nvme_rtkit)) {
printf("nvme: ANS sent a message before its reset; not resetting it\n");
return false;
}
/*
* The CPU is stopped right after that look, and only then is the mailbox drained
* (rtkit_stop()): nothing it sent before the stop is dropped without being looked at.
*/
asc_cpu_stop(nvme_asc);
nvme_ans_stopped_for_reset = true;
if (rtkit_can_recv(nvme_rtkit))
printf("nvme: ANS sent a message as it was stopped; resetting it\n");
rtkit_stop(nvme_rtkit);
rtkit_free(nvme_rtkit);
nvme_rtkit = NULL;
if (power_cycle && (pcie_ans_power_off() || pcie_ans_power_on())) {
printf("nvme: couldn't power-cycle ANS\n");
return false;
}

u32 before = asc_cpu_status(nvme_asc);
int ret = pmgr_reset_held(nvme_die, "ANS");
/* Some machines call it ANS2, as nvme_ans_sleep_and_reset() notes. */
if (ret)
ret = pmgr_reset_held(nvme_die, "ANS2");
printf("nvme: ANS reset%s: CPU status 0x%x before, 0x%x after\n", ret ? " FAILED" : "", before,
asc_cpu_status(nvme_asc));
if (ret)
return false;
nvme_ans_drain();
if (!sart_set_entries(nvme_sart, nvme_sart_saved, nvme_sart_saved_cnt)) {
printf("nvme: couldn't restore the %d SART entries\n", nvme_sart_saved_cnt);
return false;
}

nvme_rtkit = rtkit_init("nvme", nvme_asc, NULL, NULL, nvme_sart, false);
return nvme_rtkit != NULL;
}

/*
* A failed start that got no HELLO at all, or HELLO and then nothing at all: no message of any
* kind during its IOP power wait, nor in the short grace after it (seen on the J713 with CPU
* status 0x6e). A firmware that reported a power state, sent anything, or got as far as
* BOOT_STATUS may be resuming (NAND work around these transitions, a slow power report) and isn't
* reset under it.
*/
static bool nvme_ans_silent(void)
{
if (!nvme_rtkit)
return false;
bool no_hello = rtkit_no_hello(nvme_rtkit);
nvme_ans_went_quiet = !no_hello && rtkit_silent_after_power_wait(nvme_rtkit);
return no_hello || nvme_ans_went_quiet;
}

/*
* Post-M4, ANS off: power, the SART entries back, then the firmware's start after a power loss.
*
* A firmware whose CPU was stopped without a reset since it last ran (an earlier owner's idle
* that the SoC reset caught halfway, a watchdog reset with ANS stopped for sleep) sends no HELLO
* after the run bit, then or later, and the state survives warm reboots. Such a start is made
* again from a reset ANS, warm and then cold: first after a reset of ANS alone, then after a
* power cycle of ANS and its PCIe block as well. Only a start that got no HELLO, or HELLO and no
* power report at all, is made again.
*/
static bool nvme_ans_power_on(void)
{
bool ok = !pcie_ans_power_on();
Expand All @@ -569,16 +688,39 @@ static bool nvme_ans_power_on(void)
nvme_ans_drain();
if (ok && !(ok = sart_set_entries(nvme_sart, nvme_sart_saved, nvme_sart_saved_cnt)))
printf("nvme: couldn't restore the %d SART entries\n", nvme_sart_saved_cnt);
if (ok && !(ok = rtkit_boot_warm(nvme_rtkit, BIT(0))))
printf("nvme: no warm handshake (CPU status 0x%x)\n", asc_cpu_status(nvme_asc));
if (ok && !(ok = poll32(nvme_base + NVME_BOOT_STATUS, 0xffffffff, NVME_BOOT_STATUS_OK,
USEC_PER_SEC) >= 0))
printf("nvme: BOOT_STATUS 0x%x after the handshake\n",
read32(nvme_base + NVME_BOOT_STATUS));
if (!ok) {
printf("nvme: ANS didn't start from off; it stays down until a reboot\n");
return false;
}

bool started = nvme_ans_start_warm();
for (int attempt = 0; !started && attempt < 2; attempt++) {
if (!nvme_ans_silent()) {
printf("nvme: ANS answered but didn't finish its start; not resetting it\n");
break;
}
printf("nvme: ANS didn't start from off (CPU status 0x%x); %s\n", asc_cpu_status(nvme_asc),
attempt ? "power-cycling and resetting it" : "resetting it");
if (!nvme_ans_reset_for_start(attempt > 0))
break;
started = nvme_ans_start_warm();
if (!started) {
if (!nvme_ans_silent()) {
printf("nvme: ANS answered but didn't finish its start; not resetting it\n");
break;
}
printf("nvme: no warm start after the reset; starting ANS cold\n");
if (!nvme_ans_reset_for_start(false))
break;
started = nvme_ans_start_cold();
}
if (started)
printf("nvme: ANS started from a reset\n");
}
if (!started) {
printf("nvme: ANS didn't start from off; it stays down until a reboot\n");
return false;
}
nvme_boot_ticks = get_ticks();
printf("nvme: ANS started from off\n");
return true;
Expand All @@ -601,6 +743,118 @@ bool nvme_io_failed(void)
return nvme_reboot_required || ioq.failed || adminq.failed;
}

/* Nothing at all arrives in ANS's mailbox for this long; it is only looked at, never read. */
static bool nvme_ans_mailbox_stays_empty(u32 usec)
{
u64 timeout = timeout_calculate(usec);

while (!timeout_expired(timeout))
if (rtkit_can_recv(nvme_rtkit))
return false;
return true;
}

#define NVME_ANS_RESET_GRACE_US (500 * 1000)

/*
* One failed start of ANS running from iBoot, made again from a reset: only when the firmware sent
* no HELLO, or HELLO and then nothing at all, the start granted it no buffers, and nothing arrives
* in the mailbox for another 0.5 s (a late HELLO stays queued and stops the reset; a start that
* went quiet already had that grace in rtkit_silent_after_power_wait()). Without this the boot
* stops with NVMe down, and the reboot that follows resets ANS as well.
*/
static bool nvme_ans_running_may_reset(void)
{
if (!nvme_ans_silent()) {
printf("nvme: ANS answered but didn't finish its start; not resetting it\n");
return false;
}
if (rtkit_has_owned_buffers(nvme_rtkit)) {
printf("nvme: the failed start granted ANS buffers; not starting it again\n");
return false;
}
if (!nvme_ans_went_quiet && !nvme_ans_mailbox_stays_empty(NVME_ANS_RESET_GRACE_US)) {
printf("nvme: ANS sent a message after its failed start; not resetting it\n");
return false;
}
return true;
}

/*
* Post-M4, ANS running from iBoot: its one RTKit handshake, with the IOP power report bounded as a
* start from off has it, so a firmware that says HELLO and then nothing fails rather than hangs.
*
* A start that nvme_ans_running_may_reset() lets go again is made from a reset of ANS alone, cold,
* as iBoot starts it at every boot, then once more after a power cycle of ANS and its PCIe block
* (nvme_ans_reset_for_start(), with iBoot's SART entries put back). A cold start asks this stage
* for new buffers, so nothing iBoot gave the firmware is used after the reset. *reset_taken says
* whether ANS's CPU was stopped for a reset: a failure after that keeps the queues and needs a
* reboot. A session that started from a reset is handed on as one that came running, like
* iBoot's, which was also started cold.
*/
static bool nvme_ans_boot_running(bool *reset_taken)
{
*reset_taken = false;
#ifdef NVME_TEST_RUNNING_FALLBACK
/*
* Test builds only: the reset path on a healthy ANS. No handshake; iBoot's session is idled
* instead, and reset only once the firmware acknowledged the idle.
*/
printf("nvme: test build: idling running ANS instead of its handshake\n");
if (!rtkit_idle(nvme_rtkit)) {
printf("nvme: test build: ANS didn't acknowledge idle; not resetting it\n");
return false;
}
bool started = false;
#else
bool started = rtkit_boot_bounded(nvme_rtkit, NVME_ANS_COLD_TIMEOUT_US);
#endif
if (started) {
nvme_boot_ticks = get_ticks();
if (poll32(nvme_base + NVME_BOOT_STATUS, 0xffffffff, NVME_BOOT_STATUS_OK, USEC_PER_SEC) <
0) {
printf("nvme: ANS did not boot correctly.\n");
return false;
}
return true;
}
#ifndef NVME_TEST_RUNNING_FALLBACK
if (!nvme_ans_running_may_reset())
return false;
#endif

/* iBoot's entries, put back after each reset; never reported as ANS powered off. */
nvme_sart_saved_cnt = sart_get_entries(nvme_sart, nvme_sart_saved, ARRAY_SIZE(nvme_sart_saved));
for (int attempt = 0; attempt < 2; attempt++) {
printf("nvme: running ANS didn't start (CPU status 0x%x); %s\n", asc_cpu_status(nvme_asc),
attempt ? "power-cycling and resetting it" : "resetting it");
if (attempt && !nvme_ans_running_may_reset())
break;
/* Last look just before the stop, which would discard a late HELLO. */
if (rtkit_can_recv(nvme_rtkit)) {
printf("nvme: ANS sent a message before its reset; not resetting it\n");
break;
}
/* nvme_ans_reset_for_start() looks once more, right before its stop, for both cases. */
nvme_ans_went_quiet = true;
nvme_ans_stopped_for_reset = false;
bool reset = nvme_ans_reset_for_start(attempt > 0);
*reset_taken = *reset_taken || nvme_ans_stopped_for_reset;
if (!reset)
break;
if (nvme_ans_start_cold()) {
nvme_boot_ticks = get_ticks();
/* The snapshot was for the resets only: ANS runs, and only a power-off saves it. */
nvme_sart_saved_cnt = 0;
printf("nvme: ANS started from a reset\n");
return true;
}
}
nvme_sart_saved_cnt = 0;
printf("nvme: ANS didn't start from a reset; reboot required\n");
return false;
}

bool nvme_init(void)
{
if (nvme_reboot_required) {
Expand Down Expand Up @@ -779,6 +1033,13 @@ bool nvme_init(void)
/* An older stage's running session is shut down first, as that stage should have. */
if ((earlier && !nvme_ans_power_off()) || !nvme_ans_power_on())
goto out_preserve;
} else if (nvme_type == NVME_T8132 && !nvme_secure_regs) {
bool reset_taken;
if (!nvme_ans_boot_running(&reset_taken)) {
if (reset_taken)
goto out_preserve;
goto out_shutdown;
}
} else {
if (!rtkit_boot(nvme_rtkit))
goto out_rtkit;
Expand Down
4 changes: 1 addition & 3 deletions src/pmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,6 @@ int pmgr_reset(int die, const char *name)
return pmgr_reset_device(die, dev);
}

#ifdef SPTM_VIRT
/*
* A device's reset, with the device held on for it (an idle one won't take a reset), then its
* target and auto-enable as they were.
Expand All @@ -868,11 +867,10 @@ int pmgr_reset_held(int die, const char *name)
if (!ret)
ret = pmgr_reset_device(die, dev);
mask32(addr, PMGR_AUTO_ENABLE | PMGR_PS_TARGET, was & (PMGR_AUTO_ENABLE | PMGR_PS_TARGET));
printf("pmgr: %d.%s 0x%x before, 0x%x after%s\n", die, dev->name, was, read32(addr),
printf("pmgr: %d.%.16s 0x%x before, 0x%x after%s\n", die, dev->name, was, read32(addr),
ret ? ", NOT reset" : "");
return ret;
}
#endif /* SPTM_VIRT */

uintptr_t pmgr_psreg_by_name(int die, const char *name)
{
Expand Down
Loading
Loading