|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +//! NVM Express Protocols. |
| 4 | +
|
| 5 | +use crate::helpers::{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::nvme::{ |
| 11 | + NvmExpressCommand, NvmExpressCommandCdwValidity, NvmExpressPassThruCommandPacket, |
| 12 | +}; |
| 13 | + |
| 14 | +pub mod pass_thru; |
| 15 | + |
| 16 | +/// Represents the completion status of an NVMe command. |
| 17 | +/// |
| 18 | +/// This structure contains various fields related to the status and results |
| 19 | +/// of an executed command, including fields for error codes, specific command IDs, |
| 20 | +/// and general state of the NVMe device. |
| 21 | +pub type NvmeCompletion = uefi_raw::protocol::nvme::NvmExpressCompletion; |
| 22 | + |
| 23 | +/// Type of queues an NVMe command can be placed into |
| 24 | +/// (Which queue a command should be placed into depends on the command) |
| 25 | +pub type NvmeQueueType = uefi_raw::protocol::nvme::NvmExpressQueueType; |
| 26 | + |
| 27 | +/// Represents a request for executing an NVMe command. |
| 28 | +/// |
| 29 | +/// This structure encapsulates the command to be sent to the NVMe device, along with |
| 30 | +/// optional data transfer and metadata buffers. It ensures proper alignment and safety |
| 31 | +/// during interactions with the NVMe protocol. |
| 32 | +#[derive(Debug)] |
| 33 | +pub struct NvmeRequest<'a> { |
| 34 | + io_align: u32, |
| 35 | + cmd: NvmExpressCommand, |
| 36 | + packet: NvmExpressPassThruCommandPacket, |
| 37 | + transfer_buffer: Option<AlignedBuffer>, |
| 38 | + meta_data_buffer: Option<AlignedBuffer>, |
| 39 | + _phantom: PhantomData<&'a u8>, |
| 40 | +} |
| 41 | + |
| 42 | +macro_rules! define_nvme_command_builder_with_cdw { |
| 43 | + ($fnname:ident: $fieldname:ident => $flagmask:expr) => { |
| 44 | + /// Set the $fieldname parameter on the constructed nvme command. |
| 45 | + /// This also automatically flags the parameter as valid in the command's `flags` field. |
| 46 | + #[must_use] |
| 47 | + pub const fn $fnname(mut self, $fieldname: u32) -> Self { |
| 48 | + self.req.cmd.$fieldname = $fieldname; |
| 49 | + self.req.cmd.flags |= $flagmask.bits(); |
| 50 | + self |
| 51 | + } |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/// Builder for constructing an NVMe request. |
| 56 | +/// |
| 57 | +/// This structure provides convenient methods for configuring NVMe commands, |
| 58 | +/// including parameters like command-specific data words (CDWs) |
| 59 | +/// and optional buffers for transfer and metadata operations. |
| 60 | +/// |
| 61 | +/// It ensures safe and ergonomic setup of NVMe requests. |
| 62 | +#[derive(Debug)] |
| 63 | +pub struct NvmeRequestBuilder<'a> { |
| 64 | + req: NvmeRequest<'a>, |
| 65 | +} |
| 66 | +impl<'a> NvmeRequestBuilder<'a> { |
| 67 | + /// Creates a new builder for configuring an NVMe request. |
| 68 | + /// |
| 69 | + /// # Parameters |
| 70 | + /// - `io_align`: Memory alignment requirements for buffers. |
| 71 | + /// - `opcode`: The opcode for the NVMe command. |
| 72 | + /// - `queue_type`: Specifies the type of queue the command should be placed into. |
| 73 | + /// |
| 74 | + /// # Returns |
| 75 | + /// An instance of [`NvmeRequestBuilder`] for further configuration. |
| 76 | + #[must_use] |
| 77 | + pub fn new(io_align: u32, opcode: u8, queue_type: NvmeQueueType) -> Self { |
| 78 | + Self { |
| 79 | + req: NvmeRequest { |
| 80 | + io_align, |
| 81 | + cmd: NvmExpressCommand { |
| 82 | + cdw0: opcode as u32, |
| 83 | + ..Default::default() |
| 84 | + }, |
| 85 | + packet: NvmExpressPassThruCommandPacket { |
| 86 | + command_timeout: 0, |
| 87 | + transfer_buffer: ptr::null_mut(), |
| 88 | + transfer_length: 0, |
| 89 | + meta_data_buffer: ptr::null_mut(), |
| 90 | + meta_data_length: 0, |
| 91 | + queue_type, |
| 92 | + nvme_cmd: ptr::null(), // filled during execution |
| 93 | + nvme_completion: ptr::null_mut(), // filled during execution |
| 94 | + }, |
| 95 | + transfer_buffer: None, |
| 96 | + meta_data_buffer: None, |
| 97 | + _phantom: PhantomData, |
| 98 | + }, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// Configure the given timeout for this request. |
| 103 | + #[must_use] |
| 104 | + pub const fn with_timeout(mut self, timeout: Duration) -> Self { |
| 105 | + self.req.packet.command_timeout = (timeout.as_nanos() / 100) as u64; |
| 106 | + self |
| 107 | + } |
| 108 | + |
| 109 | + define_nvme_command_builder_with_cdw!(with_cdw2: cdw2 => NvmExpressCommandCdwValidity::CDW_2); |
| 110 | + define_nvme_command_builder_with_cdw!(with_cdw3: cdw3 => NvmExpressCommandCdwValidity::CDW_3); |
| 111 | + define_nvme_command_builder_with_cdw!(with_cdw10: cdw10 => NvmExpressCommandCdwValidity::CDW_10); |
| 112 | + define_nvme_command_builder_with_cdw!(with_cdw11: cdw11 => NvmExpressCommandCdwValidity::CDW_11); |
| 113 | + define_nvme_command_builder_with_cdw!(with_cdw12: cdw12 => NvmExpressCommandCdwValidity::CDW_12); |
| 114 | + define_nvme_command_builder_with_cdw!(with_cdw13: cdw13 => NvmExpressCommandCdwValidity::CDW_13); |
| 115 | + define_nvme_command_builder_with_cdw!(with_cdw14: cdw14 => NvmExpressCommandCdwValidity::CDW_14); |
| 116 | + define_nvme_command_builder_with_cdw!(with_cdw15: cdw15 => NvmExpressCommandCdwValidity::CDW_15); |
| 117 | + |
| 118 | + // # TRANSFER BUFFER |
| 119 | + // ######################################################################################## |
| 120 | + |
| 121 | + /// Uses a user-supplied buffer for reading data from the device. |
| 122 | + /// |
| 123 | + /// # Parameters |
| 124 | + /// - `bfr`: A mutable reference to an [`AlignedBuffer`] that will be used to store data read from the device. |
| 125 | + /// |
| 126 | + /// # Returns |
| 127 | + /// `Result<Self, AlignmentError>` indicating success or an alignment issue with the provided buffer. |
| 128 | + /// |
| 129 | + /// # Description |
| 130 | + /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to |
| 131 | + /// the `transfer_buffer` of the underlying [`NvmeRequest`]. |
| 132 | + pub fn use_transfer_buffer( |
| 133 | + mut self, |
| 134 | + bfr: &'a mut AlignedBuffer, |
| 135 | + ) -> Result<Self, AlignmentError> { |
| 136 | + // check alignment of externally supplied buffer |
| 137 | + bfr.check_alignment(self.req.io_align as usize)?; |
| 138 | + self.req.transfer_buffer = None; |
| 139 | + self.req.packet.transfer_buffer = bfr.ptr_mut().cast(); |
| 140 | + self.req.packet.transfer_length = bfr.len() as u32; |
| 141 | + Ok(self) |
| 142 | + } |
| 143 | + |
| 144 | + /// Adds a newly allocated transfer buffer to the built NVMe request. |
| 145 | + /// |
| 146 | + /// # Parameters |
| 147 | + /// - `len`: The size of the buffer (in bytes) to allocate for receiving data. |
| 148 | + /// |
| 149 | + /// # Returns |
| 150 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 151 | + pub fn with_transfer_buffer(mut self, len: usize) -> Result<Self, LayoutError> { |
| 152 | + let mut bfr = AlignedBuffer::alloc(len, self.req.io_align as usize)?; |
| 153 | + self.req.packet.transfer_buffer = bfr.ptr_mut().cast(); |
| 154 | + self.req.packet.transfer_length = bfr.len() as u32; |
| 155 | + self.req.transfer_buffer = Some(bfr); |
| 156 | + Ok(self) |
| 157 | + } |
| 158 | + |
| 159 | + // # METADATA BUFFER |
| 160 | + // ######################################################################################## |
| 161 | + |
| 162 | + /// Uses a user-supplied metadata buffer. |
| 163 | + /// |
| 164 | + /// # Parameters |
| 165 | + /// - `bfr`: A mutable reference to an [`AlignedBuffer`] that will be used to store metadata. |
| 166 | + /// |
| 167 | + /// # Returns |
| 168 | + /// `Result<Self, AlignmentError>` indicating success or an alignment issue with the provided buffer. |
| 169 | + /// |
| 170 | + /// # Description |
| 171 | + /// This method checks the alignment of the buffer against the protocol's requirements and assigns it to |
| 172 | + /// the `meta_data_buffer` of the underlying [`NvmeRequest`]. |
| 173 | + pub fn use_metadata_buffer( |
| 174 | + mut self, |
| 175 | + bfr: &'a mut AlignedBuffer, |
| 176 | + ) -> Result<Self, AlignmentError> { |
| 177 | + // check alignment of externally supplied buffer |
| 178 | + bfr.check_alignment(self.req.io_align as usize)?; |
| 179 | + self.req.meta_data_buffer = None; |
| 180 | + self.req.packet.meta_data_buffer = bfr.ptr_mut().cast(); |
| 181 | + self.req.packet.meta_data_length = bfr.len() as u32; |
| 182 | + Ok(self) |
| 183 | + } |
| 184 | + |
| 185 | + /// Adds a newly allocated metadata buffer to the built NVMe request. |
| 186 | + /// |
| 187 | + /// # Parameters |
| 188 | + /// - `len`: The size of the buffer (in bytes) to allocate for storing metadata. |
| 189 | + /// |
| 190 | + /// # Returns |
| 191 | + /// `Result<Self, LayoutError>` indicating success or a memory allocation error. |
| 192 | + pub fn with_metadata_buffer(mut self, len: usize) -> Result<Self, LayoutError> { |
| 193 | + let mut bfr = AlignedBuffer::alloc(len, self.req.io_align as usize)?; |
| 194 | + self.req.packet.meta_data_buffer = bfr.ptr_mut().cast(); |
| 195 | + self.req.packet.meta_data_length = bfr.len() as u32; |
| 196 | + self.req.meta_data_buffer = Some(bfr); |
| 197 | + Ok(self) |
| 198 | + } |
| 199 | + |
| 200 | + /// Build the final [`NvmeRequest`]. |
| 201 | + /// |
| 202 | + /// # Returns |
| 203 | + /// A fully-configured [`NvmeRequest`] ready for execution. |
| 204 | + #[must_use] |
| 205 | + pub fn build(self) -> NvmeRequest<'a> { |
| 206 | + self.req |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +/// Represents the response from executing an NVMe command. |
| 211 | +/// |
| 212 | +/// This structure encapsulates the original request, as well as the command's completion status. |
| 213 | +#[derive(Debug)] |
| 214 | +pub struct NvmeResponse<'a> { |
| 215 | + req: NvmeRequest<'a>, |
| 216 | + completion: NvmeCompletion, |
| 217 | +} |
| 218 | +impl<'a> NvmeResponse<'a> { |
| 219 | + /// Returns the buffer containing transferred data from the device (if any). |
| 220 | + /// |
| 221 | + /// # Returns |
| 222 | + /// `Option<&[u8]>`: A slice of the transfer buffer, or `None` if the request was started without. |
| 223 | + #[must_use] |
| 224 | + pub fn transfer_buffer(&self) -> Option<&'a [u8]> { |
| 225 | + if self.req.packet.transfer_buffer.is_null() { |
| 226 | + return None; |
| 227 | + } |
| 228 | + unsafe { |
| 229 | + Some(core::slice::from_raw_parts( |
| 230 | + self.req.packet.transfer_buffer.cast(), |
| 231 | + self.req.packet.transfer_length as usize, |
| 232 | + )) |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + /// Returns the buffer containing metadata data from the device (if any). |
| 237 | + /// |
| 238 | + /// # Returns |
| 239 | + /// `Option<&[u8]>`: A slice of the metadata buffer, or `None` if the request was started without. |
| 240 | + #[must_use] |
| 241 | + pub fn metadata_buffer(&self) -> Option<&'a [u8]> { |
| 242 | + if self.req.packet.meta_data_buffer.is_null() { |
| 243 | + return None; |
| 244 | + } |
| 245 | + unsafe { |
| 246 | + Some(core::slice::from_raw_parts( |
| 247 | + self.req.packet.meta_data_buffer.cast(), |
| 248 | + self.req.packet.meta_data_length as usize, |
| 249 | + )) |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + /// Provides access to the completion structure of the NVMe command. |
| 254 | + /// |
| 255 | + /// # Returns |
| 256 | + /// A reference to the [`NvmeCompletion`] structure containing the status and results of the command. |
| 257 | + #[must_use] |
| 258 | + pub const fn completion(&self) -> &NvmeCompletion { |
| 259 | + &self.completion |
| 260 | + } |
| 261 | +} |
0 commit comments