nvme-mi-dev 0.1.0

A device-side implementation of NVMe-MI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
// SPDX-License-Identifier: GPL-3.0-only
/*
 * Copyright (c) 2025 Code Construct
 */
pub mod mi;

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

use crate::wire::WireFlagSet;
use crate::wire::WireString;
use crate::wire::WireUuid;
use crate::wire::WireVec;
use crate::{Discriminant, Encode};

// Base v2.1, 3.1.4, Figure 33
#[repr(usize)]
pub enum ControllerProperties {
    Cc(ControllerConfiguration) = 0x14,
}

// Base v2.1, 3.1.4.5, Figure 41
#[derive(Clone, Copy, Debug, Default)]
pub struct ControllerConfiguration {
    pub en: bool,
}

// Base v2.1, 3.1.4.6, Figure 42
flags! {
    #[repr(u32)]
    pub enum ControllerStatusFlags: u32 {
        Rdy = 1 << 0,
        Cfs = 1 << 1,
        ShstInProgress = 1 << 2,
        ShstComplete = 1 << 3,
        ShstReserved = (ControllerStatusFlags::ShstInProgress | ControllerStatusFlags::ShstComplete).bits(),
        Nssro = 1 << 4,
        Pp = 1 << 5,
        St = 1 << 6,
    }
}

// Base v2.1, 4.2.1, Figure 98
//
// Switch to LSB order because the packing of these fields is just messy,
// largely thanks to the position of P. However, the SC / SCT ordering also
// throws ergonomic deku representations straight out the window.
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(bit_order = "lsb", ctx = "endian: Endian", endian = "endian")]
struct AdminIoCqeStatus {
    cid: u16,
    #[deku(bits = 1)]
    p: bool,
    #[deku(bits = 8)]
    sc: u8,
    #[deku(bits = 3)]
    sct: u8,
    #[deku(bits = 2)]
    crd: CommandRetryDelay,
    #[deku(bits = 1)]
    m: bool,
    #[deku(bits = 1)]
    dnr: bool,
}

impl From<AdminIoCqeStatusType> for AdminIoCqeStatus {
    fn from(value: AdminIoCqeStatusType) -> Self {
        match value {
            AdminIoCqeStatusType::GenericCommandStatus(sts) => AdminIoCqeStatus {
                cid: 0,
                sc: sts as u8,
                p: true,
                dnr: sts != AdminIoCqeGenericCommandStatus::SuccessfulCompletion,
                m: false,
                crd: CommandRetryDelay::None,
                sct: value.id(),
            },
            AdminIoCqeStatusType::CommandSpecificStatus(sts) => AdminIoCqeStatus {
                cid: 0,
                sc: sts,
                p: true,
                dnr: true,
                m: false,
                crd: CommandRetryDelay::None,
                sct: value.id(),
            },
            AdminIoCqeStatusType::MediaAndDataIntegrityErrors => todo!(),
            AdminIoCqeStatusType::PathRelatedStatus => todo!(),
            AdminIoCqeStatusType::VendorSpecific => todo!(),
        }
    }
}

// Base v2.1, 4.2.3, Figure 100, CRD
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(
    bits = "bits.0",
    bit_order = "order",
    ctx = "endian: Endian, bits: BitSize, order: Order",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
enum CommandRetryDelay {
    None = 0x00,
    Time1 = 0x01,
    Time2 = 0x02,
    Time3 = 0x03,
}

// Base v2.1, 4.2.3, Figure 101
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
enum AdminIoCqeStatusType {
    GenericCommandStatus(AdminIoCqeGenericCommandStatus) = 0x00,
    CommandSpecificStatus(u8) = 0x01,
    #[expect(dead_code)]
    MediaAndDataIntegrityErrors = 0x02,
    #[expect(dead_code)]
    PathRelatedStatus = 0x03,
    #[expect(dead_code)]
    VendorSpecific = 0x07,
}
unsafe impl Discriminant<u8> for AdminIoCqeStatusType {}

// Base v2.1, 4.2.3.1, Figure 102
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
enum AdminIoCqeGenericCommandStatus {
    SuccessfulCompletion = 0x00,
    InvalidFieldInCommand = 0x02,
    InternalError = 0x06,
    InvalidNamespaceOrFormat = 0x0b,
}

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)]
#[deku(ctx = "endian: Endian", endian = "little")]
struct ControllerListRequest {
    numids: u16,
    #[deku(count = "*numids")]
    ids: WireVec<u16, 2047>,
}

// Base v2.1, 5.1.10, Figure 189, SES
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(
    bits = "bits.0",
    ctx = "endian: Endian, bits: BitSize",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
enum SecureEraseSettings {
    NoOperation = 0b000,
    UserDataErase = 0b001,
    CryptographicErase = 0b010,
}

// Base v2.1, 5.1.10, Figure 189
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian", endian = "endian")]
struct AdminFormatNvmConfiguration {
    #[deku(bits = "3")]
    pi: u8,
    #[deku(bits = "1")]
    mset: bool,
    #[deku(bits = "4")]
    lbafl: u8,
    #[deku(bits = "2", pad_bits_before = "2")]
    lbafu: u8,
    #[deku(bits = "3")]
    ses: SecureEraseSettings,
    #[deku(bits = "1", pad_bytes_after = "2")]
    pil: bool,
}

impl AdminFormatNvmConfiguration {
    fn lbafi(&self) -> u8 {
        self.lbafu << 4 | self.lbafl
    }
}

// Base v2.1, 5.1.12, Figure 202
// MI v2.0, 6.3, Figure 141
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian, lid: u8", id = "lid", endian = "endian")]
#[repr(u8)]
pub enum AdminGetLogPageLidRequestType {
    SupportedLogPages = 0x00,
    ErrorInformation = 0x01,
    SmartHealthInformation = 0x02,
    FeatureIdentifiersSupportedAndEffects = 0x12,
    SanitizeStatus = 0x81,
}
unsafe impl crate::Discriminant<u8> for AdminGetLogPageLidRequestType {}

// Base v2.1, 5.1.12.1.1, Figure 203
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "little")]
pub struct AdminGetLogPageSupportedLogPagesResponse {
    lsids: WireVec<LidSupportedAndEffectsDataStructure, 256>,
}
impl Encode<1024> for AdminGetLogPageSupportedLogPagesResponse {}

// Base v2.1, 5.1.12.1.1, Figure 204
flags! {
    pub enum LidSupportedAndEffectsFlags: u8 {
        Lsupp,
        Ios,
    }
}

// Base v2.1, 5.1.12.1.1, Figure 204
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite)]
#[deku(ctx = "endian: Endian", endian = "endian")]
pub struct LidSupportedAndEffectsDataStructure {
    flags: WireFlagSet<LidSupportedAndEffectsFlags>,
    #[deku(seek_from_current = "1")]
    lidsp: u16,
}

// Base v2.1, 5.1.12.1.3, Figure 206, CW
flags! {
    pub enum CriticalWarningFlags: u8 {
        Ascbt,
        Ttc,
        Ndr,
        Amro,
        Vmbf,
        Pmrro,
    }
}

// Base v2.1, 5.1.12.1.3, Figure 206, EGCWS
flags! {
    pub enum EnduranceGroupCriticalWarningSummaryFlags: u8 {
        Egascbt = 1 << 0,
        Egdr = 1 << 2,
        Egro = 1 << 3,
    }
}

// Base v2.1, 5.1.12.1.3, Figure 206
#[derive(Debug, Default, DekuRead, DekuWrite)]
#[deku(endian = "little")]
pub struct SmartHealthInformationLogPageResponse {
    cw: WireFlagSet<CriticalWarningFlags>,
    ctemp: u16,
    avsp: u8,
    avspt: u8,
    pused: u8,
    egcws: WireFlagSet<EnduranceGroupCriticalWarningSummaryFlags>,
    #[deku(seek_from_current = "25")]
    dur: u128,
    duw: u128,
    hrc: u128,
    hwc: u128,
    cbt: u128,
    pwrc: u128,
    poh: u128,
    upl: u128,
    mdie: u128,
    neile: u128,
    wctt: u32,
    cctt: u32,
    tsen: [u16; 8],
    tmttc: [u32; 2],
    #[deku(pad_bytes_after = "280")]
    tttmt: [u32; 2],
}
impl Encode<512> for SmartHealthInformationLogPageResponse {}

// Base v2.1, 5.1.12.1.18, Figure 262
flags! {
    pub enum FidSupportedAndEffectsFlags: u32 {
        Fsupp = 1 << 0,
        Udcc = 1 << 1,
        Ncc = 1 << 2,
        Nic = 1 << 3,
        Ccc = 1 << 4,
        Uss = 1 << 19,
        FspNscpe = 1 << 20,
        FspCscpe = 1 << 21,
        FspNsetscpe = 1 << 22,
        FspEgscpe = 1 << 23,
        FspDscpe = 1 << 24,
        FspNsubscpe = 1 << 25,
        FspCdqscp = 1 << 26,
        FspMask = (
            FidSupportedAndEffectsFlags::FspNscpe
            | FidSupportedAndEffectsFlags::FspNsetscpe
            | FidSupportedAndEffectsFlags::FspEgscpe
            | FidSupportedAndEffectsFlags::FspNsubscpe
            | FidSupportedAndEffectsFlags::FspCdqscp
        ).bits()
    }
}

// Base v2.1, 5.1.12.1.33, Figure 291, SSTAT, SOS
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(
    bits = "bits.0",
    ctx = "endian: Endian, bits: BitSize",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
pub enum SanitizeOperationStatus {
    #[default]
    SanitizeNeverStarted = 0b000,
    Sanitized = 0b001,
    Sanitizing = 0b010,
    SanitizeFailed = 0b011,
    SanitizedUnexpectedDeallocate = 0b100,
}
unsafe impl crate::Discriminant<u8> for SanitizeOperationStatus {}

// Base v2.1, 5.1.12.1.33, Figure 291, SSTAT
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite)]
#[deku(ctx = "endian: Endian", endian = "endian")]
pub struct SanitizeStatus {
    #[deku(bits = "5")]
    opc: u8,
    #[deku(bits = "3")]
    sos: SanitizeOperationStatus,
    #[deku(bits = "1", pad_bits_before = "6")]
    mvcncled: bool,
    #[deku(bits = "1")]
    gde: bool,
}

// Base v2.1, 5.12.1.33, Fgure 291, SSI, SANS
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(
    bits = "bits.0",
    ctx = "endian: Endian, bits: BitSize",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
enum SanitizeState {
    #[default]
    Idle = 0x00,
    RestrictedProcessing = 0x01,
    RestrictedFailure = 0x02,
    UnrestrictedProcessing = 0x03,
    UnrestrictedFailure = 0x04,
    MediaVerification = 0x05,
    PostVerificationDeallocation = 0x06,
}
unsafe impl crate::Discriminant<u8> for SanitizeState {}

// Base v2.1, 5.1.12.1.33, Figure 291, SSI
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite)]
#[deku(ctx = "endian: Endian", endian = "endian")]
pub struct SanitizeStateInformation {
    #[deku(bits = "4")]
    fails: u8,
    #[deku(bits = "4")]
    sans: SanitizeState,
}

// Base v2.1, 5.1.12.1.33, Figure 291
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "little")]
struct SanitizeStatusLogPageResponse {
    sprog: u16,
    sstat: SanitizeStatus,
    scdw10: AdminSanitizeConfiguration,
    eto: u32,
    etbe: u32,
    etce: u32,
    etodmm: u32,
    etbenmm: u32,
    etcenmm: u32,
    etpvds: u32,
    ssi: SanitizeStateInformation,
}
impl Encode<512> for SanitizeStatusLogPageResponse {}

// Base v2.1, 5.1.13.1, Figure 310
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian, cns: u8", id = "cns", endian = "endian")]
#[repr(u8)]
enum AdminIdentifyCnsRequestType {
    NvmIdentifyNamespace = 0x00,
    IdentifyController = 0x01,
    ActiveNamespaceIDList = 0x02,
    NamespaceIdentificationDescriptorList = 0x03,
    IoIdentifyNamespace = 0x05,
    IoIdentifyController = 0x06,
    IoActiveNamespaceIdList = 0x07,
    IdentifyNamespace = 0x08,
    AllocatedNamespaceIdList = 0x10,
    IdentifyNamespaceForAllocatedNamespaceId = 0x11,
    NamespaceAttachedControllerList = 0x12,
    NvmSubsystemControllerList = 0x13,
    SecondaryControllerList = 0x15,
}
unsafe impl Discriminant<u8> for AdminIdentifyCnsRequestType {}

// Base v2.1, 5.1.13.1, Figure 310
// NVM Command Set v1.0c, 4.1.5.1, Figure 97
#[derive(Debug, Default, DekuRead, DekuWrite)]
#[deku(endian = "little")]
pub struct AdminIdentifyNvmIdentifyNamespaceResponse {
    nsze: u64,
    ncap: u64,
    nuse: u64,
    nsfeat: u8,
    nlbaf: u8,
    flbas: u8,
    mc: u8,
    dpc: u8,
    dps: u8,
    #[deku(seek_from_current = "18")]
    nvmcap: u128,
    #[deku(seek_from_current = "64")]
    // FIXME: use another struct
    lbaf0: u16,
    lbaf0_lbads: u8,
    lbaf0_rp: u8,
}
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")]
#[repr(u8)]
pub enum CommandSetIdentifier {
    Nvm = 0x00,
    KeyValue = 0x01,
    ZonedNamespace = 0x02,
    SubsystemLocalMemory = 0x03,
    ComputationalPrograms = 0x04,
}

// Base v2.1, 5.1.13.2.1, Figure 312, CMIC
flags! {
    enum ControllerMultipathIoNamespaceSharingCapabilityFlags: u8 {
        Mports,
        Mctrs,
        Ft,
        Anars,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, CTRATT
flags! {
    enum ControllerAttributeFlags: u32 {
        Hids,
        Nopspm,
        Nsets,
        Rrlvls,
        Egs,
        Plm,
        Tbkas,
        Ng,
        Sqa,
        Ulist,
        Mds,
        Fcm,
        Vcm,
        Deg,
        Dnvms,
        Elbas,
        Mem,
        Hmbr,
        Rhii,
        Fdps,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, CNTRLTYPE
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite, PartialEq)]
#[deku(id_type = "u8", endian = "endian", ctx = "endian: Endian")]
#[repr(u8)]
enum ControllerType {
    Reserved = 0x00,
    IoController = 0x01,
    DiscoveryController = 0x02,
    AdministrativeController = 0x03,
}

impl From<crate::ControllerType> for ControllerType {
    fn from(value: crate::ControllerType) -> Self {
        match value {
            crate::ControllerType::Io => Self::IoController,
            crate::ControllerType::Discovery => Self::DiscoveryController,
            crate::ControllerType::Administrative => Self::AdministrativeController,
        }
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, NVMSR
flags! {
    enum NvmSubsystemReportFlags: u8 {
        Nvmesd,
        Nvmee,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, MEC
flags! {
    enum ManagementEndpointCapabilityFlags: u8 {
        Twpme,
        Pcieme,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, LPA
flags! {
    #[repr(u8)]
    pub enum LogPageAttributes: u8 {
        Smarts,
        Cses,
        Lpeds,
        Ts,
        Pes,
        Mlps,
        Da4s,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, SANICAP, NODMMAS
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite)]
#[deku(
    bits = "bits.0",
    bit_order = "order",
    ctx = "endian: Endian, bits: BitSize, order: Order",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
pub enum NoDeallocateModifiesMediaAfterSanitize {
    Undefined = 0b00,
    #[default]
    Unmodified = 0b01,
    Modified = 0b10,
    Reserved = 0b11,
}

// Base v2.1, 5.1.13.2.1, Figure 312, SANICAP
flags! {
    pub enum SanitizeCapabilityFlags: u32 {
        Ces = 1 << 0,
        Bes = 1 << 1,
        Ows = 1 << 2,
        Vers = 1 << 3,
        Ndi = 1 << 29,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312, SANICAP
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite)]
#[deku(bit_order = "lsb", ctx = "endian: Endian", endian = "endian")]
pub struct SanitizeCapabilities {
    #[deku(bits = 30)]
    caps: WireFlagSet<SanitizeCapabilityFlags>,
    #[deku(bits = 2)]
    nodmmas: NoDeallocateModifiesMediaAfterSanitize,
}

// Base v2.1, 5.1.13.2.1, Figure 312, FNA
flags! {
    pub enum FormatNvmAttributes: u8 {
        Fns,
        Sens,
        Cryes,
        Fnvmbs,
    }
}

// Base v2.1, 5.1.13.2.1, Figure 312
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "little")]
struct AdminIdentifyControllerResponse {
    vid: u16,
    ssvid: u16,
    sn: WireString<20>,
    mn: WireString<40>,
    fr: WireString<8>,
    rab: u8,
    ieee: [u8; 3],
    cmic: WireFlagSet<ControllerMultipathIoNamespaceSharingCapabilityFlags>,
    mdts: u8,
    cntlid: u16,
    ver: u32,
    rtd3r: u32,
    rtd3e: u32,
    oaes: u32,
    ctratt: WireFlagSet<ControllerAttributeFlags>,
    #[deku(seek_from_current = "11")]
    cntrltype: crate::nvme::ControllerType,
    #[deku(seek_from_current = "141")]
    nvmsr: WireFlagSet<NvmSubsystemReportFlags>,
    vwci: u8,
    mec: WireFlagSet<ManagementEndpointCapabilityFlags>,
    ocas: u16,
    acl: u8,
    aerl: u8,
    frmw: u8,
    lpa: WireFlagSet<LogPageAttributes>,
    elpe: u8,
    npss: u8,
    avscc: u8,
    apsta: u8,
    wctemp: u16,
    cctemp: u16,
    #[deku(seek_from_current = "49")]
    fwug: u8,
    kas: u16,
    #[deku(seek_from_current = "6")]
    sanicap: SanitizeCapabilities,
    #[deku(seek_from_current = "54")]
    cqt: u16,
    #[deku(seek_from_current = "124")]
    sqes: u8,
    cqes: u8,
    maxcmd: u16,
    nn: u32,
    oncs: u16,
    fuses: u16,
    fna: WireFlagSet<FormatNvmAttributes>,
    vwc: u8,
    awun: u16,
    awupf: u16,
    icsvscc: u8,
    nwpc: u8,
    #[deku(seek_from_current = "8")]
    mnan: u32,
    #[deku(seek_from_current = "224")]
    subnqn: WireString<256>,
    #[deku(seek_from_current = "778")]
    fcatt: u8,
    msdbd: u8,
    ofcs: u16,
}
impl Encode<4096> for AdminIdentifyControllerResponse {}

// Base v2.1, 5.1.13.2.2
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "little")]
struct AdminIdentifyActiveNamespaceIdListResponse {
    nsid: WireVec<u32, 1024>,
}
impl Encode<4096> for AdminIdentifyActiveNamespaceIdListResponse {}

impl AdminIdentifyActiveNamespaceIdListResponse {
    fn new() -> Self {
        Self {
            nsid: WireVec::new(),
        }
    }
}

// Base v2.1, 5.1.13.2.3, Figure 315
#[derive(Clone, Copy, Debug, DekuRead, DekuWrite)]
#[deku(id_type = "u8", endian = "endian", ctx = "endian: Endian")]
#[repr(u8)]
enum NamespaceIdentifierType {
    Reserved = 0,
    #[deku(id = 1)]
    Ieuid(u8, u16, [u8; 8]),
    #[deku(id = 2)]
    Nguid(u8, u16, [u8; 16]),
    #[deku(id = 3)]
    Nuuid(u8, u16, WireUuid),
    #[deku(id = 4)]
    Csi(u8, u16, crate::nvme::CommandSetIdentifier),
}

impl From<crate::NamespaceIdentifierType> for NamespaceIdentifierType {
    fn from(value: crate::NamespaceIdentifierType) -> Self {
        match value {
            crate::NamespaceIdentifierType::Ieuid(v) => Self::Ieuid(v.len() as u8, 0, v),
            crate::NamespaceIdentifierType::Nguid(v) => Self::Nguid(v.len() as u8, 0, v),
            crate::NamespaceIdentifierType::Nuuid(uuid) => Self::Nuuid(16, 0, WireUuid::new(uuid)),
            crate::NamespaceIdentifierType::Csi(v) => Self::Csi(1, 0, v),
        }
    }
}

// Base v2.1, 5.1.13.2.3, Figure 315
#[derive(Debug)]
#[deku_derive(DekuRead, DekuWrite)]
#[deku(endian = "little")]
struct AdminIdentifyNamespaceIdentificationDescriptorListResponse {
    nids: WireVec<NamespaceIdentifierType, { crate::MAX_NIDTS }>,
}
impl Encode<4096> for AdminIdentifyNamespaceIdentificationDescriptorListResponse {}

// Base v2.1, 5.1.13.2.9
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(endian = "little")]
struct AdminIdentifyAllocatedNamespaceIdListResponse {
    nsid: WireVec<u32, 1024>,
}
impl Encode<4096> for AdminIdentifyAllocatedNamespaceIdListResponse {}

// Base v2.1, Section 5.1.13.2.12
#[derive(Debug, DekuWrite)]
#[deku(endian = "little")]
struct ControllerListResponse {
    #[deku(update = "self.ids.len()")]
    numids: u16,
    #[deku(count = "numids")]
    ids: WireVec<u16, 2047>,
}
impl Encode<4096> for ControllerListResponse {}

impl ControllerListResponse {
    fn new() -> Self {
        Self {
            numids: 0,
            ids: WireVec::new(),
        }
    }
}

// Base v2.1, 5.1.20.1, Figure 364, SEL
#[derive(Debug, DekuRead, Eq, PartialEq)]
#[deku(ctx = "endian: Endian", endian = "endian", id_type = "u8")]
#[repr(u8)]
enum AdminNamespaceAttachmentSelect {
    ControllerAttach = 0x00,
    ControllerDetach = 0x01,
}

// Base v2.1, 5.1.21, Figure 376, SEL
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian, sel: u8", endian = "endian", id = "sel")]
#[repr(u8)]
enum AdminNamespaceManagementSelect {
    #[deku(id = 0x00)]
    Create(NvmNamespaceManagementCreate),
    Delete = 0x01,
}

// Base v2.1, 5.1.21
// NVM Command Set v1.0c, 4.1.6, Figure 105
#[derive(Debug, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian", endian = "endian")]
struct NvmNamespaceManagementCreate {
    nsze: u64,
    ncap: u64,
    #[deku(seek_from_current = "10")]
    flbas: u8,
    #[deku(seek_from_current = "2")]
    dps: u8,
    nmic: u8,
    #[deku(seek_from_current = "61")]
    anagrpid: u32,
    #[deku(seek_from_current = "4")]
    nvmsetid: u16,
    endgid: u16,
    #[deku(seek_from_current = "280")]
    #[deku(pad_bytes_after = "3704")]
    lbstm: u64,
}

// Base v2.1, 5.1.22, Figure 372, SANACT
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(
    bits = "bits.0",
    ctx = "endian: Endian, bits: BitSize",
    endian = "endian",
    id_type = "u8"
)]
#[repr(u8)]
enum SanitizeAction {
    #[default]
    Reserved = 0x00,
    ExitFailureMode = 0x01,
    StartBlockErase = 0x02,
    StartOverwrite = 0x03,
    StartCryptoErase = 0x04,
    ExitMediaVerificationState = 0x05,
}

impl TryFrom<u32> for SanitizeAction {
    type Error = ();

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            0x00 => Ok(Self::Reserved),
            0x01 => Ok(Self::ExitFailureMode),
            0x02 => Ok(Self::StartBlockErase),
            0x03 => Ok(Self::StartOverwrite),
            0x04 => Ok(Self::StartCryptoErase),
            0x05 => Ok(Self::ExitMediaVerificationState),
            _ => Err(()),
        }
    }
}

// Base v2.1, 5.1.22, Figure 372
#[derive(Clone, Copy, Debug, Default, DekuRead, DekuWrite, Eq, PartialEq)]
#[deku(ctx = "endian: Endian", endian = "endian")]
pub struct AdminSanitizeConfiguration {
    #[deku(bits = "4")]
    owpass: u8,
    #[deku(bits = "1")]
    ause: bool,
    #[deku(bits = "3")]
    sanact: SanitizeAction,
    #[deku(bits = "1", pad_bits_before = "5")]
    emvs: bool,
    #[deku(bits = "1")]
    ndas: bool,
    #[deku(bits = "1", pad_bytes_after = "2")]
    oipbp: bool,
}

// Base v2.1, 5.1.25, Figure 385
// Base v2.1, 3.1.3.6, Figure 32
#[derive(Debug, DekuRead, DekuWrite)]
#[deku(ctx = "endian: Endian", endian = "endian", id_type = "u8")]
#[repr(u8)]
pub enum FeatureIdentifiers {
    KeepAliveTimer = 0x0f,
}