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