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/machine/Pci.h"
5#include "pedigree/kernel/process/Mutex.h"
6#include "pedigree/kernel/process/Semaphore.h"
7#include "pedigree/kernel/processor/MemoryRegion.h"
8
9#include "Registers.h"
10class IoBase;
11
12class NvmeQueue {
13 public:
14 enum class Result { Success, CommandError, TransportError };
15 NvmeQueue();
16 bool initialise(IoBase* registers, uint16_t id, uint16_t depth, size_t stride,
17 size_t transferBytes, Device* dmaDevice);
18 Result execute(Nvme::Command command, void* buffer, size_t bytes, bool writing, bool interrupts,
19 size_t timeoutSeconds, uint32_t* result = nullptr, bool interruptProbe = false,
20 bool cacheFill = false, physical_uintptr_t directWritePhysical = 0,
21 PciBus::DmaMapping* directReadMapping = nullptr);
22 bool complete(bool fromInterrupt);
23 void stop();
25 void releaseDma();
26 size_t interruptCompletions() const;
27 size_t maximumOutstanding() const;
28 uint64_t submissionAddress() const {
29 return m_SubmissionDma.address();
30 }
31 uint64_t completionAddress() const {
32 return m_CompletionDma.address();
33 }
34 uint16_t depth() const {
35 return m_Depth;
36 }
37
38 private:
39 struct Slot {
40 Slot();
41 MemoryRegion data;
42 physical_uintptr_t firstPage = 0;
43 MemoryRegion prps;
44 PciBus::DmaMapping dataDma[Nvme::MaxTransfer / Nvme::PageSize];
45 PciBus::DmaMapping prpsDma;
46 Semaphore completion;
47 bool active;
48 bool done;
49 uint16_t status;
50 uint32_t result;
51 };
52 bool observe(bool fromInterrupt, bool interruptsEnabled = false);
53 void stopLocked();
54 IoBase* m_Registers;
55 Device* m_DmaDevice;
56 MemoryRegion m_Submission;
57 MemoryRegion m_Completion;
58 PciBus::DmaMapping m_SubmissionDma;
59 PciBus::DmaMapping m_CompletionDma;
60 Slot m_Slots[Nvme::QueueDepth - 1];
61 mutable Mutex m_Lock;
62 Semaphore m_Available;
63 size_t m_Stride;
64 size_t m_TransferBytes;
65 uint16_t m_Id;
66 uint16_t m_Depth;
67 uint16_t m_Tail;
68 uint16_t m_Head;
69 uint16_t m_SubmissionHead;
70 bool m_Phase;
71 bool m_Online;
72 bool m_PolledInterrupt;
73 size_t m_InterruptCompletions;
74 size_t m_Outstanding;
75 size_t m_MaximumOutstanding;
76};
77#endif
Abstrace base class for hardware I/O capabilities.
Definition IoBase.h:32
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56
void releaseDma()
Definition NvmeQueue.cc:118