The Pedigree Project 0.1
AhciPort.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 * SPDX-License-Identifier: ISC
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16#ifndef AHCI_PORT_H
17#define AHCI_PORT_H
18#include "pedigree/kernel/machine/Disk.h"
19#include "pedigree/kernel/process/Mutex.h"
20#include "pedigree/kernel/process/Semaphore.h"
21#include "pedigree/kernel/processor/MemoryRegion.h"
22#include "pedigree/kernel/processor/types.h"
23class IoBase;
24class Device;
25class AhciPort {
26 public:
27 AhciPort(IoBase* registers, size_t port, Device* dmaDevice);
28 ~AhciPort();
29 bool initialise(uint32_t capabilities, uint32_t version, uint32_t extendedCapabilities);
30 void shutdown();
31 void enableInterrupts();
32 bool interrupt(bool pending);
33 bool command(uint8_t opcode, uint64_t lba, uint16_t sectors, void* buffer, size_t bytes,
34 bool write, bool interrupts, bool interruptProbe = false, bool cacheFill = false,
35 physical_uintptr_t dmaPhysical = 0,
36 const physical_uintptr_t* directReadPages = nullptr);
37 bool readBatch(Disk::ReadBuffer* buffers, size_t count, bool interrupts);
38 bool writeBatch(Disk::WriteBuffer* buffers, size_t count, bool interrupts);
39 size_t interruptCompletions() const;
40 size_t maximumOutstanding() const;
41 void configureDisk(size_t sectorBytes, size_t queueDepth);
42 size_t sectorBytes() const {
43 return m_SectorBytes;
44 }
45
46 private:
47 bool transferBatch(Disk::ReadBuffer* buffers, size_t count, bool interrupts, bool writing,
48 const physical_uintptr_t* dmaPhysical = nullptr,
49 const physical_uintptr_t* const* directReadPages = nullptr);
50 uint32_t read(size_t reg) const;
51 void write(size_t reg, uint32_t value);
52 void waitForProgress();
53 bool waitClear(size_t reg, uint32_t bits, size_t milliseconds);
54 bool stopEngines();
55 void acknowledge(uint32_t status);
56 void observe(uint32_t status, bool fromInterrupt);
57 void pollCompletions(bool interrupts);
58
59 bool chooseSlot(bool queued, size_t& index);
60 bool issueCommand(size_t index, uint8_t opcode, uint64_t lba, uint16_t sectors, void* buffer,
61 size_t bytes, bool writing, bool queued, bool interrupts,
62 physical_uintptr_t directPhysical = 0,
63 const physical_uintptr_t* directReadPages = nullptr);
64 bool reapCommand(size_t index, uint8_t opcode, void* buffer, size_t bytes, bool writing,
65 bool queued, bool interrupts, bool interruptProbe, bool directRead = false);
66
67 IoBase* m_Registers;
68 Device* m_DmaDevice;
69 size_t m_Port;
70 MemoryRegion m_Control;
71 struct Slot {
72 Slot()
73 : data("AHCI transfer buffer"), completion(0, false), done(false), errors(0), deadline(0) {}
74 MemoryRegion data;
75 physical_uintptr_t pages[16];
76 Semaphore completion;
77 bool done;
78 uint32_t errors;
79 uint64_t deadline;
80 };
81 Slot m_Slots[32];
82 Mutex m_CommandLock;
83 mutable Mutex m_StateLock;
84 bool m_Online;
85 // Protected by m_CommandLock, shared by every writer on this device.
86 bool m_WritesPending;
87 uint32_t m_Active;
88 uint32_t m_Queued;
89 size_t m_SlotCount;
90 size_t m_QueueDepth;
91 size_t m_SectorBytes;
92 bool m_SupportsNcq;
93 bool m_AddressesInstalled;
94 bool m_PolledInterrupt;
95 size_t m_InterruptCompletions;
96 size_t m_MaximumOutstanding;
97 size_t m_Outstanding;
98};
99#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