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 "core.h" 8 #include "debug.h" 9 #include "fw.h" 10 #include "mac.h" 11 #include "phy.h" 12 #include "ps.h" 13 #include "reg.h" 14 #include "util.h" 15 16 static int rtw89_fw_leave_lps_check(struct rtw89_dev *rtwdev, u8 macid) 17 { 18 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def; 19 u32 pwr_en_bit = 0xE; 20 u32 chk_msk = pwr_en_bit << (4 * macid); 21 u32 polling; 22 int ret; 23 24 ret = read_poll_timeout_atomic(rtw89_read32_mask, polling, !polling, 25 1000, 50000, false, rtwdev, 26 mac->ps_status, chk_msk); 27 if (ret) { 28 rtw89_info(rtwdev, "rtw89: failed to leave lps state\n"); 29 return -EBUSY; 30 } 31 32 return 0; 33 } 34 35 static void rtw89_ps_power_mode_change_with_hci(struct rtw89_dev *rtwdev, 36 bool enter) 37 { 38 ieee80211_stop_queues(rtwdev->hw); 39 rtwdev->hci.paused = true; 40 flush_work(&rtwdev->txq_work); 41 ieee80211_wake_queues(rtwdev->hw); 42 43 rtw89_hci_pause(rtwdev, true); 44 rtw89_mac_power_mode_change(rtwdev, enter); 45 rtw89_hci_switch_mode(rtwdev, enter); 46 rtw89_hci_pause(rtwdev, false); 47 48 rtwdev->hci.paused = false; 49 50 if (!enter) { 51 local_bh_disable(); 52 napi_schedule(&rtwdev->napi); 53 local_bh_enable(); 54 } 55 } 56 57 static void rtw89_ps_power_mode_change(struct rtw89_dev *rtwdev, bool enter) 58 { 59 if (rtwdev->chip->low_power_hci_modes & BIT(rtwdev->ps_mode) && 60 !test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) 61 rtw89_ps_power_mode_change_with_hci(rtwdev, enter); 62 else 63 rtw89_mac_power_mode_change(rtwdev, enter); 64 } 65 66 void __rtw89_enter_ps_mode(struct rtw89_dev *rtwdev) 67 { 68 if (!rtwdev->ps_mode) 69 return; 70 71 if (test_and_set_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags)) 72 return; 73 74 rtw89_ps_power_mode_change(rtwdev, true); 75 } 76 77 void __rtw89_leave_ps_mode(struct rtw89_dev *rtwdev) 78 { 79 if (!rtwdev->ps_mode) 80 return; 81 82 if (test_and_clear_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags)) 83 rtw89_ps_power_mode_change(rtwdev, false); 84 } 85 86 static void __rtw89_enter_lps_link(struct rtw89_dev *rtwdev, 87 struct rtw89_vif_link *rtwvif_link) 88 { 89 struct rtw89_lps_parm lps_param = { 90 .macid = rtwvif_link->mac_id, 91 .psmode = RTW89_MAC_AX_PS_MODE_LEGACY, 92 .lastrpwm = RTW89_LAST_RPWM_PS, 93 }; 94 95 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_FW_CTRL); 96 rtw89_fw_h2c_lps_parm(rtwdev, &lps_param); 97 } 98 99 static void __rtw89_leave_lps(struct rtw89_dev *rtwdev, 100 struct rtw89_vif_link *rtwvif_link) 101 { 102 struct rtw89_lps_parm lps_param = { 103 .macid = rtwvif_link->mac_id, 104 .psmode = RTW89_MAC_AX_PS_MODE_ACTIVE, 105 .lastrpwm = RTW89_LAST_RPWM_ACTIVE, 106 }; 107 108 rtw89_fw_h2c_lps_parm(rtwdev, &lps_param); 109 rtw89_fw_leave_lps_check(rtwdev, 0); 110 rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON); 111 rtw89_chip_digital_pwr_comp(rtwdev, rtwvif_link->phy_idx); 112 } 113 114 void rtw89_leave_ps_mode(struct rtw89_dev *rtwdev) 115 { 116 lockdep_assert_wiphy(rtwdev->hw->wiphy); 117 118 __rtw89_leave_ps_mode(rtwdev); 119 } 120 121 void rtw89_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif, 122 bool ps_mode) 123 { 124 struct rtw89_vif_link *rtwvif_link; 125 bool can_ps_mode = true; 126 unsigned int link_id; 127 128 lockdep_assert_wiphy(rtwdev->hw->wiphy); 129 130 if (test_and_set_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags)) 131 return; 132 133 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) { 134 __rtw89_enter_lps_link(rtwdev, rtwvif_link); 135 136 if (rtwvif_link->wifi_role == RTW89_WIFI_ROLE_P2P_CLIENT) 137 can_ps_mode = false; 138 } 139 140 if (RTW89_CHK_FW_FEATURE(LPS_CH_INFO, &rtwdev->fw)) 141 rtw89_fw_h2c_lps_ch_info(rtwdev, rtwvif); 142 else 143 rtw89_fw_h2c_lps_ml_cmn_info(rtwdev, rtwvif); 144 145 if (ps_mode && can_ps_mode) 146 __rtw89_enter_ps_mode(rtwdev); 147 } 148 149 static void rtw89_leave_lps_vif(struct rtw89_dev *rtwdev, 150 struct rtw89_vif_link *rtwvif_link) 151 { 152 if (rtwvif_link->wifi_role != RTW89_WIFI_ROLE_STATION && 153 rtwvif_link->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT) 154 return; 155 156 __rtw89_leave_lps(rtwdev, rtwvif_link); 157 } 158 159 void rtw89_leave_lps(struct rtw89_dev *rtwdev) 160 { 161 struct rtw89_vif_link *rtwvif_link; 162 struct rtw89_vif *rtwvif; 163 unsigned int link_id; 164 165 lockdep_assert_wiphy(rtwdev->hw->wiphy); 166 167 if (!test_and_clear_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags)) 168 return; 169 170 __rtw89_leave_ps_mode(rtwdev); 171 172 rtw89_phy_dm_reinit(rtwdev); 173 174 rtw89_for_each_rtwvif(rtwdev, rtwvif) 175 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) 176 rtw89_leave_lps_vif(rtwdev, rtwvif_link); 177 } 178 179 void rtw89_enter_ips(struct rtw89_dev *rtwdev) 180 { 181 struct rtw89_vif_link *rtwvif_link; 182 struct rtw89_vif *rtwvif; 183 unsigned int link_id; 184 185 set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags); 186 187 if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 188 return; 189 190 rtw89_for_each_rtwvif(rtwdev, rtwvif) 191 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) 192 rtw89_mac_vif_deinit(rtwdev, rtwvif_link); 193 194 rtw89_core_stop(rtwdev); 195 } 196 197 void rtw89_leave_ips(struct rtw89_dev *rtwdev) 198 { 199 struct rtw89_vif_link *rtwvif_link; 200 struct rtw89_vif *rtwvif; 201 unsigned int link_id; 202 int ret; 203 204 if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 205 return; 206 207 ret = rtw89_core_start(rtwdev); 208 if (ret) 209 rtw89_err(rtwdev, "failed to leave idle state\n"); 210 211 rtw89_set_channel(rtwdev); 212 213 rtw89_for_each_rtwvif(rtwdev, rtwvif) 214 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) 215 rtw89_mac_vif_init(rtwdev, rtwvif_link); 216 217 clear_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags); 218 } 219 220 void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl) 221 { 222 if (btc_ctrl) 223 rtw89_leave_lps(rtwdev); 224 } 225 226 static void rtw89_tsf32_toggle(struct rtw89_dev *rtwdev, 227 struct rtw89_vif_link *rtwvif_link, 228 enum rtw89_p2pps_action act) 229 { 230 if (act == RTW89_P2P_ACT_UPDATE || act == RTW89_P2P_ACT_REMOVE) 231 return; 232 233 if (act == RTW89_P2P_ACT_INIT) 234 rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, true); 235 else if (act == RTW89_P2P_ACT_TERMINATE) 236 rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, false); 237 } 238 239 static void rtw89_p2p_disable_all_noa(struct rtw89_dev *rtwdev, 240 struct rtw89_vif_link *rtwvif_link, 241 struct ieee80211_bss_conf *bss_conf) 242 { 243 enum rtw89_p2pps_action act; 244 u8 noa_id; 245 246 if (rtwvif_link->last_noa_nr == 0) 247 return; 248 249 for (noa_id = 0; noa_id < rtwvif_link->last_noa_nr; noa_id++) { 250 if (noa_id == rtwvif_link->last_noa_nr - 1) 251 act = RTW89_P2P_ACT_TERMINATE; 252 else 253 act = RTW89_P2P_ACT_REMOVE; 254 rtw89_tsf32_toggle(rtwdev, rtwvif_link, act); 255 rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, bss_conf, 256 NULL, act, noa_id); 257 } 258 } 259 260 static void rtw89_p2p_update_noa(struct rtw89_dev *rtwdev, 261 struct rtw89_vif_link *rtwvif_link, 262 struct ieee80211_bss_conf *bss_conf) 263 { 264 struct ieee80211_p2p_noa_desc *desc; 265 enum rtw89_p2pps_action act; 266 u8 noa_id; 267 268 for (noa_id = 0; noa_id < RTW89_P2P_MAX_NOA_NUM; noa_id++) { 269 desc = &bss_conf->p2p_noa_attr.desc[noa_id]; 270 if (!desc->count || !desc->duration) 271 break; 272 273 if (noa_id == 0) 274 act = RTW89_P2P_ACT_INIT; 275 else 276 act = RTW89_P2P_ACT_UPDATE; 277 rtw89_tsf32_toggle(rtwdev, rtwvif_link, act); 278 rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, bss_conf, 279 desc, act, noa_id); 280 } 281 rtwvif_link->last_noa_nr = noa_id; 282 } 283 284 void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev, 285 struct rtw89_vif_link *rtwvif_link, 286 struct ieee80211_bss_conf *bss_conf) 287 { 288 rtw89_p2p_disable_all_noa(rtwdev, rtwvif_link, bss_conf); 289 rtw89_p2p_update_noa(rtwdev, rtwvif_link, bss_conf); 290 } 291 292 void rtw89_recalc_lps(struct rtw89_dev *rtwdev) 293 { 294 struct ieee80211_vif *vif, *found_vif = NULL; 295 struct rtw89_vif *rtwvif; 296 enum rtw89_entity_mode mode; 297 int count = 0; 298 299 mode = rtw89_get_entity_mode(rtwdev); 300 if (mode == RTW89_ENTITY_MODE_MCC) 301 goto disable_lps; 302 303 rtw89_for_each_rtwvif(rtwdev, rtwvif) { 304 vif = rtwvif_to_vif(rtwvif); 305 306 if (vif->type != NL80211_IFTYPE_STATION) { 307 count = 0; 308 break; 309 } 310 311 count++; 312 found_vif = vif; 313 } 314 315 if (count == 1 && found_vif->cfg.ps) { 316 rtwdev->lps_enabled = true; 317 return; 318 } 319 320 disable_lps: 321 rtw89_leave_lps(rtwdev); 322 rtwdev->lps_enabled = false; 323 } 324 325 void rtw89_p2p_noa_renew(struct rtw89_vif_link *rtwvif_link) 326 { 327 struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa; 328 struct rtw89_p2p_noa_ie *ie = &setter->ie; 329 struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head; 330 struct rtw89_noa_attr_head *noa_head = &ie->noa_head; 331 332 if (setter->noa_count) { 333 setter->noa_index++; 334 setter->noa_count = 0; 335 } 336 337 memset(ie, 0, sizeof(*ie)); 338 339 p2p_head->eid = WLAN_EID_VENDOR_SPECIFIC; 340 p2p_head->ie_len = 4 + sizeof(*noa_head); 341 p2p_head->oui[0] = (WLAN_OUI_WFA >> 16) & 0xff; 342 p2p_head->oui[1] = (WLAN_OUI_WFA >> 8) & 0xff; 343 p2p_head->oui[2] = (WLAN_OUI_WFA >> 0) & 0xff; 344 p2p_head->oui_type = WLAN_OUI_TYPE_WFA_P2P; 345 346 noa_head->attr_type = IEEE80211_P2P_ATTR_ABSENCE_NOTICE; 347 noa_head->attr_len = cpu_to_le16(2); 348 noa_head->index = setter->noa_index; 349 noa_head->oppps_ctwindow = 0; 350 } 351 352 void rtw89_p2p_noa_append(struct rtw89_vif_link *rtwvif_link, 353 const struct ieee80211_p2p_noa_desc *desc) 354 { 355 struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa; 356 struct rtw89_p2p_noa_ie *ie = &setter->ie; 357 struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head; 358 struct rtw89_noa_attr_head *noa_head = &ie->noa_head; 359 360 if (!desc->count || !desc->duration) 361 return; 362 363 if (setter->noa_count >= RTW89_P2P_MAX_NOA_NUM) 364 return; 365 366 p2p_head->ie_len += sizeof(*desc); 367 le16_add_cpu(&noa_head->attr_len, sizeof(*desc)); 368 369 ie->noa_desc[setter->noa_count++] = *desc; 370 } 371 372 u8 rtw89_p2p_noa_fetch(struct rtw89_vif_link *rtwvif_link, void **data) 373 { 374 struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa; 375 struct rtw89_p2p_noa_ie *ie = &setter->ie; 376 void *tail; 377 378 if (!setter->noa_count) 379 return 0; 380 381 *data = ie; 382 tail = ie->noa_desc + setter->noa_count; 383 return tail - *data; 384 } 385 386 static void rtw89_ps_noa_once_set_work(struct wiphy *wiphy, struct wiphy_work *work) 387 { 388 struct rtw89_ps_noa_once_handler *noa_once = 389 container_of(work, struct rtw89_ps_noa_once_handler, set_work.work); 390 391 lockdep_assert_wiphy(wiphy); 392 393 noa_once->in_duration = true; 394 } 395 396 static void rtw89_ps_noa_once_clr_work(struct wiphy *wiphy, struct wiphy_work *work) 397 { 398 struct rtw89_ps_noa_once_handler *noa_once = 399 container_of(work, struct rtw89_ps_noa_once_handler, clr_work.work); 400 struct rtw89_vif_link *rtwvif_link = 401 container_of(noa_once, struct rtw89_vif_link, noa_once); 402 struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev; 403 404 lockdep_assert_wiphy(wiphy); 405 406 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true); 407 noa_once->in_duration = false; 408 } 409 410 void rtw89_p2p_noa_once_init(struct rtw89_vif_link *rtwvif_link) 411 { 412 struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once; 413 414 noa_once->in_duration = false; 415 noa_once->tsf_begin = 0; 416 noa_once->tsf_end = 0; 417 418 wiphy_delayed_work_init(&noa_once->set_work, rtw89_ps_noa_once_set_work); 419 wiphy_delayed_work_init(&noa_once->clr_work, rtw89_ps_noa_once_clr_work); 420 } 421 422 static void rtw89_p2p_noa_once_cancel(struct rtw89_vif_link *rtwvif_link) 423 { 424 struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once; 425 struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev; 426 struct wiphy *wiphy = rtwdev->hw->wiphy; 427 428 wiphy_delayed_work_cancel(wiphy, &noa_once->set_work); 429 wiphy_delayed_work_cancel(wiphy, &noa_once->clr_work); 430 } 431 432 void rtw89_p2p_noa_once_deinit(struct rtw89_vif_link *rtwvif_link) 433 { 434 rtw89_p2p_noa_once_cancel(rtwvif_link); 435 rtw89_p2p_noa_once_init(rtwvif_link); 436 } 437 438 void rtw89_p2p_noa_once_recalc(struct rtw89_vif_link *rtwvif_link) 439 { 440 struct rtw89_ps_noa_once_handler *noa_once = &rtwvif_link->noa_once; 441 struct rtw89_dev *rtwdev = rtwvif_link->rtwvif->rtwdev; 442 const struct ieee80211_p2p_noa_desc *noa_desc; 443 struct wiphy *wiphy = rtwdev->hw->wiphy; 444 struct ieee80211_bss_conf *bss_conf; 445 u64 tsf_begin = U64_MAX, tsf_end; 446 u64 set_delay_us = 0; 447 u64 clr_delay_us = 0; 448 u32 start_time; 449 u32 interval; 450 u32 duration; 451 u64 tsf; 452 int ret; 453 int i; 454 455 lockdep_assert_wiphy(wiphy); 456 457 ret = rtw89_mac_port_get_tsf(rtwdev, rtwvif_link, &tsf); 458 if (ret) { 459 rtw89_warn(rtwdev, "%s: failed to get tsf\n", __func__); 460 return; 461 } 462 463 rcu_read_lock(); 464 465 bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true); 466 467 for (i = 0; i < ARRAY_SIZE(bss_conf->p2p_noa_attr.desc); i++) { 468 bool first = tsf_begin == U64_MAX; 469 u64 tmp; 470 471 noa_desc = &bss_conf->p2p_noa_attr.desc[i]; 472 if (noa_desc->count == 0 || noa_desc->count == 255) 473 continue; 474 475 start_time = le32_to_cpu(noa_desc->start_time); 476 interval = le32_to_cpu(noa_desc->interval); 477 duration = le32_to_cpu(noa_desc->duration); 478 479 if (unlikely(duration == 0 || 480 (noa_desc->count > 1 && interval == 0))) 481 continue; 482 483 tmp = start_time + interval * (noa_desc->count - 1) + duration; 484 tmp = (tsf & GENMASK_ULL(63, 32)) + tmp; 485 if (unlikely(tmp <= tsf)) 486 continue; 487 tsf_end = first ? tmp : max(tsf_end, tmp); 488 489 tmp = (tsf & GENMASK_ULL(63, 32)) | start_time; 490 tsf_begin = first ? tmp : min(tsf_begin, tmp); 491 } 492 493 rcu_read_unlock(); 494 495 if (tsf_begin == U64_MAX) 496 return; 497 498 rtw89_p2p_noa_once_cancel(rtwvif_link); 499 500 if (noa_once->tsf_end > tsf) { 501 tsf_begin = min(tsf_begin, noa_once->tsf_begin); 502 tsf_end = max(tsf_end, noa_once->tsf_end); 503 } 504 505 clr_delay_us = min_t(u64, tsf_end - tsf, UINT_MAX); 506 507 if (tsf_begin <= tsf) { 508 noa_once->in_duration = true; 509 goto out; 510 } 511 512 set_delay_us = tsf_begin - tsf; 513 if (unlikely(set_delay_us > UINT_MAX)) { 514 rtw89_warn(rtwdev, "%s: unhandled begin\n", __func__); 515 set_delay_us = 0; 516 clr_delay_us = 0; 517 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true); 518 noa_once->in_duration = false; 519 } 520 521 out: 522 if (set_delay_us) 523 wiphy_delayed_work_queue(wiphy, &noa_once->set_work, 524 usecs_to_jiffies(set_delay_us)); 525 if (clr_delay_us) 526 wiphy_delayed_work_queue(wiphy, &noa_once->clr_work, 527 usecs_to_jiffies(clr_delay_us)); 528 529 noa_once->tsf_begin = tsf_begin; 530 noa_once->tsf_end = tsf_end; 531 } 532