1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 
5 #include "coex.h"
6 #include "debug.h"
7 #include "fw.h"
8 #include "mac.h"
9 #include "phy.h"
10 #include "ps.h"
11 #include "reg.h"
12 #include "sar.h"
13 #include "util.h"
14 
15 static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev,
16 			     const struct rtw89_ra_report *report)
17 {
18 	u32 bit_rate = report->bit_rate;
19 
20 	/* lower than ofdm, do not aggregate */
21 	if (bit_rate < 550)
22 		return 1;
23 
24 	/* avoid AMSDU for legacy rate */
25 	if (report->might_fallback_legacy)
26 		return 1;
27 
28 	/* lower than 20M vht 2ss mcs8, make it small */
29 	if (bit_rate < 1800)
30 		return 1200;
31 
32 	/* lower than 40M vht 2ss mcs9, make it medium */
33 	if (bit_rate < 4000)
34 		return 2600;
35 
36 	/* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */
37 	if (bit_rate < 7000)
38 		return 3500;
39 
40 	return rtwdev->chip->max_amsdu_limit;
41 }
42 
43 static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap)
44 {
45 	u64 ra_mask = 0;
46 	u8 mcs_cap;
47 	int i, nss;
48 
49 	for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) {
50 		mcs_cap = mcs_map & 0x3;
51 		switch (mcs_cap) {
52 		case 2:
53 			ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss;
54 			break;
55 		case 1:
56 			ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss;
57 			break;
58 		case 0:
59 			ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss;
60 			break;
61 		default:
62 			break;
63 		}
64 	}
65 
66 	return ra_mask;
67 }
68 
69 static u64 get_he_ra_mask(struct ieee80211_sta *sta)
70 {
71 	struct ieee80211_sta_he_cap cap = sta->deflink.he_cap;
72 	u16 mcs_map;
73 
74 	switch (sta->deflink.bandwidth) {
75 	case IEEE80211_STA_RX_BW_160:
76 		if (cap.he_cap_elem.phy_cap_info[0] &
77 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
78 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80);
79 		else
80 			mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160);
81 		break;
82 	default:
83 		mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80);
84 	}
85 
86 	/* MCS11, MCS9, MCS7 */
87 	return get_mcs_ra_mask(mcs_map, 11, 2);
88 }
89 
90 #define RA_FLOOR_TABLE_SIZE	7
91 #define RA_FLOOR_UP_GAP		3
92 static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi,
93 				  u8 ratr_state)
94 {
95 	u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100};
96 	u8 rssi_lv = 0;
97 	u8 i;
98 
99 	rssi >>= 1;
100 	for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) {
101 		if (i >= ratr_state)
102 			rssi_lv_t[i] += RA_FLOOR_UP_GAP;
103 		if (rssi < rssi_lv_t[i]) {
104 			rssi_lv = i;
105 			break;
106 		}
107 	}
108 	if (rssi_lv == 0)
109 		return 0xffffffffffffffffULL;
110 	else if (rssi_lv == 1)
111 		return 0xfffffffffffffff0ULL;
112 	else if (rssi_lv == 2)
113 		return 0xffffffffffffefe0ULL;
114 	else if (rssi_lv == 3)
115 		return 0xffffffffffffcfc0ULL;
116 	else if (rssi_lv == 4)
117 		return 0xffffffffffff8f80ULL;
118 	else if (rssi_lv >= 5)
119 		return 0xffffffffffff0f00ULL;
120 
121 	return 0xffffffffffffffffULL;
122 }
123 
124 static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak)
125 {
126 	if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0)
127 		ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
128 
129 	if (ra_mask == 0)
130 		ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES));
131 
132 	return ra_mask;
133 }
134 
135 static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta)
136 {
137 	struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta);
138 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
139 	struct cfg80211_bitrate_mask *mask = &rtwsta->mask;
140 	enum nl80211_band band;
141 	u64 cfg_mask;
142 
143 	if (!rtwsta->use_cfg_mask)
144 		return -1;
145 
146 	switch (chan->band_type) {
147 	case RTW89_BAND_2G:
148 		band = NL80211_BAND_2GHZ;
149 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy,
150 					   RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES);
151 		break;
152 	case RTW89_BAND_5G:
153 		band = NL80211_BAND_5GHZ;
154 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy,
155 					   RA_MASK_OFDM_RATES);
156 		break;
157 	case RTW89_BAND_6G:
158 		band = NL80211_BAND_6GHZ;
159 		cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy,
160 					   RA_MASK_OFDM_RATES);
161 		break;
162 	default:
163 		rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type);
164 		return -1;
165 	}
166 
167 	if (sta->deflink.he_cap.has_he) {
168 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0],
169 					    RA_MASK_HE_1SS_RATES);
170 		cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1],
171 					    RA_MASK_HE_2SS_RATES);
172 	} else if (sta->deflink.vht_cap.vht_supported) {
173 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0],
174 					    RA_MASK_VHT_1SS_RATES);
175 		cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1],
176 					    RA_MASK_VHT_2SS_RATES);
177 	} else if (sta->deflink.ht_cap.ht_supported) {
178 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0],
179 					    RA_MASK_HT_1SS_RATES);
180 		cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1],
181 					    RA_MASK_HT_2SS_RATES);
182 	}
183 
184 	return cfg_mask;
185 }
186 
187 static const u64
188 rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES,
189 			     RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES};
190 static const u64
191 rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES,
192 			      RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES};
193 static const u64
194 rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES,
195 			     RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES};
196 
197 static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev,
198 				struct rtw89_sta *rtwsta,
199 				bool *fix_giltf_en, u8 *fix_giltf)
200 {
201 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
202 	struct cfg80211_bitrate_mask *mask = &rtwsta->mask;
203 	u8 band = chan->band_type;
204 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
205 	u8 he_gi = mask->control[nl_band].he_gi;
206 	u8 he_ltf = mask->control[nl_band].he_ltf;
207 
208 	if (!rtwsta->use_cfg_mask)
209 		return;
210 
211 	if (he_ltf == 2 && he_gi == 2) {
212 		*fix_giltf = RTW89_GILTF_LGI_4XHE32;
213 	} else if (he_ltf == 2 && he_gi == 0) {
214 		*fix_giltf = RTW89_GILTF_SGI_4XHE08;
215 	} else if (he_ltf == 1 && he_gi == 1) {
216 		*fix_giltf = RTW89_GILTF_2XHE16;
217 	} else if (he_ltf == 1 && he_gi == 0) {
218 		*fix_giltf = RTW89_GILTF_2XHE08;
219 	} else if (he_ltf == 0 && he_gi == 1) {
220 		*fix_giltf = RTW89_GILTF_1XHE16;
221 	} else if (he_ltf == 0 && he_gi == 0) {
222 		*fix_giltf = RTW89_GILTF_1XHE08;
223 	} else {
224 		*fix_giltf_en = false;
225 		return;
226 	}
227 
228 	*fix_giltf_en = true;
229 }
230 
231 static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev,
232 				    struct ieee80211_sta *sta, bool csi)
233 {
234 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
235 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
236 	struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern;
237 	struct rtw89_ra_info *ra = &rtwsta->ra;
238 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
239 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif);
240 	const u64 *high_rate_masks = rtw89_ra_mask_ht_rates;
241 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi);
242 	u64 ra_mask = 0;
243 	u64 ra_mask_bak;
244 	u8 mode = 0;
245 	u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY;
246 	u8 bw_mode = 0;
247 	u8 stbc_en = 0;
248 	u8 ldpc_en = 0;
249 	u8 fix_giltf = 0;
250 	u8 i;
251 	bool sgi = false;
252 	bool fix_giltf_en = false;
253 
254 	memset(ra, 0, sizeof(*ra));
255 	/* Set the ra mask from sta's capability */
256 	if (sta->deflink.he_cap.has_he) {
257 		mode |= RTW89_RA_MODE_HE;
258 		csi_mode = RTW89_RA_RPT_MODE_HE;
259 		ra_mask |= get_he_ra_mask(sta);
260 		high_rate_masks = rtw89_ra_mask_he_rates;
261 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] &
262 		    IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ)
263 			stbc_en = 1;
264 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] &
265 		    IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD)
266 			ldpc_en = 1;
267 		rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, &fix_giltf_en, &fix_giltf);
268 	} else if (sta->deflink.vht_cap.vht_supported) {
269 		u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map);
270 
271 		mode |= RTW89_RA_MODE_VHT;
272 		csi_mode = RTW89_RA_RPT_MODE_VHT;
273 		/* MCS9, MCS8, MCS7 */
274 		ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1);
275 		high_rate_masks = rtw89_ra_mask_vht_rates;
276 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK)
277 			stbc_en = 1;
278 		if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC)
279 			ldpc_en = 1;
280 	} else if (sta->deflink.ht_cap.ht_supported) {
281 		mode |= RTW89_RA_MODE_HT;
282 		csi_mode = RTW89_RA_RPT_MODE_HT;
283 		ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) |
284 			   ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) |
285 			   (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) |
286 			   (sta->deflink.ht_cap.mcs.rx_mask[0] << 12);
287 		high_rate_masks = rtw89_ra_mask_ht_rates;
288 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
289 			stbc_en = 1;
290 		if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING)
291 			ldpc_en = 1;
292 	}
293 
294 	switch (chan->band_type) {
295 	case RTW89_BAND_2G:
296 		ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ];
297 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf)
298 			mode |= RTW89_RA_MODE_CCK;
299 		if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0)
300 			mode |= RTW89_RA_MODE_OFDM;
301 		break;
302 	case RTW89_BAND_5G:
303 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4;
304 		mode |= RTW89_RA_MODE_OFDM;
305 		break;
306 	case RTW89_BAND_6G:
307 		ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4;
308 		mode |= RTW89_RA_MODE_OFDM;
309 		break;
310 	default:
311 		rtw89_err(rtwdev, "Unknown band type\n");
312 		break;
313 	}
314 
315 	ra_mask_bak = ra_mask;
316 
317 	if (mode >= RTW89_RA_MODE_HT) {
318 		u64 mask = 0;
319 		for (i = 0; i < rtwdev->hal.tx_nss; i++)
320 			mask |= high_rate_masks[i];
321 		if (mode & RTW89_RA_MODE_OFDM)
322 			mask |= RA_MASK_SUBOFDM_RATES;
323 		if (mode & RTW89_RA_MODE_CCK)
324 			mask |= RA_MASK_SUBCCK_RATES;
325 		ra_mask &= mask;
326 	} else if (mode & RTW89_RA_MODE_OFDM) {
327 		ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES);
328 	}
329 
330 	if (mode != RTW89_RA_MODE_CCK)
331 		ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0);
332 
333 	ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak);
334 	ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
335 
336 	switch (sta->deflink.bandwidth) {
337 	case IEEE80211_STA_RX_BW_160:
338 		bw_mode = RTW89_CHANNEL_WIDTH_160;
339 		sgi = sta->deflink.vht_cap.vht_supported &&
340 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160);
341 		break;
342 	case IEEE80211_STA_RX_BW_80:
343 		bw_mode = RTW89_CHANNEL_WIDTH_80;
344 		sgi = sta->deflink.vht_cap.vht_supported &&
345 		      (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80);
346 		break;
347 	case IEEE80211_STA_RX_BW_40:
348 		bw_mode = RTW89_CHANNEL_WIDTH_40;
349 		sgi = sta->deflink.ht_cap.ht_supported &&
350 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40);
351 		break;
352 	default:
353 		bw_mode = RTW89_CHANNEL_WIDTH_20;
354 		sgi = sta->deflink.ht_cap.ht_supported &&
355 		      (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20);
356 		break;
357 	}
358 
359 	if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] &
360 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM)
361 		ra->dcm_cap = 1;
362 
363 	if (rate_pattern->enable && !vif->p2p) {
364 		ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta);
365 		ra_mask &= rate_pattern->ra_mask;
366 		mode = rate_pattern->ra_mode;
367 	}
368 
369 	ra->bw_cap = bw_mode;
370 	ra->er_cap = rtwsta->er_cap;
371 	ra->mode_ctrl = mode;
372 	ra->macid = rtwsta->mac_id;
373 	ra->stbc_cap = stbc_en;
374 	ra->ldpc_cap = ldpc_en;
375 	ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1;
376 	ra->en_sgi = sgi;
377 	ra->ra_mask = ra_mask;
378 	ra->fix_giltf_en = fix_giltf_en;
379 	ra->fix_giltf = fix_giltf;
380 
381 	if (!csi)
382 		return;
383 
384 	ra->fixed_csi_rate_en = false;
385 	ra->ra_csi_rate_en = true;
386 	ra->cr_tbl_sel = false;
387 	ra->band_num = rtwvif->phy_idx;
388 	ra->csi_bw = bw_mode;
389 	ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32;
390 	ra->csi_mcs_ss_idx = 5;
391 	ra->csi_mode = csi_mode;
392 }
393 
394 void rtw89_phy_ra_updata_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta,
395 			     u32 changed)
396 {
397 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
398 	struct rtw89_ra_info *ra = &rtwsta->ra;
399 
400 	rtw89_phy_ra_sta_update(rtwdev, sta, false);
401 
402 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED)
403 		ra->upd_mask = 1;
404 	if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED))
405 		ra->upd_bw_nss_mask = 1;
406 
407 	rtw89_debug(rtwdev, RTW89_DBG_RA,
408 		    "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d",
409 		    ra->macid,
410 		    ra->bw_cap,
411 		    ra->ss_num,
412 		    ra->en_sgi,
413 		    ra->giltf);
414 
415 	rtw89_fw_h2c_ra(rtwdev, ra, false);
416 }
417 
418 static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next,
419 				 u16 rate_base, u64 ra_mask, u8 ra_mode,
420 				 u32 rate_ctrl, u32 ctrl_skip, bool force)
421 {
422 	u8 n, c;
423 
424 	if (rate_ctrl == ctrl_skip)
425 		return true;
426 
427 	n = hweight32(rate_ctrl);
428 	if (n == 0)
429 		return true;
430 
431 	if (force && n != 1)
432 		return false;
433 
434 	if (next->enable)
435 		return false;
436 
437 	c = __fls(rate_ctrl);
438 	next->rate = rate_base + c;
439 	next->ra_mode = ra_mode;
440 	next->ra_mask = ra_mask;
441 	next->enable = true;
442 
443 	return true;
444 }
445 
446 void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev,
447 				struct ieee80211_vif *vif,
448 				const struct cfg80211_bitrate_mask *mask)
449 {
450 	struct ieee80211_supported_band *sband;
451 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
452 	struct rtw89_phy_rate_pattern next_pattern = {0};
453 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
454 	static const u16 hw_rate_he[] = {RTW89_HW_RATE_HE_NSS1_MCS0,
455 					 RTW89_HW_RATE_HE_NSS2_MCS0,
456 					 RTW89_HW_RATE_HE_NSS3_MCS0,
457 					 RTW89_HW_RATE_HE_NSS4_MCS0};
458 	static const u16 hw_rate_vht[] = {RTW89_HW_RATE_VHT_NSS1_MCS0,
459 					  RTW89_HW_RATE_VHT_NSS2_MCS0,
460 					  RTW89_HW_RATE_VHT_NSS3_MCS0,
461 					  RTW89_HW_RATE_VHT_NSS4_MCS0};
462 	static const u16 hw_rate_ht[] = {RTW89_HW_RATE_MCS0,
463 					 RTW89_HW_RATE_MCS8,
464 					 RTW89_HW_RATE_MCS16,
465 					 RTW89_HW_RATE_MCS24};
466 	u8 band = chan->band_type;
467 	enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
468 	u8 tx_nss = rtwdev->hal.tx_nss;
469 	u8 i;
470 
471 	for (i = 0; i < tx_nss; i++)
472 		if (!__check_rate_pattern(&next_pattern, hw_rate_he[i],
473 					  RA_MASK_HE_RATES, RTW89_RA_MODE_HE,
474 					  mask->control[nl_band].he_mcs[i],
475 					  0, true))
476 			goto out;
477 
478 	for (i = 0; i < tx_nss; i++)
479 		if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i],
480 					  RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT,
481 					  mask->control[nl_band].vht_mcs[i],
482 					  0, true))
483 			goto out;
484 
485 	for (i = 0; i < tx_nss; i++)
486 		if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i],
487 					  RA_MASK_HT_RATES, RTW89_RA_MODE_HT,
488 					  mask->control[nl_band].ht_mcs[i],
489 					  0, true))
490 			goto out;
491 
492 	/* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and
493 	 * require at least one basic rate for ieee80211_set_bitrate_mask,
494 	 * so the decision just depends on if all bitrates are set or not.
495 	 */
496 	sband = rtwdev->hw->wiphy->bands[nl_band];
497 	if (band == RTW89_BAND_2G) {
498 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1,
499 					  RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES,
500 					  RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM,
501 					  mask->control[nl_band].legacy,
502 					  BIT(sband->n_bitrates) - 1, false))
503 			goto out;
504 	} else {
505 		if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6,
506 					  RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM,
507 					  mask->control[nl_band].legacy,
508 					  BIT(sband->n_bitrates) - 1, false))
509 			goto out;
510 	}
511 
512 	if (!next_pattern.enable)
513 		goto out;
514 
515 	rtwvif->rate_pattern = next_pattern;
516 	rtw89_debug(rtwdev, RTW89_DBG_RA,
517 		    "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n",
518 		    next_pattern.rate,
519 		    next_pattern.ra_mask,
520 		    next_pattern.ra_mode);
521 	return;
522 
523 out:
524 	rtwvif->rate_pattern.enable = false;
525 	rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n");
526 }
527 
528 static void rtw89_phy_ra_updata_sta_iter(void *data, struct ieee80211_sta *sta)
529 {
530 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
531 
532 	rtw89_phy_ra_updata_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED);
533 }
534 
535 void rtw89_phy_ra_update(struct rtw89_dev *rtwdev)
536 {
537 	ieee80211_iterate_stations_atomic(rtwdev->hw,
538 					  rtw89_phy_ra_updata_sta_iter,
539 					  rtwdev);
540 }
541 
542 void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta)
543 {
544 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
545 	struct rtw89_ra_info *ra = &rtwsta->ra;
546 	u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR;
547 	bool csi = rtw89_sta_has_beamformer_cap(sta);
548 
549 	rtw89_phy_ra_sta_update(rtwdev, sta, csi);
550 
551 	if (rssi > 40)
552 		ra->init_rate_lv = 1;
553 	else if (rssi > 20)
554 		ra->init_rate_lv = 2;
555 	else if (rssi > 1)
556 		ra->init_rate_lv = 3;
557 	else
558 		ra->init_rate_lv = 0;
559 	ra->upd_all = 1;
560 	rtw89_debug(rtwdev, RTW89_DBG_RA,
561 		    "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d",
562 		    ra->macid,
563 		    ra->mode_ctrl,
564 		    ra->bw_cap,
565 		    ra->ss_num,
566 		    ra->init_rate_lv);
567 	rtw89_debug(rtwdev, RTW89_DBG_RA,
568 		    "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d",
569 		    ra->dcm_cap,
570 		    ra->er_cap,
571 		    ra->ldpc_cap,
572 		    ra->stbc_cap,
573 		    ra->en_sgi,
574 		    ra->giltf);
575 
576 	rtw89_fw_h2c_ra(rtwdev, ra, csi);
577 }
578 
579 u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev,
580 		      const struct rtw89_chan *chan,
581 		      enum rtw89_bandwidth dbw)
582 {
583 	enum rtw89_bandwidth cbw = chan->band_width;
584 	u8 pri_ch = chan->primary_channel;
585 	u8 central_ch = chan->channel;
586 	u8 txsc_idx = 0;
587 	u8 tmp = 0;
588 
589 	if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20)
590 		return txsc_idx;
591 
592 	switch (cbw) {
593 	case RTW89_CHANNEL_WIDTH_40:
594 		txsc_idx = pri_ch > central_ch ? 1 : 2;
595 		break;
596 	case RTW89_CHANNEL_WIDTH_80:
597 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
598 			if (pri_ch > central_ch)
599 				txsc_idx = (pri_ch - central_ch) >> 1;
600 			else
601 				txsc_idx = ((central_ch - pri_ch) >> 1) + 1;
602 		} else {
603 			txsc_idx = pri_ch > central_ch ? 9 : 10;
604 		}
605 		break;
606 	case RTW89_CHANNEL_WIDTH_160:
607 		if (pri_ch > central_ch)
608 			tmp = (pri_ch - central_ch) >> 1;
609 		else
610 			tmp = ((central_ch - pri_ch) >> 1) + 1;
611 
612 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
613 			txsc_idx = tmp;
614 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
615 			if (tmp == 1 || tmp == 3)
616 				txsc_idx = 9;
617 			else if (tmp == 5 || tmp == 7)
618 				txsc_idx = 11;
619 			else if (tmp == 2 || tmp == 4)
620 				txsc_idx = 10;
621 			else if (tmp == 6 || tmp == 8)
622 				txsc_idx = 12;
623 			else
624 				return 0xff;
625 		} else {
626 			txsc_idx = pri_ch > central_ch ? 13 : 14;
627 		}
628 		break;
629 	case RTW89_CHANNEL_WIDTH_80_80:
630 		if (dbw == RTW89_CHANNEL_WIDTH_20) {
631 			if (pri_ch > central_ch)
632 				txsc_idx = (10 - (pri_ch - central_ch)) >> 1;
633 			else
634 				txsc_idx = ((central_ch - pri_ch) >> 1) + 5;
635 		} else if (dbw == RTW89_CHANNEL_WIDTH_40) {
636 			txsc_idx = pri_ch > central_ch ? 10 : 12;
637 		} else {
638 			txsc_idx = 14;
639 		}
640 		break;
641 	default:
642 		break;
643 	}
644 
645 	return txsc_idx;
646 }
647 EXPORT_SYMBOL(rtw89_phy_get_txsc);
648 
649 static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev)
650 {
651 	return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) ||
652 	       !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1);
653 }
654 
655 u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
656 		      u32 addr, u32 mask)
657 {
658 	const struct rtw89_chip_info *chip = rtwdev->chip;
659 	const u32 *base_addr = chip->rf_base_addr;
660 	u32 val, direct_addr;
661 
662 	if (rf_path >= rtwdev->chip->rf_path_num) {
663 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
664 		return INV_RF_DATA;
665 	}
666 
667 	addr &= 0xff;
668 	direct_addr = base_addr[rf_path] + (addr << 2);
669 	mask &= RFREG_MASK;
670 
671 	val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask);
672 
673 	return val;
674 }
675 EXPORT_SYMBOL(rtw89_phy_read_rf);
676 
677 static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev,
678 			       enum rtw89_rf_path rf_path, u32 addr, u32 mask)
679 {
680 	bool busy;
681 	bool done;
682 	u32 val;
683 	int ret;
684 
685 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
686 				       1, 30, false, rtwdev);
687 	if (ret) {
688 		rtw89_err(rtwdev, "read rf busy swsi\n");
689 		return INV_RF_DATA;
690 	}
691 
692 	mask &= RFREG_MASK;
693 
694 	val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) |
695 	      FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr);
696 	rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val);
697 	udelay(2);
698 
699 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1,
700 				       30, false, rtwdev, R_SWSI_V1,
701 				       B_SWSI_R_DATA_DONE_V1);
702 	if (ret) {
703 		rtw89_err(rtwdev, "read swsi busy\n");
704 		return INV_RF_DATA;
705 	}
706 
707 	return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask);
708 }
709 
710 u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
711 			 u32 addr, u32 mask)
712 {
713 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
714 
715 	if (rf_path >= rtwdev->chip->rf_path_num) {
716 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
717 		return INV_RF_DATA;
718 	}
719 
720 	if (ad_sel)
721 		return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask);
722 	else
723 		return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask);
724 }
725 EXPORT_SYMBOL(rtw89_phy_read_rf_v1);
726 
727 bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
728 			u32 addr, u32 mask, u32 data)
729 {
730 	const struct rtw89_chip_info *chip = rtwdev->chip;
731 	const u32 *base_addr = chip->rf_base_addr;
732 	u32 direct_addr;
733 
734 	if (rf_path >= rtwdev->chip->rf_path_num) {
735 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
736 		return false;
737 	}
738 
739 	addr &= 0xff;
740 	direct_addr = base_addr[rf_path] + (addr << 2);
741 	mask &= RFREG_MASK;
742 
743 	rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data);
744 
745 	/* delay to ensure writing properly */
746 	udelay(1);
747 
748 	return true;
749 }
750 EXPORT_SYMBOL(rtw89_phy_write_rf);
751 
752 static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev,
753 				 enum rtw89_rf_path rf_path, u32 addr, u32 mask,
754 				 u32 data)
755 {
756 	u8 bit_shift;
757 	u32 val;
758 	bool busy, b_msk_en = false;
759 	int ret;
760 
761 	ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy,
762 				       1, 30, false, rtwdev);
763 	if (ret) {
764 		rtw89_err(rtwdev, "write rf busy swsi\n");
765 		return false;
766 	}
767 
768 	data &= RFREG_MASK;
769 	mask &= RFREG_MASK;
770 
771 	if (mask != RFREG_MASK) {
772 		b_msk_en = true;
773 		rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK,
774 				       mask);
775 		bit_shift = __ffs(mask);
776 		data = (data << bit_shift) & RFREG_MASK;
777 	}
778 
779 	val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) |
780 	      FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) |
781 	      FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) |
782 	      FIELD_PREP(B_SWSI_DATA_VAL_V1, data);
783 
784 	rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val);
785 
786 	return true;
787 }
788 
789 bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path,
790 			   u32 addr, u32 mask, u32 data)
791 {
792 	bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr);
793 
794 	if (rf_path >= rtwdev->chip->rf_path_num) {
795 		rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path);
796 		return false;
797 	}
798 
799 	if (ad_sel)
800 		return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data);
801 	else
802 		return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data);
803 }
804 EXPORT_SYMBOL(rtw89_phy_write_rf_v1);
805 
806 static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev)
807 {
808 	return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1;
809 }
810 
811 static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev,
812 			       enum rtw89_phy_idx phy_idx)
813 {
814 	const struct rtw89_chip_info *chip = rtwdev->chip;
815 
816 	chip->ops->bb_reset(rtwdev, phy_idx);
817 }
818 
819 static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev,
820 				    const struct rtw89_reg2_def *reg,
821 				    enum rtw89_rf_path rf_path,
822 				    void *extra_data)
823 {
824 	if (reg->addr == 0xfe)
825 		mdelay(50);
826 	else if (reg->addr == 0xfd)
827 		mdelay(5);
828 	else if (reg->addr == 0xfc)
829 		mdelay(1);
830 	else if (reg->addr == 0xfb)
831 		udelay(50);
832 	else if (reg->addr == 0xfa)
833 		udelay(5);
834 	else if (reg->addr == 0xf9)
835 		udelay(1);
836 	else
837 		rtw89_phy_write32(rtwdev, reg->addr, reg->data);
838 }
839 
840 union rtw89_phy_bb_gain_arg {
841 	u32 addr;
842 	struct {
843 		union {
844 			u8 type;
845 			struct {
846 				u8 rxsc_start:4;
847 				u8 bw:4;
848 			};
849 		};
850 		u8 path;
851 		u8 gain_band;
852 		u8 cfg_type;
853 	};
854 } __packed;
855 
856 static void
857 rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev,
858 			    union rtw89_phy_bb_gain_arg arg, u32 data)
859 {
860 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
861 	u8 type = arg.type;
862 	u8 path = arg.path;
863 	u8 gband = arg.gain_band;
864 	int i;
865 
866 	switch (type) {
867 	case 0:
868 		for (i = 0; i < 4; i++, data >>= 8)
869 			gain->lna_gain[gband][path][i] = data & 0xff;
870 		break;
871 	case 1:
872 		for (i = 4; i < 7; i++, data >>= 8)
873 			gain->lna_gain[gband][path][i] = data & 0xff;
874 		break;
875 	case 2:
876 		for (i = 0; i < 2; i++, data >>= 8)
877 			gain->tia_gain[gband][path][i] = data & 0xff;
878 		break;
879 	default:
880 		rtw89_warn(rtwdev,
881 			   "bb gain error {0x%x:0x%x} with unknown type: %d\n",
882 			   arg.addr, data, type);
883 		break;
884 	}
885 }
886 
887 enum rtw89_phy_bb_rxsc_start_idx {
888 	RTW89_BB_RXSC_START_IDX_FULL = 0,
889 	RTW89_BB_RXSC_START_IDX_20 = 1,
890 	RTW89_BB_RXSC_START_IDX_20_1 = 5,
891 	RTW89_BB_RXSC_START_IDX_40 = 9,
892 	RTW89_BB_RXSC_START_IDX_80 = 13,
893 };
894 
895 static void
896 rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev,
897 			  union rtw89_phy_bb_gain_arg arg, u32 data)
898 {
899 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
900 	u8 rxsc_start = arg.rxsc_start;
901 	u8 bw = arg.bw;
902 	u8 path = arg.path;
903 	u8 gband = arg.gain_band;
904 	u8 rxsc;
905 	s8 ofst;
906 	int i;
907 
908 	switch (bw) {
909 	case RTW89_CHANNEL_WIDTH_20:
910 		gain->rpl_ofst_20[gband][path] = (s8)data;
911 		break;
912 	case RTW89_CHANNEL_WIDTH_40:
913 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
914 			gain->rpl_ofst_40[gband][path][0] = (s8)data;
915 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
916 			for (i = 0; i < 2; i++, data >>= 8) {
917 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
918 				ofst = (s8)(data & 0xff);
919 				gain->rpl_ofst_40[gband][path][rxsc] = ofst;
920 			}
921 		}
922 		break;
923 	case RTW89_CHANNEL_WIDTH_80:
924 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
925 			gain->rpl_ofst_80[gband][path][0] = (s8)data;
926 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
927 			for (i = 0; i < 4; i++, data >>= 8) {
928 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
929 				ofst = (s8)(data & 0xff);
930 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
931 			}
932 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
933 			for (i = 0; i < 2; i++, data >>= 8) {
934 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
935 				ofst = (s8)(data & 0xff);
936 				gain->rpl_ofst_80[gband][path][rxsc] = ofst;
937 			}
938 		}
939 		break;
940 	case RTW89_CHANNEL_WIDTH_160:
941 		if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) {
942 			gain->rpl_ofst_160[gband][path][0] = (s8)data;
943 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) {
944 			for (i = 0; i < 4; i++, data >>= 8) {
945 				rxsc = RTW89_BB_RXSC_START_IDX_20 + i;
946 				ofst = (s8)(data & 0xff);
947 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
948 			}
949 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) {
950 			for (i = 0; i < 4; i++, data >>= 8) {
951 				rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i;
952 				ofst = (s8)(data & 0xff);
953 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
954 			}
955 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) {
956 			for (i = 0; i < 4; i++, data >>= 8) {
957 				rxsc = RTW89_BB_RXSC_START_IDX_40 + i;
958 				ofst = (s8)(data & 0xff);
959 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
960 			}
961 		} else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) {
962 			for (i = 0; i < 2; i++, data >>= 8) {
963 				rxsc = RTW89_BB_RXSC_START_IDX_80 + i;
964 				ofst = (s8)(data & 0xff);
965 				gain->rpl_ofst_160[gband][path][rxsc] = ofst;
966 			}
967 		}
968 		break;
969 	default:
970 		rtw89_warn(rtwdev,
971 			   "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n",
972 			   arg.addr, data, bw);
973 		break;
974 	}
975 }
976 
977 static void
978 rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev,
979 			     union rtw89_phy_bb_gain_arg arg, u32 data)
980 {
981 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
982 	u8 type = arg.type;
983 	u8 path = arg.path;
984 	u8 gband = arg.gain_band;
985 	int i;
986 
987 	switch (type) {
988 	case 0:
989 		for (i = 0; i < 4; i++, data >>= 8)
990 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
991 		break;
992 	case 1:
993 		for (i = 4; i < 7; i++, data >>= 8)
994 			gain->lna_gain_bypass[gband][path][i] = data & 0xff;
995 		break;
996 	default:
997 		rtw89_warn(rtwdev,
998 			   "bb gain bypass {0x%x:0x%x} with unknown type: %d\n",
999 			   arg.addr, data, type);
1000 		break;
1001 	}
1002 }
1003 
1004 static void
1005 rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev,
1006 			    union rtw89_phy_bb_gain_arg arg, u32 data)
1007 {
1008 	struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain;
1009 	u8 type = arg.type;
1010 	u8 path = arg.path;
1011 	u8 gband = arg.gain_band;
1012 	int i;
1013 
1014 	switch (type) {
1015 	case 0:
1016 		for (i = 0; i < 4; i++, data >>= 8)
1017 			gain->lna_op1db[gband][path][i] = data & 0xff;
1018 		break;
1019 	case 1:
1020 		for (i = 4; i < 7; i++, data >>= 8)
1021 			gain->lna_op1db[gband][path][i] = data & 0xff;
1022 		break;
1023 	case 2:
1024 		for (i = 0; i < 4; i++, data >>= 8)
1025 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1026 		break;
1027 	case 3:
1028 		for (i = 4; i < 8; i++, data >>= 8)
1029 			gain->tia_lna_op1db[gband][path][i] = data & 0xff;
1030 		break;
1031 	default:
1032 		rtw89_warn(rtwdev,
1033 			   "bb gain op1db {0x%x:0x%x} with unknown type: %d\n",
1034 			   arg.addr, data, type);
1035 		break;
1036 	}
1037 }
1038 
1039 static void rtw89_phy_config_bb_gain(struct rtw89_dev *rtwdev,
1040 				     const struct rtw89_reg2_def *reg,
1041 				     enum rtw89_rf_path rf_path,
1042 				     void *extra_data)
1043 {
1044 	const struct rtw89_chip_info *chip = rtwdev->chip;
1045 	union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr };
1046 	struct rtw89_efuse *efuse = &rtwdev->efuse;
1047 
1048 	if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR)
1049 		return;
1050 
1051 	if (arg.path >= chip->rf_path_num)
1052 		return;
1053 
1054 	if (arg.addr >= 0xf9 && arg.addr <= 0xfe) {
1055 		rtw89_warn(rtwdev, "bb gain table with flow ctrl\n");
1056 		return;
1057 	}
1058 
1059 	switch (arg.cfg_type) {
1060 	case 0:
1061 		rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data);
1062 		break;
1063 	case 1:
1064 		rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data);
1065 		break;
1066 	case 2:
1067 		rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data);
1068 		break;
1069 	case 3:
1070 		rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data);
1071 		break;
1072 	case 4:
1073 		/* This cfg_type is only used by rfe_type >= 50 with eFEM */
1074 		if (efuse->rfe_type < 50)
1075 			break;
1076 		fallthrough;
1077 	default:
1078 		rtw89_warn(rtwdev,
1079 			   "bb gain {0x%x:0x%x} with unknown cfg type: %d\n",
1080 			   arg.addr, reg->data, arg.cfg_type);
1081 		break;
1082 	}
1083 }
1084 
1085 static void
1086 rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev,
1087 			     const struct rtw89_reg2_def *reg,
1088 			     enum rtw89_rf_path rf_path,
1089 			     struct rtw89_fw_h2c_rf_reg_info *info)
1090 {
1091 	u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE;
1092 	u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE;
1093 
1094 	if (page >= RTW89_H2C_RF_PAGE_NUM) {
1095 		rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d",
1096 			   rf_path, info->curr_idx);
1097 		return;
1098 	}
1099 
1100 	info->rtw89_phy_config_rf_h2c[page][idx] =
1101 		cpu_to_le32((reg->addr << 20) | reg->data);
1102 	info->curr_idx++;
1103 }
1104 
1105 static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev,
1106 				      struct rtw89_fw_h2c_rf_reg_info *info)
1107 {
1108 	u16 remain = info->curr_idx;
1109 	u16 len = 0;
1110 	u8 i;
1111 	int ret = 0;
1112 
1113 	if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) {
1114 		rtw89_warn(rtwdev,
1115 			   "rf reg h2c total len %d larger than %d\n",
1116 			   remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE);
1117 		ret = -EINVAL;
1118 		goto out;
1119 	}
1120 
1121 	for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) {
1122 		len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain;
1123 		ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i);
1124 		if (ret)
1125 			goto out;
1126 	}
1127 out:
1128 	info->curr_idx = 0;
1129 
1130 	return ret;
1131 }
1132 
1133 static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev,
1134 					 const struct rtw89_reg2_def *reg,
1135 					 enum rtw89_rf_path rf_path,
1136 					 void *extra_data)
1137 {
1138 	u32 addr = reg->addr;
1139 
1140 	if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb ||
1141 	    addr == 0xfa || addr == 0xf9)
1142 		return;
1143 
1144 	if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100)
1145 		return;
1146 
1147 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1148 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1149 }
1150 
1151 static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev,
1152 				    const struct rtw89_reg2_def *reg,
1153 				    enum rtw89_rf_path rf_path,
1154 				    void *extra_data)
1155 {
1156 	if (reg->addr == 0xfe) {
1157 		mdelay(50);
1158 	} else if (reg->addr == 0xfd) {
1159 		mdelay(5);
1160 	} else if (reg->addr == 0xfc) {
1161 		mdelay(1);
1162 	} else if (reg->addr == 0xfb) {
1163 		udelay(50);
1164 	} else if (reg->addr == 0xfa) {
1165 		udelay(5);
1166 	} else if (reg->addr == 0xf9) {
1167 		udelay(1);
1168 	} else {
1169 		rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data);
1170 		rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1171 					     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1172 	}
1173 }
1174 
1175 void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev,
1176 				const struct rtw89_reg2_def *reg,
1177 				enum rtw89_rf_path rf_path,
1178 				void *extra_data)
1179 {
1180 	rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data);
1181 
1182 	if (reg->addr < 0x100)
1183 		return;
1184 
1185 	rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path,
1186 				     (struct rtw89_fw_h2c_rf_reg_info *)extra_data);
1187 }
1188 EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1);
1189 
1190 static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev,
1191 				  const struct rtw89_phy_table *table,
1192 				  u32 *headline_size, u32 *headline_idx,
1193 				  u8 rfe, u8 cv)
1194 {
1195 	const struct rtw89_reg2_def *reg;
1196 	u32 headline;
1197 	u32 compare, target;
1198 	u8 rfe_para, cv_para;
1199 	u8 cv_max = 0;
1200 	bool case_matched = false;
1201 	u32 i;
1202 
1203 	for (i = 0; i < table->n_regs; i++) {
1204 		reg = &table->regs[i];
1205 		headline = get_phy_headline(reg->addr);
1206 		if (headline != PHY_HEADLINE_VALID)
1207 			break;
1208 	}
1209 	*headline_size = i;
1210 	if (*headline_size == 0)
1211 		return 0;
1212 
1213 	/* case 1: RFE match, CV match */
1214 	compare = get_phy_compare(rfe, cv);
1215 	for (i = 0; i < *headline_size; i++) {
1216 		reg = &table->regs[i];
1217 		target = get_phy_target(reg->addr);
1218 		if (target == compare) {
1219 			*headline_idx = i;
1220 			return 0;
1221 		}
1222 	}
1223 
1224 	/* case 2: RFE match, CV don't care */
1225 	compare = get_phy_compare(rfe, PHY_COND_DONT_CARE);
1226 	for (i = 0; i < *headline_size; i++) {
1227 		reg = &table->regs[i];
1228 		target = get_phy_target(reg->addr);
1229 		if (target == compare) {
1230 			*headline_idx = i;
1231 			return 0;
1232 		}
1233 	}
1234 
1235 	/* case 3: RFE match, CV max in table */
1236 	for (i = 0; i < *headline_size; i++) {
1237 		reg = &table->regs[i];
1238 		rfe_para = get_phy_cond_rfe(reg->addr);
1239 		cv_para = get_phy_cond_cv(reg->addr);
1240 		if (rfe_para == rfe) {
1241 			if (cv_para >= cv_max) {
1242 				cv_max = cv_para;
1243 				*headline_idx = i;
1244 				case_matched = true;
1245 			}
1246 		}
1247 	}
1248 
1249 	if (case_matched)
1250 		return 0;
1251 
1252 	/* case 4: RFE don't care, CV max in table */
1253 	for (i = 0; i < *headline_size; i++) {
1254 		reg = &table->regs[i];
1255 		rfe_para = get_phy_cond_rfe(reg->addr);
1256 		cv_para = get_phy_cond_cv(reg->addr);
1257 		if (rfe_para == PHY_COND_DONT_CARE) {
1258 			if (cv_para >= cv_max) {
1259 				cv_max = cv_para;
1260 				*headline_idx = i;
1261 				case_matched = true;
1262 			}
1263 		}
1264 	}
1265 
1266 	if (case_matched)
1267 		return 0;
1268 
1269 	return -EINVAL;
1270 }
1271 
1272 static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev,
1273 			       const struct rtw89_phy_table *table,
1274 			       void (*config)(struct rtw89_dev *rtwdev,
1275 					      const struct rtw89_reg2_def *reg,
1276 					      enum rtw89_rf_path rf_path,
1277 					      void *data),
1278 			       void *extra_data)
1279 {
1280 	const struct rtw89_reg2_def *reg;
1281 	enum rtw89_rf_path rf_path = table->rf_path;
1282 	u8 rfe = rtwdev->efuse.rfe_type;
1283 	u8 cv = rtwdev->hal.cv;
1284 	u32 i;
1285 	u32 headline_size = 0, headline_idx = 0;
1286 	u32 target = 0, cfg_target;
1287 	u8 cond;
1288 	bool is_matched = true;
1289 	bool target_found = false;
1290 	int ret;
1291 
1292 	ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size,
1293 				     &headline_idx, rfe, cv);
1294 	if (ret) {
1295 		rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv);
1296 		return;
1297 	}
1298 
1299 	cfg_target = get_phy_target(table->regs[headline_idx].addr);
1300 	for (i = headline_size; i < table->n_regs; i++) {
1301 		reg = &table->regs[i];
1302 		cond = get_phy_cond(reg->addr);
1303 		switch (cond) {
1304 		case PHY_COND_BRANCH_IF:
1305 		case PHY_COND_BRANCH_ELIF:
1306 			target = get_phy_target(reg->addr);
1307 			break;
1308 		case PHY_COND_BRANCH_ELSE:
1309 			is_matched = false;
1310 			if (!target_found) {
1311 				rtw89_warn(rtwdev, "failed to load CR %x/%x\n",
1312 					   reg->addr, reg->data);
1313 				return;
1314 			}
1315 			break;
1316 		case PHY_COND_BRANCH_END:
1317 			is_matched = true;
1318 			target_found = false;
1319 			break;
1320 		case PHY_COND_CHECK:
1321 			if (target_found) {
1322 				is_matched = false;
1323 				break;
1324 			}
1325 
1326 			if (target == cfg_target) {
1327 				is_matched = true;
1328 				target_found = true;
1329 			} else {
1330 				is_matched = false;
1331 				target_found = false;
1332 			}
1333 			break;
1334 		default:
1335 			if (is_matched)
1336 				config(rtwdev, reg, rf_path, extra_data);
1337 			break;
1338 		}
1339 	}
1340 }
1341 
1342 void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev)
1343 {
1344 	const struct rtw89_chip_info *chip = rtwdev->chip;
1345 	const struct rtw89_phy_table *bb_table = chip->bb_table;
1346 	const struct rtw89_phy_table *bb_gain_table = chip->bb_gain_table;
1347 
1348 	rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL);
1349 	rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0);
1350 	if (bb_gain_table)
1351 		rtw89_phy_init_reg(rtwdev, bb_gain_table,
1352 				   rtw89_phy_config_bb_gain, NULL);
1353 	rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0);
1354 }
1355 
1356 static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev)
1357 {
1358 	rtw89_phy_write32(rtwdev, 0x8080, 0x4);
1359 	udelay(1);
1360 	return rtw89_phy_read32(rtwdev, 0x8080);
1361 }
1362 
1363 void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio)
1364 {
1365 	void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg,
1366 		       enum rtw89_rf_path rf_path, void *data);
1367 	const struct rtw89_chip_info *chip = rtwdev->chip;
1368 	const struct rtw89_phy_table *rf_table;
1369 	struct rtw89_fw_h2c_rf_reg_info *rf_reg_info;
1370 	u8 path;
1371 
1372 	rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL);
1373 	if (!rf_reg_info)
1374 		return;
1375 
1376 	for (path = RF_PATH_A; path < chip->rf_path_num; path++) {
1377 		rf_table = chip->rf_table[path];
1378 		rf_reg_info->rf_path = rf_table->rf_path;
1379 		if (noio)
1380 			config = rtw89_phy_config_rf_reg_noio;
1381 		else
1382 			config = rf_table->config ? rf_table->config :
1383 				 rtw89_phy_config_rf_reg;
1384 		rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info);
1385 		if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info))
1386 			rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n",
1387 				   rf_reg_info->rf_path);
1388 	}
1389 	kfree(rf_reg_info);
1390 }
1391 
1392 static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev)
1393 {
1394 	const struct rtw89_chip_info *chip = rtwdev->chip;
1395 	const struct rtw89_phy_table *nctl_table;
1396 	u32 val;
1397 	int ret;
1398 
1399 	/* IQK/DPK clock & reset */
1400 	rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3);
1401 	rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1);
1402 	rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000);
1403 	rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000);
1404 	if (chip->chip_id == RTL8852B)
1405 		rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2);
1406 
1407 	/* check 0x8080 */
1408 	rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8);
1409 
1410 	ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10,
1411 				1000, false, rtwdev);
1412 	if (ret)
1413 		rtw89_err(rtwdev, "failed to poll nctl block\n");
1414 
1415 	nctl_table = chip->nctl_table;
1416 	rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL);
1417 }
1418 
1419 static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr)
1420 {
1421 	u32 phy_page = addr >> 8;
1422 	u32 ofst = 0;
1423 
1424 	switch (phy_page) {
1425 	case 0x6:
1426 	case 0x7:
1427 	case 0x8:
1428 	case 0x9:
1429 	case 0xa:
1430 	case 0xb:
1431 	case 0xc:
1432 	case 0xd:
1433 	case 0x19:
1434 	case 0x1a:
1435 	case 0x1b:
1436 		ofst = 0x2000;
1437 		break;
1438 	default:
1439 		/* warning case */
1440 		ofst = 0;
1441 		break;
1442 	}
1443 
1444 	if (phy_page >= 0x40 && phy_page <= 0x4f)
1445 		ofst = 0x2000;
1446 
1447 	return ofst;
1448 }
1449 
1450 void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1451 			   u32 data, enum rtw89_phy_idx phy_idx)
1452 {
1453 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1454 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1455 	rtw89_phy_write32_mask(rtwdev, addr, mask, data);
1456 }
1457 EXPORT_SYMBOL(rtw89_phy_write32_idx);
1458 
1459 u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1460 			 enum rtw89_phy_idx phy_idx)
1461 {
1462 	if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1)
1463 		addr += rtw89_phy0_phy1_offset(rtwdev, addr);
1464 	return rtw89_phy_read32_mask(rtwdev, addr, mask);
1465 }
1466 EXPORT_SYMBOL(rtw89_phy_read32_idx);
1467 
1468 void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask,
1469 			    u32 val)
1470 {
1471 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0);
1472 
1473 	if (!rtwdev->dbcc_en)
1474 		return;
1475 
1476 	rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1);
1477 }
1478 
1479 void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev,
1480 			      const struct rtw89_phy_reg3_tbl *tbl)
1481 {
1482 	const struct rtw89_reg3_def *reg3;
1483 	int i;
1484 
1485 	for (i = 0; i < tbl->size; i++) {
1486 		reg3 = &tbl->reg3[i];
1487 		rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data);
1488 	}
1489 }
1490 EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl);
1491 
1492 static const u8 rtw89_rs_idx_max[] = {
1493 	[RTW89_RS_CCK] = RTW89_RATE_CCK_MAX,
1494 	[RTW89_RS_OFDM] = RTW89_RATE_OFDM_MAX,
1495 	[RTW89_RS_MCS] = RTW89_RATE_MCS_MAX,
1496 	[RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_MAX,
1497 	[RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_MAX,
1498 };
1499 
1500 static const u8 rtw89_rs_nss_max[] = {
1501 	[RTW89_RS_CCK] = 1,
1502 	[RTW89_RS_OFDM] = 1,
1503 	[RTW89_RS_MCS] = RTW89_NSS_MAX,
1504 	[RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_MAX,
1505 	[RTW89_RS_OFFSET] = 1,
1506 };
1507 
1508 static const u8 _byr_of_rs[] = {
1509 	[RTW89_RS_CCK] = offsetof(struct rtw89_txpwr_byrate, cck),
1510 	[RTW89_RS_OFDM] = offsetof(struct rtw89_txpwr_byrate, ofdm),
1511 	[RTW89_RS_MCS] = offsetof(struct rtw89_txpwr_byrate, mcs),
1512 	[RTW89_RS_HEDCM] = offsetof(struct rtw89_txpwr_byrate, hedcm),
1513 	[RTW89_RS_OFFSET] = offsetof(struct rtw89_txpwr_byrate, offset),
1514 };
1515 
1516 #define _byr_seek(rs, raw) ((s8 *)(raw) + _byr_of_rs[rs])
1517 #define _byr_idx(rs, nss, idx) ((nss) * rtw89_rs_idx_max[rs] + (idx))
1518 #define _byr_chk(rs, nss, idx) \
1519 	((nss) < rtw89_rs_nss_max[rs] && (idx) < rtw89_rs_idx_max[rs])
1520 
1521 void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev,
1522 				 const struct rtw89_txpwr_table *tbl)
1523 {
1524 	const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data;
1525 	const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size;
1526 	s8 *byr;
1527 	u32 data;
1528 	u8 i, idx;
1529 
1530 	for (; cfg < end; cfg++) {
1531 		byr = _byr_seek(cfg->rs, &rtwdev->byr[cfg->band]);
1532 		data = cfg->data;
1533 
1534 		for (i = 0; i < cfg->len; i++, data >>= 8) {
1535 			idx = _byr_idx(cfg->rs, cfg->nss, (cfg->shf + i));
1536 			byr[idx] = (s8)(data & 0xff);
1537 		}
1538 	}
1539 }
1540 EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate);
1541 
1542 #define _phy_txpwr_rf_to_mac(rtwdev, txpwr_rf)				\
1543 ({									\
1544 	const struct rtw89_chip_info *__c = (rtwdev)->chip;		\
1545 	(txpwr_rf) >> (__c->txpwr_factor_rf - __c->txpwr_factor_mac);	\
1546 })
1547 
1548 static
1549 s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band,
1550 			       const struct rtw89_rate_desc *rate_desc)
1551 {
1552 	s8 *byr;
1553 	u8 idx;
1554 
1555 	if (rate_desc->rs == RTW89_RS_CCK)
1556 		band = RTW89_BAND_2G;
1557 
1558 	if (!_byr_chk(rate_desc->rs, rate_desc->nss, rate_desc->idx)) {
1559 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1560 			    "[TXPWR] unknown byrate desc rs=%d nss=%d idx=%d\n",
1561 			    rate_desc->rs, rate_desc->nss, rate_desc->idx);
1562 
1563 		return 0;
1564 	}
1565 
1566 	byr = _byr_seek(rate_desc->rs, &rtwdev->byr[band]);
1567 	idx = _byr_idx(rate_desc->rs, rate_desc->nss, rate_desc->idx);
1568 
1569 	return _phy_txpwr_rf_to_mac(rtwdev, byr[idx]);
1570 }
1571 
1572 static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g)
1573 {
1574 	switch (channel_6g) {
1575 	case 1 ... 29:
1576 		return (channel_6g - 1) / 2;
1577 	case 33 ... 61:
1578 		return (channel_6g - 3) / 2;
1579 	case 65 ... 93:
1580 		return (channel_6g - 5) / 2;
1581 	case 97 ... 125:
1582 		return (channel_6g - 7) / 2;
1583 	case 129 ... 157:
1584 		return (channel_6g - 9) / 2;
1585 	case 161 ... 189:
1586 		return (channel_6g - 11) / 2;
1587 	case 193 ... 221:
1588 		return (channel_6g - 13) / 2;
1589 	case 225 ... 253:
1590 		return (channel_6g - 15) / 2;
1591 	default:
1592 		rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g);
1593 		return 0;
1594 	}
1595 }
1596 
1597 static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel)
1598 {
1599 	if (band == RTW89_BAND_6G)
1600 		return rtw89_channel_6g_to_idx(rtwdev, channel);
1601 
1602 	switch (channel) {
1603 	case 1 ... 14:
1604 		return channel - 1;
1605 	case 36 ... 64:
1606 		return (channel - 36) / 2;
1607 	case 100 ... 144:
1608 		return ((channel - 100) / 2) + 15;
1609 	case 149 ... 177:
1610 		return ((channel - 149) / 2) + 38;
1611 	default:
1612 		rtw89_warn(rtwdev, "unknown channel: %d\n", channel);
1613 		return 0;
1614 	}
1615 }
1616 
1617 s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band,
1618 			      u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch)
1619 {
1620 	const struct rtw89_chip_info *chip = rtwdev->chip;
1621 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1622 	u8 regd = rtw89_regd_get(rtwdev, band);
1623 	s8 lmt = 0, sar;
1624 
1625 	switch (band) {
1626 	case RTW89_BAND_2G:
1627 		lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf][regd][ch_idx];
1628 		if (!lmt)
1629 			lmt = (*chip->txpwr_lmt_2g)[bw][ntx][rs][bf]
1630 						   [RTW89_WW][ch_idx];
1631 		break;
1632 	case RTW89_BAND_5G:
1633 		lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf][regd][ch_idx];
1634 		if (!lmt)
1635 			lmt = (*chip->txpwr_lmt_5g)[bw][ntx][rs][bf]
1636 						   [RTW89_WW][ch_idx];
1637 		break;
1638 	case RTW89_BAND_6G:
1639 		lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf][regd][ch_idx];
1640 		if (!lmt)
1641 			lmt = (*chip->txpwr_lmt_6g)[bw][ntx][rs][bf]
1642 						   [RTW89_WW][ch_idx];
1643 		break;
1644 	default:
1645 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1646 		return 0;
1647 	}
1648 
1649 	lmt = _phy_txpwr_rf_to_mac(rtwdev, lmt);
1650 	sar = rtw89_query_sar(rtwdev);
1651 
1652 	return min(lmt, sar);
1653 }
1654 EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit);
1655 
1656 #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch)		\
1657 	do {								\
1658 		u8 __i;							\
1659 		for (__i = 0; __i < RTW89_BF_NUM; __i++)		\
1660 			ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev,	\
1661 							      band,	\
1662 							      bw, ntx,	\
1663 							      rs, __i,	\
1664 							      (ch));	\
1665 	} while (0)
1666 
1667 static void rtw89_phy_fill_txpwr_limit_20m(struct rtw89_dev *rtwdev,
1668 					   struct rtw89_txpwr_limit *lmt,
1669 					   u8 band, u8 ntx, u8 ch)
1670 {
1671 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1672 				    ntx, RTW89_RS_CCK, ch);
1673 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1674 				    ntx, RTW89_RS_CCK, ch);
1675 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1676 				    ntx, RTW89_RS_OFDM, ch);
1677 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
1678 				    RTW89_CHANNEL_WIDTH_20,
1679 				    ntx, RTW89_RS_MCS, ch);
1680 }
1681 
1682 static void rtw89_phy_fill_txpwr_limit_40m(struct rtw89_dev *rtwdev,
1683 					   struct rtw89_txpwr_limit *lmt,
1684 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1685 {
1686 	__fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20,
1687 				    ntx, RTW89_RS_CCK, ch - 2);
1688 	__fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40,
1689 				    ntx, RTW89_RS_CCK, ch);
1690 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1691 				    ntx, RTW89_RS_OFDM, pri_ch);
1692 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
1693 				    RTW89_CHANNEL_WIDTH_20,
1694 				    ntx, RTW89_RS_MCS, ch - 2);
1695 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
1696 				    RTW89_CHANNEL_WIDTH_20,
1697 				    ntx, RTW89_RS_MCS, ch + 2);
1698 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
1699 				    RTW89_CHANNEL_WIDTH_40,
1700 				    ntx, RTW89_RS_MCS, ch);
1701 }
1702 
1703 static void rtw89_phy_fill_txpwr_limit_80m(struct rtw89_dev *rtwdev,
1704 					   struct rtw89_txpwr_limit *lmt,
1705 					   u8 band, u8 ntx, u8 ch, u8 pri_ch)
1706 {
1707 	s8 val_0p5_n[RTW89_BF_NUM];
1708 	s8 val_0p5_p[RTW89_BF_NUM];
1709 	u8 i;
1710 
1711 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1712 				    ntx, RTW89_RS_OFDM, pri_ch);
1713 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
1714 				    RTW89_CHANNEL_WIDTH_20,
1715 				    ntx, RTW89_RS_MCS, ch - 6);
1716 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
1717 				    RTW89_CHANNEL_WIDTH_20,
1718 				    ntx, RTW89_RS_MCS, ch - 2);
1719 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
1720 				    RTW89_CHANNEL_WIDTH_20,
1721 				    ntx, RTW89_RS_MCS, ch + 2);
1722 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
1723 				    RTW89_CHANNEL_WIDTH_20,
1724 				    ntx, RTW89_RS_MCS, ch + 6);
1725 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
1726 				    RTW89_CHANNEL_WIDTH_40,
1727 				    ntx, RTW89_RS_MCS, ch - 4);
1728 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
1729 				    RTW89_CHANNEL_WIDTH_40,
1730 				    ntx, RTW89_RS_MCS, ch + 4);
1731 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
1732 				    RTW89_CHANNEL_WIDTH_80,
1733 				    ntx, RTW89_RS_MCS, ch);
1734 
1735 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
1736 				    ntx, RTW89_RS_MCS, ch - 4);
1737 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
1738 				    ntx, RTW89_RS_MCS, ch + 4);
1739 
1740 	for (i = 0; i < RTW89_BF_NUM; i++)
1741 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1742 }
1743 
1744 static void rtw89_phy_fill_txpwr_limit_160m(struct rtw89_dev *rtwdev,
1745 					    struct rtw89_txpwr_limit *lmt,
1746 					    u8 band, u8 ntx, u8 ch, u8 pri_ch)
1747 {
1748 	s8 val_0p5_n[RTW89_BF_NUM];
1749 	s8 val_0p5_p[RTW89_BF_NUM];
1750 	s8 val_2p5_n[RTW89_BF_NUM];
1751 	s8 val_2p5_p[RTW89_BF_NUM];
1752 	u8 i;
1753 
1754 	/* fill ofdm section */
1755 	__fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20,
1756 				    ntx, RTW89_RS_OFDM, pri_ch);
1757 
1758 	/* fill mcs 20m section */
1759 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band,
1760 				    RTW89_CHANNEL_WIDTH_20,
1761 				    ntx, RTW89_RS_MCS, ch - 14);
1762 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band,
1763 				    RTW89_CHANNEL_WIDTH_20,
1764 				    ntx, RTW89_RS_MCS, ch - 10);
1765 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band,
1766 				    RTW89_CHANNEL_WIDTH_20,
1767 				    ntx, RTW89_RS_MCS, ch - 6);
1768 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band,
1769 				    RTW89_CHANNEL_WIDTH_20,
1770 				    ntx, RTW89_RS_MCS, ch - 2);
1771 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band,
1772 				    RTW89_CHANNEL_WIDTH_20,
1773 				    ntx, RTW89_RS_MCS, ch + 2);
1774 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band,
1775 				    RTW89_CHANNEL_WIDTH_20,
1776 				    ntx, RTW89_RS_MCS, ch + 6);
1777 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band,
1778 				    RTW89_CHANNEL_WIDTH_20,
1779 				    ntx, RTW89_RS_MCS, ch + 10);
1780 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band,
1781 				    RTW89_CHANNEL_WIDTH_20,
1782 				    ntx, RTW89_RS_MCS, ch + 14);
1783 
1784 	/* fill mcs 40m section */
1785 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band,
1786 				    RTW89_CHANNEL_WIDTH_40,
1787 				    ntx, RTW89_RS_MCS, ch - 12);
1788 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band,
1789 				    RTW89_CHANNEL_WIDTH_40,
1790 				    ntx, RTW89_RS_MCS, ch - 4);
1791 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band,
1792 				    RTW89_CHANNEL_WIDTH_40,
1793 				    ntx, RTW89_RS_MCS, ch + 4);
1794 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band,
1795 				    RTW89_CHANNEL_WIDTH_40,
1796 				    ntx, RTW89_RS_MCS, ch + 12);
1797 
1798 	/* fill mcs 80m section */
1799 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band,
1800 				    RTW89_CHANNEL_WIDTH_80,
1801 				    ntx, RTW89_RS_MCS, ch - 8);
1802 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band,
1803 				    RTW89_CHANNEL_WIDTH_80,
1804 				    ntx, RTW89_RS_MCS, ch + 8);
1805 
1806 	/* fill mcs 160m section */
1807 	__fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band,
1808 				    RTW89_CHANNEL_WIDTH_160,
1809 				    ntx, RTW89_RS_MCS, ch);
1810 
1811 	/* fill mcs 40m 0p5 section */
1812 	__fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40,
1813 				    ntx, RTW89_RS_MCS, ch - 4);
1814 	__fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40,
1815 				    ntx, RTW89_RS_MCS, ch + 4);
1816 
1817 	for (i = 0; i < RTW89_BF_NUM; i++)
1818 		lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]);
1819 
1820 	/* fill mcs 40m 2p5 section */
1821 	__fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40,
1822 				    ntx, RTW89_RS_MCS, ch - 8);
1823 	__fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40,
1824 				    ntx, RTW89_RS_MCS, ch + 8);
1825 
1826 	for (i = 0; i < RTW89_BF_NUM; i++)
1827 		lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]);
1828 }
1829 
1830 static
1831 void rtw89_phy_fill_txpwr_limit(struct rtw89_dev *rtwdev,
1832 				const struct rtw89_chan *chan,
1833 				struct rtw89_txpwr_limit *lmt,
1834 				u8 ntx)
1835 {
1836 	u8 band = chan->band_type;
1837 	u8 pri_ch = chan->primary_channel;
1838 	u8 ch = chan->channel;
1839 	u8 bw = chan->band_width;
1840 
1841 	memset(lmt, 0, sizeof(*lmt));
1842 
1843 	switch (bw) {
1844 	case RTW89_CHANNEL_WIDTH_20:
1845 		rtw89_phy_fill_txpwr_limit_20m(rtwdev, lmt, band, ntx, ch);
1846 		break;
1847 	case RTW89_CHANNEL_WIDTH_40:
1848 		rtw89_phy_fill_txpwr_limit_40m(rtwdev, lmt, band, ntx, ch,
1849 					       pri_ch);
1850 		break;
1851 	case RTW89_CHANNEL_WIDTH_80:
1852 		rtw89_phy_fill_txpwr_limit_80m(rtwdev, lmt, band, ntx, ch,
1853 					       pri_ch);
1854 		break;
1855 	case RTW89_CHANNEL_WIDTH_160:
1856 		rtw89_phy_fill_txpwr_limit_160m(rtwdev, lmt, band, ntx, ch,
1857 						pri_ch);
1858 		break;
1859 	}
1860 }
1861 
1862 static s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band,
1863 					u8 ru, u8 ntx, u8 ch)
1864 {
1865 	const struct rtw89_chip_info *chip = rtwdev->chip;
1866 	u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch);
1867 	u8 regd = rtw89_regd_get(rtwdev, band);
1868 	s8 lmt_ru = 0, sar;
1869 
1870 	switch (band) {
1871 	case RTW89_BAND_2G:
1872 		lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx][regd][ch_idx];
1873 		if (!lmt_ru)
1874 			lmt_ru = (*chip->txpwr_lmt_ru_2g)[ru][ntx]
1875 							 [RTW89_WW][ch_idx];
1876 		break;
1877 	case RTW89_BAND_5G:
1878 		lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx][regd][ch_idx];
1879 		if (!lmt_ru)
1880 			lmt_ru = (*chip->txpwr_lmt_ru_5g)[ru][ntx]
1881 							 [RTW89_WW][ch_idx];
1882 		break;
1883 	case RTW89_BAND_6G:
1884 		lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx][regd][ch_idx];
1885 		if (!lmt_ru)
1886 			lmt_ru = (*chip->txpwr_lmt_ru_6g)[ru][ntx]
1887 							 [RTW89_WW][ch_idx];
1888 		break;
1889 	default:
1890 		rtw89_warn(rtwdev, "unknown band type: %d\n", band);
1891 		return 0;
1892 	}
1893 
1894 	lmt_ru = _phy_txpwr_rf_to_mac(rtwdev, lmt_ru);
1895 	sar = rtw89_query_sar(rtwdev);
1896 
1897 	return min(lmt_ru, sar);
1898 }
1899 
1900 static void
1901 rtw89_phy_fill_txpwr_limit_ru_20m(struct rtw89_dev *rtwdev,
1902 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1903 				  u8 band, u8 ntx, u8 ch)
1904 {
1905 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1906 							RTW89_RU26,
1907 							ntx, ch);
1908 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1909 							RTW89_RU52,
1910 							ntx, ch);
1911 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1912 							 RTW89_RU106,
1913 							 ntx, ch);
1914 }
1915 
1916 static void
1917 rtw89_phy_fill_txpwr_limit_ru_40m(struct rtw89_dev *rtwdev,
1918 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1919 				  u8 band, u8 ntx, u8 ch)
1920 {
1921 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1922 							RTW89_RU26,
1923 							ntx, ch - 2);
1924 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1925 							RTW89_RU26,
1926 							ntx, ch + 2);
1927 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1928 							RTW89_RU52,
1929 							ntx, ch - 2);
1930 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1931 							RTW89_RU52,
1932 							ntx, ch + 2);
1933 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1934 							 RTW89_RU106,
1935 							 ntx, ch - 2);
1936 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1937 							 RTW89_RU106,
1938 							 ntx, ch + 2);
1939 }
1940 
1941 static void
1942 rtw89_phy_fill_txpwr_limit_ru_80m(struct rtw89_dev *rtwdev,
1943 				  struct rtw89_txpwr_limit_ru *lmt_ru,
1944 				  u8 band, u8 ntx, u8 ch)
1945 {
1946 	lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1947 							RTW89_RU26,
1948 							ntx, ch - 6);
1949 	lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1950 							RTW89_RU26,
1951 							ntx, ch - 2);
1952 	lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1953 							RTW89_RU26,
1954 							ntx, ch + 2);
1955 	lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1956 							RTW89_RU26,
1957 							ntx, ch + 6);
1958 	lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1959 							RTW89_RU52,
1960 							ntx, ch - 6);
1961 	lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1962 							RTW89_RU52,
1963 							ntx, ch - 2);
1964 	lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1965 							RTW89_RU52,
1966 							ntx, ch + 2);
1967 	lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1968 							RTW89_RU52,
1969 							ntx, ch + 6);
1970 	lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1971 							 RTW89_RU106,
1972 							 ntx, ch - 6);
1973 	lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1974 							 RTW89_RU106,
1975 							 ntx, ch - 2);
1976 	lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1977 							 RTW89_RU106,
1978 							 ntx, ch + 2);
1979 	lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1980 							 RTW89_RU106,
1981 							 ntx, ch + 6);
1982 }
1983 
1984 static void
1985 rtw89_phy_fill_txpwr_limit_ru_160m(struct rtw89_dev *rtwdev,
1986 				   struct rtw89_txpwr_limit_ru *lmt_ru,
1987 				   u8 band, u8 ntx, u8 ch)
1988 {
1989 	static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 };
1990 	int i;
1991 
1992 	static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM);
1993 	for (i = 0; i < RTW89_RU_SEC_NUM; i++) {
1994 		lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1995 								RTW89_RU26,
1996 								ntx,
1997 								ch + ofst[i]);
1998 		lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
1999 								RTW89_RU52,
2000 								ntx,
2001 								ch + ofst[i]);
2002 		lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band,
2003 								 RTW89_RU106,
2004 								 ntx,
2005 								 ch + ofst[i]);
2006 	}
2007 }
2008 
2009 static
2010 void rtw89_phy_fill_txpwr_limit_ru(struct rtw89_dev *rtwdev,
2011 				   const struct rtw89_chan *chan,
2012 				   struct rtw89_txpwr_limit_ru *lmt_ru,
2013 				   u8 ntx)
2014 {
2015 	u8 band = chan->band_type;
2016 	u8 ch = chan->channel;
2017 	u8 bw = chan->band_width;
2018 
2019 	memset(lmt_ru, 0, sizeof(*lmt_ru));
2020 
2021 	switch (bw) {
2022 	case RTW89_CHANNEL_WIDTH_20:
2023 		rtw89_phy_fill_txpwr_limit_ru_20m(rtwdev, lmt_ru, band, ntx,
2024 						  ch);
2025 		break;
2026 	case RTW89_CHANNEL_WIDTH_40:
2027 		rtw89_phy_fill_txpwr_limit_ru_40m(rtwdev, lmt_ru, band, ntx,
2028 						  ch);
2029 		break;
2030 	case RTW89_CHANNEL_WIDTH_80:
2031 		rtw89_phy_fill_txpwr_limit_ru_80m(rtwdev, lmt_ru, band, ntx,
2032 						  ch);
2033 		break;
2034 	case RTW89_CHANNEL_WIDTH_160:
2035 		rtw89_phy_fill_txpwr_limit_ru_160m(rtwdev, lmt_ru, band, ntx,
2036 						   ch);
2037 		break;
2038 	}
2039 }
2040 
2041 void rtw89_phy_set_txpwr_byrate(struct rtw89_dev *rtwdev,
2042 				const struct rtw89_chan *chan,
2043 				enum rtw89_phy_idx phy_idx)
2044 {
2045 	u8 max_nss_num = rtwdev->chip->rf_path_num;
2046 	static const u8 rs[] = {
2047 		RTW89_RS_CCK,
2048 		RTW89_RS_OFDM,
2049 		RTW89_RS_MCS,
2050 		RTW89_RS_HEDCM,
2051 	};
2052 	struct rtw89_rate_desc cur;
2053 	u8 band = chan->band_type;
2054 	u8 ch = chan->channel;
2055 	u32 addr, val;
2056 	s8 v[4] = {};
2057 	u8 i;
2058 
2059 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
2060 		    "[TXPWR] set txpwr byrate with ch=%d\n", ch);
2061 
2062 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_CCK] % 4);
2063 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_OFDM] % 4);
2064 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_MCS] % 4);
2065 	BUILD_BUG_ON(rtw89_rs_idx_max[RTW89_RS_HEDCM] % 4);
2066 
2067 	addr = R_AX_PWR_BY_RATE;
2068 	for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) {
2069 		for (i = 0; i < ARRAY_SIZE(rs); i++) {
2070 			if (cur.nss >= rtw89_rs_nss_max[rs[i]])
2071 				continue;
2072 
2073 			cur.rs = rs[i];
2074 			for (cur.idx = 0; cur.idx < rtw89_rs_idx_max[rs[i]];
2075 			     cur.idx++) {
2076 				v[cur.idx % 4] =
2077 					rtw89_phy_read_txpwr_byrate(rtwdev,
2078 								    band,
2079 								    &cur);
2080 
2081 				if ((cur.idx + 1) % 4)
2082 					continue;
2083 
2084 				val = FIELD_PREP(GENMASK(7, 0), v[0]) |
2085 				      FIELD_PREP(GENMASK(15, 8), v[1]) |
2086 				      FIELD_PREP(GENMASK(23, 16), v[2]) |
2087 				      FIELD_PREP(GENMASK(31, 24), v[3]);
2088 
2089 				rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr,
2090 							val);
2091 				addr += 4;
2092 			}
2093 		}
2094 	}
2095 }
2096 EXPORT_SYMBOL(rtw89_phy_set_txpwr_byrate);
2097 
2098 void rtw89_phy_set_txpwr_offset(struct rtw89_dev *rtwdev,
2099 				const struct rtw89_chan *chan,
2100 				enum rtw89_phy_idx phy_idx)
2101 {
2102 	struct rtw89_rate_desc desc = {
2103 		.nss = RTW89_NSS_1,
2104 		.rs = RTW89_RS_OFFSET,
2105 	};
2106 	u8 band = chan->band_type;
2107 	s8 v[RTW89_RATE_OFFSET_MAX] = {};
2108 	u32 val;
2109 
2110 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n");
2111 
2112 	for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_MAX; desc.idx++)
2113 		v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, &desc);
2114 
2115 	BUILD_BUG_ON(RTW89_RATE_OFFSET_MAX != 5);
2116 	val = FIELD_PREP(GENMASK(3, 0), v[0]) |
2117 	      FIELD_PREP(GENMASK(7, 4), v[1]) |
2118 	      FIELD_PREP(GENMASK(11, 8), v[2]) |
2119 	      FIELD_PREP(GENMASK(15, 12), v[3]) |
2120 	      FIELD_PREP(GENMASK(19, 16), v[4]);
2121 
2122 	rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL,
2123 				     GENMASK(19, 0), val);
2124 }
2125 EXPORT_SYMBOL(rtw89_phy_set_txpwr_offset);
2126 
2127 void rtw89_phy_set_txpwr_limit(struct rtw89_dev *rtwdev,
2128 			       const struct rtw89_chan *chan,
2129 			       enum rtw89_phy_idx phy_idx)
2130 {
2131 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
2132 	struct rtw89_txpwr_limit lmt;
2133 	u8 ch = chan->channel;
2134 	u8 bw = chan->band_width;
2135 	const s8 *ptr;
2136 	u32 addr, val;
2137 	u8 i, j;
2138 
2139 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
2140 		    "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw);
2141 
2142 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit) !=
2143 		     RTW89_TXPWR_LMT_PAGE_SIZE);
2144 
2145 	addr = R_AX_PWR_LMT;
2146 	for (i = 0; i < max_ntx_num; i++) {
2147 		rtw89_phy_fill_txpwr_limit(rtwdev, chan, &lmt, i);
2148 
2149 		ptr = (s8 *)&lmt;
2150 		for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE;
2151 		     j += 4, addr += 4, ptr += 4) {
2152 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
2153 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
2154 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
2155 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
2156 
2157 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
2158 		}
2159 	}
2160 }
2161 EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit);
2162 
2163 void rtw89_phy_set_txpwr_limit_ru(struct rtw89_dev *rtwdev,
2164 				  const struct rtw89_chan *chan,
2165 				  enum rtw89_phy_idx phy_idx)
2166 {
2167 	u8 max_ntx_num = rtwdev->chip->rf_path_num;
2168 	struct rtw89_txpwr_limit_ru lmt_ru;
2169 	u8 ch = chan->channel;
2170 	u8 bw = chan->band_width;
2171 	const s8 *ptr;
2172 	u32 addr, val;
2173 	u8 i, j;
2174 
2175 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
2176 		    "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw);
2177 
2178 	BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru) !=
2179 		     RTW89_TXPWR_LMT_RU_PAGE_SIZE);
2180 
2181 	addr = R_AX_PWR_RU_LMT;
2182 	for (i = 0; i < max_ntx_num; i++) {
2183 		rtw89_phy_fill_txpwr_limit_ru(rtwdev, chan, &lmt_ru, i);
2184 
2185 		ptr = (s8 *)&lmt_ru;
2186 		for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE;
2187 		     j += 4, addr += 4, ptr += 4) {
2188 			val = FIELD_PREP(GENMASK(7, 0), ptr[0]) |
2189 			      FIELD_PREP(GENMASK(15, 8), ptr[1]) |
2190 			      FIELD_PREP(GENMASK(23, 16), ptr[2]) |
2191 			      FIELD_PREP(GENMASK(31, 24), ptr[3]);
2192 
2193 			rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val);
2194 		}
2195 	}
2196 }
2197 EXPORT_SYMBOL(rtw89_phy_set_txpwr_limit_ru);
2198 
2199 struct rtw89_phy_iter_ra_data {
2200 	struct rtw89_dev *rtwdev;
2201 	struct sk_buff *c2h;
2202 };
2203 
2204 static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta)
2205 {
2206 	struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data;
2207 	struct rtw89_dev *rtwdev = ra_data->rtwdev;
2208 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2209 	struct rtw89_ra_report *ra_report = &rtwsta->ra_report;
2210 	struct sk_buff *c2h = ra_data->c2h;
2211 	u8 mode, rate, bw, giltf, mac_id;
2212 	u16 legacy_bitrate;
2213 	bool valid;
2214 	u8 mcs = 0;
2215 
2216 	mac_id = RTW89_GET_PHY_C2H_RA_RPT_MACID(c2h->data);
2217 	if (mac_id != rtwsta->mac_id)
2218 		return;
2219 
2220 	rate = RTW89_GET_PHY_C2H_RA_RPT_MCSNSS(c2h->data);
2221 	bw = RTW89_GET_PHY_C2H_RA_RPT_BW(c2h->data);
2222 	giltf = RTW89_GET_PHY_C2H_RA_RPT_GILTF(c2h->data);
2223 	mode = RTW89_GET_PHY_C2H_RA_RPT_MD_SEL(c2h->data);
2224 
2225 	if (mode == RTW89_RA_RPT_MODE_LEGACY) {
2226 		valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate);
2227 		if (!valid)
2228 			return;
2229 	}
2230 
2231 	memset(&ra_report->txrate, 0, sizeof(ra_report->txrate));
2232 
2233 	switch (mode) {
2234 	case RTW89_RA_RPT_MODE_LEGACY:
2235 		ra_report->txrate.legacy = legacy_bitrate;
2236 		break;
2237 	case RTW89_RA_RPT_MODE_HT:
2238 		ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS;
2239 		if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw))
2240 			rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate),
2241 						FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate));
2242 		else
2243 			rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate);
2244 		ra_report->txrate.mcs = rate;
2245 		if (giltf)
2246 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
2247 		mcs = ra_report->txrate.mcs & 0x07;
2248 		break;
2249 	case RTW89_RA_RPT_MODE_VHT:
2250 		ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
2251 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
2252 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
2253 		if (giltf)
2254 			ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
2255 		mcs = ra_report->txrate.mcs;
2256 		break;
2257 	case RTW89_RA_RPT_MODE_HE:
2258 		ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS;
2259 		ra_report->txrate.mcs = FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate);
2260 		ra_report->txrate.nss = FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate) + 1;
2261 		if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08)
2262 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2263 		else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16)
2264 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2265 		else
2266 			ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2;
2267 		mcs = ra_report->txrate.mcs;
2268 		break;
2269 	}
2270 
2271 	ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw);
2272 	ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate);
2273 	ra_report->hw_rate = FIELD_PREP(RTW89_HW_RATE_MASK_MOD, mode) |
2274 			     FIELD_PREP(RTW89_HW_RATE_MASK_VAL, rate);
2275 	ra_report->might_fallback_legacy = mcs <= 2;
2276 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report);
2277 	rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1;
2278 }
2279 
2280 static void
2281 rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
2282 {
2283 	struct rtw89_phy_iter_ra_data ra_data;
2284 
2285 	ra_data.rtwdev = rtwdev;
2286 	ra_data.c2h = c2h;
2287 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2288 					  rtw89_phy_c2h_ra_rpt_iter,
2289 					  &ra_data);
2290 }
2291 
2292 static
2293 void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev,
2294 					  struct sk_buff *c2h, u32 len) = {
2295 	[RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt,
2296 	[RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL,
2297 	[RTW89_PHY_C2H_FUNC_TXSTS] = NULL,
2298 };
2299 
2300 void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
2301 			  u32 len, u8 class, u8 func)
2302 {
2303 	void (*handler)(struct rtw89_dev *rtwdev,
2304 			struct sk_buff *c2h, u32 len) = NULL;
2305 
2306 	switch (class) {
2307 	case RTW89_PHY_C2H_CLASS_RA:
2308 		if (func < RTW89_PHY_C2H_FUNC_RA_MAX)
2309 			handler = rtw89_phy_c2h_ra_handler[func];
2310 		break;
2311 	case RTW89_PHY_C2H_CLASS_DM:
2312 		if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY)
2313 			return;
2314 		fallthrough;
2315 	default:
2316 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
2317 		return;
2318 	}
2319 	if (!handler) {
2320 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
2321 			   func);
2322 		return;
2323 	}
2324 	handler(rtwdev, skb, len);
2325 }
2326 
2327 static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo)
2328 {
2329 	u32 reg_mask;
2330 
2331 	if (sc_xo)
2332 		reg_mask = B_AX_XTAL_SC_XO_MASK;
2333 	else
2334 		reg_mask = B_AX_XTAL_SC_XI_MASK;
2335 
2336 	return (u8)rtw89_read32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask);
2337 }
2338 
2339 static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo,
2340 				       u8 val)
2341 {
2342 	u32 reg_mask;
2343 
2344 	if (sc_xo)
2345 		reg_mask = B_AX_XTAL_SC_XO_MASK;
2346 	else
2347 		reg_mask = B_AX_XTAL_SC_XI_MASK;
2348 
2349 	rtw89_write32_mask(rtwdev, R_AX_XTAL_ON_CTRL0, reg_mask, val);
2350 }
2351 
2352 static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev,
2353 					  u8 crystal_cap, bool force)
2354 {
2355 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2356 	const struct rtw89_chip_info *chip = rtwdev->chip;
2357 	u8 sc_xi_val, sc_xo_val;
2358 
2359 	if (!force && cfo->crystal_cap == crystal_cap)
2360 		return;
2361 	crystal_cap = clamp_t(u8, crystal_cap, 0, 127);
2362 	if (chip->chip_id == RTL8852A) {
2363 		rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap);
2364 		rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap);
2365 		sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true);
2366 		sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false);
2367 	} else {
2368 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO,
2369 					crystal_cap, XTAL_SC_XO_MASK);
2370 		rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI,
2371 					crystal_cap, XTAL_SC_XI_MASK);
2372 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val);
2373 		rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val);
2374 	}
2375 	cfo->crystal_cap = sc_xi_val;
2376 	cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap);
2377 
2378 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val);
2379 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val);
2380 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n",
2381 		    cfo->x_cap_ofst);
2382 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n");
2383 }
2384 
2385 static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev)
2386 {
2387 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2388 	u8 cap;
2389 
2390 	cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK;
2391 	cfo->is_adjust = false;
2392 	if (cfo->crystal_cap == cfo->def_x_cap)
2393 		return;
2394 	cap = cfo->crystal_cap;
2395 	cap += (cap > cfo->def_x_cap ? -1 : 1);
2396 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false);
2397 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2398 		    "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap,
2399 		    cfo->def_x_cap);
2400 }
2401 
2402 static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo)
2403 {
2404 	const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp;
2405 	bool is_linked = rtwdev->total_sta_assoc > 0;
2406 	s32 cfo_avg_312;
2407 	s32 dcfo_comp_val;
2408 	u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft;
2409 	int sign;
2410 
2411 	if (!is_linked) {
2412 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n",
2413 			    is_linked);
2414 		return;
2415 	}
2416 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo);
2417 	if (curr_cfo == 0)
2418 		return;
2419 	dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO);
2420 	sign = curr_cfo > 0 ? 1 : -1;
2421 	cfo_avg_312 = (curr_cfo << dcfo_comp_sft) / 5 + sign * dcfo_comp_val;
2422 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: avg_cfo=%d\n", cfo_avg_312);
2423 	if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV)
2424 		cfo_avg_312 = -cfo_avg_312;
2425 	rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask,
2426 			       cfo_avg_312);
2427 }
2428 
2429 static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev)
2430 {
2431 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_OPT, B_DCFO_OPT_EN, 1);
2432 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO_WEIGHT, B_DCFO_WEIGHT_MSK, 8);
2433 	rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, B_AX_PWR_UL_CFO_MASK);
2434 }
2435 
2436 static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev)
2437 {
2438 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2439 	struct rtw89_efuse *efuse = &rtwdev->efuse;
2440 
2441 	cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK;
2442 	cfo->crystal_cap = cfo->crystal_cap_default;
2443 	cfo->def_x_cap = cfo->crystal_cap;
2444 	cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f);
2445 	cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1);
2446 	cfo->is_adjust = false;
2447 	cfo->divergence_lock_en = false;
2448 	cfo->x_cap_ofst = 0;
2449 	cfo->lock_cnt = 0;
2450 	cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE;
2451 	cfo->apply_compensation = false;
2452 	cfo->residual_cfo_acc = 0;
2453 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n",
2454 		    cfo->crystal_cap_default);
2455 	rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true);
2456 	rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1);
2457 	rtw89_dcfo_comp_init(rtwdev);
2458 	cfo->cfo_timer_ms = 2000;
2459 	cfo->cfo_trig_by_timer_en = false;
2460 	cfo->phy_cfo_trk_cnt = 0;
2461 	cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2462 	cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE;
2463 }
2464 
2465 static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev,
2466 					     s32 curr_cfo)
2467 {
2468 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2469 	s8 crystal_cap = cfo->crystal_cap;
2470 	s32 cfo_abs = abs(curr_cfo);
2471 	int sign;
2472 
2473 	if (!cfo->is_adjust) {
2474 		if (cfo_abs > CFO_TRK_ENABLE_TH)
2475 			cfo->is_adjust = true;
2476 	} else {
2477 		if (cfo_abs < CFO_TRK_STOP_TH)
2478 			cfo->is_adjust = false;
2479 	}
2480 	if (!cfo->is_adjust) {
2481 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n");
2482 		return;
2483 	}
2484 	sign = curr_cfo > 0 ? 1 : -1;
2485 	if (cfo_abs > CFO_TRK_STOP_TH_4)
2486 		crystal_cap += 7 * sign;
2487 	else if (cfo_abs > CFO_TRK_STOP_TH_3)
2488 		crystal_cap += 5 * sign;
2489 	else if (cfo_abs > CFO_TRK_STOP_TH_2)
2490 		crystal_cap += 3 * sign;
2491 	else if (cfo_abs > CFO_TRK_STOP_TH_1)
2492 		crystal_cap += 1 * sign;
2493 	else
2494 		return;
2495 	rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false);
2496 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2497 		    "X_cap{Curr,Default}={0x%x,0x%x}\n",
2498 		    cfo->crystal_cap, cfo->def_x_cap);
2499 }
2500 
2501 static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev)
2502 {
2503 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2504 	s32 cfo_khz_all = 0;
2505 	s32 cfo_cnt_all = 0;
2506 	s32 cfo_all_avg = 0;
2507 	u8 i;
2508 
2509 	if (rtwdev->total_sta_assoc != 1)
2510 		return 0;
2511 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n");
2512 	for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2513 		if (cfo->cfo_cnt[i] == 0)
2514 			continue;
2515 		cfo_khz_all += cfo->cfo_tail[i];
2516 		cfo_cnt_all += cfo->cfo_cnt[i];
2517 		cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all);
2518 		cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2519 	}
2520 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2521 		    "CFO track for macid = %d\n", i);
2522 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2523 		    "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n",
2524 		    cfo_khz_all, cfo_cnt_all, cfo_all_avg);
2525 	return cfo_all_avg;
2526 }
2527 
2528 static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev)
2529 {
2530 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2531 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2532 	s32 target_cfo = 0;
2533 	s32 cfo_khz_all = 0;
2534 	s32 cfo_khz_all_tp_wgt = 0;
2535 	s32 cfo_avg = 0;
2536 	s32 max_cfo_lb = BIT(31);
2537 	s32 min_cfo_ub = GENMASK(30, 0);
2538 	u16 cfo_cnt_all = 0;
2539 	u8 active_entry_cnt = 0;
2540 	u8 sta_cnt = 0;
2541 	u32 tp_all = 0;
2542 	u8 i;
2543 	u8 cfo_tol = 0;
2544 
2545 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n");
2546 	if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) {
2547 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n");
2548 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2549 			if (cfo->cfo_cnt[i] == 0)
2550 				continue;
2551 			cfo_khz_all += cfo->cfo_tail[i];
2552 			cfo_cnt_all += cfo->cfo_cnt[i];
2553 			cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all);
2554 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2555 				    "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n",
2556 				    cfo_khz_all, cfo_cnt_all, cfo_avg);
2557 			target_cfo = cfo_avg;
2558 		}
2559 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) {
2560 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n");
2561 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2562 			if (cfo->cfo_cnt[i] == 0)
2563 				continue;
2564 			cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2565 						  (s32)cfo->cfo_cnt[i]);
2566 			cfo_khz_all += cfo->cfo_avg[i];
2567 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2568 				    "Macid=%d, cfo_avg=%d\n", i,
2569 				    cfo->cfo_avg[i]);
2570 		}
2571 		sta_cnt = rtwdev->total_sta_assoc;
2572 		cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt);
2573 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2574 			    "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n",
2575 			    cfo_khz_all, sta_cnt, cfo_avg);
2576 		target_cfo = cfo_avg;
2577 	} else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) {
2578 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n");
2579 		cfo_tol = cfo->sta_cfo_tolerance;
2580 		for (i = 0; i < CFO_TRACK_MAX_USER; i++) {
2581 			sta_cnt++;
2582 			if (cfo->cfo_cnt[i] != 0) {
2583 				cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i],
2584 							  (s32)cfo->cfo_cnt[i]);
2585 				active_entry_cnt++;
2586 			} else {
2587 				cfo->cfo_avg[i] = cfo->pre_cfo_avg[i];
2588 			}
2589 			max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb);
2590 			min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub);
2591 			cfo_khz_all += cfo->cfo_avg[i];
2592 			/* need tp for each entry */
2593 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2594 				    "[%d] cfo_avg=%d, tp=tbd\n",
2595 				    i, cfo->cfo_avg[i]);
2596 			if (sta_cnt >= rtwdev->total_sta_assoc)
2597 				break;
2598 		}
2599 		tp_all = stats->rx_throughput; /* need tp for each entry */
2600 		cfo_avg =  phy_div(cfo_khz_all_tp_wgt, (s32)tp_all);
2601 
2602 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n",
2603 			    sta_cnt);
2604 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n",
2605 			    active_entry_cnt);
2606 		rtw89_debug(rtwdev, RTW89_DBG_CFO,
2607 			    "Msta cfo with tp_wgt=%d, avg_cfo=%d\n",
2608 			    cfo_khz_all_tp_wgt, cfo_avg);
2609 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n",
2610 			    max_cfo_lb, min_cfo_ub);
2611 		if (max_cfo_lb <= min_cfo_ub) {
2612 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2613 				    "cfo win_size=%d\n",
2614 				    min_cfo_ub - max_cfo_lb);
2615 			target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub);
2616 		} else {
2617 			rtw89_debug(rtwdev, RTW89_DBG_CFO,
2618 				    "No intersection of cfo tolerance windows\n");
2619 			target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt);
2620 		}
2621 		for (i = 0; i < CFO_TRACK_MAX_USER; i++)
2622 			cfo->pre_cfo_avg[i] = cfo->cfo_avg[i];
2623 	}
2624 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo);
2625 	return target_cfo;
2626 }
2627 
2628 static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev)
2629 {
2630 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2631 
2632 	memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail));
2633 	memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt));
2634 	cfo->packet_count = 0;
2635 	cfo->packet_count_pre = 0;
2636 	cfo->cfo_avg_pre = 0;
2637 }
2638 
2639 static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev)
2640 {
2641 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2642 	s32 new_cfo = 0;
2643 	bool x_cap_update = false;
2644 	u8 pre_x_cap = cfo->crystal_cap;
2645 
2646 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n",
2647 		    rtwdev->total_sta_assoc);
2648 	if (rtwdev->total_sta_assoc == 0) {
2649 		rtw89_phy_cfo_reset(rtwdev);
2650 		return;
2651 	}
2652 	if (cfo->packet_count == 0) {
2653 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n");
2654 		return;
2655 	}
2656 	if (cfo->packet_count == cfo->packet_count_pre) {
2657 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n");
2658 		return;
2659 	}
2660 	if (rtwdev->total_sta_assoc == 1)
2661 		new_cfo = rtw89_phy_average_cfo_calc(rtwdev);
2662 	else
2663 		new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev);
2664 	if (new_cfo == 0) {
2665 		rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n");
2666 		return;
2667 	}
2668 	if (cfo->divergence_lock_en) {
2669 		cfo->lock_cnt++;
2670 		if (cfo->lock_cnt > CFO_PERIOD_CNT) {
2671 			cfo->divergence_lock_en = false;
2672 			cfo->lock_cnt = 0;
2673 		} else {
2674 			rtw89_phy_cfo_reset(rtwdev);
2675 		}
2676 		return;
2677 	}
2678 	if (cfo->crystal_cap >= cfo->x_cap_ub ||
2679 	    cfo->crystal_cap <= cfo->x_cap_lb) {
2680 		cfo->divergence_lock_en = true;
2681 		rtw89_phy_cfo_reset(rtwdev);
2682 		return;
2683 	}
2684 
2685 	rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo);
2686 	cfo->cfo_avg_pre = new_cfo;
2687 	x_cap_update =  cfo->crystal_cap != pre_x_cap;
2688 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update);
2689 	rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n",
2690 		    cfo->def_x_cap, pre_x_cap, cfo->crystal_cap,
2691 		    cfo->x_cap_ofst);
2692 	if (x_cap_update) {
2693 		if (new_cfo > 0)
2694 			new_cfo -= CFO_SW_COMP_FINE_TUNE;
2695 		else
2696 			new_cfo += CFO_SW_COMP_FINE_TUNE;
2697 	}
2698 	rtw89_dcfo_comp(rtwdev, new_cfo);
2699 	rtw89_phy_cfo_statistics_reset(rtwdev);
2700 }
2701 
2702 void rtw89_phy_cfo_track_work(struct work_struct *work)
2703 {
2704 	struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev,
2705 						cfo_track_work.work);
2706 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2707 
2708 	mutex_lock(&rtwdev->mutex);
2709 	if (!cfo->cfo_trig_by_timer_en)
2710 		goto out;
2711 	rtw89_leave_ps_mode(rtwdev);
2712 	rtw89_phy_cfo_dm(rtwdev);
2713 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2714 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2715 out:
2716 	mutex_unlock(&rtwdev->mutex);
2717 }
2718 
2719 static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev)
2720 {
2721 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2722 
2723 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work,
2724 				     msecs_to_jiffies(cfo->cfo_timer_ms));
2725 }
2726 
2727 void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev)
2728 {
2729 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2730 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2731 	bool is_ul_ofdma = false, ofdma_acc_en = false;
2732 
2733 	if (stats->rx_tf_periodic > CFO_TF_CNT_TH)
2734 		is_ul_ofdma = true;
2735 	if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE &&
2736 	    is_ul_ofdma)
2737 		ofdma_acc_en = true;
2738 
2739 	switch (cfo->phy_cfo_status) {
2740 	case RTW89_PHY_DCFO_STATE_NORMAL:
2741 		if (stats->tx_throughput >= CFO_TP_UPPER) {
2742 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE;
2743 			cfo->cfo_trig_by_timer_en = true;
2744 			cfo->cfo_timer_ms = CFO_COMP_PERIOD;
2745 			rtw89_phy_cfo_start_work(rtwdev);
2746 		}
2747 		break;
2748 	case RTW89_PHY_DCFO_STATE_ENHANCE:
2749 		if (stats->tx_throughput <= CFO_TP_LOWER)
2750 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2751 		else if (ofdma_acc_en &&
2752 			 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT)
2753 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD;
2754 		else
2755 			cfo->phy_cfo_trk_cnt++;
2756 
2757 		if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) {
2758 			cfo->phy_cfo_trk_cnt = 0;
2759 			cfo->cfo_trig_by_timer_en = false;
2760 		}
2761 		break;
2762 	case RTW89_PHY_DCFO_STATE_HOLD:
2763 		if (stats->tx_throughput <= CFO_TP_LOWER) {
2764 			cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2765 			cfo->phy_cfo_trk_cnt = 0;
2766 			cfo->cfo_trig_by_timer_en = false;
2767 		} else {
2768 			cfo->phy_cfo_trk_cnt++;
2769 		}
2770 		break;
2771 	default:
2772 		cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL;
2773 		cfo->phy_cfo_trk_cnt = 0;
2774 		break;
2775 	}
2776 	rtw89_debug(rtwdev, RTW89_DBG_CFO,
2777 		    "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n",
2778 		    stats->tx_throughput, cfo->phy_cfo_status,
2779 		    cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt,
2780 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[0]));
2781 	if (cfo->cfo_trig_by_timer_en)
2782 		return;
2783 	rtw89_phy_cfo_dm(rtwdev);
2784 }
2785 
2786 void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val,
2787 			 struct rtw89_rx_phy_ppdu *phy_ppdu)
2788 {
2789 	struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking;
2790 	u8 macid = phy_ppdu->mac_id;
2791 
2792 	if (macid >= CFO_TRACK_MAX_USER) {
2793 		rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid);
2794 		return;
2795 	}
2796 
2797 	cfo->cfo_tail[macid] += cfo_val;
2798 	cfo->cfo_cnt[macid]++;
2799 	cfo->packet_count++;
2800 }
2801 
2802 void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
2803 {
2804 	const struct rtw89_chip_info *chip = rtwdev->chip;
2805 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2806 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
2807 
2808 	if (!chip->support_ul_tb_ctrl)
2809 		return;
2810 
2811 	rtwvif->def_tri_idx =
2812 		rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG);
2813 
2814 	if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV)
2815 		rtwvif->dyn_tb_bedge_en = false;
2816 	else if (chan->band_type >= RTW89_BAND_5G &&
2817 		 chan->band_width >= RTW89_CHANNEL_WIDTH_40)
2818 		rtwvif->dyn_tb_bedge_en = true;
2819 	else
2820 		rtwvif->dyn_tb_bedge_en = false;
2821 
2822 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2823 		    "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n",
2824 		    ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx);
2825 	rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2826 		    "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n",
2827 		    rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en);
2828 }
2829 
2830 struct rtw89_phy_ul_tb_check_data {
2831 	bool valid;
2832 	bool high_tf_client;
2833 	bool low_tf_client;
2834 	bool dyn_tb_bedge_en;
2835 	u8 def_tri_idx;
2836 };
2837 
2838 static
2839 void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev,
2840 				struct rtw89_vif *rtwvif,
2841 				struct rtw89_phy_ul_tb_check_data *ul_tb_data)
2842 {
2843 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
2844 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
2845 
2846 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION)
2847 		return;
2848 
2849 	if (!vif->cfg.assoc)
2850 		return;
2851 
2852 	if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH)
2853 		ul_tb_data->high_tf_client = true;
2854 	else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH)
2855 		ul_tb_data->low_tf_client = true;
2856 
2857 	ul_tb_data->valid = true;
2858 	ul_tb_data->def_tri_idx = rtwvif->def_tri_idx;
2859 	ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en;
2860 }
2861 
2862 void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev)
2863 {
2864 	const struct rtw89_chip_info *chip = rtwdev->chip;
2865 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
2866 	struct rtw89_phy_ul_tb_check_data ul_tb_data = {};
2867 	struct rtw89_vif *rtwvif;
2868 
2869 	if (!chip->support_ul_tb_ctrl)
2870 		return;
2871 
2872 	if (rtwdev->total_sta_assoc != 1)
2873 		return;
2874 
2875 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
2876 		rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data);
2877 
2878 	if (!ul_tb_data.valid)
2879 		return;
2880 
2881 	if (ul_tb_data.dyn_tb_bedge_en) {
2882 		if (ul_tb_data.high_tf_client) {
2883 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0);
2884 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2885 				    "[ULTB] Turn off if_bandedge\n");
2886 		} else if (ul_tb_data.low_tf_client) {
2887 			rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN,
2888 					       ul_tb_info->def_if_bandedge);
2889 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2890 				    "[ULTB] Set to default if_bandedge = %d\n",
2891 				    ul_tb_info->def_if_bandedge);
2892 		}
2893 	}
2894 
2895 	if (ul_tb_info->dyn_tb_tri_en) {
2896 		if (ul_tb_data.high_tf_client) {
2897 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
2898 					       B_TXSHAPE_TRIANGULAR_CFG, 0);
2899 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2900 				    "[ULTB] Turn off Tx triangle\n");
2901 		} else if (ul_tb_data.low_tf_client) {
2902 			rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT,
2903 					       B_TXSHAPE_TRIANGULAR_CFG,
2904 					       ul_tb_data.def_tri_idx);
2905 			rtw89_debug(rtwdev, RTW89_DBG_UL_TB,
2906 				    "[ULTB] Set to default tx_shap_idx = %d\n",
2907 				    ul_tb_data.def_tri_idx);
2908 		}
2909 	}
2910 }
2911 
2912 static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev)
2913 {
2914 	const struct rtw89_chip_info *chip = rtwdev->chip;
2915 	struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info;
2916 
2917 	if (!chip->support_ul_tb_ctrl)
2918 		return;
2919 
2920 	ul_tb_info->dyn_tb_tri_en = true;
2921 	ul_tb_info->def_if_bandedge =
2922 		rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN);
2923 }
2924 
2925 static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev)
2926 {
2927 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
2928 	int i;
2929 	u8 th;
2930 
2931 	for (i = 0; i < rtwdev->chip->rf_path_num; i++) {
2932 		th = rtw89_chip_get_thermal(rtwdev, i);
2933 		if (th)
2934 			ewma_thermal_add(&phystat->avg_thermal[i], th);
2935 
2936 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2937 			    "path(%d) thermal cur=%u avg=%ld", i, th,
2938 			    ewma_thermal_read(&phystat->avg_thermal[i]));
2939 	}
2940 }
2941 
2942 struct rtw89_phy_iter_rssi_data {
2943 	struct rtw89_dev *rtwdev;
2944 	struct rtw89_phy_ch_info *ch_info;
2945 	bool rssi_changed;
2946 };
2947 
2948 static void rtw89_phy_stat_rssi_update_iter(void *data,
2949 					    struct ieee80211_sta *sta)
2950 {
2951 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
2952 	struct rtw89_phy_iter_rssi_data *rssi_data =
2953 					(struct rtw89_phy_iter_rssi_data *)data;
2954 	struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info;
2955 	unsigned long rssi_curr;
2956 
2957 	rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi);
2958 
2959 	if (rssi_curr < ch_info->rssi_min) {
2960 		ch_info->rssi_min = rssi_curr;
2961 		ch_info->rssi_min_macid = rtwsta->mac_id;
2962 	}
2963 
2964 	if (rtwsta->prev_rssi == 0) {
2965 		rtwsta->prev_rssi = rssi_curr;
2966 	} else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) {
2967 		rtwsta->prev_rssi = rssi_curr;
2968 		rssi_data->rssi_changed = true;
2969 	}
2970 }
2971 
2972 static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev)
2973 {
2974 	struct rtw89_phy_iter_rssi_data rssi_data = {0};
2975 
2976 	rssi_data.rtwdev = rtwdev;
2977 	rssi_data.ch_info = &rtwdev->ch_info;
2978 	rssi_data.ch_info->rssi_min = U8_MAX;
2979 	ieee80211_iterate_stations_atomic(rtwdev->hw,
2980 					  rtw89_phy_stat_rssi_update_iter,
2981 					  &rssi_data);
2982 	if (rssi_data.rssi_changed)
2983 		rtw89_btc_ntfy_wl_sta(rtwdev);
2984 }
2985 
2986 static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev)
2987 {
2988 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
2989 	int i;
2990 
2991 	for (i = 0; i < rtwdev->chip->rf_path_num; i++)
2992 		ewma_thermal_init(&phystat->avg_thermal[i]);
2993 
2994 	rtw89_phy_stat_thermal_update(rtwdev);
2995 
2996 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
2997 	memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat));
2998 }
2999 
3000 void rtw89_phy_stat_track(struct rtw89_dev *rtwdev)
3001 {
3002 	struct rtw89_phy_stat *phystat = &rtwdev->phystat;
3003 
3004 	rtw89_phy_stat_thermal_update(rtwdev);
3005 	rtw89_phy_stat_rssi_update(rtwdev);
3006 
3007 	phystat->last_pkt_stat = phystat->cur_pkt_stat;
3008 	memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat));
3009 }
3010 
3011 static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us)
3012 {
3013 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3014 
3015 	return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3016 }
3017 
3018 static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx)
3019 {
3020 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3021 
3022 	return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx);
3023 }
3024 
3025 static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev)
3026 {
3027 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3028 
3029 	env->ccx_manual_ctrl = false;
3030 	env->ccx_ongoing = false;
3031 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3032 	env->ccx_rpt_stamp = 0;
3033 	env->ccx_period = 0;
3034 	env->ccx_unit_idx = RTW89_CCX_32_US;
3035 	env->ccx_trigger_time = 0;
3036 	env->ccx_edcca_opt_bw_idx = RTW89_CCX_EDCCA_BW20_0;
3037 
3038 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EN_MSK, 1);
3039 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_TRIG_OPT_MSK, 1);
3040 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3041 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_CCX_EDCCA_OPT_MSK,
3042 			       RTW89_CCX_EDCCA_BW20_0);
3043 }
3044 
3045 static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report,
3046 				    u16 score)
3047 {
3048 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3049 	u32 numer = 0;
3050 	u16 ret = 0;
3051 
3052 	numer = report * score + (env->ccx_period >> 1);
3053 	if (env->ccx_period)
3054 		ret = numer / env->ccx_period;
3055 
3056 	return ret >= score ? score - 1 : ret;
3057 }
3058 
3059 static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev,
3060 					    u16 time_ms, u32 *period,
3061 					    u32 *unit_idx)
3062 {
3063 	u32 idx;
3064 	u8 quotient;
3065 
3066 	if (time_ms >= CCX_MAX_PERIOD)
3067 		time_ms = CCX_MAX_PERIOD;
3068 
3069 	quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD;
3070 
3071 	if (quotient < 4)
3072 		idx = RTW89_CCX_4_US;
3073 	else if (quotient < 8)
3074 		idx = RTW89_CCX_8_US;
3075 	else if (quotient < 16)
3076 		idx = RTW89_CCX_16_US;
3077 	else
3078 		idx = RTW89_CCX_32_US;
3079 
3080 	*unit_idx = idx;
3081 	*period = (time_ms * MS_TO_4US_RATIO) >> idx;
3082 
3083 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3084 		    "[Trigger Time] period:%d, unit_idx:%d\n",
3085 		    *period, *unit_idx);
3086 }
3087 
3088 static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev)
3089 {
3090 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3091 
3092 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3093 		    "lv:(%d)->(0)\n", env->ccx_rac_lv);
3094 
3095 	env->ccx_ongoing = false;
3096 	env->ccx_rac_lv = RTW89_RAC_RELEASE;
3097 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3098 }
3099 
3100 static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev,
3101 					      struct rtw89_ccx_para_info *para)
3102 {
3103 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3104 	bool is_update = env->ifs_clm_app != para->ifs_clm_app;
3105 	u8 i = 0;
3106 	u16 *ifs_th_l = env->ifs_clm_th_l;
3107 	u16 *ifs_th_h = env->ifs_clm_th_h;
3108 	u32 ifs_th0_us = 0, ifs_th_times = 0;
3109 	u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0};
3110 
3111 	if (!is_update)
3112 		goto ifs_update_finished;
3113 
3114 	switch (para->ifs_clm_app) {
3115 	case RTW89_IFS_CLM_INIT:
3116 	case RTW89_IFS_CLM_BACKGROUND:
3117 	case RTW89_IFS_CLM_ACS:
3118 	case RTW89_IFS_CLM_DBG:
3119 	case RTW89_IFS_CLM_DIG:
3120 	case RTW89_IFS_CLM_TDMA_DIG:
3121 		ifs_th0_us = IFS_CLM_TH0_UPPER;
3122 		ifs_th_times = IFS_CLM_TH_MUL;
3123 		break;
3124 	case RTW89_IFS_CLM_DBG_MANUAL:
3125 		ifs_th0_us = para->ifs_clm_manual_th0;
3126 		ifs_th_times = para->ifs_clm_manual_th_times;
3127 		break;
3128 	default:
3129 		break;
3130 	}
3131 
3132 	/* Set sampling threshold for 4 different regions, unit in idx_cnt.
3133 	 * low[i] = high[i-1] + 1
3134 	 * high[i] = high[i-1] * ifs_th_times
3135 	 */
3136 	ifs_th_l[IFS_CLM_TH_START_IDX] = 0;
3137 	ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us;
3138 	ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev,
3139 								 ifs_th0_us);
3140 	for (i = 1; i < RTW89_IFS_CLM_NUM; i++) {
3141 		ifs_th_l[i] = ifs_th_h[i - 1] + 1;
3142 		ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times;
3143 		ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]);
3144 	}
3145 
3146 ifs_update_finished:
3147 	if (!is_update)
3148 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3149 			    "No need to update IFS_TH\n");
3150 
3151 	return is_update;
3152 }
3153 
3154 static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev)
3155 {
3156 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3157 	u8 i = 0;
3158 
3159 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_LOW_MSK,
3160 			       env->ifs_clm_th_l[0]);
3161 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_LOW_MSK,
3162 			       env->ifs_clm_th_l[1]);
3163 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_LOW_MSK,
3164 			       env->ifs_clm_th_l[2]);
3165 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_LOW_MSK,
3166 			       env->ifs_clm_th_l[3]);
3167 
3168 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_TH_HIGH_MSK,
3169 			       env->ifs_clm_th_h[0]);
3170 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_TH_HIGH_MSK,
3171 			       env->ifs_clm_th_h[1]);
3172 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_TH_HIGH_MSK,
3173 			       env->ifs_clm_th_h[2]);
3174 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_TH_HIGH_MSK,
3175 			       env->ifs_clm_th_h[3]);
3176 
3177 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3178 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3179 			    "Update IFS_T%d_th{low, high} : {%d, %d}\n",
3180 			    i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]);
3181 }
3182 
3183 static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev)
3184 {
3185 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3186 	struct rtw89_ccx_para_info para = {0};
3187 
3188 	env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3189 	env->ifs_clm_mntr_time = 0;
3190 
3191 	para.ifs_clm_app = RTW89_IFS_CLM_INIT;
3192 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, &para))
3193 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3194 
3195 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COLLECT_EN,
3196 			       true);
3197 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T1, B_IFS_T1_EN_MSK, true);
3198 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T2, B_IFS_T2_EN_MSK, true);
3199 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T3, B_IFS_T3_EN_MSK, true);
3200 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_T4, B_IFS_T4_EN_MSK, true);
3201 }
3202 
3203 static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev,
3204 				     enum rtw89_env_racing_lv level)
3205 {
3206 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3207 	int ret = 0;
3208 
3209 	if (level >= RTW89_RAC_MAX_NUM) {
3210 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3211 			    "[WARNING] Wrong LV=%d\n", level);
3212 		return -EINVAL;
3213 	}
3214 
3215 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3216 		    "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing,
3217 		    env->ccx_rac_lv, level);
3218 
3219 	if (env->ccx_ongoing) {
3220 		if (level <= env->ccx_rac_lv)
3221 			ret = -EINVAL;
3222 		else
3223 			env->ccx_ongoing = false;
3224 	}
3225 
3226 	if (ret == 0)
3227 		env->ccx_rac_lv = level;
3228 
3229 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n",
3230 		    !ret);
3231 
3232 	return ret;
3233 }
3234 
3235 static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev)
3236 {
3237 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3238 
3239 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 0);
3240 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 0);
3241 	rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER, B_IFS_COUNTER_CLR_MSK, 1);
3242 	rtw89_phy_set_phy_regs(rtwdev, R_CCX, B_MEASUREMENT_TRIG_MSK, 1);
3243 
3244 	env->ccx_rpt_stamp++;
3245 	env->ccx_ongoing = true;
3246 }
3247 
3248 static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev)
3249 {
3250 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3251 	u8 i = 0;
3252 	u32 res = 0;
3253 
3254 	env->ifs_clm_tx_ratio =
3255 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT);
3256 	env->ifs_clm_edcca_excl_cca_ratio =
3257 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca,
3258 					 PERCENT);
3259 	env->ifs_clm_cck_fa_ratio =
3260 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT);
3261 	env->ifs_clm_ofdm_fa_ratio =
3262 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT);
3263 	env->ifs_clm_cck_cca_excl_fa_ratio =
3264 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa,
3265 					 PERCENT);
3266 	env->ifs_clm_ofdm_cca_excl_fa_ratio =
3267 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa,
3268 					 PERCENT);
3269 	env->ifs_clm_cck_fa_permil =
3270 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL);
3271 	env->ifs_clm_ofdm_fa_permil =
3272 		rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL);
3273 
3274 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++) {
3275 		if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) {
3276 			env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD;
3277 		} else {
3278 			env->ifs_clm_ifs_avg[i] =
3279 				rtw89_phy_ccx_idx_to_us(rtwdev,
3280 							env->ifs_clm_avg[i]);
3281 		}
3282 
3283 		res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]);
3284 		res += env->ifs_clm_his[i] >> 1;
3285 		if (env->ifs_clm_his[i])
3286 			res /= env->ifs_clm_his[i];
3287 		else
3288 			res = 0;
3289 		env->ifs_clm_cca_avg[i] = res;
3290 	}
3291 
3292 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3293 		    "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3294 		    env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio);
3295 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3296 		    "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n",
3297 		    env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio);
3298 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3299 		    "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n",
3300 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil);
3301 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3302 		    "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n",
3303 		    env->ifs_clm_cck_cca_excl_fa_ratio,
3304 		    env->ifs_clm_ofdm_cca_excl_fa_ratio);
3305 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3306 		    "Time:[his, ifs_avg(us), cca_avg(us)]\n");
3307 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3308 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n",
3309 			    i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i],
3310 			    env->ifs_clm_cca_avg[i]);
3311 }
3312 
3313 static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev)
3314 {
3315 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3316 	u8 i = 0;
3317 
3318 	if (rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_DONE_MSK) == 0) {
3319 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3320 			    "Get IFS_CLM report Fail\n");
3321 		return false;
3322 	}
3323 
3324 	env->ifs_clm_tx =
3325 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3326 				      B_IFS_CLM_TX_CNT_MSK);
3327 	env->ifs_clm_edcca_excl_cca =
3328 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_TX_CNT,
3329 				      B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK);
3330 	env->ifs_clm_cckcca_excl_fa =
3331 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3332 				      B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK);
3333 	env->ifs_clm_ofdmcca_excl_fa =
3334 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_CCA,
3335 				      B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK);
3336 	env->ifs_clm_cckfa =
3337 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3338 				      B_IFS_CLM_CCK_FA_MSK);
3339 	env->ifs_clm_ofdmfa =
3340 		rtw89_phy_read32_mask(rtwdev, R_IFS_CLM_FA,
3341 				      B_IFS_CLM_OFDM_FA_MSK);
3342 
3343 	env->ifs_clm_his[0] =
3344 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T1_HIS_MSK);
3345 	env->ifs_clm_his[1] =
3346 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T2_HIS_MSK);
3347 	env->ifs_clm_his[2] =
3348 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T3_HIS_MSK);
3349 	env->ifs_clm_his[3] =
3350 		rtw89_phy_read32_mask(rtwdev, R_IFS_HIS, B_IFS_T4_HIS_MSK);
3351 
3352 	env->ifs_clm_avg[0] =
3353 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T1_AVG_MSK);
3354 	env->ifs_clm_avg[1] =
3355 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_L, B_IFS_T2_AVG_MSK);
3356 	env->ifs_clm_avg[2] =
3357 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T3_AVG_MSK);
3358 	env->ifs_clm_avg[3] =
3359 		rtw89_phy_read32_mask(rtwdev, R_IFS_AVG_H, B_IFS_T4_AVG_MSK);
3360 
3361 	env->ifs_clm_cca[0] =
3362 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T1_CCA_MSK);
3363 	env->ifs_clm_cca[1] =
3364 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_L, B_IFS_T2_CCA_MSK);
3365 	env->ifs_clm_cca[2] =
3366 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T3_CCA_MSK);
3367 	env->ifs_clm_cca[3] =
3368 		rtw89_phy_read32_mask(rtwdev, R_IFS_CCA_H, B_IFS_T4_CCA_MSK);
3369 
3370 	env->ifs_clm_total_ifs =
3371 		rtw89_phy_read32_mask(rtwdev, R_IFSCNT, B_IFSCNT_TOTAL_CNT_MSK);
3372 
3373 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n",
3374 		    env->ifs_clm_total_ifs);
3375 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3376 		    "{Tx, EDCCA_exclu_cca} = {%d, %d}\n",
3377 		    env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca);
3378 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3379 		    "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n",
3380 		    env->ifs_clm_cckfa, env->ifs_clm_ofdmfa);
3381 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3382 		    "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n",
3383 		    env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa);
3384 
3385 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n");
3386 	for (i = 0; i < RTW89_IFS_CLM_NUM; i++)
3387 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3388 			    "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i],
3389 			    env->ifs_clm_avg[i], env->ifs_clm_cca[i]);
3390 
3391 	rtw89_phy_ifs_clm_get_utility(rtwdev);
3392 
3393 	return true;
3394 }
3395 
3396 static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev,
3397 				 struct rtw89_ccx_para_info *para)
3398 {
3399 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3400 	u32 period = 0;
3401 	u32 unit_idx = 0;
3402 
3403 	if (para->mntr_time == 0) {
3404 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3405 			    "[WARN] MNTR_TIME is 0\n");
3406 		return -EINVAL;
3407 	}
3408 
3409 	if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv))
3410 		return -EINVAL;
3411 
3412 	if (para->mntr_time != env->ifs_clm_mntr_time) {
3413 		rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time,
3414 						&period, &unit_idx);
3415 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3416 				       B_IFS_CLM_PERIOD_MSK, period);
3417 		rtw89_phy_set_phy_regs(rtwdev, R_IFS_COUNTER,
3418 				       B_IFS_CLM_COUNTER_UNIT_MSK, unit_idx);
3419 
3420 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3421 			    "Update IFS-CLM time ((%d)) -> ((%d))\n",
3422 			    env->ifs_clm_mntr_time, para->mntr_time);
3423 
3424 		env->ifs_clm_mntr_time = para->mntr_time;
3425 		env->ccx_period = (u16)period;
3426 		env->ccx_unit_idx = (u8)unit_idx;
3427 	}
3428 
3429 	if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) {
3430 		env->ifs_clm_app = para->ifs_clm_app;
3431 		rtw89_phy_ifs_clm_set_th_reg(rtwdev);
3432 	}
3433 
3434 	return 0;
3435 }
3436 
3437 void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev)
3438 {
3439 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3440 	struct rtw89_ccx_para_info para = {0};
3441 	u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3442 
3443 	env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL;
3444 	if (env->ccx_manual_ctrl) {
3445 		rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3446 			    "CCX in manual ctrl\n");
3447 		return;
3448 	}
3449 
3450 	/* only ifs_clm for now */
3451 	if (rtw89_phy_ifs_clm_get_result(rtwdev))
3452 		env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3453 
3454 	rtw89_phy_ccx_racing_release(rtwdev);
3455 	para.mntr_time = 1900;
3456 	para.rac_lv = RTW89_RAC_LV_1;
3457 	para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND;
3458 
3459 	if (rtw89_phy_ifs_clm_set(rtwdev, &para) == 0)
3460 		chk_result |= RTW89_PHY_ENV_MON_IFS_CLM;
3461 	if (chk_result)
3462 		rtw89_phy_ccx_trigger(rtwdev);
3463 
3464 	rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK,
3465 		    "get_result=0x%x, chk_result:0x%x\n",
3466 		    env->ccx_watchdog_result, chk_result);
3467 }
3468 
3469 static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page)
3470 {
3471 	if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM ||
3472 	    *ie_page == RTW89_RSVD_9)
3473 		return false;
3474 	else if (*ie_page > RTW89_RSVD_9)
3475 		*ie_page -= 1;
3476 
3477 	return true;
3478 }
3479 
3480 static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page)
3481 {
3482 	static const u8 ie_page_shift = 2;
3483 
3484 	return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift);
3485 }
3486 
3487 static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev,
3488 				      enum rtw89_phy_status_bitmap ie_page)
3489 {
3490 	u32 addr;
3491 
3492 	if (!rtw89_physts_ie_page_valid(&ie_page))
3493 		return 0;
3494 
3495 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3496 
3497 	return rtw89_phy_read32(rtwdev, addr);
3498 }
3499 
3500 static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev,
3501 				       enum rtw89_phy_status_bitmap ie_page,
3502 				       u32 val)
3503 {
3504 	const struct rtw89_chip_info *chip = rtwdev->chip;
3505 	u32 addr;
3506 
3507 	if (!rtw89_physts_ie_page_valid(&ie_page))
3508 		return;
3509 
3510 	if (chip->chip_id == RTL8852A)
3511 		val &= B_PHY_STS_BITMAP_MSK_52A;
3512 
3513 	addr = rtw89_phy_get_ie_bitmap_addr(ie_page);
3514 	rtw89_phy_write32(rtwdev, addr, val);
3515 }
3516 
3517 static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev,
3518 					  enum rtw89_phy_status_bitmap bitmap,
3519 					  enum rtw89_phy_status_ie_type ie,
3520 					  bool enable)
3521 {
3522 	u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap);
3523 
3524 	if (enable)
3525 		val |= BIT(ie);
3526 	else
3527 		val &= ~BIT(ie);
3528 
3529 	rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val);
3530 }
3531 
3532 static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev,
3533 					    bool enable,
3534 					    enum rtw89_phy_idx phy_idx)
3535 {
3536 	if (enable) {
3537 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3538 				      B_STS_DIS_TRIG_BY_FAIL);
3539 		rtw89_phy_write32_clr(rtwdev, R_PLCP_HISTOGRAM,
3540 				      B_STS_DIS_TRIG_BY_BRK);
3541 	} else {
3542 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3543 				      B_STS_DIS_TRIG_BY_FAIL);
3544 		rtw89_phy_write32_set(rtwdev, R_PLCP_HISTOGRAM,
3545 				      B_STS_DIS_TRIG_BY_BRK);
3546 	}
3547 }
3548 
3549 static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev)
3550 {
3551 	u8 i;
3552 
3553 	rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0);
3554 
3555 	for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) {
3556 		if (i >= RTW89_CCK_PKT)
3557 			rtw89_physts_enable_ie_bitmap(rtwdev, i,
3558 						      RTW89_PHYSTS_IE09_FTR_0,
3559 						      true);
3560 		if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) ||
3561 		    (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT))
3562 			continue;
3563 		rtw89_physts_enable_ie_bitmap(rtwdev, i,
3564 					      RTW89_PHYSTS_IE24_OFDM_TD_PATH_A,
3565 					      true);
3566 	}
3567 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT,
3568 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3569 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT,
3570 				      RTW89_PHYSTS_IE13_DL_MU_DEF, true);
3571 
3572 	/* force IE01 for channel index, only channel field is valid */
3573 	rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT,
3574 				      RTW89_PHYSTS_IE01_CMN_OFDM, true);
3575 }
3576 
3577 static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type)
3578 {
3579 	const struct rtw89_chip_info *chip = rtwdev->chip;
3580 	struct rtw89_dig_info *dig = &rtwdev->dig;
3581 	const struct rtw89_phy_dig_gain_cfg *cfg;
3582 	const char *msg;
3583 	u8 i;
3584 	s8 gain_base;
3585 	s8 *gain_arr;
3586 	u32 tmp;
3587 
3588 	switch (type) {
3589 	case RTW89_DIG_GAIN_LNA_G:
3590 		gain_arr = dig->lna_gain_g;
3591 		gain_base = LNA0_GAIN;
3592 		cfg = chip->dig_table->cfg_lna_g;
3593 		msg = "lna_gain_g";
3594 		break;
3595 	case RTW89_DIG_GAIN_TIA_G:
3596 		gain_arr = dig->tia_gain_g;
3597 		gain_base = TIA0_GAIN_G;
3598 		cfg = chip->dig_table->cfg_tia_g;
3599 		msg = "tia_gain_g";
3600 		break;
3601 	case RTW89_DIG_GAIN_LNA_A:
3602 		gain_arr = dig->lna_gain_a;
3603 		gain_base = LNA0_GAIN;
3604 		cfg = chip->dig_table->cfg_lna_a;
3605 		msg = "lna_gain_a";
3606 		break;
3607 	case RTW89_DIG_GAIN_TIA_A:
3608 		gain_arr = dig->tia_gain_a;
3609 		gain_base = TIA0_GAIN_A;
3610 		cfg = chip->dig_table->cfg_tia_a;
3611 		msg = "tia_gain_a";
3612 		break;
3613 	default:
3614 		return;
3615 	}
3616 
3617 	for (i = 0; i < cfg->size; i++) {
3618 		tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr,
3619 					    cfg->table[i].mask);
3620 		tmp >>= DIG_GAIN_SHIFT;
3621 		gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base;
3622 		gain_base += DIG_GAIN;
3623 
3624 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n",
3625 			    msg, i, gain_arr[i]);
3626 	}
3627 }
3628 
3629 static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev)
3630 {
3631 	struct rtw89_dig_info *dig = &rtwdev->dig;
3632 	u32 tmp;
3633 	u8 i;
3634 
3635 	if (!rtwdev->hal.support_igi)
3636 		return;
3637 
3638 	tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW,
3639 				    B_PATH0_IB_PKPW_MSK);
3640 	dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT);
3641 	dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK,
3642 					    B_PATH0_IB_PBK_MSK);
3643 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n",
3644 		    dig->ib_pkpwr, dig->ib_pbk);
3645 
3646 	for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++)
3647 		rtw89_phy_dig_read_gain_table(rtwdev, i);
3648 }
3649 
3650 static const u8 rssi_nolink = 22;
3651 static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104};
3652 static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88};
3653 static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16};
3654 static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528};
3655 
3656 static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev)
3657 {
3658 	struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info;
3659 	struct rtw89_dig_info *dig = &rtwdev->dig;
3660 	bool is_linked = rtwdev->total_sta_assoc > 0;
3661 
3662 	if (is_linked) {
3663 		dig->igi_rssi = ch_info->rssi_min >> 1;
3664 	} else {
3665 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n");
3666 		dig->igi_rssi = rssi_nolink;
3667 	}
3668 }
3669 
3670 static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev)
3671 {
3672 	struct rtw89_dig_info *dig = &rtwdev->dig;
3673 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3674 	bool is_linked = rtwdev->total_sta_assoc > 0;
3675 	const u16 *fa_th_src = NULL;
3676 
3677 	switch (chan->band_type) {
3678 	case RTW89_BAND_2G:
3679 		dig->lna_gain = dig->lna_gain_g;
3680 		dig->tia_gain = dig->tia_gain_g;
3681 		fa_th_src = is_linked ? fa_th_2g : fa_th_nolink;
3682 		dig->force_gaincode_idx_en = false;
3683 		dig->dyn_pd_th_en = true;
3684 		break;
3685 	case RTW89_BAND_5G:
3686 	default:
3687 		dig->lna_gain = dig->lna_gain_a;
3688 		dig->tia_gain = dig->tia_gain_a;
3689 		fa_th_src = is_linked ? fa_th_5g : fa_th_nolink;
3690 		dig->force_gaincode_idx_en = true;
3691 		dig->dyn_pd_th_en = true;
3692 		break;
3693 	}
3694 	memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th));
3695 	memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th));
3696 }
3697 
3698 static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20;
3699 static const u8 igi_max_performance_mode = 0x5a;
3700 static const u8 dynamic_pd_threshold_max;
3701 
3702 static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev)
3703 {
3704 	struct rtw89_dig_info *dig = &rtwdev->dig;
3705 
3706 	dig->cur_gaincode.lna_idx = LNA_IDX_MAX;
3707 	dig->cur_gaincode.tia_idx = TIA_IDX_MAX;
3708 	dig->cur_gaincode.rxb_idx = RXB_IDX_MAX;
3709 	dig->force_gaincode.lna_idx = LNA_IDX_MAX;
3710 	dig->force_gaincode.tia_idx = TIA_IDX_MAX;
3711 	dig->force_gaincode.rxb_idx = RXB_IDX_MAX;
3712 
3713 	dig->dyn_igi_max = igi_max_performance_mode;
3714 	dig->dyn_igi_min = dynamic_igi_min;
3715 	dig->dyn_pd_th_max = dynamic_pd_threshold_max;
3716 	dig->pd_low_th_ofst = pd_low_th_offset;
3717 	dig->is_linked_pre = false;
3718 }
3719 
3720 static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev)
3721 {
3722 	rtw89_phy_dig_update_gain_para(rtwdev);
3723 	rtw89_phy_dig_reset(rtwdev);
3724 }
3725 
3726 static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3727 {
3728 	struct rtw89_dig_info *dig = &rtwdev->dig;
3729 	u8 lna_idx;
3730 
3731 	if (rssi < dig->igi_rssi_th[0])
3732 		lna_idx = RTW89_DIG_GAIN_LNA_IDX6;
3733 	else if (rssi < dig->igi_rssi_th[1])
3734 		lna_idx = RTW89_DIG_GAIN_LNA_IDX5;
3735 	else if (rssi < dig->igi_rssi_th[2])
3736 		lna_idx = RTW89_DIG_GAIN_LNA_IDX4;
3737 	else if (rssi < dig->igi_rssi_th[3])
3738 		lna_idx = RTW89_DIG_GAIN_LNA_IDX3;
3739 	else if (rssi < dig->igi_rssi_th[4])
3740 		lna_idx = RTW89_DIG_GAIN_LNA_IDX2;
3741 	else
3742 		lna_idx = RTW89_DIG_GAIN_LNA_IDX1;
3743 
3744 	return lna_idx;
3745 }
3746 
3747 static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi)
3748 {
3749 	struct rtw89_dig_info *dig = &rtwdev->dig;
3750 	u8 tia_idx;
3751 
3752 	if (rssi < dig->igi_rssi_th[0])
3753 		tia_idx = RTW89_DIG_GAIN_TIA_IDX1;
3754 	else
3755 		tia_idx = RTW89_DIG_GAIN_TIA_IDX0;
3756 
3757 	return tia_idx;
3758 }
3759 
3760 #define IB_PBK_BASE 110
3761 #define WB_RSSI_BASE 10
3762 static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3763 					struct rtw89_agc_gaincode_set *set)
3764 {
3765 	struct rtw89_dig_info *dig = &rtwdev->dig;
3766 	s8 lna_gain = dig->lna_gain[set->lna_idx];
3767 	s8 tia_gain = dig->tia_gain[set->tia_idx];
3768 	s32 wb_rssi = rssi + lna_gain + tia_gain;
3769 	s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE;
3770 	u8 rxb_idx;
3771 
3772 	rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi;
3773 	rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX);
3774 
3775 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n",
3776 		    wb_rssi, rxb_idx_tmp);
3777 
3778 	return rxb_idx;
3779 }
3780 
3781 static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi,
3782 					   struct rtw89_agc_gaincode_set *set)
3783 {
3784 	set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi);
3785 	set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi);
3786 	set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set);
3787 
3788 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3789 		    "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n",
3790 		    rssi, set->lna_idx, set->tia_idx, set->rxb_idx);
3791 }
3792 
3793 #define IGI_OFFSET_MAX 25
3794 #define IGI_OFFSET_MUL 2
3795 static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev)
3796 {
3797 	struct rtw89_dig_info *dig = &rtwdev->dig;
3798 	struct rtw89_env_monitor_info *env = &rtwdev->env_monitor;
3799 	enum rtw89_dig_noisy_level noisy_lv;
3800 	u8 igi_offset = dig->fa_rssi_ofst;
3801 	u16 fa_ratio = 0;
3802 
3803 	fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil;
3804 
3805 	if (fa_ratio < dig->fa_th[0])
3806 		noisy_lv = RTW89_DIG_NOISY_LEVEL0;
3807 	else if (fa_ratio < dig->fa_th[1])
3808 		noisy_lv = RTW89_DIG_NOISY_LEVEL1;
3809 	else if (fa_ratio < dig->fa_th[2])
3810 		noisy_lv = RTW89_DIG_NOISY_LEVEL2;
3811 	else if (fa_ratio < dig->fa_th[3])
3812 		noisy_lv = RTW89_DIG_NOISY_LEVEL3;
3813 	else
3814 		noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX;
3815 
3816 	if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2)
3817 		igi_offset = 0;
3818 	else
3819 		igi_offset += noisy_lv * IGI_OFFSET_MUL;
3820 
3821 	igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX);
3822 	dig->fa_rssi_ofst = igi_offset;
3823 
3824 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3825 		    "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n",
3826 		    dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]);
3827 
3828 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3829 		    "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n",
3830 		    env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil,
3831 		    env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil,
3832 		    noisy_lv, igi_offset);
3833 }
3834 
3835 static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx)
3836 {
3837 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
3838 
3839 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr,
3840 			       dig_regs->p0_lna_init.mask, lna_idx);
3841 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr,
3842 			       dig_regs->p1_lna_init.mask, lna_idx);
3843 }
3844 
3845 static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx)
3846 {
3847 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
3848 
3849 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr,
3850 			       dig_regs->p0_tia_init.mask, tia_idx);
3851 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr,
3852 			       dig_regs->p1_tia_init.mask, tia_idx);
3853 }
3854 
3855 static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx)
3856 {
3857 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
3858 
3859 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr,
3860 			       dig_regs->p0_rxb_init.mask, rxb_idx);
3861 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr,
3862 			       dig_regs->p1_rxb_init.mask, rxb_idx);
3863 }
3864 
3865 static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev,
3866 				     const struct rtw89_agc_gaincode_set set)
3867 {
3868 	rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx);
3869 	rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx);
3870 	rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx);
3871 
3872 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n",
3873 		    set.lna_idx, set.tia_idx, set.rxb_idx);
3874 }
3875 
3876 static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev,
3877 						   bool enable)
3878 {
3879 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
3880 
3881 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr,
3882 			       dig_regs->p0_p20_pagcugc_en.mask, enable);
3883 	rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr,
3884 			       dig_regs->p0_s20_pagcugc_en.mask, enable);
3885 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr,
3886 			       dig_regs->p1_p20_pagcugc_en.mask, enable);
3887 	rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr,
3888 			       dig_regs->p1_s20_pagcugc_en.mask, enable);
3889 
3890 	rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable);
3891 }
3892 
3893 static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev)
3894 {
3895 	struct rtw89_dig_info *dig = &rtwdev->dig;
3896 
3897 	if (!rtwdev->hal.support_igi)
3898 		return;
3899 
3900 	if (dig->force_gaincode_idx_en) {
3901 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
3902 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
3903 			    "Force gaincode index enabled.\n");
3904 	} else {
3905 		rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi,
3906 					       &dig->cur_gaincode);
3907 		rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode);
3908 	}
3909 }
3910 
3911 static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi,
3912 				    bool enable)
3913 {
3914 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3915 	const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs;
3916 	enum rtw89_bandwidth cbw = chan->band_width;
3917 	struct rtw89_dig_info *dig = &rtwdev->dig;
3918 	u8 final_rssi = 0, under_region = dig->pd_low_th_ofst;
3919 	u8 ofdm_cca_th;
3920 	s8 cck_cca_th;
3921 	u32 pd_val = 0;
3922 
3923 	under_region += PD_TH_SB_FLTR_CMP_VAL;
3924 
3925 	switch (cbw) {
3926 	case RTW89_CHANNEL_WIDTH_40:
3927 		under_region += PD_TH_BW40_CMP_VAL;
3928 		break;
3929 	case RTW89_CHANNEL_WIDTH_80:
3930 		under_region += PD_TH_BW80_CMP_VAL;
3931 		break;
3932 	case RTW89_CHANNEL_WIDTH_160:
3933 		under_region += PD_TH_BW160_CMP_VAL;
3934 		break;
3935 	case RTW89_CHANNEL_WIDTH_20:
3936 		fallthrough;
3937 	default:
3938 		under_region += PD_TH_BW20_CMP_VAL;
3939 		break;
3940 	}
3941 
3942 	dig->dyn_pd_th_max = dig->igi_rssi;
3943 
3944 	final_rssi = min_t(u8, rssi, dig->igi_rssi);
3945 	ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region,
3946 			      PD_TH_MAX_RSSI + under_region);
3947 
3948 	if (enable) {
3949 		pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1;
3950 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
3951 			    "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n",
3952 			    final_rssi, ofdm_cca_th, under_region, pd_val);
3953 	} else {
3954 		rtw89_debug(rtwdev, RTW89_DBG_DIG,
3955 			    "Dynamic PD th disabled, Set PD_low_bd=0\n");
3956 	}
3957 
3958 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
3959 			       dig_regs->pd_lower_bound_mask, pd_val);
3960 	rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg,
3961 			       dig_regs->pd_spatial_reuse_en, enable);
3962 
3963 	if (!rtwdev->hal.support_cckpd)
3964 		return;
3965 
3966 	cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI);
3967 	pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX);
3968 
3969 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
3970 		    "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n",
3971 		    final_rssi, cck_cca_th, under_region, pd_val);
3972 
3973 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_EN_V1,
3974 			       B_BMODE_PDTH_LIMIT_EN_MSK_V1, enable);
3975 	rtw89_phy_write32_mask(rtwdev, R_BMODE_PDTH_V1,
3976 			       B_BMODE_PDTH_LOWER_BOUND_MSK_V1, pd_val);
3977 }
3978 
3979 void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev)
3980 {
3981 	struct rtw89_dig_info *dig = &rtwdev->dig;
3982 
3983 	dig->bypass_dig = false;
3984 	rtw89_phy_dig_para_reset(rtwdev);
3985 	rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode);
3986 	rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false);
3987 	rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
3988 	rtw89_phy_dig_update_para(rtwdev);
3989 }
3990 
3991 #define IGI_RSSI_MIN 10
3992 void rtw89_phy_dig(struct rtw89_dev *rtwdev)
3993 {
3994 	struct rtw89_dig_info *dig = &rtwdev->dig;
3995 	bool is_linked = rtwdev->total_sta_assoc > 0;
3996 
3997 	if (unlikely(dig->bypass_dig)) {
3998 		dig->bypass_dig = false;
3999 		return;
4000 	}
4001 
4002 	if (!dig->is_linked_pre && is_linked) {
4003 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n");
4004 		rtw89_phy_dig_update_para(rtwdev);
4005 	} else if (dig->is_linked_pre && !is_linked) {
4006 		rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n");
4007 		rtw89_phy_dig_update_para(rtwdev);
4008 	}
4009 	dig->is_linked_pre = is_linked;
4010 
4011 	rtw89_phy_dig_igi_offset_by_env(rtwdev);
4012 	rtw89_phy_dig_update_rssi_info(rtwdev);
4013 
4014 	dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ?
4015 			    dig->igi_rssi - IGI_RSSI_MIN : 0;
4016 	dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX;
4017 	dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst;
4018 
4019 	dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min,
4020 				 dig->dyn_igi_max);
4021 
4022 	rtw89_debug(rtwdev, RTW89_DBG_DIG,
4023 		    "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n",
4024 		    dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min,
4025 		    dig->igi_fa_rssi);
4026 
4027 	rtw89_phy_dig_config_igi(rtwdev);
4028 
4029 	rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en);
4030 
4031 	if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max)
4032 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true);
4033 	else
4034 		rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false);
4035 }
4036 
4037 static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta)
4038 {
4039 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
4040 	struct rtw89_dev *rtwdev = rtwsta->rtwdev;
4041 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
4042 	struct rtw89_hal *hal = &rtwdev->hal;
4043 	bool *done = data;
4044 	u8 rssi_a, rssi_b;
4045 	u32 candidate;
4046 
4047 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls)
4048 		return;
4049 
4050 	if (*done)
4051 		return;
4052 
4053 	*done = true;
4054 
4055 	rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]);
4056 	rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]);
4057 
4058 	if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH)
4059 		candidate = RF_A;
4060 	else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH)
4061 		candidate = RF_B;
4062 	else
4063 		return;
4064 
4065 	if (hal->antenna_tx == candidate)
4066 		return;
4067 
4068 	hal->antenna_tx = candidate;
4069 	rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta);
4070 
4071 	if (hal->antenna_tx == RF_A) {
4072 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12);
4073 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11);
4074 	} else if (hal->antenna_tx == RF_B) {
4075 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11);
4076 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12);
4077 	}
4078 }
4079 
4080 void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev)
4081 {
4082 	struct rtw89_hal *hal = &rtwdev->hal;
4083 	bool done = false;
4084 
4085 	if (!hal->tx_path_diversity)
4086 		return;
4087 
4088 	ieee80211_iterate_stations_atomic(rtwdev->hw,
4089 					  rtw89_phy_tx_path_div_sta_iter,
4090 					  &done);
4091 }
4092 
4093 static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev)
4094 {
4095 	rtw89_phy_ccx_top_setting_init(rtwdev);
4096 	rtw89_phy_ifs_clm_setting_init(rtwdev);
4097 }
4098 
4099 void rtw89_phy_dm_init(struct rtw89_dev *rtwdev)
4100 {
4101 	const struct rtw89_chip_info *chip = rtwdev->chip;
4102 
4103 	rtw89_phy_stat_init(rtwdev);
4104 
4105 	rtw89_chip_bb_sethw(rtwdev);
4106 
4107 	rtw89_phy_env_monitor_init(rtwdev);
4108 	rtw89_physts_parsing_init(rtwdev);
4109 	rtw89_phy_dig_init(rtwdev);
4110 	rtw89_phy_cfo_init(rtwdev);
4111 	rtw89_phy_ul_tb_info_init(rtwdev);
4112 
4113 	rtw89_phy_init_rf_nctl(rtwdev);
4114 	rtw89_chip_rfk_init(rtwdev);
4115 	rtw89_load_txpwr_table(rtwdev, chip->byr_table);
4116 	rtw89_chip_set_txpwr_ctrl(rtwdev);
4117 	rtw89_chip_power_trim(rtwdev);
4118 	rtw89_chip_cfg_txrx_path(rtwdev);
4119 }
4120 
4121 void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
4122 {
4123 	const struct rtw89_chip_info *chip = rtwdev->chip;
4124 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
4125 	u8 bss_color;
4126 
4127 	if (!vif->bss_conf.he_support || !vif->cfg.assoc)
4128 		return;
4129 
4130 	bss_color = vif->bss_conf.he_bss_color.color;
4131 
4132 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_VLD0, 0x1,
4133 			      phy_idx);
4134 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT,
4135 			      bss_color, phy_idx);
4136 	rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID,
4137 			      vif->cfg.aid, phy_idx);
4138 }
4139 
4140 static void
4141 _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4142 {
4143 	rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data);
4144 }
4145 
4146 static void
4147 _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4148 {
4149 	rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
4150 }
4151 
4152 static void
4153 _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4154 {
4155 	rtw89_phy_write32_set(rtwdev, def->addr, def->mask);
4156 }
4157 
4158 static void
4159 _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4160 {
4161 	rtw89_phy_write32_clr(rtwdev, def->addr, def->mask);
4162 }
4163 
4164 static void
4165 _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def)
4166 {
4167 	udelay(def->data);
4168 }
4169 
4170 static void
4171 (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = {
4172 	[RTW89_RFK_F_WRF] = _rfk_write_rf,
4173 	[RTW89_RFK_F_WM] = _rfk_write32_mask,
4174 	[RTW89_RFK_F_WS] = _rfk_write32_set,
4175 	[RTW89_RFK_F_WC] = _rfk_write32_clr,
4176 	[RTW89_RFK_F_DELAY] = _rfk_delay,
4177 };
4178 
4179 static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM);
4180 
4181 void
4182 rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl)
4183 {
4184 	const struct rtw89_reg5_def *p = tbl->defs;
4185 	const struct rtw89_reg5_def *end = tbl->defs + tbl->size;
4186 
4187 	for (; p < end; p++)
4188 		_rfk_handler[p->flag](rtwdev, p);
4189 }
4190 EXPORT_SYMBOL(rtw89_rfk_parser);
4191 
4192 #define RTW89_TSSI_FAST_MODE_NUM 4
4193 
4194 static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = {
4195 	{0xD934, 0xff0000},
4196 	{0xD934, 0xff000000},
4197 	{0xD938, 0xff},
4198 	{0xD934, 0xff00},
4199 };
4200 
4201 static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = {
4202 	{0xD930, 0xff0000},
4203 	{0xD930, 0xff000000},
4204 	{0xD934, 0xff},
4205 	{0xD930, 0xff00},
4206 };
4207 
4208 static
4209 void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev,
4210 					   enum rtw89_mac_idx mac_idx,
4211 					   enum rtw89_tssi_bandedge_cfg bandedge_cfg,
4212 					   u32 val)
4213 {
4214 	const struct rtw89_reg_def *regs;
4215 	u32 reg;
4216 	int i;
4217 
4218 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4219 		regs = rtw89_tssi_fastmode_regs_flat;
4220 	else
4221 		regs = rtw89_tssi_fastmode_regs_level;
4222 
4223 	for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) {
4224 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4225 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, val);
4226 	}
4227 }
4228 
4229 static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = {
4230 	{0xD91C, 0xff000000},
4231 	{0xD920, 0xff},
4232 	{0xD920, 0xff00},
4233 	{0xD920, 0xff0000},
4234 	{0xD920, 0xff000000},
4235 	{0xD924, 0xff},
4236 	{0xD924, 0xff00},
4237 	{0xD914, 0xff000000},
4238 	{0xD918, 0xff},
4239 	{0xD918, 0xff00},
4240 	{0xD918, 0xff0000},
4241 	{0xD918, 0xff000000},
4242 	{0xD91C, 0xff},
4243 	{0xD91C, 0xff00},
4244 	{0xD91C, 0xff0000},
4245 };
4246 
4247 static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = {
4248 	{0xD910, 0xff},
4249 	{0xD910, 0xff00},
4250 	{0xD910, 0xff0000},
4251 	{0xD910, 0xff000000},
4252 	{0xD914, 0xff},
4253 	{0xD914, 0xff00},
4254 	{0xD914, 0xff0000},
4255 	{0xD908, 0xff},
4256 	{0xD908, 0xff00},
4257 	{0xD908, 0xff0000},
4258 	{0xD908, 0xff000000},
4259 	{0xD90C, 0xff},
4260 	{0xD90C, 0xff00},
4261 	{0xD90C, 0xff0000},
4262 	{0xD90C, 0xff000000},
4263 };
4264 
4265 void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev,
4266 					  enum rtw89_mac_idx mac_idx,
4267 					  enum rtw89_tssi_bandedge_cfg bandedge_cfg)
4268 {
4269 	const struct rtw89_chip_info *chip = rtwdev->chip;
4270 	const struct rtw89_reg_def *regs;
4271 	const u32 *data;
4272 	u32 reg;
4273 	int i;
4274 
4275 	if (bandedge_cfg >= RTW89_TSSI_CFG_NUM)
4276 		return;
4277 
4278 	if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT)
4279 		regs = rtw89_tssi_bandedge_regs_flat;
4280 	else
4281 		regs = rtw89_tssi_bandedge_regs_level;
4282 
4283 	data = chip->tssi_dbw_table->data[bandedge_cfg];
4284 
4285 	for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) {
4286 		reg = rtw89_mac_reg_by_idx(regs[i].addr, mac_idx);
4287 		rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]);
4288 	}
4289 
4290 	reg = rtw89_mac_reg_by_idx(R_AX_BANDEDGE_CFG, mac_idx);
4291 	rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg);
4292 
4293 	rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg,
4294 					      data[RTW89_TSSI_SBW20]);
4295 }
4296 EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg);
4297