[][MAC80211][WiFi6][mt76][Support spatial reuse debug commands]

[Description]
Add 3 new spatial reuse feature debug commands.
1. sr_enable: enable/disable spatial reuse
2. sr_scene_cond: check spatial resue scene detection algorithm result
3. sr_stats: check spatial resue tx statistics

[Release-log]
N/A

Change-Id: I85bd591340965ff1396c717cab253b125f5f80d1
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/8619413
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/1049-wifi-mt76-mt7915-support-spatial-reuse-debug-command.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/1049-wifi-mt76-mt7915-support-spatial-reuse-debug-command.patch
new file mode 100644
index 0000000..628f2c2
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/1049-wifi-mt76-mt7915-support-spatial-reuse-debug-command.patch
@@ -0,0 +1,331 @@
+From 771b77a2b49f7b4cfd4181cea48391e549a046b1 Mon Sep 17 00:00:00 2001
+From: Howard Hsu <howard-yh.hsu@mediatek.com>
+Date: Thu, 15 Feb 2024 11:16:16 +0800
+Subject: [PATCH] wifi: mt76: mt7915: support spatial reuse debug commands
+
+Support 3 spatial reuse debug commands:
+1. sr_enable: enable/disable spatial reuse
+2. sr_scene_cond: check spatial resue scene detection algorithm result
+3. sr_stats: check spatial resue tx statistics
+
+Signed-off-by: Howard Hsu <howard-yh.hsu@mediatek.com>
+---
+ mt76_connac_mcu.h    |  1 +
+ mt7915/mcu.c         | 94 ++++++++++++++++++++++++++++++++++++++++++++
+ mt7915/mcu.h         | 48 ++++++++++++++++++++++
+ mt7915/mt7915.h      |  3 ++
+ mt7915/mtk_debugfs.c | 48 ++++++++++++++++++++--
+ 5 files changed, 190 insertions(+), 4 deletions(-)
+
+diff --git a/mt76_connac_mcu.h b/mt76_connac_mcu.h
+index e581084..791dc1d 100644
+--- a/mt76_connac_mcu.h
++++ b/mt76_connac_mcu.h
+@@ -1024,6 +1024,7 @@ enum {
+ 	MCU_EXT_EVENT_WA_TX_STAT = 0x74,
+ 	MCU_EXT_EVENT_BCC_NOTIFY = 0x75,
+ 	MCU_EXT_EVENT_MURU_CTRL = 0x9f,
++	MCU_EXT_EVENT_SR = 0xa8,
+ 	MCU_EXT_EVENT_CSI_REPORT = 0xc2,
+ };
+ 
+diff --git a/mt7915/mcu.c b/mt7915/mcu.c
+index 3b214a2..4bd7a3b 100644
+--- a/mt7915/mcu.c
++++ b/mt7915/mcu.c
+@@ -452,6 +452,90 @@ mt7915_mcu_rx_bss_acq_pkt_cnt(struct mt7915_dev *dev, struct sk_buff * skb)
+ 	}
+ }
+ 
++#ifdef MTK_DEBUG
++void mt7915_mcu_rx_sr_swsd(struct mt7915_dev *dev, struct sk_buff *skb)
++{
++#define SR_SCENE_DETECTION_TIMER_PERIOD_MS 500
++	struct mt7915_mcu_sr_swsd_event *event;
++	static const char * const rules[] = {"NO CONNECTED", "NO CONGESTION",
++					     "NO INTERFERENCE", "SR ON"};
++	u8 idx;
++
++	event = (struct mt7915_mcu_sr_swsd_event *)skb->data;
++	idx = event->basic.band_idx;
++
++	dev_info(dev->mt76.dev, "Band index = %u\n", event->basic.band_idx);
++	dev_info(dev->mt76.dev, "Hit Rule = %s\n", rules[event->rule[idx]]);
++	dev_info(dev->mt76.dev, "Timer Period = %d(us)\n"
++		 "Congestion Ratio  = %d.%1d%%\n",
++		 SR_SCENE_DETECTION_TIMER_PERIOD_MS * 1000,
++		 le32_to_cpu(event->total_airtime_ratio[idx]) / 10,
++		 le32_to_cpu(event->total_airtime_ratio[idx]) % 10);
++	dev_info(dev->mt76.dev,
++		 "Total Airtime = %d(us)\n"
++		 "ChBusy = %d\n"
++		 "SrTx = %d\n"
++		 "OBSS = %d\n"
++		 "MyTx = %d\n"
++		 "MyRx = %d\n"
++		 "Interference Ratio = %d.%1d%%\n",
++		 le32_to_cpu(event->total_airtime[idx]),
++		 le32_to_cpu(event->channel_busy_time[idx]),
++		 le32_to_cpu(event->sr_tx_airtime[idx]),
++		 le32_to_cpu(event->obss_airtime[idx]),
++		 le32_to_cpu(event->my_tx_airtime[idx]),
++		 le32_to_cpu(event->my_rx_airtime[idx]),
++		 le32_to_cpu(event->obss_airtime_ratio[idx]) / 10,
++		 le32_to_cpu(event->obss_airtime_ratio[idx]) % 10);
++}
++
++void mt7915_mcu_rx_sr_hw_indicator(struct mt7915_dev *dev, struct sk_buff *skb)
++{
++	struct mt7915_mcu_sr_hw_ind_event *event;
++
++	event = (struct mt7915_mcu_sr_hw_ind_event *)skb->data;
++
++	dev_info(dev->mt76.dev, "Inter PPDU Count = %u\n",
++		 le16_to_cpu(event->inter_bss_ppdu_cnt));
++	dev_info(dev->mt76.dev, "SR Valid Count = %u\n",
++		 le16_to_cpu(event->non_srg_valid_cnt));
++	dev_info(dev->mt76.dev, "SR Tx Count = %u\n",
++		 le32_to_cpu(event->sr_ampdu_mpdu_cnt));
++	dev_info(dev->mt76.dev, "SR Tx Acked Count = %u\n",
++		 le32_to_cpu(event->sr_ampdu_mpdu_acked_cnt));
++}
++
++static void
++mt7915_mcu_rx_sr_event(struct mt7915_dev *dev, struct sk_buff *skb)
++{
++	struct mt7915_mcu_sr_basic_event *event;
++	struct mt7915_phy *phy;
++
++	event = (struct mt7915_mcu_sr_basic_event *)skb->data;
++
++	if (event->band_idx > MT_BAND1)
++		dev_info(dev->mt76.dev, "Invalid band idx %d\n", event->band_idx);
++
++	switch (event->sub_event_id) {
++	case SPR_EVENT_CFG_SR_ENABLE:
++		phy = dev->mt76.phys[event->band_idx]->priv;
++		if (phy)
++			phy->sr_enable = le32_to_cpu(event->value);
++		break;
++	case SPR_EVENT_SR_SW_SD:
++		mt7915_mcu_rx_sr_swsd(dev, skb);
++		break;
++	case SPR_EVENT_SR_HW_IND:
++		mt7915_mcu_rx_sr_hw_indicator(dev, skb);
++		break;
++	default:
++		dev_info(dev->mt76.dev, "Unknown SR event tag %d\n",
++			 event->sub_event_id);
++		break;
++	}
++}
++#endif
++
+ static void
+ mt7915_mcu_rx_ext_event(struct mt7915_dev *dev, struct sk_buff *skb)
+ {
+@@ -479,6 +563,11 @@ mt7915_mcu_rx_ext_event(struct mt7915_dev *dev, struct sk_buff *skb)
+ 	case MCU_EXT_EVENT_BCC_NOTIFY:
+ 		mt7915_mcu_rx_bcc_notify(dev, skb);
+ 		break;
++#ifdef MTK_DEBUG
++	case MCU_EXT_EVENT_SR:
++		mt7915_mcu_rx_sr_event(dev, skb);
++		break;
++#endif
+ #if defined CONFIG_NL80211_TESTMODE || defined MTK_DEBUG
+ 	case MCU_EXT_EVENT_BF_STATUS_READ:
+ 		mt7915_mcu_txbf_status_read(dev, skb);
+@@ -524,6 +613,7 @@ void mt7915_mcu_rx_event(struct mt7915_dev *dev, struct sk_buff *skb)
+ 	     rxd->ext_eid == MCU_EXT_EVENT_PS_SYNC ||
+ 	     rxd->ext_eid == MCU_EXT_EVENT_BCC_NOTIFY ||
+ 	     rxd->ext_eid == MCU_EXT_EVENT_BF_STATUS_READ ||
++	     rxd->ext_eid == MCU_EXT_EVENT_SR ||
+ 	     !rxd->seq) &&
+ 	     !(rxd->eid == MCU_CMD_EXT_CID &&
+ 	       rxd->ext_eid == MCU_EXT_EVENT_WA_TX_STAT))
+@@ -4002,6 +4092,10 @@ int mt7915_mcu_add_obss_spr(struct mt7915_phy *phy, struct ieee80211_vif *vif,
+ 	if (ret)
+ 		return ret;
+ 
++#ifdef MTK_DEBUG
++	phy->sr_enable = he_obss_pd->enable;
++#endif
++
+ 	if (sr_scene_detect || !he_obss_pd->enable)
+ 		return 0;
+ 
+diff --git a/mt7915/mcu.h b/mt7915/mcu.h
+index 742a785..f476767 100644
+--- a/mt7915/mcu.h
++++ b/mt7915/mcu.h
+@@ -42,6 +42,45 @@ struct mt7915_mcu_thermal_notify {
+ 	u8 rsv[8];
+ } __packed;
+ 
++#ifdef MTK_DEBUG
++struct mt7915_mcu_sr_basic_event {
++	struct mt76_connac2_mcu_rxd rxd;
++
++	u8 sub_event_id;
++	u8 arg_num;
++	u8 band_idx;
++	u8 status;
++	u8 rsv[4];
++	__le32 value;
++} __packed;
++
++struct mt7915_mcu_sr_swsd_event {
++	struct mt7915_mcu_sr_basic_event basic;
++	u8 rsv[32];
++	__le32 sr_tx_airtime[2];
++	__le32 obss_airtime[2];
++	__le32 my_tx_airtime[2];
++	__le32 my_rx_airtime[2];
++	__le32 channel_busy_time[2];
++	__le32 total_airtime[2];
++	__le32 total_airtime_ratio[2];
++	__le32 obss_airtime_ratio[2];
++	u8 rule[2];
++	u8 rsv2[102];
++} __packed;
++
++struct mt7915_mcu_sr_hw_ind_event {
++	struct mt7915_mcu_sr_basic_event basic;
++	u8 rsv[2];
++	__le16 non_srg_valid_cnt;
++	u8 rsv2[4];
++	__le16 inter_bss_ppdu_cnt;
++	u8 rsv3[6];
++	__le32 sr_ampdu_mpdu_cnt;
++	__le32 sr_ampdu_mpdu_acked_cnt;
++} __packed;
++#endif
++
+ struct mt7915_mcu_csa_notify {
+ 	struct mt76_connac2_mcu_rxd rxd;
+ 
+@@ -532,15 +571,24 @@ enum {
+ 
+ enum {
+ 	SPR_ENABLE = 0x1,
++	SPR_GET_ENABLE = 0x2,
+ 	SPR_ENABLE_SD = 0x3,
+ 	SPR_ENABLE_MODE = 0x5,
+ 	SPR_ENABLE_DPD = 0x23,
+ 	SPR_ENABLE_TX = 0x25,
+ 	SPR_SET_SRG_BITMAP = 0x80,
++	SPR_GET_SCENE_COND = 0x84,
+ 	SPR_SET_PARAM = 0xc2,
++	SPR_GET_STATS = 0xd4,
+ 	SPR_SET_SIGA = 0xdc,
+ };
+ 
++enum {
++	SPR_EVENT_CFG_SR_ENABLE = 0x1,
++	SPR_EVENT_SR_SW_SD = 0x82,
++	SPR_EVENT_SR_HW_IND = 0xC9,
++};
++
+ enum {
+ 	THERMAL_PROTECT_PARAMETER_CTRL,
+ 	THERMAL_PROTECT_BASIC_INFO,
+diff --git a/mt7915/mt7915.h b/mt7915/mt7915.h
+index 3a596da..2ec2c77 100644
+--- a/mt7915/mt7915.h
++++ b/mt7915/mt7915.h
+@@ -354,6 +354,9 @@ struct mt7915_phy {
+ 	struct mt7915_air_monitor_ctrl amnt_ctrl;
+ #endif
+ 	struct mt7915_scs_ctrl scs_ctrl;
++#ifdef MTK_DEBUG
++	bool sr_enable;
++#endif
+ };
+ 
+ struct mt7915_dev {
+diff --git a/mt7915/mtk_debugfs.c b/mt7915/mtk_debugfs.c
+index 1017ee7..9f8b408 100644
+--- a/mt7915/mtk_debugfs.c
++++ b/mt7915/mtk_debugfs.c
+@@ -3786,16 +3786,34 @@ mt7915_sw_aci_set(void *data, u64 val)
+ DEFINE_DEBUGFS_ATTRIBUTE(fops_sw_aci, NULL,
+ 			 mt7915_sw_aci_set, "%llx\n");
+ 
++static int
++mt7915_sr_enable_get(void *data, u64 *enable)
++{
++	struct mt7915_phy *phy = data;
++
++	*enable = phy->sr_enable;
++
++	return 0;
++}
++
+ static int
+ mt7915_sr_enable_set(void *data, u64 val)
+ {
+ 	struct mt7915_phy *phy = data;
++	int ret;
++
++	if (!!val == phy->sr_enable)
++		return 0;
++
++	ret = mt7915_mcu_enable_obss_spr(phy, SPR_ENABLE, !!val);
++	if (ret)
++		return ret;
+ 
+-	return mt7915_mcu_enable_obss_spr(phy, SPR_ENABLE, val);
++	return mt7915_mcu_enable_obss_spr(phy, SPR_GET_ENABLE, 0);
+ }
+ 
+-DEFINE_DEBUGFS_ATTRIBUTE(fops_sr_enable, NULL,
+-			 mt7915_sr_enable_set, "%llx\n");
++DEFINE_DEBUGFS_ATTRIBUTE(fops_sr_enable, mt7915_sr_enable_get,
++			 mt7915_sr_enable_set, "%llu\n");
+ 
+ static int
+ mt7915_scs_enable_set(void *data, u64 val)
+@@ -3836,6 +3854,26 @@ mt7915_thermal_recal_set(void *data, u64 val)
+ DEFINE_DEBUGFS_ATTRIBUTE(fops_thermal_recal, NULL,
+ 			 mt7915_thermal_recal_set, "%llu\n");
+ 
++static int
++mt7915_sr_stats_show(struct seq_file *file, void *data)
++{
++	struct mt7915_phy *phy = file->private;
++
++	return mt7915_mcu_enable_obss_spr(phy, SPR_GET_STATS, 0);
++}
++
++DEFINE_SHOW_ATTRIBUTE(mt7915_sr_stats);
++
++static int
++mt7915_sr_scene_cond_show(struct seq_file *file, void *data)
++{
++	struct mt7915_phy *phy = file->private;
++
++	return mt7915_mcu_enable_obss_spr(phy, SPR_GET_SCENE_COND, 0);
++}
++
++DEFINE_SHOW_ATTRIBUTE(mt7915_sr_scene_cond);
++
+ int mt7915_mtk_init_debugfs(struct mt7915_phy *phy, struct dentry *dir)
+ {
+ 	struct mt7915_dev *dev = phy->dev;
+@@ -3928,9 +3966,11 @@ int mt7915_mtk_init_debugfs(struct mt7915_phy *phy, struct dentry *dir)
+ 				    mt7915_show_eeprom_mode);
+ 	debugfs_create_file("sw_aci", 0600, dir, dev,
+ 			    &fops_sw_aci);
+-	debugfs_create_file("sr_enable", 0200, dir, phy, &fops_sr_enable);
++	debugfs_create_file("sr_enable", 0600, dir, phy, &fops_sr_enable);
+ 	debugfs_create_file("scs_enable", 0200, dir, phy, &fops_scs_enable);
+ 	debugfs_create_file("thermal_recal", 0200, dir, dev, &fops_thermal_recal);
++	debugfs_create_file("sr_stats", 0400, dir, phy, &mt7915_sr_stats_fops);
++	debugfs_create_file("sr_scene_cond", 0400, dir, phy, &mt7915_sr_scene_cond_fops);
+ 
+ 	return 0;
+ }
+-- 
+2.18.0
+