1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include "main.h"
6 #include "reg.h"
7 #include "fw.h"
8 #include "ps.h"
9 #include "mac.h"
10 #include "coex.h"
11 #include "debug.h"
12 
13 static int rtw_ips_pwr_up(struct rtw_dev *rtwdev)
14 {
15 	int ret;
16 
17 	ret = rtw_core_start(rtwdev);
18 	if (ret)
19 		rtw_err(rtwdev, "leave idle state failed\n");
20 
21 	rtw_coex_ips_notify(rtwdev, COEX_IPS_LEAVE);
22 	rtw_set_channel(rtwdev);
23 
24 	return ret;
25 }
26 
27 int rtw_enter_ips(struct rtw_dev *rtwdev)
28 {
29 	if (!test_bit(RTW_FLAG_POWERON, rtwdev->flags))
30 		return 0;
31 
32 	rtw_coex_ips_notify(rtwdev, COEX_IPS_ENTER);
33 
34 	rtw_core_stop(rtwdev);
35 	rtw_hci_link_ps(rtwdev, true);
36 
37 	return 0;
38 }
39 
40 static void rtw_restore_port_cfg_iter(void *data, u8 *mac,
41 				      struct ieee80211_vif *vif)
42 {
43 	struct rtw_dev *rtwdev = data;
44 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
45 	u32 config = ~0;
46 
47 	rtw_vif_port_config(rtwdev, rtwvif, config);
48 }
49 
50 int rtw_leave_ips(struct rtw_dev *rtwdev)
51 {
52 	int ret;
53 
54 	if (test_bit(RTW_FLAG_POWERON, rtwdev->flags))
55 		return 0;
56 
57 	rtw_hci_link_ps(rtwdev, false);
58 
59 	ret = rtw_ips_pwr_up(rtwdev);
60 	if (ret) {
61 		rtw_err(rtwdev, "failed to leave ips state\n");
62 		return ret;
63 	}
64 
65 	rtw_iterate_vifs(rtwdev, rtw_restore_port_cfg_iter, rtwdev);
66 
67 	return 0;
68 }
69 
70 void rtw_power_mode_change(struct rtw_dev *rtwdev, bool enter)
71 {
72 	u8 request, confirm, polling;
73 	int ret;
74 
75 	request = rtw_read8(rtwdev, rtwdev->hci.rpwm_addr);
76 	confirm = rtw_read8(rtwdev, rtwdev->hci.cpwm_addr);
77 
78 	/* toggle to request power mode, others remain 0 */
79 	request ^= request | BIT_RPWM_TOGGLE;
80 	if (enter) {
81 		request |= POWER_MODE_LCLK;
82 		if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
83 			request |= POWER_MODE_PG;
84 	}
85 	/* Each request require an ack from firmware */
86 	request |= POWER_MODE_ACK;
87 
88 	if (rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_TX_WAKE))
89 		request |= POWER_TX_WAKE;
90 
91 	rtw_write8(rtwdev, rtwdev->hci.rpwm_addr, request);
92 
93 	/* Check firmware get the power requset and ack via cpwm register */
94 	ret = read_poll_timeout_atomic(rtw_read8, polling,
95 				       (polling ^ confirm) & BIT_RPWM_TOGGLE,
96 				       100, 15000, true, rtwdev,
97 				       rtwdev->hci.cpwm_addr);
98 	if (ret) {
99 		/* Hit here means that driver failed to get an ack from firmware.
100 		 * The reason could be that hardware is locked at Deep sleep,
101 		 * so most of the hardware circuits are not working, even
102 		 * register read/write; or firmware is locked in some state and
103 		 * cannot get the request. It should be treated as fatal error
104 		 * and requires an entire analysis about the firmware/hardware.
105 		 */
106 		WARN(1, "firmware failed to ack driver for %s Deep Power mode\n",
107 		     enter ? "entering" : "leaving");
108 	}
109 }
110 EXPORT_SYMBOL(rtw_power_mode_change);
111 
112 static void __rtw_leave_lps_deep(struct rtw_dev *rtwdev)
113 {
114 	rtw_hci_deep_ps(rtwdev, false);
115 }
116 
117 static int __rtw_fw_leave_lps_check_reg(struct rtw_dev *rtwdev)
118 {
119 	int i;
120 
121 	/* Driver needs to wait for firmware to leave LPS state
122 	 * successfully. Firmware will send null packet to inform AP,
123 	 * and see if AP sends an ACK back, then firmware will restore
124 	 * the REG_TCR register.
125 	 *
126 	 * If driver does not wait for firmware, null packet with
127 	 * PS bit could be sent due to incorrect REG_TCR setting.
128 	 *
129 	 * In our test, 100ms should be enough for firmware to finish
130 	 * the flow. If REG_TCR Register is still incorrect after 100ms,
131 	 * just modify it directly, and throw a warn message.
132 	 */
133 	for (i = 0 ; i < LEAVE_LPS_TRY_CNT; i++) {
134 		if (rtw_read32_mask(rtwdev, REG_TCR, BIT_PWRMGT_HWDATA_EN) == 0)
135 			return 0;
136 		msleep(20);
137 	}
138 
139 	return -EBUSY;
140 }
141 
142 static  int __rtw_fw_leave_lps_check_c2h(struct rtw_dev *rtwdev)
143 {
144 	if (wait_for_completion_timeout(&rtwdev->lps_leave_check,
145 					LEAVE_LPS_TIMEOUT))
146 		return 0;
147 	return -EBUSY;
148 }
149 
150 static void rtw_fw_leave_lps_check(struct rtw_dev *rtwdev)
151 {
152 	bool ret = false;
153 	struct rtw_fw_state *fw;
154 
155 	if (test_bit(RTW_FLAG_WOWLAN, rtwdev->flags))
156 		fw = &rtwdev->wow_fw;
157 	else
158 		fw = &rtwdev->fw;
159 
160 	if (rtw_fw_feature_check(fw, FW_FEATURE_LPS_C2H))
161 		ret = __rtw_fw_leave_lps_check_c2h(rtwdev);
162 	else
163 		ret = __rtw_fw_leave_lps_check_reg(rtwdev);
164 
165 	if (ret) {
166 		rtw_write32_clr(rtwdev, REG_TCR, BIT_PWRMGT_HWDATA_EN);
167 		rtw_warn(rtwdev, "firmware failed to leave lps state\n");
168 	}
169 }
170 
171 static void rtw_fw_leave_lps_check_prepare(struct rtw_dev *rtwdev)
172 {
173 	struct rtw_fw_state *fw;
174 
175 	if (test_bit(RTW_FLAG_WOWLAN, rtwdev->flags))
176 		fw = &rtwdev->wow_fw;
177 	else
178 		fw = &rtwdev->fw;
179 
180 	if (rtw_fw_feature_check(fw, FW_FEATURE_LPS_C2H))
181 		reinit_completion(&rtwdev->lps_leave_check);
182 }
183 
184 static void rtw_leave_lps_core(struct rtw_dev *rtwdev)
185 {
186 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
187 
188 	conf->state = RTW_ALL_ON;
189 	conf->awake_interval = 1;
190 	conf->rlbm = 0;
191 	conf->smart_ps = 0;
192 
193 	rtw_hci_link_ps(rtwdev, false);
194 	rtw_fw_leave_lps_check_prepare(rtwdev);
195 	rtw_fw_set_pwr_mode(rtwdev);
196 	rtw_fw_leave_lps_check(rtwdev);
197 
198 	clear_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags);
199 
200 	rtw_coex_lps_notify(rtwdev, COEX_LPS_DISABLE);
201 }
202 
203 enum rtw_lps_deep_mode rtw_get_lps_deep_mode(struct rtw_dev *rtwdev)
204 {
205 	if (test_bit(RTW_FLAG_WOWLAN, rtwdev->flags))
206 		return rtwdev->lps_conf.wow_deep_mode;
207 	else
208 		return rtwdev->lps_conf.deep_mode;
209 }
210 
211 static void __rtw_enter_lps_deep(struct rtw_dev *rtwdev)
212 {
213 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_NONE)
214 		return;
215 
216 	if (!test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags)) {
217 		rtw_dbg(rtwdev, RTW_DBG_PS,
218 			"Should enter LPS before entering deep PS\n");
219 		return;
220 	}
221 
222 	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
223 		rtw_fw_set_pg_info(rtwdev);
224 
225 	rtw_hci_deep_ps(rtwdev, true);
226 }
227 
228 static void rtw_enter_lps_core(struct rtw_dev *rtwdev)
229 {
230 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
231 
232 	conf->state = RTW_RF_OFF;
233 	conf->awake_interval = 1;
234 	conf->rlbm = 1;
235 	conf->smart_ps = 2;
236 
237 	rtw_coex_lps_notify(rtwdev, COEX_LPS_ENABLE);
238 
239 	rtw_fw_set_pwr_mode(rtwdev);
240 	rtw_hci_link_ps(rtwdev, true);
241 
242 	set_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags);
243 }
244 
245 static void __rtw_enter_lps(struct rtw_dev *rtwdev, u8 port_id)
246 {
247 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
248 
249 	if (test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags))
250 		return;
251 
252 	conf->mode = RTW_MODE_LPS;
253 	conf->port_id = port_id;
254 
255 	rtw_enter_lps_core(rtwdev);
256 }
257 
258 static void __rtw_leave_lps(struct rtw_dev *rtwdev)
259 {
260 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
261 
262 	if (test_and_clear_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags)) {
263 		rtw_dbg(rtwdev, RTW_DBG_PS,
264 			"Should leave deep PS before leaving LPS\n");
265 		__rtw_leave_lps_deep(rtwdev);
266 	}
267 
268 	if (!test_bit(RTW_FLAG_LEISURE_PS, rtwdev->flags))
269 		return;
270 
271 	conf->mode = RTW_MODE_ACTIVE;
272 
273 	rtw_leave_lps_core(rtwdev);
274 }
275 
276 void rtw_enter_lps(struct rtw_dev *rtwdev, u8 port_id)
277 {
278 	lockdep_assert_held(&rtwdev->mutex);
279 
280 	if (rtwdev->coex.stat.wl_force_lps_ctrl)
281 		return;
282 
283 	__rtw_enter_lps(rtwdev, port_id);
284 	__rtw_enter_lps_deep(rtwdev);
285 }
286 
287 void rtw_leave_lps(struct rtw_dev *rtwdev)
288 {
289 	lockdep_assert_held(&rtwdev->mutex);
290 
291 	__rtw_leave_lps_deep(rtwdev);
292 	__rtw_leave_lps(rtwdev);
293 }
294 
295 void rtw_leave_lps_deep(struct rtw_dev *rtwdev)
296 {
297 	lockdep_assert_held(&rtwdev->mutex);
298 
299 	__rtw_leave_lps_deep(rtwdev);
300 }
301 
302 struct rtw_vif_recalc_lps_iter_data {
303 	struct rtw_dev *rtwdev;
304 	struct ieee80211_vif *found_vif;
305 	int count;
306 };
307 
308 static void __rtw_vif_recalc_lps(struct rtw_vif_recalc_lps_iter_data *data,
309 				 struct ieee80211_vif *vif)
310 {
311 	if (data->count < 0)
312 		return;
313 
314 	if (vif->type != NL80211_IFTYPE_STATION) {
315 		data->count = -1;
316 		return;
317 	}
318 
319 	data->count++;
320 	data->found_vif = vif;
321 }
322 
323 static void rtw_vif_recalc_lps_iter(void *data, u8 *mac,
324 				    struct ieee80211_vif *vif)
325 {
326 	__rtw_vif_recalc_lps(data, vif);
327 }
328 
329 void rtw_recalc_lps(struct rtw_dev *rtwdev, struct ieee80211_vif *new_vif)
330 {
331 	struct rtw_vif_recalc_lps_iter_data data = { .rtwdev = rtwdev };
332 
333 	if (new_vif)
334 		__rtw_vif_recalc_lps(&data, new_vif);
335 	rtw_iterate_vifs(rtwdev, rtw_vif_recalc_lps_iter, &data);
336 
337 	if (data.count == 1 && data.found_vif->cfg.ps) {
338 		rtwdev->ps_enabled = true;
339 	} else {
340 		rtwdev->ps_enabled = false;
341 		rtw_leave_lps(rtwdev);
342 	}
343 }
344