aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / nvme_shutdown.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* nvme_shutdown.h -- NVMe normal shutdown (CC.SHN = 01b, wait for |
| 18 | * CSTS.SHST = 10b) for the C kernel's halt/reboot path. Register access goes |
| 19 | * through ops so the sequence is host-testable against a model controller |
| 20 | * (svc/tests/stage_test.c). native/disk is not touched: this sits in |
| 21 | * native/kernel/dev next to the bind code that owns the BAR. */ |
| 22 | #ifndef CK_NVME_SHUTDOWN_H |
| 23 | #define CK_NVME_SHUTDOWN_H |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #define CK_NVME_REG_CC 0x14u |
| 27 | #define CK_NVME_REG_CSTS 0x1cu |
| 28 | #define CK_NVME_CC_EN 0x1u |
| 29 | #define CK_NVME_CC_SHN_MASK (3u << 14) |
| 30 | #define CK_NVME_CC_SHN_NORMAL (1u << 14) |
| 31 | #define CK_NVME_CSTS_RDY 0x1u |
| 32 | #define CK_NVME_CSTS_CFS (1u << 1) |
| 33 | #define CK_NVME_CSTS_SHST_MASK (3u << 2) |
| 34 | #define CK_NVME_CSTS_SHST_DONE (2u << 2) |
| 35 | #define CK_NVME_SHUT_POLL_US 100u |
| 36 | #define CK_NVME_SHUT_TIMEOUT_US 5000000u /* no RTD3E use yet: a fixed 5 s bound */ |
| 37 | #define CK_NVME_DISABLE_TIMEOUT_US 1000000u /* fallback CC.EN = 0, bounded 1 s */ |
| 38 | |
| 39 | enum { |
| 40 | CK_NVME_SHUT_OK = 0, /* SHST reached 10b */ |
| 41 | CK_NVME_SHUT_NOT_ENABLED = 1, /* CC.EN = 0: controller idle, nothing to shut down */ |
| 42 | CK_NVME_SHUT_TIMEOUT = -1, /* SHST never reached 10b within the bound */ |
| 43 | CK_NVME_SHUT_GONE = -2, /* CSTS reads all ones: device not answering */ |
| 44 | CK_NVME_SHUT_EARG = -3, |
| 45 | CK_NVME_SHUT_FATAL = -4, /* CSTS.CFS = 1: controller fatal, cannot shut down normally */ |
| 46 | CK_NVME_SHUT_NOT_READY = -5, /* CC.EN = 1 but CSTS.RDY = 0: SHN must not be written */ |
| 47 | CK_NVME_SHUT_ALREADY = 2, /* SHST already 10b: nothing written */ |
| 48 | }; |
| 49 | |
| 50 | struct ck_nvme_shut_ops { |
| 51 | void *ctx; |
| 52 | uint32_t (*r32)(void *ctx, uint32_t off); |
| 53 | void (*w32)(void *ctx, uint32_t off, uint32_t v); |
| 54 | void (*barrier)(void *ctx); |
| 55 | void (*delay)(void *ctx, uint32_t us); |
| 56 | }; |
| 57 | |
| 58 | struct ck_nvme_shut_result { |
| 59 | uint32_t cc_before; /* CC as found */ |
| 60 | uint32_t cc_after; /* CC read back after the SHN write */ |
| 61 | uint32_t csts; /* last CSTS read */ |
| 62 | uint32_t waited_us; /* polling time spent */ |
| 63 | }; |
| 64 | |
| 65 | /* Requests a normal shutdown and waits (bounded) for it to complete. Bus |
| 66 | * mastering must still be on: the controller may DMA while it finishes. */ |
| 67 | int ck_nvme_shutdown(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r); |
| 68 | const char *ck_nvme_shutdown_str(int rc); |
| 69 | |
| 70 | /* Fallback when a normal shutdown cannot complete (TIMEOUT, FATAL, |
| 71 | * NOT_READY): clear CC.EN (and SHN) and wait, bounded, for CSTS.RDY = 0 so |
| 72 | * the controller stops issuing DMA before bus mastering is cut. Returns OK, |
| 73 | * TIMEOUT, GONE or EARG; r->cc_after is CC read back after the write. */ |
| 74 | int ck_nvme_disable(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r); |
| 75 | /* 1 when rc means the controller may still be live and must be disabled. */ |
| 76 | int ck_nvme_shutdown_needs_disable(int rc); |
| 77 | |
| 78 | #endif |