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
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
//! HyperTransport Link Capability
//!
//! This Capability structure provides control and status for devices that implement HyperTransport
//! Technology links. For details, refer to the HyperTransport I/O Link Specification
//!
//! Capability types:
//! - [x] [Slave or Primary Interface](SlaveOrPrimaryInterface)
//! - [x] [Host or Secondary Interface](HostOrSecondaryInterface)
//! - [ ] Switch
//! - [ ] Reserved-Host
//! - [ ] Interrupt Discovery and Configuration
//! - [x] [Revision ID](RevisionId)
//! - [ ] UnitID Clumping
//! - [ ] Extended Configuration Space Access
//! - [ ] Address Mapping
//! - [x] [MSI Mapping](MsiMapping)
//! - [ ] DirectRoute
//! - [ ] VCSet
//! - [ ] Retry Mode
//! - [ ] X86 Encoding (Reserved)
//! - [ ] Gen3
//! - [ ] Function-Level Extension
//! - [ ] Power Management
//! - [ ] High Node Count

use heterob::{bit_numbering::Lsb, endianness::Le, P10, P11, P13, P16, P17, P2, P3, P5, P6, P8};
use snafu::Snafu;

/// HyperTransport errors
#[derive(Snafu, Debug, Clone, PartialEq, Eq)]
pub enum HypertransportError {
    CapabilityType,
    SlaveOrPrimaryInterface,
    HostOrSecondaryInterface,
    Switch,
    ReservedHost,
    InterruptDiscoveryAndConfiguration,
    RevisionId,
    UnitIdClumping,
    ExtendedConfigurationSpaceAccess,
    AddressMapping,
    MsiMapping,
    DirectRoute,
    VCSet,
    X86Encoding,
    Gen3,
    FunctionLevelExtension,
    PowerManagement,
    HighNodeCount,
}

/// The layout of the capabilities block is determined by the value in the Capability Type field in
/// the Command register
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Hypertransport {
    /// Slave or Primary Interface
    SlaveOrPrimaryInterface(SlaveOrPrimaryInterface),
    /// Host or Secondary Interface
    HostOrSecondaryInterface(HostOrSecondaryInterface),
    /// Switch
    Switch(Switch),
    /// Reserved-Host
    ReservedHost(ReservedHost),
    /// Interrupt Discovery and Configuration
    InterruptDiscoveryAndConfiguration(InterruptDiscoveryAndConfiguration),
    /// Revision ID
    RevisionId(RevisionId),
    /// UnitID Clumping
    UnitIdClumping(UnitIdClumping),
    /// Extended Configuration Space Access
    ExtendedConfigurationSpaceAccess(ExtendedConfigurationSpaceAccess),
    /// Address Mapping
    AddressMapping(AddressMapping),
    /// MSI Mapping
    MsiMapping(MsiMapping),
    /// DirectRoute
    DirectRoute(DirectRoute),
    /// VCSet
    VCSet(VCSet),
    /// Retry Mode
    RetryMode(RetryMode),
    /// X86 Encoding (Reserved)
    X86Encoding(X86Encoding),
    /// Gen3
    Gen3(Gen3),
    /// Function-Level Extension
    FunctionLevelExtension(FunctionLevelExtension),
    /// Power Management
    PowerManagement(PowerManagement),
    /// High Node Count
    HighNodeCount(HighNodeCount),
    /// Reserved
    Reserved(u8),
}
impl<'a> TryFrom<&'a [u8]> for Hypertransport {
    type Error = HypertransportError;

    fn try_from(slice: &'a [u8]) -> Result<Self, Self::Error> {
        let cmd = slice
            .get(..2)
            .and_then(|slice| <[u8; 2]>::try_from(slice).ok())
            .map(u16::from_le_bytes)
            .ok_or(HypertransportError::CapabilityType)?;
        // For the primary and secondary interface capability blocks,
        // there are 3 bits (15:13) used in the encoding.
        // For all other HyperTransport capability blocks, 5 bits (15:11) are used.
        let Lsb(((), capability_type)) = P2::<u16, 11, 5>(cmd).into();
        let _: u8 = capability_type;
        Ok(match capability_type {
            0b00000..=0b00011 => slice
                .get(..SlaveOrPrimaryInterface::SIZE)
                .and_then(|slice| <[u8; SlaveOrPrimaryInterface::SIZE]>::try_from(slice).ok())
                .map(|data| Self::SlaveOrPrimaryInterface(data.into()))
                .ok_or(HypertransportError::SlaveOrPrimaryInterface)?,
            0b00100..=0b00111 => slice
                .get(..HostOrSecondaryInterface::SIZE)
                .and_then(|slice| <[u8; HostOrSecondaryInterface::SIZE]>::try_from(slice).ok())
                .map(|data| Self::HostOrSecondaryInterface(data.into()))
                .ok_or(HypertransportError::HostOrSecondaryInterface)?,
            0b01000 => Self::Switch(Switch {}),
            0b01001 => Self::ReservedHost(ReservedHost {}),
            0b10000 => {
                Self::InterruptDiscoveryAndConfiguration(InterruptDiscoveryAndConfiguration {})
            }
            0b10001 => slice
                .first()
                .map(|&data| Self::RevisionId(data.into()))
                .ok_or(HypertransportError::RevisionId)?,
            0b10010 => Self::UnitIdClumping(UnitIdClumping {}),
            0b10011 => Self::ExtendedConfigurationSpaceAccess(ExtendedConfigurationSpaceAccess {}),
            0b10100 => Self::AddressMapping(AddressMapping {}),
            0b10101 => slice
                .get(..MsiMapping::SIZE)
                .and_then(|slice| <[u8; MsiMapping::SIZE]>::try_from(slice).ok())
                .map(|data| Self::MsiMapping(data.into()))
                .ok_or(HypertransportError::MsiMapping)?,
            0b10110 => Self::DirectRoute(DirectRoute {}),
            0b10111 => Self::VCSet(VCSet {}),
            0b11000 => Self::RetryMode(RetryMode {}),
            0b11001 => Self::X86Encoding(X86Encoding {}),
            0b11010 => Self::Gen3(Gen3 {}),
            0b11011 => Self::FunctionLevelExtension(FunctionLevelExtension {}),
            0b11100 => Self::PowerManagement(PowerManagement {}),
            0b11101 => Self::HighNodeCount(HighNodeCount {}),
            v => Self::Reserved(v as u8),
        })
    }
}

impl<'a> From<&'a Hypertransport> for u8 {
    fn from(ht: &'a Hypertransport) -> Self {
        match ht {
            Hypertransport::SlaveOrPrimaryInterface(_) => 0b00000,
            Hypertransport::HostOrSecondaryInterface(_) => 0b00100,
            Hypertransport::Switch(_) => 0b01000,
            Hypertransport::ReservedHost(_) => 0b01001,
            Hypertransport::InterruptDiscoveryAndConfiguration(_) => 0b10000,
            Hypertransport::RevisionId(_) => 0b10001,
            Hypertransport::UnitIdClumping(_) => 0b10010,
            Hypertransport::ExtendedConfigurationSpaceAccess(_) => 0b10011,
            Hypertransport::AddressMapping(_) => 0b10100,
            Hypertransport::MsiMapping(_) => 0b10101,
            Hypertransport::DirectRoute(_) => 0b10110,
            Hypertransport::VCSet(_) => 0b10111,
            Hypertransport::RetryMode(_) => 0b11000,
            Hypertransport::X86Encoding(_) => 0b11001,
            Hypertransport::Gen3(_) => 0b11010,
            Hypertransport::FunctionLevelExtension(_) => 0b11011,
            Hypertransport::PowerManagement(_) => 0b11100,
            Hypertransport::HighNodeCount(_) => 0b11101,
            Hypertransport::Reserved(v) => *v,
        }
    }
}

/// Slave/Primary Interface
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlaveOrPrimaryInterface {
    /// Command
    pub command: SlaveOrPrimaryCommand,
    /// Link Control 0
    pub link_control_0: LinkControl,
    /// Link Config 0
    pub link_config_0: LinkConfiguration,
    /// Link Control 1
    pub link_control_1: LinkControl,
    /// Link Config 1
    pub link_config_1: LinkConfiguration,
    /// Revision ID
    pub revision_id: RevisionId,
    /// Link Freq 0
    pub link_freq_0: u8,
    /// Link Error 0
    pub link_error_0: LinkError,
    /// LinkFreqCap0
    pub link_freq_cap_0: LinkFrequencyCapability,
    /// Feature
    pub feature: FeatureCapability,
    /// Link Freq 1
    pub link_freq_1: u8,
    /// Link Error 1
    pub link_error_1: LinkError,
    /// LinkFreqCap1
    pub link_freq_cap_1: LinkFrequencyCapability,
    /// Enumeration Scratchpad
    pub enumeration_scratchpad: u16,
    /// Error Handling
    pub error_handling: ErrorHandling,
    /// Mem Base Upper
    pub mem_base_upper: u8,
    /// Mem Limit Upper
    pub mem_limit_upper: u8,
    /// Bus Number
    pub bus_number: u8,
}
impl SlaveOrPrimaryInterface {
    pub const SIZE: usize = 26;
    pub fn link_freq_0(&self, link_freq_ext: bool) -> LinkFrequency {
        LinkFrequency::new(link_freq_ext, self.link_freq_0)
    }
    pub fn link_freq_1(&self, link_freq_ext: bool) -> LinkFrequency {
        LinkFrequency::new(link_freq_ext, self.link_freq_1)
    }
}
impl From<[u8; Self::SIZE]> for SlaveOrPrimaryInterface {
    fn from(bytes: [u8; Self::SIZE]) -> Self {
        let Le((
            command,
            link_control_0,
            link_config_0,
            link_control_1,
            link_config_1,
            revision_id,
            link_freq_err_0,
            link_freq_cap_0,
            feature,
            link_freq_err_1,
            link_freq_cap_1,
            enumeration_scratchpad,
            error_handling,
            mem_base_upper,
            mem_limit_upper,
            bus_number,
            rsvd,
        )) = P17(bytes).into();
        let _: (u8, u8) = (feature, rsvd);
        let Lsb((link_freq_0, protocol_error, overflow_error, end_of_chain_error, ctl_timeout)) =
            P5::<u8, 4, 1, 1, 1, 1>(link_freq_err_0).into();
        let link_error_0 = LinkError {
            protocol_error,
            overflow_error,
            end_of_chain_error,
            ctl_timeout,
        };
        let Lsb((link_freq_1, protocol_error, overflow_error, end_of_chain_error, ctl_timeout)) =
            P5::<u8, 4, 1, 1, 1, 1>(link_freq_err_1).into();
        let link_error_1 = LinkError {
            protocol_error,
            overflow_error,
            end_of_chain_error,
            ctl_timeout,
        };
        Self {
            command: From::<u16>::from(command),
            link_control_0: From::<u16>::from(link_control_0),
            link_config_0: From::<u16>::from(link_config_0),
            link_control_1: From::<u16>::from(link_control_1),
            link_config_1: From::<u16>::from(link_config_1),
            revision_id: From::<u8>::from(revision_id),
            link_freq_0,
            link_error_0,
            link_freq_cap_0: From::<u16>::from(link_freq_cap_0),
            feature: (feature as u16).into(),
            link_freq_1,
            link_error_1,
            link_freq_cap_1: From::<u16>::from(link_freq_cap_1),
            enumeration_scratchpad,
            error_handling: From::<u16>::from(error_handling),
            mem_base_upper,
            mem_limit_upper,
            bus_number,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlaveOrPrimaryCommand {
    /// Base UnitID
    pub base_unitid: u8,
    /// Unit Count
    pub unit_count: u8,
    /// Master Host
    pub master_host: bool,
    /// Default Direction
    pub default_direction: bool,
    /// Drop on Uninitialized Link
    pub drop_on_uninitialized_link: bool,
}

impl From<u16> for SlaveOrPrimaryCommand {
    fn from(word: u16) -> Self {
        let Lsb((
            base_unitid,
            unit_count,
            master_host,
            default_direction,
            drop_on_uninitialized_link,
            (),
        )) = P6::<_, 5, 5, 1, 1, 1, 3>(word).into();
        Self {
            base_unitid,
            unit_count,
            master_host,
            default_direction,
            drop_on_uninitialized_link,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkControl {
    /// Source ID Enable
    pub source_id_enable: bool,
    /// CRC Flood Enable
    pub crc_flood_enable: bool,
    /// CRC Start Test
    pub crc_start_test: bool,
    /// CRC Force Error
    pub crc_force_error: bool,
    /// Link Failure
    pub link_failure: bool,
    /// Initialization Complete
    pub initialization_complete: bool,
    /// End of Chain
    pub end_of_chain: bool,
    /// Transmitter Off
    pub transmitter_off: bool,
    /// CRC Error
    pub crc_error: u8,
    /// Isochronous Flow Control Enable
    pub isochronous_flow_control_enable: bool,
    /// LDTSTOP# Tristate Enable
    pub ldtstop_tristate_enable: bool,
    /// Extended CTL Time
    pub extended_ctl_time: bool,
    /// 64 Bit Addressing Enable
    pub enable_64_bit_addressing: bool,
}

impl From<u16> for LinkControl {
    fn from(word: u16) -> Self {
        let Lsb((
            source_id_enable,
            crc_flood_enable,
            crc_start_test,
            crc_force_error,
            link_failure,
            initialization_complete,
            end_of_chain,
            transmitter_off,
            crc_error,
            isochronous_flow_control_enable,
            ldtstop_tristate_enable,
            extended_ctl_time,
            enable_64_bit_addressing,
        )) = P13::<_, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1>(word).into();
        Self {
            source_id_enable,
            crc_flood_enable,
            crc_start_test,
            crc_force_error,
            link_failure,
            initialization_complete,
            end_of_chain,
            transmitter_off,
            crc_error,
            isochronous_flow_control_enable,
            ldtstop_tristate_enable,
            extended_ctl_time,
            enable_64_bit_addressing,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkConfiguration {
    /// Max Link Width In
    pub max_link_width_in: LinkWidth,
    /// Doubleword Flow Control In
    pub doubleword_flow_control_in: bool,
    /// Max Link Width Out
    pub max_link_width_out: LinkWidth,
    /// Doubleword Flow Control Out
    pub doubleword_flow_control_out: bool,
    /// Link Width In
    pub link_width_in: LinkWidth,
    /// Doubleword Flow Control In Enable
    pub doubleword_flow_control_in_enable: bool,
    /// Link Width Out
    pub link_width_out: LinkWidth,
    /// Doubleword Flow Control Out Enable
    pub doubleword_flow_control_out_enable: bool,
}

impl From<u16> for LinkConfiguration {
    fn from(word: u16) -> Self {
        let Lsb((
            max_link_width_in,
            doubleword_flow_control_in,
            max_link_width_out,
            doubleword_flow_control_out,
            link_width_in,
            doubleword_flow_control_in_enable,
            link_width_out,
            doubleword_flow_control_out_enable,
        )) = P8::<_, 3, 1, 3, 1, 3, 1, 3, 1>(word).into();
        Self {
            max_link_width_in: From::<u8>::from(max_link_width_in),
            doubleword_flow_control_in,
            max_link_width_out: From::<u8>::from(max_link_width_out),
            doubleword_flow_control_out,
            link_width_in: From::<u8>::from(link_width_in),
            doubleword_flow_control_in_enable,
            link_width_out: From::<u8>::from(link_width_out),
            doubleword_flow_control_out_enable,
        }
    }
}

/// Indicate the physical width of the incoming side of the HyperTransport link implemented by this
/// device. Unganged links indicate a maximum width of 8 bits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkWidth {
    /// 8 bits
    Width8bits,
    /// 16 bits
    Width16bits,
    /// 32 bits
    Width32bits,
    /// 2 bits
    Width2bits,
    /// 4 bits
    Width4bits,
    /// Link physically not connected
    NotConnected,
    /// Reserved
    Reserved(u8),
}
impl From<u8> for LinkWidth {
    fn from(byte: u8) -> Self {
        match byte {
            0b000 => Self::Width8bits,
            0b001 => Self::Width16bits,
            0b011 => Self::Width32bits,
            0b100 => Self::Width2bits,
            0b101 => Self::Width4bits,
            0b111 => Self::NotConnected,
            v => Self::Reserved(v),
        }
    }
}
impl Default for LinkWidth {
    fn default() -> Self {
        Self::Width8bits
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RevisionId {
    pub minor: u8,
    pub major: u8,
}
impl RevisionId {
    pub const SIZE: usize = 1;
}
impl From<u8> for RevisionId {
    fn from(byte: u8) -> Self {
        let Lsb((minor, major)) = P2::<_, 5, 3>(byte).into();
        Self { minor, major }
    }
}
impl<'a> From<&'a RevisionId> for u8 {
    fn from(data: &'a RevisionId) -> Self {
        (data.major << 5) | (data.minor & 0b11111)
    }
}

/// The Link Frequency register specifies the operating frequency of the link’s transmitter
/// clock—the data rate is twice this value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkFrequency {
    Rate200MHz,
    Rate300MHz,
    Rate400MHz,
    Rate500MHz,
    Rate600MHz,
    Rate800MHz,
    Rate1000MHz,
    Rate1200MHz,
    Rate1400MHz,
    Rate1600MHz,
    Rate1800MHz,
    Rate2000MHz,
    Rate2200MHz,
    Rate2400MHz,
    Rate2600MHz,
    VendorSpecific,
    Rate2800MHz,
    Rate3000MHz,
    Rate3200MHz,
    Reserved(u8),
}
impl LinkFrequency {
    /// The encoding of this field, combined with another bit from the Link Frequency Extension
    /// register defined in [Gen3] capability.
    pub fn new(link_freq_ext: bool, link_freq: u8) -> Self {
        match (link_freq_ext, link_freq) {
            (false, 0b0000) => Self::Rate200MHz,
            (false, 0b0001) => Self::Rate300MHz,
            (false, 0b0010) => Self::Rate400MHz,
            (false, 0b0011) => Self::Rate500MHz,
            (false, 0b0100) => Self::Rate600MHz,
            (false, 0b0101) => Self::Rate800MHz,
            (false, 0b0110) => Self::Rate1000MHz,
            (false, 0b0111) => Self::Rate1200MHz,
            (false, 0b1000) => Self::Rate1400MHz,
            (false, 0b1001) => Self::Rate1600MHz,
            (false, 0b1010) => Self::Rate1800MHz,
            (false, 0b1011) => Self::Rate2000MHz,
            (false, 0b1100) => Self::Rate2200MHz,
            (false, 0b1101) => Self::Rate2400MHz,
            (false, 0b1110) => Self::Rate2600MHz,
            (false, 0b1111) => Self::VendorSpecific,
            (true, 0b0001) => Self::Rate2800MHz,
            (true, 0b0010) => Self::Rate3000MHz,
            (true, 0b0011) => Self::Rate3200MHz,
            (b, v) => Self::Reserved((v & 0b1111) | ((b as u8) << 4)),
        }
    }
}
impl Default for LinkFrequency {
    fn default() -> Self {
        Self::Rate200MHz
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkError {
    /// Protocol Error
    pub protocol_error: bool,
    /// Overflow Error
    pub overflow_error: bool,
    /// End Of Chain Error
    pub end_of_chain_error: bool,
    /// CTL Timeout
    pub ctl_timeout: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkFrequencyCapability {
    pub supports_200mhz: bool,
    pub supports_300mhz: bool,
    pub supports_400mhz: bool,
    pub supports_500mhz: bool,
    pub supports_600mhz: bool,
    pub supports_800mhz: bool,
    pub supports_1000mhz: bool,
    pub supports_1200mhz: bool,
    pub supports_1400mhz: bool,
    pub supports_1600mhz: bool,
    pub supports_1800mhz: bool,
    pub supports_2000mhz: bool,
    pub supports_2200mhz: bool,
    pub supports_2400mhz: bool,
    pub supports_2600mhz: bool,
    pub supports_vendor_specific: bool,
}

impl From<u16> for LinkFrequencyCapability {
    fn from(word: u16) -> Self {
        let Lsb((
            supports_200mhz,
            supports_300mhz,
            supports_400mhz,
            supports_500mhz,
            supports_600mhz,
            supports_800mhz,
            supports_1000mhz,
            supports_1200mhz,
            supports_1400mhz,
            supports_1600mhz,
            supports_1800mhz,
            supports_2000mhz,
            supports_2200mhz,
            supports_2400mhz,
            supports_2600mhz,
            supports_vendor_specific,
        )) = P16::<_, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(word).into();
        Self {
            supports_200mhz,
            supports_300mhz,
            supports_400mhz,
            supports_500mhz,
            supports_600mhz,
            supports_800mhz,
            supports_1000mhz,
            supports_1200mhz,
            supports_1400mhz,
            supports_1600mhz,
            supports_1800mhz,
            supports_2000mhz,
            supports_2200mhz,
            supports_2400mhz,
            supports_2600mhz,
            supports_vendor_specific,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureCapability {
    /// Isochronous Flow Control Mode
    pub isochronous_flow_control_mode: bool,
    /// LDTSTOP#
    pub ldtstop: bool,
    /// CRC Test Mode
    pub crc_test_mode: bool,
    /// Extended CTL Time Required
    pub extended_ctl_time_required: bool,
    /// 64 Bit Addressing
    pub qword_addressing: bool,
    /// UnitID Reorder Disable
    pub unitid_reorder_disable: bool,
    /// Source Identification Extension
    pub source_identification_extension: bool,
    /// Extended Register Set
    pub extended_register_set: bool,
    /// Upstream Configuration Enable
    pub upstream_configuration_enable: bool,
}

impl From<u16> for FeatureCapability {
    fn from(word: u16) -> Self {
        let Lsb((
            isochronous_flow_control_mode,
            ldtstop,
            crc_test_mode,
            extended_ctl_time_required,
            qword_addressing,
            unitid_reorder_disable,
            source_identification_extension,
            (),
            extended_register_set,
            upstream_configuration_enable,
            (),
        )) = P11::<_, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6>(word).into();
        Self {
            isochronous_flow_control_mode,
            ldtstop,
            crc_test_mode,
            extended_ctl_time_required,
            qword_addressing,
            unitid_reorder_disable,
            source_identification_extension,
            extended_register_set,
            upstream_configuration_enable,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorHandling {
    /// Protocol Error Flood Enable
    pub protocol_error_flood_enable: bool,
    /// Overflow Error Flood Enable
    pub overflow_error_flood_enable: bool,
    /// Protocol Error Fatal Enable
    pub protocol_error_fatal_enable: bool,
    /// Overflow Error Fatal Enable
    pub overflow_error_fatal_enable: bool,
    /// End of Chain Error Fatal Enable
    pub end_of_chain_error_fatal_enable: bool,
    /// Response Error Fatal Enable
    pub response_error_fatal_enable: bool,
    /// CRC Error Fatal Enable
    pub crc_error_fatal_enable: bool,
    /// System Error Fatal Enable
    pub system_error_fatal_enable: bool,
    /// Chain Fail
    pub chain_fail: bool,
    /// Response Error
    pub response_error: bool,
    /// Protocol Error Nonfatal Enable
    pub protocol_error_nonfatal_enable: bool,
    /// Overflow Error Nonfatal Enable
    pub overflow_error_nonfatal_enable: bool,
    /// End of Chain Error Nonfatal Enable
    pub end_of_chain_error_nonfatal_enable: bool,
    /// Response Error Nonfatal Enable
    pub response_error_nonfatal_enable: bool,
    /// CRC Error Nonfatal Enable
    pub crc_error_nonfatal_enable: bool,
    /// System Error Nonfatal Enable
    pub system_error_nonfatal_enable: bool,
}

impl From<u16> for ErrorHandling {
    fn from(word: u16) -> Self {
        let Lsb((
            protocol_error_flood_enable,
            overflow_error_flood_enable,
            protocol_error_fatal_enable,
            overflow_error_fatal_enable,
            end_of_chain_error_fatal_enable,
            response_error_fatal_enable,
            crc_error_fatal_enable,
            system_error_fatal_enable,
            chain_fail,
            response_error,
            protocol_error_nonfatal_enable,
            overflow_error_nonfatal_enable,
            end_of_chain_error_nonfatal_enable,
            response_error_nonfatal_enable,
            crc_error_nonfatal_enable,
            system_error_nonfatal_enable,
        )) = P16::<_, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1>(word).into();
        Self {
            protocol_error_flood_enable,
            overflow_error_flood_enable,
            protocol_error_fatal_enable,
            overflow_error_fatal_enable,
            end_of_chain_error_fatal_enable,
            response_error_fatal_enable,
            crc_error_fatal_enable,
            system_error_fatal_enable,
            chain_fail,
            response_error,
            protocol_error_nonfatal_enable,
            overflow_error_nonfatal_enable,
            end_of_chain_error_nonfatal_enable,
            response_error_nonfatal_enable,
            crc_error_nonfatal_enable,
            system_error_nonfatal_enable,
        }
    }
}

/// Host/Secondary Interface
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostOrSecondaryInterface {
    /// Command
    pub command: HostOrSecondaryCommand,
    /// Link Control
    pub link_control: LinkControl,
    /// Link Config
    pub link_config: LinkConfiguration,
    /// Revision ID
    pub revision_id: RevisionId,
    /// Link Freq
    pub link_freq: u8,
    /// Link Error
    pub link_error: LinkError,
    /// LinkFreqCap
    pub link_freq_cap: LinkFrequencyCapability,
    /// Feature
    pub feature: FeatureCapability,
    /// Enumeration Scratchpad
    pub enumeration_scratchpad: u16,
    /// Error Handling
    pub error_handling: ErrorHandling,
    /// Mem Base Upper
    pub mem_base_upper: u8,
    /// Mem Limit Upper
    pub mem_limit_upper: u8,
}

impl HostOrSecondaryInterface {
    pub const SIZE: usize = 22;
    pub fn link_freq(&self, link_freq_ext: bool) -> LinkFrequency {
        LinkFrequency::new(link_freq_ext, self.link_freq)
    }
}
impl From<[u8; Self::SIZE]> for HostOrSecondaryInterface {
    fn from(bytes: [u8; Self::SIZE]) -> Self {
        let Le((
            command,
            link_control,
            link_config,
            revision_id,
            link_freq_err,
            link_freq_cap,
            feature,
            rsvd_0,
            enumeration_scratchpad,
            error_handling,
            mem_base_upper,
            mem_limit_upper,
            rsvd_1,
        )) = P13(bytes).into();
        let _: (u16, u16) = (rsvd_0, rsvd_1);
        let Lsb((link_freq, protocol_error, overflow_error, end_of_chain_error, ctl_timeout)) =
            P5::<u8, 4, 1, 1, 1, 1>(link_freq_err).into();
        let link_error = LinkError {
            protocol_error,
            overflow_error,
            end_of_chain_error,
            ctl_timeout,
        };
        Self {
            command: From::<u16>::from(command),
            link_control: From::<u16>::from(link_control),
            link_config: From::<u16>::from(link_config),
            revision_id: From::<u8>::from(revision_id),
            link_freq,
            link_error,
            link_freq_cap: From::<u16>::from(link_freq_cap),
            feature: From::<u16>::from(feature),
            enumeration_scratchpad,
            error_handling: From::<u16>::from(error_handling),
            mem_base_upper,
            mem_limit_upper,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostOrSecondaryCommand {
    /// Warm Reset
    pub warm_reset: bool,
    /// Double-Ended
    pub double_ended: bool,
    /// Device Number
    pub device_number: u8,
    /// Chain Side
    pub chain_side: bool,
    /// Host Hide
    pub host_hide: bool,
    /// Act as Slave
    pub act_as_slave: bool,
    /// Host Inbound End of Chain Error
    pub host_inbound_end_of_chain_error: bool,
    /// Drop on Uninitialized Link
    pub drop_on_uninitialized_link: bool,
}

impl From<u16> for HostOrSecondaryCommand {
    fn from(word: u16) -> Self {
        let Lsb((
            warm_reset,
            double_ended,
            device_number,
            chain_side,
            host_hide,
            (),
            act_as_slave,
            host_inbound_end_of_chain_error,
            drop_on_uninitialized_link,
            (),
        )) = P10::<_, 1, 1, 5, 1, 1, 1, 1, 1, 1, 3>(word).into();
        Self {
            warm_reset,
            double_ended,
            device_number,
            chain_side,
            host_hide,
            act_as_slave,
            host_inbound_end_of_chain_error,
            drop_on_uninitialized_link,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Switch {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReservedHost {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InterruptDiscoveryAndConfiguration {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnitIdClumping {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtendedConfigurationSpaceAccess {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddressMapping {}

/// MSI Mapping Capability
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MsiMapping {
    /// Indicating if the mapping is active
    pub enabled: bool,
    /// Indicating if the next two doublewords for programming address are present in the
    /// capability
    pub fixed: bool,
    /// Holds the lower portion of the base address where the mapping of MSIs takes place. It is
    /// set to FEEh upon warm reset
    pub base_address_lower: u32,
    /// Holds the upper portion of the base address for MSI mapping. It is cleared upon warm reset
    pub base_address_upper: u32,
}
impl MsiMapping {
    pub const SIZE: usize = 2 + 4 + 4;
    pub fn base_address(&self) -> u64 {
        let h = (self.base_address_upper as u64) << 32;
        let l = (self.base_address_lower as u64) & !0xfffff;
        h | l
    }
}
impl From<[u8; Self::SIZE]> for MsiMapping {
    fn from(bytes: [u8; Self::SIZE]) -> Self {
        let Le((cmd, base_address_lower, base_address_upper)) = P3(bytes).into();
        let Lsb((enabled, fixed, ())) = P3::<u16, 1, 1, 14>(cmd).into();
        Self {
            enabled,
            fixed,
            base_address_lower,
            base_address_upper,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirectRoute {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VCSet {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryMode {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct X86Encoding {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Gen3 {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionLevelExtension {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PowerManagement {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HighNodeCount {}

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

    #[test]
    fn slave_or_primary_interface() {
        // Command: BaseUnitID=0 UnitCnt=12 MastHost- DefDir- DUL-
        // Link Control 0: CFlE- CST- CFE- <LkFail- Init+ EOC- TXO- <CRCErr=0 IsocEn- LSEn- ExtCTL- 64b-
        // Link Config 0: MLWI=16bit DwFcIn- MLWO=16bit DwFcOut- LWI=16bit DwFcInEn- LWO=16bit DwFcOutEn-
        // Link Control 1: CFlE- CST- CFE- <LkFail+ Init- EOC+ TXO+ <CRCErr=0 IsocEn- LSEn- ExtCTL- 64b-
        // Link Config 1: MLWI=8bit DwFcIn- MLWO=8bit DwFcOut- LWI=8bit DwFcInEn- LWO=8bit DwFcOutEn-
        // Revision ID: 3.00
        // Link Frequency 0: [c]
        // Link Error 0: <Prot- <Ovfl- <EOC- CTLTm-
        // Link Frequency Capability 0: 200MHz+ 300MHz- 400MHz+ 500MHz- 600MHz+ 800MHz+ 1.0GHz+ 1.2GHz- 1.4GHz- 1.6GHz- Vend-
        // Feature Capability: IsocFC- LDTSTOP+ CRCTM- ECTLT- 64bA- UIDRD-
        // Link Frequency 1: 200MHz
        // Link Error 1: <Prot- <Ovfl- <EOC- CTLTm-
        // Link Frequency Capability 1: 200MHz- 300MHz- 400MHz- 500MHz- 600MHz- 800MHz- 1.0GHz- 1.2GHz- 1.4GHz- 1.6GHz- Vend-
        // Error Handling: PFlE- OFlE- PFE- OFE- EOCFE- RFE- CRCFE- SERRFE- CF- RE- PNFE- ONFE- EOCNFE- RNFE- CRCNFE- SERRNFE-
        // Prefetchable memory behind bridge Upper: 00-00
        // Bus Number: 00
        let data = [
            0x08, 0x54, 0x80, 0x01, // +00h
            0x20, 0x00, 0x11, 0x11, // +04h
            0xd0, 0x00, 0x00, 0x00, // +08h
            0x60, 0x0c, 0x75, 0x1e, // +0Ch
            0x02, 0x00, 0x00, 0x00, // +10h
            0x00, 0x00, 0x00, 0x00, // +14h
            0x00, 0x00, 0x00, 0x00, // +18h
        ];
        let data: [u8; SlaveOrPrimaryInterface::SIZE] = data[2..].try_into().unwrap();
        let result = data.into();
        let sample = SlaveOrPrimaryInterface {
            command: SlaveOrPrimaryCommand {
                base_unitid: 0,
                unit_count: 12,
                master_host: false,
                default_direction: false,
                drop_on_uninitialized_link: false,
            },
            link_control_0: LinkControl {
                source_id_enable: false,
                crc_flood_enable: false,
                crc_start_test: false,
                crc_force_error: false,
                link_failure: false,
                initialization_complete: true,
                end_of_chain: false,
                transmitter_off: false,
                crc_error: 0,
                isochronous_flow_control_enable: false,
                ldtstop_tristate_enable: false,
                extended_ctl_time: false,
                enable_64_bit_addressing: false,
            },
            link_config_0: LinkConfiguration {
                max_link_width_in: LinkWidth::Width16bits,
                doubleword_flow_control_in: false,
                max_link_width_out: LinkWidth::Width16bits,
                doubleword_flow_control_out: false,
                link_width_in: LinkWidth::Width16bits,
                doubleword_flow_control_in_enable: false,
                link_width_out: LinkWidth::Width16bits,
                doubleword_flow_control_out_enable: false,
            },
            link_control_1: LinkControl {
                source_id_enable: false,
                crc_flood_enable: false,
                crc_start_test: false,
                crc_force_error: false,
                link_failure: true,
                initialization_complete: false,
                end_of_chain: true,
                transmitter_off: true,
                crc_error: 0,
                isochronous_flow_control_enable: false,
                ldtstop_tristate_enable: false,
                extended_ctl_time: false,
                enable_64_bit_addressing: false,
            },
            link_config_1: LinkConfiguration {
                max_link_width_in: LinkWidth::Width8bits,
                doubleword_flow_control_in: false,
                max_link_width_out: LinkWidth::Width8bits,
                doubleword_flow_control_out: false,
                link_width_in: LinkWidth::Width8bits,
                doubleword_flow_control_in_enable: false,
                link_width_out: LinkWidth::Width8bits,
                doubleword_flow_control_out_enable: false,
            },
            revision_id: RevisionId { minor: 0, major: 3 },
            link_freq_0: 0b1100,
            link_error_0: LinkError {
                protocol_error: false,
                overflow_error: false,
                end_of_chain_error: false,
                ctl_timeout: false,
            },
            link_freq_cap_0: LinkFrequencyCapability {
                supports_200mhz: true,
                supports_300mhz: false,
                supports_400mhz: true,
                supports_500mhz: false,
                supports_600mhz: true,
                supports_800mhz: true,
                supports_1000mhz: true,
                supports_1200mhz: false,
                supports_1400mhz: false,
                supports_1600mhz: true,
                supports_1800mhz: true,
                supports_2000mhz: true,
                supports_2200mhz: true,
                supports_2400mhz: false,
                supports_2600mhz: false,
                supports_vendor_specific: false,
            },
            feature: FeatureCapability {
                isochronous_flow_control_mode: false,
                ldtstop: true,
                crc_test_mode: false,
                extended_ctl_time_required: false,
                qword_addressing: false,
                unitid_reorder_disable: false,
                source_identification_extension: false,
                extended_register_set: false,
                upstream_configuration_enable: false,
            },
            link_freq_1: 0, // 0 -> 200MHz (default)
            link_error_1: LinkError {
                protocol_error: false,
                overflow_error: false,
                end_of_chain_error: false,
                ctl_timeout: false,
            },
            link_freq_cap_1: LinkFrequencyCapability {
                supports_200mhz: false,
                supports_300mhz: false,
                supports_400mhz: false,
                supports_500mhz: false,
                supports_600mhz: false,
                supports_800mhz: false,
                supports_1000mhz: false,
                supports_1200mhz: false,
                supports_1400mhz: false,
                supports_1600mhz: false,
                supports_1800mhz: false,
                supports_2000mhz: false,
                supports_2200mhz: false,
                supports_2400mhz: false,
                supports_2600mhz: false,
                supports_vendor_specific: false,
            },
            enumeration_scratchpad: 0,
            error_handling: ErrorHandling {
                protocol_error_flood_enable: false,
                overflow_error_flood_enable: false,
                protocol_error_fatal_enable: false,
                overflow_error_fatal_enable: false,
                end_of_chain_error_fatal_enable: false,
                response_error_fatal_enable: false,
                crc_error_fatal_enable: false,
                system_error_fatal_enable: false,
                chain_fail: false,
                response_error: false,
                protocol_error_nonfatal_enable: false,
                overflow_error_nonfatal_enable: false,
                end_of_chain_error_nonfatal_enable: false,
                response_error_nonfatal_enable: false,
                crc_error_nonfatal_enable: false,
                system_error_nonfatal_enable: false,
            },
            mem_base_upper: 0,
            mem_limit_upper: 0,
            bus_number: 0,
        };
        assert_eq!(sample, result);
    }

    #[test]
    fn host_or_secondary_interface() {
        // Command: WarmRst+ DblEnd- DevNum=0 ChainSide- HostHide+ Slave- <EOCErr- DUL-
        // Link Control: CFlE- CST- CFE- <LkFail- Init+ EOC- TXO- <CRCErr=0 IsocEn- LSEn+ ExtCTL- 64b+
        // Link Config: MLWI=16bit DwFcIn- MLWO=16bit DwFcOut- LWI=16bit DwFcInEn- LWO=16bit DwFcOutEn-
        // Revision ID: 3.00
        // Link Frequency: [c]
        // Link Error: <Prot- <Ovfl- <EOC- CTLTm-
        // Link Frequency Capability: 200MHz+ 300MHz- 400MHz+ 500MHz- 600MHz+ 800MHz+ 1.0GHz+ 1.2GHz+ 1.4GHz+ 1.6GHz+ Vend+
        // Feature Capability: IsocFC+ LDTSTOP+ CRCTM- ECTLT- 64bA+ UIDRD- ExtRS- UCnfE-
        let data = [
            0x08, 0x00, 0x01, 0x21, // +00h
            0x20, 0xa0, 0x11, 0x11, // +04h
            0x60, 0x0c, 0xf5, 0xff, // +08h
            0x13, 0x00, 0x00, 0x00, // +0Ch
            0xee, 0x02, 0x84, 0x80, // +10h
            0x00, 0x00, 0x01, 0x00, // +14h
        ];
        let data: [u8; HostOrSecondaryInterface::SIZE] = data[2..].try_into().unwrap();
        let result = data.into();
        let sample = HostOrSecondaryInterface {
            command: HostOrSecondaryCommand {
                warm_reset: true,
                double_ended: false,
                device_number: 0,
                chain_side: false,
                host_hide: true,
                act_as_slave: false,
                host_inbound_end_of_chain_error: false,
                drop_on_uninitialized_link: false,
            },
            link_control: LinkControl {
                source_id_enable: false,
                crc_flood_enable: false,
                crc_start_test: false,
                crc_force_error: false,
                link_failure: false,
                initialization_complete: true,
                end_of_chain: false,
                transmitter_off: false,
                crc_error: 0,
                isochronous_flow_control_enable: false,
                ldtstop_tristate_enable: true,
                extended_ctl_time: false,
                enable_64_bit_addressing: true,
            },
            link_config: LinkConfiguration {
                max_link_width_in: LinkWidth::Width16bits,
                doubleword_flow_control_in: false,
                max_link_width_out: LinkWidth::Width16bits,
                doubleword_flow_control_out: false,
                link_width_in: LinkWidth::Width16bits,
                doubleword_flow_control_in_enable: false,
                link_width_out: LinkWidth::Width16bits,
                doubleword_flow_control_out_enable: false,
            },
            revision_id: RevisionId { minor: 0, major: 3 },
            link_freq: 0b1100,
            link_error: LinkError {
                protocol_error: false,
                overflow_error: false,
                end_of_chain_error: false,
                ctl_timeout: false,
            },
            // There is an error in lspci.c:
            // lfcap defined as u8 (should be u16)
            // therefore 1400MHz - VendorSpecific always not set
            link_freq_cap: LinkFrequencyCapability {
                supports_200mhz: true,
                supports_300mhz: false,
                supports_400mhz: true,
                supports_500mhz: false,
                supports_600mhz: true,
                supports_800mhz: true,
                supports_1000mhz: true,
                supports_1200mhz: true,
                supports_1400mhz: true,
                supports_1600mhz: true,
                supports_1800mhz: true,
                supports_2000mhz: true,
                supports_2200mhz: true,
                supports_2400mhz: true,
                supports_2600mhz: true,
                supports_vendor_specific: true,
            },
            feature: FeatureCapability {
                isochronous_flow_control_mode: true,
                ldtstop: true,
                crc_test_mode: false,
                extended_ctl_time_required: false,
                qword_addressing: true,
                unitid_reorder_disable: false,
                source_identification_extension: false,
                extended_register_set: false,
                upstream_configuration_enable: false,
            },
            enumeration_scratchpad: 0x02ee,
            error_handling: ErrorHandling {
                protocol_error_flood_enable: false,
                overflow_error_flood_enable: false,
                protocol_error_fatal_enable: true,
                overflow_error_fatal_enable: false,
                end_of_chain_error_fatal_enable: false,
                response_error_fatal_enable: false,
                crc_error_fatal_enable: false,
                system_error_fatal_enable: true,
                chain_fail: false,
                response_error: false,
                protocol_error_nonfatal_enable: false,
                overflow_error_nonfatal_enable: false,
                end_of_chain_error_nonfatal_enable: false,
                response_error_nonfatal_enable: false,
                crc_error_nonfatal_enable: false,
                system_error_nonfatal_enable: true,
            },
            mem_base_upper: 0,
            mem_limit_upper: 0,
        };
        assert_eq!(sample, result);
    }

    #[test]
    fn revision_id() {
        let data = 0b00100101;
        let result = data.into();
        let sample = RevisionId { major: 1, minor: 5 };
        assert_eq!(sample, result);
    }

    #[test]
    fn msi_mapping() {
        // MSI Mapping Enable+ Fixed+
        let data = [
            0x08, 0x00, 0x03, 0xa8, // +00h
            0x00, 0x00, 0x00, 0x00, // +04h
            0x34, 0x17, 0xda, 0x11, // +08h
        ];
        let data: [u8; MsiMapping::SIZE] = data[2..].try_into().unwrap();
        let result = data.into();
        let sample = MsiMapping {
            enabled: true,
            fixed: true,
            base_address_lower: 0,
            base_address_upper: 0x11da1734,
        };
        assert_eq!(sample, result);

        assert_eq!(0x11da173400000000, result.base_address());
    }
}