blob: d7afd0956970568a559ba11a647b8c55f1d9487c [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"
19
20void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
21 u16 initiator, u16 reason)
22{
23 struct ieee80211_local *local = sdata->local;
24 struct ieee80211_hw *hw = &local->hw;
25 struct sta_info *sta;
26 int ret, i;
27
28 rcu_read_lock();
29
30 sta = sta_info_get(local, ra);
31 if (!sta) {
32 rcu_read_unlock();
33 return;
34 }
35
36 /* check if TID is in operational state */
37 spin_lock_bh(&sta->lock);
38 if (sta->ampdu_mlme.tid_state_rx[tid]
39 != HT_AGG_STATE_OPERATIONAL) {
40 spin_unlock_bh(&sta->lock);
41 rcu_read_unlock();
42 return;
43 }
44 sta->ampdu_mlme.tid_state_rx[tid] =
45 HT_AGG_STATE_REQ_STOP_BA_MSK |
46 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
47 spin_unlock_bh(&sta->lock);
48
49 /* stop HW Rx aggregation. ampdu_action existence
50 * already verified in session init so we add the BUG_ON */
51 BUG_ON(!local->ops->ampdu_action);
52
53#ifdef CONFIG_MAC80211_HT_DEBUG
54 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
55 ra, tid);
56#endif /* CONFIG_MAC80211_HT_DEBUG */
57
58 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
59 &sta->sta, tid, NULL);
60 if (ret)
61 printk(KERN_DEBUG "HW problem - can not stop rx "
62 "aggregation for tid %d\n", tid);
63
64 /* shutdown timer has not expired */
65 if (initiator != WLAN_BACK_TIMER)
66 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
67
68 /* check if this is a self generated aggregation halt */
69 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
70 ieee80211_send_delba(sdata, ra, tid, 0, reason);
71
72 /* free the reordering buffer */
73 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
74 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
75 /* release the reordered frames */
76 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
77 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
78 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
79 }
80 }
81 /* free resources */
82 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
83 kfree(sta->ampdu_mlme.tid_rx[tid]);
84 sta->ampdu_mlme.tid_rx[tid] = NULL;
85 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
86
87 rcu_read_unlock();
88}
89
90/*
91 * After accepting the AddBA Request we activated a timer,
92 * resetting it after each frame that arrives from the originator.
93 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
94 */
95static void sta_rx_agg_session_timer_expired(unsigned long data)
96{
97 /* not an elegant detour, but there is no choice as the timer passes
98 * only one argument, and various sta_info are needed here, so init
99 * flow in sta_info_create gives the TID as data, while the timer_to_id
100 * array gives the sta through container_of */
101 u8 *ptid = (u8 *)data;
102 u8 *timer_to_id = ptid - *ptid;
103 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
104 timer_to_tid[0]);
105
106#ifdef CONFIG_MAC80211_HT_DEBUG
107 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
108#endif
109 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
110 (u16)*ptid, WLAN_BACK_TIMER,
111 WLAN_REASON_QSTA_TIMEOUT);
112}
113
114static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
115 u8 dialog_token, u16 status, u16 policy,
116 u16 buf_size, u16 timeout)
117{
118 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
119 struct ieee80211_local *local = sdata->local;
120 struct sk_buff *skb;
121 struct ieee80211_mgmt *mgmt;
122 u16 capab;
123
124 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
125
126 if (!skb) {
127 printk(KERN_DEBUG "%s: failed to allocate buffer "
128 "for addba resp frame\n", sdata->dev->name);
129 return;
130 }
131
132 skb_reserve(skb, local->hw.extra_tx_headroom);
133 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
134 memset(mgmt, 0, 24);
135 memcpy(mgmt->da, da, ETH_ALEN);
136 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg8abd3f92009-02-10 21:25:47 +0100137 if (sdata->vif.type == NL80211_IFTYPE_AP ||
138 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Bergb8695a82009-02-10 21:25:46 +0100139 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
140 else
141 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
142 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
143 IEEE80211_STYPE_ACTION);
144
145 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
146 mgmt->u.action.category = WLAN_CATEGORY_BACK;
147 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
148 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
149
150 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
151 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
152 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
153
154 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
155 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
156 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
157
158 ieee80211_tx_skb(sdata, skb, 1);
159}
160
161void ieee80211_process_addba_request(struct ieee80211_local *local,
162 struct sta_info *sta,
163 struct ieee80211_mgmt *mgmt,
164 size_t len)
165{
166 struct ieee80211_hw *hw = &local->hw;
167 struct ieee80211_conf *conf = &hw->conf;
168 struct tid_ampdu_rx *tid_agg_rx;
169 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
170 u8 dialog_token;
171 int ret = -EOPNOTSUPP;
172
173 /* extract session parameters from addba request frame */
174 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
175 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
176 start_seq_num =
177 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
178
179 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
180 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
181 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
182 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
183
184 status = WLAN_STATUS_REQUEST_DECLINED;
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_state_rx[tid] != HT_AGG_STATE_IDLE) {
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 if (!tid_agg_rx->reorder_buf) {
250#ifdef CONFIG_MAC80211_HT_DEBUG
251 if (net_ratelimit())
252 printk(KERN_ERR "can not allocate reordering buffer "
253 "to tid %d\n", tid);
254#endif
255 kfree(sta->ampdu_mlme.tid_rx[tid]);
256 goto end;
257 }
258
259 if (local->ops->ampdu_action)
260 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
261 &sta->sta, tid, &start_seq_num);
262#ifdef CONFIG_MAC80211_HT_DEBUG
263 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
264#endif /* CONFIG_MAC80211_HT_DEBUG */
265
266 if (ret) {
267 kfree(tid_agg_rx->reorder_buf);
268 kfree(tid_agg_rx);
269 sta->ampdu_mlme.tid_rx[tid] = NULL;
270 goto end;
271 }
272
273 /* change state and send addba resp */
274 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
275 tid_agg_rx->dialog_token = dialog_token;
276 tid_agg_rx->ssn = start_seq_num;
277 tid_agg_rx->head_seq_num = start_seq_num;
278 tid_agg_rx->buf_size = buf_size;
279 tid_agg_rx->timeout = timeout;
280 tid_agg_rx->stored_mpdu_num = 0;
281 status = WLAN_STATUS_SUCCESS;
282end:
283 spin_unlock_bh(&sta->lock);
284
285end_no_lock:
286 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
287 dialog_token, status, 1, buf_size, timeout);
288}