offchannel.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Off-channel operation helpers
  3. *
  4. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Copyright 2004, Instant802 Networks, Inc.
  6. * Copyright 2005, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "driver-trace.h"
  18. /*
  19. * Tell our hardware to disable PS.
  20. * Optionally inform AP that we will go to sleep so that it will buffer
  21. * the frames while we are doing off-channel work. This is optional
  22. * because we *may* be doing work on-operating channel, and want our
  23. * hardware unconditionally awake, but still let the AP send us normal frames.
  24. */
  25. static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata,
  26. bool tell_ap)
  27. {
  28. struct ieee80211_local *local = sdata->local;
  29. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  30. local->offchannel_ps_enabled = false;
  31. /* FIXME: what to do when local->pspolling is true? */
  32. del_timer_sync(&local->dynamic_ps_timer);
  33. del_timer_sync(&ifmgd->bcn_mon_timer);
  34. del_timer_sync(&ifmgd->conn_mon_timer);
  35. cancel_work_sync(&local->dynamic_ps_enable_work);
  36. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  37. local->offchannel_ps_enabled = true;
  38. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  39. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  40. }
  41. if (tell_ap && (!local->offchannel_ps_enabled ||
  42. !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)))
  43. /*
  44. * If power save was enabled, no need to send a nullfunc
  45. * frame because AP knows that we are sleeping. But if the
  46. * hardware is creating the nullfunc frame for power save
  47. * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
  48. * enabled) and power save was enabled, the firmware just
  49. * sent a null frame with power save disabled. So we need
  50. * to send a new nullfunc frame to inform the AP that we
  51. * are again sleeping.
  52. */
  53. ieee80211_send_nullfunc(local, sdata, 1);
  54. }
  55. /* inform AP that we are awake again, unless power save is enabled */
  56. static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
  57. {
  58. struct ieee80211_local *local = sdata->local;
  59. if (!local->ps_sdata)
  60. ieee80211_send_nullfunc(local, sdata, 0);
  61. else if (local->offchannel_ps_enabled) {
  62. /*
  63. * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
  64. * will send a nullfunc frame with the powersave bit set
  65. * even though the AP already knows that we are sleeping.
  66. * This could be avoided by sending a null frame with power
  67. * save bit disabled before enabling the power save, but
  68. * this doesn't gain anything.
  69. *
  70. * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
  71. * to send a nullfunc frame because AP already knows that
  72. * we are sleeping, let's just enable power save mode in
  73. * hardware.
  74. */
  75. /* TODO: Only set hardware if CONF_PS changed?
  76. * TODO: Should we set offchannel_ps_enabled to false?
  77. */
  78. local->hw.conf.flags |= IEEE80211_CONF_PS;
  79. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  80. } else if (local->hw.conf.dynamic_ps_timeout > 0) {
  81. /*
  82. * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
  83. * had been running before leaving the operating channel,
  84. * restart the timer now and send a nullfunc frame to inform
  85. * the AP that we are awake.
  86. */
  87. ieee80211_send_nullfunc(local, sdata, 0);
  88. mod_timer(&local->dynamic_ps_timer, jiffies +
  89. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  90. }
  91. ieee80211_sta_reset_beacon_monitor(sdata);
  92. ieee80211_sta_reset_conn_monitor(sdata);
  93. }
  94. void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
  95. bool offchannel_ps_enable)
  96. {
  97. struct ieee80211_sub_if_data *sdata;
  98. /*
  99. * notify the AP about us leaving the channel and stop all
  100. * STA interfaces.
  101. */
  102. mutex_lock(&local->iflist_mtx);
  103. list_for_each_entry(sdata, &local->interfaces, list) {
  104. if (!ieee80211_sdata_running(sdata))
  105. continue;
  106. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  107. set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  108. /* Check to see if we should disable beaconing. */
  109. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  110. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  111. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  112. ieee80211_bss_info_change_notify(
  113. sdata, BSS_CHANGED_BEACON_ENABLED);
  114. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  115. netif_tx_stop_all_queues(sdata->dev);
  116. if (offchannel_ps_enable &&
  117. (sdata->vif.type == NL80211_IFTYPE_STATION) &&
  118. sdata->u.mgd.associated)
  119. ieee80211_offchannel_ps_enable(sdata, true);
  120. }
  121. }
  122. mutex_unlock(&local->iflist_mtx);
  123. }
  124. void ieee80211_offchannel_enable_all_ps(struct ieee80211_local *local,
  125. bool tell_ap)
  126. {
  127. struct ieee80211_sub_if_data *sdata;
  128. mutex_lock(&local->iflist_mtx);
  129. list_for_each_entry(sdata, &local->interfaces, list) {
  130. if (!ieee80211_sdata_running(sdata))
  131. continue;
  132. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  133. sdata->u.mgd.associated)
  134. ieee80211_offchannel_ps_enable(sdata, tell_ap);
  135. }
  136. mutex_unlock(&local->iflist_mtx);
  137. }
  138. void ieee80211_offchannel_return(struct ieee80211_local *local,
  139. bool enable_beaconing,
  140. bool offchannel_ps_disable)
  141. {
  142. struct ieee80211_sub_if_data *sdata;
  143. mutex_lock(&local->iflist_mtx);
  144. list_for_each_entry(sdata, &local->interfaces, list) {
  145. if (!ieee80211_sdata_running(sdata))
  146. continue;
  147. /* Tell AP we're back */
  148. if (offchannel_ps_disable &&
  149. sdata->vif.type == NL80211_IFTYPE_STATION) {
  150. if (sdata->u.mgd.associated)
  151. ieee80211_offchannel_ps_disable(sdata);
  152. }
  153. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  154. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  155. /*
  156. * This may wake up queues even though the driver
  157. * currently has them stopped. This is not very
  158. * likely, since the driver won't have gotten any
  159. * (or hardly any) new packets while we weren't
  160. * on the right channel, and even if it happens
  161. * it will at most lead to queueing up one more
  162. * packet per queue in mac80211 rather than on
  163. * the interface qdisc.
  164. */
  165. netif_tx_wake_all_queues(sdata->dev);
  166. }
  167. /* Check to see if we should re-enable beaconing */
  168. if (enable_beaconing &&
  169. (sdata->vif.type == NL80211_IFTYPE_AP ||
  170. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  171. sdata->vif.type == NL80211_IFTYPE_MESH_POINT))
  172. ieee80211_bss_info_change_notify(
  173. sdata, BSS_CHANGED_BEACON_ENABLED);
  174. }
  175. mutex_unlock(&local->iflist_mtx);
  176. }
  177. static void ieee80211_hw_roc_start(struct work_struct *work)
  178. {
  179. struct ieee80211_local *local =
  180. container_of(work, struct ieee80211_local, hw_roc_start);
  181. struct ieee80211_sub_if_data *sdata;
  182. mutex_lock(&local->mtx);
  183. if (!local->hw_roc_channel) {
  184. mutex_unlock(&local->mtx);
  185. return;
  186. }
  187. ieee80211_recalc_idle(local);
  188. if (local->hw_roc_skb) {
  189. sdata = IEEE80211_DEV_TO_SUB_IF(local->hw_roc_dev);
  190. ieee80211_tx_skb(sdata, local->hw_roc_skb);
  191. local->hw_roc_skb = NULL;
  192. } else {
  193. cfg80211_ready_on_channel(local->hw_roc_dev,
  194. local->hw_roc_cookie,
  195. local->hw_roc_channel,
  196. local->hw_roc_channel_type,
  197. local->hw_roc_duration,
  198. GFP_KERNEL);
  199. }
  200. mutex_unlock(&local->mtx);
  201. }
  202. void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
  203. {
  204. struct ieee80211_local *local = hw_to_local(hw);
  205. trace_api_ready_on_channel(local);
  206. ieee80211_queue_work(hw, &local->hw_roc_start);
  207. }
  208. EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
  209. static void ieee80211_hw_roc_done(struct work_struct *work)
  210. {
  211. struct ieee80211_local *local =
  212. container_of(work, struct ieee80211_local, hw_roc_done);
  213. mutex_lock(&local->mtx);
  214. if (!local->hw_roc_channel) {
  215. mutex_unlock(&local->mtx);
  216. return;
  217. }
  218. if (!local->hw_roc_for_tx)
  219. cfg80211_remain_on_channel_expired(local->hw_roc_dev,
  220. local->hw_roc_cookie,
  221. local->hw_roc_channel,
  222. local->hw_roc_channel_type,
  223. GFP_KERNEL);
  224. local->hw_roc_channel = NULL;
  225. local->hw_roc_cookie = 0;
  226. ieee80211_recalc_idle(local);
  227. mutex_unlock(&local->mtx);
  228. }
  229. void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
  230. {
  231. struct ieee80211_local *local = hw_to_local(hw);
  232. trace_api_remain_on_channel_expired(local);
  233. ieee80211_queue_work(hw, &local->hw_roc_done);
  234. }
  235. EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
  236. void ieee80211_hw_roc_setup(struct ieee80211_local *local)
  237. {
  238. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  239. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  240. }