2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
//
|
|
|
|
// Parallel ATA (PATA) controller driver
|
|
|
|
//
|
|
|
|
// This driver describes a logical PATA Channel. Each channel can connect up to 2
|
|
|
|
// IDE Hard Disk Drives. The drives themselves can be either the master drive (hd0)
|
|
|
|
// or the slave drive (hd1).
|
|
|
|
//
|
|
|
|
// More information about the ATA spec for PATA can be found here:
|
|
|
|
// ftp://ftp.seagate.com/acrobat/reference/111-1c.pdf
|
|
|
|
//
|
2020-05-28 20:40:53 +02:00
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/RefPtr.h>
|
2020-11-02 11:16:01 -07:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2020-05-28 20:40:53 +02:00
|
|
|
#include <Kernel/IO.h>
|
2020-12-19 12:50:57 +02:00
|
|
|
#include <Kernel/Interrupts/IRQHandler.h>
|
2019-07-28 23:44:01 +10:00
|
|
|
#include <Kernel/Lock.h>
|
2020-05-28 20:40:53 +02:00
|
|
|
#include <Kernel/PhysicalAddress.h>
|
2020-06-24 14:07:28 -06:00
|
|
|
#include <Kernel/Random.h>
|
2020-12-19 12:50:57 +02:00
|
|
|
#include <Kernel/Storage/StorageDevice.h>
|
2019-07-28 23:44:01 +10:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
2019-12-01 12:47:53 +01:00
|
|
|
#include <Kernel/WaitQueue.h>
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-11-02 11:16:01 -07:00
|
|
|
class AsyncBlockDeviceRequest;
|
|
|
|
|
2020-12-19 12:50:57 +02:00
|
|
|
class IDEController;
|
2021-03-27 09:01:00 +03:00
|
|
|
class IDEChannel : public RefCounted<IDEChannel>
|
2021-03-27 06:22:55 +03:00
|
|
|
, public IRQHandler {
|
2020-12-19 12:50:57 +02:00
|
|
|
friend class IDEController;
|
2019-07-28 23:44:01 +10:00
|
|
|
friend class PATADiskDevice;
|
|
|
|
AK_MAKE_ETERNAL
|
|
|
|
public:
|
|
|
|
enum class ChannelType : u8 {
|
|
|
|
Primary,
|
|
|
|
Secondary
|
|
|
|
};
|
|
|
|
|
2020-12-19 12:50:57 +02:00
|
|
|
struct IOAddressGroup {
|
|
|
|
IOAddressGroup(IOAddress io_base, IOAddress control_base, IOAddress bus_master_base)
|
|
|
|
: m_io_base(io_base)
|
|
|
|
, m_control_base(control_base)
|
|
|
|
, m_bus_master_base(bus_master_base)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-27 09:22:25 +03:00
|
|
|
IOAddressGroup(IOAddress io_base, IOAddress control_base)
|
|
|
|
: m_io_base(io_base)
|
|
|
|
, m_control_base(control_base)
|
|
|
|
, m_bus_master_base()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-16 18:03:11 +03:00
|
|
|
IOAddressGroup(const IOAddressGroup& other, IOAddress bus_master_base)
|
|
|
|
: m_io_base(other.io_base())
|
|
|
|
, m_control_base(other.control_base())
|
|
|
|
, m_bus_master_base(bus_master_base)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-19 12:50:57 +02:00
|
|
|
// Disable default implementations that would use surprising integer promotion.
|
|
|
|
bool operator==(const IOAddressGroup&) const = delete;
|
|
|
|
bool operator<=(const IOAddressGroup&) const = delete;
|
|
|
|
bool operator>=(const IOAddressGroup&) const = delete;
|
|
|
|
bool operator<(const IOAddressGroup&) const = delete;
|
|
|
|
bool operator>(const IOAddressGroup&) const = delete;
|
|
|
|
|
|
|
|
IOAddress io_base() const { return m_io_base; };
|
|
|
|
IOAddress control_base() const { return m_control_base; }
|
2021-03-27 09:22:25 +03:00
|
|
|
Optional<IOAddress> bus_master_base() const { return m_bus_master_base; }
|
2020-12-19 12:50:57 +02:00
|
|
|
|
|
|
|
const IOAddressGroup& operator=(const IOAddressGroup& group)
|
|
|
|
{
|
|
|
|
m_io_base = group.io_base();
|
|
|
|
m_control_base = group.control_base();
|
|
|
|
m_bus_master_base = group.bus_master_base();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IOAddress m_io_base;
|
|
|
|
IOAddress m_control_base;
|
2021-03-27 09:22:25 +03:00
|
|
|
Optional<IOAddress> m_bus_master_base;
|
2020-12-19 12:50:57 +02:00
|
|
|
};
|
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
public:
|
2021-03-27 09:01:00 +03:00
|
|
|
static NonnullRefPtr<IDEChannel> create(const IDEController&, IOAddressGroup, ChannelType type);
|
2021-03-27 21:44:25 +03:00
|
|
|
static NonnullRefPtr<IDEChannel> create(const IDEController&, u8 irq, IOAddressGroup, ChannelType type);
|
2020-12-19 12:50:57 +02:00
|
|
|
virtual ~IDEChannel() override;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-12-19 13:56:12 +02:00
|
|
|
RefPtr<StorageDevice> master_device() const;
|
|
|
|
RefPtr<StorageDevice> slave_device() const;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-03-05 19:13:55 +02:00
|
|
|
virtual const char* purpose() const override { return "PATA Channel"; }
|
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
virtual bool is_dma_enabled() const { return false; }
|
2021-03-27 06:22:55 +03:00
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
private:
|
|
|
|
void complete_current_request(AsyncDeviceRequest::RequestResult);
|
2021-03-27 21:44:25 +03:00
|
|
|
void initialize();
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
protected:
|
2021-01-29 19:37:40 +00:00
|
|
|
enum class LBAMode : u8 {
|
|
|
|
None, // CHS
|
|
|
|
TwentyEightBit,
|
|
|
|
FortyEightBit,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Direction : u8 {
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
};
|
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
IDEChannel(const IDEController&, IOAddressGroup, ChannelType type);
|
2021-03-27 21:44:25 +03:00
|
|
|
IDEChannel(const IDEController&, u8 irq, IOAddressGroup, ChannelType type);
|
2021-03-27 09:01:00 +03:00
|
|
|
//^ IRQHandler
|
|
|
|
virtual void handle_irq(const RegisterState&) override;
|
|
|
|
|
|
|
|
virtual void send_ata_io_command(LBAMode lba_mode, Direction direction) const;
|
|
|
|
|
|
|
|
virtual void ata_read_sectors(bool, u16);
|
|
|
|
virtual void ata_write_sectors(bool, u16);
|
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
void detect_disks();
|
2021-01-29 19:37:40 +00:00
|
|
|
String channel_type_string() const;
|
|
|
|
|
|
|
|
void try_disambiguate_error();
|
2021-04-16 18:03:11 +03:00
|
|
|
bool wait_until_not_busy(bool slave, size_t milliseconds_timeout);
|
|
|
|
bool wait_until_not_busy(size_t milliseconds_timeout);
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
void start_request(AsyncBlockDeviceRequest&, bool, u16);
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2021-02-05 06:51:47 +02:00
|
|
|
void clear_pending_interrupts() const;
|
|
|
|
|
2021-03-27 09:01:00 +03:00
|
|
|
void ata_access(Direction, bool, u64, u8, u16);
|
|
|
|
|
2020-11-02 11:16:01 -07:00
|
|
|
bool ata_do_read_sector();
|
|
|
|
void ata_do_write_sector();
|
2020-03-06 03:22:53 +02:00
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
// Data members
|
2021-01-29 19:37:40 +00:00
|
|
|
ChannelType m_channel_type { ChannelType::Primary };
|
2020-12-19 12:50:57 +02:00
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
volatile u8 m_device_error { 0 };
|
2020-06-24 14:07:28 -06:00
|
|
|
EntropySource m_entropy_source;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-12-19 12:50:57 +02:00
|
|
|
RefPtr<StorageDevice> m_master;
|
|
|
|
RefPtr<StorageDevice> m_slave;
|
2020-11-02 11:16:01 -07:00
|
|
|
|
2021-03-27 09:53:59 +03:00
|
|
|
RefPtr<AsyncBlockDeviceRequest> m_current_request;
|
|
|
|
size_t m_current_request_block_index { 0 };
|
2020-11-02 11:16:01 -07:00
|
|
|
bool m_current_request_flushing_cache { false };
|
|
|
|
SpinLock<u8> m_request_lock;
|
2021-03-27 09:53:59 +03:00
|
|
|
Lock m_lock { "IDEChannel" };
|
2020-12-19 12:50:57 +02:00
|
|
|
|
|
|
|
IOAddressGroup m_io_group;
|
|
|
|
NonnullRefPtr<IDEController> m_parent_controller;
|
2019-07-28 23:44:01 +10:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|