ps.c 7.0 KB

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