ps.c 25 KB

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