pcics 0.3.2

PCI configuration space access library
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
/*!
# PCI Express Root Complex Link Declaration`

The PCI Express Root Complex Link Declaration Capability is an optional Capability that is
permitted to be implemented by Root Ports, RCiEPs, or RCRBs to declare a Root Complex’s
internal topology.

## Struct diagram
[RootComplexLinkDeclaration]
- [ElementSelfDescription]
  - [ElementType]
- [LinkEntries]: [LinkEntry0](LinkEntry) .. [LinkEntryN](LinkEntry)

## Examples
> ```text
> Desc:  PortNumber=03 ComponentID=01 EltType=Config
> Link0: Desc: TargetPort=00 TargetComponent=01
>              AssocRCRB- LinkType=MemMapped LinkValid+
>        Addr: 00000000fed19000
> ```

```rust
# use pcics::extended_capabilities::root_complex_link_declaration::*;
let data = [
    0x05, 0x00, 0x01, 0x1c, // Extended Capability Header
    0x00, 0x01, 0x01, 0x03, // Element Self Description
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Reserved
    0x01, 0x00, 0x01, 0x00, // Link Entry 1 Description
    0x00, 0x00, 0x00, 0x00, // Link Entry 1 Reserved
    0x00, 0x90, 0xd1, 0xfe, // Link Entry 1 Address First DWORD
    0x00, 0x00, 0x00, 0x00, // Link Entry 1 Address Second DWORD
];
let mut rcld_result: RootComplexLinkDeclaration = data[4..].try_into().unwrap();
let rcld_sample = RootComplexLinkDeclaration {
    element_self_description: ElementSelfDescription {
        port_number: 3,
        component_id: 1,
        element_type: ElementType::ConfigurationSpaceElement,
        reserved: 0,
        number_of_link_entries: 1,
    },
    link_entries: LinkEntries::new(&data[0x10..0x20], 1),
};

let le_result = rcld_result.link_entries.next().unwrap();
let le_sample = LinkEntry {
    link_description: LinkDescription {
        target_port_number: 0,
        target_component_id: 1,
        associate_rcrb_header: false,
        link_type: 0,
        link_valid: true,
    },
    link_address: LinkAddress::MemoryMappedSpace(0xfed19000),
};
assert_eq!(le_sample, le_result);
```
*/

use heterob::{
    endianness::{Le, LeBytesTryInto},
    Seq, P2,
};

use super::ExtendedCapabilityDataError;

/// The Serial Number register is a 64-bit field that contains the IEEE defined 64-bit extended
/// unique identifier (EUI-64™).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceSerialNumber {
    /// PCI Express Device Serial Number (1st DW)
    pub lower_dword: u32,
    /// PCI Express Device Serial Number (2nd DW)
    pub upper_dword: u32,
}
impl TryFrom<&[u8]> for DeviceSerialNumber {
    type Error = ExtendedCapabilityDataError;

    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
        let Seq {
            head: Le((lower_dword, upper_dword)),
            ..
        } = P2(slice)
            .try_into()
            .map_err(|_| ExtendedCapabilityDataError {
                name: "Device Serial Number",
                size: 8,
            })?;
        Ok(Self {
            lower_dword,
            upper_dword,
        })
    }
}

use core::slice::Chunks;

use heterob::{bit_numbering::Lsb, endianness::FromLeBytes, P4, P5, P6};
use snafu::prelude::*;

/// Root Complex Link Declaration Error
#[derive(Snafu, Debug, Clone, PartialEq, Eq)]
pub enum RootComplexLinkDeclarationError {
    #[snafu(display("can't read Element Self Description (4 bytes) from slice"))]
    ElementSelfDescription,
    #[snafu(display("number of link entries must be > 1"))]
    NumberOfLinkEntries {
        element_self_description: ElementSelfDescription,
    },
    #[snafu(display("reserved space should be readable"))]
    ReservedSpace {
        element_self_description: ElementSelfDescription,
    },
    #[snafu(display("there must be at least one LinkEntry"))]
    LinkEntry1 {
        element_self_description: ElementSelfDescription,
    },
}

/// Root Complex Link Declaration
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RootComplexLinkDeclaration<'a> {
    pub element_self_description: ElementSelfDescription,
    pub link_entries: LinkEntries<'a>,
}
impl<'a> TryFrom<&'a [u8]> for RootComplexLinkDeclaration<'a> {
    type Error = RootComplexLinkDeclarationError;
    fn try_from(slice: &'a [u8]) -> Result<Self, Self::Error> {
        let Seq {
            head: element_self_description,
            tail: slice,
        } = slice
            .le_bytes_try_into()
            .map_err(|_| RootComplexLinkDeclarationError::ElementSelfDescription)?;
        let element_self_description @ ElementSelfDescription {
            number_of_link_entries,
            ..
        } = From::<u32>::from(element_self_description);
        if number_of_link_entries < 1 {
            return Err(RootComplexLinkDeclarationError::NumberOfLinkEntries {
                element_self_description,
            });
        }
        let Seq {
            head: _reserved,
            tail: slice,
        } = slice.le_bytes_try_into().map_err(|_| {
            RootComplexLinkDeclarationError::ReservedSpace {
                element_self_description: element_self_description.clone(),
            }
        })?;
        let _: [u8; 8] = _reserved;
        if slice.len() < LinkEntry::SIZE {
            return Err(RootComplexLinkDeclarationError::LinkEntry1 {
                element_self_description,
            });
        }
        let link_entries = LinkEntries::new(slice, number_of_link_entries);

        Ok(Self {
            element_self_description,
            link_entries,
        })
    }
}

/// Provides information about the Root Complex element containing the Root
/// Complex Link Declaration Capability
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ElementSelfDescription {
    /// Element Type
    pub element_type: ElementType,
    /// Reserved
    pub reserved: u8,
    /// Number of Link Entries
    pub number_of_link_entries: u8,
    /// Component ID
    pub component_id: u8,
    /// Port Number
    pub port_number: u8,
}
impl ElementSelfDescription {
    pub const BYTES: usize = 4;
}
impl From<u32> for ElementSelfDescription {
    fn from(dword: u32) -> Self {
        let Lsb((element_type, reserved, number_of_link_entries, component_id, port_number)) =
            P5::<_, 4, 4, 8, 8, 8>(dword).into();
        let element_type = From::<u8>::from(element_type);
        Self {
            element_type,
            reserved,
            number_of_link_entries,
            component_id,
            port_number,
        }
    }
}
impl FromLeBytes<4> for ElementSelfDescription {
    fn from_le_bytes(bytes: [u8; 4]) -> Self {
        u32::from_le_bytes(bytes).into()
    }
}

/// Indicates the type of the Root Complex Element
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ElementType {
    /// Configuration Space Element
    ConfigurationSpaceElement,
    /// System Egress Port or internal sink (memory)
    SystemEgressPortOrInternalSink,
    /// Internal Root Complex Link
    InternalRootComplexLink,
    /// Reserved
    Reserved(u8),
}
impl From<u8> for ElementType {
    fn from(byte: u8) -> Self {
        match byte {
            0x00 => Self::ConfigurationSpaceElement,
            0x01 => Self::SystemEgressPortOrInternalSink,
            0x02 => Self::InternalRootComplexLink,
            v => Self::Reserved(v),
        }
    }
}
impl From<ElementType> for u8 {
    fn from(et: ElementType) -> Self {
        match et {
            ElementType::ConfigurationSpaceElement => 0x00,
            ElementType::SystemEgressPortOrInternalSink => 0x01,
            ElementType::InternalRootComplexLink => 0x02,
            ElementType::Reserved(v) => v,
        }
    }
}

/// Link Entries
#[derive(Debug, Clone)]
pub struct LinkEntries<'a> {
    chunks: Chunks<'a, u8>,
    pub state: LinkEntriesState,
}
impl<'a> LinkEntries<'a> {
    pub const FIRST_ENTRY_OFFSET: usize = 0x10 - super::ECH_BYTES;
    pub fn new(slice: &'a [u8], number_of_link_entries: u8) -> Self {
        let length = (number_of_link_entries as usize) * LinkEntry::SIZE;
        Self {
            chunks: slice[..slice.len().min(length)].chunks(LinkEntry::SIZE),
            state: if slice.len() >= length {
                LinkEntriesState::Valid
            } else if slice.len() % LinkEntry::SIZE == 0 {
                LinkEntriesState::Incomplete
            } else {
                LinkEntriesState::Invalid
            },
        }
    }
}

impl<'a> PartialEq for LinkEntries<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.clone().eq(other.clone())
    }
}
impl<'a> Eq for LinkEntries<'a> {}
impl<'a> Iterator for LinkEntries<'a> {
    type Item = LinkEntry;

    fn next(&mut self) -> Option<Self::Item> {
        let slice = self.chunks.next()?;
        let Seq {
            head: Le((link_description, _reserved, addr_low, addr_high)),
            ..
        } = P4(slice).try_into().ok()?;
        let link_description @ LinkDescription { link_type, .. } =
            From::<u32>::from(link_description);
        let _: [u8; 4] = _reserved;
        Some(LinkEntry {
            link_description,
            link_address: LinkAddress::new(link_type, addr_low, addr_high),
        })
    }
}

/// Link Entries State depends on bytes number
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkEntriesState {
    /// Number of entries equal to [ElementSelfDescription::number_of_link_entries]
    Valid,
    /// Number of entries is less than [ElementSelfDescription::number_of_link_entries]
    Incomplete,
    /// Link entries bytes number is not aligned to [LinkEntry] size
    Invalid,
}

/// Declares an internal Link to another Root Complex Element
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkEntry {
    /// Link Description
    pub link_description: LinkDescription,
    /// Link Address
    pub link_address: LinkAddress,
}
impl LinkEntry {
    pub const DWORDS: usize = 4;
    pub const SIZE: usize = 16;
}

/// Link Description
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkDescription {
    /// Link Valid
    pub link_valid: bool,
    /// Link Type
    pub link_type: u8,
    /// Associate RCRB Header
    pub associate_rcrb_header: bool,
    /// Target Component ID
    pub target_component_id: u8,
    /// Target Port Number
    pub target_port_number: u8,
}
impl From<u32> for LinkDescription {
    fn from(dword: u32) -> Self {
        let Lsb((
            link_valid,
            link_type,
            associate_rcrb_header,
            (),
            target_component_id,
            target_port_number,
        )) = P6::<_, 1, 1, 1, 13, 8, 8>(dword).into();
        Self {
            link_valid,
            link_type,
            associate_rcrb_header,
            target_component_id,
            target_port_number,
        }
    }
}
impl FromLeBytes<4> for LinkDescription {
    fn from_le_bytes(bytes: [u8; 4]) -> Self {
        u32::from_le_bytes(bytes).into()
    }
}

/// Identifies the target element for the Link entry
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkAddress {
    /// Pointing to a memory-mapped RCRB
    MemoryMappedSpace(u64),
    /// Pointing to the Configuration Space of a Root Complex element
    ConfigurationSpace {
        /// Encoded number of Bus Number bits
        n: u8,
        /// Reserved
        reserved: u16,
        /// Function Number
        function: u8,
        /// Device Number
        device: u8,
        /// Bus Number
        bus: u8,
        /// PCI Express Configuration Space Base Address
        address: u64,
    },
}
impl LinkAddress {
    const DWORDS: usize = 2;
}
impl LinkAddress {
    #[must_use]
    pub fn new(link_type: u8, first_dword: u32, second_dword: u32) -> Self {
        const BUS_AND_ADDR_OFFSET: u8 = 20;
        let address = (second_dword as u64) << 32 | first_dword as u64;
        if link_type == 0 {
            Self::MemoryMappedSpace(address)
        } else {
            let Lsb((n, reserved, function, device)) = P4::<_, 3, 9, 3, 5>(first_dword).into();
            // with N = 000b specifying n = 8 and all other encodings specifying n = <value of N>
            let n: u8 = if n == 0 { 8 } else { n };
            // Bits (19 + n):20 specify the Bus Number, with 1 ≤ n ≤ 8
            let bus = ((first_dword >> BUS_AND_ADDR_OFFSET) & !(u32::MAX << n)) as u8;
            let laddr_mask = u64::MAX << (BUS_AND_ADDR_OFFSET + n);
            // Bits 31:(20 + n) of the first DWORD together with the second DWORD
            let address = address & laddr_mask;
            Self::ConfigurationSpace {
                n,
                reserved,
                function,
                device,
                bus,
                address,
            }
        }
    }
}
impl From<LinkAddress> for [u32; LinkAddress::DWORDS] {
    fn from(la: LinkAddress) -> Self {
        match la {
            LinkAddress::MemoryMappedSpace(qword) => [qword as u32, (qword >> 32) as u32],
            LinkAddress::ConfigurationSpace {
                n,
                reserved,
                function,
                device,
                bus,
                address,
            } => [
                address as u32
                    | (bus as u32) << 20
                    | (device as u32) << 15
                    | (function as u32) << 12
                    | (reserved as u32) << 3
                    | (if n == 8 { 0 } else { n }) as u32,
                (address >> 32) as u32,
            ],
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use std::prelude::v1::*;

    #[test]
    fn link_description() {
        let result: LinkDescription = 0b10101010_10101010_0000000000000_1_0_1.into();
        let sample = LinkDescription {
            link_valid: true,
            link_type: 0,
            associate_rcrb_header: true,
            target_component_id: 0xAA,
            target_port_number: 0xAA,
        };
        assert_eq!(sample, result);
    }

    #[test]
    fn link_address_memory_mapped_space() {
        let data = [0b1111_1111_1111_1111_1111_0000_0000_0000u32, 0x33221100];
        let result = LinkAddress::new(0, data[0], data[1]);
        let sample = LinkAddress::MemoryMappedSpace(0x33221100FFFFF000);
        assert_eq!(sample, result);
    }

    #[test]
    fn link_address_configuration_space() {
        //           address    bus  dev   fn  reserved  N
        let data = [0b11111111_0101_01010_101_000000000_100u32, 0x33221100];
        let result = LinkAddress::new(1, data[0], data[1]);
        let sample = LinkAddress::ConfigurationSpace {
            n: 4,
            reserved: 0,
            function: 0b101,
            device: 0b01010,
            bus: 0b0101,
            address: 0x33221100FF000000,
        };
        assert_eq!(sample, result);
    }

    #[test]
    fn link_address_into() {
        let data = [
            [0x00, 0x00],
            [0xaa, 0xaa],
            [0x55, 0x55],
            [0x0f, 0x0f],
            [0xf0, 0xf0],
            [0xff, 0xff],
        ];
        for sample in data {
            let la = LinkAddress::new(1, sample[0], sample[1]);
            let result: [u32; 2] = la.clone().into();
            assert_eq!(sample, result, "{:04x?}", (sample, la));
        }
    }

    #[test]
    fn link_entries() {
        // Link0:  Desc:   TargetPort=00 TargetComponent=01 AssocRCRB- LinkType=MemMapped LinkValid+
        //         Addr:   00000000fed19000
        let data = [
            0x01, 0x00, 0x01, 0x00, // Link entry 1 description
            0x00, 0x00, 0x00, 0x00, // Link entry 1 reserved
            0x00, 0x90, 0xd1, 0xfe, 0x00, 0x00, 0x00, 0x00, // Link entry 1 address
        ];
        let le = LinkEntries::new(data.as_slice(), 1);

        assert_eq!(LinkEntriesState::Valid, le.state, "State");

        let result = le.collect::<Vec<_>>();

        let sample = vec![LinkEntry {
            link_description: LinkDescription {
                link_valid: true,
                link_type: 0,
                associate_rcrb_header: false,
                target_component_id: 0x01,
                target_port_number: 0x00,
            },
            link_address: LinkAddress::MemoryMappedSpace(0x00000000fed19000),
        }];
        assert_eq!(sample, result, "Entry");
    }

    #[test]
    fn parse_full_struct() {
        // Desc:   PortNumber=02 ComponentID=01 EltType=Config
        // Link0:  Desc:   TargetPort=00 TargetComponent=01 AssocRCRB- LinkType=MemMapped LinkValid+
        //         Addr:   00000000fed19000
        let data = [
            0x05, 0x00, 0x01,
            0x1c, // Root Complex Link Declaration Extended Capability Header
            0x00, 0x01, 0x01, 0x02, // Element self description
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
            0x01, 0x00, 0x01, 0x00, // Link entry 1 description
            0x00, 0x00, 0x00, 0x00, // Link entry 1 reserved
            0x00, 0x90, 0xd1, 0xfe, 0x00, 0x00, 0x00, 0x00, // Link entry 1 address
        ];
        let result: RootComplexLinkDeclaration = data[4..].try_into().unwrap();

        let sample = RootComplexLinkDeclaration {
            element_self_description: ElementSelfDescription {
                element_type: ElementType::ConfigurationSpaceElement,
                reserved: 0,
                number_of_link_entries: 1,
                component_id: 0x01,
                port_number: 0x02,
            },
            link_entries: LinkEntries::new(&data[0x10..], 1),
        };
        assert_eq!(sample, result);
    }
}