nvme/ds/identify/
ns_desc_list.rs

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
use modular_bitfield::prelude::*;

pub struct NamespaceIdentBuffer {
    data: [u8; 4096],
}

#[bitfield(bits = 32)]
struct NamespaceIdentDescriptorHead {
    #[skip(setters)]
    ident_type: u8,
    #[skip(setters)]
    ident_len: u8,
    #[skip]
    res: u16,
}

pub enum NamespaceDescriptor<'a> {
    IeeeExtended(&'a [u8]),
    NamespaceGUID(&'a [u8]),
    NamespaceUUID(&'a [u8]),
    CommandSetId(&'a [u8]),
}

pub struct NamespaceIdentBufferIterator<'a> {
    buf: &'a NamespaceIdentBuffer,
    pos: usize,
}

impl NamespaceIdentBuffer {
    pub fn iter(&self) -> NamespaceIdentBufferIterator<'_> {
        NamespaceIdentBufferIterator { buf: self, pos: 0 }
    }
}

impl<'a> Iterator for NamespaceIdentBufferIterator<'a> {
    type Item = NamespaceDescriptor<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= 4096 {
            return None;
        }
        let head =
            NamespaceIdentDescriptorHead::from_bytes(self.buf.data[0..4].try_into().unwrap());
        let desc_len = head.ident_len() as usize + 4;
        let loc = self.pos;
        self.pos += desc_len;
        if self.pos > 4096 {
            return None;
        }

        let slice = &self.buf.data[(loc + 4)..(loc + 4 + head.ident_len() as usize)];

        Some(match head.ident_type() {
            1 => NamespaceDescriptor::IeeeExtended(slice),
            2 => NamespaceDescriptor::NamespaceGUID(slice),
            3 => NamespaceDescriptor::NamespaceUUID(slice),
            4 => NamespaceDescriptor::CommandSetId(slice),
            _ => return self.next(),
        })
    }
}