[][MAC80211][core][Conditionally skip bandwidth downgrading]

[Description]
Add the condition that checks the control channel before downgrading the bandwidth to avoid
undesirable channel downgrading

[Release-log]
N/A


Change-Id: I299ec185aff058d87e05742ca1693eac8f3e7967
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/6905448
diff --git a/autobuild_mac80211_release/package/kernel/mac80211/patches/subsys/912-mac80211-mtk-check-the-control-channel-before-downgr.patch b/autobuild_mac80211_release/package/kernel/mac80211/patches/subsys/912-mac80211-mtk-check-the-control-channel-before-downgr.patch
new file mode 100644
index 0000000..1f115ef
--- /dev/null
+++ b/autobuild_mac80211_release/package/kernel/mac80211/patches/subsys/912-mac80211-mtk-check-the-control-channel-before-downgr.patch
@@ -0,0 +1,55 @@
+From 8fe4d8388de19d99992da0dd6ba0ae90ed4141e9 Mon Sep 17 00:00:00 2001
+From: mtk31095 <michael-cy.lee@mediatek.com>
+Date: Fri, 16 Dec 2022 10:37:53 +0800
+Subject: [PATCH] mac80211: mtk: check the control channel before downgrading
+ the bandwidth
+
+Signed-off-by: mtk31095 <michael-cy.lee@mediatek.com>
+---
+ net/mac80211/mlme.c | 23 +++++++++++++++++++++++
+ 1 file changed, 23 insertions(+)
+
+diff --git a/net/mac80211/mlme.c b/net/mac80211/mlme.c
+index f97bf2b..8ee325a 100644
+--- a/net/mac80211/mlme.c
++++ b/net/mac80211/mlme.c
+@@ -5019,6 +5019,26 @@ ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
+ 	return false;
+ }
+ 
++static bool ieee80211_check_same_ctrl_channel(struct ieee80211_sub_if_data *sdata,
++					      const struct cfg80211_chan_def *chandef)
++{
++	struct ieee80211_local *local = sdata->local;
++	struct ieee80211_chanctx *ctx;
++
++	mutex_lock(&local->chanctx_mtx);
++	list_for_each_entry(ctx, &local->chanctx_list, list) {
++		if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
++			continue;
++		if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
++			continue;
++		if (chandef->chan == ctx->conf.def.chan)
++			return true;
++	}
++
++	mutex_unlock(&local->chanctx_mtx);
++	return false;
++}
++
+ static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
+ 				  struct cfg80211_bss *cbss)
+ {
+@@ -5179,6 +5199,9 @@ static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
+ 	    chandef.width == NL80211_CHAN_WIDTH_10)
+ 		goto out;
+ 
++	if (!ret || !ieee80211_check_same_ctrl_channel(sdata, &chandef))
++		goto out;
++
+ 	while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
+ 		ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
+ 		ret = ieee80211_vif_use_channel(sdata, &chandef,
+-- 
+2.25.1
+