2 * Off-channel operation helpers
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>
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.
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"
22 * 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.
28 static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata,
31 struct ieee80211_local *local = sdata->local;
32 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
34 local->offchannel_ps_enabled = false;
36 /* FIXME: what to do when local->pspolling is true? */
38 del_timer_sync(&local->dynamic_ps_timer);
39 del_timer_sync(&ifmgd->bcn_mon_timer);
40 del_timer_sync(&ifmgd->conn_mon_timer);
42 cancel_work_sync(&local->dynamic_ps_enable_work);
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);
50 if (tell_ap && (!local->offchannel_ps_enabled ||
51 !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)))
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
62 ieee80211_send_nullfunc(local, sdata, 1);
65 /* inform AP that we are awake again, unless power save is enabled */
66 static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
68 struct ieee80211_local *local = sdata->local;
71 ieee80211_send_nullfunc(local, sdata, 0);
72 else if (local->offchannel_ps_enabled) {
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.
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
86 /* TODO: Only set hardware if CONF_PS changed?
87 * TODO: Should we set offchannel_ps_enabled to false?
89 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) {
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.
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));
103 ieee80211_sta_reset_beacon_monitor(sdata);
104 ieee80211_sta_reset_conn_monitor(sdata);
107 void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
108 bool offchannel_ps_enable)
110 struct ieee80211_sub_if_data *sdata;
113 * notify the AP about us leaving the channel and stop all
116 mutex_lock(&local->iflist_mtx);
117 list_for_each_entry(sdata, &local->interfaces, list) {
118 if (!ieee80211_sdata_running(sdata))
121 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
122 set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
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);
131 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
132 netif_tx_stop_all_queues(sdata->dev);
133 if (offchannel_ps_enable &&
134 (sdata->vif.type == NL80211_IFTYPE_STATION) &&
135 sdata->u.mgd.associated)
136 ieee80211_offchannel_ps_enable(sdata, true);
139 mutex_unlock(&local->iflist_mtx);
142 void ieee80211_offchannel_return(struct ieee80211_local *local,
143 bool offchannel_ps_disable)
145 struct ieee80211_sub_if_data *sdata;
147 mutex_lock(&local->iflist_mtx);
148 list_for_each_entry(sdata, &local->interfaces, list) {
149 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
150 clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
152 if (!ieee80211_sdata_running(sdata))
155 /* Tell AP we're back */
156 if (offchannel_ps_disable &&
157 sdata->vif.type == NL80211_IFTYPE_STATION) {
158 if (sdata->u.mgd.associated)
159 ieee80211_offchannel_ps_disable(sdata);
162 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
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.
173 netif_tx_wake_all_queues(sdata->dev);
176 if (sdata->vif.type == NL80211_IFTYPE_AP ||
177 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
178 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
179 ieee80211_bss_info_change_notify(
180 sdata, BSS_CHANGED_BEACON_ENABLED);
182 mutex_unlock(&local->iflist_mtx);
185 void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc)
190 if (roc->mgmt_tx_cookie) {
191 if (!WARN_ON(!roc->frame)) {
192 ieee80211_tx_skb(roc->sdata, roc->frame);
196 cfg80211_ready_on_channel(roc->sdata->dev, (unsigned long)roc,
197 roc->chan, roc->chan_type,
198 roc->req_duration, GFP_KERNEL);
201 roc->notified = true;
204 static void ieee80211_hw_roc_start(struct work_struct *work)
206 struct ieee80211_local *local =
207 container_of(work, struct ieee80211_local, hw_roc_start);
208 struct ieee80211_roc_work *roc, *dep, *tmp;
210 mutex_lock(&local->mtx);
212 if (list_empty(&local->roc_list))
215 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
221 roc->hw_begun = true;
222 roc->hw_start_time = local->hw_roc_start_time;
224 ieee80211_handle_roc_started(roc);
225 list_for_each_entry_safe(dep, tmp, &roc->dependents, list) {
226 ieee80211_handle_roc_started(dep);
228 if (dep->duration > roc->duration) {
229 u32 dur = dep->duration;
230 dep->duration = dur - roc->duration;
232 list_del(&dep->list);
233 list_add(&dep->list, &roc->list);
237 mutex_unlock(&local->mtx);
240 void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
242 struct ieee80211_local *local = hw_to_local(hw);
244 local->hw_roc_start_time = jiffies;
246 trace_api_ready_on_channel(local);
248 ieee80211_queue_work(hw, &local->hw_roc_start);
250 EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
252 void ieee80211_start_next_roc(struct ieee80211_local *local)
254 struct ieee80211_roc_work *roc;
256 lockdep_assert_held(&local->mtx);
258 if (list_empty(&local->roc_list)) {
259 ieee80211_run_deferred_scan(local);
263 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
266 if (local->ops->remain_on_channel) {
267 int ret, duration = roc->duration;
269 /* XXX: duplicated, see ieee80211_start_roc_work() */
273 ret = drv_remain_on_channel(local, roc->chan,
280 wiphy_warn(local->hw.wiphy,
281 "failed to start next HW ROC (%d)\n", ret);
283 * queue the work struct again to avoid recursion
284 * when multiple failures occur
286 ieee80211_remain_on_channel_expired(&local->hw);
290 ieee80211_queue_delayed_work(&local->hw, &roc->work,
291 round_jiffies_relative(HZ/2));
295 void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
297 struct ieee80211_roc_work *dep, *tmp;
299 /* was never transmitted */
301 cfg80211_mgmt_tx_status(roc->sdata->dev,
302 (unsigned long)roc->frame,
303 roc->frame->data, roc->frame->len,
305 kfree_skb(roc->frame);
308 if (!roc->mgmt_tx_cookie)
309 cfg80211_remain_on_channel_expired(roc->sdata->dev,
311 roc->chan, roc->chan_type,
314 list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
315 ieee80211_roc_notify_destroy(dep);
320 void ieee80211_sw_roc_work(struct work_struct *work)
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;
327 mutex_lock(&local->mtx);
332 if (WARN_ON(list_empty(&local->roc_list)))
335 if (WARN_ON(roc != list_first_entry(&local->roc_list,
336 struct ieee80211_roc_work,
341 struct ieee80211_roc_work *dep;
345 /* switch channel etc */
346 ieee80211_recalc_idle(local);
348 local->tmp_channel = roc->chan;
349 local->tmp_channel_type = roc->chan_type;
350 ieee80211_hw_config(local, 0);
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);
357 /* if it was pure TX, just finish right away */
362 ieee80211_queue_delayed_work(&local->hw, &roc->work,
363 msecs_to_jiffies(roc->duration));
365 /* finish this ROC */
367 list_del(&roc->list);
368 ieee80211_roc_notify_destroy(roc);
371 drv_flush(local, false);
373 local->tmp_channel = NULL;
374 ieee80211_hw_config(local, 0);
376 ieee80211_offchannel_return(local, true);
379 ieee80211_recalc_idle(local);
381 ieee80211_start_next_roc(local);
382 ieee80211_run_deferred_scan(local);
386 mutex_unlock(&local->mtx);
389 static void ieee80211_hw_roc_done(struct work_struct *work)
391 struct ieee80211_local *local =
392 container_of(work, struct ieee80211_local, hw_roc_done);
393 struct ieee80211_roc_work *roc;
395 mutex_lock(&local->mtx);
397 if (list_empty(&local->roc_list))
400 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
406 list_del(&roc->list);
408 ieee80211_roc_notify_destroy(roc);
410 /* if there's another roc, start it now */
411 ieee80211_start_next_roc(local);
414 ieee80211_run_deferred_scan(local);
417 mutex_unlock(&local->mtx);
420 void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
422 struct ieee80211_local *local = hw_to_local(hw);
424 trace_api_remain_on_channel_expired(local);
426 ieee80211_queue_work(hw, &local->hw_roc_done);
428 EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
430 void ieee80211_roc_setup(struct ieee80211_local *local)
432 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
433 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
434 INIT_LIST_HEAD(&local->roc_list);
437 void ieee80211_roc_purge(struct ieee80211_sub_if_data *sdata)
439 struct ieee80211_local *local = sdata->local;
440 struct ieee80211_roc_work *roc, *tmp;
443 mutex_lock(&local->mtx);
444 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
445 if (roc->sdata != sdata)
448 if (roc->started && local->ops->remain_on_channel) {
449 /* can race, so ignore return value */
450 drv_cancel_remain_on_channel(local);
453 list_move_tail(&roc->list, &tmp_list);
457 ieee80211_start_next_roc(local);
458 ieee80211_run_deferred_scan(local);
459 mutex_unlock(&local->mtx);
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);
466 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
468 /* work will clean up etc */
469 flush_delayed_work(&roc->work);
473 WARN_ON_ONCE(!list_empty(&tmp_list));