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