/
transceivers.rs
1280 lines (1169 loc) · 41.2 KB
/
transceivers.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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::{Addr, Reg};
use drv_fpga_api::{FpgaError, FpgaUserDesign, WriteOp};
use drv_transceivers_api::{ModuleStatus, TransceiversError, NUM_PORTS};
use transceiver_messages::ModuleId;
use userlib::{FromPrimitive, UnwrapLite};
use zerocopy::{byteorder, AsBytes, FromBytes, Unaligned, U16};
// The transceiver modules are split across two FPGAs on the QSFP Front IO
// board, so while we present the modules as a unit, the communication is
// actually bifurcated.
pub struct Transceivers {
fpgas: [FpgaUserDesign; 2],
}
// There are two FPGA controllers, each controlling the FPGA on either the left
// or right of the board.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum FpgaController {
Left = 0,
Right = 1,
}
/// Physical port location
#[derive(Copy, Clone, PartialEq)]
pub struct PortLocation {
pub controller: FpgaController,
pub port: PhysicalPort,
}
impl From<LogicalPort> for PortLocation {
fn from(port: LogicalPort) -> PortLocation {
PORT_MAP[port]
}
}
/// Physical port location within a particular FPGA, as a 0-15 index
#[derive(Copy, Clone, PartialEq)]
pub struct PhysicalPort(pub u8);
impl PhysicalPort {
pub fn as_mask(&self) -> PhysicalPortMask {
PhysicalPortMask(1 << self.0)
}
pub fn get(&self) -> u8 {
self.0
}
pub fn to_logical_port(
&self,
fpga: FpgaController,
) -> Result<LogicalPort, TransceiversError> {
let loc = PortLocation {
controller: fpga,
port: *self,
};
match PORT_MAP.into_iter().position(|&l| l == loc) {
Some(p) => Ok(LogicalPort(p as u8)),
None => Err(TransceiversError::InvalidPhysicalToLogicalMap),
}
}
}
/// Physical port mask within a particular FPGA, as a 16-bit bitfield
#[derive(Copy, Clone, Default)]
pub struct PhysicalPortMask(pub u16);
impl PhysicalPortMask {
pub fn get(&self) -> u16 {
self.0
}
pub fn set(&mut self, index: PhysicalPort) {
self.0 |= index.as_mask().0
}
pub fn merge(&mut self, other: PhysicalPortMask) {
self.0 |= other.0
}
pub fn is_set(&self, index: PhysicalPort) -> bool {
self.0 & index.as_mask().0 != 0
}
pub fn is_empty(&self) -> bool {
self.0 == 0
}
}
/// Physical port maps, using bitfields to mark active ports
#[derive(Copy, Clone, Default)]
pub struct FpgaPortMasks {
pub left: PhysicalPortMask,
pub right: PhysicalPortMask,
}
impl FpgaPortMasks {
/// Returns an iterator over FPGAs that are active in the mask
///
/// (possibilities include `Left`, `Right`, both, or none)
fn iter_fpgas(&self) -> impl Iterator<Item = FpgaController> {
let out = [
Some(FpgaController::Left).filter(|_| !self.left.is_empty()),
Some(FpgaController::Right).filter(|_| !self.right.is_empty()),
];
out.into_iter().flatten()
}
fn get(&self, fpga: FpgaController) -> PhysicalPortMask {
match fpga {
FpgaController::Left => self.left,
FpgaController::Right => self.right,
}
}
fn get_mut(&mut self, fpga: FpgaController) -> &mut PhysicalPortMask {
match fpga {
FpgaController::Left => &mut self.left,
FpgaController::Right => &mut self.right,
}
}
}
/// Represents a single logical port (0-31)
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct LogicalPort(pub u8);
impl LogicalPort {
pub fn as_mask(&self) -> LogicalPortMask {
LogicalPortMask(1 << self.0)
}
pub fn get_physical_location(&self) -> PortLocation {
PortLocation::from(*self)
}
}
/// Represents a set of selected logical ports, i.e. a 32-bit bitmask
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct LogicalPortMask(pub u32);
impl LogicalPortMask {
pub const MAX_PORT_INDEX: LogicalPort = LogicalPort(NUM_PORTS - 1);
pub fn get(&self) -> u32 {
self.0
}
pub fn set(&mut self, index: LogicalPort) {
*self |= index
}
pub fn clear(&mut self, index: LogicalPort) {
*self &= !index.as_mask()
}
pub fn merge(&mut self, other: LogicalPortMask) {
*self |= other
}
pub fn count(&self) -> usize {
self.0.count_ones() as _
}
pub fn is_set(&self, index: LogicalPort) -> bool {
!(*self & index).is_empty()
}
pub fn is_empty(&self) -> bool {
self.0 == 0
}
pub fn to_indices(&self) -> impl Iterator<Item = LogicalPort> + '_ {
(0..32).map(LogicalPort).filter(|p| self.is_set(*p))
}
}
// `ModuleId` is a 64-bit logical port mask. The choice of u64 was to provide
// future flexibility, but currently we only support 32 distinct ports, so we
// ignore the upper 32 bits of `ModuleId` when constructing a `LogicalPortMask`.
impl From<ModuleId> for LogicalPortMask {
fn from(v: ModuleId) -> Self {
Self(v.0 as u32)
}
}
impl From<LogicalPortMask> for ModuleId {
fn from(v: LogicalPortMask) -> Self {
Self(v.0 as u64)
}
}
// It is convenient to have the ergonomics for a LogicalPortMask resemble the
// bitwise mask that it represents, so we implement some bitwise operations.
impl core::ops::BitOr for LogicalPortMask {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
LogicalPortMask(self.0 | rhs.0)
}
}
impl core::ops::BitOr<LogicalPort> for LogicalPortMask {
type Output = Self;
fn bitor(self, rhs: LogicalPort) -> Self {
LogicalPortMask(self.0 | rhs.as_mask().0)
}
}
impl core::ops::BitOrAssign for LogicalPortMask {
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs
}
}
impl core::ops::BitOrAssign<LogicalPort> for LogicalPortMask {
fn bitor_assign(&mut self, rhs: LogicalPort) {
*self = *self | rhs
}
}
impl core::ops::BitAnd for LogicalPortMask {
type Output = Self;
fn bitand(self, rhs: Self) -> Self {
LogicalPortMask(self.0 & rhs.0)
}
}
impl core::ops::BitAnd<LogicalPort> for LogicalPortMask {
type Output = Self;
fn bitand(self, rhs: LogicalPort) -> Self {
LogicalPortMask(self.0 & rhs.as_mask().0)
}
}
impl core::ops::BitAndAssign for LogicalPortMask {
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs
}
}
impl core::ops::BitAndAssign<LogicalPort> for LogicalPortMask {
fn bitand_assign(&mut self, rhs: LogicalPort) {
*self = *self & rhs
}
}
impl core::ops::Not for LogicalPortMask {
type Output = Self;
fn not(self) -> Self {
LogicalPortMask(!self.0)
}
}
// Maps physical port `mask` to a logical port mask
impl From<FpgaPortMasks> for LogicalPortMask {
fn from(mask: FpgaPortMasks) -> LogicalPortMask {
let mut logical_mask = LogicalPortMask(0);
for logical_port in (0..NUM_PORTS).map(LogicalPort) {
let port_location = PortLocation::from(logical_port);
let bits = mask.get(port_location.controller);
if bits.is_set(port_location.port) {
logical_mask |= logical_port;
}
}
logical_mask
}
}
// Maps logical port `mask` to physical FPGA locations
impl From<LogicalPortMask> for FpgaPortMasks {
fn from(mask: LogicalPortMask) -> FpgaPortMasks {
let mut fpga_port_masks = FpgaPortMasks::default();
for (i, port_loc) in PORT_MAP.enumerate() {
if mask.is_set(i) {
fpga_port_masks
.get_mut(port_loc.controller)
.set(port_loc.port);
}
}
fpga_port_masks
}
}
/// Logical -> physical mapping for ports
///
/// Each index in this map represents the location of its transceiver port, so
/// index 0 is for port 0, and so on. The ports numbered 0-15 left to right
/// across the top of the board and 16-31 left to right across the bottom. The
/// ports are split up between the FPGAs based on locality, not logically and
/// the FPGAs share code, resulting in each one reporting in terms of ports
/// 0-15.
struct PortMap([PortLocation; NUM_PORTS as usize]);
impl core::ops::Index<LogicalPort> for PortMap {
type Output = PortLocation;
fn index(&self, i: LogicalPort) -> &Self::Output {
&self.0[i.0 as usize]
}
}
impl<'a> IntoIterator for &'a PortMap {
type Item = &'a PortLocation;
type IntoIter = core::slice::Iter<'a, PortLocation>;
fn into_iter(self) -> Self::IntoIter {
self.0.as_slice().iter()
}
}
impl PortMap {
fn enumerate(&self) -> impl Iterator<Item = (LogicalPort, &PortLocation)> {
self.0
.iter()
.enumerate()
.map(|(i, v)| (LogicalPort(i as u8), v))
}
}
const PORT_MAP: PortMap = PortMap([
// Port 0
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(0),
},
// Port 1
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(1),
},
// Port 2
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(2),
},
// Port 3
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(3),
},
// Port 4
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(4),
},
// Port 5
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(5),
},
// Port 6
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(6),
},
// Port 7
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(7),
},
// Port 8
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(0),
},
// Port 9
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(1),
},
// Port 10
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(2),
},
// Port 11
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(3),
},
// Port 12
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(4),
},
// Port 13
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(5),
},
// Port 14
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(6),
},
// Port 15
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(7),
},
// Port 16
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(8),
},
// Port 17
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(9),
},
// Port 18
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(10),
},
// Port 19
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(11),
},
// Port 20
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(12),
},
// Port 21
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(13),
},
// Port 22
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(14),
},
// Port 23
PortLocation {
controller: FpgaController::Left,
port: PhysicalPort(15),
},
// Port 24
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(8),
},
// Port 25
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(9),
},
// Port 26
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(10),
},
// Port 27
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(11),
},
// Port 28
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(12),
},
// Port 29
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(13),
},
// Port 30
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(14),
},
// Port 31
PortLocation {
controller: FpgaController::Right,
port: PhysicalPort(15),
},
]);
// These constants represent which logical locations are covered by each FPGA.
// This is convienient in the event that we cannot talk to one of the FPGAs as
// we can know which modules may be impacted.
const LEFT_LOGICAL_MASK: LogicalPortMask = LogicalPortMask(0x00ff00ff);
const RIGHT_LOGICAL_MASK: LogicalPortMask = LogicalPortMask(0xff00ff00);
/// Consolidates per-module success/failure/error information.
///
/// For operations which have no failure path, just success or error, make use
/// of the `ModuleResultNoFailure` type. Since multiple modules can be accessed
/// in parallel, we need to be able to handle a mix of the following cases on a
/// per-module basis:
/// - The module operation succeeded
/// - The module operation failed
/// - The module could not be interacted with due to an FPGA communication error
#[derive(Copy, Clone, Default, PartialEq)]
pub struct ModuleResult {
success: LogicalPortMask,
failure: LogicalPortMask,
error: LogicalPortMask,
failure_types: LogicalPortFailureTypes,
}
/// This is a slimmed down version of ModuleResult for use in the ringbuf
#[derive(Copy, Clone, PartialEq)]
pub struct ModuleResultSlim {
success: LogicalPortMask,
failure: LogicalPortMask,
error: LogicalPortMask,
}
impl From<ModuleResultNoFailure> for ModuleResult {
fn from(r: ModuleResultNoFailure) -> Self {
ModuleResult::new(
r.success(),
LogicalPortMask(0),
r.error(),
LogicalPortFailureTypes::default(),
)
.unwrap_lite()
}
}
impl ModuleResult {
/// Enforces no overlap in the success, failure, and error masks.
pub fn new(
success: LogicalPortMask,
failure: LogicalPortMask,
error: LogicalPortMask,
failure_types: LogicalPortFailureTypes,
) -> Result<Self, TransceiversError> {
if !(success & failure).is_empty()
|| !(success & error).is_empty()
|| !(failure & error).is_empty()
{
return Err(TransceiversError::InvalidModuleResult);
}
Ok(Self {
success,
failure,
error,
failure_types,
})
}
pub fn success(&self) -> LogicalPortMask {
self.success
}
pub fn failure(&self) -> LogicalPortMask {
self.failure
}
pub fn error(&self) -> LogicalPortMask {
self.error
}
pub fn failure_types(&self) -> LogicalPortFailureTypes {
self.failure_types
}
/// Combines two `ModuleResults`
///
/// Intended to be used for a sequence of `ModuleResult` yielding function
/// calls. Building such a sequence is generally done with the following
/// form (where `modules` is a `LogicalPortMask` of requested modules):
///
/// let result = some_result_fn(modules);
/// let next_result = result.chain(another_result_fn(result.success()))
///
/// So the initial result includes some set of success, failure, and error
/// masks which then need to be reconciled with a new set of masks, generally
/// a subset of the success mask of the initial result. Notably, there
/// cannot be overlap between these masks, which this function enforces.
///
/// # Panics
///
/// This function panics if the `next.success` mask is not a subset of
/// self.success. Additionally, it will panic if any of the success,
/// failure, or error masks overlap with one another.
pub fn chain<R: Into<ModuleResult>>(&self, next: R) -> Self {
let next: ModuleResult = next.into();
// success mask is just what the success of the next step is as long
// as next.success is a subset of self.success, ensuring the semantics
// of "chaining"
assert!(next
.success()
.to_indices()
.all(|idx| self.success().is_set(idx)));
let success = next.success();
// combine any new errors with the existing error mask
let error = self.error() | next.error();
// combine any new failures with the existing failure mask. Errors
// supercede failures, so make sure to clear any failures where an error
// has subsequently occurred.
let failure = (self.failure() | next.failure()) & !self.error();
// merge the failure types observed, prefering newer failure types
// should both results have a failure at the same port.
let mut combined_failures = LogicalPortFailureTypes::default();
for p in failure.to_indices() {
if next.failure().is_set(p) {
combined_failures.0[p.0 as usize] =
next.failure_types().0[p.0 as usize];
} else if self.failure().is_set(p) {
combined_failures.0[p.0 as usize] =
self.failure_types().0[p.0 as usize];
}
}
Self::new(success, failure, error, combined_failures).unwrap_lite()
}
/// Helper to provide a nice way to get a ModuleResultSlim from this result
pub fn to_slim(&self) -> ModuleResultSlim {
ModuleResultSlim {
success: self.success(),
failure: self.failure(),
error: self.error(),
}
}
}
/// A type to consolidate per-module success/error information.
///
/// Since multiple modules can be accessed in parallel, we need to be able to
/// handle a mix of the following cases on a per-module basis:
/// - The module operation succeeded
/// - The module could not be interacted with due to an FPGA communication error
#[derive(Copy, Clone, Default, PartialEq)]
pub struct ModuleResultNoFailure {
success: LogicalPortMask,
error: LogicalPortMask,
}
impl ModuleResultNoFailure {
/// Enforces no overlap in the success and error masks.
pub fn new(
success: LogicalPortMask,
error: LogicalPortMask,
) -> Result<Self, TransceiversError> {
if !(success & error).is_empty() {
return Err(TransceiversError::InvalidModuleResult);
}
Ok(Self { success, error })
}
pub fn success(&self) -> LogicalPortMask {
self.success
}
pub fn error(&self) -> LogicalPortMask {
self.error
}
}
/// A type to provide more ergonomic access to the FPGA generated type
pub type FpgaI2CFailure = Reg::QSFP::PORT0_STATUS::Encoded;
/// A type to consolidate per-module failure types.
///
/// Currently the only types of operations that can be considered failures are
/// those that involve the FPGA doing I2C. Thus, that is the only supported type
/// right now.
#[derive(Copy, Clone, PartialEq)]
pub struct LogicalPortFailureTypes(pub [FpgaI2CFailure; NUM_PORTS as usize]);
impl Default for LogicalPortFailureTypes {
fn default() -> Self {
Self([FpgaI2CFailure::NoError; NUM_PORTS as usize])
}
}
impl core::ops::Index<LogicalPort> for LogicalPortFailureTypes {
type Output = FpgaI2CFailure;
fn index(&self, i: LogicalPort) -> &Self::Output {
&self.0[i.0 as usize]
}
}
#[derive(Copy, Clone)]
pub struct PortI2CStatus {
pub done: bool,
pub error: FpgaI2CFailure,
}
impl PortI2CStatus {
pub fn new(status: u8) -> Self {
Self {
done: (status & Reg::QSFP::PORT0_STATUS::BUSY) == 0,
error: FpgaI2CFailure::from_u8(
status & Reg::QSFP::PORT0_STATUS::ERROR,
)
.unwrap_lite(),
}
}
}
impl Transceivers {
pub fn new(fpga_task: userlib::TaskId) -> Self {
Self {
// There are 16 QSFP-DD transceivers connected to each FPGA
fpgas: [
FpgaUserDesign::new(fpga_task, 0),
FpgaUserDesign::new(fpga_task, 1),
],
}
}
pub fn fpga(&self, c: FpgaController) -> &FpgaUserDesign {
&self.fpgas[c as usize]
}
/// Executes a WriteOp (`op`) at `addr` for all ports per the `mask`.
///
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
fn masked_port_op(
&self,
op: WriteOp,
mask: LogicalPortMask,
addr: Addr,
) -> ModuleResultNoFailure {
let mut error = LogicalPortMask(0);
// map the logical mask into a physical one
let fpga_masks: FpgaPortMasks = mask.into();
// talk to both FPGAs
for fpga_index in fpga_masks.iter_fpgas() {
let mask = fpga_masks.get(fpga_index);
if !mask.is_empty() {
let fpga = self.fpga(fpga_index);
let wdata: U16<byteorder::LittleEndian> = U16::new(mask.get());
// mark that an error occurred so we can modify the success mask
if fpga.write(op, addr, wdata).is_err() {
error |= match fpga_index {
FpgaController::Left => LEFT_LOGICAL_MASK,
FpgaController::Right => RIGHT_LOGICAL_MASK,
}
}
}
}
// success is wherever we did not encounter an `FpgaError`
let success = mask & !error;
// only have an error where there was a requested module in mask
error &= mask;
ModuleResultNoFailure::new(success, error).unwrap_lite()
}
/// Set power enable bits per the specified `mask`.
///
/// Controls whether or not a module's hot swap control will be turned on by
/// the FPGA upon module insertion.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn enable_power(&self, mask: LogicalPortMask) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitSet, mask, Addr::QSFP_POWER_EN0)
}
/// Clear power enable bits per the specified `mask`.
///
/// Controls whether or not a module's hot swap control will be turned on by
/// the FPGA upon module insertion.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn disable_power(
&self,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitClear, mask, Addr::QSFP_POWER_EN0)
}
/// Set ResetL bits per the specified `mask`.
///
/// This directly controls the ResetL signal to the module.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn deassert_reset(
&self,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitSet, mask, Addr::QSFP_MOD_RESETL0)
}
/// Clear ResetL bits per the specified `mask`.
///
/// This directly controls the ResetL signal to the module.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn assert_reset(&self, mask: LogicalPortMask) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitClear, mask, Addr::QSFP_MOD_RESETL0)
}
/// Set LpMode bits per the specified `mask`.
///
/// This directly controls the LpMode signal to the module.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn assert_lpmode(
&self,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitSet, mask, Addr::QSFP_MOD_LPMODE0)
}
/// Clear LpMode bits per the specified `mask`.
///
/// This directly controls the LpMode signal to the module.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn deassert_lpmode(
&self,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.masked_port_op(WriteOp::BitClear, mask, Addr::QSFP_MOD_LPMODE0)
}
/// Get the current status of all low speed signals for all ports.
///
/// This is Enable, Reset, LpMode/TxDis, Power Good, Power Good Timeout,
/// Present, and IRQ/RxLos.
/// The meaning of the returned `ModuleResult`:
/// success: we were able to read from the FPGA
/// error: an `FpgaError` occurred
pub fn get_module_status(&self) -> (ModuleStatus, ModuleResultNoFailure) {
let ldata: Option<[U16<byteorder::LittleEndian>; 8]> = self
.fpga(FpgaController::Left)
.read(Addr::QSFP_POWER_EN0)
.ok();
let rdata: Option<[U16<byteorder::LittleEndian>; 8]> = self
.fpga(FpgaController::Right)
.read(Addr::QSFP_POWER_EN0)
.ok();
let mut status_masks: [u32; 8] = [0; 8];
// loop through each logical port
for port in (0..32).map(LogicalPort) {
// Convert to a physical port using PORT_MAP
let port_loc: PortLocation = port.into();
// get the relevant data from the correct FPGA
let local_data = match port_loc.controller {
FpgaController::Left => ldata,
FpgaController::Right => rdata,
};
let Some(local_data) = local_data else { continue };
// loop through the 8 different fields we need to map
for (word, out) in local_data.iter().zip(status_masks.iter_mut()) {
// if the bit is set, update our status mask at the correct
// logical position
let word: PhysicalPortMask = PhysicalPortMask((*word).into());
if word.is_set(port_loc.port) {
*out |= 1 << port.0;
}
}
}
let success = match (ldata, rdata) {
(None, None) => LogicalPortMask(0),
(Some(_), None) => LEFT_LOGICAL_MASK,
(None, Some(_)) => RIGHT_LOGICAL_MASK,
(Some(_), Some(_)) => LEFT_LOGICAL_MASK | RIGHT_LOGICAL_MASK,
};
let error = !success;
(
ModuleStatus::read_from(status_masks.as_bytes()).unwrap_lite(),
ModuleResultNoFailure::new(success, error).unwrap_lite(),
)
}
/// Clear a fault for each port per the specified `mask`.
///
/// The meaning of the
/// returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn clear_power_fault(
&self,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
let mut error = LogicalPortMask(0);
// map the logical mask into the physical one
let fpga_masks: FpgaPortMasks = mask.into();
// talk to both FPGAs
for fpga_index in fpga_masks.iter_fpgas() {
let mask = fpga_masks.get(fpga_index);
if !mask.is_empty() {
let fpga = self.fpga(fpga_index);
for port in 0..16 {
if mask.is_set(PhysicalPort(port))
&& fpga
.write(
WriteOp::Write,
Addr::QSFP_CONTROL_PORT0 as u16
+ u16::from(port),
Reg::QSFP::CONTROL_PORT0::CLEAR_FAULT,
)
.is_err()
{
error |= match fpga_index {
FpgaController::Left => LEFT_LOGICAL_MASK,
FpgaController::Right => RIGHT_LOGICAL_MASK,
}
}
}
}
}
// success is wherever we did not encounter an `FpgaError`
let success = mask & !error;
// only have an error where there was a requested module in mask
error &= mask;
ModuleResultNoFailure::new(success, error).unwrap_lite()
}
/// Initiate an I2C random read on all ports per the specified `mask`.
///
/// The maximum value of `num_bytes` is 128.The meaning of the returned
/// `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn setup_i2c_read(
&self,
reg: u8,
num_bytes: u8,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.setup_i2c_op(true, reg, num_bytes, mask)
}
/// Initiate an I2C write on all ports per the specified `mask`.
///
/// The maximum value of `num_bytes` is 128. The meaning of the
/// returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
pub fn setup_i2c_write(
&self,
reg: u8,
num_bytes: u8,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
self.setup_i2c_op(false, reg, num_bytes, mask)
}
/// Initiate an I2C operation on all ports per the specified `mask`.
///
/// When `is_read` is true, the operation will be a random-read, not a pure
/// I2C read. The maximum value of `num_bytes` is 128.
/// The meaning of the returned `ModuleResultNoFailure`:
/// success: we were able to write to the FPGA
/// error: an `FpgaError` occurred
fn setup_i2c_op(
&self,
is_read: bool,
reg: u8,
num_bytes: u8,
mask: LogicalPortMask,
) -> ModuleResultNoFailure {
let fpga_masks: FpgaPortMasks = mask.into();
let mut success = LogicalPortMask(0);
let i2c_op = if is_read {
// Defaulting to RandomRead, rather than Read, because RandomRead
// sets the reg addr in the target device, then issues a restart to
// do the read at that reg addr. On the other hand, Read just starts
// a read wherever the reg addr is after the last transaction.
TransceiverI2COperation::RandomRead
} else {
TransceiverI2COperation::Write
};
if !fpga_masks.left.is_empty() {
let request = TransceiversI2CRequest {
reg,
num_bytes,
mask: U16::new(fpga_masks.left.0),
op: i2c_op as u8,
};
if self
.fpga(FpgaController::Left)
.write(WriteOp::Write, Addr::QSFP_I2C_REG_ADDR, request)
.is_ok()
{
success |= LEFT_LOGICAL_MASK;
}
}
if !fpga_masks.right.is_empty() {
let request = TransceiversI2CRequest {
reg,
num_bytes,