Skip to contentSkip to conversation

wifi: mt76: keep the Null frame of an AP on its AC queue

PATCH New Felix Fietkau 1 participant 16 messages Nov 26

Subsystems (2)
lore
Share & tools
lore

Felix Fietkau

author maintainer
#0 lore raw

PATCH 1/16 wifi: mt76: keep the Null frame of an AP on its AC queue

Since commit 331e766e75d2 ("wifi: mt76: route nullfunc frames to PSD/ALTX queue"), every Null frame goes to the PSD queue, which the drivers with MT_DRV_HW_MGMT_TXQ map to ALTX. On mt7996 the firmware does not hold an ALTX frame for a station in power save. The inactivity poll of hostapd thus goes on air while the station dozes, gets no ACK, and hostapd disassociates the station.

Keep the Null frame of an AP interface on its AC queue, so that the hardware buffers it until the station wakes. The Null frames of a station interface stay on the PSD queue, since the PM=1 frame before an off-channel period must leave at once.

Fixes 331e766e75d2 ("wifi: mt76: route nullfunc frames to PSD/ALTX queue")
---
 drivers/net/wireless/mediatek/mt76/tx.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/tx.c +14 -1
index d8036466f495..1621fc3e2cfb 100644
@@ -707,6 +707,19 @@ void mt76_txq_schedule(struct mt76_phy *phy, enum mt76_txq_id qid)
 }
 EXPORT_SYMBOL_GPL(mt76_txq_schedule);
 
+static bool
+mt76_tx_ps_bufferable(struct sk_buff *skb)
+{
+	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
+	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+	struct ieee80211_vif *vif = info->control.vif;
+
+	if (ieee80211_is_any_nullfunc(hdr->frame_control))
+		return vif && vif->type == NL80211_IFTYPE_AP;
+
+	return ieee80211_is_bufferable_mmpdu(skb);
+}
+
 static int
 mt76_txq_schedule_pending_wcid(struct mt76_phy *phy, struct mt76_wcid *wcid,
 			       struct sk_buff_head *head)
@@ -727,7 +740,7 @@ mt76_txq_schedule_pending_wcid(struct mt76_phy *phy, struct mt76_wcid *wcid,
 		    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
 		    !ieee80211_is_data_present(hdr->frame_control) &&
 		    (!(wcid->tx_info & MT_WCID_TX_INFO_SET) ||
-		     !ieee80211_is_bufferable_mmpdu(skb) ||
+		     !mt76_tx_ps_bufferable(skb) ||
 		     ieee80211_is_deauth(hdr->frame_control) ||
 		     head == &wcid->tx_offchannel))
 			qid = MT_TXQ_PSD;
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 2/16 wifi: mt76: mt7996: limit the HE and EHT maps of a peer to its rx_nss

The firmware takes the stream count of an HE or EHT peer from its MCS maps. mt7996_mcu_set_sta_he_mcs() keeps the peer codes of the streams from rx_nss up, and mt7996_mcu_sta_eht_tlv() copies the peer EHT maps. A lower rx_nss from an Operating Mode Notification thus never reaches the firmware.

Limit both maps to rx_nss. A 6 GHz HE peer without EHT that has fewer streams at 160 MHz than at 80 MHz now gets the 160 MHz count at every width. The rate update after a notification sends no EHT maps, so for EHT only the notification in the association request takes effect.

Fixes 98686cd21624 ("wifi: mt76: mt7996: add driver for MediaTek Wi-Fi 7 (802.11be) devices")
Fixes 92aa2da9fa49 ("wifi: mt76: mt7996: enable EHT support in firmware")
---
 .../net/wireless/mediatek/mt76/mt7996/mcu.c   | 34 ++++++++++++++++---
 1 file changed, 29 insertions(+), 5 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7996/mcu.c +29 -5
index e4fd4b672e22..a3717dffe9ad 100644
@@ -176,9 +176,26 @@ mt7996_mcu_set_sta_he_mcs(struct ieee80211_link_sta *link_sta,
 		mcs_map |= mcs << (nss * 2);
 	}
 
+	mcs_map |= GENMASK(15, 2 * max_nss);
+
 	*he_mcs = cpu_to_le16(mcs_map);
 }
 
+static void
+mt7996_mcu_set_sta_eht_mcs(struct ieee80211_link_sta *link_sta,
+			   u8 *eht_mcs, const u8 *mcs_map, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		u8 nss = u8_get_bits(mcs_map[i], IEEE80211_EHT_MCS_NSS_RX);
+
+		nss = min(nss, link_sta->rx_nss);
+		eht_mcs[i] = u8_replace_bits(mcs_map[i], nss,
+					     IEEE80211_EHT_MCS_NSS_RX);
+	}
+}
+
 static void
 mt7996_mcu_set_sta_vht_mcs(struct ieee80211_link_sta *link_sta,
 			   __le16 *vht_mcs, const u16 *mask)
@@ -1735,14 +1752,21 @@ mt7996_mcu_sta_eht_tlv(struct sk_buff *skb,
 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0) {
-		memcpy(eht->mcs_map_bw20, &mcs_map->only_20mhz,
-		       sizeof(eht->mcs_map_bw20));
+		mt7996_mcu_set_sta_eht_mcs(link_sta, eht->mcs_map_bw20,
+					   (const u8 *)&mcs_map->only_20mhz,
+					   sizeof(eht->mcs_map_bw20));
 		return;
 	}
 
-	memcpy(eht->mcs_map_bw80, &mcs_map->bw._80, sizeof(eht->mcs_map_bw80));
-	memcpy(eht->mcs_map_bw160, &mcs_map->bw._160, sizeof(eht->mcs_map_bw160));
-	memcpy(eht->mcs_map_bw320, &mcs_map->bw._320, sizeof(eht->mcs_map_bw320));
+	mt7996_mcu_set_sta_eht_mcs(link_sta, eht->mcs_map_bw80,
+				   (const u8 *)&mcs_map->bw._80,
+				   sizeof(eht->mcs_map_bw80));
+	mt7996_mcu_set_sta_eht_mcs(link_sta, eht->mcs_map_bw160,
+				   (const u8 *)&mcs_map->bw._160,
+				   sizeof(eht->mcs_map_bw160));
+	mt7996_mcu_set_sta_eht_mcs(link_sta, eht->mcs_map_bw320,
+				   (const u8 *)&mcs_map->bw._320,
+				   sizeof(eht->mcs_map_bw320));
 }
 
 static void
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 3/16 wifi: mt76: mt7915: send the current SM power save mode in STA_REC_HT

In reply to Felix Fietkau

mt7915_mcu_sta_ht_tlv() sends the SM power save bits of the HT capabilities of the association, which mac80211 does not update when the peer changes its mode by an Action frame. A later STA_REC update, for example after a firmware reset, thus restores the old mode, from which the MT7915 chip takes the stream limit of the peer.

Send the current mode of the peer, as STA_REC_RA and the MMPS update already do. For the peer of a station or mesh interface, mac80211 keeps the mode disabled, since only a non-AP STA indicates it.

Fixes e57b7901469f ("mt76: add mac80211 driver for MT7915 PCIe-based chipsets")
---
 .../net/wireless/mediatek/mt76/mt7915/mcu.c   | 33 ++++++++++---------
 1 file changed, 18 insertions(+), 15 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7915/mcu.c +18 -15
index 5da3bfaa5c53..6a19f52d9932 100644
@@ -1031,6 +1031,21 @@ mt7915_mcu_sta_muru_tlv(struct mt7915_dev *dev, struct sk_buff *skb,
 		HE_MAC(CAP3_RX_CTRL_FRAME_TO_MULTIBSS, elem->mac_cap_info[3]);
 }
 
+static enum mcu_mmps_mode
+mt7915_mcu_get_mmps_mode(enum ieee80211_smps_mode smps)
+{
+	switch (smps) {
+	case IEEE80211_SMPS_OFF:
+		return MCU_MMPS_DISABLE;
+	case IEEE80211_SMPS_STATIC:
+		return MCU_MMPS_STATIC;
+	case IEEE80211_SMPS_DYNAMIC:
+		return MCU_MMPS_DYNAMIC;
+	default:
+		return MCU_MMPS_DISABLE;
+	}
+}
+
 static void
 mt7915_mcu_sta_ht_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
 		      struct ieee80211_sta *sta)
@@ -1045,6 +1060,9 @@ mt7915_mcu_sta_ht_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
 
 	if (!mvif->cap.ht_ldpc)
 		cap &= ~IEEE80211_HT_CAP_LDPC_CODING;
+	u16p_replace_bits(&cap,
+			  mt7915_mcu_get_mmps_mode(sta->deflink.smps_mode),
+			  IEEE80211_HT_CAP_SM_PS);
 
 	tlv = mt76_connac_mcu_add_tlv(skb, STA_REC_HT, sizeof(*ht));
 
@@ -1429,21 +1447,6 @@ mt7915_mcu_sta_bfee_tlv(struct mt7915_dev *dev, struct sk_buff *skb,
 	bfee->fb_identity_matrix = (nrow == 1 && tx_ant == 2);
 }
 
-static enum mcu_mmps_mode
-mt7915_mcu_get_mmps_mode(enum ieee80211_smps_mode smps)
-{
-	switch (smps) {
-	case IEEE80211_SMPS_OFF:
-		return MCU_MMPS_DISABLE;
-	case IEEE80211_SMPS_STATIC:
-		return MCU_MMPS_STATIC;
-	case IEEE80211_SMPS_DYNAMIC:
-		return MCU_MMPS_DYNAMIC;
-	default:
-		return MCU_MMPS_DISABLE;
-	}
-}
-
 int mt7915_mcu_set_fixed_rate_ctrl(struct mt7915_dev *dev,
 				   struct ieee80211_vif *vif,
 				   struct ieee80211_sta *sta,
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 4/16 wifi: mt76: mt7996: take the HE caps of the band of each link

In reply to Felix Fietkau

mt76_connac_get_he_phy_cap() reads the band from the deflink of vif->drv_priv, which on mt7996 is link 0. Every other link of an MLD thus gets the HE caps of the band of link 0. In mt7996_mcu_sta_bfer_he() these caps limit the sounding dimensions in STA_REC_BF. With link 0 on 2.4 GHz, a 5 GHz HE peer without EHT gets fewer, none above 80 MHz.

Take the HE caps of the band of the phy of the link, like mt76_connac_get_eht_phy_cap() and mt7996_is_ebf_supported() do. Use them for UNI_BSS_INFO_HE_BASIC as well, whose MCS and NSS fields came from the same lookup.

Fixes a9eae65d97f3 ("wifi: mt76: mt7996: Export MLO AP capabilities to mac80211")
---
 drivers/net/wireless/mediatek/mt76/mt7996/mcu.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7996/mcu.c +13 -2
index a3717dffe9ad..ba0e2a266d0a 100644
@@ -123,6 +123,17 @@ mt7996_mcu_get_sta_nss(u16 mcs_map)
 	return nss ? nss - 1 : 0;
 }
 
+static const struct ieee80211_sta_he_cap *
+mt7996_mcu_get_he_phy_cap(struct mt7996_phy *phy, struct ieee80211_vif *vif)
+{
+	struct ieee80211_supported_band *sband;
+	struct mt76_phy *mphy = phy->mt76;
+
+	sband = mphy->hw->wiphy->bands[mphy->chandef.chan->band];
+
+	return ieee80211_get_he_iftype_cap(sband, vif->type);
+}
+
 static void
 mt7996_mcu_set_sta_he_mcs(struct ieee80211_link_sta *link_sta,
 			  struct mt7996_vif_link *link,
@@ -1144,7 +1155,7 @@ mt7996_mcu_bss_he_tlv(struct sk_buff *skb, struct ieee80211_vif *vif,
 	struct bss_info_uni_he *he;
 	struct tlv *tlv;
 
-	cap = mt76_connac_get_he_phy_cap(phy->mt76, vif);
+	cap = mt7996_mcu_get_he_phy_cap(phy, vif);
 	if (!cap)
 		return;
 
@@ -2052,7 +2063,7 @@ mt7996_mcu_sta_bfer_he(struct ieee80211_link_sta *link_sta,
 	const struct ieee80211_sta_he_cap *vc;
 	u8 snd_dim, sts;
 
-	vc = mt76_connac_get_he_phy_cap(phy->mt76, vif);
+	vc = mt7996_mcu_get_he_phy_cap(phy, vif);
 	if (!vc)
 		return;
 
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 5/16 wifi: mt76: mt7915: count the WA TX statistics from an unsolicited event

In reply to Felix Fietkau

The WA firmware answers the TX statistics query with seq 0, so mt7915_mcu_parse_response() takes a WA_TX_STAT event as the answer of any waited command. An answer that arrives after its own wait ended is taken by the next waited command, whose caller then reads the wrong payload. Reachable via a station dump with WED active, when the wait ends at a timeout or an MCU reset.

Count the event as unsolicited and send the query without a wait. TX_PACKETS of a station dump thus lags by one query. Send no query for a station index above 255, since the event of the mt7915 chip names only its low byte.

Fixes 161a7528e407 ("wifi: mt76: mt7915: rework tx packets counting when WED is active")
---
 .../net/wireless/mediatek/mt76/mt7915/mcu.c   | 114 ++++++++----------
 1 file changed, 51 insertions(+), 63 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7915/mcu.c +51 -63
index 6a19f52d9932..74c632a27c12 100644
@@ -195,9 +195,7 @@ mt7915_mcu_parse_response(struct mt76_dev *mdev, int cmd,
 		return -EINVAL;
 
 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
-	if (seq != rxd->seq &&
-	    !(rxd->eid == MCU_CMD_EXT_CID &&
-	      rxd->ext_eid == MCU_EXT_EVENT_WA_TX_STAT))
+	if (seq != rxd->seq)
 		return -EAGAIN;
 
 	if (cmd == MCU_CMD(PATCH_SEM_CONTROL)) {
@@ -464,6 +462,41 @@ mt7915_mcu_rx_ps_sync(struct mt7915_dev *dev, struct sk_buff *skb)
 	mt7915_mac_sta_ps_transition(dev, wcid, ps);
 }
 
+static void
+mt7915_mcu_rx_wa_tx_stat(struct mt7915_dev *dev, struct sk_buff *skb)
+{
+	struct mt7915_mcu_wa_tx_stat {
+		__le16 wcid;
+		u8 __rsv2[2];
+
+		/* tx_bytes is deprecated since WA byte counter uses u32,
+		 * which easily leads to overflow.
+		 */
+		__le32 tx_bytes;
+		__le32 tx_packets;
+	} __packed *res;
+	int offset = sizeof(struct mt76_connac2_mcu_rxd);
+	struct mt76_wcid *wcid;
+	u16 wlan_idx;
+
+	if (!is_mt7915(&dev->mt76))
+		offset += 4;
+
+	if (skb->len < offset + sizeof(*res))
+		return;
+
+	res = (struct mt7915_mcu_wa_tx_stat *)(skb->data + offset);
+	wlan_idx = le16_to_cpu(res->wcid);
+	if (is_mt7915(&dev->mt76))
+		wlan_idx &= 0xff;
+
+	rcu_read_lock();
+	wcid = mt76_wcid_ptr(dev, wlan_idx);
+	if (wcid)
+		wcid->stats.tx_packets += le32_to_cpu(res->tx_packets);
+	rcu_read_unlock();
+}
+
 static void
 mt7915_mcu_rx_ext_event(struct mt7915_dev *dev, struct sk_buff *skb)
 {
@@ -489,6 +522,9 @@ mt7915_mcu_rx_ext_event(struct mt7915_dev *dev, struct sk_buff *skb)
 	case MCU_EXT_EVENT_PS_SYNC:
 		mt7915_mcu_rx_ps_sync(dev, skb);
 		break;
+	case MCU_EXT_EVENT_WA_TX_STAT:
+		mt7915_mcu_rx_wa_tx_stat(dev, skb);
+		break;
 	default:
 		break;
 	}
@@ -515,14 +551,12 @@ void mt7915_mcu_rx_event(struct mt7915_dev *dev, struct sk_buff *skb)
 	struct mt76_connac2_mcu_rxd *rxd;
 
 	rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
-	if ((rxd->ext_eid == MCU_EXT_EVENT_THERMAL_PROTECT ||
-	     rxd->ext_eid == MCU_EXT_EVENT_FW_LOG_2_HOST ||
-	     rxd->ext_eid == MCU_EXT_EVENT_ASSERT_DUMP ||
-	     rxd->ext_eid == MCU_EXT_EVENT_PS_SYNC ||
-	     rxd->ext_eid == MCU_EXT_EVENT_BCC_NOTIFY ||
-	     !rxd->seq) &&
-	     !(rxd->eid == MCU_CMD_EXT_CID &&
-	       rxd->ext_eid == MCU_EXT_EVENT_WA_TX_STAT))
+	if (rxd->ext_eid == MCU_EXT_EVENT_THERMAL_PROTECT ||
+	    rxd->ext_eid == MCU_EXT_EVENT_FW_LOG_2_HOST ||
+	    rxd->ext_eid == MCU_EXT_EVENT_ASSERT_DUMP ||
+	    rxd->ext_eid == MCU_EXT_EVENT_PS_SYNC ||
+	    rxd->ext_eid == MCU_EXT_EVENT_BCC_NOTIFY ||
+	    !rxd->seq)
 		mt7915_mcu_rx_unsolicited_event(dev, skb);
 	else
 		mt76_mcu_rx_event(&dev->mt76, skb);
@@ -4309,22 +4343,12 @@ int mt7915_mcu_wed_wa_tx_stats(struct mt7915_dev *dev, u16 wlan_idx)
 	} __packed req = {
 		.cmd = cpu_to_le32(0x15),
 	};
-	struct mt7915_mcu_wa_tx_stat {
-		__le16 wcid;
-		u8 __rsv2[2];
-
-		/* tx_bytes is deprecated since WA byte counter uses u32,
-		 * which easily leads to overflow.
-		 */
-		__le32 tx_bytes;
-		__le32 tx_packets;
-	} __packed *res;
-	struct mt76_wcid *wcid;
-	struct sk_buff *skb;
-	int ret, len, offset;
-	u16 ret_wcid;
+	int len;
 
 	if (is_mt7915(&dev->mt76)) {
+		if (wlan_idx > 0xff)
+			return -EINVAL;
+
 		req.arg0 = cpu_to_le32(wlan_idx);
 		len = sizeof(req) - sizeof(req.arg2);
 	} else {
@@ -4333,44 +4357,8 @@ int mt7915_mcu_wed_wa_tx_stats(struct mt7915_dev *dev, u16 wlan_idx)
 		len = sizeof(req);
 	}
 
-	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WA_PARAM_CMD(QUERY) |
-					MT76_MCU_CMD_READ,
-					&req, len, true, &skb);
-	if (ret)
-		return ret;
-
-	offset = is_mt7915(&dev->mt76) ? 0 : 4;
-	if (skb->len < offset + sizeof(*res)) {
-		dev_kfree_skb(skb);
-		return -EINVAL;
-	}
-
-	skb_pull(skb, offset);
-
-	res = (struct mt7915_mcu_wa_tx_stat *)skb->data;
-
-	ret_wcid = le16_to_cpu(res->wcid);
-	if (is_mt7915(&dev->mt76))
-		ret_wcid &= 0xff;
-
-	if (ret_wcid != wlan_idx) {
-		ret = -EINVAL;
-		goto out;
-	}
-
-	rcu_read_lock();
-
-	wcid = mt76_wcid_ptr(dev, wlan_idx);
-	if (wcid)
-		wcid->stats.tx_packets += le32_to_cpu(res->tx_packets);
-	else
-		ret = -EINVAL;
-
-	rcu_read_unlock();
-out:
-	dev_kfree_skb(skb);
-
-	return ret;
+	return mt76_mcu_send_msg(&dev->mt76, MCU_WA_PARAM_CMD(QUERY) |
+				 MT76_MCU_CMD_READ, &req, len, false);
 }
 
 int mt7915_mcu_rf_regval(struct mt7915_dev *dev, u32 regidx, u32 *val, bool set)
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 6/16 wifi: mt76: keep an RX replay counter per group key id and TID

In reply to Felix Fietkau

Where mt76 strips the IV of a decrypted frame, mac80211 leaves its PN check to mt76_check_ccmp_pn(). That function uses per-TID counters that all keys of the wcid share. After a GTK rekey, frames of the new key thus read as replays until their PN passes that of the old key.

Keep counters per group key id and TID in the vif link of a station interface, and check a group addressed frame that a driver marks with its key id against them. Other interface types need none: an AP receives nothing under its own group key, and each IBSS or mesh peer has a group key of its own. All other frames keep the per-TID counters of their wcid, so that each driver can move to the new counters on its own.

---
 drivers/net/wireless/mediatek/mt76/mac80211.c | 95 ++++++++++++++-----
 drivers/net/wireless/mediatek/mt76/mt76.h     | 26 ++++-
 2 files changed, 97 insertions(+), 24 deletions(-)
drivers/net/wireless/mediatek/mt76/mac80211.c +72 -23
index a42a950c20cb..33f2bd5a25f7 100644
@@ -1292,33 +1292,58 @@ int mt76_get_survey(struct ieee80211_hw *hw, int idx,
 }
 EXPORT_SYMBOL_GPL(mt76_get_survey);
 
+static void
+mt76_key_rx_pn_get(struct ieee80211_key_conf *key, int tid, u8 *pn)
+{
+	struct ieee80211_key_seq seq;
+
+	ieee80211_get_key_rx_seq(key, tid, &seq);
+	if (key->cipher == WLAN_CIPHER_SUITE_GCMP ||
+	    key->cipher == WLAN_CIPHER_SUITE_GCMP_256)
+		memcpy(pn, seq.gcmp.pn, sizeof(seq.gcmp.pn));
+	else
+		memcpy(pn, seq.ccmp.pn, sizeof(seq.ccmp.pn));
+}
+
+static void
+mt76_wcid_group_key_setup(struct mt76_wcid *wcid,
+			  struct ieee80211_key_conf *key)
+{
+	struct mt76_rx_group_pn *group_pn = wcid->rx_group_pn;
+	int i;
+
+	if (!group_pn || key->keyidx >= ARRAY_SIZE(group_pn->pn))
+		return;
+
+	if (!mt76_key_has_rx_pn(key)) {
+		group_pn->key_mask &= ~BIT(key->keyidx);
+		return;
+	}
+
+	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
+		mt76_key_rx_pn_get(key, i, group_pn->pn[key->keyidx][i]);
+
+	group_pn->key_mask |= BIT(key->keyidx);
+}
+
 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
 			 struct ieee80211_key_conf *key)
 {
-	struct ieee80211_key_seq seq;
-	u8 *pn;
 	int i;
 
+	if (key && !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
+		mt76_wcid_group_key_setup(wcid, key);
+
 	mt76_wcid_key_link_setup(wcid, key);
 	if (!key || !mt76_key_has_rx_pn(key))
 		return;
 
-	if (key->cipher == WLAN_CIPHER_SUITE_GCMP ||
-	    key->cipher == WLAN_CIPHER_SUITE_GCMP_256)
-		pn = seq.gcmp.pn;
-	else
-		pn = seq.ccmp.pn;
-
 	/* data frame */
-	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
-		ieee80211_get_key_rx_seq(key, i, &seq);
-		memcpy(wcid->rx_key_pn[i], pn, sizeof(wcid->rx_key_pn[i]));
-	}
+	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
+		mt76_key_rx_pn_get(key, i, wcid->rx_key_pn[i]);
 
 	/* robust management frame */
-	ieee80211_get_key_rx_seq(key, -1, &seq);
-	memcpy(wcid->rx_key_pn[i], pn, sizeof(wcid->rx_key_pn[i]));
-
+	mt76_key_rx_pn_get(key, -1, wcid->rx_key_pn[i]);
 }
 EXPORT_SYMBOL(mt76_wcid_key_setup);
 
@@ -1411,13 +1436,25 @@ mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
 	*hw = mt76_phy_hw(dev, mstat.phy_idx);
 }
 
+static u8 *
+mt76_rx_group_pn(struct mt76_rx_status *status, int security_idx)
+{
+	struct mt76_rx_group_pn *group_pn = status->wcid->rx_group_pn;
+
+	if (!group_pn || !(group_pn->key_mask & BIT(status->key_id)))
+		return NULL;
+
+	return group_pn->pn[status->key_id][security_idx];
+}
+
 void mt76_check_ccmp_pn(struct sk_buff *skb)
 {
 	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 	struct mt76_wcid *wcid = status->wcid;
 	struct ieee80211_hdr *hdr;
-	struct mt76_wcid *pn_wcid;
 	int security_idx;
+	bool group;
+	u8 *rx_pn;
 	int ret;
 
 	if (!(status->flag & RX_FLAG_DECRYPTED))
@@ -1429,12 +1466,10 @@ void mt76_check_ccmp_pn(struct sk_buff *skb)
 	if (status->skip_pn_check)
 		return;
 
-	if (!wcid || !wcid->rx_check_pn)
+	if (!wcid)
 		return;
 
-	/* the links of an MLD peer share one PN space per key */
-	pn_wcid = mt76_wcid_primary(wcid);
-
+	group = status->group && wcid->rx_group_pn;
 	security_idx = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
 	if (status->flag & RX_FLAG_8023)
 		goto skip_hdr_check;
@@ -1450,6 +1485,10 @@ void mt76_check_ccmp_pn(struct sk_buff *skb)
 			return;
 	}
 
+	/* group state has no counter for Management frames */
+	if (ieee80211_is_mgmt(hdr->frame_control))
+		group = false;
+
 	/* IEEE 802.11-2020, 12.5.3.4.4 "PN and replay detection" c):
 	 *
 	 * the recipient shall maintain a single replay counter for received
@@ -1461,15 +1500,25 @@ void mt76_check_ccmp_pn(struct sk_buff *skb)
 		security_idx = IEEE80211_NUM_TIDS;
 
 skip_hdr_check:
+	/* the links of an MLD peer share one PN space per pairwise key */
+	if (group)
+		rx_pn = mt76_rx_group_pn(status, security_idx);
+	else if (wcid->rx_check_pn)
+		rx_pn = mt76_wcid_primary(wcid)->rx_key_pn[security_idx];
+	else
+		return;
+	if (!rx_pn)
+		return;
+
 	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
-	ret = memcmp(status->iv, pn_wcid->rx_key_pn[security_idx],
-		     sizeof(status->iv));
+	BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_group_pn->pn[0][0]));
+	ret = memcmp(status->iv, rx_pn, sizeof(status->iv));
 	if (ret <= 0) {
 		status->flag |= RX_FLAG_ONLY_MONITOR;
 		return;
 	}
 
-	memcpy(pn_wcid->rx_key_pn[security_idx], status->iv, sizeof(status->iv));
+	memcpy(rx_pn, status->iv, sizeof(status->iv));
 
 	if (status->flag & RX_FLAG_IV_STRIPPED)
 		status->flag |= RX_FLAG_PN_VALIDATED;
drivers/net/wireless/mediatek/mt76/mt76.h +25 -1
index 688039f2d967..e2156d7c1108 100644
@@ -383,6 +383,11 @@ DECLARE_EWMA(signal, 10, 8);
 #define MT_WCID_TX_INFO_TXPWR_ADJ	GENMASK(25, 18)
 #define MT_WCID_TX_INFO_SET		BIT(31)
 
+struct mt76_rx_group_pn {
+	u8 key_mask;
+	u8 pn[4][IEEE80211_NUM_TIDS][6];
+};
+
 struct mt76_wcid {
 	struct mt76_rx_tid __rcu *aggr[IEEE80211_NUM_TIDS];
 
@@ -410,6 +415,7 @@ struct mt76_wcid {
 
 	u8 rx_check_pn;
 	u8 rx_key_pn[IEEE80211_NUM_TIDS + 1][6];
+	struct mt76_rx_group_pn *rx_group_pn;
 	u16 cipher;
 
 	u32 tx_info;
@@ -782,7 +788,8 @@ struct mt76_rx_status {
 		} eht;
 	};
 
-	u8 amsdu:1, first_amsdu:1, last_amsdu:1, skip_pn_check:1;
+	u8 amsdu:1, first_amsdu:1, last_amsdu:1, skip_pn_check:1, group:1,
+	   key_id:2;
 	u8 rate_idx;
 	u8 nss:5, band:3;
 	u8 chains;
@@ -860,6 +867,7 @@ struct mt76_vif_link {
 	struct ieee80211_chanctx_conf *ctx;
 	struct mt76_wcid *wcid;
 	struct mt76_vif_data *mvif;
+	struct mt76_rx_group_pn rx_group_pn;
 	struct rcu_head rcu_head;
 };
 
@@ -1655,6 +1663,22 @@ mt76_wcid_key_link_setup(struct mt76_wcid *wcid,
 	wcid->rx_check_pn = key && mt76_key_has_rx_pn(key);
 }
 
+static inline void
+mt76_wcid_rx_group_pn_set(struct mt76_wcid *wcid, struct ieee80211_vif *vif,
+			  struct mt76_rx_group_pn *group_pn)
+{
+	wcid->rx_group_pn = vif->type == NL80211_IFTYPE_STATION ? group_pn : NULL;
+}
+
+static inline struct mt76_wcid *
+mt76_rx_group_wcid(struct mt76_wcid *wcid)
+{
+	if (!wcid || wcid->sta || !wcid->rx_group_pn)
+		return NULL;
+
+	return wcid;
+}
+
 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
 			 struct ieee80211_key_conf *key);
 
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

PATCH 7/16 wifi: mt76: mt7996: check the RX PN of group addressed frames

In reply to Felix Fietkau

The RX descriptor of a decrypted group addressed frame names the vif link wcid, which has no station, so mt7996_rx_get_wcid() gives the frame no wcid. With the IV stripped, the frame gets no replay check.

Give such a frame the vif link wcid and check it against the counters of its key id. Seed the counters only when mac80211 sets the group key, not when the driver installs the key again at a link add, since mac80211 does not advance the RSC of a frame with a stripped IV.

Fixes 98686cd21624 ("wifi: mt76: mt7996: add driver for MediaTek Wi-Fi 7 (802.11be) devices")
---
 drivers/net/wireless/mediatek/mt76/mt7996/mac.c  | 5 +++++
 drivers/net/wireless/mediatek/mt76/mt7996/main.c | 7 +++++++
 2 files changed, 12 insertions(+)
drivers/net/wireless/mediatek/mt76/mt7996/mac.c +5 -0
index 62e16e3c989b..ec4c8118d679 100644
@@ -478,6 +478,8 @@ mt7996_mac_fill_rx(struct mt7996_dev *dev, enum mt76_rxq_id q,
 		status->flag |= RX_FLAG_ONLY_MONITOR;
 
 	unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
 	status->wcid = mt7996_rx_get_wcid(dev, idx, band_idx);
 
@@ -519,6 +521,9 @@ mt7996_mac_fill_rx(struct mt7996_dev *dev, enum mt76_rxq_id q,
 		status->flag |= RX_FLAG_MMIC_STRIPPED | RX_FLAG_MIC_STRIPPED;
 	}
 
+	if (!unicast && !status->wcid && (status->flag & RX_FLAG_DECRYPTED))
+		status->wcid = mt76_rx_group_wcid(mt76_wcid_ptr(dev, idx));
+
 	remove_pad = FIELD_GET(MT_RXD2_NORMAL_HDR_OFFSET, rxd2);
 
 	if (rxd2 & MT_RXD2_NORMAL_MAX_LEN_ERROR)
drivers/net/wireless/mediatek/mt76/mt7996/main.c +7 -0
index bcecd6558fda..7e5bf40c257d 100644
@@ -469,6 +469,7 @@ int mt7996_vif_link_add(struct mt76_phy *mphy, struct ieee80211_vif *vif,
 	msta_link->wcid.link_valid = ieee80211_vif_is_mld(vif);
 	msta_link->wcid.tx_info |= MT_WCID_TX_INFO_SET;
 	mt76_wcid_init(&msta_link->wcid, band_idx);
+	mt76_wcid_rx_group_pn_set(&msta_link->wcid, vif, &mlink->rx_group_pn);
 
 	if (vif->type != NL80211_IFTYPE_AP &&
 	    (!mlink->omac_idx || mlink->omac_idx > 3))
@@ -900,6 +901,12 @@ static int mt7996_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 	}
 
 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
+		struct mt7996_vif_link *link = mt7996_vif_link(dev, vif, link_id);
+
+		/* a replay of the key at a link add must keep the counters */
+		if (!sta && link && cmd == SET_KEY)
+			mt76_wcid_key_setup(&dev->mt76, &link->msta_link.wcid, key);
+
 		err = mt7996_set_hw_key(hw, cmd, vif, sta, link_id, key);
 		if (err)
 			break;
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 8/16 wifi: mt76: mt7615: check the RX PN of group addressed frames

In reply to Felix Fietkau

For a group addressed frame whose WLAN_IDX names a peer, mt7615_rx_get_wcid() returns the vif wcid, whose per-TID counters all group keys of the vif share. After a GTK rekey, frames of the new key thus read as replays until their PN passes that of the old key. A frame whose WLAN_IDX names the vif wcid gets no wcid and no replay check.

Check a group addressed frame against the counters of its key id, also where WLAN_IDX names the vif wcid.

Fixes 04b8e65922f6 ("mt76: add mac80211 driver for MT7615 PCIe-based chipsets")
---
 drivers/net/wireless/mediatek/mt76/mt7615/mac.c  | 4 +++-
 drivers/net/wireless/mediatek/mt76/mt7615/main.c | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7615/mac.c +3 -1
index 0def4ba75817..492d1e1b6f0f 100644
@@ -95,7 +95,7 @@ static struct mt76_wcid *mt7615_rx_get_wcid(struct mt7615_dev *dev,
 		return wcid;
 
 	if (!wcid->sta)
-		return NULL;
+		return mt76_rx_group_wcid(wcid);
 
 	sta = container_of(wcid, struct mt7615_sta, wcid);
 	if (!sta->vif)
@@ -378,6 +378,8 @@ static int mt7615_mac_fill_rx(struct mt7615_dev *dev, struct sk_buff *skb)
 		status->flag |= RX_FLAG_ONLY_MONITOR;
 
 	unicast = (rxd1 & MT_RXD1_NORMAL_ADDR_TYPE) == MT_RXD1_NORMAL_U2M;
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD2_NORMAL_WLAN_IDX, rxd2);
 	status->wcid = mt7615_rx_get_wcid(dev, idx, unicast);
 
drivers/net/wireless/mediatek/mt76/mt7615/main.c +2 -0
index 175e847f49ef..7af53894edd1 100644
@@ -226,6 +226,8 @@ static int mt7615_add_interface(struct ieee80211_hw *hw,
 	INIT_LIST_HEAD(&mvif->sta.wcid.poll_list);
 	mvif->sta.wcid.idx = idx;
 	mt76_wcid_init(&mvif->sta.wcid, mvif->mt76.band_idx);
+	mt76_wcid_rx_group_pn_set(&mvif->sta.wcid, vif,
+				  &mvif->mt76.rx_group_pn);
 
 	mt7615_mac_wtbl_update(dev, idx,
 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
Signature · 1 line
2.47.3
author maintainer
#8 lore raw

PATCH 9/16 wifi: mt76: mt7615: reset the group RX PN counters at WoWLAN resume

In reply to Felix Fietkau

With GTK rekey offload, the firmware can renew the GTK during WoWLAN suspend without reporting it. If the new key reuses a key id whose counters hold the PN of an earlier key, its frames read as replays after resume.

At resume, zero the group counters of an interface that gave rekey data to the firmware. Where the link held a group key with a PN check, also check key ids 1 to 3, since the new key can use any of them. Forget the key ids at the end of an association, so that a reassociation without such a key arms no check. After resume, a replay with a nonzero PN passes until the first genuine frame of its key id and TID.

Fixes b47e21e75c80 ("mt76: mt7615: add gtk rekey offload support")
---
 drivers/net/wireless/mediatek/mt76/mt76.h     | 10 +++++++
 .../net/wireless/mediatek/mt76/mt7615/main.c  | 28 +++++++++++++++----
 .../wireless/mediatek/mt76/mt7615/mt7615.h    |  1 +
 3 files changed, 34 insertions(+), 5 deletions(-)
drivers/net/wireless/mediatek/mt76/mt76.h +10 -0
index e2156d7c1108..d635df2cece2 100644
@@ -1679,6 +1679,16 @@ mt76_rx_group_wcid(struct mt76_wcid *wcid)
 	return wcid;
 }
 
+static inline void
+mt76_rx_group_pn_reset(struct mt76_rx_group_pn *group_pn)
+{
+	memset(group_pn->pn, 0, sizeof(group_pn->pn));
+
+	/* the firmware can renew the GTK in suspend under another key id */
+	if (group_pn->key_mask)
+		group_pn->key_mask |= GENMASK(3, 1);
+}
+
 void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
 			 struct ieee80211_key_conf *key);
 
drivers/net/wireless/mediatek/mt76/mt7615/main.c +23 -5
index 7af53894edd1..425640b575d2 100644
@@ -573,6 +573,7 @@ static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
 				    struct ieee80211_bss_conf *info,
 				    u64 changed)
 {
+	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
 
@@ -607,15 +608,17 @@ static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
 		mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
 
 	if ((changed & BSS_CHANGED_ARP_FILTER) &&
-	    mt7615_firmware_offload(dev)) {
-		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
-
+	    mt7615_firmware_offload(dev))
 		mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
 						  info);
-	}
 
-	if (changed & BSS_CHANGED_ASSOC)
+	if (changed & BSS_CHANGED_ASSOC) {
 		mt7615_mac_set_beacon_filter(phy, vif, vif->cfg.assoc);
+		if (!vif->cfg.assoc) {
+			mvif->gtk_rekey_offload = false;
+			mvif->mt76.rx_group_pn.key_mask = 0;
+		}
+	}
 
 	if (changed & BSS_CHANGED_MU_GROUPS)
 		 mt7615_update_mu_group(hw, vif, info);
@@ -1264,6 +1267,15 @@ static int mt7615_suspend(struct ieee80211_hw *hw,
 	return err;
 }
 
+static void
+mt7615_rx_group_pn_reset_iter(void *priv, u8 *mac, struct ieee80211_vif *vif)
+{
+	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
+
+	if (mvif->gtk_rekey_offload)
+		mt76_rx_group_pn_reset(&mvif->mt76.rx_group_pn);
+}
+
 static int mt7615_resume(struct ieee80211_hw *hw)
 {
 	struct mt7615_phy *phy = mt7615_hw_phy(hw);
@@ -1287,6 +1299,10 @@ static int mt7615_resume(struct ieee80211_hw *hw)
 	}
 
 	clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
+	ieee80211_iterate_active_interfaces(hw,
+					    IEEE80211_IFACE_ITER_RESUME_ALL,
+					    mt7615_rx_group_pn_reset_iter,
+					    NULL);
 	ieee80211_iterate_active_interfaces(hw,
 					    IEEE80211_IFACE_ITER_RESUME_ALL,
 					    mt76_connac_mcu_set_suspend_iter,
@@ -1312,10 +1328,12 @@ static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
 				  struct ieee80211_vif *vif,
 				  struct cfg80211_gtk_rekey_data *data)
 {
+	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
 	struct mt7615_dev *dev = mt7615_hw_dev(hw);
 
 	mt7615_mutex_acquire(dev);
 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
+	mvif->gtk_rekey_offload = true;
 	mt7615_mutex_release(dev);
 }
 #endif /* CONFIG_PM */
drivers/net/wireless/mediatek/mt76/mt7615/mt7615.h +1 -0
index 391928405f32..121a4fb690ef 100644
@@ -143,6 +143,7 @@ struct mt7615_vif {
 	struct mt76_vif_link mt76; /* must be first */
 	struct mt7615_sta sta;
 	bool sta_added;
+	bool gtk_rekey_offload;
 };
 
 struct mib_stats {
Signature · 1 line
2.47.3
author maintainer
#9 lore raw

PATCH 10/16 wifi: mt76: mt7915: check the RX PN of group addressed frames

In reply to Felix Fietkau

For a group addressed frame whose WLAN_IDX names a peer, mt7915_rx_get_wcid() returns the vif wcid, whose per-TID counters all group keys of the vif share. After a GTK rekey, frames of the new key thus read as replays until their PN passes that of the old key. A frame whose WLAN_IDX names the vif wcid gets no wcid and no replay check.

Check a group addressed frame against the counters of its key id, also where WLAN_IDX names the vif wcid.

Fixes e57b7901469f ("mt76: add mac80211 driver for MT7915 PCIe-based chipsets")
---
 drivers/net/wireless/mediatek/mt76/mt7915/mac.c  | 4 +++-
 drivers/net/wireless/mediatek/mt76/mt7915/main.c | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7915/mac.c +3 -1
index e458661d750b..0505d55f1c0a 100644
@@ -61,7 +61,7 @@ static struct mt76_wcid *mt7915_rx_get_wcid(struct mt7915_dev *dev,
 		return wcid;
 
 	if (!wcid->sta)
-		return NULL;
+		return mt76_rx_group_wcid(wcid);
 
 	sta = container_of(wcid, struct mt7915_sta, wcid);
 	if (!sta->vif)
@@ -313,6 +313,8 @@ mt7915_mac_fill_rx(struct mt7915_dev *dev, struct sk_buff *skb,
 		status->flag |= RX_FLAG_ONLY_MONITOR;
 
 	unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
 	status->wcid = mt7915_rx_get_wcid(dev, idx, unicast);
 
drivers/net/wireless/mediatek/mt76/mt7915/main.c +2 -0
index 50e67a209693..e604b82635e2 100644
@@ -258,6 +258,8 @@ static int mt7915_add_interface(struct ieee80211_hw *hw,
 	mvif->sta.wcid.idx = idx;
 	mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
 	mt76_wcid_init(&mvif->sta.wcid, phy->mt76->band_idx);
+	mt76_wcid_rx_group_pn_set(&mvif->sta.wcid, vif,
+				  &mvif->mt76.rx_group_pn);
 
 	mt7915_mac_wtbl_update(dev, idx,
 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
Signature · 1 line
2.47.3
author maintainer
#10 lore raw

PATCH 11/16 wifi: mt76: mt792x: check the RX PN of group addressed frames

In reply to Felix Fietkau

For a group addressed frame whose WLAN_IDX names a peer, mt792x_rx_get_wcid() returns the vif wcid, whose per-TID counters all group keys of the vif share. After a GTK rekey, frames of the new key thus read as replays until their PN passes that of the old key. On mt7925 this wcid also serves every link of an MLD station, each with a GTK of its own. A frame whose WLAN_IDX names the vif wcid gets no wcid and no replay check.

Check a group addressed frame against the counters of its key id, on the vif wcid of the link that holds the key.

Also point mvif->sta.vif at the vif in mt7921_add_interface(), as mt7925_add_interface() does, since the link lookup reads it. The fragment path of mt7921_mac_fill_rx() then needs a station behind the wcid, as mt7925_reverse_frag0_hdr_trans() does.

Fixes 163f4d22c118 ("mt76: mt7921: add MAC support")
Fixes c948b5da6bbe ("wifi: mt76: mt7925: add Mediatek Wi-Fi7 driver for mt7925 chips")
Fixes 86c051f2c418 ("wifi: mt76: mt7925: enabling MLO when the firmware supports it")
---
 drivers/net/wireless/mediatek/mt76/mt7921/mac.c  | 4 +++-
 drivers/net/wireless/mediatek/mt76/mt7921/main.c | 3 +++
 drivers/net/wireless/mediatek/mt76/mt7925/mac.c  | 2 ++
 drivers/net/wireless/mediatek/mt76/mt7925/main.c | 1 +
 drivers/net/wireless/mediatek/mt76/mt792x_mac.c  | 8 ++++++--
 5 files changed, 15 insertions(+), 3 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7921/mac.c +3 -1
index a2d577872566..a843b0a46a99 100644
@@ -212,6 +212,8 @@ mt7921_mac_fill_rx(struct mt792x_dev *dev, struct sk_buff *skb)
 
 	chfreq = FIELD_GET(MT_RXD3_NORMAL_CH_FREQ, rxd3);
 	unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
 	status->wcid = mt792x_rx_get_wcid(dev, idx, unicast);
 
@@ -385,7 +387,7 @@ mt7921_mac_fill_rx(struct mt792x_dev *dev, struct sk_buff *skb)
 		struct ieee80211_vif *vif;
 		int err;
 
-		if (!msta || !msta->vif)
+		if (!msta || !msta->vif || !status->wcid->sta)
 			return -EINVAL;
 
 		vif = container_of((void *)msta->vif, struct ieee80211_vif,
drivers/net/wireless/mediatek/mt76/mt7921/main.c +3 -0
index 9b13e9c25cf3..eaf8e5098b0c 100644
@@ -317,6 +317,7 @@ mt7921_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 	mvif->bss_conf.mt76.omac_idx = mvif->bss_conf.mt76.idx;
 	mvif->phy = phy;
 	mvif->bss_conf.vif = mvif;
+	mvif->sta.vif = mvif;
 	mvif->bss_conf.mt76.band_idx = 0;
 	mvif->bss_conf.mt76.wmm_idx = mvif->bss_conf.mt76.idx % MT76_CONNAC_MAX_WMM_SETS;
 
@@ -335,6 +336,8 @@ mt7921_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 	mvif->sta.deflink.wcid.idx = idx;
 	mvif->sta.deflink.wcid.tx_info |= MT_WCID_TX_INFO_SET;
 	mt76_wcid_init(&mvif->sta.deflink.wcid, mvif->bss_conf.mt76.band_idx);
+	mt76_wcid_rx_group_pn_set(&mvif->sta.deflink.wcid, vif,
+				  &mvif->bss_conf.mt76.rx_group_pn);
 
 	mt7921_mac_wtbl_update(dev, idx,
 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
drivers/net/wireless/mediatek/mt76/mt7925/mac.c +2 -0
index 3389a3236187..1e8498956cc7 100644
@@ -444,6 +444,8 @@ mt7925_mac_fill_rx(struct mt792x_dev *dev, struct sk_buff *skb)
 
 	chfreq = FIELD_GET(MT_RXD3_NORMAL_CH_FREQ, rxd3);
 	unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
 	status->wcid = mt792x_rx_get_wcid(dev, idx, unicast);
 
drivers/net/wireless/mediatek/mt76/mt7925/main.c +1 -0
index c26d9982650f..3e614af35141 100644
@@ -449,6 +449,7 @@ static int mt7925_mac_link_bss_add(struct mt792x_dev *dev,
 	mlink->wcid.idx = idx;
 	mlink->wcid.tx_info |= MT_WCID_TX_INFO_SET;
 	mt76_wcid_init(&mlink->wcid, 0);
+	mt76_wcid_rx_group_pn_set(&mlink->wcid, vif, &mconf->mt76.rx_group_pn);
 
 	mt7925_mac_wtbl_update(dev, idx,
 			       MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
drivers/net/wireless/mediatek/mt76/mt792x_mac.c +6 -2
index ebf4c32924c4..d4ea2142631c 100644
@@ -147,14 +147,18 @@ struct mt76_wcid *mt792x_rx_get_wcid(struct mt792x_dev *dev, u16 idx,
 		return wcid;
 
 	if (!wcid->sta)
-		return NULL;
+		return mt76_rx_group_wcid(wcid);
 
 	link = container_of(wcid, struct mt792x_link_sta, wcid);
 	sta = link->sta;
 	if (!sta->vif)
 		return NULL;
 
-	return &sta->vif->sta.deflink.wcid;
+	link = mt792x_sta_to_link_rcu(&sta->vif->sta, wcid->link_id);
+	if (!link)
+		link = &sta->vif->sta.deflink;
+
+	return &link->wcid;
 }
 EXPORT_SYMBOL_GPL(mt792x_rx_get_wcid);
 
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

PATCH 12/16 wifi: mt76: mt792x: reset the group RX PN counters at WoWLAN resume

In reply to Felix Fietkau

With GTK rekey offload, the firmware can renew the GTK during WoWLAN suspend without reporting it. If the new key reuses a key id whose counters hold the PN of an earlier key, its frames read as replays after resume.

At resume, zero the group counters of each link of an interface that gave rekey data to the firmware. Where a link held a group key with a PN check, also check key ids 1 to 3, since the new key can use any of them. Forget the key ids at the end of an association, so that a reassociation without such a key arms no check. After resume, a replay with a nonzero PN passes until the first genuine frame of its key id and TID.

Pass no key at a key removal in mt7921_set_key(), as mt7925_set_link_key() does, since the keys of an association go after its end and would mark their key ids again.

Fixes ffa1bf97425b ("mt76: mt7921: introduce PM support")
Fixes c948b5da6bbe ("wifi: mt76: mt7925: add Mediatek Wi-Fi7 driver for mt7925 chips")
---
 .../net/wireless/mediatek/mt76/mt7921/main.c  | 10 +++++-
 .../net/wireless/mediatek/mt76/mt7925/main.c  |  8 +++++
 drivers/net/wireless/mediatek/mt76/mt792x.h   |  4 +++
 .../net/wireless/mediatek/mt76/mt792x_core.c  | 36 +++++++++++++++++++
 4 files changed, 57 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7921/main.c +9 -1
index eaf8e5098b0c..23f92ce01080 100644
@@ -562,7 +562,7 @@ static int mt7921_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 			goto out;
 	}
 
-	mt76_wcid_key_setup(&dev->mt76, wcid, key);
+	mt76_wcid_key_setup(&dev->mt76, wcid, cmd == SET_KEY ? key : NULL);
 	err = mt76_connac_mcu_add_key(&dev->mt76, vif, &msta->deflink.bip,
 				      key, MCU_UNI_CMD(STA_REC_UPDATE),
 				      &msta->deflink.wcid, cmd);
@@ -731,6 +731,8 @@ static void mt7921_bss_info_changed(struct ieee80211_hw *hw,
 		mt7921_mcu_sta_update(dev, NULL, vif, true,
 				      MT76_STA_INFO_STATE_ASSOC);
 		mt7921_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
+		if (!vif->cfg.assoc)
+			mt792x_rx_group_pn_forget(vif);
 	}
 
 	if (changed & BSS_CHANGED_ARP_FILTER) {
@@ -1182,6 +1184,10 @@ static int mt7921_resume(struct ieee80211_hw *hw)
 	mt792x_mutex_acquire(dev);
 
 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
+	ieee80211_iterate_active_interfaces_mtx(hw,
+						IEEE80211_IFACE_ITER_RESUME_ALL,
+						mt792x_rx_group_pn_reset_iter,
+						NULL);
 	ieee80211_iterate_active_interfaces_mtx(hw,
 						IEEE80211_IFACE_ITER_RESUME_ALL,
 						mt76_connac_mcu_set_suspend_iter,
@@ -1199,10 +1205,12 @@ static void mt7921_set_rekey_data(struct ieee80211_hw *hw,
 				  struct ieee80211_vif *vif,
 				  struct cfg80211_gtk_rekey_data *data)
 {
+	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 
 	mt792x_mutex_acquire(dev);
 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
+	mvif->gtk_rekey_offload = true;
 	mt792x_mutex_release(dev);
 }
 #endif /* CONFIG_PM */
drivers/net/wireless/mediatek/mt76/mt7925/main.c +8 -0
index 3e614af35141..6b3f2dafae4d 100644
@@ -2027,6 +2027,10 @@ static int mt7925_resume(struct ieee80211_hw *hw)
 	mt792x_mutex_acquire(dev);
 
 	set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
+	ieee80211_iterate_active_interfaces_mtx(hw,
+						IEEE80211_IFACE_ITER_RESUME_ALL,
+						mt792x_rx_group_pn_reset_iter,
+						NULL);
 	ieee80211_iterate_active_interfaces_mtx(hw,
 						IEEE80211_IFACE_ITER_RESUME_ALL,
 						mt7925_mcu_set_suspend_iter,
@@ -2044,10 +2048,12 @@ static void mt7925_set_rekey_data(struct ieee80211_hw *hw,
 				  struct ieee80211_vif *vif,
 				  struct cfg80211_gtk_rekey_data *data)
 {
+	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
 	struct mt792x_dev *dev = mt792x_hw_dev(hw);
 
 	mt792x_mutex_acquire(dev);
 	mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
+	mvif->gtk_rekey_offload = true;
 	mt792x_mutex_release(dev);
 }
 #endif /* CONFIG_PM */
@@ -2468,6 +2474,8 @@ static void mt7925_vif_cfg_changed(struct ieee80211_hw *hw,
 				      &mvif->sta.deflink, true,
 				      MT76_STA_INFO_STATE_ASSOC, NULL);
 		mt7925_mcu_set_beacon_filter(dev, vif, vif->cfg.assoc);
+		if (!vif->cfg.assoc)
+			mt792x_rx_group_pn_forget(vif);
 
 		if (ieee80211_vif_is_mld(vif))
 			mvif->mlo_pm_state = MT792x_MLO_LINK_ASSOC;
drivers/net/wireless/mediatek/mt76/mt792x.h +4 -0
index 260abdc978f8..fa64dd2ae28a 100644
@@ -250,6 +250,7 @@ struct mt792x_vif {
 
 	struct mt792x_phy *phy;
 	bool roc_join_held; /* join ROC kept until the STA is authorized */
+	bool gtk_rekey_offload;
 	u16 valid_links;
 	u8 deflink_id;
 	enum mt792x_mlo_pm_state mlo_pm_state;
@@ -622,6 +623,9 @@ void mt792x_unassign_vif_chanctx(struct ieee80211_hw *hw,
 				 struct ieee80211_bss_conf *link_conf,
 				 struct ieee80211_chanctx_conf *ctx);
 void mt792x_set_wakeup(struct ieee80211_hw *hw, bool enabled);
+void mt792x_rx_group_pn_reset_iter(void *priv, u8 *mac,
+				   struct ieee80211_vif *vif);
+void mt792x_rx_group_pn_forget(struct ieee80211_vif *vif);
 void mt792x_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 			   u32 sset, u8 *data);
 int mt792x_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
drivers/net/wireless/mediatek/mt76/mt792x_core.c +36 -0
index 7dddce02d600..99bcd90f8e1a 100644
@@ -559,6 +559,42 @@ void mt792x_set_wakeup(struct ieee80211_hw *hw, bool enabled)
 }
 EXPORT_SYMBOL_GPL(mt792x_set_wakeup);
 
+void mt792x_rx_group_pn_reset_iter(void *priv, u8 *mac,
+				   struct ieee80211_vif *vif)
+{
+	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
+	unsigned long valid = ieee80211_vif_is_mld(vif) ?
+			      mvif->valid_links : BIT(0);
+	unsigned int link_id;
+
+	if (!mvif->gtk_rekey_offload)
+		return;
+
+	for_each_set_bit(link_id, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
+		struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, link_id);
+
+		mt76_rx_group_pn_reset(&mconf->mt76.rx_group_pn);
+	}
+}
+EXPORT_SYMBOL_GPL(mt792x_rx_group_pn_reset_iter);
+
+void mt792x_rx_group_pn_forget(struct ieee80211_vif *vif)
+{
+	struct mt792x_vif *mvif = (struct mt792x_vif *)vif->drv_priv;
+	unsigned long valid = ieee80211_vif_is_mld(vif) ?
+			      mvif->valid_links : BIT(0);
+	unsigned int link_id;
+
+	mvif->gtk_rekey_offload = false;
+	mvif->bss_conf.mt76.rx_group_pn.key_mask = 0;
+	for_each_set_bit(link_id, &valid, IEEE80211_MLD_MAX_NUM_LINKS) {
+		struct mt792x_bss_conf *mconf = mt792x_vif_to_link(mvif, link_id);
+
+		mconf->mt76.rx_group_pn.key_mask = 0;
+	}
+}
+EXPORT_SYMBOL_GPL(mt792x_rx_group_pn_forget);
+
 static const char mt792x_gstrings_stats[][ETH_GSTRING_LEN] = {
 	/* tx counters */
 	"tx_ampdu_cnt",
Signature · 1 line
2.47.3
author maintainer
#12 lore raw

PATCH 13/16 wifi: mt76: mt7603: check the RX PN of group addressed frames

In reply to Felix Fietkau

For a group addressed frame whose WLAN_IDX names a peer, mt7603_rx_get_wcid() returns the vif wcid, whose per-TID counters all group keys of the vif share. After a GTK rekey, frames of the new key thus read as replays until their PN passes that of the old key. A frame whose WLAN_IDX names the vif wcid gets no wcid and no replay check.

Check a group addressed frame against the counters of its key id, also where WLAN_IDX names the vif wcid.

Fixes c8846e101502 ("mt76: add driver for MT7603E and MT7628/7688")
---
 drivers/net/wireless/mediatek/mt76/mt7603/mac.c    | 4 +++-
 drivers/net/wireless/mediatek/mt76/mt7603/main.c   | 1 +
 drivers/net/wireless/mediatek/mt76/mt7603/mt7603.h | 1 +
 3 files changed, 5 insertions(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt7603/mac.c +3 -1
index 836ce7dfb551..5940306ed3ce 100644
@@ -519,7 +519,7 @@ mt7603_rx_get_wcid(struct mt7603_dev *dev, u8 idx, bool unicast)
 		return wcid;
 
 	if (!wcid->sta)
-		return NULL;
+		return mt76_rx_group_wcid(wcid);
 
 	sta = container_of(wcid, struct mt7603_sta, wcid);
 	if (!sta->vif)
@@ -550,6 +550,8 @@ mt7603_mac_fill_rx(struct mt7603_dev *dev, struct sk_buff *skb)
 	sband = (i & 1) ? &dev->mphy.sband_5g.sband : &dev->mphy.sband_2g.sband;
 	i >>= 1;
 
+	status->group = !unicast;
+	status->key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
 	idx = FIELD_GET(MT_RXD2_NORMAL_WLAN_IDX, rxd2);
 	status->wcid = mt7603_rx_get_wcid(dev, idx, unicast);
 
drivers/net/wireless/mediatek/mt76/mt7603/main.c +1 -0
index 61f246193987..c8f77ace0eb1 100644
@@ -69,6 +69,7 @@ mt7603_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 	mvif->sta.wcid.idx = idx;
 	mvif->sta.vif = mvif;
 	mt76_wcid_init(&mvif->sta.wcid, 0);
+	mt76_wcid_rx_group_pn_set(&mvif->sta.wcid, vif, &mvif->rx_group_pn);
 
 	eth_broadcast_addr(bc_addr);
 	mt7603_wtbl_init(dev, idx, mvif->idx, bc_addr);
drivers/net/wireless/mediatek/mt76/mt7603/mt7603.h +1 -0
index 47f5be44292d..6c89e087d57d 100644
@@ -86,6 +86,7 @@ struct mt7603_sta {
 struct mt7603_vif {
 	struct mt7603_sta sta; /* must be first */
 
+	struct mt76_rx_group_pn rx_group_pn;
 	u8 idx;
 };
 
Signature · 1 line
2.47.3
author maintainer
#13 lore raw

PATCH 14/16 wifi: mt76: mt76x02: find no station for the group wcid of a vif

In reply to Felix Fietkau

dev->wcid[] holds the group wcid of each vif at MT_VIF_WCID(). mt76x02_rx_get_sta() refused such an index until commit dc66a129adf1 ("wifi: mt76: add a wrapper for wcid access with validation") replaced its bound with the size of the table. It now takes the group wcid as a struct mt76x02_sta. For a group addressed frame whose RXWI names that wcid, mt76x02_rx_get_sta_wcid() derives a pointer from the vif index that follows the wcid, and mt76_check_sta() dereferences it. Whether the hardware reports that index is not known.

Find no station for a wcid without one.

Fixes dc66a129adf1 ("wifi: mt76: add a wrapper for wcid access with validation")
---
 drivers/net/wireless/mediatek/mt76/mt76x02.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt76x02.h +1 -1
index f5a25608f8ff..da35daeb977a 100644
@@ -265,7 +265,7 @@ mt76x02_rx_get_sta(struct mt76_dev *dev, u8 idx)
 	struct mt76_wcid *wcid;
 
 	wcid = __mt76_wcid_ptr(dev, idx);
-	if (!wcid)
+	if (!wcid || !wcid->sta)
 		return NULL;
 
 	return container_of(wcid, struct mt76x02_sta, wcid);
Signature · 1 line
2.47.3
author maintainer
#14 lore raw

PATCH 15/16 wifi: mt76: mt7915: stop resets before the hw is unregistered

In reply to Felix Fietkau

recovery.hw_init_done stays set while mt7915_unregister_device() and the late error path of mt7915_register_device() unregister the hw. The MCU timeout path, the interrupt tasklet and debugfs can thus queue reset_work or dump_work after the cancel, for example on an MCU timeout in ieee80211_unregister_hw(). The work then runs from destroy_workqueue() in mt76_free_device() and calls ieee80211_restart_hw() on the unregistered hw.

Clear the flag before the hw is unregistered, as mt7925e_unregister_device() does. Test the flag and queue the work under dev->mt76.lock, and set and clear it under the same lock, so that the cancels after the clear catch every queued work.

Also make the recovery flags plain bools, since the reset work writes them without a lock and a bitfield write can undo another. In the error path, also cancel init_work, as mt7996_register_device() does, and wait for a reset or a dump queued before the error, since mt7915_stop_hardware() cancels neither.

Fixes 8a55712d124f ("wifi: mt76: mt7915: enable full system reset support")
Fixes 10f73bb3938f ("wifi: mt76: mt7915: reset the device after MCU timeout")
---
 drivers/net/wireless/mediatek/mt76/mt7915/init.c   | 14 +++++++++++++-
 drivers/net/wireless/mediatek/mt76/mt7915/mac.c    | 10 +++++++---
 drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h |  6 +++---
 3 files changed, 23 insertions(+), 7 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7915/init.c +13 -1
index fb92a0b2e4ab..4b3bf2648d3d 100644
@@ -1273,6 +1273,13 @@ static void mt7915_stop_hardware(struct mt7915_dev *dev)
 		mt7986_wmac_disable(dev);
 }
 
+static void mt7915_set_hw_init_done(struct mt7915_dev *dev, bool done)
+{
+	spin_lock_bh(&dev->mt76.lock);
+	dev->recovery.hw_init_done = done;
+	spin_unlock_bh(&dev->mt76.lock);
+}
+
 int mt7915_register_device(struct mt7915_dev *dev)
 {
 	struct mt7915_phy *phy2;
@@ -1324,7 +1331,7 @@ int mt7915_register_device(struct mt7915_dev *dev)
 
 	ieee80211_queue_work(mt76_hw(dev), &dev->init_work);
 
-	dev->recovery.hw_init_done = true;
+	mt7915_set_hw_init_done(dev, true);
 
 	ret = mt7915_init_debugfs(&dev->phy);
 	if (ret)
@@ -1337,6 +1344,10 @@ int mt7915_register_device(struct mt7915_dev *dev)
 	return 0;
 
 unreg_ext_phy:
+	mt7915_set_hw_init_done(dev, false);
+	cancel_work_sync(&dev->init_work);
+	cancel_work_sync(&dev->dump_work);
+	cancel_work_sync(&dev->reset_work);
 	if (phy2) {
 		mt7915_unregister_ext_phy(dev);
 		phy2 = NULL;
@@ -1355,6 +1366,7 @@ int mt7915_register_device(struct mt7915_dev *dev)
 
 void mt7915_unregister_device(struct mt7915_dev *dev)
 {
+	mt7915_set_hw_init_done(dev, false);
 	cancel_work_sync(&dev->dump_work);
 	cancel_work_sync(&dev->reset_work);
 	cancel_work_sync(&dev->rc_work);
drivers/net/wireless/mediatek/mt76/mt7915/mac.c +7 -3
index 0505d55f1c0a..dc23b8fcd216 100644
@@ -1765,11 +1765,13 @@ void mt7915_mac_dump_work(struct work_struct *work)
 
 void mt7915_reset(struct mt7915_dev *dev)
 {
+	spin_lock_bh(&dev->mt76.lock);
+
 	if (!dev->recovery.hw_init_done)
-		return;
+		goto out;
 
 	if (dev->recovery.hw_full_reset)
-		return;
+		goto out;
 
 	/* wm/wa exception: do full recovery */
 	if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WDT_MASK) {
@@ -1780,7 +1782,7 @@ void mt7915_reset(struct mt7915_dev *dev)
 
 		mt7915_irq_disable(dev, MT_INT_MCU_CMD);
 		queue_work(dev->mt76.wq, &dev->dump_work);
-		return;
+		goto out;
 	}
 
 	if ((READ_ONCE(dev->recovery.state) & MT_MCU_CMD_STOP_DMA)) {
@@ -1790,6 +1792,8 @@ void mt7915_reset(struct mt7915_dev *dev)
 
 	queue_work(dev->mt76.wq, &dev->reset_work);
 	wake_up(&dev->reset_wait);
+out:
+	spin_unlock_bh(&dev->mt76.lock);
 }
 
 void mt7915_mac_update_stats(struct mt7915_phy *phy)
drivers/net/wireless/mediatek/mt76/mt7915/mt7915.h +3 -3
index 42a59f9e3ba1..8ba11c457111 100644
@@ -279,9 +279,9 @@ struct mt7915_dev {
 		u32 state;
 		u32 wa_reset_count;
 		u32 wm_reset_count;
-		bool hw_full_reset:1;
-		bool hw_init_done:1;
-		bool restart:1;
+		bool hw_full_reset;
+		bool hw_init_done;
+		bool restart;
 	} recovery;
 
 	/* protects coredump data */
Signature · 1 line
2.47.3
author maintainer
#15 lore raw

PATCH 16/16 wifi: mt76: mt7996: stop resets before the hw is unregistered

In reply to Felix Fietkau

recovery.hw_init_done stays set while mt7996_unregister_device() unregisters the hw. The MCU timeout path, the interrupt tasklet and debugfs can thus queue reset_work or dump_work after the cancel, for example on an MCU timeout in ieee80211_unregister_hw(). The work then runs from destroy_workqueue() in mt76_free_device() and calls ieee80211_restart_hw() on the unregistered hw.

Clear the flag before the hw is unregistered, as mt7925e_unregister_device() does. Test the flag and queue the work under dev->mt76.lock, and set and clear it under the same lock, so that the cancels after the clear catch every queued work.

Also make the recovery flags plain bools, since the reset work writes them without a lock and a bitfield write can undo another.

Fixes 27015b6fbcca ("wifi: mt76: mt7996: enable full system reset support")
Fixes d2b860454ea2 ("wifi: mt76: mt7996: reset device after MCU message timeout")
---
 drivers/net/wireless/mediatek/mt76/mt7996/init.c   | 10 +++++++++-
 drivers/net/wireless/mediatek/mt76/mt7996/mac.c    | 10 +++++++---
 drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h |  6 +++---
 3 files changed, 19 insertions(+), 7 deletions(-)
drivers/net/wireless/mediatek/mt76/mt7996/init.c +9 -1
index 18eab0eaaaad..dd7755f3258b 100644
@@ -1743,6 +1743,13 @@ void mt7996_set_stream_he_eht_caps(struct mt7996_phy *phy)
 						NL80211_BAND_6GHZ);
 }
 
+static void mt7996_set_hw_init_done(struct mt7996_dev *dev, bool done)
+{
+	spin_lock_bh(&dev->mt76.lock);
+	dev->recovery.hw_init_done = done;
+	spin_unlock_bh(&dev->mt76.lock);
+}
+
 int mt7996_register_device(struct mt7996_dev *dev)
 {
 	struct ieee80211_hw *hw = mt76_hw(dev);
@@ -1792,7 +1799,7 @@ int mt7996_register_device(struct mt7996_dev *dev)
 
 	ieee80211_queue_work(mt76_hw(dev), &dev->init_work);
 
-	dev->recovery.hw_init_done = true;
+	mt7996_set_hw_init_done(dev, true);
 
 	ret = mt7996_init_debugfs(dev);
 	if (ret)
@@ -1814,6 +1821,7 @@ void mt7996_unregister_device(struct mt7996_dev *dev)
 {
 	int i;
 
+	mt7996_set_hw_init_done(dev, false);
 	cancel_work_sync(&dev->dump_work);
 	cancel_work_sync(&dev->wed_rro.work);
 	cancel_work_sync(&dev->reset_work);
drivers/net/wireless/mediatek/mt76/mt7996/mac.c +7 -3
index ec4c8118d679..1a9e2a2db7a5 100644
@@ -2889,11 +2889,13 @@ void mt7996_mac_dump_work(struct work_struct *work)
 
 void mt7996_reset(struct mt7996_dev *dev)
 {
+	spin_lock_bh(&dev->mt76.lock);
+
 	if (!dev->recovery.hw_init_done)
-		return;
+		goto out;
 
 	if (dev->recovery.hw_full_reset)
-		return;
+		goto out;
 
 	/* wm/wa exception: do full recovery */
 	if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_WDT_MASK) {
@@ -2904,7 +2906,7 @@ void mt7996_reset(struct mt7996_dev *dev)
 
 		mt7996_irq_disable(dev, MT_INT_MCU_CMD);
 		queue_work(dev->mt76.wq, &dev->dump_work);
-		return;
+		goto out;
 	}
 
 	if (READ_ONCE(dev->recovery.state) & MT_MCU_CMD_STOP_DMA) {
@@ -2914,6 +2916,8 @@ void mt7996_reset(struct mt7996_dev *dev)
 
 	queue_work(dev->mt76.wq, &dev->reset_work);
 	wake_up(&dev->reset_wait);
+out:
+	spin_unlock_bh(&dev->mt76.lock);
 }
 
 void mt7996_mac_update_stats(struct mt7996_phy *phy)
drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h +3 -3
index 1354bd82808c..86c718d6ec82 100644
@@ -468,9 +468,9 @@ struct mt7996_dev {
 		u32 state;
 		u32 wa_reset_count;
 		u32 wm_reset_count;
-		bool hw_full_reset:1;
-		bool hw_init_done:1;
-		bool restart:1;
+		bool hw_full_reset;
+		bool hw_init_done;
+		bool restart;
 	} recovery;
 
 	/* protects coredump data */
Signature · 1 line
2.47.3