xref: /openbmc/linux/drivers/net/wireless/realtek/rtw89/phy.c (revision cbb145b98b63e1c479e2e23d3bc1f15bfe0e2b3a)
1e3ec7017SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3ec7017SPing-Ke Shih /* Copyright(c) 2019-2020  Realtek Corporation
3e3ec7017SPing-Ke Shih  */
4e3ec7017SPing-Ke Shih 
5e3ec7017SPing-Ke Shih #include "debug.h"
6e3ec7017SPing-Ke Shih #include "fw.h"
78379fa61SYuan-Han Zhang #include "mac.h"
8e3ec7017SPing-Ke Shih #include "phy.h"
9e3ec7017SPing-Ke Shih #include "ps.h"
10e3ec7017SPing-Ke Shih #include "reg.h"
11e3ec7017SPing-Ke Shih #include "sar.h"
12e3ec7017SPing-Ke Shih #include "coex.h"
13e3ec7017SPing-Ke Shih 
14e3ec7017SPing-Ke Shih static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev,
15e3ec7017SPing-Ke Shih 			     const struct rtw89_ra_report *report)
16e3ec7017SPing-Ke Shih {
17e3ec7017SPing-Ke Shih 	const struct rate_info *txrate = &report->txrate;
18e3ec7017SPing-Ke Shih 	u32 bit_rate = report->bit_rate;
19e3ec7017SPing-Ke Shih 	u8 mcs;
20e3ec7017SPing-Ke Shih 
21e3ec7017SPing-Ke Shih 	/* lower than ofdm, do not aggregate */
22e3ec7017SPing-Ke Shih 	if (bit_rate < 550)
23e3ec7017SPing-Ke Shih 		return 1;
24e3ec7017SPing-Ke Shih 
25e3ec7017SPing-Ke Shih 	/* prevent hardware rate fallback to G mode rate */
26e3ec7017SPing-Ke Shih 	if (txrate->flags & RATE_INFO_FLAGS_MCS)
27e3ec7017SPing-Ke Shih 		mcs = txrate->mcs & 0x07;
28e3ec7017SPing-Ke Shih 	else if (txrate->flags & (RATE_INFO_FLAGS_VHT_MCS | RATE_INFO_FLAGS_HE_MCS))
29e3ec7017SPing-Ke Shih 		mcs = txrate->mcs;
30e3ec7017SPing-Ke Shih 	else
31e3ec7017SPing-Ke Shih 		mcs = 0;
32e3ec7017SPing-Ke Shih 
33e3ec7017SPing-Ke Shih 	if (mcs <= 2)
34e3ec7017SPing-Ke Shih 		return 1;
35e3ec7017SPing-Ke Shih 
36e3ec7017SPing-Ke Shih 	/* lower than 20M vht 2ss mcs8, make it small */
37e3ec7017SPing-Ke Shih 	if (bit_rate < 1800)
38e3ec7017SPing-Ke Shih 		return 1200;
39e3ec7017SPing-Ke Shih 
40e3ec7017SPing-Ke Shih 	/* lower than 40M vht 2ss mcs9, make it medium */
41e3ec7017SPing-Ke Shih 	if (bit_rate < 4000)
42e3ec7017SPing-Ke Shih 		return 2600;
43e3ec7017SPing-Ke Shih 
44e3ec7017SPing-Ke Shih 	/* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */
45e3ec7017SPing-Ke Shih 	if (bit_rate < 7000)
46e3ec7017SPing-Ke Shih 		return 3500;
47e3ec7017SPing-Ke Shih 
48e3ec7017SPing-Ke Shih 	return rtwdev->chip->max_amsdu_limit;
49e3ec7017SPing-Ke Shih }
50e3ec7017SPing-Ke Shih 
51e3ec7017SPing-Ke Shih static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap)
52e3ec7017SPing-Ke Shih {
53e3ec7017SPing-Ke Shih 	u64 ra_mask = 0;
54e3ec7017SPing-Ke Shih 	u8 mcs_cap;
55e3ec7017SPing-Ke Shih 	int i, nss;
56e3ec7017SPing-Ke Shih 
57e3ec7017SPing-Ke Shih 	for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) {
58e3ec7017SPing-Ke Shih 		mcs_cap = mcs_map & 0x3;
59e3ec7017SPing-Ke Shih 		switch (mcs_cap) {
60e3ec7017SPing-Ke Shih 		case 2:
61e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss;
62e3ec7017SPing-Ke Shih 			break;
63e3ec7017SPing-Ke Shih 		case 1:
64e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss;
65e3ec7017SPing-Ke Shih 			break;
66e3ec7017SPing-Ke Shih 		case 0:
67e3ec7017SPing-Ke Shih 			ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss;
68e3ec7017SPing-Ke Shih 			break;
69e3ec7017SPing-Ke Shih 		default:
70e3ec7017SPing-Ke Shih 			break;
71e3ec7017SPing-Ke Shih 		}
72e3ec7017SPing-Ke Shih 	}
73e3ec7017SPing-Ke Shih 
74e3ec7017SPing-Ke Shih 	return ra_mask;
75e3ec7017SPing-Ke Shih }
76e3ec7017SPing-Ke Shih 
77e3ec7017SPing-Ke Shih static u64 get_he_ra_mask(struct ieee80211_sta *sta)
78e3ec7017SPing-Ke Shih {
79046d2e7cSSriram R 	struct ieee80211_sta_he_cap cap = sta->deflink.he_cap;
80e3ec7017SPing-Ke Shih 	u16 mcs_map;
81e3ec7017SPing-Ke Shih 
82046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
83e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_160:
84e3ec7017SPing-Ke Shih 		if (cap.he_cap_elem.phy_cap_info[0] &
85e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
86e3ec7017SPing-Ke Shih 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80);
87e3ec7017SPing-Ke Shih 		else
88e3ec7017SPing-Ke Shih 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160);
89e3ec7017SPing-Ke Shih 		break;
90e3ec7017SPing-Ke Shih 	default:
91e3ec7017SPing-Ke Shih 		mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80);
92e3ec7017SPing-Ke Shih 	}
93e3ec7017SPing-Ke Shih 
94e3ec7017SPing-Ke Shih 	/* MCS11, MCS9, MCS7 */
95e3ec7017SPing-Ke Shih 	return get_mcs_ra_mask(mcs_map, 11, 2);
96e3ec7017SPing-Ke Shih }
97e3ec7017SPing-Ke Shih 
98e3ec7017SPing-Ke Shih #define RA_FLOOR_TABLE_SIZE	7
99e3ec7017SPing-Ke Shih #define RA_FLOOR_UP_GAP		3
100e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi,
101e3ec7017SPing-Ke Shih 				  u8 ratr_state)
102e3ec7017SPing-Ke Shih {
103e3ec7017SPing-Ke Shih 	u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100};
104e3ec7017SPing-Ke Shih 	u8 rssi_lv = 0;
105e3ec7017SPing-Ke Shih 	u8 i;
106e3ec7017SPing-Ke Shih 
107e3ec7017SPing-Ke Shih 	rssi >>= 1;
108e3ec7017SPing-Ke Shih 	for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) {
109e3ec7017SPing-Ke Shih 		if (i >= ratr_state)
110e3ec7017SPing-Ke Shih 			rssi_lv_t[i] += RA_FLOOR_UP_GAP;
111e3ec7017SPing-Ke Shih 		if (rssi < rssi_lv_t[i]) {
112e3ec7017SPing-Ke Shih 			rssi_lv = i;
113e3ec7017SPing-Ke Shih 			break;
114e3ec7017SPing-Ke Shih 		}
115e3ec7017SPing-Ke Shih 	}
116e3ec7017SPing-Ke Shih 	if (rssi_lv == 0)
117e3ec7017SPing-Ke Shih 		return 0xffffffffffffffffULL;
118e3ec7017SPing-Ke Shih 	else if (rssi_lv == 1)
119e3ec7017SPing-Ke Shih 		return 0xfffffffffffffff0ULL;
120e3ec7017SPing-Ke Shih 	else if (rssi_lv == 2)
1213c2c2e2eSChien-Hsun Liao 		return 0xffffffffffffefe0ULL;
122e3ec7017SPing-Ke Shih 	else if (rssi_lv == 3)
1233c2c2e2eSChien-Hsun Liao 		return 0xffffffffffffcfc0ULL;
124e3ec7017SPing-Ke Shih 	else if (rssi_lv == 4)
1253c2c2e2eSChien-Hsun Liao 		return 0xffffffffffff8f80ULL;
126e3ec7017SPing-Ke Shih 	else if (rssi_lv >= 5)
1273c2c2e2eSChien-Hsun Liao 		return 0xffffffffffff0f00ULL;
128e3ec7017SPing-Ke Shih 
129e3ec7017SPing-Ke Shih 	return 0xffffffffffffffffULL;
130e3ec7017SPing-Ke Shih }
131e3ec7017SPing-Ke Shih 
1323c2c2e2eSChien-Hsun Liao static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak)
1333c2c2e2eSChien-Hsun Liao {
1343c2c2e2eSChien-Hsun Liao 	if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0)
1353c2c2e2eSChien-Hsun Liao 		ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
1363c2c2e2eSChien-Hsun Liao 
1373c2c2e2eSChien-Hsun Liao 	if (ra_mask == 0)
1383c2c2e2eSChien-Hsun Liao 		ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
1393c2c2e2eSChien-Hsun Liao 
1403c2c2e2eSChien-Hsun Liao 	return ra_mask;
1413c2c2e2eSChien-Hsun Liao }
1423c2c2e2eSChien-Hsun Liao 
143e3ec7017SPing-Ke Shih static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta)
144e3ec7017SPing-Ke Shih {
145e3ec7017SPing-Ke Shih 	struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta);
146*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
147e3ec7017SPing-Ke Shih 	struct cfg80211_bitrate_mask *mask = &rtwsta->mask;
148e3ec7017SPing-Ke Shih 	enum nl80211_band band;
149e3ec7017SPing-Ke Shih 	u64 cfg_mask;
150e3ec7017SPing-Ke Shih 
151e3ec7017SPing-Ke Shih 	if (!rtwsta->use_cfg_mask)
152e3ec7017SPing-Ke Shih 		return -1;
153e3ec7017SPing-Ke Shih 
154*cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
155e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
156e3ec7017SPing-Ke Shih 		band = NL80211_BAND_2GHZ;
157e3ec7017SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy,
158e3ec7017SPing-Ke Shih 					   RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES);
159e3ec7017SPing-Ke Shih 		break;
160e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
161e3ec7017SPing-Ke Shih 		band = NL80211_BAND_5GHZ;
162e3ec7017SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy,
163e3ec7017SPing-Ke Shih 					   RA_MASK_OFDM_RATES);
164e3ec7017SPing-Ke Shih 		break;
165f76b3276SPing-Ke Shih 	case RTW89_BAND_6G:
166f76b3276SPing-Ke Shih 		band = NL80211_BAND_6GHZ;
167f76b3276SPing-Ke Shih 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy,
168f76b3276SPing-Ke Shih 					   RA_MASK_OFDM_RATES);
169f76b3276SPing-Ke Shih 		break;
170e3ec7017SPing-Ke Shih 	default:
171*cbb145b9SZong-Zhe Yang 		rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type);
172e3ec7017SPing-Ke Shih 		return -1;
173e3ec7017SPing-Ke Shih 	}
174e3ec7017SPing-Ke Shih 
175046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
176e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0],
177e3ec7017SPing-Ke Shih 					    RA_MASK_HE_1SS_RATES);
178e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1],
179e3ec7017SPing-Ke Shih 					    RA_MASK_HE_2SS_RATES);
180046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
181e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0],
182e3ec7017SPing-Ke Shih 					    RA_MASK_VHT_1SS_RATES);
183e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1],
184e3ec7017SPing-Ke Shih 					    RA_MASK_VHT_2SS_RATES);
185046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
186e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0],
187e3ec7017SPing-Ke Shih 					    RA_MASK_HT_1SS_RATES);
188e3ec7017SPing-Ke Shih 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1],
189e3ec7017SPing-Ke Shih 					    RA_MASK_HT_2SS_RATES);
190e3ec7017SPing-Ke Shih 	}
191e3ec7017SPing-Ke Shih 
192e3ec7017SPing-Ke Shih 	return cfg_mask;
193e3ec7017SPing-Ke Shih }
194e3ec7017SPing-Ke Shih 
195e3ec7017SPing-Ke Shih static const u64
196e3ec7017SPing-Ke Shih rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES,
197e3ec7017SPing-Ke Shih 			     RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES};
198e3ec7017SPing-Ke Shih static const u64
199e3ec7017SPing-Ke Shih rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES,
200e3ec7017SPing-Ke Shih 			      RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES};
201e3ec7017SPing-Ke Shih static const u64
202e3ec7017SPing-Ke Shih rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES,
203e3ec7017SPing-Ke Shih 			     RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES};
204e3ec7017SPing-Ke Shih 
205e3ec7017SPing-Ke Shih static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev,
206e3ec7017SPing-Ke Shih 				    struct ieee80211_sta *sta, bool csi)
207e3ec7017SPing-Ke Shih {
208e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
209e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
210e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
211e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
212*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
213e3ec7017SPing-Ke Shih 	const u64 *high_rate_masks = rtw89_ra_mask_ht_rates;
214e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi);
215e3ec7017SPing-Ke Shih 	u64 ra_mask = 0;
2163c2c2e2eSChien-Hsun Liao 	u64 ra_mask_bak;
217e3ec7017SPing-Ke Shih 	u8 mode = 0;
218e3ec7017SPing-Ke Shih 	u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY;
219e3ec7017SPing-Ke Shih 	u8 bw_mode = 0;
220e3ec7017SPing-Ke Shih 	u8 stbc_en = 0;
221e3ec7017SPing-Ke Shih 	u8 ldpc_en = 0;
222e3ec7017SPing-Ke Shih 	u8 i;
223e3ec7017SPing-Ke Shih 	bool sgi = false;
224e3ec7017SPing-Ke Shih 
225e3ec7017SPing-Ke Shih 	memset(ra, 0, sizeof(*ra));
226e3ec7017SPing-Ke Shih 	/* Set the ra mask from sta's capability */
227046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
228e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HE;
229e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HE;
230e3ec7017SPing-Ke Shih 		ra_mask |= get_he_ra_mask(sta);
231e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_he_rates;
232046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] &
233e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
234e3ec7017SPing-Ke Shih 			stbc_en = 1;
235046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] &
236e3ec7017SPing-Ke Shih 		    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
237e3ec7017SPing-Ke Shih 			ldpc_en = 1;
238046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
239046d2e7cSSriram R 		u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map);
240e3ec7017SPing-Ke Shih 
241e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_VHT;
242e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_VHT;
243e3ec7017SPing-Ke Shih 		/* MCS9, MCS8, MCS7 */
244e3ec7017SPing-Ke Shih 		ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1);
245e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_vht_rates;
246046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
247e3ec7017SPing-Ke Shih 			stbc_en = 1;
248046d2e7cSSriram R 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
249e3ec7017SPing-Ke Shih 			ldpc_en = 1;
250046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
251e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_HT;
252e3ec7017SPing-Ke Shih 		csi_mode = RTW89_RA_RPT_MODE_HT;
253046d2e7cSSriram R 		ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) |
254046d2e7cSSriram R 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) |
255046d2e7cSSriram R 			   (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) |
256046d2e7cSSriram R 			   (sta->deflink.ht_cap.mcs.rx_mask[0] << 12);
257e3ec7017SPing-Ke Shih 		high_rate_masks = rtw89_ra_mask_ht_rates;
258046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
259e3ec7017SPing-Ke Shih 			stbc_en = 1;
260046d2e7cSSriram R 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
261e3ec7017SPing-Ke Shih 			ldpc_en = 1;
262e3ec7017SPing-Ke Shih 	}
263e3ec7017SPing-Ke Shih 
264*cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
265f76b3276SPing-Ke Shih 	case RTW89_BAND_2G:
266046d2e7cSSriram R 		ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ];
267046d2e7cSSriram R 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] <= 0xf)
268e3ec7017SPing-Ke Shih 			mode |= RTW89_RA_MODE_CCK;
269e3ec7017SPing-Ke Shih 		else
270e3ec7017SPing-Ke Shih 			mode |= RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM;
271f76b3276SPing-Ke Shih 		break;
272f76b3276SPing-Ke Shih 	case RTW89_BAND_5G:
273046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4;
274e3ec7017SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
275f76b3276SPing-Ke Shih 		break;
276f76b3276SPing-Ke Shih 	case RTW89_BAND_6G:
277046d2e7cSSriram R 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4;
278f76b3276SPing-Ke Shih 		mode |= RTW89_RA_MODE_OFDM;
279f76b3276SPing-Ke Shih 		break;
280f76b3276SPing-Ke Shih 	default:
281f76b3276SPing-Ke Shih 		rtw89_err(rtwdev, "Unknown band type\n");
282f76b3276SPing-Ke Shih 		break;
283e3ec7017SPing-Ke Shih 	}
284e3ec7017SPing-Ke Shih 
2853c2c2e2eSChien-Hsun Liao 	ra_mask_bak = ra_mask;
2863c2c2e2eSChien-Hsun Liao 
287e3ec7017SPing-Ke Shih 	if (mode >= RTW89_RA_MODE_HT) {
2883c2c2e2eSChien-Hsun Liao 		u64 mask = 0;
289e3ec7017SPing-Ke Shih 		for (i = 0; i < rtwdev->hal.tx_nss; i++)
2903c2c2e2eSChien-Hsun Liao 			mask |= high_rate_masks[i];
291e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_OFDM)
2923c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBOFDM_RATES;
293e3ec7017SPing-Ke Shih 		if (mode & RTW89_RA_MODE_CCK)
2943c2c2e2eSChien-Hsun Liao 			mask |= RA_MASK_SUBCCK_RATES;
2953c2c2e2eSChien-Hsun Liao 		ra_mask &= mask;
296e3ec7017SPing-Ke Shih 	} else if (mode & RTW89_RA_MODE_OFDM) {
2973c2c2e2eSChien-Hsun Liao 		ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES);
298e3ec7017SPing-Ke Shih 	}
299e3ec7017SPing-Ke Shih 
3003c2c2e2eSChien-Hsun Liao 	if (mode != RTW89_RA_MODE_CCK)
301e3ec7017SPing-Ke Shih 		ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0);
3023c2c2e2eSChien-Hsun Liao 
3033c2c2e2eSChien-Hsun Liao 	ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak);
304e3ec7017SPing-Ke Shih 	ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
305e3ec7017SPing-Ke Shih 
306046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
307167044afSPing-Ke Shih 	case IEEE80211_STA_RX_BW_160:
308167044afSPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_160;
309046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
310046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160);
311167044afSPing-Ke Shih 		break;
312e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_80:
313e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_80;
314046d2e7cSSriram R 		sgi = sta->deflink.vht_cap.vht_supported &&
315046d2e7cSSriram R 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
316e3ec7017SPing-Ke Shih 		break;
317e3ec7017SPing-Ke Shih 	case IEEE80211_STA_RX_BW_40:
318e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_40;
319046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
320046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40);
321e3ec7017SPing-Ke Shih 		break;
322e3ec7017SPing-Ke Shih 	default:
323e3ec7017SPing-Ke Shih 		bw_mode = RTW89_CHANNEL_WIDTH_20;
324046d2e7cSSriram R 		sgi = sta->deflink.ht_cap.ht_supported &&
325046d2e7cSSriram R 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
326e3ec7017SPing-Ke Shih 		break;
327e3ec7017SPing-Ke Shih 	}
328e3ec7017SPing-Ke Shih 
329046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] &
330e3ec7017SPing-Ke Shih 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM)
331e3ec7017SPing-Ke Shih 		ra->dcm_cap = 1;
332e3ec7017SPing-Ke Shih 
333e3ec7017SPing-Ke Shih 	if (rate_pattern->enable) {
334e3ec7017SPing-Ke Shih 		ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
335e3ec7017SPing-Ke Shih 		ra_mask &= rate_pattern->ra_mask;
336e3ec7017SPing-Ke Shih 		mode = rate_pattern->ra_mode;
337e3ec7017SPing-Ke Shih 	}
338e3ec7017SPing-Ke Shih 
339e3ec7017SPing-Ke Shih 	ra->bw_cap = bw_mode;
340e3ec7017SPing-Ke Shih 	ra->mode_ctrl = mode;
341e3ec7017SPing-Ke Shih 	ra->macid = rtwsta->mac_id;
342e3ec7017SPing-Ke Shih 	ra->stbc_cap = stbc_en;
343e3ec7017SPing-Ke Shih 	ra->ldpc_cap = ldpc_en;
344046d2e7cSSriram R 	ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1;
345e3ec7017SPing-Ke Shih 	ra->en_sgi = sgi;
346e3ec7017SPing-Ke Shih 	ra->ra_mask = ra_mask;
347e3ec7017SPing-Ke Shih 
348e3ec7017SPing-Ke Shih 	if (!csi)
349e3ec7017SPing-Ke Shih 		return;
350e3ec7017SPing-Ke Shih 
351e3ec7017SPing-Ke Shih 	ra->fixed_csi_rate_en = false;
352e3ec7017SPing-Ke Shih 	ra->ra_csi_rate_en = true;
353e3ec7017SPing-Ke Shih 	ra->cr_tbl_sel = false;
354e3ec7017SPing-Ke Shih 	ra->band_num = rtwvif->phy_idx;
355e3ec7017SPing-Ke Shih 	ra->csi_bw = bw_mode;
356e3ec7017SPing-Ke Shih 	ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32;
357e3ec7017SPing-Ke Shih 	ra->csi_mcs_ss_idx = 5;
358e3ec7017SPing-Ke Shih 	ra->csi_mode = csi_mode;
359e3ec7017SPing-Ke Shih }
360e3ec7017SPing-Ke Shih 
3619d9a9edcSPing-Ke Shih void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta,
3629d9a9edcSPing-Ke Shih 			     u32 changed)
363e3ec7017SPing-Ke Shih {
364e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
365e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
366e3ec7017SPing-Ke Shih 
367e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, false);
3689d9a9edcSPing-Ke Shih 
3699d9a9edcSPing-Ke Shih 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED)
370e3ec7017SPing-Ke Shih 		ra->upd_mask = 1;
3719d9a9edcSPing-Ke Shih 	if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED))
3729d9a9edcSPing-Ke Shih 		ra->upd_bw_nss_mask = 1;
3739d9a9edcSPing-Ke Shih 
374e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
375e3ec7017SPing-Ke Shih 		    "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d",
376e3ec7017SPing-Ke Shih 		    ra->macid,
377e3ec7017SPing-Ke Shih 		    ra->bw_cap,
378e3ec7017SPing-Ke Shih 		    ra->ss_num,
379e3ec7017SPing-Ke Shih 		    ra->en_sgi,
380e3ec7017SPing-Ke Shih 		    ra->giltf);
381e3ec7017SPing-Ke Shih 
382e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, false);
383e3ec7017SPing-Ke Shih }
384e3ec7017SPing-Ke Shih 
385e3ec7017SPing-Ke Shih static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next,
386e3ec7017SPing-Ke Shih 				 u16 rate_base, u64 ra_mask, u8 ra_mode,
387e3ec7017SPing-Ke Shih 				 u32 rate_ctrl, u32 ctrl_skip, bool force)
388e3ec7017SPing-Ke Shih {
389e3ec7017SPing-Ke Shih 	u8 n, c;
390e3ec7017SPing-Ke Shih 
391e3ec7017SPing-Ke Shih 	if (rate_ctrl == ctrl_skip)
392e3ec7017SPing-Ke Shih 		return true;
393e3ec7017SPing-Ke Shih 
394e3ec7017SPing-Ke Shih 	n = hweight32(rate_ctrl);
395e3ec7017SPing-Ke Shih 	if (n == 0)
396e3ec7017SPing-Ke Shih 		return true;
397e3ec7017SPing-Ke Shih 
398e3ec7017SPing-Ke Shih 	if (force && n != 1)
399e3ec7017SPing-Ke Shih 		return false;
400e3ec7017SPing-Ke Shih 
401e3ec7017SPing-Ke Shih 	if (next->enable)
402e3ec7017SPing-Ke Shih 		return false;
403e3ec7017SPing-Ke Shih 
404e3ec7017SPing-Ke Shih 	c = __fls(rate_ctrl);
405e3ec7017SPing-Ke Shih 	next->rate = rate_base + c;
406e3ec7017SPing-Ke Shih 	next->ra_mode = ra_mode;
407e3ec7017SPing-Ke Shih 	next->ra_mask = ra_mask;
408e3ec7017SPing-Ke Shih 	next->enable = true;
409e3ec7017SPing-Ke Shih 
410e3ec7017SPing-Ke Shih 	return true;
411e3ec7017SPing-Ke Shih }
412e3ec7017SPing-Ke Shih 
413e3ec7017SPing-Ke Shih void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev,
414e3ec7017SPing-Ke Shih 				struct ieee80211_vif *vif,
415e3ec7017SPing-Ke Shih 				const struct cfg80211_bitrate_mask *mask)
416e3ec7017SPing-Ke Shih {
417e3ec7017SPing-Ke Shih 	struct ieee80211_supported_band *sband;
418e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
419e3ec7017SPing-Ke Shih 	struct rtw89_phy_rate_pattern next_pattern = {0};
420*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
421e3ec7017SPing-Ke Shih 	static const u16 hw_rate_he[] = {RTW89_HW_RATE_HE_NSS1_MCS0,
422e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS2_MCS0,
423e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS3_MCS0,
424e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_HE_NSS4_MCS0};
425e3ec7017SPing-Ke Shih 	static const u16 hw_rate_vht[] = {RTW89_HW_RATE_VHT_NSS1_MCS0,
426e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS2_MCS0,
427e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS3_MCS0,
428e3ec7017SPing-Ke Shih 					  RTW89_HW_RATE_VHT_NSS4_MCS0};
429e3ec7017SPing-Ke Shih 	static const u16 hw_rate_ht[] = {RTW89_HW_RATE_MCS0,
430e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS8,
431e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS16,
432e3ec7017SPing-Ke Shih 					 RTW89_HW_RATE_MCS24};
433*cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
434a06d2dd7SZong-Zhe Yang 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
435e3ec7017SPing-Ke Shih 	u8 tx_nss = rtwdev->hal.tx_nss;
436e3ec7017SPing-Ke Shih 	u8 i;
437e3ec7017SPing-Ke Shih 
438e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
439e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_he[i],
440e3ec7017SPing-Ke Shih 					  RA_MASK_HE_RATES, RTW89_RA_MODE_HE,
441a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].he_mcs[i],
442e3ec7017SPing-Ke Shih 					  0, true))
443e3ec7017SPing-Ke Shih 			goto out;
444e3ec7017SPing-Ke Shih 
445e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
446e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i],
447e3ec7017SPing-Ke Shih 					  RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT,
448a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].vht_mcs[i],
449e3ec7017SPing-Ke Shih 					  0, true))
450e3ec7017SPing-Ke Shih 			goto out;
451e3ec7017SPing-Ke Shih 
452e3ec7017SPing-Ke Shih 	for (i = 0; i < tx_nss; i++)
453e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i],
454e3ec7017SPing-Ke Shih 					  RA_MASK_HT_RATES, RTW89_RA_MODE_HT,
455a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].ht_mcs[i],
456e3ec7017SPing-Ke Shih 					  0, true))
457e3ec7017SPing-Ke Shih 			goto out;
458e3ec7017SPing-Ke Shih 
459e3ec7017SPing-Ke Shih 	/* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and
460e3ec7017SPing-Ke Shih 	 * require at least one basic rate for ieee80211_set_bitrate_mask,
461e3ec7017SPing-Ke Shih 	 * so the decision just depends on if all bitrates are set or not.
462e3ec7017SPing-Ke Shih 	 */
463a06d2dd7SZong-Zhe Yang 	sband = rtwdev->hw->wiphy->bands[nl_band];
464e3ec7017SPing-Ke Shih 	if (band == RTW89_BAND_2G) {
465e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1,
466e3ec7017SPing-Ke Shih 					  RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES,
467e3ec7017SPing-Ke Shih 					  RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM,
468a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
469e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
470e3ec7017SPing-Ke Shih 			goto out;
471e3ec7017SPing-Ke Shih 	} else {
472e3ec7017SPing-Ke Shih 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6,
473e3ec7017SPing-Ke Shih 					  RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM,
474a06d2dd7SZong-Zhe Yang 					  mask->control[nl_band].legacy,
475e3ec7017SPing-Ke Shih 					  BIT(sband->n_bitrates) - 1, false))
476e3ec7017SPing-Ke Shih 			goto out;
477e3ec7017SPing-Ke Shih 	}
478e3ec7017SPing-Ke Shih 
479e3ec7017SPing-Ke Shih 	if (!next_pattern.enable)
480e3ec7017SPing-Ke Shih 		goto out;
481e3ec7017SPing-Ke Shih 
482e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern = next_pattern;
483e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
484e3ec7017SPing-Ke Shih 		    "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n",
485e3ec7017SPing-Ke Shih 		    next_pattern.rate,
486e3ec7017SPing-Ke Shih 		    next_pattern.ra_mask,
487e3ec7017SPing-Ke Shih 		    next_pattern.ra_mode);
488e3ec7017SPing-Ke Shih 	return;
489e3ec7017SPing-Ke Shih 
490e3ec7017SPing-Ke Shih out:
491e3ec7017SPing-Ke Shih 	rtwvif->rate_pattern.enable = false;
492e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n");
493e3ec7017SPing-Ke Shih }
494e3ec7017SPing-Ke Shih 
495e3ec7017SPing-Ke Shih static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta)
496e3ec7017SPing-Ke Shih {
497e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
498e3ec7017SPing-Ke Shih 
4999d9a9edcSPing-Ke Shih 	rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
500e3ec7017SPing-Ke Shih }
501e3ec7017SPing-Ke Shih 
502e3ec7017SPing-Ke Shih void rtw89_phy_ra_update(struct rtw89_dev *rtwdev)
503e3ec7017SPing-Ke Shih {
504e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
505e3ec7017SPing-Ke Shih 					  rtw89_phy_ra_updata_sta_iter,
506e3ec7017SPing-Ke Shih 					  rtwdev);
507e3ec7017SPing-Ke Shih }
508e3ec7017SPing-Ke Shih 
509e3ec7017SPing-Ke Shih void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta)
510e3ec7017SPing-Ke Shih {
511e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
512e3ec7017SPing-Ke Shih 	struct rtw89_ra_info *ra = &rtwsta->ra;
513e3ec7017SPing-Ke Shih 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR;
514e3ec7017SPing-Ke Shih 	bool csi = rtw89_sta_has_beamformer_cap(sta);
515e3ec7017SPing-Ke Shih 
516e3ec7017SPing-Ke Shih 	rtw89_phy_ra_sta_update(rtwdev, sta, csi);
517e3ec7017SPing-Ke Shih 
518e3ec7017SPing-Ke Shih 	if (rssi > 40)
519e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 1;
520e3ec7017SPing-Ke Shih 	else if (rssi > 20)
521e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 2;
522e3ec7017SPing-Ke Shih 	else if (rssi > 1)
523e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 3;
524e3ec7017SPing-Ke Shih 	else
525e3ec7017SPing-Ke Shih 		ra->init_rate_lv = 0;
526e3ec7017SPing-Ke Shih 	ra->upd_all = 1;
527e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
528e3ec7017SPing-Ke Shih 		    "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d",
529e3ec7017SPing-Ke Shih 		    ra->macid,
530e3ec7017SPing-Ke Shih 		    ra->mode_ctrl,
531e3ec7017SPing-Ke Shih 		    ra->bw_cap,
532e3ec7017SPing-Ke Shih 		    ra->ss_num,
533e3ec7017SPing-Ke Shih 		    ra->init_rate_lv);
534e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RA,
535e3ec7017SPing-Ke Shih 		    "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d",
536e3ec7017SPing-Ke Shih 		    ra->dcm_cap,
537e3ec7017SPing-Ke Shih 		    ra->er_cap,
538e3ec7017SPing-Ke Shih 		    ra->ldpc_cap,
539e3ec7017SPing-Ke Shih 		    ra->stbc_cap,
540e3ec7017SPing-Ke Shih 		    ra->en_sgi,
541e3ec7017SPing-Ke Shih 		    ra->giltf);
542e3ec7017SPing-Ke Shih 
543e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_ra(rtwdev, ra, csi);
544e3ec7017SPing-Ke Shih }
545e3ec7017SPing-Ke Shih 
546e3ec7017SPing-Ke Shih u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev,
5473e5831caSZong-Zhe Yang 		      const struct rtw89_chan *chan,
548e3ec7017SPing-Ke Shih 		      enum rtw89_bandwidth dbw)
549e3ec7017SPing-Ke Shih {
5503e5831caSZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
5513e5831caSZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
5523e5831caSZong-Zhe Yang 	u8 central_ch = chan->channel;
553e3ec7017SPing-Ke Shih 	u8 txsc_idx = 0;
554e3ec7017SPing-Ke Shih 	u8 tmp = 0;
555e3ec7017SPing-Ke Shih 
556e3ec7017SPing-Ke Shih 	if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20)
557e3ec7017SPing-Ke Shih 		return txsc_idx;
558e3ec7017SPing-Ke Shih 
559e3ec7017SPing-Ke Shih 	switch (cbw) {
560e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
561e3ec7017SPing-Ke Shih 		txsc_idx = pri_ch > central_ch ? 1 : 2;
562e3ec7017SPing-Ke Shih 		break;
563e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
564e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
565e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
566e3ec7017SPing-Ke Shih 				txsc_idx = (pri_ch - central_ch) >> 1;
567e3ec7017SPing-Ke Shih 			else
568e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 1;
569e3ec7017SPing-Ke Shih 		} else {
570e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 9 : 10;
571e3ec7017SPing-Ke Shih 		}
572e3ec7017SPing-Ke Shih 		break;
573e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
574e3ec7017SPing-Ke Shih 		if (pri_ch > central_ch)
575e3ec7017SPing-Ke Shih 			tmp = (pri_ch - central_ch) >> 1;
576e3ec7017SPing-Ke Shih 		else
577e3ec7017SPing-Ke Shih 			tmp = ((central_ch - pri_ch) >> 1) + 1;
578e3ec7017SPing-Ke Shih 
579e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
580e3ec7017SPing-Ke Shih 			txsc_idx = tmp;
581e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
582e3ec7017SPing-Ke Shih 			if (tmp == 1 || tmp == 3)
583e3ec7017SPing-Ke Shih 				txsc_idx = 9;
584e3ec7017SPing-Ke Shih 			else if (tmp == 5 || tmp == 7)
585e3ec7017SPing-Ke Shih 				txsc_idx = 11;
586e3ec7017SPing-Ke Shih 			else if (tmp == 2 || tmp == 4)
587e3ec7017SPing-Ke Shih 				txsc_idx = 10;
588e3ec7017SPing-Ke Shih 			else if (tmp == 6 || tmp == 8)
589e3ec7017SPing-Ke Shih 				txsc_idx = 12;
590e3ec7017SPing-Ke Shih 			else
591e3ec7017SPing-Ke Shih 				return 0xff;
592e3ec7017SPing-Ke Shih 		} else {
593e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 13 : 14;
594e3ec7017SPing-Ke Shih 		}
595e3ec7017SPing-Ke Shih 		break;
596e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80_80:
597e3ec7017SPing-Ke Shih 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
598e3ec7017SPing-Ke Shih 			if (pri_ch > central_ch)
599e3ec7017SPing-Ke Shih 				txsc_idx = (10 - (pri_ch - central_ch)) >> 1;
600e3ec7017SPing-Ke Shih 			else
601e3ec7017SPing-Ke Shih 				txsc_idx = ((central_ch - pri_ch) >> 1) + 5;
602e3ec7017SPing-Ke Shih 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
603e3ec7017SPing-Ke Shih 			txsc_idx = pri_ch > central_ch ? 10 : 12;
604e3ec7017SPing-Ke Shih 		} else {
605e3ec7017SPing-Ke Shih 			txsc_idx = 14;
606e3ec7017SPing-Ke Shih 		}
607e3ec7017SPing-Ke Shih 		break;
608e3ec7017SPing-Ke Shih 	default:
609e3ec7017SPing-Ke Shih 		break;
610e3ec7017SPing-Ke Shih 	}
611e3ec7017SPing-Ke Shih 
612e3ec7017SPing-Ke Shih 	return txsc_idx;
613e3ec7017SPing-Ke Shih }
614861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_get_txsc);
615e3ec7017SPing-Ke Shih 
61684d0e33eSChung-Hsuan Hung static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev)
61784d0e33eSChung-Hsuan Hung {
61884d0e33eSChung-Hsuan Hung 	return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) ||
61984d0e33eSChung-Hsuan Hung 	       !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1);
62084d0e33eSChung-Hsuan Hung }
62184d0e33eSChung-Hsuan Hung 
622e3ec7017SPing-Ke Shih u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
623e3ec7017SPing-Ke Shih 		      u32 addr, u32 mask)
624e3ec7017SPing-Ke Shih {
625e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
626e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
627e3ec7017SPing-Ke Shih 	u32 val, direct_addr;
628e3ec7017SPing-Ke Shih 
629e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
630e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
631e3ec7017SPing-Ke Shih 		return INV_RF_DATA;
632e3ec7017SPing-Ke Shih 	}
633e3ec7017SPing-Ke Shih 
634e3ec7017SPing-Ke Shih 	addr &= 0xff;
635e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
636e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
637e3ec7017SPing-Ke Shih 
638e3ec7017SPing-Ke Shih 	val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask);
639e3ec7017SPing-Ke Shih 
640e3ec7017SPing-Ke Shih 	return val;
641e3ec7017SPing-Ke Shih }
642e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_read_rf);
643e3ec7017SPing-Ke Shih 
64484d0e33eSChung-Hsuan Hung static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev,
64584d0e33eSChung-Hsuan Hung 			       enum rtw89_rf_path rf_path, u32 addr, u32 mask)
64684d0e33eSChung-Hsuan Hung {
64784d0e33eSChung-Hsuan Hung 	bool busy;
64884d0e33eSChung-Hsuan Hung 	bool done;
64984d0e33eSChung-Hsuan Hung 	u32 val;
65084d0e33eSChung-Hsuan Hung 	int ret;
65184d0e33eSChung-Hsuan Hung 
65284d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
65384d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
65484d0e33eSChung-Hsuan Hung 	if (ret) {
65584d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read rf busy swsi\n");
65684d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
65784d0e33eSChung-Hsuan Hung 	}
65884d0e33eSChung-Hsuan Hung 
65984d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
66084d0e33eSChung-Hsuan Hung 
66184d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) |
66284d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr);
66384d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val);
66484d0e33eSChung-Hsuan Hung 	udelay(2);
66584d0e33eSChung-Hsuan Hung 
66684d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1,
66784d0e33eSChung-Hsuan Hung 				       30, false, rtwdev, R_SWSI_V1,
66884d0e33eSChung-Hsuan Hung 				       B_SWSI_R_DATA_DONE_V1);
66984d0e33eSChung-Hsuan Hung 	if (ret) {
67084d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "read swsi busy\n");
67184d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
67284d0e33eSChung-Hsuan Hung 	}
67384d0e33eSChung-Hsuan Hung 
67484d0e33eSChung-Hsuan Hung 	return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask);
67584d0e33eSChung-Hsuan Hung }
67684d0e33eSChung-Hsuan Hung 
67784d0e33eSChung-Hsuan Hung u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
67884d0e33eSChung-Hsuan Hung 			 u32 addr, u32 mask)
67984d0e33eSChung-Hsuan Hung {
68084d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
68184d0e33eSChung-Hsuan Hung 
68284d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
68384d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
68484d0e33eSChung-Hsuan Hung 		return INV_RF_DATA;
68584d0e33eSChung-Hsuan Hung 	}
68684d0e33eSChung-Hsuan Hung 
68784d0e33eSChung-Hsuan Hung 	if (ad_sel)
68884d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask);
68984d0e33eSChung-Hsuan Hung 	else
69084d0e33eSChung-Hsuan Hung 		return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask);
69184d0e33eSChung-Hsuan Hung }
69284d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_read_rf_v1);
69384d0e33eSChung-Hsuan Hung 
694e3ec7017SPing-Ke Shih bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
695e3ec7017SPing-Ke Shih 			u32 addr, u32 mask, u32 data)
696e3ec7017SPing-Ke Shih {
697e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
698e3ec7017SPing-Ke Shih 	const u32 *base_addr = chip->rf_base_addr;
699e3ec7017SPing-Ke Shih 	u32 direct_addr;
700e3ec7017SPing-Ke Shih 
701e3ec7017SPing-Ke Shih 	if (rf_path >= rtwdev->chip->rf_path_num) {
702e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
703e3ec7017SPing-Ke Shih 		return false;
704e3ec7017SPing-Ke Shih 	}
705e3ec7017SPing-Ke Shih 
706e3ec7017SPing-Ke Shih 	addr &= 0xff;
707e3ec7017SPing-Ke Shih 	direct_addr = base_addr[rf_path] + (addr << 2);
708e3ec7017SPing-Ke Shih 	mask &= RFREG_MASK;
709e3ec7017SPing-Ke Shih 
710e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data);
711e3ec7017SPing-Ke Shih 
712e3ec7017SPing-Ke Shih 	/* delay to ensure writing properly */
713e3ec7017SPing-Ke Shih 	udelay(1);
714e3ec7017SPing-Ke Shih 
715e3ec7017SPing-Ke Shih 	return true;
716e3ec7017SPing-Ke Shih }
717e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_write_rf);
718e3ec7017SPing-Ke Shih 
71984d0e33eSChung-Hsuan Hung static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev,
72084d0e33eSChung-Hsuan Hung 				 enum rtw89_rf_path rf_path, u32 addr, u32 mask,
72184d0e33eSChung-Hsuan Hung 				 u32 data)
72284d0e33eSChung-Hsuan Hung {
72384d0e33eSChung-Hsuan Hung 	u8 bit_shift;
72484d0e33eSChung-Hsuan Hung 	u32 val;
72584d0e33eSChung-Hsuan Hung 	bool busy, b_msk_en = false;
72684d0e33eSChung-Hsuan Hung 	int ret;
72784d0e33eSChung-Hsuan Hung 
72884d0e33eSChung-Hsuan Hung 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
72984d0e33eSChung-Hsuan Hung 				       1, 30, false, rtwdev);
73084d0e33eSChung-Hsuan Hung 	if (ret) {
73184d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "write rf busy swsi\n");
73284d0e33eSChung-Hsuan Hung 		return false;
73384d0e33eSChung-Hsuan Hung 	}
73484d0e33eSChung-Hsuan Hung 
73584d0e33eSChung-Hsuan Hung 	data &= RFREG_MASK;
73684d0e33eSChung-Hsuan Hung 	mask &= RFREG_MASK;
73784d0e33eSChung-Hsuan Hung 
73884d0e33eSChung-Hsuan Hung 	if (mask != RFREG_MASK) {
73984d0e33eSChung-Hsuan Hung 		b_msk_en = true;
74084d0e33eSChung-Hsuan Hung 		rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK,
74184d0e33eSChung-Hsuan Hung 				       mask);
74284d0e33eSChung-Hsuan Hung 		bit_shift = __ffs(mask);
74384d0e33eSChung-Hsuan Hung 		data = (data << bit_shift) & RFREG_MASK;
74484d0e33eSChung-Hsuan Hung 	}
74584d0e33eSChung-Hsuan Hung 
74684d0e33eSChung-Hsuan Hung 	val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) |
74784d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) |
74884d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) |
74984d0e33eSChung-Hsuan Hung 	      FIELD_PREP(B_SWSI_DATA_VAL_V1, data);
75084d0e33eSChung-Hsuan Hung 
75184d0e33eSChung-Hsuan Hung 	rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val);
75284d0e33eSChung-Hsuan Hung 
75384d0e33eSChung-Hsuan Hung 	return true;
75484d0e33eSChung-Hsuan Hung }
75584d0e33eSChung-Hsuan Hung 
75684d0e33eSChung-Hsuan Hung bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
75784d0e33eSChung-Hsuan Hung 			   u32 addr, u32 mask, u32 data)
75884d0e33eSChung-Hsuan Hung {
75984d0e33eSChung-Hsuan Hung 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
76084d0e33eSChung-Hsuan Hung 
76184d0e33eSChung-Hsuan Hung 	if (rf_path >= rtwdev->chip->rf_path_num) {
76284d0e33eSChung-Hsuan Hung 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
76384d0e33eSChung-Hsuan Hung 		return false;
76484d0e33eSChung-Hsuan Hung 	}
76584d0e33eSChung-Hsuan Hung 
76684d0e33eSChung-Hsuan Hung 	if (ad_sel)
76784d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data);
76884d0e33eSChung-Hsuan Hung 	else
76984d0e33eSChung-Hsuan Hung 		return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data);
77084d0e33eSChung-Hsuan Hung }
77184d0e33eSChung-Hsuan Hung EXPORT_SYMBOL(rtw89_phy_write_rf_v1);
77284d0e33eSChung-Hsuan Hung 
773e3ec7017SPing-Ke Shih static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev,
774e3ec7017SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx)
775e3ec7017SPing-Ke Shih {
776e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
777e3ec7017SPing-Ke Shih 
778e3ec7017SPing-Ke Shih 	chip->ops->bb_reset(rtwdev, phy_idx);
779e3ec7017SPing-Ke Shih }
780e3ec7017SPing-Ke Shih 
781e3ec7017SPing-Ke Shih static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev,
782e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
783e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
784e3ec7017SPing-Ke Shih 				    void *extra_data)
785e3ec7017SPing-Ke Shih {
786e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe)
787e3ec7017SPing-Ke Shih 		mdelay(50);
788e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfd)
789e3ec7017SPing-Ke Shih 		mdelay(5);
790e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfc)
791e3ec7017SPing-Ke Shih 		mdelay(1);
792e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfb)
793e3ec7017SPing-Ke Shih 		udelay(50);
794e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xfa)
795e3ec7017SPing-Ke Shih 		udelay(5);
796e3ec7017SPing-Ke Shih 	else if (reg->addr == 0xf9)
797e3ec7017SPing-Ke Shih 		udelay(1);
798e3ec7017SPing-Ke Shih 	else
799e3ec7017SPing-Ke Shih 		rtw89_phy_write32(rtwdev, reg->addr, reg->data);
800e3ec7017SPing-Ke Shih }
801e3ec7017SPing-Ke Shih 
802e885871eSZong-Zhe Yang union rtw89_phy_bb_gain_arg {
803e885871eSZong-Zhe Yang 	u32 addr;
804e885871eSZong-Zhe Yang 	struct {
805e885871eSZong-Zhe Yang 		union {
806e885871eSZong-Zhe Yang 			u8 type;
807e885871eSZong-Zhe Yang 			struct {
808e885871eSZong-Zhe Yang 				u8 rxsc_start:4;
809e885871eSZong-Zhe Yang 				u8 bw:4;
810e885871eSZong-Zhe Yang 			};
811e885871eSZong-Zhe Yang 		};
812e885871eSZong-Zhe Yang 		u8 path;
813e885871eSZong-Zhe Yang 		u8 gain_band;
814e885871eSZong-Zhe Yang 		u8 cfg_type;
815e885871eSZong-Zhe Yang 	};
816e885871eSZong-Zhe Yang } __packed;
817e885871eSZong-Zhe Yang 
818e885871eSZong-Zhe Yang static void
819e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev,
820e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
821e885871eSZong-Zhe Yang {
822e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
823e885871eSZong-Zhe Yang 	u8 type = arg.type;
824e885871eSZong-Zhe Yang 	u8 path = arg.path;
825e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
826e885871eSZong-Zhe Yang 	int i;
827e885871eSZong-Zhe Yang 
828e885871eSZong-Zhe Yang 	switch (type) {
829e885871eSZong-Zhe Yang 	case 0:
830e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
831e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
832e885871eSZong-Zhe Yang 		break;
833e885871eSZong-Zhe Yang 	case 1:
834e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
835e885871eSZong-Zhe Yang 			gain->lna_gain[gband][path][i] = data & 0xff;
836e885871eSZong-Zhe Yang 		break;
837e885871eSZong-Zhe Yang 	case 2:
838e885871eSZong-Zhe Yang 		for (i = 0; i < 2; i++, data >>= 8)
839e885871eSZong-Zhe Yang 			gain->tia_gain[gband][path][i] = data & 0xff;
840e885871eSZong-Zhe Yang 		break;
841e885871eSZong-Zhe Yang 	default:
842e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
843e885871eSZong-Zhe Yang 			   "bb gain error {0x%x:0x%x} with unknown type: %d\n",
844e885871eSZong-Zhe Yang 			   arg.addr, data, type);
845e885871eSZong-Zhe Yang 		break;
846e885871eSZong-Zhe Yang 	}
847e885871eSZong-Zhe Yang }
848e885871eSZong-Zhe Yang 
849e885871eSZong-Zhe Yang enum rtw89_phy_bb_rxsc_start_idx {
850e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_FULL = 0,
851e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20 = 1,
852e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_20_1 = 5,
853e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_40 = 9,
854e885871eSZong-Zhe Yang 	RTW89_BB_RXSC_START_IDX_80 = 13,
855e885871eSZong-Zhe Yang };
856e885871eSZong-Zhe Yang 
857e885871eSZong-Zhe Yang static void
858e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev,
859e885871eSZong-Zhe Yang 			  union rtw89_phy_bb_gain_arg arg, u32 data)
860e885871eSZong-Zhe Yang {
861e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
862e885871eSZong-Zhe Yang 	u8 rxsc_start = arg.rxsc_start;
863e885871eSZong-Zhe Yang 	u8 bw = arg.bw;
864e885871eSZong-Zhe Yang 	u8 path = arg.path;
865e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
866e885871eSZong-Zhe Yang 	u8 rxsc;
867e885871eSZong-Zhe Yang 	s8 ofst;
868e885871eSZong-Zhe Yang 	int i;
869e885871eSZong-Zhe Yang 
870e885871eSZong-Zhe Yang 	switch (bw) {
871e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_20:
872e885871eSZong-Zhe Yang 		gain->rpl_ofst_20[gband][path] = (s8)data;
873e885871eSZong-Zhe Yang 		break;
874e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_40:
875e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
876e885871eSZong-Zhe Yang 			gain->rpl_ofst_40[gband][path][0] = (s8)data;
877e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
878e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
879e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
880e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
881e885871eSZong-Zhe Yang 				gain->rpl_ofst_40[gband][path][rxsc] = ofst;
882e885871eSZong-Zhe Yang 			}
883e885871eSZong-Zhe Yang 		}
884e885871eSZong-Zhe Yang 		break;
885e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_80:
886e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
887e885871eSZong-Zhe Yang 			gain->rpl_ofst_80[gband][path][0] = (s8)data;
888e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
889e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
890e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
891e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
892e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
893e885871eSZong-Zhe Yang 			}
894e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
895e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
896e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
897e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
898e885871eSZong-Zhe Yang 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
899e885871eSZong-Zhe Yang 			}
900e885871eSZong-Zhe Yang 		}
901e885871eSZong-Zhe Yang 		break;
902e885871eSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
903e885871eSZong-Zhe Yang 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
904e885871eSZong-Zhe Yang 			gain->rpl_ofst_160[gband][path][0] = (s8)data;
905e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
906e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
907e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
908e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
909e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
910e885871eSZong-Zhe Yang 			}
911e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) {
912e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
913e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i;
914e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
915e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
916e885871eSZong-Zhe Yang 			}
917e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
918e885871eSZong-Zhe Yang 			for (i = 0; i < 4; i++, data >>= 8) {
919e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
920e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
921e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
922e885871eSZong-Zhe Yang 			}
923e885871eSZong-Zhe Yang 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) {
924e885871eSZong-Zhe Yang 			for (i = 0; i < 2; i++, data >>= 8) {
925e885871eSZong-Zhe Yang 				rxsc = RTW89_BB_RXSC_START_IDX_80 + i;
926e885871eSZong-Zhe Yang 				ofst = (s8)(data & 0xff);
927e885871eSZong-Zhe Yang 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
928e885871eSZong-Zhe Yang 			}
929e885871eSZong-Zhe Yang 		}
930e885871eSZong-Zhe Yang 		break;
931e885871eSZong-Zhe Yang 	default:
932e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
933e885871eSZong-Zhe Yang 			   "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n",
934e885871eSZong-Zhe Yang 			   arg.addr, data, bw);
935e885871eSZong-Zhe Yang 		break;
936e885871eSZong-Zhe Yang 	}
937e885871eSZong-Zhe Yang }
938e885871eSZong-Zhe Yang 
939e885871eSZong-Zhe Yang static void
940e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev,
941e885871eSZong-Zhe Yang 			     union rtw89_phy_bb_gain_arg arg, u32 data)
942e885871eSZong-Zhe Yang {
943e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
944e885871eSZong-Zhe Yang 	u8 type = arg.type;
945e885871eSZong-Zhe Yang 	u8 path = arg.path;
946e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
947e885871eSZong-Zhe Yang 	int i;
948e885871eSZong-Zhe Yang 
949e885871eSZong-Zhe Yang 	switch (type) {
950e885871eSZong-Zhe Yang 	case 0:
951e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
952e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
953e885871eSZong-Zhe Yang 		break;
954e885871eSZong-Zhe Yang 	case 1:
955e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
956e885871eSZong-Zhe Yang 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
957e885871eSZong-Zhe Yang 		break;
958e885871eSZong-Zhe Yang 	default:
959e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
960e885871eSZong-Zhe Yang 			   "bb gain bypass {0x%x:0x%x} with unknown type: %d\n",
961e885871eSZong-Zhe Yang 			   arg.addr, data, type);
962e885871eSZong-Zhe Yang 		break;
963e885871eSZong-Zhe Yang 	}
964e885871eSZong-Zhe Yang }
965e885871eSZong-Zhe Yang 
966e885871eSZong-Zhe Yang static void
967e885871eSZong-Zhe Yang rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev,
968e885871eSZong-Zhe Yang 			    union rtw89_phy_bb_gain_arg arg, u32 data)
969e885871eSZong-Zhe Yang {
970e885871eSZong-Zhe Yang 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
971e885871eSZong-Zhe Yang 	u8 type = arg.type;
972e885871eSZong-Zhe Yang 	u8 path = arg.path;
973e885871eSZong-Zhe Yang 	u8 gband = arg.gain_band;
974e885871eSZong-Zhe Yang 	int i;
975e885871eSZong-Zhe Yang 
976e885871eSZong-Zhe Yang 	switch (type) {
977e885871eSZong-Zhe Yang 	case 0:
978e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
979e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
980e885871eSZong-Zhe Yang 		break;
981e885871eSZong-Zhe Yang 	case 1:
982e885871eSZong-Zhe Yang 		for (i = 4; i < 7; i++, data >>= 8)
983e885871eSZong-Zhe Yang 			gain->lna_op1db[gband][path][i] = data & 0xff;
984e885871eSZong-Zhe Yang 		break;
985e885871eSZong-Zhe Yang 	case 2:
986e885871eSZong-Zhe Yang 		for (i = 0; i < 4; i++, data >>= 8)
987e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
988e885871eSZong-Zhe Yang 		break;
989e885871eSZong-Zhe Yang 	case 3:
990e885871eSZong-Zhe Yang 		for (i = 4; i < 8; i++, data >>= 8)
991e885871eSZong-Zhe Yang 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
992e885871eSZong-Zhe Yang 		break;
993e885871eSZong-Zhe Yang 	default:
994e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
995e885871eSZong-Zhe Yang 			   "bb gain op1db {0x%x:0x%x} with unknown type: %d\n",
996e885871eSZong-Zhe Yang 			   arg.addr, data, type);
997e885871eSZong-Zhe Yang 		break;
998e885871eSZong-Zhe Yang 	}
999e885871eSZong-Zhe Yang }
1000e885871eSZong-Zhe Yang 
1001e885871eSZong-Zhe Yang static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev,
1002e885871eSZong-Zhe Yang 				     const struct rtw89_reg2_def *reg,
1003e885871eSZong-Zhe Yang 				     enum rtw89_rf_path rf_path,
1004e885871eSZong-Zhe Yang 				     void *extra_data)
1005e885871eSZong-Zhe Yang {
1006e885871eSZong-Zhe Yang 	const struct rtw89_chip_info *chip = rtwdev->chip;
1007e885871eSZong-Zhe Yang 	union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr };
1008e885871eSZong-Zhe Yang 
1009e885871eSZong-Zhe Yang 	if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR)
1010e885871eSZong-Zhe Yang 		return;
1011e885871eSZong-Zhe Yang 
1012e885871eSZong-Zhe Yang 	if (arg.path >= chip->rf_path_num)
1013e885871eSZong-Zhe Yang 		return;
1014e885871eSZong-Zhe Yang 
1015e885871eSZong-Zhe Yang 	if (arg.addr >= 0xf9 && arg.addr <= 0xfe) {
1016e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev, "bb gain table with flow ctrl\n");
1017e885871eSZong-Zhe Yang 		return;
1018e885871eSZong-Zhe Yang 	}
1019e885871eSZong-Zhe Yang 
1020e885871eSZong-Zhe Yang 	switch (arg.cfg_type) {
1021e885871eSZong-Zhe Yang 	case 0:
1022e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data);
1023e885871eSZong-Zhe Yang 		break;
1024e885871eSZong-Zhe Yang 	case 1:
1025e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data);
1026e885871eSZong-Zhe Yang 		break;
1027e885871eSZong-Zhe Yang 	case 2:
1028e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data);
1029e885871eSZong-Zhe Yang 		break;
1030e885871eSZong-Zhe Yang 	case 3:
1031e885871eSZong-Zhe Yang 		rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data);
1032e885871eSZong-Zhe Yang 		break;
1033e885871eSZong-Zhe Yang 	default:
1034e885871eSZong-Zhe Yang 		rtw89_warn(rtwdev,
1035e885871eSZong-Zhe Yang 			   "bb gain {0x%x:0x%x} with unknown cfg type: %d\n",
1036e885871eSZong-Zhe Yang 			   arg.addr, reg->data, arg.cfg_type);
1037e885871eSZong-Zhe Yang 		break;
1038e885871eSZong-Zhe Yang 	}
1039e885871eSZong-Zhe Yang }
1040e885871eSZong-Zhe Yang 
1041e3ec7017SPing-Ke Shih static void
1042e3ec7017SPing-Ke Shih rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev,
1043e3ec7017SPing-Ke Shih 			     const struct rtw89_reg2_def *reg,
1044e3ec7017SPing-Ke Shih 			     enum rtw89_rf_path rf_path,
1045e3ec7017SPing-Ke Shih 			     struct rtw89_fw_h2c_rf_reg_info *info)
1046e3ec7017SPing-Ke Shih {
1047e3ec7017SPing-Ke Shih 	u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE;
1048e3ec7017SPing-Ke Shih 	u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE;
1049e3ec7017SPing-Ke Shih 
105030101812SPing-Ke Shih 	if (page >= RTW89_H2C_RF_PAGE_NUM) {
105130101812SPing-Ke Shih 		rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d",
105230101812SPing-Ke Shih 			   rf_path, info->curr_idx);
105330101812SPing-Ke Shih 		return;
105430101812SPing-Ke Shih 	}
105530101812SPing-Ke Shih 
1056e3ec7017SPing-Ke Shih 	info->rtw89_phy_config_rf_h2c[page][idx] =
1057e3ec7017SPing-Ke Shih 		cpu_to_le32((reg->addr << 20) | reg->data);
1058e3ec7017SPing-Ke Shih 	info->curr_idx++;
1059e3ec7017SPing-Ke Shih }
1060e3ec7017SPing-Ke Shih 
1061e3ec7017SPing-Ke Shih static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev,
1062e3ec7017SPing-Ke Shih 				      struct rtw89_fw_h2c_rf_reg_info *info)
1063e3ec7017SPing-Ke Shih {
106430101812SPing-Ke Shih 	u16 remain = info->curr_idx;
106530101812SPing-Ke Shih 	u16 len = 0;
1066e3ec7017SPing-Ke Shih 	u8 i;
1067e3ec7017SPing-Ke Shih 	int ret = 0;
1068e3ec7017SPing-Ke Shih 
106930101812SPing-Ke Shih 	if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) {
1070e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev,
107130101812SPing-Ke Shih 			   "rf reg h2c total len %d larger than %d\n",
107230101812SPing-Ke Shih 			   remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE);
107330101812SPing-Ke Shih 		ret = -EINVAL;
107430101812SPing-Ke Shih 		goto out;
1075e3ec7017SPing-Ke Shih 	}
1076e3ec7017SPing-Ke Shih 
107730101812SPing-Ke Shih 	for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) {
107830101812SPing-Ke Shih 		len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain;
107930101812SPing-Ke Shih 		ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i);
1080e3ec7017SPing-Ke Shih 		if (ret)
108130101812SPing-Ke Shih 			goto out;
1082e3ec7017SPing-Ke Shih 	}
108330101812SPing-Ke Shih out:
1084e3ec7017SPing-Ke Shih 	info->curr_idx = 0;
1085e3ec7017SPing-Ke Shih 
108630101812SPing-Ke Shih 	return ret;
1087e3ec7017SPing-Ke Shih }
1088e3ec7017SPing-Ke Shih 
1089e3ec7017SPing-Ke Shih static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev,
1090e3ec7017SPing-Ke Shih 				    const struct rtw89_reg2_def *reg,
1091e3ec7017SPing-Ke Shih 				    enum rtw89_rf_path rf_path,
1092e3ec7017SPing-Ke Shih 				    void *extra_data)
1093e3ec7017SPing-Ke Shih {
1094e3ec7017SPing-Ke Shih 	if (reg->addr == 0xfe) {
1095e3ec7017SPing-Ke Shih 		mdelay(50);
1096e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfd) {
1097e3ec7017SPing-Ke Shih 		mdelay(5);
1098e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfc) {
1099e3ec7017SPing-Ke Shih 		mdelay(1);
1100e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfb) {
1101e3ec7017SPing-Ke Shih 		udelay(50);
1102e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xfa) {
1103e3ec7017SPing-Ke Shih 		udelay(5);
1104e3ec7017SPing-Ke Shih 	} else if (reg->addr == 0xf9) {
1105e3ec7017SPing-Ke Shih 		udelay(1);
1106e3ec7017SPing-Ke Shih 	} else {
1107e3ec7017SPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data);
1108e3ec7017SPing-Ke Shih 		rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1109e3ec7017SPing-Ke Shih 					     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1110e3ec7017SPing-Ke Shih 	}
1111e3ec7017SPing-Ke Shih }
1112e3ec7017SPing-Ke Shih 
11132a5f2b32SPing-Ke Shih void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev,
11142a5f2b32SPing-Ke Shih 				const struct rtw89_reg2_def *reg,
11152a5f2b32SPing-Ke Shih 				enum rtw89_rf_path rf_path,
11162a5f2b32SPing-Ke Shih 				void *extra_data)
11172a5f2b32SPing-Ke Shih {
11182a5f2b32SPing-Ke Shih 	rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data);
11192a5f2b32SPing-Ke Shih 
11202a5f2b32SPing-Ke Shih 	if (reg->addr < 0x100)
11212a5f2b32SPing-Ke Shih 		return;
11222a5f2b32SPing-Ke Shih 
11232a5f2b32SPing-Ke Shih 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
11242a5f2b32SPing-Ke Shih 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
11252a5f2b32SPing-Ke Shih }
11262a5f2b32SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1);
11272a5f2b32SPing-Ke Shih 
1128e3ec7017SPing-Ke Shih static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev,
1129e3ec7017SPing-Ke Shih 				  const struct rtw89_phy_table *table,
1130e3ec7017SPing-Ke Shih 				  u32 *headline_size, u32 *headline_idx,
1131e3ec7017SPing-Ke Shih 				  u8 rfe, u8 cv)
1132e3ec7017SPing-Ke Shih {
1133e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1134e3ec7017SPing-Ke Shih 	u32 headline;
1135e3ec7017SPing-Ke Shih 	u32 compare, target;
1136e3ec7017SPing-Ke Shih 	u8 rfe_para, cv_para;
1137e3ec7017SPing-Ke Shih 	u8 cv_max = 0;
1138e3ec7017SPing-Ke Shih 	bool case_matched = false;
1139e3ec7017SPing-Ke Shih 	u32 i;
1140e3ec7017SPing-Ke Shih 
1141e3ec7017SPing-Ke Shih 	for (i = 0; i < table->n_regs; i++) {
1142e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1143e3ec7017SPing-Ke Shih 		headline = get_phy_headline(reg->addr);
1144e3ec7017SPing-Ke Shih 		if (headline != PHY_HEADLINE_VALID)
1145e3ec7017SPing-Ke Shih 			break;
1146e3ec7017SPing-Ke Shih 	}
1147e3ec7017SPing-Ke Shih 	*headline_size = i;
1148e3ec7017SPing-Ke Shih 	if (*headline_size == 0)
1149e3ec7017SPing-Ke Shih 		return 0;
1150e3ec7017SPing-Ke Shih 
1151e3ec7017SPing-Ke Shih 	/* case 1: RFE match, CV match */
1152e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, cv);
1153e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1154e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1155e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1156e3ec7017SPing-Ke Shih 		if (target == compare) {
1157e3ec7017SPing-Ke Shih 			*headline_idx = i;
1158e3ec7017SPing-Ke Shih 			return 0;
1159e3ec7017SPing-Ke Shih 		}
1160e3ec7017SPing-Ke Shih 	}
1161e3ec7017SPing-Ke Shih 
1162e3ec7017SPing-Ke Shih 	/* case 2: RFE match, CV don't care */
1163e3ec7017SPing-Ke Shih 	compare = get_phy_compare(rfe, PHY_COND_DONT_CARE);
1164e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1165e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1166e3ec7017SPing-Ke Shih 		target = get_phy_target(reg->addr);
1167e3ec7017SPing-Ke Shih 		if (target == compare) {
1168e3ec7017SPing-Ke Shih 			*headline_idx = i;
1169e3ec7017SPing-Ke Shih 			return 0;
1170e3ec7017SPing-Ke Shih 		}
1171e3ec7017SPing-Ke Shih 	}
1172e3ec7017SPing-Ke Shih 
1173e3ec7017SPing-Ke Shih 	/* case 3: RFE match, CV max in table */
1174e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1175e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1176e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1177e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1178e3ec7017SPing-Ke Shih 		if (rfe_para == rfe) {
1179e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1180e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1181e3ec7017SPing-Ke Shih 				*headline_idx = i;
1182e3ec7017SPing-Ke Shih 				case_matched = true;
1183e3ec7017SPing-Ke Shih 			}
1184e3ec7017SPing-Ke Shih 		}
1185e3ec7017SPing-Ke Shih 	}
1186e3ec7017SPing-Ke Shih 
1187e3ec7017SPing-Ke Shih 	if (case_matched)
1188e3ec7017SPing-Ke Shih 		return 0;
1189e3ec7017SPing-Ke Shih 
1190e3ec7017SPing-Ke Shih 	/* case 4: RFE don't care, CV max in table */
1191e3ec7017SPing-Ke Shih 	for (i = 0; i < *headline_size; i++) {
1192e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1193e3ec7017SPing-Ke Shih 		rfe_para = get_phy_cond_rfe(reg->addr);
1194e3ec7017SPing-Ke Shih 		cv_para = get_phy_cond_cv(reg->addr);
1195e3ec7017SPing-Ke Shih 		if (rfe_para == PHY_COND_DONT_CARE) {
1196e3ec7017SPing-Ke Shih 			if (cv_para >= cv_max) {
1197e3ec7017SPing-Ke Shih 				cv_max = cv_para;
1198e3ec7017SPing-Ke Shih 				*headline_idx = i;
1199e3ec7017SPing-Ke Shih 				case_matched = true;
1200e3ec7017SPing-Ke Shih 			}
1201e3ec7017SPing-Ke Shih 		}
1202e3ec7017SPing-Ke Shih 	}
1203e3ec7017SPing-Ke Shih 
1204e3ec7017SPing-Ke Shih 	if (case_matched)
1205e3ec7017SPing-Ke Shih 		return 0;
1206e3ec7017SPing-Ke Shih 
1207e3ec7017SPing-Ke Shih 	return -EINVAL;
1208e3ec7017SPing-Ke Shih }
1209e3ec7017SPing-Ke Shih 
1210e3ec7017SPing-Ke Shih static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev,
1211e3ec7017SPing-Ke Shih 			       const struct rtw89_phy_table *table,
1212e3ec7017SPing-Ke Shih 			       void (*config)(struct rtw89_dev *rtwdev,
1213e3ec7017SPing-Ke Shih 					      const struct rtw89_reg2_def *reg,
1214e3ec7017SPing-Ke Shih 					      enum rtw89_rf_path rf_path,
1215e3ec7017SPing-Ke Shih 					      void *data),
1216e3ec7017SPing-Ke Shih 			       void *extra_data)
1217e3ec7017SPing-Ke Shih {
1218e3ec7017SPing-Ke Shih 	const struct rtw89_reg2_def *reg;
1219e3ec7017SPing-Ke Shih 	enum rtw89_rf_path rf_path = table->rf_path;
1220e3ec7017SPing-Ke Shih 	u8 rfe = rtwdev->efuse.rfe_type;
1221e3ec7017SPing-Ke Shih 	u8 cv = rtwdev->hal.cv;
1222e3ec7017SPing-Ke Shih 	u32 i;
1223e3ec7017SPing-Ke Shih 	u32 headline_size = 0, headline_idx = 0;
1224e3ec7017SPing-Ke Shih 	u32 target = 0, cfg_target;
1225e3ec7017SPing-Ke Shih 	u8 cond;
1226e3ec7017SPing-Ke Shih 	bool is_matched = true;
1227e3ec7017SPing-Ke Shih 	bool target_found = false;
1228e3ec7017SPing-Ke Shih 	int ret;
1229e3ec7017SPing-Ke Shih 
1230e3ec7017SPing-Ke Shih 	ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size,
1231e3ec7017SPing-Ke Shih 				     &headline_idx, rfe, cv);
1232e3ec7017SPing-Ke Shih 	if (ret) {
1233e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv);
1234e3ec7017SPing-Ke Shih 		return;
1235e3ec7017SPing-Ke Shih 	}
1236e3ec7017SPing-Ke Shih 
1237e3ec7017SPing-Ke Shih 	cfg_target = get_phy_target(table->regs[headline_idx].addr);
1238e3ec7017SPing-Ke Shih 	for (i = headline_size; i < table->n_regs; i++) {
1239e3ec7017SPing-Ke Shih 		reg = &table->regs[i];
1240e3ec7017SPing-Ke Shih 		cond = get_phy_cond(reg->addr);
1241e3ec7017SPing-Ke Shih 		switch (cond) {
1242e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_IF:
1243e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELIF:
1244e3ec7017SPing-Ke Shih 			target = get_phy_target(reg->addr);
1245e3ec7017SPing-Ke Shih 			break;
1246e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_ELSE:
1247e3ec7017SPing-Ke Shih 			is_matched = false;
1248e3ec7017SPing-Ke Shih 			if (!target_found) {
1249e3ec7017SPing-Ke Shih 				rtw89_warn(rtwdev, "failed to load CR %x/%x\n",
1250e3ec7017SPing-Ke Shih 					   reg->addr, reg->data);
1251e3ec7017SPing-Ke Shih 				return;
1252e3ec7017SPing-Ke Shih 			}
1253e3ec7017SPing-Ke Shih 			break;
1254e3ec7017SPing-Ke Shih 		case PHY_COND_BRANCH_END:
1255e3ec7017SPing-Ke Shih 			is_matched = true;
1256e3ec7017SPing-Ke Shih 			target_found = false;
1257e3ec7017SPing-Ke Shih 			break;
1258e3ec7017SPing-Ke Shih 		case PHY_COND_CHECK:
1259e3ec7017SPing-Ke Shih 			if (target_found) {
1260e3ec7017SPing-Ke Shih 				is_matched = false;
1261e3ec7017SPing-Ke Shih 				break;
1262e3ec7017SPing-Ke Shih 			}
1263e3ec7017SPing-Ke Shih 
1264e3ec7017SPing-Ke Shih 			if (target == cfg_target) {
1265e3ec7017SPing-Ke Shih 				is_matched = true;
1266e3ec7017SPing-Ke Shih 				target_found = true;
1267e3ec7017SPing-Ke Shih 			} else {
1268e3ec7017SPing-Ke Shih 				is_matched = false;
1269e3ec7017SPing-Ke Shih 				target_found = false;
1270e3ec7017SPing-Ke Shih 			}
1271e3ec7017SPing-Ke Shih 			break;
1272e3ec7017SPing-Ke Shih 		default:
1273e3ec7017SPing-Ke Shih 			if (is_matched)
1274e3ec7017SPing-Ke Shih 				config(rtwdev, reg, rf_path, extra_data);
1275e3ec7017SPing-Ke Shih 			break;
1276e3ec7017SPing-Ke Shih 		}
1277e3ec7017SPing-Ke Shih 	}
1278e3ec7017SPing-Ke Shih }
1279e3ec7017SPing-Ke Shih 
1280e3ec7017SPing-Ke Shih void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev)
1281e3ec7017SPing-Ke Shih {
1282e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1283e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *bb_table = chip->bb_table;
1284e885871eSZong-Zhe Yang 	const struct rtw89_phy_table *bb_gain_table = chip->bb_gain_table;
1285e3ec7017SPing-Ke Shih 
1286e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL);
1287e3ec7017SPing-Ke Shih 	rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0);
1288e885871eSZong-Zhe Yang 	if (bb_gain_table)
1289e885871eSZong-Zhe Yang 		rtw89_phy_init_reg(rtwdev, bb_gain_table,
1290e885871eSZong-Zhe Yang 				   rtw89_phy_config_bb_gain, NULL);
1291e3ec7017SPing-Ke Shih 	rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0);
1292e3ec7017SPing-Ke Shih }
1293e3ec7017SPing-Ke Shih 
1294e3ec7017SPing-Ke Shih static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev)
1295e3ec7017SPing-Ke Shih {
1296e3ec7017SPing-Ke Shih 	rtw89_phy_write32(rtwdev, 0x8080, 0x4);
1297e3ec7017SPing-Ke Shih 	udelay(1);
1298e3ec7017SPing-Ke Shih 	return rtw89_phy_read32(rtwdev, 0x8080);
1299e3ec7017SPing-Ke Shih }
1300e3ec7017SPing-Ke Shih 
1301e3ec7017SPing-Ke Shih void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev)
1302e3ec7017SPing-Ke Shih {
13032a5f2b32SPing-Ke Shih 	void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg,
13042a5f2b32SPing-Ke Shih 		       enum rtw89_rf_path rf_path, void *data);
1305e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1306e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *rf_table;
1307e3ec7017SPing-Ke Shih 	struct rtw89_fw_h2c_rf_reg_info *rf_reg_info;
1308e3ec7017SPing-Ke Shih 	u8 path;
1309e3ec7017SPing-Ke Shih 
1310e3ec7017SPing-Ke Shih 	rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL);
1311e3ec7017SPing-Ke Shih 	if (!rf_reg_info)
1312e3ec7017SPing-Ke Shih 		return;
1313e3ec7017SPing-Ke Shih 
1314e3ec7017SPing-Ke Shih 	for (path = RF_PATH_A; path < chip->rf_path_num; path++) {
1315e3ec7017SPing-Ke Shih 		rf_table = chip->rf_table[path];
13162a5f2b32SPing-Ke Shih 		rf_reg_info->rf_path = rf_table->rf_path;
13172a5f2b32SPing-Ke Shih 		config = rf_table->config ? rf_table->config : rtw89_phy_config_rf_reg;
13182a5f2b32SPing-Ke Shih 		rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info);
1319e3ec7017SPing-Ke Shih 		if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info))
1320e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n",
13212a5f2b32SPing-Ke Shih 				   rf_reg_info->rf_path);
1322e3ec7017SPing-Ke Shih 	}
1323e3ec7017SPing-Ke Shih 	kfree(rf_reg_info);
1324e3ec7017SPing-Ke Shih }
1325e3ec7017SPing-Ke Shih 
1326e3ec7017SPing-Ke Shih static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev)
1327e3ec7017SPing-Ke Shih {
1328e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1329e3ec7017SPing-Ke Shih 	const struct rtw89_phy_table *nctl_table;
1330e3ec7017SPing-Ke Shih 	u32 val;
1331e3ec7017SPing-Ke Shih 	int ret;
1332e3ec7017SPing-Ke Shih 
1333e3ec7017SPing-Ke Shih 	/* IQK/DPK clock & reset */
1334e3ec7017SPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, 0x0c60, 0x3);
1335e3ec7017SPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, 0x0c6c, 0x1);
1336e3ec7017SPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, 0x58ac, 0x8000000);
1337e3ec7017SPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, 0x78ac, 0x8000000);
1338e3ec7017SPing-Ke Shih 
1339e3ec7017SPing-Ke Shih 	/* check 0x8080 */
1340e3ec7017SPing-Ke Shih 	rtw89_phy_write32(rtwdev, 0x8000, 0x8);
1341e3ec7017SPing-Ke Shih 
1342e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10,
1343e3ec7017SPing-Ke Shih 				1000, false, rtwdev);
1344e3ec7017SPing-Ke Shih 	if (ret)
1345e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "failed to poll nctl block\n");
1346e3ec7017SPing-Ke Shih 
1347e3ec7017SPing-Ke Shih 	nctl_table = chip->nctl_table;
1348e3ec7017SPing-Ke Shih 	rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL);
1349e3ec7017SPing-Ke Shih }
1350e3ec7017SPing-Ke Shih 
1351e3ec7017SPing-Ke Shih static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr)
1352e3ec7017SPing-Ke Shih {
1353e3ec7017SPing-Ke Shih 	u32 phy_page = addr >> 8;
1354e3ec7017SPing-Ke Shih 	u32 ofst = 0;
1355e3ec7017SPing-Ke Shih 
1356e3ec7017SPing-Ke Shih 	switch (phy_page) {
1357e3ec7017SPing-Ke Shih 	case 0x6:
1358e3ec7017SPing-Ke Shih 	case 0x7:
1359e3ec7017SPing-Ke Shih 	case 0x8:
1360e3ec7017SPing-Ke Shih 	case 0x9:
1361e3ec7017SPing-Ke Shih 	case 0xa:
1362e3ec7017SPing-Ke Shih 	case 0xb:
1363e3ec7017SPing-Ke Shih 	case 0xc:
1364e3ec7017SPing-Ke Shih 	case 0xd:
1365e3ec7017SPing-Ke Shih 	case 0x19:
1366e3ec7017SPing-Ke Shih 	case 0x1a:
1367e3ec7017SPing-Ke Shih 	case 0x1b:
1368e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1369e3ec7017SPing-Ke Shih 		break;
1370e3ec7017SPing-Ke Shih 	default:
1371e3ec7017SPing-Ke Shih 		/* warning case */
1372e3ec7017SPing-Ke Shih 		ofst = 0;
1373e3ec7017SPing-Ke Shih 		break;
1374e3ec7017SPing-Ke Shih 	}
1375e3ec7017SPing-Ke Shih 
1376e3ec7017SPing-Ke Shih 	if (phy_page >= 0x40 && phy_page <= 0x4f)
1377e3ec7017SPing-Ke Shih 		ofst = 0x2000;
1378e3ec7017SPing-Ke Shih 
1379e3ec7017SPing-Ke Shih 	return ofst;
1380e3ec7017SPing-Ke Shih }
1381e3ec7017SPing-Ke Shih 
1382e3ec7017SPing-Ke Shih void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1383e3ec7017SPing-Ke Shih 			   u32 data, enum rtw89_phy_idx phy_idx)
1384e3ec7017SPing-Ke Shih {
1385e3ec7017SPing-Ke Shih 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1386e3ec7017SPing-Ke Shih 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1387e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, addr, mask, data);
1388e3ec7017SPing-Ke Shih }
1389861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write32_idx);
1390e3ec7017SPing-Ke Shih 
1391e3ec7017SPing-Ke Shih void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1392e3ec7017SPing-Ke Shih 			    u32 val)
1393e3ec7017SPing-Ke Shih {
1394e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0);
1395e3ec7017SPing-Ke Shih 
1396e3ec7017SPing-Ke Shih 	if (!rtwdev->dbcc_en)
1397e3ec7017SPing-Ke Shih 		return;
1398e3ec7017SPing-Ke Shih 
1399e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1);
1400e3ec7017SPing-Ke Shih }
1401e3ec7017SPing-Ke Shih 
1402e3ec7017SPing-Ke Shih void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev,
1403e3ec7017SPing-Ke Shih 			      const struct rtw89_phy_reg3_tbl *tbl)
1404e3ec7017SPing-Ke Shih {
1405e3ec7017SPing-Ke Shih 	const struct rtw89_reg3_def *reg3;
1406e3ec7017SPing-Ke Shih 	int i;
1407e3ec7017SPing-Ke Shih 
1408e3ec7017SPing-Ke Shih 	for (i = 0; i < tbl->size; i++) {
1409e3ec7017SPing-Ke Shih 		reg3 = &tbl->reg3[i];
1410e3ec7017SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data);
1411e3ec7017SPing-Ke Shih 	}
1412e3ec7017SPing-Ke Shih }
1413861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl);
1414e3ec7017SPing-Ke Shih 
1415e3ec7017SPing-Ke Shih const u8 rtw89_rs_idx_max[] = {
1416e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = RTW89_RATE_CCK_MAX,
1417e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = RTW89_RATE_OFDM_MAX,
1418e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = RTW89_RATE_MCS_MAX,
1419e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_MAX,
1420e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_MAX,
1421e3ec7017SPing-Ke Shih };
1422861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_rs_idx_max);
1423e3ec7017SPing-Ke Shih 
1424e3ec7017SPing-Ke Shih const u8 rtw89_rs_nss_max[] = {
1425e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = 1,
1426e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = 1,
1427e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = RTW89_NSS_MAX,
1428e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_MAX,
1429e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = 1,
1430e3ec7017SPing-Ke Shih };
1431861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_rs_nss_max);
1432e3ec7017SPing-Ke Shih 
1433e3ec7017SPing-Ke Shih static const u8 _byr_of_rs[] = {
1434e3ec7017SPing-Ke Shih 	[RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck),
1435e3ec7017SPing-Ke Shih 	[RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm),
1436e3ec7017SPing-Ke Shih 	[RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs),
1437e3ec7017SPing-Ke Shih 	[RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm),
1438e3ec7017SPing-Ke Shih 	[RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset),
1439e3ec7017SPing-Ke Shih };
1440e3ec7017SPing-Ke Shih 
1441e3ec7017SPing-Ke Shih #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs])
1442e3ec7017SPing-Ke Shih #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_max[rs] + (idx))
1443e3ec7017SPing-Ke Shih #define _byr_chk(rs, nss, idx) \
1444e3ec7017SPing-Ke Shih 	((nss) < rtw89_rs_nss_max[rs] && (idx) < rtw89_rs_idx_max[rs])
1445e3ec7017SPing-Ke Shih 
1446e3ec7017SPing-Ke Shih void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev,
1447e3ec7017SPing-Ke Shih 				 const struct rtw89_txpwr_table *tbl)
1448e3ec7017SPing-Ke Shih {
1449e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data;
1450e3ec7017SPing-Ke Shih 	const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size;
1451e3ec7017SPing-Ke Shih 	s8 *byr;
1452e3ec7017SPing-Ke Shih 	u32 data;
1453e3ec7017SPing-Ke Shih 	u8 i, idx;
1454e3ec7017SPing-Ke Shih 
1455e3ec7017SPing-Ke Shih 	for (; cfg < end; cfg++) {
1456e3ec7017SPing-Ke Shih 		byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]);
1457e3ec7017SPing-Ke Shih 		data = cfg->data;
1458e3ec7017SPing-Ke Shih 
1459e3ec7017SPing-Ke Shih 		for (i = 0; i < cfg->len; i++, data >>= 8) {
1460e3ec7017SPing-Ke Shih 			idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i));
1461e3ec7017SPing-Ke Shih 			byr[idx] = (s8)(data & 0xff);
1462e3ec7017SPing-Ke Shih 		}
1463e3ec7017SPing-Ke Shih 	}
1464e3ec7017SPing-Ke Shih }
1465861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate);
1466e3ec7017SPing-Ke Shih 
1467e3ec7017SPing-Ke Shih #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf)				\
1468e3ec7017SPing-Ke Shih ({									\
1469e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *__c = (rtwdev)->chip;		\
1470e3ec7017SPing-Ke Shih 	(txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac);	\
1471e3ec7017SPing-Ke Shih })
1472e3ec7017SPing-Ke Shih 
1473e3ec7017SPing-Ke Shih s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev,
1474e3ec7017SPing-Ke Shih 			       const struct rtw89_rate_desc *rate_desc)
1475e3ec7017SPing-Ke Shih {
1476*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1477*cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
1478e3ec7017SPing-Ke Shih 	s8 *byr;
1479e3ec7017SPing-Ke Shih 	u8 idx;
1480e3ec7017SPing-Ke Shih 
1481e3ec7017SPing-Ke Shih 	if (rate_desc->rs == RTW89_RS_CCK)
1482e3ec7017SPing-Ke Shih 		band = RTW89_BAND_2G;
1483e3ec7017SPing-Ke Shih 
1484e3ec7017SPing-Ke Shih 	if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) {
1485e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1486e3ec7017SPing-Ke Shih 			    "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n",
1487e3ec7017SPing-Ke Shih 			    rate_desc->rs, rate_desc->nss, rate_desc->idx);
1488e3ec7017SPing-Ke Shih 
1489e3ec7017SPing-Ke Shih 		return 0;
1490e3ec7017SPing-Ke Shih 	}
1491e3ec7017SPing-Ke Shih 
1492e3ec7017SPing-Ke Shih 	byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]);
1493e3ec7017SPing-Ke Shih 	idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx);
1494e3ec7017SPing-Ke Shih 
1495e3ec7017SPing-Ke Shih 	return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]);
1496e3ec7017SPing-Ke Shih }
1497861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_byrate);
1498e3ec7017SPing-Ke Shih 
1499ac74f016SZong-Zhe Yang static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g)
1500e3ec7017SPing-Ke Shih {
1501ac74f016SZong-Zhe Yang 	switch (channel_6g) {
1502ac74f016SZong-Zhe Yang 	case 1 ... 29:
1503ac74f016SZong-Zhe Yang 		return (channel_6g - 1) / 2;
1504ac74f016SZong-Zhe Yang 	case 33 ... 61:
1505ac74f016SZong-Zhe Yang 		return (channel_6g - 3) / 2;
1506ac74f016SZong-Zhe Yang 	case 65 ... 93:
1507ac74f016SZong-Zhe Yang 		return (channel_6g - 5) / 2;
1508ac74f016SZong-Zhe Yang 	case 97 ... 125:
1509ac74f016SZong-Zhe Yang 		return (channel_6g - 7) / 2;
1510ac74f016SZong-Zhe Yang 	case 129 ... 157:
1511ac74f016SZong-Zhe Yang 		return (channel_6g - 9) / 2;
1512ac74f016SZong-Zhe Yang 	case 161 ... 189:
1513ac74f016SZong-Zhe Yang 		return (channel_6g - 11) / 2;
1514ac74f016SZong-Zhe Yang 	case 193 ... 221:
1515ac74f016SZong-Zhe Yang 		return (channel_6g - 13) / 2;
1516ac74f016SZong-Zhe Yang 	case 225 ... 253:
1517ac74f016SZong-Zhe Yang 		return (channel_6g - 15) / 2;
1518ac74f016SZong-Zhe Yang 	default:
1519ac74f016SZong-Zhe Yang 		rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g);
1520ac74f016SZong-Zhe Yang 		return 0;
1521ac74f016SZong-Zhe Yang 	}
1522ac74f016SZong-Zhe Yang }
1523ac74f016SZong-Zhe Yang 
1524ac74f016SZong-Zhe Yang static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel)
1525ac74f016SZong-Zhe Yang {
1526ac74f016SZong-Zhe Yang 	if (band == RTW89_BAND_6G)
1527ac74f016SZong-Zhe Yang 		return rtw89_channel_6g_to_idx(rtwdev, channel);
1528ac74f016SZong-Zhe Yang 
1529e3ec7017SPing-Ke Shih 	switch (channel) {
1530e3ec7017SPing-Ke Shih 	case 1 ... 14:
1531e3ec7017SPing-Ke Shih 		return channel - 1;
1532e3ec7017SPing-Ke Shih 	case 36 ... 64:
1533e3ec7017SPing-Ke Shih 		return (channel - 36) / 2;
1534e3ec7017SPing-Ke Shih 	case 100 ... 144:
1535e3ec7017SPing-Ke Shih 		return ((channel - 100) / 2) + 15;
1536e3ec7017SPing-Ke Shih 	case 149 ... 177:
1537e3ec7017SPing-Ke Shih 		return ((channel - 149) / 2) + 38;
1538e3ec7017SPing-Ke Shih 	default:
1539e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown channel: %d\n", channel);
1540e3ec7017SPing-Ke Shih 		return 0;
1541e3ec7017SPing-Ke Shih 	}
1542e3ec7017SPing-Ke Shih }
1543e3ec7017SPing-Ke Shih 
1544e3ec7017SPing-Ke Shih s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev,
1545e3ec7017SPing-Ke Shih 			      u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch)
1546e3ec7017SPing-Ke Shih {
1547e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1548*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1549*cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
1550ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1551e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1552e3ec7017SPing-Ke Shih 	s8 lmt = 0, sar;
1553e3ec7017SPing-Ke Shih 
1554e3ec7017SPing-Ke Shih 	switch (band) {
1555e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
1556e3ec7017SPing-Ke Shih 		lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf][regd][ch_idx];
155754257714SZong-Zhe Yang 		if (!lmt)
155854257714SZong-Zhe Yang 			lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf]
155954257714SZong-Zhe Yang 						   [RTW89_WW][ch_idx];
1560e3ec7017SPing-Ke Shih 		break;
1561e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
1562e3ec7017SPing-Ke Shih 		lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf][regd][ch_idx];
156354257714SZong-Zhe Yang 		if (!lmt)
156454257714SZong-Zhe Yang 			lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf]
156554257714SZong-Zhe Yang 						   [RTW89_WW][ch_idx];
1566e3ec7017SPing-Ke Shih 		break;
1567ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
1568ac74f016SZong-Zhe Yang 		lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf][regd][ch_idx];
1569ac74f016SZong-Zhe Yang 		if (!lmt)
1570ac74f016SZong-Zhe Yang 			lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf]
1571ac74f016SZong-Zhe Yang 						   [RTW89_WW][ch_idx];
1572ac74f016SZong-Zhe Yang 		break;
1573e3ec7017SPing-Ke Shih 	default:
1574e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1575e3ec7017SPing-Ke Shih 		return 0;
1576e3ec7017SPing-Ke Shih 	}
1577e3ec7017SPing-Ke Shih 
1578e3ec7017SPing-Ke Shih 	lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt);
1579e3ec7017SPing-Ke Shih 	sar = rtw89_query_sar(rtwdev);
1580e3ec7017SPing-Ke Shih 
1581e3ec7017SPing-Ke Shih 	return min(lmt, sar);
1582e3ec7017SPing-Ke Shih }
1583861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit);
1584e3ec7017SPing-Ke Shih 
1585e3ec7017SPing-Ke Shih #define __fill_txpwr_limit_nonbf_bf(ptr, bw, ntx, rs, ch)		\
1586e3ec7017SPing-Ke Shih 	do {								\
1587e3ec7017SPing-Ke Shih 		u8 __i;							\
1588e3ec7017SPing-Ke Shih 		for (__i = 0; __i < RTW89_BF_NUM; __i++)		\
1589e3ec7017SPing-Ke Shih 			ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev,	\
1590e3ec7017SPing-Ke Shih 							      bw, ntx,	\
1591e3ec7017SPing-Ke Shih 							      rs, __i,	\
1592e3ec7017SPing-Ke Shih 							      (ch));	\
1593e3ec7017SPing-Ke Shih 	} while (0)
1594e3ec7017SPing-Ke Shih 
1595e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev,
1596e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
1597e3ec7017SPing-Ke Shih 					   u8 ntx, u8 ch)
1598e3ec7017SPing-Ke Shih {
1599e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, RTW89_CHANNEL_WIDTH_20,
1600e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
1601e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, RTW89_CHANNEL_WIDTH_40,
1602e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
1603e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20,
1604e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_OFDM, ch);
1605e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20,
1606e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1607e3ec7017SPing-Ke Shih }
1608e3ec7017SPing-Ke Shih 
1609e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev,
1610e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
161194b70cafSZong-Zhe Yang 					   u8 ntx, u8 ch, u8 pri_ch)
1612e3ec7017SPing-Ke Shih {
1613e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, RTW89_CHANNEL_WIDTH_20,
1614e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch - 2);
1615e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, RTW89_CHANNEL_WIDTH_40,
1616e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_CCK, ch);
1617e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20,
161894b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
1619e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20,
1620e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
1621e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20,
1622e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
1623e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40,
1624e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1625e3ec7017SPing-Ke Shih }
1626e3ec7017SPing-Ke Shih 
1627e3ec7017SPing-Ke Shih static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev,
1628e3ec7017SPing-Ke Shih 					   struct rtw89_txpwr_limit *lmt,
162994b70cafSZong-Zhe Yang 					   u8 ntx, u8 ch, u8 pri_ch)
1630e3ec7017SPing-Ke Shih {
1631e3ec7017SPing-Ke Shih 	s8 val_0p5_n[RTW89_BF_NUM];
1632e3ec7017SPing-Ke Shih 	s8 val_0p5_p[RTW89_BF_NUM];
1633e3ec7017SPing-Ke Shih 	u8 i;
1634e3ec7017SPing-Ke Shih 
1635e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20,
163694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
1637e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20,
1638e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 6);
1639e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20,
1640e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 2);
1641e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], RTW89_CHANNEL_WIDTH_20,
1642e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 2);
1643e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], RTW89_CHANNEL_WIDTH_20,
1644e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 6);
1645e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40,
1646e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
1647e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], RTW89_CHANNEL_WIDTH_40,
1648e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
1649e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], RTW89_CHANNEL_WIDTH_80,
1650e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch);
1651e3ec7017SPing-Ke Shih 
1652e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, RTW89_CHANNEL_WIDTH_40,
1653e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch - 4);
1654e3ec7017SPing-Ke Shih 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, RTW89_CHANNEL_WIDTH_40,
1655e3ec7017SPing-Ke Shih 				    ntx, RTW89_RS_MCS, ch + 4);
1656e3ec7017SPing-Ke Shih 
1657e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_BF_NUM; i++)
1658e3ec7017SPing-Ke Shih 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1659e3ec7017SPing-Ke Shih }
1660e3ec7017SPing-Ke Shih 
166194b70cafSZong-Zhe Yang static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev,
166294b70cafSZong-Zhe Yang 					    struct rtw89_txpwr_limit *lmt,
166394b70cafSZong-Zhe Yang 					    u8 ntx, u8 ch, u8 pri_ch)
166494b70cafSZong-Zhe Yang {
166594b70cafSZong-Zhe Yang 	s8 val_0p5_n[RTW89_BF_NUM];
166694b70cafSZong-Zhe Yang 	s8 val_0p5_p[RTW89_BF_NUM];
166794b70cafSZong-Zhe Yang 	s8 val_2p5_n[RTW89_BF_NUM];
166894b70cafSZong-Zhe Yang 	s8 val_2p5_p[RTW89_BF_NUM];
166994b70cafSZong-Zhe Yang 	u8 i;
167094b70cafSZong-Zhe Yang 
167194b70cafSZong-Zhe Yang 	/* fill ofdm section */
167294b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, RTW89_CHANNEL_WIDTH_20,
167394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_OFDM, pri_ch);
167494b70cafSZong-Zhe Yang 
167594b70cafSZong-Zhe Yang 	/* fill mcs 20m section */
167694b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], RTW89_CHANNEL_WIDTH_20,
167794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 14);
167894b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], RTW89_CHANNEL_WIDTH_20,
167994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 10);
168094b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], RTW89_CHANNEL_WIDTH_20,
168194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 6);
168294b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], RTW89_CHANNEL_WIDTH_20,
168394b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 2);
168494b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], RTW89_CHANNEL_WIDTH_20,
168594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 2);
168694b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], RTW89_CHANNEL_WIDTH_20,
168794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 6);
168894b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], RTW89_CHANNEL_WIDTH_20,
168994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 10);
169094b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], RTW89_CHANNEL_WIDTH_20,
169194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 14);
169294b70cafSZong-Zhe Yang 
169394b70cafSZong-Zhe Yang 	/* fill mcs 40m section */
169494b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], RTW89_CHANNEL_WIDTH_40,
169594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 12);
169694b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], RTW89_CHANNEL_WIDTH_40,
169794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
169894b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], RTW89_CHANNEL_WIDTH_40,
169994b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
170094b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], RTW89_CHANNEL_WIDTH_40,
170194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 12);
170294b70cafSZong-Zhe Yang 
170394b70cafSZong-Zhe Yang 	/* fill mcs 80m section */
170494b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], RTW89_CHANNEL_WIDTH_80,
170594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
170694b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], RTW89_CHANNEL_WIDTH_80,
170794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
170894b70cafSZong-Zhe Yang 
170994b70cafSZong-Zhe Yang 	/* fill mcs 160m section */
171094b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, RTW89_CHANNEL_WIDTH_160,
171194b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch);
171294b70cafSZong-Zhe Yang 
171394b70cafSZong-Zhe Yang 	/* fill mcs 40m 0p5 section */
171494b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, RTW89_CHANNEL_WIDTH_40,
171594b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 4);
171694b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, RTW89_CHANNEL_WIDTH_40,
171794b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 4);
171894b70cafSZong-Zhe Yang 
171994b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
172094b70cafSZong-Zhe Yang 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
172194b70cafSZong-Zhe Yang 
172294b70cafSZong-Zhe Yang 	/* fill mcs 40m 2p5 section */
172394b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_n, RTW89_CHANNEL_WIDTH_40,
172494b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch - 8);
172594b70cafSZong-Zhe Yang 	__fill_txpwr_limit_nonbf_bf(val_2p5_p, RTW89_CHANNEL_WIDTH_40,
172694b70cafSZong-Zhe Yang 				    ntx, RTW89_RS_MCS, ch + 8);
172794b70cafSZong-Zhe Yang 
172894b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_BF_NUM; i++)
172994b70cafSZong-Zhe Yang 		lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]);
173094b70cafSZong-Zhe Yang }
173194b70cafSZong-Zhe Yang 
1732e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev,
1733e3ec7017SPing-Ke Shih 				struct rtw89_txpwr_limit *lmt,
1734e3ec7017SPing-Ke Shih 				u8 ntx)
1735e3ec7017SPing-Ke Shih {
1736*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1737*cbb145b9SZong-Zhe Yang 	u8 pri_ch = chan->primary_channel;
1738*cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
1739*cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
1740e3ec7017SPing-Ke Shih 
1741e3ec7017SPing-Ke Shih 	memset(lmt, 0, sizeof(*lmt));
1742e3ec7017SPing-Ke Shih 
1743e3ec7017SPing-Ke Shih 	switch (bw) {
1744e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
1745e3ec7017SPing-Ke Shih 		rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, ntx, ch);
1746e3ec7017SPing-Ke Shih 		break;
1747e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
174894b70cafSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, ntx, ch, pri_ch);
1749e3ec7017SPing-Ke Shih 		break;
1750e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
175194b70cafSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, ntx, ch, pri_ch);
175294b70cafSZong-Zhe Yang 		break;
175394b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
175494b70cafSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, ntx, ch, pri_ch);
1755e3ec7017SPing-Ke Shih 		break;
1756e3ec7017SPing-Ke Shih 	}
1757e3ec7017SPing-Ke Shih }
1758861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_fill_txpwr_limit);
1759e3ec7017SPing-Ke Shih 
1760e3ec7017SPing-Ke Shih static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev,
1761e3ec7017SPing-Ke Shih 					u8 ru, u8 ntx, u8 ch)
1762e3ec7017SPing-Ke Shih {
1763e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1764*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1765*cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
1766ac74f016SZong-Zhe Yang 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1767e3ec7017SPing-Ke Shih 	u8 regd = rtw89_regd_get(rtwdev, band);
1768e3ec7017SPing-Ke Shih 	s8 lmt_ru = 0, sar;
1769e3ec7017SPing-Ke Shih 
1770e3ec7017SPing-Ke Shih 	switch (band) {
1771e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
1772e3ec7017SPing-Ke Shih 		lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx][regd][ch_idx];
177354257714SZong-Zhe Yang 		if (!lmt_ru)
177454257714SZong-Zhe Yang 			lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx]
177554257714SZong-Zhe Yang 							 [RTW89_WW][ch_idx];
1776e3ec7017SPing-Ke Shih 		break;
1777e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
1778e3ec7017SPing-Ke Shih 		lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx][regd][ch_idx];
177954257714SZong-Zhe Yang 		if (!lmt_ru)
178054257714SZong-Zhe Yang 			lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx]
178154257714SZong-Zhe Yang 							 [RTW89_WW][ch_idx];
1782e3ec7017SPing-Ke Shih 		break;
1783ac74f016SZong-Zhe Yang 	case RTW89_BAND_6G:
1784ac74f016SZong-Zhe Yang 		lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx][regd][ch_idx];
1785ac74f016SZong-Zhe Yang 		if (!lmt_ru)
1786ac74f016SZong-Zhe Yang 			lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx]
1787ac74f016SZong-Zhe Yang 							 [RTW89_WW][ch_idx];
1788ac74f016SZong-Zhe Yang 		break;
1789e3ec7017SPing-Ke Shih 	default:
1790e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1791e3ec7017SPing-Ke Shih 		return 0;
1792e3ec7017SPing-Ke Shih 	}
1793e3ec7017SPing-Ke Shih 
1794e3ec7017SPing-Ke Shih 	lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru);
1795e3ec7017SPing-Ke Shih 	sar = rtw89_query_sar(rtwdev);
1796e3ec7017SPing-Ke Shih 
1797e3ec7017SPing-Ke Shih 	return min(lmt_ru, sar);
1798e3ec7017SPing-Ke Shih }
1799e3ec7017SPing-Ke Shih 
1800e3ec7017SPing-Ke Shih static void
1801e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev,
1802e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1803e3ec7017SPing-Ke Shih 				  u8 ntx, u8 ch)
1804e3ec7017SPing-Ke Shih {
1805e3ec7017SPing-Ke Shih 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1806e3ec7017SPing-Ke Shih 							ntx, ch);
1807e3ec7017SPing-Ke Shih 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1808e3ec7017SPing-Ke Shih 							ntx, ch);
1809e3ec7017SPing-Ke Shih 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1810e3ec7017SPing-Ke Shih 							 ntx, ch);
1811e3ec7017SPing-Ke Shih }
1812e3ec7017SPing-Ke Shih 
1813e3ec7017SPing-Ke Shih static void
1814e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev,
1815e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1816e3ec7017SPing-Ke Shih 				  u8 ntx, u8 ch)
1817e3ec7017SPing-Ke Shih {
1818e3ec7017SPing-Ke Shih 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1819e3ec7017SPing-Ke Shih 							ntx, ch - 2);
1820e3ec7017SPing-Ke Shih 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1821e3ec7017SPing-Ke Shih 							ntx, ch + 2);
1822e3ec7017SPing-Ke Shih 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1823e3ec7017SPing-Ke Shih 							ntx, ch - 2);
1824e3ec7017SPing-Ke Shih 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1825e3ec7017SPing-Ke Shih 							ntx, ch + 2);
1826e3ec7017SPing-Ke Shih 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1827e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
1828e3ec7017SPing-Ke Shih 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1829e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
1830e3ec7017SPing-Ke Shih }
1831e3ec7017SPing-Ke Shih 
1832e3ec7017SPing-Ke Shih static void
1833e3ec7017SPing-Ke Shih rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev,
1834e3ec7017SPing-Ke Shih 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1835e3ec7017SPing-Ke Shih 				  u8 ntx, u8 ch)
1836e3ec7017SPing-Ke Shih {
1837e3ec7017SPing-Ke Shih 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1838e3ec7017SPing-Ke Shih 							ntx, ch - 6);
1839e3ec7017SPing-Ke Shih 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1840e3ec7017SPing-Ke Shih 							ntx, ch - 2);
1841e3ec7017SPing-Ke Shih 	lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1842e3ec7017SPing-Ke Shih 							ntx, ch + 2);
1843e3ec7017SPing-Ke Shih 	lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU26,
1844e3ec7017SPing-Ke Shih 							ntx, ch + 6);
1845e3ec7017SPing-Ke Shih 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1846e3ec7017SPing-Ke Shih 							ntx, ch - 6);
1847e3ec7017SPing-Ke Shih 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1848e3ec7017SPing-Ke Shih 							ntx, ch - 2);
1849e3ec7017SPing-Ke Shih 	lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1850e3ec7017SPing-Ke Shih 							ntx, ch + 2);
1851e3ec7017SPing-Ke Shih 	lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU52,
1852e3ec7017SPing-Ke Shih 							ntx, ch + 6);
1853e3ec7017SPing-Ke Shih 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1854e3ec7017SPing-Ke Shih 							 ntx, ch - 6);
1855e3ec7017SPing-Ke Shih 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1856e3ec7017SPing-Ke Shih 							 ntx, ch - 2);
1857e3ec7017SPing-Ke Shih 	lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1858e3ec7017SPing-Ke Shih 							 ntx, ch + 2);
1859e3ec7017SPing-Ke Shih 	lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, RTW89_RU106,
1860e3ec7017SPing-Ke Shih 							 ntx, ch + 6);
1861e3ec7017SPing-Ke Shih }
1862e3ec7017SPing-Ke Shih 
186394b70cafSZong-Zhe Yang static void
186494b70cafSZong-Zhe Yang rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev,
186594b70cafSZong-Zhe Yang 				   struct rtw89_txpwr_limit_ru *lmt_ru,
186694b70cafSZong-Zhe Yang 				   u8 ntx, u8 ch)
186794b70cafSZong-Zhe Yang {
186894b70cafSZong-Zhe Yang 	static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 };
186994b70cafSZong-Zhe Yang 	int i;
187094b70cafSZong-Zhe Yang 
187194b70cafSZong-Zhe Yang 	static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM);
187294b70cafSZong-Zhe Yang 	for (i = 0; i < RTW89_RU_SEC_NUM; i++) {
187394b70cafSZong-Zhe Yang 		lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev,
187494b70cafSZong-Zhe Yang 								RTW89_RU26,
187594b70cafSZong-Zhe Yang 								ntx,
187694b70cafSZong-Zhe Yang 								ch + ofst[i]);
187794b70cafSZong-Zhe Yang 		lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev,
187894b70cafSZong-Zhe Yang 								RTW89_RU52,
187994b70cafSZong-Zhe Yang 								ntx,
188094b70cafSZong-Zhe Yang 								ch + ofst[i]);
188194b70cafSZong-Zhe Yang 		lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev,
188294b70cafSZong-Zhe Yang 								 RTW89_RU106,
188394b70cafSZong-Zhe Yang 								 ntx,
188494b70cafSZong-Zhe Yang 								 ch + ofst[i]);
188594b70cafSZong-Zhe Yang 	}
188694b70cafSZong-Zhe Yang }
188794b70cafSZong-Zhe Yang 
1888e3ec7017SPing-Ke Shih void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev,
1889e3ec7017SPing-Ke Shih 				   struct rtw89_txpwr_limit_ru *lmt_ru,
1890e3ec7017SPing-Ke Shih 				   u8 ntx)
1891e3ec7017SPing-Ke Shih {
1892*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1893*cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
1894*cbb145b9SZong-Zhe Yang 	u8 bw = chan->band_width;
1895e3ec7017SPing-Ke Shih 
1896e3ec7017SPing-Ke Shih 	memset(lmt_ru, 0, sizeof(*lmt_ru));
1897e3ec7017SPing-Ke Shih 
1898e3ec7017SPing-Ke Shih 	switch (bw) {
1899e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
1900e3ec7017SPing-Ke Shih 		rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, ntx, ch);
1901e3ec7017SPing-Ke Shih 		break;
1902e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
1903e3ec7017SPing-Ke Shih 		rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, ntx, ch);
1904e3ec7017SPing-Ke Shih 		break;
1905e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
1906e3ec7017SPing-Ke Shih 		rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, ntx, ch);
1907e3ec7017SPing-Ke Shih 		break;
190894b70cafSZong-Zhe Yang 	case RTW89_CHANNEL_WIDTH_160:
190994b70cafSZong-Zhe Yang 		rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, ntx, ch);
191094b70cafSZong-Zhe Yang 		break;
1911e3ec7017SPing-Ke Shih 	}
1912e3ec7017SPing-Ke Shih }
1913861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_phy_fill_txpwr_limit_ru);
1914e3ec7017SPing-Ke Shih 
1915e3ec7017SPing-Ke Shih struct rtw89_phy_iter_ra_data {
1916e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
1917e3ec7017SPing-Ke Shih 	struct sk_buff *c2h;
1918e3ec7017SPing-Ke Shih };
1919e3ec7017SPing-Ke Shih 
1920e3ec7017SPing-Ke Shih static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta)
1921e3ec7017SPing-Ke Shih {
1922e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data;
1923e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = ra_data->rtwdev;
1924e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
1925e3ec7017SPing-Ke Shih 	struct rtw89_ra_report *ra_report = &rtwsta->ra_report;
1926e3ec7017SPing-Ke Shih 	struct sk_buff *c2h = ra_data->c2h;
1927e3ec7017SPing-Ke Shih 	u8 mode, rate, bw, giltf, mac_id;
19289a3a593cSPing-Ke Shih 	u16 legacy_bitrate;
19299a3a593cSPing-Ke Shih 	bool valid;
1930e3ec7017SPing-Ke Shih 
1931e3ec7017SPing-Ke Shih 	mac_id = RTW89_GET_PHY_C2H_RA_RPT_MACID(c2h->data);
1932e3ec7017SPing-Ke Shih 	if (mac_id != rtwsta->mac_id)
1933e3ec7017SPing-Ke Shih 		return;
1934e3ec7017SPing-Ke Shih 
1935e3ec7017SPing-Ke Shih 	rate = RTW89_GET_PHY_C2H_RA_RPT_MCSNSS(c2h->data);
1936e3ec7017SPing-Ke Shih 	bw = RTW89_GET_PHY_C2H_RA_RPT_BW(c2h->data);
1937e3ec7017SPing-Ke Shih 	giltf = RTW89_GET_PHY_C2H_RA_RPT_GILTF(c2h->data);
1938e3ec7017SPing-Ke Shih 	mode = RTW89_GET_PHY_C2H_RA_RPT_MD_SEL(c2h->data);
1939e3ec7017SPing-Ke Shih 
19409a3a593cSPing-Ke Shih 	if (mode == RTW89_RA_RPT_MODE_LEGACY) {
19419a3a593cSPing-Ke Shih 		valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate);
19429a3a593cSPing-Ke Shih 		if (!valid)
19439a3a593cSPing-Ke Shih 			return;
19449a3a593cSPing-Ke Shih 	}
19459a3a593cSPing-Ke Shih 
19469a3a593cSPing-Ke Shih 	memset(ra_report, 0, sizeof(*ra_report));
19479a3a593cSPing-Ke Shih 
1948e3ec7017SPing-Ke Shih 	switch (mode) {
1949e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_LEGACY:
19509a3a593cSPing-Ke Shih 		ra_report->txrate.legacy = legacy_bitrate;
1951e3ec7017SPing-Ke Shih 		break;
1952e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HT:
1953e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS;
195411fe4ccdSZong-Zhe Yang 		if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw))
1955e3ec7017SPing-Ke Shih 			rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate),
1956e3ec7017SPing-Ke Shih 						FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate));
1957e3ec7017SPing-Ke Shih 		else
1958e3ec7017SPing-Ke Shih 			rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate);
1959e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = rate;
1960e3ec7017SPing-Ke Shih 		if (giltf)
1961e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1962e3ec7017SPing-Ke Shih 		break;
1963e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_VHT:
1964e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
1965e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
1966e3ec7017SPing-Ke Shih 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
1967e3ec7017SPing-Ke Shih 		if (giltf)
1968e3ec7017SPing-Ke Shih 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1969e3ec7017SPing-Ke Shih 		break;
1970e3ec7017SPing-Ke Shih 	case RTW89_RA_RPT_MODE_HE:
1971e3ec7017SPing-Ke Shih 		ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS;
1972e3ec7017SPing-Ke Shih 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
1973e3ec7017SPing-Ke Shih 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
1974e3ec7017SPing-Ke Shih 		if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08)
1975e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1976e3ec7017SPing-Ke Shih 		else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16)
1977e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1978e3ec7017SPing-Ke Shih 		else
1979e3ec7017SPing-Ke Shih 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1980e3ec7017SPing-Ke Shih 		break;
1981e3ec7017SPing-Ke Shih 	}
1982e3ec7017SPing-Ke Shih 
1983167044afSPing-Ke Shih 	ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw);
1984e3ec7017SPing-Ke Shih 	ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate);
1985e3ec7017SPing-Ke Shih 	ra_report->hw_rate = FIELD_PREP(RTW89_HW_RATE_MASK_MOD, mode) |
1986e3ec7017SPing-Ke Shih 			     FIELD_PREP(RTW89_HW_RATE_MASK_VAL, rate);
1987e3ec7017SPing-Ke Shih 	sta->max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report);
1988e3ec7017SPing-Ke Shih 	rtwsta->max_agg_wait = sta->max_rc_amsdu_len / 1500 - 1;
1989e3ec7017SPing-Ke Shih }
1990e3ec7017SPing-Ke Shih 
1991e3ec7017SPing-Ke Shih static void
1992e3ec7017SPing-Ke Shih rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
1993e3ec7017SPing-Ke Shih {
1994e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_ra_data ra_data;
1995e3ec7017SPing-Ke Shih 
1996e3ec7017SPing-Ke Shih 	ra_data.rtwdev = rtwdev;
1997e3ec7017SPing-Ke Shih 	ra_data.c2h = c2h;
1998e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
1999e3ec7017SPing-Ke Shih 					  rtw89_phy_c2h_ra_rpt_iter,
2000e3ec7017SPing-Ke Shih 					  &ra_data);
2001e3ec7017SPing-Ke Shih }
2002e3ec7017SPing-Ke Shih 
2003e3ec7017SPing-Ke Shih static
2004e3ec7017SPing-Ke Shih void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev,
2005e3ec7017SPing-Ke Shih 					  struct sk_buff *c2h, u32 len) = {
2006e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt,
2007e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL,
2008e3ec7017SPing-Ke Shih 	[RTW89_PHY_C2H_FUNC_TXSTS] = NULL,
2009e3ec7017SPing-Ke Shih };
2010e3ec7017SPing-Ke Shih 
2011e3ec7017SPing-Ke Shih void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
2012e3ec7017SPing-Ke Shih 			  u32 len, u8 class, u8 func)
2013e3ec7017SPing-Ke Shih {
2014e3ec7017SPing-Ke Shih 	void (*handler)(struct rtw89_dev *rtwdev,
2015e3ec7017SPing-Ke Shih 			struct sk_buff *c2h, u32 len) = NULL;
2016e3ec7017SPing-Ke Shih 
2017e3ec7017SPing-Ke Shih 	switch (class) {
2018e3ec7017SPing-Ke Shih 	case RTW89_PHY_C2H_CLASS_RA:
2019e3ec7017SPing-Ke Shih 		if (func < RTW89_PHY_C2H_FUNC_RA_MAX)
2020e3ec7017SPing-Ke Shih 			handler = rtw89_phy_c2h_ra_handler[func];
2021e3ec7017SPing-Ke Shih 		break;
2022e3ec7017SPing-Ke Shih 	default:
2023e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
2024e3ec7017SPing-Ke Shih 		return;
2025e3ec7017SPing-Ke Shih 	}
2026e3ec7017SPing-Ke Shih 	if (!handler) {
2027e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
2028e3ec7017SPing-Ke Shih 			   func);
2029e3ec7017SPing-Ke Shih 		return;
2030e3ec7017SPing-Ke Shih 	}
2031e3ec7017SPing-Ke Shih 	handler(rtwdev, skb, len);
2032e3ec7017SPing-Ke Shih }
2033e3ec7017SPing-Ke Shih 
2034e3ec7017SPing-Ke Shih static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo)
2035e3ec7017SPing-Ke Shih {
2036e3ec7017SPing-Ke Shih 	u32 reg_mask;
2037e3ec7017SPing-Ke Shih 
2038e3ec7017SPing-Ke Shih 	if (sc_xo)
2039e3ec7017SPing-Ke Shih 		reg_mask = B_AX_XTAL_SC_XO_MASK;
2040e3ec7017SPing-Ke Shih 	else
2041e3ec7017SPing-Ke Shih 		reg_mask = B_AX_XTAL_SC_XI_MASK;
2042e3ec7017SPing-Ke Shih 
2043e3ec7017SPing-Ke Shih 	return (u8)rtw89_read32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask);
2044e3ec7017SPing-Ke Shih }
2045e3ec7017SPing-Ke Shih 
2046e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo,
2047e3ec7017SPing-Ke Shih 				       u8 val)
2048e3ec7017SPing-Ke Shih {
2049e3ec7017SPing-Ke Shih 	u32 reg_mask;
2050e3ec7017SPing-Ke Shih 
2051e3ec7017SPing-Ke Shih 	if (sc_xo)
2052e3ec7017SPing-Ke Shih 		reg_mask = B_AX_XTAL_SC_XO_MASK;
2053e3ec7017SPing-Ke Shih 	else
2054e3ec7017SPing-Ke Shih 		reg_mask = B_AX_XTAL_SC_XI_MASK;
2055e3ec7017SPing-Ke Shih 
2056e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask, val);
2057e3ec7017SPing-Ke Shih }
2058e3ec7017SPing-Ke Shih 
2059e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev,
2060e3ec7017SPing-Ke Shih 					  u8 crystal_cap, bool force)
2061e3ec7017SPing-Ke Shih {
2062e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
20638379fa61SYuan-Han Zhang 	const struct rtw89_chip_info *chip = rtwdev->chip;
2064e3ec7017SPing-Ke Shih 	u8 sc_xi_val, sc_xo_val;
2065e3ec7017SPing-Ke Shih 
2066e3ec7017SPing-Ke Shih 	if (!force && cfo->crystal_cap == crystal_cap)
2067e3ec7017SPing-Ke Shih 		return;
2068e3ec7017SPing-Ke Shih 	crystal_cap = clamp_t(u8, crystal_cap, 0, 127);
20698379fa61SYuan-Han Zhang 	if (chip->chip_id == RTL8852A) {
2070e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap);
2071e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap);
2072e3ec7017SPing-Ke Shih 		sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true);
2073e3ec7017SPing-Ke Shih 		sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false);
20748379fa61SYuan-Han Zhang 	} else {
20758379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO,
20768379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XO_MASK);
20778379fa61SYuan-Han Zhang 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI,
20788379fa61SYuan-Han Zhang 					crystal_cap, XTAL_SC_XI_MASK);
20798379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val);
20808379fa61SYuan-Han Zhang 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val);
20818379fa61SYuan-Han Zhang 	}
2082e3ec7017SPing-Ke Shih 	cfo->crystal_cap = sc_xi_val;
2083e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap);
2084e3ec7017SPing-Ke Shih 
2085e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val);
2086e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val);
2087e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n",
2088e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2089e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n");
2090e3ec7017SPing-Ke Shih }
2091e3ec7017SPing-Ke Shih 
2092e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev)
2093e3ec7017SPing-Ke Shih {
2094e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2095e3ec7017SPing-Ke Shih 	u8 cap;
2096e3ec7017SPing-Ke Shih 
2097e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK;
2098e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2099e3ec7017SPing-Ke Shih 	if (cfo->crystal_cap == cfo->def_x_cap)
2100e3ec7017SPing-Ke Shih 		return;
2101e3ec7017SPing-Ke Shih 	cap = cfo->crystal_cap;
2102e3ec7017SPing-Ke Shih 	cap += (cap > cfo->def_x_cap ? -1 : 1);
2103e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false);
2104e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2105e3ec7017SPing-Ke Shih 		    "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap,
2106e3ec7017SPing-Ke Shih 		    cfo->def_x_cap);
2107e3ec7017SPing-Ke Shih }
2108e3ec7017SPing-Ke Shih 
2109e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo)
2110e3ec7017SPing-Ke Shih {
2111b7379148SYuan-Han Zhang 	const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp;
2112e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
2113e3ec7017SPing-Ke Shih 	s32 cfo_avg_312;
2114b7379148SYuan-Han Zhang 	s32 dcfo_comp_val;
2115b7379148SYuan-Han Zhang 	u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft;
2116e3ec7017SPing-Ke Shih 	int sign;
2117e3ec7017SPing-Ke Shih 
2118e3ec7017SPing-Ke Shih 	if (!is_linked) {
2119e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n",
2120e3ec7017SPing-Ke Shih 			    is_linked);
2121e3ec7017SPing-Ke Shih 		return;
2122e3ec7017SPing-Ke Shih 	}
2123e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo);
2124e3ec7017SPing-Ke Shih 	if (curr_cfo == 0)
2125e3ec7017SPing-Ke Shih 		return;
2126b7379148SYuan-Han Zhang 	dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO);
2127e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
2128b7379148SYuan-Han Zhang 	cfo_avg_312 = (curr_cfo << dcfo_comp_sft) / 5 + sign * dcfo_comp_val;
2129e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: avg_cfo=%d\n", cfo_avg_312);
2130e3ec7017SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV)
2131e3ec7017SPing-Ke Shih 		cfo_avg_312 = -cfo_avg_312;
2132b7379148SYuan-Han Zhang 	rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask,
2133e3ec7017SPing-Ke Shih 			       cfo_avg_312);
2134e3ec7017SPing-Ke Shih }
2135e3ec7017SPing-Ke Shih 
2136e3ec7017SPing-Ke Shih static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev)
2137e3ec7017SPing-Ke Shih {
2138e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1);
2139e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8);
2140e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK);
2141e3ec7017SPing-Ke Shih }
2142e3ec7017SPing-Ke Shih 
2143e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev)
2144e3ec7017SPing-Ke Shih {
2145e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2146e3ec7017SPing-Ke Shih 	struct rtw89_efuse *efuse = &rtwdev->efuse;
2147e3ec7017SPing-Ke Shih 
2148e3ec7017SPing-Ke Shih 	cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK;
2149e3ec7017SPing-Ke Shih 	cfo->crystal_cap = cfo->crystal_cap_default;
2150e3ec7017SPing-Ke Shih 	cfo->def_x_cap = cfo->crystal_cap;
2151a9e06f2eSYi-Tang Chiu 	cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f);
2152a9e06f2eSYi-Tang Chiu 	cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1);
2153e3ec7017SPing-Ke Shih 	cfo->is_adjust = false;
2154a9e06f2eSYi-Tang Chiu 	cfo->divergence_lock_en = false;
2155e3ec7017SPing-Ke Shih 	cfo->x_cap_ofst = 0;
2156a9e06f2eSYi-Tang Chiu 	cfo->lock_cnt = 0;
2157e3ec7017SPing-Ke Shih 	cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE;
2158e3ec7017SPing-Ke Shih 	cfo->apply_compensation = false;
2159e3ec7017SPing-Ke Shih 	cfo->residual_cfo_acc = 0;
2160e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n",
2161e3ec7017SPing-Ke Shih 		    cfo->crystal_cap_default);
2162e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true);
2163e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1);
2164e3ec7017SPing-Ke Shih 	rtw89_dcfo_comp_init(rtwdev);
2165e3ec7017SPing-Ke Shih 	cfo->cfo_timer_ms = 2000;
2166e3ec7017SPing-Ke Shih 	cfo->cfo_trig_by_timer_en = false;
2167e3ec7017SPing-Ke Shih 	cfo->phy_cfo_trk_cnt = 0;
2168e3ec7017SPing-Ke Shih 	cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2169bc013052SEric Huang 	cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE;
2170e3ec7017SPing-Ke Shih }
2171e3ec7017SPing-Ke Shih 
2172e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev,
2173e3ec7017SPing-Ke Shih 					     s32 curr_cfo)
2174e3ec7017SPing-Ke Shih {
2175e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2176e3ec7017SPing-Ke Shih 	s8 crystal_cap = cfo->crystal_cap;
2177e3ec7017SPing-Ke Shih 	s32 cfo_abs = abs(curr_cfo);
2178e3ec7017SPing-Ke Shih 	int sign;
2179e3ec7017SPing-Ke Shih 
2180e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2181e3ec7017SPing-Ke Shih 		if (cfo_abs > CFO_TRK_ENABLE_TH)
2182e3ec7017SPing-Ke Shih 			cfo->is_adjust = true;
2183e3ec7017SPing-Ke Shih 	} else {
2184e3ec7017SPing-Ke Shih 		if (cfo_abs < CFO_TRK_STOP_TH)
2185e3ec7017SPing-Ke Shih 			cfo->is_adjust = false;
2186e3ec7017SPing-Ke Shih 	}
2187e3ec7017SPing-Ke Shih 	if (!cfo->is_adjust) {
2188e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n");
2189e3ec7017SPing-Ke Shih 		return;
2190e3ec7017SPing-Ke Shih 	}
2191e3ec7017SPing-Ke Shih 	sign = curr_cfo > 0 ? 1 : -1;
2192e3ec7017SPing-Ke Shih 	if (cfo_abs > CFO_TRK_STOP_TH_4)
2193e3ec7017SPing-Ke Shih 		crystal_cap += 7 * sign;
2194e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_3)
2195e3ec7017SPing-Ke Shih 		crystal_cap += 5 * sign;
2196e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_2)
2197e3ec7017SPing-Ke Shih 		crystal_cap += 3 * sign;
2198e3ec7017SPing-Ke Shih 	else if (cfo_abs > CFO_TRK_STOP_TH_1)
2199e3ec7017SPing-Ke Shih 		crystal_cap += 1 * sign;
2200e3ec7017SPing-Ke Shih 	else
2201e3ec7017SPing-Ke Shih 		return;
2202e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false);
2203e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2204e3ec7017SPing-Ke Shih 		    "X_cap{Curr,Default}={0x%x,0x%x}\n",
2205e3ec7017SPing-Ke Shih 		    cfo->crystal_cap, cfo->def_x_cap);
2206e3ec7017SPing-Ke Shih }
2207e3ec7017SPing-Ke Shih 
2208e3ec7017SPing-Ke Shih static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev)
2209e3ec7017SPing-Ke Shih {
2210e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2211e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2212e3ec7017SPing-Ke Shih 	s32 cfo_cnt_all = 0;
2213e3ec7017SPing-Ke Shih 	s32 cfo_all_avg = 0;
2214e3ec7017SPing-Ke Shih 	u8 i;
2215e3ec7017SPing-Ke Shih 
2216e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc != 1)
2217e3ec7017SPing-Ke Shih 		return 0;
2218e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n");
2219e3ec7017SPing-Ke Shih 	for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2220e3ec7017SPing-Ke Shih 		if (cfo->cfo_cnt[i] == 0)
2221e3ec7017SPing-Ke Shih 			continue;
2222e3ec7017SPing-Ke Shih 		cfo_khz_all += cfo->cfo_tail[i];
2223e3ec7017SPing-Ke Shih 		cfo_cnt_all += cfo->cfo_cnt[i];
2224e3ec7017SPing-Ke Shih 		cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all);
2225e3ec7017SPing-Ke Shih 		cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2226e3ec7017SPing-Ke Shih 	}
2227e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2228e3ec7017SPing-Ke Shih 		    "CFO track for macid = %d\n", i);
2229e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2230e3ec7017SPing-Ke Shih 		    "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n",
2231e3ec7017SPing-Ke Shih 		    cfo_khz_all, cfo_cnt_all, cfo_all_avg);
2232e3ec7017SPing-Ke Shih 	return cfo_all_avg;
2233e3ec7017SPing-Ke Shih }
2234e3ec7017SPing-Ke Shih 
2235e3ec7017SPing-Ke Shih static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev)
2236e3ec7017SPing-Ke Shih {
2237e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2238e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2239e3ec7017SPing-Ke Shih 	s32 target_cfo = 0;
2240e3ec7017SPing-Ke Shih 	s32 cfo_khz_all = 0;
2241e3ec7017SPing-Ke Shih 	s32 cfo_khz_all_tp_wgt = 0;
2242e3ec7017SPing-Ke Shih 	s32 cfo_avg = 0;
2243e3ec7017SPing-Ke Shih 	s32 max_cfo_lb = BIT(31);
2244e3ec7017SPing-Ke Shih 	s32 min_cfo_ub = GENMASK(30, 0);
2245e3ec7017SPing-Ke Shih 	u16 cfo_cnt_all = 0;
2246e3ec7017SPing-Ke Shih 	u8 active_entry_cnt = 0;
2247e3ec7017SPing-Ke Shih 	u8 sta_cnt = 0;
2248e3ec7017SPing-Ke Shih 	u32 tp_all = 0;
2249e3ec7017SPing-Ke Shih 	u8 i;
2250e3ec7017SPing-Ke Shih 	u8 cfo_tol = 0;
2251e3ec7017SPing-Ke Shih 
2252e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n");
2253e3ec7017SPing-Ke Shih 	if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) {
2254e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n");
2255e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2256e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2257e3ec7017SPing-Ke Shih 				continue;
2258e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_tail[i];
2259e3ec7017SPing-Ke Shih 			cfo_cnt_all += cfo->cfo_cnt[i];
2260e3ec7017SPing-Ke Shih 			cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all);
2261e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2262e3ec7017SPing-Ke Shih 				    "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n",
2263e3ec7017SPing-Ke Shih 				    cfo_khz_all, cfo_cnt_all, cfo_avg);
2264e3ec7017SPing-Ke Shih 			target_cfo = cfo_avg;
2265e3ec7017SPing-Ke Shih 		}
2266e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) {
2267e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n");
2268e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2269e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] == 0)
2270e3ec7017SPing-Ke Shih 				continue;
2271e3ec7017SPing-Ke Shih 			cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2272e3ec7017SPing-Ke Shih 						  (s32)cfo->cfo_cnt[i]);
2273e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2274e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2275e3ec7017SPing-Ke Shih 				    "Macid=%d, cfo_avg=%d\n", i,
2276e3ec7017SPing-Ke Shih 				    cfo->cfo_avg[i]);
2277e3ec7017SPing-Ke Shih 		}
2278e3ec7017SPing-Ke Shih 		sta_cnt = rtwdev->total_sta_assoc;
2279e3ec7017SPing-Ke Shih 		cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt);
2280e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2281e3ec7017SPing-Ke Shih 			    "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n",
2282e3ec7017SPing-Ke Shih 			    cfo_khz_all, sta_cnt, cfo_avg);
2283e3ec7017SPing-Ke Shih 		target_cfo = cfo_avg;
2284e3ec7017SPing-Ke Shih 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) {
2285e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n");
2286e3ec7017SPing-Ke Shih 		cfo_tol = cfo->sta_cfo_tolerance;
2287e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2288e3ec7017SPing-Ke Shih 			sta_cnt++;
2289e3ec7017SPing-Ke Shih 			if (cfo->cfo_cnt[i] != 0) {
2290e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2291e3ec7017SPing-Ke Shih 							  (s32)cfo->cfo_cnt[i]);
2292e3ec7017SPing-Ke Shih 				active_entry_cnt++;
2293e3ec7017SPing-Ke Shih 			} else {
2294e3ec7017SPing-Ke Shih 				cfo->cfo_avg[i] = cfo->pre_cfo_avg[i];
2295e3ec7017SPing-Ke Shih 			}
2296e3ec7017SPing-Ke Shih 			max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb);
2297e3ec7017SPing-Ke Shih 			min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub);
2298e3ec7017SPing-Ke Shih 			cfo_khz_all += cfo->cfo_avg[i];
2299e3ec7017SPing-Ke Shih 			/* need tp for each entry */
2300e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2301e3ec7017SPing-Ke Shih 				    "[%d] cfo_avg=%d, tp=tbd\n",
2302e3ec7017SPing-Ke Shih 				    i, cfo->cfo_avg[i]);
2303e3ec7017SPing-Ke Shih 			if (sta_cnt >= rtwdev->total_sta_assoc)
2304e3ec7017SPing-Ke Shih 				break;
2305e3ec7017SPing-Ke Shih 		}
2306e3ec7017SPing-Ke Shih 		tp_all = stats->rx_throughput; /* need tp for each entry */
2307e3ec7017SPing-Ke Shih 		cfo_avg =  phy_div(cfo_khz_all_tp_wgt, (s32)tp_all);
2308e3ec7017SPing-Ke Shih 
2309e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n",
2310e3ec7017SPing-Ke Shih 			    sta_cnt);
2311e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n",
2312e3ec7017SPing-Ke Shih 			    active_entry_cnt);
2313e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2314e3ec7017SPing-Ke Shih 			    "Msta cfo with tp_wgt=%d, avg_cfo=%d\n",
2315e3ec7017SPing-Ke Shih 			    cfo_khz_all_tp_wgt, cfo_avg);
2316e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n",
2317e3ec7017SPing-Ke Shih 			    max_cfo_lb, min_cfo_ub);
2318e3ec7017SPing-Ke Shih 		if (max_cfo_lb <= min_cfo_ub) {
2319e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2320e3ec7017SPing-Ke Shih 				    "cfo win_size=%d\n",
2321e3ec7017SPing-Ke Shih 				    min_cfo_ub - max_cfo_lb);
2322e3ec7017SPing-Ke Shih 			target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub);
2323e3ec7017SPing-Ke Shih 		} else {
2324e3ec7017SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2325c51ed740SColin Ian King 				    "No intersection of cfo tolerance windows\n");
2326e3ec7017SPing-Ke Shih 			target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt);
2327e3ec7017SPing-Ke Shih 		}
2328e3ec7017SPing-Ke Shih 		for (i = 0; i < CFO_TRACK_MAX_USER; i++)
2329e3ec7017SPing-Ke Shih 			cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2330e3ec7017SPing-Ke Shih 	}
2331e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo);
2332e3ec7017SPing-Ke Shih 	return target_cfo;
2333e3ec7017SPing-Ke Shih }
2334e3ec7017SPing-Ke Shih 
2335e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev)
2336e3ec7017SPing-Ke Shih {
2337e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2338e3ec7017SPing-Ke Shih 
2339e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail));
2340e3ec7017SPing-Ke Shih 	memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt));
2341e3ec7017SPing-Ke Shih 	cfo->packet_count = 0;
2342e3ec7017SPing-Ke Shih 	cfo->packet_count_pre = 0;
2343e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = 0;
2344e3ec7017SPing-Ke Shih }
2345e3ec7017SPing-Ke Shih 
2346e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev)
2347e3ec7017SPing-Ke Shih {
2348e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2349e3ec7017SPing-Ke Shih 	s32 new_cfo = 0;
2350e3ec7017SPing-Ke Shih 	bool x_cap_update = false;
2351e3ec7017SPing-Ke Shih 	u8 pre_x_cap = cfo->crystal_cap;
2352e3ec7017SPing-Ke Shih 
2353e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n",
2354e3ec7017SPing-Ke Shih 		    rtwdev->total_sta_assoc);
2355e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 0) {
2356e3ec7017SPing-Ke Shih 		rtw89_phy_cfo_reset(rtwdev);
2357e3ec7017SPing-Ke Shih 		return;
2358e3ec7017SPing-Ke Shih 	}
2359e3ec7017SPing-Ke Shih 	if (cfo->packet_count == 0) {
2360e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n");
2361e3ec7017SPing-Ke Shih 		return;
2362e3ec7017SPing-Ke Shih 	}
2363e3ec7017SPing-Ke Shih 	if (cfo->packet_count == cfo->packet_count_pre) {
2364e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n");
2365e3ec7017SPing-Ke Shih 		return;
2366e3ec7017SPing-Ke Shih 	}
2367e3ec7017SPing-Ke Shih 	if (rtwdev->total_sta_assoc == 1)
2368e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_average_cfo_calc(rtwdev);
2369e3ec7017SPing-Ke Shih 	else
2370e3ec7017SPing-Ke Shih 		new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev);
2371e3ec7017SPing-Ke Shih 	if (new_cfo == 0) {
2372e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n");
2373e3ec7017SPing-Ke Shih 		return;
2374e3ec7017SPing-Ke Shih 	}
2375a9e06f2eSYi-Tang Chiu 	if (cfo->divergence_lock_en) {
2376a9e06f2eSYi-Tang Chiu 		cfo->lock_cnt++;
2377a9e06f2eSYi-Tang Chiu 		if (cfo->lock_cnt > CFO_PERIOD_CNT) {
2378a9e06f2eSYi-Tang Chiu 			cfo->divergence_lock_en = false;
2379a9e06f2eSYi-Tang Chiu 			cfo->lock_cnt = 0;
2380a9e06f2eSYi-Tang Chiu 		} else {
2381a9e06f2eSYi-Tang Chiu 			rtw89_phy_cfo_reset(rtwdev);
2382a9e06f2eSYi-Tang Chiu 		}
2383a9e06f2eSYi-Tang Chiu 		return;
2384a9e06f2eSYi-Tang Chiu 	}
2385a9e06f2eSYi-Tang Chiu 	if (cfo->crystal_cap >= cfo->x_cap_ub ||
2386a9e06f2eSYi-Tang Chiu 	    cfo->crystal_cap <= cfo->x_cap_lb) {
2387a9e06f2eSYi-Tang Chiu 		cfo->divergence_lock_en = true;
2388a9e06f2eSYi-Tang Chiu 		rtw89_phy_cfo_reset(rtwdev);
2389a9e06f2eSYi-Tang Chiu 		return;
2390a9e06f2eSYi-Tang Chiu 	}
2391a9e06f2eSYi-Tang Chiu 
2392e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo);
2393e3ec7017SPing-Ke Shih 	cfo->cfo_avg_pre = new_cfo;
23941646ce8fSYe Guojin 	x_cap_update =  cfo->crystal_cap != pre_x_cap;
2395e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update);
2396e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n",
2397e3ec7017SPing-Ke Shih 		    cfo->def_x_cap, pre_x_cap, cfo->crystal_cap,
2398e3ec7017SPing-Ke Shih 		    cfo->x_cap_ofst);
2399e3ec7017SPing-Ke Shih 	if (x_cap_update) {
2400e3ec7017SPing-Ke Shih 		if (new_cfo > 0)
2401e3ec7017SPing-Ke Shih 			new_cfo -= CFO_SW_COMP_FINE_TUNE;
2402e3ec7017SPing-Ke Shih 		else
2403e3ec7017SPing-Ke Shih 			new_cfo += CFO_SW_COMP_FINE_TUNE;
2404e3ec7017SPing-Ke Shih 	}
2405e3ec7017SPing-Ke Shih 	rtw89_dcfo_comp(rtwdev, new_cfo);
2406e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_statistics_reset(rtwdev);
2407e3ec7017SPing-Ke Shih }
2408e3ec7017SPing-Ke Shih 
2409e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track_work(struct work_struct *work)
2410e3ec7017SPing-Ke Shih {
2411e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2412e3ec7017SPing-Ke Shih 						cfo_track_work.work);
2413e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2414e3ec7017SPing-Ke Shih 
2415e3ec7017SPing-Ke Shih 	mutex_lock(&rtwdev->mutex);
2416e3ec7017SPing-Ke Shih 	if (!cfo->cfo_trig_by_timer_en)
2417e3ec7017SPing-Ke Shih 		goto out;
2418e3ec7017SPing-Ke Shih 	rtw89_leave_ps_mode(rtwdev);
2419e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2420e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2421e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2422e3ec7017SPing-Ke Shih out:
2423e3ec7017SPing-Ke Shih 	mutex_unlock(&rtwdev->mutex);
2424e3ec7017SPing-Ke Shih }
2425e3ec7017SPing-Ke Shih 
2426e3ec7017SPing-Ke Shih static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev)
2427e3ec7017SPing-Ke Shih {
2428e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2429e3ec7017SPing-Ke Shih 
2430e3ec7017SPing-Ke Shih 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2431e3ec7017SPing-Ke Shih 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2432e3ec7017SPing-Ke Shih }
2433e3ec7017SPing-Ke Shih 
2434e3ec7017SPing-Ke Shih void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev)
2435e3ec7017SPing-Ke Shih {
2436e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2437e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2438bc013052SEric Huang 	bool is_ul_ofdma = false, ofdma_acc_en = false;
2439bc013052SEric Huang 
2440bc013052SEric Huang 	if (stats->rx_tf_periodic > CFO_TF_CNT_TH)
2441bc013052SEric Huang 		is_ul_ofdma = true;
2442bc013052SEric Huang 	if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE &&
2443bc013052SEric Huang 	    is_ul_ofdma)
2444bc013052SEric Huang 		ofdma_acc_en = true;
2445e3ec7017SPing-Ke Shih 
2446e3ec7017SPing-Ke Shih 	switch (cfo->phy_cfo_status) {
2447e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_NORMAL:
2448e3ec7017SPing-Ke Shih 		if (stats->tx_throughput >= CFO_TP_UPPER) {
2449e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE;
2450e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = true;
2451e3ec7017SPing-Ke Shih 			cfo->cfo_timer_ms = CFO_COMP_PERIOD;
2452e3ec7017SPing-Ke Shih 			rtw89_phy_cfo_start_work(rtwdev);
2453e3ec7017SPing-Ke Shih 		}
2454e3ec7017SPing-Ke Shih 		break;
2455e3ec7017SPing-Ke Shih 	case RTW89_PHY_DCFO_STATE_ENHANCE:
2456bc013052SEric Huang 		if (stats->tx_throughput <= CFO_TP_LOWER)
2457bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2458bc013052SEric Huang 		else if (ofdma_acc_en &&
2459bc013052SEric Huang 			 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT)
2460bc013052SEric Huang 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD;
2461bc013052SEric Huang 		else
2462bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2463bc013052SEric Huang 
2464bc013052SEric Huang 		if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) {
2465e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2466e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2467e3ec7017SPing-Ke Shih 		}
2468bc013052SEric Huang 		break;
2469bc013052SEric Huang 	case RTW89_PHY_DCFO_STATE_HOLD:
2470e3ec7017SPing-Ke Shih 		if (stats->tx_throughput <= CFO_TP_LOWER) {
2471e3ec7017SPing-Ke Shih 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2472e3ec7017SPing-Ke Shih 			cfo->phy_cfo_trk_cnt = 0;
2473e3ec7017SPing-Ke Shih 			cfo->cfo_trig_by_timer_en = false;
2474bc013052SEric Huang 		} else {
2475bc013052SEric Huang 			cfo->phy_cfo_trk_cnt++;
2476e3ec7017SPing-Ke Shih 		}
2477e3ec7017SPing-Ke Shih 		break;
2478e3ec7017SPing-Ke Shih 	default:
2479e3ec7017SPing-Ke Shih 		cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2480e3ec7017SPing-Ke Shih 		cfo->phy_cfo_trk_cnt = 0;
2481e3ec7017SPing-Ke Shih 		break;
2482e3ec7017SPing-Ke Shih 	}
2483e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2484e3ec7017SPing-Ke Shih 		    "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n",
2485e3ec7017SPing-Ke Shih 		    stats->tx_throughput, cfo->phy_cfo_status,
2486e3ec7017SPing-Ke Shih 		    cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt,
2487e3ec7017SPing-Ke Shih 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[0]));
2488e3ec7017SPing-Ke Shih 	if (cfo->cfo_trig_by_timer_en)
2489e3ec7017SPing-Ke Shih 		return;
2490e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_dm(rtwdev);
2491e3ec7017SPing-Ke Shih }
2492e3ec7017SPing-Ke Shih 
2493e3ec7017SPing-Ke Shih void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val,
2494e3ec7017SPing-Ke Shih 			 struct rtw89_rx_phy_ppdu *phy_ppdu)
2495e3ec7017SPing-Ke Shih {
2496e3ec7017SPing-Ke Shih 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2497e3ec7017SPing-Ke Shih 	u8 macid = phy_ppdu->mac_id;
2498e3ec7017SPing-Ke Shih 
249997df8587SPing-Ke Shih 	if (macid >= CFO_TRACK_MAX_USER) {
250097df8587SPing-Ke Shih 		rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid);
250197df8587SPing-Ke Shih 		return;
250297df8587SPing-Ke Shih 	}
250397df8587SPing-Ke Shih 
2504e3ec7017SPing-Ke Shih 	cfo->cfo_tail[macid] += cfo_val;
2505e3ec7017SPing-Ke Shih 	cfo->cfo_cnt[macid]++;
2506e3ec7017SPing-Ke Shih 	cfo->packet_count++;
2507e3ec7017SPing-Ke Shih }
2508e3ec7017SPing-Ke Shih 
2509e3ec7017SPing-Ke Shih static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev)
2510e3ec7017SPing-Ke Shih {
2511e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
2512e3ec7017SPing-Ke Shih 	int i;
2513e3ec7017SPing-Ke Shih 	u8 th;
2514e3ec7017SPing-Ke Shih 
2515e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++) {
2516e3ec7017SPing-Ke Shih 		th = rtw89_chip_get_thermal(rtwdev, i);
2517e3ec7017SPing-Ke Shih 		if (th)
2518e3ec7017SPing-Ke Shih 			ewma_thermal_add(&phystat->avg_thermal[i], th);
2519e3ec7017SPing-Ke Shih 
2520e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2521e3ec7017SPing-Ke Shih 			    "path(%d) thermal cur=%u avg=%ld", i, th,
2522e3ec7017SPing-Ke Shih 			    ewma_thermal_read(&phystat->avg_thermal[i]));
2523e3ec7017SPing-Ke Shih 	}
2524e3ec7017SPing-Ke Shih }
2525e3ec7017SPing-Ke Shih 
2526e3ec7017SPing-Ke Shih struct rtw89_phy_iter_rssi_data {
2527e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
2528e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info;
2529e3ec7017SPing-Ke Shih 	bool rssi_changed;
2530e3ec7017SPing-Ke Shih };
2531e3ec7017SPing-Ke Shih 
2532e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update_iter(void *data,
2533e3ec7017SPing-Ke Shih 					    struct ieee80211_sta *sta)
2534e3ec7017SPing-Ke Shih {
2535e3ec7017SPing-Ke Shih 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2536e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data *rssi_data =
2537e3ec7017SPing-Ke Shih 					(struct rtw89_phy_iter_rssi_data *)data;
2538e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info;
2539e3ec7017SPing-Ke Shih 	unsigned long rssi_curr;
2540e3ec7017SPing-Ke Shih 
2541e3ec7017SPing-Ke Shih 	rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi);
2542e3ec7017SPing-Ke Shih 
2543e3ec7017SPing-Ke Shih 	if (rssi_curr < ch_info->rssi_min) {
2544e3ec7017SPing-Ke Shih 		ch_info->rssi_min = rssi_curr;
2545e3ec7017SPing-Ke Shih 		ch_info->rssi_min_macid = rtwsta->mac_id;
2546e3ec7017SPing-Ke Shih 	}
2547e3ec7017SPing-Ke Shih 
2548e3ec7017SPing-Ke Shih 	if (rtwsta->prev_rssi == 0) {
2549e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
2550e3ec7017SPing-Ke Shih 	} else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) {
2551e3ec7017SPing-Ke Shih 		rtwsta->prev_rssi = rssi_curr;
2552e3ec7017SPing-Ke Shih 		rssi_data->rssi_changed = true;
2553e3ec7017SPing-Ke Shih 	}
2554e3ec7017SPing-Ke Shih }
2555e3ec7017SPing-Ke Shih 
2556e3ec7017SPing-Ke Shih static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev)
2557e3ec7017SPing-Ke Shih {
2558e3ec7017SPing-Ke Shih 	struct rtw89_phy_iter_rssi_data rssi_data = {0};
2559e3ec7017SPing-Ke Shih 
2560e3ec7017SPing-Ke Shih 	rssi_data.rtwdev = rtwdev;
2561e3ec7017SPing-Ke Shih 	rssi_data.ch_info = &rtwdev->ch_info;
2562e3ec7017SPing-Ke Shih 	rssi_data.ch_info->rssi_min = U8_MAX;
2563e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2564e3ec7017SPing-Ke Shih 					  rtw89_phy_stat_rssi_update_iter,
2565e3ec7017SPing-Ke Shih 					  &rssi_data);
2566e3ec7017SPing-Ke Shih 	if (rssi_data.rssi_changed)
2567e3ec7017SPing-Ke Shih 		rtw89_btc_ntfy_wl_sta(rtwdev);
2568e3ec7017SPing-Ke Shih }
2569e3ec7017SPing-Ke Shih 
2570e3ec7017SPing-Ke Shih static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev)
2571e3ec7017SPing-Ke Shih {
2572e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
2573e3ec7017SPing-Ke Shih 	int i;
2574e3ec7017SPing-Ke Shih 
2575e3ec7017SPing-Ke Shih 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
2576e3ec7017SPing-Ke Shih 		ewma_thermal_init(&phystat->avg_thermal[i]);
2577e3ec7017SPing-Ke Shih 
2578e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
2579e3ec7017SPing-Ke Shih 
2580e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
2581e3ec7017SPing-Ke Shih 	memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat));
2582e3ec7017SPing-Ke Shih }
2583e3ec7017SPing-Ke Shih 
2584e3ec7017SPing-Ke Shih void rtw89_phy_stat_track(struct rtw89_dev *rtwdev)
2585e3ec7017SPing-Ke Shih {
2586e3ec7017SPing-Ke Shih 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
2587e3ec7017SPing-Ke Shih 
2588e3ec7017SPing-Ke Shih 	rtw89_phy_stat_thermal_update(rtwdev);
2589e3ec7017SPing-Ke Shih 	rtw89_phy_stat_rssi_update(rtwdev);
2590e3ec7017SPing-Ke Shih 
2591e3ec7017SPing-Ke Shih 	phystat->last_pkt_stat = phystat->cur_pkt_stat;
2592e3ec7017SPing-Ke Shih 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
2593e3ec7017SPing-Ke Shih }
2594e3ec7017SPing-Ke Shih 
2595e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us)
2596e3ec7017SPing-Ke Shih {
2597e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2598e3ec7017SPing-Ke Shih 
2599e3ec7017SPing-Ke Shih 	return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
2600e3ec7017SPing-Ke Shih }
2601e3ec7017SPing-Ke Shih 
2602e3ec7017SPing-Ke Shih static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx)
2603e3ec7017SPing-Ke Shih {
2604e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2605e3ec7017SPing-Ke Shih 
2606e3ec7017SPing-Ke Shih 	return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
2607e3ec7017SPing-Ke Shih }
2608e3ec7017SPing-Ke Shih 
2609e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev)
2610e3ec7017SPing-Ke Shih {
2611e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2612e3ec7017SPing-Ke Shih 
2613e3ec7017SPing-Ke Shih 	env->ccx_manual_ctrl = false;
2614e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
2615e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
2616e3ec7017SPing-Ke Shih 	env->ccx_rpt_stamp = 0;
2617e3ec7017SPing-Ke Shih 	env->ccx_period = 0;
2618e3ec7017SPing-Ke Shih 	env->ccx_unit_idx = RTW89_CCX_32_US;
2619e3ec7017SPing-Ke Shih 	env->ccx_trigger_time = 0;
2620e3ec7017SPing-Ke Shih 	env->ccx_edcca_opt_bw_idx = RTW89_CCX_EDCCA_BW20_0;
2621e3ec7017SPing-Ke Shih 
2622e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1);
2623e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1);
2624e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
2625e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK,
2626e3ec7017SPing-Ke Shih 			       RTW89_CCX_EDCCA_BW20_0);
2627e3ec7017SPing-Ke Shih }
2628e3ec7017SPing-Ke Shih 
2629e3ec7017SPing-Ke Shih static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report,
2630e3ec7017SPing-Ke Shih 				    u16 score)
2631e3ec7017SPing-Ke Shih {
2632e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2633e3ec7017SPing-Ke Shih 	u32 numer = 0;
2634e3ec7017SPing-Ke Shih 	u16 ret = 0;
2635e3ec7017SPing-Ke Shih 
2636e3ec7017SPing-Ke Shih 	numer = report * score + (env->ccx_period >> 1);
2637e3ec7017SPing-Ke Shih 	if (env->ccx_period)
2638e3ec7017SPing-Ke Shih 		ret = numer / env->ccx_period;
2639e3ec7017SPing-Ke Shih 
2640e3ec7017SPing-Ke Shih 	return ret >= score ? score - 1 : ret;
2641e3ec7017SPing-Ke Shih }
2642e3ec7017SPing-Ke Shih 
2643e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev,
2644e3ec7017SPing-Ke Shih 					    u16 time_ms, u32 *period,
2645e3ec7017SPing-Ke Shih 					    u32 *unit_idx)
2646e3ec7017SPing-Ke Shih {
2647e3ec7017SPing-Ke Shih 	u32 idx;
2648e3ec7017SPing-Ke Shih 	u8 quotient;
2649e3ec7017SPing-Ke Shih 
2650e3ec7017SPing-Ke Shih 	if (time_ms >= CCX_MAX_PERIOD)
2651e3ec7017SPing-Ke Shih 		time_ms = CCX_MAX_PERIOD;
2652e3ec7017SPing-Ke Shih 
2653e3ec7017SPing-Ke Shih 	quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD;
2654e3ec7017SPing-Ke Shih 
2655e3ec7017SPing-Ke Shih 	if (quotient < 4)
2656e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_4_US;
2657e3ec7017SPing-Ke Shih 	else if (quotient < 8)
2658e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_8_US;
2659e3ec7017SPing-Ke Shih 	else if (quotient < 16)
2660e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_16_US;
2661e3ec7017SPing-Ke Shih 	else
2662e3ec7017SPing-Ke Shih 		idx = RTW89_CCX_32_US;
2663e3ec7017SPing-Ke Shih 
2664e3ec7017SPing-Ke Shih 	*unit_idx = idx;
2665e3ec7017SPing-Ke Shih 	*period = (time_ms * MS_TO_4US_RATIO) >> idx;
2666e3ec7017SPing-Ke Shih 
2667e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2668e3ec7017SPing-Ke Shih 		    "[Trigger Time] period:%d, unit_idx:%d\n",
2669e3ec7017SPing-Ke Shih 		    *period, *unit_idx);
2670e3ec7017SPing-Ke Shih }
2671e3ec7017SPing-Ke Shih 
2672e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev)
2673e3ec7017SPing-Ke Shih {
2674e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2675e3ec7017SPing-Ke Shih 
2676e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2677e3ec7017SPing-Ke Shih 		    "lv:(%d)->(0)\n", env->ccx_rac_lv);
2678e3ec7017SPing-Ke Shih 
2679e3ec7017SPing-Ke Shih 	env->ccx_ongoing = false;
2680e3ec7017SPing-Ke Shih 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
2681e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
2682e3ec7017SPing-Ke Shih }
2683e3ec7017SPing-Ke Shih 
2684e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev,
2685e3ec7017SPing-Ke Shih 					      struct rtw89_ccx_para_info *para)
2686e3ec7017SPing-Ke Shih {
2687e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2688e3ec7017SPing-Ke Shih 	bool is_update = env->ifs_clm_app != para->ifs_clm_app;
2689e3ec7017SPing-Ke Shih 	u8 i = 0;
2690e3ec7017SPing-Ke Shih 	u16 *ifs_th_l = env->ifs_clm_th_l;
2691e3ec7017SPing-Ke Shih 	u16 *ifs_th_h = env->ifs_clm_th_h;
2692e3ec7017SPing-Ke Shih 	u32 ifs_th0_us = 0, ifs_th_times = 0;
2693e3ec7017SPing-Ke Shih 	u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0};
2694e3ec7017SPing-Ke Shih 
2695e3ec7017SPing-Ke Shih 	if (!is_update)
2696e3ec7017SPing-Ke Shih 		goto ifs_update_finished;
2697e3ec7017SPing-Ke Shih 
2698e3ec7017SPing-Ke Shih 	switch (para->ifs_clm_app) {
2699e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_INIT:
2700e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_BACKGROUND:
2701e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_ACS:
2702e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG:
2703e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DIG:
2704e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_TDMA_DIG:
2705e3ec7017SPing-Ke Shih 		ifs_th0_us = IFS_CLM_TH0_UPPER;
2706e3ec7017SPing-Ke Shih 		ifs_th_times = IFS_CLM_TH_MUL;
2707e3ec7017SPing-Ke Shih 		break;
2708e3ec7017SPing-Ke Shih 	case RTW89_IFS_CLM_DBG_MANUAL:
2709e3ec7017SPing-Ke Shih 		ifs_th0_us = para->ifs_clm_manual_th0;
2710e3ec7017SPing-Ke Shih 		ifs_th_times = para->ifs_clm_manual_th_times;
2711e3ec7017SPing-Ke Shih 		break;
2712e3ec7017SPing-Ke Shih 	default:
2713e3ec7017SPing-Ke Shih 		break;
2714e3ec7017SPing-Ke Shih 	}
2715e3ec7017SPing-Ke Shih 
2716e3ec7017SPing-Ke Shih 	/* Set sampling threshold for 4 different regions, unit in idx_cnt.
2717e3ec7017SPing-Ke Shih 	 * low[i] = high[i-1] + 1
2718e3ec7017SPing-Ke Shih 	 * high[i] = high[i-1] * ifs_th_times
2719e3ec7017SPing-Ke Shih 	 */
2720e3ec7017SPing-Ke Shih 	ifs_th_l[IFS_CLM_TH_START_IDX] = 0;
2721e3ec7017SPing-Ke Shih 	ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us;
2722e3ec7017SPing-Ke Shih 	ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev,
2723e3ec7017SPing-Ke Shih 								 ifs_th0_us);
2724e3ec7017SPing-Ke Shih 	for (i = 1; i < RTW89_IFS_CLM_NUM; i++) {
2725e3ec7017SPing-Ke Shih 		ifs_th_l[i] = ifs_th_h[i - 1] + 1;
2726e3ec7017SPing-Ke Shih 		ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times;
2727e3ec7017SPing-Ke Shih 		ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]);
2728e3ec7017SPing-Ke Shih 	}
2729e3ec7017SPing-Ke Shih 
2730e3ec7017SPing-Ke Shih ifs_update_finished:
2731e3ec7017SPing-Ke Shih 	if (!is_update)
2732e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2733e3ec7017SPing-Ke Shih 			    "No need to update IFS_TH\n");
2734e3ec7017SPing-Ke Shih 
2735e3ec7017SPing-Ke Shih 	return is_update;
2736e3ec7017SPing-Ke Shih }
2737e3ec7017SPing-Ke Shih 
2738e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev)
2739e3ec7017SPing-Ke Shih {
2740e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2741e3ec7017SPing-Ke Shih 	u8 i = 0;
2742e3ec7017SPing-Ke Shih 
2743e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK,
2744e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[0]);
2745e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK,
2746e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[1]);
2747e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK,
2748e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[2]);
2749e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK,
2750e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_l[3]);
2751e3ec7017SPing-Ke Shih 
2752e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK,
2753e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[0]);
2754e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK,
2755e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[1]);
2756e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK,
2757e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[2]);
2758e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK,
2759e3ec7017SPing-Ke Shih 			       env->ifs_clm_th_h[3]);
2760e3ec7017SPing-Ke Shih 
2761e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
2762e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2763e3ec7017SPing-Ke Shih 			    "Update IFS_T%d_th{low, high} : {%d, %d}\n",
2764e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]);
2765e3ec7017SPing-Ke Shih }
2766e3ec7017SPing-Ke Shih 
2767e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev)
2768e3ec7017SPing-Ke Shih {
2769e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2770e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
2771e3ec7017SPing-Ke Shih 
2772e3ec7017SPing-Ke Shih 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
2773e3ec7017SPing-Ke Shih 	env->ifs_clm_mntr_time = 0;
2774e3ec7017SPing-Ke Shih 
2775e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_INIT;
2776e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, &para))
2777e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
2778e3ec7017SPing-Ke Shih 
2779e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN,
2780e3ec7017SPing-Ke Shih 			       true);
2781e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true);
2782e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true);
2783e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true);
2784e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true);
2785e3ec7017SPing-Ke Shih }
2786e3ec7017SPing-Ke Shih 
2787e3ec7017SPing-Ke Shih static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev,
2788e3ec7017SPing-Ke Shih 				     enum rtw89_env_racing_lv level)
2789e3ec7017SPing-Ke Shih {
2790e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2791e3ec7017SPing-Ke Shih 	int ret = 0;
2792e3ec7017SPing-Ke Shih 
2793e3ec7017SPing-Ke Shih 	if (level >= RTW89_RAC_MAX_NUM) {
2794e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2795e3ec7017SPing-Ke Shih 			    "[WARNING] Wrong LV=%d\n", level);
2796e3ec7017SPing-Ke Shih 		return -EINVAL;
2797e3ec7017SPing-Ke Shih 	}
2798e3ec7017SPing-Ke Shih 
2799e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2800e3ec7017SPing-Ke Shih 		    "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing,
2801e3ec7017SPing-Ke Shih 		    env->ccx_rac_lv, level);
2802e3ec7017SPing-Ke Shih 
2803e3ec7017SPing-Ke Shih 	if (env->ccx_ongoing) {
2804e3ec7017SPing-Ke Shih 		if (level <= env->ccx_rac_lv)
2805e3ec7017SPing-Ke Shih 			ret = -EINVAL;
2806e3ec7017SPing-Ke Shih 		else
2807e3ec7017SPing-Ke Shih 			env->ccx_ongoing = false;
2808e3ec7017SPing-Ke Shih 	}
2809e3ec7017SPing-Ke Shih 
2810e3ec7017SPing-Ke Shih 	if (ret == 0)
2811e3ec7017SPing-Ke Shih 		env->ccx_rac_lv = level;
2812e3ec7017SPing-Ke Shih 
2813e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n",
2814e3ec7017SPing-Ke Shih 		    !ret);
2815e3ec7017SPing-Ke Shih 
2816e3ec7017SPing-Ke Shih 	return ret;
2817e3ec7017SPing-Ke Shih }
2818e3ec7017SPing-Ke Shih 
2819e3ec7017SPing-Ke Shih static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev)
2820e3ec7017SPing-Ke Shih {
2821e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2822e3ec7017SPing-Ke Shih 
2823e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0);
2824e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0);
2825e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1);
2826e3ec7017SPing-Ke Shih 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
2827e3ec7017SPing-Ke Shih 
2828e3ec7017SPing-Ke Shih 	env->ccx_rpt_stamp++;
2829e3ec7017SPing-Ke Shih 	env->ccx_ongoing = true;
2830e3ec7017SPing-Ke Shih }
2831e3ec7017SPing-Ke Shih 
2832e3ec7017SPing-Ke Shih static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev)
2833e3ec7017SPing-Ke Shih {
2834e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2835e3ec7017SPing-Ke Shih 	u8 i = 0;
2836e3ec7017SPing-Ke Shih 	u32 res = 0;
2837e3ec7017SPing-Ke Shih 
2838e3ec7017SPing-Ke Shih 	env->ifs_clm_tx_ratio =
2839e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT);
2840e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca_ratio =
2841e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca,
2842e3ec7017SPing-Ke Shih 					 PERCENT);
2843e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_ratio =
2844e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT);
2845e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_ratio =
2846e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT);
2847e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_cca_excl_fa_ratio =
2848e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa,
2849e3ec7017SPing-Ke Shih 					 PERCENT);
2850e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_cca_excl_fa_ratio =
2851e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa,
2852e3ec7017SPing-Ke Shih 					 PERCENT);
2853e3ec7017SPing-Ke Shih 	env->ifs_clm_cck_fa_permil =
2854e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL);
2855e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdm_fa_permil =
2856e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL);
2857e3ec7017SPing-Ke Shih 
2858e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++) {
2859e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) {
2860e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD;
2861e3ec7017SPing-Ke Shih 		} else {
2862e3ec7017SPing-Ke Shih 			env->ifs_clm_ifs_avg[i] =
2863e3ec7017SPing-Ke Shih 				rtw89_phy_ccx_idx_to_us(rtwdev,
2864e3ec7017SPing-Ke Shih 							env->ifs_clm_avg[i]);
2865e3ec7017SPing-Ke Shih 		}
2866e3ec7017SPing-Ke Shih 
2867e3ec7017SPing-Ke Shih 		res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]);
2868e3ec7017SPing-Ke Shih 		res += env->ifs_clm_his[i] >> 1;
2869e3ec7017SPing-Ke Shih 		if (env->ifs_clm_his[i])
2870e3ec7017SPing-Ke Shih 			res /= env->ifs_clm_his[i];
2871e3ec7017SPing-Ke Shih 		else
2872e3ec7017SPing-Ke Shih 			res = 0;
2873e3ec7017SPing-Ke Shih 		env->ifs_clm_cca_avg[i] = res;
2874e3ec7017SPing-Ke Shih 	}
2875e3ec7017SPing-Ke Shih 
2876e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2877e3ec7017SPing-Ke Shih 		    "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n",
2878e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio);
2879e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2880e3ec7017SPing-Ke Shih 		    "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n",
2881e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio);
2882e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2883e3ec7017SPing-Ke Shih 		    "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n",
2884e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil);
2885e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2886e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n",
2887e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_cca_excl_fa_ratio,
2888e3ec7017SPing-Ke Shih 		    env->ifs_clm_ofdm_cca_excl_fa_ratio);
2889e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2890e3ec7017SPing-Ke Shih 		    "Time:[his, ifs_avg(us), cca_avg(us)]\n");
2891e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
2892e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n",
2893e3ec7017SPing-Ke Shih 			    i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i],
2894e3ec7017SPing-Ke Shih 			    env->ifs_clm_cca_avg[i]);
2895e3ec7017SPing-Ke Shih }
2896e3ec7017SPing-Ke Shih 
2897e3ec7017SPing-Ke Shih static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev)
2898e3ec7017SPing-Ke Shih {
2899e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2900e3ec7017SPing-Ke Shih 	u8 i = 0;
2901e3ec7017SPing-Ke Shih 
2902e3ec7017SPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) {
2903e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2904e3ec7017SPing-Ke Shih 			    "Get IFS_CLM report Fail\n");
2905e3ec7017SPing-Ke Shih 		return false;
2906e3ec7017SPing-Ke Shih 	}
2907e3ec7017SPing-Ke Shih 
2908e3ec7017SPing-Ke Shih 	env->ifs_clm_tx =
2909e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
2910e3ec7017SPing-Ke Shih 				      B_IFS_CLM_TX_CNT_MSK);
2911e3ec7017SPing-Ke Shih 	env->ifs_clm_edcca_excl_cca =
2912e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
2913e3ec7017SPing-Ke Shih 				      B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK);
2914e3ec7017SPing-Ke Shih 	env->ifs_clm_cckcca_excl_fa =
2915e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
2916e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK);
2917e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmcca_excl_fa =
2918e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
2919e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK);
2920e3ec7017SPing-Ke Shih 	env->ifs_clm_cckfa =
2921e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
2922e3ec7017SPing-Ke Shih 				      B_IFS_CLM_CCK_FA_MSK);
2923e3ec7017SPing-Ke Shih 	env->ifs_clm_ofdmfa =
2924e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
2925e3ec7017SPing-Ke Shih 				      B_IFS_CLM_OFDM_FA_MSK);
2926e3ec7017SPing-Ke Shih 
2927e3ec7017SPing-Ke Shih 	env->ifs_clm_his[0] =
2928e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK);
2929e3ec7017SPing-Ke Shih 	env->ifs_clm_his[1] =
2930e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK);
2931e3ec7017SPing-Ke Shih 	env->ifs_clm_his[2] =
2932e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK);
2933e3ec7017SPing-Ke Shih 	env->ifs_clm_his[3] =
2934e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK);
2935e3ec7017SPing-Ke Shih 
2936e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[0] =
2937e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK);
2938e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[1] =
2939e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK);
2940e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[2] =
2941e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK);
2942e3ec7017SPing-Ke Shih 	env->ifs_clm_avg[3] =
2943e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK);
2944e3ec7017SPing-Ke Shih 
2945e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[0] =
2946e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK);
2947e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[1] =
2948e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK);
2949e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[2] =
2950e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK);
2951e3ec7017SPing-Ke Shih 	env->ifs_clm_cca[3] =
2952e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK);
2953e3ec7017SPing-Ke Shih 
2954e3ec7017SPing-Ke Shih 	env->ifs_clm_total_ifs =
2955e3ec7017SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK);
2956e3ec7017SPing-Ke Shih 
2957e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n",
2958e3ec7017SPing-Ke Shih 		    env->ifs_clm_total_ifs);
2959e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2960e3ec7017SPing-Ke Shih 		    "{Tx, EDCCA_exclu_cca} = {%d, %d}\n",
2961e3ec7017SPing-Ke Shih 		    env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca);
2962e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2963e3ec7017SPing-Ke Shih 		    "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n",
2964e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckfa, env->ifs_clm_ofdmfa);
2965e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2966e3ec7017SPing-Ke Shih 		    "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n",
2967e3ec7017SPing-Ke Shih 		    env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa);
2968e3ec7017SPing-Ke Shih 
2969e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n");
2970e3ec7017SPing-Ke Shih 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
2971e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2972e3ec7017SPing-Ke Shih 			    "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i],
2973e3ec7017SPing-Ke Shih 			    env->ifs_clm_avg[i], env->ifs_clm_cca[i]);
2974e3ec7017SPing-Ke Shih 
2975e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_get_utility(rtwdev);
2976e3ec7017SPing-Ke Shih 
2977e3ec7017SPing-Ke Shih 	return true;
2978e3ec7017SPing-Ke Shih }
2979e3ec7017SPing-Ke Shih 
2980e3ec7017SPing-Ke Shih static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev,
2981e3ec7017SPing-Ke Shih 				 struct rtw89_ccx_para_info *para)
2982e3ec7017SPing-Ke Shih {
2983e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
2984e3ec7017SPing-Ke Shih 	u32 period = 0;
2985e3ec7017SPing-Ke Shih 	u32 unit_idx = 0;
2986e3ec7017SPing-Ke Shih 
2987e3ec7017SPing-Ke Shih 	if (para->mntr_time == 0) {
2988e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
2989e3ec7017SPing-Ke Shih 			    "[WARN] MNTR_TIME is 0\n");
2990e3ec7017SPing-Ke Shih 		return -EINVAL;
2991e3ec7017SPing-Ke Shih 	}
2992e3ec7017SPing-Ke Shih 
2993e3ec7017SPing-Ke Shih 	if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv))
2994e3ec7017SPing-Ke Shih 		return -EINVAL;
2995e3ec7017SPing-Ke Shih 
2996e3ec7017SPing-Ke Shih 	if (para->mntr_time != env->ifs_clm_mntr_time) {
2997e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time,
2998e3ec7017SPing-Ke Shih 						&period, &unit_idx);
2999e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3000e3ec7017SPing-Ke Shih 				       B_IFS_CLM_PERIOD_MSK, period);
3001e3ec7017SPing-Ke Shih 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3002e3ec7017SPing-Ke Shih 				       B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx);
3003e3ec7017SPing-Ke Shih 
3004e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3005e3ec7017SPing-Ke Shih 			    "Update IFS-CLM time ((%d)) -> ((%d))\n",
3006e3ec7017SPing-Ke Shih 			    env->ifs_clm_mntr_time, para->mntr_time);
3007e3ec7017SPing-Ke Shih 
3008e3ec7017SPing-Ke Shih 		env->ifs_clm_mntr_time = para->mntr_time;
3009e3ec7017SPing-Ke Shih 		env->ccx_period = (u16)period;
3010e3ec7017SPing-Ke Shih 		env->ccx_unit_idx = (u8)unit_idx;
3011e3ec7017SPing-Ke Shih 	}
3012e3ec7017SPing-Ke Shih 
3013e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) {
3014e3ec7017SPing-Ke Shih 		env->ifs_clm_app = para->ifs_clm_app;
3015e3ec7017SPing-Ke Shih 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3016e3ec7017SPing-Ke Shih 	}
3017e3ec7017SPing-Ke Shih 
3018e3ec7017SPing-Ke Shih 	return 0;
3019e3ec7017SPing-Ke Shih }
3020e3ec7017SPing-Ke Shih 
3021e3ec7017SPing-Ke Shih void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev)
3022e3ec7017SPing-Ke Shih {
3023e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3024e3ec7017SPing-Ke Shih 	struct rtw89_ccx_para_info para = {0};
3025e3ec7017SPing-Ke Shih 	u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3026e3ec7017SPing-Ke Shih 
3027e3ec7017SPing-Ke Shih 	env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3028e3ec7017SPing-Ke Shih 	if (env->ccx_manual_ctrl) {
3029e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3030e3ec7017SPing-Ke Shih 			    "CCX in manual ctrl\n");
3031e3ec7017SPing-Ke Shih 		return;
3032e3ec7017SPing-Ke Shih 	}
3033e3ec7017SPing-Ke Shih 
3034e3ec7017SPing-Ke Shih 	/* only ifs_clm for now */
3035e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_get_result(rtwdev))
3036e3ec7017SPing-Ke Shih 		env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3037e3ec7017SPing-Ke Shih 
3038e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_racing_release(rtwdev);
3039e3ec7017SPing-Ke Shih 	para.mntr_time = 1900;
3040e3ec7017SPing-Ke Shih 	para.rac_lv = RTW89_RAC_LV_1;
3041e3ec7017SPing-Ke Shih 	para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3042e3ec7017SPing-Ke Shih 
3043e3ec7017SPing-Ke Shih 	if (rtw89_phy_ifs_clm_set(rtwdev, &para) == 0)
3044e3ec7017SPing-Ke Shih 		chk_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3045e3ec7017SPing-Ke Shih 	if (chk_result)
3046e3ec7017SPing-Ke Shih 		rtw89_phy_ccx_trigger(rtwdev);
3047e3ec7017SPing-Ke Shih 
3048e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3049e3ec7017SPing-Ke Shih 		    "get_result=0x%x, chk_result:0x%x\n",
3050e3ec7017SPing-Ke Shih 		    env->ccx_watchdog_result, chk_result);
3051e3ec7017SPing-Ke Shih }
3052e3ec7017SPing-Ke Shih 
3053eb4e52b3SPo Hao Huang static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page)
3054eb4e52b3SPo Hao Huang {
3055eb4e52b3SPo Hao Huang 	if (*ie_page > RTW89_PHYSTS_BITMAP_NUM ||
3056eb4e52b3SPo Hao Huang 	    *ie_page == RTW89_RSVD_9)
3057eb4e52b3SPo Hao Huang 		return false;
3058eb4e52b3SPo Hao Huang 	else if (*ie_page > RTW89_RSVD_9)
3059eb4e52b3SPo Hao Huang 		*ie_page -= 1;
3060eb4e52b3SPo Hao Huang 
3061eb4e52b3SPo Hao Huang 	return true;
3062eb4e52b3SPo Hao Huang }
3063eb4e52b3SPo Hao Huang 
3064eb4e52b3SPo Hao Huang static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)
3065eb4e52b3SPo Hao Huang {
3066eb4e52b3SPo Hao Huang 	static const u8 ie_page_shift = 2;
3067eb4e52b3SPo Hao Huang 
3068eb4e52b3SPo Hao Huang 	return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift);
3069eb4e52b3SPo Hao Huang }
3070eb4e52b3SPo Hao Huang 
3071eb4e52b3SPo Hao Huang static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev,
3072eb4e52b3SPo Hao Huang 				      enum rtw89_phy_status_bitmap ie_page)
3073eb4e52b3SPo Hao Huang {
3074eb4e52b3SPo Hao Huang 	u32 addr;
3075eb4e52b3SPo Hao Huang 
3076eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3077eb4e52b3SPo Hao Huang 		return 0;
3078eb4e52b3SPo Hao Huang 
3079eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3080eb4e52b3SPo Hao Huang 
3081eb4e52b3SPo Hao Huang 	return rtw89_phy_read32(rtwdev, addr);
3082eb4e52b3SPo Hao Huang }
3083eb4e52b3SPo Hao Huang 
3084eb4e52b3SPo Hao Huang static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev,
3085eb4e52b3SPo Hao Huang 				       enum rtw89_phy_status_bitmap ie_page,
3086eb4e52b3SPo Hao Huang 				       u32 val)
3087eb4e52b3SPo Hao Huang {
3088eb4e52b3SPo Hao Huang 	const struct rtw89_chip_info *chip = rtwdev->chip;
3089eb4e52b3SPo Hao Huang 	u32 addr;
3090eb4e52b3SPo Hao Huang 
3091eb4e52b3SPo Hao Huang 	if (!rtw89_physts_ie_page_valid(&ie_page))
3092eb4e52b3SPo Hao Huang 		return;
3093eb4e52b3SPo Hao Huang 
3094eb4e52b3SPo Hao Huang 	if (chip->chip_id == RTL8852A)
3095eb4e52b3SPo Hao Huang 		val &= B_PHY_STS_BITMAP_MSK_52A;
3096eb4e52b3SPo Hao Huang 
3097eb4e52b3SPo Hao Huang 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3098eb4e52b3SPo Hao Huang 	rtw89_phy_write32(rtwdev, addr, val);
3099eb4e52b3SPo Hao Huang }
3100eb4e52b3SPo Hao Huang 
3101eb4e52b3SPo Hao Huang static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev,
3102eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_bitmap bitmap,
3103eb4e52b3SPo Hao Huang 					  enum rtw89_phy_status_ie_type ie,
3104eb4e52b3SPo Hao Huang 					  bool enable)
3105eb4e52b3SPo Hao Huang {
3106eb4e52b3SPo Hao Huang 	u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap);
3107eb4e52b3SPo Hao Huang 
3108eb4e52b3SPo Hao Huang 	if (enable)
3109eb4e52b3SPo Hao Huang 		val |= BIT(ie);
3110eb4e52b3SPo Hao Huang 	else
3111eb4e52b3SPo Hao Huang 		val &= ~BIT(ie);
3112eb4e52b3SPo Hao Huang 
3113eb4e52b3SPo Hao Huang 	rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val);
3114eb4e52b3SPo Hao Huang }
3115eb4e52b3SPo Hao Huang 
3116eb4e52b3SPo Hao Huang static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev,
3117eb4e52b3SPo Hao Huang 					    bool enable,
3118eb4e52b3SPo Hao Huang 					    enum rtw89_phy_idx phy_idx)
3119eb4e52b3SPo Hao Huang {
3120eb4e52b3SPo Hao Huang 	if (enable) {
3121eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3122eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3123eb4e52b3SPo Hao Huang 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3124eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3125eb4e52b3SPo Hao Huang 	} else {
3126eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3127eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_FAIL);
3128eb4e52b3SPo Hao Huang 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3129eb4e52b3SPo Hao Huang 				      B_STS_DIS_TRIG_BY_BRK);
3130eb4e52b3SPo Hao Huang 	}
3131eb4e52b3SPo Hao Huang }
3132eb4e52b3SPo Hao Huang 
3133eb4e52b3SPo Hao Huang static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev)
3134eb4e52b3SPo Hao Huang {
3135eb4e52b3SPo Hao Huang 	u8 i;
3136eb4e52b3SPo Hao Huang 
3137eb4e52b3SPo Hao Huang 	rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0);
3138eb4e52b3SPo Hao Huang 
3139eb4e52b3SPo Hao Huang 	for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) {
3140eb4e52b3SPo Hao Huang 		if (i >= RTW89_CCK_PKT)
3141eb4e52b3SPo Hao Huang 			rtw89_physts_enable_ie_bitmap(rtwdev, i,
3142eb4e52b3SPo Hao Huang 						      RTW89_PHYSTS_IE09_FTR_0,
3143eb4e52b3SPo Hao Huang 						      true);
3144eb4e52b3SPo Hao Huang 		if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) ||
3145eb4e52b3SPo Hao Huang 		    (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT))
3146eb4e52b3SPo Hao Huang 			continue;
3147eb4e52b3SPo Hao Huang 		rtw89_physts_enable_ie_bitmap(rtwdev, i,
3148eb4e52b3SPo Hao Huang 					      RTW89_PHYSTS_IE24_OFDM_TD_PATH_A,
3149eb4e52b3SPo Hao Huang 					      true);
3150eb4e52b3SPo Hao Huang 	}
3151eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT,
3152eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3153eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT,
3154eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3155eb4e52b3SPo Hao Huang 
3156eb4e52b3SPo Hao Huang 	/* force IE01 for channel index, only channel field is valid */
3157eb4e52b3SPo Hao Huang 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT,
3158eb4e52b3SPo Hao Huang 				      RTW89_PHYSTS_IE01_CMN_OFDM, true);
3159eb4e52b3SPo Hao Huang }
3160eb4e52b3SPo Hao Huang 
3161e3ec7017SPing-Ke Shih static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type)
3162e3ec7017SPing-Ke Shih {
3163e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3164e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3165e3ec7017SPing-Ke Shih 	const struct rtw89_phy_dig_gain_cfg *cfg;
3166e3ec7017SPing-Ke Shih 	const char *msg;
3167e3ec7017SPing-Ke Shih 	u8 i;
3168e3ec7017SPing-Ke Shih 	s8 gain_base;
3169e3ec7017SPing-Ke Shih 	s8 *gain_arr;
3170e3ec7017SPing-Ke Shih 	u32 tmp;
3171e3ec7017SPing-Ke Shih 
3172e3ec7017SPing-Ke Shih 	switch (type) {
3173e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_G:
3174e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_g;
3175e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3176e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_g;
3177e3ec7017SPing-Ke Shih 		msg = "lna_gain_g";
3178e3ec7017SPing-Ke Shih 		break;
3179e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_G:
3180e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_g;
3181e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_G;
3182e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_g;
3183e3ec7017SPing-Ke Shih 		msg = "tia_gain_g";
3184e3ec7017SPing-Ke Shih 		break;
3185e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_LNA_A:
3186e3ec7017SPing-Ke Shih 		gain_arr = dig->lna_gain_a;
3187e3ec7017SPing-Ke Shih 		gain_base = LNA0_GAIN;
3188e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_lna_a;
3189e3ec7017SPing-Ke Shih 		msg = "lna_gain_a";
3190e3ec7017SPing-Ke Shih 		break;
3191e3ec7017SPing-Ke Shih 	case RTW89_DIG_GAIN_TIA_A:
3192e3ec7017SPing-Ke Shih 		gain_arr = dig->tia_gain_a;
3193e3ec7017SPing-Ke Shih 		gain_base = TIA0_GAIN_A;
3194e3ec7017SPing-Ke Shih 		cfg = chip->dig_table->cfg_tia_a;
3195e3ec7017SPing-Ke Shih 		msg = "tia_gain_a";
3196e3ec7017SPing-Ke Shih 		break;
3197e3ec7017SPing-Ke Shih 	default:
3198e3ec7017SPing-Ke Shih 		return;
3199e3ec7017SPing-Ke Shih 	}
3200e3ec7017SPing-Ke Shih 
3201e3ec7017SPing-Ke Shih 	for (i = 0; i < cfg->size; i++) {
3202e3ec7017SPing-Ke Shih 		tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr,
3203e3ec7017SPing-Ke Shih 					    cfg->table[i].mask);
3204e3ec7017SPing-Ke Shih 		tmp >>= DIG_GAIN_SHIFT;
3205e3ec7017SPing-Ke Shih 		gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base;
3206e3ec7017SPing-Ke Shih 		gain_base += DIG_GAIN;
3207e3ec7017SPing-Ke Shih 
3208e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n",
3209e3ec7017SPing-Ke Shih 			    msg, i, gain_arr[i]);
3210e3ec7017SPing-Ke Shih 	}
3211e3ec7017SPing-Ke Shih }
3212e3ec7017SPing-Ke Shih 
3213e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev)
3214e3ec7017SPing-Ke Shih {
3215e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3216e3ec7017SPing-Ke Shih 	u32 tmp;
3217e3ec7017SPing-Ke Shih 	u8 i;
3218e3ec7017SPing-Ke Shih 
3219d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
3220d264edb1SJohnson Lin 		return;
3221d264edb1SJohnson Lin 
3222e3ec7017SPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW,
3223e3ec7017SPing-Ke Shih 				    B_PATH0_IB_PKPW_MSK);
3224e3ec7017SPing-Ke Shih 	dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT);
3225e3ec7017SPing-Ke Shih 	dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK,
3226e3ec7017SPing-Ke Shih 					    B_PATH0_IB_PBK_MSK);
3227e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n",
3228e3ec7017SPing-Ke Shih 		    dig->ib_pkpwr, dig->ib_pbk);
3229e3ec7017SPing-Ke Shih 
3230e3ec7017SPing-Ke Shih 	for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++)
3231e3ec7017SPing-Ke Shih 		rtw89_phy_dig_read_gain_table(rtwdev, i);
3232e3ec7017SPing-Ke Shih }
3233e3ec7017SPing-Ke Shih 
3234e3ec7017SPing-Ke Shih static const u8 rssi_nolink = 22;
3235e3ec7017SPing-Ke Shih static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104};
3236e3ec7017SPing-Ke Shih static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88};
3237e3ec7017SPing-Ke Shih static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16};
3238e3ec7017SPing-Ke Shih static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528};
3239e3ec7017SPing-Ke Shih 
3240e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev)
3241e3ec7017SPing-Ke Shih {
3242e3ec7017SPing-Ke Shih 	struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info;
3243e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3244e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3245e3ec7017SPing-Ke Shih 
3246e3ec7017SPing-Ke Shih 	if (is_linked) {
3247e3ec7017SPing-Ke Shih 		dig->igi_rssi = ch_info->rssi_min >> 1;
3248e3ec7017SPing-Ke Shih 	} else {
3249e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n");
3250e3ec7017SPing-Ke Shih 		dig->igi_rssi = rssi_nolink;
3251e3ec7017SPing-Ke Shih 	}
3252e3ec7017SPing-Ke Shih }
3253e3ec7017SPing-Ke Shih 
3254e3ec7017SPing-Ke Shih static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev)
3255e3ec7017SPing-Ke Shih {
3256e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3257*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3258e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3259e3ec7017SPing-Ke Shih 	const u16 *fa_th_src = NULL;
3260e3ec7017SPing-Ke Shih 
3261*cbb145b9SZong-Zhe Yang 	switch (chan->band_type) {
3262e3ec7017SPing-Ke Shih 	case RTW89_BAND_2G:
3263e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_g;
3264e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_g;
3265e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_2g : fa_th_nolink;
3266e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = false;
3267e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3268e3ec7017SPing-Ke Shih 		break;
3269e3ec7017SPing-Ke Shih 	case RTW89_BAND_5G:
3270e3ec7017SPing-Ke Shih 	default:
3271e3ec7017SPing-Ke Shih 		dig->lna_gain = dig->lna_gain_a;
3272e3ec7017SPing-Ke Shih 		dig->tia_gain = dig->tia_gain_a;
3273e3ec7017SPing-Ke Shih 		fa_th_src = is_linked ? fa_th_5g : fa_th_nolink;
3274e3ec7017SPing-Ke Shih 		dig->force_gaincode_idx_en = true;
3275e3ec7017SPing-Ke Shih 		dig->dyn_pd_th_en = true;
3276e3ec7017SPing-Ke Shih 		break;
3277e3ec7017SPing-Ke Shih 	}
3278e3ec7017SPing-Ke Shih 	memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th));
3279e3ec7017SPing-Ke Shih 	memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th));
3280e3ec7017SPing-Ke Shih }
3281e3ec7017SPing-Ke Shih 
3282e3ec7017SPing-Ke Shih static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20;
3283e3ec7017SPing-Ke Shih static const u8 igi_max_performance_mode = 0x5a;
3284e3ec7017SPing-Ke Shih static const u8 dynamic_pd_threshold_max;
3285e3ec7017SPing-Ke Shih 
3286e3ec7017SPing-Ke Shih static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev)
3287e3ec7017SPing-Ke Shih {
3288e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3289e3ec7017SPing-Ke Shih 
3290e3ec7017SPing-Ke Shih 	dig->cur_gaincode.lna_idx = LNA_IDX_MAX;
3291e3ec7017SPing-Ke Shih 	dig->cur_gaincode.tia_idx = TIA_IDX_MAX;
3292e3ec7017SPing-Ke Shih 	dig->cur_gaincode.rxb_idx = RXB_IDX_MAX;
3293e3ec7017SPing-Ke Shih 	dig->force_gaincode.lna_idx = LNA_IDX_MAX;
3294e3ec7017SPing-Ke Shih 	dig->force_gaincode.tia_idx = TIA_IDX_MAX;
3295e3ec7017SPing-Ke Shih 	dig->force_gaincode.rxb_idx = RXB_IDX_MAX;
3296e3ec7017SPing-Ke Shih 
3297e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = igi_max_performance_mode;
3298e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = dynamic_igi_min;
3299e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dynamic_pd_threshold_max;
3300e3ec7017SPing-Ke Shih 	dig->pd_low_th_ofst = pd_low_th_offset;
3301e3ec7017SPing-Ke Shih 	dig->is_linked_pre = false;
3302e3ec7017SPing-Ke Shih }
3303e3ec7017SPing-Ke Shih 
3304e3ec7017SPing-Ke Shih static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev)
3305e3ec7017SPing-Ke Shih {
3306e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_gain_para(rtwdev);
3307e3ec7017SPing-Ke Shih 	rtw89_phy_dig_reset(rtwdev);
3308e3ec7017SPing-Ke Shih }
3309e3ec7017SPing-Ke Shih 
3310e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3311e3ec7017SPing-Ke Shih {
3312e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3313e3ec7017SPing-Ke Shih 	u8 lna_idx;
3314e3ec7017SPing-Ke Shih 
3315e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3316e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX6;
3317e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[1])
3318e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX5;
3319e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[2])
3320e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX4;
3321e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[3])
3322e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX3;
3323e3ec7017SPing-Ke Shih 	else if (rssi < dig->igi_rssi_th[4])
3324e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX2;
3325e3ec7017SPing-Ke Shih 	else
3326e3ec7017SPing-Ke Shih 		lna_idx = RTW89_DIG_GAIN_LNA_IDX1;
3327e3ec7017SPing-Ke Shih 
3328e3ec7017SPing-Ke Shih 	return lna_idx;
3329e3ec7017SPing-Ke Shih }
3330e3ec7017SPing-Ke Shih 
3331e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3332e3ec7017SPing-Ke Shih {
3333e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3334e3ec7017SPing-Ke Shih 	u8 tia_idx;
3335e3ec7017SPing-Ke Shih 
3336e3ec7017SPing-Ke Shih 	if (rssi < dig->igi_rssi_th[0])
3337e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX1;
3338e3ec7017SPing-Ke Shih 	else
3339e3ec7017SPing-Ke Shih 		tia_idx = RTW89_DIG_GAIN_TIA_IDX0;
3340e3ec7017SPing-Ke Shih 
3341e3ec7017SPing-Ke Shih 	return tia_idx;
3342e3ec7017SPing-Ke Shih }
3343e3ec7017SPing-Ke Shih 
3344e3ec7017SPing-Ke Shih #define IB_PBK_BASE 110
3345e3ec7017SPing-Ke Shih #define WB_RSSI_BASE 10
3346e3ec7017SPing-Ke Shih static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3347e3ec7017SPing-Ke Shih 					struct rtw89_agc_gaincode_set *set)
3348e3ec7017SPing-Ke Shih {
3349e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3350e3ec7017SPing-Ke Shih 	s8 lna_gain = dig->lna_gain[set->lna_idx];
3351e3ec7017SPing-Ke Shih 	s8 tia_gain = dig->tia_gain[set->tia_idx];
3352e3ec7017SPing-Ke Shih 	s32 wb_rssi = rssi + lna_gain + tia_gain;
3353e3ec7017SPing-Ke Shih 	s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE;
3354e3ec7017SPing-Ke Shih 	u8 rxb_idx;
3355e3ec7017SPing-Ke Shih 
3356e3ec7017SPing-Ke Shih 	rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi;
3357e3ec7017SPing-Ke Shih 	rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX);
3358e3ec7017SPing-Ke Shih 
3359e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n",
3360e3ec7017SPing-Ke Shih 		    wb_rssi, rxb_idx_tmp);
3361e3ec7017SPing-Ke Shih 
3362e3ec7017SPing-Ke Shih 	return rxb_idx;
3363e3ec7017SPing-Ke Shih }
3364e3ec7017SPing-Ke Shih 
3365e3ec7017SPing-Ke Shih static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3366e3ec7017SPing-Ke Shih 					   struct rtw89_agc_gaincode_set *set)
3367e3ec7017SPing-Ke Shih {
3368e3ec7017SPing-Ke Shih 	set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi);
3369e3ec7017SPing-Ke Shih 	set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi);
3370e3ec7017SPing-Ke Shih 	set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set);
3371e3ec7017SPing-Ke Shih 
3372e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3373e3ec7017SPing-Ke Shih 		    "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n",
3374e3ec7017SPing-Ke Shih 		    rssi, set->lna_idx, set->tia_idx, set->rxb_idx);
3375e3ec7017SPing-Ke Shih }
3376e3ec7017SPing-Ke Shih 
3377e3ec7017SPing-Ke Shih #define IGI_OFFSET_MAX 25
3378e3ec7017SPing-Ke Shih #define IGI_OFFSET_MUL 2
3379e3ec7017SPing-Ke Shih static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev)
3380e3ec7017SPing-Ke Shih {
3381e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3382e3ec7017SPing-Ke Shih 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3383e3ec7017SPing-Ke Shih 	enum rtw89_dig_noisy_level noisy_lv;
3384e3ec7017SPing-Ke Shih 	u8 igi_offset = dig->fa_rssi_ofst;
3385e3ec7017SPing-Ke Shih 	u16 fa_ratio = 0;
3386e3ec7017SPing-Ke Shih 
3387e3ec7017SPing-Ke Shih 	fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil;
3388e3ec7017SPing-Ke Shih 
3389e3ec7017SPing-Ke Shih 	if (fa_ratio < dig->fa_th[0])
3390e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL0;
3391e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[1])
3392e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL1;
3393e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[2])
3394e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL2;
3395e3ec7017SPing-Ke Shih 	else if (fa_ratio < dig->fa_th[3])
3396e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL3;
3397e3ec7017SPing-Ke Shih 	else
3398e3ec7017SPing-Ke Shih 		noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX;
3399e3ec7017SPing-Ke Shih 
3400e3ec7017SPing-Ke Shih 	if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2)
3401e3ec7017SPing-Ke Shih 		igi_offset = 0;
3402e3ec7017SPing-Ke Shih 	else
3403e3ec7017SPing-Ke Shih 		igi_offset += noisy_lv * IGI_OFFSET_MUL;
3404e3ec7017SPing-Ke Shih 
3405e3ec7017SPing-Ke Shih 	igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX);
3406e3ec7017SPing-Ke Shih 	dig->fa_rssi_ofst = igi_offset;
3407e3ec7017SPing-Ke Shih 
3408e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3409e3ec7017SPing-Ke Shih 		    "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n",
3410e3ec7017SPing-Ke Shih 		    dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]);
3411e3ec7017SPing-Ke Shih 
3412e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3413e3ec7017SPing-Ke Shih 		    "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n",
3414e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil,
3415e3ec7017SPing-Ke Shih 		    env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil,
3416e3ec7017SPing-Ke Shih 		    noisy_lv, igi_offset);
3417e3ec7017SPing-Ke Shih }
3418e3ec7017SPing-Ke Shih 
3419e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx)
3420e3ec7017SPing-Ke Shih {
3421e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH0_LNA_INIT,
3422e3ec7017SPing-Ke Shih 			       B_PATH0_LNA_INIT_IDX_MSK, lna_idx);
3423e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH1_LNA_INIT,
3424e3ec7017SPing-Ke Shih 			       B_PATH1_LNA_INIT_IDX_MSK, lna_idx);
3425e3ec7017SPing-Ke Shih }
3426e3ec7017SPing-Ke Shih 
3427e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx)
3428e3ec7017SPing-Ke Shih {
3429e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH0_TIA_INIT,
3430e3ec7017SPing-Ke Shih 			       B_PATH0_TIA_INIT_IDX_MSK, tia_idx);
3431e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH1_TIA_INIT,
3432e3ec7017SPing-Ke Shih 			       B_PATH1_TIA_INIT_IDX_MSK, tia_idx);
3433e3ec7017SPing-Ke Shih }
3434e3ec7017SPing-Ke Shih 
3435e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx)
3436e3ec7017SPing-Ke Shih {
3437e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH0_RXB_INIT,
3438e3ec7017SPing-Ke Shih 			       B_PATH0_RXB_INIT_IDX_MSK, rxb_idx);
3439e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_PATH1_RXB_INIT,
3440e3ec7017SPing-Ke Shih 			       B_PATH1_RXB_INIT_IDX_MSK, rxb_idx);
3441e3ec7017SPing-Ke Shih }
3442e3ec7017SPing-Ke Shih 
3443e3ec7017SPing-Ke Shih static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev,
3444e3ec7017SPing-Ke Shih 				     const struct rtw89_agc_gaincode_set set)
3445e3ec7017SPing-Ke Shih {
3446e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx);
3447e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx);
3448e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx);
3449e3ec7017SPing-Ke Shih 
3450e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n",
3451e3ec7017SPing-Ke Shih 		    set.lna_idx, set.tia_idx, set.rxb_idx);
3452e3ec7017SPing-Ke Shih }
3453e3ec7017SPing-Ke Shih 
3454e3ec7017SPing-Ke Shih static const struct rtw89_reg_def sdagc_config[4] = {
3455e3ec7017SPing-Ke Shih 	{R_PATH0_P20_FOLLOW_BY_PAGCUGC, B_PATH0_P20_FOLLOW_BY_PAGCUGC_EN_MSK},
3456e3ec7017SPing-Ke Shih 	{R_PATH0_S20_FOLLOW_BY_PAGCUGC, B_PATH0_S20_FOLLOW_BY_PAGCUGC_EN_MSK},
3457e3ec7017SPing-Ke Shih 	{R_PATH1_P20_FOLLOW_BY_PAGCUGC, B_PATH1_P20_FOLLOW_BY_PAGCUGC_EN_MSK},
3458e3ec7017SPing-Ke Shih 	{R_PATH1_S20_FOLLOW_BY_PAGCUGC, B_PATH1_S20_FOLLOW_BY_PAGCUGC_EN_MSK},
3459e3ec7017SPing-Ke Shih };
3460e3ec7017SPing-Ke Shih 
3461e3ec7017SPing-Ke Shih static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev,
3462e3ec7017SPing-Ke Shih 						   bool enable)
3463e3ec7017SPing-Ke Shih {
3464e3ec7017SPing-Ke Shih 	u8 i = 0;
3465e3ec7017SPing-Ke Shih 
3466e3ec7017SPing-Ke Shih 	for (i = 0; i < ARRAY_SIZE(sdagc_config); i++)
3467e3ec7017SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, sdagc_config[i].addr,
3468e3ec7017SPing-Ke Shih 				       sdagc_config[i].mask, enable);
3469e3ec7017SPing-Ke Shih 
3470e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable);
3471e3ec7017SPing-Ke Shih }
3472e3ec7017SPing-Ke Shih 
3473bed4045fSJohnson Lin static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev)
3474bed4045fSJohnson Lin {
3475bed4045fSJohnson Lin 	struct rtw89_dig_info *dig = &rtwdev->dig;
3476bed4045fSJohnson Lin 
3477d264edb1SJohnson Lin 	if (!rtwdev->hal.support_igi)
3478d264edb1SJohnson Lin 		return;
3479d264edb1SJohnson Lin 
3480bed4045fSJohnson Lin 	if (dig->force_gaincode_idx_en) {
3481bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
3482bed4045fSJohnson Lin 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
3483bed4045fSJohnson Lin 			    "Force gaincode index enabled.\n");
3484bed4045fSJohnson Lin 	} else {
3485bed4045fSJohnson Lin 		rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi,
3486bed4045fSJohnson Lin 					       &dig->cur_gaincode);
3487bed4045fSJohnson Lin 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode);
3488bed4045fSJohnson Lin 	}
3489bed4045fSJohnson Lin }
3490bed4045fSJohnson Lin 
3491e3ec7017SPing-Ke Shih static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi,
3492e3ec7017SPing-Ke Shih 				    bool enable)
3493e3ec7017SPing-Ke Shih {
3494*cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3495*cbb145b9SZong-Zhe Yang 	enum rtw89_bandwidth cbw = chan->band_width;
3496e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3497e3ec7017SPing-Ke Shih 	u8 final_rssi = 0, under_region = dig->pd_low_th_ofst;
34981c2423deSJohnson Lin 	u8 ofdm_cca_th;
34991c2423deSJohnson Lin 	s8 cck_cca_th;
35001c2423deSJohnson Lin 	u32 pd_val = 0;
3501e3ec7017SPing-Ke Shih 
3502e3ec7017SPing-Ke Shih 	under_region += PD_TH_SB_FLTR_CMP_VAL;
3503e3ec7017SPing-Ke Shih 
3504e3ec7017SPing-Ke Shih 	switch (cbw) {
3505e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
3506e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW40_CMP_VAL;
3507e3ec7017SPing-Ke Shih 		break;
3508e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
3509e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW80_CMP_VAL;
3510e3ec7017SPing-Ke Shih 		break;
35111c2423deSJohnson Lin 	case RTW89_CHANNEL_WIDTH_160:
35121c2423deSJohnson Lin 		under_region += PD_TH_BW160_CMP_VAL;
35131c2423deSJohnson Lin 		break;
3514e3ec7017SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
3515e3ec7017SPing-Ke Shih 		fallthrough;
3516e3ec7017SPing-Ke Shih 	default:
3517e3ec7017SPing-Ke Shih 		under_region += PD_TH_BW20_CMP_VAL;
3518e3ec7017SPing-Ke Shih 		break;
3519e3ec7017SPing-Ke Shih 	}
3520e3ec7017SPing-Ke Shih 
3521e3ec7017SPing-Ke Shih 	dig->dyn_pd_th_max = dig->igi_rssi;
3522e3ec7017SPing-Ke Shih 
3523e3ec7017SPing-Ke Shih 	final_rssi = min_t(u8, rssi, dig->igi_rssi);
35241c2423deSJohnson Lin 	ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region,
3525e3ec7017SPing-Ke Shih 			      PD_TH_MAX_RSSI + under_region);
3526e3ec7017SPing-Ke Shih 
3527e3ec7017SPing-Ke Shih 	if (enable) {
35281c2423deSJohnson Lin 		pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1;
3529e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
35301c2423deSJohnson Lin 			    "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n",
35311c2423deSJohnson Lin 			    final_rssi, ofdm_cca_th, under_region, pd_val);
3532e3ec7017SPing-Ke Shih 	} else {
3533e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
3534c51ed740SColin Ian King 			    "Dynamic PD th disabled, Set PD_low_bd=0\n");
3535e3ec7017SPing-Ke Shih 	}
3536e3ec7017SPing-Ke Shih 
3537e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SEG0R_PD, B_SEG0R_PD_LOWER_BOUND_MSK,
35381c2423deSJohnson Lin 			       pd_val);
3539e3ec7017SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SEG0R_PD,
3540e3ec7017SPing-Ke Shih 			       B_SEG0R_PD_SPATIAL_REUSE_EN_MSK, enable);
35411c2423deSJohnson Lin 
35421c2423deSJohnson Lin 	if (!rtwdev->hal.support_cckpd)
35431c2423deSJohnson Lin 		return;
35441c2423deSJohnson Lin 
35451c2423deSJohnson Lin 	cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI);
35461c2423deSJohnson Lin 	pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX);
35471c2423deSJohnson Lin 
35481c2423deSJohnson Lin 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
35491c2423deSJohnson Lin 		    "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n",
35501c2423deSJohnson Lin 		    final_rssi, cck_cca_th, under_region, pd_val);
35511c2423deSJohnson Lin 
35521c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1,
35531c2423deSJohnson Lin 			       B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable);
35541c2423deSJohnson Lin 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1,
35551c2423deSJohnson Lin 			       B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val);
3556e3ec7017SPing-Ke Shih }
3557e3ec7017SPing-Ke Shih 
3558e3ec7017SPing-Ke Shih void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev)
3559e3ec7017SPing-Ke Shih {
3560e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3561e3ec7017SPing-Ke Shih 
3562e3ec7017SPing-Ke Shih 	dig->bypass_dig = false;
3563e3ec7017SPing-Ke Shih 	rtw89_phy_dig_para_reset(rtwdev);
3564e3ec7017SPing-Ke Shih 	rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
3565e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false);
3566e3ec7017SPing-Ke Shih 	rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
3567e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_para(rtwdev);
3568e3ec7017SPing-Ke Shih }
3569e3ec7017SPing-Ke Shih 
3570e3ec7017SPing-Ke Shih #define IGI_RSSI_MIN 10
3571e3ec7017SPing-Ke Shih void rtw89_phy_dig(struct rtw89_dev *rtwdev)
3572e3ec7017SPing-Ke Shih {
3573e3ec7017SPing-Ke Shih 	struct rtw89_dig_info *dig = &rtwdev->dig;
3574e3ec7017SPing-Ke Shih 	bool is_linked = rtwdev->total_sta_assoc > 0;
3575e3ec7017SPing-Ke Shih 
3576e3ec7017SPing-Ke Shih 	if (unlikely(dig->bypass_dig)) {
3577e3ec7017SPing-Ke Shih 		dig->bypass_dig = false;
3578e3ec7017SPing-Ke Shih 		return;
3579e3ec7017SPing-Ke Shih 	}
3580e3ec7017SPing-Ke Shih 
3581e3ec7017SPing-Ke Shih 	if (!dig->is_linked_pre && is_linked) {
3582e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n");
3583e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
3584e3ec7017SPing-Ke Shih 	} else if (dig->is_linked_pre && !is_linked) {
3585e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n");
3586e3ec7017SPing-Ke Shih 		rtw89_phy_dig_update_para(rtwdev);
3587e3ec7017SPing-Ke Shih 	}
3588e3ec7017SPing-Ke Shih 	dig->is_linked_pre = is_linked;
3589e3ec7017SPing-Ke Shih 
3590e3ec7017SPing-Ke Shih 	rtw89_phy_dig_igi_offset_by_env(rtwdev);
3591e3ec7017SPing-Ke Shih 	rtw89_phy_dig_update_rssi_info(rtwdev);
3592e3ec7017SPing-Ke Shih 
3593e3ec7017SPing-Ke Shih 	dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ?
3594e3ec7017SPing-Ke Shih 			    dig->igi_rssi - IGI_RSSI_MIN : 0;
3595e3ec7017SPing-Ke Shih 	dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX;
3596e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst;
3597e3ec7017SPing-Ke Shih 
3598e3ec7017SPing-Ke Shih 	dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min,
3599e3ec7017SPing-Ke Shih 				 dig->dyn_igi_max);
3600e3ec7017SPing-Ke Shih 
3601e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3602e3ec7017SPing-Ke Shih 		    "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n",
3603e3ec7017SPing-Ke Shih 		    dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min,
3604e3ec7017SPing-Ke Shih 		    dig->igi_fa_rssi);
3605e3ec7017SPing-Ke Shih 
3606bed4045fSJohnson Lin 	rtw89_phy_dig_config_igi(rtwdev);
3607e3ec7017SPing-Ke Shih 
3608e3ec7017SPing-Ke Shih 	rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en);
3609e3ec7017SPing-Ke Shih 
3610e3ec7017SPing-Ke Shih 	if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max)
3611e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true);
3612e3ec7017SPing-Ke Shih 	else
3613e3ec7017SPing-Ke Shih 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
3614e3ec7017SPing-Ke Shih }
3615e3ec7017SPing-Ke Shih 
3616e3ec7017SPing-Ke Shih static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev)
3617e3ec7017SPing-Ke Shih {
3618e3ec7017SPing-Ke Shih 	rtw89_phy_ccx_top_setting_init(rtwdev);
3619e3ec7017SPing-Ke Shih 	rtw89_phy_ifs_clm_setting_init(rtwdev);
3620e3ec7017SPing-Ke Shih }
3621e3ec7017SPing-Ke Shih 
3622e3ec7017SPing-Ke Shih void rtw89_phy_dm_init(struct rtw89_dev *rtwdev)
3623e3ec7017SPing-Ke Shih {
3624e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3625e3ec7017SPing-Ke Shih 
3626e3ec7017SPing-Ke Shih 	rtw89_phy_stat_init(rtwdev);
3627e3ec7017SPing-Ke Shih 
3628e3ec7017SPing-Ke Shih 	rtw89_chip_bb_sethw(rtwdev);
3629e3ec7017SPing-Ke Shih 
3630e3ec7017SPing-Ke Shih 	rtw89_phy_env_monitor_init(rtwdev);
3631eb4e52b3SPo Hao Huang 	rtw89_physts_parsing_init(rtwdev);
3632e3ec7017SPing-Ke Shih 	rtw89_phy_dig_init(rtwdev);
3633e3ec7017SPing-Ke Shih 	rtw89_phy_cfo_init(rtwdev);
3634e3ec7017SPing-Ke Shih 
3635e3ec7017SPing-Ke Shih 	rtw89_phy_init_rf_nctl(rtwdev);
3636e3ec7017SPing-Ke Shih 	rtw89_chip_rfk_init(rtwdev);
3637e3ec7017SPing-Ke Shih 	rtw89_load_txpwr_table(rtwdev, chip->byr_table);
3638e3ec7017SPing-Ke Shih 	rtw89_chip_set_txpwr_ctrl(rtwdev);
3639e3ec7017SPing-Ke Shih 	rtw89_chip_power_trim(rtwdev);
3640cd89a471SPing-Ke Shih 	rtw89_chip_cfg_txrx_path(rtwdev);
3641e3ec7017SPing-Ke Shih }
3642e3ec7017SPing-Ke Shih 
3643e3ec7017SPing-Ke Shih void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
3644e3ec7017SPing-Ke Shih {
3645e3ec7017SPing-Ke Shih 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
3646e3ec7017SPing-Ke Shih 	u8 bss_color;
3647e3ec7017SPing-Ke Shih 
3648f276e20bSJohannes Berg 	if (!vif->bss_conf.he_support || !vif->cfg.assoc)
3649e3ec7017SPing-Ke Shih 		return;
3650e3ec7017SPing-Ke Shih 
3651e3ec7017SPing-Ke Shih 	bss_color = vif->bss_conf.he_bss_color.color;
3652e3ec7017SPing-Ke Shih 
3653e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_VLD0, 0x1,
3654e3ec7017SPing-Ke Shih 			      phy_idx);
3655e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_TGT, bss_color,
3656e3ec7017SPing-Ke Shih 			      phy_idx);
3657e3ec7017SPing-Ke Shih 	rtw89_phy_write32_idx(rtwdev, R_BSS_CLR_MAP, B_BSS_CLR_MAP_STAID,
3658f276e20bSJohannes Berg 			      vif->cfg.aid, phy_idx);
3659e3ec7017SPing-Ke Shih }
3660db7fa61aSZong-Zhe Yang 
3661db7fa61aSZong-Zhe Yang static void
3662db7fa61aSZong-Zhe Yang _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
3663db7fa61aSZong-Zhe Yang {
3664db7fa61aSZong-Zhe Yang 	rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data);
3665db7fa61aSZong-Zhe Yang }
3666db7fa61aSZong-Zhe Yang 
3667db7fa61aSZong-Zhe Yang static void
3668db7fa61aSZong-Zhe Yang _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
3669db7fa61aSZong-Zhe Yang {
3670db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
3671db7fa61aSZong-Zhe Yang }
3672db7fa61aSZong-Zhe Yang 
3673db7fa61aSZong-Zhe Yang static void
3674db7fa61aSZong-Zhe Yang _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
3675db7fa61aSZong-Zhe Yang {
3676db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_set(rtwdev, def->addr, def->mask);
3677db7fa61aSZong-Zhe Yang }
3678db7fa61aSZong-Zhe Yang 
3679db7fa61aSZong-Zhe Yang static void
3680db7fa61aSZong-Zhe Yang _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
3681db7fa61aSZong-Zhe Yang {
3682db7fa61aSZong-Zhe Yang 	rtw89_phy_write32_clr(rtwdev, def->addr, def->mask);
3683db7fa61aSZong-Zhe Yang }
3684db7fa61aSZong-Zhe Yang 
3685db7fa61aSZong-Zhe Yang static void
3686db7fa61aSZong-Zhe Yang _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
3687db7fa61aSZong-Zhe Yang {
3688db7fa61aSZong-Zhe Yang 	udelay(def->data);
3689db7fa61aSZong-Zhe Yang }
3690db7fa61aSZong-Zhe Yang 
3691db7fa61aSZong-Zhe Yang static void
3692db7fa61aSZong-Zhe Yang (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = {
3693db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WRF] = _rfk_write_rf,
3694db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WM] = _rfk_write32_mask,
3695db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WS] = _rfk_write32_set,
3696db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_WC] = _rfk_write32_clr,
3697db7fa61aSZong-Zhe Yang 	[RTW89_RFK_F_DELAY] = _rfk_delay,
3698db7fa61aSZong-Zhe Yang };
3699db7fa61aSZong-Zhe Yang 
3700db7fa61aSZong-Zhe Yang static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM);
3701db7fa61aSZong-Zhe Yang 
3702db7fa61aSZong-Zhe Yang void
3703db7fa61aSZong-Zhe Yang rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl)
3704db7fa61aSZong-Zhe Yang {
3705db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *p = tbl->defs;
3706db7fa61aSZong-Zhe Yang 	const struct rtw89_reg5_def *end = tbl->defs + tbl->size;
3707db7fa61aSZong-Zhe Yang 
3708db7fa61aSZong-Zhe Yang 	for (; p < end; p++)
3709db7fa61aSZong-Zhe Yang 		_rfk_handler[p->flag](rtwdev, p);
3710db7fa61aSZong-Zhe Yang }
3711db7fa61aSZong-Zhe Yang EXPORT_SYMBOL(rtw89_rfk_parser);
3712c7845551SPing-Ke Shih 
3713c7845551SPing-Ke Shih #define RTW89_TSSI_FAST_MODE_NUM 4
3714c7845551SPing-Ke Shih 
3715c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = {
3716c7845551SPing-Ke Shih 	{0xD934, 0xff0000},
3717c7845551SPing-Ke Shih 	{0xD934, 0xff000000},
3718c7845551SPing-Ke Shih 	{0xD938, 0xff},
3719c7845551SPing-Ke Shih 	{0xD934, 0xff00},
3720c7845551SPing-Ke Shih };
3721c7845551SPing-Ke Shih 
3722c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = {
3723c7845551SPing-Ke Shih 	{0xD930, 0xff0000},
3724c7845551SPing-Ke Shih 	{0xD930, 0xff000000},
3725c7845551SPing-Ke Shih 	{0xD934, 0xff},
3726c7845551SPing-Ke Shih 	{0xD930, 0xff00},
3727c7845551SPing-Ke Shih };
3728c7845551SPing-Ke Shih 
3729c7845551SPing-Ke Shih static
3730c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev,
3731c7845551SPing-Ke Shih 					   enum rtw89_mac_idx mac_idx,
3732c7845551SPing-Ke Shih 					   enum rtw89_tssi_bandedge_cfg bandedge_cfg,
3733c7845551SPing-Ke Shih 					   u32 val)
3734c7845551SPing-Ke Shih {
3735c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
3736c7845551SPing-Ke Shih 	u32 reg;
3737c7845551SPing-Ke Shih 	int i;
3738c7845551SPing-Ke Shih 
3739c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
3740c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_flat;
3741c7845551SPing-Ke Shih 	else
3742c7845551SPing-Ke Shih 		regs = rtw89_tssi_fastmode_regs_level;
3743c7845551SPing-Ke Shih 
3744c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) {
3745c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
3746c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, val);
3747c7845551SPing-Ke Shih 	}
3748c7845551SPing-Ke Shih }
3749c7845551SPing-Ke Shih 
3750c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = {
3751c7845551SPing-Ke Shih 	{0xD91C, 0xff000000},
3752c7845551SPing-Ke Shih 	{0xD920, 0xff},
3753c7845551SPing-Ke Shih 	{0xD920, 0xff00},
3754c7845551SPing-Ke Shih 	{0xD920, 0xff0000},
3755c7845551SPing-Ke Shih 	{0xD920, 0xff000000},
3756c7845551SPing-Ke Shih 	{0xD924, 0xff},
3757c7845551SPing-Ke Shih 	{0xD924, 0xff00},
3758c7845551SPing-Ke Shih 	{0xD914, 0xff000000},
3759c7845551SPing-Ke Shih 	{0xD918, 0xff},
3760c7845551SPing-Ke Shih 	{0xD918, 0xff00},
3761c7845551SPing-Ke Shih 	{0xD918, 0xff0000},
3762c7845551SPing-Ke Shih 	{0xD918, 0xff000000},
3763c7845551SPing-Ke Shih 	{0xD91C, 0xff},
3764c7845551SPing-Ke Shih 	{0xD91C, 0xff00},
3765c7845551SPing-Ke Shih 	{0xD91C, 0xff0000},
3766c7845551SPing-Ke Shih };
3767c7845551SPing-Ke Shih 
3768c7845551SPing-Ke Shih static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = {
3769c7845551SPing-Ke Shih 	{0xD910, 0xff},
3770c7845551SPing-Ke Shih 	{0xD910, 0xff00},
3771c7845551SPing-Ke Shih 	{0xD910, 0xff0000},
3772c7845551SPing-Ke Shih 	{0xD910, 0xff000000},
3773c7845551SPing-Ke Shih 	{0xD914, 0xff},
3774c7845551SPing-Ke Shih 	{0xD914, 0xff00},
3775c7845551SPing-Ke Shih 	{0xD914, 0xff0000},
3776c7845551SPing-Ke Shih 	{0xD908, 0xff},
3777c7845551SPing-Ke Shih 	{0xD908, 0xff00},
3778c7845551SPing-Ke Shih 	{0xD908, 0xff0000},
3779c7845551SPing-Ke Shih 	{0xD908, 0xff000000},
3780c7845551SPing-Ke Shih 	{0xD90C, 0xff},
3781c7845551SPing-Ke Shih 	{0xD90C, 0xff00},
3782c7845551SPing-Ke Shih 	{0xD90C, 0xff0000},
3783c7845551SPing-Ke Shih 	{0xD90C, 0xff000000},
3784c7845551SPing-Ke Shih };
3785c7845551SPing-Ke Shih 
3786c7845551SPing-Ke Shih void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev,
3787c7845551SPing-Ke Shih 					  enum rtw89_mac_idx mac_idx,
3788c7845551SPing-Ke Shih 					  enum rtw89_tssi_bandedge_cfg bandedge_cfg)
3789c7845551SPing-Ke Shih {
3790c7845551SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
3791c7845551SPing-Ke Shih 	const struct rtw89_reg_def *regs;
3792c7845551SPing-Ke Shih 	const u32 *data;
3793c7845551SPing-Ke Shih 	u32 reg;
3794c7845551SPing-Ke Shih 	int i;
3795c7845551SPing-Ke Shih 
3796c7845551SPing-Ke Shih 	if (bandedge_cfg >= RTW89_TSSI_CFG_NUM)
3797c7845551SPing-Ke Shih 		return;
3798c7845551SPing-Ke Shih 
3799c7845551SPing-Ke Shih 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
3800c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_flat;
3801c7845551SPing-Ke Shih 	else
3802c7845551SPing-Ke Shih 		regs = rtw89_tssi_bandedge_regs_level;
3803c7845551SPing-Ke Shih 
3804c7845551SPing-Ke Shih 	data = chip->tssi_dbw_table->data[bandedge_cfg];
3805c7845551SPing-Ke Shih 
3806c7845551SPing-Ke Shih 	for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) {
3807c7845551SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
3808c7845551SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]);
3809c7845551SPing-Ke Shih 	}
3810c7845551SPing-Ke Shih 
3811c7845551SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx);
3812c7845551SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg);
3813c7845551SPing-Ke Shih 
3814c7845551SPing-Ke Shih 	rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg,
3815c7845551SPing-Ke Shih 					      data[RTW89_TSSI_SBW20]);
3816c7845551SPing-Ke Shih }
3817c7845551SPing-Ke Shih EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg);
3818