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_bind.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_bind.h -- bind native/disk's freestanding NVMe driver to a PCI |
| 18 | * function through ck.h services (MMIO map, barrier, delay, DMA memory). */ |
| 19 | #ifndef AIENOS_CK_NVME_BIND_H |
| 20 | #define AIENOS_CK_NVME_BIND_H |
| 21 | #include "disk.h" |
| 22 | #include "disk_nvme.h" |
| 23 | #include "pci.h" |
| 24 | #include "disk_part.h" |
| 25 | |
| 26 | /* DMA region handed to the driver: 6 fixed pages + 128 KiB bounce. */ |
| 27 | #define CK_NVME_DMA_BYTES ((NVME_FIXED_PAGES * NVME_PAGE) + NVME_MAX_XFER) |
| 28 | |
| 29 | typedef struct { |
| 30 | nvme_ctrl ctrl; |
| 31 | disk_dev disk; |
| 32 | volatile uint8_t *bar0; |
| 33 | const pci_func *pf; |
| 34 | pci_func fn; /* the bound function, built from the discovery result (pf points here) */ |
| 35 | int claimed; /* ownership recorded once: a second bind is refused until the first is fully released */ |
| 36 | int stuck; /* device was written and bus master would not clear: stays owned through release (fail closed) */ |
| 37 | int bound; |
| 38 | int bm_on; /* bus mastering was enabled for this function */ |
| 39 | int confined; /* DMA confined by the SMMU (ck_dma_confine) */ |
| 40 | int shut; /* normal shutdown already attempted (ck_nvme_shutdown_bound) */ |
| 41 | uint32_t stream_id; /* SMMU stream when confined */ |
| 42 | ck_part part; /* the AIENOS partition view (dev/disk_part.h): the only writable disk */ |
| 43 | } ck_nvme; |
| 44 | |
| 45 | /* Bind the NVMe the multi-segment read-only discovery found (aienos#31): the |
| 46 | * first class 01/08/02 function in `found` (lowest segment, bus, device, |
| 47 | * function; any segment). Ownership is recorded once (n->claimed) before any |
| 48 | * device access: a second call while owned, or while DMA is not fully released, |
| 49 | * returns NVME_ESTATE and touches nothing. `probe` (may be NULL) is only used |
| 50 | * to keep the "assigned by stage" log wording. Returns -1 when discovery holds |
| 51 | * no NVMe. Bus mastering a function firmware left enabled is cleared before the |
| 52 | * DMA gate (refused if it sticks). DMA gate and everything after it are |
| 53 | * unchanged: |
| 54 | * |
| 55 | * gate DMA, map BAR0, init the |
| 56 | * controller, fill n->disk, print identify/geometry/atomicity lines, run the |
| 57 | * read-only checks (bounds refusal, read LBA 0), parse the GPT read-only and |
| 58 | * select the AIENOS partition (n->part; none or an invalid GPT: print |
| 59 | * "disk: no AIENOS partition, refusing writes" and return the CK_GPT_* code |
| 60 | * with nothing written), check the translation layer refuses past the |
| 61 | * partition end, then run the write+flush+read-back probe on the last 4 KiB |
| 62 | * unit OF THE PARTITION. Returns 0, or a negative NVME_E* / DISK_E* / |
| 63 | * CK_GPT_* / -1 when no NVMe function exists. */ |
| 64 | int ck_nvme_bind(ck_nvme *n, const pci_found *found, uint32_t nfound, const pci_system *probe); |
| 65 | |
| 66 | /* Normal NVMe shutdown (dev/nvme_shutdown.c) on the bound controller, run |
| 67 | * while bus mastering is still on, before the bus-master revoke. Prints |
| 68 | * "nvme: shutdown normal cc=0x..->0x.. csts=0x.. shst=<result> waited_us=N". |
| 69 | * Runs at most once per bind; returns the CK_NVME_SHUT_* code. */ |
| 70 | int ck_nvme_shutdown_bound(ck_nvme *n); |
| 71 | |
| 72 | /* Run the write/flush/read-back probe on the last whole unit of `d` (the |
| 73 | * kernel passes the AIENOS partition view, never the whole namespace; |
| 74 | * shared with the host tests). `*lba_out` is relative to `d`. `seed` varies the pattern per boot. |
| 75 | * The unit's original bytes are read first and written back (flushed and |
| 76 | * verified) afterwards, so a boot leaves the disk bytes as it found them. */ |
| 77 | int ck_disk_rw_probe(const disk_dev *d, uint32_t seed, uint64_t *lba_out); |
| 78 | #endif |