blob: aa858a03951cb66efe920ec5d5c969507ac7210a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Johannes Bergb8695a82009-02-10 21:25:46 +010018#include <net/mac80211.h>
19#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020020#include "driver-ops.h"
Johannes Bergb8695a82009-02-10 21:25:46 +010021
Johannes Berga87f7362010-06-10 10:21:38 +020022static void ieee80211_free_tid_rx(struct rcu_head *h)
23{
24 struct tid_ampdu_rx *tid_rx =
25 container_of(h, struct tid_ampdu_rx, rcu_head);
26 int i;
27
28 for (i = 0; i < tid_rx->buf_size; i++)
29 dev_kfree_skb(tid_rx->reorder_buf[i]);
30 kfree(tid_rx->reorder_buf);
31 kfree(tid_rx->reorder_time);
32 kfree(tid_rx);
33}
34
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020035void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
36 u16 initiator, u16 reason)
Johannes Bergb8695a82009-02-10 21:25:46 +010037{
Johannes Bergd75636e2009-02-10 21:25:53 +010038 struct ieee80211_local *local = sta->local;
Johannes Berg098a6072010-04-06 11:18:47 +020039 struct tid_ampdu_rx *tid_rx;
Johannes Bergb8695a82009-02-10 21:25:46 +010040
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020041 lockdep_assert_held(&sta->lock);
Johannes Berg098a6072010-04-06 11:18:47 +020042
Johannes Berga87f7362010-06-10 10:21:38 +020043 tid_rx = sta->ampdu_mlme.tid_rx[tid];
44
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020045 if (!tid_rx)
Johannes Bergb8695a82009-02-10 21:25:46 +010046 return;
Johannes Bergd75636e2009-02-10 21:25:53 +010047
Johannes Berga87f7362010-06-10 10:21:38 +020048 rcu_assign_pointer(sta->ampdu_mlme.tid_rx[tid], NULL);
Johannes Bergb8695a82009-02-10 21:25:46 +010049
Johannes Bergb8695a82009-02-10 21:25:46 +010050#ifdef CONFIG_MAC80211_HT_DEBUG
51 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
Johannes Bergd75636e2009-02-10 21:25:53 +010052 sta->sta.addr, tid);
Johannes Bergb8695a82009-02-10 21:25:46 +010053#endif /* CONFIG_MAC80211_HT_DEBUG */
54
Johannes Berg12375ef2009-11-25 20:30:31 +010055 if (drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_STOP,
Johannes Berg24487982009-04-23 18:52:52 +020056 &sta->sta, tid, NULL))
Johannes Bergb8695a82009-02-10 21:25:46 +010057 printk(KERN_DEBUG "HW problem - can not stop rx "
58 "aggregation for tid %d\n", tid);
59
Johannes Bergb8695a82009-02-10 21:25:46 +010060 /* check if this is a self generated aggregation halt */
Johannes Berg098a6072010-04-06 11:18:47 +020061 if (initiator == WLAN_BACK_RECIPIENT)
Johannes Bergd75636e2009-02-10 21:25:53 +010062 ieee80211_send_delba(sta->sdata, sta->sta.addr,
63 tid, 0, reason);
Johannes Bergb8695a82009-02-10 21:25:46 +010064
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020065 del_timer_sync(&tid_rx->session_timer);
Johannes Berga87f7362010-06-10 10:21:38 +020066
67 call_rcu(&tid_rx->rcu_head, ieee80211_free_tid_rx);
Johannes Bergb8695a82009-02-10 21:25:46 +010068}
69
Johannes Berg2aab4c22010-04-19 11:00:24 +020070void __ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
71 u16 initiator, u16 reason)
72{
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020073 spin_lock_bh(&sta->lock);
74 ___ieee80211_stop_rx_ba_session(sta, tid, initiator, reason);
75 spin_unlock_bh(&sta->lock);
Johannes Berg2aab4c22010-04-19 11:00:24 +020076}
77
Johannes Bergb8695a82009-02-10 21:25:46 +010078/*
79 * After accepting the AddBA Request we activated a timer,
80 * resetting it after each frame that arrives from the originator.
Johannes Bergb8695a82009-02-10 21:25:46 +010081 */
82static void sta_rx_agg_session_timer_expired(unsigned long data)
83{
84 /* not an elegant detour, but there is no choice as the timer passes
85 * only one argument, and various sta_info are needed here, so init
86 * flow in sta_info_create gives the TID as data, while the timer_to_id
87 * array gives the sta through container_of */
88 u8 *ptid = (u8 *)data;
89 u8 *timer_to_id = ptid - *ptid;
90 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
91 timer_to_tid[0]);
92
93#ifdef CONFIG_MAC80211_HT_DEBUG
94 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
95#endif
Johannes Berg7c3b1dd2010-06-10 10:21:44 +020096 set_bit(*ptid, sta->ampdu_mlme.tid_rx_timer_expired);
97 ieee80211_queue_work(&sta->local->hw, &sta->ampdu_mlme.work);
Johannes Bergb8695a82009-02-10 21:25:46 +010098}
99
100static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
101 u8 dialog_token, u16 status, u16 policy,
102 u16 buf_size, u16 timeout)
103{
Johannes Bergb8695a82009-02-10 21:25:46 +0100104 struct ieee80211_local *local = sdata->local;
105 struct sk_buff *skb;
106 struct ieee80211_mgmt *mgmt;
107 u16 capab;
108
109 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
110
111 if (!skb) {
112 printk(KERN_DEBUG "%s: failed to allocate buffer "
Johannes Berg47846c92009-11-25 17:46:19 +0100113 "for addba resp frame\n", sdata->name);
Johannes Bergb8695a82009-02-10 21:25:46 +0100114 return;
115 }
116
117 skb_reserve(skb, local->hw.extra_tx_headroom);
118 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
119 memset(mgmt, 0, 24);
120 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100121 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg8abd3f92009-02-10 21:25:47 +0100122 if (sdata->vif.type == NL80211_IFTYPE_AP ||
123 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
Johannes Berg47846c92009-11-25 17:46:19 +0100124 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +0100125 else if (sdata->vif.type == NL80211_IFTYPE_STATION)
126 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
127
Johannes Bergb8695a82009-02-10 21:25:46 +0100128 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
129 IEEE80211_STYPE_ACTION);
130
131 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
132 mgmt->u.action.category = WLAN_CATEGORY_BACK;
133 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
134 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
135
136 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
137 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
138 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
139
140 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
141 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
142 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
143
Johannes Berg62ae67b2009-11-18 18:42:05 +0100144 ieee80211_tx_skb(sdata, skb);
Johannes Bergb8695a82009-02-10 21:25:46 +0100145}
146
147void ieee80211_process_addba_request(struct ieee80211_local *local,
148 struct sta_info *sta,
149 struct ieee80211_mgmt *mgmt,
150 size_t len)
151{
152 struct ieee80211_hw *hw = &local->hw;
153 struct ieee80211_conf *conf = &hw->conf;
154 struct tid_ampdu_rx *tid_agg_rx;
155 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
156 u8 dialog_token;
157 int ret = -EOPNOTSUPP;
158
159 /* extract session parameters from addba request frame */
160 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
161 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
162 start_seq_num =
163 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
164
165 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
166 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
167 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
168 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
169
170 status = WLAN_STATUS_REQUEST_DECLINED;
171
Johannes Berg618f3562010-04-06 11:18:46 +0200172 if (test_sta_flags(sta, WLAN_STA_BLOCK_BA)) {
Sujith722f0692009-03-17 08:50:06 +0530173#ifdef CONFIG_MAC80211_HT_DEBUG
174 printk(KERN_DEBUG "Suspend in progress. "
175 "Denying ADDBA request\n");
176#endif
177 goto end_no_lock;
178 }
179
Johannes Bergb8695a82009-02-10 21:25:46 +0100180 /* sanity check for incoming parameters:
181 * check if configuration can support the BA policy
182 * and if buffer size does not exceeds max value */
183 /* XXX: check own ht delayed BA capability?? */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800184 if (((ba_policy != 1) &&
185 (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA))) ||
186 (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100187 status = WLAN_STATUS_INVALID_QOS_PARAM;
188#ifdef CONFIG_MAC80211_HT_DEBUG
189 if (net_ratelimit())
190 printk(KERN_DEBUG "AddBA Req with bad params from "
191 "%pM on tid %u. policy %d, buffer size %d\n",
192 mgmt->sa, tid, ba_policy,
193 buf_size);
194#endif /* CONFIG_MAC80211_HT_DEBUG */
195 goto end_no_lock;
196 }
197 /* determine default buffer size */
198 if (buf_size == 0) {
199 struct ieee80211_supported_band *sband;
200
201 sband = local->hw.wiphy->bands[conf->channel->band];
202 buf_size = IEEE80211_MIN_AMPDU_BUF;
203 buf_size = buf_size << sband->ht_cap.ampdu_factor;
204 }
205
206
207 /* examine state machine */
208 spin_lock_bh(&sta->lock);
209
Johannes Berga87f7362010-06-10 10:21:38 +0200210 if (sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100211#ifdef CONFIG_MAC80211_HT_DEBUG
212 if (net_ratelimit())
213 printk(KERN_DEBUG "unexpected AddBA Req from "
214 "%pM on tid %u\n",
215 mgmt->sa, tid);
216#endif /* CONFIG_MAC80211_HT_DEBUG */
217 goto end;
218 }
219
220 /* prepare A-MPDU MLME for Rx aggregation */
Johannes Berga87f7362010-06-10 10:21:38 +0200221 tid_agg_rx = kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
222 if (!tid_agg_rx) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100223#ifdef CONFIG_MAC80211_HT_DEBUG
224 if (net_ratelimit())
225 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
226 tid);
227#endif
228 goto end;
229 }
Johannes Bergb8695a82009-02-10 21:25:46 +0100230
Johannes Berga87f7362010-06-10 10:21:38 +0200231 /* rx timer */
232 tid_agg_rx->session_timer.function = sta_rx_agg_session_timer_expired;
233 tid_agg_rx->session_timer.data = (unsigned long)&sta->timer_to_tid[tid];
234 init_timer(&tid_agg_rx->session_timer);
Johannes Bergb8695a82009-02-10 21:25:46 +0100235
236 /* prepare reordering buffer */
237 tid_agg_rx->reorder_buf =
238 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
Jouni Malinen4d050f12009-05-05 20:35:14 +0300239 tid_agg_rx->reorder_time =
240 kcalloc(buf_size, sizeof(unsigned long), GFP_ATOMIC);
241 if (!tid_agg_rx->reorder_buf || !tid_agg_rx->reorder_time) {
Johannes Bergb8695a82009-02-10 21:25:46 +0100242#ifdef CONFIG_MAC80211_HT_DEBUG
243 if (net_ratelimit())
244 printk(KERN_ERR "can not allocate reordering buffer "
245 "to tid %d\n", tid);
246#endif
Jouni Malinen4d050f12009-05-05 20:35:14 +0300247 kfree(tid_agg_rx->reorder_buf);
248 kfree(tid_agg_rx->reorder_time);
Johannes Berga87f7362010-06-10 10:21:38 +0200249 kfree(tid_agg_rx);
Johannes Bergb8695a82009-02-10 21:25:46 +0100250 goto end;
251 }
252
Johannes Berg12375ef2009-11-25 20:30:31 +0100253 ret = drv_ampdu_action(local, sta->sdata, IEEE80211_AMPDU_RX_START,
Johannes Berg24487982009-04-23 18:52:52 +0200254 &sta->sta, tid, &start_seq_num);
Johannes Bergb8695a82009-02-10 21:25:46 +0100255#ifdef CONFIG_MAC80211_HT_DEBUG
256 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
257#endif /* CONFIG_MAC80211_HT_DEBUG */
258
259 if (ret) {
260 kfree(tid_agg_rx->reorder_buf);
Johannes Berga87f7362010-06-10 10:21:38 +0200261 kfree(tid_agg_rx->reorder_time);
Johannes Bergb8695a82009-02-10 21:25:46 +0100262 kfree(tid_agg_rx);
Johannes Bergb8695a82009-02-10 21:25:46 +0100263 goto end;
264 }
265
Johannes Berga87f7362010-06-10 10:21:38 +0200266 /* update data */
Johannes Bergb8695a82009-02-10 21:25:46 +0100267 tid_agg_rx->dialog_token = dialog_token;
268 tid_agg_rx->ssn = start_seq_num;
269 tid_agg_rx->head_seq_num = start_seq_num;
270 tid_agg_rx->buf_size = buf_size;
271 tid_agg_rx->timeout = timeout;
272 tid_agg_rx->stored_mpdu_num = 0;
273 status = WLAN_STATUS_SUCCESS;
Johannes Berga87f7362010-06-10 10:21:38 +0200274
275 /* activate it for RX */
276 rcu_assign_pointer(sta->ampdu_mlme.tid_rx[tid], tid_agg_rx);
Johannes Bergf955ebb2010-06-10 10:21:45 +0200277
278 if (timeout)
279 mod_timer(&tid_agg_rx->session_timer, TU_TO_EXP_TIME(timeout));
280
Johannes Bergb8695a82009-02-10 21:25:46 +0100281end:
282 spin_unlock_bh(&sta->lock);
283
284end_no_lock:
285 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
286 dialog_token, status, 1, buf_size, timeout);
287}