offchannel.c 13 KB

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