Skip to content

Commit 8c01d63

Browse files
authored
feat: added TryFrom<[u32; 4]> to Command/Transfer TRB structs (#137)
* added TryFrom<[u32; 4]> to Command/Transfer TRB structs * fixed wrong bit range specifications * applied fmt
1 parent 658cba1 commit 8c01d63

File tree

5 files changed

+243
-31
lines changed

5 files changed

+243
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## Unreleased - ReleaseDate
4+
### Added
5+
- Add the implementation of `TryFrom<[u32; 4]>` to TRB structs in `ring::trb::command` and `ring::trb::transfer`
46

57
## 0.8.4 - 2022-05-29
68
### Fixed

src/ring/trb/command.rs

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Command TRBs.
22
3-
use super::Link;
3+
use super::{Link, Type};
44
use bit_field::BitField;
55
use core::convert::TryInto;
6+
use num_traits::FromPrimitive;
67

78
allowed! {
89
/// TRBs which are allowed to be pushed to the Command Ring.
@@ -45,17 +46,66 @@ allowed! {
4546
SetExtendedProperty
4647
}
4748
}
49+
impl TryFrom<[u32; 4]> for Allowed {
50+
type Error = [u32; 4];
51+
52+
fn try_from(raw: [u32; 4]) -> Result<Self, Self::Error> {
53+
try_from!(
54+
raw =>
55+
Link,
56+
EnableSlot,
57+
DisableSlot,
58+
AddressDevice,
59+
ConfigureEndpoint,
60+
EvaluateContext,
61+
ResetEndpoint,
62+
StopEndpoint,
63+
SetTrDequeuePointer,
64+
ResetDevice,
65+
ForceEvent,
66+
NegotiateBandwidth,
67+
SetLatencyToleranceValue,
68+
GetPortBandwidth,
69+
ForceHeader,
70+
Noop(Command),
71+
GetExtendedProperty,
72+
SetExtendedProperty,
73+
);
74+
Err(raw)
75+
}
76+
}
4877

4978
add_trb_with_default!(Noop, "No Op Command TRB", Type::NoopCommand);
79+
reserved!(Noop(Type::NoopCommand) {
80+
[0]0..=31;
81+
[1]0..=31;
82+
[2]0..=31;
83+
[3]1..=9;
84+
[3]21..=31;
85+
});
5086
impl_debug_for_trb!(Noop {});
5187

5288
add_trb_with_default!(EnableSlot, "Enable Slot Command TRB", Type::EnableSlot);
89+
reserved!(EnableSlot(Type::EnableSlot) {
90+
[0]0..=31;
91+
[1]0..=31;
92+
[2]0..=31;
93+
[3]1..=9;
94+
[3]21..=31;
95+
});
5396
impl EnableSlot {
5497
rw_field!([3](16..=20), slot_type, "Slot Type", u8);
5598
}
5699
impl_debug_for_trb!(EnableSlot { slot_type });
57100

58101
add_trb_with_default!(DisableSlot, "Disable Slot Command TRB", Type::DisableSlot);
102+
reserved!(DisableSlot(Type::DisableSlot) {
103+
[0]0..=31;
104+
[1]0..=31;
105+
[2]0..=31;
106+
[3]1..=9;
107+
[3]16..=23;
108+
});
59109
impl DisableSlot {
60110
rw_field!([3](24..=31), slot_id, "Slot ID", u8);
61111
}
@@ -66,6 +116,12 @@ add_trb_with_default!(
66116
"Address Device Command TRB",
67117
Type::AddressDevice
68118
);
119+
reserved!(AddressDevice(Type::AddressDevice) {
120+
[0]0..=3;
121+
[2]0..=31;
122+
[3]1..=8;
123+
[3]16..=23;
124+
});
69125
impl AddressDevice {
70126
/// Sets the value of the Input Context Pointer field.
71127
///
@@ -114,6 +170,12 @@ add_trb_with_default!(
114170
"Configure Endpoint Command TRB",
115171
Type::ConfigureEndpoint
116172
);
173+
reserved!(ConfigureEndpoint(Type::ConfigureEndpoint) {
174+
[0]0..=3;
175+
[2]0..=31;
176+
[3]1..=8;
177+
[3]16..=23;
178+
});
117179
impl ConfigureEndpoint {
118180
/// Sets the value of the Input Context Pointer field.
119181
///
@@ -158,6 +220,12 @@ add_trb_with_default!(
158220
"Evaluate Context Command TRB",
159221
Type::EvaluateContext
160222
);
223+
reserved!(EvaluateContext(Type::EvaluateContext) {
224+
[0]0..=3;
225+
[2]0..=31;
226+
[3]1..=8;
227+
[3]16..=23;
228+
});
161229
impl EvaluateContext {
162230
/// Sets the value of the Input Context Pointer field.
163231
///
@@ -199,6 +267,13 @@ add_trb_with_default!(
199267
"Reset Endpoint Command TRB",
200268
Type::ResetEndpoint
201269
);
270+
reserved!(ResetEndpoint(Type::ResetEndpoint) {
271+
[0]0..=31;
272+
[1]0..=31;
273+
[2]0..=31;
274+
[3]1..=8;
275+
[3]21..=23;
276+
});
202277
impl ResetEndpoint {
203278
rw_bit!([3](9), transfer_state_preserve, "Transfer State Preserve");
204279
rw_field!([3](16..=20), endpoint_id, "Endpoint ID", u8);
@@ -215,6 +290,13 @@ add_trb_with_default!(
215290
"Stop Endpoint Command TRB",
216291
Type::StopEndpoint
217292
);
293+
reserved!(StopEndpoint(Type::StopEndpoint) {
294+
[0]0..=31;
295+
[1]0..=31;
296+
[2]0..=31;
297+
[3]1..=9;
298+
[3]21..=22;
299+
});
218300
impl StopEndpoint {
219301
rw_field!([3](16..=20), endpoint_id, "Endpoint ID", u8);
220302
rw_bit!([3](23), suspend, "Suspend");
@@ -231,6 +313,11 @@ add_trb_with_default!(
231313
"Set TR Dequeue Pointer Command TRB",
232314
Type::SetTrDequeuePointer
233315
);
316+
reserved!(SetTrDequeuePointer(Type::SetTrDequeuePointer) {
317+
[2]0..=15;
318+
[3]1..=9;
319+
[3]21..=23;
320+
});
234321
impl SetTrDequeuePointer {
235322
rw_bit!([0](0), dequeue_cycle_state, "Dequeue Cycle State");
236323
rw_field!([0](1..=3), stream_context_type, "Stream Context Type", u8);
@@ -278,12 +365,25 @@ impl_debug_for_trb!(SetTrDequeuePointer {
278365
});
279366

280367
add_trb_with_default!(ResetDevice, "Reset Device Command TRB", Type::ResetDevice);
368+
reserved!(ResetDevice(Type::ResetDevice) {
369+
[0]0..=31;
370+
[1]0..=31;
371+
[2]0..=31;
372+
[3]1..=9;
373+
[3]16..=23;
374+
});
281375
impl ResetDevice {
282376
rw_field!([3](24..=31), slot_id, "Slot ID", u8);
283377
}
284378
impl_debug_for_trb!(ResetDevice { slot_id });
285379

286380
add_trb_with_default!(ForceEvent, "Force Event Command TRB", Type::ForceEvent);
381+
reserved!(ForceEvent(Type::ForceEvent) {
382+
[0]0..=3;
383+
[2]0..=21;
384+
[3]1..=9;
385+
[3]24..=31;
386+
});
287387
impl ForceEvent {
288388
/// Sets the value of the Event TRB Pointer field.
289389
///
@@ -330,6 +430,13 @@ add_trb_with_default!(
330430
"Negotiate Bandwidth Command TRB",
331431
Type::NegotiateBandwidth
332432
);
433+
reserved!(NegotiateBandwidth(Type::NegotiateBandwidth) {
434+
[0]0..=31;
435+
[1]0..=31;
436+
[2]0..=31;
437+
[3]1..=9;
438+
[3]16..=23;
439+
});
333440
impl NegotiateBandwidth {
334441
rw_field!([3](24..=31), slot_id, "Slot ID", u8);
335442
}
@@ -340,6 +447,13 @@ add_trb_with_default!(
340447
"Set Latency Tolerance Value Command TRB",
341448
Type::SetLatencyToleranceValue
342449
);
450+
reserved!(SetLatencyToleranceValue(Type::SetLatencyToleranceValue) {
451+
[0]0..=31;
452+
[1]0..=31;
453+
[2]0..=31;
454+
[3]1..=9;
455+
[3]28..=31;
456+
});
343457
impl SetLatencyToleranceValue {
344458
rw_field!(
345459
[3](16..=27),
@@ -357,6 +471,12 @@ add_trb_with_default!(
357471
"Get Port Bandwidth Command TRB",
358472
Type::GetPortBandwidth
359473
);
474+
reserved!(GetPortBandwidth(Type::GetPortBandwidth) {
475+
[0]0..=3;
476+
[2]0..=31;
477+
[3]1..=9;
478+
[3]20..=23;
479+
});
360480
impl GetPortBandwidth {
361481
/// Sets the value of the Port Bandwidth Context Pointer field.
362482
///
@@ -396,6 +516,10 @@ impl_debug_for_trb!(GetPortBandwidth {
396516
});
397517

398518
add_trb_with_default!(ForceHeader, "Force Header Command TRB", Type::ForceHeader);
519+
reserved!(ForceHeader(Type::ForceHeader) {
520+
[3]1..=9;
521+
[3]16..=23;
522+
});
399523
impl ForceHeader {
400524
rw_field!([0](0..=4), packet_type, "Packet Type", u8);
401525

@@ -440,6 +564,11 @@ add_trb_with_default!(
440564
"Get Extended Property Command TRB",
441565
Type::GetExtendedProperty
442566
);
567+
reserved!(GetExtendedProperty(Type::GetExtendedProperty) {
568+
[0]0..=3;
569+
[2]16..=31;
570+
[3]1..=9;
571+
});
443572
impl GetExtendedProperty {
444573
/// Sets the value of the Extended Property Context Pointer field.
445574
///
@@ -493,6 +622,12 @@ add_trb_with_default!(
493622
"Set Extended Property Command TRB",
494623
Type::SetExtendedProperty
495624
);
625+
reserved!(SetExtendedProperty(Type::SetExtendedProperty) {
626+
[0]0..=31;
627+
[1]0..=31;
628+
[2]24..=31;
629+
[3]1..=9;
630+
});
496631
impl SetExtendedProperty {
497632
rw_field!(
498633
[2](0..=15),

src/ring/trb/event.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Event TRBs.
22
3+
use super::Type;
34
use bit_field::BitField;
45
use core::convert::{TryFrom, TryInto};
56
use num_derive::FromPrimitive;
@@ -30,23 +31,17 @@ impl TryFrom<[u32; 4]> for Allowed {
3031
type Error = [u32; 4];
3132

3233
fn try_from(raw: [u32; 4]) -> Result<Self, Self::Error> {
33-
macro_rules! try_from {
34-
($name:ident) => {
35-
if let Ok(t) = $name::try_from(raw) {
36-
return Ok(Self::$name(t));
37-
}
38-
};
39-
}
40-
41-
try_from!(TransferEvent);
42-
try_from!(CommandCompletion);
43-
try_from!(PortStatusChange);
44-
try_from!(BandwidthRequest);
45-
try_from!(Doorbell);
46-
try_from!(HostController);
47-
try_from!(DeviceNotification);
48-
try_from!(MfindexWrap);
49-
34+
try_from!(
35+
raw =>
36+
TransferEvent,
37+
CommandCompletion,
38+
PortStatusChange,
39+
BandwidthRequest,
40+
Doorbell,
41+
HostController,
42+
DeviceNotification,
43+
MfindexWrap,
44+
);
5045
Err(raw)
5146
}
5247
}
@@ -99,7 +94,7 @@ reserved!(PortStatusChange(Type::PortStatusChange){
9994
[1]0..=31;
10095
[2]0..=23;
10196
[3]1..=9;
102-
[3]16..=31
97+
[3]16..=31;
10398
});
10499
impl PortStatusChange {
105100
ro_field!([0](24..=31), port_id, "Port ID", u8);
@@ -110,7 +105,7 @@ event!(TransferEvent, "Transfer Event TRB", Type::TransferEvent);
110105
reserved!(TransferEvent(Type::TransferEvent){
111106
[3]1..=1;
112107
[3]3..=9;
113-
[3]21..=23
108+
[3]21..=23;
114109
});
115110
impl TransferEvent {
116111
/// Returns the value of the TRB Pointer field.
@@ -142,7 +137,7 @@ event!(
142137
);
143138
reserved!(CommandCompletion(Type::CommandCompletion){
144139
[0]0..=3;
145-
[3]1..=9
140+
[3]1..=9;
146141
});
147142
impl CommandCompletion {
148143
/// Returns the value of the Command TRB Pointer field.
@@ -180,7 +175,7 @@ reserved!(BandwidthRequest(Type::BandwidthRequest){
180175
[1]0..=31;
181176
[2]0..=23;
182177
[3]1..=9;
183-
[3]16..=23
178+
[3]16..=23;
184179
});
185180
impl BandwidthRequest {
186181
ro_field!([3](24..=31), slot_id, "Slot ID", u8);
@@ -192,7 +187,7 @@ reserved!(Doorbell(Type::Doorbell){
192187
[0]5..=31;
193188
[1]0..=31;
194189
[2]0..=23;
195-
[3]1..=9
190+
[3]1..=9;
196191
});
197192
impl Doorbell {
198193
ro_field!([0](0..=4), db_reason, "DB Reason", u8);
@@ -215,7 +210,7 @@ reserved!(HostController(Type::HostController){
215210
[1]0..=31;
216211
[2]0..=23;
217212
[3]1..=9;
218-
[3]16..=31
213+
[3]16..=31;
219214
});
220215
impl_debug_for_event_trb!(HostController {});
221216

@@ -229,7 +224,7 @@ reserved!(DeviceNotification(Type::DeviceNotification){
229224
[1]0..=31;
230225
[2]0..=23;
231226
[3]1..=9;
232-
[3]16..=31
227+
[3]16..=31;
233228
});
234229
impl DeviceNotification {
235230
ro_field!([0](4..=7), notification_type, "Notification Type", u8);
@@ -256,7 +251,7 @@ reserved!(MfindexWrap(Type::MfindexWrap){
256251
[0]0..=3;
257252
[2]0..=23;
258253
[3]1..=9;
259-
[3]16..=23
254+
[3]16..=23;
260255
});
261256
impl_debug_for_event_trb!(MfindexWrap {});
262257

0 commit comments

Comments
 (0)