ps.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include "ps.h"
  24. #include "io.h"
  25. #include "tx.h"
  26. #include "debug.h"
  27. #define WL1271_WAKEUP_TIMEOUT 500
  28. #define ELP_ENTRY_DELAY 30
  29. #define ELP_ENTRY_DELAY_FORCE_PS 5
  30. void wl1271_elp_work(struct work_struct *work)
  31. {
  32. struct delayed_work *dwork;
  33. struct wl1271 *wl;
  34. struct wl12xx_vif *wlvif;
  35. int ret;
  36. dwork = to_delayed_work(work);
  37. wl = container_of(dwork, struct wl1271, elp_work);
  38. wl1271_debug(DEBUG_PSM, "elp work");
  39. mutex_lock(&wl->mutex);
  40. if (unlikely(wl->state != WLCORE_STATE_ON))
  41. goto out;
  42. /* our work might have been already cancelled */
  43. if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  44. goto out;
  45. if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  46. goto out;
  47. wl12xx_for_each_wlvif(wl, wlvif) {
  48. if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
  49. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  50. goto out;
  51. }
  52. wl1271_debug(DEBUG_PSM, "chip to elp");
  53. ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
  54. if (ret < 0) {
  55. wl12xx_queue_recovery_work(wl);
  56. goto out;
  57. }
  58. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  59. out:
  60. mutex_unlock(&wl->mutex);
  61. }
  62. /* Routines to toggle sleep mode while in ELP */
  63. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  64. {
  65. struct wl12xx_vif *wlvif;
  66. u32 timeout;
  67. /* We do not enter elp sleep in PLT mode */
  68. if (wl->plt)
  69. return;
  70. if (wl->sleep_auth != WL1271_PSM_ELP)
  71. return;
  72. /* we shouldn't get consecutive sleep requests */
  73. if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  74. return;
  75. wl12xx_for_each_wlvif(wl, wlvif) {
  76. if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
  77. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  78. return;
  79. }
  80. timeout = wl->conf.conn.forced_ps ?
  81. ELP_ENTRY_DELAY_FORCE_PS : ELP_ENTRY_DELAY;
  82. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  83. msecs_to_jiffies(timeout));
  84. }
  85. EXPORT_SYMBOL_GPL(wl1271_ps_elp_sleep);
  86. int wl1271_ps_elp_wakeup(struct wl1271 *wl)
  87. {
  88. DECLARE_COMPLETION_ONSTACK(compl);
  89. unsigned long flags;
  90. int ret;
  91. unsigned long start_time = jiffies;
  92. bool pending = false;
  93. /*
  94. * we might try to wake up even if we didn't go to sleep
  95. * before (e.g. on boot)
  96. */
  97. if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
  98. return 0;
  99. /* don't cancel_sync as it might contend for a mutex and deadlock */
  100. cancel_delayed_work(&wl->elp_work);
  101. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  102. return 0;
  103. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  104. /*
  105. * The spinlock is required here to synchronize both the work and
  106. * the completion variable in one entity.
  107. */
  108. spin_lock_irqsave(&wl->wl_lock, flags);
  109. if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
  110. pending = true;
  111. else
  112. wl->elp_compl = &compl;
  113. spin_unlock_irqrestore(&wl->wl_lock, flags);
  114. ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
  115. if (ret < 0) {
  116. wl12xx_queue_recovery_work(wl);
  117. goto err;
  118. }
  119. if (!pending) {
  120. ret = wait_for_completion_timeout(
  121. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  122. if (ret == 0) {
  123. wl1271_error("ELP wakeup timeout!");
  124. wl12xx_queue_recovery_work(wl);
  125. ret = -ETIMEDOUT;
  126. goto err;
  127. }
  128. }
  129. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  130. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  131. jiffies_to_msecs(jiffies - start_time));
  132. goto out;
  133. err:
  134. spin_lock_irqsave(&wl->wl_lock, flags);
  135. wl->elp_compl = NULL;
  136. spin_unlock_irqrestore(&wl->wl_lock, flags);
  137. return ret;
  138. out:
  139. return 0;
  140. }
  141. EXPORT_SYMBOL_GPL(wl1271_ps_elp_wakeup);
  142. int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  143. enum wl1271_cmd_ps_mode mode)
  144. {
  145. int ret;
  146. u16 timeout = wl->conf.conn.dynamic_ps_timeout;
  147. switch (mode) {
  148. case STATION_AUTO_PS_MODE:
  149. case STATION_POWER_SAVE_MODE:
  150. wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
  151. mode, timeout);
  152. ret = wl1271_acx_wake_up_conditions(wl, wlvif,
  153. wl->conf.conn.wake_up_event,
  154. wl->conf.conn.listen_interval);
  155. if (ret < 0) {
  156. wl1271_error("couldn't set wake up conditions");
  157. return ret;
  158. }
  159. ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
  160. if (ret < 0)
  161. return ret;
  162. set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
  163. /*
  164. * enable beacon early termination.
  165. * Not relevant for 5GHz and for high rates.
  166. */
  167. if ((wlvif->band == NL80211_BAND_2GHZ) &&
  168. (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
  169. ret = wl1271_acx_bet_enable(wl, wlvif, true);
  170. if (ret < 0)
  171. return ret;
  172. }
  173. break;
  174. case STATION_ACTIVE_MODE:
  175. wl1271_debug(DEBUG_PSM, "leaving psm");
  176. /* disable beacon early termination */
  177. if ((wlvif->band == NL80211_BAND_2GHZ) &&
  178. (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
  179. ret = wl1271_acx_bet_enable(wl, wlvif, false);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
  184. if (ret < 0)
  185. return ret;
  186. clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
  187. break;
  188. default:
  189. wl1271_warning("trying to set ps to unsupported mode %d", mode);
  190. ret = -EINVAL;
  191. }
  192. return ret;
  193. }
  194. static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
  195. {
  196. int i;
  197. struct sk_buff *skb;
  198. struct ieee80211_tx_info *info;
  199. unsigned long flags;
  200. int filtered[NUM_TX_QUEUES];
  201. struct wl1271_link *lnk = &wl->links[hlid];
  202. /* filter all frames currently in the low level queues for this hlid */
  203. for (i = 0; i < NUM_TX_QUEUES; i++) {
  204. filtered[i] = 0;
  205. while ((skb = skb_dequeue(&lnk->tx_queue[i]))) {
  206. filtered[i]++;
  207. if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
  208. continue;
  209. info = IEEE80211_SKB_CB(skb);
  210. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  211. info->status.rates[0].idx = -1;
  212. ieee80211_tx_status_ni(wl->hw, skb);
  213. }
  214. }
  215. spin_lock_irqsave(&wl->wl_lock, flags);
  216. for (i = 0; i < NUM_TX_QUEUES; i++) {
  217. wl->tx_queue_count[i] -= filtered[i];
  218. if (lnk->wlvif)
  219. lnk->wlvif->tx_queue_count[i] -= filtered[i];
  220. }
  221. spin_unlock_irqrestore(&wl->wl_lock, flags);
  222. wl1271_handle_tx_low_watermark(wl);
  223. }
  224. void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  225. u8 hlid, bool clean_queues)
  226. {
  227. struct ieee80211_sta *sta;
  228. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  229. if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
  230. return;
  231. if (!test_bit(hlid, wlvif->ap.sta_hlid_map) ||
  232. test_bit(hlid, &wl->ap_ps_map))
  233. return;
  234. wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
  235. "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
  236. clean_queues);
  237. rcu_read_lock();
  238. sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
  239. if (!sta) {
  240. wl1271_error("could not find sta %pM for starting ps",
  241. wl->links[hlid].addr);
  242. rcu_read_unlock();
  243. return;
  244. }
  245. ieee80211_sta_ps_transition_ni(sta, true);
  246. rcu_read_unlock();
  247. /* do we want to filter all frames from this link's queues? */
  248. if (clean_queues)
  249. wl1271_ps_filter_frames(wl, hlid);
  250. __set_bit(hlid, &wl->ap_ps_map);
  251. }
  252. void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
  253. {
  254. struct ieee80211_sta *sta;
  255. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  256. if (!test_bit(hlid, &wl->ap_ps_map))
  257. return;
  258. wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
  259. __clear_bit(hlid, &wl->ap_ps_map);
  260. rcu_read_lock();
  261. sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
  262. if (!sta) {
  263. wl1271_error("could not find sta %pM for ending ps",
  264. wl->links[hlid].addr);
  265. goto end;
  266. }
  267. ieee80211_sta_ps_transition_ni(sta, false);
  268. end:
  269. rcu_read_unlock();
  270. }