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 AhciPort {
25 public:
26 AhciPort(IoBase* registers, size_t port);
27 ~AhciPort();
28 bool initialise(uint32_t capabilities, uint32_t version, uint32_t extendedCapabilities);
29 void shutdown();
30 void enableInterrupts();
31 bool interrupt(bool pending);
32 bool command(uint8_t opcode, uint64_t lba, uint16_t sectors, void* buffer, size_t bytes,
33 bool write, bool interrupts, bool interruptProbe = false);
34 bool readBatch(Disk::ReadBuffer* buffers, size_t count, bool interrupts);
35 bool writeBatch(Disk::WriteBuffer* buffers, size_t count, bool interrupts);
36 size_t interruptCompletions() const;
37 size_t maximumOutstanding() const;
38 void configureDisk(size_t sectorBytes, size_t queueDepth);
39 size_t sectorBytes() const {
40 return m_SectorBytes;
41 }
42
43 private:
44 bool transferBatch(Disk::ReadBuffer* buffers, size_t count, bool interrupts, bool writing);
45 uint32_t read(size_t reg) const;
46 void write(size_t reg, uint32_t value);
47 void waitForProgress();
48 bool waitClear(size_t reg, uint32_t bits, size_t milliseconds);
49 bool stopEngines();
50 void acknowledge(uint32_t status);
51 void observe(uint32_t status, bool fromInterrupt);
52 void pollCompletions(bool interrupts);
53
54 bool chooseSlot(bool queued, size_t& index);
55 bool issueCommand(size_t index, uint8_t opcode, uint64_t lba, uint16_t sectors, void* buffer,
56 size_t bytes, bool writing, bool queued, bool interrupts);
57 bool reapCommand(size_t index, uint8_t opcode, void* buffer, size_t bytes, bool writing,
58 bool queued, bool interrupts, bool interruptProbe);
59
60 IoBase* m_Registers;
61 size_t m_Port;
62 MemoryRegion m_Control;
63 struct Slot {
64 Slot()
65 : data("AHCI transfer buffer"), completion(0, false), done(false), errors(0), deadline(0) {}
66 MemoryRegion data;
67 physical_uintptr_t pages[16];
68 Semaphore completion;
69 bool done;
70 uint32_t errors;
71 uint64_t deadline;
72 };
73 Slot m_Slots[32];
74 Mutex m_CommandLock;
75 mutable Mutex m_StateLock;
76 bool m_Online;
77 uint32_t m_Active;
78 uint32_t m_Queued;
79 size_t m_SlotCount;
80 size_t m_QueueDepth;
81 size_t m_SectorBytes;
82 bool m_SupportsNcq;
83 bool m_AddressesInstalled;
84 bool m_PolledInterrupt;
85 size_t m_InterruptCompletions;
86 size_t m_MaximumOutstanding;
87 size_t m_Outstanding;
88};
89#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