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