firewire_dice_protocols/tcat/extension/
current_config_section.rs

1// SPDX-License-Identifier: LGPL-3.0-or-later
2// Copyright (c) 2020 Takashi Sakamoto
3
4//! Current configuration section in protocol extension defined by TCAT for ASICs of DICE.
5//!
6//! The module includes structure, enumeration, and trait and its implementation for current
7//! configuration section in protocol extension defined by TCAT for ASICs of DICE.
8
9use super::{
10    router_entry::*,
11    stream_format_entry::*,
12    {caps_section::*, cmd_section::*, *},
13};
14
15/// Parameters of router entries in current configuration section.
16#[derive(Default, Debug, Clone, PartialEq, Eq)]
17pub struct CurrentRouterParams {
18    pub entries: RouterParams,
19    pub rate_mode: RateMode,
20}
21
22/// Parameters of stream format entries in current configuration section.
23#[derive(Default, Debug, Clone, PartialEq, Eq)]
24pub struct CurrentStreamFormatParams {
25    pub pair: StreamFormatParams,
26    pub rate_mode: RateMode,
27}
28
29const LOW_ROUTER_CONFIG_OFFSET: usize = 0x0000;
30const LOW_STREAM_CONFIG_OFFSET: usize = 0x1000;
31const MID_ROUTER_CONFIG_OFFSET: usize = 0x2000;
32const MID_STREAM_CONFIG_OFFSET: usize = 0x3000;
33const HIGH_ROUTER_CONFIG_OFFSET: usize = 0x4000;
34const HIGH_STREAM_CONFIG_OFFSET: usize = 0x5000;
35
36impl<O: TcatExtensionOperation> TcatExtensionSectionParamsOperation<CurrentRouterParams> for O {
37    fn cache_extension_whole_params(
38        req: &FwReq,
39        node: &FwNode,
40        sections: &ExtensionSections,
41        caps: &ExtensionCaps,
42        params: &mut CurrentRouterParams,
43        timeout_ms: u32,
44    ) -> Result<(), Error> {
45        if !caps.router.is_exposed {
46            let msg = "Router configuration is not exposed.";
47            Err(Error::new(ProtocolExtensionError::CurrentConfig, &msg))?;
48        }
49
50        let offset = match params.rate_mode {
51            RateMode::Low => LOW_ROUTER_CONFIG_OFFSET,
52            RateMode::Middle => MID_ROUTER_CONFIG_OFFSET,
53            RateMode::High => HIGH_ROUTER_CONFIG_OFFSET,
54        };
55
56        let mut raw = vec![0u8; 4];
57        Self::read_extension(
58            req,
59            node,
60            &sections.current_config,
61            offset,
62            &mut raw,
63            timeout_ms,
64        )?;
65
66        let mut val = 0u32;
67        deserialize_u32(&mut val, &raw[..4]);
68
69        let entry_count = std::cmp::min(val as usize, caps.router.maximum_entry_count as usize);
70
71        params.entries.0.resize_with(entry_count, Default::default);
72        raw.resize_with(
73            4 + calculate_router_entries_size(entry_count),
74            Default::default,
75        );
76
77        Self::read_extension(
78            req,
79            node,
80            &sections.current_config,
81            offset + 4,
82            &mut raw[4..],
83            timeout_ms,
84        )?;
85
86        deserialize_router_entries(&mut params.entries.0, &raw[4..])
87            .map_err(|cause| Error::new(ProtocolExtensionError::CurrentConfig, &cause))
88    }
89}
90
91impl<O: TcatExtensionOperation> TcatExtensionSectionParamsOperation<CurrentStreamFormatParams>
92    for O
93{
94    fn cache_extension_whole_params(
95        req: &FwReq,
96        node: &FwNode,
97        sections: &ExtensionSections,
98        caps: &ExtensionCaps,
99        params: &mut CurrentStreamFormatParams,
100        timeout_ms: u32,
101    ) -> Result<(), Error> {
102        let offset = match params.rate_mode {
103            RateMode::Low => LOW_STREAM_CONFIG_OFFSET,
104            RateMode::Middle => MID_STREAM_CONFIG_OFFSET,
105            RateMode::High => HIGH_STREAM_CONFIG_OFFSET,
106        };
107
108        let size = calculate_stream_format_entries_size(
109            caps.general.max_tx_streams as usize,
110            caps.general.max_rx_streams as usize,
111        );
112        let mut raw = vec![0u8; size];
113        Self::read_extension(
114            req,
115            node,
116            &sections.current_config,
117            offset,
118            &mut raw,
119            timeout_ms,
120        )?;
121
122        let mut val = 0u32;
123        deserialize_u32(&mut val, &raw[..4]);
124        let tx_entries_count = val as usize;
125        params
126            .pair
127            .tx_entries
128            .resize_with(tx_entries_count, Default::default);
129
130        deserialize_u32(&mut val, &raw[4..8]);
131        let rx_entries_count = val as usize;
132        params
133            .pair
134            .rx_entries
135            .resize_with(rx_entries_count, Default::default);
136
137        deserialize_stream_format_entries(
138            (&mut params.pair.tx_entries, &mut params.pair.rx_entries),
139            &raw,
140        )
141        .map_err(|cause| Error::new(ProtocolExtensionError::CurrentConfig, &cause))
142    }
143}