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/disk/disk_nvme.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 / disk / disk_nvme.c` 553 lines ยท 18636 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 /* 18 * disk_nvme.c -- freestanding polled NVMe driver (see disk_nvme.h). 19 * No libc, no heap, no clock: platform access only through nvme_ops. 20 * Assumes a little-endian CPU (NVMe structures are little-endian). 21 */ 22 #include "disk_nvme.h" 23 24 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ 25 #error "disk_nvme.c assumes a little-endian CPU" 26 #endif 27 28 #define CC_EN 1u 29 #define CC_IOSQES_64 (6u << 16) 30 #define CC_IOCQES_16 (4u << 20) 31 #define CSTS_RDY 1u 32 #define CSTS_CFS 2u 33 #define POLL_STEP_US 10u 34 #define SQE_BYTES 64u 35 #define CQE_BYTES 16u 36 37 /* ---- small freestanding helpers (volatile so the compiler cannot turn them 38 * into memset/memcpy calls) ---- */ 39 40 static void zero_bytes(uint8_t *p, uint64_t n) 41 { 42 volatile uint8_t *q = p; 43 while (n--) 44 *q++ = 0; 45 } 46 47 static void copy_bytes(uint8_t *d, const uint8_t *s, uint64_t n) 48 { 49 volatile uint8_t *q = d; 50 const volatile uint8_t *r = s; 51 while (n--) 52 *q++ = *r++; 53 } 54 55 static uint16_t ld16(const uint8_t *p) 56 { 57 return (uint16_t)(p[0] | (p[1] << 8)); 58 } 59 60 static uint32_t ld32(const uint8_t *p) 61 { 62 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); 63 } 64 65 static uint64_t ld64(const uint8_t *p) 66 { 67 return (uint64_t)ld32(p) | ((uint64_t)ld32(p + 4) << 32); 68 } 69 70 static uint32_t rd32(nvme_ctrl *c, uint32_t off) 71 { 72 return c->ops.read32(c->ops.ctx, off); 73 } 74 75 static void wr32(nvme_ctrl *c, uint32_t off, uint32_t v) 76 { 77 c->ops.write32(c->ops.ctx, off, v); 78 } 79 80 static uint64_t rd64(nvme_ctrl *c, uint32_t off) 81 { 82 if (c->ops.read64) 83 return c->ops.read64(c->ops.ctx, off); 84 uint64_t lo = rd32(c, off); 85 return lo | ((uint64_t)rd32(c, off + 4) << 32); 86 } 87 88 static void wr64(nvme_ctrl *c, uint32_t off, uint64_t v) 89 { 90 if (c->ops.write64) { 91 c->ops.write64(c->ops.ctx, off, v); 92 return; 93 } 94 wr32(c, off, (uint32_t)v); 95 wr32(c, off + 4, (uint32_t)(v >> 32)); 96 } 97 98 static void barrier(nvme_ctrl *c) 99 { 100 c->ops.barrier(c->ops.ctx); 101 } 102 103 static int fail(nvme_ctrl *c, int err) 104 { 105 c->failed = 1; 106 c->last_err = err; 107 return err; 108 } 109 110 /* Wait for CSTS.RDY == want. Reads CSTS, then delays 1 ms, at most 111 * timeout_ms delays in total: the bound is exactly CAP.TO (P2 audit fix). */ 112 static int wait_ready(nvme_ctrl *c, int want) 113 { 114 for (uint32_t i = 0;; i++) { 115 uint32_t s = rd32(c, NVME_REG_CSTS); 116 if (s & CSTS_CFS) return NVME_ECFS; /* GUARD:nvme-cfs-ready */ 117 if (((s & CSTS_RDY) != 0) == (want != 0)) 118 return NVME_OK; 119 if (i >= c->timeout_ms) return NVME_ETIMEOUT; /* GUARD:nvme-ready-bound */ 120 c->ops.delay_us(c->ops.ctx, 1000); 121 } 122 } 123 124 /* ---- queue pair submission and polled completion ---- */ 125 126 static void qpair_setup(nvme_ctrl *c, nvme_qpair *q, uint16_t sqid, uint16_t depth, uint8_t *sq, 127 uint64_t sq_phys, uint8_t *cq, uint64_t cq_phys) 128 { 129 q->tail = 0; 130 q->head = 0; 131 q->depth = depth; 132 q->sqid = sqid; 133 q->phase = 1; 134 q->sq = sq; 135 q->cq = cq; 136 q->sq_phys = sq_phys; 137 q->cq_phys = cq_phys; 138 q->sq_db = NVME_REG_DOORBELL + (2u * sqid) * c->dstrd_stride; 139 q->cq_db = NVME_REG_DOORBELL + (2u * sqid + 1u) * c->dstrd_stride; 140 } 141 142 /* Submit one command (16 dwords; CID is filled in) and poll for its 143 * completion. Returns NVME_OK, NVME_ESTATUS (queue still usable), or a 144 * fail-stop error. */ 145 static int submit(nvme_ctrl *c, nvme_qpair *q, uint32_t *sqe, uint32_t *result) 146 { 147 if (c->failed) 148 return NVME_ESTATE; 149 uint16_t cid = c->next_cid++; 150 sqe[0] = (sqe[0] & 0xffffu) | ((uint32_t)cid << 16); 151 volatile uint32_t *slot = (volatile uint32_t *)(q->sq + (uint64_t)q->tail * SQE_BYTES); 152 for (int i = 0; i < 16; i++) 153 slot[i] = sqe[i]; 154 barrier(c); /* command and data visible before the doorbell */ 155 q->tail = (uint16_t)((q->tail + 1u) % q->depth); 156 wr32(c, q->sq_db, q->tail); 157 158 volatile uint32_t *cqe = (volatile uint32_t *)(q->cq + (uint64_t)q->head * CQE_BYTES); 159 uint64_t budget = (uint64_t)c->cmd_timeout_ms * (1000u / POLL_STEP_US); 160 uint32_t dw3; 161 for (uint64_t i = 0;; i++) { 162 barrier(c); 163 dw3 = cqe[3]; 164 if (((dw3 >> 16) & 1u) == q->phase) 165 break; 166 if (rd32(c, NVME_REG_CSTS) & CSTS_CFS) return fail(c, NVME_ECFS); /* GUARD:nvme-cfs-cmd */ 167 if (i >= budget) return fail(c, NVME_ETIMEOUT); /* GUARD:nvme-cmd-bound */ 168 c->ops.delay_us(c->ops.ctx, POLL_STEP_US); 169 } 170 barrier(c); /* completion seen before reading its fields or data */ 171 uint32_t dw0 = cqe[0]; 172 uint32_t dw2 = cqe[2]; 173 q->head = (uint16_t)(q->head + 1u); 174 if (q->head == q->depth) { 175 q->head = 0; 176 q->phase ^= 1u; 177 } 178 wr32(c, q->cq_db, q->head); 179 if ((dw3 & 0xffffu) != cid) return fail(c, NVME_ECID); /* GUARD:nvme-cid */ 180 if ((dw2 >> 16) != q->sqid) return fail(c, NVME_ESQID); /* GUARD:nvme-sqid */ 181 if ((dw3 >> 17) & 0x7ffu) { /* SC (bits 24:17) and SCT (27:25) */ 182 c->last_err = NVME_ESTATUS; 183 return NVME_ESTATUS; 184 } 185 if (result) 186 *result = dw0; 187 return NVME_OK; 188 } 189 190 static void sqe_clear(uint32_t *sqe) 191 { 192 for (int i = 0; i < 16; i++) 193 sqe[i] = 0; 194 } 195 196 static int identify(nvme_ctrl *c, uint32_t cns, uint32_t nsid) 197 { 198 uint32_t sqe[16]; 199 zero_bytes(c->identify, NVME_PAGE); 200 sqe_clear(sqe); 201 sqe[0] = 0x06; 202 sqe[1] = nsid; 203 sqe[6] = (uint32_t)c->identify_phys; 204 sqe[7] = (uint32_t)(c->identify_phys >> 32); 205 sqe[10] = cns; 206 return submit(c, &c->admin, sqe, 0); 207 } 208 209 /* ---- init ---- */ 210 211 int nvme_init(nvme_ctrl *c, const nvme_ops *ops, const nvme_dma *dma, const nvme_config *cfg) 212 { 213 if (!c || !ops || !dma || !ops->read32 || !ops->write32 || !ops->barrier || !ops->delay_us) 214 return NVME_EARG; 215 zero_bytes((uint8_t *)c, sizeof *c); 216 /* field by field: a struct copy may become a memcpy call (e.g. -mstrict-align) */ 217 c->ops.ctx = ops->ctx; 218 c->ops.read32 = ops->read32; 219 c->ops.write32 = ops->write32; 220 c->ops.read64 = ops->read64; 221 c->ops.write64 = ops->write64; 222 c->ops.barrier = ops->barrier; 223 c->ops.delay_us = ops->delay_us; 224 c->last_err = NVME_EARG; 225 uint32_t want_nsid = cfg ? cfg->nsid : 0; 226 uint16_t io_depth = (cfg && cfg->io_depth) ? cfg->io_depth : NVME_IO_DEPTH_DEFAULT; 227 if (io_depth < 2 || io_depth > NVME_IO_DEPTH_MAX) 228 return NVME_EARG; 229 if (!dma->virt || ((uintptr_t)dma->virt & (NVME_PAGE - 1)) || (dma->phys & (NVME_PAGE - 1)) || dma->bytes < NVME_DMA_MIN_BYTES) 230 return NVME_EARG; 231 if (dma->phys + dma->bytes < dma->phys) /* bus address wraps */ 232 return NVME_EARG; 233 c->dma.virt = dma->virt; 234 c->dma.phys = dma->phys; 235 c->dma.bytes = dma->bytes; 236 237 c->cap = rd64(c, NVME_REG_CAP); 238 c->vs = rd32(c, NVME_REG_VS); 239 uint32_t major = c->vs >> 16; 240 if (major < 1 || major > 2) return c->last_err = NVME_EVS; /* GUARD:nvme-vs */ 241 /* MPSMIN == 0 means 4 KiB is the minimum page; MPSMAX >= 0 always holds, 242 * so 4 KiB is then within [MPSMIN, MPSMAX]. */ 243 if ((c->cap >> 48) & 0xfu) return c->last_err = NVME_EMPS; /* GUARD:nvme-mpsmin */ 244 uint32_t mqes1 = (uint32_t)(c->cap & 0xffffu) + 1u; 245 if (NVME_ADMIN_DEPTH > mqes1) return c->last_err = NVME_EMQES; /* GUARD:nvme-mqes-admin */ 246 if (io_depth > mqes1) return c->last_err = NVME_EMQES; /* GUARD:nvme-mqes-io */ 247 if (!((c->cap >> 37) & 1u)) return c->last_err = NVME_ECSS; /* GUARD:nvme-css */ 248 uint32_t to = (uint32_t)((c->cap >> 24) & 0xffu); 249 c->timeout_ms = (to ? to : 1u) * 500u; 250 c->cmd_timeout_ms = c->timeout_ms > 1000u ? c->timeout_ms : 1000u; 251 c->dstrd_stride = 4u << ((c->cap >> 32) & 0xfu); 252 253 /* Reset: clear EN, wait for RDY = 0. */ 254 uint32_t cc = rd32(c, NVME_REG_CC); 255 wr32(c, NVME_REG_CC, cc & ~CC_EN); 256 int rc = wait_ready(c, 0); 257 if (rc) 258 return c->last_err = rc; 259 260 /* Carve the DMA region. */ 261 uint8_t *v = dma->virt; 262 uint64_t p = dma->phys; 263 uint8_t *asq = v, *acq = v + NVME_PAGE, *iosq = v + 3 * NVME_PAGE, *iocq = v + 4 * NVME_PAGE; 264 c->identify = v + 2 * NVME_PAGE; 265 c->identify_phys = p + 2 * NVME_PAGE; 266 c->prp_list = v + 5 * NVME_PAGE; 267 c->prp_list_phys = p + 5 * NVME_PAGE; 268 c->bounce = v + NVME_FIXED_PAGES * NVME_PAGE; 269 c->bounce_phys = p + NVME_FIXED_PAGES * NVME_PAGE; 270 c->bounce_bytes = (dma->bytes - NVME_FIXED_PAGES * NVME_PAGE) & ~(uint64_t)(NVME_PAGE - 1); 271 zero_bytes(v, NVME_FIXED_PAGES * NVME_PAGE); 272 barrier(c); 273 274 qpair_setup(c, &c->admin, 0, NVME_ADMIN_DEPTH, asq, p, acq, p + NVME_PAGE); 275 wr32(c, NVME_REG_AQA, ((NVME_ADMIN_DEPTH - 1u) << 16) | (NVME_ADMIN_DEPTH - 1u)); 276 wr64(c, NVME_REG_ASQ, c->admin.sq_phys); 277 wr64(c, NVME_REG_ACQ, c->admin.cq_phys); 278 /* CSS = 0 (NVM), MPS = 0 (4 KiB), AMS = 0 (round robin, mandatory). */ 279 wr32(c, NVME_REG_CC, CC_IOSQES_64 | CC_IOCQES_16 | CC_EN); 280 rc = wait_ready(c, 1); 281 if (rc) 282 return fail(c, rc); 283 c->next_cid = 1; 284 285 /* Identify Controller (CNS 01h). */ 286 if ((rc = identify(c, 1, 0)) != NVME_OK) 287 return fail(c, rc); 288 const uint8_t *id = c->identify; 289 barrier(c); 290 uint8_t sqes = id[512], cqes = id[513]; 291 if ((sqes & 0xf) > 6 || (sqes >> 4) < 6) return fail(c, NVME_EQES); /* GUARD:nvme-sqes */ 292 if ((cqes & 0xf) > 4 || (cqes >> 4) < 4) return fail(c, NVME_EQES); /* GUARD:nvme-cqes */ 293 c->mdts = id[77]; 294 c->nn = ld32(id + 516); 295 c->vwc = id[525] & 1u; 296 c->awupf_raw = ld16(id + 528); 297 298 /* Namespace: caller's, or the first active one (CNS 02h). */ 299 if (want_nsid == 0) { 300 if ((rc = identify(c, 2, 0)) != NVME_OK) 301 return fail(c, rc == NVME_ESTATUS ? NVME_ENSID : rc); 302 barrier(c); 303 want_nsid = ld32(c->identify); 304 } 305 if (want_nsid == 0 || want_nsid > c->nn) return fail(c, NVME_ENSID); /* GUARD:nvme-nsid */ 306 c->nsid = want_nsid; 307 308 /* Identify Namespace (CNS 00h). */ 309 if ((rc = identify(c, 0, c->nsid)) != NVME_OK) 310 return fail(c, rc == NVME_ESTATUS ? NVME_ENSID : rc); 311 barrier(c); 312 uint64_t nsze = ld64(id); 313 uint8_t nlbaf = id[25], flbas = id[26]; 314 if (nsze == 0) return fail(c, NVME_ENSZE); /* GUARD:nvme-nsze */ 315 if ((flbas & 0x60) || (flbas & 0xf) > nlbaf) return fail(c, NVME_EFLBAS); /* GUARD:nvme-flbas */ 316 uint32_t lbaf = ld32(id + 128 + (flbas & 0xfu) * 4u); 317 uint32_t lbads = (lbaf >> 16) & 0xffu; 318 if (lbaf & 0xffffu) return fail(c, NVME_EMETA); /* GUARD:nvme-meta */ 319 if (lbads != 9 && lbads != 12) return fail(c, NVME_ELBADS); /* GUARD:nvme-lbads */ 320 c->block_size = 1u << lbads; 321 c->block_count = nsze; 322 c->nawupf_raw = ld16(id + 36); 323 c->nabo_blocks = ld16(id + 42); 324 c->nabspf_raw = ld16(id + 44); 325 326 /* Transfer limit. */ 327 uint64_t lim = NVME_MAX_XFER; 328 if (c->mdts && c->mdts < 20 && ((uint64_t)NVME_PAGE << c->mdts) < lim) 329 lim = (uint64_t)NVME_PAGE << c->mdts; 330 if (c->bounce_bytes < lim) 331 lim = c->bounce_bytes; 332 lim = lim / c->block_size * c->block_size; 333 if (lim < c->block_size) 334 return fail(c, NVME_EARG); 335 c->max_xfer_bytes = (uint32_t)lim; 336 337 /* I/O queue pair, QID 1: completion queue first (polled: PC=1, IEN=0). */ 338 qpair_setup(c, &c->io, 1, io_depth, iosq, p + 3 * NVME_PAGE, iocq, p + 4 * NVME_PAGE); 339 uint32_t sqe[16]; 340 sqe_clear(sqe); 341 sqe[0] = 0x05; 342 sqe[6] = (uint32_t)c->io.cq_phys; 343 sqe[7] = (uint32_t)(c->io.cq_phys >> 32); 344 sqe[10] = 1u | ((uint32_t)(io_depth - 1u) << 16); 345 sqe[11] = 1u; 346 if ((rc = submit(c, &c->admin, sqe, 0)) != NVME_OK) 347 return fail(c, rc); 348 sqe_clear(sqe); 349 sqe[0] = 0x01; 350 sqe[6] = (uint32_t)c->io.sq_phys; 351 sqe[7] = (uint32_t)(c->io.sq_phys >> 32); 352 sqe[10] = 1u | ((uint32_t)(io_depth - 1u) << 16); 353 sqe[11] = 1u | (1u << 16); 354 if ((rc = submit(c, &c->admin, sqe, 0)) != NVME_OK) 355 return fail(c, rc); 356 c->ready = 1; 357 c->last_err = NVME_OK; 358 return NVME_OK; 359 } 360 361 int nvme_disable(nvme_ctrl *c) 362 { 363 if (!c || !c->ops.read32) 364 return NVME_EARG; 365 c->ready = 0; 366 wr32(c, NVME_REG_CC, rd32(c, NVME_REG_CC) & ~CC_EN); 367 return wait_ready(c, 0); 368 } 369 370 /* ---- PRP construction (port of Rust build_prps) ---- */ 371 372 int nvme_build_prps(uint64_t addr, uint64_t len, uint64_t page_size, uint64_t list_addr, 373 uint64_t *list, uint32_t list_cap, uint64_t *prp1, uint64_t *prp2, 374 uint32_t *used) 375 { 376 if (!prp1 || !prp2 || !used) 377 return NVME_EARG; 378 if (len == 0 || page_size < 8 || (page_size & (page_size - 1))) 379 return NVME_EARG; 380 uint64_t off = addr & (page_size - 1); 381 uint64_t covered = off + len; 382 if (covered < off) return NVME_EARG; /* GUARD:prp-len-wrap */ 383 uint64_t pages = covered / page_size + ((covered % page_size) != 0); 384 *prp1 = addr; 385 *prp2 = 0; 386 *used = 0; 387 if (pages == 1) 388 return NVME_OK; 389 uint64_t second = (addr & ~(page_size - 1)) + page_size; 390 if (second < addr) return NVME_EARG; /* GUARD:prp-addr-wrap */ 391 if (pages == 2) { 392 *prp2 = second; 393 return NVME_OK; 394 } 395 if (list_addr == 0 || (list_addr & (page_size - 1)) || !list) 396 return NVME_EARG; 397 uint64_t per = page_size / 8, count = pages - 1, data = 0, slot = 0; 398 while (data < count) { 399 if (slot >= list_cap) return NVME_ERANGE; /* GUARD:prp-list-cap */ 400 uint64_t e; 401 if (slot % per == per - 1 && count - data > 1) { 402 e = list_addr + (slot / per + 1) * page_size; /* chain to next list page */ 403 if (e < list_addr) 404 return NVME_EARG; 405 } else { 406 e = second + data * page_size; 407 if (e < second) 408 return NVME_EARG; 409 data++; 410 } 411 list[slot++] = e; 412 } 413 *prp2 = list_addr; 414 *used = (uint32_t)slot; 415 return NVME_OK; 416 } 417 418 /* ---- block I/O ---- */ 419 420 static int check_io(nvme_ctrl *c, uint64_t lba, uint32_t count, const void *buf) 421 { 422 if (!c) 423 return NVME_EARG; 424 if (!c->ready || c->failed) return NVME_ESTATE; /* GUARD:nvme-failstop */ 425 if (!buf || count == 0) 426 return NVME_EARG; 427 if ((uint64_t)count * c->block_size > c->max_xfer_bytes) return NVME_ERANGE; /* GUARD:nvme-xfer */ 428 if (lba >= c->block_count || count > c->block_count - lba) return NVME_ERANGE; /* GUARD:nvme-range */ 429 return NVME_OK; 430 } 431 432 static int rw(nvme_ctrl *c, uint32_t opc, uint64_t lba, uint32_t count) 433 { 434 uint64_t bytes = (uint64_t)count * c->block_size, prp1, prp2; 435 uint32_t used; 436 int rc = nvme_build_prps(c->bounce_phys, bytes, NVME_PAGE, c->prp_list_phys, 437 (uint64_t *)(void *)c->prp_list, NVME_PAGE / 8, &prp1, &prp2, &used); 438 if (rc) 439 return rc; 440 uint32_t sqe[16]; 441 sqe_clear(sqe); 442 sqe[0] = opc; 443 sqe[1] = c->nsid; 444 sqe[6] = (uint32_t)prp1; 445 sqe[7] = (uint32_t)(prp1 >> 32); 446 sqe[8] = (uint32_t)prp2; 447 sqe[9] = (uint32_t)(prp2 >> 32); 448 sqe[10] = (uint32_t)lba; 449 sqe[11] = (uint32_t)(lba >> 32); 450 sqe[12] = count - 1u; /* NLB is 0-based */ 451 return submit(c, &c->io, sqe, 0); 452 } 453 454 int nvme_read(nvme_ctrl *c, uint64_t lba, uint32_t count, uint8_t *buf) 455 { 456 int rc = check_io(c, lba, count, buf); 457 if (rc) 458 return rc; 459 if ((rc = rw(c, 0x02, lba, count)) != NVME_OK) 460 return rc; 461 barrier(c); 462 copy_bytes(buf, c->bounce, (uint64_t)count * c->block_size); 463 return NVME_OK; 464 } 465 466 int nvme_write(nvme_ctrl *c, uint64_t lba, uint32_t count, const uint8_t *buf) 467 { 468 int rc = check_io(c, lba, count, buf); 469 if (rc) 470 return rc; 471 copy_bytes(c->bounce, buf, (uint64_t)count * c->block_size); 472 return rw(c, 0x01, lba, count); 473 } 474 475 int nvme_flush(nvme_ctrl *c) 476 { 477 if (!c) 478 return NVME_EARG; 479 if (!c->ready || c->failed) 480 return NVME_ESTATE; 481 uint32_t sqe[16]; 482 sqe_clear(sqe); 483 sqe[0] = 0x00; 484 sqe[1] = c->nsid; 485 return submit(c, &c->io, sqe, 0); 486 } 487 488 /* ---- disk_dev backend ---- */ 489 490 static int to_disk(int rc) 491 { 492 switch (rc) { 493 case NVME_OK: return DISK_OK; 494 case NVME_ERANGE: return DISK_ERANGE; 495 case NVME_EARG: return DISK_EARG; 496 case NVME_ESTATE: return DISK_ESTATE; 497 default: return DISK_EIO; 498 } 499 } 500 501 static int d_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf) 502 { 503 return to_disk(nvme_read((nvme_ctrl *)ctx, lba, count, buf)); 504 } 505 506 static int d_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf) 507 { 508 return to_disk(nvme_write((nvme_ctrl *)ctx, lba, count, buf)); 509 } 510 511 static int d_flush(void *ctx) 512 { 513 return to_disk(nvme_flush((nvme_ctrl *)ctx)); 514 } 515 516 int nvme_disk(nvme_ctrl *c, disk_dev *out) 517 { 518 if (!c || !out) 519 return DISK_EARG; 520 if (!c->ready || c->failed) 521 return DISK_ESTATE; 522 out->ctx = c; 523 out->block_size = c->block_size; 524 out->block_count = c->block_count; 525 out->read = d_read; 526 out->write = d_write; 527 out->flush = d_flush; 528 out->max_blocks_per_io = c->max_xfer_bytes / c->block_size; 529 return disk_check(out); 530 } 531 532 /* ---- power-fail atomicity (port of atomicity.rs) ---- */ 533 534 int nvme_write_is_power_fail_atomic(const nvme_ctrl *c, uint64_t start_lba, uint64_t lba_count) 535 { 536 if (!c || !c->ready) 537 return NVME_ATOMIC_UNKNOWN; 538 if (lba_count == 0) 539 return NVME_NOT_ATOMIC; 540 uint64_t unit = c->nawupf_raw ? (uint64_t)c->nawupf_raw + 1 : (uint64_t)c->awupf_raw + 1; 541 if (lba_count > unit) return NVME_NOT_ATOMIC; /* GUARD:atomic-unit */ 542 if (c->nabspf_raw) { 543 uint64_t size = (uint64_t)c->nabspf_raw + 1, off = c->nabo_blocks % size; 544 uint64_t end = start_lba + lba_count - 1; 545 if (end < start_lba) 546 return NVME_NOT_ATOMIC; 547 /* region index shifted by one so blocks before the offset are region 0 */ 548 uint64_t r0 = start_lba < off ? 0 : (start_lba - off) / size + 1; 549 uint64_t r1 = end < off ? 0 : (end - off) / size + 1; 550 if (r0 != r1) return NVME_NOT_ATOMIC; /* GUARD:atomic-boundary */ 551 } 552 return NVME_ATOMIC; 553 }