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