Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/nvme_shutdown.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / nvme_shutdown.c` 103 lines ยท 4052 bytes Raw Blame History 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.c -- see nvme_shutdown.h. NVMe Base 2.0 section 3.6.2 18 * (normal shutdown): set CC.SHN to 01b, then wait for CSTS.SHST = 10b. 19 * SHN is only written to an enabled, ready, non-fatal controller that has not 20 * already shut down; otherwise the caller falls back to ck_nvme_disable. */ 21 #include "nvme_shutdown.h" 22 23 int ck_nvme_shutdown(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r) 24 { 25 if (!o || !o->r32 || !o->w32 || !o->delay || !r) 26 return CK_NVME_SHUT_EARG; 27 r->waited_us = 0; 28 r->cc_before = o->r32(o->ctx, CK_NVME_REG_CC); 29 r->cc_after = r->cc_before; 30 r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); 31 if (r->cc_before == 0xffffffffu || r->csts == 0xffffffffu) 32 return CK_NVME_SHUT_GONE; 33 if (r->csts & CK_NVME_CSTS_CFS) 34 return CK_NVME_SHUT_FATAL; 35 if (!(r->cc_before & CK_NVME_CC_EN)) 36 return CK_NVME_SHUT_NOT_ENABLED; 37 if ((r->csts & CK_NVME_CSTS_SHST_MASK) == CK_NVME_CSTS_SHST_DONE) 38 return CK_NVME_SHUT_ALREADY; 39 if (!(r->csts & CK_NVME_CSTS_RDY)) 40 return CK_NVME_SHUT_NOT_READY; 41 o->w32(o->ctx, CK_NVME_REG_CC, (r->cc_before & ~CK_NVME_CC_SHN_MASK) | CK_NVME_CC_SHN_NORMAL); 42 if (o->barrier) 43 o->barrier(o->ctx); 44 r->cc_after = o->r32(o->ctx, CK_NVME_REG_CC); 45 for (;;) { 46 r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); 47 if (r->csts == 0xffffffffu) 48 return CK_NVME_SHUT_GONE; 49 if ((r->csts & CK_NVME_CSTS_SHST_MASK) == CK_NVME_CSTS_SHST_DONE) 50 return CK_NVME_SHUT_OK; 51 if (r->csts & CK_NVME_CSTS_CFS) 52 return CK_NVME_SHUT_FATAL; 53 if (r->waited_us >= timeout_us) 54 return CK_NVME_SHUT_TIMEOUT; 55 o->delay(o->ctx, CK_NVME_SHUT_POLL_US); 56 r->waited_us += CK_NVME_SHUT_POLL_US; 57 } 58 } 59 60 int ck_nvme_shutdown_needs_disable(int rc) 61 { 62 return rc == CK_NVME_SHUT_TIMEOUT || rc == CK_NVME_SHUT_FATAL || rc == CK_NVME_SHUT_NOT_READY; 63 } 64 65 int ck_nvme_disable(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r) 66 { 67 if (!o || !o->r32 || !o->w32 || !o->delay || !r) 68 return CK_NVME_SHUT_EARG; 69 r->waited_us = 0; 70 r->cc_before = o->r32(o->ctx, CK_NVME_REG_CC); 71 r->cc_after = r->cc_before; 72 if (r->cc_before == 0xffffffffu) 73 return CK_NVME_SHUT_GONE; 74 o->w32(o->ctx, CK_NVME_REG_CC, r->cc_before & ~(CK_NVME_CC_EN | CK_NVME_CC_SHN_MASK)); 75 if (o->barrier) 76 o->barrier(o->ctx); 77 r->cc_after = o->r32(o->ctx, CK_NVME_REG_CC); 78 for (;;) { 79 r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); 80 if (r->csts == 0xffffffffu) 81 return CK_NVME_SHUT_GONE; 82 if (!(r->csts & CK_NVME_CSTS_RDY)) 83 return CK_NVME_SHUT_OK; 84 if (r->waited_us >= timeout_us) 85 return CK_NVME_SHUT_TIMEOUT; 86 o->delay(o->ctx, CK_NVME_SHUT_POLL_US); 87 r->waited_us += CK_NVME_SHUT_POLL_US; 88 } 89 } 90 91 const char *ck_nvme_shutdown_str(int rc) 92 { 93 switch (rc) { 94 case CK_NVME_SHUT_OK: return "complete"; 95 case CK_NVME_SHUT_ALREADY: return "already-complete"; 96 case CK_NVME_SHUT_NOT_ENABLED: return "not-enabled"; 97 case CK_NVME_SHUT_TIMEOUT: return "TIMEOUT"; 98 case CK_NVME_SHUT_GONE: return "DEVICE-GONE"; 99 case CK_NVME_SHUT_FATAL: return "CONTROLLER-FATAL"; 100 case CK_NVME_SHUT_NOT_READY: return "NOT-READY"; 101 default: return "EARG"; 102 } 103 }