ps.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "reg.h"
  24. #include "ps.h"
  25. #include "io.h"
  26. #include "tx.h"
  27. #define WL1271_WAKEUP_TIMEOUT 500
  28. void wl1271_elp_work(struct work_struct *work)
  29. {
  30. struct delayed_work *dwork;
  31. struct wl1271 *wl;
  32. dwork = container_of(work, struct delayed_work, work);
  33. wl = container_of(dwork, struct wl1271, elp_work);
  34. wl1271_debug(DEBUG_PSM, "elp work");
  35. mutex_lock(&wl->mutex);
  36. if (unlikely(wl->state == WL1271_STATE_OFF))
  37. goto out;
  38. /* our work might have been already cancelled */
  39. if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  40. goto out;
  41. if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
  42. (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
  43. !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
  44. goto out;
  45. wl1271_debug(DEBUG_PSM, "chip to elp");
  46. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  47. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  48. out:
  49. mutex_unlock(&wl->mutex);
  50. }
  51. #define ELP_ENTRY_DELAY 5
  52. /* Routines to toggle sleep mode while in ELP */
  53. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  54. {
  55. /* we shouldn't get consecutive sleep requests */
  56. if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
  57. return;
  58. if (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
  59. !test_bit(WL1271_FLAG_IDLE, &wl->flags))
  60. return;
  61. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  62. msecs_to_jiffies(ELP_ENTRY_DELAY));
  63. }
  64. int wl1271_ps_elp_wakeup(struct wl1271 *wl)
  65. {
  66. DECLARE_COMPLETION_ONSTACK(compl);
  67. unsigned long flags;
  68. int ret;
  69. u32 start_time = jiffies;
  70. bool pending = false;
  71. /*
  72. * we might try to wake up even if we didn't go to sleep
  73. * before (e.g. on boot)
  74. */
  75. if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
  76. return 0;
  77. /* don't cancel_sync as it might contend for a mutex and deadlock */
  78. cancel_delayed_work(&wl->elp_work);
  79. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  80. return 0;
  81. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  82. /*
  83. * The spinlock is required here to synchronize both the work and
  84. * the completion variable in one entity.
  85. */
  86. spin_lock_irqsave(&wl->wl_lock, flags);
  87. if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
  88. pending = true;
  89. else
  90. wl->elp_compl = &compl;
  91. spin_unlock_irqrestore(&wl->wl_lock, flags);
  92. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  93. if (!pending) {
  94. ret = wait_for_completion_timeout(
  95. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  96. if (ret == 0) {
  97. wl1271_error("ELP wakeup timeout!");
  98. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  99. ret = -ETIMEDOUT;
  100. goto err;
  101. } else if (ret < 0) {
  102. wl1271_error("ELP wakeup completion error.");
  103. goto err;
  104. }
  105. }
  106. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  107. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  108. jiffies_to_msecs(jiffies - start_time));
  109. goto out;
  110. err:
  111. spin_lock_irqsave(&wl->wl_lock, flags);
  112. wl->elp_compl = NULL;
  113. spin_unlock_irqrestore(&wl->wl_lock, flags);
  114. return ret;
  115. out:
  116. return 0;
  117. }
  118. int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
  119. u32 rates, bool send)
  120. {
  121. int ret;
  122. switch (mode) {
  123. case STATION_POWER_SAVE_MODE:
  124. wl1271_debug(DEBUG_PSM, "entering psm");
  125. ret = wl1271_acx_wake_up_conditions(wl);
  126. if (ret < 0) {
  127. wl1271_error("couldn't set wake up conditions");
  128. return ret;
  129. }
  130. ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  131. if (ret < 0)
  132. return ret;
  133. set_bit(WL1271_FLAG_PSM, &wl->flags);
  134. break;
  135. case STATION_ACTIVE_MODE:
  136. default:
  137. wl1271_debug(DEBUG_PSM, "leaving psm");
  138. /* disable beacon early termination */
  139. ret = wl1271_acx_bet_enable(wl, false);
  140. if (ret < 0)
  141. return ret;
  142. /* disable beacon filtering */
  143. ret = wl1271_acx_beacon_filter_opt(wl, false);
  144. if (ret < 0)
  145. return ret;
  146. ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  147. if (ret < 0)
  148. return ret;
  149. clear_bit(WL1271_FLAG_PSM, &wl->flags);
  150. break;
  151. }
  152. return ret;
  153. }
  154. static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
  155. {
  156. int i, filtered = 0;
  157. struct sk_buff *skb;
  158. struct ieee80211_tx_info *info;
  159. unsigned long flags;
  160. /* filter all frames currently the low level queus for this hlid */
  161. for (i = 0; i < NUM_TX_QUEUES; i++) {
  162. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  163. info = IEEE80211_SKB_CB(skb);
  164. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  165. info->status.rates[0].idx = -1;
  166. ieee80211_tx_status(wl->hw, skb);
  167. filtered++;
  168. }
  169. }
  170. spin_lock_irqsave(&wl->wl_lock, flags);
  171. wl->tx_queue_count -= filtered;
  172. spin_unlock_irqrestore(&wl->wl_lock, flags);
  173. wl1271_handle_tx_low_watermark(wl);
  174. }
  175. void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
  176. {
  177. struct ieee80211_sta *sta;
  178. if (test_bit(hlid, &wl->ap_ps_map))
  179. return;
  180. wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
  181. "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
  182. clean_queues);
  183. rcu_read_lock();
  184. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  185. if (!sta) {
  186. wl1271_error("could not find sta %pM for starting ps",
  187. wl->links[hlid].addr);
  188. rcu_read_unlock();
  189. return;
  190. }
  191. ieee80211_sta_ps_transition_ni(sta, true);
  192. rcu_read_unlock();
  193. /* do we want to filter all frames from this link's queues? */
  194. if (clean_queues)
  195. wl1271_ps_filter_frames(wl, hlid);
  196. __set_bit(hlid, &wl->ap_ps_map);
  197. }
  198. void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
  199. {
  200. struct ieee80211_sta *sta;
  201. if (!test_bit(hlid, &wl->ap_ps_map))
  202. return;
  203. wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
  204. __clear_bit(hlid, &wl->ap_ps_map);
  205. rcu_read_lock();
  206. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  207. if (!sta) {
  208. wl1271_error("could not find sta %pM for ending ps",
  209. wl->links[hlid].addr);
  210. goto end;
  211. }
  212. ieee80211_sta_ps_transition_ni(sta, false);
  213. end:
  214. rcu_read_unlock();
  215. }