[][MAC80211][MT76][Rework Thermal Protection]

[Description]
Fix thermal protection bugs.

[Release-log]
N/A

Change-Id: I3ec0295572893c14c662a172349110e4129d1ce3
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/6923892
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/0001-wifi-mt76-mt7915-call-mt7915_mcu_set_thermal_throttl.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/0001-wifi-mt76-mt7915-call-mt7915_mcu_set_thermal_throttl.patch
new file mode 100644
index 0000000..c4c2b78
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/0001-wifi-mt76-mt7915-call-mt7915_mcu_set_thermal_throttl.patch
@@ -0,0 +1,52 @@
+From 00cd9ac2311cf4b66ab7c3d6eb3593b52bea1bec Mon Sep 17 00:00:00 2001
+From: Howard Hsu <howard-yh.hsu@mediatek.com>
+Date: Fri, 2 Dec 2022 10:16:49 +0800
+Subject: [PATCH 1/4] wifi: mt76: mt7915: call
+ mt7915_mcu_set_thermal_throttling() only after init_work
+
+Enable thermal management by default shall not be executed during mcu
+init. This causes thermal configuration being reset to the firmware
+default settings.
+
+Fixes: 0063b86c9120 ("mt76: mt7915e: Enable thermal management by default")
+Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
+Signed-off-by: Howard Hsu <howard-yh.hsu@mediatek.com>
+---
+ mt7915/init.c | 3 +--
+ mt7915/main.c | 6 ++++++
+ 2 files changed, 7 insertions(+), 2 deletions(-)
+
+diff --git a/mt7915/init.c b/mt7915/init.c
+index 6fd5c63e..a03d0b3b 100644
+--- a/mt7915/init.c
++++ b/mt7915/init.c
+@@ -201,8 +201,7 @@ static int mt7915_thermal_init(struct mt7915_phy *phy)
+ 	phy->throttle_temp[0] = 110;
+ 	phy->throttle_temp[1] = 120;
+ 
+-	return mt7915_mcu_set_thermal_throttling(phy,
+-						 MT7915_THERMAL_THROTTLE_MAX);
++	return 0;
+ }
+ 
+ static void mt7915_led_set_config(struct led_classdev *led_cdev,
+diff --git a/mt7915/main.c b/mt7915/main.c
+index 0511d6a5..98af032e 100644
+--- a/mt7915/main.c
++++ b/mt7915/main.c
+@@ -57,6 +57,12 @@ int mt7915_run(struct ieee80211_hw *hw)
+ 		mt7915_mac_enable_nf(dev, phy->mt76->band_idx);
+ 	}
+ 
++	ret = mt7915_mcu_set_thermal_throttling(phy,
++						MT7915_THERMAL_THROTTLE_MAX);
++
++	if (ret)
++		goto out;
++
+ 	ret = mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b,
+ 					     phy->mt76->band_idx);
+ 	if (ret)
+-- 
+2.18.0
+
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/0002-wifi-mt76-mt7915-rework-mt7915_mcu_set_thermal_throt.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/0002-wifi-mt76-mt7915-rework-mt7915_mcu_set_thermal_throt.patch
new file mode 100644
index 0000000..618a14e
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/0002-wifi-mt76-mt7915-rework-mt7915_mcu_set_thermal_throt.patch
@@ -0,0 +1,151 @@
+From 906bad79c7beb43c81a80fdb97898ebb977e527a Mon Sep 17 00:00:00 2001
+From: Howard Hsu <howard-yh.hsu@mediatek.com>
+Date: Mon, 5 Dec 2022 14:09:18 +0800
+Subject: [PATCH 2/4] wifi: mt76: mt7915: rework
+ mt7915_mcu_set_thermal_throttling
+
+This patch includes 2 changes:
+1. Firmware expects to disable thermal protect first before
+reconfiguring.
+2. Separate setting thermal_protect and setting thermal_tx_duty into
+different functions. These two firmware commands do not need to send
+together.
+
+Fixes: 34b877d972be ("mt76: mt7915: add thermal cooling device support")
+Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
+Signed-off-by: Howard Hsu <howard-yh.hsu@mediatek.com>
+---
+ mt7915/init.c   |  3 ---
+ mt7915/main.c   |  5 +++++
+ mt7915/mcu.c    | 54 ++++++++++++++++++++++++++++---------------------
+ mt7915/mt7915.h |  1 +
+ 4 files changed, 37 insertions(+), 26 deletions(-)
+
+diff --git a/mt7915/init.c b/mt7915/init.c
+index a03d0b3b..df3a35d5 100644
+--- a/mt7915/init.c
++++ b/mt7915/init.c
+@@ -134,9 +134,6 @@ mt7915_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
+ 	if (state > MT7915_CDEV_THROTTLE_MAX)
+ 		return -EINVAL;
+ 
+-	if (phy->throttle_temp[0] > phy->throttle_temp[1])
+-		return 0;
+-
+ 	if (state == phy->cdev_state)
+ 		return 0;
+ 
+diff --git a/mt7915/main.c b/mt7915/main.c
+index 98af032e..7589af4b 100644
+--- a/mt7915/main.c
++++ b/mt7915/main.c
+@@ -60,6 +60,11 @@ int mt7915_run(struct ieee80211_hw *hw)
+ 	ret = mt7915_mcu_set_thermal_throttling(phy,
+ 						MT7915_THERMAL_THROTTLE_MAX);
+ 
++	if (ret)
++		goto out;
++
++	ret = mt7915_mcu_set_thermal_protect(phy);
++
+ 	if (ret)
+ 		goto out;
+ 
+diff --git a/mt7915/mcu.c b/mt7915/mcu.c
+index 35441dcb..7c14a189 100644
+--- a/mt7915/mcu.c
++++ b/mt7915/mcu.c
+@@ -3077,6 +3077,29 @@ int mt7915_mcu_get_temperature(struct mt7915_phy *phy)
+ }
+ 
+ int mt7915_mcu_set_thermal_throttling(struct mt7915_phy *phy, u8 state)
++{
++	struct mt7915_dev *dev = phy->dev;
++	struct mt7915_mcu_thermal_ctrl req = {
++		.band_idx = phy->mt76->band_idx,
++		.ctrl_id = THERMAL_PROTECT_DUTY_CONFIG,
++	};
++	int level, ret;
++
++	/* set duty cycle and level */
++	for (level = 0; level < 4; level++) {
++		req.duty.duty_level = level;
++		req.duty.duty_cycle = state;
++		state /= 2;
++
++		ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_PROT),
++					&req, sizeof(req), false);
++		if (ret)
++			return ret;
++	}
++	return 0;
++}
++
++int mt7915_mcu_set_thermal_protect(struct mt7915_phy *phy)
+ {
+ 	struct mt7915_dev *dev = phy->dev;
+ 	struct {
+@@ -3089,29 +3112,18 @@ int mt7915_mcu_set_thermal_throttling(struct mt7915_phy *phy, u8 state)
+ 	} __packed req = {
+ 		.ctrl = {
+ 			.band_idx = phy->mt76->band_idx,
++			.type.protect_type = 1,
++			.type.trigger_type = 1,
+ 		},
+ 	};
+-	int level;
+-
+-	if (!state) {
+-		req.ctrl.ctrl_id = THERMAL_PROTECT_DISABLE;
+-		goto out;
+-	}
+-
+-	/* set duty cycle and level */
+-	for (level = 0; level < 4; level++) {
+-		int ret;
++	int ret;
+ 
+-		req.ctrl.ctrl_id = THERMAL_PROTECT_DUTY_CONFIG;
+-		req.ctrl.duty.duty_level = level;
+-		req.ctrl.duty.duty_cycle = state;
+-		state /= 2;
++	req.ctrl.ctrl_id = THERMAL_PROTECT_DISABLE;
++	ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_PROT),
++				&req, sizeof(req.ctrl), false);
+ 
+-		ret = mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_PROT),
+-					&req, sizeof(req.ctrl), false);
+-		if (ret)
+-			return ret;
+-	}
++	if (ret)
++		return ret;
+ 
+ 	/* set high-temperature trigger threshold */
+ 	req.ctrl.ctrl_id = THERMAL_PROTECT_ENABLE;
+@@ -3120,10 +3132,6 @@ int mt7915_mcu_set_thermal_throttling(struct mt7915_phy *phy, u8 state)
+ 	req.trigger_temp = cpu_to_le32(phy->throttle_temp[1]);
+ 	req.sustain_time = cpu_to_le16(10);
+ 
+-out:
+-	req.ctrl.type.protect_type = 1;
+-	req.ctrl.type.trigger_type = 1;
+-
+ 	return mt76_mcu_send_msg(&dev->mt76, MCU_EXT_CMD(THERMAL_PROT),
+ 				 &req, sizeof(req), false);
+ }
+diff --git a/mt7915/mt7915.h b/mt7915/mt7915.h
+index 6351feba..855779f8 100644
+--- a/mt7915/mt7915.h
++++ b/mt7915/mt7915.h
+@@ -543,6 +543,7 @@ int mt7915_mcu_apply_tx_dpd(struct mt7915_phy *phy);
+ int mt7915_mcu_get_chan_mib_info(struct mt7915_phy *phy, bool chan_switch);
+ int mt7915_mcu_get_temperature(struct mt7915_phy *phy);
+ int mt7915_mcu_set_thermal_throttling(struct mt7915_phy *phy, u8 state);
++int mt7915_mcu_set_thermal_protect(struct mt7915_phy *phy);
+ int mt7915_mcu_get_rx_rate(struct mt7915_phy *phy, struct ieee80211_vif *vif,
+ 			   struct ieee80211_sta *sta, struct rate_info *rate);
+ int mt7915_mcu_rdd_background_enable(struct mt7915_phy *phy,
+-- 
+2.18.0
+
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/0003-wifi-mt76-mt7915-rework-mt7915_thermal_temp_store.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/0003-wifi-mt76-mt7915-rework-mt7915_thermal_temp_store.patch
new file mode 100644
index 0000000..f1c0093
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/0003-wifi-mt76-mt7915-rework-mt7915_thermal_temp_store.patch
@@ -0,0 +1,73 @@
+From 2cd5969305cc6ba22a1a3ffc45f46758d09bbd1e Mon Sep 17 00:00:00 2001
+From: Howard Hsu <howard-yh.hsu@mediatek.com>
+Date: Mon, 12 Dec 2022 11:30:37 +0800
+Subject: [PATCH 3/4] wifi: mt76: mt7915: rework mt7915_thermal_temp_store()
+
+Call mt7915_mcu_set_thermal_protect() through
+mt7915_thermal_temp_store() to update firmware trigger/restore temp
+directly.
+
+Fixes: 02ee68b95d81 ("mt76: mt7915: add control knobs for thermal throttling")
+Reviewed-by: Ryder Lee <ryder.lee@mediatek.com>
+Signed-off-by: Howard Hsu <howard-yh.hsu@mediatek.com>
+---
+ mt7915/init.c   | 18 ++++++++++++++++--
+ mt7915/mt7915.h |  3 +++
+ 2 files changed, 19 insertions(+), 2 deletions(-)
+
+diff --git a/mt7915/init.c b/mt7915/init.c
+index df3a35d5..98655e4d 100644
+--- a/mt7915/init.c
++++ b/mt7915/init.c
+@@ -83,9 +83,23 @@ static ssize_t mt7915_thermal_temp_store(struct device *dev,
+ 
+ 	mutex_lock(&phy->dev->mt76.mutex);
+ 	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 60, 130);
++
++	if ((i - 1 == MT7915_CRIT_TEMP_IDX &&
++	     val > phy->throttle_temp[MT7915_MAX_TEMP_IDX]) ||
++	    (i - 1 == MT7915_MAX_TEMP_IDX &&
++	     val < phy->throttle_temp[MT7915_CRIT_TEMP_IDX])) {
++		dev_err(phy->dev->mt76.dev,
++			"temp1_max shall be greater than temp1_crit.");
++		return -EINVAL;
++	}
++
+ 	phy->throttle_temp[i - 1] = val;
+ 	mutex_unlock(&phy->dev->mt76.mutex);
+ 
++	ret = mt7915_mcu_set_thermal_protect(phy);
++	if (ret)
++		return ret;
++
+ 	return count;
+ }
+ 
+@@ -195,8 +209,8 @@ static int mt7915_thermal_init(struct mt7915_phy *phy)
+ 		return PTR_ERR(hwmon);
+ 
+ 	/* initialize critical/maximum high temperature */
+-	phy->throttle_temp[0] = 110;
+-	phy->throttle_temp[1] = 120;
++	phy->throttle_temp[MT7915_CRIT_TEMP_IDX] = 110;
++	phy->throttle_temp[MT7915_MAX_TEMP_IDX] = 120;
+ 
+ 	return 0;
+ }
+diff --git a/mt7915/mt7915.h b/mt7915/mt7915.h
+index 855779f8..e58650bb 100644
+--- a/mt7915/mt7915.h
++++ b/mt7915/mt7915.h
+@@ -70,6 +70,9 @@
+ 
+ #define MT7915_WED_RX_TOKEN_SIZE	12288
+ 
++#define MT7915_CRIT_TEMP_IDX		0
++#define MT7915_MAX_TEMP_IDX		1
++
+ struct mt7915_vif;
+ struct mt7915_sta;
+ struct mt7915_dfs_pulse;
+-- 
+2.18.0
+
diff --git a/autobuild_mac80211_release/package/kernel/mt76/patches/0004-wifi-mt76-mt7915-add-error-message-in-mt7915_thermal.patch b/autobuild_mac80211_release/package/kernel/mt76/patches/0004-wifi-mt76-mt7915-add-error-message-in-mt7915_thermal.patch
new file mode 100644
index 0000000..8f9fa45
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mt76/patches/0004-wifi-mt76-mt7915-add-error-message-in-mt7915_thermal.patch
@@ -0,0 +1,78 @@
+From 30bd3547690ec1463b34c726577ae821e92a4390 Mon Sep 17 00:00:00 2001
+From: Howard Hsu <howard-yh.hsu@mediatek.com>
+Date: Mon, 12 Dec 2022 13:18:49 +0800
+Subject: [PATCH 4/4] wifi: mt76: mt7915: add error message in
+ mt7915_thermal_set_cur_throttle_state()
+
+Add dev_err so that it is easier to see invalid setting while looking at
+dmesg.
+
+Co-developed-by: Ryder Lee <ryder.lee@mediatek.com>
+Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
+Signed-off-by: Howard Hsu <howard-yh.hsu@mediatek.com>
+---
+ mt7915/init.c   | 12 +++++++-----
+ mt7915/mt7915.h |  2 ++
+ 2 files changed, 9 insertions(+), 5 deletions(-)
+
+diff --git a/mt7915/init.c b/mt7915/init.c
+index 98655e4d..b23a2fb8 100644
+--- a/mt7915/init.c
++++ b/mt7915/init.c
+@@ -145,8 +145,11 @@ mt7915_thermal_set_cur_throttle_state(struct thermal_cooling_device *cdev,
+ 	u8 throttling = MT7915_THERMAL_THROTTLE_MAX - state;
+ 	int ret;
+ 
+-	if (state > MT7915_CDEV_THROTTLE_MAX)
++	if (state > MT7915_CDEV_THROTTLE_MAX) {
++		dev_err(phy->dev->mt76.dev,
++			"please specify a valid throttling state\n");
+ 		return -EINVAL;
++	}
+ 
+ 	if (state == phy->cdev_state)
+ 		return 0;
+@@ -175,7 +178,7 @@ static void mt7915_unregister_thermal(struct mt7915_phy *phy)
+ 	struct wiphy *wiphy = phy->mt76->hw->wiphy;
+ 
+ 	if (!phy->cdev)
+-	    return;
++		return;
+ 
+ 	sysfs_remove_link(&wiphy->dev.kobj, "cooling_device");
+ 	thermal_cooling_device_unregister(phy->cdev);
+@@ -209,8 +212,8 @@ static int mt7915_thermal_init(struct mt7915_phy *phy)
+ 		return PTR_ERR(hwmon);
+ 
+ 	/* initialize critical/maximum high temperature */
+-	phy->throttle_temp[MT7915_CRIT_TEMP_IDX] = 110;
+-	phy->throttle_temp[MT7915_MAX_TEMP_IDX] = 120;
++	phy->throttle_temp[MT7915_CRIT_TEMP_IDX] = MT7915_CRIT_TEMP;
++	phy->throttle_temp[MT7915_MAX_TEMP_IDX] = MT7915_MAX_TEMP;
+ 
+ 	return 0;
+ }
+@@ -1185,7 +1188,6 @@ static void mt7915_stop_hardware(struct mt7915_dev *dev)
+ 		mt7986_wmac_disable(dev);
+ }
+ 
+-
+ int mt7915_register_device(struct mt7915_dev *dev)
+ {
+ 	struct mt7915_phy *phy2;
+diff --git a/mt7915/mt7915.h b/mt7915/mt7915.h
+index e58650bb..942d70c5 100644
+--- a/mt7915/mt7915.h
++++ b/mt7915/mt7915.h
+@@ -72,6 +72,8 @@
+ 
+ #define MT7915_CRIT_TEMP_IDX		0
+ #define MT7915_MAX_TEMP_IDX		1
++#define MT7915_CRIT_TEMP		110
++#define MT7915_MAX_TEMP			120
+ 
+ struct mt7915_vif;
+ struct mt7915_sta;
+-- 
+2.18.0
+