2021-02-26 07:25:45 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-26 07:25:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/RefPtr.h>
|
|
|
|
#include <Kernel/Devices/Device.h>
|
|
|
|
#include <Kernel/IO.h>
|
|
|
|
#include <Kernel/Interrupts/IRQHandler.h>
|
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#include <Kernel/PhysicalAddress.h>
|
|
|
|
#include <Kernel/Random.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2021-03-19 02:32:42 +03:00
|
|
|
#include <Kernel/SpinLock.h>
|
2021-02-26 07:25:45 +03:00
|
|
|
#include <Kernel/Storage/AHCI.h>
|
|
|
|
#include <Kernel/Storage/AHCIPortHandler.h>
|
|
|
|
#include <Kernel/Storage/StorageDevice.h>
|
2021-02-26 15:38:17 +03:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2021-02-26 07:25:45 +03:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
2021-04-24 04:30:27 +03:00
|
|
|
#include <Kernel/VM/ScatterGatherList.h>
|
2021-02-26 07:25:45 +03:00
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class AsyncBlockDeviceRequest;
|
|
|
|
|
|
|
|
class AHCIPortHandler;
|
|
|
|
class SATADiskDevice;
|
|
|
|
class AHCIPort : public RefCounted<AHCIPort> {
|
|
|
|
friend class AHCIPortHandler;
|
|
|
|
friend class SATADiskDevice;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UNMAP_AFTER_INIT static NonnullRefPtr<AHCIPort> create(const AHCIPortHandler&, volatile AHCI::PortRegisters&, u32 port_index);
|
|
|
|
|
|
|
|
u32 port_index() const { return m_port_index; }
|
|
|
|
u32 representative_port_index() const { return port_index() + 1; }
|
|
|
|
bool is_operable() const;
|
|
|
|
bool is_hot_pluggable() const;
|
|
|
|
bool is_atapi_attached() const { return m_port_registers.sig == (u32)AHCI::DeviceSignature::ATAPI; };
|
|
|
|
|
|
|
|
RefPtr<StorageDevice> connected_device() const { return m_connected_device; }
|
|
|
|
|
|
|
|
bool reset();
|
2021-03-12 19:35:16 +03:00
|
|
|
UNMAP_AFTER_INIT bool initialize_without_reset();
|
2021-02-26 07:25:45 +03:00
|
|
|
void handle_interrupt();
|
|
|
|
|
|
|
|
private:
|
2021-03-12 19:35:16 +03:00
|
|
|
bool is_phy_enabled() const { return (m_port_registers.ssts & 0xf) == 3; }
|
2021-03-19 02:32:42 +03:00
|
|
|
bool initialize(ScopedSpinLock<SpinLock<u8>>&);
|
2021-03-12 19:35:16 +03:00
|
|
|
|
2021-02-26 07:25:45 +03:00
|
|
|
UNMAP_AFTER_INIT AHCIPort(const AHCIPortHandler&, volatile AHCI::PortRegisters&, u32 port_index);
|
|
|
|
|
|
|
|
ALWAYS_INLINE void clear_sata_error_register() const;
|
|
|
|
|
|
|
|
void eject();
|
|
|
|
|
|
|
|
const char* try_disambiguate_sata_status();
|
2021-03-15 10:29:39 +03:00
|
|
|
void try_disambiguate_sata_error();
|
2021-02-26 07:25:45 +03:00
|
|
|
|
2021-03-19 02:32:42 +03:00
|
|
|
bool initiate_sata_reset(ScopedSpinLock<SpinLock<u8>>&);
|
2021-02-26 07:25:45 +03:00
|
|
|
void rebase();
|
|
|
|
void recover_from_fatal_error();
|
|
|
|
bool shutdown();
|
|
|
|
ALWAYS_INLINE void spin_up() const;
|
|
|
|
ALWAYS_INLINE void power_on() const;
|
|
|
|
|
|
|
|
void start_request(AsyncBlockDeviceRequest&);
|
|
|
|
void complete_current_request(AsyncDeviceRequest::RequestResult);
|
|
|
|
bool access_device(AsyncBlockDeviceRequest::RequestType, u64 lba, u8 block_count);
|
2021-02-26 15:38:17 +03:00
|
|
|
size_t calculate_descriptors_count(size_t block_count) const;
|
|
|
|
[[nodiscard]] Optional<AsyncDeviceRequest::RequestResult> prepare_and_set_scatter_list(AsyncBlockDeviceRequest& request);
|
2021-02-26 07:25:45 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE bool is_interrupts_enabled() const;
|
|
|
|
|
|
|
|
bool spin_until_ready() const;
|
|
|
|
|
2021-03-19 02:32:42 +03:00
|
|
|
bool identify_device(ScopedSpinLock<SpinLock<u8>>&);
|
2021-02-26 07:25:45 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE void start_command_list_processing() const;
|
|
|
|
ALWAYS_INLINE void mark_command_header_ready_to_process(u8 command_header_index) const;
|
|
|
|
ALWAYS_INLINE void stop_command_list_processing() const;
|
|
|
|
|
|
|
|
ALWAYS_INLINE void start_fis_receiving() const;
|
|
|
|
ALWAYS_INLINE void stop_fis_receiving() const;
|
|
|
|
|
|
|
|
ALWAYS_INLINE void set_active_state() const;
|
|
|
|
ALWAYS_INLINE void set_sleep_state() const;
|
|
|
|
|
|
|
|
void set_interface_state(AHCI::DeviceDetectionInitialization);
|
|
|
|
|
|
|
|
Optional<u8> try_to_find_unused_command_header();
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool is_interface_disabled() const { return (m_port_registers.ssts & 0xf) == 4; };
|
|
|
|
|
|
|
|
// Data members
|
|
|
|
|
|
|
|
EntropySource m_entropy_source;
|
2021-03-19 02:32:42 +03:00
|
|
|
RefPtr<AsyncBlockDeviceRequest> m_current_request;
|
|
|
|
SpinLock<u8> m_hard_lock;
|
|
|
|
Lock m_lock { "AHCIPort" };
|
|
|
|
|
|
|
|
mutable bool m_wait_for_completion { false };
|
|
|
|
bool m_wait_connect_for_completion { false };
|
2021-02-26 07:25:45 +03:00
|
|
|
|
|
|
|
NonnullRefPtrVector<PhysicalPage> m_dma_buffers;
|
|
|
|
NonnullRefPtrVector<PhysicalPage> m_command_table_pages;
|
|
|
|
RefPtr<PhysicalPage> m_command_list_page;
|
|
|
|
OwnPtr<Region> m_command_list_region;
|
|
|
|
RefPtr<PhysicalPage> m_fis_receive_page;
|
|
|
|
RefPtr<StorageDevice> m_connected_device;
|
|
|
|
|
|
|
|
u32 m_port_index;
|
|
|
|
volatile AHCI::PortRegisters& m_port_registers;
|
|
|
|
NonnullRefPtr<AHCIPortHandler> m_parent_handler;
|
|
|
|
AHCI::PortInterruptStatusBitField m_interrupt_status;
|
|
|
|
AHCI::PortInterruptEnableBitField m_interrupt_enable;
|
2021-02-26 15:38:17 +03:00
|
|
|
|
2021-04-24 04:30:27 +03:00
|
|
|
RefPtr<ScatterGatherList> m_current_scatter_list;
|
2021-02-26 07:25:45 +03:00
|
|
|
bool m_disabled_by_firmware { false };
|
|
|
|
};
|
|
|
|
}
|