From d292fa69c51f1041f6fc5880fabdbde5121138ef Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 2 Dec 2026 09:25:30 +0200 Subject: [PATCH 1/6] wifi: apple: centauri: t6050: report station rates and counters from firmware Ask the firmware for its statistics reports (command 0xb0001, TLV 363) when cfg80211 reads the station, as the shared transport driver does: chip counters for TX retries, failures and rates, the TX report for the MCS, width, NSS and GI, frame counters for beacons and the PHY dump for per-chain signal. Add a survey of the operating channel with its noise and CCA load, and take the firmware version from the reply to command 0. The reports are optional. A query the firmware refuses is not sent again, and one it consumes without a reply stops them all after 500 ms instead of faulting the interface. Every TLV is bounds checked, and a missing report leaves its station fields unset. Signed-off-by: vlad --- .../apple/centauri/t6050/centauri_wifi.c | 859 +++++++++++++++++- 1 file changed, 856 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c index 9c6b2339e2078..ab2ec4f32db1e 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c @@ -59,6 +59,29 @@ module_param(host_setup, bool, 0644); MODULE_PARM_DESC(host_setup, "Before a join, tell the firmware the host is awake (command 6) and apply the reference TX defaults (commands 43 and 52); 1 = on (default)"); +/* What the firmware statistics said for the current association. */ +struct centauri_t6050_station_stats { + bool session_valid; + unsigned long session; + unsigned long counters_at, beacon_at, tx_info_at, phy_dump_at; + unsigned long noise_at, cca_at; + u32 tx_retrans, tx_retrans_base, tx_fail, tx_fail_base; + bool tx_retrans_valid, tx_retrans_base_valid; + bool tx_fail_valid, tx_fail_base_valid; + u32 chip_tx_rate_kbps, rx_rate_kbps; + bool chip_tx_rate_valid, rx_rate_valid; + u32 beacon_rx, beacon_rx_base, beacon_loss, beacon_loss_base; + bool beacon_valid, beacon_base_valid; + struct rate_info tx_rate; + bool tx_rate_valid; + s8 chain_signal[2]; + u8 chains; + s8 noise, phy_noise; + bool noise_valid, phy_noise_valid; + u64 cca_active_ms, cca_busy_ms, cca_tx_ms; + bool cca_valid; +}; + /* A command reply is separate from the uncorrelated disconnect event. */ struct centauri_wifi_disconnect_receipt { bool seen, stopping_skip, submitted, replied, waited; @@ -117,6 +140,16 @@ struct centauri_t6050_wifi { bool station_signal_valid; unsigned long station_signal_at, connected_at; struct net_device_stats station_start; + /* + * The firmware's statistics (command 0xb0001), for the station and + * survey dumps. A query the firmware refuses is not sent again + * (stats_off, by statistics bit); none is sent after the firmware + * took one and gave no reply (stats_absent). + */ + bool command_probe, stats_absent; + u64 stats_off, stats_bad_logged; + u8 stats_reply[REPLY_SIZE]; + struct centauri_t6050_station_stats st; u32 data_rings_open; bool data_failed; /* Sent once per firmware boot, before the first join (host_setup). */ @@ -887,7 +920,8 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, int ret; u32 stage; u8 *packet, *descriptor; - unsigned long deadline = jiffies + msecs_to_jiffies(1500); + unsigned long deadline = + jiffies + msecs_to_jiffies(wifi->command_probe ? 500 : 1500); if (submitted) *submitted = false; @@ -966,6 +1000,14 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, } usleep_range(500, 1000); } while (time_before(jiffies, deadline)); + /* + * An optional query (command_probe) the firmware consumed but did not + * answer: the firmware is alive and its payload buffer is free again. + */ + if (wifi->command_probe && command_payload_consumed(wifi, slot)) { + ret = -ENODATA; + goto out; + } dev_err(&wifi->control->pdev->dev, "Wi-Fi command %#x timed out (rx=%u)\n", id, wifi->rx_seen[2]); command_fault(wifi); @@ -1226,9 +1268,13 @@ static void bands_init(struct centauri_t6050_wifi *wifi) } } +static bool fw_version_read(const u8 *payload, size_t size, char *out, + size_t out_size); + static int radio_start(struct centauri_t6050_wifi *wifi) { unsigned int ring, slot; + size_t size; int ret; for (ring = 1; ring <= 4; ring++) { @@ -1246,9 +1292,17 @@ static int radio_start(struct centauri_t6050_wifi *wifi) dma_wmb(); writel(1, wifi->a.bar + 0x1004); msleep(400); - ret = command(wifi, 0, NULL, 0); - if (!ret) + size = sizeof(wifi->stats_reply); + ret = command_reply(wifi, 0, NULL, 0, wifi->stats_reply, &size, NULL); + if (!ret) { + if (fw_version_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + wifi->wiphy->fw_version, + sizeof(wifi->wiphy->fw_version))) + dev_info(&wifi->control->pdev->dev, + "Wi-Fi firmware %s\n", wifi->wiphy->fw_version); ret = station_configure(wifi, wifi->netdev->dev_addr); + } if (ret) return ret; msleep(1000); @@ -2813,6 +2867,755 @@ static void wifi_regulatory(struct wiphy *wiphy, mutex_unlock(&wifi->lock); } +/* + * The firmware's statistics: command 0xb0001 takes a bit mask of the + * reports wanted and TLV 363 (an interface value, a slice and a scope), and + * answers with one TLV per report. The layouts are those of the shared + * transport N1 driver (../centauri_wifi.c). + */ +enum { + STATS_CHIP_COUNTERS = 1, /* TLV 0xf901 */ + STATS_CCA = 3, /* TLV 0xf903 */ + STATS_STA_INFO = 7, /* TLV 0xf908 */ + STATS_FRAMES = 19, /* TLV 0xf915 */ + STATS_TX_REPORT = 29, /* TLV 0xf920 */ + STATS_PHY_DUMP = 32, /* TLV 0xf923 */ +}; + +struct cen_tlv_hdr { + __le16 tag; + __le16 length; +} __packed; + +struct cen_sta_info_report { + u8 reserved[2]; + s8 noise_dbm; +} __packed; +static_assert(sizeof(struct cen_sta_info_report) == 3); + +struct cen_chip_sta_counters { + __le32 reserved0[2]; + __le32 tx_retrans; + __le32 tx_fail_retry; + __le32 tx_fail_drop; + __le32 reserved1[4]; + __le32 tx_rate_kbps; + __le32 reserved2[34]; + __le32 rx_rate_kbps; +} __packed; +static_assert(offsetof(struct cen_chip_sta_counters, tx_retrans) == 0x08); +static_assert(offsetof(struct cen_chip_sta_counters, tx_rate_kbps) == 0x24); +static_assert(offsetof(struct cen_chip_sta_counters, rx_rate_kbps) == 0xb0); + +struct cen_cca_chan_slot { + u8 channel; + u8 band_flags; + u8 bw_primary; + u8 reserved; +} __packed; + +#define CEN_CCA_SLOTS 12 + +struct cen_cca_report { + __le32 reserved0[CEN_CCA_SLOTS]; + __le32 obss_us[CEN_CCA_SLOTS]; + __le32 interference_us[CEN_CCA_SLOTS]; + __le32 tx_primary_us[CEN_CCA_SLOTS]; + __le32 reserved1[CEN_CCA_SLOTS]; + __le32 rx_self_us[CEN_CCA_SLOTS]; + __le32 rx_other_us[CEN_CCA_SLOTS]; + __le32 tx_secondary_us[CEN_CCA_SLOTS]; + __le32 reserved2[3]; + struct cen_cca_chan_slot channels[CEN_CCA_SLOTS]; + __le64 dur_awake_us; + __le64 dur_sleep_us; +} __packed; +static_assert(sizeof(struct cen_cca_report) == 0x1cc); + +struct cen_tx_power_record { + u8 reserved0[14]; + u8 flags[2]; + u8 power_qdbm; + u8 preamble; + u8 reserved1[2]; + u8 mcs; + u8 nss_minus1; + u8 reserved2[2]; + u8 bw; + u8 reserved3[2]; + u8 gi; +} __packed; +static_assert(sizeof(struct cen_tx_power_record) == 28); + +struct cen_beacon_stats_report { + __le32 reserved0[46]; + __le32 missed_beacons; + __le32 reserved1[32]; + __le32 rx_beacons; +} __packed; +static_assert(sizeof(struct cen_beacon_stats_report) == 0x140); + +struct cen_phy_dump_record { + __le32 reserved0[2]; + __le32 noise_chain[2]; + __le32 reserved1[2]; + __le32 rssi_chain[2]; + __le32 reserved2; + __le32 valid; +} __packed; +static_assert(sizeof(struct cen_phy_dump_record) == 40); + +#define CEN_PHY_DUMP_RECORDS_PER_CORE 10 + +struct cen_fw_version_hdr { + __le32 reserved; + __le16 api_major; + __le16 api_minor; +} __packed; + +/* The TLV with this tag, or NULL; its length fits inside the reply. */ +static const void *cen_find_tlv(const u8 *payload, size_t size, u16 wanted_tag, + u16 *out_len) +{ + size_t pos = 0; + + while (pos + sizeof(struct cen_tlv_hdr) <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - sizeof(struct cen_tlv_hdr)) + break; + if (tag == wanted_tag) { + *out_len = length; + return payload + pos + sizeof(struct cen_tlv_hdr); + } + pos += sizeof(struct cen_tlv_hdr) + length; + } + return NULL; +} + +static bool sta_info_noise_read(const u8 *payload, size_t size, s8 *noise) +{ + const struct cen_sta_info_report *rep; + u16 length; + + rep = cen_find_tlv(payload, size, 0xf908, &length); + if (!rep || length < sizeof(*rep)) + return false; + if (rep->noise_dbm < -120 || rep->noise_dbm > -40) + return false; + *noise = rep->noise_dbm; + return true; +} + +static bool chip_sta_counters_read(const u8 *payload, size_t size, u32 *retrans, + u32 *tx_fail, bool *tx_fail_valid, + u32 *tx_rate_kbps, u32 *rx_rate_kbps) +{ + const struct cen_chip_sta_counters *cnt; + u16 length; + + *tx_fail = 0; + *tx_fail_valid = false; + *tx_rate_kbps = 0; + *rx_rate_kbps = 0; + cnt = cen_find_tlv(payload, size, 0xf901, &length); + if (!cnt || + length < offsetofend(struct cen_chip_sta_counters, tx_retrans)) + return false; + *retrans = get_unaligned_le32(&cnt->tx_retrans); + if (length >= offsetofend(struct cen_chip_sta_counters, tx_fail_drop)) { + *tx_fail = get_unaligned_le32(&cnt->tx_fail_retry) + + get_unaligned_le32(&cnt->tx_fail_drop); + *tx_fail_valid = true; + } + if (length >= offsetofend(struct cen_chip_sta_counters, tx_rate_kbps)) + *tx_rate_kbps = get_unaligned_le32(&cnt->tx_rate_kbps); + if (length >= offsetofend(struct cen_chip_sta_counters, rx_rate_kbps)) + *rx_rate_kbps = get_unaligned_le32(&cnt->rx_rate_kbps); + return true; +} + +static bool cca_survey_read(const u8 *payload, size_t size, u8 primary_chan, + u8 fw_band, u64 *active_ms, u64 *busy_ms, + u64 *tx_ms) +{ + const struct cen_cca_report *cca; + u64 dur0, dur1, active_us, busy_us, tx_us, act_ms; + unsigned int i; + int slot = -1; + u16 length; + + cca = cen_find_tlv(payload, size, 0xf903, &length); + if (!cca || length < sizeof(*cca)) + return false; + for (i = 0; i < CEN_CCA_SLOTS; i++) { + const struct cen_cca_chan_slot *ch = &cca->channels[i]; + + if (ch->channel == primary_chan && + (ch->band_flags & 3) == fw_band) { + slot = i; + break; + } + } + if (slot < 0) + return false; + dur0 = get_unaligned_le64(&cca->dur_awake_us); + dur1 = get_unaligned_le64(&cca->dur_sleep_us); + active_us = dur0 + dur1; + if (active_us < dur0) + return false; + act_ms = div_u64(active_us, 1000); + if (!act_ms) + return false; + tx_us = (u64)get_unaligned_le32(&cca->tx_primary_us[slot]) + + get_unaligned_le32(&cca->tx_secondary_us[slot]); + busy_us = (u64)get_unaligned_le32(&cca->obss_us[slot]) + + get_unaligned_le32(&cca->interference_us[slot]) + + get_unaligned_le32(&cca->rx_self_us[slot]) + + get_unaligned_le32(&cca->rx_other_us[slot]) + tx_us; + *active_ms = act_ms; + *busy_ms = min(div_u64(busy_us, 1000), act_ms); + *tx_ms = min(div_u64(tx_us, 1000), *busy_ms); + return true; +} + +/* + * The TX report: ten records, one per rate class. The rate is that of the + * highest PHY mode in use among the first five. + */ +static bool tx_rate_read(const u8 *payload, size_t size, struct rate_info *rate) +{ + static const u8 preamble_mode[13] = { + 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 4, 6, 6, + }; + static const u16 legacy_rates[16] = { + [0] = 10, [1] = 20, [2] = 55, [3] = 110, + [8] = 480, [9] = 240, [10] = 120, [11] = 60, + [12] = 540, [13] = 360, [14] = 180, [15] = 90, + }; + static const u8 bw_map[4] = { + RATE_INFO_BW_20, + RATE_INFO_BW_40, + RATE_INFO_BW_80, + RATE_INFO_BW_160, + }; + const u8 *v; + size_t stride; + int best_mode = -1; + unsigned int i; + u16 length; + + v = cen_find_tlv(payload, size, 0xf920, &length); + if (!v || (length != 280 && length != 320)) + return false; + stride = length / 10; + + for (i = 0; i < 5; i++) { + const struct cen_tx_power_record *rec = + (const void *)(v + i * stride); + u8 pre = rec->preamble; + u8 raw_mcs = rec->mcs; + u8 nss = rec->nss_minus1 + 1; + u8 hw_bw = rec->bw; + u8 hw_gi = rec->gi; + struct rate_info cand = {}; + u8 mode; + + if (!(rec->flags[0] | rec->flags[1] | rec->power_qdbm | pre | + raw_mcs | rec->nss_minus1 | hw_bw | hw_gi)) + continue; + if (pre >= ARRAY_SIZE(preamble_mode)) + continue; + mode = preamble_mode[pre]; + if ((int)mode <= best_mode) + continue; + if (mode <= 2) { + if (raw_mcs >= ARRAY_SIZE(legacy_rates) || + !legacy_rates[raw_mcs]) + continue; + cand.legacy = legacy_rates[raw_mcs]; + } else if (hw_bw >= ARRAY_SIZE(bw_map)) { + continue; + } else if (mode == 3) { + if (hw_bw > 1) + continue; + if (raw_mcs <= 7 && nss >= 1 && nss <= 4) + cand.mcs = (nss - 1) * 8 + raw_mcs; + else if (raw_mcs <= 31) + cand.mcs = raw_mcs; + else + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_MCS; + if (hw_gi) + cand.flags |= RATE_INFO_FLAGS_SHORT_GI; + } else if (mode == 4) { + if (raw_mcs > 11 || nss < 1 || nss > 4) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_VHT_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + if (hw_gi) + cand.flags |= RATE_INFO_FLAGS_SHORT_GI; + } else if (mode == 5) { + if (raw_mcs > 11 || nss < 1 || nss > 4 || + hw_gi > NL80211_RATE_INFO_HE_GI_3_2) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_HE_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + cand.he_gi = hw_gi; + } else { + if (raw_mcs > 13 || nss < 1 || nss > 4 || + hw_gi > NL80211_RATE_INFO_EHT_GI_3_2) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_EHT_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + cand.eht_gi = hw_gi; + } + best_mode = mode; + *rate = cand; + } + return best_mode >= 0; +} + +static bool beacon_stats_read(const u8 *payload, size_t size, u32 *rx_beacons, + u32 *missed_beacons) +{ + const struct cen_beacon_stats_report *bcn; + u16 length; + + bcn = cen_find_tlv(payload, size, 0xf915, &length); + if (!bcn || length < sizeof(*bcn)) + return false; + *missed_beacons = get_unaligned_le32(&bcn->missed_beacons); + *rx_beacons = get_unaligned_le32(&bcn->rx_beacons); + return true; +} + +/* One core's average of its valid records, as -dBm in half-dB steps. */ +static bool phy_dump_average(u64 sum, u32 count, int floor, int ceiling, + s8 *out) +{ + s64 val; + + if (!count) + return false; + val = -(s64)div_u64(sum, count * 2); + if (val < floor || val > ceiling) + return false; + *out = val; + return true; +} + +/* + * The PHY dump: ten records per core, core 0 for 2.4 GHz and core 1 for + * 5 and 6 GHz, each with the noise and RSSI of both chains. + */ +static bool phy_dump_read(const u8 *payload, size_t size, u8 fw_band, + s8 chain_rssi[2], u8 *chains, s8 *noise, + bool *noise_valid) +{ + const struct cen_phy_dump_record *records; + unsigned int core; + u16 length; + + *chains = 0; + *noise_valid = false; + records = cen_find_tlv(payload, size, 0xf923, &length); + if (!records || + length < 2 * CEN_PHY_DUMP_RECORDS_PER_CORE * sizeof(*records)) + return false; + + core = fw_band == 0 ? 0 : 1; + for (;;) { + const struct cen_phy_dump_record *base = + records + core * CEN_PHY_DUMP_RECORDS_PER_CORE; + u64 rsum[2] = {}, nsum[2] = {}; + u32 rcnt[2] = {}, ncnt[2] = {}; + s32 ntotal = 0; + u32 nvalid = 0; + unsigned int i, c; + s8 val; + + for (i = 0; i < CEN_PHY_DUMP_RECORDS_PER_CORE; i++) { + const struct cen_phy_dump_record *rec = &base[i]; + + if (!get_unaligned_le32(&rec->valid)) + continue; + for (c = 0; c < 2; c++) { + u32 n = get_unaligned_le32(&rec->noise_chain[c]); + u32 r = get_unaligned_le32(&rec->rssi_chain[c]); + + if (n) { + nsum[c] += n; + ncnt[c]++; + } + if (r) { + rsum[c] += r; + rcnt[c]++; + } + } + } + for (c = 0; c < 2; c++) { + if (phy_dump_average(rsum[c], rcnt[c], -120, -1, &val)) { + chain_rssi[c] = val; + *chains |= BIT(c); + } + if (phy_dump_average(nsum[c], ncnt[c], -120, -40, + &val)) { + ntotal += val; + nvalid++; + } + } + if (nvalid) { + *noise = ntotal / (s32)nvalid; + *noise_valid = true; + } + if (*chains || *noise_valid || !core) + break; + core = 0; + } + return *chains || *noise_valid; +} + +/* The reply to system command 0: a version header, then a version string. */ +static bool fw_version_read(const u8 *payload, size_t size, char *out, + size_t out_size) +{ + const struct cen_fw_version_hdr *hdr = (const void *)payload; + u16 api_major, api_minor; + size_t i, len = 0; + char fw[40]; + + if (!out_size || size < sizeof(*hdr)) + return false; + api_major = get_unaligned_le16(&hdr->api_major); + api_minor = get_unaligned_le16(&hdr->api_minor); + if (size > sizeof(*hdr)) { + size_t max_len = + min_t(size_t, size - sizeof(*hdr), sizeof(fw) - 1); + + for (i = 0; i < max_len && payload[sizeof(*hdr) + i]; i++) { + u8 c = payload[sizeof(*hdr) + i]; + + if (c < 0x20 || c > 0x7e) + continue; + if (!len && c == ' ') + continue; + fw[len++] = c; + } + while (len && fw[len - 1] == ' ') + len--; + } + fw[len] = '\0'; + if (len) { + strscpy(out, fw, out_size); + return true; + } + if (!api_major && !api_minor) + return false; + snprintf(out, out_size, "api %u.%u", api_major, api_minor); + return true; +} + +/* Caller holds wifi->lock. Statistics start again with each association. */ +static void stats_session(struct centauri_t6050_wifi *wifi) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + unsigned long stale = jiffies - HZ; + + if (st->session_valid && st->session == wifi->connected_at) + return; + memset(st, 0, sizeof(*st)); + st->session_valid = true; + st->session = wifi->connected_at; + st->counters_at = stale; + st->beacon_at = stale; + st->tx_info_at = stale; + st->phy_dump_at = stale; + st->noise_at = stale; + st->cca_at = stale; +} + +static void stats_unusable(struct centauri_t6050_wifi *wifi, unsigned int bit, + size_t size) +{ + if (wifi->stats_bad_logged & BIT_ULL(bit)) + return; + wifi->stats_bad_logged |= BIT_ULL(bit); + dev_info(&wifi->control->pdev->dev, + "Wi-Fi statistics %u: no usable report in a %zu-byte reply\n", + bit, size); +} + +/* + * Caller holds wifi->lock. Ask the firmware for one statistics report; on + * success *size is the reply's length within stats_reply. The query is + * optional: a refusal stops this report, a query the firmware takes but + * does not answer stops them all, and neither faults the interface. + */ +static int stats_query(struct centauri_t6050_wifi *wifi, unsigned int bit, + u8 scope, u64 slice, size_t *size) +{ + u8 payload[22] = {}; + int ret; + + if (wifi->stats_absent || (wifi->stats_off & BIT_ULL(bit))) + return -EOPNOTSUPP; + if (!wifi->configured || wifi->scan_request || wifi->command_failed || + wifi->host_sleep_sent || READ_ONCE(wifi->transport_paused)) + return -EBUSY; + put_unaligned_le64(BIT_ULL(bit), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + put_unaligned_le64(slice, payload + 13); + payload[21] = scope; + *size = sizeof(wifi->stats_reply); + wifi->command_probe = true; + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, size, NULL); + wifi->command_probe = false; + if (!ret) { + *size = min(*size, sizeof(wifi->stats_reply)); + } else if (ret == -EREMOTEIO) { + wifi->stats_off |= BIT_ULL(bit); + dev_info(&wifi->control->pdev->dev, + "Wi-Fi statistics %u: firmware status %u; not queried again\n", + bit, wifi->reply_status); + } else if (ret == -ENODATA) { + wifi->stats_absent = true; + dev_info(&wifi->control->pdev->dev, + "Wi-Fi statistics: no reply to command 0xb0001; not queried again\n"); + } + return ret; +} + +static void query_sta_counters(struct centauri_t6050_wifi *wifi) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + u32 retrans, tx_fail, tx_rate_kbps, rx_rate_kbps; + bool tx_fail_valid; + size_t size; + + st->tx_retrans_valid = false; + st->tx_fail_valid = false; + st->rx_rate_valid = false; + st->chip_tx_rate_valid = false; + st->counters_at = jiffies; + if (stats_query(wifi, STATS_CHIP_COUNTERS, 2, 0, &size)) + return; + if (!chip_sta_counters_read(wifi->stats_reply, size, &retrans, + &tx_fail, &tx_fail_valid, &tx_rate_kbps, + &rx_rate_kbps)) { + stats_unusable(wifi, STATS_CHIP_COUNTERS, size); + return; + } + if (!st->tx_retrans_base_valid) { + st->tx_retrans_base = retrans; + st->tx_retrans_base_valid = true; + } + st->tx_retrans = retrans; + st->tx_retrans_valid = true; + if (tx_fail_valid) { + if (!st->tx_fail_base_valid) { + st->tx_fail_base = tx_fail; + st->tx_fail_base_valid = true; + } + st->tx_fail = tx_fail; + st->tx_fail_valid = true; + } + if (tx_rate_kbps >= 1000 && tx_rate_kbps <= 6500000) { + st->chip_tx_rate_kbps = tx_rate_kbps; + st->chip_tx_rate_valid = true; + } + if (rx_rate_kbps >= 1000 && rx_rate_kbps <= 6500000) { + st->rx_rate_kbps = rx_rate_kbps; + st->rx_rate_valid = true; + } +} + +/* The TX report of the band's slice: 1 for 2.4 GHz, else 2, then 1. */ +static void query_tx_rate(struct centauri_t6050_wifi *wifi, int fw_band) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + struct rate_info rate = {}; + u64 slice = fw_band == 0 ? 1 : 2; + size_t size; + + st->tx_rate_valid = false; + st->tx_info_at = jiffies; + for (;;) { + if (stats_query(wifi, STATS_TX_REPORT, 1, slice, &size)) + return; + if (tx_rate_read(wifi->stats_reply, size, &rate)) { + st->tx_rate = rate; + st->tx_rate_valid = true; + return; + } + if (slice == 1) + break; + slice = 1; + } +} + +static void query_beacon_stats(struct centauri_t6050_wifi *wifi) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + u32 rx_beacons, missed_beacons; + size_t size; + + st->beacon_valid = false; + st->beacon_at = jiffies; + if (stats_query(wifi, STATS_FRAMES, 0, 0, &size)) + return; + if (!beacon_stats_read(wifi->stats_reply, size, &rx_beacons, + &missed_beacons)) { + stats_unusable(wifi, STATS_FRAMES, size); + return; + } + if (!st->beacon_base_valid) { + st->beacon_rx_base = rx_beacons; + st->beacon_loss_base = missed_beacons; + st->beacon_base_valid = true; + } + st->beacon_rx = rx_beacons; + st->beacon_loss = missed_beacons; + st->beacon_valid = true; +} + +static void query_phy_dump(struct centauri_t6050_wifi *wifi, int fw_band) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + s8 chain_rssi[2] = {}, noise = 0; + bool noise_valid; + u8 chains; + size_t size; + + st->chains = 0; + st->phy_noise_valid = false; + st->phy_dump_at = jiffies; + if (stats_query(wifi, STATS_PHY_DUMP, 0, 0, &size)) + return; + if (!phy_dump_read(wifi->stats_reply, size, fw_band, chain_rssi, + &chains, &noise, &noise_valid)) { + stats_unusable(wifi, STATS_PHY_DUMP, size); + return; + } + st->chain_signal[0] = chain_rssi[0]; + st->chain_signal[1] = chain_rssi[1]; + st->chains = chains; + st->phy_noise = noise; + st->phy_noise_valid = noise_valid; +} + +static void query_sta_noise(struct centauri_t6050_wifi *wifi) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + size_t size; + s8 noise; + + st->noise_valid = false; + st->noise_at = jiffies; + if (stats_query(wifi, STATS_STA_INFO, 2, 0, &size)) + return; + if (!sta_info_noise_read(wifi->stats_reply, size, &noise)) { + stats_unusable(wifi, STATS_STA_INFO, size); + return; + } + st->noise = noise; + st->noise_valid = true; +} + +/* The CCA report of the band's slice: 1 for 2.4 GHz, else 2, then 1. */ +static void query_cca_survey(struct centauri_t6050_wifi *wifi, + const struct ieee80211_channel *chan, int fw_band) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + u64 active_ms, busy_ms, tx_ms, slice = fw_band == 0 ? 1 : 2; + size_t size; + + st->cca_valid = false; + st->cca_at = jiffies; + for (;;) { + if (stats_query(wifi, STATS_CCA, 1, slice, &size)) + return; + if (cca_survey_read(wifi->stats_reply, size, chan->hw_value, + fw_band, &active_ms, &busy_ms, &tx_ms)) { + st->cca_active_ms = active_ms; + st->cca_busy_ms = busy_ms; + st->cca_tx_ms = tx_ms; + st->cca_valid = true; + return; + } + if (slice == 1) + break; + slice = 1; + } +} + +/* Caller holds wifi->lock: each report is read at most once a second. */ +static void fill_station_stats(struct centauri_t6050_wifi *wifi, + struct station_info *sinfo) +{ + struct centauri_t6050_station_stats *st = &wifi->st; + int fw_band = wifi->connect_channel ? + firmware_band(wifi->connect_channel->band) : + -ENOENT; + + stats_session(wifi); + if (time_after_eq(jiffies, st->counters_at + HZ)) + query_sta_counters(wifi); + if (time_after_eq(jiffies, st->beacon_at + HZ)) + query_beacon_stats(wifi); + if (fw_band >= 0 && time_after_eq(jiffies, st->tx_info_at + HZ)) + query_tx_rate(wifi, fw_band); + if (fw_band >= 0 && time_after_eq(jiffies, st->phy_dump_at + HZ)) + query_phy_dump(wifi, fw_band); + + if (st->tx_fail_valid && st->tx_fail_base_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); + sinfo->tx_failed = st->tx_fail - st->tx_fail_base; + } + if (st->tx_retrans_valid && st->tx_retrans_base_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); + sinfo->tx_retries = st->tx_retrans - st->tx_retrans_base; + } + if (st->beacon_valid && st->beacon_base_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | + BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); + sinfo->rx_beacon = st->beacon_rx - st->beacon_rx_base; + sinfo->beacon_loss_count = st->beacon_loss - st->beacon_loss_base; + } + if (st->tx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); + sinfo->txrate = st->tx_rate; + } else if (st->chip_tx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); + sinfo->txrate = (struct rate_info){ + .legacy = DIV_ROUND_CLOSEST(st->chip_tx_rate_kbps, 100), + }; + } + if (st->rx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); + sinfo->rxrate = (struct rate_info){ + .legacy = DIV_ROUND_CLOSEST(st->rx_rate_kbps, 100), + }; + } + if (st->chains) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); + sinfo->chains = st->chains; + sinfo->chain_signal[0] = st->chain_signal[0]; + sinfo->chain_signal[1] = st->chain_signal[1]; + } +} + static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info *sinfo) { const struct net_device_stats *stats = &wifi->netdev->stats; @@ -2837,6 +3640,7 @@ static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info * sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); sinfo->signal = wifi->station_signal; } + fill_station_stats(wifi, sinfo); } static int wifi_get_station(struct wiphy *wiphy, struct wireless_dev *wdev, @@ -2875,6 +3679,54 @@ static int wifi_dump_station(struct wiphy *wiphy, struct wireless_dev *wdev, return ret; } +/* The operating channel only: its noise and, from the CCA report, its load. */ +static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, + int idx, struct survey_info *survey) +{ + struct centauri_t6050_wifi *wifi = + *(struct centauri_t6050_wifi **)wiphy_priv(wiphy); + struct centauri_t6050_station_stats *st = &wifi->st; + struct ieee80211_channel *chan; + int fw_band, ret = 0; + + if (idx) + return -ENOENT; + mutex_lock(&wifi->lock); + chan = wifi->connect_channel; + fw_band = chan ? firmware_band(chan->band) : -ENOENT; + if (netdev != wifi->netdev || !wifi->connected || + READ_ONCE(wifi->stopping) || fw_band < 0) { + ret = -ENOENT; + goto out; + } + stats_session(wifi); + if (time_after_eq(jiffies, st->noise_at + HZ)) + query_sta_noise(wifi); + if (time_after_eq(jiffies, st->cca_at + HZ)) + query_cca_survey(wifi, chan, fw_band); + if (!st->noise_valid && time_after_eq(jiffies, st->phy_dump_at + HZ)) + query_phy_dump(wifi, fw_band); + survey->channel = chan; + survey->filled = SURVEY_INFO_IN_USE; + if (st->noise_valid) { + survey->filled |= SURVEY_INFO_NOISE_DBM; + survey->noise = st->noise; + } else if (st->phy_noise_valid) { + survey->filled |= SURVEY_INFO_NOISE_DBM; + survey->noise = st->phy_noise; + } + if (st->cca_valid) { + survey->filled |= SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY | + SURVEY_INFO_TIME_TX; + survey->time = st->cca_active_ms; + survey->time_busy = st->cca_busy_ms; + survey->time_tx = st->cca_tx_ms; + } +out: + mutex_unlock(&wifi->lock); + return ret; +} + static const struct cfg80211_ops wifi_ops = { .suspend = wifi_suspend, .resume = wifi_resume, @@ -2885,6 +3737,7 @@ static const struct cfg80211_ops wifi_ops = { .set_power_mgmt = wifi_power_mgmt, .get_station = wifi_get_station, .dump_station = wifi_dump_station, + .dump_survey = wifi_dump_survey, }; static int data_start(struct centauri_t6050_wifi *wifi); From 99732b2a925af6ab55637878e4278ee5348f85f4 Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 2 Dec 2026 09:39:10 +0200 Subject: [PATCH 2/6] wifi: apple: centauri: t6050: keep statistics queries from faulting the link A statistics query the firmware still holds after 500 ms now gets the full 1.5 s command deadline; only one it consumed without a reply ends early. Record reply status and command only for the sequence being waited for, so a late reply to an abandoned query cannot hide the completion of the next command. Cache a refused statistics report per slice and still try slice 1 after a refused slice 2. Prefer the CCA slot that matches the link's primary channel position, count only awake time as survey time, and stop reporting missed beacons as beacon loss events. Signed-off-by: vlad --- .../apple/centauri/t6050/centauri_wifi.c | 110 +++++++++++------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c index ab2ec4f32db1e..edac9fcd457a0 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c @@ -70,7 +70,7 @@ struct centauri_t6050_station_stats { bool tx_fail_valid, tx_fail_base_valid; u32 chip_tx_rate_kbps, rx_rate_kbps; bool chip_tx_rate_valid, rx_rate_valid; - u32 beacon_rx, beacon_rx_base, beacon_loss, beacon_loss_base; + u32 beacon_rx, beacon_rx_base; bool beacon_valid, beacon_base_valid; struct rate_info tx_rate; bool tx_rate_valid; @@ -137,7 +137,8 @@ struct centauri_t6050_wifi { u32 data_tx_capped, data_tx_cap_bypassed; unsigned long data_tx_completed_at; s8 station_signal; - bool station_signal_valid; + bool station_signal_valid, station_bw_valid; + u8 station_bw_primary; unsigned long station_signal_at, connected_at; struct net_device_stats station_start; /* @@ -147,7 +148,7 @@ struct centauri_t6050_wifi { * took one and gave no reply (stats_absent). */ bool command_probe, stats_absent; - u64 stats_off, stats_bad_logged; + u64 stats_off, stats_off_slice2, stats_bad_logged; u8 stats_reply[REPLY_SIZE]; struct centauri_t6050_station_stats st; u32 data_rings_open; @@ -788,7 +789,8 @@ static void drain_data_tx(struct centauri_t6050_wifi *wifi) } /* The interface status reply: 8 fixed bytes, then TLVs. */ -static bool link_signal_read(const u8 *packet, size_t size, s8 *signal) +static bool link_signal_read(const u8 *packet, size_t size, s8 *signal, + u8 *bw_primary, bool *bw_valid) { size_t pos = 8; @@ -799,6 +801,8 @@ static bool link_signal_read(const u8 *packet, size_t size, s8 *signal) if (length > size - pos - 4) return false; if (tag == 268 && length >= 115) { + *bw_primary = packet[pos + 4 + 18]; + *bw_valid = true; *signal = (s8)packet[pos + 5]; return *signal != 0; } @@ -856,14 +860,18 @@ static void drain(struct centauri_t6050_wifi *wifi) if (command == 0x80000004) wifi->disconnect_pending = false; - } else { + } else if (sequence == wifi->reply_capture) { + /* + * Only the command being waited for: a late + * reply to an abandoned statistics query must + * not hide the completion of the next command. + */ wifi->reply_valid = true; wifi->reply_sequence = sequence; wifi->reply_status = status; wifi->reply_command = wire_command; - if (wifi->reply_buffer && - sequence == wifi->reply_capture) { + if (wifi->reply_buffer) { wifi->reply_size = size - 24; memcpy(wifi->reply_buffer, packet + 24, min(wifi->reply_size, @@ -920,8 +928,8 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, int ret; u32 stage; u8 *packet, *descriptor; - unsigned long deadline = - jiffies + msecs_to_jiffies(wifi->command_probe ? 500 : 1500); + unsigned long deadline = jiffies + msecs_to_jiffies(1500); + unsigned long probe_deadline = jiffies + msecs_to_jiffies(500); if (submitted) *submitted = false; @@ -998,16 +1006,20 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, *reply_size = wifi->reply_size; goto out; } + /* + * An optional query (command_probe) the firmware consumed but + * did not answer within 500 ms: the firmware is alive and its + * payload buffer is free again. One it still holds gets the + * full deadline. + */ + if (wifi->command_probe && + time_after(jiffies, probe_deadline) && + command_payload_consumed(wifi, slot)) { + ret = -ENODATA; + goto out; + } usleep_range(500, 1000); } while (time_before(jiffies, deadline)); - /* - * An optional query (command_probe) the firmware consumed but did not - * answer: the firmware is alive and its payload buffer is free again. - */ - if (wifi->command_probe && command_payload_consumed(wifi, slot)) { - ret = -ENODATA; - goto out; - } dev_err(&wifi->control->pdev->dev, "Wi-Fi command %#x timed out (rx=%u)\n", id, wifi->rx_seen[2]); command_fault(wifi); @@ -3037,11 +3049,11 @@ static bool chip_sta_counters_read(const u8 *payload, size_t size, u32 *retrans, } static bool cca_survey_read(const u8 *payload, size_t size, u8 primary_chan, - u8 fw_band, u64 *active_ms, u64 *busy_ms, - u64 *tx_ms) + u8 fw_band, u8 bw_primary, bool bw_valid, + u64 *active_ms, u64 *busy_ms, u64 *tx_ms) { const struct cen_cca_report *cca; - u64 dur0, dur1, active_us, busy_us, tx_us, act_ms; + u64 active_us, busy_us, tx_us, act_ms; unsigned int i; int slot = -1; u16 length; @@ -3052,19 +3064,20 @@ static bool cca_survey_read(const u8 *payload, size_t size, u8 primary_chan, for (i = 0; i < CEN_CCA_SLOTS; i++) { const struct cen_cca_chan_slot *ch = &cca->channels[i]; - if (ch->channel == primary_chan && - (ch->band_flags & 3) == fw_band) { + if (ch->channel != primary_chan || + (ch->band_flags & 3) != fw_band) + continue; + if (bw_valid && ch->bw_primary == bw_primary) { slot = i; break; } + if (slot < 0) + slot = i; } if (slot < 0) return false; - dur0 = get_unaligned_le64(&cca->dur_awake_us); - dur1 = get_unaligned_le64(&cca->dur_sleep_us); - active_us = dur0 + dur1; - if (active_us < dur0) - return false; + /* Survey time is the time the radio was on: awake, not asleep. */ + active_us = get_unaligned_le64(&cca->dur_awake_us); act_ms = div_u64(active_us, 1000); if (!act_ms) return false; @@ -3366,7 +3379,9 @@ static int stats_query(struct centauri_t6050_wifi *wifi, unsigned int bit, u8 payload[22] = {}; int ret; - if (wifi->stats_absent || (wifi->stats_off & BIT_ULL(bit))) + u64 *off = slice == 2 ? &wifi->stats_off_slice2 : &wifi->stats_off; + + if (wifi->stats_absent || (*off & BIT_ULL(bit))) return -EOPNOTSUPP; if (!wifi->configured || wifi->scan_request || wifi->command_failed || wifi->host_sleep_sent || READ_ONCE(wifi->transport_paused)) @@ -3385,10 +3400,10 @@ static int stats_query(struct centauri_t6050_wifi *wifi, unsigned int bit, if (!ret) { *size = min(*size, sizeof(wifi->stats_reply)); } else if (ret == -EREMOTEIO) { - wifi->stats_off |= BIT_ULL(bit); + *off |= BIT_ULL(bit); dev_info(&wifi->control->pdev->dev, - "Wi-Fi statistics %u: firmware status %u; not queried again\n", - bit, wifi->reply_status); + "Wi-Fi statistics %u slice %llu: firmware status %u; not queried again\n", + bit, slice, wifi->reply_status); } else if (ret == -ENODATA) { wifi->stats_absent = true; dev_info(&wifi->control->pdev->dev, @@ -3448,13 +3463,16 @@ static void query_tx_rate(struct centauri_t6050_wifi *wifi, int fw_band) struct rate_info rate = {}; u64 slice = fw_band == 0 ? 1 : 2; size_t size; + int ret; st->tx_rate_valid = false; st->tx_info_at = jiffies; for (;;) { - if (stats_query(wifi, STATS_TX_REPORT, 1, slice, &size)) + ret = stats_query(wifi, STATS_TX_REPORT, 1, slice, &size); + /* A refused slice 2 still leaves slice 1, as in the reference. */ + if (ret && (slice == 1 || (ret != -EREMOTEIO && ret != -EOPNOTSUPP))) return; - if (tx_rate_read(wifi->stats_reply, size, &rate)) { + if (!ret && tx_rate_read(wifi->stats_reply, size, &rate)) { st->tx_rate = rate; st->tx_rate_valid = true; return; @@ -3482,11 +3500,9 @@ static void query_beacon_stats(struct centauri_t6050_wifi *wifi) } if (!st->beacon_base_valid) { st->beacon_rx_base = rx_beacons; - st->beacon_loss_base = missed_beacons; st->beacon_base_valid = true; } st->beacon_rx = rx_beacons; - st->beacon_loss = missed_beacons; st->beacon_valid = true; } @@ -3540,14 +3556,19 @@ static void query_cca_survey(struct centauri_t6050_wifi *wifi, struct centauri_t6050_station_stats *st = &wifi->st; u64 active_ms, busy_ms, tx_ms, slice = fw_band == 0 ? 1 : 2; size_t size; + int ret; st->cca_valid = false; st->cca_at = jiffies; for (;;) { - if (stats_query(wifi, STATS_CCA, 1, slice, &size)) + ret = stats_query(wifi, STATS_CCA, 1, slice, &size); + if (ret && (slice == 1 || (ret != -EREMOTEIO && ret != -EOPNOTSUPP))) return; - if (cca_survey_read(wifi->stats_reply, size, chan->hw_value, - fw_band, &active_ms, &busy_ms, &tx_ms)) { + if (!ret && + cca_survey_read(wifi->stats_reply, size, chan->hw_value, + fw_band, wifi->station_bw_primary, + wifi->station_bw_valid, &active_ms, + &busy_ms, &tx_ms)) { st->cca_active_ms = active_ms; st->cca_busy_ms = busy_ms; st->cca_tx_ms = tx_ms; @@ -3588,10 +3609,12 @@ static void fill_station_stats(struct centauri_t6050_wifi *wifi, sinfo->tx_retries = st->tx_retrans - st->tx_retrans_base; } if (st->beacon_valid && st->beacon_base_valid) { - sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | - BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); + /* + * Received beacons only: the firmware counts missed beacons, + * while cfg80211's beacon loss counts loss events. + */ + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX); sinfo->rx_beacon = st->beacon_rx - st->beacon_rx_base; - sinfo->beacon_loss_count = st->beacon_loss - st->beacon_loss_base; } if (st->tx_rate_valid) { sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); @@ -3995,11 +4018,14 @@ static int query_link_status(struct centauri_t6050_wifi *wifi) if (!wifi->configured) return -ENETDOWN; ret = command_reply(wifi, 20, NULL, 0, wifi->link_status, &size, NULL); + wifi->station_bw_valid = false; if (!ret) wifi->station_signal_valid = link_signal_read(wifi->link_status, min(size, sizeof(wifi->link_status)), - &wifi->station_signal); + &wifi->station_signal, + &wifi->station_bw_primary, + &wifi->station_bw_valid); wifi->station_signal_at = jiffies; return ret; } From d3fd4caf97d52e24d261a4612ed550c216858028 Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 2 Dec 2026 14:48:11 +0200 Subject: [PATCH 3/6] wifi: apple: centauri: t6050: stop reporting survey busy and tx time The CCA report's per-read deltas are inconsistent with each other: in one second with power save on, the primary tx time grew by 30.9 ms and the summed busy time by 40.0 ms, while the awake time grew by 5.0 ms. No time base the report carries is known to give a valid busy or tx time, so report only the channel noise and stop querying the CCA report. Signed-off-by: vlad --- .../apple/centauri/t6050/centauri_wifi.c | 124 ++---------------- 1 file changed, 8 insertions(+), 116 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c index edac9fcd457a0..6eaea5643a37d 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c @@ -64,7 +64,7 @@ struct centauri_t6050_station_stats { bool session_valid; unsigned long session; unsigned long counters_at, beacon_at, tx_info_at, phy_dump_at; - unsigned long noise_at, cca_at; + unsigned long noise_at; u32 tx_retrans, tx_retrans_base, tx_fail, tx_fail_base; bool tx_retrans_valid, tx_retrans_base_valid; bool tx_fail_valid, tx_fail_base_valid; @@ -78,8 +78,6 @@ struct centauri_t6050_station_stats { u8 chains; s8 noise, phy_noise; bool noise_valid, phy_noise_valid; - u64 cca_active_ms, cca_busy_ms, cca_tx_ms; - bool cca_valid; }; /* A command reply is separate from the uncorrelated disconnect event. */ @@ -2919,31 +2917,6 @@ static_assert(offsetof(struct cen_chip_sta_counters, tx_retrans) == 0x08); static_assert(offsetof(struct cen_chip_sta_counters, tx_rate_kbps) == 0x24); static_assert(offsetof(struct cen_chip_sta_counters, rx_rate_kbps) == 0xb0); -struct cen_cca_chan_slot { - u8 channel; - u8 band_flags; - u8 bw_primary; - u8 reserved; -} __packed; - -#define CEN_CCA_SLOTS 12 - -struct cen_cca_report { - __le32 reserved0[CEN_CCA_SLOTS]; - __le32 obss_us[CEN_CCA_SLOTS]; - __le32 interference_us[CEN_CCA_SLOTS]; - __le32 tx_primary_us[CEN_CCA_SLOTS]; - __le32 reserved1[CEN_CCA_SLOTS]; - __le32 rx_self_us[CEN_CCA_SLOTS]; - __le32 rx_other_us[CEN_CCA_SLOTS]; - __le32 tx_secondary_us[CEN_CCA_SLOTS]; - __le32 reserved2[3]; - struct cen_cca_chan_slot channels[CEN_CCA_SLOTS]; - __le64 dur_awake_us; - __le64 dur_sleep_us; -} __packed; -static_assert(sizeof(struct cen_cca_report) == 0x1cc); - struct cen_tx_power_record { u8 reserved0[14]; u8 flags[2]; @@ -3048,51 +3021,6 @@ static bool chip_sta_counters_read(const u8 *payload, size_t size, u32 *retrans, return true; } -static bool cca_survey_read(const u8 *payload, size_t size, u8 primary_chan, - u8 fw_band, u8 bw_primary, bool bw_valid, - u64 *active_ms, u64 *busy_ms, u64 *tx_ms) -{ - const struct cen_cca_report *cca; - u64 active_us, busy_us, tx_us, act_ms; - unsigned int i; - int slot = -1; - u16 length; - - cca = cen_find_tlv(payload, size, 0xf903, &length); - if (!cca || length < sizeof(*cca)) - return false; - for (i = 0; i < CEN_CCA_SLOTS; i++) { - const struct cen_cca_chan_slot *ch = &cca->channels[i]; - - if (ch->channel != primary_chan || - (ch->band_flags & 3) != fw_band) - continue; - if (bw_valid && ch->bw_primary == bw_primary) { - slot = i; - break; - } - if (slot < 0) - slot = i; - } - if (slot < 0) - return false; - /* Survey time is the time the radio was on: awake, not asleep. */ - active_us = get_unaligned_le64(&cca->dur_awake_us); - act_ms = div_u64(active_us, 1000); - if (!act_ms) - return false; - tx_us = (u64)get_unaligned_le32(&cca->tx_primary_us[slot]) + - get_unaligned_le32(&cca->tx_secondary_us[slot]); - busy_us = (u64)get_unaligned_le32(&cca->obss_us[slot]) + - get_unaligned_le32(&cca->interference_us[slot]) + - get_unaligned_le32(&cca->rx_self_us[slot]) + - get_unaligned_le32(&cca->rx_other_us[slot]) + tx_us; - *active_ms = act_ms; - *busy_ms = min(div_u64(busy_us, 1000), act_ms); - *tx_ms = min(div_u64(tx_us, 1000), *busy_ms); - return true; -} - /* * The TX report: ten records, one per rate class. The rate is that of the * highest PHY mode in use among the first five. @@ -3353,7 +3281,6 @@ static void stats_session(struct centauri_t6050_wifi *wifi) st->tx_info_at = stale; st->phy_dump_at = stale; st->noise_at = stale; - st->cca_at = stale; } static void stats_unusable(struct centauri_t6050_wifi *wifi, unsigned int bit, @@ -3549,38 +3476,6 @@ static void query_sta_noise(struct centauri_t6050_wifi *wifi) st->noise_valid = true; } -/* The CCA report of the band's slice: 1 for 2.4 GHz, else 2, then 1. */ -static void query_cca_survey(struct centauri_t6050_wifi *wifi, - const struct ieee80211_channel *chan, int fw_band) -{ - struct centauri_t6050_station_stats *st = &wifi->st; - u64 active_ms, busy_ms, tx_ms, slice = fw_band == 0 ? 1 : 2; - size_t size; - int ret; - - st->cca_valid = false; - st->cca_at = jiffies; - for (;;) { - ret = stats_query(wifi, STATS_CCA, 1, slice, &size); - if (ret && (slice == 1 || (ret != -EREMOTEIO && ret != -EOPNOTSUPP))) - return; - if (!ret && - cca_survey_read(wifi->stats_reply, size, chan->hw_value, - fw_band, wifi->station_bw_primary, - wifi->station_bw_valid, &active_ms, - &busy_ms, &tx_ms)) { - st->cca_active_ms = active_ms; - st->cca_busy_ms = busy_ms; - st->cca_tx_ms = tx_ms; - st->cca_valid = true; - return; - } - if (slice == 1) - break; - slice = 1; - } -} - /* Caller holds wifi->lock: each report is read at most once a second. */ static void fill_station_stats(struct centauri_t6050_wifi *wifi, struct station_info *sinfo) @@ -3702,7 +3597,13 @@ static int wifi_dump_station(struct wiphy *wiphy, struct wireless_dev *wdev, return ret; } -/* The operating channel only: its noise and, from the CCA report, its load. */ +/* + * The operating channel only, and only its noise. The CCA report (TLV + * 0xf903) is not used: its per-read deltas are inconsistent (one second + * with power save on: primary tx +30.9 ms, summed busy +40.0 ms, awake + * time +5.0 ms), so no time base it carries is known to give a valid + * busy or tx time. + */ static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, int idx, struct survey_info *survey) { @@ -3725,8 +3626,6 @@ static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, stats_session(wifi); if (time_after_eq(jiffies, st->noise_at + HZ)) query_sta_noise(wifi); - if (time_after_eq(jiffies, st->cca_at + HZ)) - query_cca_survey(wifi, chan, fw_band); if (!st->noise_valid && time_after_eq(jiffies, st->phy_dump_at + HZ)) query_phy_dump(wifi, fw_band); survey->channel = chan; @@ -3738,13 +3637,6 @@ static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, survey->filled |= SURVEY_INFO_NOISE_DBM; survey->noise = st->phy_noise; } - if (st->cca_valid) { - survey->filled |= SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY | - SURVEY_INFO_TIME_TX; - survey->time = st->cca_active_ms; - survey->time_busy = st->cca_busy_ms; - survey->time_tx = st->cca_tx_ms; - } out: mutex_unlock(&wifi->lock); return ret; From 3611b3f8d555ff95f9f5015065c290d20fc98475 Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 2 Dec 2026 13:47:48 +0200 Subject: [PATCH 4/6] wifi: apple: centauri: t6050: use a wake's host warm reset only once The host sets its warm-reset flag when it resets the N1 in a wake's noirq resume and clears it only at the next suspend prepare. A port cycle for recovery leaves it set too. Any later recovery in the same wake, such as a Bluetooth unbind that cannot fence its DMA or a probe failure, then reads the old flag, skips the port cycle and enumerates the functions again on firmware that was never reset. Record that the wake's reset went to an enumeration and cycle the port for every later recovery. A PM notifier clears the record at suspend prepare, where the host clears its flag. Signed-off-by: vlad --- .../apple/centauri/t6050/centauri_pci.c | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index 97331b2c20186..0ea9e23c5367d 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -51,6 +51,8 @@ struct centauri_t6050_rewake { size_t arena_size; bool beta_lease; bool beta_fallback; + /* The host's reset of the last wake already went to an enumeration. */ + bool host_reset_used; }; static LIST_HEAD(cen_recovery_owners); @@ -1014,7 +1016,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work) ktime_t begin, start, up_at; unsigned int sleep_flags; u16 lnksta = 0, first = 0; - bool force_recycle, need_recycle, smc_locked = false, exiting; + bool force_recycle, reset_used, need_recycle, smc_locked = false, exiting; int i, ret; sleep_flags = lock_system_sleep(); @@ -1039,8 +1041,14 @@ static void centauri_t6050_rewake_worker(struct work_struct *work) goto unlock; mutex_lock(&cen_recovery_lock); force_recycle = rw->force_recycle; + reset_used = rw->host_reset_used; mutex_unlock(&cen_recovery_lock); - need_recycle = force_recycle || !apple_pcie_t6050_warm_reset_done(bridge); + /* + * The host's flag stays set until the next suspend: once a new enumeration has run on + * this wake's reset (or a port cycle since), a later recovery needs its own reset. + */ + need_recycle = force_recycle || reset_used || + !apple_pcie_t6050_warm_reset_done(bridge); if (need_recycle) { smc_locked = apple_pcie_t6050_recycle_lock(bridge); if (!smc_locked) { @@ -1066,6 +1074,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work) rw->teardown_active = false; rw->force_recycle = false; rw->fault_only = false; + rw->host_reset_used = true; mutex_unlock(&cen_recovery_lock); /* * The host resets the N1 in its noirq resume, while the port is still down. A reset given @@ -1679,6 +1688,24 @@ static struct notifier_block cen_reboot_nb = { .notifier_call = centauri_t6050_reboot_notify, }; +/* The host clears its warm-reset flag here too; the next wake's reset is unused. */ +static int centauri_t6050_pm_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct centauri_t6050_rewake *rw; + + if (action != PM_SUSPEND_PREPARE) + return NOTIFY_DONE; + guard(mutex)(&cen_recovery_lock); + list_for_each_entry(rw, &cen_recovery_owners, node) + rw->host_reset_used = false; + return NOTIFY_DONE; +} + +static struct notifier_block cen_pm_nb = { + .notifier_call = centauri_t6050_pm_notify, +}; + static const struct dev_pm_ops centauri_t6050_pm_ops = { .prepare = centauri_t6050_prepare, .complete = centauri_t6050_complete, @@ -1720,8 +1747,15 @@ static int __init centauri_t6050_init(void) destroy_workqueue(cen_rewake_wq); return ret; } + ret = register_pm_notifier(&cen_pm_nb); + if (ret) { + unregister_reboot_notifier(&cen_reboot_nb); + destroy_workqueue(cen_rewake_wq); + return ret; + } ret = pci_register_driver(¢auri_t6050_driver); if (ret) { + unregister_pm_notifier(&cen_pm_nb); unregister_reboot_notifier(&cen_reboot_nb); destroy_workqueue(cen_rewake_wq); } @@ -1733,6 +1767,7 @@ static void __exit centauri_t6050_exit(void) { struct centauri_t6050_rewake *rw, *next; + unregister_pm_notifier(&cen_pm_nb); unregister_reboot_notifier(&cen_reboot_nb); /* Drain before unregister: no worker can race callback data destruction. */ centauri_t6050_stop_recovery(); From 0f62a44291a55cc7365872ea255d8fdd1bc157de Mon Sep 17 00:00:00 2001 From: vlad Date: Wed, 2 Dec 2026 14:07:33 +0200 Subject: [PATCH 5/6] wifi: apple: centauri: t6050: take a new owner's host reset as used A recovery owner allocated in the middle of a wake, after a module reload, started with host_reset_used false while the PCI host still reported this wake's warm reset as done, so the next recovery could skip the port cycle on firmware that was never reset. Start a new owner with the reset taken as used; the suspend-prepare notifier clears it for the first sleep the owner sees. Signed-off-by: vlad --- drivers/net/wireless/apple/centauri/t6050/centauri_pci.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index 0ea9e23c5367d..feb804d40b6c7 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -97,6 +97,13 @@ static struct centauri_t6050_rewake *centauri_t6050_owner_get(struct pci_dev *fn INIT_DELAYED_WORK(&rw->fault_work, centauri_t6050_fault_worker); rw->bridge = pci_dev_get(fn->bus->self); rw->slot = PCI_SLOT(fn->devfn); + /* + * A new owner (first probe, or a module reload in the middle of a + * wake) cannot know whether this wake's host warm reset was already + * used: take it as used, so the next recovery cycles the port. The + * suspend-prepare notifier clears it for the first sleep it sees. + */ + rw->host_reset_used = true; list_add_tail(&rw->node, &cen_recovery_owners); kref_get(&rw->ref); return rw; From 80ab0c0fc880064971c7a9c0ebc2725314c345cc Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 17:35:35 -0500 Subject: [PATCH 6/6] tools/asahi: centauri tests: follow the t6050 statistics and warm reset Three host tests copy or model t6050 driver code that this series changed, so they no longer built: - centauri-wifi-pm-tests: command_reply() gives up an optional query the firmware consumed but did not answer. Model command_probe and extract command_payload_consumed(). Check that such a query ends with -ENODATA and no fault, that one the firmware still holds and any other command keep the full deadline and fault, and that the next command gets its own reply. - centauri-scan-tests: fill_station() now reads the statistics. Run stats_query() from it and check that a station read during a scan sends no statistics command, like command 20. - centauri-physical-recovery-tests: model host_reset_used and extract the PM notifier. Check that the host's warm reset serves one enumeration per wake, that a later recovery cycles the port, that the next suspend makes the reset usable again and that a refused recovery does not use it. Add a negative control for each new check. The driver is unchanged. Signed-off-by: DjDeveloperr --- .../asahi/centauri-physical-recovery-tests.c | 48 ++++++++++++++++ .../asahi/centauri-physical-recovery-tests.py | 31 ++++++++--- tools/asahi/centauri-scan-tests.py | 50 ++++++++++++++++- tools/asahi/centauri-wifi-pm-tests.py | 55 +++++++++++++++++-- tools/asahi/run-host-tests.py | 4 +- 5 files changed, 173 insertions(+), 15 deletions(-) diff --git a/tools/asahi/centauri-physical-recovery-tests.c b/tools/asahi/centauri-physical-recovery-tests.c index 90e35c2f93e8a..577a52212663e 100644 --- a/tools/asahi/centauri-physical-recovery-tests.c +++ b/tools/asahi/centauri-physical-recovery-tests.c @@ -26,6 +26,13 @@ #define CEN_BOOT_STOPPED 5 #define SZ_16K 16384 #define CEN_ARENA_SIZE 65536 +#define NOTIFY_DONE 0 +#define PM_SUSPEND_PREPARE 3 +#define PM_POST_SUSPEND 4 +/* The one owner of the model is the whole list. */ +#define cen_recovery_owners owner +#define list_for_each_entry(p, head, member) for (p = (head); p; p = NULL) +#define guard(type) guard_##type typedef uint16_t u16; typedef int64_t s64; typedef int64_t ktime_t; @@ -61,8 +68,10 @@ struct centauri_t6050_rewake { void *firmware, *descriptor, *arena, *data; dma_addr_t firmware_dma, descriptor_dma, arena_dma; size_t firmware_size, arena_size; + bool host_reset_used; const struct apple_centauri_sleep_ops *ops; }; +struct notifier_block { int unused; }; struct centauri_t6050_irqs { struct pci_dev *pdev; int count; }; struct centauri_t6050 { struct pci_dev *pdev; @@ -109,6 +118,7 @@ static bool centauri_t6050_ipc_fenced(struct centauri_t6050_ipc *p); static void centauri_t6050_release_ipc(struct centauri_t6050_ipc *p); static void mutex_lock(int *p) { assert(!*p); *p = 1; } static void mutex_unlock(int *p) { assert(*p); *p = 0; } +static void guard_mutex(int *p) { assert(!*p); } static void device_lock(struct device *p) { mutex_lock(&p->locked); } static void device_unlock(struct device *p) { mutex_unlock(&p->locked); } static struct pci_dev *pci_get_slot(struct pci_bus *b, int devfn) @@ -483,11 +493,49 @@ static void test_runtime_force_recycle(void) puts("runtime force_recycle ignores stale warm_done: PASS"); } +static void next_recovery(bool used) +{ + /* Later recovery of the same wake: the host's flag is still set. */ + fixture(); + publish_all(); + warm_done = true; + owner.host_reset_used = used; + thaw_and_run(); + assert(!refusals && removes == 3 && rescans == 1 && !live_allocations()); +} + +static void test_host_reset_used_once(void) +{ + struct notifier_block nb = {}; + + recycle_ret = 0; + can_recycle_val = true; + next_recovery(false); + assert(!recycles && owner.host_reset_used); + next_recovery(owner.host_reset_used); + assert(recycles == 1 && owner.host_reset_used); + /* Only the next suspend makes the host's next reset unused. */ + centauri_t6050_pm_notify(&nb, PM_POST_SUSPEND, NULL); + assert(owner.host_reset_used); + centauri_t6050_pm_notify(&nb, PM_SUSPEND_PREPARE, NULL); + assert(!owner.host_reset_used); + next_recovery(owner.host_reset_used); + assert(!recycles && owner.host_reset_used); + /* A refused recovery enumerates nothing and leaves the reset unused. */ + only_tuple(1, false); + force_pending = true; + warm_done = true; + thaw_and_run(); + assert(refusals == 1 && !removes && !rescans && !owner.host_reset_used); + puts("host warm reset serves one enumeration per wake: PASS"); +} + int main(void) { test_physical(); test_devices_only_and_aborted_suspend(); test_each_retained_tuple(); test_pending_and_refusal_retry(); test_lost_port_and_identity(); test_callback_refusals(); test_runtime_force_recycle(); + test_host_reset_used_once(); puts("PASS: production teardown/worker reset-domain and refusal suite"); return 0; } diff --git a/tools/asahi/centauri-physical-recovery-tests.py b/tools/asahi/centauri-physical-recovery-tests.py index d9c843d666c5b..31b857da5fafa 100644 --- a/tools/asahi/centauri-physical-recovery-tests.py +++ b/tools/asahi/centauri-physical-recovery-tests.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: GPL-2.0-only """Exercise N1 recovery with reset-hidden old PCI objects and retained DMA. -Extract 15 production functions from this checkout; PCI core, provider hardware +Extract 16 production functions from this checkout; PCI core, provider hardware and scheduling are mocks. Negative controls must compile and fail a C assertion. This tests teardown/worker ownership, not physical sleep or fresh firmware boot. """ @@ -33,7 +33,21 @@ 'missing-owner-descriptor': ('owner', 'rw->descriptor || '), 'missing-owner-arena': ('owner', ' || rw->arena'), } -MUTATIONS = ('bound-guard-reverted', 'owner-guard-reverted', *OMISSIONS) +EDITS = { + 'host-reset-reused': ('force_recycle || reset_used ||', + 'force_recycle || (false && reset_used) ||'), + 'host-reset-not-recorded': ('\tmutex_lock(&cen_recovery_lock);\n' + '\trw->teardown_active = false;\n' + '\trw->force_recycle = false;\n' + '\trw->fault_only = false;\n' + '\trw->host_reset_used = true;\n', + '\tmutex_lock(&cen_recovery_lock);\n' + '\trw->teardown_active = false;\n' + '\trw->force_recycle = false;\n' + '\trw->fault_only = false;\n'), + 'sleep-keeps-reset-used': ('\t\trw->host_reset_used = false;\n', '\t\t(void)rw;\n'), +} +MUTATIONS = ('bound-guard-reverted', 'owner-guard-reverted', *OMISSIONS, *EDITS) def function(source, name): @@ -52,7 +66,9 @@ def function(source, name): def mutate(source, mutation): if not mutation: return source - if mutation in OMISSIONS: + if mutation in EDITS: + guard, replacement = EDITS[mutation] + elif mutation in OMISSIONS: owner, term = OMISSIONS[mutation] guard = GUARDS[owner] replacement = guard.replace(term, '') @@ -61,7 +77,7 @@ def mutate(source, mutation): replacement = '' # A stale mutation is a harness error, never a successful mutant kill. if source.count(guard) != 1: - raise ValueError(f'{mutation}: expected exactly one inventory guard') + raise ValueError(f'{mutation}: expected exactly one match') return source.replace(guard, replacement, 1) @@ -78,7 +94,8 @@ def generate(sources, mutation): 'centauri_t6050_free_boot', 'centauri_t6050_beta_call', 'centauri_t6050_domain_fence', 'centauri_t6050_quiesce', 'centauri_t6050_remove', 'centauri_t6050_rewake_teardown', - 'centauri_t6050_link_active', 'centauri_t6050_rewake_worker')] + 'centauri_t6050_link_active', 'centauri_t6050_rewake_worker', + 'centauri_t6050_pm_notify')] fixture = Path(__file__).with_suffix('.c').read_text() marker = '/* ACTUAL_FUNCTIONS */' if fixture.count(marker) != 1: @@ -94,7 +111,7 @@ def run(args, out): c.write_text(generate(sources, args.mutation)) command = shlex.split(os.environ.get('CC', 'cc')) + [ '-std=gnu11', '-Wall', '-Wextra', '-Werror', '-Wno-unused-function', - '-g0', '-O1', str(c), '-o', str(binary)] + '-Wno-unused-parameter', '-g0', '-O1', str(c), '-o', str(binary)] # Build/extraction errors propagate as Python tracebacks, which the runner # rejects for KILL jobs. Only an executed model assertion counts as a kill. subprocess.run(command, check=True, timeout=60) @@ -102,7 +119,7 @@ def run(args, out): (out / 'model.log').write_text(result.stdout + result.stderr) (out / 'result.json').write_text(json.dumps({ 'mutation': args.mutation, 'exit': result.returncode, - 'extracted_functions': 15, 'compiler_command': command, + 'extracted_functions': 16, 'compiler_command': command, 'sources': {path: hashlib.sha256(text.encode()).hexdigest() for path, text in sources.items()}, }, indent=2) + '\n') diff --git a/tools/asahi/centauri-scan-tests.py b/tools/asahi/centauri-scan-tests.py index ff64985f6c2a6..165412ac57ae5 100755 --- a/tools/asahi/centauri-scan-tests.py +++ b/tools/asahi/centauri-scan-tests.py @@ -59,6 +59,9 @@ def extract_fn(source, name): #define time_before(a, b) ((long)((a) - (b)) < 0) #define time_after_eq(a, b) (!time_before(a, b)) #define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define REPLY_SIZE 64 +#define STATS_CHIP_COUNTERS 1 #define NL80211_BAND_2GHZ 0 #define NL80211_BAND_5GHZ 1 #define NL80211_BAND_6GHZ 2 @@ -71,6 +74,7 @@ def extract_fn(source, name): #define lockdep_assert_held(l) assert(*(l)) #define dev_warn(...) ((void)0) #define dev_err(...) ((void)0) +#define dev_info(...) ((void)0) struct device { int dummy; }; struct pci_dev { struct device dev; }; @@ -107,10 +111,14 @@ def extract_fn(source, name): bool station_signal_valid; unsigned long station_signal_at, connected_at; struct net_device_stats station_start; + bool configured, transport_paused, command_probe, stats_absent; + u64 stats_off, stats_off_slice2; + u8 stats_reply[REPLY_SIZE]; }; static unsigned long jiffies; static unsigned int bar_reads, sleep_calls, link_queries, stop_commands; +static unsigned int stats_queries; static unsigned int stop_event_delay, complete_after_drains, drains_seen; static unsigned int sleep_step_ms, sleep_sets_host_sleep_at; static unsigned int sleep_completes_on_drain_at, fault_reports; @@ -122,12 +130,15 @@ def extract_fn(source, name): static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info *sinfo); +static int stats_query(struct centauri_t6050_wifi *wifi, unsigned int bit, + u8 scope, u64 slice, size_t *size); static void mutex_lock(int *l) { assert(!*l); *l = 1; } static void mutex_unlock(int *l) { assert(*l); *l = 0; } static void put_unaligned_le16(u16 v, void *p) { memcpy(p, &v, 2); } static u32 readl(void *addr) { (void)addr; bar_reads++; return bar_state; } static int firmware_band(int band) { return band; } +static void put_unaligned_le64(u64 v, void *p) { memcpy(p, &v, 8); } static int ieee80211_frequency_to_channel(u32 freq) { return freq == 2412 ? 1 : 36; @@ -150,6 +161,27 @@ def extract_fn(source, name): wifi->station_signal_at = jiffies; return 0; } +static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, + const void *payload, size_t size, void *reply, + size_t *reply_size, bool *submitted) +{ + (void)payload; (void)size; (void)reply; (void)submitted; + assert(wifi->lock && wifi->command_probe && id == 0xb0001); + stats_queries++; + *reply_size = 0; + return 0; +} +/* The reports' parsing is not modelled; each one goes through stats_query(). */ +static void fill_station_stats(struct centauri_t6050_wifi *wifi, + struct station_info *sinfo) +{ + size_t size; + + (void)sinfo; + assert(wifi->lock); + stats_query(wifi, STATS_CHIP_COUNTERS, 2, 0, &size); + assert(!wifi->command_probe); +} static int command(struct centauri_t6050_wifi *wifi, u32 id, const void *payload, size_t size) { @@ -198,6 +230,7 @@ def extract_fn(source, name): fill_station(g_wifi, &sinfo); mutex_unlock(&g_wifi->lock); assert(link_queries == 0); + assert(stats_queries == 0); assert(sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)); assert(sinfo.signal == -61); station_ran_in_sleep = true; @@ -234,6 +267,7 @@ def extract_fn(source, name): wifi.control = &ctrl; wifi.a.bar = fake_bar; wifi.netdev = &ndev; + wifi.configured = true; req.n_channels = 1; req.channels[0] = &ch1; wifi.scan_request = &req; @@ -250,11 +284,15 @@ def extract_fn(source, name): stop_refuse_342 = stop_refuse_other = false; sleep_sets_abort = sleep_sets_host_sleep = false; sleep_checks_station = station_ran_in_sleep = false; + stats_queries = 0; } int main(void) { - /* 1. Normal scan unlocks during sleep; concurrent fill_station skips FW cmd 20. */ + /* + * 1. Normal scan unlocks during sleep; concurrent fill_station skips FW + * cmd 20 and the statistics queries. + */ reset_wifi(); complete_after_drains = 3; sleep_checks_station = true; @@ -263,7 +301,7 @@ def extract_fn(source, name): mutex_unlock(&wifi.lock); assert(sleep_calls >= 1 && station_ran_in_sleep && link_queries == 0); - /* Outside a scan, fill_station refreshes link status normally. */ + /* Outside a scan, fill_station refreshes link status and statistics. */ wifi.scan_request = NULL; { struct station_info sinfo = {}; @@ -272,6 +310,7 @@ def extract_fn(source, name): fill_station(&wifi, &sinfo); mutex_unlock(&wifi.lock); assert(link_queries == 1 && sinfo.signal == -52); + assert(stats_queries == 1); } /* 2. Scan abort sends 65537 and waits for 0x80000005 completion. */ @@ -783,6 +822,7 @@ def main(): "scan_transport_stopped", "scan_stop_chunk", "scan_profile27", + "stats_query", "fill_station", ) ) @@ -821,6 +861,12 @@ def main(): "if (time_after_eq(jiffies, wifi->station_signal_at + HZ))", "link_queries == 0", ), + ( + "station-stats-during-scan", + "if (!wifi->configured || wifi->scan_request || wifi->command_failed ||", + "if (!wifi->configured || wifi->command_failed ||", + "stats_queries == 0", + ), ( "timeout-skips-stop-wait", "\t\t\tret = scan_stop_chunk(wifi, done);\n\t\t\treturn ret == -EIO ? -EIO : -ETIMEDOUT;", diff --git a/tools/asahi/centauri-wifi-pm-tests.py b/tools/asahi/centauri-wifi-pm-tests.py index 4e21cf3aebecd..3f3b78819edf4 100644 --- a/tools/asahi/centauri-wifi-pm-tests.py +++ b/tools/asahi/centauri-wifi-pm-tests.py @@ -56,6 +56,7 @@ def block(source, name): #define lockdep_assert_held(l) assert(*(l)) #define msecs_to_jiffies(x) (x) #define time_before(a, b) ((long)((a) - (b)) < 0) +#define time_after(a, b) time_before(b, a) #define time_after_eq(a, b) (!time_before(a, b)) static unsigned long jiffies; struct centauri_t6050_wifi; @@ -73,7 +74,7 @@ def block(source, name): struct { u8 *window, *bar; uint64_t window_dma; } a; int lock, connect_work, link_work, data_tx_queue; bool command_failed, stage_fault_logged, stopping, connect_abort, disconnect_pending, connecting, connected; - bool reply_valid, pm_prepared, pm_wowlan_armed; + bool reply_valid, command_probe, pm_prepared, pm_wowlan_armed; bool data_tx_busy[DATA_TX_SLOTS]; bool host_events_fallback_logged; bool configured, tx_paused, transport_paused, host_sleep_sent, transport_sleep_sent; @@ -90,7 +91,7 @@ def block(source, name): static const u16 ring_size[5] = {0, 64}; static unsigned int doorbells, drains, notifications, consumer, tx_drain_rounds, info_logs; static unsigned int reads, index_reads, reports, applies, fault_reports; -static bool rtnl_held, fail_firmware_report; +static bool rtnl_held, fail_firmware_report, consume_commands; #define ASSERT_RTNL() assert(rtnl_held) static void dev_info(const void *dev, const char *fmt, ...) { (void)dev; (void)fmt; info_logs++; } @@ -114,7 +115,8 @@ def block(source, name): { assert(wifi->lock); (void)array; (void)index; index_reads++; return consumer; } static void index_write(struct centauri_t6050_wifi *wifi, int array, int index, u16 value) { assert(wifi->lock); (void)array; (void)index; (void)value; - if (submission_probe) assert(*submission_probe); } + if (submission_probe) assert(*submission_probe); + if (consume_commands) consumer = value; } static void put_unaligned_le16(u16 value, void *p) { memcpy(p, &value, 2); } static void put_unaligned_le32(u32 value, void *p) { memcpy(p, &value, 4); } static void put_unaligned_le64(uint64_t value, void *p) { memcpy(p, &value, 8); } @@ -219,7 +221,7 @@ def block(source, name): wiphy.wifi = &wifi; doorbells = drains = notifications = consumer = tx_drain_rounds = info_logs = 0; reads = index_reads = reports = applies = fault_reports = 0; - rtnl_held = fail_firmware_report = false; + rtnl_held = fail_firmware_report = consume_commands = false; firmware_status = last_cmd6_tlv280_len = mock_fw_transient_status = 0; mock_reply_command = 131073; mock_fw_max_events = last_cmd6_tlv280_count = 0; @@ -384,11 +386,47 @@ def block(source, name): assert(fault_reports == 0); } +/* Only an optional query the firmware consumed is given up without a fault. */ +static void test_optional_query(void) +{ + reset(); + wifi.command_probe = consume_commands = true; + transport_mode = TIMEOUT; + mutex_lock(&wifi.lock); + assert(command_reply(&wifi, 9, NULL, 0, NULL, NULL, NULL) == -ENODATA); + assert(jiffies > 500 && jiffies < 1500 && !wifi.command_failed && !fault_reports); + /* The next command gets its own reply. */ + wifi.command_probe = false; + transport_mode = REPLY; + mock_reply_command = 0; + assert(command_reply(&wifi, 9, NULL, 0, NULL, NULL, NULL) == 0); + mutex_unlock(&wifi.lock); + + /* The firmware still holds the payload: the full deadline, then a fault. */ + reset(); + wifi.command_probe = true; + transport_mode = TIMEOUT; + mutex_lock(&wifi.lock); + assert(command_reply(&wifi, 9, NULL, 0, NULL, NULL, NULL) == -ETIMEDOUT); + mutex_unlock(&wifi.lock); + assert(jiffies >= 1500 && wifi.command_failed && fault_reports == 1); + + /* Any other command the firmware consumed without a reply faults. */ + reset(); + consume_commands = true; + transport_mode = TIMEOUT; + mutex_lock(&wifi.lock); + assert(command_reply(&wifi, 43, NULL, 0, NULL, NULL, NULL) == -ETIMEDOUT); + mutex_unlock(&wifi.lock); + assert(jiffies >= 1500 && wifi.command_failed && fault_reports == 1); +} + int main(void) { test_registered_pm_contract(); test_host_sleep_gate(); test_deferred_regulatory(); + test_optional_query(); prepare(); assert(!wifi.pm_disconnect.seen); expect_error(0); /* Missing transaction remains missing. */ @@ -595,7 +633,8 @@ def main(): receipt = re.search(r"struct centauri_wifi_disconnect_receipt \{.*?\n\};", source, re.S) if not receipt: raise RuntimeError("Production receipt structure missing") - names = ["command_reply", "wait_disconnect", "data_tx_in_flight", + names = ["command_payload_consumed", "command_reply", "wait_disconnect", + "data_tx_in_flight", "drain_tx_in_flight", "wifi_disconnect", "centauri_t6050_wifi_pm_prepare", "centauri_t6050_wifi_pm_error", "centauri_t6050_wifi_pm_disconnected", "centauri_t6050_wifi_wowlan_armed", @@ -668,6 +707,10 @@ def main(): ("lost-deferred-country", "country_set(wifi, wifi->country_deferred);", "(void)wifi;"), ("ignored-regulatory-transport-loss", "if (wifi->command_failed)\n\t\t\treturn -EIO;", "if (false)\n\t\t\treturn -EIO;"), + ("optional-query-held-payload", + "command_payload_consumed(wifi, slot)) {", "(command_payload_consumed(wifi, slot) || true)) {"), + ("ordinary-command-given-up", "if (wifi->command_probe &&\n\t\t time_after(", + "if (time_after("), ] failures = { 'lost-suspend-error': 'centauri_t6050_wifi_pm_error(&control) == expected', @@ -680,6 +723,8 @@ def main(): 'regulatory-in-sleep': '!index_reads && !doorbells && !drains && !reads && !reports && !applies', 'lost-deferred-country': 'doorbells == 2 && reports == 1 && applies == 1', 'ignored-regulatory-transport-loss': 'alpha_resume_locked(&wifi) == -EIO', + 'optional-query-held-payload': 'command_reply(&wifi, 9, NULL, 0, NULL, NULL, NULL) == -ETIMEDOUT', + 'ordinary-command-given-up': 'command_reply(&wifi, 43, NULL, 0, NULL, NULL, NULL) == -ETIMEDOUT', } for name, before, after in negative_controls if args.negative_controls else []: if before not in production: diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index c60b58486a190..34ab9cfb477b8 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -261,7 +261,9 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('bound-guard-reverted', 'owner-guard-reverted', 'missing-control-firmware', 'missing-control-descriptor', 'missing-owner-ipc', 'missing-owner-firmware', - 'missing-owner-descriptor', 'missing-owner-arena'), out()), + 'missing-owner-descriptor', 'missing-owner-arena', + 'host-reset-reused', 'host-reset-not-recorded', + 'sleep-keeps-reset-used'), out()), # A DP audio PCM open reads a replaced service's successor; a gone service opens unplugged. 'dcpaud-open-fallback-tests.py': [([], PASS)] + mutations('--mutation', ('no-retry', 'wait-any', 'gone-fails', 'keep-cookie', 'mask-all'), []), # a7638d51880c, the parent of the fix, ran pm_runtime_force_suspend in the late