]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/mac80211/offchannel.c
Merge branch 'wl12xx-next' into for-linville
[karo-tx-linux.git] / net / mac80211 / offchannel.c
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 /*
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.
27  */
28 static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata,
29                                            bool tell_ap)
30 {
31         struct ieee80211_local *local = sdata->local;
32         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
33
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);
39         del_timer_sync(&ifmgd->bcn_mon_timer);
40         del_timer_sync(&ifmgd->conn_mon_timer);
41
42         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
50         if (tell_ap && (!local->offchannel_ps_enabled ||
51                         !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)))
52                 /*
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 */
66 static 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                  */
86                 /* TODO:  Only set hardware if CONF_PS changed?
87                  * TODO:  Should we set offchannel_ps_enabled to false?
88                  */
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) {
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         }
102
103         ieee80211_sta_reset_beacon_monitor(sdata);
104         ieee80211_sta_reset_conn_monitor(sdata);
105 }
106
107 void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
108                                     bool offchannel_ps_enable)
109 {
110         struct ieee80211_sub_if_data *sdata;
111
112         /*
113          * notify the AP about us leaving the channel and stop all
114          * STA interfaces.
115          */
116         mutex_lock(&local->iflist_mtx);
117         list_for_each_entry(sdata, &local->interfaces, list) {
118                 if (!ieee80211_sdata_running(sdata))
119                         continue;
120
121                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
122                         set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
123
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) {
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);
137                 }
138         }
139         mutex_unlock(&local->iflist_mtx);
140 }
141
142 void ieee80211_offchannel_return(struct ieee80211_local *local,
143                                  bool offchannel_ps_disable)
144 {
145         struct ieee80211_sub_if_data *sdata;
146
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);
151
152                 if (!ieee80211_sdata_running(sdata))
153                         continue;
154
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);
160                 }
161
162                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
163                         /*
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                          */
173                         netif_tx_wake_all_queues(sdata->dev);
174                 }
175
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);
181         }
182         mutex_unlock(&local->iflist_mtx);
183 }
184
185 void 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
204 static 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);
208         struct ieee80211_roc_work *roc, *dep, *tmp;
209
210         mutex_lock(&local->mtx);
211
212         if (list_empty(&local->roc_list))
213                 goto out_unlock;
214
215         roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
216                                list);
217
218         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:
237         mutex_unlock(&local->mtx);
238 }
239
240 void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
241 {
242         struct ieee80211_local *local = hw_to_local(hw);
243
244         local->hw_roc_start_time = jiffies;
245
246         trace_api_ready_on_channel(local);
247
248         ieee80211_queue_work(hw, &local->hw_roc_start);
249 }
250 EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
251
252 void 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
295 void 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
320 void 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
389 static 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);
393         struct ieee80211_roc_work *roc;
394
395         mutex_lock(&local->mtx);
396
397         if (list_empty(&local->roc_list))
398                 goto out_unlock;
399
400         roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
401                                list);
402
403         if (!roc->started)
404                 goto out_unlock;
405
406         list_del(&roc->list);
407
408         ieee80211_roc_notify_destroy(roc);
409
410         /* if there's another roc, start it now */
411         ieee80211_start_next_roc(local);
412
413         /* or scan maybe */
414         ieee80211_run_deferred_scan(local);
415
416  out_unlock:
417         mutex_unlock(&local->mtx);
418 }
419
420 void 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 }
428 EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
429
430 void ieee80211_roc_setup(struct ieee80211_local *local)
431 {
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);
435 }
436
437 void 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));
474 }