net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.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
20 /*
21  * inform AP that we will go to sleep so that it will buffer the frames
22  * while we scan
23  */
24 static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
25 {
26         struct ieee80211_local *local = sdata->local;
27         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
28
29         local->offchannel_ps_enabled = false;
30
31         /* FIXME: what to do when local->pspolling is true? */
32
33         del_timer_sync(&local->dynamic_ps_timer);
34         del_timer_sync(&ifmgd->bcn_mon_timer);
35         del_timer_sync(&ifmgd->conn_mon_timer);
36
37         cancel_work_sync(&local->dynamic_ps_enable_work);
38
39         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
40                 local->offchannel_ps_enabled = true;
41                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
42                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
43         }
44
45         if (!(local->offchannel_ps_enabled) ||
46             !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
47                 /*
48                  * If power save was enabled, no need to send a nullfunc
49                  * frame because AP knows that we are sleeping. But if the
50                  * hardware is creating the nullfunc frame for power save
51                  * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
52                  * enabled) and power save was enabled, the firmware just
53                  * sent a null frame with power save disabled. So we need
54                  * to send a new nullfunc frame to inform the AP that we
55                  * are again sleeping.
56                  */
57                 ieee80211_send_nullfunc(local, sdata, 1);
58 }
59
60 /* inform AP that we are awake again, unless power save is enabled */
61 static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
62 {
63         struct ieee80211_local *local = sdata->local;
64
65         if (!local->ps_sdata)
66                 ieee80211_send_nullfunc(local, sdata, 0);
67         else if (local->offchannel_ps_enabled) {
68                 /*
69                  * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
70                  * will send a nullfunc frame with the powersave bit set
71                  * even though the AP already knows that we are sleeping.
72                  * This could be avoided by sending a null frame with power
73                  * save bit disabled before enabling the power save, but
74                  * this doesn't gain anything.
75                  *
76                  * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
77                  * to send a nullfunc frame because AP already knows that
78                  * we are sleeping, let's just enable power save mode in
79                  * hardware.
80                  */
81                 local->hw.conf.flags |= IEEE80211_CONF_PS;
82                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
83         } else if (local->hw.conf.dynamic_ps_timeout > 0) {
84                 /*
85                  * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
86                  * had been running before leaving the operating channel,
87                  * restart the timer now and send a nullfunc frame to inform
88                  * the AP that we are awake.
89                  */
90                 ieee80211_send_nullfunc(local, sdata, 0);
91                 mod_timer(&local->dynamic_ps_timer, jiffies +
92                           msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
93         }
94
95         ieee80211_sta_reset_beacon_monitor(sdata);
96         ieee80211_sta_reset_conn_monitor(sdata);
97 }
98
99 void ieee80211_offchannel_stop_beaconing(struct ieee80211_local *local)
100 {
101         struct ieee80211_sub_if_data *sdata;
102
103         mutex_lock(&local->iflist_mtx);
104         list_for_each_entry(sdata, &local->interfaces, list) {
105                 if (!ieee80211_sdata_running(sdata))
106                         continue;
107
108                 /* disable beaconing */
109                 if (sdata->vif.type == NL80211_IFTYPE_AP ||
110                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
111                     sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
112                         ieee80211_bss_info_change_notify(
113                                 sdata, BSS_CHANGED_BEACON_ENABLED);
114
115                 /*
116                  * only handle non-STA interfaces here, STA interfaces
117                  * are handled in ieee80211_offchannel_stop_station(),
118                  * e.g., from the background scan state machine.
119                  *
120                  * In addition, do not stop monitor interface to allow it to be
121                  * used from user space controlled off-channel operations.
122                  */
123                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
124                     sdata->vif.type != NL80211_IFTYPE_MONITOR) {
125                         set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
126                         netif_tx_stop_all_queues(sdata->dev);
127                 }
128         }
129         mutex_unlock(&local->iflist_mtx);
130 }
131
132 void ieee80211_offchannel_stop_station(struct ieee80211_local *local)
133 {
134         struct ieee80211_sub_if_data *sdata;
135
136         /*
137          * notify the AP about us leaving the channel and stop all STA interfaces
138          */
139         mutex_lock(&local->iflist_mtx);
140         list_for_each_entry(sdata, &local->interfaces, list) {
141                 if (!ieee80211_sdata_running(sdata))
142                         continue;
143
144                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
145                         set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
146                         netif_tx_stop_all_queues(sdata->dev);
147                         if (sdata->u.mgd.associated)
148                                 ieee80211_offchannel_ps_enable(sdata);
149                 }
150         }
151         mutex_unlock(&local->iflist_mtx);
152 }
153
154 void ieee80211_offchannel_return(struct ieee80211_local *local,
155                                  bool enable_beaconing)
156 {
157         struct ieee80211_sub_if_data *sdata;
158
159         mutex_lock(&local->iflist_mtx);
160         list_for_each_entry(sdata, &local->interfaces, list) {
161                 if (!ieee80211_sdata_running(sdata))
162                         continue;
163
164                 /* Tell AP we're back */
165                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
166                         if (sdata->u.mgd.associated)
167                                 ieee80211_offchannel_ps_disable(sdata);
168                 }
169
170                 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
171                         clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
172                         /*
173                          * This may wake up queues even though the driver
174                          * currently has them stopped. This is not very
175                          * likely, since the driver won't have gotten any
176                          * (or hardly any) new packets while we weren't
177                          * on the right channel, and even if it happens
178                          * it will at most lead to queueing up one more
179                          * packet per queue in mac80211 rather than on
180                          * the interface qdisc.
181                          */
182                         netif_tx_wake_all_queues(sdata->dev);
183                 }
184
185                 /* re-enable beaconing */
186                 if (enable_beaconing &&
187                     (sdata->vif.type == NL80211_IFTYPE_AP ||
188                      sdata->vif.type == NL80211_IFTYPE_ADHOC ||
189                      sdata->vif.type == NL80211_IFTYPE_MESH_POINT))
190                         ieee80211_bss_info_change_notify(
191                                 sdata, BSS_CHANGED_BEACON_ENABLED);
192         }
193         mutex_unlock(&local->iflist_mtx);
194 }
195
196 static void ieee80211_hw_roc_start(struct work_struct *work)
197 {
198         struct ieee80211_local *local =
199                 container_of(work, struct ieee80211_local, hw_roc_start);
200         struct ieee80211_sub_if_data *sdata;
201
202         mutex_lock(&local->mtx);
203
204         if (!local->hw_roc_channel) {
205                 mutex_unlock(&local->mtx);
206                 return;
207         }
208
209         ieee80211_recalc_idle(local);
210
211         if (local->hw_roc_skb) {
212                 sdata = IEEE80211_DEV_TO_SUB_IF(local->hw_roc_dev);
213                 ieee80211_tx_skb(sdata, local->hw_roc_skb);
214                 local->hw_roc_skb = NULL;
215         } else {
216                 cfg80211_ready_on_channel(local->hw_roc_dev,
217                                           local->hw_roc_cookie,
218                                           local->hw_roc_channel,
219                                           local->hw_roc_channel_type,
220                                           local->hw_roc_duration,
221                                           GFP_KERNEL);
222         }
223
224         mutex_unlock(&local->mtx);
225 }
226
227 void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
228 {
229         struct ieee80211_local *local = hw_to_local(hw);
230
231         trace_api_ready_on_channel(local);
232
233         ieee80211_queue_work(hw, &local->hw_roc_start);
234 }
235 EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
236
237 static void ieee80211_hw_roc_done(struct work_struct *work)
238 {
239         struct ieee80211_local *local =
240                 container_of(work, struct ieee80211_local, hw_roc_done);
241
242         mutex_lock(&local->mtx);
243
244         if (!local->hw_roc_channel) {
245                 mutex_unlock(&local->mtx);
246                 return;
247         }
248
249         /* was never transmitted */
250         if (local->hw_roc_skb) {
251                 u64 cookie;
252
253                 cookie = local->hw_roc_cookie ^ 2;
254
255                 cfg80211_mgmt_tx_status(local->hw_roc_dev, cookie,
256                                         local->hw_roc_skb->data,
257                                         local->hw_roc_skb->len, false,
258                                         GFP_KERNEL);
259
260                 kfree_skb(local->hw_roc_skb);
261                 local->hw_roc_skb = NULL;
262                 local->hw_roc_skb_for_status = NULL;
263         }
264
265         if (!local->hw_roc_for_tx)
266                 cfg80211_remain_on_channel_expired(local->hw_roc_dev,
267                                                    local->hw_roc_cookie,
268                                                    local->hw_roc_channel,
269                                                    local->hw_roc_channel_type,
270                                                    GFP_KERNEL);
271
272         local->hw_roc_channel = NULL;
273         local->hw_roc_cookie = 0;
274
275         ieee80211_recalc_idle(local);
276
277         mutex_unlock(&local->mtx);
278 }
279
280 void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
281 {
282         struct ieee80211_local *local = hw_to_local(hw);
283
284         trace_api_remain_on_channel_expired(local);
285
286         ieee80211_queue_work(hw, &local->hw_roc_done);
287 }
288 EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
289
290 void ieee80211_hw_roc_setup(struct ieee80211_local *local)
291 {
292         INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
293         INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
294 }