xref: /linux/drivers/net/wireless/realtek/rtw89/ps.c (revision fcc79e1714e8c2b8e216dc3149812edd37884eef)
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 
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 
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 
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 
65 void __rtw89_enter_ps_mode(struct rtw89_dev *rtwdev, struct rtw89_vif_link *rtwvif_link)
66 {
67 	if (rtwvif_link->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 
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 
88 static void __rtw89_enter_lps(struct rtw89_dev *rtwdev,
89 			      struct rtw89_vif_link *rtwvif_link)
90 {
91 	struct rtw89_lps_parm lps_param = {
92 		.macid = rtwvif_link->mac_id,
93 		.psmode = RTW89_MAC_AX_PS_MODE_LEGACY,
94 		.lastrpwm = RTW89_LAST_RPWM_PS,
95 	};
96 
97 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_FW_CTRL);
98 	rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
99 	rtw89_fw_h2c_lps_ch_info(rtwdev, rtwvif_link);
100 }
101 
102 static void __rtw89_leave_lps(struct rtw89_dev *rtwdev,
103 			      struct rtw89_vif_link *rtwvif_link)
104 {
105 	struct rtw89_lps_parm lps_param = {
106 		.macid = rtwvif_link->mac_id,
107 		.psmode = RTW89_MAC_AX_PS_MODE_ACTIVE,
108 		.lastrpwm = RTW89_LAST_RPWM_ACTIVE,
109 	};
110 
111 	rtw89_fw_h2c_lps_parm(rtwdev, &lps_param);
112 	rtw89_fw_leave_lps_check(rtwdev, 0);
113 	rtw89_btc_ntfy_radio_state(rtwdev, BTC_RFCTRL_WL_ON);
114 	rtw89_chip_digital_pwr_comp(rtwdev, rtwvif_link->phy_idx);
115 }
116 
117 void rtw89_leave_ps_mode(struct rtw89_dev *rtwdev)
118 {
119 	lockdep_assert_held(&rtwdev->mutex);
120 
121 	__rtw89_leave_ps_mode(rtwdev);
122 }
123 
124 void rtw89_enter_lps(struct rtw89_dev *rtwdev, struct rtw89_vif_link *rtwvif_link,
125 		     bool ps_mode)
126 {
127 	lockdep_assert_held(&rtwdev->mutex);
128 
129 	if (test_and_set_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
130 		return;
131 
132 	__rtw89_enter_lps(rtwdev, rtwvif_link);
133 	if (ps_mode)
134 		__rtw89_enter_ps_mode(rtwdev, rtwvif_link);
135 }
136 
137 static void rtw89_leave_lps_vif(struct rtw89_dev *rtwdev,
138 				struct rtw89_vif_link *rtwvif_link)
139 {
140 	if (rtwvif_link->wifi_role != RTW89_WIFI_ROLE_STATION &&
141 	    rtwvif_link->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT)
142 		return;
143 
144 	__rtw89_leave_lps(rtwdev, rtwvif_link);
145 }
146 
147 void rtw89_leave_lps(struct rtw89_dev *rtwdev)
148 {
149 	struct rtw89_vif_link *rtwvif_link;
150 	struct rtw89_vif *rtwvif;
151 	unsigned int link_id;
152 
153 	lockdep_assert_held(&rtwdev->mutex);
154 
155 	if (!test_and_clear_bit(RTW89_FLAG_LEISURE_PS, rtwdev->flags))
156 		return;
157 
158 	__rtw89_leave_ps_mode(rtwdev);
159 
160 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
161 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
162 			rtw89_leave_lps_vif(rtwdev, rtwvif_link);
163 }
164 
165 void rtw89_enter_ips(struct rtw89_dev *rtwdev)
166 {
167 	struct rtw89_vif_link *rtwvif_link;
168 	struct rtw89_vif *rtwvif;
169 	unsigned int link_id;
170 
171 	set_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
172 
173 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
174 		return;
175 
176 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
177 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
178 			rtw89_mac_vif_deinit(rtwdev, rtwvif_link);
179 
180 	rtw89_core_stop(rtwdev);
181 }
182 
183 void rtw89_leave_ips(struct rtw89_dev *rtwdev)
184 {
185 	struct rtw89_vif_link *rtwvif_link;
186 	struct rtw89_vif *rtwvif;
187 	unsigned int link_id;
188 	int ret;
189 
190 	if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
191 		return;
192 
193 	ret = rtw89_core_start(rtwdev);
194 	if (ret)
195 		rtw89_err(rtwdev, "failed to leave idle state\n");
196 
197 	rtw89_set_channel(rtwdev);
198 
199 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
200 		rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id)
201 			rtw89_mac_vif_init(rtwdev, rtwvif_link);
202 
203 	clear_bit(RTW89_FLAG_INACTIVE_PS, rtwdev->flags);
204 }
205 
206 void rtw89_set_coex_ctrl_lps(struct rtw89_dev *rtwdev, bool btc_ctrl)
207 {
208 	if (btc_ctrl)
209 		rtw89_leave_lps(rtwdev);
210 }
211 
212 static void rtw89_tsf32_toggle(struct rtw89_dev *rtwdev,
213 			       struct rtw89_vif_link *rtwvif_link,
214 			       enum rtw89_p2pps_action act)
215 {
216 	if (act == RTW89_P2P_ACT_UPDATE || act == RTW89_P2P_ACT_REMOVE)
217 		return;
218 
219 	if (act == RTW89_P2P_ACT_INIT)
220 		rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, true);
221 	else if (act == RTW89_P2P_ACT_TERMINATE)
222 		rtw89_fw_h2c_tsf32_toggle(rtwdev, rtwvif_link, false);
223 }
224 
225 static void rtw89_p2p_disable_all_noa(struct rtw89_dev *rtwdev,
226 				      struct rtw89_vif_link *rtwvif_link,
227 				      struct ieee80211_bss_conf *bss_conf)
228 {
229 	enum rtw89_p2pps_action act;
230 	u8 noa_id;
231 
232 	if (rtwvif_link->last_noa_nr == 0)
233 		return;
234 
235 	for (noa_id = 0; noa_id < rtwvif_link->last_noa_nr; noa_id++) {
236 		if (noa_id == rtwvif_link->last_noa_nr - 1)
237 			act = RTW89_P2P_ACT_TERMINATE;
238 		else
239 			act = RTW89_P2P_ACT_REMOVE;
240 		rtw89_tsf32_toggle(rtwdev, rtwvif_link, act);
241 		rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, bss_conf,
242 				     NULL, act, noa_id);
243 	}
244 }
245 
246 static void rtw89_p2p_update_noa(struct rtw89_dev *rtwdev,
247 				 struct rtw89_vif_link *rtwvif_link,
248 				 struct ieee80211_bss_conf *bss_conf)
249 {
250 	struct ieee80211_p2p_noa_desc *desc;
251 	enum rtw89_p2pps_action act;
252 	u8 noa_id;
253 
254 	for (noa_id = 0; noa_id < RTW89_P2P_MAX_NOA_NUM; noa_id++) {
255 		desc = &bss_conf->p2p_noa_attr.desc[noa_id];
256 		if (!desc->count || !desc->duration)
257 			break;
258 
259 		if (noa_id == 0)
260 			act = RTW89_P2P_ACT_INIT;
261 		else
262 			act = RTW89_P2P_ACT_UPDATE;
263 		rtw89_tsf32_toggle(rtwdev, rtwvif_link, act);
264 		rtw89_fw_h2c_p2p_act(rtwdev, rtwvif_link, bss_conf,
265 				     desc, act, noa_id);
266 	}
267 	rtwvif_link->last_noa_nr = noa_id;
268 }
269 
270 void rtw89_process_p2p_ps(struct rtw89_dev *rtwdev,
271 			  struct rtw89_vif_link *rtwvif_link,
272 			  struct ieee80211_bss_conf *bss_conf)
273 {
274 	rtw89_p2p_disable_all_noa(rtwdev, rtwvif_link, bss_conf);
275 	rtw89_p2p_update_noa(rtwdev, rtwvif_link, bss_conf);
276 }
277 
278 void rtw89_recalc_lps(struct rtw89_dev *rtwdev)
279 {
280 	struct ieee80211_vif *vif, *found_vif = NULL;
281 	struct rtw89_vif *rtwvif;
282 	enum rtw89_entity_mode mode;
283 	int count = 0;
284 
285 	/* FIXME: Fix rtw89_enter_lps() and __rtw89_enter_ps_mode()
286 	 * to take MLO cases into account before doing the following.
287 	 */
288 	if (rtwdev->support_mlo)
289 		goto disable_lps;
290 
291 	mode = rtw89_get_entity_mode(rtwdev);
292 	if (mode == RTW89_ENTITY_MODE_MCC)
293 		goto disable_lps;
294 
295 	rtw89_for_each_rtwvif(rtwdev, rtwvif) {
296 		vif = rtwvif_to_vif(rtwvif);
297 
298 		if (vif->type != NL80211_IFTYPE_STATION) {
299 			count = 0;
300 			break;
301 		}
302 
303 		count++;
304 		found_vif = vif;
305 	}
306 
307 	if (count == 1 && found_vif->cfg.ps) {
308 		rtwdev->lps_enabled = true;
309 		return;
310 	}
311 
312 disable_lps:
313 	rtw89_leave_lps(rtwdev);
314 	rtwdev->lps_enabled = false;
315 }
316 
317 void rtw89_p2p_noa_renew(struct rtw89_vif_link *rtwvif_link)
318 {
319 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
320 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
321 	struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
322 	struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
323 
324 	if (setter->noa_count) {
325 		setter->noa_index++;
326 		setter->noa_count = 0;
327 	}
328 
329 	memset(ie, 0, sizeof(*ie));
330 
331 	p2p_head->eid = WLAN_EID_VENDOR_SPECIFIC;
332 	p2p_head->ie_len = 4 + sizeof(*noa_head);
333 	p2p_head->oui[0] = (WLAN_OUI_WFA >> 16) & 0xff;
334 	p2p_head->oui[1] = (WLAN_OUI_WFA >> 8) & 0xff;
335 	p2p_head->oui[2] = (WLAN_OUI_WFA >> 0) & 0xff;
336 	p2p_head->oui_type = WLAN_OUI_TYPE_WFA_P2P;
337 
338 	noa_head->attr_type = IEEE80211_P2P_ATTR_ABSENCE_NOTICE;
339 	noa_head->attr_len = cpu_to_le16(2);
340 	noa_head->index = setter->noa_index;
341 	noa_head->oppps_ctwindow = 0;
342 }
343 
344 void rtw89_p2p_noa_append(struct rtw89_vif_link *rtwvif_link,
345 			  const struct ieee80211_p2p_noa_desc *desc)
346 {
347 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
348 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
349 	struct rtw89_p2p_ie_head *p2p_head = &ie->p2p_head;
350 	struct rtw89_noa_attr_head *noa_head = &ie->noa_head;
351 
352 	if (!desc->count || !desc->duration)
353 		return;
354 
355 	if (setter->noa_count >= RTW89_P2P_MAX_NOA_NUM)
356 		return;
357 
358 	p2p_head->ie_len += sizeof(*desc);
359 	le16_add_cpu(&noa_head->attr_len, sizeof(*desc));
360 
361 	ie->noa_desc[setter->noa_count++] = *desc;
362 }
363 
364 u8 rtw89_p2p_noa_fetch(struct rtw89_vif_link *rtwvif_link, void **data)
365 {
366 	struct rtw89_p2p_noa_setter *setter = &rtwvif_link->p2p_noa;
367 	struct rtw89_p2p_noa_ie *ie = &setter->ie;
368 	void *tail;
369 
370 	if (!setter->noa_count)
371 		return 0;
372 
373 	*data = ie;
374 	tail = ie->noa_desc + setter->noa_count;
375 	return tail - *data;
376 }
377