aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / scsi / pass_thru.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Extended SCSI Pass Thru protocols. |
| 4 | |
| 5 | use super::{ScsiRequest, ScsiResponse}; |
| 6 | use crate::StatusExt; |
| 7 | use crate::mem::{AlignedBuffer, PoolAllocation}; |
| 8 | use crate::proto::device_path::PoolDevicePathNode; |
| 9 | use crate::proto::unsafe_protocol; |
| 10 | use core::alloc::LayoutError; |
| 11 | use core::cell::UnsafeCell; |
| 12 | use core::ptr::{self, NonNull}; |
| 13 | use uefi_raw::Status; |
| 14 | use uefi_raw::protocol::device_path::DevicePathProtocol; |
| 15 | use uefi_raw::protocol::scsi::{ |
| 16 | ExtScsiPassThruMode, ExtScsiPassThruProtocol, SCSI_TARGET_MAX_BYTES, |
| 17 | }; |
| 18 | |
| 19 | /// Structure representing a SCSI target address. |
| 20 | pub type ScsiTarget = [u8; SCSI_TARGET_MAX_BYTES]; |
| 21 | |
| 22 | /// Structure representing a fully-qualified device address, consisting of SCSI target and LUN. |
| 23 | #[derive(Clone, Debug)] |
| 24 | pub struct ScsiTargetLun(ScsiTarget, u64); |
| 25 | impl Default for ScsiTargetLun { |
| 26 | fn default() -> Self { |
| 27 | Self([0xFF; SCSI_TARGET_MAX_BYTES], 0) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Enables interaction with SCSI devices using the Extended SCSI Pass Thru protocol. |
| 32 | /// |
| 33 | /// This protocol allows communication with SCSI devices connected to the system, |
| 34 | /// providing methods to send commands, reset devices, and enumerate SCSI targets. |
| 35 | /// |
| 36 | /// This API offers a safe and convenient, yet still low-level interface to SCSI devices. |
| 37 | /// It is designed as a foundational layer, leaving higher-level abstractions responsible for implementing |
| 38 | /// richer storage semantics, device-specific commands, and advanced use cases. |
| 39 | /// |
| 40 | /// # UEFI Specification |
| 41 | /// Provides services that allow SCSI Pass Thru commands to be sent to SCSI devices attached to a SCSI channel. It also |
| 42 | /// allows packet-based commands (ATAPI cmds) to be sent to ATAPI devices attached to a ATA controller. |
| 43 | #[derive(Debug)] |
| 44 | #[repr(transparent)] |
| 45 | #[unsafe_protocol(ExtScsiPassThruProtocol::GUID)] |
| 46 | pub struct ExtScsiPassThru(UnsafeCell<ExtScsiPassThruProtocol>); |
| 47 | |
| 48 | impl ExtScsiPassThru { |
| 49 | /// Retrieves the mode structure for the Extended SCSI Pass Thru protocol. |
| 50 | /// |
| 51 | /// # Returns |
| 52 | /// The [`ExtScsiPassThruMode`] structure containing configuration details of the protocol. |
| 53 | #[must_use] |
| 54 | pub fn mode(&self) -> ExtScsiPassThruMode { |
| 55 | // SAFETY: The memory is valid. |
| 56 | let mut mode = unsafe { (*(*self.0.get()).passthru_mode).clone() }; |
| 57 | mode.io_align = mode.io_align.max(1); // 0 and 1 is the same, says UEFI spec |
| 58 | mode |
| 59 | } |
| 60 | |
| 61 | /// Retrieves the I/O buffer alignment required by this SCSI channel. |
| 62 | /// |
| 63 | /// # Returns |
| 64 | /// - A `u32` value representing the required I/O alignment. |
| 65 | #[must_use] |
| 66 | pub fn io_align(&self) -> u32 { |
| 67 | self.mode().io_align |
| 68 | } |
| 69 | |
| 70 | /// Allocates an I/O buffer with the necessary alignment for this SCSI channel. |
| 71 | /// |
| 72 | /// You can alternatively do this yourself using the [`AlignedBuffer`] helper directly. |
| 73 | /// The Scsi api will validate that your buffers have the correct alignment and crash |
| 74 | /// if they don't. |
| 75 | /// |
| 76 | /// # Arguments |
| 77 | /// - `len`: The size (in bytes) of the buffer to allocate. |
| 78 | /// |
| 79 | /// # Returns |
| 80 | /// [`AlignedBuffer`] containing the allocated memory. |
| 81 | /// |
| 82 | /// # Errors |
| 83 | /// This method can fail due to alignment or memory allocation issues. |
| 84 | pub fn alloc_io_buffer(&self, len: usize) -> Result<AlignedBuffer, LayoutError> { |
| 85 | AlignedBuffer::from_size_align(len, self.io_align() as usize) |
| 86 | } |
| 87 | |
| 88 | /// Iterate over all potential SCSI devices on this channel. |
| 89 | /// |
| 90 | /// # Warnings |
| 91 | /// Depending on the UEFI implementation, this does not only return all actually available devices. |
| 92 | /// Most implementations instead return a list of all possible fully-qualified device addresses. |
| 93 | /// You have to probe for availability yourself, using [`ScsiDevice::execute_command`]. |
| 94 | /// |
| 95 | /// # Returns |
| 96 | /// [`ScsiTargetLunIterator`] to iterate through connected SCSI devices. |
| 97 | #[must_use] |
| 98 | pub fn iter_devices(&self) -> ScsiTargetLunIterator<'_> { |
| 99 | ScsiTargetLunIterator { |
| 100 | proto: &self.0, |
| 101 | prev: ScsiTargetLun::default(), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Resets the SCSI channel associated with the protocol. |
| 106 | /// |
| 107 | /// The EFI_EXT_SCSI_PASS_THRU_PROTOCOL.ResetChannel() function resets a SCSI channel. |
| 108 | /// This operation resets all the SCSI devices connected to the SCSI channel. |
| 109 | /// |
| 110 | /// # Returns |
| 111 | /// [`Result`] indicating the success or failure of the operation. |
| 112 | /// |
| 113 | /// # Errors |
| 114 | /// - [`Status::UNSUPPORTED`] The SCSI channel does not support a channel reset operation. |
| 115 | /// - [`Status::DEVICE_ERROR`] A device error occurred while attempting to reset the SCSI channel. |
| 116 | /// - [`Status::TIMEOUT`] A timeout occurred while attempting to reset the SCSI channel. |
| 117 | pub fn reset_channel(&mut self) -> crate::Result<()> { |
| 118 | // SAFETY: The memory is valid. |
| 119 | unsafe { ((*self.0.get()).reset_channel)(self.0.get()).to_result() } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Structure representing a potential ScsiDevice. |
| 124 | /// |
| 125 | /// In the UEFI Specification, this corresponds to a (SCSI target, LUN) tuple. |
| 126 | /// |
| 127 | /// # Warnings |
| 128 | /// This does not actually have to correspond to an actual device! |
| 129 | /// You have to probe for availability before doing anything meaningful with it. |
| 130 | #[derive(Clone, Debug)] |
| 131 | pub struct ScsiDevice<'a> { |
| 132 | proto: &'a UnsafeCell<ExtScsiPassThruProtocol>, |
| 133 | target_lun: ScsiTargetLun, |
| 134 | } |
| 135 | impl ScsiDevice<'_> { |
| 136 | /// Returns the SCSI target address of the potential device. |
| 137 | #[must_use] |
| 138 | pub const fn target(&self) -> &ScsiTarget { |
| 139 | &self.target_lun.0 |
| 140 | } |
| 141 | |
| 142 | /// Returns the logical unit number (LUN) of the potential device. |
| 143 | #[must_use] |
| 144 | pub const fn lun(&self) -> u64 { |
| 145 | self.target_lun.1 |
| 146 | } |
| 147 | |
| 148 | /// Get the final device path node for this device. |
| 149 | /// |
| 150 | /// For a full [`crate::proto::device_path::DevicePath`] pointing to this device, this needs to be appended to |
| 151 | /// the controller's device path. |
| 152 | pub fn path_node(&self) -> crate::Result<PoolDevicePathNode> { |
| 153 | // SAFETY: The memory is valid. |
| 154 | unsafe { |
| 155 | let mut path_ptr: *mut DevicePathProtocol = ptr::null_mut(); |
| 156 | ((*self.proto.get()).build_device_path)( |
| 157 | self.proto.get(), |
| 158 | self.target().as_ptr(), |
| 159 | self.lun(), |
| 160 | &mut path_ptr, |
| 161 | ) |
| 162 | .to_result()?; |
| 163 | NonNull::new(path_ptr) |
| 164 | .map(|p| PoolDevicePathNode(PoolAllocation::new(p.cast()))) |
| 165 | .ok_or_else(|| Status::OUT_OF_RESOURCES.into()) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /// Resets the potential SCSI device represented by this instance. |
| 170 | /// |
| 171 | /// The `EFI_EXT_SCSI_PASS_THRU_PROTOCOL.ResetTargetLun()` function resets the SCSI logical unit |
| 172 | /// specified by `Target` and `Lun`. This allows for recovering a device that may be in an error state |
| 173 | /// or requires reinitialization. The function behavior is dependent on the SCSI channel's capability |
| 174 | /// to perform target resets. |
| 175 | /// |
| 176 | /// # Returns |
| 177 | /// [`Result`] indicating the success or failure of the operation. |
| 178 | /// |
| 179 | /// # Errors |
| 180 | /// - [`Status::UNSUPPORTED`] The SCSI channel does not support a target reset operation. |
| 181 | /// - [`Status::INVALID_PARAMETER`] The `Target` or `Lun` values are invalid. |
| 182 | /// - [`Status::DEVICE_ERROR`] A device error occurred while attempting to reset the SCSI device |
| 183 | /// specified by `Target` and `Lun`. |
| 184 | /// - [`Status::TIMEOUT`] A timeout occurred while attempting to reset the SCSI device specified |
| 185 | /// by `Target` and `Lun`. |
| 186 | pub fn reset(&mut self) -> crate::Result<()> { |
| 187 | // SAFETY: The memory is valid. |
| 188 | unsafe { |
| 189 | ((*self.proto.get()).reset_target_lun)( |
| 190 | self.proto.get(), |
| 191 | self.target_lun.0.as_ptr(), |
| 192 | self.lun(), |
| 193 | ) |
| 194 | .to_result() |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// Sends a SCSI command to the potential target device and retrieves the response. |
| 199 | /// |
| 200 | /// This method sends a SCSI Request Packet to a SCSI device attached to the SCSI channel. |
| 201 | /// It supports both blocking and nonblocking I/O. Blocking I/O is mandatory, while |
| 202 | /// nonblocking I/O is optional and dependent on the driver's implementation. |
| 203 | /// |
| 204 | /// # Arguments |
| 205 | /// - `scsi_req`: The [`ScsiRequest`] containing the command and data to send to the device. |
| 206 | /// |
| 207 | /// # Returns |
| 208 | /// [`ScsiResponse`] containing the results of the operation, such as data and status. |
| 209 | /// |
| 210 | /// # Errors |
| 211 | /// - [`Status::BAD_BUFFER_SIZE`] The SCSI Request Packet was not executed because the data |
| 212 | /// buffer size exceeded the allowed transfer size for a single command. The number of bytes |
| 213 | /// that could be transferred is returned in `InTransferLength` or `OutTransferLength`. |
| 214 | /// - [`Status::NOT_READY`] The SCSI Request Packet could not be sent because too many packets |
| 215 | /// are already queued. The caller may retry later. |
| 216 | /// - [`Status::DEVICE_ERROR`] A device error occurred while attempting to send the SCSI Request Packet. |
| 217 | /// Additional status information is available in `HostAdapterStatus`, `TargetStatus`, `SenseDataLength`, |
| 218 | /// and `SenseData`. |
| 219 | /// - [`Status::INVALID_PARAMETER`] The `Target`, `Lun`, or the contents of `ScsiRequestPacket` are invalid. |
| 220 | /// The SCSI Request Packet was not sent, and no additional status information is available. |
| 221 | /// - [`Status::UNSUPPORTED`] The command described by the SCSI Request Packet is not supported by the |
| 222 | /// host adapter, including unsupported bi-directional SCSI commands. The SCSI Request Packet was not |
| 223 | /// sent, and no additional status information is available. |
| 224 | /// - [`Status::TIMEOUT`] A timeout occurred while executing the SCSI Request Packet. Additional status |
| 225 | /// information is available in `HostAdapterStatus`, `TargetStatus`, `SenseDataLength`, and `SenseData`. |
| 226 | pub fn execute_command<'req>( |
| 227 | &mut self, |
| 228 | mut scsi_req: ScsiRequest<'req>, |
| 229 | ) -> crate::Result<ScsiResponse<'req>> { |
| 230 | // SAFETY: The memory is valid. |
| 231 | unsafe { |
| 232 | ((*self.proto.get()).pass_thru)( |
| 233 | self.proto.get(), |
| 234 | self.target_lun.0.as_ptr(), |
| 235 | self.target_lun.1, |
| 236 | &mut scsi_req.packet, |
| 237 | ptr::null_mut(), |
| 238 | ) |
| 239 | .to_result_with_val(|| ScsiResponse(scsi_req)) |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /// An iterator over SCSI devices available on the channel. |
| 245 | #[derive(Debug)] |
| 246 | pub struct ScsiTargetLunIterator<'a> { |
| 247 | proto: &'a UnsafeCell<ExtScsiPassThruProtocol>, |
| 248 | prev: ScsiTargetLun, |
| 249 | } |
| 250 | impl<'a> Iterator for ScsiTargetLunIterator<'a> { |
| 251 | type Item = ScsiDevice<'a>; |
| 252 | |
| 253 | fn next(&mut self) -> Option<Self::Item> { |
| 254 | // get_next_target_lun() takes the target as a double ptr, meaning that the spec allows |
| 255 | // the implementation to return us a new buffer (most impls don't actually seem to do though) |
| 256 | let mut target: *mut u8 = self.prev.0.as_mut_ptr(); |
| 257 | // SAFETY: The memory is valid. |
| 258 | let result = unsafe { |
| 259 | ((*self.proto.get()).get_next_target_lun)( |
| 260 | self.proto.get(), |
| 261 | &mut target, |
| 262 | &mut self.prev.1, |
| 263 | ) |
| 264 | }; |
| 265 | if target != self.prev.0.as_mut_ptr() { |
| 266 | // impl has returned us a new pointer instead of writing in our buffer, copy back |
| 267 | // SAFETY: The memory is valid. |
| 268 | unsafe { |
| 269 | target.copy_to(self.prev.0.as_mut_ptr(), SCSI_TARGET_MAX_BYTES); |
| 270 | } |
| 271 | } |
| 272 | let scsi_device = ScsiDevice { |
| 273 | proto: self.proto, |
| 274 | target_lun: self.prev.clone(), |
| 275 | }; |
| 276 | match result { |
| 277 | Status::SUCCESS => Some(scsi_device), |
| 278 | Status::NOT_FOUND => None, |
| 279 | _ => panic!("Must not happen according to spec!"), |
| 280 | } |
| 281 | } |
| 282 | } |