1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2019-2022 Realtek Corporation 3 */ 4 #include "cam.h" 5 #include "core.h" 6 #include "debug.h" 7 #include "fw.h" 8 #include "mac.h" 9 #include "phy.h" 10 #include "ps.h" 11 #include "reg.h" 12 #include "util.h" 13 #include "wow.h" 14 15 static void rtw89_wow_leave_deep_ps(struct rtw89_dev *rtwdev) 16 { 17 __rtw89_leave_ps_mode(rtwdev); 18 } 19 20 static void rtw89_wow_enter_deep_ps(struct rtw89_dev *rtwdev) 21 { 22 struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif; 23 struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv; 24 25 __rtw89_enter_ps_mode(rtwdev, rtwvif); 26 } 27 28 static void rtw89_wow_enter_lps(struct rtw89_dev *rtwdev) 29 { 30 struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif; 31 struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv; 32 33 rtw89_enter_lps(rtwdev, rtwvif); 34 } 35 36 static void rtw89_wow_leave_lps(struct rtw89_dev *rtwdev) 37 { 38 rtw89_leave_lps(rtwdev); 39 } 40 41 static int rtw89_wow_config_mac(struct rtw89_dev *rtwdev, bool enable_wow) 42 { 43 int ret; 44 45 if (enable_wow) { 46 ret = rtw89_mac_resize_ple_rx_quota(rtwdev, true); 47 if (ret) { 48 rtw89_err(rtwdev, "[ERR]patch rx qta %d\n", ret); 49 return ret; 50 } 51 rtw89_write32_set(rtwdev, R_AX_RX_FUNCTION_STOP, B_AX_HDR_RX_STOP); 52 rtw89_write32_clr(rtwdev, R_AX_RX_FLTR_OPT, B_AX_SNIFFER_MODE); 53 rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false); 54 rtw89_write32(rtwdev, R_AX_ACTION_FWD0, 0); 55 rtw89_write32(rtwdev, R_AX_ACTION_FWD1, 0); 56 rtw89_write32(rtwdev, R_AX_TF_FWD, 0); 57 rtw89_write32(rtwdev, R_AX_HW_RPT_FWD, 0); 58 } else { 59 ret = rtw89_mac_resize_ple_rx_quota(rtwdev, false); 60 if (ret) { 61 rtw89_err(rtwdev, "[ERR]patch rx qta %d\n", ret); 62 return ret; 63 } 64 rtw89_write32_clr(rtwdev, R_AX_RX_FUNCTION_STOP, B_AX_HDR_RX_STOP); 65 rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true); 66 rtw89_write32(rtwdev, R_AX_ACTION_FWD0, TRXCFG_MPDU_PROC_ACT_FRWD); 67 rtw89_write32(rtwdev, R_AX_TF_FWD, TRXCFG_MPDU_PROC_TF_FRWD); 68 } 69 70 return 0; 71 } 72 73 static void rtw89_wow_set_rx_filter(struct rtw89_dev *rtwdev, bool enable) 74 { 75 enum rtw89_mac_fwd_target fwd_target = enable ? 76 RTW89_FWD_DONT_CARE : 77 RTW89_FWD_TO_HOST; 78 79 rtw89_mac_typ_fltr_opt(rtwdev, RTW89_MGNT, fwd_target, RTW89_MAC_0); 80 rtw89_mac_typ_fltr_opt(rtwdev, RTW89_CTRL, fwd_target, RTW89_MAC_0); 81 rtw89_mac_typ_fltr_opt(rtwdev, RTW89_DATA, fwd_target, RTW89_MAC_0); 82 } 83 84 static void rtw89_wow_show_wakeup_reason(struct rtw89_dev *rtwdev) 85 { 86 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 87 struct cfg80211_wowlan_nd_info nd_info; 88 struct cfg80211_wowlan_wakeup wakeup = { 89 .pattern_idx = -1, 90 }; 91 u32 wow_reason_reg; 92 u8 reason; 93 94 if (chip_id == RTL8852A || chip_id == RTL8852B) 95 wow_reason_reg = R_AX_C2HREG_DATA3 + 3; 96 else 97 wow_reason_reg = R_AX_C2HREG_DATA3_V1 + 3; 98 99 reason = rtw89_read8(rtwdev, wow_reason_reg); 100 101 switch (reason) { 102 case RTW89_WOW_RSN_RX_DEAUTH: 103 wakeup.disconnect = true; 104 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx deauth\n"); 105 break; 106 case RTW89_WOW_RSN_DISCONNECT: 107 wakeup.disconnect = true; 108 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: AP is off\n"); 109 break; 110 case RTW89_WOW_RSN_RX_MAGIC_PKT: 111 wakeup.magic_pkt = true; 112 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx magic packet\n"); 113 break; 114 case RTW89_WOW_RSN_RX_GTK_REKEY: 115 wakeup.gtk_rekey_failure = true; 116 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx gtk rekey\n"); 117 break; 118 case RTW89_WOW_RSN_RX_PATTERN_MATCH: 119 /* Current firmware and driver don't report pattern index 120 * Use pattern_idx to 0 defaultly. 121 */ 122 wakeup.pattern_idx = 0; 123 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx pattern match packet\n"); 124 break; 125 case RTW89_WOW_RSN_RX_NLO: 126 /* Current firmware and driver don't report ssid index. 127 * Use 0 for n_matches based on its comment. 128 */ 129 nd_info.n_matches = 0; 130 wakeup.net_detect = &nd_info; 131 rtw89_debug(rtwdev, RTW89_DBG_WOW, "Rx NLO\n"); 132 break; 133 default: 134 rtw89_warn(rtwdev, "Unknown wakeup reason %x\n", reason); 135 ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, NULL, 136 GFP_KERNEL); 137 return; 138 } 139 140 ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, &wakeup, 141 GFP_KERNEL); 142 } 143 144 static void rtw89_wow_vif_iter(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 145 { 146 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 147 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 148 149 /* Current wowlan function support setting of only one STATION vif. 150 * So when one suitable vif is found, stop the iteration. 151 */ 152 if (rtw_wow->wow_vif || vif->type != NL80211_IFTYPE_STATION) 153 return; 154 155 switch (rtwvif->net_type) { 156 case RTW89_NET_TYPE_INFRA: 157 rtw_wow->wow_vif = vif; 158 break; 159 case RTW89_NET_TYPE_NO_LINK: 160 default: 161 break; 162 } 163 } 164 165 static u16 __rtw89_cal_crc16(u8 data, u16 crc) 166 { 167 u8 shift_in, data_bit; 168 u8 crc_bit4, crc_bit11, crc_bit15; 169 u16 crc_result; 170 int index; 171 172 for (index = 0; index < 8; index++) { 173 crc_bit15 = crc & BIT(15) ? 1 : 0; 174 data_bit = data & BIT(index) ? 1 : 0; 175 shift_in = crc_bit15 ^ data_bit; 176 177 crc_result = crc << 1; 178 179 if (shift_in == 0) 180 crc_result &= ~BIT(0); 181 else 182 crc_result |= BIT(0); 183 184 crc_bit11 = (crc & BIT(11) ? 1 : 0) ^ shift_in; 185 186 if (crc_bit11 == 0) 187 crc_result &= ~BIT(12); 188 else 189 crc_result |= BIT(12); 190 191 crc_bit4 = (crc & BIT(4) ? 1 : 0) ^ shift_in; 192 193 if (crc_bit4 == 0) 194 crc_result &= ~BIT(5); 195 else 196 crc_result |= BIT(5); 197 198 crc = crc_result; 199 } 200 return crc; 201 } 202 203 static u16 rtw89_calc_crc(u8 *pdata, int length) 204 { 205 u16 crc = 0xffff; 206 int i; 207 208 for (i = 0; i < length; i++) 209 crc = __rtw89_cal_crc16(pdata[i], crc); 210 211 /* get 1' complement */ 212 return ~crc; 213 } 214 215 static int rtw89_wow_pattern_get_type(struct rtw89_vif *rtwvif, 216 struct rtw89_wow_cam_info *rtw_pattern, 217 const u8 *pattern, u8 da_mask) 218 { 219 u8 da[ETH_ALEN]; 220 221 ether_addr_copy_mask(da, pattern, da_mask); 222 223 /* Each pattern is divided into different kinds by DA address 224 * a. DA is broadcast address: set bc = 0; 225 * b. DA is multicast address: set mc = 0 226 * c. DA is unicast address same as dev's mac address: set uc = 0 227 * d. DA is unmasked. Also called wildcard type: set uc = bc = mc = 0 228 * e. Others is invalid type. 229 */ 230 231 if (is_broadcast_ether_addr(da)) 232 rtw_pattern->bc = true; 233 else if (is_multicast_ether_addr(da)) 234 rtw_pattern->mc = true; 235 else if (ether_addr_equal(da, rtwvif->mac_addr) && 236 da_mask == GENMASK(5, 0)) 237 rtw_pattern->uc = true; 238 else if (!da_mask) /*da_mask == 0 mean wildcard*/ 239 return 0; 240 else 241 return -EPERM; 242 243 return 0; 244 } 245 246 static int rtw89_wow_pattern_generate(struct rtw89_dev *rtwdev, 247 struct rtw89_vif *rtwvif, 248 const struct cfg80211_pkt_pattern *pkt_pattern, 249 struct rtw89_wow_cam_info *rtw_pattern) 250 { 251 u8 mask_hw[RTW89_MAX_PATTERN_MASK_SIZE * 4] = {0}; 252 u8 content[RTW89_MAX_PATTERN_SIZE] = {0}; 253 const u8 *mask; 254 const u8 *pattern; 255 u8 mask_len; 256 u16 count; 257 u32 len; 258 int i, ret; 259 260 pattern = pkt_pattern->pattern; 261 len = pkt_pattern->pattern_len; 262 mask = pkt_pattern->mask; 263 mask_len = DIV_ROUND_UP(len, 8); 264 memset(rtw_pattern, 0, sizeof(*rtw_pattern)); 265 266 ret = rtw89_wow_pattern_get_type(rtwvif, rtw_pattern, pattern, 267 mask[0] & GENMASK(5, 0)); 268 if (ret) 269 return ret; 270 271 /* translate mask from os to mask for hw 272 * pattern from OS uses 'ethenet frame', like this: 273 * | 6 | 6 | 2 | 20 | Variable | 4 | 274 * |--------+--------+------+-----------+------------+-----| 275 * | 802.3 Mac Header | IP Header | TCP Packet | FCS | 276 * | DA | SA | Type | 277 * 278 * BUT, packet catched by our HW is in '802.11 frame', begin from LLC 279 * | 24 or 30 | 6 | 2 | 20 | Variable | 4 | 280 * |-------------------+--------+------+-----------+------------+-----| 281 * | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS | 282 * | Others | Tpye | 283 * 284 * Therefore, we need translate mask_from_OS to mask_to_hw. 285 * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0, 286 * because new mask[0~5] means 'SA', but our HW packet begins from LLC, 287 * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match. 288 */ 289 290 /* Shift 6 bits */ 291 for (i = 0; i < mask_len - 1; i++) { 292 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6)) | 293 u8_get_bits(mask[i + 1], GENMASK(5, 0)) << 2; 294 } 295 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6)); 296 297 /* Set bit 0-5 to zero */ 298 mask_hw[0] &= ~GENMASK(5, 0); 299 300 memcpy(rtw_pattern->mask, mask_hw, sizeof(rtw_pattern->mask)); 301 302 /* To get the wake up pattern from the mask. 303 * We do not count first 12 bits which means 304 * DA[6] and SA[6] in the pattern to match HW design. 305 */ 306 count = 0; 307 for (i = 12; i < len; i++) { 308 if ((mask[i / 8] >> (i % 8)) & 0x01) { 309 content[count] = pattern[i]; 310 count++; 311 } 312 } 313 314 rtw_pattern->crc = rtw89_calc_crc(content, count); 315 316 return 0; 317 } 318 319 static int rtw89_wow_parse_patterns(struct rtw89_dev *rtwdev, 320 struct rtw89_vif *rtwvif, 321 struct cfg80211_wowlan *wowlan) 322 { 323 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 324 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns; 325 int i; 326 int ret; 327 328 if (!wowlan->n_patterns || !wowlan->patterns) 329 return 0; 330 331 for (i = 0; i < wowlan->n_patterns; i++) { 332 rtw_pattern = &rtw_wow->patterns[i]; 333 ret = rtw89_wow_pattern_generate(rtwdev, rtwvif, 334 &wowlan->patterns[i], 335 rtw_pattern); 336 if (ret) { 337 rtw89_err(rtwdev, "failed to generate pattern(%d)\n", i); 338 rtw_wow->pattern_cnt = 0; 339 return ret; 340 } 341 342 rtw_pattern->r_w = true; 343 rtw_pattern->idx = i; 344 rtw_pattern->negative_pattern_match = false; 345 rtw_pattern->skip_mac_hdr = true; 346 rtw_pattern->valid = true; 347 } 348 rtw_wow->pattern_cnt = wowlan->n_patterns; 349 350 return 0; 351 } 352 353 static void rtw89_wow_pattern_clear_cam(struct rtw89_dev *rtwdev) 354 { 355 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 356 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns; 357 int i = 0; 358 359 for (i = 0; i < rtw_wow->pattern_cnt; i++) { 360 rtw_pattern = &rtw_wow->patterns[i]; 361 rtw_pattern->valid = false; 362 rtw89_fw_wow_cam_update(rtwdev, rtw_pattern); 363 } 364 } 365 366 static void rtw89_wow_pattern_write(struct rtw89_dev *rtwdev) 367 { 368 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 369 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns; 370 int i; 371 372 for (i = 0; i < rtw_wow->pattern_cnt; i++) 373 rtw89_fw_wow_cam_update(rtwdev, rtw_pattern + i); 374 } 375 376 static void rtw89_wow_pattern_clear(struct rtw89_dev *rtwdev) 377 { 378 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 379 380 rtw89_wow_pattern_clear_cam(rtwdev); 381 382 rtw_wow->pattern_cnt = 0; 383 memset(rtw_wow->patterns, 0, sizeof(rtw_wow->patterns)); 384 } 385 386 static void rtw89_wow_clear_wakeups(struct rtw89_dev *rtwdev) 387 { 388 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 389 390 rtw_wow->wow_vif = NULL; 391 rtw89_core_release_all_bits_map(rtw_wow->flags, RTW89_WOW_FLAG_NUM); 392 rtw_wow->pattern_cnt = 0; 393 } 394 395 static int rtw89_wow_set_wakeups(struct rtw89_dev *rtwdev, 396 struct cfg80211_wowlan *wowlan) 397 { 398 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 399 struct rtw89_vif *rtwvif; 400 401 if (wowlan->disconnect) 402 set_bit(RTW89_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags); 403 if (wowlan->magic_pkt) 404 set_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags); 405 406 rtw89_for_each_rtwvif(rtwdev, rtwvif) 407 rtw89_wow_vif_iter(rtwdev, rtwvif); 408 409 if (!rtw_wow->wow_vif) 410 return -EPERM; 411 412 rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv; 413 return rtw89_wow_parse_patterns(rtwdev, rtwvif, wowlan); 414 } 415 416 static int rtw89_wow_cfg_wake(struct rtw89_dev *rtwdev, bool wow) 417 { 418 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 419 struct ieee80211_vif *wow_vif = rtw_wow->wow_vif; 420 struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv; 421 struct ieee80211_sta *wow_sta; 422 struct rtw89_sta *rtwsta = NULL; 423 bool is_conn = true; 424 int ret; 425 426 wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid); 427 if (wow_sta) 428 rtwsta = (struct rtw89_sta *)wow_sta->drv_priv; 429 else 430 is_conn = false; 431 432 if (wow) { 433 if (rtw_wow->pattern_cnt) 434 rtwvif->wowlan_pattern = true; 435 if (test_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags)) 436 rtwvif->wowlan_magic = true; 437 } else { 438 rtwvif->wowlan_pattern = false; 439 rtwvif->wowlan_magic = false; 440 } 441 442 ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif, wow); 443 if (ret) { 444 rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n"); 445 return ret; 446 } 447 448 if (wow) { 449 ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif, rtwsta); 450 if (ret) { 451 rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n", 452 ret); 453 return ret; 454 } 455 } 456 457 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, !is_conn); 458 if (ret) { 459 rtw89_warn(rtwdev, "failed to send h2c join info\n"); 460 return ret; 461 } 462 463 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL); 464 if (ret) { 465 rtw89_warn(rtwdev, "failed to send h2c cam\n"); 466 return ret; 467 } 468 469 ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif, wow); 470 if (ret) { 471 rtw89_err(rtwdev, "failed to fw wow global\n"); 472 return ret; 473 } 474 475 return 0; 476 } 477 478 static int rtw89_wow_check_fw_status(struct rtw89_dev *rtwdev, bool wow_enable) 479 { 480 u8 polling; 481 int ret; 482 483 ret = read_poll_timeout_atomic(rtw89_read8_mask, polling, 484 wow_enable == !!polling, 485 50, 50000, false, rtwdev, 486 R_AX_WOW_CTRL, B_AX_WOW_WOWEN); 487 if (ret) 488 rtw89_err(rtwdev, "failed to check wow status %s\n", 489 wow_enable ? "enabled" : "disabled"); 490 return ret; 491 } 492 493 static void rtw89_wow_release_pkt_list(struct rtw89_dev *rtwdev) 494 { 495 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 496 struct list_head *pkt_list = &rtw_wow->pkt_list; 497 struct rtw89_pktofld_info *info, *tmp; 498 499 list_for_each_entry_safe(info, tmp, pkt_list, list) { 500 rtw89_fw_h2c_del_pkt_offload(rtwdev, info->id); 501 rtw89_core_release_bit_map(rtwdev->pkt_offload, 502 info->id); 503 list_del(&info->list); 504 kfree(info); 505 } 506 } 507 508 static int rtw89_wow_swap_fw(struct rtw89_dev *rtwdev, bool wow) 509 { 510 enum rtw89_fw_type fw_type = wow ? RTW89_FW_WOWLAN : RTW89_FW_NORMAL; 511 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 512 struct ieee80211_vif *wow_vif = rtw_wow->wow_vif; 513 struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv; 514 struct ieee80211_sta *wow_sta; 515 struct rtw89_sta *rtwsta = NULL; 516 bool is_conn = true; 517 int ret; 518 519 rtw89_hci_disable_intr(rtwdev); 520 521 wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid); 522 if (wow_sta) 523 rtwsta = (struct rtw89_sta *)wow_sta->drv_priv; 524 else 525 is_conn = false; 526 527 ret = rtw89_fw_download(rtwdev, fw_type); 528 if (ret) { 529 rtw89_warn(rtwdev, "download fw failed\n"); 530 return ret; 531 } 532 533 rtw89_phy_init_rf_reg(rtwdev, true); 534 535 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta, 536 RTW89_ROLE_FW_RESTORE); 537 if (ret) { 538 rtw89_warn(rtwdev, "failed to send h2c role maintain\n"); 539 return ret; 540 } 541 542 ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, wow_vif, wow_sta); 543 if (ret) { 544 rtw89_warn(rtwdev, "failed to send h2c assoc cmac tbl\n"); 545 return ret; 546 } 547 548 if (!is_conn) 549 rtw89_cam_reset_keys(rtwdev); 550 551 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, !is_conn); 552 if (ret) { 553 rtw89_warn(rtwdev, "failed to send h2c join info\n"); 554 return ret; 555 } 556 557 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL); 558 if (ret) { 559 rtw89_warn(rtwdev, "failed to send h2c cam\n"); 560 return ret; 561 } 562 563 if (is_conn) { 564 rtw89_phy_ra_assoc(rtwdev, wow_sta); 565 rtw89_phy_set_bss_color(rtwdev, wow_vif); 566 rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, wow_vif); 567 } 568 569 rtw89_mac_hw_mgnt_sec(rtwdev, wow); 570 rtw89_hci_enable_intr(rtwdev); 571 572 return 0; 573 } 574 575 static int rtw89_wow_enable_trx_pre(struct rtw89_dev *rtwdev) 576 { 577 int ret; 578 579 rtw89_hci_ctrl_txdma_ch(rtwdev, false); 580 rtw89_hci_ctrl_txdma_fw_ch(rtwdev, true); 581 582 rtw89_mac_ptk_drop_by_band_and_wait(rtwdev, RTW89_MAC_0); 583 584 ret = rtw89_hci_poll_txdma_ch(rtwdev); 585 if (ret) { 586 rtw89_err(rtwdev, "txdma ch busy\n"); 587 return ret; 588 } 589 rtw89_wow_set_rx_filter(rtwdev, true); 590 591 ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false); 592 if (ret) { 593 rtw89_err(rtwdev, "cfg ppdu status\n"); 594 return ret; 595 } 596 597 return 0; 598 } 599 600 static int rtw89_wow_enable_trx_post(struct rtw89_dev *rtwdev) 601 { 602 int ret; 603 604 rtw89_hci_disable_intr(rtwdev); 605 rtw89_hci_ctrl_trxhci(rtwdev, false); 606 607 ret = rtw89_hci_poll_txdma_ch(rtwdev); 608 if (ret) { 609 rtw89_err(rtwdev, "failed to poll txdma ch idle pcie\n"); 610 return ret; 611 } 612 613 ret = rtw89_wow_config_mac(rtwdev, true); 614 if (ret) { 615 rtw89_err(rtwdev, "failed to config mac\n"); 616 return ret; 617 } 618 619 rtw89_wow_set_rx_filter(rtwdev, false); 620 rtw89_hci_reset(rtwdev); 621 622 return 0; 623 } 624 625 static int rtw89_wow_disable_trx_pre(struct rtw89_dev *rtwdev) 626 { 627 int ret; 628 629 rtw89_hci_clr_idx_all(rtwdev); 630 631 ret = rtw89_hci_rst_bdram(rtwdev); 632 if (ret) { 633 rtw89_warn(rtwdev, "reset bdram busy\n"); 634 return ret; 635 } 636 637 rtw89_hci_ctrl_trxhci(rtwdev, true); 638 rtw89_hci_ctrl_txdma_ch(rtwdev, true); 639 640 ret = rtw89_wow_config_mac(rtwdev, false); 641 if (ret) { 642 rtw89_err(rtwdev, "failed to config mac\n"); 643 return ret; 644 } 645 rtw89_hci_enable_intr(rtwdev); 646 647 return 0; 648 } 649 650 static int rtw89_wow_disable_trx_post(struct rtw89_dev *rtwdev) 651 { 652 int ret; 653 654 ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true); 655 if (ret) 656 rtw89_err(rtwdev, "cfg ppdu status\n"); 657 658 return ret; 659 } 660 661 static int rtw89_wow_fw_start(struct rtw89_dev *rtwdev) 662 { 663 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 664 struct rtw89_vif *rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv; 665 int ret; 666 667 rtw89_wow_pattern_write(rtwdev); 668 669 ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, true); 670 if (ret) { 671 rtw89_err(rtwdev, "wow: failed to enable keep alive\n"); 672 return ret; 673 } 674 675 ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, true); 676 if (ret) { 677 rtw89_err(rtwdev, "wow: failed to enable disconnect detect\n"); 678 goto out; 679 } 680 681 ret = rtw89_wow_cfg_wake(rtwdev, true); 682 if (ret) { 683 rtw89_err(rtwdev, "wow: failed to config wake\n"); 684 goto out; 685 } 686 687 ret = rtw89_wow_check_fw_status(rtwdev, true); 688 if (ret) { 689 rtw89_err(rtwdev, "wow: failed to check enable fw ready\n"); 690 goto out; 691 } 692 693 out: 694 return ret; 695 } 696 697 static int rtw89_wow_fw_stop(struct rtw89_dev *rtwdev) 698 { 699 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 700 struct rtw89_vif *rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv; 701 int ret; 702 703 rtw89_wow_pattern_clear(rtwdev); 704 705 ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, false); 706 if (ret) { 707 rtw89_err(rtwdev, "wow: failed to disable keep alive\n"); 708 goto out; 709 } 710 711 rtw89_wow_release_pkt_list(rtwdev); 712 713 ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, false); 714 if (ret) { 715 rtw89_err(rtwdev, "wow: failed to disable disconnect detect\n"); 716 goto out; 717 } 718 719 ret = rtw89_wow_cfg_wake(rtwdev, false); 720 if (ret) { 721 rtw89_err(rtwdev, "wow: failed to disable config wake\n"); 722 goto out; 723 } 724 725 ret = rtw89_wow_check_fw_status(rtwdev, false); 726 if (ret) { 727 rtw89_err(rtwdev, "wow: failed to check disable fw ready\n"); 728 goto out; 729 } 730 731 out: 732 return ret; 733 } 734 735 static int rtw89_wow_enable(struct rtw89_dev *rtwdev) 736 { 737 int ret; 738 739 set_bit(RTW89_FLAG_WOWLAN, rtwdev->flags); 740 741 ret = rtw89_wow_enable_trx_pre(rtwdev); 742 if (ret) { 743 rtw89_err(rtwdev, "wow: failed to enable trx_pre\n"); 744 goto out; 745 } 746 747 ret = rtw89_wow_swap_fw(rtwdev, true); 748 if (ret) { 749 rtw89_err(rtwdev, "wow: failed to swap to wow fw\n"); 750 goto out; 751 } 752 753 ret = rtw89_wow_fw_start(rtwdev); 754 if (ret) { 755 rtw89_err(rtwdev, "wow: failed to let wow fw start\n"); 756 goto out; 757 } 758 759 rtw89_wow_enter_lps(rtwdev); 760 761 ret = rtw89_wow_enable_trx_post(rtwdev); 762 if (ret) { 763 rtw89_err(rtwdev, "wow: failed to enable trx_post\n"); 764 goto out; 765 } 766 767 return 0; 768 769 out: 770 clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags); 771 return ret; 772 } 773 774 static int rtw89_wow_disable(struct rtw89_dev *rtwdev) 775 { 776 int ret; 777 778 ret = rtw89_wow_disable_trx_pre(rtwdev); 779 if (ret) { 780 rtw89_err(rtwdev, "wow: failed to disable trx_pre\n"); 781 goto out; 782 } 783 784 rtw89_wow_leave_lps(rtwdev); 785 786 ret = rtw89_wow_fw_stop(rtwdev); 787 if (ret) { 788 rtw89_err(rtwdev, "wow: failed to swap to normal fw\n"); 789 goto out; 790 } 791 792 ret = rtw89_wow_swap_fw(rtwdev, false); 793 if (ret) { 794 rtw89_err(rtwdev, "wow: failed to disable trx_post\n"); 795 goto out; 796 } 797 798 ret = rtw89_wow_disable_trx_post(rtwdev); 799 if (ret) { 800 rtw89_err(rtwdev, "wow: failed to disable trx_pre\n"); 801 goto out; 802 } 803 804 out: 805 clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags); 806 return ret; 807 } 808 809 int rtw89_wow_resume(struct rtw89_dev *rtwdev) 810 { 811 int ret; 812 813 if (!test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) { 814 rtw89_err(rtwdev, "wow is not enabled\n"); 815 ret = -EPERM; 816 goto out; 817 } 818 819 if (!rtw89_mac_get_power_state(rtwdev)) { 820 rtw89_err(rtwdev, "chip is no power when resume\n"); 821 ret = -EPERM; 822 goto out; 823 } 824 825 rtw89_wow_leave_deep_ps(rtwdev); 826 827 rtw89_wow_show_wakeup_reason(rtwdev); 828 829 ret = rtw89_wow_disable(rtwdev); 830 if (ret) 831 rtw89_err(rtwdev, "failed to disable wow\n"); 832 833 out: 834 rtw89_wow_clear_wakeups(rtwdev); 835 return ret; 836 } 837 838 int rtw89_wow_suspend(struct rtw89_dev *rtwdev, struct cfg80211_wowlan *wowlan) 839 { 840 int ret; 841 842 ret = rtw89_wow_set_wakeups(rtwdev, wowlan); 843 if (ret) { 844 rtw89_err(rtwdev, "failed to set wakeup event\n"); 845 return ret; 846 } 847 848 rtw89_wow_leave_lps(rtwdev); 849 850 ret = rtw89_wow_enable(rtwdev); 851 if (ret) { 852 rtw89_err(rtwdev, "failed to enable wow\n"); 853 return ret; 854 } 855 856 rtw89_wow_enter_deep_ps(rtwdev); 857 858 return 0; 859 } 860