blob: a978e666ed6f06a79fa4b7529feec540a7ab2908 [file] [log] [blame]
Johannes Bergb8695a82009-02-10 21:25:46 +01001/*
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"
Johannes Berg24487982009-04-23 18:52:52 +020019#include "driver-ops.h"
Johannes Bergb8695a82009-02-10 21:25:46 +010020
Johannes Berg849b7962009-02-10 21:25:54 +010021void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
22 u16 initiator, u16 reason)
Johannes Bergb8695a82009-02-10 21:25:46 +010023{
Johannes Bergd75636e2009-02-10 21:25:53 +010024 struct ieee80211_local *local = sta->local;
Johannes Bergd75636e2009-02-10 21:25:53 +010025 int i;
Johannes Bergb8695a82009-02-10 21:25:46 +010026
27 /* check if TID is in operational state */
28 spin_lock_bh(&sta->lock);
Johannes Bergd75636e2009-02-10 21:25:53 +010029 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL) {
Johannes Bergb8695a82009-02-10 21:25:46 +010030 spin_unlock_bh(&sta->lock);
Johannes Bergb8695a82009-02-10 21:25:46 +010031 return;
32 }
Johannes Bergd75636e2009-02-10 21:25:53 +010033
Johannes Bergb8695a82009-02-10 21:25:46 +010034 sta->ampdu_mlme.tid_state_rx[tid] =
35 HT_AGG_STATE_REQ_STOP_BA_MSK |
36 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
37 spin_unlock_bh(&sta->lock);
38
Johannes Bergb8695a82009-02-10 21:25:46 +010039#ifdef CONFIG_MAC80211_HT_DEBUG
40 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
Johannes Bergd75636e2009-02-10 21:25:53 +010041 sta->sta.addr, tid);
Johannes Bergb8695a82009-02-10 21:25:46 +010042#endif /* CONFIG_MAC80211_HT_DEBUG */
43
Johannes Berg12375ef2009-11-25 20:30:31 +010044 if (drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_STOP,
Johannes Berg24487982009-04-23 18:52:52 +020045 &sta->sta, tid, NULL))
Johannes Bergb8695a82009-02-10 21:25:46 +010046 printk(KERN_DEBUG "HW problem - can not stop rx "
47 "aggregation for tid %d\n", tid);
48
49 /* shutdown timer has not expired */
50 if (initiator != WLAN_BACK_TIMER)
51 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
52
53 /* check if this is a self generated aggregation halt */
54 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
Johannes Bergd75636e2009-02-10 21:25:53 +010055 ieee80211_send_delba(sta->sdata, sta->sta.addr,
56 tid, 0, reason);
Johannes Bergb8695a82009-02-10 21:25:46 +010057
58 /* free the reordering buffer */
59 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
60 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
61 /* release the reordered frames */
62 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
63 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
64 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
65 }
66 }
Johannes Berg55687e32009-02-10 21:25:51 +010067
68 spin_lock_bh(&sta->lock);
Johannes Bergb8695a82009-02-10 21:25:46 +010069 /* free resources */
70 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
Jouni Malinen4d050f12009-05-05 20:35:14 +030071 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_time);
Johannes Berg55687e32009-02-10 21:25:51 +010072
73 if (!sta->ampdu_mlme.tid_rx[tid]->shutdown) {
74 kfree(sta->ampdu_mlme.tid_rx[tid]);
75 sta->ampdu_mlme.tid_rx[tid] = NULL;
76 }
77
Johannes Bergb8695a82009-02-10 21:25:46 +010078 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Johannes Berg55687e32009-02-10 21:25:51 +010079 spin_unlock_bh(&sta->lock);
Johannes Bergd75636e2009-02-10 21:25:53 +010080}
81
82void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
83 u16 initiator, u16 reason)
84{
Johannes Bergd75636e2009-02-10 21:25:53 +010085 struct sta_info *sta;
86
Johannes Bergd75636e2009-02-10 21:25:53 +010087 rcu_read_lock();
88
Johannes Bergabe60632009-11-25 17:46:18 +010089 sta = sta_info_get(sdata, ra);
Johannes Bergd75636e2009-02-10 21:25:53 +010090 if (!sta) {
91 rcu_read_unlock();
92 return;
93 }
94
Johannes Berg849b7962009-02-10 21:25:54 +010095 __ieee80211_stop_rx_ba_session(sta, tid, initiator, reason);
Johannes Bergb8695a82009-02-10 21:25:46 +010096
97 rcu_read_unlock();
98}
99
100/*
101 * After accepting the AddBA Request we activated a timer,
102 * resetting it after each frame that arrives from the originator.
103 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
104 */
105static void sta_rx_agg_session_timer_expired(unsigned long data)
106{
107 /* not an elegant detour, but there is no choice as the timer passes
108 * only one argument, and various sta_info are needed here, so init
109 * flow in sta_info_create gives the TID as data, while the timer_to_id
110 * array gives the sta through container_of */
111 u8 *ptid = (u8 *)data;
112 u8 *timer_to_id = ptid - *ptid;
113 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
114 timer_to_tid[0]);
115
116#ifdef CONFIG_MAC80211_HT_DEBUG
117 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
118#endif
119 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
120 (u16)*ptid, WLAN_BACK_TIMER,
121 WLAN_REASON_QSTA_TIMEOUT);
122}
123
124static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
125 u8 dialog_token, u16 status, u16 policy,
126 u16 buf_size, u16 timeout)
127{
Johannes Bergb8695a82009-02-10 21:25:46 +0100128 struct ieee80211_local *local = sdata->local;
129 struct sk_buff *skb;
130 struct ieee80211_mgmt *mgmt;
131 u16 capab;
132
133 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
134
135 if (!skb) {
136 printk(KERN_DEBUG "%s: failed to allocate buffer "
Johannes Berg47846c92009-11-25 17:46:19 +0100137 "for addba resp frame\n", sdata->name);
Johannes Bergb8695a82009-02-10 21:25:46 +0100138 return;
139 }
140
141 skb_reserve(skb, local->hw.extra_tx_headroom);
142 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
143 memset(mgmt, 0, 24);
144 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100145 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg8abd3f92009-02-10 21:25:47 +0100146 if (sdata->vif.type == NL80211_IFTYPE_AP ||
147 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg47846c92009-11-25 17:46:19 +0100148 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100149 else if (sdata->vif.type == NL80211_IFTYPE_STATION)
150 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
151
Johannes Bergb8695a82009-02-10 21:25:46 +0100152 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
153 IEEE80211_STYPE_ACTION);
154
155 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
156 mgmt->u.action.category = WLAN_CATEGORY_BACK;
157 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
158 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
159
160 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
161 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
162 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
163
164 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
165 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
166 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
167
Johannes Berg62ae67b2009-11-18 18:42:05 +0100168 ieee80211_tx_skb(sdata, skb);
Johannes Bergb8695a82009-02-10 21:25:46 +0100169}
170
171void ieee80211_process_addba_request(struct ieee80211_local *local,
172 struct sta_info *sta,
173 struct ieee80211_mgmt *mgmt,
174 size_t len)
175{
176 struct ieee80211_hw *hw = &local->hw;
177 struct ieee80211_conf *conf = &hw->conf;
178 struct tid_ampdu_rx *tid_agg_rx;
179 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
180 u8 dialog_token;
181 int ret = -EOPNOTSUPP;
182
183 /* extract session parameters from addba request frame */
184 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
185 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
186 start_seq_num =
187 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
188
189 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
190 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
191 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
192 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
193
194 status = WLAN_STATUS_REQUEST_DECLINED;
195
Sujith722f0692009-03-17 08:50:06 +0530196 if (test_sta_flags(sta, WLAN_STA_SUSPEND)) {
197#ifdef CONFIG_MAC80211_HT_DEBUG
198 printk(KERN_DEBUG "Suspend in progress. "
199 "Denying ADDBA request\n");
200#endif
201 goto end_no_lock;
202 }
203
Johannes Bergb8695a82009-02-10 21:25:46 +0100204 /* sanity check for incoming parameters:
205 * check if configuration can support the BA policy
206 * and if buffer size does not exceeds max value */
207 /* XXX: check own ht delayed BA capability?? */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800208 if (((ba_policy != 1) &&
209 (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
210 (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100211 status = WLAN_STATUS_INVALID_QOS_PARAM;
212#ifdef CONFIG_MAC80211_HT_DEBUG
213 if (net_ratelimit())
214 printk(KERN_DEBUG "AddBA Req with bad params from "
215 "%pM on tid %u. policy %d, buffer size %d\n",
216 mgmt->sa, tid, ba_policy,
217 buf_size);
218#endif /* CONFIG_MAC80211_HT_DEBUG */
219 goto end_no_lock;
220 }
221 /* determine default buffer size */
222 if (buf_size == 0) {
223 struct ieee80211_supported_band *sband;
224
225 sband = local->hw.wiphy->bands[conf->channel->band];
226 buf_size = IEEE80211_MIN_AMPDU_BUF;
227 buf_size = buf_size << sband->ht_cap.ampdu_factor;
228 }
229
230
231 /* examine state machine */
232 spin_lock_bh(&sta->lock);
233
234 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
235#ifdef CONFIG_MAC80211_HT_DEBUG
236 if (net_ratelimit())
237 printk(KERN_DEBUG "unexpected AddBA Req from "
238 "%pM on tid %u\n",
239 mgmt->sa, tid);
240#endif /* CONFIG_MAC80211_HT_DEBUG */
241 goto end;
242 }
243
244 /* prepare A-MPDU MLME for Rx aggregation */
245 sta->ampdu_mlme.tid_rx[tid] =
246 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
247 if (!sta->ampdu_mlme.tid_rx[tid]) {
248#ifdef CONFIG_MAC80211_HT_DEBUG
249 if (net_ratelimit())
250 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
251 tid);
252#endif
253 goto end;
254 }
255 /* rx timer */
256 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
257 sta_rx_agg_session_timer_expired;
258 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
259 (unsigned long)&sta->timer_to_tid[tid];
260 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
261
262 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
263
264 /* prepare reordering buffer */
265 tid_agg_rx->reorder_buf =
266 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
Jouni Malinen4d050f12009-05-05 20:35:14 +0300267 tid_agg_rx->reorder_time =
268 kcalloc(buf_size, sizeof(unsigned long), GFP_ATOMIC);
269 if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100270#ifdef CONFIG_MAC80211_HT_DEBUG
271 if (net_ratelimit())
272 printk(KERN_ERR "can not allocate reordering buffer "
273 "to tid %d\n", tid);
274#endif
Jouni Malinen4d050f12009-05-05 20:35:14 +0300275 kfree(tid_agg_rx->reorder_buf);
276 kfree(tid_agg_rx->reorder_time);
Johannes Bergb8695a82009-02-10 21:25:46 +0100277 kfree(sta->ampdu_mlme.tid_rx[tid]);
Jouni Malinen4d050f12009-05-05 20:35:14 +0300278 sta->ampdu_mlme.tid_rx[tid] = NULL;
Johannes Bergb8695a82009-02-10 21:25:46 +0100279 goto end;
280 }
281
Johannes Berg12375ef2009-11-25 20:30:31 +0100282 ret = drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_START,
Johannes Berg24487982009-04-23 18:52:52 +0200283 &sta->sta, tid, &start_seq_num);
Johannes Bergb8695a82009-02-10 21:25:46 +0100284#ifdef CONFIG_MAC80211_HT_DEBUG
285 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
286#endif /* CONFIG_MAC80211_HT_DEBUG */
287
288 if (ret) {
289 kfree(tid_agg_rx->reorder_buf);
290 kfree(tid_agg_rx);
291 sta->ampdu_mlme.tid_rx[tid] = NULL;
292 goto end;
293 }
294
295 /* change state and send addba resp */
296 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
297 tid_agg_rx->dialog_token = dialog_token;
298 tid_agg_rx->ssn = start_seq_num;
299 tid_agg_rx->head_seq_num = start_seq_num;
300 tid_agg_rx->buf_size = buf_size;
301 tid_agg_rx->timeout = timeout;
302 tid_agg_rx->stored_mpdu_num = 0;
303 status = WLAN_STATUS_SUCCESS;
304end:
305 spin_unlock_bh(&sta->lock);
306
307end_no_lock:
308 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
309 dialog_token, status, 1, buf_size, timeout);
310}