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