work.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * mac80211 work implementation
  3. *
  4. * Copyright 2003-2008, Jouni Malinen <j@w1.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/delay.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "rate.h"
  26. #include "driver-ops.h"
  27. enum work_action {
  28. WORK_ACT_NONE,
  29. WORK_ACT_TIMEOUT,
  30. };
  31. /* utils */
  32. static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
  33. {
  34. lockdep_assert_held(&local->mtx);
  35. }
  36. /*
  37. * We can have multiple work items (and connection probing)
  38. * scheduling this timer, but we need to take care to only
  39. * reschedule it when it should fire _earlier_ than it was
  40. * asked for before, or if it's not pending right now. This
  41. * function ensures that. Note that it then is required to
  42. * run this function for all timeouts after the first one
  43. * has happened -- the work that runs from this timer will
  44. * do that.
  45. */
  46. static void run_again(struct ieee80211_local *local,
  47. unsigned long timeout)
  48. {
  49. ASSERT_WORK_MTX(local);
  50. if (!timer_pending(&local->work_timer) ||
  51. time_before(timeout, local->work_timer.expires))
  52. mod_timer(&local->work_timer, timeout);
  53. }
  54. void free_work(struct ieee80211_work *wk)
  55. {
  56. kfree_rcu(wk, rcu_head);
  57. }
  58. static enum work_action __must_check
  59. ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
  60. {
  61. /*
  62. * First time we run, do nothing -- the generic code will
  63. * have switched to the right channel etc.
  64. */
  65. if (!wk->started) {
  66. wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
  67. cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
  68. wk->chan, wk->chan_type,
  69. wk->remain.duration, GFP_KERNEL);
  70. return WORK_ACT_NONE;
  71. }
  72. return WORK_ACT_TIMEOUT;
  73. }
  74. static enum work_action __must_check
  75. ieee80211_offchannel_tx(struct ieee80211_work *wk)
  76. {
  77. if (!wk->started) {
  78. wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
  79. /*
  80. * After this, offchan_tx.frame remains but now is no
  81. * longer a valid pointer -- we still need it as the
  82. * cookie for canceling this work/status matching.
  83. */
  84. ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
  85. return WORK_ACT_NONE;
  86. }
  87. return WORK_ACT_TIMEOUT;
  88. }
  89. static void ieee80211_work_timer(unsigned long data)
  90. {
  91. struct ieee80211_local *local = (void *) data;
  92. if (local->quiescing)
  93. return;
  94. ieee80211_queue_work(&local->hw, &local->work_work);
  95. }
  96. static void ieee80211_work_work(struct work_struct *work)
  97. {
  98. struct ieee80211_local *local =
  99. container_of(work, struct ieee80211_local, work_work);
  100. struct ieee80211_work *wk, *tmp;
  101. LIST_HEAD(free_work);
  102. enum work_action rma;
  103. bool remain_off_channel = false;
  104. /*
  105. * ieee80211_queue_work() should have picked up most cases,
  106. * here we'll pick the rest.
  107. */
  108. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  109. return;
  110. mutex_lock(&local->mtx);
  111. if (local->scanning) {
  112. mutex_unlock(&local->mtx);
  113. return;
  114. }
  115. ieee80211_recalc_idle(local);
  116. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  117. bool started = wk->started;
  118. /* mark work as started if it's on the current off-channel */
  119. if (!started && local->tmp_channel &&
  120. wk->chan == local->tmp_channel &&
  121. wk->chan_type == local->tmp_channel_type) {
  122. started = true;
  123. wk->timeout = jiffies;
  124. }
  125. if (!started && !local->tmp_channel) {
  126. ieee80211_offchannel_stop_vifs(local, true);
  127. local->tmp_channel = wk->chan;
  128. local->tmp_channel_type = wk->chan_type;
  129. ieee80211_hw_config(local, 0);
  130. started = true;
  131. wk->timeout = jiffies;
  132. }
  133. /* don't try to work with items that aren't started */
  134. if (!started)
  135. continue;
  136. if (time_is_after_jiffies(wk->timeout)) {
  137. /*
  138. * This work item isn't supposed to be worked on
  139. * right now, but take care to adjust the timer
  140. * properly.
  141. */
  142. run_again(local, wk->timeout);
  143. continue;
  144. }
  145. switch (wk->type) {
  146. default:
  147. WARN_ON(1);
  148. /* nothing */
  149. rma = WORK_ACT_NONE;
  150. break;
  151. case IEEE80211_WORK_ABORT:
  152. rma = WORK_ACT_TIMEOUT;
  153. break;
  154. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  155. rma = ieee80211_remain_on_channel_timeout(wk);
  156. break;
  157. case IEEE80211_WORK_OFFCHANNEL_TX:
  158. rma = ieee80211_offchannel_tx(wk);
  159. break;
  160. }
  161. wk->started = started;
  162. switch (rma) {
  163. case WORK_ACT_NONE:
  164. /* might have changed the timeout */
  165. run_again(local, wk->timeout);
  166. break;
  167. case WORK_ACT_TIMEOUT:
  168. list_del_rcu(&wk->list);
  169. synchronize_rcu();
  170. list_add(&wk->list, &free_work);
  171. break;
  172. default:
  173. WARN(1, "unexpected: %d", rma);
  174. }
  175. }
  176. list_for_each_entry(wk, &local->work_list, list) {
  177. if (!wk->started)
  178. continue;
  179. if (wk->chan != local->tmp_channel ||
  180. wk->chan_type != local->tmp_channel_type)
  181. continue;
  182. remain_off_channel = true;
  183. }
  184. if (!remain_off_channel && local->tmp_channel) {
  185. local->tmp_channel = NULL;
  186. ieee80211_hw_config(local, 0);
  187. ieee80211_offchannel_return(local, true);
  188. /* give connection some time to breathe */
  189. run_again(local, jiffies + HZ/2);
  190. }
  191. ieee80211_recalc_idle(local);
  192. ieee80211_run_deferred_scan(local);
  193. mutex_unlock(&local->mtx);
  194. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  195. wk->done(wk, NULL);
  196. list_del(&wk->list);
  197. kfree(wk);
  198. }
  199. }
  200. void ieee80211_add_work(struct ieee80211_work *wk)
  201. {
  202. struct ieee80211_local *local;
  203. if (WARN_ON(!wk->chan))
  204. return;
  205. if (WARN_ON(!wk->sdata))
  206. return;
  207. if (WARN_ON(!wk->done))
  208. return;
  209. if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
  210. return;
  211. wk->started = false;
  212. local = wk->sdata->local;
  213. mutex_lock(&local->mtx);
  214. list_add_tail(&wk->list, &local->work_list);
  215. mutex_unlock(&local->mtx);
  216. ieee80211_queue_work(&local->hw, &local->work_work);
  217. }
  218. void ieee80211_work_init(struct ieee80211_local *local)
  219. {
  220. INIT_LIST_HEAD(&local->work_list);
  221. setup_timer(&local->work_timer, ieee80211_work_timer,
  222. (unsigned long)local);
  223. INIT_WORK(&local->work_work, ieee80211_work_work);
  224. }
  225. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  226. {
  227. struct ieee80211_local *local = sdata->local;
  228. struct ieee80211_work *wk;
  229. bool cleanup = false;
  230. mutex_lock(&local->mtx);
  231. list_for_each_entry(wk, &local->work_list, list) {
  232. if (wk->sdata != sdata)
  233. continue;
  234. cleanup = true;
  235. wk->type = IEEE80211_WORK_ABORT;
  236. wk->started = true;
  237. wk->timeout = jiffies;
  238. }
  239. mutex_unlock(&local->mtx);
  240. /* run cleanups etc. */
  241. if (cleanup)
  242. ieee80211_work_work(&local->work_work);
  243. mutex_lock(&local->mtx);
  244. list_for_each_entry(wk, &local->work_list, list) {
  245. if (wk->sdata != sdata)
  246. continue;
  247. WARN_ON(1);
  248. break;
  249. }
  250. mutex_unlock(&local->mtx);
  251. }
  252. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  253. struct sk_buff *skb)
  254. {
  255. /*
  256. * We are done serving the remain-on-channel command.
  257. */
  258. cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
  259. wk->chan, wk->chan_type,
  260. GFP_KERNEL);
  261. return WORK_DONE_DESTROY;
  262. }
  263. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  264. struct ieee80211_channel *chan,
  265. enum nl80211_channel_type channel_type,
  266. unsigned int duration, u64 *cookie)
  267. {
  268. struct ieee80211_work *wk;
  269. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  270. if (!wk)
  271. return -ENOMEM;
  272. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  273. wk->chan = chan;
  274. wk->chan_type = channel_type;
  275. wk->sdata = sdata;
  276. wk->done = ieee80211_remain_done;
  277. wk->remain.duration = duration;
  278. *cookie = (unsigned long) wk;
  279. ieee80211_add_work(wk);
  280. return 0;
  281. }
  282. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  283. u64 cookie)
  284. {
  285. struct ieee80211_local *local = sdata->local;
  286. struct ieee80211_work *wk, *tmp;
  287. bool found = false;
  288. mutex_lock(&local->mtx);
  289. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  290. if ((unsigned long) wk == cookie) {
  291. wk->timeout = jiffies;
  292. found = true;
  293. break;
  294. }
  295. }
  296. mutex_unlock(&local->mtx);
  297. if (!found)
  298. return -ENOENT;
  299. ieee80211_queue_work(&local->hw, &local->work_work);
  300. return 0;
  301. }