Linux wireless drivers development
 help / color / mirror / Atom feed
From: Felix Fietkau <nbd@nbd.name>
To: linux-wireless@vger.kernel.org
Subject: [PATCH mt76-next 16/20] wifi: mt76: mt7996: read the TSF twice against the firmware's latch
Date: Tue, 24 Nov 2026 09:43:05 +0100	[thread overview]
Message-ID: <20261124084309.1981968-16-nbd@nbd.name> (raw)
In-Reply-To: <20261124084309.1981968-1-nbd@nbd.name>

__mt7996_get_tsf() latches the timer into the UTTR0/UTTR1 register pair
of the band and reads the pair. The firmware reads the TSF through the
same pair, with no lock. It does so in its pre-TBTT work, for Critical
Update, TTLM and TWT, and for a few commands. A firmware latch between
the latch and the reads of the host gives the host the value of the
firmware. That value can belong to another timer of the band, or be off
by 2^32 us at a low word wrap. Reachable via mt7996_get_tsf(), the TWT
setup and the TTLM and AP Removal Timer code.

Latch and read twice, and take the second value only if it is at most
about 1 ms later. Retry a pair that does not agree a few times, then
warn once and return the last value. This lowers the rate of the race
and does not remove it, since two reads with the same value of the
firmware still agree. The firmware holds no lock or semaphore for the
register pair, so the host cannot exclude it. The GET_TSF command of
the firmware queues behind a deferred beacon template, which breaks the
pairing of the TSF with ktime_get_boottime_ns() that the TTLM and AP
Removal Timer code needs.

Fixes: 98686cd21624 ("wifi: mt76: mt7996: add driver for MediaTek Wi-Fi 7 (802.11be) devices")
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 .../net/wireless/mediatek/mt76/mt7996/main.c  | 39 ++++++++++++++-----
 .../wireless/mediatek/mt76/mt7996/mt7996.h    |  2 +
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
index 3486781609e3..b778728bcecc 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
@@ -2350,15 +2350,29 @@ mt7996_get_stats(struct ieee80211_hw *hw,
 	return 0;
 }
 
-u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif_link *link)
+static u64 mt7996_tsf_latch(struct mt7996_dev *dev, u8 band, u16 n)
 {
-	struct mt7996_phy *phy = mt7996_vif_link_phy(link);
-	struct mt7996_dev *dev = mt7996_hw_dev(hw);
 	union {
 		u64 t64;
 		u32 t32[2];
 	} tsf;
+
+	mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
+		 MT_LPON_TCR_SW_READ);
+	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
+	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
+
+	return tsf.t64;
+}
+
+u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif_link *link)
+{
+	struct mt7996_phy *phy = mt7996_vif_link_phy(link);
+	struct mt7996_dev *dev = mt7996_hw_dev(hw);
+	u64 first = 0, tsf = 0;
+	u8 band;
 	u16 n;
+	int i;
 
 	if (!phy)
 		return 0;
@@ -2367,13 +2381,20 @@ u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif_link *link)
 
 	n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
 					       : link->mt76.omac_idx;
-	/* TSF software read */
-	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
-		 MT_LPON_TCR_SW_READ);
-	tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(phy->mt76->band_idx));
-	tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(phy->mt76->band_idx));
+	band = phy->mt76->band_idx;
 
-	return tsf.t64;
+	for (i = 0; i < MT7996_TSF_READ_TRIES; i++) {
+		first = mt7996_tsf_latch(dev, band, n);
+		tsf = mt7996_tsf_latch(dev, band, n);
+		if (tsf - first <= MT7996_TSF_READ_SPAN_US)
+			return tsf;
+	}
+
+	dev_warn_once(dev->mt76.dev,
+		      "TSF reads of band %u do not agree: %llu, %llu\n",
+		      band, first, tsf);
+
+	return tsf;
 }
 
 static u64
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
index f5bca533c3e1..65a0ef6b1161 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
@@ -21,6 +21,8 @@
 
 #define MT7996_WATCHDOG_TIME		(HZ / 10)
 #define MT7996_RESET_TIMEOUT		(30 * HZ)
+#define MT7996_TSF_READ_TRIES		3
+#define MT7996_TSF_READ_SPAN_US		1000
 
 #define MT7996_TX_RING_SIZE		2048
 #define MT7996_TX_MCU_RING_SIZE		256
-- 
2.47.3


  parent reply	other threads:[~2026-11-24  8:43 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  8:42 [PATCH mt76-next 01/20] wifi: mt76: mt7915: dump the firmware memory of mt7916 Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 02/20] wifi: mt76: mt7915: set the LED GPIO mux " Felix Fietkau
2026-11-24  8:53   ` Lorenzo Bianconi
2026-11-24  8:42 ` [PATCH mt76-next 03/20] wifi: mt76: fail an MCU command started during an MCU reset Felix Fietkau
2026-11-24  8:54   ` Lorenzo Bianconi
2026-11-24  8:42 ` [PATCH mt76-next 04/20] wifi: mt76: mark an MCU command that only reads Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 05/20] wifi: mt76: retry only an MCU command that reads Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 06/20] wifi: mt76: report an MCU command whose outcome is unknown Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 07/20] wifi: mt76: mt7996: mark the MCU commands that only read Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 08/20] wifi: mt76: mt7996: reset after an MCU reset lost a state command Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 09/20] wifi: mt76: mt7915: mark the MCU commands that only read Felix Fietkau
2026-11-24  8:42 ` [PATCH mt76-next 10/20] wifi: mt76: mt7915: hold the device mutex in the rate control work Felix Fietkau
2026-11-24  8:56   ` Lorenzo Bianconi
2026-11-24  8:43 ` [PATCH mt76-next 11/20] wifi: mt76: mt7915: hold the device mutex in the station and regd updates Felix Fietkau
2026-11-24  8:58   ` Lorenzo Bianconi
2026-11-24  8:43 ` [PATCH mt76-next 12/20] wifi: mt76: mt7915: reset after an MCU reset lost a state command Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 13/20] wifi: mt76: mt7996: clear the PS state of stations at a reset Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 14/20] wifi: mt76: mt7996: apply the PS sync bitmap to MLD stations alone Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 15/20] wifi: mt76: mt7996: keep the TWT state out of the PS flag Felix Fietkau
2026-11-24  8:43 ` Felix Fietkau [this message]
2026-11-24  8:43 ` [PATCH mt76-next 17/20] wifi: mt76: mt7996: check a TSF write against the firmware's latch Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 18/20] wifi: mt76: mt7915: keep the TWT state out of the PS flag Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 19/20] wifi: mt76: mt7915: clear the PS flag of a station at a reset Felix Fietkau
2026-11-24  8:43 ` [PATCH mt76-next 20/20] wifi: mt76: mt7996: set the default link again when a link is reused Felix Fietkau
2026-11-24  8:53 ` [PATCH mt76-next 01/20] wifi: mt76: mt7915: dump the firmware memory of mt7916 Lorenzo Bianconi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124084309.1981968-16-nbd@nbd.name \
    --to=nbd@nbd.name \
    --cc=linux-wireless@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox