forked from Bananymous/banan-os
Kernel: Rework whole ATA driver structure
Make ATA driver more compatible when we are adding SATA support
This commit is contained in:
@@ -1,48 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Errors.h>
|
||||
#include <BAN/RefPtr.h>
|
||||
#include <BAN/Vector.h>
|
||||
#include <kernel/InterruptController.h>
|
||||
#include <kernel/SpinLock.h>
|
||||
#include <kernel/Storage/ATAController.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class ATADevice;
|
||||
|
||||
class ATABus : public Interruptable
|
||||
class ATABus : public BAN::RefCounted<ATABus>, public Interruptable
|
||||
{
|
||||
public:
|
||||
enum class DeviceType
|
||||
{
|
||||
None,
|
||||
ATA,
|
||||
ATAPI,
|
||||
};
|
||||
|
||||
public:
|
||||
static ATABus* create(ATAController&, uint16_t base, uint16_t ctrl, uint8_t irq);
|
||||
static BAN::ErrorOr<BAN::RefPtr<ATABus>> create(uint16_t base, uint16_t ctrl, uint8_t irq);
|
||||
|
||||
BAN::ErrorOr<void> read(ATADevice&, uint64_t, uint8_t, uint8_t*);
|
||||
BAN::ErrorOr<void> write(ATADevice&, uint64_t, uint8_t, const uint8_t*);
|
||||
|
||||
ATAController& controller() { return m_controller; }
|
||||
|
||||
virtual void handle_irq() override;
|
||||
|
||||
void initialize_devfs();
|
||||
|
||||
private:
|
||||
ATABus(ATAController& controller, uint16_t base, uint16_t ctrl)
|
||||
: m_controller(controller)
|
||||
, m_base(base)
|
||||
ATABus(uint16_t base, uint16_t ctrl)
|
||||
: m_base(base)
|
||||
, m_ctrl(ctrl)
|
||||
{}
|
||||
void initialize(uint8_t irq);
|
||||
BAN::ErrorOr<void> initialize();
|
||||
|
||||
void select_device(const ATADevice&);
|
||||
DeviceType identify(const ATADevice&, uint16_t*);
|
||||
void select_device(bool secondary);
|
||||
BAN::ErrorOr<DeviceType> identify(bool secondary, BAN::Span<uint16_t> buffer);
|
||||
|
||||
void block_until_irq();
|
||||
uint8_t device_index(const ATADevice&) const;
|
||||
//uint8_t device_index(const ATADevice&) const;
|
||||
|
||||
uint8_t io_read(uint16_t);
|
||||
void io_write(uint16_t, uint8_t);
|
||||
@@ -52,14 +50,14 @@ namespace Kernel
|
||||
BAN::Error error();
|
||||
|
||||
private:
|
||||
ATAController& m_controller;
|
||||
const uint16_t m_base;
|
||||
const uint16_t m_ctrl;
|
||||
SpinLock m_lock;
|
||||
|
||||
bool m_has_got_irq { false };
|
||||
|
||||
BAN::RefPtr<ATADevice> m_devices[2] {};
|
||||
// Non-owning pointers
|
||||
BAN::Vector<ATADevice*> m_devices;
|
||||
|
||||
friend class ATAController;
|
||||
};
|
||||
27
kernel/include/kernel/Storage/ATA/ATAController.h
Normal file
27
kernel/include/kernel/Storage/ATA/ATAController.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/UniqPtr.h>
|
||||
#include <kernel/PCI.h>
|
||||
#include <kernel/Storage/StorageController.h>
|
||||
#include <kernel/Storage/ATA/ATABus.h>
|
||||
#include <kernel/Storage/ATA/ATADevice.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class ATAController : public StorageController
|
||||
{
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::UniqPtr<StorageController>> create(PCI::Device&);
|
||||
virtual BAN::ErrorOr<void> initialize() override;
|
||||
|
||||
private:
|
||||
ATAController(PCI::Device& pci_device)
|
||||
: m_pci_device(pci_device)
|
||||
{ }
|
||||
|
||||
private:
|
||||
PCI::Device& m_pci_device;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#define ATA_PROGIF_PRIMARY_NATIVE (1 << 0)
|
||||
#define ATA_PROGIF_SECONDARY_NATIVE (1 << 2)
|
||||
|
||||
#define ATA_PORT_DATA 0x00
|
||||
#define ATA_PORT_ERROR 0x00
|
||||
#define ATA_PORT_SECTOR_COUNT 0x02
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/Storage/ATABus.h>
|
||||
#include <kernel/Storage/ATA/ATABus.h>
|
||||
#include <kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel
|
||||
@@ -9,23 +9,31 @@ namespace Kernel
|
||||
class ATADevice final : public StorageDevice
|
||||
{
|
||||
public:
|
||||
ATADevice(ATABus&);
|
||||
BAN::ErrorOr<void> initialize(ATABus::DeviceType, const uint16_t*);
|
||||
static BAN::ErrorOr<BAN::RefPtr<ATADevice>> create(BAN::RefPtr<ATABus>, ATABus::DeviceType, bool is_secondary, BAN::Span<const uint16_t> identify_data);
|
||||
|
||||
virtual uint32_t sector_size() const override { return m_sector_words * 2; }
|
||||
virtual uint64_t total_size() const override { return m_lba_count * sector_size(); }
|
||||
|
||||
bool is_secondary() const { return m_is_secondary; }
|
||||
uint32_t words_per_sector() const { return m_sector_words; }
|
||||
uint64_t sector_count() const { return m_lba_count; }
|
||||
|
||||
BAN::StringView model() const { return m_model; }
|
||||
BAN::StringView name() const;
|
||||
|
||||
protected:
|
||||
virtual BAN::ErrorOr<void> read_sectors_impl(uint64_t, uint8_t, uint8_t*) override;
|
||||
virtual BAN::ErrorOr<void> write_sectors_impl(uint64_t, uint8_t, const uint8_t*) override;
|
||||
|
||||
private:
|
||||
ATABus& m_bus;
|
||||
uint8_t m_index;
|
||||
ATADevice(BAN::RefPtr<ATABus>, ATABus::DeviceType, bool is_secodary);
|
||||
BAN::ErrorOr<void> initialize(BAN::Span<const uint16_t> identify_data);
|
||||
|
||||
ATABus::DeviceType m_type;
|
||||
private:
|
||||
BAN::RefPtr<ATABus> m_bus;
|
||||
const ATABus::DeviceType m_type;
|
||||
const bool m_is_secondary;
|
||||
|
||||
uint16_t m_signature;
|
||||
uint16_t m_capabilities;
|
||||
uint32_t m_command_set;
|
||||
@@ -33,8 +41,6 @@ namespace Kernel
|
||||
uint64_t m_lba_count;
|
||||
char m_model[41];
|
||||
|
||||
friend class ATABus;
|
||||
|
||||
public:
|
||||
virtual Mode mode() const override { return { Mode::IFBLK | Mode::IRUSR | Mode::IRGRP }; }
|
||||
virtual uid_t uid() const override { return 0; }
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/PCI.h>
|
||||
#include <kernel/Storage/StorageController.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class ATABus;
|
||||
|
||||
class ATAController final : public StorageController
|
||||
{
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::RefPtr<ATAController>> create(const PCI::Device&);
|
||||
|
||||
virtual BAN::Vector<BAN::RefPtr<StorageDevice>> devices() override;
|
||||
|
||||
private:
|
||||
ATAController();
|
||||
BAN::ErrorOr<void> initialize(const PCI::Device& device);
|
||||
|
||||
private:
|
||||
ATABus* m_buses[2] { nullptr, nullptr };
|
||||
friend class ATABus;
|
||||
|
||||
public:
|
||||
virtual Mode mode() const override { return { Mode::IFCHR }; }
|
||||
virtual uid_t uid() const override { return 0; }
|
||||
virtual gid_t gid() const override { return 0; }
|
||||
virtual dev_t rdev() const override { return m_rdev; }
|
||||
|
||||
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) { return BAN::Error::from_errno(ENOTSUP); }
|
||||
|
||||
private:
|
||||
const dev_t m_rdev;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,17 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class StorageController : public CharacterDevice
|
||||
class StorageController
|
||||
{
|
||||
public:
|
||||
StorageController()
|
||||
: CharacterDevice(0660, 0, 0)
|
||||
{ }
|
||||
virtual BAN::Vector<BAN::RefPtr<StorageDevice>> devices() = 0;
|
||||
virtual ~StorageController() {}
|
||||
virtual BAN::ErrorOr<void> initialize() = 0;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace Kernel
|
||||
class Partition final : public BlockDevice
|
||||
{
|
||||
public:
|
||||
Partition(StorageDevice&, const GUID&, const GUID&, uint64_t, uint64_t, uint64_t, const char*, uint32_t);
|
||||
static BAN::ErrorOr<BAN::RefPtr<Partition>> create(StorageDevice&, const GUID& type, const GUID& guid, uint64_t start, uint64_t end, uint64_t attr, const char* label, uint32_t index);
|
||||
|
||||
const GUID& partition_type() const { return m_type; }
|
||||
const GUID& partition_guid() const { return m_guid; }
|
||||
@@ -32,6 +32,11 @@ namespace Kernel
|
||||
|
||||
BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer);
|
||||
BAN::ErrorOr<void> write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer);
|
||||
|
||||
virtual BAN::StringView name() const override { return m_name; }
|
||||
|
||||
private:
|
||||
Partition(StorageDevice&, const GUID&, const GUID&, uint64_t, uint64_t, uint64_t, const char*, uint32_t);
|
||||
|
||||
private:
|
||||
StorageDevice& m_device;
|
||||
@@ -41,6 +46,7 @@ namespace Kernel
|
||||
const uint64_t m_lba_end;
|
||||
const uint64_t m_attributes;
|
||||
char m_label[36 * 4 + 1];
|
||||
const BAN::String m_name;
|
||||
|
||||
public:
|
||||
virtual bool is_partition() const override { return true; }
|
||||
@@ -73,8 +79,8 @@ namespace Kernel
|
||||
virtual uint32_t sector_size() const = 0;
|
||||
virtual uint64_t total_size() const = 0;
|
||||
|
||||
BAN::Vector<Partition*>& partitions() { return m_partitions; }
|
||||
const BAN::Vector<Partition*>& partitions() const { return m_partitions; }
|
||||
BAN::Vector<BAN::RefPtr<Partition>>& partitions() { return m_partitions; }
|
||||
const BAN::Vector<BAN::RefPtr<Partition>>& partitions() const { return m_partitions; }
|
||||
|
||||
BAN::ErrorOr<void> sync_disk_cache();
|
||||
virtual bool is_storage_device() const override { return true; }
|
||||
@@ -85,9 +91,9 @@ namespace Kernel
|
||||
void add_disk_cache();
|
||||
|
||||
private:
|
||||
SpinLock m_lock;
|
||||
BAN::Optional<DiskCache> m_disk_cache;
|
||||
BAN::Vector<Partition*> m_partitions;
|
||||
SpinLock m_lock;
|
||||
BAN::Optional<DiskCache> m_disk_cache;
|
||||
BAN::Vector<BAN::RefPtr<Partition>> m_partitions;
|
||||
|
||||
friend class DiskCache;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user