ps.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // SPDX-License-Identifier: GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Copyright(c) 2009-2012 Realtek Corporation.
  5. *
  6. * Contact Information:
  7. * wlanfae <wlanfae@realtek.com>
  8. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  9. * Hsinchu 300, Taiwan.
  10. *
  11. * Larry Finger <Larry.Finger@lwfinger.net>
  12. *
  13. *****************************************************************************/
  14. #include "wifi.h"
  15. #include "base.h"
  16. #include "ps.h"
  17. #include <linux/export.h>
  18. #include "btcoexist/rtl_btc.h"
  19. bool rtl_ps_enable_nic(struct ieee80211_hw *hw)
  20. {
  21. struct rtl_priv *rtlpriv = rtl_priv(hw);
  22. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  23. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  24. struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
  25. /*<1> reset trx ring */
  26. if (rtlhal->interface == INTF_PCI)
  27. rtlpriv->intf_ops->reset_trx_ring(hw);
  28. if (is_hal_stop(rtlhal))
  29. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  30. "Driver is already down!\n");
  31. /*<2> Enable Adapter */
  32. if (rtlpriv->cfg->ops->hw_init(hw))
  33. return false;
  34. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RETRY_LIMIT,
  35. &rtlmac->retry_long);
  36. RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
  37. /*<2.1> Switch Channel & Bandwidth to last rtl_op_config setting*/
  38. rtlpriv->cfg->ops->switch_channel(hw);
  39. rtlpriv->cfg->ops->set_channel_access(hw);
  40. rtlpriv->cfg->ops->set_bw_mode(hw,
  41. cfg80211_get_chandef_type(&hw->conf.chandef));
  42. /*<3> Enable Interrupt */
  43. rtlpriv->cfg->ops->enable_interrupt(hw);
  44. /*<enable timer> */
  45. rtl_watch_dog_timer_callback(&rtlpriv->works.watchdog_timer);
  46. return true;
  47. }
  48. bool rtl_ps_disable_nic(struct ieee80211_hw *hw)
  49. {
  50. struct rtl_priv *rtlpriv = rtl_priv(hw);
  51. /*<1> Stop all timer */
  52. rtl_deinit_deferred_work(hw);
  53. /*<2> Disable Interrupt */
  54. rtlpriv->cfg->ops->disable_interrupt(hw);
  55. tasklet_kill(&rtlpriv->works.irq_tasklet);
  56. /*<3> Disable Adapter */
  57. rtlpriv->cfg->ops->hw_disable(hw);
  58. return true;
  59. }
  60. static bool rtl_ps_set_rf_state(struct ieee80211_hw *hw,
  61. enum rf_pwrstate state_toset,
  62. u32 changesource)
  63. {
  64. struct rtl_priv *rtlpriv = rtl_priv(hw);
  65. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  66. enum rf_pwrstate rtstate;
  67. bool actionallowed = false;
  68. u16 rfwait_cnt = 0;
  69. /*Only one thread can change
  70. *the RF state at one time, and others
  71. *should wait to be executed.
  72. */
  73. while (true) {
  74. spin_lock(&rtlpriv->locks.rf_ps_lock);
  75. if (ppsc->rfchange_inprogress) {
  76. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  77. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  78. "RF Change in progress! Wait to set..state_toset(%d).\n",
  79. state_toset);
  80. /* Set RF after the previous action is done. */
  81. while (ppsc->rfchange_inprogress) {
  82. rfwait_cnt++;
  83. mdelay(1);
  84. /*Wait too long, return false to avoid
  85. *to be stuck here.
  86. */
  87. if (rfwait_cnt > 100)
  88. return false;
  89. }
  90. } else {
  91. ppsc->rfchange_inprogress = true;
  92. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  93. break;
  94. }
  95. }
  96. rtstate = ppsc->rfpwr_state;
  97. switch (state_toset) {
  98. case ERFON:
  99. ppsc->rfoff_reason &= (~changesource);
  100. if ((changesource == RF_CHANGE_BY_HW) &&
  101. (ppsc->hwradiooff)) {
  102. ppsc->hwradiooff = false;
  103. }
  104. if (!ppsc->rfoff_reason) {
  105. ppsc->rfoff_reason = 0;
  106. actionallowed = true;
  107. }
  108. break;
  109. case ERFOFF:
  110. if ((changesource == RF_CHANGE_BY_HW) && !ppsc->hwradiooff)
  111. ppsc->hwradiooff = true;
  112. ppsc->rfoff_reason |= changesource;
  113. actionallowed = true;
  114. break;
  115. case ERFSLEEP:
  116. ppsc->rfoff_reason |= changesource;
  117. actionallowed = true;
  118. break;
  119. default:
  120. pr_err("switch case %#x not processed\n", state_toset);
  121. break;
  122. }
  123. if (actionallowed)
  124. rtlpriv->cfg->ops->set_rf_power_state(hw, state_toset);
  125. spin_lock(&rtlpriv->locks.rf_ps_lock);
  126. ppsc->rfchange_inprogress = false;
  127. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  128. return actionallowed;
  129. }
  130. static void _rtl_ps_inactive_ps(struct ieee80211_hw *hw)
  131. {
  132. struct rtl_priv *rtlpriv = rtl_priv(hw);
  133. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  134. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  135. ppsc->swrf_processing = true;
  136. if (ppsc->inactive_pwrstate == ERFON &&
  137. rtlhal->interface == INTF_PCI) {
  138. if ((ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM) &&
  139. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  140. rtlhal->interface == INTF_PCI) {
  141. rtlpriv->intf_ops->disable_aspm(hw);
  142. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  143. }
  144. }
  145. rtl_ps_set_rf_state(hw, ppsc->inactive_pwrstate,
  146. RF_CHANGE_BY_IPS);
  147. if (ppsc->inactive_pwrstate == ERFOFF &&
  148. rtlhal->interface == INTF_PCI) {
  149. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  150. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  151. rtlpriv->intf_ops->enable_aspm(hw);
  152. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  153. }
  154. }
  155. ppsc->swrf_processing = false;
  156. }
  157. void rtl_ips_nic_off_wq_callback(void *data)
  158. {
  159. struct rtl_works *rtlworks =
  160. container_of_dwork_rtl(data, struct rtl_works, ips_nic_off_wq);
  161. struct ieee80211_hw *hw = rtlworks->hw;
  162. struct rtl_priv *rtlpriv = rtl_priv(hw);
  163. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  164. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  165. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  166. enum rf_pwrstate rtstate;
  167. if (mac->opmode != NL80211_IFTYPE_STATION) {
  168. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  169. "not station return\n");
  170. return;
  171. }
  172. if (mac->p2p_in_use)
  173. return;
  174. if (mac->link_state > MAC80211_NOLINK)
  175. return;
  176. if (is_hal_stop(rtlhal))
  177. return;
  178. if (rtlpriv->sec.being_setkey)
  179. return;
  180. if (rtlpriv->cfg->ops->bt_coex_off_before_lps)
  181. rtlpriv->cfg->ops->bt_coex_off_before_lps(hw);
  182. if (ppsc->inactiveps) {
  183. rtstate = ppsc->rfpwr_state;
  184. /*
  185. *Do not enter IPS in the following conditions:
  186. *(1) RF is already OFF or Sleep
  187. *(2) swrf_processing (indicates the IPS is still under going)
  188. *(3) Connectted (only disconnected can trigger IPS)
  189. *(4) IBSS (send Beacon)
  190. *(5) AP mode (send Beacon)
  191. *(6) monitor mode (rcv packet)
  192. */
  193. if (rtstate == ERFON &&
  194. !ppsc->swrf_processing &&
  195. (mac->link_state == MAC80211_NOLINK) &&
  196. !mac->act_scanning) {
  197. RT_TRACE(rtlpriv, COMP_RF, DBG_TRACE,
  198. "IPSEnter(): Turn off RF\n");
  199. ppsc->inactive_pwrstate = ERFOFF;
  200. ppsc->in_powersavemode = true;
  201. /* call before RF off */
  202. if (rtlpriv->cfg->ops->get_btc_status())
  203. rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
  204. ppsc->inactive_pwrstate);
  205. /*rtl_pci_reset_trx_ring(hw); */
  206. _rtl_ps_inactive_ps(hw);
  207. }
  208. }
  209. }
  210. void rtl_ips_nic_off(struct ieee80211_hw *hw)
  211. {
  212. struct rtl_priv *rtlpriv = rtl_priv(hw);
  213. /* because when link with ap, mac80211 will ask us
  214. * to disable nic quickly after scan before linking,
  215. * this will cause link failed, so we delay 100ms here
  216. */
  217. queue_delayed_work(rtlpriv->works.rtl_wq,
  218. &rtlpriv->works.ips_nic_off_wq, MSECS(100));
  219. }
  220. /* NOTICE: any opmode should exc nic_on, or disable without
  221. * nic_on may something wrong, like adhoc TP
  222. */
  223. void rtl_ips_nic_on(struct ieee80211_hw *hw)
  224. {
  225. struct rtl_priv *rtlpriv = rtl_priv(hw);
  226. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  227. enum rf_pwrstate rtstate;
  228. cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
  229. mutex_lock(&rtlpriv->locks.ips_mutex);
  230. if (ppsc->inactiveps) {
  231. rtstate = ppsc->rfpwr_state;
  232. if (rtstate != ERFON &&
  233. !ppsc->swrf_processing &&
  234. ppsc->rfoff_reason <= RF_CHANGE_BY_IPS) {
  235. ppsc->inactive_pwrstate = ERFON;
  236. ppsc->in_powersavemode = false;
  237. _rtl_ps_inactive_ps(hw);
  238. /* call after RF on */
  239. if (rtlpriv->phydm.ops)
  240. rtlpriv->phydm.ops->phydm_reset_dm(rtlpriv);
  241. if (rtlpriv->cfg->ops->get_btc_status())
  242. rtlpriv->btcoexist.btc_ops->btc_ips_notify(rtlpriv,
  243. ppsc->inactive_pwrstate);
  244. }
  245. }
  246. mutex_unlock(&rtlpriv->locks.ips_mutex);
  247. }
  248. /*for FW LPS*/
  249. /*
  250. *Determine if we can set Fw into PS mode
  251. *in current condition.Return TRUE if it
  252. *can enter PS mode.
  253. */
  254. static bool rtl_get_fwlps_doze(struct ieee80211_hw *hw)
  255. {
  256. struct rtl_priv *rtlpriv = rtl_priv(hw);
  257. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  258. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  259. u32 ps_timediff;
  260. ps_timediff = jiffies_to_msecs(jiffies -
  261. ppsc->last_delaylps_stamp_jiffies);
  262. if (ps_timediff < 2000) {
  263. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  264. "Delay enter Fw LPS for DHCP, ARP, or EAPOL exchanging state\n");
  265. return false;
  266. }
  267. if (mac->link_state != MAC80211_LINKED)
  268. return false;
  269. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  270. return false;
  271. return true;
  272. }
  273. /* Change current and default preamble mode.*/
  274. void rtl_lps_set_psmode(struct ieee80211_hw *hw, u8 rt_psmode)
  275. {
  276. struct rtl_priv *rtlpriv = rtl_priv(hw);
  277. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  278. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  279. bool enter_fwlps;
  280. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  281. return;
  282. if (mac->link_state != MAC80211_LINKED)
  283. return;
  284. if (ppsc->dot11_psmode == rt_psmode && rt_psmode == EACTIVE)
  285. return;
  286. /* Update power save mode configured. */
  287. ppsc->dot11_psmode = rt_psmode;
  288. /*
  289. *<FW control LPS>
  290. *1. Enter PS mode
  291. * Set RPWM to Fw to turn RF off and send H2C fw_pwrmode
  292. * cmd to set Fw into PS mode.
  293. *2. Leave PS mode
  294. * Send H2C fw_pwrmode cmd to Fw to set Fw into Active
  295. * mode and set RPWM to turn RF on.
  296. */
  297. if ((ppsc->fwctrl_lps) && ppsc->report_linked) {
  298. if (ppsc->dot11_psmode == EACTIVE) {
  299. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  300. "FW LPS leave ps_mode:%x\n",
  301. FW_PS_ACTIVE_MODE);
  302. enter_fwlps = false;
  303. ppsc->pwr_mode = FW_PS_ACTIVE_MODE;
  304. ppsc->smart_ps = 0;
  305. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_LPS_ACTION,
  306. (u8 *)(&enter_fwlps));
  307. if (ppsc->p2p_ps_info.opp_ps)
  308. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  309. if (rtlpriv->cfg->ops->get_btc_status())
  310. rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
  311. } else {
  312. if (rtl_get_fwlps_doze(hw)) {
  313. RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
  314. "FW LPS enter ps_mode:%x\n",
  315. ppsc->fwctrl_psmode);
  316. if (rtlpriv->cfg->ops->get_btc_status())
  317. rtlpriv->btcoexist.btc_ops->btc_lps_notify(rtlpriv, rt_psmode);
  318. enter_fwlps = true;
  319. ppsc->pwr_mode = ppsc->fwctrl_psmode;
  320. ppsc->smart_ps = 2;
  321. rtlpriv->cfg->ops->set_hw_reg(hw,
  322. HW_VAR_FW_LPS_ACTION,
  323. (u8 *)(&enter_fwlps));
  324. } else {
  325. /* Reset the power save related parameters. */
  326. ppsc->dot11_psmode = EACTIVE;
  327. }
  328. }
  329. }
  330. }
  331. /* Interrupt safe routine to enter the leisure power save mode.*/
  332. static void rtl_lps_enter_core(struct ieee80211_hw *hw)
  333. {
  334. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  335. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  336. struct rtl_priv *rtlpriv = rtl_priv(hw);
  337. if (!ppsc->fwctrl_lps)
  338. return;
  339. if (rtlpriv->sec.being_setkey)
  340. return;
  341. if (rtlpriv->link_info.busytraffic)
  342. return;
  343. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  344. if (mac->cnt_after_linked < 5)
  345. return;
  346. if (mac->opmode == NL80211_IFTYPE_ADHOC)
  347. return;
  348. if (mac->link_state != MAC80211_LINKED)
  349. return;
  350. mutex_lock(&rtlpriv->locks.lps_mutex);
  351. /* Don't need to check (ppsc->dot11_psmode == EACTIVE), because
  352. * bt_ccoexist may ask to enter lps.
  353. * In normal case, this constraint move to rtl_lps_set_psmode().
  354. */
  355. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  356. "Enter 802.11 power save mode...\n");
  357. rtl_lps_set_psmode(hw, EAUTOPS);
  358. mutex_unlock(&rtlpriv->locks.lps_mutex);
  359. }
  360. /* Interrupt safe routine to leave the leisure power save mode.*/
  361. static void rtl_lps_leave_core(struct ieee80211_hw *hw)
  362. {
  363. struct rtl_priv *rtlpriv = rtl_priv(hw);
  364. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  365. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  366. mutex_lock(&rtlpriv->locks.lps_mutex);
  367. if (ppsc->fwctrl_lps) {
  368. if (ppsc->dot11_psmode != EACTIVE) {
  369. /*FIX ME */
  370. /*rtlpriv->cfg->ops->enable_interrupt(hw); */
  371. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  372. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM) &&
  373. rtlhal->interface == INTF_PCI) {
  374. rtlpriv->intf_ops->disable_aspm(hw);
  375. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  376. }
  377. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  378. "Busy Traffic,Leave 802.11 power save..\n");
  379. rtl_lps_set_psmode(hw, EACTIVE);
  380. }
  381. }
  382. mutex_unlock(&rtlpriv->locks.lps_mutex);
  383. }
  384. /* For sw LPS*/
  385. void rtl_swlps_beacon(struct ieee80211_hw *hw, void *data, unsigned int len)
  386. {
  387. struct rtl_priv *rtlpriv = rtl_priv(hw);
  388. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  389. struct ieee80211_hdr *hdr = data;
  390. struct ieee80211_tim_ie *tim_ie;
  391. u8 *tim;
  392. u8 tim_len;
  393. bool u_buffed;
  394. bool m_buffed;
  395. if (mac->opmode != NL80211_IFTYPE_STATION)
  396. return;
  397. if (!rtlpriv->psc.swctrl_lps)
  398. return;
  399. if (rtlpriv->mac80211.link_state != MAC80211_LINKED)
  400. return;
  401. if (!rtlpriv->psc.sw_ps_enabled)
  402. return;
  403. if (rtlpriv->psc.fwctrl_lps)
  404. return;
  405. if (likely(!(hw->conf.flags & IEEE80211_CONF_PS)))
  406. return;
  407. /* check if this really is a beacon */
  408. if (!ieee80211_is_beacon(hdr->frame_control))
  409. return;
  410. /* min. beacon length + FCS_LEN */
  411. if (len <= 40 + FCS_LEN)
  412. return;
  413. /* and only beacons from the associated BSSID, please */
  414. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  415. return;
  416. rtlpriv->psc.last_beacon = jiffies;
  417. tim = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  418. if (!tim)
  419. return;
  420. if (tim[1] < sizeof(*tim_ie))
  421. return;
  422. tim_len = tim[1];
  423. tim_ie = (struct ieee80211_tim_ie *)&tim[2];
  424. if (!WARN_ON_ONCE(!hw->conf.ps_dtim_period))
  425. rtlpriv->psc.dtim_counter = tim_ie->dtim_count;
  426. /* Check whenever the PHY can be turned off again. */
  427. /* 1. What about buffered unicast traffic for our AID? */
  428. u_buffed = ieee80211_check_tim(tim_ie, tim_len,
  429. rtlpriv->mac80211.assoc_id);
  430. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  431. m_buffed = tim_ie->bitmap_ctrl & 0x01;
  432. rtlpriv->psc.multi_buffered = m_buffed;
  433. /* unicast will process by mac80211 through
  434. * set ~IEEE80211_CONF_PS, So we just check
  435. * multicast frames here
  436. */
  437. if (!m_buffed) {
  438. /* back to low-power land. and delay is
  439. * prevent null power save frame tx fail
  440. */
  441. queue_delayed_work(rtlpriv->works.rtl_wq,
  442. &rtlpriv->works.ps_work, MSECS(5));
  443. } else {
  444. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  445. "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
  446. }
  447. }
  448. void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
  449. {
  450. struct rtl_priv *rtlpriv = rtl_priv(hw);
  451. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  452. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  453. if (!rtlpriv->psc.swctrl_lps)
  454. return;
  455. if (mac->link_state != MAC80211_LINKED)
  456. return;
  457. if (ppsc->reg_rfps_level & RT_RF_LPS_LEVEL_ASPM &&
  458. RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  459. rtlpriv->intf_ops->disable_aspm(hw);
  460. RT_CLEAR_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  461. }
  462. mutex_lock(&rtlpriv->locks.lps_mutex);
  463. rtl_ps_set_rf_state(hw, ERFON, RF_CHANGE_BY_PS);
  464. mutex_unlock(&rtlpriv->locks.lps_mutex);
  465. }
  466. void rtl_swlps_rfon_wq_callback(void *data)
  467. {
  468. struct rtl_works *rtlworks =
  469. container_of_dwork_rtl(data, struct rtl_works, ps_rfon_wq);
  470. struct ieee80211_hw *hw = rtlworks->hw;
  471. rtl_swlps_rf_awake(hw);
  472. }
  473. void rtl_swlps_rf_sleep(struct ieee80211_hw *hw)
  474. {
  475. struct rtl_priv *rtlpriv = rtl_priv(hw);
  476. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  477. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  478. u8 sleep_intv;
  479. if (!rtlpriv->psc.sw_ps_enabled)
  480. return;
  481. if ((rtlpriv->sec.being_setkey) ||
  482. (mac->opmode == NL80211_IFTYPE_ADHOC))
  483. return;
  484. /*sleep after linked 10s, to let DHCP and 4-way handshake ok enough!! */
  485. if ((mac->link_state != MAC80211_LINKED) || (mac->cnt_after_linked < 5))
  486. return;
  487. if (rtlpriv->link_info.busytraffic)
  488. return;
  489. spin_lock(&rtlpriv->locks.rf_ps_lock);
  490. if (rtlpriv->psc.rfchange_inprogress) {
  491. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  492. return;
  493. }
  494. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  495. mutex_lock(&rtlpriv->locks.lps_mutex);
  496. rtl_ps_set_rf_state(hw, ERFSLEEP, RF_CHANGE_BY_PS);
  497. mutex_unlock(&rtlpriv->locks.lps_mutex);
  498. if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_ASPM &&
  499. !RT_IN_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM)) {
  500. rtlpriv->intf_ops->enable_aspm(hw);
  501. RT_SET_PS_LEVEL(ppsc, RT_PS_LEVEL_ASPM);
  502. }
  503. /* here is power save alg, when this beacon is DTIM
  504. * we will set sleep time to dtim_period * n;
  505. * when this beacon is not DTIM, we will set sleep
  506. * time to sleep_intv = rtlpriv->psc.dtim_counter or
  507. * MAX_SW_LPS_SLEEP_INTV(default set to 5)
  508. */
  509. if (rtlpriv->psc.dtim_counter == 0) {
  510. if (hw->conf.ps_dtim_period == 1)
  511. sleep_intv = hw->conf.ps_dtim_period * 2;
  512. else
  513. sleep_intv = hw->conf.ps_dtim_period;
  514. } else {
  515. sleep_intv = rtlpriv->psc.dtim_counter;
  516. }
  517. if (sleep_intv > MAX_SW_LPS_SLEEP_INTV)
  518. sleep_intv = MAX_SW_LPS_SLEEP_INTV;
  519. /* this print should always be dtim_conter = 0 &
  520. * sleep = dtim_period, that meaons, we should
  521. * awake before every dtim
  522. */
  523. RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
  524. "dtim_counter:%x will sleep :%d beacon_intv\n",
  525. rtlpriv->psc.dtim_counter, sleep_intv);
  526. /* we tested that 40ms is enough for sw & hw sw delay */
  527. queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.ps_rfon_wq,
  528. MSECS(sleep_intv *
  529. mac->vif->bss_conf.beacon_int - 40));
  530. }
  531. void rtl_lps_change_work_callback(struct work_struct *work)
  532. {
  533. struct rtl_works *rtlworks =
  534. container_of(work, struct rtl_works, lps_change_work);
  535. struct ieee80211_hw *hw = rtlworks->hw;
  536. struct rtl_priv *rtlpriv = rtl_priv(hw);
  537. if (rtlpriv->enter_ps)
  538. rtl_lps_enter_core(hw);
  539. else
  540. rtl_lps_leave_core(hw);
  541. }
  542. void rtl_lps_enter(struct ieee80211_hw *hw)
  543. {
  544. struct rtl_priv *rtlpriv = rtl_priv(hw);
  545. if (!in_interrupt())
  546. return rtl_lps_enter_core(hw);
  547. rtlpriv->enter_ps = true;
  548. schedule_work(&rtlpriv->works.lps_change_work);
  549. }
  550. void rtl_lps_leave(struct ieee80211_hw *hw)
  551. {
  552. struct rtl_priv *rtlpriv = rtl_priv(hw);
  553. if (!in_interrupt())
  554. return rtl_lps_leave_core(hw);
  555. rtlpriv->enter_ps = false;
  556. schedule_work(&rtlpriv->works.lps_change_work);
  557. }
  558. void rtl_swlps_wq_callback(void *data)
  559. {
  560. struct rtl_works *rtlworks = container_of_dwork_rtl(data,
  561. struct rtl_works,
  562. ps_work);
  563. struct ieee80211_hw *hw = rtlworks->hw;
  564. struct rtl_priv *rtlpriv = rtl_priv(hw);
  565. bool ps = false;
  566. ps = (hw->conf.flags & IEEE80211_CONF_PS);
  567. /* we can sleep after ps null send ok */
  568. if (rtlpriv->psc.state_inap) {
  569. rtl_swlps_rf_sleep(hw);
  570. if (rtlpriv->psc.state && !ps) {
  571. rtlpriv->psc.sleep_ms = jiffies_to_msecs(jiffies -
  572. rtlpriv->psc.last_action);
  573. }
  574. if (ps)
  575. rtlpriv->psc.last_slept = jiffies;
  576. rtlpriv->psc.last_action = jiffies;
  577. rtlpriv->psc.state = ps;
  578. }
  579. }
  580. static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
  581. unsigned int len)
  582. {
  583. struct rtl_priv *rtlpriv = rtl_priv(hw);
  584. struct ieee80211_mgmt *mgmt = data;
  585. struct rtl_p2p_ps_info *p2pinfo = &rtlpriv->psc.p2p_ps_info;
  586. u8 *pos, *end, *ie;
  587. u16 noa_len;
  588. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  589. u8 noa_num, index, i, noa_index = 0;
  590. bool find_p2p_ie = false, find_p2p_ps_ie = false;
  591. pos = (u8 *)mgmt->u.beacon.variable;
  592. end = data + len;
  593. ie = NULL;
  594. while (pos + 1 < end) {
  595. if (pos + 2 + pos[1] > end)
  596. return;
  597. if (pos[0] == 221 && pos[1] > 4) {
  598. if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
  599. ie = pos + 2 + 4;
  600. break;
  601. }
  602. }
  603. pos += 2 + pos[1];
  604. }
  605. if (!ie)
  606. return;
  607. find_p2p_ie = true;
  608. /*to find noa ie*/
  609. while (ie + 1 < end) {
  610. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  611. if (ie + 3 + ie[1] > end)
  612. return;
  613. if (ie[0] == 12) {
  614. find_p2p_ps_ie = true;
  615. if ((noa_len - 2) % 13 != 0) {
  616. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  617. "P2P notice of absence: invalid length.%d\n",
  618. noa_len);
  619. return;
  620. }
  621. noa_num = (noa_len - 2) / 13;
  622. noa_index = ie[3];
  623. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  624. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  625. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  626. "update NOA ie.\n");
  627. p2pinfo->noa_index = noa_index;
  628. p2pinfo->opp_ps = (ie[4] >> 7);
  629. p2pinfo->ctwindow = ie[4] & 0x7F;
  630. p2pinfo->noa_num = noa_num;
  631. index = 5;
  632. for (i = 0; i < noa_num; i++) {
  633. p2pinfo->noa_count_type[i] =
  634. READEF1BYTE(ie + index);
  635. index += 1;
  636. p2pinfo->noa_duration[i] =
  637. READEF4BYTE((__le32 *)ie + index);
  638. index += 4;
  639. p2pinfo->noa_interval[i] =
  640. READEF4BYTE((__le32 *)ie + index);
  641. index += 4;
  642. p2pinfo->noa_start_time[i] =
  643. READEF4BYTE((__le32 *)ie + index);
  644. index += 4;
  645. }
  646. if (p2pinfo->opp_ps == 1) {
  647. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  648. /* Driver should wait LPS entering
  649. * CTWindow
  650. */
  651. if (rtlpriv->psc.fw_current_inpsmode)
  652. rtl_p2p_ps_cmd(hw,
  653. P2P_PS_ENABLE);
  654. } else if (p2pinfo->noa_num > 0) {
  655. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  656. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  657. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  658. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  659. }
  660. }
  661. break;
  662. }
  663. ie += 3 + noa_len;
  664. }
  665. if (find_p2p_ie) {
  666. if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
  667. (!find_p2p_ps_ie))
  668. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  669. }
  670. }
  671. static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
  672. unsigned int len)
  673. {
  674. struct rtl_priv *rtlpriv = rtl_priv(hw);
  675. struct ieee80211_mgmt *mgmt = data;
  676. struct rtl_p2p_ps_info *p2pinfo = &rtlpriv->psc.p2p_ps_info;
  677. u8 noa_num, index, i, noa_index = 0;
  678. u8 *pos, *end, *ie;
  679. u16 noa_len;
  680. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  681. pos = (u8 *)&mgmt->u.action.category;
  682. end = data + len;
  683. ie = NULL;
  684. if (pos[0] == 0x7f) {
  685. if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
  686. ie = pos + 3 + 4;
  687. }
  688. if (!ie)
  689. return;
  690. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
  691. /*to find noa ie*/
  692. while (ie + 1 < end) {
  693. noa_len = READEF2BYTE((__le16 *)&ie[1]);
  694. if (ie + 3 + ie[1] > end)
  695. return;
  696. if (ie[0] == 12) {
  697. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
  698. RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
  699. ie, noa_len);
  700. if ((noa_len - 2) % 13 != 0) {
  701. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  702. "P2P notice of absence: invalid length.%d\n",
  703. noa_len);
  704. return;
  705. }
  706. noa_num = (noa_len - 2) / 13;
  707. noa_index = ie[3];
  708. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  709. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  710. p2pinfo->noa_index = noa_index;
  711. p2pinfo->opp_ps = (ie[4] >> 7);
  712. p2pinfo->ctwindow = ie[4] & 0x7F;
  713. p2pinfo->noa_num = noa_num;
  714. index = 5;
  715. for (i = 0; i < noa_num; i++) {
  716. p2pinfo->noa_count_type[i] =
  717. READEF1BYTE(ie + index);
  718. index += 1;
  719. p2pinfo->noa_duration[i] =
  720. READEF4BYTE((__le32 *)ie + index);
  721. index += 4;
  722. p2pinfo->noa_interval[i] =
  723. READEF4BYTE((__le32 *)ie + index);
  724. index += 4;
  725. p2pinfo->noa_start_time[i] =
  726. READEF4BYTE((__le32 *)ie + index);
  727. index += 4;
  728. }
  729. if (p2pinfo->opp_ps == 1) {
  730. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  731. /* Driver should wait LPS entering
  732. * CTWindow
  733. */
  734. if (rtlpriv->psc.fw_current_inpsmode)
  735. rtl_p2p_ps_cmd(hw,
  736. P2P_PS_ENABLE);
  737. } else if (p2pinfo->noa_num > 0) {
  738. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  739. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  740. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  741. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  742. }
  743. }
  744. break;
  745. }
  746. ie += 3 + noa_len;
  747. }
  748. }
  749. void rtl_p2p_ps_cmd(struct ieee80211_hw *hw, u8 p2p_ps_state)
  750. {
  751. struct rtl_priv *rtlpriv = rtl_priv(hw);
  752. struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
  753. struct rtl_p2p_ps_info *p2pinfo = &rtlpriv->psc.p2p_ps_info;
  754. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "p2p state %x\n", p2p_ps_state);
  755. switch (p2p_ps_state) {
  756. case P2P_PS_DISABLE:
  757. p2pinfo->p2p_ps_state = p2p_ps_state;
  758. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  759. &p2p_ps_state);
  760. p2pinfo->noa_index = 0;
  761. p2pinfo->ctwindow = 0;
  762. p2pinfo->opp_ps = 0;
  763. p2pinfo->noa_num = 0;
  764. p2pinfo->p2p_ps_mode = P2P_PS_NONE;
  765. if (rtlps->fw_current_inpsmode) {
  766. if (rtlps->smart_ps == 0) {
  767. rtlps->smart_ps = 2;
  768. rtlpriv->cfg->ops->set_hw_reg(hw,
  769. HW_VAR_H2C_FW_PWRMODE,
  770. &rtlps->pwr_mode);
  771. }
  772. }
  773. break;
  774. case P2P_PS_ENABLE:
  775. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  776. p2pinfo->p2p_ps_state = p2p_ps_state;
  777. if (p2pinfo->ctwindow > 0) {
  778. if (rtlps->smart_ps != 0) {
  779. rtlps->smart_ps = 0;
  780. rtlpriv->cfg->ops->set_hw_reg(hw,
  781. HW_VAR_H2C_FW_PWRMODE,
  782. &rtlps->pwr_mode);
  783. }
  784. }
  785. rtlpriv->cfg->ops->set_hw_reg(hw,
  786. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  787. &p2p_ps_state);
  788. }
  789. break;
  790. case P2P_PS_SCAN:
  791. case P2P_PS_SCAN_DONE:
  792. case P2P_PS_ALLSTASLEEP:
  793. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  794. p2pinfo->p2p_ps_state = p2p_ps_state;
  795. rtlpriv->cfg->ops->set_hw_reg(hw,
  796. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  797. &p2p_ps_state);
  798. }
  799. break;
  800. default:
  801. break;
  802. }
  803. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  804. "ctwindow %x oppps %x\n",
  805. p2pinfo->ctwindow, p2pinfo->opp_ps);
  806. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD,
  807. "count %x duration %x index %x interval %x start time %x noa num %x\n",
  808. p2pinfo->noa_count_type[0],
  809. p2pinfo->noa_duration[0],
  810. p2pinfo->noa_index,
  811. p2pinfo->noa_interval[0],
  812. p2pinfo->noa_start_time[0],
  813. p2pinfo->noa_num);
  814. RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
  815. }
  816. void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
  817. {
  818. struct rtl_priv *rtlpriv = rtl_priv(hw);
  819. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  820. struct ieee80211_hdr *hdr = data;
  821. if (!mac->p2p)
  822. return;
  823. if (mac->link_state != MAC80211_LINKED)
  824. return;
  825. /* min. beacon length + FCS_LEN */
  826. if (len <= 40 + FCS_LEN)
  827. return;
  828. /* and only beacons from the associated BSSID, please */
  829. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  830. return;
  831. /* check if this really is a beacon */
  832. if (!(ieee80211_is_beacon(hdr->frame_control) ||
  833. ieee80211_is_probe_resp(hdr->frame_control) ||
  834. ieee80211_is_action(hdr->frame_control)))
  835. return;
  836. if (ieee80211_is_action(hdr->frame_control))
  837. rtl_p2p_action_ie(hw, data, len - FCS_LEN);
  838. else
  839. rtl_p2p_noa_ie(hw, data, len - FCS_LEN);
  840. }