The Pedigree Project 0.1
NvmeQueue.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef NVME_QUEUE_H
3#define NVME_QUEUE_H
4#include "pedigree/kernel/process/Mutex.h"
5#include "pedigree/kernel/process/Semaphore.h"
6#include "pedigree/kernel/processor/MemoryRegion.h"
7
8#include "Registers.h"
9class IoBase;
10
11class NvmeQueue {
12 public:
13 enum class Result { Success, CommandError, TransportError };
14 NvmeQueue();
15 bool initialise(IoBase* registers, uint16_t id, uint16_t depth, size_t stride,
16 size_t transferBytes);
17 Result execute(Nvme::Command command, void* buffer, size_t bytes, bool writing, bool interrupts,
18 size_t timeoutSeconds, uint32_t* result = nullptr, bool interruptProbe = false);
19 bool complete(bool fromInterrupt);
20 void stop();
21 size_t interruptCompletions() const;
22 size_t maximumOutstanding() const;
23 uint64_t submissionAddress() const {
24 return m_Submission.physicalAddress();
25 }
26 uint64_t completionAddress() const {
27 return m_Completion.physicalAddress();
28 }
29 uint16_t depth() const {
30 return m_Depth;
31 }
32
33 private:
34 struct Slot {
35 Slot();
36 MemoryRegion data;
37 physical_uintptr_t firstPage = 0;
38 MemoryRegion prps;
39 Semaphore completion;
40 bool active;
41 bool done;
42 uint16_t status;
43 uint32_t result;
44 };
45 bool observe(bool fromInterrupt, bool interruptsEnabled = false);
46 void stopLocked();
47 IoBase* m_Registers;
48 MemoryRegion m_Submission;
49 MemoryRegion m_Completion;
50 Slot m_Slots[Nvme::QueueDepth - 1];
51 mutable Mutex m_Lock;
52 Semaphore m_Available;
53 size_t m_Stride;
54 size_t m_TransferBytes;
55 uint16_t m_Id;
56 uint16_t m_Depth;
57 uint16_t m_Tail;
58 uint16_t m_Head;
59 uint16_t m_SubmissionHead;
60 bool m_Phase;
61 bool m_Online;
62 bool m_PolledInterrupt;
63 size_t m_InterruptCompletions;
64 size_t m_Outstanding;
65 size_t m_MaximumOutstanding;
66};
67#endif
Abstrace base class for hardware I/O capabilities.
Definition IoBase.h:32
Special memory entity in the kernel's virtual address space.
physical_uintptr_t physicalAddress() const
Definition Mutex.h:56