ps.c 6.8 KB

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