feat(wifi): let an RTL8822C sleep through quiet periods
This commit is contained in:
1 parent
35cac60348
commit
0d364f7fa1
13 files changed
+461
-57
No files matched your search
@@ -31,4 +31,5 @@ pub mod regd;
|
||||
pub mod registers;
|
||||
pub mod rf;
|
||||
pub mod station;
|
||||
pub mod templates;
|
||||
pub mod transmit;
|
||||
@@ -144,7 +144,7 @@ pub fn descriptors(
|
||||
address: u32,
|
||||
band: FirmwareBand,
|
||||
) -> Result<([u8; 48], [u8; 16]), Error> {
|
||||
if !(8..=MAX_DATA).contains(&data.len()) {
|
||||
if !(1..=MAX_DATA).contains(&data.len()) {
|
||||
return Err(Error::InvalidLength);
|
||||
}
|
||||
address
|
||||
@@ -160,7 +160,11 @@ pub fn descriptors(
|
||||
for (word, value) in [
|
||||
(
|
||||
0,
|
||||
data.len() as u32 | 48 << 16 | u32::from(data[4] & 1) << 24 | 1 << 26 | 1 << 31,
|
||||
data.len() as u32
|
||||
| 48 << 16
|
||||
| u32::from(data.get(4).is_some_and(|byte| byte & 1 != 0)) << 24
|
||||
| 1 << 26
|
||||
| 1 << 31,
|
||||
),
|
||||
(1, 16 << 8 | rate_id << 16),
|
||||
(3, 1 << 8 | 1 << 10),
|
||||
|
||||
@@ -436,7 +436,7 @@ fn source_packet_bounds_psb_rounding_multicast_and_band_fields_are_preserved() {
|
||||
}
|
||||
}
|
||||
}
|
||||
for size in [0, 1, 7, 4097] {
|
||||
for size in [0, 4097] {
|
||||
assert_eq!(
|
||||
descriptors(&vec![0; size], 0x1000, FirmwareBand::Ghz2),
|
||||
Err(Error::InvalidLength)
|
||||
|
||||
@@ -19,6 +19,10 @@ const CALIBRATION: u32 = 0x6d;
|
||||
/// `C2H_SCAN_RESULT`.
|
||||
const SCAN_RESULT: u8 = 0x38;
|
||||
|
||||
/// `H2C_CMD_RSVD_PAGE` and `H2C_CMD_SET_PWR_MODE`.
|
||||
const RESERVED_PAGES: u32 = 0;
|
||||
const POWER_MODE: u32 = 0x20;
|
||||
|
||||
/// `C2H_CCX_TX_RPT`.
|
||||
const CCX_TX_REPORT: u8 = 0x03;
|
||||
/// `C2H_RA_RPT`.
|
||||
@@ -27,6 +31,28 @@ const RA_REPORT: u8 = 0x0c;
|
||||
const HALMAC: u8 = 0xff;
|
||||
const HALMAC_CCX_REPORT: u8 = 0x0f;
|
||||
|
||||
/// `rtw_send_rsvd_page_h2c`, with no probe response among the pages.
|
||||
pub fn reserved_pages(ps_poll: u8, null: u8, qos_null: u8) -> (u32, u32) {
|
||||
(
|
||||
RESERVED_PAGES | (u32::from(ps_poll) << 16) | (u32::from(null) << 24),
|
||||
u32::from(qos_null),
|
||||
)
|
||||
}
|
||||
|
||||
/// `rtw_fw_set_pwr_mode` as `rtw_enter_lps_core` and `rtw_leave_lps_core` set
|
||||
/// it for the station on `port`.
|
||||
pub fn power_mode(leisure: bool, port: u8) -> (u32, u32) {
|
||||
let (mode, rlbm, smart, state) = if leisure {
|
||||
(1, 1, 2, 0)
|
||||
} else {
|
||||
(0, 0, 0, 0xc)
|
||||
};
|
||||
(
|
||||
POWER_MODE | (mode << 8) | (rlbm << 16) | (smart << 20) | (1 << 24),
|
||||
(u32::from(port) << 5) | (state << 8),
|
||||
)
|
||||
}
|
||||
|
||||
/// `rtw_fw_media_status_report`.
|
||||
pub fn media_status(mac_id: u8, connected: bool) -> (u32, u32) {
|
||||
(
|
||||
@@ -166,6 +192,9 @@ mod tests {
|
||||
fn commands_keep_their_fields_in_rtw88s_bits() {
|
||||
assert_eq!(media_status(1, true), (0x0001_0101, 0));
|
||||
assert_eq!(default_port(0, 1), (0x0001_002c, 0));
|
||||
assert_eq!(reserved_pages(1, 3, 2), (0x0301_0000, 2));
|
||||
assert_eq!(power_mode(true, 0), (0x0121_0120, 0));
|
||||
assert_eq!(power_mode(false, 0), (0x0100_0020, 0xc00));
|
||||
let (message, mask) = ra_info(RateInfo {
|
||||
mac_id: 1,
|
||||
rate_id: 9,
|
||||
|
||||
@@ -32,6 +32,10 @@ pub struct Packet {
|
||||
pub report: Option<u8>,
|
||||
pub fixed_rate: bool,
|
||||
pub rts: bool,
|
||||
/// `nav_use_hdr`: the frame's own duration field holds its AID.
|
||||
pub navigation_from_header: bool,
|
||||
/// `bt_null`: firmware sends this to tell the AP it left for Bluetooth.
|
||||
pub bluetooth_null: bool,
|
||||
pub rate: u8,
|
||||
pub width: u8,
|
||||
pub ldpc: bool,
|
||||
@@ -77,10 +81,12 @@ impl Packet {
|
||||
| bit(self.queue == QSEL_HIGH, 29);
|
||||
words[2] = bit(self.aggregate.is_some(), 12)
|
||||
| bit(self.report.is_some(), 19)
|
||||
| (u32::from(density & 7) << 20);
|
||||
| (u32::from(density & 7) << 20)
|
||||
| bit(self.bluetooth_null, 23);
|
||||
words[3] = bit(self.fixed_rate, 8)
|
||||
| bit(self.fixed_rate, 10)
|
||||
| bit(self.rts, 12)
|
||||
| bit(self.navigation_from_header, 15)
|
||||
| (u32::from(factor & 0x1f) << 17);
|
||||
words[4] = u32::from(self.rate & 0x7f);
|
||||
words[5] =
|
||||
|
||||
@@ -18,6 +18,56 @@ const BCN_VALID: u16 = 1 << 15;
|
||||
const MAX_POLLS: usize = 1000;
|
||||
const CHUNK: usize = 4096;
|
||||
|
||||
fn poll<I: Io>(io: &mut I, register: u32, mask: u32, target: u32) -> Result<(), Error> {
|
||||
for _ in 0..MAX_POLLS {
|
||||
let value = io.read32(register)?;
|
||||
// Removed/unmapped devices must not satisfy all-set ready masks.
|
||||
if value == u32::MAX {
|
||||
return Err(Error::Register);
|
||||
}
|
||||
if value & mask == target {
|
||||
return Ok(());
|
||||
}
|
||||
io.delay_us(10);
|
||||
}
|
||||
Err(Error::Timeout { register, mask })
|
||||
}
|
||||
|
||||
/// `rtw_fw_write_data_rsvd_page` of `data` at page `head`, with the beacon
|
||||
/// queue's head left at `boundary`.
|
||||
pub fn write_reserved<I: Io>(
|
||||
io: &mut I,
|
||||
head: u16,
|
||||
boundary: u16,
|
||||
data: &[u8],
|
||||
) -> Result<(), Error> {
|
||||
let beacon = io.read8(REG_BCN_CTRL)?;
|
||||
io.write16(REG_FIFOPAGE_CTRL_2, head | BCN_VALID)?;
|
||||
let cr = io.read8(REG_CR + 1)?;
|
||||
io.write8(REG_CR + 1, cr | 1)?;
|
||||
io.write8(REG_BCN_CTRL, (beacon & !8) | 16)?;
|
||||
let txq = io.read8(REG_FWHW_TXQ_CTRL + 2)?;
|
||||
io.write8(REG_FWHW_TXQ_CTRL + 2, txq & !64)?;
|
||||
let result = io.submit_beacon(data).and_then(|()| {
|
||||
poll(
|
||||
io,
|
||||
REG_FIFOPAGE_CTRL_2,
|
||||
u32::from(BCN_VALID),
|
||||
u32::from(BCN_VALID),
|
||||
)?;
|
||||
// SAFETY: cleared BCN_VALID before this exact payload,
|
||||
// then observed its new hardware event under exclusive I/O.
|
||||
unsafe { io.beacon_consumed() }
|
||||
});
|
||||
// Restore even for queue/valid-bit failure. An error in restoration is
|
||||
// itself uncertain; outer start quarantines all remaining resources.
|
||||
io.write16(REG_FIFOPAGE_CTRL_2, boundary | BCN_VALID)?;
|
||||
io.write8(REG_BCN_CTRL, beacon)?;
|
||||
io.write8(REG_FWHW_TXQ_CTRL + 2, txq)?;
|
||||
io.write8(REG_CR + 1, cr)?;
|
||||
result
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum StartError {
|
||||
Hardware(Error),
|
||||
@@ -161,18 +211,7 @@ impl<I: Io> Download<I> {
|
||||
self.io.write8(register, (value & !clear) | set)
|
||||
}
|
||||
fn poll(&mut self, register: u32, mask: u32, target: u32) -> Result<(), Error> {
|
||||
for _ in 0..MAX_POLLS {
|
||||
let value = self.io.read32(register)?;
|
||||
// Removed/unmapped devices must not satisfy all-set ready masks.
|
||||
if value == u32::MAX {
|
||||
return Err(Error::Register);
|
||||
}
|
||||
if value & mask == target {
|
||||
return Ok(());
|
||||
}
|
||||
self.io.delay_us(10);
|
||||
}
|
||||
Err(Error::Timeout { register, mask })
|
||||
poll(&mut self.io, register, mask, target)
|
||||
}
|
||||
fn cpu(&mut self, enabled: bool) -> Result<(), Error> {
|
||||
if enabled {
|
||||
@@ -245,31 +284,7 @@ impl<I: Io> Download<I> {
|
||||
}
|
||||
}
|
||||
fn reserved_page(&mut self, data: &[u8]) -> Result<(), Error> {
|
||||
let beacon = self.io.read8(REG_BCN_CTRL)?;
|
||||
self.io.write16(REG_FIFOPAGE_CTRL_2, BCN_VALID)?;
|
||||
let cr = self.io.read8(REG_CR + 1)?;
|
||||
self.io.write8(REG_CR + 1, cr | 1)?;
|
||||
self.io.write8(REG_BCN_CTRL, (beacon & !8) | 16)?;
|
||||
let txq = self.io.read8(REG_FWHW_TXQ_CTRL + 2)?;
|
||||
self.io.write8(REG_FWHW_TXQ_CTRL + 2, txq & !64)?;
|
||||
let result = self.io.submit_beacon(data).and_then(|()| {
|
||||
self.poll(
|
||||
REG_FIFOPAGE_CTRL_2,
|
||||
u32::from(BCN_VALID),
|
||||
u32::from(BCN_VALID),
|
||||
)?;
|
||||
// SAFETY: cleared BCN_VALID before this exact payload,
|
||||
// then observed its new hardware event under exclusive I/O.
|
||||
unsafe { self.io.beacon_consumed() }
|
||||
});
|
||||
// Restore even for queue/valid-bit failure. An error in restoration is
|
||||
// itself uncertain; outer start quarantines all remaining resources.
|
||||
self.io
|
||||
.write16(REG_FIFOPAGE_CTRL_2, self.boundary | BCN_VALID)?;
|
||||
self.io.write8(REG_BCN_CTRL, beacon)?;
|
||||
self.io.write8(REG_FWHW_TXQ_CTRL + 2, txq)?;
|
||||
self.io.write8(REG_CR + 1, cr)?;
|
||||
result
|
||||
write_reserved(&mut self.io, 0, self.boundary, data)
|
||||
}
|
||||
fn region(&mut self, destination: u32, data: &[u8]) -> Result<(), Error> {
|
||||
let control = self.io.read32(REG_DDMA_CH0CTRL)?;
|
||||
|
||||
@@ -57,6 +57,8 @@ const COVERAGE_MAX: u8 = 0x2a;
|
||||
const COVERAGE_MID: u8 = 0x26;
|
||||
/// `DIG_CVRG_MIN`, where a scan listens widest.
|
||||
pub const MAX_COVERAGE: u8 = 0x1c;
|
||||
/// `RTW_LPS_THRESHOLD`.
|
||||
const LPS_THRESHOLD: u32 = 50;
|
||||
const RSSI_GAIN_OFFSET: u8 = 15;
|
||||
/// `DECLARE_EWMA(rssi, 10, 16)`.
|
||||
const AVERAGE_PRECISION: u32 = 10;
|
||||
@@ -564,6 +566,9 @@ pub struct Dynamic {
|
||||
evm_db: u8,
|
||||
/// Unicast data bytes this period, and their averaged megabits.
|
||||
tx_bytes: u64,
|
||||
/// `stats.tx_cnt` and `stats.rx_cnt`.
|
||||
tx_frames: u32,
|
||||
rx_frames: u32,
|
||||
rx_bytes: u64,
|
||||
tx_throughput: u64,
|
||||
rx_throughput: u64,
|
||||
@@ -614,6 +619,8 @@ impl Dynamic {
|
||||
rx_rate: 0,
|
||||
evm_db: 0,
|
||||
tx_bytes: 0,
|
||||
tx_frames: 0,
|
||||
rx_frames: 0,
|
||||
rx_bytes: 0,
|
||||
tx_throughput: 0,
|
||||
rx_throughput: 0,
|
||||
@@ -643,6 +650,7 @@ impl Dynamic {
|
||||
pub fn frame(&mut self, bytes: &[u8], signal: Option<Signal>, address: Mac, bssid: Mac) {
|
||||
if is_unicast_data(bytes) {
|
||||
self.rx_bytes += bytes.len() as u64;
|
||||
self.rx_frames += 1;
|
||||
}
|
||||
let Some(signal) = signal else {
|
||||
return;
|
||||
@@ -674,15 +682,20 @@ impl Dynamic {
|
||||
pub fn sent(&mut self, mpdu: &[u8]) {
|
||||
if is_unicast_data(mpdu) {
|
||||
self.tx_bytes += mpdu.len() as u64;
|
||||
self.tx_frames += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// `rtw_watch_dog_work`'s accounts of the period that ended.
|
||||
pub fn close_period(&mut self) {
|
||||
/// `rtw_watch_dog_work`'s accounts of the period that ended, and whether
|
||||
/// it was too busy for power save.
|
||||
pub fn close_period(&mut self) -> bool {
|
||||
let tx_frames = core::mem::take(&mut self.tx_frames);
|
||||
let rx_frames = core::mem::take(&mut self.rx_frames);
|
||||
let tx = core::mem::take(&mut self.tx_bytes) >> MBPS_SHIFT;
|
||||
let rx = core::mem::take(&mut self.rx_bytes) >> MBPS_SHIFT;
|
||||
average_in(&mut self.tx_throughput, tx, THROUGHPUT_WEIGHT);
|
||||
average_in(&mut self.rx_throughput, rx, THROUGHPUT_WEIGHT);
|
||||
tx_frames > LPS_THRESHOLD || rx_frames > LPS_THRESHOLD
|
||||
}
|
||||
|
||||
/// `rtw_fw_update_wl_phy_info`.
|
||||
|
||||
@@ -11,6 +11,7 @@ use super::{
|
||||
coex::{self, Coex, Link},
|
||||
commands::{self, Report},
|
||||
descriptor::Packet,
|
||||
download,
|
||||
dynamic::{self, Dynamic, set_mask},
|
||||
features,
|
||||
firmware_control::Control,
|
||||
@@ -23,7 +24,8 @@ use super::{
|
||||
receive::{self, Ring},
|
||||
regd,
|
||||
registers::{REG_BCN_CTRL, REG_CR},
|
||||
station::{CamKey, Device, Incoming, MAC_ID, Received},
|
||||
station::{CamKey, Device, Incoming, MAC_ID, PORT, Received, Watched},
|
||||
templates::Templates,
|
||||
transmit::{self, Queue, Rings},
|
||||
};
|
||||
use crate::{
|
||||
@@ -38,6 +40,13 @@ use crate::{
|
||||
use alloc::{format, string::String, vec::Vec};
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// `REG_TCR`'s `BIT_PWRMGT_HWDATA_EN`.
|
||||
const REG_TCR: u32 = 0x604;
|
||||
const POWER_BIT_BY_HARDWARE: u32 = 1 << 7;
|
||||
/// `LEAVE_LPS_TRY_CNT` and its 20 ms.
|
||||
const LEAVE_POWER_SAVE_TRIES: u32 = 5;
|
||||
const LEAVE_POWER_SAVE_WAIT_US: u64 = 20_000;
|
||||
|
||||
/// `rtw_vif_port[0]`.
|
||||
const REG_MAC_ADDRESS: u32 = 0x610;
|
||||
const REG_BSSID: u32 = 0x618;
|
||||
@@ -138,6 +147,8 @@ pub struct Hardware {
|
||||
coex: Coex,
|
||||
report: features::Report,
|
||||
pages: &'static dyn PageSource,
|
||||
/// `fifo.rsvd_drv_addr`, which is also `fifo.rsvd_boundary`.
|
||||
boundary: u16,
|
||||
/// The 2.4 and 5 GHz factory sheets of the country.
|
||||
sheets: (FactorySheet, FactorySheet),
|
||||
/// `rtwdev->need_rfk`: the channel changed outside a scan.
|
||||
@@ -181,6 +192,7 @@ impl Hardware {
|
||||
coex,
|
||||
mac,
|
||||
pages,
|
||||
geometry,
|
||||
..
|
||||
} = up;
|
||||
let initial = phy.factory().calibration().initial();
|
||||
@@ -212,6 +224,7 @@ impl Hardware {
|
||||
coex,
|
||||
report,
|
||||
pages,
|
||||
boundary: geometry.boundary(),
|
||||
sheets: regd::sheets(alpha2),
|
||||
calibrate: true,
|
||||
calibration,
|
||||
@@ -519,6 +532,49 @@ impl Device for Hardware {
|
||||
self.control.command(command).map_err(failed("command"))
|
||||
}
|
||||
|
||||
/// `rtw_fw_download_rsvd_page`, the dummy again last to give the first
|
||||
/// page its own descriptor, and `rtw_send_rsvd_page_h2c`.
|
||||
fn set_templates(&mut self, templates: &Templates) -> Result<(), String> {
|
||||
let boundary = self.boundary;
|
||||
let io = self.control.io().map_err(failed("registers"))?;
|
||||
for data in [&templates.pages[..], &[0]] {
|
||||
download::write_reserved(io, boundary, boundary, data)
|
||||
.map_err(failed("reserved pages"))?;
|
||||
}
|
||||
let Templates {
|
||||
ps_poll,
|
||||
null,
|
||||
qos_null,
|
||||
..
|
||||
} = *templates;
|
||||
self.control
|
||||
.command(commands::reserved_pages(ps_poll, null, qos_null))
|
||||
.map_err(failed("reserved pages"))
|
||||
}
|
||||
|
||||
/// `rtw_enter_lps_core` and `rtw_leave_lps_core`. Whether firmware reports
|
||||
/// the return or not, it clears the bit it set, so this waits on that.
|
||||
fn set_power_save(&mut self, on: bool) -> Result<(), String> {
|
||||
self.control
|
||||
.command(commands::power_mode(on, PORT))
|
||||
.map_err(failed("power mode"))?;
|
||||
if on {
|
||||
return Ok(());
|
||||
}
|
||||
let io = self.control.io().map_err(failed("registers"))?;
|
||||
for _ in 0..LEAVE_POWER_SAVE_TRIES {
|
||||
let control = io.read32(REG_TCR).map_err(failed("power save"))?;
|
||||
if control & POWER_BIT_BY_HARDWARE == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
io.delay_us(LEAVE_POWER_SAVE_WAIT_US);
|
||||
}
|
||||
set_mask(io, REG_TCR, POWER_BIT_BY_HARDWARE, 0).map_err(failed("power save"))?;
|
||||
self.limitations
|
||||
.push("firmware that did not leave power save in time");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `rtw_vif_port_config`.
|
||||
fn set_port(&mut self, bssid: Mac, aid: u16, linked: bool) -> Result<(), String> {
|
||||
let io = self.control.io().map_err(failed("registers"))?;
|
||||
@@ -573,10 +629,10 @@ impl Device for Hardware {
|
||||
}
|
||||
|
||||
/// `rtw_phy_dynamic_mechanism`.
|
||||
fn watchdog(&mut self, scanning: bool, stbc: bool) -> Result<u8, String> {
|
||||
self.dynamic.close_period();
|
||||
fn watchdog(&mut self, scanning: bool, stbc: bool) -> Result<Watched, String> {
|
||||
let busy = self.dynamic.close_period();
|
||||
if scanning {
|
||||
return Ok(0);
|
||||
return Ok(Watched { level: 0, busy });
|
||||
}
|
||||
let linked = self.connected;
|
||||
self.dynamic.sample(linked);
|
||||
@@ -622,7 +678,10 @@ impl Device for Hardware {
|
||||
unsafe { self.dynamic.track_temperature(io, &self.report, band) }
|
||||
.map_err(failed("temperature"))?;
|
||||
self.adapt()?;
|
||||
Ok(self.dynamic.level())
|
||||
Ok(Watched {
|
||||
level: self.dynamic.level(),
|
||||
busy,
|
||||
})
|
||||
}
|
||||
|
||||
/// `rtw_phy_dig_set_max_coverage`, and `rtw_phy_dig_reset` after, each
|
||||
|
||||
@@ -13,7 +13,7 @@ use super::{
|
||||
descriptor::{
|
||||
Aggregate, Packet, QSEL_MANAGEMENT, RATE_1M, RATE_6M, RATE_ID_B, RATE_ID_G, SECURITY_CCMP,
|
||||
},
|
||||
dynamic,
|
||||
dynamic, templates,
|
||||
transmit::Queue,
|
||||
};
|
||||
use crate::wifi::security::aggregate::{OrderedGroup, Shape};
|
||||
@@ -67,7 +67,7 @@ const BSS_EXPIRY_US: u64 = 30_000_000;
|
||||
/// rtw88's sole station shares its interface's MAC ID, the first it acquires.
|
||||
pub const MAC_ID: u8 = 0;
|
||||
/// `rtw_ops_add_interface`'s first port.
|
||||
const PORT: u8 = 0;
|
||||
pub const PORT: u8 = 0;
|
||||
/// `RTW_SEC_DEFAULT_KEY_NUM`: group keys sit at their index, pairwise ones after.
|
||||
const PAIRWISE_CAM: u8 = 4;
|
||||
/// `RTW_CAM_AES`.
|
||||
@@ -139,11 +139,18 @@ pub trait Device {
|
||||
fn set_edca(&mut self, edca: &[AccessCategory; 4], short_slot: bool) -> Result<(), String>;
|
||||
fn write_key(&mut self, key: &CamKey<'_>) -> Result<(), String>;
|
||||
fn clear_key(&mut self, entry: u8) -> Result<(), String>;
|
||||
/// `rtw_fw_download_rsvd_page` and `rtw_send_rsvd_page_h2c`.
|
||||
fn set_templates(&mut self, _templates: &templates::Templates) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
/// `rtw_enter_lps` and `rtw_leave_lps`.
|
||||
fn set_power_save(&mut self, _on: bool) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
/// `rtw_watch_dog_work`'s traffic accounts, and unless `scanning` its work
|
||||
/// on the air, for a peer that takes STBC or not. Gives the peer's
|
||||
/// `rssi_level`.
|
||||
fn watchdog(&mut self, _scanning: bool, _stbc: bool) -> Result<u8, String> {
|
||||
Ok(0)
|
||||
/// on the air, for a peer that takes STBC or not.
|
||||
fn watchdog(&mut self, _scanning: bool, _stbc: bool) -> Result<Watched, String> {
|
||||
Ok(Watched::default())
|
||||
}
|
||||
/// `rtw_core_scan_start` and `rtw_core_scan_complete`'s gain.
|
||||
fn scanning(&mut self, _scanning: bool) -> Result<(), String> {
|
||||
@@ -196,6 +203,15 @@ struct Hop {
|
||||
leaves_at: u64,
|
||||
}
|
||||
|
||||
/// What a run of the watchdog found.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Watched {
|
||||
/// The peer's `rssi_level`.
|
||||
pub level: u8,
|
||||
/// Too many frames for power save.
|
||||
pub busy: bool,
|
||||
}
|
||||
|
||||
/// One key the CAM holds.
|
||||
struct Key {
|
||||
slot: KeySlot,
|
||||
@@ -462,6 +478,8 @@ pub struct Rtl8822c<D: Device> {
|
||||
/// A report's number, which the management frame it names may await.
|
||||
reporting: Option<u8>,
|
||||
port_open: bool,
|
||||
/// `RTW_FLAG_LEISURE_PS`.
|
||||
sleeping: bool,
|
||||
watchdog_at: u64,
|
||||
/// `rtwdev->watch_dog_cnt`.
|
||||
watchdogs: u32,
|
||||
@@ -506,6 +524,7 @@ impl<D: Device> Rtl8822c<D> {
|
||||
report: 0,
|
||||
reporting: None,
|
||||
port_open: false,
|
||||
sleeping: false,
|
||||
watchdog_at,
|
||||
watchdogs: 0,
|
||||
departing: None,
|
||||
@@ -592,6 +611,7 @@ impl<D: Device> Rtl8822c<D> {
|
||||
for key in core::mem::take(&mut self.keys) {
|
||||
Self::hardware(self.device.clear_key(key.entry))?;
|
||||
}
|
||||
self.power_save(false)?;
|
||||
for agreement in &mut self.agreements {
|
||||
*agreement = None;
|
||||
}
|
||||
@@ -681,7 +701,10 @@ impl<D: Device> Rtl8822c<D> {
|
||||
self.watchdog_at = now + WATCHDOG_US;
|
||||
let scanning = self.hop.is_some();
|
||||
let stbc = self.context.as_deref().is_some_and(peer_stbc);
|
||||
let level = Self::hardware(self.device.watchdog(scanning, stbc))?;
|
||||
if !scanning {
|
||||
self.power_save(false)?;
|
||||
}
|
||||
let Watched { level, busy } = Self::hardware(self.device.watchdog(scanning, stbc))?;
|
||||
if scanning {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -693,6 +716,24 @@ impl<D: Device> Rtl8822c<D> {
|
||||
Self::hardware(self.device.command(commands::ra_info(info)))?;
|
||||
}
|
||||
self.watchdogs = self.watchdogs.wrapping_add(1);
|
||||
// `rtw_recalc_lps`, with `ieee80211_powersave_allowed`.
|
||||
let allowed = self.port_open
|
||||
&& self
|
||||
.following
|
||||
.as_ref()
|
||||
.is_some_and(follow::Following::may_sleep);
|
||||
if allowed && !busy {
|
||||
self.power_save(true)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `rtw_enter_lps` and `rtw_leave_lps`.
|
||||
fn power_save(&mut self, on: bool) -> Result<(), Error> {
|
||||
if self.sleeping != on {
|
||||
Self::hardware(self.device.set_power_save(on))?;
|
||||
self.sleeping = on;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1688,6 +1729,7 @@ impl<D: Device> Radio for Rtl8822c<D> {
|
||||
|
||||
fn begin_scan(&mut self, request: scan::Request, budget_us: u64) -> Result<(), Error> {
|
||||
let now = self.clock.now_us();
|
||||
self.power_save(false)?;
|
||||
self.observed
|
||||
.retain(|heard| now.saturating_sub(heard.at) < BSS_EXPIRY_US);
|
||||
let channels = request.channels().to_vec();
|
||||
@@ -1853,6 +1895,11 @@ impl<D: Device> Radio for Rtl8822c<D> {
|
||||
context.response().aid & 0x7ff,
|
||||
true,
|
||||
))?;
|
||||
let wmm = context.negotiated().wmm.is_some();
|
||||
let aid = context.response().aid & 0x7ff;
|
||||
let band = context.channel().band;
|
||||
let pages = templates::build(self.address, context.bssid(), aid, wmm, band);
|
||||
Self::hardware(self.device.set_templates(&pages))?;
|
||||
Self::hardware(self.device.command(commands::default_port(PORT, MAC_ID)))?;
|
||||
self.context = Some(context.clone());
|
||||
self.station
|
||||
|
||||
@@ -38,6 +38,8 @@ pub(super) struct Following {
|
||||
definition: ChannelDefinition,
|
||||
/// The TX report number the nullfunc awaits.
|
||||
probing: Option<u8>,
|
||||
/// `have_beacon`.
|
||||
heard_beacon: bool,
|
||||
}
|
||||
|
||||
impl Following {
|
||||
@@ -56,6 +58,7 @@ impl Following {
|
||||
.map(|wmm| (wmm.parameter_set_count, wmm.access_categories)),
|
||||
definition: context.definition(),
|
||||
probing: None,
|
||||
heard_beacon: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +72,11 @@ impl Following {
|
||||
self.watch.silent()
|
||||
}
|
||||
|
||||
/// `ieee80211_powersave_allowed`, of what this follows.
|
||||
pub(super) fn may_sleep(&self) -> bool {
|
||||
self.heard_beacon && !self.monitor.probing() && !self.watch.moving()
|
||||
}
|
||||
|
||||
/// A unicast frame came from the AP.
|
||||
pub(super) fn heard(&mut self, now: u64) {
|
||||
self.monitor.heard(now);
|
||||
@@ -95,6 +103,7 @@ impl<D: Device> Rtl8822c<D> {
|
||||
return Ok(());
|
||||
}
|
||||
following.monitor.beacon(now);
|
||||
following.heard_beacon = true;
|
||||
let bytes = frame.bytes();
|
||||
let (Some(&[low, high]), Some(elements)) = (
|
||||
bytes.get(34..36).and_then(|bits| bits.first_chunk()),
|
||||
@@ -234,6 +243,8 @@ impl<D: Device> Rtl8822c<D> {
|
||||
Step::Wait => Ok(()),
|
||||
Step::Lost => self.lose(),
|
||||
Step::Probe => {
|
||||
// `ieee80211_recalc_ps`: a station in doubt stays awake.
|
||||
self.power_save(false)?;
|
||||
self.probe(&context);
|
||||
Ok(())
|
||||
}
|
||||
@@ -250,6 +261,7 @@ impl<D: Device> Rtl8822c<D> {
|
||||
|
||||
/// `rtw_ops_config` of the link's channel, then `rtw_sta_rc_work`.
|
||||
fn retune(&mut self, context: &AssociationContext) -> Result<(), Error> {
|
||||
self.power_save(false)?;
|
||||
let Some(following) = &self.following else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -71,6 +71,11 @@ struct Air {
|
||||
scans: Vec<bool>,
|
||||
/// Management frames lost to the air before the AP hears them.
|
||||
losses: usize,
|
||||
/// Each power save request, and the pages each association gave firmware.
|
||||
sleeps: Vec<bool>,
|
||||
templates: Vec<templates::Templates>,
|
||||
/// A watchdog run finds too many frames for power save.
|
||||
busy: bool,
|
||||
/// The AP stopped beaconing on its channel.
|
||||
quiet: bool,
|
||||
/// The AP takes no frame at all.
|
||||
@@ -189,12 +194,23 @@ impl Device for Model {
|
||||
self.0.borrow_mut().cleared.push(entry);
|
||||
Ok(())
|
||||
}
|
||||
fn watchdog(&mut self, scanning: bool, stbc: bool) -> Result<u8, String> {
|
||||
fn set_templates(&mut self, templates: &templates::Templates) -> Result<(), String> {
|
||||
self.0.borrow_mut().templates.push(templates.clone());
|
||||
Ok(())
|
||||
}
|
||||
fn set_power_save(&mut self, on: bool) -> Result<(), String> {
|
||||
self.0.borrow_mut().sleeps.push(on);
|
||||
Ok(())
|
||||
}
|
||||
fn watchdog(&mut self, scanning: bool, stbc: bool) -> Result<Watched, String> {
|
||||
let mut air = self.0.borrow_mut();
|
||||
if !scanning {
|
||||
air.watchdogs.push(stbc);
|
||||
}
|
||||
Ok(air.level)
|
||||
Ok(Watched {
|
||||
level: air.level,
|
||||
busy: air.busy,
|
||||
})
|
||||
}
|
||||
fn scanning(&mut self, scanning: bool) -> Result<(), String> {
|
||||
self.0.borrow_mut().scans.push(scanning);
|
||||
@@ -621,6 +637,51 @@ fn nullfuncs(air: &Air) -> usize {
|
||||
air.sent.iter().filter(|sent| sent.2[0] == 0x48).count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_association_leaves_firmware_its_frames() {
|
||||
let (_, model) = connected();
|
||||
let air = model.0.borrow();
|
||||
assert_eq!(air.templates.len(), 1);
|
||||
let pages = &air.templates[0];
|
||||
// The PS-Poll's AID from the association response, in its first bytes.
|
||||
assert_eq!(pages.pages[80 + 48..80 + 52], [0xa4, 0, 1, 0xc0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_quiet_link_sleeps_between_watchdogs_and_wakes_for_each() {
|
||||
let (mut wifi, model) = connected();
|
||||
let watch = |wifi: &mut Wifi<Model>| {
|
||||
crate::test_clock::advance(CLOCK, WATCHDOG_US);
|
||||
wifi.carrier();
|
||||
wifi.carrier();
|
||||
};
|
||||
watch(&mut wifi);
|
||||
watch(&mut wifi);
|
||||
assert_eq!(model.0.borrow().sleeps, [true, false, true]);
|
||||
model.0.borrow_mut().busy = true;
|
||||
watch(&mut wifi);
|
||||
assert_eq!(model.0.borrow().sleeps, [true, false, true, false]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_doubted_link_stays_awake_and_a_departure_wakes_it() {
|
||||
let (mut wifi, model) = connected();
|
||||
crate::test_clock::advance(CLOCK, WATCHDOG_US);
|
||||
wifi.carrier();
|
||||
wifi.carrier();
|
||||
assert_eq!(model.0.borrow().sleeps.last(), Some(&true));
|
||||
model.0.borrow_mut().quiet = true;
|
||||
assert!(run(&mut wifi, &model, |_, air| nullfuncs(air) == 1));
|
||||
assert_eq!(model.0.borrow().sleeps.last(), Some(&false));
|
||||
let (mut wifi, model) = connected();
|
||||
crate::test_clock::advance(CLOCK, WATCHDOG_US);
|
||||
wifi.carrier();
|
||||
wifi.carrier();
|
||||
model.0.borrow_mut().hear(management(0xc0, &[1, 0]), false);
|
||||
assert!(run(&mut wifi, &model, |_, air| air.cleared.len() == 2));
|
||||
assert_eq!(model.0.borrow().sleeps.last(), Some(&false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ap_gone_silent_is_probed_and_given_up_on() {
|
||||
let (mut wifi, model) = connected();
|
||||
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
|
||||
// Copyright(c) 2018-2019 Realtek Corporation
|
||||
//! The frames firmware sends for a station by itself, as `rtw_build_rsvd_page`
|
||||
//! lays them out: a dummy where a beacon would go, then a PS-Poll, a QoS null
|
||||
//! and a null, each on its own page behind its descriptor.
|
||||
use super::descriptor::{Packet, QSEL_MANAGEMENT, RATE_1M, RATE_6M, RATE_ID_B, RATE_ID_G};
|
||||
use crate::{Mac, wifi::Band};
|
||||
use alloc::{vec, vec::Vec};
|
||||
|
||||
/// `TX_PAGE_SIZE`.
|
||||
const PAGE: usize = 128;
|
||||
/// `tx_pkt_desc_sz`.
|
||||
const DESCRIPTOR: usize = 48;
|
||||
/// `rsvd_drv_pg_num`.
|
||||
const MAX_PAGES: usize = 16;
|
||||
/// What page 0 is: one byte, behind the descriptor the queue writes.
|
||||
const DUMMY: usize = 1;
|
||||
|
||||
/// The pages for firmware, and where each frame's page is.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Templates {
|
||||
pub pages: Vec<u8>,
|
||||
pub ps_poll: u8,
|
||||
pub qos_null: u8,
|
||||
pub null: u8,
|
||||
}
|
||||
|
||||
/// `rtw_tx_rsvd_page_pkt_info_update` of `frame`.
|
||||
fn descriptor(band: Band, frame: &[u8]) -> Packet {
|
||||
let (rate_id, rate) = if band == Band::Ghz2 {
|
||||
(RATE_ID_B, RATE_1M)
|
||||
} else {
|
||||
(RATE_ID_G, RATE_6M)
|
||||
};
|
||||
let control = frame[0];
|
||||
Packet {
|
||||
size: frame.len() as u16,
|
||||
queue: QSEL_MANAGEMENT,
|
||||
rate_id,
|
||||
rate,
|
||||
fixed_rate: true,
|
||||
navigation_from_header: control == PS_POLL,
|
||||
device_sequence: control != PS_POLL,
|
||||
bluetooth_null: control == QOS_NULL,
|
||||
..Packet::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// `IEEE80211_STYPE_PSPOLL` and `IEEE80211_STYPE_QOS_NULLFUNC`.
|
||||
const PS_POLL: u8 = 0xa4;
|
||||
const QOS_NULL: u8 = 0xc8;
|
||||
|
||||
/// `ieee80211_pspoll_get`.
|
||||
fn ps_poll(local: Mac, bssid: Mac, aid: u16) -> Vec<u8> {
|
||||
let mut frame = vec![PS_POLL, 0];
|
||||
frame.extend((aid | 0xc000).to_le_bytes());
|
||||
frame.extend(bssid.0);
|
||||
frame.extend(local.0);
|
||||
frame
|
||||
}
|
||||
|
||||
/// `ieee80211_nullfunc_get`.
|
||||
fn null(local: Mac, bssid: Mac, qos: bool) -> Vec<u8> {
|
||||
let mut frame = vec![if qos { QOS_NULL } else { 0x48 }, 1, 0, 0];
|
||||
frame.extend(bssid.0);
|
||||
frame.extend(local.0);
|
||||
frame.extend(bssid.0);
|
||||
frame.extend([0, 0]);
|
||||
if qos {
|
||||
frame.extend(7u16.to_le_bytes());
|
||||
}
|
||||
frame
|
||||
}
|
||||
|
||||
/// `rtw_build_rsvd_page` for an association of `local` to `bssid` as `aid`,
|
||||
/// over WMM or not, on `band`.
|
||||
pub fn build(local: Mac, bssid: Mac, aid: u16, wmm: bool, band: Band) -> Templates {
|
||||
let frames = [
|
||||
ps_poll(local, bssid, aid),
|
||||
null(local, bssid, wmm),
|
||||
null(local, bssid, false),
|
||||
];
|
||||
let mut packets: [Vec<u8>; 3] = Default::default();
|
||||
let mut locations = [0u8; 3];
|
||||
let mut page = (DUMMY + DESCRIPTOR).div_ceil(PAGE);
|
||||
for ((packet, location), frame) in packets.iter_mut().zip(&mut locations).zip(&frames) {
|
||||
packet.extend(descriptor(band, frame).encode());
|
||||
packet.extend(frame);
|
||||
*location = page as u8;
|
||||
page += packet.len().div_ceil(PAGE);
|
||||
}
|
||||
debug_assert!(page <= MAX_PAGES);
|
||||
// The queue's own descriptor takes the head of page 0, and the rest of the
|
||||
// pages follow what is left of it.
|
||||
let margin = PAGE - DESCRIPTOR;
|
||||
let mut pages = vec![0; (page - 1) * PAGE + margin];
|
||||
for (packet, location) in packets.iter().zip(locations) {
|
||||
let at = margin + PAGE * (usize::from(location) - 1);
|
||||
pages[at..at + packet.len()].copy_from_slice(packet);
|
||||
}
|
||||
let [ps_poll, qos_null, null] = locations;
|
||||
Templates {
|
||||
pages,
|
||||
ps_poll,
|
||||
qos_null,
|
||||
null,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const LOCAL: Mac = Mac([2, 0, 0, 0, 0, 1]);
|
||||
const AP: Mac = Mac([2, 0, 0, 0, 0, 2]);
|
||||
|
||||
#[test]
|
||||
fn each_frame_has_a_page_past_the_dummy() {
|
||||
let built = build(LOCAL, AP, 5, true, Band::Ghz2);
|
||||
assert_eq!(
|
||||
(built.ps_poll, built.qos_null, built.null),
|
||||
(1, 2, 3),
|
||||
"PS-Poll, QoS null and null, as rtw_add_rsvd_page_sta orders them"
|
||||
);
|
||||
assert_eq!(built.pages.len(), 3 * PAGE + 80);
|
||||
let at = |page: usize| 80 + PAGE * (page - 1) + DESCRIPTOR;
|
||||
let poll = &built.pages[at(1)..at(1) + 16];
|
||||
assert_eq!(poll[..4], [0xa4, 0, 5, 0xc0]);
|
||||
assert_eq!(poll[4..10], AP.0);
|
||||
assert_eq!(poll[10..16], LOCAL.0);
|
||||
let qos = &built.pages[at(2)..at(2) + 26];
|
||||
assert_eq!(qos[..2], [0xc8, 1]);
|
||||
assert_eq!(qos[24..26], [7, 0]);
|
||||
assert_eq!(built.pages[at(3)..at(3) + 2], [0x48, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_peer_without_wmm_gets_a_plain_null_for_the_qos_one() {
|
||||
let built = build(LOCAL, AP, 1, false, Band::Ghz5);
|
||||
let at = 80 + PAGE + DESCRIPTOR;
|
||||
assert_eq!(built.pages[at..at + 2], [0x48, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptors_say_who_numbers_the_frame() {
|
||||
let poll = descriptor(Band::Ghz2, &ps_poll(LOCAL, AP, 1));
|
||||
assert!(poll.navigation_from_header && !poll.device_sequence);
|
||||
let qos = descriptor(Band::Ghz5, &null(LOCAL, AP, true));
|
||||
assert!(qos.bluetooth_null && qos.device_sequence);
|
||||
assert_eq!(qos.rate, RATE_6M);
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,11 @@ impl Monitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// `IEEE80211_STA_CONNECTION_POLL`.
|
||||
pub fn probing(&self) -> bool {
|
||||
self.probe.is_some()
|
||||
}
|
||||
|
||||
/// `ieee80211_reset_ap_probe` without its check: nothing is in question.
|
||||
pub fn restart(&mut self, now: u64) {
|
||||
self.probe = None;
|
||||
|
||||
Reference in new issue
Block a user