|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +//! SCSI Bus specific protocols. |
| 4 | +
|
| 5 | +use crate::mem::{AlignedBuffer, AlignmentError}; |
| 6 | +use core::alloc::LayoutError; |
| 7 | +use core::marker::PhantomData; |
| 8 | +use core::ptr; |
| 9 | +use core::time::Duration; |
| 10 | +use uefi_raw::protocol::scsi::{ |
| 11 | + ScsiIoDataDirection, ScsiIoHostAdapterStatus, ScsiIoScsiRequestPacket, ScsiIoTargetStatus, |
| 12 | +}; |
| 13 | + |
| 14 | +pub mod pass_thru; |
| 15 | + |
| 16 | +/// Represents the data direction for a SCSI request. |
| 17 | +/// |
| 18 | +/// Used to specify whether the request involves reading, writing, or bidirectional data transfer. |
| 19 | +pub type ScsiRequestDirection = uefi_raw::protocol::scsi::ScsiIoDataDirection; |
| 20 | + |
| 21 | +/// Represents a SCSI request packet. |
| 22 | +/// |
| 23 | +/// This structure encapsulates the necessary data for sending a command to a SCSI device. |
| 24 | +#[derive(Debug)] |
| 25 | +pub struct ScsiRequest<'a> { |
| 26 | + packet: ScsiIoScsiRequestPacket, |
| 27 | + io_align: u32, |
| 28 | + in_data_buffer: Option<AlignedBuffer>, |
| 29 | + out_data_buffer: Option<AlignedBuffer>, |
| 30 | + sense_data_buffer: Option<AlignedBuffer>, |
| 31 | + cdb_buffer: Option<AlignedBuffer>, |
| 32 | + _phantom: PhantomData<&'a u8>, |
| 33 | +} |
| 34 | + |
| 35 | +/// A builder for constructing [`ScsiRequest`] instances. |
| 36 | +/// |
| 37 | +/// Provides a safe and ergonomic interface for configuring SCSI request packets, including timeout, |
| 38 | +/// data buffers, and command descriptor blocks. |
| 39 | +#[derive(Debug)] |
| 40 | +pub struct ScsiRequestBuilder<'a> { |
| 41 | + req: ScsiRequest<'a>, |
| 42 | +} |
| 43 | +impl ScsiRequestBuilder<'_> { |
| 44 | + /// Creates a new instance with the specified data direction and alignment. |
| 45 | + /// |
| 46 | + /// # Parameters |
| 47 | + /// - `direction`: Specifies the direction of data transfer (READ, WRITE, or BIDIRECTIONAL). |
| 48 | + /// - `io_align`: Specifies the required alignment for data buffers. (SCSI Controller specific!) |
| 49 | + #[must_use] |
| 50 | + pub fn new(direction: ScsiRequestDirection, io_align: u32) -> Self { |
| 51 | + Self { |
| 52 | + req: ScsiRequest { |
| 53 | + in_data_buffer: None, |
| 54 | + out_data_buffer: None, |
| 55 | + sense_data_buffer: None, |
| 56 | + cdb_buffer: None, |
| 57 | + packet: ScsiIoScsiRequestPacket { |
| 58 | + timeout: 0, |
| 59 | + in_data_buffer: ptr::null_mut(), |
| 60 | + out_data_buffer: ptr::null_mut(), |
| 61 | + sense_data: ptr::null_mut(), |
| 62 | + cdb: ptr::null_mut(), |
| 63 | + in_transfer_length: 0, |
| 64 | + out_transfer_length: 0, |
| 65 | + cdb_length: 0, |
| 66 | + data_direction: direction, |
| 67 | + host_adapter_status: ScsiIoHostAdapterStatus::default(), |
| 68 | + target_status: ScsiIoTargetStatus::default(), |
| 69 | + sense_data_length: 0, |
| 70 | + }, |
| 71 | + io_align, |
| 72 | + _phantom: Default::default(), |
| 73 | + }, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Starts a new builder preconfigured for READ operations. |
| 78 | + /// |
| 79 | + /// Some examples of SCSI read commands are: |
| 80 | + /// - INQUIRY |
| 81 | + /// - READ |
| 82 | + /// - MODE_SENSE |
| 83 | + /// |
| 84 | + /// # Parameters |
| 85 | + /// - `io_align`: Specifies the required alignment for data buffers. |
| 86 | + #[must_use] |
| 87 | + pub fn read(io_align: u32) -> Self { |
| 88 | + Self::new(ScsiIoDataDirection::READ, io_align) |
| 89 | + } |
| 90 | + |
| 91 | + /// Starts a new builder preconfigured for WRITE operations. |
| 92 | + /// |
| 93 | + /// Some examples of SCSI write commands are: |
| 94 | + /// - WRITE |
| 95 | + /// - MODE_SELECT |
| 96 | + /// |
| 97 | + /// # Parameters |
| 98 | + /// - `io_align`: Specifies the required alignment for data buffers. |
| 99 | + #[must_use] |
| 100 | + pub fn write(io_align: u32) -> Self { |
| 101 | + Self::new(ScsiIoDataDirection::WRITE, io_align) |
| 102 | + } |
| 103 | + |
| 104 | + /// Starts a new builder preconfigured for BIDIRECTIONAL operations. |
| 105 | + /// |
| 106 | + /// Some examples of SCSI bidirectional commands are: |
| 107 | + /// - SEND DIAGNOSTIC |
| 108 | + /// |
| 109 | + /// # Parameters |
| 110 | + /// - `io_align`: Specifies the required alignment for data buffers. |
| 111 | + #[must_use] |
| 112 | + pub fn bidirectional(io_align: u32) -> Self { |
| 113 | + Self::new(ScsiIoDataDirection::BIDIRECTIONAL, io_align) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<'a> ScsiRequestBuilder<'a> { |
| 118 | + /// Sets a timeout for the SCSI request. |
| 119 | + /// |
| 120 | + /// # Parameters |
| 121 | + /// - `timeout`: A [`Duration`] representing the maximum time allowed for the request. |
| 122 | + /// The value is converted to 100-nanosecond units. |
| 123 | + /// |
| 124 | + /// # Description |
| 125 | + /// By default (without calling this method, or by calling with [`Duration::ZERO`]), |
| 126 | + /// SCSI requests have no timeout. |
| 127 | + /// Setting a timeout here will cause SCSI commands to potentially fail with [`crate::Status::TIMEOUT`]. |
| 128 | + #[must_use] |
| 129 | + pub const fn with_timeout(mut self, timeout: Duration) -> Self { |
| 130 | + self.req.packet.timeout = (timeout.as_nanos() / 100) as u64; |
| 131 | + self |
| 132 | + } |
| 133 | + |
| 134 | + // # IN BUFFER |
| 135 | + // ######################################################################################## |
| 136 | + |
| 137 | + /// Uses a user-supplied buffer for reading data from the device. |
| 138 | + /// |
| 139 | + /// # Parameters |
| 140 | + /// - `bfr`: A mutable reference to an [`AlignedBuffer`] that will be used to store data read from the device. |
| 141 | + /// |
| 142 | + /// # Returns |
| 143 | + /// `Result<Self, AlignmentError>` indicating success or an alignment issue with the provided buffer. |
| 144 | + /// |
| 145 | + /// # Description |
| 146 | + /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to |
| 147 | + /// the `in_data_buffer` of the underlying `ScsiRequest`. |
| 148 | + pub fn use_read_buffer(mut self, bfr: &'a mut AlignedBuffer) -> Result<Self, AlignmentError> { |
| 149 | + // check alignment of externally supplied buffer |
| 150 | + bfr.check_alignment(self.req.io_align as usize)?; |
| 151 | + self.req.in_data_buffer = None; |
| 152 | + self.req.packet.in_data_buffer = bfr.ptr_mut().cast(); |
| 153 | + self.req.packet.in_transfer_length = bfr.size() as u32; |
| 154 | + Ok(self) |
| 155 | + } |
| 156 | + |
| 157 | + /// Adds a newly allocated read buffer to the built SCSI request. |
| 158 | + /// |
| 159 | + /// # Parameters |
| 160 | + /// - `len`: The size of the buffer (in bytes) to allocate for receiving data. |
| 161 | + /// |
| 162 | + /// # Returns |
| 163 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 164 | + pub fn with_read_buffer(mut self, len: usize) -> Result<Self, LayoutError> { |
| 165 | + let mut bfr = AlignedBuffer::from_size_align(len, self.req.io_align as usize)?; |
| 166 | + self.req.packet.in_data_buffer = bfr.ptr_mut().cast(); |
| 167 | + self.req.packet.in_transfer_length = bfr.size() as u32; |
| 168 | + self.req.in_data_buffer = Some(bfr); |
| 169 | + Ok(self) |
| 170 | + } |
| 171 | + |
| 172 | + // # SENSE BUFFER |
| 173 | + // ######################################################################################## |
| 174 | + |
| 175 | + /// Adds a newly allocated sense buffer to the built SCSI request. |
| 176 | + /// |
| 177 | + /// # Parameters |
| 178 | + /// - `len`: The size of the buffer (in bytes) to allocate for receiving sense data. |
| 179 | + /// |
| 180 | + /// # Returns |
| 181 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 182 | + pub fn with_sense_buffer(mut self, len: u8) -> Result<Self, LayoutError> { |
| 183 | + let mut bfr = AlignedBuffer::from_size_align(len as usize, self.req.io_align as usize)?; |
| 184 | + self.req.packet.sense_data = bfr.ptr_mut().cast(); |
| 185 | + self.req.packet.sense_data_length = len; |
| 186 | + self.req.sense_data_buffer = Some(bfr); |
| 187 | + Ok(self) |
| 188 | + } |
| 189 | + |
| 190 | + // # WRITE BUFFER |
| 191 | + // ######################################################################################## |
| 192 | + |
| 193 | + /// Uses a user-supplied buffer for writing data to the device. |
| 194 | + /// |
| 195 | + /// # Parameters |
| 196 | + /// - `bfr`: A mutable reference to an [`AlignedBuffer`] containing the data to be written to the device. |
| 197 | + /// |
| 198 | + /// # Returns |
| 199 | + /// `Result<Self, AlignmentError>` indicating success or an alignment issue with the provided buffer. |
| 200 | + /// |
| 201 | + /// # Description |
| 202 | + /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to |
| 203 | + /// the `out_data_buffer` of the underlying `ScsiRequest`. |
| 204 | + pub fn use_write_buffer(mut self, bfr: &'a mut AlignedBuffer) -> Result<Self, AlignmentError> { |
| 205 | + // check alignment of externally supplied buffer |
| 206 | + bfr.check_alignment(self.req.io_align as usize)?; |
| 207 | + self.req.out_data_buffer = None; |
| 208 | + self.req.packet.out_data_buffer = bfr.ptr_mut().cast(); |
| 209 | + self.req.packet.out_transfer_length = bfr.size() as u32; |
| 210 | + Ok(self) |
| 211 | + } |
| 212 | + |
| 213 | + /// Adds a newly allocated write buffer to the built SCSI request that is filled from the |
| 214 | + /// given data buffer. (Done for memory alignment and lifetime purposes) |
| 215 | + /// |
| 216 | + /// # Parameters |
| 217 | + /// - `data`: A slice of bytes representing the data to be written. |
| 218 | + /// |
| 219 | + /// # Returns |
| 220 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 221 | + pub fn with_write_data(mut self, data: &[u8]) -> Result<Self, LayoutError> { |
| 222 | + let mut bfr = AlignedBuffer::from_size_align(data.len(), self.req.io_align as usize)?; |
| 223 | + bfr.copy_from_slice(data); |
| 224 | + self.req.packet.out_data_buffer = bfr.ptr_mut().cast(); |
| 225 | + self.req.packet.out_transfer_length = bfr.size() as u32; |
| 226 | + self.req.out_data_buffer = Some(bfr); |
| 227 | + Ok(self) |
| 228 | + } |
| 229 | + |
| 230 | + // # COMMAND BUFFER |
| 231 | + // ######################################################################################## |
| 232 | + |
| 233 | + /// Uses a user-supplied Command Data Block (CDB) buffer. |
| 234 | + /// |
| 235 | + /// # Parameters |
| 236 | + /// - `data`: A mutable reference to an [`AlignedBuffer`] containing the CDB to be sent to the device. |
| 237 | + /// |
| 238 | + /// # Returns |
| 239 | + /// `Result<Self, AlignmentError>` indicating success or an alignment issue with the provided buffer. |
| 240 | + /// |
| 241 | + /// # Notes |
| 242 | + /// The maximum length of a CDB is 255 bytes. |
| 243 | + pub fn use_command_buffer( |
| 244 | + mut self, |
| 245 | + data: &'a mut AlignedBuffer, |
| 246 | + ) -> Result<Self, AlignmentError> { |
| 247 | + assert!(data.size() <= 255); |
| 248 | + // check alignment of externally supplied buffer |
| 249 | + data.check_alignment(self.req.io_align as usize)?; |
| 250 | + self.req.cdb_buffer = None; |
| 251 | + self.req.packet.cdb = data.ptr_mut().cast(); |
| 252 | + self.req.packet.cdb_length = data.size() as u8; |
| 253 | + Ok(self) |
| 254 | + } |
| 255 | + |
| 256 | + /// Adds a newly allocated Command Data Block (CDB) buffer to the built SCSI request that is filled from the |
| 257 | + /// given data buffer. (Done for memory alignment and lifetime purposes) |
| 258 | + /// |
| 259 | + /// # Parameters |
| 260 | + /// - `data`: A slice of bytes representing the command to be sent. |
| 261 | + /// |
| 262 | + /// # Returns |
| 263 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 264 | + /// |
| 265 | + /// # Notes |
| 266 | + /// The maximum length of a CDB is 255 bytes. |
| 267 | + pub fn with_command_data(mut self, data: &[u8]) -> Result<Self, LayoutError> { |
| 268 | + assert!(data.len() <= 255); |
| 269 | + let mut bfr = AlignedBuffer::from_size_align(data.len(), self.req.io_align as usize)?; |
| 270 | + bfr.copy_from_slice(data); |
| 271 | + self.req.packet.cdb = bfr.ptr_mut().cast(); |
| 272 | + self.req.packet.cdb_length = bfr.size() as u8; |
| 273 | + self.req.cdb_buffer = Some(bfr); |
| 274 | + Ok(self) |
| 275 | + } |
| 276 | + |
| 277 | + /// Build the final `ScsiRequest`. |
| 278 | + /// |
| 279 | + /// # Returns |
| 280 | + /// A fully-configured [`ScsiRequest`] ready for execution. |
| 281 | + #[must_use] |
| 282 | + pub fn build(self) -> ScsiRequest<'a> { |
| 283 | + self.req |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +/// Represents the response of a SCSI request. |
| 288 | +/// |
| 289 | +/// This struct encapsulates the results of a SCSI operation, including data buffers |
| 290 | +/// for read and sense data, as well as status codes returned by the host adapter and target device. |
| 291 | +#[derive(Debug)] |
| 292 | +#[repr(transparent)] |
| 293 | +pub struct ScsiResponse<'a>(ScsiRequest<'a>); |
| 294 | +impl<'a> ScsiResponse<'a> { |
| 295 | + /// Retrieves the buffer containing data read from the device (if any). |
| 296 | + /// |
| 297 | + /// # Returns |
| 298 | + /// `Option<&[u8]>`: A slice of the data read from the device, or `None` if no read buffer was assigned. |
| 299 | + /// |
| 300 | + /// # Safety |
| 301 | + /// - If the buffer pointer is `NULL`, the method returns `None` and avoids dereferencing it. |
| 302 | + #[must_use] |
| 303 | + pub fn read_buffer(&self) -> Option<&'a [u8]> { |
| 304 | + if self.0.packet.in_data_buffer.is_null() { |
| 305 | + return None; |
| 306 | + } |
| 307 | + unsafe { |
| 308 | + Some(core::slice::from_raw_parts( |
| 309 | + self.0.packet.in_data_buffer.cast(), |
| 310 | + self.0.packet.in_transfer_length as usize, |
| 311 | + )) |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + /// Retrieves the buffer containing sense data returned by the device (if any). |
| 316 | + /// |
| 317 | + /// # Returns |
| 318 | + /// `Option<&[u8]>`: A slice of the sense data, or `None` if no sense data buffer was assigned. |
| 319 | + /// |
| 320 | + /// # Safety |
| 321 | + /// - If the buffer pointer is `NULL`, the method returns `None` and avoids dereferencing it. |
| 322 | + #[must_use] |
| 323 | + pub fn sense_data(&self) -> Option<&'a [u8]> { |
| 324 | + if self.0.packet.sense_data.is_null() { |
| 325 | + return None; |
| 326 | + } |
| 327 | + unsafe { |
| 328 | + Some(core::slice::from_raw_parts( |
| 329 | + self.0.packet.sense_data.cast(), |
| 330 | + self.0.packet.sense_data_length as usize, |
| 331 | + )) |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + /// Retrieves the status of the host adapter after executing the SCSI request. |
| 336 | + /// |
| 337 | + /// # Returns |
| 338 | + /// [`ScsiIoHostAdapterStatus`]: The status code indicating the result of the operation from the host adapter. |
| 339 | + #[must_use] |
| 340 | + pub const fn host_adapter_status(&self) -> ScsiIoHostAdapterStatus { |
| 341 | + self.0.packet.host_adapter_status |
| 342 | + } |
| 343 | + |
| 344 | + /// Retrieves the status of the target device after executing the SCSI request. |
| 345 | + /// |
| 346 | + /// # Returns |
| 347 | + /// [`ScsiIoTargetStatus`]: The status code returned by the target device. |
| 348 | + #[must_use] |
| 349 | + pub const fn target_status(&self) -> ScsiIoTargetStatus { |
| 350 | + self.0.packet.target_status |
| 351 | + } |
| 352 | +} |
0 commit comments