ps.c 7.4 KB

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