offchannel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 <linux/export.h>
  16. #include <net/mac80211.h>
  17. #include "ieee80211_i.h"
  18. #include "driver-trace.h"
  19. #include "driver-ops.h"
  20. /*
  21. * Tell our hardware to disable PS.
  22. * Optionally inform AP that we will go to sleep so that it will buffer
  23. * the frames while we are doing off-channel work. This is optional
  24. * because we *may* be doing work on-operating channel, and want our
  25. * hardware unconditionally awake, but still let the AP send us normal frames.
  26. */
  27. static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
  28. {
  29. struct ieee80211_local *local = sdata->local;
  30. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  31. local->offchannel_ps_enabled = false;
  32. /* FIXME: what to do when local->pspolling is true? */
  33. del_timer_sync(&local->dynamic_ps_timer);
  34. del_timer_sync(&ifmgd->bcn_mon_timer);
  35. del_timer_sync(&ifmgd->conn_mon_timer);
  36. cancel_work_sync(&local->dynamic_ps_enable_work);
  37. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  38. local->offchannel_ps_enabled = true;
  39. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  40. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  41. }
  42. if (!local->offchannel_ps_enabled ||
  43. !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
  44. /*
  45. * If power save was enabled, no need to send a nullfunc
  46. * frame because AP knows that we are sleeping. But if the
  47. * hardware is creating the nullfunc frame for power save
  48. * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
  49. * enabled) and power save was enabled, the firmware just
  50. * sent a null frame with power save disabled. So we need
  51. * to send a new nullfunc frame to inform the AP that we
  52. * are again sleeping.
  53. */
  54. ieee80211_send_nullfunc(local, sdata, 1);
  55. }
  56. /* inform AP that we are awake again, unless power save is enabled */
  57. static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
  58. {
  59. struct ieee80211_local *local = sdata->local;
  60. if (!local->ps_sdata)
  61. ieee80211_send_nullfunc(local, sdata, 0);
  62. else if (local->offchannel_ps_enabled) {
  63. /*
  64. * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
  65. * will send a nullfunc frame with the powersave bit set
  66. * even though the AP already knows that we are sleeping.
  67. * This could be avoided by sending a null frame with power
  68. * save bit disabled before enabling the power save, but
  69. * this doesn't gain anything.
  70. *
  71. * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
  72. * to send a nullfunc frame because AP already knows that
  73. * we are sleeping, let's just enable power save mode in
  74. * hardware.
  75. */
  76. /* TODO: Only set hardware if CONF_PS changed?
  77. * TODO: Should we set offchannel_ps_enabled to false?
  78. */
  79. local->hw.conf.flags |= IEEE80211_CONF_PS;
  80. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  81. } else if (local->hw.conf.dynamic_ps_timeout > 0) {
  82. /*
  83. * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
  84. * had been running before leaving the operating channel,
  85. * restart the timer now and send a nullfunc frame to inform
  86. * the AP that we are awake.
  87. */
  88. ieee80211_send_nullfunc(local, sdata, 0);
  89. mod_timer(&local->dynamic_ps_timer, jiffies +
  90. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  91. }
  92. ieee80211_sta_reset_beacon_monitor(sdata);
  93. ieee80211_sta_reset_conn_monitor(sdata);
  94. }
  95. void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
  96. bool offchannel_ps_enable)
  97. {
  98. struct ieee80211_sub_if_data *sdata;
  99. /*
  100. * notify the AP about us leaving the channel and stop all
  101. * STA interfaces.
  102. */
  103. mutex_lock(&local->iflist_mtx);
  104. list_for_each_entry(sdata, &local->interfaces, list) {
  105. if (!ieee80211_sdata_running(sdata))
  106. continue;
  107. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  108. set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  109. /* Check to see if we should disable beaconing. */
  110. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  111. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  112. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  113. ieee80211_bss_info_change_notify(
  114. sdata, BSS_CHANGED_BEACON_ENABLED);
  115. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  116. netif_tx_stop_all_queues(sdata->dev);
  117. if (offchannel_ps_enable &&
  118. (sdata->vif.type == NL80211_IFTYPE_STATION) &&
  119. sdata->u.mgd.associated)
  120. ieee80211_offchannel_ps_enable(sdata);
  121. }
  122. }
  123. mutex_unlock(&local->iflist_mtx);
  124. }
  125. void ieee80211_offchannel_return(struct ieee80211_local *local,
  126. bool offchannel_ps_disable)
  127. {
  128. struct ieee80211_sub_if_data *sdata;
  129. mutex_lock(&local->iflist_mtx);
  130. list_for_each_entry(sdata, &local->interfaces, list) {
  131. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  132. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  133. if (!ieee80211_sdata_running(sdata))
  134. continue;
  135. /* Tell AP we're back */
  136. if (offchannel_ps_disable &&
  137. sdata->vif.type == NL80211_IFTYPE_STATION) {
  138. if (sdata->u.mgd.associated)
  139. ieee80211_offchannel_ps_disable(sdata);
  140. }
  141. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  142. /*
  143. * This may wake up queues even though the driver
  144. * currently has them stopped. This is not very
  145. * likely, since the driver won't have gotten any
  146. * (or hardly any) new packets while we weren't
  147. * on the right channel, and even if it happens
  148. * it will at most lead to queueing up one more
  149. * packet per queue in mac80211 rather than on
  150. * the interface qdisc.
  151. */
  152. netif_tx_wake_all_queues(sdata->dev);
  153. }
  154. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  155. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  156. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  157. ieee80211_bss_info_change_notify(
  158. sdata, BSS_CHANGED_BEACON_ENABLED);
  159. }
  160. mutex_unlock(&local->iflist_mtx);
  161. }
  162. void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc)
  163. {
  164. if (roc->notified)
  165. return;
  166. if (roc->mgmt_tx_cookie) {
  167. if (!WARN_ON(!roc->frame)) {
  168. ieee80211_tx_skb(roc->sdata, roc->frame);
  169. roc->frame = NULL;
  170. }
  171. } else {
  172. cfg80211_ready_on_channel(roc->sdata->dev, (unsigned long)roc,
  173. roc->chan, roc->chan_type,
  174. roc->req_duration, GFP_KERNEL);
  175. }
  176. roc->notified = true;
  177. }
  178. static void ieee80211_hw_roc_start(struct work_struct *work)
  179. {
  180. struct ieee80211_local *local =
  181. container_of(work, struct ieee80211_local, hw_roc_start);
  182. struct ieee80211_roc_work *roc, *dep, *tmp;
  183. mutex_lock(&local->mtx);
  184. if (list_empty(&local->roc_list))
  185. goto out_unlock;
  186. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  187. list);
  188. if (!roc->started)
  189. goto out_unlock;
  190. roc->hw_begun = true;
  191. roc->hw_start_time = local->hw_roc_start_time;
  192. ieee80211_handle_roc_started(roc);
  193. list_for_each_entry_safe(dep, tmp, &roc->dependents, list) {
  194. ieee80211_handle_roc_started(dep);
  195. if (dep->duration > roc->duration) {
  196. u32 dur = dep->duration;
  197. dep->duration = dur - roc->duration;
  198. roc->duration = dur;
  199. list_del(&dep->list);
  200. list_add(&dep->list, &roc->list);
  201. }
  202. }
  203. out_unlock:
  204. mutex_unlock(&local->mtx);
  205. }
  206. void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
  207. {
  208. struct ieee80211_local *local = hw_to_local(hw);
  209. local->hw_roc_start_time = jiffies;
  210. trace_api_ready_on_channel(local);
  211. ieee80211_queue_work(hw, &local->hw_roc_start);
  212. }
  213. EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
  214. void ieee80211_start_next_roc(struct ieee80211_local *local)
  215. {
  216. struct ieee80211_roc_work *roc;
  217. lockdep_assert_held(&local->mtx);
  218. if (list_empty(&local->roc_list)) {
  219. ieee80211_run_deferred_scan(local);
  220. return;
  221. }
  222. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  223. list);
  224. if (local->ops->remain_on_channel) {
  225. int ret, duration = roc->duration;
  226. /* XXX: duplicated, see ieee80211_start_roc_work() */
  227. if (!duration)
  228. duration = 10;
  229. ret = drv_remain_on_channel(local, roc->chan,
  230. roc->chan_type,
  231. duration);
  232. roc->started = true;
  233. if (ret) {
  234. wiphy_warn(local->hw.wiphy,
  235. "failed to start next HW ROC (%d)\n", ret);
  236. /*
  237. * queue the work struct again to avoid recursion
  238. * when multiple failures occur
  239. */
  240. ieee80211_remain_on_channel_expired(&local->hw);
  241. }
  242. } else {
  243. /* delay it a bit */
  244. ieee80211_queue_delayed_work(&local->hw, &roc->work,
  245. round_jiffies_relative(HZ/2));
  246. }
  247. }
  248. void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
  249. {
  250. struct ieee80211_roc_work *dep, *tmp;
  251. /* was never transmitted */
  252. if (roc->frame) {
  253. cfg80211_mgmt_tx_status(roc->sdata->dev,
  254. (unsigned long)roc->frame,
  255. roc->frame->data, roc->frame->len,
  256. false, GFP_KERNEL);
  257. kfree_skb(roc->frame);
  258. }
  259. if (!roc->mgmt_tx_cookie)
  260. cfg80211_remain_on_channel_expired(roc->sdata->dev,
  261. (unsigned long)roc,
  262. roc->chan, roc->chan_type,
  263. GFP_KERNEL);
  264. list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
  265. ieee80211_roc_notify_destroy(dep);
  266. kfree(roc);
  267. }
  268. void ieee80211_sw_roc_work(struct work_struct *work)
  269. {
  270. struct ieee80211_roc_work *roc =
  271. container_of(work, struct ieee80211_roc_work, work.work);
  272. struct ieee80211_sub_if_data *sdata = roc->sdata;
  273. struct ieee80211_local *local = sdata->local;
  274. mutex_lock(&local->mtx);
  275. if (roc->abort)
  276. goto finish;
  277. if (WARN_ON(list_empty(&local->roc_list)))
  278. goto out_unlock;
  279. if (WARN_ON(roc != list_first_entry(&local->roc_list,
  280. struct ieee80211_roc_work,
  281. list)))
  282. goto out_unlock;
  283. if (!roc->started) {
  284. struct ieee80211_roc_work *dep;
  285. /* start this ROC */
  286. /* switch channel etc */
  287. ieee80211_recalc_idle(local);
  288. local->tmp_channel = roc->chan;
  289. local->tmp_channel_type = roc->chan_type;
  290. ieee80211_hw_config(local, 0);
  291. /* tell userspace or send frame */
  292. ieee80211_handle_roc_started(roc);
  293. list_for_each_entry(dep, &roc->dependents, list)
  294. ieee80211_handle_roc_started(dep);
  295. /* if it was pure TX, just finish right away */
  296. if (!roc->duration)
  297. goto finish;
  298. roc->started = true;
  299. ieee80211_queue_delayed_work(&local->hw, &roc->work,
  300. msecs_to_jiffies(roc->duration));
  301. } else {
  302. /* finish this ROC */
  303. finish:
  304. list_del(&roc->list);
  305. ieee80211_roc_notify_destroy(roc);
  306. if (roc->started) {
  307. drv_flush(local, false);
  308. local->tmp_channel = NULL;
  309. ieee80211_hw_config(local, 0);
  310. ieee80211_offchannel_return(local, true);
  311. }
  312. ieee80211_recalc_idle(local);
  313. ieee80211_start_next_roc(local);
  314. }
  315. out_unlock:
  316. mutex_unlock(&local->mtx);
  317. }
  318. static void ieee80211_hw_roc_done(struct work_struct *work)
  319. {
  320. struct ieee80211_local *local =
  321. container_of(work, struct ieee80211_local, hw_roc_done);
  322. struct ieee80211_roc_work *roc;
  323. mutex_lock(&local->mtx);
  324. if (list_empty(&local->roc_list))
  325. goto out_unlock;
  326. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  327. list);
  328. if (!roc->started)
  329. goto out_unlock;
  330. list_del(&roc->list);
  331. ieee80211_roc_notify_destroy(roc);
  332. /* if there's another roc, start it now */
  333. ieee80211_start_next_roc(local);
  334. out_unlock:
  335. mutex_unlock(&local->mtx);
  336. }
  337. void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
  338. {
  339. struct ieee80211_local *local = hw_to_local(hw);
  340. trace_api_remain_on_channel_expired(local);
  341. ieee80211_queue_work(hw, &local->hw_roc_done);
  342. }
  343. EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
  344. void ieee80211_roc_setup(struct ieee80211_local *local)
  345. {
  346. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  347. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  348. INIT_LIST_HEAD(&local->roc_list);
  349. }
  350. void ieee80211_roc_purge(struct ieee80211_sub_if_data *sdata)
  351. {
  352. struct ieee80211_local *local = sdata->local;
  353. struct ieee80211_roc_work *roc, *tmp;
  354. LIST_HEAD(tmp_list);
  355. mutex_lock(&local->mtx);
  356. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  357. if (roc->sdata != sdata)
  358. continue;
  359. if (roc->started && local->ops->remain_on_channel) {
  360. /* can race, so ignore return value */
  361. drv_cancel_remain_on_channel(local);
  362. }
  363. list_move_tail(&roc->list, &tmp_list);
  364. roc->abort = true;
  365. }
  366. ieee80211_start_next_roc(local);
  367. mutex_unlock(&local->mtx);
  368. list_for_each_entry_safe(roc, tmp, &tmp_list, list) {
  369. if (local->ops->remain_on_channel) {
  370. list_del(&roc->list);
  371. ieee80211_roc_notify_destroy(roc);
  372. } else {
  373. ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
  374. /* work will clean up etc */
  375. flush_delayed_work(&roc->work);
  376. }
  377. }
  378. WARN_ON_ONCE(!list_empty(&tmp_list));
  379. }