blob: abb226dc4753f74e3902d309a9e93d51d38782de [file] [log] [blame]
Jouni Malinenb203ffc2009-12-23 13:15:40 +01001/*
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 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Jouni Malinenb203ffc2009-12-23 13:15:40 +010016#include <net/mac80211.h>
17#include "ieee80211_i.h"
Johannes Berg21f83582010-12-18 17:20:47 +010018#include "driver-trace.h"
Johannes Berg2eb278e2012-06-05 14:28:42 +020019#include "driver-ops.h"
Jouni Malinenb203ffc2009-12-23 13:15:40 +010020
21/*
Ben Greearb23b0252011-02-04 11:54:17 -080022 * Tell our hardware to disable PS.
23 * Optionally inform AP that we will go to sleep so that it will buffer
24 * the frames while we are doing off-channel work. This is optional
25 * because we *may* be doing work on-operating channel, and want our
26 * hardware unconditionally awake, but still let the AP send us normal frames.
Jouni Malinenb203ffc2009-12-23 13:15:40 +010027 */
Ben Greearb23b0252011-02-04 11:54:17 -080028static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata,
29 bool tell_ap)
Jouni Malinenb203ffc2009-12-23 13:15:40 +010030{
31 struct ieee80211_local *local = sdata->local;
Luis R. Rodriguez4730d592010-09-16 15:12:31 -040032 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinenb203ffc2009-12-23 13:15:40 +010033
34 local->offchannel_ps_enabled = false;
35
36 /* FIXME: what to do when local->pspolling is true? */
37
38 del_timer_sync(&local->dynamic_ps_timer);
Luis R. Rodriguez3bc3c0d2010-09-16 15:12:33 -040039 del_timer_sync(&ifmgd->bcn_mon_timer);
Luis R. Rodriguez4730d592010-09-16 15:12:31 -040040 del_timer_sync(&ifmgd->conn_mon_timer);
41
Jouni Malinenb203ffc2009-12-23 13:15:40 +010042 cancel_work_sync(&local->dynamic_ps_enable_work);
43
44 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
45 local->offchannel_ps_enabled = true;
46 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
47 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
48 }
49
Ben Greearb23b0252011-02-04 11:54:17 -080050 if (tell_ap && (!local->offchannel_ps_enabled ||
51 !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)))
Jouni Malinenb203ffc2009-12-23 13:15:40 +010052 /*
53 * If power save was enabled, no need to send a nullfunc
54 * frame because AP knows that we are sleeping. But if the
55 * hardware is creating the nullfunc frame for power save
56 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
57 * enabled) and power save was enabled, the firmware just
58 * sent a null frame with power save disabled. So we need
59 * to send a new nullfunc frame to inform the AP that we
60 * are again sleeping.
61 */
62 ieee80211_send_nullfunc(local, sdata, 1);
63}
64
65/* inform AP that we are awake again, unless power save is enabled */
66static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
67{
68 struct ieee80211_local *local = sdata->local;
69
70 if (!local->ps_sdata)
71 ieee80211_send_nullfunc(local, sdata, 0);
72 else if (local->offchannel_ps_enabled) {
73 /*
74 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
75 * will send a nullfunc frame with the powersave bit set
76 * even though the AP already knows that we are sleeping.
77 * This could be avoided by sending a null frame with power
78 * save bit disabled before enabling the power save, but
79 * this doesn't gain anything.
80 *
81 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
82 * to send a nullfunc frame because AP already knows that
83 * we are sleeping, let's just enable power save mode in
84 * hardware.
85 */
Ben Greearb23b0252011-02-04 11:54:17 -080086 /* TODO: Only set hardware if CONF_PS changed?
87 * TODO: Should we set offchannel_ps_enabled to false?
88 */
Jouni Malinenb203ffc2009-12-23 13:15:40 +010089 local->hw.conf.flags |= IEEE80211_CONF_PS;
90 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
91 } else if (local->hw.conf.dynamic_ps_timeout > 0) {
92 /*
93 * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
94 * had been running before leaving the operating channel,
95 * restart the timer now and send a nullfunc frame to inform
96 * the AP that we are awake.
97 */
98 ieee80211_send_nullfunc(local, sdata, 0);
99 mod_timer(&local->dynamic_ps_timer, jiffies +
100 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
101 }
Luis R. Rodriguez4730d592010-09-16 15:12:31 -0400102
Luis R. Rodriguez3bc3c0d2010-09-16 15:12:33 -0400103 ieee80211_sta_reset_beacon_monitor(sdata);
Luis R. Rodriguez4730d592010-09-16 15:12:31 -0400104 ieee80211_sta_reset_conn_monitor(sdata);
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100105}
106
Ben Greearb23b0252011-02-04 11:54:17 -0800107void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
108 bool offchannel_ps_enable)
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100109{
110 struct ieee80211_sub_if_data *sdata;
111
112 /*
Ben Greearb23b0252011-02-04 11:54:17 -0800113 * notify the AP about us leaving the channel and stop all
114 * STA interfaces.
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100115 */
116 mutex_lock(&local->iflist_mtx);
117 list_for_each_entry(sdata, &local->interfaces, list) {
118 if (!ieee80211_sdata_running(sdata))
119 continue;
120
Ben Greearb23b0252011-02-04 11:54:17 -0800121 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
Johannes Berg5b714c62010-08-27 13:45:28 +0200122 set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
Ben Greearb23b0252011-02-04 11:54:17 -0800123
124 /* Check to see if we should disable beaconing. */
125 if (sdata->vif.type == NL80211_IFTYPE_AP ||
126 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
127 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
128 ieee80211_bss_info_change_notify(
129 sdata, BSS_CHANGED_BEACON_ENABLED);
130
131 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
John W. Linvillecfa6cb22010-01-06 17:22:54 -0500132 netif_tx_stop_all_queues(sdata->dev);
Ben Greearb23b0252011-02-04 11:54:17 -0800133 if (offchannel_ps_enable &&
134 (sdata->vif.type == NL80211_IFTYPE_STATION) &&
135 sdata->u.mgd.associated)
136 ieee80211_offchannel_ps_enable(sdata, true);
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100137 }
138 }
139 mutex_unlock(&local->iflist_mtx);
140}
141
142void ieee80211_offchannel_return(struct ieee80211_local *local,
Ben Greearb23b0252011-02-04 11:54:17 -0800143 bool offchannel_ps_disable)
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100144{
145 struct ieee80211_sub_if_data *sdata;
146
147 mutex_lock(&local->iflist_mtx);
148 list_for_each_entry(sdata, &local->interfaces, list) {
Eliad Pellerf6e8cb72011-12-23 01:48:06 +0200149 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
150 clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
151
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100152 if (!ieee80211_sdata_running(sdata))
153 continue;
154
155 /* Tell AP we're back */
Ben Greearb23b0252011-02-04 11:54:17 -0800156 if (offchannel_ps_disable &&
157 sdata->vif.type == NL80211_IFTYPE_STATION) {
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100158 if (sdata->u.mgd.associated)
159 ieee80211_offchannel_ps_disable(sdata);
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100160 }
161
Johannes Berg5b714c62010-08-27 13:45:28 +0200162 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
Johannes Berg5b714c62010-08-27 13:45:28 +0200163 /*
164 * This may wake up queues even though the driver
165 * currently has them stopped. This is not very
166 * likely, since the driver won't have gotten any
167 * (or hardly any) new packets while we weren't
168 * on the right channel, and even if it happens
169 * it will at most lead to queueing up one more
170 * packet per queue in mac80211 rather than on
171 * the interface qdisc.
172 */
Benoit Papillault93895752010-01-14 23:20:31 +0100173 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg5b714c62010-08-27 13:45:28 +0200174 }
Benoit Papillault93895752010-01-14 23:20:31 +0100175
Johannes Berge76aadc2011-11-29 10:20:02 +0100176 if (sdata->vif.type == NL80211_IFTYPE_AP ||
177 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
178 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
Jouni Malinenb203ffc2009-12-23 13:15:40 +0100179 ieee80211_bss_info_change_notify(
180 sdata, BSS_CHANGED_BEACON_ENABLED);
181 }
182 mutex_unlock(&local->iflist_mtx);
183}
Johannes Berg21f83582010-12-18 17:20:47 +0100184
Johannes Berg2eb278e2012-06-05 14:28:42 +0200185void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc)
186{
187 if (roc->notified)
188 return;
189
190 if (roc->mgmt_tx_cookie) {
191 if (!WARN_ON(!roc->frame)) {
192 ieee80211_tx_skb(roc->sdata, roc->frame);
193 roc->frame = NULL;
194 }
195 } else {
196 cfg80211_ready_on_channel(roc->sdata->dev, (unsigned long)roc,
197 roc->chan, roc->chan_type,
198 roc->req_duration, GFP_KERNEL);
199 }
200
201 roc->notified = true;
202}
203
Johannes Berg21f83582010-12-18 17:20:47 +0100204static void ieee80211_hw_roc_start(struct work_struct *work)
205{
206 struct ieee80211_local *local =
207 container_of(work, struct ieee80211_local, hw_roc_start);
Johannes Berg2eb278e2012-06-05 14:28:42 +0200208 struct ieee80211_roc_work *roc, *dep, *tmp;
Johannes Berg21f83582010-12-18 17:20:47 +0100209
210 mutex_lock(&local->mtx);
211
Johannes Berg2eb278e2012-06-05 14:28:42 +0200212 if (list_empty(&local->roc_list))
213 goto out_unlock;
Johannes Berg21f83582010-12-18 17:20:47 +0100214
Johannes Berg2eb278e2012-06-05 14:28:42 +0200215 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
216 list);
Johannes Berg90fc4b32010-12-18 17:20:48 +0100217
Johannes Berg2eb278e2012-06-05 14:28:42 +0200218 if (!roc->started)
219 goto out_unlock;
220
221 roc->hw_begun = true;
222 roc->hw_start_time = local->hw_roc_start_time;
223
224 ieee80211_handle_roc_started(roc);
225 list_for_each_entry_safe(dep, tmp, &roc->dependents, list) {
226 ieee80211_handle_roc_started(dep);
227
228 if (dep->duration > roc->duration) {
229 u32 dur = dep->duration;
230 dep->duration = dur - roc->duration;
231 roc->duration = dur;
232 list_del(&dep->list);
233 list_add(&dep->list, &roc->list);
234 }
235 }
236 out_unlock:
Johannes Berg21f83582010-12-18 17:20:47 +0100237 mutex_unlock(&local->mtx);
238}
239
240void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
241{
242 struct ieee80211_local *local = hw_to_local(hw);
243
Johannes Berg2eb278e2012-06-05 14:28:42 +0200244 local->hw_roc_start_time = jiffies;
245
Johannes Berg21f83582010-12-18 17:20:47 +0100246 trace_api_ready_on_channel(local);
247
248 ieee80211_queue_work(hw, &local->hw_roc_start);
249}
250EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
251
Johannes Berg2eb278e2012-06-05 14:28:42 +0200252void ieee80211_start_next_roc(struct ieee80211_local *local)
253{
254 struct ieee80211_roc_work *roc;
255
256 lockdep_assert_held(&local->mtx);
257
258 if (list_empty(&local->roc_list)) {
259 ieee80211_run_deferred_scan(local);
260 return;
261 }
262
263 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
264 list);
265
266 if (local->ops->remain_on_channel) {
267 int ret, duration = roc->duration;
268
269 /* XXX: duplicated, see ieee80211_start_roc_work() */
270 if (!duration)
271 duration = 10;
272
273 ret = drv_remain_on_channel(local, roc->chan,
274 roc->chan_type,
275 duration);
276
277 roc->started = true;
278
279 if (ret) {
280 wiphy_warn(local->hw.wiphy,
281 "failed to start next HW ROC (%d)\n", ret);
282 /*
283 * queue the work struct again to avoid recursion
284 * when multiple failures occur
285 */
286 ieee80211_remain_on_channel_expired(&local->hw);
287 }
288 } else {
289 /* delay it a bit */
290 ieee80211_queue_delayed_work(&local->hw, &roc->work,
291 round_jiffies_relative(HZ/2));
292 }
293}
294
295void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
296{
297 struct ieee80211_roc_work *dep, *tmp;
298
299 /* was never transmitted */
300 if (roc->frame) {
301 cfg80211_mgmt_tx_status(roc->sdata->dev,
302 (unsigned long)roc->frame,
303 roc->frame->data, roc->frame->len,
304 false, GFP_KERNEL);
305 kfree_skb(roc->frame);
306 }
307
308 if (!roc->mgmt_tx_cookie)
309 cfg80211_remain_on_channel_expired(roc->sdata->dev,
310 (unsigned long)roc,
311 roc->chan, roc->chan_type,
312 GFP_KERNEL);
313
314 list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
315 ieee80211_roc_notify_destroy(dep);
316
317 kfree(roc);
318}
319
320void ieee80211_sw_roc_work(struct work_struct *work)
321{
322 struct ieee80211_roc_work *roc =
323 container_of(work, struct ieee80211_roc_work, work.work);
324 struct ieee80211_sub_if_data *sdata = roc->sdata;
325 struct ieee80211_local *local = sdata->local;
326
327 mutex_lock(&local->mtx);
328
329 if (roc->abort)
330 goto finish;
331
332 if (WARN_ON(list_empty(&local->roc_list)))
333 goto out_unlock;
334
335 if (WARN_ON(roc != list_first_entry(&local->roc_list,
336 struct ieee80211_roc_work,
337 list)))
338 goto out_unlock;
339
340 if (!roc->started) {
341 struct ieee80211_roc_work *dep;
342
343 /* start this ROC */
344
345 /* switch channel etc */
346 ieee80211_recalc_idle(local);
347
348 local->tmp_channel = roc->chan;
349 local->tmp_channel_type = roc->chan_type;
350 ieee80211_hw_config(local, 0);
351
352 /* tell userspace or send frame */
353 ieee80211_handle_roc_started(roc);
354 list_for_each_entry(dep, &roc->dependents, list)
355 ieee80211_handle_roc_started(dep);
356
357 /* if it was pure TX, just finish right away */
358 if (!roc->duration)
359 goto finish;
360
361 roc->started = true;
362 ieee80211_queue_delayed_work(&local->hw, &roc->work,
363 msecs_to_jiffies(roc->duration));
364 } else {
365 /* finish this ROC */
366 finish:
367 list_del(&roc->list);
368 ieee80211_roc_notify_destroy(roc);
369
370 if (roc->started) {
371 drv_flush(local, false);
372
373 local->tmp_channel = NULL;
374 ieee80211_hw_config(local, 0);
375
376 ieee80211_offchannel_return(local, true);
377 }
378
379 ieee80211_recalc_idle(local);
380
381 ieee80211_start_next_roc(local);
382 ieee80211_run_deferred_scan(local);
383 }
384
385 out_unlock:
386 mutex_unlock(&local->mtx);
387}
388
Johannes Berg21f83582010-12-18 17:20:47 +0100389static void ieee80211_hw_roc_done(struct work_struct *work)
390{
391 struct ieee80211_local *local =
392 container_of(work, struct ieee80211_local, hw_roc_done);
Johannes Berg2eb278e2012-06-05 14:28:42 +0200393 struct ieee80211_roc_work *roc;
Johannes Berg21f83582010-12-18 17:20:47 +0100394
395 mutex_lock(&local->mtx);
396
Johannes Berg2eb278e2012-06-05 14:28:42 +0200397 if (list_empty(&local->roc_list))
398 goto out_unlock;
Johannes Berg21f83582010-12-18 17:20:47 +0100399
Johannes Berg2eb278e2012-06-05 14:28:42 +0200400 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
401 list);
Johannes Berg71ecfa12012-05-31 15:09:27 +0200402
Johannes Berg2eb278e2012-06-05 14:28:42 +0200403 if (!roc->started)
404 goto out_unlock;
Johannes Berg71ecfa12012-05-31 15:09:27 +0200405
Johannes Berg2eb278e2012-06-05 14:28:42 +0200406 list_del(&roc->list);
Johannes Berg71ecfa12012-05-31 15:09:27 +0200407
Johannes Berg2eb278e2012-06-05 14:28:42 +0200408 ieee80211_roc_notify_destroy(roc);
Johannes Berg71ecfa12012-05-31 15:09:27 +0200409
Johannes Berg2eb278e2012-06-05 14:28:42 +0200410 /* if there's another roc, start it now */
411 ieee80211_start_next_roc(local);
Johannes Berg21f83582010-12-18 17:20:47 +0100412
Johannes Berg2eb278e2012-06-05 14:28:42 +0200413 /* or scan maybe */
414 ieee80211_run_deferred_scan(local);
Johannes Berg21f83582010-12-18 17:20:47 +0100415
Johannes Berg2eb278e2012-06-05 14:28:42 +0200416 out_unlock:
Johannes Berg21f83582010-12-18 17:20:47 +0100417 mutex_unlock(&local->mtx);
418}
419
420void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
421{
422 struct ieee80211_local *local = hw_to_local(hw);
423
424 trace_api_remain_on_channel_expired(local);
425
426 ieee80211_queue_work(hw, &local->hw_roc_done);
427}
428EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
429
Johannes Berg2eb278e2012-06-05 14:28:42 +0200430void ieee80211_roc_setup(struct ieee80211_local *local)
Johannes Berg21f83582010-12-18 17:20:47 +0100431{
432 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
433 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
Johannes Berg2eb278e2012-06-05 14:28:42 +0200434 INIT_LIST_HEAD(&local->roc_list);
435}
436
437void ieee80211_roc_purge(struct ieee80211_sub_if_data *sdata)
438{
439 struct ieee80211_local *local = sdata->local;
440 struct ieee80211_roc_work *roc, *tmp;
441 LIST_HEAD(tmp_list);
442
443 mutex_lock(&local->mtx);
444 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
445 if (roc->sdata != sdata)
446 continue;
447
448 if (roc->started && local->ops->remain_on_channel) {
449 /* can race, so ignore return value */
450 drv_cancel_remain_on_channel(local);
451 }
452
453 list_move_tail(&roc->list, &tmp_list);
454 roc->abort = true;
455 }
456
457 ieee80211_start_next_roc(local);
458 ieee80211_run_deferred_scan(local);
459 mutex_unlock(&local->mtx);
460
461 list_for_each_entry_safe(roc, tmp, &tmp_list, list) {
462 if (local->ops->remain_on_channel) {
463 list_del(&roc->list);
464 ieee80211_roc_notify_destroy(roc);
465 } else {
466 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
467
468 /* work will clean up etc */
469 flush_delayed_work(&roc->work);
470 }
471 }
472
473 WARN_ON_ONCE(!list_empty(&tmp_list));
Johannes Berg21f83582010-12-18 17:20:47 +0100474}