Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / net / mac80211 / agg-rx.c
1 /*
2  * HT handling
3  *
4  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5  * Copyright 2002-2005, Instant802 Networks, Inc.
6  * Copyright 2005-2006, Devicescape Software, Inc.
7  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
8  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9  * Copyright 2007-2008, Intel Corporation
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
16 #include <linux/ieee80211.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "driver-ops.h"
20
21 static void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
22                                             u16 initiator, u16 reason,
23                                             bool from_timer)
24 {
25         struct ieee80211_local *local = sta->local;
26         struct tid_ampdu_rx *tid_rx;
27         int i;
28
29         spin_lock_bh(&sta->lock);
30
31         /* check if TID is in operational state */
32         if (!sta->ampdu_mlme.tid_active_rx[tid]) {
33                 spin_unlock_bh(&sta->lock);
34                 return;
35         }
36
37         sta->ampdu_mlme.tid_active_rx[tid] = false;
38
39         tid_rx = sta->ampdu_mlme.tid_rx[tid];
40
41 #ifdef CONFIG_MAC80211_HT_DEBUG
42         printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
43                sta->sta.addr, tid);
44 #endif /* CONFIG_MAC80211_HT_DEBUG */
45
46         if (drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_STOP,
47                              &sta->sta, tid, NULL))
48                 printk(KERN_DEBUG "HW problem - can not stop rx "
49                                 "aggregation for tid %d\n", tid);
50
51         /* check if this is a self generated aggregation halt */
52         if (initiator == WLAN_BACK_RECIPIENT)
53                 ieee80211_send_delba(sta->sdata, sta->sta.addr,
54                                      tid, 0, reason);
55
56         /* free the reordering buffer */
57         for (i = 0; i < tid_rx->buf_size; i++) {
58                 if (tid_rx->reorder_buf[i]) {
59                         /* release the reordered frames */
60                         dev_kfree_skb(tid_rx->reorder_buf[i]);
61                         tid_rx->stored_mpdu_num--;
62                         tid_rx->reorder_buf[i] = NULL;
63                 }
64         }
65
66         /* free resources */
67         kfree(tid_rx->reorder_buf);
68         kfree(tid_rx->reorder_time);
69         sta->ampdu_mlme.tid_rx[tid] = NULL;
70
71         spin_unlock_bh(&sta->lock);
72
73         if (!from_timer)
74                 del_timer_sync(&tid_rx->session_timer);
75         kfree(tid_rx);
76 }
77
78 void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
79                                     u16 initiator, u16 reason)
80 {
81         ___ieee80211_stop_rx_ba_session(sta, tid, initiator, reason, false);
82 }
83
84 /*
85  * After accepting the AddBA Request we activated a timer,
86  * resetting it after each frame that arrives from the originator.
87  */
88 static void sta_rx_agg_session_timer_expired(unsigned long data)
89 {
90         /* not an elegant detour, but there is no choice as the timer passes
91          * only one argument, and various sta_info are needed here, so init
92          * flow in sta_info_create gives the TID as data, while the timer_to_id
93          * array gives the sta through container_of */
94         u8 *ptid = (u8 *)data;
95         u8 *timer_to_id = ptid - *ptid;
96         struct sta_info *sta = container_of(timer_to_id, struct sta_info,
97                                          timer_to_tid[0]);
98
99 #ifdef CONFIG_MAC80211_HT_DEBUG
100         printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
101 #endif
102         ___ieee80211_stop_rx_ba_session(sta, *ptid, WLAN_BACK_RECIPIENT,
103                                         WLAN_REASON_QSTA_TIMEOUT, true);
104 }
105
106 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
107                                       u8 dialog_token, u16 status, u16 policy,
108                                       u16 buf_size, u16 timeout)
109 {
110         struct ieee80211_local *local = sdata->local;
111         struct sk_buff *skb;
112         struct ieee80211_mgmt *mgmt;
113         u16 capab;
114
115         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
116
117         if (!skb) {
118                 printk(KERN_DEBUG "%s: failed to allocate buffer "
119                        "for addba resp frame\n", sdata->name);
120                 return;
121         }
122
123         skb_reserve(skb, local->hw.extra_tx_headroom);
124         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
125         memset(mgmt, 0, 24);
126         memcpy(mgmt->da, da, ETH_ALEN);
127         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
128         if (sdata->vif.type == NL80211_IFTYPE_AP ||
129             sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
131         else if (sdata->vif.type == NL80211_IFTYPE_STATION)
132                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
133
134         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
135                                           IEEE80211_STYPE_ACTION);
136
137         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
138         mgmt->u.action.category = WLAN_CATEGORY_BACK;
139         mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
140         mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
141
142         capab = (u16)(policy << 1);     /* bit 1 aggregation policy */
143         capab |= (u16)(tid << 2);       /* bit 5:2 TID number */
144         capab |= (u16)(buf_size << 6);  /* bit 15:6 max size of aggregation */
145
146         mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
147         mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
148         mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
149
150         ieee80211_tx_skb(sdata, skb);
151 }
152
153 void ieee80211_process_addba_request(struct ieee80211_local *local,
154                                      struct sta_info *sta,
155                                      struct ieee80211_mgmt *mgmt,
156                                      size_t len)
157 {
158         struct ieee80211_hw *hw = &local->hw;
159         struct ieee80211_conf *conf = &hw->conf;
160         struct tid_ampdu_rx *tid_agg_rx;
161         u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
162         u8 dialog_token;
163         int ret = -EOPNOTSUPP;
164
165         /* extract session parameters from addba request frame */
166         dialog_token = mgmt->u.action.u.addba_req.dialog_token;
167         timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
168         start_seq_num =
169                 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
170
171         capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
172         ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
173         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
174         buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
175
176         status = WLAN_STATUS_REQUEST_DECLINED;
177
178         if (test_sta_flags(sta, WLAN_STA_BLOCK_BA)) {
179 #ifdef CONFIG_MAC80211_HT_DEBUG
180                 printk(KERN_DEBUG "Suspend in progress. "
181                        "Denying ADDBA request\n");
182 #endif
183                 goto end_no_lock;
184         }
185
186         /* sanity check for incoming parameters:
187          * check if configuration can support the BA policy
188          * and if buffer size does not exceeds max value */
189         /* XXX: check own ht delayed BA capability?? */
190         if (((ba_policy != 1) &&
191              (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
192             (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
193                 status = WLAN_STATUS_INVALID_QOS_PARAM;
194 #ifdef CONFIG_MAC80211_HT_DEBUG
195                 if (net_ratelimit())
196                         printk(KERN_DEBUG "AddBA Req with bad params from "
197                                 "%pM on tid %u. policy %d, buffer size %d\n",
198                                 mgmt->sa, tid, ba_policy,
199                                 buf_size);
200 #endif /* CONFIG_MAC80211_HT_DEBUG */
201                 goto end_no_lock;
202         }
203         /* determine default buffer size */
204         if (buf_size == 0) {
205                 struct ieee80211_supported_band *sband;
206
207                 sband = local->hw.wiphy->bands[conf->channel->band];
208                 buf_size = IEEE80211_MIN_AMPDU_BUF;
209                 buf_size = buf_size << sband->ht_cap.ampdu_factor;
210         }
211
212
213         /* examine state machine */
214         spin_lock_bh(&sta->lock);
215
216         if (sta->ampdu_mlme.tid_active_rx[tid]) {
217 #ifdef CONFIG_MAC80211_HT_DEBUG
218                 if (net_ratelimit())
219                         printk(KERN_DEBUG "unexpected AddBA Req from "
220                                 "%pM on tid %u\n",
221                                 mgmt->sa, tid);
222 #endif /* CONFIG_MAC80211_HT_DEBUG */
223                 goto end;
224         }
225
226         /* prepare A-MPDU MLME for Rx aggregation */
227         sta->ampdu_mlme.tid_rx[tid] =
228                         kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
229         if (!sta->ampdu_mlme.tid_rx[tid]) {
230 #ifdef CONFIG_MAC80211_HT_DEBUG
231                 if (net_ratelimit())
232                         printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
233                                         tid);
234 #endif
235                 goto end;
236         }
237         /* rx timer */
238         sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
239                                 sta_rx_agg_session_timer_expired;
240         sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
241                                 (unsigned long)&sta->timer_to_tid[tid];
242         init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
243
244         tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
245
246         /* prepare reordering buffer */
247         tid_agg_rx->reorder_buf =
248                 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
249         tid_agg_rx->reorder_time =
250                 kcalloc(buf_size, sizeof(unsigned long), GFP_ATOMIC);
251         if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
252 #ifdef CONFIG_MAC80211_HT_DEBUG
253                 if (net_ratelimit())
254                         printk(KERN_ERR "can not allocate reordering buffer "
255                                "to tid %d\n", tid);
256 #endif
257                 kfree(tid_agg_rx->reorder_buf);
258                 kfree(tid_agg_rx->reorder_time);
259                 kfree(sta->ampdu_mlme.tid_rx[tid]);
260                 sta->ampdu_mlme.tid_rx[tid] = NULL;
261                 goto end;
262         }
263
264         ret = drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_START,
265                                &sta->sta, tid, &start_seq_num);
266 #ifdef CONFIG_MAC80211_HT_DEBUG
267         printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
268 #endif /* CONFIG_MAC80211_HT_DEBUG */
269
270         if (ret) {
271                 kfree(tid_agg_rx->reorder_buf);
272                 kfree(tid_agg_rx);
273                 sta->ampdu_mlme.tid_rx[tid] = NULL;
274                 goto end;
275         }
276
277         /* change state and send addba resp */
278         sta->ampdu_mlme.tid_active_rx[tid] = true;
279         tid_agg_rx->dialog_token = dialog_token;
280         tid_agg_rx->ssn = start_seq_num;
281         tid_agg_rx->head_seq_num = start_seq_num;
282         tid_agg_rx->buf_size = buf_size;
283         tid_agg_rx->timeout = timeout;
284         tid_agg_rx->stored_mpdu_num = 0;
285         status = WLAN_STATUS_SUCCESS;
286 end:
287         spin_unlock_bh(&sta->lock);
288
289 end_no_lock:
290         ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
291                                   dialog_token, status, 1, buf_size, timeout);
292 }