diff --git a/src/nvme.c b/src/nvme.c index 12ecf3705..c6fc386e6 100644 --- a/src/nvme.c +++ b/src/nvme.c @@ -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(); @@ -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; @@ -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) { @@ -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; diff --git a/src/pmgr.c b/src/pmgr.c index 20ae16d7f..55a08e3f8 100644 --- a/src/pmgr.c +++ b/src/pmgr.c @@ -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. @@ -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) { diff --git a/src/rtkit.c b/src/rtkit.c index 5a6cda060..614e44efb 100644 --- a/src/rtkit.c +++ b/src/rtkit.c @@ -119,6 +119,15 @@ struct rtkit_dev { bool crashed; /* The last boot attempt got no HELLO after the wakeup. */ bool no_hello; + /* + * The last boot attempt got HELLO, then its IOP power wait timed out with no message of any + * kind read during the wait and no crash. + */ + bool iop_wait_silent; + /* An IOP power report came during the current boot attempt. */ + bool iop_power_reported; + /* Messages read from the mailbox, of any kind. */ + u32 rx_count; /* Started warm: the firmware keeps the buffers an earlier owner gave it. */ bool inherited; @@ -555,6 +564,7 @@ int rtkit_recv(rtkit_dev_t *rtk, struct rtkit_message *msg) return -1; while (asc_recv(rtk->asc, &asc_msg)) { + rtk->rx_count++; if (asc_msg.msg1 >= 0x100) { rtkit_printf("WARNING: received message for invalid endpoint %x >= 0x100\n", asc_msg.msg1); @@ -574,6 +584,7 @@ int rtkit_recv(rtkit_dev_t *rtk, struct rtkit_message *msg) switch (msgtype) { case MGMT_MSG_IOP_PWR_STATE_ACK: rtk->iop_power = FIELD_GET(MGMT_PWR_STATE, msg->msg); + rtk->iop_power_reported = true; /* A warm start's only progress report: say what came and when. */ if (rtk->inherited) rtkit_printf("IOP power state %#x\n", rtk->iop_power); @@ -680,10 +691,14 @@ bool rtkit_start_ep(rtkit_dev_t *rtk, u8 ep) } static bool rtkit_wait_for_power(rtkit_dev_t *rtk, enum rtkit_power_state *state, - enum rtkit_power_state target, u32 timeout_usec, const char *name) + enum rtkit_power_state target, u32 timeout_usec, const char *name, + bool *timed_out) { u64 timeout = timeout_usec ? timeout_calculate(timeout_usec) : 0; + if (timed_out) + *timed_out = false; + while (*state != target) { struct rtkit_message rtk_msg; int ret = rtkit_recv(rtk, &rtk_msg); @@ -694,9 +709,14 @@ static bool rtkit_wait_for_power(rtkit_dev_t *rtk, enum rtkit_power_state *state else if (ret < 0) return false; + /* A report read just before the deadline counts. */ + if (*state == target) + break; if (timeout && timeout_expired(timeout)) { rtkit_printf("timed out waiting for %s power state %#x (last %#x)\n", name, target, *state); + if (timed_out) + *timed_out = true; return false; } } @@ -704,16 +724,25 @@ static bool rtkit_wait_for_power(rtkit_dev_t *rtk, enum rtkit_power_state *state return true; } +/* The IOP power wait gave up on its timeout, with nothing read since it began. */ +static bool rtkit_iop_wait_was_silent(const rtkit_dev_t *rtk, bool timed_out, u32 rx_before) +{ + return timed_out && !rtk->iop_power_reported && !rtk->crashed && rtk->rx_count == rx_before; +} + /* * Warm: after a power cycle under kept firmware state. The firmware says HELLO by itself, the * endpoint map is answered as macOS does after a wake (bit 1 added to every part but the last, * the last masked by warm_last_mask) and no endpoint is started again. */ -static bool rtkit_boot_internal(rtkit_dev_t *rtk, u32 timeout_usec, bool warm, u32 warm_last_mask) +static bool rtkit_boot_internal(rtkit_dev_t *rtk, u32 timeout_usec, bool warm, u32 warm_last_mask, + bool wait_ap) { struct asc_message msg; rtk->no_hello = false; + rtk->iop_wait_silent = false; + rtk->iop_power_reported = false; /* After a power loss macOS turns the receive side on before the run bit (T6050 cp9d). */ if (warm) @@ -860,8 +889,13 @@ static bool rtkit_boot_internal(rtkit_dev_t *rtk, u32 timeout_usec, bool warm, u (has_oslog && !rtkit_start_ep(rtk, RTKIT_EP_OSLOG)))) return false; - if (!rtkit_wait_for_power(rtk, &rtk->iop_power, RTKIT_POWER_ON, timeout_usec, "IOP")) + u32 rx_before = rtk->rx_count; + bool timed_out; + if (!rtkit_wait_for_power(rtk, &rtk->iop_power, RTKIT_POWER_ON, timeout_usec, "IOP", + &timed_out)) { + rtk->iop_wait_silent = rtkit_iop_wait_was_silent(rtk, timed_out, rx_before); return false; + } /* this enables syslog */ msg.msg0 = @@ -872,8 +906,8 @@ static bool rtkit_boot_internal(rtkit_dev_t *rtk, u32 timeout_usec, bool warm, u return false; } - if (timeout_usec && - !rtkit_wait_for_power(rtk, &rtk->ap_power, RTKIT_POWER_ON, timeout_usec, "AP")) + if (wait_ap && + !rtkit_wait_for_power(rtk, &rtk->ap_power, RTKIT_POWER_ON, timeout_usec, "AP", NULL)) return false; return true; @@ -910,7 +944,21 @@ bool rtkit_get_live_state(rtkit_dev_t *rtk, struct rtkit_live_state *st) bool rtkit_boot(rtkit_dev_t *rtk) { - return rtkit_boot_internal(rtk, 0, false, 0); + return rtkit_boot_internal(rtk, 0, false, 0, false); +} + +/* + * rtkit_boot() with the IOP power report bounded: a firmware that says HELLO and then nothing + * fails the start rather than hanging it. Nothing else changes: the AP power state isn't waited + * for, and later calls keep their default timeouts. + */ +bool rtkit_boot_bounded(rtkit_dev_t *rtk, u32 iop_timeout_usec) +{ + if (!iop_timeout_usec) { + rtkit_printf("bounded boot requires a nonzero timeout\n"); + return false; + } + return rtkit_boot_internal(rtk, iop_timeout_usec, false, 0, false); } /* @@ -923,7 +971,7 @@ bool rtkit_boot(rtkit_dev_t *rtk) bool rtkit_boot_warm(rtkit_dev_t *rtk, u32 last_mask) { rtk->inherited = true; - return rtkit_boot_internal(rtk, RTKIT_WARM_TIMEOUT_US, true, last_mask); + return rtkit_boot_internal(rtk, RTKIT_WARM_TIMEOUT_US, true, last_mask, true); } /* Boot the IOP, or adopt the session of one a previous stage left running. Such @@ -954,7 +1002,38 @@ bool rtkit_boot_timed(rtkit_dev_t *rtk, u32 timeout_usec) } rtk->timeout_usec = timeout_usec; - return rtkit_boot_internal(rtk, timeout_usec, false, 0); + return rtkit_boot_internal(rtk, timeout_usec, false, 0, true); +} + +/* The last start failed because the firmware sent no HELLO: it never answered at all. */ +bool rtkit_no_hello(const rtkit_dev_t *rtk) +{ + return rtk->no_hello; +} + +#define RTKIT_SILENT_GRACE_US (500 * 1000) + +/* + * For a caller that would restart an IOP that went quiet: the last start got HELLO, then its IOP + * power wait timed out with nothing at all read during it, and nothing arrives in the mailbox for + * another 0.5 s either. The mailbox is only looked at, never read, so a late message stays queued + * and nothing is handled (no buffer granted) on the way to a restart. Only the caller that asks + * pays for the wait; a failed boot itself is unchanged. + */ +bool rtkit_silent_after_power_wait(rtkit_dev_t *rtk) +{ + if (!rtk->iop_wait_silent) + return false; + + u64 timeout = timeout_calculate(RTKIT_SILENT_GRACE_US); + while (!timeout_expired(timeout)) { + if (rtkit_can_recv(rtk)) { + rtkit_printf("IOP sent a message after its power wait; not silent\n"); + rtk->iop_wait_silent = false; + return false; + } + } + return !rtk->crashed; } u32 rtkit_get_timeout(rtkit_dev_t *rtk) diff --git a/src/rtkit.h b/src/rtkit.h index 966b5afb0..fb4e41076 100644 --- a/src/rtkit.h +++ b/src/rtkit.h @@ -62,8 +62,11 @@ bool rtkit_has_owned_buffers(const rtkit_dev_t *rtk); bool rtkit_start_ep(rtkit_dev_t *rtk, u8 ep); bool rtkit_boot(rtkit_dev_t *rtk); +bool rtkit_boot_bounded(rtkit_dev_t *rtk, u32 iop_timeout_usec); bool rtkit_boot_or_adopt(rtkit_dev_t *rtk, bool *adopted); bool rtkit_boot_timed(rtkit_dev_t *rtk, u32 timeout_usec); +bool rtkit_no_hello(const rtkit_dev_t *rtk); +bool rtkit_silent_after_power_wait(rtkit_dev_t *rtk); u32 rtkit_get_timeout(rtkit_dev_t *rtk); bool rtkit_can_recv(rtkit_dev_t *rtk); diff --git a/tests/python/test_nvme_adoption.py b/tests/python/test_nvme_adoption.py index 5c68abf19..99e5b1baf 100644 --- a/tests/python/test_nvme_adoption.py +++ b/tests/python/test_nvme_adoption.py @@ -21,7 +21,8 @@ def test_adopted_queue_lifetime(tmp_path): qregs = source[source.index("static u64 nvme_qreg(u32 reg)"):source.index("static void nvme_log_entry_state")] # The post-M4 power off and power on around a stage's use of ANS. power = source[source.index("/* The ADT describes a post-M4 ANS"):source.index("/* A write or flush, timed:")] - init = source[source.index("bool nvme_init(void)"):source.index("void nvme_ensure_shutdown(void)")] + # nvme_init(), with the handshake with an ANS running from iBoot just before it. + init = source[source.index("/* Nothing at all arrives in ANS's mailbox"):source.index("void nvme_ensure_shutdown(void)")] # The M1-M3 sleep-and-reset helper that every legacy power-down goes through. sleep_reset = source[source.index("static bool nvme_ans_sleep_and_reset(void)"): source.index("static bool nvme_exec_command(")] @@ -172,6 +173,8 @@ def test_adopted_queue_lifetime(tmp_path): static rtkit_dev_t *rtkit_init(const char *name, asc_dev_t *a, void *d, void *i, sart_dev_t *s, bool sram) { return &rtkit; } static bool rtkit_boot(rtkit_dev_t *r) { boots++; started = failure != 8; return failure != 8; } +/* Post-M4 iBoot sessions: failure 8 is a firmware that answered and then failed, never restarted. */ +static bool rtkit_boot_bounded(rtkit_dev_t *r, u32 t) { assert(t); return rtkit_boot(r); } /* M1-M3 power-down order, one letter per step: S sleep acked, F sleep failed, X stop, R reset. */ static char events[64]; static bool sleep_fails; @@ -187,6 +190,16 @@ def test_adopted_queue_lifetime(tmp_path): static bool rtkit_quiesce(rtkit_dev_t *r) { assert(r); quiesces++; return true; } static void rtkit_free(rtkit_dev_t *r) {} static void pmgr_reset(u8 die, const char *name) { resets++; event('R'); } +/* The post-M4 start from off always works here (rtkit_boot_warm()), so it is never retried. */ +static bool rtkit_has_owned_buffers(const rtkit_dev_t *r) { return false; } +static int pmgr_reset_held(u8 die, const char *name) { assert(0); return -1; } +static bool rtkit_boot_timed(rtkit_dev_t *r, u32 t) { assert(0); return false; } +static bool rtkit_no_hello(const rtkit_dev_t *r) { assert(failure == 8); return false; } +static bool rtkit_silent_after_power_wait(rtkit_dev_t *r) { assert(failure == 8); return false; } +static bool rtkit_can_recv(rtkit_dev_t *r) { assert(0); return false; } +/* Only the grace before a reset of a running ANS waits on time, and no case here reaches it. */ +static u64 timeout_calculate(u32 usec) { assert(0); return 0; } +static bool timeout_expired(u64 t) { assert(0); return true; } static int poll32(u64 a, u32 mask, u32 value, unsigned timeout) { return 0; } static bool nvme_ctrl_disable(void) { disable_calls++; return failure != 2 && !(shutdown_fault == 4 && disable_calls == 2); } diff --git a/tests/python/test_nvme_ans_running_restart.py b/tests/python/test_nvme_ans_running_restart.py new file mode 100644 index 000000000..5c231a8e1 --- /dev/null +++ b/tests/python/test_nvme_ans_running_restart.py @@ -0,0 +1,164 @@ +# SPDX-License-Identifier: GPL-2.0-only +"""A post-M4 ANS running from iBoot: its handshake's IOP power report is bounded, and a firmware that +never answers, or answers and then sends nothing, is reset and started cold (then once more after a +power cycle). A firmware that answered and got further, was granted buffers, or sent anything in the +grace after its failed start is left alone, untouched. The test build idles iBoot's session instead +of its handshake and resets it only after an acknowledged idle.""" + +from pathlib import Path +import os +import subprocess + +import pytest + +from host_compiler import host_cc + +# name: (first handshake: "ok" | "no_hello" | "quiet" | "answered", BOOT_STATUS fails after it, +# cold starts with no HELLO, cold start answers and fails, buffers granted, message queued +# before the reset, reset fails, expected) +CASES = { + "clean": ("ok", 0, 0, 0, 0, 0, 0, "ok boots=1 resets=0 cycles=0 restarted=0 saved=0"), + "boot-status-fails": ("ok", 1, 0, 0, 0, 0, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + # iBoot's SART entries are kept for the resets only: never reported as ANS powered off after. + "no-hello": ("no_hello", 0, 0, 0, 0, 0, 0, "ok boots=2 resets=1 cycles=0 restarted=1 saved=0"), + "quiet": ("quiet", 0, 0, 0, 0, 0, 0, "ok boots=2 resets=1 cycles=0 restarted=1 saved=0"), + "after-power-cycle": ("no_hello", 0, 1, 0, 0, 0, 0, "ok boots=3 resets=2 cycles=1 restarted=1 saved=0"), + "never": ("no_hello", 0, 99, 0, 0, 0, 0, "fail boots=3 resets=2 cycles=1 restarted=1 saved=0"), + "cold-answers": ("no_hello", 0, 0, 1, 0, 0, 0, "fail boots=2 resets=1 cycles=0 restarted=1 saved=0"), + "reset-fails": ("no_hello", 0, 0, 0, 0, 0, 1, "fail boots=1 resets=1 cycles=0 restarted=1 saved=0"), + # Left untouched: a firmware that answered, was granted buffers, or sent anything afterwards. + "answered": ("answered", 0, 0, 0, 0, 0, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + "buffers-granted": ("no_hello", 0, 0, 0, 1, 0, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + "no-hello-late-message": ("no_hello", 0, 0, 0, 0, 1, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + # A message that comes after the 0.5 s grace (3 looks) but before the stop still stops the reset: + # at the last look before the reset, or at nvme_ans_reset_for_start()'s own, right before its stop. + "no-hello-message-before-reset": ("no_hello", 0, 0, 0, 0, 4, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + "no-hello-message-at-stop": ("no_hello", 0, 0, 0, 0, 5, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + # Once the CPU is stopped the reset goes on; what it sent is drained only after the stop. + "no-hello-message-as-stopped": ("no_hello", 0, 0, 0, 0, 6, 0, "ok boots=2 resets=1 cycles=0 restarted=1 saved=0"), + "quiet-message-before-reset": ("quiet", 0, 0, 0, 0, 1, 0, "fail boots=1 resets=0 cycles=0 restarted=0 saved=0"), + # The test build: no handshake, iBoot's session idled, reset only after an acknowledged idle. + "test-build": ("test", 0, 0, 0, 0, 0, 0, "ok boots=1 resets=1 cycles=0 restarted=1 saved=0 idles=1"), + "test-build-no-idle-ack": ("test_noack", 0, 0, 0, 0, 0, 0, "fail boots=0 resets=0 cycles=0 restarted=0 saved=0 idles=1"), +} + + +@pytest.mark.parametrize("case", sorted(CASES)) +def test_ans_running_restart(tmp_path, case): + first, bs, cs, ch, own, late, rst, expected = CASES[case] + test_build = first.startswith("test") + source = Path(os.environ.get("NVME_SOURCE", Path(__file__).resolve().parents[2] / "src/nvme.c")).read_text() + start = source.index("/*\n * Discard what an earlier owner left in the receive mailbox") + body = source[start:source.index("/* A write or flush, timed:")] + start = source.index("/* Nothing at all arrives in ANS's mailbox") + body += source[start:source.index("bool nvme_init(void)\n{")] + harness = r''' +#include +#include +#include +#include +typedef uint8_t u8; +typedef uint32_t u32; +typedef uint64_t u64; +#define BIT(n) (1U << (n)) +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define USEC_PER_SEC 1000000 +#define NVME_BOOT_STATUS 0x1300 +#define NVME_BOOT_STATUS_OK 0xde71ce55 +typedef int asc_dev_t; +typedef int rtkit_dev_t; +typedef int sart_dev_t; +struct sart_entry { u64 a; }; +struct asc_message { u64 msg0; u32 msg1; }; +static asc_dev_t asc; +static rtkit_dev_t rtkit; +static sart_dev_t sart; +static asc_dev_t *nvme_asc = &asc; +static rtkit_dev_t *nvme_rtkit = &rtkit; +static sart_dev_t *nvme_sart = &sart; +static struct sart_entry nvme_sart_saved[16]; +static int nvme_sart_saved_cnt; +static u64 nvme_base, nvme_boot_ticks; +static u8 nvme_die; +static int boots, resets, cycles, poll_fails = @BS@, cold_fails = @CS@, cold_late = @CH@; +static bool no_hello, no_report; +/* A message is queued from this mailbox look on (1: from the start, 0: never). */ +static unsigned late_from = @LATE@, looks; +static u64 get_ticks(void) { return 1; } +static u32 read32(u64 a) { return 0; } +static u32 asc_cpu_status(asc_dev_t *a) { return 0x6e; } +static void asc_i2a_enable(asc_dev_t *a, bool on) {} +static bool asc_recv(asc_dev_t *a, struct asc_message *m) { return false; } +static int pcie_ans_power_on(void) { return 0; } +static int pcie_ans_power_off(void) { cycles++; return 0; } +static unsigned now, idles; +static u64 timeout_calculate(u32 usec) { return now + 3; } +static bool timeout_expired(u64 t) { return now >= t; } +static bool rtkit_idle(rtkit_dev_t *r) { idles++; return strcmp("@FIRST@", "test_noack"); } +static int sart_get_entries(sart_dev_t *s, struct sart_entry *e, int max) { return 3; } +static bool sart_set_entries(sart_dev_t *s, const struct sart_entry *e, int n) { return n == 3; } +static bool rtkit_boot_bounded(rtkit_dev_t *r, u32 timeout) +{ + boots++; + const char *first = "@FIRST@"; + no_hello = !strcmp(first, "no_hello"); + no_report = !strcmp(first, "quiet"); + return timeout && !strcmp(first, "ok"); +} +static bool rtkit_boot_warm(rtkit_dev_t *r, u32 mask) { boots++; return false; } +static bool rtkit_boot_timed(rtkit_dev_t *r, u32 timeout) +{ + boots++; + no_hello = cold_fails; + no_report = false; + if (!timeout) + return false; + if (cold_fails) { cold_fails--; return false; } + if (cold_late) { cold_late--; return false; } + return true; +} +static bool rtkit_no_hello(const rtkit_dev_t *r) { return no_hello; } +static bool rtkit_silent_after_power_wait(rtkit_dev_t *r) { return no_report; } +static bool rtkit_can_recv(rtkit_dev_t *r) { now++; return late_from && ++looks >= late_from; } +static bool rtkit_has_owned_buffers(const rtkit_dev_t *r) { return @OWN@; } +/* Order of the stop: the CPU first, then the drain (rtkit_stop()). */ +static bool cpu_stopped, drained_before_stop; +static void asc_cpu_stop(asc_dev_t *a) { cpu_stopped = true; } +static void rtkit_stop(rtkit_dev_t *r) { drained_before_stop |= !cpu_stopped; } +static void rtkit_free(rtkit_dev_t *r) {} +/* A fresh RTKit instance is a fresh start: the stop order is checked again for it. */ +static rtkit_dev_t *rtkit_init(const char *n, asc_dev_t *a, void *m, void *h, sart_dev_t *s, bool f) +{ cpu_stopped = false; return &rtkit; } +static int pmgr_reset_held(int die, const char *name) +{ if (!strcmp(name, "ANS2")) return -1; resets++; return @RST@; } +static int poll32(u64 a, u32 m, u32 v, unsigned t) +{ + if (!poll_fails) + return 0; + poll_fails--; + return -1; +} +''' + body + r''' +int main(void) +{ + bool restarted = true; + bool ok = nvme_ans_boot_running(&restarted); + printf("%s boots=%d resets=%d cycles=%d restarted=%d saved=%d%s", ok ? "ok" : "fail", boots, + resets, cycles, restarted, nvme_sart_saved_cnt, @TEST@ || drained_before_stop ? "" : "\n"); + if (drained_before_stop) + printf(" DRAINED-BEFORE-STOP%s", @TEST@ ? "" : "\n"); + if (@TEST@) + printf(" idles=%u\n", idles); + return 0; +} +''' + for k, v in (("@FIRST@", first), ("@BS@", bs), ("@CS@", cs), ("@CH@", ch), ("@OWN@", own), + ("@LATE@", late), ("@RST@", rst), ("@TEST@", int(test_build))): + harness = harness.replace(k, str(v)) + c = tmp_path / "t.c" + c.write_text(harness) + exe = tmp_path / "t" + subprocess.run([*host_cc(), *(["-DNVME_TEST_RUNNING_FALLBACK"] if test_build else []), "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", + "-Wno-unused-function", "-Wno-unused-variable", "-Wno-format", "-o", str(exe), str(c)], check=True) + out = subprocess.run([str(exe)], check=True, capture_output=True, text=True).stdout + assert out.splitlines()[-1] == expected diff --git a/tests/python/test_nvme_ans_warm_start.py b/tests/python/test_nvme_ans_warm_start.py index 1b63b12c7..ac65e6685 100644 --- a/tests/python/test_nvme_ans_warm_start.py +++ b/tests/python/test_nvme_ans_warm_start.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only """A post-M4 ANS started from off: stale messages in the receive mailbox are dropped before the run -bit, and a failed start says which step failed.""" +bit, a start that fails is made again from a reset ANS (warm, then cold, then after a power cycle), +and a failed start says which step failed.""" from pathlib import Path import os @@ -11,19 +12,53 @@ from host_compiler import host_cc CASES = { - # name: (stale messages, power on fails, SART fails, handshake fails, BOOT_STATUS fails, expected) - "clean": (0, 0, 0, 0, 0, "ok drained=0 boots=1"), - "stale-at-start": (3, 0, 0, 0, 0, "ok drained=3 boots=1"), - "power-on-fails": (0, 1, 0, 0, 0, "fail drained=0 boots=0 why=power"), - "sart-fails": (0, 0, 1, 0, 0, "fail drained=0 boots=0 why=sart"), - "no-handshake": (0, 0, 0, 1, 0, "fail drained=0 boots=1 why=handshake"), - "boot-status": (0, 0, 0, 0, 1, "fail drained=0 boots=1 why=boot_status"), + # name: (stale messages, power on fails, SART fails, warm starts with no HELLO, failed + # BOOT_STATUS polls, cold starts with no HELLO, reset fails, buffers granted, warm starts + # failing after HELLO, cold starts failing after HELLO, expected) + "clean": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=1 resets=0 cycles=0"), + "stale-at-start": (3, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ok drained=3 boots=1 resets=0 cycles=0"), + "power-on-fails": (0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "fail drained=0 boots=0 resets=0 cycles=0 why=power"), + "sart-fails": (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "fail drained=0 boots=0 resets=0 cycles=0 why=sart"), + "warm-after-reset": (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=2 resets=1 cycles=0"), + "boot-status-fails": (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "fail drained=0 boots=1 resets=0 cycles=0 why=boot_status"), + "late-power-report": (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "fail drained=0 boots=1 resets=0 cycles=0 why=power_report"), + "warm-after-reset-answers": (0, 0, 0, 1, 0, 0, 0, 0, 1, 0, "fail drained=0 boots=2 resets=1 cycles=0 why=power_report"), + "cold-answers": (0, 0, 0, 99, 0, 0, 0, 0, 0, 1, "fail drained=0 boots=3 resets=2 cycles=0 why=cold_power_report"), + "cold-after-reset": (0, 0, 0, 99, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=3 resets=2 cycles=0"), + "after-power-cycle": (0, 0, 0, 99, 0, 1, 0, 0, 0, 0, "ok drained=0 boots=5 resets=4 cycles=1"), + "never": (0, 0, 0, 99, 0, 99, 0, 0, 0, 0, "fail drained=0 boots=5 resets=4 cycles=1 why=cold"), + "reset-fails": (0, 0, 0, 1, 0, 0, 1, 0, 0, 0, "fail drained=0 boots=1 resets=1 cycles=0 why=reset"), + "buffers-granted": (0, 0, 0, 1, 0, 0, 0, 1, 0, 0, "fail drained=0 boots=1 resets=0 cycles=0 why=buffers"), + # HELLO, then no IOP power report of any kind: made again like a start with no HELLO. + "no-power-report": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=2 resets=1 cycles=0"), + "no-power-report-then-cold": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=3 resets=2 cycles=0"), + "no-power-report-cold-too": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=5 resets=4 cycles=1"), + "no-power-report-never": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "fail drained=0 boots=5 resets=4 cycles=1 why=cold_no_report"), + "no-power-report-buffers": (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "fail drained=0 boots=1 resets=0 cycles=0 why=buffers"), + # A quiet start whose firmware has a message queued by the time of the reset isn't reset; a + # start with no HELLO is reset as before. + "no-power-report-late-message": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "fail drained=0 boots=1 resets=0 cycles=0 why=no_report"), + "no-hello-late-message": (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "ok drained=0 boots=2 resets=1 cycles=0"), +} + +# Cases with a message queued in the mailbox when the reset is about to stop ANS. +LATE_MESSAGE = {"no-power-report-late-message", "no-hello-late-message"} + +# name: (warm starts with HELLO and no power report, cold starts with HELLO and no power report) +NO_REPORT = { + "no-power-report": (1, 0), + "no-power-report-then-cold": (99, 0), + "no-power-report-cold-too": (99, 1), + "no-power-report-never": (99, 99), + "no-power-report-buffers": (1, 0), + "no-power-report-late-message": (1, 0), } @pytest.mark.parametrize("case", sorted(CASES)) def test_ans_warm_start(tmp_path, case): - stale, pwr, sart, hs, bs, expected = CASES[case] + stale, pwr, sart, hs, bs, cs, rst, own, hh, ch, expected = CASES[case] + wnr, cnr = NO_REPORT.get(case, (0, 0)) source = Path(os.environ.get("NVME_SOURCE", Path(__file__).resolve().parents[2] / "src/nvme.c")).read_text() start = source.index("/*\n * Discard what an earlier owner left in the receive mailbox") body = source[start:source.index("/* A write or flush, timed:")] @@ -53,7 +88,11 @@ def test_ans_warm_start(tmp_path, case): static struct sart_entry nvme_sart_saved[16]; static int nvme_sart_saved_cnt = 9; static u64 nvme_base, nvme_boot_ticks; -static int stale = @STALE@, drained, boots; +static u8 nvme_die; +static int stale = @STALE@, drained, boots, resets, cycles; +static int warm_fails = @HS@, poll_fails = @BS@, cold_fails = @CS@, warm_late = @HH@, cold_late = @CH@; +static int warm_no_report = @WNR@, cold_no_report = @CNR@; +static bool no_hello, no_report, late_message = @LATE@; static const char *why = ""; static u64 get_ticks(void) { return 1; } static u32 read32(u64 a) { return 0; } @@ -69,23 +108,63 @@ def test_ans_warm_start(tmp_path, case): static int pcie_ans_power_on(void) { if (@PWR@) why = "power"; return @PWR@; } static bool sart_set_entries(sart_dev_t *s, const struct sart_entry *e, int n) { if (@SART@) why = "sart"; return !@SART@; } +static int pcie_ans_power_off(void) { cycles++; return 0; } static bool rtkit_boot_warm(rtkit_dev_t *r, u32 mask) { boots++; /* A queued message is read in place of HELLO. */ - if (stale || @HS@) { why = "handshake"; return false; } + no_hello = !stale && warm_fails; + no_report = false; + if (stale || warm_fails) { warm_fails -= !stale; why = "handshake"; return false; } + if (warm_no_report) { warm_no_report--; no_report = true; why = "no_report"; return false; } + if (warm_late) { warm_late--; why = "power_report"; return false; } return true; } -static int poll32(u64 a, u32 m, u32 v, unsigned t) { if (@BS@) why = "boot_status"; return @BS@ ? -1 : 0; } +static bool rtkit_boot_timed(rtkit_dev_t *r, u32 timeout) +{ + boots++; + no_hello = cold_fails; + no_report = false; + if (!timeout) { why = "untimed"; return false; } + if (cold_fails) { cold_fails--; why = "cold"; return false; } + if (cold_no_report) { cold_no_report--; no_report = true; why = "cold_no_report"; return false; } + if (cold_late) { cold_late--; why = "cold_power_report"; return false; } + return true; +} +static bool rtkit_no_hello(const rtkit_dev_t *r) { return no_hello; } +static bool rtkit_silent_after_power_wait(rtkit_dev_t *r) { return no_report; } +static bool rtkit_can_recv(rtkit_dev_t *r) { return late_message; } +static bool rtkit_has_owned_buffers(const rtkit_dev_t *r) { if (@OWN@) why = "buffers"; return @OWN@; } +/* Order of the stop: the CPU first, then the drain (rtkit_stop()). */ +static bool cpu_stopped, drained_before_stop; +static void asc_cpu_stop(asc_dev_t *a) { cpu_stopped = true; } +static void rtkit_stop(rtkit_dev_t *r) { drained_before_stop |= !cpu_stopped; } +static void rtkit_free(rtkit_dev_t *r) {} +/* A fresh RTKit instance is a fresh start: the stop order is checked again for it. */ +static rtkit_dev_t *rtkit_init(const char *n, asc_dev_t *a, void *m, void *h, sart_dev_t *s, bool f) +{ cpu_stopped = false; return &rtkit; } +static int pmgr_reset_held(int die, const char *name) +{ if (!strcmp(name, "ANS2")) return -1; resets++; if (@RST@) why = "reset"; return @RST@; } +static int poll32(u64 a, u32 m, u32 v, unsigned t) +{ + if (!poll_fails) + return 0; + poll_fails--; why = "boot_status"; + return -1; +} ''' + body + r''' int main(void) { bool ok = nvme_ans_power_on(); - printf("%s drained=%d boots=%d%s%s\n", ok ? "ok" : "fail", drained, boots, *why ? " why=" : "", why); + printf("%s drained=%d boots=%d resets=%d cycles=%d%s%s%s\n", ok ? "ok" : "fail", drained, boots, + resets, cycles, ok ? "" : " why=", ok ? "" : why, + drained_before_stop ? " DRAINED-BEFORE-STOP" : ""); return 0; } ''' - for k, v in (("@STALE@", stale), ("@PWR@", pwr), ("@SART@", sart), ("@HS@", hs), ("@BS@", bs)): + for k, v in (("@STALE@", stale), ("@PWR@", pwr), ("@SART@", sart), ("@HS@", hs), ("@BS@", bs), + ("@CS@", cs), ("@RST@", rst), ("@OWN@", own), ("@HH@", hh), ("@CH@", ch), + ("@WNR@", wnr), ("@CNR@", cnr), ("@LATE@", int(case in LATE_MESSAGE))): harness = harness.replace(k, str(v)) c = tmp_path / "t.c" c.write_text(harness) diff --git a/tests/python/test_nvme_poisoned_io.py b/tests/python/test_nvme_poisoned_io.py index 5f3f6ccd6..cac75cf59 100644 --- a/tests/python/test_nvme_poisoned_io.py +++ b/tests/python/test_nvme_poisoned_io.py @@ -17,7 +17,7 @@ def test_poisoned_session_refuses_io_before_buffer_changes(tmp_path): source.index("/* The ADT describes a post-M4 ANS")] # The ANS power helpers between these fragments need physical firmware state. execute += source[source.index("/* A write or flush, timed:"): - source.index("bool nvme_init(void)")] + source.index("/* Nothing at all arrives in ANS's mailbox")] read = source[source.index("bool nvme_flush("):] read_limit = next(line for line in (root / "src/nvme.h").read_text().splitlines() if line.startswith("#define NVME_MAX_READ_BLOCKS ")) diff --git a/tests/python/test_rtkit_iop_power_silent.py b/tests/python/test_rtkit_iop_power_silent.py new file mode 100644 index 000000000..a302f4c54 --- /dev/null +++ b/tests/python/test_rtkit_iop_power_silent.py @@ -0,0 +1,130 @@ +# SPDX-License-Identifier: MIT +"""An IOP power wait that gave up counts as silence only on a timeout with nothing read during it, +and only when nothing at all is queued in the short grace after it, which looks at the mailbox +without reading it.""" +from pathlib import Path +import subprocess + +import pytest + +from host_compiler import host_cc + +ROOT = Path(__file__).resolve().parents[2] + + +def function(source, signature): + start = source.index(signature) + return source[start:source.index("\n}", source.index("{", start)) + 2] + "\n" + + +# name: (script of mailbox events, one per rtkit_recv() call, expected "wait=<0|1> silent=<0|1>") +# Events: "-" nothing queued, "ack0" an IOP power report of state 0, "on" a report of ON, +# "sys" a system message rtkit_recv() handles itself, "app" an app message, "err" a receive error, +# "crash" a crash. The wait times out after 4 recv calls, the grace after 3 more looks. +CASES = { + "silent": ("- - - - - - - - -", "wait=0 timed_out=1 silent=1 left=0"), + "power-on": ("- on", "wait=1 timed_out=0 silent=0 left=0"), + # A report read by the call that reaches the deadline counts. + "power-on-at-deadline": ("- - - on", "wait=1 timed_out=0 silent=0 left=0"), + "report-of-0-in-wait": ("- ack0 - - - - - - -", "wait=0 timed_out=1 silent=0 left=0"), + "system-message-at-end-of-wait": ("- - - sys - - - -", "wait=0 timed_out=1 silent=0 left=0"), + "app-message-in-wait": ("- app - - - - - -", "wait=0 timed_out=1 silent=0 left=0"), + "recv-error-in-wait": ("- err - - - - - -", "wait=0 timed_out=0 silent=0 left=0"), + "crash-in-wait": ("- crash - - - - - -", "wait=0 timed_out=0 silent=0 left=0"), + # A message seen in the grace stays queued: it is neither handled nor discarded. + "ack-in-grace": ("- - - - ack0 - - -", "wait=0 timed_out=1 silent=0 left=1"), + "on-late-in-grace": ("- - - - - - on -", "wait=0 timed_out=1 silent=0 left=1"), + "system-message-in-grace": ("- - - - - sys - -", "wait=0 timed_out=1 silent=0 left=1"), + "app-message-in-grace": ("- - - - app - - -", "wait=0 timed_out=1 silent=0 left=1"), +} + + +@pytest.mark.parametrize("case", sorted(CASES)) +def test_iop_power_silent(tmp_path, case): + script, expected = CASES[case] + rtkit = (ROOT / "src/rtkit.c").read_text() + body = (function(rtkit, "static bool rtkit_wait_for_power(") + + function(rtkit, "static bool rtkit_iop_wait_was_silent(") + + rtkit[rtkit.index("#define RTKIT_SILENT_GRACE_US"):rtkit.index("bool rtkit_silent_after_power_wait(")] + + function(rtkit, "bool rtkit_silent_after_power_wait(")) + events = ", ".join(f'"{e}"' for e in script.split()) + harness = r''' +#include +#include +#include +#include +typedef uint8_t u8; +typedef uint32_t u32; +typedef uint64_t u64; +#define rtkit_printf(...) ((void)0) +enum rtkit_power_state { RTKIT_POWER_OFF = 0x00, RTKIT_POWER_ON = 0x20 }; +struct rtkit_message { u8 ep; u64 msg; }; +typedef struct { + enum rtkit_power_state iop_power; + bool iop_power_reported, crashed, iop_wait_silent; + u32 rx_count; +} rtkit_dev_t; +static const char *events[] = {@EVENTS@}; +static unsigned next_event, now; +/* The wait's 6 s are 4 recv calls; the grace's 0.5 s are 3 more. */ +static u64 timeout_calculate(u32 usec) { return now + (usec >= 1000000 ? 4 : 3); } +static bool timeout_expired(u64 t) { return now >= t; } +#define NEVENTS (sizeof(events) / sizeof(events[0])) +/* Looks without reading: time passes over an empty slot only. */ +static bool rtkit_can_recv(rtkit_dev_t *rtk) +{ + now++; + if (rtk->crashed) + return false; + if (next_event < NEVENTS && strcmp(events[next_event], "-")) + return true; + next_event++; + return false; +} +static int rtkit_recv(rtkit_dev_t *rtk, struct rtkit_message *msg) +{ + now++; + if (rtk->crashed) + return -1; + const char *e = next_event < sizeof(events) / sizeof(events[0]) ? events[next_event++] : "-"; + if (!strcmp(e, "-")) + return 0; + rtk->rx_count++; + if (!strcmp(e, "ack0") || !strcmp(e, "on")) { + rtk->iop_power = !strcmp(e, "on") ? RTKIT_POWER_ON : RTKIT_POWER_OFF; + rtk->iop_power_reported = true; + return 0; + } + if (!strcmp(e, "sys")) + return 0; + if (!strcmp(e, "app")) { + msg->ep = 0x20; + return 1; + } + if (!strcmp(e, "crash")) + rtk->crashed = true; + return -1; +} +''' + body + r''' +int main(void) +{ + rtkit_dev_t rtk = {0}; + bool timed_out; + u32 rx_before = rtk.rx_count; + bool ok = rtkit_wait_for_power(&rtk, &rtk.iop_power, RTKIT_POWER_ON, 6000000, "IOP", &timed_out); + if (!ok) + rtk.iop_wait_silent = rtkit_iop_wait_was_silent(&rtk, timed_out, rx_before); + bool silent = !ok && rtkit_silent_after_power_wait(&rtk); + bool left = next_event < NEVENTS && strcmp(events[next_event], "-"); + printf("wait=%d timed_out=%d silent=%d left=%d\n", ok, timed_out, silent, left); + return 0; +} +''' + harness = harness.replace("@EVENTS@", events) + c = tmp_path / "t.c" + c.write_text(harness) + exe = tmp_path / "t" + subprocess.run([*host_cc(), "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", + "-Wno-unused-function", "-o", str(exe), str(c)], check=True) + out = subprocess.run([str(exe)], check=True, capture_output=True, text=True).stdout + assert out.splitlines()[-1] == expected