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 <net/mac80211.h>
18 #include "ieee80211_i.h"
20 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
21 u16 initiator, u16 reason)
23 struct ieee80211_local *local = sdata->local;
24 struct ieee80211_hw *hw = &local->hw;
30 sta = sta_info_get(local, ra);
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);
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);
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);
53 #ifdef CONFIG_MAC80211_HT_DEBUG
54 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
56 #endif /* CONFIG_MAC80211_HT_DEBUG */
58 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
59 &sta->sta, tid, NULL);
61 printk(KERN_DEBUG "HW problem - can not stop rx "
62 "aggregation for tid %d\n", tid);
64 /* shutdown timer has not expired */
65 if (initiator != WLAN_BACK_TIMER)
66 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
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);
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;
82 spin_lock_bh(&sta->lock);
84 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
86 if (!sta->ampdu_mlme.tid_rx[tid]->shutdown) {
87 kfree(sta->ampdu_mlme.tid_rx[tid]);
88 sta->ampdu_mlme.tid_rx[tid] = NULL;
91 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
92 spin_unlock_bh(&sta->lock);
98 * After accepting the AddBA Request we activated a timer,
99 * resetting it after each frame that arrives from the originator.
100 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
102 static void sta_rx_agg_session_timer_expired(unsigned long data)
104 /* not an elegant detour, but there is no choice as the timer passes
105 * only one argument, and various sta_info are needed here, so init
106 * flow in sta_info_create gives the TID as data, while the timer_to_id
107 * array gives the sta through container_of */
108 u8 *ptid = (u8 *)data;
109 u8 *timer_to_id = ptid - *ptid;
110 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
113 #ifdef CONFIG_MAC80211_HT_DEBUG
114 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
116 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
117 (u16)*ptid, WLAN_BACK_TIMER,
118 WLAN_REASON_QSTA_TIMEOUT);
121 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
122 u8 dialog_token, u16 status, u16 policy,
123 u16 buf_size, u16 timeout)
125 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
126 struct ieee80211_local *local = sdata->local;
128 struct ieee80211_mgmt *mgmt;
131 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
134 printk(KERN_DEBUG "%s: failed to allocate buffer "
135 "for addba resp frame\n", sdata->dev->name);
139 skb_reserve(skb, local->hw.extra_tx_headroom);
140 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
142 memcpy(mgmt->da, da, ETH_ALEN);
143 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
144 if (sdata->vif.type == NL80211_IFTYPE_AP ||
145 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
146 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
148 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
149 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
150 IEEE80211_STYPE_ACTION);
152 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
153 mgmt->u.action.category = WLAN_CATEGORY_BACK;
154 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
155 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
157 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
158 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
159 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
161 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
162 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
163 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
165 ieee80211_tx_skb(sdata, skb, 1);
168 void ieee80211_process_addba_request(struct ieee80211_local *local,
169 struct sta_info *sta,
170 struct ieee80211_mgmt *mgmt,
173 struct ieee80211_hw *hw = &local->hw;
174 struct ieee80211_conf *conf = &hw->conf;
175 struct tid_ampdu_rx *tid_agg_rx;
176 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
178 int ret = -EOPNOTSUPP;
180 /* extract session parameters from addba request frame */
181 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
182 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
184 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
186 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
187 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
188 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
189 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
191 status = WLAN_STATUS_REQUEST_DECLINED;
193 /* sanity check for incoming parameters:
194 * check if configuration can support the BA policy
195 * and if buffer size does not exceeds max value */
196 /* XXX: check own ht delayed BA capability?? */
197 if (((ba_policy != 1)
198 && (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA)))
199 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
200 status = WLAN_STATUS_INVALID_QOS_PARAM;
201 #ifdef CONFIG_MAC80211_HT_DEBUG
203 printk(KERN_DEBUG "AddBA Req with bad params from "
204 "%pM on tid %u. policy %d, buffer size %d\n",
205 mgmt->sa, tid, ba_policy,
207 #endif /* CONFIG_MAC80211_HT_DEBUG */
210 /* determine default buffer size */
212 struct ieee80211_supported_band *sband;
214 sband = local->hw.wiphy->bands[conf->channel->band];
215 buf_size = IEEE80211_MIN_AMPDU_BUF;
216 buf_size = buf_size << sband->ht_cap.ampdu_factor;
220 /* examine state machine */
221 spin_lock_bh(&sta->lock);
223 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
224 #ifdef CONFIG_MAC80211_HT_DEBUG
226 printk(KERN_DEBUG "unexpected AddBA Req from "
229 #endif /* CONFIG_MAC80211_HT_DEBUG */
233 /* prepare A-MPDU MLME for Rx aggregation */
234 sta->ampdu_mlme.tid_rx[tid] =
235 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
236 if (!sta->ampdu_mlme.tid_rx[tid]) {
237 #ifdef CONFIG_MAC80211_HT_DEBUG
239 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
245 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
246 sta_rx_agg_session_timer_expired;
247 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
248 (unsigned long)&sta->timer_to_tid[tid];
249 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
251 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
253 /* prepare reordering buffer */
254 tid_agg_rx->reorder_buf =
255 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
256 if (!tid_agg_rx->reorder_buf) {
257 #ifdef CONFIG_MAC80211_HT_DEBUG
259 printk(KERN_ERR "can not allocate reordering buffer "
262 kfree(sta->ampdu_mlme.tid_rx[tid]);
266 if (local->ops->ampdu_action)
267 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
268 &sta->sta, tid, &start_seq_num);
269 #ifdef CONFIG_MAC80211_HT_DEBUG
270 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
271 #endif /* CONFIG_MAC80211_HT_DEBUG */
274 kfree(tid_agg_rx->reorder_buf);
276 sta->ampdu_mlme.tid_rx[tid] = NULL;
280 /* change state and send addba resp */
281 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
282 tid_agg_rx->dialog_token = dialog_token;
283 tid_agg_rx->ssn = start_seq_num;
284 tid_agg_rx->head_seq_num = start_seq_num;
285 tid_agg_rx->buf_size = buf_size;
286 tid_agg_rx->timeout = timeout;
287 tid_agg_rx->stored_mpdu_num = 0;
288 status = WLAN_STATUS_SUCCESS;
290 spin_unlock_bh(&sta->lock);
293 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
294 dialog_token, status, 1, buf_size, timeout);