ps.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 5
  29. void wl1271_elp_work(struct work_struct *work)
  30. {
  31. struct delayed_work *dwork;
  32. struct wl1271 *wl;
  33. struct wl12xx_vif *wlvif;
  34. int ret;
  35. dwork = container_of(work, struct delayed_work, work);
  36. wl = container_of(dwork, struct wl1271, elp_work);
  37. wl1271_debug(DEBUG_PSM, "elp work");
  38. mutex_lock(&wl->mutex);
  39. if (unlikely(wl->state == WL1271_STATE_OFF))
  40. goto out;
  41. /* our work might have been already cancelled */
  42. if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  43. goto out;
  44. if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  45. goto out;
  46. wl12xx_for_each_wlvif(wl, wlvif) {
  47. if (wlvif->bss_type == BSS_TYPE_AP_BSS)
  48. goto out;
  49. if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
  50. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  51. goto out;
  52. }
  53. wl1271_debug(DEBUG_PSM, "chip to elp");
  54. ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
  55. if (ret < 0) {
  56. wl12xx_queue_recovery_work(wl);
  57. goto out;
  58. }
  59. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  60. out:
  61. mutex_unlock(&wl->mutex);
  62. }
  63. /* Routines to toggle sleep mode while in ELP */
  64. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  65. {
  66. struct wl12xx_vif *wlvif;
  67. u32 timeout;
  68. if (wl->sleep_auth != WL1271_PSM_ELP)
  69. return;
  70. /* we shouldn't get consecutive sleep requests */
  71. if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  72. return;
  73. wl12xx_for_each_wlvif(wl, wlvif) {
  74. if (wlvif->bss_type == BSS_TYPE_AP_BSS)
  75. return;
  76. if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
  77. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  78. return;
  79. }
  80. if (wl->conf.conn.forced_ps)
  81. timeout = ELP_ENTRY_DELAY;
  82. else
  83. timeout = wl->conf.conn.dynamic_ps_timeout;
  84. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  85. msecs_to_jiffies(timeout));
  86. }
  87. int wl1271_ps_elp_wakeup(struct wl1271 *wl)
  88. {
  89. DECLARE_COMPLETION_ONSTACK(compl);
  90. unsigned long flags;
  91. int ret;
  92. u32 start_time = jiffies;
  93. bool pending = false;
  94. /*
  95. * we might try to wake up even if we didn't go to sleep
  96. * before (e.g. on boot)
  97. */
  98. if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
  99. return 0;
  100. /* don't cancel_sync as it might contend for a mutex and deadlock */
  101. cancel_delayed_work(&wl->elp_work);
  102. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  103. return 0;
  104. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  105. /*
  106. * The spinlock is required here to synchronize both the work and
  107. * the completion variable in one entity.
  108. */
  109. spin_lock_irqsave(&wl->wl_lock, flags);
  110. if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
  111. pending = true;
  112. else
  113. wl->elp_compl = &compl;
  114. spin_unlock_irqrestore(&wl->wl_lock, flags);
  115. ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
  116. if (ret < 0) {
  117. wl12xx_queue_recovery_work(wl);
  118. goto err;
  119. }
  120. if (!pending) {
  121. ret = wait_for_completion_timeout(
  122. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  123. if (ret == 0) {
  124. wl1271_error("ELP wakeup timeout!");
  125. wl12xx_queue_recovery_work(wl);
  126. ret = -ETIMEDOUT;
  127. goto err;
  128. } else if (ret < 0) {
  129. wl1271_error("ELP wakeup completion error.");
  130. goto err;
  131. }
  132. }
  133. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  134. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  135. jiffies_to_msecs(jiffies - start_time));
  136. goto out;
  137. err:
  138. spin_lock_irqsave(&wl->wl_lock, flags);
  139. wl->elp_compl = NULL;
  140. spin_unlock_irqrestore(&wl->wl_lock, flags);
  141. return ret;
  142. out:
  143. return 0;
  144. }
  145. int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  146. enum wl1271_cmd_ps_mode mode)
  147. {
  148. int ret;
  149. u16 timeout = wl->conf.conn.dynamic_ps_timeout;
  150. switch (mode) {
  151. case STATION_AUTO_PS_MODE:
  152. case STATION_POWER_SAVE_MODE:
  153. wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
  154. mode, timeout);
  155. ret = wl1271_acx_wake_up_conditions(wl, wlvif,
  156. wl->conf.conn.wake_up_event,
  157. wl->conf.conn.listen_interval);
  158. if (ret < 0) {
  159. wl1271_error("couldn't set wake up conditions");
  160. return ret;
  161. }
  162. ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
  163. if (ret < 0)
  164. return ret;
  165. set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
  166. /*
  167. * enable beacon early termination.
  168. * Not relevant for 5GHz and for high rates.
  169. */
  170. if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
  171. (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
  172. ret = wl1271_acx_bet_enable(wl, wlvif, true);
  173. if (ret < 0)
  174. return ret;
  175. }
  176. break;
  177. case STATION_ACTIVE_MODE:
  178. wl1271_debug(DEBUG_PSM, "leaving psm");
  179. /* disable beacon early termination */
  180. if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
  181. (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
  182. ret = wl1271_acx_bet_enable(wl, wlvif, false);
  183. if (ret < 0)
  184. return ret;
  185. }
  186. ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
  187. if (ret < 0)
  188. return ret;
  189. clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
  190. break;
  191. default:
  192. wl1271_warning("trying to set ps to unsupported mode %d", mode);
  193. ret = -EINVAL;
  194. }
  195. return ret;
  196. }
  197. static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
  198. {
  199. int i;
  200. struct sk_buff *skb;
  201. struct ieee80211_tx_info *info;
  202. unsigned long flags;
  203. int filtered[NUM_TX_QUEUES];
  204. /* filter all frames currently in the low level queues for this hlid */
  205. for (i = 0; i < NUM_TX_QUEUES; i++) {
  206. filtered[i] = 0;
  207. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  208. filtered[i]++;
  209. if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
  210. continue;
  211. info = IEEE80211_SKB_CB(skb);
  212. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  213. info->status.rates[0].idx = -1;
  214. ieee80211_tx_status_ni(wl->hw, skb);
  215. }
  216. }
  217. spin_lock_irqsave(&wl->wl_lock, flags);
  218. for (i = 0; i < NUM_TX_QUEUES; i++)
  219. wl->tx_queue_count[i] -= filtered[i];
  220. spin_unlock_irqrestore(&wl->wl_lock, flags);
  221. wl1271_handle_tx_low_watermark(wl);
  222. }
  223. void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  224. u8 hlid, bool clean_queues)
  225. {
  226. struct ieee80211_sta *sta;
  227. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  228. if (test_bit(hlid, &wl->ap_ps_map))
  229. return;
  230. wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
  231. "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
  232. clean_queues);
  233. rcu_read_lock();
  234. sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
  235. if (!sta) {
  236. wl1271_error("could not find sta %pM for starting ps",
  237. wl->links[hlid].addr);
  238. rcu_read_unlock();
  239. return;
  240. }
  241. ieee80211_sta_ps_transition_ni(sta, true);
  242. rcu_read_unlock();
  243. /* do we want to filter all frames from this link's queues? */
  244. if (clean_queues)
  245. wl1271_ps_filter_frames(wl, hlid);
  246. __set_bit(hlid, &wl->ap_ps_map);
  247. }
  248. void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
  249. {
  250. struct ieee80211_sta *sta;
  251. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  252. if (!test_bit(hlid, &wl->ap_ps_map))
  253. return;
  254. wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
  255. __clear_bit(hlid, &wl->ap_ps_map);
  256. rcu_read_lock();
  257. sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
  258. if (!sta) {
  259. wl1271_error("could not find sta %pM for ending ps",
  260. wl->links[hlid].addr);
  261. goto end;
  262. }
  263. ieee80211_sta_ps_transition_ni(sta, false);
  264. end:
  265. rcu_read_unlock();
  266. }