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 17/20] wifi: mt76: mt7996: check a TSF write against the firmware's latch
Date: Tue, 24 Nov 2026 09:43:06 +0100	[thread overview]
Message-ID: <20261124084309.1981968-17-nbd@nbd.name> (raw)
In-Reply-To: <20261124084309.1981968-1-nbd@nbd.name>

mt7996_set_tsf() and mt7996_offset_tsf() write the new value or the
offset to the UTTR0/UTTR1 pair of the band. They then write the write
or adjust mode to the TCR of the timer. The firmware latches its own TSF
reads into the same register pair, with no lock. A firmware read
between the UTTR writes and the mode write replaces the value of the
host. The timer then loads the latched TSF, or adds a whole TSF as the
offset. Reachable via the debugfs tsf file of mac80211 and, for the
offset, the mesh TSF sync.

Write mode 0 after the write or adjust mode, as the TSF write of the
firmware does. The firmware updates other fields of the TCR by
read-modify-write. If the hardware reads the mode back, such a write
would otherwise start the load or the add again. After a write, read
the TSF back. If it is not within about 1 ms after the written value,
write once more. This lowers the rate of the race and does not remove
it.

Do not repeat an adjust, since a check that races itself would add the
offset a second time. The race thus stays for mt7996_offset_tsf(). No
host write order prevents it, and the firmware holds no lock or
semaphore for the register pair.

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  | 42 +++++++++++--------
 .../wireless/mediatek/mt76/mt7996/mt7996.h    |  1 +
 2 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/main.c b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
index b778728bcecc..c220376cded6 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/main.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/main.c
@@ -2411,6 +2411,20 @@ mt7996_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 	return ret;
 }
 
+static void
+mt7996_tsf_load(struct mt7996_dev *dev, u8 band, u16 n, u64 val, u32 mode)
+{
+	union {
+		u64 t64;
+		u32 t32[2];
+	} tsf = { .t64 = val, };
+
+	mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
+	mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
+	mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE, mode);
+	mt76_clear(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE);
+}
+
 static void
 mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	       u64 timestamp)
@@ -2419,11 +2433,8 @@ mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
 	struct mt7996_vif_link *link;
 	struct mt7996_phy *phy;
-	union {
-		u64 t64;
-		u32 t32[2];
-	} tsf = { .t64 = timestamp, };
 	u16 n;
+	int i;
 
 	mutex_lock(&dev->mt76.mutex);
 
@@ -2437,11 +2448,13 @@ mt7996_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	if (!phy)
 		goto unlock;
 
-	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
-	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
-	/* TSF software overwrite */
-	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
-		 MT_LPON_TCR_SW_WRITE);
+	for (i = 0; i < MT7996_TSF_WRITE_TRIES; i++) {
+		mt7996_tsf_load(dev, phy->mt76->band_idx, n, timestamp,
+				MT_LPON_TCR_SW_WRITE);
+		if (__mt7996_get_tsf(hw, link) - timestamp <=
+		    MT7996_TSF_READ_SPAN_US)
+			break;
+	}
 
 unlock:
 	mutex_unlock(&dev->mt76.mutex);
@@ -2455,10 +2468,6 @@ mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	struct mt7996_dev *dev = mt7996_hw_dev(hw);
 	struct mt7996_vif_link *link;
 	struct mt7996_phy *phy;
-	union {
-		u64 t64;
-		u32 t32[2];
-	} tsf = { .t64 = timestamp, };
 	u16 n;
 
 	mutex_lock(&dev->mt76.mutex);
@@ -2473,11 +2482,8 @@ mt7996_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 
 	n = link->mt76.omac_idx > HW_BSSID_MAX ? HW_BSSID_0
 					       : link->mt76.omac_idx;
-	mt76_wr(dev, MT_LPON_UTTR0(phy->mt76->band_idx), tsf.t32[0]);
-	mt76_wr(dev, MT_LPON_UTTR1(phy->mt76->band_idx), tsf.t32[1]);
-	/* TSF software adjust*/
-	mt76_rmw(dev, MT_LPON_TCR(phy->mt76->band_idx, n), MT_LPON_TCR_SW_MODE,
-		 MT_LPON_TCR_SW_ADJUST);
+	mt7996_tsf_load(dev, phy->mt76->band_idx, n, timestamp,
+			MT_LPON_TCR_SW_ADJUST);
 
 unlock:
 	mutex_unlock(&dev->mt76.mutex);
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
index 65a0ef6b1161..e6bf1151447f 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
@@ -23,6 +23,7 @@
 #define MT7996_RESET_TIMEOUT		(30 * HZ)
 #define MT7996_TSF_READ_TRIES		3
 #define MT7996_TSF_READ_SPAN_US		1000
+#define MT7996_TSF_WRITE_TRIES		2
 
 #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 ` [PATCH mt76-next 16/20] wifi: mt76: mt7996: read the TSF twice against the firmware's latch Felix Fietkau
2026-11-24  8:43 ` Felix Fietkau [this message]
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-17-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