ps.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. wl12xx_queue_recovery_work(wl);
  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. if (wl->band == IEEE80211_BAND_2GHZ) {
  140. ret = wl1271_acx_bet_enable(wl, false);
  141. if (ret < 0)
  142. return ret;
  143. }
  144. /* disable beacon filtering */
  145. ret = wl1271_acx_beacon_filter_opt(wl, false);
  146. if (ret < 0)
  147. return ret;
  148. ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  149. if (ret < 0)
  150. return ret;
  151. clear_bit(WL1271_FLAG_PSM, &wl->flags);
  152. break;
  153. }
  154. return ret;
  155. }
  156. static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
  157. {
  158. int i;
  159. struct sk_buff *skb;
  160. struct ieee80211_tx_info *info;
  161. unsigned long flags;
  162. int filtered[NUM_TX_QUEUES];
  163. /* filter all frames currently the low level queus for this hlid */
  164. for (i = 0; i < NUM_TX_QUEUES; i++) {
  165. filtered[i] = 0;
  166. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  167. info = IEEE80211_SKB_CB(skb);
  168. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  169. info->status.rates[0].idx = -1;
  170. ieee80211_tx_status_ni(wl->hw, skb);
  171. filtered[i]++;
  172. }
  173. }
  174. spin_lock_irqsave(&wl->wl_lock, flags);
  175. for (i = 0; i < NUM_TX_QUEUES; i++)
  176. wl->tx_queue_count[i] -= filtered[i];
  177. spin_unlock_irqrestore(&wl->wl_lock, flags);
  178. wl1271_handle_tx_low_watermark(wl);
  179. }
  180. void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
  181. {
  182. struct ieee80211_sta *sta;
  183. if (test_bit(hlid, &wl->ap_ps_map))
  184. return;
  185. wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
  186. "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
  187. clean_queues);
  188. rcu_read_lock();
  189. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  190. if (!sta) {
  191. wl1271_error("could not find sta %pM for starting ps",
  192. wl->links[hlid].addr);
  193. rcu_read_unlock();
  194. return;
  195. }
  196. ieee80211_sta_ps_transition_ni(sta, true);
  197. rcu_read_unlock();
  198. /* do we want to filter all frames from this link's queues? */
  199. if (clean_queues)
  200. wl1271_ps_filter_frames(wl, hlid);
  201. __set_bit(hlid, &wl->ap_ps_map);
  202. }
  203. void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
  204. {
  205. struct ieee80211_sta *sta;
  206. if (!test_bit(hlid, &wl->ap_ps_map))
  207. return;
  208. wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
  209. __clear_bit(hlid, &wl->ap_ps_map);
  210. rcu_read_lock();
  211. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  212. if (!sta) {
  213. wl1271_error("could not find sta %pM for ending ps",
  214. wl->links[hlid].addr);
  215. goto end;
  216. }
  217. ieee80211_sta_ps_transition_ni(sta, false);
  218. end:
  219. rcu_read_unlock();
  220. }