ps.c 6.0 KB

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