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.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.h -- AIENOS native block-device interface (C, no outside library). |
| 19 | * |
| 20 | * One narrow interface between the Store engine (native/store) and a block |
| 21 | * device backend. Backends: |
| 22 | * disk_file.c hosted file-backed backend for tests (libc; never linked |
| 23 | * into a freestanding image), with power-cut injection. |
| 24 | * disk_nvme.c freestanding NVMe driver ported from the audited Rust path |
| 25 | * (crates/aienos-kernel/src/nvme*, docs/P3_NVME_*.md). No libc. |
| 26 | * disk_sdhci.c freestanding polled PIO SD Host Controller driver (Pi 5 |
| 27 | * microSD). No libc, no DMA. |
| 28 | * |
| 29 | * Geometry. The interface sector is 512 bytes (DISK_SECTOR_BYTES). A backend |
| 30 | * reports its logical block size (512 or 4096); lba and count are in logical |
| 31 | * blocks of that size. The Store sees 4096-byte units on top. |
| 32 | * |
| 33 | * Contract (every backend): |
| 34 | * - read/write return only after the transfer completed or failed. |
| 35 | * - flush returns only after every previously acknowledged write is |
| 36 | * persistent (NVMe Flush with a volatile write cache, or no cache). |
| 37 | * - count is 1..max_blocks_per_io; lba + count <= block_count. disk_read / |
| 38 | * disk_write / disk_flush below check these before calling the backend. |
| 39 | * |
| 40 | * The field order of disk_dev's first six members matches ts_device in |
| 41 | * native/store/torn_slot.h so a disk_dev converts to a ts_device by copy. |
| 42 | */ |
| 43 | #ifndef AIENOS_DISK_H |
| 44 | #define AIENOS_DISK_H |
| 45 | |
| 46 | #include <stddef.h> |
| 47 | #include <stdint.h> |
| 48 | |
| 49 | #define DISK_SECTOR_BYTES 512u |
| 50 | #define DISK_QUEUE_DEPTH 32u /* bounded request queue, see disk_queue */ |
| 51 | |
| 52 | enum { |
| 53 | DISK_OK = 0, |
| 54 | DISK_EIO = -1, /* the backend reported an error */ |
| 55 | DISK_ERANGE = -2, /* lba/count out of bounds or overflow */ |
| 56 | DISK_EARG = -3, /* NULL pointer, zero count, bad parameter */ |
| 57 | DISK_EGEOMETRY = -4, /* block size not 512 or 4096, or zero blocks */ |
| 58 | DISK_EFULL = -5, /* queue full */ |
| 59 | DISK_EPOWER = -6, /* hosted test backend: simulated power cut */ |
| 60 | DISK_ESTATE = -7, /* backend not ready / failed closed */ |
| 61 | }; |
| 62 | |
| 63 | typedef struct { |
| 64 | void *ctx; |
| 65 | uint32_t block_size; /* logical block size: 512 or 4096 */ |
| 66 | uint64_t block_count; /* blocks in the device */ |
| 67 | int (*read)(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf); |
| 68 | int (*write)(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf); |
| 69 | int (*flush)(void *ctx); |
| 70 | uint32_t max_blocks_per_io; /* backend transfer limit, >= 1 */ |
| 71 | } disk_dev; |
| 72 | |
| 73 | /* Checked entry points: validate geometry and bounds, then call the backend. |
| 74 | * Transfers larger than max_blocks_per_io are split. */ |
| 75 | int disk_check(const disk_dev *d); |
| 76 | int disk_read(const disk_dev *d, uint64_t lba, uint32_t count, uint8_t *buf); |
| 77 | int disk_write(const disk_dev *d, uint64_t lba, uint32_t count, const uint8_t *buf); |
| 78 | int disk_flush(const disk_dev *d); |
| 79 | |
| 80 | /* Bounded request queue: at most DISK_QUEUE_DEPTH outstanding requests, |
| 81 | * drained in submission order; a FLUSH is a barrier (everything queued before |
| 82 | * it completes before it is issued). disk_queue_push refuses with DISK_EFULL |
| 83 | * instead of growing. The first failing request stops the drain; the queue is |
| 84 | * then failed closed until disk_queue_reset. */ |
| 85 | typedef enum { DISK_OP_READ = 1, DISK_OP_WRITE = 2, DISK_OP_FLUSH = 3 } disk_op; |
| 86 | |
| 87 | typedef struct { |
| 88 | disk_op op; |
| 89 | uint64_t lba; |
| 90 | uint32_t count; |
| 91 | uint8_t *buf; /* read target or write source (cast away const on write) */ |
| 92 | int status; /* filled by drain */ |
| 93 | } disk_req; |
| 94 | |
| 95 | typedef struct { |
| 96 | const disk_dev *dev; |
| 97 | disk_req req[DISK_QUEUE_DEPTH]; |
| 98 | uint32_t head, len; |
| 99 | int failed; |
| 100 | } disk_queue; |
| 101 | |
| 102 | void disk_queue_init(disk_queue *q, const disk_dev *dev); |
| 103 | int disk_queue_push(disk_queue *q, disk_op op, uint64_t lba, uint32_t count, uint8_t *buf); |
| 104 | int disk_queue_drain(disk_queue *q); /* DISK_OK or the first error */ |
| 105 | void disk_queue_reset(disk_queue *q); |
| 106 | |
| 107 | #endif /* AIENOS_DISK_H */ |