Skip to content

nvme-apple: T8140 (J700) ANS2 support (reworked; inline encryption dropped) - #21

Merged
RyanTheTide merged 10 commits into
aurora-wipfrom
series/neo-ans-aurora-wip-20260925
Sep 25, 2026
Merged

RyanTheTide merged 10 commits into
aurora-wipfrom
series/neo-ans-aurora-wip-20260925

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

T8140 (J700) ANS2 NVMe support, reworked after the 2026-09-25 maintainer audit and rebased onto aurora-wip.

What changed since the audited head (36d332f1)

  • The ~1000-line commit and its two in-series fix-ups are re-cut into ten bisectable patches: the upstream acquire/release backport first (pristine, Gui-Dong Han's authorship, commit … upstream.), the binding, the post-M4 register layout, restarting a stopped resident ANS, adopting a live inherited RTKit session, the serialized disable/recovery/reset, the quarantine of a controller that fails to stop, no deferred flushes on post-M4, the NVME_QUIRK_BROKEN_FUA core quirk, and its T8140 user through hardware data.
  • Dropped: the inline-encryption profile and TCB key transport (undisclosed in the original subject, third-party code, no in-tree consumer, select BLK_INLINE_ENCRYPTION for every Asahi kernel); the Python "selftest" and its Makefile hooks; the Documentation/arch/arm64/*.rst memo.
  • remove() keeps the base order on every controller (stop, remove namespaces, then disable), with a new DELETING branch in the post-M4 timeout handler so a hung command during removal stops the controller instead of re-arming forever.
  • The LSQ_CTRL and queue-size read-backs only apply to post-M4 controllers; M1–M3 keep their untouched sequence. has_linear_sq_ctrl is gone (it equalled has_lsq_nvmmu everywhere).
  • The module reference is taken only when a controller is quarantined, not on every post-M4 probe.
  • apple,rtkit-handoff is read with of_property_read_string(); the FUA quirk is applied through match data, not of_device_is_compatible(); kzalloc_obj(), no redundant mb()/dma_wmb(), comments reformatted, breadcrumb logging removed.
  • Attribution: Satya's restart path and Acelogic's adoption predicate are credited in the messages; their Signed-off-by lines are outstanding and will be added when they provide them (no trailers were forged).
  • Every commit: author Ryan Murray [email protected], contiguous trailers, wrapped bodies with the hardware facts.

Checks: per-commit W=1 C=1 (upstream sparse) compile of apple.o and core.o, checkpatch --strict --codespell, dt_binding_check on the binding.

Known issues carried, stated in the messages: a request that times out while the post-M4 reset holds disable_lock across nvme_wait_freeze() can deadlock recovery (present in the original series; the pci.c-style fix needs hardware testing); a controller that cannot be stopped now blocks remove() in nvme_remove_namespaces(); ctrl->cap is zero if the very first reset fails before CAP is read.

Not run on hardware in this form. Before relying on it: boot the J700 through each handoff path, suspend/resume, unbind with and without I/O in flight, and one M1/M2 boot for the shared code paths.

🤖 Generated with Claude Code

2045gemini and others added 10 commits September 26, 2026 01:24
commit f61c934 upstream.

apple_nvme_init_queue() initializes queue state and then marks the queue
enabled. The interrupt and request paths check enabled before using that
queue state.

The old wmb() after WRITE_ONCE(enabled, true) does not publish the
earlier initialization before enabled becomes visible. Use a release store
when enabling the queue and acquire loads when testing it.

Although the shutdown-side enabled accesses are not used for publishing
queue initialization, use helpers for them as well for consistency.

Fixes: 5bd2927 ("nvme-apple: Add initial Apple SoC NVMe driver")
Signed-off-by: Gui-Dong Han <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Signed-off-by: Keith Busch <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
Starting with T8132 (M4), the NVMMU registers of the ANS2 controller are
no longer part of the NVMe register block but a region of their own, and
T8140 (A18 Pro) uses the same layout. Add "apple,t8132-nvme-ans2" for
this generation and "apple,t8140-nvme-ans2" with it as fallback, and a
third "nvmmu" register region that these compatibles require and the
older ones must not have.

T8140 needs a third power domain, for the PCIe PHY of the link to the
storage. Nothing else references that domain, and while it is off the
link drops to L1 idle and the ANS firmware fails its first read with a
link timeout. Allow "apcie_phy" as the name of the third domain and
require three domains on T8140, as on T6000 and T6020.

The ANS2 reset stays required, but on T8140 asserting it raises an
SError, so it cannot be used to restart the co-processor there. Say so
in its description.

A bootloader can hand the ANS2 firmware over to the operating system
still running, with the NVMe controller stopped, instead of shutting it
down. Add the "apple,rtkit-handoff" string property with which it
states this at boot. The property describes the state the bootloader
left behind rather than the hardware, so only that bootloader adds it,
at runtime; it must not appear in a board device tree source. It is
only valid for T8140.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
Starting with T8132 (M4), the ANS2 NVMMU registers are no longer part of
the NVMe register block, and the I/O queues are set up differently.
T8140 (A18 Pro) uses the same layout. Add hardware data for it:

 - Map the NVMMU from its own "nvmmu" register region.
 - After Create SQ, also program the I/O CQ and SQ base addresses into
   the registers at 0x1208 and 0x1200. Without them the firmware crashes
   and its crash log reports null I/O SQ and CQ addresses.
 - The register at 0x1210, which takes queue entry counts on older
   SoCs, takes zero-based sizes here, like AQA. Programming 64 for the
   64-entry queue makes the firmware treat it as 65 entries and reject
   CQ head 63 once the host wraps through the queue.
 - The queue and NVMMU TCB base registers are pairs of 32-bit registers;
   write them low word first with lo_hi_writeq(), as m1n1 and U-Boot do.
 - Do not send Delete SQ/CQ. The firmware rejects both with BAD_CMD, and
   the controller disable tears the I/O queues down.
 - Do not send Set Features (Number of Queues). These controllers
   provide exactly one I/O queue pair, and m1n1 brings them up without
   that command; the driver keeps sending it on the older SoCs.

On these SoCs, read LSQ_CTRL and the queue size register back after
programming them and fail the reset if either did not take. Older SoCs
keep their existing sequence, whose registers have never been read back.

Only T8140 has been tested here. The T8132 entry follows m1n1, which
programs both generations the same way, and has not been run.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
When a previous boot stage has run the ANS and stopped it cleanly, the
driver finds CPU_CONTROL.RUN clear and takes its soft-reset path, which
asserts the ANS reset. On post-M4 SoCs that assert raises an SError.

The stopped firmware stays resident, so on these SoCs set RUN and run a
fresh RTKit boot handshake instead, as U-Boot does, and keep the reset
path for the older generations.

This was tested on T8140 (MacBook Neo, J700) booting Linux from the
internal NVMe through a chain whose first m1n1 stage runs the ANS and
stops it (CPU_CONTROL.RUN clear, firmware still resident) before
chainloading the stage that boots Linux. It does not recover an ANS
that Linux itself has stopped: once Linux has shut RTKit down and
cleared RUN, as suspend did, the RTKit boot after setting RUN again
times out.

Satya wrote this change for Aurora's tree on 2026-09-18, after the
reset path raised the SError on his J700, and has been booting Omarchy
from the internal NVMe with it since 2026-09-16. It is carried here
with the restart message demoted to dev_dbg; his Signed-off-by is
outstanding.

Assisted-by: LLM checkpatch sparse
Signed-off-by: Ryan Murray <[email protected]>
On post-M4 SoCs the ANS2 co-processor cannot be reset (asserting its
PMGR reset raises an SError), and a bootloader that used the internal
storage may hand its firmware over still running. That firmware has
completed HELLO and EPMAP with its previous owner and will not repeat
them, so the driver has to adopt the running RTKit session instead of
booting it.

Adopt the session when the controller shows a healthy firmware that
still has an owner:

 - CPU_CONTROL.RUN is set, BOOT_STATUS reads 0xde71ce55 and CSTS.CFS is
   clear, and
 - either CC.EN and CSTS.RDY are both set, because the bootloader left
   the controller running (U-Boot does so until the session has a new
   owner), or both are clear and the device tree carries
   "apple,rtkit-handoff" = "nvme-disabled-v1", with which the bootloader
   states that it stopped the controller but kept the session.

RUN and BOOT_STATUS alone do not identify a live session: both stay set
after the firmware has gone inactive, and adopting that state marks the
RTKit power states as on without the wake handshake, which leaves
CSTS.RDY stuck at zero. A controller with neither CC.EN nor the property
therefore takes the existing wake path. If the property is present but
the controller state does not match it, probe fails rather than guess.
A controller whose RUN bit is clear is restarted as before.

The adopted firmware keeps using the shared buffers its previous owner
gave it, so its buffer requests arrive with a non-zero IOVA. Map such a
buffer only if a SART entry programmed by the bootloader covers it, it
is cache-line aligned, and every page of it is reserved System RAM in
the linear map. Map it with DMA_ATTR_SKIP_CPU_SYNC and reject it unless
the DMA address equals the firmware's IOVA, so that a bounce buffer is
never substituted for live data, and let RTKit sync it before reading.
The inherited SART entries are never removed.

Create the RTKit instance as adopted before mailbox RX starts, so that
inherited syslog traffic is not dropped as coming from undiscovered
endpoints. In the reset work, adopt the session, read CAP and CC from
the controller and clear CC.EN if it is set; only then program the admin
queue and NVMMU base registers. No command is ever submitted to queue or
TCB memory that the driver did not allocate. Because mailbox RX now runs
from probe, an RTKit crash can be reported before the NVMe controller is
initialized, so the crash callback only schedules a reset once probe has
armed it.

The adoption predicate, apple_nvme_can_adopt_rtkit(), is Acelogic's
work for Aurora's tree (2026-09-09), where it let a J700 booted directly
through m1n1 with the controller left enabled complete initialization
(CC=0x460001, CSTS=1); it is extended here with the CSTS.CFS check and
the bootloader-asserted stopped state. His Signed-off-by is
outstanding.

The enabled-controller path is the one the J700 boots through today:
the m1n1 builds used by its boot chain hand the controller over
running. The stopped-controller path is produced by the m1n1 builds
from 2026-09-24 on, which retire the controller's DMA, reserve the
retained heap and only then publish apple,rtkit-handoff. Booting
through U-Boot has not been tested here.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
A post-M4 ANS2 cannot be restarted once it has been stopped. On J700,
after Linux has shut RTKit down and cleared CPU_CONTROL.RUN, the RTKit
boot that follows setting RUN again times out, and the PMGR reset that
older SoCs use to reload the firmware raises an SError. The RTKit
shutdown handshake itself crashes the firmware at system shutdown. A
controller reset on these SoCs therefore keeps the owned RTKit session
and only stops and rebuilds the NVMe queues, and only a completed CC.EN
clear (CSTS.RDY reading 0) stops the controller's DMA: neither CSTS.CFS,
a firmware crash nor quiescing the block layer queues does.

Give post-M4 its own disable: freeze and quiesce, mark both queues
disabled, clear CC.EN and wait for CSTS.RDY, then reap the completion
queues and cancel what is left. It sends no Delete SQ/CQ, which the
firmware rejects, no shutdown notification and no RTKit sleep.
Whether the T8140 firmware completes a shutdown notification has not
been measured here; m1n1 sends one before it hands the controller over
and tolerates a timeout. The legacy disable is unchanged.

disable_lock serializes this disable with the register programming in
the reset work and with RTKit teardown. The reset work drops the lock
while it waits for admin commands (Identify, Create CQ/SQ), so that a
timeout of one of them can still stop the controller, and re-checks for
removal or an abort after each unlocked phase.

A post-M4 request timeout never completes a request before the
controller has stopped. On a live controller the handler moves it to
RESETTING and hands the stop to recovery_work, which schedules the reset
once the controller has stopped. A timeout of the reset's own admin
command, while CONNECTING, marks that reset aborted and lets
recovery_work stop the controller. During removal the handler stops the
controller itself, as pci.c does. Outstanding requests are cancelled
only after the stop. On all controllers the timeout handler now polls
the completion queue before the non-live fast-fail, so that a lost
interrupt during the first Identify no longer turns a completed command
into a reset, and its warnings include CC, CSTS and the completion queue
state.

The NVMe character device can outlive the platform device, so struct
apple_nvme is now reference counted: devres holds one reference and the
nvme_ctrl the other, and the register accessors fail with -ENODEV once
the device is detached. RTKit teardown on post-M4 uses
apple_rtkit_free_retaining_buffers(), because the firmware keeps running
after unbind and may still write the shared buffers.

remove() keeps its order: stop the controller, remove the namespaces,
which drains outstanding I/O, then disable. On all controllers remove()
now cancels a pending reset or recovery instead of waiting for it, and
shutdown() cancels them too; both leave a post-M4 RTKit session running.
suspend() moves a post-M4 controller to RESETTING, so that neither
timeout recovery nor an RTKit crash can start a reset while it is
suspended, and disables only the NVMe controller; resume() schedules
the reset that rebuilds the queues on the owned session.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
If clearing CC.EN on a post-M4 controller does not complete, the
controller may still read and write its queue memory, the NVMMU TCBs,
the PRP lists and the data pages of every request in flight. It cannot
be reset, so nothing can make that memory safe to reuse before the SoC
itself is reset.

Quarantine such a controller instead of tearing it down: disable its
interrupt, keep its power domains on, move it to DELETING, mark its
namespaces dead, and keep every allocation it may still access. The
queue memory and PRP pools move from managed allocations to explicit
devres actions, and those actions, like the ones for the IOD mempool and
the tag sets (which own the requests in flight), skip the free while the
controller is quarantined. The power domains, the RTKit buffers and the
devres reference on struct apple_nvme are kept as well. The requests in
flight are never completed; their timeouts re-arm and their pages stay
with the device. Register access, polling, timeout recovery and RTKit
crash handling stop, resume fails with -EIO, and remove() and shutdown()
return as soon as the stop has failed.

A quarantined controller is recorded by the address of its NVMe
registers, and a later probe of the same controller fails with -EBUSY
until the next boot.

The retained requests keep the block layer calling into this driver, so
take a module reference when a controller is quarantined. It is never
dropped. It cannot stop a module unload that is already in progress: a
quarantine during module removal still leaves the retained state
pointing at code that is about to be freed, so kernels that must rule
this out need the driver built in.

All of this memory is leaked on purpose until reboot.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
The driver completes a flush at once, and sends the real Flush command
later from a delayed work, when the previous flush was less than
flush_interval (1000 ms by default) ago. Until that work runs, the
filesystem treats data as stable that may still be in the controller's
volatile write cache.

Keep that behaviour on the older controllers, where it was added
because their flushes are slow, but always send flushes to post-M4
controllers and complete them when the controller does. The FUA
emulation added later in this series turns every FUA write on T8140
into a write followed by a flush, and completing that flush early would
defeat it. The module parameter description now says that it is ignored
on post-M4 controllers.

Assisted-by: LLM checkpatch sparse
Signed-off-by: Ryan Murray <[email protected]>
Some controllers report a volatile write cache and accept writes with
the FUA bit set, but a FUA write is not reliably durable when it
completes, while explicit Flush commands are.

Add a quirk that keeps the write cache advertised but clears
BLK_FEAT_FUA. The block layer then sends each FUA write as a plain
write followed by a flush.

The first user is the Apple ANS2 controller on T8140.

Assisted-by: LLM checkpatch sparse
Signed-off-by: Ryan Murray <[email protected]>
In testing on T8140 (J700) with a Btrfs root on the internal NVMe, a
16 MiB overwrite that had been fsynced reverted to its previous
contents after an abrupt system reset (a reset over USB-C VDM, which
gives the controller no shutdown), while data written before explicit
Flush commands survived. With FUA writes turned into write plus flush
the same overwrite survived the same reset. The number of trials was
not recorded, and the controller's VWC field was not captured; if it
reports no volatile write cache the quirk changes nothing.

Apply NVME_QUIRK_BROKEN_FUA to T8140 through its hardware data, so that
the block layer follows every FUA write with a flush. Flushes are no
longer deferred on post-M4 controllers, so these reach the controller.
This is not a claim about other post-M4 controllers; T8132 keeps native
FUA. An abrupt reset is also not a power loss, which was not tested.

Assisted-by: LLM checkpatch sparse
Co-authored-by: Claude Fable 5.1 <[email protected]>
Signed-off-by: Ryan Murray <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants