1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2019-2020 Realtek Corporation 3 */ 4 5 #include "chan.h" 6 #include "coex.h" 7 #include "debug.h" 8 #include "fw.h" 9 #include "mac.h" 10 #include "phy.h" 11 #include "ps.h" 12 #include "reg.h" 13 #include "sar.h" 14 #include "txrx.h" 15 #include "util.h" 16 17 static u32 rtw89_phy0_phy1_offset(struct rtw89_dev *rtwdev, u32 addr) 18 { 19 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 20 21 return phy->phy0_phy1_offset(rtwdev, addr); 22 } 23 24 static u16 get_max_amsdu_len(struct rtw89_dev *rtwdev, 25 const struct rtw89_ra_report *report) 26 { 27 u32 bit_rate = report->bit_rate; 28 29 /* lower than ofdm, do not aggregate */ 30 if (bit_rate < 550) 31 return 1; 32 33 /* avoid AMSDU for legacy rate */ 34 if (report->might_fallback_legacy) 35 return 1; 36 37 /* lower than 20M vht 2ss mcs8, make it small */ 38 if (bit_rate < 1800) 39 return 1200; 40 41 /* lower than 40M vht 2ss mcs9, make it medium */ 42 if (bit_rate < 4000) 43 return 2600; 44 45 /* not yet 80M vht 2ss mcs8/9, make it twice regular packet size */ 46 if (bit_rate < 7000) 47 return 3500; 48 49 return rtwdev->chip->max_amsdu_limit; 50 } 51 52 static u64 get_mcs_ra_mask(u16 mcs_map, u8 highest_mcs, u8 gap) 53 { 54 u64 ra_mask = 0; 55 u8 mcs_cap; 56 int i, nss; 57 58 for (i = 0, nss = 12; i < 4; i++, mcs_map >>= 2, nss += 12) { 59 mcs_cap = mcs_map & 0x3; 60 switch (mcs_cap) { 61 case 2: 62 ra_mask |= GENMASK_ULL(highest_mcs, 0) << nss; 63 break; 64 case 1: 65 ra_mask |= GENMASK_ULL(highest_mcs - gap, 0) << nss; 66 break; 67 case 0: 68 ra_mask |= GENMASK_ULL(highest_mcs - gap * 2, 0) << nss; 69 break; 70 default: 71 break; 72 } 73 } 74 75 return ra_mask; 76 } 77 78 static u64 get_he_ra_mask(struct ieee80211_sta *sta) 79 { 80 struct ieee80211_sta_he_cap cap = sta->deflink.he_cap; 81 u16 mcs_map; 82 83 switch (sta->deflink.bandwidth) { 84 case IEEE80211_STA_RX_BW_160: 85 if (cap.he_cap_elem.phy_cap_info[0] & 86 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) 87 mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80p80); 88 else 89 mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_160); 90 break; 91 default: 92 mcs_map = le16_to_cpu(cap.he_mcs_nss_supp.rx_mcs_80); 93 } 94 95 /* MCS11, MCS9, MCS7 */ 96 return get_mcs_ra_mask(mcs_map, 11, 2); 97 } 98 99 static u64 get_eht_mcs_ra_mask(u8 *max_nss, u8 start_mcs, u8 n_nss) 100 { 101 u64 nss_mcs_shift; 102 u64 nss_mcs_val; 103 u64 mask = 0; 104 int i, j; 105 u8 nss; 106 107 for (i = 0; i < n_nss; i++) { 108 nss = u8_get_bits(max_nss[i], IEEE80211_EHT_MCS_NSS_RX); 109 if (!nss) 110 continue; 111 112 nss_mcs_val = GENMASK_ULL(start_mcs + i * 2, 0); 113 114 for (j = 0, nss_mcs_shift = 12; j < nss; j++, nss_mcs_shift += 16) 115 mask |= nss_mcs_val << nss_mcs_shift; 116 } 117 118 return mask; 119 } 120 121 static u64 get_eht_ra_mask(struct ieee80211_sta *sta) 122 { 123 struct ieee80211_sta_eht_cap *eht_cap = &sta->deflink.eht_cap; 124 struct ieee80211_eht_mcs_nss_supp_20mhz_only *mcs_nss_20mhz; 125 struct ieee80211_eht_mcs_nss_supp_bw *mcs_nss; 126 u8 *he_phy_cap = sta->deflink.he_cap.he_cap_elem.phy_cap_info; 127 128 switch (sta->deflink.bandwidth) { 129 case IEEE80211_STA_RX_BW_320: 130 mcs_nss = &eht_cap->eht_mcs_nss_supp.bw._320; 131 /* MCS 9, 11, 13 */ 132 return get_eht_mcs_ra_mask(mcs_nss->rx_tx_max_nss, 9, 3); 133 case IEEE80211_STA_RX_BW_160: 134 mcs_nss = &eht_cap->eht_mcs_nss_supp.bw._160; 135 /* MCS 9, 11, 13 */ 136 return get_eht_mcs_ra_mask(mcs_nss->rx_tx_max_nss, 9, 3); 137 case IEEE80211_STA_RX_BW_20: 138 if (!(he_phy_cap[0] & 139 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) { 140 mcs_nss_20mhz = &eht_cap->eht_mcs_nss_supp.only_20mhz; 141 /* MCS 7, 9, 11, 13 */ 142 return get_eht_mcs_ra_mask(mcs_nss_20mhz->rx_tx_max_nss, 7, 4); 143 } 144 fallthrough; 145 case IEEE80211_STA_RX_BW_80: 146 default: 147 mcs_nss = &eht_cap->eht_mcs_nss_supp.bw._80; 148 /* MCS 9, 11, 13 */ 149 return get_eht_mcs_ra_mask(mcs_nss->rx_tx_max_nss, 9, 3); 150 } 151 } 152 153 #define RA_FLOOR_TABLE_SIZE 7 154 #define RA_FLOOR_UP_GAP 3 155 static u64 rtw89_phy_ra_mask_rssi(struct rtw89_dev *rtwdev, u8 rssi, 156 u8 ratr_state) 157 { 158 u8 rssi_lv_t[RA_FLOOR_TABLE_SIZE] = {30, 44, 48, 52, 56, 60, 100}; 159 u8 rssi_lv = 0; 160 u8 i; 161 162 rssi >>= 1; 163 for (i = 0; i < RA_FLOOR_TABLE_SIZE; i++) { 164 if (i >= ratr_state) 165 rssi_lv_t[i] += RA_FLOOR_UP_GAP; 166 if (rssi < rssi_lv_t[i]) { 167 rssi_lv = i; 168 break; 169 } 170 } 171 if (rssi_lv == 0) 172 return 0xffffffffffffffffULL; 173 else if (rssi_lv == 1) 174 return 0xfffffffffffffff0ULL; 175 else if (rssi_lv == 2) 176 return 0xffffffffffffefe0ULL; 177 else if (rssi_lv == 3) 178 return 0xffffffffffffcfc0ULL; 179 else if (rssi_lv == 4) 180 return 0xffffffffffff8f80ULL; 181 else if (rssi_lv >= 5) 182 return 0xffffffffffff0f00ULL; 183 184 return 0xffffffffffffffffULL; 185 } 186 187 static u64 rtw89_phy_ra_mask_recover(u64 ra_mask, u64 ra_mask_bak) 188 { 189 if ((ra_mask & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)) == 0) 190 ra_mask |= (ra_mask_bak & ~(RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 191 192 if (ra_mask == 0) 193 ra_mask |= (ra_mask_bak & (RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES)); 194 195 return ra_mask; 196 } 197 198 static u64 rtw89_phy_ra_mask_cfg(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta, 199 const struct rtw89_chan *chan) 200 { 201 struct ieee80211_sta *sta = rtwsta_to_sta(rtwsta); 202 struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 203 enum nl80211_band band; 204 u64 cfg_mask; 205 206 if (!rtwsta->use_cfg_mask) 207 return -1; 208 209 switch (chan->band_type) { 210 case RTW89_BAND_2G: 211 band = NL80211_BAND_2GHZ; 212 cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_2GHZ].legacy, 213 RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES); 214 break; 215 case RTW89_BAND_5G: 216 band = NL80211_BAND_5GHZ; 217 cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_5GHZ].legacy, 218 RA_MASK_OFDM_RATES); 219 break; 220 case RTW89_BAND_6G: 221 band = NL80211_BAND_6GHZ; 222 cfg_mask = u64_encode_bits(mask->control[NL80211_BAND_6GHZ].legacy, 223 RA_MASK_OFDM_RATES); 224 break; 225 default: 226 rtw89_warn(rtwdev, "unhandled band type %d\n", chan->band_type); 227 return -1; 228 } 229 230 if (sta->deflink.he_cap.has_he) { 231 cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[0], 232 RA_MASK_HE_1SS_RATES); 233 cfg_mask |= u64_encode_bits(mask->control[band].he_mcs[1], 234 RA_MASK_HE_2SS_RATES); 235 } else if (sta->deflink.vht_cap.vht_supported) { 236 cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[0], 237 RA_MASK_VHT_1SS_RATES); 238 cfg_mask |= u64_encode_bits(mask->control[band].vht_mcs[1], 239 RA_MASK_VHT_2SS_RATES); 240 } else if (sta->deflink.ht_cap.ht_supported) { 241 cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[0], 242 RA_MASK_HT_1SS_RATES); 243 cfg_mask |= u64_encode_bits(mask->control[band].ht_mcs[1], 244 RA_MASK_HT_2SS_RATES); 245 } 246 247 return cfg_mask; 248 } 249 250 static const u64 251 rtw89_ra_mask_ht_rates[4] = {RA_MASK_HT_1SS_RATES, RA_MASK_HT_2SS_RATES, 252 RA_MASK_HT_3SS_RATES, RA_MASK_HT_4SS_RATES}; 253 static const u64 254 rtw89_ra_mask_vht_rates[4] = {RA_MASK_VHT_1SS_RATES, RA_MASK_VHT_2SS_RATES, 255 RA_MASK_VHT_3SS_RATES, RA_MASK_VHT_4SS_RATES}; 256 static const u64 257 rtw89_ra_mask_he_rates[4] = {RA_MASK_HE_1SS_RATES, RA_MASK_HE_2SS_RATES, 258 RA_MASK_HE_3SS_RATES, RA_MASK_HE_4SS_RATES}; 259 static const u64 260 rtw89_ra_mask_eht_rates[4] = {RA_MASK_EHT_1SS_RATES, RA_MASK_EHT_2SS_RATES, 261 RA_MASK_EHT_3SS_RATES, RA_MASK_EHT_4SS_RATES}; 262 263 static void rtw89_phy_ra_gi_ltf(struct rtw89_dev *rtwdev, 264 struct rtw89_sta *rtwsta, 265 const struct rtw89_chan *chan, 266 bool *fix_giltf_en, u8 *fix_giltf) 267 { 268 struct cfg80211_bitrate_mask *mask = &rtwsta->mask; 269 u8 band = chan->band_type; 270 enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 271 u8 he_gi = mask->control[nl_band].he_gi; 272 u8 he_ltf = mask->control[nl_band].he_ltf; 273 274 if (!rtwsta->use_cfg_mask) 275 return; 276 277 if (he_ltf == 2 && he_gi == 2) { 278 *fix_giltf = RTW89_GILTF_LGI_4XHE32; 279 } else if (he_ltf == 2 && he_gi == 0) { 280 *fix_giltf = RTW89_GILTF_SGI_4XHE08; 281 } else if (he_ltf == 1 && he_gi == 1) { 282 *fix_giltf = RTW89_GILTF_2XHE16; 283 } else if (he_ltf == 1 && he_gi == 0) { 284 *fix_giltf = RTW89_GILTF_2XHE08; 285 } else if (he_ltf == 0 && he_gi == 1) { 286 *fix_giltf = RTW89_GILTF_1XHE16; 287 } else if (he_ltf == 0 && he_gi == 0) { 288 *fix_giltf = RTW89_GILTF_1XHE08; 289 } else { 290 *fix_giltf_en = false; 291 return; 292 } 293 294 *fix_giltf_en = true; 295 } 296 297 static void rtw89_phy_ra_sta_update(struct rtw89_dev *rtwdev, 298 struct ieee80211_sta *sta, bool csi) 299 { 300 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 301 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 302 struct rtw89_phy_rate_pattern *rate_pattern = &rtwvif->rate_pattern; 303 struct rtw89_ra_info *ra = &rtwsta->ra; 304 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, 305 rtwvif->chanctx_idx); 306 struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif); 307 const u64 *high_rate_masks = rtw89_ra_mask_ht_rates; 308 u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi); 309 u64 ra_mask = 0; 310 u64 ra_mask_bak; 311 u8 mode = 0; 312 u8 csi_mode = RTW89_RA_RPT_MODE_LEGACY; 313 u8 bw_mode = 0; 314 u8 stbc_en = 0; 315 u8 ldpc_en = 0; 316 u8 fix_giltf = 0; 317 u8 i; 318 bool sgi = false; 319 bool fix_giltf_en = false; 320 321 memset(ra, 0, sizeof(*ra)); 322 /* Set the ra mask from sta's capability */ 323 if (sta->deflink.eht_cap.has_eht) { 324 mode |= RTW89_RA_MODE_EHT; 325 ra_mask |= get_eht_ra_mask(sta); 326 high_rate_masks = rtw89_ra_mask_eht_rates; 327 } else if (sta->deflink.he_cap.has_he) { 328 mode |= RTW89_RA_MODE_HE; 329 csi_mode = RTW89_RA_RPT_MODE_HE; 330 ra_mask |= get_he_ra_mask(sta); 331 high_rate_masks = rtw89_ra_mask_he_rates; 332 if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[2] & 333 IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ) 334 stbc_en = 1; 335 if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[1] & 336 IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD) 337 ldpc_en = 1; 338 rtw89_phy_ra_gi_ltf(rtwdev, rtwsta, chan, &fix_giltf_en, &fix_giltf); 339 } else if (sta->deflink.vht_cap.vht_supported) { 340 u16 mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.rx_mcs_map); 341 342 mode |= RTW89_RA_MODE_VHT; 343 csi_mode = RTW89_RA_RPT_MODE_VHT; 344 /* MCS9 (non-20MHz), MCS8, MCS7 */ 345 if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) 346 ra_mask |= get_mcs_ra_mask(mcs_map, 8, 1); 347 else 348 ra_mask |= get_mcs_ra_mask(mcs_map, 9, 1); 349 high_rate_masks = rtw89_ra_mask_vht_rates; 350 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK) 351 stbc_en = 1; 352 if (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC) 353 ldpc_en = 1; 354 } else if (sta->deflink.ht_cap.ht_supported) { 355 mode |= RTW89_RA_MODE_HT; 356 csi_mode = RTW89_RA_RPT_MODE_HT; 357 ra_mask |= ((u64)sta->deflink.ht_cap.mcs.rx_mask[3] << 48) | 358 ((u64)sta->deflink.ht_cap.mcs.rx_mask[2] << 36) | 359 (sta->deflink.ht_cap.mcs.rx_mask[1] << 24) | 360 (sta->deflink.ht_cap.mcs.rx_mask[0] << 12); 361 high_rate_masks = rtw89_ra_mask_ht_rates; 362 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC) 363 stbc_en = 1; 364 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING) 365 ldpc_en = 1; 366 } 367 368 switch (chan->band_type) { 369 case RTW89_BAND_2G: 370 ra_mask |= sta->deflink.supp_rates[NL80211_BAND_2GHZ]; 371 if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xf) 372 mode |= RTW89_RA_MODE_CCK; 373 if (sta->deflink.supp_rates[NL80211_BAND_2GHZ] & 0xff0) 374 mode |= RTW89_RA_MODE_OFDM; 375 break; 376 case RTW89_BAND_5G: 377 ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 4; 378 mode |= RTW89_RA_MODE_OFDM; 379 break; 380 case RTW89_BAND_6G: 381 ra_mask |= (u64)sta->deflink.supp_rates[NL80211_BAND_6GHZ] << 4; 382 mode |= RTW89_RA_MODE_OFDM; 383 break; 384 default: 385 rtw89_err(rtwdev, "Unknown band type\n"); 386 break; 387 } 388 389 ra_mask_bak = ra_mask; 390 391 if (mode >= RTW89_RA_MODE_HT) { 392 u64 mask = 0; 393 for (i = 0; i < rtwdev->hal.tx_nss; i++) 394 mask |= high_rate_masks[i]; 395 if (mode & RTW89_RA_MODE_OFDM) 396 mask |= RA_MASK_SUBOFDM_RATES; 397 if (mode & RTW89_RA_MODE_CCK) 398 mask |= RA_MASK_SUBCCK_RATES; 399 ra_mask &= mask; 400 } else if (mode & RTW89_RA_MODE_OFDM) { 401 ra_mask &= (RA_MASK_OFDM_RATES | RA_MASK_SUBCCK_RATES); 402 } 403 404 if (mode != RTW89_RA_MODE_CCK) 405 ra_mask &= rtw89_phy_ra_mask_rssi(rtwdev, rssi, 0); 406 407 ra_mask = rtw89_phy_ra_mask_recover(ra_mask, ra_mask_bak); 408 ra_mask &= rtw89_phy_ra_mask_cfg(rtwdev, rtwsta, chan); 409 410 switch (sta->deflink.bandwidth) { 411 case IEEE80211_STA_RX_BW_160: 412 bw_mode = RTW89_CHANNEL_WIDTH_160; 413 sgi = sta->deflink.vht_cap.vht_supported && 414 (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_160); 415 break; 416 case IEEE80211_STA_RX_BW_80: 417 bw_mode = RTW89_CHANNEL_WIDTH_80; 418 sgi = sta->deflink.vht_cap.vht_supported && 419 (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80); 420 break; 421 case IEEE80211_STA_RX_BW_40: 422 bw_mode = RTW89_CHANNEL_WIDTH_40; 423 sgi = sta->deflink.ht_cap.ht_supported && 424 (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40); 425 break; 426 default: 427 bw_mode = RTW89_CHANNEL_WIDTH_20; 428 sgi = sta->deflink.ht_cap.ht_supported && 429 (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20); 430 break; 431 } 432 433 if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[3] & 434 IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_16_QAM) 435 ra->dcm_cap = 1; 436 437 if (rate_pattern->enable && !vif->p2p) { 438 ra_mask = rtw89_phy_ra_mask_cfg(rtwdev, rtwsta, chan); 439 ra_mask &= rate_pattern->ra_mask; 440 mode = rate_pattern->ra_mode; 441 } 442 443 ra->bw_cap = bw_mode; 444 ra->er_cap = rtwsta->er_cap; 445 ra->mode_ctrl = mode; 446 ra->macid = rtwsta->mac_id; 447 ra->stbc_cap = stbc_en; 448 ra->ldpc_cap = ldpc_en; 449 ra->ss_num = min(sta->deflink.rx_nss, rtwdev->hal.tx_nss) - 1; 450 ra->en_sgi = sgi; 451 ra->ra_mask = ra_mask; 452 ra->fix_giltf_en = fix_giltf_en; 453 ra->fix_giltf = fix_giltf; 454 455 if (!csi) 456 return; 457 458 ra->fixed_csi_rate_en = false; 459 ra->ra_csi_rate_en = true; 460 ra->cr_tbl_sel = false; 461 ra->band_num = rtwvif->phy_idx; 462 ra->csi_bw = bw_mode; 463 ra->csi_gi_ltf = RTW89_GILTF_LGI_4XHE32; 464 ra->csi_mcs_ss_idx = 5; 465 ra->csi_mode = csi_mode; 466 } 467 468 void rtw89_phy_ra_update_sta(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta, 469 u32 changed) 470 { 471 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 472 struct rtw89_ra_info *ra = &rtwsta->ra; 473 474 rtw89_phy_ra_sta_update(rtwdev, sta, false); 475 476 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) 477 ra->upd_mask = 1; 478 if (changed & (IEEE80211_RC_BW_CHANGED | IEEE80211_RC_NSS_CHANGED)) 479 ra->upd_bw_nss_mask = 1; 480 481 rtw89_debug(rtwdev, RTW89_DBG_RA, 482 "ra updat: macid = %d, bw = %d, nss = %d, gi = %d %d", 483 ra->macid, 484 ra->bw_cap, 485 ra->ss_num, 486 ra->en_sgi, 487 ra->giltf); 488 489 rtw89_fw_h2c_ra(rtwdev, ra, false); 490 } 491 492 static bool __check_rate_pattern(struct rtw89_phy_rate_pattern *next, 493 u16 rate_base, u64 ra_mask, u8 ra_mode, 494 u32 rate_ctrl, u32 ctrl_skip, bool force) 495 { 496 u8 n, c; 497 498 if (rate_ctrl == ctrl_skip) 499 return true; 500 501 n = hweight32(rate_ctrl); 502 if (n == 0) 503 return true; 504 505 if (force && n != 1) 506 return false; 507 508 if (next->enable) 509 return false; 510 511 c = __fls(rate_ctrl); 512 next->rate = rate_base + c; 513 next->ra_mode = ra_mode; 514 next->ra_mask = ra_mask; 515 next->enable = true; 516 517 return true; 518 } 519 520 #define RTW89_HW_RATE_BY_CHIP_GEN(rate) \ 521 { \ 522 [RTW89_CHIP_AX] = RTW89_HW_RATE_ ## rate, \ 523 [RTW89_CHIP_BE] = RTW89_HW_RATE_V1_ ## rate, \ 524 } 525 526 void rtw89_phy_rate_pattern_vif(struct rtw89_dev *rtwdev, 527 struct ieee80211_vif *vif, 528 const struct cfg80211_bitrate_mask *mask) 529 { 530 struct ieee80211_supported_band *sband; 531 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 532 struct rtw89_phy_rate_pattern next_pattern = {0}; 533 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, 534 rtwvif->chanctx_idx); 535 static const u16 hw_rate_he[][RTW89_CHIP_GEN_NUM] = { 536 RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS1_MCS0), 537 RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS2_MCS0), 538 RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS3_MCS0), 539 RTW89_HW_RATE_BY_CHIP_GEN(HE_NSS4_MCS0), 540 }; 541 static const u16 hw_rate_vht[][RTW89_CHIP_GEN_NUM] = { 542 RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS1_MCS0), 543 RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS2_MCS0), 544 RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS3_MCS0), 545 RTW89_HW_RATE_BY_CHIP_GEN(VHT_NSS4_MCS0), 546 }; 547 static const u16 hw_rate_ht[][RTW89_CHIP_GEN_NUM] = { 548 RTW89_HW_RATE_BY_CHIP_GEN(MCS0), 549 RTW89_HW_RATE_BY_CHIP_GEN(MCS8), 550 RTW89_HW_RATE_BY_CHIP_GEN(MCS16), 551 RTW89_HW_RATE_BY_CHIP_GEN(MCS24), 552 }; 553 u8 band = chan->band_type; 554 enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 555 enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen; 556 u8 tx_nss = rtwdev->hal.tx_nss; 557 u8 i; 558 559 for (i = 0; i < tx_nss; i++) 560 if (!__check_rate_pattern(&next_pattern, hw_rate_he[i][chip_gen], 561 RA_MASK_HE_RATES, RTW89_RA_MODE_HE, 562 mask->control[nl_band].he_mcs[i], 563 0, true)) 564 goto out; 565 566 for (i = 0; i < tx_nss; i++) 567 if (!__check_rate_pattern(&next_pattern, hw_rate_vht[i][chip_gen], 568 RA_MASK_VHT_RATES, RTW89_RA_MODE_VHT, 569 mask->control[nl_band].vht_mcs[i], 570 0, true)) 571 goto out; 572 573 for (i = 0; i < tx_nss; i++) 574 if (!__check_rate_pattern(&next_pattern, hw_rate_ht[i][chip_gen], 575 RA_MASK_HT_RATES, RTW89_RA_MODE_HT, 576 mask->control[nl_band].ht_mcs[i], 577 0, true)) 578 goto out; 579 580 /* lagacy cannot be empty for nl80211_parse_tx_bitrate_mask, and 581 * require at least one basic rate for ieee80211_set_bitrate_mask, 582 * so the decision just depends on if all bitrates are set or not. 583 */ 584 sband = rtwdev->hw->wiphy->bands[nl_band]; 585 if (band == RTW89_BAND_2G) { 586 if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_CCK1, 587 RA_MASK_CCK_RATES | RA_MASK_OFDM_RATES, 588 RTW89_RA_MODE_CCK | RTW89_RA_MODE_OFDM, 589 mask->control[nl_band].legacy, 590 BIT(sband->n_bitrates) - 1, false)) 591 goto out; 592 } else { 593 if (!__check_rate_pattern(&next_pattern, RTW89_HW_RATE_OFDM6, 594 RA_MASK_OFDM_RATES, RTW89_RA_MODE_OFDM, 595 mask->control[nl_band].legacy, 596 BIT(sband->n_bitrates) - 1, false)) 597 goto out; 598 } 599 600 if (!next_pattern.enable) 601 goto out; 602 603 rtwvif->rate_pattern = next_pattern; 604 rtw89_debug(rtwdev, RTW89_DBG_RA, 605 "configure pattern: rate 0x%x, mask 0x%llx, mode 0x%x\n", 606 next_pattern.rate, 607 next_pattern.ra_mask, 608 next_pattern.ra_mode); 609 return; 610 611 out: 612 rtwvif->rate_pattern.enable = false; 613 rtw89_debug(rtwdev, RTW89_DBG_RA, "unset rate pattern\n"); 614 } 615 616 static void rtw89_phy_ra_update_sta_iter(void *data, struct ieee80211_sta *sta) 617 { 618 struct rtw89_dev *rtwdev = (struct rtw89_dev *)data; 619 620 rtw89_phy_ra_update_sta(rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED); 621 } 622 623 void rtw89_phy_ra_update(struct rtw89_dev *rtwdev) 624 { 625 ieee80211_iterate_stations_atomic(rtwdev->hw, 626 rtw89_phy_ra_update_sta_iter, 627 rtwdev); 628 } 629 630 void rtw89_phy_ra_assoc(struct rtw89_dev *rtwdev, struct ieee80211_sta *sta) 631 { 632 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 633 struct rtw89_ra_info *ra = &rtwsta->ra; 634 u8 rssi = ewma_rssi_read(&rtwsta->avg_rssi) >> RSSI_FACTOR; 635 bool csi = rtw89_sta_has_beamformer_cap(sta); 636 637 rtw89_phy_ra_sta_update(rtwdev, sta, csi); 638 639 if (rssi > 40) 640 ra->init_rate_lv = 1; 641 else if (rssi > 20) 642 ra->init_rate_lv = 2; 643 else if (rssi > 1) 644 ra->init_rate_lv = 3; 645 else 646 ra->init_rate_lv = 0; 647 ra->upd_all = 1; 648 rtw89_debug(rtwdev, RTW89_DBG_RA, 649 "ra assoc: macid = %d, mode = %d, bw = %d, nss = %d, lv = %d", 650 ra->macid, 651 ra->mode_ctrl, 652 ra->bw_cap, 653 ra->ss_num, 654 ra->init_rate_lv); 655 rtw89_debug(rtwdev, RTW89_DBG_RA, 656 "ra assoc: dcm = %d, er = %d, ldpc = %d, stbc = %d, gi = %d %d", 657 ra->dcm_cap, 658 ra->er_cap, 659 ra->ldpc_cap, 660 ra->stbc_cap, 661 ra->en_sgi, 662 ra->giltf); 663 664 rtw89_fw_h2c_ra(rtwdev, ra, csi); 665 } 666 667 u8 rtw89_phy_get_txsc(struct rtw89_dev *rtwdev, 668 const struct rtw89_chan *chan, 669 enum rtw89_bandwidth dbw) 670 { 671 enum rtw89_bandwidth cbw = chan->band_width; 672 u8 pri_ch = chan->primary_channel; 673 u8 central_ch = chan->channel; 674 u8 txsc_idx = 0; 675 u8 tmp = 0; 676 677 if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20) 678 return txsc_idx; 679 680 switch (cbw) { 681 case RTW89_CHANNEL_WIDTH_40: 682 txsc_idx = pri_ch > central_ch ? 1 : 2; 683 break; 684 case RTW89_CHANNEL_WIDTH_80: 685 if (dbw == RTW89_CHANNEL_WIDTH_20) { 686 if (pri_ch > central_ch) 687 txsc_idx = (pri_ch - central_ch) >> 1; 688 else 689 txsc_idx = ((central_ch - pri_ch) >> 1) + 1; 690 } else { 691 txsc_idx = pri_ch > central_ch ? 9 : 10; 692 } 693 break; 694 case RTW89_CHANNEL_WIDTH_160: 695 if (pri_ch > central_ch) 696 tmp = (pri_ch - central_ch) >> 1; 697 else 698 tmp = ((central_ch - pri_ch) >> 1) + 1; 699 700 if (dbw == RTW89_CHANNEL_WIDTH_20) { 701 txsc_idx = tmp; 702 } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 703 if (tmp == 1 || tmp == 3) 704 txsc_idx = 9; 705 else if (tmp == 5 || tmp == 7) 706 txsc_idx = 11; 707 else if (tmp == 2 || tmp == 4) 708 txsc_idx = 10; 709 else if (tmp == 6 || tmp == 8) 710 txsc_idx = 12; 711 else 712 return 0xff; 713 } else { 714 txsc_idx = pri_ch > central_ch ? 13 : 14; 715 } 716 break; 717 case RTW89_CHANNEL_WIDTH_80_80: 718 if (dbw == RTW89_CHANNEL_WIDTH_20) { 719 if (pri_ch > central_ch) 720 txsc_idx = (10 - (pri_ch - central_ch)) >> 1; 721 else 722 txsc_idx = ((central_ch - pri_ch) >> 1) + 5; 723 } else if (dbw == RTW89_CHANNEL_WIDTH_40) { 724 txsc_idx = pri_ch > central_ch ? 10 : 12; 725 } else { 726 txsc_idx = 14; 727 } 728 break; 729 default: 730 break; 731 } 732 733 return txsc_idx; 734 } 735 EXPORT_SYMBOL(rtw89_phy_get_txsc); 736 737 u8 rtw89_phy_get_txsb(struct rtw89_dev *rtwdev, const struct rtw89_chan *chan, 738 enum rtw89_bandwidth dbw) 739 { 740 enum rtw89_bandwidth cbw = chan->band_width; 741 u8 pri_ch = chan->primary_channel; 742 u8 central_ch = chan->channel; 743 u8 txsb_idx = 0; 744 745 if (cbw == dbw || cbw == RTW89_CHANNEL_WIDTH_20) 746 return txsb_idx; 747 748 switch (cbw) { 749 case RTW89_CHANNEL_WIDTH_40: 750 txsb_idx = pri_ch > central_ch ? 1 : 0; 751 break; 752 case RTW89_CHANNEL_WIDTH_80: 753 if (dbw == RTW89_CHANNEL_WIDTH_20) 754 txsb_idx = (pri_ch - central_ch + 6) / 4; 755 else 756 txsb_idx = pri_ch > central_ch ? 1 : 0; 757 break; 758 case RTW89_CHANNEL_WIDTH_160: 759 if (dbw == RTW89_CHANNEL_WIDTH_20) 760 txsb_idx = (pri_ch - central_ch + 14) / 4; 761 else if (dbw == RTW89_CHANNEL_WIDTH_40) 762 txsb_idx = (pri_ch - central_ch + 12) / 8; 763 else 764 txsb_idx = pri_ch > central_ch ? 1 : 0; 765 break; 766 case RTW89_CHANNEL_WIDTH_320: 767 if (dbw == RTW89_CHANNEL_WIDTH_20) 768 txsb_idx = (pri_ch - central_ch + 30) / 4; 769 else if (dbw == RTW89_CHANNEL_WIDTH_40) 770 txsb_idx = (pri_ch - central_ch + 28) / 8; 771 else if (dbw == RTW89_CHANNEL_WIDTH_80) 772 txsb_idx = (pri_ch - central_ch + 24) / 16; 773 else 774 txsb_idx = pri_ch > central_ch ? 1 : 0; 775 break; 776 default: 777 break; 778 } 779 780 return txsb_idx; 781 } 782 EXPORT_SYMBOL(rtw89_phy_get_txsb); 783 784 static bool rtw89_phy_check_swsi_busy(struct rtw89_dev *rtwdev) 785 { 786 return !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_W_BUSY_V1) || 787 !!rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, B_SWSI_R_BUSY_V1); 788 } 789 790 u32 rtw89_phy_read_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 791 u32 addr, u32 mask) 792 { 793 const struct rtw89_chip_info *chip = rtwdev->chip; 794 const u32 *base_addr = chip->rf_base_addr; 795 u32 val, direct_addr; 796 797 if (rf_path >= rtwdev->chip->rf_path_num) { 798 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 799 return INV_RF_DATA; 800 } 801 802 addr &= 0xff; 803 direct_addr = base_addr[rf_path] + (addr << 2); 804 mask &= RFREG_MASK; 805 806 val = rtw89_phy_read32_mask(rtwdev, direct_addr, mask); 807 808 return val; 809 } 810 EXPORT_SYMBOL(rtw89_phy_read_rf); 811 812 static u32 rtw89_phy_read_rf_a(struct rtw89_dev *rtwdev, 813 enum rtw89_rf_path rf_path, u32 addr, u32 mask) 814 { 815 bool busy; 816 bool done; 817 u32 val; 818 int ret; 819 820 ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 821 1, 30, false, rtwdev); 822 if (ret) { 823 rtw89_err(rtwdev, "read rf busy swsi\n"); 824 return INV_RF_DATA; 825 } 826 827 mask &= RFREG_MASK; 828 829 val = FIELD_PREP(B_SWSI_READ_ADDR_PATH_V1, rf_path) | 830 FIELD_PREP(B_SWSI_READ_ADDR_ADDR_V1, addr); 831 rtw89_phy_write32_mask(rtwdev, R_SWSI_READ_ADDR_V1, B_SWSI_READ_ADDR_V1, val); 832 udelay(2); 833 834 ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 1, 835 30, false, rtwdev, R_SWSI_V1, 836 B_SWSI_R_DATA_DONE_V1); 837 if (ret) { 838 rtw89_err(rtwdev, "read swsi busy\n"); 839 return INV_RF_DATA; 840 } 841 842 return rtw89_phy_read32_mask(rtwdev, R_SWSI_V1, mask); 843 } 844 845 u32 rtw89_phy_read_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 846 u32 addr, u32 mask) 847 { 848 bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 849 850 if (rf_path >= rtwdev->chip->rf_path_num) { 851 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 852 return INV_RF_DATA; 853 } 854 855 if (ad_sel) 856 return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask); 857 else 858 return rtw89_phy_read_rf_a(rtwdev, rf_path, addr, mask); 859 } 860 EXPORT_SYMBOL(rtw89_phy_read_rf_v1); 861 862 static u32 rtw89_phy_read_full_rf_v2_a(struct rtw89_dev *rtwdev, 863 enum rtw89_rf_path rf_path, u32 addr) 864 { 865 static const u16 r_addr_ofst[2] = {0x2C24, 0x2D24}; 866 static const u16 addr_ofst[2] = {0x2ADC, 0x2BDC}; 867 bool busy, done; 868 int ret; 869 u32 val; 870 871 rtw89_phy_write32_mask(rtwdev, addr_ofst[rf_path], B_HWSI_ADD_CTL_MASK, 0x1); 872 ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, busy, !busy, 873 1, 3800, false, 874 rtwdev, r_addr_ofst[rf_path], B_HWSI_VAL_BUSY); 875 if (ret) { 876 rtw89_warn(rtwdev, "poll HWSI is busy\n"); 877 return INV_RF_DATA; 878 } 879 880 rtw89_phy_write32_mask(rtwdev, addr_ofst[rf_path], B_HWSI_ADD_MASK, addr); 881 rtw89_phy_write32_mask(rtwdev, addr_ofst[rf_path], B_HWSI_ADD_RD, 0x1); 882 udelay(2); 883 884 ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, done, done, 885 1, 3800, false, 886 rtwdev, r_addr_ofst[rf_path], B_HWSI_VAL_RDONE); 887 if (ret) { 888 rtw89_warn(rtwdev, "read HWSI is busy\n"); 889 val = INV_RF_DATA; 890 goto out; 891 } 892 893 val = rtw89_phy_read32_mask(rtwdev, r_addr_ofst[rf_path], RFREG_MASK); 894 out: 895 rtw89_phy_write32_mask(rtwdev, addr_ofst[rf_path], B_HWSI_ADD_POLL_MASK, 0); 896 897 return val; 898 } 899 900 static u32 rtw89_phy_read_rf_v2_a(struct rtw89_dev *rtwdev, 901 enum rtw89_rf_path rf_path, u32 addr, u32 mask) 902 { 903 u32 val; 904 905 val = rtw89_phy_read_full_rf_v2_a(rtwdev, rf_path, addr); 906 907 return (val & mask) >> __ffs(mask); 908 } 909 910 u32 rtw89_phy_read_rf_v2(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 911 u32 addr, u32 mask) 912 { 913 bool ad_sel = u32_get_bits(addr, RTW89_RF_ADDR_ADSEL_MASK); 914 915 if (rf_path >= rtwdev->chip->rf_path_num) { 916 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 917 return INV_RF_DATA; 918 } 919 920 if (ad_sel) 921 return rtw89_phy_read_rf(rtwdev, rf_path, addr, mask); 922 else 923 return rtw89_phy_read_rf_v2_a(rtwdev, rf_path, addr, mask); 924 } 925 EXPORT_SYMBOL(rtw89_phy_read_rf_v2); 926 927 bool rtw89_phy_write_rf(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 928 u32 addr, u32 mask, u32 data) 929 { 930 const struct rtw89_chip_info *chip = rtwdev->chip; 931 const u32 *base_addr = chip->rf_base_addr; 932 u32 direct_addr; 933 934 if (rf_path >= rtwdev->chip->rf_path_num) { 935 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 936 return false; 937 } 938 939 addr &= 0xff; 940 direct_addr = base_addr[rf_path] + (addr << 2); 941 mask &= RFREG_MASK; 942 943 rtw89_phy_write32_mask(rtwdev, direct_addr, mask, data); 944 945 /* delay to ensure writing properly */ 946 udelay(1); 947 948 return true; 949 } 950 EXPORT_SYMBOL(rtw89_phy_write_rf); 951 952 static bool rtw89_phy_write_rf_a(struct rtw89_dev *rtwdev, 953 enum rtw89_rf_path rf_path, u32 addr, u32 mask, 954 u32 data) 955 { 956 u8 bit_shift; 957 u32 val; 958 bool busy, b_msk_en = false; 959 int ret; 960 961 ret = read_poll_timeout_atomic(rtw89_phy_check_swsi_busy, busy, !busy, 962 1, 30, false, rtwdev); 963 if (ret) { 964 rtw89_err(rtwdev, "write rf busy swsi\n"); 965 return false; 966 } 967 968 data &= RFREG_MASK; 969 mask &= RFREG_MASK; 970 971 if (mask != RFREG_MASK) { 972 b_msk_en = true; 973 rtw89_phy_write32_mask(rtwdev, R_SWSI_BIT_MASK_V1, RFREG_MASK, 974 mask); 975 bit_shift = __ffs(mask); 976 data = (data << bit_shift) & RFREG_MASK; 977 } 978 979 val = FIELD_PREP(B_SWSI_DATA_BIT_MASK_EN_V1, b_msk_en) | 980 FIELD_PREP(B_SWSI_DATA_PATH_V1, rf_path) | 981 FIELD_PREP(B_SWSI_DATA_ADDR_V1, addr) | 982 FIELD_PREP(B_SWSI_DATA_VAL_V1, data); 983 984 rtw89_phy_write32_mask(rtwdev, R_SWSI_DATA_V1, MASKDWORD, val); 985 986 return true; 987 } 988 989 bool rtw89_phy_write_rf_v1(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 990 u32 addr, u32 mask, u32 data) 991 { 992 bool ad_sel = FIELD_GET(RTW89_RF_ADDR_ADSEL_MASK, addr); 993 994 if (rf_path >= rtwdev->chip->rf_path_num) { 995 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 996 return false; 997 } 998 999 if (ad_sel) 1000 return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data); 1001 else 1002 return rtw89_phy_write_rf_a(rtwdev, rf_path, addr, mask, data); 1003 } 1004 EXPORT_SYMBOL(rtw89_phy_write_rf_v1); 1005 1006 static 1007 bool rtw89_phy_write_full_rf_v2_a(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 1008 u32 addr, u32 data) 1009 { 1010 static const u32 addr_is_idle[2] = {0x2C24, 0x2D24}; 1011 static const u32 addr_ofst[2] = {0x2AE0, 0x2BE0}; 1012 bool busy; 1013 u32 val; 1014 int ret; 1015 1016 ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, busy, !busy, 1017 1, 3800, false, 1018 rtwdev, addr_is_idle[rf_path], BIT(29)); 1019 if (ret) { 1020 rtw89_warn(rtwdev, "[%s] HWSI is busy\n", __func__); 1021 return false; 1022 } 1023 1024 val = u32_encode_bits(addr, B_HWSI_DATA_ADDR) | 1025 u32_encode_bits(data, B_HWSI_DATA_VAL); 1026 1027 rtw89_phy_write32(rtwdev, addr_ofst[rf_path], val); 1028 1029 return true; 1030 } 1031 1032 static 1033 bool rtw89_phy_write_rf_a_v2(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 1034 u32 addr, u32 mask, u32 data) 1035 { 1036 u32 val; 1037 1038 if (mask == RFREG_MASK) { 1039 val = data; 1040 } else { 1041 val = rtw89_phy_read_full_rf_v2_a(rtwdev, rf_path, addr); 1042 val &= ~mask; 1043 val |= (data << __ffs(mask)) & mask; 1044 } 1045 1046 return rtw89_phy_write_full_rf_v2_a(rtwdev, rf_path, addr, val); 1047 } 1048 1049 bool rtw89_phy_write_rf_v2(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path, 1050 u32 addr, u32 mask, u32 data) 1051 { 1052 bool ad_sel = u32_get_bits(addr, RTW89_RF_ADDR_ADSEL_MASK); 1053 1054 if (rf_path >= rtwdev->chip->rf_path_num) { 1055 rtw89_err(rtwdev, "unsupported rf path (%d)\n", rf_path); 1056 return INV_RF_DATA; 1057 } 1058 1059 if (ad_sel) 1060 return rtw89_phy_write_rf(rtwdev, rf_path, addr, mask, data); 1061 else 1062 return rtw89_phy_write_rf_a_v2(rtwdev, rf_path, addr, mask, data); 1063 } 1064 EXPORT_SYMBOL(rtw89_phy_write_rf_v2); 1065 1066 static bool rtw89_chip_rf_v1(struct rtw89_dev *rtwdev) 1067 { 1068 return rtwdev->chip->ops->write_rf == rtw89_phy_write_rf_v1; 1069 } 1070 1071 static void rtw89_phy_bb_reset(struct rtw89_dev *rtwdev, 1072 enum rtw89_phy_idx phy_idx) 1073 { 1074 const struct rtw89_chip_info *chip = rtwdev->chip; 1075 1076 chip->ops->bb_reset(rtwdev, phy_idx); 1077 } 1078 1079 static void rtw89_phy_config_bb_reg(struct rtw89_dev *rtwdev, 1080 const struct rtw89_reg2_def *reg, 1081 enum rtw89_rf_path rf_path, 1082 void *extra_data) 1083 { 1084 u32 addr; 1085 1086 if (reg->addr == 0xfe) { 1087 mdelay(50); 1088 } else if (reg->addr == 0xfd) { 1089 mdelay(5); 1090 } else if (reg->addr == 0xfc) { 1091 mdelay(1); 1092 } else if (reg->addr == 0xfb) { 1093 udelay(50); 1094 } else if (reg->addr == 0xfa) { 1095 udelay(5); 1096 } else if (reg->addr == 0xf9) { 1097 udelay(1); 1098 } else if (reg->data == BYPASS_CR_DATA) { 1099 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Bypass CR 0x%x\n", reg->addr); 1100 } else { 1101 addr = reg->addr; 1102 1103 if ((uintptr_t)extra_data == RTW89_PHY_1) 1104 addr += rtw89_phy0_phy1_offset(rtwdev, reg->addr); 1105 1106 rtw89_phy_write32(rtwdev, addr, reg->data); 1107 } 1108 } 1109 1110 union rtw89_phy_bb_gain_arg { 1111 u32 addr; 1112 struct { 1113 union { 1114 u8 type; 1115 struct { 1116 u8 rxsc_start:4; 1117 u8 bw:4; 1118 }; 1119 }; 1120 u8 path; 1121 u8 gain_band; 1122 u8 cfg_type; 1123 }; 1124 } __packed; 1125 1126 static void 1127 rtw89_phy_cfg_bb_gain_error(struct rtw89_dev *rtwdev, 1128 union rtw89_phy_bb_gain_arg arg, u32 data) 1129 { 1130 struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax; 1131 u8 type = arg.type; 1132 u8 path = arg.path; 1133 u8 gband = arg.gain_band; 1134 int i; 1135 1136 switch (type) { 1137 case 0: 1138 for (i = 0; i < 4; i++, data >>= 8) 1139 gain->lna_gain[gband][path][i] = data & 0xff; 1140 break; 1141 case 1: 1142 for (i = 4; i < 7; i++, data >>= 8) 1143 gain->lna_gain[gband][path][i] = data & 0xff; 1144 break; 1145 case 2: 1146 for (i = 0; i < 2; i++, data >>= 8) 1147 gain->tia_gain[gband][path][i] = data & 0xff; 1148 break; 1149 default: 1150 rtw89_warn(rtwdev, 1151 "bb gain error {0x%x:0x%x} with unknown type: %d\n", 1152 arg.addr, data, type); 1153 break; 1154 } 1155 } 1156 1157 enum rtw89_phy_bb_rxsc_start_idx { 1158 RTW89_BB_RXSC_START_IDX_FULL = 0, 1159 RTW89_BB_RXSC_START_IDX_20 = 1, 1160 RTW89_BB_RXSC_START_IDX_20_1 = 5, 1161 RTW89_BB_RXSC_START_IDX_40 = 9, 1162 RTW89_BB_RXSC_START_IDX_80 = 13, 1163 }; 1164 1165 static void 1166 rtw89_phy_cfg_bb_rpl_ofst(struct rtw89_dev *rtwdev, 1167 union rtw89_phy_bb_gain_arg arg, u32 data) 1168 { 1169 struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax; 1170 u8 rxsc_start = arg.rxsc_start; 1171 u8 bw = arg.bw; 1172 u8 path = arg.path; 1173 u8 gband = arg.gain_band; 1174 u8 rxsc; 1175 s8 ofst; 1176 int i; 1177 1178 switch (bw) { 1179 case RTW89_CHANNEL_WIDTH_20: 1180 gain->rpl_ofst_20[gband][path] = (s8)data; 1181 break; 1182 case RTW89_CHANNEL_WIDTH_40: 1183 if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 1184 gain->rpl_ofst_40[gband][path][0] = (s8)data; 1185 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 1186 for (i = 0; i < 2; i++, data >>= 8) { 1187 rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 1188 ofst = (s8)(data & 0xff); 1189 gain->rpl_ofst_40[gband][path][rxsc] = ofst; 1190 } 1191 } 1192 break; 1193 case RTW89_CHANNEL_WIDTH_80: 1194 if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 1195 gain->rpl_ofst_80[gband][path][0] = (s8)data; 1196 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 1197 for (i = 0; i < 4; i++, data >>= 8) { 1198 rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 1199 ofst = (s8)(data & 0xff); 1200 gain->rpl_ofst_80[gband][path][rxsc] = ofst; 1201 } 1202 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 1203 for (i = 0; i < 2; i++, data >>= 8) { 1204 rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 1205 ofst = (s8)(data & 0xff); 1206 gain->rpl_ofst_80[gband][path][rxsc] = ofst; 1207 } 1208 } 1209 break; 1210 case RTW89_CHANNEL_WIDTH_160: 1211 if (rxsc_start == RTW89_BB_RXSC_START_IDX_FULL) { 1212 gain->rpl_ofst_160[gband][path][0] = (s8)data; 1213 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20) { 1214 for (i = 0; i < 4; i++, data >>= 8) { 1215 rxsc = RTW89_BB_RXSC_START_IDX_20 + i; 1216 ofst = (s8)(data & 0xff); 1217 gain->rpl_ofst_160[gband][path][rxsc] = ofst; 1218 } 1219 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_20_1) { 1220 for (i = 0; i < 4; i++, data >>= 8) { 1221 rxsc = RTW89_BB_RXSC_START_IDX_20_1 + i; 1222 ofst = (s8)(data & 0xff); 1223 gain->rpl_ofst_160[gband][path][rxsc] = ofst; 1224 } 1225 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_40) { 1226 for (i = 0; i < 4; i++, data >>= 8) { 1227 rxsc = RTW89_BB_RXSC_START_IDX_40 + i; 1228 ofst = (s8)(data & 0xff); 1229 gain->rpl_ofst_160[gband][path][rxsc] = ofst; 1230 } 1231 } else if (rxsc_start == RTW89_BB_RXSC_START_IDX_80) { 1232 for (i = 0; i < 2; i++, data >>= 8) { 1233 rxsc = RTW89_BB_RXSC_START_IDX_80 + i; 1234 ofst = (s8)(data & 0xff); 1235 gain->rpl_ofst_160[gband][path][rxsc] = ofst; 1236 } 1237 } 1238 break; 1239 default: 1240 rtw89_warn(rtwdev, 1241 "bb rpl ofst {0x%x:0x%x} with unknown bw: %d\n", 1242 arg.addr, data, bw); 1243 break; 1244 } 1245 } 1246 1247 static void 1248 rtw89_phy_cfg_bb_gain_bypass(struct rtw89_dev *rtwdev, 1249 union rtw89_phy_bb_gain_arg arg, u32 data) 1250 { 1251 struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax; 1252 u8 type = arg.type; 1253 u8 path = arg.path; 1254 u8 gband = arg.gain_band; 1255 int i; 1256 1257 switch (type) { 1258 case 0: 1259 for (i = 0; i < 4; i++, data >>= 8) 1260 gain->lna_gain_bypass[gband][path][i] = data & 0xff; 1261 break; 1262 case 1: 1263 for (i = 4; i < 7; i++, data >>= 8) 1264 gain->lna_gain_bypass[gband][path][i] = data & 0xff; 1265 break; 1266 default: 1267 rtw89_warn(rtwdev, 1268 "bb gain bypass {0x%x:0x%x} with unknown type: %d\n", 1269 arg.addr, data, type); 1270 break; 1271 } 1272 } 1273 1274 static void 1275 rtw89_phy_cfg_bb_gain_op1db(struct rtw89_dev *rtwdev, 1276 union rtw89_phy_bb_gain_arg arg, u32 data) 1277 { 1278 struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax; 1279 u8 type = arg.type; 1280 u8 path = arg.path; 1281 u8 gband = arg.gain_band; 1282 int i; 1283 1284 switch (type) { 1285 case 0: 1286 for (i = 0; i < 4; i++, data >>= 8) 1287 gain->lna_op1db[gband][path][i] = data & 0xff; 1288 break; 1289 case 1: 1290 for (i = 4; i < 7; i++, data >>= 8) 1291 gain->lna_op1db[gband][path][i] = data & 0xff; 1292 break; 1293 case 2: 1294 for (i = 0; i < 4; i++, data >>= 8) 1295 gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1296 break; 1297 case 3: 1298 for (i = 4; i < 8; i++, data >>= 8) 1299 gain->tia_lna_op1db[gband][path][i] = data & 0xff; 1300 break; 1301 default: 1302 rtw89_warn(rtwdev, 1303 "bb gain op1db {0x%x:0x%x} with unknown type: %d\n", 1304 arg.addr, data, type); 1305 break; 1306 } 1307 } 1308 1309 static void rtw89_phy_config_bb_gain_ax(struct rtw89_dev *rtwdev, 1310 const struct rtw89_reg2_def *reg, 1311 enum rtw89_rf_path rf_path, 1312 void *extra_data) 1313 { 1314 const struct rtw89_chip_info *chip = rtwdev->chip; 1315 union rtw89_phy_bb_gain_arg arg = { .addr = reg->addr }; 1316 struct rtw89_efuse *efuse = &rtwdev->efuse; 1317 1318 if (arg.gain_band >= RTW89_BB_GAIN_BAND_NR) 1319 return; 1320 1321 if (arg.path >= chip->rf_path_num) 1322 return; 1323 1324 if (arg.addr >= 0xf9 && arg.addr <= 0xfe) { 1325 rtw89_warn(rtwdev, "bb gain table with flow ctrl\n"); 1326 return; 1327 } 1328 1329 switch (arg.cfg_type) { 1330 case 0: 1331 rtw89_phy_cfg_bb_gain_error(rtwdev, arg, reg->data); 1332 break; 1333 case 1: 1334 rtw89_phy_cfg_bb_rpl_ofst(rtwdev, arg, reg->data); 1335 break; 1336 case 2: 1337 rtw89_phy_cfg_bb_gain_bypass(rtwdev, arg, reg->data); 1338 break; 1339 case 3: 1340 rtw89_phy_cfg_bb_gain_op1db(rtwdev, arg, reg->data); 1341 break; 1342 case 4: 1343 /* This cfg_type is only used by rfe_type >= 50 with eFEM */ 1344 if (efuse->rfe_type < 50) 1345 break; 1346 fallthrough; 1347 default: 1348 rtw89_warn(rtwdev, 1349 "bb gain {0x%x:0x%x} with unknown cfg type: %d\n", 1350 arg.addr, reg->data, arg.cfg_type); 1351 break; 1352 } 1353 } 1354 1355 static void 1356 rtw89_phy_cofig_rf_reg_store(struct rtw89_dev *rtwdev, 1357 const struct rtw89_reg2_def *reg, 1358 enum rtw89_rf_path rf_path, 1359 struct rtw89_fw_h2c_rf_reg_info *info) 1360 { 1361 u16 idx = info->curr_idx % RTW89_H2C_RF_PAGE_SIZE; 1362 u8 page = info->curr_idx / RTW89_H2C_RF_PAGE_SIZE; 1363 1364 if (page >= RTW89_H2C_RF_PAGE_NUM) { 1365 rtw89_warn(rtwdev, "RF parameters exceed size. path=%d, idx=%d", 1366 rf_path, info->curr_idx); 1367 return; 1368 } 1369 1370 info->rtw89_phy_config_rf_h2c[page][idx] = 1371 cpu_to_le32((reg->addr << 20) | reg->data); 1372 info->curr_idx++; 1373 } 1374 1375 static int rtw89_phy_config_rf_reg_fw(struct rtw89_dev *rtwdev, 1376 struct rtw89_fw_h2c_rf_reg_info *info) 1377 { 1378 u16 remain = info->curr_idx; 1379 u16 len = 0; 1380 u8 i; 1381 int ret = 0; 1382 1383 if (remain > RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE) { 1384 rtw89_warn(rtwdev, 1385 "rf reg h2c total len %d larger than %d\n", 1386 remain, RTW89_H2C_RF_PAGE_NUM * RTW89_H2C_RF_PAGE_SIZE); 1387 ret = -EINVAL; 1388 goto out; 1389 } 1390 1391 for (i = 0; i < RTW89_H2C_RF_PAGE_NUM && remain; i++, remain -= len) { 1392 len = remain > RTW89_H2C_RF_PAGE_SIZE ? RTW89_H2C_RF_PAGE_SIZE : remain; 1393 ret = rtw89_fw_h2c_rf_reg(rtwdev, info, len * 4, i); 1394 if (ret) 1395 goto out; 1396 } 1397 out: 1398 info->curr_idx = 0; 1399 1400 return ret; 1401 } 1402 1403 static void rtw89_phy_config_rf_reg_noio(struct rtw89_dev *rtwdev, 1404 const struct rtw89_reg2_def *reg, 1405 enum rtw89_rf_path rf_path, 1406 void *extra_data) 1407 { 1408 u32 addr = reg->addr; 1409 1410 if (addr == 0xfe || addr == 0xfd || addr == 0xfc || addr == 0xfb || 1411 addr == 0xfa || addr == 0xf9) 1412 return; 1413 1414 if (rtw89_chip_rf_v1(rtwdev) && addr < 0x100) 1415 return; 1416 1417 rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1418 (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1419 } 1420 1421 static void rtw89_phy_config_rf_reg(struct rtw89_dev *rtwdev, 1422 const struct rtw89_reg2_def *reg, 1423 enum rtw89_rf_path rf_path, 1424 void *extra_data) 1425 { 1426 if (reg->addr == 0xfe) { 1427 mdelay(50); 1428 } else if (reg->addr == 0xfd) { 1429 mdelay(5); 1430 } else if (reg->addr == 0xfc) { 1431 mdelay(1); 1432 } else if (reg->addr == 0xfb) { 1433 udelay(50); 1434 } else if (reg->addr == 0xfa) { 1435 udelay(5); 1436 } else if (reg->addr == 0xf9) { 1437 udelay(1); 1438 } else { 1439 rtw89_write_rf(rtwdev, rf_path, reg->addr, 0xfffff, reg->data); 1440 rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1441 (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1442 } 1443 } 1444 1445 void rtw89_phy_config_rf_reg_v1(struct rtw89_dev *rtwdev, 1446 const struct rtw89_reg2_def *reg, 1447 enum rtw89_rf_path rf_path, 1448 void *extra_data) 1449 { 1450 rtw89_write_rf(rtwdev, rf_path, reg->addr, RFREG_MASK, reg->data); 1451 1452 if (reg->addr < 0x100) 1453 return; 1454 1455 rtw89_phy_cofig_rf_reg_store(rtwdev, reg, rf_path, 1456 (struct rtw89_fw_h2c_rf_reg_info *)extra_data); 1457 } 1458 EXPORT_SYMBOL(rtw89_phy_config_rf_reg_v1); 1459 1460 static int rtw89_phy_sel_headline(struct rtw89_dev *rtwdev, 1461 const struct rtw89_phy_table *table, 1462 u32 *headline_size, u32 *headline_idx, 1463 u8 rfe, u8 cv) 1464 { 1465 const struct rtw89_reg2_def *reg; 1466 u32 headline; 1467 u32 compare, target; 1468 u8 rfe_para, cv_para; 1469 u8 cv_max = 0; 1470 bool case_matched = false; 1471 u32 i; 1472 1473 for (i = 0; i < table->n_regs; i++) { 1474 reg = &table->regs[i]; 1475 headline = get_phy_headline(reg->addr); 1476 if (headline != PHY_HEADLINE_VALID) 1477 break; 1478 } 1479 *headline_size = i; 1480 if (*headline_size == 0) 1481 return 0; 1482 1483 /* case 1: RFE match, CV match */ 1484 compare = get_phy_compare(rfe, cv); 1485 for (i = 0; i < *headline_size; i++) { 1486 reg = &table->regs[i]; 1487 target = get_phy_target(reg->addr); 1488 if (target == compare) { 1489 *headline_idx = i; 1490 return 0; 1491 } 1492 } 1493 1494 /* case 2: RFE match, CV don't care */ 1495 compare = get_phy_compare(rfe, PHY_COND_DONT_CARE); 1496 for (i = 0; i < *headline_size; i++) { 1497 reg = &table->regs[i]; 1498 target = get_phy_target(reg->addr); 1499 if (target == compare) { 1500 *headline_idx = i; 1501 return 0; 1502 } 1503 } 1504 1505 /* case 3: RFE match, CV max in table */ 1506 for (i = 0; i < *headline_size; i++) { 1507 reg = &table->regs[i]; 1508 rfe_para = get_phy_cond_rfe(reg->addr); 1509 cv_para = get_phy_cond_cv(reg->addr); 1510 if (rfe_para == rfe) { 1511 if (cv_para >= cv_max) { 1512 cv_max = cv_para; 1513 *headline_idx = i; 1514 case_matched = true; 1515 } 1516 } 1517 } 1518 1519 if (case_matched) 1520 return 0; 1521 1522 /* case 4: RFE don't care, CV max in table */ 1523 for (i = 0; i < *headline_size; i++) { 1524 reg = &table->regs[i]; 1525 rfe_para = get_phy_cond_rfe(reg->addr); 1526 cv_para = get_phy_cond_cv(reg->addr); 1527 if (rfe_para == PHY_COND_DONT_CARE) { 1528 if (cv_para >= cv_max) { 1529 cv_max = cv_para; 1530 *headline_idx = i; 1531 case_matched = true; 1532 } 1533 } 1534 } 1535 1536 if (case_matched) 1537 return 0; 1538 1539 return -EINVAL; 1540 } 1541 1542 static void rtw89_phy_init_reg(struct rtw89_dev *rtwdev, 1543 const struct rtw89_phy_table *table, 1544 void (*config)(struct rtw89_dev *rtwdev, 1545 const struct rtw89_reg2_def *reg, 1546 enum rtw89_rf_path rf_path, 1547 void *data), 1548 void *extra_data) 1549 { 1550 const struct rtw89_reg2_def *reg; 1551 enum rtw89_rf_path rf_path = table->rf_path; 1552 u8 rfe = rtwdev->efuse.rfe_type; 1553 u8 cv = rtwdev->hal.cv; 1554 u32 i; 1555 u32 headline_size = 0, headline_idx = 0; 1556 u32 target = 0, cfg_target; 1557 u8 cond; 1558 bool is_matched = true; 1559 bool target_found = false; 1560 int ret; 1561 1562 ret = rtw89_phy_sel_headline(rtwdev, table, &headline_size, 1563 &headline_idx, rfe, cv); 1564 if (ret) { 1565 rtw89_err(rtwdev, "invalid PHY package: %d/%d\n", rfe, cv); 1566 return; 1567 } 1568 1569 cfg_target = get_phy_target(table->regs[headline_idx].addr); 1570 for (i = headline_size; i < table->n_regs; i++) { 1571 reg = &table->regs[i]; 1572 cond = get_phy_cond(reg->addr); 1573 switch (cond) { 1574 case PHY_COND_BRANCH_IF: 1575 case PHY_COND_BRANCH_ELIF: 1576 target = get_phy_target(reg->addr); 1577 break; 1578 case PHY_COND_BRANCH_ELSE: 1579 is_matched = false; 1580 if (!target_found) { 1581 rtw89_warn(rtwdev, "failed to load CR %x/%x\n", 1582 reg->addr, reg->data); 1583 return; 1584 } 1585 break; 1586 case PHY_COND_BRANCH_END: 1587 is_matched = true; 1588 target_found = false; 1589 break; 1590 case PHY_COND_CHECK: 1591 if (target_found) { 1592 is_matched = false; 1593 break; 1594 } 1595 1596 if (target == cfg_target) { 1597 is_matched = true; 1598 target_found = true; 1599 } else { 1600 is_matched = false; 1601 target_found = false; 1602 } 1603 break; 1604 default: 1605 if (is_matched) 1606 config(rtwdev, reg, rf_path, extra_data); 1607 break; 1608 } 1609 } 1610 } 1611 1612 void rtw89_phy_init_bb_reg(struct rtw89_dev *rtwdev) 1613 { 1614 struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info; 1615 const struct rtw89_chip_info *chip = rtwdev->chip; 1616 const struct rtw89_phy_table *bb_table; 1617 const struct rtw89_phy_table *bb_gain_table; 1618 1619 bb_table = elm_info->bb_tbl ? elm_info->bb_tbl : chip->bb_table; 1620 rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, NULL); 1621 if (rtwdev->dbcc_en) 1622 rtw89_phy_init_reg(rtwdev, bb_table, rtw89_phy_config_bb_reg, 1623 (void *)RTW89_PHY_1); 1624 rtw89_chip_init_txpwr_unit(rtwdev, RTW89_PHY_0); 1625 1626 bb_gain_table = elm_info->bb_gain ? elm_info->bb_gain : chip->bb_gain_table; 1627 if (bb_gain_table) 1628 rtw89_phy_init_reg(rtwdev, bb_gain_table, 1629 chip->phy_def->config_bb_gain, NULL); 1630 rtw89_phy_bb_reset(rtwdev, RTW89_PHY_0); 1631 } 1632 1633 static u32 rtw89_phy_nctl_poll(struct rtw89_dev *rtwdev) 1634 { 1635 rtw89_phy_write32(rtwdev, 0x8080, 0x4); 1636 udelay(1); 1637 return rtw89_phy_read32(rtwdev, 0x8080); 1638 } 1639 1640 void rtw89_phy_init_rf_reg(struct rtw89_dev *rtwdev, bool noio) 1641 { 1642 void (*config)(struct rtw89_dev *rtwdev, const struct rtw89_reg2_def *reg, 1643 enum rtw89_rf_path rf_path, void *data); 1644 struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info; 1645 const struct rtw89_chip_info *chip = rtwdev->chip; 1646 const struct rtw89_phy_table *rf_table; 1647 struct rtw89_fw_h2c_rf_reg_info *rf_reg_info; 1648 u8 path; 1649 1650 rf_reg_info = kzalloc(sizeof(*rf_reg_info), GFP_KERNEL); 1651 if (!rf_reg_info) 1652 return; 1653 1654 for (path = RF_PATH_A; path < chip->rf_path_num; path++) { 1655 rf_table = elm_info->rf_radio[path] ? 1656 elm_info->rf_radio[path] : chip->rf_table[path]; 1657 rf_reg_info->rf_path = rf_table->rf_path; 1658 if (noio) 1659 config = rtw89_phy_config_rf_reg_noio; 1660 else 1661 config = rf_table->config ? rf_table->config : 1662 rtw89_phy_config_rf_reg; 1663 rtw89_phy_init_reg(rtwdev, rf_table, config, (void *)rf_reg_info); 1664 if (rtw89_phy_config_rf_reg_fw(rtwdev, rf_reg_info)) 1665 rtw89_warn(rtwdev, "rf path %d reg h2c config failed\n", 1666 rf_reg_info->rf_path); 1667 } 1668 kfree(rf_reg_info); 1669 } 1670 1671 static void rtw89_phy_preinit_rf_nctl_ax(struct rtw89_dev *rtwdev) 1672 { 1673 const struct rtw89_chip_info *chip = rtwdev->chip; 1674 u32 val; 1675 int ret; 1676 1677 /* IQK/DPK clock & reset */ 1678 rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x3); 1679 rtw89_phy_write32_set(rtwdev, R_GNT_BT_WGT_EN, 0x1); 1680 rtw89_phy_write32_set(rtwdev, R_P0_PATH_RST, 0x8000000); 1681 if (chip->chip_id != RTL8851B) 1682 rtw89_phy_write32_set(rtwdev, R_P1_PATH_RST, 0x8000000); 1683 if (chip->chip_id == RTL8852B || chip->chip_id == RTL8852BT) 1684 rtw89_phy_write32_set(rtwdev, R_IOQ_IQK_DPK, 0x2); 1685 1686 /* check 0x8080 */ 1687 rtw89_phy_write32(rtwdev, R_NCTL_CFG, 0x8); 1688 1689 ret = read_poll_timeout(rtw89_phy_nctl_poll, val, val == 0x4, 10, 1690 1000, false, rtwdev); 1691 if (ret) 1692 rtw89_err(rtwdev, "failed to poll nctl block\n"); 1693 } 1694 1695 static void rtw89_phy_init_rf_nctl(struct rtw89_dev *rtwdev) 1696 { 1697 struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info; 1698 const struct rtw89_chip_info *chip = rtwdev->chip; 1699 const struct rtw89_phy_table *nctl_table; 1700 1701 rtw89_phy_preinit_rf_nctl(rtwdev); 1702 1703 nctl_table = elm_info->rf_nctl ? elm_info->rf_nctl : chip->nctl_table; 1704 rtw89_phy_init_reg(rtwdev, nctl_table, rtw89_phy_config_bb_reg, NULL); 1705 1706 if (chip->nctl_post_table) 1707 rtw89_rfk_parser(rtwdev, chip->nctl_post_table); 1708 } 1709 1710 static u32 rtw89_phy0_phy1_offset_ax(struct rtw89_dev *rtwdev, u32 addr) 1711 { 1712 u32 phy_page = addr >> 8; 1713 u32 ofst = 0; 1714 1715 switch (phy_page) { 1716 case 0x6: 1717 case 0x7: 1718 case 0x8: 1719 case 0x9: 1720 case 0xa: 1721 case 0xb: 1722 case 0xc: 1723 case 0xd: 1724 case 0x19: 1725 case 0x1a: 1726 case 0x1b: 1727 ofst = 0x2000; 1728 break; 1729 default: 1730 /* warning case */ 1731 ofst = 0; 1732 break; 1733 } 1734 1735 if (phy_page >= 0x40 && phy_page <= 0x4f) 1736 ofst = 0x2000; 1737 1738 return ofst; 1739 } 1740 1741 void rtw89_phy_write32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1742 u32 data, enum rtw89_phy_idx phy_idx) 1743 { 1744 if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 1745 addr += rtw89_phy0_phy1_offset(rtwdev, addr); 1746 rtw89_phy_write32_mask(rtwdev, addr, mask, data); 1747 } 1748 EXPORT_SYMBOL(rtw89_phy_write32_idx); 1749 1750 u32 rtw89_phy_read32_idx(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1751 enum rtw89_phy_idx phy_idx) 1752 { 1753 if (rtwdev->dbcc_en && phy_idx == RTW89_PHY_1) 1754 addr += rtw89_phy0_phy1_offset(rtwdev, addr); 1755 return rtw89_phy_read32_mask(rtwdev, addr, mask); 1756 } 1757 EXPORT_SYMBOL(rtw89_phy_read32_idx); 1758 1759 void rtw89_phy_set_phy_regs(struct rtw89_dev *rtwdev, u32 addr, u32 mask, 1760 u32 val) 1761 { 1762 rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_0); 1763 1764 if (!rtwdev->dbcc_en) 1765 return; 1766 1767 rtw89_phy_write32_idx(rtwdev, addr, mask, val, RTW89_PHY_1); 1768 } 1769 EXPORT_SYMBOL(rtw89_phy_set_phy_regs); 1770 1771 void rtw89_phy_write_reg3_tbl(struct rtw89_dev *rtwdev, 1772 const struct rtw89_phy_reg3_tbl *tbl) 1773 { 1774 const struct rtw89_reg3_def *reg3; 1775 int i; 1776 1777 for (i = 0; i < tbl->size; i++) { 1778 reg3 = &tbl->reg3[i]; 1779 rtw89_phy_write32_mask(rtwdev, reg3->addr, reg3->mask, reg3->data); 1780 } 1781 } 1782 EXPORT_SYMBOL(rtw89_phy_write_reg3_tbl); 1783 1784 static const u8 rtw89_rs_idx_num_ax[] = { 1785 [RTW89_RS_CCK] = RTW89_RATE_CCK_NUM, 1786 [RTW89_RS_OFDM] = RTW89_RATE_OFDM_NUM, 1787 [RTW89_RS_MCS] = RTW89_RATE_MCS_NUM_AX, 1788 [RTW89_RS_HEDCM] = RTW89_RATE_HEDCM_NUM, 1789 [RTW89_RS_OFFSET] = RTW89_RATE_OFFSET_NUM_AX, 1790 }; 1791 1792 static const u8 rtw89_rs_nss_num_ax[] = { 1793 [RTW89_RS_CCK] = 1, 1794 [RTW89_RS_OFDM] = 1, 1795 [RTW89_RS_MCS] = RTW89_NSS_NUM, 1796 [RTW89_RS_HEDCM] = RTW89_NSS_HEDCM_NUM, 1797 [RTW89_RS_OFFSET] = 1, 1798 }; 1799 1800 s8 *rtw89_phy_raw_byr_seek(struct rtw89_dev *rtwdev, 1801 struct rtw89_txpwr_byrate *head, 1802 const struct rtw89_rate_desc *desc) 1803 { 1804 switch (desc->rs) { 1805 case RTW89_RS_CCK: 1806 return &head->cck[desc->idx]; 1807 case RTW89_RS_OFDM: 1808 return &head->ofdm[desc->idx]; 1809 case RTW89_RS_MCS: 1810 return &head->mcs[desc->ofdma][desc->nss][desc->idx]; 1811 case RTW89_RS_HEDCM: 1812 return &head->hedcm[desc->ofdma][desc->nss][desc->idx]; 1813 case RTW89_RS_OFFSET: 1814 return &head->offset[desc->idx]; 1815 default: 1816 rtw89_warn(rtwdev, "unrecognized byr rs: %d\n", desc->rs); 1817 return &head->trap; 1818 } 1819 } 1820 1821 void rtw89_phy_load_txpwr_byrate(struct rtw89_dev *rtwdev, 1822 const struct rtw89_txpwr_table *tbl) 1823 { 1824 const struct rtw89_txpwr_byrate_cfg *cfg = tbl->data; 1825 const struct rtw89_txpwr_byrate_cfg *end = cfg + tbl->size; 1826 struct rtw89_txpwr_byrate *byr_head; 1827 struct rtw89_rate_desc desc = {}; 1828 s8 *byr; 1829 u32 data; 1830 u8 i; 1831 1832 for (; cfg < end; cfg++) { 1833 byr_head = &rtwdev->byr[cfg->band][0]; 1834 desc.rs = cfg->rs; 1835 desc.nss = cfg->nss; 1836 data = cfg->data; 1837 1838 for (i = 0; i < cfg->len; i++, data >>= 8) { 1839 desc.idx = cfg->shf + i; 1840 byr = rtw89_phy_raw_byr_seek(rtwdev, byr_head, &desc); 1841 *byr = data & 0xff; 1842 } 1843 } 1844 } 1845 EXPORT_SYMBOL(rtw89_phy_load_txpwr_byrate); 1846 1847 static s8 rtw89_phy_txpwr_rf_to_mac(struct rtw89_dev *rtwdev, s8 txpwr_rf) 1848 { 1849 const struct rtw89_chip_info *chip = rtwdev->chip; 1850 1851 return txpwr_rf >> (chip->txpwr_factor_rf - chip->txpwr_factor_mac); 1852 } 1853 1854 static s8 rtw89_phy_txpwr_dbm_to_mac(struct rtw89_dev *rtwdev, s8 dbm) 1855 { 1856 const struct rtw89_chip_info *chip = rtwdev->chip; 1857 1858 return clamp_t(s16, dbm << chip->txpwr_factor_mac, -64, 63); 1859 } 1860 1861 static s8 rtw89_phy_txpwr_dbm_without_tolerance(s8 dbm) 1862 { 1863 const u8 tssi_deviation_point = 0; 1864 const u8 tssi_max_deviation = 2; 1865 1866 if (dbm <= tssi_deviation_point) 1867 dbm -= tssi_max_deviation; 1868 1869 return dbm; 1870 } 1871 1872 static s8 rtw89_phy_get_tpe_constraint(struct rtw89_dev *rtwdev, u8 band) 1873 { 1874 struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory; 1875 const struct rtw89_reg_6ghz_tpe *tpe = ®ulatory->reg_6ghz_tpe; 1876 s8 cstr = S8_MAX; 1877 1878 if (band == RTW89_BAND_6G && tpe->valid) 1879 cstr = rtw89_phy_txpwr_dbm_without_tolerance(tpe->constraint); 1880 1881 return rtw89_phy_txpwr_dbm_to_mac(rtwdev, cstr); 1882 } 1883 1884 s8 rtw89_phy_read_txpwr_byrate(struct rtw89_dev *rtwdev, u8 band, u8 bw, 1885 const struct rtw89_rate_desc *rate_desc) 1886 { 1887 struct rtw89_txpwr_byrate *byr_head; 1888 s8 *byr; 1889 1890 if (rate_desc->rs == RTW89_RS_CCK) 1891 band = RTW89_BAND_2G; 1892 1893 byr_head = &rtwdev->byr[band][bw]; 1894 byr = rtw89_phy_raw_byr_seek(rtwdev, byr_head, rate_desc); 1895 1896 return rtw89_phy_txpwr_rf_to_mac(rtwdev, *byr); 1897 } 1898 1899 static u8 rtw89_channel_6g_to_idx(struct rtw89_dev *rtwdev, u8 channel_6g) 1900 { 1901 switch (channel_6g) { 1902 case 1 ... 29: 1903 return (channel_6g - 1) / 2; 1904 case 33 ... 61: 1905 return (channel_6g - 3) / 2; 1906 case 65 ... 93: 1907 return (channel_6g - 5) / 2; 1908 case 97 ... 125: 1909 return (channel_6g - 7) / 2; 1910 case 129 ... 157: 1911 return (channel_6g - 9) / 2; 1912 case 161 ... 189: 1913 return (channel_6g - 11) / 2; 1914 case 193 ... 221: 1915 return (channel_6g - 13) / 2; 1916 case 225 ... 253: 1917 return (channel_6g - 15) / 2; 1918 default: 1919 rtw89_warn(rtwdev, "unknown 6g channel: %d\n", channel_6g); 1920 return 0; 1921 } 1922 } 1923 1924 static u8 rtw89_channel_to_idx(struct rtw89_dev *rtwdev, u8 band, u8 channel) 1925 { 1926 if (band == RTW89_BAND_6G) 1927 return rtw89_channel_6g_to_idx(rtwdev, channel); 1928 1929 switch (channel) { 1930 case 1 ... 14: 1931 return channel - 1; 1932 case 36 ... 64: 1933 return (channel - 36) / 2; 1934 case 100 ... 144: 1935 return ((channel - 100) / 2) + 15; 1936 case 149 ... 177: 1937 return ((channel - 149) / 2) + 38; 1938 default: 1939 rtw89_warn(rtwdev, "unknown channel: %d\n", channel); 1940 return 0; 1941 } 1942 } 1943 1944 s8 rtw89_phy_read_txpwr_limit(struct rtw89_dev *rtwdev, u8 band, 1945 u8 bw, u8 ntx, u8 rs, u8 bf, u8 ch) 1946 { 1947 const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms; 1948 const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz; 1949 const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz; 1950 const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz; 1951 struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory; 1952 enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 1953 u32 freq = ieee80211_channel_to_frequency(ch, nl_band); 1954 u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 1955 u8 regd = rtw89_regd_get(rtwdev, band); 1956 u8 reg6 = regulatory->reg_6ghz_power; 1957 s8 lmt = 0, sar; 1958 s8 cstr; 1959 1960 switch (band) { 1961 case RTW89_BAND_2G: 1962 lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx]; 1963 if (lmt) 1964 break; 1965 1966 lmt = (*rule_2ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx]; 1967 break; 1968 case RTW89_BAND_5G: 1969 lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][regd][ch_idx]; 1970 if (lmt) 1971 break; 1972 1973 lmt = (*rule_5ghz->lmt)[bw][ntx][rs][bf][RTW89_WW][ch_idx]; 1974 break; 1975 case RTW89_BAND_6G: 1976 lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][regd][reg6][ch_idx]; 1977 if (lmt) 1978 break; 1979 1980 lmt = (*rule_6ghz->lmt)[bw][ntx][rs][bf][RTW89_WW] 1981 [RTW89_REG_6GHZ_POWER_DFLT] 1982 [ch_idx]; 1983 break; 1984 default: 1985 rtw89_warn(rtwdev, "unknown band type: %d\n", band); 1986 return 0; 1987 } 1988 1989 lmt = rtw89_phy_txpwr_rf_to_mac(rtwdev, lmt); 1990 sar = rtw89_query_sar(rtwdev, freq); 1991 cstr = rtw89_phy_get_tpe_constraint(rtwdev, band); 1992 1993 return min3(lmt, sar, cstr); 1994 } 1995 EXPORT_SYMBOL(rtw89_phy_read_txpwr_limit); 1996 1997 #define __fill_txpwr_limit_nonbf_bf(ptr, band, bw, ntx, rs, ch) \ 1998 do { \ 1999 u8 __i; \ 2000 for (__i = 0; __i < RTW89_BF_NUM; __i++) \ 2001 ptr[__i] = rtw89_phy_read_txpwr_limit(rtwdev, \ 2002 band, \ 2003 bw, ntx, \ 2004 rs, __i, \ 2005 (ch)); \ 2006 } while (0) 2007 2008 static void rtw89_phy_fill_txpwr_limit_20m_ax(struct rtw89_dev *rtwdev, 2009 struct rtw89_txpwr_limit_ax *lmt, 2010 u8 band, u8 ntx, u8 ch) 2011 { 2012 __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 2013 ntx, RTW89_RS_CCK, ch); 2014 __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 2015 ntx, RTW89_RS_CCK, ch); 2016 __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 2017 ntx, RTW89_RS_OFDM, ch); 2018 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 2019 RTW89_CHANNEL_WIDTH_20, 2020 ntx, RTW89_RS_MCS, ch); 2021 } 2022 2023 static void rtw89_phy_fill_txpwr_limit_40m_ax(struct rtw89_dev *rtwdev, 2024 struct rtw89_txpwr_limit_ax *lmt, 2025 u8 band, u8 ntx, u8 ch, u8 pri_ch) 2026 { 2027 __fill_txpwr_limit_nonbf_bf(lmt->cck_20m, band, RTW89_CHANNEL_WIDTH_20, 2028 ntx, RTW89_RS_CCK, ch - 2); 2029 __fill_txpwr_limit_nonbf_bf(lmt->cck_40m, band, RTW89_CHANNEL_WIDTH_40, 2030 ntx, RTW89_RS_CCK, ch); 2031 __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 2032 ntx, RTW89_RS_OFDM, pri_ch); 2033 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 2034 RTW89_CHANNEL_WIDTH_20, 2035 ntx, RTW89_RS_MCS, ch - 2); 2036 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 2037 RTW89_CHANNEL_WIDTH_20, 2038 ntx, RTW89_RS_MCS, ch + 2); 2039 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 2040 RTW89_CHANNEL_WIDTH_40, 2041 ntx, RTW89_RS_MCS, ch); 2042 } 2043 2044 static void rtw89_phy_fill_txpwr_limit_80m_ax(struct rtw89_dev *rtwdev, 2045 struct rtw89_txpwr_limit_ax *lmt, 2046 u8 band, u8 ntx, u8 ch, u8 pri_ch) 2047 { 2048 s8 val_0p5_n[RTW89_BF_NUM]; 2049 s8 val_0p5_p[RTW89_BF_NUM]; 2050 u8 i; 2051 2052 __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 2053 ntx, RTW89_RS_OFDM, pri_ch); 2054 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 2055 RTW89_CHANNEL_WIDTH_20, 2056 ntx, RTW89_RS_MCS, ch - 6); 2057 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 2058 RTW89_CHANNEL_WIDTH_20, 2059 ntx, RTW89_RS_MCS, ch - 2); 2060 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 2061 RTW89_CHANNEL_WIDTH_20, 2062 ntx, RTW89_RS_MCS, ch + 2); 2063 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 2064 RTW89_CHANNEL_WIDTH_20, 2065 ntx, RTW89_RS_MCS, ch + 6); 2066 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 2067 RTW89_CHANNEL_WIDTH_40, 2068 ntx, RTW89_RS_MCS, ch - 4); 2069 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 2070 RTW89_CHANNEL_WIDTH_40, 2071 ntx, RTW89_RS_MCS, ch + 4); 2072 __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 2073 RTW89_CHANNEL_WIDTH_80, 2074 ntx, RTW89_RS_MCS, ch); 2075 2076 __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 2077 ntx, RTW89_RS_MCS, ch - 4); 2078 __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 2079 ntx, RTW89_RS_MCS, ch + 4); 2080 2081 for (i = 0; i < RTW89_BF_NUM; i++) 2082 lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 2083 } 2084 2085 static void rtw89_phy_fill_txpwr_limit_160m_ax(struct rtw89_dev *rtwdev, 2086 struct rtw89_txpwr_limit_ax *lmt, 2087 u8 band, u8 ntx, u8 ch, u8 pri_ch) 2088 { 2089 s8 val_0p5_n[RTW89_BF_NUM]; 2090 s8 val_0p5_p[RTW89_BF_NUM]; 2091 s8 val_2p5_n[RTW89_BF_NUM]; 2092 s8 val_2p5_p[RTW89_BF_NUM]; 2093 u8 i; 2094 2095 /* fill ofdm section */ 2096 __fill_txpwr_limit_nonbf_bf(lmt->ofdm, band, RTW89_CHANNEL_WIDTH_20, 2097 ntx, RTW89_RS_OFDM, pri_ch); 2098 2099 /* fill mcs 20m section */ 2100 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[0], band, 2101 RTW89_CHANNEL_WIDTH_20, 2102 ntx, RTW89_RS_MCS, ch - 14); 2103 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[1], band, 2104 RTW89_CHANNEL_WIDTH_20, 2105 ntx, RTW89_RS_MCS, ch - 10); 2106 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[2], band, 2107 RTW89_CHANNEL_WIDTH_20, 2108 ntx, RTW89_RS_MCS, ch - 6); 2109 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[3], band, 2110 RTW89_CHANNEL_WIDTH_20, 2111 ntx, RTW89_RS_MCS, ch - 2); 2112 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[4], band, 2113 RTW89_CHANNEL_WIDTH_20, 2114 ntx, RTW89_RS_MCS, ch + 2); 2115 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[5], band, 2116 RTW89_CHANNEL_WIDTH_20, 2117 ntx, RTW89_RS_MCS, ch + 6); 2118 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[6], band, 2119 RTW89_CHANNEL_WIDTH_20, 2120 ntx, RTW89_RS_MCS, ch + 10); 2121 __fill_txpwr_limit_nonbf_bf(lmt->mcs_20m[7], band, 2122 RTW89_CHANNEL_WIDTH_20, 2123 ntx, RTW89_RS_MCS, ch + 14); 2124 2125 /* fill mcs 40m section */ 2126 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[0], band, 2127 RTW89_CHANNEL_WIDTH_40, 2128 ntx, RTW89_RS_MCS, ch - 12); 2129 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[1], band, 2130 RTW89_CHANNEL_WIDTH_40, 2131 ntx, RTW89_RS_MCS, ch - 4); 2132 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[2], band, 2133 RTW89_CHANNEL_WIDTH_40, 2134 ntx, RTW89_RS_MCS, ch + 4); 2135 __fill_txpwr_limit_nonbf_bf(lmt->mcs_40m[3], band, 2136 RTW89_CHANNEL_WIDTH_40, 2137 ntx, RTW89_RS_MCS, ch + 12); 2138 2139 /* fill mcs 80m section */ 2140 __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[0], band, 2141 RTW89_CHANNEL_WIDTH_80, 2142 ntx, RTW89_RS_MCS, ch - 8); 2143 __fill_txpwr_limit_nonbf_bf(lmt->mcs_80m[1], band, 2144 RTW89_CHANNEL_WIDTH_80, 2145 ntx, RTW89_RS_MCS, ch + 8); 2146 2147 /* fill mcs 160m section */ 2148 __fill_txpwr_limit_nonbf_bf(lmt->mcs_160m, band, 2149 RTW89_CHANNEL_WIDTH_160, 2150 ntx, RTW89_RS_MCS, ch); 2151 2152 /* fill mcs 40m 0p5 section */ 2153 __fill_txpwr_limit_nonbf_bf(val_0p5_n, band, RTW89_CHANNEL_WIDTH_40, 2154 ntx, RTW89_RS_MCS, ch - 4); 2155 __fill_txpwr_limit_nonbf_bf(val_0p5_p, band, RTW89_CHANNEL_WIDTH_40, 2156 ntx, RTW89_RS_MCS, ch + 4); 2157 2158 for (i = 0; i < RTW89_BF_NUM; i++) 2159 lmt->mcs_40m_0p5[i] = min_t(s8, val_0p5_n[i], val_0p5_p[i]); 2160 2161 /* fill mcs 40m 2p5 section */ 2162 __fill_txpwr_limit_nonbf_bf(val_2p5_n, band, RTW89_CHANNEL_WIDTH_40, 2163 ntx, RTW89_RS_MCS, ch - 8); 2164 __fill_txpwr_limit_nonbf_bf(val_2p5_p, band, RTW89_CHANNEL_WIDTH_40, 2165 ntx, RTW89_RS_MCS, ch + 8); 2166 2167 for (i = 0; i < RTW89_BF_NUM; i++) 2168 lmt->mcs_40m_2p5[i] = min_t(s8, val_2p5_n[i], val_2p5_p[i]); 2169 } 2170 2171 static 2172 void rtw89_phy_fill_txpwr_limit_ax(struct rtw89_dev *rtwdev, 2173 const struct rtw89_chan *chan, 2174 struct rtw89_txpwr_limit_ax *lmt, 2175 u8 ntx) 2176 { 2177 u8 band = chan->band_type; 2178 u8 pri_ch = chan->primary_channel; 2179 u8 ch = chan->channel; 2180 u8 bw = chan->band_width; 2181 2182 memset(lmt, 0, sizeof(*lmt)); 2183 2184 switch (bw) { 2185 case RTW89_CHANNEL_WIDTH_20: 2186 rtw89_phy_fill_txpwr_limit_20m_ax(rtwdev, lmt, band, ntx, ch); 2187 break; 2188 case RTW89_CHANNEL_WIDTH_40: 2189 rtw89_phy_fill_txpwr_limit_40m_ax(rtwdev, lmt, band, ntx, ch, 2190 pri_ch); 2191 break; 2192 case RTW89_CHANNEL_WIDTH_80: 2193 rtw89_phy_fill_txpwr_limit_80m_ax(rtwdev, lmt, band, ntx, ch, 2194 pri_ch); 2195 break; 2196 case RTW89_CHANNEL_WIDTH_160: 2197 rtw89_phy_fill_txpwr_limit_160m_ax(rtwdev, lmt, band, ntx, ch, 2198 pri_ch); 2199 break; 2200 } 2201 } 2202 2203 s8 rtw89_phy_read_txpwr_limit_ru(struct rtw89_dev *rtwdev, u8 band, 2204 u8 ru, u8 ntx, u8 ch) 2205 { 2206 const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms; 2207 const struct rtw89_txpwr_rule_2ghz *rule_2ghz = &rfe_parms->rule_2ghz; 2208 const struct rtw89_txpwr_rule_5ghz *rule_5ghz = &rfe_parms->rule_5ghz; 2209 const struct rtw89_txpwr_rule_6ghz *rule_6ghz = &rfe_parms->rule_6ghz; 2210 struct rtw89_regulatory_info *regulatory = &rtwdev->regulatory; 2211 enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band); 2212 u32 freq = ieee80211_channel_to_frequency(ch, nl_band); 2213 u8 ch_idx = rtw89_channel_to_idx(rtwdev, band, ch); 2214 u8 regd = rtw89_regd_get(rtwdev, band); 2215 u8 reg6 = regulatory->reg_6ghz_power; 2216 s8 lmt_ru = 0, sar; 2217 s8 cstr; 2218 2219 switch (band) { 2220 case RTW89_BAND_2G: 2221 lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][regd][ch_idx]; 2222 if (lmt_ru) 2223 break; 2224 2225 lmt_ru = (*rule_2ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx]; 2226 break; 2227 case RTW89_BAND_5G: 2228 lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][regd][ch_idx]; 2229 if (lmt_ru) 2230 break; 2231 2232 lmt_ru = (*rule_5ghz->lmt_ru)[ru][ntx][RTW89_WW][ch_idx]; 2233 break; 2234 case RTW89_BAND_6G: 2235 lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][regd][reg6][ch_idx]; 2236 if (lmt_ru) 2237 break; 2238 2239 lmt_ru = (*rule_6ghz->lmt_ru)[ru][ntx][RTW89_WW] 2240 [RTW89_REG_6GHZ_POWER_DFLT] 2241 [ch_idx]; 2242 break; 2243 default: 2244 rtw89_warn(rtwdev, "unknown band type: %d\n", band); 2245 return 0; 2246 } 2247 2248 lmt_ru = rtw89_phy_txpwr_rf_to_mac(rtwdev, lmt_ru); 2249 sar = rtw89_query_sar(rtwdev, freq); 2250 cstr = rtw89_phy_get_tpe_constraint(rtwdev, band); 2251 2252 return min3(lmt_ru, sar, cstr); 2253 } 2254 2255 static void 2256 rtw89_phy_fill_txpwr_limit_ru_20m_ax(struct rtw89_dev *rtwdev, 2257 struct rtw89_txpwr_limit_ru_ax *lmt_ru, 2258 u8 band, u8 ntx, u8 ch) 2259 { 2260 lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2261 RTW89_RU26, 2262 ntx, ch); 2263 lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2264 RTW89_RU52, 2265 ntx, ch); 2266 lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2267 RTW89_RU106, 2268 ntx, ch); 2269 } 2270 2271 static void 2272 rtw89_phy_fill_txpwr_limit_ru_40m_ax(struct rtw89_dev *rtwdev, 2273 struct rtw89_txpwr_limit_ru_ax *lmt_ru, 2274 u8 band, u8 ntx, u8 ch) 2275 { 2276 lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2277 RTW89_RU26, 2278 ntx, ch - 2); 2279 lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2280 RTW89_RU26, 2281 ntx, ch + 2); 2282 lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2283 RTW89_RU52, 2284 ntx, ch - 2); 2285 lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2286 RTW89_RU52, 2287 ntx, ch + 2); 2288 lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2289 RTW89_RU106, 2290 ntx, ch - 2); 2291 lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2292 RTW89_RU106, 2293 ntx, ch + 2); 2294 } 2295 2296 static void 2297 rtw89_phy_fill_txpwr_limit_ru_80m_ax(struct rtw89_dev *rtwdev, 2298 struct rtw89_txpwr_limit_ru_ax *lmt_ru, 2299 u8 band, u8 ntx, u8 ch) 2300 { 2301 lmt_ru->ru26[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2302 RTW89_RU26, 2303 ntx, ch - 6); 2304 lmt_ru->ru26[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2305 RTW89_RU26, 2306 ntx, ch - 2); 2307 lmt_ru->ru26[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2308 RTW89_RU26, 2309 ntx, ch + 2); 2310 lmt_ru->ru26[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2311 RTW89_RU26, 2312 ntx, ch + 6); 2313 lmt_ru->ru52[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2314 RTW89_RU52, 2315 ntx, ch - 6); 2316 lmt_ru->ru52[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2317 RTW89_RU52, 2318 ntx, ch - 2); 2319 lmt_ru->ru52[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2320 RTW89_RU52, 2321 ntx, ch + 2); 2322 lmt_ru->ru52[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2323 RTW89_RU52, 2324 ntx, ch + 6); 2325 lmt_ru->ru106[0] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2326 RTW89_RU106, 2327 ntx, ch - 6); 2328 lmt_ru->ru106[1] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2329 RTW89_RU106, 2330 ntx, ch - 2); 2331 lmt_ru->ru106[2] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2332 RTW89_RU106, 2333 ntx, ch + 2); 2334 lmt_ru->ru106[3] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2335 RTW89_RU106, 2336 ntx, ch + 6); 2337 } 2338 2339 static void 2340 rtw89_phy_fill_txpwr_limit_ru_160m_ax(struct rtw89_dev *rtwdev, 2341 struct rtw89_txpwr_limit_ru_ax *lmt_ru, 2342 u8 band, u8 ntx, u8 ch) 2343 { 2344 static const int ofst[] = { -14, -10, -6, -2, 2, 6, 10, 14 }; 2345 int i; 2346 2347 static_assert(ARRAY_SIZE(ofst) == RTW89_RU_SEC_NUM_AX); 2348 for (i = 0; i < RTW89_RU_SEC_NUM_AX; i++) { 2349 lmt_ru->ru26[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2350 RTW89_RU26, 2351 ntx, 2352 ch + ofst[i]); 2353 lmt_ru->ru52[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2354 RTW89_RU52, 2355 ntx, 2356 ch + ofst[i]); 2357 lmt_ru->ru106[i] = rtw89_phy_read_txpwr_limit_ru(rtwdev, band, 2358 RTW89_RU106, 2359 ntx, 2360 ch + ofst[i]); 2361 } 2362 } 2363 2364 static 2365 void rtw89_phy_fill_txpwr_limit_ru_ax(struct rtw89_dev *rtwdev, 2366 const struct rtw89_chan *chan, 2367 struct rtw89_txpwr_limit_ru_ax *lmt_ru, 2368 u8 ntx) 2369 { 2370 u8 band = chan->band_type; 2371 u8 ch = chan->channel; 2372 u8 bw = chan->band_width; 2373 2374 memset(lmt_ru, 0, sizeof(*lmt_ru)); 2375 2376 switch (bw) { 2377 case RTW89_CHANNEL_WIDTH_20: 2378 rtw89_phy_fill_txpwr_limit_ru_20m_ax(rtwdev, lmt_ru, band, ntx, 2379 ch); 2380 break; 2381 case RTW89_CHANNEL_WIDTH_40: 2382 rtw89_phy_fill_txpwr_limit_ru_40m_ax(rtwdev, lmt_ru, band, ntx, 2383 ch); 2384 break; 2385 case RTW89_CHANNEL_WIDTH_80: 2386 rtw89_phy_fill_txpwr_limit_ru_80m_ax(rtwdev, lmt_ru, band, ntx, 2387 ch); 2388 break; 2389 case RTW89_CHANNEL_WIDTH_160: 2390 rtw89_phy_fill_txpwr_limit_ru_160m_ax(rtwdev, lmt_ru, band, ntx, 2391 ch); 2392 break; 2393 } 2394 } 2395 2396 static void rtw89_phy_set_txpwr_byrate_ax(struct rtw89_dev *rtwdev, 2397 const struct rtw89_chan *chan, 2398 enum rtw89_phy_idx phy_idx) 2399 { 2400 u8 max_nss_num = rtwdev->chip->rf_path_num; 2401 static const u8 rs[] = { 2402 RTW89_RS_CCK, 2403 RTW89_RS_OFDM, 2404 RTW89_RS_MCS, 2405 RTW89_RS_HEDCM, 2406 }; 2407 struct rtw89_rate_desc cur = {}; 2408 u8 band = chan->band_type; 2409 u8 ch = chan->channel; 2410 u32 addr, val; 2411 s8 v[4] = {}; 2412 u8 i; 2413 2414 rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 2415 "[TXPWR] set txpwr byrate with ch=%d\n", ch); 2416 2417 BUILD_BUG_ON(rtw89_rs_idx_num_ax[RTW89_RS_CCK] % 4); 2418 BUILD_BUG_ON(rtw89_rs_idx_num_ax[RTW89_RS_OFDM] % 4); 2419 BUILD_BUG_ON(rtw89_rs_idx_num_ax[RTW89_RS_MCS] % 4); 2420 BUILD_BUG_ON(rtw89_rs_idx_num_ax[RTW89_RS_HEDCM] % 4); 2421 2422 addr = R_AX_PWR_BY_RATE; 2423 for (cur.nss = 0; cur.nss < max_nss_num; cur.nss++) { 2424 for (i = 0; i < ARRAY_SIZE(rs); i++) { 2425 if (cur.nss >= rtw89_rs_nss_num_ax[rs[i]]) 2426 continue; 2427 2428 cur.rs = rs[i]; 2429 for (cur.idx = 0; cur.idx < rtw89_rs_idx_num_ax[rs[i]]; 2430 cur.idx++) { 2431 v[cur.idx % 4] = 2432 rtw89_phy_read_txpwr_byrate(rtwdev, 2433 band, 0, 2434 &cur); 2435 2436 if ((cur.idx + 1) % 4) 2437 continue; 2438 2439 val = FIELD_PREP(GENMASK(7, 0), v[0]) | 2440 FIELD_PREP(GENMASK(15, 8), v[1]) | 2441 FIELD_PREP(GENMASK(23, 16), v[2]) | 2442 FIELD_PREP(GENMASK(31, 24), v[3]); 2443 2444 rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, 2445 val); 2446 addr += 4; 2447 } 2448 } 2449 } 2450 } 2451 2452 static 2453 void rtw89_phy_set_txpwr_offset_ax(struct rtw89_dev *rtwdev, 2454 const struct rtw89_chan *chan, 2455 enum rtw89_phy_idx phy_idx) 2456 { 2457 struct rtw89_rate_desc desc = { 2458 .nss = RTW89_NSS_1, 2459 .rs = RTW89_RS_OFFSET, 2460 }; 2461 u8 band = chan->band_type; 2462 s8 v[RTW89_RATE_OFFSET_NUM_AX] = {}; 2463 u32 val; 2464 2465 rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr offset\n"); 2466 2467 for (desc.idx = 0; desc.idx < RTW89_RATE_OFFSET_NUM_AX; desc.idx++) 2468 v[desc.idx] = rtw89_phy_read_txpwr_byrate(rtwdev, band, 0, &desc); 2469 2470 BUILD_BUG_ON(RTW89_RATE_OFFSET_NUM_AX != 5); 2471 val = FIELD_PREP(GENMASK(3, 0), v[0]) | 2472 FIELD_PREP(GENMASK(7, 4), v[1]) | 2473 FIELD_PREP(GENMASK(11, 8), v[2]) | 2474 FIELD_PREP(GENMASK(15, 12), v[3]) | 2475 FIELD_PREP(GENMASK(19, 16), v[4]); 2476 2477 rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_OFST_CTRL, 2478 GENMASK(19, 0), val); 2479 } 2480 2481 static void rtw89_phy_set_txpwr_limit_ax(struct rtw89_dev *rtwdev, 2482 const struct rtw89_chan *chan, 2483 enum rtw89_phy_idx phy_idx) 2484 { 2485 u8 max_ntx_num = rtwdev->chip->rf_path_num; 2486 struct rtw89_txpwr_limit_ax lmt; 2487 u8 ch = chan->channel; 2488 u8 bw = chan->band_width; 2489 const s8 *ptr; 2490 u32 addr, val; 2491 u8 i, j; 2492 2493 rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 2494 "[TXPWR] set txpwr limit with ch=%d bw=%d\n", ch, bw); 2495 2496 BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ax) != 2497 RTW89_TXPWR_LMT_PAGE_SIZE_AX); 2498 2499 addr = R_AX_PWR_LMT; 2500 for (i = 0; i < max_ntx_num; i++) { 2501 rtw89_phy_fill_txpwr_limit_ax(rtwdev, chan, &lmt, i); 2502 2503 ptr = (s8 *)&lmt; 2504 for (j = 0; j < RTW89_TXPWR_LMT_PAGE_SIZE_AX; 2505 j += 4, addr += 4, ptr += 4) { 2506 val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 2507 FIELD_PREP(GENMASK(15, 8), ptr[1]) | 2508 FIELD_PREP(GENMASK(23, 16), ptr[2]) | 2509 FIELD_PREP(GENMASK(31, 24), ptr[3]); 2510 2511 rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 2512 } 2513 } 2514 } 2515 2516 static void rtw89_phy_set_txpwr_limit_ru_ax(struct rtw89_dev *rtwdev, 2517 const struct rtw89_chan *chan, 2518 enum rtw89_phy_idx phy_idx) 2519 { 2520 u8 max_ntx_num = rtwdev->chip->rf_path_num; 2521 struct rtw89_txpwr_limit_ru_ax lmt_ru; 2522 u8 ch = chan->channel; 2523 u8 bw = chan->band_width; 2524 const s8 *ptr; 2525 u32 addr, val; 2526 u8 i, j; 2527 2528 rtw89_debug(rtwdev, RTW89_DBG_TXPWR, 2529 "[TXPWR] set txpwr limit ru with ch=%d bw=%d\n", ch, bw); 2530 2531 BUILD_BUG_ON(sizeof(struct rtw89_txpwr_limit_ru_ax) != 2532 RTW89_TXPWR_LMT_RU_PAGE_SIZE_AX); 2533 2534 addr = R_AX_PWR_RU_LMT; 2535 for (i = 0; i < max_ntx_num; i++) { 2536 rtw89_phy_fill_txpwr_limit_ru_ax(rtwdev, chan, &lmt_ru, i); 2537 2538 ptr = (s8 *)&lmt_ru; 2539 for (j = 0; j < RTW89_TXPWR_LMT_RU_PAGE_SIZE_AX; 2540 j += 4, addr += 4, ptr += 4) { 2541 val = FIELD_PREP(GENMASK(7, 0), ptr[0]) | 2542 FIELD_PREP(GENMASK(15, 8), ptr[1]) | 2543 FIELD_PREP(GENMASK(23, 16), ptr[2]) | 2544 FIELD_PREP(GENMASK(31, 24), ptr[3]); 2545 2546 rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, val); 2547 } 2548 } 2549 } 2550 2551 struct rtw89_phy_iter_ra_data { 2552 struct rtw89_dev *rtwdev; 2553 struct sk_buff *c2h; 2554 }; 2555 2556 static void rtw89_phy_c2h_ra_rpt_iter(void *data, struct ieee80211_sta *sta) 2557 { 2558 struct rtw89_phy_iter_ra_data *ra_data = (struct rtw89_phy_iter_ra_data *)data; 2559 struct rtw89_dev *rtwdev = ra_data->rtwdev; 2560 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 2561 const struct rtw89_c2h_ra_rpt *c2h = 2562 (const struct rtw89_c2h_ra_rpt *)ra_data->c2h->data; 2563 struct rtw89_ra_report *ra_report = &rtwsta->ra_report; 2564 const struct rtw89_chip_info *chip = rtwdev->chip; 2565 bool format_v1 = chip->chip_gen == RTW89_CHIP_BE; 2566 u8 mode, rate, bw, giltf, mac_id; 2567 u16 legacy_bitrate; 2568 bool valid; 2569 u8 mcs = 0; 2570 u8 t; 2571 2572 mac_id = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MACID); 2573 if (mac_id != rtwsta->mac_id) 2574 return; 2575 2576 rate = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MCSNSS); 2577 bw = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW); 2578 giltf = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_GILTF); 2579 mode = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL); 2580 2581 if (format_v1) { 2582 t = le32_get_bits(c2h->w2, RTW89_C2H_RA_RPT_W2_MCSNSS_B7); 2583 rate |= u8_encode_bits(t, BIT(7)); 2584 t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_BW_B2); 2585 bw |= u8_encode_bits(t, BIT(2)); 2586 t = le32_get_bits(c2h->w3, RTW89_C2H_RA_RPT_W3_MD_SEL_B2); 2587 mode |= u8_encode_bits(t, BIT(2)); 2588 } 2589 2590 if (mode == RTW89_RA_RPT_MODE_LEGACY) { 2591 valid = rtw89_ra_report_to_bitrate(rtwdev, rate, &legacy_bitrate); 2592 if (!valid) 2593 return; 2594 } 2595 2596 memset(&ra_report->txrate, 0, sizeof(ra_report->txrate)); 2597 2598 switch (mode) { 2599 case RTW89_RA_RPT_MODE_LEGACY: 2600 ra_report->txrate.legacy = legacy_bitrate; 2601 break; 2602 case RTW89_RA_RPT_MODE_HT: 2603 ra_report->txrate.flags |= RATE_INFO_FLAGS_MCS; 2604 if (RTW89_CHK_FW_FEATURE(OLD_HT_RA_FORMAT, &rtwdev->fw)) 2605 rate = RTW89_MK_HT_RATE(FIELD_GET(RTW89_RA_RATE_MASK_NSS, rate), 2606 FIELD_GET(RTW89_RA_RATE_MASK_MCS, rate)); 2607 else 2608 rate = FIELD_GET(RTW89_RA_RATE_MASK_HT_MCS, rate); 2609 ra_report->txrate.mcs = rate; 2610 if (giltf) 2611 ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 2612 mcs = ra_report->txrate.mcs & 0x07; 2613 break; 2614 case RTW89_RA_RPT_MODE_VHT: 2615 ra_report->txrate.flags |= RATE_INFO_FLAGS_VHT_MCS; 2616 ra_report->txrate.mcs = format_v1 ? 2617 u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) : 2618 u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS); 2619 ra_report->txrate.nss = format_v1 ? 2620 u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 : 2621 u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1; 2622 if (giltf) 2623 ra_report->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI; 2624 mcs = ra_report->txrate.mcs; 2625 break; 2626 case RTW89_RA_RPT_MODE_HE: 2627 ra_report->txrate.flags |= RATE_INFO_FLAGS_HE_MCS; 2628 ra_report->txrate.mcs = format_v1 ? 2629 u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1) : 2630 u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS); 2631 ra_report->txrate.nss = format_v1 ? 2632 u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1 : 2633 u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS) + 1; 2634 if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08) 2635 ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_0_8; 2636 else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16) 2637 ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_1_6; 2638 else 2639 ra_report->txrate.he_gi = NL80211_RATE_INFO_HE_GI_3_2; 2640 mcs = ra_report->txrate.mcs; 2641 break; 2642 case RTW89_RA_RPT_MODE_EHT: 2643 ra_report->txrate.flags |= RATE_INFO_FLAGS_EHT_MCS; 2644 ra_report->txrate.mcs = u8_get_bits(rate, RTW89_RA_RATE_MASK_MCS_V1); 2645 ra_report->txrate.nss = u8_get_bits(rate, RTW89_RA_RATE_MASK_NSS_V1) + 1; 2646 if (giltf == RTW89_GILTF_2XHE08 || giltf == RTW89_GILTF_1XHE08) 2647 ra_report->txrate.eht_gi = NL80211_RATE_INFO_EHT_GI_0_8; 2648 else if (giltf == RTW89_GILTF_2XHE16 || giltf == RTW89_GILTF_1XHE16) 2649 ra_report->txrate.eht_gi = NL80211_RATE_INFO_EHT_GI_1_6; 2650 else 2651 ra_report->txrate.eht_gi = NL80211_RATE_INFO_EHT_GI_3_2; 2652 mcs = ra_report->txrate.mcs; 2653 break; 2654 } 2655 2656 ra_report->txrate.bw = rtw89_hw_to_rate_info_bw(bw); 2657 ra_report->bit_rate = cfg80211_calculate_bitrate(&ra_report->txrate); 2658 ra_report->hw_rate = format_v1 ? 2659 u16_encode_bits(mode, RTW89_HW_RATE_V1_MASK_MOD) | 2660 u16_encode_bits(rate, RTW89_HW_RATE_V1_MASK_VAL) : 2661 u16_encode_bits(mode, RTW89_HW_RATE_MASK_MOD) | 2662 u16_encode_bits(rate, RTW89_HW_RATE_MASK_VAL); 2663 ra_report->might_fallback_legacy = mcs <= 2; 2664 sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(rtwdev, ra_report); 2665 rtwsta->max_agg_wait = sta->deflink.agg.max_rc_amsdu_len / 1500 - 1; 2666 } 2667 2668 static void 2669 rtw89_phy_c2h_ra_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2670 { 2671 struct rtw89_phy_iter_ra_data ra_data; 2672 2673 ra_data.rtwdev = rtwdev; 2674 ra_data.c2h = c2h; 2675 ieee80211_iterate_stations_atomic(rtwdev->hw, 2676 rtw89_phy_c2h_ra_rpt_iter, 2677 &ra_data); 2678 } 2679 2680 static 2681 void (* const rtw89_phy_c2h_ra_handler[])(struct rtw89_dev *rtwdev, 2682 struct sk_buff *c2h, u32 len) = { 2683 [RTW89_PHY_C2H_FUNC_STS_RPT] = rtw89_phy_c2h_ra_rpt, 2684 [RTW89_PHY_C2H_FUNC_MU_GPTBL_RPT] = NULL, 2685 [RTW89_PHY_C2H_FUNC_TXSTS] = NULL, 2686 }; 2687 2688 static void rtw89_phy_c2h_rfk_rpt_log(struct rtw89_dev *rtwdev, 2689 enum rtw89_phy_c2h_rfk_log_func func, 2690 void *content, u16 len) 2691 { 2692 struct rtw89_c2h_rf_txgapk_rpt_log *txgapk; 2693 struct rtw89_c2h_rf_rxdck_rpt_log *rxdck; 2694 struct rtw89_c2h_rf_dack_rpt_log *dack; 2695 struct rtw89_c2h_rf_dpk_rpt_log *dpk; 2696 2697 switch (func) { 2698 case RTW89_PHY_C2H_RFK_LOG_FUNC_DPK: 2699 if (len != sizeof(*dpk)) 2700 goto out; 2701 2702 dpk = content; 2703 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2704 "DPK ver:%d idx:%2ph band:%2ph bw:%2ph ch:%2ph path:%2ph\n", 2705 dpk->ver, dpk->idx, dpk->band, dpk->bw, dpk->ch, dpk->path_ok); 2706 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2707 "DPK txagc:%2ph ther:%2ph gs:%2ph dc_i:%4ph dc_q:%4ph\n", 2708 dpk->txagc, dpk->ther, dpk->gs, dpk->dc_i, dpk->dc_q); 2709 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2710 "DPK corr_v:%2ph corr_i:%2ph to:%2ph ov:%2ph\n", 2711 dpk->corr_val, dpk->corr_idx, dpk->is_timeout, dpk->rxbb_ov); 2712 return; 2713 case RTW89_PHY_C2H_RFK_LOG_FUNC_DACK: 2714 if (len != sizeof(*dack)) 2715 goto out; 2716 2717 dack = content; 2718 2719 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]ver=0x%x 0x%x\n", 2720 dack->fwdack_ver, dack->fwdack_rpt_ver); 2721 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 CDACK ic = [0x%x, 0x%x]\n", 2722 dack->cdack_d[0][0][0], dack->cdack_d[0][0][1]); 2723 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 CDACK qc = [0x%x, 0x%x]\n", 2724 dack->cdack_d[0][1][0], dack->cdack_d[0][1][1]); 2725 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 CDACK ic = [0x%x, 0x%x]\n", 2726 dack->cdack_d[1][0][0], dack->cdack_d[1][0][1]); 2727 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 CDACK qc = [0x%x, 0x%x]\n", 2728 dack->cdack_d[1][1][0], dack->cdack_d[1][1][1]); 2729 2730 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADC_DCK ic = [0x%x, 0x%x]\n", 2731 dack->addck2_d[0][0][0], dack->addck2_d[0][0][1]); 2732 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADC_DCK qc = [0x%x, 0x%x]\n", 2733 dack->addck2_d[0][1][0], dack->addck2_d[0][1][1]); 2734 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 ADC_DCK ic = [0x%x, 0x%x]\n", 2735 dack->addck2_d[1][0][0], dack->addck2_d[1][0][1]); 2736 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 ADC_DCK qc = [0x%x, 0x%x]\n", 2737 dack->addck2_d[1][1][0], dack->addck2_d[1][1][1]); 2738 2739 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADC_GAINK ic = 0x%x, qc = 0x%x\n", 2740 dack->adgaink_d[0][0], dack->adgaink_d[0][1]); 2741 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 ADC_GAINK ic = 0x%x, qc = 0x%x\n", 2742 dack->adgaink_d[1][0], dack->adgaink_d[1][1]); 2743 2744 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DAC_DCK ic = 0x%x, qc = 0x%x\n", 2745 dack->dadck_d[0][0], dack->dadck_d[0][1]); 2746 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 DAC_DCK ic = 0x%x, qc = 0x%x\n", 2747 dack->dadck_d[1][0], dack->dadck_d[1][1]); 2748 2749 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 biask iqc = 0x%x\n", 2750 dack->biask_d[0][0]); 2751 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 biask iqc = 0x%x\n", 2752 dack->biask_d[1][0]); 2753 2754 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK ic: %*ph\n", 2755 (int)sizeof(dack->msbk_d[0][0]), dack->msbk_d[0][0]); 2756 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK qc: %*ph\n", 2757 (int)sizeof(dack->msbk_d[0][1]), dack->msbk_d[0][1]); 2758 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK ic: %*ph\n", 2759 (int)sizeof(dack->msbk_d[1][0]), dack->msbk_d[1][0]); 2760 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK qc: %*ph\n", 2761 (int)sizeof(dack->msbk_d[1][1]), dack->msbk_d[1][1]); 2762 return; 2763 case RTW89_PHY_C2H_RFK_LOG_FUNC_RXDCK: 2764 if (len != sizeof(*rxdck)) 2765 goto out; 2766 2767 rxdck = content; 2768 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2769 "RXDCK ver:%d band:%2ph bw:%2ph ch:%2ph to:%2ph\n", 2770 rxdck->ver, rxdck->band, rxdck->bw, rxdck->ch, 2771 rxdck->timeout); 2772 return; 2773 case RTW89_PHY_C2H_RFK_LOG_FUNC_TXGAPK: 2774 if (len != sizeof(*txgapk)) 2775 goto out; 2776 2777 txgapk = content; 2778 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2779 "[TXGAPK]rpt r0x8010[0]=0x%x, r0x8010[1]=0x%x\n", 2780 le32_to_cpu(txgapk->r0x8010[0]), 2781 le32_to_cpu(txgapk->r0x8010[1])); 2782 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt chk_id = %d\n", 2783 txgapk->chk_id); 2784 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt chk_cnt = %d\n", 2785 le32_to_cpu(txgapk->chk_cnt)); 2786 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt ver = 0x%x\n", 2787 txgapk->ver); 2788 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt rsv1 = %d\n", 2789 txgapk->rsv1); 2790 2791 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt track_d[0] = %*ph\n", 2792 (int)sizeof(txgapk->track_d[0]), txgapk->track_d[0]); 2793 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt power_d[0] = %*ph\n", 2794 (int)sizeof(txgapk->power_d[0]), txgapk->power_d[0]); 2795 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt track_d[1] = %*ph\n", 2796 (int)sizeof(txgapk->track_d[1]), txgapk->track_d[1]); 2797 rtw89_debug(rtwdev, RTW89_DBG_RFK, "[TXGAPK]rpt power_d[1] = %*ph\n", 2798 (int)sizeof(txgapk->power_d[1]), txgapk->power_d[1]); 2799 return; 2800 default: 2801 break; 2802 } 2803 2804 out: 2805 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2806 "unexpected RFK func %d report log with length %d\n", func, len); 2807 } 2808 2809 static bool rtw89_phy_c2h_rfk_run_log(struct rtw89_dev *rtwdev, 2810 enum rtw89_phy_c2h_rfk_log_func func, 2811 void *content, u16 len) 2812 { 2813 struct rtw89_fw_elm_info *elm_info = &rtwdev->fw.elm_info; 2814 const struct rtw89_c2h_rf_run_log *log = content; 2815 const struct rtw89_fw_element_hdr *elm; 2816 u32 fmt_idx; 2817 u16 offset; 2818 2819 if (sizeof(*log) != len) 2820 return false; 2821 2822 if (!elm_info->rfk_log_fmt) 2823 return false; 2824 2825 elm = elm_info->rfk_log_fmt->elm[func]; 2826 fmt_idx = le32_to_cpu(log->fmt_idx); 2827 if (!elm || fmt_idx >= elm->u.rfk_log_fmt.nr) 2828 return false; 2829 2830 offset = le16_to_cpu(elm->u.rfk_log_fmt.offset[fmt_idx]); 2831 if (offset == 0) 2832 return false; 2833 2834 rtw89_debug(rtwdev, RTW89_DBG_RFK, &elm->u.common.contents[offset], 2835 le32_to_cpu(log->arg[0]), le32_to_cpu(log->arg[1]), 2836 le32_to_cpu(log->arg[2]), le32_to_cpu(log->arg[3])); 2837 2838 return true; 2839 } 2840 2841 static void rtw89_phy_c2h_rfk_log(struct rtw89_dev *rtwdev, struct sk_buff *c2h, 2842 u32 len, enum rtw89_phy_c2h_rfk_log_func func, 2843 const char *rfk_name) 2844 { 2845 struct rtw89_c2h_hdr *c2h_hdr = (struct rtw89_c2h_hdr *)c2h->data; 2846 struct rtw89_c2h_rf_log_hdr *log_hdr; 2847 void *log_ptr = c2h_hdr; 2848 u16 content_len; 2849 u16 chunk_len; 2850 bool handled; 2851 2852 if (!rtw89_debug_is_enabled(rtwdev, RTW89_DBG_RFK)) 2853 return; 2854 2855 log_ptr += sizeof(*c2h_hdr); 2856 len -= sizeof(*c2h_hdr); 2857 2858 while (len > sizeof(*log_hdr)) { 2859 log_hdr = log_ptr; 2860 content_len = le16_to_cpu(log_hdr->len); 2861 chunk_len = content_len + sizeof(*log_hdr); 2862 2863 if (chunk_len > len) 2864 break; 2865 2866 switch (log_hdr->type) { 2867 case RTW89_RF_RUN_LOG: 2868 handled = rtw89_phy_c2h_rfk_run_log(rtwdev, func, 2869 log_hdr->content, content_len); 2870 if (handled) 2871 break; 2872 2873 rtw89_debug(rtwdev, RTW89_DBG_RFK, "%s run: %*ph\n", 2874 rfk_name, content_len, log_hdr->content); 2875 break; 2876 case RTW89_RF_RPT_LOG: 2877 rtw89_phy_c2h_rfk_rpt_log(rtwdev, func, 2878 log_hdr->content, content_len); 2879 break; 2880 default: 2881 return; 2882 } 2883 2884 log_ptr += chunk_len; 2885 len -= chunk_len; 2886 } 2887 } 2888 2889 static void 2890 rtw89_phy_c2h_rfk_log_iqk(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2891 { 2892 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2893 RTW89_PHY_C2H_RFK_LOG_FUNC_IQK, "IQK"); 2894 } 2895 2896 static void 2897 rtw89_phy_c2h_rfk_log_dpk(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2898 { 2899 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2900 RTW89_PHY_C2H_RFK_LOG_FUNC_DPK, "DPK"); 2901 } 2902 2903 static void 2904 rtw89_phy_c2h_rfk_log_dack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2905 { 2906 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2907 RTW89_PHY_C2H_RFK_LOG_FUNC_DACK, "DACK"); 2908 } 2909 2910 static void 2911 rtw89_phy_c2h_rfk_log_rxdck(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2912 { 2913 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2914 RTW89_PHY_C2H_RFK_LOG_FUNC_RXDCK, "RX_DCK"); 2915 } 2916 2917 static void 2918 rtw89_phy_c2h_rfk_log_tssi(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2919 { 2920 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2921 RTW89_PHY_C2H_RFK_LOG_FUNC_TSSI, "TSSI"); 2922 } 2923 2924 static void 2925 rtw89_phy_c2h_rfk_log_txgapk(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2926 { 2927 rtw89_phy_c2h_rfk_log(rtwdev, c2h, len, 2928 RTW89_PHY_C2H_RFK_LOG_FUNC_TXGAPK, "TXGAPK"); 2929 } 2930 2931 static 2932 void (* const rtw89_phy_c2h_rfk_log_handler[])(struct rtw89_dev *rtwdev, 2933 struct sk_buff *c2h, u32 len) = { 2934 [RTW89_PHY_C2H_RFK_LOG_FUNC_IQK] = rtw89_phy_c2h_rfk_log_iqk, 2935 [RTW89_PHY_C2H_RFK_LOG_FUNC_DPK] = rtw89_phy_c2h_rfk_log_dpk, 2936 [RTW89_PHY_C2H_RFK_LOG_FUNC_DACK] = rtw89_phy_c2h_rfk_log_dack, 2937 [RTW89_PHY_C2H_RFK_LOG_FUNC_RXDCK] = rtw89_phy_c2h_rfk_log_rxdck, 2938 [RTW89_PHY_C2H_RFK_LOG_FUNC_TSSI] = rtw89_phy_c2h_rfk_log_tssi, 2939 [RTW89_PHY_C2H_RFK_LOG_FUNC_TXGAPK] = rtw89_phy_c2h_rfk_log_txgapk, 2940 }; 2941 2942 static 2943 void rtw89_phy_rfk_report_prep(struct rtw89_dev *rtwdev) 2944 { 2945 struct rtw89_rfk_wait_info *wait = &rtwdev->rfk_wait; 2946 2947 wait->state = RTW89_RFK_STATE_START; 2948 wait->start_time = ktime_get(); 2949 reinit_completion(&wait->completion); 2950 } 2951 2952 static 2953 int rtw89_phy_rfk_report_wait(struct rtw89_dev *rtwdev, const char *rfk_name, 2954 unsigned int ms) 2955 { 2956 struct rtw89_rfk_wait_info *wait = &rtwdev->rfk_wait; 2957 unsigned long time_left; 2958 2959 /* Since we can't receive C2H event during SER, use a fixed delay. */ 2960 if (test_bit(RTW89_FLAG_SER_HANDLING, rtwdev->flags)) { 2961 fsleep(1000 * ms / 2); 2962 goto out; 2963 } 2964 2965 time_left = wait_for_completion_timeout(&wait->completion, 2966 msecs_to_jiffies(ms)); 2967 if (time_left == 0) { 2968 rtw89_warn(rtwdev, "failed to wait RF %s\n", rfk_name); 2969 return -ETIMEDOUT; 2970 } else if (wait->state != RTW89_RFK_STATE_OK) { 2971 rtw89_warn(rtwdev, "failed to do RF %s result from state %d\n", 2972 rfk_name, wait->state); 2973 return -EFAULT; 2974 } 2975 2976 out: 2977 rtw89_debug(rtwdev, RTW89_DBG_RFK, "RF %s takes %lld ms to complete\n", 2978 rfk_name, ktime_ms_delta(ktime_get(), wait->start_time)); 2979 2980 return 0; 2981 } 2982 2983 static void 2984 rtw89_phy_c2h_rfk_report_state(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 2985 { 2986 const struct rtw89_c2h_rfk_report *report = 2987 (const struct rtw89_c2h_rfk_report *)c2h->data; 2988 struct rtw89_rfk_wait_info *wait = &rtwdev->rfk_wait; 2989 2990 wait->state = report->state; 2991 wait->version = report->version; 2992 2993 complete(&wait->completion); 2994 2995 rtw89_debug(rtwdev, RTW89_DBG_RFK, 2996 "RFK report state %d with version %d (%*ph)\n", 2997 wait->state, wait->version, 2998 (int)(len - sizeof(report->hdr)), &report->state); 2999 } 3000 3001 static 3002 void (* const rtw89_phy_c2h_rfk_report_handler[])(struct rtw89_dev *rtwdev, 3003 struct sk_buff *c2h, u32 len) = { 3004 [RTW89_PHY_C2H_RFK_REPORT_FUNC_STATE] = rtw89_phy_c2h_rfk_report_state, 3005 }; 3006 3007 bool rtw89_phy_c2h_chk_atomic(struct rtw89_dev *rtwdev, u8 class, u8 func) 3008 { 3009 switch (class) { 3010 case RTW89_PHY_C2H_RFK_LOG: 3011 switch (func) { 3012 case RTW89_PHY_C2H_RFK_LOG_FUNC_IQK: 3013 case RTW89_PHY_C2H_RFK_LOG_FUNC_DPK: 3014 case RTW89_PHY_C2H_RFK_LOG_FUNC_DACK: 3015 case RTW89_PHY_C2H_RFK_LOG_FUNC_RXDCK: 3016 case RTW89_PHY_C2H_RFK_LOG_FUNC_TSSI: 3017 case RTW89_PHY_C2H_RFK_LOG_FUNC_TXGAPK: 3018 return true; 3019 default: 3020 return false; 3021 } 3022 case RTW89_PHY_C2H_RFK_REPORT: 3023 switch (func) { 3024 case RTW89_PHY_C2H_RFK_REPORT_FUNC_STATE: 3025 return true; 3026 default: 3027 return false; 3028 } 3029 default: 3030 return false; 3031 } 3032 } 3033 3034 void rtw89_phy_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb, 3035 u32 len, u8 class, u8 func) 3036 { 3037 void (*handler)(struct rtw89_dev *rtwdev, 3038 struct sk_buff *c2h, u32 len) = NULL; 3039 3040 switch (class) { 3041 case RTW89_PHY_C2H_CLASS_RA: 3042 if (func < RTW89_PHY_C2H_FUNC_RA_MAX) 3043 handler = rtw89_phy_c2h_ra_handler[func]; 3044 break; 3045 case RTW89_PHY_C2H_RFK_LOG: 3046 if (func < ARRAY_SIZE(rtw89_phy_c2h_rfk_log_handler)) 3047 handler = rtw89_phy_c2h_rfk_log_handler[func]; 3048 break; 3049 case RTW89_PHY_C2H_RFK_REPORT: 3050 if (func < ARRAY_SIZE(rtw89_phy_c2h_rfk_report_handler)) 3051 handler = rtw89_phy_c2h_rfk_report_handler[func]; 3052 break; 3053 case RTW89_PHY_C2H_CLASS_DM: 3054 if (func == RTW89_PHY_C2H_DM_FUNC_LOWRT_RTY) 3055 return; 3056 fallthrough; 3057 default: 3058 rtw89_info(rtwdev, "c2h class %d not support\n", class); 3059 return; 3060 } 3061 if (!handler) { 3062 rtw89_info(rtwdev, "c2h class %d func %d not support\n", class, 3063 func); 3064 return; 3065 } 3066 handler(rtwdev, skb, len); 3067 } 3068 3069 int rtw89_phy_rfk_pre_ntfy_and_wait(struct rtw89_dev *rtwdev, 3070 enum rtw89_phy_idx phy_idx, 3071 unsigned int ms) 3072 { 3073 int ret; 3074 3075 rtw89_phy_rfk_report_prep(rtwdev); 3076 3077 ret = rtw89_fw_h2c_rf_pre_ntfy(rtwdev, phy_idx); 3078 if (ret) 3079 return ret; 3080 3081 return rtw89_phy_rfk_report_wait(rtwdev, "PRE_NTFY", ms); 3082 } 3083 EXPORT_SYMBOL(rtw89_phy_rfk_pre_ntfy_and_wait); 3084 3085 int rtw89_phy_rfk_tssi_and_wait(struct rtw89_dev *rtwdev, 3086 enum rtw89_phy_idx phy_idx, 3087 enum rtw89_tssi_mode tssi_mode, 3088 unsigned int ms) 3089 { 3090 int ret; 3091 3092 rtw89_phy_rfk_report_prep(rtwdev); 3093 3094 ret = rtw89_fw_h2c_rf_tssi(rtwdev, phy_idx, tssi_mode); 3095 if (ret) 3096 return ret; 3097 3098 return rtw89_phy_rfk_report_wait(rtwdev, "TSSI", ms); 3099 } 3100 EXPORT_SYMBOL(rtw89_phy_rfk_tssi_and_wait); 3101 3102 int rtw89_phy_rfk_iqk_and_wait(struct rtw89_dev *rtwdev, 3103 enum rtw89_phy_idx phy_idx, 3104 unsigned int ms) 3105 { 3106 int ret; 3107 3108 rtw89_phy_rfk_report_prep(rtwdev); 3109 3110 ret = rtw89_fw_h2c_rf_iqk(rtwdev, phy_idx); 3111 if (ret) 3112 return ret; 3113 3114 return rtw89_phy_rfk_report_wait(rtwdev, "IQK", ms); 3115 } 3116 EXPORT_SYMBOL(rtw89_phy_rfk_iqk_and_wait); 3117 3118 int rtw89_phy_rfk_dpk_and_wait(struct rtw89_dev *rtwdev, 3119 enum rtw89_phy_idx phy_idx, 3120 unsigned int ms) 3121 { 3122 int ret; 3123 3124 rtw89_phy_rfk_report_prep(rtwdev); 3125 3126 ret = rtw89_fw_h2c_rf_dpk(rtwdev, phy_idx); 3127 if (ret) 3128 return ret; 3129 3130 return rtw89_phy_rfk_report_wait(rtwdev, "DPK", ms); 3131 } 3132 EXPORT_SYMBOL(rtw89_phy_rfk_dpk_and_wait); 3133 3134 int rtw89_phy_rfk_txgapk_and_wait(struct rtw89_dev *rtwdev, 3135 enum rtw89_phy_idx phy_idx, 3136 unsigned int ms) 3137 { 3138 int ret; 3139 3140 rtw89_phy_rfk_report_prep(rtwdev); 3141 3142 ret = rtw89_fw_h2c_rf_txgapk(rtwdev, phy_idx); 3143 if (ret) 3144 return ret; 3145 3146 return rtw89_phy_rfk_report_wait(rtwdev, "TXGAPK", ms); 3147 } 3148 EXPORT_SYMBOL(rtw89_phy_rfk_txgapk_and_wait); 3149 3150 int rtw89_phy_rfk_dack_and_wait(struct rtw89_dev *rtwdev, 3151 enum rtw89_phy_idx phy_idx, 3152 unsigned int ms) 3153 { 3154 int ret; 3155 3156 rtw89_phy_rfk_report_prep(rtwdev); 3157 3158 ret = rtw89_fw_h2c_rf_dack(rtwdev, phy_idx); 3159 if (ret) 3160 return ret; 3161 3162 return rtw89_phy_rfk_report_wait(rtwdev, "DACK", ms); 3163 } 3164 EXPORT_SYMBOL(rtw89_phy_rfk_dack_and_wait); 3165 3166 int rtw89_phy_rfk_rxdck_and_wait(struct rtw89_dev *rtwdev, 3167 enum rtw89_phy_idx phy_idx, 3168 unsigned int ms) 3169 { 3170 int ret; 3171 3172 rtw89_phy_rfk_report_prep(rtwdev); 3173 3174 ret = rtw89_fw_h2c_rf_rxdck(rtwdev, phy_idx); 3175 if (ret) 3176 return ret; 3177 3178 return rtw89_phy_rfk_report_wait(rtwdev, "RX_DCK", ms); 3179 } 3180 EXPORT_SYMBOL(rtw89_phy_rfk_rxdck_and_wait); 3181 3182 static u32 phy_tssi_get_cck_group(u8 ch) 3183 { 3184 switch (ch) { 3185 case 1 ... 2: 3186 return 0; 3187 case 3 ... 5: 3188 return 1; 3189 case 6 ... 8: 3190 return 2; 3191 case 9 ... 11: 3192 return 3; 3193 case 12 ... 13: 3194 return 4; 3195 case 14: 3196 return 5; 3197 } 3198 3199 return 0; 3200 } 3201 3202 #define PHY_TSSI_EXTRA_GROUP_BIT BIT(31) 3203 #define PHY_TSSI_EXTRA_GROUP(idx) (PHY_TSSI_EXTRA_GROUP_BIT | (idx)) 3204 #define PHY_IS_TSSI_EXTRA_GROUP(group) ((group) & PHY_TSSI_EXTRA_GROUP_BIT) 3205 #define PHY_TSSI_EXTRA_GET_GROUP_IDX1(group) \ 3206 ((group) & ~PHY_TSSI_EXTRA_GROUP_BIT) 3207 #define PHY_TSSI_EXTRA_GET_GROUP_IDX2(group) \ 3208 (PHY_TSSI_EXTRA_GET_GROUP_IDX1(group) + 1) 3209 3210 static u32 phy_tssi_get_ofdm_group(u8 ch) 3211 { 3212 switch (ch) { 3213 case 1 ... 2: 3214 return 0; 3215 case 3 ... 5: 3216 return 1; 3217 case 6 ... 8: 3218 return 2; 3219 case 9 ... 11: 3220 return 3; 3221 case 12 ... 14: 3222 return 4; 3223 case 36 ... 40: 3224 return 5; 3225 case 41 ... 43: 3226 return PHY_TSSI_EXTRA_GROUP(5); 3227 case 44 ... 48: 3228 return 6; 3229 case 49 ... 51: 3230 return PHY_TSSI_EXTRA_GROUP(6); 3231 case 52 ... 56: 3232 return 7; 3233 case 57 ... 59: 3234 return PHY_TSSI_EXTRA_GROUP(7); 3235 case 60 ... 64: 3236 return 8; 3237 case 100 ... 104: 3238 return 9; 3239 case 105 ... 107: 3240 return PHY_TSSI_EXTRA_GROUP(9); 3241 case 108 ... 112: 3242 return 10; 3243 case 113 ... 115: 3244 return PHY_TSSI_EXTRA_GROUP(10); 3245 case 116 ... 120: 3246 return 11; 3247 case 121 ... 123: 3248 return PHY_TSSI_EXTRA_GROUP(11); 3249 case 124 ... 128: 3250 return 12; 3251 case 129 ... 131: 3252 return PHY_TSSI_EXTRA_GROUP(12); 3253 case 132 ... 136: 3254 return 13; 3255 case 137 ... 139: 3256 return PHY_TSSI_EXTRA_GROUP(13); 3257 case 140 ... 144: 3258 return 14; 3259 case 149 ... 153: 3260 return 15; 3261 case 154 ... 156: 3262 return PHY_TSSI_EXTRA_GROUP(15); 3263 case 157 ... 161: 3264 return 16; 3265 case 162 ... 164: 3266 return PHY_TSSI_EXTRA_GROUP(16); 3267 case 165 ... 169: 3268 return 17; 3269 case 170 ... 172: 3270 return PHY_TSSI_EXTRA_GROUP(17); 3271 case 173 ... 177: 3272 return 18; 3273 } 3274 3275 return 0; 3276 } 3277 3278 static u32 phy_tssi_get_6g_ofdm_group(u8 ch) 3279 { 3280 switch (ch) { 3281 case 1 ... 5: 3282 return 0; 3283 case 6 ... 8: 3284 return PHY_TSSI_EXTRA_GROUP(0); 3285 case 9 ... 13: 3286 return 1; 3287 case 14 ... 16: 3288 return PHY_TSSI_EXTRA_GROUP(1); 3289 case 17 ... 21: 3290 return 2; 3291 case 22 ... 24: 3292 return PHY_TSSI_EXTRA_GROUP(2); 3293 case 25 ... 29: 3294 return 3; 3295 case 33 ... 37: 3296 return 4; 3297 case 38 ... 40: 3298 return PHY_TSSI_EXTRA_GROUP(4); 3299 case 41 ... 45: 3300 return 5; 3301 case 46 ... 48: 3302 return PHY_TSSI_EXTRA_GROUP(5); 3303 case 49 ... 53: 3304 return 6; 3305 case 54 ... 56: 3306 return PHY_TSSI_EXTRA_GROUP(6); 3307 case 57 ... 61: 3308 return 7; 3309 case 65 ... 69: 3310 return 8; 3311 case 70 ... 72: 3312 return PHY_TSSI_EXTRA_GROUP(8); 3313 case 73 ... 77: 3314 return 9; 3315 case 78 ... 80: 3316 return PHY_TSSI_EXTRA_GROUP(9); 3317 case 81 ... 85: 3318 return 10; 3319 case 86 ... 88: 3320 return PHY_TSSI_EXTRA_GROUP(10); 3321 case 89 ... 93: 3322 return 11; 3323 case 97 ... 101: 3324 return 12; 3325 case 102 ... 104: 3326 return PHY_TSSI_EXTRA_GROUP(12); 3327 case 105 ... 109: 3328 return 13; 3329 case 110 ... 112: 3330 return PHY_TSSI_EXTRA_GROUP(13); 3331 case 113 ... 117: 3332 return 14; 3333 case 118 ... 120: 3334 return PHY_TSSI_EXTRA_GROUP(14); 3335 case 121 ... 125: 3336 return 15; 3337 case 129 ... 133: 3338 return 16; 3339 case 134 ... 136: 3340 return PHY_TSSI_EXTRA_GROUP(16); 3341 case 137 ... 141: 3342 return 17; 3343 case 142 ... 144: 3344 return PHY_TSSI_EXTRA_GROUP(17); 3345 case 145 ... 149: 3346 return 18; 3347 case 150 ... 152: 3348 return PHY_TSSI_EXTRA_GROUP(18); 3349 case 153 ... 157: 3350 return 19; 3351 case 161 ... 165: 3352 return 20; 3353 case 166 ... 168: 3354 return PHY_TSSI_EXTRA_GROUP(20); 3355 case 169 ... 173: 3356 return 21; 3357 case 174 ... 176: 3358 return PHY_TSSI_EXTRA_GROUP(21); 3359 case 177 ... 181: 3360 return 22; 3361 case 182 ... 184: 3362 return PHY_TSSI_EXTRA_GROUP(22); 3363 case 185 ... 189: 3364 return 23; 3365 case 193 ... 197: 3366 return 24; 3367 case 198 ... 200: 3368 return PHY_TSSI_EXTRA_GROUP(24); 3369 case 201 ... 205: 3370 return 25; 3371 case 206 ... 208: 3372 return PHY_TSSI_EXTRA_GROUP(25); 3373 case 209 ... 213: 3374 return 26; 3375 case 214 ... 216: 3376 return PHY_TSSI_EXTRA_GROUP(26); 3377 case 217 ... 221: 3378 return 27; 3379 case 225 ... 229: 3380 return 28; 3381 case 230 ... 232: 3382 return PHY_TSSI_EXTRA_GROUP(28); 3383 case 233 ... 237: 3384 return 29; 3385 case 238 ... 240: 3386 return PHY_TSSI_EXTRA_GROUP(29); 3387 case 241 ... 245: 3388 return 30; 3389 case 246 ... 248: 3390 return PHY_TSSI_EXTRA_GROUP(30); 3391 case 249 ... 253: 3392 return 31; 3393 } 3394 3395 return 0; 3396 } 3397 3398 static u32 phy_tssi_get_trim_group(u8 ch) 3399 { 3400 switch (ch) { 3401 case 1 ... 8: 3402 return 0; 3403 case 9 ... 14: 3404 return 1; 3405 case 36 ... 48: 3406 return 2; 3407 case 49 ... 51: 3408 return PHY_TSSI_EXTRA_GROUP(2); 3409 case 52 ... 64: 3410 return 3; 3411 case 100 ... 112: 3412 return 4; 3413 case 113 ... 115: 3414 return PHY_TSSI_EXTRA_GROUP(4); 3415 case 116 ... 128: 3416 return 5; 3417 case 132 ... 144: 3418 return 6; 3419 case 149 ... 177: 3420 return 7; 3421 } 3422 3423 return 0; 3424 } 3425 3426 static u32 phy_tssi_get_6g_trim_group(u8 ch) 3427 { 3428 switch (ch) { 3429 case 1 ... 13: 3430 return 0; 3431 case 14 ... 16: 3432 return PHY_TSSI_EXTRA_GROUP(0); 3433 case 17 ... 29: 3434 return 1; 3435 case 33 ... 45: 3436 return 2; 3437 case 46 ... 48: 3438 return PHY_TSSI_EXTRA_GROUP(2); 3439 case 49 ... 61: 3440 return 3; 3441 case 65 ... 77: 3442 return 4; 3443 case 78 ... 80: 3444 return PHY_TSSI_EXTRA_GROUP(4); 3445 case 81 ... 93: 3446 return 5; 3447 case 97 ... 109: 3448 return 6; 3449 case 110 ... 112: 3450 return PHY_TSSI_EXTRA_GROUP(6); 3451 case 113 ... 125: 3452 return 7; 3453 case 129 ... 141: 3454 return 8; 3455 case 142 ... 144: 3456 return PHY_TSSI_EXTRA_GROUP(8); 3457 case 145 ... 157: 3458 return 9; 3459 case 161 ... 173: 3460 return 10; 3461 case 174 ... 176: 3462 return PHY_TSSI_EXTRA_GROUP(10); 3463 case 177 ... 189: 3464 return 11; 3465 case 193 ... 205: 3466 return 12; 3467 case 206 ... 208: 3468 return PHY_TSSI_EXTRA_GROUP(12); 3469 case 209 ... 221: 3470 return 13; 3471 case 225 ... 237: 3472 return 14; 3473 case 238 ... 240: 3474 return PHY_TSSI_EXTRA_GROUP(14); 3475 case 241 ... 253: 3476 return 15; 3477 } 3478 3479 return 0; 3480 } 3481 3482 static s8 phy_tssi_get_ofdm_de(struct rtw89_dev *rtwdev, 3483 enum rtw89_phy_idx phy, 3484 const struct rtw89_chan *chan, 3485 enum rtw89_rf_path path) 3486 { 3487 struct rtw89_tssi_info *tssi_info = &rtwdev->tssi; 3488 enum rtw89_band band = chan->band_type; 3489 u8 ch = chan->channel; 3490 u32 gidx_1st; 3491 u32 gidx_2nd; 3492 s8 de_1st; 3493 s8 de_2nd; 3494 u32 gidx; 3495 s8 val; 3496 3497 if (band == RTW89_BAND_6G) 3498 goto calc_6g; 3499 3500 gidx = phy_tssi_get_ofdm_group(ch); 3501 3502 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3503 "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n", 3504 path, gidx); 3505 3506 if (PHY_IS_TSSI_EXTRA_GROUP(gidx)) { 3507 gidx_1st = PHY_TSSI_EXTRA_GET_GROUP_IDX1(gidx); 3508 gidx_2nd = PHY_TSSI_EXTRA_GET_GROUP_IDX2(gidx); 3509 de_1st = tssi_info->tssi_mcs[path][gidx_1st]; 3510 de_2nd = tssi_info->tssi_mcs[path][gidx_2nd]; 3511 val = (de_1st + de_2nd) / 2; 3512 3513 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3514 "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n", 3515 path, val, de_1st, de_2nd); 3516 } else { 3517 val = tssi_info->tssi_mcs[path][gidx]; 3518 3519 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3520 "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val); 3521 } 3522 3523 return val; 3524 3525 calc_6g: 3526 gidx = phy_tssi_get_6g_ofdm_group(ch); 3527 3528 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3529 "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n", 3530 path, gidx); 3531 3532 if (PHY_IS_TSSI_EXTRA_GROUP(gidx)) { 3533 gidx_1st = PHY_TSSI_EXTRA_GET_GROUP_IDX1(gidx); 3534 gidx_2nd = PHY_TSSI_EXTRA_GET_GROUP_IDX2(gidx); 3535 de_1st = tssi_info->tssi_6g_mcs[path][gidx_1st]; 3536 de_2nd = tssi_info->tssi_6g_mcs[path][gidx_2nd]; 3537 val = (de_1st + de_2nd) / 2; 3538 3539 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3540 "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n", 3541 path, val, de_1st, de_2nd); 3542 } else { 3543 val = tssi_info->tssi_6g_mcs[path][gidx]; 3544 3545 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3546 "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val); 3547 } 3548 3549 return val; 3550 } 3551 3552 static s8 phy_tssi_get_ofdm_trim_de(struct rtw89_dev *rtwdev, 3553 enum rtw89_phy_idx phy, 3554 const struct rtw89_chan *chan, 3555 enum rtw89_rf_path path) 3556 { 3557 struct rtw89_tssi_info *tssi_info = &rtwdev->tssi; 3558 enum rtw89_band band = chan->band_type; 3559 u8 ch = chan->channel; 3560 u32 tgidx_1st; 3561 u32 tgidx_2nd; 3562 s8 tde_1st; 3563 s8 tde_2nd; 3564 u32 tgidx; 3565 s8 val; 3566 3567 if (band == RTW89_BAND_6G) 3568 goto calc_6g; 3569 3570 tgidx = phy_tssi_get_trim_group(ch); 3571 3572 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3573 "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n", 3574 path, tgidx); 3575 3576 if (PHY_IS_TSSI_EXTRA_GROUP(tgidx)) { 3577 tgidx_1st = PHY_TSSI_EXTRA_GET_GROUP_IDX1(tgidx); 3578 tgidx_2nd = PHY_TSSI_EXTRA_GET_GROUP_IDX2(tgidx); 3579 tde_1st = tssi_info->tssi_trim[path][tgidx_1st]; 3580 tde_2nd = tssi_info->tssi_trim[path][tgidx_2nd]; 3581 val = (tde_1st + tde_2nd) / 2; 3582 3583 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3584 "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n", 3585 path, val, tde_1st, tde_2nd); 3586 } else { 3587 val = tssi_info->tssi_trim[path][tgidx]; 3588 3589 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3590 "[TSSI][TRIM]: path=%d mcs trim_de=%d\n", 3591 path, val); 3592 } 3593 3594 return val; 3595 3596 calc_6g: 3597 tgidx = phy_tssi_get_6g_trim_group(ch); 3598 3599 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3600 "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n", 3601 path, tgidx); 3602 3603 if (PHY_IS_TSSI_EXTRA_GROUP(tgidx)) { 3604 tgidx_1st = PHY_TSSI_EXTRA_GET_GROUP_IDX1(tgidx); 3605 tgidx_2nd = PHY_TSSI_EXTRA_GET_GROUP_IDX2(tgidx); 3606 tde_1st = tssi_info->tssi_trim_6g[path][tgidx_1st]; 3607 tde_2nd = tssi_info->tssi_trim_6g[path][tgidx_2nd]; 3608 val = (tde_1st + tde_2nd) / 2; 3609 3610 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3611 "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n", 3612 path, val, tde_1st, tde_2nd); 3613 } else { 3614 val = tssi_info->tssi_trim_6g[path][tgidx]; 3615 3616 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3617 "[TSSI][TRIM]: path=%d mcs trim_de=%d\n", 3618 path, val); 3619 } 3620 3621 return val; 3622 } 3623 3624 void rtw89_phy_rfk_tssi_fill_fwcmd_efuse_to_de(struct rtw89_dev *rtwdev, 3625 enum rtw89_phy_idx phy, 3626 const struct rtw89_chan *chan, 3627 struct rtw89_h2c_rf_tssi *h2c) 3628 { 3629 struct rtw89_tssi_info *tssi_info = &rtwdev->tssi; 3630 u8 ch = chan->channel; 3631 s8 trim_de; 3632 s8 ofdm_de; 3633 s8 cck_de; 3634 u8 gidx; 3635 s8 val; 3636 int i; 3637 3638 rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI][TRIM]: phy=%d ch=%d\n", 3639 phy, ch); 3640 3641 for (i = RF_PATH_A; i <= RF_PATH_B; i++) { 3642 trim_de = phy_tssi_get_ofdm_trim_de(rtwdev, phy, chan, i); 3643 h2c->curr_tssi_trim_de[i] = trim_de; 3644 3645 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3646 "[TSSI][TRIM]: path=%d trim_de=0x%x\n", i, trim_de); 3647 3648 gidx = phy_tssi_get_cck_group(ch); 3649 cck_de = tssi_info->tssi_cck[i][gidx]; 3650 val = u32_get_bits(cck_de + trim_de, 0xff); 3651 3652 h2c->curr_tssi_cck_de[i] = 0x0; 3653 h2c->curr_tssi_cck_de_20m[i] = val; 3654 h2c->curr_tssi_cck_de_40m[i] = val; 3655 h2c->curr_tssi_efuse_cck_de[i] = cck_de; 3656 3657 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3658 "[TSSI][TRIM]: path=%d cck_de=0x%x\n", i, cck_de); 3659 3660 ofdm_de = phy_tssi_get_ofdm_de(rtwdev, phy, chan, i); 3661 val = u32_get_bits(ofdm_de + trim_de, 0xff); 3662 3663 h2c->curr_tssi_ofdm_de[i] = 0x0; 3664 h2c->curr_tssi_ofdm_de_20m[i] = val; 3665 h2c->curr_tssi_ofdm_de_40m[i] = val; 3666 h2c->curr_tssi_ofdm_de_80m[i] = val; 3667 h2c->curr_tssi_ofdm_de_160m[i] = val; 3668 h2c->curr_tssi_ofdm_de_320m[i] = val; 3669 h2c->curr_tssi_efuse_ofdm_de[i] = ofdm_de; 3670 3671 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3672 "[TSSI][TRIM]: path=%d ofdm_de=0x%x\n", i, ofdm_de); 3673 } 3674 } 3675 3676 void rtw89_phy_rfk_tssi_fill_fwcmd_tmeter_tbl(struct rtw89_dev *rtwdev, 3677 enum rtw89_phy_idx phy, 3678 const struct rtw89_chan *chan, 3679 struct rtw89_h2c_rf_tssi *h2c) 3680 { 3681 struct rtw89_fw_txpwr_track_cfg *trk = rtwdev->fw.elm_info.txpwr_trk; 3682 struct rtw89_tssi_info *tssi_info = &rtwdev->tssi; 3683 const s8 *thm_up[RF_PATH_B + 1] = {}; 3684 const s8 *thm_down[RF_PATH_B + 1] = {}; 3685 u8 subband = chan->subband_type; 3686 s8 thm_ofst[128] = {0}; 3687 u8 thermal; 3688 u8 path; 3689 u8 i, j; 3690 3691 switch (subband) { 3692 default: 3693 case RTW89_CH_2G: 3694 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_2GA_P][0]; 3695 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_2GA_N][0]; 3696 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_2GB_P][0]; 3697 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_2GB_N][0]; 3698 break; 3699 case RTW89_CH_5G_BAND_1: 3700 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_P][0]; 3701 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_N][0]; 3702 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_P][0]; 3703 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_N][0]; 3704 break; 3705 case RTW89_CH_5G_BAND_3: 3706 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_P][1]; 3707 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_N][1]; 3708 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_P][1]; 3709 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_N][1]; 3710 break; 3711 case RTW89_CH_5G_BAND_4: 3712 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_P][2]; 3713 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GA_N][2]; 3714 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_P][2]; 3715 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_5GB_N][2]; 3716 break; 3717 case RTW89_CH_6G_BAND_IDX0: 3718 case RTW89_CH_6G_BAND_IDX1: 3719 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_P][0]; 3720 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_N][0]; 3721 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_P][0]; 3722 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_N][0]; 3723 break; 3724 case RTW89_CH_6G_BAND_IDX2: 3725 case RTW89_CH_6G_BAND_IDX3: 3726 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_P][1]; 3727 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_N][1]; 3728 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_P][1]; 3729 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_N][1]; 3730 break; 3731 case RTW89_CH_6G_BAND_IDX4: 3732 case RTW89_CH_6G_BAND_IDX5: 3733 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_P][2]; 3734 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_N][2]; 3735 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_P][2]; 3736 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_N][2]; 3737 break; 3738 case RTW89_CH_6G_BAND_IDX6: 3739 case RTW89_CH_6G_BAND_IDX7: 3740 thm_up[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_P][3]; 3741 thm_down[RF_PATH_A] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GA_N][3]; 3742 thm_up[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_P][3]; 3743 thm_down[RF_PATH_B] = trk->delta[RTW89_FW_TXPWR_TRK_TYPE_6GB_N][3]; 3744 break; 3745 } 3746 3747 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3748 "[TSSI] tmeter tbl on subband: %u\n", subband); 3749 3750 for (path = RF_PATH_A; path <= RF_PATH_B; path++) { 3751 thermal = tssi_info->thermal[path]; 3752 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3753 "path: %u, pg thermal: 0x%x\n", path, thermal); 3754 3755 if (thermal == 0xff) { 3756 h2c->pg_thermal[path] = 0x38; 3757 memset(h2c->ftable[path], 0, sizeof(h2c->ftable[path])); 3758 continue; 3759 } 3760 3761 h2c->pg_thermal[path] = thermal; 3762 3763 i = 0; 3764 for (j = 0; j < 64; j++) 3765 thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ? 3766 thm_up[path][i++] : 3767 thm_up[path][DELTA_SWINGIDX_SIZE - 1]; 3768 3769 i = 1; 3770 for (j = 127; j >= 64; j--) 3771 thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ? 3772 -thm_down[path][i++] : 3773 -thm_down[path][DELTA_SWINGIDX_SIZE - 1]; 3774 3775 for (i = 0; i < 128; i += 4) { 3776 h2c->ftable[path][i + 0] = thm_ofst[i + 3]; 3777 h2c->ftable[path][i + 1] = thm_ofst[i + 2]; 3778 h2c->ftable[path][i + 2] = thm_ofst[i + 1]; 3779 h2c->ftable[path][i + 3] = thm_ofst[i + 0]; 3780 3781 rtw89_debug(rtwdev, RTW89_DBG_TSSI, 3782 "thm ofst [%x]: %02x %02x %02x %02x\n", 3783 i, thm_ofst[i], thm_ofst[i + 1], 3784 thm_ofst[i + 2], thm_ofst[i + 3]); 3785 } 3786 } 3787 } 3788 3789 static u8 rtw89_phy_cfo_get_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo) 3790 { 3791 const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info; 3792 u32 reg_mask; 3793 3794 if (sc_xo) 3795 reg_mask = xtal->sc_xo_mask; 3796 else 3797 reg_mask = xtal->sc_xi_mask; 3798 3799 return (u8)rtw89_read32_mask(rtwdev, xtal->xcap_reg, reg_mask); 3800 } 3801 3802 static void rtw89_phy_cfo_set_xcap_reg(struct rtw89_dev *rtwdev, bool sc_xo, 3803 u8 val) 3804 { 3805 const struct rtw89_xtal_info *xtal = rtwdev->chip->xtal_info; 3806 u32 reg_mask; 3807 3808 if (sc_xo) 3809 reg_mask = xtal->sc_xo_mask; 3810 else 3811 reg_mask = xtal->sc_xi_mask; 3812 3813 rtw89_write32_mask(rtwdev, xtal->xcap_reg, reg_mask, val); 3814 } 3815 3816 static void rtw89_phy_cfo_set_crystal_cap(struct rtw89_dev *rtwdev, 3817 u8 crystal_cap, bool force) 3818 { 3819 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 3820 const struct rtw89_chip_info *chip = rtwdev->chip; 3821 u8 sc_xi_val, sc_xo_val; 3822 3823 if (!force && cfo->crystal_cap == crystal_cap) 3824 return; 3825 crystal_cap = clamp_t(u8, crystal_cap, 0, 127); 3826 if (chip->chip_id == RTL8852A || chip->chip_id == RTL8851B) { 3827 rtw89_phy_cfo_set_xcap_reg(rtwdev, true, crystal_cap); 3828 rtw89_phy_cfo_set_xcap_reg(rtwdev, false, crystal_cap); 3829 sc_xo_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, true); 3830 sc_xi_val = rtw89_phy_cfo_get_xcap_reg(rtwdev, false); 3831 } else { 3832 rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, 3833 crystal_cap, XTAL_SC_XO_MASK); 3834 rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, 3835 crystal_cap, XTAL_SC_XI_MASK); 3836 rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XO, &sc_xo_val); 3837 rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_XTAL_SC_XI, &sc_xi_val); 3838 } 3839 cfo->crystal_cap = sc_xi_val; 3840 cfo->x_cap_ofst = (s8)((int)cfo->crystal_cap - cfo->def_x_cap); 3841 3842 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xi=0x%x\n", sc_xi_val); 3843 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set sc_xo=0x%x\n", sc_xo_val); 3844 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Get xcap_ofst=%d\n", 3845 cfo->x_cap_ofst); 3846 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Set xcap OK\n"); 3847 } 3848 3849 static void rtw89_phy_cfo_reset(struct rtw89_dev *rtwdev) 3850 { 3851 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 3852 u8 cap; 3853 3854 cfo->def_x_cap = cfo->crystal_cap_default & B_AX_XTAL_SC_MASK; 3855 cfo->is_adjust = false; 3856 if (cfo->crystal_cap == cfo->def_x_cap) 3857 return; 3858 cap = cfo->crystal_cap; 3859 cap += (cap > cfo->def_x_cap ? -1 : 1); 3860 rtw89_phy_cfo_set_crystal_cap(rtwdev, cap, false); 3861 rtw89_debug(rtwdev, RTW89_DBG_CFO, 3862 "(0x%x) approach to dflt_val=(0x%x)\n", cfo->crystal_cap, 3863 cfo->def_x_cap); 3864 } 3865 3866 static void rtw89_dcfo_comp(struct rtw89_dev *rtwdev, s32 curr_cfo) 3867 { 3868 const struct rtw89_reg_def *dcfo_comp = rtwdev->chip->dcfo_comp; 3869 bool is_linked = rtwdev->total_sta_assoc > 0; 3870 s32 cfo_avg_312; 3871 s32 dcfo_comp_val; 3872 int sign; 3873 3874 if (rtwdev->chip->chip_id == RTL8922A) 3875 return; 3876 3877 if (!is_linked) { 3878 rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: is_linked=%d\n", 3879 is_linked); 3880 return; 3881 } 3882 rtw89_debug(rtwdev, RTW89_DBG_CFO, "DCFO: curr_cfo=%d\n", curr_cfo); 3883 if (curr_cfo == 0) 3884 return; 3885 dcfo_comp_val = rtw89_phy_read32_mask(rtwdev, R_DCFO, B_DCFO); 3886 sign = curr_cfo > 0 ? 1 : -1; 3887 cfo_avg_312 = curr_cfo / 625 + sign * dcfo_comp_val; 3888 rtw89_debug(rtwdev, RTW89_DBG_CFO, "avg_cfo_312=%d step\n", cfo_avg_312); 3889 if (rtwdev->chip->chip_id == RTL8852A && rtwdev->hal.cv == CHIP_CBV) 3890 cfo_avg_312 = -cfo_avg_312; 3891 rtw89_phy_set_phy_regs(rtwdev, dcfo_comp->addr, dcfo_comp->mask, 3892 cfo_avg_312); 3893 } 3894 3895 static void rtw89_dcfo_comp_init(struct rtw89_dev *rtwdev) 3896 { 3897 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 3898 const struct rtw89_chip_info *chip = rtwdev->chip; 3899 const struct rtw89_cfo_regs *cfo = phy->cfo; 3900 3901 rtw89_phy_set_phy_regs(rtwdev, cfo->comp_seg0, cfo->valid_0_mask, 1); 3902 rtw89_phy_set_phy_regs(rtwdev, cfo->comp, cfo->weighting_mask, 8); 3903 3904 if (chip->chip_gen == RTW89_CHIP_AX) { 3905 if (chip->cfo_hw_comp) { 3906 rtw89_write32_mask(rtwdev, R_AX_PWR_UL_CTRL2, 3907 B_AX_PWR_UL_CFO_MASK, 0x6); 3908 } else { 3909 rtw89_phy_set_phy_regs(rtwdev, R_DCFO, B_DCFO, 1); 3910 rtw89_write32_clr(rtwdev, R_AX_PWR_UL_CTRL2, 3911 B_AX_PWR_UL_CFO_MASK); 3912 } 3913 } 3914 } 3915 3916 static void rtw89_phy_cfo_init(struct rtw89_dev *rtwdev) 3917 { 3918 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 3919 struct rtw89_efuse *efuse = &rtwdev->efuse; 3920 3921 cfo->crystal_cap_default = efuse->xtal_cap & B_AX_XTAL_SC_MASK; 3922 cfo->crystal_cap = cfo->crystal_cap_default; 3923 cfo->def_x_cap = cfo->crystal_cap; 3924 cfo->x_cap_ub = min_t(int, cfo->def_x_cap + CFO_BOUND, 0x7f); 3925 cfo->x_cap_lb = max_t(int, cfo->def_x_cap - CFO_BOUND, 0x1); 3926 cfo->is_adjust = false; 3927 cfo->divergence_lock_en = false; 3928 cfo->x_cap_ofst = 0; 3929 cfo->lock_cnt = 0; 3930 cfo->rtw89_multi_cfo_mode = RTW89_TP_BASED_AVG_MODE; 3931 cfo->apply_compensation = false; 3932 cfo->residual_cfo_acc = 0; 3933 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Default xcap=%0x\n", 3934 cfo->crystal_cap_default); 3935 rtw89_phy_cfo_set_crystal_cap(rtwdev, cfo->crystal_cap_default, true); 3936 rtw89_dcfo_comp_init(rtwdev); 3937 cfo->cfo_timer_ms = 2000; 3938 cfo->cfo_trig_by_timer_en = false; 3939 cfo->phy_cfo_trk_cnt = 0; 3940 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 3941 cfo->cfo_ul_ofdma_acc_mode = RTW89_CFO_UL_OFDMA_ACC_ENABLE; 3942 } 3943 3944 static void rtw89_phy_cfo_crystal_cap_adjust(struct rtw89_dev *rtwdev, 3945 s32 curr_cfo) 3946 { 3947 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 3948 s8 crystal_cap = cfo->crystal_cap; 3949 s32 cfo_abs = abs(curr_cfo); 3950 int sign; 3951 3952 if (curr_cfo == 0) { 3953 rtw89_debug(rtwdev, RTW89_DBG_CFO, "curr_cfo=0\n"); 3954 return; 3955 } 3956 if (!cfo->is_adjust) { 3957 if (cfo_abs > CFO_TRK_ENABLE_TH) 3958 cfo->is_adjust = true; 3959 } else { 3960 if (cfo_abs <= CFO_TRK_STOP_TH) 3961 cfo->is_adjust = false; 3962 } 3963 if (!cfo->is_adjust) { 3964 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Stop CFO tracking\n"); 3965 return; 3966 } 3967 sign = curr_cfo > 0 ? 1 : -1; 3968 if (cfo_abs > CFO_TRK_STOP_TH_4) 3969 crystal_cap += 7 * sign; 3970 else if (cfo_abs > CFO_TRK_STOP_TH_3) 3971 crystal_cap += 5 * sign; 3972 else if (cfo_abs > CFO_TRK_STOP_TH_2) 3973 crystal_cap += 3 * sign; 3974 else if (cfo_abs > CFO_TRK_STOP_TH_1) 3975 crystal_cap += 1 * sign; 3976 else 3977 return; 3978 rtw89_phy_cfo_set_crystal_cap(rtwdev, (u8)crystal_cap, false); 3979 rtw89_debug(rtwdev, RTW89_DBG_CFO, 3980 "X_cap{Curr,Default}={0x%x,0x%x}\n", 3981 cfo->crystal_cap, cfo->def_x_cap); 3982 } 3983 3984 static s32 rtw89_phy_average_cfo_calc(struct rtw89_dev *rtwdev) 3985 { 3986 const struct rtw89_chip_info *chip = rtwdev->chip; 3987 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 3988 s32 cfo_khz_all = 0; 3989 s32 cfo_cnt_all = 0; 3990 s32 cfo_all_avg = 0; 3991 u8 i; 3992 3993 if (rtwdev->total_sta_assoc != 1) 3994 return 0; 3995 rtw89_debug(rtwdev, RTW89_DBG_CFO, "one_entry_only\n"); 3996 for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 3997 if (cfo->cfo_cnt[i] == 0) 3998 continue; 3999 cfo_khz_all += cfo->cfo_tail[i]; 4000 cfo_cnt_all += cfo->cfo_cnt[i]; 4001 cfo_all_avg = phy_div(cfo_khz_all, cfo_cnt_all); 4002 cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 4003 cfo->dcfo_avg = phy_div(cfo_khz_all << chip->dcfo_comp_sft, 4004 cfo_cnt_all); 4005 } 4006 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4007 "CFO track for macid = %d\n", i); 4008 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4009 "Total cfo=%dK, pkt_cnt=%d, avg_cfo=%dK\n", 4010 cfo_khz_all, cfo_cnt_all, cfo_all_avg); 4011 return cfo_all_avg; 4012 } 4013 4014 static s32 rtw89_phy_multi_sta_cfo_calc(struct rtw89_dev *rtwdev) 4015 { 4016 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4017 struct rtw89_traffic_stats *stats = &rtwdev->stats; 4018 s32 target_cfo = 0; 4019 s32 cfo_khz_all = 0; 4020 s32 cfo_khz_all_tp_wgt = 0; 4021 s32 cfo_avg = 0; 4022 s32 max_cfo_lb = BIT(31); 4023 s32 min_cfo_ub = GENMASK(30, 0); 4024 u16 cfo_cnt_all = 0; 4025 u8 active_entry_cnt = 0; 4026 u8 sta_cnt = 0; 4027 u32 tp_all = 0; 4028 u8 i; 4029 u8 cfo_tol = 0; 4030 4031 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Multi entry cfo_trk\n"); 4032 if (cfo->rtw89_multi_cfo_mode == RTW89_PKT_BASED_AVG_MODE) { 4033 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt based avg mode\n"); 4034 for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 4035 if (cfo->cfo_cnt[i] == 0) 4036 continue; 4037 cfo_khz_all += cfo->cfo_tail[i]; 4038 cfo_cnt_all += cfo->cfo_cnt[i]; 4039 cfo_avg = phy_div(cfo_khz_all, (s32)cfo_cnt_all); 4040 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4041 "Msta cfo=%d, pkt_cnt=%d, avg_cfo=%d\n", 4042 cfo_khz_all, cfo_cnt_all, cfo_avg); 4043 target_cfo = cfo_avg; 4044 } 4045 } else if (cfo->rtw89_multi_cfo_mode == RTW89_ENTRY_BASED_AVG_MODE) { 4046 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Entry based avg mode\n"); 4047 for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 4048 if (cfo->cfo_cnt[i] == 0) 4049 continue; 4050 cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 4051 (s32)cfo->cfo_cnt[i]); 4052 cfo_khz_all += cfo->cfo_avg[i]; 4053 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4054 "Macid=%d, cfo_avg=%d\n", i, 4055 cfo->cfo_avg[i]); 4056 } 4057 sta_cnt = rtwdev->total_sta_assoc; 4058 cfo_avg = phy_div(cfo_khz_all, (s32)sta_cnt); 4059 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4060 "Msta cfo_acc=%d, ent_cnt=%d, avg_cfo=%d\n", 4061 cfo_khz_all, sta_cnt, cfo_avg); 4062 target_cfo = cfo_avg; 4063 } else if (cfo->rtw89_multi_cfo_mode == RTW89_TP_BASED_AVG_MODE) { 4064 rtw89_debug(rtwdev, RTW89_DBG_CFO, "TP based avg mode\n"); 4065 cfo_tol = cfo->sta_cfo_tolerance; 4066 for (i = 0; i < CFO_TRACK_MAX_USER; i++) { 4067 sta_cnt++; 4068 if (cfo->cfo_cnt[i] != 0) { 4069 cfo->cfo_avg[i] = phy_div(cfo->cfo_tail[i], 4070 (s32)cfo->cfo_cnt[i]); 4071 active_entry_cnt++; 4072 } else { 4073 cfo->cfo_avg[i] = cfo->pre_cfo_avg[i]; 4074 } 4075 max_cfo_lb = max(cfo->cfo_avg[i] - cfo_tol, max_cfo_lb); 4076 min_cfo_ub = min(cfo->cfo_avg[i] + cfo_tol, min_cfo_ub); 4077 cfo_khz_all += cfo->cfo_avg[i]; 4078 /* need tp for each entry */ 4079 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4080 "[%d] cfo_avg=%d, tp=tbd\n", 4081 i, cfo->cfo_avg[i]); 4082 if (sta_cnt >= rtwdev->total_sta_assoc) 4083 break; 4084 } 4085 tp_all = stats->rx_throughput; /* need tp for each entry */ 4086 cfo_avg = phy_div(cfo_khz_all_tp_wgt, (s32)tp_all); 4087 4088 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Assoc sta cnt=%d\n", 4089 sta_cnt); 4090 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Active sta cnt=%d\n", 4091 active_entry_cnt); 4092 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4093 "Msta cfo with tp_wgt=%d, avg_cfo=%d\n", 4094 cfo_khz_all_tp_wgt, cfo_avg); 4095 rtw89_debug(rtwdev, RTW89_DBG_CFO, "cfo_lb=%d,cfo_ub=%d\n", 4096 max_cfo_lb, min_cfo_ub); 4097 if (max_cfo_lb <= min_cfo_ub) { 4098 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4099 "cfo win_size=%d\n", 4100 min_cfo_ub - max_cfo_lb); 4101 target_cfo = clamp(cfo_avg, max_cfo_lb, min_cfo_ub); 4102 } else { 4103 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4104 "No intersection of cfo tolerance windows\n"); 4105 target_cfo = phy_div(cfo_khz_all, (s32)sta_cnt); 4106 } 4107 for (i = 0; i < CFO_TRACK_MAX_USER; i++) 4108 cfo->pre_cfo_avg[i] = cfo->cfo_avg[i]; 4109 } 4110 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Target cfo=%d\n", target_cfo); 4111 return target_cfo; 4112 } 4113 4114 static void rtw89_phy_cfo_statistics_reset(struct rtw89_dev *rtwdev) 4115 { 4116 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4117 4118 memset(&cfo->cfo_tail, 0, sizeof(cfo->cfo_tail)); 4119 memset(&cfo->cfo_cnt, 0, sizeof(cfo->cfo_cnt)); 4120 cfo->packet_count = 0; 4121 cfo->packet_count_pre = 0; 4122 cfo->cfo_avg_pre = 0; 4123 } 4124 4125 static void rtw89_phy_cfo_dm(struct rtw89_dev *rtwdev) 4126 { 4127 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4128 s32 new_cfo = 0; 4129 bool x_cap_update = false; 4130 u8 pre_x_cap = cfo->crystal_cap; 4131 u8 dcfo_comp_sft = rtwdev->chip->dcfo_comp_sft; 4132 4133 cfo->dcfo_avg = 0; 4134 rtw89_debug(rtwdev, RTW89_DBG_CFO, "CFO:total_sta_assoc=%d\n", 4135 rtwdev->total_sta_assoc); 4136 if (rtwdev->total_sta_assoc == 0) { 4137 rtw89_phy_cfo_reset(rtwdev); 4138 return; 4139 } 4140 if (cfo->packet_count == 0) { 4141 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt = 0\n"); 4142 return; 4143 } 4144 if (cfo->packet_count == cfo->packet_count_pre) { 4145 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Pkt cnt doesn't change\n"); 4146 return; 4147 } 4148 if (rtwdev->total_sta_assoc == 1) 4149 new_cfo = rtw89_phy_average_cfo_calc(rtwdev); 4150 else 4151 new_cfo = rtw89_phy_multi_sta_cfo_calc(rtwdev); 4152 if (cfo->divergence_lock_en) { 4153 cfo->lock_cnt++; 4154 if (cfo->lock_cnt > CFO_PERIOD_CNT) { 4155 cfo->divergence_lock_en = false; 4156 cfo->lock_cnt = 0; 4157 } else { 4158 rtw89_phy_cfo_reset(rtwdev); 4159 } 4160 return; 4161 } 4162 if (cfo->crystal_cap >= cfo->x_cap_ub || 4163 cfo->crystal_cap <= cfo->x_cap_lb) { 4164 cfo->divergence_lock_en = true; 4165 rtw89_phy_cfo_reset(rtwdev); 4166 return; 4167 } 4168 4169 rtw89_phy_cfo_crystal_cap_adjust(rtwdev, new_cfo); 4170 cfo->cfo_avg_pre = new_cfo; 4171 cfo->dcfo_avg_pre = cfo->dcfo_avg; 4172 x_cap_update = cfo->crystal_cap != pre_x_cap; 4173 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap_up=%d\n", x_cap_update); 4174 rtw89_debug(rtwdev, RTW89_DBG_CFO, "Xcap: D:%x C:%x->%x, ofst=%d\n", 4175 cfo->def_x_cap, pre_x_cap, cfo->crystal_cap, 4176 cfo->x_cap_ofst); 4177 if (x_cap_update) { 4178 if (cfo->dcfo_avg > 0) 4179 cfo->dcfo_avg -= CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft; 4180 else 4181 cfo->dcfo_avg += CFO_SW_COMP_FINE_TUNE << dcfo_comp_sft; 4182 } 4183 rtw89_dcfo_comp(rtwdev, cfo->dcfo_avg); 4184 rtw89_phy_cfo_statistics_reset(rtwdev); 4185 } 4186 4187 void rtw89_phy_cfo_track_work(struct work_struct *work) 4188 { 4189 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 4190 cfo_track_work.work); 4191 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4192 4193 mutex_lock(&rtwdev->mutex); 4194 if (!cfo->cfo_trig_by_timer_en) 4195 goto out; 4196 rtw89_leave_ps_mode(rtwdev); 4197 rtw89_phy_cfo_dm(rtwdev); 4198 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 4199 msecs_to_jiffies(cfo->cfo_timer_ms)); 4200 out: 4201 mutex_unlock(&rtwdev->mutex); 4202 } 4203 4204 static void rtw89_phy_cfo_start_work(struct rtw89_dev *rtwdev) 4205 { 4206 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4207 4208 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->cfo_track_work, 4209 msecs_to_jiffies(cfo->cfo_timer_ms)); 4210 } 4211 4212 void rtw89_phy_cfo_track(struct rtw89_dev *rtwdev) 4213 { 4214 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4215 struct rtw89_traffic_stats *stats = &rtwdev->stats; 4216 bool is_ul_ofdma = false, ofdma_acc_en = false; 4217 4218 if (stats->rx_tf_periodic > CFO_TF_CNT_TH) 4219 is_ul_ofdma = true; 4220 if (cfo->cfo_ul_ofdma_acc_mode == RTW89_CFO_UL_OFDMA_ACC_ENABLE && 4221 is_ul_ofdma) 4222 ofdma_acc_en = true; 4223 4224 switch (cfo->phy_cfo_status) { 4225 case RTW89_PHY_DCFO_STATE_NORMAL: 4226 if (stats->tx_throughput >= CFO_TP_UPPER) { 4227 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_ENHANCE; 4228 cfo->cfo_trig_by_timer_en = true; 4229 cfo->cfo_timer_ms = CFO_COMP_PERIOD; 4230 rtw89_phy_cfo_start_work(rtwdev); 4231 } 4232 break; 4233 case RTW89_PHY_DCFO_STATE_ENHANCE: 4234 if (stats->tx_throughput <= CFO_TP_LOWER) 4235 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 4236 else if (ofdma_acc_en && 4237 cfo->phy_cfo_trk_cnt >= CFO_PERIOD_CNT) 4238 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_HOLD; 4239 else 4240 cfo->phy_cfo_trk_cnt++; 4241 4242 if (cfo->phy_cfo_status == RTW89_PHY_DCFO_STATE_NORMAL) { 4243 cfo->phy_cfo_trk_cnt = 0; 4244 cfo->cfo_trig_by_timer_en = false; 4245 } 4246 break; 4247 case RTW89_PHY_DCFO_STATE_HOLD: 4248 if (stats->tx_throughput <= CFO_TP_LOWER) { 4249 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 4250 cfo->phy_cfo_trk_cnt = 0; 4251 cfo->cfo_trig_by_timer_en = false; 4252 } else { 4253 cfo->phy_cfo_trk_cnt++; 4254 } 4255 break; 4256 default: 4257 cfo->phy_cfo_status = RTW89_PHY_DCFO_STATE_NORMAL; 4258 cfo->phy_cfo_trk_cnt = 0; 4259 break; 4260 } 4261 rtw89_debug(rtwdev, RTW89_DBG_CFO, 4262 "[CFO]WatchDog tp=%d,state=%d,timer_en=%d,trk_cnt=%d,thermal=%ld\n", 4263 stats->tx_throughput, cfo->phy_cfo_status, 4264 cfo->cfo_trig_by_timer_en, cfo->phy_cfo_trk_cnt, 4265 ewma_thermal_read(&rtwdev->phystat.avg_thermal[0])); 4266 if (cfo->cfo_trig_by_timer_en) 4267 return; 4268 rtw89_phy_cfo_dm(rtwdev); 4269 } 4270 4271 void rtw89_phy_cfo_parse(struct rtw89_dev *rtwdev, s16 cfo_val, 4272 struct rtw89_rx_phy_ppdu *phy_ppdu) 4273 { 4274 struct rtw89_cfo_tracking_info *cfo = &rtwdev->cfo_tracking; 4275 u8 macid = phy_ppdu->mac_id; 4276 4277 if (macid >= CFO_TRACK_MAX_USER) { 4278 rtw89_warn(rtwdev, "mac_id %d is out of range\n", macid); 4279 return; 4280 } 4281 4282 cfo->cfo_tail[macid] += cfo_val; 4283 cfo->cfo_cnt[macid]++; 4284 cfo->packet_count++; 4285 } 4286 4287 void rtw89_phy_ul_tb_assoc(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4288 { 4289 const struct rtw89_chip_info *chip = rtwdev->chip; 4290 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, 4291 rtwvif->chanctx_idx); 4292 struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 4293 4294 if (!chip->ul_tb_waveform_ctrl) 4295 return; 4296 4297 rtwvif->def_tri_idx = 4298 rtw89_phy_read32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG); 4299 4300 if (chip->chip_id == RTL8852B && rtwdev->hal.cv > CHIP_CBV) 4301 rtwvif->dyn_tb_bedge_en = false; 4302 else if (chan->band_type >= RTW89_BAND_5G && 4303 chan->band_width >= RTW89_CHANNEL_WIDTH_40) 4304 rtwvif->dyn_tb_bedge_en = true; 4305 else 4306 rtwvif->dyn_tb_bedge_en = false; 4307 4308 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4309 "[ULTB] def_if_bandedge=%d, def_tri_idx=%d\n", 4310 ul_tb_info->def_if_bandedge, rtwvif->def_tri_idx); 4311 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4312 "[ULTB] dyn_tb_begde_en=%d, dyn_tb_tri_en=%d\n", 4313 rtwvif->dyn_tb_bedge_en, ul_tb_info->dyn_tb_tri_en); 4314 } 4315 4316 struct rtw89_phy_ul_tb_check_data { 4317 bool valid; 4318 bool high_tf_client; 4319 bool low_tf_client; 4320 bool dyn_tb_bedge_en; 4321 u8 def_tri_idx; 4322 }; 4323 4324 struct rtw89_phy_power_diff { 4325 u32 q_00; 4326 u32 q_11; 4327 u32 q_matrix_en; 4328 u32 ultb_1t_norm_160; 4329 u32 ultb_2t_norm_160; 4330 u32 com1_norm_1sts; 4331 u32 com2_resp_1sts_path; 4332 }; 4333 4334 static void rtw89_phy_ofdma_power_diff(struct rtw89_dev *rtwdev, 4335 struct rtw89_vif *rtwvif) 4336 { 4337 static const struct rtw89_phy_power_diff table[2] = { 4338 {0x0, 0x0, 0x0, 0x0, 0xf4, 0x3, 0x3}, 4339 {0xb50, 0xb50, 0x1, 0xc, 0x0, 0x1, 0x1}, 4340 }; 4341 const struct rtw89_phy_power_diff *param; 4342 u32 reg; 4343 4344 if (!rtwdev->chip->ul_tb_pwr_diff) 4345 return; 4346 4347 if (rtwvif->pwr_diff_en == rtwvif->pre_pwr_diff_en) { 4348 rtwvif->pwr_diff_en = false; 4349 return; 4350 } 4351 4352 rtwvif->pre_pwr_diff_en = rtwvif->pwr_diff_en; 4353 param = &table[rtwvif->pwr_diff_en]; 4354 4355 rtw89_phy_write32_mask(rtwdev, R_Q_MATRIX_00, B_Q_MATRIX_00_REAL, 4356 param->q_00); 4357 rtw89_phy_write32_mask(rtwdev, R_Q_MATRIX_11, B_Q_MATRIX_11_REAL, 4358 param->q_11); 4359 rtw89_phy_write32_mask(rtwdev, R_CUSTOMIZE_Q_MATRIX, 4360 B_CUSTOMIZE_Q_MATRIX_EN, param->q_matrix_en); 4361 4362 reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PWR_UL_TB_1T, rtwvif->mac_idx); 4363 rtw89_write32_mask(rtwdev, reg, B_AX_PWR_UL_TB_1T_NORM_BW160, 4364 param->ultb_1t_norm_160); 4365 4366 reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PWR_UL_TB_2T, rtwvif->mac_idx); 4367 rtw89_write32_mask(rtwdev, reg, B_AX_PWR_UL_TB_2T_NORM_BW160, 4368 param->ultb_2t_norm_160); 4369 4370 reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PATH_COM1, rtwvif->mac_idx); 4371 rtw89_write32_mask(rtwdev, reg, B_AX_PATH_COM1_NORM_1STS, 4372 param->com1_norm_1sts); 4373 4374 reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PATH_COM2, rtwvif->mac_idx); 4375 rtw89_write32_mask(rtwdev, reg, B_AX_PATH_COM2_RESP_1STS_PATH, 4376 param->com2_resp_1sts_path); 4377 } 4378 4379 static 4380 void rtw89_phy_ul_tb_ctrl_check(struct rtw89_dev *rtwdev, 4381 struct rtw89_vif *rtwvif, 4382 struct rtw89_phy_ul_tb_check_data *ul_tb_data) 4383 { 4384 struct rtw89_traffic_stats *stats = &rtwdev->stats; 4385 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 4386 4387 if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION) 4388 return; 4389 4390 if (!vif->cfg.assoc) 4391 return; 4392 4393 if (rtwdev->chip->ul_tb_waveform_ctrl) { 4394 if (stats->rx_tf_periodic > UL_TB_TF_CNT_L2H_TH) 4395 ul_tb_data->high_tf_client = true; 4396 else if (stats->rx_tf_periodic < UL_TB_TF_CNT_H2L_TH) 4397 ul_tb_data->low_tf_client = true; 4398 4399 ul_tb_data->valid = true; 4400 ul_tb_data->def_tri_idx = rtwvif->def_tri_idx; 4401 ul_tb_data->dyn_tb_bedge_en = rtwvif->dyn_tb_bedge_en; 4402 } 4403 4404 rtw89_phy_ofdma_power_diff(rtwdev, rtwvif); 4405 } 4406 4407 static void rtw89_phy_ul_tb_waveform_ctrl(struct rtw89_dev *rtwdev, 4408 struct rtw89_phy_ul_tb_check_data *ul_tb_data) 4409 { 4410 struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 4411 4412 if (!rtwdev->chip->ul_tb_waveform_ctrl) 4413 return; 4414 4415 if (ul_tb_data->dyn_tb_bedge_en) { 4416 if (ul_tb_data->high_tf_client) { 4417 rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 0); 4418 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4419 "[ULTB] Turn off if_bandedge\n"); 4420 } else if (ul_tb_data->low_tf_client) { 4421 rtw89_phy_write32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN, 4422 ul_tb_info->def_if_bandedge); 4423 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4424 "[ULTB] Set to default if_bandedge = %d\n", 4425 ul_tb_info->def_if_bandedge); 4426 } 4427 } 4428 4429 if (ul_tb_info->dyn_tb_tri_en) { 4430 if (ul_tb_data->high_tf_client) { 4431 rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 4432 B_TXSHAPE_TRIANGULAR_CFG, 0); 4433 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4434 "[ULTB] Turn off Tx triangle\n"); 4435 } else if (ul_tb_data->low_tf_client) { 4436 rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, 4437 B_TXSHAPE_TRIANGULAR_CFG, 4438 ul_tb_data->def_tri_idx); 4439 rtw89_debug(rtwdev, RTW89_DBG_UL_TB, 4440 "[ULTB] Set to default tx_shap_idx = %d\n", 4441 ul_tb_data->def_tri_idx); 4442 } 4443 } 4444 } 4445 4446 void rtw89_phy_ul_tb_ctrl_track(struct rtw89_dev *rtwdev) 4447 { 4448 const struct rtw89_chip_info *chip = rtwdev->chip; 4449 struct rtw89_phy_ul_tb_check_data ul_tb_data = {}; 4450 struct rtw89_vif *rtwvif; 4451 4452 if (!chip->ul_tb_waveform_ctrl && !chip->ul_tb_pwr_diff) 4453 return; 4454 4455 if (rtwdev->total_sta_assoc != 1) 4456 return; 4457 4458 rtw89_for_each_rtwvif(rtwdev, rtwvif) 4459 rtw89_phy_ul_tb_ctrl_check(rtwdev, rtwvif, &ul_tb_data); 4460 4461 if (!ul_tb_data.valid) 4462 return; 4463 4464 rtw89_phy_ul_tb_waveform_ctrl(rtwdev, &ul_tb_data); 4465 } 4466 4467 static void rtw89_phy_ul_tb_info_init(struct rtw89_dev *rtwdev) 4468 { 4469 const struct rtw89_chip_info *chip = rtwdev->chip; 4470 struct rtw89_phy_ul_tb_info *ul_tb_info = &rtwdev->ul_tb_info; 4471 4472 if (!chip->ul_tb_waveform_ctrl) 4473 return; 4474 4475 ul_tb_info->dyn_tb_tri_en = true; 4476 ul_tb_info->def_if_bandedge = 4477 rtw89_phy_read32_mask(rtwdev, R_BANDEDGE, B_BANDEDGE_EN); 4478 } 4479 4480 static 4481 void rtw89_phy_antdiv_sts_instance_reset(struct rtw89_antdiv_stats *antdiv_sts) 4482 { 4483 ewma_rssi_init(&antdiv_sts->cck_rssi_avg); 4484 ewma_rssi_init(&antdiv_sts->ofdm_rssi_avg); 4485 ewma_rssi_init(&antdiv_sts->non_legacy_rssi_avg); 4486 antdiv_sts->pkt_cnt_cck = 0; 4487 antdiv_sts->pkt_cnt_ofdm = 0; 4488 antdiv_sts->pkt_cnt_non_legacy = 0; 4489 antdiv_sts->evm = 0; 4490 } 4491 4492 static void rtw89_phy_antdiv_sts_instance_add(struct rtw89_dev *rtwdev, 4493 struct rtw89_rx_phy_ppdu *phy_ppdu, 4494 struct rtw89_antdiv_stats *stats) 4495 { 4496 if (rtw89_get_data_rate_mode(rtwdev, phy_ppdu->rate) == DATA_RATE_MODE_NON_HT) { 4497 if (phy_ppdu->rate < RTW89_HW_RATE_OFDM6) { 4498 ewma_rssi_add(&stats->cck_rssi_avg, phy_ppdu->rssi_avg); 4499 stats->pkt_cnt_cck++; 4500 } else { 4501 ewma_rssi_add(&stats->ofdm_rssi_avg, phy_ppdu->rssi_avg); 4502 stats->pkt_cnt_ofdm++; 4503 stats->evm += phy_ppdu->ofdm.evm_min; 4504 } 4505 } else { 4506 ewma_rssi_add(&stats->non_legacy_rssi_avg, phy_ppdu->rssi_avg); 4507 stats->pkt_cnt_non_legacy++; 4508 stats->evm += phy_ppdu->ofdm.evm_min; 4509 } 4510 } 4511 4512 static u8 rtw89_phy_antdiv_sts_instance_get_rssi(struct rtw89_antdiv_stats *stats) 4513 { 4514 if (stats->pkt_cnt_non_legacy >= stats->pkt_cnt_cck && 4515 stats->pkt_cnt_non_legacy >= stats->pkt_cnt_ofdm) 4516 return ewma_rssi_read(&stats->non_legacy_rssi_avg); 4517 else if (stats->pkt_cnt_ofdm >= stats->pkt_cnt_cck && 4518 stats->pkt_cnt_ofdm >= stats->pkt_cnt_non_legacy) 4519 return ewma_rssi_read(&stats->ofdm_rssi_avg); 4520 else 4521 return ewma_rssi_read(&stats->cck_rssi_avg); 4522 } 4523 4524 static u8 rtw89_phy_antdiv_sts_instance_get_evm(struct rtw89_antdiv_stats *stats) 4525 { 4526 return phy_div(stats->evm, stats->pkt_cnt_non_legacy + stats->pkt_cnt_ofdm); 4527 } 4528 4529 void rtw89_phy_antdiv_parse(struct rtw89_dev *rtwdev, 4530 struct rtw89_rx_phy_ppdu *phy_ppdu) 4531 { 4532 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4533 struct rtw89_hal *hal = &rtwdev->hal; 4534 4535 if (!hal->ant_diversity || hal->ant_diversity_fixed) 4536 return; 4537 4538 rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->target_stats); 4539 4540 if (!antdiv->get_stats) 4541 return; 4542 4543 if (hal->antenna_rx == RF_A) 4544 rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->main_stats); 4545 else if (hal->antenna_rx == RF_B) 4546 rtw89_phy_antdiv_sts_instance_add(rtwdev, phy_ppdu, &antdiv->aux_stats); 4547 } 4548 4549 static void rtw89_phy_antdiv_reg_init(struct rtw89_dev *rtwdev) 4550 { 4551 rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_ANT_TRAIN_EN, 4552 0x0, RTW89_PHY_0); 4553 rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_TX_ANT_SEL, 4554 0x0, RTW89_PHY_0); 4555 4556 rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_TRSW_TX_EXTEND, 4557 0x0, RTW89_PHY_0); 4558 rtw89_phy_write32_idx(rtwdev, R_P0_ANT_SW, B_P0_HW_ANTSW_DIS_BY_GNT_BT, 4559 0x0, RTW89_PHY_0); 4560 4561 rtw89_phy_write32_idx(rtwdev, R_P0_TRSW, B_P0_BT_FORCE_ANTIDX_EN, 4562 0x0, RTW89_PHY_0); 4563 4564 rtw89_phy_write32_idx(rtwdev, R_RFSW_CTRL_ANT0_BASE, B_RFSW_CTRL_ANT_MAPPING, 4565 0x0100, RTW89_PHY_0); 4566 4567 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_BTG_TRX, 4568 0x1, RTW89_PHY_0); 4569 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_HW_CTRL, 4570 0x0, RTW89_PHY_0); 4571 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_2G, 4572 0x0, RTW89_PHY_0); 4573 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_SW_5G, 4574 0x0, RTW89_PHY_0); 4575 } 4576 4577 static void rtw89_phy_antdiv_sts_reset(struct rtw89_dev *rtwdev) 4578 { 4579 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4580 4581 rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats); 4582 rtw89_phy_antdiv_sts_instance_reset(&antdiv->main_stats); 4583 rtw89_phy_antdiv_sts_instance_reset(&antdiv->aux_stats); 4584 } 4585 4586 static void rtw89_phy_antdiv_init(struct rtw89_dev *rtwdev) 4587 { 4588 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 4589 struct rtw89_hal *hal = &rtwdev->hal; 4590 4591 if (!hal->ant_diversity) 4592 return; 4593 4594 antdiv->get_stats = false; 4595 antdiv->rssi_pre = 0; 4596 rtw89_phy_antdiv_sts_reset(rtwdev); 4597 rtw89_phy_antdiv_reg_init(rtwdev); 4598 } 4599 4600 static void rtw89_phy_stat_thermal_update(struct rtw89_dev *rtwdev) 4601 { 4602 struct rtw89_phy_stat *phystat = &rtwdev->phystat; 4603 int i; 4604 u8 th; 4605 4606 for (i = 0; i < rtwdev->chip->rf_path_num; i++) { 4607 th = rtw89_chip_get_thermal(rtwdev, i); 4608 if (th) 4609 ewma_thermal_add(&phystat->avg_thermal[i], th); 4610 4611 rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, 4612 "path(%d) thermal cur=%u avg=%ld", i, th, 4613 ewma_thermal_read(&phystat->avg_thermal[i])); 4614 } 4615 } 4616 4617 struct rtw89_phy_iter_rssi_data { 4618 struct rtw89_dev *rtwdev; 4619 struct rtw89_phy_ch_info *ch_info; 4620 bool rssi_changed; 4621 }; 4622 4623 static void rtw89_phy_stat_rssi_update_iter(void *data, 4624 struct ieee80211_sta *sta) 4625 { 4626 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 4627 struct rtw89_phy_iter_rssi_data *rssi_data = 4628 (struct rtw89_phy_iter_rssi_data *)data; 4629 struct rtw89_phy_ch_info *ch_info = rssi_data->ch_info; 4630 unsigned long rssi_curr; 4631 4632 rssi_curr = ewma_rssi_read(&rtwsta->avg_rssi); 4633 4634 if (rssi_curr < ch_info->rssi_min) { 4635 ch_info->rssi_min = rssi_curr; 4636 ch_info->rssi_min_macid = rtwsta->mac_id; 4637 } 4638 4639 if (rtwsta->prev_rssi == 0) { 4640 rtwsta->prev_rssi = rssi_curr; 4641 } else if (abs((int)rtwsta->prev_rssi - (int)rssi_curr) > (3 << RSSI_FACTOR)) { 4642 rtwsta->prev_rssi = rssi_curr; 4643 rssi_data->rssi_changed = true; 4644 } 4645 } 4646 4647 static void rtw89_phy_stat_rssi_update(struct rtw89_dev *rtwdev) 4648 { 4649 struct rtw89_phy_iter_rssi_data rssi_data = {0}; 4650 4651 rssi_data.rtwdev = rtwdev; 4652 rssi_data.ch_info = &rtwdev->ch_info; 4653 rssi_data.ch_info->rssi_min = U8_MAX; 4654 ieee80211_iterate_stations_atomic(rtwdev->hw, 4655 rtw89_phy_stat_rssi_update_iter, 4656 &rssi_data); 4657 if (rssi_data.rssi_changed) 4658 rtw89_btc_ntfy_wl_sta(rtwdev); 4659 } 4660 4661 static void rtw89_phy_stat_init(struct rtw89_dev *rtwdev) 4662 { 4663 struct rtw89_phy_stat *phystat = &rtwdev->phystat; 4664 int i; 4665 4666 for (i = 0; i < rtwdev->chip->rf_path_num; i++) 4667 ewma_thermal_init(&phystat->avg_thermal[i]); 4668 4669 rtw89_phy_stat_thermal_update(rtwdev); 4670 4671 memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 4672 memset(&phystat->last_pkt_stat, 0, sizeof(phystat->last_pkt_stat)); 4673 } 4674 4675 void rtw89_phy_stat_track(struct rtw89_dev *rtwdev) 4676 { 4677 struct rtw89_phy_stat *phystat = &rtwdev->phystat; 4678 4679 rtw89_phy_stat_thermal_update(rtwdev); 4680 rtw89_phy_stat_rssi_update(rtwdev); 4681 4682 phystat->last_pkt_stat = phystat->cur_pkt_stat; 4683 memset(&phystat->cur_pkt_stat, 0, sizeof(phystat->cur_pkt_stat)); 4684 } 4685 4686 static u16 rtw89_phy_ccx_us_to_idx(struct rtw89_dev *rtwdev, u32 time_us) 4687 { 4688 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4689 4690 return time_us >> (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 4691 } 4692 4693 static u32 rtw89_phy_ccx_idx_to_us(struct rtw89_dev *rtwdev, u16 idx) 4694 { 4695 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4696 4697 return idx << (ilog2(CCX_US_BASE_RATIO) + env->ccx_unit_idx); 4698 } 4699 4700 static void rtw89_phy_ccx_top_setting_init(struct rtw89_dev *rtwdev) 4701 { 4702 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 4703 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4704 const struct rtw89_ccx_regs *ccx = phy->ccx; 4705 4706 env->ccx_manual_ctrl = false; 4707 env->ccx_ongoing = false; 4708 env->ccx_rac_lv = RTW89_RAC_RELEASE; 4709 env->ccx_period = 0; 4710 env->ccx_unit_idx = RTW89_CCX_32_US; 4711 4712 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->en_mask, 1); 4713 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->trig_opt_mask, 1); 4714 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 1); 4715 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->edcca_opt_mask, 4716 RTW89_CCX_EDCCA_BW20_0); 4717 } 4718 4719 static u16 rtw89_phy_ccx_get_report(struct rtw89_dev *rtwdev, u16 report, 4720 u16 score) 4721 { 4722 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4723 u32 numer = 0; 4724 u16 ret = 0; 4725 4726 numer = report * score + (env->ccx_period >> 1); 4727 if (env->ccx_period) 4728 ret = numer / env->ccx_period; 4729 4730 return ret >= score ? score - 1 : ret; 4731 } 4732 4733 static void rtw89_phy_ccx_ms_to_period_unit(struct rtw89_dev *rtwdev, 4734 u16 time_ms, u32 *period, 4735 u32 *unit_idx) 4736 { 4737 u32 idx; 4738 u8 quotient; 4739 4740 if (time_ms >= CCX_MAX_PERIOD) 4741 time_ms = CCX_MAX_PERIOD; 4742 4743 quotient = CCX_MAX_PERIOD_UNIT * time_ms / CCX_MAX_PERIOD; 4744 4745 if (quotient < 4) 4746 idx = RTW89_CCX_4_US; 4747 else if (quotient < 8) 4748 idx = RTW89_CCX_8_US; 4749 else if (quotient < 16) 4750 idx = RTW89_CCX_16_US; 4751 else 4752 idx = RTW89_CCX_32_US; 4753 4754 *unit_idx = idx; 4755 *period = (time_ms * MS_TO_4US_RATIO) >> idx; 4756 4757 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4758 "[Trigger Time] period:%d, unit_idx:%d\n", 4759 *period, *unit_idx); 4760 } 4761 4762 static void rtw89_phy_ccx_racing_release(struct rtw89_dev *rtwdev) 4763 { 4764 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4765 4766 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4767 "lv:(%d)->(0)\n", env->ccx_rac_lv); 4768 4769 env->ccx_ongoing = false; 4770 env->ccx_rac_lv = RTW89_RAC_RELEASE; 4771 env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 4772 } 4773 4774 static bool rtw89_phy_ifs_clm_th_update_check(struct rtw89_dev *rtwdev, 4775 struct rtw89_ccx_para_info *para) 4776 { 4777 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4778 bool is_update = env->ifs_clm_app != para->ifs_clm_app; 4779 u8 i = 0; 4780 u16 *ifs_th_l = env->ifs_clm_th_l; 4781 u16 *ifs_th_h = env->ifs_clm_th_h; 4782 u32 ifs_th0_us = 0, ifs_th_times = 0; 4783 u32 ifs_th_h_us[RTW89_IFS_CLM_NUM] = {0}; 4784 4785 if (!is_update) 4786 goto ifs_update_finished; 4787 4788 switch (para->ifs_clm_app) { 4789 case RTW89_IFS_CLM_INIT: 4790 case RTW89_IFS_CLM_BACKGROUND: 4791 case RTW89_IFS_CLM_ACS: 4792 case RTW89_IFS_CLM_DBG: 4793 case RTW89_IFS_CLM_DIG: 4794 case RTW89_IFS_CLM_TDMA_DIG: 4795 ifs_th0_us = IFS_CLM_TH0_UPPER; 4796 ifs_th_times = IFS_CLM_TH_MUL; 4797 break; 4798 case RTW89_IFS_CLM_DBG_MANUAL: 4799 ifs_th0_us = para->ifs_clm_manual_th0; 4800 ifs_th_times = para->ifs_clm_manual_th_times; 4801 break; 4802 default: 4803 break; 4804 } 4805 4806 /* Set sampling threshold for 4 different regions, unit in idx_cnt. 4807 * low[i] = high[i-1] + 1 4808 * high[i] = high[i-1] * ifs_th_times 4809 */ 4810 ifs_th_l[IFS_CLM_TH_START_IDX] = 0; 4811 ifs_th_h_us[IFS_CLM_TH_START_IDX] = ifs_th0_us; 4812 ifs_th_h[IFS_CLM_TH_START_IDX] = rtw89_phy_ccx_us_to_idx(rtwdev, 4813 ifs_th0_us); 4814 for (i = 1; i < RTW89_IFS_CLM_NUM; i++) { 4815 ifs_th_l[i] = ifs_th_h[i - 1] + 1; 4816 ifs_th_h_us[i] = ifs_th_h_us[i - 1] * ifs_th_times; 4817 ifs_th_h[i] = rtw89_phy_ccx_us_to_idx(rtwdev, ifs_th_h_us[i]); 4818 } 4819 4820 ifs_update_finished: 4821 if (!is_update) 4822 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4823 "No need to update IFS_TH\n"); 4824 4825 return is_update; 4826 } 4827 4828 static void rtw89_phy_ifs_clm_set_th_reg(struct rtw89_dev *rtwdev) 4829 { 4830 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 4831 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4832 const struct rtw89_ccx_regs *ccx = phy->ccx; 4833 u8 i = 0; 4834 4835 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_th_l_mask, 4836 env->ifs_clm_th_l[0]); 4837 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_th_l_mask, 4838 env->ifs_clm_th_l[1]); 4839 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_th_l_mask, 4840 env->ifs_clm_th_l[2]); 4841 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_th_l_mask, 4842 env->ifs_clm_th_l[3]); 4843 4844 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_th_h_mask, 4845 env->ifs_clm_th_h[0]); 4846 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_th_h_mask, 4847 env->ifs_clm_th_h[1]); 4848 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_th_h_mask, 4849 env->ifs_clm_th_h[2]); 4850 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_th_h_mask, 4851 env->ifs_clm_th_h[3]); 4852 4853 for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 4854 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4855 "Update IFS_T%d_th{low, high} : {%d, %d}\n", 4856 i + 1, env->ifs_clm_th_l[i], env->ifs_clm_th_h[i]); 4857 } 4858 4859 static void rtw89_phy_ifs_clm_setting_init(struct rtw89_dev *rtwdev) 4860 { 4861 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 4862 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4863 const struct rtw89_ccx_regs *ccx = phy->ccx; 4864 struct rtw89_ccx_para_info para = {0}; 4865 4866 env->ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 4867 env->ifs_clm_mntr_time = 0; 4868 4869 para.ifs_clm_app = RTW89_IFS_CLM_INIT; 4870 if (rtw89_phy_ifs_clm_th_update_check(rtwdev, ¶)) 4871 rtw89_phy_ifs_clm_set_th_reg(rtwdev); 4872 4873 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_collect_en_mask, true); 4874 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t1_addr, ccx->ifs_t1_en_mask, true); 4875 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t2_addr, ccx->ifs_t2_en_mask, true); 4876 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t3_addr, ccx->ifs_t3_en_mask, true); 4877 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_t4_addr, ccx->ifs_t4_en_mask, true); 4878 } 4879 4880 static int rtw89_phy_ccx_racing_ctrl(struct rtw89_dev *rtwdev, 4881 enum rtw89_env_racing_lv level) 4882 { 4883 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4884 int ret = 0; 4885 4886 if (level >= RTW89_RAC_MAX_NUM) { 4887 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4888 "[WARNING] Wrong LV=%d\n", level); 4889 return -EINVAL; 4890 } 4891 4892 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4893 "ccx_ongoing=%d, level:(%d)->(%d)\n", env->ccx_ongoing, 4894 env->ccx_rac_lv, level); 4895 4896 if (env->ccx_ongoing) { 4897 if (level <= env->ccx_rac_lv) 4898 ret = -EINVAL; 4899 else 4900 env->ccx_ongoing = false; 4901 } 4902 4903 if (ret == 0) 4904 env->ccx_rac_lv = level; 4905 4906 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "ccx racing success=%d\n", 4907 !ret); 4908 4909 return ret; 4910 } 4911 4912 static void rtw89_phy_ccx_trigger(struct rtw89_dev *rtwdev) 4913 { 4914 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 4915 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4916 const struct rtw89_ccx_regs *ccx = phy->ccx; 4917 4918 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_clm_cnt_clear_mask, 0); 4919 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 0); 4920 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, ccx->ifs_clm_cnt_clear_mask, 1); 4921 rtw89_phy_set_phy_regs(rtwdev, ccx->setting_addr, ccx->measurement_trig_mask, 1); 4922 4923 env->ccx_ongoing = true; 4924 } 4925 4926 static void rtw89_phy_ifs_clm_get_utility(struct rtw89_dev *rtwdev) 4927 { 4928 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4929 u8 i = 0; 4930 u32 res = 0; 4931 4932 env->ifs_clm_tx_ratio = 4933 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_tx, PERCENT); 4934 env->ifs_clm_edcca_excl_cca_ratio = 4935 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_edcca_excl_cca, 4936 PERCENT); 4937 env->ifs_clm_cck_fa_ratio = 4938 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERCENT); 4939 env->ifs_clm_ofdm_fa_ratio = 4940 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERCENT); 4941 env->ifs_clm_cck_cca_excl_fa_ratio = 4942 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckcca_excl_fa, 4943 PERCENT); 4944 env->ifs_clm_ofdm_cca_excl_fa_ratio = 4945 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmcca_excl_fa, 4946 PERCENT); 4947 env->ifs_clm_cck_fa_permil = 4948 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_cckfa, PERMIL); 4949 env->ifs_clm_ofdm_fa_permil = 4950 rtw89_phy_ccx_get_report(rtwdev, env->ifs_clm_ofdmfa, PERMIL); 4951 4952 for (i = 0; i < RTW89_IFS_CLM_NUM; i++) { 4953 if (env->ifs_clm_his[i] > ENV_MNTR_IFSCLM_HIS_MAX) { 4954 env->ifs_clm_ifs_avg[i] = ENV_MNTR_FAIL_DWORD; 4955 } else { 4956 env->ifs_clm_ifs_avg[i] = 4957 rtw89_phy_ccx_idx_to_us(rtwdev, 4958 env->ifs_clm_avg[i]); 4959 } 4960 4961 res = rtw89_phy_ccx_idx_to_us(rtwdev, env->ifs_clm_cca[i]); 4962 res += env->ifs_clm_his[i] >> 1; 4963 if (env->ifs_clm_his[i]) 4964 res /= env->ifs_clm_his[i]; 4965 else 4966 res = 0; 4967 env->ifs_clm_cca_avg[i] = res; 4968 } 4969 4970 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4971 "IFS-CLM ratio {Tx, EDCCA_exclu_cca} = {%d, %d}\n", 4972 env->ifs_clm_tx_ratio, env->ifs_clm_edcca_excl_cca_ratio); 4973 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4974 "IFS-CLM FA ratio {CCK, OFDM} = {%d, %d}\n", 4975 env->ifs_clm_cck_fa_ratio, env->ifs_clm_ofdm_fa_ratio); 4976 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4977 "IFS-CLM FA permil {CCK, OFDM} = {%d, %d}\n", 4978 env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil); 4979 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4980 "IFS-CLM CCA_exclu_FA ratio {CCK, OFDM} = {%d, %d}\n", 4981 env->ifs_clm_cck_cca_excl_fa_ratio, 4982 env->ifs_clm_ofdm_cca_excl_fa_ratio); 4983 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 4984 "Time:[his, ifs_avg(us), cca_avg(us)]\n"); 4985 for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 4986 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "T%d:[%d, %d, %d]\n", 4987 i + 1, env->ifs_clm_his[i], env->ifs_clm_ifs_avg[i], 4988 env->ifs_clm_cca_avg[i]); 4989 } 4990 4991 static bool rtw89_phy_ifs_clm_get_result(struct rtw89_dev *rtwdev) 4992 { 4993 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 4994 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 4995 const struct rtw89_ccx_regs *ccx = phy->ccx; 4996 u8 i = 0; 4997 4998 if (rtw89_phy_read32_mask(rtwdev, ccx->ifs_total_addr, 4999 ccx->ifs_cnt_done_mask) == 0) { 5000 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5001 "Get IFS_CLM report Fail\n"); 5002 return false; 5003 } 5004 5005 env->ifs_clm_tx = 5006 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_tx_cnt_addr, 5007 ccx->ifs_clm_tx_cnt_msk); 5008 env->ifs_clm_edcca_excl_cca = 5009 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_tx_cnt_addr, 5010 ccx->ifs_clm_edcca_excl_cca_fa_mask); 5011 env->ifs_clm_cckcca_excl_fa = 5012 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_cca_addr, 5013 ccx->ifs_clm_cckcca_excl_fa_mask); 5014 env->ifs_clm_ofdmcca_excl_fa = 5015 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_cca_addr, 5016 ccx->ifs_clm_ofdmcca_excl_fa_mask); 5017 env->ifs_clm_cckfa = 5018 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_fa_addr, 5019 ccx->ifs_clm_cck_fa_mask); 5020 env->ifs_clm_ofdmfa = 5021 rtw89_phy_read32_mask(rtwdev, ccx->ifs_clm_fa_addr, 5022 ccx->ifs_clm_ofdm_fa_mask); 5023 5024 env->ifs_clm_his[0] = 5025 rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr, 5026 ccx->ifs_t1_his_mask); 5027 env->ifs_clm_his[1] = 5028 rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr, 5029 ccx->ifs_t2_his_mask); 5030 env->ifs_clm_his[2] = 5031 rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr, 5032 ccx->ifs_t3_his_mask); 5033 env->ifs_clm_his[3] = 5034 rtw89_phy_read32_mask(rtwdev, ccx->ifs_his_addr, 5035 ccx->ifs_t4_his_mask); 5036 5037 env->ifs_clm_avg[0] = 5038 rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_l_addr, 5039 ccx->ifs_t1_avg_mask); 5040 env->ifs_clm_avg[1] = 5041 rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_l_addr, 5042 ccx->ifs_t2_avg_mask); 5043 env->ifs_clm_avg[2] = 5044 rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_h_addr, 5045 ccx->ifs_t3_avg_mask); 5046 env->ifs_clm_avg[3] = 5047 rtw89_phy_read32_mask(rtwdev, ccx->ifs_avg_h_addr, 5048 ccx->ifs_t4_avg_mask); 5049 5050 env->ifs_clm_cca[0] = 5051 rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_l_addr, 5052 ccx->ifs_t1_cca_mask); 5053 env->ifs_clm_cca[1] = 5054 rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_l_addr, 5055 ccx->ifs_t2_cca_mask); 5056 env->ifs_clm_cca[2] = 5057 rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_h_addr, 5058 ccx->ifs_t3_cca_mask); 5059 env->ifs_clm_cca[3] = 5060 rtw89_phy_read32_mask(rtwdev, ccx->ifs_cca_h_addr, 5061 ccx->ifs_t4_cca_mask); 5062 5063 env->ifs_clm_total_ifs = 5064 rtw89_phy_read32_mask(rtwdev, ccx->ifs_total_addr, 5065 ccx->ifs_total_mask); 5066 5067 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "IFS-CLM total_ifs = %d\n", 5068 env->ifs_clm_total_ifs); 5069 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5070 "{Tx, EDCCA_exclu_cca} = {%d, %d}\n", 5071 env->ifs_clm_tx, env->ifs_clm_edcca_excl_cca); 5072 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5073 "IFS-CLM FA{CCK, OFDM} = {%d, %d}\n", 5074 env->ifs_clm_cckfa, env->ifs_clm_ofdmfa); 5075 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5076 "IFS-CLM CCA_exclu_FA{CCK, OFDM} = {%d, %d}\n", 5077 env->ifs_clm_cckcca_excl_fa, env->ifs_clm_ofdmcca_excl_fa); 5078 5079 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, "Time:[his, avg, cca]\n"); 5080 for (i = 0; i < RTW89_IFS_CLM_NUM; i++) 5081 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5082 "T%d:[%d, %d, %d]\n", i + 1, env->ifs_clm_his[i], 5083 env->ifs_clm_avg[i], env->ifs_clm_cca[i]); 5084 5085 rtw89_phy_ifs_clm_get_utility(rtwdev); 5086 5087 return true; 5088 } 5089 5090 static int rtw89_phy_ifs_clm_set(struct rtw89_dev *rtwdev, 5091 struct rtw89_ccx_para_info *para) 5092 { 5093 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 5094 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 5095 const struct rtw89_ccx_regs *ccx = phy->ccx; 5096 u32 period = 0; 5097 u32 unit_idx = 0; 5098 5099 if (para->mntr_time == 0) { 5100 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5101 "[WARN] MNTR_TIME is 0\n"); 5102 return -EINVAL; 5103 } 5104 5105 if (rtw89_phy_ccx_racing_ctrl(rtwdev, para->rac_lv)) 5106 return -EINVAL; 5107 5108 if (para->mntr_time != env->ifs_clm_mntr_time) { 5109 rtw89_phy_ccx_ms_to_period_unit(rtwdev, para->mntr_time, 5110 &period, &unit_idx); 5111 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, 5112 ccx->ifs_clm_period_mask, period); 5113 rtw89_phy_set_phy_regs(rtwdev, ccx->ifs_cnt_addr, 5114 ccx->ifs_clm_cnt_unit_mask, 5115 unit_idx); 5116 5117 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5118 "Update IFS-CLM time ((%d)) -> ((%d))\n", 5119 env->ifs_clm_mntr_time, para->mntr_time); 5120 5121 env->ifs_clm_mntr_time = para->mntr_time; 5122 env->ccx_period = (u16)period; 5123 env->ccx_unit_idx = (u8)unit_idx; 5124 } 5125 5126 if (rtw89_phy_ifs_clm_th_update_check(rtwdev, para)) { 5127 env->ifs_clm_app = para->ifs_clm_app; 5128 rtw89_phy_ifs_clm_set_th_reg(rtwdev); 5129 } 5130 5131 return 0; 5132 } 5133 5134 void rtw89_phy_env_monitor_track(struct rtw89_dev *rtwdev) 5135 { 5136 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 5137 struct rtw89_ccx_para_info para = {0}; 5138 u8 chk_result = RTW89_PHY_ENV_MON_CCX_FAIL; 5139 5140 env->ccx_watchdog_result = RTW89_PHY_ENV_MON_CCX_FAIL; 5141 if (env->ccx_manual_ctrl) { 5142 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5143 "CCX in manual ctrl\n"); 5144 return; 5145 } 5146 5147 /* only ifs_clm for now */ 5148 if (rtw89_phy_ifs_clm_get_result(rtwdev)) 5149 env->ccx_watchdog_result |= RTW89_PHY_ENV_MON_IFS_CLM; 5150 5151 rtw89_phy_ccx_racing_release(rtwdev); 5152 para.mntr_time = 1900; 5153 para.rac_lv = RTW89_RAC_LV_1; 5154 para.ifs_clm_app = RTW89_IFS_CLM_BACKGROUND; 5155 5156 if (rtw89_phy_ifs_clm_set(rtwdev, ¶) == 0) 5157 chk_result |= RTW89_PHY_ENV_MON_IFS_CLM; 5158 if (chk_result) 5159 rtw89_phy_ccx_trigger(rtwdev); 5160 5161 rtw89_debug(rtwdev, RTW89_DBG_PHY_TRACK, 5162 "get_result=0x%x, chk_result:0x%x\n", 5163 env->ccx_watchdog_result, chk_result); 5164 } 5165 5166 static bool rtw89_physts_ie_page_valid(enum rtw89_phy_status_bitmap *ie_page) 5167 { 5168 if (*ie_page >= RTW89_PHYSTS_BITMAP_NUM || 5169 *ie_page == RTW89_RSVD_9) 5170 return false; 5171 else if (*ie_page > RTW89_RSVD_9) 5172 *ie_page -= 1; 5173 5174 return true; 5175 } 5176 5177 static u32 rtw89_phy_get_ie_bitmap_addr(enum rtw89_phy_status_bitmap ie_page) 5178 { 5179 static const u8 ie_page_shift = 2; 5180 5181 return R_PHY_STS_BITMAP_ADDR_START + (ie_page << ie_page_shift); 5182 } 5183 5184 static u32 rtw89_physts_get_ie_bitmap(struct rtw89_dev *rtwdev, 5185 enum rtw89_phy_status_bitmap ie_page) 5186 { 5187 u32 addr; 5188 5189 if (!rtw89_physts_ie_page_valid(&ie_page)) 5190 return 0; 5191 5192 addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 5193 5194 return rtw89_phy_read32(rtwdev, addr); 5195 } 5196 5197 static void rtw89_physts_set_ie_bitmap(struct rtw89_dev *rtwdev, 5198 enum rtw89_phy_status_bitmap ie_page, 5199 u32 val) 5200 { 5201 const struct rtw89_chip_info *chip = rtwdev->chip; 5202 u32 addr; 5203 5204 if (!rtw89_physts_ie_page_valid(&ie_page)) 5205 return; 5206 5207 if (chip->chip_id == RTL8852A) 5208 val &= B_PHY_STS_BITMAP_MSK_52A; 5209 5210 addr = rtw89_phy_get_ie_bitmap_addr(ie_page); 5211 rtw89_phy_write32(rtwdev, addr, val); 5212 } 5213 5214 static void rtw89_physts_enable_ie_bitmap(struct rtw89_dev *rtwdev, 5215 enum rtw89_phy_status_bitmap bitmap, 5216 enum rtw89_phy_status_ie_type ie, 5217 bool enable) 5218 { 5219 u32 val = rtw89_physts_get_ie_bitmap(rtwdev, bitmap); 5220 5221 if (enable) 5222 val |= BIT(ie); 5223 else 5224 val &= ~BIT(ie); 5225 5226 rtw89_physts_set_ie_bitmap(rtwdev, bitmap, val); 5227 } 5228 5229 static void rtw89_physts_enable_fail_report(struct rtw89_dev *rtwdev, 5230 bool enable, 5231 enum rtw89_phy_idx phy_idx) 5232 { 5233 const struct rtw89_phy_gen_def *phy = rtwdev->chip->phy_def; 5234 const struct rtw89_physts_regs *physts = phy->physts; 5235 5236 if (enable) { 5237 rtw89_phy_write32_clr(rtwdev, physts->setting_addr, 5238 physts->dis_trigger_fail_mask); 5239 rtw89_phy_write32_clr(rtwdev, physts->setting_addr, 5240 physts->dis_trigger_brk_mask); 5241 } else { 5242 rtw89_phy_write32_set(rtwdev, physts->setting_addr, 5243 physts->dis_trigger_fail_mask); 5244 rtw89_phy_write32_set(rtwdev, physts->setting_addr, 5245 physts->dis_trigger_brk_mask); 5246 } 5247 } 5248 5249 static void rtw89_physts_parsing_init(struct rtw89_dev *rtwdev) 5250 { 5251 u8 i; 5252 5253 rtw89_physts_enable_fail_report(rtwdev, false, RTW89_PHY_0); 5254 5255 for (i = 0; i < RTW89_PHYSTS_BITMAP_NUM; i++) { 5256 if (i >= RTW89_CCK_PKT) 5257 rtw89_physts_enable_ie_bitmap(rtwdev, i, 5258 RTW89_PHYSTS_IE09_FTR_0, 5259 true); 5260 if ((i >= RTW89_CCK_BRK && i <= RTW89_VHT_MU) || 5261 (i >= RTW89_RSVD_9 && i <= RTW89_CCK_PKT)) 5262 continue; 5263 rtw89_physts_enable_ie_bitmap(rtwdev, i, 5264 RTW89_PHYSTS_IE24_OFDM_TD_PATH_A, 5265 true); 5266 } 5267 rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_VHT_PKT, 5268 RTW89_PHYSTS_IE13_DL_MU_DEF, true); 5269 rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_HE_PKT, 5270 RTW89_PHYSTS_IE13_DL_MU_DEF, true); 5271 5272 /* force IE01 for channel index, only channel field is valid */ 5273 rtw89_physts_enable_ie_bitmap(rtwdev, RTW89_CCK_PKT, 5274 RTW89_PHYSTS_IE01_CMN_OFDM, true); 5275 } 5276 5277 static void rtw89_phy_dig_read_gain_table(struct rtw89_dev *rtwdev, int type) 5278 { 5279 const struct rtw89_chip_info *chip = rtwdev->chip; 5280 struct rtw89_dig_info *dig = &rtwdev->dig; 5281 const struct rtw89_phy_dig_gain_cfg *cfg; 5282 const char *msg; 5283 u8 i; 5284 s8 gain_base; 5285 s8 *gain_arr; 5286 u32 tmp; 5287 5288 switch (type) { 5289 case RTW89_DIG_GAIN_LNA_G: 5290 gain_arr = dig->lna_gain_g; 5291 gain_base = LNA0_GAIN; 5292 cfg = chip->dig_table->cfg_lna_g; 5293 msg = "lna_gain_g"; 5294 break; 5295 case RTW89_DIG_GAIN_TIA_G: 5296 gain_arr = dig->tia_gain_g; 5297 gain_base = TIA0_GAIN_G; 5298 cfg = chip->dig_table->cfg_tia_g; 5299 msg = "tia_gain_g"; 5300 break; 5301 case RTW89_DIG_GAIN_LNA_A: 5302 gain_arr = dig->lna_gain_a; 5303 gain_base = LNA0_GAIN; 5304 cfg = chip->dig_table->cfg_lna_a; 5305 msg = "lna_gain_a"; 5306 break; 5307 case RTW89_DIG_GAIN_TIA_A: 5308 gain_arr = dig->tia_gain_a; 5309 gain_base = TIA0_GAIN_A; 5310 cfg = chip->dig_table->cfg_tia_a; 5311 msg = "tia_gain_a"; 5312 break; 5313 default: 5314 return; 5315 } 5316 5317 for (i = 0; i < cfg->size; i++) { 5318 tmp = rtw89_phy_read32_mask(rtwdev, cfg->table[i].addr, 5319 cfg->table[i].mask); 5320 tmp >>= DIG_GAIN_SHIFT; 5321 gain_arr[i] = sign_extend32(tmp, U4_MAX_BIT) + gain_base; 5322 gain_base += DIG_GAIN; 5323 5324 rtw89_debug(rtwdev, RTW89_DBG_DIG, "%s[%d]=%d\n", 5325 msg, i, gain_arr[i]); 5326 } 5327 } 5328 5329 static void rtw89_phy_dig_update_gain_para(struct rtw89_dev *rtwdev) 5330 { 5331 struct rtw89_dig_info *dig = &rtwdev->dig; 5332 u32 tmp; 5333 u8 i; 5334 5335 if (!rtwdev->hal.support_igi) 5336 return; 5337 5338 tmp = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PKPW, 5339 B_PATH0_IB_PKPW_MSK); 5340 dig->ib_pkpwr = sign_extend32(tmp >> DIG_GAIN_SHIFT, U8_MAX_BIT); 5341 dig->ib_pbk = rtw89_phy_read32_mask(rtwdev, R_PATH0_IB_PBK, 5342 B_PATH0_IB_PBK_MSK); 5343 rtw89_debug(rtwdev, RTW89_DBG_DIG, "ib_pkpwr=%d, ib_pbk=%d\n", 5344 dig->ib_pkpwr, dig->ib_pbk); 5345 5346 for (i = RTW89_DIG_GAIN_LNA_G; i < RTW89_DIG_GAIN_MAX; i++) 5347 rtw89_phy_dig_read_gain_table(rtwdev, i); 5348 } 5349 5350 static const u8 rssi_nolink = 22; 5351 static const u8 igi_rssi_th[IGI_RSSI_TH_NUM] = {68, 84, 90, 98, 104}; 5352 static const u16 fa_th_2g[FA_TH_NUM] = {22, 44, 66, 88}; 5353 static const u16 fa_th_5g[FA_TH_NUM] = {4, 8, 12, 16}; 5354 static const u16 fa_th_nolink[FA_TH_NUM] = {196, 352, 440, 528}; 5355 5356 static void rtw89_phy_dig_update_rssi_info(struct rtw89_dev *rtwdev) 5357 { 5358 struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info; 5359 struct rtw89_dig_info *dig = &rtwdev->dig; 5360 bool is_linked = rtwdev->total_sta_assoc > 0; 5361 5362 if (is_linked) { 5363 dig->igi_rssi = ch_info->rssi_min >> 1; 5364 } else { 5365 rtw89_debug(rtwdev, RTW89_DBG_DIG, "RSSI update : NO Link\n"); 5366 dig->igi_rssi = rssi_nolink; 5367 } 5368 } 5369 5370 static void rtw89_phy_dig_update_para(struct rtw89_dev *rtwdev) 5371 { 5372 struct rtw89_dig_info *dig = &rtwdev->dig; 5373 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_CHANCTX_0); 5374 bool is_linked = rtwdev->total_sta_assoc > 0; 5375 const u16 *fa_th_src = NULL; 5376 5377 switch (chan->band_type) { 5378 case RTW89_BAND_2G: 5379 dig->lna_gain = dig->lna_gain_g; 5380 dig->tia_gain = dig->tia_gain_g; 5381 fa_th_src = is_linked ? fa_th_2g : fa_th_nolink; 5382 dig->force_gaincode_idx_en = false; 5383 dig->dyn_pd_th_en = true; 5384 break; 5385 case RTW89_BAND_5G: 5386 default: 5387 dig->lna_gain = dig->lna_gain_a; 5388 dig->tia_gain = dig->tia_gain_a; 5389 fa_th_src = is_linked ? fa_th_5g : fa_th_nolink; 5390 dig->force_gaincode_idx_en = true; 5391 dig->dyn_pd_th_en = true; 5392 break; 5393 } 5394 memcpy(dig->fa_th, fa_th_src, sizeof(dig->fa_th)); 5395 memcpy(dig->igi_rssi_th, igi_rssi_th, sizeof(dig->igi_rssi_th)); 5396 } 5397 5398 static const u8 pd_low_th_offset = 20, dynamic_igi_min = 0x20; 5399 static const u8 igi_max_performance_mode = 0x5a; 5400 static const u8 dynamic_pd_threshold_max; 5401 5402 static void rtw89_phy_dig_para_reset(struct rtw89_dev *rtwdev) 5403 { 5404 struct rtw89_dig_info *dig = &rtwdev->dig; 5405 5406 dig->cur_gaincode.lna_idx = LNA_IDX_MAX; 5407 dig->cur_gaincode.tia_idx = TIA_IDX_MAX; 5408 dig->cur_gaincode.rxb_idx = RXB_IDX_MAX; 5409 dig->force_gaincode.lna_idx = LNA_IDX_MAX; 5410 dig->force_gaincode.tia_idx = TIA_IDX_MAX; 5411 dig->force_gaincode.rxb_idx = RXB_IDX_MAX; 5412 5413 dig->dyn_igi_max = igi_max_performance_mode; 5414 dig->dyn_igi_min = dynamic_igi_min; 5415 dig->dyn_pd_th_max = dynamic_pd_threshold_max; 5416 dig->pd_low_th_ofst = pd_low_th_offset; 5417 dig->is_linked_pre = false; 5418 } 5419 5420 static void rtw89_phy_dig_init(struct rtw89_dev *rtwdev) 5421 { 5422 rtw89_phy_dig_update_gain_para(rtwdev); 5423 rtw89_phy_dig_reset(rtwdev); 5424 } 5425 5426 static u8 rtw89_phy_dig_lna_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 5427 { 5428 struct rtw89_dig_info *dig = &rtwdev->dig; 5429 u8 lna_idx; 5430 5431 if (rssi < dig->igi_rssi_th[0]) 5432 lna_idx = RTW89_DIG_GAIN_LNA_IDX6; 5433 else if (rssi < dig->igi_rssi_th[1]) 5434 lna_idx = RTW89_DIG_GAIN_LNA_IDX5; 5435 else if (rssi < dig->igi_rssi_th[2]) 5436 lna_idx = RTW89_DIG_GAIN_LNA_IDX4; 5437 else if (rssi < dig->igi_rssi_th[3]) 5438 lna_idx = RTW89_DIG_GAIN_LNA_IDX3; 5439 else if (rssi < dig->igi_rssi_th[4]) 5440 lna_idx = RTW89_DIG_GAIN_LNA_IDX2; 5441 else 5442 lna_idx = RTW89_DIG_GAIN_LNA_IDX1; 5443 5444 return lna_idx; 5445 } 5446 5447 static u8 rtw89_phy_dig_tia_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi) 5448 { 5449 struct rtw89_dig_info *dig = &rtwdev->dig; 5450 u8 tia_idx; 5451 5452 if (rssi < dig->igi_rssi_th[0]) 5453 tia_idx = RTW89_DIG_GAIN_TIA_IDX1; 5454 else 5455 tia_idx = RTW89_DIG_GAIN_TIA_IDX0; 5456 5457 return tia_idx; 5458 } 5459 5460 #define IB_PBK_BASE 110 5461 #define WB_RSSI_BASE 10 5462 static u8 rtw89_phy_dig_rxb_idx_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 5463 struct rtw89_agc_gaincode_set *set) 5464 { 5465 struct rtw89_dig_info *dig = &rtwdev->dig; 5466 s8 lna_gain = dig->lna_gain[set->lna_idx]; 5467 s8 tia_gain = dig->tia_gain[set->tia_idx]; 5468 s32 wb_rssi = rssi + lna_gain + tia_gain; 5469 s32 rxb_idx_tmp = IB_PBK_BASE + WB_RSSI_BASE; 5470 u8 rxb_idx; 5471 5472 rxb_idx_tmp += dig->ib_pkpwr - dig->ib_pbk - wb_rssi; 5473 rxb_idx = clamp_t(s32, rxb_idx_tmp, RXB_IDX_MIN, RXB_IDX_MAX); 5474 5475 rtw89_debug(rtwdev, RTW89_DBG_DIG, "wb_rssi=%03d, rxb_idx_tmp=%03d\n", 5476 wb_rssi, rxb_idx_tmp); 5477 5478 return rxb_idx; 5479 } 5480 5481 static void rtw89_phy_dig_gaincode_by_rssi(struct rtw89_dev *rtwdev, u8 rssi, 5482 struct rtw89_agc_gaincode_set *set) 5483 { 5484 set->lna_idx = rtw89_phy_dig_lna_idx_by_rssi(rtwdev, rssi); 5485 set->tia_idx = rtw89_phy_dig_tia_idx_by_rssi(rtwdev, rssi); 5486 set->rxb_idx = rtw89_phy_dig_rxb_idx_by_rssi(rtwdev, rssi, set); 5487 5488 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5489 "final_rssi=%03d, (lna,tia,rab)=(%d,%d,%02d)\n", 5490 rssi, set->lna_idx, set->tia_idx, set->rxb_idx); 5491 } 5492 5493 #define IGI_OFFSET_MAX 25 5494 #define IGI_OFFSET_MUL 2 5495 static void rtw89_phy_dig_igi_offset_by_env(struct rtw89_dev *rtwdev) 5496 { 5497 struct rtw89_dig_info *dig = &rtwdev->dig; 5498 struct rtw89_env_monitor_info *env = &rtwdev->env_monitor; 5499 enum rtw89_dig_noisy_level noisy_lv; 5500 u8 igi_offset = dig->fa_rssi_ofst; 5501 u16 fa_ratio = 0; 5502 5503 fa_ratio = env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil; 5504 5505 if (fa_ratio < dig->fa_th[0]) 5506 noisy_lv = RTW89_DIG_NOISY_LEVEL0; 5507 else if (fa_ratio < dig->fa_th[1]) 5508 noisy_lv = RTW89_DIG_NOISY_LEVEL1; 5509 else if (fa_ratio < dig->fa_th[2]) 5510 noisy_lv = RTW89_DIG_NOISY_LEVEL2; 5511 else if (fa_ratio < dig->fa_th[3]) 5512 noisy_lv = RTW89_DIG_NOISY_LEVEL3; 5513 else 5514 noisy_lv = RTW89_DIG_NOISY_LEVEL_MAX; 5515 5516 if (noisy_lv == RTW89_DIG_NOISY_LEVEL0 && igi_offset < 2) 5517 igi_offset = 0; 5518 else 5519 igi_offset += noisy_lv * IGI_OFFSET_MUL; 5520 5521 igi_offset = min_t(u8, igi_offset, IGI_OFFSET_MAX); 5522 dig->fa_rssi_ofst = igi_offset; 5523 5524 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5525 "fa_th: [+6 (%d) +4 (%d) +2 (%d) 0 (%d) -2 ]\n", 5526 dig->fa_th[3], dig->fa_th[2], dig->fa_th[1], dig->fa_th[0]); 5527 5528 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5529 "fa(CCK,OFDM,ALL)=(%d,%d,%d)%%, noisy_lv=%d, ofst=%d\n", 5530 env->ifs_clm_cck_fa_permil, env->ifs_clm_ofdm_fa_permil, 5531 env->ifs_clm_cck_fa_permil + env->ifs_clm_ofdm_fa_permil, 5532 noisy_lv, igi_offset); 5533 } 5534 5535 static void rtw89_phy_dig_set_lna_idx(struct rtw89_dev *rtwdev, u8 lna_idx) 5536 { 5537 const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 5538 5539 rtw89_phy_write32_mask(rtwdev, dig_regs->p0_lna_init.addr, 5540 dig_regs->p0_lna_init.mask, lna_idx); 5541 rtw89_phy_write32_mask(rtwdev, dig_regs->p1_lna_init.addr, 5542 dig_regs->p1_lna_init.mask, lna_idx); 5543 } 5544 5545 static void rtw89_phy_dig_set_tia_idx(struct rtw89_dev *rtwdev, u8 tia_idx) 5546 { 5547 const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 5548 5549 rtw89_phy_write32_mask(rtwdev, dig_regs->p0_tia_init.addr, 5550 dig_regs->p0_tia_init.mask, tia_idx); 5551 rtw89_phy_write32_mask(rtwdev, dig_regs->p1_tia_init.addr, 5552 dig_regs->p1_tia_init.mask, tia_idx); 5553 } 5554 5555 static void rtw89_phy_dig_set_rxb_idx(struct rtw89_dev *rtwdev, u8 rxb_idx) 5556 { 5557 const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 5558 5559 rtw89_phy_write32_mask(rtwdev, dig_regs->p0_rxb_init.addr, 5560 dig_regs->p0_rxb_init.mask, rxb_idx); 5561 rtw89_phy_write32_mask(rtwdev, dig_regs->p1_rxb_init.addr, 5562 dig_regs->p1_rxb_init.mask, rxb_idx); 5563 } 5564 5565 static void rtw89_phy_dig_set_igi_cr(struct rtw89_dev *rtwdev, 5566 const struct rtw89_agc_gaincode_set set) 5567 { 5568 if (!rtwdev->hal.support_igi) 5569 return; 5570 5571 rtw89_phy_dig_set_lna_idx(rtwdev, set.lna_idx); 5572 rtw89_phy_dig_set_tia_idx(rtwdev, set.tia_idx); 5573 rtw89_phy_dig_set_rxb_idx(rtwdev, set.rxb_idx); 5574 5575 rtw89_debug(rtwdev, RTW89_DBG_DIG, "Set (lna,tia,rxb)=((%d,%d,%02d))\n", 5576 set.lna_idx, set.tia_idx, set.rxb_idx); 5577 } 5578 5579 static void rtw89_phy_dig_sdagc_follow_pagc_config(struct rtw89_dev *rtwdev, 5580 bool enable) 5581 { 5582 const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 5583 5584 rtw89_phy_write32_mask(rtwdev, dig_regs->p0_p20_pagcugc_en.addr, 5585 dig_regs->p0_p20_pagcugc_en.mask, enable); 5586 rtw89_phy_write32_mask(rtwdev, dig_regs->p0_s20_pagcugc_en.addr, 5587 dig_regs->p0_s20_pagcugc_en.mask, enable); 5588 rtw89_phy_write32_mask(rtwdev, dig_regs->p1_p20_pagcugc_en.addr, 5589 dig_regs->p1_p20_pagcugc_en.mask, enable); 5590 rtw89_phy_write32_mask(rtwdev, dig_regs->p1_s20_pagcugc_en.addr, 5591 dig_regs->p1_s20_pagcugc_en.mask, enable); 5592 5593 rtw89_debug(rtwdev, RTW89_DBG_DIG, "sdagc_follow_pagc=%d\n", enable); 5594 } 5595 5596 static void rtw89_phy_dig_config_igi(struct rtw89_dev *rtwdev) 5597 { 5598 struct rtw89_dig_info *dig = &rtwdev->dig; 5599 5600 if (!rtwdev->hal.support_igi) 5601 return; 5602 5603 if (dig->force_gaincode_idx_en) { 5604 rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 5605 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5606 "Force gaincode index enabled.\n"); 5607 } else { 5608 rtw89_phy_dig_gaincode_by_rssi(rtwdev, dig->igi_fa_rssi, 5609 &dig->cur_gaincode); 5610 rtw89_phy_dig_set_igi_cr(rtwdev, dig->cur_gaincode); 5611 } 5612 } 5613 5614 static void rtw89_phy_dig_dyn_pd_th(struct rtw89_dev *rtwdev, u8 rssi, 5615 bool enable) 5616 { 5617 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_CHANCTX_0); 5618 const struct rtw89_dig_regs *dig_regs = rtwdev->chip->dig_regs; 5619 enum rtw89_bandwidth cbw = chan->band_width; 5620 struct rtw89_dig_info *dig = &rtwdev->dig; 5621 u8 final_rssi = 0, under_region = dig->pd_low_th_ofst; 5622 u8 ofdm_cca_th; 5623 s8 cck_cca_th; 5624 u32 pd_val = 0; 5625 5626 if (rtwdev->chip->chip_gen == RTW89_CHIP_AX) 5627 under_region += PD_TH_SB_FLTR_CMP_VAL; 5628 5629 switch (cbw) { 5630 case RTW89_CHANNEL_WIDTH_40: 5631 under_region += PD_TH_BW40_CMP_VAL; 5632 break; 5633 case RTW89_CHANNEL_WIDTH_80: 5634 under_region += PD_TH_BW80_CMP_VAL; 5635 break; 5636 case RTW89_CHANNEL_WIDTH_160: 5637 under_region += PD_TH_BW160_CMP_VAL; 5638 break; 5639 case RTW89_CHANNEL_WIDTH_20: 5640 fallthrough; 5641 default: 5642 under_region += PD_TH_BW20_CMP_VAL; 5643 break; 5644 } 5645 5646 dig->dyn_pd_th_max = dig->igi_rssi; 5647 5648 final_rssi = min_t(u8, rssi, dig->igi_rssi); 5649 ofdm_cca_th = clamp_t(u8, final_rssi, PD_TH_MIN_RSSI + under_region, 5650 PD_TH_MAX_RSSI + under_region); 5651 5652 if (enable) { 5653 pd_val = (ofdm_cca_th - under_region - PD_TH_MIN_RSSI) >> 1; 5654 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5655 "igi=%d, ofdm_ccaTH=%d, backoff=%d, PD_low=%d\n", 5656 final_rssi, ofdm_cca_th, under_region, pd_val); 5657 } else { 5658 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5659 "Dynamic PD th disabled, Set PD_low_bd=0\n"); 5660 } 5661 5662 rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 5663 dig_regs->pd_lower_bound_mask, pd_val); 5664 rtw89_phy_write32_mask(rtwdev, dig_regs->seg0_pd_reg, 5665 dig_regs->pd_spatial_reuse_en, enable); 5666 5667 if (!rtwdev->hal.support_cckpd) 5668 return; 5669 5670 cck_cca_th = max_t(s8, final_rssi - under_region, CCKPD_TH_MIN_RSSI); 5671 pd_val = (u32)(cck_cca_th - IGI_RSSI_MAX); 5672 5673 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5674 "igi=%d, cck_ccaTH=%d, backoff=%d, cck_PD_low=((%d))dB\n", 5675 final_rssi, cck_cca_th, under_region, pd_val); 5676 5677 rtw89_phy_write32_mask(rtwdev, dig_regs->bmode_pd_reg, 5678 dig_regs->bmode_cca_rssi_limit_en, enable); 5679 rtw89_phy_write32_mask(rtwdev, dig_regs->bmode_pd_lower_bound_reg, 5680 dig_regs->bmode_rssi_nocca_low_th_mask, pd_val); 5681 } 5682 5683 void rtw89_phy_dig_reset(struct rtw89_dev *rtwdev) 5684 { 5685 struct rtw89_dig_info *dig = &rtwdev->dig; 5686 5687 dig->bypass_dig = false; 5688 rtw89_phy_dig_para_reset(rtwdev); 5689 rtw89_phy_dig_set_igi_cr(rtwdev, dig->force_gaincode); 5690 rtw89_phy_dig_dyn_pd_th(rtwdev, rssi_nolink, false); 5691 rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 5692 rtw89_phy_dig_update_para(rtwdev); 5693 } 5694 5695 #define IGI_RSSI_MIN 10 5696 void rtw89_phy_dig(struct rtw89_dev *rtwdev) 5697 { 5698 struct rtw89_dig_info *dig = &rtwdev->dig; 5699 bool is_linked = rtwdev->total_sta_assoc > 0; 5700 5701 if (unlikely(dig->bypass_dig)) { 5702 dig->bypass_dig = false; 5703 return; 5704 } 5705 5706 if (!dig->is_linked_pre && is_linked) { 5707 rtw89_debug(rtwdev, RTW89_DBG_DIG, "First connected\n"); 5708 rtw89_phy_dig_update_para(rtwdev); 5709 } else if (dig->is_linked_pre && !is_linked) { 5710 rtw89_debug(rtwdev, RTW89_DBG_DIG, "First disconnected\n"); 5711 rtw89_phy_dig_update_para(rtwdev); 5712 } 5713 dig->is_linked_pre = is_linked; 5714 5715 rtw89_phy_dig_igi_offset_by_env(rtwdev); 5716 rtw89_phy_dig_update_rssi_info(rtwdev); 5717 5718 dig->dyn_igi_min = (dig->igi_rssi > IGI_RSSI_MIN) ? 5719 dig->igi_rssi - IGI_RSSI_MIN : 0; 5720 dig->dyn_igi_max = dig->dyn_igi_min + IGI_OFFSET_MAX; 5721 dig->igi_fa_rssi = dig->dyn_igi_min + dig->fa_rssi_ofst; 5722 5723 dig->igi_fa_rssi = clamp(dig->igi_fa_rssi, dig->dyn_igi_min, 5724 dig->dyn_igi_max); 5725 5726 rtw89_debug(rtwdev, RTW89_DBG_DIG, 5727 "rssi=%03d, dyn(max,min)=(%d,%d), final_rssi=%d\n", 5728 dig->igi_rssi, dig->dyn_igi_max, dig->dyn_igi_min, 5729 dig->igi_fa_rssi); 5730 5731 rtw89_phy_dig_config_igi(rtwdev); 5732 5733 rtw89_phy_dig_dyn_pd_th(rtwdev, dig->igi_fa_rssi, dig->dyn_pd_th_en); 5734 5735 if (dig->dyn_pd_th_en && dig->igi_fa_rssi > dig->dyn_pd_th_max) 5736 rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, true); 5737 else 5738 rtw89_phy_dig_sdagc_follow_pagc_config(rtwdev, false); 5739 } 5740 5741 static void rtw89_phy_tx_path_div_sta_iter(void *data, struct ieee80211_sta *sta) 5742 { 5743 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 5744 struct rtw89_dev *rtwdev = rtwsta->rtwdev; 5745 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 5746 struct rtw89_hal *hal = &rtwdev->hal; 5747 bool *done = data; 5748 u8 rssi_a, rssi_b; 5749 u32 candidate; 5750 5751 if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION || sta->tdls) 5752 return; 5753 5754 if (*done) 5755 return; 5756 5757 *done = true; 5758 5759 rssi_a = ewma_rssi_read(&rtwsta->rssi[RF_PATH_A]); 5760 rssi_b = ewma_rssi_read(&rtwsta->rssi[RF_PATH_B]); 5761 5762 if (rssi_a > rssi_b + RTW89_TX_DIV_RSSI_RAW_TH) 5763 candidate = RF_A; 5764 else if (rssi_b > rssi_a + RTW89_TX_DIV_RSSI_RAW_TH) 5765 candidate = RF_B; 5766 else 5767 return; 5768 5769 if (hal->antenna_tx == candidate) 5770 return; 5771 5772 hal->antenna_tx = candidate; 5773 rtw89_fw_h2c_txpath_cmac_tbl(rtwdev, rtwsta); 5774 5775 if (hal->antenna_tx == RF_A) { 5776 rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x12); 5777 rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x11); 5778 } else if (hal->antenna_tx == RF_B) { 5779 rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE, B_P0_RFMODE_MUX, 0x11); 5780 rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE, B_P1_RFMODE_MUX, 0x12); 5781 } 5782 } 5783 5784 void rtw89_phy_tx_path_div_track(struct rtw89_dev *rtwdev) 5785 { 5786 struct rtw89_hal *hal = &rtwdev->hal; 5787 bool done = false; 5788 5789 if (!hal->tx_path_diversity) 5790 return; 5791 5792 ieee80211_iterate_stations_atomic(rtwdev->hw, 5793 rtw89_phy_tx_path_div_sta_iter, 5794 &done); 5795 } 5796 5797 #define ANTDIV_MAIN 0 5798 #define ANTDIV_AUX 1 5799 5800 static void rtw89_phy_antdiv_set_ant(struct rtw89_dev *rtwdev) 5801 { 5802 struct rtw89_hal *hal = &rtwdev->hal; 5803 u8 default_ant, optional_ant; 5804 5805 if (!hal->ant_diversity || hal->antenna_tx == 0) 5806 return; 5807 5808 if (hal->antenna_tx == RF_B) { 5809 default_ant = ANTDIV_AUX; 5810 optional_ant = ANTDIV_MAIN; 5811 } else { 5812 default_ant = ANTDIV_MAIN; 5813 optional_ant = ANTDIV_AUX; 5814 } 5815 5816 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_CGCS_CTRL, 5817 default_ant, RTW89_PHY_0); 5818 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ORI, 5819 default_ant, RTW89_PHY_0); 5820 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_RX_ALT, 5821 optional_ant, RTW89_PHY_0); 5822 rtw89_phy_write32_idx(rtwdev, R_P0_ANTSEL, B_P0_ANTSEL_TX_ORI, 5823 default_ant, RTW89_PHY_0); 5824 } 5825 5826 static void rtw89_phy_swap_hal_antenna(struct rtw89_dev *rtwdev) 5827 { 5828 struct rtw89_hal *hal = &rtwdev->hal; 5829 5830 hal->antenna_rx = hal->antenna_rx == RF_A ? RF_B : RF_A; 5831 hal->antenna_tx = hal->antenna_rx; 5832 } 5833 5834 static void rtw89_phy_antdiv_decision_state(struct rtw89_dev *rtwdev) 5835 { 5836 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 5837 struct rtw89_hal *hal = &rtwdev->hal; 5838 bool no_change = false; 5839 u8 main_rssi, aux_rssi; 5840 u8 main_evm, aux_evm; 5841 u32 candidate; 5842 5843 antdiv->get_stats = false; 5844 antdiv->training_count = 0; 5845 5846 main_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->main_stats); 5847 main_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->main_stats); 5848 aux_rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->aux_stats); 5849 aux_evm = rtw89_phy_antdiv_sts_instance_get_evm(&antdiv->aux_stats); 5850 5851 if (main_evm > aux_evm + ANTDIV_EVM_DIFF_TH) 5852 candidate = RF_A; 5853 else if (aux_evm > main_evm + ANTDIV_EVM_DIFF_TH) 5854 candidate = RF_B; 5855 else if (main_rssi > aux_rssi + RTW89_TX_DIV_RSSI_RAW_TH) 5856 candidate = RF_A; 5857 else if (aux_rssi > main_rssi + RTW89_TX_DIV_RSSI_RAW_TH) 5858 candidate = RF_B; 5859 else 5860 no_change = true; 5861 5862 if (no_change) { 5863 /* swap back from training antenna to original */ 5864 rtw89_phy_swap_hal_antenna(rtwdev); 5865 return; 5866 } 5867 5868 hal->antenna_tx = candidate; 5869 hal->antenna_rx = candidate; 5870 } 5871 5872 static void rtw89_phy_antdiv_training_state(struct rtw89_dev *rtwdev) 5873 { 5874 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 5875 u64 state_period; 5876 5877 if (antdiv->training_count % 2 == 0) { 5878 if (antdiv->training_count == 0) 5879 rtw89_phy_antdiv_sts_reset(rtwdev); 5880 5881 antdiv->get_stats = true; 5882 state_period = msecs_to_jiffies(ANTDIV_TRAINNING_INTVL); 5883 } else { 5884 antdiv->get_stats = false; 5885 state_period = msecs_to_jiffies(ANTDIV_DELAY); 5886 5887 rtw89_phy_swap_hal_antenna(rtwdev); 5888 rtw89_phy_antdiv_set_ant(rtwdev); 5889 } 5890 5891 antdiv->training_count++; 5892 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 5893 state_period); 5894 } 5895 5896 void rtw89_phy_antdiv_work(struct work_struct *work) 5897 { 5898 struct rtw89_dev *rtwdev = container_of(work, struct rtw89_dev, 5899 antdiv_work.work); 5900 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 5901 5902 mutex_lock(&rtwdev->mutex); 5903 5904 if (antdiv->training_count <= ANTDIV_TRAINNING_CNT) { 5905 rtw89_phy_antdiv_training_state(rtwdev); 5906 } else { 5907 rtw89_phy_antdiv_decision_state(rtwdev); 5908 rtw89_phy_antdiv_set_ant(rtwdev); 5909 } 5910 5911 mutex_unlock(&rtwdev->mutex); 5912 } 5913 5914 void rtw89_phy_antdiv_track(struct rtw89_dev *rtwdev) 5915 { 5916 struct rtw89_antdiv_info *antdiv = &rtwdev->antdiv; 5917 struct rtw89_hal *hal = &rtwdev->hal; 5918 u8 rssi, rssi_pre; 5919 5920 if (!hal->ant_diversity || hal->ant_diversity_fixed) 5921 return; 5922 5923 rssi = rtw89_phy_antdiv_sts_instance_get_rssi(&antdiv->target_stats); 5924 rssi_pre = antdiv->rssi_pre; 5925 antdiv->rssi_pre = rssi; 5926 rtw89_phy_antdiv_sts_instance_reset(&antdiv->target_stats); 5927 5928 if (abs((int)rssi - (int)rssi_pre) < ANTDIV_RSSI_DIFF_TH) 5929 return; 5930 5931 antdiv->training_count = 0; 5932 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->antdiv_work, 0); 5933 } 5934 5935 static void rtw89_phy_env_monitor_init(struct rtw89_dev *rtwdev) 5936 { 5937 rtw89_phy_ccx_top_setting_init(rtwdev); 5938 rtw89_phy_ifs_clm_setting_init(rtwdev); 5939 } 5940 5941 static void rtw89_phy_edcca_init(struct rtw89_dev *rtwdev) 5942 { 5943 const struct rtw89_edcca_regs *edcca_regs = rtwdev->chip->edcca_regs; 5944 struct rtw89_edcca_bak *edcca_bak = &rtwdev->hal.edcca_bak; 5945 5946 memset(edcca_bak, 0, sizeof(*edcca_bak)); 5947 5948 if (rtwdev->chip->chip_id == RTL8922A && rtwdev->hal.cv == CHIP_CAV) { 5949 rtw89_phy_set_phy_regs(rtwdev, R_TXGATING, B_TXGATING_EN, 0); 5950 rtw89_phy_set_phy_regs(rtwdev, R_CTLTOP, B_CTLTOP_VAL, 2); 5951 rtw89_phy_set_phy_regs(rtwdev, R_CTLTOP, B_CTLTOP_ON, 1); 5952 rtw89_phy_set_phy_regs(rtwdev, R_SPOOF_CG, B_SPOOF_CG_EN, 0); 5953 rtw89_phy_set_phy_regs(rtwdev, R_DFS_FFT_CG, B_DFS_CG_EN, 0); 5954 rtw89_phy_set_phy_regs(rtwdev, R_DFS_FFT_CG, B_DFS_FFT_EN, 0); 5955 rtw89_phy_set_phy_regs(rtwdev, R_SEGSND, B_SEGSND_EN, 0); 5956 rtw89_phy_set_phy_regs(rtwdev, R_SEGSND, B_SEGSND_EN, 1); 5957 rtw89_phy_set_phy_regs(rtwdev, R_DFS_FFT_CG, B_DFS_FFT_EN, 1); 5958 } 5959 5960 rtw89_phy_write32_mask(rtwdev, edcca_regs->tx_collision_t2r_st, 5961 edcca_regs->tx_collision_t2r_st_mask, 0x29); 5962 } 5963 5964 void rtw89_phy_dm_init(struct rtw89_dev *rtwdev) 5965 { 5966 rtw89_phy_stat_init(rtwdev); 5967 5968 rtw89_chip_bb_sethw(rtwdev); 5969 5970 rtw89_phy_env_monitor_init(rtwdev); 5971 rtw89_physts_parsing_init(rtwdev); 5972 rtw89_phy_dig_init(rtwdev); 5973 rtw89_phy_cfo_init(rtwdev); 5974 rtw89_phy_bb_wrap_init(rtwdev); 5975 rtw89_phy_edcca_init(rtwdev); 5976 rtw89_phy_ch_info_init(rtwdev); 5977 rtw89_phy_ul_tb_info_init(rtwdev); 5978 rtw89_phy_antdiv_init(rtwdev); 5979 rtw89_chip_rfe_gpio(rtwdev); 5980 rtw89_phy_antdiv_set_ant(rtwdev); 5981 5982 rtw89_chip_rfk_hw_init(rtwdev); 5983 rtw89_phy_init_rf_nctl(rtwdev); 5984 rtw89_chip_rfk_init(rtwdev); 5985 rtw89_chip_set_txpwr_ctrl(rtwdev); 5986 rtw89_chip_power_trim(rtwdev); 5987 rtw89_chip_cfg_txrx_path(rtwdev); 5988 } 5989 5990 void rtw89_phy_set_bss_color(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 5991 { 5992 const struct rtw89_chip_info *chip = rtwdev->chip; 5993 const struct rtw89_reg_def *bss_clr_vld = &chip->bss_clr_vld; 5994 enum rtw89_phy_idx phy_idx = RTW89_PHY_0; 5995 u8 bss_color; 5996 5997 if (!vif->bss_conf.he_support || !vif->cfg.assoc) 5998 return; 5999 6000 bss_color = vif->bss_conf.he_bss_color.color; 6001 6002 rtw89_phy_write32_idx(rtwdev, bss_clr_vld->addr, bss_clr_vld->mask, 0x1, 6003 phy_idx); 6004 rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_TGT, 6005 bss_color, phy_idx); 6006 rtw89_phy_write32_idx(rtwdev, chip->bss_clr_map_reg, B_BSS_CLR_MAP_STAID, 6007 vif->cfg.aid, phy_idx); 6008 } 6009 6010 static bool rfk_chan_validate_desc(const struct rtw89_rfk_chan_desc *desc) 6011 { 6012 return desc->ch != 0; 6013 } 6014 6015 static bool rfk_chan_is_equivalent(const struct rtw89_rfk_chan_desc *desc, 6016 const struct rtw89_chan *chan) 6017 { 6018 if (!rfk_chan_validate_desc(desc)) 6019 return false; 6020 6021 if (desc->ch != chan->channel) 6022 return false; 6023 6024 if (desc->has_band && desc->band != chan->band_type) 6025 return false; 6026 6027 if (desc->has_bw && desc->bw != chan->band_width) 6028 return false; 6029 6030 return true; 6031 } 6032 6033 struct rfk_chan_iter_data { 6034 const struct rtw89_rfk_chan_desc desc; 6035 unsigned int found; 6036 }; 6037 6038 static int rfk_chan_iter_search(const struct rtw89_chan *chan, void *data) 6039 { 6040 struct rfk_chan_iter_data *iter_data = data; 6041 6042 if (rfk_chan_is_equivalent(&iter_data->desc, chan)) 6043 iter_data->found++; 6044 6045 return 0; 6046 } 6047 6048 u8 rtw89_rfk_chan_lookup(struct rtw89_dev *rtwdev, 6049 const struct rtw89_rfk_chan_desc *desc, u8 desc_nr, 6050 const struct rtw89_chan *target_chan) 6051 { 6052 int sel = -1; 6053 u8 i; 6054 6055 for (i = 0; i < desc_nr; i++) { 6056 struct rfk_chan_iter_data iter_data = { 6057 .desc = desc[i], 6058 }; 6059 6060 if (rfk_chan_is_equivalent(&desc[i], target_chan)) 6061 return i; 6062 6063 rtw89_iterate_entity_chan(rtwdev, rfk_chan_iter_search, &iter_data); 6064 if (!iter_data.found && sel == -1) 6065 sel = i; 6066 } 6067 6068 if (sel == -1) { 6069 rtw89_debug(rtwdev, RTW89_DBG_RFK, 6070 "no idle rfk entry; force replace the first\n"); 6071 sel = 0; 6072 } 6073 6074 return sel; 6075 } 6076 EXPORT_SYMBOL(rtw89_rfk_chan_lookup); 6077 6078 static void 6079 _rfk_write_rf(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 6080 { 6081 rtw89_write_rf(rtwdev, def->path, def->addr, def->mask, def->data); 6082 } 6083 6084 static void 6085 _rfk_write32_mask(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 6086 { 6087 rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data); 6088 } 6089 6090 static void 6091 _rfk_write32_set(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 6092 { 6093 rtw89_phy_write32_set(rtwdev, def->addr, def->mask); 6094 } 6095 6096 static void 6097 _rfk_write32_clr(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 6098 { 6099 rtw89_phy_write32_clr(rtwdev, def->addr, def->mask); 6100 } 6101 6102 static void 6103 _rfk_delay(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) 6104 { 6105 udelay(def->data); 6106 } 6107 6108 static void 6109 (*_rfk_handler[])(struct rtw89_dev *rtwdev, const struct rtw89_reg5_def *def) = { 6110 [RTW89_RFK_F_WRF] = _rfk_write_rf, 6111 [RTW89_RFK_F_WM] = _rfk_write32_mask, 6112 [RTW89_RFK_F_WS] = _rfk_write32_set, 6113 [RTW89_RFK_F_WC] = _rfk_write32_clr, 6114 [RTW89_RFK_F_DELAY] = _rfk_delay, 6115 }; 6116 6117 static_assert(ARRAY_SIZE(_rfk_handler) == RTW89_RFK_F_NUM); 6118 6119 void 6120 rtw89_rfk_parser(struct rtw89_dev *rtwdev, const struct rtw89_rfk_tbl *tbl) 6121 { 6122 const struct rtw89_reg5_def *p = tbl->defs; 6123 const struct rtw89_reg5_def *end = tbl->defs + tbl->size; 6124 6125 for (; p < end; p++) 6126 _rfk_handler[p->flag](rtwdev, p); 6127 } 6128 EXPORT_SYMBOL(rtw89_rfk_parser); 6129 6130 #define RTW89_TSSI_FAST_MODE_NUM 4 6131 6132 static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_flat[RTW89_TSSI_FAST_MODE_NUM] = { 6133 {0xD934, 0xff0000}, 6134 {0xD934, 0xff000000}, 6135 {0xD938, 0xff}, 6136 {0xD934, 0xff00}, 6137 }; 6138 6139 static const struct rtw89_reg_def rtw89_tssi_fastmode_regs_level[RTW89_TSSI_FAST_MODE_NUM] = { 6140 {0xD930, 0xff0000}, 6141 {0xD930, 0xff000000}, 6142 {0xD934, 0xff}, 6143 {0xD930, 0xff00}, 6144 }; 6145 6146 static 6147 void rtw89_phy_tssi_ctrl_set_fast_mode_cfg(struct rtw89_dev *rtwdev, 6148 enum rtw89_mac_idx mac_idx, 6149 enum rtw89_tssi_bandedge_cfg bandedge_cfg, 6150 u32 val) 6151 { 6152 const struct rtw89_reg_def *regs; 6153 u32 reg; 6154 int i; 6155 6156 if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 6157 regs = rtw89_tssi_fastmode_regs_flat; 6158 else 6159 regs = rtw89_tssi_fastmode_regs_level; 6160 6161 for (i = 0; i < RTW89_TSSI_FAST_MODE_NUM; i++) { 6162 reg = rtw89_mac_reg_by_idx(rtwdev, regs[i].addr, mac_idx); 6163 rtw89_write32_mask(rtwdev, reg, regs[i].mask, val); 6164 } 6165 } 6166 6167 static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_flat[RTW89_TSSI_SBW_NUM] = { 6168 {0xD91C, 0xff000000}, 6169 {0xD920, 0xff}, 6170 {0xD920, 0xff00}, 6171 {0xD920, 0xff0000}, 6172 {0xD920, 0xff000000}, 6173 {0xD924, 0xff}, 6174 {0xD924, 0xff00}, 6175 {0xD914, 0xff000000}, 6176 {0xD918, 0xff}, 6177 {0xD918, 0xff00}, 6178 {0xD918, 0xff0000}, 6179 {0xD918, 0xff000000}, 6180 {0xD91C, 0xff}, 6181 {0xD91C, 0xff00}, 6182 {0xD91C, 0xff0000}, 6183 }; 6184 6185 static const struct rtw89_reg_def rtw89_tssi_bandedge_regs_level[RTW89_TSSI_SBW_NUM] = { 6186 {0xD910, 0xff}, 6187 {0xD910, 0xff00}, 6188 {0xD910, 0xff0000}, 6189 {0xD910, 0xff000000}, 6190 {0xD914, 0xff}, 6191 {0xD914, 0xff00}, 6192 {0xD914, 0xff0000}, 6193 {0xD908, 0xff}, 6194 {0xD908, 0xff00}, 6195 {0xD908, 0xff0000}, 6196 {0xD908, 0xff000000}, 6197 {0xD90C, 0xff}, 6198 {0xD90C, 0xff00}, 6199 {0xD90C, 0xff0000}, 6200 {0xD90C, 0xff000000}, 6201 }; 6202 6203 void rtw89_phy_tssi_ctrl_set_bandedge_cfg(struct rtw89_dev *rtwdev, 6204 enum rtw89_mac_idx mac_idx, 6205 enum rtw89_tssi_bandedge_cfg bandedge_cfg) 6206 { 6207 const struct rtw89_chip_info *chip = rtwdev->chip; 6208 const struct rtw89_reg_def *regs; 6209 const u32 *data; 6210 u32 reg; 6211 int i; 6212 6213 if (bandedge_cfg >= RTW89_TSSI_CFG_NUM) 6214 return; 6215 6216 if (bandedge_cfg == RTW89_TSSI_BANDEDGE_FLAT) 6217 regs = rtw89_tssi_bandedge_regs_flat; 6218 else 6219 regs = rtw89_tssi_bandedge_regs_level; 6220 6221 data = chip->tssi_dbw_table->data[bandedge_cfg]; 6222 6223 for (i = 0; i < RTW89_TSSI_SBW_NUM; i++) { 6224 reg = rtw89_mac_reg_by_idx(rtwdev, regs[i].addr, mac_idx); 6225 rtw89_write32_mask(rtwdev, reg, regs[i].mask, data[i]); 6226 } 6227 6228 reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_BANDEDGE_CFG, mac_idx); 6229 rtw89_write32_mask(rtwdev, reg, B_AX_BANDEDGE_CFG_IDX_MASK, bandedge_cfg); 6230 6231 rtw89_phy_tssi_ctrl_set_fast_mode_cfg(rtwdev, mac_idx, bandedge_cfg, 6232 data[RTW89_TSSI_SBW20]); 6233 } 6234 EXPORT_SYMBOL(rtw89_phy_tssi_ctrl_set_bandedge_cfg); 6235 6236 static 6237 const u8 rtw89_ch_base_table[16] = {1, 0xff, 6238 36, 100, 132, 149, 0xff, 6239 1, 33, 65, 97, 129, 161, 193, 225, 0xff}; 6240 #define RTW89_CH_BASE_IDX_2G 0 6241 #define RTW89_CH_BASE_IDX_5G_FIRST 2 6242 #define RTW89_CH_BASE_IDX_5G_LAST 5 6243 #define RTW89_CH_BASE_IDX_6G_FIRST 7 6244 #define RTW89_CH_BASE_IDX_6G_LAST 14 6245 6246 #define RTW89_CH_BASE_IDX_MASK GENMASK(7, 4) 6247 #define RTW89_CH_OFFSET_MASK GENMASK(3, 0) 6248 6249 u8 rtw89_encode_chan_idx(struct rtw89_dev *rtwdev, u8 central_ch, u8 band) 6250 { 6251 u8 chan_idx; 6252 u8 last, first; 6253 u8 idx; 6254 6255 switch (band) { 6256 case RTW89_BAND_2G: 6257 chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, RTW89_CH_BASE_IDX_2G) | 6258 FIELD_PREP(RTW89_CH_OFFSET_MASK, central_ch); 6259 return chan_idx; 6260 case RTW89_BAND_5G: 6261 first = RTW89_CH_BASE_IDX_5G_FIRST; 6262 last = RTW89_CH_BASE_IDX_5G_LAST; 6263 break; 6264 case RTW89_BAND_6G: 6265 first = RTW89_CH_BASE_IDX_6G_FIRST; 6266 last = RTW89_CH_BASE_IDX_6G_LAST; 6267 break; 6268 default: 6269 rtw89_warn(rtwdev, "Unsupported band %d\n", band); 6270 return 0; 6271 } 6272 6273 for (idx = last; idx >= first; idx--) 6274 if (central_ch >= rtw89_ch_base_table[idx]) 6275 break; 6276 6277 if (idx < first) { 6278 rtw89_warn(rtwdev, "Unknown band %d channel %d\n", band, central_ch); 6279 return 0; 6280 } 6281 6282 chan_idx = FIELD_PREP(RTW89_CH_BASE_IDX_MASK, idx) | 6283 FIELD_PREP(RTW89_CH_OFFSET_MASK, 6284 (central_ch - rtw89_ch_base_table[idx]) >> 1); 6285 return chan_idx; 6286 } 6287 EXPORT_SYMBOL(rtw89_encode_chan_idx); 6288 6289 void rtw89_decode_chan_idx(struct rtw89_dev *rtwdev, u8 chan_idx, 6290 u8 *ch, enum nl80211_band *band) 6291 { 6292 u8 idx, offset; 6293 6294 idx = FIELD_GET(RTW89_CH_BASE_IDX_MASK, chan_idx); 6295 offset = FIELD_GET(RTW89_CH_OFFSET_MASK, chan_idx); 6296 6297 if (idx == RTW89_CH_BASE_IDX_2G) { 6298 *band = NL80211_BAND_2GHZ; 6299 *ch = offset; 6300 return; 6301 } 6302 6303 *band = idx <= RTW89_CH_BASE_IDX_5G_LAST ? NL80211_BAND_5GHZ : NL80211_BAND_6GHZ; 6304 *ch = rtw89_ch_base_table[idx] + (offset << 1); 6305 } 6306 EXPORT_SYMBOL(rtw89_decode_chan_idx); 6307 6308 void rtw89_phy_config_edcca(struct rtw89_dev *rtwdev, bool scan) 6309 { 6310 const struct rtw89_edcca_regs *edcca_regs = rtwdev->chip->edcca_regs; 6311 struct rtw89_edcca_bak *edcca_bak = &rtwdev->hal.edcca_bak; 6312 6313 if (scan) { 6314 edcca_bak->a = 6315 rtw89_phy_read32_mask(rtwdev, edcca_regs->edcca_level, 6316 edcca_regs->edcca_mask); 6317 edcca_bak->p = 6318 rtw89_phy_read32_mask(rtwdev, edcca_regs->edcca_level, 6319 edcca_regs->edcca_p_mask); 6320 edcca_bak->ppdu = 6321 rtw89_phy_read32_mask(rtwdev, edcca_regs->ppdu_level, 6322 edcca_regs->ppdu_mask); 6323 6324 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6325 edcca_regs->edcca_mask, EDCCA_MAX); 6326 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6327 edcca_regs->edcca_p_mask, EDCCA_MAX); 6328 rtw89_phy_write32_mask(rtwdev, edcca_regs->ppdu_level, 6329 edcca_regs->ppdu_mask, EDCCA_MAX); 6330 } else { 6331 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6332 edcca_regs->edcca_mask, 6333 edcca_bak->a); 6334 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6335 edcca_regs->edcca_p_mask, 6336 edcca_bak->p); 6337 rtw89_phy_write32_mask(rtwdev, edcca_regs->ppdu_level, 6338 edcca_regs->ppdu_mask, 6339 edcca_bak->ppdu); 6340 } 6341 } 6342 6343 static void rtw89_phy_edcca_log(struct rtw89_dev *rtwdev) 6344 { 6345 const struct rtw89_edcca_regs *edcca_regs = rtwdev->chip->edcca_regs; 6346 bool flag_fb, flag_p20, flag_s20, flag_s40, flag_s80; 6347 s8 pwdb_fb, pwdb_p20, pwdb_s20, pwdb_s40, pwdb_s80; 6348 u8 path, per20_bitmap; 6349 u8 pwdb[8]; 6350 u32 tmp; 6351 6352 if (!rtw89_debug_is_enabled(rtwdev, RTW89_DBG_EDCCA)) 6353 return; 6354 6355 if (rtwdev->chip->chip_id == RTL8922A) 6356 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel_be, 6357 edcca_regs->rpt_sel_be_mask, 0); 6358 6359 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6360 edcca_regs->rpt_sel_mask, 0); 6361 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_b); 6362 path = u32_get_bits(tmp, B_EDCCA_RPT_B_PATH_MASK); 6363 flag_s80 = u32_get_bits(tmp, B_EDCCA_RPT_B_S80); 6364 flag_s40 = u32_get_bits(tmp, B_EDCCA_RPT_B_S40); 6365 flag_s20 = u32_get_bits(tmp, B_EDCCA_RPT_B_S20); 6366 flag_p20 = u32_get_bits(tmp, B_EDCCA_RPT_B_P20); 6367 flag_fb = u32_get_bits(tmp, B_EDCCA_RPT_B_FB); 6368 pwdb_s20 = u32_get_bits(tmp, MASKBYTE1); 6369 pwdb_p20 = u32_get_bits(tmp, MASKBYTE2); 6370 pwdb_fb = u32_get_bits(tmp, MASKBYTE3); 6371 6372 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6373 edcca_regs->rpt_sel_mask, 4); 6374 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_b); 6375 pwdb_s80 = u32_get_bits(tmp, MASKBYTE1); 6376 pwdb_s40 = u32_get_bits(tmp, MASKBYTE2); 6377 6378 per20_bitmap = rtw89_phy_read32_mask(rtwdev, edcca_regs->rpt_a, 6379 MASKBYTE0); 6380 6381 if (rtwdev->chip->chip_id == RTL8922A) { 6382 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel_be, 6383 edcca_regs->rpt_sel_be_mask, 4); 6384 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_b); 6385 pwdb[0] = u32_get_bits(tmp, MASKBYTE3); 6386 pwdb[1] = u32_get_bits(tmp, MASKBYTE2); 6387 pwdb[2] = u32_get_bits(tmp, MASKBYTE1); 6388 pwdb[3] = u32_get_bits(tmp, MASKBYTE0); 6389 6390 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel_be, 6391 edcca_regs->rpt_sel_be_mask, 5); 6392 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_b); 6393 pwdb[4] = u32_get_bits(tmp, MASKBYTE3); 6394 pwdb[5] = u32_get_bits(tmp, MASKBYTE2); 6395 pwdb[6] = u32_get_bits(tmp, MASKBYTE1); 6396 pwdb[7] = u32_get_bits(tmp, MASKBYTE0); 6397 } else { 6398 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6399 edcca_regs->rpt_sel_mask, 0); 6400 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_a); 6401 pwdb[0] = u32_get_bits(tmp, MASKBYTE3); 6402 pwdb[1] = u32_get_bits(tmp, MASKBYTE2); 6403 6404 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6405 edcca_regs->rpt_sel_mask, 1); 6406 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_a); 6407 pwdb[2] = u32_get_bits(tmp, MASKBYTE3); 6408 pwdb[3] = u32_get_bits(tmp, MASKBYTE2); 6409 6410 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6411 edcca_regs->rpt_sel_mask, 2); 6412 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_a); 6413 pwdb[4] = u32_get_bits(tmp, MASKBYTE3); 6414 pwdb[5] = u32_get_bits(tmp, MASKBYTE2); 6415 6416 rtw89_phy_write32_mask(rtwdev, edcca_regs->rpt_sel, 6417 edcca_regs->rpt_sel_mask, 3); 6418 tmp = rtw89_phy_read32(rtwdev, edcca_regs->rpt_a); 6419 pwdb[6] = u32_get_bits(tmp, MASKBYTE3); 6420 pwdb[7] = u32_get_bits(tmp, MASKBYTE2); 6421 } 6422 6423 rtw89_debug(rtwdev, RTW89_DBG_EDCCA, 6424 "[EDCCA]: edcca_bitmap = %04x\n", per20_bitmap); 6425 6426 rtw89_debug(rtwdev, RTW89_DBG_EDCCA, 6427 "[EDCCA]: pwdb per20{0,1,2,3,4,5,6,7} = {%d,%d,%d,%d,%d,%d,%d,%d}(dBm)\n", 6428 pwdb[0], pwdb[1], pwdb[2], pwdb[3], pwdb[4], pwdb[5], 6429 pwdb[6], pwdb[7]); 6430 6431 rtw89_debug(rtwdev, RTW89_DBG_EDCCA, 6432 "[EDCCA]: path=%d, flag {FB,p20,s20,s40,s80} = {%d,%d,%d,%d,%d}\n", 6433 path, flag_fb, flag_p20, flag_s20, flag_s40, flag_s80); 6434 6435 rtw89_debug(rtwdev, RTW89_DBG_EDCCA, 6436 "[EDCCA]: pwdb {FB,p20,s20,s40,s80} = {%d,%d,%d,%d,%d}(dBm)\n", 6437 pwdb_fb, pwdb_p20, pwdb_s20, pwdb_s40, pwdb_s80); 6438 } 6439 6440 static u8 rtw89_phy_edcca_get_thre_by_rssi(struct rtw89_dev *rtwdev) 6441 { 6442 struct rtw89_phy_ch_info *ch_info = &rtwdev->ch_info; 6443 bool is_linked = rtwdev->total_sta_assoc > 0; 6444 u8 rssi_min = ch_info->rssi_min >> 1; 6445 u8 edcca_thre; 6446 6447 if (!is_linked) { 6448 edcca_thre = EDCCA_MAX; 6449 } else { 6450 edcca_thre = rssi_min - RSSI_UNIT_CONVER + EDCCA_UNIT_CONVER - 6451 EDCCA_TH_REF; 6452 edcca_thre = max_t(u8, edcca_thre, EDCCA_TH_L2H_LB); 6453 } 6454 6455 return edcca_thre; 6456 } 6457 6458 void rtw89_phy_edcca_thre_calc(struct rtw89_dev *rtwdev) 6459 { 6460 const struct rtw89_edcca_regs *edcca_regs = rtwdev->chip->edcca_regs; 6461 struct rtw89_edcca_bak *edcca_bak = &rtwdev->hal.edcca_bak; 6462 u8 th; 6463 6464 th = rtw89_phy_edcca_get_thre_by_rssi(rtwdev); 6465 if (th == edcca_bak->th_old) 6466 return; 6467 6468 edcca_bak->th_old = th; 6469 6470 rtw89_debug(rtwdev, RTW89_DBG_EDCCA, 6471 "[EDCCA]: Normal Mode, EDCCA_th = %d\n", th); 6472 6473 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6474 edcca_regs->edcca_mask, th); 6475 rtw89_phy_write32_mask(rtwdev, edcca_regs->edcca_level, 6476 edcca_regs->edcca_p_mask, th); 6477 rtw89_phy_write32_mask(rtwdev, edcca_regs->ppdu_level, 6478 edcca_regs->ppdu_mask, th); 6479 } 6480 6481 void rtw89_phy_edcca_track(struct rtw89_dev *rtwdev) 6482 { 6483 struct rtw89_hal *hal = &rtwdev->hal; 6484 6485 if (hal->disabled_dm_bitmap & BIT(RTW89_DM_DYNAMIC_EDCCA)) 6486 return; 6487 6488 rtw89_phy_edcca_thre_calc(rtwdev); 6489 rtw89_phy_edcca_log(rtwdev); 6490 } 6491 6492 enum rtw89_rf_path_bit rtw89_phy_get_kpath(struct rtw89_dev *rtwdev, 6493 enum rtw89_phy_idx phy_idx) 6494 { 6495 rtw89_debug(rtwdev, RTW89_DBG_RFK, 6496 "[RFK] kpath dbcc_en: 0x%x, mode=0x%x, PHY%d\n", 6497 rtwdev->dbcc_en, rtwdev->mlo_dbcc_mode, phy_idx); 6498 6499 switch (rtwdev->mlo_dbcc_mode) { 6500 case MLO_1_PLUS_1_1RF: 6501 if (phy_idx == RTW89_PHY_0) 6502 return RF_A; 6503 else 6504 return RF_B; 6505 case MLO_1_PLUS_1_2RF: 6506 if (phy_idx == RTW89_PHY_0) 6507 return RF_A; 6508 else 6509 return RF_D; 6510 case MLO_0_PLUS_2_1RF: 6511 case MLO_2_PLUS_0_1RF: 6512 /* for both PHY 0/1 */ 6513 return RF_AB; 6514 case MLO_0_PLUS_2_2RF: 6515 case MLO_2_PLUS_0_2RF: 6516 case MLO_2_PLUS_2_2RF: 6517 default: 6518 if (phy_idx == RTW89_PHY_0) 6519 return RF_AB; 6520 else 6521 return RF_CD; 6522 } 6523 } 6524 EXPORT_SYMBOL(rtw89_phy_get_kpath); 6525 6526 enum rtw89_rf_path rtw89_phy_get_syn_sel(struct rtw89_dev *rtwdev, 6527 enum rtw89_phy_idx phy_idx) 6528 { 6529 rtw89_debug(rtwdev, RTW89_DBG_RFK, 6530 "[RFK] kpath dbcc_en: 0x%x, mode=0x%x, PHY%d\n", 6531 rtwdev->dbcc_en, rtwdev->mlo_dbcc_mode, phy_idx); 6532 6533 switch (rtwdev->mlo_dbcc_mode) { 6534 case MLO_1_PLUS_1_1RF: 6535 if (phy_idx == RTW89_PHY_0) 6536 return RF_PATH_A; 6537 else 6538 return RF_PATH_B; 6539 case MLO_1_PLUS_1_2RF: 6540 if (phy_idx == RTW89_PHY_0) 6541 return RF_PATH_A; 6542 else 6543 return RF_PATH_D; 6544 case MLO_0_PLUS_2_1RF: 6545 case MLO_2_PLUS_0_1RF: 6546 if (phy_idx == RTW89_PHY_0) 6547 return RF_PATH_A; 6548 else 6549 return RF_PATH_B; 6550 case MLO_0_PLUS_2_2RF: 6551 case MLO_2_PLUS_0_2RF: 6552 case MLO_2_PLUS_2_2RF: 6553 default: 6554 if (phy_idx == RTW89_PHY_0) 6555 return RF_PATH_A; 6556 else 6557 return RF_PATH_C; 6558 } 6559 } 6560 EXPORT_SYMBOL(rtw89_phy_get_syn_sel); 6561 6562 static const struct rtw89_ccx_regs rtw89_ccx_regs_ax = { 6563 .setting_addr = R_CCX, 6564 .edcca_opt_mask = B_CCX_EDCCA_OPT_MSK, 6565 .measurement_trig_mask = B_MEASUREMENT_TRIG_MSK, 6566 .trig_opt_mask = B_CCX_TRIG_OPT_MSK, 6567 .en_mask = B_CCX_EN_MSK, 6568 .ifs_cnt_addr = R_IFS_COUNTER, 6569 .ifs_clm_period_mask = B_IFS_CLM_PERIOD_MSK, 6570 .ifs_clm_cnt_unit_mask = B_IFS_CLM_COUNTER_UNIT_MSK, 6571 .ifs_clm_cnt_clear_mask = B_IFS_COUNTER_CLR_MSK, 6572 .ifs_collect_en_mask = B_IFS_COLLECT_EN, 6573 .ifs_t1_addr = R_IFS_T1, 6574 .ifs_t1_th_h_mask = B_IFS_T1_TH_HIGH_MSK, 6575 .ifs_t1_en_mask = B_IFS_T1_EN_MSK, 6576 .ifs_t1_th_l_mask = B_IFS_T1_TH_LOW_MSK, 6577 .ifs_t2_addr = R_IFS_T2, 6578 .ifs_t2_th_h_mask = B_IFS_T2_TH_HIGH_MSK, 6579 .ifs_t2_en_mask = B_IFS_T2_EN_MSK, 6580 .ifs_t2_th_l_mask = B_IFS_T2_TH_LOW_MSK, 6581 .ifs_t3_addr = R_IFS_T3, 6582 .ifs_t3_th_h_mask = B_IFS_T3_TH_HIGH_MSK, 6583 .ifs_t3_en_mask = B_IFS_T3_EN_MSK, 6584 .ifs_t3_th_l_mask = B_IFS_T3_TH_LOW_MSK, 6585 .ifs_t4_addr = R_IFS_T4, 6586 .ifs_t4_th_h_mask = B_IFS_T4_TH_HIGH_MSK, 6587 .ifs_t4_en_mask = B_IFS_T4_EN_MSK, 6588 .ifs_t4_th_l_mask = B_IFS_T4_TH_LOW_MSK, 6589 .ifs_clm_tx_cnt_addr = R_IFS_CLM_TX_CNT, 6590 .ifs_clm_edcca_excl_cca_fa_mask = B_IFS_CLM_EDCCA_EXCLUDE_CCA_FA_MSK, 6591 .ifs_clm_tx_cnt_msk = B_IFS_CLM_TX_CNT_MSK, 6592 .ifs_clm_cca_addr = R_IFS_CLM_CCA, 6593 .ifs_clm_ofdmcca_excl_fa_mask = B_IFS_CLM_OFDMCCA_EXCLUDE_FA_MSK, 6594 .ifs_clm_cckcca_excl_fa_mask = B_IFS_CLM_CCKCCA_EXCLUDE_FA_MSK, 6595 .ifs_clm_fa_addr = R_IFS_CLM_FA, 6596 .ifs_clm_ofdm_fa_mask = B_IFS_CLM_OFDM_FA_MSK, 6597 .ifs_clm_cck_fa_mask = B_IFS_CLM_CCK_FA_MSK, 6598 .ifs_his_addr = R_IFS_HIS, 6599 .ifs_t4_his_mask = B_IFS_T4_HIS_MSK, 6600 .ifs_t3_his_mask = B_IFS_T3_HIS_MSK, 6601 .ifs_t2_his_mask = B_IFS_T2_HIS_MSK, 6602 .ifs_t1_his_mask = B_IFS_T1_HIS_MSK, 6603 .ifs_avg_l_addr = R_IFS_AVG_L, 6604 .ifs_t2_avg_mask = B_IFS_T2_AVG_MSK, 6605 .ifs_t1_avg_mask = B_IFS_T1_AVG_MSK, 6606 .ifs_avg_h_addr = R_IFS_AVG_H, 6607 .ifs_t4_avg_mask = B_IFS_T4_AVG_MSK, 6608 .ifs_t3_avg_mask = B_IFS_T3_AVG_MSK, 6609 .ifs_cca_l_addr = R_IFS_CCA_L, 6610 .ifs_t2_cca_mask = B_IFS_T2_CCA_MSK, 6611 .ifs_t1_cca_mask = B_IFS_T1_CCA_MSK, 6612 .ifs_cca_h_addr = R_IFS_CCA_H, 6613 .ifs_t4_cca_mask = B_IFS_T4_CCA_MSK, 6614 .ifs_t3_cca_mask = B_IFS_T3_CCA_MSK, 6615 .ifs_total_addr = R_IFSCNT, 6616 .ifs_cnt_done_mask = B_IFSCNT_DONE_MSK, 6617 .ifs_total_mask = B_IFSCNT_TOTAL_CNT_MSK, 6618 }; 6619 6620 static const struct rtw89_physts_regs rtw89_physts_regs_ax = { 6621 .setting_addr = R_PLCP_HISTOGRAM, 6622 .dis_trigger_fail_mask = B_STS_DIS_TRIG_BY_FAIL, 6623 .dis_trigger_brk_mask = B_STS_DIS_TRIG_BY_BRK, 6624 }; 6625 6626 static const struct rtw89_cfo_regs rtw89_cfo_regs_ax = { 6627 .comp = R_DCFO_WEIGHT, 6628 .weighting_mask = B_DCFO_WEIGHT_MSK, 6629 .comp_seg0 = R_DCFO_OPT, 6630 .valid_0_mask = B_DCFO_OPT_EN, 6631 }; 6632 6633 const struct rtw89_phy_gen_def rtw89_phy_gen_ax = { 6634 .cr_base = 0x10000, 6635 .ccx = &rtw89_ccx_regs_ax, 6636 .physts = &rtw89_physts_regs_ax, 6637 .cfo = &rtw89_cfo_regs_ax, 6638 .phy0_phy1_offset = rtw89_phy0_phy1_offset_ax, 6639 .config_bb_gain = rtw89_phy_config_bb_gain_ax, 6640 .preinit_rf_nctl = rtw89_phy_preinit_rf_nctl_ax, 6641 .bb_wrap_init = NULL, 6642 .ch_info_init = NULL, 6643 6644 .set_txpwr_byrate = rtw89_phy_set_txpwr_byrate_ax, 6645 .set_txpwr_offset = rtw89_phy_set_txpwr_offset_ax, 6646 .set_txpwr_limit = rtw89_phy_set_txpwr_limit_ax, 6647 .set_txpwr_limit_ru = rtw89_phy_set_txpwr_limit_ru_ax, 6648 }; 6649 EXPORT_SYMBOL(rtw89_phy_gen_ax); 6650