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