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
use std::convert::TryFrom;
use std::fmt::Display;
#[macro_use]
extern crate bitfield;
#[repr(u8)]
#[derive(PartialEq, Copy, Clone)]
pub enum TlpFmt {
NoDataHeader3DW = 0b000,
NoDataHeader4DW = 0b001,
WithDataHeader3DW = 0b010,
WithDataHeader4DW = 0b011,
TlpPrefix = 0b100,
}
impl Display for TlpFmt {
fn fmt (&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
let name = match &self {
TlpFmt::NoDataHeader3DW => "3DW no Data Header",
TlpFmt::NoDataHeader4DW => "4DW no Data Header",
TlpFmt::WithDataHeader3DW => "3DW with Data Header",
TlpFmt::WithDataHeader4DW => "4DW with Data Header",
TlpFmt::TlpPrefix => "Tlp Prefix",
};
write!(fmt, "{}", name)
}
}
impl TryFrom<u32> for TlpFmt {
type Error = ();
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == TlpFmt::NoDataHeader3DW as u32 => Ok(TlpFmt::NoDataHeader3DW),
x if x == TlpFmt::NoDataHeader4DW as u32 => Ok(TlpFmt::NoDataHeader4DW),
x if x == TlpFmt::WithDataHeader3DW as u32 => Ok(TlpFmt::WithDataHeader3DW),
x if x == TlpFmt::WithDataHeader4DW as u32 => Ok(TlpFmt::WithDataHeader4DW),
x if x == TlpFmt::TlpPrefix as u32 => Ok(TlpFmt::TlpPrefix),
_ => Err(()),
}
}
}
#[derive(PartialEq)]
pub enum TlpFormatEncodingType {
MemoryRequest = 0b00000,
MemoryLockRequest = 0b00001,
IORequest = 0b00010,
ConfigType0Request = 0b00100,
ConfigType1Request = 0b00101,
Completion = 0b01010,
CompletionLocked = 0b01011,
FetchAtomicOpRequest = 0b01100,
UnconSwapAtomicOpRequest= 0b01101,
CompSwapAtomicOpRequest = 0b01110,
}
impl TryFrom<u32> for TlpFormatEncodingType {
type Error = ();
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == TlpFormatEncodingType::MemoryRequest as u32 => Ok(TlpFormatEncodingType::MemoryRequest),
x if x == TlpFormatEncodingType::MemoryLockRequest as u32 => Ok(TlpFormatEncodingType::MemoryLockRequest),
x if x == TlpFormatEncodingType::IORequest as u32 => Ok(TlpFormatEncodingType::IORequest),
x if x == TlpFormatEncodingType::ConfigType0Request as u32 => Ok(TlpFormatEncodingType::ConfigType0Request),
x if x == TlpFormatEncodingType::ConfigType1Request as u32 => Ok(TlpFormatEncodingType::ConfigType1Request),
x if x == TlpFormatEncodingType::Completion as u32 => Ok(TlpFormatEncodingType::Completion),
x if x == TlpFormatEncodingType::CompletionLocked as u32 => Ok(TlpFormatEncodingType::CompletionLocked),
x if x == TlpFormatEncodingType::FetchAtomicOpRequest as u32 => Ok(TlpFormatEncodingType::FetchAtomicOpRequest),
x if x == TlpFormatEncodingType::UnconSwapAtomicOpRequest as u32 => Ok(TlpFormatEncodingType::UnconSwapAtomicOpRequest),
x if x == TlpFormatEncodingType::CompSwapAtomicOpRequest as u32 => Ok(TlpFormatEncodingType::CompSwapAtomicOpRequest),
_ => Err(()),
}
}
}
#[derive(PartialEq)]
#[derive(Debug)]
pub enum TlpType {
MemReadReq,
MemReadLockReq,
MemWriteReq,
IOReadReq,
IOWriteReq,
ConfType0ReadReq,
ConfType0WriteReq,
ConfType1ReadReq,
ConfType1WriteReq,
MsgReq,
MsgReqData,
Cpl,
CplData,
CplLocked,
CplDataLocked,
FetchAddAtomicOpReq,
SwapAtomicOpReq,
CompareSwapAtomicOpReq,
LocalTlpPrefix,
EndToEndTlpPrefix,
}
impl Display for TlpType {
fn fmt (&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
let name = match &self {
TlpType::MemReadReq => "Memory Read Request",
TlpType::MemReadLockReq => "Locked Memory Read Request",
TlpType::MemWriteReq => "Memory Write Request",
TlpType::IOReadReq => "IO Read Request",
TlpType::IOWriteReq => "IO Write Request",
TlpType::ConfType0ReadReq => "Type 0 Config Read Request",
TlpType::ConfType0WriteReq => "Type 0 Config Write Request",
TlpType::ConfType1ReadReq => "Type 1 Config Read Request",
TlpType::ConfType1WriteReq => "Type 1 Config Write Request",
TlpType::MsgReq => "Message Request",
TlpType::MsgReqData => "Message with Data Request",
TlpType::Cpl => "Completion",
TlpType::CplData => "Completion with Data",
TlpType::CplLocked => "Locked Completion",
TlpType::CplDataLocked => "Locked Completion with Data",
TlpType::FetchAddAtomicOpReq => "Fetch Add Atomic Op Request",
TlpType::SwapAtomicOpReq => "Swap Atomic Op Request",
TlpType::CompareSwapAtomicOpReq => "Compare Swap Atomic Op Request",
TlpType::LocalTlpPrefix => "Local Tlp Prefix",
TlpType::EndToEndTlpPrefix => "End To End Tlp Prefix",
};
write!(fmt, "{}", name)
}
}
bitfield! {
struct TlpHeader(MSB0 [u8]);
u32;
get_format, _: 2, 0;
get_type, _: 7, 3;
get_t9, _: 8, 8;
get_tc, _: 11, 9;
get_t8, _: 12, 12;
get_attr_b2, _: 13, 13;
get_ln, _: 14, 14;
get_th, _: 15, 15;
get_td, _: 16, 16;
get_ep, _: 17, 17;
get_attr, _: 19, 18;
get_at, _: 21, 20;
get_length, _: 31, 22;
}
impl<T: AsRef<[u8]>> TlpHeader<T> {
fn get_tlp_type(&self) -> Result<TlpType, ()> {
let tlp_type = self.get_type();
let tlp_fmt = self.get_format();
match TlpFormatEncodingType::try_from(tlp_type) {
Ok(TlpFormatEncodingType::MemoryRequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::MemReadReq),
Ok(TlpFmt::NoDataHeader4DW) => Ok(TlpType::MemReadReq),
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::MemWriteReq),
Ok(TlpFmt::WithDataHeader4DW) => Ok(TlpType::MemWriteReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::MemoryLockRequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::MemReadLockReq),
Ok(TlpFmt::NoDataHeader4DW) => Ok(TlpType::MemReadLockReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::IORequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::IOReadReq),
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::IOWriteReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::ConfigType0Request) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::ConfType0ReadReq),
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::ConfType0WriteReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::Completion) => {
println!("Completion fmt: {}", tlp_fmt);
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::Cpl),
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::CplData),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::CompletionLocked) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::NoDataHeader3DW) => Ok(TlpType::CplLocked),
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::CplDataLocked),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::FetchAtomicOpRequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::FetchAddAtomicOpReq),
Ok(TlpFmt::WithDataHeader4DW) => Ok(TlpType::FetchAddAtomicOpReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::UnconSwapAtomicOpRequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::SwapAtomicOpReq),
Ok(TlpFmt::WithDataHeader4DW) => Ok(TlpType::SwapAtomicOpReq),
_ => Err(()),
}
}
Ok(TlpFormatEncodingType::CompSwapAtomicOpRequest) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::WithDataHeader3DW) => Ok(TlpType::CompareSwapAtomicOpReq),
Ok(TlpFmt::WithDataHeader4DW) => Ok(TlpType::CompareSwapAtomicOpReq),
_ => Err(()),
}
}
Ok(_) => {
match TlpFmt::try_from(tlp_fmt) {
Ok(TlpFmt::TlpPrefix) => {
match tlp_type & 0b10000 {
0b10000 => Ok(TlpType::LocalTlpPrefix),
_ => Ok(TlpType::EndToEndTlpPrefix),
}
}
Ok(TlpFmt::NoDataHeader4DW) => {
if (tlp_type >> 3) == 0b10 {
Ok(TlpType::MsgReqData)
} else {
Err(())
}
}
Ok(TlpFmt::WithDataHeader4DW) => {
if (tlp_type >> 3) == 0b10 {
Ok(TlpType::MsgReq)
} else {
Err(())
}
}
_ => Err(()),
}
}
Err(_) => Err(())
}
}
}
pub trait MemRequest {
fn address(&self) -> u64;
fn req_id(&self) -> u16;
fn tag(&self) -> u8;
fn ldwbe(&self) -> u8;
fn fdwbe(&self) -> u8;
}
bitfield! {
pub struct MemRequest3DW(MSB0 [u8]);
u32;
pub get_requester_id, _: 15, 0;
pub get_tag, _: 23, 16;
pub get_last_dw_be, _: 27, 24;
pub get_first_dw_be, _: 31, 28;
pub get_address32, _: 63, 32;
}
bitfield! {
pub struct MemRequest4DW(MSB0 [u8]);
u64;
pub get_requester_id, _: 15, 0;
pub get_tag, _: 23, 16;
pub get_last_dw_be, _: 27, 24;
pub get_first_dw_be, _: 31, 28;
pub get_address64, _: 95, 32;
}
impl <T: AsRef<[u8]>> MemRequest for MemRequest3DW<T> {
fn address(&self) -> u64 {
self.get_address32().into()
}
fn req_id(&self) -> u16 {
self.get_requester_id() as u16
}
fn tag(&self) -> u8 {
self.get_tag() as u8
}
fn ldwbe(&self) -> u8 {
self.get_last_dw_be() as u8
}
fn fdwbe(&self) -> u8 {
self.get_first_dw_be() as u8
}
}
impl <T: AsRef<[u8]>> MemRequest for MemRequest4DW<T> {
fn address(&self) -> u64 {
self.get_address64()
}
fn req_id(&self) -> u16 {
self.get_requester_id() as u16
}
fn tag(&self) -> u8 {
self.get_tag() as u8
}
fn ldwbe(&self) -> u8 {
self.get_last_dw_be() as u8
}
fn fdwbe(&self) -> u8 {
self.get_first_dw_be() as u8
}
}
pub fn new_mem_req(bytes: Vec<u8>, format: &TlpFmt) -> Box<dyn MemRequest> {
match format {
TlpFmt::NoDataHeader3DW => Box::new(MemRequest3DW(bytes)),
TlpFmt::NoDataHeader4DW => Box::new(MemRequest4DW(bytes)),
TlpFmt::WithDataHeader3DW => Box::new(MemRequest3DW(bytes)),
TlpFmt::WithDataHeader4DW => Box::new(MemRequest4DW(bytes)),
TlpFmt::TlpPrefix => Box::new(MemRequest3DW(bytes)),
}
}
pub trait ConfigurationRequest {
fn req_id(&self) -> u16;
fn tag(&self) -> u8;
fn bus_nr(&self) -> u8;
fn dev_nr(&self) -> u8;
fn func_nr(&self) -> u8;
fn ext_reg_nr(&self) -> u8;
fn reg_nr(&self) -> u8;
}
pub fn new_conf_req(bytes: Vec<u8>, _format: &TlpFmt) -> Box<dyn ConfigurationRequest> {
Box::new(ConfigRequest(bytes))
}
bitfield! {
pub struct ConfigRequest(MSB0 [u8]);
u32;
pub get_requester_id, _: 15, 0;
pub get_tag, _: 23, 16;
pub get_last_dw_be, _: 27, 24;
pub get_first_dw_be, _: 31, 28;
pub get_bus_nr, _: 39, 32;
pub get_dev_nr, _: 44, 40;
pub get_func_nr, _: 47, 45;
pub rsvd, _: 51, 48;
pub get_ext_reg_nr, _: 55, 52;
pub get_register_nr, _: 61, 56;
r, _: 63, 62;
}
impl <T: AsRef<[u8]>> ConfigurationRequest for ConfigRequest<T> {
fn req_id(&self) -> u16 {
self.get_requester_id() as u16
}
fn tag(&self) -> u8 {
self.get_tag() as u8
}
fn bus_nr(&self) -> u8 {
self.get_bus_nr() as u8
}
fn dev_nr(&self) -> u8 {
self.get_dev_nr() as u8
}
fn func_nr(&self) -> u8 {
self.get_func_nr() as u8
}
fn ext_reg_nr(&self) -> u8 {
self.get_ext_reg_nr() as u8
}
fn reg_nr(&self) -> u8 {
self.get_register_nr() as u8
}
}
pub trait CompletionRequest {
fn cmpl_id(&self) -> u16;
fn cmpl_stat(&self) -> u8;
fn bcm(&self) -> u8;
fn byte_cnt(&self) -> u16;
fn req_id(&self) -> u16;
fn tag(&self) -> u8;
fn laddr(&self) -> u8;
}
bitfield! {
pub struct CompletionReqDW23(MSB0 [u8]);
u16;
pub get_completer_id, _: 15, 0;
pub get_cmpl_stat, _: 18, 16;
pub get_bcm, _: 19, 19;
pub get_byte_cnt, _: 31, 20;
pub get_req_id, _: 47, 32;
pub get_tag, _: 55, 48;
r, _: 57, 56;
pub get_laddr, _: 63, 58;
}
impl <T: AsRef<[u8]>> CompletionRequest for CompletionReqDW23<T> {
fn cmpl_id(&self) -> u16 {
self.get_completer_id() as u16
}
fn cmpl_stat(&self) -> u8 {
self.get_cmpl_stat() as u8
}
fn bcm(&self) -> u8 {
self.get_bcm() as u8
}
fn byte_cnt(&self) -> u16 {
self.get_byte_cnt() as u16
}
fn req_id(&self) -> u16 {
self.get_req_id() as u16
}
fn tag(&self) -> u8 {
self.get_tag() as u8
}
fn laddr(&self) -> u8 {
self.get_laddr() as u8
}
}
pub fn new_cmpl_req(bytes: Vec<u8>, _format: &TlpFmt) -> Box<dyn CompletionRequest> {
Box::new(CompletionReqDW23(bytes))
}
pub trait MessageRequest {
fn req_id(&self) -> u16;
fn tag(&self) -> u8;
fn msg_code(&self) -> u8;
fn dw3(&self) -> u32;
fn dw4(&self) -> u32;
}
bitfield! {
pub struct MessageReqDW24(MSB0 [u8]);
u16;
pub get_requester_id, _: 15, 0;
pub get_tag, _: 23, 16;
pub get_msg_code, _: 31, 24;
pub get_dw3, _: 63, 32;
pub get_dw4, _: 96, 64;
}
impl <T: AsRef<[u8]>> MessageRequest for MessageReqDW24<T> {
fn req_id(&self) -> u16 {
self.get_requester_id() as u16
}
fn tag(&self) -> u8 {
self.get_tag() as u8
}
fn msg_code(&self) -> u8 {
self.get_msg_code() as u8
}
fn dw3(&self) -> u32 {
self.get_dw3() as u32
}
fn dw4(&self) -> u32 {
self.get_dw4() as u32
}
}
pub fn new_msg_req(bytes: Vec<u8>, _format: &TlpFmt) -> Box<dyn MessageRequest> {
Box::new(MessageReqDW24(bytes))
}
pub struct TlpPacketHeader {
bytes: Vec<u8>,
header: TlpHeader<Vec<u8>>,
}
impl TlpPacketHeader {
pub fn new(bytes: Vec<u8>) -> TlpPacketHeader {
let mut dw0 = vec![0; 4];
dw0[..4].clone_from_slice(&bytes[0..4]);
TlpPacketHeader { bytes: bytes, header: TlpHeader(dw0) }
}
pub fn get_tlp_type(&self) -> Result<TlpType, ()> {
let mut dw0 = vec![0; 4];
dw0[..4].clone_from_slice(&self.bytes[0..4]);
let tlp_head = TlpHeader(dw0);
tlp_head.get_tlp_type()
}
pub fn get_format(&self) -> u32 {self.header.get_format()}
pub fn get_type(&self) -> u32 {self.header.get_type()}
pub fn get_t9(&self) -> u32 {self.header.get_t9()}
pub fn get_tc(&self) -> u32 {self.header.get_tc()}
pub fn get_t8(&self) -> u32 {self.header.get_t8()}
pub fn get_attr_b2(&self) -> u32 {self.header.get_attr_b2()}
pub fn get_ln(&self) -> u32 {self.header.get_ln()}
pub fn get_th(&self) -> u32 {self.header.get_th()}
pub fn get_td(&self) -> u32 {self.header.get_td()}
pub fn get_ep(&self) -> u32 {self.header.get_ep()}
pub fn get_attr(&self) -> u32 {self.header.get_attr()}
pub fn get_at(&self) -> u32 {self.header.get_at()}
pub fn get_length(&self) -> u32 {self.header.get_length()}
}
pub struct TlpPacket {
header: TlpPacketHeader,
data: Vec<u8>,
}
impl TlpPacket {
pub fn new(bytes: Vec<u8>) -> TlpPacket {
let mut ownbytes = bytes.to_vec();
let mut header = vec![0; 4];
header.clone_from_slice(&ownbytes[0..4]);
let data = ownbytes.drain(4..).collect();
TlpPacket {
header: TlpPacketHeader::new(header),
data: data,
}
}
pub fn get_header(&self) -> &TlpPacketHeader {
&self.header
}
pub fn get_data(&self) -> Vec<u8> {
self.data.to_vec()
}
pub fn get_tlp_type(&self) -> TlpType {
self.header.get_tlp_type().expect("Cannot Parse TLP!")
}
pub fn get_tlp_format(&self) -> TlpFmt {
let fmt : TlpFmt = TlpFmt::try_from(self.header.get_format()).unwrap();
fmt
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tlp_packet() {
let d = vec![0x04, 0x00, 0x00, 0x01, 0x20, 0x01, 0xFF, 0x00, 0xC2, 0x81, 0xFF, 0x10];
let tlp = TlpPacket::new(d);
assert_eq!(tlp.get_tlp_type(), TlpType::ConfType0ReadReq);
assert_eq!(tlp.get_data(), vec![0x20, 0x01, 0xFF, 0x00, 0xC2, 0x81, 0xFF, 0x10]);
}
#[test]
fn test_complreq_trait() {
let cmpl_req = CompletionReqDW23([0x20, 0x01, 0xFF, 0x00, 0xC2, 0x81, 0xFF, 0x10]);
assert_eq!(0x2001, cmpl_req.cmpl_id());
assert_eq!(0x7, cmpl_req.cmpl_stat());
assert_eq!(0x1, cmpl_req.bcm());
assert_eq!(0xF00, cmpl_req.byte_cnt());
assert_eq!(0xC281, cmpl_req.req_id());
assert_eq!(0xFF, cmpl_req.tag());
assert_eq!(0x10, cmpl_req.laddr());
}
#[test]
fn test_configreq_trait() {
let conf_req = ConfigRequest([0x20, 0x01, 0xFF, 0x00, 0xC2, 0x81, 0xFF, 0x10]);
assert_eq!(0x2001, conf_req.req_id());
assert_eq!(0xFF, conf_req.tag());
assert_eq!(0xC2, conf_req.bus_nr());
assert_eq!(0x10, conf_req.dev_nr());
assert_eq!(0x01, conf_req.func_nr());
assert_eq!(0x0F, conf_req.ext_reg_nr());
assert_eq!(0x04, conf_req.reg_nr());
}
#[test]
fn is_memreq_tag_works() {
let mr3dw1 = MemRequest3DW([0x00, 0x00, 0x20, 0x0F, 0xF6, 0x20, 0x00, 0x0C]);
let mr3dw2 = MemRequest3DW([0x00, 0x00, 0x01, 0x0F, 0xF6, 0x20, 0x00, 0x0C]);
let mr3dw3 = MemRequest3DW([0x00, 0x00, 0x10, 0x0F, 0xF6, 0x20, 0x00, 0x0C]);
let mr3dw4 = MemRequest3DW([0x00, 0x00, 0x81, 0x0F, 0xF6, 0x20, 0x00, 0x0C]);
assert_eq!(0x20, mr3dw1.tag());
assert_eq!(0x01, mr3dw2.tag());
assert_eq!(0x10, mr3dw3.tag());
assert_eq!(0x81, mr3dw4.tag());
let mr4dw1 = MemRequest4DW([0x00, 0x00, 0x01, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00]);
let mr4dw2 = MemRequest4DW([0x00, 0x00, 0x10, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00]);
let mr4dw3 = MemRequest4DW([0x00, 0x00, 0x81, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00]);
let mr4dw4 = MemRequest4DW([0x00, 0x00, 0xFF, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00]);
let mr4dw5 = MemRequest4DW([0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00]);
assert_eq!(0x01, mr4dw1.tag());
assert_eq!(0x10, mr4dw2.tag());
assert_eq!(0x81, mr4dw3.tag());
assert_eq!(0xFF, mr4dw4.tag());
assert_eq!(0x00, mr4dw5.tag());
}
#[test]
fn is_memreq_3dw_address_works() {
let memreq_3dw = [0x00, 0x00, 0x20, 0x0F, 0xF6, 0x20, 0x00, 0x0C];
let mr = MemRequest3DW(memreq_3dw);
assert_eq!(0xF620000C, mr.address());
}
#[test]
fn is_memreq_4dw_address_works() {
let memreq_4dw = [0x00, 0x00, 0x20, 0x0F, 0x00, 0x00, 0x01, 0x7f, 0xc0, 0x00, 0x00, 0x00];
let mr = MemRequest4DW(memreq_4dw);
assert_eq!(0x17fc0000000, mr.address());
}
#[test]
fn is_tlppacket_creates() {
let memrd32_header = [0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x20, 0x0F, 0xF6, 0x20, 0x00, 0x0C];
let mr = TlpPacketHeader::new(memrd32_header.to_vec());
assert_eq!(mr.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemReadReq);
}
#[test]
fn tlp_header_type() {
let memread = TlpHeader([0x0, 0x0, 0x0, 0x0]);
assert_eq!(memread.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemReadReq);
let memread32 = TlpHeader([0x00, 0x00, 0x20, 0x01]);
assert_eq!(memread32.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemReadReq);
let memwrite32 = TlpHeader([0x40, 0x00, 0x00, 0x01]);
assert_eq!(memwrite32.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemWriteReq);
let cpl_no_data = TlpHeader([0x0a, 0x00, 0x10, 0x00]);
assert_eq!(cpl_no_data.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::Cpl);
let cpl_with_data = TlpHeader([0x4a, 0x00, 0x20, 0x40]);
assert_eq!(cpl_with_data.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::CplData);
let memread_4dw = TlpHeader([0x20, 0x00, 0x20, 0x40]);
assert_eq!(memread_4dw.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemReadReq);
let conf_t0_read = TlpHeader([0x04, 0x00, 0x00, 0x01]);
assert_eq!(conf_t0_read.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::ConfType0ReadReq);
let conf_t0_write = TlpHeader([0x44, 0x00, 0x00, 0x01]);
assert_eq!(conf_t0_write.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::ConfType0WriteReq);
let memwrite64 = TlpHeader([0x60, 0x00, 0x90, 0x01]);
assert_eq!(memwrite64.get_tlp_type().expect("Cannot Parse TLP!"), TlpType::MemWriteReq);
}
#[test]
fn tlp_header_works_all_zeros() {
let bits_locations = TlpHeader([0x0, 0x0, 0x0, 0x0]);
assert_eq!(bits_locations.get_format(), 0);
assert_eq!(bits_locations.get_type(), 0);
assert_eq!(bits_locations.get_t9(), 0);
assert_eq!(bits_locations.get_tc(), 0);
assert_eq!(bits_locations.get_t8(), 0);
assert_eq!(bits_locations.get_attr_b2(), 0);
assert_eq!(bits_locations.get_ln(), 0);
assert_eq!(bits_locations.get_th(), 0);
assert_eq!(bits_locations.get_td(), 0);
assert_eq!(bits_locations.get_ep(), 0);
assert_eq!(bits_locations.get_attr(), 0);
assert_eq!(bits_locations.get_at(), 0);
assert_eq!(bits_locations.get_length(), 0);
}
#[test]
fn tlp_header_works_all_ones() {
let bits_locations = TlpHeader([0xff, 0xff, 0xff, 0xff]);
assert_eq!(bits_locations.get_format(), 0x7);
assert_eq!(bits_locations.get_type(), 0x1f);
assert_eq!(bits_locations.get_t9(), 0x1);
assert_eq!(bits_locations.get_tc(), 0x7);
assert_eq!(bits_locations.get_t8(), 0x1);
assert_eq!(bits_locations.get_attr_b2(), 0x1);
assert_eq!(bits_locations.get_ln(), 0x1);
assert_eq!(bits_locations.get_th(), 0x1);
assert_eq!(bits_locations.get_td(), 0x1);
assert_eq!(bits_locations.get_ep(), 0x1);
assert_eq!(bits_locations.get_attr(), 0x3);
assert_eq!(bits_locations.get_at(), 0x3);
assert_eq!(bits_locations.get_length(), 0x3ff);
}
}