From e34540b77edd44fed125132e625d4e718a448a30 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sat, 1 Feb 2025 14:49:41 -0800 Subject: [PATCH] Move command into its own file. --- rust/sys/denali/src/ahci/command.rs | 112 ++++++++++++++++++ rust/sys/denali/src/ahci/controller.rs | 2 +- rust/sys/denali/src/ahci/mod.rs | 3 +- rust/sys/denali/src/ahci/port_controller.rs | 120 ++------------------ 4 files changed, 122 insertions(+), 115 deletions(-) create mode 100644 rust/sys/denali/src/ahci/command.rs diff --git a/rust/sys/denali/src/ahci/command.rs b/rust/sys/denali/src/ahci/command.rs new file mode 100644 index 0000000..947082e --- /dev/null +++ b/rust/sys/denali/src/ahci/command.rs @@ -0,0 +1,112 @@ +use core::{ + future::Future, + ops::Deref, + ops::DerefMut, + pin::Pin, + task::{Context, Poll, Waker}, +}; + +use alloc::boxed::Box; +use alloc::sync::Arc; +use mammoth::{cap::Capability, sync::Mutex, syscall, zion::ZError}; + +use super::ahci_command::{HostToDeviceRegisterFis, SataCommand}; + +pub enum CommandStatus { + Empty, + NotSent, + Pending(Waker), + Complete, +} + +pub struct CommandFuture { + status: Arc>, + trigger: Box, +} + +impl CommandFuture { + pub fn new(status: Arc>, trigger: Box) -> Self { + Self { status, trigger } + } +} + +impl Drop for CommandFuture { + fn drop(&mut self) { + *self.status.lock() = CommandStatus::Empty; + } +} + +impl Future for CommandFuture { + type Output = (); + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let s = self.deref_mut(); + let mut status = s.status.lock(); + match status.deref() { + CommandStatus::NotSent => { + *status = CommandStatus::Pending(cx.waker().clone()); + (s.trigger)(); + Poll::Pending + } + CommandStatus::Pending(_) => Poll::Pending, + CommandStatus::Complete => Poll::Ready(()), + CommandStatus::Empty => panic!("Polling empty command slot"), + } + } +} + +pub struct Command { + pub command: SataCommand, + pub lba: u64, + pub sector_cnt: u16, + pub paddr: u64, + + memory_region: Option, +} + +impl Command { + pub fn identify() -> Result { + let (memory_region, paddr) = syscall::memory_object_contiguous_physical(512)?; + + Ok(Self { + command: SataCommand::IdentifyDevice, + lba: 0, + sector_cnt: 1, + paddr, + memory_region: Some(memory_region), + }) + } + + pub fn read(lba: u64, lba_count: u16) -> Result { + let (memory_region, paddr) = + syscall::memory_object_contiguous_physical(512 * (lba_count as u64))?; + + Ok(Self { + command: SataCommand::DmaReadExt, + lba, + sector_cnt: lba_count, + paddr, + memory_region: Some(memory_region), + }) + } + + pub fn read_manual(lba: u64, lba_count: u16, paddr: u64) -> Self { + Self { + command: SataCommand::DmaReadExt, + lba, + sector_cnt: lba_count, + paddr: paddr, + memory_region: None, + } + } + + pub fn release_mem_cap(&mut self) -> u64 { + self.memory_region.take().unwrap().release() + } +} + +impl From<&Command> for HostToDeviceRegisterFis { + fn from(val: &Command) -> Self { + HostToDeviceRegisterFis::new_command(val.command, val.lba, val.sector_cnt) + } +} diff --git a/rust/sys/denali/src/ahci/controller.rs b/rust/sys/denali/src/ahci/controller.rs index d0656a6..059f3a7 100644 --- a/rust/sys/denali/src/ahci/controller.rs +++ b/rust/sys/denali/src/ahci/controller.rs @@ -9,7 +9,7 @@ use mammoth::{ use crate::ahci::{ port::{AhciDeviceDetection, AhciInterfacePowerManagement}, - port_controller::Command, + Command, }; use super::{hba::AhciHba, port::AhciPortHba, port_controller::PortController}; diff --git a/rust/sys/denali/src/ahci/mod.rs b/rust/sys/denali/src/ahci/mod.rs index 8200d8e..f98bcf2 100644 --- a/rust/sys/denali/src/ahci/mod.rs +++ b/rust/sys/denali/src/ahci/mod.rs @@ -1,10 +1,11 @@ mod ahci_command; +mod command; mod controller; mod hba; mod port; mod port_controller; +pub use command::Command; pub use controller::identify_ports; pub use controller::spawn_irq_thread; pub use controller::AhciController; -pub use port_controller::Command; diff --git a/rust/sys/denali/src/ahci/port_controller.rs b/rust/sys/denali/src/ahci/port_controller.rs index 5b42543..556e8f7 100644 --- a/rust/sys/denali/src/ahci/port_controller.rs +++ b/rust/sys/denali/src/ahci/port_controller.rs @@ -1,124 +1,18 @@ -use core::{ - array, - future::Future, - ops::Deref, - ops::DerefMut, - pin::Pin, - task::{Context, Poll, Waker}, -}; +use core::array; +use core::ops::Deref; -use alloc::boxed::Box; -use alloc::sync::Arc; -use mammoth::{cap::Capability, mem, sync::Mutex, syscall, zion::ZError}; +use alloc::{boxed::Box, sync::Arc}; +use mammoth::{mem, sync::Mutex, zion::ZError}; use crate::ahci::port::AhciPortInterruptStatus; use super::{ - ahci_command::{ - CommandList, CommandTable, FisType, HostToDeviceRegisterFis, ReceivedFis, SataCommand, - }, + ahci_command::{CommandList, CommandTable, FisType, HostToDeviceRegisterFis, ReceivedFis}, + command::{CommandFuture, CommandStatus}, port::AhciPortHba, + Command, }; -enum CommandStatus { - Empty, - NotSent, - Pending(Waker), - Complete, -} - -pub struct CommandFuture { - status: Arc>, - trigger: Box, -} - -impl CommandFuture { - fn new(status: Arc>, trigger: Box) -> Self { - Self { status, trigger } - } -} - -impl Drop for CommandFuture { - fn drop(&mut self) { - *self.status.lock() = CommandStatus::Empty; - } -} - -impl Future for CommandFuture { - type Output = (); - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let s = self.deref_mut(); - let mut status = s.status.lock(); - match status.deref() { - CommandStatus::NotSent => { - *status = CommandStatus::Pending(cx.waker().clone()); - (s.trigger)(); - Poll::Pending - } - CommandStatus::Pending(_) => Poll::Pending, - CommandStatus::Complete => Poll::Ready(()), - CommandStatus::Empty => panic!("Polling empty command slot"), - } - } -} - -pub struct Command { - command: SataCommand, - lba: u64, - sector_cnt: u16, - paddr: u64, - - memory_region: Option, -} - -impl Command { - pub fn identify() -> Result { - let (memory_region, paddr) = syscall::memory_object_contiguous_physical(512)?; - - Ok(Self { - command: SataCommand::IdentifyDevice, - lba: 0, - sector_cnt: 1, - paddr, - memory_region: Some(memory_region), - }) - } - - pub fn read(lba: u64, lba_count: u16) -> Result { - let (memory_region, paddr) = - syscall::memory_object_contiguous_physical(512 * (lba_count as u64))?; - - Ok(Self { - command: SataCommand::DmaReadExt, - lba, - sector_cnt: lba_count, - paddr, - memory_region: Some(memory_region), - }) - } - - pub fn read_manual(lba: u64, lba_count: u16, paddr: u64) -> Self { - Self { - command: SataCommand::DmaReadExt, - lba, - sector_cnt: lba_count, - paddr: paddr, - memory_region: None, - } - } - - pub fn release_mem_cap(&mut self) -> u64 { - self.memory_region.take().unwrap().release() - } -} - -impl From<&Command> for HostToDeviceRegisterFis { - fn from(val: &Command) -> Self { - HostToDeviceRegisterFis::new_command(val.command, val.lba, val.sector_cnt) - } -} - struct CommandStructures { command_list: &'static mut CommandList, received_fis: &'static mut ReceivedFis,