ps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. /* we can sleep after ps null send ok */
  551. if (rtlpriv->psc.state_inap)
  552. rtl_swlps_rf_sleep(hw);
  553. }
  554. static void rtl_p2p_noa_ie(struct ieee80211_hw *hw, void *data,
  555. unsigned int len)
  556. {
  557. struct rtl_priv *rtlpriv = rtl_priv(hw);
  558. struct ieee80211_mgmt *mgmt = data;
  559. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  560. u8 *pos, *end, *ie;
  561. u16 noa_len;
  562. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  563. u8 noa_num, index , i, noa_index = 0;
  564. bool find_p2p_ie = false , find_p2p_ps_ie = false;
  565. pos = (u8 *)mgmt->u.beacon.variable;
  566. end = data + len;
  567. ie = NULL;
  568. while (pos + 1 < end) {
  569. if (pos + 2 + pos[1] > end)
  570. return;
  571. if (pos[0] == 221 && pos[1] > 4) {
  572. if (memcmp(&pos[2], p2p_oui_ie_type, 4) == 0) {
  573. ie = pos + 2+4;
  574. break;
  575. }
  576. }
  577. pos += 2 + pos[1];
  578. }
  579. if (ie == NULL)
  580. return;
  581. find_p2p_ie = true;
  582. /*to find noa ie*/
  583. while (ie + 1 < end) {
  584. noa_len = le16_to_cpu(*((__le16 *)&ie[1]));
  585. if (ie + 3 + ie[1] > end)
  586. return;
  587. if (ie[0] == 12) {
  588. find_p2p_ps_ie = true;
  589. if ((noa_len - 2) % 13 != 0) {
  590. rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
  591. "P2P notice of absence: invalid length.%d\n",
  592. noa_len);
  593. return;
  594. } else {
  595. noa_num = (noa_len - 2) / 13;
  596. if (noa_num > P2P_MAX_NOA_NUM)
  597. noa_num = P2P_MAX_NOA_NUM;
  598. }
  599. noa_index = ie[3];
  600. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  601. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  602. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD,
  603. "update NOA ie.\n");
  604. p2pinfo->noa_index = noa_index;
  605. p2pinfo->opp_ps = (ie[4] >> 7);
  606. p2pinfo->ctwindow = ie[4] & 0x7F;
  607. p2pinfo->noa_num = noa_num;
  608. index = 5;
  609. for (i = 0; i < noa_num; i++) {
  610. p2pinfo->noa_count_type[i] =
  611. *(u8 *)(ie + index);
  612. index += 1;
  613. p2pinfo->noa_duration[i] =
  614. le32_to_cpu(*(__le32 *)(ie + index));
  615. index += 4;
  616. p2pinfo->noa_interval[i] =
  617. le32_to_cpu(*(__le32 *)(ie + index));
  618. index += 4;
  619. p2pinfo->noa_start_time[i] =
  620. le32_to_cpu(*(__le32 *)(ie + index));
  621. index += 4;
  622. }
  623. if (p2pinfo->opp_ps == 1) {
  624. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  625. /* Driver should wait LPS entering
  626. * CTWindow
  627. */
  628. if (rtlpriv->psc.fw_current_inpsmode)
  629. rtl_p2p_ps_cmd(hw,
  630. P2P_PS_ENABLE);
  631. } else if (p2pinfo->noa_num > 0) {
  632. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  633. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  634. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  635. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  636. }
  637. }
  638. break;
  639. }
  640. ie += 3 + noa_len;
  641. }
  642. if (find_p2p_ie) {
  643. if ((p2pinfo->p2p_ps_mode > P2P_PS_NONE) &&
  644. (!find_p2p_ps_ie))
  645. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  646. }
  647. }
  648. static void rtl_p2p_action_ie(struct ieee80211_hw *hw, void *data,
  649. unsigned int len)
  650. {
  651. struct rtl_priv *rtlpriv = rtl_priv(hw);
  652. struct ieee80211_mgmt *mgmt = data;
  653. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  654. u8 noa_num, index , i , noa_index = 0;
  655. u8 *pos, *end, *ie;
  656. u16 noa_len;
  657. static u8 p2p_oui_ie_type[4] = {0x50, 0x6f, 0x9a, 0x09};
  658. pos = (u8 *)&mgmt->u.action.category;
  659. end = data + len;
  660. ie = NULL;
  661. if (pos[0] == 0x7f) {
  662. if (memcmp(&pos[1], p2p_oui_ie_type, 4) == 0)
  663. ie = pos + 3+4;
  664. }
  665. if (ie == NULL)
  666. return;
  667. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "action frame find P2P IE.\n");
  668. /*to find noa ie*/
  669. while (ie + 1 < end) {
  670. noa_len = le16_to_cpu(*(__le16 *)&ie[1]);
  671. if (ie + 3 + ie[1] > end)
  672. return;
  673. if (ie[0] == 12) {
  674. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "find NOA IE.\n");
  675. RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_LOUD, "noa ie ",
  676. ie, noa_len);
  677. if ((noa_len - 2) % 13 != 0) {
  678. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD,
  679. "P2P notice of absence: invalid length.%d\n",
  680. noa_len);
  681. return;
  682. } else {
  683. noa_num = (noa_len - 2) / 13;
  684. if (noa_num > P2P_MAX_NOA_NUM)
  685. noa_num = P2P_MAX_NOA_NUM;
  686. }
  687. noa_index = ie[3];
  688. if (rtlpriv->psc.p2p_ps_info.p2p_ps_mode ==
  689. P2P_PS_NONE || noa_index != p2pinfo->noa_index) {
  690. p2pinfo->noa_index = noa_index;
  691. p2pinfo->opp_ps = (ie[4] >> 7);
  692. p2pinfo->ctwindow = ie[4] & 0x7F;
  693. p2pinfo->noa_num = noa_num;
  694. index = 5;
  695. for (i = 0; i < noa_num; i++) {
  696. p2pinfo->noa_count_type[i] =
  697. *(u8 *)(ie + index);
  698. index += 1;
  699. p2pinfo->noa_duration[i] =
  700. le32_to_cpu(*(__le32 *)(ie + index));
  701. index += 4;
  702. p2pinfo->noa_interval[i] =
  703. le32_to_cpu(*(__le32 *)(ie + index));
  704. index += 4;
  705. p2pinfo->noa_start_time[i] =
  706. le32_to_cpu(*(__le32 *)(ie + index));
  707. index += 4;
  708. }
  709. if (p2pinfo->opp_ps == 1) {
  710. p2pinfo->p2p_ps_mode = P2P_PS_CTWINDOW;
  711. /* Driver should wait LPS entering
  712. * CTWindow
  713. */
  714. if (rtlpriv->psc.fw_current_inpsmode)
  715. rtl_p2p_ps_cmd(hw,
  716. P2P_PS_ENABLE);
  717. } else if (p2pinfo->noa_num > 0) {
  718. p2pinfo->p2p_ps_mode = P2P_PS_NOA;
  719. rtl_p2p_ps_cmd(hw, P2P_PS_ENABLE);
  720. } else if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  721. rtl_p2p_ps_cmd(hw, P2P_PS_DISABLE);
  722. }
  723. }
  724. break;
  725. }
  726. ie += 3 + noa_len;
  727. }
  728. }
  729. void rtl_p2p_ps_cmd(struct ieee80211_hw *hw , u8 p2p_ps_state)
  730. {
  731. struct rtl_priv *rtlpriv = rtl_priv(hw);
  732. struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
  733. struct rtl_p2p_ps_info *p2pinfo = &(rtlpriv->psc.p2p_ps_info);
  734. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, " p2p state %x\n", p2p_ps_state);
  735. switch (p2p_ps_state) {
  736. case P2P_PS_DISABLE:
  737. p2pinfo->p2p_ps_state = p2p_ps_state;
  738. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  739. &p2p_ps_state);
  740. p2pinfo->noa_index = 0;
  741. p2pinfo->ctwindow = 0;
  742. p2pinfo->opp_ps = 0;
  743. p2pinfo->noa_num = 0;
  744. p2pinfo->p2p_ps_mode = P2P_PS_NONE;
  745. if (rtlps->fw_current_inpsmode) {
  746. if (rtlps->smart_ps == 0) {
  747. rtlps->smart_ps = 2;
  748. rtlpriv->cfg->ops->set_hw_reg(hw,
  749. HW_VAR_H2C_FW_PWRMODE,
  750. &rtlps->pwr_mode);
  751. }
  752. }
  753. break;
  754. case P2P_PS_ENABLE:
  755. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  756. p2pinfo->p2p_ps_state = p2p_ps_state;
  757. if (p2pinfo->ctwindow > 0) {
  758. if (rtlps->smart_ps != 0) {
  759. rtlps->smart_ps = 0;
  760. rtlpriv->cfg->ops->set_hw_reg(hw,
  761. HW_VAR_H2C_FW_PWRMODE,
  762. &rtlps->pwr_mode);
  763. }
  764. }
  765. rtlpriv->cfg->ops->set_hw_reg(hw,
  766. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  767. &p2p_ps_state);
  768. }
  769. break;
  770. case P2P_PS_SCAN:
  771. case P2P_PS_SCAN_DONE:
  772. case P2P_PS_ALLSTASLEEP:
  773. if (p2pinfo->p2p_ps_mode > P2P_PS_NONE) {
  774. p2pinfo->p2p_ps_state = p2p_ps_state;
  775. rtlpriv->cfg->ops->set_hw_reg(hw,
  776. HW_VAR_H2C_FW_P2P_PS_OFFLOAD,
  777. &p2p_ps_state);
  778. }
  779. break;
  780. default:
  781. break;
  782. }
  783. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD,
  784. "ctwindow %x oppps %x\n",
  785. p2pinfo->ctwindow, p2pinfo->opp_ps);
  786. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD,
  787. "count %x duration %x index %x interval %x start time %x noa num %x\n",
  788. p2pinfo->noa_count_type[0],
  789. p2pinfo->noa_duration[0],
  790. p2pinfo->noa_index,
  791. p2pinfo->noa_interval[0],
  792. p2pinfo->noa_start_time[0],
  793. p2pinfo->noa_num);
  794. rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "end\n");
  795. }
  796. void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len)
  797. {
  798. struct rtl_priv *rtlpriv = rtl_priv(hw);
  799. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  800. struct ieee80211_hdr *hdr = data;
  801. if (!mac->p2p)
  802. return;
  803. if (mac->link_state != MAC80211_LINKED)
  804. return;
  805. /* min. beacon length + FCS_LEN */
  806. if (len <= 40 + FCS_LEN)
  807. return;
  808. /* and only beacons from the associated BSSID, please */
  809. if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
  810. return;
  811. /* check if this really is a beacon */
  812. if (!(ieee80211_is_beacon(hdr->frame_control) ||
  813. ieee80211_is_probe_resp(hdr->frame_control) ||
  814. ieee80211_is_action(hdr->frame_control)))
  815. return;
  816. if (ieee80211_is_action(hdr->frame_control))
  817. rtl_p2p_action_ie(hw , data , len - FCS_LEN);
  818. else
  819. rtl_p2p_noa_ie(hw , data , len - FCS_LEN);
  820. }
  821. EXPORT_SYMBOL_GPL(rtl_p2p_info);