ps.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright(c) 2019-2020 Realtek Corporation
  3. */
  4. #include "chan.h"
  5. #include "coex.h"
  6. #include "core.h"
  7. #include "debug.h"
  8. #include "fw.h"
  9. #include "mac.h"
  10. #include "ps.h"
  11. #include "reg.h"
  12. #include "util.h"
  13. static int rtw89_fw_leave_lps_check(struct rtw89_dev *rtwdev, u8 macid)
  14. {
  15. u32 pwr_en_bit = 0xE;
  16. u32 chk_msk = pwr_en_bit << (4 * macid);
  17. u32 polling;
  18. int ret;
  19. ret = read_poll_timeout_atomic(rtw89_read32_mask, polling, !polling,
  20. 1000, 50000, false, rtwdev,
  21. R_AX_PPWRBIT_SETTING, chk_msk);
  22. if (ret) {
  23. rtw89_info(rtwdev, "rtw89: failed to leave lps state\n");
  24. return -EBUSY;
  25. }
  26. return 0;
  27. }
  28. static void rtw89_ps_power_mode_change_with_hci(struct rtw89_dev *rtwdev,
  29. bool enter)
  30. {
  31. ieee80211_stop_queues(rtwdev->hw);
  32. rtwdev->hci.paused = true;
  33. flush_work(&rtwdev->txq_work);
  34. ieee80211_wake_queues(rtwdev->hw);
  35. rtw89_hci_pause(rtwdev, true);
  36. rtw89_mac_power_mode_change(rtwdev, enter);
  37. rtw89_hci_switch_mode(rtwdev, enter);
  38. rtw89_hci_pause(rtwdev, false);
  39. rtwdev->hci.paused = false;
  40. if (!enter) {
  41. local_bh_disable();
  42. napi_schedule(&rtwdev->napi);
  43. local_bh_enable();
  44. }
  45. }
  46. static void rtw89_ps_power_mode_change(struct rtw89_dev *rtwdev, bool enter)
  47. {
  48. if (rtwdev->chip->low_power_hci_modes & BIT(rtwdev->ps_mode))
  49. rtw89_ps_power_mode_change_with_hci(rtwdev, enter);
  50. else
  51. rtw89_mac_power_mode_change(rtwdev, enter);
  52. }
  53. void __rtw89_enter_ps_mode(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  54. {
  55. if (rtwvif->wifi_role == RTW89_WIFI_ROLE_P2P_CLIENT)
  56. return;
  57. if (!rtwdev->ps_mode)
  58. return;
  59. if (test_and_set_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
  60. return;
  61. rtw89_ps_power_mode_change(rtwdev, true);
  62. }
  63. void __rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
  64. {
  65. if (!rtwdev->ps_mode)
  66. return;
  67. if (test_and_clear_bit(RTW89_FLAG_LOW_POWER_MODE, rtwdev->flags))
  68. rtw89_ps_power_mode_change(rtwdev, false);
  69. }
  70. static void __rtw89_enter_lps(struct rtw89_dev *rtwdev, u8 mac_id)
  71. {
  72. struct rtw89_lps_parm lps_param = {
  73. .macid = mac_id,
  74. .psmode = RTW89_MAC_AX_PS_MODE_LEGACY,
  75. .lastrpwm = RTW89_LAST_RPWM_PS,
  76. };
  77. rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_FW_CTRL);
  78. rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
  79. }
  80. static void __rtw89_leave_lps(struct rtw89_dev *rtwdev, u8 mac_id)
  81. {
  82. struct rtw89_lps_parm lps_param = {
  83. .macid = mac_id,
  84. .psmode = RTW89_MAC_AX_PS_MODE_ACTIVE,
  85. .lastrpwm = RTW89_LAST_RPWM_ACTIVE,
  86. };
  87. rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
  88. rtw89_fw_leave_lps_check(rtwdev, 0);
  89. rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
  90. }
  91. void rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
  92. {
  93. lockdep_assert_held(&rtwdev->mutex);
  94. __rtw89_leave_ps_mode(rtwdev);
  95. }
  96. void rtw89_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
  97. bool ps_mode)
  98. {
  99. lockdep_assert_held(&rtwdev->mutex);
  100. if (test_and_set_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
  101. return;
  102. __rtw89_enter_lps(rtwdev, rtwvif->mac_id);
  103. if (ps_mode)
  104. __rtw89_enter_ps_mode(rtwdev, rtwvif);
  105. }
  106. static void rtw89_leave_lps_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  107. {
  108. if (rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION &&
  109. rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT)
  110. return;
  111. __rtw89_leave_lps(rtwdev, rtwvif->mac_id);
  112. }
  113. void rtw89_leave_lps(struct rtw89_dev *rtwdev)
  114. {
  115. struct rtw89_vif *rtwvif;
  116. lockdep_assert_held(&rtwdev->mutex);
  117. if (!test_and_clear_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
  118. return;
  119. __rtw89_leave_ps_mode(rtwdev);
  120. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  121. rtw89_leave_lps_vif(rtwdev, rtwvif);
  122. }
  123. void rtw89_enter_ips(struct rtw89_dev *rtwdev)
  124. {
  125. struct rtw89_vif *rtwvif;
  126. set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
  127. if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
  128. return;
  129. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  130. rtw89_mac_vif_deinit(rtwdev, rtwvif);
  131. rtw89_core_stop(rtwdev);
  132. }
  133. void rtw89_leave_ips(struct rtw89_dev *rtwdev)
  134. {
  135. struct rtw89_vif *rtwvif;
  136. int ret;
  137. if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
  138. return;
  139. ret = rtw89_core_start(rtwdev);
  140. if (ret)
  141. rtw89_err(rtwdev, "failed to leave idle state\n");
  142. rtw89_set_channel(rtwdev);
  143. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  144. rtw89_mac_vif_init(rtwdev, rtwvif);
  145. clear_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
  146. }
  147. void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl)
  148. {
  149. if (btc_ctrl)
  150. rtw89_leave_lps(rtwdev);
  151. }
  152. static void rtw89_tsf32_toggle(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif,
  153. enum rtw89_p2pps_action act)
  154. {
  155. if (act == RTW89_P2P_ACT_UPDATE || act == RTW89_P2P_ACT_REMOVE)
  156. return;
  157. if (act == RTW89_P2P_ACT_INIT)
  158. rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif, true);
  159. else if (act == RTW89_P2P_ACT_TERMINATE)
  160. rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif, false);
  161. }
  162. static void rtw89_p2p_disable_all_noa(struct rtw89_dev *rtwdev,
  163. struct ieee80211_vif *vif)
  164. {
  165. struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
  166. enum rtw89_p2pps_action act;
  167. u8 noa_id;
  168. if (rtwvif->last_noa_nr == 0)
  169. return;
  170. for (noa_id = 0; noa_id < rtwvif->last_noa_nr; noa_id++) {
  171. if (noa_id == rtwvif->last_noa_nr - 1)
  172. act = RTW89_P2P_ACT_TERMINATE;
  173. else
  174. act = RTW89_P2P_ACT_REMOVE;
  175. rtw89_tsf32_toggle(rtwdev, rtwvif, act);
  176. rtw89_fw_h2c_p2p_act(rtwdev, vif, NULL, act, noa_id);
  177. }
  178. }
  179. static void rtw89_p2p_update_noa(struct rtw89_dev *rtwdev,
  180. struct ieee80211_vif *vif)
  181. {
  182. struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
  183. struct ieee80211_p2p_noa_desc *desc;
  184. enum rtw89_p2pps_action act;
  185. u8 noa_id;
  186. for (noa_id = 0; noa_id < RTW89_P2P_MAX_NOA_NUM; noa_id++) {
  187. desc = &vif->bss_conf.p2p_noa_attr.desc[noa_id];
  188. if (!desc->count || !desc->duration)
  189. break;
  190. if (noa_id == 0)
  191. act = RTW89_P2P_ACT_INIT;
  192. else
  193. act = RTW89_P2P_ACT_UPDATE;
  194. rtw89_tsf32_toggle(rtwdev, rtwvif, act);
  195. rtw89_fw_h2c_p2p_act(rtwdev, vif, desc, act, noa_id);
  196. }
  197. rtwvif->last_noa_nr = noa_id;
  198. }
  199. void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif)
  200. {
  201. rtw89_p2p_disable_all_noa(rtwdev, vif);
  202. rtw89_p2p_update_noa(rtwdev, vif);
  203. }
  204. void rtw89_recalc_lps(struct rtw89_dev *rtwdev)
  205. {
  206. struct ieee80211_vif *vif, *found_vif = NULL;
  207. struct rtw89_vif *rtwvif;
  208. enum rtw89_entity_mode mode;
  209. int count = 0;
  210. mode = rtw89_get_entity_mode(rtwdev);
  211. if (mode == RTW89_ENTITY_MODE_MCC)
  212. goto disable_lps;
  213. rtw89_for_each_rtwvif(rtwdev, rtwvif) {
  214. vif = rtwvif_to_vif(rtwvif);
  215. if (vif->type != NL80211_IFTYPE_STATION) {
  216. count = 0;
  217. break;
  218. }
  219. count++;
  220. found_vif = vif;
  221. }
  222. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0))
  223. if (count == 1 && found_vif->cfg.ps) {
  224. rtwdev->lps_enabled = true;
  225. } else {
  226. rtw89_leave_lps(rtwdev);
  227. rtwdev->lps_enabled = false;
  228. return;
  229. }
  230. #endif
  231. disable_lps:
  232. rtw89_leave_lps(rtwdev);
  233. rtwdev->lps_enabled = false;
  234. }
  235. void rtw89_p2p_noa_renew(struct rtw89_vif *rtwvif)
  236. {
  237. struct rtw89_p2p_noa_setter *setter = &rtwvif->p2p_noa;
  238. struct rtw89_p2p_noa_ie *ie = &setter->ie;
  239. struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
  240. struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
  241. if (setter->noa_count) {
  242. setter->noa_index++;
  243. setter->noa_count = 0;
  244. }
  245. memset(ie, 0, sizeof(*ie));
  246. p2p_head->eid = WLAN_EID_VENDOR_SPECIFIC;
  247. p2p_head->ie_len = 4 + sizeof(*noa_head);
  248. p2p_head->oui[0] = (WLAN_OUI_WFA >> 16) & 0xff;
  249. p2p_head->oui[1] = (WLAN_OUI_WFA >> 8) & 0xff;
  250. p2p_head->oui[2] = (WLAN_OUI_WFA >> 0) & 0xff;
  251. p2p_head->oui_type = WLAN_OUI_TYPE_WFA_P2P;
  252. noa_head->attr_type = IEEE80211_P2P_ATTR_ABSENCE_NOTICE;
  253. noa_head->attr_len = cpu_to_le16(2);
  254. noa_head->index = setter->noa_index;
  255. noa_head->oppps_ctwindow = 0;
  256. }
  257. void rtw89_p2p_noa_append(struct rtw89_vif *rtwvif,
  258. const struct ieee80211_p2p_noa_desc *desc)
  259. {
  260. struct rtw89_p2p_noa_setter *setter = &rtwvif->p2p_noa;
  261. struct rtw89_p2p_noa_ie *ie = &setter->ie;
  262. struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
  263. struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
  264. if (!desc->count || !desc->duration)
  265. return;
  266. if (setter->noa_count >= RTW89_P2P_MAX_NOA_NUM)
  267. return;
  268. p2p_head->ie_len += sizeof(*desc);
  269. le16_add_cpu(&noa_head->attr_len, sizeof(*desc));
  270. ie->noa_desc[setter->noa_count++] = *desc;
  271. }
  272. u8 rtw89_p2p_noa_fetch(struct rtw89_vif *rtwvif, void **data)
  273. {
  274. struct rtw89_p2p_noa_setter *setter = &rtwvif->p2p_noa;
  275. struct rtw89_p2p_noa_ie *ie = &setter->ie;
  276. void *tail;
  277. if (!setter->noa_count)
  278. return 0;
  279. *data = ie;
  280. tail = ie->noa_desc + setter->noa_count;
  281. return tail - *data;
  282. }