Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,61 @@ pub enum NamespaceIdentifierType {
Csi(nvme::CommandSetIdentifier),
}

// Base v2.1, 3.2.1
// Base v2.1, 3.2.1.5, Figure 71
#[derive(Clone, Copy, Debug)]
enum NamespaceIdDisposition<'a> {
Invalid,
Broadcast,
Unallocated,
Inactive(&'a Namespace),
Active(&'a Namespace),
}

// NSID
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NamespaceId(u32);

impl NamespaceId {
fn disposition<'a>(&self, subsys: &'a Subsystem) -> NamespaceIdDisposition<'a> {
if self.0 == 0 {
return NamespaceIdDisposition::Invalid;
}

if self.0 == u32::MAX {
return NamespaceIdDisposition::Broadcast;
}

assert!(subsys.nss.capacity() <= u32::MAX.try_into().unwrap());
if self.0 > subsys.nss.capacity() as u32 {
return NamespaceIdDisposition::Invalid;
}

let Some(ns) = subsys.nss.iter().find(|nsid| self.0 == nsid.id.0) else {
return NamespaceIdDisposition::Unallocated;
};

if !subsys
.ctlrs
.iter()
.flat_map(|c| c.active_ns.iter())
.any(|&nsid| nsid.0 == self.0)
{
return NamespaceIdDisposition::Inactive(ns);
}

NamespaceIdDisposition::Active(ns)
}

fn max(subsys: &Subsystem) -> u32 {
subsys
.nss
.capacity()
.try_into()
.expect("Too many namespaces")
}
}

#[derive(Debug)]
pub struct Namespace {
id: NamespaceId,
Expand All @@ -423,10 +478,6 @@ pub struct Namespace {
nids: [NamespaceIdentifierType; 2],
}

// NSID
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NamespaceId(u32);

impl Namespace {
fn generate_uuid(seed: &[u8], nsid: NamespaceId) -> Uuid {
let mut hasher = hmac::Hmac::<sha2::Sha256>::new_from_slice(seed).unwrap();
Expand Down
36 changes: 34 additions & 2 deletions src/nvme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
pub mod mi;

use deku::ctx::Endian;
use deku::{DekuRead, DekuWrite, deku_derive};
use deku::{DekuError, DekuRead, DekuWrite, deku_derive};
use flagset::flags;
use log::debug;

use crate::wire::WireFlagSet;
use crate::wire::WireString;
Expand Down Expand Up @@ -85,7 +86,7 @@ enum CommandRetryDelay {
}
unsafe impl Discriminant<u8> for CommandRetryDelay {}

// Base v2.1, 4.3.2, Figure 101
// Base v2.1, 4.2.3, Figure 101
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
enum AdminIoCqeStatusType {
Expand All @@ -106,9 +107,18 @@ unsafe impl Discriminant<u8> for AdminIoCqeStatusType {}
enum AdminIoCqeGenericCommandStatus {
SuccessfulCompletion = 0x00,
InvalidFieldInCommand = 0x02,
InternalError = 0x06,
InvalidNamespaceOrFormat = 0x0b,
}
unsafe impl Discriminant<u8> for AdminIoCqeGenericCommandStatus {}

impl From<DekuError> for AdminIoCqeGenericCommandStatus {
fn from(err: DekuError) -> Self {
debug!("Codec operation failed: {err}");
Self::InternalError
}
}

// Base v2.1, 4.6.1, Figure 137
// TODO: Unify with ControllerListResponse
#[derive(Debug, DekuRead, Eq, PartialEq)]
Expand Down Expand Up @@ -375,6 +385,8 @@ enum AdminIdentifyCnsRequestType {
IoActiveNamespaceIdList = 0x07,
IdentifyNamespace = 0x08,
AllocatedNamespaceIdList = 0x10,
IdentifyNamespaceForAllocatedNamespaceId = 0x11,
NamespaceAttachedControllerList = 0x12,
NvmSubsystemControllerList = 0x13,
SecondaryControllerList = 0x15,
}
Expand Down Expand Up @@ -404,6 +416,26 @@ pub struct AdminIdentifyNvmIdentifyNamespaceResponse {
}
impl Encode<4096> for AdminIdentifyNvmIdentifyNamespaceResponse {}

impl From<&crate::Namespace> for AdminIdentifyNvmIdentifyNamespaceResponse {
fn from(value: &crate::Namespace) -> Self {
Self {
nsze: value.size,
ncap: value.capacity,
nuse: value.used,
nsfeat: ((value.size == value.capacity) as u8),
nlbaf: 0,
flbas: 0,
mc: 0,
dpc: 0,
dps: 0,
nvmcap: 2_u128.pow(value.block_order as u32) * value.size as u128,
lbaf0: 0,
lbaf0_lbads: value.block_order,
lbaf0_rp: 0,
}
}
}

// Base v2.1, 5.1.13.1, Figure 311
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite)]
#[deku(id_type = "u8", endian = "endian", ctx = "endian: Endian")]
Expand Down
Loading