ps.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright(c) 2019-2020 Realtek Corporation
  3. */
  4. #include "coex.h"
  5. #include "core.h"
  6. #include "debug.h"
  7. #include "fw.h"
  8. #include "mac.h"
  9. #include "ps.h"
  10. #include "reg.h"
  11. #include "util.h"
  12. static int rtw89_fw_leave_lps_check(struct rtw89_dev *rtwdev, u8 macid)
  13. {
  14. u32 pwr_en_bit = 0xE;
  15. u32 chk_msk = pwr_en_bit << (4 * macid);
  16. u32 polling;
  17. int ret;
  18. ret = read_poll_timeout_atomic(rtw89_read32_mask, polling, !polling,
  19. 1000, 50000, false, rtwdev,
  20. R_AX_PPWRBIT_SETTING, chk_msk);
  21. if (ret) {
  22. rtw89_info(rtwdev, "rtw89: failed to leave lps state\n");
  23. return -EBUSY;
  24. }
  25. return 0;
  26. }
  27. static void rtw89_ps_power_mode_change_with_hci(struct rtw89_dev *rtwdev,
  28. bool enter)
  29. {
  30. ieee80211_stop_queues(rtwdev->hw);
  31. rtwdev->hci.paused = true;
  32. flush_work(&rtwdev->txq_work);
  33. ieee80211_wake_queues(rtwdev->hw);
  34. rtw89_hci_pause(rtwdev, true);
  35. rtw89_mac_power_mode_change(rtwdev, enter);
  36. rtw89_hci_switch_mode(rtwdev, enter);
  37. rtw89_hci_pause(rtwdev, false);
  38. rtwdev->hci.paused = false;
  39. if (!enter) {
  40. local_bh_disable();
  41. napi_schedule(&rtwdev->napi);
  42. local_bh_enable();
  43. }
  44. }
  45. static void rtw89_ps_power_mode_change(struct rtw89_dev *rtwdev, bool enter)
  46. {
  47. if (rtwdev->chip->low_power_hci_modes & BIT(rtwdev->ps_mode))
  48. rtw89_ps_power_mode_change_with_hci(rtwdev, enter);
  49. else
  50. rtw89_mac_power_mode_change(rtwdev, enter);
  51. }
  52. static void __rtw89_enter_ps_mode(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  53. {
  54. if (rtwvif->wifi_role == RTW89_WIFI_ROLE_P2P_CLIENT)
  55. return;
  56. if (!rtwdev->ps_mode)
  57. return;
  58. if (test_and_set_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
  59. return;
  60. rtw89_ps_power_mode_change(rtwdev, true);
  61. }
  62. void __rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
  63. {
  64. if (!rtwdev->ps_mode)
  65. return;
  66. if (test_and_clear_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
  67. rtw89_ps_power_mode_change(rtwdev, false);
  68. }
  69. static void __rtw89_enter_lps(struct rtw89_dev *rtwdev, u8 mac_id)
  70. {
  71. struct rtw89_lps_parm lps_param = {
  72. .macid = mac_id,
  73. .psmode = RTW89_MAC_AX_PS_MODE_LEGACY,
  74. .lastrpwm = RTW89_LAST_RPWM_PS,
  75. };
  76. rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_FW_CTRL);
  77. rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
  78. }
  79. static void __rtw89_leave_lps(struct rtw89_dev *rtwdev, u8 mac_id)
  80. {
  81. struct rtw89_lps_parm lps_param = {
  82. .macid = mac_id,
  83. .psmode = RTW89_MAC_AX_PS_MODE_ACTIVE,
  84. .lastrpwm = RTW89_LAST_RPWM_ACTIVE,
  85. };
  86. rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
  87. rtw89_fw_leave_lps_check(rtwdev, 0);
  88. rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
  89. }
  90. void rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
  91. {
  92. lockdep_assert_held(&rtwdev->mutex);
  93. __rtw89_leave_ps_mode(rtwdev);
  94. }
  95. void rtw89_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  96. {
  97. lockdep_assert_held(&rtwdev->mutex);
  98. if (test_and_set_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
  99. return;
  100. __rtw89_enter_lps(rtwdev, rtwvif->mac_id);
  101. __rtw89_enter_ps_mode(rtwdev, rtwvif);
  102. }
  103. static void rtw89_leave_lps_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  104. {
  105. if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION &&
  106. rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT)
  107. return;
  108. __rtw89_leave_lps(rtwdev, rtwvif->mac_id);
  109. }
  110. void rtw89_leave_lps(struct rtw89_dev *rtwdev)
  111. {
  112. struct rtw89_vif *rtwvif;
  113. lockdep_assert_held(&rtwdev->mutex);
  114. if (!test_and_clear_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
  115. return;
  116. __rtw89_leave_ps_mode(rtwdev);
  117. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  118. rtw89_leave_lps_vif(rtwdev, rtwvif);
  119. }
  120. void rtw89_enter_ips(struct rtw89_dev *rtwdev)
  121. {
  122. struct rtw89_vif *rtwvif;
  123. set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
  124. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  125. rtw89_mac_vif_deinit(rtwdev, rtwvif);
  126. rtw89_core_stop(rtwdev);
  127. }
  128. void rtw89_leave_ips(struct rtw89_dev *rtwdev)
  129. {
  130. struct rtw89_vif *rtwvif;
  131. int ret;
  132. ret = rtw89_core_start(rtwdev);
  133. if (ret)
  134. rtw89_err(rtwdev, "failed to leave idle state\n");
  135. rtw89_set_channel(rtwdev);
  136. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  137. rtw89_mac_vif_init(rtwdev, rtwvif);
  138. clear_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
  139. }
  140. void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl)
  141. {
  142. if (btc_ctrl)
  143. rtw89_leave_lps(rtwdev);
  144. }
  145. static void rtw89_tsf32_toggle(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
  146. enum rtw89_p2pps_action act)
  147. {
  148. if (act == RTW89_P2P_ACT_UPDATE || act == RTW89_P2P_ACT_REMOVE)
  149. return;
  150. if (act == RTW89_P2P_ACT_INIT)
  151. rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif, true);
  152. else if (act == RTW89_P2P_ACT_TERMINATE)
  153. rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif, false);
  154. }
  155. static void rtw89_p2p_disable_all_noa(struct rtw89_dev *rtwdev,
  156. struct ieee80211_vif *vif)
  157. {
  158. struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
  159. enum rtw89_p2pps_action act;
  160. u8 noa_id;
  161. if (rtwvif->last_noa_nr == 0)
  162. return;
  163. for (noa_id = 0; noa_id < rtwvif->last_noa_nr; noa_id++) {
  164. if (noa_id == rtwvif->last_noa_nr - 1)
  165. act = RTW89_P2P_ACT_TERMINATE;
  166. else
  167. act = RTW89_P2P_ACT_REMOVE;
  168. rtw89_tsf32_toggle(rtwdev, rtwvif, act);
  169. rtw89_fw_h2c_p2p_act(rtwdev, vif, NULL, act, noa_id);
  170. }
  171. }
  172. static void rtw89_p2p_update_noa(struct rtw89_dev *rtwdev,
  173. struct ieee80211_vif *vif)
  174. {
  175. struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
  176. struct ieee80211_p2p_noa_desc *desc;
  177. enum rtw89_p2pps_action act;
  178. u8 noa_id;
  179. for (noa_id = 0; noa_id < RTW89_P2P_MAX_NOA_NUM; noa_id++) {
  180. desc = &vif->bss_conf.p2p_noa_attr.desc[noa_id];
  181. if (!desc->count || !desc->duration)
  182. break;
  183. if (noa_id == 0)
  184. act = RTW89_P2P_ACT_INIT;
  185. else
  186. act = RTW89_P2P_ACT_UPDATE;
  187. rtw89_tsf32_toggle(rtwdev, rtwvif, act);
  188. rtw89_fw_h2c_p2p_act(rtwdev, vif, desc, act, noa_id);
  189. }
  190. rtwvif->last_noa_nr = noa_id;
  191. }
  192. void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
  193. {
  194. rtw89_p2p_disable_all_noa(rtwdev, vif);
  195. rtw89_p2p_update_noa(rtwdev, vif);
  196. }
  197. void rtw89_recalc_lps(struct rtw89_dev *rtwdev)
  198. {
  199. struct ieee80211_vif *vif, *found_vif = NULL;
  200. struct rtw89_vif *rtwvif;
  201. int count = 0;
  202. rtw89_for_each_rtwvif(rtwdev, rtwvif) {
  203. vif = rtwvif_to_vif(rtwvif);
  204. if (vif->type != NL80211_IFTYPE_STATION) {
  205. count = 0;
  206. break;
  207. }
  208. count++;
  209. found_vif = vif;
  210. }
  211. if (count == 1 && found_vif->cfg.ps) {
  212. rtwdev->lps_enabled = true;
  213. } else {
  214. rtw89_leave_lps(rtwdev);
  215. rtwdev->lps_enabled = false;
  216. }
  217. }