mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
Kernel: Introduce the new Storage subsystem
This new subsystem is somewhat replacing the IDE disk code we had with a new flexible design. StorageDevice is a generic class that represent a generic storage device. It is meant that specific storage hardware will override the interface. StorageController is a generic class that represent a storage controller that can be found in a machine. The IDEController class governs two IDEChannels. An IDEChannel is responsible to manage the master & slave devices of the channel, therefore an IDEChannel is an IRQHandler.
This commit is contained in:
parent
39c1783387
commit
0a2b00a1bf
12 changed files with 604 additions and 213 deletions
615
Kernel/Storage/IDEChannel.cpp
Normal file
615
Kernel/Storage/IDEChannel.cpp
Normal file
|
@ -0,0 +1,615 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Singleton.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <Kernel/FileSystem/ProcFS.h>
|
||||
#include <Kernel/IO.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/Storage/IDEChannel.h>
|
||||
#include <Kernel/Storage/IDEController.h>
|
||||
#include <Kernel/Storage/PATADiskDevice.h>
|
||||
#include <Kernel/VM/MemoryManager.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
#define PATA_PRIMARY_IRQ 14
|
||||
#define PATA_SECONDARY_IRQ 15
|
||||
|
||||
//#define PATA_DEBUG
|
||||
|
||||
#define ATA_SR_BSY 0x80
|
||||
#define ATA_SR_DRDY 0x40
|
||||
#define ATA_SR_DF 0x20
|
||||
#define ATA_SR_DSC 0x10
|
||||
#define ATA_SR_DRQ 0x08
|
||||
#define ATA_SR_CORR 0x04
|
||||
#define ATA_SR_IDX 0x02
|
||||
#define ATA_SR_ERR 0x01
|
||||
|
||||
#define ATA_ER_BBK 0x80
|
||||
#define ATA_ER_UNC 0x40
|
||||
#define ATA_ER_MC 0x20
|
||||
#define ATA_ER_IDNF 0x10
|
||||
#define ATA_ER_MCR 0x08
|
||||
#define ATA_ER_ABRT 0x04
|
||||
#define ATA_ER_TK0NF 0x02
|
||||
#define ATA_ER_AMNF 0x01
|
||||
|
||||
#define ATA_CMD_READ_PIO 0x20
|
||||
#define ATA_CMD_READ_PIO_EXT 0x24
|
||||
#define ATA_CMD_READ_DMA 0xC8
|
||||
#define ATA_CMD_READ_DMA_EXT 0x25
|
||||
#define ATA_CMD_WRITE_PIO 0x30
|
||||
#define ATA_CMD_WRITE_PIO_EXT 0x34
|
||||
#define ATA_CMD_WRITE_DMA 0xCA
|
||||
#define ATA_CMD_WRITE_DMA_EXT 0x35
|
||||
#define ATA_CMD_CACHE_FLUSH 0xE7
|
||||
#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
|
||||
#define ATA_CMD_PACKET 0xA0
|
||||
#define ATA_CMD_IDENTIFY_PACKET 0xA1
|
||||
#define ATA_CMD_IDENTIFY 0xEC
|
||||
|
||||
#define ATAPI_CMD_READ 0xA8
|
||||
#define ATAPI_CMD_EJECT 0x1B
|
||||
|
||||
#define ATA_IDENT_DEVICETYPE 0
|
||||
#define ATA_IDENT_CYLINDERS 2
|
||||
#define ATA_IDENT_HEADS 6
|
||||
#define ATA_IDENT_SECTORS 12
|
||||
#define ATA_IDENT_SERIAL 20
|
||||
#define ATA_IDENT_MODEL 54
|
||||
#define ATA_IDENT_CAPABILITIES 98
|
||||
#define ATA_IDENT_FIELDVALID 106
|
||||
#define ATA_IDENT_MAX_LBA 120
|
||||
#define ATA_IDENT_COMMANDSETS 164
|
||||
#define ATA_IDENT_MAX_LBA_EXT 200
|
||||
|
||||
#define IDE_ATA 0x00
|
||||
#define IDE_ATAPI 0x01
|
||||
|
||||
#define ATA_REG_DATA 0x00
|
||||
#define ATA_REG_ERROR 0x01
|
||||
#define ATA_REG_FEATURES 0x01
|
||||
#define ATA_REG_SECCOUNT0 0x02
|
||||
#define ATA_REG_LBA0 0x03
|
||||
#define ATA_REG_LBA1 0x04
|
||||
#define ATA_REG_LBA2 0x05
|
||||
#define ATA_REG_HDDEVSEL 0x06
|
||||
#define ATA_REG_COMMAND 0x07
|
||||
#define ATA_REG_STATUS 0x07
|
||||
#define ATA_CTL_CONTROL 0x00
|
||||
#define ATA_CTL_ALTSTATUS 0x00
|
||||
#define ATA_CTL_DEVADDRESS 0x01
|
||||
|
||||
#define PCI_Mass_Storage_Class 0x1
|
||||
#define PCI_IDE_Controller_Subclass 0x1
|
||||
|
||||
NonnullOwnPtr<IDEChannel> IDEChannel::create(const IDEController& controller, IOAddressGroup io_group, ChannelType type, bool force_pio)
|
||||
{
|
||||
return make<IDEChannel>(controller, io_group, type, force_pio);
|
||||
}
|
||||
|
||||
RefPtr<StorageDevice> IDEChannel::master_device()
|
||||
{
|
||||
return m_master;
|
||||
}
|
||||
RefPtr<StorageDevice> IDEChannel::slave_device()
|
||||
{
|
||||
return m_slave;
|
||||
}
|
||||
|
||||
IDEChannel::IDEChannel(const IDEController& controller, IOAddressGroup io_group, ChannelType type, bool force_pio)
|
||||
: IRQHandler(type == ChannelType::Primary ? PATA_PRIMARY_IRQ : PATA_SECONDARY_IRQ)
|
||||
, m_channel_number((type == ChannelType::Primary ? 0 : 1))
|
||||
, m_io_group(io_group)
|
||||
, m_parent_controller(controller)
|
||||
{
|
||||
disable_irq();
|
||||
|
||||
m_dma_enabled.resource() = !force_pio;
|
||||
ProcFS::add_sys_bool("ide_dma", m_dma_enabled);
|
||||
|
||||
initialize(force_pio);
|
||||
detect_disks();
|
||||
enable_irq();
|
||||
}
|
||||
|
||||
IDEChannel::~IDEChannel()
|
||||
{
|
||||
}
|
||||
|
||||
void IDEChannel::start_request(AsyncBlockDeviceRequest& request, bool use_dma, bool is_slave)
|
||||
{
|
||||
ScopedSpinLock lock(m_request_lock);
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel::start_request";
|
||||
#endif
|
||||
m_current_request = &request;
|
||||
m_current_request_block_index = 0;
|
||||
m_current_request_uses_dma = use_dma;
|
||||
m_current_request_flushing_cache = false;
|
||||
|
||||
if (request.request_type() == AsyncBlockDeviceRequest::Read) {
|
||||
if (use_dma)
|
||||
ata_read_sectors_with_dma(is_slave);
|
||||
else
|
||||
ata_read_sectors(is_slave);
|
||||
} else {
|
||||
if (use_dma)
|
||||
ata_write_sectors_with_dma(is_slave);
|
||||
else
|
||||
ata_write_sectors(is_slave);
|
||||
}
|
||||
}
|
||||
|
||||
void IDEChannel::complete_current_request(AsyncDeviceRequest::RequestResult result)
|
||||
{
|
||||
// NOTE: this may be called from the interrupt handler!
|
||||
ASSERT(m_current_request);
|
||||
ASSERT(m_request_lock.is_locked());
|
||||
|
||||
// Now schedule reading back the buffer as soon as we leave the irq handler.
|
||||
// This is important so that we can safely write the buffer back,
|
||||
// which could cause page faults. Note that this may be called immediately
|
||||
// before Processor::deferred_call_queue returns!
|
||||
Processor::deferred_call_queue([this, result]() {
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel::complete_current_request result: " << result;
|
||||
#endif
|
||||
ASSERT(m_current_request);
|
||||
auto& request = *m_current_request;
|
||||
m_current_request = nullptr;
|
||||
|
||||
if (m_current_request_uses_dma) {
|
||||
if (result == AsyncDeviceRequest::Success) {
|
||||
if (request.request_type() == AsyncBlockDeviceRequest::Read) {
|
||||
if (!request.write_to_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
|
||||
request.complete(AsyncDeviceRequest::MemoryFault);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// I read somewhere that this may trigger a cache flush so let's do it.
|
||||
m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
|
||||
}
|
||||
}
|
||||
|
||||
request.complete(result);
|
||||
});
|
||||
}
|
||||
|
||||
void IDEChannel::initialize(bool force_pio)
|
||||
{
|
||||
m_parent_controller->enable_pin_based_interrupts();
|
||||
if (force_pio) {
|
||||
klog() << "IDEChannel: Requested to force PIO mode; not setting up DMA";
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's try to set up DMA transfers.
|
||||
PCI::enable_bus_mastering(m_parent_controller->pci_address());
|
||||
m_prdt_page = MM.allocate_supervisor_physical_page();
|
||||
prdt().end_of_table = 0x8000;
|
||||
m_dma_buffer_page = MM.allocate_supervisor_physical_page();
|
||||
klog() << "IDEChannel: Bus master IDE: " << m_io_group.bus_master_base();
|
||||
}
|
||||
|
||||
static void print_ide_status(u8 status)
|
||||
{
|
||||
klog() << "IDEChannel: print_ide_status: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0) << " DSC=" << ((status & ATA_SR_DSC) != 0) << " DF=" << ((status & ATA_SR_DF) != 0) << " CORR=" << ((status & ATA_SR_CORR) != 0) << " IDX=" << ((status & ATA_SR_IDX) != 0) << " ERR=" << ((status & ATA_SR_ERR) != 0);
|
||||
}
|
||||
|
||||
void IDEChannel::handle_irq(const RegisterState&)
|
||||
{
|
||||
u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
||||
|
||||
m_entropy_source.add_random_event(status);
|
||||
|
||||
u8 bstatus = m_io_group.bus_master_base().offset(2).in<u8>();
|
||||
if (!(bstatus & 0x4)) {
|
||||
// interrupt not from this device, ignore
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel: ignore interrupt";
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
ScopedSpinLock lock(m_request_lock);
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel: interrupt: DRQ=" << ((status & ATA_SR_DRQ) != 0) << " BSY=" << ((status & ATA_SR_BSY) != 0) << " DRDY=" << ((status & ATA_SR_DRDY) != 0);
|
||||
#endif
|
||||
|
||||
if (!m_current_request) {
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel: IRQ but no pending request!";
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
bool received_all_irqs = m_current_request_uses_dma || m_current_request_block_index + 1 >= m_current_request->block_count();
|
||||
|
||||
if (status & ATA_SR_ERR) {
|
||||
print_ide_status(status);
|
||||
m_device_error = m_io_group.io_base().offset(ATA_REG_ERROR).in<u8>();
|
||||
klog() << "IDEChannel: Error " << String::format("%b", m_device_error) << "!";
|
||||
complete_current_request(AsyncDeviceRequest::Failure);
|
||||
return;
|
||||
}
|
||||
|
||||
m_device_error = 0;
|
||||
if (received_all_irqs) {
|
||||
complete_current_request(AsyncDeviceRequest::Success);
|
||||
} else {
|
||||
ASSERT(!m_current_request_uses_dma);
|
||||
|
||||
// Now schedule reading/writing the buffer as soon as we leave the irq handler.
|
||||
// This is important so that we can safely access the buffers, which could
|
||||
// trigger page faults
|
||||
Processor::deferred_call_queue([this]() {
|
||||
if (m_current_request->request_type() == AsyncBlockDeviceRequest::Read) {
|
||||
dbg() << "IDEChannel: Read block " << m_current_request_block_index << "/" << m_current_request->block_count();
|
||||
if (ata_do_read_sector()) {
|
||||
if (++m_current_request_block_index >= m_current_request->block_count()) {
|
||||
complete_current_request(AsyncDeviceRequest::Success);
|
||||
return;
|
||||
}
|
||||
// Wait for the next block
|
||||
enable_irq();
|
||||
}
|
||||
} else {
|
||||
if (!m_current_request_flushing_cache) {
|
||||
dbg() << "IDEChannel: Wrote block " << m_current_request_block_index << "/" << m_current_request->block_count();
|
||||
if (++m_current_request_block_index >= m_current_request->block_count()) {
|
||||
// We read the last block, flush cache
|
||||
ASSERT(!m_current_request_flushing_cache);
|
||||
m_current_request_flushing_cache = true;
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_CACHE_FLUSH);
|
||||
} else {
|
||||
// Read next block
|
||||
ata_do_write_sector();
|
||||
}
|
||||
} else {
|
||||
complete_current_request(AsyncDeviceRequest::Success);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static void io_delay()
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
IO::in8(0x3f6);
|
||||
}
|
||||
|
||||
void IDEChannel::detect_disks()
|
||||
{
|
||||
// There are only two possible disks connected to a channel
|
||||
for (auto i = 0; i < 2; i++) {
|
||||
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0xA0 | (i << 4)); // First, we need to select the drive itself
|
||||
|
||||
// Apparently these need to be 0 before sending IDENTIFY?!
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0x00);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0x00);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0x00);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0x00);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_IDENTIFY); // Send the ATA_IDENTIFY command
|
||||
|
||||
// Wait for the BSY flag to be reset
|
||||
while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
|
||||
;
|
||||
|
||||
if (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() == 0x00) {
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel: No " << (i == 0 ? "master" : "slave") << " disk detected!";
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
|
||||
ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
|
||||
u8* b = bbuf.data();
|
||||
u16* w = (u16*)wbuf.data();
|
||||
const u16* wbufbase = (u16*)wbuf.data();
|
||||
|
||||
for (u32 i = 0; i < 256; ++i) {
|
||||
u16 data = m_io_group.io_base().offset(ATA_REG_DATA).in<u16>();
|
||||
*(w++) = data;
|
||||
*(b++) = MSB(data);
|
||||
*(b++) = LSB(data);
|
||||
}
|
||||
|
||||
// "Unpad" the device name string.
|
||||
for (u32 i = 93; i > 54 && bbuf[i] == ' '; --i)
|
||||
bbuf[i] = 0;
|
||||
|
||||
u8 cyls = wbufbase[1];
|
||||
u8 heads = wbufbase[3];
|
||||
u8 spt = wbufbase[6];
|
||||
if (cyls == 0 || heads == 0 || spt == 0)
|
||||
continue;
|
||||
klog() << "IDEChannel: Name=" << ((char*)bbuf.data() + 54) << ", C/H/Spt=" << cyls << "/" << heads << "/" << spt;
|
||||
|
||||
int major = (m_channel_number == 0) ? 3 : 4;
|
||||
if (i == 0) {
|
||||
m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, cyls, heads, spt, major, 0);
|
||||
} else {
|
||||
m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, cyls, heads, spt, major, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IDEChannel::ata_read_sectors_with_dma(bool slave_request)
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
u32 lba = request.block_index();
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel::ata_read_sectors_with_dma (" << lba << " x" << request.block_count() << ")";
|
||||
#endif
|
||||
|
||||
prdt().offset = m_dma_buffer_page->paddr();
|
||||
prdt().size = 512 * request.block_count();
|
||||
|
||||
ASSERT(prdt().size <= PAGE_SIZE);
|
||||
|
||||
// Stop bus master
|
||||
m_io_group.bus_master_base().out<u8>(0);
|
||||
|
||||
// Write the PRDT location
|
||||
m_io_group.bus_master_base().offset(4).out(m_prdt_page->paddr().get());
|
||||
|
||||
// Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
|
||||
m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
|
||||
|
||||
// Set transfer direction
|
||||
m_io_group.bus_master_base().out<u8>(0x8);
|
||||
|
||||
while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
|
||||
;
|
||||
|
||||
m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
|
||||
io_delay();
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
|
||||
|
||||
for (;;) {
|
||||
auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
||||
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
||||
break;
|
||||
}
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_DMA_EXT);
|
||||
io_delay();
|
||||
|
||||
enable_irq();
|
||||
// Start bus master
|
||||
m_io_group.bus_master_base().out<u8>(0x9);
|
||||
}
|
||||
|
||||
bool IDEChannel::ata_do_read_sector()
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
||||
ssize_t nwritten = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) {
|
||||
for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
|
||||
*(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
|
||||
return (ssize_t)buffer_bytes;
|
||||
});
|
||||
if (nwritten < 0) {
|
||||
// TODO: Do we need to abort the PATA read if this wasn't the last block?
|
||||
complete_current_request(AsyncDeviceRequest::MemoryFault);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void IDEChannel::ata_read_sectors(bool slave_request)
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
ASSERT(request.block_count() <= 256);
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel::ata_read_sectors";
|
||||
#endif
|
||||
|
||||
while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
|
||||
;
|
||||
|
||||
auto lba = request.block_index();
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel: Reading " << request.block_count() << " sector(s) @ LBA " << lba;
|
||||
#endif
|
||||
|
||||
u8 devsel = 0xe0;
|
||||
if (slave_request)
|
||||
devsel |= 0x10;
|
||||
|
||||
m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | (static_cast<u8>(slave_request) << 4) | 0x40);
|
||||
io_delay();
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_FEATURES).out<u8>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
|
||||
|
||||
for (;;) {
|
||||
auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
||||
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
||||
break;
|
||||
}
|
||||
|
||||
enable_irq();
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_READ_PIO);
|
||||
}
|
||||
|
||||
void IDEChannel::ata_write_sectors_with_dma(bool slave_request)
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
u32 lba = request.block_index();
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel::ata_write_sectors_with_dma (" << lba << " x" << request.block_count() << ")";
|
||||
#endif
|
||||
|
||||
prdt().offset = m_dma_buffer_page->paddr();
|
||||
prdt().size = 512 * request.block_count();
|
||||
|
||||
if (!request.read_from_buffer(request.buffer(), m_dma_buffer_page->paddr().offset(0xc0000000).as_ptr(), 512 * request.block_count())) {
|
||||
complete_current_request(AsyncDeviceRequest::MemoryFault);
|
||||
return;
|
||||
}
|
||||
|
||||
ASSERT(prdt().size <= PAGE_SIZE);
|
||||
|
||||
// Stop bus master
|
||||
m_io_group.bus_master_base().out<u8>(0);
|
||||
|
||||
// Write the PRDT location
|
||||
m_io_group.bus_master_base().offset(4).out<u32>(m_prdt_page->paddr().get());
|
||||
|
||||
// Turn on "Interrupt" and "Error" flag. The error flag should be cleared by hardware.
|
||||
m_io_group.bus_master_base().offset(2).out<u8>(m_io_group.bus_master_base().offset(2).in<u8>() | 0x6);
|
||||
|
||||
while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
|
||||
;
|
||||
|
||||
m_io_group.control_base().offset(ATA_CTL_CONTROL).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(0x40 | (static_cast<u8>(slave_request) << 4));
|
||||
io_delay();
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_FEATURES).out<u16>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>(0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>(0);
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(request.block_count());
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>((lba & 0x000000ff) >> 0);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((lba & 0x0000ff00) >> 8);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((lba & 0x00ff0000) >> 16);
|
||||
|
||||
for (;;) {
|
||||
auto status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
||||
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
||||
break;
|
||||
}
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_DMA_EXT);
|
||||
io_delay();
|
||||
|
||||
enable_irq();
|
||||
// Start bus master
|
||||
m_io_group.bus_master_base().out<u8>(0x1);
|
||||
}
|
||||
|
||||
void IDEChannel::ata_do_write_sector()
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
|
||||
io_delay();
|
||||
while ((m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY) || !(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRQ))
|
||||
;
|
||||
|
||||
u8 status = m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>();
|
||||
ASSERT(status & ATA_SR_DRQ);
|
||||
|
||||
auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
||||
#ifdef PATA_DEBUG
|
||||
dbg() << "IDEChannel: Writing 512 bytes (part " << m_current_request_block_index << ") (status=" << String::format("%b", status) << ")...";
|
||||
#endif
|
||||
ssize_t nread = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](const u8* buffer, size_t buffer_bytes) {
|
||||
for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
|
||||
IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]);
|
||||
return (ssize_t)buffer_bytes;
|
||||
});
|
||||
if (nread < 0)
|
||||
complete_current_request(AsyncDeviceRequest::MemoryFault);
|
||||
}
|
||||
|
||||
void IDEChannel::ata_write_sectors(bool slave_request)
|
||||
{
|
||||
auto& request = *m_current_request;
|
||||
|
||||
ASSERT(request.block_count() <= 256);
|
||||
u32 start_sector = request.block_index();
|
||||
u32 count = request.block_count();
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel::ata_write_sectors request (" << count << " sector(s) @ " << start_sector << ")";
|
||||
#endif
|
||||
|
||||
while (m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY)
|
||||
;
|
||||
|
||||
#ifdef PATA_DEBUG
|
||||
klog() << "IDEChannel: Writing " << count << " sector(s) @ LBA " << start_sector;
|
||||
#endif
|
||||
|
||||
u8 devsel = 0xe0;
|
||||
if (slave_request)
|
||||
devsel |= 0x10;
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_SECCOUNT0).out<u8>(count == 256 ? 0 : LSB(count));
|
||||
m_io_group.io_base().offset(ATA_REG_LBA0).out<u8>(start_sector & 0xff);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA1).out<u8>((start_sector >> 8) & 0xff);
|
||||
m_io_group.io_base().offset(ATA_REG_LBA2).out<u8>((start_sector >> 16) & 0xff);
|
||||
m_io_group.io_base().offset(ATA_REG_HDDEVSEL).out<u8>(devsel | ((start_sector >> 24) & 0xf));
|
||||
|
||||
IO::out8(0x3F6, 0x08);
|
||||
while (!(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRDY))
|
||||
;
|
||||
|
||||
m_io_group.io_base().offset(ATA_REG_COMMAND).out<u8>(ATA_CMD_WRITE_PIO);
|
||||
|
||||
io_delay();
|
||||
while ((m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_BSY) || !(m_io_group.io_base().offset(ATA_REG_STATUS).in<u8>() & ATA_SR_DRQ))
|
||||
;
|
||||
|
||||
enable_irq();
|
||||
ata_do_write_sector();
|
||||
}
|
||||
|
||||
}
|
156
Kernel/Storage/IDEChannel.h
Normal file
156
Kernel/Storage/IDEChannel.h
Normal file
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
#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>
|
||||
#include <Kernel/Storage/StorageDevice.h>
|
||||
#include <Kernel/VM/PhysicalPage.h>
|
||||
#include <Kernel/WaitQueue.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class AsyncBlockDeviceRequest;
|
||||
|
||||
struct PhysicalRegionDescriptor {
|
||||
PhysicalAddress offset;
|
||||
u16 size { 0 };
|
||||
u16 end_of_table { 0 };
|
||||
};
|
||||
|
||||
class IDEController;
|
||||
class IDEChannel final : public IRQHandler {
|
||||
friend class IDEController;
|
||||
friend class PATADiskDevice;
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
enum class ChannelType : u8 {
|
||||
Primary,
|
||||
Secondary
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
// 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; }
|
||||
IOAddress bus_master_base() const { return m_bus_master_base; }
|
||||
|
||||
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;
|
||||
IOAddress m_bus_master_base;
|
||||
};
|
||||
|
||||
public:
|
||||
static NonnullOwnPtr<IDEChannel> create(const IDEController&, IOAddressGroup, ChannelType type, bool force_pio);
|
||||
IDEChannel(const IDEController&, IOAddressGroup, ChannelType type, bool force_pio);
|
||||
virtual ~IDEChannel() override;
|
||||
|
||||
RefPtr<StorageDevice> master_device();
|
||||
RefPtr<StorageDevice> slave_device();
|
||||
|
||||
virtual const char* purpose() const override { return "PATA Channel"; }
|
||||
|
||||
private:
|
||||
//^ IRQHandler
|
||||
virtual void handle_irq(const RegisterState&) override;
|
||||
|
||||
void initialize(bool force_pio);
|
||||
void detect_disks();
|
||||
|
||||
void start_request(AsyncBlockDeviceRequest&, bool, bool);
|
||||
void complete_current_request(AsyncDeviceRequest::RequestResult);
|
||||
|
||||
void ata_read_sectors_with_dma(bool);
|
||||
void ata_read_sectors(bool);
|
||||
bool ata_do_read_sector();
|
||||
void ata_write_sectors_with_dma(bool);
|
||||
void ata_write_sectors(bool);
|
||||
void ata_do_write_sector();
|
||||
|
||||
// Data members
|
||||
u8 m_channel_number { 0 }; // Channel number. 0 = master, 1 = slave
|
||||
|
||||
volatile u8 m_device_error { 0 };
|
||||
|
||||
PhysicalRegionDescriptor& prdt() { return *reinterpret_cast<PhysicalRegionDescriptor*>(m_prdt_page->paddr().offset(0xc0000000).as_ptr()); }
|
||||
RefPtr<PhysicalPage> m_prdt_page;
|
||||
RefPtr<PhysicalPage> m_dma_buffer_page;
|
||||
Lockable<bool> m_dma_enabled;
|
||||
EntropySource m_entropy_source;
|
||||
|
||||
RefPtr<StorageDevice> m_master;
|
||||
RefPtr<StorageDevice> m_slave;
|
||||
|
||||
AsyncBlockDeviceRequest* m_current_request { nullptr };
|
||||
u32 m_current_request_block_index { 0 };
|
||||
bool m_current_request_uses_dma { false };
|
||||
bool m_current_request_flushing_cache { false };
|
||||
SpinLock<u8> m_request_lock;
|
||||
|
||||
IOAddressGroup m_io_group;
|
||||
NonnullRefPtr<IDEController> m_parent_controller;
|
||||
};
|
||||
}
|
103
Kernel/Storage/IDEController.cpp
Normal file
103
Kernel/Storage/IDEController.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/Storage/IDEController.h>
|
||||
#include <Kernel/Storage/PATADiskDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
NonnullRefPtr<IDEController> IDEController::initialize(PCI::Address address, bool force_pio)
|
||||
{
|
||||
return adopt(*new IDEController(address, force_pio));
|
||||
}
|
||||
|
||||
bool IDEController::reset()
|
||||
{
|
||||
TODO();
|
||||
}
|
||||
|
||||
bool IDEController::shutdown()
|
||||
{
|
||||
TODO();
|
||||
}
|
||||
|
||||
void IDEController::start_request(const StorageDevice&, AsyncBlockDeviceRequest&)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
void IDEController::complete_current_request(AsyncDeviceRequest::RequestResult)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
IDEController::IDEController(PCI::Address address, bool force_pio)
|
||||
: StorageController(address)
|
||||
{
|
||||
initialize(force_pio);
|
||||
}
|
||||
|
||||
IDEController::~IDEController()
|
||||
{
|
||||
}
|
||||
|
||||
void IDEController::initialize(bool force_pio)
|
||||
{
|
||||
auto bus_master_base = IOAddress(PCI::get_BAR4(pci_address()) & (~1));
|
||||
|
||||
auto bar0 = PCI::get_BAR0(pci_address());
|
||||
auto base_io = (bar0 == 0x1 || bar0 == 0) ? IOAddress(0x1F0) : IOAddress(bar0);
|
||||
auto bar1 = PCI::get_BAR1(pci_address());
|
||||
auto control_io = (bar1 == 0x1 || bar1 == 0) ? IOAddress(0x3F6) : IOAddress(bar1);
|
||||
|
||||
m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base }, IDEChannel::ChannelType::Primary, force_pio));
|
||||
|
||||
auto bar2 = PCI::get_BAR2(pci_address());
|
||||
base_io = (bar2 == 0x1 || bar2 == 0) ? IOAddress(0x170) : IOAddress(bar2);
|
||||
auto bar3 = PCI::get_BAR3(pci_address());
|
||||
control_io = (bar3 == 0x1 || bar3 == 0) ? IOAddress(0x376) : IOAddress(bar3);
|
||||
m_channels.append(IDEChannel::create(*this, { base_io, control_io, bus_master_base.offset(8) }, IDEChannel::ChannelType::Secondary, force_pio));
|
||||
}
|
||||
|
||||
RefPtr<StorageDevice> IDEController::device(u32 index)
|
||||
{
|
||||
switch (index) {
|
||||
case 0:
|
||||
return m_channels[0].master_device();
|
||||
case 1:
|
||||
return m_channels[0].slave_device();
|
||||
case 2:
|
||||
return m_channels[1].master_device();
|
||||
case 3:
|
||||
return m_channels[1].slave_device();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
61
Kernel/Storage/IDEController.h
Normal file
61
Kernel/Storage/IDEController.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/Storage/IDEChannel.h>
|
||||
#include <Kernel/Storage/StorageController.h>
|
||||
#include <Kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class AsyncBlockDeviceRequest;
|
||||
|
||||
class IDEController final : public StorageController {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
public:
|
||||
static NonnullRefPtr<IDEController> initialize(PCI::Address address, bool force_pio);
|
||||
virtual ~IDEController() override;
|
||||
|
||||
virtual RefPtr<StorageDevice> device(u32 index) override;
|
||||
virtual bool reset() override;
|
||||
virtual bool shutdown() override;
|
||||
virtual void start_request(const StorageDevice&, AsyncBlockDeviceRequest&) override;
|
||||
virtual void complete_current_request(AsyncDeviceRequest::RequestResult) override;
|
||||
|
||||
private:
|
||||
IDEController(PCI::Address address, bool force_pio);
|
||||
|
||||
void initialize(bool force_pio);
|
||||
void detect_disks();
|
||||
|
||||
NonnullOwnPtrVector<IDEChannel> m_channels;
|
||||
};
|
||||
}
|
78
Kernel/Storage/PATADiskDevice.cpp
Normal file
78
Kernel/Storage/PATADiskDevice.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//#define PATA_DEVICE_DEBUG
|
||||
|
||||
#include <AK/Memory.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/Storage/IDEChannel.h>
|
||||
#include <Kernel/Storage/IDEController.h>
|
||||
#include <Kernel/Storage/PATADiskDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, u8 cylinders, u8 heads, u8 spt, int major, int minor)
|
||||
{
|
||||
return adopt(*new PATADiskDevice(controller, channel, type, cylinders, heads, spt, major, minor));
|
||||
}
|
||||
|
||||
PATADiskDevice::PATADiskDevice(const IDEController& controller, IDEChannel& channel, DriveType type, u8 cylinders, u8 heads, u8 spt, int major, int minor)
|
||||
: StorageDevice(controller, major, minor, 512, 0)
|
||||
, m_cylinders(cylinders)
|
||||
, m_heads(heads)
|
||||
, m_sectors_per_track(spt)
|
||||
, m_channel(channel)
|
||||
, m_drive_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
PATADiskDevice::~PATADiskDevice()
|
||||
{
|
||||
}
|
||||
|
||||
const char* PATADiskDevice::class_name() const
|
||||
{
|
||||
return "PATADiskDevice";
|
||||
}
|
||||
|
||||
void PATADiskDevice::start_request(AsyncBlockDeviceRequest& request)
|
||||
{
|
||||
bool use_dma = !m_channel.m_io_group.bus_master_base().is_null() && m_channel.m_dma_enabled.resource();
|
||||
m_channel.start_request(request, use_dma, is_slave());
|
||||
}
|
||||
|
||||
size_t PATADiskDevice::max_addressable_block() const
|
||||
{
|
||||
return m_cylinders * m_heads * m_sectors_per_track;
|
||||
}
|
||||
|
||||
bool PATADiskDevice::is_slave() const
|
||||
{
|
||||
return m_drive_type == DriveType::Slave;
|
||||
}
|
||||
|
||||
}
|
81
Kernel/Storage/PATADiskDevice.h
Normal file
81
Kernel/Storage/PATADiskDevice.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// A Disk Device Connected to a PATA Channel
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Kernel/Interrupts/IRQHandler.h>
|
||||
#include <Kernel/Lock.h>
|
||||
#include <Kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class IDEController;
|
||||
|
||||
class PATADiskDevice final : public StorageDevice {
|
||||
friend class IDEController;
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
// Type of drive this IDEDiskDevice is on the ATA channel.
|
||||
//
|
||||
// Each PATA channel can contain only two devices, which (I think) are
|
||||
// jumper selectable on the drive itself by shorting two pins.
|
||||
enum class DriveType : u8 {
|
||||
Master,
|
||||
Slave
|
||||
};
|
||||
|
||||
public:
|
||||
static NonnullRefPtr<PATADiskDevice> create(const IDEController&, IDEChannel&, DriveType, u8, u8, u8, int major, int minor);
|
||||
virtual ~PATADiskDevice() override;
|
||||
|
||||
// ^StorageDevice
|
||||
virtual Type type() const override { return StorageDevice::Type::IDE; }
|
||||
virtual size_t max_addressable_block() const override;
|
||||
|
||||
// ^BlockDevice
|
||||
virtual void start_request(AsyncBlockDeviceRequest&) override;
|
||||
|
||||
private:
|
||||
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, u8, u8, u8, int major, int minor);
|
||||
|
||||
// ^DiskDevice
|
||||
virtual const char* class_name() const override;
|
||||
|
||||
bool is_slave() const;
|
||||
|
||||
Lock m_lock { "IDEDiskDevice" };
|
||||
u16 m_cylinders { 0 };
|
||||
u16 m_heads { 0 };
|
||||
u16 m_sectors_per_track { 0 };
|
||||
IDEChannel& m_channel;
|
||||
DriveType m_drive_type { DriveType::Master };
|
||||
};
|
||||
|
||||
}
|
64
Kernel/Storage/StorageController.h
Normal file
64
Kernel/Storage/StorageController.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <Kernel/Devices/Device.h>
|
||||
#include <Kernel/IO.h>
|
||||
#include <Kernel/Lock.h>
|
||||
#include <Kernel/PCI/Access.h>
|
||||
#include <Kernel/PCI/DeviceController.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
#include <Kernel/Random.h>
|
||||
#include <Kernel/VM/PhysicalPage.h>
|
||||
#include <Kernel/WaitQueue.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class AsyncBlockDeviceRequest;
|
||||
class StorageDevice;
|
||||
class StorageController : public RefCounted<StorageController>
|
||||
, public PCI::DeviceController {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
protected:
|
||||
explicit StorageController(PCI::Address address)
|
||||
: PCI::DeviceController(address)
|
||||
{
|
||||
}
|
||||
|
||||
virtual RefPtr<StorageDevice> device(u32 index) = 0;
|
||||
virtual void start_request(const StorageDevice&, AsyncBlockDeviceRequest&) = 0;
|
||||
|
||||
protected:
|
||||
virtual bool reset() = 0;
|
||||
virtual bool shutdown() = 0;
|
||||
|
||||
virtual void complete_current_request(AsyncDeviceRequest::RequestResult) = 0;
|
||||
};
|
||||
}
|
212
Kernel/Storage/StorageDevice.cpp
Normal file
212
Kernel/Storage/StorageDevice.cpp
Normal file
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//#define STORAGE_DEVICE_DEBUG
|
||||
|
||||
#include <AK/Memory.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, size_t max_addressable_block)
|
||||
: BlockDevice(major, minor, sector_size)
|
||||
, m_storage_controller(controller)
|
||||
, m_max_addressable_block(max_addressable_block)
|
||||
{
|
||||
}
|
||||
|
||||
const char* StorageDevice::class_name() const
|
||||
{
|
||||
return "StorageDevice";
|
||||
}
|
||||
|
||||
NonnullRefPtr<StorageController> StorageDevice::controller() const
|
||||
{
|
||||
return m_storage_controller;
|
||||
}
|
||||
|
||||
KResultOr<size_t> StorageDevice::read(FileDescription&, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
|
||||
{
|
||||
unsigned index = offset / block_size();
|
||||
u16 whole_blocks = len / block_size();
|
||||
ssize_t remaining = len % block_size();
|
||||
|
||||
unsigned blocks_per_page = PAGE_SIZE / block_size();
|
||||
|
||||
// PATAChannel will chuck a wobbly if we try to read more than PAGE_SIZE
|
||||
// at a time, because it uses a single page for its DMA buffer.
|
||||
if (whole_blocks >= blocks_per_page) {
|
||||
whole_blocks = blocks_per_page;
|
||||
remaining = 0;
|
||||
}
|
||||
|
||||
#ifdef STORAGE_DEVICE_DEBUG
|
||||
klog() << "StorageDevice::read() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
|
||||
#endif
|
||||
|
||||
if (whole_blocks > 0) {
|
||||
auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, whole_blocks, outbuf, whole_blocks * block_size());
|
||||
auto result = read_request->wait();
|
||||
if (result.wait_result().was_interrupted())
|
||||
return KResult(-EINTR);
|
||||
switch (result.request_result()) {
|
||||
case AsyncDeviceRequest::Failure:
|
||||
case AsyncDeviceRequest::Cancelled:
|
||||
return KResult(-EIO);
|
||||
case AsyncDeviceRequest::MemoryFault:
|
||||
return KResult(-EFAULT);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
off_t pos = whole_blocks * block_size();
|
||||
|
||||
if (remaining > 0) {
|
||||
auto data = ByteBuffer::create_uninitialized(block_size());
|
||||
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
|
||||
auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size());
|
||||
auto result = read_request->wait();
|
||||
if (result.wait_result().was_interrupted())
|
||||
return KResult(-EINTR);
|
||||
switch (result.request_result()) {
|
||||
case AsyncDeviceRequest::Failure:
|
||||
return pos;
|
||||
case AsyncDeviceRequest::Cancelled:
|
||||
return KResult(-EIO);
|
||||
case AsyncDeviceRequest::MemoryFault:
|
||||
// This should never happen, we're writing to a kernel buffer!
|
||||
ASSERT_NOT_REACHED();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!outbuf.write(data.data(), pos, remaining))
|
||||
return KResult(-EFAULT);
|
||||
}
|
||||
|
||||
return pos + remaining;
|
||||
}
|
||||
|
||||
bool StorageDevice::can_read(const FileDescription&, size_t offset) const
|
||||
{
|
||||
return offset < (max_addressable_block() * block_size());
|
||||
}
|
||||
|
||||
KResultOr<size_t> StorageDevice::write(FileDescription&, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
|
||||
{
|
||||
unsigned index = offset / block_size();
|
||||
u16 whole_blocks = len / block_size();
|
||||
ssize_t remaining = len % block_size();
|
||||
|
||||
unsigned blocks_per_page = PAGE_SIZE / block_size();
|
||||
|
||||
// PATAChannel will chuck a wobbly if we try to write more than PAGE_SIZE
|
||||
// at a time, because it uses a single page for its DMA buffer.
|
||||
if (whole_blocks >= blocks_per_page) {
|
||||
whole_blocks = blocks_per_page;
|
||||
remaining = 0;
|
||||
}
|
||||
|
||||
#ifdef STORAGE_DEVICE_DEBUG
|
||||
klog() << "StorageDevice::write() index=" << index << " whole_blocks=" << whole_blocks << " remaining=" << remaining;
|
||||
#endif
|
||||
|
||||
if (whole_blocks > 0) {
|
||||
auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, whole_blocks, inbuf, whole_blocks * block_size());
|
||||
auto result = write_request->wait();
|
||||
if (result.wait_result().was_interrupted())
|
||||
return KResult(-EINTR);
|
||||
switch (result.request_result()) {
|
||||
case AsyncDeviceRequest::Failure:
|
||||
case AsyncDeviceRequest::Cancelled:
|
||||
return KResult(-EIO);
|
||||
case AsyncDeviceRequest::MemoryFault:
|
||||
return KResult(-EFAULT);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
off_t pos = whole_blocks * block_size();
|
||||
|
||||
// since we can only write in block_size() increments, if we want to do a
|
||||
// partial write, we have to read the block's content first, modify it,
|
||||
// then write the whole block back to the disk.
|
||||
if (remaining > 0) {
|
||||
auto data = ByteBuffer::create_zeroed(block_size());
|
||||
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
|
||||
|
||||
{
|
||||
auto read_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size());
|
||||
auto result = read_request->wait();
|
||||
if (result.wait_result().was_interrupted())
|
||||
return KResult(-EINTR);
|
||||
switch (result.request_result()) {
|
||||
case AsyncDeviceRequest::Failure:
|
||||
return pos;
|
||||
case AsyncDeviceRequest::Cancelled:
|
||||
return KResult(-EIO);
|
||||
case AsyncDeviceRequest::MemoryFault:
|
||||
// This should never happen, we're writing to a kernel buffer!
|
||||
ASSERT_NOT_REACHED();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!inbuf.read(data.data(), pos, remaining))
|
||||
return KResult(-EFAULT);
|
||||
|
||||
{
|
||||
auto write_request = make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size());
|
||||
auto result = write_request->wait();
|
||||
if (result.wait_result().was_interrupted())
|
||||
return KResult(-EINTR);
|
||||
switch (result.request_result()) {
|
||||
case AsyncDeviceRequest::Failure:
|
||||
return pos;
|
||||
case AsyncDeviceRequest::Cancelled:
|
||||
return KResult(-EIO);
|
||||
case AsyncDeviceRequest::MemoryFault:
|
||||
// This should never happen, we're writing to a kernel buffer!
|
||||
ASSERT_NOT_REACHED();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pos + remaining;
|
||||
}
|
||||
|
||||
bool StorageDevice::can_write(const FileDescription&, size_t offset) const
|
||||
{
|
||||
return offset < (max_addressable_block() * block_size());
|
||||
}
|
||||
|
||||
}
|
66
Kernel/Storage/StorageDevice.h
Normal file
66
Kernel/Storage/StorageDevice.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Kernel/Devices/BlockDevice.h>
|
||||
#include <Kernel/Interrupts/IRQHandler.h>
|
||||
#include <Kernel/Lock.h>
|
||||
#include <Kernel/Storage/StorageController.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class StorageDevice : public BlockDevice {
|
||||
AK_MAKE_ETERNAL
|
||||
public:
|
||||
enum class Type : u8 {
|
||||
IDE,
|
||||
NVMe,
|
||||
};
|
||||
|
||||
public:
|
||||
virtual Type type() const = 0;
|
||||
virtual size_t max_addressable_block() const { return m_max_addressable_block; }
|
||||
|
||||
NonnullRefPtr<StorageController> controller() const;
|
||||
|
||||
// ^BlockDevice
|
||||
virtual KResultOr<size_t> read(FileDescription&, size_t, UserOrKernelBuffer&, size_t) override;
|
||||
virtual bool can_read(const FileDescription&, size_t) const override;
|
||||
virtual KResultOr<size_t> write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t) override;
|
||||
virtual bool can_write(const FileDescription&, size_t) const override;
|
||||
|
||||
protected:
|
||||
StorageDevice(const StorageController&, int, int, size_t, size_t);
|
||||
// ^DiskDevice
|
||||
virtual const char* class_name() const override;
|
||||
|
||||
private:
|
||||
NonnullRefPtr<StorageController> m_storage_controller;
|
||||
size_t m_max_addressable_block;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue