offchannel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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-ops.h"
  19. /*
  20. * Tell our hardware to disable PS.
  21. * Optionally inform AP that we will go to sleep so that it will buffer
  22. * the frames while we are doing off-channel work. This is optional
  23. * because we *may* be doing work on-operating channel, and want our
  24. * hardware unconditionally awake, but still let the AP send us normal frames.
  25. */
  26. static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
  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 (!local->offchannel_ps_enabled ||
  42. !ieee80211_hw_check(&local->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, true);
  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, false);
  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, false);
  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. {
  96. struct ieee80211_sub_if_data *sdata;
  97. if (WARN_ON(local->use_chanctx))
  98. return;
  99. /*
  100. * notify the AP about us leaving the channel and stop all
  101. * STA interfaces.
  102. */
  103. /*
  104. * Stop queues and transmit all frames queued by the driver
  105. * before sending nullfunc to enable powersave at the AP.
  106. */
  107. ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  108. IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
  109. false);
  110. ieee80211_flush_queues(local, NULL, false);
  111. mutex_lock(&local->iflist_mtx);
  112. list_for_each_entry(sdata, &local->interfaces, list) {
  113. if (!ieee80211_sdata_running(sdata))
  114. continue;
  115. if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
  116. continue;
  117. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  118. set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  119. /* Check to see if we should disable beaconing. */
  120. if (sdata->vif.bss_conf.enable_beacon) {
  121. set_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
  122. &sdata->state);
  123. sdata->vif.bss_conf.enable_beacon = false;
  124. ieee80211_bss_info_change_notify(
  125. sdata, BSS_CHANGED_BEACON_ENABLED);
  126. }
  127. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  128. sdata->u.mgd.associated)
  129. ieee80211_offchannel_ps_enable(sdata);
  130. }
  131. mutex_unlock(&local->iflist_mtx);
  132. }
  133. void ieee80211_offchannel_return(struct ieee80211_local *local)
  134. {
  135. struct ieee80211_sub_if_data *sdata;
  136. if (WARN_ON(local->use_chanctx))
  137. return;
  138. mutex_lock(&local->iflist_mtx);
  139. list_for_each_entry(sdata, &local->interfaces, list) {
  140. if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
  141. continue;
  142. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  143. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  144. if (!ieee80211_sdata_running(sdata))
  145. continue;
  146. /* Tell AP we're back */
  147. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  148. sdata->u.mgd.associated)
  149. ieee80211_offchannel_ps_disable(sdata);
  150. if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
  151. &sdata->state)) {
  152. sdata->vif.bss_conf.enable_beacon = true;
  153. ieee80211_bss_info_change_notify(
  154. sdata, BSS_CHANGED_BEACON_ENABLED);
  155. }
  156. }
  157. mutex_unlock(&local->iflist_mtx);
  158. ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  159. IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
  160. false);
  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_tid_band(roc->sdata, roc->frame, 7,
  169. roc->chan->band);
  170. roc->frame = NULL;
  171. }
  172. } else {
  173. cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
  174. roc->chan, roc->req_duration,
  175. 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_move(&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 (WARN_ON_ONCE(roc->started))
  225. return;
  226. if (local->ops->remain_on_channel) {
  227. int ret, duration = roc->duration;
  228. /* XXX: duplicated, see ieee80211_start_roc_work() */
  229. if (!duration)
  230. duration = 10;
  231. ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
  232. duration, roc->type);
  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, bool free)
  250. {
  251. struct ieee80211_roc_work *dep, *tmp;
  252. if (WARN_ON(roc->to_be_freed))
  253. return;
  254. /* was never transmitted */
  255. if (roc->frame) {
  256. cfg80211_mgmt_tx_status(&roc->sdata->wdev,
  257. (unsigned long)roc->frame,
  258. roc->frame->data, roc->frame->len,
  259. false, GFP_KERNEL);
  260. kfree_skb(roc->frame);
  261. }
  262. if (!roc->mgmt_tx_cookie)
  263. cfg80211_remain_on_channel_expired(&roc->sdata->wdev,
  264. roc->cookie, roc->chan,
  265. GFP_KERNEL);
  266. list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
  267. ieee80211_roc_notify_destroy(dep, true);
  268. if (free)
  269. kfree(roc);
  270. else
  271. roc->to_be_freed = true;
  272. }
  273. void ieee80211_sw_roc_work(struct work_struct *work)
  274. {
  275. struct ieee80211_roc_work *roc =
  276. container_of(work, struct ieee80211_roc_work, work.work);
  277. struct ieee80211_sub_if_data *sdata = roc->sdata;
  278. struct ieee80211_local *local = sdata->local;
  279. bool started, on_channel;
  280. mutex_lock(&local->mtx);
  281. if (roc->to_be_freed)
  282. goto out_unlock;
  283. if (roc->abort)
  284. goto finish;
  285. if (WARN_ON(list_empty(&local->roc_list)))
  286. goto out_unlock;
  287. if (WARN_ON(roc != list_first_entry(&local->roc_list,
  288. struct ieee80211_roc_work,
  289. list)))
  290. goto out_unlock;
  291. if (!roc->started) {
  292. struct ieee80211_roc_work *dep;
  293. WARN_ON(local->use_chanctx);
  294. /* If actually operating on the desired channel (with at least
  295. * 20 MHz channel width) don't stop all the operations but still
  296. * treat it as though the ROC operation started properly, so
  297. * other ROC operations won't interfere with this one.
  298. */
  299. roc->on_channel = roc->chan == local->_oper_chandef.chan &&
  300. local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
  301. local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
  302. /* start this ROC */
  303. ieee80211_recalc_idle(local);
  304. if (!roc->on_channel) {
  305. ieee80211_offchannel_stop_vifs(local);
  306. local->tmp_channel = roc->chan;
  307. ieee80211_hw_config(local, 0);
  308. }
  309. /* tell userspace or send frame */
  310. ieee80211_handle_roc_started(roc);
  311. list_for_each_entry(dep, &roc->dependents, list)
  312. ieee80211_handle_roc_started(dep);
  313. /* if it was pure TX, just finish right away */
  314. if (!roc->duration)
  315. goto finish;
  316. roc->started = true;
  317. ieee80211_queue_delayed_work(&local->hw, &roc->work,
  318. msecs_to_jiffies(roc->duration));
  319. } else {
  320. /* finish this ROC */
  321. finish:
  322. list_del(&roc->list);
  323. started = roc->started;
  324. on_channel = roc->on_channel;
  325. ieee80211_roc_notify_destroy(roc, !roc->abort);
  326. if (started && !on_channel) {
  327. ieee80211_flush_queues(local, NULL, false);
  328. local->tmp_channel = NULL;
  329. ieee80211_hw_config(local, 0);
  330. ieee80211_offchannel_return(local);
  331. }
  332. ieee80211_recalc_idle(local);
  333. if (started)
  334. ieee80211_start_next_roc(local);
  335. else if (list_empty(&local->roc_list))
  336. ieee80211_run_deferred_scan(local);
  337. }
  338. out_unlock:
  339. mutex_unlock(&local->mtx);
  340. }
  341. static void ieee80211_hw_roc_done(struct work_struct *work)
  342. {
  343. struct ieee80211_local *local =
  344. container_of(work, struct ieee80211_local, hw_roc_done);
  345. struct ieee80211_roc_work *roc;
  346. mutex_lock(&local->mtx);
  347. if (list_empty(&local->roc_list))
  348. goto out_unlock;
  349. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  350. list);
  351. if (!roc->started)
  352. goto out_unlock;
  353. list_del(&roc->list);
  354. ieee80211_roc_notify_destroy(roc, true);
  355. /* if there's another roc, start it now */
  356. ieee80211_start_next_roc(local);
  357. out_unlock:
  358. mutex_unlock(&local->mtx);
  359. }
  360. void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
  361. {
  362. struct ieee80211_local *local = hw_to_local(hw);
  363. trace_api_remain_on_channel_expired(local);
  364. ieee80211_queue_work(hw, &local->hw_roc_done);
  365. }
  366. EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
  367. void ieee80211_roc_setup(struct ieee80211_local *local)
  368. {
  369. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  370. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  371. INIT_LIST_HEAD(&local->roc_list);
  372. }
  373. void ieee80211_roc_purge(struct ieee80211_local *local,
  374. struct ieee80211_sub_if_data *sdata)
  375. {
  376. struct ieee80211_roc_work *roc, *tmp;
  377. LIST_HEAD(tmp_list);
  378. flush_work(&local->hw_roc_start);
  379. mutex_lock(&local->mtx);
  380. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  381. if (sdata && roc->sdata != sdata)
  382. continue;
  383. if (roc->started && local->ops->remain_on_channel) {
  384. /* can race, so ignore return value */
  385. drv_cancel_remain_on_channel(local);
  386. }
  387. list_move_tail(&roc->list, &tmp_list);
  388. roc->abort = true;
  389. }
  390. mutex_unlock(&local->mtx);
  391. list_for_each_entry_safe(roc, tmp, &tmp_list, list) {
  392. if (local->ops->remain_on_channel) {
  393. list_del(&roc->list);
  394. ieee80211_roc_notify_destroy(roc, true);
  395. } else {
  396. ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
  397. /* work will clean up etc */
  398. flush_delayed_work(&roc->work);
  399. WARN_ON(!roc->to_be_freed);
  400. kfree(roc);
  401. }
  402. }
  403. WARN_ON_ONCE(!list_empty(&tmp_list));
  404. }