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
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.
16 #include <linux/ieee80211.h>
17 #include <linux/slab.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "driver-ops.h"
22 void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
23 u16 initiator, u16 reason)
25 struct ieee80211_local *local = sta->local;
26 struct tid_ampdu_rx *tid_rx;
29 spin_lock_bh(&sta->lock);
31 /* check if TID is in operational state */
32 if (!sta->ampdu_mlme.tid_active_rx[tid]) {
33 spin_unlock_bh(&sta->lock);
37 sta->ampdu_mlme.tid_active_rx[tid] = false;
39 tid_rx = sta->ampdu_mlme.tid_rx[tid];
41 #ifdef CONFIG_MAC80211_HT_DEBUG
42 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
44 #endif /* CONFIG_MAC80211_HT_DEBUG */
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);
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,
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;
67 kfree(tid_rx->reorder_buf);
68 kfree(tid_rx->reorder_time);
69 sta->ampdu_mlme.tid_rx[tid] = NULL;
71 spin_unlock_bh(&sta->lock);
73 del_timer_sync(&tid_rx->session_timer);
78 * After accepting the AddBA Request we activated a timer,
79 * resetting it after each frame that arrives from the originator.
81 static void sta_rx_agg_session_timer_expired(unsigned long data)
83 /* not an elegant detour, but there is no choice as the timer passes
84 * only one argument, and various sta_info are needed here, so init
85 * flow in sta_info_create gives the TID as data, while the timer_to_id
86 * array gives the sta through container_of */
87 u8 *ptid = (u8 *)data;
88 u8 *timer_to_id = ptid - *ptid;
89 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
92 #ifdef CONFIG_MAC80211_HT_DEBUG
93 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
95 __ieee80211_stop_rx_ba_session(sta, *ptid, WLAN_BACK_RECIPIENT,
96 WLAN_REASON_QSTA_TIMEOUT);
99 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
100 u8 dialog_token, u16 status, u16 policy,
101 u16 buf_size, u16 timeout)
103 struct ieee80211_local *local = sdata->local;
105 struct ieee80211_mgmt *mgmt;
108 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
111 printk(KERN_DEBUG "%s: failed to allocate buffer "
112 "for addba resp frame\n", sdata->name);
116 skb_reserve(skb, local->hw.extra_tx_headroom);
117 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
119 memcpy(mgmt->da, da, ETH_ALEN);
120 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
121 if (sdata->vif.type == NL80211_IFTYPE_AP ||
122 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
123 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
124 else if (sdata->vif.type == NL80211_IFTYPE_STATION)
125 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
127 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
128 IEEE80211_STYPE_ACTION);
130 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
131 mgmt->u.action.category = WLAN_CATEGORY_BACK;
132 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
133 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
135 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
136 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
137 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
139 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
140 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
141 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
143 ieee80211_tx_skb(sdata, skb);
146 void ieee80211_process_addba_request(struct ieee80211_local *local,
147 struct sta_info *sta,
148 struct ieee80211_mgmt *mgmt,
151 struct ieee80211_hw *hw = &local->hw;
152 struct ieee80211_conf *conf = &hw->conf;
153 struct tid_ampdu_rx *tid_agg_rx;
154 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
156 int ret = -EOPNOTSUPP;
158 /* extract session parameters from addba request frame */
159 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
160 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
162 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
164 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
165 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
166 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
167 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
169 status = WLAN_STATUS_REQUEST_DECLINED;
171 if (test_sta_flags(sta, WLAN_STA_BLOCK_BA)) {
172 #ifdef CONFIG_MAC80211_HT_DEBUG
173 printk(KERN_DEBUG "Suspend in progress. "
174 "Denying ADDBA request\n");
179 /* sanity check for incoming parameters:
180 * check if configuration can support the BA policy
181 * and if buffer size does not exceeds max value */
182 /* XXX: check own ht delayed BA capability?? */
183 if (((ba_policy != 1) &&
184 (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
185 (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
186 status = WLAN_STATUS_INVALID_QOS_PARAM;
187 #ifdef CONFIG_MAC80211_HT_DEBUG
189 printk(KERN_DEBUG "AddBA Req with bad params from "
190 "%pM on tid %u. policy %d, buffer size %d\n",
191 mgmt->sa, tid, ba_policy,
193 #endif /* CONFIG_MAC80211_HT_DEBUG */
196 /* determine default buffer size */
198 struct ieee80211_supported_band *sband;
200 sband = local->hw.wiphy->bands[conf->channel->band];
201 buf_size = IEEE80211_MIN_AMPDU_BUF;
202 buf_size = buf_size << sband->ht_cap.ampdu_factor;
206 /* examine state machine */
207 spin_lock_bh(&sta->lock);
209 if (sta->ampdu_mlme.tid_active_rx[tid]) {
210 #ifdef CONFIG_MAC80211_HT_DEBUG
212 printk(KERN_DEBUG "unexpected AddBA Req from "
215 #endif /* CONFIG_MAC80211_HT_DEBUG */
219 /* prepare A-MPDU MLME for Rx aggregation */
220 sta->ampdu_mlme.tid_rx[tid] =
221 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
222 if (!sta->ampdu_mlme.tid_rx[tid]) {
223 #ifdef CONFIG_MAC80211_HT_DEBUG
225 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
231 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
232 sta_rx_agg_session_timer_expired;
233 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
234 (unsigned long)&sta->timer_to_tid[tid];
235 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
237 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
239 /* prepare reordering buffer */
240 tid_agg_rx->reorder_buf =
241 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
242 tid_agg_rx->reorder_time =
243 kcalloc(buf_size, sizeof(unsigned long), GFP_ATOMIC);
244 if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
245 #ifdef CONFIG_MAC80211_HT_DEBUG
247 printk(KERN_ERR "can not allocate reordering buffer "
250 kfree(tid_agg_rx->reorder_buf);
251 kfree(tid_agg_rx->reorder_time);
252 kfree(sta->ampdu_mlme.tid_rx[tid]);
253 sta->ampdu_mlme.tid_rx[tid] = NULL;
257 ret = drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_START,
258 &sta->sta, tid, &start_seq_num);
259 #ifdef CONFIG_MAC80211_HT_DEBUG
260 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
261 #endif /* CONFIG_MAC80211_HT_DEBUG */
264 kfree(tid_agg_rx->reorder_buf);
266 sta->ampdu_mlme.tid_rx[tid] = NULL;
270 /* change state and send addba resp */
271 sta->ampdu_mlme.tid_active_rx[tid] = true;
272 tid_agg_rx->dialog_token = dialog_token;
273 tid_agg_rx->ssn = start_seq_num;
274 tid_agg_rx->head_seq_num = start_seq_num;
275 tid_agg_rx->buf_size = buf_size;
276 tid_agg_rx->timeout = timeout;
277 tid_agg_rx->stored_mpdu_num = 0;
278 status = WLAN_STATUS_SUCCESS;
280 spin_unlock_bh(&sta->lock);
283 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
284 dialog_token, status, 1, buf_size, timeout);