2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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/OwnPtr.h>
|
|
|
|
#include <AK/RefPtr.h>
|
2020-05-28 20:40:53 +02:00
|
|
|
#include <Kernel/IO.h>
|
2019-07-28 23:44:01 +10:00
|
|
|
#include <Kernel/Lock.h>
|
2019-12-31 13:04:30 +02:00
|
|
|
#include <Kernel/PCI/Access.h>
|
2020-02-22 20:08:20 +02:00
|
|
|
#include <Kernel/PCI/Device.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>
|
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 {
|
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
struct PhysicalRegionDescriptor {
|
|
|
|
PhysicalAddress offset;
|
|
|
|
u16 size { 0 };
|
|
|
|
u16 end_of_table { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
class PATADiskDevice;
|
2020-02-22 20:08:20 +02:00
|
|
|
class PATAChannel final : public PCI::Device {
|
2019-07-28 23:44:01 +10:00
|
|
|
friend class PATADiskDevice;
|
|
|
|
AK_MAKE_ETERNAL
|
|
|
|
public:
|
|
|
|
enum class ChannelType : u8 {
|
|
|
|
Primary,
|
|
|
|
Secondary
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2020-01-22 22:23:50 +01:00
|
|
|
static OwnPtr<PATAChannel> create(ChannelType type, bool force_pio);
|
2020-02-22 20:08:20 +02:00
|
|
|
PATAChannel(PCI::Address address, ChannelType type, bool force_pio);
|
2019-07-28 23:44:01 +10:00
|
|
|
virtual ~PATAChannel() override;
|
|
|
|
|
|
|
|
RefPtr<PATADiskDevice> master_device() { return m_master; };
|
|
|
|
RefPtr<PATADiskDevice> slave_device() { return m_slave; };
|
|
|
|
|
2020-03-05 19:13:55 +02:00
|
|
|
virtual const char* purpose() const override { return "PATA Channel"; }
|
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
private:
|
|
|
|
//^ IRQHandler
|
2020-03-09 16:24:29 +02:00
|
|
|
virtual void handle_irq(const RegisterState&) override;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2019-11-13 19:29:16 +02:00
|
|
|
void initialize(bool force_pio);
|
2019-07-28 23:44:01 +10:00
|
|
|
void detect_disks();
|
|
|
|
|
2020-11-04 21:25:26 +01:00
|
|
|
void wait_for_irq();
|
|
|
|
bool ata_read_sectors_with_dma(u32, u16, UserOrKernelBuffer&, bool);
|
|
|
|
bool ata_write_sectors_with_dma(u32, u16, const UserOrKernelBuffer&, bool);
|
|
|
|
bool ata_read_sectors(u32, u16, UserOrKernelBuffer&, bool);
|
|
|
|
bool ata_write_sectors(u32, u16, const UserOrKernelBuffer&, bool);
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-11-04 21:25:26 +01:00
|
|
|
inline void prepare_for_irq();
|
2020-03-06 03:22:53 +02:00
|
|
|
|
2019-07-28 23:44:01 +10:00
|
|
|
// Data members
|
|
|
|
u8 m_channel_number { 0 }; // Channel number. 0 = master, 1 = slave
|
2020-03-01 20:47:11 +02:00
|
|
|
IOAddress m_io_base;
|
|
|
|
IOAddress m_control_base;
|
2019-07-28 23:44:01 +10:00
|
|
|
volatile u8 m_device_error { 0 };
|
2019-12-01 12:47:53 +01:00
|
|
|
|
|
|
|
WaitQueue m_irq_queue;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-01-17 19:59:20 +01:00
|
|
|
PhysicalRegionDescriptor& prdt() { return *reinterpret_cast<PhysicalRegionDescriptor*>(m_prdt_page->paddr().offset(0xc0000000).as_ptr()); }
|
|
|
|
RefPtr<PhysicalPage> m_prdt_page;
|
2019-07-28 23:44:01 +10:00
|
|
|
RefPtr<PhysicalPage> m_dma_buffer_page;
|
2020-03-01 20:47:11 +02:00
|
|
|
IOAddress m_bus_master_base;
|
2019-07-28 23:44:01 +10:00
|
|
|
Lockable<bool> m_dma_enabled;
|
2020-06-24 14:07:28 -06:00
|
|
|
EntropySource m_entropy_source;
|
2019-07-28 23:44:01 +10:00
|
|
|
|
|
|
|
RefPtr<PATADiskDevice> m_master;
|
|
|
|
RefPtr<PATADiskDevice> m_slave;
|
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|