Skip to content

Commit 5c6d725

Browse files
committed
nvme: mi: Convert to deku
Use of bitfield was getting unwieldy. Deku is an ergonomic binary marshalling crate that we're starting to use elsewhere as well. Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
1 parent 44a91c4 commit 5c6d725

File tree

9 files changed

+1339
-1082
lines changed

9 files changed

+1339
-1082
lines changed

Cargo.lock

Lines changed: 118 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ edition = "2024"
55
license = "GPL-3.0-only"
66

77
[dependencies]
8-
bitfield = "0.14.0"
98
crc = "3.2.1"
9+
deku = { git = "https://github.com/CodeConstruct/deku.git", tag = "cc/deku-v0.19.1/no-alloc-3", default-features = false }
1010
heapless = "0.8.0"
1111
hmac = { version = "0.12.1", default-features = false }
1212
log = "0.4.22"
1313
mctp = { version = "0.2.0", default-features = false }
14+
num-derive = { version = "0.4", default-features = false }
15+
num-traits = { version = "0.2", default-features = false }
1416
sha2 = { version = "0.10.9", default-features = false }
1517
uuid = { version = "1.17.0", default-features = false }
1618

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* Copyright (c) 2025 Code Construct
44
*/
55
#![no_std]
6-
// bitfield needs this to cope with large structure sizes?
7-
#![recursion_limit = "512"]
86

97
pub mod nvme;
8+
mod wire;
109

11-
#[macro_use]
12-
extern crate bitfield;
10+
extern crate deku;
11+
extern crate num_derive;

src/nvme.rs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ pub mod mi;
77
use hmac::Mac;
88
use uuid::Uuid;
99

10+
const MAX_CONTROLLERS: usize = 2;
11+
const MAX_NAMESPACES: usize = 2;
12+
const MAX_PORTS: usize = 2;
13+
const MAX_NIDTS: usize = 2;
14+
1015
#[allow(dead_code)]
1116
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1217
#[repr(u8)]
@@ -97,19 +102,12 @@ impl Default for PCIePort {
97102
}
98103

99104
#[allow(dead_code)]
100-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101-
#[repr(u8)]
105+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
102106
enum SMBusFrequency {
103-
FreqNotSupported = 0x00,
104-
Freq100kHz = 0x01,
105-
Freq400kHz = 0x02,
106-
Freq1MHz = 0x03,
107-
}
108-
109-
impl From<SMBusFrequency> for u8 {
110-
fn from(freq: SMBusFrequency) -> Self {
111-
freq as Self
112-
}
107+
FreqNotSupported,
108+
Freq100kHz,
109+
Freq400kHz,
110+
Freq1MHz,
113111
}
114112

115113
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -367,43 +365,21 @@ impl SubsystemHealth {
367365
}
368366
}
369367

370-
#[derive(Debug)]
371-
#[repr(u8)]
368+
#[derive(Clone, Copy, Debug)]
372369
pub enum CommandSetIdentifier {
373-
NVMCommandSet = 0,
374-
KeyValueCommandSet = 1,
375-
ZonedNamespaceCommandSet = 2,
376-
SubsystemLocalMemoryCommandSet = 3,
377-
ComputationalProgramsCommandSet = 4,
378-
}
379-
380-
impl CommandSetIdentifier {
381-
fn id(&self) -> u8 {
382-
// https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.access-memory
383-
unsafe { *(self as *const Self as *const u8) }
384-
}
370+
NVMCommandSet,
371+
KeyValueCommandSet,
372+
ZonedNamespaceCommandSet,
373+
SubsystemLocalMemoryCommandSet,
374+
ComputationalProgramsCommandSet,
385375
}
386376

387-
// NSID
388-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
389-
pub struct NamespaceId(u32);
390-
391-
// NID: 5.1.13.2.3, Base v2.1
392-
#[derive(Debug)]
393-
#[repr(u8)]
377+
#[derive(Clone, Copy, Debug)]
394378
pub enum NamespaceIdentifierType {
395-
Reserved = 0,
396-
IEUID([u8; 8]) = 1,
397-
NGUID([u8; 16]) = 2,
398-
NUUID(Uuid) = 3,
399-
CSI(CommandSetIdentifier) = 4,
400-
}
401-
402-
impl NamespaceIdentifierType {
403-
fn id(&self) -> u8 {
404-
// https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.discriminant.access-memory
405-
unsafe { *(self as *const Self as *const u8) }
406-
}
379+
IEUID([u8; 8]),
380+
NGUID([u8; 16]),
381+
NUUID(Uuid),
382+
CSI(CommandSetIdentifier),
407383
}
408384

409385
#[derive(Debug)]
@@ -415,6 +391,10 @@ pub struct Namespace {
415391
nids: [NamespaceIdentifierType; 2],
416392
}
417393

394+
// NSID
395+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
396+
pub struct NamespaceId(u32);
397+
418398
impl Namespace {
419399
fn generate_uuid(seed: &[u8], nsid: NamespaceId) -> Uuid {
420400
let mut hasher = hmac::Hmac::<sha2::Sha256>::new_from_slice(seed).unwrap();
@@ -438,10 +418,6 @@ impl Namespace {
438418
}
439419
}
440420

441-
const MAX_CONTROLLERS: usize = 2;
442-
const MAX_NAMESPACES: usize = 2;
443-
const MAX_PORTS: usize = 2;
444-
445421
#[derive(Clone, Copy, Debug)]
446422
pub struct SubsystemInfo {
447423
pub pci_vid: u16,

0 commit comments

Comments
 (0)