The Pedigree Project 0.1
VirtioBlkController.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef VIRTIO_BLK_CONTROLLER_H
3#define VIRTIO_BLK_CONTROLLER_H
4
5#include "pedigree/kernel/machine/IrqHandler.h"
6#include "pedigree/kernel/process/Mutex.h"
7#include "pedigree/kernel/process/Semaphore.h"
8#include "pedigree/kernel/processor/MemoryRegion.h"
9
10#include "modules/drivers/common/scsi/ScsiController.h"
11#include "modules/drivers/common/virtio/VirtioPci.h"
12#include "modules/drivers/common/virtio/Virtqueue.h"
13
14class VirtioBlkDisk;
15
16class EXPORTED_PUBLIC VirtioBlkController final : public ScsiController, public IrqHandler {
17 public:
18 explicit VirtioBlkController(Device* pci);
19 ~VirtioBlkController() override;
20
21 bool initialiseController();
22 void shutdown();
23 bool readWrite(uint64_t location, void* buffer, size_t bytes, bool writing);
24 bool flush();
25 size_t sizeBytes() const {
26 return m_Bytes;
27 }
28 size_t sectorBytes() const {
29 return m_SectorBytes;
30 }
31 bool readOnly() const {
32 return m_ReadOnly;
33 }
34
35 IrqDisposition irq(irq_id_t number) override;
36 bool sendCommand(size_t, uintptr_t, uint8_t, uintptr_t, uint16_t, bool) override {
37 return false;
38 }
39
40 protected:
41 size_t getNumUnits() override {
42 return getNumChildren();
43 }
44
45 private:
46 bool command(uint32_t type, uint64_t sector, void* buffer, size_t bytes, size_t timeoutSeconds,
47 bool interruptProbe);
48 void drainCompletions(bool fromInterrupt);
49 void failController();
50
51 Device* m_Pci;
52 Virtio::PciTransport m_Transport;
53 Virtio::Queue m_Queue;
54 MemoryRegion m_Control;
55 MemoryRegion m_Data;
56 Mutex m_CommandLock;
57 Mutex m_IrqLock;
58 Semaphore m_Completion;
59 irq_id_t m_Irq;
60 size_t m_Bytes;
61 size_t m_SectorBytes;
62 size_t m_ExpectedUsed;
63 size_t m_InterruptCompletions;
64 bool m_ReadOnly;
65 bool m_Flush;
66 bool m_TransportInitialised;
67 bool m_Ready;
68 bool m_CommandSeen;
69 bool m_CommandValid;
70 bool m_Stopping;
71 bool m_Failed;
72 bool m_Shutdown;
73};
74
75#endif
size_t getNumChildren()
Definition Device.cc:143
virtual IrqDisposition irq(irq_id_t number)=0
Special memory entity in the kernel's virtual address space.
Definition Mutex.h:56
IrqDisposition
Definition IrqHandler.h:31