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