xref: /linux/drivers/staging/rtl8723bs/core/rtw_pwrctrl.c (revision 37bb2e7217b01404e2abf9d90d8e5705a5603b52)
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <hal_data.h>
9 #include <linux/jiffies.h>
10 
_ips_enter(struct adapter * padapter)11 void _ips_enter(struct adapter *padapter)
12 {
13 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
14 
15 	pwrpriv->bips_processing = true;
16 
17 	/*  syn ips_mode with request */
18 	pwrpriv->ips_mode = pwrpriv->ips_mode_req;
19 
20 	pwrpriv->ips_enter_cnts++;
21 
22 	if (rf_off == pwrpriv->change_rfpwrstate) {
23 		pwrpriv->bpower_saving = true;
24 
25 		if (pwrpriv->ips_mode == IPS_LEVEL_2)
26 			pwrpriv->bkeepfwalive = true;
27 
28 		rtw_ips_pwr_down(padapter);
29 		pwrpriv->rf_pwrstate = rf_off;
30 	}
31 	pwrpriv->bips_processing = false;
32 
33 }
34 
ips_enter(struct adapter * padapter)35 void ips_enter(struct adapter *padapter)
36 {
37 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
38 
39 
40 	hal_btcoex_IpsNotify(padapter, pwrpriv->ips_mode_req);
41 
42 	mutex_lock(&pwrpriv->lock);
43 	_ips_enter(padapter);
44 	mutex_unlock(&pwrpriv->lock);
45 }
46 
_ips_leave(struct adapter * padapter)47 int _ips_leave(struct adapter *padapter)
48 {
49 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
50 	int result = _SUCCESS;
51 
52 	if ((pwrpriv->rf_pwrstate == rf_off) && (!pwrpriv->bips_processing)) {
53 		pwrpriv->bips_processing = true;
54 		pwrpriv->change_rfpwrstate = rf_on;
55 		pwrpriv->ips_leave_cnts++;
56 
57 		result = rtw_ips_pwr_up(padapter);
58 		if (result == _SUCCESS)
59 			pwrpriv->rf_pwrstate = rf_on;
60 		pwrpriv->bips_processing = false;
61 
62 		pwrpriv->bkeepfwalive = false;
63 		pwrpriv->bpower_saving = false;
64 	}
65 
66 	return result;
67 }
68 
ips_leave(struct adapter * padapter)69 int ips_leave(struct adapter *padapter)
70 {
71 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
72 	int ret;
73 
74 	mutex_lock(&pwrpriv->lock);
75 	ret = _ips_leave(padapter);
76 	mutex_unlock(&pwrpriv->lock);
77 
78 	if (ret == _SUCCESS)
79 		hal_btcoex_IpsNotify(padapter, IPS_NONE);
80 
81 	return ret;
82 }
83 
rtw_pwr_unassociated_idle(struct adapter * adapter)84 static bool rtw_pwr_unassociated_idle(struct adapter *adapter)
85 {
86 	struct adapter *buddy = adapter->pbuddy_adapter;
87 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
88 	struct xmit_priv *pxmit_priv = &adapter->xmitpriv;
89 
90 	bool ret = false;
91 
92 	if (adapter_to_pwrctl(adapter)->bpower_saving)
93 		goto exit;
94 
95 	if (time_before(jiffies, adapter_to_pwrctl(adapter)->ips_deny_time))
96 		goto exit;
97 
98 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
99 		|| check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
100 		|| check_fwstate(pmlmepriv, WIFI_AP_STATE)
101 		|| check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
102 	)
103 		goto exit;
104 
105 	/* consider buddy, if exist */
106 	if (buddy) {
107 		struct mlme_priv *b_pmlmepriv = &(buddy->mlmepriv);
108 
109 		if (check_fwstate(b_pmlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)
110 			|| check_fwstate(b_pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
111 			|| check_fwstate(b_pmlmepriv, WIFI_AP_STATE)
112 			|| check_fwstate(b_pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
113 		)
114 			goto exit;
115 	}
116 
117 	if (pxmit_priv->free_xmitbuf_cnt != NR_XMITBUFF ||
118 		pxmit_priv->free_xmit_extbuf_cnt != NR_XMIT_EXTBUFF) {
119 		netdev_dbg(adapter->pnetdev,
120 			   "There are some pkts to transmit\n");
121 		netdev_dbg(adapter->pnetdev,
122 			   "free_xmitbuf_cnt: %d, free_xmit_extbuf_cnt: %d\n",
123 			   pxmit_priv->free_xmitbuf_cnt,
124 			   pxmit_priv->free_xmit_extbuf_cnt);
125 		goto exit;
126 	}
127 
128 	ret = true;
129 
130 exit:
131 	return ret;
132 }
133 
134 
135 /*
136  * ATTENTION:
137  *rtw_ps_processor() doesn't handle LPS.
138  */
rtw_ps_processor(struct adapter * padapter)139 void rtw_ps_processor(struct adapter *padapter)
140 {
141 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
142 	struct dvobj_priv *psdpriv = padapter->dvobj;
143 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
144 	u32 ps_deny = 0;
145 
146 	mutex_lock(&adapter_to_pwrctl(padapter)->lock);
147 	ps_deny = rtw_ps_deny_get(padapter);
148 	mutex_unlock(&adapter_to_pwrctl(padapter)->lock);
149 	if (ps_deny != 0)
150 		goto exit;
151 
152 	if (pwrpriv->bInSuspend) {/* system suspend or autosuspend */
153 		pdbgpriv->dbg_ps_insuspend_cnt++;
154 		return;
155 	}
156 
157 	pwrpriv->ps_processing = true;
158 
159 	if (pwrpriv->ips_mode_req == IPS_NONE)
160 		goto exit;
161 
162 	if (!rtw_pwr_unassociated_idle(padapter))
163 		goto exit;
164 
165 	if ((pwrpriv->rf_pwrstate == rf_on) && ((pwrpriv->pwr_state_check_cnts%4) == 0)) {
166 		pwrpriv->change_rfpwrstate = rf_off;
167 		{
168 			ips_enter(padapter);
169 		}
170 	}
171 exit:
172 	pwrpriv->ps_processing = false;
173 }
174 
pwr_state_check_handler(struct timer_list * t)175 static void pwr_state_check_handler(struct timer_list *t)
176 {
177 	struct pwrctrl_priv *pwrctrlpriv =
178 		timer_container_of(pwrctrlpriv, t, pwr_state_check_timer);
179 	struct adapter *padapter = pwrctrlpriv->adapter;
180 
181 	rtw_ps_cmd(padapter);
182 }
183 
traffic_check_for_leave_lps(struct adapter * padapter,u8 tx,u32 tx_packets)184 void traffic_check_for_leave_lps(struct adapter *padapter, u8 tx, u32 tx_packets)
185 {
186 	static unsigned long start_time;
187 	static u32 xmit_cnt;
188 	u8 bLeaveLPS = false;
189 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
190 
191 
192 
193 	if (tx) { /* from tx */
194 		xmit_cnt += tx_packets;
195 
196 		if (start_time == 0)
197 			start_time = jiffies;
198 
199 		if (jiffies_to_msecs(jiffies - start_time) > 2000) { /*  2 sec == watch dog timer */
200 			if (xmit_cnt > 8) {
201 				if (adapter_to_pwrctl(padapter)->bLeisurePs
202 				    && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
203 				    && !(hal_btcoex_IsBtControlLps(padapter))) {
204 					bLeaveLPS = true;
205 				}
206 			}
207 
208 			start_time = jiffies;
209 			xmit_cnt = 0;
210 		}
211 
212 	} else { /*  from rx path */
213 		if (pmlmepriv->LinkDetectInfo.NumRxUnicastOkInPeriod > 4/*2*/) {
214 			if (adapter_to_pwrctl(padapter)->bLeisurePs
215 			    && (adapter_to_pwrctl(padapter)->pwr_mode != PS_MODE_ACTIVE)
216 			    && !(hal_btcoex_IsBtControlLps(padapter)))
217 				bLeaveLPS = true;
218 		}
219 	}
220 
221 	if (bLeaveLPS)
222 		/* rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, 1); */
223 		rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_LEAVE, tx?0:1);
224 }
225 
226 /*
227  * Description:
228  *This function MUST be called under power lock protect
229  *
230  * Parameters
231  *padapter
232  *pslv			power state level, only could be PS_STATE_S0 ~ PS_STATE_S4
233  *
234  */
rtw_set_rpwm(struct adapter * padapter,u8 pslv)235 void rtw_set_rpwm(struct adapter *padapter, u8 pslv)
236 {
237 	u8 rpwm;
238 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
239 	u8 cpwm_orig;
240 
241 	pslv = PS_STATE(pslv);
242 
243 	if (!pwrpriv->brpwmtimeout) {
244 		if (pwrpriv->rpwm == pslv ||
245 		    (pwrpriv->rpwm >= PS_STATE_S2 && pslv >= PS_STATE_S2))
246 			return;
247 
248 	}
249 
250 	if ((padapter->bSurpriseRemoved) || !(padapter->hw_init_completed)) {
251 		pwrpriv->cpwm = PS_STATE_S4;
252 
253 		return;
254 	}
255 
256 	if (padapter->bDriverStopped) {
257 		if (pslv < PS_STATE_S2)
258 			return;
259 	}
260 
261 	rpwm = pslv | pwrpriv->tog;
262 	/*  only when from PS_STATE S0/S1 to S2 and higher needs ACK */
263 	if ((pwrpriv->cpwm < PS_STATE_S2) && (pslv >= PS_STATE_S2))
264 		rpwm |= PS_ACK;
265 
266 	pwrpriv->rpwm = pslv;
267 
268 	cpwm_orig = 0;
269 	if (rpwm & PS_ACK)
270 		rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_orig);
271 
272 	if (rpwm & PS_ACK)
273 		_set_timer(&pwrpriv->pwr_rpwm_timer, LPS_RPWM_WAIT_MS);
274 	rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&rpwm));
275 
276 	pwrpriv->tog += 0x80;
277 
278 	/*  No LPS 32K, No Ack */
279 	if (rpwm & PS_ACK) {
280 		unsigned long start_time;
281 		u8 cpwm_now;
282 
283 		start_time = jiffies;
284 
285 		/*  polling cpwm */
286 		do {
287 			mdelay(1);
288 			rtw_hal_get_hwreg(padapter, HW_VAR_CPWM, &cpwm_now);
289 			if ((cpwm_orig ^ cpwm_now) & 0x80) {
290 				pwrpriv->cpwm = PS_STATE_S4;
291 				pwrpriv->cpwm_tog = cpwm_now & PS_TOGGLE;
292 				break;
293 			}
294 
295 			if (jiffies_to_msecs(jiffies - start_time) > LPS_RPWM_WAIT_MS) {
296 				_set_timer(&pwrpriv->pwr_rpwm_timer, 1);
297 				break;
298 			}
299 		} while (1);
300 	} else
301 		pwrpriv->cpwm = pslv;
302 }
303 
PS_RDY_CHECK(struct adapter * padapter)304 static u8 PS_RDY_CHECK(struct adapter *padapter)
305 {
306 	unsigned long curr_time, delta_time;
307 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
308 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
309 
310 	if (pwrpriv->bInSuspend)
311 		return false;
312 
313 	curr_time = jiffies;
314 
315 	delta_time = curr_time - pwrpriv->DelayLPSLastTimeStamp;
316 
317 	if (delta_time < LPS_DELAY_TIME)
318 		return false;
319 
320 	if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)
321 		|| check_fwstate(pmlmepriv, WIFI_UNDER_LINKING|WIFI_UNDER_WPS)
322 		|| check_fwstate(pmlmepriv, WIFI_AP_STATE)
323 		|| check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_ADHOC_STATE)
324 		|| rtw_is_scan_deny(padapter)
325 	)
326 		return false;
327 
328 	if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X &&
329 	    !padapter->securitypriv.binstallGrpkey)
330 		return false;
331 
332 	if (!rtw_cfg80211_pwr_mgmt(padapter))
333 		return false;
334 
335 	return true;
336 }
337 
rtw_set_ps_mode(struct adapter * padapter,u8 ps_mode,u8 smart_ps,u8 bcn_ant_mode,const char * msg)338 void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg)
339 {
340 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
341 
342 	if (ps_mode > PM_Card_Disable)
343 		return;
344 
345 	if (pwrpriv->pwr_mode == ps_mode)
346 		if (ps_mode == PS_MODE_ACTIVE)
347 			return;
348 
349 
350 	mutex_lock(&pwrpriv->lock);
351 
352 	/* if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) */
353 	if (ps_mode == PS_MODE_ACTIVE) {
354 		if (!(hal_btcoex_IsBtControlLps(padapter))
355 				|| (hal_btcoex_IsBtControlLps(padapter)
356 					&& !(hal_btcoex_IsLpsOn(padapter)))) {
357 			pwrpriv->pwr_mode = ps_mode;
358 			rtw_set_rpwm(padapter, PS_STATE_S4);
359 
360 			rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
361 			pwrpriv->fw_current_in_ps_mode = false;
362 
363 			hal_btcoex_LpsNotify(padapter, ps_mode);
364 		}
365 	} else {
366 		if ((PS_RDY_CHECK(padapter) && check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) ||
367 		    ((hal_btcoex_IsBtControlLps(padapter)) && (hal_btcoex_IsLpsOn(padapter)))
368 			) {
369 			u8 pslv;
370 
371 			hal_btcoex_LpsNotify(padapter, ps_mode);
372 
373 			pwrpriv->fw_current_in_ps_mode = true;
374 			pwrpriv->pwr_mode = ps_mode;
375 			pwrpriv->smart_ps = smart_ps;
376 			pwrpriv->bcn_ant_mode = bcn_ant_mode;
377 			rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_PWRMODE, (u8 *)(&ps_mode));
378 
379 			pslv = PS_STATE_S2;
380 			if (pwrpriv->alives == 0)
381 				pslv = PS_STATE_S0;
382 
383 			if (!(hal_btcoex_IsBtDisabled(padapter)) &&
384 			    (hal_btcoex_IsBtControlLps(padapter))) {
385 				u8 val8;
386 
387 				val8 = hal_btcoex_LpsVal(padapter);
388 				if (val8 & BIT(4))
389 					pslv = PS_STATE_S2;
390 			}
391 
392 			rtw_set_rpwm(padapter, pslv);
393 		}
394 	}
395 
396 	mutex_unlock(&pwrpriv->lock);
397 }
398 
399 /*
400  * Return:
401  *0:	Leave OK
402  *-1:	Timeout
403  *-2:	Other error
404  */
LPS_RF_ON_check(struct adapter * padapter,u32 delay_ms)405 s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms)
406 {
407 	unsigned long start_time;
408 	u8 bAwake = false;
409 	s32 err = 0;
410 
411 
412 	start_time = jiffies;
413 	while (1) {
414 		rtw_hal_get_hwreg(padapter, HW_VAR_FWLPS_RF_ON, &bAwake);
415 		if (bAwake)
416 			break;
417 
418 		if (padapter->bSurpriseRemoved) {
419 			err = -2;
420 			break;
421 		}
422 
423 		if (jiffies_to_msecs(jiffies - start_time) > delay_ms) {
424 			err = -1;
425 			break;
426 		}
427 		msleep(1);
428 	}
429 
430 	return err;
431 }
432 
433 /* Description: Enter the leisure power save mode. */
LPS_Enter(struct adapter * padapter,const char * msg)434 void LPS_Enter(struct adapter *padapter, const char *msg)
435 {
436 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
437 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
438 	int n_assoc_iface = 0;
439 	char buf[32] = {0};
440 
441 	if (hal_btcoex_IsBtControlLps(padapter))
442 		return;
443 
444 	/* Skip lps enter request if number of associated adapters is not 1 */
445 	if (check_fwstate(&(dvobj->padapters->mlmepriv), WIFI_ASOC_STATE))
446 		n_assoc_iface++;
447 	if (n_assoc_iface != 1)
448 		return;
449 
450 	if (!PS_RDY_CHECK(dvobj->padapters))
451 		return;
452 
453 	if (pwrpriv->bLeisurePs) {
454 		/*  Idle for a while if we connect to AP a while ago. */
455 		if (pwrpriv->LpsIdleCount >= 2) { /*   4 Sec */
456 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) {
457 				scnprintf(buf, sizeof(buf), "WIFI-%s", msg);
458 				pwrpriv->bpower_saving = true;
459 				rtw_set_ps_mode(padapter, pwrpriv->power_mgnt, padapter->registrypriv.smart_ps, 0, buf);
460 			}
461 		} else
462 			pwrpriv->LpsIdleCount++;
463 	}
464 }
465 
466 /* Description: Leave the leisure power save mode. */
LPS_Leave(struct adapter * padapter,const char * msg)467 void LPS_Leave(struct adapter *padapter, const char *msg)
468 {
469 #define LPS_LEAVE_TIMEOUT_MS 100
470 
471 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
472 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
473 	char buf[32] = {0};
474 
475 	if (hal_btcoex_IsBtControlLps(padapter))
476 		return;
477 
478 	if (pwrpriv->bLeisurePs) {
479 		if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) {
480 			scnprintf(buf, sizeof(buf), "WIFI-%s", msg);
481 			rtw_set_ps_mode(padapter, PS_MODE_ACTIVE, 0, 0, buf);
482 
483 			if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
484 				LPS_RF_ON_check(padapter, LPS_LEAVE_TIMEOUT_MS);
485 		}
486 	}
487 
488 	pwrpriv->bpower_saving = false;
489 }
490 
LeaveAllPowerSaveModeDirect(struct adapter * Adapter)491 void LeaveAllPowerSaveModeDirect(struct adapter *Adapter)
492 {
493 	struct adapter *pri_padapter = GET_PRIMARY_ADAPTER(Adapter);
494 	struct mlme_priv *pmlmepriv = &(Adapter->mlmepriv);
495 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(Adapter);
496 
497 	if (Adapter->bSurpriseRemoved)
498 		return;
499 
500 	if (check_fwstate(pmlmepriv, _FW_LINKED)) { /* connect */
501 
502 		if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
503 			return;
504 
505 		mutex_lock(&pwrpriv->lock);
506 
507 		rtw_set_rpwm(Adapter, PS_STATE_S4);
508 
509 		mutex_unlock(&pwrpriv->lock);
510 
511 		rtw_lps_ctrl_wk_cmd(pri_padapter, LPS_CTRL_LEAVE, 0);
512 	} else {
513 		if (pwrpriv->rf_pwrstate == rf_off)
514 			ips_leave(pri_padapter);
515 	}
516 }
517 
518 /*  */
519 /*  Description: Leave all power save mode: LPS, FwLPS, IPS if needed. */
520 /*  Move code to function by tynli. 2010.03.26. */
521 /*  */
LeaveAllPowerSaveMode(struct adapter * Adapter)522 void LeaveAllPowerSaveMode(struct adapter *Adapter)
523 {
524 	struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter);
525 	u8 enqueue = 0;
526 	int n_assoc_iface = 0;
527 
528 	if (!Adapter->bup)
529 		return;
530 
531 	if (Adapter->bSurpriseRemoved)
532 		return;
533 
534 	if (check_fwstate(&(dvobj->padapters->mlmepriv), WIFI_ASOC_STATE))
535 		n_assoc_iface++;
536 
537 	if (n_assoc_iface) { /* connect */
538 		enqueue = 1;
539 
540 		rtw_lps_ctrl_wk_cmd(Adapter, LPS_CTRL_LEAVE, enqueue);
541 
542 		LPS_Leave_check(Adapter);
543 	} else {
544 		if (adapter_to_pwrctl(Adapter)->rf_pwrstate == rf_off)
545 			ips_leave(Adapter);
546 	}
547 }
548 
LPS_Leave_check(struct adapter * padapter)549 void LPS_Leave_check(struct adapter *padapter)
550 {
551 	struct pwrctrl_priv *pwrpriv;
552 	unsigned long	start_time;
553 	u8 bReady;
554 
555 	pwrpriv = adapter_to_pwrctl(padapter);
556 
557 	bReady = false;
558 	start_time = jiffies;
559 
560 	cond_resched();
561 
562 	while (1) {
563 		mutex_lock(&pwrpriv->lock);
564 
565 		if (padapter->bSurpriseRemoved ||
566 		    !(padapter->hw_init_completed) ||
567 		    (pwrpriv->pwr_mode == PS_MODE_ACTIVE))
568 			bReady = true;
569 
570 		mutex_unlock(&pwrpriv->lock);
571 
572 		if (bReady)
573 			break;
574 
575 		if (jiffies_to_msecs(jiffies - start_time) > 100)
576 			break;
577 
578 		msleep(1);
579 	}
580 }
581 
582 /*
583  * Caller:ISR handler...
584  *
585  * This will be called when CPWM interrupt is up.
586  *
587  * using to update cpwn of drv; and drv willl make a decision to up or down pwr level
588  */
cpwm_int_hdl(struct adapter * padapter,struct reportpwrstate_parm * preportpwrstate)589 void cpwm_int_hdl(struct adapter *padapter, struct reportpwrstate_parm *preportpwrstate)
590 {
591 	struct pwrctrl_priv *pwrpriv;
592 
593 	pwrpriv = adapter_to_pwrctl(padapter);
594 
595 	mutex_lock(&pwrpriv->lock);
596 
597 	if (pwrpriv->rpwm < PS_STATE_S2)
598 		goto exit;
599 
600 	pwrpriv->cpwm = PS_STATE(preportpwrstate->state);
601 	pwrpriv->cpwm_tog = preportpwrstate->state & PS_TOGGLE;
602 
603 	if (pwrpriv->cpwm >= PS_STATE_S2) {
604 		if (pwrpriv->alives & CMD_ALIVE)
605 			complete(&padapter->cmdpriv.cmd_queue_comp);
606 
607 		if (pwrpriv->alives & XMIT_ALIVE)
608 			complete(&padapter->xmitpriv.xmit_comp);
609 	}
610 
611 exit:
612 	mutex_unlock(&pwrpriv->lock);
613 
614 }
615 
cpwm_event_callback(struct work_struct * work)616 static void cpwm_event_callback(struct work_struct *work)
617 {
618 	struct pwrctrl_priv *pwrpriv = container_of(work, struct pwrctrl_priv, cpwm_event);
619 	struct dvobj_priv *dvobj = pwrctl_to_dvobj(pwrpriv);
620 	struct adapter *adapter = dvobj->if1;
621 	struct reportpwrstate_parm report;
622 
623 	report.state = PS_STATE_S2;
624 	cpwm_int_hdl(adapter, &report);
625 }
626 
rpwmtimeout_workitem_callback(struct work_struct * work)627 static void rpwmtimeout_workitem_callback(struct work_struct *work)
628 {
629 	struct adapter *padapter;
630 	struct dvobj_priv *dvobj;
631 	struct pwrctrl_priv *pwrpriv;
632 
633 
634 	pwrpriv = container_of(work, struct pwrctrl_priv, rpwmtimeoutwi);
635 	dvobj = pwrctl_to_dvobj(pwrpriv);
636 	padapter = dvobj->if1;
637 
638 	mutex_lock(&pwrpriv->lock);
639 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
640 		goto exit;
641 
642 	mutex_unlock(&pwrpriv->lock);
643 
644 	if (rtw_read8(padapter, 0x100) != 0xEA) {
645 		struct reportpwrstate_parm report;
646 
647 		report.state = PS_STATE_S2;
648 		cpwm_int_hdl(padapter, &report);
649 
650 		return;
651 	}
652 
653 	mutex_lock(&pwrpriv->lock);
654 
655 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
656 		goto exit;
657 
658 	pwrpriv->brpwmtimeout = true;
659 	rtw_set_rpwm(padapter, pwrpriv->rpwm);
660 	pwrpriv->brpwmtimeout = false;
661 
662 exit:
663 	mutex_unlock(&pwrpriv->lock);
664 }
665 
666 /*
667  * This function is a timer handler, can't do any IO in it.
668  */
pwr_rpwm_timeout_handler(struct timer_list * t)669 static void pwr_rpwm_timeout_handler(struct timer_list *t)
670 {
671 	struct pwrctrl_priv *pwrpriv = timer_container_of(pwrpriv, t,
672 							  pwr_rpwm_timer);
673 
674 	if ((pwrpriv->rpwm == pwrpriv->cpwm) || (pwrpriv->cpwm >= PS_STATE_S2))
675 		return;
676 
677 	_set_workitem(&pwrpriv->rpwmtimeoutwi);
678 }
679 
register_task_alive(struct pwrctrl_priv * pwrctrl,u32 tag)680 static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
681 {
682 	pwrctrl->alives |= tag;
683 }
684 
unregister_task_alive(struct pwrctrl_priv * pwrctrl,u32 tag)685 static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, u32 tag)
686 {
687 	pwrctrl->alives &= ~tag;
688 }
689 
690 
691 /*
692  * Description:
693  *Check if the fw_pwrstate is okay for I/O.
694  *If not (cpwm is less than S2), then the sub-routine
695  *will raise the cpwm to be greater than or equal to S2.
696  *
697  *Calling Context: Passive
698  *
699  *Constraint:
700  *	1. this function will request pwrctrl->lock
701  *
702  * Return Value:
703  *_SUCCESS	hardware is ready for I/O
704  *_FAIL		can't I/O right now
705  */
rtw_register_task_alive(struct adapter * padapter,u32 task)706 s32 rtw_register_task_alive(struct adapter *padapter, u32 task)
707 {
708 	s32 res;
709 	struct pwrctrl_priv *pwrctrl;
710 	u8 pslv;
711 
712 	res = _SUCCESS;
713 	pwrctrl = adapter_to_pwrctl(padapter);
714 	pslv = PS_STATE_S2;
715 
716 	mutex_lock(&pwrctrl->lock);
717 
718 	register_task_alive(pwrctrl, task);
719 
720 	if (pwrctrl->fw_current_in_ps_mode) {
721 		if (pwrctrl->cpwm < pslv) {
722 			if (pwrctrl->cpwm < PS_STATE_S2)
723 				res = _FAIL;
724 			if (pwrctrl->rpwm < pslv)
725 				rtw_set_rpwm(padapter, pslv);
726 		}
727 	}
728 
729 	mutex_unlock(&pwrctrl->lock);
730 
731 	if (res == _FAIL)
732 		if (pwrctrl->cpwm >= PS_STATE_S2)
733 			res = _SUCCESS;
734 
735 	return res;
736 }
737 
738 /*
739  * Description:
740  *If task is done, call this func. to power down firmware again.
741  *
742  *Constraint:
743  *	1. this function will request pwrctrl->lock
744  *
745  * Return Value:
746  *none
747  */
rtw_unregister_task_alive(struct adapter * padapter,u32 task)748 void rtw_unregister_task_alive(struct adapter *padapter, u32 task)
749 {
750 	struct pwrctrl_priv *pwrctrl;
751 	u8 pslv;
752 
753 	pwrctrl = adapter_to_pwrctl(padapter);
754 	pslv = PS_STATE_S0;
755 
756 	if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
757 		u8 val8;
758 
759 		val8 = hal_btcoex_LpsVal(padapter);
760 		if (val8 & BIT(4))
761 			pslv = PS_STATE_S2;
762 	}
763 
764 	mutex_lock(&pwrctrl->lock);
765 
766 	unregister_task_alive(pwrctrl, task);
767 
768 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
769 		if (pwrctrl->cpwm > pslv)
770 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
771 				rtw_set_rpwm(padapter, pslv);
772 
773 	}
774 
775 	mutex_unlock(&pwrctrl->lock);
776 }
777 
778 /*
779  * Caller: rtw_xmit_thread
780  *
781  * Check if the fw_pwrstate is okay for xmit.
782  * If not (cpwm is less than S3), then the sub-routine
783  * will raise the cpwm to be greater than or equal to S3.
784  *
785  * Calling Context: Passive
786  *
787  * Return Value:
788  * _SUCCESS	rtw_xmit_thread can write fifo/txcmd afterwards.
789  * _FAIL		rtw_xmit_thread can not do anything.
790  */
rtw_register_tx_alive(struct adapter * padapter)791 s32 rtw_register_tx_alive(struct adapter *padapter)
792 {
793 	s32 res;
794 	struct pwrctrl_priv *pwrctrl;
795 	u8 pslv;
796 
797 	res = _SUCCESS;
798 	pwrctrl = adapter_to_pwrctl(padapter);
799 	pslv = PS_STATE_S2;
800 
801 	mutex_lock(&pwrctrl->lock);
802 
803 	register_task_alive(pwrctrl, XMIT_ALIVE);
804 
805 	if (pwrctrl->fw_current_in_ps_mode) {
806 		if (pwrctrl->cpwm < pslv) {
807 			if (pwrctrl->cpwm < PS_STATE_S2)
808 				res = _FAIL;
809 			if (pwrctrl->rpwm < pslv)
810 				rtw_set_rpwm(padapter, pslv);
811 		}
812 	}
813 
814 	mutex_unlock(&pwrctrl->lock);
815 
816 	if (res == _FAIL)
817 		if (pwrctrl->cpwm >= PS_STATE_S2)
818 			res = _SUCCESS;
819 
820 	return res;
821 }
822 
823 /*
824  * Caller: rtw_cmd_thread
825  *
826  * Check if the fw_pwrstate is okay for issuing cmd.
827  * If not (cpwm should be is less than S2), then the sub-routine
828  * will raise the cpwm to be greater than or equal to S2.
829  *
830  * Calling Context: Passive
831  *
832  * Return Value:
833  *_SUCCESS	rtw_cmd_thread can issue cmds to firmware afterwards.
834  *_FAIL		rtw_cmd_thread can not do anything.
835  */
rtw_register_cmd_alive(struct adapter * padapter)836 s32 rtw_register_cmd_alive(struct adapter *padapter)
837 {
838 	s32 res;
839 	struct pwrctrl_priv *pwrctrl;
840 	u8 pslv;
841 
842 	res = _SUCCESS;
843 	pwrctrl = adapter_to_pwrctl(padapter);
844 	pslv = PS_STATE_S2;
845 
846 	mutex_lock(&pwrctrl->lock);
847 
848 	register_task_alive(pwrctrl, CMD_ALIVE);
849 
850 	if (pwrctrl->fw_current_in_ps_mode) {
851 		if (pwrctrl->cpwm < pslv) {
852 			if (pwrctrl->cpwm < PS_STATE_S2)
853 				res = _FAIL;
854 			if (pwrctrl->rpwm < pslv)
855 				rtw_set_rpwm(padapter, pslv);
856 		}
857 	}
858 
859 	mutex_unlock(&pwrctrl->lock);
860 
861 	if (res == _FAIL)
862 		if (pwrctrl->cpwm >= PS_STATE_S2)
863 			res = _SUCCESS;
864 
865 	return res;
866 }
867 
868 /*
869  * Caller: ISR
870  *
871  * If ISR's txdone,
872  * No more pkts for TX,
873  * Then driver shall call this fun. to power down firmware again.
874  */
rtw_unregister_tx_alive(struct adapter * padapter)875 void rtw_unregister_tx_alive(struct adapter *padapter)
876 {
877 	struct pwrctrl_priv *pwrctrl;
878 	u8 pslv;
879 
880 	pwrctrl = adapter_to_pwrctl(padapter);
881 	pslv = PS_STATE_S0;
882 
883 	if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
884 		u8 val8;
885 
886 		val8 = hal_btcoex_LpsVal(padapter);
887 		if (val8 & BIT(4))
888 			pslv = PS_STATE_S2;
889 	}
890 
891 	mutex_lock(&pwrctrl->lock);
892 
893 	unregister_task_alive(pwrctrl, XMIT_ALIVE);
894 
895 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
896 		if (pwrctrl->cpwm > pslv)
897 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
898 				rtw_set_rpwm(padapter, pslv);
899 	}
900 
901 	mutex_unlock(&pwrctrl->lock);
902 }
903 
904 /*
905  * Caller: ISR
906  *
907  * If all commands have been done,
908  * and no more command to do,
909  * then driver shall call this fun. to power down firmware again.
910  */
rtw_unregister_cmd_alive(struct adapter * padapter)911 void rtw_unregister_cmd_alive(struct adapter *padapter)
912 {
913 	struct pwrctrl_priv *pwrctrl;
914 	u8 pslv;
915 
916 	pwrctrl = adapter_to_pwrctl(padapter);
917 	pslv = PS_STATE_S0;
918 
919 	if (!(hal_btcoex_IsBtDisabled(padapter)) && hal_btcoex_IsBtControlLps(padapter)) {
920 		u8 val8;
921 
922 		val8 = hal_btcoex_LpsVal(padapter);
923 		if (val8 & BIT(4))
924 			pslv = PS_STATE_S2;
925 	}
926 
927 	mutex_lock(&pwrctrl->lock);
928 
929 	unregister_task_alive(pwrctrl, CMD_ALIVE);
930 
931 	if ((pwrctrl->pwr_mode != PS_MODE_ACTIVE) && pwrctrl->fw_current_in_ps_mode) {
932 		if (pwrctrl->cpwm > pslv) {
933 			if ((pslv >= PS_STATE_S2) || (pwrctrl->alives == 0))
934 				rtw_set_rpwm(padapter, pslv);
935 		}
936 	}
937 
938 	mutex_unlock(&pwrctrl->lock);
939 }
940 
rtw_init_pwrctrl_priv(struct adapter * padapter)941 void rtw_init_pwrctrl_priv(struct adapter *padapter)
942 {
943 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
944 
945 	mutex_init(&pwrctrlpriv->lock);
946 	pwrctrlpriv->rf_pwrstate = rf_on;
947 	pwrctrlpriv->ips_enter_cnts = 0;
948 	pwrctrlpriv->ips_leave_cnts = 0;
949 	pwrctrlpriv->bips_processing = false;
950 
951 	pwrctrlpriv->ips_mode = padapter->registrypriv.ips_mode;
952 	pwrctrlpriv->ips_mode_req = padapter->registrypriv.ips_mode;
953 
954 	pwrctrlpriv->pwr_state_check_interval = RTW_PWR_STATE_CHK_INTERVAL;
955 	pwrctrlpriv->pwr_state_check_cnts = 0;
956 	pwrctrlpriv->bInternalAutoSuspend = false;
957 	pwrctrlpriv->bInSuspend = false;
958 	pwrctrlpriv->bkeepfwalive = false;
959 
960 	pwrctrlpriv->LpsIdleCount = 0;
961 	pwrctrlpriv->power_mgnt = padapter->registrypriv.power_mgnt;/*  PS_MODE_MIN; */
962 	pwrctrlpriv->bLeisurePs = pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
963 
964 	pwrctrlpriv->fw_current_in_ps_mode = false;
965 
966 	pwrctrlpriv->rpwm = 0;
967 	pwrctrlpriv->cpwm = PS_STATE_S4;
968 
969 	pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
970 	pwrctrlpriv->smart_ps = padapter->registrypriv.smart_ps;
971 	pwrctrlpriv->bcn_ant_mode = 0;
972 	pwrctrlpriv->dtim = 0;
973 
974 	pwrctrlpriv->tog = 0x80;
975 
976 	rtw_hal_set_hwreg(padapter, HW_VAR_SET_RPWM, (u8 *)(&pwrctrlpriv->rpwm));
977 
978 	_init_workitem(&pwrctrlpriv->cpwm_event, cpwm_event_callback, NULL);
979 
980 	pwrctrlpriv->brpwmtimeout = false;
981 	pwrctrlpriv->adapter = padapter;
982 	_init_workitem(&pwrctrlpriv->rpwmtimeoutwi, rpwmtimeout_workitem_callback, NULL);
983 	timer_setup(&pwrctrlpriv->pwr_rpwm_timer, pwr_rpwm_timeout_handler, 0);
984 	timer_setup(&pwrctrlpriv->pwr_state_check_timer,
985 		    pwr_state_check_handler, 0);
986 
987 	pwrctrlpriv->wowlan_mode = false;
988 	pwrctrlpriv->wowlan_ap_mode = false;
989 }
990 
rtw_free_pwrctrl_priv(struct adapter * adapter)991 void rtw_free_pwrctrl_priv(struct adapter *adapter)
992 {
993 }
994 
rtw_set_ips_deny(struct adapter * padapter,u32 ms)995 inline void rtw_set_ips_deny(struct adapter *padapter, u32 ms)
996 {
997 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
998 	pwrpriv->ips_deny_time = jiffies + msecs_to_jiffies(ms);
999 }
1000 
1001 /*
1002  * rtw_pwr_wakeup - Wake the NIC up from: 1)IPS. 2)USB autosuspend
1003  * @adapter: pointer to struct adapter structure
1004  * @ips_deffer_ms: the ms will prevent from falling into IPS after wakeup
1005  * Return _SUCCESS or _FAIL
1006  */
1007 
_rtw_pwr_wakeup(struct adapter * padapter,u32 ips_deffer_ms,const char * caller)1008 int _rtw_pwr_wakeup(struct adapter *padapter, u32 ips_deffer_ms, const char *caller)
1009 {
1010 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
1011 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj);
1012 	struct mlme_priv *pmlmepriv;
1013 	int ret = _SUCCESS;
1014 	unsigned long start = jiffies;
1015 	unsigned long deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
1016 
1017 	/* for LPS */
1018 	LeaveAllPowerSaveMode(padapter);
1019 
1020 	/* IPS still bound with primary adapter */
1021 	padapter = GET_PRIMARY_ADAPTER(padapter);
1022 	pmlmepriv = &padapter->mlmepriv;
1023 
1024 	if (time_before(pwrpriv->ips_deny_time, deny_time))
1025 		pwrpriv->ips_deny_time = deny_time;
1026 
1027 
1028 	if (pwrpriv->ps_processing)
1029 		while (pwrpriv->ps_processing && jiffies_to_msecs(jiffies - start) <= 3000)
1030 			mdelay(10);
1031 
1032 	if (!(pwrpriv->bInternalAutoSuspend) && pwrpriv->bInSuspend)
1033 		while (pwrpriv->bInSuspend && jiffies_to_msecs(jiffies - start) <= 3000
1034 		)
1035 			mdelay(10);
1036 
1037 	/* System suspend is not allowed to wakeup */
1038 	if (!(pwrpriv->bInternalAutoSuspend) && pwrpriv->bInSuspend) {
1039 		ret = _FAIL;
1040 		goto exit;
1041 	}
1042 
1043 	/* block??? */
1044 	if (pwrpriv->bInternalAutoSuspend  && padapter->net_closed) {
1045 		ret = _FAIL;
1046 		goto exit;
1047 	}
1048 
1049 	/* I think this should be check in IPS, LPS, autosuspend functions... */
1050 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1051 		ret = _SUCCESS;
1052 		goto exit;
1053 	}
1054 
1055 	if (rf_off == pwrpriv->rf_pwrstate) {
1056 		{
1057 			if (ips_leave(padapter) == _FAIL) {
1058 				ret = _FAIL;
1059 				goto exit;
1060 			}
1061 		}
1062 	}
1063 
1064 	/* TODO: the following checking need to be merged... */
1065 	if (padapter->bDriverStopped || !padapter->bup || !padapter->hw_init_completed) {
1066 		ret = false;
1067 		goto exit;
1068 	}
1069 
1070 exit:
1071 	deny_time = jiffies + msecs_to_jiffies(ips_deffer_ms);
1072 	if (time_before(pwrpriv->ips_deny_time, deny_time))
1073 		pwrpriv->ips_deny_time = deny_time;
1074 	return ret;
1075 
1076 }
1077 
rtw_pm_set_lps(struct adapter * padapter,u8 mode)1078 int rtw_pm_set_lps(struct adapter *padapter, u8 mode)
1079 {
1080 	int	ret = 0;
1081 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1082 
1083 	if (mode < PS_MODE_NUM) {
1084 		if (pwrctrlpriv->power_mgnt != mode) {
1085 			if (mode == PS_MODE_ACTIVE)
1086 				LeaveAllPowerSaveMode(padapter);
1087 			else
1088 				pwrctrlpriv->LpsIdleCount = 2;
1089 
1090 			pwrctrlpriv->power_mgnt = mode;
1091 			pwrctrlpriv->bLeisurePs =
1092 				pwrctrlpriv->power_mgnt != PS_MODE_ACTIVE;
1093 		}
1094 	} else
1095 		ret = -EINVAL;
1096 
1097 	return ret;
1098 }
1099 
rtw_pm_set_ips(struct adapter * padapter,u8 mode)1100 int rtw_pm_set_ips(struct adapter *padapter, u8 mode)
1101 {
1102 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
1103 
1104 	if (mode == IPS_NORMAL || mode == IPS_LEVEL_2) {
1105 		rtw_ips_mode_req(pwrctrlpriv, mode);
1106 		return 0;
1107 	} else if (mode == IPS_NONE) {
1108 		rtw_ips_mode_req(pwrctrlpriv, mode);
1109 		if ((padapter->bSurpriseRemoved == 0) && (rtw_pwr_wakeup(padapter) == _FAIL))
1110 			return -EFAULT;
1111 	} else
1112 		return -EINVAL;
1113 
1114 	return 0;
1115 }
1116 
1117 /*
1118  * ATTENTION:
1119  *This function will request pwrctrl LOCK!
1120  */
rtw_ps_deny(struct adapter * padapter,enum ps_deny_reason reason)1121 void rtw_ps_deny(struct adapter *padapter, enum ps_deny_reason reason)
1122 {
1123 	struct pwrctrl_priv *pwrpriv;
1124 
1125 	pwrpriv = adapter_to_pwrctl(padapter);
1126 
1127 	mutex_lock(&pwrpriv->lock);
1128 	pwrpriv->ps_deny |= BIT(reason);
1129 	mutex_unlock(&pwrpriv->lock);
1130 }
1131 
1132 /*
1133  * ATTENTION:
1134  *This function will request pwrctrl LOCK!
1135  */
rtw_ps_deny_cancel(struct adapter * padapter,enum ps_deny_reason reason)1136 void rtw_ps_deny_cancel(struct adapter *padapter, enum ps_deny_reason reason)
1137 {
1138 	struct pwrctrl_priv *pwrpriv;
1139 
1140 	pwrpriv = adapter_to_pwrctl(padapter);
1141 
1142 	mutex_lock(&pwrpriv->lock);
1143 	pwrpriv->ps_deny &= ~BIT(reason);
1144 	mutex_unlock(&pwrpriv->lock);
1145 }
1146 
1147 /*
1148  * ATTENTION:
1149  *Before calling this function pwrctrl lock should be occupied already,
1150  *otherwise it may return incorrect value.
1151  */
rtw_ps_deny_get(struct adapter * padapter)1152 u32 rtw_ps_deny_get(struct adapter *padapter)
1153 {
1154 	return adapter_to_pwrctl(padapter)->ps_deny;
1155 }
1156