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 / disk / disk_nvme.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 | /* |
| 18 | * disk_nvme.h -- AIENOS freestanding NVMe driver (C, no libc, no heap). |
| 19 | * |
| 20 | * Ported from the audited Rust path (crates/aienos-kernel/src/nvme.rs, |
| 21 | * nvme/driver.rs, nvme/atomicity.rs; docs/P3_NVME_*.md). Polled, one admin |
| 22 | * queue pair and one I/O queue pair, no interrupts. |
| 23 | * |
| 24 | * The caller provides: |
| 25 | * - nvme_ops: 32-bit MMIO access to BAR0 (64-bit optional), a full memory |
| 26 | * barrier, and a bounded delay in microseconds. |
| 27 | * - nvme_dma: ONE physically contiguous, 4 KiB aligned region with its CPU |
| 28 | * pointer and its bus (physical) address. The driver carves it into fixed |
| 29 | * 4 KiB pages (admin SQ, admin CQ, identify buffer, I/O SQ, I/O CQ, PRP |
| 30 | * list) followed by a bounce buffer for data. The region must be |
| 31 | * coherent with the device (or the barrier op must make it so). |
| 32 | * |
| 33 | * Every request goes through the bounce buffer, so caller buffers need not be |
| 34 | * DMA addressable. One command is outstanding at a time; the driver waits for |
| 35 | * its completion before returning. |
| 36 | * |
| 37 | * Checks performed by nvme_init (each refusal returns its own NVME_E* code; |
| 38 | * docs/P3_NVME_UNSAFE_ASSUMPTIONS.md items marked [UA]): |
| 39 | * [UA] CAP.MPSMIN (bits 51:48) must be 0: the driver uses 4 KiB pages only |
| 40 | * (CC.MPS = 0). CAP.MPSMAX (bits 55:52) must be >= MPSMIN. NVME_EMPS |
| 41 | * [UA] CAP.MQES (bits 15:0, 0-based): admin depth NVME_ADMIN_DEPTH and the |
| 42 | * configured I/O depth must each be <= MQES + 1. NVME_EMQES |
| 43 | * [UA] CAP.CSS bit 37 (NVM command set) must be set; CC.CSS = 0. NVME_ECSS |
| 44 | * [UA] CAP.AMS: documented limit, not a refusal. Round robin (CC.AMS = 0) is |
| 45 | * mandatory for every controller (NVMe base spec, CC.AMS); CAP.AMS |
| 46 | * bits 18:17 only advertise the optional WRR and vendor schemes. The |
| 47 | * audit text reading "bit 0" as round robin is wrong; the driver always |
| 48 | * programs CC.AMS = 0 (asserted by the test model). |
| 49 | * [UA] CAP.TO (bits 31:24, 500 ms units; 0 treated as 500 ms): readiness |
| 50 | * waits poll CSTS every 1 ms and give up after exactly TO*500 ms of |
| 51 | * delay (no extra delay after the final read). NVME_ETIMEOUT |
| 52 | * CSTS.CFS seen during any wait or command poll. NVME_ECFS |
| 53 | * [UA] VS (0x08): major version must be 1 or 2. NVME_EVS |
| 54 | * [UA] NSID: never hard-coded. cfg.nsid == 0 picks the first active NSID |
| 55 | * from Identify CNS=02h; a caller NSID must be in 1..NN (Identify |
| 56 | * Controller NN) and identify with NSZE != 0. NVME_ENSID |
| 57 | * [UA] LBADS: only 9 (512 B) or 12 (4096 B) accepted (disk_dev contract; |
| 58 | * 10, 11 and 13..31 are valid NVMe but unsupported here). NVME_ELBADS |
| 59 | * LBA format metadata size must be 0. NVME_EMETA |
| 60 | * FLBAS index must be <= NLBAF. NVME_EFLBAS |
| 61 | * NSZE must be non-zero. NVME_ENSZE |
| 62 | * [UA] Completion SQID must equal the queue the command went to (0 admin, |
| 63 | * 1 I/O) and CID must match. NVME_ESQID / NVME_ECID |
| 64 | * Identify Controller SQES/CQES must allow 64 B / 16 B entries. NVME_EQES |
| 65 | * DMA region: non-NULL, 4 KiB aligned phys, size >= 6 pages + one data |
| 66 | * page, no address wrap. NVME_EARG |
| 67 | * |
| 68 | * Limits (documented, not checks): |
| 69 | * - Transfer size per command = min(bounce bytes, 128 KiB, 4 KiB << MDTS) |
| 70 | * (MDTS 0 = no device limit). Larger disk_dev requests are split by |
| 71 | * disk.c using max_blocks_per_io. 128 KiB needs at most 31 PRP list |
| 72 | * entries, so one PRP list page is enough; nvme_build_prps still |
| 73 | * implements list chaining (ported from Rust build_prps) and is tested. |
| 74 | * - Command poll budget: max(1000 ms, CAP.TO*500 ms) in 10 us steps. |
| 75 | * - Any timeout, CFS, CID/SQID mismatch or failed queue creation puts the |
| 76 | * controller in the failed state; every later call returns DISK_ESTATE |
| 77 | * until nvme_init runs again (fail-stop). A command that completes with a |
| 78 | * non-zero status returns DISK_EIO and the queue stays usable. |
| 79 | * - Doorbell offsets use CAP.DSTRD (4 << DSTRD stride). |
| 80 | */ |
| 81 | #ifndef AIENOS_DISK_NVME_H |
| 82 | #define AIENOS_DISK_NVME_H |
| 83 | |
| 84 | #include <stdint.h> |
| 85 | #include "disk.h" |
| 86 | |
| 87 | #define NVME_PAGE 4096u |
| 88 | #define NVME_ADMIN_DEPTH 8u |
| 89 | #define NVME_IO_DEPTH_DEFAULT 16u |
| 90 | #define NVME_IO_DEPTH_MAX 64u /* 64 entries x 64 B = one 4 KiB page */ |
| 91 | #define NVME_MAX_XFER (128u * 1024u) /* self-imposed cap, as in the Rust driver */ |
| 92 | #define NVME_FIXED_PAGES 6u /* asq, acq, identify, iosq, iocq, prp list */ |
| 93 | #define NVME_DMA_MIN_BYTES ((NVME_FIXED_PAGES + 1u) * NVME_PAGE) |
| 94 | |
| 95 | /* Register offsets (NVMe base spec). */ |
| 96 | #define NVME_REG_CAP 0x00u |
| 97 | #define NVME_REG_VS 0x08u |
| 98 | #define NVME_REG_INTMS 0x0cu |
| 99 | #define NVME_REG_CC 0x14u |
| 100 | #define NVME_REG_CSTS 0x1cu |
| 101 | #define NVME_REG_AQA 0x24u |
| 102 | #define NVME_REG_ASQ 0x28u |
| 103 | #define NVME_REG_ACQ 0x30u |
| 104 | #define NVME_REG_DOORBELL 0x1000u |
| 105 | |
| 106 | enum { |
| 107 | NVME_OK = 0, |
| 108 | NVME_EARG = -100, /* bad ops / dma / config */ |
| 109 | NVME_EMPS = -101, /* CAP.MPSMIN != 0 or MPSMAX < MPSMIN */ |
| 110 | NVME_EMQES = -102, /* queue depth above CAP.MQES + 1 */ |
| 111 | NVME_ECSS = -103, /* NVM command set not supported */ |
| 112 | NVME_EVS = -104, /* unsupported major version */ |
| 113 | NVME_ETIMEOUT = -105, /* readiness or command poll budget exhausted */ |
| 114 | NVME_ECFS = -106, /* CSTS.CFS: controller fatal status */ |
| 115 | NVME_ESTATUS = -107, /* completion with non-zero status */ |
| 116 | NVME_ECID = -108, /* completion CID mismatch */ |
| 117 | NVME_ESQID = -109, /* completion SQID mismatch */ |
| 118 | NVME_ENSID = -110, /* no usable namespace */ |
| 119 | NVME_ELBADS = -111, /* LBA data size not 512 or 4096 */ |
| 120 | NVME_EMETA = -112, /* LBA format carries metadata */ |
| 121 | NVME_EFLBAS = -113, /* FLBAS index above NLBAF */ |
| 122 | NVME_ENSZE = -114, /* namespace size zero */ |
| 123 | NVME_EQES = -115, /* SQES/CQES do not allow 64/16 byte entries */ |
| 124 | NVME_ERANGE = -116, /* lba/count out of range */ |
| 125 | NVME_ESTATE = -117, /* not initialized or failed (fail-stop) */ |
| 126 | }; |
| 127 | |
| 128 | typedef struct { |
| 129 | void *ctx; |
| 130 | uint32_t (*read32)(void *ctx, uint32_t off); |
| 131 | void (*write32)(void *ctx, uint32_t off, uint32_t val); |
| 132 | /* Optional: when NULL, 64-bit registers are accessed as two 32-bit |
| 133 | * halves (low dword first), as the Rust driver does. */ |
| 134 | uint64_t (*read64)(void *ctx, uint32_t off); |
| 135 | void (*write64)(void *ctx, uint32_t off, uint64_t val); |
| 136 | void (*barrier)(void *ctx); /* full memory + DMA barrier */ |
| 137 | void (*delay_us)(void *ctx, uint32_t us); /* bounded busy wait */ |
| 138 | } nvme_ops; |
| 139 | |
| 140 | typedef struct { |
| 141 | uint8_t *virt; /* CPU pointer */ |
| 142 | uint64_t phys; /* device-visible address, 4 KiB aligned */ |
| 143 | uint64_t bytes; /* >= NVME_DMA_MIN_BYTES */ |
| 144 | } nvme_dma; |
| 145 | |
| 146 | typedef struct { |
| 147 | uint32_t nsid; /* 0 = first active namespace */ |
| 148 | uint16_t io_depth; /* 0 = NVME_IO_DEPTH_DEFAULT; 2..NVME_IO_DEPTH_MAX */ |
| 149 | } nvme_config; |
| 150 | |
| 151 | typedef struct { |
| 152 | uint16_t tail, head, depth, sqid; |
| 153 | uint8_t phase; |
| 154 | uint8_t *sq, *cq; /* CPU pointers into the DMA region */ |
| 155 | uint64_t sq_phys, cq_phys; |
| 156 | uint32_t sq_db, cq_db; /* doorbell register offsets */ |
| 157 | } nvme_qpair; |
| 158 | |
| 159 | typedef struct { |
| 160 | nvme_ops ops; |
| 161 | nvme_dma dma; |
| 162 | uint64_t cap; |
| 163 | uint32_t vs; |
| 164 | uint32_t timeout_ms; /* CAP.TO * 500, min 500 */ |
| 165 | uint32_t cmd_timeout_ms; /* max(1000, timeout_ms) */ |
| 166 | uint32_t dstrd_stride; |
| 167 | nvme_qpair admin, io; |
| 168 | uint16_t next_cid; |
| 169 | int ready, failed; |
| 170 | int last_err; /* last NVME_E* code, for diagnostics */ |
| 171 | /* Identify results */ |
| 172 | uint8_t mdts, vwc; |
| 173 | uint32_t nn, nsid; |
| 174 | uint32_t block_size; |
| 175 | uint64_t block_count; |
| 176 | uint32_t max_xfer_bytes; |
| 177 | uint8_t *identify, *prp_list, *bounce; |
| 178 | uint64_t identify_phys, prp_list_phys, bounce_phys, bounce_bytes; |
| 179 | /* Power-fail atomicity fields (ported from nvme/atomicity.rs). */ |
| 180 | uint16_t awupf_raw, nawupf_raw, nabspf_raw, nabo_blocks; |
| 181 | } nvme_ctrl; |
| 182 | |
| 183 | /* Reset, configure and enable the controller, identify it and the namespace, |
| 184 | * create the I/O queue pair. Calling it again is a full reset ("reopen"). */ |
| 185 | int nvme_init(nvme_ctrl *c, const nvme_ops *ops, const nvme_dma *dma, const nvme_config *cfg); |
| 186 | |
| 187 | /* Clear CC.EN and wait for CSTS.RDY = 0 (bounded by CAP.TO). */ |
| 188 | int nvme_disable(nvme_ctrl *c); |
| 189 | |
| 190 | /* Block I/O in logical blocks; count <= max_xfer_bytes / block_size. */ |
| 191 | int nvme_read(nvme_ctrl *c, uint64_t lba, uint32_t count, uint8_t *buf); |
| 192 | int nvme_write(nvme_ctrl *c, uint64_t lba, uint32_t count, const uint8_t *buf); |
| 193 | int nvme_flush(nvme_ctrl *c); |
| 194 | |
| 195 | /* Fill a disk_dev backed by an initialized controller. */ |
| 196 | int nvme_disk(nvme_ctrl *c, disk_dev *out); |
| 197 | |
| 198 | /* PRP construction (port of Rust build_prps). list holds list_cap u64 |
| 199 | * entries that live at bus address list_addr (page aligned, contiguous). |
| 200 | * On success *used is the number of list entries written. */ |
| 201 | int nvme_build_prps(uint64_t addr, uint64_t len, uint64_t page_size, uint64_t list_addr, |
| 202 | uint64_t *list, uint32_t list_cap, uint64_t *prp1, uint64_t *prp2, |
| 203 | uint32_t *used); |
| 204 | |
| 205 | /* Power-fail atomicity (port of atomicity.rs). Returns NVME_ATOMIC, |
| 206 | * NVME_NOT_ATOMIC or NVME_ATOMIC_UNKNOWN for a write of lba_count blocks. */ |
| 207 | enum { NVME_ATOMIC = 1, NVME_NOT_ATOMIC = 0, NVME_ATOMIC_UNKNOWN = -1 }; |
| 208 | int nvme_write_is_power_fail_atomic(const nvme_ctrl *c, uint64_t start_lba, uint64_t lba_count); |
| 209 | |
| 210 | #endif /* AIENOS_DISK_NVME_H */ |