ps.c 6.9 KB

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